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