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