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