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