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