1/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3/*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 *    Rob Clark <robclark@freedesktop.org>
27 */
28
29
30#include "pipe/p_state.h"
31#include "util/u_string.h"
32#include "util/u_memory.h"
33
34#include "fd2_zsa.h"
35#include "fd2_context.h"
36#include "fd2_util.h"
37
38void *
39fd2_zsa_state_create(struct pipe_context *pctx,
40		const struct pipe_depth_stencil_alpha_state *cso)
41{
42	struct fd2_zsa_stateobj *so;
43
44	so = CALLOC_STRUCT(fd2_zsa_stateobj);
45	if (!so)
46		return NULL;
47
48	so->base = *cso;
49
50	so->rb_depthcontrol |=
51		A2XX_RB_DEPTHCONTROL_ZFUNC(cso->depth.func); /* maps 1:1 */
52
53	if (cso->depth.enabled)
54		so->rb_depthcontrol |= A2XX_RB_DEPTHCONTROL_Z_ENABLE;
55	if (cso->depth.writemask)
56		so->rb_depthcontrol |= A2XX_RB_DEPTHCONTROL_Z_WRITE_ENABLE;
57
58	if (cso->stencil[0].enabled) {
59		const struct pipe_stencil_state *s = &cso->stencil[0];
60
61		so->rb_depthcontrol |=
62			A2XX_RB_DEPTHCONTROL_STENCIL_ENABLE |
63			A2XX_RB_DEPTHCONTROL_STENCILFUNC(s->func) | /* maps 1:1 */
64			A2XX_RB_DEPTHCONTROL_STENCILFAIL(fd_stencil_op(s->fail_op)) |
65			A2XX_RB_DEPTHCONTROL_STENCILZPASS(fd_stencil_op(s->zpass_op)) |
66			A2XX_RB_DEPTHCONTROL_STENCILZFAIL(fd_stencil_op(s->zfail_op));
67		so->rb_stencilrefmask |=
68			0xff000000 | /* ??? */
69			A2XX_RB_STENCILREFMASK_STENCILWRITEMASK(s->writemask) |
70			A2XX_RB_STENCILREFMASK_STENCILMASK(s->valuemask);
71
72		if (cso->stencil[1].enabled) {
73			const struct pipe_stencil_state *bs = &cso->stencil[1];
74
75			so->rb_depthcontrol |=
76				A2XX_RB_DEPTHCONTROL_BACKFACE_ENABLE |
77				A2XX_RB_DEPTHCONTROL_STENCILFUNC_BF(bs->func) | /* maps 1:1 */
78				A2XX_RB_DEPTHCONTROL_STENCILFAIL_BF(fd_stencil_op(bs->fail_op)) |
79				A2XX_RB_DEPTHCONTROL_STENCILZPASS_BF(fd_stencil_op(bs->zpass_op)) |
80				A2XX_RB_DEPTHCONTROL_STENCILZFAIL_BF(fd_stencil_op(bs->zfail_op));
81			so->rb_stencilrefmask_bf |=
82				0xff000000 | /* ??? */
83				A2XX_RB_STENCILREFMASK_STENCILWRITEMASK(bs->writemask) |
84				A2XX_RB_STENCILREFMASK_STENCILMASK(bs->valuemask);
85		}
86	}
87
88	if (cso->alpha.enabled) {
89		so->rb_colorcontrol =
90			A2XX_RB_COLORCONTROL_ALPHA_FUNC(cso->alpha.func) |
91			A2XX_RB_COLORCONTROL_ALPHA_TEST_ENABLE;
92		so->rb_alpha_ref = fui(cso->alpha.ref_value);
93	}
94
95	return so;
96}
97