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