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, \
124 /** Gen4 predicated IF. */
126 vec4_visitor::IF(uint32_t predicate
)
128 vec4_instruction
*inst
;
130 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_IF
);
131 inst
->predicate
= predicate
;
136 /** Gen6+ IF with embedded comparison. */
138 vec4_visitor::IF(src_reg src0
, src_reg src1
, uint32_t condition
)
140 assert(intel
->gen
>= 6);
142 vec4_instruction
*inst
;
144 resolve_ud_negate(&src0
);
145 resolve_ud_negate(&src1
);
147 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_IF
, dst_null_d(),
149 inst
->conditional_mod
= condition
;
155 * CMP: Sets the low bit of the destination channels with the result
156 * of the comparison, while the upper bits are undefined, and updates
157 * the flag register with the packed 16 bits of the result.
160 vec4_visitor::CMP(dst_reg dst
, src_reg src0
, src_reg src1
, uint32_t condition
)
162 vec4_instruction
*inst
;
164 /* original gen4 does type conversion to the destination type
165 * before before comparison, producing garbage results for floating
168 if (intel
->gen
== 4) {
169 dst
.type
= src0
.type
;
170 if (dst
.file
== HW_REG
)
171 dst
.fixed_hw_reg
.type
= dst
.type
;
174 resolve_ud_negate(&src0
);
175 resolve_ud_negate(&src1
);
177 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_CMP
, dst
, src0
, src1
);
178 inst
->conditional_mod
= condition
;
184 vec4_visitor::SCRATCH_READ(dst_reg dst
, src_reg index
)
186 vec4_instruction
*inst
;
188 inst
= new(mem_ctx
) vec4_instruction(this, VS_OPCODE_SCRATCH_READ
,
197 vec4_visitor::SCRATCH_WRITE(dst_reg dst
, src_reg src
, src_reg index
)
199 vec4_instruction
*inst
;
201 inst
= new(mem_ctx
) vec4_instruction(this, VS_OPCODE_SCRATCH_WRITE
,
210 vec4_visitor::emit_dp(dst_reg dst
, src_reg src0
, src_reg src1
, unsigned elements
)
212 static enum opcode dot_opcodes
[] = {
213 BRW_OPCODE_DP2
, BRW_OPCODE_DP3
, BRW_OPCODE_DP4
216 emit(dot_opcodes
[elements
- 2], dst
, src0
, src1
);
220 vec4_visitor::emit_math1_gen6(enum opcode opcode
, dst_reg dst
, src_reg src
)
222 /* The gen6 math instruction ignores the source modifiers --
223 * swizzle, abs, negate, and at least some parts of the register
224 * region description.
226 * While it would seem that this MOV could be avoided at this point
227 * in the case that the swizzle is matched up with the destination
228 * writemask, note that uniform packing and register allocation
229 * could rearrange our swizzle, so let's leave this matter up to
230 * copy propagation later.
232 src_reg temp_src
= src_reg(this, glsl_type::vec4_type
);
233 emit(MOV(dst_reg(temp_src
), src
));
235 if (dst
.writemask
!= WRITEMASK_XYZW
) {
236 /* The gen6 math instruction must be align1, so we can't do
239 dst_reg temp_dst
= dst_reg(this, glsl_type::vec4_type
);
241 emit(opcode
, temp_dst
, temp_src
);
243 emit(MOV(dst
, src_reg(temp_dst
)));
245 emit(opcode
, dst
, temp_src
);
250 vec4_visitor::emit_math1_gen4(enum opcode opcode
, dst_reg dst
, src_reg src
)
252 vec4_instruction
*inst
= emit(opcode
, dst
, src
);
258 vec4_visitor::emit_math(opcode opcode
, dst_reg dst
, src_reg src
)
261 case SHADER_OPCODE_RCP
:
262 case SHADER_OPCODE_RSQ
:
263 case SHADER_OPCODE_SQRT
:
264 case SHADER_OPCODE_EXP2
:
265 case SHADER_OPCODE_LOG2
:
266 case SHADER_OPCODE_SIN
:
267 case SHADER_OPCODE_COS
:
270 assert(!"not reached: bad math opcode");
274 if (intel
->gen
>= 7) {
275 emit(opcode
, dst
, src
);
276 } else if (intel
->gen
== 6) {
277 return emit_math1_gen6(opcode
, dst
, src
);
279 return emit_math1_gen4(opcode
, dst
, src
);
284 vec4_visitor::emit_math2_gen6(enum opcode opcode
,
285 dst_reg dst
, src_reg src0
, src_reg src1
)
289 /* The gen6 math instruction ignores the source modifiers --
290 * swizzle, abs, negate, and at least some parts of the register
291 * region description. Move the sources to temporaries to make it
295 expanded
= src_reg(this, glsl_type::vec4_type
);
296 expanded
.type
= src0
.type
;
297 emit(MOV(dst_reg(expanded
), src0
));
300 expanded
= src_reg(this, glsl_type::vec4_type
);
301 expanded
.type
= src1
.type
;
302 emit(MOV(dst_reg(expanded
), src1
));
305 if (dst
.writemask
!= WRITEMASK_XYZW
) {
306 /* The gen6 math instruction must be align1, so we can't do
309 dst_reg temp_dst
= dst_reg(this, glsl_type::vec4_type
);
310 temp_dst
.type
= dst
.type
;
312 emit(opcode
, temp_dst
, src0
, src1
);
314 emit(MOV(dst
, src_reg(temp_dst
)));
316 emit(opcode
, dst
, src0
, src1
);
321 vec4_visitor::emit_math2_gen4(enum opcode opcode
,
322 dst_reg dst
, src_reg src0
, src_reg src1
)
324 vec4_instruction
*inst
= emit(opcode
, dst
, src0
, src1
);
330 vec4_visitor::emit_math(enum opcode opcode
,
331 dst_reg dst
, src_reg src0
, src_reg src1
)
334 case SHADER_OPCODE_POW
:
335 case SHADER_OPCODE_INT_QUOTIENT
:
336 case SHADER_OPCODE_INT_REMAINDER
:
339 assert(!"not reached: unsupported binary math opcode");
343 if (intel
->gen
>= 7) {
344 emit(opcode
, dst
, src0
, src1
);
345 } else if (intel
->gen
== 6) {
346 return emit_math2_gen6(opcode
, dst
, src0
, src1
);
348 return emit_math2_gen4(opcode
, dst
, src0
, src1
);
353 vec4_visitor::visit_instructions(const exec_list
*list
)
355 foreach_list(node
, list
) {
356 ir_instruction
*ir
= (ir_instruction
*)node
;
365 type_size(const struct glsl_type
*type
)
370 switch (type
->base_type
) {
373 case GLSL_TYPE_FLOAT
:
375 if (type
->is_matrix()) {
376 return type
->matrix_columns
;
378 /* Regardless of size of vector, it gets a vec4. This is bad
379 * packing for things like floats, but otherwise arrays become a
380 * mess. Hopefully a later pass over the code can pack scalars
381 * down if appropriate.
385 case GLSL_TYPE_ARRAY
:
386 assert(type
->length
> 0);
387 return type_size(type
->fields
.array
) * type
->length
;
388 case GLSL_TYPE_STRUCT
:
390 for (i
= 0; i
< type
->length
; i
++) {
391 size
+= type_size(type
->fields
.structure
[i
].type
);
394 case GLSL_TYPE_SAMPLER
:
395 /* Samplers take up one slot in UNIFORMS[], but they're baked in
406 vec4_visitor::virtual_grf_alloc(int size
)
408 if (virtual_grf_array_size
<= virtual_grf_count
) {
409 if (virtual_grf_array_size
== 0)
410 virtual_grf_array_size
= 16;
412 virtual_grf_array_size
*= 2;
413 virtual_grf_sizes
= reralloc(mem_ctx
, virtual_grf_sizes
, int,
414 virtual_grf_array_size
);
415 virtual_grf_reg_map
= reralloc(mem_ctx
, virtual_grf_reg_map
, int,
416 virtual_grf_array_size
);
418 virtual_grf_reg_map
[virtual_grf_count
] = virtual_grf_reg_count
;
419 virtual_grf_reg_count
+= size
;
420 virtual_grf_sizes
[virtual_grf_count
] = size
;
421 return virtual_grf_count
++;
424 src_reg::src_reg(class vec4_visitor
*v
, const struct glsl_type
*type
)
429 this->reg
= v
->virtual_grf_alloc(type_size(type
));
431 if (type
->is_array() || type
->is_record()) {
432 this->swizzle
= BRW_SWIZZLE_NOOP
;
434 this->swizzle
= swizzle_for_size(type
->vector_elements
);
437 this->type
= brw_type_for_base_type(type
);
440 dst_reg::dst_reg(class vec4_visitor
*v
, const struct glsl_type
*type
)
445 this->reg
= v
->virtual_grf_alloc(type_size(type
));
447 if (type
->is_array() || type
->is_record()) {
448 this->writemask
= WRITEMASK_XYZW
;
450 this->writemask
= (1 << type
->vector_elements
) - 1;
453 this->type
= brw_type_for_base_type(type
);
456 /* Our support for uniforms is piggy-backed on the struct
457 * gl_fragment_program, because that's where the values actually
458 * get stored, rather than in some global gl_shader_program uniform
462 vec4_visitor::setup_uniform_values(int loc
, const glsl_type
*type
)
464 unsigned int offset
= 0;
465 float *values
= &this->vp
->Base
.Parameters
->ParameterValues
[loc
][0].f
;
467 if (type
->is_matrix()) {
468 const glsl_type
*column
= type
->column_type();
470 for (unsigned int i
= 0; i
< type
->matrix_columns
; i
++) {
471 offset
+= setup_uniform_values(loc
+ offset
, column
);
477 switch (type
->base_type
) {
478 case GLSL_TYPE_FLOAT
:
482 for (unsigned int i
= 0; i
< type
->vector_elements
; i
++) {
483 c
->prog_data
.param
[this->uniforms
* 4 + i
] = &values
[i
];
486 /* Set up pad elements to get things aligned to a vec4 boundary. */
487 for (unsigned int i
= type
->vector_elements
; i
< 4; i
++) {
488 static float zero
= 0;
490 c
->prog_data
.param
[this->uniforms
* 4 + i
] = &zero
;
493 /* Track the size of this uniform vector, for future packing of
496 this->uniform_vector_size
[this->uniforms
] = type
->vector_elements
;
501 case GLSL_TYPE_STRUCT
:
502 for (unsigned int i
= 0; i
< type
->length
; i
++) {
503 offset
+= setup_uniform_values(loc
+ offset
,
504 type
->fields
.structure
[i
].type
);
508 case GLSL_TYPE_ARRAY
:
509 for (unsigned int i
= 0; i
< type
->length
; i
++) {
510 offset
+= setup_uniform_values(loc
+ offset
, type
->fields
.array
);
514 case GLSL_TYPE_SAMPLER
:
515 /* The sampler takes up a slot, but we don't use any values from it. */
519 assert(!"not reached");
525 vec4_visitor::setup_uniform_clipplane_values()
527 gl_clip_plane
*clip_planes
= brw_select_clip_planes(ctx
);
529 /* Pre-Gen6, we compact clip planes. For example, if the user
530 * enables just clip planes 0, 1, and 3, we will enable clip planes
531 * 0, 1, and 2 in the hardware, and we'll move clip plane 3 to clip
532 * plane 2. This simplifies the implementation of the Gen6 clip
535 * In Gen6 and later, we don't compact clip planes, because this
536 * simplifies the implementation of gl_ClipDistance.
538 int compacted_clipplane_index
= 0;
539 for (int i
= 0; i
< c
->key
.nr_userclip_plane_consts
; ++i
) {
540 if (intel
->gen
< 6 &&
541 !(c
->key
.userclip_planes_enabled_gen_4_5
& (1 << i
))) {
544 this->uniform_vector_size
[this->uniforms
] = 4;
545 this->userplane
[compacted_clipplane_index
] = dst_reg(UNIFORM
, this->uniforms
);
546 this->userplane
[compacted_clipplane_index
].type
= BRW_REGISTER_TYPE_F
;
547 for (int j
= 0; j
< 4; ++j
) {
548 c
->prog_data
.param
[this->uniforms
* 4 + j
] = &clip_planes
[i
][j
];
550 ++compacted_clipplane_index
;
555 /* Our support for builtin uniforms is even scarier than non-builtin.
556 * It sits on top of the PROG_STATE_VAR parameters that are
557 * automatically updated from GL context state.
560 vec4_visitor::setup_builtin_uniform_values(ir_variable
*ir
)
562 const ir_state_slot
*const slots
= ir
->state_slots
;
563 assert(ir
->state_slots
!= NULL
);
565 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
566 /* This state reference has already been setup by ir_to_mesa,
567 * but we'll get the same index back here. We can reference
568 * ParameterValues directly, since unlike brw_fs.cpp, we never
569 * add new state references during compile.
571 int index
= _mesa_add_state_reference(this->vp
->Base
.Parameters
,
572 (gl_state_index
*)slots
[i
].tokens
);
573 float *values
= &this->vp
->Base
.Parameters
->ParameterValues
[index
][0].f
;
575 this->uniform_vector_size
[this->uniforms
] = 0;
576 /* Add each of the unique swizzled channels of the element.
577 * This will end up matching the size of the glsl_type of this field.
580 for (unsigned int j
= 0; j
< 4; j
++) {
581 int swiz
= GET_SWZ(slots
[i
].swizzle
, j
);
584 c
->prog_data
.param
[this->uniforms
* 4 + j
] = &values
[swiz
];
585 if (swiz
<= last_swiz
)
586 this->uniform_vector_size
[this->uniforms
]++;
593 vec4_visitor::variable_storage(ir_variable
*var
)
595 return (dst_reg
*)hash_table_find(this->variable_ht
, var
);
599 vec4_visitor::emit_bool_to_cond_code(ir_rvalue
*ir
, uint32_t *predicate
)
601 ir_expression
*expr
= ir
->as_expression();
603 *predicate
= BRW_PREDICATE_NORMAL
;
607 vec4_instruction
*inst
;
609 assert(expr
->get_num_operands() <= 2);
610 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
611 expr
->operands
[i
]->accept(this);
612 op
[i
] = this->result
;
614 resolve_ud_negate(&op
[i
]);
617 switch (expr
->operation
) {
618 case ir_unop_logic_not
:
619 inst
= emit(AND(dst_null_d(), op
[0], src_reg(1)));
620 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
623 case ir_binop_logic_xor
:
624 inst
= emit(XOR(dst_null_d(), op
[0], op
[1]));
625 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
628 case ir_binop_logic_or
:
629 inst
= emit(OR(dst_null_d(), op
[0], op
[1]));
630 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
633 case ir_binop_logic_and
:
634 inst
= emit(AND(dst_null_d(), op
[0], op
[1]));
635 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
639 if (intel
->gen
>= 6) {
640 emit(CMP(dst_null_d(), op
[0], src_reg(0.0f
), BRW_CONDITIONAL_NZ
));
642 inst
= emit(MOV(dst_null_f(), op
[0]));
643 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
648 if (intel
->gen
>= 6) {
649 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
651 inst
= emit(MOV(dst_null_d(), op
[0]));
652 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
656 case ir_binop_all_equal
:
657 inst
= emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
658 *predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
661 case ir_binop_any_nequal
:
662 inst
= emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
663 *predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
667 inst
= emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
668 *predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
671 case ir_binop_greater
:
672 case ir_binop_gequal
:
674 case ir_binop_lequal
:
676 case ir_binop_nequal
:
677 emit(CMP(dst_null_d(), op
[0], op
[1],
678 brw_conditional_for_comparison(expr
->operation
)));
682 assert(!"not reached");
690 resolve_ud_negate(&this->result
);
692 if (intel
->gen
>= 6) {
693 vec4_instruction
*inst
= emit(AND(dst_null_d(),
694 this->result
, src_reg(1)));
695 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
697 vec4_instruction
*inst
= emit(MOV(dst_null_d(), this->result
));
698 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
703 * Emit a gen6 IF statement with the comparison folded into the IF
707 vec4_visitor::emit_if_gen6(ir_if
*ir
)
709 ir_expression
*expr
= ir
->condition
->as_expression();
715 assert(expr
->get_num_operands() <= 2);
716 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
717 expr
->operands
[i
]->accept(this);
718 op
[i
] = this->result
;
721 switch (expr
->operation
) {
722 case ir_unop_logic_not
:
723 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_Z
));
726 case ir_binop_logic_xor
:
727 emit(IF(op
[0], op
[1], BRW_CONDITIONAL_NZ
));
730 case ir_binop_logic_or
:
731 temp
= dst_reg(this, glsl_type::bool_type
);
732 emit(OR(temp
, op
[0], op
[1]));
733 emit(IF(src_reg(temp
), src_reg(0), BRW_CONDITIONAL_NZ
));
736 case ir_binop_logic_and
:
737 temp
= dst_reg(this, glsl_type::bool_type
);
738 emit(AND(temp
, op
[0], op
[1]));
739 emit(IF(src_reg(temp
), src_reg(0), BRW_CONDITIONAL_NZ
));
743 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
747 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
750 case ir_binop_greater
:
751 case ir_binop_gequal
:
753 case ir_binop_lequal
:
755 case ir_binop_nequal
:
756 emit(IF(op
[0], op
[1],
757 brw_conditional_for_comparison(expr
->operation
)));
760 case ir_binop_all_equal
:
761 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
762 emit(IF(BRW_PREDICATE_ALIGN16_ALL4H
));
765 case ir_binop_any_nequal
:
766 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
767 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H
));
771 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
772 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H
));
776 assert(!"not reached");
777 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
783 ir
->condition
->accept(this);
785 emit(IF(this->result
, src_reg(0), BRW_CONDITIONAL_NZ
));
789 vec4_visitor::visit(ir_variable
*ir
)
793 if (variable_storage(ir
))
798 reg
= new(mem_ctx
) dst_reg(ATTR
, ir
->location
);
800 /* Do GL_FIXED rescaling for GLES2.0. Our GL_FIXED attributes
801 * come in as floating point conversions of the integer values.
803 for (int i
= ir
->location
; i
< ir
->location
+ type_size(ir
->type
); i
++) {
804 if (!c
->key
.gl_fixed_input_size
[i
])
808 dst
.type
= brw_type_for_base_type(ir
->type
);
809 dst
.writemask
= (1 << c
->key
.gl_fixed_input_size
[i
]) - 1;
810 emit(MUL(dst
, src_reg(dst
), src_reg(1.0f
/ 65536.0f
)));
815 reg
= new(mem_ctx
) dst_reg(this, ir
->type
);
817 for (int i
= 0; i
< type_size(ir
->type
); i
++) {
818 output_reg
[ir
->location
+ i
] = *reg
;
819 output_reg
[ir
->location
+ i
].reg_offset
= i
;
820 output_reg
[ir
->location
+ i
].type
=
821 brw_type_for_base_type(ir
->type
->get_scalar_type());
822 output_reg_annotation
[ir
->location
+ i
] = ir
->name
;
827 case ir_var_temporary
:
828 reg
= new(mem_ctx
) dst_reg(this, ir
->type
);
832 reg
= new(this->mem_ctx
) dst_reg(UNIFORM
, this->uniforms
);
834 /* Thanks to the lower_ubo_reference pass, we will see only
835 * ir_binop_ubo_load expressions and not ir_dereference_variable for UBO
836 * variables, so no need for them to be in variable_ht.
838 if (ir
->uniform_block
!= -1)
841 /* Track how big the whole uniform variable is, in case we need to put a
842 * copy of its data into pull constants for array access.
844 this->uniform_size
[this->uniforms
] = type_size(ir
->type
);
846 if (!strncmp(ir
->name
, "gl_", 3)) {
847 setup_builtin_uniform_values(ir
);
849 setup_uniform_values(ir
->location
, ir
->type
);
853 case ir_var_system_value
:
854 /* VertexID is stored by the VF as the last vertex element, but
855 * we don't represent it with a flag in inputs_read, so we call
856 * it VERT_ATTRIB_MAX, which setup_attributes() picks up on.
858 reg
= new(mem_ctx
) dst_reg(ATTR
, VERT_ATTRIB_MAX
);
859 prog_data
->uses_vertexid
= true;
861 switch (ir
->location
) {
862 case SYSTEM_VALUE_VERTEX_ID
:
863 reg
->writemask
= WRITEMASK_X
;
865 case SYSTEM_VALUE_INSTANCE_ID
:
866 reg
->writemask
= WRITEMASK_Y
;
869 assert(!"not reached");
875 assert(!"not reached");
878 reg
->type
= brw_type_for_base_type(ir
->type
);
879 hash_table_insert(this->variable_ht
, reg
, ir
);
883 vec4_visitor::visit(ir_loop
*ir
)
887 /* We don't want debugging output to print the whole body of the
888 * loop as the annotation.
890 this->base_ir
= NULL
;
892 if (ir
->counter
!= NULL
) {
893 this->base_ir
= ir
->counter
;
894 ir
->counter
->accept(this);
895 counter
= *(variable_storage(ir
->counter
));
897 if (ir
->from
!= NULL
) {
898 this->base_ir
= ir
->from
;
899 ir
->from
->accept(this);
901 emit(MOV(counter
, this->result
));
908 this->base_ir
= ir
->to
;
909 ir
->to
->accept(this);
911 emit(CMP(dst_null_d(), src_reg(counter
), this->result
,
912 brw_conditional_for_comparison(ir
->cmp
)));
914 vec4_instruction
*inst
= emit(BRW_OPCODE_BREAK
);
915 inst
->predicate
= BRW_PREDICATE_NORMAL
;
918 visit_instructions(&ir
->body_instructions
);
922 this->base_ir
= ir
->increment
;
923 ir
->increment
->accept(this);
924 emit(ADD(counter
, src_reg(counter
), this->result
));
927 emit(BRW_OPCODE_WHILE
);
931 vec4_visitor::visit(ir_loop_jump
*ir
)
934 case ir_loop_jump::jump_break
:
935 emit(BRW_OPCODE_BREAK
);
937 case ir_loop_jump::jump_continue
:
938 emit(BRW_OPCODE_CONTINUE
);
945 vec4_visitor::visit(ir_function_signature
*ir
)
952 vec4_visitor::visit(ir_function
*ir
)
954 /* Ignore function bodies other than main() -- we shouldn't see calls to
955 * them since they should all be inlined.
957 if (strcmp(ir
->name
, "main") == 0) {
958 const ir_function_signature
*sig
;
961 sig
= ir
->matching_signature(&empty
);
965 visit_instructions(&sig
->body
);
970 vec4_visitor::try_emit_sat(ir_expression
*ir
)
972 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
976 sat_src
->accept(this);
977 src_reg src
= this->result
;
979 this->result
= src_reg(this, ir
->type
);
980 vec4_instruction
*inst
;
981 inst
= emit(MOV(dst_reg(this->result
), src
));
982 inst
->saturate
= true;
988 vec4_visitor::emit_bool_comparison(unsigned int op
,
989 dst_reg dst
, src_reg src0
, src_reg src1
)
991 /* original gen4 does destination conversion before comparison. */
993 dst
.type
= src0
.type
;
995 emit(CMP(dst
, src0
, src1
, brw_conditional_for_comparison(op
)));
997 dst
.type
= BRW_REGISTER_TYPE_D
;
998 emit(AND(dst
, src_reg(dst
), src_reg(0x1)));
1002 vec4_visitor::emit_minmax(uint32_t conditionalmod
, dst_reg dst
,
1003 src_reg src0
, src_reg src1
)
1005 vec4_instruction
*inst
;
1007 if (intel
->gen
>= 6) {
1008 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
1009 inst
->conditional_mod
= conditionalmod
;
1011 emit(CMP(dst
, src0
, src1
, conditionalmod
));
1013 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
1014 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1019 vec4_visitor::visit(ir_expression
*ir
)
1021 unsigned int operand
;
1022 src_reg op
[Elements(ir
->operands
)];
1025 vec4_instruction
*inst
;
1027 if (try_emit_sat(ir
))
1030 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1031 this->result
.file
= BAD_FILE
;
1032 ir
->operands
[operand
]->accept(this);
1033 if (this->result
.file
== BAD_FILE
) {
1034 printf("Failed to get tree for expression operand:\n");
1035 ir
->operands
[operand
]->print();
1038 op
[operand
] = this->result
;
1040 /* Matrix expression operands should have been broken down to vector
1041 * operations already.
1043 assert(!ir
->operands
[operand
]->type
->is_matrix());
1046 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1047 if (ir
->operands
[1]) {
1048 vector_elements
= MAX2(vector_elements
,
1049 ir
->operands
[1]->type
->vector_elements
);
1052 this->result
.file
= BAD_FILE
;
1054 /* Storage for our result. Ideally for an assignment we'd be using
1055 * the actual storage for the result here, instead.
1057 result_src
= src_reg(this, ir
->type
);
1058 /* convenience for the emit functions below. */
1059 result_dst
= dst_reg(result_src
);
1060 /* If nothing special happens, this is the result. */
1061 this->result
= result_src
;
1062 /* Limit writes to the channels that will be used by result_src later.
1063 * This does limit this temp's use as a temporary for multi-instruction
1066 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1068 switch (ir
->operation
) {
1069 case ir_unop_logic_not
:
1070 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
1071 * ones complement of the whole register, not just bit 0.
1073 emit(XOR(result_dst
, op
[0], src_reg(1)));
1076 op
[0].negate
= !op
[0].negate
;
1077 this->result
= op
[0];
1081 op
[0].negate
= false;
1082 this->result
= op
[0];
1086 emit(MOV(result_dst
, src_reg(0.0f
)));
1088 emit(CMP(dst_null_d(), op
[0], src_reg(0.0f
), BRW_CONDITIONAL_G
));
1089 inst
= emit(MOV(result_dst
, src_reg(1.0f
)));
1090 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1092 emit(CMP(dst_null_d(), op
[0], src_reg(0.0f
), BRW_CONDITIONAL_L
));
1093 inst
= emit(MOV(result_dst
, src_reg(-1.0f
)));
1094 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1099 emit_math(SHADER_OPCODE_RCP
, result_dst
, op
[0]);
1103 emit_math(SHADER_OPCODE_EXP2
, result_dst
, op
[0]);
1106 emit_math(SHADER_OPCODE_LOG2
, result_dst
, op
[0]);
1110 assert(!"not reached: should be handled by ir_explog_to_explog2");
1113 case ir_unop_sin_reduced
:
1114 emit_math(SHADER_OPCODE_SIN
, result_dst
, op
[0]);
1117 case ir_unop_cos_reduced
:
1118 emit_math(SHADER_OPCODE_COS
, result_dst
, op
[0]);
1123 assert(!"derivatives not valid in vertex shader");
1127 assert(!"not reached: should be handled by lower_noise");
1131 emit(ADD(result_dst
, op
[0], op
[1]));
1134 assert(!"not reached: should be handled by ir_sub_to_add_neg");
1138 if (ir
->type
->is_integer()) {
1139 /* For integer multiplication, the MUL uses the low 16 bits
1140 * of one of the operands (src0 on gen6, src1 on gen7). The
1141 * MACH accumulates in the contribution of the upper 16 bits
1144 * FINISHME: Emit just the MUL if we know an operand is small
1147 struct brw_reg acc
= retype(brw_acc_reg(), BRW_REGISTER_TYPE_D
);
1149 emit(MUL(acc
, op
[0], op
[1]));
1150 emit(MACH(dst_null_d(), op
[0], op
[1]));
1151 emit(MOV(result_dst
, src_reg(acc
)));
1153 emit(MUL(result_dst
, op
[0], op
[1]));
1157 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
1158 assert(ir
->type
->is_integer());
1159 emit_math(SHADER_OPCODE_INT_QUOTIENT
, result_dst
, op
[0], op
[1]);
1162 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
1163 assert(ir
->type
->is_integer());
1164 emit_math(SHADER_OPCODE_INT_REMAINDER
, result_dst
, op
[0], op
[1]);
1168 case ir_binop_greater
:
1169 case ir_binop_lequal
:
1170 case ir_binop_gequal
:
1171 case ir_binop_equal
:
1172 case ir_binop_nequal
: {
1173 emit(CMP(result_dst
, op
[0], op
[1],
1174 brw_conditional_for_comparison(ir
->operation
)));
1175 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1179 case ir_binop_all_equal
:
1180 /* "==" operator producing a scalar boolean. */
1181 if (ir
->operands
[0]->type
->is_vector() ||
1182 ir
->operands
[1]->type
->is_vector()) {
1183 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
1184 emit(MOV(result_dst
, src_reg(0)));
1185 inst
= emit(MOV(result_dst
, src_reg(1)));
1186 inst
->predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
1188 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_Z
));
1189 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1192 case ir_binop_any_nequal
:
1193 /* "!=" operator producing a scalar boolean. */
1194 if (ir
->operands
[0]->type
->is_vector() ||
1195 ir
->operands
[1]->type
->is_vector()) {
1196 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
1198 emit(MOV(result_dst
, src_reg(0)));
1199 inst
= emit(MOV(result_dst
, src_reg(1)));
1200 inst
->predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
1202 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_NZ
));
1203 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1208 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
1209 emit(MOV(result_dst
, src_reg(0)));
1211 inst
= emit(MOV(result_dst
, src_reg(1)));
1212 inst
->predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
1215 case ir_binop_logic_xor
:
1216 emit(XOR(result_dst
, op
[0], op
[1]));
1219 case ir_binop_logic_or
:
1220 emit(OR(result_dst
, op
[0], op
[1]));
1223 case ir_binop_logic_and
:
1224 emit(AND(result_dst
, op
[0], op
[1]));
1228 assert(ir
->operands
[0]->type
->is_vector());
1229 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1230 emit_dp(result_dst
, op
[0], op
[1], ir
->operands
[0]->type
->vector_elements
);
1234 emit_math(SHADER_OPCODE_SQRT
, result_dst
, op
[0]);
1237 emit_math(SHADER_OPCODE_RSQ
, result_dst
, op
[0]);
1240 case ir_unop_bitcast_i2f
:
1241 case ir_unop_bitcast_u2f
:
1242 this->result
= op
[0];
1243 this->result
.type
= BRW_REGISTER_TYPE_F
;
1246 case ir_unop_bitcast_f2i
:
1247 this->result
= op
[0];
1248 this->result
.type
= BRW_REGISTER_TYPE_D
;
1251 case ir_unop_bitcast_f2u
:
1252 this->result
= op
[0];
1253 this->result
.type
= BRW_REGISTER_TYPE_UD
;
1264 emit(MOV(result_dst
, op
[0]));
1268 emit(CMP(result_dst
, op
[0], src_reg(0.0f
), BRW_CONDITIONAL_NZ
));
1269 emit(AND(result_dst
, result_src
, src_reg(1)));
1274 emit(RNDZ(result_dst
, op
[0]));
1277 op
[0].negate
= !op
[0].negate
;
1278 inst
= emit(RNDD(result_dst
, op
[0]));
1279 this->result
.negate
= true;
1282 inst
= emit(RNDD(result_dst
, op
[0]));
1285 inst
= emit(FRC(result_dst
, op
[0]));
1287 case ir_unop_round_even
:
1288 emit(RNDE(result_dst
, op
[0]));
1292 emit_minmax(BRW_CONDITIONAL_L
, result_dst
, op
[0], op
[1]);
1295 emit_minmax(BRW_CONDITIONAL_G
, result_dst
, op
[0], op
[1]);
1299 emit_math(SHADER_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1302 case ir_unop_bit_not
:
1303 inst
= emit(NOT(result_dst
, op
[0]));
1305 case ir_binop_bit_and
:
1306 inst
= emit(AND(result_dst
, op
[0], op
[1]));
1308 case ir_binop_bit_xor
:
1309 inst
= emit(XOR(result_dst
, op
[0], op
[1]));
1311 case ir_binop_bit_or
:
1312 inst
= emit(OR(result_dst
, op
[0], op
[1]));
1315 case ir_binop_lshift
:
1316 inst
= emit(BRW_OPCODE_SHL
, result_dst
, op
[0], op
[1]);
1319 case ir_binop_rshift
:
1320 if (ir
->type
->base_type
== GLSL_TYPE_INT
)
1321 inst
= emit(BRW_OPCODE_ASR
, result_dst
, op
[0], op
[1]);
1323 inst
= emit(BRW_OPCODE_SHR
, result_dst
, op
[0], op
[1]);
1326 case ir_binop_ubo_load
: {
1327 ir_constant
*uniform_block
= ir
->operands
[0]->as_constant();
1328 ir_constant
*const_offset_ir
= ir
->operands
[1]->as_constant();
1329 unsigned const_offset
= const_offset_ir
? const_offset_ir
->value
.u
[0] : 0;
1330 src_reg offset
= op
[1];
1332 /* Now, load the vector from that offset. */
1333 assert(ir
->type
->is_vector() || ir
->type
->is_scalar());
1335 src_reg packed_consts
= src_reg(this, glsl_type::vec4_type
);
1336 packed_consts
.type
= result
.type
;
1337 src_reg surf_index
=
1338 src_reg(SURF_INDEX_VS_UBO(uniform_block
->value
.u
[0]));
1339 if (const_offset_ir
) {
1340 offset
= src_reg(const_offset
/ 16);
1342 emit(BRW_OPCODE_SHR
, dst_reg(offset
), offset
, src_reg(4));
1345 vec4_instruction
*pull
=
1346 emit(new(mem_ctx
) vec4_instruction(this,
1347 VS_OPCODE_PULL_CONSTANT_LOAD
,
1348 dst_reg(packed_consts
),
1351 pull
->base_mrf
= 14;
1354 packed_consts
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1355 packed_consts
.swizzle
+= BRW_SWIZZLE4(const_offset
% 16 / 4,
1356 const_offset
% 16 / 4,
1357 const_offset
% 16 / 4,
1358 const_offset
% 16 / 4);
1360 /* UBO bools are any nonzero int. We store bools as either 0 or 1. */
1361 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1362 emit(CMP(result_dst
, packed_consts
, src_reg(0u),
1363 BRW_CONDITIONAL_NZ
));
1364 emit(AND(result_dst
, result
, src_reg(0x1)));
1366 emit(MOV(result_dst
, packed_consts
));
1371 case ir_quadop_vector
:
1372 assert(!"not reached: should be handled by lower_quadop_vector");
1379 vec4_visitor::visit(ir_swizzle
*ir
)
1385 /* Note that this is only swizzles in expressions, not those on the left
1386 * hand side of an assignment, which do write masking. See ir_assignment
1390 ir
->val
->accept(this);
1392 assert(src
.file
!= BAD_FILE
);
1394 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1397 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1400 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1403 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1406 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1410 for (; i
< 4; i
++) {
1411 /* Replicate the last channel out. */
1412 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1415 src
.swizzle
= BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1421 vec4_visitor::visit(ir_dereference_variable
*ir
)
1423 const struct glsl_type
*type
= ir
->type
;
1424 dst_reg
*reg
= variable_storage(ir
->var
);
1427 fail("Failed to find variable storage for %s\n", ir
->var
->name
);
1428 this->result
= src_reg(brw_null_reg());
1432 this->result
= src_reg(*reg
);
1434 /* System values get their swizzle from the dst_reg writemask */
1435 if (ir
->var
->mode
== ir_var_system_value
)
1438 if (type
->is_scalar() || type
->is_vector() || type
->is_matrix())
1439 this->result
.swizzle
= swizzle_for_size(type
->vector_elements
);
1443 vec4_visitor::visit(ir_dereference_array
*ir
)
1445 ir_constant
*constant_index
;
1447 int element_size
= type_size(ir
->type
);
1449 constant_index
= ir
->array_index
->constant_expression_value();
1451 ir
->array
->accept(this);
1454 if (constant_index
) {
1455 src
.reg_offset
+= constant_index
->value
.i
[0] * element_size
;
1457 /* Variable index array dereference. It eats the "vec4" of the
1458 * base of the array and an index that offsets the Mesa register
1461 ir
->array_index
->accept(this);
1465 if (element_size
== 1) {
1466 index_reg
= this->result
;
1468 index_reg
= src_reg(this, glsl_type::int_type
);
1470 emit(MUL(dst_reg(index_reg
), this->result
, src_reg(element_size
)));
1474 src_reg temp
= src_reg(this, glsl_type::int_type
);
1476 emit(ADD(dst_reg(temp
), *src
.reladdr
, index_reg
));
1481 src
.reladdr
= ralloc(mem_ctx
, src_reg
);
1482 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1485 /* If the type is smaller than a vec4, replicate the last channel out. */
1486 if (ir
->type
->is_scalar() || ir
->type
->is_vector() || ir
->type
->is_matrix())
1487 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1489 src
.swizzle
= BRW_SWIZZLE_NOOP
;
1490 src
.type
= brw_type_for_base_type(ir
->type
);
1496 vec4_visitor::visit(ir_dereference_record
*ir
)
1499 const glsl_type
*struct_type
= ir
->record
->type
;
1502 ir
->record
->accept(this);
1504 for (i
= 0; i
< struct_type
->length
; i
++) {
1505 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1507 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1510 /* If the type is smaller than a vec4, replicate the last channel out. */
1511 if (ir
->type
->is_scalar() || ir
->type
->is_vector() || ir
->type
->is_matrix())
1512 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1514 this->result
.swizzle
= BRW_SWIZZLE_NOOP
;
1515 this->result
.type
= brw_type_for_base_type(ir
->type
);
1517 this->result
.reg_offset
+= offset
;
1521 * We want to be careful in assignment setup to hit the actual storage
1522 * instead of potentially using a temporary like we might with the
1523 * ir_dereference handler.
1526 get_assignment_lhs(ir_dereference
*ir
, vec4_visitor
*v
)
1528 /* The LHS must be a dereference. If the LHS is a variable indexed array
1529 * access of a vector, it must be separated into a series conditional moves
1530 * before reaching this point (see ir_vec_index_to_cond_assign).
1532 assert(ir
->as_dereference());
1533 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1535 assert(!deref_array
->array
->type
->is_vector());
1538 /* Use the rvalue deref handler for the most part. We'll ignore
1539 * swizzles in it and write swizzles using writemask, though.
1542 return dst_reg(v
->result
);
1546 vec4_visitor::emit_block_move(dst_reg
*dst
, src_reg
*src
,
1547 const struct glsl_type
*type
, uint32_t predicate
)
1549 if (type
->base_type
== GLSL_TYPE_STRUCT
) {
1550 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1551 emit_block_move(dst
, src
, type
->fields
.structure
[i
].type
, predicate
);
1556 if (type
->is_array()) {
1557 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1558 emit_block_move(dst
, src
, type
->fields
.array
, predicate
);
1563 if (type
->is_matrix()) {
1564 const struct glsl_type
*vec_type
;
1566 vec_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
1567 type
->vector_elements
, 1);
1569 for (int i
= 0; i
< type
->matrix_columns
; i
++) {
1570 emit_block_move(dst
, src
, vec_type
, predicate
);
1575 assert(type
->is_scalar() || type
->is_vector());
1577 dst
->type
= brw_type_for_base_type(type
);
1578 src
->type
= dst
->type
;
1580 dst
->writemask
= (1 << type
->vector_elements
) - 1;
1582 src
->swizzle
= swizzle_for_size(type
->vector_elements
);
1584 vec4_instruction
*inst
= emit(MOV(*dst
, *src
));
1585 inst
->predicate
= predicate
;
1592 /* If the RHS processing resulted in an instruction generating a
1593 * temporary value, and it would be easy to rewrite the instruction to
1594 * generate its result right into the LHS instead, do so. This ends
1595 * up reliably removing instructions where it can be tricky to do so
1596 * later without real UD chain information.
1599 vec4_visitor::try_rewrite_rhs_to_dst(ir_assignment
*ir
,
1602 vec4_instruction
*pre_rhs_inst
,
1603 vec4_instruction
*last_rhs_inst
)
1605 /* This could be supported, but it would take more smarts. */
1609 if (pre_rhs_inst
== last_rhs_inst
)
1610 return false; /* No instructions generated to work with. */
1612 /* Make sure the last instruction generated our source reg. */
1613 if (src
.file
!= GRF
||
1614 src
.file
!= last_rhs_inst
->dst
.file
||
1615 src
.reg
!= last_rhs_inst
->dst
.reg
||
1616 src
.reg_offset
!= last_rhs_inst
->dst
.reg_offset
||
1620 last_rhs_inst
->predicate
!= BRW_PREDICATE_NONE
)
1623 /* Check that that last instruction fully initialized the channels
1624 * we want to use, in the order we want to use them. We could
1625 * potentially reswizzle the operands of many instructions so that
1626 * we could handle out of order channels, but don't yet.
1629 for (unsigned i
= 0; i
< 4; i
++) {
1630 if (dst
.writemask
& (1 << i
)) {
1631 if (!(last_rhs_inst
->dst
.writemask
& (1 << i
)))
1634 if (BRW_GET_SWZ(src
.swizzle
, i
) != i
)
1639 /* Success! Rewrite the instruction. */
1640 last_rhs_inst
->dst
.file
= dst
.file
;
1641 last_rhs_inst
->dst
.reg
= dst
.reg
;
1642 last_rhs_inst
->dst
.reg_offset
= dst
.reg_offset
;
1643 last_rhs_inst
->dst
.reladdr
= dst
.reladdr
;
1644 last_rhs_inst
->dst
.writemask
&= dst
.writemask
;
1650 vec4_visitor::visit(ir_assignment
*ir
)
1652 dst_reg dst
= get_assignment_lhs(ir
->lhs
, this);
1653 uint32_t predicate
= BRW_PREDICATE_NONE
;
1655 if (!ir
->lhs
->type
->is_scalar() &&
1656 !ir
->lhs
->type
->is_vector()) {
1657 ir
->rhs
->accept(this);
1658 src_reg src
= this->result
;
1660 if (ir
->condition
) {
1661 emit_bool_to_cond_code(ir
->condition
, &predicate
);
1664 /* emit_block_move doesn't account for swizzles in the source register.
1665 * This should be ok, since the source register is a structure or an
1666 * array, and those can't be swizzled. But double-check to be sure.
1668 assert(src
.swizzle
==
1669 (ir
->rhs
->type
->is_matrix()
1670 ? swizzle_for_size(ir
->rhs
->type
->vector_elements
)
1671 : BRW_SWIZZLE_NOOP
));
1673 emit_block_move(&dst
, &src
, ir
->rhs
->type
, predicate
);
1677 /* Now we're down to just a scalar/vector with writemasks. */
1680 vec4_instruction
*pre_rhs_inst
, *last_rhs_inst
;
1681 pre_rhs_inst
= (vec4_instruction
*)this->instructions
.get_tail();
1683 ir
->rhs
->accept(this);
1685 last_rhs_inst
= (vec4_instruction
*)this->instructions
.get_tail();
1687 src_reg src
= this->result
;
1690 int first_enabled_chan
= 0;
1693 assert(ir
->lhs
->type
->is_vector() ||
1694 ir
->lhs
->type
->is_scalar());
1695 dst
.writemask
= ir
->write_mask
;
1697 for (int i
= 0; i
< 4; i
++) {
1698 if (dst
.writemask
& (1 << i
)) {
1699 first_enabled_chan
= BRW_GET_SWZ(src
.swizzle
, i
);
1704 /* Swizzle a small RHS vector into the channels being written.
1706 * glsl ir treats write_mask as dictating how many channels are
1707 * present on the RHS while in our instructions we need to make
1708 * those channels appear in the slots of the vec4 they're written to.
1710 for (int i
= 0; i
< 4; i
++) {
1711 if (dst
.writemask
& (1 << i
))
1712 swizzles
[i
] = BRW_GET_SWZ(src
.swizzle
, src_chan
++);
1714 swizzles
[i
] = first_enabled_chan
;
1716 src
.swizzle
= BRW_SWIZZLE4(swizzles
[0], swizzles
[1],
1717 swizzles
[2], swizzles
[3]);
1719 if (try_rewrite_rhs_to_dst(ir
, dst
, src
, pre_rhs_inst
, last_rhs_inst
)) {
1723 if (ir
->condition
) {
1724 emit_bool_to_cond_code(ir
->condition
, &predicate
);
1727 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1728 vec4_instruction
*inst
= emit(MOV(dst
, src
));
1729 inst
->predicate
= predicate
;
1737 vec4_visitor::emit_constant_values(dst_reg
*dst
, ir_constant
*ir
)
1739 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1740 foreach_list(node
, &ir
->components
) {
1741 ir_constant
*field_value
= (ir_constant
*)node
;
1743 emit_constant_values(dst
, field_value
);
1748 if (ir
->type
->is_array()) {
1749 for (unsigned int i
= 0; i
< ir
->type
->length
; i
++) {
1750 emit_constant_values(dst
, ir
->array_elements
[i
]);
1755 if (ir
->type
->is_matrix()) {
1756 for (int i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
1757 float *vec
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
1759 for (int j
= 0; j
< ir
->type
->vector_elements
; j
++) {
1760 dst
->writemask
= 1 << j
;
1761 dst
->type
= BRW_REGISTER_TYPE_F
;
1763 emit(MOV(*dst
, src_reg(vec
[j
])));
1770 int remaining_writemask
= (1 << ir
->type
->vector_elements
) - 1;
1772 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1773 if (!(remaining_writemask
& (1 << i
)))
1776 dst
->writemask
= 1 << i
;
1777 dst
->type
= brw_type_for_base_type(ir
->type
);
1779 /* Find other components that match the one we're about to
1780 * write. Emits fewer instructions for things like vec4(0.5,
1783 for (int j
= i
+ 1; j
< ir
->type
->vector_elements
; j
++) {
1784 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1785 if (ir
->value
.b
[i
] == ir
->value
.b
[j
])
1786 dst
->writemask
|= (1 << j
);
1788 /* u, i, and f storage all line up, so no need for a
1789 * switch case for comparing each type.
1791 if (ir
->value
.u
[i
] == ir
->value
.u
[j
])
1792 dst
->writemask
|= (1 << j
);
1796 switch (ir
->type
->base_type
) {
1797 case GLSL_TYPE_FLOAT
:
1798 emit(MOV(*dst
, src_reg(ir
->value
.f
[i
])));
1801 emit(MOV(*dst
, src_reg(ir
->value
.i
[i
])));
1803 case GLSL_TYPE_UINT
:
1804 emit(MOV(*dst
, src_reg(ir
->value
.u
[i
])));
1806 case GLSL_TYPE_BOOL
:
1807 emit(MOV(*dst
, src_reg(ir
->value
.b
[i
])));
1810 assert(!"Non-float/uint/int/bool constant");
1814 remaining_writemask
&= ~dst
->writemask
;
1820 vec4_visitor::visit(ir_constant
*ir
)
1822 dst_reg dst
= dst_reg(this, ir
->type
);
1823 this->result
= src_reg(dst
);
1825 emit_constant_values(&dst
, ir
);
1829 vec4_visitor::visit(ir_call
*ir
)
1831 assert(!"not reached");
1835 vec4_visitor::visit(ir_texture
*ir
)
1837 int sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
, prog
, &vp
->Base
);
1839 /* Should be lowered by do_lower_texture_projection */
1840 assert(!ir
->projector
);
1842 /* Generate code to compute all the subexpression trees. This has to be
1843 * done before loading any values into MRFs for the sampler message since
1844 * generating these values may involve SEND messages that need the MRFs.
1847 if (ir
->coordinate
) {
1848 ir
->coordinate
->accept(this);
1849 coordinate
= this->result
;
1852 src_reg shadow_comparitor
;
1853 if (ir
->shadow_comparitor
) {
1854 ir
->shadow_comparitor
->accept(this);
1855 shadow_comparitor
= this->result
;
1858 src_reg lod
, dPdx
, dPdy
;
1863 ir
->lod_info
.lod
->accept(this);
1867 ir
->lod_info
.grad
.dPdx
->accept(this);
1868 dPdx
= this->result
;
1870 ir
->lod_info
.grad
.dPdy
->accept(this);
1871 dPdy
= this->result
;
1878 vec4_instruction
*inst
= NULL
;
1882 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXL
);
1885 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXD
);
1888 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXF
);
1891 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXS
);
1894 assert(!"TXB is not valid for vertex shaders.");
1897 /* Texel offsets go in the message header; Gen4 also requires headers. */
1898 inst
->header_present
= ir
->offset
|| intel
->gen
< 5;
1900 inst
->mlen
= inst
->header_present
+ 1; /* always at least one */
1901 inst
->sampler
= sampler
;
1902 inst
->dst
= dst_reg(this, ir
->type
);
1903 inst
->shadow_compare
= ir
->shadow_comparitor
!= NULL
;
1905 if (ir
->offset
!= NULL
&& ir
->op
!= ir_txf
)
1906 inst
->texture_offset
= brw_texture_offset(ir
->offset
->as_constant());
1908 /* MRF for the first parameter */
1909 int param_base
= inst
->base_mrf
+ inst
->header_present
;
1911 if (ir
->op
== ir_txs
) {
1912 int writemask
= intel
->gen
== 4 ? WRITEMASK_W
: WRITEMASK_X
;
1913 emit(MOV(dst_reg(MRF
, param_base
, ir
->lod_info
.lod
->type
, writemask
),
1916 int i
, coord_mask
= 0, zero_mask
= 0;
1917 /* Load the coordinate */
1918 /* FINISHME: gl_clamp_mask and saturate */
1919 for (i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++)
1920 coord_mask
|= (1 << i
);
1922 zero_mask
|= (1 << i
);
1924 if (ir
->offset
&& ir
->op
== ir_txf
) {
1925 /* It appears that the ld instruction used for txf does its
1926 * address bounds check before adding in the offset. To work
1927 * around this, just add the integer offset to the integer
1928 * texel coordinate, and don't put the offset in the header.
1930 ir_constant
*offset
= ir
->offset
->as_constant();
1933 for (int j
= 0; j
< ir
->coordinate
->type
->vector_elements
; j
++) {
1934 src_reg src
= coordinate
;
1935 src
.swizzle
= BRW_SWIZZLE4(BRW_GET_SWZ(src
.swizzle
, j
),
1936 BRW_GET_SWZ(src
.swizzle
, j
),
1937 BRW_GET_SWZ(src
.swizzle
, j
),
1938 BRW_GET_SWZ(src
.swizzle
, j
));
1939 emit(ADD(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, 1 << j
),
1940 src
, offset
->value
.i
[j
]));
1943 emit(MOV(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, coord_mask
),
1946 emit(MOV(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, zero_mask
),
1948 /* Load the shadow comparitor */
1949 if (ir
->shadow_comparitor
) {
1950 emit(MOV(dst_reg(MRF
, param_base
+ 1, ir
->shadow_comparitor
->type
,
1952 shadow_comparitor
));
1956 /* Load the LOD info */
1957 if (ir
->op
== ir_txl
) {
1959 if (intel
->gen
>= 5) {
1960 mrf
= param_base
+ 1;
1961 if (ir
->shadow_comparitor
) {
1962 writemask
= WRITEMASK_Y
;
1963 /* mlen already incremented */
1965 writemask
= WRITEMASK_X
;
1968 } else /* intel->gen == 4 */ {
1970 writemask
= WRITEMASK_Z
;
1972 emit(MOV(dst_reg(MRF
, mrf
, ir
->lod_info
.lod
->type
, writemask
), lod
));
1973 } else if (ir
->op
== ir_txf
) {
1974 emit(MOV(dst_reg(MRF
, param_base
, ir
->lod_info
.lod
->type
, WRITEMASK_W
),
1976 } else if (ir
->op
== ir_txd
) {
1977 const glsl_type
*type
= ir
->lod_info
.grad
.dPdx
->type
;
1979 if (intel
->gen
>= 5) {
1980 dPdx
.swizzle
= BRW_SWIZZLE4(SWIZZLE_X
,SWIZZLE_X
,SWIZZLE_Y
,SWIZZLE_Y
);
1981 dPdy
.swizzle
= BRW_SWIZZLE4(SWIZZLE_X
,SWIZZLE_X
,SWIZZLE_Y
,SWIZZLE_Y
);
1982 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_XZ
), dPdx
));
1983 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_YW
), dPdy
));
1986 if (ir
->type
->vector_elements
== 3) {
1987 dPdx
.swizzle
= BRW_SWIZZLE_ZZZZ
;
1988 dPdy
.swizzle
= BRW_SWIZZLE_ZZZZ
;
1989 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_X
), dPdx
));
1990 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_Y
), dPdy
));
1993 } else /* intel->gen == 4 */ {
1994 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_XYZ
), dPdx
));
1995 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_XYZ
), dPdy
));
2003 swizzle_result(ir
, src_reg(inst
->dst
), sampler
);
2007 vec4_visitor::swizzle_result(ir_texture
*ir
, src_reg orig_val
, int sampler
)
2009 this->result
= orig_val
;
2011 int s
= c
->key
.tex
.swizzles
[sampler
];
2013 if (ir
->op
== ir_txs
|| ir
->type
== glsl_type::float_type
2014 || s
== SWIZZLE_NOOP
)
2017 int zero_mask
= 0, one_mask
= 0, copy_mask
= 0;
2020 for (int i
= 0; i
< 4; i
++) {
2021 switch (GET_SWZ(s
, i
)) {
2023 zero_mask
|= (1 << i
);
2026 one_mask
|= (1 << i
);
2029 copy_mask
|= (1 << i
);
2030 swizzle
[i
] = GET_SWZ(s
, i
);
2035 this->result
= src_reg(this, ir
->type
);
2036 dst_reg
swizzled_result(this->result
);
2039 orig_val
.swizzle
= BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2040 swizzled_result
.writemask
= copy_mask
;
2041 emit(MOV(swizzled_result
, orig_val
));
2045 swizzled_result
.writemask
= zero_mask
;
2046 emit(MOV(swizzled_result
, src_reg(0.0f
)));
2050 swizzled_result
.writemask
= one_mask
;
2051 emit(MOV(swizzled_result
, src_reg(1.0f
)));
2056 vec4_visitor::visit(ir_return
*ir
)
2058 assert(!"not reached");
2062 vec4_visitor::visit(ir_discard
*ir
)
2064 assert(!"not reached");
2068 vec4_visitor::visit(ir_if
*ir
)
2070 /* Don't point the annotation at the if statement, because then it plus
2071 * the then and else blocks get printed.
2073 this->base_ir
= ir
->condition
;
2075 if (intel
->gen
== 6) {
2079 emit_bool_to_cond_code(ir
->condition
, &predicate
);
2080 emit(IF(predicate
));
2083 visit_instructions(&ir
->then_instructions
);
2085 if (!ir
->else_instructions
.is_empty()) {
2086 this->base_ir
= ir
->condition
;
2087 emit(BRW_OPCODE_ELSE
);
2089 visit_instructions(&ir
->else_instructions
);
2092 this->base_ir
= ir
->condition
;
2093 emit(BRW_OPCODE_ENDIF
);
2097 vec4_visitor::emit_ndc_computation()
2099 /* Get the position */
2100 src_reg pos
= src_reg(output_reg
[VERT_RESULT_HPOS
]);
2102 /* Build ndc coords, which are (x/w, y/w, z/w, 1/w) */
2103 dst_reg ndc
= dst_reg(this, glsl_type::vec4_type
);
2104 output_reg
[BRW_VERT_RESULT_NDC
] = ndc
;
2106 current_annotation
= "NDC";
2107 dst_reg ndc_w
= ndc
;
2108 ndc_w
.writemask
= WRITEMASK_W
;
2109 src_reg pos_w
= pos
;
2110 pos_w
.swizzle
= BRW_SWIZZLE4(SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
);
2111 emit_math(SHADER_OPCODE_RCP
, ndc_w
, pos_w
);
2113 dst_reg ndc_xyz
= ndc
;
2114 ndc_xyz
.writemask
= WRITEMASK_XYZ
;
2116 emit(MUL(ndc_xyz
, pos
, src_reg(ndc_w
)));
2120 vec4_visitor::emit_psiz_and_flags(struct brw_reg reg
)
2122 if (intel
->gen
< 6 &&
2123 ((c
->prog_data
.outputs_written
& BITFIELD64_BIT(VERT_RESULT_PSIZ
)) ||
2124 c
->key
.userclip_active
|| brw
->has_negative_rhw_bug
)) {
2125 dst_reg header1
= dst_reg(this, glsl_type::uvec4_type
);
2126 dst_reg header1_w
= header1
;
2127 header1_w
.writemask
= WRITEMASK_W
;
2130 emit(MOV(header1
, 0u));
2132 if (c
->prog_data
.outputs_written
& BITFIELD64_BIT(VERT_RESULT_PSIZ
)) {
2133 src_reg psiz
= src_reg(output_reg
[VERT_RESULT_PSIZ
]);
2135 current_annotation
= "Point size";
2136 emit(MUL(header1_w
, psiz
, src_reg((float)(1 << 11))));
2137 emit(AND(header1_w
, src_reg(header1_w
), 0x7ff << 8));
2140 current_annotation
= "Clipping flags";
2141 for (i
= 0; i
< c
->key
.nr_userclip_plane_consts
; i
++) {
2142 vec4_instruction
*inst
;
2144 inst
= emit(DP4(dst_null_f(), src_reg(output_reg
[VERT_RESULT_HPOS
]),
2145 src_reg(this->userplane
[i
])));
2146 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
2148 inst
= emit(OR(header1_w
, src_reg(header1_w
), 1u << i
));
2149 inst
->predicate
= BRW_PREDICATE_NORMAL
;
2152 /* i965 clipping workaround:
2153 * 1) Test for -ve rhw
2155 * set ndc = (0,0,0,0)
2158 * Later, clipping will detect ucp[6] and ensure the primitive is
2159 * clipped against all fixed planes.
2161 if (brw
->has_negative_rhw_bug
) {
2165 vec8(brw_null_reg()),
2167 brw_swizzle1(output_reg
[BRW_VERT_RESULT_NDC
], 3),
2170 brw_OR(p
, brw_writemask(header1
, WRITEMASK_W
), header1
, brw_imm_ud(1<<6));
2171 brw_MOV(p
, output_reg
[BRW_VERT_RESULT_NDC
], brw_imm_f(0));
2172 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
2176 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_UD
), src_reg(header1
)));
2177 } else if (intel
->gen
< 6) {
2178 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_UD
), 0u));
2180 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_D
), src_reg(0)));
2181 if (c
->prog_data
.outputs_written
& BITFIELD64_BIT(VERT_RESULT_PSIZ
)) {
2182 emit(MOV(brw_writemask(reg
, WRITEMASK_W
),
2183 src_reg(output_reg
[VERT_RESULT_PSIZ
])));
2189 vec4_visitor::emit_clip_distances(struct brw_reg reg
, int offset
)
2191 if (intel
->gen
< 6) {
2192 /* Clip distance slots are set aside in gen5, but they are not used. It
2193 * is not clear whether we actually need to set aside space for them,
2194 * but the performance cost is negligible.
2199 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
2201 * "If a linked set of shaders forming the vertex stage contains no
2202 * static write to gl_ClipVertex or gl_ClipDistance, but the
2203 * application has requested clipping against user clip planes through
2204 * the API, then the coordinate written to gl_Position is used for
2205 * comparison against the user clip planes."
2207 * This function is only called if the shader didn't write to
2208 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
2209 * if the user wrote to it; otherwise we use gl_Position.
2211 gl_vert_result clip_vertex
= VERT_RESULT_CLIP_VERTEX
;
2212 if (!(c
->prog_data
.outputs_written
2213 & BITFIELD64_BIT(VERT_RESULT_CLIP_VERTEX
))) {
2214 clip_vertex
= VERT_RESULT_HPOS
;
2217 for (int i
= 0; i
+ offset
< c
->key
.nr_userclip_plane_consts
&& i
< 4;
2219 emit(DP4(dst_reg(brw_writemask(reg
, 1 << i
)),
2220 src_reg(output_reg
[clip_vertex
]),
2221 src_reg(this->userplane
[i
+ offset
])));
2226 vec4_visitor::emit_generic_urb_slot(dst_reg reg
, int vert_result
)
2228 assert (vert_result
< VERT_RESULT_MAX
);
2229 reg
.type
= output_reg
[vert_result
].type
;
2230 current_annotation
= output_reg_annotation
[vert_result
];
2231 /* Copy the register, saturating if necessary */
2232 vec4_instruction
*inst
= emit(MOV(reg
,
2233 src_reg(output_reg
[vert_result
])));
2234 if ((vert_result
== VERT_RESULT_COL0
||
2235 vert_result
== VERT_RESULT_COL1
||
2236 vert_result
== VERT_RESULT_BFC0
||
2237 vert_result
== VERT_RESULT_BFC1
) &&
2238 c
->key
.clamp_vertex_color
) {
2239 inst
->saturate
= true;
2244 vec4_visitor::emit_urb_slot(int mrf
, int vert_result
)
2246 struct brw_reg hw_reg
= brw_message_reg(mrf
);
2247 dst_reg reg
= dst_reg(MRF
, mrf
);
2248 reg
.type
= BRW_REGISTER_TYPE_F
;
2250 switch (vert_result
) {
2251 case VERT_RESULT_PSIZ
:
2252 /* PSIZ is always in slot 0, and is coupled with other flags. */
2253 current_annotation
= "indices, point width, clip flags";
2254 emit_psiz_and_flags(hw_reg
);
2256 case BRW_VERT_RESULT_NDC
:
2257 current_annotation
= "NDC";
2258 emit(MOV(reg
, src_reg(output_reg
[BRW_VERT_RESULT_NDC
])));
2260 case BRW_VERT_RESULT_HPOS_DUPLICATE
:
2261 case VERT_RESULT_HPOS
:
2262 current_annotation
= "gl_Position";
2263 emit(MOV(reg
, src_reg(output_reg
[VERT_RESULT_HPOS
])));
2265 case VERT_RESULT_CLIP_DIST0
:
2266 case VERT_RESULT_CLIP_DIST1
:
2267 if (this->c
->key
.uses_clip_distance
) {
2268 emit_generic_urb_slot(reg
, vert_result
);
2270 current_annotation
= "user clip distances";
2271 emit_clip_distances(hw_reg
, (vert_result
- VERT_RESULT_CLIP_DIST0
) * 4);
2274 case VERT_RESULT_EDGE
:
2275 /* This is present when doing unfilled polygons. We're supposed to copy
2276 * the edge flag from the user-provided vertex array
2277 * (glEdgeFlagPointer), or otherwise we'll copy from the current value
2278 * of that attribute (starts as 1.0f). This is then used in clipping to
2279 * determine which edges should be drawn as wireframe.
2281 current_annotation
= "edge flag";
2282 emit(MOV(reg
, src_reg(dst_reg(ATTR
, VERT_ATTRIB_EDGEFLAG
,
2283 glsl_type::float_type
, WRITEMASK_XYZW
))));
2285 case BRW_VERT_RESULT_PAD
:
2286 /* No need to write to this slot */
2289 emit_generic_urb_slot(reg
, vert_result
);
2295 align_interleaved_urb_mlen(struct brw_context
*brw
, int mlen
)
2297 struct intel_context
*intel
= &brw
->intel
;
2299 if (intel
->gen
>= 6) {
2300 /* URB data written (does not include the message header reg) must
2301 * be a multiple of 256 bits, or 2 VS registers. See vol5c.5,
2302 * section 5.4.3.2.2: URB_INTERLEAVED.
2304 * URB entries are allocated on a multiple of 1024 bits, so an
2305 * extra 128 bits written here to make the end align to 256 is
2308 if ((mlen
% 2) != 1)
2316 * Generates the VUE payload plus the 1 or 2 URB write instructions to
2317 * complete the VS thread.
2319 * The VUE layout is documented in Volume 2a.
2322 vec4_visitor::emit_urb_writes()
2324 /* MRF 0 is reserved for the debugger, so start with message header
2329 /* In the process of generating our URB write message contents, we
2330 * may need to unspill a register or load from an array. Those
2331 * reads would use MRFs 14-15.
2333 int max_usable_mrf
= 13;
2335 /* The following assertion verifies that max_usable_mrf causes an
2336 * even-numbered amount of URB write data, which will meet gen6's
2337 * requirements for length alignment.
2339 assert ((max_usable_mrf
- base_mrf
) % 2 == 0);
2341 /* First mrf is the g0-based message header containing URB handles and such,
2342 * which is implied in VS_OPCODE_URB_WRITE.
2346 if (intel
->gen
< 6) {
2347 emit_ndc_computation();
2350 /* Set up the VUE data for the first URB write */
2352 for (slot
= 0; slot
< c
->prog_data
.vue_map
.num_slots
; ++slot
) {
2353 emit_urb_slot(mrf
++, c
->prog_data
.vue_map
.slot_to_vert_result
[slot
]);
2355 /* If this was max_usable_mrf, we can't fit anything more into this URB
2358 if (mrf
> max_usable_mrf
) {
2364 current_annotation
= "URB write";
2365 vec4_instruction
*inst
= emit(VS_OPCODE_URB_WRITE
);
2366 inst
->base_mrf
= base_mrf
;
2367 inst
->mlen
= align_interleaved_urb_mlen(brw
, mrf
- base_mrf
);
2368 inst
->eot
= (slot
>= c
->prog_data
.vue_map
.num_slots
);
2370 /* Optional second URB write */
2374 for (; slot
< c
->prog_data
.vue_map
.num_slots
; ++slot
) {
2375 assert(mrf
< max_usable_mrf
);
2377 emit_urb_slot(mrf
++, c
->prog_data
.vue_map
.slot_to_vert_result
[slot
]);
2380 current_annotation
= "URB write";
2381 inst
= emit(VS_OPCODE_URB_WRITE
);
2382 inst
->base_mrf
= base_mrf
;
2383 inst
->mlen
= align_interleaved_urb_mlen(brw
, mrf
- base_mrf
);
2385 /* URB destination offset. In the previous write, we got MRFs
2386 * 2-13 minus the one header MRF, so 12 regs. URB offset is in
2387 * URB row increments, and each of our MRFs is half of one of
2388 * those, since we're doing interleaved writes.
2390 inst
->offset
= (max_usable_mrf
- base_mrf
) / 2;
2395 vec4_visitor::get_scratch_offset(vec4_instruction
*inst
,
2396 src_reg
*reladdr
, int reg_offset
)
2398 /* Because we store the values to scratch interleaved like our
2399 * vertex data, we need to scale the vec4 index by 2.
2401 int message_header_scale
= 2;
2403 /* Pre-gen6, the message header uses byte offsets instead of vec4
2404 * (16-byte) offset units.
2407 message_header_scale
*= 16;
2410 src_reg index
= src_reg(this, glsl_type::int_type
);
2412 emit_before(inst
, ADD(dst_reg(index
), *reladdr
, src_reg(reg_offset
)));
2413 emit_before(inst
, MUL(dst_reg(index
),
2414 index
, src_reg(message_header_scale
)));
2418 return src_reg(reg_offset
* message_header_scale
);
2423 vec4_visitor::get_pull_constant_offset(vec4_instruction
*inst
,
2424 src_reg
*reladdr
, int reg_offset
)
2427 src_reg index
= src_reg(this, glsl_type::int_type
);
2429 emit_before(inst
, ADD(dst_reg(index
), *reladdr
, src_reg(reg_offset
)));
2431 /* Pre-gen6, the message header uses byte offsets instead of vec4
2432 * (16-byte) offset units.
2434 if (intel
->gen
< 6) {
2435 emit_before(inst
, MUL(dst_reg(index
), index
, src_reg(16)));
2440 int message_header_scale
= intel
->gen
< 6 ? 16 : 1;
2441 return src_reg(reg_offset
* message_header_scale
);
2446 * Emits an instruction before @inst to load the value named by @orig_src
2447 * from scratch space at @base_offset to @temp.
2449 * @base_offset is measured in 32-byte units (the size of a register).
2452 vec4_visitor::emit_scratch_read(vec4_instruction
*inst
,
2453 dst_reg temp
, src_reg orig_src
,
2456 int reg_offset
= base_offset
+ orig_src
.reg_offset
;
2457 src_reg index
= get_scratch_offset(inst
, orig_src
.reladdr
, reg_offset
);
2459 emit_before(inst
, SCRATCH_READ(temp
, index
));
2463 * Emits an instruction after @inst to store the value to be written
2464 * to @orig_dst to scratch space at @base_offset, from @temp.
2466 * @base_offset is measured in 32-byte units (the size of a register).
2469 vec4_visitor::emit_scratch_write(vec4_instruction
*inst
, int base_offset
)
2471 int reg_offset
= base_offset
+ inst
->dst
.reg_offset
;
2472 src_reg index
= get_scratch_offset(inst
, inst
->dst
.reladdr
, reg_offset
);
2474 /* Create a temporary register to store *inst's result in.
2476 * We have to be careful in MOVing from our temporary result register in
2477 * the scratch write. If we swizzle from channels of the temporary that
2478 * weren't initialized, it will confuse live interval analysis, which will
2479 * make spilling fail to make progress.
2481 src_reg temp
= src_reg(this, glsl_type::vec4_type
);
2482 temp
.type
= inst
->dst
.type
;
2483 int first_writemask_chan
= ffs(inst
->dst
.writemask
) - 1;
2485 for (int i
= 0; i
< 4; i
++)
2486 if (inst
->dst
.writemask
& (1 << i
))
2489 swizzles
[i
] = first_writemask_chan
;
2490 temp
.swizzle
= BRW_SWIZZLE4(swizzles
[0], swizzles
[1],
2491 swizzles
[2], swizzles
[3]);
2493 dst_reg dst
= dst_reg(brw_writemask(brw_vec8_grf(0, 0),
2494 inst
->dst
.writemask
));
2495 vec4_instruction
*write
= SCRATCH_WRITE(dst
, temp
, index
);
2496 write
->predicate
= inst
->predicate
;
2497 write
->ir
= inst
->ir
;
2498 write
->annotation
= inst
->annotation
;
2499 inst
->insert_after(write
);
2501 inst
->dst
.file
= temp
.file
;
2502 inst
->dst
.reg
= temp
.reg
;
2503 inst
->dst
.reg_offset
= temp
.reg_offset
;
2504 inst
->dst
.reladdr
= NULL
;
2508 * We can't generally support array access in GRF space, because a
2509 * single instruction's destination can only span 2 contiguous
2510 * registers. So, we send all GRF arrays that get variable index
2511 * access to scratch space.
2514 vec4_visitor::move_grf_array_access_to_scratch()
2516 int scratch_loc
[this->virtual_grf_count
];
2518 for (int i
= 0; i
< this->virtual_grf_count
; i
++) {
2519 scratch_loc
[i
] = -1;
2522 /* First, calculate the set of virtual GRFs that need to be punted
2523 * to scratch due to having any array access on them, and where in
2526 foreach_list(node
, &this->instructions
) {
2527 vec4_instruction
*inst
= (vec4_instruction
*)node
;
2529 if (inst
->dst
.file
== GRF
&& inst
->dst
.reladdr
&&
2530 scratch_loc
[inst
->dst
.reg
] == -1) {
2531 scratch_loc
[inst
->dst
.reg
] = c
->last_scratch
;
2532 c
->last_scratch
+= this->virtual_grf_sizes
[inst
->dst
.reg
];
2535 for (int i
= 0 ; i
< 3; i
++) {
2536 src_reg
*src
= &inst
->src
[i
];
2538 if (src
->file
== GRF
&& src
->reladdr
&&
2539 scratch_loc
[src
->reg
] == -1) {
2540 scratch_loc
[src
->reg
] = c
->last_scratch
;
2541 c
->last_scratch
+= this->virtual_grf_sizes
[src
->reg
];
2546 /* Now, for anything that will be accessed through scratch, rewrite
2547 * it to load/store. Note that this is a _safe list walk, because
2548 * we may generate a new scratch_write instruction after the one
2551 foreach_list_safe(node
, &this->instructions
) {
2552 vec4_instruction
*inst
= (vec4_instruction
*)node
;
2554 /* Set up the annotation tracking for new generated instructions. */
2556 current_annotation
= inst
->annotation
;
2558 if (inst
->dst
.file
== GRF
&& scratch_loc
[inst
->dst
.reg
] != -1) {
2559 emit_scratch_write(inst
, scratch_loc
[inst
->dst
.reg
]);
2562 for (int i
= 0 ; i
< 3; i
++) {
2563 if (inst
->src
[i
].file
!= GRF
|| scratch_loc
[inst
->src
[i
].reg
] == -1)
2566 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
2568 emit_scratch_read(inst
, temp
, inst
->src
[i
],
2569 scratch_loc
[inst
->src
[i
].reg
]);
2571 inst
->src
[i
].file
= temp
.file
;
2572 inst
->src
[i
].reg
= temp
.reg
;
2573 inst
->src
[i
].reg_offset
= temp
.reg_offset
;
2574 inst
->src
[i
].reladdr
= NULL
;
2580 * Emits an instruction before @inst to load the value named by @orig_src
2581 * from the pull constant buffer (surface) at @base_offset to @temp.
2584 vec4_visitor::emit_pull_constant_load(vec4_instruction
*inst
,
2585 dst_reg temp
, src_reg orig_src
,
2588 int reg_offset
= base_offset
+ orig_src
.reg_offset
;
2589 src_reg index
= src_reg((unsigned)SURF_INDEX_VERT_CONST_BUFFER
);
2590 src_reg offset
= get_pull_constant_offset(inst
, orig_src
.reladdr
, reg_offset
);
2591 vec4_instruction
*load
;
2593 load
= new(mem_ctx
) vec4_instruction(this, VS_OPCODE_PULL_CONSTANT_LOAD
,
2594 temp
, index
, offset
);
2595 load
->base_mrf
= 14;
2597 emit_before(inst
, load
);
2601 * Implements array access of uniforms by inserting a
2602 * PULL_CONSTANT_LOAD instruction.
2604 * Unlike temporary GRF array access (where we don't support it due to
2605 * the difficulty of doing relative addressing on instruction
2606 * destinations), we could potentially do array access of uniforms
2607 * that were loaded in GRF space as push constants. In real-world
2608 * usage we've seen, though, the arrays being used are always larger
2609 * than we could load as push constants, so just always move all
2610 * uniform array access out to a pull constant buffer.
2613 vec4_visitor::move_uniform_array_access_to_pull_constants()
2615 int pull_constant_loc
[this->uniforms
];
2617 for (int i
= 0; i
< this->uniforms
; i
++) {
2618 pull_constant_loc
[i
] = -1;
2621 /* Walk through and find array access of uniforms. Put a copy of that
2622 * uniform in the pull constant buffer.
2624 * Note that we don't move constant-indexed accesses to arrays. No
2625 * testing has been done of the performance impact of this choice.
2627 foreach_list_safe(node
, &this->instructions
) {
2628 vec4_instruction
*inst
= (vec4_instruction
*)node
;
2630 for (int i
= 0 ; i
< 3; i
++) {
2631 if (inst
->src
[i
].file
!= UNIFORM
|| !inst
->src
[i
].reladdr
)
2634 int uniform
= inst
->src
[i
].reg
;
2636 /* If this array isn't already present in the pull constant buffer,
2639 if (pull_constant_loc
[uniform
] == -1) {
2640 const float **values
= &prog_data
->param
[uniform
* 4];
2642 pull_constant_loc
[uniform
] = prog_data
->nr_pull_params
/ 4;
2644 for (int j
= 0; j
< uniform_size
[uniform
] * 4; j
++) {
2645 prog_data
->pull_param
[prog_data
->nr_pull_params
++] = values
[j
];
2649 /* Set up the annotation tracking for new generated instructions. */
2651 current_annotation
= inst
->annotation
;
2653 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
2655 emit_pull_constant_load(inst
, temp
, inst
->src
[i
],
2656 pull_constant_loc
[uniform
]);
2658 inst
->src
[i
].file
= temp
.file
;
2659 inst
->src
[i
].reg
= temp
.reg
;
2660 inst
->src
[i
].reg_offset
= temp
.reg_offset
;
2661 inst
->src
[i
].reladdr
= NULL
;
2665 /* Now there are no accesses of the UNIFORM file with a reladdr, so
2666 * no need to track them as larger-than-vec4 objects. This will be
2667 * relied on in cutting out unused uniform vectors from push
2670 split_uniform_registers();
2674 vec4_visitor::resolve_ud_negate(src_reg
*reg
)
2676 if (reg
->type
!= BRW_REGISTER_TYPE_UD
||
2680 src_reg temp
= src_reg(this, glsl_type::uvec4_type
);
2681 emit(BRW_OPCODE_MOV
, dst_reg(temp
), *reg
);
2685 vec4_visitor::vec4_visitor(struct brw_vs_compile
*c
,
2686 struct gl_shader_program
*prog
,
2687 struct brw_shader
*shader
)
2692 this->intel
= &brw
->intel
;
2693 this->ctx
= &intel
->ctx
;
2695 this->shader
= shader
;
2697 this->mem_ctx
= ralloc_context(NULL
);
2698 this->failed
= false;
2700 this->base_ir
= NULL
;
2701 this->current_annotation
= NULL
;
2704 this->vp
= &c
->vp
->program
;
2705 this->prog_data
= &c
->prog_data
;
2707 this->variable_ht
= hash_table_ctor(0,
2708 hash_table_pointer_hash
,
2709 hash_table_pointer_compare
);
2711 this->virtual_grf_def
= NULL
;
2712 this->virtual_grf_use
= NULL
;
2713 this->virtual_grf_sizes
= NULL
;
2714 this->virtual_grf_count
= 0;
2715 this->virtual_grf_reg_map
= NULL
;
2716 this->virtual_grf_reg_count
= 0;
2717 this->virtual_grf_array_size
= 0;
2718 this->live_intervals_valid
= false;
2720 this->max_grf
= intel
->gen
>= 7 ? GEN7_MRF_HACK_START
: BRW_MAX_GRF
;
2725 vec4_visitor::~vec4_visitor()
2727 ralloc_free(this->mem_ctx
);
2728 hash_table_dtor(this->variable_ht
);
2733 vec4_visitor::fail(const char *format
, ...)
2743 va_start(va
, format
);
2744 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
2746 msg
= ralloc_asprintf(mem_ctx
, "VS compile failed: %s\n", msg
);
2748 this->fail_msg
= msg
;
2750 if (INTEL_DEBUG
& DEBUG_VS
) {
2751 fprintf(stderr
, "%s", msg
);
2755 } /* namespace brw */