2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "brw_context.h"
34 #include "brw_state.h"
35 #include "brw_defines.h"
37 #include "intel_fbo.h"
38 #include "main/macros.h"
39 #include "main/enums.h"
41 static void prepare_cc_vp( struct brw_context
*brw
)
43 GLcontext
*ctx
= &brw
->intel
.ctx
;
44 struct brw_cc_viewport ccv
;
46 memset(&ccv
, 0, sizeof(ccv
));
49 if (ctx
->Transform
.DepthClamp
) {
51 ccv
.min_depth
= MIN2(ctx
->Viewport
.Near
, ctx
->Viewport
.Far
);
52 ccv
.max_depth
= MAX2(ctx
->Viewport
.Near
, ctx
->Viewport
.Far
);
58 dri_bo_unreference(brw
->cc
.vp_bo
);
59 brw
->cc
.vp_bo
= brw_cache_data(&brw
->cache
, BRW_CC_VP
, &ccv
, sizeof(ccv
),
63 const struct brw_tracked_state brw_cc_vp
= {
65 .mesa
= _NEW_VIEWPORT
| _NEW_TRANSFORM
,
66 .brw
= BRW_NEW_CONTEXT
,
69 .prepare
= prepare_cc_vp
72 struct brw_cc_unit_key
{
73 GLboolean stencil
, stencil_two_side
, color_blend
, alpha_enabled
;
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];
80 GLenum blend_eq_rgb
, blend_eq_a
;
81 GLenum blend_src_rgb
, blend_src_a
;
82 GLenum blend_dst_rgb
, blend_dst_a
;
89 GLboolean depth_test
, depth_write
;
94 * Modify blend function to force destination alpha to 1.0
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.
101 fix_xRGB_alpha(GLenum function
)
107 case GL_ONE_MINUS_DST_ALPHA
:
108 case GL_SRC_ALPHA_SATURATE
:
116 cc_unit_populate_key(struct brw_context
*brw
, struct brw_cc_unit_key
*key
)
118 GLcontext
*ctx
= &brw
->intel
.ctx
;
119 const unsigned back
= ctx
->Stencil
._BackFace
;
121 memset(key
, 0, sizeof(*key
));
123 key
->stencil
= ctx
->Stencil
._Enabled
;
124 key
->stencil_two_side
= ctx
->Stencil
._TestTwoSide
;
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];
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
];
145 if (ctx
->Color
._LogicOpEnabled
)
146 key
->logic_op
= ctx
->Color
.LogicOp
;
148 key
->logic_op
= GL_COPY
;
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
;
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.
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
);
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
;
177 key
->dither
= ctx
->Color
.DitherFlag
;
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
;
187 * Creates the state cache entry for the given CC unit key.
190 cc_unit_create_from_key(struct brw_context
*brw
, struct brw_cc_unit_key
*key
)
192 struct brw_cc_unit_state cc
;
195 memset(&cc
, 0, sizeof(cc
));
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];
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];
227 /* Not really sure about this:
229 if (key
->stencil_write_mask
[0] ||
230 (key
->stencil_two_side
&& key
->stencil_write_mask
[1]))
231 cc
.cc0
.stencil_write_enable
= 1;
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
;
246 if (eqRGB
== GL_MIN
|| eqRGB
== GL_MAX
) {
247 srcRGB
= dstRGB
= GL_ONE
;
250 if (eqA
== GL_MIN
|| eqA
== GL_MAX
) {
251 srcA
= dstA
= GL_ONE
;
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
);
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
);
262 cc
.cc3
.blend_enable
= 1;
263 cc
.cc3
.ia_blend_enable
= (srcA
!= srcRGB
||
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
;
273 UNCLAMPED_FLOAT_TO_UBYTE(cc
.cc7
.alpha_ref
.ub
[0], key
->alpha_ref
);
277 cc
.cc5
.dither_enable
= 1;
278 cc
.cc6
.y_dither_offset
= 0;
279 cc
.cc6
.x_dither_offset
= 0;
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
;
289 /* CACHE_NEW_CC_VP */
290 cc
.cc4
.cc_viewport_state_offset
= brw
->cc
.vp_bo
->offset
>> 5; /* reloc */
292 if (INTEL_DEBUG
& DEBUG_STATS
)
293 cc
.cc5
.statistics_enable
= 1;
295 bo
= brw_upload_cache(&brw
->cache
, BRW_CC_UNIT
,
301 /* Emit CC viewport relocation */
302 dri_bo_emit_reloc(bo
,
303 I915_GEM_DOMAIN_INSTRUCTION
,
306 offsetof(struct brw_cc_unit_state
, cc4
),
312 static void prepare_cc_unit( struct brw_context
*brw
)
314 struct brw_cc_unit_key key
;
316 cc_unit_populate_key(brw
, &key
);
318 dri_bo_unreference(brw
->cc
.state_bo
);
319 brw
->cc
.state_bo
= brw_search_cache(&brw
->cache
, BRW_CC_UNIT
,
324 if (brw
->cc
.state_bo
== NULL
)
325 brw
->cc
.state_bo
= cc_unit_create_from_key(brw
, &key
);
328 const struct brw_tracked_state brw_cc_unit
= {
330 .mesa
= _NEW_STENCIL
| _NEW_COLOR
| _NEW_DEPTH
,
332 .cache
= CACHE_NEW_CC_VP
334 .prepare
= prepare_cc_unit
,