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