i965: Gen4-5: Include alpha func/ref in program key
[mesa.git] / src / mesa / drivers / dri / i965 / brw_wm.c
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 #include "brw_context.h"
33 #include "brw_wm.h"
34 #include "brw_state.h"
35 #include "main/enums.h"
36 #include "main/formats.h"
37 #include "main/fbobject.h"
38 #include "main/samplerobj.h"
39 #include "program/prog_parameter.h"
40 #include "program/program.h"
41
42 #include "glsl/ralloc.h"
43
44 /**
45 * Return a bitfield where bit n is set if barycentric interpolation mode n
46 * (see enum brw_wm_barycentric_interp_mode) is needed by the fragment shader.
47 */
48 static unsigned
49 brw_compute_barycentric_interp_modes(struct brw_context *brw,
50 bool shade_model_flat,
51 const struct gl_fragment_program *fprog)
52 {
53 unsigned barycentric_interp_modes = 0;
54 int attr;
55
56 /* Loop through all fragment shader inputs to figure out what interpolation
57 * modes are in use, and set the appropriate bits in
58 * barycentric_interp_modes.
59 */
60 for (attr = 0; attr < VARYING_SLOT_MAX; ++attr) {
61 enum glsl_interp_qualifier interp_qualifier =
62 fprog->InterpQualifier[attr];
63 bool is_centroid = fprog->IsCentroid & BITFIELD64_BIT(attr);
64 bool is_gl_Color = attr == VARYING_SLOT_COL0 || attr == VARYING_SLOT_COL1;
65
66 /* Ignore unused inputs. */
67 if (!(fprog->Base.InputsRead & BITFIELD64_BIT(attr)))
68 continue;
69
70 /* Ignore WPOS and FACE, because they don't require interpolation. */
71 if (attr == VARYING_SLOT_POS || attr == VARYING_SLOT_FACE)
72 continue;
73
74 /* Determine the set (or sets) of barycentric coordinates needed to
75 * interpolate this variable. Note that when
76 * brw->needs_unlit_centroid_workaround is set, centroid interpolation
77 * uses PIXEL interpolation for unlit pixels and CENTROID interpolation
78 * for lit pixels, so we need both sets of barycentric coordinates.
79 */
80 if (interp_qualifier == INTERP_QUALIFIER_NOPERSPECTIVE) {
81 if (is_centroid) {
82 barycentric_interp_modes |=
83 1 << BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC;
84 }
85 if (!is_centroid || brw->needs_unlit_centroid_workaround) {
86 barycentric_interp_modes |=
87 1 << BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC;
88 }
89 } else if (interp_qualifier == INTERP_QUALIFIER_SMOOTH ||
90 (!(shade_model_flat && is_gl_Color) &&
91 interp_qualifier == INTERP_QUALIFIER_NONE)) {
92 if (is_centroid) {
93 barycentric_interp_modes |=
94 1 << BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC;
95 }
96 if (!is_centroid || brw->needs_unlit_centroid_workaround) {
97 barycentric_interp_modes |=
98 1 << BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC;
99 }
100 }
101 }
102
103 return barycentric_interp_modes;
104 }
105
106 bool
107 brw_wm_prog_data_compare(const void *in_a, const void *in_b)
108 {
109 const struct brw_wm_prog_data *a = in_a;
110 const struct brw_wm_prog_data *b = in_b;
111
112 /* Compare all the struct (including the base) up to the pointers. */
113 if (memcmp(a, b, offsetof(struct brw_wm_prog_data, param)))
114 return false;
115
116 if (memcmp(a->param, b->param, a->nr_params * sizeof(void *)))
117 return false;
118
119 if (memcmp(a->pull_param, b->pull_param, a->nr_pull_params * sizeof(void *)))
120 return false;
121
122 return true;
123 }
124
125 void
126 brw_wm_prog_data_free(const void *in_prog_data)
127 {
128 const struct brw_wm_prog_data *prog_data = in_prog_data;
129
130 ralloc_free((void *)prog_data->param);
131 ralloc_free((void *)prog_data->pull_param);
132 }
133
134 /**
135 * All Mesa program -> GPU code generation goes through this function.
136 * Depending on the instructions used (i.e. flow control instructions)
137 * we'll use one of two code generators.
138 */
139 bool do_wm_prog(struct brw_context *brw,
140 struct gl_shader_program *prog,
141 struct brw_fragment_program *fp,
142 struct brw_wm_prog_key *key)
143 {
144 struct brw_wm_compile *c;
145 const GLuint *program;
146 struct gl_shader *fs = NULL;
147 GLuint program_size;
148
149 if (prog)
150 fs = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
151
152 c = rzalloc(NULL, struct brw_wm_compile);
153
154 /* Allocate the references to the uniforms that will end up in the
155 * prog_data associated with the compiled program, and which will be freed
156 * by the state cache.
157 */
158 int param_count;
159 if (fs) {
160 param_count = fs->num_uniform_components;
161 } else {
162 param_count = fp->program.Base.Parameters->NumParameters * 4;
163 }
164 /* The backend also sometimes adds params for texture size. */
165 param_count += 2 * BRW_MAX_TEX_UNIT;
166 c->prog_data.param = rzalloc_array(NULL, const float *, param_count);
167 c->prog_data.pull_param = rzalloc_array(NULL, const float *, param_count);
168
169 memcpy(&c->key, key, sizeof(*key));
170
171 c->prog_data.barycentric_interp_modes =
172 brw_compute_barycentric_interp_modes(brw, c->key.flat_shade,
173 &fp->program);
174
175 program = brw_wm_fs_emit(brw, c, &fp->program, prog, &program_size);
176 if (program == NULL)
177 return false;
178
179 /* Scratch space is used for register spilling */
180 if (c->last_scratch) {
181 perf_debug("Fragment shader triggered register spilling. "
182 "Try reducing the number of live scalar values to "
183 "improve performance.\n");
184
185 c->prog_data.total_scratch = brw_get_scratch_size(c->last_scratch);
186
187 brw_get_scratch_bo(brw, &brw->wm.base.scratch_bo,
188 c->prog_data.total_scratch * brw->max_wm_threads);
189 }
190
191 if (unlikely(INTEL_DEBUG & DEBUG_WM))
192 fprintf(stderr, "\n");
193
194 brw_upload_cache(&brw->cache, BRW_WM_PROG,
195 &c->key, sizeof(c->key),
196 program, program_size,
197 &c->prog_data, sizeof(c->prog_data),
198 &brw->wm.base.prog_offset, &brw->wm.prog_data);
199
200 ralloc_free(c);
201
202 return true;
203 }
204
205 static bool
206 key_debug(struct brw_context *brw, const char *name, int a, int b)
207 {
208 if (a != b) {
209 perf_debug(" %s %d->%d\n", name, a, b);
210 return true;
211 } else {
212 return false;
213 }
214 }
215
216 bool
217 brw_debug_recompile_sampler_key(struct brw_context *brw,
218 const struct brw_sampler_prog_key_data *old_key,
219 const struct brw_sampler_prog_key_data *key)
220 {
221 bool found = false;
222
223 for (unsigned int i = 0; i < MAX_SAMPLERS; i++) {
224 found |= key_debug(brw, "EXT_texture_swizzle or DEPTH_TEXTURE_MODE",
225 old_key->swizzles[i], key->swizzles[i]);
226 }
227 found |= key_debug(brw, "GL_CLAMP enabled on any texture unit's 1st coordinate",
228 old_key->gl_clamp_mask[0], key->gl_clamp_mask[0]);
229 found |= key_debug(brw, "GL_CLAMP enabled on any texture unit's 2nd coordinate",
230 old_key->gl_clamp_mask[1], key->gl_clamp_mask[1]);
231 found |= key_debug(brw, "GL_CLAMP enabled on any texture unit's 3rd coordinate",
232 old_key->gl_clamp_mask[2], key->gl_clamp_mask[2]);
233 found |= key_debug(brw, "GL_MESA_ycbcr texturing\n",
234 old_key->yuvtex_mask, key->yuvtex_mask);
235 found |= key_debug(brw, "GL_MESA_ycbcr UV swapping\n",
236 old_key->yuvtex_swap_mask, key->yuvtex_swap_mask);
237 found |= key_debug(brw, "gather channel quirk on any texture unit",
238 old_key->gather_channel_quirk_mask, key->gather_channel_quirk_mask);
239
240 return found;
241 }
242
243 void
244 brw_wm_debug_recompile(struct brw_context *brw,
245 struct gl_shader_program *prog,
246 const struct brw_wm_prog_key *key)
247 {
248 struct brw_cache_item *c = NULL;
249 const struct brw_wm_prog_key *old_key = NULL;
250 bool found = false;
251
252 perf_debug("Recompiling fragment shader for program %d\n", prog->Name);
253
254 for (unsigned int i = 0; i < brw->cache.size; i++) {
255 for (c = brw->cache.items[i]; c; c = c->next) {
256 if (c->cache_id == BRW_WM_PROG) {
257 old_key = c->key;
258
259 if (old_key->program_string_id == key->program_string_id)
260 break;
261 }
262 }
263 if (c)
264 break;
265 }
266
267 if (!c) {
268 perf_debug(" Didn't find previous compile in the shader cache for debug\n");
269 return;
270 }
271
272 found |= key_debug(brw, "alphatest, computed depth, depth test, or "
273 "depth write",
274 old_key->iz_lookup, key->iz_lookup);
275 found |= key_debug(brw, "depth statistics",
276 old_key->stats_wm, key->stats_wm);
277 found |= key_debug(brw, "flat shading",
278 old_key->flat_shade, key->flat_shade);
279 found |= key_debug(brw, "number of color buffers",
280 old_key->nr_color_regions, key->nr_color_regions);
281 found |= key_debug(brw, "MRT alpha test or alpha-to-coverage",
282 old_key->replicate_alpha, key->replicate_alpha);
283 found |= key_debug(brw, "rendering to FBO",
284 old_key->render_to_fbo, key->render_to_fbo);
285 found |= key_debug(brw, "fragment color clamping",
286 old_key->clamp_fragment_color, key->clamp_fragment_color);
287 found |= key_debug(brw, "line smoothing",
288 old_key->line_aa, key->line_aa);
289 found |= key_debug(brw, "renderbuffer height",
290 old_key->drawable_height, key->drawable_height);
291 found |= key_debug(brw, "input slots valid",
292 old_key->input_slots_valid, key->input_slots_valid);
293 found |= key_debug(brw, "mrt alpha test function",
294 old_key->alpha_test_func, key->alpha_test_func);
295 found |= key_debug(brw, "mrt alpha test reference value",
296 old_key->alpha_test_ref, key->alpha_test_ref);
297
298 found |= brw_debug_recompile_sampler_key(brw, &old_key->tex, &key->tex);
299
300 if (!found) {
301 perf_debug(" Something else\n");
302 }
303 }
304
305 void
306 brw_populate_sampler_prog_key_data(struct gl_context *ctx,
307 const struct gl_program *prog,
308 unsigned sampler_count,
309 struct brw_sampler_prog_key_data *key)
310 {
311 struct brw_context *brw = brw_context(ctx);
312
313 for (int s = 0; s < sampler_count; s++) {
314 key->swizzles[s] = SWIZZLE_NOOP;
315
316 if (!(prog->SamplersUsed & (1 << s)))
317 continue;
318
319 int unit_id = prog->SamplerUnits[s];
320 const struct gl_texture_unit *unit = &ctx->Texture.Unit[unit_id];
321
322 if (unit->_ReallyEnabled && unit->_Current->Target != GL_TEXTURE_BUFFER) {
323 const struct gl_texture_object *t = unit->_Current;
324 const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
325 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit_id);
326
327 const bool alpha_depth = t->DepthMode == GL_ALPHA &&
328 (img->_BaseFormat == GL_DEPTH_COMPONENT ||
329 img->_BaseFormat == GL_DEPTH_STENCIL);
330
331 /* Haswell handles texture swizzling as surface format overrides
332 * (except for GL_ALPHA); all other platforms need MOVs in the shader.
333 */
334 if (!brw->is_haswell || alpha_depth)
335 key->swizzles[s] = brw_get_texture_swizzle(ctx, t);
336
337 if (img->InternalFormat == GL_YCBCR_MESA) {
338 key->yuvtex_mask |= 1 << s;
339 if (img->TexFormat == MESA_FORMAT_YCBCR)
340 key->yuvtex_swap_mask |= 1 << s;
341 }
342
343 if (sampler->MinFilter != GL_NEAREST &&
344 sampler->MagFilter != GL_NEAREST) {
345 if (sampler->WrapS == GL_CLAMP)
346 key->gl_clamp_mask[0] |= 1 << s;
347 if (sampler->WrapT == GL_CLAMP)
348 key->gl_clamp_mask[1] |= 1 << s;
349 if (sampler->WrapR == GL_CLAMP)
350 key->gl_clamp_mask[2] |= 1 << s;
351 }
352
353 /* gather4's channel select for green from RG32F is broken;
354 * requires a shader w/a on IVB; fixable with just SCS on HSW. */
355 if (brw->gen >= 7 && !brw->is_haswell && prog->UsesGather) {
356 if (img->InternalFormat == GL_RG32F)
357 key->gather_channel_quirk_mask |= 1 << s;
358 }
359 }
360 }
361 }
362
363 static void brw_wm_populate_key( struct brw_context *brw,
364 struct brw_wm_prog_key *key )
365 {
366 struct gl_context *ctx = &brw->ctx;
367 /* BRW_NEW_FRAGMENT_PROGRAM */
368 const struct brw_fragment_program *fp =
369 (struct brw_fragment_program *)brw->fragment_program;
370 const struct gl_program *prog = (struct gl_program *) brw->fragment_program;
371 GLuint lookup = 0;
372 GLuint line_aa;
373 bool program_uses_dfdy = fp->program.UsesDFdy;
374 bool multisample_fbo = ctx->DrawBuffer->Visual.samples > 1;
375
376 memset(key, 0, sizeof(*key));
377
378 /* Build the index for table lookup
379 */
380 if (brw->gen < 6) {
381 /* _NEW_COLOR */
382 if (fp->program.UsesKill || ctx->Color.AlphaEnabled)
383 lookup |= IZ_PS_KILL_ALPHATEST_BIT;
384
385 if (fp->program.Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
386 lookup |= IZ_PS_COMPUTES_DEPTH_BIT;
387
388 /* _NEW_DEPTH */
389 if (ctx->Depth.Test)
390 lookup |= IZ_DEPTH_TEST_ENABLE_BIT;
391
392 if (ctx->Depth.Test && ctx->Depth.Mask) /* ?? */
393 lookup |= IZ_DEPTH_WRITE_ENABLE_BIT;
394
395 /* _NEW_STENCIL | _NEW_BUFFERS */
396 if (ctx->Stencil._Enabled) {
397 lookup |= IZ_STENCIL_TEST_ENABLE_BIT;
398
399 if (ctx->Stencil.WriteMask[0] ||
400 ctx->Stencil.WriteMask[ctx->Stencil._BackFace])
401 lookup |= IZ_STENCIL_WRITE_ENABLE_BIT;
402 }
403 key->iz_lookup = lookup;
404 }
405
406 line_aa = AA_NEVER;
407
408 /* _NEW_LINE, _NEW_POLYGON, BRW_NEW_REDUCED_PRIMITIVE */
409 if (ctx->Line.SmoothFlag) {
410 if (brw->reduced_primitive == GL_LINES) {
411 line_aa = AA_ALWAYS;
412 }
413 else if (brw->reduced_primitive == GL_TRIANGLES) {
414 if (ctx->Polygon.FrontMode == GL_LINE) {
415 line_aa = AA_SOMETIMES;
416
417 if (ctx->Polygon.BackMode == GL_LINE ||
418 (ctx->Polygon.CullFlag &&
419 ctx->Polygon.CullFaceMode == GL_BACK))
420 line_aa = AA_ALWAYS;
421 }
422 else if (ctx->Polygon.BackMode == GL_LINE) {
423 line_aa = AA_SOMETIMES;
424
425 if ((ctx->Polygon.CullFlag &&
426 ctx->Polygon.CullFaceMode == GL_FRONT))
427 line_aa = AA_ALWAYS;
428 }
429 }
430 }
431
432 key->line_aa = line_aa;
433
434 /* _NEW_HINT */
435 if (brw->disable_derivative_optimization) {
436 key->high_quality_derivatives =
437 ctx->Hint.FragmentShaderDerivative != GL_FASTEST;
438 } else {
439 key->high_quality_derivatives =
440 ctx->Hint.FragmentShaderDerivative == GL_NICEST;
441 }
442
443 if (brw->gen < 6)
444 key->stats_wm = brw->stats_wm;
445
446 /* _NEW_LIGHT */
447 key->flat_shade = (ctx->Light.ShadeModel == GL_FLAT);
448
449 /* _NEW_FRAG_CLAMP | _NEW_BUFFERS */
450 key->clamp_fragment_color = ctx->Color._ClampFragmentColor;
451
452 /* _NEW_TEXTURE */
453 brw_populate_sampler_prog_key_data(ctx, prog, brw->wm.base.sampler_count,
454 &key->tex);
455
456 /* _NEW_BUFFERS */
457 /*
458 * Include the draw buffer origin and height so that we can calculate
459 * fragment position values relative to the bottom left of the drawable,
460 * from the incoming screen origin relative position we get as part of our
461 * payload.
462 *
463 * This is only needed for the WM_WPOSXY opcode when the fragment program
464 * uses the gl_FragCoord input.
465 *
466 * We could avoid recompiling by including this as a constant referenced by
467 * our program, but if we were to do that it would also be nice to handle
468 * getting that constant updated at batchbuffer submit time (when we
469 * hold the lock and know where the buffer really is) rather than at emit
470 * time when we don't hold the lock and are just guessing. We could also
471 * just avoid using this as key data if the program doesn't use
472 * fragment.position.
473 *
474 * For DRI2 the origin_x/y will always be (0,0) but we still need the
475 * drawable height in order to invert the Y axis.
476 */
477 if (fp->program.Base.InputsRead & VARYING_BIT_POS) {
478 key->drawable_height = ctx->DrawBuffer->Height;
479 }
480
481 if ((fp->program.Base.InputsRead & VARYING_BIT_POS) || program_uses_dfdy) {
482 key->render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
483 }
484
485 /* _NEW_BUFFERS */
486 key->nr_color_regions = ctx->DrawBuffer->_NumColorDrawBuffers;
487
488 /* _NEW_MULTISAMPLE, _NEW_COLOR, _NEW_BUFFERS */
489 key->replicate_alpha = ctx->DrawBuffer->_NumColorDrawBuffers > 1 &&
490 (ctx->Multisample.SampleAlphaToCoverage || ctx->Color.AlphaEnabled);
491
492 /* _NEW_BUFFERS _NEW_MULTISAMPLE */
493 key->compute_pos_offset =
494 _mesa_get_min_invocations_per_fragment(ctx, &fp->program) > 1 &&
495 fp->program.Base.SystemValuesRead & SYSTEM_BIT_SAMPLE_POS;
496
497 key->compute_sample_id =
498 multisample_fbo &&
499 ctx->Multisample.Enabled &&
500 (fp->program.Base.SystemValuesRead & SYSTEM_BIT_SAMPLE_ID);
501
502 /* BRW_NEW_VUE_MAP_GEOM_OUT */
503 if (brw->gen < 6 || _mesa_bitcount_64(fp->program.Base.InputsRead &
504 BRW_FS_VARYING_INPUT_MASK) > 16)
505 key->input_slots_valid = brw->vue_map_geom_out.slots_valid;
506
507
508 /* _NEW_COLOR | _NEW_BUFFERS */
509 /* Pre-gen6, the hardware alpha test always used each render
510 * target's alpha to do alpha test, as opposed to render target 0's alpha
511 * like GL requires. Fix that by building the alpha test into the
512 * shader, and we'll skip enabling the fixed function alpha test.
513 */
514 if (brw->gen < 6 && ctx->DrawBuffer->_NumColorDrawBuffers > 1 && ctx->Color.AlphaEnabled) {
515 key->alpha_test_func = ctx->Color.AlphaFunc;
516 key->alpha_test_ref = ctx->Color.AlphaRef;
517 }
518
519 /* The unique fragment program ID */
520 key->program_string_id = fp->id;
521 }
522
523
524 static void
525 brw_upload_wm_prog(struct brw_context *brw)
526 {
527 struct gl_context *ctx = &brw->ctx;
528 struct brw_wm_prog_key key;
529 struct brw_fragment_program *fp = (struct brw_fragment_program *)
530 brw->fragment_program;
531
532 brw_wm_populate_key(brw, &key);
533
534 if (!brw_search_cache(&brw->cache, BRW_WM_PROG,
535 &key, sizeof(key),
536 &brw->wm.base.prog_offset, &brw->wm.prog_data)) {
537 bool success = do_wm_prog(brw, ctx->Shader._CurrentFragmentProgram, fp,
538 &key);
539 (void) success;
540 assert(success);
541 }
542 brw->wm.base.prog_data = &brw->wm.prog_data->base;
543 }
544
545
546 const struct brw_tracked_state brw_wm_prog = {
547 .dirty = {
548 .mesa = (_NEW_COLOR |
549 _NEW_DEPTH |
550 _NEW_STENCIL |
551 _NEW_POLYGON |
552 _NEW_LINE |
553 _NEW_HINT |
554 _NEW_LIGHT |
555 _NEW_FRAG_CLAMP |
556 _NEW_BUFFERS |
557 _NEW_TEXTURE |
558 _NEW_MULTISAMPLE),
559 .brw = (BRW_NEW_FRAGMENT_PROGRAM |
560 BRW_NEW_REDUCED_PRIMITIVE |
561 BRW_NEW_VUE_MAP_GEOM_OUT |
562 BRW_NEW_STATS_WM)
563 },
564 .emit = brw_upload_wm_prog
565 };
566