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