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