Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / svga / svga_pipe_depthstencil.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "pipe/p_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30
31 #include "svga_context.h"
32 #include "svga_hw_reg.h"
33
34
35 static INLINE unsigned
36 svga_translate_compare_func(unsigned func)
37 {
38 switch (func) {
39 case PIPE_FUNC_NEVER: return SVGA3D_CMP_NEVER;
40 case PIPE_FUNC_LESS: return SVGA3D_CMP_LESS;
41 case PIPE_FUNC_LEQUAL: return SVGA3D_CMP_LESSEQUAL;
42 case PIPE_FUNC_GREATER: return SVGA3D_CMP_GREATER;
43 case PIPE_FUNC_GEQUAL: return SVGA3D_CMP_GREATEREQUAL;
44 case PIPE_FUNC_NOTEQUAL: return SVGA3D_CMP_NOTEQUAL;
45 case PIPE_FUNC_EQUAL: return SVGA3D_CMP_EQUAL;
46 case PIPE_FUNC_ALWAYS: return SVGA3D_CMP_ALWAYS;
47 default:
48 assert(0);
49 return SVGA3D_CMP_ALWAYS;
50 }
51 }
52
53 static INLINE unsigned
54 svga_translate_stencil_op(unsigned op)
55 {
56 switch (op) {
57 case PIPE_STENCIL_OP_KEEP: return SVGA3D_STENCILOP_KEEP;
58 case PIPE_STENCIL_OP_ZERO: return SVGA3D_STENCILOP_ZERO;
59 case PIPE_STENCIL_OP_REPLACE: return SVGA3D_STENCILOP_REPLACE;
60 case PIPE_STENCIL_OP_INCR: return SVGA3D_STENCILOP_INCR;
61 case PIPE_STENCIL_OP_DECR: return SVGA3D_STENCILOP_DECR;
62 case PIPE_STENCIL_OP_INCR_WRAP: return SVGA3D_STENCILOP_INCRSAT; /* incorrect? */
63 case PIPE_STENCIL_OP_DECR_WRAP: return SVGA3D_STENCILOP_DECRSAT; /* incorrect? */
64 case PIPE_STENCIL_OP_INVERT: return SVGA3D_STENCILOP_INVERT;
65 default:
66 assert(0);
67 return SVGA3D_STENCILOP_KEEP;
68 }
69 }
70
71
72 static void *
73 svga_create_depth_stencil_state(struct pipe_context *pipe,
74 const struct pipe_depth_stencil_alpha_state *templ)
75 {
76 struct svga_depth_stencil_state *ds = CALLOC_STRUCT( svga_depth_stencil_state );
77
78 /* Don't try to figure out CW/CCW correspondence with
79 * stencil[0]/[1] at this point. Presumably this can change as
80 * back/front face are modified.
81 */
82 ds->stencil[0].enabled = templ->stencil[0].enabled;
83 if (ds->stencil[0].enabled) {
84 ds->stencil[0].func = svga_translate_compare_func(templ->stencil[0].func);
85 ds->stencil[0].fail = svga_translate_stencil_op(templ->stencil[0].fail_op);
86 ds->stencil[0].zfail = svga_translate_stencil_op(templ->stencil[0].zfail_op);
87 ds->stencil[0].pass = svga_translate_stencil_op(templ->stencil[0].zpass_op);
88
89 /* SVGA3D has one ref/mask/writemask triple shared between front &
90 * back face stencil. We really need two:
91 */
92 ds->stencil_ref = templ->stencil[0].ref_value & 0xff;
93 ds->stencil_mask = templ->stencil[0].valuemask & 0xff;
94 ds->stencil_writemask = templ->stencil[0].writemask & 0xff;
95 }
96
97
98 ds->stencil[1].enabled = templ->stencil[1].enabled;
99 if (templ->stencil[1].enabled) {
100 ds->stencil[1].func = svga_translate_compare_func(templ->stencil[1].func);
101 ds->stencil[1].fail = svga_translate_stencil_op(templ->stencil[1].fail_op);
102 ds->stencil[1].zfail = svga_translate_stencil_op(templ->stencil[1].zfail_op);
103 ds->stencil[1].pass = svga_translate_stencil_op(templ->stencil[1].zpass_op);
104
105 ds->stencil_ref = templ->stencil[1].ref_value & 0xff;
106 ds->stencil_mask = templ->stencil[1].valuemask & 0xff;
107 ds->stencil_writemask = templ->stencil[1].writemask & 0xff;
108 }
109
110
111 ds->zenable = templ->depth.enabled;
112 if (ds->zenable) {
113 ds->zfunc = svga_translate_compare_func(templ->depth.func);
114 ds->zwriteenable = templ->depth.writemask;
115 }
116
117 ds->alphatestenable = templ->alpha.enabled;
118 if (ds->alphatestenable) {
119 ds->alphafunc = svga_translate_compare_func(templ->alpha.func);
120 ds->alpharef = templ->alpha.ref_value;
121 }
122
123 return ds;
124 }
125
126 static void svga_bind_depth_stencil_state(struct pipe_context *pipe,
127 void *depth_stencil)
128 {
129 struct svga_context *svga = svga_context(pipe);
130
131 svga->curr.depth = (const struct svga_depth_stencil_state *)depth_stencil;
132 svga->dirty |= SVGA_NEW_DEPTH_STENCIL;
133 }
134
135 static void svga_delete_depth_stencil_state(struct pipe_context *pipe,
136 void *depth_stencil)
137 {
138 FREE(depth_stencil);
139 }
140
141
142
143 void svga_init_depth_stencil_functions( struct svga_context *svga )
144 {
145 svga->pipe.create_depth_stencil_alpha_state = svga_create_depth_stencil_state;
146 svga->pipe.bind_depth_stencil_alpha_state = svga_bind_depth_stencil_state;
147 svga->pipe.delete_depth_stencil_alpha_state = svga_delete_depth_stencil_state;
148 }
149
150
151
152