Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_nir.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 #include "compiler/glsl/ir.h"
25 #include "main/shaderimage.h"
26 #include "brw_fs.h"
27 #include "brw_fs_surface_builder.h"
28 #include "brw_nir.h"
29 #include "brw_program.h"
30
31 using namespace brw;
32 using namespace brw::surface_access;
33
34 void
35 fs_visitor::emit_nir_code()
36 {
37 /* emit the arrays used for inputs and outputs - load/store intrinsics will
38 * be converted to reads/writes of these arrays
39 */
40 nir_setup_inputs();
41 nir_setup_outputs();
42 nir_setup_uniforms();
43 nir_emit_system_values();
44
45 /* get the main function and emit it */
46 nir_foreach_function(nir, function) {
47 assert(strcmp(function->name, "main") == 0);
48 assert(function->impl);
49 nir_emit_impl(function->impl);
50 }
51 }
52
53 void
54 fs_visitor::nir_setup_inputs()
55 {
56 if (stage != MESA_SHADER_FRAGMENT)
57 return;
58
59 nir_inputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_inputs);
60
61 nir_foreach_variable(var, &nir->inputs) {
62 fs_reg input = offset(nir_inputs, bld, var->data.driver_location);
63
64 fs_reg reg;
65 if (var->data.location == VARYING_SLOT_POS) {
66 reg = *emit_fragcoord_interpolation(var->data.pixel_center_integer,
67 var->data.origin_upper_left);
68 emit_percomp(bld, fs_inst(BRW_OPCODE_MOV, bld.dispatch_width(),
69 input, reg), 0xF);
70 } else if (var->data.location == VARYING_SLOT_LAYER) {
71 struct brw_reg reg = suboffset(interp_reg(VARYING_SLOT_LAYER, 1), 3);
72 reg.type = BRW_REGISTER_TYPE_D;
73 bld.emit(FS_OPCODE_CINTERP, retype(input, BRW_REGISTER_TYPE_D), reg);
74 } else if (var->data.location == VARYING_SLOT_VIEWPORT) {
75 struct brw_reg reg = suboffset(interp_reg(VARYING_SLOT_VIEWPORT, 2), 3);
76 reg.type = BRW_REGISTER_TYPE_D;
77 bld.emit(FS_OPCODE_CINTERP, retype(input, BRW_REGISTER_TYPE_D), reg);
78 } else {
79 int location = var->data.location;
80 emit_general_interpolation(&input, var->name, var->type,
81 (glsl_interp_qualifier) var->data.interpolation,
82 &location, var->data.centroid,
83 var->data.sample);
84 }
85 }
86 }
87
88 void
89 fs_visitor::nir_setup_single_output_varying(fs_reg *reg,
90 const glsl_type *type,
91 unsigned *location)
92 {
93 if (type->is_array() || type->is_matrix()) {
94 const struct glsl_type *elem_type = glsl_get_array_element(type);
95 const unsigned length = glsl_get_length(type);
96
97 for (unsigned i = 0; i < length; i++) {
98 nir_setup_single_output_varying(reg, elem_type, location);
99 }
100 } else if (type->is_record()) {
101 for (unsigned i = 0; i < type->length; i++) {
102 const struct glsl_type *field_type = type->fields.structure[i].type;
103 nir_setup_single_output_varying(reg, field_type, location);
104 }
105 } else {
106 assert(type->is_scalar() || type->is_vector());
107 this->outputs[*location] = *reg;
108 this->output_components[*location] = type->vector_elements;
109 *reg = offset(*reg, bld, 4);
110 (*location)++;
111 }
112 }
113
114 void
115 fs_visitor::nir_setup_outputs()
116 {
117 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
118
119 nir_outputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_outputs);
120
121 nir_foreach_variable(var, &nir->outputs) {
122 fs_reg reg = offset(nir_outputs, bld, var->data.driver_location);
123
124 switch (stage) {
125 case MESA_SHADER_VERTEX:
126 case MESA_SHADER_TESS_EVAL:
127 case MESA_SHADER_GEOMETRY: {
128 unsigned location = var->data.location;
129 nir_setup_single_output_varying(&reg, var->type, &location);
130 break;
131 }
132 case MESA_SHADER_FRAGMENT:
133 if (key->force_dual_color_blend &&
134 var->data.location == FRAG_RESULT_DATA1) {
135 this->dual_src_output = reg;
136 this->do_dual_src = true;
137 } else if (var->data.index > 0) {
138 assert(var->data.location == FRAG_RESULT_DATA0);
139 assert(var->data.index == 1);
140 this->dual_src_output = reg;
141 this->do_dual_src = true;
142 } else if (var->data.location == FRAG_RESULT_COLOR) {
143 /* Writing gl_FragColor outputs to all color regions. */
144 for (unsigned int i = 0; i < MAX2(key->nr_color_regions, 1); i++) {
145 this->outputs[i] = reg;
146 this->output_components[i] = 4;
147 }
148 } else if (var->data.location == FRAG_RESULT_DEPTH) {
149 this->frag_depth = reg;
150 } else if (var->data.location == FRAG_RESULT_STENCIL) {
151 this->frag_stencil = reg;
152 } else if (var->data.location == FRAG_RESULT_SAMPLE_MASK) {
153 this->sample_mask = reg;
154 } else {
155 int vector_elements = var->type->without_array()->vector_elements;
156
157 /* gl_FragData or a user-defined FS output */
158 assert(var->data.location >= FRAG_RESULT_DATA0 &&
159 var->data.location < FRAG_RESULT_DATA0+BRW_MAX_DRAW_BUFFERS);
160
161 /* General color output. */
162 for (unsigned int i = 0; i < MAX2(1, var->type->length); i++) {
163 int output = var->data.location - FRAG_RESULT_DATA0 + i;
164 this->outputs[output] = offset(reg, bld, vector_elements * i);
165 this->output_components[output] = vector_elements;
166 }
167 }
168 break;
169 default:
170 unreachable("unhandled shader stage");
171 }
172 }
173 }
174
175 void
176 fs_visitor::nir_setup_uniforms()
177 {
178 if (dispatch_width != 8)
179 return;
180
181 uniforms = nir->num_uniforms / 4;
182 }
183
184 static bool
185 emit_system_values_block(nir_block *block, void *void_visitor)
186 {
187 fs_visitor *v = (fs_visitor *)void_visitor;
188 fs_reg *reg;
189
190 nir_foreach_instr(block, instr) {
191 if (instr->type != nir_instr_type_intrinsic)
192 continue;
193
194 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
195 switch (intrin->intrinsic) {
196 case nir_intrinsic_load_vertex_id:
197 unreachable("should be lowered by lower_vertex_id().");
198
199 case nir_intrinsic_load_vertex_id_zero_base:
200 assert(v->stage == MESA_SHADER_VERTEX);
201 reg = &v->nir_system_values[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE];
202 if (reg->file == BAD_FILE)
203 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE);
204 break;
205
206 case nir_intrinsic_load_base_vertex:
207 assert(v->stage == MESA_SHADER_VERTEX);
208 reg = &v->nir_system_values[SYSTEM_VALUE_BASE_VERTEX];
209 if (reg->file == BAD_FILE)
210 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_BASE_VERTEX);
211 break;
212
213 case nir_intrinsic_load_instance_id:
214 assert(v->stage == MESA_SHADER_VERTEX);
215 reg = &v->nir_system_values[SYSTEM_VALUE_INSTANCE_ID];
216 if (reg->file == BAD_FILE)
217 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_INSTANCE_ID);
218 break;
219
220 case nir_intrinsic_load_base_instance:
221 assert(v->stage == MESA_SHADER_VERTEX);
222 reg = &v->nir_system_values[SYSTEM_VALUE_BASE_INSTANCE];
223 if (reg->file == BAD_FILE)
224 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_BASE_INSTANCE);
225 break;
226
227 case nir_intrinsic_load_draw_id:
228 assert(v->stage == MESA_SHADER_VERTEX);
229 reg = &v->nir_system_values[SYSTEM_VALUE_DRAW_ID];
230 if (reg->file == BAD_FILE)
231 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_DRAW_ID);
232 break;
233
234 case nir_intrinsic_load_invocation_id:
235 assert(v->stage == MESA_SHADER_GEOMETRY);
236 reg = &v->nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
237 if (reg->file == BAD_FILE) {
238 const fs_builder abld = v->bld.annotate("gl_InvocationID", NULL);
239 fs_reg g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
240 fs_reg iid = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
241 abld.SHR(iid, g1, brw_imm_ud(27u));
242 *reg = iid;
243 }
244 break;
245
246 case nir_intrinsic_load_sample_pos:
247 assert(v->stage == MESA_SHADER_FRAGMENT);
248 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
249 if (reg->file == BAD_FILE)
250 *reg = *v->emit_samplepos_setup();
251 break;
252
253 case nir_intrinsic_load_sample_id:
254 assert(v->stage == MESA_SHADER_FRAGMENT);
255 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
256 if (reg->file == BAD_FILE)
257 *reg = *v->emit_sampleid_setup();
258 break;
259
260 case nir_intrinsic_load_sample_mask_in:
261 assert(v->stage == MESA_SHADER_FRAGMENT);
262 assert(v->devinfo->gen >= 7);
263 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_MASK_IN];
264 if (reg->file == BAD_FILE)
265 *reg = fs_reg(retype(brw_vec8_grf(v->payload.sample_mask_in_reg, 0),
266 BRW_REGISTER_TYPE_D));
267 break;
268
269 case nir_intrinsic_load_local_invocation_id:
270 assert(v->stage == MESA_SHADER_COMPUTE);
271 reg = &v->nir_system_values[SYSTEM_VALUE_LOCAL_INVOCATION_ID];
272 if (reg->file == BAD_FILE)
273 *reg = *v->emit_cs_local_invocation_id_setup();
274 break;
275
276 case nir_intrinsic_load_work_group_id:
277 assert(v->stage == MESA_SHADER_COMPUTE);
278 reg = &v->nir_system_values[SYSTEM_VALUE_WORK_GROUP_ID];
279 if (reg->file == BAD_FILE)
280 *reg = *v->emit_cs_work_group_id_setup();
281 break;
282
283 case nir_intrinsic_load_helper_invocation:
284 assert(v->stage == MESA_SHADER_FRAGMENT);
285 reg = &v->nir_system_values[SYSTEM_VALUE_HELPER_INVOCATION];
286 if (reg->file == BAD_FILE) {
287 const fs_builder abld =
288 v->bld.annotate("gl_HelperInvocation", NULL);
289
290 /* On Gen6+ (gl_HelperInvocation is only exposed on Gen7+) the
291 * pixel mask is in g1.7 of the thread payload.
292 *
293 * We move the per-channel pixel enable bit to the low bit of each
294 * channel by shifting the byte containing the pixel mask by the
295 * vector immediate 0x76543210UV.
296 *
297 * The region of <1,8,0> reads only 1 byte (the pixel masks for
298 * subspans 0 and 1) in SIMD8 and an additional byte (the pixel
299 * masks for 2 and 3) in SIMD16.
300 */
301 fs_reg shifted = abld.vgrf(BRW_REGISTER_TYPE_UW, 1);
302 abld.SHR(shifted,
303 stride(byte_offset(retype(brw_vec1_grf(1, 0),
304 BRW_REGISTER_TYPE_UB), 28),
305 1, 8, 0),
306 brw_imm_uv(0x76543210));
307
308 /* A set bit in the pixel mask means the channel is enabled, but
309 * that is the opposite of gl_HelperInvocation so we need to invert
310 * the mask.
311 *
312 * The negate source-modifier bit of logical instructions on Gen8+
313 * performs 1's complement negation, so we can use that instead of
314 * a NOT instruction.
315 */
316 fs_reg inverted = negate(shifted);
317 if (v->devinfo->gen < 8) {
318 inverted = abld.vgrf(BRW_REGISTER_TYPE_UW);
319 abld.NOT(inverted, shifted);
320 }
321
322 /* We then resolve the 0/1 result to 0/~0 boolean values by ANDing
323 * with 1 and negating.
324 */
325 fs_reg anded = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
326 abld.AND(anded, inverted, brw_imm_uw(1));
327
328 fs_reg dst = abld.vgrf(BRW_REGISTER_TYPE_D, 1);
329 abld.MOV(dst, negate(retype(anded, BRW_REGISTER_TYPE_D)));
330 *reg = dst;
331 }
332 break;
333
334 default:
335 break;
336 }
337 }
338
339 return true;
340 }
341
342 void
343 fs_visitor::nir_emit_system_values()
344 {
345 nir_system_values = ralloc_array(mem_ctx, fs_reg, SYSTEM_VALUE_MAX);
346 for (unsigned i = 0; i < SYSTEM_VALUE_MAX; i++) {
347 nir_system_values[i] = fs_reg();
348 }
349
350 nir_foreach_function(nir, function) {
351 assert(strcmp(function->name, "main") == 0);
352 assert(function->impl);
353 nir_foreach_block(function->impl, emit_system_values_block, this);
354 }
355 }
356
357 void
358 fs_visitor::nir_emit_impl(nir_function_impl *impl)
359 {
360 nir_locals = ralloc_array(mem_ctx, fs_reg, impl->reg_alloc);
361 for (unsigned i = 0; i < impl->reg_alloc; i++) {
362 nir_locals[i] = fs_reg();
363 }
364
365 foreach_list_typed(nir_register, reg, node, &impl->registers) {
366 unsigned array_elems =
367 reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
368 unsigned size = array_elems * reg->num_components;
369 nir_locals[reg->index] = bld.vgrf(BRW_REGISTER_TYPE_F, size);
370 }
371
372 nir_ssa_values = reralloc(mem_ctx, nir_ssa_values, fs_reg,
373 impl->ssa_alloc);
374
375 nir_emit_cf_list(&impl->body);
376 }
377
378 void
379 fs_visitor::nir_emit_cf_list(exec_list *list)
380 {
381 exec_list_validate(list);
382 foreach_list_typed(nir_cf_node, node, node, list) {
383 switch (node->type) {
384 case nir_cf_node_if:
385 nir_emit_if(nir_cf_node_as_if(node));
386 break;
387
388 case nir_cf_node_loop:
389 nir_emit_loop(nir_cf_node_as_loop(node));
390 break;
391
392 case nir_cf_node_block:
393 nir_emit_block(nir_cf_node_as_block(node));
394 break;
395
396 default:
397 unreachable("Invalid CFG node block");
398 }
399 }
400 }
401
402 void
403 fs_visitor::nir_emit_if(nir_if *if_stmt)
404 {
405 /* first, put the condition into f0 */
406 fs_inst *inst = bld.MOV(bld.null_reg_d(),
407 retype(get_nir_src(if_stmt->condition),
408 BRW_REGISTER_TYPE_D));
409 inst->conditional_mod = BRW_CONDITIONAL_NZ;
410
411 bld.IF(BRW_PREDICATE_NORMAL);
412
413 nir_emit_cf_list(&if_stmt->then_list);
414
415 /* note: if the else is empty, dead CF elimination will remove it */
416 bld.emit(BRW_OPCODE_ELSE);
417
418 nir_emit_cf_list(&if_stmt->else_list);
419
420 bld.emit(BRW_OPCODE_ENDIF);
421 }
422
423 void
424 fs_visitor::nir_emit_loop(nir_loop *loop)
425 {
426 bld.emit(BRW_OPCODE_DO);
427
428 nir_emit_cf_list(&loop->body);
429
430 bld.emit(BRW_OPCODE_WHILE);
431 }
432
433 void
434 fs_visitor::nir_emit_block(nir_block *block)
435 {
436 nir_foreach_instr(block, instr) {
437 nir_emit_instr(instr);
438 }
439 }
440
441 void
442 fs_visitor::nir_emit_instr(nir_instr *instr)
443 {
444 const fs_builder abld = bld.annotate(NULL, instr);
445
446 switch (instr->type) {
447 case nir_instr_type_alu:
448 nir_emit_alu(abld, nir_instr_as_alu(instr));
449 break;
450
451 case nir_instr_type_intrinsic:
452 switch (stage) {
453 case MESA_SHADER_VERTEX:
454 nir_emit_vs_intrinsic(abld, nir_instr_as_intrinsic(instr));
455 break;
456 case MESA_SHADER_TESS_EVAL:
457 nir_emit_tes_intrinsic(abld, nir_instr_as_intrinsic(instr));
458 break;
459 case MESA_SHADER_GEOMETRY:
460 nir_emit_gs_intrinsic(abld, nir_instr_as_intrinsic(instr));
461 break;
462 case MESA_SHADER_FRAGMENT:
463 nir_emit_fs_intrinsic(abld, nir_instr_as_intrinsic(instr));
464 break;
465 case MESA_SHADER_COMPUTE:
466 nir_emit_cs_intrinsic(abld, nir_instr_as_intrinsic(instr));
467 break;
468 default:
469 unreachable("unsupported shader stage");
470 }
471 break;
472
473 case nir_instr_type_tex:
474 nir_emit_texture(abld, nir_instr_as_tex(instr));
475 break;
476
477 case nir_instr_type_load_const:
478 nir_emit_load_const(abld, nir_instr_as_load_const(instr));
479 break;
480
481 case nir_instr_type_ssa_undef:
482 nir_emit_undef(abld, nir_instr_as_ssa_undef(instr));
483 break;
484
485 case nir_instr_type_jump:
486 nir_emit_jump(abld, nir_instr_as_jump(instr));
487 break;
488
489 default:
490 unreachable("unknown instruction type");
491 }
492 }
493
494 bool
495 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr *instr,
496 const fs_reg &result)
497 {
498 if (!instr->src[0].src.is_ssa ||
499 instr->src[0].src.ssa->parent_instr->type != nir_instr_type_intrinsic)
500 return false;
501
502 nir_intrinsic_instr *src0 =
503 nir_instr_as_intrinsic(instr->src[0].src.ssa->parent_instr);
504
505 if (src0->intrinsic != nir_intrinsic_load_front_face)
506 return false;
507
508 nir_const_value *value1 = nir_src_as_const_value(instr->src[1].src);
509 if (!value1 || fabsf(value1->f[0]) != 1.0f)
510 return false;
511
512 nir_const_value *value2 = nir_src_as_const_value(instr->src[2].src);
513 if (!value2 || fabsf(value2->f[0]) != 1.0f)
514 return false;
515
516 fs_reg tmp = vgrf(glsl_type::int_type);
517
518 if (devinfo->gen >= 6) {
519 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
520 fs_reg g0 = fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W));
521
522 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
523 *
524 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
525 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
526 *
527 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
528 *
529 * This negation looks like it's safe in practice, because bits 0:4 will
530 * surely be TRIANGLES
531 */
532
533 if (value1->f[0] == -1.0f) {
534 g0.negate = true;
535 }
536
537 tmp.type = BRW_REGISTER_TYPE_W;
538 tmp.subreg_offset = 2;
539 tmp.stride = 2;
540
541 bld.OR(tmp, g0, brw_imm_uw(0x3f80));
542
543 tmp.type = BRW_REGISTER_TYPE_D;
544 tmp.subreg_offset = 0;
545 tmp.stride = 1;
546 } else {
547 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
548 fs_reg g1_6 = fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D));
549
550 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
551 *
552 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
553 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
554 *
555 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
556 *
557 * This negation looks like it's safe in practice, because bits 0:4 will
558 * surely be TRIANGLES
559 */
560
561 if (value1->f[0] == -1.0f) {
562 g1_6.negate = true;
563 }
564
565 bld.OR(tmp, g1_6, brw_imm_d(0x3f800000));
566 }
567 bld.AND(retype(result, BRW_REGISTER_TYPE_D), tmp, brw_imm_d(0xbf800000));
568
569 return true;
570 }
571
572 void
573 fs_visitor::nir_emit_alu(const fs_builder &bld, nir_alu_instr *instr)
574 {
575 struct brw_wm_prog_key *fs_key = (struct brw_wm_prog_key *) this->key;
576 fs_inst *inst;
577
578 fs_reg result = get_nir_dest(instr->dest.dest);
579 result.type = brw_type_for_nir_type(nir_op_infos[instr->op].output_type);
580
581 fs_reg op[4];
582 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
583 op[i] = get_nir_src(instr->src[i].src);
584 op[i].type = brw_type_for_nir_type(nir_op_infos[instr->op].input_types[i]);
585 op[i].abs = instr->src[i].abs;
586 op[i].negate = instr->src[i].negate;
587 }
588
589 /* We get a bunch of mov's out of the from_ssa pass and they may still
590 * be vectorized. We'll handle them as a special-case. We'll also
591 * handle vecN here because it's basically the same thing.
592 */
593 switch (instr->op) {
594 case nir_op_imov:
595 case nir_op_fmov:
596 case nir_op_vec2:
597 case nir_op_vec3:
598 case nir_op_vec4: {
599 fs_reg temp = result;
600 bool need_extra_copy = false;
601 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
602 if (!instr->src[i].src.is_ssa &&
603 instr->dest.dest.reg.reg == instr->src[i].src.reg.reg) {
604 need_extra_copy = true;
605 temp = bld.vgrf(result.type, 4);
606 break;
607 }
608 }
609
610 for (unsigned i = 0; i < 4; i++) {
611 if (!(instr->dest.write_mask & (1 << i)))
612 continue;
613
614 if (instr->op == nir_op_imov || instr->op == nir_op_fmov) {
615 inst = bld.MOV(offset(temp, bld, i),
616 offset(op[0], bld, instr->src[0].swizzle[i]));
617 } else {
618 inst = bld.MOV(offset(temp, bld, i),
619 offset(op[i], bld, instr->src[i].swizzle[0]));
620 }
621 inst->saturate = instr->dest.saturate;
622 }
623
624 /* In this case the source and destination registers were the same,
625 * so we need to insert an extra set of moves in order to deal with
626 * any swizzling.
627 */
628 if (need_extra_copy) {
629 for (unsigned i = 0; i < 4; i++) {
630 if (!(instr->dest.write_mask & (1 << i)))
631 continue;
632
633 bld.MOV(offset(result, bld, i), offset(temp, bld, i));
634 }
635 }
636 return;
637 }
638 default:
639 break;
640 }
641
642 /* At this point, we have dealt with any instruction that operates on
643 * more than a single channel. Therefore, we can just adjust the source
644 * and destination registers for that channel and emit the instruction.
645 */
646 unsigned channel = 0;
647 if (nir_op_infos[instr->op].output_size == 0) {
648 /* Since NIR is doing the scalarizing for us, we should only ever see
649 * vectorized operations with a single channel.
650 */
651 assert(_mesa_bitcount(instr->dest.write_mask) == 1);
652 channel = ffs(instr->dest.write_mask) - 1;
653
654 result = offset(result, bld, channel);
655 }
656
657 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
658 assert(nir_op_infos[instr->op].input_sizes[i] < 2);
659 op[i] = offset(op[i], bld, instr->src[i].swizzle[channel]);
660 }
661
662 switch (instr->op) {
663 case nir_op_i2f:
664 case nir_op_u2f:
665 inst = bld.MOV(result, op[0]);
666 inst->saturate = instr->dest.saturate;
667 break;
668
669 case nir_op_f2i:
670 case nir_op_f2u:
671 bld.MOV(result, op[0]);
672 break;
673
674 case nir_op_fsign: {
675 /* AND(val, 0x80000000) gives the sign bit.
676 *
677 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
678 * zero.
679 */
680 bld.CMP(bld.null_reg_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ);
681
682 fs_reg result_int = retype(result, BRW_REGISTER_TYPE_UD);
683 op[0].type = BRW_REGISTER_TYPE_UD;
684 result.type = BRW_REGISTER_TYPE_UD;
685 bld.AND(result_int, op[0], brw_imm_ud(0x80000000u));
686
687 inst = bld.OR(result_int, result_int, brw_imm_ud(0x3f800000u));
688 inst->predicate = BRW_PREDICATE_NORMAL;
689 if (instr->dest.saturate) {
690 inst = bld.MOV(result, result);
691 inst->saturate = true;
692 }
693 break;
694 }
695
696 case nir_op_isign:
697 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
698 * -> non-negative val generates 0x00000000.
699 * Predicated OR sets 1 if val is positive.
700 */
701 bld.CMP(bld.null_reg_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_G);
702 bld.ASR(result, op[0], brw_imm_d(31));
703 inst = bld.OR(result, result, brw_imm_d(1));
704 inst->predicate = BRW_PREDICATE_NORMAL;
705 break;
706
707 case nir_op_frcp:
708 inst = bld.emit(SHADER_OPCODE_RCP, result, op[0]);
709 inst->saturate = instr->dest.saturate;
710 break;
711
712 case nir_op_fexp2:
713 inst = bld.emit(SHADER_OPCODE_EXP2, result, op[0]);
714 inst->saturate = instr->dest.saturate;
715 break;
716
717 case nir_op_flog2:
718 inst = bld.emit(SHADER_OPCODE_LOG2, result, op[0]);
719 inst->saturate = instr->dest.saturate;
720 break;
721
722 case nir_op_fsin: {
723 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F);
724 inst = bld.emit(SHADER_OPCODE_SIN, tmp, op[0]);
725 if (instr->dest.saturate) {
726 inst->dst = result;
727 inst->saturate = true;
728 } else {
729 bld.MUL(result, tmp, brw_imm_f(0.99997));
730 }
731 break;
732 }
733
734 case nir_op_fcos: {
735 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F);
736 inst = bld.emit(SHADER_OPCODE_COS, tmp, op[0]);
737 if (instr->dest.saturate) {
738 inst->dst = result;
739 inst->saturate = true;
740 } else {
741 bld.MUL(result, tmp, brw_imm_f(0.99997));
742 }
743 break;
744 }
745
746 case nir_op_fddx:
747 if (fs_key->high_quality_derivatives) {
748 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
749 } else {
750 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
751 }
752 inst->saturate = instr->dest.saturate;
753 break;
754 case nir_op_fddx_fine:
755 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
756 inst->saturate = instr->dest.saturate;
757 break;
758 case nir_op_fddx_coarse:
759 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
760 inst->saturate = instr->dest.saturate;
761 break;
762 case nir_op_fddy:
763 if (fs_key->high_quality_derivatives) {
764 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0],
765 brw_imm_d(fs_key->render_to_fbo));
766 } else {
767 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0],
768 brw_imm_d(fs_key->render_to_fbo));
769 }
770 inst->saturate = instr->dest.saturate;
771 break;
772 case nir_op_fddy_fine:
773 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0],
774 brw_imm_d(fs_key->render_to_fbo));
775 inst->saturate = instr->dest.saturate;
776 break;
777 case nir_op_fddy_coarse:
778 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0],
779 brw_imm_d(fs_key->render_to_fbo));
780 inst->saturate = instr->dest.saturate;
781 break;
782
783 case nir_op_fadd:
784 case nir_op_iadd:
785 inst = bld.ADD(result, op[0], op[1]);
786 inst->saturate = instr->dest.saturate;
787 break;
788
789 case nir_op_fmul:
790 inst = bld.MUL(result, op[0], op[1]);
791 inst->saturate = instr->dest.saturate;
792 break;
793
794 case nir_op_imul:
795 bld.MUL(result, op[0], op[1]);
796 break;
797
798 case nir_op_imul_high:
799 case nir_op_umul_high:
800 bld.emit(SHADER_OPCODE_MULH, result, op[0], op[1]);
801 break;
802
803 case nir_op_idiv:
804 case nir_op_udiv:
805 bld.emit(SHADER_OPCODE_INT_QUOTIENT, result, op[0], op[1]);
806 break;
807
808 case nir_op_uadd_carry:
809 unreachable("Should have been lowered by carry_to_arith().");
810
811 case nir_op_usub_borrow:
812 unreachable("Should have been lowered by borrow_to_arith().");
813
814 case nir_op_umod:
815 case nir_op_irem:
816 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
817 * appears that our hardware just does the right thing for signed
818 * remainder.
819 */
820 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
821 break;
822
823 case nir_op_imod: {
824 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
825 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
826
827 /* Math instructions don't support conditional mod */
828 inst = bld.MOV(bld.null_reg_d(), result);
829 inst->conditional_mod = BRW_CONDITIONAL_NZ;
830
831 /* Now, we need to determine if signs of the sources are different.
832 * When we XOR the sources, the top bit is 0 if they are the same and 1
833 * if they are different. We can then use a conditional modifier to
834 * turn that into a predicate. This leads us to an XOR.l instruction.
835 */
836 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_D);
837 inst = bld.XOR(tmp, op[0], op[1]);
838 inst->predicate = BRW_PREDICATE_NORMAL;
839 inst->conditional_mod = BRW_CONDITIONAL_L;
840
841 /* If the result of the initial remainder operation is non-zero and the
842 * two sources have different signs, add in a copy of op[1] to get the
843 * final integer modulus value.
844 */
845 inst = bld.ADD(result, result, op[1]);
846 inst->predicate = BRW_PREDICATE_NORMAL;
847 break;
848 }
849
850 case nir_op_flt:
851 case nir_op_ilt:
852 case nir_op_ult:
853 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_L);
854 break;
855
856 case nir_op_fge:
857 case nir_op_ige:
858 case nir_op_uge:
859 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_GE);
860 break;
861
862 case nir_op_feq:
863 case nir_op_ieq:
864 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_Z);
865 break;
866
867 case nir_op_fne:
868 case nir_op_ine:
869 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_NZ);
870 break;
871
872 case nir_op_inot:
873 if (devinfo->gen >= 8) {
874 op[0] = resolve_source_modifiers(op[0]);
875 }
876 bld.NOT(result, op[0]);
877 break;
878 case nir_op_ixor:
879 if (devinfo->gen >= 8) {
880 op[0] = resolve_source_modifiers(op[0]);
881 op[1] = resolve_source_modifiers(op[1]);
882 }
883 bld.XOR(result, op[0], op[1]);
884 break;
885 case nir_op_ior:
886 if (devinfo->gen >= 8) {
887 op[0] = resolve_source_modifiers(op[0]);
888 op[1] = resolve_source_modifiers(op[1]);
889 }
890 bld.OR(result, op[0], op[1]);
891 break;
892 case nir_op_iand:
893 if (devinfo->gen >= 8) {
894 op[0] = resolve_source_modifiers(op[0]);
895 op[1] = resolve_source_modifiers(op[1]);
896 }
897 bld.AND(result, op[0], op[1]);
898 break;
899
900 case nir_op_fdot2:
901 case nir_op_fdot3:
902 case nir_op_fdot4:
903 case nir_op_ball_fequal2:
904 case nir_op_ball_iequal2:
905 case nir_op_ball_fequal3:
906 case nir_op_ball_iequal3:
907 case nir_op_ball_fequal4:
908 case nir_op_ball_iequal4:
909 case nir_op_bany_fnequal2:
910 case nir_op_bany_inequal2:
911 case nir_op_bany_fnequal3:
912 case nir_op_bany_inequal3:
913 case nir_op_bany_fnequal4:
914 case nir_op_bany_inequal4:
915 unreachable("Lowered by nir_lower_alu_reductions");
916
917 case nir_op_fnoise1_1:
918 case nir_op_fnoise1_2:
919 case nir_op_fnoise1_3:
920 case nir_op_fnoise1_4:
921 case nir_op_fnoise2_1:
922 case nir_op_fnoise2_2:
923 case nir_op_fnoise2_3:
924 case nir_op_fnoise2_4:
925 case nir_op_fnoise3_1:
926 case nir_op_fnoise3_2:
927 case nir_op_fnoise3_3:
928 case nir_op_fnoise3_4:
929 case nir_op_fnoise4_1:
930 case nir_op_fnoise4_2:
931 case nir_op_fnoise4_3:
932 case nir_op_fnoise4_4:
933 unreachable("not reached: should be handled by lower_noise");
934
935 case nir_op_ldexp:
936 unreachable("not reached: should be handled by ldexp_to_arith()");
937
938 case nir_op_fsqrt:
939 inst = bld.emit(SHADER_OPCODE_SQRT, result, op[0]);
940 inst->saturate = instr->dest.saturate;
941 break;
942
943 case nir_op_frsq:
944 inst = bld.emit(SHADER_OPCODE_RSQ, result, op[0]);
945 inst->saturate = instr->dest.saturate;
946 break;
947
948 case nir_op_b2i:
949 case nir_op_b2f:
950 bld.MOV(result, negate(op[0]));
951 break;
952
953 case nir_op_f2b:
954 bld.CMP(result, op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ);
955 break;
956 case nir_op_i2b:
957 bld.CMP(result, op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ);
958 break;
959
960 case nir_op_ftrunc:
961 inst = bld.RNDZ(result, op[0]);
962 inst->saturate = instr->dest.saturate;
963 break;
964
965 case nir_op_fceil: {
966 op[0].negate = !op[0].negate;
967 fs_reg temp = vgrf(glsl_type::float_type);
968 bld.RNDD(temp, op[0]);
969 temp.negate = true;
970 inst = bld.MOV(result, temp);
971 inst->saturate = instr->dest.saturate;
972 break;
973 }
974 case nir_op_ffloor:
975 inst = bld.RNDD(result, op[0]);
976 inst->saturate = instr->dest.saturate;
977 break;
978 case nir_op_ffract:
979 inst = bld.FRC(result, op[0]);
980 inst->saturate = instr->dest.saturate;
981 break;
982 case nir_op_fround_even:
983 inst = bld.RNDE(result, op[0]);
984 inst->saturate = instr->dest.saturate;
985 break;
986
987 case nir_op_fquantize2f16: {
988 fs_reg tmp16 = bld.vgrf(BRW_REGISTER_TYPE_D);
989 fs_reg tmp32 = bld.vgrf(BRW_REGISTER_TYPE_F);
990 fs_reg zero = bld.vgrf(BRW_REGISTER_TYPE_F);
991
992 /* The destination stride must be at least as big as the source stride. */
993 tmp16.type = BRW_REGISTER_TYPE_W;
994 tmp16.stride = 2;
995
996 /* Check for denormal */
997 fs_reg abs_src0 = op[0];
998 abs_src0.abs = true;
999 bld.CMP(bld.null_reg_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1000 BRW_CONDITIONAL_L);
1001 /* Get the appropriately signed zero */
1002 bld.AND(retype(zero, BRW_REGISTER_TYPE_UD),
1003 retype(op[0], BRW_REGISTER_TYPE_UD),
1004 brw_imm_ud(0x80000000));
1005 /* Do the actual F32 -> F16 -> F32 conversion */
1006 bld.emit(BRW_OPCODE_F32TO16, tmp16, op[0]);
1007 bld.emit(BRW_OPCODE_F16TO32, tmp32, tmp16);
1008 /* Select that or zero based on normal status */
1009 inst = bld.SEL(result, zero, tmp32);
1010 inst->predicate = BRW_PREDICATE_NORMAL;
1011 inst->saturate = instr->dest.saturate;
1012 break;
1013 }
1014
1015 case nir_op_fmin:
1016 case nir_op_imin:
1017 case nir_op_umin:
1018 if (devinfo->gen >= 6) {
1019 inst = bld.emit(BRW_OPCODE_SEL, result, op[0], op[1]);
1020 inst->conditional_mod = BRW_CONDITIONAL_L;
1021 } else {
1022 bld.CMP(bld.null_reg_d(), op[0], op[1], BRW_CONDITIONAL_L);
1023 inst = bld.SEL(result, op[0], op[1]);
1024 inst->predicate = BRW_PREDICATE_NORMAL;
1025 }
1026 inst->saturate = instr->dest.saturate;
1027 break;
1028
1029 case nir_op_fmax:
1030 case nir_op_imax:
1031 case nir_op_umax:
1032 if (devinfo->gen >= 6) {
1033 inst = bld.emit(BRW_OPCODE_SEL, result, op[0], op[1]);
1034 inst->conditional_mod = BRW_CONDITIONAL_GE;
1035 } else {
1036 bld.CMP(bld.null_reg_d(), op[0], op[1], BRW_CONDITIONAL_GE);
1037 inst = bld.SEL(result, op[0], op[1]);
1038 inst->predicate = BRW_PREDICATE_NORMAL;
1039 }
1040 inst->saturate = instr->dest.saturate;
1041 break;
1042
1043 case nir_op_pack_snorm_2x16:
1044 case nir_op_pack_snorm_4x8:
1045 case nir_op_pack_unorm_2x16:
1046 case nir_op_pack_unorm_4x8:
1047 case nir_op_unpack_snorm_2x16:
1048 case nir_op_unpack_snorm_4x8:
1049 case nir_op_unpack_unorm_2x16:
1050 case nir_op_unpack_unorm_4x8:
1051 case nir_op_unpack_half_2x16:
1052 case nir_op_pack_half_2x16:
1053 unreachable("not reached: should be handled by lower_packing_builtins");
1054
1055 case nir_op_unpack_half_2x16_split_x:
1056 inst = bld.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X, result, op[0]);
1057 inst->saturate = instr->dest.saturate;
1058 break;
1059 case nir_op_unpack_half_2x16_split_y:
1060 inst = bld.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y, result, op[0]);
1061 inst->saturate = instr->dest.saturate;
1062 break;
1063
1064 case nir_op_fpow:
1065 inst = bld.emit(SHADER_OPCODE_POW, result, op[0], op[1]);
1066 inst->saturate = instr->dest.saturate;
1067 break;
1068
1069 case nir_op_bitfield_reverse:
1070 bld.BFREV(result, op[0]);
1071 break;
1072
1073 case nir_op_bit_count:
1074 bld.CBIT(result, op[0]);
1075 break;
1076
1077 case nir_op_ufind_msb:
1078 case nir_op_ifind_msb: {
1079 bld.FBH(retype(result, BRW_REGISTER_TYPE_UD), op[0]);
1080
1081 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1082 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1083 * subtract the result from 31 to convert the MSB count into an LSB count.
1084 */
1085 bld.CMP(bld.null_reg_d(), result, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
1086
1087 inst = bld.ADD(result, result, brw_imm_d(31));
1088 inst->predicate = BRW_PREDICATE_NORMAL;
1089 inst->src[0].negate = true;
1090 break;
1091 }
1092
1093 case nir_op_find_lsb:
1094 bld.FBL(result, op[0]);
1095 break;
1096
1097 case nir_op_ubitfield_extract:
1098 case nir_op_ibitfield_extract:
1099 unreachable("should have been lowered");
1100 case nir_op_ubfe:
1101 case nir_op_ibfe:
1102 bld.BFE(result, op[2], op[1], op[0]);
1103 break;
1104 case nir_op_bfm:
1105 bld.BFI1(result, op[0], op[1]);
1106 break;
1107 case nir_op_bfi:
1108 bld.BFI2(result, op[0], op[1], op[2]);
1109 break;
1110
1111 case nir_op_bitfield_insert:
1112 unreachable("not reached: should have been lowered");
1113
1114 case nir_op_ishl:
1115 bld.SHL(result, op[0], op[1]);
1116 break;
1117 case nir_op_ishr:
1118 bld.ASR(result, op[0], op[1]);
1119 break;
1120 case nir_op_ushr:
1121 bld.SHR(result, op[0], op[1]);
1122 break;
1123
1124 case nir_op_pack_half_2x16_split:
1125 bld.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT, result, op[0], op[1]);
1126 break;
1127
1128 case nir_op_ffma:
1129 inst = bld.MAD(result, op[2], op[1], op[0]);
1130 inst->saturate = instr->dest.saturate;
1131 break;
1132
1133 case nir_op_flrp:
1134 inst = bld.LRP(result, op[0], op[1], op[2]);
1135 inst->saturate = instr->dest.saturate;
1136 break;
1137
1138 case nir_op_bcsel:
1139 if (optimize_frontfacing_ternary(instr, result))
1140 return;
1141
1142 bld.CMP(bld.null_reg_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ);
1143 inst = bld.SEL(result, op[1], op[2]);
1144 inst->predicate = BRW_PREDICATE_NORMAL;
1145 break;
1146
1147 case nir_op_extract_u8:
1148 case nir_op_extract_i8: {
1149 nir_const_value *byte = nir_src_as_const_value(instr->src[1].src);
1150 bld.emit(SHADER_OPCODE_EXTRACT_BYTE,
1151 result, op[0], brw_imm_ud(byte->u[0]));
1152 break;
1153 }
1154
1155 case nir_op_extract_u16:
1156 case nir_op_extract_i16: {
1157 nir_const_value *word = nir_src_as_const_value(instr->src[1].src);
1158 bld.emit(SHADER_OPCODE_EXTRACT_WORD,
1159 result, op[0], brw_imm_ud(word->u[0]));
1160 break;
1161 }
1162
1163 default:
1164 unreachable("unhandled instruction");
1165 }
1166
1167 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1168 * to sign extend the low bit to 0/~0
1169 */
1170 if (devinfo->gen <= 5 &&
1171 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1172 fs_reg masked = vgrf(glsl_type::int_type);
1173 bld.AND(masked, result, brw_imm_d(1));
1174 masked.negate = true;
1175 bld.MOV(retype(result, BRW_REGISTER_TYPE_D), masked);
1176 }
1177 }
1178
1179 void
1180 fs_visitor::nir_emit_load_const(const fs_builder &bld,
1181 nir_load_const_instr *instr)
1182 {
1183 fs_reg reg = bld.vgrf(BRW_REGISTER_TYPE_D, instr->def.num_components);
1184
1185 for (unsigned i = 0; i < instr->def.num_components; i++)
1186 bld.MOV(offset(reg, bld, i), brw_imm_d(instr->value.i[i]));
1187
1188 nir_ssa_values[instr->def.index] = reg;
1189 }
1190
1191 void
1192 fs_visitor::nir_emit_undef(const fs_builder &bld, nir_ssa_undef_instr *instr)
1193 {
1194 nir_ssa_values[instr->def.index] = bld.vgrf(BRW_REGISTER_TYPE_D,
1195 instr->def.num_components);
1196 }
1197
1198 fs_reg
1199 fs_visitor::get_nir_src(nir_src src)
1200 {
1201 fs_reg reg;
1202 if (src.is_ssa) {
1203 reg = nir_ssa_values[src.ssa->index];
1204 } else {
1205 /* We don't handle indirects on locals */
1206 assert(src.reg.indirect == NULL);
1207 reg = offset(nir_locals[src.reg.reg->index], bld,
1208 src.reg.base_offset * src.reg.reg->num_components);
1209 }
1210
1211 /* to avoid floating-point denorm flushing problems, set the type by
1212 * default to D - instructions that need floating point semantics will set
1213 * this to F if they need to
1214 */
1215 return retype(reg, BRW_REGISTER_TYPE_D);
1216 }
1217
1218 fs_reg
1219 fs_visitor::get_nir_dest(nir_dest dest)
1220 {
1221 if (dest.is_ssa) {
1222 nir_ssa_values[dest.ssa.index] = bld.vgrf(BRW_REGISTER_TYPE_F,
1223 dest.ssa.num_components);
1224 return nir_ssa_values[dest.ssa.index];
1225 } else {
1226 /* We don't handle indirects on locals */
1227 assert(dest.reg.indirect == NULL);
1228 return offset(nir_locals[dest.reg.reg->index], bld,
1229 dest.reg.base_offset * dest.reg.reg->num_components);
1230 }
1231 }
1232
1233 fs_reg
1234 fs_visitor::get_nir_image_deref(const nir_deref_var *deref)
1235 {
1236 fs_reg image(UNIFORM, deref->var->data.driver_location / 4,
1237 BRW_REGISTER_TYPE_UD);
1238 fs_reg indirect;
1239 unsigned indirect_max = 0;
1240
1241 for (const nir_deref *tail = &deref->deref; tail->child;
1242 tail = tail->child) {
1243 const nir_deref_array *deref_array = nir_deref_as_array(tail->child);
1244 assert(tail->child->deref_type == nir_deref_type_array);
1245 const unsigned size = glsl_get_length(tail->type);
1246 const unsigned element_size = type_size_scalar(deref_array->deref.type);
1247 const unsigned base = MIN2(deref_array->base_offset, size - 1);
1248 image = offset(image, bld, base * element_size);
1249
1250 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
1251 fs_reg tmp = vgrf(glsl_type::uint_type);
1252
1253 if (devinfo->gen == 7 && !devinfo->is_haswell) {
1254 /* IVB hangs when trying to access an invalid surface index with
1255 * the dataport. According to the spec "if the index used to
1256 * select an individual element is negative or greater than or
1257 * equal to the size of the array, the results of the operation
1258 * are undefined but may not lead to termination" -- which is one
1259 * of the possible outcomes of the hang. Clamp the index to
1260 * prevent access outside of the array bounds.
1261 */
1262 bld.emit_minmax(tmp, retype(get_nir_src(deref_array->indirect),
1263 BRW_REGISTER_TYPE_UD),
1264 brw_imm_ud(size - base - 1), BRW_CONDITIONAL_L);
1265 } else {
1266 bld.MOV(tmp, get_nir_src(deref_array->indirect));
1267 }
1268
1269 indirect_max += element_size * (tail->type->length - 1);
1270
1271 bld.MUL(tmp, tmp, brw_imm_ud(element_size * 4));
1272 if (indirect.file == BAD_FILE) {
1273 indirect = tmp;
1274 } else {
1275 bld.ADD(indirect, indirect, tmp);
1276 }
1277 }
1278 }
1279
1280 if (indirect.file == BAD_FILE) {
1281 return image;
1282 } else {
1283 /* Emit a pile of MOVs to load the uniform into a temporary. The
1284 * dead-code elimination pass will get rid of what we don't use.
1285 */
1286 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, BRW_IMAGE_PARAM_SIZE);
1287 for (unsigned j = 0; j < BRW_IMAGE_PARAM_SIZE; j++) {
1288 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
1289 offset(tmp, bld, j), offset(image, bld, j),
1290 indirect, brw_imm_ud((indirect_max + 1) * 4));
1291 }
1292 return tmp;
1293 }
1294 }
1295
1296 void
1297 fs_visitor::emit_percomp(const fs_builder &bld, const fs_inst &inst,
1298 unsigned wr_mask)
1299 {
1300 for (unsigned i = 0; i < 4; i++) {
1301 if (!((wr_mask >> i) & 1))
1302 continue;
1303
1304 fs_inst *new_inst = new(mem_ctx) fs_inst(inst);
1305 new_inst->dst = offset(new_inst->dst, bld, i);
1306 for (unsigned j = 0; j < new_inst->sources; j++)
1307 if (new_inst->src[j].file == VGRF)
1308 new_inst->src[j] = offset(new_inst->src[j], bld, i);
1309
1310 bld.emit(new_inst);
1311 }
1312 }
1313
1314 /**
1315 * Get the matching channel register datatype for an image intrinsic of the
1316 * specified GLSL image type.
1317 */
1318 static brw_reg_type
1319 get_image_base_type(const glsl_type *type)
1320 {
1321 switch ((glsl_base_type)type->sampler_type) {
1322 case GLSL_TYPE_UINT:
1323 return BRW_REGISTER_TYPE_UD;
1324 case GLSL_TYPE_INT:
1325 return BRW_REGISTER_TYPE_D;
1326 case GLSL_TYPE_FLOAT:
1327 return BRW_REGISTER_TYPE_F;
1328 default:
1329 unreachable("Not reached.");
1330 }
1331 }
1332
1333 /**
1334 * Get the appropriate atomic op for an image atomic intrinsic.
1335 */
1336 static unsigned
1337 get_image_atomic_op(nir_intrinsic_op op, const glsl_type *type)
1338 {
1339 switch (op) {
1340 case nir_intrinsic_image_atomic_add:
1341 return BRW_AOP_ADD;
1342 case nir_intrinsic_image_atomic_min:
1343 return (get_image_base_type(type) == BRW_REGISTER_TYPE_D ?
1344 BRW_AOP_IMIN : BRW_AOP_UMIN);
1345 case nir_intrinsic_image_atomic_max:
1346 return (get_image_base_type(type) == BRW_REGISTER_TYPE_D ?
1347 BRW_AOP_IMAX : BRW_AOP_UMAX);
1348 case nir_intrinsic_image_atomic_and:
1349 return BRW_AOP_AND;
1350 case nir_intrinsic_image_atomic_or:
1351 return BRW_AOP_OR;
1352 case nir_intrinsic_image_atomic_xor:
1353 return BRW_AOP_XOR;
1354 case nir_intrinsic_image_atomic_exchange:
1355 return BRW_AOP_MOV;
1356 case nir_intrinsic_image_atomic_comp_swap:
1357 return BRW_AOP_CMPWR;
1358 default:
1359 unreachable("Not reachable.");
1360 }
1361 }
1362
1363 static fs_inst *
1364 emit_pixel_interpolater_send(const fs_builder &bld,
1365 enum opcode opcode,
1366 const fs_reg &dst,
1367 const fs_reg &src,
1368 const fs_reg &desc,
1369 glsl_interp_qualifier interpolation)
1370 {
1371 fs_inst *inst;
1372 fs_reg payload;
1373 int mlen;
1374
1375 if (src.file == BAD_FILE) {
1376 /* Dummy payload */
1377 payload = bld.vgrf(BRW_REGISTER_TYPE_F, 1);
1378 mlen = 1;
1379 } else {
1380 payload = src;
1381 mlen = 2 * bld.dispatch_width() / 8;
1382 }
1383
1384 inst = bld.emit(opcode, dst, payload, desc);
1385 inst->mlen = mlen;
1386 /* 2 floats per slot returned */
1387 inst->regs_written = 2 * bld.dispatch_width() / 8;
1388 inst->pi_noperspective = interpolation == INTERP_QUALIFIER_NOPERSPECTIVE;
1389
1390 return inst;
1391 }
1392
1393 /**
1394 * Computes 1 << x, given a D/UD register containing some value x.
1395 */
1396 static fs_reg
1397 intexp2(const fs_builder &bld, const fs_reg &x)
1398 {
1399 assert(x.type == BRW_REGISTER_TYPE_UD || x.type == BRW_REGISTER_TYPE_D);
1400
1401 fs_reg result = bld.vgrf(x.type, 1);
1402 fs_reg one = bld.vgrf(x.type, 1);
1403
1404 bld.MOV(one, retype(brw_imm_d(1), one.type));
1405 bld.SHL(result, one, x);
1406 return result;
1407 }
1408
1409 void
1410 fs_visitor::emit_gs_end_primitive(const nir_src &vertex_count_nir_src)
1411 {
1412 assert(stage == MESA_SHADER_GEOMETRY);
1413
1414 struct brw_gs_prog_data *gs_prog_data =
1415 (struct brw_gs_prog_data *) prog_data;
1416
1417 /* We can only do EndPrimitive() functionality when the control data
1418 * consists of cut bits. Fortunately, the only time it isn't is when the
1419 * output type is points, in which case EndPrimitive() is a no-op.
1420 */
1421 if (gs_prog_data->control_data_format !=
1422 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
1423 return;
1424 }
1425
1426 /* Cut bits use one bit per vertex. */
1427 assert(gs_compile->control_data_bits_per_vertex == 1);
1428
1429 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
1430 vertex_count.type = BRW_REGISTER_TYPE_UD;
1431
1432 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
1433 * vertex n, 0 otherwise. So all we need to do here is mark bit
1434 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
1435 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
1436 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
1437 *
1438 * Note that if EndPrimitive() is called before emitting any vertices, this
1439 * will cause us to set bit 31 of the control_data_bits register to 1.
1440 * That's fine because:
1441 *
1442 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
1443 * output, so the hardware will ignore cut bit 31.
1444 *
1445 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
1446 * last vertex, so setting cut bit 31 has no effect (since the primitive
1447 * is automatically ended when the GS terminates).
1448 *
1449 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
1450 * control_data_bits register to 0 when the first vertex is emitted.
1451 */
1452
1453 const fs_builder abld = bld.annotate("end primitive");
1454
1455 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
1456 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1457 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
1458 fs_reg mask = intexp2(abld, prev_count);
1459 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1460 * attention to the lower 5 bits of its second source argument, so on this
1461 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
1462 * ((vertex_count - 1) % 32).
1463 */
1464 abld.OR(this->control_data_bits, this->control_data_bits, mask);
1465 }
1466
1467 void
1468 fs_visitor::emit_gs_control_data_bits(const fs_reg &vertex_count)
1469 {
1470 assert(stage == MESA_SHADER_GEOMETRY);
1471 assert(gs_compile->control_data_bits_per_vertex != 0);
1472
1473 struct brw_gs_prog_data *gs_prog_data =
1474 (struct brw_gs_prog_data *) prog_data;
1475
1476 const fs_builder abld = bld.annotate("emit control data bits");
1477 const fs_builder fwa_bld = bld.exec_all();
1478
1479 /* We use a single UD register to accumulate control data bits (32 bits
1480 * for each of the SIMD8 channels). So we need to write a DWord (32 bits)
1481 * at a time.
1482 *
1483 * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
1484 * We have select a 128-bit group via the Global and Per-Slot Offsets, then
1485 * use the Channel Mask phase to enable/disable which DWord within that
1486 * group to write. (Remember, different SIMD8 channels may have emitted
1487 * different numbers of vertices, so we may need per-slot offsets.)
1488 *
1489 * Channel masking presents an annoying problem: we may have to replicate
1490 * the data up to 4 times:
1491 *
1492 * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
1493 *
1494 * To avoid penalizing shaders that emit a small number of vertices, we
1495 * can avoid these sometimes: if the size of the control data header is
1496 * <= 128 bits, then there is only 1 OWord. All SIMD8 channels will land
1497 * land in the same 128-bit group, so we can skip per-slot offsets.
1498 *
1499 * Similarly, if the control data header is <= 32 bits, there is only one
1500 * DWord, so we can skip channel masks.
1501 */
1502 enum opcode opcode = SHADER_OPCODE_URB_WRITE_SIMD8;
1503
1504 fs_reg channel_mask, per_slot_offset;
1505
1506 if (gs_compile->control_data_header_size_bits > 32) {
1507 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
1508 channel_mask = vgrf(glsl_type::uint_type);
1509 }
1510
1511 if (gs_compile->control_data_header_size_bits > 128) {
1512 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT;
1513 per_slot_offset = vgrf(glsl_type::uint_type);
1514 }
1515
1516 /* Figure out which DWord we're trying to write to using the formula:
1517 *
1518 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
1519 *
1520 * Since bits_per_vertex is a power of two, and is known at compile
1521 * time, this can be optimized to:
1522 *
1523 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
1524 */
1525 if (opcode != SHADER_OPCODE_URB_WRITE_SIMD8) {
1526 fs_reg dword_index = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1527 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1528 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
1529 unsigned log2_bits_per_vertex =
1530 _mesa_fls(gs_compile->control_data_bits_per_vertex);
1531 abld.SHR(dword_index, prev_count, brw_imm_ud(6u - log2_bits_per_vertex));
1532
1533 if (per_slot_offset.file != BAD_FILE) {
1534 /* Set the per-slot offset to dword_index / 4, so that we'll write to
1535 * the appropriate OWord within the control data header.
1536 */
1537 abld.SHR(per_slot_offset, dword_index, brw_imm_ud(2u));
1538 }
1539
1540 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
1541 * write to the appropriate DWORD within the OWORD.
1542 */
1543 fs_reg channel = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1544 fwa_bld.AND(channel, dword_index, brw_imm_ud(3u));
1545 channel_mask = intexp2(fwa_bld, channel);
1546 /* Then the channel masks need to be in bits 23:16. */
1547 fwa_bld.SHL(channel_mask, channel_mask, brw_imm_ud(16u));
1548 }
1549
1550 /* Store the control data bits in the message payload and send it. */
1551 int mlen = 2;
1552 if (channel_mask.file != BAD_FILE)
1553 mlen += 4; /* channel masks, plus 3 extra copies of the data */
1554 if (per_slot_offset.file != BAD_FILE)
1555 mlen++;
1556
1557 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
1558 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, mlen);
1559 int i = 0;
1560 sources[i++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
1561 if (per_slot_offset.file != BAD_FILE)
1562 sources[i++] = per_slot_offset;
1563 if (channel_mask.file != BAD_FILE)
1564 sources[i++] = channel_mask;
1565 while (i < mlen) {
1566 sources[i++] = this->control_data_bits;
1567 }
1568
1569 abld.LOAD_PAYLOAD(payload, sources, mlen, mlen);
1570 fs_inst *inst = abld.emit(opcode, reg_undef, payload);
1571 inst->mlen = mlen;
1572 /* We need to increment Global Offset by 256-bits to make room for
1573 * Broadwell's extra "Vertex Count" payload at the beginning of the
1574 * URB entry. Since this is an OWord message, Global Offset is counted
1575 * in 128-bit units, so we must set it to 2.
1576 */
1577 if (gs_prog_data->static_vertex_count == -1)
1578 inst->offset = 2;
1579 }
1580
1581 void
1582 fs_visitor::set_gs_stream_control_data_bits(const fs_reg &vertex_count,
1583 unsigned stream_id)
1584 {
1585 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
1586
1587 /* Note: we are calling this *before* increasing vertex_count, so
1588 * this->vertex_count == vertex_count - 1 in the formula above.
1589 */
1590
1591 /* Stream mode uses 2 bits per vertex */
1592 assert(gs_compile->control_data_bits_per_vertex == 2);
1593
1594 /* Must be a valid stream */
1595 assert(stream_id >= 0 && stream_id < MAX_VERTEX_STREAMS);
1596
1597 /* Control data bits are initialized to 0 so we don't have to set any
1598 * bits when sending vertices to stream 0.
1599 */
1600 if (stream_id == 0)
1601 return;
1602
1603 const fs_builder abld = bld.annotate("set stream control data bits", NULL);
1604
1605 /* reg::sid = stream_id */
1606 fs_reg sid = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1607 abld.MOV(sid, brw_imm_ud(stream_id));
1608
1609 /* reg:shift_count = 2 * (vertex_count - 1) */
1610 fs_reg shift_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1611 abld.SHL(shift_count, vertex_count, brw_imm_ud(1u));
1612
1613 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1614 * attention to the lower 5 bits of its second source argument, so on this
1615 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
1616 * stream_id << ((2 * (vertex_count - 1)) % 32).
1617 */
1618 fs_reg mask = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1619 abld.SHL(mask, sid, shift_count);
1620 abld.OR(this->control_data_bits, this->control_data_bits, mask);
1621 }
1622
1623 void
1624 fs_visitor::emit_gs_vertex(const nir_src &vertex_count_nir_src,
1625 unsigned stream_id)
1626 {
1627 assert(stage == MESA_SHADER_GEOMETRY);
1628
1629 struct brw_gs_prog_data *gs_prog_data =
1630 (struct brw_gs_prog_data *) prog_data;
1631
1632 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
1633 vertex_count.type = BRW_REGISTER_TYPE_UD;
1634
1635 /* Haswell and later hardware ignores the "Render Stream Select" bits
1636 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
1637 * and instead sends all primitives down the pipeline for rasterization.
1638 * If the SOL stage is enabled, "Render Stream Select" is honored and
1639 * primitives bound to non-zero streams are discarded after stream output.
1640 *
1641 * Since the only purpose of primives sent to non-zero streams is to
1642 * be recorded by transform feedback, we can simply discard all geometry
1643 * bound to these streams when transform feedback is disabled.
1644 */
1645 if (stream_id > 0 && !nir->info.has_transform_feedback_varyings)
1646 return;
1647
1648 /* If we're outputting 32 control data bits or less, then we can wait
1649 * until the shader is over to output them all. Otherwise we need to
1650 * output them as we go. Now is the time to do it, since we're about to
1651 * output the vertex_count'th vertex, so it's guaranteed that the
1652 * control data bits associated with the (vertex_count - 1)th vertex are
1653 * correct.
1654 */
1655 if (gs_compile->control_data_header_size_bits > 32) {
1656 const fs_builder abld =
1657 bld.annotate("emit vertex: emit control data bits");
1658
1659 /* Only emit control data bits if we've finished accumulating a batch
1660 * of 32 bits. This is the case when:
1661 *
1662 * (vertex_count * bits_per_vertex) % 32 == 0
1663 *
1664 * (in other words, when the last 5 bits of vertex_count *
1665 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
1666 * integer n (which is always the case, since bits_per_vertex is
1667 * always 1 or 2), this is equivalent to requiring that the last 5-n
1668 * bits of vertex_count are 0:
1669 *
1670 * vertex_count & (2^(5-n) - 1) == 0
1671 *
1672 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
1673 * equivalent to:
1674 *
1675 * vertex_count & (32 / bits_per_vertex - 1) == 0
1676 *
1677 * TODO: If vertex_count is an immediate, we could do some of this math
1678 * at compile time...
1679 */
1680 fs_inst *inst =
1681 abld.AND(bld.null_reg_d(), vertex_count,
1682 brw_imm_ud(32u / gs_compile->control_data_bits_per_vertex - 1u));
1683 inst->conditional_mod = BRW_CONDITIONAL_Z;
1684
1685 abld.IF(BRW_PREDICATE_NORMAL);
1686 /* If vertex_count is 0, then no control data bits have been
1687 * accumulated yet, so we can skip emitting them.
1688 */
1689 abld.CMP(bld.null_reg_d(), vertex_count, brw_imm_ud(0u),
1690 BRW_CONDITIONAL_NEQ);
1691 abld.IF(BRW_PREDICATE_NORMAL);
1692 emit_gs_control_data_bits(vertex_count);
1693 abld.emit(BRW_OPCODE_ENDIF);
1694
1695 /* Reset control_data_bits to 0 so we can start accumulating a new
1696 * batch.
1697 *
1698 * Note: in the case where vertex_count == 0, this neutralizes the
1699 * effect of any call to EndPrimitive() that the shader may have
1700 * made before outputting its first vertex.
1701 */
1702 inst = abld.MOV(this->control_data_bits, brw_imm_ud(0u));
1703 inst->force_writemask_all = true;
1704 abld.emit(BRW_OPCODE_ENDIF);
1705 }
1706
1707 emit_urb_writes(vertex_count);
1708
1709 /* In stream mode we have to set control data bits for all vertices
1710 * unless we have disabled control data bits completely (which we do
1711 * do for GL_POINTS outputs that don't use streams).
1712 */
1713 if (gs_compile->control_data_header_size_bits > 0 &&
1714 gs_prog_data->control_data_format ==
1715 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
1716 set_gs_stream_control_data_bits(vertex_count, stream_id);
1717 }
1718 }
1719
1720 void
1721 fs_visitor::emit_gs_input_load(const fs_reg &dst,
1722 const nir_src &vertex_src,
1723 unsigned base_offset,
1724 const nir_src &offset_src,
1725 unsigned num_components)
1726 {
1727 struct brw_gs_prog_data *gs_prog_data = (struct brw_gs_prog_data *) prog_data;
1728
1729 nir_const_value *vertex_const = nir_src_as_const_value(vertex_src);
1730 nir_const_value *offset_const = nir_src_as_const_value(offset_src);
1731 const unsigned push_reg_count = gs_prog_data->base.urb_read_length * 8;
1732
1733 /* Offset 0 is the VUE header, which contains VARYING_SLOT_LAYER [.y],
1734 * VARYING_SLOT_VIEWPORT [.z], and VARYING_SLOT_PSIZ [.w]. Only
1735 * gl_PointSize is available as a GS input, however, so it must be that.
1736 */
1737 const bool is_point_size = (base_offset == 0);
1738
1739 if (offset_const != NULL && vertex_const != NULL &&
1740 4 * (base_offset + offset_const->u[0]) < push_reg_count) {
1741 int imm_offset = (base_offset + offset_const->u[0]) * 4 +
1742 vertex_const->u[0] * push_reg_count;
1743 /* This input was pushed into registers. */
1744 if (is_point_size) {
1745 /* gl_PointSize comes in .w */
1746 assert(imm_offset == 0);
1747 bld.MOV(dst, fs_reg(ATTR, imm_offset + 3, dst.type));
1748 } else {
1749 for (unsigned i = 0; i < num_components; i++) {
1750 bld.MOV(offset(dst, bld, i),
1751 fs_reg(ATTR, imm_offset + i, dst.type));
1752 }
1753 }
1754 } else {
1755 /* Resort to the pull model. Ensure the VUE handles are provided. */
1756 gs_prog_data->base.include_vue_handles = true;
1757
1758 unsigned first_icp_handle = gs_prog_data->include_primitive_id ? 3 : 2;
1759 fs_reg icp_handle;
1760
1761 if (vertex_const) {
1762 /* The vertex index is constant; just select the proper URB handle. */
1763 icp_handle =
1764 retype(brw_vec8_grf(first_icp_handle + vertex_const->i[0], 0),
1765 BRW_REGISTER_TYPE_UD);
1766 } else {
1767 /* The vertex index is non-constant. We need to use indirect
1768 * addressing to fetch the proper URB handle.
1769 *
1770 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
1771 * indicating that channel <n> should read the handle from
1772 * DWord <n>. We convert that to bytes by multiplying by 4.
1773 *
1774 * Next, we convert the vertex index to bytes by multiplying
1775 * by 32 (shifting by 5), and add the two together. This is
1776 * the final indirect byte offset.
1777 */
1778 fs_reg sequence = bld.vgrf(BRW_REGISTER_TYPE_W, 1);
1779 fs_reg channel_offsets = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1780 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1781 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1782 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1783
1784 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
1785 bld.MOV(sequence, fs_reg(brw_imm_v(0x76543210)));
1786 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
1787 bld.SHL(channel_offsets, sequence, brw_imm_ud(2u));
1788 /* Convert vertex_index to bytes (multiply by 32) */
1789 bld.SHL(vertex_offset_bytes,
1790 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
1791 brw_imm_ud(5u));
1792 bld.ADD(icp_offset_bytes, vertex_offset_bytes, channel_offsets);
1793
1794 /* Use first_icp_handle as the base offset. There is one register
1795 * of URB handles per vertex, so inform the register allocator that
1796 * we might read up to nir->info.gs.vertices_in registers.
1797 */
1798 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
1799 fs_reg(brw_vec8_grf(first_icp_handle, 0)),
1800 fs_reg(icp_offset_bytes),
1801 brw_imm_ud(nir->info.gs.vertices_in * REG_SIZE));
1802 }
1803
1804 fs_inst *inst;
1805 if (offset_const) {
1806 /* Constant indexing - use global offset. */
1807 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
1808 inst->offset = base_offset + offset_const->u[0];
1809 inst->base_mrf = -1;
1810 inst->mlen = 1;
1811 inst->regs_written = num_components;
1812 } else {
1813 /* Indirect indexing - use per-slot offsets as well. */
1814 const fs_reg srcs[] = { icp_handle, get_nir_src(offset_src) };
1815 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
1816 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
1817
1818 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst, payload);
1819 inst->offset = base_offset;
1820 inst->base_mrf = -1;
1821 inst->mlen = 2;
1822 inst->regs_written = num_components;
1823 }
1824
1825 if (is_point_size) {
1826 /* Read the whole VUE header (because of alignment) and read .w. */
1827 fs_reg tmp = bld.vgrf(dst.type, 4);
1828 inst->dst = tmp;
1829 inst->regs_written = 4;
1830 bld.MOV(dst, offset(tmp, bld, 3));
1831 }
1832 }
1833 }
1834
1835 fs_reg
1836 fs_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
1837 {
1838 nir_src *offset_src = nir_get_io_offset_src(instr);
1839 nir_const_value *const_value = nir_src_as_const_value(*offset_src);
1840
1841 if (const_value) {
1842 /* The only constant offset we should find is 0. brw_nir.c's
1843 * add_const_offset_to_base() will fold other constant offsets
1844 * into instr->const_index[0].
1845 */
1846 assert(const_value->u[0] == 0);
1847 return fs_reg();
1848 }
1849
1850 return get_nir_src(*offset_src);
1851 }
1852
1853 void
1854 fs_visitor::nir_emit_vs_intrinsic(const fs_builder &bld,
1855 nir_intrinsic_instr *instr)
1856 {
1857 assert(stage == MESA_SHADER_VERTEX);
1858
1859 fs_reg dest;
1860 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
1861 dest = get_nir_dest(instr->dest);
1862
1863 switch (instr->intrinsic) {
1864 case nir_intrinsic_load_vertex_id:
1865 unreachable("should be lowered by lower_vertex_id()");
1866
1867 case nir_intrinsic_load_vertex_id_zero_base:
1868 case nir_intrinsic_load_base_vertex:
1869 case nir_intrinsic_load_instance_id:
1870 case nir_intrinsic_load_base_instance:
1871 case nir_intrinsic_load_draw_id: {
1872 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
1873 fs_reg val = nir_system_values[sv];
1874 assert(val.file != BAD_FILE);
1875 dest.type = val.type;
1876 bld.MOV(dest, val);
1877 break;
1878 }
1879
1880 default:
1881 nir_emit_intrinsic(bld, instr);
1882 break;
1883 }
1884 }
1885
1886 void
1887 fs_visitor::nir_emit_tes_intrinsic(const fs_builder &bld,
1888 nir_intrinsic_instr *instr)
1889 {
1890 assert(stage == MESA_SHADER_TESS_EVAL);
1891 struct brw_tes_prog_data *tes_prog_data = (struct brw_tes_prog_data *) prog_data;
1892
1893 fs_reg dest;
1894 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
1895 dest = get_nir_dest(instr->dest);
1896
1897 switch (instr->intrinsic) {
1898 case nir_intrinsic_load_primitive_id:
1899 bld.MOV(dest, fs_reg(brw_vec1_grf(0, 1)));
1900 break;
1901 case nir_intrinsic_load_tess_coord:
1902 /* gl_TessCoord is part of the payload in g1-3 */
1903 for (unsigned i = 0; i < 3; i++) {
1904 bld.MOV(offset(dest, bld, i), fs_reg(brw_vec8_grf(1 + i, 0)));
1905 }
1906 break;
1907
1908 case nir_intrinsic_load_tess_level_outer:
1909 /* When the TES reads gl_TessLevelOuter, we ensure that the patch header
1910 * appears as a push-model input. So, we can simply use the ATTR file
1911 * rather than issuing URB read messages. The data is stored in the
1912 * high DWords in reverse order - DWord 7 contains .x, DWord 6 contains
1913 * .y, and so on.
1914 */
1915 switch (tes_prog_data->domain) {
1916 case BRW_TESS_DOMAIN_QUAD:
1917 for (unsigned i = 0; i < 4; i++)
1918 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 7 - i));
1919 break;
1920 case BRW_TESS_DOMAIN_TRI:
1921 for (unsigned i = 0; i < 3; i++)
1922 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 7 - i));
1923 break;
1924 case BRW_TESS_DOMAIN_ISOLINE:
1925 for (unsigned i = 0; i < 2; i++)
1926 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 7 - i));
1927 break;
1928 }
1929 break;
1930
1931 case nir_intrinsic_load_tess_level_inner:
1932 /* When the TES reads gl_TessLevelInner, we ensure that the patch header
1933 * appears as a push-model input. So, we can simply use the ATTR file
1934 * rather than issuing URB read messages.
1935 */
1936 switch (tes_prog_data->domain) {
1937 case BRW_TESS_DOMAIN_QUAD:
1938 bld.MOV(dest, component(fs_reg(ATTR, 0), 3));
1939 bld.MOV(offset(dest, bld, 1), component(fs_reg(ATTR, 0), 2));
1940 break;
1941 case BRW_TESS_DOMAIN_TRI:
1942 bld.MOV(dest, component(fs_reg(ATTR, 0), 4));
1943 break;
1944 case BRW_TESS_DOMAIN_ISOLINE:
1945 /* ignore - value is undefined */
1946 break;
1947 }
1948 break;
1949
1950 case nir_intrinsic_load_input:
1951 case nir_intrinsic_load_per_vertex_input: {
1952 fs_reg indirect_offset = get_indirect_offset(instr);
1953 unsigned imm_offset = instr->const_index[0];
1954
1955 fs_inst *inst;
1956 if (indirect_offset.file == BAD_FILE) {
1957 /* Arbitrarily only push up to 32 vec4 slots worth of data,
1958 * which is 16 registers (since each holds 2 vec4 slots).
1959 */
1960 const unsigned max_push_slots = 32;
1961 if (imm_offset < max_push_slots) {
1962 fs_reg src = fs_reg(ATTR, imm_offset / 2, dest.type);
1963 for (int i = 0; i < instr->num_components; i++) {
1964 bld.MOV(offset(dest, bld, i),
1965 component(src, 4 * (imm_offset % 2) + i));
1966 }
1967 tes_prog_data->base.urb_read_length =
1968 MAX2(tes_prog_data->base.urb_read_length,
1969 DIV_ROUND_UP(imm_offset + 1, 2));
1970 } else {
1971 /* Replicate the patch handle to all enabled channels */
1972 const fs_reg srcs[] = {
1973 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD)
1974 };
1975 fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1976 bld.LOAD_PAYLOAD(patch_handle, srcs, ARRAY_SIZE(srcs), 0);
1977
1978 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dest, patch_handle);
1979 inst->mlen = 1;
1980 inst->offset = imm_offset;
1981 inst->base_mrf = -1;
1982 inst->regs_written = instr->num_components;
1983 }
1984 } else {
1985 /* Indirect indexing - use per-slot offsets as well. */
1986 const fs_reg srcs[] = {
1987 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD),
1988 indirect_offset
1989 };
1990 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
1991 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
1992
1993 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dest, payload);
1994 inst->mlen = 2;
1995 inst->offset = imm_offset;
1996 inst->base_mrf = -1;
1997 inst->regs_written = instr->num_components;
1998 }
1999 break;
2000 }
2001 default:
2002 nir_emit_intrinsic(bld, instr);
2003 break;
2004 }
2005 }
2006
2007 void
2008 fs_visitor::nir_emit_gs_intrinsic(const fs_builder &bld,
2009 nir_intrinsic_instr *instr)
2010 {
2011 assert(stage == MESA_SHADER_GEOMETRY);
2012 fs_reg indirect_offset;
2013
2014 fs_reg dest;
2015 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2016 dest = get_nir_dest(instr->dest);
2017
2018 switch (instr->intrinsic) {
2019 case nir_intrinsic_load_primitive_id:
2020 assert(stage == MESA_SHADER_GEOMETRY);
2021 assert(((struct brw_gs_prog_data *)prog_data)->include_primitive_id);
2022 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
2023 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD));
2024 break;
2025
2026 case nir_intrinsic_load_input:
2027 unreachable("load_input intrinsics are invalid for the GS stage");
2028
2029 case nir_intrinsic_load_per_vertex_input:
2030 emit_gs_input_load(dest, instr->src[0], instr->const_index[0],
2031 instr->src[1], instr->num_components);
2032 break;
2033
2034 case nir_intrinsic_emit_vertex_with_counter:
2035 emit_gs_vertex(instr->src[0], instr->const_index[0]);
2036 break;
2037
2038 case nir_intrinsic_end_primitive_with_counter:
2039 emit_gs_end_primitive(instr->src[0]);
2040 break;
2041
2042 case nir_intrinsic_set_vertex_count:
2043 bld.MOV(this->final_gs_vertex_count, get_nir_src(instr->src[0]));
2044 break;
2045
2046 case nir_intrinsic_load_invocation_id: {
2047 fs_reg val = nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
2048 assert(val.file != BAD_FILE);
2049 dest.type = val.type;
2050 bld.MOV(dest, val);
2051 break;
2052 }
2053
2054 default:
2055 nir_emit_intrinsic(bld, instr);
2056 break;
2057 }
2058 }
2059
2060 void
2061 fs_visitor::nir_emit_fs_intrinsic(const fs_builder &bld,
2062 nir_intrinsic_instr *instr)
2063 {
2064 assert(stage == MESA_SHADER_FRAGMENT);
2065 struct brw_wm_prog_data *wm_prog_data =
2066 (struct brw_wm_prog_data *) prog_data;
2067
2068 fs_reg dest;
2069 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2070 dest = get_nir_dest(instr->dest);
2071
2072 switch (instr->intrinsic) {
2073 case nir_intrinsic_load_front_face:
2074 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D),
2075 *emit_frontfacing_interpolation());
2076 break;
2077
2078 case nir_intrinsic_load_sample_pos: {
2079 fs_reg sample_pos = nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
2080 assert(sample_pos.file != BAD_FILE);
2081 dest.type = sample_pos.type;
2082 bld.MOV(dest, sample_pos);
2083 bld.MOV(offset(dest, bld, 1), offset(sample_pos, bld, 1));
2084 break;
2085 }
2086
2087 case nir_intrinsic_load_helper_invocation:
2088 case nir_intrinsic_load_sample_mask_in:
2089 case nir_intrinsic_load_sample_id: {
2090 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
2091 fs_reg val = nir_system_values[sv];
2092 assert(val.file != BAD_FILE);
2093 dest.type = val.type;
2094 bld.MOV(dest, val);
2095 break;
2096 }
2097
2098 case nir_intrinsic_discard:
2099 case nir_intrinsic_discard_if: {
2100 /* We track our discarded pixels in f0.1. By predicating on it, we can
2101 * update just the flag bits that aren't yet discarded. If there's no
2102 * condition, we emit a CMP of g0 != g0, so all currently executing
2103 * channels will get turned off.
2104 */
2105 fs_inst *cmp;
2106 if (instr->intrinsic == nir_intrinsic_discard_if) {
2107 cmp = bld.CMP(bld.null_reg_f(), get_nir_src(instr->src[0]),
2108 brw_imm_d(0), BRW_CONDITIONAL_Z);
2109 } else {
2110 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
2111 BRW_REGISTER_TYPE_UW));
2112 cmp = bld.CMP(bld.null_reg_f(), some_reg, some_reg, BRW_CONDITIONAL_NZ);
2113 }
2114 cmp->predicate = BRW_PREDICATE_NORMAL;
2115 cmp->flag_subreg = 1;
2116
2117 if (devinfo->gen >= 6) {
2118 emit_discard_jump();
2119 }
2120 break;
2121 }
2122
2123 case nir_intrinsic_interp_var_at_centroid:
2124 case nir_intrinsic_interp_var_at_sample:
2125 case nir_intrinsic_interp_var_at_offset: {
2126 /* Handle ARB_gpu_shader5 interpolation intrinsics
2127 *
2128 * It's worth a quick word of explanation as to why we handle the full
2129 * variable-based interpolation intrinsic rather than a lowered version
2130 * with like we do for other inputs. We have to do that because the way
2131 * we set up inputs doesn't allow us to use the already setup inputs for
2132 * interpolation. At the beginning of the shader, we go through all of
2133 * the input variables and do the initial interpolation and put it in
2134 * the nir_inputs array based on its location as determined in
2135 * nir_lower_io. If the input isn't used, dead code cleans up and
2136 * everything works fine. However, when we get to the ARB_gpu_shader5
2137 * interpolation intrinsics, we need to reinterpolate the input
2138 * differently. If we used an intrinsic that just had an index it would
2139 * only give us the offset into the nir_inputs array. However, this is
2140 * useless because that value is post-interpolation and we need
2141 * pre-interpolation. In order to get the actual location of the bits
2142 * we get from the vertex fetching hardware, we need the variable.
2143 */
2144 wm_prog_data->pulls_bary = true;
2145
2146 fs_reg dst_xy = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
2147 const glsl_interp_qualifier interpolation =
2148 (glsl_interp_qualifier) instr->variables[0]->var->data.interpolation;
2149
2150 switch (instr->intrinsic) {
2151 case nir_intrinsic_interp_var_at_centroid:
2152 emit_pixel_interpolater_send(bld,
2153 FS_OPCODE_INTERPOLATE_AT_CENTROID,
2154 dst_xy,
2155 fs_reg(), /* src */
2156 brw_imm_ud(0u),
2157 interpolation);
2158 break;
2159
2160 case nir_intrinsic_interp_var_at_sample: {
2161 nir_const_value *const_sample = nir_src_as_const_value(instr->src[0]);
2162
2163 if (const_sample) {
2164 unsigned msg_data = const_sample->i[0] << 4;
2165
2166 emit_pixel_interpolater_send(bld,
2167 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
2168 dst_xy,
2169 fs_reg(), /* src */
2170 brw_imm_ud(msg_data),
2171 interpolation);
2172 } else {
2173 const fs_reg sample_src = retype(get_nir_src(instr->src[0]),
2174 BRW_REGISTER_TYPE_UD);
2175
2176 if (nir_src_is_dynamically_uniform(instr->src[0])) {
2177 const fs_reg sample_id = bld.emit_uniformize(sample_src);
2178 const fs_reg msg_data = vgrf(glsl_type::uint_type);
2179 bld.exec_all().group(1, 0)
2180 .SHL(msg_data, sample_id, brw_imm_ud(4u));
2181 emit_pixel_interpolater_send(bld,
2182 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
2183 dst_xy,
2184 fs_reg(), /* src */
2185 msg_data,
2186 interpolation);
2187 } else {
2188 /* Make a loop that sends a message to the pixel interpolater
2189 * for the sample number in each live channel. If there are
2190 * multiple channels with the same sample number then these
2191 * will be handled simultaneously with a single interation of
2192 * the loop.
2193 */
2194 bld.emit(BRW_OPCODE_DO);
2195
2196 /* Get the next live sample number into sample_id_reg */
2197 const fs_reg sample_id = bld.emit_uniformize(sample_src);
2198
2199 /* Set the flag register so that we can perform the send
2200 * message on all channels that have the same sample number
2201 */
2202 bld.CMP(bld.null_reg_ud(),
2203 sample_src, sample_id,
2204 BRW_CONDITIONAL_EQ);
2205 const fs_reg msg_data = vgrf(glsl_type::uint_type);
2206 bld.exec_all().group(1, 0)
2207 .SHL(msg_data, sample_id, brw_imm_ud(4u));
2208 fs_inst *inst =
2209 emit_pixel_interpolater_send(bld,
2210 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
2211 dst_xy,
2212 fs_reg(), /* src */
2213 msg_data,
2214 interpolation);
2215 set_predicate(BRW_PREDICATE_NORMAL, inst);
2216
2217 /* Continue the loop if there are any live channels left */
2218 set_predicate_inv(BRW_PREDICATE_NORMAL,
2219 true, /* inverse */
2220 bld.emit(BRW_OPCODE_WHILE));
2221 }
2222 }
2223
2224 break;
2225 }
2226
2227 case nir_intrinsic_interp_var_at_offset: {
2228 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
2229
2230 if (const_offset) {
2231 unsigned off_x = MIN2((int)(const_offset->f[0] * 16), 7) & 0xf;
2232 unsigned off_y = MIN2((int)(const_offset->f[1] * 16), 7) & 0xf;
2233
2234 emit_pixel_interpolater_send(bld,
2235 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET,
2236 dst_xy,
2237 fs_reg(), /* src */
2238 brw_imm_ud(off_x | (off_y << 4)),
2239 interpolation);
2240 } else {
2241 fs_reg src = vgrf(glsl_type::ivec2_type);
2242 fs_reg offset_src = retype(get_nir_src(instr->src[0]),
2243 BRW_REGISTER_TYPE_F);
2244 for (int i = 0; i < 2; i++) {
2245 fs_reg temp = vgrf(glsl_type::float_type);
2246 bld.MUL(temp, offset(offset_src, bld, i), brw_imm_f(16.0f));
2247 fs_reg itemp = vgrf(glsl_type::int_type);
2248 bld.MOV(itemp, temp); /* float to int */
2249
2250 /* Clamp the upper end of the range to +7/16.
2251 * ARB_gpu_shader5 requires that we support a maximum offset
2252 * of +0.5, which isn't representable in a S0.4 value -- if
2253 * we didn't clamp it, we'd end up with -8/16, which is the
2254 * opposite of what the shader author wanted.
2255 *
2256 * This is legal due to ARB_gpu_shader5's quantization
2257 * rules:
2258 *
2259 * "Not all values of <offset> may be supported; x and y
2260 * offsets may be rounded to fixed-point values with the
2261 * number of fraction bits given by the
2262 * implementation-dependent constant
2263 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
2264 */
2265 set_condmod(BRW_CONDITIONAL_L,
2266 bld.SEL(offset(src, bld, i), itemp, brw_imm_d(7)));
2267 }
2268
2269 const enum opcode opcode = FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET;
2270 emit_pixel_interpolater_send(bld,
2271 opcode,
2272 dst_xy,
2273 src,
2274 brw_imm_ud(0u),
2275 interpolation);
2276 }
2277 break;
2278 }
2279
2280 default:
2281 unreachable("Invalid intrinsic");
2282 }
2283
2284 for (unsigned j = 0; j < instr->num_components; j++) {
2285 fs_reg src = interp_reg(instr->variables[0]->var->data.location, j);
2286 src.type = dest.type;
2287
2288 bld.emit(FS_OPCODE_LINTERP, dest, dst_xy, src);
2289 dest = offset(dest, bld, 1);
2290 }
2291 break;
2292 }
2293 default:
2294 nir_emit_intrinsic(bld, instr);
2295 break;
2296 }
2297 }
2298
2299 void
2300 fs_visitor::nir_emit_cs_intrinsic(const fs_builder &bld,
2301 nir_intrinsic_instr *instr)
2302 {
2303 assert(stage == MESA_SHADER_COMPUTE);
2304 struct brw_cs_prog_data *cs_prog_data =
2305 (struct brw_cs_prog_data *) prog_data;
2306
2307 fs_reg dest;
2308 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2309 dest = get_nir_dest(instr->dest);
2310
2311 switch (instr->intrinsic) {
2312 case nir_intrinsic_barrier:
2313 emit_barrier();
2314 cs_prog_data->uses_barrier = true;
2315 break;
2316
2317 case nir_intrinsic_load_local_invocation_id:
2318 case nir_intrinsic_load_work_group_id: {
2319 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
2320 fs_reg val = nir_system_values[sv];
2321 assert(val.file != BAD_FILE);
2322 dest.type = val.type;
2323 for (unsigned i = 0; i < 3; i++)
2324 bld.MOV(offset(dest, bld, i), offset(val, bld, i));
2325 break;
2326 }
2327
2328 case nir_intrinsic_load_num_work_groups: {
2329 const unsigned surface =
2330 cs_prog_data->binding_table.work_groups_start;
2331
2332 cs_prog_data->uses_num_work_groups = true;
2333
2334 fs_reg surf_index = brw_imm_ud(surface);
2335 brw_mark_surface_used(prog_data, surface);
2336
2337 /* Read the 3 GLuint components of gl_NumWorkGroups */
2338 for (unsigned i = 0; i < 3; i++) {
2339 fs_reg read_result =
2340 emit_untyped_read(bld, surf_index,
2341 brw_imm_ud(i << 2),
2342 1 /* dims */, 1 /* size */,
2343 BRW_PREDICATE_NONE);
2344 read_result.type = dest.type;
2345 bld.MOV(dest, read_result);
2346 dest = offset(dest, bld, 1);
2347 }
2348 break;
2349 }
2350
2351 case nir_intrinsic_shared_atomic_add:
2352 nir_emit_shared_atomic(bld, BRW_AOP_ADD, instr);
2353 break;
2354 case nir_intrinsic_shared_atomic_imin:
2355 nir_emit_shared_atomic(bld, BRW_AOP_IMIN, instr);
2356 break;
2357 case nir_intrinsic_shared_atomic_umin:
2358 nir_emit_shared_atomic(bld, BRW_AOP_UMIN, instr);
2359 break;
2360 case nir_intrinsic_shared_atomic_imax:
2361 nir_emit_shared_atomic(bld, BRW_AOP_IMAX, instr);
2362 break;
2363 case nir_intrinsic_shared_atomic_umax:
2364 nir_emit_shared_atomic(bld, BRW_AOP_UMAX, instr);
2365 break;
2366 case nir_intrinsic_shared_atomic_and:
2367 nir_emit_shared_atomic(bld, BRW_AOP_AND, instr);
2368 break;
2369 case nir_intrinsic_shared_atomic_or:
2370 nir_emit_shared_atomic(bld, BRW_AOP_OR, instr);
2371 break;
2372 case nir_intrinsic_shared_atomic_xor:
2373 nir_emit_shared_atomic(bld, BRW_AOP_XOR, instr);
2374 break;
2375 case nir_intrinsic_shared_atomic_exchange:
2376 nir_emit_shared_atomic(bld, BRW_AOP_MOV, instr);
2377 break;
2378 case nir_intrinsic_shared_atomic_comp_swap:
2379 nir_emit_shared_atomic(bld, BRW_AOP_CMPWR, instr);
2380 break;
2381
2382 case nir_intrinsic_load_shared: {
2383 assert(devinfo->gen >= 7);
2384
2385 fs_reg surf_index = brw_imm_ud(GEN7_BTI_SLM);
2386
2387 /* Get the offset to read from */
2388 fs_reg offset_reg;
2389 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
2390 if (const_offset) {
2391 offset_reg = brw_imm_ud(instr->const_index[0] + const_offset->u[0]);
2392 } else {
2393 offset_reg = vgrf(glsl_type::uint_type);
2394 bld.ADD(offset_reg,
2395 retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_UD),
2396 brw_imm_ud(instr->const_index[0]));
2397 }
2398
2399 /* Read the vector */
2400 fs_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
2401 1 /* dims */,
2402 instr->num_components,
2403 BRW_PREDICATE_NONE);
2404 read_result.type = dest.type;
2405 for (int i = 0; i < instr->num_components; i++)
2406 bld.MOV(offset(dest, bld, i), offset(read_result, bld, i));
2407
2408 break;
2409 }
2410
2411 case nir_intrinsic_store_shared: {
2412 assert(devinfo->gen >= 7);
2413
2414 /* Block index */
2415 fs_reg surf_index = brw_imm_ud(GEN7_BTI_SLM);
2416
2417 /* Value */
2418 fs_reg val_reg = get_nir_src(instr->src[0]);
2419
2420 /* Writemask */
2421 unsigned writemask = instr->const_index[1];
2422
2423 /* Combine groups of consecutive enabled channels in one write
2424 * message. We use ffs to find the first enabled channel and then ffs on
2425 * the bit-inverse, down-shifted writemask to determine the length of
2426 * the block of enabled bits.
2427 */
2428 while (writemask) {
2429 unsigned first_component = ffs(writemask) - 1;
2430 unsigned length = ffs(~(writemask >> first_component)) - 1;
2431 fs_reg offset_reg;
2432
2433 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
2434 if (const_offset) {
2435 offset_reg = brw_imm_ud(instr->const_index[0] + const_offset->u[0] +
2436 4 * first_component);
2437 } else {
2438 offset_reg = vgrf(glsl_type::uint_type);
2439 bld.ADD(offset_reg,
2440 retype(get_nir_src(instr->src[1]), BRW_REGISTER_TYPE_UD),
2441 brw_imm_ud(instr->const_index[0] + 4 * first_component));
2442 }
2443
2444 emit_untyped_write(bld, surf_index, offset_reg,
2445 offset(val_reg, bld, first_component),
2446 1 /* dims */, length,
2447 BRW_PREDICATE_NONE);
2448
2449 /* Clear the bits in the writemask that we just wrote, then try
2450 * again to see if more channels are left.
2451 */
2452 writemask &= (15 << (first_component + length));
2453 }
2454
2455 break;
2456 }
2457
2458 default:
2459 nir_emit_intrinsic(bld, instr);
2460 break;
2461 }
2462 }
2463
2464 void
2465 fs_visitor::nir_emit_intrinsic(const fs_builder &bld, nir_intrinsic_instr *instr)
2466 {
2467 fs_reg dest;
2468 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2469 dest = get_nir_dest(instr->dest);
2470
2471 switch (instr->intrinsic) {
2472 case nir_intrinsic_atomic_counter_inc:
2473 case nir_intrinsic_atomic_counter_dec:
2474 case nir_intrinsic_atomic_counter_read: {
2475 using namespace surface_access;
2476
2477 /* Get the arguments of the atomic intrinsic. */
2478 const fs_reg offset = get_nir_src(instr->src[0]);
2479 const unsigned surface = (stage_prog_data->binding_table.abo_start +
2480 instr->const_index[0]);
2481 fs_reg tmp;
2482
2483 /* Emit a surface read or atomic op. */
2484 switch (instr->intrinsic) {
2485 case nir_intrinsic_atomic_counter_read:
2486 tmp = emit_untyped_read(bld, brw_imm_ud(surface), offset, 1, 1);
2487 break;
2488
2489 case nir_intrinsic_atomic_counter_inc:
2490 tmp = emit_untyped_atomic(bld, brw_imm_ud(surface), offset, fs_reg(),
2491 fs_reg(), 1, 1, BRW_AOP_INC);
2492 break;
2493
2494 case nir_intrinsic_atomic_counter_dec:
2495 tmp = emit_untyped_atomic(bld, brw_imm_ud(surface), offset, fs_reg(),
2496 fs_reg(), 1, 1, BRW_AOP_PREDEC);
2497 break;
2498
2499 default:
2500 unreachable("Unreachable");
2501 }
2502
2503 /* Assign the result. */
2504 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD), tmp);
2505
2506 /* Mark the surface as used. */
2507 brw_mark_surface_used(stage_prog_data, surface);
2508 break;
2509 }
2510
2511 case nir_intrinsic_image_load:
2512 case nir_intrinsic_image_store:
2513 case nir_intrinsic_image_atomic_add:
2514 case nir_intrinsic_image_atomic_min:
2515 case nir_intrinsic_image_atomic_max:
2516 case nir_intrinsic_image_atomic_and:
2517 case nir_intrinsic_image_atomic_or:
2518 case nir_intrinsic_image_atomic_xor:
2519 case nir_intrinsic_image_atomic_exchange:
2520 case nir_intrinsic_image_atomic_comp_swap: {
2521 using namespace image_access;
2522
2523 /* Get the referenced image variable and type. */
2524 const nir_variable *var = instr->variables[0]->var;
2525 const glsl_type *type = var->type->without_array();
2526 const brw_reg_type base_type = get_image_base_type(type);
2527
2528 /* Get some metadata from the image intrinsic. */
2529 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
2530 const unsigned arr_dims = type->sampler_array ? 1 : 0;
2531 const unsigned surf_dims = type->coordinate_components() - arr_dims;
2532 const mesa_format format =
2533 (var->data.image.write_only ? MESA_FORMAT_NONE :
2534 _mesa_get_shader_image_format(var->data.image.format));
2535
2536 /* Get the arguments of the image intrinsic. */
2537 const fs_reg image = get_nir_image_deref(instr->variables[0]);
2538 const fs_reg addr = retype(get_nir_src(instr->src[0]),
2539 BRW_REGISTER_TYPE_UD);
2540 const fs_reg src0 = (info->num_srcs >= 3 ?
2541 retype(get_nir_src(instr->src[2]), base_type) :
2542 fs_reg());
2543 const fs_reg src1 = (info->num_srcs >= 4 ?
2544 retype(get_nir_src(instr->src[3]), base_type) :
2545 fs_reg());
2546 fs_reg tmp;
2547
2548 /* Emit an image load, store or atomic op. */
2549 if (instr->intrinsic == nir_intrinsic_image_load)
2550 tmp = emit_image_load(bld, image, addr, surf_dims, arr_dims, format);
2551
2552 else if (instr->intrinsic == nir_intrinsic_image_store)
2553 emit_image_store(bld, image, addr, src0, surf_dims, arr_dims, format);
2554
2555 else
2556 tmp = emit_image_atomic(bld, image, addr, src0, src1,
2557 surf_dims, arr_dims, info->dest_components,
2558 get_image_atomic_op(instr->intrinsic, type));
2559
2560 /* Assign the result. */
2561 for (unsigned c = 0; c < info->dest_components; ++c)
2562 bld.MOV(offset(retype(dest, base_type), bld, c),
2563 offset(tmp, bld, c));
2564 break;
2565 }
2566
2567 case nir_intrinsic_memory_barrier_atomic_counter:
2568 case nir_intrinsic_memory_barrier_buffer:
2569 case nir_intrinsic_memory_barrier_image:
2570 case nir_intrinsic_memory_barrier: {
2571 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 16 / dispatch_width);
2572 bld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp)
2573 ->regs_written = 2;
2574 break;
2575 }
2576
2577 case nir_intrinsic_group_memory_barrier:
2578 case nir_intrinsic_memory_barrier_shared:
2579 /* We treat these workgroup-level barriers as no-ops. This should be
2580 * safe at present and as long as:
2581 *
2582 * - Memory access instructions are not subsequently reordered by the
2583 * compiler back-end.
2584 *
2585 * - All threads from a given compute shader workgroup fit within a
2586 * single subslice and therefore talk to the same HDC shared unit
2587 * what supposedly guarantees ordering and coherency between threads
2588 * from the same workgroup. This may change in the future when we
2589 * start splitting workgroups across multiple subslices.
2590 *
2591 * - The context is not in fault-and-stream mode, which could cause
2592 * memory transactions (including to SLM) prior to the barrier to be
2593 * replayed after the barrier if a pagefault occurs. This shouldn't
2594 * be a problem up to and including SKL because fault-and-stream is
2595 * not usable due to hardware issues, but that's likely to change in
2596 * the future.
2597 */
2598 break;
2599
2600 case nir_intrinsic_shader_clock: {
2601 /* We cannot do anything if there is an event, so ignore it for now */
2602 fs_reg shader_clock = get_timestamp(bld);
2603 const fs_reg srcs[] = { shader_clock.set_smear(0), shader_clock.set_smear(1) };
2604
2605 bld.LOAD_PAYLOAD(dest, srcs, ARRAY_SIZE(srcs), 0);
2606 break;
2607 }
2608
2609 case nir_intrinsic_image_size: {
2610 /* Get the referenced image variable and type. */
2611 const nir_variable *var = instr->variables[0]->var;
2612 const glsl_type *type = var->type->without_array();
2613
2614 /* Get the size of the image. */
2615 const fs_reg image = get_nir_image_deref(instr->variables[0]);
2616 const fs_reg size = offset(image, bld, BRW_IMAGE_PARAM_SIZE_OFFSET);
2617
2618 /* For 1DArray image types, the array index is stored in the Z component.
2619 * Fix this by swizzling the Z component to the Y component.
2620 */
2621 const bool is_1d_array_image =
2622 type->sampler_dimensionality == GLSL_SAMPLER_DIM_1D &&
2623 type->sampler_array;
2624
2625 /* For CubeArray images, we should count the number of cubes instead
2626 * of the number of faces. Fix it by dividing the (Z component) by 6.
2627 */
2628 const bool is_cube_array_image =
2629 type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE &&
2630 type->sampler_array;
2631
2632 /* Copy all the components. */
2633 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
2634 for (unsigned c = 0; c < info->dest_components; ++c) {
2635 if ((int)c >= type->coordinate_components()) {
2636 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
2637 brw_imm_d(1));
2638 } else if (c == 1 && is_1d_array_image) {
2639 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
2640 offset(size, bld, 2));
2641 } else if (c == 2 && is_cube_array_image) {
2642 bld.emit(SHADER_OPCODE_INT_QUOTIENT,
2643 offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
2644 offset(size, bld, c), brw_imm_d(6));
2645 } else {
2646 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
2647 offset(size, bld, c));
2648 }
2649 }
2650
2651 break;
2652 }
2653
2654 case nir_intrinsic_image_samples:
2655 /* The driver does not support multi-sampled images. */
2656 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), brw_imm_d(1));
2657 break;
2658
2659 case nir_intrinsic_load_uniform: {
2660 /* Offsets are in bytes but they should always be multiples of 4 */
2661 assert(instr->const_index[0] % 4 == 0);
2662
2663 fs_reg src(UNIFORM, instr->const_index[0] / 4, dest.type);
2664
2665 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
2666 if (const_offset) {
2667 /* Offsets are in bytes but they should always be multiples of 4 */
2668 assert(const_offset->u[0] % 4 == 0);
2669 src.reg_offset = const_offset->u[0] / 4;
2670
2671 for (unsigned j = 0; j < instr->num_components; j++) {
2672 bld.MOV(offset(dest, bld, j), offset(src, bld, j));
2673 }
2674 } else {
2675 fs_reg indirect = retype(get_nir_src(instr->src[0]),
2676 BRW_REGISTER_TYPE_UD);
2677
2678 /* We need to pass a size to the MOV_INDIRECT but we don't want it to
2679 * go past the end of the uniform. In order to keep the n'th
2680 * component from running past, we subtract off the size of all but
2681 * one component of the vector.
2682 */
2683 assert(instr->const_index[1] >= instr->num_components * 4);
2684 unsigned read_size = instr->const_index[1] -
2685 (instr->num_components - 1) * 4;
2686
2687 for (unsigned j = 0; j < instr->num_components; j++) {
2688 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
2689 offset(dest, bld, j), offset(src, bld, j),
2690 indirect, brw_imm_ud(read_size));
2691 }
2692 }
2693 break;
2694 }
2695
2696 case nir_intrinsic_load_ubo: {
2697 nir_const_value *const_index = nir_src_as_const_value(instr->src[0]);
2698 fs_reg surf_index;
2699
2700 if (const_index) {
2701 const unsigned index = stage_prog_data->binding_table.ubo_start +
2702 const_index->u[0];
2703 surf_index = brw_imm_ud(index);
2704 brw_mark_surface_used(prog_data, index);
2705 } else {
2706 /* The block index is not a constant. Evaluate the index expression
2707 * per-channel and add the base UBO index; we have to select a value
2708 * from any live channel.
2709 */
2710 surf_index = vgrf(glsl_type::uint_type);
2711 bld.ADD(surf_index, get_nir_src(instr->src[0]),
2712 brw_imm_ud(stage_prog_data->binding_table.ubo_start));
2713 surf_index = bld.emit_uniformize(surf_index);
2714
2715 /* Assume this may touch any UBO. It would be nice to provide
2716 * a tighter bound, but the array information is already lowered away.
2717 */
2718 brw_mark_surface_used(prog_data,
2719 stage_prog_data->binding_table.ubo_start +
2720 nir->info.num_ubos - 1);
2721 }
2722
2723 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
2724 if (const_offset == NULL) {
2725 fs_reg base_offset = retype(get_nir_src(instr->src[1]),
2726 BRW_REGISTER_TYPE_D);
2727
2728 for (int i = 0; i < instr->num_components; i++)
2729 VARYING_PULL_CONSTANT_LOAD(bld, offset(dest, bld, i), surf_index,
2730 base_offset, i * 4);
2731 } else {
2732 fs_reg packed_consts = vgrf(glsl_type::float_type);
2733 packed_consts.type = dest.type;
2734
2735 struct brw_reg const_offset_reg = brw_imm_ud(const_offset->u[0] & ~15);
2736 bld.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD, packed_consts,
2737 surf_index, const_offset_reg);
2738
2739 for (unsigned i = 0; i < instr->num_components; i++) {
2740 packed_consts.set_smear(const_offset->u[0] % 16 / 4 + i);
2741
2742 /* The std140 packing rules don't allow vectors to cross 16-byte
2743 * boundaries, and a reg is 32 bytes.
2744 */
2745 assert(packed_consts.subreg_offset < 32);
2746
2747 bld.MOV(dest, packed_consts);
2748 dest = offset(dest, bld, 1);
2749 }
2750 }
2751 break;
2752 }
2753
2754 case nir_intrinsic_load_ssbo: {
2755 assert(devinfo->gen >= 7);
2756
2757 nir_const_value *const_uniform_block =
2758 nir_src_as_const_value(instr->src[0]);
2759
2760 fs_reg surf_index;
2761 if (const_uniform_block) {
2762 unsigned index = stage_prog_data->binding_table.ssbo_start +
2763 const_uniform_block->u[0];
2764 surf_index = brw_imm_ud(index);
2765 brw_mark_surface_used(prog_data, index);
2766 } else {
2767 surf_index = vgrf(glsl_type::uint_type);
2768 bld.ADD(surf_index, get_nir_src(instr->src[0]),
2769 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
2770
2771 /* Assume this may touch any UBO. It would be nice to provide
2772 * a tighter bound, but the array information is already lowered away.
2773 */
2774 brw_mark_surface_used(prog_data,
2775 stage_prog_data->binding_table.ssbo_start +
2776 nir->info.num_ssbos - 1);
2777 }
2778
2779 fs_reg offset_reg;
2780 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
2781 if (const_offset) {
2782 offset_reg = brw_imm_ud(const_offset->u[0]);
2783 } else {
2784 offset_reg = get_nir_src(instr->src[1]);
2785 }
2786
2787 /* Read the vector */
2788 fs_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
2789 1 /* dims */,
2790 instr->num_components,
2791 BRW_PREDICATE_NONE);
2792 read_result.type = dest.type;
2793 for (int i = 0; i < instr->num_components; i++)
2794 bld.MOV(offset(dest, bld, i), offset(read_result, bld, i));
2795
2796 break;
2797 }
2798
2799 case nir_intrinsic_load_input: {
2800 fs_reg src;
2801 if (stage == MESA_SHADER_VERTEX) {
2802 src = fs_reg(ATTR, instr->const_index[0], dest.type);
2803 } else {
2804 src = offset(retype(nir_inputs, dest.type), bld,
2805 instr->const_index[0]);
2806 }
2807
2808 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
2809 assert(const_offset && "Indirect input loads not allowed");
2810 src = offset(src, bld, const_offset->u[0]);
2811
2812 for (unsigned j = 0; j < instr->num_components; j++) {
2813 bld.MOV(offset(dest, bld, j), offset(src, bld, j));
2814 }
2815 break;
2816 }
2817
2818 case nir_intrinsic_store_ssbo: {
2819 assert(devinfo->gen >= 7);
2820
2821 /* Block index */
2822 fs_reg surf_index;
2823 nir_const_value *const_uniform_block =
2824 nir_src_as_const_value(instr->src[1]);
2825 if (const_uniform_block) {
2826 unsigned index = stage_prog_data->binding_table.ssbo_start +
2827 const_uniform_block->u[0];
2828 surf_index = brw_imm_ud(index);
2829 brw_mark_surface_used(prog_data, index);
2830 } else {
2831 surf_index = vgrf(glsl_type::uint_type);
2832 bld.ADD(surf_index, get_nir_src(instr->src[1]),
2833 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
2834
2835 brw_mark_surface_used(prog_data,
2836 stage_prog_data->binding_table.ssbo_start +
2837 nir->info.num_ssbos - 1);
2838 }
2839
2840 /* Value */
2841 fs_reg val_reg = get_nir_src(instr->src[0]);
2842
2843 /* Writemask */
2844 unsigned writemask = instr->const_index[0];
2845
2846 /* Combine groups of consecutive enabled channels in one write
2847 * message. We use ffs to find the first enabled channel and then ffs on
2848 * the bit-inverse, down-shifted writemask to determine the length of
2849 * the block of enabled bits.
2850 */
2851 while (writemask) {
2852 unsigned first_component = ffs(writemask) - 1;
2853 unsigned length = ffs(~(writemask >> first_component)) - 1;
2854
2855 fs_reg offset_reg;
2856 nir_const_value *const_offset = nir_src_as_const_value(instr->src[2]);
2857 if (const_offset) {
2858 offset_reg = brw_imm_ud(const_offset->u[0] + 4 * first_component);
2859 } else {
2860 offset_reg = vgrf(glsl_type::uint_type);
2861 bld.ADD(offset_reg,
2862 retype(get_nir_src(instr->src[2]), BRW_REGISTER_TYPE_UD),
2863 brw_imm_ud(4 * first_component));
2864 }
2865
2866 emit_untyped_write(bld, surf_index, offset_reg,
2867 offset(val_reg, bld, first_component),
2868 1 /* dims */, length,
2869 BRW_PREDICATE_NONE);
2870
2871 /* Clear the bits in the writemask that we just wrote, then try
2872 * again to see if more channels are left.
2873 */
2874 writemask &= (15 << (first_component + length));
2875 }
2876 break;
2877 }
2878
2879 case nir_intrinsic_store_output: {
2880 fs_reg src = get_nir_src(instr->src[0]);
2881 fs_reg new_dest = offset(retype(nir_outputs, src.type), bld,
2882 instr->const_index[0]);
2883
2884 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
2885 assert(const_offset && "Indirect output stores not allowed");
2886 new_dest = offset(new_dest, bld, const_offset->u[0]);
2887
2888 for (unsigned j = 0; j < instr->num_components; j++) {
2889 bld.MOV(offset(new_dest, bld, j), offset(src, bld, j));
2890 }
2891 break;
2892 }
2893
2894 case nir_intrinsic_ssbo_atomic_add:
2895 nir_emit_ssbo_atomic(bld, BRW_AOP_ADD, instr);
2896 break;
2897 case nir_intrinsic_ssbo_atomic_imin:
2898 nir_emit_ssbo_atomic(bld, BRW_AOP_IMIN, instr);
2899 break;
2900 case nir_intrinsic_ssbo_atomic_umin:
2901 nir_emit_ssbo_atomic(bld, BRW_AOP_UMIN, instr);
2902 break;
2903 case nir_intrinsic_ssbo_atomic_imax:
2904 nir_emit_ssbo_atomic(bld, BRW_AOP_IMAX, instr);
2905 break;
2906 case nir_intrinsic_ssbo_atomic_umax:
2907 nir_emit_ssbo_atomic(bld, BRW_AOP_UMAX, instr);
2908 break;
2909 case nir_intrinsic_ssbo_atomic_and:
2910 nir_emit_ssbo_atomic(bld, BRW_AOP_AND, instr);
2911 break;
2912 case nir_intrinsic_ssbo_atomic_or:
2913 nir_emit_ssbo_atomic(bld, BRW_AOP_OR, instr);
2914 break;
2915 case nir_intrinsic_ssbo_atomic_xor:
2916 nir_emit_ssbo_atomic(bld, BRW_AOP_XOR, instr);
2917 break;
2918 case nir_intrinsic_ssbo_atomic_exchange:
2919 nir_emit_ssbo_atomic(bld, BRW_AOP_MOV, instr);
2920 break;
2921 case nir_intrinsic_ssbo_atomic_comp_swap:
2922 nir_emit_ssbo_atomic(bld, BRW_AOP_CMPWR, instr);
2923 break;
2924
2925 case nir_intrinsic_get_buffer_size: {
2926 nir_const_value *const_uniform_block = nir_src_as_const_value(instr->src[0]);
2927 unsigned ssbo_index = const_uniform_block ? const_uniform_block->u[0] : 0;
2928 int reg_width = dispatch_width / 8;
2929
2930 /* Set LOD = 0 */
2931 fs_reg source = brw_imm_d(0);
2932
2933 int mlen = 1 * reg_width;
2934
2935 /* A resinfo's sampler message is used to get the buffer size.
2936 * The SIMD8's writeback message consists of four registers and
2937 * SIMD16's writeback message consists of 8 destination registers
2938 * (two per each component), although we are only interested on the
2939 * first component, where resinfo returns the buffer size for
2940 * SURFTYPE_BUFFER.
2941 */
2942 int regs_written = 4 * mlen;
2943 fs_reg src_payload = fs_reg(VGRF, alloc.allocate(mlen),
2944 BRW_REGISTER_TYPE_UD);
2945 bld.LOAD_PAYLOAD(src_payload, &source, 1, 0);
2946 fs_reg buffer_size = fs_reg(VGRF, alloc.allocate(regs_written),
2947 BRW_REGISTER_TYPE_UD);
2948 const unsigned index = prog_data->binding_table.ssbo_start + ssbo_index;
2949 fs_inst *inst = bld.emit(FS_OPCODE_GET_BUFFER_SIZE, buffer_size,
2950 src_payload, brw_imm_ud(index));
2951 inst->header_size = 0;
2952 inst->mlen = mlen;
2953 inst->regs_written = regs_written;
2954 bld.emit(inst);
2955 bld.MOV(retype(dest, buffer_size.type), buffer_size);
2956
2957 brw_mark_surface_used(prog_data, index);
2958 break;
2959 }
2960
2961 default:
2962 unreachable("unknown intrinsic");
2963 }
2964 }
2965
2966 void
2967 fs_visitor::nir_emit_ssbo_atomic(const fs_builder &bld,
2968 int op, nir_intrinsic_instr *instr)
2969 {
2970 fs_reg dest;
2971 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2972 dest = get_nir_dest(instr->dest);
2973
2974 fs_reg surface;
2975 nir_const_value *const_surface = nir_src_as_const_value(instr->src[0]);
2976 if (const_surface) {
2977 unsigned surf_index = stage_prog_data->binding_table.ssbo_start +
2978 const_surface->u[0];
2979 surface = brw_imm_ud(surf_index);
2980 brw_mark_surface_used(prog_data, surf_index);
2981 } else {
2982 surface = vgrf(glsl_type::uint_type);
2983 bld.ADD(surface, get_nir_src(instr->src[0]),
2984 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
2985
2986 /* Assume this may touch any SSBO. This is the same we do for other
2987 * UBO/SSBO accesses with non-constant surface.
2988 */
2989 brw_mark_surface_used(prog_data,
2990 stage_prog_data->binding_table.ssbo_start +
2991 nir->info.num_ssbos - 1);
2992 }
2993
2994 fs_reg offset = get_nir_src(instr->src[1]);
2995 fs_reg data1 = get_nir_src(instr->src[2]);
2996 fs_reg data2;
2997 if (op == BRW_AOP_CMPWR)
2998 data2 = get_nir_src(instr->src[3]);
2999
3000 /* Emit the actual atomic operation operation */
3001
3002 fs_reg atomic_result =
3003 surface_access::emit_untyped_atomic(bld, surface, offset,
3004 data1, data2,
3005 1 /* dims */, 1 /* rsize */,
3006 op,
3007 BRW_PREDICATE_NONE);
3008 dest.type = atomic_result.type;
3009 bld.MOV(dest, atomic_result);
3010 }
3011
3012 void
3013 fs_visitor::nir_emit_shared_atomic(const fs_builder &bld,
3014 int op, nir_intrinsic_instr *instr)
3015 {
3016 fs_reg dest;
3017 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3018 dest = get_nir_dest(instr->dest);
3019
3020 fs_reg surface = brw_imm_ud(GEN7_BTI_SLM);
3021 fs_reg offset = get_nir_src(instr->src[0]);
3022 fs_reg data1 = get_nir_src(instr->src[1]);
3023 fs_reg data2;
3024 if (op == BRW_AOP_CMPWR)
3025 data2 = get_nir_src(instr->src[2]);
3026
3027 /* Emit the actual atomic operation operation */
3028
3029 fs_reg atomic_result =
3030 surface_access::emit_untyped_atomic(bld, surface, offset,
3031 data1, data2,
3032 1 /* dims */, 1 /* rsize */,
3033 op,
3034 BRW_PREDICATE_NONE);
3035 dest.type = atomic_result.type;
3036 bld.MOV(dest, atomic_result);
3037 }
3038
3039 void
3040 fs_visitor::nir_emit_texture(const fs_builder &bld, nir_tex_instr *instr)
3041 {
3042 unsigned texture = instr->texture_index;
3043 unsigned sampler = instr->sampler_index;
3044 fs_reg texture_reg(brw_imm_ud(texture));
3045 fs_reg sampler_reg(brw_imm_ud(sampler));
3046
3047 int gather_component = instr->component;
3048
3049 bool is_cube_array = instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
3050 instr->is_array;
3051
3052 int lod_components = 0;
3053 int UNUSED offset_components = 0;
3054
3055 fs_reg coordinate, shadow_comparitor, lod, lod2, sample_index, mcs, tex_offset;
3056
3057 /* Our hardware requires a LOD for buffer textures */
3058 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
3059 lod = brw_imm_d(0);
3060
3061 for (unsigned i = 0; i < instr->num_srcs; i++) {
3062 fs_reg src = get_nir_src(instr->src[i].src);
3063 switch (instr->src[i].src_type) {
3064 case nir_tex_src_bias:
3065 lod = retype(src, BRW_REGISTER_TYPE_F);
3066 break;
3067 case nir_tex_src_comparitor:
3068 shadow_comparitor = retype(src, BRW_REGISTER_TYPE_F);
3069 break;
3070 case nir_tex_src_coord:
3071 switch (instr->op) {
3072 case nir_texop_txf:
3073 case nir_texop_txf_ms:
3074 case nir_texop_samples_identical:
3075 coordinate = retype(src, BRW_REGISTER_TYPE_D);
3076 break;
3077 default:
3078 coordinate = retype(src, BRW_REGISTER_TYPE_F);
3079 break;
3080 }
3081 break;
3082 case nir_tex_src_ddx:
3083 lod = retype(src, BRW_REGISTER_TYPE_F);
3084 lod_components = nir_tex_instr_src_size(instr, i);
3085 break;
3086 case nir_tex_src_ddy:
3087 lod2 = retype(src, BRW_REGISTER_TYPE_F);
3088 break;
3089 case nir_tex_src_lod:
3090 switch (instr->op) {
3091 case nir_texop_txs:
3092 lod = retype(src, BRW_REGISTER_TYPE_UD);
3093 break;
3094 case nir_texop_txf:
3095 lod = retype(src, BRW_REGISTER_TYPE_D);
3096 break;
3097 default:
3098 lod = retype(src, BRW_REGISTER_TYPE_F);
3099 break;
3100 }
3101 break;
3102 case nir_tex_src_ms_index:
3103 sample_index = retype(src, BRW_REGISTER_TYPE_UD);
3104 break;
3105 case nir_tex_src_offset:
3106 tex_offset = retype(src, BRW_REGISTER_TYPE_D);
3107 if (instr->is_array)
3108 offset_components = instr->coord_components - 1;
3109 else
3110 offset_components = instr->coord_components;
3111 break;
3112 case nir_tex_src_projector:
3113 unreachable("should be lowered");
3114
3115 case nir_tex_src_texture_offset: {
3116 /* Figure out the highest possible texture index and mark it as used */
3117 uint32_t max_used = texture + instr->texture_array_size - 1;
3118 if (instr->op == nir_texop_tg4 && devinfo->gen < 8) {
3119 max_used += stage_prog_data->binding_table.gather_texture_start;
3120 } else {
3121 max_used += stage_prog_data->binding_table.texture_start;
3122 }
3123 brw_mark_surface_used(prog_data, max_used);
3124
3125 /* Emit code to evaluate the actual indexing expression */
3126 texture_reg = vgrf(glsl_type::uint_type);
3127 bld.ADD(texture_reg, src, brw_imm_ud(texture));
3128 texture_reg = bld.emit_uniformize(texture_reg);
3129 break;
3130 }
3131
3132 case nir_tex_src_sampler_offset: {
3133 /* Emit code to evaluate the actual indexing expression */
3134 sampler_reg = vgrf(glsl_type::uint_type);
3135 bld.ADD(sampler_reg, src, brw_imm_ud(sampler));
3136 sampler_reg = bld.emit_uniformize(sampler_reg);
3137 break;
3138 }
3139
3140 default:
3141 unreachable("unknown texture source");
3142 }
3143 }
3144
3145 if (instr->op == nir_texop_txf_ms ||
3146 instr->op == nir_texop_samples_identical) {
3147 if (devinfo->gen >= 7 &&
3148 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
3149 mcs = emit_mcs_fetch(coordinate, instr->coord_components, texture_reg);
3150 } else {
3151 mcs = brw_imm_ud(0u);
3152 }
3153 }
3154
3155 for (unsigned i = 0; i < 3; i++) {
3156 if (instr->const_offset[i] != 0) {
3157 assert(offset_components == 0);
3158 tex_offset = brw_imm_ud(brw_texture_offset(instr->const_offset, 3));
3159 break;
3160 }
3161 }
3162
3163 enum glsl_base_type dest_base_type =
3164 brw_glsl_base_type_for_nir_type (instr->dest_type);
3165
3166 const glsl_type *dest_type =
3167 glsl_type::get_instance(dest_base_type, nir_tex_instr_dest_size(instr),
3168 1);
3169
3170 ir_texture_opcode op;
3171 switch (instr->op) {
3172 case nir_texop_lod: op = ir_lod; break;
3173 case nir_texop_query_levels: op = ir_query_levels; break;
3174 case nir_texop_tex: op = ir_tex; break;
3175 case nir_texop_tg4: op = ir_tg4; break;
3176 case nir_texop_txb: op = ir_txb; break;
3177 case nir_texop_txd: op = ir_txd; break;
3178 case nir_texop_txf: op = ir_txf; break;
3179 case nir_texop_txf_ms: op = ir_txf_ms; break;
3180 case nir_texop_txl: op = ir_txl; break;
3181 case nir_texop_txs: op = ir_txs; break;
3182 case nir_texop_texture_samples: {
3183 fs_reg dst = retype(get_nir_dest(instr->dest), BRW_REGISTER_TYPE_D);
3184 fs_inst *inst = bld.emit(SHADER_OPCODE_SAMPLEINFO, dst,
3185 bld.vgrf(BRW_REGISTER_TYPE_D, 1),
3186 texture_reg, texture_reg);
3187 inst->mlen = 1;
3188 inst->header_size = 1;
3189 inst->base_mrf = -1;
3190 return;
3191 }
3192 case nir_texop_samples_identical: op = ir_samples_identical; break;
3193 default:
3194 unreachable("unknown texture opcode");
3195 }
3196
3197 emit_texture(op, dest_type, coordinate, instr->coord_components,
3198 shadow_comparitor, lod, lod2, lod_components, sample_index,
3199 tex_offset, mcs, gather_component,
3200 is_cube_array, texture, texture_reg, sampler, sampler_reg);
3201
3202 fs_reg dest = get_nir_dest(instr->dest);
3203 dest.type = this->result.type;
3204 unsigned num_components = nir_tex_instr_dest_size(instr);
3205 emit_percomp(bld, fs_inst(BRW_OPCODE_MOV, bld.dispatch_width(),
3206 dest, this->result),
3207 (1 << num_components) - 1);
3208 }
3209
3210 void
3211 fs_visitor::nir_emit_jump(const fs_builder &bld, nir_jump_instr *instr)
3212 {
3213 switch (instr->type) {
3214 case nir_jump_break:
3215 bld.emit(BRW_OPCODE_BREAK);
3216 break;
3217 case nir_jump_continue:
3218 bld.emit(BRW_OPCODE_CONTINUE);
3219 break;
3220 case nir_jump_return:
3221 default:
3222 unreachable("unknown jump");
3223 }
3224 }