intel/compiler: Move all live interval analysis results into fs_live_variables
[mesa.git] / src / intel / compiler / 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 "brw_fs.h"
31 #include "compiler/glsl_types.h"
32
33 using namespace brw;
34
35 /* Sample from the MCS surface attached to this multisample texture. */
36 fs_reg
37 fs_visitor::emit_mcs_fetch(const fs_reg &coordinate, unsigned components,
38 const fs_reg &texture,
39 const fs_reg &texture_handle)
40 {
41 const fs_reg dest = vgrf(glsl_type::uvec4_type);
42
43 fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
44 srcs[TEX_LOGICAL_SRC_COORDINATE] = coordinate;
45 srcs[TEX_LOGICAL_SRC_SURFACE] = texture;
46 srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_ud(0);
47 srcs[TEX_LOGICAL_SRC_SURFACE_HANDLE] = texture_handle;
48 srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_d(components);
49 srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_d(0);
50
51 fs_inst *inst = bld.emit(SHADER_OPCODE_TXF_MCS_LOGICAL, dest, srcs,
52 ARRAY_SIZE(srcs));
53
54 /* We only care about one or two regs of response, but the sampler always
55 * writes 4/8.
56 */
57 inst->size_written = 4 * dest.component_size(inst->exec_size);
58
59 return dest;
60 }
61
62 /**
63 * Apply workarounds for Gen6 gather with UINT/SINT
64 */
65 void
66 fs_visitor::emit_gen6_gather_wa(uint8_t wa, fs_reg dst)
67 {
68 if (!wa)
69 return;
70
71 int width = (wa & WA_8BIT) ? 8 : 16;
72
73 for (int i = 0; i < 4; i++) {
74 fs_reg dst_f = retype(dst, BRW_REGISTER_TYPE_F);
75 /* Convert from UNORM to UINT */
76 bld.MUL(dst_f, dst_f, brw_imm_f((1 << width) - 1));
77 bld.MOV(dst, dst_f);
78
79 if (wa & WA_SIGN) {
80 /* Reinterpret the UINT value as a signed INT value by
81 * shifting the sign bit into place, then shifting back
82 * preserving sign.
83 */
84 bld.SHL(dst, dst, brw_imm_d(32 - width));
85 bld.ASR(dst, dst, brw_imm_d(32 - width));
86 }
87
88 dst = offset(dst, bld, 1);
89 }
90 }
91
92 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
93 void
94 fs_visitor::emit_dummy_fs()
95 {
96 int reg_width = dispatch_width / 8;
97
98 /* Everyone's favorite color. */
99 const float color[4] = { 1.0, 0.0, 1.0, 0.0 };
100 for (int i = 0; i < 4; i++) {
101 bld.MOV(fs_reg(MRF, 2 + i * reg_width, BRW_REGISTER_TYPE_F),
102 brw_imm_f(color[i]));
103 }
104
105 fs_inst *write;
106 write = bld.emit(FS_OPCODE_FB_WRITE);
107 write->eot = true;
108 write->last_rt = true;
109 if (devinfo->gen >= 6) {
110 write->base_mrf = 2;
111 write->mlen = 4 * reg_width;
112 } else {
113 write->header_size = 2;
114 write->base_mrf = 0;
115 write->mlen = 2 + 4 * reg_width;
116 }
117
118 /* Tell the SF we don't have any inputs. Gen4-5 require at least one
119 * varying to avoid GPU hangs, so set that.
120 */
121 struct brw_wm_prog_data *wm_prog_data = brw_wm_prog_data(this->prog_data);
122 wm_prog_data->num_varying_inputs = devinfo->gen < 6 ? 1 : 0;
123 memset(wm_prog_data->urb_setup, -1,
124 sizeof(wm_prog_data->urb_setup[0]) * VARYING_SLOT_MAX);
125
126 /* We don't have any uniforms. */
127 stage_prog_data->nr_params = 0;
128 stage_prog_data->nr_pull_params = 0;
129 stage_prog_data->curb_read_length = 0;
130 stage_prog_data->dispatch_grf_start_reg = 2;
131 wm_prog_data->dispatch_grf_start_reg_16 = 2;
132 wm_prog_data->dispatch_grf_start_reg_32 = 2;
133 grf_used = 1; /* Gen4-5 don't allow zero GRF blocks */
134
135 calculate_cfg();
136 }
137
138 /* The register location here is relative to the start of the URB
139 * data. It will get adjusted to be a real location before
140 * generate_code() time.
141 */
142 fs_reg
143 fs_visitor::interp_reg(int location, int channel)
144 {
145 assert(stage == MESA_SHADER_FRAGMENT);
146 struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);
147 int regnr = prog_data->urb_setup[location] * 4 + channel;
148 assert(prog_data->urb_setup[location] != -1);
149
150 return fs_reg(ATTR, regnr, BRW_REGISTER_TYPE_F);
151 }
152
153 /** Emits the interpolation for the varying inputs. */
154 void
155 fs_visitor::emit_interpolation_setup_gen4()
156 {
157 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
158
159 fs_builder abld = bld.annotate("compute pixel centers");
160 this->pixel_x = vgrf(glsl_type::uint_type);
161 this->pixel_y = vgrf(glsl_type::uint_type);
162 this->pixel_x.type = BRW_REGISTER_TYPE_UW;
163 this->pixel_y.type = BRW_REGISTER_TYPE_UW;
164 abld.ADD(this->pixel_x,
165 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
166 fs_reg(brw_imm_v(0x10101010)));
167 abld.ADD(this->pixel_y,
168 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
169 fs_reg(brw_imm_v(0x11001100)));
170
171 abld = bld.annotate("compute pixel deltas from v0");
172
173 this->delta_xy[BRW_BARYCENTRIC_PERSPECTIVE_PIXEL] =
174 vgrf(glsl_type::vec2_type);
175 const fs_reg &delta_xy = this->delta_xy[BRW_BARYCENTRIC_PERSPECTIVE_PIXEL];
176 const fs_reg xstart(negate(brw_vec1_grf(1, 0)));
177 const fs_reg ystart(negate(brw_vec1_grf(1, 1)));
178
179 if (devinfo->has_pln) {
180 for (unsigned i = 0; i < dispatch_width / 8; i++) {
181 abld.half(i).ADD(half(offset(delta_xy, abld, 0), i),
182 half(this->pixel_x, i), xstart);
183 abld.half(i).ADD(half(offset(delta_xy, abld, 1), i),
184 half(this->pixel_y, i), ystart);
185 }
186 } else {
187 abld.ADD(offset(delta_xy, abld, 0), this->pixel_x, xstart);
188 abld.ADD(offset(delta_xy, abld, 1), this->pixel_y, ystart);
189 }
190
191 abld = bld.annotate("compute pos.w and 1/pos.w");
192 /* Compute wpos.w. It's always in our setup, since it's needed to
193 * interpolate the other attributes.
194 */
195 this->wpos_w = vgrf(glsl_type::float_type);
196 abld.emit(FS_OPCODE_LINTERP, wpos_w, delta_xy,
197 component(interp_reg(VARYING_SLOT_POS, 3), 0));
198 /* Compute the pixel 1/W value from wpos.w. */
199 this->pixel_w = vgrf(glsl_type::float_type);
200 abld.emit(SHADER_OPCODE_RCP, this->pixel_w, wpos_w);
201 }
202
203 static unsigned
204 brw_rnd_mode_from_nir(unsigned mode, unsigned *mask)
205 {
206 unsigned brw_mode = 0;
207 *mask = 0;
208
209 if ((FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 |
210 FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 |
211 FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64) &
212 mode) {
213 brw_mode |= BRW_RND_MODE_RTZ << BRW_CR0_RND_MODE_SHIFT;
214 *mask |= BRW_CR0_RND_MODE_MASK;
215 }
216 if ((FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 |
217 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32 |
218 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64) &
219 mode) {
220 brw_mode |= BRW_RND_MODE_RTNE << BRW_CR0_RND_MODE_SHIFT;
221 *mask |= BRW_CR0_RND_MODE_MASK;
222 }
223 if (mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP16) {
224 brw_mode |= BRW_CR0_FP16_DENORM_PRESERVE;
225 *mask |= BRW_CR0_FP16_DENORM_PRESERVE;
226 }
227 if (mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP32) {
228 brw_mode |= BRW_CR0_FP32_DENORM_PRESERVE;
229 *mask |= BRW_CR0_FP32_DENORM_PRESERVE;
230 }
231 if (mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP64) {
232 brw_mode |= BRW_CR0_FP64_DENORM_PRESERVE;
233 *mask |= BRW_CR0_FP64_DENORM_PRESERVE;
234 }
235 if (mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16)
236 *mask |= BRW_CR0_FP16_DENORM_PRESERVE;
237 if (mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32)
238 *mask |= BRW_CR0_FP32_DENORM_PRESERVE;
239 if (mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64)
240 *mask |= BRW_CR0_FP64_DENORM_PRESERVE;
241 if (mode == FLOAT_CONTROLS_DEFAULT_FLOAT_CONTROL_MODE)
242 *mask |= BRW_CR0_FP_MODE_MASK;
243
244 return brw_mode;
245 }
246
247 void
248 fs_visitor::emit_shader_float_controls_execution_mode()
249 {
250 unsigned execution_mode = this->nir->info.float_controls_execution_mode;
251 if (execution_mode == FLOAT_CONTROLS_DEFAULT_FLOAT_CONTROL_MODE)
252 return;
253
254 fs_builder abld = bld.annotate("shader floats control execution mode");
255 unsigned mask = 0;
256 unsigned mode = brw_rnd_mode_from_nir(execution_mode, &mask);
257 abld.emit(SHADER_OPCODE_FLOAT_CONTROL_MODE, bld.null_reg_ud(),
258 brw_imm_d(mode), brw_imm_d(mask));
259 }
260
261 /** Emits the interpolation for the varying inputs. */
262 void
263 fs_visitor::emit_interpolation_setup_gen6()
264 {
265 fs_builder abld = bld.annotate("compute pixel centers");
266
267 this->pixel_x = vgrf(glsl_type::float_type);
268 this->pixel_y = vgrf(glsl_type::float_type);
269
270 for (unsigned i = 0; i < DIV_ROUND_UP(dispatch_width, 16); i++) {
271 const fs_builder hbld = abld.group(MIN2(16, dispatch_width), i);
272 struct brw_reg gi_uw = retype(brw_vec1_grf(1 + i, 0), BRW_REGISTER_TYPE_UW);
273
274 if (devinfo->gen >= 8 || dispatch_width == 8) {
275 /* The "Register Region Restrictions" page says for BDW (and newer,
276 * presumably):
277 *
278 * "When destination spans two registers, the source may be one or
279 * two registers. The destination elements must be evenly split
280 * between the two registers."
281 *
282 * Thus we can do a single add(16) in SIMD8 or an add(32) in SIMD16
283 * to compute our pixel centers.
284 */
285 const fs_builder dbld =
286 abld.exec_all().group(hbld.dispatch_width() * 2, 0);
287 fs_reg int_pixel_xy = dbld.vgrf(BRW_REGISTER_TYPE_UW);
288
289 dbld.ADD(int_pixel_xy,
290 fs_reg(stride(suboffset(gi_uw, 4), 1, 4, 0)),
291 fs_reg(brw_imm_v(0x11001010)));
292
293 hbld.emit(FS_OPCODE_PIXEL_X, offset(pixel_x, hbld, i), int_pixel_xy);
294 hbld.emit(FS_OPCODE_PIXEL_Y, offset(pixel_y, hbld, i), int_pixel_xy);
295 } else {
296 /* The "Register Region Restrictions" page says for SNB, IVB, HSW:
297 *
298 * "When destination spans two registers, the source MUST span
299 * two registers."
300 *
301 * Since the GRF source of the ADD will only read a single register,
302 * we must do two separate ADDs in SIMD16.
303 */
304 const fs_reg int_pixel_x = hbld.vgrf(BRW_REGISTER_TYPE_UW);
305 const fs_reg int_pixel_y = hbld.vgrf(BRW_REGISTER_TYPE_UW);
306
307 hbld.ADD(int_pixel_x,
308 fs_reg(stride(suboffset(gi_uw, 4), 2, 4, 0)),
309 fs_reg(brw_imm_v(0x10101010)));
310 hbld.ADD(int_pixel_y,
311 fs_reg(stride(suboffset(gi_uw, 5), 2, 4, 0)),
312 fs_reg(brw_imm_v(0x11001100)));
313
314 /* As of gen6, we can no longer mix float and int sources. We have
315 * to turn the integer pixel centers into floats for their actual
316 * use.
317 */
318 hbld.MOV(offset(pixel_x, hbld, i), int_pixel_x);
319 hbld.MOV(offset(pixel_y, hbld, i), int_pixel_y);
320 }
321 }
322
323 abld = bld.annotate("compute pos.w");
324 this->pixel_w = fetch_payload_reg(abld, payload.source_w_reg);
325 this->wpos_w = vgrf(glsl_type::float_type);
326 abld.emit(SHADER_OPCODE_RCP, this->wpos_w, this->pixel_w);
327
328 struct brw_wm_prog_data *wm_prog_data = brw_wm_prog_data(prog_data);
329
330 for (int i = 0; i < BRW_BARYCENTRIC_MODE_COUNT; ++i) {
331 this->delta_xy[i] = fetch_barycentric_reg(
332 bld, payload.barycentric_coord_reg[i]);
333 }
334
335 uint32_t centroid_modes = wm_prog_data->barycentric_interp_modes &
336 (1 << BRW_BARYCENTRIC_PERSPECTIVE_CENTROID |
337 1 << BRW_BARYCENTRIC_NONPERSPECTIVE_CENTROID);
338
339 if (devinfo->needs_unlit_centroid_workaround && centroid_modes) {
340 /* Get the pixel/sample mask into f0 so that we know which
341 * pixels are lit. Then, for each channel that is unlit,
342 * replace the centroid data with non-centroid data.
343 */
344 for (unsigned i = 0; i < DIV_ROUND_UP(dispatch_width, 16); i++) {
345 bld.exec_all().group(1, 0)
346 .MOV(retype(brw_flag_reg(0, i), BRW_REGISTER_TYPE_UW),
347 retype(brw_vec1_grf(1 + i, 7), BRW_REGISTER_TYPE_UW));
348 }
349
350 for (int i = 0; i < BRW_BARYCENTRIC_MODE_COUNT; ++i) {
351 if (!(centroid_modes & (1 << i)))
352 continue;
353
354 const fs_reg centroid_delta_xy = delta_xy[i];
355 const fs_reg &pixel_delta_xy = delta_xy[i - 1];
356
357 delta_xy[i] = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
358
359 for (unsigned c = 0; c < 2; c++) {
360 for (unsigned q = 0; q < dispatch_width / 8; q++) {
361 set_predicate(BRW_PREDICATE_NORMAL,
362 bld.half(q).SEL(half(offset(delta_xy[i], bld, c), q),
363 half(offset(centroid_delta_xy, bld, c), q),
364 half(offset(pixel_delta_xy, bld, c), q)));
365 }
366 }
367 }
368 }
369 }
370
371 static enum brw_conditional_mod
372 cond_for_alpha_func(GLenum func)
373 {
374 switch(func) {
375 case GL_GREATER:
376 return BRW_CONDITIONAL_G;
377 case GL_GEQUAL:
378 return BRW_CONDITIONAL_GE;
379 case GL_LESS:
380 return BRW_CONDITIONAL_L;
381 case GL_LEQUAL:
382 return BRW_CONDITIONAL_LE;
383 case GL_EQUAL:
384 return BRW_CONDITIONAL_EQ;
385 case GL_NOTEQUAL:
386 return BRW_CONDITIONAL_NEQ;
387 default:
388 unreachable("Not reached");
389 }
390 }
391
392 /**
393 * Alpha test support for when we compile it into the shader instead
394 * of using the normal fixed-function alpha test.
395 */
396 void
397 fs_visitor::emit_alpha_test()
398 {
399 assert(stage == MESA_SHADER_FRAGMENT);
400 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
401 const fs_builder abld = bld.annotate("Alpha test");
402
403 fs_inst *cmp;
404 if (key->alpha_test_func == GL_ALWAYS)
405 return;
406
407 if (key->alpha_test_func == GL_NEVER) {
408 /* f0.1 = 0 */
409 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
410 BRW_REGISTER_TYPE_UW));
411 cmp = abld.CMP(bld.null_reg_f(), some_reg, some_reg,
412 BRW_CONDITIONAL_NEQ);
413 } else {
414 /* RT0 alpha */
415 fs_reg color = offset(outputs[0], bld, 3);
416
417 /* f0.1 &= func(color, ref) */
418 cmp = abld.CMP(bld.null_reg_f(), color, brw_imm_f(key->alpha_test_ref),
419 cond_for_alpha_func(key->alpha_test_func));
420 }
421 cmp->predicate = BRW_PREDICATE_NORMAL;
422 cmp->flag_subreg = 1;
423 }
424
425 fs_inst *
426 fs_visitor::emit_single_fb_write(const fs_builder &bld,
427 fs_reg color0, fs_reg color1,
428 fs_reg src0_alpha, unsigned components)
429 {
430 assert(stage == MESA_SHADER_FRAGMENT);
431 struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);
432
433 /* Hand over gl_FragDepth or the payload depth. */
434 const fs_reg dst_depth = fetch_payload_reg(bld, payload.dest_depth_reg);
435 fs_reg src_depth, src_stencil;
436
437 if (source_depth_to_render_target) {
438 if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
439 src_depth = frag_depth;
440 else
441 src_depth = fetch_payload_reg(bld, payload.source_depth_reg);
442 }
443
444 if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL))
445 src_stencil = frag_stencil;
446
447 const fs_reg sources[] = {
448 color0, color1, src0_alpha, src_depth, dst_depth, src_stencil,
449 (prog_data->uses_omask ? sample_mask : fs_reg()),
450 brw_imm_ud(components)
451 };
452 assert(ARRAY_SIZE(sources) - 1 == FB_WRITE_LOGICAL_SRC_COMPONENTS);
453 fs_inst *write = bld.emit(FS_OPCODE_FB_WRITE_LOGICAL, fs_reg(),
454 sources, ARRAY_SIZE(sources));
455
456 if (prog_data->uses_kill) {
457 write->predicate = BRW_PREDICATE_NORMAL;
458 write->flag_subreg = sample_mask_flag_subreg(this);
459 }
460
461 return write;
462 }
463
464 void
465 fs_visitor::emit_fb_writes()
466 {
467 assert(stage == MESA_SHADER_FRAGMENT);
468 struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);
469 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
470
471 fs_inst *inst = NULL;
472
473 if (source_depth_to_render_target && devinfo->gen == 6) {
474 /* For outputting oDepth on gen6, SIMD8 writes have to be used. This
475 * would require SIMD8 moves of each half to message regs, e.g. by using
476 * the SIMD lowering pass. Unfortunately this is more difficult than it
477 * sounds because the SIMD8 single-source message lacks channel selects
478 * for the second and third subspans.
479 */
480 limit_dispatch_width(8, "Depth writes unsupported in SIMD16+ mode.\n");
481 }
482
483 if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
484 /* From the 'Render Target Write message' section of the docs:
485 * "Output Stencil is not supported with SIMD16 Render Target Write
486 * Messages."
487 */
488 limit_dispatch_width(8, "gl_FragStencilRefARB unsupported "
489 "in SIMD16+ mode.\n");
490 }
491
492 /* ANV doesn't know about sample mask output during the wm key creation
493 * so we compute if we need replicate alpha and emit alpha to coverage
494 * workaround here.
495 */
496 const bool replicate_alpha = key->alpha_test_replicate_alpha ||
497 (key->nr_color_regions > 1 && key->alpha_to_coverage &&
498 (sample_mask.file == BAD_FILE || devinfo->gen == 6));
499
500 for (int target = 0; target < key->nr_color_regions; target++) {
501 /* Skip over outputs that weren't written. */
502 if (this->outputs[target].file == BAD_FILE)
503 continue;
504
505 const fs_builder abld = bld.annotate(
506 ralloc_asprintf(this->mem_ctx, "FB write target %d", target));
507
508 fs_reg src0_alpha;
509 if (devinfo->gen >= 6 && replicate_alpha && target != 0)
510 src0_alpha = offset(outputs[0], bld, 3);
511
512 inst = emit_single_fb_write(abld, this->outputs[target],
513 this->dual_src_output, src0_alpha, 4);
514 inst->target = target;
515 }
516
517 prog_data->dual_src_blend = (this->dual_src_output.file != BAD_FILE &&
518 this->outputs[0].file != BAD_FILE);
519 assert(!prog_data->dual_src_blend || key->nr_color_regions == 1);
520
521 if (inst == NULL) {
522 /* Even if there's no color buffers enabled, we still need to send
523 * alpha out the pipeline to our null renderbuffer to support
524 * alpha-testing, alpha-to-coverage, and so on.
525 */
526 /* FINISHME: Factor out this frequently recurring pattern into a
527 * helper function.
528 */
529 const fs_reg srcs[] = { reg_undef, reg_undef,
530 reg_undef, offset(this->outputs[0], bld, 3) };
531 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 4);
532 bld.LOAD_PAYLOAD(tmp, srcs, 4, 0);
533
534 inst = emit_single_fb_write(bld, tmp, reg_undef, reg_undef, 4);
535 inst->target = 0;
536 }
537
538 inst->last_rt = true;
539 inst->eot = true;
540
541 if (devinfo->gen == 11 && prog_data->dual_src_blend) {
542 /* The dual-source RT write messages fail to release the thread
543 * dependency on ICL with SIMD32 dispatch, leading to hangs.
544 *
545 * XXX - Emit an extra single-source NULL RT-write marked LastRT in
546 * order to release the thread dependency without disabling
547 * SIMD32.
548 */
549 limit_dispatch_width(16, "Dual source blending unsupported "
550 "in SIMD32 mode.\n");
551 }
552 }
553
554 void
555 fs_visitor::emit_urb_writes(const fs_reg &gs_vertex_count)
556 {
557 int slot, urb_offset, length;
558 int starting_urb_offset = 0;
559 const struct brw_vue_prog_data *vue_prog_data =
560 brw_vue_prog_data(this->prog_data);
561 const struct brw_vs_prog_key *vs_key =
562 (const struct brw_vs_prog_key *) this->key;
563 const GLbitfield64 psiz_mask =
564 VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT | VARYING_BIT_PSIZ;
565 const struct brw_vue_map *vue_map = &vue_prog_data->vue_map;
566 bool flush;
567 fs_reg sources[8];
568 fs_reg urb_handle;
569
570 if (stage == MESA_SHADER_TESS_EVAL)
571 urb_handle = fs_reg(retype(brw_vec8_grf(4, 0), BRW_REGISTER_TYPE_UD));
572 else
573 urb_handle = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
574
575 opcode opcode = SHADER_OPCODE_URB_WRITE_SIMD8;
576 int header_size = 1;
577 fs_reg per_slot_offsets;
578
579 if (stage == MESA_SHADER_GEOMETRY) {
580 const struct brw_gs_prog_data *gs_prog_data =
581 brw_gs_prog_data(this->prog_data);
582
583 /* We need to increment the Global Offset to skip over the control data
584 * header and the extra "Vertex Count" field (1 HWord) at the beginning
585 * of the VUE. We're counting in OWords, so the units are doubled.
586 */
587 starting_urb_offset = 2 * gs_prog_data->control_data_header_size_hwords;
588 if (gs_prog_data->static_vertex_count == -1)
589 starting_urb_offset += 2;
590
591 /* We also need to use per-slot offsets. The per-slot offset is the
592 * Vertex Count. SIMD8 mode processes 8 different primitives at a
593 * time; each may output a different number of vertices.
594 */
595 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT;
596 header_size++;
597
598 /* The URB offset is in 128-bit units, so we need to multiply by 2 */
599 const int output_vertex_size_owords =
600 gs_prog_data->output_vertex_size_hwords * 2;
601
602 if (gs_vertex_count.file == IMM) {
603 per_slot_offsets = brw_imm_ud(output_vertex_size_owords *
604 gs_vertex_count.ud);
605 } else {
606 per_slot_offsets = vgrf(glsl_type::uint_type);
607 bld.MUL(per_slot_offsets, gs_vertex_count,
608 brw_imm_ud(output_vertex_size_owords));
609 }
610 }
611
612 length = 0;
613 urb_offset = starting_urb_offset;
614 flush = false;
615
616 /* SSO shaders can have VUE slots allocated which are never actually
617 * written to, so ignore them when looking for the last (written) slot.
618 */
619 int last_slot = vue_map->num_slots - 1;
620 while (last_slot > 0 &&
621 (vue_map->slot_to_varying[last_slot] == BRW_VARYING_SLOT_PAD ||
622 outputs[vue_map->slot_to_varying[last_slot]].file == BAD_FILE)) {
623 last_slot--;
624 }
625
626 bool urb_written = false;
627 for (slot = 0; slot < vue_map->num_slots; slot++) {
628 int varying = vue_map->slot_to_varying[slot];
629 switch (varying) {
630 case VARYING_SLOT_PSIZ: {
631 /* The point size varying slot is the vue header and is always in the
632 * vue map. But often none of the special varyings that live there
633 * are written and in that case we can skip writing to the vue
634 * header, provided the corresponding state properly clamps the
635 * values further down the pipeline. */
636 if ((vue_map->slots_valid & psiz_mask) == 0) {
637 assert(length == 0);
638 urb_offset++;
639 break;
640 }
641
642 fs_reg zero(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
643 bld.MOV(zero, brw_imm_ud(0u));
644
645 sources[length++] = zero;
646 if (vue_map->slots_valid & VARYING_BIT_LAYER)
647 sources[length++] = this->outputs[VARYING_SLOT_LAYER];
648 else
649 sources[length++] = zero;
650
651 if (vue_map->slots_valid & VARYING_BIT_VIEWPORT)
652 sources[length++] = this->outputs[VARYING_SLOT_VIEWPORT];
653 else
654 sources[length++] = zero;
655
656 if (vue_map->slots_valid & VARYING_BIT_PSIZ)
657 sources[length++] = this->outputs[VARYING_SLOT_PSIZ];
658 else
659 sources[length++] = zero;
660 break;
661 }
662 case BRW_VARYING_SLOT_NDC:
663 case VARYING_SLOT_EDGE:
664 unreachable("unexpected scalar vs output");
665 break;
666
667 default:
668 /* gl_Position is always in the vue map, but isn't always written by
669 * the shader. Other varyings (clip distances) get added to the vue
670 * map but don't always get written. In those cases, the
671 * corresponding this->output[] slot will be invalid we and can skip
672 * the urb write for the varying. If we've already queued up a vue
673 * slot for writing we flush a mlen 5 urb write, otherwise we just
674 * advance the urb_offset.
675 */
676 if (varying == BRW_VARYING_SLOT_PAD ||
677 this->outputs[varying].file == BAD_FILE) {
678 if (length > 0)
679 flush = true;
680 else
681 urb_offset++;
682 break;
683 }
684
685 if (stage == MESA_SHADER_VERTEX && vs_key->clamp_vertex_color &&
686 (varying == VARYING_SLOT_COL0 ||
687 varying == VARYING_SLOT_COL1 ||
688 varying == VARYING_SLOT_BFC0 ||
689 varying == VARYING_SLOT_BFC1)) {
690 /* We need to clamp these guys, so do a saturating MOV into a
691 * temp register and use that for the payload.
692 */
693 for (int i = 0; i < 4; i++) {
694 fs_reg reg = fs_reg(VGRF, alloc.allocate(1), outputs[varying].type);
695 fs_reg src = offset(this->outputs[varying], bld, i);
696 set_saturate(true, bld.MOV(reg, src));
697 sources[length++] = reg;
698 }
699 } else {
700 for (unsigned i = 0; i < 4; i++)
701 sources[length++] = offset(this->outputs[varying], bld, i);
702 }
703 break;
704 }
705
706 const fs_builder abld = bld.annotate("URB write");
707
708 /* If we've queued up 8 registers of payload (2 VUE slots), if this is
709 * the last slot or if we need to flush (see BAD_FILE varying case
710 * above), emit a URB write send now to flush out the data.
711 */
712 if (length == 8 || (length > 0 && slot == last_slot))
713 flush = true;
714 if (flush) {
715 fs_reg *payload_sources =
716 ralloc_array(mem_ctx, fs_reg, length + header_size);
717 fs_reg payload = fs_reg(VGRF, alloc.allocate(length + header_size),
718 BRW_REGISTER_TYPE_F);
719 payload_sources[0] = urb_handle;
720
721 if (opcode == SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT)
722 payload_sources[1] = per_slot_offsets;
723
724 memcpy(&payload_sources[header_size], sources,
725 length * sizeof sources[0]);
726
727 abld.LOAD_PAYLOAD(payload, payload_sources, length + header_size,
728 header_size);
729
730 fs_inst *inst = abld.emit(opcode, reg_undef, payload);
731
732 /* For ICL WA 1805992985 one needs additional write in the end. */
733 if (devinfo->gen == 11 && stage == MESA_SHADER_TESS_EVAL)
734 inst->eot = false;
735 else
736 inst->eot = slot == last_slot && stage != MESA_SHADER_GEOMETRY;
737
738 inst->mlen = length + header_size;
739 inst->offset = urb_offset;
740 urb_offset = starting_urb_offset + slot + 1;
741 length = 0;
742 flush = false;
743 urb_written = true;
744 }
745 }
746
747 /* If we don't have any valid slots to write, just do a minimal urb write
748 * send to terminate the shader. This includes 1 slot of undefined data,
749 * because it's invalid to write 0 data:
750 *
751 * From the Broadwell PRM, Volume 7: 3D Media GPGPU, Shared Functions -
752 * Unified Return Buffer (URB) > URB_SIMD8_Write and URB_SIMD8_Read >
753 * Write Data Payload:
754 *
755 * "The write data payload can be between 1 and 8 message phases long."
756 */
757 if (!urb_written) {
758 /* For GS, just turn EmitVertex() into a no-op. We don't want it to
759 * end the thread, and emit_gs_thread_end() already emits a SEND with
760 * EOT at the end of the program for us.
761 */
762 if (stage == MESA_SHADER_GEOMETRY)
763 return;
764
765 fs_reg payload = fs_reg(VGRF, alloc.allocate(2), BRW_REGISTER_TYPE_UD);
766 bld.exec_all().MOV(payload, urb_handle);
767
768 fs_inst *inst = bld.emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, payload);
769 inst->eot = true;
770 inst->mlen = 2;
771 inst->offset = 1;
772 return;
773 }
774
775 /* ICL WA 1805992985:
776 *
777 * ICLLP GPU hangs on one of tessellation vkcts tests with DS not done. The
778 * send cycle, which is a urb write with an eot must be 4 phases long and
779 * all 8 lanes must valid.
780 */
781 if (devinfo->gen == 11 && stage == MESA_SHADER_TESS_EVAL) {
782 fs_reg payload = fs_reg(VGRF, alloc.allocate(6), BRW_REGISTER_TYPE_UD);
783
784 /* Workaround requires all 8 channels (lanes) to be valid. This is
785 * understood to mean they all need to be alive. First trick is to find
786 * a live channel and copy its urb handle for all the other channels to
787 * make sure all handles are valid.
788 */
789 bld.exec_all().MOV(payload, bld.emit_uniformize(urb_handle));
790
791 /* Second trick is to use masked URB write where one can tell the HW to
792 * actually write data only for selected channels even though all are
793 * active.
794 * Third trick is to take advantage of the must-be-zero (MBZ) area in
795 * the very beginning of the URB.
796 *
797 * One masks data to be written only for the first channel and uses
798 * offset zero explicitly to land data to the MBZ area avoiding trashing
799 * any other part of the URB.
800 *
801 * Since the WA says that the write needs to be 4 phases long one uses
802 * 4 slots data. All are explicitly zeros in order to to keep the MBZ
803 * area written as zeros.
804 */
805 bld.exec_all().MOV(offset(payload, bld, 1), brw_imm_ud(0x10000u));
806 bld.exec_all().MOV(offset(payload, bld, 2), brw_imm_ud(0u));
807 bld.exec_all().MOV(offset(payload, bld, 3), brw_imm_ud(0u));
808 bld.exec_all().MOV(offset(payload, bld, 4), brw_imm_ud(0u));
809 bld.exec_all().MOV(offset(payload, bld, 5), brw_imm_ud(0u));
810
811 fs_inst *inst = bld.exec_all().emit(SHADER_OPCODE_URB_WRITE_SIMD8_MASKED,
812 reg_undef, payload);
813 inst->eot = true;
814 inst->mlen = 6;
815 inst->offset = 0;
816 }
817 }
818
819 void
820 fs_visitor::emit_cs_terminate()
821 {
822 assert(devinfo->gen >= 7);
823
824 /* We are getting the thread ID from the compute shader header */
825 assert(stage == MESA_SHADER_COMPUTE);
826
827 /* We can't directly send from g0, since sends with EOT have to use
828 * g112-127. So, copy it to a virtual register, The register allocator will
829 * make sure it uses the appropriate register range.
830 */
831 struct brw_reg g0 = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD);
832 fs_reg payload = fs_reg(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
833 bld.group(8, 0).exec_all().MOV(payload, g0);
834
835 /* Send a message to the thread spawner to terminate the thread. */
836 fs_inst *inst = bld.exec_all()
837 .emit(CS_OPCODE_CS_TERMINATE, reg_undef, payload);
838 inst->eot = true;
839 }
840
841 void
842 fs_visitor::emit_barrier()
843 {
844 uint32_t barrier_id_mask;
845 switch (devinfo->gen) {
846 case 7:
847 case 8:
848 barrier_id_mask = 0x0f000000u; break;
849 case 9:
850 case 10:
851 barrier_id_mask = 0x8f000000u; break;
852 case 11:
853 case 12:
854 barrier_id_mask = 0x7f000000u; break;
855 default:
856 unreachable("barrier is only available on gen >= 7");
857 }
858
859 /* We are getting the barrier ID from the compute shader header */
860 assert(stage == MESA_SHADER_COMPUTE);
861
862 fs_reg payload = fs_reg(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
863
864 /* Clear the message payload */
865 bld.exec_all().group(8, 0).MOV(payload, brw_imm_ud(0u));
866
867 /* Copy the barrier id from r0.2 to the message payload reg.2 */
868 fs_reg r0_2 = fs_reg(retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD));
869 bld.exec_all().group(1, 0).AND(component(payload, 2), r0_2,
870 brw_imm_ud(barrier_id_mask));
871
872 /* Emit a gateway "barrier" message using the payload we set up, followed
873 * by a wait instruction.
874 */
875 bld.exec_all().emit(SHADER_OPCODE_BARRIER, reg_undef, payload);
876 }
877
878 fs_visitor::fs_visitor(const struct brw_compiler *compiler, void *log_data,
879 void *mem_ctx,
880 const brw_base_prog_key *key,
881 struct brw_stage_prog_data *prog_data,
882 const nir_shader *shader,
883 unsigned dispatch_width,
884 int shader_time_index,
885 const struct brw_vue_map *input_vue_map)
886 : backend_shader(compiler, log_data, mem_ctx, shader, prog_data),
887 key(key), gs_compile(NULL), prog_data(prog_data),
888 input_vue_map(input_vue_map),
889 dispatch_width(dispatch_width),
890 shader_time_index(shader_time_index),
891 bld(fs_builder(this, dispatch_width).at_end())
892 {
893 init();
894 }
895
896 fs_visitor::fs_visitor(const struct brw_compiler *compiler, void *log_data,
897 void *mem_ctx,
898 struct brw_gs_compile *c,
899 struct brw_gs_prog_data *prog_data,
900 const nir_shader *shader,
901 int shader_time_index)
902 : backend_shader(compiler, log_data, mem_ctx, shader,
903 &prog_data->base.base),
904 key(&c->key.base), gs_compile(c),
905 prog_data(&prog_data->base.base),
906 dispatch_width(8),
907 shader_time_index(shader_time_index),
908 bld(fs_builder(this, dispatch_width).at_end())
909 {
910 init();
911 }
912
913
914 void
915 fs_visitor::init()
916 {
917 if (key)
918 this->key_tex = &key->tex;
919 else
920 this->key_tex = NULL;
921
922 this->max_dispatch_width = 32;
923 this->prog_data = this->stage_prog_data;
924
925 this->failed = false;
926
927 this->nir_locals = NULL;
928 this->nir_ssa_values = NULL;
929
930 memset(&this->payload, 0, sizeof(this->payload));
931 this->source_depth_to_render_target = false;
932 this->runtime_check_aads_emit = false;
933 this->first_non_payload_grf = 0;
934 this->max_grf = devinfo->gen >= 7 ? GEN7_MRF_HACK_START : BRW_MAX_GRF;
935
936 this->live_intervals = NULL;
937 this->regs_live_at_ip = NULL;
938
939 this->uniforms = 0;
940 this->last_scratch = 0;
941 this->pull_constant_loc = NULL;
942 this->push_constant_loc = NULL;
943
944 this->shader_stats.scheduler_mode = NULL;
945 this->shader_stats.promoted_constants = 0,
946
947 this->grf_used = 0;
948 this->spilled_any_registers = false;
949 }
950
951 fs_visitor::~fs_visitor()
952 {
953 }