Merge remote branch 'origin/master' into pipe-video
[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
39 GLenum logic_op;
40
41 GLenum blend_eq_rgb, blend_eq_a;
42 GLenum blend_src_rgb, blend_src_a;
43 GLenum blend_dst_rgb, blend_dst_a;
44
45 GLenum alpha_func;
46 };
47
48 static void
49 blend_state_populate_key(struct brw_context *brw,
50 struct gen6_blend_state_key *key)
51 {
52 struct gl_context *ctx = &brw->intel.ctx;
53
54 memset(key, 0, sizeof(*key));
55
56 /* _NEW_COLOR */
57 if (ctx->Color._LogicOpEnabled)
58 key->logic_op = ctx->Color.LogicOp;
59 else
60 key->logic_op = GL_COPY;
61
62 /* _NEW_COLOR */
63 key->color_blend = ctx->Color.BlendEnabled;
64 if (key->color_blend) {
65 key->blend_eq_rgb = ctx->Color.BlendEquationRGB;
66 key->blend_eq_a = ctx->Color.BlendEquationA;
67 key->blend_src_rgb = ctx->Color.BlendSrcRGB;
68 key->blend_dst_rgb = ctx->Color.BlendDstRGB;
69 key->blend_src_a = ctx->Color.BlendSrcA;
70 key->blend_dst_a = ctx->Color.BlendDstA;
71 }
72
73 /* _NEW_COLOR */
74 key->alpha_enabled = ctx->Color.AlphaEnabled;
75 if (key->alpha_enabled) {
76 key->alpha_func = ctx->Color.AlphaFunc;
77 }
78
79 /* _NEW_COLOR */
80 key->dither = ctx->Color.DitherFlag;
81 }
82
83 /**
84 * Creates the state cache entry for the given CC unit key.
85 */
86 static drm_intel_bo *
87 blend_state_create_from_key(struct brw_context *brw,
88 struct gen6_blend_state_key *key)
89 {
90 struct gen6_blend_state blend;
91 drm_intel_bo *bo;
92
93 memset(&blend, 0, sizeof(blend));
94
95 if (key->logic_op != GL_COPY) {
96 blend.blend1.logic_op_enable = 1;
97 blend.blend1.logic_op_func = intel_translate_logic_op(key->logic_op);
98 } else if (key->color_blend) {
99 GLenum eqRGB = key->blend_eq_rgb;
100 GLenum eqA = key->blend_eq_a;
101 GLenum srcRGB = key->blend_src_rgb;
102 GLenum dstRGB = key->blend_dst_rgb;
103 GLenum srcA = key->blend_src_a;
104 GLenum dstA = key->blend_dst_a;
105
106 if (eqRGB == GL_MIN || eqRGB == GL_MAX) {
107 srcRGB = dstRGB = GL_ONE;
108 }
109
110 if (eqA == GL_MIN || eqA == GL_MAX) {
111 srcA = dstA = GL_ONE;
112 }
113
114 blend.blend0.dest_blend_factor = brw_translate_blend_factor(dstRGB);
115 blend.blend0.source_blend_factor = brw_translate_blend_factor(srcRGB);
116 blend.blend0.blend_func = brw_translate_blend_equation(eqRGB);
117
118 blend.blend0.ia_dest_blend_factor = brw_translate_blend_factor(dstA);
119 blend.blend0.ia_source_blend_factor = brw_translate_blend_factor(srcA);
120 blend.blend0.ia_blend_func = brw_translate_blend_equation(eqA);
121
122 blend.blend0.blend_enable = 1;
123 blend.blend0.ia_blend_enable = (srcA != srcRGB ||
124 dstA != dstRGB ||
125 eqA != eqRGB);
126 }
127
128 if (key->alpha_enabled) {
129 blend.blend1.alpha_test_enable = 1;
130 blend.blend1.alpha_test_func = intel_translate_compare_func(key->alpha_func);
131
132 }
133
134 if (key->dither) {
135 blend.blend1.dither_enable = 1;
136 blend.blend1.y_dither_offset = 0;
137 blend.blend1.x_dither_offset = 0;
138 }
139
140 bo = brw_upload_cache(&brw->cache, BRW_BLEND_STATE,
141 key, sizeof(*key),
142 NULL, 0,
143 &blend, sizeof(blend));
144
145 return bo;
146 }
147
148 static void
149 prepare_blend_state(struct brw_context *brw)
150 {
151 struct gen6_blend_state_key key;
152
153 blend_state_populate_key(brw, &key);
154
155 drm_intel_bo_unreference(brw->cc.blend_state_bo);
156 brw->cc.blend_state_bo = brw_search_cache(&brw->cache, BRW_BLEND_STATE,
157 &key, sizeof(key),
158 NULL, 0,
159 NULL);
160
161 if (brw->cc.blend_state_bo == NULL)
162 brw->cc.blend_state_bo = blend_state_create_from_key(brw, &key);
163 }
164
165 const struct brw_tracked_state gen6_blend_state = {
166 .dirty = {
167 .mesa = _NEW_COLOR,
168 .brw = 0,
169 .cache = 0,
170 },
171 .prepare = prepare_blend_state,
172 };
173
174 struct gen6_color_calc_state_key {
175 GLubyte blend_constant_color[4];
176 GLclampf alpha_ref;
177 GLubyte stencil_ref[2];
178 };
179
180 static void
181 color_calc_state_populate_key(struct brw_context *brw,
182 struct gen6_color_calc_state_key *key)
183 {
184 struct gl_context *ctx = &brw->intel.ctx;
185
186 memset(key, 0, sizeof(*key));
187
188 /* _NEW_STENCIL */
189 if (ctx->Stencil._Enabled) {
190 const unsigned back = ctx->Stencil._BackFace;
191
192 key->stencil_ref[0] = ctx->Stencil.Ref[0];
193 if (ctx->Stencil._TestTwoSide)
194 key->stencil_ref[1] = ctx->Stencil.Ref[back];
195 }
196
197 /* _NEW_COLOR */
198 if (ctx->Color.AlphaEnabled)
199 key->alpha_ref = ctx->Color.AlphaRef;
200
201 key->blend_constant_color[0] = ctx->Color.BlendColor[0];
202 key->blend_constant_color[1] = ctx->Color.BlendColor[1];
203 key->blend_constant_color[2] = ctx->Color.BlendColor[2];
204 key->blend_constant_color[3] = ctx->Color.BlendColor[3];
205 }
206
207 /**
208 * Creates the state cache entry for the given CC state key.
209 */
210 static drm_intel_bo *
211 color_calc_state_create_from_key(struct brw_context *brw,
212 struct gen6_color_calc_state_key *key)
213 {
214 struct gen6_color_calc_state cc;
215 drm_intel_bo *bo;
216
217 memset(&cc, 0, sizeof(cc));
218
219 cc.cc0.alpha_test_format = BRW_ALPHATEST_FORMAT_UNORM8;
220 UNCLAMPED_FLOAT_TO_UBYTE(cc.cc1.alpha_ref_fi.ui, key->alpha_ref);
221
222 cc.cc0.stencil_ref = key->stencil_ref[0];
223 cc.cc0.bf_stencil_ref = key->stencil_ref[1];
224
225 cc.constant_r = key->blend_constant_color[0];
226 cc.constant_g = key->blend_constant_color[1];
227 cc.constant_b = key->blend_constant_color[2];
228 cc.constant_a = key->blend_constant_color[3];
229
230 bo = brw_upload_cache(&brw->cache, BRW_COLOR_CALC_STATE,
231 key, sizeof(*key),
232 NULL, 0,
233 &cc, sizeof(cc));
234
235 return bo;
236 }
237
238 static void
239 prepare_color_calc_state(struct brw_context *brw)
240 {
241 struct gen6_color_calc_state_key key;
242
243 color_calc_state_populate_key(brw, &key);
244
245 drm_intel_bo_unreference(brw->cc.state_bo);
246 brw->cc.state_bo = brw_search_cache(&brw->cache, BRW_COLOR_CALC_STATE,
247 &key, sizeof(key),
248 NULL, 0,
249 NULL);
250
251 if (brw->cc.state_bo == NULL)
252 brw->cc.state_bo = color_calc_state_create_from_key(brw, &key);
253 }
254
255 const struct brw_tracked_state gen6_color_calc_state = {
256 .dirty = {
257 .mesa = _NEW_COLOR | _NEW_STENCIL,
258 .brw = 0,
259 .cache = 0,
260 },
261 .prepare = prepare_color_calc_state,
262 };
263
264 static void upload_cc_state_pointers(struct brw_context *brw)
265 {
266 struct intel_context *intel = &brw->intel;
267
268 BEGIN_BATCH(4);
269 OUT_BATCH(CMD_3D_CC_STATE_POINTERS << 16 | (4 - 2));
270 OUT_RELOC(brw->cc.blend_state_bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 1);
271 OUT_RELOC(brw->cc.depth_stencil_state_bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 1);
272 OUT_RELOC(brw->cc.state_bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 1);
273 ADVANCE_BATCH();
274 }
275
276
277 static void prepare_cc_state_pointers(struct brw_context *brw)
278 {
279 brw_add_validated_bo(brw, brw->cc.state_bo);
280 brw_add_validated_bo(brw, brw->cc.blend_state_bo);
281 brw_add_validated_bo(brw, brw->cc.depth_stencil_state_bo);
282 }
283
284 const struct brw_tracked_state gen6_cc_state_pointers = {
285 .dirty = {
286 .mesa = 0,
287 .brw = BRW_NEW_BATCH,
288 .cache = (CACHE_NEW_BLEND_STATE |
289 CACHE_NEW_COLOR_CALC_STATE |
290 CACHE_NEW_DEPTH_STENCIL_STATE)
291 },
292 .prepare = prepare_cc_state_pointers,
293 .emit = upload_cc_state_pointers,
294 };