Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / gallium / drivers / i965simple / brw_cc.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34
35 #include "brw_context.h"
36 #include "brw_state.h"
37 #include "brw_defines.h"
38 #include "brw_util.h"
39
40
41 static int brw_translate_compare_func(int func)
42 {
43 switch(func) {
44 case PIPE_FUNC_NEVER:
45 return BRW_COMPAREFUNCTION_NEVER;
46 case PIPE_FUNC_LESS:
47 return BRW_COMPAREFUNCTION_LESS;
48 case PIPE_FUNC_LEQUAL:
49 return BRW_COMPAREFUNCTION_LEQUAL;
50 case PIPE_FUNC_GREATER:
51 return BRW_COMPAREFUNCTION_GREATER;
52 case PIPE_FUNC_GEQUAL:
53 return BRW_COMPAREFUNCTION_GEQUAL;
54 case PIPE_FUNC_NOTEQUAL:
55 return BRW_COMPAREFUNCTION_NOTEQUAL;
56 case PIPE_FUNC_EQUAL:
57 return BRW_COMPAREFUNCTION_EQUAL;
58 case PIPE_FUNC_ALWAYS:
59 return BRW_COMPAREFUNCTION_ALWAYS;
60 }
61
62 debug_printf("Unknown value in %s: %x\n", __FUNCTION__, func);
63 return BRW_COMPAREFUNCTION_ALWAYS;
64 }
65
66 static int brw_translate_stencil_op(int op)
67 {
68 switch(op) {
69 case PIPE_STENCIL_OP_KEEP:
70 return BRW_STENCILOP_KEEP;
71 case PIPE_STENCIL_OP_ZERO:
72 return BRW_STENCILOP_ZERO;
73 case PIPE_STENCIL_OP_REPLACE:
74 return BRW_STENCILOP_REPLACE;
75 case PIPE_STENCIL_OP_INCR:
76 return BRW_STENCILOP_INCRSAT;
77 case PIPE_STENCIL_OP_DECR:
78 return BRW_STENCILOP_DECRSAT;
79 case PIPE_STENCIL_OP_INCR_WRAP:
80 return BRW_STENCILOP_INCR;
81 case PIPE_STENCIL_OP_DECR_WRAP:
82 return BRW_STENCILOP_DECR;
83 case PIPE_STENCIL_OP_INVERT:
84 return BRW_STENCILOP_INVERT;
85 default:
86 return BRW_STENCILOP_ZERO;
87 }
88 }
89
90
91 static int brw_translate_logic_op(int opcode)
92 {
93 switch(opcode) {
94 case PIPE_LOGICOP_CLEAR:
95 return BRW_LOGICOPFUNCTION_CLEAR;
96 case PIPE_LOGICOP_AND:
97 return BRW_LOGICOPFUNCTION_AND;
98 case PIPE_LOGICOP_AND_REVERSE:
99 return BRW_LOGICOPFUNCTION_AND_REVERSE;
100 case PIPE_LOGICOP_COPY:
101 return BRW_LOGICOPFUNCTION_COPY;
102 case PIPE_LOGICOP_COPY_INVERTED:
103 return BRW_LOGICOPFUNCTION_COPY_INVERTED;
104 case PIPE_LOGICOP_AND_INVERTED:
105 return BRW_LOGICOPFUNCTION_AND_INVERTED;
106 case PIPE_LOGICOP_NOOP:
107 return BRW_LOGICOPFUNCTION_NOOP;
108 case PIPE_LOGICOP_XOR:
109 return BRW_LOGICOPFUNCTION_XOR;
110 case PIPE_LOGICOP_OR:
111 return BRW_LOGICOPFUNCTION_OR;
112 case PIPE_LOGICOP_OR_INVERTED:
113 return BRW_LOGICOPFUNCTION_OR_INVERTED;
114 case PIPE_LOGICOP_NOR:
115 return BRW_LOGICOPFUNCTION_NOR;
116 case PIPE_LOGICOP_EQUIV:
117 return BRW_LOGICOPFUNCTION_EQUIV;
118 case PIPE_LOGICOP_INVERT:
119 return BRW_LOGICOPFUNCTION_INVERT;
120 case PIPE_LOGICOP_OR_REVERSE:
121 return BRW_LOGICOPFUNCTION_OR_REVERSE;
122 case PIPE_LOGICOP_NAND:
123 return BRW_LOGICOPFUNCTION_NAND;
124 case PIPE_LOGICOP_SET:
125 return BRW_LOGICOPFUNCTION_SET;
126 default:
127 return BRW_LOGICOPFUNCTION_SET;
128 }
129 }
130
131
132 static void upload_cc_vp( struct brw_context *brw )
133 {
134 struct brw_cc_viewport ccv;
135
136 memset(&ccv, 0, sizeof(ccv));
137
138 ccv.min_depth = 0.0;
139 ccv.max_depth = 1.0;
140
141 brw->cc.vp_gs_offset = brw_cache_data( &brw->cache[BRW_CC_VP], &ccv );
142 }
143
144 const struct brw_tracked_state brw_cc_vp = {
145 .dirty = {
146 .brw = BRW_NEW_SCENE,
147 .cache = 0
148 },
149 .update = upload_cc_vp
150 };
151
152
153 static void upload_cc_unit( struct brw_context *brw )
154 {
155 struct brw_cc_unit_state cc;
156
157 memset(&cc, 0, sizeof(cc));
158
159 /* BRW_NEW_DEPTH_STENCIL */
160 if (brw->attribs.DepthStencil->stencil[0].enabled) {
161 cc.cc0.stencil_enable = brw->attribs.DepthStencil->stencil[0].enabled;
162 cc.cc0.stencil_func = brw_translate_compare_func(brw->attribs.DepthStencil->stencil[0].func);
163 cc.cc0.stencil_fail_op = brw_translate_stencil_op(brw->attribs.DepthStencil->stencil[0].fail_op);
164 cc.cc0.stencil_pass_depth_fail_op = brw_translate_stencil_op(
165 brw->attribs.DepthStencil->stencil[0].zfail_op);
166 cc.cc0.stencil_pass_depth_pass_op = brw_translate_stencil_op(
167 brw->attribs.DepthStencil->stencil[0].zpass_op);
168 cc.cc1.stencil_ref = brw->attribs.DepthStencil->stencil[0].ref_value;
169 cc.cc1.stencil_write_mask = brw->attribs.DepthStencil->stencil[0].writemask;
170 cc.cc1.stencil_test_mask = brw->attribs.DepthStencil->stencil[0].valuemask;
171
172 if (brw->attribs.DepthStencil->stencil[1].enabled) {
173 cc.cc0.bf_stencil_enable = brw->attribs.DepthStencil->stencil[1].enabled;
174 cc.cc0.bf_stencil_func = brw_translate_compare_func(
175 brw->attribs.DepthStencil->stencil[1].func);
176 cc.cc0.bf_stencil_fail_op = brw_translate_stencil_op(
177 brw->attribs.DepthStencil->stencil[1].fail_op);
178 cc.cc0.bf_stencil_pass_depth_fail_op = brw_translate_stencil_op(
179 brw->attribs.DepthStencil->stencil[1].zfail_op);
180 cc.cc0.bf_stencil_pass_depth_pass_op = brw_translate_stencil_op(
181 brw->attribs.DepthStencil->stencil[1].zpass_op);
182 cc.cc1.bf_stencil_ref = brw->attribs.DepthStencil->stencil[1].ref_value;
183 cc.cc2.bf_stencil_write_mask = brw->attribs.DepthStencil->stencil[1].writemask;
184 cc.cc2.bf_stencil_test_mask = brw->attribs.DepthStencil->stencil[1].valuemask;
185 }
186
187 /* Not really sure about this:
188 */
189 if (brw->attribs.DepthStencil->stencil[0].writemask ||
190 brw->attribs.DepthStencil->stencil[1].writemask)
191 cc.cc0.stencil_write_enable = 1;
192 }
193
194 /* BRW_NEW_BLEND */
195 if (brw->attribs.Blend->logicop_enable) {
196 cc.cc2.logicop_enable = 1;
197 cc.cc5.logicop_func = brw_translate_logic_op( brw->attribs.Blend->logicop_func );
198 }
199 else if (brw->attribs.Blend->blend_enable) {
200 int eqRGB = brw->attribs.Blend->rgb_func;
201 int eqA = brw->attribs.Blend->alpha_func;
202 int srcRGB = brw->attribs.Blend->rgb_src_factor;
203 int dstRGB = brw->attribs.Blend->rgb_dst_factor;
204 int srcA = brw->attribs.Blend->alpha_src_factor;
205 int dstA = brw->attribs.Blend->alpha_dst_factor;
206
207 if (eqRGB == PIPE_BLEND_MIN || eqRGB == PIPE_BLEND_MAX) {
208 srcRGB = dstRGB = PIPE_BLENDFACTOR_ONE;
209 }
210
211 if (eqA == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MAX) {
212 srcA = dstA = PIPE_BLENDFACTOR_ONE;
213 }
214
215 cc.cc6.dest_blend_factor = brw_translate_blend_factor(dstRGB);
216 cc.cc6.src_blend_factor = brw_translate_blend_factor(srcRGB);
217 cc.cc6.blend_function = brw_translate_blend_equation( eqRGB );
218
219 cc.cc5.ia_dest_blend_factor = brw_translate_blend_factor(dstA);
220 cc.cc5.ia_src_blend_factor = brw_translate_blend_factor(srcA);
221 cc.cc5.ia_blend_function = brw_translate_blend_equation( eqA );
222
223 cc.cc3.blend_enable = 1;
224 cc.cc3.ia_blend_enable = (srcA != srcRGB ||
225 dstA != dstRGB ||
226 eqA != eqRGB);
227 }
228
229 /* BRW_NEW_ALPHATEST
230 */
231 if (brw->attribs.DepthStencil->alpha.enabled) {
232 cc.cc3.alpha_test = 1;
233 cc.cc3.alpha_test_func =
234 brw_translate_compare_func(brw->attribs.DepthStencil->alpha.func);
235
236 cc.cc7.alpha_ref.ub[0] = float_to_ubyte(brw->attribs.DepthStencil->alpha.ref_value);
237
238 cc.cc3.alpha_test_format = BRW_ALPHATEST_FORMAT_UNORM8;
239 }
240
241 if (brw->attribs.Blend->dither) {
242 cc.cc5.dither_enable = 1;
243 cc.cc6.y_dither_offset = 0;
244 cc.cc6.x_dither_offset = 0;
245 }
246
247 if (brw->attribs.DepthStencil->depth.enabled) {
248 cc.cc2.depth_test = brw->attribs.DepthStencil->depth.enabled;
249 cc.cc2.depth_test_function = brw_translate_compare_func(brw->attribs.DepthStencil->depth.func);
250 cc.cc2.depth_write_enable = brw->attribs.DepthStencil->depth.writemask;
251 }
252
253 /* CACHE_NEW_CC_VP */
254 cc.cc4.cc_viewport_state_offset = brw->cc.vp_gs_offset >> 5;
255
256 if (BRW_DEBUG & DEBUG_STATS)
257 cc.cc5.statistics_enable = 1;
258
259 brw->cc.state_gs_offset = brw_cache_data( &brw->cache[BRW_CC_UNIT], &cc );
260 }
261
262 const struct brw_tracked_state brw_cc_unit = {
263 .dirty = {
264 .brw = BRW_NEW_DEPTH_STENCIL | BRW_NEW_BLEND | BRW_NEW_ALPHA_TEST,
265 .cache = CACHE_NEW_CC_VP
266 },
267 .update = upload_cc_unit
268 };
269