2 * Copyright © 2011 Intel Corporation
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:
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
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
26 #include "main/context.h"
27 #include "main/macros.h"
28 #include "program/prog_parameter.h"
29 #include "program/sampler.h"
34 vec4_instruction::vec4_instruction(vec4_visitor *v,
35 enum opcode opcode, dst_reg dst,
36 src_reg src0, src_reg src1, src_reg src2)
38 this->opcode = opcode;
43 this->ir = v->base_ir;
44 this->annotation = v->current_annotation;
48 vec4_visitor::emit(vec4_instruction *inst)
50 this->instructions.push_tail(inst);
56 vec4_visitor::emit_before(vec4_instruction *inst, vec4_instruction *new_inst)
58 new_inst->ir = inst->ir;
59 new_inst->annotation = inst->annotation;
61 inst->insert_before(new_inst);
67 vec4_visitor::emit(enum opcode opcode, dst_reg dst,
68 src_reg src0, src_reg src1, src_reg src2)
70 return emit(new(mem_ctx) vec4_instruction(this, opcode, dst,
76 vec4_visitor::emit(enum opcode opcode, dst_reg dst, src_reg src0, src_reg src1)
78 return emit(new(mem_ctx) vec4_instruction(this, opcode, dst, src0, src1));
82 vec4_visitor::emit(enum opcode opcode, dst_reg dst, src_reg src0)
84 return emit(new(mem_ctx) vec4_instruction(this, opcode, dst, src0));
88 vec4_visitor::emit(enum opcode opcode)
90 return emit(new(mem_ctx) vec4_instruction(this, opcode, dst_reg()));
95 vec4_visitor::op(dst_reg dst, src_reg src0) \
97 return new(mem_ctx) vec4_instruction(this, BRW_OPCODE_##op, dst, \
103 vec4_visitor::op(dst_reg dst, src_reg src0, src_reg src1) \
105 return new(mem_ctx) vec4_instruction(this, BRW_OPCODE_##op, dst, \
128 /** Gen4 predicated IF. */
130 vec4_visitor::IF(uint32_t predicate)
132 vec4_instruction *inst;
134 inst = new(mem_ctx) vec4_instruction(this, BRW_OPCODE_IF);
135 inst->predicate = predicate;
140 /** Gen6+ IF with embedded comparison. */
142 vec4_visitor::IF(src_reg src0, src_reg src1, uint32_t condition)
144 assert(intel->gen >= 6);
146 vec4_instruction *inst;
148 resolve_ud_negate(&src0);
149 resolve_ud_negate(&src1);
151 inst = new(mem_ctx) vec4_instruction(this, BRW_OPCODE_IF, dst_null_d(),
153 inst->conditional_mod = condition;
159 * CMP: Sets the low bit of the destination channels with the result
160 * of the comparison, while the upper bits are undefined, and updates
161 * the flag register with the packed 16 bits of the result.
164 vec4_visitor::CMP(dst_reg dst, src_reg src0, src_reg src1, uint32_t condition)
166 vec4_instruction *inst;
168 /* original gen4 does type conversion to the destination type
169 * before before comparison, producing garbage results for floating
172 if (intel->gen == 4) {
173 dst.type = src0.type;
174 if (dst.file == HW_REG)
175 dst.fixed_hw_reg.type = dst.type;
178 resolve_ud_negate(&src0);
179 resolve_ud_negate(&src1);
181 inst = new(mem_ctx) vec4_instruction(this, BRW_OPCODE_CMP, dst, src0, src1);
182 inst->conditional_mod = condition;
188 vec4_visitor::SCRATCH_READ(dst_reg dst, src_reg index)
190 vec4_instruction *inst;
192 inst = new(mem_ctx) vec4_instruction(this, VS_OPCODE_SCRATCH_READ,
201 vec4_visitor::SCRATCH_WRITE(dst_reg dst, src_reg src, src_reg index)
203 vec4_instruction *inst;
205 inst = new(mem_ctx) vec4_instruction(this, VS_OPCODE_SCRATCH_WRITE,
214 vec4_visitor::emit_dp(dst_reg dst, src_reg src0, src_reg src1, unsigned elements)
216 static enum opcode dot_opcodes[] = {
217 BRW_OPCODE_DP2, BRW_OPCODE_DP3, BRW_OPCODE_DP4
220 emit(dot_opcodes[elements - 2], dst, src0, src1);
224 vec4_visitor::fix_math_operand(src_reg src)
226 /* The gen6 math instruction ignores the source modifiers --
227 * swizzle, abs, negate, and at least some parts of the register
228 * region description.
230 * Rather than trying to enumerate all these cases, *always* expand the
231 * operand to a temp GRF for gen6.
233 * For gen7, keep the operand as-is, except if immediate, which gen7 still
237 if (intel->gen == 7 && src.file != IMM)
240 dst_reg expanded = dst_reg(this, glsl_type::vec4_type);
241 expanded.type = src.type;
242 emit(MOV(expanded, src));
243 return src_reg(expanded);
247 vec4_visitor::emit_math1_gen6(enum opcode opcode, dst_reg dst, src_reg src)
249 src = fix_math_operand(src);
251 if (dst.writemask != WRITEMASK_XYZW) {
252 /* The gen6 math instruction must be align1, so we can't do
255 dst_reg temp_dst = dst_reg(this, glsl_type::vec4_type);
257 emit(opcode, temp_dst, src);
259 emit(MOV(dst, src_reg(temp_dst)));
261 emit(opcode, dst, src);
266 vec4_visitor::emit_math1_gen4(enum opcode opcode, dst_reg dst, src_reg src)
268 vec4_instruction *inst = emit(opcode, dst, src);
274 vec4_visitor::emit_math(opcode opcode, dst_reg dst, src_reg src)
277 case SHADER_OPCODE_RCP:
278 case SHADER_OPCODE_RSQ:
279 case SHADER_OPCODE_SQRT:
280 case SHADER_OPCODE_EXP2:
281 case SHADER_OPCODE_LOG2:
282 case SHADER_OPCODE_SIN:
283 case SHADER_OPCODE_COS:
286 assert(!"not reached: bad math opcode");
290 if (intel->gen >= 6) {
291 return emit_math1_gen6(opcode, dst, src);
293 return emit_math1_gen4(opcode, dst, src);
298 vec4_visitor::emit_math2_gen6(enum opcode opcode,
299 dst_reg dst, src_reg src0, src_reg src1)
301 src0 = fix_math_operand(src0);
302 src1 = fix_math_operand(src1);
304 if (dst.writemask != WRITEMASK_XYZW) {
305 /* The gen6 math instruction must be align1, so we can't do
308 dst_reg temp_dst = dst_reg(this, glsl_type::vec4_type);
309 temp_dst.type = dst.type;
311 emit(opcode, temp_dst, src0, src1);
313 emit(MOV(dst, src_reg(temp_dst)));
315 emit(opcode, dst, src0, src1);
320 vec4_visitor::emit_math2_gen4(enum opcode opcode,
321 dst_reg dst, src_reg src0, src_reg src1)
323 vec4_instruction *inst = emit(opcode, dst, src0, src1);
329 vec4_visitor::emit_math(enum opcode opcode,
330 dst_reg dst, src_reg src0, src_reg src1)
333 case SHADER_OPCODE_POW:
334 case SHADER_OPCODE_INT_QUOTIENT:
335 case SHADER_OPCODE_INT_REMAINDER:
338 assert(!"not reached: unsupported binary math opcode");
342 if (intel->gen >= 6) {
343 return emit_math2_gen6(opcode, dst, src0, src1);
345 return emit_math2_gen4(opcode, dst, src0, src1);
350 vec4_visitor::visit_instructions(const exec_list *list)
352 foreach_list(node, list) {
353 ir_instruction *ir = (ir_instruction *)node;
362 type_size(const struct glsl_type *type)
367 switch (type->base_type) {
370 case GLSL_TYPE_FLOAT:
372 if (type->is_matrix()) {
373 return type->matrix_columns;
375 /* Regardless of size of vector, it gets a vec4. This is bad
376 * packing for things like floats, but otherwise arrays become a
377 * mess. Hopefully a later pass over the code can pack scalars
378 * down if appropriate.
382 case GLSL_TYPE_ARRAY:
383 assert(type->length > 0);
384 return type_size(type->fields.array) * type->length;
385 case GLSL_TYPE_STRUCT:
387 for (i = 0; i < type->length; i++) {
388 size += type_size(type->fields.structure[i].type);
391 case GLSL_TYPE_SAMPLER:
392 /* Samplers take up one slot in UNIFORMS[], but they're baked in
403 vec4_visitor::virtual_grf_alloc(int size)
405 if (virtual_grf_array_size <= virtual_grf_count) {
406 if (virtual_grf_array_size == 0)
407 virtual_grf_array_size = 16;
409 virtual_grf_array_size *= 2;
410 virtual_grf_sizes = reralloc(mem_ctx, virtual_grf_sizes, int,
411 virtual_grf_array_size);
412 virtual_grf_reg_map = reralloc(mem_ctx, virtual_grf_reg_map, int,
413 virtual_grf_array_size);
415 virtual_grf_reg_map[virtual_grf_count] = virtual_grf_reg_count;
416 virtual_grf_reg_count += size;
417 virtual_grf_sizes[virtual_grf_count] = size;
418 return virtual_grf_count++;
421 src_reg::src_reg(class vec4_visitor *v, const struct glsl_type *type)
426 this->reg = v->virtual_grf_alloc(type_size(type));
428 if (type->is_array() || type->is_record()) {
429 this->swizzle = BRW_SWIZZLE_NOOP;
431 this->swizzle = swizzle_for_size(type->vector_elements);
434 this->type = brw_type_for_base_type(type);
437 dst_reg::dst_reg(class vec4_visitor *v, const struct glsl_type *type)
442 this->reg = v->virtual_grf_alloc(type_size(type));
444 if (type->is_array() || type->is_record()) {
445 this->writemask = WRITEMASK_XYZW;
447 this->writemask = (1 << type->vector_elements) - 1;
450 this->type = brw_type_for_base_type(type);
453 /* Our support for uniforms is piggy-backed on the struct
454 * gl_fragment_program, because that's where the values actually
455 * get stored, rather than in some global gl_shader_program uniform
459 vec4_visitor::setup_uniform_values(int loc, const glsl_type *type)
461 unsigned int offset = 0;
462 float *values = &this->vp->Base.Parameters->ParameterValues[loc][0].f;
464 if (type->is_matrix()) {
465 const glsl_type *column = type->column_type();
467 for (unsigned int i = 0; i < type->matrix_columns; i++) {
468 offset += setup_uniform_values(loc + offset, column);
474 switch (type->base_type) {
475 case GLSL_TYPE_FLOAT:
479 for (unsigned int i = 0; i < type->vector_elements; i++) {
480 c->prog_data.param[this->uniforms * 4 + i] = &values[i];
483 /* Set up pad elements to get things aligned to a vec4 boundary. */
484 for (unsigned int i = type->vector_elements; i < 4; i++) {
485 static float zero = 0;
487 c->prog_data.param[this->uniforms * 4 + i] = &zero;
490 /* Track the size of this uniform vector, for future packing of
493 this->uniform_vector_size[this->uniforms] = type->vector_elements;
498 case GLSL_TYPE_STRUCT:
499 for (unsigned int i = 0; i < type->length; i++) {
500 offset += setup_uniform_values(loc + offset,
501 type->fields.structure[i].type);
505 case GLSL_TYPE_ARRAY:
506 for (unsigned int i = 0; i < type->length; i++) {
507 offset += setup_uniform_values(loc + offset, type->fields.array);
511 case GLSL_TYPE_SAMPLER:
512 /* The sampler takes up a slot, but we don't use any values from it. */
516 assert(!"not reached");
522 vec4_visitor::setup_uniform_clipplane_values()
524 gl_clip_plane *clip_planes = brw_select_clip_planes(ctx);
526 if (intel->gen < 6) {
527 /* Pre-Gen6, we compact clip planes. For example, if the user
528 * enables just clip planes 0, 1, and 3, we will enable clip planes
529 * 0, 1, and 2 in the hardware, and we'll move clip plane 3 to clip
530 * plane 2. This simplifies the implementation of the Gen6 clip
533 int compacted_clipplane_index = 0;
534 for (int i = 0; i < MAX_CLIP_PLANES; ++i) {
535 if (!(c->key.userclip_planes_enabled_gen_4_5 & (1 << i)))
538 this->uniform_vector_size[this->uniforms] = 4;
539 this->userplane[compacted_clipplane_index] = dst_reg(UNIFORM, this->uniforms);
540 this->userplane[compacted_clipplane_index].type = BRW_REGISTER_TYPE_F;
541 for (int j = 0; j < 4; ++j) {
542 c->prog_data.param[this->uniforms * 4 + j] = &clip_planes[i][j];
544 ++compacted_clipplane_index;
548 /* In Gen6 and later, we don't compact clip planes, because this
549 * simplifies the implementation of gl_ClipDistance.
551 for (int i = 0; i < c->key.nr_userclip_plane_consts; ++i) {
552 this->uniform_vector_size[this->uniforms] = 4;
553 this->userplane[i] = dst_reg(UNIFORM, this->uniforms);
554 this->userplane[i].type = BRW_REGISTER_TYPE_F;
555 for (int j = 0; j < 4; ++j) {
556 c->prog_data.param[this->uniforms * 4 + j] = &clip_planes[i][j];
563 /* Our support for builtin uniforms is even scarier than non-builtin.
564 * It sits on top of the PROG_STATE_VAR parameters that are
565 * automatically updated from GL context state.
568 vec4_visitor::setup_builtin_uniform_values(ir_variable *ir)
570 const ir_state_slot *const slots = ir->state_slots;
571 assert(ir->state_slots != NULL);
573 for (unsigned int i = 0; i < ir->num_state_slots; i++) {
574 /* This state reference has already been setup by ir_to_mesa,
575 * but we'll get the same index back here. We can reference
576 * ParameterValues directly, since unlike brw_fs.cpp, we never
577 * add new state references during compile.
579 int index = _mesa_add_state_reference(this->vp->Base.Parameters,
580 (gl_state_index *)slots[i].tokens);
581 float *values = &this->vp->Base.Parameters->ParameterValues[index][0].f;
583 this->uniform_vector_size[this->uniforms] = 0;
584 /* Add each of the unique swizzled channels of the element.
585 * This will end up matching the size of the glsl_type of this field.
588 for (unsigned int j = 0; j < 4; j++) {
589 int swiz = GET_SWZ(slots[i].swizzle, j);
592 c->prog_data.param[this->uniforms * 4 + j] = &values[swiz];
593 if (swiz <= last_swiz)
594 this->uniform_vector_size[this->uniforms]++;
601 vec4_visitor::variable_storage(ir_variable *var)
603 return (dst_reg *)hash_table_find(this->variable_ht, var);
607 vec4_visitor::emit_bool_to_cond_code(ir_rvalue *ir, uint32_t *predicate)
609 ir_expression *expr = ir->as_expression();
611 *predicate = BRW_PREDICATE_NORMAL;
615 vec4_instruction *inst;
617 assert(expr->get_num_operands() <= 2);
618 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
619 expr->operands[i]->accept(this);
620 op[i] = this->result;
622 resolve_ud_negate(&op[i]);
625 switch (expr->operation) {
626 case ir_unop_logic_not:
627 inst = emit(AND(dst_null_d(), op[0], src_reg(1)));
628 inst->conditional_mod = BRW_CONDITIONAL_Z;
631 case ir_binop_logic_xor:
632 inst = emit(XOR(dst_null_d(), op[0], op[1]));
633 inst->conditional_mod = BRW_CONDITIONAL_NZ;
636 case ir_binop_logic_or:
637 inst = emit(OR(dst_null_d(), op[0], op[1]));
638 inst->conditional_mod = BRW_CONDITIONAL_NZ;
641 case ir_binop_logic_and:
642 inst = emit(AND(dst_null_d(), op[0], op[1]));
643 inst->conditional_mod = BRW_CONDITIONAL_NZ;
647 if (intel->gen >= 6) {
648 emit(CMP(dst_null_d(), op[0], src_reg(0.0f), BRW_CONDITIONAL_NZ));
650 inst = emit(MOV(dst_null_f(), op[0]));
651 inst->conditional_mod = BRW_CONDITIONAL_NZ;
656 if (intel->gen >= 6) {
657 emit(CMP(dst_null_d(), op[0], src_reg(0), BRW_CONDITIONAL_NZ));
659 inst = emit(MOV(dst_null_d(), op[0]));
660 inst->conditional_mod = BRW_CONDITIONAL_NZ;
664 case ir_binop_all_equal:
665 inst = emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_Z));
666 *predicate = BRW_PREDICATE_ALIGN16_ALL4H;
669 case ir_binop_any_nequal:
670 inst = emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_NZ));
671 *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
675 inst = emit(CMP(dst_null_d(), op[0], src_reg(0), BRW_CONDITIONAL_NZ));
676 *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
679 case ir_binop_greater:
680 case ir_binop_gequal:
682 case ir_binop_lequal:
684 case ir_binop_nequal:
685 emit(CMP(dst_null_d(), op[0], op[1],
686 brw_conditional_for_comparison(expr->operation)));
690 assert(!"not reached");
698 resolve_ud_negate(&this->result);
700 if (intel->gen >= 6) {
701 vec4_instruction *inst = emit(AND(dst_null_d(),
702 this->result, src_reg(1)));
703 inst->conditional_mod = BRW_CONDITIONAL_NZ;
705 vec4_instruction *inst = emit(MOV(dst_null_d(), this->result));
706 inst->conditional_mod = BRW_CONDITIONAL_NZ;
711 * Emit a gen6 IF statement with the comparison folded into the IF
715 vec4_visitor::emit_if_gen6(ir_if *ir)
717 ir_expression *expr = ir->condition->as_expression();
723 assert(expr->get_num_operands() <= 2);
724 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
725 expr->operands[i]->accept(this);
726 op[i] = this->result;
729 switch (expr->operation) {
730 case ir_unop_logic_not:
731 emit(IF(op[0], src_reg(0), BRW_CONDITIONAL_Z));
734 case ir_binop_logic_xor:
735 emit(IF(op[0], op[1], BRW_CONDITIONAL_NZ));
738 case ir_binop_logic_or:
739 temp = dst_reg(this, glsl_type::bool_type);
740 emit(OR(temp, op[0], op[1]));
741 emit(IF(src_reg(temp), src_reg(0), BRW_CONDITIONAL_NZ));
744 case ir_binop_logic_and:
745 temp = dst_reg(this, glsl_type::bool_type);
746 emit(AND(temp, op[0], op[1]));
747 emit(IF(src_reg(temp), src_reg(0), BRW_CONDITIONAL_NZ));
751 emit(IF(op[0], src_reg(0), BRW_CONDITIONAL_NZ));
755 emit(IF(op[0], src_reg(0), BRW_CONDITIONAL_NZ));
758 case ir_binop_greater:
759 case ir_binop_gequal:
761 case ir_binop_lequal:
763 case ir_binop_nequal:
764 emit(IF(op[0], op[1],
765 brw_conditional_for_comparison(expr->operation)));
768 case ir_binop_all_equal:
769 emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_Z));
770 emit(IF(BRW_PREDICATE_ALIGN16_ALL4H));
773 case ir_binop_any_nequal:
774 emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_NZ));
775 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H));
779 emit(CMP(dst_null_d(), op[0], src_reg(0), BRW_CONDITIONAL_NZ));
780 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H));
784 assert(!"not reached");
785 emit(IF(op[0], src_reg(0), BRW_CONDITIONAL_NZ));
791 ir->condition->accept(this);
793 emit(IF(this->result, src_reg(0), BRW_CONDITIONAL_NZ));
797 with_writemask(dst_reg const & r, int mask)
800 result.writemask = mask;
805 vec4_visitor::emit_attribute_fixups()
807 dst_reg sign_recovery_shift;
808 dst_reg normalize_factor;
809 dst_reg es3_normalize_factor;
811 for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
812 if (prog_data->inputs_read & BITFIELD64_BIT(i)) {
813 uint8_t wa_flags = c->key.gl_attrib_wa_flags[i];
814 dst_reg reg(ATTR, i);
816 reg_d.type = BRW_REGISTER_TYPE_D;
817 dst_reg reg_ud = reg;
818 reg_ud.type = BRW_REGISTER_TYPE_UD;
820 /* Do GL_FIXED rescaling for GLES2.0. Our GL_FIXED attributes
821 * come in as floating point conversions of the integer values.
823 if (wa_flags & BRW_ATTRIB_WA_COMPONENT_MASK) {
825 dst.type = brw_type_for_base_type(glsl_type::vec4_type);
826 dst.writemask = (1 << (wa_flags & BRW_ATTRIB_WA_COMPONENT_MASK)) - 1;
827 emit(MUL(dst, src_reg(dst), src_reg(1.0f / 65536.0f)));
830 /* Do sign recovery for 2101010 formats if required. */
831 if (wa_flags & BRW_ATTRIB_WA_SIGN) {
832 if (sign_recovery_shift.file == BAD_FILE) {
833 /* shift constant: <22,22,22,30> */
834 sign_recovery_shift = dst_reg(this, glsl_type::uvec4_type);
835 emit(MOV(with_writemask(sign_recovery_shift, WRITEMASK_XYZ), src_reg(22u)));
836 emit(MOV(with_writemask(sign_recovery_shift, WRITEMASK_W), src_reg(30u)));
839 emit(SHL(reg_ud, src_reg(reg_ud), src_reg(sign_recovery_shift)));
840 emit(ASR(reg_d, src_reg(reg_d), src_reg(sign_recovery_shift)));
843 /* Apply BGRA swizzle if required. */
844 if (wa_flags & BRW_ATTRIB_WA_BGRA) {
845 src_reg temp = src_reg(reg);
846 temp.swizzle = BRW_SWIZZLE4(2,1,0,3);
847 emit(MOV(reg, temp));
850 if (wa_flags & BRW_ATTRIB_WA_NORMALIZE) {
851 /* ES 3.0 has different rules for converting signed normalized
852 * fixed-point numbers than desktop GL.
854 if (_mesa_is_gles3(ctx) && (wa_flags & BRW_ATTRIB_WA_SIGN)) {
855 /* According to equation 2.2 of the ES 3.0 specification,
856 * signed normalization conversion is done by:
858 * f = c / (2^(b-1)-1)
860 if (es3_normalize_factor.file == BAD_FILE) {
861 /* mul constant: 1 / (2^(b-1) - 1) */
862 es3_normalize_factor = dst_reg(this, glsl_type::vec4_type);
863 emit(MOV(with_writemask(es3_normalize_factor, WRITEMASK_XYZ),
864 src_reg(1.0f / ((1<<9) - 1))));
865 emit(MOV(with_writemask(es3_normalize_factor, WRITEMASK_W),
866 src_reg(1.0f / ((1<<1) - 1))));
870 dst.type = brw_type_for_base_type(glsl_type::vec4_type);
871 emit(MOV(dst, src_reg(reg_d)));
872 emit(MUL(dst, src_reg(dst), src_reg(es3_normalize_factor)));
873 emit_minmax(BRW_CONDITIONAL_G, dst, src_reg(dst), src_reg(-1.0f));
875 /* The following equations are from the OpenGL 3.2 specification:
877 * 2.1 unsigned normalization
880 * 2.2 signed normalization
883 * Both of these share a common divisor, which is represented by
884 * "normalize_factor" in the code below.
886 if (normalize_factor.file == BAD_FILE) {
887 /* 1 / (2^b - 1) for b=<10,10,10,2> */
888 normalize_factor = dst_reg(this, glsl_type::vec4_type);
889 emit(MOV(with_writemask(normalize_factor, WRITEMASK_XYZ),
890 src_reg(1.0f / ((1<<10) - 1))));
891 emit(MOV(with_writemask(normalize_factor, WRITEMASK_W),
892 src_reg(1.0f / ((1<<2) - 1))));
896 dst.type = brw_type_for_base_type(glsl_type::vec4_type);
897 emit(MOV(dst, src_reg((wa_flags & BRW_ATTRIB_WA_SIGN) ? reg_d : reg_ud)));
899 /* For signed normalization, we want the numerator to be 2c+1. */
900 if (wa_flags & BRW_ATTRIB_WA_SIGN) {
901 emit(MUL(dst, src_reg(dst), src_reg(2.0f)));
902 emit(ADD(dst, src_reg(dst), src_reg(1.0f)));
905 emit(MUL(dst, src_reg(dst), src_reg(normalize_factor)));
909 if (wa_flags & BRW_ATTRIB_WA_SCALE) {
911 dst.type = brw_type_for_base_type(glsl_type::vec4_type);
912 emit(MOV(dst, src_reg((wa_flags & BRW_ATTRIB_WA_SIGN) ? reg_d : reg_ud)));
919 vec4_visitor::visit(ir_variable *ir)
923 if (variable_storage(ir))
928 reg = new(mem_ctx) dst_reg(ATTR, ir->location);
932 reg = new(mem_ctx) dst_reg(this, ir->type);
934 for (int i = 0; i < type_size(ir->type); i++) {
935 output_reg[ir->location + i] = *reg;
936 output_reg[ir->location + i].reg_offset = i;
937 output_reg[ir->location + i].type =
938 brw_type_for_base_type(ir->type->get_scalar_type());
939 output_reg_annotation[ir->location + i] = ir->name;
944 case ir_var_temporary:
945 reg = new(mem_ctx) dst_reg(this, ir->type);
949 reg = new(this->mem_ctx) dst_reg(UNIFORM, this->uniforms);
951 /* Thanks to the lower_ubo_reference pass, we will see only
952 * ir_binop_ubo_load expressions and not ir_dereference_variable for UBO
953 * variables, so no need for them to be in variable_ht.
955 if (ir->uniform_block != -1)
958 /* Track how big the whole uniform variable is, in case we need to put a
959 * copy of its data into pull constants for array access.
961 this->uniform_size[this->uniforms] = type_size(ir->type);
963 if (!strncmp(ir->name, "gl_", 3)) {
964 setup_builtin_uniform_values(ir);
966 setup_uniform_values(ir->location, ir->type);
970 case ir_var_system_value:
971 /* VertexID is stored by the VF as the last vertex element, but
972 * we don't represent it with a flag in inputs_read, so we call
973 * it VERT_ATTRIB_MAX, which setup_attributes() picks up on.
975 reg = new(mem_ctx) dst_reg(ATTR, VERT_ATTRIB_MAX);
976 prog_data->uses_vertexid = true;
978 switch (ir->location) {
979 case SYSTEM_VALUE_VERTEX_ID:
980 reg->writemask = WRITEMASK_X;
982 case SYSTEM_VALUE_INSTANCE_ID:
983 reg->writemask = WRITEMASK_Y;
986 assert(!"not reached");
992 assert(!"not reached");
995 reg->type = brw_type_for_base_type(ir->type);
996 hash_table_insert(this->variable_ht, reg, ir);
1000 vec4_visitor::visit(ir_loop *ir)
1004 /* We don't want debugging output to print the whole body of the
1005 * loop as the annotation.
1007 this->base_ir = NULL;
1009 if (ir->counter != NULL) {
1010 this->base_ir = ir->counter;
1011 ir->counter->accept(this);
1012 counter = *(variable_storage(ir->counter));
1014 if (ir->from != NULL) {
1015 this->base_ir = ir->from;
1016 ir->from->accept(this);
1018 emit(MOV(counter, this->result));
1022 emit(BRW_OPCODE_DO);
1025 this->base_ir = ir->to;
1026 ir->to->accept(this);
1028 emit(CMP(dst_null_d(), src_reg(counter), this->result,
1029 brw_conditional_for_comparison(ir->cmp)));
1031 vec4_instruction *inst = emit(BRW_OPCODE_BREAK);
1032 inst->predicate = BRW_PREDICATE_NORMAL;
1035 visit_instructions(&ir->body_instructions);
1038 if (ir->increment) {
1039 this->base_ir = ir->increment;
1040 ir->increment->accept(this);
1041 emit(ADD(counter, src_reg(counter), this->result));
1044 emit(BRW_OPCODE_WHILE);
1048 vec4_visitor::visit(ir_loop_jump *ir)
1051 case ir_loop_jump::jump_break:
1052 emit(BRW_OPCODE_BREAK);
1054 case ir_loop_jump::jump_continue:
1055 emit(BRW_OPCODE_CONTINUE);
1062 vec4_visitor::visit(ir_function_signature *ir)
1069 vec4_visitor::visit(ir_function *ir)
1071 /* Ignore function bodies other than main() -- we shouldn't see calls to
1072 * them since they should all be inlined.
1074 if (strcmp(ir->name, "main") == 0) {
1075 const ir_function_signature *sig;
1078 sig = ir->matching_signature(&empty);
1082 visit_instructions(&sig->body);
1087 vec4_visitor::try_emit_sat(ir_expression *ir)
1089 ir_rvalue *sat_src = ir->as_rvalue_to_saturate();
1093 sat_src->accept(this);
1094 src_reg src = this->result;
1096 this->result = src_reg(this, ir->type);
1097 vec4_instruction *inst;
1098 inst = emit(MOV(dst_reg(this->result), src));
1099 inst->saturate = true;
1105 vec4_visitor::emit_bool_comparison(unsigned int op,
1106 dst_reg dst, src_reg src0, src_reg src1)
1108 /* original gen4 does destination conversion before comparison. */
1110 dst.type = src0.type;
1112 emit(CMP(dst, src0, src1, brw_conditional_for_comparison(op)));
1114 dst.type = BRW_REGISTER_TYPE_D;
1115 emit(AND(dst, src_reg(dst), src_reg(0x1)));
1119 vec4_visitor::emit_minmax(uint32_t conditionalmod, dst_reg dst,
1120 src_reg src0, src_reg src1)
1122 vec4_instruction *inst;
1124 if (intel->gen >= 6) {
1125 inst = emit(BRW_OPCODE_SEL, dst, src0, src1);
1126 inst->conditional_mod = conditionalmod;
1128 emit(CMP(dst, src0, src1, conditionalmod));
1130 inst = emit(BRW_OPCODE_SEL, dst, src0, src1);
1131 inst->predicate = BRW_PREDICATE_NORMAL;
1136 vec4_visitor::visit(ir_expression *ir)
1138 unsigned int operand;
1139 src_reg op[Elements(ir->operands)];
1142 vec4_instruction *inst;
1144 if (try_emit_sat(ir))
1147 for (operand = 0; operand < ir->get_num_operands(); operand++) {
1148 this->result.file = BAD_FILE;
1149 ir->operands[operand]->accept(this);
1150 if (this->result.file == BAD_FILE) {
1151 printf("Failed to get tree for expression operand:\n");
1152 ir->operands[operand]->print();
1155 op[operand] = this->result;
1157 /* Matrix expression operands should have been broken down to vector
1158 * operations already.
1160 assert(!ir->operands[operand]->type->is_matrix());
1163 int vector_elements = ir->operands[0]->type->vector_elements;
1164 if (ir->operands[1]) {
1165 vector_elements = MAX2(vector_elements,
1166 ir->operands[1]->type->vector_elements);
1169 this->result.file = BAD_FILE;
1171 /* Storage for our result. Ideally for an assignment we'd be using
1172 * the actual storage for the result here, instead.
1174 result_src = src_reg(this, ir->type);
1175 /* convenience for the emit functions below. */
1176 result_dst = dst_reg(result_src);
1177 /* If nothing special happens, this is the result. */
1178 this->result = result_src;
1179 /* Limit writes to the channels that will be used by result_src later.
1180 * This does limit this temp's use as a temporary for multi-instruction
1183 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
1185 switch (ir->operation) {
1186 case ir_unop_logic_not:
1187 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
1188 * ones complement of the whole register, not just bit 0.
1190 emit(XOR(result_dst, op[0], src_reg(1)));
1193 op[0].negate = !op[0].negate;
1194 this->result = op[0];
1198 op[0].negate = false;
1199 this->result = op[0];
1203 emit(MOV(result_dst, src_reg(0.0f)));
1205 emit(CMP(dst_null_d(), op[0], src_reg(0.0f), BRW_CONDITIONAL_G));
1206 inst = emit(MOV(result_dst, src_reg(1.0f)));
1207 inst->predicate = BRW_PREDICATE_NORMAL;
1209 emit(CMP(dst_null_d(), op[0], src_reg(0.0f), BRW_CONDITIONAL_L));
1210 inst = emit(MOV(result_dst, src_reg(-1.0f)));
1211 inst->predicate = BRW_PREDICATE_NORMAL;
1216 emit_math(SHADER_OPCODE_RCP, result_dst, op[0]);
1220 emit_math(SHADER_OPCODE_EXP2, result_dst, op[0]);
1223 emit_math(SHADER_OPCODE_LOG2, result_dst, op[0]);
1227 assert(!"not reached: should be handled by ir_explog_to_explog2");
1230 case ir_unop_sin_reduced:
1231 emit_math(SHADER_OPCODE_SIN, result_dst, op[0]);
1234 case ir_unop_cos_reduced:
1235 emit_math(SHADER_OPCODE_COS, result_dst, op[0]);
1240 assert(!"derivatives not valid in vertex shader");
1244 assert(!"not reached: should be handled by lower_noise");
1248 emit(ADD(result_dst, op[0], op[1]));
1251 assert(!"not reached: should be handled by ir_sub_to_add_neg");
1255 if (ir->type->is_integer()) {
1256 /* For integer multiplication, the MUL uses the low 16 bits
1257 * of one of the operands (src0 on gen6, src1 on gen7). The
1258 * MACH accumulates in the contribution of the upper 16 bits
1261 * FINISHME: Emit just the MUL if we know an operand is small
1264 struct brw_reg acc = retype(brw_acc_reg(), BRW_REGISTER_TYPE_D);
1266 emit(MUL(acc, op[0], op[1]));
1267 emit(MACH(dst_null_d(), op[0], op[1]));
1268 emit(MOV(result_dst, src_reg(acc)));
1270 emit(MUL(result_dst, op[0], op[1]));
1274 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
1275 assert(ir->type->is_integer());
1276 emit_math(SHADER_OPCODE_INT_QUOTIENT, result_dst, op[0], op[1]);
1279 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
1280 assert(ir->type->is_integer());
1281 emit_math(SHADER_OPCODE_INT_REMAINDER, result_dst, op[0], op[1]);
1285 case ir_binop_greater:
1286 case ir_binop_lequal:
1287 case ir_binop_gequal:
1288 case ir_binop_equal:
1289 case ir_binop_nequal: {
1290 emit(CMP(result_dst, op[0], op[1],
1291 brw_conditional_for_comparison(ir->operation)));
1292 emit(AND(result_dst, result_src, src_reg(0x1)));
1296 case ir_binop_all_equal:
1297 /* "==" operator producing a scalar boolean. */
1298 if (ir->operands[0]->type->is_vector() ||
1299 ir->operands[1]->type->is_vector()) {
1300 emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_Z));
1301 emit(MOV(result_dst, src_reg(0)));
1302 inst = emit(MOV(result_dst, src_reg(1)));
1303 inst->predicate = BRW_PREDICATE_ALIGN16_ALL4H;
1305 emit(CMP(result_dst, op[0], op[1], BRW_CONDITIONAL_Z));
1306 emit(AND(result_dst, result_src, src_reg(0x1)));
1309 case ir_binop_any_nequal:
1310 /* "!=" operator producing a scalar boolean. */
1311 if (ir->operands[0]->type->is_vector() ||
1312 ir->operands[1]->type->is_vector()) {
1313 emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_NZ));
1315 emit(MOV(result_dst, src_reg(0)));
1316 inst = emit(MOV(result_dst, src_reg(1)));
1317 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1319 emit(CMP(result_dst, op[0], op[1], BRW_CONDITIONAL_NZ));
1320 emit(AND(result_dst, result_src, src_reg(0x1)));
1325 emit(CMP(dst_null_d(), op[0], src_reg(0), BRW_CONDITIONAL_NZ));
1326 emit(MOV(result_dst, src_reg(0)));
1328 inst = emit(MOV(result_dst, src_reg(1)));
1329 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1332 case ir_binop_logic_xor:
1333 emit(XOR(result_dst, op[0], op[1]));
1336 case ir_binop_logic_or:
1337 emit(OR(result_dst, op[0], op[1]));
1340 case ir_binop_logic_and:
1341 emit(AND(result_dst, op[0], op[1]));
1345 assert(ir->operands[0]->type->is_vector());
1346 assert(ir->operands[0]->type == ir->operands[1]->type);
1347 emit_dp(result_dst, op[0], op[1], ir->operands[0]->type->vector_elements);
1351 emit_math(SHADER_OPCODE_SQRT, result_dst, op[0]);
1354 emit_math(SHADER_OPCODE_RSQ, result_dst, op[0]);
1357 case ir_unop_bitcast_i2f:
1358 case ir_unop_bitcast_u2f:
1359 this->result = op[0];
1360 this->result.type = BRW_REGISTER_TYPE_F;
1363 case ir_unop_bitcast_f2i:
1364 this->result = op[0];
1365 this->result.type = BRW_REGISTER_TYPE_D;
1368 case ir_unop_bitcast_f2u:
1369 this->result = op[0];
1370 this->result.type = BRW_REGISTER_TYPE_UD;
1381 emit(MOV(result_dst, op[0]));
1385 emit(CMP(result_dst, op[0], src_reg(0.0f), BRW_CONDITIONAL_NZ));
1386 emit(AND(result_dst, result_src, src_reg(1)));
1391 emit(RNDZ(result_dst, op[0]));
1394 op[0].negate = !op[0].negate;
1395 inst = emit(RNDD(result_dst, op[0]));
1396 this->result.negate = true;
1399 inst = emit(RNDD(result_dst, op[0]));
1402 inst = emit(FRC(result_dst, op[0]));
1404 case ir_unop_round_even:
1405 emit(RNDE(result_dst, op[0]));
1409 emit_minmax(BRW_CONDITIONAL_L, result_dst, op[0], op[1]);
1412 emit_minmax(BRW_CONDITIONAL_G, result_dst, op[0], op[1]);
1416 emit_math(SHADER_OPCODE_POW, result_dst, op[0], op[1]);
1419 case ir_unop_bit_not:
1420 inst = emit(NOT(result_dst, op[0]));
1422 case ir_binop_bit_and:
1423 inst = emit(AND(result_dst, op[0], op[1]));
1425 case ir_binop_bit_xor:
1426 inst = emit(XOR(result_dst, op[0], op[1]));
1428 case ir_binop_bit_or:
1429 inst = emit(OR(result_dst, op[0], op[1]));
1432 case ir_binop_lshift:
1433 inst = emit(SHL(result_dst, op[0], op[1]));
1436 case ir_binop_rshift:
1437 if (ir->type->base_type == GLSL_TYPE_INT)
1438 inst = emit(ASR(result_dst, op[0], op[1]));
1440 inst = emit(SHR(result_dst, op[0], op[1]));
1443 case ir_binop_ubo_load: {
1444 ir_constant *uniform_block = ir->operands[0]->as_constant();
1445 ir_constant *const_offset_ir = ir->operands[1]->as_constant();
1446 unsigned const_offset = const_offset_ir ? const_offset_ir->value.u[0] : 0;
1447 src_reg offset = op[1];
1449 /* Now, load the vector from that offset. */
1450 assert(ir->type->is_vector() || ir->type->is_scalar());
1452 src_reg packed_consts = src_reg(this, glsl_type::vec4_type);
1453 packed_consts.type = result.type;
1454 src_reg surf_index =
1455 src_reg(SURF_INDEX_VS_UBO(uniform_block->value.u[0]));
1456 if (const_offset_ir) {
1457 offset = src_reg(const_offset / 16);
1459 emit(SHR(dst_reg(offset), offset, src_reg(4)));
1462 vec4_instruction *pull =
1463 emit(new(mem_ctx) vec4_instruction(this,
1464 VS_OPCODE_PULL_CONSTANT_LOAD,
1465 dst_reg(packed_consts),
1468 pull->base_mrf = 14;
1471 packed_consts.swizzle = swizzle_for_size(ir->type->vector_elements);
1472 packed_consts.swizzle += BRW_SWIZZLE4(const_offset % 16 / 4,
1473 const_offset % 16 / 4,
1474 const_offset % 16 / 4,
1475 const_offset % 16 / 4);
1477 /* UBO bools are any nonzero int. We store bools as either 0 or 1. */
1478 if (ir->type->base_type == GLSL_TYPE_BOOL) {
1479 emit(CMP(result_dst, packed_consts, src_reg(0u),
1480 BRW_CONDITIONAL_NZ));
1481 emit(AND(result_dst, result, src_reg(0x1)));
1483 emit(MOV(result_dst, packed_consts));
1488 case ir_quadop_vector:
1489 assert(!"not reached: should be handled by lower_quadop_vector");
1496 vec4_visitor::visit(ir_swizzle *ir)
1502 /* Note that this is only swizzles in expressions, not those on the left
1503 * hand side of an assignment, which do write masking. See ir_assignment
1507 ir->val->accept(this);
1509 assert(src.file != BAD_FILE);
1511 for (i = 0; i < ir->type->vector_elements; i++) {
1514 swizzle[i] = BRW_GET_SWZ(src.swizzle, ir->mask.x);
1517 swizzle[i] = BRW_GET_SWZ(src.swizzle, ir->mask.y);
1520 swizzle[i] = BRW_GET_SWZ(src.swizzle, ir->mask.z);
1523 swizzle[i] = BRW_GET_SWZ(src.swizzle, ir->mask.w);
1527 for (; i < 4; i++) {
1528 /* Replicate the last channel out. */
1529 swizzle[i] = swizzle[ir->type->vector_elements - 1];
1532 src.swizzle = BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
1538 vec4_visitor::visit(ir_dereference_variable *ir)
1540 const struct glsl_type *type = ir->type;
1541 dst_reg *reg = variable_storage(ir->var);
1544 fail("Failed to find variable storage for %s\n", ir->var->name);
1545 this->result = src_reg(brw_null_reg());
1549 this->result = src_reg(*reg);
1551 /* System values get their swizzle from the dst_reg writemask */
1552 if (ir->var->mode == ir_var_system_value)
1555 if (type->is_scalar() || type->is_vector() || type->is_matrix())
1556 this->result.swizzle = swizzle_for_size(type->vector_elements);
1560 vec4_visitor::visit(ir_dereference_array *ir)
1562 ir_constant *constant_index;
1564 int element_size = type_size(ir->type);
1566 constant_index = ir->array_index->constant_expression_value();
1568 ir->array->accept(this);
1571 if (constant_index) {
1572 src.reg_offset += constant_index->value.i[0] * element_size;
1574 /* Variable index array dereference. It eats the "vec4" of the
1575 * base of the array and an index that offsets the Mesa register
1578 ir->array_index->accept(this);
1582 if (element_size == 1) {
1583 index_reg = this->result;
1585 index_reg = src_reg(this, glsl_type::int_type);
1587 emit(MUL(dst_reg(index_reg), this->result, src_reg(element_size)));
1591 src_reg temp = src_reg(this, glsl_type::int_type);
1593 emit(ADD(dst_reg(temp), *src.reladdr, index_reg));
1598 src.reladdr = ralloc(mem_ctx, src_reg);
1599 memcpy(src.reladdr, &index_reg, sizeof(index_reg));
1602 /* If the type is smaller than a vec4, replicate the last channel out. */
1603 if (ir->type->is_scalar() || ir->type->is_vector() || ir->type->is_matrix())
1604 src.swizzle = swizzle_for_size(ir->type->vector_elements);
1606 src.swizzle = BRW_SWIZZLE_NOOP;
1607 src.type = brw_type_for_base_type(ir->type);
1613 vec4_visitor::visit(ir_dereference_record *ir)
1616 const glsl_type *struct_type = ir->record->type;
1619 ir->record->accept(this);
1621 for (i = 0; i < struct_type->length; i++) {
1622 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
1624 offset += type_size(struct_type->fields.structure[i].type);
1627 /* If the type is smaller than a vec4, replicate the last channel out. */
1628 if (ir->type->is_scalar() || ir->type->is_vector() || ir->type->is_matrix())
1629 this->result.swizzle = swizzle_for_size(ir->type->vector_elements);
1631 this->result.swizzle = BRW_SWIZZLE_NOOP;
1632 this->result.type = brw_type_for_base_type(ir->type);
1634 this->result.reg_offset += offset;
1638 * We want to be careful in assignment setup to hit the actual storage
1639 * instead of potentially using a temporary like we might with the
1640 * ir_dereference handler.
1643 get_assignment_lhs(ir_dereference *ir, vec4_visitor *v)
1645 /* The LHS must be a dereference. If the LHS is a variable indexed array
1646 * access of a vector, it must be separated into a series conditional moves
1647 * before reaching this point (see ir_vec_index_to_cond_assign).
1649 assert(ir->as_dereference());
1650 ir_dereference_array *deref_array = ir->as_dereference_array();
1652 assert(!deref_array->array->type->is_vector());
1655 /* Use the rvalue deref handler for the most part. We'll ignore
1656 * swizzles in it and write swizzles using writemask, though.
1659 return dst_reg(v->result);
1663 vec4_visitor::emit_block_move(dst_reg *dst, src_reg *src,
1664 const struct glsl_type *type, uint32_t predicate)
1666 if (type->base_type == GLSL_TYPE_STRUCT) {
1667 for (unsigned int i = 0; i < type->length; i++) {
1668 emit_block_move(dst, src, type->fields.structure[i].type, predicate);
1673 if (type->is_array()) {
1674 for (unsigned int i = 0; i < type->length; i++) {
1675 emit_block_move(dst, src, type->fields.array, predicate);
1680 if (type->is_matrix()) {
1681 const struct glsl_type *vec_type;
1683 vec_type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
1684 type->vector_elements, 1);
1686 for (int i = 0; i < type->matrix_columns; i++) {
1687 emit_block_move(dst, src, vec_type, predicate);
1692 assert(type->is_scalar() || type->is_vector());
1694 dst->type = brw_type_for_base_type(type);
1695 src->type = dst->type;
1697 dst->writemask = (1 << type->vector_elements) - 1;
1699 src->swizzle = swizzle_for_size(type->vector_elements);
1701 vec4_instruction *inst = emit(MOV(*dst, *src));
1702 inst->predicate = predicate;
1709 /* If the RHS processing resulted in an instruction generating a
1710 * temporary value, and it would be easy to rewrite the instruction to
1711 * generate its result right into the LHS instead, do so. This ends
1712 * up reliably removing instructions where it can be tricky to do so
1713 * later without real UD chain information.
1716 vec4_visitor::try_rewrite_rhs_to_dst(ir_assignment *ir,
1719 vec4_instruction *pre_rhs_inst,
1720 vec4_instruction *last_rhs_inst)
1722 /* This could be supported, but it would take more smarts. */
1726 if (pre_rhs_inst == last_rhs_inst)
1727 return false; /* No instructions generated to work with. */
1729 /* Make sure the last instruction generated our source reg. */
1730 if (src.file != GRF ||
1731 src.file != last_rhs_inst->dst.file ||
1732 src.reg != last_rhs_inst->dst.reg ||
1733 src.reg_offset != last_rhs_inst->dst.reg_offset ||
1737 last_rhs_inst->predicate != BRW_PREDICATE_NONE)
1740 /* Check that that last instruction fully initialized the channels
1741 * we want to use, in the order we want to use them. We could
1742 * potentially reswizzle the operands of many instructions so that
1743 * we could handle out of order channels, but don't yet.
1746 for (unsigned i = 0; i < 4; i++) {
1747 if (dst.writemask & (1 << i)) {
1748 if (!(last_rhs_inst->dst.writemask & (1 << i)))
1751 if (BRW_GET_SWZ(src.swizzle, i) != i)
1756 /* Success! Rewrite the instruction. */
1757 last_rhs_inst->dst.file = dst.file;
1758 last_rhs_inst->dst.reg = dst.reg;
1759 last_rhs_inst->dst.reg_offset = dst.reg_offset;
1760 last_rhs_inst->dst.reladdr = dst.reladdr;
1761 last_rhs_inst->dst.writemask &= dst.writemask;
1767 vec4_visitor::visit(ir_assignment *ir)
1769 dst_reg dst = get_assignment_lhs(ir->lhs, this);
1770 uint32_t predicate = BRW_PREDICATE_NONE;
1772 if (!ir->lhs->type->is_scalar() &&
1773 !ir->lhs->type->is_vector()) {
1774 ir->rhs->accept(this);
1775 src_reg src = this->result;
1777 if (ir->condition) {
1778 emit_bool_to_cond_code(ir->condition, &predicate);
1781 /* emit_block_move doesn't account for swizzles in the source register.
1782 * This should be ok, since the source register is a structure or an
1783 * array, and those can't be swizzled. But double-check to be sure.
1785 assert(src.swizzle ==
1786 (ir->rhs->type->is_matrix()
1787 ? swizzle_for_size(ir->rhs->type->vector_elements)
1788 : BRW_SWIZZLE_NOOP));
1790 emit_block_move(&dst, &src, ir->rhs->type, predicate);
1794 /* Now we're down to just a scalar/vector with writemasks. */
1797 vec4_instruction *pre_rhs_inst, *last_rhs_inst;
1798 pre_rhs_inst = (vec4_instruction *)this->instructions.get_tail();
1800 ir->rhs->accept(this);
1802 last_rhs_inst = (vec4_instruction *)this->instructions.get_tail();
1804 src_reg src = this->result;
1807 int first_enabled_chan = 0;
1810 assert(ir->lhs->type->is_vector() ||
1811 ir->lhs->type->is_scalar());
1812 dst.writemask = ir->write_mask;
1814 for (int i = 0; i < 4; i++) {
1815 if (dst.writemask & (1 << i)) {
1816 first_enabled_chan = BRW_GET_SWZ(src.swizzle, i);
1821 /* Swizzle a small RHS vector into the channels being written.
1823 * glsl ir treats write_mask as dictating how many channels are
1824 * present on the RHS while in our instructions we need to make
1825 * those channels appear in the slots of the vec4 they're written to.
1827 for (int i = 0; i < 4; i++) {
1828 if (dst.writemask & (1 << i))
1829 swizzles[i] = BRW_GET_SWZ(src.swizzle, src_chan++);
1831 swizzles[i] = first_enabled_chan;
1833 src.swizzle = BRW_SWIZZLE4(swizzles[0], swizzles[1],
1834 swizzles[2], swizzles[3]);
1836 if (try_rewrite_rhs_to_dst(ir, dst, src, pre_rhs_inst, last_rhs_inst)) {
1840 if (ir->condition) {
1841 emit_bool_to_cond_code(ir->condition, &predicate);
1844 for (i = 0; i < type_size(ir->lhs->type); i++) {
1845 vec4_instruction *inst = emit(MOV(dst, src));
1846 inst->predicate = predicate;
1854 vec4_visitor::emit_constant_values(dst_reg *dst, ir_constant *ir)
1856 if (ir->type->base_type == GLSL_TYPE_STRUCT) {
1857 foreach_list(node, &ir->components) {
1858 ir_constant *field_value = (ir_constant *)node;
1860 emit_constant_values(dst, field_value);
1865 if (ir->type->is_array()) {
1866 for (unsigned int i = 0; i < ir->type->length; i++) {
1867 emit_constant_values(dst, ir->array_elements[i]);
1872 if (ir->type->is_matrix()) {
1873 for (int i = 0; i < ir->type->matrix_columns; i++) {
1874 float *vec = &ir->value.f[i * ir->type->vector_elements];
1876 for (int j = 0; j < ir->type->vector_elements; j++) {
1877 dst->writemask = 1 << j;
1878 dst->type = BRW_REGISTER_TYPE_F;
1880 emit(MOV(*dst, src_reg(vec[j])));
1887 int remaining_writemask = (1 << ir->type->vector_elements) - 1;
1889 for (int i = 0; i < ir->type->vector_elements; i++) {
1890 if (!(remaining_writemask & (1 << i)))
1893 dst->writemask = 1 << i;
1894 dst->type = brw_type_for_base_type(ir->type);
1896 /* Find other components that match the one we're about to
1897 * write. Emits fewer instructions for things like vec4(0.5,
1900 for (int j = i + 1; j < ir->type->vector_elements; j++) {
1901 if (ir->type->base_type == GLSL_TYPE_BOOL) {
1902 if (ir->value.b[i] == ir->value.b[j])
1903 dst->writemask |= (1 << j);
1905 /* u, i, and f storage all line up, so no need for a
1906 * switch case for comparing each type.
1908 if (ir->value.u[i] == ir->value.u[j])
1909 dst->writemask |= (1 << j);
1913 switch (ir->type->base_type) {
1914 case GLSL_TYPE_FLOAT:
1915 emit(MOV(*dst, src_reg(ir->value.f[i])));
1918 emit(MOV(*dst, src_reg(ir->value.i[i])));
1920 case GLSL_TYPE_UINT:
1921 emit(MOV(*dst, src_reg(ir->value.u[i])));
1923 case GLSL_TYPE_BOOL:
1924 emit(MOV(*dst, src_reg(ir->value.b[i])));
1927 assert(!"Non-float/uint/int/bool constant");
1931 remaining_writemask &= ~dst->writemask;
1937 vec4_visitor::visit(ir_constant *ir)
1939 dst_reg dst = dst_reg(this, ir->type);
1940 this->result = src_reg(dst);
1942 emit_constant_values(&dst, ir);
1946 vec4_visitor::visit(ir_call *ir)
1948 assert(!"not reached");
1952 vec4_visitor::visit(ir_texture *ir)
1954 int sampler = _mesa_get_sampler_uniform_value(ir->sampler, prog, &vp->Base);
1956 /* Should be lowered by do_lower_texture_projection */
1957 assert(!ir->projector);
1959 /* Generate code to compute all the subexpression trees. This has to be
1960 * done before loading any values into MRFs for the sampler message since
1961 * generating these values may involve SEND messages that need the MRFs.
1964 if (ir->coordinate) {
1965 ir->coordinate->accept(this);
1966 coordinate = this->result;
1969 src_reg shadow_comparitor;
1970 if (ir->shadow_comparitor) {
1971 ir->shadow_comparitor->accept(this);
1972 shadow_comparitor = this->result;
1975 src_reg lod, dPdx, dPdy;
1980 ir->lod_info.lod->accept(this);
1984 ir->lod_info.grad.dPdx->accept(this);
1985 dPdx = this->result;
1987 ir->lod_info.grad.dPdy->accept(this);
1988 dPdy = this->result;
1995 vec4_instruction *inst = NULL;
1999 inst = new(mem_ctx) vec4_instruction(this, SHADER_OPCODE_TXL);
2002 inst = new(mem_ctx) vec4_instruction(this, SHADER_OPCODE_TXD);
2005 inst = new(mem_ctx) vec4_instruction(this, SHADER_OPCODE_TXF);
2008 inst = new(mem_ctx) vec4_instruction(this, SHADER_OPCODE_TXS);
2011 assert(!"TXB is not valid for vertex shaders.");
2014 /* Texel offsets go in the message header; Gen4 also requires headers. */
2015 inst->header_present = ir->offset || intel->gen < 5;
2017 inst->mlen = inst->header_present + 1; /* always at least one */
2018 inst->sampler = sampler;
2019 inst->dst = dst_reg(this, ir->type);
2020 inst->shadow_compare = ir->shadow_comparitor != NULL;
2022 if (ir->offset != NULL && ir->op != ir_txf)
2023 inst->texture_offset = brw_texture_offset(ir->offset->as_constant());
2025 /* MRF for the first parameter */
2026 int param_base = inst->base_mrf + inst->header_present;
2028 if (ir->op == ir_txs) {
2029 int writemask = intel->gen == 4 ? WRITEMASK_W : WRITEMASK_X;
2030 emit(MOV(dst_reg(MRF, param_base, ir->lod_info.lod->type, writemask),
2033 int i, coord_mask = 0, zero_mask = 0;
2034 /* Load the coordinate */
2035 /* FINISHME: gl_clamp_mask and saturate */
2036 for (i = 0; i < ir->coordinate->type->vector_elements; i++)
2037 coord_mask |= (1 << i);
2039 zero_mask |= (1 << i);
2041 if (ir->offset && ir->op == ir_txf) {
2042 /* It appears that the ld instruction used for txf does its
2043 * address bounds check before adding in the offset. To work
2044 * around this, just add the integer offset to the integer
2045 * texel coordinate, and don't put the offset in the header.
2047 ir_constant *offset = ir->offset->as_constant();
2050 for (int j = 0; j < ir->coordinate->type->vector_elements; j++) {
2051 src_reg src = coordinate;
2052 src.swizzle = BRW_SWIZZLE4(BRW_GET_SWZ(src.swizzle, j),
2053 BRW_GET_SWZ(src.swizzle, j),
2054 BRW_GET_SWZ(src.swizzle, j),
2055 BRW_GET_SWZ(src.swizzle, j));
2056 emit(ADD(dst_reg(MRF, param_base, ir->coordinate->type, 1 << j),
2057 src, offset->value.i[j]));
2060 emit(MOV(dst_reg(MRF, param_base, ir->coordinate->type, coord_mask),
2063 emit(MOV(dst_reg(MRF, param_base, ir->coordinate->type, zero_mask),
2065 /* Load the shadow comparitor */
2066 if (ir->shadow_comparitor) {
2067 emit(MOV(dst_reg(MRF, param_base + 1, ir->shadow_comparitor->type,
2069 shadow_comparitor));
2073 /* Load the LOD info */
2074 if (ir->op == ir_txl) {
2076 if (intel->gen >= 5) {
2077 mrf = param_base + 1;
2078 if (ir->shadow_comparitor) {
2079 writemask = WRITEMASK_Y;
2080 /* mlen already incremented */
2082 writemask = WRITEMASK_X;
2085 } else /* intel->gen == 4 */ {
2087 writemask = WRITEMASK_Z;
2089 emit(MOV(dst_reg(MRF, mrf, ir->lod_info.lod->type, writemask), lod));
2090 } else if (ir->op == ir_txf) {
2091 emit(MOV(dst_reg(MRF, param_base, ir->lod_info.lod->type, WRITEMASK_W),
2093 } else if (ir->op == ir_txd) {
2094 const glsl_type *type = ir->lod_info.grad.dPdx->type;
2096 if (intel->gen >= 5) {
2097 dPdx.swizzle = BRW_SWIZZLE4(SWIZZLE_X,SWIZZLE_X,SWIZZLE_Y,SWIZZLE_Y);
2098 dPdy.swizzle = BRW_SWIZZLE4(SWIZZLE_X,SWIZZLE_X,SWIZZLE_Y,SWIZZLE_Y);
2099 emit(MOV(dst_reg(MRF, param_base + 1, type, WRITEMASK_XZ), dPdx));
2100 emit(MOV(dst_reg(MRF, param_base + 1, type, WRITEMASK_YW), dPdy));
2103 if (ir->type->vector_elements == 3) {
2104 dPdx.swizzle = BRW_SWIZZLE_ZZZZ;
2105 dPdy.swizzle = BRW_SWIZZLE_ZZZZ;
2106 emit(MOV(dst_reg(MRF, param_base + 2, type, WRITEMASK_X), dPdx));
2107 emit(MOV(dst_reg(MRF, param_base + 2, type, WRITEMASK_Y), dPdy));
2110 } else /* intel->gen == 4 */ {
2111 emit(MOV(dst_reg(MRF, param_base + 1, type, WRITEMASK_XYZ), dPdx));
2112 emit(MOV(dst_reg(MRF, param_base + 2, type, WRITEMASK_XYZ), dPdy));
2120 /* fixup num layers (z) for cube arrays: hardware returns faces * layers;
2121 * spec requires layers.
2123 if (ir->op == ir_txs) {
2124 glsl_type const *type = ir->sampler->type;
2125 if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE &&
2126 type->sampler_array) {
2127 emit_math(SHADER_OPCODE_INT_QUOTIENT,
2128 with_writemask(inst->dst, WRITEMASK_Z),
2129 src_reg(inst->dst), src_reg(6));
2133 swizzle_result(ir, src_reg(inst->dst), sampler);
2137 vec4_visitor::swizzle_result(ir_texture *ir, src_reg orig_val, int sampler)
2139 this->result = orig_val;
2141 int s = c->key.tex.swizzles[sampler];
2143 if (ir->op == ir_txs || ir->type == glsl_type::float_type
2144 || s == SWIZZLE_NOOP)
2147 int zero_mask = 0, one_mask = 0, copy_mask = 0;
2150 for (int i = 0; i < 4; i++) {
2151 switch (GET_SWZ(s, i)) {
2153 zero_mask |= (1 << i);
2156 one_mask |= (1 << i);
2159 copy_mask |= (1 << i);
2160 swizzle[i] = GET_SWZ(s, i);
2165 this->result = src_reg(this, ir->type);
2166 dst_reg swizzled_result(this->result);
2169 orig_val.swizzle = BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
2170 swizzled_result.writemask = copy_mask;
2171 emit(MOV(swizzled_result, orig_val));
2175 swizzled_result.writemask = zero_mask;
2176 emit(MOV(swizzled_result, src_reg(0.0f)));
2180 swizzled_result.writemask = one_mask;
2181 emit(MOV(swizzled_result, src_reg(1.0f)));
2186 vec4_visitor::visit(ir_return *ir)
2188 assert(!"not reached");
2192 vec4_visitor::visit(ir_discard *ir)
2194 assert(!"not reached");
2198 vec4_visitor::visit(ir_if *ir)
2200 /* Don't point the annotation at the if statement, because then it plus
2201 * the then and else blocks get printed.
2203 this->base_ir = ir->condition;
2205 if (intel->gen == 6) {
2209 emit_bool_to_cond_code(ir->condition, &predicate);
2210 emit(IF(predicate));
2213 visit_instructions(&ir->then_instructions);
2215 if (!ir->else_instructions.is_empty()) {
2216 this->base_ir = ir->condition;
2217 emit(BRW_OPCODE_ELSE);
2219 visit_instructions(&ir->else_instructions);
2222 this->base_ir = ir->condition;
2223 emit(BRW_OPCODE_ENDIF);
2227 vec4_visitor::emit_ndc_computation()
2229 /* Get the position */
2230 src_reg pos = src_reg(output_reg[VERT_RESULT_HPOS]);
2232 /* Build ndc coords, which are (x/w, y/w, z/w, 1/w) */
2233 dst_reg ndc = dst_reg(this, glsl_type::vec4_type);
2234 output_reg[BRW_VERT_RESULT_NDC] = ndc;
2236 current_annotation = "NDC";
2237 dst_reg ndc_w = ndc;
2238 ndc_w.writemask = WRITEMASK_W;
2239 src_reg pos_w = pos;
2240 pos_w.swizzle = BRW_SWIZZLE4(SWIZZLE_W, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W);
2241 emit_math(SHADER_OPCODE_RCP, ndc_w, pos_w);
2243 dst_reg ndc_xyz = ndc;
2244 ndc_xyz.writemask = WRITEMASK_XYZ;
2246 emit(MUL(ndc_xyz, pos, src_reg(ndc_w)));
2250 vec4_visitor::emit_psiz_and_flags(struct brw_reg reg)
2252 if (intel->gen < 6 &&
2253 ((c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_PSIZ)) ||
2254 c->key.userclip_active || brw->has_negative_rhw_bug)) {
2255 dst_reg header1 = dst_reg(this, glsl_type::uvec4_type);
2256 dst_reg header1_w = header1;
2257 header1_w.writemask = WRITEMASK_W;
2260 emit(MOV(header1, 0u));
2262 if (c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_PSIZ)) {
2263 src_reg psiz = src_reg(output_reg[VERT_RESULT_PSIZ]);
2265 current_annotation = "Point size";
2266 emit(MUL(header1_w, psiz, src_reg((float)(1 << 11))));
2267 emit(AND(header1_w, src_reg(header1_w), 0x7ff << 8));
2270 current_annotation = "Clipping flags";
2271 for (i = 0; i < c->key.nr_userclip_plane_consts; i++) {
2272 vec4_instruction *inst;
2274 inst = emit(DP4(dst_null_f(), src_reg(output_reg[VERT_RESULT_HPOS]),
2275 src_reg(this->userplane[i])));
2276 inst->conditional_mod = BRW_CONDITIONAL_L;
2278 inst = emit(OR(header1_w, src_reg(header1_w), 1u << i));
2279 inst->predicate = BRW_PREDICATE_NORMAL;
2282 /* i965 clipping workaround:
2283 * 1) Test for -ve rhw
2285 * set ndc = (0,0,0,0)
2288 * Later, clipping will detect ucp[6] and ensure the primitive is
2289 * clipped against all fixed planes.
2291 if (brw->has_negative_rhw_bug) {
2295 vec8(brw_null_reg()),
2297 brw_swizzle1(output_reg[BRW_VERT_RESULT_NDC], 3),
2300 brw_OR(p, brw_writemask(header1, WRITEMASK_W), header1, brw_imm_ud(1<<6));
2301 brw_MOV(p, output_reg[BRW_VERT_RESULT_NDC], brw_imm_f(0));
2302 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
2306 emit(MOV(retype(reg, BRW_REGISTER_TYPE_UD), src_reg(header1)));
2307 } else if (intel->gen < 6) {
2308 emit(MOV(retype(reg, BRW_REGISTER_TYPE_UD), 0u));
2310 emit(MOV(retype(reg, BRW_REGISTER_TYPE_D), src_reg(0)));
2311 if (c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_PSIZ)) {
2312 emit(MOV(brw_writemask(reg, WRITEMASK_W),
2313 src_reg(output_reg[VERT_RESULT_PSIZ])));
2319 vec4_visitor::emit_clip_distances(struct brw_reg reg, int offset)
2321 if (intel->gen < 6) {
2322 /* Clip distance slots are set aside in gen5, but they are not used. It
2323 * is not clear whether we actually need to set aside space for them,
2324 * but the performance cost is negligible.
2329 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
2331 * "If a linked set of shaders forming the vertex stage contains no
2332 * static write to gl_ClipVertex or gl_ClipDistance, but the
2333 * application has requested clipping against user clip planes through
2334 * the API, then the coordinate written to gl_Position is used for
2335 * comparison against the user clip planes."
2337 * This function is only called if the shader didn't write to
2338 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
2339 * if the user wrote to it; otherwise we use gl_Position.
2341 gl_vert_result clip_vertex = VERT_RESULT_CLIP_VERTEX;
2342 if (!(c->prog_data.outputs_written
2343 & BITFIELD64_BIT(VERT_RESULT_CLIP_VERTEX))) {
2344 clip_vertex = VERT_RESULT_HPOS;
2347 for (int i = 0; i + offset < c->key.nr_userclip_plane_consts && i < 4;
2349 emit(DP4(dst_reg(brw_writemask(reg, 1 << i)),
2350 src_reg(output_reg[clip_vertex]),
2351 src_reg(this->userplane[i + offset])));
2356 vec4_visitor::emit_generic_urb_slot(dst_reg reg, int vert_result)
2358 assert (vert_result < VERT_RESULT_MAX);
2359 reg.type = output_reg[vert_result].type;
2360 current_annotation = output_reg_annotation[vert_result];
2361 /* Copy the register, saturating if necessary */
2362 vec4_instruction *inst = emit(MOV(reg,
2363 src_reg(output_reg[vert_result])));
2364 if ((vert_result == VERT_RESULT_COL0 ||
2365 vert_result == VERT_RESULT_COL1 ||
2366 vert_result == VERT_RESULT_BFC0 ||
2367 vert_result == VERT_RESULT_BFC1) &&
2368 c->key.clamp_vertex_color) {
2369 inst->saturate = true;
2374 vec4_visitor::emit_urb_slot(int mrf, int vert_result)
2376 struct brw_reg hw_reg = brw_message_reg(mrf);
2377 dst_reg reg = dst_reg(MRF, mrf);
2378 reg.type = BRW_REGISTER_TYPE_F;
2380 switch (vert_result) {
2381 case VERT_RESULT_PSIZ:
2382 /* PSIZ is always in slot 0, and is coupled with other flags. */
2383 current_annotation = "indices, point width, clip flags";
2384 emit_psiz_and_flags(hw_reg);
2386 case BRW_VERT_RESULT_NDC:
2387 current_annotation = "NDC";
2388 emit(MOV(reg, src_reg(output_reg[BRW_VERT_RESULT_NDC])));
2390 case BRW_VERT_RESULT_HPOS_DUPLICATE:
2391 case VERT_RESULT_HPOS:
2392 current_annotation = "gl_Position";
2393 emit(MOV(reg, src_reg(output_reg[VERT_RESULT_HPOS])));
2395 case VERT_RESULT_CLIP_DIST0:
2396 case VERT_RESULT_CLIP_DIST1:
2397 if (this->c->key.uses_clip_distance) {
2398 emit_generic_urb_slot(reg, vert_result);
2400 current_annotation = "user clip distances";
2401 emit_clip_distances(hw_reg, (vert_result - VERT_RESULT_CLIP_DIST0) * 4);
2404 case VERT_RESULT_EDGE:
2405 /* This is present when doing unfilled polygons. We're supposed to copy
2406 * the edge flag from the user-provided vertex array
2407 * (glEdgeFlagPointer), or otherwise we'll copy from the current value
2408 * of that attribute (starts as 1.0f). This is then used in clipping to
2409 * determine which edges should be drawn as wireframe.
2411 current_annotation = "edge flag";
2412 emit(MOV(reg, src_reg(dst_reg(ATTR, VERT_ATTRIB_EDGEFLAG,
2413 glsl_type::float_type, WRITEMASK_XYZW))));
2415 case BRW_VERT_RESULT_PAD:
2416 /* No need to write to this slot */
2419 emit_generic_urb_slot(reg, vert_result);
2425 align_interleaved_urb_mlen(struct brw_context *brw, int mlen)
2427 struct intel_context *intel = &brw->intel;
2429 if (intel->gen >= 6) {
2430 /* URB data written (does not include the message header reg) must
2431 * be a multiple of 256 bits, or 2 VS registers. See vol5c.5,
2432 * section 5.4.3.2.2: URB_INTERLEAVED.
2434 * URB entries are allocated on a multiple of 1024 bits, so an
2435 * extra 128 bits written here to make the end align to 256 is
2438 if ((mlen % 2) != 1)
2446 * Generates the VUE payload plus the 1 or 2 URB write instructions to
2447 * complete the VS thread.
2449 * The VUE layout is documented in Volume 2a.
2452 vec4_visitor::emit_urb_writes()
2454 /* MRF 0 is reserved for the debugger, so start with message header
2459 /* In the process of generating our URB write message contents, we
2460 * may need to unspill a register or load from an array. Those
2461 * reads would use MRFs 14-15.
2463 int max_usable_mrf = 13;
2465 /* The following assertion verifies that max_usable_mrf causes an
2466 * even-numbered amount of URB write data, which will meet gen6's
2467 * requirements for length alignment.
2469 assert ((max_usable_mrf - base_mrf) % 2 == 0);
2471 /* First mrf is the g0-based message header containing URB handles and such,
2472 * which is implied in VS_OPCODE_URB_WRITE.
2476 if (intel->gen < 6) {
2477 emit_ndc_computation();
2480 /* Set up the VUE data for the first URB write */
2482 for (slot = 0; slot < c->prog_data.vue_map.num_slots; ++slot) {
2483 emit_urb_slot(mrf++, c->prog_data.vue_map.slot_to_vert_result[slot]);
2485 /* If this was max_usable_mrf, we can't fit anything more into this URB
2488 if (mrf > max_usable_mrf) {
2494 current_annotation = "URB write";
2495 vec4_instruction *inst = emit(VS_OPCODE_URB_WRITE);
2496 inst->base_mrf = base_mrf;
2497 inst->mlen = align_interleaved_urb_mlen(brw, mrf - base_mrf);
2498 inst->eot = (slot >= c->prog_data.vue_map.num_slots);
2500 /* Optional second URB write */
2504 for (; slot < c->prog_data.vue_map.num_slots; ++slot) {
2505 assert(mrf < max_usable_mrf);
2507 emit_urb_slot(mrf++, c->prog_data.vue_map.slot_to_vert_result[slot]);
2510 current_annotation = "URB write";
2511 inst = emit(VS_OPCODE_URB_WRITE);
2512 inst->base_mrf = base_mrf;
2513 inst->mlen = align_interleaved_urb_mlen(brw, mrf - base_mrf);
2515 /* URB destination offset. In the previous write, we got MRFs
2516 * 2-13 minus the one header MRF, so 12 regs. URB offset is in
2517 * URB row increments, and each of our MRFs is half of one of
2518 * those, since we're doing interleaved writes.
2520 inst->offset = (max_usable_mrf - base_mrf) / 2;
2525 vec4_visitor::get_scratch_offset(vec4_instruction *inst,
2526 src_reg *reladdr, int reg_offset)
2528 /* Because we store the values to scratch interleaved like our
2529 * vertex data, we need to scale the vec4 index by 2.
2531 int message_header_scale = 2;
2533 /* Pre-gen6, the message header uses byte offsets instead of vec4
2534 * (16-byte) offset units.
2537 message_header_scale *= 16;
2540 src_reg index = src_reg(this, glsl_type::int_type);
2542 emit_before(inst, ADD(dst_reg(index), *reladdr, src_reg(reg_offset)));
2543 emit_before(inst, MUL(dst_reg(index),
2544 index, src_reg(message_header_scale)));
2548 return src_reg(reg_offset * message_header_scale);
2553 vec4_visitor::get_pull_constant_offset(vec4_instruction *inst,
2554 src_reg *reladdr, int reg_offset)
2557 src_reg index = src_reg(this, glsl_type::int_type);
2559 emit_before(inst, ADD(dst_reg(index), *reladdr, src_reg(reg_offset)));
2561 /* Pre-gen6, the message header uses byte offsets instead of vec4
2562 * (16-byte) offset units.
2564 if (intel->gen < 6) {
2565 emit_before(inst, MUL(dst_reg(index), index, src_reg(16)));
2570 int message_header_scale = intel->gen < 6 ? 16 : 1;
2571 return src_reg(reg_offset * message_header_scale);
2576 * Emits an instruction before @inst to load the value named by @orig_src
2577 * from scratch space at @base_offset to @temp.
2579 * @base_offset is measured in 32-byte units (the size of a register).
2582 vec4_visitor::emit_scratch_read(vec4_instruction *inst,
2583 dst_reg temp, src_reg orig_src,
2586 int reg_offset = base_offset + orig_src.reg_offset;
2587 src_reg index = get_scratch_offset(inst, orig_src.reladdr, reg_offset);
2589 emit_before(inst, SCRATCH_READ(temp, index));
2593 * Emits an instruction after @inst to store the value to be written
2594 * to @orig_dst to scratch space at @base_offset, from @temp.
2596 * @base_offset is measured in 32-byte units (the size of a register).
2599 vec4_visitor::emit_scratch_write(vec4_instruction *inst, int base_offset)
2601 int reg_offset = base_offset + inst->dst.reg_offset;
2602 src_reg index = get_scratch_offset(inst, inst->dst.reladdr, reg_offset);
2604 /* Create a temporary register to store *inst's result in.
2606 * We have to be careful in MOVing from our temporary result register in
2607 * the scratch write. If we swizzle from channels of the temporary that
2608 * weren't initialized, it will confuse live interval analysis, which will
2609 * make spilling fail to make progress.
2611 src_reg temp = src_reg(this, glsl_type::vec4_type);
2612 temp.type = inst->dst.type;
2613 int first_writemask_chan = ffs(inst->dst.writemask) - 1;
2615 for (int i = 0; i < 4; i++)
2616 if (inst->dst.writemask & (1 << i))
2619 swizzles[i] = first_writemask_chan;
2620 temp.swizzle = BRW_SWIZZLE4(swizzles[0], swizzles[1],
2621 swizzles[2], swizzles[3]);
2623 dst_reg dst = dst_reg(brw_writemask(brw_vec8_grf(0, 0),
2624 inst->dst.writemask));
2625 vec4_instruction *write = SCRATCH_WRITE(dst, temp, index);
2626 write->predicate = inst->predicate;
2627 write->ir = inst->ir;
2628 write->annotation = inst->annotation;
2629 inst->insert_after(write);
2631 inst->dst.file = temp.file;
2632 inst->dst.reg = temp.reg;
2633 inst->dst.reg_offset = temp.reg_offset;
2634 inst->dst.reladdr = NULL;
2638 * We can't generally support array access in GRF space, because a
2639 * single instruction's destination can only span 2 contiguous
2640 * registers. So, we send all GRF arrays that get variable index
2641 * access to scratch space.
2644 vec4_visitor::move_grf_array_access_to_scratch()
2646 int scratch_loc[this->virtual_grf_count];
2648 for (int i = 0; i < this->virtual_grf_count; i++) {
2649 scratch_loc[i] = -1;
2652 /* First, calculate the set of virtual GRFs that need to be punted
2653 * to scratch due to having any array access on them, and where in
2656 foreach_list(node, &this->instructions) {
2657 vec4_instruction *inst = (vec4_instruction *)node;
2659 if (inst->dst.file == GRF && inst->dst.reladdr &&
2660 scratch_loc[inst->dst.reg] == -1) {
2661 scratch_loc[inst->dst.reg] = c->last_scratch;
2662 c->last_scratch += this->virtual_grf_sizes[inst->dst.reg];
2665 for (int i = 0 ; i < 3; i++) {
2666 src_reg *src = &inst->src[i];
2668 if (src->file == GRF && src->reladdr &&
2669 scratch_loc[src->reg] == -1) {
2670 scratch_loc[src->reg] = c->last_scratch;
2671 c->last_scratch += this->virtual_grf_sizes[src->reg];
2676 /* Now, for anything that will be accessed through scratch, rewrite
2677 * it to load/store. Note that this is a _safe list walk, because
2678 * we may generate a new scratch_write instruction after the one
2681 foreach_list_safe(node, &this->instructions) {
2682 vec4_instruction *inst = (vec4_instruction *)node;
2684 /* Set up the annotation tracking for new generated instructions. */
2686 current_annotation = inst->annotation;
2688 if (inst->dst.file == GRF && scratch_loc[inst->dst.reg] != -1) {
2689 emit_scratch_write(inst, scratch_loc[inst->dst.reg]);
2692 for (int i = 0 ; i < 3; i++) {
2693 if (inst->src[i].file != GRF || scratch_loc[inst->src[i].reg] == -1)
2696 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
2698 emit_scratch_read(inst, temp, inst->src[i],
2699 scratch_loc[inst->src[i].reg]);
2701 inst->src[i].file = temp.file;
2702 inst->src[i].reg = temp.reg;
2703 inst->src[i].reg_offset = temp.reg_offset;
2704 inst->src[i].reladdr = NULL;
2710 * Emits an instruction before @inst to load the value named by @orig_src
2711 * from the pull constant buffer (surface) at @base_offset to @temp.
2714 vec4_visitor::emit_pull_constant_load(vec4_instruction *inst,
2715 dst_reg temp, src_reg orig_src,
2718 int reg_offset = base_offset + orig_src.reg_offset;
2719 src_reg index = src_reg((unsigned)SURF_INDEX_VERT_CONST_BUFFER);
2720 src_reg offset = get_pull_constant_offset(inst, orig_src.reladdr, reg_offset);
2721 vec4_instruction *load;
2723 load = new(mem_ctx) vec4_instruction(this, VS_OPCODE_PULL_CONSTANT_LOAD,
2724 temp, index, offset);
2725 load->base_mrf = 14;
2727 emit_before(inst, load);
2731 * Implements array access of uniforms by inserting a
2732 * PULL_CONSTANT_LOAD instruction.
2734 * Unlike temporary GRF array access (where we don't support it due to
2735 * the difficulty of doing relative addressing on instruction
2736 * destinations), we could potentially do array access of uniforms
2737 * that were loaded in GRF space as push constants. In real-world
2738 * usage we've seen, though, the arrays being used are always larger
2739 * than we could load as push constants, so just always move all
2740 * uniform array access out to a pull constant buffer.
2743 vec4_visitor::move_uniform_array_access_to_pull_constants()
2745 int pull_constant_loc[this->uniforms];
2747 for (int i = 0; i < this->uniforms; i++) {
2748 pull_constant_loc[i] = -1;
2751 /* Walk through and find array access of uniforms. Put a copy of that
2752 * uniform in the pull constant buffer.
2754 * Note that we don't move constant-indexed accesses to arrays. No
2755 * testing has been done of the performance impact of this choice.
2757 foreach_list_safe(node, &this->instructions) {
2758 vec4_instruction *inst = (vec4_instruction *)node;
2760 for (int i = 0 ; i < 3; i++) {
2761 if (inst->src[i].file != UNIFORM || !inst->src[i].reladdr)
2764 int uniform = inst->src[i].reg;
2766 /* If this array isn't already present in the pull constant buffer,
2769 if (pull_constant_loc[uniform] == -1) {
2770 const float **values = &prog_data->param[uniform * 4];
2772 pull_constant_loc[uniform] = prog_data->nr_pull_params / 4;
2774 for (int j = 0; j < uniform_size[uniform] * 4; j++) {
2775 prog_data->pull_param[prog_data->nr_pull_params++] = values[j];
2779 /* Set up the annotation tracking for new generated instructions. */
2781 current_annotation = inst->annotation;
2783 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
2785 emit_pull_constant_load(inst, temp, inst->src[i],
2786 pull_constant_loc[uniform]);
2788 inst->src[i].file = temp.file;
2789 inst->src[i].reg = temp.reg;
2790 inst->src[i].reg_offset = temp.reg_offset;
2791 inst->src[i].reladdr = NULL;
2795 /* Now there are no accesses of the UNIFORM file with a reladdr, so
2796 * no need to track them as larger-than-vec4 objects. This will be
2797 * relied on in cutting out unused uniform vectors from push
2800 split_uniform_registers();
2804 vec4_visitor::resolve_ud_negate(src_reg *reg)
2806 if (reg->type != BRW_REGISTER_TYPE_UD ||
2810 src_reg temp = src_reg(this, glsl_type::uvec4_type);
2811 emit(BRW_OPCODE_MOV, dst_reg(temp), *reg);
2815 vec4_visitor::vec4_visitor(struct brw_context *brw,
2816 struct brw_vs_compile *c,
2817 struct gl_shader_program *prog,
2818 struct brw_shader *shader,
2823 this->intel = &brw->intel;
2824 this->ctx = &intel->ctx;
2826 this->shader = shader;
2828 this->mem_ctx = mem_ctx;
2829 this->failed = false;
2831 this->base_ir = NULL;
2832 this->current_annotation = NULL;
2833 memset(this->output_reg_annotation, 0, sizeof(this->output_reg_annotation));
2836 this->vp = &c->vp->program;
2837 this->prog_data = &c->prog_data;
2839 this->variable_ht = hash_table_ctor(0,
2840 hash_table_pointer_hash,
2841 hash_table_pointer_compare);
2843 this->virtual_grf_def = NULL;
2844 this->virtual_grf_use = NULL;
2845 this->virtual_grf_sizes = NULL;
2846 this->virtual_grf_count = 0;
2847 this->virtual_grf_reg_map = NULL;
2848 this->virtual_grf_reg_count = 0;
2849 this->virtual_grf_array_size = 0;
2850 this->live_intervals_valid = false;
2852 this->max_grf = intel->gen >= 7 ? GEN7_MRF_HACK_START : BRW_MAX_GRF;
2857 vec4_visitor::~vec4_visitor()
2859 hash_table_dtor(this->variable_ht);
2864 vec4_visitor::fail(const char *format, ...)
2874 va_start(va, format);
2875 msg = ralloc_vasprintf(mem_ctx, format, va);
2877 msg = ralloc_asprintf(mem_ctx, "VS compile failed: %s\n", msg);
2879 this->fail_msg = msg;
2881 if (INTEL_DEBUG & DEBUG_VS) {
2882 fprintf(stderr, "%s", msg);
2886 } /* namespace brw */