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