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