i965/fs: Read all components of a SSBO field with one send
[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 "glsl/ir.h"
25 #include "glsl/ir_optimization.h"
26 #include "glsl/nir/glsl_to_nir.h"
27 #include "main/shaderimage.h"
28 #include "program/prog_to_nir.h"
29 #include "brw_fs.h"
30 #include "brw_fs_surface_builder.h"
31 #include "brw_nir.h"
32 #include "brw_fs_surface_builder.h"
33 #include "brw_vec4_gs_visitor.h"
34
35 using namespace brw;
36 using namespace brw::surface_access;
37
38 void
39 fs_visitor::emit_nir_code()
40 {
41 /* emit the arrays used for inputs and outputs - load/store intrinsics will
42 * be converted to reads/writes of these arrays
43 */
44 nir_setup_inputs();
45 nir_setup_outputs();
46 nir_setup_uniforms();
47 nir_emit_system_values();
48
49 /* get the main function and emit it */
50 nir_foreach_overload(nir, overload) {
51 assert(strcmp(overload->function->name, "main") == 0);
52 assert(overload->impl);
53 nir_emit_impl(overload->impl);
54 }
55 }
56
57 void
58 fs_visitor::nir_setup_inputs()
59 {
60 if (stage != MESA_SHADER_FRAGMENT)
61 return;
62
63 nir_inputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_inputs);
64
65 nir_foreach_variable(var, &nir->inputs) {
66 fs_reg input = offset(nir_inputs, bld, var->data.driver_location);
67
68 fs_reg reg;
69 if (var->data.location == VARYING_SLOT_POS) {
70 reg = *emit_fragcoord_interpolation(var->data.pixel_center_integer,
71 var->data.origin_upper_left);
72 emit_percomp(bld, fs_inst(BRW_OPCODE_MOV, bld.dispatch_width(),
73 input, reg), 0xF);
74 } else {
75 emit_general_interpolation(input, var->name, var->type,
76 (glsl_interp_qualifier) var->data.interpolation,
77 var->data.location, var->data.centroid,
78 var->data.sample);
79 }
80 }
81 }
82
83 void
84 fs_visitor::nir_setup_outputs()
85 {
86 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
87
88 nir_outputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_outputs);
89
90 nir_foreach_variable(var, &nir->outputs) {
91 fs_reg reg = offset(nir_outputs, bld, var->data.driver_location);
92
93 int vector_elements = var->type->without_array()->vector_elements;
94
95 switch (stage) {
96 case MESA_SHADER_VERTEX:
97 for (unsigned int i = 0; i < ALIGN(type_size_scalar(var->type), 4) / 4; i++) {
98 int output = var->data.location + i;
99 this->outputs[output] = offset(reg, bld, 4 * i);
100 this->output_components[output] = vector_elements;
101 }
102 break;
103 case MESA_SHADER_FRAGMENT:
104 if (var->data.index > 0) {
105 assert(var->data.location == FRAG_RESULT_DATA0);
106 assert(var->data.index == 1);
107 this->dual_src_output = reg;
108 this->do_dual_src = true;
109 } else if (var->data.location == FRAG_RESULT_COLOR) {
110 /* Writing gl_FragColor outputs to all color regions. */
111 for (unsigned int i = 0; i < MAX2(key->nr_color_regions, 1); i++) {
112 this->outputs[i] = reg;
113 this->output_components[i] = 4;
114 }
115 } else if (var->data.location == FRAG_RESULT_DEPTH) {
116 this->frag_depth = reg;
117 } else if (var->data.location == FRAG_RESULT_STENCIL) {
118 this->frag_stencil = reg;
119 } else if (var->data.location == FRAG_RESULT_SAMPLE_MASK) {
120 this->sample_mask = reg;
121 } else {
122 /* gl_FragData or a user-defined FS output */
123 assert(var->data.location >= FRAG_RESULT_DATA0 &&
124 var->data.location < FRAG_RESULT_DATA0+BRW_MAX_DRAW_BUFFERS);
125
126 /* General color output. */
127 for (unsigned int i = 0; i < MAX2(1, var->type->length); i++) {
128 int output = var->data.location - FRAG_RESULT_DATA0 + i;
129 this->outputs[output] = offset(reg, bld, vector_elements * i);
130 this->output_components[output] = vector_elements;
131 }
132 }
133 break;
134 default:
135 unreachable("unhandled shader stage");
136 }
137 }
138 }
139
140 void
141 fs_visitor::nir_setup_uniforms()
142 {
143 if (dispatch_width != 8)
144 return;
145
146 uniforms = nir->num_uniforms;
147
148 nir_foreach_variable(var, &nir->uniforms) {
149 /* UBO's and atomics don't take up space in the uniform file */
150 if (var->interface_type != NULL || var->type->contains_atomic())
151 continue;
152
153 if (type_size_scalar(var->type) > 0)
154 param_size[var->data.driver_location] = type_size_scalar(var->type);
155 }
156 }
157
158 static bool
159 emit_system_values_block(nir_block *block, void *void_visitor)
160 {
161 fs_visitor *v = (fs_visitor *)void_visitor;
162 fs_reg *reg;
163
164 nir_foreach_instr(block, instr) {
165 if (instr->type != nir_instr_type_intrinsic)
166 continue;
167
168 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
169 switch (intrin->intrinsic) {
170 case nir_intrinsic_load_vertex_id:
171 unreachable("should be lowered by lower_vertex_id().");
172
173 case nir_intrinsic_load_vertex_id_zero_base:
174 assert(v->stage == MESA_SHADER_VERTEX);
175 reg = &v->nir_system_values[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE];
176 if (reg->file == BAD_FILE)
177 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE);
178 break;
179
180 case nir_intrinsic_load_base_vertex:
181 assert(v->stage == MESA_SHADER_VERTEX);
182 reg = &v->nir_system_values[SYSTEM_VALUE_BASE_VERTEX];
183 if (reg->file == BAD_FILE)
184 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_BASE_VERTEX);
185 break;
186
187 case nir_intrinsic_load_instance_id:
188 assert(v->stage == MESA_SHADER_VERTEX);
189 reg = &v->nir_system_values[SYSTEM_VALUE_INSTANCE_ID];
190 if (reg->file == BAD_FILE)
191 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_INSTANCE_ID);
192 break;
193
194 case nir_intrinsic_load_invocation_id:
195 assert(v->stage == MESA_SHADER_GEOMETRY);
196 reg = &v->nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
197 if (reg->file == BAD_FILE) {
198 const fs_builder abld = v->bld.annotate("gl_InvocationID", NULL);
199 fs_reg g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
200 fs_reg iid = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
201 abld.SHR(iid, g1, fs_reg(27u));
202 *reg = iid;
203 }
204 break;
205
206 case nir_intrinsic_load_sample_pos:
207 assert(v->stage == MESA_SHADER_FRAGMENT);
208 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
209 if (reg->file == BAD_FILE)
210 *reg = *v->emit_samplepos_setup();
211 break;
212
213 case nir_intrinsic_load_sample_id:
214 assert(v->stage == MESA_SHADER_FRAGMENT);
215 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
216 if (reg->file == BAD_FILE)
217 *reg = *v->emit_sampleid_setup();
218 break;
219
220 case nir_intrinsic_load_sample_mask_in:
221 assert(v->stage == MESA_SHADER_FRAGMENT);
222 assert(v->devinfo->gen >= 7);
223 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_MASK_IN];
224 if (reg->file == BAD_FILE)
225 *reg = fs_reg(retype(brw_vec8_grf(v->payload.sample_mask_in_reg, 0),
226 BRW_REGISTER_TYPE_D));
227 break;
228
229 case nir_intrinsic_load_local_invocation_id:
230 assert(v->stage == MESA_SHADER_COMPUTE);
231 reg = &v->nir_system_values[SYSTEM_VALUE_LOCAL_INVOCATION_ID];
232 if (reg->file == BAD_FILE)
233 *reg = *v->emit_cs_local_invocation_id_setup();
234 break;
235
236 case nir_intrinsic_load_work_group_id:
237 assert(v->stage == MESA_SHADER_COMPUTE);
238 reg = &v->nir_system_values[SYSTEM_VALUE_WORK_GROUP_ID];
239 if (reg->file == BAD_FILE)
240 *reg = *v->emit_cs_work_group_id_setup();
241 break;
242
243 default:
244 break;
245 }
246 }
247
248 return true;
249 }
250
251 void
252 fs_visitor::nir_emit_system_values()
253 {
254 nir_system_values = ralloc_array(mem_ctx, fs_reg, SYSTEM_VALUE_MAX);
255 nir_foreach_overload(nir, overload) {
256 assert(strcmp(overload->function->name, "main") == 0);
257 assert(overload->impl);
258 nir_foreach_block(overload->impl, emit_system_values_block, this);
259 }
260 }
261
262 void
263 fs_visitor::nir_emit_impl(nir_function_impl *impl)
264 {
265 nir_locals = reralloc(mem_ctx, nir_locals, fs_reg, impl->reg_alloc);
266 foreach_list_typed(nir_register, reg, node, &impl->registers) {
267 unsigned array_elems =
268 reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
269 unsigned size = array_elems * reg->num_components;
270 nir_locals[reg->index] = bld.vgrf(BRW_REGISTER_TYPE_F, size);
271 }
272
273 nir_ssa_values = reralloc(mem_ctx, nir_ssa_values, fs_reg,
274 impl->ssa_alloc);
275
276 nir_emit_cf_list(&impl->body);
277 }
278
279 void
280 fs_visitor::nir_emit_cf_list(exec_list *list)
281 {
282 exec_list_validate(list);
283 foreach_list_typed(nir_cf_node, node, node, list) {
284 switch (node->type) {
285 case nir_cf_node_if:
286 nir_emit_if(nir_cf_node_as_if(node));
287 break;
288
289 case nir_cf_node_loop:
290 nir_emit_loop(nir_cf_node_as_loop(node));
291 break;
292
293 case nir_cf_node_block:
294 nir_emit_block(nir_cf_node_as_block(node));
295 break;
296
297 default:
298 unreachable("Invalid CFG node block");
299 }
300 }
301 }
302
303 void
304 fs_visitor::nir_emit_if(nir_if *if_stmt)
305 {
306 /* first, put the condition into f0 */
307 fs_inst *inst = bld.MOV(bld.null_reg_d(),
308 retype(get_nir_src(if_stmt->condition),
309 BRW_REGISTER_TYPE_D));
310 inst->conditional_mod = BRW_CONDITIONAL_NZ;
311
312 bld.IF(BRW_PREDICATE_NORMAL);
313
314 nir_emit_cf_list(&if_stmt->then_list);
315
316 /* note: if the else is empty, dead CF elimination will remove it */
317 bld.emit(BRW_OPCODE_ELSE);
318
319 nir_emit_cf_list(&if_stmt->else_list);
320
321 bld.emit(BRW_OPCODE_ENDIF);
322 }
323
324 void
325 fs_visitor::nir_emit_loop(nir_loop *loop)
326 {
327 bld.emit(BRW_OPCODE_DO);
328
329 nir_emit_cf_list(&loop->body);
330
331 bld.emit(BRW_OPCODE_WHILE);
332 }
333
334 void
335 fs_visitor::nir_emit_block(nir_block *block)
336 {
337 nir_foreach_instr(block, instr) {
338 nir_emit_instr(instr);
339 }
340 }
341
342 void
343 fs_visitor::nir_emit_instr(nir_instr *instr)
344 {
345 const fs_builder abld = bld.annotate(NULL, instr);
346
347 switch (instr->type) {
348 case nir_instr_type_alu:
349 nir_emit_alu(abld, nir_instr_as_alu(instr));
350 break;
351
352 case nir_instr_type_intrinsic:
353 nir_emit_intrinsic(abld, nir_instr_as_intrinsic(instr));
354 break;
355
356 case nir_instr_type_tex:
357 nir_emit_texture(abld, nir_instr_as_tex(instr));
358 break;
359
360 case nir_instr_type_load_const:
361 nir_emit_load_const(abld, nir_instr_as_load_const(instr));
362 break;
363
364 case nir_instr_type_ssa_undef:
365 nir_emit_undef(abld, nir_instr_as_ssa_undef(instr));
366 break;
367
368 case nir_instr_type_jump:
369 nir_emit_jump(abld, nir_instr_as_jump(instr));
370 break;
371
372 default:
373 unreachable("unknown instruction type");
374 }
375 }
376
377 bool
378 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr *instr,
379 const fs_reg &result)
380 {
381 if (!instr->src[0].src.is_ssa ||
382 instr->src[0].src.ssa->parent_instr->type != nir_instr_type_intrinsic)
383 return false;
384
385 nir_intrinsic_instr *src0 =
386 nir_instr_as_intrinsic(instr->src[0].src.ssa->parent_instr);
387
388 if (src0->intrinsic != nir_intrinsic_load_front_face)
389 return false;
390
391 nir_const_value *value1 = nir_src_as_const_value(instr->src[1].src);
392 if (!value1 || fabsf(value1->f[0]) != 1.0f)
393 return false;
394
395 nir_const_value *value2 = nir_src_as_const_value(instr->src[2].src);
396 if (!value2 || fabsf(value2->f[0]) != 1.0f)
397 return false;
398
399 fs_reg tmp = vgrf(glsl_type::int_type);
400
401 if (devinfo->gen >= 6) {
402 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
403 fs_reg g0 = fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W));
404
405 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
406 *
407 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
408 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
409 *
410 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
411 *
412 * This negation looks like it's safe in practice, because bits 0:4 will
413 * surely be TRIANGLES
414 */
415
416 if (value1->f[0] == -1.0f) {
417 g0.negate = true;
418 }
419
420 tmp.type = BRW_REGISTER_TYPE_W;
421 tmp.subreg_offset = 2;
422 tmp.stride = 2;
423
424 fs_inst *or_inst = bld.OR(tmp, g0, fs_reg(0x3f80));
425 or_inst->src[1].type = BRW_REGISTER_TYPE_UW;
426
427 tmp.type = BRW_REGISTER_TYPE_D;
428 tmp.subreg_offset = 0;
429 tmp.stride = 1;
430 } else {
431 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
432 fs_reg g1_6 = fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D));
433
434 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
435 *
436 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
437 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
438 *
439 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
440 *
441 * This negation looks like it's safe in practice, because bits 0:4 will
442 * surely be TRIANGLES
443 */
444
445 if (value1->f[0] == -1.0f) {
446 g1_6.negate = true;
447 }
448
449 bld.OR(tmp, g1_6, fs_reg(0x3f800000));
450 }
451 bld.AND(retype(result, BRW_REGISTER_TYPE_D), tmp, fs_reg(0xbf800000));
452
453 return true;
454 }
455
456 void
457 fs_visitor::nir_emit_alu(const fs_builder &bld, nir_alu_instr *instr)
458 {
459 struct brw_wm_prog_key *fs_key = (struct brw_wm_prog_key *) this->key;
460 fs_inst *inst;
461
462 fs_reg result = get_nir_dest(instr->dest.dest);
463 result.type = brw_type_for_nir_type(nir_op_infos[instr->op].output_type);
464
465 fs_reg op[4];
466 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
467 op[i] = get_nir_src(instr->src[i].src);
468 op[i].type = brw_type_for_nir_type(nir_op_infos[instr->op].input_types[i]);
469 op[i].abs = instr->src[i].abs;
470 op[i].negate = instr->src[i].negate;
471 }
472
473 /* We get a bunch of mov's out of the from_ssa pass and they may still
474 * be vectorized. We'll handle them as a special-case. We'll also
475 * handle vecN here because it's basically the same thing.
476 */
477 switch (instr->op) {
478 case nir_op_imov:
479 case nir_op_fmov:
480 case nir_op_vec2:
481 case nir_op_vec3:
482 case nir_op_vec4: {
483 fs_reg temp = result;
484 bool need_extra_copy = false;
485 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
486 if (!instr->src[i].src.is_ssa &&
487 instr->dest.dest.reg.reg == instr->src[i].src.reg.reg) {
488 need_extra_copy = true;
489 temp = bld.vgrf(result.type, 4);
490 break;
491 }
492 }
493
494 for (unsigned i = 0; i < 4; i++) {
495 if (!(instr->dest.write_mask & (1 << i)))
496 continue;
497
498 if (instr->op == nir_op_imov || instr->op == nir_op_fmov) {
499 inst = bld.MOV(offset(temp, bld, i),
500 offset(op[0], bld, instr->src[0].swizzle[i]));
501 } else {
502 inst = bld.MOV(offset(temp, bld, i),
503 offset(op[i], bld, instr->src[i].swizzle[0]));
504 }
505 inst->saturate = instr->dest.saturate;
506 }
507
508 /* In this case the source and destination registers were the same,
509 * so we need to insert an extra set of moves in order to deal with
510 * any swizzling.
511 */
512 if (need_extra_copy) {
513 for (unsigned i = 0; i < 4; i++) {
514 if (!(instr->dest.write_mask & (1 << i)))
515 continue;
516
517 bld.MOV(offset(result, bld, i), offset(temp, bld, i));
518 }
519 }
520 return;
521 }
522 default:
523 break;
524 }
525
526 /* At this point, we have dealt with any instruction that operates on
527 * more than a single channel. Therefore, we can just adjust the source
528 * and destination registers for that channel and emit the instruction.
529 */
530 unsigned channel = 0;
531 if (nir_op_infos[instr->op].output_size == 0) {
532 /* Since NIR is doing the scalarizing for us, we should only ever see
533 * vectorized operations with a single channel.
534 */
535 assert(_mesa_bitcount(instr->dest.write_mask) == 1);
536 channel = ffs(instr->dest.write_mask) - 1;
537
538 result = offset(result, bld, channel);
539 }
540
541 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
542 assert(nir_op_infos[instr->op].input_sizes[i] < 2);
543 op[i] = offset(op[i], bld, instr->src[i].swizzle[channel]);
544 }
545
546 switch (instr->op) {
547 case nir_op_i2f:
548 case nir_op_u2f:
549 inst = bld.MOV(result, op[0]);
550 inst->saturate = instr->dest.saturate;
551 break;
552
553 case nir_op_f2i:
554 case nir_op_f2u:
555 bld.MOV(result, op[0]);
556 break;
557
558 case nir_op_fsign: {
559 /* AND(val, 0x80000000) gives the sign bit.
560 *
561 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
562 * zero.
563 */
564 bld.CMP(bld.null_reg_f(), op[0], fs_reg(0.0f), BRW_CONDITIONAL_NZ);
565
566 fs_reg result_int = retype(result, BRW_REGISTER_TYPE_UD);
567 op[0].type = BRW_REGISTER_TYPE_UD;
568 result.type = BRW_REGISTER_TYPE_UD;
569 bld.AND(result_int, op[0], fs_reg(0x80000000u));
570
571 inst = bld.OR(result_int, result_int, fs_reg(0x3f800000u));
572 inst->predicate = BRW_PREDICATE_NORMAL;
573 if (instr->dest.saturate) {
574 inst = bld.MOV(result, result);
575 inst->saturate = true;
576 }
577 break;
578 }
579
580 case nir_op_isign:
581 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
582 * -> non-negative val generates 0x00000000.
583 * Predicated OR sets 1 if val is positive.
584 */
585 bld.CMP(bld.null_reg_d(), op[0], fs_reg(0), BRW_CONDITIONAL_G);
586 bld.ASR(result, op[0], fs_reg(31));
587 inst = bld.OR(result, result, fs_reg(1));
588 inst->predicate = BRW_PREDICATE_NORMAL;
589 break;
590
591 case nir_op_frcp:
592 inst = bld.emit(SHADER_OPCODE_RCP, result, op[0]);
593 inst->saturate = instr->dest.saturate;
594 break;
595
596 case nir_op_fexp2:
597 inst = bld.emit(SHADER_OPCODE_EXP2, result, op[0]);
598 inst->saturate = instr->dest.saturate;
599 break;
600
601 case nir_op_flog2:
602 inst = bld.emit(SHADER_OPCODE_LOG2, result, op[0]);
603 inst->saturate = instr->dest.saturate;
604 break;
605
606 case nir_op_fsin:
607 inst = bld.emit(SHADER_OPCODE_SIN, result, op[0]);
608 inst->saturate = instr->dest.saturate;
609 break;
610
611 case nir_op_fcos:
612 inst = bld.emit(SHADER_OPCODE_COS, result, op[0]);
613 inst->saturate = instr->dest.saturate;
614 break;
615
616 case nir_op_fddx:
617 if (fs_key->high_quality_derivatives) {
618 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
619 } else {
620 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
621 }
622 inst->saturate = instr->dest.saturate;
623 break;
624 case nir_op_fddx_fine:
625 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
626 inst->saturate = instr->dest.saturate;
627 break;
628 case nir_op_fddx_coarse:
629 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
630 inst->saturate = instr->dest.saturate;
631 break;
632 case nir_op_fddy:
633 if (fs_key->high_quality_derivatives) {
634 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0],
635 fs_reg(fs_key->render_to_fbo));
636 } else {
637 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0],
638 fs_reg(fs_key->render_to_fbo));
639 }
640 inst->saturate = instr->dest.saturate;
641 break;
642 case nir_op_fddy_fine:
643 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0],
644 fs_reg(fs_key->render_to_fbo));
645 inst->saturate = instr->dest.saturate;
646 break;
647 case nir_op_fddy_coarse:
648 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0],
649 fs_reg(fs_key->render_to_fbo));
650 inst->saturate = instr->dest.saturate;
651 break;
652
653 case nir_op_fadd:
654 case nir_op_iadd:
655 inst = bld.ADD(result, op[0], op[1]);
656 inst->saturate = instr->dest.saturate;
657 break;
658
659 case nir_op_fmul:
660 inst = bld.MUL(result, op[0], op[1]);
661 inst->saturate = instr->dest.saturate;
662 break;
663
664 case nir_op_imul:
665 bld.MUL(result, op[0], op[1]);
666 break;
667
668 case nir_op_imul_high:
669 case nir_op_umul_high:
670 bld.emit(SHADER_OPCODE_MULH, result, op[0], op[1]);
671 break;
672
673 case nir_op_idiv:
674 case nir_op_udiv:
675 bld.emit(SHADER_OPCODE_INT_QUOTIENT, result, op[0], op[1]);
676 break;
677
678 case nir_op_uadd_carry:
679 unreachable("Should have been lowered by carry_to_arith().");
680
681 case nir_op_usub_borrow:
682 unreachable("Should have been lowered by borrow_to_arith().");
683
684 case nir_op_umod:
685 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
686 break;
687
688 case nir_op_flt:
689 case nir_op_ilt:
690 case nir_op_ult:
691 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_L);
692 break;
693
694 case nir_op_fge:
695 case nir_op_ige:
696 case nir_op_uge:
697 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_GE);
698 break;
699
700 case nir_op_feq:
701 case nir_op_ieq:
702 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_Z);
703 break;
704
705 case nir_op_fne:
706 case nir_op_ine:
707 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_NZ);
708 break;
709
710 case nir_op_inot:
711 if (devinfo->gen >= 8) {
712 op[0] = resolve_source_modifiers(op[0]);
713 }
714 bld.NOT(result, op[0]);
715 break;
716 case nir_op_ixor:
717 if (devinfo->gen >= 8) {
718 op[0] = resolve_source_modifiers(op[0]);
719 op[1] = resolve_source_modifiers(op[1]);
720 }
721 bld.XOR(result, op[0], op[1]);
722 break;
723 case nir_op_ior:
724 if (devinfo->gen >= 8) {
725 op[0] = resolve_source_modifiers(op[0]);
726 op[1] = resolve_source_modifiers(op[1]);
727 }
728 bld.OR(result, op[0], op[1]);
729 break;
730 case nir_op_iand:
731 if (devinfo->gen >= 8) {
732 op[0] = resolve_source_modifiers(op[0]);
733 op[1] = resolve_source_modifiers(op[1]);
734 }
735 bld.AND(result, op[0], op[1]);
736 break;
737
738 case nir_op_fdot2:
739 case nir_op_fdot3:
740 case nir_op_fdot4:
741 case nir_op_bany2:
742 case nir_op_bany3:
743 case nir_op_bany4:
744 case nir_op_ball2:
745 case nir_op_ball3:
746 case nir_op_ball4:
747 case nir_op_ball_fequal2:
748 case nir_op_ball_iequal2:
749 case nir_op_ball_fequal3:
750 case nir_op_ball_iequal3:
751 case nir_op_ball_fequal4:
752 case nir_op_ball_iequal4:
753 case nir_op_bany_fnequal2:
754 case nir_op_bany_inequal2:
755 case nir_op_bany_fnequal3:
756 case nir_op_bany_inequal3:
757 case nir_op_bany_fnequal4:
758 case nir_op_bany_inequal4:
759 unreachable("Lowered by nir_lower_alu_reductions");
760
761 case nir_op_fnoise1_1:
762 case nir_op_fnoise1_2:
763 case nir_op_fnoise1_3:
764 case nir_op_fnoise1_4:
765 case nir_op_fnoise2_1:
766 case nir_op_fnoise2_2:
767 case nir_op_fnoise2_3:
768 case nir_op_fnoise2_4:
769 case nir_op_fnoise3_1:
770 case nir_op_fnoise3_2:
771 case nir_op_fnoise3_3:
772 case nir_op_fnoise3_4:
773 case nir_op_fnoise4_1:
774 case nir_op_fnoise4_2:
775 case nir_op_fnoise4_3:
776 case nir_op_fnoise4_4:
777 unreachable("not reached: should be handled by lower_noise");
778
779 case nir_op_ldexp:
780 unreachable("not reached: should be handled by ldexp_to_arith()");
781
782 case nir_op_fsqrt:
783 inst = bld.emit(SHADER_OPCODE_SQRT, result, op[0]);
784 inst->saturate = instr->dest.saturate;
785 break;
786
787 case nir_op_frsq:
788 inst = bld.emit(SHADER_OPCODE_RSQ, result, op[0]);
789 inst->saturate = instr->dest.saturate;
790 break;
791
792 case nir_op_b2i:
793 case nir_op_b2f:
794 bld.MOV(result, negate(op[0]));
795 break;
796
797 case nir_op_f2b:
798 bld.CMP(result, op[0], fs_reg(0.0f), BRW_CONDITIONAL_NZ);
799 break;
800 case nir_op_i2b:
801 bld.CMP(result, op[0], fs_reg(0), BRW_CONDITIONAL_NZ);
802 break;
803
804 case nir_op_ftrunc:
805 inst = bld.RNDZ(result, op[0]);
806 inst->saturate = instr->dest.saturate;
807 break;
808
809 case nir_op_fceil: {
810 op[0].negate = !op[0].negate;
811 fs_reg temp = vgrf(glsl_type::float_type);
812 bld.RNDD(temp, op[0]);
813 temp.negate = true;
814 inst = bld.MOV(result, temp);
815 inst->saturate = instr->dest.saturate;
816 break;
817 }
818 case nir_op_ffloor:
819 inst = bld.RNDD(result, op[0]);
820 inst->saturate = instr->dest.saturate;
821 break;
822 case nir_op_ffract:
823 inst = bld.FRC(result, op[0]);
824 inst->saturate = instr->dest.saturate;
825 break;
826 case nir_op_fround_even:
827 inst = bld.RNDE(result, op[0]);
828 inst->saturate = instr->dest.saturate;
829 break;
830
831 case nir_op_fmin:
832 case nir_op_imin:
833 case nir_op_umin:
834 if (devinfo->gen >= 6) {
835 inst = bld.emit(BRW_OPCODE_SEL, result, op[0], op[1]);
836 inst->conditional_mod = BRW_CONDITIONAL_L;
837 } else {
838 bld.CMP(bld.null_reg_d(), op[0], op[1], BRW_CONDITIONAL_L);
839 inst = bld.SEL(result, op[0], op[1]);
840 inst->predicate = BRW_PREDICATE_NORMAL;
841 }
842 inst->saturate = instr->dest.saturate;
843 break;
844
845 case nir_op_fmax:
846 case nir_op_imax:
847 case nir_op_umax:
848 if (devinfo->gen >= 6) {
849 inst = bld.emit(BRW_OPCODE_SEL, result, op[0], op[1]);
850 inst->conditional_mod = BRW_CONDITIONAL_GE;
851 } else {
852 bld.CMP(bld.null_reg_d(), op[0], op[1], BRW_CONDITIONAL_GE);
853 inst = bld.SEL(result, op[0], op[1]);
854 inst->predicate = BRW_PREDICATE_NORMAL;
855 }
856 inst->saturate = instr->dest.saturate;
857 break;
858
859 case nir_op_pack_snorm_2x16:
860 case nir_op_pack_snorm_4x8:
861 case nir_op_pack_unorm_2x16:
862 case nir_op_pack_unorm_4x8:
863 case nir_op_unpack_snorm_2x16:
864 case nir_op_unpack_snorm_4x8:
865 case nir_op_unpack_unorm_2x16:
866 case nir_op_unpack_unorm_4x8:
867 case nir_op_unpack_half_2x16:
868 case nir_op_pack_half_2x16:
869 unreachable("not reached: should be handled by lower_packing_builtins");
870
871 case nir_op_unpack_half_2x16_split_x:
872 inst = bld.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X, result, op[0]);
873 inst->saturate = instr->dest.saturate;
874 break;
875 case nir_op_unpack_half_2x16_split_y:
876 inst = bld.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y, result, op[0]);
877 inst->saturate = instr->dest.saturate;
878 break;
879
880 case nir_op_fpow:
881 inst = bld.emit(SHADER_OPCODE_POW, result, op[0], op[1]);
882 inst->saturate = instr->dest.saturate;
883 break;
884
885 case nir_op_bitfield_reverse:
886 bld.BFREV(result, op[0]);
887 break;
888
889 case nir_op_bit_count:
890 bld.CBIT(result, op[0]);
891 break;
892
893 case nir_op_ufind_msb:
894 case nir_op_ifind_msb: {
895 bld.FBH(retype(result, BRW_REGISTER_TYPE_UD), op[0]);
896
897 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
898 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
899 * subtract the result from 31 to convert the MSB count into an LSB count.
900 */
901
902 bld.CMP(bld.null_reg_d(), result, fs_reg(-1), BRW_CONDITIONAL_NZ);
903 fs_reg neg_result(result);
904 neg_result.negate = true;
905 inst = bld.ADD(result, neg_result, fs_reg(31));
906 inst->predicate = BRW_PREDICATE_NORMAL;
907 break;
908 }
909
910 case nir_op_find_lsb:
911 bld.FBL(result, op[0]);
912 break;
913
914 case nir_op_ubitfield_extract:
915 case nir_op_ibitfield_extract:
916 bld.BFE(result, op[2], op[1], op[0]);
917 break;
918 case nir_op_bfm:
919 bld.BFI1(result, op[0], op[1]);
920 break;
921 case nir_op_bfi:
922 bld.BFI2(result, op[0], op[1], op[2]);
923 break;
924
925 case nir_op_bitfield_insert:
926 unreachable("not reached: should be handled by "
927 "lower_instructions::bitfield_insert_to_bfm_bfi");
928
929 case nir_op_ishl:
930 bld.SHL(result, op[0], op[1]);
931 break;
932 case nir_op_ishr:
933 bld.ASR(result, op[0], op[1]);
934 break;
935 case nir_op_ushr:
936 bld.SHR(result, op[0], op[1]);
937 break;
938
939 case nir_op_pack_half_2x16_split:
940 bld.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT, result, op[0], op[1]);
941 break;
942
943 case nir_op_ffma:
944 inst = bld.MAD(result, op[2], op[1], op[0]);
945 inst->saturate = instr->dest.saturate;
946 break;
947
948 case nir_op_flrp:
949 inst = bld.LRP(result, op[0], op[1], op[2]);
950 inst->saturate = instr->dest.saturate;
951 break;
952
953 case nir_op_bcsel:
954 if (optimize_frontfacing_ternary(instr, result))
955 return;
956
957 bld.CMP(bld.null_reg_d(), op[0], fs_reg(0), BRW_CONDITIONAL_NZ);
958 inst = bld.SEL(result, op[1], op[2]);
959 inst->predicate = BRW_PREDICATE_NORMAL;
960 break;
961
962 default:
963 unreachable("unhandled instruction");
964 }
965
966 /* If we need to do a boolean resolve, replace the result with -(x & 1)
967 * to sign extend the low bit to 0/~0
968 */
969 if (devinfo->gen <= 5 &&
970 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
971 fs_reg masked = vgrf(glsl_type::int_type);
972 bld.AND(masked, result, fs_reg(1));
973 masked.negate = true;
974 bld.MOV(retype(result, BRW_REGISTER_TYPE_D), masked);
975 }
976 }
977
978 void
979 fs_visitor::nir_emit_load_const(const fs_builder &bld,
980 nir_load_const_instr *instr)
981 {
982 fs_reg reg = bld.vgrf(BRW_REGISTER_TYPE_D, instr->def.num_components);
983
984 for (unsigned i = 0; i < instr->def.num_components; i++)
985 bld.MOV(offset(reg, bld, i), fs_reg(instr->value.i[i]));
986
987 nir_ssa_values[instr->def.index] = reg;
988 }
989
990 void
991 fs_visitor::nir_emit_undef(const fs_builder &bld, nir_ssa_undef_instr *instr)
992 {
993 nir_ssa_values[instr->def.index] = bld.vgrf(BRW_REGISTER_TYPE_D,
994 instr->def.num_components);
995 }
996
997 static fs_reg
998 fs_reg_for_nir_reg(fs_visitor *v, nir_register *nir_reg,
999 unsigned base_offset, nir_src *indirect)
1000 {
1001 fs_reg reg;
1002
1003 assert(!nir_reg->is_global);
1004
1005 reg = v->nir_locals[nir_reg->index];
1006
1007 reg = offset(reg, v->bld, base_offset * nir_reg->num_components);
1008 if (indirect) {
1009 int multiplier = nir_reg->num_components * (v->dispatch_width / 8);
1010
1011 reg.reladdr = new(v->mem_ctx) fs_reg(v->vgrf(glsl_type::int_type));
1012 v->bld.MUL(*reg.reladdr, v->get_nir_src(*indirect),
1013 fs_reg(multiplier));
1014 }
1015
1016 return reg;
1017 }
1018
1019 fs_reg
1020 fs_visitor::get_nir_src(nir_src src)
1021 {
1022 fs_reg reg;
1023 if (src.is_ssa) {
1024 reg = nir_ssa_values[src.ssa->index];
1025 } else {
1026 reg = fs_reg_for_nir_reg(this, src.reg.reg, src.reg.base_offset,
1027 src.reg.indirect);
1028 }
1029
1030 /* to avoid floating-point denorm flushing problems, set the type by
1031 * default to D - instructions that need floating point semantics will set
1032 * this to F if they need to
1033 */
1034 return retype(reg, BRW_REGISTER_TYPE_D);
1035 }
1036
1037 fs_reg
1038 fs_visitor::get_nir_dest(nir_dest dest)
1039 {
1040 if (dest.is_ssa) {
1041 nir_ssa_values[dest.ssa.index] = bld.vgrf(BRW_REGISTER_TYPE_F,
1042 dest.ssa.num_components);
1043 return nir_ssa_values[dest.ssa.index];
1044 }
1045
1046 return fs_reg_for_nir_reg(this, dest.reg.reg, dest.reg.base_offset,
1047 dest.reg.indirect);
1048 }
1049
1050 fs_reg
1051 fs_visitor::get_nir_image_deref(const nir_deref_var *deref)
1052 {
1053 fs_reg image(UNIFORM, deref->var->data.driver_location,
1054 BRW_REGISTER_TYPE_UD);
1055
1056 if (deref->deref.child) {
1057 const nir_deref_array *deref_array =
1058 nir_deref_as_array(deref->deref.child);
1059 assert(deref->deref.child->deref_type == nir_deref_type_array &&
1060 deref_array->deref.child == NULL);
1061 const unsigned size = glsl_get_length(deref->var->type);
1062 const unsigned base = MIN2(deref_array->base_offset, size - 1);
1063
1064 image = offset(image, bld, base * BRW_IMAGE_PARAM_SIZE);
1065
1066 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
1067 fs_reg *tmp = new(mem_ctx) fs_reg(vgrf(glsl_type::int_type));
1068
1069 if (devinfo->gen == 7 && !devinfo->is_haswell) {
1070 /* IVB hangs when trying to access an invalid surface index with
1071 * the dataport. According to the spec "if the index used to
1072 * select an individual element is negative or greater than or
1073 * equal to the size of the array, the results of the operation
1074 * are undefined but may not lead to termination" -- which is one
1075 * of the possible outcomes of the hang. Clamp the index to
1076 * prevent access outside of the array bounds.
1077 */
1078 bld.emit_minmax(*tmp, retype(get_nir_src(deref_array->indirect),
1079 BRW_REGISTER_TYPE_UD),
1080 fs_reg(size - base - 1), BRW_CONDITIONAL_L);
1081 } else {
1082 bld.MOV(*tmp, get_nir_src(deref_array->indirect));
1083 }
1084
1085 bld.MUL(*tmp, *tmp, fs_reg(BRW_IMAGE_PARAM_SIZE));
1086 image.reladdr = tmp;
1087 }
1088 }
1089
1090 return image;
1091 }
1092
1093 void
1094 fs_visitor::emit_percomp(const fs_builder &bld, const fs_inst &inst,
1095 unsigned wr_mask)
1096 {
1097 for (unsigned i = 0; i < 4; i++) {
1098 if (!((wr_mask >> i) & 1))
1099 continue;
1100
1101 fs_inst *new_inst = new(mem_ctx) fs_inst(inst);
1102 new_inst->dst = offset(new_inst->dst, bld, i);
1103 for (unsigned j = 0; j < new_inst->sources; j++)
1104 if (new_inst->src[j].file == GRF)
1105 new_inst->src[j] = offset(new_inst->src[j], bld, i);
1106
1107 bld.emit(new_inst);
1108 }
1109 }
1110
1111 /**
1112 * Get the matching channel register datatype for an image intrinsic of the
1113 * specified GLSL image type.
1114 */
1115 static brw_reg_type
1116 get_image_base_type(const glsl_type *type)
1117 {
1118 switch ((glsl_base_type)type->sampler_type) {
1119 case GLSL_TYPE_UINT:
1120 return BRW_REGISTER_TYPE_UD;
1121 case GLSL_TYPE_INT:
1122 return BRW_REGISTER_TYPE_D;
1123 case GLSL_TYPE_FLOAT:
1124 return BRW_REGISTER_TYPE_F;
1125 default:
1126 unreachable("Not reached.");
1127 }
1128 }
1129
1130 /**
1131 * Get the appropriate atomic op for an image atomic intrinsic.
1132 */
1133 static unsigned
1134 get_image_atomic_op(nir_intrinsic_op op, const glsl_type *type)
1135 {
1136 switch (op) {
1137 case nir_intrinsic_image_atomic_add:
1138 return BRW_AOP_ADD;
1139 case nir_intrinsic_image_atomic_min:
1140 return (get_image_base_type(type) == BRW_REGISTER_TYPE_D ?
1141 BRW_AOP_IMIN : BRW_AOP_UMIN);
1142 case nir_intrinsic_image_atomic_max:
1143 return (get_image_base_type(type) == BRW_REGISTER_TYPE_D ?
1144 BRW_AOP_IMAX : BRW_AOP_UMAX);
1145 case nir_intrinsic_image_atomic_and:
1146 return BRW_AOP_AND;
1147 case nir_intrinsic_image_atomic_or:
1148 return BRW_AOP_OR;
1149 case nir_intrinsic_image_atomic_xor:
1150 return BRW_AOP_XOR;
1151 case nir_intrinsic_image_atomic_exchange:
1152 return BRW_AOP_MOV;
1153 case nir_intrinsic_image_atomic_comp_swap:
1154 return BRW_AOP_CMPWR;
1155 default:
1156 unreachable("Not reachable.");
1157 }
1158 }
1159
1160 static fs_inst *
1161 emit_pixel_interpolater_send(const fs_builder &bld,
1162 enum opcode opcode,
1163 const fs_reg &dst,
1164 const fs_reg &src,
1165 const fs_reg &desc,
1166 glsl_interp_qualifier interpolation)
1167 {
1168 fs_inst *inst;
1169 fs_reg payload;
1170 int mlen;
1171
1172 if (src.file == BAD_FILE) {
1173 /* Dummy payload */
1174 payload = bld.vgrf(BRW_REGISTER_TYPE_F, 1);
1175 mlen = 1;
1176 } else {
1177 payload = src;
1178 mlen = 2 * bld.dispatch_width() / 8;
1179 }
1180
1181 inst = bld.emit(opcode, dst, payload, desc);
1182 inst->mlen = mlen;
1183 /* 2 floats per slot returned */
1184 inst->regs_written = 2 * bld.dispatch_width() / 8;
1185 inst->pi_noperspective = interpolation == INTERP_QUALIFIER_NOPERSPECTIVE;
1186
1187 return inst;
1188 }
1189
1190 void
1191 fs_visitor::nir_emit_intrinsic(const fs_builder &bld, nir_intrinsic_instr *instr)
1192 {
1193 fs_reg dest;
1194 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
1195 dest = get_nir_dest(instr->dest);
1196
1197 bool has_indirect = false;
1198
1199 switch (instr->intrinsic) {
1200 case nir_intrinsic_discard:
1201 case nir_intrinsic_discard_if: {
1202 /* We track our discarded pixels in f0.1. By predicating on it, we can
1203 * update just the flag bits that aren't yet discarded. If there's no
1204 * condition, we emit a CMP of g0 != g0, so all currently executing
1205 * channels will get turned off.
1206 */
1207 fs_inst *cmp;
1208 if (instr->intrinsic == nir_intrinsic_discard_if) {
1209 cmp = bld.CMP(bld.null_reg_f(), get_nir_src(instr->src[0]),
1210 fs_reg(0), BRW_CONDITIONAL_Z);
1211 } else {
1212 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
1213 BRW_REGISTER_TYPE_UW));
1214 cmp = bld.CMP(bld.null_reg_f(), some_reg, some_reg, BRW_CONDITIONAL_NZ);
1215 }
1216 cmp->predicate = BRW_PREDICATE_NORMAL;
1217 cmp->flag_subreg = 1;
1218
1219 if (devinfo->gen >= 6) {
1220 emit_discard_jump();
1221 }
1222 break;
1223 }
1224
1225 case nir_intrinsic_atomic_counter_inc:
1226 case nir_intrinsic_atomic_counter_dec:
1227 case nir_intrinsic_atomic_counter_read: {
1228 using namespace surface_access;
1229
1230 /* Get the arguments of the atomic intrinsic. */
1231 const fs_reg offset = get_nir_src(instr->src[0]);
1232 const unsigned surface = (stage_prog_data->binding_table.abo_start +
1233 instr->const_index[0]);
1234 fs_reg tmp;
1235
1236 /* Emit a surface read or atomic op. */
1237 switch (instr->intrinsic) {
1238 case nir_intrinsic_atomic_counter_read:
1239 tmp = emit_untyped_read(bld, fs_reg(surface), offset, 1, 1);
1240 break;
1241
1242 case nir_intrinsic_atomic_counter_inc:
1243 tmp = emit_untyped_atomic(bld, fs_reg(surface), offset, fs_reg(),
1244 fs_reg(), 1, 1, BRW_AOP_INC);
1245 break;
1246
1247 case nir_intrinsic_atomic_counter_dec:
1248 tmp = emit_untyped_atomic(bld, fs_reg(surface), offset, fs_reg(),
1249 fs_reg(), 1, 1, BRW_AOP_PREDEC);
1250 break;
1251
1252 default:
1253 unreachable("Unreachable");
1254 }
1255
1256 /* Assign the result. */
1257 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD), tmp);
1258
1259 /* Mark the surface as used. */
1260 brw_mark_surface_used(stage_prog_data, surface);
1261 break;
1262 }
1263
1264 case nir_intrinsic_image_load:
1265 case nir_intrinsic_image_store:
1266 case nir_intrinsic_image_atomic_add:
1267 case nir_intrinsic_image_atomic_min:
1268 case nir_intrinsic_image_atomic_max:
1269 case nir_intrinsic_image_atomic_and:
1270 case nir_intrinsic_image_atomic_or:
1271 case nir_intrinsic_image_atomic_xor:
1272 case nir_intrinsic_image_atomic_exchange:
1273 case nir_intrinsic_image_atomic_comp_swap: {
1274 using namespace image_access;
1275
1276 /* Get the referenced image variable and type. */
1277 const nir_variable *var = instr->variables[0]->var;
1278 const glsl_type *type = var->type->without_array();
1279 const brw_reg_type base_type = get_image_base_type(type);
1280
1281 /* Get some metadata from the image intrinsic. */
1282 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
1283 const unsigned arr_dims = type->sampler_array ? 1 : 0;
1284 const unsigned surf_dims = type->coordinate_components() - arr_dims;
1285 const mesa_format format =
1286 (var->data.image.write_only ? MESA_FORMAT_NONE :
1287 _mesa_get_shader_image_format(var->data.image.format));
1288
1289 /* Get the arguments of the image intrinsic. */
1290 const fs_reg image = get_nir_image_deref(instr->variables[0]);
1291 const fs_reg addr = retype(get_nir_src(instr->src[0]),
1292 BRW_REGISTER_TYPE_UD);
1293 const fs_reg src0 = (info->num_srcs >= 3 ?
1294 retype(get_nir_src(instr->src[2]), base_type) :
1295 fs_reg());
1296 const fs_reg src1 = (info->num_srcs >= 4 ?
1297 retype(get_nir_src(instr->src[3]), base_type) :
1298 fs_reg());
1299 fs_reg tmp;
1300
1301 /* Emit an image load, store or atomic op. */
1302 if (instr->intrinsic == nir_intrinsic_image_load)
1303 tmp = emit_image_load(bld, image, addr, surf_dims, arr_dims, format);
1304
1305 else if (instr->intrinsic == nir_intrinsic_image_store)
1306 emit_image_store(bld, image, addr, src0, surf_dims, arr_dims, format);
1307
1308 else
1309 tmp = emit_image_atomic(bld, image, addr, src0, src1,
1310 surf_dims, arr_dims, info->dest_components,
1311 get_image_atomic_op(instr->intrinsic, type));
1312
1313 /* Assign the result. */
1314 for (unsigned c = 0; c < info->dest_components; ++c)
1315 bld.MOV(offset(retype(dest, base_type), bld, c),
1316 offset(tmp, bld, c));
1317 break;
1318 }
1319
1320 case nir_intrinsic_memory_barrier: {
1321 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 16 / dispatch_width);
1322 bld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp)
1323 ->regs_written = 2;
1324 break;
1325 }
1326
1327 case nir_intrinsic_image_size: {
1328 /* Get the referenced image variable and type. */
1329 const nir_variable *var = instr->variables[0]->var;
1330 const glsl_type *type = var->type->without_array();
1331
1332 /* Get the size of the image. */
1333 const fs_reg image = get_nir_image_deref(instr->variables[0]);
1334 const fs_reg size = offset(image, bld, BRW_IMAGE_PARAM_SIZE_OFFSET);
1335
1336 /* For 1DArray image types, the array index is stored in the Z component.
1337 * Fix this by swizzling the Z component to the Y component.
1338 */
1339 const bool is_1d_array_image =
1340 type->sampler_dimensionality == GLSL_SAMPLER_DIM_1D &&
1341 type->sampler_array;
1342
1343 /* For CubeArray images, we should count the number of cubes instead
1344 * of the number of faces. Fix it by dividing the (Z component) by 6.
1345 */
1346 const bool is_cube_array_image =
1347 type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE &&
1348 type->sampler_array;
1349
1350 /* Copy all the components. */
1351 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
1352 for (unsigned c = 0; c < info->dest_components; ++c) {
1353 if ((int)c >= type->coordinate_components()) {
1354 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
1355 fs_reg(1));
1356 } else if (c == 1 && is_1d_array_image) {
1357 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
1358 offset(size, bld, 2));
1359 } else if (c == 2 && is_cube_array_image) {
1360 bld.emit(SHADER_OPCODE_INT_QUOTIENT,
1361 offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
1362 offset(size, bld, c), fs_reg(6));
1363 } else {
1364 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
1365 offset(size, bld, c));
1366 }
1367 }
1368
1369 break;
1370 }
1371
1372 case nir_intrinsic_image_samples:
1373 /* The driver does not support multi-sampled images. */
1374 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), fs_reg(1));
1375 break;
1376
1377 case nir_intrinsic_load_front_face:
1378 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D),
1379 *emit_frontfacing_interpolation());
1380 break;
1381
1382 case nir_intrinsic_load_vertex_id:
1383 unreachable("should be lowered by lower_vertex_id()");
1384
1385 case nir_intrinsic_load_primitive_id:
1386 assert(stage == MESA_SHADER_GEOMETRY);
1387 assert(((struct brw_gs_prog_data *)prog_data)->include_primitive_id);
1388 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
1389 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD));
1390 break;
1391
1392 case nir_intrinsic_load_vertex_id_zero_base:
1393 case nir_intrinsic_load_base_vertex:
1394 case nir_intrinsic_load_instance_id:
1395 case nir_intrinsic_load_invocation_id:
1396 case nir_intrinsic_load_sample_mask_in:
1397 case nir_intrinsic_load_sample_id: {
1398 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
1399 fs_reg val = nir_system_values[sv];
1400 assert(val.file != BAD_FILE);
1401 dest.type = val.type;
1402 bld.MOV(dest, val);
1403 break;
1404 }
1405
1406 case nir_intrinsic_load_sample_pos: {
1407 fs_reg sample_pos = nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
1408 assert(sample_pos.file != BAD_FILE);
1409 dest.type = sample_pos.type;
1410 bld.MOV(dest, sample_pos);
1411 bld.MOV(offset(dest, bld, 1), offset(sample_pos, bld, 1));
1412 break;
1413 }
1414
1415 case nir_intrinsic_load_uniform_indirect:
1416 has_indirect = true;
1417 /* fallthrough */
1418 case nir_intrinsic_load_uniform: {
1419 fs_reg uniform_reg(UNIFORM, instr->const_index[0]);
1420 uniform_reg.reg_offset = instr->const_index[1];
1421
1422 for (unsigned j = 0; j < instr->num_components; j++) {
1423 fs_reg src = offset(retype(uniform_reg, dest.type), bld, j);
1424 if (has_indirect)
1425 src.reladdr = new(mem_ctx) fs_reg(get_nir_src(instr->src[0]));
1426
1427 bld.MOV(dest, src);
1428 dest = offset(dest, bld, 1);
1429 }
1430 break;
1431 }
1432
1433 case nir_intrinsic_load_ubo_indirect:
1434 has_indirect = true;
1435 /* fallthrough */
1436 case nir_intrinsic_load_ubo: {
1437 nir_const_value *const_index = nir_src_as_const_value(instr->src[0]);
1438 fs_reg surf_index;
1439
1440 if (const_index) {
1441 surf_index = fs_reg(stage_prog_data->binding_table.ubo_start +
1442 const_index->u[0]);
1443 } else {
1444 /* The block index is not a constant. Evaluate the index expression
1445 * per-channel and add the base UBO index; we have to select a value
1446 * from any live channel.
1447 */
1448 surf_index = vgrf(glsl_type::uint_type);
1449 bld.ADD(surf_index, get_nir_src(instr->src[0]),
1450 fs_reg(stage_prog_data->binding_table.ubo_start));
1451 surf_index = bld.emit_uniformize(surf_index);
1452
1453 /* Assume this may touch any UBO. It would be nice to provide
1454 * a tighter bound, but the array information is already lowered away.
1455 */
1456 brw_mark_surface_used(prog_data,
1457 stage_prog_data->binding_table.ubo_start +
1458 nir->info.num_ubos - 1);
1459 }
1460
1461 if (has_indirect) {
1462 /* Turn the byte offset into a dword offset. */
1463 fs_reg base_offset = vgrf(glsl_type::int_type);
1464 bld.SHR(base_offset, retype(get_nir_src(instr->src[1]),
1465 BRW_REGISTER_TYPE_D),
1466 fs_reg(2));
1467
1468 unsigned vec4_offset = instr->const_index[0] / 4;
1469 for (int i = 0; i < instr->num_components; i++)
1470 VARYING_PULL_CONSTANT_LOAD(bld, offset(dest, bld, i), surf_index,
1471 base_offset, vec4_offset + i);
1472 } else {
1473 fs_reg packed_consts = vgrf(glsl_type::float_type);
1474 packed_consts.type = dest.type;
1475
1476 fs_reg const_offset_reg((unsigned) instr->const_index[0] & ~15);
1477 bld.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD, packed_consts,
1478 surf_index, const_offset_reg);
1479
1480 for (unsigned i = 0; i < instr->num_components; i++) {
1481 packed_consts.set_smear(instr->const_index[0] % 16 / 4 + i);
1482
1483 /* The std140 packing rules don't allow vectors to cross 16-byte
1484 * boundaries, and a reg is 32 bytes.
1485 */
1486 assert(packed_consts.subreg_offset < 32);
1487
1488 bld.MOV(dest, packed_consts);
1489 dest = offset(dest, bld, 1);
1490 }
1491 }
1492 break;
1493 }
1494
1495 case nir_intrinsic_load_ssbo_indirect:
1496 has_indirect = true;
1497 /* fallthrough */
1498 case nir_intrinsic_load_ssbo: {
1499 assert(devinfo->gen >= 7);
1500
1501 nir_const_value *const_uniform_block =
1502 nir_src_as_const_value(instr->src[0]);
1503
1504 fs_reg surf_index;
1505 if (const_uniform_block) {
1506 unsigned index = stage_prog_data->binding_table.ssbo_start +
1507 const_uniform_block->u[0];
1508 surf_index = fs_reg(index);
1509 brw_mark_surface_used(prog_data, index);
1510 } else {
1511 surf_index = vgrf(glsl_type::uint_type);
1512 bld.ADD(surf_index, get_nir_src(instr->src[0]),
1513 fs_reg(stage_prog_data->binding_table.ssbo_start));
1514 surf_index = bld.emit_uniformize(surf_index);
1515
1516 /* Assume this may touch any UBO. It would be nice to provide
1517 * a tighter bound, but the array information is already lowered away.
1518 */
1519 brw_mark_surface_used(prog_data,
1520 stage_prog_data->binding_table.ssbo_start +
1521 nir->info.num_ssbos - 1);
1522 }
1523
1524 /* Get the offset to read from */
1525 fs_reg offset_reg = vgrf(glsl_type::uint_type);
1526 unsigned const_offset_bytes = 0;
1527 if (has_indirect) {
1528 bld.MOV(offset_reg, get_nir_src(instr->src[1]));
1529 } else {
1530 const_offset_bytes = instr->const_index[0];
1531 bld.MOV(offset_reg, fs_reg(const_offset_bytes));
1532 }
1533
1534 /* Read the vector */
1535 fs_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
1536 1 /* dims */,
1537 instr->num_components,
1538 BRW_PREDICATE_NONE);
1539 read_result.type = dest.type;
1540 for (int i = 0; i < instr->num_components; i++)
1541 bld.MOV(offset(dest, bld, i), offset(read_result, bld, i));
1542
1543 break;
1544 }
1545
1546 case nir_intrinsic_load_input_indirect:
1547 has_indirect = true;
1548 /* fallthrough */
1549 case nir_intrinsic_load_input: {
1550 unsigned index = 0;
1551 for (unsigned j = 0; j < instr->num_components; j++) {
1552 fs_reg src;
1553 if (stage == MESA_SHADER_VERTEX) {
1554 src = offset(fs_reg(ATTR, instr->const_index[0], dest.type), bld, index);
1555 } else {
1556 src = offset(retype(nir_inputs, dest.type), bld,
1557 instr->const_index[0] + index);
1558 }
1559 if (has_indirect)
1560 src.reladdr = new(mem_ctx) fs_reg(get_nir_src(instr->src[0]));
1561 index++;
1562
1563 bld.MOV(dest, src);
1564 dest = offset(dest, bld, 1);
1565 }
1566 break;
1567 }
1568
1569 /* Handle ARB_gpu_shader5 interpolation intrinsics
1570 *
1571 * It's worth a quick word of explanation as to why we handle the full
1572 * variable-based interpolation intrinsic rather than a lowered version
1573 * with like we do for other inputs. We have to do that because the way
1574 * we set up inputs doesn't allow us to use the already setup inputs for
1575 * interpolation. At the beginning of the shader, we go through all of
1576 * the input variables and do the initial interpolation and put it in
1577 * the nir_inputs array based on its location as determined in
1578 * nir_lower_io. If the input isn't used, dead code cleans up and
1579 * everything works fine. However, when we get to the ARB_gpu_shader5
1580 * interpolation intrinsics, we need to reinterpolate the input
1581 * differently. If we used an intrinsic that just had an index it would
1582 * only give us the offset into the nir_inputs array. However, this is
1583 * useless because that value is post-interpolation and we need
1584 * pre-interpolation. In order to get the actual location of the bits
1585 * we get from the vertex fetching hardware, we need the variable.
1586 */
1587 case nir_intrinsic_interp_var_at_centroid:
1588 case nir_intrinsic_interp_var_at_sample:
1589 case nir_intrinsic_interp_var_at_offset: {
1590 assert(stage == MESA_SHADER_FRAGMENT);
1591
1592 ((struct brw_wm_prog_data *) prog_data)->pulls_bary = true;
1593
1594 fs_reg dst_xy = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
1595 const glsl_interp_qualifier interpolation =
1596 (glsl_interp_qualifier) instr->variables[0]->var->data.interpolation;
1597
1598 switch (instr->intrinsic) {
1599 case nir_intrinsic_interp_var_at_centroid:
1600 emit_pixel_interpolater_send(bld,
1601 FS_OPCODE_INTERPOLATE_AT_CENTROID,
1602 dst_xy,
1603 fs_reg(), /* src */
1604 fs_reg(0u),
1605 interpolation);
1606 break;
1607
1608 case nir_intrinsic_interp_var_at_sample: {
1609 nir_const_value *const_sample = nir_src_as_const_value(instr->src[0]);
1610
1611 if (const_sample) {
1612 unsigned msg_data = const_sample->i[0] << 4;
1613
1614 emit_pixel_interpolater_send(bld,
1615 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
1616 dst_xy,
1617 fs_reg(), /* src */
1618 fs_reg(msg_data),
1619 interpolation);
1620 } else {
1621 const fs_reg sample_src = retype(get_nir_src(instr->src[0]),
1622 BRW_REGISTER_TYPE_UD);
1623
1624 if (nir_src_is_dynamically_uniform(instr->src[0])) {
1625 const fs_reg sample_id = bld.emit_uniformize(sample_src);
1626 const fs_reg msg_data = vgrf(glsl_type::uint_type);
1627 bld.exec_all().group(1, 0).SHL(msg_data, sample_id, fs_reg(4u));
1628 emit_pixel_interpolater_send(bld,
1629 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
1630 dst_xy,
1631 fs_reg(), /* src */
1632 msg_data,
1633 interpolation);
1634 } else {
1635 /* Make a loop that sends a message to the pixel interpolater
1636 * for the sample number in each live channel. If there are
1637 * multiple channels with the same sample number then these
1638 * will be handled simultaneously with a single interation of
1639 * the loop.
1640 */
1641 bld.emit(BRW_OPCODE_DO);
1642
1643 /* Get the next live sample number into sample_id_reg */
1644 const fs_reg sample_id = bld.emit_uniformize(sample_src);
1645
1646 /* Set the flag register so that we can perform the send
1647 * message on all channels that have the same sample number
1648 */
1649 bld.CMP(bld.null_reg_ud(),
1650 sample_src, sample_id,
1651 BRW_CONDITIONAL_EQ);
1652 const fs_reg msg_data = vgrf(glsl_type::uint_type);
1653 bld.exec_all().group(1, 0).SHL(msg_data, sample_id, fs_reg(4u));
1654 fs_inst *inst =
1655 emit_pixel_interpolater_send(bld,
1656 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
1657 dst_xy,
1658 fs_reg(), /* src */
1659 msg_data,
1660 interpolation);
1661 set_predicate(BRW_PREDICATE_NORMAL, inst);
1662
1663 /* Continue the loop if there are any live channels left */
1664 set_predicate_inv(BRW_PREDICATE_NORMAL,
1665 true, /* inverse */
1666 bld.emit(BRW_OPCODE_WHILE));
1667 }
1668 }
1669
1670 break;
1671 }
1672
1673 case nir_intrinsic_interp_var_at_offset: {
1674 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
1675
1676 if (const_offset) {
1677 unsigned off_x = MIN2((int)(const_offset->f[0] * 16), 7) & 0xf;
1678 unsigned off_y = MIN2((int)(const_offset->f[1] * 16), 7) & 0xf;
1679
1680 emit_pixel_interpolater_send(bld,
1681 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET,
1682 dst_xy,
1683 fs_reg(), /* src */
1684 fs_reg(off_x | (off_y << 4)),
1685 interpolation);
1686 } else {
1687 fs_reg src = vgrf(glsl_type::ivec2_type);
1688 fs_reg offset_src = retype(get_nir_src(instr->src[0]),
1689 BRW_REGISTER_TYPE_F);
1690 for (int i = 0; i < 2; i++) {
1691 fs_reg temp = vgrf(glsl_type::float_type);
1692 bld.MUL(temp, offset(offset_src, bld, i), fs_reg(16.0f));
1693 fs_reg itemp = vgrf(glsl_type::int_type);
1694 bld.MOV(itemp, temp); /* float to int */
1695
1696 /* Clamp the upper end of the range to +7/16.
1697 * ARB_gpu_shader5 requires that we support a maximum offset
1698 * of +0.5, which isn't representable in a S0.4 value -- if
1699 * we didn't clamp it, we'd end up with -8/16, which is the
1700 * opposite of what the shader author wanted.
1701 *
1702 * This is legal due to ARB_gpu_shader5's quantization
1703 * rules:
1704 *
1705 * "Not all values of <offset> may be supported; x and y
1706 * offsets may be rounded to fixed-point values with the
1707 * number of fraction bits given by the
1708 * implementation-dependent constant
1709 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
1710 */
1711 set_condmod(BRW_CONDITIONAL_L,
1712 bld.SEL(offset(src, bld, i), itemp, fs_reg(7)));
1713 }
1714
1715 const enum opcode opcode = FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET;
1716 emit_pixel_interpolater_send(bld,
1717 opcode,
1718 dst_xy,
1719 src,
1720 fs_reg(0u),
1721 interpolation);
1722 }
1723 break;
1724 }
1725
1726 default:
1727 unreachable("Invalid intrinsic");
1728 }
1729
1730 for (unsigned j = 0; j < instr->num_components; j++) {
1731 fs_reg src = interp_reg(instr->variables[0]->var->data.location, j);
1732 src.type = dest.type;
1733
1734 bld.emit(FS_OPCODE_LINTERP, dest, dst_xy, src);
1735 dest = offset(dest, bld, 1);
1736 }
1737 break;
1738 }
1739
1740 case nir_intrinsic_store_ssbo_indirect:
1741 has_indirect = true;
1742 /* fallthrough */
1743 case nir_intrinsic_store_ssbo: {
1744 assert(devinfo->gen >= 7);
1745
1746 /* Block index */
1747 fs_reg surf_index;
1748 nir_const_value *const_uniform_block =
1749 nir_src_as_const_value(instr->src[1]);
1750 if (const_uniform_block) {
1751 unsigned index = stage_prog_data->binding_table.ssbo_start +
1752 const_uniform_block->u[0];
1753 surf_index = fs_reg(index);
1754 brw_mark_surface_used(prog_data, index);
1755 } else {
1756 surf_index = vgrf(glsl_type::uint_type);
1757 bld.ADD(surf_index, get_nir_src(instr->src[1]),
1758 fs_reg(stage_prog_data->binding_table.ssbo_start));
1759 surf_index = bld.emit_uniformize(surf_index);
1760
1761 brw_mark_surface_used(prog_data,
1762 stage_prog_data->binding_table.ssbo_start +
1763 nir->info.num_ssbos - 1);
1764 }
1765
1766 /* Offset */
1767 fs_reg offset_reg = vgrf(glsl_type::uint_type);
1768 unsigned const_offset_bytes = 0;
1769 if (has_indirect) {
1770 bld.MOV(offset_reg, get_nir_src(instr->src[2]));
1771 } else {
1772 const_offset_bytes = instr->const_index[0];
1773 bld.MOV(offset_reg, fs_reg(const_offset_bytes));
1774 }
1775
1776 /* Value */
1777 fs_reg val_reg = get_nir_src(instr->src[0]);
1778
1779 /* Writemask */
1780 unsigned writemask = instr->const_index[1];
1781
1782 /* Write each component present in the writemask */
1783 unsigned skipped_channels = 0;
1784 for (int i = 0; i < instr->num_components; i++) {
1785 int component_mask = 1 << i;
1786 if (writemask & component_mask) {
1787 if (skipped_channels) {
1788 if (!has_indirect) {
1789 const_offset_bytes += 4 * skipped_channels;
1790 bld.MOV(offset_reg, fs_reg(const_offset_bytes));
1791 } else {
1792 bld.ADD(offset_reg, offset_reg,
1793 brw_imm_ud(4 * skipped_channels));
1794 }
1795 skipped_channels = 0;
1796 }
1797
1798 emit_untyped_write(bld, surf_index, offset_reg,
1799 offset(val_reg, bld, i),
1800 1 /* dims */, 1 /* size */,
1801 BRW_PREDICATE_NONE);
1802 }
1803
1804 skipped_channels++;
1805 }
1806 break;
1807 }
1808
1809 case nir_intrinsic_store_output_indirect:
1810 has_indirect = true;
1811 /* fallthrough */
1812 case nir_intrinsic_store_output: {
1813 fs_reg src = get_nir_src(instr->src[0]);
1814 unsigned index = 0;
1815 for (unsigned j = 0; j < instr->num_components; j++) {
1816 fs_reg new_dest = offset(retype(nir_outputs, src.type), bld,
1817 instr->const_index[0] + index);
1818 if (has_indirect)
1819 src.reladdr = new(mem_ctx) fs_reg(get_nir_src(instr->src[1]));
1820 index++;
1821 bld.MOV(new_dest, src);
1822 src = offset(src, bld, 1);
1823 }
1824 break;
1825 }
1826
1827 case nir_intrinsic_barrier:
1828 emit_barrier();
1829 if (stage == MESA_SHADER_COMPUTE)
1830 ((struct brw_cs_prog_data *) prog_data)->uses_barrier = true;
1831 break;
1832
1833 case nir_intrinsic_load_local_invocation_id:
1834 case nir_intrinsic_load_work_group_id: {
1835 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
1836 fs_reg val = nir_system_values[sv];
1837 assert(val.file != BAD_FILE);
1838 dest.type = val.type;
1839 for (unsigned i = 0; i < 3; i++)
1840 bld.MOV(offset(dest, bld, i), offset(val, bld, i));
1841 break;
1842 }
1843
1844 case nir_intrinsic_ssbo_atomic_add:
1845 nir_emit_ssbo_atomic(bld, BRW_AOP_ADD, instr);
1846 break;
1847 case nir_intrinsic_ssbo_atomic_imin:
1848 nir_emit_ssbo_atomic(bld, BRW_AOP_IMIN, instr);
1849 break;
1850 case nir_intrinsic_ssbo_atomic_umin:
1851 nir_emit_ssbo_atomic(bld, BRW_AOP_UMIN, instr);
1852 break;
1853 case nir_intrinsic_ssbo_atomic_imax:
1854 nir_emit_ssbo_atomic(bld, BRW_AOP_IMAX, instr);
1855 break;
1856 case nir_intrinsic_ssbo_atomic_umax:
1857 nir_emit_ssbo_atomic(bld, BRW_AOP_UMAX, instr);
1858 break;
1859 case nir_intrinsic_ssbo_atomic_and:
1860 nir_emit_ssbo_atomic(bld, BRW_AOP_AND, instr);
1861 break;
1862 case nir_intrinsic_ssbo_atomic_or:
1863 nir_emit_ssbo_atomic(bld, BRW_AOP_OR, instr);
1864 break;
1865 case nir_intrinsic_ssbo_atomic_xor:
1866 nir_emit_ssbo_atomic(bld, BRW_AOP_XOR, instr);
1867 break;
1868 case nir_intrinsic_ssbo_atomic_exchange:
1869 nir_emit_ssbo_atomic(bld, BRW_AOP_MOV, instr);
1870 break;
1871 case nir_intrinsic_ssbo_atomic_comp_swap:
1872 nir_emit_ssbo_atomic(bld, BRW_AOP_CMPWR, instr);
1873 break;
1874
1875 case nir_intrinsic_get_buffer_size: {
1876 nir_const_value *const_uniform_block = nir_src_as_const_value(instr->src[0]);
1877 unsigned ssbo_index = const_uniform_block ? const_uniform_block->u[0] : 0;
1878 int reg_width = dispatch_width / 8;
1879
1880 /* Set LOD = 0 */
1881 fs_reg source = fs_reg(0);
1882
1883 int mlen = 1 * reg_width;
1884 fs_reg src_payload = fs_reg(GRF, alloc.allocate(mlen),
1885 BRW_REGISTER_TYPE_UD);
1886 bld.LOAD_PAYLOAD(src_payload, &source, 1, 0);
1887
1888 fs_reg surf_index = fs_reg(prog_data->binding_table.ssbo_start + ssbo_index);
1889 fs_inst *inst = bld.emit(FS_OPCODE_GET_BUFFER_SIZE, dest,
1890 src_payload, surf_index);
1891 inst->header_size = 0;
1892 inst->mlen = mlen;
1893 bld.emit(inst);
1894 break;
1895 }
1896
1897 case nir_intrinsic_load_num_work_groups: {
1898 assert(devinfo->gen >= 7);
1899 assert(stage == MESA_SHADER_COMPUTE);
1900
1901 struct brw_cs_prog_data *cs_prog_data =
1902 (struct brw_cs_prog_data *) prog_data;
1903 const unsigned surface =
1904 cs_prog_data->binding_table.work_groups_start;
1905
1906 cs_prog_data->uses_num_work_groups = true;
1907
1908 fs_reg surf_index = fs_reg(surface);
1909 brw_mark_surface_used(prog_data, surface);
1910
1911 /* Read the 3 GLuint components of gl_NumWorkGroups */
1912 for (unsigned i = 0; i < 3; i++) {
1913 fs_reg read_result =
1914 emit_untyped_read(bld, surf_index,
1915 fs_reg(i << 2),
1916 1 /* dims */, 1 /* size */,
1917 BRW_PREDICATE_NONE);
1918 read_result.type = dest.type;
1919 bld.MOV(dest, read_result);
1920 dest = offset(dest, bld, 1);
1921 }
1922 break;
1923 }
1924
1925 default:
1926 unreachable("unknown intrinsic");
1927 }
1928 }
1929
1930 void
1931 fs_visitor::nir_emit_ssbo_atomic(const fs_builder &bld,
1932 int op, nir_intrinsic_instr *instr)
1933 {
1934 fs_reg dest;
1935 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
1936 dest = get_nir_dest(instr->dest);
1937
1938 fs_reg surface;
1939 nir_const_value *const_surface = nir_src_as_const_value(instr->src[0]);
1940 if (const_surface) {
1941 unsigned surf_index = stage_prog_data->binding_table.ssbo_start +
1942 const_surface->u[0];
1943 surface = fs_reg(surf_index);
1944 brw_mark_surface_used(prog_data, surf_index);
1945 } else {
1946 surface = vgrf(glsl_type::uint_type);
1947 bld.ADD(surface, get_nir_src(instr->src[0]),
1948 fs_reg(stage_prog_data->binding_table.ssbo_start));
1949
1950 /* Assume this may touch any SSBO. This is the same we do for other
1951 * UBO/SSBO accesses with non-constant surface.
1952 */
1953 brw_mark_surface_used(prog_data,
1954 stage_prog_data->binding_table.ssbo_start +
1955 nir->info.num_ssbos - 1);
1956 }
1957
1958 fs_reg offset = get_nir_src(instr->src[1]);
1959 fs_reg data1 = get_nir_src(instr->src[2]);
1960 fs_reg data2;
1961 if (op == BRW_AOP_CMPWR)
1962 data2 = get_nir_src(instr->src[3]);
1963
1964 /* Emit the actual atomic operation operation */
1965
1966 fs_reg atomic_result =
1967 surface_access::emit_untyped_atomic(bld, surface, offset,
1968 data1, data2,
1969 1 /* dims */, 1 /* rsize */,
1970 op,
1971 BRW_PREDICATE_NONE);
1972 dest.type = atomic_result.type;
1973 bld.MOV(dest, atomic_result);
1974 }
1975
1976 void
1977 fs_visitor::nir_emit_texture(const fs_builder &bld, nir_tex_instr *instr)
1978 {
1979 unsigned sampler = instr->sampler_index;
1980 fs_reg sampler_reg(sampler);
1981
1982 int gather_component = instr->component;
1983
1984 bool is_rect = instr->sampler_dim == GLSL_SAMPLER_DIM_RECT;
1985
1986 bool is_cube_array = instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
1987 instr->is_array;
1988
1989 int lod_components = 0;
1990 int UNUSED offset_components = 0;
1991
1992 fs_reg coordinate, shadow_comparitor, lod, lod2, sample_index, mcs, tex_offset;
1993
1994 for (unsigned i = 0; i < instr->num_srcs; i++) {
1995 fs_reg src = get_nir_src(instr->src[i].src);
1996 switch (instr->src[i].src_type) {
1997 case nir_tex_src_bias:
1998 lod = retype(src, BRW_REGISTER_TYPE_F);
1999 break;
2000 case nir_tex_src_comparitor:
2001 shadow_comparitor = retype(src, BRW_REGISTER_TYPE_F);
2002 break;
2003 case nir_tex_src_coord:
2004 switch (instr->op) {
2005 case nir_texop_txf:
2006 case nir_texop_txf_ms:
2007 coordinate = retype(src, BRW_REGISTER_TYPE_D);
2008 break;
2009 default:
2010 coordinate = retype(src, BRW_REGISTER_TYPE_F);
2011 break;
2012 }
2013 break;
2014 case nir_tex_src_ddx:
2015 lod = retype(src, BRW_REGISTER_TYPE_F);
2016 lod_components = nir_tex_instr_src_size(instr, i);
2017 break;
2018 case nir_tex_src_ddy:
2019 lod2 = retype(src, BRW_REGISTER_TYPE_F);
2020 break;
2021 case nir_tex_src_lod:
2022 switch (instr->op) {
2023 case nir_texop_txs:
2024 lod = retype(src, BRW_REGISTER_TYPE_UD);
2025 break;
2026 case nir_texop_txf:
2027 lod = retype(src, BRW_REGISTER_TYPE_D);
2028 break;
2029 default:
2030 lod = retype(src, BRW_REGISTER_TYPE_F);
2031 break;
2032 }
2033 break;
2034 case nir_tex_src_ms_index:
2035 sample_index = retype(src, BRW_REGISTER_TYPE_UD);
2036 break;
2037 case nir_tex_src_offset:
2038 tex_offset = retype(src, BRW_REGISTER_TYPE_D);
2039 if (instr->is_array)
2040 offset_components = instr->coord_components - 1;
2041 else
2042 offset_components = instr->coord_components;
2043 break;
2044 case nir_tex_src_projector:
2045 unreachable("should be lowered");
2046
2047 case nir_tex_src_sampler_offset: {
2048 /* Figure out the highest possible sampler index and mark it as used */
2049 uint32_t max_used = sampler + instr->sampler_array_size - 1;
2050 if (instr->op == nir_texop_tg4 && devinfo->gen < 8) {
2051 max_used += stage_prog_data->binding_table.gather_texture_start;
2052 } else {
2053 max_used += stage_prog_data->binding_table.texture_start;
2054 }
2055 brw_mark_surface_used(prog_data, max_used);
2056
2057 /* Emit code to evaluate the actual indexing expression */
2058 sampler_reg = vgrf(glsl_type::uint_type);
2059 bld.ADD(sampler_reg, src, fs_reg(sampler));
2060 sampler_reg = bld.emit_uniformize(sampler_reg);
2061 break;
2062 }
2063
2064 default:
2065 unreachable("unknown texture source");
2066 }
2067 }
2068
2069 if (instr->op == nir_texop_txf_ms) {
2070 if (devinfo->gen >= 7 &&
2071 key_tex->compressed_multisample_layout_mask & (1 << sampler)) {
2072 mcs = emit_mcs_fetch(coordinate, instr->coord_components, sampler_reg);
2073 } else {
2074 mcs = fs_reg(0u);
2075 }
2076 }
2077
2078 for (unsigned i = 0; i < 3; i++) {
2079 if (instr->const_offset[i] != 0) {
2080 assert(offset_components == 0);
2081 tex_offset = fs_reg(brw_texture_offset(instr->const_offset, 3));
2082 break;
2083 }
2084 }
2085
2086 enum glsl_base_type dest_base_type =
2087 brw_glsl_base_type_for_nir_type (instr->dest_type);
2088
2089 const glsl_type *dest_type =
2090 glsl_type::get_instance(dest_base_type, nir_tex_instr_dest_size(instr),
2091 1);
2092
2093 ir_texture_opcode op;
2094 switch (instr->op) {
2095 case nir_texop_lod: op = ir_lod; break;
2096 case nir_texop_query_levels: op = ir_query_levels; break;
2097 case nir_texop_tex: op = ir_tex; break;
2098 case nir_texop_tg4: op = ir_tg4; break;
2099 case nir_texop_txb: op = ir_txb; break;
2100 case nir_texop_txd: op = ir_txd; break;
2101 case nir_texop_txf: op = ir_txf; break;
2102 case nir_texop_txf_ms: op = ir_txf_ms; break;
2103 case nir_texop_txl: op = ir_txl; break;
2104 case nir_texop_txs: op = ir_txs; break;
2105 case nir_texop_texture_samples: {
2106 fs_reg dst = retype(get_nir_dest(instr->dest), BRW_REGISTER_TYPE_D);
2107 fs_inst *inst = bld.emit(SHADER_OPCODE_SAMPLEINFO, dst,
2108 bld.vgrf(BRW_REGISTER_TYPE_D, 1),
2109 sampler_reg);
2110 inst->mlen = 1;
2111 inst->header_size = 1;
2112 inst->base_mrf = -1;
2113 return;
2114 }
2115 default:
2116 unreachable("unknown texture opcode");
2117 }
2118
2119 emit_texture(op, dest_type, coordinate, instr->coord_components,
2120 shadow_comparitor, lod, lod2, lod_components, sample_index,
2121 tex_offset, mcs, gather_component,
2122 is_cube_array, is_rect, sampler, sampler_reg);
2123
2124 fs_reg dest = get_nir_dest(instr->dest);
2125 dest.type = this->result.type;
2126 unsigned num_components = nir_tex_instr_dest_size(instr);
2127 emit_percomp(bld, fs_inst(BRW_OPCODE_MOV, bld.dispatch_width(),
2128 dest, this->result),
2129 (1 << num_components) - 1);
2130 }
2131
2132 void
2133 fs_visitor::nir_emit_jump(const fs_builder &bld, nir_jump_instr *instr)
2134 {
2135 switch (instr->type) {
2136 case nir_jump_break:
2137 bld.emit(BRW_OPCODE_BREAK);
2138 break;
2139 case nir_jump_continue:
2140 bld.emit(BRW_OPCODE_CONTINUE);
2141 break;
2142 case nir_jump_return:
2143 default:
2144 unreachable("unknown jump");
2145 }
2146 }