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