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