i965: Alphabetize brw_tracked_state flags and use a consistent style.
[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 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 <keithw@vmware.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/stencil.h"
39 #include "intel_batchbuffer.h"
40
41 static void
42 brw_upload_cc_vp(struct brw_context *brw)
43 {
44 struct gl_context *ctx = &brw->ctx;
45 struct brw_cc_viewport *ccv;
46
47 ccv = brw_state_batch(brw, AUB_TRACE_CC_VP_STATE,
48 sizeof(*ccv) * ctx->Const.MaxViewports, 32,
49 &brw->cc.vp_offset);
50
51 /* _NEW_TRANSFORM */
52 for (unsigned i = 0; i < ctx->Const.MaxViewports; i++) {
53 if (ctx->Transform.DepthClamp) {
54 /* _NEW_VIEWPORT */
55 ccv[i].min_depth = MIN2(ctx->ViewportArray[i].Near,
56 ctx->ViewportArray[i].Far);
57 ccv[i].max_depth = MAX2(ctx->ViewportArray[i].Near,
58 ctx->ViewportArray[i].Far);
59 } else {
60 ccv[i].min_depth = 0.0;
61 ccv[i].max_depth = 1.0;
62 }
63 }
64
65 brw->state.dirty.cache |= CACHE_NEW_CC_VP;
66 }
67
68 const struct brw_tracked_state brw_cc_vp = {
69 .dirty = {
70 .mesa = _NEW_TRANSFORM |
71 _NEW_VIEWPORT,
72 .brw = BRW_NEW_BATCH,
73 .cache = 0
74 },
75 .emit = brw_upload_cc_vp
76 };
77
78 /**
79 * Modify blend function to force destination alpha to 1.0
80 *
81 * If \c function specifies a blend function that uses destination alpha,
82 * replace it with a function that hard-wires destination alpha to 1.0. This
83 * is used when rendering to xRGB targets.
84 */
85 GLenum
86 brw_fix_xRGB_alpha(GLenum function)
87 {
88 switch (function) {
89 case GL_DST_ALPHA:
90 return GL_ONE;
91
92 case GL_ONE_MINUS_DST_ALPHA:
93 case GL_SRC_ALPHA_SATURATE:
94 return GL_ZERO;
95 }
96
97 return function;
98 }
99
100 /**
101 * Creates a CC unit packet from the current blend state.
102 */
103 static void upload_cc_unit(struct brw_context *brw)
104 {
105 struct gl_context *ctx = &brw->ctx;
106 struct brw_cc_unit_state *cc;
107
108 cc = brw_state_batch(brw, AUB_TRACE_CC_STATE,
109 sizeof(*cc), 64, &brw->cc.state_offset);
110 memset(cc, 0, sizeof(*cc));
111
112 /* _NEW_STENCIL | _NEW_BUFFERS */
113 if (ctx->Stencil._Enabled) {
114 const unsigned back = ctx->Stencil._BackFace;
115
116 cc->cc0.stencil_enable = 1;
117 cc->cc0.stencil_func =
118 intel_translate_compare_func(ctx->Stencil.Function[0]);
119 cc->cc0.stencil_fail_op =
120 intel_translate_stencil_op(ctx->Stencil.FailFunc[0]);
121 cc->cc0.stencil_pass_depth_fail_op =
122 intel_translate_stencil_op(ctx->Stencil.ZFailFunc[0]);
123 cc->cc0.stencil_pass_depth_pass_op =
124 intel_translate_stencil_op(ctx->Stencil.ZPassFunc[0]);
125 cc->cc1.stencil_ref = _mesa_get_stencil_ref(ctx, 0);
126 cc->cc1.stencil_write_mask = ctx->Stencil.WriteMask[0];
127 cc->cc1.stencil_test_mask = ctx->Stencil.ValueMask[0];
128
129 if (ctx->Stencil._TestTwoSide) {
130 cc->cc0.bf_stencil_enable = 1;
131 cc->cc0.bf_stencil_func =
132 intel_translate_compare_func(ctx->Stencil.Function[back]);
133 cc->cc0.bf_stencil_fail_op =
134 intel_translate_stencil_op(ctx->Stencil.FailFunc[back]);
135 cc->cc0.bf_stencil_pass_depth_fail_op =
136 intel_translate_stencil_op(ctx->Stencil.ZFailFunc[back]);
137 cc->cc0.bf_stencil_pass_depth_pass_op =
138 intel_translate_stencil_op(ctx->Stencil.ZPassFunc[back]);
139 cc->cc1.bf_stencil_ref = _mesa_get_stencil_ref(ctx, back);
140 cc->cc2.bf_stencil_write_mask = ctx->Stencil.WriteMask[back];
141 cc->cc2.bf_stencil_test_mask = ctx->Stencil.ValueMask[back];
142 }
143
144 /* Not really sure about this:
145 */
146 if (ctx->Stencil.WriteMask[0] ||
147 (ctx->Stencil._TestTwoSide && ctx->Stencil.WriteMask[back]))
148 cc->cc0.stencil_write_enable = 1;
149 }
150
151 /* _NEW_COLOR */
152 if (ctx->Color.ColorLogicOpEnabled && ctx->Color.LogicOp != GL_COPY) {
153 cc->cc2.logicop_enable = 1;
154 cc->cc5.logicop_func = intel_translate_logic_op(ctx->Color.LogicOp);
155 } else if (ctx->Color.BlendEnabled) {
156 GLenum eqRGB = ctx->Color.Blend[0].EquationRGB;
157 GLenum eqA = ctx->Color.Blend[0].EquationA;
158 GLenum srcRGB = ctx->Color.Blend[0].SrcRGB;
159 GLenum dstRGB = ctx->Color.Blend[0].DstRGB;
160 GLenum srcA = ctx->Color.Blend[0].SrcA;
161 GLenum dstA = ctx->Color.Blend[0].DstA;
162
163 /* If the renderbuffer is XRGB, we have to frob the blend function to
164 * force the destination alpha to 1.0. This means replacing GL_DST_ALPHA
165 * with GL_ONE and GL_ONE_MINUS_DST_ALPHA with GL_ZERO.
166 */
167 if (ctx->DrawBuffer->Visual.alphaBits == 0) {
168 srcRGB = brw_fix_xRGB_alpha(srcRGB);
169 srcA = brw_fix_xRGB_alpha(srcA);
170 dstRGB = brw_fix_xRGB_alpha(dstRGB);
171 dstA = brw_fix_xRGB_alpha(dstA);
172 }
173
174 if (eqRGB == GL_MIN || eqRGB == GL_MAX) {
175 srcRGB = dstRGB = GL_ONE;
176 }
177
178 if (eqA == GL_MIN || eqA == GL_MAX) {
179 srcA = dstA = GL_ONE;
180 }
181
182 cc->cc6.dest_blend_factor = brw_translate_blend_factor(dstRGB);
183 cc->cc6.src_blend_factor = brw_translate_blend_factor(srcRGB);
184 cc->cc6.blend_function = brw_translate_blend_equation(eqRGB);
185
186 cc->cc5.ia_dest_blend_factor = brw_translate_blend_factor(dstA);
187 cc->cc5.ia_src_blend_factor = brw_translate_blend_factor(srcA);
188 cc->cc5.ia_blend_function = brw_translate_blend_equation(eqA);
189
190 cc->cc3.blend_enable = 1;
191 cc->cc3.ia_blend_enable = (srcA != srcRGB ||
192 dstA != dstRGB ||
193 eqA != eqRGB);
194 }
195
196 /* _NEW_BUFFERS */
197 if (ctx->Color.AlphaEnabled && ctx->DrawBuffer->_NumColorDrawBuffers <= 1) {
198 cc->cc3.alpha_test = 1;
199 cc->cc3.alpha_test_func =
200 intel_translate_compare_func(ctx->Color.AlphaFunc);
201 cc->cc3.alpha_test_format = BRW_ALPHATEST_FORMAT_UNORM8;
202
203 UNCLAMPED_FLOAT_TO_UBYTE(cc->cc7.alpha_ref.ub[0], ctx->Color.AlphaRef);
204 }
205
206 if (ctx->Color.DitherFlag) {
207 cc->cc5.dither_enable = 1;
208 cc->cc6.y_dither_offset = 0;
209 cc->cc6.x_dither_offset = 0;
210 }
211
212 /* _NEW_DEPTH */
213 if (ctx->Depth.Test) {
214 cc->cc2.depth_test = 1;
215 cc->cc2.depth_test_function =
216 intel_translate_compare_func(ctx->Depth.Func);
217 cc->cc2.depth_write_enable = ctx->Depth.Mask;
218 }
219
220 if (brw->stats_wm || unlikely(INTEL_DEBUG & DEBUG_STATS))
221 cc->cc5.statistics_enable = 1;
222
223 /* CACHE_NEW_CC_VP */
224 cc->cc4.cc_viewport_state_offset = (brw->batch.bo->offset64 +
225 brw->cc.vp_offset) >> 5; /* reloc */
226
227 brw->state.dirty.cache |= CACHE_NEW_CC_UNIT;
228
229 /* Emit CC viewport relocation */
230 drm_intel_bo_emit_reloc(brw->batch.bo,
231 (brw->cc.state_offset +
232 offsetof(struct brw_cc_unit_state, cc4)),
233 brw->batch.bo, brw->cc.vp_offset,
234 I915_GEM_DOMAIN_INSTRUCTION, 0);
235 }
236
237 const struct brw_tracked_state brw_cc_unit = {
238 .dirty = {
239 .mesa = _NEW_BUFFERS |
240 _NEW_COLOR |
241 _NEW_DEPTH |
242 _NEW_STENCIL,
243 .brw = BRW_NEW_BATCH |
244 BRW_NEW_STATS_WM,
245 .cache = CACHE_NEW_CC_VP
246 },
247 .emit = upload_cc_unit,
248 };
249
250 static void upload_blend_constant_color(struct brw_context *brw)
251 {
252 struct gl_context *ctx = &brw->ctx;
253
254 BEGIN_BATCH(5);
255 OUT_BATCH(_3DSTATE_BLEND_CONSTANT_COLOR << 16 | (5-2));
256 OUT_BATCH_F(ctx->Color.BlendColorUnclamped[0]);
257 OUT_BATCH_F(ctx->Color.BlendColorUnclamped[1]);
258 OUT_BATCH_F(ctx->Color.BlendColorUnclamped[2]);
259 OUT_BATCH_F(ctx->Color.BlendColorUnclamped[3]);
260 ADVANCE_BATCH();
261 }
262
263 const struct brw_tracked_state brw_blend_constant_color = {
264 .dirty = {
265 .mesa = _NEW_COLOR,
266 .brw = BRW_NEW_CONTEXT,
267 .cache = 0
268 },
269 .emit = upload_blend_constant_color
270 };