Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_visitor.cpp
1 /*
2 * Copyright © 2010 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
24 /** @file brw_fs_visitor.cpp
25 *
26 * This file supports generating the FS LIR from the GLSL IR. The LIR
27 * makes it easier to do backend-specific optimizations than doing so
28 * in the GLSL IR or in the native code.
29 */
30 #include <sys/types.h>
31
32 #include "main/macros.h"
33 #include "main/shaderobj.h"
34 #include "program/prog_parameter.h"
35 #include "program/prog_print.h"
36 #include "program/prog_optimize.h"
37 #include "util/register_allocate.h"
38 #include "program/hash_table.h"
39 #include "brw_context.h"
40 #include "brw_eu.h"
41 #include "brw_wm.h"
42 #include "brw_cs.h"
43 #include "brw_vec4.h"
44 #include "brw_vec4_gs_visitor.h"
45 #include "brw_fs.h"
46 #include "main/uniforms.h"
47 #include "glsl/nir/glsl_types.h"
48 #include "glsl/ir_optimization.h"
49 #include "program/sampler.h"
50
51 using namespace brw;
52
53 fs_reg *
54 fs_visitor::emit_vs_system_value(int location)
55 {
56 fs_reg *reg = new(this->mem_ctx)
57 fs_reg(ATTR, 4 * _mesa_bitcount_64(nir->info.inputs_read),
58 BRW_REGISTER_TYPE_D);
59 brw_vs_prog_data *vs_prog_data = (brw_vs_prog_data *) prog_data;
60
61 switch (location) {
62 case SYSTEM_VALUE_BASE_VERTEX:
63 reg->reg_offset = 0;
64 vs_prog_data->uses_vertexid = true;
65 break;
66 case SYSTEM_VALUE_VERTEX_ID:
67 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
68 reg->reg_offset = 2;
69 vs_prog_data->uses_vertexid = true;
70 break;
71 case SYSTEM_VALUE_INSTANCE_ID:
72 reg->reg_offset = 3;
73 vs_prog_data->uses_instanceid = true;
74 break;
75 default:
76 unreachable("not reached");
77 }
78
79 return reg;
80 }
81
82 fs_reg
83 fs_visitor::rescale_texcoord(fs_reg coordinate, int coord_components,
84 bool is_rect, uint32_t sampler)
85 {
86 bool needs_gl_clamp = true;
87 fs_reg scale_x, scale_y;
88
89 /* The 965 requires the EU to do the normalization of GL rectangle
90 * texture coordinates. We use the program parameter state
91 * tracking to get the scaling factor.
92 */
93 if (is_rect &&
94 (devinfo->gen < 6 ||
95 (devinfo->gen >= 6 && (key_tex->gl_clamp_mask[0] & (1 << sampler) ||
96 key_tex->gl_clamp_mask[1] & (1 << sampler))))) {
97 struct gl_program_parameter_list *params = prog->Parameters;
98
99
100 /* FINISHME: We're failing to recompile our programs when the sampler is
101 * updated. This only matters for the texture rectangle scale
102 * parameters (pre-gen6, or gen6+ with GL_CLAMP).
103 */
104 int tokens[STATE_LENGTH] = {
105 STATE_INTERNAL,
106 STATE_TEXRECT_SCALE,
107 prog->SamplerUnits[sampler],
108 0,
109 0
110 };
111
112 no16("rectangle scale uniform setup not supported on SIMD16\n");
113 if (dispatch_width == 16) {
114 return coordinate;
115 }
116
117 GLuint index = _mesa_add_state_reference(params,
118 (gl_state_index *)tokens);
119 /* Try to find existing copies of the texrect scale uniforms. */
120 for (unsigned i = 0; i < uniforms; i++) {
121 if (stage_prog_data->param[i] ==
122 &prog->Parameters->ParameterValues[index][0]) {
123 scale_x = fs_reg(UNIFORM, i);
124 scale_y = fs_reg(UNIFORM, i + 1);
125 break;
126 }
127 }
128
129 /* If we didn't already set them up, do so now. */
130 if (scale_x.file == BAD_FILE) {
131 scale_x = fs_reg(UNIFORM, uniforms);
132 scale_y = fs_reg(UNIFORM, uniforms + 1);
133
134 stage_prog_data->param[uniforms++] =
135 &prog->Parameters->ParameterValues[index][0];
136 stage_prog_data->param[uniforms++] =
137 &prog->Parameters->ParameterValues[index][1];
138 }
139 }
140
141 /* The 965 requires the EU to do the normalization of GL rectangle
142 * texture coordinates. We use the program parameter state
143 * tracking to get the scaling factor.
144 */
145 if (devinfo->gen < 6 && is_rect) {
146 fs_reg dst = fs_reg(GRF, alloc.allocate(coord_components));
147 fs_reg src = coordinate;
148 coordinate = dst;
149
150 bld.MUL(dst, src, scale_x);
151 dst = offset(dst, bld, 1);
152 src = offset(src, bld, 1);
153 bld.MUL(dst, src, scale_y);
154 } else if (is_rect) {
155 /* On gen6+, the sampler handles the rectangle coordinates
156 * natively, without needing rescaling. But that means we have
157 * to do GL_CLAMP clamping at the [0, width], [0, height] scale,
158 * not [0, 1] like the default case below.
159 */
160 needs_gl_clamp = false;
161
162 for (int i = 0; i < 2; i++) {
163 if (key_tex->gl_clamp_mask[i] & (1 << sampler)) {
164 fs_reg chan = coordinate;
165 chan = offset(chan, bld, i);
166
167 set_condmod(BRW_CONDITIONAL_GE,
168 bld.emit(BRW_OPCODE_SEL, chan, chan, fs_reg(0.0f)));
169
170 /* Our parameter comes in as 1.0/width or 1.0/height,
171 * because that's what people normally want for doing
172 * texture rectangle handling. We need width or height
173 * for clamping, but we don't care enough to make a new
174 * parameter type, so just invert back.
175 */
176 fs_reg limit = vgrf(glsl_type::float_type);
177 bld.MOV(limit, i == 0 ? scale_x : scale_y);
178 bld.emit(SHADER_OPCODE_RCP, limit, limit);
179
180 set_condmod(BRW_CONDITIONAL_L,
181 bld.emit(BRW_OPCODE_SEL, chan, chan, limit));
182 }
183 }
184 }
185
186 if (coord_components > 0 && needs_gl_clamp) {
187 for (int i = 0; i < MIN2(coord_components, 3); i++) {
188 if (key_tex->gl_clamp_mask[i] & (1 << sampler)) {
189 fs_reg chan = coordinate;
190 chan = offset(chan, bld, i);
191 set_saturate(true, bld.MOV(chan, chan));
192 }
193 }
194 }
195 return coordinate;
196 }
197
198 /* Sample from the MCS surface attached to this multisample texture. */
199 fs_reg
200 fs_visitor::emit_mcs_fetch(const fs_reg &coordinate, unsigned components,
201 const fs_reg &sampler)
202 {
203 const fs_reg dest = vgrf(glsl_type::uvec4_type);
204 const fs_reg srcs[] = {
205 coordinate, fs_reg(), fs_reg(), fs_reg(), fs_reg(), fs_reg(),
206 sampler, fs_reg(), fs_reg(components), fs_reg(0)
207 };
208 fs_inst *inst = bld.emit(SHADER_OPCODE_TXF_MCS_LOGICAL, dest, srcs,
209 ARRAY_SIZE(srcs));
210
211 /* We only care about one reg of response, but the sampler always writes
212 * 4/8.
213 */
214 inst->regs_written = 4 * dispatch_width / 8;
215
216 return dest;
217 }
218
219 void
220 fs_visitor::emit_texture(ir_texture_opcode op,
221 const glsl_type *dest_type,
222 fs_reg coordinate, int coord_components,
223 fs_reg shadow_c,
224 fs_reg lod, fs_reg lod2, int grad_components,
225 fs_reg sample_index,
226 fs_reg offset_value,
227 fs_reg mcs,
228 int gather_component,
229 bool is_cube_array,
230 bool is_rect,
231 uint32_t sampler,
232 fs_reg sampler_reg)
233 {
234 fs_inst *inst = NULL;
235
236 if (op == ir_tg4) {
237 /* When tg4 is used with the degenerate ZERO/ONE swizzles, don't bother
238 * emitting anything other than setting up the constant result.
239 */
240 int swiz = GET_SWZ(key_tex->swizzles[sampler], gather_component);
241 if (swiz == SWIZZLE_ZERO || swiz == SWIZZLE_ONE) {
242
243 fs_reg res = vgrf(glsl_type::vec4_type);
244 this->result = res;
245
246 for (int i=0; i<4; i++) {
247 bld.MOV(res, fs_reg(swiz == SWIZZLE_ZERO ? 0.0f : 1.0f));
248 res = offset(res, bld, 1);
249 }
250 return;
251 }
252 }
253
254 if (op == ir_query_levels) {
255 /* textureQueryLevels() is implemented in terms of TXS so we need to
256 * pass a valid LOD argument.
257 */
258 assert(lod.file == BAD_FILE);
259 lod = fs_reg(0u);
260 }
261
262 if (coordinate.file != BAD_FILE) {
263 /* FINISHME: Texture coordinate rescaling doesn't work with non-constant
264 * samplers. This should only be a problem with GL_CLAMP on Gen7.
265 */
266 coordinate = rescale_texcoord(coordinate, coord_components, is_rect,
267 sampler);
268 }
269
270 /* Writemasking doesn't eliminate channels on SIMD8 texture
271 * samples, so don't worry about them.
272 */
273 fs_reg dst = vgrf(glsl_type::get_instance(dest_type->base_type, 4, 1));
274 const fs_reg srcs[] = {
275 coordinate, shadow_c, lod, lod2,
276 sample_index, mcs, sampler_reg, offset_value,
277 fs_reg(coord_components), fs_reg(grad_components)
278 };
279 enum opcode opcode;
280
281 switch (op) {
282 case ir_tex:
283 opcode = SHADER_OPCODE_TEX_LOGICAL;
284 break;
285 case ir_txb:
286 opcode = FS_OPCODE_TXB_LOGICAL;
287 break;
288 case ir_txl:
289 opcode = SHADER_OPCODE_TXL_LOGICAL;
290 break;
291 case ir_txd:
292 opcode = SHADER_OPCODE_TXD_LOGICAL;
293 break;
294 case ir_txf:
295 opcode = SHADER_OPCODE_TXF_LOGICAL;
296 break;
297 case ir_txf_ms:
298 opcode = SHADER_OPCODE_TXF_CMS_LOGICAL;
299 break;
300 case ir_txs:
301 case ir_query_levels:
302 opcode = SHADER_OPCODE_TXS_LOGICAL;
303 break;
304 case ir_lod:
305 opcode = SHADER_OPCODE_LOD_LOGICAL;
306 break;
307 case ir_tg4:
308 opcode = (offset_value.file != BAD_FILE && offset_value.file != IMM ?
309 SHADER_OPCODE_TG4_OFFSET_LOGICAL : SHADER_OPCODE_TG4_LOGICAL);
310 break;
311 default:
312 unreachable("Invalid texture opcode.");
313 }
314
315 inst = bld.emit(opcode, dst, srcs, ARRAY_SIZE(srcs));
316 inst->regs_written = 4 * dispatch_width / 8;
317
318 if (shadow_c.file != BAD_FILE)
319 inst->shadow_compare = true;
320
321 if (offset_value.file == IMM)
322 inst->offset = offset_value.fixed_hw_reg.dw1.ud;
323
324 if (op == ir_tg4) {
325 inst->offset |=
326 gather_channel(gather_component, sampler) << 16; /* M0.2:16-17 */
327
328 if (devinfo->gen == 6)
329 emit_gen6_gather_wa(key_tex->gen6_gather_wa[sampler], dst);
330 }
331
332 /* fixup #layers for cube map arrays */
333 if (op == ir_txs && is_cube_array) {
334 fs_reg depth = offset(dst, bld, 2);
335 fs_reg fixed_depth = vgrf(glsl_type::int_type);
336 bld.emit(SHADER_OPCODE_INT_QUOTIENT, fixed_depth, depth, fs_reg(6));
337
338 fs_reg *fixed_payload = ralloc_array(mem_ctx, fs_reg, inst->regs_written);
339 int components = inst->regs_written / (inst->exec_size / 8);
340 for (int i = 0; i < components; i++) {
341 if (i == 2) {
342 fixed_payload[i] = fixed_depth;
343 } else {
344 fixed_payload[i] = offset(dst, bld, i);
345 }
346 }
347 bld.LOAD_PAYLOAD(dst, fixed_payload, components, 0);
348 }
349
350 swizzle_result(op, dest_type->vector_elements, dst, sampler);
351 }
352
353 /**
354 * Apply workarounds for Gen6 gather with UINT/SINT
355 */
356 void
357 fs_visitor::emit_gen6_gather_wa(uint8_t wa, fs_reg dst)
358 {
359 if (!wa)
360 return;
361
362 int width = (wa & WA_8BIT) ? 8 : 16;
363
364 for (int i = 0; i < 4; i++) {
365 fs_reg dst_f = retype(dst, BRW_REGISTER_TYPE_F);
366 /* Convert from UNORM to UINT */
367 bld.MUL(dst_f, dst_f, fs_reg((float)((1 << width) - 1)));
368 bld.MOV(dst, dst_f);
369
370 if (wa & WA_SIGN) {
371 /* Reinterpret the UINT value as a signed INT value by
372 * shifting the sign bit into place, then shifting back
373 * preserving sign.
374 */
375 bld.SHL(dst, dst, fs_reg(32 - width));
376 bld.ASR(dst, dst, fs_reg(32 - width));
377 }
378
379 dst = offset(dst, bld, 1);
380 }
381 }
382
383 /**
384 * Set up the gather channel based on the swizzle, for gather4.
385 */
386 uint32_t
387 fs_visitor::gather_channel(int orig_chan, uint32_t sampler)
388 {
389 int swiz = GET_SWZ(key_tex->swizzles[sampler], orig_chan);
390 switch (swiz) {
391 case SWIZZLE_X: return 0;
392 case SWIZZLE_Y:
393 /* gather4 sampler is broken for green channel on RG32F --
394 * we must ask for blue instead.
395 */
396 if (key_tex->gather_channel_quirk_mask & (1 << sampler))
397 return 2;
398 return 1;
399 case SWIZZLE_Z: return 2;
400 case SWIZZLE_W: return 3;
401 default:
402 unreachable("Not reached"); /* zero, one swizzles handled already */
403 }
404 }
405
406 /**
407 * Swizzle the result of a texture result. This is necessary for
408 * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons.
409 */
410 void
411 fs_visitor::swizzle_result(ir_texture_opcode op, int dest_components,
412 fs_reg orig_val, uint32_t sampler)
413 {
414 if (op == ir_query_levels) {
415 /* # levels is in .w */
416 this->result = offset(orig_val, bld, 3);
417 return;
418 }
419
420 this->result = orig_val;
421
422 /* txs,lod don't actually sample the texture, so swizzling the result
423 * makes no sense.
424 */
425 if (op == ir_txs || op == ir_lod || op == ir_tg4)
426 return;
427
428 if (dest_components == 1) {
429 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
430 } else if (key_tex->swizzles[sampler] != SWIZZLE_NOOP) {
431 fs_reg swizzled_result = vgrf(glsl_type::vec4_type);
432 swizzled_result.type = orig_val.type;
433
434 for (int i = 0; i < 4; i++) {
435 int swiz = GET_SWZ(key_tex->swizzles[sampler], i);
436 fs_reg l = swizzled_result;
437 l = offset(l, bld, i);
438
439 if (swiz == SWIZZLE_ZERO) {
440 bld.MOV(l, fs_reg(0.0f));
441 } else if (swiz == SWIZZLE_ONE) {
442 bld.MOV(l, fs_reg(1.0f));
443 } else {
444 bld.MOV(l, offset(orig_val, bld,
445 GET_SWZ(key_tex->swizzles[sampler], i)));
446 }
447 }
448 this->result = swizzled_result;
449 }
450 }
451
452 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
453 void
454 fs_visitor::emit_dummy_fs()
455 {
456 int reg_width = dispatch_width / 8;
457
458 /* Everyone's favorite color. */
459 const float color[4] = { 1.0, 0.0, 1.0, 0.0 };
460 for (int i = 0; i < 4; i++) {
461 bld.MOV(fs_reg(MRF, 2 + i * reg_width, BRW_REGISTER_TYPE_F),
462 fs_reg(color[i]));
463 }
464
465 fs_inst *write;
466 write = bld.emit(FS_OPCODE_FB_WRITE);
467 write->eot = true;
468 if (devinfo->gen >= 6) {
469 write->base_mrf = 2;
470 write->mlen = 4 * reg_width;
471 } else {
472 write->header_size = 2;
473 write->base_mrf = 0;
474 write->mlen = 2 + 4 * reg_width;
475 }
476
477 /* Tell the SF we don't have any inputs. Gen4-5 require at least one
478 * varying to avoid GPU hangs, so set that.
479 */
480 brw_wm_prog_data *wm_prog_data = (brw_wm_prog_data *) this->prog_data;
481 wm_prog_data->num_varying_inputs = devinfo->gen < 6 ? 1 : 0;
482 memset(wm_prog_data->urb_setup, -1,
483 sizeof(wm_prog_data->urb_setup[0]) * VARYING_SLOT_MAX);
484
485 /* We don't have any uniforms. */
486 stage_prog_data->nr_params = 0;
487 stage_prog_data->nr_pull_params = 0;
488 stage_prog_data->curb_read_length = 0;
489 stage_prog_data->dispatch_grf_start_reg = 2;
490 wm_prog_data->dispatch_grf_start_reg_16 = 2;
491 grf_used = 1; /* Gen4-5 don't allow zero GRF blocks */
492
493 calculate_cfg();
494 }
495
496 /* The register location here is relative to the start of the URB
497 * data. It will get adjusted to be a real location before
498 * generate_code() time.
499 */
500 struct brw_reg
501 fs_visitor::interp_reg(int location, int channel)
502 {
503 assert(stage == MESA_SHADER_FRAGMENT);
504 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
505 int regnr = prog_data->urb_setup[location] * 2 + channel / 2;
506 int stride = (channel & 1) * 4;
507
508 assert(prog_data->urb_setup[location] != -1);
509
510 return brw_vec1_grf(regnr, stride);
511 }
512
513 /** Emits the interpolation for the varying inputs. */
514 void
515 fs_visitor::emit_interpolation_setup_gen4()
516 {
517 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
518
519 fs_builder abld = bld.annotate("compute pixel centers");
520 this->pixel_x = vgrf(glsl_type::uint_type);
521 this->pixel_y = vgrf(glsl_type::uint_type);
522 this->pixel_x.type = BRW_REGISTER_TYPE_UW;
523 this->pixel_y.type = BRW_REGISTER_TYPE_UW;
524 abld.ADD(this->pixel_x,
525 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
526 fs_reg(brw_imm_v(0x10101010)));
527 abld.ADD(this->pixel_y,
528 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
529 fs_reg(brw_imm_v(0x11001100)));
530
531 abld = bld.annotate("compute pixel deltas from v0");
532
533 this->delta_xy[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] =
534 vgrf(glsl_type::vec2_type);
535 const fs_reg &delta_xy = this->delta_xy[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC];
536 const fs_reg xstart(negate(brw_vec1_grf(1, 0)));
537 const fs_reg ystart(negate(brw_vec1_grf(1, 1)));
538
539 if (devinfo->has_pln && dispatch_width == 16) {
540 for (unsigned i = 0; i < 2; i++) {
541 abld.half(i).ADD(half(offset(delta_xy, abld, i), 0),
542 half(this->pixel_x, i), xstart);
543 abld.half(i).ADD(half(offset(delta_xy, abld, i), 1),
544 half(this->pixel_y, i), ystart);
545 }
546 } else {
547 abld.ADD(offset(delta_xy, abld, 0), this->pixel_x, xstart);
548 abld.ADD(offset(delta_xy, abld, 1), this->pixel_y, ystart);
549 }
550
551 abld = bld.annotate("compute pos.w and 1/pos.w");
552 /* Compute wpos.w. It's always in our setup, since it's needed to
553 * interpolate the other attributes.
554 */
555 this->wpos_w = vgrf(glsl_type::float_type);
556 abld.emit(FS_OPCODE_LINTERP, wpos_w, delta_xy,
557 interp_reg(VARYING_SLOT_POS, 3));
558 /* Compute the pixel 1/W value from wpos.w. */
559 this->pixel_w = vgrf(glsl_type::float_type);
560 abld.emit(SHADER_OPCODE_RCP, this->pixel_w, wpos_w);
561 }
562
563 /** Emits the interpolation for the varying inputs. */
564 void
565 fs_visitor::emit_interpolation_setup_gen6()
566 {
567 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
568
569 fs_builder abld = bld.annotate("compute pixel centers");
570 if (devinfo->gen >= 8 || dispatch_width == 8) {
571 /* The "Register Region Restrictions" page says for BDW (and newer,
572 * presumably):
573 *
574 * "When destination spans two registers, the source may be one or
575 * two registers. The destination elements must be evenly split
576 * between the two registers."
577 *
578 * Thus we can do a single add(16) in SIMD8 or an add(32) in SIMD16 to
579 * compute our pixel centers.
580 */
581 fs_reg int_pixel_xy(GRF, alloc.allocate(dispatch_width / 8),
582 BRW_REGISTER_TYPE_UW);
583
584 const fs_builder dbld = abld.exec_all().group(dispatch_width * 2, 0);
585 dbld.ADD(int_pixel_xy,
586 fs_reg(stride(suboffset(g1_uw, 4), 1, 4, 0)),
587 fs_reg(brw_imm_v(0x11001010)));
588
589 this->pixel_x = vgrf(glsl_type::float_type);
590 this->pixel_y = vgrf(glsl_type::float_type);
591 abld.emit(FS_OPCODE_PIXEL_X, this->pixel_x, int_pixel_xy);
592 abld.emit(FS_OPCODE_PIXEL_Y, this->pixel_y, int_pixel_xy);
593 } else {
594 /* The "Register Region Restrictions" page says for SNB, IVB, HSW:
595 *
596 * "When destination spans two registers, the source MUST span two
597 * registers."
598 *
599 * Since the GRF source of the ADD will only read a single register, we
600 * must do two separate ADDs in SIMD16.
601 */
602 fs_reg int_pixel_x = vgrf(glsl_type::uint_type);
603 fs_reg int_pixel_y = vgrf(glsl_type::uint_type);
604 int_pixel_x.type = BRW_REGISTER_TYPE_UW;
605 int_pixel_y.type = BRW_REGISTER_TYPE_UW;
606 abld.ADD(int_pixel_x,
607 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
608 fs_reg(brw_imm_v(0x10101010)));
609 abld.ADD(int_pixel_y,
610 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
611 fs_reg(brw_imm_v(0x11001100)));
612
613 /* As of gen6, we can no longer mix float and int sources. We have
614 * to turn the integer pixel centers into floats for their actual
615 * use.
616 */
617 this->pixel_x = vgrf(glsl_type::float_type);
618 this->pixel_y = vgrf(glsl_type::float_type);
619 abld.MOV(this->pixel_x, int_pixel_x);
620 abld.MOV(this->pixel_y, int_pixel_y);
621 }
622
623 abld = bld.annotate("compute pos.w");
624 this->pixel_w = fs_reg(brw_vec8_grf(payload.source_w_reg, 0));
625 this->wpos_w = vgrf(glsl_type::float_type);
626 abld.emit(SHADER_OPCODE_RCP, this->wpos_w, this->pixel_w);
627
628 for (int i = 0; i < BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT; ++i) {
629 uint8_t reg = payload.barycentric_coord_reg[i];
630 this->delta_xy[i] = fs_reg(brw_vec16_grf(reg, 0));
631 }
632 }
633
634 static enum brw_conditional_mod
635 cond_for_alpha_func(GLenum func)
636 {
637 switch(func) {
638 case GL_GREATER:
639 return BRW_CONDITIONAL_G;
640 case GL_GEQUAL:
641 return BRW_CONDITIONAL_GE;
642 case GL_LESS:
643 return BRW_CONDITIONAL_L;
644 case GL_LEQUAL:
645 return BRW_CONDITIONAL_LE;
646 case GL_EQUAL:
647 return BRW_CONDITIONAL_EQ;
648 case GL_NOTEQUAL:
649 return BRW_CONDITIONAL_NEQ;
650 default:
651 unreachable("Not reached");
652 }
653 }
654
655 /**
656 * Alpha test support for when we compile it into the shader instead
657 * of using the normal fixed-function alpha test.
658 */
659 void
660 fs_visitor::emit_alpha_test()
661 {
662 assert(stage == MESA_SHADER_FRAGMENT);
663 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
664 const fs_builder abld = bld.annotate("Alpha test");
665
666 fs_inst *cmp;
667 if (key->alpha_test_func == GL_ALWAYS)
668 return;
669
670 if (key->alpha_test_func == GL_NEVER) {
671 /* f0.1 = 0 */
672 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
673 BRW_REGISTER_TYPE_UW));
674 cmp = abld.CMP(bld.null_reg_f(), some_reg, some_reg,
675 BRW_CONDITIONAL_NEQ);
676 } else {
677 /* RT0 alpha */
678 fs_reg color = offset(outputs[0], bld, 3);
679
680 /* f0.1 &= func(color, ref) */
681 cmp = abld.CMP(bld.null_reg_f(), color, fs_reg(key->alpha_test_ref),
682 cond_for_alpha_func(key->alpha_test_func));
683 }
684 cmp->predicate = BRW_PREDICATE_NORMAL;
685 cmp->flag_subreg = 1;
686 }
687
688 fs_inst *
689 fs_visitor::emit_single_fb_write(const fs_builder &bld,
690 fs_reg color0, fs_reg color1,
691 fs_reg src0_alpha, unsigned components)
692 {
693 assert(stage == MESA_SHADER_FRAGMENT);
694 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
695
696 /* Hand over gl_FragDepth or the payload depth. */
697 const fs_reg dst_depth = (payload.dest_depth_reg ?
698 fs_reg(brw_vec8_grf(payload.dest_depth_reg, 0)) :
699 fs_reg());
700 fs_reg src_depth, src_stencil;
701
702 if (source_depth_to_render_target) {
703 if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
704 src_depth = frag_depth;
705 else
706 src_depth = fs_reg(brw_vec8_grf(payload.source_depth_reg, 0));
707 }
708
709 if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL))
710 src_stencil = frag_stencil;
711
712 const fs_reg sources[] = {
713 color0, color1, src0_alpha, src_depth, dst_depth, src_stencil,
714 sample_mask, fs_reg(components)
715 };
716 assert(ARRAY_SIZE(sources) - 1 == FB_WRITE_LOGICAL_SRC_COMPONENTS);
717 fs_inst *write = bld.emit(FS_OPCODE_FB_WRITE_LOGICAL, fs_reg(),
718 sources, ARRAY_SIZE(sources));
719
720 if (prog_data->uses_kill) {
721 write->predicate = BRW_PREDICATE_NORMAL;
722 write->flag_subreg = 1;
723 }
724
725 return write;
726 }
727
728 void
729 fs_visitor::emit_fb_writes()
730 {
731 assert(stage == MESA_SHADER_FRAGMENT);
732 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
733 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
734
735 fs_inst *inst = NULL;
736
737 if (source_depth_to_render_target && devinfo->gen == 6) {
738 /* For outputting oDepth on gen6, SIMD8 writes have to be used. This
739 * would require SIMD8 moves of each half to message regs, e.g. by using
740 * the SIMD lowering pass. Unfortunately this is more difficult than it
741 * sounds because the SIMD8 single-source message lacks channel selects
742 * for the second and third subspans.
743 */
744 no16("Missing support for simd16 depth writes on gen6\n");
745 }
746
747 if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
748 /* From the 'Render Target Write message' section of the docs:
749 * "Output Stencil is not supported with SIMD16 Render Target Write
750 * Messages."
751 *
752 * FINISHME: split 16 into 2 8s
753 */
754 no16("FINISHME: support 2 simd8 writes for gl_FragStencilRefARB\n");
755 }
756
757 if (do_dual_src) {
758 const fs_builder abld = bld.annotate("FB dual-source write");
759
760 inst = emit_single_fb_write(abld, this->outputs[0],
761 this->dual_src_output, reg_undef, 4);
762 inst->target = 0;
763
764 prog_data->dual_src_blend = true;
765 } else {
766 for (int target = 0; target < key->nr_color_regions; target++) {
767 /* Skip over outputs that weren't written. */
768 if (this->outputs[target].file == BAD_FILE)
769 continue;
770
771 const fs_builder abld = bld.annotate(
772 ralloc_asprintf(this->mem_ctx, "FB write target %d", target));
773
774 fs_reg src0_alpha;
775 if (devinfo->gen >= 6 && key->replicate_alpha && target != 0)
776 src0_alpha = offset(outputs[0], bld, 3);
777
778 inst = emit_single_fb_write(abld, this->outputs[target], reg_undef,
779 src0_alpha,
780 this->output_components[target]);
781 inst->target = target;
782 }
783 }
784
785 if (inst == NULL) {
786 /* Even if there's no color buffers enabled, we still need to send
787 * alpha out the pipeline to our null renderbuffer to support
788 * alpha-testing, alpha-to-coverage, and so on.
789 */
790 /* FINISHME: Factor out this frequently recurring pattern into a
791 * helper function.
792 */
793 const fs_reg srcs[] = { reg_undef, reg_undef,
794 reg_undef, offset(this->outputs[0], bld, 3) };
795 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 4);
796 bld.LOAD_PAYLOAD(tmp, srcs, 4, 0);
797
798 inst = emit_single_fb_write(bld, tmp, reg_undef, reg_undef, 4);
799 inst->target = 0;
800 }
801
802 inst->eot = true;
803 }
804
805 void
806 fs_visitor::setup_uniform_clipplane_values(gl_clip_plane *clip_planes)
807 {
808 const struct brw_vs_prog_key *key =
809 (const struct brw_vs_prog_key *) this->key;
810
811 for (int i = 0; i < key->nr_userclip_plane_consts; i++) {
812 this->userplane[i] = fs_reg(UNIFORM, uniforms);
813 for (int j = 0; j < 4; ++j) {
814 stage_prog_data->param[uniforms + j] =
815 (gl_constant_value *) &clip_planes[i][j];
816 }
817 uniforms += 4;
818 }
819 }
820
821 /**
822 * Lower legacy fixed-function and gl_ClipVertex clipping to clip distances.
823 *
824 * This does nothing if the shader uses gl_ClipDistance or user clipping is
825 * disabled altogether.
826 */
827 void fs_visitor::compute_clip_distance(gl_clip_plane *clip_planes)
828 {
829 struct brw_vue_prog_data *vue_prog_data =
830 (struct brw_vue_prog_data *) prog_data;
831 const struct brw_vs_prog_key *key =
832 (const struct brw_vs_prog_key *) this->key;
833
834 /* Bail unless some sort of legacy clipping is enabled */
835 if (key->nr_userclip_plane_consts == 0)
836 return;
837
838 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
839 *
840 * "If a linked set of shaders forming the vertex stage contains no
841 * static write to gl_ClipVertex or gl_ClipDistance, but the
842 * application has requested clipping against user clip planes through
843 * the API, then the coordinate written to gl_Position is used for
844 * comparison against the user clip planes."
845 *
846 * This function is only called if the shader didn't write to
847 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
848 * if the user wrote to it; otherwise we use gl_Position.
849 */
850
851 gl_varying_slot clip_vertex = VARYING_SLOT_CLIP_VERTEX;
852 if (!(vue_prog_data->vue_map.slots_valid & VARYING_BIT_CLIP_VERTEX))
853 clip_vertex = VARYING_SLOT_POS;
854
855 /* If the clip vertex isn't written, skip this. Typically this means
856 * the GS will set up clipping. */
857 if (outputs[clip_vertex].file == BAD_FILE)
858 return;
859
860 setup_uniform_clipplane_values(clip_planes);
861
862 const fs_builder abld = bld.annotate("user clip distances");
863
864 this->outputs[VARYING_SLOT_CLIP_DIST0] = vgrf(glsl_type::vec4_type);
865 this->output_components[VARYING_SLOT_CLIP_DIST0] = 4;
866 this->outputs[VARYING_SLOT_CLIP_DIST1] = vgrf(glsl_type::vec4_type);
867 this->output_components[VARYING_SLOT_CLIP_DIST1] = 4;
868
869 for (int i = 0; i < key->nr_userclip_plane_consts; i++) {
870 fs_reg u = userplane[i];
871 fs_reg output = outputs[VARYING_SLOT_CLIP_DIST0 + i / 4];
872 output.reg_offset = i & 3;
873
874 abld.MUL(output, outputs[clip_vertex], u);
875 for (int j = 1; j < 4; j++) {
876 u.reg = userplane[i].reg + j;
877 abld.MAD(output, output, offset(outputs[clip_vertex], bld, j), u);
878 }
879 }
880 }
881
882 void
883 fs_visitor::emit_urb_writes()
884 {
885 int slot, urb_offset, length;
886 int starting_urb_offset = 0;
887 const struct brw_vue_prog_data *vue_prog_data =
888 (const struct brw_vue_prog_data *) this->prog_data;
889 const struct brw_vs_prog_key *vs_key =
890 (const struct brw_vs_prog_key *) this->key;
891 const GLbitfield64 psiz_mask =
892 VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT | VARYING_BIT_PSIZ;
893 const struct brw_vue_map *vue_map = &vue_prog_data->vue_map;
894 bool flush;
895 fs_reg sources[8];
896
897 /* If we don't have any valid slots to write, just do a minimal urb write
898 * send to terminate the shader. This includes 1 slot of undefined data,
899 * because it's invalid to write 0 data:
900 *
901 * From the Broadwell PRM, Volume 7: 3D Media GPGPU, Shared Functions -
902 * Unified Return Buffer (URB) > URB_SIMD8_Write and URB_SIMD8_Read >
903 * Write Data Payload:
904 *
905 * "The write data payload can be between 1 and 8 message phases long."
906 */
907 if (vue_map->slots_valid == 0) {
908 fs_reg payload = fs_reg(GRF, alloc.allocate(2), BRW_REGISTER_TYPE_UD);
909 bld.exec_all().MOV(payload, fs_reg(retype(brw_vec8_grf(1, 0),
910 BRW_REGISTER_TYPE_UD)));
911
912 fs_inst *inst = bld.emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, payload);
913 inst->eot = true;
914 inst->mlen = 2;
915 inst->offset = 1;
916 return;
917 }
918
919 if (stage == MESA_SHADER_GEOMETRY) {
920 const struct brw_gs_prog_data *gs_prog_data =
921 (const struct brw_gs_prog_data *) prog_data;
922
923 /* We need to increment the Global Offset to skip over the control data
924 * header and the extra "Vertex Count" field (1 HWord) at the beginning
925 * of the VUE. We're counting in OWords, so the units are doubled.
926 */
927 starting_urb_offset = 2 * gs_prog_data->control_data_header_size_hwords;
928 if (gs_prog_data->static_vertex_count == -1)
929 starting_urb_offset += 2;
930 }
931
932 length = 0;
933 urb_offset = starting_urb_offset;
934 flush = false;
935 for (slot = 0; slot < vue_map->num_slots; slot++) {
936 int varying = vue_map->slot_to_varying[slot];
937 switch (varying) {
938 case VARYING_SLOT_PSIZ: {
939 /* The point size varying slot is the vue header and is always in the
940 * vue map. But often none of the special varyings that live there
941 * are written and in that case we can skip writing to the vue
942 * header, provided the corresponding state properly clamps the
943 * values further down the pipeline. */
944 if ((vue_map->slots_valid & psiz_mask) == 0) {
945 assert(length == 0);
946 urb_offset++;
947 break;
948 }
949
950 fs_reg zero(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
951 bld.MOV(zero, fs_reg(0u));
952
953 sources[length++] = zero;
954 if (vue_map->slots_valid & VARYING_BIT_LAYER)
955 sources[length++] = this->outputs[VARYING_SLOT_LAYER];
956 else
957 sources[length++] = zero;
958
959 if (vue_map->slots_valid & VARYING_BIT_VIEWPORT)
960 sources[length++] = this->outputs[VARYING_SLOT_VIEWPORT];
961 else
962 sources[length++] = zero;
963
964 if (vue_map->slots_valid & VARYING_BIT_PSIZ)
965 sources[length++] = this->outputs[VARYING_SLOT_PSIZ];
966 else
967 sources[length++] = zero;
968 break;
969 }
970 case BRW_VARYING_SLOT_NDC:
971 case VARYING_SLOT_EDGE:
972 unreachable("unexpected scalar vs output");
973 break;
974
975 default:
976 /* gl_Position is always in the vue map, but isn't always written by
977 * the shader. Other varyings (clip distances) get added to the vue
978 * map but don't always get written. In those cases, the
979 * corresponding this->output[] slot will be invalid we and can skip
980 * the urb write for the varying. If we've already queued up a vue
981 * slot for writing we flush a mlen 5 urb write, otherwise we just
982 * advance the urb_offset.
983 */
984 if (varying == BRW_VARYING_SLOT_PAD ||
985 this->outputs[varying].file == BAD_FILE) {
986 if (length > 0)
987 flush = true;
988 else
989 urb_offset++;
990 break;
991 }
992
993 if (stage == MESA_SHADER_VERTEX && vs_key->clamp_vertex_color &&
994 (varying == VARYING_SLOT_COL0 ||
995 varying == VARYING_SLOT_COL1 ||
996 varying == VARYING_SLOT_BFC0 ||
997 varying == VARYING_SLOT_BFC1)) {
998 /* We need to clamp these guys, so do a saturating MOV into a
999 * temp register and use that for the payload.
1000 */
1001 for (int i = 0; i < 4; i++) {
1002 fs_reg reg = fs_reg(GRF, alloc.allocate(1), outputs[varying].type);
1003 fs_reg src = offset(this->outputs[varying], bld, i);
1004 set_saturate(true, bld.MOV(reg, src));
1005 sources[length++] = reg;
1006 }
1007 } else {
1008 for (unsigned i = 0; i < output_components[varying]; i++)
1009 sources[length++] = offset(this->outputs[varying], bld, i);
1010 for (unsigned i = output_components[varying]; i < 4; i++)
1011 sources[length++] = fs_reg(0);
1012 }
1013 break;
1014 }
1015
1016 const fs_builder abld = bld.annotate("URB write");
1017
1018 /* If we've queued up 8 registers of payload (2 VUE slots), if this is
1019 * the last slot or if we need to flush (see BAD_FILE varying case
1020 * above), emit a URB write send now to flush out the data.
1021 */
1022 int last = slot == vue_map->num_slots - 1;
1023 if (length == 8 || last)
1024 flush = true;
1025 if (flush) {
1026 fs_reg *payload_sources = ralloc_array(mem_ctx, fs_reg, length + 1);
1027 fs_reg payload = fs_reg(GRF, alloc.allocate(length + 1),
1028 BRW_REGISTER_TYPE_F);
1029 payload_sources[0] =
1030 fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
1031
1032 memcpy(&payload_sources[1], sources, length * sizeof sources[0]);
1033 abld.LOAD_PAYLOAD(payload, payload_sources, length + 1, 1);
1034
1035 fs_inst *inst =
1036 abld.emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, payload);
1037 inst->eot = last && stage == MESA_SHADER_VERTEX;
1038 inst->mlen = length + 1;
1039 inst->offset = urb_offset;
1040 urb_offset = starting_urb_offset + slot + 1;
1041 length = 0;
1042 flush = false;
1043 }
1044 }
1045 }
1046
1047 void
1048 fs_visitor::emit_cs_terminate()
1049 {
1050 assert(devinfo->gen >= 7);
1051
1052 /* We are getting the thread ID from the compute shader header */
1053 assert(stage == MESA_SHADER_COMPUTE);
1054
1055 /* We can't directly send from g0, since sends with EOT have to use
1056 * g112-127. So, copy it to a virtual register, The register allocator will
1057 * make sure it uses the appropriate register range.
1058 */
1059 struct brw_reg g0 = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD);
1060 fs_reg payload = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
1061 bld.group(8, 0).exec_all().MOV(payload, g0);
1062
1063 /* Send a message to the thread spawner to terminate the thread. */
1064 fs_inst *inst = bld.exec_all()
1065 .emit(CS_OPCODE_CS_TERMINATE, reg_undef, payload);
1066 inst->eot = true;
1067 }
1068
1069 void
1070 fs_visitor::emit_barrier()
1071 {
1072 assert(devinfo->gen >= 7);
1073
1074 /* We are getting the barrier ID from the compute shader header */
1075 assert(stage == MESA_SHADER_COMPUTE);
1076
1077 fs_reg payload = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
1078
1079 const fs_builder pbld = bld.exec_all().group(8, 0);
1080
1081 /* Clear the message payload */
1082 pbld.MOV(payload, fs_reg(0u));
1083
1084 /* Copy bits 27:24 of r0.2 (barrier id) to the message payload reg.2 */
1085 fs_reg r0_2 = fs_reg(retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD));
1086 pbld.AND(component(payload, 2), r0_2, fs_reg(0x0f000000u));
1087
1088 /* Emit a gateway "barrier" message using the payload we set up, followed
1089 * by a wait instruction.
1090 */
1091 bld.exec_all().emit(SHADER_OPCODE_BARRIER, reg_undef, payload);
1092 }
1093
1094 fs_visitor::fs_visitor(const struct brw_compiler *compiler, void *log_data,
1095 void *mem_ctx,
1096 const void *key,
1097 struct brw_stage_prog_data *prog_data,
1098 struct gl_program *prog,
1099 const nir_shader *shader,
1100 unsigned dispatch_width,
1101 int shader_time_index)
1102 : backend_shader(compiler, log_data, mem_ctx, shader, prog_data),
1103 key(key), gs_compile(NULL), prog_data(prog_data), prog(prog),
1104 dispatch_width(dispatch_width),
1105 shader_time_index(shader_time_index),
1106 bld(fs_builder(this, dispatch_width).at_end())
1107 {
1108 init();
1109 }
1110
1111 fs_visitor::fs_visitor(const struct brw_compiler *compiler, void *log_data,
1112 void *mem_ctx,
1113 struct brw_gs_compile *c,
1114 struct brw_gs_prog_data *prog_data,
1115 const nir_shader *shader)
1116 : backend_shader(compiler, log_data, mem_ctx, shader,
1117 &prog_data->base.base),
1118 key(&c->key), gs_compile(c),
1119 prog_data(&prog_data->base.base), prog(NULL),
1120 dispatch_width(8),
1121 shader_time_index(ST_GS),
1122 bld(fs_builder(this, dispatch_width).at_end())
1123 {
1124 init();
1125 }
1126
1127
1128 void
1129 fs_visitor::init()
1130 {
1131 switch (stage) {
1132 case MESA_SHADER_FRAGMENT:
1133 key_tex = &((const brw_wm_prog_key *) key)->tex;
1134 break;
1135 case MESA_SHADER_VERTEX:
1136 key_tex = &((const brw_vs_prog_key *) key)->tex;
1137 break;
1138 case MESA_SHADER_GEOMETRY:
1139 key_tex = &((const brw_gs_prog_key *) key)->tex;
1140 break;
1141 case MESA_SHADER_COMPUTE:
1142 key_tex = &((const brw_cs_prog_key*) key)->tex;
1143 break;
1144 default:
1145 unreachable("unhandled shader stage");
1146 }
1147
1148 this->prog_data = this->stage_prog_data;
1149
1150 this->failed = false;
1151 this->simd16_unsupported = false;
1152 this->no16_msg = NULL;
1153
1154 this->nir_locals = NULL;
1155 this->nir_ssa_values = NULL;
1156
1157 memset(&this->payload, 0, sizeof(this->payload));
1158 memset(this->outputs, 0, sizeof(this->outputs));
1159 memset(this->output_components, 0, sizeof(this->output_components));
1160 this->source_depth_to_render_target = false;
1161 this->runtime_check_aads_emit = false;
1162 this->first_non_payload_grf = 0;
1163 this->max_grf = devinfo->gen >= 7 ? GEN7_MRF_HACK_START : BRW_MAX_GRF;
1164
1165 this->virtual_grf_start = NULL;
1166 this->virtual_grf_end = NULL;
1167 this->live_intervals = NULL;
1168 this->regs_live_at_ip = NULL;
1169
1170 this->uniforms = 0;
1171 this->last_scratch = 0;
1172 this->pull_constant_loc = NULL;
1173 this->push_constant_loc = NULL;
1174
1175 this->promoted_constants = 0,
1176
1177 this->spilled_any_registers = false;
1178 this->do_dual_src = false;
1179
1180 if (dispatch_width == 8)
1181 this->param_size = rzalloc_array(mem_ctx, int, stage_prog_data->nr_params);
1182 }
1183
1184 fs_visitor::~fs_visitor()
1185 {
1186 }