i965/fs: Don't emit duplicated SSBO GET_BUFFER_SIZE instruction unnecessarily.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_nir.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "compiler/glsl/ir.h"
25 #include "brw_fs.h"
26 #include "brw_fs_surface_builder.h"
27 #include "brw_nir.h"
28 #include "brw_program.h"
29
30 using namespace brw;
31 using namespace brw::surface_access;
32
33 void
34 fs_visitor::emit_nir_code()
35 {
36 /* emit the arrays used for inputs and outputs - load/store intrinsics will
37 * be converted to reads/writes of these arrays
38 */
39 nir_setup_inputs();
40 nir_setup_outputs();
41 nir_setup_uniforms();
42 nir_emit_system_values();
43
44 /* get the main function and emit it */
45 nir_foreach_function(function, nir) {
46 assert(strcmp(function->name, "main") == 0);
47 assert(function->impl);
48 nir_emit_impl(function->impl);
49 }
50 }
51
52 void
53 fs_visitor::nir_setup_inputs()
54 {
55 if (stage != MESA_SHADER_FRAGMENT)
56 return;
57
58 nir_inputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_inputs);
59
60 nir_foreach_variable(var, &nir->inputs) {
61 fs_reg input = offset(nir_inputs, bld, var->data.driver_location);
62
63 fs_reg reg;
64 if (var->data.location == VARYING_SLOT_POS) {
65 reg = *emit_fragcoord_interpolation();
66 emit_percomp(bld, fs_inst(BRW_OPCODE_MOV, bld.dispatch_width(),
67 input, reg), 0xF);
68 } else if (var->data.location == VARYING_SLOT_LAYER) {
69 struct brw_reg reg = suboffset(interp_reg(VARYING_SLOT_LAYER, 1), 3);
70 reg.type = BRW_REGISTER_TYPE_D;
71 bld.emit(FS_OPCODE_CINTERP, retype(input, BRW_REGISTER_TYPE_D), reg);
72 } else if (var->data.location == VARYING_SLOT_VIEWPORT) {
73 struct brw_reg reg = suboffset(interp_reg(VARYING_SLOT_VIEWPORT, 2), 3);
74 reg.type = BRW_REGISTER_TYPE_D;
75 bld.emit(FS_OPCODE_CINTERP, retype(input, BRW_REGISTER_TYPE_D), reg);
76 } else {
77 int location = var->data.location;
78 emit_general_interpolation(&input, var->name, var->type,
79 (glsl_interp_qualifier) var->data.interpolation,
80 &location, var->data.centroid,
81 var->data.sample);
82 }
83 }
84 }
85
86 void
87 fs_visitor::nir_setup_single_output_varying(fs_reg *reg,
88 const glsl_type *type,
89 unsigned *location)
90 {
91 if (type->is_array() || type->is_matrix()) {
92 const struct glsl_type *elem_type = glsl_get_array_element(type);
93 const unsigned length = glsl_get_length(type);
94
95 for (unsigned i = 0; i < length; i++) {
96 nir_setup_single_output_varying(reg, elem_type, location);
97 }
98 } else if (type->is_record()) {
99 for (unsigned i = 0; i < type->length; i++) {
100 const struct glsl_type *field_type = type->fields.structure[i].type;
101 nir_setup_single_output_varying(reg, field_type, location);
102 }
103 } else {
104 assert(type->is_scalar() || type->is_vector());
105 unsigned num_elements = type->vector_elements;
106 if (type->is_double())
107 num_elements *= 2;
108 for (unsigned count = 0; count < num_elements; count += 4) {
109 this->outputs[*location] = *reg;
110 this->output_components[*location] = MIN2(4, num_elements - count);
111 *reg = offset(*reg, bld, 4);
112 (*location)++;
113 }
114 }
115 }
116
117 void
118 fs_visitor::nir_setup_outputs()
119 {
120 if (stage == MESA_SHADER_TESS_CTRL)
121 return;
122
123 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
124
125 nir_outputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_outputs);
126
127 nir_foreach_variable(var, &nir->outputs) {
128 fs_reg reg = offset(nir_outputs, bld, var->data.driver_location);
129
130 switch (stage) {
131 case MESA_SHADER_VERTEX:
132 case MESA_SHADER_TESS_EVAL:
133 case MESA_SHADER_GEOMETRY: {
134 unsigned location = var->data.location;
135 nir_setup_single_output_varying(&reg, var->type, &location);
136 break;
137 }
138 case MESA_SHADER_FRAGMENT:
139 if (key->force_dual_color_blend &&
140 var->data.location == FRAG_RESULT_DATA1) {
141 this->dual_src_output = reg;
142 this->do_dual_src = true;
143 } else if (var->data.index > 0) {
144 assert(var->data.location == FRAG_RESULT_DATA0);
145 assert(var->data.index == 1);
146 this->dual_src_output = reg;
147 this->do_dual_src = true;
148 } else if (var->data.location == FRAG_RESULT_COLOR) {
149 /* Writing gl_FragColor outputs to all color regions. */
150 for (unsigned int i = 0; i < MAX2(key->nr_color_regions, 1); i++) {
151 this->outputs[i] = reg;
152 this->output_components[i] = 4;
153 }
154 } else if (var->data.location == FRAG_RESULT_DEPTH) {
155 this->frag_depth = reg;
156 } else if (var->data.location == FRAG_RESULT_STENCIL) {
157 this->frag_stencil = reg;
158 } else if (var->data.location == FRAG_RESULT_SAMPLE_MASK) {
159 this->sample_mask = reg;
160 } else {
161 int vector_elements = var->type->without_array()->vector_elements;
162
163 /* gl_FragData or a user-defined FS output */
164 assert(var->data.location >= FRAG_RESULT_DATA0 &&
165 var->data.location < FRAG_RESULT_DATA0+BRW_MAX_DRAW_BUFFERS);
166
167 /* General color output. */
168 for (unsigned int i = 0; i < MAX2(1, var->type->length); i++) {
169 int output = var->data.location - FRAG_RESULT_DATA0 + i;
170 this->outputs[output] = offset(reg, bld, vector_elements * i);
171 this->output_components[output] = vector_elements;
172 }
173 }
174 break;
175 default:
176 unreachable("unhandled shader stage");
177 }
178 }
179 }
180
181 void
182 fs_visitor::nir_setup_uniforms()
183 {
184 if (dispatch_width != min_dispatch_width)
185 return;
186
187 uniforms = nir->num_uniforms / 4;
188 }
189
190 static bool
191 emit_system_values_block(nir_block *block, fs_visitor *v)
192 {
193 fs_reg *reg;
194
195 nir_foreach_instr(instr, block) {
196 if (instr->type != nir_instr_type_intrinsic)
197 continue;
198
199 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
200 switch (intrin->intrinsic) {
201 case nir_intrinsic_load_vertex_id:
202 unreachable("should be lowered by lower_vertex_id().");
203
204 case nir_intrinsic_load_vertex_id_zero_base:
205 assert(v->stage == MESA_SHADER_VERTEX);
206 reg = &v->nir_system_values[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE];
207 if (reg->file == BAD_FILE)
208 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE);
209 break;
210
211 case nir_intrinsic_load_base_vertex:
212 assert(v->stage == MESA_SHADER_VERTEX);
213 reg = &v->nir_system_values[SYSTEM_VALUE_BASE_VERTEX];
214 if (reg->file == BAD_FILE)
215 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_BASE_VERTEX);
216 break;
217
218 case nir_intrinsic_load_instance_id:
219 assert(v->stage == MESA_SHADER_VERTEX);
220 reg = &v->nir_system_values[SYSTEM_VALUE_INSTANCE_ID];
221 if (reg->file == BAD_FILE)
222 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_INSTANCE_ID);
223 break;
224
225 case nir_intrinsic_load_base_instance:
226 assert(v->stage == MESA_SHADER_VERTEX);
227 reg = &v->nir_system_values[SYSTEM_VALUE_BASE_INSTANCE];
228 if (reg->file == BAD_FILE)
229 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_BASE_INSTANCE);
230 break;
231
232 case nir_intrinsic_load_draw_id:
233 assert(v->stage == MESA_SHADER_VERTEX);
234 reg = &v->nir_system_values[SYSTEM_VALUE_DRAW_ID];
235 if (reg->file == BAD_FILE)
236 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_DRAW_ID);
237 break;
238
239 case nir_intrinsic_load_invocation_id:
240 if (v->stage == MESA_SHADER_TESS_CTRL)
241 break;
242 assert(v->stage == MESA_SHADER_GEOMETRY);
243 reg = &v->nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
244 if (reg->file == BAD_FILE) {
245 const fs_builder abld = v->bld.annotate("gl_InvocationID", NULL);
246 fs_reg g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
247 fs_reg iid = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
248 abld.SHR(iid, g1, brw_imm_ud(27u));
249 *reg = iid;
250 }
251 break;
252
253 case nir_intrinsic_load_sample_pos:
254 assert(v->stage == MESA_SHADER_FRAGMENT);
255 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
256 if (reg->file == BAD_FILE)
257 *reg = *v->emit_samplepos_setup();
258 break;
259
260 case nir_intrinsic_load_sample_id:
261 assert(v->stage == MESA_SHADER_FRAGMENT);
262 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
263 if (reg->file == BAD_FILE)
264 *reg = *v->emit_sampleid_setup();
265 break;
266
267 case nir_intrinsic_load_sample_mask_in:
268 assert(v->stage == MESA_SHADER_FRAGMENT);
269 assert(v->devinfo->gen >= 7);
270 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_MASK_IN];
271 if (reg->file == BAD_FILE)
272 *reg = *v->emit_samplemaskin_setup();
273 break;
274
275 case nir_intrinsic_load_local_invocation_id:
276 assert(v->stage == MESA_SHADER_COMPUTE);
277 reg = &v->nir_system_values[SYSTEM_VALUE_LOCAL_INVOCATION_ID];
278 if (reg->file == BAD_FILE)
279 *reg = *v->emit_cs_local_invocation_id_setup();
280 break;
281
282 case nir_intrinsic_load_work_group_id:
283 assert(v->stage == MESA_SHADER_COMPUTE);
284 reg = &v->nir_system_values[SYSTEM_VALUE_WORK_GROUP_ID];
285 if (reg->file == BAD_FILE)
286 *reg = *v->emit_cs_work_group_id_setup();
287 break;
288
289 case nir_intrinsic_load_helper_invocation:
290 assert(v->stage == MESA_SHADER_FRAGMENT);
291 reg = &v->nir_system_values[SYSTEM_VALUE_HELPER_INVOCATION];
292 if (reg->file == BAD_FILE) {
293 const fs_builder abld =
294 v->bld.annotate("gl_HelperInvocation", NULL);
295
296 /* On Gen6+ (gl_HelperInvocation is only exposed on Gen7+) the
297 * pixel mask is in g1.7 of the thread payload.
298 *
299 * We move the per-channel pixel enable bit to the low bit of each
300 * channel by shifting the byte containing the pixel mask by the
301 * vector immediate 0x76543210UV.
302 *
303 * The region of <1,8,0> reads only 1 byte (the pixel masks for
304 * subspans 0 and 1) in SIMD8 and an additional byte (the pixel
305 * masks for 2 and 3) in SIMD16.
306 */
307 fs_reg shifted = abld.vgrf(BRW_REGISTER_TYPE_UW, 1);
308 abld.SHR(shifted,
309 stride(byte_offset(retype(brw_vec1_grf(1, 0),
310 BRW_REGISTER_TYPE_UB), 28),
311 1, 8, 0),
312 brw_imm_v(0x76543210));
313
314 /* A set bit in the pixel mask means the channel is enabled, but
315 * that is the opposite of gl_HelperInvocation so we need to invert
316 * the mask.
317 *
318 * The negate source-modifier bit of logical instructions on Gen8+
319 * performs 1's complement negation, so we can use that instead of
320 * a NOT instruction.
321 */
322 fs_reg inverted = negate(shifted);
323 if (v->devinfo->gen < 8) {
324 inverted = abld.vgrf(BRW_REGISTER_TYPE_UW);
325 abld.NOT(inverted, shifted);
326 }
327
328 /* We then resolve the 0/1 result to 0/~0 boolean values by ANDing
329 * with 1 and negating.
330 */
331 fs_reg anded = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
332 abld.AND(anded, inverted, brw_imm_uw(1));
333
334 fs_reg dst = abld.vgrf(BRW_REGISTER_TYPE_D, 1);
335 abld.MOV(dst, negate(retype(anded, BRW_REGISTER_TYPE_D)));
336 *reg = dst;
337 }
338 break;
339
340 default:
341 break;
342 }
343 }
344
345 return true;
346 }
347
348 void
349 fs_visitor::nir_emit_system_values()
350 {
351 nir_system_values = ralloc_array(mem_ctx, fs_reg, SYSTEM_VALUE_MAX);
352 for (unsigned i = 0; i < SYSTEM_VALUE_MAX; i++) {
353 nir_system_values[i] = fs_reg();
354 }
355
356 nir_foreach_function(function, nir) {
357 assert(strcmp(function->name, "main") == 0);
358 assert(function->impl);
359 nir_foreach_block(block, function->impl) {
360 emit_system_values_block(block, this);
361 }
362 }
363 }
364
365 void
366 fs_visitor::nir_emit_impl(nir_function_impl *impl)
367 {
368 nir_locals = ralloc_array(mem_ctx, fs_reg, impl->reg_alloc);
369 for (unsigned i = 0; i < impl->reg_alloc; i++) {
370 nir_locals[i] = fs_reg();
371 }
372
373 foreach_list_typed(nir_register, reg, node, &impl->registers) {
374 unsigned array_elems =
375 reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
376 unsigned size = array_elems * reg->num_components;
377 const brw_reg_type reg_type =
378 reg->bit_size == 32 ? BRW_REGISTER_TYPE_F : BRW_REGISTER_TYPE_DF;
379 nir_locals[reg->index] = bld.vgrf(reg_type, size);
380 }
381
382 nir_ssa_values = reralloc(mem_ctx, nir_ssa_values, fs_reg,
383 impl->ssa_alloc);
384
385 nir_emit_cf_list(&impl->body);
386 }
387
388 void
389 fs_visitor::nir_emit_cf_list(exec_list *list)
390 {
391 exec_list_validate(list);
392 foreach_list_typed(nir_cf_node, node, node, list) {
393 switch (node->type) {
394 case nir_cf_node_if:
395 nir_emit_if(nir_cf_node_as_if(node));
396 break;
397
398 case nir_cf_node_loop:
399 nir_emit_loop(nir_cf_node_as_loop(node));
400 break;
401
402 case nir_cf_node_block:
403 nir_emit_block(nir_cf_node_as_block(node));
404 break;
405
406 default:
407 unreachable("Invalid CFG node block");
408 }
409 }
410 }
411
412 void
413 fs_visitor::nir_emit_if(nir_if *if_stmt)
414 {
415 /* first, put the condition into f0 */
416 fs_inst *inst = bld.MOV(bld.null_reg_d(),
417 retype(get_nir_src(if_stmt->condition),
418 BRW_REGISTER_TYPE_D));
419 inst->conditional_mod = BRW_CONDITIONAL_NZ;
420
421 bld.IF(BRW_PREDICATE_NORMAL);
422
423 nir_emit_cf_list(&if_stmt->then_list);
424
425 /* note: if the else is empty, dead CF elimination will remove it */
426 bld.emit(BRW_OPCODE_ELSE);
427
428 nir_emit_cf_list(&if_stmt->else_list);
429
430 bld.emit(BRW_OPCODE_ENDIF);
431 }
432
433 void
434 fs_visitor::nir_emit_loop(nir_loop *loop)
435 {
436 bld.emit(BRW_OPCODE_DO);
437
438 nir_emit_cf_list(&loop->body);
439
440 bld.emit(BRW_OPCODE_WHILE);
441 }
442
443 void
444 fs_visitor::nir_emit_block(nir_block *block)
445 {
446 nir_foreach_instr(instr, block) {
447 nir_emit_instr(instr);
448 }
449 }
450
451 void
452 fs_visitor::nir_emit_instr(nir_instr *instr)
453 {
454 const fs_builder abld = bld.annotate(NULL, instr);
455
456 switch (instr->type) {
457 case nir_instr_type_alu:
458 nir_emit_alu(abld, nir_instr_as_alu(instr));
459 break;
460
461 case nir_instr_type_intrinsic:
462 switch (stage) {
463 case MESA_SHADER_VERTEX:
464 nir_emit_vs_intrinsic(abld, nir_instr_as_intrinsic(instr));
465 break;
466 case MESA_SHADER_TESS_CTRL:
467 nir_emit_tcs_intrinsic(abld, nir_instr_as_intrinsic(instr));
468 break;
469 case MESA_SHADER_TESS_EVAL:
470 nir_emit_tes_intrinsic(abld, nir_instr_as_intrinsic(instr));
471 break;
472 case MESA_SHADER_GEOMETRY:
473 nir_emit_gs_intrinsic(abld, nir_instr_as_intrinsic(instr));
474 break;
475 case MESA_SHADER_FRAGMENT:
476 nir_emit_fs_intrinsic(abld, nir_instr_as_intrinsic(instr));
477 break;
478 case MESA_SHADER_COMPUTE:
479 nir_emit_cs_intrinsic(abld, nir_instr_as_intrinsic(instr));
480 break;
481 default:
482 unreachable("unsupported shader stage");
483 }
484 break;
485
486 case nir_instr_type_tex:
487 nir_emit_texture(abld, nir_instr_as_tex(instr));
488 break;
489
490 case nir_instr_type_load_const:
491 nir_emit_load_const(abld, nir_instr_as_load_const(instr));
492 break;
493
494 case nir_instr_type_ssa_undef:
495 nir_emit_undef(abld, nir_instr_as_ssa_undef(instr));
496 break;
497
498 case nir_instr_type_jump:
499 nir_emit_jump(abld, nir_instr_as_jump(instr));
500 break;
501
502 default:
503 unreachable("unknown instruction type");
504 }
505 }
506
507 /**
508 * Recognizes a parent instruction of nir_op_extract_* and changes the type to
509 * match instr.
510 */
511 bool
512 fs_visitor::optimize_extract_to_float(nir_alu_instr *instr,
513 const fs_reg &result)
514 {
515 if (!instr->src[0].src.is_ssa ||
516 !instr->src[0].src.ssa->parent_instr)
517 return false;
518
519 if (instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
520 return false;
521
522 nir_alu_instr *src0 =
523 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
524
525 if (src0->op != nir_op_extract_u8 && src0->op != nir_op_extract_u16 &&
526 src0->op != nir_op_extract_i8 && src0->op != nir_op_extract_i16)
527 return false;
528
529 nir_const_value *element = nir_src_as_const_value(src0->src[1].src);
530 assert(element != NULL);
531
532 /* Element type to extract.*/
533 const brw_reg_type type = brw_int_type(
534 src0->op == nir_op_extract_u16 || src0->op == nir_op_extract_i16 ? 2 : 1,
535 src0->op == nir_op_extract_i16 || src0->op == nir_op_extract_i8);
536
537 fs_reg op0 = get_nir_src(src0->src[0].src);
538 op0.type = brw_type_for_nir_type(
539 (nir_alu_type)(nir_op_infos[src0->op].input_types[0] |
540 nir_src_bit_size(src0->src[0].src)));
541 op0 = offset(op0, bld, src0->src[0].swizzle[0]);
542
543 set_saturate(instr->dest.saturate,
544 bld.MOV(result, subscript(op0, type, element->u32[0])));
545 return true;
546 }
547
548 bool
549 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr *instr,
550 const fs_reg &result)
551 {
552 if (!instr->src[0].src.is_ssa ||
553 instr->src[0].src.ssa->parent_instr->type != nir_instr_type_intrinsic)
554 return false;
555
556 nir_intrinsic_instr *src0 =
557 nir_instr_as_intrinsic(instr->src[0].src.ssa->parent_instr);
558
559 if (src0->intrinsic != nir_intrinsic_load_front_face)
560 return false;
561
562 nir_const_value *value1 = nir_src_as_const_value(instr->src[1].src);
563 if (!value1 || fabsf(value1->f32[0]) != 1.0f)
564 return false;
565
566 nir_const_value *value2 = nir_src_as_const_value(instr->src[2].src);
567 if (!value2 || fabsf(value2->f32[0]) != 1.0f)
568 return false;
569
570 fs_reg tmp = vgrf(glsl_type::int_type);
571
572 if (devinfo->gen >= 6) {
573 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
574 fs_reg g0 = fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W));
575
576 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
577 *
578 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
579 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
580 *
581 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
582 *
583 * This negation looks like it's safe in practice, because bits 0:4 will
584 * surely be TRIANGLES
585 */
586
587 if (value1->f32[0] == -1.0f) {
588 g0.negate = true;
589 }
590
591 tmp.type = BRW_REGISTER_TYPE_W;
592 tmp.subreg_offset = 2;
593 tmp.stride = 2;
594
595 bld.OR(tmp, g0, brw_imm_uw(0x3f80));
596
597 tmp.type = BRW_REGISTER_TYPE_D;
598 tmp.subreg_offset = 0;
599 tmp.stride = 1;
600 } else {
601 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
602 fs_reg g1_6 = fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D));
603
604 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
605 *
606 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
607 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
608 *
609 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
610 *
611 * This negation looks like it's safe in practice, because bits 0:4 will
612 * surely be TRIANGLES
613 */
614
615 if (value1->f32[0] == -1.0f) {
616 g1_6.negate = true;
617 }
618
619 bld.OR(tmp, g1_6, brw_imm_d(0x3f800000));
620 }
621 bld.AND(retype(result, BRW_REGISTER_TYPE_D), tmp, brw_imm_d(0xbf800000));
622
623 return true;
624 }
625
626 void
627 fs_visitor::nir_emit_alu(const fs_builder &bld, nir_alu_instr *instr)
628 {
629 struct brw_wm_prog_key *fs_key = (struct brw_wm_prog_key *) this->key;
630 fs_inst *inst;
631
632 fs_reg result = get_nir_dest(instr->dest.dest);
633 result.type = brw_type_for_nir_type(
634 (nir_alu_type)(nir_op_infos[instr->op].output_type |
635 nir_dest_bit_size(instr->dest.dest)));
636
637 fs_reg op[4];
638 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
639 op[i] = get_nir_src(instr->src[i].src);
640 op[i].type = brw_type_for_nir_type(
641 (nir_alu_type)(nir_op_infos[instr->op].input_types[i] |
642 nir_src_bit_size(instr->src[i].src)));
643 op[i].abs = instr->src[i].abs;
644 op[i].negate = instr->src[i].negate;
645 }
646
647 /* We get a bunch of mov's out of the from_ssa pass and they may still
648 * be vectorized. We'll handle them as a special-case. We'll also
649 * handle vecN here because it's basically the same thing.
650 */
651 switch (instr->op) {
652 case nir_op_imov:
653 case nir_op_fmov:
654 case nir_op_vec2:
655 case nir_op_vec3:
656 case nir_op_vec4: {
657 fs_reg temp = result;
658 bool need_extra_copy = false;
659 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
660 if (!instr->src[i].src.is_ssa &&
661 instr->dest.dest.reg.reg == instr->src[i].src.reg.reg) {
662 need_extra_copy = true;
663 temp = bld.vgrf(result.type, 4);
664 break;
665 }
666 }
667
668 for (unsigned i = 0; i < 4; i++) {
669 if (!(instr->dest.write_mask & (1 << i)))
670 continue;
671
672 if (instr->op == nir_op_imov || instr->op == nir_op_fmov) {
673 inst = bld.MOV(offset(temp, bld, i),
674 offset(op[0], bld, instr->src[0].swizzle[i]));
675 } else {
676 inst = bld.MOV(offset(temp, bld, i),
677 offset(op[i], bld, instr->src[i].swizzle[0]));
678 }
679 inst->saturate = instr->dest.saturate;
680 }
681
682 /* In this case the source and destination registers were the same,
683 * so we need to insert an extra set of moves in order to deal with
684 * any swizzling.
685 */
686 if (need_extra_copy) {
687 for (unsigned i = 0; i < 4; i++) {
688 if (!(instr->dest.write_mask & (1 << i)))
689 continue;
690
691 bld.MOV(offset(result, bld, i), offset(temp, bld, i));
692 }
693 }
694 return;
695 }
696 default:
697 break;
698 }
699
700 /* At this point, we have dealt with any instruction that operates on
701 * more than a single channel. Therefore, we can just adjust the source
702 * and destination registers for that channel and emit the instruction.
703 */
704 unsigned channel = 0;
705 if (nir_op_infos[instr->op].output_size == 0) {
706 /* Since NIR is doing the scalarizing for us, we should only ever see
707 * vectorized operations with a single channel.
708 */
709 assert(_mesa_bitcount(instr->dest.write_mask) == 1);
710 channel = ffs(instr->dest.write_mask) - 1;
711
712 result = offset(result, bld, channel);
713 }
714
715 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
716 assert(nir_op_infos[instr->op].input_sizes[i] < 2);
717 op[i] = offset(op[i], bld, instr->src[i].swizzle[channel]);
718 }
719
720 switch (instr->op) {
721 case nir_op_i2f:
722 case nir_op_u2f:
723 if (optimize_extract_to_float(instr, result))
724 return;
725
726 case nir_op_f2d:
727 case nir_op_i2d:
728 case nir_op_u2d:
729 case nir_op_d2f:
730 case nir_op_d2i:
731 case nir_op_d2u:
732 inst = bld.MOV(result, op[0]);
733 inst->saturate = instr->dest.saturate;
734 break;
735
736 case nir_op_f2i:
737 case nir_op_f2u:
738 bld.MOV(result, op[0]);
739 break;
740
741 case nir_op_fsign: {
742 if (type_sz(op[0].type) < 8) {
743 /* AND(val, 0x80000000) gives the sign bit.
744 *
745 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
746 * zero.
747 */
748 bld.CMP(bld.null_reg_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ);
749
750 fs_reg result_int = retype(result, BRW_REGISTER_TYPE_UD);
751 op[0].type = BRW_REGISTER_TYPE_UD;
752 result.type = BRW_REGISTER_TYPE_UD;
753 bld.AND(result_int, op[0], brw_imm_ud(0x80000000u));
754
755 inst = bld.OR(result_int, result_int, brw_imm_ud(0x3f800000u));
756 inst->predicate = BRW_PREDICATE_NORMAL;
757 if (instr->dest.saturate) {
758 inst = bld.MOV(result, result);
759 inst->saturate = true;
760 }
761 } else {
762 /* For doubles we do the same but we need to consider:
763 *
764 * - 2-src instructions can't operate with 64-bit immediates
765 * - The sign is encoded in the high 32-bit of each DF
766 * - CMP with DF requires special handling in SIMD16
767 * - We need to produce a DF result.
768 */
769
770 /* 2-src instructions can't have 64-bit immediates, so put 0.0 in
771 * a register and compare with that.
772 */
773 fs_reg tmp = vgrf(glsl_type::double_type);
774 bld.MOV(tmp, brw_imm_df(0.0));
775
776 /* A direct DF CMP using the flag register (null dst) won't work in
777 * SIMD16 because the CMP will be split in two by lower_simd_width,
778 * resulting in two CMP instructions with the same dst (NULL),
779 * leading to dead code elimination of the first one. In SIMD8,
780 * however, there is no need to split the CMP and we can save some
781 * work.
782 */
783 fs_reg dst_tmp = vgrf(glsl_type::double_type);
784 bld.CMP(dst_tmp, op[0], tmp, BRW_CONDITIONAL_NZ);
785
786 /* In SIMD16 we want to avoid using a NULL dst register with DF CMP,
787 * so we store the result of the comparison in a vgrf instead and
788 * then we generate a UD comparison from that that won't have to
789 * be split by lower_simd_width. This is what NIR does to handle
790 * double comparisons in the general case.
791 */
792 if (bld.dispatch_width() == 16 ) {
793 fs_reg dst_tmp_ud = retype(dst_tmp, BRW_REGISTER_TYPE_UD);
794 bld.MOV(dst_tmp_ud, subscript(dst_tmp, BRW_REGISTER_TYPE_UD, 0));
795 bld.CMP(bld.null_reg_ud(),
796 dst_tmp_ud, brw_imm_ud(0), BRW_CONDITIONAL_NZ);
797 }
798
799 /* Get the high 32-bit of each double component where the sign is */
800 fs_reg result_int = retype(result, BRW_REGISTER_TYPE_UD);
801 bld.MOV(result_int, subscript(op[0], BRW_REGISTER_TYPE_UD, 1));
802
803 /* Get the sign bit */
804 bld.AND(result_int, result_int, brw_imm_ud(0x80000000u));
805
806 /* Add 1.0 to the sign, predicated to skip the case of op[0] == 0.0 */
807 inst = bld.OR(result_int, result_int, brw_imm_ud(0x3f800000u));
808 inst->predicate = BRW_PREDICATE_NORMAL;
809
810 /* Convert from 32-bit float to 64-bit double */
811 result.type = BRW_REGISTER_TYPE_DF;
812 inst = bld.MOV(result, retype(result_int, BRW_REGISTER_TYPE_F));
813
814 if (instr->dest.saturate) {
815 inst = bld.MOV(result, result);
816 inst->saturate = true;
817 }
818 }
819 break;
820 }
821
822 case nir_op_isign:
823 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
824 * -> non-negative val generates 0x00000000.
825 * Predicated OR sets 1 if val is positive.
826 */
827 assert(nir_dest_bit_size(instr->dest.dest) < 64);
828 bld.CMP(bld.null_reg_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_G);
829 bld.ASR(result, op[0], brw_imm_d(31));
830 inst = bld.OR(result, result, brw_imm_d(1));
831 inst->predicate = BRW_PREDICATE_NORMAL;
832 break;
833
834 case nir_op_frcp:
835 inst = bld.emit(SHADER_OPCODE_RCP, result, op[0]);
836 inst->saturate = instr->dest.saturate;
837 break;
838
839 case nir_op_fexp2:
840 inst = bld.emit(SHADER_OPCODE_EXP2, result, op[0]);
841 inst->saturate = instr->dest.saturate;
842 break;
843
844 case nir_op_flog2:
845 inst = bld.emit(SHADER_OPCODE_LOG2, result, op[0]);
846 inst->saturate = instr->dest.saturate;
847 break;
848
849 case nir_op_fsin:
850 inst = bld.emit(SHADER_OPCODE_SIN, result, op[0]);
851 inst->saturate = instr->dest.saturate;
852 break;
853
854 case nir_op_fcos:
855 inst = bld.emit(SHADER_OPCODE_COS, result, op[0]);
856 inst->saturate = instr->dest.saturate;
857 break;
858
859 case nir_op_fddx:
860 if (fs_key->high_quality_derivatives) {
861 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
862 } else {
863 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
864 }
865 inst->saturate = instr->dest.saturate;
866 break;
867 case nir_op_fddx_fine:
868 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
869 inst->saturate = instr->dest.saturate;
870 break;
871 case nir_op_fddx_coarse:
872 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
873 inst->saturate = instr->dest.saturate;
874 break;
875 case nir_op_fddy:
876 if (fs_key->high_quality_derivatives) {
877 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0]);
878 } else {
879 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0]);
880 }
881 inst->saturate = instr->dest.saturate;
882 break;
883 case nir_op_fddy_fine:
884 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0]);
885 inst->saturate = instr->dest.saturate;
886 break;
887 case nir_op_fddy_coarse:
888 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0]);
889 inst->saturate = instr->dest.saturate;
890 break;
891
892 case nir_op_iadd:
893 assert(nir_dest_bit_size(instr->dest.dest) < 64);
894 case nir_op_fadd:
895 inst = bld.ADD(result, op[0], op[1]);
896 inst->saturate = instr->dest.saturate;
897 break;
898
899 case nir_op_fmul:
900 inst = bld.MUL(result, op[0], op[1]);
901 inst->saturate = instr->dest.saturate;
902 break;
903
904 case nir_op_imul:
905 assert(nir_dest_bit_size(instr->dest.dest) < 64);
906 bld.MUL(result, op[0], op[1]);
907 break;
908
909 case nir_op_imul_high:
910 case nir_op_umul_high:
911 assert(nir_dest_bit_size(instr->dest.dest) < 64);
912 bld.emit(SHADER_OPCODE_MULH, result, op[0], op[1]);
913 break;
914
915 case nir_op_idiv:
916 case nir_op_udiv:
917 assert(nir_dest_bit_size(instr->dest.dest) < 64);
918 bld.emit(SHADER_OPCODE_INT_QUOTIENT, result, op[0], op[1]);
919 break;
920
921 case nir_op_uadd_carry:
922 unreachable("Should have been lowered by carry_to_arith().");
923
924 case nir_op_usub_borrow:
925 unreachable("Should have been lowered by borrow_to_arith().");
926
927 case nir_op_umod:
928 case nir_op_irem:
929 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
930 * appears that our hardware just does the right thing for signed
931 * remainder.
932 */
933 assert(nir_dest_bit_size(instr->dest.dest) < 64);
934 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
935 break;
936
937 case nir_op_imod: {
938 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
939 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
940
941 /* Math instructions don't support conditional mod */
942 inst = bld.MOV(bld.null_reg_d(), result);
943 inst->conditional_mod = BRW_CONDITIONAL_NZ;
944
945 /* Now, we need to determine if signs of the sources are different.
946 * When we XOR the sources, the top bit is 0 if they are the same and 1
947 * if they are different. We can then use a conditional modifier to
948 * turn that into a predicate. This leads us to an XOR.l instruction.
949 *
950 * Technically, according to the PRM, you're not allowed to use .l on a
951 * XOR instruction. However, emperical experiments and Curro's reading
952 * of the simulator source both indicate that it's safe.
953 */
954 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_D);
955 inst = bld.XOR(tmp, op[0], op[1]);
956 inst->predicate = BRW_PREDICATE_NORMAL;
957 inst->conditional_mod = BRW_CONDITIONAL_L;
958
959 /* If the result of the initial remainder operation is non-zero and the
960 * two sources have different signs, add in a copy of op[1] to get the
961 * final integer modulus value.
962 */
963 inst = bld.ADD(result, result, op[1]);
964 inst->predicate = BRW_PREDICATE_NORMAL;
965 break;
966 }
967
968 case nir_op_flt:
969 case nir_op_fge:
970 case nir_op_feq:
971 case nir_op_fne: {
972 fs_reg dest = result;
973 if (nir_src_bit_size(instr->src[0].src) > 32) {
974 dest = bld.vgrf(BRW_REGISTER_TYPE_DF, 1);
975 }
976 brw_conditional_mod cond;
977 switch (instr->op) {
978 case nir_op_flt:
979 cond = BRW_CONDITIONAL_L;
980 break;
981 case nir_op_fge:
982 cond = BRW_CONDITIONAL_GE;
983 break;
984 case nir_op_feq:
985 cond = BRW_CONDITIONAL_Z;
986 break;
987 case nir_op_fne:
988 cond = BRW_CONDITIONAL_NZ;
989 break;
990 default:
991 unreachable("bad opcode");
992 }
993 bld.CMP(dest, op[0], op[1], cond);
994 if (nir_src_bit_size(instr->src[0].src) > 32) {
995 bld.MOV(result, subscript(dest, BRW_REGISTER_TYPE_UD, 0));
996 }
997 break;
998 }
999
1000 case nir_op_ilt:
1001 case nir_op_ult:
1002 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1003 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_L);
1004 break;
1005
1006 case nir_op_ige:
1007 case nir_op_uge:
1008 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1009 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_GE);
1010 break;
1011
1012 case nir_op_ieq:
1013 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1014 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_Z);
1015 break;
1016
1017 case nir_op_ine:
1018 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1019 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_NZ);
1020 break;
1021
1022 case nir_op_inot:
1023 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1024 if (devinfo->gen >= 8) {
1025 op[0] = resolve_source_modifiers(op[0]);
1026 }
1027 bld.NOT(result, op[0]);
1028 break;
1029 case nir_op_ixor:
1030 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1031 if (devinfo->gen >= 8) {
1032 op[0] = resolve_source_modifiers(op[0]);
1033 op[1] = resolve_source_modifiers(op[1]);
1034 }
1035 bld.XOR(result, op[0], op[1]);
1036 break;
1037 case nir_op_ior:
1038 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1039 if (devinfo->gen >= 8) {
1040 op[0] = resolve_source_modifiers(op[0]);
1041 op[1] = resolve_source_modifiers(op[1]);
1042 }
1043 bld.OR(result, op[0], op[1]);
1044 break;
1045 case nir_op_iand:
1046 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1047 if (devinfo->gen >= 8) {
1048 op[0] = resolve_source_modifiers(op[0]);
1049 op[1] = resolve_source_modifiers(op[1]);
1050 }
1051 bld.AND(result, op[0], op[1]);
1052 break;
1053
1054 case nir_op_fdot2:
1055 case nir_op_fdot3:
1056 case nir_op_fdot4:
1057 case nir_op_ball_fequal2:
1058 case nir_op_ball_iequal2:
1059 case nir_op_ball_fequal3:
1060 case nir_op_ball_iequal3:
1061 case nir_op_ball_fequal4:
1062 case nir_op_ball_iequal4:
1063 case nir_op_bany_fnequal2:
1064 case nir_op_bany_inequal2:
1065 case nir_op_bany_fnequal3:
1066 case nir_op_bany_inequal3:
1067 case nir_op_bany_fnequal4:
1068 case nir_op_bany_inequal4:
1069 unreachable("Lowered by nir_lower_alu_reductions");
1070
1071 case nir_op_fnoise1_1:
1072 case nir_op_fnoise1_2:
1073 case nir_op_fnoise1_3:
1074 case nir_op_fnoise1_4:
1075 case nir_op_fnoise2_1:
1076 case nir_op_fnoise2_2:
1077 case nir_op_fnoise2_3:
1078 case nir_op_fnoise2_4:
1079 case nir_op_fnoise3_1:
1080 case nir_op_fnoise3_2:
1081 case nir_op_fnoise3_3:
1082 case nir_op_fnoise3_4:
1083 case nir_op_fnoise4_1:
1084 case nir_op_fnoise4_2:
1085 case nir_op_fnoise4_3:
1086 case nir_op_fnoise4_4:
1087 unreachable("not reached: should be handled by lower_noise");
1088
1089 case nir_op_ldexp:
1090 unreachable("not reached: should be handled by ldexp_to_arith()");
1091
1092 case nir_op_fsqrt:
1093 inst = bld.emit(SHADER_OPCODE_SQRT, result, op[0]);
1094 inst->saturate = instr->dest.saturate;
1095 break;
1096
1097 case nir_op_frsq:
1098 inst = bld.emit(SHADER_OPCODE_RSQ, result, op[0]);
1099 inst->saturate = instr->dest.saturate;
1100 break;
1101
1102 case nir_op_b2i:
1103 case nir_op_b2f:
1104 bld.MOV(result, negate(op[0]));
1105 break;
1106
1107 case nir_op_f2b:
1108 bld.CMP(result, op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ);
1109 break;
1110 case nir_op_d2b: {
1111 /* two-argument instructions can't take 64-bit immediates */
1112 fs_reg zero = vgrf(glsl_type::double_type);
1113 bld.MOV(zero, brw_imm_df(0.0));
1114 /* A SIMD16 execution needs to be split in two instructions, so use
1115 * a vgrf instead of the flag register as dst so instruction splitting
1116 * works
1117 */
1118 fs_reg tmp = vgrf(glsl_type::double_type);
1119 bld.CMP(tmp, op[0], zero, BRW_CONDITIONAL_NZ);
1120 bld.MOV(result, subscript(tmp, BRW_REGISTER_TYPE_UD, 0));
1121 break;
1122 }
1123 case nir_op_i2b:
1124 bld.CMP(result, op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ);
1125 break;
1126
1127 case nir_op_ftrunc:
1128 inst = bld.RNDZ(result, op[0]);
1129 inst->saturate = instr->dest.saturate;
1130 break;
1131
1132 case nir_op_fceil: {
1133 op[0].negate = !op[0].negate;
1134 fs_reg temp = vgrf(glsl_type::float_type);
1135 bld.RNDD(temp, op[0]);
1136 temp.negate = true;
1137 inst = bld.MOV(result, temp);
1138 inst->saturate = instr->dest.saturate;
1139 break;
1140 }
1141 case nir_op_ffloor:
1142 inst = bld.RNDD(result, op[0]);
1143 inst->saturate = instr->dest.saturate;
1144 break;
1145 case nir_op_ffract:
1146 inst = bld.FRC(result, op[0]);
1147 inst->saturate = instr->dest.saturate;
1148 break;
1149 case nir_op_fround_even:
1150 inst = bld.RNDE(result, op[0]);
1151 inst->saturate = instr->dest.saturate;
1152 break;
1153
1154 case nir_op_fquantize2f16: {
1155 fs_reg tmp16 = bld.vgrf(BRW_REGISTER_TYPE_D);
1156 fs_reg tmp32 = bld.vgrf(BRW_REGISTER_TYPE_F);
1157 fs_reg zero = bld.vgrf(BRW_REGISTER_TYPE_F);
1158
1159 /* The destination stride must be at least as big as the source stride. */
1160 tmp16.type = BRW_REGISTER_TYPE_W;
1161 tmp16.stride = 2;
1162
1163 /* Check for denormal */
1164 fs_reg abs_src0 = op[0];
1165 abs_src0.abs = true;
1166 bld.CMP(bld.null_reg_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1167 BRW_CONDITIONAL_L);
1168 /* Get the appropriately signed zero */
1169 bld.AND(retype(zero, BRW_REGISTER_TYPE_UD),
1170 retype(op[0], BRW_REGISTER_TYPE_UD),
1171 brw_imm_ud(0x80000000));
1172 /* Do the actual F32 -> F16 -> F32 conversion */
1173 bld.emit(BRW_OPCODE_F32TO16, tmp16, op[0]);
1174 bld.emit(BRW_OPCODE_F16TO32, tmp32, tmp16);
1175 /* Select that or zero based on normal status */
1176 inst = bld.SEL(result, zero, tmp32);
1177 inst->predicate = BRW_PREDICATE_NORMAL;
1178 inst->saturate = instr->dest.saturate;
1179 break;
1180 }
1181
1182 case nir_op_imin:
1183 case nir_op_umin:
1184 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1185 case nir_op_fmin:
1186 inst = bld.emit_minmax(result, op[0], op[1], BRW_CONDITIONAL_L);
1187 inst->saturate = instr->dest.saturate;
1188 break;
1189
1190 case nir_op_imax:
1191 case nir_op_umax:
1192 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1193 case nir_op_fmax:
1194 inst = bld.emit_minmax(result, op[0], op[1], BRW_CONDITIONAL_GE);
1195 inst->saturate = instr->dest.saturate;
1196 break;
1197
1198 case nir_op_pack_snorm_2x16:
1199 case nir_op_pack_snorm_4x8:
1200 case nir_op_pack_unorm_2x16:
1201 case nir_op_pack_unorm_4x8:
1202 case nir_op_unpack_snorm_2x16:
1203 case nir_op_unpack_snorm_4x8:
1204 case nir_op_unpack_unorm_2x16:
1205 case nir_op_unpack_unorm_4x8:
1206 case nir_op_unpack_half_2x16:
1207 case nir_op_pack_half_2x16:
1208 unreachable("not reached: should be handled by lower_packing_builtins");
1209
1210 case nir_op_unpack_half_2x16_split_x:
1211 inst = bld.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X, result, op[0]);
1212 inst->saturate = instr->dest.saturate;
1213 break;
1214 case nir_op_unpack_half_2x16_split_y:
1215 inst = bld.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y, result, op[0]);
1216 inst->saturate = instr->dest.saturate;
1217 break;
1218
1219 case nir_op_pack_double_2x32_split:
1220 /* Optimize the common case where we are re-packing a double with
1221 * the result of a previous double unpack. In this case we can take the
1222 * 32-bit value to use in the re-pack from the original double and bypass
1223 * the unpack operation.
1224 */
1225 for (int i = 0; i < 2; i++) {
1226 if (instr->src[i].src.is_ssa)
1227 continue;
1228
1229 const nir_instr *parent_instr = instr->src[i].src.ssa->parent_instr;
1230 if (parent_instr->type == nir_instr_type_alu)
1231 continue;
1232
1233 const nir_alu_instr *alu_parent = nir_instr_as_alu(parent_instr);
1234 if (alu_parent->op == nir_op_unpack_double_2x32_split_x ||
1235 alu_parent->op == nir_op_unpack_double_2x32_split_y)
1236 continue;
1237
1238 if (!alu_parent->src[0].src.is_ssa)
1239 continue;
1240
1241 op[i] = get_nir_src(alu_parent->src[0].src);
1242 op[i] = offset(retype(op[i], BRW_REGISTER_TYPE_DF), bld,
1243 alu_parent->src[0].swizzle[channel]);
1244 if (alu_parent->op == nir_op_unpack_double_2x32_split_y)
1245 op[i] = subscript(op[i], BRW_REGISTER_TYPE_UD, 1);
1246 else
1247 op[i] = subscript(op[i], BRW_REGISTER_TYPE_UD, 0);
1248 }
1249 bld.emit(FS_OPCODE_PACK, result, op[0], op[1]);
1250 break;
1251
1252 case nir_op_unpack_double_2x32_split_x:
1253 case nir_op_unpack_double_2x32_split_y: {
1254 /* Optimize the common case where we are unpacking from a double we have
1255 * previously packed. In this case we can just bypass the pack operation
1256 * and source directly from its arguments.
1257 */
1258 unsigned index = (instr->op == nir_op_unpack_double_2x32_split_x) ? 0 : 1;
1259 if (instr->src[0].src.is_ssa) {
1260 nir_instr *parent_instr = instr->src[0].src.ssa->parent_instr;
1261 if (parent_instr->type == nir_instr_type_alu) {
1262 nir_alu_instr *alu_parent = nir_instr_as_alu(parent_instr);
1263 if (alu_parent->op == nir_op_pack_double_2x32_split &&
1264 alu_parent->src[index].src.is_ssa) {
1265 op[0] = retype(get_nir_src(alu_parent->src[index].src),
1266 BRW_REGISTER_TYPE_UD);
1267 op[0] =
1268 offset(op[0], bld, alu_parent->src[index].swizzle[channel]);
1269 bld.MOV(result, op[0]);
1270 break;
1271 }
1272 }
1273 }
1274
1275 if (instr->op == nir_op_unpack_double_2x32_split_x)
1276 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UD, 0));
1277 else
1278 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UD, 1));
1279 break;
1280 }
1281
1282 case nir_op_fpow:
1283 inst = bld.emit(SHADER_OPCODE_POW, result, op[0], op[1]);
1284 inst->saturate = instr->dest.saturate;
1285 break;
1286
1287 case nir_op_bitfield_reverse:
1288 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1289 bld.BFREV(result, op[0]);
1290 break;
1291
1292 case nir_op_bit_count:
1293 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1294 bld.CBIT(result, op[0]);
1295 break;
1296
1297 case nir_op_ufind_msb:
1298 case nir_op_ifind_msb: {
1299 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1300 bld.FBH(retype(result, BRW_REGISTER_TYPE_UD), op[0]);
1301
1302 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1303 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1304 * subtract the result from 31 to convert the MSB count into an LSB count.
1305 */
1306 bld.CMP(bld.null_reg_d(), result, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
1307
1308 inst = bld.ADD(result, result, brw_imm_d(31));
1309 inst->predicate = BRW_PREDICATE_NORMAL;
1310 inst->src[0].negate = true;
1311 break;
1312 }
1313
1314 case nir_op_find_lsb:
1315 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1316 bld.FBL(result, op[0]);
1317 break;
1318
1319 case nir_op_ubitfield_extract:
1320 case nir_op_ibitfield_extract:
1321 unreachable("should have been lowered");
1322 case nir_op_ubfe:
1323 case nir_op_ibfe:
1324 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1325 bld.BFE(result, op[2], op[1], op[0]);
1326 break;
1327 case nir_op_bfm:
1328 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1329 bld.BFI1(result, op[0], op[1]);
1330 break;
1331 case nir_op_bfi:
1332 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1333 bld.BFI2(result, op[0], op[1], op[2]);
1334 break;
1335
1336 case nir_op_bitfield_insert:
1337 unreachable("not reached: should have been lowered");
1338
1339 case nir_op_ishl:
1340 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1341 bld.SHL(result, op[0], op[1]);
1342 break;
1343 case nir_op_ishr:
1344 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1345 bld.ASR(result, op[0], op[1]);
1346 break;
1347 case nir_op_ushr:
1348 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1349 bld.SHR(result, op[0], op[1]);
1350 break;
1351
1352 case nir_op_pack_half_2x16_split:
1353 bld.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT, result, op[0], op[1]);
1354 break;
1355
1356 case nir_op_ffma:
1357 inst = bld.MAD(result, op[2], op[1], op[0]);
1358 inst->saturate = instr->dest.saturate;
1359 break;
1360
1361 case nir_op_flrp:
1362 inst = bld.LRP(result, op[0], op[1], op[2]);
1363 inst->saturate = instr->dest.saturate;
1364 break;
1365
1366 case nir_op_bcsel:
1367 if (optimize_frontfacing_ternary(instr, result))
1368 return;
1369
1370 bld.CMP(bld.null_reg_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ);
1371 inst = bld.SEL(result, op[1], op[2]);
1372 inst->predicate = BRW_PREDICATE_NORMAL;
1373 break;
1374
1375 case nir_op_extract_u8:
1376 case nir_op_extract_i8: {
1377 const brw_reg_type type = brw_int_type(1, instr->op == nir_op_extract_i8);
1378 nir_const_value *byte = nir_src_as_const_value(instr->src[1].src);
1379 assert(byte != NULL);
1380 bld.MOV(result, subscript(op[0], type, byte->u32[0]));
1381 break;
1382 }
1383
1384 case nir_op_extract_u16:
1385 case nir_op_extract_i16: {
1386 const brw_reg_type type = brw_int_type(2, instr->op == nir_op_extract_i16);
1387 nir_const_value *word = nir_src_as_const_value(instr->src[1].src);
1388 assert(word != NULL);
1389 bld.MOV(result, subscript(op[0], type, word->u32[0]));
1390 break;
1391 }
1392
1393 default:
1394 unreachable("unhandled instruction");
1395 }
1396
1397 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1398 * to sign extend the low bit to 0/~0
1399 */
1400 if (devinfo->gen <= 5 &&
1401 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1402 fs_reg masked = vgrf(glsl_type::int_type);
1403 bld.AND(masked, result, brw_imm_d(1));
1404 masked.negate = true;
1405 bld.MOV(retype(result, BRW_REGISTER_TYPE_D), masked);
1406 }
1407 }
1408
1409 void
1410 fs_visitor::nir_emit_load_const(const fs_builder &bld,
1411 nir_load_const_instr *instr)
1412 {
1413 const brw_reg_type reg_type =
1414 instr->def.bit_size == 32 ? BRW_REGISTER_TYPE_D : BRW_REGISTER_TYPE_DF;
1415 fs_reg reg = bld.vgrf(reg_type, instr->def.num_components);
1416
1417 switch (instr->def.bit_size) {
1418 case 32:
1419 for (unsigned i = 0; i < instr->def.num_components; i++)
1420 bld.MOV(offset(reg, bld, i), brw_imm_d(instr->value.i32[i]));
1421 break;
1422
1423 case 64:
1424 for (unsigned i = 0; i < instr->def.num_components; i++)
1425 bld.MOV(offset(reg, bld, i), brw_imm_df(instr->value.f64[i]));
1426 break;
1427
1428 default:
1429 unreachable("Invalid bit size");
1430 }
1431
1432 nir_ssa_values[instr->def.index] = reg;
1433 }
1434
1435 void
1436 fs_visitor::nir_emit_undef(const fs_builder &bld, nir_ssa_undef_instr *instr)
1437 {
1438 const brw_reg_type reg_type =
1439 instr->def.bit_size == 32 ? BRW_REGISTER_TYPE_D : BRW_REGISTER_TYPE_DF;
1440 nir_ssa_values[instr->def.index] =
1441 bld.vgrf(reg_type, instr->def.num_components);
1442 }
1443
1444 fs_reg
1445 fs_visitor::get_nir_src(const nir_src &src)
1446 {
1447 fs_reg reg;
1448 if (src.is_ssa) {
1449 reg = nir_ssa_values[src.ssa->index];
1450 } else {
1451 /* We don't handle indirects on locals */
1452 assert(src.reg.indirect == NULL);
1453 reg = offset(nir_locals[src.reg.reg->index], bld,
1454 src.reg.base_offset * src.reg.reg->num_components);
1455 }
1456
1457 /* to avoid floating-point denorm flushing problems, set the type by
1458 * default to D - instructions that need floating point semantics will set
1459 * this to F if they need to
1460 */
1461 return retype(reg, BRW_REGISTER_TYPE_D);
1462 }
1463
1464 /**
1465 * Return an IMM for constants; otherwise call get_nir_src() as normal.
1466 */
1467 fs_reg
1468 fs_visitor::get_nir_src_imm(const nir_src &src)
1469 {
1470 nir_const_value *val = nir_src_as_const_value(src);
1471 return val ? fs_reg(brw_imm_d(val->i32[0])) : get_nir_src(src);
1472 }
1473
1474 fs_reg
1475 fs_visitor::get_nir_dest(const nir_dest &dest)
1476 {
1477 if (dest.is_ssa) {
1478 const brw_reg_type reg_type =
1479 dest.ssa.bit_size == 32 ? BRW_REGISTER_TYPE_F : BRW_REGISTER_TYPE_DF;
1480 nir_ssa_values[dest.ssa.index] =
1481 bld.vgrf(reg_type, dest.ssa.num_components);
1482 return nir_ssa_values[dest.ssa.index];
1483 } else {
1484 /* We don't handle indirects on locals */
1485 assert(dest.reg.indirect == NULL);
1486 return offset(nir_locals[dest.reg.reg->index], bld,
1487 dest.reg.base_offset * dest.reg.reg->num_components);
1488 }
1489 }
1490
1491 fs_reg
1492 fs_visitor::get_nir_image_deref(const nir_deref_var *deref)
1493 {
1494 fs_reg image(UNIFORM, deref->var->data.driver_location / 4,
1495 BRW_REGISTER_TYPE_UD);
1496 fs_reg indirect;
1497 unsigned indirect_max = 0;
1498
1499 for (const nir_deref *tail = &deref->deref; tail->child;
1500 tail = tail->child) {
1501 const nir_deref_array *deref_array = nir_deref_as_array(tail->child);
1502 assert(tail->child->deref_type == nir_deref_type_array);
1503 const unsigned size = glsl_get_length(tail->type);
1504 const unsigned element_size = type_size_scalar(deref_array->deref.type);
1505 const unsigned base = MIN2(deref_array->base_offset, size - 1);
1506 image = offset(image, bld, base * element_size);
1507
1508 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
1509 fs_reg tmp = vgrf(glsl_type::uint_type);
1510
1511 /* Accessing an invalid surface index with the dataport can result
1512 * in a hang. According to the spec "if the index used to
1513 * select an individual element is negative or greater than or
1514 * equal to the size of the array, the results of the operation
1515 * are undefined but may not lead to termination" -- which is one
1516 * of the possible outcomes of the hang. Clamp the index to
1517 * prevent access outside of the array bounds.
1518 */
1519 bld.emit_minmax(tmp, retype(get_nir_src(deref_array->indirect),
1520 BRW_REGISTER_TYPE_UD),
1521 brw_imm_ud(size - base - 1), BRW_CONDITIONAL_L);
1522
1523 indirect_max += element_size * (tail->type->length - 1);
1524
1525 bld.MUL(tmp, tmp, brw_imm_ud(element_size * 4));
1526 if (indirect.file == BAD_FILE) {
1527 indirect = tmp;
1528 } else {
1529 bld.ADD(indirect, indirect, tmp);
1530 }
1531 }
1532 }
1533
1534 if (indirect.file == BAD_FILE) {
1535 return image;
1536 } else {
1537 /* Emit a pile of MOVs to load the uniform into a temporary. The
1538 * dead-code elimination pass will get rid of what we don't use.
1539 */
1540 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, BRW_IMAGE_PARAM_SIZE);
1541 for (unsigned j = 0; j < BRW_IMAGE_PARAM_SIZE; j++) {
1542 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
1543 offset(tmp, bld, j), offset(image, bld, j),
1544 indirect, brw_imm_ud((indirect_max + 1) * 4));
1545 }
1546 return tmp;
1547 }
1548 }
1549
1550 void
1551 fs_visitor::emit_percomp(const fs_builder &bld, const fs_inst &inst,
1552 unsigned wr_mask)
1553 {
1554 for (unsigned i = 0; i < 4; i++) {
1555 if (!((wr_mask >> i) & 1))
1556 continue;
1557
1558 fs_inst *new_inst = new(mem_ctx) fs_inst(inst);
1559 new_inst->dst = offset(new_inst->dst, bld, i);
1560 for (unsigned j = 0; j < new_inst->sources; j++)
1561 if (new_inst->src[j].file == VGRF)
1562 new_inst->src[j] = offset(new_inst->src[j], bld, i);
1563
1564 bld.emit(new_inst);
1565 }
1566 }
1567
1568 /**
1569 * Get the matching channel register datatype for an image intrinsic of the
1570 * specified GLSL image type.
1571 */
1572 static brw_reg_type
1573 get_image_base_type(const glsl_type *type)
1574 {
1575 switch ((glsl_base_type)type->sampled_type) {
1576 case GLSL_TYPE_UINT:
1577 return BRW_REGISTER_TYPE_UD;
1578 case GLSL_TYPE_INT:
1579 return BRW_REGISTER_TYPE_D;
1580 case GLSL_TYPE_FLOAT:
1581 return BRW_REGISTER_TYPE_F;
1582 default:
1583 unreachable("Not reached.");
1584 }
1585 }
1586
1587 /**
1588 * Get the appropriate atomic op for an image atomic intrinsic.
1589 */
1590 static unsigned
1591 get_image_atomic_op(nir_intrinsic_op op, const glsl_type *type)
1592 {
1593 switch (op) {
1594 case nir_intrinsic_image_atomic_add:
1595 return BRW_AOP_ADD;
1596 case nir_intrinsic_image_atomic_min:
1597 return (get_image_base_type(type) == BRW_REGISTER_TYPE_D ?
1598 BRW_AOP_IMIN : BRW_AOP_UMIN);
1599 case nir_intrinsic_image_atomic_max:
1600 return (get_image_base_type(type) == BRW_REGISTER_TYPE_D ?
1601 BRW_AOP_IMAX : BRW_AOP_UMAX);
1602 case nir_intrinsic_image_atomic_and:
1603 return BRW_AOP_AND;
1604 case nir_intrinsic_image_atomic_or:
1605 return BRW_AOP_OR;
1606 case nir_intrinsic_image_atomic_xor:
1607 return BRW_AOP_XOR;
1608 case nir_intrinsic_image_atomic_exchange:
1609 return BRW_AOP_MOV;
1610 case nir_intrinsic_image_atomic_comp_swap:
1611 return BRW_AOP_CMPWR;
1612 default:
1613 unreachable("Not reachable.");
1614 }
1615 }
1616
1617 static fs_inst *
1618 emit_pixel_interpolater_send(const fs_builder &bld,
1619 enum opcode opcode,
1620 const fs_reg &dst,
1621 const fs_reg &src,
1622 const fs_reg &desc,
1623 glsl_interp_qualifier interpolation)
1624 {
1625 fs_inst *inst;
1626 fs_reg payload;
1627 int mlen;
1628
1629 if (src.file == BAD_FILE) {
1630 /* Dummy payload */
1631 payload = bld.vgrf(BRW_REGISTER_TYPE_F, 1);
1632 mlen = 1;
1633 } else {
1634 payload = src;
1635 mlen = 2 * bld.dispatch_width() / 8;
1636 }
1637
1638 inst = bld.emit(opcode, dst, payload, desc);
1639 inst->mlen = mlen;
1640 /* 2 floats per slot returned */
1641 inst->regs_written = 2 * bld.dispatch_width() / 8;
1642 inst->pi_noperspective = interpolation == INTERP_QUALIFIER_NOPERSPECTIVE;
1643
1644 return inst;
1645 }
1646
1647 /**
1648 * Computes 1 << x, given a D/UD register containing some value x.
1649 */
1650 static fs_reg
1651 intexp2(const fs_builder &bld, const fs_reg &x)
1652 {
1653 assert(x.type == BRW_REGISTER_TYPE_UD || x.type == BRW_REGISTER_TYPE_D);
1654
1655 fs_reg result = bld.vgrf(x.type, 1);
1656 fs_reg one = bld.vgrf(x.type, 1);
1657
1658 bld.MOV(one, retype(brw_imm_d(1), one.type));
1659 bld.SHL(result, one, x);
1660 return result;
1661 }
1662
1663 void
1664 fs_visitor::emit_gs_end_primitive(const nir_src &vertex_count_nir_src)
1665 {
1666 assert(stage == MESA_SHADER_GEOMETRY);
1667
1668 struct brw_gs_prog_data *gs_prog_data =
1669 (struct brw_gs_prog_data *) prog_data;
1670
1671 /* We can only do EndPrimitive() functionality when the control data
1672 * consists of cut bits. Fortunately, the only time it isn't is when the
1673 * output type is points, in which case EndPrimitive() is a no-op.
1674 */
1675 if (gs_prog_data->control_data_format !=
1676 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
1677 return;
1678 }
1679
1680 /* Cut bits use one bit per vertex. */
1681 assert(gs_compile->control_data_bits_per_vertex == 1);
1682
1683 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
1684 vertex_count.type = BRW_REGISTER_TYPE_UD;
1685
1686 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
1687 * vertex n, 0 otherwise. So all we need to do here is mark bit
1688 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
1689 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
1690 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
1691 *
1692 * Note that if EndPrimitive() is called before emitting any vertices, this
1693 * will cause us to set bit 31 of the control_data_bits register to 1.
1694 * That's fine because:
1695 *
1696 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
1697 * output, so the hardware will ignore cut bit 31.
1698 *
1699 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
1700 * last vertex, so setting cut bit 31 has no effect (since the primitive
1701 * is automatically ended when the GS terminates).
1702 *
1703 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
1704 * control_data_bits register to 0 when the first vertex is emitted.
1705 */
1706
1707 const fs_builder abld = bld.annotate("end primitive");
1708
1709 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
1710 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1711 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
1712 fs_reg mask = intexp2(abld, prev_count);
1713 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1714 * attention to the lower 5 bits of its second source argument, so on this
1715 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
1716 * ((vertex_count - 1) % 32).
1717 */
1718 abld.OR(this->control_data_bits, this->control_data_bits, mask);
1719 }
1720
1721 void
1722 fs_visitor::emit_gs_control_data_bits(const fs_reg &vertex_count)
1723 {
1724 assert(stage == MESA_SHADER_GEOMETRY);
1725 assert(gs_compile->control_data_bits_per_vertex != 0);
1726
1727 struct brw_gs_prog_data *gs_prog_data =
1728 (struct brw_gs_prog_data *) prog_data;
1729
1730 const fs_builder abld = bld.annotate("emit control data bits");
1731 const fs_builder fwa_bld = bld.exec_all();
1732
1733 /* We use a single UD register to accumulate control data bits (32 bits
1734 * for each of the SIMD8 channels). So we need to write a DWord (32 bits)
1735 * at a time.
1736 *
1737 * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
1738 * We have select a 128-bit group via the Global and Per-Slot Offsets, then
1739 * use the Channel Mask phase to enable/disable which DWord within that
1740 * group to write. (Remember, different SIMD8 channels may have emitted
1741 * different numbers of vertices, so we may need per-slot offsets.)
1742 *
1743 * Channel masking presents an annoying problem: we may have to replicate
1744 * the data up to 4 times:
1745 *
1746 * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
1747 *
1748 * To avoid penalizing shaders that emit a small number of vertices, we
1749 * can avoid these sometimes: if the size of the control data header is
1750 * <= 128 bits, then there is only 1 OWord. All SIMD8 channels will land
1751 * land in the same 128-bit group, so we can skip per-slot offsets.
1752 *
1753 * Similarly, if the control data header is <= 32 bits, there is only one
1754 * DWord, so we can skip channel masks.
1755 */
1756 enum opcode opcode = SHADER_OPCODE_URB_WRITE_SIMD8;
1757
1758 fs_reg channel_mask, per_slot_offset;
1759
1760 if (gs_compile->control_data_header_size_bits > 32) {
1761 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
1762 channel_mask = vgrf(glsl_type::uint_type);
1763 }
1764
1765 if (gs_compile->control_data_header_size_bits > 128) {
1766 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT;
1767 per_slot_offset = vgrf(glsl_type::uint_type);
1768 }
1769
1770 /* Figure out which DWord we're trying to write to using the formula:
1771 *
1772 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
1773 *
1774 * Since bits_per_vertex is a power of two, and is known at compile
1775 * time, this can be optimized to:
1776 *
1777 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
1778 */
1779 if (opcode != SHADER_OPCODE_URB_WRITE_SIMD8) {
1780 fs_reg dword_index = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1781 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1782 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
1783 unsigned log2_bits_per_vertex =
1784 _mesa_fls(gs_compile->control_data_bits_per_vertex);
1785 abld.SHR(dword_index, prev_count, brw_imm_ud(6u - log2_bits_per_vertex));
1786
1787 if (per_slot_offset.file != BAD_FILE) {
1788 /* Set the per-slot offset to dword_index / 4, so that we'll write to
1789 * the appropriate OWord within the control data header.
1790 */
1791 abld.SHR(per_slot_offset, dword_index, brw_imm_ud(2u));
1792 }
1793
1794 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
1795 * write to the appropriate DWORD within the OWORD.
1796 */
1797 fs_reg channel = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1798 fwa_bld.AND(channel, dword_index, brw_imm_ud(3u));
1799 channel_mask = intexp2(fwa_bld, channel);
1800 /* Then the channel masks need to be in bits 23:16. */
1801 fwa_bld.SHL(channel_mask, channel_mask, brw_imm_ud(16u));
1802 }
1803
1804 /* Store the control data bits in the message payload and send it. */
1805 int mlen = 2;
1806 if (channel_mask.file != BAD_FILE)
1807 mlen += 4; /* channel masks, plus 3 extra copies of the data */
1808 if (per_slot_offset.file != BAD_FILE)
1809 mlen++;
1810
1811 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
1812 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, mlen);
1813 int i = 0;
1814 sources[i++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
1815 if (per_slot_offset.file != BAD_FILE)
1816 sources[i++] = per_slot_offset;
1817 if (channel_mask.file != BAD_FILE)
1818 sources[i++] = channel_mask;
1819 while (i < mlen) {
1820 sources[i++] = this->control_data_bits;
1821 }
1822
1823 abld.LOAD_PAYLOAD(payload, sources, mlen, mlen);
1824 fs_inst *inst = abld.emit(opcode, reg_undef, payload);
1825 inst->mlen = mlen;
1826 /* We need to increment Global Offset by 256-bits to make room for
1827 * Broadwell's extra "Vertex Count" payload at the beginning of the
1828 * URB entry. Since this is an OWord message, Global Offset is counted
1829 * in 128-bit units, so we must set it to 2.
1830 */
1831 if (gs_prog_data->static_vertex_count == -1)
1832 inst->offset = 2;
1833 }
1834
1835 void
1836 fs_visitor::set_gs_stream_control_data_bits(const fs_reg &vertex_count,
1837 unsigned stream_id)
1838 {
1839 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
1840
1841 /* Note: we are calling this *before* increasing vertex_count, so
1842 * this->vertex_count == vertex_count - 1 in the formula above.
1843 */
1844
1845 /* Stream mode uses 2 bits per vertex */
1846 assert(gs_compile->control_data_bits_per_vertex == 2);
1847
1848 /* Must be a valid stream */
1849 assert(stream_id >= 0 && stream_id < MAX_VERTEX_STREAMS);
1850
1851 /* Control data bits are initialized to 0 so we don't have to set any
1852 * bits when sending vertices to stream 0.
1853 */
1854 if (stream_id == 0)
1855 return;
1856
1857 const fs_builder abld = bld.annotate("set stream control data bits", NULL);
1858
1859 /* reg::sid = stream_id */
1860 fs_reg sid = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1861 abld.MOV(sid, brw_imm_ud(stream_id));
1862
1863 /* reg:shift_count = 2 * (vertex_count - 1) */
1864 fs_reg shift_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1865 abld.SHL(shift_count, vertex_count, brw_imm_ud(1u));
1866
1867 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1868 * attention to the lower 5 bits of its second source argument, so on this
1869 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
1870 * stream_id << ((2 * (vertex_count - 1)) % 32).
1871 */
1872 fs_reg mask = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1873 abld.SHL(mask, sid, shift_count);
1874 abld.OR(this->control_data_bits, this->control_data_bits, mask);
1875 }
1876
1877 void
1878 fs_visitor::emit_gs_vertex(const nir_src &vertex_count_nir_src,
1879 unsigned stream_id)
1880 {
1881 assert(stage == MESA_SHADER_GEOMETRY);
1882
1883 struct brw_gs_prog_data *gs_prog_data =
1884 (struct brw_gs_prog_data *) prog_data;
1885
1886 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
1887 vertex_count.type = BRW_REGISTER_TYPE_UD;
1888
1889 /* Haswell and later hardware ignores the "Render Stream Select" bits
1890 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
1891 * and instead sends all primitives down the pipeline for rasterization.
1892 * If the SOL stage is enabled, "Render Stream Select" is honored and
1893 * primitives bound to non-zero streams are discarded after stream output.
1894 *
1895 * Since the only purpose of primives sent to non-zero streams is to
1896 * be recorded by transform feedback, we can simply discard all geometry
1897 * bound to these streams when transform feedback is disabled.
1898 */
1899 if (stream_id > 0 && !nir->info.has_transform_feedback_varyings)
1900 return;
1901
1902 /* If we're outputting 32 control data bits or less, then we can wait
1903 * until the shader is over to output them all. Otherwise we need to
1904 * output them as we go. Now is the time to do it, since we're about to
1905 * output the vertex_count'th vertex, so it's guaranteed that the
1906 * control data bits associated with the (vertex_count - 1)th vertex are
1907 * correct.
1908 */
1909 if (gs_compile->control_data_header_size_bits > 32) {
1910 const fs_builder abld =
1911 bld.annotate("emit vertex: emit control data bits");
1912
1913 /* Only emit control data bits if we've finished accumulating a batch
1914 * of 32 bits. This is the case when:
1915 *
1916 * (vertex_count * bits_per_vertex) % 32 == 0
1917 *
1918 * (in other words, when the last 5 bits of vertex_count *
1919 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
1920 * integer n (which is always the case, since bits_per_vertex is
1921 * always 1 or 2), this is equivalent to requiring that the last 5-n
1922 * bits of vertex_count are 0:
1923 *
1924 * vertex_count & (2^(5-n) - 1) == 0
1925 *
1926 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
1927 * equivalent to:
1928 *
1929 * vertex_count & (32 / bits_per_vertex - 1) == 0
1930 *
1931 * TODO: If vertex_count is an immediate, we could do some of this math
1932 * at compile time...
1933 */
1934 fs_inst *inst =
1935 abld.AND(bld.null_reg_d(), vertex_count,
1936 brw_imm_ud(32u / gs_compile->control_data_bits_per_vertex - 1u));
1937 inst->conditional_mod = BRW_CONDITIONAL_Z;
1938
1939 abld.IF(BRW_PREDICATE_NORMAL);
1940 /* If vertex_count is 0, then no control data bits have been
1941 * accumulated yet, so we can skip emitting them.
1942 */
1943 abld.CMP(bld.null_reg_d(), vertex_count, brw_imm_ud(0u),
1944 BRW_CONDITIONAL_NEQ);
1945 abld.IF(BRW_PREDICATE_NORMAL);
1946 emit_gs_control_data_bits(vertex_count);
1947 abld.emit(BRW_OPCODE_ENDIF);
1948
1949 /* Reset control_data_bits to 0 so we can start accumulating a new
1950 * batch.
1951 *
1952 * Note: in the case where vertex_count == 0, this neutralizes the
1953 * effect of any call to EndPrimitive() that the shader may have
1954 * made before outputting its first vertex.
1955 */
1956 inst = abld.MOV(this->control_data_bits, brw_imm_ud(0u));
1957 inst->force_writemask_all = true;
1958 abld.emit(BRW_OPCODE_ENDIF);
1959 }
1960
1961 emit_urb_writes(vertex_count);
1962
1963 /* In stream mode we have to set control data bits for all vertices
1964 * unless we have disabled control data bits completely (which we do
1965 * do for GL_POINTS outputs that don't use streams).
1966 */
1967 if (gs_compile->control_data_header_size_bits > 0 &&
1968 gs_prog_data->control_data_format ==
1969 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
1970 set_gs_stream_control_data_bits(vertex_count, stream_id);
1971 }
1972 }
1973
1974 void
1975 fs_visitor::emit_gs_input_load(const fs_reg &dst,
1976 const nir_src &vertex_src,
1977 unsigned base_offset,
1978 const nir_src &offset_src,
1979 unsigned num_components)
1980 {
1981 struct brw_gs_prog_data *gs_prog_data = (struct brw_gs_prog_data *) prog_data;
1982
1983 nir_const_value *vertex_const = nir_src_as_const_value(vertex_src);
1984 nir_const_value *offset_const = nir_src_as_const_value(offset_src);
1985 const unsigned push_reg_count = gs_prog_data->base.urb_read_length * 8;
1986
1987 /* Offset 0 is the VUE header, which contains VARYING_SLOT_LAYER [.y],
1988 * VARYING_SLOT_VIEWPORT [.z], and VARYING_SLOT_PSIZ [.w]. Only
1989 * gl_PointSize is available as a GS input, however, so it must be that.
1990 */
1991 const bool is_point_size = (base_offset == 0);
1992
1993 /* TODO: figure out push input layout for invocations == 1 */
1994 if (gs_prog_data->invocations == 1 &&
1995 offset_const != NULL && vertex_const != NULL &&
1996 4 * (base_offset + offset_const->u32[0]) < push_reg_count) {
1997 int imm_offset = (base_offset + offset_const->u32[0]) * 4 +
1998 vertex_const->u32[0] * push_reg_count;
1999 /* This input was pushed into registers. */
2000 if (is_point_size) {
2001 /* gl_PointSize comes in .w */
2002 bld.MOV(dst, fs_reg(ATTR, imm_offset + 3, dst.type));
2003 } else {
2004 for (unsigned i = 0; i < num_components; i++) {
2005 bld.MOV(offset(dst, bld, i),
2006 fs_reg(ATTR, imm_offset + i, dst.type));
2007 }
2008 }
2009 return;
2010 }
2011
2012 /* Resort to the pull model. Ensure the VUE handles are provided. */
2013 gs_prog_data->base.include_vue_handles = true;
2014
2015 unsigned first_icp_handle = gs_prog_data->include_primitive_id ? 3 : 2;
2016 fs_reg icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2017
2018 if (gs_prog_data->invocations == 1) {
2019 if (vertex_const) {
2020 /* The vertex index is constant; just select the proper URB handle. */
2021 icp_handle =
2022 retype(brw_vec8_grf(first_icp_handle + vertex_const->i32[0], 0),
2023 BRW_REGISTER_TYPE_UD);
2024 } else {
2025 /* The vertex index is non-constant. We need to use indirect
2026 * addressing to fetch the proper URB handle.
2027 *
2028 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
2029 * indicating that channel <n> should read the handle from
2030 * DWord <n>. We convert that to bytes by multiplying by 4.
2031 *
2032 * Next, we convert the vertex index to bytes by multiplying
2033 * by 32 (shifting by 5), and add the two together. This is
2034 * the final indirect byte offset.
2035 */
2036 fs_reg sequence = bld.vgrf(BRW_REGISTER_TYPE_W, 1);
2037 fs_reg channel_offsets = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2038 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2039 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2040
2041 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
2042 bld.MOV(sequence, fs_reg(brw_imm_v(0x76543210)));
2043 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
2044 bld.SHL(channel_offsets, sequence, brw_imm_ud(2u));
2045 /* Convert vertex_index to bytes (multiply by 32) */
2046 bld.SHL(vertex_offset_bytes,
2047 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2048 brw_imm_ud(5u));
2049 bld.ADD(icp_offset_bytes, vertex_offset_bytes, channel_offsets);
2050
2051 /* Use first_icp_handle as the base offset. There is one register
2052 * of URB handles per vertex, so inform the register allocator that
2053 * we might read up to nir->info.gs.vertices_in registers.
2054 */
2055 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2056 fs_reg(brw_vec8_grf(first_icp_handle, 0)),
2057 fs_reg(icp_offset_bytes),
2058 brw_imm_ud(nir->info.gs.vertices_in * REG_SIZE));
2059 }
2060 } else {
2061 assert(gs_prog_data->invocations > 1);
2062
2063 if (vertex_const) {
2064 assert(devinfo->gen >= 9 || vertex_const->i32[0] <= 5);
2065 bld.MOV(icp_handle,
2066 retype(brw_vec1_grf(first_icp_handle +
2067 vertex_const->i32[0] / 8,
2068 vertex_const->i32[0] % 8),
2069 BRW_REGISTER_TYPE_UD));
2070 } else {
2071 /* The vertex index is non-constant. We need to use indirect
2072 * addressing to fetch the proper URB handle.
2073 *
2074 */
2075 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2076
2077 /* Convert vertex_index to bytes (multiply by 4) */
2078 bld.SHL(icp_offset_bytes,
2079 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2080 brw_imm_ud(2u));
2081
2082 /* Use first_icp_handle as the base offset. There is one DWord
2083 * of URB handles per vertex, so inform the register allocator that
2084 * we might read up to ceil(nir->info.gs.vertices_in / 8) registers.
2085 */
2086 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2087 fs_reg(brw_vec8_grf(first_icp_handle, 0)),
2088 fs_reg(icp_offset_bytes),
2089 brw_imm_ud(DIV_ROUND_UP(nir->info.gs.vertices_in, 8) *
2090 REG_SIZE));
2091 }
2092 }
2093
2094 fs_inst *inst;
2095 if (offset_const) {
2096 /* Constant indexing - use global offset. */
2097 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
2098 inst->offset = base_offset + offset_const->u32[0];
2099 inst->base_mrf = -1;
2100 inst->mlen = 1;
2101 inst->regs_written = num_components;
2102 } else {
2103 /* Indirect indexing - use per-slot offsets as well. */
2104 const fs_reg srcs[] = { icp_handle, get_nir_src(offset_src) };
2105 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2106 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2107
2108 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst, payload);
2109 inst->offset = base_offset;
2110 inst->base_mrf = -1;
2111 inst->mlen = 2;
2112 inst->regs_written = num_components;
2113 }
2114
2115 if (is_point_size) {
2116 /* Read the whole VUE header (because of alignment) and read .w. */
2117 fs_reg tmp = bld.vgrf(dst.type, 4);
2118 inst->dst = tmp;
2119 inst->regs_written = 4;
2120 bld.MOV(dst, offset(tmp, bld, 3));
2121 }
2122 }
2123
2124 fs_reg
2125 fs_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
2126 {
2127 nir_src *offset_src = nir_get_io_offset_src(instr);
2128 nir_const_value *const_value = nir_src_as_const_value(*offset_src);
2129
2130 if (const_value) {
2131 /* The only constant offset we should find is 0. brw_nir.c's
2132 * add_const_offset_to_base() will fold other constant offsets
2133 * into instr->const_index[0].
2134 */
2135 assert(const_value->u32[0] == 0);
2136 return fs_reg();
2137 }
2138
2139 return get_nir_src(*offset_src);
2140 }
2141
2142 static void
2143 do_untyped_vector_read(const fs_builder &bld,
2144 const fs_reg dest,
2145 const fs_reg surf_index,
2146 const fs_reg offset_reg,
2147 unsigned num_components)
2148 {
2149 if (type_sz(dest.type) == 4) {
2150 fs_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
2151 1 /* dims */,
2152 num_components,
2153 BRW_PREDICATE_NONE);
2154 read_result.type = dest.type;
2155 for (unsigned i = 0; i < num_components; i++)
2156 bld.MOV(offset(dest, bld, i), offset(read_result, bld, i));
2157 } else if (type_sz(dest.type) == 8) {
2158 /* Reading a dvec, so we need to:
2159 *
2160 * 1. Multiply num_components by 2, to account for the fact that we
2161 * need to read 64-bit components.
2162 * 2. Shuffle the result of the load to form valid 64-bit elements
2163 * 3. Emit a second load (for components z/w) if needed.
2164 */
2165 fs_reg read_offset = bld.vgrf(BRW_REGISTER_TYPE_UD);
2166 bld.MOV(read_offset, offset_reg);
2167
2168 int iters = num_components <= 2 ? 1 : 2;
2169
2170 /* Load the dvec, the first iteration loads components x/y, the second
2171 * iteration, if needed, loads components z/w
2172 */
2173 for (int it = 0; it < iters; it++) {
2174 /* Compute number of components to read in this iteration */
2175 int iter_components = MIN2(2, num_components);
2176 num_components -= iter_components;
2177
2178 /* Read. Since this message reads 32-bit components, we need to
2179 * read twice as many components.
2180 */
2181 fs_reg read_result = emit_untyped_read(bld, surf_index, read_offset,
2182 1 /* dims */,
2183 iter_components * 2,
2184 BRW_PREDICATE_NONE);
2185
2186 /* Shuffle the 32-bit load result into valid 64-bit data */
2187 const fs_reg packed_result = bld.vgrf(dest.type, iter_components);
2188 shuffle_32bit_load_result_to_64bit_data(
2189 bld, packed_result, read_result, iter_components);
2190
2191 /* Move each component to its destination */
2192 read_result = retype(read_result, BRW_REGISTER_TYPE_DF);
2193 for (int c = 0; c < iter_components; c++) {
2194 bld.MOV(offset(dest, bld, it * 2 + c),
2195 offset(packed_result, bld, c));
2196 }
2197
2198 bld.ADD(read_offset, read_offset, brw_imm_ud(16));
2199 }
2200 } else {
2201 unreachable("Unsupported type");
2202 }
2203 }
2204
2205 void
2206 fs_visitor::nir_emit_vs_intrinsic(const fs_builder &bld,
2207 nir_intrinsic_instr *instr)
2208 {
2209 assert(stage == MESA_SHADER_VERTEX);
2210
2211 fs_reg dest;
2212 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2213 dest = get_nir_dest(instr->dest);
2214
2215 switch (instr->intrinsic) {
2216 case nir_intrinsic_load_vertex_id:
2217 unreachable("should be lowered by lower_vertex_id()");
2218
2219 case nir_intrinsic_load_vertex_id_zero_base:
2220 case nir_intrinsic_load_base_vertex:
2221 case nir_intrinsic_load_instance_id:
2222 case nir_intrinsic_load_base_instance:
2223 case nir_intrinsic_load_draw_id: {
2224 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
2225 fs_reg val = nir_system_values[sv];
2226 assert(val.file != BAD_FILE);
2227 dest.type = val.type;
2228 bld.MOV(dest, val);
2229 break;
2230 }
2231
2232 default:
2233 nir_emit_intrinsic(bld, instr);
2234 break;
2235 }
2236 }
2237
2238 void
2239 fs_visitor::nir_emit_tcs_intrinsic(const fs_builder &bld,
2240 nir_intrinsic_instr *instr)
2241 {
2242 assert(stage == MESA_SHADER_TESS_CTRL);
2243 struct brw_tcs_prog_key *tcs_key = (struct brw_tcs_prog_key *) key;
2244 struct brw_tcs_prog_data *tcs_prog_data =
2245 (struct brw_tcs_prog_data *) prog_data;
2246
2247 fs_reg dst;
2248 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2249 dst = get_nir_dest(instr->dest);
2250
2251 switch (instr->intrinsic) {
2252 case nir_intrinsic_load_primitive_id:
2253 bld.MOV(dst, fs_reg(brw_vec1_grf(0, 1)));
2254 break;
2255 case nir_intrinsic_load_invocation_id:
2256 bld.MOV(retype(dst, invocation_id.type), invocation_id);
2257 break;
2258 case nir_intrinsic_load_patch_vertices_in:
2259 bld.MOV(retype(dst, BRW_REGISTER_TYPE_D),
2260 brw_imm_d(tcs_key->input_vertices));
2261 break;
2262
2263 case nir_intrinsic_barrier: {
2264 if (tcs_prog_data->instances == 1)
2265 break;
2266
2267 fs_reg m0 = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2268 fs_reg m0_2 = byte_offset(m0, 2 * sizeof(uint32_t));
2269
2270 const fs_builder fwa_bld = bld.exec_all();
2271
2272 /* Zero the message header */
2273 fwa_bld.MOV(m0, brw_imm_ud(0u));
2274
2275 /* Copy "Barrier ID" from r0.2, bits 16:13 */
2276 fwa_bld.AND(m0_2, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD),
2277 brw_imm_ud(INTEL_MASK(16, 13)));
2278
2279 /* Shift it up to bits 27:24. */
2280 fwa_bld.SHL(m0_2, m0_2, brw_imm_ud(11));
2281
2282 /* Set the Barrier Count and the enable bit */
2283 fwa_bld.OR(m0_2, m0_2,
2284 brw_imm_ud(tcs_prog_data->instances << 8 | (1 << 15)));
2285
2286 bld.emit(SHADER_OPCODE_BARRIER, bld.null_reg_ud(), m0);
2287 break;
2288 }
2289
2290 case nir_intrinsic_load_input:
2291 unreachable("nir_lower_io should never give us these.");
2292 break;
2293
2294 case nir_intrinsic_load_per_vertex_input: {
2295 fs_reg indirect_offset = get_indirect_offset(instr);
2296 unsigned imm_offset = instr->const_index[0];
2297
2298 const nir_src &vertex_src = instr->src[0];
2299 nir_const_value *vertex_const = nir_src_as_const_value(vertex_src);
2300
2301 fs_inst *inst;
2302
2303 fs_reg icp_handle;
2304
2305 if (vertex_const) {
2306 /* Emit a MOV to resolve <0,1,0> regioning. */
2307 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2308 bld.MOV(icp_handle,
2309 retype(brw_vec1_grf(1 + (vertex_const->i32[0] >> 3),
2310 vertex_const->i32[0] & 7),
2311 BRW_REGISTER_TYPE_UD));
2312 } else if (tcs_prog_data->instances == 1 &&
2313 vertex_src.is_ssa &&
2314 vertex_src.ssa->parent_instr->type == nir_instr_type_intrinsic &&
2315 nir_instr_as_intrinsic(vertex_src.ssa->parent_instr)->intrinsic == nir_intrinsic_load_invocation_id) {
2316 /* For the common case of only 1 instance, an array index of
2317 * gl_InvocationID means reading g1. Skip all the indirect work.
2318 */
2319 icp_handle = retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD);
2320 } else {
2321 /* The vertex index is non-constant. We need to use indirect
2322 * addressing to fetch the proper URB handle.
2323 */
2324 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2325
2326 /* Each ICP handle is a single DWord (4 bytes) */
2327 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2328 bld.SHL(vertex_offset_bytes,
2329 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2330 brw_imm_ud(2u));
2331
2332 /* Start at g1. We might read up to 4 registers. */
2333 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2334 fs_reg(brw_vec8_grf(1, 0)), vertex_offset_bytes,
2335 brw_imm_ud(4 * REG_SIZE));
2336 }
2337
2338 /* We can only read two double components with each URB read, so
2339 * we send two read messages in that case, each one loading up to
2340 * two double components.
2341 */
2342 unsigned num_iterations = 1;
2343 unsigned num_components = instr->num_components;
2344 fs_reg orig_dst = dst;
2345 if (type_sz(dst.type) == 8) {
2346 if (instr->num_components > 2) {
2347 num_iterations = 2;
2348 num_components = 2;
2349 }
2350
2351 fs_reg tmp = fs_reg(VGRF, alloc.allocate(4), dst.type);
2352 dst = tmp;
2353 }
2354
2355 for (unsigned iter = 0; iter < num_iterations; iter++) {
2356 if (indirect_offset.file == BAD_FILE) {
2357 /* Constant indexing - use global offset. */
2358 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
2359 inst->offset = imm_offset;
2360 inst->mlen = 1;
2361 inst->base_mrf = -1;
2362 } else {
2363 /* Indirect indexing - use per-slot offsets as well. */
2364 const fs_reg srcs[] = { icp_handle, indirect_offset };
2365 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2366 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2367
2368 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst, payload);
2369 inst->offset = imm_offset;
2370 inst->base_mrf = -1;
2371 inst->mlen = 2;
2372 }
2373 inst->regs_written = num_components * type_sz(dst.type) / 4;
2374
2375 /* If we are reading 64-bit data using 32-bit read messages we need
2376 * build proper 64-bit data elements by shuffling the low and high
2377 * 32-bit components around like we do for other things like UBOs
2378 * or SSBOs.
2379 */
2380 if (type_sz(dst.type) == 8) {
2381 shuffle_32bit_load_result_to_64bit_data(
2382 bld, dst, retype(dst, BRW_REGISTER_TYPE_F), num_components);
2383
2384 for (unsigned c = 0; c < num_components; c++) {
2385 bld.MOV(offset(orig_dst, bld, iter * 2 + c),
2386 offset(dst, bld, c));
2387 }
2388 }
2389
2390 /* Copy the temporary to the destination to deal with writemasking.
2391 *
2392 * Also attempt to deal with gl_PointSize being in the .w component.
2393 */
2394 if (inst->offset == 0 && indirect_offset.file == BAD_FILE) {
2395 assert(type_sz(dst.type) < 8);
2396 inst->dst = bld.vgrf(dst.type, 4);
2397 inst->regs_written = 4;
2398 bld.MOV(dst, offset(inst->dst, bld, 3));
2399 }
2400
2401 /* If we are loading double data and we need a second read message
2402 * adjust the write offset
2403 */
2404 if (num_iterations > 1) {
2405 num_components = instr->num_components - 2;
2406 if (indirect_offset.file == BAD_FILE) {
2407 imm_offset++;
2408 } else {
2409 fs_reg new_indirect = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2410 bld.ADD(new_indirect, indirect_offset, brw_imm_ud(1u));
2411 indirect_offset = new_indirect;
2412 }
2413 }
2414 }
2415 break;
2416 }
2417
2418 case nir_intrinsic_load_output:
2419 case nir_intrinsic_load_per_vertex_output: {
2420 fs_reg indirect_offset = get_indirect_offset(instr);
2421 unsigned imm_offset = instr->const_index[0];
2422
2423 fs_inst *inst;
2424 if (indirect_offset.file == BAD_FILE) {
2425 /* Replicate the patch handle to all enabled channels */
2426 fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2427 bld.MOV(patch_handle,
2428 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD));
2429
2430 if (imm_offset == 0) {
2431 /* This is a read of gl_TessLevelInner[], which lives in the
2432 * Patch URB header. The layout depends on the domain.
2433 */
2434 dst.type = BRW_REGISTER_TYPE_F;
2435 switch (tcs_key->tes_primitive_mode) {
2436 case GL_QUADS: {
2437 /* DWords 3-2 (reversed) */
2438 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 4);
2439
2440 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, patch_handle);
2441 inst->offset = 0;
2442 inst->mlen = 1;
2443 inst->base_mrf = -1;
2444 inst->regs_written = 4;
2445
2446 /* dst.xy = tmp.wz */
2447 bld.MOV(dst, offset(tmp, bld, 3));
2448 bld.MOV(offset(dst, bld, 1), offset(tmp, bld, 2));
2449 break;
2450 }
2451 case GL_TRIANGLES:
2452 /* DWord 4; hardcode offset = 1 and regs_written = 1 */
2453 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, patch_handle);
2454 inst->offset = 1;
2455 inst->mlen = 1;
2456 inst->base_mrf = -1;
2457 inst->regs_written = 1;
2458 break;
2459 case GL_ISOLINES:
2460 /* All channels are undefined. */
2461 break;
2462 default:
2463 unreachable("Bogus tessellation domain");
2464 }
2465 } else if (imm_offset == 1) {
2466 /* This is a read of gl_TessLevelOuter[], which lives in the
2467 * Patch URB header. The layout depends on the domain.
2468 */
2469 dst.type = BRW_REGISTER_TYPE_F;
2470
2471 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 4);
2472 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, patch_handle);
2473 inst->offset = 1;
2474 inst->mlen = 1;
2475 inst->base_mrf = -1;
2476 inst->regs_written = 4;
2477
2478 /* Reswizzle: WZYX */
2479 fs_reg srcs[4] = {
2480 offset(tmp, bld, 3),
2481 offset(tmp, bld, 2),
2482 offset(tmp, bld, 1),
2483 offset(tmp, bld, 0),
2484 };
2485
2486 unsigned num_components;
2487 switch (tcs_key->tes_primitive_mode) {
2488 case GL_QUADS:
2489 num_components = 4;
2490 break;
2491 case GL_TRIANGLES:
2492 num_components = 3;
2493 break;
2494 case GL_ISOLINES:
2495 /* Isolines are not reversed; swizzle .zw -> .xy */
2496 srcs[0] = offset(tmp, bld, 2);
2497 srcs[1] = offset(tmp, bld, 3);
2498 num_components = 2;
2499 break;
2500 default:
2501 unreachable("Bogus tessellation domain");
2502 }
2503 bld.LOAD_PAYLOAD(dst, srcs, num_components, 0);
2504 } else {
2505 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, patch_handle);
2506 inst->offset = imm_offset;
2507 inst->mlen = 1;
2508 inst->base_mrf = -1;
2509 inst->regs_written = instr->num_components;
2510 }
2511 } else {
2512 /* Indirect indexing - use per-slot offsets as well. */
2513 const fs_reg srcs[] = {
2514 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD),
2515 indirect_offset
2516 };
2517 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2518 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2519
2520 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst, payload);
2521 inst->offset = imm_offset;
2522 inst->mlen = 2;
2523 inst->base_mrf = -1;
2524 inst->regs_written = instr->num_components;
2525 }
2526 break;
2527 }
2528
2529 case nir_intrinsic_store_output:
2530 case nir_intrinsic_store_per_vertex_output: {
2531 fs_reg value = get_nir_src(instr->src[0]);
2532 bool is_64bit = (instr->src[0].is_ssa ?
2533 instr->src[0].ssa->bit_size : instr->src[0].reg.reg->bit_size) == 64;
2534 fs_reg indirect_offset = get_indirect_offset(instr);
2535 unsigned imm_offset = instr->const_index[0];
2536 unsigned swiz = BRW_SWIZZLE_XYZW;
2537 unsigned mask = instr->const_index[1];
2538 unsigned header_regs = 0;
2539 fs_reg srcs[7];
2540 srcs[header_regs++] = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD);
2541
2542 if (indirect_offset.file != BAD_FILE) {
2543 srcs[header_regs++] = indirect_offset;
2544 } else if (!is_passthrough_shader) {
2545 if (imm_offset == 0) {
2546 value.type = BRW_REGISTER_TYPE_F;
2547
2548 mask &= (1 << tesslevel_inner_components(tcs_key->tes_primitive_mode)) - 1;
2549
2550 /* This is a write to gl_TessLevelInner[], which lives in the
2551 * Patch URB header. The layout depends on the domain.
2552 */
2553 switch (tcs_key->tes_primitive_mode) {
2554 case GL_QUADS:
2555 /* gl_TessLevelInner[].xy lives at DWords 3-2 (reversed).
2556 * We use an XXYX swizzle to reverse put .xy in the .wz
2557 * channels, and use a .zw writemask.
2558 */
2559 mask = writemask_for_backwards_vector(mask);
2560 swiz = BRW_SWIZZLE4(0, 0, 1, 0);
2561 break;
2562 case GL_TRIANGLES:
2563 /* gl_TessLevelInner[].x lives at DWord 4, so we set the
2564 * writemask to X and bump the URB offset by 1.
2565 */
2566 imm_offset = 1;
2567 break;
2568 case GL_ISOLINES:
2569 /* Skip; gl_TessLevelInner[] doesn't exist for isolines. */
2570 return;
2571 default:
2572 unreachable("Bogus tessellation domain");
2573 }
2574 } else if (imm_offset == 1) {
2575 /* This is a write to gl_TessLevelOuter[] which lives in the
2576 * Patch URB Header at DWords 4-7. However, it's reversed, so
2577 * instead of .xyzw we have .wzyx.
2578 */
2579 value.type = BRW_REGISTER_TYPE_F;
2580
2581 mask &= (1 << tesslevel_outer_components(tcs_key->tes_primitive_mode)) - 1;
2582
2583 if (tcs_key->tes_primitive_mode == GL_ISOLINES) {
2584 /* Isolines .xy should be stored in .zw, in order. */
2585 swiz = BRW_SWIZZLE4(0, 0, 0, 1);
2586 mask <<= 2;
2587 } else {
2588 /* Other domains are reversed; store .wzyx instead of .xyzw */
2589 swiz = BRW_SWIZZLE_WZYX;
2590 mask = writemask_for_backwards_vector(mask);
2591 }
2592 }
2593 }
2594
2595 if (mask == 0)
2596 break;
2597
2598 unsigned num_components = _mesa_fls(mask);
2599 enum opcode opcode;
2600
2601 /* We can only pack two 64-bit components in a single message, so send
2602 * 2 messages if we have more components
2603 */
2604 unsigned num_iterations = 1;
2605 unsigned iter_components = num_components;
2606 if (is_64bit && instr->num_components > 2) {
2607 num_iterations = 2;
2608 iter_components = 2;
2609 }
2610
2611 /* 64-bit data needs to me shuffled before we can write it to the URB.
2612 * We will use this temporary to shuffle the components in each
2613 * iteration.
2614 */
2615 fs_reg tmp =
2616 fs_reg(VGRF, alloc.allocate(2 * iter_components), value.type);
2617
2618 for (unsigned iter = 0; iter < num_iterations; iter++) {
2619 if (!is_64bit && mask != WRITEMASK_XYZW) {
2620 srcs[header_regs++] = brw_imm_ud(mask << 16);
2621 opcode = indirect_offset.file != BAD_FILE ?
2622 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT :
2623 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
2624 } else if (is_64bit && ((mask & WRITEMASK_XY) != WRITEMASK_XY)) {
2625 /* Expand the 64-bit mask to 32-bit channels. We only handle
2626 * two channels in each iteration, so we only care about X/Y.
2627 */
2628 unsigned mask32 = 0;
2629 if (mask & WRITEMASK_X)
2630 mask32 |= WRITEMASK_XY;
2631 if (mask & WRITEMASK_Y)
2632 mask32 |= WRITEMASK_ZW;
2633
2634 /* If the mask does not include any of the channels X or Y there
2635 * is nothing to do in this iteration. Move on to the next couple
2636 * of 64-bit channels.
2637 */
2638 if (!mask32) {
2639 mask >>= 2;
2640 imm_offset++;
2641 continue;
2642 }
2643
2644 srcs[header_regs++] = brw_imm_ud(mask32 << 16);
2645 opcode = indirect_offset.file != BAD_FILE ?
2646 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT :
2647 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
2648 } else {
2649 opcode = indirect_offset.file != BAD_FILE ?
2650 SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT :
2651 SHADER_OPCODE_URB_WRITE_SIMD8;
2652 }
2653
2654 for (unsigned i = 0; i < iter_components; i++) {
2655 if (!(mask & (1 << i)))
2656 continue;
2657
2658 if (!is_64bit) {
2659 srcs[header_regs + i] = offset(value, bld, BRW_GET_SWZ(swiz, i));
2660 } else {
2661 /* We need to shuffle the 64-bit data to match the layout
2662 * expected by our 32-bit URB write messages. We use a temporary
2663 * for that.
2664 */
2665 unsigned channel = BRW_GET_SWZ(swiz, iter * 2 + i);
2666 shuffle_64bit_data_for_32bit_write(bld,
2667 retype(offset(tmp, bld, 2 * i), BRW_REGISTER_TYPE_F),
2668 retype(offset(value, bld, 2 * channel), BRW_REGISTER_TYPE_DF),
2669 1);
2670
2671 /* Now copy the data to the destination */
2672 fs_reg dest = fs_reg(VGRF, alloc.allocate(2), value.type);
2673 unsigned idx = 2 * i;
2674 bld.MOV(dest, offset(tmp, bld, idx));
2675 bld.MOV(offset(dest, bld, 1), offset(tmp, bld, idx + 1));
2676 srcs[header_regs + idx] = dest;
2677 srcs[header_regs + idx + 1] = offset(dest, bld, 1);
2678 }
2679 }
2680
2681 unsigned mlen =
2682 header_regs + (is_64bit ? 2 * iter_components : iter_components);
2683 fs_reg payload =
2684 bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
2685 bld.LOAD_PAYLOAD(payload, srcs, mlen, header_regs);
2686
2687 fs_inst *inst = bld.emit(opcode, bld.null_reg_ud(), payload);
2688 inst->offset = imm_offset;
2689 inst->mlen = mlen;
2690 inst->base_mrf = -1;
2691
2692 /* If this is a 64-bit attribute, select the next two 64-bit channels
2693 * to be handled in the next iteration.
2694 */
2695 if (is_64bit) {
2696 mask >>= 2;
2697 imm_offset++;
2698 }
2699 }
2700 break;
2701 }
2702
2703 default:
2704 nir_emit_intrinsic(bld, instr);
2705 break;
2706 }
2707 }
2708
2709 void
2710 fs_visitor::nir_emit_tes_intrinsic(const fs_builder &bld,
2711 nir_intrinsic_instr *instr)
2712 {
2713 assert(stage == MESA_SHADER_TESS_EVAL);
2714 struct brw_tes_prog_data *tes_prog_data = (struct brw_tes_prog_data *) prog_data;
2715
2716 fs_reg dest;
2717 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2718 dest = get_nir_dest(instr->dest);
2719
2720 switch (instr->intrinsic) {
2721 case nir_intrinsic_load_primitive_id:
2722 bld.MOV(dest, fs_reg(brw_vec1_grf(0, 1)));
2723 break;
2724 case nir_intrinsic_load_tess_coord:
2725 /* gl_TessCoord is part of the payload in g1-3 */
2726 for (unsigned i = 0; i < 3; i++) {
2727 bld.MOV(offset(dest, bld, i), fs_reg(brw_vec8_grf(1 + i, 0)));
2728 }
2729 break;
2730
2731 case nir_intrinsic_load_tess_level_outer:
2732 /* When the TES reads gl_TessLevelOuter, we ensure that the patch header
2733 * appears as a push-model input. So, we can simply use the ATTR file
2734 * rather than issuing URB read messages. The data is stored in the
2735 * high DWords in reverse order - DWord 7 contains .x, DWord 6 contains
2736 * .y, and so on.
2737 */
2738 switch (tes_prog_data->domain) {
2739 case BRW_TESS_DOMAIN_QUAD:
2740 for (unsigned i = 0; i < 4; i++)
2741 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 7 - i));
2742 break;
2743 case BRW_TESS_DOMAIN_TRI:
2744 for (unsigned i = 0; i < 3; i++)
2745 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 7 - i));
2746 break;
2747 case BRW_TESS_DOMAIN_ISOLINE:
2748 for (unsigned i = 0; i < 2; i++)
2749 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 7 - i));
2750 break;
2751 }
2752 break;
2753
2754 case nir_intrinsic_load_tess_level_inner:
2755 /* When the TES reads gl_TessLevelInner, we ensure that the patch header
2756 * appears as a push-model input. So, we can simply use the ATTR file
2757 * rather than issuing URB read messages.
2758 */
2759 switch (tes_prog_data->domain) {
2760 case BRW_TESS_DOMAIN_QUAD:
2761 bld.MOV(dest, component(fs_reg(ATTR, 0), 3));
2762 bld.MOV(offset(dest, bld, 1), component(fs_reg(ATTR, 0), 2));
2763 break;
2764 case BRW_TESS_DOMAIN_TRI:
2765 bld.MOV(dest, component(fs_reg(ATTR, 0), 4));
2766 break;
2767 case BRW_TESS_DOMAIN_ISOLINE:
2768 /* ignore - value is undefined */
2769 break;
2770 }
2771 break;
2772
2773 case nir_intrinsic_load_input:
2774 case nir_intrinsic_load_per_vertex_input: {
2775 fs_reg indirect_offset = get_indirect_offset(instr);
2776 unsigned imm_offset = instr->const_index[0];
2777
2778 fs_inst *inst;
2779 if (indirect_offset.file == BAD_FILE) {
2780 /* Arbitrarily only push up to 32 vec4 slots worth of data,
2781 * which is 16 registers (since each holds 2 vec4 slots).
2782 */
2783 const unsigned max_push_slots = 32;
2784 if (imm_offset < max_push_slots) {
2785 fs_reg src = fs_reg(ATTR, imm_offset / 2, dest.type);
2786 for (int i = 0; i < instr->num_components; i++) {
2787 unsigned comp = 16 / type_sz(dest.type) * (imm_offset % 2) + i;
2788 bld.MOV(offset(dest, bld, i), component(src, comp));
2789 }
2790 tes_prog_data->base.urb_read_length =
2791 MAX2(tes_prog_data->base.urb_read_length,
2792 DIV_ROUND_UP(imm_offset + 1, 2));
2793 } else {
2794 /* Replicate the patch handle to all enabled channels */
2795 const fs_reg srcs[] = {
2796 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD)
2797 };
2798 fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2799 bld.LOAD_PAYLOAD(patch_handle, srcs, ARRAY_SIZE(srcs), 0);
2800
2801 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dest, patch_handle);
2802 inst->mlen = 1;
2803 inst->offset = imm_offset;
2804 inst->base_mrf = -1;
2805 inst->regs_written = instr->num_components;
2806 }
2807 } else {
2808 /* Indirect indexing - use per-slot offsets as well. */
2809 const fs_reg srcs[] = {
2810 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD),
2811 indirect_offset
2812 };
2813 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2814 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2815
2816 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dest, payload);
2817 inst->mlen = 2;
2818 inst->offset = imm_offset;
2819 inst->base_mrf = -1;
2820 inst->regs_written = instr->num_components;
2821 }
2822 break;
2823 }
2824 default:
2825 nir_emit_intrinsic(bld, instr);
2826 break;
2827 }
2828 }
2829
2830 void
2831 fs_visitor::nir_emit_gs_intrinsic(const fs_builder &bld,
2832 nir_intrinsic_instr *instr)
2833 {
2834 assert(stage == MESA_SHADER_GEOMETRY);
2835 fs_reg indirect_offset;
2836
2837 fs_reg dest;
2838 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2839 dest = get_nir_dest(instr->dest);
2840
2841 switch (instr->intrinsic) {
2842 case nir_intrinsic_load_primitive_id:
2843 assert(stage == MESA_SHADER_GEOMETRY);
2844 assert(((struct brw_gs_prog_data *)prog_data)->include_primitive_id);
2845 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
2846 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD));
2847 break;
2848
2849 case nir_intrinsic_load_input:
2850 unreachable("load_input intrinsics are invalid for the GS stage");
2851
2852 case nir_intrinsic_load_per_vertex_input:
2853 emit_gs_input_load(dest, instr->src[0], instr->const_index[0],
2854 instr->src[1], instr->num_components);
2855 break;
2856
2857 case nir_intrinsic_emit_vertex_with_counter:
2858 emit_gs_vertex(instr->src[0], instr->const_index[0]);
2859 break;
2860
2861 case nir_intrinsic_end_primitive_with_counter:
2862 emit_gs_end_primitive(instr->src[0]);
2863 break;
2864
2865 case nir_intrinsic_set_vertex_count:
2866 bld.MOV(this->final_gs_vertex_count, get_nir_src(instr->src[0]));
2867 break;
2868
2869 case nir_intrinsic_load_invocation_id: {
2870 fs_reg val = nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
2871 assert(val.file != BAD_FILE);
2872 dest.type = val.type;
2873 bld.MOV(dest, val);
2874 break;
2875 }
2876
2877 default:
2878 nir_emit_intrinsic(bld, instr);
2879 break;
2880 }
2881 }
2882
2883 void
2884 fs_visitor::nir_emit_fs_intrinsic(const fs_builder &bld,
2885 nir_intrinsic_instr *instr)
2886 {
2887 assert(stage == MESA_SHADER_FRAGMENT);
2888 struct brw_wm_prog_data *wm_prog_data =
2889 (struct brw_wm_prog_data *) prog_data;
2890 const struct brw_wm_prog_key *wm_key = (const struct brw_wm_prog_key *) key;
2891
2892 fs_reg dest;
2893 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2894 dest = get_nir_dest(instr->dest);
2895
2896 switch (instr->intrinsic) {
2897 case nir_intrinsic_load_front_face:
2898 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D),
2899 *emit_frontfacing_interpolation());
2900 break;
2901
2902 case nir_intrinsic_load_sample_pos: {
2903 fs_reg sample_pos = nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
2904 assert(sample_pos.file != BAD_FILE);
2905 dest.type = sample_pos.type;
2906 bld.MOV(dest, sample_pos);
2907 bld.MOV(offset(dest, bld, 1), offset(sample_pos, bld, 1));
2908 break;
2909 }
2910
2911 case nir_intrinsic_load_helper_invocation:
2912 case nir_intrinsic_load_sample_mask_in:
2913 case nir_intrinsic_load_sample_id: {
2914 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
2915 fs_reg val = nir_system_values[sv];
2916 assert(val.file != BAD_FILE);
2917 dest.type = val.type;
2918 bld.MOV(dest, val);
2919 break;
2920 }
2921
2922 case nir_intrinsic_discard:
2923 case nir_intrinsic_discard_if: {
2924 /* We track our discarded pixels in f0.1. By predicating on it, we can
2925 * update just the flag bits that aren't yet discarded. If there's no
2926 * condition, we emit a CMP of g0 != g0, so all currently executing
2927 * channels will get turned off.
2928 */
2929 fs_inst *cmp;
2930 if (instr->intrinsic == nir_intrinsic_discard_if) {
2931 cmp = bld.CMP(bld.null_reg_f(), get_nir_src(instr->src[0]),
2932 brw_imm_d(0), BRW_CONDITIONAL_Z);
2933 } else {
2934 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
2935 BRW_REGISTER_TYPE_UW));
2936 cmp = bld.CMP(bld.null_reg_f(), some_reg, some_reg, BRW_CONDITIONAL_NZ);
2937 }
2938 cmp->predicate = BRW_PREDICATE_NORMAL;
2939 cmp->flag_subreg = 1;
2940
2941 if (devinfo->gen >= 6) {
2942 emit_discard_jump();
2943 }
2944 break;
2945 }
2946
2947 case nir_intrinsic_interp_var_at_centroid:
2948 case nir_intrinsic_interp_var_at_sample:
2949 case nir_intrinsic_interp_var_at_offset: {
2950 /* Handle ARB_gpu_shader5 interpolation intrinsics
2951 *
2952 * It's worth a quick word of explanation as to why we handle the full
2953 * variable-based interpolation intrinsic rather than a lowered version
2954 * with like we do for other inputs. We have to do that because the way
2955 * we set up inputs doesn't allow us to use the already setup inputs for
2956 * interpolation. At the beginning of the shader, we go through all of
2957 * the input variables and do the initial interpolation and put it in
2958 * the nir_inputs array based on its location as determined in
2959 * nir_lower_io. If the input isn't used, dead code cleans up and
2960 * everything works fine. However, when we get to the ARB_gpu_shader5
2961 * interpolation intrinsics, we need to reinterpolate the input
2962 * differently. If we used an intrinsic that just had an index it would
2963 * only give us the offset into the nir_inputs array. However, this is
2964 * useless because that value is post-interpolation and we need
2965 * pre-interpolation. In order to get the actual location of the bits
2966 * we get from the vertex fetching hardware, we need the variable.
2967 */
2968 wm_prog_data->pulls_bary = true;
2969
2970 fs_reg dst_xy = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
2971 const glsl_interp_qualifier interpolation =
2972 (glsl_interp_qualifier) instr->variables[0]->var->data.interpolation;
2973
2974 switch (instr->intrinsic) {
2975 case nir_intrinsic_interp_var_at_centroid:
2976 emit_pixel_interpolater_send(bld,
2977 FS_OPCODE_INTERPOLATE_AT_CENTROID,
2978 dst_xy,
2979 fs_reg(), /* src */
2980 brw_imm_ud(0u),
2981 interpolation);
2982 break;
2983
2984 case nir_intrinsic_interp_var_at_sample: {
2985 if (!wm_key->multisample_fbo) {
2986 /* From the ARB_gpu_shader5 specification:
2987 * "If multisample buffers are not available, the input varying
2988 * will be evaluated at the center of the pixel."
2989 */
2990 emit_pixel_interpolater_send(bld,
2991 FS_OPCODE_INTERPOLATE_AT_CENTROID,
2992 dst_xy,
2993 fs_reg(), /* src */
2994 brw_imm_ud(0u),
2995 interpolation);
2996 break;
2997 }
2998
2999 nir_const_value *const_sample = nir_src_as_const_value(instr->src[0]);
3000
3001 if (const_sample) {
3002 unsigned msg_data = const_sample->i32[0] << 4;
3003
3004 emit_pixel_interpolater_send(bld,
3005 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3006 dst_xy,
3007 fs_reg(), /* src */
3008 brw_imm_ud(msg_data),
3009 interpolation);
3010 } else {
3011 const fs_reg sample_src = retype(get_nir_src(instr->src[0]),
3012 BRW_REGISTER_TYPE_UD);
3013
3014 if (nir_src_is_dynamically_uniform(instr->src[0])) {
3015 const fs_reg sample_id = bld.emit_uniformize(sample_src);
3016 const fs_reg msg_data = vgrf(glsl_type::uint_type);
3017 bld.exec_all().group(1, 0)
3018 .SHL(msg_data, sample_id, brw_imm_ud(4u));
3019 emit_pixel_interpolater_send(bld,
3020 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3021 dst_xy,
3022 fs_reg(), /* src */
3023 msg_data,
3024 interpolation);
3025 } else {
3026 /* Make a loop that sends a message to the pixel interpolater
3027 * for the sample number in each live channel. If there are
3028 * multiple channels with the same sample number then these
3029 * will be handled simultaneously with a single interation of
3030 * the loop.
3031 */
3032 bld.emit(BRW_OPCODE_DO);
3033
3034 /* Get the next live sample number into sample_id_reg */
3035 const fs_reg sample_id = bld.emit_uniformize(sample_src);
3036
3037 /* Set the flag register so that we can perform the send
3038 * message on all channels that have the same sample number
3039 */
3040 bld.CMP(bld.null_reg_ud(),
3041 sample_src, sample_id,
3042 BRW_CONDITIONAL_EQ);
3043 const fs_reg msg_data = vgrf(glsl_type::uint_type);
3044 bld.exec_all().group(1, 0)
3045 .SHL(msg_data, sample_id, brw_imm_ud(4u));
3046 fs_inst *inst =
3047 emit_pixel_interpolater_send(bld,
3048 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3049 dst_xy,
3050 fs_reg(), /* src */
3051 msg_data,
3052 interpolation);
3053 set_predicate(BRW_PREDICATE_NORMAL, inst);
3054
3055 /* Continue the loop if there are any live channels left */
3056 set_predicate_inv(BRW_PREDICATE_NORMAL,
3057 true, /* inverse */
3058 bld.emit(BRW_OPCODE_WHILE));
3059 }
3060 }
3061
3062 break;
3063 }
3064
3065 case nir_intrinsic_interp_var_at_offset: {
3066 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3067
3068 if (const_offset) {
3069 unsigned off_x = MIN2((int)(const_offset->f32[0] * 16), 7) & 0xf;
3070 unsigned off_y = MIN2((int)(const_offset->f32[1] * 16), 7) & 0xf;
3071
3072 emit_pixel_interpolater_send(bld,
3073 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET,
3074 dst_xy,
3075 fs_reg(), /* src */
3076 brw_imm_ud(off_x | (off_y << 4)),
3077 interpolation);
3078 } else {
3079 fs_reg src = vgrf(glsl_type::ivec2_type);
3080 fs_reg offset_src = retype(get_nir_src(instr->src[0]),
3081 BRW_REGISTER_TYPE_F);
3082 for (int i = 0; i < 2; i++) {
3083 fs_reg temp = vgrf(glsl_type::float_type);
3084 bld.MUL(temp, offset(offset_src, bld, i), brw_imm_f(16.0f));
3085 fs_reg itemp = vgrf(glsl_type::int_type);
3086 /* float to int */
3087 bld.MOV(itemp, temp);
3088
3089 /* Clamp the upper end of the range to +7/16.
3090 * ARB_gpu_shader5 requires that we support a maximum offset
3091 * of +0.5, which isn't representable in a S0.4 value -- if
3092 * we didn't clamp it, we'd end up with -8/16, which is the
3093 * opposite of what the shader author wanted.
3094 *
3095 * This is legal due to ARB_gpu_shader5's quantization
3096 * rules:
3097 *
3098 * "Not all values of <offset> may be supported; x and y
3099 * offsets may be rounded to fixed-point values with the
3100 * number of fraction bits given by the
3101 * implementation-dependent constant
3102 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
3103 */
3104 set_condmod(BRW_CONDITIONAL_L,
3105 bld.SEL(offset(src, bld, i), itemp, brw_imm_d(7)));
3106 }
3107
3108 const enum opcode opcode = FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET;
3109 emit_pixel_interpolater_send(bld,
3110 opcode,
3111 dst_xy,
3112 src,
3113 brw_imm_ud(0u),
3114 interpolation);
3115 }
3116 break;
3117 }
3118
3119 default:
3120 unreachable("Invalid intrinsic");
3121 }
3122
3123 for (unsigned j = 0; j < instr->num_components; j++) {
3124 fs_reg src = interp_reg(instr->variables[0]->var->data.location, j);
3125 src.type = dest.type;
3126
3127 bld.emit(FS_OPCODE_LINTERP, dest, dst_xy, src);
3128 dest = offset(dest, bld, 1);
3129 }
3130 break;
3131 }
3132 default:
3133 nir_emit_intrinsic(bld, instr);
3134 break;
3135 }
3136 }
3137
3138 void
3139 fs_visitor::nir_emit_cs_intrinsic(const fs_builder &bld,
3140 nir_intrinsic_instr *instr)
3141 {
3142 assert(stage == MESA_SHADER_COMPUTE);
3143 struct brw_cs_prog_data *cs_prog_data =
3144 (struct brw_cs_prog_data *) prog_data;
3145
3146 fs_reg dest;
3147 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3148 dest = get_nir_dest(instr->dest);
3149
3150 switch (instr->intrinsic) {
3151 case nir_intrinsic_barrier:
3152 emit_barrier();
3153 cs_prog_data->uses_barrier = true;
3154 break;
3155
3156 case nir_intrinsic_load_local_invocation_id:
3157 case nir_intrinsic_load_work_group_id: {
3158 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
3159 fs_reg val = nir_system_values[sv];
3160 assert(val.file != BAD_FILE);
3161 dest.type = val.type;
3162 for (unsigned i = 0; i < 3; i++)
3163 bld.MOV(offset(dest, bld, i), offset(val, bld, i));
3164 break;
3165 }
3166
3167 case nir_intrinsic_load_num_work_groups: {
3168 const unsigned surface =
3169 cs_prog_data->binding_table.work_groups_start;
3170
3171 cs_prog_data->uses_num_work_groups = true;
3172
3173 fs_reg surf_index = brw_imm_ud(surface);
3174 brw_mark_surface_used(prog_data, surface);
3175
3176 /* Read the 3 GLuint components of gl_NumWorkGroups */
3177 for (unsigned i = 0; i < 3; i++) {
3178 fs_reg read_result =
3179 emit_untyped_read(bld, surf_index,
3180 brw_imm_ud(i << 2),
3181 1 /* dims */, 1 /* size */,
3182 BRW_PREDICATE_NONE);
3183 read_result.type = dest.type;
3184 bld.MOV(dest, read_result);
3185 dest = offset(dest, bld, 1);
3186 }
3187 break;
3188 }
3189
3190 case nir_intrinsic_shared_atomic_add:
3191 nir_emit_shared_atomic(bld, BRW_AOP_ADD, instr);
3192 break;
3193 case nir_intrinsic_shared_atomic_imin:
3194 nir_emit_shared_atomic(bld, BRW_AOP_IMIN, instr);
3195 break;
3196 case nir_intrinsic_shared_atomic_umin:
3197 nir_emit_shared_atomic(bld, BRW_AOP_UMIN, instr);
3198 break;
3199 case nir_intrinsic_shared_atomic_imax:
3200 nir_emit_shared_atomic(bld, BRW_AOP_IMAX, instr);
3201 break;
3202 case nir_intrinsic_shared_atomic_umax:
3203 nir_emit_shared_atomic(bld, BRW_AOP_UMAX, instr);
3204 break;
3205 case nir_intrinsic_shared_atomic_and:
3206 nir_emit_shared_atomic(bld, BRW_AOP_AND, instr);
3207 break;
3208 case nir_intrinsic_shared_atomic_or:
3209 nir_emit_shared_atomic(bld, BRW_AOP_OR, instr);
3210 break;
3211 case nir_intrinsic_shared_atomic_xor:
3212 nir_emit_shared_atomic(bld, BRW_AOP_XOR, instr);
3213 break;
3214 case nir_intrinsic_shared_atomic_exchange:
3215 nir_emit_shared_atomic(bld, BRW_AOP_MOV, instr);
3216 break;
3217 case nir_intrinsic_shared_atomic_comp_swap:
3218 nir_emit_shared_atomic(bld, BRW_AOP_CMPWR, instr);
3219 break;
3220
3221 case nir_intrinsic_load_shared: {
3222 assert(devinfo->gen >= 7);
3223
3224 fs_reg surf_index = brw_imm_ud(GEN7_BTI_SLM);
3225
3226 /* Get the offset to read from */
3227 fs_reg offset_reg;
3228 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3229 if (const_offset) {
3230 offset_reg = brw_imm_ud(instr->const_index[0] + const_offset->u32[0]);
3231 } else {
3232 offset_reg = vgrf(glsl_type::uint_type);
3233 bld.ADD(offset_reg,
3234 retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_UD),
3235 brw_imm_ud(instr->const_index[0]));
3236 }
3237
3238 /* Read the vector */
3239 do_untyped_vector_read(bld, dest, surf_index, offset_reg,
3240 instr->num_components);
3241 break;
3242 }
3243
3244 case nir_intrinsic_store_shared: {
3245 assert(devinfo->gen >= 7);
3246
3247 /* Block index */
3248 fs_reg surf_index = brw_imm_ud(GEN7_BTI_SLM);
3249
3250 /* Value */
3251 fs_reg val_reg = get_nir_src(instr->src[0]);
3252
3253 /* Writemask */
3254 unsigned writemask = instr->const_index[1];
3255
3256 /* get_nir_src() retypes to integer. Be wary of 64-bit types though
3257 * since the untyped writes below operate in units of 32-bits, which
3258 * means that we need to write twice as many components each time.
3259 * Also, we have to suffle 64-bit data to be in the appropriate layout
3260 * expected by our 32-bit write messages.
3261 */
3262 unsigned type_size = 4;
3263 unsigned bit_size = instr->src[0].is_ssa ?
3264 instr->src[0].ssa->bit_size : instr->src[0].reg.reg->bit_size;
3265 if (bit_size == 64) {
3266 type_size = 8;
3267 fs_reg tmp =
3268 fs_reg(VGRF, alloc.allocate(alloc.sizes[val_reg.nr]), val_reg.type);
3269 shuffle_64bit_data_for_32bit_write(
3270 bld,
3271 retype(tmp, BRW_REGISTER_TYPE_F),
3272 retype(val_reg, BRW_REGISTER_TYPE_DF),
3273 instr->num_components);
3274 val_reg = tmp;
3275 }
3276
3277 unsigned type_slots = type_size / 4;
3278
3279 /* Combine groups of consecutive enabled channels in one write
3280 * message. We use ffs to find the first enabled channel and then ffs on
3281 * the bit-inverse, down-shifted writemask to determine the length of
3282 * the block of enabled bits.
3283 */
3284 while (writemask) {
3285 unsigned first_component = ffs(writemask) - 1;
3286 unsigned length = ffs(~(writemask >> first_component)) - 1;
3287
3288 /* We can't write more than 2 64-bit components at once. Limit the
3289 * length of the write to what we can do and let the next iteration
3290 * handle the rest
3291 */
3292 if (type_size > 4)
3293 length = MIN2(2, length);
3294
3295 fs_reg offset_reg;
3296 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
3297 if (const_offset) {
3298 offset_reg = brw_imm_ud(instr->const_index[0] + const_offset->u32[0] +
3299 type_size * first_component);
3300 } else {
3301 offset_reg = vgrf(glsl_type::uint_type);
3302 bld.ADD(offset_reg,
3303 retype(get_nir_src(instr->src[1]), BRW_REGISTER_TYPE_UD),
3304 brw_imm_ud(instr->const_index[0] + type_size * first_component));
3305 }
3306
3307 emit_untyped_write(bld, surf_index, offset_reg,
3308 offset(val_reg, bld, first_component * type_slots),
3309 1 /* dims */, length * type_slots,
3310 BRW_PREDICATE_NONE);
3311
3312 /* Clear the bits in the writemask that we just wrote, then try
3313 * again to see if more channels are left.
3314 */
3315 writemask &= (15 << (first_component + length));
3316 }
3317
3318 break;
3319 }
3320
3321 default:
3322 nir_emit_intrinsic(bld, instr);
3323 break;
3324 }
3325 }
3326
3327 void
3328 fs_visitor::nir_emit_intrinsic(const fs_builder &bld, nir_intrinsic_instr *instr)
3329 {
3330 fs_reg dest;
3331 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3332 dest = get_nir_dest(instr->dest);
3333
3334 switch (instr->intrinsic) {
3335 case nir_intrinsic_atomic_counter_inc:
3336 case nir_intrinsic_atomic_counter_dec:
3337 case nir_intrinsic_atomic_counter_read: {
3338 /* Get the arguments of the atomic intrinsic. */
3339 const fs_reg offset = get_nir_src(instr->src[0]);
3340 const unsigned surface = (stage_prog_data->binding_table.abo_start +
3341 instr->const_index[0]);
3342 fs_reg tmp;
3343
3344 /* Emit a surface read or atomic op. */
3345 switch (instr->intrinsic) {
3346 case nir_intrinsic_atomic_counter_read:
3347 tmp = emit_untyped_read(bld, brw_imm_ud(surface), offset, 1, 1);
3348 break;
3349
3350 case nir_intrinsic_atomic_counter_inc:
3351 tmp = emit_untyped_atomic(bld, brw_imm_ud(surface), offset, fs_reg(),
3352 fs_reg(), 1, 1, BRW_AOP_INC);
3353 break;
3354
3355 case nir_intrinsic_atomic_counter_dec:
3356 tmp = emit_untyped_atomic(bld, brw_imm_ud(surface), offset, fs_reg(),
3357 fs_reg(), 1, 1, BRW_AOP_PREDEC);
3358 break;
3359
3360 default:
3361 unreachable("Unreachable");
3362 }
3363
3364 /* Assign the result. */
3365 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD), tmp);
3366
3367 /* Mark the surface as used. */
3368 brw_mark_surface_used(stage_prog_data, surface);
3369 break;
3370 }
3371
3372 case nir_intrinsic_image_load:
3373 case nir_intrinsic_image_store:
3374 case nir_intrinsic_image_atomic_add:
3375 case nir_intrinsic_image_atomic_min:
3376 case nir_intrinsic_image_atomic_max:
3377 case nir_intrinsic_image_atomic_and:
3378 case nir_intrinsic_image_atomic_or:
3379 case nir_intrinsic_image_atomic_xor:
3380 case nir_intrinsic_image_atomic_exchange:
3381 case nir_intrinsic_image_atomic_comp_swap: {
3382 using namespace image_access;
3383
3384 /* Get the referenced image variable and type. */
3385 const nir_variable *var = instr->variables[0]->var;
3386 const glsl_type *type = var->type->without_array();
3387 const brw_reg_type base_type = get_image_base_type(type);
3388
3389 /* Get some metadata from the image intrinsic. */
3390 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
3391 const unsigned arr_dims = type->sampler_array ? 1 : 0;
3392 const unsigned surf_dims = type->coordinate_components() - arr_dims;
3393 const unsigned format = var->data.image.format;
3394
3395 /* Get the arguments of the image intrinsic. */
3396 const fs_reg image = get_nir_image_deref(instr->variables[0]);
3397 const fs_reg addr = retype(get_nir_src(instr->src[0]),
3398 BRW_REGISTER_TYPE_UD);
3399 const fs_reg src0 = (info->num_srcs >= 3 ?
3400 retype(get_nir_src(instr->src[2]), base_type) :
3401 fs_reg());
3402 const fs_reg src1 = (info->num_srcs >= 4 ?
3403 retype(get_nir_src(instr->src[3]), base_type) :
3404 fs_reg());
3405 fs_reg tmp;
3406
3407 /* Emit an image load, store or atomic op. */
3408 if (instr->intrinsic == nir_intrinsic_image_load)
3409 tmp = emit_image_load(bld, image, addr, surf_dims, arr_dims, format);
3410
3411 else if (instr->intrinsic == nir_intrinsic_image_store)
3412 emit_image_store(bld, image, addr, src0, surf_dims, arr_dims,
3413 var->data.image.write_only ? GL_NONE : format);
3414
3415 else
3416 tmp = emit_image_atomic(bld, image, addr, src0, src1,
3417 surf_dims, arr_dims, info->dest_components,
3418 get_image_atomic_op(instr->intrinsic, type));
3419
3420 /* Assign the result. */
3421 for (unsigned c = 0; c < info->dest_components; ++c)
3422 bld.MOV(offset(retype(dest, base_type), bld, c),
3423 offset(tmp, bld, c));
3424 break;
3425 }
3426
3427 case nir_intrinsic_memory_barrier_atomic_counter:
3428 case nir_intrinsic_memory_barrier_buffer:
3429 case nir_intrinsic_memory_barrier_image:
3430 case nir_intrinsic_memory_barrier: {
3431 const fs_builder ubld = bld.group(8, 0);
3432 const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
3433 ubld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp)
3434 ->regs_written = 2;
3435 break;
3436 }
3437
3438 case nir_intrinsic_group_memory_barrier:
3439 case nir_intrinsic_memory_barrier_shared:
3440 /* We treat these workgroup-level barriers as no-ops. This should be
3441 * safe at present and as long as:
3442 *
3443 * - Memory access instructions are not subsequently reordered by the
3444 * compiler back-end.
3445 *
3446 * - All threads from a given compute shader workgroup fit within a
3447 * single subslice and therefore talk to the same HDC shared unit
3448 * what supposedly guarantees ordering and coherency between threads
3449 * from the same workgroup. This may change in the future when we
3450 * start splitting workgroups across multiple subslices.
3451 *
3452 * - The context is not in fault-and-stream mode, which could cause
3453 * memory transactions (including to SLM) prior to the barrier to be
3454 * replayed after the barrier if a pagefault occurs. This shouldn't
3455 * be a problem up to and including SKL because fault-and-stream is
3456 * not usable due to hardware issues, but that's likely to change in
3457 * the future.
3458 */
3459 break;
3460
3461 case nir_intrinsic_shader_clock: {
3462 /* We cannot do anything if there is an event, so ignore it for now */
3463 fs_reg shader_clock = get_timestamp(bld);
3464 const fs_reg srcs[] = { shader_clock.set_smear(0), shader_clock.set_smear(1) };
3465
3466 bld.LOAD_PAYLOAD(dest, srcs, ARRAY_SIZE(srcs), 0);
3467 break;
3468 }
3469
3470 case nir_intrinsic_image_size: {
3471 /* Get the referenced image variable and type. */
3472 const nir_variable *var = instr->variables[0]->var;
3473 const glsl_type *type = var->type->without_array();
3474
3475 /* Get the size of the image. */
3476 const fs_reg image = get_nir_image_deref(instr->variables[0]);
3477 const fs_reg size = offset(image, bld, BRW_IMAGE_PARAM_SIZE_OFFSET);
3478
3479 /* For 1DArray image types, the array index is stored in the Z component.
3480 * Fix this by swizzling the Z component to the Y component.
3481 */
3482 const bool is_1d_array_image =
3483 type->sampler_dimensionality == GLSL_SAMPLER_DIM_1D &&
3484 type->sampler_array;
3485
3486 /* For CubeArray images, we should count the number of cubes instead
3487 * of the number of faces. Fix it by dividing the (Z component) by 6.
3488 */
3489 const bool is_cube_array_image =
3490 type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE &&
3491 type->sampler_array;
3492
3493 /* Copy all the components. */
3494 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
3495 for (unsigned c = 0; c < info->dest_components; ++c) {
3496 if ((int)c >= type->coordinate_components()) {
3497 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3498 brw_imm_d(1));
3499 } else if (c == 1 && is_1d_array_image) {
3500 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3501 offset(size, bld, 2));
3502 } else if (c == 2 && is_cube_array_image) {
3503 bld.emit(SHADER_OPCODE_INT_QUOTIENT,
3504 offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3505 offset(size, bld, c), brw_imm_d(6));
3506 } else {
3507 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3508 offset(size, bld, c));
3509 }
3510 }
3511
3512 break;
3513 }
3514
3515 case nir_intrinsic_image_samples:
3516 /* The driver does not support multi-sampled images. */
3517 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), brw_imm_d(1));
3518 break;
3519
3520 case nir_intrinsic_load_uniform: {
3521 /* Offsets are in bytes but they should always be multiples of 4 */
3522 assert(instr->const_index[0] % 4 == 0);
3523
3524 fs_reg src(UNIFORM, instr->const_index[0] / 4, dest.type);
3525
3526 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3527 if (const_offset) {
3528 /* Offsets are in bytes but they should always be multiples of 4 */
3529 assert(const_offset->u32[0] % 4 == 0);
3530 src.reg_offset = const_offset->u32[0] / 4;
3531
3532 for (unsigned j = 0; j < instr->num_components; j++) {
3533 bld.MOV(offset(dest, bld, j), offset(src, bld, j));
3534 }
3535 } else {
3536 fs_reg indirect = retype(get_nir_src(instr->src[0]),
3537 BRW_REGISTER_TYPE_UD);
3538
3539 /* We need to pass a size to the MOV_INDIRECT but we don't want it to
3540 * go past the end of the uniform. In order to keep the n'th
3541 * component from running past, we subtract off the size of all but
3542 * one component of the vector.
3543 */
3544 assert(instr->const_index[1] >=
3545 instr->num_components * (int) type_sz(dest.type));
3546 unsigned read_size = instr->const_index[1] -
3547 (instr->num_components - 1) * type_sz(dest.type);
3548
3549 for (unsigned j = 0; j < instr->num_components; j++) {
3550 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
3551 offset(dest, bld, j), offset(src, bld, j),
3552 indirect, brw_imm_ud(read_size));
3553 }
3554 }
3555 break;
3556 }
3557
3558 case nir_intrinsic_load_ubo: {
3559 nir_const_value *const_index = nir_src_as_const_value(instr->src[0]);
3560 fs_reg surf_index;
3561
3562 if (const_index) {
3563 const unsigned index = stage_prog_data->binding_table.ubo_start +
3564 const_index->u32[0];
3565 surf_index = brw_imm_ud(index);
3566 brw_mark_surface_used(prog_data, index);
3567 } else {
3568 /* The block index is not a constant. Evaluate the index expression
3569 * per-channel and add the base UBO index; we have to select a value
3570 * from any live channel.
3571 */
3572 surf_index = vgrf(glsl_type::uint_type);
3573 bld.ADD(surf_index, get_nir_src(instr->src[0]),
3574 brw_imm_ud(stage_prog_data->binding_table.ubo_start));
3575 surf_index = bld.emit_uniformize(surf_index);
3576
3577 /* Assume this may touch any UBO. It would be nice to provide
3578 * a tighter bound, but the array information is already lowered away.
3579 */
3580 brw_mark_surface_used(prog_data,
3581 stage_prog_data->binding_table.ubo_start +
3582 nir->info.num_ubos - 1);
3583 }
3584
3585 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
3586 if (const_offset == NULL) {
3587 fs_reg base_offset = retype(get_nir_src(instr->src[1]),
3588 BRW_REGISTER_TYPE_UD);
3589
3590 for (int i = 0; i < instr->num_components; i++)
3591 VARYING_PULL_CONSTANT_LOAD(bld, offset(dest, bld, i), surf_index,
3592 base_offset, i * type_sz(dest.type));
3593 } else {
3594 /* Even if we are loading doubles, a pull constant load will load
3595 * a 32-bit vec4, so should only reserve vgrf space for that. If we
3596 * need to load a full dvec4 we will have to emit 2 loads. This is
3597 * similar to demote_pull_constants(), except that in that case we
3598 * see individual accesses to each component of the vector and then
3599 * we let CSE deal with duplicate loads. Here we see a vector access
3600 * and we have to split it if necessary.
3601 */
3602 const unsigned type_size = type_sz(dest.type);
3603 const fs_reg packed_consts = bld.vgrf(BRW_REGISTER_TYPE_F);
3604 for (unsigned c = 0; c < instr->num_components;) {
3605 const unsigned base = const_offset->u32[0] + c * type_size;
3606
3607 /* Number of usable components in the next 16B-aligned load */
3608 const unsigned count = MIN2(instr->num_components - c,
3609 (16 - base % 16) / type_size);
3610
3611 bld.exec_all()
3612 .emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD,
3613 packed_consts, surf_index, brw_imm_ud(base & ~15));
3614
3615 const fs_reg consts =
3616 retype(byte_offset(packed_consts, base & 15), dest.type);
3617
3618 for (unsigned d = 0; d < count; d++)
3619 bld.MOV(offset(dest, bld, c + d), component(consts, d));
3620
3621 c += count;
3622 }
3623 }
3624 break;
3625 }
3626
3627 case nir_intrinsic_load_ssbo: {
3628 assert(devinfo->gen >= 7);
3629
3630 nir_const_value *const_uniform_block =
3631 nir_src_as_const_value(instr->src[0]);
3632
3633 fs_reg surf_index;
3634 if (const_uniform_block) {
3635 unsigned index = stage_prog_data->binding_table.ssbo_start +
3636 const_uniform_block->u32[0];
3637 surf_index = brw_imm_ud(index);
3638 brw_mark_surface_used(prog_data, index);
3639 } else {
3640 surf_index = vgrf(glsl_type::uint_type);
3641 bld.ADD(surf_index, get_nir_src(instr->src[0]),
3642 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
3643
3644 /* Assume this may touch any UBO. It would be nice to provide
3645 * a tighter bound, but the array information is already lowered away.
3646 */
3647 brw_mark_surface_used(prog_data,
3648 stage_prog_data->binding_table.ssbo_start +
3649 nir->info.num_ssbos - 1);
3650 }
3651
3652 fs_reg offset_reg;
3653 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
3654 if (const_offset) {
3655 offset_reg = brw_imm_ud(const_offset->u32[0]);
3656 } else {
3657 offset_reg = get_nir_src(instr->src[1]);
3658 }
3659
3660 /* Read the vector */
3661 do_untyped_vector_read(bld, dest, surf_index, offset_reg,
3662 instr->num_components);
3663
3664 break;
3665 }
3666
3667 case nir_intrinsic_load_input: {
3668 fs_reg src;
3669 if (stage == MESA_SHADER_VERTEX) {
3670 src = fs_reg(ATTR, instr->const_index[0], dest.type);
3671 } else {
3672 src = offset(retype(nir_inputs, dest.type), bld,
3673 instr->const_index[0]);
3674 }
3675
3676 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3677 assert(const_offset && "Indirect input loads not allowed");
3678 src = offset(src, bld, const_offset->u32[0]);
3679
3680 for (unsigned j = 0; j < instr->num_components; j++) {
3681 bld.MOV(offset(dest, bld, j), offset(src, bld, j));
3682 }
3683
3684 if (type_sz(src.type) == 8) {
3685 shuffle_32bit_load_result_to_64bit_data(bld,
3686 dest,
3687 retype(dest, BRW_REGISTER_TYPE_F),
3688 instr->num_components);
3689 }
3690
3691 break;
3692 }
3693
3694 case nir_intrinsic_store_ssbo: {
3695 assert(devinfo->gen >= 7);
3696
3697 /* Block index */
3698 fs_reg surf_index;
3699 nir_const_value *const_uniform_block =
3700 nir_src_as_const_value(instr->src[1]);
3701 if (const_uniform_block) {
3702 unsigned index = stage_prog_data->binding_table.ssbo_start +
3703 const_uniform_block->u32[0];
3704 surf_index = brw_imm_ud(index);
3705 brw_mark_surface_used(prog_data, index);
3706 } else {
3707 surf_index = vgrf(glsl_type::uint_type);
3708 bld.ADD(surf_index, get_nir_src(instr->src[1]),
3709 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
3710
3711 brw_mark_surface_used(prog_data,
3712 stage_prog_data->binding_table.ssbo_start +
3713 nir->info.num_ssbos - 1);
3714 }
3715
3716 /* Value */
3717 fs_reg val_reg = get_nir_src(instr->src[0]);
3718
3719 /* Writemask */
3720 unsigned writemask = instr->const_index[0];
3721
3722 /* get_nir_src() retypes to integer. Be wary of 64-bit types though
3723 * since the untyped writes below operate in units of 32-bits, which
3724 * means that we need to write twice as many components each time.
3725 * Also, we have to suffle 64-bit data to be in the appropriate layout
3726 * expected by our 32-bit write messages.
3727 */
3728 unsigned type_size = 4;
3729 unsigned bit_size = instr->src[0].is_ssa ?
3730 instr->src[0].ssa->bit_size : instr->src[0].reg.reg->bit_size;
3731 if (bit_size == 64) {
3732 type_size = 8;
3733 fs_reg tmp =
3734 fs_reg(VGRF, alloc.allocate(alloc.sizes[val_reg.nr]), val_reg.type);
3735 shuffle_64bit_data_for_32bit_write(bld,
3736 retype(tmp, BRW_REGISTER_TYPE_F),
3737 retype(val_reg, BRW_REGISTER_TYPE_DF),
3738 instr->num_components);
3739 val_reg = tmp;
3740 }
3741
3742 unsigned type_slots = type_size / 4;
3743
3744 /* Combine groups of consecutive enabled channels in one write
3745 * message. We use ffs to find the first enabled channel and then ffs on
3746 * the bit-inverse, down-shifted writemask to determine the length of
3747 * the block of enabled bits.
3748 */
3749 while (writemask) {
3750 unsigned first_component = ffs(writemask) - 1;
3751 unsigned length = ffs(~(writemask >> first_component)) - 1;
3752
3753 /* We can't write more than 2 64-bit components at once. Limit the
3754 * length of the write to what we can do and let the next iteration
3755 * handle the rest
3756 */
3757 if (type_size > 4)
3758 length = MIN2(2, length);
3759
3760 fs_reg offset_reg;
3761 nir_const_value *const_offset = nir_src_as_const_value(instr->src[2]);
3762 if (const_offset) {
3763 offset_reg = brw_imm_ud(const_offset->u32[0] +
3764 type_size * first_component);
3765 } else {
3766 offset_reg = vgrf(glsl_type::uint_type);
3767 bld.ADD(offset_reg,
3768 retype(get_nir_src(instr->src[2]), BRW_REGISTER_TYPE_UD),
3769 brw_imm_ud(type_size * first_component));
3770 }
3771
3772
3773 emit_untyped_write(bld, surf_index, offset_reg,
3774 offset(val_reg, bld, first_component * type_slots),
3775 1 /* dims */, length * type_slots,
3776 BRW_PREDICATE_NONE);
3777
3778 /* Clear the bits in the writemask that we just wrote, then try
3779 * again to see if more channels are left.
3780 */
3781 writemask &= (15 << (first_component + length));
3782 }
3783 break;
3784 }
3785
3786 case nir_intrinsic_store_output: {
3787 fs_reg src = get_nir_src(instr->src[0]);
3788 fs_reg new_dest = offset(retype(nir_outputs, src.type), bld,
3789 instr->const_index[0]);
3790
3791 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
3792 assert(const_offset && "Indirect output stores not allowed");
3793 new_dest = offset(new_dest, bld, const_offset->u32[0]);
3794
3795 unsigned num_components = instr->num_components;
3796 unsigned bit_size = instr->src[0].is_ssa ?
3797 instr->src[0].ssa->bit_size : instr->src[0].reg.reg->bit_size;
3798 if (bit_size == 64) {
3799 fs_reg tmp =
3800 fs_reg(VGRF, alloc.allocate(2 * num_components),
3801 BRW_REGISTER_TYPE_F);
3802 shuffle_64bit_data_for_32bit_write(
3803 bld, tmp, retype(src, BRW_REGISTER_TYPE_DF), num_components);
3804 src = retype(tmp, src.type);
3805 num_components *= 2;
3806 }
3807
3808 for (unsigned j = 0; j < num_components; j++) {
3809 bld.MOV(offset(new_dest, bld, j), offset(src, bld, j));
3810 }
3811 break;
3812 }
3813
3814 case nir_intrinsic_ssbo_atomic_add:
3815 nir_emit_ssbo_atomic(bld, BRW_AOP_ADD, instr);
3816 break;
3817 case nir_intrinsic_ssbo_atomic_imin:
3818 nir_emit_ssbo_atomic(bld, BRW_AOP_IMIN, instr);
3819 break;
3820 case nir_intrinsic_ssbo_atomic_umin:
3821 nir_emit_ssbo_atomic(bld, BRW_AOP_UMIN, instr);
3822 break;
3823 case nir_intrinsic_ssbo_atomic_imax:
3824 nir_emit_ssbo_atomic(bld, BRW_AOP_IMAX, instr);
3825 break;
3826 case nir_intrinsic_ssbo_atomic_umax:
3827 nir_emit_ssbo_atomic(bld, BRW_AOP_UMAX, instr);
3828 break;
3829 case nir_intrinsic_ssbo_atomic_and:
3830 nir_emit_ssbo_atomic(bld, BRW_AOP_AND, instr);
3831 break;
3832 case nir_intrinsic_ssbo_atomic_or:
3833 nir_emit_ssbo_atomic(bld, BRW_AOP_OR, instr);
3834 break;
3835 case nir_intrinsic_ssbo_atomic_xor:
3836 nir_emit_ssbo_atomic(bld, BRW_AOP_XOR, instr);
3837 break;
3838 case nir_intrinsic_ssbo_atomic_exchange:
3839 nir_emit_ssbo_atomic(bld, BRW_AOP_MOV, instr);
3840 break;
3841 case nir_intrinsic_ssbo_atomic_comp_swap:
3842 nir_emit_ssbo_atomic(bld, BRW_AOP_CMPWR, instr);
3843 break;
3844
3845 case nir_intrinsic_get_buffer_size: {
3846 nir_const_value *const_uniform_block = nir_src_as_const_value(instr->src[0]);
3847 unsigned ssbo_index = const_uniform_block ? const_uniform_block->u32[0] : 0;
3848 int reg_width = dispatch_width / 8;
3849
3850 /* Set LOD = 0 */
3851 fs_reg source = brw_imm_d(0);
3852
3853 int mlen = 1 * reg_width;
3854
3855 /* A resinfo's sampler message is used to get the buffer size.
3856 * The SIMD8's writeback message consists of four registers and
3857 * SIMD16's writeback message consists of 8 destination registers
3858 * (two per each component), although we are only interested on the
3859 * first component, where resinfo returns the buffer size for
3860 * SURFTYPE_BUFFER.
3861 */
3862 int regs_written = 4 * mlen;
3863 fs_reg src_payload = fs_reg(VGRF, alloc.allocate(mlen),
3864 BRW_REGISTER_TYPE_UD);
3865 bld.LOAD_PAYLOAD(src_payload, &source, 1, 0);
3866 fs_reg buffer_size = fs_reg(VGRF, alloc.allocate(regs_written),
3867 BRW_REGISTER_TYPE_UD);
3868 const unsigned index = prog_data->binding_table.ssbo_start + ssbo_index;
3869 fs_inst *inst = bld.emit(FS_OPCODE_GET_BUFFER_SIZE, buffer_size,
3870 src_payload, brw_imm_ud(index));
3871 inst->header_size = 0;
3872 inst->mlen = mlen;
3873 inst->regs_written = regs_written;
3874 bld.MOV(retype(dest, buffer_size.type), buffer_size);
3875
3876 brw_mark_surface_used(prog_data, index);
3877 break;
3878 }
3879
3880 default:
3881 unreachable("unknown intrinsic");
3882 }
3883 }
3884
3885 void
3886 fs_visitor::nir_emit_ssbo_atomic(const fs_builder &bld,
3887 int op, nir_intrinsic_instr *instr)
3888 {
3889 fs_reg dest;
3890 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3891 dest = get_nir_dest(instr->dest);
3892
3893 fs_reg surface;
3894 nir_const_value *const_surface = nir_src_as_const_value(instr->src[0]);
3895 if (const_surface) {
3896 unsigned surf_index = stage_prog_data->binding_table.ssbo_start +
3897 const_surface->u32[0];
3898 surface = brw_imm_ud(surf_index);
3899 brw_mark_surface_used(prog_data, surf_index);
3900 } else {
3901 surface = vgrf(glsl_type::uint_type);
3902 bld.ADD(surface, get_nir_src(instr->src[0]),
3903 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
3904
3905 /* Assume this may touch any SSBO. This is the same we do for other
3906 * UBO/SSBO accesses with non-constant surface.
3907 */
3908 brw_mark_surface_used(prog_data,
3909 stage_prog_data->binding_table.ssbo_start +
3910 nir->info.num_ssbos - 1);
3911 }
3912
3913 fs_reg offset = get_nir_src(instr->src[1]);
3914 fs_reg data1 = get_nir_src(instr->src[2]);
3915 fs_reg data2;
3916 if (op == BRW_AOP_CMPWR)
3917 data2 = get_nir_src(instr->src[3]);
3918
3919 /* Emit the actual atomic operation operation */
3920
3921 fs_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
3922 data1, data2,
3923 1 /* dims */, 1 /* rsize */,
3924 op,
3925 BRW_PREDICATE_NONE);
3926 dest.type = atomic_result.type;
3927 bld.MOV(dest, atomic_result);
3928 }
3929
3930 void
3931 fs_visitor::nir_emit_shared_atomic(const fs_builder &bld,
3932 int op, nir_intrinsic_instr *instr)
3933 {
3934 fs_reg dest;
3935 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3936 dest = get_nir_dest(instr->dest);
3937
3938 fs_reg surface = brw_imm_ud(GEN7_BTI_SLM);
3939 fs_reg offset = get_nir_src(instr->src[0]);
3940 fs_reg data1 = get_nir_src(instr->src[1]);
3941 fs_reg data2;
3942 if (op == BRW_AOP_CMPWR)
3943 data2 = get_nir_src(instr->src[2]);
3944
3945 /* Emit the actual atomic operation operation */
3946
3947 fs_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
3948 data1, data2,
3949 1 /* dims */, 1 /* rsize */,
3950 op,
3951 BRW_PREDICATE_NONE);
3952 dest.type = atomic_result.type;
3953 bld.MOV(dest, atomic_result);
3954 }
3955
3956 void
3957 fs_visitor::nir_emit_texture(const fs_builder &bld, nir_tex_instr *instr)
3958 {
3959 unsigned texture = instr->texture_index;
3960 unsigned sampler = instr->sampler_index;
3961
3962 fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
3963
3964 srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(texture);
3965 srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_ud(sampler);
3966
3967 int lod_components = 0;
3968
3969 /* The hardware requires a LOD for buffer textures */
3970 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
3971 srcs[TEX_LOGICAL_SRC_LOD] = brw_imm_d(0);
3972
3973 for (unsigned i = 0; i < instr->num_srcs; i++) {
3974 fs_reg src = get_nir_src(instr->src[i].src);
3975 switch (instr->src[i].src_type) {
3976 case nir_tex_src_bias:
3977 srcs[TEX_LOGICAL_SRC_LOD] =
3978 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
3979 break;
3980 case nir_tex_src_comparitor:
3981 srcs[TEX_LOGICAL_SRC_SHADOW_C] = retype(src, BRW_REGISTER_TYPE_F);
3982 break;
3983 case nir_tex_src_coord:
3984 switch (instr->op) {
3985 case nir_texop_txf:
3986 case nir_texop_txf_ms:
3987 case nir_texop_txf_ms_mcs:
3988 case nir_texop_samples_identical:
3989 srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_D);
3990 break;
3991 default:
3992 srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_F);
3993 break;
3994 }
3995 break;
3996 case nir_tex_src_ddx:
3997 srcs[TEX_LOGICAL_SRC_LOD] = retype(src, BRW_REGISTER_TYPE_F);
3998 lod_components = nir_tex_instr_src_size(instr, i);
3999 break;
4000 case nir_tex_src_ddy:
4001 srcs[TEX_LOGICAL_SRC_LOD2] = retype(src, BRW_REGISTER_TYPE_F);
4002 break;
4003 case nir_tex_src_lod:
4004 switch (instr->op) {
4005 case nir_texop_txs:
4006 srcs[TEX_LOGICAL_SRC_LOD] =
4007 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_UD);
4008 break;
4009 case nir_texop_txf:
4010 srcs[TEX_LOGICAL_SRC_LOD] =
4011 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_D);
4012 break;
4013 default:
4014 srcs[TEX_LOGICAL_SRC_LOD] =
4015 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
4016 break;
4017 }
4018 break;
4019 case nir_tex_src_ms_index:
4020 srcs[TEX_LOGICAL_SRC_SAMPLE_INDEX] = retype(src, BRW_REGISTER_TYPE_UD);
4021 break;
4022
4023 case nir_tex_src_offset: {
4024 nir_const_value *const_offset =
4025 nir_src_as_const_value(instr->src[i].src);
4026 if (const_offset) {
4027 unsigned header_bits = brw_texture_offset(const_offset->i32, 3);
4028 if (header_bits != 0)
4029 srcs[TEX_LOGICAL_SRC_OFFSET_VALUE] = brw_imm_ud(header_bits);
4030 } else {
4031 srcs[TEX_LOGICAL_SRC_OFFSET_VALUE] =
4032 retype(src, BRW_REGISTER_TYPE_D);
4033 }
4034 break;
4035 }
4036
4037 case nir_tex_src_projector:
4038 unreachable("should be lowered");
4039
4040 case nir_tex_src_texture_offset: {
4041 /* Figure out the highest possible texture index and mark it as used */
4042 uint32_t max_used = texture + instr->texture_array_size - 1;
4043 if (instr->op == nir_texop_tg4 && devinfo->gen < 8) {
4044 max_used += stage_prog_data->binding_table.gather_texture_start;
4045 } else {
4046 max_used += stage_prog_data->binding_table.texture_start;
4047 }
4048 brw_mark_surface_used(prog_data, max_used);
4049
4050 /* Emit code to evaluate the actual indexing expression */
4051 fs_reg tmp = vgrf(glsl_type::uint_type);
4052 bld.ADD(tmp, src, brw_imm_ud(texture));
4053 srcs[TEX_LOGICAL_SRC_SURFACE] = bld.emit_uniformize(tmp);
4054 break;
4055 }
4056
4057 case nir_tex_src_sampler_offset: {
4058 /* Emit code to evaluate the actual indexing expression */
4059 fs_reg tmp = vgrf(glsl_type::uint_type);
4060 bld.ADD(tmp, src, brw_imm_ud(sampler));
4061 srcs[TEX_LOGICAL_SRC_SAMPLER] = bld.emit_uniformize(tmp);
4062 break;
4063 }
4064
4065 case nir_tex_src_ms_mcs:
4066 assert(instr->op == nir_texop_txf_ms);
4067 srcs[TEX_LOGICAL_SRC_MCS] = retype(src, BRW_REGISTER_TYPE_D);
4068 break;
4069
4070 case nir_tex_src_plane: {
4071 nir_const_value *const_plane =
4072 nir_src_as_const_value(instr->src[i].src);
4073 const uint32_t plane = const_plane->u32[0];
4074 const uint32_t texture_index =
4075 instr->texture_index +
4076 stage_prog_data->binding_table.plane_start[plane] -
4077 stage_prog_data->binding_table.texture_start;
4078
4079 srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(texture_index);
4080 break;
4081 }
4082
4083 default:
4084 unreachable("unknown texture source");
4085 }
4086 }
4087
4088 if (srcs[TEX_LOGICAL_SRC_MCS].file == BAD_FILE &&
4089 (instr->op == nir_texop_txf_ms ||
4090 instr->op == nir_texop_samples_identical)) {
4091 if (devinfo->gen >= 7 &&
4092 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
4093 srcs[TEX_LOGICAL_SRC_MCS] =
4094 emit_mcs_fetch(srcs[TEX_LOGICAL_SRC_COORDINATE],
4095 instr->coord_components,
4096 srcs[TEX_LOGICAL_SRC_SURFACE]);
4097 } else {
4098 srcs[TEX_LOGICAL_SRC_MCS] = brw_imm_ud(0u);
4099 }
4100 }
4101
4102 srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_d(instr->coord_components);
4103 srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_d(lod_components);
4104
4105 if (instr->op == nir_texop_query_levels) {
4106 /* textureQueryLevels() is implemented in terms of TXS so we need to
4107 * pass a valid LOD argument.
4108 */
4109 assert(srcs[TEX_LOGICAL_SRC_LOD].file == BAD_FILE);
4110 srcs[TEX_LOGICAL_SRC_LOD] = brw_imm_ud(0u);
4111 }
4112
4113 enum opcode opcode;
4114 switch (instr->op) {
4115 case nir_texop_tex:
4116 opcode = SHADER_OPCODE_TEX_LOGICAL;
4117 break;
4118 case nir_texop_txb:
4119 opcode = FS_OPCODE_TXB_LOGICAL;
4120 break;
4121 case nir_texop_txl:
4122 opcode = SHADER_OPCODE_TXL_LOGICAL;
4123 break;
4124 case nir_texop_txd:
4125 opcode = SHADER_OPCODE_TXD_LOGICAL;
4126 break;
4127 case nir_texop_txf:
4128 opcode = SHADER_OPCODE_TXF_LOGICAL;
4129 break;
4130 case nir_texop_txf_ms:
4131 if ((key_tex->msaa_16 & (1 << sampler)))
4132 opcode = SHADER_OPCODE_TXF_CMS_W_LOGICAL;
4133 else
4134 opcode = SHADER_OPCODE_TXF_CMS_LOGICAL;
4135 break;
4136 case nir_texop_txf_ms_mcs:
4137 opcode = SHADER_OPCODE_TXF_MCS_LOGICAL;
4138 break;
4139 case nir_texop_query_levels:
4140 case nir_texop_txs:
4141 opcode = SHADER_OPCODE_TXS_LOGICAL;
4142 break;
4143 case nir_texop_lod:
4144 opcode = SHADER_OPCODE_LOD_LOGICAL;
4145 break;
4146 case nir_texop_tg4:
4147 if (srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].file != BAD_FILE &&
4148 srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].file != IMM)
4149 opcode = SHADER_OPCODE_TG4_OFFSET_LOGICAL;
4150 else
4151 opcode = SHADER_OPCODE_TG4_LOGICAL;
4152 break;
4153 case nir_texop_texture_samples:
4154 opcode = SHADER_OPCODE_SAMPLEINFO_LOGICAL;
4155 break;
4156 case nir_texop_samples_identical: {
4157 fs_reg dst = retype(get_nir_dest(instr->dest), BRW_REGISTER_TYPE_D);
4158
4159 /* If mcs is an immediate value, it means there is no MCS. In that case
4160 * just return false.
4161 */
4162 if (srcs[TEX_LOGICAL_SRC_MCS].file == BRW_IMMEDIATE_VALUE) {
4163 bld.MOV(dst, brw_imm_ud(0u));
4164 } else if ((key_tex->msaa_16 & (1 << sampler))) {
4165 fs_reg tmp = vgrf(glsl_type::uint_type);
4166 bld.OR(tmp, srcs[TEX_LOGICAL_SRC_MCS],
4167 offset(srcs[TEX_LOGICAL_SRC_MCS], bld, 1));
4168 bld.CMP(dst, tmp, brw_imm_ud(0u), BRW_CONDITIONAL_EQ);
4169 } else {
4170 bld.CMP(dst, srcs[TEX_LOGICAL_SRC_MCS], brw_imm_ud(0u),
4171 BRW_CONDITIONAL_EQ);
4172 }
4173 return;
4174 }
4175 default:
4176 unreachable("unknown texture opcode");
4177 }
4178
4179 fs_reg dst = bld.vgrf(brw_type_for_nir_type(instr->dest_type), 4);
4180 fs_inst *inst = bld.emit(opcode, dst, srcs, ARRAY_SIZE(srcs));
4181
4182 const unsigned dest_size = nir_tex_instr_dest_size(instr);
4183 if (devinfo->gen >= 9 &&
4184 instr->op != nir_texop_tg4 && instr->op != nir_texop_query_levels) {
4185 unsigned write_mask = instr->dest.is_ssa ?
4186 nir_ssa_def_components_read(&instr->dest.ssa):
4187 (1 << dest_size) - 1;
4188 assert(write_mask != 0); /* dead code should have been eliminated */
4189 inst->regs_written = _mesa_fls(write_mask) * dispatch_width / 8;
4190 } else {
4191 inst->regs_written = 4 * dispatch_width / 8;
4192 }
4193
4194 if (srcs[TEX_LOGICAL_SRC_SHADOW_C].file != BAD_FILE)
4195 inst->shadow_compare = true;
4196
4197 if (srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].file == IMM)
4198 inst->offset = srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].ud;
4199
4200 if (instr->op == nir_texop_tg4) {
4201 if (instr->component == 1 &&
4202 key_tex->gather_channel_quirk_mask & (1 << texture)) {
4203 /* gather4 sampler is broken for green channel on RG32F --
4204 * we must ask for blue instead.
4205 */
4206 inst->offset |= 2 << 16;
4207 } else {
4208 inst->offset |= instr->component << 16;
4209 }
4210
4211 if (devinfo->gen == 6)
4212 emit_gen6_gather_wa(key_tex->gen6_gather_wa[texture], dst);
4213 }
4214
4215 fs_reg nir_dest[4];
4216 for (unsigned i = 0; i < dest_size; i++)
4217 nir_dest[i] = offset(dst, bld, i);
4218
4219 bool is_cube_array = instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
4220 instr->is_array;
4221
4222 if (instr->op == nir_texop_query_levels) {
4223 /* # levels is in .w */
4224 nir_dest[0] = offset(dst, bld, 3);
4225 } else if (instr->op == nir_texop_txs && dest_size >= 3 &&
4226 (devinfo->gen < 7 || is_cube_array)) {
4227 fs_reg depth = offset(dst, bld, 2);
4228 fs_reg fixed_depth = vgrf(glsl_type::int_type);
4229
4230 if (is_cube_array) {
4231 /* fixup #layers for cube map arrays */
4232 bld.emit(SHADER_OPCODE_INT_QUOTIENT, fixed_depth, depth, brw_imm_d(6));
4233 } else if (devinfo->gen < 7) {
4234 /* Gen4-6 return 0 instead of 1 for single layer surfaces. */
4235 bld.emit_minmax(fixed_depth, depth, brw_imm_d(1), BRW_CONDITIONAL_GE);
4236 }
4237
4238 nir_dest[2] = fixed_depth;
4239 }
4240
4241 bld.LOAD_PAYLOAD(get_nir_dest(instr->dest), nir_dest, dest_size, 0);
4242 }
4243
4244 void
4245 fs_visitor::nir_emit_jump(const fs_builder &bld, nir_jump_instr *instr)
4246 {
4247 switch (instr->type) {
4248 case nir_jump_break:
4249 bld.emit(BRW_OPCODE_BREAK);
4250 break;
4251 case nir_jump_continue:
4252 bld.emit(BRW_OPCODE_CONTINUE);
4253 break;
4254 case nir_jump_return:
4255 default:
4256 unreachable("unknown jump");
4257 }
4258 }
4259
4260 /**
4261 * This helper takes the result of a load operation that reads 32-bit elements
4262 * in this format:
4263 *
4264 * x x x x x x x x
4265 * y y y y y y y y
4266 * z z z z z z z z
4267 * w w w w w w w w
4268 *
4269 * and shuffles the data to get this:
4270 *
4271 * x y x y x y x y
4272 * x y x y x y x y
4273 * z w z w z w z w
4274 * z w z w z w z w
4275 *
4276 * Which is exactly what we want if the load is reading 64-bit components
4277 * like doubles, where x represents the low 32-bit of the x double component
4278 * and y represents the high 32-bit of the x double component (likewise with
4279 * z and w for double component y). The parameter @components represents
4280 * the number of 64-bit components present in @src. This would typically be
4281 * 2 at most, since we can only fit 2 double elements in the result of a
4282 * vec4 load.
4283 *
4284 * Notice that @dst and @src can be the same register.
4285 */
4286 void
4287 shuffle_32bit_load_result_to_64bit_data(const fs_builder &bld,
4288 const fs_reg &dst,
4289 const fs_reg &src,
4290 uint32_t components)
4291 {
4292 assert(type_sz(src.type) == 4);
4293 assert(type_sz(dst.type) == 8);
4294
4295 /* A temporary that we will use to shuffle the 32-bit data of each
4296 * component in the vector into valid 64-bit data. We can't write directly
4297 * to dst because dst can be (and would usually be) the same as src
4298 * and in that case the first MOV in the loop below would overwrite the
4299 * data read in the second MOV.
4300 */
4301 fs_reg tmp = bld.vgrf(dst.type);
4302
4303 for (unsigned i = 0; i < components; i++) {
4304 const fs_reg component_i = offset(src, bld, 2 * i);
4305
4306 bld.MOV(subscript(tmp, src.type, 0), component_i);
4307 bld.MOV(subscript(tmp, src.type, 1), offset(component_i, bld, 1));
4308
4309 bld.MOV(offset(dst, bld, i), tmp);
4310 }
4311 }
4312
4313 /**
4314 * This helper does the inverse operation of
4315 * SHUFFLE_32BIT_LOAD_RESULT_TO_64BIT_DATA.
4316 *
4317 * We need to do this when we are going to use untyped write messsages that
4318 * operate with 32-bit components in order to arrange our 64-bit data to be
4319 * in the expected layout.
4320 *
4321 * Notice that callers of this function, unlike in the case of the inverse
4322 * operation, would typically need to call this with dst and src being
4323 * different registers, since they would otherwise corrupt the original
4324 * 64-bit data they are about to write. Because of this the function checks
4325 * that the src and dst regions involved in the operation do not overlap.
4326 */
4327 void
4328 shuffle_64bit_data_for_32bit_write(const fs_builder &bld,
4329 const fs_reg &dst,
4330 const fs_reg &src,
4331 uint32_t components)
4332 {
4333 assert(type_sz(src.type) == 8);
4334 assert(type_sz(dst.type) == 4);
4335
4336 assert(!src.in_range(dst, 2 * components * bld.dispatch_width() / 8));
4337
4338 for (unsigned i = 0; i < components; i++) {
4339 const fs_reg component_i = offset(src, bld, i);
4340 bld.MOV(offset(dst, bld, 2 * i), subscript(component_i, dst.type, 0));
4341 bld.MOV(offset(dst, bld, 2 * i + 1), subscript(component_i, dst.type, 1));
4342 }
4343 }