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