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