i965/fs: Print spills:fills and number of promoted constants.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_visitor.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 /** @file brw_fs_visitor.cpp
25 *
26 * This file supports generating the FS LIR from the GLSL IR. The LIR
27 * makes it easier to do backend-specific optimizations than doing so
28 * in the GLSL IR or in the native code.
29 */
30 #include <sys/types.h>
31
32 #include "main/macros.h"
33 #include "main/shaderobj.h"
34 #include "program/prog_parameter.h"
35 #include "program/prog_print.h"
36 #include "program/prog_optimize.h"
37 #include "util/register_allocate.h"
38 #include "program/hash_table.h"
39 #include "brw_context.h"
40 #include "brw_eu.h"
41 #include "brw_wm.h"
42 #include "brw_vec4.h"
43 #include "brw_fs.h"
44 #include "main/uniforms.h"
45 #include "glsl/glsl_types.h"
46 #include "glsl/ir_optimization.h"
47 #include "program/sampler.h"
48
49
50 fs_reg *
51 fs_visitor::emit_vs_system_value(int location)
52 {
53 fs_reg *reg = new(this->mem_ctx)
54 fs_reg(ATTR, VERT_ATTRIB_MAX, BRW_REGISTER_TYPE_D);
55 brw_vs_prog_data *vs_prog_data = (brw_vs_prog_data *) prog_data;
56
57 switch (location) {
58 case SYSTEM_VALUE_BASE_VERTEX:
59 reg->reg_offset = 0;
60 vs_prog_data->uses_vertexid = true;
61 break;
62 case SYSTEM_VALUE_VERTEX_ID:
63 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
64 reg->reg_offset = 2;
65 vs_prog_data->uses_vertexid = true;
66 break;
67 case SYSTEM_VALUE_INSTANCE_ID:
68 reg->reg_offset = 3;
69 vs_prog_data->uses_instanceid = true;
70 break;
71 default:
72 unreachable("not reached");
73 }
74
75 return reg;
76 }
77
78 void
79 fs_visitor::visit(ir_variable *ir)
80 {
81 fs_reg *reg = NULL;
82
83 if (variable_storage(ir))
84 return;
85
86 if (ir->data.mode == ir_var_shader_in) {
87 assert(ir->data.location != -1);
88 if (stage == MESA_SHADER_VERTEX) {
89 reg = new(this->mem_ctx)
90 fs_reg(ATTR, ir->data.location,
91 brw_type_for_base_type(ir->type->get_scalar_type()));
92 } else if (ir->data.location == VARYING_SLOT_POS) {
93 reg = emit_fragcoord_interpolation(ir->data.pixel_center_integer,
94 ir->data.origin_upper_left);
95 } else if (ir->data.location == VARYING_SLOT_FACE) {
96 reg = emit_frontfacing_interpolation();
97 } else {
98 reg = new(this->mem_ctx) fs_reg(vgrf(ir->type));
99 emit_general_interpolation(*reg, ir->name, ir->type,
100 (glsl_interp_qualifier) ir->data.interpolation,
101 ir->data.location, ir->data.centroid,
102 ir->data.sample);
103 }
104 assert(reg);
105 hash_table_insert(this->variable_ht, reg, ir);
106 return;
107 } else if (ir->data.mode == ir_var_shader_out) {
108 reg = new(this->mem_ctx) fs_reg(vgrf(ir->type));
109
110 if (stage == MESA_SHADER_VERTEX) {
111 int vector_elements =
112 ir->type->is_array() ? ir->type->fields.array->vector_elements
113 : ir->type->vector_elements;
114
115 for (int i = 0; i < (type_size(ir->type) + 3) / 4; i++) {
116 int output = ir->data.location + i;
117 this->outputs[output] = *reg;
118 this->outputs[output].reg_offset = i * 4;
119 this->output_components[output] = vector_elements;
120 }
121
122 } else if (ir->data.index > 0) {
123 assert(ir->data.location == FRAG_RESULT_DATA0);
124 assert(ir->data.index == 1);
125 this->dual_src_output = *reg;
126 this->do_dual_src = true;
127 } else if (ir->data.location == FRAG_RESULT_COLOR) {
128 /* Writing gl_FragColor outputs to all color regions. */
129 assert(stage == MESA_SHADER_FRAGMENT);
130 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
131 for (unsigned int i = 0; i < MAX2(key->nr_color_regions, 1); i++) {
132 this->outputs[i] = *reg;
133 this->output_components[i] = 4;
134 }
135 } else if (ir->data.location == FRAG_RESULT_DEPTH) {
136 this->frag_depth = *reg;
137 } else if (ir->data.location == FRAG_RESULT_SAMPLE_MASK) {
138 this->sample_mask = *reg;
139 } else {
140 /* gl_FragData or a user-defined FS output */
141 assert(ir->data.location >= FRAG_RESULT_DATA0 &&
142 ir->data.location < FRAG_RESULT_DATA0 + BRW_MAX_DRAW_BUFFERS);
143
144 int vector_elements =
145 ir->type->is_array() ? ir->type->fields.array->vector_elements
146 : ir->type->vector_elements;
147
148 /* General color output. */
149 for (unsigned int i = 0; i < MAX2(1, ir->type->length); i++) {
150 int output = ir->data.location - FRAG_RESULT_DATA0 + i;
151 this->outputs[output] = offset(*reg, vector_elements * i);
152 this->output_components[output] = vector_elements;
153 }
154 }
155 } else if (ir->data.mode == ir_var_uniform) {
156 int param_index = uniforms;
157
158 /* Thanks to the lower_ubo_reference pass, we will see only
159 * ir_binop_ubo_load expressions and not ir_dereference_variable for UBO
160 * variables, so no need for them to be in variable_ht.
161 *
162 * Some uniforms, such as samplers and atomic counters, have no actual
163 * storage, so we should ignore them.
164 */
165 if (ir->is_in_uniform_block() || type_size(ir->type) == 0)
166 return;
167
168 if (dispatch_width == 16) {
169 if (!variable_storage(ir)) {
170 fail("Failed to find uniform '%s' in SIMD16\n", ir->name);
171 }
172 return;
173 }
174
175 param_size[param_index] = type_size(ir->type);
176 if (!strncmp(ir->name, "gl_", 3)) {
177 setup_builtin_uniform_values(ir);
178 } else {
179 setup_uniform_values(ir);
180 }
181
182 reg = new(this->mem_ctx) fs_reg(UNIFORM, param_index);
183 reg->type = brw_type_for_base_type(ir->type);
184
185 } else if (ir->data.mode == ir_var_system_value) {
186 switch (ir->data.location) {
187 case SYSTEM_VALUE_BASE_VERTEX:
188 case SYSTEM_VALUE_VERTEX_ID:
189 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
190 case SYSTEM_VALUE_INSTANCE_ID:
191 reg = emit_vs_system_value(ir->data.location);
192 break;
193 case SYSTEM_VALUE_SAMPLE_POS:
194 reg = emit_samplepos_setup();
195 break;
196 case SYSTEM_VALUE_SAMPLE_ID:
197 reg = emit_sampleid_setup();
198 break;
199 case SYSTEM_VALUE_SAMPLE_MASK_IN:
200 assert(brw->gen >= 7);
201 reg = new(mem_ctx)
202 fs_reg(retype(brw_vec8_grf(payload.sample_mask_in_reg, 0),
203 BRW_REGISTER_TYPE_D));
204 break;
205 }
206 }
207
208 if (!reg)
209 reg = new(this->mem_ctx) fs_reg(vgrf(ir->type));
210
211 hash_table_insert(this->variable_ht, reg, ir);
212 }
213
214 void
215 fs_visitor::visit(ir_dereference_variable *ir)
216 {
217 fs_reg *reg = variable_storage(ir->var);
218
219 if (!reg) {
220 fail("Failed to find variable storage for %s\n", ir->var->name);
221 this->result = fs_reg(reg_null_d);
222 return;
223 }
224 this->result = *reg;
225 }
226
227 void
228 fs_visitor::visit(ir_dereference_record *ir)
229 {
230 const glsl_type *struct_type = ir->record->type;
231
232 ir->record->accept(this);
233
234 unsigned int off = 0;
235 for (unsigned int i = 0; i < struct_type->length; i++) {
236 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
237 break;
238 off += type_size(struct_type->fields.structure[i].type);
239 }
240 this->result = offset(this->result, off);
241 this->result.type = brw_type_for_base_type(ir->type);
242 }
243
244 void
245 fs_visitor::visit(ir_dereference_array *ir)
246 {
247 ir_constant *constant_index;
248 fs_reg src;
249 int element_size = type_size(ir->type);
250
251 constant_index = ir->array_index->as_constant();
252
253 ir->array->accept(this);
254 src = this->result;
255 src.type = brw_type_for_base_type(ir->type);
256
257 if (constant_index) {
258 if (src.file == ATTR) {
259 /* Attribute arrays get loaded as one vec4 per element. In that case
260 * offset the source register.
261 */
262 src.reg += constant_index->value.i[0];
263 } else {
264 assert(src.file == UNIFORM || src.file == GRF || src.file == HW_REG);
265 src = offset(src, constant_index->value.i[0] * element_size);
266 }
267 } else {
268 /* Variable index array dereference. We attach the variable index
269 * component to the reg as a pointer to a register containing the
270 * offset. Currently only uniform arrays are supported in this patch,
271 * and that reladdr pointer is resolved by
272 * move_uniform_array_access_to_pull_constants(). All other array types
273 * are lowered by lower_variable_index_to_cond_assign().
274 */
275 ir->array_index->accept(this);
276
277 fs_reg index_reg;
278 index_reg = vgrf(glsl_type::int_type);
279 emit(BRW_OPCODE_MUL, index_reg, this->result, fs_reg(element_size));
280
281 if (src.reladdr) {
282 emit(BRW_OPCODE_ADD, index_reg, *src.reladdr, index_reg);
283 }
284
285 src.reladdr = ralloc(mem_ctx, fs_reg);
286 memcpy(src.reladdr, &index_reg, sizeof(index_reg));
287 }
288 this->result = src;
289 }
290
291 void
292 fs_visitor::emit_lrp(const fs_reg &dst, const fs_reg &x, const fs_reg &y,
293 const fs_reg &a)
294 {
295 if (brw->gen < 6) {
296 /* We can't use the LRP instruction. Emit x*(1-a) + y*a. */
297 fs_reg y_times_a = vgrf(glsl_type::float_type);
298 fs_reg one_minus_a = vgrf(glsl_type::float_type);
299 fs_reg x_times_one_minus_a = vgrf(glsl_type::float_type);
300
301 emit(MUL(y_times_a, y, a));
302
303 fs_reg negative_a = a;
304 negative_a.negate = !a.negate;
305 emit(ADD(one_minus_a, negative_a, fs_reg(1.0f)));
306 emit(MUL(x_times_one_minus_a, x, one_minus_a));
307
308 emit(ADD(dst, x_times_one_minus_a, y_times_a));
309 } else {
310 /* The LRP instruction actually does op1 * op0 + op2 * (1 - op0), so
311 * we need to reorder the operands.
312 */
313 emit(LRP(dst, a, y, x));
314 }
315 }
316
317 void
318 fs_visitor::emit_minmax(enum brw_conditional_mod conditionalmod, const fs_reg &dst,
319 const fs_reg &src0, const fs_reg &src1)
320 {
321 assert(conditionalmod == BRW_CONDITIONAL_GE ||
322 conditionalmod == BRW_CONDITIONAL_L);
323
324 fs_inst *inst;
325
326 if (brw->gen >= 6) {
327 inst = emit(BRW_OPCODE_SEL, dst, src0, src1);
328 inst->conditional_mod = conditionalmod;
329 } else {
330 emit(CMP(reg_null_d, src0, src1, conditionalmod));
331
332 inst = emit(BRW_OPCODE_SEL, dst, src0, src1);
333 inst->predicate = BRW_PREDICATE_NORMAL;
334 }
335 }
336
337 bool
338 fs_visitor::try_emit_saturate(ir_expression *ir)
339 {
340 if (ir->operation != ir_unop_saturate)
341 return false;
342
343 ir_rvalue *sat_val = ir->operands[0];
344
345 fs_inst *pre_inst = (fs_inst *) this->instructions.get_tail();
346
347 sat_val->accept(this);
348 fs_reg src = this->result;
349
350 fs_inst *last_inst = (fs_inst *) this->instructions.get_tail();
351
352 /* If the last instruction from our accept() generated our
353 * src, just set the saturate flag instead of emmitting a separate mov.
354 */
355 fs_inst *modify = get_instruction_generating_reg(pre_inst, last_inst, src);
356 if (modify && modify->regs_written == modify->dst.width / 8 &&
357 modify->can_do_saturate()) {
358 modify->saturate = true;
359 this->result = src;
360 return true;
361 }
362
363 return false;
364 }
365
366 bool
367 fs_visitor::try_emit_line(ir_expression *ir)
368 {
369 /* LINE's src0 must be of type float. */
370 if (ir->type != glsl_type::float_type)
371 return false;
372
373 ir_rvalue *nonmul = ir->operands[1];
374 ir_expression *mul = ir->operands[0]->as_expression();
375
376 if (!mul || mul->operation != ir_binop_mul) {
377 nonmul = ir->operands[0];
378 mul = ir->operands[1]->as_expression();
379
380 if (!mul || mul->operation != ir_binop_mul)
381 return false;
382 }
383
384 ir_constant *const_add = nonmul->as_constant();
385 if (!const_add)
386 return false;
387
388 int add_operand_vf = brw_float_to_vf(const_add->value.f[0]);
389 if (add_operand_vf == -1)
390 return false;
391
392 ir_rvalue *non_const_mul = mul->operands[1];
393 ir_constant *const_mul = mul->operands[0]->as_constant();
394 if (!const_mul) {
395 const_mul = mul->operands[1]->as_constant();
396
397 if (!const_mul)
398 return false;
399
400 non_const_mul = mul->operands[0];
401 }
402
403 int mul_operand_vf = brw_float_to_vf(const_mul->value.f[0]);
404 if (mul_operand_vf == -1)
405 return false;
406
407 non_const_mul->accept(this);
408 fs_reg src1 = this->result;
409
410 fs_reg src0 = vgrf(ir->type);
411 emit(BRW_OPCODE_MOV, src0,
412 fs_reg((uint8_t)mul_operand_vf, 0, 0, (uint8_t)add_operand_vf));
413
414 this->result = vgrf(ir->type);
415 emit(BRW_OPCODE_LINE, this->result, src0, src1);
416 return true;
417 }
418
419 bool
420 fs_visitor::try_emit_mad(ir_expression *ir)
421 {
422 /* 3-src instructions were introduced in gen6. */
423 if (brw->gen < 6)
424 return false;
425
426 /* MAD can only handle floating-point data. */
427 if (ir->type != glsl_type::float_type)
428 return false;
429
430 ir_rvalue *nonmul;
431 ir_expression *mul;
432 bool mul_negate, mul_abs;
433
434 for (int i = 0; i < 2; i++) {
435 mul_negate = false;
436 mul_abs = false;
437
438 mul = ir->operands[i]->as_expression();
439 nonmul = ir->operands[1 - i];
440
441 if (mul && mul->operation == ir_unop_abs) {
442 mul = mul->operands[0]->as_expression();
443 mul_abs = true;
444 } else if (mul && mul->operation == ir_unop_neg) {
445 mul = mul->operands[0]->as_expression();
446 mul_negate = true;
447 }
448
449 if (mul && mul->operation == ir_binop_mul)
450 break;
451 }
452
453 if (!mul || mul->operation != ir_binop_mul)
454 return false;
455
456 nonmul->accept(this);
457 fs_reg src0 = this->result;
458
459 mul->operands[0]->accept(this);
460 fs_reg src1 = this->result;
461 src1.negate ^= mul_negate;
462 src1.abs = mul_abs;
463 if (mul_abs)
464 src1.negate = false;
465
466 mul->operands[1]->accept(this);
467 fs_reg src2 = this->result;
468 src2.abs = mul_abs;
469 if (mul_abs)
470 src2.negate = false;
471
472 this->result = vgrf(ir->type);
473 emit(BRW_OPCODE_MAD, this->result, src0, src1, src2);
474
475 return true;
476 }
477
478 bool
479 fs_visitor::try_emit_b2f_of_comparison(ir_expression *ir)
480 {
481 /* On platforms that do not natively generate 0u and ~0u for Boolean
482 * results, b2f expressions that look like
483 *
484 * f = b2f(expr cmp 0)
485 *
486 * will generate better code by pretending the expression is
487 *
488 * f = ir_triop_csel(0.0, 1.0, expr cmp 0)
489 *
490 * This is because the last instruction of "expr" can generate the
491 * condition code for the "cmp 0". This avoids having to do the "-(b & 1)"
492 * trick to generate 0u or ~0u for the Boolean result. This means code like
493 *
494 * mov(16) g16<1>F 1F
495 * mul.ge.f0(16) null g6<8,8,1>F g14<8,8,1>F
496 * (+f0) sel(16) m6<1>F g16<8,8,1>F 0F
497 *
498 * will be generated instead of
499 *
500 * mul(16) g2<1>F g12<8,8,1>F g4<8,8,1>F
501 * cmp.ge.f0(16) g2<1>D g4<8,8,1>F 0F
502 * and(16) g4<1>D g2<8,8,1>D 1D
503 * and(16) m6<1>D -g4<8,8,1>D 0x3f800000UD
504 *
505 * When the comparison is either == 0.0 or != 0.0 using the knowledge that
506 * the true (or false) case already results in zero would allow better code
507 * generation by possibly avoiding a load-immediate instruction.
508 */
509 ir_expression *cmp = ir->operands[0]->as_expression();
510 if (cmp == NULL)
511 return false;
512
513 if (cmp->operation == ir_binop_equal || cmp->operation == ir_binop_nequal) {
514 for (unsigned i = 0; i < 2; i++) {
515 ir_constant *c = cmp->operands[i]->as_constant();
516 if (c == NULL || !c->is_zero())
517 continue;
518
519 ir_expression *expr = cmp->operands[i ^ 1]->as_expression();
520 if (expr != NULL) {
521 fs_reg op[2];
522
523 for (unsigned j = 0; j < 2; j++) {
524 cmp->operands[j]->accept(this);
525 op[j] = this->result;
526
527 resolve_ud_negate(&op[j]);
528 }
529
530 emit_bool_to_cond_code_of_reg(cmp, op);
531
532 /* In this case we know when the condition is true, op[i ^ 1]
533 * contains zero. Invert the predicate, use op[i ^ 1] as src0,
534 * and immediate 1.0f as src1.
535 */
536 this->result = vgrf(ir->type);
537 op[i ^ 1].type = BRW_REGISTER_TYPE_F;
538
539 fs_inst *inst = emit(SEL(this->result, op[i ^ 1], fs_reg(1.0f)));
540 inst->predicate = BRW_PREDICATE_NORMAL;
541 inst->predicate_inverse = cmp->operation == ir_binop_equal;
542 return true;
543 }
544 }
545 }
546
547 emit_bool_to_cond_code(cmp);
548
549 fs_reg temp = vgrf(ir->type);
550 emit(MOV(temp, fs_reg(1.0f)));
551
552 this->result = vgrf(ir->type);
553 fs_inst *inst = emit(SEL(this->result, temp, fs_reg(0.0f)));
554 inst->predicate = BRW_PREDICATE_NORMAL;
555
556 return true;
557 }
558
559 static int
560 pack_pixel_offset(float x)
561 {
562 /* Clamp upper end of the range to +7/16. See explanation in non-constant
563 * offset case below. */
564 int n = MIN2((int)(x * 16), 7);
565 return n & 0xf;
566 }
567
568 void
569 fs_visitor::emit_interpolate_expression(ir_expression *ir)
570 {
571 /* in SIMD16 mode, the pixel interpolator returns coords interleaved
572 * 8 channels at a time, same as the barycentric coords presented in
573 * the FS payload. this requires a bit of extra work to support.
574 */
575 no16("interpolate_at_* not yet supported in SIMD16 mode.");
576
577 assert(stage == MESA_SHADER_FRAGMENT);
578 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
579
580 ir_dereference * deref = ir->operands[0]->as_dereference();
581 ir_swizzle * swiz = NULL;
582 if (!deref) {
583 /* the api does not allow a swizzle here, but the varying packing code
584 * may have pushed one into here.
585 */
586 swiz = ir->operands[0]->as_swizzle();
587 assert(swiz);
588 deref = swiz->val->as_dereference();
589 }
590 assert(deref);
591 ir_variable * var = deref->variable_referenced();
592 assert(var);
593
594 /* 1. collect interpolation factors */
595
596 fs_reg dst_x = vgrf(glsl_type::get_instance(ir->type->base_type, 2, 1));
597 fs_reg dst_y = offset(dst_x, 1);
598
599 /* for most messages, we need one reg of ignored data; the hardware requires mlen==1
600 * even when there is no payload. in the per-slot offset case, we'll replace this with
601 * the proper source data. */
602 fs_reg src = vgrf(glsl_type::float_type);
603 int mlen = 1; /* one reg unless overriden */
604 int reg_width = dispatch_width / 8;
605 fs_inst *inst;
606
607 switch (ir->operation) {
608 case ir_unop_interpolate_at_centroid:
609 inst = emit(FS_OPCODE_INTERPOLATE_AT_CENTROID, dst_x, src, fs_reg(0u));
610 break;
611
612 case ir_binop_interpolate_at_sample: {
613 ir_constant *sample_num = ir->operands[1]->as_constant();
614 assert(sample_num || !"nonconstant sample number should have been lowered.");
615
616 unsigned msg_data = sample_num->value.i[0] << 4;
617 inst = emit(FS_OPCODE_INTERPOLATE_AT_SAMPLE, dst_x, src, fs_reg(msg_data));
618 break;
619 }
620
621 case ir_binop_interpolate_at_offset: {
622 ir_constant *const_offset = ir->operands[1]->as_constant();
623 if (const_offset) {
624 unsigned msg_data = pack_pixel_offset(const_offset->value.f[0]) |
625 (pack_pixel_offset(const_offset->value.f[1]) << 4);
626 inst = emit(FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET, dst_x, src,
627 fs_reg(msg_data));
628 } else {
629 /* pack the operands: hw wants offsets as 4 bit signed ints */
630 ir->operands[1]->accept(this);
631 src = vgrf(glsl_type::ivec2_type);
632 fs_reg src2 = src;
633 for (int i = 0; i < 2; i++) {
634 fs_reg temp = vgrf(glsl_type::float_type);
635 emit(MUL(temp, this->result, fs_reg(16.0f)));
636 emit(MOV(src2, temp)); /* float to int */
637
638 /* Clamp the upper end of the range to +7/16. ARB_gpu_shader5 requires
639 * that we support a maximum offset of +0.5, which isn't representable
640 * in a S0.4 value -- if we didn't clamp it, we'd end up with -8/16,
641 * which is the opposite of what the shader author wanted.
642 *
643 * This is legal due to ARB_gpu_shader5's quantization rules:
644 *
645 * "Not all values of <offset> may be supported; x and y offsets may
646 * be rounded to fixed-point values with the number of fraction bits
647 * given by the implementation-dependent constant
648 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
649 */
650
651 fs_inst *inst = emit(BRW_OPCODE_SEL, src2, src2, fs_reg(7));
652 inst->conditional_mod = BRW_CONDITIONAL_L; /* min(src2, 7) */
653
654 src2 = offset(src2, 1);
655 this->result = offset(this->result, 1);
656 }
657
658 mlen = 2 * reg_width;
659 inst = emit(FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET, dst_x, src,
660 fs_reg(0u));
661 }
662 break;
663 }
664
665 default:
666 unreachable("not reached");
667 }
668
669 inst->mlen = mlen;
670 inst->regs_written = 2 * reg_width; /* 2 floats per slot returned */
671 inst->pi_noperspective = var->determine_interpolation_mode(key->flat_shade) ==
672 INTERP_QUALIFIER_NOPERSPECTIVE;
673
674 /* 2. emit linterp */
675
676 fs_reg res = vgrf(ir->type);
677 this->result = res;
678
679 for (int i = 0; i < ir->type->vector_elements; i++) {
680 int ch = swiz ? ((*(int *)&swiz->mask) >> 2*i) & 3 : i;
681 emit(FS_OPCODE_LINTERP, res,
682 dst_x, dst_y,
683 fs_reg(interp_reg(var->data.location, ch)));
684 res = offset(res, 1);
685 }
686 }
687
688 void
689 fs_visitor::visit(ir_expression *ir)
690 {
691 unsigned int operand;
692 fs_reg op[3], temp;
693 fs_inst *inst;
694 struct brw_wm_prog_key *fs_key = (struct brw_wm_prog_key *) this->key;
695
696 assert(ir->get_num_operands() <= 3);
697
698 if (try_emit_saturate(ir))
699 return;
700
701 /* Deal with the real oddball stuff first */
702 switch (ir->operation) {
703 case ir_binop_add:
704 if (brw->gen <= 5 && try_emit_line(ir))
705 return;
706 if (try_emit_mad(ir))
707 return;
708 break;
709
710 case ir_triop_csel:
711 ir->operands[1]->accept(this);
712 op[1] = this->result;
713 ir->operands[2]->accept(this);
714 op[2] = this->result;
715
716 emit_bool_to_cond_code(ir->operands[0]);
717
718 this->result = vgrf(ir->type);
719 inst = emit(SEL(this->result, op[1], op[2]));
720 inst->predicate = BRW_PREDICATE_NORMAL;
721 return;
722
723 case ir_unop_b2f:
724 if (brw->gen <= 5 && try_emit_b2f_of_comparison(ir))
725 return;
726 break;
727
728 case ir_unop_interpolate_at_centroid:
729 case ir_binop_interpolate_at_offset:
730 case ir_binop_interpolate_at_sample:
731 emit_interpolate_expression(ir);
732 return;
733
734 default:
735 break;
736 }
737
738 for (operand = 0; operand < ir->get_num_operands(); operand++) {
739 ir->operands[operand]->accept(this);
740 if (this->result.file == BAD_FILE) {
741 fail("Failed to get tree for expression operand:\n");
742 ir->operands[operand]->fprint(stderr);
743 fprintf(stderr, "\n");
744 }
745 assert(this->result.file == GRF ||
746 this->result.file == UNIFORM || this->result.file == ATTR);
747 op[operand] = this->result;
748
749 /* Matrix expression operands should have been broken down to vector
750 * operations already.
751 */
752 assert(!ir->operands[operand]->type->is_matrix());
753 /* And then those vector operands should have been broken down to scalar.
754 */
755 assert(!ir->operands[operand]->type->is_vector());
756 }
757
758 /* Storage for our result. If our result goes into an assignment, it will
759 * just get copy-propagated out, so no worries.
760 */
761 this->result = vgrf(ir->type);
762
763 switch (ir->operation) {
764 case ir_unop_logic_not:
765 emit(NOT(this->result, op[0]));
766 break;
767 case ir_unop_neg:
768 op[0].negate = !op[0].negate;
769 emit(MOV(this->result, op[0]));
770 break;
771 case ir_unop_abs:
772 op[0].abs = true;
773 op[0].negate = false;
774 emit(MOV(this->result, op[0]));
775 break;
776 case ir_unop_sign:
777 if (ir->type->is_float()) {
778 /* AND(val, 0x80000000) gives the sign bit.
779 *
780 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
781 * zero.
782 */
783 emit(CMP(reg_null_f, op[0], fs_reg(0.0f), BRW_CONDITIONAL_NZ));
784
785 op[0].type = BRW_REGISTER_TYPE_UD;
786 this->result.type = BRW_REGISTER_TYPE_UD;
787 emit(AND(this->result, op[0], fs_reg(0x80000000u)));
788
789 inst = emit(OR(this->result, this->result, fs_reg(0x3f800000u)));
790 inst->predicate = BRW_PREDICATE_NORMAL;
791
792 this->result.type = BRW_REGISTER_TYPE_F;
793 } else {
794 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
795 * -> non-negative val generates 0x00000000.
796 * Predicated OR sets 1 if val is positive.
797 */
798 emit(CMP(reg_null_d, op[0], fs_reg(0), BRW_CONDITIONAL_G));
799
800 emit(ASR(this->result, op[0], fs_reg(31)));
801
802 inst = emit(OR(this->result, this->result, fs_reg(1)));
803 inst->predicate = BRW_PREDICATE_NORMAL;
804 }
805 break;
806 case ir_unop_rcp:
807 emit_math(SHADER_OPCODE_RCP, this->result, op[0]);
808 break;
809
810 case ir_unop_exp2:
811 emit_math(SHADER_OPCODE_EXP2, this->result, op[0]);
812 break;
813 case ir_unop_log2:
814 emit_math(SHADER_OPCODE_LOG2, this->result, op[0]);
815 break;
816 case ir_unop_exp:
817 case ir_unop_log:
818 unreachable("not reached: should be handled by ir_explog_to_explog2");
819 case ir_unop_sin:
820 case ir_unop_sin_reduced:
821 emit_math(SHADER_OPCODE_SIN, this->result, op[0]);
822 break;
823 case ir_unop_cos:
824 case ir_unop_cos_reduced:
825 emit_math(SHADER_OPCODE_COS, this->result, op[0]);
826 break;
827
828 case ir_unop_dFdx:
829 /* Select one of the two opcodes based on the glHint value. */
830 if (fs_key->high_quality_derivatives)
831 emit(FS_OPCODE_DDX_FINE, this->result, op[0]);
832 else
833 emit(FS_OPCODE_DDX_COARSE, this->result, op[0]);
834 break;
835
836 case ir_unop_dFdx_coarse:
837 emit(FS_OPCODE_DDX_COARSE, this->result, op[0]);
838 break;
839
840 case ir_unop_dFdx_fine:
841 emit(FS_OPCODE_DDX_FINE, this->result, op[0]);
842 break;
843
844 case ir_unop_dFdy:
845 /* Select one of the two opcodes based on the glHint value. */
846 if (fs_key->high_quality_derivatives)
847 emit(FS_OPCODE_DDY_FINE, result, op[0], fs_reg(fs_key->render_to_fbo));
848 else
849 emit(FS_OPCODE_DDY_COARSE, result, op[0], fs_reg(fs_key->render_to_fbo));
850 break;
851
852 case ir_unop_dFdy_coarse:
853 emit(FS_OPCODE_DDY_COARSE, result, op[0], fs_reg(fs_key->render_to_fbo));
854 break;
855
856 case ir_unop_dFdy_fine:
857 emit(FS_OPCODE_DDY_FINE, result, op[0], fs_reg(fs_key->render_to_fbo));
858 break;
859
860 case ir_binop_add:
861 emit(ADD(this->result, op[0], op[1]));
862 break;
863 case ir_binop_sub:
864 unreachable("not reached: should be handled by ir_sub_to_add_neg");
865
866 case ir_binop_mul:
867 if (brw->gen < 8 && ir->type->is_integer()) {
868 /* For integer multiplication, the MUL uses the low 16 bits
869 * of one of the operands (src0 on gen6, src1 on gen7). The
870 * MACH accumulates in the contribution of the upper 16 bits
871 * of that operand.
872 */
873 if (ir->operands[0]->is_uint16_constant()) {
874 if (brw->gen < 7)
875 emit(MUL(this->result, op[0], op[1]));
876 else
877 emit(MUL(this->result, op[1], op[0]));
878 } else if (ir->operands[1]->is_uint16_constant()) {
879 if (brw->gen < 7)
880 emit(MUL(this->result, op[1], op[0]));
881 else
882 emit(MUL(this->result, op[0], op[1]));
883 } else {
884 if (brw->gen >= 7)
885 no16("SIMD16 explicit accumulator operands unsupported\n");
886
887 struct brw_reg acc = retype(brw_acc_reg(dispatch_width),
888 this->result.type);
889
890 emit(MUL(acc, op[0], op[1]));
891 emit(MACH(reg_null_d, op[0], op[1]));
892 emit(MOV(this->result, fs_reg(acc)));
893 }
894 } else {
895 emit(MUL(this->result, op[0], op[1]));
896 }
897 break;
898 case ir_binop_imul_high: {
899 if (brw->gen == 7)
900 no16("SIMD16 explicit accumulator operands unsupported\n");
901
902 struct brw_reg acc = retype(brw_acc_reg(dispatch_width),
903 this->result.type);
904
905 fs_inst *mul = emit(MUL(acc, op[0], op[1]));
906 emit(MACH(this->result, op[0], op[1]));
907
908 /* Until Gen8, integer multiplies read 32-bits from one source, and
909 * 16-bits from the other, and relying on the MACH instruction to
910 * generate the high bits of the result.
911 *
912 * On Gen8, the multiply instruction does a full 32x32-bit multiply,
913 * but in order to do a 64x64-bit multiply we have to simulate the
914 * previous behavior and then use a MACH instruction.
915 *
916 * FINISHME: Don't use source modifiers on src1.
917 */
918 if (brw->gen >= 8) {
919 assert(mul->src[1].type == BRW_REGISTER_TYPE_D ||
920 mul->src[1].type == BRW_REGISTER_TYPE_UD);
921 if (mul->src[1].type == BRW_REGISTER_TYPE_D) {
922 mul->src[1].type = BRW_REGISTER_TYPE_W;
923 } else {
924 mul->src[1].type = BRW_REGISTER_TYPE_UW;
925 }
926 }
927
928 break;
929 }
930 case ir_binop_div:
931 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
932 assert(ir->type->is_integer());
933 emit_math(SHADER_OPCODE_INT_QUOTIENT, this->result, op[0], op[1]);
934 break;
935 case ir_binop_carry: {
936 if (brw->gen == 7)
937 no16("SIMD16 explicit accumulator operands unsupported\n");
938
939 struct brw_reg acc = retype(brw_acc_reg(dispatch_width),
940 BRW_REGISTER_TYPE_UD);
941
942 emit(ADDC(reg_null_ud, op[0], op[1]));
943 emit(MOV(this->result, fs_reg(acc)));
944 break;
945 }
946 case ir_binop_borrow: {
947 if (brw->gen == 7)
948 no16("SIMD16 explicit accumulator operands unsupported\n");
949
950 struct brw_reg acc = retype(brw_acc_reg(dispatch_width),
951 BRW_REGISTER_TYPE_UD);
952
953 emit(SUBB(reg_null_ud, op[0], op[1]));
954 emit(MOV(this->result, fs_reg(acc)));
955 break;
956 }
957 case ir_binop_mod:
958 /* Floating point should be lowered by MOD_TO_FLOOR in the compiler. */
959 assert(ir->type->is_integer());
960 emit_math(SHADER_OPCODE_INT_REMAINDER, this->result, op[0], op[1]);
961 break;
962
963 case ir_binop_less:
964 case ir_binop_greater:
965 case ir_binop_lequal:
966 case ir_binop_gequal:
967 case ir_binop_equal:
968 case ir_binop_all_equal:
969 case ir_binop_nequal:
970 case ir_binop_any_nequal:
971 if (brw->gen <= 5) {
972 resolve_bool_comparison(ir->operands[0], &op[0]);
973 resolve_bool_comparison(ir->operands[1], &op[1]);
974 }
975
976 emit(CMP(this->result, op[0], op[1],
977 brw_conditional_for_comparison(ir->operation)));
978 break;
979
980 case ir_binop_logic_xor:
981 emit(XOR(this->result, op[0], op[1]));
982 break;
983
984 case ir_binop_logic_or:
985 emit(OR(this->result, op[0], op[1]));
986 break;
987
988 case ir_binop_logic_and:
989 emit(AND(this->result, op[0], op[1]));
990 break;
991
992 case ir_binop_dot:
993 case ir_unop_any:
994 unreachable("not reached: should be handled by brw_fs_channel_expressions");
995
996 case ir_unop_noise:
997 unreachable("not reached: should be handled by lower_noise");
998
999 case ir_quadop_vector:
1000 unreachable("not reached: should be handled by lower_quadop_vector");
1001
1002 case ir_binop_vector_extract:
1003 unreachable("not reached: should be handled by lower_vec_index_to_cond_assign()");
1004
1005 case ir_triop_vector_insert:
1006 unreachable("not reached: should be handled by lower_vector_insert()");
1007
1008 case ir_binop_ldexp:
1009 unreachable("not reached: should be handled by ldexp_to_arith()");
1010
1011 case ir_unop_sqrt:
1012 emit_math(SHADER_OPCODE_SQRT, this->result, op[0]);
1013 break;
1014
1015 case ir_unop_rsq:
1016 emit_math(SHADER_OPCODE_RSQ, this->result, op[0]);
1017 break;
1018
1019 case ir_unop_bitcast_i2f:
1020 case ir_unop_bitcast_u2f:
1021 op[0].type = BRW_REGISTER_TYPE_F;
1022 this->result = op[0];
1023 break;
1024 case ir_unop_i2u:
1025 case ir_unop_bitcast_f2u:
1026 op[0].type = BRW_REGISTER_TYPE_UD;
1027 this->result = op[0];
1028 break;
1029 case ir_unop_u2i:
1030 case ir_unop_bitcast_f2i:
1031 op[0].type = BRW_REGISTER_TYPE_D;
1032 this->result = op[0];
1033 break;
1034 case ir_unop_i2f:
1035 case ir_unop_u2f:
1036 case ir_unop_f2i:
1037 case ir_unop_f2u:
1038 emit(MOV(this->result, op[0]));
1039 break;
1040
1041 case ir_unop_b2i:
1042 emit(AND(this->result, op[0], fs_reg(1)));
1043 break;
1044 case ir_unop_b2f:
1045 if (brw->gen <= 5) {
1046 resolve_bool_comparison(ir->operands[0], &op[0]);
1047 }
1048 op[0].type = BRW_REGISTER_TYPE_D;
1049 this->result.type = BRW_REGISTER_TYPE_D;
1050 emit(AND(this->result, op[0], fs_reg(0x3f800000u)));
1051 this->result.type = BRW_REGISTER_TYPE_F;
1052 break;
1053
1054 case ir_unop_f2b:
1055 emit(CMP(this->result, op[0], fs_reg(0.0f), BRW_CONDITIONAL_NZ));
1056 break;
1057 case ir_unop_i2b:
1058 emit(CMP(this->result, op[0], fs_reg(0), BRW_CONDITIONAL_NZ));
1059 break;
1060
1061 case ir_unop_trunc:
1062 emit(RNDZ(this->result, op[0]));
1063 break;
1064 case ir_unop_ceil: {
1065 fs_reg tmp = vgrf(ir->type);
1066 op[0].negate = !op[0].negate;
1067 emit(RNDD(tmp, op[0]));
1068 tmp.negate = true;
1069 emit(MOV(this->result, tmp));
1070 }
1071 break;
1072 case ir_unop_floor:
1073 emit(RNDD(this->result, op[0]));
1074 break;
1075 case ir_unop_fract:
1076 emit(FRC(this->result, op[0]));
1077 break;
1078 case ir_unop_round_even:
1079 emit(RNDE(this->result, op[0]));
1080 break;
1081
1082 case ir_binop_min:
1083 case ir_binop_max:
1084 resolve_ud_negate(&op[0]);
1085 resolve_ud_negate(&op[1]);
1086 emit_minmax(ir->operation == ir_binop_min ?
1087 BRW_CONDITIONAL_L : BRW_CONDITIONAL_GE,
1088 this->result, op[0], op[1]);
1089 break;
1090 case ir_unop_pack_snorm_2x16:
1091 case ir_unop_pack_snorm_4x8:
1092 case ir_unop_pack_unorm_2x16:
1093 case ir_unop_pack_unorm_4x8:
1094 case ir_unop_unpack_snorm_2x16:
1095 case ir_unop_unpack_snorm_4x8:
1096 case ir_unop_unpack_unorm_2x16:
1097 case ir_unop_unpack_unorm_4x8:
1098 case ir_unop_unpack_half_2x16:
1099 case ir_unop_pack_half_2x16:
1100 unreachable("not reached: should be handled by lower_packing_builtins");
1101 case ir_unop_unpack_half_2x16_split_x:
1102 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X, this->result, op[0]);
1103 break;
1104 case ir_unop_unpack_half_2x16_split_y:
1105 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y, this->result, op[0]);
1106 break;
1107 case ir_binop_pow:
1108 emit_math(SHADER_OPCODE_POW, this->result, op[0], op[1]);
1109 break;
1110
1111 case ir_unop_bitfield_reverse:
1112 emit(BFREV(this->result, op[0]));
1113 break;
1114 case ir_unop_bit_count:
1115 emit(CBIT(this->result, op[0]));
1116 break;
1117 case ir_unop_find_msb:
1118 temp = vgrf(glsl_type::uint_type);
1119 emit(FBH(temp, op[0]));
1120
1121 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1122 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1123 * subtract the result from 31 to convert the MSB count into an LSB count.
1124 */
1125
1126 /* FBH only supports UD type for dst, so use a MOV to convert UD to D. */
1127 emit(MOV(this->result, temp));
1128 emit(CMP(reg_null_d, this->result, fs_reg(-1), BRW_CONDITIONAL_NZ));
1129
1130 temp.negate = true;
1131 inst = emit(ADD(this->result, temp, fs_reg(31)));
1132 inst->predicate = BRW_PREDICATE_NORMAL;
1133 break;
1134 case ir_unop_find_lsb:
1135 emit(FBL(this->result, op[0]));
1136 break;
1137 case ir_unop_saturate:
1138 inst = emit(MOV(this->result, op[0]));
1139 inst->saturate = true;
1140 break;
1141 case ir_triop_bitfield_extract:
1142 /* Note that the instruction's argument order is reversed from GLSL
1143 * and the IR.
1144 */
1145 emit(BFE(this->result, op[2], op[1], op[0]));
1146 break;
1147 case ir_binop_bfm:
1148 emit(BFI1(this->result, op[0], op[1]));
1149 break;
1150 case ir_triop_bfi:
1151 emit(BFI2(this->result, op[0], op[1], op[2]));
1152 break;
1153 case ir_quadop_bitfield_insert:
1154 unreachable("not reached: should be handled by "
1155 "lower_instructions::bitfield_insert_to_bfm_bfi");
1156
1157 case ir_unop_bit_not:
1158 emit(NOT(this->result, op[0]));
1159 break;
1160 case ir_binop_bit_and:
1161 emit(AND(this->result, op[0], op[1]));
1162 break;
1163 case ir_binop_bit_xor:
1164 emit(XOR(this->result, op[0], op[1]));
1165 break;
1166 case ir_binop_bit_or:
1167 emit(OR(this->result, op[0], op[1]));
1168 break;
1169
1170 case ir_binop_lshift:
1171 emit(SHL(this->result, op[0], op[1]));
1172 break;
1173
1174 case ir_binop_rshift:
1175 if (ir->type->base_type == GLSL_TYPE_INT)
1176 emit(ASR(this->result, op[0], op[1]));
1177 else
1178 emit(SHR(this->result, op[0], op[1]));
1179 break;
1180 case ir_binop_pack_half_2x16_split:
1181 emit(FS_OPCODE_PACK_HALF_2x16_SPLIT, this->result, op[0], op[1]);
1182 break;
1183 case ir_binop_ubo_load: {
1184 /* This IR node takes a constant uniform block and a constant or
1185 * variable byte offset within the block and loads a vector from that.
1186 */
1187 ir_constant *const_uniform_block = ir->operands[0]->as_constant();
1188 ir_constant *const_offset = ir->operands[1]->as_constant();
1189 fs_reg surf_index;
1190
1191 if (const_uniform_block) {
1192 /* The block index is a constant, so just emit the binding table entry
1193 * as an immediate.
1194 */
1195 surf_index = fs_reg(stage_prog_data->binding_table.ubo_start +
1196 const_uniform_block->value.u[0]);
1197 } else {
1198 /* The block index is not a constant. Evaluate the index expression
1199 * per-channel and add the base UBO index; the generator will select
1200 * a value from any live channel.
1201 */
1202 surf_index = vgrf(glsl_type::uint_type);
1203 emit(ADD(surf_index, op[0],
1204 fs_reg(stage_prog_data->binding_table.ubo_start)))
1205 ->force_writemask_all = true;
1206
1207 /* Assume this may touch any UBO. It would be nice to provide
1208 * a tighter bound, but the array information is already lowered away.
1209 */
1210 brw_mark_surface_used(prog_data,
1211 stage_prog_data->binding_table.ubo_start +
1212 shader_prog->NumUniformBlocks - 1);
1213 }
1214
1215 if (const_offset) {
1216 fs_reg packed_consts = vgrf(glsl_type::float_type);
1217 packed_consts.type = result.type;
1218
1219 fs_reg const_offset_reg = fs_reg(const_offset->value.u[0] & ~15);
1220 emit(new(mem_ctx) fs_inst(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD, 8,
1221 packed_consts, surf_index, const_offset_reg));
1222
1223 for (int i = 0; i < ir->type->vector_elements; i++) {
1224 packed_consts.set_smear(const_offset->value.u[0] % 16 / 4 + i);
1225
1226 /* The std140 packing rules don't allow vectors to cross 16-byte
1227 * boundaries, and a reg is 32 bytes.
1228 */
1229 assert(packed_consts.subreg_offset < 32);
1230
1231 /* UBO bools are any nonzero value. We consider bools to be
1232 * values with the low bit set to 1. Convert them using CMP.
1233 */
1234 if (ir->type->base_type == GLSL_TYPE_BOOL) {
1235 emit(CMP(result, packed_consts, fs_reg(0u), BRW_CONDITIONAL_NZ));
1236 } else {
1237 emit(MOV(result, packed_consts));
1238 }
1239
1240 result = offset(result, 1);
1241 }
1242 } else {
1243 /* Turn the byte offset into a dword offset. */
1244 fs_reg base_offset = vgrf(glsl_type::int_type);
1245 emit(SHR(base_offset, op[1], fs_reg(2)));
1246
1247 for (int i = 0; i < ir->type->vector_elements; i++) {
1248 emit(VARYING_PULL_CONSTANT_LOAD(result, surf_index,
1249 base_offset, i));
1250
1251 if (ir->type->base_type == GLSL_TYPE_BOOL)
1252 emit(CMP(result, result, fs_reg(0), BRW_CONDITIONAL_NZ));
1253
1254 result = offset(result, 1);
1255 }
1256 }
1257
1258 result.reg_offset = 0;
1259 break;
1260 }
1261
1262 case ir_triop_fma:
1263 /* Note that the instruction's argument order is reversed from GLSL
1264 * and the IR.
1265 */
1266 emit(MAD(this->result, op[2], op[1], op[0]));
1267 break;
1268
1269 case ir_triop_lrp:
1270 emit_lrp(this->result, op[0], op[1], op[2]);
1271 break;
1272
1273 case ir_triop_csel:
1274 case ir_unop_interpolate_at_centroid:
1275 case ir_binop_interpolate_at_offset:
1276 case ir_binop_interpolate_at_sample:
1277 unreachable("already handled above");
1278 break;
1279
1280 case ir_unop_d2f:
1281 case ir_unop_f2d:
1282 case ir_unop_d2i:
1283 case ir_unop_i2d:
1284 case ir_unop_d2u:
1285 case ir_unop_u2d:
1286 case ir_unop_d2b:
1287 case ir_unop_pack_double_2x32:
1288 case ir_unop_unpack_double_2x32:
1289 case ir_unop_frexp_sig:
1290 case ir_unop_frexp_exp:
1291 unreachable("fp64 todo");
1292 break;
1293 }
1294 }
1295
1296 void
1297 fs_visitor::emit_assignment_writes(fs_reg &l, fs_reg &r,
1298 const glsl_type *type, bool predicated)
1299 {
1300 switch (type->base_type) {
1301 case GLSL_TYPE_FLOAT:
1302 case GLSL_TYPE_UINT:
1303 case GLSL_TYPE_INT:
1304 case GLSL_TYPE_BOOL:
1305 for (unsigned int i = 0; i < type->components(); i++) {
1306 l.type = brw_type_for_base_type(type);
1307 r.type = brw_type_for_base_type(type);
1308
1309 if (predicated || !l.equals(r)) {
1310 fs_inst *inst = emit(MOV(l, r));
1311 inst->predicate = predicated ? BRW_PREDICATE_NORMAL : BRW_PREDICATE_NONE;
1312 }
1313
1314 l = offset(l, 1);
1315 r = offset(r, 1);
1316 }
1317 break;
1318 case GLSL_TYPE_ARRAY:
1319 for (unsigned int i = 0; i < type->length; i++) {
1320 emit_assignment_writes(l, r, type->fields.array, predicated);
1321 }
1322 break;
1323
1324 case GLSL_TYPE_STRUCT:
1325 for (unsigned int i = 0; i < type->length; i++) {
1326 emit_assignment_writes(l, r, type->fields.structure[i].type,
1327 predicated);
1328 }
1329 break;
1330
1331 case GLSL_TYPE_SAMPLER:
1332 case GLSL_TYPE_IMAGE:
1333 case GLSL_TYPE_ATOMIC_UINT:
1334 break;
1335
1336 case GLSL_TYPE_DOUBLE:
1337 case GLSL_TYPE_VOID:
1338 case GLSL_TYPE_ERROR:
1339 case GLSL_TYPE_INTERFACE:
1340 unreachable("not reached");
1341 }
1342 }
1343
1344 /* If the RHS processing resulted in an instruction generating a
1345 * temporary value, and it would be easy to rewrite the instruction to
1346 * generate its result right into the LHS instead, do so. This ends
1347 * up reliably removing instructions where it can be tricky to do so
1348 * later without real UD chain information.
1349 */
1350 bool
1351 fs_visitor::try_rewrite_rhs_to_dst(ir_assignment *ir,
1352 fs_reg dst,
1353 fs_reg src,
1354 fs_inst *pre_rhs_inst,
1355 fs_inst *last_rhs_inst)
1356 {
1357 /* Only attempt if we're doing a direct assignment. */
1358 if (ir->condition ||
1359 !(ir->lhs->type->is_scalar() ||
1360 (ir->lhs->type->is_vector() &&
1361 ir->write_mask == (1 << ir->lhs->type->vector_elements) - 1)))
1362 return false;
1363
1364 /* Make sure the last instruction generated our source reg. */
1365 fs_inst *modify = get_instruction_generating_reg(pre_rhs_inst,
1366 last_rhs_inst,
1367 src);
1368 if (!modify)
1369 return false;
1370
1371 /* If last_rhs_inst wrote a different number of components than our LHS,
1372 * we can't safely rewrite it.
1373 */
1374 if (alloc.sizes[dst.reg] != modify->regs_written)
1375 return false;
1376
1377 /* Success! Rewrite the instruction. */
1378 modify->dst = dst;
1379
1380 return true;
1381 }
1382
1383 void
1384 fs_visitor::visit(ir_assignment *ir)
1385 {
1386 fs_reg l, r;
1387 fs_inst *inst;
1388
1389 /* FINISHME: arrays on the lhs */
1390 ir->lhs->accept(this);
1391 l = this->result;
1392
1393 fs_inst *pre_rhs_inst = (fs_inst *) this->instructions.get_tail();
1394
1395 ir->rhs->accept(this);
1396 r = this->result;
1397
1398 fs_inst *last_rhs_inst = (fs_inst *) this->instructions.get_tail();
1399
1400 assert(l.file != BAD_FILE);
1401 assert(r.file != BAD_FILE);
1402
1403 if (try_rewrite_rhs_to_dst(ir, l, r, pre_rhs_inst, last_rhs_inst))
1404 return;
1405
1406 if (ir->condition) {
1407 emit_bool_to_cond_code(ir->condition);
1408 }
1409
1410 if (ir->lhs->type->is_scalar() ||
1411 ir->lhs->type->is_vector()) {
1412 for (int i = 0; i < ir->lhs->type->vector_elements; i++) {
1413 if (ir->write_mask & (1 << i)) {
1414 inst = emit(MOV(l, r));
1415 if (ir->condition)
1416 inst->predicate = BRW_PREDICATE_NORMAL;
1417 r = offset(r, 1);
1418 }
1419 l = offset(l, 1);
1420 }
1421 } else {
1422 emit_assignment_writes(l, r, ir->lhs->type, ir->condition != NULL);
1423 }
1424 }
1425
1426 fs_inst *
1427 fs_visitor::emit_texture_gen4(ir_texture_opcode op, fs_reg dst,
1428 fs_reg coordinate, int coord_components,
1429 fs_reg shadow_c,
1430 fs_reg lod, fs_reg dPdy, int grad_components,
1431 uint32_t sampler)
1432 {
1433 int mlen;
1434 int base_mrf = 1;
1435 bool simd16 = false;
1436 fs_reg orig_dst;
1437
1438 /* g0 header. */
1439 mlen = 1;
1440
1441 if (shadow_c.file != BAD_FILE) {
1442 for (int i = 0; i < coord_components; i++) {
1443 emit(MOV(fs_reg(MRF, base_mrf + mlen + i), coordinate));
1444 coordinate = offset(coordinate, 1);
1445 }
1446
1447 /* gen4's SIMD8 sampler always has the slots for u,v,r present.
1448 * the unused slots must be zeroed.
1449 */
1450 for (int i = coord_components; i < 3; i++) {
1451 emit(MOV(fs_reg(MRF, base_mrf + mlen + i), fs_reg(0.0f)));
1452 }
1453 mlen += 3;
1454
1455 if (op == ir_tex) {
1456 /* There's no plain shadow compare message, so we use shadow
1457 * compare with a bias of 0.0.
1458 */
1459 emit(MOV(fs_reg(MRF, base_mrf + mlen), fs_reg(0.0f)));
1460 mlen++;
1461 } else if (op == ir_txb || op == ir_txl) {
1462 emit(MOV(fs_reg(MRF, base_mrf + mlen), lod));
1463 mlen++;
1464 } else {
1465 unreachable("Should not get here.");
1466 }
1467
1468 emit(MOV(fs_reg(MRF, base_mrf + mlen), shadow_c));
1469 mlen++;
1470 } else if (op == ir_tex) {
1471 for (int i = 0; i < coord_components; i++) {
1472 emit(MOV(fs_reg(MRF, base_mrf + mlen + i), coordinate));
1473 coordinate = offset(coordinate, 1);
1474 }
1475 /* zero the others. */
1476 for (int i = coord_components; i<3; i++) {
1477 emit(MOV(fs_reg(MRF, base_mrf + mlen + i), fs_reg(0.0f)));
1478 }
1479 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
1480 mlen += 3;
1481 } else if (op == ir_txd) {
1482 fs_reg &dPdx = lod;
1483
1484 for (int i = 0; i < coord_components; i++) {
1485 emit(MOV(fs_reg(MRF, base_mrf + mlen + i), coordinate));
1486 coordinate = offset(coordinate, 1);
1487 }
1488 /* the slots for u and v are always present, but r is optional */
1489 mlen += MAX2(coord_components, 2);
1490
1491 /* P = u, v, r
1492 * dPdx = dudx, dvdx, drdx
1493 * dPdy = dudy, dvdy, drdy
1494 *
1495 * 1-arg: Does not exist.
1496 *
1497 * 2-arg: dudx dvdx dudy dvdy
1498 * dPdx.x dPdx.y dPdy.x dPdy.y
1499 * m4 m5 m6 m7
1500 *
1501 * 3-arg: dudx dvdx drdx dudy dvdy drdy
1502 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
1503 * m5 m6 m7 m8 m9 m10
1504 */
1505 for (int i = 0; i < grad_components; i++) {
1506 emit(MOV(fs_reg(MRF, base_mrf + mlen), dPdx));
1507 dPdx = offset(dPdx, 1);
1508 }
1509 mlen += MAX2(grad_components, 2);
1510
1511 for (int i = 0; i < grad_components; i++) {
1512 emit(MOV(fs_reg(MRF, base_mrf + mlen), dPdy));
1513 dPdy = offset(dPdy, 1);
1514 }
1515 mlen += MAX2(grad_components, 2);
1516 } else if (op == ir_txs) {
1517 /* There's no SIMD8 resinfo message on Gen4. Use SIMD16 instead. */
1518 simd16 = true;
1519 emit(MOV(fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_UD), lod));
1520 mlen += 2;
1521 } else {
1522 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
1523 * instructions. We'll need to do SIMD16 here.
1524 */
1525 simd16 = true;
1526 assert(op == ir_txb || op == ir_txl || op == ir_txf);
1527
1528 for (int i = 0; i < coord_components; i++) {
1529 emit(MOV(fs_reg(MRF, base_mrf + mlen + i * 2, coordinate.type),
1530 coordinate));
1531 coordinate = offset(coordinate, 1);
1532 }
1533
1534 /* Initialize the rest of u/v/r with 0.0. Empirically, this seems to
1535 * be necessary for TXF (ld), but seems wise to do for all messages.
1536 */
1537 for (int i = coord_components; i < 3; i++) {
1538 emit(MOV(fs_reg(MRF, base_mrf + mlen + i * 2), fs_reg(0.0f)));
1539 }
1540
1541 /* lod/bias appears after u/v/r. */
1542 mlen += 6;
1543
1544 emit(MOV(fs_reg(MRF, base_mrf + mlen, lod.type), lod));
1545 mlen++;
1546
1547 /* The unused upper half. */
1548 mlen++;
1549 }
1550
1551 if (simd16) {
1552 /* Now, since we're doing simd16, the return is 2 interleaved
1553 * vec4s where the odd-indexed ones are junk. We'll need to move
1554 * this weirdness around to the expected layout.
1555 */
1556 orig_dst = dst;
1557 dst = fs_reg(GRF, alloc.allocate(8), orig_dst.type);
1558 }
1559
1560 enum opcode opcode;
1561 switch (op) {
1562 case ir_tex: opcode = SHADER_OPCODE_TEX; break;
1563 case ir_txb: opcode = FS_OPCODE_TXB; break;
1564 case ir_txl: opcode = SHADER_OPCODE_TXL; break;
1565 case ir_txd: opcode = SHADER_OPCODE_TXD; break;
1566 case ir_txs: opcode = SHADER_OPCODE_TXS; break;
1567 case ir_txf: opcode = SHADER_OPCODE_TXF; break;
1568 default:
1569 unreachable("not reached");
1570 }
1571
1572 fs_inst *inst = emit(opcode, dst, reg_undef, fs_reg(sampler));
1573 inst->base_mrf = base_mrf;
1574 inst->mlen = mlen;
1575 inst->header_present = true;
1576 inst->regs_written = simd16 ? 8 : 4;
1577
1578 if (simd16) {
1579 for (int i = 0; i < 4; i++) {
1580 emit(MOV(orig_dst, dst));
1581 orig_dst = offset(orig_dst, 1);
1582 dst = offset(dst, 2);
1583 }
1584 }
1585
1586 return inst;
1587 }
1588
1589 /* gen5's sampler has slots for u, v, r, array index, then optional
1590 * parameters like shadow comparitor or LOD bias. If optional
1591 * parameters aren't present, those base slots are optional and don't
1592 * need to be included in the message.
1593 *
1594 * We don't fill in the unnecessary slots regardless, which may look
1595 * surprising in the disassembly.
1596 */
1597 fs_inst *
1598 fs_visitor::emit_texture_gen5(ir_texture_opcode op, fs_reg dst,
1599 fs_reg coordinate, int vector_elements,
1600 fs_reg shadow_c,
1601 fs_reg lod, fs_reg lod2, int grad_components,
1602 fs_reg sample_index, uint32_t sampler,
1603 bool has_offset)
1604 {
1605 int reg_width = dispatch_width / 8;
1606 bool header_present = false;
1607
1608 fs_reg message(MRF, 2, BRW_REGISTER_TYPE_F, dispatch_width);
1609 fs_reg msg_coords = message;
1610
1611 if (has_offset) {
1612 /* The offsets set up by the ir_texture visitor are in the
1613 * m1 header, so we can't go headerless.
1614 */
1615 header_present = true;
1616 message.reg--;
1617 }
1618
1619 for (int i = 0; i < vector_elements; i++) {
1620 emit(MOV(retype(offset(msg_coords, i), coordinate.type), coordinate));
1621 coordinate = offset(coordinate, 1);
1622 }
1623 fs_reg msg_end = offset(msg_coords, vector_elements);
1624 fs_reg msg_lod = offset(msg_coords, 4);
1625
1626 if (shadow_c.file != BAD_FILE) {
1627 fs_reg msg_shadow = msg_lod;
1628 emit(MOV(msg_shadow, shadow_c));
1629 msg_lod = offset(msg_shadow, 1);
1630 msg_end = msg_lod;
1631 }
1632
1633 enum opcode opcode;
1634 switch (op) {
1635 case ir_tex:
1636 opcode = SHADER_OPCODE_TEX;
1637 break;
1638 case ir_txb:
1639 emit(MOV(msg_lod, lod));
1640 msg_end = offset(msg_lod, 1);
1641
1642 opcode = FS_OPCODE_TXB;
1643 break;
1644 case ir_txl:
1645 emit(MOV(msg_lod, lod));
1646 msg_end = offset(msg_lod, 1);
1647
1648 opcode = SHADER_OPCODE_TXL;
1649 break;
1650 case ir_txd: {
1651 /**
1652 * P = u, v, r
1653 * dPdx = dudx, dvdx, drdx
1654 * dPdy = dudy, dvdy, drdy
1655 *
1656 * Load up these values:
1657 * - dudx dudy dvdx dvdy drdx drdy
1658 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
1659 */
1660 msg_end = msg_lod;
1661 for (int i = 0; i < grad_components; i++) {
1662 emit(MOV(msg_end, lod));
1663 lod = offset(lod, 1);
1664 msg_end = offset(msg_end, 1);
1665
1666 emit(MOV(msg_end, lod2));
1667 lod2 = offset(lod2, 1);
1668 msg_end = offset(msg_end, 1);
1669 }
1670
1671 opcode = SHADER_OPCODE_TXD;
1672 break;
1673 }
1674 case ir_txs:
1675 msg_lod = retype(msg_end, BRW_REGISTER_TYPE_UD);
1676 emit(MOV(msg_lod, lod));
1677 msg_end = offset(msg_lod, 1);
1678
1679 opcode = SHADER_OPCODE_TXS;
1680 break;
1681 case ir_query_levels:
1682 msg_lod = msg_end;
1683 emit(MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), fs_reg(0u)));
1684 msg_end = offset(msg_lod, 1);
1685
1686 opcode = SHADER_OPCODE_TXS;
1687 break;
1688 case ir_txf:
1689 msg_lod = offset(msg_coords, 3);
1690 emit(MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), lod));
1691 msg_end = offset(msg_lod, 1);
1692
1693 opcode = SHADER_OPCODE_TXF;
1694 break;
1695 case ir_txf_ms:
1696 msg_lod = offset(msg_coords, 3);
1697 /* lod */
1698 emit(MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), fs_reg(0u)));
1699 /* sample index */
1700 emit(MOV(retype(offset(msg_lod, 1), BRW_REGISTER_TYPE_UD), sample_index));
1701 msg_end = offset(msg_lod, 2);
1702
1703 opcode = SHADER_OPCODE_TXF_CMS;
1704 break;
1705 case ir_lod:
1706 opcode = SHADER_OPCODE_LOD;
1707 break;
1708 case ir_tg4:
1709 opcode = SHADER_OPCODE_TG4;
1710 break;
1711 default:
1712 unreachable("not reached");
1713 }
1714
1715 fs_inst *inst = emit(opcode, dst, reg_undef, fs_reg(sampler));
1716 inst->base_mrf = message.reg;
1717 inst->mlen = msg_end.reg - message.reg;
1718 inst->header_present = header_present;
1719 inst->regs_written = 4 * reg_width;
1720
1721 if (inst->mlen > MAX_SAMPLER_MESSAGE_SIZE) {
1722 fail("Message length >" STRINGIFY(MAX_SAMPLER_MESSAGE_SIZE)
1723 " disallowed by hardware\n");
1724 }
1725
1726 return inst;
1727 }
1728
1729 static bool
1730 is_high_sampler(struct brw_context *brw, fs_reg sampler)
1731 {
1732 if (brw->gen < 8 && !brw->is_haswell)
1733 return false;
1734
1735 return sampler.file != IMM || sampler.fixed_hw_reg.dw1.ud >= 16;
1736 }
1737
1738 fs_inst *
1739 fs_visitor::emit_texture_gen7(ir_texture_opcode op, fs_reg dst,
1740 fs_reg coordinate, int coord_components,
1741 fs_reg shadow_c,
1742 fs_reg lod, fs_reg lod2, int grad_components,
1743 fs_reg sample_index, fs_reg mcs, fs_reg sampler,
1744 fs_reg offset_value)
1745 {
1746 int reg_width = dispatch_width / 8;
1747 bool header_present = false;
1748
1749 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, MAX_SAMPLER_MESSAGE_SIZE);
1750 for (int i = 0; i < MAX_SAMPLER_MESSAGE_SIZE; i++) {
1751 sources[i] = vgrf(glsl_type::float_type);
1752 }
1753 int length = 0;
1754
1755 if (op == ir_tg4 || offset_value.file != BAD_FILE ||
1756 is_high_sampler(brw, sampler)) {
1757 /* For general texture offsets (no txf workaround), we need a header to
1758 * put them in. Note that for SIMD16 we're making space for two actual
1759 * hardware registers here, so the emit will have to fix up for this.
1760 *
1761 * * ir4_tg4 needs to place its channel select in the header,
1762 * for interaction with ARB_texture_swizzle
1763 *
1764 * The sampler index is only 4-bits, so for larger sampler numbers we
1765 * need to offset the Sampler State Pointer in the header.
1766 */
1767 header_present = true;
1768 sources[0] = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
1769 length++;
1770 }
1771
1772 if (shadow_c.file != BAD_FILE) {
1773 emit(MOV(sources[length], shadow_c));
1774 length++;
1775 }
1776
1777 bool has_nonconstant_offset =
1778 offset_value.file != BAD_FILE && offset_value.file != IMM;
1779 bool coordinate_done = false;
1780
1781 /* Set up the LOD info */
1782 switch (op) {
1783 case ir_tex:
1784 case ir_lod:
1785 break;
1786 case ir_txb:
1787 emit(MOV(sources[length], lod));
1788 length++;
1789 break;
1790 case ir_txl:
1791 emit(MOV(sources[length], lod));
1792 length++;
1793 break;
1794 case ir_txd: {
1795 no16("Gen7 does not support sample_d/sample_d_c in SIMD16 mode.");
1796
1797 /* Load dPdx and the coordinate together:
1798 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
1799 */
1800 for (int i = 0; i < coord_components; i++) {
1801 emit(MOV(sources[length], coordinate));
1802 coordinate = offset(coordinate, 1);
1803 length++;
1804
1805 /* For cube map array, the coordinate is (u,v,r,ai) but there are
1806 * only derivatives for (u, v, r).
1807 */
1808 if (i < grad_components) {
1809 emit(MOV(sources[length], lod));
1810 lod = offset(lod, 1);
1811 length++;
1812
1813 emit(MOV(sources[length], lod2));
1814 lod2 = offset(lod2, 1);
1815 length++;
1816 }
1817 }
1818
1819 coordinate_done = true;
1820 break;
1821 }
1822 case ir_txs:
1823 emit(MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), lod));
1824 length++;
1825 break;
1826 case ir_query_levels:
1827 emit(MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), fs_reg(0u)));
1828 length++;
1829 break;
1830 case ir_txf:
1831 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r. */
1832 emit(MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate));
1833 coordinate = offset(coordinate, 1);
1834 length++;
1835
1836 emit(MOV(retype(sources[length], BRW_REGISTER_TYPE_D), lod));
1837 length++;
1838
1839 for (int i = 1; i < coord_components; i++) {
1840 emit(MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate));
1841 coordinate = offset(coordinate, 1);
1842 length++;
1843 }
1844
1845 coordinate_done = true;
1846 break;
1847 case ir_txf_ms:
1848 emit(MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), sample_index));
1849 length++;
1850
1851 /* data from the multisample control surface */
1852 emit(MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), mcs));
1853 length++;
1854
1855 /* there is no offsetting for this message; just copy in the integer
1856 * texture coordinates
1857 */
1858 for (int i = 0; i < coord_components; i++) {
1859 emit(MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate));
1860 coordinate = offset(coordinate, 1);
1861 length++;
1862 }
1863
1864 coordinate_done = true;
1865 break;
1866 case ir_tg4:
1867 if (has_nonconstant_offset) {
1868 if (shadow_c.file != BAD_FILE)
1869 no16("Gen7 does not support gather4_po_c in SIMD16 mode.");
1870
1871 /* More crazy intermixing */
1872 for (int i = 0; i < 2; i++) { /* u, v */
1873 emit(MOV(sources[length], coordinate));
1874 coordinate = offset(coordinate, 1);
1875 length++;
1876 }
1877
1878 for (int i = 0; i < 2; i++) { /* offu, offv */
1879 emit(MOV(retype(sources[length], BRW_REGISTER_TYPE_D), offset_value));
1880 offset_value = offset(offset_value, 1);
1881 length++;
1882 }
1883
1884 if (coord_components == 3) { /* r if present */
1885 emit(MOV(sources[length], coordinate));
1886 coordinate = offset(coordinate, 1);
1887 length++;
1888 }
1889
1890 coordinate_done = true;
1891 }
1892 break;
1893 }
1894
1895 /* Set up the coordinate (except for cases where it was done above) */
1896 if (!coordinate_done) {
1897 for (int i = 0; i < coord_components; i++) {
1898 emit(MOV(sources[length], coordinate));
1899 coordinate = offset(coordinate, 1);
1900 length++;
1901 }
1902 }
1903
1904 int mlen;
1905 if (reg_width == 2)
1906 mlen = length * reg_width - header_present;
1907 else
1908 mlen = length * reg_width;
1909
1910 fs_reg src_payload = fs_reg(GRF, alloc.allocate(mlen),
1911 BRW_REGISTER_TYPE_F);
1912 emit(LOAD_PAYLOAD(src_payload, sources, length));
1913
1914 /* Generate the SEND */
1915 enum opcode opcode;
1916 switch (op) {
1917 case ir_tex: opcode = SHADER_OPCODE_TEX; break;
1918 case ir_txb: opcode = FS_OPCODE_TXB; break;
1919 case ir_txl: opcode = SHADER_OPCODE_TXL; break;
1920 case ir_txd: opcode = SHADER_OPCODE_TXD; break;
1921 case ir_txf: opcode = SHADER_OPCODE_TXF; break;
1922 case ir_txf_ms: opcode = SHADER_OPCODE_TXF_CMS; break;
1923 case ir_txs: opcode = SHADER_OPCODE_TXS; break;
1924 case ir_query_levels: opcode = SHADER_OPCODE_TXS; break;
1925 case ir_lod: opcode = SHADER_OPCODE_LOD; break;
1926 case ir_tg4:
1927 if (has_nonconstant_offset)
1928 opcode = SHADER_OPCODE_TG4_OFFSET;
1929 else
1930 opcode = SHADER_OPCODE_TG4;
1931 break;
1932 default:
1933 unreachable("not reached");
1934 }
1935 fs_inst *inst = emit(opcode, dst, src_payload, sampler);
1936 inst->base_mrf = -1;
1937 inst->mlen = mlen;
1938 inst->header_present = header_present;
1939 inst->regs_written = 4 * reg_width;
1940
1941 if (inst->mlen > MAX_SAMPLER_MESSAGE_SIZE) {
1942 fail("Message length >" STRINGIFY(MAX_SAMPLER_MESSAGE_SIZE)
1943 " disallowed by hardware\n");
1944 }
1945
1946 return inst;
1947 }
1948
1949 fs_reg
1950 fs_visitor::rescale_texcoord(fs_reg coordinate, int coord_components,
1951 bool is_rect, uint32_t sampler, int texunit)
1952 {
1953 fs_inst *inst = NULL;
1954 bool needs_gl_clamp = true;
1955 fs_reg scale_x, scale_y;
1956
1957 /* The 965 requires the EU to do the normalization of GL rectangle
1958 * texture coordinates. We use the program parameter state
1959 * tracking to get the scaling factor.
1960 */
1961 if (is_rect &&
1962 (brw->gen < 6 ||
1963 (brw->gen >= 6 && (key_tex->gl_clamp_mask[0] & (1 << sampler) ||
1964 key_tex->gl_clamp_mask[1] & (1 << sampler))))) {
1965 struct gl_program_parameter_list *params = prog->Parameters;
1966 int tokens[STATE_LENGTH] = {
1967 STATE_INTERNAL,
1968 STATE_TEXRECT_SCALE,
1969 texunit,
1970 0,
1971 0
1972 };
1973
1974 no16("rectangle scale uniform setup not supported on SIMD16\n");
1975 if (dispatch_width == 16) {
1976 return coordinate;
1977 }
1978
1979 GLuint index = _mesa_add_state_reference(params,
1980 (gl_state_index *)tokens);
1981 /* Try to find existing copies of the texrect scale uniforms. */
1982 for (unsigned i = 0; i < uniforms; i++) {
1983 if (stage_prog_data->param[i] ==
1984 &prog->Parameters->ParameterValues[index][0]) {
1985 scale_x = fs_reg(UNIFORM, i);
1986 scale_y = fs_reg(UNIFORM, i + 1);
1987 break;
1988 }
1989 }
1990
1991 /* If we didn't already set them up, do so now. */
1992 if (scale_x.file == BAD_FILE) {
1993 scale_x = fs_reg(UNIFORM, uniforms);
1994 scale_y = fs_reg(UNIFORM, uniforms + 1);
1995
1996 stage_prog_data->param[uniforms++] =
1997 &prog->Parameters->ParameterValues[index][0];
1998 stage_prog_data->param[uniforms++] =
1999 &prog->Parameters->ParameterValues[index][1];
2000 }
2001 }
2002
2003 /* The 965 requires the EU to do the normalization of GL rectangle
2004 * texture coordinates. We use the program parameter state
2005 * tracking to get the scaling factor.
2006 */
2007 if (brw->gen < 6 && is_rect) {
2008 fs_reg dst = fs_reg(GRF, alloc.allocate(coord_components));
2009 fs_reg src = coordinate;
2010 coordinate = dst;
2011
2012 emit(MUL(dst, src, scale_x));
2013 dst = offset(dst, 1);
2014 src = offset(src, 1);
2015 emit(MUL(dst, src, scale_y));
2016 } else if (is_rect) {
2017 /* On gen6+, the sampler handles the rectangle coordinates
2018 * natively, without needing rescaling. But that means we have
2019 * to do GL_CLAMP clamping at the [0, width], [0, height] scale,
2020 * not [0, 1] like the default case below.
2021 */
2022 needs_gl_clamp = false;
2023
2024 for (int i = 0; i < 2; i++) {
2025 if (key_tex->gl_clamp_mask[i] & (1 << sampler)) {
2026 fs_reg chan = coordinate;
2027 chan = offset(chan, i);
2028
2029 inst = emit(BRW_OPCODE_SEL, chan, chan, fs_reg(0.0f));
2030 inst->conditional_mod = BRW_CONDITIONAL_GE;
2031
2032 /* Our parameter comes in as 1.0/width or 1.0/height,
2033 * because that's what people normally want for doing
2034 * texture rectangle handling. We need width or height
2035 * for clamping, but we don't care enough to make a new
2036 * parameter type, so just invert back.
2037 */
2038 fs_reg limit = vgrf(glsl_type::float_type);
2039 emit(MOV(limit, i == 0 ? scale_x : scale_y));
2040 emit(SHADER_OPCODE_RCP, limit, limit);
2041
2042 inst = emit(BRW_OPCODE_SEL, chan, chan, limit);
2043 inst->conditional_mod = BRW_CONDITIONAL_L;
2044 }
2045 }
2046 }
2047
2048 if (coord_components > 0 && needs_gl_clamp) {
2049 for (int i = 0; i < MIN2(coord_components, 3); i++) {
2050 if (key_tex->gl_clamp_mask[i] & (1 << sampler)) {
2051 fs_reg chan = coordinate;
2052 chan = offset(chan, i);
2053
2054 fs_inst *inst = emit(MOV(chan, chan));
2055 inst->saturate = true;
2056 }
2057 }
2058 }
2059 return coordinate;
2060 }
2061
2062 /* Sample from the MCS surface attached to this multisample texture. */
2063 fs_reg
2064 fs_visitor::emit_mcs_fetch(fs_reg coordinate, int components, fs_reg sampler)
2065 {
2066 int reg_width = dispatch_width / 8;
2067 fs_reg payload = fs_reg(GRF, alloc.allocate(components * reg_width),
2068 BRW_REGISTER_TYPE_F);
2069 fs_reg dest = vgrf(glsl_type::uvec4_type);
2070 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, components);
2071
2072 /* parameters are: u, v, r; missing parameters are treated as zero */
2073 for (int i = 0; i < components; i++) {
2074 sources[i] = vgrf(glsl_type::float_type);
2075 emit(MOV(retype(sources[i], BRW_REGISTER_TYPE_D), coordinate));
2076 coordinate = offset(coordinate, 1);
2077 }
2078
2079 emit(LOAD_PAYLOAD(payload, sources, components));
2080
2081 fs_inst *inst = emit(SHADER_OPCODE_TXF_MCS, dest, payload, sampler);
2082 inst->base_mrf = -1;
2083 inst->mlen = components * reg_width;
2084 inst->header_present = false;
2085 inst->regs_written = 4 * reg_width; /* we only care about one reg of
2086 * response, but the sampler always
2087 * writes 4/8
2088 */
2089
2090 return dest;
2091 }
2092
2093 void
2094 fs_visitor::emit_texture(ir_texture_opcode op,
2095 const glsl_type *dest_type,
2096 fs_reg coordinate, int coord_components,
2097 fs_reg shadow_c,
2098 fs_reg lod, fs_reg lod2, int grad_components,
2099 fs_reg sample_index,
2100 fs_reg offset_value,
2101 fs_reg mcs,
2102 int gather_component,
2103 bool is_cube_array,
2104 bool is_rect,
2105 uint32_t sampler,
2106 fs_reg sampler_reg, int texunit)
2107 {
2108 fs_inst *inst = NULL;
2109
2110 if (op == ir_tg4) {
2111 /* When tg4 is used with the degenerate ZERO/ONE swizzles, don't bother
2112 * emitting anything other than setting up the constant result.
2113 */
2114 int swiz = GET_SWZ(key_tex->swizzles[sampler], gather_component);
2115 if (swiz == SWIZZLE_ZERO || swiz == SWIZZLE_ONE) {
2116
2117 fs_reg res = vgrf(glsl_type::vec4_type);
2118 this->result = res;
2119
2120 for (int i=0; i<4; i++) {
2121 emit(MOV(res, fs_reg(swiz == SWIZZLE_ZERO ? 0.0f : 1.0f)));
2122 res = offset(res, 1);
2123 }
2124 return;
2125 }
2126 }
2127
2128 if (coordinate.file != BAD_FILE) {
2129 /* FINISHME: Texture coordinate rescaling doesn't work with non-constant
2130 * samplers. This should only be a problem with GL_CLAMP on Gen7.
2131 */
2132 coordinate = rescale_texcoord(coordinate, coord_components, is_rect,
2133 sampler, texunit);
2134 }
2135
2136 /* Writemasking doesn't eliminate channels on SIMD8 texture
2137 * samples, so don't worry about them.
2138 */
2139 fs_reg dst = vgrf(glsl_type::get_instance(dest_type->base_type, 4, 1));
2140
2141 if (brw->gen >= 7) {
2142 inst = emit_texture_gen7(op, dst, coordinate, coord_components,
2143 shadow_c, lod, lod2, grad_components,
2144 sample_index, mcs, sampler_reg,
2145 offset_value);
2146 } else if (brw->gen >= 5) {
2147 inst = emit_texture_gen5(op, dst, coordinate, coord_components,
2148 shadow_c, lod, lod2, grad_components,
2149 sample_index, sampler,
2150 offset_value.file != BAD_FILE);
2151 } else {
2152 inst = emit_texture_gen4(op, dst, coordinate, coord_components,
2153 shadow_c, lod, lod2, grad_components,
2154 sampler);
2155 }
2156
2157 if (shadow_c.file != BAD_FILE)
2158 inst->shadow_compare = true;
2159
2160 if (offset_value.file == IMM)
2161 inst->offset = offset_value.fixed_hw_reg.dw1.ud;
2162
2163 if (op == ir_tg4) {
2164 inst->offset |=
2165 gather_channel(gather_component, sampler) << 16; /* M0.2:16-17 */
2166
2167 if (brw->gen == 6)
2168 emit_gen6_gather_wa(key_tex->gen6_gather_wa[sampler], dst);
2169 }
2170
2171 /* fixup #layers for cube map arrays */
2172 if (op == ir_txs && is_cube_array) {
2173 fs_reg depth = offset(dst, 2);
2174 fs_reg fixed_depth = vgrf(glsl_type::int_type);
2175 emit_math(SHADER_OPCODE_INT_QUOTIENT, fixed_depth, depth, fs_reg(6));
2176
2177 fs_reg *fixed_payload = ralloc_array(mem_ctx, fs_reg, inst->regs_written);
2178 int components = inst->regs_written / (dst.width / 8);
2179 for (int i = 0; i < components; i++) {
2180 if (i == 2) {
2181 fixed_payload[i] = fixed_depth;
2182 } else {
2183 fixed_payload[i] = offset(dst, i);
2184 }
2185 }
2186 emit(LOAD_PAYLOAD(dst, fixed_payload, components));
2187 }
2188
2189 swizzle_result(op, dest_type->vector_elements, dst, sampler);
2190 }
2191
2192 void
2193 fs_visitor::visit(ir_texture *ir)
2194 {
2195 uint32_t sampler =
2196 _mesa_get_sampler_uniform_value(ir->sampler, shader_prog, prog);
2197
2198 ir_rvalue *nonconst_sampler_index =
2199 _mesa_get_sampler_array_nonconst_index(ir->sampler);
2200
2201 /* Handle non-constant sampler array indexing */
2202 fs_reg sampler_reg;
2203 if (nonconst_sampler_index) {
2204 /* The highest sampler which may be used by this operation is
2205 * the last element of the array. Mark it here, because the generator
2206 * doesn't have enough information to determine the bound.
2207 */
2208 uint32_t array_size = ir->sampler->as_dereference_array()
2209 ->array->type->array_size();
2210
2211 uint32_t max_used = sampler + array_size - 1;
2212 if (ir->op == ir_tg4 && brw->gen < 8) {
2213 max_used += stage_prog_data->binding_table.gather_texture_start;
2214 } else {
2215 max_used += stage_prog_data->binding_table.texture_start;
2216 }
2217
2218 brw_mark_surface_used(prog_data, max_used);
2219
2220 /* Emit code to evaluate the actual indexing expression */
2221 nonconst_sampler_index->accept(this);
2222 fs_reg temp = vgrf(glsl_type::uint_type);
2223 emit(ADD(temp, this->result, fs_reg(sampler)))
2224 ->force_writemask_all = true;
2225 sampler_reg = temp;
2226 } else {
2227 /* Single sampler, or constant array index; the indexing expression
2228 * is just an immediate.
2229 */
2230 sampler_reg = fs_reg(sampler);
2231 }
2232
2233 /* FINISHME: We're failing to recompile our programs when the sampler is
2234 * updated. This only matters for the texture rectangle scale parameters
2235 * (pre-gen6, or gen6+ with GL_CLAMP).
2236 */
2237 int texunit = prog->SamplerUnits[sampler];
2238
2239 /* Should be lowered by do_lower_texture_projection */
2240 assert(!ir->projector);
2241
2242 /* Should be lowered */
2243 assert(!ir->offset || !ir->offset->type->is_array());
2244
2245 /* Generate code to compute all the subexpression trees. This has to be
2246 * done before loading any values into MRFs for the sampler message since
2247 * generating these values may involve SEND messages that need the MRFs.
2248 */
2249 fs_reg coordinate;
2250 int coord_components = 0;
2251 if (ir->coordinate) {
2252 coord_components = ir->coordinate->type->vector_elements;
2253 ir->coordinate->accept(this);
2254 coordinate = this->result;
2255 }
2256
2257 fs_reg shadow_comparitor;
2258 if (ir->shadow_comparitor) {
2259 ir->shadow_comparitor->accept(this);
2260 shadow_comparitor = this->result;
2261 }
2262
2263 fs_reg offset_value;
2264 if (ir->offset) {
2265 ir_constant *const_offset = ir->offset->as_constant();
2266 if (const_offset) {
2267 /* Store the header bitfield in an IMM register. This allows us to
2268 * use offset_value.file to distinguish between no offset, a constant
2269 * offset, and a non-constant offset.
2270 */
2271 offset_value =
2272 fs_reg(brw_texture_offset(ctx, const_offset->value.i,
2273 const_offset->type->vector_elements));
2274 } else {
2275 ir->offset->accept(this);
2276 offset_value = this->result;
2277 }
2278 }
2279
2280 fs_reg lod, lod2, sample_index, mcs;
2281 int grad_components = 0;
2282 switch (ir->op) {
2283 case ir_tex:
2284 case ir_lod:
2285 case ir_tg4:
2286 case ir_query_levels:
2287 break;
2288 case ir_txb:
2289 ir->lod_info.bias->accept(this);
2290 lod = this->result;
2291 break;
2292 case ir_txd:
2293 ir->lod_info.grad.dPdx->accept(this);
2294 lod = this->result;
2295
2296 ir->lod_info.grad.dPdy->accept(this);
2297 lod2 = this->result;
2298
2299 grad_components = ir->lod_info.grad.dPdx->type->vector_elements;
2300 break;
2301 case ir_txf:
2302 case ir_txl:
2303 case ir_txs:
2304 ir->lod_info.lod->accept(this);
2305 lod = this->result;
2306 break;
2307 case ir_txf_ms:
2308 ir->lod_info.sample_index->accept(this);
2309 sample_index = this->result;
2310
2311 if (brw->gen >= 7 &&
2312 key_tex->compressed_multisample_layout_mask & (1 << sampler)) {
2313 mcs = emit_mcs_fetch(coordinate, ir->coordinate->type->vector_elements,
2314 sampler_reg);
2315 } else {
2316 mcs = fs_reg(0u);
2317 }
2318 break;
2319 default:
2320 unreachable("Unrecognized texture opcode");
2321 };
2322
2323 int gather_component = 0;
2324 if (ir->op == ir_tg4)
2325 gather_component = ir->lod_info.component->as_constant()->value.i[0];
2326
2327 bool is_rect =
2328 ir->sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_RECT;
2329
2330 bool is_cube_array =
2331 ir->sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE &&
2332 ir->sampler->type->sampler_array;
2333
2334 emit_texture(ir->op, ir->type, coordinate, coord_components,
2335 shadow_comparitor, lod, lod2, grad_components,
2336 sample_index, offset_value, mcs,
2337 gather_component, is_cube_array, is_rect, sampler,
2338 sampler_reg, texunit);
2339 }
2340
2341 /**
2342 * Apply workarounds for Gen6 gather with UINT/SINT
2343 */
2344 void
2345 fs_visitor::emit_gen6_gather_wa(uint8_t wa, fs_reg dst)
2346 {
2347 if (!wa)
2348 return;
2349
2350 int width = (wa & WA_8BIT) ? 8 : 16;
2351
2352 for (int i = 0; i < 4; i++) {
2353 fs_reg dst_f = retype(dst, BRW_REGISTER_TYPE_F);
2354 /* Convert from UNORM to UINT */
2355 emit(MUL(dst_f, dst_f, fs_reg((float)((1 << width) - 1))));
2356 emit(MOV(dst, dst_f));
2357
2358 if (wa & WA_SIGN) {
2359 /* Reinterpret the UINT value as a signed INT value by
2360 * shifting the sign bit into place, then shifting back
2361 * preserving sign.
2362 */
2363 emit(SHL(dst, dst, fs_reg(32 - width)));
2364 emit(ASR(dst, dst, fs_reg(32 - width)));
2365 }
2366
2367 dst = offset(dst, 1);
2368 }
2369 }
2370
2371 /**
2372 * Set up the gather channel based on the swizzle, for gather4.
2373 */
2374 uint32_t
2375 fs_visitor::gather_channel(int orig_chan, uint32_t sampler)
2376 {
2377 int swiz = GET_SWZ(key_tex->swizzles[sampler], orig_chan);
2378 switch (swiz) {
2379 case SWIZZLE_X: return 0;
2380 case SWIZZLE_Y:
2381 /* gather4 sampler is broken for green channel on RG32F --
2382 * we must ask for blue instead.
2383 */
2384 if (key_tex->gather_channel_quirk_mask & (1 << sampler))
2385 return 2;
2386 return 1;
2387 case SWIZZLE_Z: return 2;
2388 case SWIZZLE_W: return 3;
2389 default:
2390 unreachable("Not reached"); /* zero, one swizzles handled already */
2391 }
2392 }
2393
2394 /**
2395 * Swizzle the result of a texture result. This is necessary for
2396 * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons.
2397 */
2398 void
2399 fs_visitor::swizzle_result(ir_texture_opcode op, int dest_components,
2400 fs_reg orig_val, uint32_t sampler)
2401 {
2402 if (op == ir_query_levels) {
2403 /* # levels is in .w */
2404 this->result = offset(orig_val, 3);
2405 return;
2406 }
2407
2408 this->result = orig_val;
2409
2410 /* txs,lod don't actually sample the texture, so swizzling the result
2411 * makes no sense.
2412 */
2413 if (op == ir_txs || op == ir_lod || op == ir_tg4)
2414 return;
2415
2416 if (dest_components == 1) {
2417 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
2418 } else if (key_tex->swizzles[sampler] != SWIZZLE_NOOP) {
2419 fs_reg swizzled_result = vgrf(glsl_type::vec4_type);
2420 swizzled_result.type = orig_val.type;
2421
2422 for (int i = 0; i < 4; i++) {
2423 int swiz = GET_SWZ(key_tex->swizzles[sampler], i);
2424 fs_reg l = swizzled_result;
2425 l = offset(l, i);
2426
2427 if (swiz == SWIZZLE_ZERO) {
2428 emit(MOV(l, fs_reg(0.0f)));
2429 } else if (swiz == SWIZZLE_ONE) {
2430 emit(MOV(l, fs_reg(1.0f)));
2431 } else {
2432 emit(MOV(l, offset(orig_val,
2433 GET_SWZ(key_tex->swizzles[sampler], i))));
2434 }
2435 }
2436 this->result = swizzled_result;
2437 }
2438 }
2439
2440 void
2441 fs_visitor::visit(ir_swizzle *ir)
2442 {
2443 ir->val->accept(this);
2444 fs_reg val = this->result;
2445
2446 if (ir->type->vector_elements == 1) {
2447 this->result = offset(this->result, ir->mask.x);
2448 return;
2449 }
2450
2451 fs_reg result = vgrf(ir->type);
2452 this->result = result;
2453
2454 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
2455 fs_reg channel = val;
2456 int swiz = 0;
2457
2458 switch (i) {
2459 case 0:
2460 swiz = ir->mask.x;
2461 break;
2462 case 1:
2463 swiz = ir->mask.y;
2464 break;
2465 case 2:
2466 swiz = ir->mask.z;
2467 break;
2468 case 3:
2469 swiz = ir->mask.w;
2470 break;
2471 }
2472
2473 emit(MOV(result, offset(channel, swiz)));
2474 result = offset(result, 1);
2475 }
2476 }
2477
2478 void
2479 fs_visitor::visit(ir_discard *ir)
2480 {
2481 /* We track our discarded pixels in f0.1. By predicating on it, we can
2482 * update just the flag bits that aren't yet discarded. If there's no
2483 * condition, we emit a CMP of g0 != g0, so all currently executing
2484 * channels will get turned off.
2485 */
2486 fs_inst *cmp;
2487 if (ir->condition) {
2488 emit_bool_to_cond_code(ir->condition);
2489 cmp = (fs_inst *) this->instructions.get_tail();
2490 cmp->conditional_mod = brw_negate_cmod(cmp->conditional_mod);
2491 } else {
2492 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
2493 BRW_REGISTER_TYPE_UW));
2494 cmp = emit(CMP(reg_null_f, some_reg, some_reg, BRW_CONDITIONAL_NZ));
2495 }
2496 cmp->predicate = BRW_PREDICATE_NORMAL;
2497 cmp->flag_subreg = 1;
2498
2499 if (brw->gen >= 6) {
2500 /* For performance, after a discard, jump to the end of the shader.
2501 * Only jump if all relevant channels have been discarded.
2502 */
2503 fs_inst *discard_jump = emit(FS_OPCODE_DISCARD_JUMP);
2504 discard_jump->flag_subreg = 1;
2505
2506 discard_jump->predicate = (dispatch_width == 8)
2507 ? BRW_PREDICATE_ALIGN1_ANY8H
2508 : BRW_PREDICATE_ALIGN1_ANY16H;
2509 discard_jump->predicate_inverse = true;
2510 }
2511 }
2512
2513 void
2514 fs_visitor::visit(ir_constant *ir)
2515 {
2516 /* Set this->result to reg at the bottom of the function because some code
2517 * paths will cause this visitor to be applied to other fields. This will
2518 * cause the value stored in this->result to be modified.
2519 *
2520 * Make reg constant so that it doesn't get accidentally modified along the
2521 * way. Yes, I actually had this problem. :(
2522 */
2523 const fs_reg reg = vgrf(ir->type);
2524 fs_reg dst_reg = reg;
2525
2526 if (ir->type->is_array()) {
2527 const unsigned size = type_size(ir->type->fields.array);
2528
2529 for (unsigned i = 0; i < ir->type->length; i++) {
2530 ir->array_elements[i]->accept(this);
2531 fs_reg src_reg = this->result;
2532
2533 dst_reg.type = src_reg.type;
2534 for (unsigned j = 0; j < size; j++) {
2535 emit(MOV(dst_reg, src_reg));
2536 src_reg = offset(src_reg, 1);
2537 dst_reg = offset(dst_reg, 1);
2538 }
2539 }
2540 } else if (ir->type->is_record()) {
2541 foreach_in_list(ir_constant, field, &ir->components) {
2542 const unsigned size = type_size(field->type);
2543
2544 field->accept(this);
2545 fs_reg src_reg = this->result;
2546
2547 dst_reg.type = src_reg.type;
2548 for (unsigned j = 0; j < size; j++) {
2549 emit(MOV(dst_reg, src_reg));
2550 src_reg = offset(src_reg, 1);
2551 dst_reg = offset(dst_reg, 1);
2552 }
2553 }
2554 } else {
2555 const unsigned size = type_size(ir->type);
2556
2557 for (unsigned i = 0; i < size; i++) {
2558 switch (ir->type->base_type) {
2559 case GLSL_TYPE_FLOAT:
2560 emit(MOV(dst_reg, fs_reg(ir->value.f[i])));
2561 break;
2562 case GLSL_TYPE_UINT:
2563 emit(MOV(dst_reg, fs_reg(ir->value.u[i])));
2564 break;
2565 case GLSL_TYPE_INT:
2566 emit(MOV(dst_reg, fs_reg(ir->value.i[i])));
2567 break;
2568 case GLSL_TYPE_BOOL:
2569 emit(MOV(dst_reg,
2570 fs_reg(ir->value.b[i] != 0 ? (int)ctx->Const.UniformBooleanTrue
2571 : 0)));
2572 break;
2573 default:
2574 unreachable("Non-float/uint/int/bool constant");
2575 }
2576 dst_reg = offset(dst_reg, 1);
2577 }
2578 }
2579
2580 this->result = reg;
2581 }
2582
2583 void
2584 fs_visitor::emit_bool_to_cond_code(ir_rvalue *ir)
2585 {
2586 ir_expression *expr = ir->as_expression();
2587
2588 if (!expr || expr->operation == ir_binop_ubo_load) {
2589 ir->accept(this);
2590
2591 fs_inst *inst = emit(AND(reg_null_d, this->result, fs_reg(1)));
2592 inst->conditional_mod = BRW_CONDITIONAL_NZ;
2593 return;
2594 }
2595
2596 fs_reg op[3];
2597
2598 assert(expr->get_num_operands() <= 3);
2599 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
2600 assert(expr->operands[i]->type->is_scalar());
2601
2602 expr->operands[i]->accept(this);
2603 op[i] = this->result;
2604
2605 resolve_ud_negate(&op[i]);
2606 }
2607
2608 emit_bool_to_cond_code_of_reg(expr, op);
2609 }
2610
2611 void
2612 fs_visitor::emit_bool_to_cond_code_of_reg(ir_expression *expr, fs_reg op[3])
2613 {
2614 fs_inst *inst;
2615
2616 switch (expr->operation) {
2617 case ir_unop_logic_not:
2618 inst = emit(AND(reg_null_d, op[0], fs_reg(1)));
2619 inst->conditional_mod = BRW_CONDITIONAL_Z;
2620 break;
2621
2622 case ir_binop_logic_xor:
2623 if (brw->gen <= 5) {
2624 fs_reg temp = vgrf(expr->type);
2625 emit(XOR(temp, op[0], op[1]));
2626 inst = emit(AND(reg_null_d, temp, fs_reg(1)));
2627 } else {
2628 inst = emit(XOR(reg_null_d, op[0], op[1]));
2629 }
2630 inst->conditional_mod = BRW_CONDITIONAL_NZ;
2631 break;
2632
2633 case ir_binop_logic_or:
2634 if (brw->gen <= 5) {
2635 fs_reg temp = vgrf(expr->type);
2636 emit(OR(temp, op[0], op[1]));
2637 inst = emit(AND(reg_null_d, temp, fs_reg(1)));
2638 } else {
2639 inst = emit(OR(reg_null_d, op[0], op[1]));
2640 }
2641 inst->conditional_mod = BRW_CONDITIONAL_NZ;
2642 break;
2643
2644 case ir_binop_logic_and:
2645 if (brw->gen <= 5) {
2646 fs_reg temp = vgrf(expr->type);
2647 emit(AND(temp, op[0], op[1]));
2648 inst = emit(AND(reg_null_d, temp, fs_reg(1)));
2649 } else {
2650 inst = emit(AND(reg_null_d, op[0], op[1]));
2651 }
2652 inst->conditional_mod = BRW_CONDITIONAL_NZ;
2653 break;
2654
2655 case ir_unop_f2b:
2656 if (brw->gen >= 6) {
2657 emit(CMP(reg_null_d, op[0], fs_reg(0.0f), BRW_CONDITIONAL_NZ));
2658 } else {
2659 inst = emit(MOV(reg_null_f, op[0]));
2660 inst->conditional_mod = BRW_CONDITIONAL_NZ;
2661 }
2662 break;
2663
2664 case ir_unop_i2b:
2665 if (brw->gen >= 6) {
2666 emit(CMP(reg_null_d, op[0], fs_reg(0), BRW_CONDITIONAL_NZ));
2667 } else {
2668 inst = emit(MOV(reg_null_d, op[0]));
2669 inst->conditional_mod = BRW_CONDITIONAL_NZ;
2670 }
2671 break;
2672
2673 case ir_binop_greater:
2674 case ir_binop_gequal:
2675 case ir_binop_less:
2676 case ir_binop_lequal:
2677 case ir_binop_equal:
2678 case ir_binop_all_equal:
2679 case ir_binop_nequal:
2680 case ir_binop_any_nequal:
2681 if (brw->gen <= 5) {
2682 resolve_bool_comparison(expr->operands[0], &op[0]);
2683 resolve_bool_comparison(expr->operands[1], &op[1]);
2684 }
2685
2686 emit(CMP(reg_null_d, op[0], op[1],
2687 brw_conditional_for_comparison(expr->operation)));
2688 break;
2689
2690 case ir_triop_csel: {
2691 /* Expand the boolean condition into the flag register. */
2692 inst = emit(MOV(reg_null_d, op[0]));
2693 inst->conditional_mod = BRW_CONDITIONAL_NZ;
2694
2695 /* Select which boolean to return. */
2696 fs_reg temp = vgrf(expr->operands[1]->type);
2697 inst = emit(SEL(temp, op[1], op[2]));
2698 inst->predicate = BRW_PREDICATE_NORMAL;
2699
2700 /* Expand the result to a condition code. */
2701 inst = emit(MOV(reg_null_d, temp));
2702 inst->conditional_mod = BRW_CONDITIONAL_NZ;
2703 break;
2704 }
2705
2706 default:
2707 unreachable("not reached");
2708 }
2709 }
2710
2711 /**
2712 * Emit a gen6 IF statement with the comparison folded into the IF
2713 * instruction.
2714 */
2715 void
2716 fs_visitor::emit_if_gen6(ir_if *ir)
2717 {
2718 ir_expression *expr = ir->condition->as_expression();
2719
2720 if (expr && expr->operation != ir_binop_ubo_load) {
2721 fs_reg op[3];
2722 fs_inst *inst;
2723 fs_reg temp;
2724
2725 assert(expr->get_num_operands() <= 3);
2726 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
2727 assert(expr->operands[i]->type->is_scalar());
2728
2729 expr->operands[i]->accept(this);
2730 op[i] = this->result;
2731 }
2732
2733 switch (expr->operation) {
2734 case ir_unop_logic_not:
2735 emit(IF(op[0], fs_reg(0), BRW_CONDITIONAL_Z));
2736 return;
2737
2738 case ir_binop_logic_xor:
2739 emit(IF(op[0], op[1], BRW_CONDITIONAL_NZ));
2740 return;
2741
2742 case ir_binop_logic_or:
2743 temp = vgrf(glsl_type::bool_type);
2744 emit(OR(temp, op[0], op[1]));
2745 emit(IF(temp, fs_reg(0), BRW_CONDITIONAL_NZ));
2746 return;
2747
2748 case ir_binop_logic_and:
2749 temp = vgrf(glsl_type::bool_type);
2750 emit(AND(temp, op[0], op[1]));
2751 emit(IF(temp, fs_reg(0), BRW_CONDITIONAL_NZ));
2752 return;
2753
2754 case ir_unop_f2b:
2755 inst = emit(BRW_OPCODE_IF, reg_null_f, op[0], fs_reg(0));
2756 inst->conditional_mod = BRW_CONDITIONAL_NZ;
2757 return;
2758
2759 case ir_unop_i2b:
2760 emit(IF(op[0], fs_reg(0), BRW_CONDITIONAL_NZ));
2761 return;
2762
2763 case ir_binop_greater:
2764 case ir_binop_gequal:
2765 case ir_binop_less:
2766 case ir_binop_lequal:
2767 case ir_binop_equal:
2768 case ir_binop_all_equal:
2769 case ir_binop_nequal:
2770 case ir_binop_any_nequal:
2771 if (brw->gen <= 5) {
2772 resolve_bool_comparison(expr->operands[0], &op[0]);
2773 resolve_bool_comparison(expr->operands[1], &op[1]);
2774 }
2775
2776 emit(IF(op[0], op[1],
2777 brw_conditional_for_comparison(expr->operation)));
2778 return;
2779
2780 case ir_triop_csel: {
2781 /* Expand the boolean condition into the flag register. */
2782 fs_inst *inst = emit(MOV(reg_null_d, op[0]));
2783 inst->conditional_mod = BRW_CONDITIONAL_NZ;
2784
2785 /* Select which boolean to use as the result. */
2786 fs_reg temp = vgrf(expr->operands[1]->type);
2787 inst = emit(SEL(temp, op[1], op[2]));
2788 inst->predicate = BRW_PREDICATE_NORMAL;
2789
2790 emit(IF(temp, fs_reg(0), BRW_CONDITIONAL_NZ));
2791 return;
2792 }
2793
2794 default:
2795 unreachable("not reached");
2796 }
2797 }
2798
2799 ir->condition->accept(this);
2800 emit(IF(this->result, fs_reg(0), BRW_CONDITIONAL_NZ));
2801 }
2802
2803 bool
2804 fs_visitor::try_opt_frontfacing_ternary(ir_if *ir)
2805 {
2806 ir_dereference_variable *deref = ir->condition->as_dereference_variable();
2807 if (!deref || strcmp(deref->var->name, "gl_FrontFacing") != 0)
2808 return false;
2809
2810 if (ir->then_instructions.length() != 1 ||
2811 ir->else_instructions.length() != 1)
2812 return false;
2813
2814 ir_assignment *then_assign =
2815 ((ir_instruction *)ir->then_instructions.head)->as_assignment();
2816 ir_assignment *else_assign =
2817 ((ir_instruction *)ir->else_instructions.head)->as_assignment();
2818
2819 if (!then_assign || then_assign->condition ||
2820 !else_assign || else_assign->condition ||
2821 then_assign->write_mask != else_assign->write_mask ||
2822 !then_assign->lhs->equals(else_assign->lhs))
2823 return false;
2824
2825 ir_constant *then_rhs = then_assign->rhs->as_constant();
2826 ir_constant *else_rhs = else_assign->rhs->as_constant();
2827
2828 if (!then_rhs || !else_rhs)
2829 return false;
2830
2831 if (then_rhs->type->base_type != GLSL_TYPE_FLOAT)
2832 return false;
2833
2834 if ((then_rhs->is_one() && else_rhs->is_negative_one()) ||
2835 (else_rhs->is_one() && then_rhs->is_negative_one())) {
2836 then_assign->lhs->accept(this);
2837 fs_reg dst = this->result;
2838 dst.type = BRW_REGISTER_TYPE_D;
2839 fs_reg tmp = vgrf(glsl_type::int_type);
2840
2841 if (brw->gen >= 6) {
2842 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
2843 fs_reg g0 = fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W));
2844
2845 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
2846 *
2847 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
2848 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
2849 *
2850 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
2851 */
2852
2853 if (then_rhs->is_negative_one()) {
2854 assert(else_rhs->is_one());
2855 g0.negate = true;
2856 }
2857
2858 tmp.type = BRW_REGISTER_TYPE_W;
2859 tmp.subreg_offset = 2;
2860 tmp.stride = 2;
2861
2862 fs_inst *or_inst = emit(OR(tmp, g0, fs_reg(0x3f80)));
2863 or_inst->src[1].type = BRW_REGISTER_TYPE_UW;
2864
2865 tmp.type = BRW_REGISTER_TYPE_D;
2866 tmp.subreg_offset = 0;
2867 tmp.stride = 1;
2868 } else {
2869 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
2870 fs_reg g1_6 = fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D));
2871
2872 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
2873 *
2874 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
2875 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
2876 *
2877 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
2878 */
2879
2880 if (then_rhs->is_negative_one()) {
2881 assert(else_rhs->is_one());
2882 g1_6.negate = true;
2883 }
2884
2885 emit(OR(tmp, g1_6, fs_reg(0x3f800000)));
2886 }
2887 emit(AND(dst, tmp, fs_reg(0xbf800000)));
2888 return true;
2889 }
2890
2891 return false;
2892 }
2893
2894 /**
2895 * Try to replace IF/MOV/ELSE/MOV/ENDIF with SEL.
2896 *
2897 * Many GLSL shaders contain the following pattern:
2898 *
2899 * x = condition ? foo : bar
2900 *
2901 * The compiler emits an ir_if tree for this, since each subexpression might be
2902 * a complex tree that could have side-effects or short-circuit logic.
2903 *
2904 * However, the common case is to simply select one of two constants or
2905 * variable values---which is exactly what SEL is for. In this case, the
2906 * assembly looks like:
2907 *
2908 * (+f0) IF
2909 * MOV dst src0
2910 * ELSE
2911 * MOV dst src1
2912 * ENDIF
2913 *
2914 * which can be easily translated into:
2915 *
2916 * (+f0) SEL dst src0 src1
2917 *
2918 * If src0 is an immediate value, we promote it to a temporary GRF.
2919 */
2920 bool
2921 fs_visitor::try_replace_with_sel()
2922 {
2923 fs_inst *endif_inst = (fs_inst *) instructions.get_tail();
2924 assert(endif_inst->opcode == BRW_OPCODE_ENDIF);
2925
2926 /* Pattern match in reverse: IF, MOV, ELSE, MOV, ENDIF. */
2927 int opcodes[] = {
2928 BRW_OPCODE_IF, BRW_OPCODE_MOV, BRW_OPCODE_ELSE, BRW_OPCODE_MOV,
2929 };
2930
2931 fs_inst *match = (fs_inst *) endif_inst->prev;
2932 for (int i = 0; i < 4; i++) {
2933 if (match->is_head_sentinel() || match->opcode != opcodes[4-i-1])
2934 return false;
2935 match = (fs_inst *) match->prev;
2936 }
2937
2938 /* The opcodes match; it looks like the right sequence of instructions. */
2939 fs_inst *else_mov = (fs_inst *) endif_inst->prev;
2940 fs_inst *then_mov = (fs_inst *) else_mov->prev->prev;
2941 fs_inst *if_inst = (fs_inst *) then_mov->prev;
2942
2943 /* Check that the MOVs are the right form. */
2944 if (then_mov->dst.equals(else_mov->dst) &&
2945 !then_mov->is_partial_write() &&
2946 !else_mov->is_partial_write()) {
2947
2948 /* Remove the matched instructions; we'll emit a SEL to replace them. */
2949 while (!if_inst->next->is_tail_sentinel())
2950 if_inst->next->exec_node::remove();
2951 if_inst->exec_node::remove();
2952
2953 /* Only the last source register can be a constant, so if the MOV in
2954 * the "then" clause uses a constant, we need to put it in a temporary.
2955 */
2956 fs_reg src0(then_mov->src[0]);
2957 if (src0.file == IMM) {
2958 src0 = vgrf(glsl_type::float_type);
2959 src0.type = then_mov->src[0].type;
2960 emit(MOV(src0, then_mov->src[0]));
2961 }
2962
2963 fs_inst *sel;
2964 if (if_inst->conditional_mod) {
2965 /* Sandybridge-specific IF with embedded comparison */
2966 emit(CMP(reg_null_d, if_inst->src[0], if_inst->src[1],
2967 if_inst->conditional_mod));
2968 sel = emit(BRW_OPCODE_SEL, then_mov->dst, src0, else_mov->src[0]);
2969 sel->predicate = BRW_PREDICATE_NORMAL;
2970 } else {
2971 /* Separate CMP and IF instructions */
2972 sel = emit(BRW_OPCODE_SEL, then_mov->dst, src0, else_mov->src[0]);
2973 sel->predicate = if_inst->predicate;
2974 sel->predicate_inverse = if_inst->predicate_inverse;
2975 }
2976
2977 return true;
2978 }
2979
2980 return false;
2981 }
2982
2983 void
2984 fs_visitor::visit(ir_if *ir)
2985 {
2986 if (try_opt_frontfacing_ternary(ir))
2987 return;
2988
2989 /* Don't point the annotation at the if statement, because then it plus
2990 * the then and else blocks get printed.
2991 */
2992 this->base_ir = ir->condition;
2993
2994 if (brw->gen == 6) {
2995 emit_if_gen6(ir);
2996 } else {
2997 emit_bool_to_cond_code(ir->condition);
2998
2999 emit(IF(BRW_PREDICATE_NORMAL));
3000 }
3001
3002 foreach_in_list(ir_instruction, ir_, &ir->then_instructions) {
3003 this->base_ir = ir_;
3004 ir_->accept(this);
3005 }
3006
3007 if (!ir->else_instructions.is_empty()) {
3008 emit(BRW_OPCODE_ELSE);
3009
3010 foreach_in_list(ir_instruction, ir_, &ir->else_instructions) {
3011 this->base_ir = ir_;
3012 ir_->accept(this);
3013 }
3014 }
3015
3016 emit(BRW_OPCODE_ENDIF);
3017
3018 if (!try_replace_with_sel() && brw->gen < 6) {
3019 no16("Can't support (non-uniform) control flow on SIMD16\n");
3020 }
3021 }
3022
3023 void
3024 fs_visitor::visit(ir_loop *ir)
3025 {
3026 if (brw->gen < 6) {
3027 no16("Can't support (non-uniform) control flow on SIMD16\n");
3028 }
3029
3030 this->base_ir = NULL;
3031 emit(BRW_OPCODE_DO);
3032
3033 foreach_in_list(ir_instruction, ir_, &ir->body_instructions) {
3034 this->base_ir = ir_;
3035 ir_->accept(this);
3036 }
3037
3038 this->base_ir = NULL;
3039 emit(BRW_OPCODE_WHILE);
3040 }
3041
3042 void
3043 fs_visitor::visit(ir_loop_jump *ir)
3044 {
3045 switch (ir->mode) {
3046 case ir_loop_jump::jump_break:
3047 emit(BRW_OPCODE_BREAK);
3048 break;
3049 case ir_loop_jump::jump_continue:
3050 emit(BRW_OPCODE_CONTINUE);
3051 break;
3052 }
3053 }
3054
3055 void
3056 fs_visitor::visit_atomic_counter_intrinsic(ir_call *ir)
3057 {
3058 ir_dereference *deref = static_cast<ir_dereference *>(
3059 ir->actual_parameters.get_head());
3060 ir_variable *location = deref->variable_referenced();
3061 unsigned surf_index = (stage_prog_data->binding_table.abo_start +
3062 location->data.binding);
3063
3064 /* Calculate the surface offset */
3065 fs_reg offset = vgrf(glsl_type::uint_type);
3066 ir_dereference_array *deref_array = deref->as_dereference_array();
3067
3068 if (deref_array) {
3069 deref_array->array_index->accept(this);
3070
3071 fs_reg tmp = vgrf(glsl_type::uint_type);
3072 emit(MUL(tmp, this->result, fs_reg(ATOMIC_COUNTER_SIZE)));
3073 emit(ADD(offset, tmp, fs_reg(location->data.atomic.offset)));
3074 } else {
3075 offset = fs_reg(location->data.atomic.offset);
3076 }
3077
3078 /* Emit the appropriate machine instruction */
3079 const char *callee = ir->callee->function_name();
3080 ir->return_deref->accept(this);
3081 fs_reg dst = this->result;
3082
3083 if (!strcmp("__intrinsic_atomic_read", callee)) {
3084 emit_untyped_surface_read(surf_index, dst, offset);
3085
3086 } else if (!strcmp("__intrinsic_atomic_increment", callee)) {
3087 emit_untyped_atomic(BRW_AOP_INC, surf_index, dst, offset,
3088 fs_reg(), fs_reg());
3089
3090 } else if (!strcmp("__intrinsic_atomic_predecrement", callee)) {
3091 emit_untyped_atomic(BRW_AOP_PREDEC, surf_index, dst, offset,
3092 fs_reg(), fs_reg());
3093 }
3094 }
3095
3096 void
3097 fs_visitor::visit(ir_call *ir)
3098 {
3099 const char *callee = ir->callee->function_name();
3100
3101 if (!strcmp("__intrinsic_atomic_read", callee) ||
3102 !strcmp("__intrinsic_atomic_increment", callee) ||
3103 !strcmp("__intrinsic_atomic_predecrement", callee)) {
3104 visit_atomic_counter_intrinsic(ir);
3105 } else {
3106 unreachable("Unsupported intrinsic.");
3107 }
3108 }
3109
3110 void
3111 fs_visitor::visit(ir_return *)
3112 {
3113 unreachable("FINISHME");
3114 }
3115
3116 void
3117 fs_visitor::visit(ir_function *ir)
3118 {
3119 /* Ignore function bodies other than main() -- we shouldn't see calls to
3120 * them since they should all be inlined before we get to ir_to_mesa.
3121 */
3122 if (strcmp(ir->name, "main") == 0) {
3123 const ir_function_signature *sig;
3124 exec_list empty;
3125
3126 sig = ir->matching_signature(NULL, &empty, false);
3127
3128 assert(sig);
3129
3130 foreach_in_list(ir_instruction, ir_, &sig->body) {
3131 this->base_ir = ir_;
3132 ir_->accept(this);
3133 }
3134 }
3135 }
3136
3137 void
3138 fs_visitor::visit(ir_function_signature *)
3139 {
3140 unreachable("not reached");
3141 }
3142
3143 void
3144 fs_visitor::visit(ir_emit_vertex *)
3145 {
3146 unreachable("not reached");
3147 }
3148
3149 void
3150 fs_visitor::visit(ir_end_primitive *)
3151 {
3152 unreachable("not reached");
3153 }
3154
3155 void
3156 fs_visitor::emit_untyped_atomic(unsigned atomic_op, unsigned surf_index,
3157 fs_reg dst, fs_reg offset, fs_reg src0,
3158 fs_reg src1)
3159 {
3160 int reg_width = dispatch_width / 8;
3161 int length = 0;
3162
3163 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, 4);
3164
3165 sources[0] = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
3166 /* Initialize the sample mask in the message header. */
3167 emit(MOV(sources[0], fs_reg(0u)))
3168 ->force_writemask_all = true;
3169
3170 if (stage == MESA_SHADER_FRAGMENT) {
3171 if (((brw_wm_prog_data*)this->prog_data)->uses_kill) {
3172 emit(MOV(component(sources[0], 7), brw_flag_reg(0, 1)))
3173 ->force_writemask_all = true;
3174 } else {
3175 emit(MOV(component(sources[0], 7),
3176 retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UD)))
3177 ->force_writemask_all = true;
3178 }
3179 } else {
3180 /* The execution mask is part of the side-band information sent together with
3181 * the message payload to the data port. It's implicitly ANDed with the sample
3182 * mask sent in the header to compute the actual set of channels that execute
3183 * the atomic operation.
3184 */
3185 assert(stage == MESA_SHADER_VERTEX || stage == MESA_SHADER_COMPUTE);
3186 emit(MOV(component(sources[0], 7),
3187 fs_reg(0xffffu)))->force_writemask_all = true;
3188 }
3189 length++;
3190
3191 /* Set the atomic operation offset. */
3192 sources[1] = vgrf(glsl_type::uint_type);
3193 emit(MOV(sources[1], offset));
3194 length++;
3195
3196 /* Set the atomic operation arguments. */
3197 if (src0.file != BAD_FILE) {
3198 sources[length] = vgrf(glsl_type::uint_type);
3199 emit(MOV(sources[length], src0));
3200 length++;
3201 }
3202
3203 if (src1.file != BAD_FILE) {
3204 sources[length] = vgrf(glsl_type::uint_type);
3205 emit(MOV(sources[length], src1));
3206 length++;
3207 }
3208
3209 int mlen = 1 + (length - 1) * reg_width;
3210 fs_reg src_payload = fs_reg(GRF, alloc.allocate(mlen),
3211 BRW_REGISTER_TYPE_UD);
3212 emit(LOAD_PAYLOAD(src_payload, sources, length));
3213
3214 /* Emit the instruction. */
3215 fs_inst *inst = emit(SHADER_OPCODE_UNTYPED_ATOMIC, dst, src_payload,
3216 fs_reg(atomic_op), fs_reg(surf_index));
3217 inst->mlen = mlen;
3218 }
3219
3220 void
3221 fs_visitor::emit_untyped_surface_read(unsigned surf_index, fs_reg dst,
3222 fs_reg offset)
3223 {
3224 int reg_width = dispatch_width / 8;
3225
3226 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, 2);
3227
3228 sources[0] = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
3229 /* Initialize the sample mask in the message header. */
3230 emit(MOV(sources[0], fs_reg(0u)))
3231 ->force_writemask_all = true;
3232
3233 if (stage == MESA_SHADER_FRAGMENT) {
3234 if (((brw_wm_prog_data*)this->prog_data)->uses_kill) {
3235 emit(MOV(component(sources[0], 7), brw_flag_reg(0, 1)))
3236 ->force_writemask_all = true;
3237 } else {
3238 emit(MOV(component(sources[0], 7),
3239 retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UD)))
3240 ->force_writemask_all = true;
3241 }
3242 } else {
3243 /* The execution mask is part of the side-band information sent together with
3244 * the message payload to the data port. It's implicitly ANDed with the sample
3245 * mask sent in the header to compute the actual set of channels that execute
3246 * the atomic operation.
3247 */
3248 assert(stage == MESA_SHADER_VERTEX || stage == MESA_SHADER_COMPUTE);
3249 emit(MOV(component(sources[0], 7),
3250 fs_reg(0xffffu)))->force_writemask_all = true;
3251 }
3252
3253 /* Set the surface read offset. */
3254 sources[1] = vgrf(glsl_type::uint_type);
3255 emit(MOV(sources[1], offset));
3256
3257 int mlen = 1 + reg_width;
3258 fs_reg src_payload = fs_reg(GRF, alloc.allocate(mlen),
3259 BRW_REGISTER_TYPE_UD);
3260 fs_inst *inst = emit(LOAD_PAYLOAD(src_payload, sources, 2));
3261
3262 /* Emit the instruction. */
3263 inst = emit(SHADER_OPCODE_UNTYPED_SURFACE_READ, dst, src_payload,
3264 fs_reg(surf_index));
3265 inst->mlen = mlen;
3266 }
3267
3268 fs_inst *
3269 fs_visitor::emit(fs_inst *inst)
3270 {
3271 if (dispatch_width == 16 && inst->exec_size == 8)
3272 inst->force_uncompressed = true;
3273
3274 inst->annotation = this->current_annotation;
3275 inst->ir = this->base_ir;
3276
3277 this->instructions.push_tail(inst);
3278
3279 return inst;
3280 }
3281
3282 void
3283 fs_visitor::emit(exec_list list)
3284 {
3285 foreach_in_list_safe(fs_inst, inst, &list) {
3286 inst->exec_node::remove();
3287 emit(inst);
3288 }
3289 }
3290
3291 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
3292 void
3293 fs_visitor::emit_dummy_fs()
3294 {
3295 int reg_width = dispatch_width / 8;
3296
3297 /* Everyone's favorite color. */
3298 const float color[4] = { 1.0, 0.0, 1.0, 0.0 };
3299 for (int i = 0; i < 4; i++) {
3300 emit(MOV(fs_reg(MRF, 2 + i * reg_width, BRW_REGISTER_TYPE_F,
3301 dispatch_width), fs_reg(color[i])));
3302 }
3303
3304 fs_inst *write;
3305 write = emit(FS_OPCODE_FB_WRITE);
3306 write->eot = true;
3307 if (brw->gen >= 6) {
3308 write->base_mrf = 2;
3309 write->mlen = 4 * reg_width;
3310 } else {
3311 write->header_present = true;
3312 write->base_mrf = 0;
3313 write->mlen = 2 + 4 * reg_width;
3314 }
3315
3316 /* Tell the SF we don't have any inputs. Gen4-5 require at least one
3317 * varying to avoid GPU hangs, so set that.
3318 */
3319 brw_wm_prog_data *wm_prog_data = (brw_wm_prog_data *) this->prog_data;
3320 wm_prog_data->num_varying_inputs = brw->gen < 6 ? 1 : 0;
3321 memset(wm_prog_data->urb_setup, -1,
3322 sizeof(wm_prog_data->urb_setup[0]) * VARYING_SLOT_MAX);
3323
3324 /* We don't have any uniforms. */
3325 stage_prog_data->nr_params = 0;
3326 stage_prog_data->nr_pull_params = 0;
3327 stage_prog_data->curb_read_length = 0;
3328 stage_prog_data->dispatch_grf_start_reg = 2;
3329 wm_prog_data->dispatch_grf_start_reg_16 = 2;
3330 grf_used = 1; /* Gen4-5 don't allow zero GRF blocks */
3331
3332 calculate_cfg();
3333 }
3334
3335 /* The register location here is relative to the start of the URB
3336 * data. It will get adjusted to be a real location before
3337 * generate_code() time.
3338 */
3339 struct brw_reg
3340 fs_visitor::interp_reg(int location, int channel)
3341 {
3342 assert(stage == MESA_SHADER_FRAGMENT);
3343 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
3344 int regnr = prog_data->urb_setup[location] * 2 + channel / 2;
3345 int stride = (channel & 1) * 4;
3346
3347 assert(prog_data->urb_setup[location] != -1);
3348
3349 return brw_vec1_grf(regnr, stride);
3350 }
3351
3352 /** Emits the interpolation for the varying inputs. */
3353 void
3354 fs_visitor::emit_interpolation_setup_gen4()
3355 {
3356 this->current_annotation = "compute pixel centers";
3357 this->pixel_x = vgrf(glsl_type::uint_type);
3358 this->pixel_y = vgrf(glsl_type::uint_type);
3359 this->pixel_x.type = BRW_REGISTER_TYPE_UW;
3360 this->pixel_y.type = BRW_REGISTER_TYPE_UW;
3361
3362 emit(FS_OPCODE_PIXEL_X, this->pixel_x);
3363 emit(FS_OPCODE_PIXEL_Y, this->pixel_y);
3364
3365 this->current_annotation = "compute pixel deltas from v0";
3366 if (brw->has_pln) {
3367 this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] =
3368 vgrf(glsl_type::vec2_type);
3369 this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] =
3370 offset(this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC], 1);
3371 } else {
3372 this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] =
3373 vgrf(glsl_type::float_type);
3374 this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] =
3375 vgrf(glsl_type::float_type);
3376 }
3377 emit(ADD(this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC],
3378 this->pixel_x, fs_reg(negate(brw_vec1_grf(1, 0)))));
3379 emit(ADD(this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC],
3380 this->pixel_y, fs_reg(negate(brw_vec1_grf(1, 1)))));
3381
3382 this->current_annotation = "compute pos.w and 1/pos.w";
3383 /* Compute wpos.w. It's always in our setup, since it's needed to
3384 * interpolate the other attributes.
3385 */
3386 this->wpos_w = vgrf(glsl_type::float_type);
3387 emit(FS_OPCODE_LINTERP, wpos_w,
3388 this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC],
3389 this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC],
3390 interp_reg(VARYING_SLOT_POS, 3));
3391 /* Compute the pixel 1/W value from wpos.w. */
3392 this->pixel_w = vgrf(glsl_type::float_type);
3393 emit_math(SHADER_OPCODE_RCP, this->pixel_w, wpos_w);
3394 this->current_annotation = NULL;
3395 }
3396
3397 /** Emits the interpolation for the varying inputs. */
3398 void
3399 fs_visitor::emit_interpolation_setup_gen6()
3400 {
3401 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
3402
3403 /* If the pixel centers end up used, the setup is the same as for gen4. */
3404 this->current_annotation = "compute pixel centers";
3405 fs_reg int_pixel_x = vgrf(glsl_type::uint_type);
3406 fs_reg int_pixel_y = vgrf(glsl_type::uint_type);
3407 int_pixel_x.type = BRW_REGISTER_TYPE_UW;
3408 int_pixel_y.type = BRW_REGISTER_TYPE_UW;
3409 emit(ADD(int_pixel_x,
3410 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
3411 fs_reg(brw_imm_v(0x10101010))));
3412 emit(ADD(int_pixel_y,
3413 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
3414 fs_reg(brw_imm_v(0x11001100))));
3415
3416 /* As of gen6, we can no longer mix float and int sources. We have
3417 * to turn the integer pixel centers into floats for their actual
3418 * use.
3419 */
3420 this->pixel_x = vgrf(glsl_type::float_type);
3421 this->pixel_y = vgrf(glsl_type::float_type);
3422 emit(MOV(this->pixel_x, int_pixel_x));
3423 emit(MOV(this->pixel_y, int_pixel_y));
3424
3425 this->current_annotation = "compute pos.w";
3426 this->pixel_w = fs_reg(brw_vec8_grf(payload.source_w_reg, 0));
3427 this->wpos_w = vgrf(glsl_type::float_type);
3428 emit_math(SHADER_OPCODE_RCP, this->wpos_w, this->pixel_w);
3429
3430 for (int i = 0; i < BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT; ++i) {
3431 uint8_t reg = payload.barycentric_coord_reg[i];
3432 this->delta_x[i] = fs_reg(brw_vec8_grf(reg, 0));
3433 this->delta_y[i] = fs_reg(brw_vec8_grf(reg + 1, 0));
3434 }
3435
3436 this->current_annotation = NULL;
3437 }
3438
3439 int
3440 fs_visitor::setup_color_payload(fs_reg *dst, fs_reg color, unsigned components,
3441 bool use_2nd_half)
3442 {
3443 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
3444 fs_inst *inst;
3445
3446 if (color.file == BAD_FILE) {
3447 return 4 * (dispatch_width / 8);
3448 }
3449
3450 uint8_t colors_enabled;
3451 if (components == 0) {
3452 /* We want to write one component to the alpha channel */
3453 colors_enabled = 0x8;
3454 } else {
3455 /* Enable the first components-many channels */
3456 colors_enabled = (1 << components) - 1;
3457 }
3458
3459 if (dispatch_width == 8 || (brw->gen >= 6 && !do_dual_src)) {
3460 /* SIMD8 write looks like:
3461 * m + 0: r0
3462 * m + 1: r1
3463 * m + 2: g0
3464 * m + 3: g1
3465 *
3466 * gen6 SIMD16 DP write looks like:
3467 * m + 0: r0
3468 * m + 1: r1
3469 * m + 2: g0
3470 * m + 3: g1
3471 * m + 4: b0
3472 * m + 5: b1
3473 * m + 6: a0
3474 * m + 7: a1
3475 */
3476 int len = 0;
3477 for (unsigned i = 0; i < 4; ++i) {
3478 if (colors_enabled & (1 << i)) {
3479 dst[len] = fs_reg(GRF, alloc.allocate(color.width / 8),
3480 color.type, color.width);
3481 inst = emit(MOV(dst[len], offset(color, i)));
3482 inst->saturate = key->clamp_fragment_color;
3483 } else if (color.width == 16) {
3484 /* We need two BAD_FILE slots for a 16-wide color */
3485 len++;
3486 }
3487 len++;
3488 }
3489 return len;
3490 } else if (brw->gen >= 6 && do_dual_src) {
3491 /* SIMD16 dual source blending for gen6+.
3492 *
3493 * From the SNB PRM, volume 4, part 1, page 193:
3494 *
3495 * "The dual source render target messages only have SIMD8 forms due to
3496 * maximum message length limitations. SIMD16 pixel shaders must send two
3497 * of these messages to cover all of the pixels. Each message contains
3498 * two colors (4 channels each) for each pixel in the message payload."
3499 *
3500 * So in SIMD16 dual source blending we will send 2 SIMD8 messages,
3501 * each one will call this function twice (one for each color involved),
3502 * so in each pass we only write 4 registers. Notice that the second
3503 * SIMD8 message needs to read color data from the 2nd half of the color
3504 * registers, so it needs to call this with use_2nd_half = true.
3505 */
3506 for (unsigned i = 0; i < 4; ++i) {
3507 if (colors_enabled & (1 << i)) {
3508 dst[i] = fs_reg(GRF, alloc.allocate(1), color.type);
3509 inst = emit(MOV(dst[i], half(offset(color, i),
3510 use_2nd_half ? 1 : 0)));
3511 inst->saturate = key->clamp_fragment_color;
3512 if (use_2nd_half)
3513 inst->force_sechalf = true;
3514 }
3515 }
3516 return 4;
3517 } else {
3518 /* pre-gen6 SIMD16 single source DP write looks like:
3519 * m + 0: r0
3520 * m + 1: g0
3521 * m + 2: b0
3522 * m + 3: a0
3523 * m + 4: r1
3524 * m + 5: g1
3525 * m + 6: b1
3526 * m + 7: a1
3527 */
3528 for (unsigned i = 0; i < 4; ++i) {
3529 if (colors_enabled & (1 << i)) {
3530 dst[i] = fs_reg(GRF, alloc.allocate(1), color.type);
3531 inst = emit(MOV(dst[i], half(offset(color, i), 0)));
3532 inst->saturate = key->clamp_fragment_color;
3533
3534 dst[i + 4] = fs_reg(GRF, alloc.allocate(1), color.type);
3535 inst = emit(MOV(dst[i + 4], half(offset(color, i), 1)));
3536 inst->saturate = key->clamp_fragment_color;
3537 inst->force_sechalf = true;
3538 }
3539 }
3540 return 8;
3541 }
3542 }
3543
3544 static enum brw_conditional_mod
3545 cond_for_alpha_func(GLenum func)
3546 {
3547 switch(func) {
3548 case GL_GREATER:
3549 return BRW_CONDITIONAL_G;
3550 case GL_GEQUAL:
3551 return BRW_CONDITIONAL_GE;
3552 case GL_LESS:
3553 return BRW_CONDITIONAL_L;
3554 case GL_LEQUAL:
3555 return BRW_CONDITIONAL_LE;
3556 case GL_EQUAL:
3557 return BRW_CONDITIONAL_EQ;
3558 case GL_NOTEQUAL:
3559 return BRW_CONDITIONAL_NEQ;
3560 default:
3561 unreachable("Not reached");
3562 }
3563 }
3564
3565 /**
3566 * Alpha test support for when we compile it into the shader instead
3567 * of using the normal fixed-function alpha test.
3568 */
3569 void
3570 fs_visitor::emit_alpha_test()
3571 {
3572 assert(stage == MESA_SHADER_FRAGMENT);
3573 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
3574 this->current_annotation = "Alpha test";
3575
3576 fs_inst *cmp;
3577 if (key->alpha_test_func == GL_ALWAYS)
3578 return;
3579
3580 if (key->alpha_test_func == GL_NEVER) {
3581 /* f0.1 = 0 */
3582 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
3583 BRW_REGISTER_TYPE_UW));
3584 cmp = emit(CMP(reg_null_f, some_reg, some_reg,
3585 BRW_CONDITIONAL_NEQ));
3586 } else {
3587 /* RT0 alpha */
3588 fs_reg color = offset(outputs[0], 3);
3589
3590 /* f0.1 &= func(color, ref) */
3591 cmp = emit(CMP(reg_null_f, color, fs_reg(key->alpha_test_ref),
3592 cond_for_alpha_func(key->alpha_test_func)));
3593 }
3594 cmp->predicate = BRW_PREDICATE_NORMAL;
3595 cmp->flag_subreg = 1;
3596 }
3597
3598 fs_inst *
3599 fs_visitor::emit_single_fb_write(fs_reg color0, fs_reg color1,
3600 fs_reg src0_alpha, unsigned components,
3601 bool use_2nd_half)
3602 {
3603 assert(stage == MESA_SHADER_FRAGMENT);
3604 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
3605 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
3606
3607 this->current_annotation = "FB write header";
3608 bool header_present = true;
3609 int reg_size = dispatch_width / 8;
3610
3611 /* We can potentially have a message length of up to 15, so we have to set
3612 * base_mrf to either 0 or 1 in order to fit in m0..m15.
3613 */
3614 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, 15);
3615 int length = 0;
3616
3617 /* From the Sandy Bridge PRM, volume 4, page 198:
3618 *
3619 * "Dispatched Pixel Enables. One bit per pixel indicating
3620 * which pixels were originally enabled when the thread was
3621 * dispatched. This field is only required for the end-of-
3622 * thread message and on all dual-source messages."
3623 */
3624 if (brw->gen >= 6 &&
3625 (brw->is_haswell || brw->gen >= 8 || !prog_data->uses_kill) &&
3626 color1.file == BAD_FILE &&
3627 key->nr_color_regions == 1) {
3628 header_present = false;
3629 }
3630
3631 if (header_present)
3632 /* Allocate 2 registers for a header */
3633 length += 2;
3634
3635 if (payload.aa_dest_stencil_reg) {
3636 sources[length] = fs_reg(GRF, alloc.allocate(1));
3637 emit(MOV(sources[length],
3638 fs_reg(brw_vec8_grf(payload.aa_dest_stencil_reg, 0))));
3639 length++;
3640 }
3641
3642 prog_data->uses_omask =
3643 prog->OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK);
3644 if (prog_data->uses_omask) {
3645 this->current_annotation = "FB write oMask";
3646 assert(this->sample_mask.file != BAD_FILE);
3647 /* Hand over gl_SampleMask. Only lower 16 bits are relevant. Since
3648 * it's unsinged single words, one vgrf is always 16-wide.
3649 */
3650 sources[length] = fs_reg(GRF, alloc.allocate(1),
3651 BRW_REGISTER_TYPE_UW, 16);
3652 emit(FS_OPCODE_SET_OMASK, sources[length], this->sample_mask);
3653 length++;
3654 }
3655
3656 if (color0.file == BAD_FILE) {
3657 /* Even if there's no color buffers enabled, we still need to send
3658 * alpha out the pipeline to our null renderbuffer to support
3659 * alpha-testing, alpha-to-coverage, and so on.
3660 */
3661 length += setup_color_payload(sources + length, this->outputs[0], 0,
3662 false);
3663 } else if (color1.file == BAD_FILE) {
3664 if (src0_alpha.file != BAD_FILE) {
3665 sources[length] = fs_reg(GRF, alloc.allocate(reg_size),
3666 src0_alpha.type, src0_alpha.width);
3667 fs_inst *inst = emit(MOV(sources[length], src0_alpha));
3668 inst->saturate = key->clamp_fragment_color;
3669 length++;
3670 }
3671
3672 length += setup_color_payload(sources + length, color0, components,
3673 false);
3674 } else {
3675 length += setup_color_payload(sources + length, color0, components,
3676 use_2nd_half);
3677 length += setup_color_payload(sources + length, color1, components,
3678 use_2nd_half);
3679 }
3680
3681 if (source_depth_to_render_target) {
3682 if (brw->gen == 6) {
3683 /* For outputting oDepth on gen6, SIMD8 writes have to be
3684 * used. This would require SIMD8 moves of each half to
3685 * message regs, kind of like pre-gen5 SIMD16 FB writes.
3686 * Just bail on doing so for now.
3687 */
3688 no16("Missing support for simd16 depth writes on gen6\n");
3689 }
3690
3691 sources[length] = vgrf(glsl_type::float_type);
3692 if (prog->OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
3693 /* Hand over gl_FragDepth. */
3694 assert(this->frag_depth.file != BAD_FILE);
3695 emit(MOV(sources[length], this->frag_depth));
3696 } else {
3697 /* Pass through the payload depth. */
3698 emit(MOV(sources[length],
3699 fs_reg(brw_vec8_grf(payload.source_depth_reg, 0))));
3700 }
3701 length++;
3702 }
3703
3704 if (payload.dest_depth_reg) {
3705 sources[length] = vgrf(glsl_type::float_type);
3706 emit(MOV(sources[length],
3707 fs_reg(brw_vec8_grf(payload.dest_depth_reg, 0))));
3708 length++;
3709 }
3710
3711 fs_inst *load;
3712 fs_inst *write;
3713 if (brw->gen >= 7) {
3714 /* Send from the GRF */
3715 fs_reg payload = fs_reg(GRF, -1, BRW_REGISTER_TYPE_F);
3716 load = emit(LOAD_PAYLOAD(payload, sources, length));
3717 payload.reg = alloc.allocate(load->regs_written);
3718 payload.width = dispatch_width;
3719 load->dst = payload;
3720 write = emit(FS_OPCODE_FB_WRITE, reg_undef, payload);
3721 write->base_mrf = -1;
3722 } else {
3723 /* Send from the MRF */
3724 load = emit(LOAD_PAYLOAD(fs_reg(MRF, 1, BRW_REGISTER_TYPE_F),
3725 sources, length));
3726 write = emit(FS_OPCODE_FB_WRITE);
3727 write->exec_size = dispatch_width;
3728 write->base_mrf = 1;
3729 }
3730
3731 write->mlen = load->regs_written;
3732 write->header_present = header_present;
3733 if (prog_data->uses_kill) {
3734 write->predicate = BRW_PREDICATE_NORMAL;
3735 write->flag_subreg = 1;
3736 }
3737 return write;
3738 }
3739
3740 void
3741 fs_visitor::emit_fb_writes()
3742 {
3743 assert(stage == MESA_SHADER_FRAGMENT);
3744 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
3745 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
3746
3747 fs_inst *inst = NULL;
3748 if (do_dual_src) {
3749 this->current_annotation = ralloc_asprintf(this->mem_ctx,
3750 "FB dual-source write");
3751 inst = emit_single_fb_write(this->outputs[0], this->dual_src_output,
3752 reg_undef, 4);
3753 inst->target = 0;
3754
3755 /* SIMD16 dual source blending requires to send two SIMD8 dual source
3756 * messages, where each message contains color data for 8 pixels. Color
3757 * data for the first group of pixels is stored in the "lower" half of
3758 * the color registers, so in SIMD16, the previous message did:
3759 * m + 0: r0
3760 * m + 1: g0
3761 * m + 2: b0
3762 * m + 3: a0
3763 *
3764 * Here goes the second message, which packs color data for the
3765 * remaining 8 pixels. Color data for these pixels is stored in the
3766 * "upper" half of the color registers, so we need to do:
3767 * m + 0: r1
3768 * m + 1: g1
3769 * m + 2: b1
3770 * m + 3: a1
3771 */
3772 if (dispatch_width == 16) {
3773 inst = emit_single_fb_write(this->outputs[0], this->dual_src_output,
3774 reg_undef, 4, true);
3775 inst->target = 0;
3776 }
3777
3778 prog_data->dual_src_blend = true;
3779 } else {
3780 for (int target = 0; target < key->nr_color_regions; target++) {
3781 /* Skip over outputs that weren't written. */
3782 if (this->outputs[target].file == BAD_FILE)
3783 continue;
3784
3785 this->current_annotation = ralloc_asprintf(this->mem_ctx,
3786 "FB write target %d",
3787 target);
3788 fs_reg src0_alpha;
3789 if (brw->gen >= 6 && key->replicate_alpha && target != 0)
3790 src0_alpha = offset(outputs[0], 3);
3791
3792 inst = emit_single_fb_write(this->outputs[target], reg_undef,
3793 src0_alpha,
3794 this->output_components[target]);
3795 inst->target = target;
3796 }
3797 }
3798
3799 if (inst == NULL) {
3800 /* Even if there's no color buffers enabled, we still need to send
3801 * alpha out the pipeline to our null renderbuffer to support
3802 * alpha-testing, alpha-to-coverage, and so on.
3803 */
3804 inst = emit_single_fb_write(reg_undef, reg_undef, reg_undef, 0);
3805 inst->target = 0;
3806 }
3807
3808 inst->eot = true;
3809 this->current_annotation = NULL;
3810 }
3811
3812 void
3813 fs_visitor::setup_uniform_clipplane_values()
3814 {
3815 gl_clip_plane *clip_planes = brw_select_clip_planes(ctx);
3816 const struct brw_vue_prog_key *key =
3817 (const struct brw_vue_prog_key *) this->key;
3818
3819 for (int i = 0; i < key->nr_userclip_plane_consts; i++) {
3820 this->userplane[i] = fs_reg(UNIFORM, uniforms);
3821 for (int j = 0; j < 4; ++j) {
3822 stage_prog_data->param[uniforms + j] =
3823 (gl_constant_value *) &clip_planes[i][j];
3824 }
3825 uniforms += 4;
3826 }
3827 }
3828
3829 void fs_visitor::compute_clip_distance()
3830 {
3831 struct brw_vue_prog_data *vue_prog_data =
3832 (struct brw_vue_prog_data *) prog_data;
3833 const struct brw_vue_prog_key *key =
3834 (const struct brw_vue_prog_key *) this->key;
3835
3836 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
3837 *
3838 * "If a linked set of shaders forming the vertex stage contains no
3839 * static write to gl_ClipVertex or gl_ClipDistance, but the
3840 * application has requested clipping against user clip planes through
3841 * the API, then the coordinate written to gl_Position is used for
3842 * comparison against the user clip planes."
3843 *
3844 * This function is only called if the shader didn't write to
3845 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
3846 * if the user wrote to it; otherwise we use gl_Position.
3847 */
3848
3849 gl_varying_slot clip_vertex = VARYING_SLOT_CLIP_VERTEX;
3850 if (!(vue_prog_data->vue_map.slots_valid & VARYING_BIT_CLIP_VERTEX))
3851 clip_vertex = VARYING_SLOT_POS;
3852
3853 /* If the clip vertex isn't written, skip this. Typically this means
3854 * the GS will set up clipping. */
3855 if (outputs[clip_vertex].file == BAD_FILE)
3856 return;
3857
3858 setup_uniform_clipplane_values();
3859
3860 current_annotation = "user clip distances";
3861
3862 this->outputs[VARYING_SLOT_CLIP_DIST0] = vgrf(glsl_type::vec4_type);
3863 this->outputs[VARYING_SLOT_CLIP_DIST1] = vgrf(glsl_type::vec4_type);
3864
3865 for (int i = 0; i < key->nr_userclip_plane_consts; i++) {
3866 fs_reg u = userplane[i];
3867 fs_reg output = outputs[VARYING_SLOT_CLIP_DIST0 + i / 4];
3868 output.reg_offset = i & 3;
3869
3870 emit(MUL(output, outputs[clip_vertex], u));
3871 for (int j = 1; j < 4; j++) {
3872 u.reg = userplane[i].reg + j;
3873 emit(MAD(output, output, offset(outputs[clip_vertex], j), u));
3874 }
3875 }
3876 }
3877
3878 void
3879 fs_visitor::emit_urb_writes()
3880 {
3881 int slot, urb_offset, length;
3882 struct brw_vs_prog_data *vs_prog_data =
3883 (struct brw_vs_prog_data *) prog_data;
3884 const struct brw_vs_prog_key *key =
3885 (const struct brw_vs_prog_key *) this->key;
3886 const GLbitfield64 psiz_mask =
3887 VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT | VARYING_BIT_PSIZ;
3888 const struct brw_vue_map *vue_map = &vs_prog_data->base.vue_map;
3889 bool flush;
3890 fs_reg sources[8];
3891
3892 /* Lower legacy ff and ClipVertex clipping to clip distances */
3893 if (key->base.userclip_active && !prog->UsesClipDistanceOut)
3894 compute_clip_distance();
3895
3896 /* If we don't have any valid slots to write, just do a minimal urb write
3897 * send to terminate the shader. */
3898 if (vue_map->slots_valid == 0) {
3899
3900 fs_reg payload = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
3901 fs_inst *inst = emit(MOV(payload, fs_reg(retype(brw_vec8_grf(1, 0),
3902 BRW_REGISTER_TYPE_UD))));
3903 inst->force_writemask_all = true;
3904
3905 inst = emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, payload);
3906 inst->eot = true;
3907 inst->mlen = 1;
3908 inst->offset = 1;
3909 return;
3910 }
3911
3912 length = 0;
3913 urb_offset = 0;
3914 flush = false;
3915 for (slot = 0; slot < vue_map->num_slots; slot++) {
3916 fs_reg reg, src, zero;
3917
3918 int varying = vue_map->slot_to_varying[slot];
3919 switch (varying) {
3920 case VARYING_SLOT_PSIZ:
3921
3922 /* The point size varying slot is the vue header and is always in the
3923 * vue map. But often none of the special varyings that live there
3924 * are written and in that case we can skip writing to the vue
3925 * header, provided the corresponding state properly clamps the
3926 * values further down the pipeline. */
3927 if ((vue_map->slots_valid & psiz_mask) == 0) {
3928 assert(length == 0);
3929 urb_offset++;
3930 break;
3931 }
3932
3933 zero = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
3934 emit(MOV(zero, fs_reg(0u)));
3935
3936 sources[length++] = zero;
3937 if (vue_map->slots_valid & VARYING_BIT_LAYER)
3938 sources[length++] = this->outputs[VARYING_SLOT_LAYER];
3939 else
3940 sources[length++] = zero;
3941
3942 if (vue_map->slots_valid & VARYING_BIT_VIEWPORT)
3943 sources[length++] = this->outputs[VARYING_SLOT_VIEWPORT];
3944 else
3945 sources[length++] = zero;
3946
3947 if (vue_map->slots_valid & VARYING_BIT_PSIZ)
3948 sources[length++] = this->outputs[VARYING_SLOT_PSIZ];
3949 else
3950 sources[length++] = zero;
3951 break;
3952
3953 case BRW_VARYING_SLOT_NDC:
3954 case VARYING_SLOT_EDGE:
3955 unreachable("unexpected scalar vs output");
3956 break;
3957
3958 case BRW_VARYING_SLOT_PAD:
3959 break;
3960
3961 default:
3962 /* gl_Position is always in the vue map, but isn't always written by
3963 * the shader. Other varyings (clip distances) get added to the vue
3964 * map but don't always get written. In those cases, the
3965 * corresponding this->output[] slot will be invalid we and can skip
3966 * the urb write for the varying. If we've already queued up a vue
3967 * slot for writing we flush a mlen 5 urb write, otherwise we just
3968 * advance the urb_offset.
3969 */
3970 if (this->outputs[varying].file == BAD_FILE) {
3971 if (length > 0)
3972 flush = true;
3973 else
3974 urb_offset++;
3975 break;
3976 }
3977
3978 if ((varying == VARYING_SLOT_COL0 ||
3979 varying == VARYING_SLOT_COL1 ||
3980 varying == VARYING_SLOT_BFC0 ||
3981 varying == VARYING_SLOT_BFC1) &&
3982 key->clamp_vertex_color) {
3983 /* We need to clamp these guys, so do a saturating MOV into a
3984 * temp register and use that for the payload.
3985 */
3986 for (int i = 0; i < 4; i++) {
3987 reg = fs_reg(GRF, alloc.allocate(1), outputs[varying].type);
3988 src = offset(this->outputs[varying], i);
3989 fs_inst *inst = emit(MOV(reg, src));
3990 inst->saturate = true;
3991 sources[length++] = reg;
3992 }
3993 } else {
3994 for (int i = 0; i < 4; i++)
3995 sources[length++] = offset(this->outputs[varying], i);
3996 }
3997 break;
3998 }
3999
4000 current_annotation = "URB write";
4001
4002 /* If we've queued up 8 registers of payload (2 VUE slots), if this is
4003 * the last slot or if we need to flush (see BAD_FILE varying case
4004 * above), emit a URB write send now to flush out the data.
4005 */
4006 int last = slot == vue_map->num_slots - 1;
4007 if (length == 8 || last)
4008 flush = true;
4009 if (flush) {
4010 fs_reg *payload_sources = ralloc_array(mem_ctx, fs_reg, length + 1);
4011 fs_reg payload = fs_reg(GRF, alloc.allocate(length + 1),
4012 BRW_REGISTER_TYPE_F);
4013
4014 /* We need WE_all on the MOV for the message header (the URB handles)
4015 * so do a MOV to a dummy register and set force_writemask_all on the
4016 * MOV. LOAD_PAYLOAD will preserve that.
4017 */
4018 fs_reg dummy = fs_reg(GRF, alloc.allocate(1),
4019 BRW_REGISTER_TYPE_UD);
4020 fs_inst *inst = emit(MOV(dummy, fs_reg(retype(brw_vec8_grf(1, 0),
4021 BRW_REGISTER_TYPE_UD))));
4022 inst->force_writemask_all = true;
4023 payload_sources[0] = dummy;
4024
4025 memcpy(&payload_sources[1], sources, length * sizeof sources[0]);
4026 emit(LOAD_PAYLOAD(payload, payload_sources, length + 1));
4027
4028 inst = emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, payload);
4029 inst->eot = last;
4030 inst->mlen = length + 1;
4031 inst->offset = urb_offset;
4032 urb_offset = slot + 1;
4033 length = 0;
4034 flush = false;
4035 }
4036 }
4037 }
4038
4039 void
4040 fs_visitor::resolve_ud_negate(fs_reg *reg)
4041 {
4042 if (reg->type != BRW_REGISTER_TYPE_UD ||
4043 !reg->negate)
4044 return;
4045
4046 fs_reg temp = vgrf(glsl_type::uint_type);
4047 emit(MOV(temp, *reg));
4048 *reg = temp;
4049 }
4050
4051 /**
4052 * Resolve the result of a Gen4-5 CMP instruction to a proper boolean.
4053 *
4054 * CMP on Gen4-5 only sets the LSB of the result; the rest are undefined.
4055 * If we need a proper boolean value, we have to fix it up to be 0 or ~0.
4056 */
4057 void
4058 fs_visitor::resolve_bool_comparison(ir_rvalue *rvalue, fs_reg *reg)
4059 {
4060 assert(brw->gen <= 5);
4061
4062 if (rvalue->type != glsl_type::bool_type)
4063 return;
4064
4065 fs_reg and_result = vgrf(glsl_type::bool_type);
4066 fs_reg neg_result = vgrf(glsl_type::bool_type);
4067 emit(AND(and_result, *reg, fs_reg(1)));
4068 emit(MOV(neg_result, negate(and_result)));
4069 *reg = neg_result;
4070 }
4071
4072 fs_visitor::fs_visitor(struct brw_context *brw,
4073 void *mem_ctx,
4074 const struct brw_wm_prog_key *key,
4075 struct brw_wm_prog_data *prog_data,
4076 struct gl_shader_program *shader_prog,
4077 struct gl_fragment_program *fp,
4078 unsigned dispatch_width)
4079 : backend_visitor(brw, shader_prog, &fp->Base, &prog_data->base,
4080 MESA_SHADER_FRAGMENT),
4081 reg_null_f(retype(brw_null_vec(dispatch_width), BRW_REGISTER_TYPE_F)),
4082 reg_null_d(retype(brw_null_vec(dispatch_width), BRW_REGISTER_TYPE_D)),
4083 reg_null_ud(retype(brw_null_vec(dispatch_width), BRW_REGISTER_TYPE_UD)),
4084 key(key), prog_data(&prog_data->base),
4085 dispatch_width(dispatch_width), promoted_constants(0)
4086 {
4087 this->mem_ctx = mem_ctx;
4088 init();
4089 }
4090
4091 fs_visitor::fs_visitor(struct brw_context *brw,
4092 void *mem_ctx,
4093 const struct brw_vs_prog_key *key,
4094 struct brw_vs_prog_data *prog_data,
4095 struct gl_shader_program *shader_prog,
4096 struct gl_vertex_program *cp,
4097 unsigned dispatch_width)
4098 : backend_visitor(brw, shader_prog, &cp->Base, &prog_data->base.base,
4099 MESA_SHADER_VERTEX),
4100 reg_null_f(retype(brw_null_vec(dispatch_width), BRW_REGISTER_TYPE_F)),
4101 reg_null_d(retype(brw_null_vec(dispatch_width), BRW_REGISTER_TYPE_D)),
4102 reg_null_ud(retype(brw_null_vec(dispatch_width), BRW_REGISTER_TYPE_UD)),
4103 key(key), prog_data(&prog_data->base.base),
4104 dispatch_width(dispatch_width), promoted_constants(0)
4105 {
4106 this->mem_ctx = mem_ctx;
4107 init();
4108 }
4109
4110 void
4111 fs_visitor::init()
4112 {
4113 switch (stage) {
4114 case MESA_SHADER_FRAGMENT:
4115 key_tex = &((const brw_wm_prog_key *) key)->tex;
4116 break;
4117 case MESA_SHADER_VERTEX:
4118 case MESA_SHADER_GEOMETRY:
4119 key_tex = &((const brw_vue_prog_key *) key)->tex;
4120 break;
4121 default:
4122 unreachable("unhandled shader stage");
4123 }
4124
4125 this->failed = false;
4126 this->simd16_unsupported = false;
4127 this->no16_msg = NULL;
4128 this->variable_ht = hash_table_ctor(0,
4129 hash_table_pointer_hash,
4130 hash_table_pointer_compare);
4131
4132 this->nir_locals = NULL;
4133 this->nir_globals = NULL;
4134
4135 memset(&this->payload, 0, sizeof(this->payload));
4136 memset(this->outputs, 0, sizeof(this->outputs));
4137 memset(this->output_components, 0, sizeof(this->output_components));
4138 this->source_depth_to_render_target = false;
4139 this->runtime_check_aads_emit = false;
4140 this->first_non_payload_grf = 0;
4141 this->max_grf = brw->gen >= 7 ? GEN7_MRF_HACK_START : BRW_MAX_GRF;
4142
4143 this->current_annotation = NULL;
4144 this->base_ir = NULL;
4145
4146 this->virtual_grf_start = NULL;
4147 this->virtual_grf_end = NULL;
4148 this->live_intervals = NULL;
4149 this->regs_live_at_ip = NULL;
4150
4151 this->uniforms = 0;
4152 this->last_scratch = 0;
4153 this->pull_constant_loc = NULL;
4154 this->push_constant_loc = NULL;
4155
4156 this->spilled_any_registers = false;
4157 this->do_dual_src = false;
4158
4159 if (dispatch_width == 8)
4160 this->param_size = rzalloc_array(mem_ctx, int, stage_prog_data->nr_params);
4161 }
4162
4163 fs_visitor::~fs_visitor()
4164 {
4165 hash_table_dtor(this->variable_ht);
4166 }