5cca605c3f1077b809599a763963c84688e91473
[mesa.git] / src / mesa / drivers / dri / i965 / 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
33 #include "brw_context.h"
34 #include "brw_state.h"
35 #include "brw_defines.h"
36 #include "brw_util.h"
37 #include "main/macros.h"
38 #include "main/enums.h"
39
40 static void prepare_cc_vp( struct brw_context *brw )
41 {
42 GLcontext *ctx = &brw->intel.ctx;
43 struct brw_cc_viewport ccv;
44
45 memset(&ccv, 0, sizeof(ccv));
46
47 /* _NEW_TRANSOFORM */
48 if (ctx->Transform.DepthClamp) {
49 /* _NEW_VIEWPORT */
50 ccv.min_depth = MIN2(ctx->Viewport.Near, ctx->Viewport.Far);
51 ccv.max_depth = MAX2(ctx->Viewport.Near, ctx->Viewport.Far);
52 } else {
53 ccv.min_depth = 0.0;
54 ccv.max_depth = 1.0;
55 }
56
57 dri_bo_unreference(brw->cc.vp_bo);
58 brw->cc.vp_bo = brw_cache_data( &brw->cache, BRW_CC_VP, &ccv, NULL, 0 );
59 }
60
61 const struct brw_tracked_state brw_cc_vp = {
62 .dirty = {
63 .mesa = _NEW_VIEWPORT | _NEW_TRANSFORM,
64 .brw = BRW_NEW_CONTEXT,
65 .cache = 0
66 },
67 .prepare = prepare_cc_vp
68 };
69
70 struct brw_cc_unit_key {
71 GLboolean stencil, stencil_two_side, color_blend, alpha_enabled;
72
73 GLenum stencil_func[2], stencil_fail_op[2];
74 GLenum stencil_pass_depth_fail_op[2], stencil_pass_depth_pass_op[2];
75 GLubyte stencil_ref[2], stencil_write_mask[2], stencil_test_mask[2];
76 GLenum logic_op;
77
78 GLenum blend_eq_rgb, blend_eq_a;
79 GLenum blend_src_rgb, blend_src_a;
80 GLenum blend_dst_rgb, blend_dst_a;
81
82 GLenum alpha_func;
83 GLclampf alpha_ref;
84
85 GLboolean dither;
86
87 GLboolean depth_test, depth_write;
88 GLenum depth_func;
89 };
90
91 static void
92 cc_unit_populate_key(struct brw_context *brw, struct brw_cc_unit_key *key)
93 {
94 GLcontext *ctx = &brw->intel.ctx;
95 const unsigned back = ctx->Stencil._BackFace;
96
97 memset(key, 0, sizeof(*key));
98
99 key->stencil = ctx->Stencil._Enabled;
100 key->stencil_two_side = ctx->Stencil._TestTwoSide;
101
102 if (key->stencil) {
103 key->stencil_func[0] = ctx->Stencil.Function[0];
104 key->stencil_fail_op[0] = ctx->Stencil.FailFunc[0];
105 key->stencil_pass_depth_fail_op[0] = ctx->Stencil.ZFailFunc[0];
106 key->stencil_pass_depth_pass_op[0] = ctx->Stencil.ZPassFunc[0];
107 key->stencil_ref[0] = ctx->Stencil.Ref[0];
108 key->stencil_write_mask[0] = ctx->Stencil.WriteMask[0];
109 key->stencil_test_mask[0] = ctx->Stencil.ValueMask[0];
110 }
111 if (key->stencil_two_side) {
112 key->stencil_func[1] = ctx->Stencil.Function[back];
113 key->stencil_fail_op[1] = ctx->Stencil.FailFunc[back];
114 key->stencil_pass_depth_fail_op[1] = ctx->Stencil.ZFailFunc[back];
115 key->stencil_pass_depth_pass_op[1] = ctx->Stencil.ZPassFunc[back];
116 key->stencil_ref[1] = ctx->Stencil.Ref[back];
117 key->stencil_write_mask[1] = ctx->Stencil.WriteMask[back];
118 key->stencil_test_mask[1] = ctx->Stencil.ValueMask[back];
119 }
120
121 if (ctx->Color._LogicOpEnabled)
122 key->logic_op = ctx->Color.LogicOp;
123 else
124 key->logic_op = GL_COPY;
125
126 key->color_blend = ctx->Color.BlendEnabled;
127 if (key->color_blend) {
128 key->blend_eq_rgb = ctx->Color.BlendEquationRGB;
129 key->blend_eq_a = ctx->Color.BlendEquationA;
130 key->blend_src_rgb = ctx->Color.BlendSrcRGB;
131 key->blend_dst_rgb = ctx->Color.BlendDstRGB;
132 key->blend_src_a = ctx->Color.BlendSrcA;
133 key->blend_dst_a = ctx->Color.BlendDstA;
134 }
135
136 key->alpha_enabled = ctx->Color.AlphaEnabled;
137 if (key->alpha_enabled) {
138 key->alpha_func = ctx->Color.AlphaFunc;
139 key->alpha_ref = ctx->Color.AlphaRef;
140 }
141
142 key->dither = ctx->Color.DitherFlag;
143
144 key->depth_test = ctx->Depth.Test;
145 if (key->depth_test) {
146 key->depth_func = ctx->Depth.Func;
147 key->depth_write = ctx->Depth.Mask;
148 }
149 }
150
151 /**
152 * Creates the state cache entry for the given CC unit key.
153 */
154 static dri_bo *
155 cc_unit_create_from_key(struct brw_context *brw, struct brw_cc_unit_key *key)
156 {
157 struct brw_cc_unit_state cc;
158 dri_bo *bo;
159
160 memset(&cc, 0, sizeof(cc));
161
162 /* _NEW_STENCIL */
163 if (key->stencil) {
164 cc.cc0.stencil_enable = 1;
165 cc.cc0.stencil_func =
166 intel_translate_compare_func(key->stencil_func[0]);
167 cc.cc0.stencil_fail_op =
168 intel_translate_stencil_op(key->stencil_fail_op[0]);
169 cc.cc0.stencil_pass_depth_fail_op =
170 intel_translate_stencil_op(key->stencil_pass_depth_fail_op[0]);
171 cc.cc0.stencil_pass_depth_pass_op =
172 intel_translate_stencil_op(key->stencil_pass_depth_pass_op[0]);
173 cc.cc1.stencil_ref = key->stencil_ref[0];
174 cc.cc1.stencil_write_mask = key->stencil_write_mask[0];
175 cc.cc1.stencil_test_mask = key->stencil_test_mask[0];
176
177 if (key->stencil_two_side) {
178 cc.cc0.bf_stencil_enable = 1;
179 cc.cc0.bf_stencil_func =
180 intel_translate_compare_func(key->stencil_func[1]);
181 cc.cc0.bf_stencil_fail_op =
182 intel_translate_stencil_op(key->stencil_fail_op[1]);
183 cc.cc0.bf_stencil_pass_depth_fail_op =
184 intel_translate_stencil_op(key->stencil_pass_depth_fail_op[1]);
185 cc.cc0.bf_stencil_pass_depth_pass_op =
186 intel_translate_stencil_op(key->stencil_pass_depth_pass_op[1]);
187 cc.cc1.bf_stencil_ref = key->stencil_ref[1];
188 cc.cc2.bf_stencil_write_mask = key->stencil_write_mask[1];
189 cc.cc2.bf_stencil_test_mask = key->stencil_test_mask[1];
190 }
191
192 /* Not really sure about this:
193 */
194 if (key->stencil_write_mask[0] ||
195 (key->stencil_two_side && key->stencil_write_mask[1]))
196 cc.cc0.stencil_write_enable = 1;
197 }
198
199 /* _NEW_COLOR */
200 if (key->logic_op != GL_COPY) {
201 cc.cc2.logicop_enable = 1;
202 cc.cc5.logicop_func = intel_translate_logic_op(key->logic_op);
203 } else if (key->color_blend) {
204 GLenum eqRGB = key->blend_eq_rgb;
205 GLenum eqA = key->blend_eq_a;
206 GLenum srcRGB = key->blend_src_rgb;
207 GLenum dstRGB = key->blend_dst_rgb;
208 GLenum srcA = key->blend_src_a;
209 GLenum dstA = key->blend_dst_a;
210
211 if (eqRGB == GL_MIN || eqRGB == GL_MAX) {
212 srcRGB = dstRGB = GL_ONE;
213 }
214
215 if (eqA == GL_MIN || eqA == GL_MAX) {
216 srcA = dstA = GL_ONE;
217 }
218
219 cc.cc6.dest_blend_factor = brw_translate_blend_factor(dstRGB);
220 cc.cc6.src_blend_factor = brw_translate_blend_factor(srcRGB);
221 cc.cc6.blend_function = brw_translate_blend_equation(eqRGB);
222
223 cc.cc5.ia_dest_blend_factor = brw_translate_blend_factor(dstA);
224 cc.cc5.ia_src_blend_factor = brw_translate_blend_factor(srcA);
225 cc.cc5.ia_blend_function = brw_translate_blend_equation(eqA);
226
227 cc.cc3.blend_enable = 1;
228 cc.cc3.ia_blend_enable = (srcA != srcRGB ||
229 dstA != dstRGB ||
230 eqA != eqRGB);
231 }
232
233 if (key->alpha_enabled) {
234 cc.cc3.alpha_test = 1;
235 cc.cc3.alpha_test_func = intel_translate_compare_func(key->alpha_func);
236 cc.cc3.alpha_test_format = BRW_ALPHATEST_FORMAT_UNORM8;
237
238 UNCLAMPED_FLOAT_TO_UBYTE(cc.cc7.alpha_ref.ub[0], key->alpha_ref);
239 }
240
241 if (key->dither) {
242 cc.cc5.dither_enable = 1;
243 cc.cc6.y_dither_offset = 0;
244 cc.cc6.x_dither_offset = 0;
245 }
246
247 /* _NEW_DEPTH */
248 if (key->depth_test) {
249 cc.cc2.depth_test = 1;
250 cc.cc2.depth_test_function = intel_translate_compare_func(key->depth_func);
251 cc.cc2.depth_write_enable = key->depth_write;
252 }
253
254 /* CACHE_NEW_CC_VP */
255 cc.cc4.cc_viewport_state_offset = brw->cc.vp_bo->offset >> 5; /* reloc */
256
257 if (INTEL_DEBUG & DEBUG_STATS)
258 cc.cc5.statistics_enable = 1;
259
260 bo = brw_upload_cache(&brw->cache, BRW_CC_UNIT,
261 key, sizeof(*key),
262 &brw->cc.vp_bo, 1,
263 &cc, sizeof(cc),
264 NULL, NULL);
265
266 /* Emit CC viewport relocation */
267 dri_bo_emit_reloc(bo,
268 I915_GEM_DOMAIN_INSTRUCTION,
269 0,
270 0,
271 offsetof(struct brw_cc_unit_state, cc4),
272 brw->cc.vp_bo);
273
274 return bo;
275 }
276
277 static void prepare_cc_unit( struct brw_context *brw )
278 {
279 struct brw_cc_unit_key key;
280
281 cc_unit_populate_key(brw, &key);
282
283 dri_bo_unreference(brw->cc.state_bo);
284 brw->cc.state_bo = brw_search_cache(&brw->cache, BRW_CC_UNIT,
285 &key, sizeof(key),
286 &brw->cc.vp_bo, 1,
287 NULL);
288
289 if (brw->cc.state_bo == NULL)
290 brw->cc.state_bo = cc_unit_create_from_key(brw, &key);
291 }
292
293 const struct brw_tracked_state brw_cc_unit = {
294 .dirty = {
295 .mesa = _NEW_STENCIL | _NEW_COLOR | _NEW_DEPTH,
296 .brw = 0,
297 .cache = CACHE_NEW_CC_VP
298 },
299 .prepare = prepare_cc_unit,
300 };
301
302
303