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