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