Revert "intel/fs: Move the scalar-region conversion to the generator."
[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 && dispatch_width == 16) {
180 for (unsigned i = 0; i < 2; i++) {
181 abld.half(i).ADD(half(offset(delta_xy, abld, i), 0),
182 half(this->pixel_x, i), xstart);
183 abld.half(i).ADD(half(offset(delta_xy, abld, i), 1),
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 /** Emits the interpolation for the varying inputs. */
204 void
205 fs_visitor::emit_interpolation_setup_gen6()
206 {
207 fs_builder abld = bld.annotate("compute pixel centers");
208
209 this->pixel_x = vgrf(glsl_type::float_type);
210 this->pixel_y = vgrf(glsl_type::float_type);
211
212 for (unsigned i = 0; i < DIV_ROUND_UP(dispatch_width, 16); i++) {
213 const fs_builder hbld = abld.group(MIN2(16, dispatch_width), i);
214 struct brw_reg gi_uw = retype(brw_vec1_grf(1 + i, 0), BRW_REGISTER_TYPE_UW);
215
216 if (devinfo->gen >= 8 || dispatch_width == 8) {
217 /* The "Register Region Restrictions" page says for BDW (and newer,
218 * presumably):
219 *
220 * "When destination spans two registers, the source may be one or
221 * two registers. The destination elements must be evenly split
222 * between the two registers."
223 *
224 * Thus we can do a single add(16) in SIMD8 or an add(32) in SIMD16
225 * to compute our pixel centers.
226 */
227 const fs_builder dbld =
228 abld.exec_all().group(hbld.dispatch_width() * 2, 0);
229 fs_reg int_pixel_xy = dbld.vgrf(BRW_REGISTER_TYPE_UW);
230
231 dbld.ADD(int_pixel_xy,
232 fs_reg(stride(suboffset(gi_uw, 4), 1, 4, 0)),
233 fs_reg(brw_imm_v(0x11001010)));
234
235 hbld.emit(FS_OPCODE_PIXEL_X, offset(pixel_x, hbld, i), int_pixel_xy);
236 hbld.emit(FS_OPCODE_PIXEL_Y, offset(pixel_y, hbld, i), int_pixel_xy);
237 } else {
238 /* The "Register Region Restrictions" page says for SNB, IVB, HSW:
239 *
240 * "When destination spans two registers, the source MUST span
241 * two registers."
242 *
243 * Since the GRF source of the ADD will only read a single register,
244 * we must do two separate ADDs in SIMD16.
245 */
246 const fs_reg int_pixel_x = hbld.vgrf(BRW_REGISTER_TYPE_UW);
247 const fs_reg int_pixel_y = hbld.vgrf(BRW_REGISTER_TYPE_UW);
248
249 hbld.ADD(int_pixel_x,
250 fs_reg(stride(suboffset(gi_uw, 4), 2, 4, 0)),
251 fs_reg(brw_imm_v(0x10101010)));
252 hbld.ADD(int_pixel_y,
253 fs_reg(stride(suboffset(gi_uw, 5), 2, 4, 0)),
254 fs_reg(brw_imm_v(0x11001100)));
255
256 /* As of gen6, we can no longer mix float and int sources. We have
257 * to turn the integer pixel centers into floats for their actual
258 * use.
259 */
260 hbld.MOV(offset(pixel_x, hbld, i), int_pixel_x);
261 hbld.MOV(offset(pixel_y, hbld, i), int_pixel_y);
262 }
263 }
264
265 abld = bld.annotate("compute pos.w");
266 this->pixel_w = fetch_payload_reg(abld, payload.source_w_reg);
267 this->wpos_w = vgrf(glsl_type::float_type);
268 abld.emit(SHADER_OPCODE_RCP, this->wpos_w, this->pixel_w);
269
270 struct brw_wm_prog_data *wm_prog_data = brw_wm_prog_data(prog_data);
271
272 for (int i = 0; i < BRW_BARYCENTRIC_MODE_COUNT; ++i) {
273 this->delta_xy[i] = fetch_payload_reg(
274 bld, payload.barycentric_coord_reg[i], BRW_REGISTER_TYPE_F, 2);
275 }
276
277 uint32_t centroid_modes = wm_prog_data->barycentric_interp_modes &
278 (1 << BRW_BARYCENTRIC_PERSPECTIVE_CENTROID |
279 1 << BRW_BARYCENTRIC_NONPERSPECTIVE_CENTROID);
280
281 if (devinfo->needs_unlit_centroid_workaround && centroid_modes) {
282 /* Get the pixel/sample mask into f0 so that we know which
283 * pixels are lit. Then, for each channel that is unlit,
284 * replace the centroid data with non-centroid data.
285 */
286 for (unsigned i = 0; i < DIV_ROUND_UP(dispatch_width, 16); i++) {
287 bld.exec_all().group(1, 0)
288 .MOV(retype(brw_flag_reg(0, i), BRW_REGISTER_TYPE_UW),
289 retype(brw_vec1_grf(1 + i, 7), BRW_REGISTER_TYPE_UW));
290 }
291
292 for (int i = 0; i < BRW_BARYCENTRIC_MODE_COUNT; ++i) {
293 if (!(centroid_modes & (1 << i)))
294 continue;
295
296 const fs_reg &pixel_delta_xy = delta_xy[i - 1];
297
298 for (unsigned q = 0; q < dispatch_width / 8; q++) {
299 for (unsigned c = 0; c < 2; c++) {
300 const unsigned idx = c + (q & 2) + (q & 1) * dispatch_width / 8;
301 set_predicate_inv(
302 BRW_PREDICATE_NORMAL, true,
303 bld.half(q).MOV(horiz_offset(delta_xy[i], idx * 8),
304 horiz_offset(pixel_delta_xy, idx * 8)));
305 }
306 }
307 }
308 }
309 }
310
311 static enum brw_conditional_mod
312 cond_for_alpha_func(GLenum func)
313 {
314 switch(func) {
315 case GL_GREATER:
316 return BRW_CONDITIONAL_G;
317 case GL_GEQUAL:
318 return BRW_CONDITIONAL_GE;
319 case GL_LESS:
320 return BRW_CONDITIONAL_L;
321 case GL_LEQUAL:
322 return BRW_CONDITIONAL_LE;
323 case GL_EQUAL:
324 return BRW_CONDITIONAL_EQ;
325 case GL_NOTEQUAL:
326 return BRW_CONDITIONAL_NEQ;
327 default:
328 unreachable("Not reached");
329 }
330 }
331
332 /**
333 * Alpha test support for when we compile it into the shader instead
334 * of using the normal fixed-function alpha test.
335 */
336 void
337 fs_visitor::emit_alpha_test()
338 {
339 assert(stage == MESA_SHADER_FRAGMENT);
340 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
341 const fs_builder abld = bld.annotate("Alpha test");
342
343 fs_inst *cmp;
344 if (key->alpha_test_func == GL_ALWAYS)
345 return;
346
347 if (key->alpha_test_func == GL_NEVER) {
348 /* f0.1 = 0 */
349 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
350 BRW_REGISTER_TYPE_UW));
351 cmp = abld.CMP(bld.null_reg_f(), some_reg, some_reg,
352 BRW_CONDITIONAL_NEQ);
353 } else {
354 /* RT0 alpha */
355 fs_reg color = offset(outputs[0], bld, 3);
356
357 /* f0.1 &= func(color, ref) */
358 cmp = abld.CMP(bld.null_reg_f(), color, brw_imm_f(key->alpha_test_ref),
359 cond_for_alpha_func(key->alpha_test_func));
360 }
361 cmp->predicate = BRW_PREDICATE_NORMAL;
362 cmp->flag_subreg = 1;
363 }
364
365 fs_inst *
366 fs_visitor::emit_single_fb_write(const fs_builder &bld,
367 fs_reg color0, fs_reg color1,
368 fs_reg src0_alpha, unsigned components)
369 {
370 assert(stage == MESA_SHADER_FRAGMENT);
371 struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);
372
373 /* Hand over gl_FragDepth or the payload depth. */
374 const fs_reg dst_depth = fetch_payload_reg(bld, payload.dest_depth_reg);
375 fs_reg src_depth, src_stencil;
376
377 if (source_depth_to_render_target) {
378 if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
379 src_depth = frag_depth;
380 else
381 src_depth = fetch_payload_reg(bld, payload.source_depth_reg);
382 }
383
384 if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL))
385 src_stencil = frag_stencil;
386
387 const fs_reg sources[] = {
388 color0, color1, src0_alpha, src_depth, dst_depth, src_stencil,
389 (prog_data->uses_omask ? sample_mask : fs_reg()),
390 brw_imm_ud(components)
391 };
392 assert(ARRAY_SIZE(sources) - 1 == FB_WRITE_LOGICAL_SRC_COMPONENTS);
393 fs_inst *write = bld.emit(FS_OPCODE_FB_WRITE_LOGICAL, fs_reg(),
394 sources, ARRAY_SIZE(sources));
395
396 if (prog_data->uses_kill) {
397 write->predicate = BRW_PREDICATE_NORMAL;
398 write->flag_subreg = 1;
399 }
400
401 return write;
402 }
403
404 void
405 fs_visitor::emit_alpha_to_coverage_workaround(const fs_reg &src0_alpha)
406 {
407 /* We need to compute alpha to coverage dithering manually in shader
408 * and replace sample mask store with the bitwise-AND of sample mask and
409 * alpha to coverage dithering.
410 *
411 * The following formula is used to compute final sample mask:
412 * m = int(16.0 * clamp(src0_alpha, 0.0, 1.0))
413 * dither_mask = 0x1111 * ((0xfea80 >> (m & ~3)) & 0xf) |
414 * 0x0808 * (m & 2) | 0x0100 * (m & 1)
415 * sample_mask = sample_mask & dither_mask
416 *
417 * It gives a number of ones proportional to the alpha for 2, 4, 8 or 16
418 * least significant bits of the result:
419 * 0.0000 0000000000000000
420 * 0.0625 0000000100000000
421 * 0.1250 0001000000010000
422 * 0.1875 0001000100010000
423 * 0.2500 1000100010001000
424 * 0.3125 1000100110001000
425 * 0.3750 1001100010011000
426 * 0.4375 1001100110011000
427 * 0.5000 1010101010101010
428 * 0.5625 1010101110101010
429 * 0.6250 1011101010111010
430 * 0.6875 1011101110111010
431 * 0.7500 1110111011101110
432 * 0.8125 1110111111101110
433 * 0.8750 1111111011111110
434 * 0.9375 1111111111111110
435 * 1.0000 1111111111111111
436 */
437 const fs_builder abld = bld.annotate("compute alpha_to_coverage & "
438 "sample_mask");
439
440 /* clamp(src0_alpha, 0.f, 1.f) */
441 const fs_reg float_tmp = abld.vgrf(BRW_REGISTER_TYPE_F);
442 set_saturate(true, abld.MOV(float_tmp, src0_alpha));
443
444 /* 16.0 * clamp(src0_alpha, 0.0, 1.0) */
445 abld.MUL(float_tmp, float_tmp, brw_imm_f(16.0));
446
447 /* m = int(16.0 * clamp(src0_alpha, 0.0, 1.0)) */
448 const fs_reg m = abld.vgrf(BRW_REGISTER_TYPE_UW);
449 abld.MOV(m, float_tmp);
450
451 /* 0x1111 * ((0xfea80 >> (m & ~3)) & 0xf) */
452 const fs_reg int_tmp_1 = abld.vgrf(BRW_REGISTER_TYPE_UW);
453 const fs_reg shift_const = abld.vgrf(BRW_REGISTER_TYPE_UD);
454 abld.MOV(shift_const, brw_imm_d(0xfea80));
455 abld.AND(int_tmp_1, m, brw_imm_uw(~3));
456 abld.SHR(int_tmp_1, shift_const, int_tmp_1);
457 abld.AND(int_tmp_1, int_tmp_1, brw_imm_uw(0xf));
458 abld.MUL(int_tmp_1, int_tmp_1, brw_imm_uw(0x1111));
459
460 /* 0x0808 * (m & 2) */
461 const fs_reg int_tmp_2 = abld.vgrf(BRW_REGISTER_TYPE_UW);
462 abld.AND(int_tmp_2, m, brw_imm_uw(2));
463 abld.MUL(int_tmp_2, int_tmp_2, brw_imm_uw(0x0808));
464
465 abld.OR(int_tmp_1, int_tmp_1, int_tmp_2);
466
467 /* 0x0100 * (m & 1) */
468 const fs_reg int_tmp_3 = abld.vgrf(BRW_REGISTER_TYPE_UW);
469 abld.AND(int_tmp_3, m, brw_imm_uw(1));
470 abld.MUL(int_tmp_3, int_tmp_3, brw_imm_uw(0x0100));
471
472 abld.OR(int_tmp_1, int_tmp_1, int_tmp_3);
473
474 /* sample_mask = sample_mask & dither_mask */
475 const fs_reg mask = abld.vgrf(BRW_REGISTER_TYPE_UD);
476 abld.AND(mask, sample_mask, int_tmp_1);
477 sample_mask = mask;
478 }
479
480 void
481 fs_visitor::emit_fb_writes()
482 {
483 assert(stage == MESA_SHADER_FRAGMENT);
484 struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);
485 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
486
487 fs_inst *inst = NULL;
488
489 if (source_depth_to_render_target && devinfo->gen == 6) {
490 /* For outputting oDepth on gen6, SIMD8 writes have to be used. This
491 * would require SIMD8 moves of each half to message regs, e.g. by using
492 * the SIMD lowering pass. Unfortunately this is more difficult than it
493 * sounds because the SIMD8 single-source message lacks channel selects
494 * for the second and third subspans.
495 */
496 limit_dispatch_width(8, "Depth writes unsupported in SIMD16+ mode.\n");
497 }
498
499 if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
500 /* From the 'Render Target Write message' section of the docs:
501 * "Output Stencil is not supported with SIMD16 Render Target Write
502 * Messages."
503 */
504 limit_dispatch_width(8, "gl_FragStencilRefARB unsupported "
505 "in SIMD16+ mode.\n");
506 }
507
508 /* ANV doesn't know about sample mask output during the wm key creation
509 * so we compute if we need replicate alpha and emit alpha to coverage
510 * workaround here.
511 */
512 prog_data->replicate_alpha = key->alpha_test_replicate_alpha ||
513 (key->nr_color_regions > 1 && key->alpha_to_coverage &&
514 (sample_mask.file == BAD_FILE || devinfo->gen == 6));
515
516 /* From the SKL PRM, Volume 7, "Alpha Coverage":
517 * "If Pixel Shader outputs oMask, AlphaToCoverage is disabled in
518 * hardware, regardless of the state setting for this feature."
519 */
520 if (devinfo->gen > 6 && key->alpha_to_coverage &&
521 sample_mask.file != BAD_FILE && this->outputs[0].file != BAD_FILE)
522 emit_alpha_to_coverage_workaround(offset(this->outputs[0], bld, 3));
523
524 for (int target = 0; target < key->nr_color_regions; target++) {
525 /* Skip over outputs that weren't written. */
526 if (this->outputs[target].file == BAD_FILE)
527 continue;
528
529 const fs_builder abld = bld.annotate(
530 ralloc_asprintf(this->mem_ctx, "FB write target %d", target));
531
532 fs_reg src0_alpha;
533 if (devinfo->gen >= 6 && prog_data->replicate_alpha && target != 0)
534 src0_alpha = offset(outputs[0], bld, 3);
535
536 inst = emit_single_fb_write(abld, this->outputs[target],
537 this->dual_src_output, src0_alpha, 4);
538 inst->target = target;
539 }
540
541 prog_data->dual_src_blend = (this->dual_src_output.file != BAD_FILE &&
542 this->outputs[0].file != BAD_FILE);
543 assert(!prog_data->dual_src_blend || key->nr_color_regions == 1);
544
545 if (inst == NULL) {
546 /* Even if there's no color buffers enabled, we still need to send
547 * alpha out the pipeline to our null renderbuffer to support
548 * alpha-testing, alpha-to-coverage, and so on.
549 */
550 /* FINISHME: Factor out this frequently recurring pattern into a
551 * helper function.
552 */
553 const fs_reg srcs[] = { reg_undef, reg_undef,
554 reg_undef, offset(this->outputs[0], bld, 3) };
555 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 4);
556 bld.LOAD_PAYLOAD(tmp, srcs, 4, 0);
557
558 inst = emit_single_fb_write(bld, tmp, reg_undef, reg_undef, 4);
559 inst->target = 0;
560 }
561
562 inst->last_rt = true;
563 inst->eot = true;
564 }
565
566 void
567 fs_visitor::emit_urb_writes(const fs_reg &gs_vertex_count)
568 {
569 int slot, urb_offset, length;
570 int starting_urb_offset = 0;
571 const struct brw_vue_prog_data *vue_prog_data =
572 brw_vue_prog_data(this->prog_data);
573 const struct brw_vs_prog_key *vs_key =
574 (const struct brw_vs_prog_key *) this->key;
575 const GLbitfield64 psiz_mask =
576 VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT | VARYING_BIT_PSIZ;
577 const struct brw_vue_map *vue_map = &vue_prog_data->vue_map;
578 bool flush;
579 fs_reg sources[8];
580 fs_reg urb_handle;
581
582 if (stage == MESA_SHADER_TESS_EVAL)
583 urb_handle = fs_reg(retype(brw_vec8_grf(4, 0), BRW_REGISTER_TYPE_UD));
584 else
585 urb_handle = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
586
587 opcode opcode = SHADER_OPCODE_URB_WRITE_SIMD8;
588 int header_size = 1;
589 fs_reg per_slot_offsets;
590
591 if (stage == MESA_SHADER_GEOMETRY) {
592 const struct brw_gs_prog_data *gs_prog_data =
593 brw_gs_prog_data(this->prog_data);
594
595 /* We need to increment the Global Offset to skip over the control data
596 * header and the extra "Vertex Count" field (1 HWord) at the beginning
597 * of the VUE. We're counting in OWords, so the units are doubled.
598 */
599 starting_urb_offset = 2 * gs_prog_data->control_data_header_size_hwords;
600 if (gs_prog_data->static_vertex_count == -1)
601 starting_urb_offset += 2;
602
603 /* We also need to use per-slot offsets. The per-slot offset is the
604 * Vertex Count. SIMD8 mode processes 8 different primitives at a
605 * time; each may output a different number of vertices.
606 */
607 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT;
608 header_size++;
609
610 /* The URB offset is in 128-bit units, so we need to multiply by 2 */
611 const int output_vertex_size_owords =
612 gs_prog_data->output_vertex_size_hwords * 2;
613
614 if (gs_vertex_count.file == IMM) {
615 per_slot_offsets = brw_imm_ud(output_vertex_size_owords *
616 gs_vertex_count.ud);
617 } else {
618 per_slot_offsets = vgrf(glsl_type::uint_type);
619 bld.MUL(per_slot_offsets, gs_vertex_count,
620 brw_imm_ud(output_vertex_size_owords));
621 }
622 }
623
624 length = 0;
625 urb_offset = starting_urb_offset;
626 flush = false;
627
628 /* SSO shaders can have VUE slots allocated which are never actually
629 * written to, so ignore them when looking for the last (written) slot.
630 */
631 int last_slot = vue_map->num_slots - 1;
632 while (last_slot > 0 &&
633 (vue_map->slot_to_varying[last_slot] == BRW_VARYING_SLOT_PAD ||
634 outputs[vue_map->slot_to_varying[last_slot]].file == BAD_FILE)) {
635 last_slot--;
636 }
637
638 bool urb_written = false;
639 for (slot = 0; slot < vue_map->num_slots; slot++) {
640 int varying = vue_map->slot_to_varying[slot];
641 switch (varying) {
642 case VARYING_SLOT_PSIZ: {
643 /* The point size varying slot is the vue header and is always in the
644 * vue map. But often none of the special varyings that live there
645 * are written and in that case we can skip writing to the vue
646 * header, provided the corresponding state properly clamps the
647 * values further down the pipeline. */
648 if ((vue_map->slots_valid & psiz_mask) == 0) {
649 assert(length == 0);
650 urb_offset++;
651 break;
652 }
653
654 fs_reg zero(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
655 bld.MOV(zero, brw_imm_ud(0u));
656
657 sources[length++] = zero;
658 if (vue_map->slots_valid & VARYING_BIT_LAYER)
659 sources[length++] = this->outputs[VARYING_SLOT_LAYER];
660 else
661 sources[length++] = zero;
662
663 if (vue_map->slots_valid & VARYING_BIT_VIEWPORT)
664 sources[length++] = this->outputs[VARYING_SLOT_VIEWPORT];
665 else
666 sources[length++] = zero;
667
668 if (vue_map->slots_valid & VARYING_BIT_PSIZ)
669 sources[length++] = this->outputs[VARYING_SLOT_PSIZ];
670 else
671 sources[length++] = zero;
672 break;
673 }
674 case BRW_VARYING_SLOT_NDC:
675 case VARYING_SLOT_EDGE:
676 unreachable("unexpected scalar vs output");
677 break;
678
679 default:
680 /* gl_Position is always in the vue map, but isn't always written by
681 * the shader. Other varyings (clip distances) get added to the vue
682 * map but don't always get written. In those cases, the
683 * corresponding this->output[] slot will be invalid we and can skip
684 * the urb write for the varying. If we've already queued up a vue
685 * slot for writing we flush a mlen 5 urb write, otherwise we just
686 * advance the urb_offset.
687 */
688 if (varying == BRW_VARYING_SLOT_PAD ||
689 this->outputs[varying].file == BAD_FILE) {
690 if (length > 0)
691 flush = true;
692 else
693 urb_offset++;
694 break;
695 }
696
697 if (stage == MESA_SHADER_VERTEX && vs_key->clamp_vertex_color &&
698 (varying == VARYING_SLOT_COL0 ||
699 varying == VARYING_SLOT_COL1 ||
700 varying == VARYING_SLOT_BFC0 ||
701 varying == VARYING_SLOT_BFC1)) {
702 /* We need to clamp these guys, so do a saturating MOV into a
703 * temp register and use that for the payload.
704 */
705 for (int i = 0; i < 4; i++) {
706 fs_reg reg = fs_reg(VGRF, alloc.allocate(1), outputs[varying].type);
707 fs_reg src = offset(this->outputs[varying], bld, i);
708 set_saturate(true, bld.MOV(reg, src));
709 sources[length++] = reg;
710 }
711 } else {
712 for (unsigned i = 0; i < 4; i++)
713 sources[length++] = offset(this->outputs[varying], bld, i);
714 }
715 break;
716 }
717
718 const fs_builder abld = bld.annotate("URB write");
719
720 /* If we've queued up 8 registers of payload (2 VUE slots), if this is
721 * the last slot or if we need to flush (see BAD_FILE varying case
722 * above), emit a URB write send now to flush out the data.
723 */
724 if (length == 8 || (length > 0 && slot == last_slot))
725 flush = true;
726 if (flush) {
727 fs_reg *payload_sources =
728 ralloc_array(mem_ctx, fs_reg, length + header_size);
729 fs_reg payload = fs_reg(VGRF, alloc.allocate(length + header_size),
730 BRW_REGISTER_TYPE_F);
731 payload_sources[0] = urb_handle;
732
733 if (opcode == SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT)
734 payload_sources[1] = per_slot_offsets;
735
736 memcpy(&payload_sources[header_size], sources,
737 length * sizeof sources[0]);
738
739 abld.LOAD_PAYLOAD(payload, payload_sources, length + header_size,
740 header_size);
741
742 fs_inst *inst = abld.emit(opcode, reg_undef, payload);
743
744 /* For ICL WA 1805992985 one needs additional write in the end. */
745 if (devinfo->gen == 11 && stage == MESA_SHADER_TESS_EVAL)
746 inst->eot = false;
747 else
748 inst->eot = slot == last_slot && stage != MESA_SHADER_GEOMETRY;
749
750 inst->mlen = length + header_size;
751 inst->offset = urb_offset;
752 urb_offset = starting_urb_offset + slot + 1;
753 length = 0;
754 flush = false;
755 urb_written = true;
756 }
757 }
758
759 /* If we don't have any valid slots to write, just do a minimal urb write
760 * send to terminate the shader. This includes 1 slot of undefined data,
761 * because it's invalid to write 0 data:
762 *
763 * From the Broadwell PRM, Volume 7: 3D Media GPGPU, Shared Functions -
764 * Unified Return Buffer (URB) > URB_SIMD8_Write and URB_SIMD8_Read >
765 * Write Data Payload:
766 *
767 * "The write data payload can be between 1 and 8 message phases long."
768 */
769 if (!urb_written) {
770 /* For GS, just turn EmitVertex() into a no-op. We don't want it to
771 * end the thread, and emit_gs_thread_end() already emits a SEND with
772 * EOT at the end of the program for us.
773 */
774 if (stage == MESA_SHADER_GEOMETRY)
775 return;
776
777 fs_reg payload = fs_reg(VGRF, alloc.allocate(2), BRW_REGISTER_TYPE_UD);
778 bld.exec_all().MOV(payload, urb_handle);
779
780 fs_inst *inst = bld.emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, payload);
781 inst->eot = true;
782 inst->mlen = 2;
783 inst->offset = 1;
784 return;
785 }
786
787 /* ICL WA 1805992985:
788 *
789 * ICLLP GPU hangs on one of tessellation vkcts tests with DS not done. The
790 * send cycle, which is a urb write with an eot must be 4 phases long and
791 * all 8 lanes must valid.
792 */
793 if (devinfo->gen == 11 && stage == MESA_SHADER_TESS_EVAL) {
794 fs_reg payload = fs_reg(VGRF, alloc.allocate(6), BRW_REGISTER_TYPE_UD);
795
796 /* Workaround requires all 8 channels (lanes) to be valid. This is
797 * understood to mean they all need to be alive. First trick is to find
798 * a live channel and copy its urb handle for all the other channels to
799 * make sure all handles are valid.
800 */
801 bld.exec_all().MOV(payload, bld.emit_uniformize(urb_handle));
802
803 /* Second trick is to use masked URB write where one can tell the HW to
804 * actually write data only for selected channels even though all are
805 * active.
806 * Third trick is to take advantage of the must-be-zero (MBZ) area in
807 * the very beginning of the URB.
808 *
809 * One masks data to be written only for the first channel and uses
810 * offset zero explicitly to land data to the MBZ area avoiding trashing
811 * any other part of the URB.
812 *
813 * Since the WA says that the write needs to be 4 phases long one uses
814 * 4 slots data. All are explicitly zeros in order to to keep the MBZ
815 * area written as zeros.
816 */
817 bld.exec_all().MOV(offset(payload, bld, 1), brw_imm_ud(0x10000u));
818 bld.exec_all().MOV(offset(payload, bld, 2), brw_imm_ud(0u));
819 bld.exec_all().MOV(offset(payload, bld, 3), brw_imm_ud(0u));
820 bld.exec_all().MOV(offset(payload, bld, 4), brw_imm_ud(0u));
821 bld.exec_all().MOV(offset(payload, bld, 5), brw_imm_ud(0u));
822
823 fs_inst *inst = bld.exec_all().emit(SHADER_OPCODE_URB_WRITE_SIMD8_MASKED,
824 reg_undef, payload);
825 inst->eot = true;
826 inst->mlen = 6;
827 inst->offset = 0;
828 }
829 }
830
831 void
832 fs_visitor::emit_cs_terminate()
833 {
834 assert(devinfo->gen >= 7);
835
836 /* We are getting the thread ID from the compute shader header */
837 assert(stage == MESA_SHADER_COMPUTE);
838
839 /* We can't directly send from g0, since sends with EOT have to use
840 * g112-127. So, copy it to a virtual register, The register allocator will
841 * make sure it uses the appropriate register range.
842 */
843 struct brw_reg g0 = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD);
844 fs_reg payload = fs_reg(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
845 bld.group(8, 0).exec_all().MOV(payload, g0);
846
847 /* Send a message to the thread spawner to terminate the thread. */
848 fs_inst *inst = bld.exec_all()
849 .emit(CS_OPCODE_CS_TERMINATE, reg_undef, payload);
850 inst->eot = true;
851 }
852
853 void
854 fs_visitor::emit_barrier()
855 {
856 uint32_t barrier_id_mask;
857 switch (devinfo->gen) {
858 case 7:
859 case 8:
860 barrier_id_mask = 0x0f000000u; break;
861 case 9:
862 case 10:
863 barrier_id_mask = 0x8f000000u; break;
864 case 11:
865 barrier_id_mask = 0x7f000000u; break;
866 default:
867 unreachable("barrier is only available on gen >= 7");
868 }
869
870 /* We are getting the barrier ID from the compute shader header */
871 assert(stage == MESA_SHADER_COMPUTE);
872
873 fs_reg payload = fs_reg(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
874
875 /* Clear the message payload */
876 bld.exec_all().group(8, 0).MOV(payload, brw_imm_ud(0u));
877
878 /* Copy the barrier id from r0.2 to the message payload reg.2 */
879 fs_reg r0_2 = fs_reg(retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD));
880 bld.exec_all().group(1, 0).AND(component(payload, 2), r0_2,
881 brw_imm_ud(barrier_id_mask));
882
883 /* Emit a gateway "barrier" message using the payload we set up, followed
884 * by a wait instruction.
885 */
886 bld.exec_all().emit(SHADER_OPCODE_BARRIER, reg_undef, payload);
887 }
888
889 fs_visitor::fs_visitor(const struct brw_compiler *compiler, void *log_data,
890 void *mem_ctx,
891 const brw_base_prog_key *key,
892 struct brw_stage_prog_data *prog_data,
893 const nir_shader *shader,
894 unsigned dispatch_width,
895 int shader_time_index,
896 const struct brw_vue_map *input_vue_map)
897 : backend_shader(compiler, log_data, mem_ctx, shader, prog_data),
898 key(key), gs_compile(NULL), prog_data(prog_data),
899 input_vue_map(input_vue_map),
900 dispatch_width(dispatch_width),
901 shader_time_index(shader_time_index),
902 bld(fs_builder(this, dispatch_width).at_end())
903 {
904 init();
905 }
906
907 fs_visitor::fs_visitor(const struct brw_compiler *compiler, void *log_data,
908 void *mem_ctx,
909 struct brw_gs_compile *c,
910 struct brw_gs_prog_data *prog_data,
911 const nir_shader *shader,
912 int shader_time_index)
913 : backend_shader(compiler, log_data, mem_ctx, shader,
914 &prog_data->base.base),
915 key(&c->key.base), gs_compile(c),
916 prog_data(&prog_data->base.base),
917 dispatch_width(8),
918 shader_time_index(shader_time_index),
919 bld(fs_builder(this, dispatch_width).at_end())
920 {
921 init();
922 }
923
924
925 void
926 fs_visitor::init()
927 {
928 this->key_tex = &key->tex;
929
930 this->max_dispatch_width = 32;
931 this->prog_data = this->stage_prog_data;
932
933 this->failed = false;
934
935 this->nir_locals = NULL;
936 this->nir_ssa_values = NULL;
937
938 memset(&this->payload, 0, sizeof(this->payload));
939 this->source_depth_to_render_target = false;
940 this->runtime_check_aads_emit = false;
941 this->first_non_payload_grf = 0;
942 this->max_grf = devinfo->gen >= 7 ? GEN7_MRF_HACK_START : BRW_MAX_GRF;
943
944 this->virtual_grf_start = NULL;
945 this->virtual_grf_end = NULL;
946 this->live_intervals = NULL;
947 this->regs_live_at_ip = NULL;
948
949 this->uniforms = 0;
950 this->last_scratch = 0;
951 this->pull_constant_loc = NULL;
952 this->push_constant_loc = NULL;
953
954 this->shader_stats.scheduler_mode = NULL;
955 this->shader_stats.promoted_constants = 0,
956
957 this->grf_used = 0;
958 this->spilled_any_registers = false;
959 }
960
961 fs_visitor::~fs_visitor()
962 {
963 }