i965/fs: Migrate translation of NIR ALU instructions to the IR builder.
[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 "program/prog_to_nir.h"
28 #include "brw_fs.h"
29 #include "brw_nir.h"
30
31 using namespace brw;
32
33 void
34 fs_visitor::emit_nir_code()
35 {
36 nir_shader *nir = prog->nir;
37
38 /* emit the arrays used for inputs and outputs - load/store intrinsics will
39 * be converted to reads/writes of these arrays
40 */
41
42 if (nir->num_inputs > 0) {
43 nir_inputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_inputs);
44 nir_setup_inputs(nir);
45 }
46
47 if (nir->num_outputs > 0) {
48 nir_outputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_outputs);
49 nir_setup_outputs(nir);
50 }
51
52 if (nir->num_uniforms > 0) {
53 nir_setup_uniforms(nir);
54 }
55
56 nir_emit_system_values(nir);
57
58 nir_globals = ralloc_array(mem_ctx, fs_reg, nir->reg_alloc);
59 foreach_list_typed(nir_register, reg, node, &nir->registers) {
60 unsigned array_elems =
61 reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
62 unsigned size = array_elems * reg->num_components;
63 nir_globals[reg->index] = bld.vgrf(BRW_REGISTER_TYPE_F, size);
64 }
65
66 /* get the main function and emit it */
67 nir_foreach_overload(nir, overload) {
68 assert(strcmp(overload->function->name, "main") == 0);
69 assert(overload->impl);
70 nir_emit_impl(overload->impl);
71 }
72 }
73
74 void
75 fs_visitor::nir_setup_inputs(nir_shader *shader)
76 {
77 foreach_list_typed(nir_variable, var, node, &shader->inputs) {
78 enum brw_reg_type type = brw_type_for_base_type(var->type);
79 fs_reg input = offset(nir_inputs, var->data.driver_location);
80
81 fs_reg reg;
82 switch (stage) {
83 case MESA_SHADER_VERTEX: {
84 /* Our ATTR file is indexed by VERT_ATTRIB_*, which is the value
85 * stored in nir_variable::location.
86 *
87 * However, NIR's load_input intrinsics use a different index - an
88 * offset into a single contiguous array containing all inputs.
89 * This index corresponds to the nir_variable::driver_location field.
90 *
91 * So, we need to copy from fs_reg(ATTR, var->location) to
92 * offset(nir_inputs, var->data.driver_location).
93 */
94 unsigned components = var->type->without_array()->components();
95 unsigned array_length = var->type->is_array() ? var->type->length : 1;
96 for (unsigned i = 0; i < array_length; i++) {
97 for (unsigned j = 0; j < components; j++) {
98 bld.MOV(retype(offset(input, components * i + j), type),
99 offset(fs_reg(ATTR, var->data.location + i, type), j));
100 }
101 }
102 break;
103 }
104 case MESA_SHADER_GEOMETRY:
105 case MESA_SHADER_COMPUTE:
106 unreachable("fs_visitor not used for these stages yet.");
107 break;
108 case MESA_SHADER_FRAGMENT:
109 if (var->data.location == VARYING_SLOT_POS) {
110 reg = *emit_fragcoord_interpolation(var->data.pixel_center_integer,
111 var->data.origin_upper_left);
112 emit_percomp(bld, fs_inst(BRW_OPCODE_MOV, input, reg), 0xF);
113 } else {
114 emit_general_interpolation(input, var->name, var->type,
115 (glsl_interp_qualifier) var->data.interpolation,
116 var->data.location, var->data.centroid,
117 var->data.sample);
118 }
119 break;
120 }
121 }
122 }
123
124 void
125 fs_visitor::nir_setup_outputs(nir_shader *shader)
126 {
127 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
128
129 foreach_list_typed(nir_variable, var, node, &shader->outputs) {
130 fs_reg reg = offset(nir_outputs, var->data.driver_location);
131
132 int vector_elements =
133 var->type->is_array() ? var->type->fields.array->vector_elements
134 : var->type->vector_elements;
135
136 if (stage == MESA_SHADER_VERTEX) {
137 for (int i = 0; i < ALIGN(type_size(var->type), 4) / 4; i++) {
138 int output = var->data.location + i;
139 this->outputs[output] = offset(reg, 4 * i);
140 this->output_components[output] = vector_elements;
141 }
142 } else if (var->data.index > 0) {
143 assert(var->data.location == FRAG_RESULT_DATA0);
144 assert(var->data.index == 1);
145 this->dual_src_output = reg;
146 this->do_dual_src = true;
147 } else if (var->data.location == FRAG_RESULT_COLOR) {
148 /* Writing gl_FragColor outputs to all color regions. */
149 for (unsigned int i = 0; i < MAX2(key->nr_color_regions, 1); i++) {
150 this->outputs[i] = reg;
151 this->output_components[i] = 4;
152 }
153 } else if (var->data.location == FRAG_RESULT_DEPTH) {
154 this->frag_depth = reg;
155 } else if (var->data.location == FRAG_RESULT_SAMPLE_MASK) {
156 this->sample_mask = reg;
157 } else {
158 /* gl_FragData or a user-defined FS output */
159 assert(var->data.location >= FRAG_RESULT_DATA0 &&
160 var->data.location < FRAG_RESULT_DATA0 + BRW_MAX_DRAW_BUFFERS);
161
162 /* General color output. */
163 for (unsigned int i = 0; i < MAX2(1, var->type->length); i++) {
164 int output = var->data.location - FRAG_RESULT_DATA0 + i;
165 this->outputs[output] = offset(reg, vector_elements * i);
166 this->output_components[output] = vector_elements;
167 }
168 }
169 }
170 }
171
172 void
173 fs_visitor::nir_setup_uniforms(nir_shader *shader)
174 {
175 uniforms = shader->num_uniforms;
176 num_direct_uniforms = shader->num_direct_uniforms;
177
178 /* We split the uniform register file in half. The first half is
179 * entirely direct uniforms. The second half is indirect.
180 */
181 param_size[0] = num_direct_uniforms;
182 if (shader->num_uniforms > num_direct_uniforms)
183 param_size[num_direct_uniforms] = shader->num_uniforms - num_direct_uniforms;
184
185 if (dispatch_width != 8)
186 return;
187
188 if (shader_prog) {
189 foreach_list_typed(nir_variable, var, node, &shader->uniforms) {
190 /* UBO's and atomics don't take up space in the uniform file */
191 if (var->interface_type != NULL || var->type->contains_atomic())
192 continue;
193
194 if (strncmp(var->name, "gl_", 3) == 0)
195 nir_setup_builtin_uniform(var);
196 else
197 nir_setup_uniform(var);
198 }
199 } else {
200 /* prog_to_nir doesn't create uniform variables; set param up directly. */
201 for (unsigned p = 0; p < prog->Parameters->NumParameters; p++) {
202 for (unsigned int i = 0; i < 4; i++) {
203 stage_prog_data->param[4 * p + i] =
204 &prog->Parameters->ParameterValues[p][i];
205 }
206 }
207 }
208 }
209
210 void
211 fs_visitor::nir_setup_uniform(nir_variable *var)
212 {
213 int namelen = strlen(var->name);
214
215 /* The data for our (non-builtin) uniforms is stored in a series of
216 * gl_uniform_driver_storage structs for each subcomponent that
217 * glGetUniformLocation() could name. We know it's been set up in the
218 * same order we'd walk the type, so walk the list of storage and find
219 * anything with our name, or the prefix of a component that starts with
220 * our name.
221 */
222 unsigned index = var->data.driver_location;
223 for (unsigned u = 0; u < shader_prog->NumUniformStorage; u++) {
224 struct gl_uniform_storage *storage = &shader_prog->UniformStorage[u];
225
226 if (storage->builtin)
227 continue;
228
229 if (strncmp(var->name, storage->name, namelen) != 0 ||
230 (storage->name[namelen] != 0 &&
231 storage->name[namelen] != '.' &&
232 storage->name[namelen] != '[')) {
233 continue;
234 }
235
236 unsigned slots = storage->type->component_slots();
237 if (storage->array_elements)
238 slots *= storage->array_elements;
239
240 for (unsigned i = 0; i < slots; i++) {
241 stage_prog_data->param[index++] = &storage->storage[i];
242 }
243 }
244
245 /* Make sure we actually initialized the right amount of stuff here. */
246 assert(var->data.driver_location + var->type->component_slots() == index);
247 }
248
249 void
250 fs_visitor::nir_setup_builtin_uniform(nir_variable *var)
251 {
252 const nir_state_slot *const slots = var->state_slots;
253 assert(var->state_slots != NULL);
254
255 unsigned uniform_index = var->data.driver_location;
256 for (unsigned int i = 0; i < var->num_state_slots; i++) {
257 /* This state reference has already been setup by ir_to_mesa, but we'll
258 * get the same index back here.
259 */
260 int index = _mesa_add_state_reference(this->prog->Parameters,
261 (gl_state_index *)slots[i].tokens);
262
263 /* Add each of the unique swizzles of the element as a parameter.
264 * This'll end up matching the expected layout of the
265 * array/matrix/structure we're trying to fill in.
266 */
267 int last_swiz = -1;
268 for (unsigned int j = 0; j < 4; j++) {
269 int swiz = GET_SWZ(slots[i].swizzle, j);
270 if (swiz == last_swiz)
271 break;
272 last_swiz = swiz;
273
274 stage_prog_data->param[uniform_index++] =
275 &prog->Parameters->ParameterValues[index][swiz];
276 }
277 }
278 }
279
280 static bool
281 emit_system_values_block(nir_block *block, void *void_visitor)
282 {
283 fs_visitor *v = (fs_visitor *)void_visitor;
284 fs_reg *reg;
285
286 nir_foreach_instr(block, instr) {
287 if (instr->type != nir_instr_type_intrinsic)
288 continue;
289
290 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
291 switch (intrin->intrinsic) {
292 case nir_intrinsic_load_vertex_id:
293 unreachable("should be lowered by lower_vertex_id().");
294
295 case nir_intrinsic_load_vertex_id_zero_base:
296 assert(v->stage == MESA_SHADER_VERTEX);
297 reg = &v->nir_system_values[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE];
298 if (reg->file == BAD_FILE)
299 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE);
300 break;
301
302 case nir_intrinsic_load_base_vertex:
303 assert(v->stage == MESA_SHADER_VERTEX);
304 reg = &v->nir_system_values[SYSTEM_VALUE_BASE_VERTEX];
305 if (reg->file == BAD_FILE)
306 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_BASE_VERTEX);
307 break;
308
309 case nir_intrinsic_load_instance_id:
310 assert(v->stage == MESA_SHADER_VERTEX);
311 reg = &v->nir_system_values[SYSTEM_VALUE_INSTANCE_ID];
312 if (reg->file == BAD_FILE)
313 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_INSTANCE_ID);
314 break;
315
316 case nir_intrinsic_load_sample_pos:
317 assert(v->stage == MESA_SHADER_FRAGMENT);
318 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
319 if (reg->file == BAD_FILE)
320 *reg = *v->emit_samplepos_setup();
321 break;
322
323 case nir_intrinsic_load_sample_id:
324 assert(v->stage == MESA_SHADER_FRAGMENT);
325 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
326 if (reg->file == BAD_FILE)
327 *reg = *v->emit_sampleid_setup();
328 break;
329
330 case nir_intrinsic_load_sample_mask_in:
331 assert(v->stage == MESA_SHADER_FRAGMENT);
332 assert(v->devinfo->gen >= 7);
333 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_MASK_IN];
334 if (reg->file == BAD_FILE)
335 *reg = fs_reg(retype(brw_vec8_grf(v->payload.sample_mask_in_reg, 0),
336 BRW_REGISTER_TYPE_D));
337 break;
338
339 default:
340 break;
341 }
342 }
343
344 return true;
345 }
346
347 void
348 fs_visitor::nir_emit_system_values(nir_shader *shader)
349 {
350 nir_system_values = ralloc_array(mem_ctx, fs_reg, SYSTEM_VALUE_MAX);
351 nir_foreach_overload(shader, overload) {
352 assert(strcmp(overload->function->name, "main") == 0);
353 assert(overload->impl);
354 nir_foreach_block(overload->impl, emit_system_values_block, this);
355 }
356 }
357
358 void
359 fs_visitor::nir_emit_impl(nir_function_impl *impl)
360 {
361 nir_locals = reralloc(mem_ctx, nir_locals, fs_reg, impl->reg_alloc);
362 foreach_list_typed(nir_register, reg, node, &impl->registers) {
363 unsigned array_elems =
364 reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
365 unsigned size = array_elems * reg->num_components;
366 nir_locals[reg->index] = bld.vgrf(BRW_REGISTER_TYPE_F, size);
367 }
368
369 nir_emit_cf_list(&impl->body);
370 }
371
372 void
373 fs_visitor::nir_emit_cf_list(exec_list *list)
374 {
375 exec_list_validate(list);
376 foreach_list_typed(nir_cf_node, node, node, list) {
377 switch (node->type) {
378 case nir_cf_node_if:
379 nir_emit_if(nir_cf_node_as_if(node));
380 break;
381
382 case nir_cf_node_loop:
383 nir_emit_loop(nir_cf_node_as_loop(node));
384 break;
385
386 case nir_cf_node_block:
387 nir_emit_block(nir_cf_node_as_block(node));
388 break;
389
390 default:
391 unreachable("Invalid CFG node block");
392 }
393 }
394 }
395
396 void
397 fs_visitor::nir_emit_if(nir_if *if_stmt)
398 {
399 /* first, put the condition into f0 */
400 fs_inst *inst = bld.MOV(bld.null_reg_d(),
401 retype(get_nir_src(if_stmt->condition),
402 BRW_REGISTER_TYPE_D));
403 inst->conditional_mod = BRW_CONDITIONAL_NZ;
404
405 bld.IF(BRW_PREDICATE_NORMAL);
406
407 nir_emit_cf_list(&if_stmt->then_list);
408
409 /* note: if the else is empty, dead CF elimination will remove it */
410 bld.emit(BRW_OPCODE_ELSE);
411
412 nir_emit_cf_list(&if_stmt->else_list);
413
414 bld.emit(BRW_OPCODE_ENDIF);
415
416 if (!try_replace_with_sel() && devinfo->gen < 6) {
417 no16("Can't support (non-uniform) control flow on SIMD16\n");
418 }
419 }
420
421 void
422 fs_visitor::nir_emit_loop(nir_loop *loop)
423 {
424 if (devinfo->gen < 6) {
425 no16("Can't support (non-uniform) control flow on SIMD16\n");
426 }
427
428 bld.emit(BRW_OPCODE_DO);
429
430 nir_emit_cf_list(&loop->body);
431
432 bld.emit(BRW_OPCODE_WHILE);
433 }
434
435 void
436 fs_visitor::nir_emit_block(nir_block *block)
437 {
438 nir_foreach_instr(block, instr) {
439 nir_emit_instr(instr);
440 }
441 }
442
443 void
444 fs_visitor::nir_emit_instr(nir_instr *instr)
445 {
446 const fs_builder abld = bld.annotate(NULL, instr);
447 this->base_ir = instr;
448
449 switch (instr->type) {
450 case nir_instr_type_alu:
451 nir_emit_alu(abld, nir_instr_as_alu(instr));
452 break;
453
454 case nir_instr_type_intrinsic:
455 nir_emit_intrinsic(nir_instr_as_intrinsic(instr));
456 break;
457
458 case nir_instr_type_tex:
459 nir_emit_texture(nir_instr_as_tex(instr));
460 break;
461
462 case nir_instr_type_load_const:
463 /* We can hit these, but we do nothing now and use them as
464 * immediates later.
465 */
466 break;
467
468 case nir_instr_type_jump:
469 nir_emit_jump(abld, nir_instr_as_jump(instr));
470 break;
471
472 default:
473 unreachable("unknown instruction type");
474 }
475
476 this->base_ir = NULL;
477 }
478
479 static brw_reg_type
480 brw_type_for_nir_type(nir_alu_type type)
481 {
482 switch (type) {
483 case nir_type_unsigned:
484 return BRW_REGISTER_TYPE_UD;
485 case nir_type_bool:
486 case nir_type_int:
487 return BRW_REGISTER_TYPE_D;
488 case nir_type_float:
489 return BRW_REGISTER_TYPE_F;
490 default:
491 unreachable("unknown type");
492 }
493
494 return BRW_REGISTER_TYPE_F;
495 }
496
497 bool
498 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr *instr,
499 const fs_reg &result)
500 {
501 if (instr->src[0].src.is_ssa ||
502 !instr->src[0].src.reg.reg ||
503 !instr->src[0].src.reg.reg->parent_instr)
504 return false;
505
506 if (instr->src[0].src.reg.reg->parent_instr->type !=
507 nir_instr_type_intrinsic)
508 return false;
509
510 nir_intrinsic_instr *src0 =
511 nir_instr_as_intrinsic(instr->src[0].src.reg.reg->parent_instr);
512
513 if (src0->intrinsic != nir_intrinsic_load_front_face)
514 return false;
515
516 nir_const_value *value1 = nir_src_as_const_value(instr->src[1].src);
517 if (!value1 || fabsf(value1->f[0]) != 1.0f)
518 return false;
519
520 nir_const_value *value2 = nir_src_as_const_value(instr->src[2].src);
521 if (!value2 || fabsf(value2->f[0]) != 1.0f)
522 return false;
523
524 fs_reg tmp = vgrf(glsl_type::int_type);
525
526 if (devinfo->gen >= 6) {
527 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
528 fs_reg g0 = fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W));
529
530 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
531 *
532 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
533 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
534 *
535 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
536 *
537 * This negation looks like it's safe in practice, because bits 0:4 will
538 * surely be TRIANGLES
539 */
540
541 if (value1->f[0] == -1.0f) {
542 g0.negate = true;
543 }
544
545 tmp.type = BRW_REGISTER_TYPE_W;
546 tmp.subreg_offset = 2;
547 tmp.stride = 2;
548
549 fs_inst *or_inst = bld.OR(tmp, g0, fs_reg(0x3f80));
550 or_inst->src[1].type = BRW_REGISTER_TYPE_UW;
551
552 tmp.type = BRW_REGISTER_TYPE_D;
553 tmp.subreg_offset = 0;
554 tmp.stride = 1;
555 } else {
556 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
557 fs_reg g1_6 = fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D));
558
559 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
560 *
561 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
562 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
563 *
564 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
565 *
566 * This negation looks like it's safe in practice, because bits 0:4 will
567 * surely be TRIANGLES
568 */
569
570 if (value1->f[0] == -1.0f) {
571 g1_6.negate = true;
572 }
573
574 bld.OR(tmp, g1_6, fs_reg(0x3f800000));
575 }
576 bld.AND(retype(result, BRW_REGISTER_TYPE_D), tmp, fs_reg(0xbf800000));
577
578 return true;
579 }
580
581 void
582 fs_visitor::nir_emit_alu(const fs_builder &bld, nir_alu_instr *instr)
583 {
584 struct brw_wm_prog_key *fs_key = (struct brw_wm_prog_key *) this->key;
585 fs_inst *inst;
586
587 fs_reg result = get_nir_dest(instr->dest.dest);
588 result.type = brw_type_for_nir_type(nir_op_infos[instr->op].output_type);
589
590 fs_reg op[4];
591 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
592 op[i] = get_nir_src(instr->src[i].src);
593 op[i].type = brw_type_for_nir_type(nir_op_infos[instr->op].input_types[i]);
594 op[i].abs = instr->src[i].abs;
595 op[i].negate = instr->src[i].negate;
596 }
597
598 /* We get a bunch of mov's out of the from_ssa pass and they may still
599 * be vectorized. We'll handle them as a special-case. We'll also
600 * handle vecN here because it's basically the same thing.
601 */
602 switch (instr->op) {
603 case nir_op_imov:
604 case nir_op_fmov:
605 case nir_op_vec2:
606 case nir_op_vec3:
607 case nir_op_vec4: {
608 fs_reg temp = result;
609 bool need_extra_copy = false;
610 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
611 if (!instr->src[i].src.is_ssa &&
612 instr->dest.dest.reg.reg == instr->src[i].src.reg.reg) {
613 need_extra_copy = true;
614 temp = bld.vgrf(result.type, 4);
615 break;
616 }
617 }
618
619 for (unsigned i = 0; i < 4; i++) {
620 if (!(instr->dest.write_mask & (1 << i)))
621 continue;
622
623 if (instr->op == nir_op_imov || instr->op == nir_op_fmov) {
624 inst = bld.MOV(offset(temp, i),
625 offset(op[0], instr->src[0].swizzle[i]));
626 } else {
627 inst = bld.MOV(offset(temp, i),
628 offset(op[i], instr->src[i].swizzle[0]));
629 }
630 inst->saturate = instr->dest.saturate;
631 }
632
633 /* In this case the source and destination registers were the same,
634 * so we need to insert an extra set of moves in order to deal with
635 * any swizzling.
636 */
637 if (need_extra_copy) {
638 for (unsigned i = 0; i < 4; i++) {
639 if (!(instr->dest.write_mask & (1 << i)))
640 continue;
641
642 bld.MOV(offset(result, i), offset(temp, i));
643 }
644 }
645 return;
646 }
647 default:
648 break;
649 }
650
651 /* At this point, we have dealt with any instruction that operates on
652 * more than a single channel. Therefore, we can just adjust the source
653 * and destination registers for that channel and emit the instruction.
654 */
655 unsigned channel = 0;
656 if (nir_op_infos[instr->op].output_size == 0) {
657 /* Since NIR is doing the scalarizing for us, we should only ever see
658 * vectorized operations with a single channel.
659 */
660 assert(_mesa_bitcount(instr->dest.write_mask) == 1);
661 channel = ffs(instr->dest.write_mask) - 1;
662
663 result = offset(result, channel);
664 }
665
666 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
667 assert(nir_op_infos[instr->op].input_sizes[i] < 2);
668 op[i] = offset(op[i], instr->src[i].swizzle[channel]);
669 }
670
671 switch (instr->op) {
672 case nir_op_i2f:
673 case nir_op_u2f:
674 inst = bld.MOV(result, op[0]);
675 inst->saturate = instr->dest.saturate;
676 break;
677
678 case nir_op_f2i:
679 case nir_op_f2u:
680 bld.MOV(result, op[0]);
681 break;
682
683 case nir_op_fsign: {
684 /* AND(val, 0x80000000) gives the sign bit.
685 *
686 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
687 * zero.
688 */
689 bld.CMP(bld.null_reg_f(), op[0], fs_reg(0.0f), BRW_CONDITIONAL_NZ);
690
691 fs_reg result_int = retype(result, BRW_REGISTER_TYPE_UD);
692 op[0].type = BRW_REGISTER_TYPE_UD;
693 result.type = BRW_REGISTER_TYPE_UD;
694 bld.AND(result_int, op[0], fs_reg(0x80000000u));
695
696 inst = bld.OR(result_int, result_int, fs_reg(0x3f800000u));
697 inst->predicate = BRW_PREDICATE_NORMAL;
698 if (instr->dest.saturate) {
699 inst = bld.MOV(result, result);
700 inst->saturate = true;
701 }
702 break;
703 }
704
705 case nir_op_isign:
706 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
707 * -> non-negative val generates 0x00000000.
708 * Predicated OR sets 1 if val is positive.
709 */
710 bld.CMP(bld.null_reg_d(), op[0], fs_reg(0), BRW_CONDITIONAL_G);
711 bld.ASR(result, op[0], fs_reg(31));
712 inst = bld.OR(result, result, fs_reg(1));
713 inst->predicate = BRW_PREDICATE_NORMAL;
714 break;
715
716 case nir_op_frcp:
717 inst = bld.emit(SHADER_OPCODE_RCP, result, op[0]);
718 inst->saturate = instr->dest.saturate;
719 break;
720
721 case nir_op_fexp2:
722 inst = bld.emit(SHADER_OPCODE_EXP2, result, op[0]);
723 inst->saturate = instr->dest.saturate;
724 break;
725
726 case nir_op_flog2:
727 inst = bld.emit(SHADER_OPCODE_LOG2, result, op[0]);
728 inst->saturate = instr->dest.saturate;
729 break;
730
731 case nir_op_fsin:
732 inst = bld.emit(SHADER_OPCODE_SIN, result, op[0]);
733 inst->saturate = instr->dest.saturate;
734 break;
735
736 case nir_op_fcos:
737 inst = bld.emit(SHADER_OPCODE_COS, result, op[0]);
738 inst->saturate = instr->dest.saturate;
739 break;
740
741 case nir_op_fddx:
742 if (fs_key->high_quality_derivatives) {
743 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
744 } else {
745 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
746 }
747 inst->saturate = instr->dest.saturate;
748 break;
749 case nir_op_fddx_fine:
750 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
751 inst->saturate = instr->dest.saturate;
752 break;
753 case nir_op_fddx_coarse:
754 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
755 inst->saturate = instr->dest.saturate;
756 break;
757 case nir_op_fddy:
758 if (fs_key->high_quality_derivatives) {
759 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0],
760 fs_reg(fs_key->render_to_fbo));
761 } else {
762 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0],
763 fs_reg(fs_key->render_to_fbo));
764 }
765 inst->saturate = instr->dest.saturate;
766 break;
767 case nir_op_fddy_fine:
768 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0],
769 fs_reg(fs_key->render_to_fbo));
770 inst->saturate = instr->dest.saturate;
771 break;
772 case nir_op_fddy_coarse:
773 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0],
774 fs_reg(fs_key->render_to_fbo));
775 inst->saturate = instr->dest.saturate;
776 break;
777
778 case nir_op_fadd:
779 case nir_op_iadd:
780 inst = bld.ADD(result, op[0], op[1]);
781 inst->saturate = instr->dest.saturate;
782 break;
783
784 case nir_op_fmul:
785 inst = bld.MUL(result, op[0], op[1]);
786 inst->saturate = instr->dest.saturate;
787 break;
788
789 case nir_op_imul:
790 bld.MUL(result, op[0], op[1]);
791 break;
792
793 case nir_op_imul_high:
794 case nir_op_umul_high: {
795 if (devinfo->gen >= 7)
796 no16("SIMD16 explicit accumulator operands unsupported\n");
797
798 struct brw_reg acc = retype(brw_acc_reg(dispatch_width), result.type);
799
800 fs_inst *mul = bld.MUL(acc, op[0], op[1]);
801 bld.MACH(result, op[0], op[1]);
802
803 /* Until Gen8, integer multiplies read 32-bits from one source, and
804 * 16-bits from the other, and relying on the MACH instruction to
805 * generate the high bits of the result.
806 *
807 * On Gen8, the multiply instruction does a full 32x32-bit multiply,
808 * but in order to do a 64x64-bit multiply we have to simulate the
809 * previous behavior and then use a MACH instruction.
810 *
811 * FINISHME: Don't use source modifiers on src1.
812 */
813 if (devinfo->gen >= 8) {
814 assert(mul->src[1].type == BRW_REGISTER_TYPE_D ||
815 mul->src[1].type == BRW_REGISTER_TYPE_UD);
816 if (mul->src[1].type == BRW_REGISTER_TYPE_D) {
817 mul->src[1].type = BRW_REGISTER_TYPE_W;
818 mul->src[1].stride = 2;
819 } else {
820 mul->src[1].type = BRW_REGISTER_TYPE_UW;
821 mul->src[1].stride = 2;
822 }
823 }
824 break;
825 }
826
827 case nir_op_idiv:
828 case nir_op_udiv:
829 bld.emit(SHADER_OPCODE_INT_QUOTIENT, result, op[0], op[1]);
830 break;
831
832 case nir_op_uadd_carry: {
833 if (devinfo->gen >= 7)
834 no16("SIMD16 explicit accumulator operands unsupported\n");
835
836 struct brw_reg acc = retype(brw_acc_reg(dispatch_width),
837 BRW_REGISTER_TYPE_UD);
838
839 bld.ADDC(bld.null_reg_ud(), op[0], op[1]);
840 bld.MOV(result, fs_reg(acc));
841 break;
842 }
843
844 case nir_op_usub_borrow: {
845 if (devinfo->gen >= 7)
846 no16("SIMD16 explicit accumulator operands unsupported\n");
847
848 struct brw_reg acc = retype(brw_acc_reg(dispatch_width),
849 BRW_REGISTER_TYPE_UD);
850
851 bld.SUBB(bld.null_reg_ud(), op[0], op[1]);
852 bld.MOV(result, fs_reg(acc));
853 break;
854 }
855
856 case nir_op_umod:
857 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
858 break;
859
860 case nir_op_flt:
861 case nir_op_ilt:
862 case nir_op_ult:
863 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_L);
864 break;
865
866 case nir_op_fge:
867 case nir_op_ige:
868 case nir_op_uge:
869 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_GE);
870 break;
871
872 case nir_op_feq:
873 case nir_op_ieq:
874 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_Z);
875 break;
876
877 case nir_op_fne:
878 case nir_op_ine:
879 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_NZ);
880 break;
881
882 case nir_op_inot:
883 if (devinfo->gen >= 8) {
884 resolve_source_modifiers(&op[0]);
885 }
886 bld.NOT(result, op[0]);
887 break;
888 case nir_op_ixor:
889 if (devinfo->gen >= 8) {
890 resolve_source_modifiers(&op[0]);
891 resolve_source_modifiers(&op[1]);
892 }
893 bld.XOR(result, op[0], op[1]);
894 break;
895 case nir_op_ior:
896 if (devinfo->gen >= 8) {
897 resolve_source_modifiers(&op[0]);
898 resolve_source_modifiers(&op[1]);
899 }
900 bld.OR(result, op[0], op[1]);
901 break;
902 case nir_op_iand:
903 if (devinfo->gen >= 8) {
904 resolve_source_modifiers(&op[0]);
905 resolve_source_modifiers(&op[1]);
906 }
907 bld.AND(result, op[0], op[1]);
908 break;
909
910 case nir_op_fdot2:
911 case nir_op_fdot3:
912 case nir_op_fdot4:
913 case nir_op_bany2:
914 case nir_op_bany3:
915 case nir_op_bany4:
916 case nir_op_ball2:
917 case nir_op_ball3:
918 case nir_op_ball4:
919 case nir_op_ball_fequal2:
920 case nir_op_ball_iequal2:
921 case nir_op_ball_fequal3:
922 case nir_op_ball_iequal3:
923 case nir_op_ball_fequal4:
924 case nir_op_ball_iequal4:
925 case nir_op_bany_fnequal2:
926 case nir_op_bany_inequal2:
927 case nir_op_bany_fnequal3:
928 case nir_op_bany_inequal3:
929 case nir_op_bany_fnequal4:
930 case nir_op_bany_inequal4:
931 unreachable("Lowered by nir_lower_alu_reductions");
932
933 case nir_op_fnoise1_1:
934 case nir_op_fnoise1_2:
935 case nir_op_fnoise1_3:
936 case nir_op_fnoise1_4:
937 case nir_op_fnoise2_1:
938 case nir_op_fnoise2_2:
939 case nir_op_fnoise2_3:
940 case nir_op_fnoise2_4:
941 case nir_op_fnoise3_1:
942 case nir_op_fnoise3_2:
943 case nir_op_fnoise3_3:
944 case nir_op_fnoise3_4:
945 case nir_op_fnoise4_1:
946 case nir_op_fnoise4_2:
947 case nir_op_fnoise4_3:
948 case nir_op_fnoise4_4:
949 unreachable("not reached: should be handled by lower_noise");
950
951 case nir_op_ldexp:
952 unreachable("not reached: should be handled by ldexp_to_arith()");
953
954 case nir_op_fsqrt:
955 inst = bld.emit(SHADER_OPCODE_SQRT, result, op[0]);
956 inst->saturate = instr->dest.saturate;
957 break;
958
959 case nir_op_frsq:
960 inst = bld.emit(SHADER_OPCODE_RSQ, result, op[0]);
961 inst->saturate = instr->dest.saturate;
962 break;
963
964 case nir_op_b2i:
965 bld.AND(result, op[0], fs_reg(1));
966 break;
967 case nir_op_b2f:
968 bld.AND(retype(result, BRW_REGISTER_TYPE_UD), op[0], fs_reg(0x3f800000u));
969 break;
970
971 case nir_op_f2b:
972 bld.CMP(result, op[0], fs_reg(0.0f), BRW_CONDITIONAL_NZ);
973 break;
974 case nir_op_i2b:
975 bld.CMP(result, op[0], fs_reg(0), BRW_CONDITIONAL_NZ);
976 break;
977
978 case nir_op_ftrunc:
979 inst = bld.RNDZ(result, op[0]);
980 inst->saturate = instr->dest.saturate;
981 break;
982
983 case nir_op_fceil: {
984 op[0].negate = !op[0].negate;
985 fs_reg temp = vgrf(glsl_type::float_type);
986 bld.RNDD(temp, op[0]);
987 temp.negate = true;
988 inst = bld.MOV(result, temp);
989 inst->saturate = instr->dest.saturate;
990 break;
991 }
992 case nir_op_ffloor:
993 inst = bld.RNDD(result, op[0]);
994 inst->saturate = instr->dest.saturate;
995 break;
996 case nir_op_ffract:
997 inst = bld.FRC(result, op[0]);
998 inst->saturate = instr->dest.saturate;
999 break;
1000 case nir_op_fround_even:
1001 inst = bld.RNDE(result, op[0]);
1002 inst->saturate = instr->dest.saturate;
1003 break;
1004
1005 case nir_op_fmin:
1006 case nir_op_imin:
1007 case nir_op_umin:
1008 if (devinfo->gen >= 6) {
1009 inst = bld.emit(BRW_OPCODE_SEL, result, op[0], op[1]);
1010 inst->conditional_mod = BRW_CONDITIONAL_L;
1011 } else {
1012 bld.CMP(bld.null_reg_d(), op[0], op[1], BRW_CONDITIONAL_L);
1013 inst = bld.SEL(result, op[0], op[1]);
1014 inst->predicate = BRW_PREDICATE_NORMAL;
1015 }
1016 inst->saturate = instr->dest.saturate;
1017 break;
1018
1019 case nir_op_fmax:
1020 case nir_op_imax:
1021 case nir_op_umax:
1022 if (devinfo->gen >= 6) {
1023 inst = bld.emit(BRW_OPCODE_SEL, result, op[0], op[1]);
1024 inst->conditional_mod = BRW_CONDITIONAL_GE;
1025 } else {
1026 bld.CMP(bld.null_reg_d(), op[0], op[1], BRW_CONDITIONAL_GE);
1027 inst = bld.SEL(result, op[0], op[1]);
1028 inst->predicate = BRW_PREDICATE_NORMAL;
1029 }
1030 inst->saturate = instr->dest.saturate;
1031 break;
1032
1033 case nir_op_pack_snorm_2x16:
1034 case nir_op_pack_snorm_4x8:
1035 case nir_op_pack_unorm_2x16:
1036 case nir_op_pack_unorm_4x8:
1037 case nir_op_unpack_snorm_2x16:
1038 case nir_op_unpack_snorm_4x8:
1039 case nir_op_unpack_unorm_2x16:
1040 case nir_op_unpack_unorm_4x8:
1041 case nir_op_unpack_half_2x16:
1042 case nir_op_pack_half_2x16:
1043 unreachable("not reached: should be handled by lower_packing_builtins");
1044
1045 case nir_op_unpack_half_2x16_split_x:
1046 inst = bld.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X, result, op[0]);
1047 inst->saturate = instr->dest.saturate;
1048 break;
1049 case nir_op_unpack_half_2x16_split_y:
1050 inst = bld.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y, result, op[0]);
1051 inst->saturate = instr->dest.saturate;
1052 break;
1053
1054 case nir_op_fpow:
1055 inst = bld.emit(SHADER_OPCODE_POW, result, op[0], op[1]);
1056 inst->saturate = instr->dest.saturate;
1057 break;
1058
1059 case nir_op_bitfield_reverse:
1060 bld.BFREV(result, op[0]);
1061 break;
1062
1063 case nir_op_bit_count:
1064 bld.CBIT(result, op[0]);
1065 break;
1066
1067 case nir_op_ufind_msb:
1068 case nir_op_ifind_msb: {
1069 bld.FBH(retype(result, BRW_REGISTER_TYPE_UD), op[0]);
1070
1071 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1072 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1073 * subtract the result from 31 to convert the MSB count into an LSB count.
1074 */
1075
1076 bld.CMP(bld.null_reg_d(), result, fs_reg(-1), BRW_CONDITIONAL_NZ);
1077 fs_reg neg_result(result);
1078 neg_result.negate = true;
1079 inst = bld.ADD(result, neg_result, fs_reg(31));
1080 inst->predicate = BRW_PREDICATE_NORMAL;
1081 break;
1082 }
1083
1084 case nir_op_find_lsb:
1085 bld.FBL(result, op[0]);
1086 break;
1087
1088 case nir_op_ubitfield_extract:
1089 case nir_op_ibitfield_extract:
1090 bld.BFE(result, op[2], op[1], op[0]);
1091 break;
1092 case nir_op_bfm:
1093 bld.BFI1(result, op[0], op[1]);
1094 break;
1095 case nir_op_bfi:
1096 bld.BFI2(result, op[0], op[1], op[2]);
1097 break;
1098
1099 case nir_op_bitfield_insert:
1100 unreachable("not reached: should be handled by "
1101 "lower_instructions::bitfield_insert_to_bfm_bfi");
1102
1103 case nir_op_ishl:
1104 bld.SHL(result, op[0], op[1]);
1105 break;
1106 case nir_op_ishr:
1107 bld.ASR(result, op[0], op[1]);
1108 break;
1109 case nir_op_ushr:
1110 bld.SHR(result, op[0], op[1]);
1111 break;
1112
1113 case nir_op_pack_half_2x16_split:
1114 bld.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT, result, op[0], op[1]);
1115 break;
1116
1117 case nir_op_ffma:
1118 inst = bld.MAD(result, op[2], op[1], op[0]);
1119 inst->saturate = instr->dest.saturate;
1120 break;
1121
1122 case nir_op_flrp:
1123 inst = bld.LRP(result, op[0], op[1], op[2]);
1124 inst->saturate = instr->dest.saturate;
1125 break;
1126
1127 case nir_op_bcsel:
1128 if (optimize_frontfacing_ternary(instr, result))
1129 return;
1130
1131 bld.CMP(bld.null_reg_d(), op[0], fs_reg(0), BRW_CONDITIONAL_NZ);
1132 inst = bld.SEL(result, op[1], op[2]);
1133 inst->predicate = BRW_PREDICATE_NORMAL;
1134 break;
1135
1136 default:
1137 unreachable("unhandled instruction");
1138 }
1139
1140 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1141 * to sign extend the low bit to 0/~0
1142 */
1143 if (devinfo->gen <= 5 &&
1144 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1145 fs_reg masked = vgrf(glsl_type::int_type);
1146 bld.AND(masked, result, fs_reg(1));
1147 masked.negate = true;
1148 bld.MOV(retype(result, BRW_REGISTER_TYPE_D), masked);
1149 }
1150 }
1151
1152 static fs_reg
1153 fs_reg_for_nir_reg(fs_visitor *v, nir_register *nir_reg,
1154 unsigned base_offset, nir_src *indirect)
1155 {
1156 fs_reg reg;
1157 if (nir_reg->is_global)
1158 reg = v->nir_globals[nir_reg->index];
1159 else
1160 reg = v->nir_locals[nir_reg->index];
1161
1162 reg = offset(reg, base_offset * nir_reg->num_components);
1163 if (indirect) {
1164 int multiplier = nir_reg->num_components * (v->dispatch_width / 8);
1165
1166 reg.reladdr = new(v->mem_ctx) fs_reg(v->vgrf(glsl_type::int_type));
1167 v->bld.MUL(*reg.reladdr, v->get_nir_src(*indirect),
1168 fs_reg(multiplier));
1169 }
1170
1171 return reg;
1172 }
1173
1174 fs_reg
1175 fs_visitor::get_nir_src(nir_src src)
1176 {
1177 if (src.is_ssa) {
1178 assert(src.ssa->parent_instr->type == nir_instr_type_load_const);
1179 nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
1180 fs_reg reg = bld.vgrf(BRW_REGISTER_TYPE_D, src.ssa->num_components);
1181
1182 for (unsigned i = 0; i < src.ssa->num_components; ++i)
1183 bld.MOV(offset(reg, i), fs_reg(load->value.i[i]));
1184
1185 return reg;
1186 } else {
1187 fs_reg reg = fs_reg_for_nir_reg(this, src.reg.reg, src.reg.base_offset,
1188 src.reg.indirect);
1189
1190 /* to avoid floating-point denorm flushing problems, set the type by
1191 * default to D - instructions that need floating point semantics will set
1192 * this to F if they need to
1193 */
1194 return retype(reg, BRW_REGISTER_TYPE_D);
1195 }
1196 }
1197
1198 fs_reg
1199 fs_visitor::get_nir_dest(nir_dest dest)
1200 {
1201 return fs_reg_for_nir_reg(this, dest.reg.reg, dest.reg.base_offset,
1202 dest.reg.indirect);
1203 }
1204
1205 void
1206 fs_visitor::emit_percomp(const fs_builder &bld, const fs_inst &inst,
1207 unsigned wr_mask)
1208 {
1209 for (unsigned i = 0; i < 4; i++) {
1210 if (!((wr_mask >> i) & 1))
1211 continue;
1212
1213 fs_inst *new_inst = new(mem_ctx) fs_inst(inst);
1214 new_inst->dst = offset(new_inst->dst, i);
1215 for (unsigned j = 0; j < new_inst->sources; j++)
1216 if (new_inst->src[j].file == GRF)
1217 new_inst->src[j] = offset(new_inst->src[j], i);
1218
1219 bld.emit(new_inst);
1220 }
1221 }
1222
1223 void
1224 fs_visitor::nir_emit_intrinsic(nir_intrinsic_instr *instr)
1225 {
1226 fs_reg dest;
1227 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
1228 dest = get_nir_dest(instr->dest);
1229
1230 bool has_indirect = false;
1231
1232 switch (instr->intrinsic) {
1233 case nir_intrinsic_discard:
1234 case nir_intrinsic_discard_if: {
1235 /* We track our discarded pixels in f0.1. By predicating on it, we can
1236 * update just the flag bits that aren't yet discarded. If there's no
1237 * condition, we emit a CMP of g0 != g0, so all currently executing
1238 * channels will get turned off.
1239 */
1240 fs_inst *cmp;
1241 if (instr->intrinsic == nir_intrinsic_discard_if) {
1242 cmp = emit(CMP(reg_null_f, get_nir_src(instr->src[0]),
1243 fs_reg(0), BRW_CONDITIONAL_Z));
1244 } else {
1245 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
1246 BRW_REGISTER_TYPE_UW));
1247 cmp = emit(CMP(reg_null_f, some_reg, some_reg, BRW_CONDITIONAL_NZ));
1248 }
1249 cmp->predicate = BRW_PREDICATE_NORMAL;
1250 cmp->flag_subreg = 1;
1251
1252 if (devinfo->gen >= 6) {
1253 emit_discard_jump();
1254 }
1255 break;
1256 }
1257
1258 case nir_intrinsic_atomic_counter_inc:
1259 case nir_intrinsic_atomic_counter_dec:
1260 case nir_intrinsic_atomic_counter_read: {
1261 unsigned surf_index = prog_data->binding_table.abo_start +
1262 (unsigned) instr->const_index[0];
1263 fs_reg offset = fs_reg(get_nir_src(instr->src[0]));
1264
1265 switch (instr->intrinsic) {
1266 case nir_intrinsic_atomic_counter_inc:
1267 emit_untyped_atomic(BRW_AOP_INC, surf_index, dest, offset,
1268 fs_reg(), fs_reg());
1269 break;
1270 case nir_intrinsic_atomic_counter_dec:
1271 emit_untyped_atomic(BRW_AOP_PREDEC, surf_index, dest, offset,
1272 fs_reg(), fs_reg());
1273 break;
1274 case nir_intrinsic_atomic_counter_read:
1275 emit_untyped_surface_read(surf_index, dest, offset);
1276 break;
1277 default:
1278 unreachable("Unreachable");
1279 }
1280 break;
1281 }
1282
1283 case nir_intrinsic_load_front_face:
1284 emit(MOV(retype(dest, BRW_REGISTER_TYPE_D),
1285 *emit_frontfacing_interpolation()));
1286 break;
1287
1288 case nir_intrinsic_load_vertex_id:
1289 unreachable("should be lowered by lower_vertex_id()");
1290
1291 case nir_intrinsic_load_vertex_id_zero_base: {
1292 fs_reg vertex_id = nir_system_values[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE];
1293 assert(vertex_id.file != BAD_FILE);
1294 dest.type = vertex_id.type;
1295 emit(MOV(dest, vertex_id));
1296 break;
1297 }
1298
1299 case nir_intrinsic_load_base_vertex: {
1300 fs_reg base_vertex = nir_system_values[SYSTEM_VALUE_BASE_VERTEX];
1301 assert(base_vertex.file != BAD_FILE);
1302 dest.type = base_vertex.type;
1303 emit(MOV(dest, base_vertex));
1304 break;
1305 }
1306
1307 case nir_intrinsic_load_instance_id: {
1308 fs_reg instance_id = nir_system_values[SYSTEM_VALUE_INSTANCE_ID];
1309 assert(instance_id.file != BAD_FILE);
1310 dest.type = instance_id.type;
1311 emit(MOV(dest, instance_id));
1312 break;
1313 }
1314
1315 case nir_intrinsic_load_sample_mask_in: {
1316 fs_reg sample_mask_in = nir_system_values[SYSTEM_VALUE_SAMPLE_MASK_IN];
1317 assert(sample_mask_in.file != BAD_FILE);
1318 dest.type = sample_mask_in.type;
1319 emit(MOV(dest, sample_mask_in));
1320 break;
1321 }
1322
1323 case nir_intrinsic_load_sample_pos: {
1324 fs_reg sample_pos = nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
1325 assert(sample_pos.file != BAD_FILE);
1326 dest.type = sample_pos.type;
1327 emit(MOV(dest, sample_pos));
1328 emit(MOV(offset(dest, 1), offset(sample_pos, 1)));
1329 break;
1330 }
1331
1332 case nir_intrinsic_load_sample_id: {
1333 fs_reg sample_id = nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
1334 assert(sample_id.file != BAD_FILE);
1335 dest.type = sample_id.type;
1336 emit(MOV(dest, sample_id));
1337 break;
1338 }
1339
1340 case nir_intrinsic_load_uniform_indirect:
1341 has_indirect = true;
1342 /* fallthrough */
1343 case nir_intrinsic_load_uniform: {
1344 unsigned index = instr->const_index[0];
1345
1346 fs_reg uniform_reg;
1347 if (index < num_direct_uniforms) {
1348 uniform_reg = fs_reg(UNIFORM, 0);
1349 } else {
1350 uniform_reg = fs_reg(UNIFORM, num_direct_uniforms);
1351 index -= num_direct_uniforms;
1352 }
1353
1354 for (unsigned j = 0; j < instr->num_components; j++) {
1355 fs_reg src = offset(retype(uniform_reg, dest.type), index);
1356 if (has_indirect)
1357 src.reladdr = new(mem_ctx) fs_reg(get_nir_src(instr->src[0]));
1358 index++;
1359
1360 emit(MOV(dest, src));
1361 dest = offset(dest, 1);
1362 }
1363 break;
1364 }
1365
1366 case nir_intrinsic_load_ubo_indirect:
1367 has_indirect = true;
1368 /* fallthrough */
1369 case nir_intrinsic_load_ubo: {
1370 nir_const_value *const_index = nir_src_as_const_value(instr->src[0]);
1371 fs_reg surf_index;
1372
1373 if (const_index) {
1374 surf_index = fs_reg(stage_prog_data->binding_table.ubo_start +
1375 const_index->u[0]);
1376 } else {
1377 /* The block index is not a constant. Evaluate the index expression
1378 * per-channel and add the base UBO index; we have to select a value
1379 * from any live channel.
1380 */
1381 surf_index = vgrf(glsl_type::uint_type);
1382 emit(ADD(surf_index, get_nir_src(instr->src[0]),
1383 fs_reg(stage_prog_data->binding_table.ubo_start)));
1384 emit_uniformize(surf_index, surf_index);
1385
1386 /* Assume this may touch any UBO. It would be nice to provide
1387 * a tighter bound, but the array information is already lowered away.
1388 */
1389 brw_mark_surface_used(prog_data,
1390 stage_prog_data->binding_table.ubo_start +
1391 shader_prog->NumUniformBlocks - 1);
1392 }
1393
1394 if (has_indirect) {
1395 /* Turn the byte offset into a dword offset. */
1396 fs_reg base_offset = vgrf(glsl_type::int_type);
1397 emit(SHR(base_offset, retype(get_nir_src(instr->src[1]),
1398 BRW_REGISTER_TYPE_D),
1399 fs_reg(2)));
1400
1401 unsigned vec4_offset = instr->const_index[0] / 4;
1402 for (int i = 0; i < instr->num_components; i++)
1403 VARYING_PULL_CONSTANT_LOAD(bld, offset(dest, i), surf_index,
1404 base_offset, vec4_offset + i);
1405 } else {
1406 fs_reg packed_consts = vgrf(glsl_type::float_type);
1407 packed_consts.type = dest.type;
1408
1409 fs_reg const_offset_reg((unsigned) instr->const_index[0] & ~15);
1410 emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD, packed_consts,
1411 surf_index, const_offset_reg);
1412
1413 for (unsigned i = 0; i < instr->num_components; i++) {
1414 packed_consts.set_smear(instr->const_index[0] % 16 / 4 + i);
1415
1416 /* The std140 packing rules don't allow vectors to cross 16-byte
1417 * boundaries, and a reg is 32 bytes.
1418 */
1419 assert(packed_consts.subreg_offset < 32);
1420
1421 emit(MOV(dest, packed_consts));
1422 dest = offset(dest, 1);
1423 }
1424 }
1425 break;
1426 }
1427
1428 case nir_intrinsic_load_input_indirect:
1429 has_indirect = true;
1430 /* fallthrough */
1431 case nir_intrinsic_load_input: {
1432 unsigned index = 0;
1433 for (unsigned j = 0; j < instr->num_components; j++) {
1434 fs_reg src = offset(retype(nir_inputs, dest.type),
1435 instr->const_index[0] + index);
1436 if (has_indirect)
1437 src.reladdr = new(mem_ctx) fs_reg(get_nir_src(instr->src[0]));
1438 index++;
1439
1440 emit(MOV(dest, src));
1441 dest = offset(dest, 1);
1442 }
1443 break;
1444 }
1445
1446 /* Handle ARB_gpu_shader5 interpolation intrinsics
1447 *
1448 * It's worth a quick word of explanation as to why we handle the full
1449 * variable-based interpolation intrinsic rather than a lowered version
1450 * with like we do for other inputs. We have to do that because the way
1451 * we set up inputs doesn't allow us to use the already setup inputs for
1452 * interpolation. At the beginning of the shader, we go through all of
1453 * the input variables and do the initial interpolation and put it in
1454 * the nir_inputs array based on its location as determined in
1455 * nir_lower_io. If the input isn't used, dead code cleans up and
1456 * everything works fine. However, when we get to the ARB_gpu_shader5
1457 * interpolation intrinsics, we need to reinterpolate the input
1458 * differently. If we used an intrinsic that just had an index it would
1459 * only give us the offset into the nir_inputs array. However, this is
1460 * useless because that value is post-interpolation and we need
1461 * pre-interpolation. In order to get the actual location of the bits
1462 * we get from the vertex fetching hardware, we need the variable.
1463 */
1464 case nir_intrinsic_interp_var_at_centroid:
1465 case nir_intrinsic_interp_var_at_sample:
1466 case nir_intrinsic_interp_var_at_offset: {
1467 /* in SIMD16 mode, the pixel interpolator returns coords interleaved
1468 * 8 channels at a time, same as the barycentric coords presented in
1469 * the FS payload. this requires a bit of extra work to support.
1470 */
1471 no16("interpolate_at_* not yet supported in SIMD16 mode.");
1472
1473 fs_reg dst_xy = vgrf(2);
1474
1475 /* For most messages, we need one reg of ignored data; the hardware
1476 * requires mlen==1 even when there is no payload. in the per-slot
1477 * offset case, we'll replace this with the proper source data.
1478 */
1479 fs_reg src = vgrf(glsl_type::float_type);
1480 int mlen = 1; /* one reg unless overriden */
1481 fs_inst *inst;
1482
1483 switch (instr->intrinsic) {
1484 case nir_intrinsic_interp_var_at_centroid:
1485 inst = emit(FS_OPCODE_INTERPOLATE_AT_CENTROID, dst_xy, src, fs_reg(0u));
1486 break;
1487
1488 case nir_intrinsic_interp_var_at_sample: {
1489 /* XXX: We should probably handle non-constant sample id's */
1490 nir_const_value *const_sample = nir_src_as_const_value(instr->src[0]);
1491 assert(const_sample);
1492 unsigned msg_data = const_sample ? const_sample->i[0] << 4 : 0;
1493 inst = emit(FS_OPCODE_INTERPOLATE_AT_SAMPLE, dst_xy, src,
1494 fs_reg(msg_data));
1495 break;
1496 }
1497
1498 case nir_intrinsic_interp_var_at_offset: {
1499 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
1500
1501 if (const_offset) {
1502 unsigned off_x = MIN2((int)(const_offset->f[0] * 16), 7) & 0xf;
1503 unsigned off_y = MIN2((int)(const_offset->f[1] * 16), 7) & 0xf;
1504
1505 inst = emit(FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET, dst_xy, src,
1506 fs_reg(off_x | (off_y << 4)));
1507 } else {
1508 src = vgrf(glsl_type::ivec2_type);
1509 fs_reg offset_src = retype(get_nir_src(instr->src[0]),
1510 BRW_REGISTER_TYPE_F);
1511 for (int i = 0; i < 2; i++) {
1512 fs_reg temp = vgrf(glsl_type::float_type);
1513 emit(MUL(temp, offset(offset_src, i), fs_reg(16.0f)));
1514 fs_reg itemp = vgrf(glsl_type::int_type);
1515 emit(MOV(itemp, temp)); /* float to int */
1516
1517 /* Clamp the upper end of the range to +7/16.
1518 * ARB_gpu_shader5 requires that we support a maximum offset
1519 * of +0.5, which isn't representable in a S0.4 value -- if
1520 * we didn't clamp it, we'd end up with -8/16, which is the
1521 * opposite of what the shader author wanted.
1522 *
1523 * This is legal due to ARB_gpu_shader5's quantization
1524 * rules:
1525 *
1526 * "Not all values of <offset> may be supported; x and y
1527 * offsets may be rounded to fixed-point values with the
1528 * number of fraction bits given by the
1529 * implementation-dependent constant
1530 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
1531 */
1532
1533 emit(BRW_OPCODE_SEL, offset(src, i), itemp, fs_reg(7))
1534 ->conditional_mod = BRW_CONDITIONAL_L; /* min(src2, 7) */
1535 }
1536
1537 mlen = 2;
1538 inst = emit(FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET, dst_xy, src,
1539 fs_reg(0u));
1540 }
1541 break;
1542 }
1543
1544 default:
1545 unreachable("Invalid intrinsic");
1546 }
1547
1548 inst->mlen = mlen;
1549 inst->regs_written = 2; /* 2 floats per slot returned */
1550 inst->pi_noperspective = instr->variables[0]->var->data.interpolation ==
1551 INTERP_QUALIFIER_NOPERSPECTIVE;
1552
1553 for (unsigned j = 0; j < instr->num_components; j++) {
1554 fs_reg src = interp_reg(instr->variables[0]->var->data.location, j);
1555 src.type = dest.type;
1556
1557 emit(FS_OPCODE_LINTERP, dest, dst_xy, src);
1558 dest = offset(dest, 1);
1559 }
1560 break;
1561 }
1562
1563 case nir_intrinsic_store_output_indirect:
1564 has_indirect = true;
1565 /* fallthrough */
1566 case nir_intrinsic_store_output: {
1567 fs_reg src = get_nir_src(instr->src[0]);
1568 unsigned index = 0;
1569 for (unsigned j = 0; j < instr->num_components; j++) {
1570 fs_reg new_dest = offset(retype(nir_outputs, src.type),
1571 instr->const_index[0] + index);
1572 if (has_indirect)
1573 src.reladdr = new(mem_ctx) fs_reg(get_nir_src(instr->src[1]));
1574 index++;
1575 emit(MOV(new_dest, src));
1576 src = offset(src, 1);
1577 }
1578 break;
1579 }
1580
1581 default:
1582 unreachable("unknown intrinsic");
1583 }
1584 }
1585
1586 void
1587 fs_visitor::nir_emit_texture(nir_tex_instr *instr)
1588 {
1589 unsigned sampler = instr->sampler_index;
1590 fs_reg sampler_reg(sampler);
1591
1592 /* FINISHME: We're failing to recompile our programs when the sampler is
1593 * updated. This only matters for the texture rectangle scale parameters
1594 * (pre-gen6, or gen6+ with GL_CLAMP).
1595 */
1596 int texunit = prog->SamplerUnits[sampler];
1597
1598 int gather_component = instr->component;
1599
1600 bool is_rect = instr->sampler_dim == GLSL_SAMPLER_DIM_RECT;
1601
1602 bool is_cube_array = instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
1603 instr->is_array;
1604
1605 int lod_components = 0, offset_components = 0;
1606
1607 fs_reg coordinate, shadow_comparitor, lod, lod2, sample_index, mcs, tex_offset;
1608
1609 for (unsigned i = 0; i < instr->num_srcs; i++) {
1610 fs_reg src = get_nir_src(instr->src[i].src);
1611 switch (instr->src[i].src_type) {
1612 case nir_tex_src_bias:
1613 lod = retype(src, BRW_REGISTER_TYPE_F);
1614 break;
1615 case nir_tex_src_comparitor:
1616 shadow_comparitor = retype(src, BRW_REGISTER_TYPE_F);
1617 break;
1618 case nir_tex_src_coord:
1619 switch (instr->op) {
1620 case nir_texop_txf:
1621 case nir_texop_txf_ms:
1622 coordinate = retype(src, BRW_REGISTER_TYPE_D);
1623 break;
1624 default:
1625 coordinate = retype(src, BRW_REGISTER_TYPE_F);
1626 break;
1627 }
1628 break;
1629 case nir_tex_src_ddx:
1630 lod = retype(src, BRW_REGISTER_TYPE_F);
1631 lod_components = nir_tex_instr_src_size(instr, i);
1632 break;
1633 case nir_tex_src_ddy:
1634 lod2 = retype(src, BRW_REGISTER_TYPE_F);
1635 break;
1636 case nir_tex_src_lod:
1637 switch (instr->op) {
1638 case nir_texop_txs:
1639 lod = retype(src, BRW_REGISTER_TYPE_UD);
1640 break;
1641 case nir_texop_txf:
1642 lod = retype(src, BRW_REGISTER_TYPE_D);
1643 break;
1644 default:
1645 lod = retype(src, BRW_REGISTER_TYPE_F);
1646 break;
1647 }
1648 break;
1649 case nir_tex_src_ms_index:
1650 sample_index = retype(src, BRW_REGISTER_TYPE_UD);
1651 break;
1652 case nir_tex_src_offset:
1653 tex_offset = retype(src, BRW_REGISTER_TYPE_D);
1654 if (instr->is_array)
1655 offset_components = instr->coord_components - 1;
1656 else
1657 offset_components = instr->coord_components;
1658 break;
1659 case nir_tex_src_projector:
1660 unreachable("should be lowered");
1661
1662 case nir_tex_src_sampler_offset: {
1663 /* Figure out the highest possible sampler index and mark it as used */
1664 uint32_t max_used = sampler + instr->sampler_array_size - 1;
1665 if (instr->op == nir_texop_tg4 && devinfo->gen < 8) {
1666 max_used += stage_prog_data->binding_table.gather_texture_start;
1667 } else {
1668 max_used += stage_prog_data->binding_table.texture_start;
1669 }
1670 brw_mark_surface_used(prog_data, max_used);
1671
1672 /* Emit code to evaluate the actual indexing expression */
1673 sampler_reg = vgrf(glsl_type::uint_type);
1674 emit(ADD(sampler_reg, src, fs_reg(sampler)));
1675 emit_uniformize(sampler_reg, sampler_reg);
1676 break;
1677 }
1678
1679 default:
1680 unreachable("unknown texture source");
1681 }
1682 }
1683
1684 if (instr->op == nir_texop_txf_ms) {
1685 if (devinfo->gen >= 7 &&
1686 key_tex->compressed_multisample_layout_mask & (1 << sampler)) {
1687 mcs = emit_mcs_fetch(coordinate, instr->coord_components, sampler_reg);
1688 } else {
1689 mcs = fs_reg(0u);
1690 }
1691 }
1692
1693 for (unsigned i = 0; i < 3; i++) {
1694 if (instr->const_offset[i] != 0) {
1695 assert(offset_components == 0);
1696 tex_offset = fs_reg(brw_texture_offset(instr->const_offset, 3));
1697 break;
1698 }
1699 }
1700
1701 enum glsl_base_type dest_base_type;
1702 switch (instr->dest_type) {
1703 case nir_type_float:
1704 dest_base_type = GLSL_TYPE_FLOAT;
1705 break;
1706 case nir_type_int:
1707 dest_base_type = GLSL_TYPE_INT;
1708 break;
1709 case nir_type_unsigned:
1710 dest_base_type = GLSL_TYPE_UINT;
1711 break;
1712 default:
1713 unreachable("bad type");
1714 }
1715
1716 const glsl_type *dest_type =
1717 glsl_type::get_instance(dest_base_type, nir_tex_instr_dest_size(instr),
1718 1);
1719
1720 ir_texture_opcode op;
1721 switch (instr->op) {
1722 case nir_texop_lod: op = ir_lod; break;
1723 case nir_texop_query_levels: op = ir_query_levels; break;
1724 case nir_texop_tex: op = ir_tex; break;
1725 case nir_texop_tg4: op = ir_tg4; break;
1726 case nir_texop_txb: op = ir_txb; break;
1727 case nir_texop_txd: op = ir_txd; break;
1728 case nir_texop_txf: op = ir_txf; break;
1729 case nir_texop_txf_ms: op = ir_txf_ms; break;
1730 case nir_texop_txl: op = ir_txl; break;
1731 case nir_texop_txs: op = ir_txs; break;
1732 default:
1733 unreachable("unknown texture opcode");
1734 }
1735
1736 emit_texture(op, dest_type, coordinate, instr->coord_components,
1737 shadow_comparitor, lod, lod2, lod_components, sample_index,
1738 tex_offset, mcs, gather_component,
1739 is_cube_array, is_rect, sampler, sampler_reg, texunit);
1740
1741 fs_reg dest = get_nir_dest(instr->dest);
1742 dest.type = this->result.type;
1743 unsigned num_components = nir_tex_instr_dest_size(instr);
1744 emit_percomp(bld, fs_inst(BRW_OPCODE_MOV, dest, this->result),
1745 (1 << num_components) - 1);
1746 }
1747
1748 void
1749 fs_visitor::nir_emit_jump(const fs_builder &bld, nir_jump_instr *instr)
1750 {
1751 switch (instr->type) {
1752 case nir_jump_break:
1753 bld.emit(BRW_OPCODE_BREAK);
1754 break;
1755 case nir_jump_continue:
1756 bld.emit(BRW_OPCODE_CONTINUE);
1757 break;
1758 case nir_jump_return:
1759 default:
1760 unreachable("unknown jump");
1761 }
1762 }