Merge branch 'gallium-noconstbuf'
[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_state.h"
33 #include "svga_hw_reg.h"
34
35
36 static INLINE unsigned
37 svga_translate_compare_func(unsigned func)
38 {
39 switch (func) {
40 case PIPE_FUNC_NEVER: return SVGA3D_CMP_NEVER;
41 case PIPE_FUNC_LESS: return SVGA3D_CMP_LESS;
42 case PIPE_FUNC_LEQUAL: return SVGA3D_CMP_LESSEQUAL;
43 case PIPE_FUNC_GREATER: return SVGA3D_CMP_GREATER;
44 case PIPE_FUNC_GEQUAL: return SVGA3D_CMP_GREATEREQUAL;
45 case PIPE_FUNC_NOTEQUAL: return SVGA3D_CMP_NOTEQUAL;
46 case PIPE_FUNC_EQUAL: return SVGA3D_CMP_EQUAL;
47 case PIPE_FUNC_ALWAYS: return SVGA3D_CMP_ALWAYS;
48 default:
49 assert(0);
50 return SVGA3D_CMP_ALWAYS;
51 }
52 }
53
54 static INLINE unsigned
55 svga_translate_stencil_op(unsigned op)
56 {
57 switch (op) {
58 case PIPE_STENCIL_OP_KEEP: return SVGA3D_STENCILOP_KEEP;
59 case PIPE_STENCIL_OP_ZERO: return SVGA3D_STENCILOP_ZERO;
60 case PIPE_STENCIL_OP_REPLACE: return SVGA3D_STENCILOP_REPLACE;
61 case PIPE_STENCIL_OP_INCR: return SVGA3D_STENCILOP_INCR;
62 case PIPE_STENCIL_OP_DECR: return SVGA3D_STENCILOP_DECR;
63 case PIPE_STENCIL_OP_INCR_WRAP: return SVGA3D_STENCILOP_INCRSAT; /* incorrect? */
64 case PIPE_STENCIL_OP_DECR_WRAP: return SVGA3D_STENCILOP_DECRSAT; /* incorrect? */
65 case PIPE_STENCIL_OP_INVERT: return SVGA3D_STENCILOP_INVERT;
66 default:
67 assert(0);
68 return SVGA3D_STENCILOP_KEEP;
69 }
70 }
71
72
73 static void *
74 svga_create_depth_stencil_state(struct pipe_context *pipe,
75 const struct pipe_depth_stencil_alpha_state *templ)
76 {
77 struct svga_depth_stencil_state *ds = CALLOC_STRUCT( svga_depth_stencil_state );
78
79 /* Don't try to figure out CW/CCW correspondence with
80 * stencil[0]/[1] at this point. Presumably this can change as
81 * back/front face are modified.
82 */
83 ds->stencil[0].enabled = templ->stencil[0].enabled;
84 if (ds->stencil[0].enabled) {
85 ds->stencil[0].func = svga_translate_compare_func(templ->stencil[0].func);
86 ds->stencil[0].fail = svga_translate_stencil_op(templ->stencil[0].fail_op);
87 ds->stencil[0].zfail = svga_translate_stencil_op(templ->stencil[0].zfail_op);
88 ds->stencil[0].pass = svga_translate_stencil_op(templ->stencil[0].zpass_op);
89
90 /* SVGA3D has one ref/mask/writemask triple shared between front &
91 * back face stencil. We really need two:
92 */
93 ds->stencil_ref = templ->stencil[0].ref_value & 0xff;
94 ds->stencil_mask = templ->stencil[0].valuemask & 0xff;
95 ds->stencil_writemask = templ->stencil[0].writemask & 0xff;
96 }
97
98
99 ds->stencil[1].enabled = templ->stencil[1].enabled;
100 if (templ->stencil[1].enabled) {
101 ds->stencil[1].func = svga_translate_compare_func(templ->stencil[1].func);
102 ds->stencil[1].fail = svga_translate_stencil_op(templ->stencil[1].fail_op);
103 ds->stencil[1].zfail = svga_translate_stencil_op(templ->stencil[1].zfail_op);
104 ds->stencil[1].pass = svga_translate_stencil_op(templ->stencil[1].zpass_op);
105
106 ds->stencil_ref = templ->stencil[1].ref_value & 0xff;
107 ds->stencil_mask = templ->stencil[1].valuemask & 0xff;
108 ds->stencil_writemask = templ->stencil[1].writemask & 0xff;
109 }
110
111
112 ds->zenable = templ->depth.enabled;
113 if (ds->zenable) {
114 ds->zfunc = svga_translate_compare_func(templ->depth.func);
115 ds->zwriteenable = templ->depth.writemask;
116 }
117
118 ds->alphatestenable = templ->alpha.enabled;
119 if (ds->alphatestenable) {
120 ds->alphafunc = svga_translate_compare_func(templ->alpha.func);
121 ds->alpharef = templ->alpha.ref_value;
122 }
123
124 return ds;
125 }
126
127 static void svga_bind_depth_stencil_state(struct pipe_context *pipe,
128 void *depth_stencil)
129 {
130 struct svga_context *svga = svga_context(pipe);
131
132 svga->curr.depth = (const struct svga_depth_stencil_state *)depth_stencil;
133 svga->dirty |= SVGA_NEW_DEPTH_STENCIL;
134 }
135
136 static void svga_delete_depth_stencil_state(struct pipe_context *pipe,
137 void *depth_stencil)
138 {
139 FREE(depth_stencil);
140 }
141
142
143
144 void svga_init_depth_stencil_functions( struct svga_context *svga )
145 {
146 svga->pipe.create_depth_stencil_alpha_state = svga_create_depth_stencil_state;
147 svga->pipe.bind_depth_stencil_alpha_state = svga_bind_depth_stencil_state;
148 svga->pipe.delete_depth_stencil_alpha_state = svga_delete_depth_stencil_state;
149 }
150
151
152
153