Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / i965 / brw_pipe_depth.c
1
2 #include "util/u_math.h"
3 #include "util/u_memory.h"
4
5 #include "brw_context.h"
6 #include "brw_defines.h"
7
8 /* XXX: Fixme - include this to get IZ_ defines
9 */
10 #include "brw_wm.h"
11
12 static unsigned brw_translate_compare_func(unsigned func)
13 {
14 switch (func) {
15 case PIPE_FUNC_NEVER:
16 return BRW_COMPAREFUNCTION_NEVER;
17 case PIPE_FUNC_LESS:
18 return BRW_COMPAREFUNCTION_LESS;
19 case PIPE_FUNC_LEQUAL:
20 return BRW_COMPAREFUNCTION_LEQUAL;
21 case PIPE_FUNC_GREATER:
22 return BRW_COMPAREFUNCTION_GREATER;
23 case PIPE_FUNC_GEQUAL:
24 return BRW_COMPAREFUNCTION_GEQUAL;
25 case PIPE_FUNC_NOTEQUAL:
26 return BRW_COMPAREFUNCTION_NOTEQUAL;
27 case PIPE_FUNC_EQUAL:
28 return BRW_COMPAREFUNCTION_EQUAL;
29 case PIPE_FUNC_ALWAYS:
30 return BRW_COMPAREFUNCTION_ALWAYS;
31 default:
32 assert(0);
33 return BRW_COMPAREFUNCTION_ALWAYS;
34 }
35 }
36
37 static unsigned translate_stencil_op(unsigned op)
38 {
39 switch (op) {
40 case PIPE_STENCIL_OP_KEEP:
41 return BRW_STENCILOP_KEEP;
42 case PIPE_STENCIL_OP_ZERO:
43 return BRW_STENCILOP_ZERO;
44 case PIPE_STENCIL_OP_REPLACE:
45 return BRW_STENCILOP_REPLACE;
46 case PIPE_STENCIL_OP_INCR:
47 return BRW_STENCILOP_INCRSAT;
48 case PIPE_STENCIL_OP_DECR:
49 return BRW_STENCILOP_DECRSAT;
50 case PIPE_STENCIL_OP_INCR_WRAP:
51 return BRW_STENCILOP_INCR;
52 case PIPE_STENCIL_OP_DECR_WRAP:
53 return BRW_STENCILOP_DECR;
54 case PIPE_STENCIL_OP_INVERT:
55 return BRW_STENCILOP_INVERT;
56 default:
57 assert(0);
58 return BRW_STENCILOP_ZERO;
59 }
60 }
61
62 static void create_bcc_state( struct brw_depth_stencil_state *zstencil,
63 const struct pipe_depth_stencil_alpha_state *templ )
64 {
65 if (templ->stencil[0].enabled) {
66 zstencil->cc0.stencil_enable = 1;
67 zstencil->cc0.stencil_func =
68 brw_translate_compare_func(templ->stencil[0].func);
69 zstencil->cc0.stencil_fail_op =
70 translate_stencil_op(templ->stencil[0].fail_op);
71 zstencil->cc0.stencil_pass_depth_fail_op =
72 translate_stencil_op(templ->stencil[0].zfail_op);
73 zstencil->cc0.stencil_pass_depth_pass_op =
74 translate_stencil_op(templ->stencil[0].zpass_op);
75 zstencil->cc1.stencil_ref = templ->stencil[0].ref_value;
76 zstencil->cc1.stencil_write_mask = templ->stencil[0].writemask;
77 zstencil->cc1.stencil_test_mask = templ->stencil[0].valuemask;
78
79 if (templ->stencil[1].enabled) {
80 zstencil->cc0.bf_stencil_enable = 1;
81 zstencil->cc0.bf_stencil_func =
82 brw_translate_compare_func(templ->stencil[1].func);
83 zstencil->cc0.bf_stencil_fail_op =
84 translate_stencil_op(templ->stencil[1].fail_op);
85 zstencil->cc0.bf_stencil_pass_depth_fail_op =
86 translate_stencil_op(templ->stencil[1].zfail_op);
87 zstencil->cc0.bf_stencil_pass_depth_pass_op =
88 translate_stencil_op(templ->stencil[1].zpass_op);
89 zstencil->cc1.bf_stencil_ref = templ->stencil[1].ref_value;
90 zstencil->cc2.bf_stencil_write_mask = templ->stencil[1].writemask;
91 zstencil->cc2.bf_stencil_test_mask = templ->stencil[1].valuemask;
92 }
93
94 zstencil->cc0.stencil_write_enable = (zstencil->cc1.stencil_write_mask ||
95 zstencil->cc2.bf_stencil_write_mask);
96 }
97
98
99 if (templ->alpha.enabled) {
100 zstencil->cc3.alpha_test = 1;
101 zstencil->cc3.alpha_test_func = brw_translate_compare_func(templ->alpha.func);
102 zstencil->cc3.alpha_test_format = BRW_ALPHATEST_FORMAT_UNORM8;
103 zstencil->cc7.alpha_ref.ub[0] = float_to_ubyte(templ->alpha.ref_value);
104 }
105
106 if (templ->depth.enabled) {
107 zstencil->cc2.depth_test = 1;
108 zstencil->cc2.depth_test_function = brw_translate_compare_func(templ->depth.func);
109 zstencil->cc2.depth_write_enable = templ->depth.writemask;
110 }
111 }
112
113 static void create_wm_iz_state( struct brw_depth_stencil_state *zstencil )
114 {
115 if (zstencil->cc3.alpha_test)
116 zstencil->iz_lookup |= IZ_PS_KILL_ALPHATEST_BIT;
117
118 if (zstencil->cc2.depth_test)
119 zstencil->iz_lookup |= IZ_DEPTH_TEST_ENABLE_BIT;
120
121 if (zstencil->cc2.depth_write_enable)
122 zstencil->iz_lookup |= IZ_DEPTH_WRITE_ENABLE_BIT;
123
124 if (zstencil->cc0.stencil_enable)
125 zstencil->iz_lookup |= IZ_STENCIL_TEST_ENABLE_BIT;
126
127 if (zstencil->cc0.stencil_write_enable)
128 zstencil->iz_lookup |= IZ_STENCIL_WRITE_ENABLE_BIT;
129
130 }
131
132
133 static void *
134 brw_create_depth_stencil_state( struct pipe_context *pipe,
135 const struct pipe_depth_stencil_alpha_state *templ )
136 {
137 struct brw_depth_stencil_state *zstencil = CALLOC_STRUCT(brw_depth_stencil_state);
138
139 create_bcc_state( zstencil, templ );
140 create_wm_iz_state( zstencil );
141
142 return (void *)zstencil;
143 }
144
145
146 static void brw_bind_depth_stencil_state(struct pipe_context *pipe,
147 void *cso)
148 {
149 struct brw_context *brw = brw_context(pipe);
150 brw->curr.zstencil = (const struct brw_depth_stencil_state *)cso;
151 brw->state.dirty.mesa |= PIPE_NEW_DEPTH_STENCIL_ALPHA;
152 }
153
154 static void brw_delete_depth_stencil_state(struct pipe_context *pipe,
155 void *cso)
156 {
157 struct brw_context *brw = brw_context(pipe);
158 assert((const void *)cso != (const void *)brw->curr.zstencil);
159 FREE(cso);
160 }
161
162
163 void brw_pipe_depth_stencil_init( struct brw_context *brw )
164 {
165 brw->base.create_depth_stencil_alpha_state = brw_create_depth_stencil_state;
166 brw->base.bind_depth_stencil_alpha_state = brw_bind_depth_stencil_state;
167 brw->base.delete_depth_stencil_alpha_state = brw_delete_depth_stencil_state;
168 }
169
170 void brw_pipe_depth_stencil_cleanup( struct brw_context *brw )
171 {
172 }