dbcdc5b86936ddc7687dfc9d4c702b48e81572a9
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_cc.c
1 /*
2 * Copyright © 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "brw_context.h"
29 #include "brw_state.h"
30 #include "brw_defines.h"
31 #include "brw_util.h"
32 #include "intel_batchbuffer.h"
33 #include "main/macros.h"
34
35 struct gen6_blend_state_key {
36 GLboolean color_blend, alpha_enabled;
37 GLboolean dither;
38 GLboolean color_mask[BRW_MAX_DRAW_BUFFERS][4];
39
40 GLenum logic_op;
41
42 GLenum blend_eq_rgb, blend_eq_a;
43 GLenum blend_src_rgb, blend_src_a;
44 GLenum blend_dst_rgb, blend_dst_a;
45
46 GLenum alpha_func;
47 };
48
49 static void
50 blend_state_populate_key(struct brw_context *brw,
51 struct gen6_blend_state_key *key)
52 {
53 struct gl_context *ctx = &brw->intel.ctx;
54
55 memset(key, 0, sizeof(*key));
56
57 /* _NEW_COLOR */
58 memcpy(key->color_mask, ctx->Color.ColorMask, sizeof(key->color_mask));
59
60 /* _NEW_COLOR */
61 if (ctx->Color._LogicOpEnabled)
62 key->logic_op = ctx->Color.LogicOp;
63 else
64 key->logic_op = GL_COPY;
65
66 /* _NEW_COLOR */
67 key->color_blend = ctx->Color.BlendEnabled;
68 if (key->color_blend) {
69 key->blend_eq_rgb = ctx->Color.BlendEquationRGB;
70 key->blend_eq_a = ctx->Color.BlendEquationA;
71 key->blend_src_rgb = ctx->Color.BlendSrcRGB;
72 key->blend_dst_rgb = ctx->Color.BlendDstRGB;
73 key->blend_src_a = ctx->Color.BlendSrcA;
74 key->blend_dst_a = ctx->Color.BlendDstA;
75 }
76
77 /* _NEW_COLOR */
78 key->alpha_enabled = ctx->Color.AlphaEnabled;
79 if (key->alpha_enabled) {
80 key->alpha_func = ctx->Color.AlphaFunc;
81 }
82
83 /* _NEW_COLOR */
84 key->dither = ctx->Color.DitherFlag;
85 }
86
87 /**
88 * Creates the state cache entry for the given CC unit key.
89 */
90 static drm_intel_bo *
91 blend_state_create_from_key(struct brw_context *brw,
92 struct gen6_blend_state_key *key)
93 {
94 struct gen6_blend_state blend[BRW_MAX_DRAW_BUFFERS];
95 drm_intel_bo *bo;
96 int b;
97
98 memset(&blend, 0, sizeof(blend));
99
100 for (b = 0; b < BRW_MAX_DRAW_BUFFERS; b++) {
101 if (key->logic_op != GL_COPY) {
102 blend[b].blend1.logic_op_enable = 1;
103 blend[b].blend1.logic_op_func = intel_translate_logic_op(key->logic_op);
104 } else if (key->color_blend & (1 << b)) {
105 GLenum eqRGB = key->blend_eq_rgb;
106 GLenum eqA = key->blend_eq_a;
107 GLenum srcRGB = key->blend_src_rgb;
108 GLenum dstRGB = key->blend_dst_rgb;
109 GLenum srcA = key->blend_src_a;
110 GLenum dstA = key->blend_dst_a;
111
112 if (eqRGB == GL_MIN || eqRGB == GL_MAX) {
113 srcRGB = dstRGB = GL_ONE;
114 }
115
116 if (eqA == GL_MIN || eqA == GL_MAX) {
117 srcA = dstA = GL_ONE;
118 }
119
120 blend[b].blend0.dest_blend_factor = brw_translate_blend_factor(dstRGB);
121 blend[b].blend0.source_blend_factor = brw_translate_blend_factor(srcRGB);
122 blend[b].blend0.blend_func = brw_translate_blend_equation(eqRGB);
123
124 blend[b].blend0.ia_dest_blend_factor = brw_translate_blend_factor(dstA);
125 blend[b].blend0.ia_source_blend_factor = brw_translate_blend_factor(srcA);
126 blend[b].blend0.ia_blend_func = brw_translate_blend_equation(eqA);
127
128 blend[b].blend0.blend_enable = 1;
129 blend[b].blend0.ia_blend_enable = (srcA != srcRGB ||
130 dstA != dstRGB ||
131 eqA != eqRGB);
132 }
133
134 if (key->alpha_enabled) {
135 blend[b].blend1.alpha_test_enable = 1;
136 blend[b].blend1.alpha_test_func = intel_translate_compare_func(key->alpha_func);
137
138 }
139
140 if (key->dither) {
141 blend[b].blend1.dither_enable = 1;
142 blend[b].blend1.y_dither_offset = 0;
143 blend[b].blend1.x_dither_offset = 0;
144 }
145
146 blend[b].blend1.write_disable_r = !key->color_mask[b][0];
147 blend[b].blend1.write_disable_g = !key->color_mask[b][1];
148 blend[b].blend1.write_disable_b = !key->color_mask[b][2];
149 blend[b].blend1.write_disable_a = !key->color_mask[b][3];
150 }
151
152 bo = brw_upload_cache(&brw->cache, BRW_BLEND_STATE,
153 key, sizeof(*key),
154 NULL, 0,
155 &blend, sizeof(blend));
156
157 return bo;
158 }
159
160 static void
161 prepare_blend_state(struct brw_context *brw)
162 {
163 struct gen6_blend_state_key key;
164
165 blend_state_populate_key(brw, &key);
166
167 drm_intel_bo_unreference(brw->cc.blend_state_bo);
168 brw->cc.blend_state_bo = brw_search_cache(&brw->cache, BRW_BLEND_STATE,
169 &key, sizeof(key),
170 NULL, 0,
171 NULL);
172
173 if (brw->cc.blend_state_bo == NULL)
174 brw->cc.blend_state_bo = blend_state_create_from_key(brw, &key);
175 }
176
177 const struct brw_tracked_state gen6_blend_state = {
178 .dirty = {
179 .mesa = _NEW_COLOR,
180 .brw = 0,
181 .cache = 0,
182 },
183 .prepare = prepare_blend_state,
184 };
185
186 struct gen6_color_calc_state_key {
187 float blend_constant_color[4];
188 GLclampf alpha_ref;
189 GLubyte stencil_ref[2];
190 };
191
192 static void
193 color_calc_state_populate_key(struct brw_context *brw,
194 struct gen6_color_calc_state_key *key)
195 {
196 struct gl_context *ctx = &brw->intel.ctx;
197
198 memset(key, 0, sizeof(*key));
199
200 /* _NEW_STENCIL */
201 if (ctx->Stencil._Enabled) {
202 const unsigned back = ctx->Stencil._BackFace;
203
204 key->stencil_ref[0] = ctx->Stencil.Ref[0];
205 if (ctx->Stencil._TestTwoSide)
206 key->stencil_ref[1] = ctx->Stencil.Ref[back];
207 }
208
209 /* _NEW_COLOR */
210 if (ctx->Color.AlphaEnabled)
211 key->alpha_ref = ctx->Color.AlphaRef;
212
213 key->blend_constant_color[0] = ctx->Color.BlendColor[0];
214 key->blend_constant_color[1] = ctx->Color.BlendColor[1];
215 key->blend_constant_color[2] = ctx->Color.BlendColor[2];
216 key->blend_constant_color[3] = ctx->Color.BlendColor[3];
217 }
218
219 /**
220 * Creates the state cache entry for the given CC state key.
221 */
222 static drm_intel_bo *
223 color_calc_state_create_from_key(struct brw_context *brw,
224 struct gen6_color_calc_state_key *key)
225 {
226 struct gen6_color_calc_state cc;
227 drm_intel_bo *bo;
228
229 memset(&cc, 0, sizeof(cc));
230
231 cc.cc0.alpha_test_format = BRW_ALPHATEST_FORMAT_UNORM8;
232 UNCLAMPED_FLOAT_TO_UBYTE(cc.cc1.alpha_ref_fi.ui, key->alpha_ref);
233
234 cc.cc0.stencil_ref = key->stencil_ref[0];
235 cc.cc0.bf_stencil_ref = key->stencil_ref[1];
236
237 cc.constant_r = key->blend_constant_color[0];
238 cc.constant_g = key->blend_constant_color[1];
239 cc.constant_b = key->blend_constant_color[2];
240 cc.constant_a = key->blend_constant_color[3];
241
242 bo = brw_upload_cache(&brw->cache, BRW_COLOR_CALC_STATE,
243 key, sizeof(*key),
244 NULL, 0,
245 &cc, sizeof(cc));
246
247 return bo;
248 }
249
250 static void
251 prepare_color_calc_state(struct brw_context *brw)
252 {
253 struct gen6_color_calc_state_key key;
254
255 color_calc_state_populate_key(brw, &key);
256
257 drm_intel_bo_unreference(brw->cc.state_bo);
258 brw->cc.state_bo = brw_search_cache(&brw->cache, BRW_COLOR_CALC_STATE,
259 &key, sizeof(key),
260 NULL, 0,
261 NULL);
262
263 if (brw->cc.state_bo == NULL)
264 brw->cc.state_bo = color_calc_state_create_from_key(brw, &key);
265 }
266
267 const struct brw_tracked_state gen6_color_calc_state = {
268 .dirty = {
269 .mesa = _NEW_COLOR | _NEW_STENCIL,
270 .brw = 0,
271 .cache = 0,
272 },
273 .prepare = prepare_color_calc_state,
274 };
275
276 static void upload_cc_state_pointers(struct brw_context *brw)
277 {
278 struct intel_context *intel = &brw->intel;
279
280 BEGIN_BATCH(4);
281 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS << 16 | (4 - 2));
282 OUT_RELOC(brw->cc.blend_state_bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 1);
283 OUT_RELOC(brw->cc.depth_stencil_state_bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 1);
284 OUT_RELOC(brw->cc.state_bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 1);
285 ADVANCE_BATCH();
286 }
287
288
289 static void prepare_cc_state_pointers(struct brw_context *brw)
290 {
291 brw_add_validated_bo(brw, brw->cc.state_bo);
292 brw_add_validated_bo(brw, brw->cc.blend_state_bo);
293 brw_add_validated_bo(brw, brw->cc.depth_stencil_state_bo);
294 }
295
296 const struct brw_tracked_state gen6_cc_state_pointers = {
297 .dirty = {
298 .mesa = 0,
299 .brw = BRW_NEW_BATCH,
300 .cache = (CACHE_NEW_BLEND_STATE |
301 CACHE_NEW_COLOR_CALC_STATE |
302 CACHE_NEW_DEPTH_STENCIL_STATE)
303 },
304 .prepare = prepare_cc_state_pointers,
305 .emit = upload_cc_state_pointers,
306 };