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/macros.h"
27 #include "program/prog_parameter.h"
28 #include "program/sampler.h"
33 vec4_instruction::vec4_instruction(vec4_visitor
*v
,
34 enum opcode opcode
, dst_reg dst
,
35 src_reg src0
, src_reg src1
, src_reg src2
)
37 this->opcode
= opcode
;
42 this->ir
= v
->base_ir
;
43 this->annotation
= v
->current_annotation
;
47 vec4_visitor::emit(vec4_instruction
*inst
)
49 this->instructions
.push_tail(inst
);
55 vec4_visitor::emit_before(vec4_instruction
*inst
, vec4_instruction
*new_inst
)
57 new_inst
->ir
= inst
->ir
;
58 new_inst
->annotation
= inst
->annotation
;
60 inst
->insert_before(new_inst
);
66 vec4_visitor::emit(enum opcode opcode
, dst_reg dst
,
67 src_reg src0
, src_reg src1
, src_reg src2
)
69 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst
,
75 vec4_visitor::emit(enum opcode opcode
, dst_reg dst
, src_reg src0
, src_reg src1
)
77 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst
, src0
, src1
));
81 vec4_visitor::emit(enum opcode opcode
, dst_reg dst
, src_reg src0
)
83 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst
, src0
));
87 vec4_visitor::emit(enum opcode opcode
)
89 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst_reg()));
94 vec4_visitor::op(dst_reg dst, src_reg src0) \
96 return new(mem_ctx) vec4_instruction(this, BRW_OPCODE_##op, dst, \
102 vec4_visitor::op(dst_reg dst, src_reg src0, src_reg src1) \
104 return new(mem_ctx) vec4_instruction(this, BRW_OPCODE_##op, dst, \
123 /** Gen4 predicated IF. */
125 vec4_visitor::IF(uint32_t predicate
)
127 vec4_instruction
*inst
;
129 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_IF
);
130 inst
->predicate
= predicate
;
135 /** Gen6+ IF with embedded comparison. */
137 vec4_visitor::IF(src_reg src0
, src_reg src1
, uint32_t condition
)
139 assert(intel
->gen
>= 6);
141 vec4_instruction
*inst
;
143 resolve_ud_negate(&src0
);
144 resolve_ud_negate(&src1
);
146 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_IF
, dst_null_d(),
148 inst
->conditional_mod
= condition
;
154 * CMP: Sets the low bit of the destination channels with the result
155 * of the comparison, while the upper bits are undefined, and updates
156 * the flag register with the packed 16 bits of the result.
159 vec4_visitor::CMP(dst_reg dst
, src_reg src0
, src_reg src1
, uint32_t condition
)
161 vec4_instruction
*inst
;
163 /* original gen4 does type conversion to the destination type
164 * before before comparison, producing garbage results for floating
167 if (intel
->gen
== 4) {
168 dst
.type
= src0
.type
;
169 if (dst
.file
== HW_REG
)
170 dst
.fixed_hw_reg
.type
= dst
.type
;
173 resolve_ud_negate(&src0
);
174 resolve_ud_negate(&src1
);
176 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_CMP
, dst
, src0
, src1
);
177 inst
->conditional_mod
= condition
;
183 vec4_visitor::SCRATCH_READ(dst_reg dst
, src_reg index
)
185 vec4_instruction
*inst
;
187 inst
= new(mem_ctx
) vec4_instruction(this, VS_OPCODE_SCRATCH_READ
,
196 vec4_visitor::SCRATCH_WRITE(dst_reg dst
, src_reg src
, src_reg index
)
198 vec4_instruction
*inst
;
200 inst
= new(mem_ctx
) vec4_instruction(this, VS_OPCODE_SCRATCH_WRITE
,
209 vec4_visitor::emit_dp(dst_reg dst
, src_reg src0
, src_reg src1
, unsigned elements
)
211 static enum opcode dot_opcodes
[] = {
212 BRW_OPCODE_DP2
, BRW_OPCODE_DP3
, BRW_OPCODE_DP4
215 emit(dot_opcodes
[elements
- 2], dst
, src0
, src1
);
219 vec4_visitor::emit_math1_gen6(enum opcode opcode
, dst_reg dst
, src_reg src
)
221 /* The gen6 math instruction ignores the source modifiers --
222 * swizzle, abs, negate, and at least some parts of the register
223 * region description.
225 * While it would seem that this MOV could be avoided at this point
226 * in the case that the swizzle is matched up with the destination
227 * writemask, note that uniform packing and register allocation
228 * could rearrange our swizzle, so let's leave this matter up to
229 * copy propagation later.
231 src_reg temp_src
= src_reg(this, glsl_type::vec4_type
);
232 emit(MOV(dst_reg(temp_src
), src
));
234 if (dst
.writemask
!= WRITEMASK_XYZW
) {
235 /* The gen6 math instruction must be align1, so we can't do
238 dst_reg temp_dst
= dst_reg(this, glsl_type::vec4_type
);
240 emit(opcode
, temp_dst
, temp_src
);
242 emit(MOV(dst
, src_reg(temp_dst
)));
244 emit(opcode
, dst
, temp_src
);
249 vec4_visitor::emit_math1_gen4(enum opcode opcode
, dst_reg dst
, src_reg src
)
251 vec4_instruction
*inst
= emit(opcode
, dst
, src
);
257 vec4_visitor::emit_math(opcode opcode
, dst_reg dst
, src_reg src
)
260 case SHADER_OPCODE_RCP
:
261 case SHADER_OPCODE_RSQ
:
262 case SHADER_OPCODE_SQRT
:
263 case SHADER_OPCODE_EXP2
:
264 case SHADER_OPCODE_LOG2
:
265 case SHADER_OPCODE_SIN
:
266 case SHADER_OPCODE_COS
:
269 assert(!"not reached: bad math opcode");
273 if (intel
->gen
>= 7) {
274 emit(opcode
, dst
, src
);
275 } else if (intel
->gen
== 6) {
276 return emit_math1_gen6(opcode
, dst
, src
);
278 return emit_math1_gen4(opcode
, dst
, src
);
283 vec4_visitor::emit_math2_gen6(enum opcode opcode
,
284 dst_reg dst
, src_reg src0
, src_reg src1
)
288 /* The gen6 math instruction ignores the source modifiers --
289 * swizzle, abs, negate, and at least some parts of the register
290 * region description. Move the sources to temporaries to make it
294 expanded
= src_reg(this, glsl_type::vec4_type
);
295 expanded
.type
= src0
.type
;
296 emit(MOV(dst_reg(expanded
), src0
));
299 expanded
= src_reg(this, glsl_type::vec4_type
);
300 expanded
.type
= src1
.type
;
301 emit(MOV(dst_reg(expanded
), 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
>= 7) {
343 emit(opcode
, dst
, src0
, src1
);
344 } else if (intel
->gen
== 6) {
345 return emit_math2_gen6(opcode
, dst
, src0
, src1
);
347 return emit_math2_gen4(opcode
, dst
, src0
, src1
);
352 vec4_visitor::visit_instructions(const exec_list
*list
)
354 foreach_list(node
, list
) {
355 ir_instruction
*ir
= (ir_instruction
*)node
;
364 type_size(const struct glsl_type
*type
)
369 switch (type
->base_type
) {
372 case GLSL_TYPE_FLOAT
:
374 if (type
->is_matrix()) {
375 return type
->matrix_columns
;
377 /* Regardless of size of vector, it gets a vec4. This is bad
378 * packing for things like floats, but otherwise arrays become a
379 * mess. Hopefully a later pass over the code can pack scalars
380 * down if appropriate.
384 case GLSL_TYPE_ARRAY
:
385 assert(type
->length
> 0);
386 return type_size(type
->fields
.array
) * type
->length
;
387 case GLSL_TYPE_STRUCT
:
389 for (i
= 0; i
< type
->length
; i
++) {
390 size
+= type_size(type
->fields
.structure
[i
].type
);
393 case GLSL_TYPE_SAMPLER
:
394 /* Samplers take up one slot in UNIFORMS[], but they're baked in
405 vec4_visitor::virtual_grf_alloc(int size
)
407 if (virtual_grf_array_size
<= virtual_grf_count
) {
408 if (virtual_grf_array_size
== 0)
409 virtual_grf_array_size
= 16;
411 virtual_grf_array_size
*= 2;
412 virtual_grf_sizes
= reralloc(mem_ctx
, virtual_grf_sizes
, int,
413 virtual_grf_array_size
);
414 virtual_grf_reg_map
= reralloc(mem_ctx
, virtual_grf_reg_map
, int,
415 virtual_grf_array_size
);
417 virtual_grf_reg_map
[virtual_grf_count
] = virtual_grf_reg_count
;
418 virtual_grf_reg_count
+= size
;
419 virtual_grf_sizes
[virtual_grf_count
] = size
;
420 return virtual_grf_count
++;
423 src_reg::src_reg(class vec4_visitor
*v
, const struct glsl_type
*type
)
428 this->reg
= v
->virtual_grf_alloc(type_size(type
));
430 if (type
->is_array() || type
->is_record()) {
431 this->swizzle
= BRW_SWIZZLE_NOOP
;
433 this->swizzle
= swizzle_for_size(type
->vector_elements
);
436 this->type
= brw_type_for_base_type(type
);
439 dst_reg::dst_reg(class vec4_visitor
*v
, const struct glsl_type
*type
)
444 this->reg
= v
->virtual_grf_alloc(type_size(type
));
446 if (type
->is_array() || type
->is_record()) {
447 this->writemask
= WRITEMASK_XYZW
;
449 this->writemask
= (1 << type
->vector_elements
) - 1;
452 this->type
= brw_type_for_base_type(type
);
455 /* Our support for uniforms is piggy-backed on the struct
456 * gl_fragment_program, because that's where the values actually
457 * get stored, rather than in some global gl_shader_program uniform
461 vec4_visitor::setup_uniform_values(int loc
, const glsl_type
*type
)
463 unsigned int offset
= 0;
464 float *values
= &this->vp
->Base
.Parameters
->ParameterValues
[loc
][0].f
;
466 if (type
->is_matrix()) {
467 const glsl_type
*column
= type
->column_type();
469 for (unsigned int i
= 0; i
< type
->matrix_columns
; i
++) {
470 offset
+= setup_uniform_values(loc
+ offset
, column
);
476 switch (type
->base_type
) {
477 case GLSL_TYPE_FLOAT
:
481 for (unsigned int i
= 0; i
< type
->vector_elements
; i
++) {
482 c
->prog_data
.param
[this->uniforms
* 4 + i
] = &values
[i
];
485 /* Set up pad elements to get things aligned to a vec4 boundary. */
486 for (unsigned int i
= type
->vector_elements
; i
< 4; i
++) {
487 static float zero
= 0;
489 c
->prog_data
.param
[this->uniforms
* 4 + i
] = &zero
;
492 /* Track the size of this uniform vector, for future packing of
495 this->uniform_vector_size
[this->uniforms
] = type
->vector_elements
;
500 case GLSL_TYPE_STRUCT
:
501 for (unsigned int i
= 0; i
< type
->length
; i
++) {
502 offset
+= setup_uniform_values(loc
+ offset
,
503 type
->fields
.structure
[i
].type
);
507 case GLSL_TYPE_ARRAY
:
508 for (unsigned int i
= 0; i
< type
->length
; i
++) {
509 offset
+= setup_uniform_values(loc
+ offset
, type
->fields
.array
);
513 case GLSL_TYPE_SAMPLER
:
514 /* The sampler takes up a slot, but we don't use any values from it. */
518 assert(!"not reached");
524 vec4_visitor::setup_uniform_clipplane_values()
526 gl_clip_plane
*clip_planes
= brw_select_clip_planes(ctx
);
528 /* Pre-Gen6, we compact clip planes. For example, if the user
529 * enables just clip planes 0, 1, and 3, we will enable clip planes
530 * 0, 1, and 2 in the hardware, and we'll move clip plane 3 to clip
531 * plane 2. This simplifies the implementation of the Gen6 clip
534 * In Gen6 and later, we don't compact clip planes, because this
535 * simplifies the implementation of gl_ClipDistance.
537 int compacted_clipplane_index
= 0;
538 for (int i
= 0; i
< c
->key
.nr_userclip_plane_consts
; ++i
) {
539 if (intel
->gen
< 6 &&
540 !(c
->key
.userclip_planes_enabled_gen_4_5
& (1 << i
))) {
543 this->uniform_vector_size
[this->uniforms
] = 4;
544 this->userplane
[compacted_clipplane_index
] = dst_reg(UNIFORM
, this->uniforms
);
545 this->userplane
[compacted_clipplane_index
].type
= BRW_REGISTER_TYPE_F
;
546 for (int j
= 0; j
< 4; ++j
) {
547 c
->prog_data
.param
[this->uniforms
* 4 + j
] = &clip_planes
[i
][j
];
549 ++compacted_clipplane_index
;
554 /* Our support for builtin uniforms is even scarier than non-builtin.
555 * It sits on top of the PROG_STATE_VAR parameters that are
556 * automatically updated from GL context state.
559 vec4_visitor::setup_builtin_uniform_values(ir_variable
*ir
)
561 const ir_state_slot
*const slots
= ir
->state_slots
;
562 assert(ir
->state_slots
!= NULL
);
564 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
565 /* This state reference has already been setup by ir_to_mesa,
566 * but we'll get the same index back here. We can reference
567 * ParameterValues directly, since unlike brw_fs.cpp, we never
568 * add new state references during compile.
570 int index
= _mesa_add_state_reference(this->vp
->Base
.Parameters
,
571 (gl_state_index
*)slots
[i
].tokens
);
572 float *values
= &this->vp
->Base
.Parameters
->ParameterValues
[index
][0].f
;
574 this->uniform_vector_size
[this->uniforms
] = 0;
575 /* Add each of the unique swizzled channels of the element.
576 * This will end up matching the size of the glsl_type of this field.
579 for (unsigned int j
= 0; j
< 4; j
++) {
580 int swiz
= GET_SWZ(slots
[i
].swizzle
, j
);
583 c
->prog_data
.param
[this->uniforms
* 4 + j
] = &values
[swiz
];
584 if (swiz
<= last_swiz
)
585 this->uniform_vector_size
[this->uniforms
]++;
592 vec4_visitor::variable_storage(ir_variable
*var
)
594 return (dst_reg
*)hash_table_find(this->variable_ht
, var
);
598 vec4_visitor::emit_bool_to_cond_code(ir_rvalue
*ir
, uint32_t *predicate
)
600 ir_expression
*expr
= ir
->as_expression();
602 *predicate
= BRW_PREDICATE_NORMAL
;
606 vec4_instruction
*inst
;
608 assert(expr
->get_num_operands() <= 2);
609 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
610 expr
->operands
[i
]->accept(this);
611 op
[i
] = this->result
;
613 resolve_ud_negate(&op
[i
]);
616 switch (expr
->operation
) {
617 case ir_unop_logic_not
:
618 inst
= emit(AND(dst_null_d(), op
[0], src_reg(1)));
619 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
622 case ir_binop_logic_xor
:
623 inst
= emit(XOR(dst_null_d(), op
[0], op
[1]));
624 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
627 case ir_binop_logic_or
:
628 inst
= emit(OR(dst_null_d(), op
[0], op
[1]));
629 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
632 case ir_binop_logic_and
:
633 inst
= emit(AND(dst_null_d(), op
[0], op
[1]));
634 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
638 if (intel
->gen
>= 6) {
639 emit(CMP(dst_null_d(), op
[0], src_reg(0.0f
), BRW_CONDITIONAL_NZ
));
641 inst
= emit(MOV(dst_null_f(), op
[0]));
642 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
647 if (intel
->gen
>= 6) {
648 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
650 inst
= emit(MOV(dst_null_d(), op
[0]));
651 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
655 case ir_binop_all_equal
:
656 inst
= emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
657 *predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
660 case ir_binop_any_nequal
:
661 inst
= emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
662 *predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
666 inst
= emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
667 *predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
670 case ir_binop_greater
:
671 case ir_binop_gequal
:
673 case ir_binop_lequal
:
675 case ir_binop_nequal
:
676 emit(CMP(dst_null_d(), op
[0], op
[1],
677 brw_conditional_for_comparison(expr
->operation
)));
681 assert(!"not reached");
689 resolve_ud_negate(&this->result
);
691 if (intel
->gen
>= 6) {
692 vec4_instruction
*inst
= emit(AND(dst_null_d(),
693 this->result
, src_reg(1)));
694 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
696 vec4_instruction
*inst
= emit(MOV(dst_null_d(), this->result
));
697 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
702 * Emit a gen6 IF statement with the comparison folded into the IF
706 vec4_visitor::emit_if_gen6(ir_if
*ir
)
708 ir_expression
*expr
= ir
->condition
->as_expression();
714 assert(expr
->get_num_operands() <= 2);
715 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
716 expr
->operands
[i
]->accept(this);
717 op
[i
] = this->result
;
720 switch (expr
->operation
) {
721 case ir_unop_logic_not
:
722 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_Z
));
725 case ir_binop_logic_xor
:
726 emit(IF(op
[0], op
[1], BRW_CONDITIONAL_NZ
));
729 case ir_binop_logic_or
:
730 temp
= dst_reg(this, glsl_type::bool_type
);
731 emit(OR(temp
, op
[0], op
[1]));
732 emit(IF(src_reg(temp
), src_reg(0), BRW_CONDITIONAL_NZ
));
735 case ir_binop_logic_and
:
736 temp
= dst_reg(this, glsl_type::bool_type
);
737 emit(AND(temp
, op
[0], op
[1]));
738 emit(IF(src_reg(temp
), src_reg(0), BRW_CONDITIONAL_NZ
));
742 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
746 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
749 case ir_binop_greater
:
750 case ir_binop_gequal
:
752 case ir_binop_lequal
:
754 case ir_binop_nequal
:
755 emit(IF(op
[0], op
[1],
756 brw_conditional_for_comparison(expr
->operation
)));
759 case ir_binop_all_equal
:
760 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
761 emit(IF(BRW_PREDICATE_ALIGN16_ALL4H
));
764 case ir_binop_any_nequal
:
765 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
766 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H
));
770 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
771 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H
));
775 assert(!"not reached");
776 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
782 ir
->condition
->accept(this);
784 emit(IF(this->result
, src_reg(0), BRW_CONDITIONAL_NZ
));
788 vec4_visitor::visit(ir_variable
*ir
)
792 if (variable_storage(ir
))
797 reg
= new(mem_ctx
) dst_reg(ATTR
, ir
->location
);
799 /* Do GL_FIXED rescaling for GLES2.0. Our GL_FIXED attributes
800 * come in as floating point conversions of the integer values.
802 for (int i
= ir
->location
; i
< ir
->location
+ type_size(ir
->type
); i
++) {
803 if (!c
->key
.gl_fixed_input_size
[i
])
807 dst
.type
= brw_type_for_base_type(ir
->type
);
808 dst
.writemask
= (1 << c
->key
.gl_fixed_input_size
[i
]) - 1;
809 emit(MUL(dst
, src_reg(dst
), src_reg(1.0f
/ 65536.0f
)));
814 reg
= new(mem_ctx
) dst_reg(this, ir
->type
);
816 for (int i
= 0; i
< type_size(ir
->type
); i
++) {
817 output_reg
[ir
->location
+ i
] = *reg
;
818 output_reg
[ir
->location
+ i
].reg_offset
= i
;
819 output_reg
[ir
->location
+ i
].type
=
820 brw_type_for_base_type(ir
->type
->get_scalar_type());
821 output_reg_annotation
[ir
->location
+ i
] = ir
->name
;
826 case ir_var_temporary
:
827 reg
= new(mem_ctx
) dst_reg(this, ir
->type
);
831 reg
= new(this->mem_ctx
) dst_reg(UNIFORM
, this->uniforms
);
833 /* Track how big the whole uniform variable is, in case we need to put a
834 * copy of its data into pull constants for array access.
836 this->uniform_size
[this->uniforms
] = type_size(ir
->type
);
838 if (!strncmp(ir
->name
, "gl_", 3)) {
839 setup_builtin_uniform_values(ir
);
841 setup_uniform_values(ir
->location
, ir
->type
);
845 case ir_var_system_value
:
846 /* VertexID is stored by the VF as the last vertex element, but
847 * we don't represent it with a flag in inputs_read, so we call
848 * it VERT_ATTRIB_MAX, which setup_attributes() picks up on.
850 reg
= new(mem_ctx
) dst_reg(ATTR
, VERT_ATTRIB_MAX
);
851 prog_data
->uses_vertexid
= true;
853 switch (ir
->location
) {
854 case SYSTEM_VALUE_VERTEX_ID
:
855 reg
->writemask
= WRITEMASK_X
;
857 case SYSTEM_VALUE_INSTANCE_ID
:
858 reg
->writemask
= WRITEMASK_Y
;
861 assert(!"not reached");
867 assert(!"not reached");
870 reg
->type
= brw_type_for_base_type(ir
->type
);
871 hash_table_insert(this->variable_ht
, reg
, ir
);
875 vec4_visitor::visit(ir_loop
*ir
)
879 /* We don't want debugging output to print the whole body of the
880 * loop as the annotation.
882 this->base_ir
= NULL
;
884 if (ir
->counter
!= NULL
) {
885 this->base_ir
= ir
->counter
;
886 ir
->counter
->accept(this);
887 counter
= *(variable_storage(ir
->counter
));
889 if (ir
->from
!= NULL
) {
890 this->base_ir
= ir
->from
;
891 ir
->from
->accept(this);
893 emit(MOV(counter
, this->result
));
900 this->base_ir
= ir
->to
;
901 ir
->to
->accept(this);
903 emit(CMP(dst_null_d(), src_reg(counter
), this->result
,
904 brw_conditional_for_comparison(ir
->cmp
)));
906 vec4_instruction
*inst
= emit(BRW_OPCODE_BREAK
);
907 inst
->predicate
= BRW_PREDICATE_NORMAL
;
910 visit_instructions(&ir
->body_instructions
);
914 this->base_ir
= ir
->increment
;
915 ir
->increment
->accept(this);
916 emit(ADD(counter
, src_reg(counter
), this->result
));
919 emit(BRW_OPCODE_WHILE
);
923 vec4_visitor::visit(ir_loop_jump
*ir
)
926 case ir_loop_jump::jump_break
:
927 emit(BRW_OPCODE_BREAK
);
929 case ir_loop_jump::jump_continue
:
930 emit(BRW_OPCODE_CONTINUE
);
937 vec4_visitor::visit(ir_function_signature
*ir
)
944 vec4_visitor::visit(ir_function
*ir
)
946 /* Ignore function bodies other than main() -- we shouldn't see calls to
947 * them since they should all be inlined.
949 if (strcmp(ir
->name
, "main") == 0) {
950 const ir_function_signature
*sig
;
953 sig
= ir
->matching_signature(&empty
);
957 visit_instructions(&sig
->body
);
962 vec4_visitor::try_emit_sat(ir_expression
*ir
)
964 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
968 sat_src
->accept(this);
969 src_reg src
= this->result
;
971 this->result
= src_reg(this, ir
->type
);
972 vec4_instruction
*inst
;
973 inst
= emit(MOV(dst_reg(this->result
), src
));
974 inst
->saturate
= true;
980 vec4_visitor::emit_bool_comparison(unsigned int op
,
981 dst_reg dst
, src_reg src0
, src_reg src1
)
983 /* original gen4 does destination conversion before comparison. */
985 dst
.type
= src0
.type
;
987 emit(CMP(dst
, src0
, src1
, brw_conditional_for_comparison(op
)));
989 dst
.type
= BRW_REGISTER_TYPE_D
;
990 emit(AND(dst
, src_reg(dst
), src_reg(0x1)));
994 vec4_visitor::visit(ir_expression
*ir
)
996 unsigned int operand
;
997 src_reg op
[Elements(ir
->operands
)];
1000 vec4_instruction
*inst
;
1002 if (try_emit_sat(ir
))
1005 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1006 this->result
.file
= BAD_FILE
;
1007 ir
->operands
[operand
]->accept(this);
1008 if (this->result
.file
== BAD_FILE
) {
1009 printf("Failed to get tree for expression operand:\n");
1010 ir
->operands
[operand
]->print();
1013 op
[operand
] = this->result
;
1015 /* Matrix expression operands should have been broken down to vector
1016 * operations already.
1018 assert(!ir
->operands
[operand
]->type
->is_matrix());
1021 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1022 if (ir
->operands
[1]) {
1023 vector_elements
= MAX2(vector_elements
,
1024 ir
->operands
[1]->type
->vector_elements
);
1027 this->result
.file
= BAD_FILE
;
1029 /* Storage for our result. Ideally for an assignment we'd be using
1030 * the actual storage for the result here, instead.
1032 result_src
= src_reg(this, ir
->type
);
1033 /* convenience for the emit functions below. */
1034 result_dst
= dst_reg(result_src
);
1035 /* If nothing special happens, this is the result. */
1036 this->result
= result_src
;
1037 /* Limit writes to the channels that will be used by result_src later.
1038 * This does limit this temp's use as a temporary for multi-instruction
1041 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1043 switch (ir
->operation
) {
1044 case ir_unop_logic_not
:
1045 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
1046 * ones complement of the whole register, not just bit 0.
1048 emit(XOR(result_dst
, op
[0], src_reg(1)));
1051 op
[0].negate
= !op
[0].negate
;
1052 this->result
= op
[0];
1056 op
[0].negate
= false;
1057 this->result
= op
[0];
1061 emit(MOV(result_dst
, src_reg(0.0f
)));
1063 emit(CMP(dst_null_d(), op
[0], src_reg(0.0f
), BRW_CONDITIONAL_G
));
1064 inst
= emit(MOV(result_dst
, src_reg(1.0f
)));
1065 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1067 emit(CMP(dst_null_d(), op
[0], src_reg(0.0f
), BRW_CONDITIONAL_L
));
1068 inst
= emit(MOV(result_dst
, src_reg(-1.0f
)));
1069 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1074 emit_math(SHADER_OPCODE_RCP
, result_dst
, op
[0]);
1078 emit_math(SHADER_OPCODE_EXP2
, result_dst
, op
[0]);
1081 emit_math(SHADER_OPCODE_LOG2
, result_dst
, op
[0]);
1085 assert(!"not reached: should be handled by ir_explog_to_explog2");
1088 case ir_unop_sin_reduced
:
1089 emit_math(SHADER_OPCODE_SIN
, result_dst
, op
[0]);
1092 case ir_unop_cos_reduced
:
1093 emit_math(SHADER_OPCODE_COS
, result_dst
, op
[0]);
1098 assert(!"derivatives not valid in vertex shader");
1102 assert(!"not reached: should be handled by lower_noise");
1106 emit(ADD(result_dst
, op
[0], op
[1]));
1109 assert(!"not reached: should be handled by ir_sub_to_add_neg");
1113 if (ir
->type
->is_integer()) {
1114 /* For integer multiplication, the MUL uses the low 16 bits
1115 * of one of the operands (src0 on gen6, src1 on gen7). The
1116 * MACH accumulates in the contribution of the upper 16 bits
1119 * FINISHME: Emit just the MUL if we know an operand is small
1122 struct brw_reg acc
= retype(brw_acc_reg(), BRW_REGISTER_TYPE_D
);
1124 emit(MUL(acc
, op
[0], op
[1]));
1125 emit(MACH(dst_null_d(), op
[0], op
[1]));
1126 emit(MOV(result_dst
, src_reg(acc
)));
1128 emit(MUL(result_dst
, op
[0], op
[1]));
1132 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
1133 assert(ir
->type
->is_integer());
1134 emit_math(SHADER_OPCODE_INT_QUOTIENT
, result_dst
, op
[0], op
[1]);
1137 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
1138 assert(ir
->type
->is_integer());
1139 emit_math(SHADER_OPCODE_INT_REMAINDER
, result_dst
, op
[0], op
[1]);
1143 case ir_binop_greater
:
1144 case ir_binop_lequal
:
1145 case ir_binop_gequal
:
1146 case ir_binop_equal
:
1147 case ir_binop_nequal
: {
1148 emit(CMP(result_dst
, op
[0], op
[1],
1149 brw_conditional_for_comparison(ir
->operation
)));
1150 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1154 case ir_binop_all_equal
:
1155 /* "==" operator producing a scalar boolean. */
1156 if (ir
->operands
[0]->type
->is_vector() ||
1157 ir
->operands
[1]->type
->is_vector()) {
1158 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
1159 emit(MOV(result_dst
, src_reg(0)));
1160 inst
= emit(MOV(result_dst
, src_reg(1)));
1161 inst
->predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
1163 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_Z
));
1164 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1167 case ir_binop_any_nequal
:
1168 /* "!=" operator producing a scalar boolean. */
1169 if (ir
->operands
[0]->type
->is_vector() ||
1170 ir
->operands
[1]->type
->is_vector()) {
1171 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
1173 emit(MOV(result_dst
, src_reg(0)));
1174 inst
= emit(MOV(result_dst
, src_reg(1)));
1175 inst
->predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
1177 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_NZ
));
1178 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1183 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
1184 emit(MOV(result_dst
, src_reg(0)));
1186 inst
= emit(MOV(result_dst
, src_reg(1)));
1187 inst
->predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
1190 case ir_binop_logic_xor
:
1191 emit(XOR(result_dst
, op
[0], op
[1]));
1194 case ir_binop_logic_or
:
1195 emit(OR(result_dst
, op
[0], op
[1]));
1198 case ir_binop_logic_and
:
1199 emit(AND(result_dst
, op
[0], op
[1]));
1203 assert(ir
->operands
[0]->type
->is_vector());
1204 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1205 emit_dp(result_dst
, op
[0], op
[1], ir
->operands
[0]->type
->vector_elements
);
1209 emit_math(SHADER_OPCODE_SQRT
, result_dst
, op
[0]);
1212 emit_math(SHADER_OPCODE_RSQ
, result_dst
, op
[0]);
1215 case ir_unop_bitcast_i2f
:
1216 case ir_unop_bitcast_u2f
:
1217 this->result
= op
[0];
1218 this->result
.type
= BRW_REGISTER_TYPE_F
;
1221 case ir_unop_bitcast_f2i
:
1222 this->result
= op
[0];
1223 this->result
.type
= BRW_REGISTER_TYPE_D
;
1226 case ir_unop_bitcast_f2u
:
1227 this->result
= op
[0];
1228 this->result
.type
= BRW_REGISTER_TYPE_UD
;
1239 emit(MOV(result_dst
, op
[0]));
1243 emit(CMP(result_dst
, op
[0], src_reg(0.0f
), BRW_CONDITIONAL_NZ
));
1244 emit(AND(result_dst
, result_src
, src_reg(1)));
1249 emit(RNDZ(result_dst
, op
[0]));
1252 op
[0].negate
= !op
[0].negate
;
1253 inst
= emit(RNDD(result_dst
, op
[0]));
1254 this->result
.negate
= true;
1257 inst
= emit(RNDD(result_dst
, op
[0]));
1260 inst
= emit(FRC(result_dst
, op
[0]));
1262 case ir_unop_round_even
:
1263 emit(RNDE(result_dst
, op
[0]));
1267 if (intel
->gen
>= 6) {
1268 inst
= emit(BRW_OPCODE_SEL
, result_dst
, op
[0], op
[1]);
1269 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1271 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_L
));
1273 inst
= emit(BRW_OPCODE_SEL
, result_dst
, op
[0], op
[1]);
1274 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1278 if (intel
->gen
>= 6) {
1279 inst
= emit(BRW_OPCODE_SEL
, result_dst
, op
[0], op
[1]);
1280 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
1282 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_G
));
1284 inst
= emit(BRW_OPCODE_SEL
, result_dst
, op
[0], op
[1]);
1285 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1290 emit_math(SHADER_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1293 case ir_unop_bit_not
:
1294 inst
= emit(NOT(result_dst
, op
[0]));
1296 case ir_binop_bit_and
:
1297 inst
= emit(AND(result_dst
, op
[0], op
[1]));
1299 case ir_binop_bit_xor
:
1300 inst
= emit(XOR(result_dst
, op
[0], op
[1]));
1302 case ir_binop_bit_or
:
1303 inst
= emit(OR(result_dst
, op
[0], op
[1]));
1306 case ir_binop_lshift
:
1307 inst
= emit(BRW_OPCODE_SHL
, result_dst
, op
[0], op
[1]);
1310 case ir_binop_rshift
:
1311 if (ir
->type
->base_type
== GLSL_TYPE_INT
)
1312 inst
= emit(BRW_OPCODE_ASR
, result_dst
, op
[0], op
[1]);
1314 inst
= emit(BRW_OPCODE_SHR
, result_dst
, op
[0], op
[1]);
1317 case ir_binop_ubo_load
:
1318 assert(!"not yet supported");
1321 case ir_quadop_vector
:
1322 assert(!"not reached: should be handled by lower_quadop_vector");
1329 vec4_visitor::visit(ir_swizzle
*ir
)
1335 /* Note that this is only swizzles in expressions, not those on the left
1336 * hand side of an assignment, which do write masking. See ir_assignment
1340 ir
->val
->accept(this);
1342 assert(src
.file
!= BAD_FILE
);
1344 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1347 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1350 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1353 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1356 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1360 for (; i
< 4; i
++) {
1361 /* Replicate the last channel out. */
1362 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1365 src
.swizzle
= BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1371 vec4_visitor::visit(ir_dereference_variable
*ir
)
1373 const struct glsl_type
*type
= ir
->type
;
1374 dst_reg
*reg
= variable_storage(ir
->var
);
1377 fail("Failed to find variable storage for %s\n", ir
->var
->name
);
1378 this->result
= src_reg(brw_null_reg());
1382 this->result
= src_reg(*reg
);
1384 /* System values get their swizzle from the dst_reg writemask */
1385 if (ir
->var
->mode
== ir_var_system_value
)
1388 if (type
->is_scalar() || type
->is_vector() || type
->is_matrix())
1389 this->result
.swizzle
= swizzle_for_size(type
->vector_elements
);
1393 vec4_visitor::visit(ir_dereference_array
*ir
)
1395 ir_constant
*constant_index
;
1397 int element_size
= type_size(ir
->type
);
1399 constant_index
= ir
->array_index
->constant_expression_value();
1401 ir
->array
->accept(this);
1404 if (constant_index
) {
1405 src
.reg_offset
+= constant_index
->value
.i
[0] * element_size
;
1407 /* Variable index array dereference. It eats the "vec4" of the
1408 * base of the array and an index that offsets the Mesa register
1411 ir
->array_index
->accept(this);
1415 if (element_size
== 1) {
1416 index_reg
= this->result
;
1418 index_reg
= src_reg(this, glsl_type::int_type
);
1420 emit(MUL(dst_reg(index_reg
), this->result
, src_reg(element_size
)));
1424 src_reg temp
= src_reg(this, glsl_type::int_type
);
1426 emit(ADD(dst_reg(temp
), *src
.reladdr
, index_reg
));
1431 src
.reladdr
= ralloc(mem_ctx
, src_reg
);
1432 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1435 /* If the type is smaller than a vec4, replicate the last channel out. */
1436 if (ir
->type
->is_scalar() || ir
->type
->is_vector() || ir
->type
->is_matrix())
1437 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1439 src
.swizzle
= BRW_SWIZZLE_NOOP
;
1440 src
.type
= brw_type_for_base_type(ir
->type
);
1446 vec4_visitor::visit(ir_dereference_record
*ir
)
1449 const glsl_type
*struct_type
= ir
->record
->type
;
1452 ir
->record
->accept(this);
1454 for (i
= 0; i
< struct_type
->length
; i
++) {
1455 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1457 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1460 /* If the type is smaller than a vec4, replicate the last channel out. */
1461 if (ir
->type
->is_scalar() || ir
->type
->is_vector() || ir
->type
->is_matrix())
1462 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1464 this->result
.swizzle
= BRW_SWIZZLE_NOOP
;
1465 this->result
.type
= brw_type_for_base_type(ir
->type
);
1467 this->result
.reg_offset
+= offset
;
1471 * We want to be careful in assignment setup to hit the actual storage
1472 * instead of potentially using a temporary like we might with the
1473 * ir_dereference handler.
1476 get_assignment_lhs(ir_dereference
*ir
, vec4_visitor
*v
)
1478 /* The LHS must be a dereference. If the LHS is a variable indexed array
1479 * access of a vector, it must be separated into a series conditional moves
1480 * before reaching this point (see ir_vec_index_to_cond_assign).
1482 assert(ir
->as_dereference());
1483 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1485 assert(!deref_array
->array
->type
->is_vector());
1488 /* Use the rvalue deref handler for the most part. We'll ignore
1489 * swizzles in it and write swizzles using writemask, though.
1492 return dst_reg(v
->result
);
1496 vec4_visitor::emit_block_move(dst_reg
*dst
, src_reg
*src
,
1497 const struct glsl_type
*type
, uint32_t predicate
)
1499 if (type
->base_type
== GLSL_TYPE_STRUCT
) {
1500 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1501 emit_block_move(dst
, src
, type
->fields
.structure
[i
].type
, predicate
);
1506 if (type
->is_array()) {
1507 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1508 emit_block_move(dst
, src
, type
->fields
.array
, predicate
);
1513 if (type
->is_matrix()) {
1514 const struct glsl_type
*vec_type
;
1516 vec_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
1517 type
->vector_elements
, 1);
1519 for (int i
= 0; i
< type
->matrix_columns
; i
++) {
1520 emit_block_move(dst
, src
, vec_type
, predicate
);
1525 assert(type
->is_scalar() || type
->is_vector());
1527 dst
->type
= brw_type_for_base_type(type
);
1528 src
->type
= dst
->type
;
1530 dst
->writemask
= (1 << type
->vector_elements
) - 1;
1532 src
->swizzle
= swizzle_for_size(type
->vector_elements
);
1534 vec4_instruction
*inst
= emit(MOV(*dst
, *src
));
1535 inst
->predicate
= predicate
;
1542 /* If the RHS processing resulted in an instruction generating a
1543 * temporary value, and it would be easy to rewrite the instruction to
1544 * generate its result right into the LHS instead, do so. This ends
1545 * up reliably removing instructions where it can be tricky to do so
1546 * later without real UD chain information.
1549 vec4_visitor::try_rewrite_rhs_to_dst(ir_assignment
*ir
,
1552 vec4_instruction
*pre_rhs_inst
,
1553 vec4_instruction
*last_rhs_inst
)
1555 /* This could be supported, but it would take more smarts. */
1559 if (pre_rhs_inst
== last_rhs_inst
)
1560 return false; /* No instructions generated to work with. */
1562 /* Make sure the last instruction generated our source reg. */
1563 if (src
.file
!= GRF
||
1564 src
.file
!= last_rhs_inst
->dst
.file
||
1565 src
.reg
!= last_rhs_inst
->dst
.reg
||
1566 src
.reg_offset
!= last_rhs_inst
->dst
.reg_offset
||
1570 last_rhs_inst
->predicate
!= BRW_PREDICATE_NONE
)
1573 /* Check that that last instruction fully initialized the channels
1574 * we want to use, in the order we want to use them. We could
1575 * potentially reswizzle the operands of many instructions so that
1576 * we could handle out of order channels, but don't yet.
1579 for (unsigned i
= 0; i
< 4; i
++) {
1580 if (dst
.writemask
& (1 << i
)) {
1581 if (!(last_rhs_inst
->dst
.writemask
& (1 << i
)))
1584 if (BRW_GET_SWZ(src
.swizzle
, i
) != i
)
1589 /* Success! Rewrite the instruction. */
1590 last_rhs_inst
->dst
.file
= dst
.file
;
1591 last_rhs_inst
->dst
.reg
= dst
.reg
;
1592 last_rhs_inst
->dst
.reg_offset
= dst
.reg_offset
;
1593 last_rhs_inst
->dst
.reladdr
= dst
.reladdr
;
1594 last_rhs_inst
->dst
.writemask
&= dst
.writemask
;
1600 vec4_visitor::visit(ir_assignment
*ir
)
1602 dst_reg dst
= get_assignment_lhs(ir
->lhs
, this);
1603 uint32_t predicate
= BRW_PREDICATE_NONE
;
1605 if (!ir
->lhs
->type
->is_scalar() &&
1606 !ir
->lhs
->type
->is_vector()) {
1607 ir
->rhs
->accept(this);
1608 src_reg src
= this->result
;
1610 if (ir
->condition
) {
1611 emit_bool_to_cond_code(ir
->condition
, &predicate
);
1614 /* emit_block_move doesn't account for swizzles in the source register.
1615 * This should be ok, since the source register is a structure or an
1616 * array, and those can't be swizzled. But double-check to be sure.
1618 assert(src
.swizzle
==
1619 (ir
->rhs
->type
->is_matrix()
1620 ? swizzle_for_size(ir
->rhs
->type
->vector_elements
)
1621 : BRW_SWIZZLE_NOOP
));
1623 emit_block_move(&dst
, &src
, ir
->rhs
->type
, predicate
);
1627 /* Now we're down to just a scalar/vector with writemasks. */
1630 vec4_instruction
*pre_rhs_inst
, *last_rhs_inst
;
1631 pre_rhs_inst
= (vec4_instruction
*)this->instructions
.get_tail();
1633 ir
->rhs
->accept(this);
1635 last_rhs_inst
= (vec4_instruction
*)this->instructions
.get_tail();
1637 src_reg src
= this->result
;
1640 int first_enabled_chan
= 0;
1643 assert(ir
->lhs
->type
->is_vector() ||
1644 ir
->lhs
->type
->is_scalar());
1645 dst
.writemask
= ir
->write_mask
;
1647 for (int i
= 0; i
< 4; i
++) {
1648 if (dst
.writemask
& (1 << i
)) {
1649 first_enabled_chan
= BRW_GET_SWZ(src
.swizzle
, i
);
1654 /* Swizzle a small RHS vector into the channels being written.
1656 * glsl ir treats write_mask as dictating how many channels are
1657 * present on the RHS while in our instructions we need to make
1658 * those channels appear in the slots of the vec4 they're written to.
1660 for (int i
= 0; i
< 4; i
++) {
1661 if (dst
.writemask
& (1 << i
))
1662 swizzles
[i
] = BRW_GET_SWZ(src
.swizzle
, src_chan
++);
1664 swizzles
[i
] = first_enabled_chan
;
1666 src
.swizzle
= BRW_SWIZZLE4(swizzles
[0], swizzles
[1],
1667 swizzles
[2], swizzles
[3]);
1669 if (try_rewrite_rhs_to_dst(ir
, dst
, src
, pre_rhs_inst
, last_rhs_inst
)) {
1673 if (ir
->condition
) {
1674 emit_bool_to_cond_code(ir
->condition
, &predicate
);
1677 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1678 vec4_instruction
*inst
= emit(MOV(dst
, src
));
1679 inst
->predicate
= predicate
;
1687 vec4_visitor::emit_constant_values(dst_reg
*dst
, ir_constant
*ir
)
1689 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1690 foreach_list(node
, &ir
->components
) {
1691 ir_constant
*field_value
= (ir_constant
*)node
;
1693 emit_constant_values(dst
, field_value
);
1698 if (ir
->type
->is_array()) {
1699 for (unsigned int i
= 0; i
< ir
->type
->length
; i
++) {
1700 emit_constant_values(dst
, ir
->array_elements
[i
]);
1705 if (ir
->type
->is_matrix()) {
1706 for (int i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
1707 float *vec
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
1709 for (int j
= 0; j
< ir
->type
->vector_elements
; j
++) {
1710 dst
->writemask
= 1 << j
;
1711 dst
->type
= BRW_REGISTER_TYPE_F
;
1713 emit(MOV(*dst
, src_reg(vec
[j
])));
1720 int remaining_writemask
= (1 << ir
->type
->vector_elements
) - 1;
1722 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1723 if (!(remaining_writemask
& (1 << i
)))
1726 dst
->writemask
= 1 << i
;
1727 dst
->type
= brw_type_for_base_type(ir
->type
);
1729 /* Find other components that match the one we're about to
1730 * write. Emits fewer instructions for things like vec4(0.5,
1733 for (int j
= i
+ 1; j
< ir
->type
->vector_elements
; j
++) {
1734 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1735 if (ir
->value
.b
[i
] == ir
->value
.b
[j
])
1736 dst
->writemask
|= (1 << j
);
1738 /* u, i, and f storage all line up, so no need for a
1739 * switch case for comparing each type.
1741 if (ir
->value
.u
[i
] == ir
->value
.u
[j
])
1742 dst
->writemask
|= (1 << j
);
1746 switch (ir
->type
->base_type
) {
1747 case GLSL_TYPE_FLOAT
:
1748 emit(MOV(*dst
, src_reg(ir
->value
.f
[i
])));
1751 emit(MOV(*dst
, src_reg(ir
->value
.i
[i
])));
1753 case GLSL_TYPE_UINT
:
1754 emit(MOV(*dst
, src_reg(ir
->value
.u
[i
])));
1756 case GLSL_TYPE_BOOL
:
1757 emit(MOV(*dst
, src_reg(ir
->value
.b
[i
])));
1760 assert(!"Non-float/uint/int/bool constant");
1764 remaining_writemask
&= ~dst
->writemask
;
1770 vec4_visitor::visit(ir_constant
*ir
)
1772 dst_reg dst
= dst_reg(this, ir
->type
);
1773 this->result
= src_reg(dst
);
1775 emit_constant_values(&dst
, ir
);
1779 vec4_visitor::visit(ir_call
*ir
)
1781 assert(!"not reached");
1785 vec4_visitor::visit(ir_texture
*ir
)
1787 int sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
, prog
, &vp
->Base
);
1788 sampler
= vp
->Base
.SamplerUnits
[sampler
];
1790 /* Should be lowered by do_lower_texture_projection */
1791 assert(!ir
->projector
);
1793 /* Generate code to compute all the subexpression trees. This has to be
1794 * done before loading any values into MRFs for the sampler message since
1795 * generating these values may involve SEND messages that need the MRFs.
1798 if (ir
->coordinate
) {
1799 ir
->coordinate
->accept(this);
1800 coordinate
= this->result
;
1803 src_reg shadow_comparitor
;
1804 if (ir
->shadow_comparitor
) {
1805 ir
->shadow_comparitor
->accept(this);
1806 shadow_comparitor
= this->result
;
1809 src_reg lod
, dPdx
, dPdy
;
1814 ir
->lod_info
.lod
->accept(this);
1818 ir
->lod_info
.grad
.dPdx
->accept(this);
1819 dPdx
= this->result
;
1821 ir
->lod_info
.grad
.dPdy
->accept(this);
1822 dPdy
= this->result
;
1829 vec4_instruction
*inst
= NULL
;
1833 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXL
);
1836 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXD
);
1839 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXF
);
1842 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXS
);
1845 assert(!"TXB is not valid for vertex shaders.");
1848 /* Texel offsets go in the message header; Gen4 also requires headers. */
1849 inst
->header_present
= ir
->offset
|| intel
->gen
< 5;
1851 inst
->mlen
= inst
->header_present
+ 1; /* always at least one */
1852 inst
->sampler
= sampler
;
1853 inst
->dst
= dst_reg(this, ir
->type
);
1854 inst
->shadow_compare
= ir
->shadow_comparitor
!= NULL
;
1856 if (ir
->offset
!= NULL
&& ir
->op
!= ir_txf
)
1857 inst
->texture_offset
= brw_texture_offset(ir
->offset
->as_constant());
1859 /* MRF for the first parameter */
1860 int param_base
= inst
->base_mrf
+ inst
->header_present
;
1862 if (ir
->op
== ir_txs
) {
1863 int writemask
= intel
->gen
== 4 ? WRITEMASK_W
: WRITEMASK_X
;
1864 emit(MOV(dst_reg(MRF
, param_base
, ir
->lod_info
.lod
->type
, writemask
),
1867 int i
, coord_mask
= 0, zero_mask
= 0;
1868 /* Load the coordinate */
1869 /* FINISHME: gl_clamp_mask and saturate */
1870 for (i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++)
1871 coord_mask
|= (1 << i
);
1873 zero_mask
|= (1 << i
);
1875 if (ir
->offset
&& ir
->op
== ir_txf
) {
1876 /* It appears that the ld instruction used for txf does its
1877 * address bounds check before adding in the offset. To work
1878 * around this, just add the integer offset to the integer
1879 * texel coordinate, and don't put the offset in the header.
1881 ir_constant
*offset
= ir
->offset
->as_constant();
1884 for (int j
= 0; j
< ir
->coordinate
->type
->vector_elements
; j
++) {
1885 src_reg src
= coordinate
;
1886 src
.swizzle
= BRW_SWIZZLE4(BRW_GET_SWZ(src
.swizzle
, j
),
1887 BRW_GET_SWZ(src
.swizzle
, j
),
1888 BRW_GET_SWZ(src
.swizzle
, j
),
1889 BRW_GET_SWZ(src
.swizzle
, j
));
1890 emit(ADD(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, 1 << j
),
1891 src
, offset
->value
.i
[j
]));
1894 emit(MOV(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, coord_mask
),
1897 emit(MOV(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, zero_mask
),
1899 /* Load the shadow comparitor */
1900 if (ir
->shadow_comparitor
) {
1901 emit(MOV(dst_reg(MRF
, param_base
+ 1, ir
->shadow_comparitor
->type
,
1903 shadow_comparitor
));
1907 /* Load the LOD info */
1908 if (ir
->op
== ir_txl
) {
1910 if (intel
->gen
>= 5) {
1911 mrf
= param_base
+ 1;
1912 if (ir
->shadow_comparitor
) {
1913 writemask
= WRITEMASK_Y
;
1914 /* mlen already incremented */
1916 writemask
= WRITEMASK_X
;
1919 } else /* intel->gen == 4 */ {
1921 writemask
= WRITEMASK_Z
;
1923 emit(MOV(dst_reg(MRF
, mrf
, ir
->lod_info
.lod
->type
, writemask
), lod
));
1924 } else if (ir
->op
== ir_txf
) {
1925 emit(MOV(dst_reg(MRF
, param_base
, ir
->lod_info
.lod
->type
, WRITEMASK_W
),
1927 } else if (ir
->op
== ir_txd
) {
1928 const glsl_type
*type
= ir
->lod_info
.grad
.dPdx
->type
;
1930 if (intel
->gen
>= 5) {
1931 dPdx
.swizzle
= BRW_SWIZZLE4(SWIZZLE_X
,SWIZZLE_X
,SWIZZLE_Y
,SWIZZLE_Y
);
1932 dPdy
.swizzle
= BRW_SWIZZLE4(SWIZZLE_X
,SWIZZLE_X
,SWIZZLE_Y
,SWIZZLE_Y
);
1933 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_XZ
), dPdx
));
1934 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_YW
), dPdy
));
1937 if (ir
->type
->vector_elements
== 3) {
1938 dPdx
.swizzle
= BRW_SWIZZLE_ZZZZ
;
1939 dPdy
.swizzle
= BRW_SWIZZLE_ZZZZ
;
1940 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_X
), dPdx
));
1941 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_Y
), dPdy
));
1944 } else /* intel->gen == 4 */ {
1945 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_XYZ
), dPdx
));
1946 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_XYZ
), dPdy
));
1954 swizzle_result(ir
, src_reg(inst
->dst
), sampler
);
1958 vec4_visitor::swizzle_result(ir_texture
*ir
, src_reg orig_val
, int sampler
)
1960 this->result
= orig_val
;
1962 int s
= c
->key
.tex
.swizzles
[sampler
];
1964 if (ir
->op
== ir_txs
|| ir
->type
== glsl_type::float_type
1965 || s
== SWIZZLE_NOOP
)
1968 int zero_mask
= 0, one_mask
= 0, copy_mask
= 0;
1971 for (int i
= 0; i
< 4; i
++) {
1972 switch (GET_SWZ(s
, i
)) {
1974 zero_mask
|= (1 << i
);
1977 one_mask
|= (1 << i
);
1980 copy_mask
|= (1 << i
);
1981 swizzle
[i
] = GET_SWZ(s
, i
);
1986 this->result
= src_reg(this, ir
->type
);
1987 dst_reg
swizzled_result(this->result
);
1990 orig_val
.swizzle
= BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1991 swizzled_result
.writemask
= copy_mask
;
1992 emit(MOV(swizzled_result
, orig_val
));
1996 swizzled_result
.writemask
= zero_mask
;
1997 emit(MOV(swizzled_result
, src_reg(0.0f
)));
2001 swizzled_result
.writemask
= one_mask
;
2002 emit(MOV(swizzled_result
, src_reg(1.0f
)));
2007 vec4_visitor::visit(ir_return
*ir
)
2009 assert(!"not reached");
2013 vec4_visitor::visit(ir_discard
*ir
)
2015 assert(!"not reached");
2019 vec4_visitor::visit(ir_if
*ir
)
2021 /* Don't point the annotation at the if statement, because then it plus
2022 * the then and else blocks get printed.
2024 this->base_ir
= ir
->condition
;
2026 if (intel
->gen
== 6) {
2030 emit_bool_to_cond_code(ir
->condition
, &predicate
);
2031 emit(IF(predicate
));
2034 visit_instructions(&ir
->then_instructions
);
2036 if (!ir
->else_instructions
.is_empty()) {
2037 this->base_ir
= ir
->condition
;
2038 emit(BRW_OPCODE_ELSE
);
2040 visit_instructions(&ir
->else_instructions
);
2043 this->base_ir
= ir
->condition
;
2044 emit(BRW_OPCODE_ENDIF
);
2048 vec4_visitor::emit_ndc_computation()
2050 /* Get the position */
2051 src_reg pos
= src_reg(output_reg
[VERT_RESULT_HPOS
]);
2053 /* Build ndc coords, which are (x/w, y/w, z/w, 1/w) */
2054 dst_reg ndc
= dst_reg(this, glsl_type::vec4_type
);
2055 output_reg
[BRW_VERT_RESULT_NDC
] = ndc
;
2057 current_annotation
= "NDC";
2058 dst_reg ndc_w
= ndc
;
2059 ndc_w
.writemask
= WRITEMASK_W
;
2060 src_reg pos_w
= pos
;
2061 pos_w
.swizzle
= BRW_SWIZZLE4(SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
);
2062 emit_math(SHADER_OPCODE_RCP
, ndc_w
, pos_w
);
2064 dst_reg ndc_xyz
= ndc
;
2065 ndc_xyz
.writemask
= WRITEMASK_XYZ
;
2067 emit(MUL(ndc_xyz
, pos
, src_reg(ndc_w
)));
2071 vec4_visitor::emit_psiz_and_flags(struct brw_reg reg
)
2073 if (intel
->gen
< 6 &&
2074 ((c
->prog_data
.outputs_written
& BITFIELD64_BIT(VERT_RESULT_PSIZ
)) ||
2075 c
->key
.userclip_active
|| brw
->has_negative_rhw_bug
)) {
2076 dst_reg header1
= dst_reg(this, glsl_type::uvec4_type
);
2077 dst_reg header1_w
= header1
;
2078 header1_w
.writemask
= WRITEMASK_W
;
2081 emit(MOV(header1
, 0u));
2083 if (c
->prog_data
.outputs_written
& BITFIELD64_BIT(VERT_RESULT_PSIZ
)) {
2084 src_reg psiz
= src_reg(output_reg
[VERT_RESULT_PSIZ
]);
2086 current_annotation
= "Point size";
2087 emit(MUL(header1_w
, psiz
, src_reg((float)(1 << 11))));
2088 emit(AND(header1_w
, src_reg(header1_w
), 0x7ff << 8));
2091 current_annotation
= "Clipping flags";
2092 for (i
= 0; i
< c
->key
.nr_userclip_plane_consts
; i
++) {
2093 vec4_instruction
*inst
;
2095 inst
= emit(DP4(dst_null_f(), src_reg(output_reg
[VERT_RESULT_HPOS
]),
2096 src_reg(this->userplane
[i
])));
2097 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
2099 inst
= emit(OR(header1_w
, src_reg(header1_w
), 1u << i
));
2100 inst
->predicate
= BRW_PREDICATE_NORMAL
;
2103 /* i965 clipping workaround:
2104 * 1) Test for -ve rhw
2106 * set ndc = (0,0,0,0)
2109 * Later, clipping will detect ucp[6] and ensure the primitive is
2110 * clipped against all fixed planes.
2112 if (brw
->has_negative_rhw_bug
) {
2116 vec8(brw_null_reg()),
2118 brw_swizzle1(output_reg
[BRW_VERT_RESULT_NDC
], 3),
2121 brw_OR(p
, brw_writemask(header1
, WRITEMASK_W
), header1
, brw_imm_ud(1<<6));
2122 brw_MOV(p
, output_reg
[BRW_VERT_RESULT_NDC
], brw_imm_f(0));
2123 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
2127 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_UD
), src_reg(header1
)));
2128 } else if (intel
->gen
< 6) {
2129 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_UD
), 0u));
2131 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_D
), src_reg(0)));
2132 if (c
->prog_data
.outputs_written
& BITFIELD64_BIT(VERT_RESULT_PSIZ
)) {
2133 emit(MOV(brw_writemask(reg
, WRITEMASK_W
),
2134 src_reg(output_reg
[VERT_RESULT_PSIZ
])));
2140 vec4_visitor::emit_clip_distances(struct brw_reg reg
, int offset
)
2142 if (intel
->gen
< 6) {
2143 /* Clip distance slots are set aside in gen5, but they are not used. It
2144 * is not clear whether we actually need to set aside space for them,
2145 * but the performance cost is negligible.
2150 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
2152 * "If a linked set of shaders forming the vertex stage contains no
2153 * static write to gl_ClipVertex or gl_ClipDistance, but the
2154 * application has requested clipping against user clip planes through
2155 * the API, then the coordinate written to gl_Position is used for
2156 * comparison against the user clip planes."
2158 * This function is only called if the shader didn't write to
2159 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
2160 * if the user wrote to it; otherwise we use gl_Position.
2162 gl_vert_result clip_vertex
= VERT_RESULT_CLIP_VERTEX
;
2163 if (!(c
->prog_data
.outputs_written
2164 & BITFIELD64_BIT(VERT_RESULT_CLIP_VERTEX
))) {
2165 clip_vertex
= VERT_RESULT_HPOS
;
2168 for (int i
= 0; i
+ offset
< c
->key
.nr_userclip_plane_consts
&& i
< 4;
2170 emit(DP4(dst_reg(brw_writemask(reg
, 1 << i
)),
2171 src_reg(output_reg
[clip_vertex
]),
2172 src_reg(this->userplane
[i
+ offset
])));
2177 vec4_visitor::emit_generic_urb_slot(dst_reg reg
, int vert_result
)
2179 assert (vert_result
< VERT_RESULT_MAX
);
2180 reg
.type
= output_reg
[vert_result
].type
;
2181 current_annotation
= output_reg_annotation
[vert_result
];
2182 /* Copy the register, saturating if necessary */
2183 vec4_instruction
*inst
= emit(MOV(reg
,
2184 src_reg(output_reg
[vert_result
])));
2185 if ((vert_result
== VERT_RESULT_COL0
||
2186 vert_result
== VERT_RESULT_COL1
||
2187 vert_result
== VERT_RESULT_BFC0
||
2188 vert_result
== VERT_RESULT_BFC1
) &&
2189 c
->key
.clamp_vertex_color
) {
2190 inst
->saturate
= true;
2195 vec4_visitor::emit_urb_slot(int mrf
, int vert_result
)
2197 struct brw_reg hw_reg
= brw_message_reg(mrf
);
2198 dst_reg reg
= dst_reg(MRF
, mrf
);
2199 reg
.type
= BRW_REGISTER_TYPE_F
;
2201 switch (vert_result
) {
2202 case VERT_RESULT_PSIZ
:
2203 /* PSIZ is always in slot 0, and is coupled with other flags. */
2204 current_annotation
= "indices, point width, clip flags";
2205 emit_psiz_and_flags(hw_reg
);
2207 case BRW_VERT_RESULT_NDC
:
2208 current_annotation
= "NDC";
2209 emit(MOV(reg
, src_reg(output_reg
[BRW_VERT_RESULT_NDC
])));
2211 case BRW_VERT_RESULT_HPOS_DUPLICATE
:
2212 case VERT_RESULT_HPOS
:
2213 current_annotation
= "gl_Position";
2214 emit(MOV(reg
, src_reg(output_reg
[VERT_RESULT_HPOS
])));
2216 case VERT_RESULT_CLIP_DIST0
:
2217 case VERT_RESULT_CLIP_DIST1
:
2218 if (this->c
->key
.uses_clip_distance
) {
2219 emit_generic_urb_slot(reg
, vert_result
);
2221 current_annotation
= "user clip distances";
2222 emit_clip_distances(hw_reg
, (vert_result
- VERT_RESULT_CLIP_DIST0
) * 4);
2225 case BRW_VERT_RESULT_PAD
:
2226 /* No need to write to this slot */
2229 emit_generic_urb_slot(reg
, vert_result
);
2235 align_interleaved_urb_mlen(struct brw_context
*brw
, int mlen
)
2237 struct intel_context
*intel
= &brw
->intel
;
2239 if (intel
->gen
>= 6) {
2240 /* URB data written (does not include the message header reg) must
2241 * be a multiple of 256 bits, or 2 VS registers. See vol5c.5,
2242 * section 5.4.3.2.2: URB_INTERLEAVED.
2244 * URB entries are allocated on a multiple of 1024 bits, so an
2245 * extra 128 bits written here to make the end align to 256 is
2248 if ((mlen
% 2) != 1)
2256 * Generates the VUE payload plus the 1 or 2 URB write instructions to
2257 * complete the VS thread.
2259 * The VUE layout is documented in Volume 2a.
2262 vec4_visitor::emit_urb_writes()
2264 /* MRF 0 is reserved for the debugger, so start with message header
2269 /* In the process of generating our URB write message contents, we
2270 * may need to unspill a register or load from an array. Those
2271 * reads would use MRFs 14-15.
2273 int max_usable_mrf
= 13;
2275 /* The following assertion verifies that max_usable_mrf causes an
2276 * even-numbered amount of URB write data, which will meet gen6's
2277 * requirements for length alignment.
2279 assert ((max_usable_mrf
- base_mrf
) % 2 == 0);
2281 /* FINISHME: edgeflag */
2283 /* First mrf is the g0-based message header containing URB handles and such,
2284 * which is implied in VS_OPCODE_URB_WRITE.
2288 if (intel
->gen
< 6) {
2289 emit_ndc_computation();
2292 /* Set up the VUE data for the first URB write */
2294 for (slot
= 0; slot
< c
->prog_data
.vue_map
.num_slots
; ++slot
) {
2295 emit_urb_slot(mrf
++, c
->prog_data
.vue_map
.slot_to_vert_result
[slot
]);
2297 /* If this was max_usable_mrf, we can't fit anything more into this URB
2300 if (mrf
> max_usable_mrf
) {
2306 current_annotation
= "URB write";
2307 vec4_instruction
*inst
= emit(VS_OPCODE_URB_WRITE
);
2308 inst
->base_mrf
= base_mrf
;
2309 inst
->mlen
= align_interleaved_urb_mlen(brw
, mrf
- base_mrf
);
2310 inst
->eot
= (slot
>= c
->prog_data
.vue_map
.num_slots
);
2312 /* Optional second URB write */
2316 for (; slot
< c
->prog_data
.vue_map
.num_slots
; ++slot
) {
2317 assert(mrf
< max_usable_mrf
);
2319 emit_urb_slot(mrf
++, c
->prog_data
.vue_map
.slot_to_vert_result
[slot
]);
2322 current_annotation
= "URB write";
2323 inst
= emit(VS_OPCODE_URB_WRITE
);
2324 inst
->base_mrf
= base_mrf
;
2325 inst
->mlen
= align_interleaved_urb_mlen(brw
, mrf
- base_mrf
);
2327 /* URB destination offset. In the previous write, we got MRFs
2328 * 2-13 minus the one header MRF, so 12 regs. URB offset is in
2329 * URB row increments, and each of our MRFs is half of one of
2330 * those, since we're doing interleaved writes.
2332 inst
->offset
= (max_usable_mrf
- base_mrf
) / 2;
2337 vec4_visitor::get_scratch_offset(vec4_instruction
*inst
,
2338 src_reg
*reladdr
, int reg_offset
)
2340 /* Because we store the values to scratch interleaved like our
2341 * vertex data, we need to scale the vec4 index by 2.
2343 int message_header_scale
= 2;
2345 /* Pre-gen6, the message header uses byte offsets instead of vec4
2346 * (16-byte) offset units.
2349 message_header_scale
*= 16;
2352 src_reg index
= src_reg(this, glsl_type::int_type
);
2354 emit_before(inst
, ADD(dst_reg(index
), *reladdr
, src_reg(reg_offset
)));
2355 emit_before(inst
, MUL(dst_reg(index
),
2356 index
, src_reg(message_header_scale
)));
2360 return src_reg(reg_offset
* message_header_scale
);
2365 vec4_visitor::get_pull_constant_offset(vec4_instruction
*inst
,
2366 src_reg
*reladdr
, int reg_offset
)
2369 src_reg index
= src_reg(this, glsl_type::int_type
);
2371 emit_before(inst
, ADD(dst_reg(index
), *reladdr
, src_reg(reg_offset
)));
2373 /* Pre-gen6, the message header uses byte offsets instead of vec4
2374 * (16-byte) offset units.
2376 if (intel
->gen
< 6) {
2377 emit_before(inst
, MUL(dst_reg(index
), index
, src_reg(16)));
2382 int message_header_scale
= intel
->gen
< 6 ? 16 : 1;
2383 return src_reg(reg_offset
* message_header_scale
);
2388 * Emits an instruction before @inst to load the value named by @orig_src
2389 * from scratch space at @base_offset to @temp.
2392 vec4_visitor::emit_scratch_read(vec4_instruction
*inst
,
2393 dst_reg temp
, src_reg orig_src
,
2396 int reg_offset
= base_offset
+ orig_src
.reg_offset
;
2397 src_reg index
= get_scratch_offset(inst
, orig_src
.reladdr
, reg_offset
);
2399 emit_before(inst
, SCRATCH_READ(temp
, index
));
2403 * Emits an instruction after @inst to store the value to be written
2404 * to @orig_dst to scratch space at @base_offset, from @temp.
2407 vec4_visitor::emit_scratch_write(vec4_instruction
*inst
,
2408 src_reg temp
, dst_reg orig_dst
,
2411 int reg_offset
= base_offset
+ orig_dst
.reg_offset
;
2412 src_reg index
= get_scratch_offset(inst
, orig_dst
.reladdr
, reg_offset
);
2414 dst_reg dst
= dst_reg(brw_writemask(brw_vec8_grf(0, 0),
2415 orig_dst
.writemask
));
2416 vec4_instruction
*write
= SCRATCH_WRITE(dst
, temp
, index
);
2417 write
->predicate
= inst
->predicate
;
2418 write
->ir
= inst
->ir
;
2419 write
->annotation
= inst
->annotation
;
2420 inst
->insert_after(write
);
2424 * We can't generally support array access in GRF space, because a
2425 * single instruction's destination can only span 2 contiguous
2426 * registers. So, we send all GRF arrays that get variable index
2427 * access to scratch space.
2430 vec4_visitor::move_grf_array_access_to_scratch()
2432 int scratch_loc
[this->virtual_grf_count
];
2434 for (int i
= 0; i
< this->virtual_grf_count
; i
++) {
2435 scratch_loc
[i
] = -1;
2438 /* First, calculate the set of virtual GRFs that need to be punted
2439 * to scratch due to having any array access on them, and where in
2442 foreach_list(node
, &this->instructions
) {
2443 vec4_instruction
*inst
= (vec4_instruction
*)node
;
2445 if (inst
->dst
.file
== GRF
&& inst
->dst
.reladdr
&&
2446 scratch_loc
[inst
->dst
.reg
] == -1) {
2447 scratch_loc
[inst
->dst
.reg
] = c
->last_scratch
;
2448 c
->last_scratch
+= this->virtual_grf_sizes
[inst
->dst
.reg
] * 8 * 4;
2451 for (int i
= 0 ; i
< 3; i
++) {
2452 src_reg
*src
= &inst
->src
[i
];
2454 if (src
->file
== GRF
&& src
->reladdr
&&
2455 scratch_loc
[src
->reg
] == -1) {
2456 scratch_loc
[src
->reg
] = c
->last_scratch
;
2457 c
->last_scratch
+= this->virtual_grf_sizes
[src
->reg
] * 8 * 4;
2462 /* Now, for anything that will be accessed through scratch, rewrite
2463 * it to load/store. Note that this is a _safe list walk, because
2464 * we may generate a new scratch_write instruction after the one
2467 foreach_list_safe(node
, &this->instructions
) {
2468 vec4_instruction
*inst
= (vec4_instruction
*)node
;
2470 /* Set up the annotation tracking for new generated instructions. */
2472 current_annotation
= inst
->annotation
;
2474 if (inst
->dst
.file
== GRF
&& scratch_loc
[inst
->dst
.reg
] != -1) {
2475 src_reg temp
= src_reg(this, glsl_type::vec4_type
);
2477 emit_scratch_write(inst
, temp
, inst
->dst
, scratch_loc
[inst
->dst
.reg
]);
2479 inst
->dst
.file
= temp
.file
;
2480 inst
->dst
.reg
= temp
.reg
;
2481 inst
->dst
.reg_offset
= temp
.reg_offset
;
2482 inst
->dst
.reladdr
= NULL
;
2485 for (int i
= 0 ; i
< 3; i
++) {
2486 if (inst
->src
[i
].file
!= GRF
|| scratch_loc
[inst
->src
[i
].reg
] == -1)
2489 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
2491 emit_scratch_read(inst
, temp
, inst
->src
[i
],
2492 scratch_loc
[inst
->src
[i
].reg
]);
2494 inst
->src
[i
].file
= temp
.file
;
2495 inst
->src
[i
].reg
= temp
.reg
;
2496 inst
->src
[i
].reg_offset
= temp
.reg_offset
;
2497 inst
->src
[i
].reladdr
= NULL
;
2503 * Emits an instruction before @inst to load the value named by @orig_src
2504 * from the pull constant buffer (surface) at @base_offset to @temp.
2507 vec4_visitor::emit_pull_constant_load(vec4_instruction
*inst
,
2508 dst_reg temp
, src_reg orig_src
,
2511 int reg_offset
= base_offset
+ orig_src
.reg_offset
;
2512 src_reg index
= get_pull_constant_offset(inst
, orig_src
.reladdr
, reg_offset
);
2513 vec4_instruction
*load
;
2515 load
= new(mem_ctx
) vec4_instruction(this, VS_OPCODE_PULL_CONSTANT_LOAD
,
2517 load
->base_mrf
= 14;
2519 emit_before(inst
, load
);
2523 * Implements array access of uniforms by inserting a
2524 * PULL_CONSTANT_LOAD instruction.
2526 * Unlike temporary GRF array access (where we don't support it due to
2527 * the difficulty of doing relative addressing on instruction
2528 * destinations), we could potentially do array access of uniforms
2529 * that were loaded in GRF space as push constants. In real-world
2530 * usage we've seen, though, the arrays being used are always larger
2531 * than we could load as push constants, so just always move all
2532 * uniform array access out to a pull constant buffer.
2535 vec4_visitor::move_uniform_array_access_to_pull_constants()
2537 int pull_constant_loc
[this->uniforms
];
2539 for (int i
= 0; i
< this->uniforms
; i
++) {
2540 pull_constant_loc
[i
] = -1;
2543 /* Walk through and find array access of uniforms. Put a copy of that
2544 * uniform in the pull constant buffer.
2546 * Note that we don't move constant-indexed accesses to arrays. No
2547 * testing has been done of the performance impact of this choice.
2549 foreach_list_safe(node
, &this->instructions
) {
2550 vec4_instruction
*inst
= (vec4_instruction
*)node
;
2552 for (int i
= 0 ; i
< 3; i
++) {
2553 if (inst
->src
[i
].file
!= UNIFORM
|| !inst
->src
[i
].reladdr
)
2556 int uniform
= inst
->src
[i
].reg
;
2558 /* If this array isn't already present in the pull constant buffer,
2561 if (pull_constant_loc
[uniform
] == -1) {
2562 const float **values
= &prog_data
->param
[uniform
* 4];
2564 pull_constant_loc
[uniform
] = prog_data
->nr_pull_params
/ 4;
2566 for (int j
= 0; j
< uniform_size
[uniform
] * 4; j
++) {
2567 prog_data
->pull_param
[prog_data
->nr_pull_params
++] = values
[j
];
2571 /* Set up the annotation tracking for new generated instructions. */
2573 current_annotation
= inst
->annotation
;
2575 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
2577 emit_pull_constant_load(inst
, temp
, inst
->src
[i
],
2578 pull_constant_loc
[uniform
]);
2580 inst
->src
[i
].file
= temp
.file
;
2581 inst
->src
[i
].reg
= temp
.reg
;
2582 inst
->src
[i
].reg_offset
= temp
.reg_offset
;
2583 inst
->src
[i
].reladdr
= NULL
;
2587 /* Now there are no accesses of the UNIFORM file with a reladdr, so
2588 * no need to track them as larger-than-vec4 objects. This will be
2589 * relied on in cutting out unused uniform vectors from push
2592 split_uniform_registers();
2596 vec4_visitor::resolve_ud_negate(src_reg
*reg
)
2598 if (reg
->type
!= BRW_REGISTER_TYPE_UD
||
2602 src_reg temp
= src_reg(this, glsl_type::uvec4_type
);
2603 emit(BRW_OPCODE_MOV
, dst_reg(temp
), *reg
);
2607 vec4_visitor::vec4_visitor(struct brw_vs_compile
*c
,
2608 struct gl_shader_program
*prog
,
2609 struct brw_shader
*shader
)
2614 this->intel
= &brw
->intel
;
2615 this->ctx
= &intel
->ctx
;
2617 this->shader
= shader
;
2619 this->mem_ctx
= ralloc_context(NULL
);
2620 this->failed
= false;
2622 this->base_ir
= NULL
;
2623 this->current_annotation
= NULL
;
2626 this->vp
= (struct gl_vertex_program
*)
2627 prog
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
2628 this->prog_data
= &c
->prog_data
;
2630 this->variable_ht
= hash_table_ctor(0,
2631 hash_table_pointer_hash
,
2632 hash_table_pointer_compare
);
2634 this->virtual_grf_def
= NULL
;
2635 this->virtual_grf_use
= NULL
;
2636 this->virtual_grf_sizes
= NULL
;
2637 this->virtual_grf_count
= 0;
2638 this->virtual_grf_reg_map
= NULL
;
2639 this->virtual_grf_reg_count
= 0;
2640 this->virtual_grf_array_size
= 0;
2641 this->live_intervals_valid
= false;
2643 this->max_grf
= intel
->gen
>= 7 ? GEN7_MRF_HACK_START
: BRW_MAX_GRF
;
2648 vec4_visitor::~vec4_visitor()
2650 ralloc_free(this->mem_ctx
);
2651 hash_table_dtor(this->variable_ht
);
2656 vec4_visitor::fail(const char *format
, ...)
2666 va_start(va
, format
);
2667 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
2669 msg
= ralloc_asprintf(mem_ctx
, "VS compile failed: %s\n", msg
);
2671 this->fail_msg
= msg
;
2673 if (INTEL_DEBUG
& DEBUG_VS
) {
2674 fprintf(stderr
, "%s", msg
);
2678 } /* namespace brw */