i965/fs: use the NIR bit size when creating registers
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_nir.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "compiler/glsl/ir.h"
25 #include "brw_fs.h"
26 #include "brw_fs_surface_builder.h"
27 #include "brw_nir.h"
28 #include "brw_program.h"
29
30 using namespace brw;
31 using namespace brw::surface_access;
32
33 void
34 fs_visitor::emit_nir_code()
35 {
36 /* emit the arrays used for inputs and outputs - load/store intrinsics will
37 * be converted to reads/writes of these arrays
38 */
39 nir_setup_inputs();
40 nir_setup_outputs();
41 nir_setup_uniforms();
42 nir_emit_system_values();
43
44 /* get the main function and emit it */
45 nir_foreach_function(function, nir) {
46 assert(strcmp(function->name, "main") == 0);
47 assert(function->impl);
48 nir_emit_impl(function->impl);
49 }
50 }
51
52 void
53 fs_visitor::nir_setup_inputs()
54 {
55 if (stage != MESA_SHADER_FRAGMENT)
56 return;
57
58 nir_inputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_inputs);
59
60 nir_foreach_variable(var, &nir->inputs) {
61 fs_reg input = offset(nir_inputs, bld, var->data.driver_location);
62
63 fs_reg reg;
64 if (var->data.location == VARYING_SLOT_POS) {
65 reg = *emit_fragcoord_interpolation(var->data.pixel_center_integer,
66 var->data.origin_upper_left);
67 emit_percomp(bld, fs_inst(BRW_OPCODE_MOV, bld.dispatch_width(),
68 input, reg), 0xF);
69 } else if (var->data.location == VARYING_SLOT_LAYER) {
70 struct brw_reg reg = suboffset(interp_reg(VARYING_SLOT_LAYER, 1), 3);
71 reg.type = BRW_REGISTER_TYPE_D;
72 bld.emit(FS_OPCODE_CINTERP, retype(input, BRW_REGISTER_TYPE_D), reg);
73 } else if (var->data.location == VARYING_SLOT_VIEWPORT) {
74 struct brw_reg reg = suboffset(interp_reg(VARYING_SLOT_VIEWPORT, 2), 3);
75 reg.type = BRW_REGISTER_TYPE_D;
76 bld.emit(FS_OPCODE_CINTERP, retype(input, BRW_REGISTER_TYPE_D), reg);
77 } else {
78 int location = var->data.location;
79 emit_general_interpolation(&input, var->name, var->type,
80 (glsl_interp_qualifier) var->data.interpolation,
81 &location, var->data.centroid,
82 var->data.sample);
83 }
84 }
85 }
86
87 void
88 fs_visitor::nir_setup_single_output_varying(fs_reg *reg,
89 const glsl_type *type,
90 unsigned *location)
91 {
92 if (type->is_array() || type->is_matrix()) {
93 const struct glsl_type *elem_type = glsl_get_array_element(type);
94 const unsigned length = glsl_get_length(type);
95
96 for (unsigned i = 0; i < length; i++) {
97 nir_setup_single_output_varying(reg, elem_type, location);
98 }
99 } else if (type->is_record()) {
100 for (unsigned i = 0; i < type->length; i++) {
101 const struct glsl_type *field_type = type->fields.structure[i].type;
102 nir_setup_single_output_varying(reg, field_type, location);
103 }
104 } else {
105 assert(type->is_scalar() || type->is_vector());
106 this->outputs[*location] = *reg;
107 this->output_components[*location] = type->vector_elements;
108 *reg = offset(*reg, bld, 4);
109 (*location)++;
110 }
111 }
112
113 void
114 fs_visitor::nir_setup_outputs()
115 {
116 if (stage == MESA_SHADER_TESS_CTRL)
117 return;
118
119 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
120
121 nir_outputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_outputs);
122
123 nir_foreach_variable(var, &nir->outputs) {
124 fs_reg reg = offset(nir_outputs, bld, var->data.driver_location);
125
126 switch (stage) {
127 case MESA_SHADER_VERTEX:
128 case MESA_SHADER_TESS_EVAL:
129 case MESA_SHADER_GEOMETRY: {
130 unsigned location = var->data.location;
131 nir_setup_single_output_varying(&reg, var->type, &location);
132 break;
133 }
134 case MESA_SHADER_FRAGMENT:
135 if (key->force_dual_color_blend &&
136 var->data.location == FRAG_RESULT_DATA1) {
137 this->dual_src_output = reg;
138 this->do_dual_src = true;
139 } else if (var->data.index > 0) {
140 assert(var->data.location == FRAG_RESULT_DATA0);
141 assert(var->data.index == 1);
142 this->dual_src_output = reg;
143 this->do_dual_src = true;
144 } else if (var->data.location == FRAG_RESULT_COLOR) {
145 /* Writing gl_FragColor outputs to all color regions. */
146 for (unsigned int i = 0; i < MAX2(key->nr_color_regions, 1); i++) {
147 this->outputs[i] = reg;
148 this->output_components[i] = 4;
149 }
150 } else if (var->data.location == FRAG_RESULT_DEPTH) {
151 this->frag_depth = reg;
152 } else if (var->data.location == FRAG_RESULT_STENCIL) {
153 this->frag_stencil = reg;
154 } else if (var->data.location == FRAG_RESULT_SAMPLE_MASK) {
155 this->sample_mask = reg;
156 } else {
157 int vector_elements = var->type->without_array()->vector_elements;
158
159 /* gl_FragData or a user-defined FS output */
160 assert(var->data.location >= FRAG_RESULT_DATA0 &&
161 var->data.location < FRAG_RESULT_DATA0+BRW_MAX_DRAW_BUFFERS);
162
163 /* General color output. */
164 for (unsigned int i = 0; i < MAX2(1, var->type->length); i++) {
165 int output = var->data.location - FRAG_RESULT_DATA0 + i;
166 this->outputs[output] = offset(reg, bld, vector_elements * i);
167 this->output_components[output] = vector_elements;
168 }
169 }
170 break;
171 default:
172 unreachable("unhandled shader stage");
173 }
174 }
175 }
176
177 void
178 fs_visitor::nir_setup_uniforms()
179 {
180 if (dispatch_width != 8)
181 return;
182
183 uniforms = nir->num_uniforms / 4;
184 }
185
186 static bool
187 emit_system_values_block(nir_block *block, fs_visitor *v)
188 {
189 fs_reg *reg;
190
191 nir_foreach_instr(instr, block) {
192 if (instr->type != nir_instr_type_intrinsic)
193 continue;
194
195 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
196 switch (intrin->intrinsic) {
197 case nir_intrinsic_load_vertex_id:
198 unreachable("should be lowered by lower_vertex_id().");
199
200 case nir_intrinsic_load_vertex_id_zero_base:
201 assert(v->stage == MESA_SHADER_VERTEX);
202 reg = &v->nir_system_values[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE];
203 if (reg->file == BAD_FILE)
204 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE);
205 break;
206
207 case nir_intrinsic_load_base_vertex:
208 assert(v->stage == MESA_SHADER_VERTEX);
209 reg = &v->nir_system_values[SYSTEM_VALUE_BASE_VERTEX];
210 if (reg->file == BAD_FILE)
211 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_BASE_VERTEX);
212 break;
213
214 case nir_intrinsic_load_instance_id:
215 assert(v->stage == MESA_SHADER_VERTEX);
216 reg = &v->nir_system_values[SYSTEM_VALUE_INSTANCE_ID];
217 if (reg->file == BAD_FILE)
218 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_INSTANCE_ID);
219 break;
220
221 case nir_intrinsic_load_base_instance:
222 assert(v->stage == MESA_SHADER_VERTEX);
223 reg = &v->nir_system_values[SYSTEM_VALUE_BASE_INSTANCE];
224 if (reg->file == BAD_FILE)
225 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_BASE_INSTANCE);
226 break;
227
228 case nir_intrinsic_load_draw_id:
229 assert(v->stage == MESA_SHADER_VERTEX);
230 reg = &v->nir_system_values[SYSTEM_VALUE_DRAW_ID];
231 if (reg->file == BAD_FILE)
232 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_DRAW_ID);
233 break;
234
235 case nir_intrinsic_load_invocation_id:
236 if (v->stage == MESA_SHADER_TESS_CTRL)
237 break;
238 assert(v->stage == MESA_SHADER_GEOMETRY);
239 reg = &v->nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
240 if (reg->file == BAD_FILE) {
241 const fs_builder abld = v->bld.annotate("gl_InvocationID", NULL);
242 fs_reg g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
243 fs_reg iid = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
244 abld.SHR(iid, g1, brw_imm_ud(27u));
245 *reg = iid;
246 }
247 break;
248
249 case nir_intrinsic_load_sample_pos:
250 assert(v->stage == MESA_SHADER_FRAGMENT);
251 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
252 if (reg->file == BAD_FILE)
253 *reg = *v->emit_samplepos_setup();
254 break;
255
256 case nir_intrinsic_load_sample_id:
257 assert(v->stage == MESA_SHADER_FRAGMENT);
258 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
259 if (reg->file == BAD_FILE)
260 *reg = *v->emit_sampleid_setup();
261 break;
262
263 case nir_intrinsic_load_sample_mask_in:
264 assert(v->stage == MESA_SHADER_FRAGMENT);
265 assert(v->devinfo->gen >= 7);
266 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_MASK_IN];
267 if (reg->file == BAD_FILE)
268 *reg = *v->emit_samplemaskin_setup();
269 break;
270
271 case nir_intrinsic_load_local_invocation_id:
272 assert(v->stage == MESA_SHADER_COMPUTE);
273 reg = &v->nir_system_values[SYSTEM_VALUE_LOCAL_INVOCATION_ID];
274 if (reg->file == BAD_FILE)
275 *reg = *v->emit_cs_local_invocation_id_setup();
276 break;
277
278 case nir_intrinsic_load_work_group_id:
279 assert(v->stage == MESA_SHADER_COMPUTE);
280 reg = &v->nir_system_values[SYSTEM_VALUE_WORK_GROUP_ID];
281 if (reg->file == BAD_FILE)
282 *reg = *v->emit_cs_work_group_id_setup();
283 break;
284
285 case nir_intrinsic_load_helper_invocation:
286 assert(v->stage == MESA_SHADER_FRAGMENT);
287 reg = &v->nir_system_values[SYSTEM_VALUE_HELPER_INVOCATION];
288 if (reg->file == BAD_FILE) {
289 const fs_builder abld =
290 v->bld.annotate("gl_HelperInvocation", NULL);
291
292 /* On Gen6+ (gl_HelperInvocation is only exposed on Gen7+) the
293 * pixel mask is in g1.7 of the thread payload.
294 *
295 * We move the per-channel pixel enable bit to the low bit of each
296 * channel by shifting the byte containing the pixel mask by the
297 * vector immediate 0x76543210UV.
298 *
299 * The region of <1,8,0> reads only 1 byte (the pixel masks for
300 * subspans 0 and 1) in SIMD8 and an additional byte (the pixel
301 * masks for 2 and 3) in SIMD16.
302 */
303 fs_reg shifted = abld.vgrf(BRW_REGISTER_TYPE_UW, 1);
304 abld.SHR(shifted,
305 stride(byte_offset(retype(brw_vec1_grf(1, 0),
306 BRW_REGISTER_TYPE_UB), 28),
307 1, 8, 0),
308 brw_imm_uv(0x76543210));
309
310 /* A set bit in the pixel mask means the channel is enabled, but
311 * that is the opposite of gl_HelperInvocation so we need to invert
312 * the mask.
313 *
314 * The negate source-modifier bit of logical instructions on Gen8+
315 * performs 1's complement negation, so we can use that instead of
316 * a NOT instruction.
317 */
318 fs_reg inverted = negate(shifted);
319 if (v->devinfo->gen < 8) {
320 inverted = abld.vgrf(BRW_REGISTER_TYPE_UW);
321 abld.NOT(inverted, shifted);
322 }
323
324 /* We then resolve the 0/1 result to 0/~0 boolean values by ANDing
325 * with 1 and negating.
326 */
327 fs_reg anded = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
328 abld.AND(anded, inverted, brw_imm_uw(1));
329
330 fs_reg dst = abld.vgrf(BRW_REGISTER_TYPE_D, 1);
331 abld.MOV(dst, negate(retype(anded, BRW_REGISTER_TYPE_D)));
332 *reg = dst;
333 }
334 break;
335
336 default:
337 break;
338 }
339 }
340
341 return true;
342 }
343
344 void
345 fs_visitor::nir_emit_system_values()
346 {
347 nir_system_values = ralloc_array(mem_ctx, fs_reg, SYSTEM_VALUE_MAX);
348 for (unsigned i = 0; i < SYSTEM_VALUE_MAX; i++) {
349 nir_system_values[i] = fs_reg();
350 }
351
352 nir_foreach_function(function, nir) {
353 assert(strcmp(function->name, "main") == 0);
354 assert(function->impl);
355 nir_foreach_block(block, function->impl) {
356 emit_system_values_block(block, this);
357 }
358 }
359 }
360
361 void
362 fs_visitor::nir_emit_impl(nir_function_impl *impl)
363 {
364 nir_locals = ralloc_array(mem_ctx, fs_reg, impl->reg_alloc);
365 for (unsigned i = 0; i < impl->reg_alloc; i++) {
366 nir_locals[i] = fs_reg();
367 }
368
369 foreach_list_typed(nir_register, reg, node, &impl->registers) {
370 unsigned array_elems =
371 reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
372 unsigned size = array_elems * reg->num_components;
373 const brw_reg_type reg_type =
374 reg->bit_size == 32 ? BRW_REGISTER_TYPE_F : BRW_REGISTER_TYPE_DF;
375 nir_locals[reg->index] = bld.vgrf(reg_type, size);
376 }
377
378 nir_ssa_values = reralloc(mem_ctx, nir_ssa_values, fs_reg,
379 impl->ssa_alloc);
380
381 nir_emit_cf_list(&impl->body);
382 }
383
384 void
385 fs_visitor::nir_emit_cf_list(exec_list *list)
386 {
387 exec_list_validate(list);
388 foreach_list_typed(nir_cf_node, node, node, list) {
389 switch (node->type) {
390 case nir_cf_node_if:
391 nir_emit_if(nir_cf_node_as_if(node));
392 break;
393
394 case nir_cf_node_loop:
395 nir_emit_loop(nir_cf_node_as_loop(node));
396 break;
397
398 case nir_cf_node_block:
399 nir_emit_block(nir_cf_node_as_block(node));
400 break;
401
402 default:
403 unreachable("Invalid CFG node block");
404 }
405 }
406 }
407
408 void
409 fs_visitor::nir_emit_if(nir_if *if_stmt)
410 {
411 /* first, put the condition into f0 */
412 fs_inst *inst = bld.MOV(bld.null_reg_d(),
413 retype(get_nir_src(if_stmt->condition),
414 BRW_REGISTER_TYPE_D));
415 inst->conditional_mod = BRW_CONDITIONAL_NZ;
416
417 bld.IF(BRW_PREDICATE_NORMAL);
418
419 nir_emit_cf_list(&if_stmt->then_list);
420
421 /* note: if the else is empty, dead CF elimination will remove it */
422 bld.emit(BRW_OPCODE_ELSE);
423
424 nir_emit_cf_list(&if_stmt->else_list);
425
426 bld.emit(BRW_OPCODE_ENDIF);
427 }
428
429 void
430 fs_visitor::nir_emit_loop(nir_loop *loop)
431 {
432 bld.emit(BRW_OPCODE_DO);
433
434 nir_emit_cf_list(&loop->body);
435
436 bld.emit(BRW_OPCODE_WHILE);
437 }
438
439 void
440 fs_visitor::nir_emit_block(nir_block *block)
441 {
442 nir_foreach_instr(instr, block) {
443 nir_emit_instr(instr);
444 }
445 }
446
447 void
448 fs_visitor::nir_emit_instr(nir_instr *instr)
449 {
450 const fs_builder abld = bld.annotate(NULL, instr);
451
452 switch (instr->type) {
453 case nir_instr_type_alu:
454 nir_emit_alu(abld, nir_instr_as_alu(instr));
455 break;
456
457 case nir_instr_type_intrinsic:
458 switch (stage) {
459 case MESA_SHADER_VERTEX:
460 nir_emit_vs_intrinsic(abld, nir_instr_as_intrinsic(instr));
461 break;
462 case MESA_SHADER_TESS_CTRL:
463 nir_emit_tcs_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 const brw_reg_type reg_type =
1215 instr->def.bit_size == 32 ? BRW_REGISTER_TYPE_D : BRW_REGISTER_TYPE_DF;
1216 fs_reg reg = bld.vgrf(reg_type, instr->def.num_components);
1217
1218 switch (instr->def.bit_size) {
1219 case 32:
1220 for (unsigned i = 0; i < instr->def.num_components; i++)
1221 bld.MOV(offset(reg, bld, i), brw_imm_d(instr->value.i32[i]));
1222 break;
1223
1224 case 64:
1225 for (unsigned i = 0; i < instr->def.num_components; i++)
1226 bld.MOV(offset(reg, bld, i), brw_imm_df(instr->value.f64[i]));
1227 break;
1228
1229 default:
1230 unreachable("Invalid bit size");
1231 }
1232
1233 nir_ssa_values[instr->def.index] = reg;
1234 }
1235
1236 void
1237 fs_visitor::nir_emit_undef(const fs_builder &bld, nir_ssa_undef_instr *instr)
1238 {
1239 const brw_reg_type reg_type =
1240 instr->def.bit_size == 32 ? BRW_REGISTER_TYPE_D : BRW_REGISTER_TYPE_DF;
1241 nir_ssa_values[instr->def.index] =
1242 bld.vgrf(reg_type, instr->def.num_components);
1243 }
1244
1245 fs_reg
1246 fs_visitor::get_nir_src(nir_src src)
1247 {
1248 fs_reg reg;
1249 if (src.is_ssa) {
1250 reg = nir_ssa_values[src.ssa->index];
1251 } else {
1252 /* We don't handle indirects on locals */
1253 assert(src.reg.indirect == NULL);
1254 reg = offset(nir_locals[src.reg.reg->index], bld,
1255 src.reg.base_offset * src.reg.reg->num_components);
1256 }
1257
1258 /* to avoid floating-point denorm flushing problems, set the type by
1259 * default to D - instructions that need floating point semantics will set
1260 * this to F if they need to
1261 */
1262 return retype(reg, BRW_REGISTER_TYPE_D);
1263 }
1264
1265 fs_reg
1266 fs_visitor::get_nir_dest(nir_dest dest)
1267 {
1268 if (dest.is_ssa) {
1269 const brw_reg_type reg_type =
1270 dest.ssa.bit_size == 32 ? BRW_REGISTER_TYPE_F : BRW_REGISTER_TYPE_DF;
1271 nir_ssa_values[dest.ssa.index] =
1272 bld.vgrf(reg_type, dest.ssa.num_components);
1273 return nir_ssa_values[dest.ssa.index];
1274 } else {
1275 /* We don't handle indirects on locals */
1276 assert(dest.reg.indirect == NULL);
1277 return offset(nir_locals[dest.reg.reg->index], bld,
1278 dest.reg.base_offset * dest.reg.reg->num_components);
1279 }
1280 }
1281
1282 fs_reg
1283 fs_visitor::get_nir_image_deref(const nir_deref_var *deref)
1284 {
1285 fs_reg image(UNIFORM, deref->var->data.driver_location / 4,
1286 BRW_REGISTER_TYPE_UD);
1287 fs_reg indirect;
1288 unsigned indirect_max = 0;
1289
1290 for (const nir_deref *tail = &deref->deref; tail->child;
1291 tail = tail->child) {
1292 const nir_deref_array *deref_array = nir_deref_as_array(tail->child);
1293 assert(tail->child->deref_type == nir_deref_type_array);
1294 const unsigned size = glsl_get_length(tail->type);
1295 const unsigned element_size = type_size_scalar(deref_array->deref.type);
1296 const unsigned base = MIN2(deref_array->base_offset, size - 1);
1297 image = offset(image, bld, base * element_size);
1298
1299 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
1300 fs_reg tmp = vgrf(glsl_type::uint_type);
1301
1302 /* Accessing an invalid surface index with the dataport can result
1303 * in a hang. According to the spec "if the index used to
1304 * select an individual element is negative or greater than or
1305 * equal to the size of the array, the results of the operation
1306 * are undefined but may not lead to termination" -- which is one
1307 * of the possible outcomes of the hang. Clamp the index to
1308 * prevent access outside of the array bounds.
1309 */
1310 bld.emit_minmax(tmp, retype(get_nir_src(deref_array->indirect),
1311 BRW_REGISTER_TYPE_UD),
1312 brw_imm_ud(size - base - 1), BRW_CONDITIONAL_L);
1313
1314 indirect_max += element_size * (tail->type->length - 1);
1315
1316 bld.MUL(tmp, tmp, brw_imm_ud(element_size * 4));
1317 if (indirect.file == BAD_FILE) {
1318 indirect = tmp;
1319 } else {
1320 bld.ADD(indirect, indirect, tmp);
1321 }
1322 }
1323 }
1324
1325 if (indirect.file == BAD_FILE) {
1326 return image;
1327 } else {
1328 /* Emit a pile of MOVs to load the uniform into a temporary. The
1329 * dead-code elimination pass will get rid of what we don't use.
1330 */
1331 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, BRW_IMAGE_PARAM_SIZE);
1332 for (unsigned j = 0; j < BRW_IMAGE_PARAM_SIZE; j++) {
1333 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
1334 offset(tmp, bld, j), offset(image, bld, j),
1335 indirect, brw_imm_ud((indirect_max + 1) * 4));
1336 }
1337 return tmp;
1338 }
1339 }
1340
1341 void
1342 fs_visitor::emit_percomp(const fs_builder &bld, const fs_inst &inst,
1343 unsigned wr_mask)
1344 {
1345 for (unsigned i = 0; i < 4; i++) {
1346 if (!((wr_mask >> i) & 1))
1347 continue;
1348
1349 fs_inst *new_inst = new(mem_ctx) fs_inst(inst);
1350 new_inst->dst = offset(new_inst->dst, bld, i);
1351 for (unsigned j = 0; j < new_inst->sources; j++)
1352 if (new_inst->src[j].file == VGRF)
1353 new_inst->src[j] = offset(new_inst->src[j], bld, i);
1354
1355 bld.emit(new_inst);
1356 }
1357 }
1358
1359 /**
1360 * Get the matching channel register datatype for an image intrinsic of the
1361 * specified GLSL image type.
1362 */
1363 static brw_reg_type
1364 get_image_base_type(const glsl_type *type)
1365 {
1366 switch ((glsl_base_type)type->sampled_type) {
1367 case GLSL_TYPE_UINT:
1368 return BRW_REGISTER_TYPE_UD;
1369 case GLSL_TYPE_INT:
1370 return BRW_REGISTER_TYPE_D;
1371 case GLSL_TYPE_FLOAT:
1372 return BRW_REGISTER_TYPE_F;
1373 default:
1374 unreachable("Not reached.");
1375 }
1376 }
1377
1378 /**
1379 * Get the appropriate atomic op for an image atomic intrinsic.
1380 */
1381 static unsigned
1382 get_image_atomic_op(nir_intrinsic_op op, const glsl_type *type)
1383 {
1384 switch (op) {
1385 case nir_intrinsic_image_atomic_add:
1386 return BRW_AOP_ADD;
1387 case nir_intrinsic_image_atomic_min:
1388 return (get_image_base_type(type) == BRW_REGISTER_TYPE_D ?
1389 BRW_AOP_IMIN : BRW_AOP_UMIN);
1390 case nir_intrinsic_image_atomic_max:
1391 return (get_image_base_type(type) == BRW_REGISTER_TYPE_D ?
1392 BRW_AOP_IMAX : BRW_AOP_UMAX);
1393 case nir_intrinsic_image_atomic_and:
1394 return BRW_AOP_AND;
1395 case nir_intrinsic_image_atomic_or:
1396 return BRW_AOP_OR;
1397 case nir_intrinsic_image_atomic_xor:
1398 return BRW_AOP_XOR;
1399 case nir_intrinsic_image_atomic_exchange:
1400 return BRW_AOP_MOV;
1401 case nir_intrinsic_image_atomic_comp_swap:
1402 return BRW_AOP_CMPWR;
1403 default:
1404 unreachable("Not reachable.");
1405 }
1406 }
1407
1408 static fs_inst *
1409 emit_pixel_interpolater_send(const fs_builder &bld,
1410 enum opcode opcode,
1411 const fs_reg &dst,
1412 const fs_reg &src,
1413 const fs_reg &desc,
1414 glsl_interp_qualifier interpolation)
1415 {
1416 fs_inst *inst;
1417 fs_reg payload;
1418 int mlen;
1419
1420 if (src.file == BAD_FILE) {
1421 /* Dummy payload */
1422 payload = bld.vgrf(BRW_REGISTER_TYPE_F, 1);
1423 mlen = 1;
1424 } else {
1425 payload = src;
1426 mlen = 2 * bld.dispatch_width() / 8;
1427 }
1428
1429 inst = bld.emit(opcode, dst, payload, desc);
1430 inst->mlen = mlen;
1431 /* 2 floats per slot returned */
1432 inst->regs_written = 2 * bld.dispatch_width() / 8;
1433 inst->pi_noperspective = interpolation == INTERP_QUALIFIER_NOPERSPECTIVE;
1434
1435 return inst;
1436 }
1437
1438 /**
1439 * Computes 1 << x, given a D/UD register containing some value x.
1440 */
1441 static fs_reg
1442 intexp2(const fs_builder &bld, const fs_reg &x)
1443 {
1444 assert(x.type == BRW_REGISTER_TYPE_UD || x.type == BRW_REGISTER_TYPE_D);
1445
1446 fs_reg result = bld.vgrf(x.type, 1);
1447 fs_reg one = bld.vgrf(x.type, 1);
1448
1449 bld.MOV(one, retype(brw_imm_d(1), one.type));
1450 bld.SHL(result, one, x);
1451 return result;
1452 }
1453
1454 void
1455 fs_visitor::emit_gs_end_primitive(const nir_src &vertex_count_nir_src)
1456 {
1457 assert(stage == MESA_SHADER_GEOMETRY);
1458
1459 struct brw_gs_prog_data *gs_prog_data =
1460 (struct brw_gs_prog_data *) prog_data;
1461
1462 /* We can only do EndPrimitive() functionality when the control data
1463 * consists of cut bits. Fortunately, the only time it isn't is when the
1464 * output type is points, in which case EndPrimitive() is a no-op.
1465 */
1466 if (gs_prog_data->control_data_format !=
1467 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
1468 return;
1469 }
1470
1471 /* Cut bits use one bit per vertex. */
1472 assert(gs_compile->control_data_bits_per_vertex == 1);
1473
1474 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
1475 vertex_count.type = BRW_REGISTER_TYPE_UD;
1476
1477 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
1478 * vertex n, 0 otherwise. So all we need to do here is mark bit
1479 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
1480 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
1481 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
1482 *
1483 * Note that if EndPrimitive() is called before emitting any vertices, this
1484 * will cause us to set bit 31 of the control_data_bits register to 1.
1485 * That's fine because:
1486 *
1487 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
1488 * output, so the hardware will ignore cut bit 31.
1489 *
1490 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
1491 * last vertex, so setting cut bit 31 has no effect (since the primitive
1492 * is automatically ended when the GS terminates).
1493 *
1494 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
1495 * control_data_bits register to 0 when the first vertex is emitted.
1496 */
1497
1498 const fs_builder abld = bld.annotate("end primitive");
1499
1500 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
1501 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1502 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
1503 fs_reg mask = intexp2(abld, prev_count);
1504 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1505 * attention to the lower 5 bits of its second source argument, so on this
1506 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
1507 * ((vertex_count - 1) % 32).
1508 */
1509 abld.OR(this->control_data_bits, this->control_data_bits, mask);
1510 }
1511
1512 void
1513 fs_visitor::emit_gs_control_data_bits(const fs_reg &vertex_count)
1514 {
1515 assert(stage == MESA_SHADER_GEOMETRY);
1516 assert(gs_compile->control_data_bits_per_vertex != 0);
1517
1518 struct brw_gs_prog_data *gs_prog_data =
1519 (struct brw_gs_prog_data *) prog_data;
1520
1521 const fs_builder abld = bld.annotate("emit control data bits");
1522 const fs_builder fwa_bld = bld.exec_all();
1523
1524 /* We use a single UD register to accumulate control data bits (32 bits
1525 * for each of the SIMD8 channels). So we need to write a DWord (32 bits)
1526 * at a time.
1527 *
1528 * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
1529 * We have select a 128-bit group via the Global and Per-Slot Offsets, then
1530 * use the Channel Mask phase to enable/disable which DWord within that
1531 * group to write. (Remember, different SIMD8 channels may have emitted
1532 * different numbers of vertices, so we may need per-slot offsets.)
1533 *
1534 * Channel masking presents an annoying problem: we may have to replicate
1535 * the data up to 4 times:
1536 *
1537 * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
1538 *
1539 * To avoid penalizing shaders that emit a small number of vertices, we
1540 * can avoid these sometimes: if the size of the control data header is
1541 * <= 128 bits, then there is only 1 OWord. All SIMD8 channels will land
1542 * land in the same 128-bit group, so we can skip per-slot offsets.
1543 *
1544 * Similarly, if the control data header is <= 32 bits, there is only one
1545 * DWord, so we can skip channel masks.
1546 */
1547 enum opcode opcode = SHADER_OPCODE_URB_WRITE_SIMD8;
1548
1549 fs_reg channel_mask, per_slot_offset;
1550
1551 if (gs_compile->control_data_header_size_bits > 32) {
1552 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
1553 channel_mask = vgrf(glsl_type::uint_type);
1554 }
1555
1556 if (gs_compile->control_data_header_size_bits > 128) {
1557 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT;
1558 per_slot_offset = vgrf(glsl_type::uint_type);
1559 }
1560
1561 /* Figure out which DWord we're trying to write to using the formula:
1562 *
1563 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
1564 *
1565 * Since bits_per_vertex is a power of two, and is known at compile
1566 * time, this can be optimized to:
1567 *
1568 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
1569 */
1570 if (opcode != SHADER_OPCODE_URB_WRITE_SIMD8) {
1571 fs_reg dword_index = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1572 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1573 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
1574 unsigned log2_bits_per_vertex =
1575 _mesa_fls(gs_compile->control_data_bits_per_vertex);
1576 abld.SHR(dword_index, prev_count, brw_imm_ud(6u - log2_bits_per_vertex));
1577
1578 if (per_slot_offset.file != BAD_FILE) {
1579 /* Set the per-slot offset to dword_index / 4, so that we'll write to
1580 * the appropriate OWord within the control data header.
1581 */
1582 abld.SHR(per_slot_offset, dword_index, brw_imm_ud(2u));
1583 }
1584
1585 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
1586 * write to the appropriate DWORD within the OWORD.
1587 */
1588 fs_reg channel = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1589 fwa_bld.AND(channel, dword_index, brw_imm_ud(3u));
1590 channel_mask = intexp2(fwa_bld, channel);
1591 /* Then the channel masks need to be in bits 23:16. */
1592 fwa_bld.SHL(channel_mask, channel_mask, brw_imm_ud(16u));
1593 }
1594
1595 /* Store the control data bits in the message payload and send it. */
1596 int mlen = 2;
1597 if (channel_mask.file != BAD_FILE)
1598 mlen += 4; /* channel masks, plus 3 extra copies of the data */
1599 if (per_slot_offset.file != BAD_FILE)
1600 mlen++;
1601
1602 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
1603 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, mlen);
1604 int i = 0;
1605 sources[i++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
1606 if (per_slot_offset.file != BAD_FILE)
1607 sources[i++] = per_slot_offset;
1608 if (channel_mask.file != BAD_FILE)
1609 sources[i++] = channel_mask;
1610 while (i < mlen) {
1611 sources[i++] = this->control_data_bits;
1612 }
1613
1614 abld.LOAD_PAYLOAD(payload, sources, mlen, mlen);
1615 fs_inst *inst = abld.emit(opcode, reg_undef, payload);
1616 inst->mlen = mlen;
1617 /* We need to increment Global Offset by 256-bits to make room for
1618 * Broadwell's extra "Vertex Count" payload at the beginning of the
1619 * URB entry. Since this is an OWord message, Global Offset is counted
1620 * in 128-bit units, so we must set it to 2.
1621 */
1622 if (gs_prog_data->static_vertex_count == -1)
1623 inst->offset = 2;
1624 }
1625
1626 void
1627 fs_visitor::set_gs_stream_control_data_bits(const fs_reg &vertex_count,
1628 unsigned stream_id)
1629 {
1630 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
1631
1632 /* Note: we are calling this *before* increasing vertex_count, so
1633 * this->vertex_count == vertex_count - 1 in the formula above.
1634 */
1635
1636 /* Stream mode uses 2 bits per vertex */
1637 assert(gs_compile->control_data_bits_per_vertex == 2);
1638
1639 /* Must be a valid stream */
1640 assert(stream_id >= 0 && stream_id < MAX_VERTEX_STREAMS);
1641
1642 /* Control data bits are initialized to 0 so we don't have to set any
1643 * bits when sending vertices to stream 0.
1644 */
1645 if (stream_id == 0)
1646 return;
1647
1648 const fs_builder abld = bld.annotate("set stream control data bits", NULL);
1649
1650 /* reg::sid = stream_id */
1651 fs_reg sid = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1652 abld.MOV(sid, brw_imm_ud(stream_id));
1653
1654 /* reg:shift_count = 2 * (vertex_count - 1) */
1655 fs_reg shift_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1656 abld.SHL(shift_count, vertex_count, brw_imm_ud(1u));
1657
1658 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1659 * attention to the lower 5 bits of its second source argument, so on this
1660 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
1661 * stream_id << ((2 * (vertex_count - 1)) % 32).
1662 */
1663 fs_reg mask = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1664 abld.SHL(mask, sid, shift_count);
1665 abld.OR(this->control_data_bits, this->control_data_bits, mask);
1666 }
1667
1668 void
1669 fs_visitor::emit_gs_vertex(const nir_src &vertex_count_nir_src,
1670 unsigned stream_id)
1671 {
1672 assert(stage == MESA_SHADER_GEOMETRY);
1673
1674 struct brw_gs_prog_data *gs_prog_data =
1675 (struct brw_gs_prog_data *) prog_data;
1676
1677 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
1678 vertex_count.type = BRW_REGISTER_TYPE_UD;
1679
1680 /* Haswell and later hardware ignores the "Render Stream Select" bits
1681 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
1682 * and instead sends all primitives down the pipeline for rasterization.
1683 * If the SOL stage is enabled, "Render Stream Select" is honored and
1684 * primitives bound to non-zero streams are discarded after stream output.
1685 *
1686 * Since the only purpose of primives sent to non-zero streams is to
1687 * be recorded by transform feedback, we can simply discard all geometry
1688 * bound to these streams when transform feedback is disabled.
1689 */
1690 if (stream_id > 0 && !nir->info.has_transform_feedback_varyings)
1691 return;
1692
1693 /* If we're outputting 32 control data bits or less, then we can wait
1694 * until the shader is over to output them all. Otherwise we need to
1695 * output them as we go. Now is the time to do it, since we're about to
1696 * output the vertex_count'th vertex, so it's guaranteed that the
1697 * control data bits associated with the (vertex_count - 1)th vertex are
1698 * correct.
1699 */
1700 if (gs_compile->control_data_header_size_bits > 32) {
1701 const fs_builder abld =
1702 bld.annotate("emit vertex: emit control data bits");
1703
1704 /* Only emit control data bits if we've finished accumulating a batch
1705 * of 32 bits. This is the case when:
1706 *
1707 * (vertex_count * bits_per_vertex) % 32 == 0
1708 *
1709 * (in other words, when the last 5 bits of vertex_count *
1710 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
1711 * integer n (which is always the case, since bits_per_vertex is
1712 * always 1 or 2), this is equivalent to requiring that the last 5-n
1713 * bits of vertex_count are 0:
1714 *
1715 * vertex_count & (2^(5-n) - 1) == 0
1716 *
1717 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
1718 * equivalent to:
1719 *
1720 * vertex_count & (32 / bits_per_vertex - 1) == 0
1721 *
1722 * TODO: If vertex_count is an immediate, we could do some of this math
1723 * at compile time...
1724 */
1725 fs_inst *inst =
1726 abld.AND(bld.null_reg_d(), vertex_count,
1727 brw_imm_ud(32u / gs_compile->control_data_bits_per_vertex - 1u));
1728 inst->conditional_mod = BRW_CONDITIONAL_Z;
1729
1730 abld.IF(BRW_PREDICATE_NORMAL);
1731 /* If vertex_count is 0, then no control data bits have been
1732 * accumulated yet, so we can skip emitting them.
1733 */
1734 abld.CMP(bld.null_reg_d(), vertex_count, brw_imm_ud(0u),
1735 BRW_CONDITIONAL_NEQ);
1736 abld.IF(BRW_PREDICATE_NORMAL);
1737 emit_gs_control_data_bits(vertex_count);
1738 abld.emit(BRW_OPCODE_ENDIF);
1739
1740 /* Reset control_data_bits to 0 so we can start accumulating a new
1741 * batch.
1742 *
1743 * Note: in the case where vertex_count == 0, this neutralizes the
1744 * effect of any call to EndPrimitive() that the shader may have
1745 * made before outputting its first vertex.
1746 */
1747 inst = abld.MOV(this->control_data_bits, brw_imm_ud(0u));
1748 inst->force_writemask_all = true;
1749 abld.emit(BRW_OPCODE_ENDIF);
1750 }
1751
1752 emit_urb_writes(vertex_count);
1753
1754 /* In stream mode we have to set control data bits for all vertices
1755 * unless we have disabled control data bits completely (which we do
1756 * do for GL_POINTS outputs that don't use streams).
1757 */
1758 if (gs_compile->control_data_header_size_bits > 0 &&
1759 gs_prog_data->control_data_format ==
1760 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
1761 set_gs_stream_control_data_bits(vertex_count, stream_id);
1762 }
1763 }
1764
1765 void
1766 fs_visitor::emit_gs_input_load(const fs_reg &dst,
1767 const nir_src &vertex_src,
1768 unsigned base_offset,
1769 const nir_src &offset_src,
1770 unsigned num_components)
1771 {
1772 struct brw_gs_prog_data *gs_prog_data = (struct brw_gs_prog_data *) prog_data;
1773
1774 nir_const_value *vertex_const = nir_src_as_const_value(vertex_src);
1775 nir_const_value *offset_const = nir_src_as_const_value(offset_src);
1776 const unsigned push_reg_count = gs_prog_data->base.urb_read_length * 8;
1777
1778 /* Offset 0 is the VUE header, which contains VARYING_SLOT_LAYER [.y],
1779 * VARYING_SLOT_VIEWPORT [.z], and VARYING_SLOT_PSIZ [.w]. Only
1780 * gl_PointSize is available as a GS input, however, so it must be that.
1781 */
1782 const bool is_point_size = (base_offset == 0);
1783
1784 if (offset_const != NULL && vertex_const != NULL &&
1785 4 * (base_offset + offset_const->u32[0]) < push_reg_count) {
1786 int imm_offset = (base_offset + offset_const->u32[0]) * 4 +
1787 vertex_const->u32[0] * push_reg_count;
1788 /* This input was pushed into registers. */
1789 if (is_point_size) {
1790 /* gl_PointSize comes in .w */
1791 assert(imm_offset == 0);
1792 bld.MOV(dst, fs_reg(ATTR, imm_offset + 3, dst.type));
1793 } else {
1794 for (unsigned i = 0; i < num_components; i++) {
1795 bld.MOV(offset(dst, bld, i),
1796 fs_reg(ATTR, imm_offset + i, dst.type));
1797 }
1798 }
1799 } else {
1800 /* Resort to the pull model. Ensure the VUE handles are provided. */
1801 gs_prog_data->base.include_vue_handles = true;
1802
1803 unsigned first_icp_handle = gs_prog_data->include_primitive_id ? 3 : 2;
1804 fs_reg icp_handle;
1805
1806 if (vertex_const) {
1807 /* The vertex index is constant; just select the proper URB handle. */
1808 icp_handle =
1809 retype(brw_vec8_grf(first_icp_handle + vertex_const->i32[0], 0),
1810 BRW_REGISTER_TYPE_UD);
1811 } else {
1812 /* The vertex index is non-constant. We need to use indirect
1813 * addressing to fetch the proper URB handle.
1814 *
1815 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
1816 * indicating that channel <n> should read the handle from
1817 * DWord <n>. We convert that to bytes by multiplying by 4.
1818 *
1819 * Next, we convert the vertex index to bytes by multiplying
1820 * by 32 (shifting by 5), and add the two together. This is
1821 * the final indirect byte offset.
1822 */
1823 fs_reg sequence = bld.vgrf(BRW_REGISTER_TYPE_W, 1);
1824 fs_reg channel_offsets = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1825 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1826 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1827 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1828
1829 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
1830 bld.MOV(sequence, fs_reg(brw_imm_v(0x76543210)));
1831 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
1832 bld.SHL(channel_offsets, sequence, brw_imm_ud(2u));
1833 /* Convert vertex_index to bytes (multiply by 32) */
1834 bld.SHL(vertex_offset_bytes,
1835 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
1836 brw_imm_ud(5u));
1837 bld.ADD(icp_offset_bytes, vertex_offset_bytes, channel_offsets);
1838
1839 /* Use first_icp_handle as the base offset. There is one register
1840 * of URB handles per vertex, so inform the register allocator that
1841 * we might read up to nir->info.gs.vertices_in registers.
1842 */
1843 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
1844 fs_reg(brw_vec8_grf(first_icp_handle, 0)),
1845 fs_reg(icp_offset_bytes),
1846 brw_imm_ud(nir->info.gs.vertices_in * REG_SIZE));
1847 }
1848
1849 fs_inst *inst;
1850 if (offset_const) {
1851 /* Constant indexing - use global offset. */
1852 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
1853 inst->offset = base_offset + offset_const->u32[0];
1854 inst->base_mrf = -1;
1855 inst->mlen = 1;
1856 inst->regs_written = num_components;
1857 } else {
1858 /* Indirect indexing - use per-slot offsets as well. */
1859 const fs_reg srcs[] = { icp_handle, get_nir_src(offset_src) };
1860 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
1861 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
1862
1863 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst, payload);
1864 inst->offset = base_offset;
1865 inst->base_mrf = -1;
1866 inst->mlen = 2;
1867 inst->regs_written = num_components;
1868 }
1869
1870 if (is_point_size) {
1871 /* Read the whole VUE header (because of alignment) and read .w. */
1872 fs_reg tmp = bld.vgrf(dst.type, 4);
1873 inst->dst = tmp;
1874 inst->regs_written = 4;
1875 bld.MOV(dst, offset(tmp, bld, 3));
1876 }
1877 }
1878 }
1879
1880 fs_reg
1881 fs_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
1882 {
1883 nir_src *offset_src = nir_get_io_offset_src(instr);
1884 nir_const_value *const_value = nir_src_as_const_value(*offset_src);
1885
1886 if (const_value) {
1887 /* The only constant offset we should find is 0. brw_nir.c's
1888 * add_const_offset_to_base() will fold other constant offsets
1889 * into instr->const_index[0].
1890 */
1891 assert(const_value->u32[0] == 0);
1892 return fs_reg();
1893 }
1894
1895 return get_nir_src(*offset_src);
1896 }
1897
1898 void
1899 fs_visitor::nir_emit_vs_intrinsic(const fs_builder &bld,
1900 nir_intrinsic_instr *instr)
1901 {
1902 assert(stage == MESA_SHADER_VERTEX);
1903
1904 fs_reg dest;
1905 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
1906 dest = get_nir_dest(instr->dest);
1907
1908 switch (instr->intrinsic) {
1909 case nir_intrinsic_load_vertex_id:
1910 unreachable("should be lowered by lower_vertex_id()");
1911
1912 case nir_intrinsic_load_vertex_id_zero_base:
1913 case nir_intrinsic_load_base_vertex:
1914 case nir_intrinsic_load_instance_id:
1915 case nir_intrinsic_load_base_instance:
1916 case nir_intrinsic_load_draw_id: {
1917 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
1918 fs_reg val = nir_system_values[sv];
1919 assert(val.file != BAD_FILE);
1920 dest.type = val.type;
1921 bld.MOV(dest, val);
1922 break;
1923 }
1924
1925 default:
1926 nir_emit_intrinsic(bld, instr);
1927 break;
1928 }
1929 }
1930
1931 void
1932 fs_visitor::nir_emit_tcs_intrinsic(const fs_builder &bld,
1933 nir_intrinsic_instr *instr)
1934 {
1935 assert(stage == MESA_SHADER_TESS_CTRL);
1936 struct brw_tcs_prog_key *tcs_key = (struct brw_tcs_prog_key *) key;
1937 struct brw_tcs_prog_data *tcs_prog_data =
1938 (struct brw_tcs_prog_data *) prog_data;
1939
1940 fs_reg dst;
1941 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
1942 dst = get_nir_dest(instr->dest);
1943
1944 switch (instr->intrinsic) {
1945 case nir_intrinsic_load_primitive_id:
1946 bld.MOV(dst, fs_reg(brw_vec1_grf(0, 1)));
1947 break;
1948 case nir_intrinsic_load_invocation_id:
1949 bld.MOV(retype(dst, invocation_id.type), invocation_id);
1950 break;
1951 case nir_intrinsic_load_patch_vertices_in:
1952 bld.MOV(retype(dst, BRW_REGISTER_TYPE_D),
1953 brw_imm_d(tcs_key->input_vertices));
1954 break;
1955
1956 case nir_intrinsic_barrier: {
1957 if (tcs_prog_data->instances == 1)
1958 break;
1959
1960 fs_reg m0 = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1961 fs_reg m0_2 = byte_offset(m0, 2 * sizeof(uint32_t));
1962
1963 const fs_builder fwa_bld = bld.exec_all();
1964
1965 /* Zero the message header */
1966 fwa_bld.MOV(m0, brw_imm_ud(0u));
1967
1968 /* Copy "Barrier ID" from r0.2, bits 16:13 */
1969 fwa_bld.AND(m0_2, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD),
1970 brw_imm_ud(INTEL_MASK(16, 13)));
1971
1972 /* Shift it up to bits 27:24. */
1973 fwa_bld.SHL(m0_2, m0_2, brw_imm_ud(11));
1974
1975 /* Set the Barrier Count and the enable bit */
1976 fwa_bld.OR(m0_2, m0_2,
1977 brw_imm_ud(tcs_prog_data->instances << 8 | (1 << 15)));
1978
1979 bld.emit(SHADER_OPCODE_BARRIER, bld.null_reg_ud(), m0);
1980 break;
1981 }
1982
1983 case nir_intrinsic_load_input:
1984 unreachable("nir_lower_io should never give us these.");
1985 break;
1986
1987 case nir_intrinsic_load_per_vertex_input: {
1988 fs_reg indirect_offset = get_indirect_offset(instr);
1989 unsigned imm_offset = instr->const_index[0];
1990
1991 const nir_src &vertex_src = instr->src[0];
1992 nir_const_value *vertex_const = nir_src_as_const_value(vertex_src);
1993
1994 fs_inst *inst;
1995
1996 fs_reg icp_handle;
1997
1998 if (vertex_const) {
1999 /* Emit a MOV to resolve <0,1,0> regioning. */
2000 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2001 bld.MOV(icp_handle,
2002 retype(brw_vec1_grf(1 + (vertex_const->i32[0] >> 3),
2003 vertex_const->i32[0] & 7),
2004 BRW_REGISTER_TYPE_UD));
2005 } else if (tcs_prog_data->instances == 1 &&
2006 vertex_src.is_ssa &&
2007 vertex_src.ssa->parent_instr->type == nir_instr_type_intrinsic &&
2008 nir_instr_as_intrinsic(vertex_src.ssa->parent_instr)->intrinsic == nir_intrinsic_load_invocation_id) {
2009 /* For the common case of only 1 instance, an array index of
2010 * gl_InvocationID means reading g1. Skip all the indirect work.
2011 */
2012 icp_handle = retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD);
2013 } else {
2014 /* The vertex index is non-constant. We need to use indirect
2015 * addressing to fetch the proper URB handle.
2016 */
2017 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2018
2019 /* Each ICP handle is a single DWord (4 bytes) */
2020 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2021 bld.SHL(vertex_offset_bytes,
2022 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2023 brw_imm_ud(2u));
2024
2025 /* Start at g1. We might read up to 4 registers. */
2026 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2027 fs_reg(brw_vec8_grf(1, 0)), vertex_offset_bytes,
2028 brw_imm_ud(4 * REG_SIZE));
2029 }
2030
2031 if (indirect_offset.file == BAD_FILE) {
2032 /* Constant indexing - use global offset. */
2033 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
2034 inst->offset = imm_offset;
2035 inst->mlen = 1;
2036 inst->base_mrf = -1;
2037 inst->regs_written = instr->num_components;
2038 } else {
2039 /* Indirect indexing - use per-slot offsets as well. */
2040 const fs_reg srcs[] = { icp_handle, indirect_offset };
2041 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2042 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2043
2044 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst, payload);
2045 inst->offset = imm_offset;
2046 inst->base_mrf = -1;
2047 inst->mlen = 2;
2048 inst->regs_written = instr->num_components;
2049 }
2050
2051 /* Copy the temporary to the destination to deal with writemasking.
2052 *
2053 * Also attempt to deal with gl_PointSize being in the .w component.
2054 */
2055 if (inst->offset == 0 && indirect_offset.file == BAD_FILE) {
2056 inst->dst = bld.vgrf(dst.type, 4);
2057 inst->regs_written = 4;
2058 bld.MOV(dst, offset(inst->dst, bld, 3));
2059 }
2060 break;
2061 }
2062
2063 case nir_intrinsic_load_output:
2064 case nir_intrinsic_load_per_vertex_output: {
2065 fs_reg indirect_offset = get_indirect_offset(instr);
2066 unsigned imm_offset = instr->const_index[0];
2067
2068 fs_inst *inst;
2069 if (indirect_offset.file == BAD_FILE) {
2070 /* Replicate the patch handle to all enabled channels */
2071 fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2072 bld.MOV(patch_handle,
2073 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD));
2074
2075 if (imm_offset == 0) {
2076 /* This is a read of gl_TessLevelInner[], which lives in the
2077 * Patch URB header. The layout depends on the domain.
2078 */
2079 dst.type = BRW_REGISTER_TYPE_F;
2080 switch (tcs_key->tes_primitive_mode) {
2081 case GL_QUADS: {
2082 /* DWords 3-2 (reversed) */
2083 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 4);
2084
2085 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, patch_handle);
2086 inst->offset = 0;
2087 inst->mlen = 1;
2088 inst->base_mrf = -1;
2089 inst->regs_written = 4;
2090
2091 /* dst.xy = tmp.wz */
2092 bld.MOV(dst, offset(tmp, bld, 3));
2093 bld.MOV(offset(dst, bld, 1), offset(tmp, bld, 2));
2094 break;
2095 }
2096 case GL_TRIANGLES:
2097 /* DWord 4; hardcode offset = 1 and regs_written = 1 */
2098 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, patch_handle);
2099 inst->offset = 1;
2100 inst->mlen = 1;
2101 inst->base_mrf = -1;
2102 inst->regs_written = 1;
2103 break;
2104 case GL_ISOLINES:
2105 /* All channels are undefined. */
2106 break;
2107 default:
2108 unreachable("Bogus tessellation domain");
2109 }
2110 } else if (imm_offset == 1) {
2111 /* This is a read of gl_TessLevelOuter[], which lives in the
2112 * Patch URB header. The layout depends on the domain.
2113 */
2114 dst.type = BRW_REGISTER_TYPE_F;
2115
2116 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 4);
2117 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, patch_handle);
2118 inst->offset = 1;
2119 inst->mlen = 1;
2120 inst->base_mrf = -1;
2121 inst->regs_written = 4;
2122
2123 /* Reswizzle: WZYX */
2124 fs_reg srcs[4] = {
2125 offset(tmp, bld, 3),
2126 offset(tmp, bld, 2),
2127 offset(tmp, bld, 1),
2128 offset(tmp, bld, 0),
2129 };
2130
2131 unsigned num_components;
2132 switch (tcs_key->tes_primitive_mode) {
2133 case GL_QUADS:
2134 num_components = 4;
2135 break;
2136 case GL_TRIANGLES:
2137 num_components = 3;
2138 break;
2139 case GL_ISOLINES:
2140 /* Isolines are not reversed; swizzle .zw -> .xy */
2141 srcs[0] = offset(tmp, bld, 2);
2142 srcs[1] = offset(tmp, bld, 3);
2143 num_components = 2;
2144 break;
2145 default:
2146 unreachable("Bogus tessellation domain");
2147 }
2148 bld.LOAD_PAYLOAD(dst, srcs, num_components, 0);
2149 } else {
2150 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, patch_handle);
2151 inst->offset = imm_offset;
2152 inst->mlen = 1;
2153 inst->base_mrf = -1;
2154 inst->regs_written = instr->num_components;
2155 }
2156 } else {
2157 /* Indirect indexing - use per-slot offsets as well. */
2158 const fs_reg srcs[] = {
2159 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD),
2160 indirect_offset
2161 };
2162 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2163 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2164
2165 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst, payload);
2166 inst->offset = imm_offset;
2167 inst->mlen = 2;
2168 inst->base_mrf = -1;
2169 inst->regs_written = instr->num_components;
2170 }
2171 break;
2172 }
2173
2174 case nir_intrinsic_store_output:
2175 case nir_intrinsic_store_per_vertex_output: {
2176 fs_reg value = get_nir_src(instr->src[0]);
2177 fs_reg indirect_offset = get_indirect_offset(instr);
2178 unsigned imm_offset = instr->const_index[0];
2179 unsigned swiz = BRW_SWIZZLE_XYZW;
2180 unsigned mask = instr->const_index[1];
2181 unsigned header_regs = 0;
2182 fs_reg srcs[7];
2183 srcs[header_regs++] = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD);
2184
2185 if (indirect_offset.file != BAD_FILE) {
2186 srcs[header_regs++] = indirect_offset;
2187 } else if (!is_passthrough_shader) {
2188 if (imm_offset == 0) {
2189 value.type = BRW_REGISTER_TYPE_F;
2190
2191 mask &= (1 << tesslevel_inner_components(tcs_key->tes_primitive_mode)) - 1;
2192
2193 /* This is a write to gl_TessLevelInner[], which lives in the
2194 * Patch URB header. The layout depends on the domain.
2195 */
2196 switch (tcs_key->tes_primitive_mode) {
2197 case GL_QUADS:
2198 /* gl_TessLevelInner[].xy lives at DWords 3-2 (reversed).
2199 * We use an XXYX swizzle to reverse put .xy in the .wz
2200 * channels, and use a .zw writemask.
2201 */
2202 mask = writemask_for_backwards_vector(mask);
2203 swiz = BRW_SWIZZLE4(0, 0, 1, 0);
2204 break;
2205 case GL_TRIANGLES:
2206 /* gl_TessLevelInner[].x lives at DWord 4, so we set the
2207 * writemask to X and bump the URB offset by 1.
2208 */
2209 imm_offset = 1;
2210 break;
2211 case GL_ISOLINES:
2212 /* Skip; gl_TessLevelInner[] doesn't exist for isolines. */
2213 return;
2214 default:
2215 unreachable("Bogus tessellation domain");
2216 }
2217 } else if (imm_offset == 1) {
2218 /* This is a write to gl_TessLevelOuter[] which lives in the
2219 * Patch URB Header at DWords 4-7. However, it's reversed, so
2220 * instead of .xyzw we have .wzyx.
2221 */
2222 value.type = BRW_REGISTER_TYPE_F;
2223
2224 mask &= (1 << tesslevel_outer_components(tcs_key->tes_primitive_mode)) - 1;
2225
2226 if (tcs_key->tes_primitive_mode == GL_ISOLINES) {
2227 /* Isolines .xy should be stored in .zw, in order. */
2228 swiz = BRW_SWIZZLE4(0, 0, 0, 1);
2229 mask <<= 2;
2230 } else {
2231 /* Other domains are reversed; store .wzyx instead of .xyzw */
2232 swiz = BRW_SWIZZLE_WZYX;
2233 mask = writemask_for_backwards_vector(mask);
2234 }
2235 }
2236 }
2237
2238 if (mask == 0)
2239 break;
2240
2241 unsigned num_components = _mesa_fls(mask);
2242 enum opcode opcode;
2243
2244 if (mask != WRITEMASK_XYZW) {
2245 srcs[header_regs++] = brw_imm_ud(mask << 16);
2246 opcode = indirect_offset.file != BAD_FILE ?
2247 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT :
2248 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
2249 } else {
2250 opcode = indirect_offset.file != BAD_FILE ?
2251 SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT :
2252 SHADER_OPCODE_URB_WRITE_SIMD8;
2253 }
2254
2255 for (unsigned i = 0; i < num_components; i++) {
2256 if (mask & (1 << i))
2257 srcs[header_regs + i] = offset(value, bld, BRW_GET_SWZ(swiz, i));
2258 }
2259
2260 unsigned mlen = header_regs + num_components;
2261
2262 fs_reg payload =
2263 bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
2264 bld.LOAD_PAYLOAD(payload, srcs, mlen, header_regs);
2265
2266 fs_inst *inst = bld.emit(opcode, bld.null_reg_ud(), payload);
2267 inst->offset = imm_offset;
2268 inst->mlen = mlen;
2269 inst->base_mrf = -1;
2270 break;
2271 }
2272
2273 default:
2274 nir_emit_intrinsic(bld, instr);
2275 break;
2276 }
2277 }
2278
2279 void
2280 fs_visitor::nir_emit_tes_intrinsic(const fs_builder &bld,
2281 nir_intrinsic_instr *instr)
2282 {
2283 assert(stage == MESA_SHADER_TESS_EVAL);
2284 struct brw_tes_prog_data *tes_prog_data = (struct brw_tes_prog_data *) prog_data;
2285
2286 fs_reg dest;
2287 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2288 dest = get_nir_dest(instr->dest);
2289
2290 switch (instr->intrinsic) {
2291 case nir_intrinsic_load_primitive_id:
2292 bld.MOV(dest, fs_reg(brw_vec1_grf(0, 1)));
2293 break;
2294 case nir_intrinsic_load_tess_coord:
2295 /* gl_TessCoord is part of the payload in g1-3 */
2296 for (unsigned i = 0; i < 3; i++) {
2297 bld.MOV(offset(dest, bld, i), fs_reg(brw_vec8_grf(1 + i, 0)));
2298 }
2299 break;
2300
2301 case nir_intrinsic_load_tess_level_outer:
2302 /* When the TES reads gl_TessLevelOuter, we ensure that the patch header
2303 * appears as a push-model input. So, we can simply use the ATTR file
2304 * rather than issuing URB read messages. The data is stored in the
2305 * high DWords in reverse order - DWord 7 contains .x, DWord 6 contains
2306 * .y, and so on.
2307 */
2308 switch (tes_prog_data->domain) {
2309 case BRW_TESS_DOMAIN_QUAD:
2310 for (unsigned i = 0; i < 4; i++)
2311 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 7 - i));
2312 break;
2313 case BRW_TESS_DOMAIN_TRI:
2314 for (unsigned i = 0; i < 3; i++)
2315 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 7 - i));
2316 break;
2317 case BRW_TESS_DOMAIN_ISOLINE:
2318 for (unsigned i = 0; i < 2; i++)
2319 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 7 - i));
2320 break;
2321 }
2322 break;
2323
2324 case nir_intrinsic_load_tess_level_inner:
2325 /* When the TES reads gl_TessLevelInner, we ensure that the patch header
2326 * appears as a push-model input. So, we can simply use the ATTR file
2327 * rather than issuing URB read messages.
2328 */
2329 switch (tes_prog_data->domain) {
2330 case BRW_TESS_DOMAIN_QUAD:
2331 bld.MOV(dest, component(fs_reg(ATTR, 0), 3));
2332 bld.MOV(offset(dest, bld, 1), component(fs_reg(ATTR, 0), 2));
2333 break;
2334 case BRW_TESS_DOMAIN_TRI:
2335 bld.MOV(dest, component(fs_reg(ATTR, 0), 4));
2336 break;
2337 case BRW_TESS_DOMAIN_ISOLINE:
2338 /* ignore - value is undefined */
2339 break;
2340 }
2341 break;
2342
2343 case nir_intrinsic_load_input:
2344 case nir_intrinsic_load_per_vertex_input: {
2345 fs_reg indirect_offset = get_indirect_offset(instr);
2346 unsigned imm_offset = instr->const_index[0];
2347
2348 fs_inst *inst;
2349 if (indirect_offset.file == BAD_FILE) {
2350 /* Arbitrarily only push up to 32 vec4 slots worth of data,
2351 * which is 16 registers (since each holds 2 vec4 slots).
2352 */
2353 const unsigned max_push_slots = 32;
2354 if (imm_offset < max_push_slots) {
2355 fs_reg src = fs_reg(ATTR, imm_offset / 2, dest.type);
2356 for (int i = 0; i < instr->num_components; i++) {
2357 bld.MOV(offset(dest, bld, i),
2358 component(src, 4 * (imm_offset % 2) + i));
2359 }
2360 tes_prog_data->base.urb_read_length =
2361 MAX2(tes_prog_data->base.urb_read_length,
2362 DIV_ROUND_UP(imm_offset + 1, 2));
2363 } else {
2364 /* Replicate the patch handle to all enabled channels */
2365 const fs_reg srcs[] = {
2366 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD)
2367 };
2368 fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2369 bld.LOAD_PAYLOAD(patch_handle, srcs, ARRAY_SIZE(srcs), 0);
2370
2371 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dest, patch_handle);
2372 inst->mlen = 1;
2373 inst->offset = imm_offset;
2374 inst->base_mrf = -1;
2375 inst->regs_written = instr->num_components;
2376 }
2377 } else {
2378 /* Indirect indexing - use per-slot offsets as well. */
2379 const fs_reg srcs[] = {
2380 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD),
2381 indirect_offset
2382 };
2383 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2384 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2385
2386 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dest, payload);
2387 inst->mlen = 2;
2388 inst->offset = imm_offset;
2389 inst->base_mrf = -1;
2390 inst->regs_written = instr->num_components;
2391 }
2392 break;
2393 }
2394 default:
2395 nir_emit_intrinsic(bld, instr);
2396 break;
2397 }
2398 }
2399
2400 void
2401 fs_visitor::nir_emit_gs_intrinsic(const fs_builder &bld,
2402 nir_intrinsic_instr *instr)
2403 {
2404 assert(stage == MESA_SHADER_GEOMETRY);
2405 fs_reg indirect_offset;
2406
2407 fs_reg dest;
2408 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2409 dest = get_nir_dest(instr->dest);
2410
2411 switch (instr->intrinsic) {
2412 case nir_intrinsic_load_primitive_id:
2413 assert(stage == MESA_SHADER_GEOMETRY);
2414 assert(((struct brw_gs_prog_data *)prog_data)->include_primitive_id);
2415 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
2416 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD));
2417 break;
2418
2419 case nir_intrinsic_load_input:
2420 unreachable("load_input intrinsics are invalid for the GS stage");
2421
2422 case nir_intrinsic_load_per_vertex_input:
2423 emit_gs_input_load(dest, instr->src[0], instr->const_index[0],
2424 instr->src[1], instr->num_components);
2425 break;
2426
2427 case nir_intrinsic_emit_vertex_with_counter:
2428 emit_gs_vertex(instr->src[0], instr->const_index[0]);
2429 break;
2430
2431 case nir_intrinsic_end_primitive_with_counter:
2432 emit_gs_end_primitive(instr->src[0]);
2433 break;
2434
2435 case nir_intrinsic_set_vertex_count:
2436 bld.MOV(this->final_gs_vertex_count, get_nir_src(instr->src[0]));
2437 break;
2438
2439 case nir_intrinsic_load_invocation_id: {
2440 fs_reg val = nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
2441 assert(val.file != BAD_FILE);
2442 dest.type = val.type;
2443 bld.MOV(dest, val);
2444 break;
2445 }
2446
2447 default:
2448 nir_emit_intrinsic(bld, instr);
2449 break;
2450 }
2451 }
2452
2453 void
2454 fs_visitor::nir_emit_fs_intrinsic(const fs_builder &bld,
2455 nir_intrinsic_instr *instr)
2456 {
2457 assert(stage == MESA_SHADER_FRAGMENT);
2458 struct brw_wm_prog_data *wm_prog_data =
2459 (struct brw_wm_prog_data *) prog_data;
2460 const struct brw_wm_prog_key *wm_key = (const struct brw_wm_prog_key *) key;
2461
2462 fs_reg dest;
2463 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2464 dest = get_nir_dest(instr->dest);
2465
2466 switch (instr->intrinsic) {
2467 case nir_intrinsic_load_front_face:
2468 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D),
2469 *emit_frontfacing_interpolation());
2470 break;
2471
2472 case nir_intrinsic_load_sample_pos: {
2473 fs_reg sample_pos = nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
2474 assert(sample_pos.file != BAD_FILE);
2475 dest.type = sample_pos.type;
2476 bld.MOV(dest, sample_pos);
2477 bld.MOV(offset(dest, bld, 1), offset(sample_pos, bld, 1));
2478 break;
2479 }
2480
2481 case nir_intrinsic_load_helper_invocation:
2482 case nir_intrinsic_load_sample_mask_in:
2483 case nir_intrinsic_load_sample_id: {
2484 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
2485 fs_reg val = nir_system_values[sv];
2486 assert(val.file != BAD_FILE);
2487 dest.type = val.type;
2488 bld.MOV(dest, val);
2489 break;
2490 }
2491
2492 case nir_intrinsic_discard:
2493 case nir_intrinsic_discard_if: {
2494 /* We track our discarded pixels in f0.1. By predicating on it, we can
2495 * update just the flag bits that aren't yet discarded. If there's no
2496 * condition, we emit a CMP of g0 != g0, so all currently executing
2497 * channels will get turned off.
2498 */
2499 fs_inst *cmp;
2500 if (instr->intrinsic == nir_intrinsic_discard_if) {
2501 cmp = bld.CMP(bld.null_reg_f(), get_nir_src(instr->src[0]),
2502 brw_imm_d(0), BRW_CONDITIONAL_Z);
2503 } else {
2504 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
2505 BRW_REGISTER_TYPE_UW));
2506 cmp = bld.CMP(bld.null_reg_f(), some_reg, some_reg, BRW_CONDITIONAL_NZ);
2507 }
2508 cmp->predicate = BRW_PREDICATE_NORMAL;
2509 cmp->flag_subreg = 1;
2510
2511 if (devinfo->gen >= 6) {
2512 emit_discard_jump();
2513 }
2514 break;
2515 }
2516
2517 case nir_intrinsic_interp_var_at_centroid:
2518 case nir_intrinsic_interp_var_at_sample:
2519 case nir_intrinsic_interp_var_at_offset: {
2520 /* Handle ARB_gpu_shader5 interpolation intrinsics
2521 *
2522 * It's worth a quick word of explanation as to why we handle the full
2523 * variable-based interpolation intrinsic rather than a lowered version
2524 * with like we do for other inputs. We have to do that because the way
2525 * we set up inputs doesn't allow us to use the already setup inputs for
2526 * interpolation. At the beginning of the shader, we go through all of
2527 * the input variables and do the initial interpolation and put it in
2528 * the nir_inputs array based on its location as determined in
2529 * nir_lower_io. If the input isn't used, dead code cleans up and
2530 * everything works fine. However, when we get to the ARB_gpu_shader5
2531 * interpolation intrinsics, we need to reinterpolate the input
2532 * differently. If we used an intrinsic that just had an index it would
2533 * only give us the offset into the nir_inputs array. However, this is
2534 * useless because that value is post-interpolation and we need
2535 * pre-interpolation. In order to get the actual location of the bits
2536 * we get from the vertex fetching hardware, we need the variable.
2537 */
2538 wm_prog_data->pulls_bary = true;
2539
2540 fs_reg dst_xy = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
2541 const glsl_interp_qualifier interpolation =
2542 (glsl_interp_qualifier) instr->variables[0]->var->data.interpolation;
2543
2544 switch (instr->intrinsic) {
2545 case nir_intrinsic_interp_var_at_centroid:
2546 emit_pixel_interpolater_send(bld,
2547 FS_OPCODE_INTERPOLATE_AT_CENTROID,
2548 dst_xy,
2549 fs_reg(), /* src */
2550 brw_imm_ud(0u),
2551 interpolation);
2552 break;
2553
2554 case nir_intrinsic_interp_var_at_sample: {
2555 if (!wm_key->multisample_fbo) {
2556 /* From the ARB_gpu_shader5 specification:
2557 * "If multisample buffers are not available, the input varying
2558 * will be evaluated at the center of the pixel."
2559 */
2560 emit_pixel_interpolater_send(bld,
2561 FS_OPCODE_INTERPOLATE_AT_CENTROID,
2562 dst_xy,
2563 fs_reg(), /* src */
2564 brw_imm_ud(0u),
2565 interpolation);
2566 break;
2567 }
2568
2569 nir_const_value *const_sample = nir_src_as_const_value(instr->src[0]);
2570
2571 if (const_sample) {
2572 unsigned msg_data = const_sample->i32[0] << 4;
2573
2574 emit_pixel_interpolater_send(bld,
2575 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
2576 dst_xy,
2577 fs_reg(), /* src */
2578 brw_imm_ud(msg_data),
2579 interpolation);
2580 } else {
2581 const fs_reg sample_src = retype(get_nir_src(instr->src[0]),
2582 BRW_REGISTER_TYPE_UD);
2583
2584 if (nir_src_is_dynamically_uniform(instr->src[0])) {
2585 const fs_reg sample_id = bld.emit_uniformize(sample_src);
2586 const fs_reg msg_data = vgrf(glsl_type::uint_type);
2587 bld.exec_all().group(1, 0)
2588 .SHL(msg_data, sample_id, brw_imm_ud(4u));
2589 emit_pixel_interpolater_send(bld,
2590 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
2591 dst_xy,
2592 fs_reg(), /* src */
2593 msg_data,
2594 interpolation);
2595 } else {
2596 /* Make a loop that sends a message to the pixel interpolater
2597 * for the sample number in each live channel. If there are
2598 * multiple channels with the same sample number then these
2599 * will be handled simultaneously with a single interation of
2600 * the loop.
2601 */
2602 bld.emit(BRW_OPCODE_DO);
2603
2604 /* Get the next live sample number into sample_id_reg */
2605 const fs_reg sample_id = bld.emit_uniformize(sample_src);
2606
2607 /* Set the flag register so that we can perform the send
2608 * message on all channels that have the same sample number
2609 */
2610 bld.CMP(bld.null_reg_ud(),
2611 sample_src, sample_id,
2612 BRW_CONDITIONAL_EQ);
2613 const fs_reg msg_data = vgrf(glsl_type::uint_type);
2614 bld.exec_all().group(1, 0)
2615 .SHL(msg_data, sample_id, brw_imm_ud(4u));
2616 fs_inst *inst =
2617 emit_pixel_interpolater_send(bld,
2618 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
2619 dst_xy,
2620 fs_reg(), /* src */
2621 msg_data,
2622 interpolation);
2623 set_predicate(BRW_PREDICATE_NORMAL, inst);
2624
2625 /* Continue the loop if there are any live channels left */
2626 set_predicate_inv(BRW_PREDICATE_NORMAL,
2627 true, /* inverse */
2628 bld.emit(BRW_OPCODE_WHILE));
2629 }
2630 }
2631
2632 break;
2633 }
2634
2635 case nir_intrinsic_interp_var_at_offset: {
2636 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
2637
2638 if (const_offset) {
2639 unsigned off_x = MIN2((int)(const_offset->f32[0] * 16), 7) & 0xf;
2640 unsigned off_y = MIN2((int)(const_offset->f32[1] * 16), 7) & 0xf;
2641
2642 emit_pixel_interpolater_send(bld,
2643 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET,
2644 dst_xy,
2645 fs_reg(), /* src */
2646 brw_imm_ud(off_x | (off_y << 4)),
2647 interpolation);
2648 } else {
2649 fs_reg src = vgrf(glsl_type::ivec2_type);
2650 fs_reg offset_src = retype(get_nir_src(instr->src[0]),
2651 BRW_REGISTER_TYPE_F);
2652 for (int i = 0; i < 2; i++) {
2653 fs_reg temp = vgrf(glsl_type::float_type);
2654 bld.MUL(temp, offset(offset_src, bld, i), brw_imm_f(16.0f));
2655 fs_reg itemp = vgrf(glsl_type::int_type);
2656 bld.MOV(itemp, temp); /* float to int */
2657
2658 /* Clamp the upper end of the range to +7/16.
2659 * ARB_gpu_shader5 requires that we support a maximum offset
2660 * of +0.5, which isn't representable in a S0.4 value -- if
2661 * we didn't clamp it, we'd end up with -8/16, which is the
2662 * opposite of what the shader author wanted.
2663 *
2664 * This is legal due to ARB_gpu_shader5's quantization
2665 * rules:
2666 *
2667 * "Not all values of <offset> may be supported; x and y
2668 * offsets may be rounded to fixed-point values with the
2669 * number of fraction bits given by the
2670 * implementation-dependent constant
2671 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
2672 */
2673 set_condmod(BRW_CONDITIONAL_L,
2674 bld.SEL(offset(src, bld, i), itemp, brw_imm_d(7)));
2675 }
2676
2677 const enum opcode opcode = FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET;
2678 emit_pixel_interpolater_send(bld,
2679 opcode,
2680 dst_xy,
2681 src,
2682 brw_imm_ud(0u),
2683 interpolation);
2684 }
2685 break;
2686 }
2687
2688 default:
2689 unreachable("Invalid intrinsic");
2690 }
2691
2692 for (unsigned j = 0; j < instr->num_components; j++) {
2693 fs_reg src = interp_reg(instr->variables[0]->var->data.location, j);
2694 src.type = dest.type;
2695
2696 bld.emit(FS_OPCODE_LINTERP, dest, dst_xy, src);
2697 dest = offset(dest, bld, 1);
2698 }
2699 break;
2700 }
2701 default:
2702 nir_emit_intrinsic(bld, instr);
2703 break;
2704 }
2705 }
2706
2707 void
2708 fs_visitor::nir_emit_cs_intrinsic(const fs_builder &bld,
2709 nir_intrinsic_instr *instr)
2710 {
2711 assert(stage == MESA_SHADER_COMPUTE);
2712 struct brw_cs_prog_data *cs_prog_data =
2713 (struct brw_cs_prog_data *) prog_data;
2714
2715 fs_reg dest;
2716 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2717 dest = get_nir_dest(instr->dest);
2718
2719 switch (instr->intrinsic) {
2720 case nir_intrinsic_barrier:
2721 emit_barrier();
2722 cs_prog_data->uses_barrier = true;
2723 break;
2724
2725 case nir_intrinsic_load_local_invocation_id:
2726 case nir_intrinsic_load_work_group_id: {
2727 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
2728 fs_reg val = nir_system_values[sv];
2729 assert(val.file != BAD_FILE);
2730 dest.type = val.type;
2731 for (unsigned i = 0; i < 3; i++)
2732 bld.MOV(offset(dest, bld, i), offset(val, bld, i));
2733 break;
2734 }
2735
2736 case nir_intrinsic_load_num_work_groups: {
2737 const unsigned surface =
2738 cs_prog_data->binding_table.work_groups_start;
2739
2740 cs_prog_data->uses_num_work_groups = true;
2741
2742 fs_reg surf_index = brw_imm_ud(surface);
2743 brw_mark_surface_used(prog_data, surface);
2744
2745 /* Read the 3 GLuint components of gl_NumWorkGroups */
2746 for (unsigned i = 0; i < 3; i++) {
2747 fs_reg read_result =
2748 emit_untyped_read(bld, surf_index,
2749 brw_imm_ud(i << 2),
2750 1 /* dims */, 1 /* size */,
2751 BRW_PREDICATE_NONE);
2752 read_result.type = dest.type;
2753 bld.MOV(dest, read_result);
2754 dest = offset(dest, bld, 1);
2755 }
2756 break;
2757 }
2758
2759 case nir_intrinsic_shared_atomic_add:
2760 nir_emit_shared_atomic(bld, BRW_AOP_ADD, instr);
2761 break;
2762 case nir_intrinsic_shared_atomic_imin:
2763 nir_emit_shared_atomic(bld, BRW_AOP_IMIN, instr);
2764 break;
2765 case nir_intrinsic_shared_atomic_umin:
2766 nir_emit_shared_atomic(bld, BRW_AOP_UMIN, instr);
2767 break;
2768 case nir_intrinsic_shared_atomic_imax:
2769 nir_emit_shared_atomic(bld, BRW_AOP_IMAX, instr);
2770 break;
2771 case nir_intrinsic_shared_atomic_umax:
2772 nir_emit_shared_atomic(bld, BRW_AOP_UMAX, instr);
2773 break;
2774 case nir_intrinsic_shared_atomic_and:
2775 nir_emit_shared_atomic(bld, BRW_AOP_AND, instr);
2776 break;
2777 case nir_intrinsic_shared_atomic_or:
2778 nir_emit_shared_atomic(bld, BRW_AOP_OR, instr);
2779 break;
2780 case nir_intrinsic_shared_atomic_xor:
2781 nir_emit_shared_atomic(bld, BRW_AOP_XOR, instr);
2782 break;
2783 case nir_intrinsic_shared_atomic_exchange:
2784 nir_emit_shared_atomic(bld, BRW_AOP_MOV, instr);
2785 break;
2786 case nir_intrinsic_shared_atomic_comp_swap:
2787 nir_emit_shared_atomic(bld, BRW_AOP_CMPWR, instr);
2788 break;
2789
2790 case nir_intrinsic_load_shared: {
2791 assert(devinfo->gen >= 7);
2792
2793 fs_reg surf_index = brw_imm_ud(GEN7_BTI_SLM);
2794
2795 /* Get the offset to read from */
2796 fs_reg offset_reg;
2797 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
2798 if (const_offset) {
2799 offset_reg = brw_imm_ud(instr->const_index[0] + const_offset->u32[0]);
2800 } else {
2801 offset_reg = vgrf(glsl_type::uint_type);
2802 bld.ADD(offset_reg,
2803 retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_UD),
2804 brw_imm_ud(instr->const_index[0]));
2805 }
2806
2807 /* Read the vector */
2808 fs_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
2809 1 /* dims */,
2810 instr->num_components,
2811 BRW_PREDICATE_NONE);
2812 read_result.type = dest.type;
2813 for (int i = 0; i < instr->num_components; i++)
2814 bld.MOV(offset(dest, bld, i), offset(read_result, bld, i));
2815
2816 break;
2817 }
2818
2819 case nir_intrinsic_store_shared: {
2820 assert(devinfo->gen >= 7);
2821
2822 /* Block index */
2823 fs_reg surf_index = brw_imm_ud(GEN7_BTI_SLM);
2824
2825 /* Value */
2826 fs_reg val_reg = get_nir_src(instr->src[0]);
2827
2828 /* Writemask */
2829 unsigned writemask = instr->const_index[1];
2830
2831 /* Combine groups of consecutive enabled channels in one write
2832 * message. We use ffs to find the first enabled channel and then ffs on
2833 * the bit-inverse, down-shifted writemask to determine the length of
2834 * the block of enabled bits.
2835 */
2836 while (writemask) {
2837 unsigned first_component = ffs(writemask) - 1;
2838 unsigned length = ffs(~(writemask >> first_component)) - 1;
2839 fs_reg offset_reg;
2840
2841 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
2842 if (const_offset) {
2843 offset_reg = brw_imm_ud(instr->const_index[0] + const_offset->u32[0] +
2844 4 * first_component);
2845 } else {
2846 offset_reg = vgrf(glsl_type::uint_type);
2847 bld.ADD(offset_reg,
2848 retype(get_nir_src(instr->src[1]), BRW_REGISTER_TYPE_UD),
2849 brw_imm_ud(instr->const_index[0] + 4 * first_component));
2850 }
2851
2852 emit_untyped_write(bld, surf_index, offset_reg,
2853 offset(val_reg, bld, first_component),
2854 1 /* dims */, length,
2855 BRW_PREDICATE_NONE);
2856
2857 /* Clear the bits in the writemask that we just wrote, then try
2858 * again to see if more channels are left.
2859 */
2860 writemask &= (15 << (first_component + length));
2861 }
2862
2863 break;
2864 }
2865
2866 default:
2867 nir_emit_intrinsic(bld, instr);
2868 break;
2869 }
2870 }
2871
2872 void
2873 fs_visitor::nir_emit_intrinsic(const fs_builder &bld, nir_intrinsic_instr *instr)
2874 {
2875 fs_reg dest;
2876 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2877 dest = get_nir_dest(instr->dest);
2878
2879 switch (instr->intrinsic) {
2880 case nir_intrinsic_atomic_counter_inc:
2881 case nir_intrinsic_atomic_counter_dec:
2882 case nir_intrinsic_atomic_counter_read: {
2883 /* Get the arguments of the atomic intrinsic. */
2884 const fs_reg offset = get_nir_src(instr->src[0]);
2885 const unsigned surface = (stage_prog_data->binding_table.abo_start +
2886 instr->const_index[0]);
2887 fs_reg tmp;
2888
2889 /* Emit a surface read or atomic op. */
2890 switch (instr->intrinsic) {
2891 case nir_intrinsic_atomic_counter_read:
2892 tmp = emit_untyped_read(bld, brw_imm_ud(surface), offset, 1, 1);
2893 break;
2894
2895 case nir_intrinsic_atomic_counter_inc:
2896 tmp = emit_untyped_atomic(bld, brw_imm_ud(surface), offset, fs_reg(),
2897 fs_reg(), 1, 1, BRW_AOP_INC);
2898 break;
2899
2900 case nir_intrinsic_atomic_counter_dec:
2901 tmp = emit_untyped_atomic(bld, brw_imm_ud(surface), offset, fs_reg(),
2902 fs_reg(), 1, 1, BRW_AOP_PREDEC);
2903 break;
2904
2905 default:
2906 unreachable("Unreachable");
2907 }
2908
2909 /* Assign the result. */
2910 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD), tmp);
2911
2912 /* Mark the surface as used. */
2913 brw_mark_surface_used(stage_prog_data, surface);
2914 break;
2915 }
2916
2917 case nir_intrinsic_image_load:
2918 case nir_intrinsic_image_store:
2919 case nir_intrinsic_image_atomic_add:
2920 case nir_intrinsic_image_atomic_min:
2921 case nir_intrinsic_image_atomic_max:
2922 case nir_intrinsic_image_atomic_and:
2923 case nir_intrinsic_image_atomic_or:
2924 case nir_intrinsic_image_atomic_xor:
2925 case nir_intrinsic_image_atomic_exchange:
2926 case nir_intrinsic_image_atomic_comp_swap: {
2927 using namespace image_access;
2928
2929 /* Get the referenced image variable and type. */
2930 const nir_variable *var = instr->variables[0]->var;
2931 const glsl_type *type = var->type->without_array();
2932 const brw_reg_type base_type = get_image_base_type(type);
2933
2934 /* Get some metadata from the image intrinsic. */
2935 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
2936 const unsigned arr_dims = type->sampler_array ? 1 : 0;
2937 const unsigned surf_dims = type->coordinate_components() - arr_dims;
2938 const unsigned format = var->data.image.format;
2939
2940 /* Get the arguments of the image intrinsic. */
2941 const fs_reg image = get_nir_image_deref(instr->variables[0]);
2942 const fs_reg addr = retype(get_nir_src(instr->src[0]),
2943 BRW_REGISTER_TYPE_UD);
2944 const fs_reg src0 = (info->num_srcs >= 3 ?
2945 retype(get_nir_src(instr->src[2]), base_type) :
2946 fs_reg());
2947 const fs_reg src1 = (info->num_srcs >= 4 ?
2948 retype(get_nir_src(instr->src[3]), base_type) :
2949 fs_reg());
2950 fs_reg tmp;
2951
2952 /* Emit an image load, store or atomic op. */
2953 if (instr->intrinsic == nir_intrinsic_image_load)
2954 tmp = emit_image_load(bld, image, addr, surf_dims, arr_dims, format);
2955
2956 else if (instr->intrinsic == nir_intrinsic_image_store)
2957 emit_image_store(bld, image, addr, src0, surf_dims, arr_dims,
2958 var->data.image.write_only ? GL_NONE : format);
2959
2960 else
2961 tmp = emit_image_atomic(bld, image, addr, src0, src1,
2962 surf_dims, arr_dims, info->dest_components,
2963 get_image_atomic_op(instr->intrinsic, type));
2964
2965 /* Assign the result. */
2966 for (unsigned c = 0; c < info->dest_components; ++c)
2967 bld.MOV(offset(retype(dest, base_type), bld, c),
2968 offset(tmp, bld, c));
2969 break;
2970 }
2971
2972 case nir_intrinsic_memory_barrier_atomic_counter:
2973 case nir_intrinsic_memory_barrier_buffer:
2974 case nir_intrinsic_memory_barrier_image:
2975 case nir_intrinsic_memory_barrier: {
2976 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 16 / dispatch_width);
2977 bld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp)
2978 ->regs_written = 2;
2979 break;
2980 }
2981
2982 case nir_intrinsic_group_memory_barrier:
2983 case nir_intrinsic_memory_barrier_shared:
2984 /* We treat these workgroup-level barriers as no-ops. This should be
2985 * safe at present and as long as:
2986 *
2987 * - Memory access instructions are not subsequently reordered by the
2988 * compiler back-end.
2989 *
2990 * - All threads from a given compute shader workgroup fit within a
2991 * single subslice and therefore talk to the same HDC shared unit
2992 * what supposedly guarantees ordering and coherency between threads
2993 * from the same workgroup. This may change in the future when we
2994 * start splitting workgroups across multiple subslices.
2995 *
2996 * - The context is not in fault-and-stream mode, which could cause
2997 * memory transactions (including to SLM) prior to the barrier to be
2998 * replayed after the barrier if a pagefault occurs. This shouldn't
2999 * be a problem up to and including SKL because fault-and-stream is
3000 * not usable due to hardware issues, but that's likely to change in
3001 * the future.
3002 */
3003 break;
3004
3005 case nir_intrinsic_shader_clock: {
3006 /* We cannot do anything if there is an event, so ignore it for now */
3007 fs_reg shader_clock = get_timestamp(bld);
3008 const fs_reg srcs[] = { shader_clock.set_smear(0), shader_clock.set_smear(1) };
3009
3010 bld.LOAD_PAYLOAD(dest, srcs, ARRAY_SIZE(srcs), 0);
3011 break;
3012 }
3013
3014 case nir_intrinsic_image_size: {
3015 /* Get the referenced image variable and type. */
3016 const nir_variable *var = instr->variables[0]->var;
3017 const glsl_type *type = var->type->without_array();
3018
3019 /* Get the size of the image. */
3020 const fs_reg image = get_nir_image_deref(instr->variables[0]);
3021 const fs_reg size = offset(image, bld, BRW_IMAGE_PARAM_SIZE_OFFSET);
3022
3023 /* For 1DArray image types, the array index is stored in the Z component.
3024 * Fix this by swizzling the Z component to the Y component.
3025 */
3026 const bool is_1d_array_image =
3027 type->sampler_dimensionality == GLSL_SAMPLER_DIM_1D &&
3028 type->sampler_array;
3029
3030 /* For CubeArray images, we should count the number of cubes instead
3031 * of the number of faces. Fix it by dividing the (Z component) by 6.
3032 */
3033 const bool is_cube_array_image =
3034 type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE &&
3035 type->sampler_array;
3036
3037 /* Copy all the components. */
3038 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
3039 for (unsigned c = 0; c < info->dest_components; ++c) {
3040 if ((int)c >= type->coordinate_components()) {
3041 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3042 brw_imm_d(1));
3043 } else if (c == 1 && is_1d_array_image) {
3044 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3045 offset(size, bld, 2));
3046 } else if (c == 2 && is_cube_array_image) {
3047 bld.emit(SHADER_OPCODE_INT_QUOTIENT,
3048 offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3049 offset(size, bld, c), brw_imm_d(6));
3050 } else {
3051 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3052 offset(size, bld, c));
3053 }
3054 }
3055
3056 break;
3057 }
3058
3059 case nir_intrinsic_image_samples:
3060 /* The driver does not support multi-sampled images. */
3061 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), brw_imm_d(1));
3062 break;
3063
3064 case nir_intrinsic_load_uniform: {
3065 /* Offsets are in bytes but they should always be multiples of 4 */
3066 assert(instr->const_index[0] % 4 == 0);
3067
3068 fs_reg src(UNIFORM, instr->const_index[0] / 4, dest.type);
3069
3070 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3071 if (const_offset) {
3072 /* Offsets are in bytes but they should always be multiples of 4 */
3073 assert(const_offset->u32[0] % 4 == 0);
3074 src.reg_offset = const_offset->u32[0] / 4;
3075
3076 for (unsigned j = 0; j < instr->num_components; j++) {
3077 bld.MOV(offset(dest, bld, j), offset(src, bld, j));
3078 }
3079 } else {
3080 fs_reg indirect = retype(get_nir_src(instr->src[0]),
3081 BRW_REGISTER_TYPE_UD);
3082
3083 /* We need to pass a size to the MOV_INDIRECT but we don't want it to
3084 * go past the end of the uniform. In order to keep the n'th
3085 * component from running past, we subtract off the size of all but
3086 * one component of the vector.
3087 */
3088 assert(instr->const_index[1] >= instr->num_components * 4);
3089 unsigned read_size = instr->const_index[1] -
3090 (instr->num_components - 1) * 4;
3091
3092 for (unsigned j = 0; j < instr->num_components; j++) {
3093 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
3094 offset(dest, bld, j), offset(src, bld, j),
3095 indirect, brw_imm_ud(read_size));
3096 }
3097 }
3098 break;
3099 }
3100
3101 case nir_intrinsic_load_ubo: {
3102 nir_const_value *const_index = nir_src_as_const_value(instr->src[0]);
3103 fs_reg surf_index;
3104
3105 if (const_index) {
3106 const unsigned index = stage_prog_data->binding_table.ubo_start +
3107 const_index->u32[0];
3108 surf_index = brw_imm_ud(index);
3109 brw_mark_surface_used(prog_data, index);
3110 } else {
3111 /* The block index is not a constant. Evaluate the index expression
3112 * per-channel and add the base UBO index; we have to select a value
3113 * from any live channel.
3114 */
3115 surf_index = vgrf(glsl_type::uint_type);
3116 bld.ADD(surf_index, get_nir_src(instr->src[0]),
3117 brw_imm_ud(stage_prog_data->binding_table.ubo_start));
3118 surf_index = bld.emit_uniformize(surf_index);
3119
3120 /* Assume this may touch any UBO. It would be nice to provide
3121 * a tighter bound, but the array information is already lowered away.
3122 */
3123 brw_mark_surface_used(prog_data,
3124 stage_prog_data->binding_table.ubo_start +
3125 nir->info.num_ubos - 1);
3126 }
3127
3128 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
3129 if (const_offset == NULL) {
3130 fs_reg base_offset = retype(get_nir_src(instr->src[1]),
3131 BRW_REGISTER_TYPE_UD);
3132
3133 for (int i = 0; i < instr->num_components; i++)
3134 VARYING_PULL_CONSTANT_LOAD(bld, offset(dest, bld, i), surf_index,
3135 base_offset, i * 4);
3136 } else {
3137 fs_reg packed_consts = vgrf(glsl_type::float_type);
3138 packed_consts.type = dest.type;
3139
3140 struct brw_reg const_offset_reg = brw_imm_ud(const_offset->u32[0] & ~15);
3141 bld.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD, packed_consts,
3142 surf_index, const_offset_reg);
3143
3144 for (unsigned i = 0; i < instr->num_components; i++) {
3145 packed_consts.set_smear(const_offset->u32[0] % 16 / 4 + i);
3146
3147 /* The std140 packing rules don't allow vectors to cross 16-byte
3148 * boundaries, and a reg is 32 bytes.
3149 */
3150 assert(packed_consts.subreg_offset < 32);
3151
3152 bld.MOV(dest, packed_consts);
3153 dest = offset(dest, bld, 1);
3154 }
3155 }
3156 break;
3157 }
3158
3159 case nir_intrinsic_load_ssbo: {
3160 assert(devinfo->gen >= 7);
3161
3162 nir_const_value *const_uniform_block =
3163 nir_src_as_const_value(instr->src[0]);
3164
3165 fs_reg surf_index;
3166 if (const_uniform_block) {
3167 unsigned index = stage_prog_data->binding_table.ssbo_start +
3168 const_uniform_block->u32[0];
3169 surf_index = brw_imm_ud(index);
3170 brw_mark_surface_used(prog_data, index);
3171 } else {
3172 surf_index = vgrf(glsl_type::uint_type);
3173 bld.ADD(surf_index, get_nir_src(instr->src[0]),
3174 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
3175
3176 /* Assume this may touch any UBO. It would be nice to provide
3177 * a tighter bound, but the array information is already lowered away.
3178 */
3179 brw_mark_surface_used(prog_data,
3180 stage_prog_data->binding_table.ssbo_start +
3181 nir->info.num_ssbos - 1);
3182 }
3183
3184 fs_reg offset_reg;
3185 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
3186 if (const_offset) {
3187 offset_reg = brw_imm_ud(const_offset->u32[0]);
3188 } else {
3189 offset_reg = get_nir_src(instr->src[1]);
3190 }
3191
3192 /* Read the vector */
3193 fs_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
3194 1 /* dims */,
3195 instr->num_components,
3196 BRW_PREDICATE_NONE);
3197 read_result.type = dest.type;
3198 for (int i = 0; i < instr->num_components; i++)
3199 bld.MOV(offset(dest, bld, i), offset(read_result, bld, i));
3200
3201 break;
3202 }
3203
3204 case nir_intrinsic_load_input: {
3205 fs_reg src;
3206 if (stage == MESA_SHADER_VERTEX) {
3207 src = fs_reg(ATTR, instr->const_index[0], dest.type);
3208 } else {
3209 src = offset(retype(nir_inputs, dest.type), bld,
3210 instr->const_index[0]);
3211 }
3212
3213 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3214 assert(const_offset && "Indirect input loads not allowed");
3215 src = offset(src, bld, const_offset->u32[0]);
3216
3217 for (unsigned j = 0; j < instr->num_components; j++) {
3218 bld.MOV(offset(dest, bld, j), offset(src, bld, j));
3219 }
3220 break;
3221 }
3222
3223 case nir_intrinsic_store_ssbo: {
3224 assert(devinfo->gen >= 7);
3225
3226 /* Block index */
3227 fs_reg surf_index;
3228 nir_const_value *const_uniform_block =
3229 nir_src_as_const_value(instr->src[1]);
3230 if (const_uniform_block) {
3231 unsigned index = stage_prog_data->binding_table.ssbo_start +
3232 const_uniform_block->u32[0];
3233 surf_index = brw_imm_ud(index);
3234 brw_mark_surface_used(prog_data, index);
3235 } else {
3236 surf_index = vgrf(glsl_type::uint_type);
3237 bld.ADD(surf_index, get_nir_src(instr->src[1]),
3238 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
3239
3240 brw_mark_surface_used(prog_data,
3241 stage_prog_data->binding_table.ssbo_start +
3242 nir->info.num_ssbos - 1);
3243 }
3244
3245 /* Value */
3246 fs_reg val_reg = get_nir_src(instr->src[0]);
3247
3248 /* Writemask */
3249 unsigned writemask = instr->const_index[0];
3250
3251 /* Combine groups of consecutive enabled channels in one write
3252 * message. We use ffs to find the first enabled channel and then ffs on
3253 * the bit-inverse, down-shifted writemask to determine the length of
3254 * the block of enabled bits.
3255 */
3256 while (writemask) {
3257 unsigned first_component = ffs(writemask) - 1;
3258 unsigned length = ffs(~(writemask >> first_component)) - 1;
3259
3260 fs_reg offset_reg;
3261 nir_const_value *const_offset = nir_src_as_const_value(instr->src[2]);
3262 if (const_offset) {
3263 offset_reg = brw_imm_ud(const_offset->u32[0] + 4 * first_component);
3264 } else {
3265 offset_reg = vgrf(glsl_type::uint_type);
3266 bld.ADD(offset_reg,
3267 retype(get_nir_src(instr->src[2]), BRW_REGISTER_TYPE_UD),
3268 brw_imm_ud(4 * first_component));
3269 }
3270
3271 emit_untyped_write(bld, surf_index, offset_reg,
3272 offset(val_reg, bld, first_component),
3273 1 /* dims */, length,
3274 BRW_PREDICATE_NONE);
3275
3276 /* Clear the bits in the writemask that we just wrote, then try
3277 * again to see if more channels are left.
3278 */
3279 writemask &= (15 << (first_component + length));
3280 }
3281 break;
3282 }
3283
3284 case nir_intrinsic_store_output: {
3285 fs_reg src = get_nir_src(instr->src[0]);
3286 fs_reg new_dest = offset(retype(nir_outputs, src.type), bld,
3287 instr->const_index[0]);
3288
3289 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
3290 assert(const_offset && "Indirect output stores not allowed");
3291 new_dest = offset(new_dest, bld, const_offset->u32[0]);
3292
3293 for (unsigned j = 0; j < instr->num_components; j++) {
3294 bld.MOV(offset(new_dest, bld, j), offset(src, bld, j));
3295 }
3296 break;
3297 }
3298
3299 case nir_intrinsic_ssbo_atomic_add:
3300 nir_emit_ssbo_atomic(bld, BRW_AOP_ADD, instr);
3301 break;
3302 case nir_intrinsic_ssbo_atomic_imin:
3303 nir_emit_ssbo_atomic(bld, BRW_AOP_IMIN, instr);
3304 break;
3305 case nir_intrinsic_ssbo_atomic_umin:
3306 nir_emit_ssbo_atomic(bld, BRW_AOP_UMIN, instr);
3307 break;
3308 case nir_intrinsic_ssbo_atomic_imax:
3309 nir_emit_ssbo_atomic(bld, BRW_AOP_IMAX, instr);
3310 break;
3311 case nir_intrinsic_ssbo_atomic_umax:
3312 nir_emit_ssbo_atomic(bld, BRW_AOP_UMAX, instr);
3313 break;
3314 case nir_intrinsic_ssbo_atomic_and:
3315 nir_emit_ssbo_atomic(bld, BRW_AOP_AND, instr);
3316 break;
3317 case nir_intrinsic_ssbo_atomic_or:
3318 nir_emit_ssbo_atomic(bld, BRW_AOP_OR, instr);
3319 break;
3320 case nir_intrinsic_ssbo_atomic_xor:
3321 nir_emit_ssbo_atomic(bld, BRW_AOP_XOR, instr);
3322 break;
3323 case nir_intrinsic_ssbo_atomic_exchange:
3324 nir_emit_ssbo_atomic(bld, BRW_AOP_MOV, instr);
3325 break;
3326 case nir_intrinsic_ssbo_atomic_comp_swap:
3327 nir_emit_ssbo_atomic(bld, BRW_AOP_CMPWR, instr);
3328 break;
3329
3330 case nir_intrinsic_get_buffer_size: {
3331 nir_const_value *const_uniform_block = nir_src_as_const_value(instr->src[0]);
3332 unsigned ssbo_index = const_uniform_block ? const_uniform_block->u32[0] : 0;
3333 int reg_width = dispatch_width / 8;
3334
3335 /* Set LOD = 0 */
3336 fs_reg source = brw_imm_d(0);
3337
3338 int mlen = 1 * reg_width;
3339
3340 /* A resinfo's sampler message is used to get the buffer size.
3341 * The SIMD8's writeback message consists of four registers and
3342 * SIMD16's writeback message consists of 8 destination registers
3343 * (two per each component), although we are only interested on the
3344 * first component, where resinfo returns the buffer size for
3345 * SURFTYPE_BUFFER.
3346 */
3347 int regs_written = 4 * mlen;
3348 fs_reg src_payload = fs_reg(VGRF, alloc.allocate(mlen),
3349 BRW_REGISTER_TYPE_UD);
3350 bld.LOAD_PAYLOAD(src_payload, &source, 1, 0);
3351 fs_reg buffer_size = fs_reg(VGRF, alloc.allocate(regs_written),
3352 BRW_REGISTER_TYPE_UD);
3353 const unsigned index = prog_data->binding_table.ssbo_start + ssbo_index;
3354 fs_inst *inst = bld.emit(FS_OPCODE_GET_BUFFER_SIZE, buffer_size,
3355 src_payload, brw_imm_ud(index));
3356 inst->header_size = 0;
3357 inst->mlen = mlen;
3358 inst->regs_written = regs_written;
3359 bld.emit(inst);
3360 bld.MOV(retype(dest, buffer_size.type), buffer_size);
3361
3362 brw_mark_surface_used(prog_data, index);
3363 break;
3364 }
3365
3366 default:
3367 unreachable("unknown intrinsic");
3368 }
3369 }
3370
3371 void
3372 fs_visitor::nir_emit_ssbo_atomic(const fs_builder &bld,
3373 int op, nir_intrinsic_instr *instr)
3374 {
3375 fs_reg dest;
3376 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3377 dest = get_nir_dest(instr->dest);
3378
3379 fs_reg surface;
3380 nir_const_value *const_surface = nir_src_as_const_value(instr->src[0]);
3381 if (const_surface) {
3382 unsigned surf_index = stage_prog_data->binding_table.ssbo_start +
3383 const_surface->u32[0];
3384 surface = brw_imm_ud(surf_index);
3385 brw_mark_surface_used(prog_data, surf_index);
3386 } else {
3387 surface = vgrf(glsl_type::uint_type);
3388 bld.ADD(surface, get_nir_src(instr->src[0]),
3389 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
3390
3391 /* Assume this may touch any SSBO. This is the same we do for other
3392 * UBO/SSBO accesses with non-constant surface.
3393 */
3394 brw_mark_surface_used(prog_data,
3395 stage_prog_data->binding_table.ssbo_start +
3396 nir->info.num_ssbos - 1);
3397 }
3398
3399 fs_reg offset = get_nir_src(instr->src[1]);
3400 fs_reg data1 = get_nir_src(instr->src[2]);
3401 fs_reg data2;
3402 if (op == BRW_AOP_CMPWR)
3403 data2 = get_nir_src(instr->src[3]);
3404
3405 /* Emit the actual atomic operation operation */
3406
3407 fs_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
3408 data1, data2,
3409 1 /* dims */, 1 /* rsize */,
3410 op,
3411 BRW_PREDICATE_NONE);
3412 dest.type = atomic_result.type;
3413 bld.MOV(dest, atomic_result);
3414 }
3415
3416 void
3417 fs_visitor::nir_emit_shared_atomic(const fs_builder &bld,
3418 int op, nir_intrinsic_instr *instr)
3419 {
3420 fs_reg dest;
3421 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3422 dest = get_nir_dest(instr->dest);
3423
3424 fs_reg surface = brw_imm_ud(GEN7_BTI_SLM);
3425 fs_reg offset = get_nir_src(instr->src[0]);
3426 fs_reg data1 = get_nir_src(instr->src[1]);
3427 fs_reg data2;
3428 if (op == BRW_AOP_CMPWR)
3429 data2 = get_nir_src(instr->src[2]);
3430
3431 /* Emit the actual atomic operation operation */
3432
3433 fs_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
3434 data1, data2,
3435 1 /* dims */, 1 /* rsize */,
3436 op,
3437 BRW_PREDICATE_NONE);
3438 dest.type = atomic_result.type;
3439 bld.MOV(dest, atomic_result);
3440 }
3441
3442 void
3443 fs_visitor::nir_emit_texture(const fs_builder &bld, nir_tex_instr *instr)
3444 {
3445 unsigned texture = instr->texture_index;
3446 unsigned sampler = instr->sampler_index;
3447
3448 fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
3449
3450 srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(texture);
3451 srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_ud(sampler);
3452
3453 int lod_components = 0;
3454
3455 /* The hardware requires a LOD for buffer textures */
3456 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
3457 srcs[TEX_LOGICAL_SRC_LOD] = brw_imm_d(0);
3458
3459 for (unsigned i = 0; i < instr->num_srcs; i++) {
3460 fs_reg src = get_nir_src(instr->src[i].src);
3461 switch (instr->src[i].src_type) {
3462 case nir_tex_src_bias:
3463 srcs[TEX_LOGICAL_SRC_LOD] = retype(src, BRW_REGISTER_TYPE_F);
3464 break;
3465 case nir_tex_src_comparitor:
3466 srcs[TEX_LOGICAL_SRC_SHADOW_C] = retype(src, BRW_REGISTER_TYPE_F);
3467 break;
3468 case nir_tex_src_coord:
3469 switch (instr->op) {
3470 case nir_texop_txf:
3471 case nir_texop_txf_ms:
3472 case nir_texop_samples_identical:
3473 srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_D);
3474 break;
3475 default:
3476 srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_F);
3477 break;
3478 }
3479 break;
3480 case nir_tex_src_ddx:
3481 srcs[TEX_LOGICAL_SRC_LOD] = retype(src, BRW_REGISTER_TYPE_F);
3482 lod_components = nir_tex_instr_src_size(instr, i);
3483 break;
3484 case nir_tex_src_ddy:
3485 srcs[TEX_LOGICAL_SRC_LOD2] = retype(src, BRW_REGISTER_TYPE_F);
3486 break;
3487 case nir_tex_src_lod:
3488 switch (instr->op) {
3489 case nir_texop_txs:
3490 srcs[TEX_LOGICAL_SRC_LOD] = retype(src, BRW_REGISTER_TYPE_UD);
3491 break;
3492 case nir_texop_txf:
3493 srcs[TEX_LOGICAL_SRC_LOD] = retype(src, BRW_REGISTER_TYPE_D);
3494 break;
3495 default:
3496 srcs[TEX_LOGICAL_SRC_LOD] = retype(src, BRW_REGISTER_TYPE_F);
3497 break;
3498 }
3499 break;
3500 case nir_tex_src_ms_index:
3501 srcs[TEX_LOGICAL_SRC_SAMPLE_INDEX] = retype(src, BRW_REGISTER_TYPE_UD);
3502 break;
3503
3504 case nir_tex_src_offset: {
3505 nir_const_value *const_offset =
3506 nir_src_as_const_value(instr->src[i].src);
3507 if (const_offset) {
3508 unsigned header_bits = brw_texture_offset(const_offset->i32, 3);
3509 if (header_bits != 0)
3510 srcs[TEX_LOGICAL_SRC_OFFSET_VALUE] = brw_imm_ud(header_bits);
3511 } else {
3512 srcs[TEX_LOGICAL_SRC_OFFSET_VALUE] =
3513 retype(src, BRW_REGISTER_TYPE_D);
3514 }
3515 break;
3516 }
3517
3518 case nir_tex_src_projector:
3519 unreachable("should be lowered");
3520
3521 case nir_tex_src_texture_offset: {
3522 /* Figure out the highest possible texture index and mark it as used */
3523 uint32_t max_used = texture + instr->texture_array_size - 1;
3524 if (instr->op == nir_texop_tg4 && devinfo->gen < 8) {
3525 max_used += stage_prog_data->binding_table.gather_texture_start;
3526 } else {
3527 max_used += stage_prog_data->binding_table.texture_start;
3528 }
3529 brw_mark_surface_used(prog_data, max_used);
3530
3531 /* Emit code to evaluate the actual indexing expression */
3532 fs_reg tmp = vgrf(glsl_type::uint_type);
3533 bld.ADD(tmp, src, brw_imm_ud(texture));
3534 srcs[TEX_LOGICAL_SRC_SURFACE] = bld.emit_uniformize(tmp);
3535 break;
3536 }
3537
3538 case nir_tex_src_sampler_offset: {
3539 /* Emit code to evaluate the actual indexing expression */
3540 fs_reg tmp = vgrf(glsl_type::uint_type);
3541 bld.ADD(tmp, src, brw_imm_ud(sampler));
3542 srcs[TEX_LOGICAL_SRC_SAMPLER] = bld.emit_uniformize(tmp);
3543 break;
3544 }
3545
3546 default:
3547 unreachable("unknown texture source");
3548 }
3549 }
3550
3551 if (instr->op == nir_texop_txf_ms ||
3552 instr->op == nir_texop_samples_identical) {
3553 if (devinfo->gen >= 7 &&
3554 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
3555 srcs[TEX_LOGICAL_SRC_MCS] =
3556 emit_mcs_fetch(srcs[TEX_LOGICAL_SRC_COORDINATE],
3557 instr->coord_components,
3558 srcs[TEX_LOGICAL_SRC_SURFACE]);
3559 } else {
3560 srcs[TEX_LOGICAL_SRC_MCS] = brw_imm_ud(0u);
3561 }
3562 }
3563
3564 srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_d(instr->coord_components);
3565 srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_d(lod_components);
3566
3567 if (instr->op == nir_texop_query_levels) {
3568 /* textureQueryLevels() is implemented in terms of TXS so we need to
3569 * pass a valid LOD argument.
3570 */
3571 assert(srcs[TEX_LOGICAL_SRC_LOD].file == BAD_FILE);
3572 srcs[TEX_LOGICAL_SRC_LOD] = brw_imm_ud(0u);
3573 }
3574
3575 enum opcode opcode;
3576 switch (instr->op) {
3577 case nir_texop_tex:
3578 opcode = SHADER_OPCODE_TEX_LOGICAL;
3579 break;
3580 case nir_texop_txb:
3581 opcode = FS_OPCODE_TXB_LOGICAL;
3582 break;
3583 case nir_texop_txl:
3584 opcode = SHADER_OPCODE_TXL_LOGICAL;
3585 break;
3586 case nir_texop_txd:
3587 opcode = SHADER_OPCODE_TXD_LOGICAL;
3588 break;
3589 case nir_texop_txf:
3590 opcode = SHADER_OPCODE_TXF_LOGICAL;
3591 break;
3592 case nir_texop_txf_ms:
3593 if ((key_tex->msaa_16 & (1 << sampler)))
3594 opcode = SHADER_OPCODE_TXF_CMS_W_LOGICAL;
3595 else
3596 opcode = SHADER_OPCODE_TXF_CMS_LOGICAL;
3597 break;
3598 case nir_texop_query_levels:
3599 case nir_texop_txs:
3600 opcode = SHADER_OPCODE_TXS_LOGICAL;
3601 break;
3602 case nir_texop_lod:
3603 opcode = SHADER_OPCODE_LOD_LOGICAL;
3604 break;
3605 case nir_texop_tg4:
3606 if (srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].file != BAD_FILE &&
3607 srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].file != IMM)
3608 opcode = SHADER_OPCODE_TG4_OFFSET_LOGICAL;
3609 else
3610 opcode = SHADER_OPCODE_TG4_LOGICAL;
3611 break;
3612 case nir_texop_texture_samples: {
3613 fs_reg dst = retype(get_nir_dest(instr->dest), BRW_REGISTER_TYPE_D);
3614
3615 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_D, 4);
3616 fs_inst *inst = bld.emit(SHADER_OPCODE_SAMPLEINFO, tmp,
3617 bld.vgrf(BRW_REGISTER_TYPE_D, 1),
3618 srcs[TEX_LOGICAL_SRC_SURFACE],
3619 srcs[TEX_LOGICAL_SRC_SURFACE]);
3620 inst->mlen = 1;
3621 inst->header_size = 1;
3622 inst->base_mrf = -1;
3623 inst->regs_written = 4 * (dispatch_width / 8);
3624
3625 /* Pick off the one component we care about */
3626 bld.MOV(dst, tmp);
3627 return;
3628 }
3629 case nir_texop_samples_identical: {
3630 fs_reg dst = retype(get_nir_dest(instr->dest), BRW_REGISTER_TYPE_D);
3631
3632 /* If mcs is an immediate value, it means there is no MCS. In that case
3633 * just return false.
3634 */
3635 if (srcs[TEX_LOGICAL_SRC_MCS].file == BRW_IMMEDIATE_VALUE) {
3636 bld.MOV(dst, brw_imm_ud(0u));
3637 } else if ((key_tex->msaa_16 & (1 << sampler))) {
3638 fs_reg tmp = vgrf(glsl_type::uint_type);
3639 bld.OR(tmp, srcs[TEX_LOGICAL_SRC_MCS],
3640 offset(srcs[TEX_LOGICAL_SRC_MCS], bld, 1));
3641 bld.CMP(dst, tmp, brw_imm_ud(0u), BRW_CONDITIONAL_EQ);
3642 } else {
3643 bld.CMP(dst, srcs[TEX_LOGICAL_SRC_MCS], brw_imm_ud(0u),
3644 BRW_CONDITIONAL_EQ);
3645 }
3646 return;
3647 }
3648 default:
3649 unreachable("unknown texture opcode");
3650 }
3651
3652 fs_reg dst = bld.vgrf(brw_type_for_nir_type(instr->dest_type), 4);
3653 fs_inst *inst = bld.emit(opcode, dst, srcs, ARRAY_SIZE(srcs));
3654
3655 const unsigned dest_size = nir_tex_instr_dest_size(instr);
3656 if (devinfo->gen >= 9 &&
3657 instr->op != nir_texop_tg4 && instr->op != nir_texop_query_levels) {
3658 unsigned write_mask = instr->dest.is_ssa ?
3659 nir_ssa_def_components_read(&instr->dest.ssa):
3660 (1 << dest_size) - 1;
3661 assert(write_mask != 0); /* dead code should have been eliminated */
3662 inst->regs_written = _mesa_fls(write_mask) * dispatch_width / 8;
3663 } else {
3664 inst->regs_written = 4 * dispatch_width / 8;
3665 }
3666
3667 if (srcs[TEX_LOGICAL_SRC_SHADOW_C].file != BAD_FILE)
3668 inst->shadow_compare = true;
3669
3670 if (srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].file == IMM)
3671 inst->offset = srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].ud;
3672
3673 if (instr->op == nir_texop_tg4) {
3674 if (instr->component == 1 &&
3675 key_tex->gather_channel_quirk_mask & (1 << texture)) {
3676 /* gather4 sampler is broken for green channel on RG32F --
3677 * we must ask for blue instead.
3678 */
3679 inst->offset |= 2 << 16;
3680 } else {
3681 inst->offset |= instr->component << 16;
3682 }
3683
3684 if (devinfo->gen == 6)
3685 emit_gen6_gather_wa(key_tex->gen6_gather_wa[texture], dst);
3686 }
3687
3688 fs_reg nir_dest[4];
3689 for (unsigned i = 0; i < dest_size; i++)
3690 nir_dest[i] = offset(dst, bld, i);
3691
3692 bool is_cube_array = instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
3693 instr->is_array;
3694
3695 if (instr->op == nir_texop_query_levels) {
3696 /* # levels is in .w */
3697 nir_dest[0] = offset(dst, bld, 3);
3698 } else if (instr->op == nir_texop_txs && dest_size >= 3 &&
3699 (devinfo->gen < 7 || is_cube_array)) {
3700 fs_reg depth = offset(dst, bld, 2);
3701 fs_reg fixed_depth = vgrf(glsl_type::int_type);
3702
3703 if (is_cube_array) {
3704 /* fixup #layers for cube map arrays */
3705 bld.emit(SHADER_OPCODE_INT_QUOTIENT, fixed_depth, depth, brw_imm_d(6));
3706 } else if (devinfo->gen < 7) {
3707 /* Gen4-6 return 0 instead of 1 for single layer surfaces. */
3708 bld.emit_minmax(fixed_depth, depth, brw_imm_d(1), BRW_CONDITIONAL_GE);
3709 }
3710
3711 nir_dest[2] = fixed_depth;
3712 }
3713
3714 bld.LOAD_PAYLOAD(get_nir_dest(instr->dest), nir_dest, dest_size, 0);
3715 }
3716
3717 void
3718 fs_visitor::nir_emit_jump(const fs_builder &bld, nir_jump_instr *instr)
3719 {
3720 switch (instr->type) {
3721 case nir_jump_break:
3722 bld.emit(BRW_OPCODE_BREAK);
3723 break;
3724 case nir_jump_continue:
3725 bld.emit(BRW_OPCODE_CONTINUE);
3726 break;
3727 case nir_jump_return:
3728 default:
3729 unreachable("unknown jump");
3730 }
3731 }