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