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 src_reg::src_reg(dst_reg reg
)
37 this->file
= reg
.file
;
39 this->reg_offset
= reg
.reg_offset
;
40 this->type
= reg
.type
;
41 this->reladdr
= reg
.reladdr
;
42 this->fixed_hw_reg
= reg
.fixed_hw_reg
;
48 for (int i
= 0; i
< 4; i
++) {
49 if (!(reg
.writemask
& (1 << i
)))
52 swizzles
[next_chan
++] = last
= i
;
55 for (; next_chan
< 4; next_chan
++) {
56 swizzles
[next_chan
] = last
;
59 this->swizzle
= BRW_SWIZZLE4(swizzles
[0], swizzles
[1],
60 swizzles
[2], swizzles
[3]);
63 dst_reg::dst_reg(src_reg reg
)
67 this->file
= reg
.file
;
69 this->reg_offset
= reg
.reg_offset
;
70 this->type
= reg
.type
;
71 this->writemask
= WRITEMASK_XYZW
;
72 this->reladdr
= reg
.reladdr
;
73 this->fixed_hw_reg
= reg
.fixed_hw_reg
;
76 vec4_instruction::vec4_instruction(vec4_visitor
*v
,
77 enum opcode opcode
, dst_reg dst
,
78 src_reg src0
, src_reg src1
, src_reg src2
)
80 this->opcode
= opcode
;
85 this->ir
= v
->base_ir
;
86 this->annotation
= v
->current_annotation
;
90 vec4_visitor::emit(vec4_instruction
*inst
)
92 this->instructions
.push_tail(inst
);
98 vec4_visitor::emit_before(vec4_instruction
*inst
, vec4_instruction
*new_inst
)
100 new_inst
->ir
= inst
->ir
;
101 new_inst
->annotation
= inst
->annotation
;
103 inst
->insert_before(new_inst
);
109 vec4_visitor::emit(enum opcode opcode
, dst_reg dst
,
110 src_reg src0
, src_reg src1
, src_reg src2
)
112 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst
,
118 vec4_visitor::emit(enum opcode opcode
, dst_reg dst
, src_reg src0
, src_reg src1
)
120 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst
, src0
, src1
));
124 vec4_visitor::emit(enum opcode opcode
, dst_reg dst
, src_reg src0
)
126 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst
, src0
));
130 vec4_visitor::emit(enum opcode opcode
)
132 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst_reg()));
137 vec4_visitor::op(dst_reg dst, src_reg src0) \
139 return new(mem_ctx) vec4_instruction(this, BRW_OPCODE_##op, dst, \
145 vec4_visitor::op(dst_reg dst, src_reg src0, src_reg src1) \
147 return new(mem_ctx) vec4_instruction(this, BRW_OPCODE_##op, dst, \
166 /** Gen4 predicated IF. */
168 vec4_visitor::IF(uint32_t predicate
)
170 vec4_instruction
*inst
;
172 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_IF
);
173 inst
->predicate
= predicate
;
178 /** Gen6+ IF with embedded comparison. */
180 vec4_visitor::IF(src_reg src0
, src_reg src1
, uint32_t condition
)
182 assert(intel
->gen
>= 6);
184 vec4_instruction
*inst
;
186 resolve_ud_negate(&src0
);
187 resolve_ud_negate(&src1
);
189 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_IF
, dst_null_d(),
191 inst
->conditional_mod
= condition
;
197 * CMP: Sets the low bit of the destination channels with the result
198 * of the comparison, while the upper bits are undefined, and updates
199 * the flag register with the packed 16 bits of the result.
202 vec4_visitor::CMP(dst_reg dst
, src_reg src0
, src_reg src1
, uint32_t condition
)
204 vec4_instruction
*inst
;
206 /* original gen4 does type conversion to the destination type
207 * before before comparison, producing garbage results for floating
210 if (intel
->gen
== 4) {
211 dst
.type
= src0
.type
;
212 if (dst
.file
== HW_REG
)
213 dst
.fixed_hw_reg
.type
= dst
.type
;
216 resolve_ud_negate(&src0
);
217 resolve_ud_negate(&src1
);
219 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_CMP
, dst
, src0
, src1
);
220 inst
->conditional_mod
= condition
;
226 vec4_visitor::SCRATCH_READ(dst_reg dst
, src_reg index
)
228 vec4_instruction
*inst
;
230 inst
= new(mem_ctx
) vec4_instruction(this, VS_OPCODE_SCRATCH_READ
,
239 vec4_visitor::SCRATCH_WRITE(dst_reg dst
, src_reg src
, src_reg index
)
241 vec4_instruction
*inst
;
243 inst
= new(mem_ctx
) vec4_instruction(this, VS_OPCODE_SCRATCH_WRITE
,
252 vec4_visitor::emit_dp(dst_reg dst
, src_reg src0
, src_reg src1
, unsigned elements
)
254 static enum opcode dot_opcodes
[] = {
255 BRW_OPCODE_DP2
, BRW_OPCODE_DP3
, BRW_OPCODE_DP4
258 emit(dot_opcodes
[elements
- 2], dst
, src0
, src1
);
262 vec4_visitor::emit_math1_gen6(enum opcode opcode
, dst_reg dst
, src_reg src
)
264 /* The gen6 math instruction ignores the source modifiers --
265 * swizzle, abs, negate, and at least some parts of the register
266 * region description.
268 * While it would seem that this MOV could be avoided at this point
269 * in the case that the swizzle is matched up with the destination
270 * writemask, note that uniform packing and register allocation
271 * could rearrange our swizzle, so let's leave this matter up to
272 * copy propagation later.
274 src_reg temp_src
= src_reg(this, glsl_type::vec4_type
);
275 emit(MOV(dst_reg(temp_src
), src
));
277 if (dst
.writemask
!= WRITEMASK_XYZW
) {
278 /* The gen6 math instruction must be align1, so we can't do
281 dst_reg temp_dst
= dst_reg(this, glsl_type::vec4_type
);
283 emit(opcode
, temp_dst
, temp_src
);
285 emit(MOV(dst
, src_reg(temp_dst
)));
287 emit(opcode
, dst
, temp_src
);
292 vec4_visitor::emit_math1_gen4(enum opcode opcode
, dst_reg dst
, src_reg src
)
294 vec4_instruction
*inst
= emit(opcode
, dst
, src
);
300 vec4_visitor::emit_math(opcode opcode
, dst_reg dst
, src_reg src
)
303 case SHADER_OPCODE_RCP
:
304 case SHADER_OPCODE_RSQ
:
305 case SHADER_OPCODE_SQRT
:
306 case SHADER_OPCODE_EXP2
:
307 case SHADER_OPCODE_LOG2
:
308 case SHADER_OPCODE_SIN
:
309 case SHADER_OPCODE_COS
:
312 assert(!"not reached: bad math opcode");
316 if (intel
->gen
>= 7) {
317 emit(opcode
, dst
, src
);
318 } else if (intel
->gen
== 6) {
319 return emit_math1_gen6(opcode
, dst
, src
);
321 return emit_math1_gen4(opcode
, dst
, src
);
326 vec4_visitor::emit_math2_gen6(enum opcode opcode
,
327 dst_reg dst
, src_reg src0
, src_reg src1
)
331 /* The gen6 math instruction ignores the source modifiers --
332 * swizzle, abs, negate, and at least some parts of the register
333 * region description. Move the sources to temporaries to make it
337 expanded
= src_reg(this, glsl_type::vec4_type
);
338 expanded
.type
= src0
.type
;
339 emit(MOV(dst_reg(expanded
), src0
));
342 expanded
= src_reg(this, glsl_type::vec4_type
);
343 expanded
.type
= src1
.type
;
344 emit(MOV(dst_reg(expanded
), src1
));
347 if (dst
.writemask
!= WRITEMASK_XYZW
) {
348 /* The gen6 math instruction must be align1, so we can't do
351 dst_reg temp_dst
= dst_reg(this, glsl_type::vec4_type
);
352 temp_dst
.type
= dst
.type
;
354 emit(opcode
, temp_dst
, src0
, src1
);
356 emit(MOV(dst
, src_reg(temp_dst
)));
358 emit(opcode
, dst
, src0
, src1
);
363 vec4_visitor::emit_math2_gen4(enum opcode opcode
,
364 dst_reg dst
, src_reg src0
, src_reg src1
)
366 vec4_instruction
*inst
= emit(opcode
, dst
, src0
, src1
);
372 vec4_visitor::emit_math(enum opcode opcode
,
373 dst_reg dst
, src_reg src0
, src_reg src1
)
376 case SHADER_OPCODE_POW
:
377 case SHADER_OPCODE_INT_QUOTIENT
:
378 case SHADER_OPCODE_INT_REMAINDER
:
381 assert(!"not reached: unsupported binary math opcode");
385 if (intel
->gen
>= 7) {
386 emit(opcode
, dst
, src0
, src1
);
387 } else if (intel
->gen
== 6) {
388 return emit_math2_gen6(opcode
, dst
, src0
, src1
);
390 return emit_math2_gen4(opcode
, dst
, src0
, src1
);
395 vec4_visitor::visit_instructions(const exec_list
*list
)
397 foreach_list(node
, list
) {
398 ir_instruction
*ir
= (ir_instruction
*)node
;
407 type_size(const struct glsl_type
*type
)
412 switch (type
->base_type
) {
415 case GLSL_TYPE_FLOAT
:
417 if (type
->is_matrix()) {
418 return type
->matrix_columns
;
420 /* Regardless of size of vector, it gets a vec4. This is bad
421 * packing for things like floats, but otherwise arrays become a
422 * mess. Hopefully a later pass over the code can pack scalars
423 * down if appropriate.
427 case GLSL_TYPE_ARRAY
:
428 assert(type
->length
> 0);
429 return type_size(type
->fields
.array
) * type
->length
;
430 case GLSL_TYPE_STRUCT
:
432 for (i
= 0; i
< type
->length
; i
++) {
433 size
+= type_size(type
->fields
.structure
[i
].type
);
436 case GLSL_TYPE_SAMPLER
:
437 /* Samplers take up one slot in UNIFORMS[], but they're baked in
448 vec4_visitor::virtual_grf_alloc(int size
)
450 if (virtual_grf_array_size
<= virtual_grf_count
) {
451 if (virtual_grf_array_size
== 0)
452 virtual_grf_array_size
= 16;
454 virtual_grf_array_size
*= 2;
455 virtual_grf_sizes
= reralloc(mem_ctx
, virtual_grf_sizes
, int,
456 virtual_grf_array_size
);
457 virtual_grf_reg_map
= reralloc(mem_ctx
, virtual_grf_reg_map
, int,
458 virtual_grf_array_size
);
460 virtual_grf_reg_map
[virtual_grf_count
] = virtual_grf_reg_count
;
461 virtual_grf_reg_count
+= size
;
462 virtual_grf_sizes
[virtual_grf_count
] = size
;
463 return virtual_grf_count
++;
466 src_reg::src_reg(class vec4_visitor
*v
, const struct glsl_type
*type
)
471 this->reg
= v
->virtual_grf_alloc(type_size(type
));
473 if (type
->is_array() || type
->is_record()) {
474 this->swizzle
= BRW_SWIZZLE_NOOP
;
476 this->swizzle
= swizzle_for_size(type
->vector_elements
);
479 this->type
= brw_type_for_base_type(type
);
482 dst_reg::dst_reg(class vec4_visitor
*v
, const struct glsl_type
*type
)
487 this->reg
= v
->virtual_grf_alloc(type_size(type
));
489 if (type
->is_array() || type
->is_record()) {
490 this->writemask
= WRITEMASK_XYZW
;
492 this->writemask
= (1 << type
->vector_elements
) - 1;
495 this->type
= brw_type_for_base_type(type
);
498 /* Our support for uniforms is piggy-backed on the struct
499 * gl_fragment_program, because that's where the values actually
500 * get stored, rather than in some global gl_shader_program uniform
504 vec4_visitor::setup_uniform_values(int loc
, const glsl_type
*type
)
506 unsigned int offset
= 0;
507 float *values
= &this->vp
->Base
.Parameters
->ParameterValues
[loc
][0].f
;
509 if (type
->is_matrix()) {
510 const glsl_type
*column
= type
->column_type();
512 for (unsigned int i
= 0; i
< type
->matrix_columns
; i
++) {
513 offset
+= setup_uniform_values(loc
+ offset
, column
);
519 switch (type
->base_type
) {
520 case GLSL_TYPE_FLOAT
:
524 for (unsigned int i
= 0; i
< type
->vector_elements
; i
++) {
525 c
->prog_data
.param
[this->uniforms
* 4 + i
] = &values
[i
];
528 /* Set up pad elements to get things aligned to a vec4 boundary. */
529 for (unsigned int i
= type
->vector_elements
; i
< 4; i
++) {
530 static float zero
= 0;
532 c
->prog_data
.param
[this->uniforms
* 4 + i
] = &zero
;
535 /* Track the size of this uniform vector, for future packing of
538 this->uniform_vector_size
[this->uniforms
] = type
->vector_elements
;
543 case GLSL_TYPE_STRUCT
:
544 for (unsigned int i
= 0; i
< type
->length
; i
++) {
545 offset
+= setup_uniform_values(loc
+ offset
,
546 type
->fields
.structure
[i
].type
);
550 case GLSL_TYPE_ARRAY
:
551 for (unsigned int i
= 0; i
< type
->length
; i
++) {
552 offset
+= setup_uniform_values(loc
+ offset
, type
->fields
.array
);
556 case GLSL_TYPE_SAMPLER
:
557 /* The sampler takes up a slot, but we don't use any values from it. */
561 assert(!"not reached");
567 vec4_visitor::setup_uniform_clipplane_values()
569 gl_clip_plane
*clip_planes
= brw_select_clip_planes(ctx
);
571 /* Pre-Gen6, we compact clip planes. For example, if the user
572 * enables just clip planes 0, 1, and 3, we will enable clip planes
573 * 0, 1, and 2 in the hardware, and we'll move clip plane 3 to clip
574 * plane 2. This simplifies the implementation of the Gen6 clip
577 * In Gen6 and later, we don't compact clip planes, because this
578 * simplifies the implementation of gl_ClipDistance.
580 int compacted_clipplane_index
= 0;
581 for (int i
= 0; i
< c
->key
.nr_userclip_plane_consts
; ++i
) {
582 if (intel
->gen
< 6 &&
583 !(c
->key
.userclip_planes_enabled_gen_4_5
& (1 << i
))) {
586 this->uniform_vector_size
[this->uniforms
] = 4;
587 this->userplane
[compacted_clipplane_index
] = dst_reg(UNIFORM
, this->uniforms
);
588 this->userplane
[compacted_clipplane_index
].type
= BRW_REGISTER_TYPE_F
;
589 for (int j
= 0; j
< 4; ++j
) {
590 c
->prog_data
.param
[this->uniforms
* 4 + j
] = &clip_planes
[i
][j
];
592 ++compacted_clipplane_index
;
597 /* Our support for builtin uniforms is even scarier than non-builtin.
598 * It sits on top of the PROG_STATE_VAR parameters that are
599 * automatically updated from GL context state.
602 vec4_visitor::setup_builtin_uniform_values(ir_variable
*ir
)
604 const ir_state_slot
*const slots
= ir
->state_slots
;
605 assert(ir
->state_slots
!= NULL
);
607 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
608 /* This state reference has already been setup by ir_to_mesa,
609 * but we'll get the same index back here. We can reference
610 * ParameterValues directly, since unlike brw_fs.cpp, we never
611 * add new state references during compile.
613 int index
= _mesa_add_state_reference(this->vp
->Base
.Parameters
,
614 (gl_state_index
*)slots
[i
].tokens
);
615 float *values
= &this->vp
->Base
.Parameters
->ParameterValues
[index
][0].f
;
617 this->uniform_vector_size
[this->uniforms
] = 0;
618 /* Add each of the unique swizzled channels of the element.
619 * This will end up matching the size of the glsl_type of this field.
622 for (unsigned int j
= 0; j
< 4; j
++) {
623 int swiz
= GET_SWZ(slots
[i
].swizzle
, j
);
626 c
->prog_data
.param
[this->uniforms
* 4 + j
] = &values
[swiz
];
627 if (swiz
<= last_swiz
)
628 this->uniform_vector_size
[this->uniforms
]++;
635 vec4_visitor::variable_storage(ir_variable
*var
)
637 return (dst_reg
*)hash_table_find(this->variable_ht
, var
);
641 vec4_visitor::emit_bool_to_cond_code(ir_rvalue
*ir
, uint32_t *predicate
)
643 ir_expression
*expr
= ir
->as_expression();
645 *predicate
= BRW_PREDICATE_NORMAL
;
649 vec4_instruction
*inst
;
651 assert(expr
->get_num_operands() <= 2);
652 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
653 expr
->operands
[i
]->accept(this);
654 op
[i
] = this->result
;
656 resolve_ud_negate(&op
[i
]);
659 switch (expr
->operation
) {
660 case ir_unop_logic_not
:
661 inst
= emit(AND(dst_null_d(), op
[0], src_reg(1)));
662 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
665 case ir_binop_logic_xor
:
666 inst
= emit(XOR(dst_null_d(), op
[0], op
[1]));
667 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
670 case ir_binop_logic_or
:
671 inst
= emit(OR(dst_null_d(), op
[0], op
[1]));
672 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
675 case ir_binop_logic_and
:
676 inst
= emit(AND(dst_null_d(), op
[0], op
[1]));
677 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
681 if (intel
->gen
>= 6) {
682 emit(CMP(dst_null_d(), op
[0], src_reg(0.0f
), BRW_CONDITIONAL_NZ
));
684 inst
= emit(MOV(dst_null_f(), op
[0]));
685 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
690 if (intel
->gen
>= 6) {
691 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
693 inst
= emit(MOV(dst_null_d(), op
[0]));
694 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
698 case ir_binop_all_equal
:
699 inst
= emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
700 *predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
703 case ir_binop_any_nequal
:
704 inst
= emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
705 *predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
709 inst
= emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
710 *predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
713 case ir_binop_greater
:
714 case ir_binop_gequal
:
716 case ir_binop_lequal
:
718 case ir_binop_nequal
:
719 emit(CMP(dst_null_d(), op
[0], op
[1],
720 brw_conditional_for_comparison(expr
->operation
)));
724 assert(!"not reached");
732 resolve_ud_negate(&this->result
);
734 if (intel
->gen
>= 6) {
735 vec4_instruction
*inst
= emit(AND(dst_null_d(),
736 this->result
, src_reg(1)));
737 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
739 vec4_instruction
*inst
= emit(MOV(dst_null_d(), this->result
));
740 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
745 * Emit a gen6 IF statement with the comparison folded into the IF
749 vec4_visitor::emit_if_gen6(ir_if
*ir
)
751 ir_expression
*expr
= ir
->condition
->as_expression();
757 assert(expr
->get_num_operands() <= 2);
758 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
759 expr
->operands
[i
]->accept(this);
760 op
[i
] = this->result
;
763 switch (expr
->operation
) {
764 case ir_unop_logic_not
:
765 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_Z
));
768 case ir_binop_logic_xor
:
769 emit(IF(op
[0], op
[1], BRW_CONDITIONAL_NZ
));
772 case ir_binop_logic_or
:
773 temp
= dst_reg(this, glsl_type::bool_type
);
774 emit(OR(temp
, op
[0], op
[1]));
775 emit(IF(src_reg(temp
), src_reg(0), BRW_CONDITIONAL_NZ
));
778 case ir_binop_logic_and
:
779 temp
= dst_reg(this, glsl_type::bool_type
);
780 emit(AND(temp
, op
[0], op
[1]));
781 emit(IF(src_reg(temp
), src_reg(0), BRW_CONDITIONAL_NZ
));
785 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
789 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
792 case ir_binop_greater
:
793 case ir_binop_gequal
:
795 case ir_binop_lequal
:
797 case ir_binop_nequal
:
798 emit(IF(op
[0], op
[1],
799 brw_conditional_for_comparison(expr
->operation
)));
802 case ir_binop_all_equal
:
803 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
804 emit(IF(BRW_PREDICATE_ALIGN16_ALL4H
));
807 case ir_binop_any_nequal
:
808 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
809 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H
));
813 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
814 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H
));
818 assert(!"not reached");
819 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
825 ir
->condition
->accept(this);
827 emit(IF(this->result
, src_reg(0), BRW_CONDITIONAL_NZ
));
831 vec4_visitor::visit(ir_variable
*ir
)
835 if (variable_storage(ir
))
840 reg
= new(mem_ctx
) dst_reg(ATTR
, ir
->location
);
842 /* Do GL_FIXED rescaling for GLES2.0. Our GL_FIXED attributes
843 * come in as floating point conversions of the integer values.
845 for (int i
= ir
->location
; i
< ir
->location
+ type_size(ir
->type
); i
++) {
846 if (!c
->key
.gl_fixed_input_size
[i
])
850 dst
.type
= brw_type_for_base_type(ir
->type
);
851 dst
.writemask
= (1 << c
->key
.gl_fixed_input_size
[i
]) - 1;
852 emit(MUL(dst
, src_reg(dst
), src_reg(1.0f
/ 65536.0f
)));
857 reg
= new(mem_ctx
) dst_reg(this, ir
->type
);
859 for (int i
= 0; i
< type_size(ir
->type
); i
++) {
860 output_reg
[ir
->location
+ i
] = *reg
;
861 output_reg
[ir
->location
+ i
].reg_offset
= i
;
862 output_reg
[ir
->location
+ i
].type
=
863 brw_type_for_base_type(ir
->type
->get_scalar_type());
864 output_reg_annotation
[ir
->location
+ i
] = ir
->name
;
869 case ir_var_temporary
:
870 reg
= new(mem_ctx
) dst_reg(this, ir
->type
);
874 reg
= new(this->mem_ctx
) dst_reg(UNIFORM
, this->uniforms
);
876 /* Track how big the whole uniform variable is, in case we need to put a
877 * copy of its data into pull constants for array access.
879 this->uniform_size
[this->uniforms
] = type_size(ir
->type
);
881 if (!strncmp(ir
->name
, "gl_", 3)) {
882 setup_builtin_uniform_values(ir
);
884 setup_uniform_values(ir
->location
, ir
->type
);
888 case ir_var_system_value
:
889 /* VertexID is stored by the VF as the last vertex element, but
890 * we don't represent it with a flag in inputs_read, so we call
891 * it VERT_ATTRIB_MAX, which setup_attributes() picks up on.
893 reg
= new(mem_ctx
) dst_reg(ATTR
, VERT_ATTRIB_MAX
);
894 prog_data
->uses_vertexid
= true;
896 switch (ir
->location
) {
897 case SYSTEM_VALUE_VERTEX_ID
:
898 reg
->writemask
= WRITEMASK_X
;
900 case SYSTEM_VALUE_INSTANCE_ID
:
901 reg
->writemask
= WRITEMASK_Y
;
904 assert(!"not reached");
910 assert(!"not reached");
913 reg
->type
= brw_type_for_base_type(ir
->type
);
914 hash_table_insert(this->variable_ht
, reg
, ir
);
918 vec4_visitor::visit(ir_loop
*ir
)
922 /* We don't want debugging output to print the whole body of the
923 * loop as the annotation.
925 this->base_ir
= NULL
;
927 if (ir
->counter
!= NULL
) {
928 this->base_ir
= ir
->counter
;
929 ir
->counter
->accept(this);
930 counter
= *(variable_storage(ir
->counter
));
932 if (ir
->from
!= NULL
) {
933 this->base_ir
= ir
->from
;
934 ir
->from
->accept(this);
936 emit(MOV(counter
, this->result
));
943 this->base_ir
= ir
->to
;
944 ir
->to
->accept(this);
946 emit(CMP(dst_null_d(), src_reg(counter
), this->result
,
947 brw_conditional_for_comparison(ir
->cmp
)));
949 vec4_instruction
*inst
= emit(BRW_OPCODE_BREAK
);
950 inst
->predicate
= BRW_PREDICATE_NORMAL
;
953 visit_instructions(&ir
->body_instructions
);
957 this->base_ir
= ir
->increment
;
958 ir
->increment
->accept(this);
959 emit(ADD(counter
, src_reg(counter
), this->result
));
962 emit(BRW_OPCODE_WHILE
);
966 vec4_visitor::visit(ir_loop_jump
*ir
)
969 case ir_loop_jump::jump_break
:
970 emit(BRW_OPCODE_BREAK
);
972 case ir_loop_jump::jump_continue
:
973 emit(BRW_OPCODE_CONTINUE
);
980 vec4_visitor::visit(ir_function_signature
*ir
)
987 vec4_visitor::visit(ir_function
*ir
)
989 /* Ignore function bodies other than main() -- we shouldn't see calls to
990 * them since they should all be inlined.
992 if (strcmp(ir
->name
, "main") == 0) {
993 const ir_function_signature
*sig
;
996 sig
= ir
->matching_signature(&empty
);
1000 visit_instructions(&sig
->body
);
1005 vec4_visitor::try_emit_sat(ir_expression
*ir
)
1007 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
1011 sat_src
->accept(this);
1012 src_reg src
= this->result
;
1014 this->result
= src_reg(this, ir
->type
);
1015 vec4_instruction
*inst
;
1016 inst
= emit(MOV(dst_reg(this->result
), src
));
1017 inst
->saturate
= true;
1023 vec4_visitor::emit_bool_comparison(unsigned int op
,
1024 dst_reg dst
, src_reg src0
, src_reg src1
)
1026 /* original gen4 does destination conversion before comparison. */
1028 dst
.type
= src0
.type
;
1030 emit(CMP(dst
, src0
, src1
, brw_conditional_for_comparison(op
)));
1032 dst
.type
= BRW_REGISTER_TYPE_D
;
1033 emit(AND(dst
, src_reg(dst
), src_reg(0x1)));
1037 vec4_visitor::visit(ir_expression
*ir
)
1039 unsigned int operand
;
1040 src_reg op
[Elements(ir
->operands
)];
1043 vec4_instruction
*inst
;
1045 if (try_emit_sat(ir
))
1048 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1049 this->result
.file
= BAD_FILE
;
1050 ir
->operands
[operand
]->accept(this);
1051 if (this->result
.file
== BAD_FILE
) {
1052 printf("Failed to get tree for expression operand:\n");
1053 ir
->operands
[operand
]->print();
1056 op
[operand
] = this->result
;
1058 /* Matrix expression operands should have been broken down to vector
1059 * operations already.
1061 assert(!ir
->operands
[operand
]->type
->is_matrix());
1064 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1065 if (ir
->operands
[1]) {
1066 vector_elements
= MAX2(vector_elements
,
1067 ir
->operands
[1]->type
->vector_elements
);
1070 this->result
.file
= BAD_FILE
;
1072 /* Storage for our result. Ideally for an assignment we'd be using
1073 * the actual storage for the result here, instead.
1075 result_src
= src_reg(this, ir
->type
);
1076 /* convenience for the emit functions below. */
1077 result_dst
= dst_reg(result_src
);
1078 /* If nothing special happens, this is the result. */
1079 this->result
= result_src
;
1080 /* Limit writes to the channels that will be used by result_src later.
1081 * This does limit this temp's use as a temporary for multi-instruction
1084 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1086 switch (ir
->operation
) {
1087 case ir_unop_logic_not
:
1088 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
1089 * ones complement of the whole register, not just bit 0.
1091 emit(XOR(result_dst
, op
[0], src_reg(1)));
1094 op
[0].negate
= !op
[0].negate
;
1095 this->result
= op
[0];
1099 op
[0].negate
= false;
1100 this->result
= op
[0];
1104 emit(MOV(result_dst
, src_reg(0.0f
)));
1106 emit(CMP(dst_null_d(), op
[0], src_reg(0.0f
), BRW_CONDITIONAL_G
));
1107 inst
= emit(MOV(result_dst
, src_reg(1.0f
)));
1108 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1110 emit(CMP(dst_null_d(), op
[0], src_reg(0.0f
), BRW_CONDITIONAL_L
));
1111 inst
= emit(MOV(result_dst
, src_reg(-1.0f
)));
1112 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1117 emit_math(SHADER_OPCODE_RCP
, result_dst
, op
[0]);
1121 emit_math(SHADER_OPCODE_EXP2
, result_dst
, op
[0]);
1124 emit_math(SHADER_OPCODE_LOG2
, result_dst
, op
[0]);
1128 assert(!"not reached: should be handled by ir_explog_to_explog2");
1131 case ir_unop_sin_reduced
:
1132 emit_math(SHADER_OPCODE_SIN
, result_dst
, op
[0]);
1135 case ir_unop_cos_reduced
:
1136 emit_math(SHADER_OPCODE_COS
, result_dst
, op
[0]);
1141 assert(!"derivatives not valid in vertex shader");
1145 assert(!"not reached: should be handled by lower_noise");
1149 emit(ADD(result_dst
, op
[0], op
[1]));
1152 assert(!"not reached: should be handled by ir_sub_to_add_neg");
1156 if (ir
->type
->is_integer()) {
1157 /* For integer multiplication, the MUL uses the low 16 bits
1158 * of one of the operands (src0 on gen6, src1 on gen7). The
1159 * MACH accumulates in the contribution of the upper 16 bits
1162 * FINISHME: Emit just the MUL if we know an operand is small
1165 struct brw_reg acc
= retype(brw_acc_reg(), BRW_REGISTER_TYPE_D
);
1167 emit(MUL(acc
, op
[0], op
[1]));
1168 emit(MACH(dst_null_d(), op
[0], op
[1]));
1169 emit(MOV(result_dst
, src_reg(acc
)));
1171 emit(MUL(result_dst
, op
[0], op
[1]));
1175 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
1176 assert(ir
->type
->is_integer());
1177 emit_math(SHADER_OPCODE_INT_QUOTIENT
, result_dst
, op
[0], op
[1]);
1180 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
1181 assert(ir
->type
->is_integer());
1182 emit_math(SHADER_OPCODE_INT_REMAINDER
, result_dst
, op
[0], op
[1]);
1186 case ir_binop_greater
:
1187 case ir_binop_lequal
:
1188 case ir_binop_gequal
:
1189 case ir_binop_equal
:
1190 case ir_binop_nequal
: {
1191 emit(CMP(result_dst
, op
[0], op
[1],
1192 brw_conditional_for_comparison(ir
->operation
)));
1193 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1197 case ir_binop_all_equal
:
1198 /* "==" operator producing a scalar boolean. */
1199 if (ir
->operands
[0]->type
->is_vector() ||
1200 ir
->operands
[1]->type
->is_vector()) {
1201 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
1202 emit(MOV(result_dst
, src_reg(0)));
1203 inst
= emit(MOV(result_dst
, src_reg(1)));
1204 inst
->predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
1206 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_Z
));
1207 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1210 case ir_binop_any_nequal
:
1211 /* "!=" operator producing a scalar boolean. */
1212 if (ir
->operands
[0]->type
->is_vector() ||
1213 ir
->operands
[1]->type
->is_vector()) {
1214 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
1216 emit(MOV(result_dst
, src_reg(0)));
1217 inst
= emit(MOV(result_dst
, src_reg(1)));
1218 inst
->predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
1220 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_NZ
));
1221 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1226 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
1227 emit(MOV(result_dst
, src_reg(0)));
1229 inst
= emit(MOV(result_dst
, src_reg(1)));
1230 inst
->predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
1233 case ir_binop_logic_xor
:
1234 emit(XOR(result_dst
, op
[0], op
[1]));
1237 case ir_binop_logic_or
:
1238 emit(OR(result_dst
, op
[0], op
[1]));
1241 case ir_binop_logic_and
:
1242 emit(AND(result_dst
, op
[0], op
[1]));
1246 assert(ir
->operands
[0]->type
->is_vector());
1247 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1248 emit_dp(result_dst
, op
[0], op
[1], ir
->operands
[0]->type
->vector_elements
);
1252 emit_math(SHADER_OPCODE_SQRT
, result_dst
, op
[0]);
1255 emit_math(SHADER_OPCODE_RSQ
, result_dst
, op
[0]);
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 if (intel
->gen
>= 6) {
1293 inst
= emit(BRW_OPCODE_SEL
, result_dst
, op
[0], op
[1]);
1294 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1296 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_L
));
1298 inst
= emit(BRW_OPCODE_SEL
, result_dst
, op
[0], op
[1]);
1299 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1303 if (intel
->gen
>= 6) {
1304 inst
= emit(BRW_OPCODE_SEL
, result_dst
, op
[0], op
[1]);
1305 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
1307 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_G
));
1309 inst
= emit(BRW_OPCODE_SEL
, result_dst
, op
[0], op
[1]);
1310 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1315 emit_math(SHADER_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1318 case ir_unop_bit_not
:
1319 inst
= emit(NOT(result_dst
, op
[0]));
1321 case ir_binop_bit_and
:
1322 inst
= emit(AND(result_dst
, op
[0], op
[1]));
1324 case ir_binop_bit_xor
:
1325 inst
= emit(XOR(result_dst
, op
[0], op
[1]));
1327 case ir_binop_bit_or
:
1328 inst
= emit(OR(result_dst
, op
[0], op
[1]));
1331 case ir_binop_lshift
:
1332 inst
= emit(BRW_OPCODE_SHL
, result_dst
, op
[0], op
[1]);
1335 case ir_binop_rshift
:
1336 if (ir
->type
->base_type
== GLSL_TYPE_INT
)
1337 inst
= emit(BRW_OPCODE_ASR
, result_dst
, op
[0], op
[1]);
1339 inst
= emit(BRW_OPCODE_SHR
, result_dst
, op
[0], op
[1]);
1342 case ir_quadop_vector
:
1343 assert(!"not reached: should be handled by lower_quadop_vector");
1350 vec4_visitor::visit(ir_swizzle
*ir
)
1356 /* Note that this is only swizzles in expressions, not those on the left
1357 * hand side of an assignment, which do write masking. See ir_assignment
1361 ir
->val
->accept(this);
1363 assert(src
.file
!= BAD_FILE
);
1365 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1368 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1371 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1374 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1377 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1381 for (; i
< 4; i
++) {
1382 /* Replicate the last channel out. */
1383 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1386 src
.swizzle
= BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1392 vec4_visitor::visit(ir_dereference_variable
*ir
)
1394 const struct glsl_type
*type
= ir
->type
;
1395 dst_reg
*reg
= variable_storage(ir
->var
);
1398 fail("Failed to find variable storage for %s\n", ir
->var
->name
);
1399 this->result
= src_reg(brw_null_reg());
1403 this->result
= src_reg(*reg
);
1405 /* System values get their swizzle from the dst_reg writemask */
1406 if (ir
->var
->mode
== ir_var_system_value
)
1409 if (type
->is_scalar() || type
->is_vector() || type
->is_matrix())
1410 this->result
.swizzle
= swizzle_for_size(type
->vector_elements
);
1414 vec4_visitor::visit(ir_dereference_array
*ir
)
1416 ir_constant
*constant_index
;
1418 int element_size
= type_size(ir
->type
);
1420 constant_index
= ir
->array_index
->constant_expression_value();
1422 ir
->array
->accept(this);
1425 if (constant_index
) {
1426 src
.reg_offset
+= constant_index
->value
.i
[0] * element_size
;
1428 /* Variable index array dereference. It eats the "vec4" of the
1429 * base of the array and an index that offsets the Mesa register
1432 ir
->array_index
->accept(this);
1436 if (element_size
== 1) {
1437 index_reg
= this->result
;
1439 index_reg
= src_reg(this, glsl_type::int_type
);
1441 emit(MUL(dst_reg(index_reg
), this->result
, src_reg(element_size
)));
1445 src_reg temp
= src_reg(this, glsl_type::int_type
);
1447 emit(ADD(dst_reg(temp
), *src
.reladdr
, index_reg
));
1452 src
.reladdr
= ralloc(mem_ctx
, src_reg
);
1453 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1456 /* If the type is smaller than a vec4, replicate the last channel out. */
1457 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1458 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1460 src
.swizzle
= BRW_SWIZZLE_NOOP
;
1461 src
.type
= brw_type_for_base_type(ir
->type
);
1467 vec4_visitor::visit(ir_dereference_record
*ir
)
1470 const glsl_type
*struct_type
= ir
->record
->type
;
1473 ir
->record
->accept(this);
1475 for (i
= 0; i
< struct_type
->length
; i
++) {
1476 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1478 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1481 /* If the type is smaller than a vec4, replicate the last channel out. */
1482 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1483 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1485 this->result
.swizzle
= BRW_SWIZZLE_NOOP
;
1486 this->result
.type
= brw_type_for_base_type(ir
->type
);
1488 this->result
.reg_offset
+= offset
;
1492 * We want to be careful in assignment setup to hit the actual storage
1493 * instead of potentially using a temporary like we might with the
1494 * ir_dereference handler.
1497 get_assignment_lhs(ir_dereference
*ir
, vec4_visitor
*v
)
1499 /* The LHS must be a dereference. If the LHS is a variable indexed array
1500 * access of a vector, it must be separated into a series conditional moves
1501 * before reaching this point (see ir_vec_index_to_cond_assign).
1503 assert(ir
->as_dereference());
1504 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1506 assert(!deref_array
->array
->type
->is_vector());
1509 /* Use the rvalue deref handler for the most part. We'll ignore
1510 * swizzles in it and write swizzles using writemask, though.
1513 return dst_reg(v
->result
);
1517 vec4_visitor::emit_block_move(dst_reg
*dst
, src_reg
*src
,
1518 const struct glsl_type
*type
, uint32_t predicate
)
1520 if (type
->base_type
== GLSL_TYPE_STRUCT
) {
1521 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1522 emit_block_move(dst
, src
, type
->fields
.structure
[i
].type
, predicate
);
1527 if (type
->is_array()) {
1528 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1529 emit_block_move(dst
, src
, type
->fields
.array
, predicate
);
1534 if (type
->is_matrix()) {
1535 const struct glsl_type
*vec_type
;
1537 vec_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
1538 type
->vector_elements
, 1);
1540 for (int i
= 0; i
< type
->matrix_columns
; i
++) {
1541 emit_block_move(dst
, src
, vec_type
, predicate
);
1546 assert(type
->is_scalar() || type
->is_vector());
1548 dst
->type
= brw_type_for_base_type(type
);
1549 src
->type
= dst
->type
;
1551 dst
->writemask
= (1 << type
->vector_elements
) - 1;
1553 src
->swizzle
= swizzle_for_size(type
->vector_elements
);
1555 vec4_instruction
*inst
= emit(MOV(*dst
, *src
));
1556 inst
->predicate
= predicate
;
1563 /* If the RHS processing resulted in an instruction generating a
1564 * temporary value, and it would be easy to rewrite the instruction to
1565 * generate its result right into the LHS instead, do so. This ends
1566 * up reliably removing instructions where it can be tricky to do so
1567 * later without real UD chain information.
1570 vec4_visitor::try_rewrite_rhs_to_dst(ir_assignment
*ir
,
1573 vec4_instruction
*pre_rhs_inst
,
1574 vec4_instruction
*last_rhs_inst
)
1576 /* This could be supported, but it would take more smarts. */
1580 if (pre_rhs_inst
== last_rhs_inst
)
1581 return false; /* No instructions generated to work with. */
1583 /* Make sure the last instruction generated our source reg. */
1584 if (src
.file
!= GRF
||
1585 src
.file
!= last_rhs_inst
->dst
.file
||
1586 src
.reg
!= last_rhs_inst
->dst
.reg
||
1587 src
.reg_offset
!= last_rhs_inst
->dst
.reg_offset
||
1591 last_rhs_inst
->predicate
!= BRW_PREDICATE_NONE
)
1594 /* Check that that last instruction fully initialized the channels
1595 * we want to use, in the order we want to use them. We could
1596 * potentially reswizzle the operands of many instructions so that
1597 * we could handle out of order channels, but don't yet.
1600 for (unsigned i
= 0; i
< 4; i
++) {
1601 if (dst
.writemask
& (1 << i
)) {
1602 if (!(last_rhs_inst
->dst
.writemask
& (1 << i
)))
1605 if (BRW_GET_SWZ(src
.swizzle
, i
) != i
)
1610 /* Success! Rewrite the instruction. */
1611 last_rhs_inst
->dst
.file
= dst
.file
;
1612 last_rhs_inst
->dst
.reg
= dst
.reg
;
1613 last_rhs_inst
->dst
.reg_offset
= dst
.reg_offset
;
1614 last_rhs_inst
->dst
.reladdr
= dst
.reladdr
;
1615 last_rhs_inst
->dst
.writemask
&= dst
.writemask
;
1621 vec4_visitor::visit(ir_assignment
*ir
)
1623 dst_reg dst
= get_assignment_lhs(ir
->lhs
, this);
1624 uint32_t predicate
= BRW_PREDICATE_NONE
;
1626 if (!ir
->lhs
->type
->is_scalar() &&
1627 !ir
->lhs
->type
->is_vector()) {
1628 ir
->rhs
->accept(this);
1629 src_reg src
= this->result
;
1631 if (ir
->condition
) {
1632 emit_bool_to_cond_code(ir
->condition
, &predicate
);
1635 /* emit_block_move doesn't account for swizzles in the source register.
1636 * This should be ok, since the source register is a structure or an
1637 * array, and those can't be swizzled. But double-check to be sure.
1639 assert(src
.swizzle
==
1640 (ir
->rhs
->type
->is_matrix()
1641 ? swizzle_for_size(ir
->rhs
->type
->vector_elements
)
1642 : BRW_SWIZZLE_NOOP
));
1644 emit_block_move(&dst
, &src
, ir
->rhs
->type
, predicate
);
1648 /* Now we're down to just a scalar/vector with writemasks. */
1651 vec4_instruction
*pre_rhs_inst
, *last_rhs_inst
;
1652 pre_rhs_inst
= (vec4_instruction
*)this->instructions
.get_tail();
1654 ir
->rhs
->accept(this);
1656 last_rhs_inst
= (vec4_instruction
*)this->instructions
.get_tail();
1658 src_reg src
= this->result
;
1661 int first_enabled_chan
= 0;
1664 assert(ir
->lhs
->type
->is_vector() ||
1665 ir
->lhs
->type
->is_scalar());
1666 dst
.writemask
= ir
->write_mask
;
1668 for (int i
= 0; i
< 4; i
++) {
1669 if (dst
.writemask
& (1 << i
)) {
1670 first_enabled_chan
= BRW_GET_SWZ(src
.swizzle
, i
);
1675 /* Swizzle a small RHS vector into the channels being written.
1677 * glsl ir treats write_mask as dictating how many channels are
1678 * present on the RHS while in our instructions we need to make
1679 * those channels appear in the slots of the vec4 they're written to.
1681 for (int i
= 0; i
< 4; i
++) {
1682 if (dst
.writemask
& (1 << i
))
1683 swizzles
[i
] = BRW_GET_SWZ(src
.swizzle
, src_chan
++);
1685 swizzles
[i
] = first_enabled_chan
;
1687 src
.swizzle
= BRW_SWIZZLE4(swizzles
[0], swizzles
[1],
1688 swizzles
[2], swizzles
[3]);
1690 if (try_rewrite_rhs_to_dst(ir
, dst
, src
, pre_rhs_inst
, last_rhs_inst
)) {
1694 if (ir
->condition
) {
1695 emit_bool_to_cond_code(ir
->condition
, &predicate
);
1698 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1699 vec4_instruction
*inst
= emit(MOV(dst
, src
));
1700 inst
->predicate
= predicate
;
1708 vec4_visitor::emit_constant_values(dst_reg
*dst
, ir_constant
*ir
)
1710 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1711 foreach_list(node
, &ir
->components
) {
1712 ir_constant
*field_value
= (ir_constant
*)node
;
1714 emit_constant_values(dst
, field_value
);
1719 if (ir
->type
->is_array()) {
1720 for (unsigned int i
= 0; i
< ir
->type
->length
; i
++) {
1721 emit_constant_values(dst
, ir
->array_elements
[i
]);
1726 if (ir
->type
->is_matrix()) {
1727 for (int i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
1728 float *vec
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
1730 for (int j
= 0; j
< ir
->type
->vector_elements
; j
++) {
1731 dst
->writemask
= 1 << j
;
1732 dst
->type
= BRW_REGISTER_TYPE_F
;
1734 emit(MOV(*dst
, src_reg(vec
[j
])));
1741 int remaining_writemask
= (1 << ir
->type
->vector_elements
) - 1;
1743 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1744 if (!(remaining_writemask
& (1 << i
)))
1747 dst
->writemask
= 1 << i
;
1748 dst
->type
= brw_type_for_base_type(ir
->type
);
1750 /* Find other components that match the one we're about to
1751 * write. Emits fewer instructions for things like vec4(0.5,
1754 for (int j
= i
+ 1; j
< ir
->type
->vector_elements
; j
++) {
1755 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1756 if (ir
->value
.b
[i
] == ir
->value
.b
[j
])
1757 dst
->writemask
|= (1 << j
);
1759 /* u, i, and f storage all line up, so no need for a
1760 * switch case for comparing each type.
1762 if (ir
->value
.u
[i
] == ir
->value
.u
[j
])
1763 dst
->writemask
|= (1 << j
);
1767 switch (ir
->type
->base_type
) {
1768 case GLSL_TYPE_FLOAT
:
1769 emit(MOV(*dst
, src_reg(ir
->value
.f
[i
])));
1772 emit(MOV(*dst
, src_reg(ir
->value
.i
[i
])));
1774 case GLSL_TYPE_UINT
:
1775 emit(MOV(*dst
, src_reg(ir
->value
.u
[i
])));
1777 case GLSL_TYPE_BOOL
:
1778 emit(MOV(*dst
, src_reg(ir
->value
.b
[i
])));
1781 assert(!"Non-float/uint/int/bool constant");
1785 remaining_writemask
&= ~dst
->writemask
;
1791 vec4_visitor::visit(ir_constant
*ir
)
1793 dst_reg dst
= dst_reg(this, ir
->type
);
1794 this->result
= src_reg(dst
);
1796 emit_constant_values(&dst
, ir
);
1800 vec4_visitor::visit(ir_call
*ir
)
1802 assert(!"not reached");
1806 vec4_visitor::visit(ir_texture
*ir
)
1808 int sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
, prog
, &vp
->Base
);
1809 sampler
= vp
->Base
.SamplerUnits
[sampler
];
1811 /* Should be lowered by do_lower_texture_projection */
1812 assert(!ir
->projector
);
1814 vec4_instruction
*inst
= NULL
;
1818 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXL
);
1821 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXD
);
1824 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXF
);
1827 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXS
);
1830 assert(!"TXB is not valid for vertex shaders.");
1833 /* Texel offsets go in the message header; Gen4 also requires headers. */
1834 inst
->header_present
= ir
->offset
|| intel
->gen
< 5;
1836 inst
->mlen
= inst
->header_present
+ 1; /* always at least one */
1837 inst
->sampler
= sampler
;
1838 inst
->dst
= dst_reg(this, ir
->type
);
1839 inst
->shadow_compare
= ir
->shadow_comparitor
!= NULL
;
1841 if (ir
->offset
!= NULL
)
1842 inst
->texture_offset
= brw_texture_offset(ir
->offset
->as_constant());
1844 /* MRF for the first parameter */
1845 int param_base
= inst
->base_mrf
+ inst
->header_present
;
1847 if (ir
->op
== ir_txs
) {
1848 ir
->lod_info
.lod
->accept(this);
1849 int writemask
= intel
->gen
== 4 ? WRITEMASK_W
: WRITEMASK_X
;
1850 emit(MOV(dst_reg(MRF
, param_base
, ir
->lod_info
.lod
->type
, writemask
),
1853 int i
, coord_mask
= 0, zero_mask
= 0;
1854 /* Load the coordinate */
1855 /* FINISHME: gl_clamp_mask and saturate */
1856 for (i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++)
1857 coord_mask
|= (1 << i
);
1859 zero_mask
|= (1 << i
);
1861 ir
->coordinate
->accept(this);
1862 emit(MOV(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, coord_mask
),
1864 emit(MOV(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, zero_mask
),
1866 /* Load the shadow comparitor */
1867 if (ir
->shadow_comparitor
) {
1868 ir
->shadow_comparitor
->accept(this);
1869 emit(MOV(dst_reg(MRF
, param_base
+ 1, ir
->shadow_comparitor
->type
,
1875 /* Load the LOD info */
1876 if (ir
->op
== ir_txl
) {
1878 if (intel
->gen
>= 5) {
1879 mrf
= param_base
+ 1;
1880 if (ir
->shadow_comparitor
) {
1881 writemask
= WRITEMASK_Y
;
1882 /* mlen already incremented */
1884 writemask
= WRITEMASK_X
;
1887 } else /* intel->gen == 4 */ {
1889 writemask
= WRITEMASK_Z
;
1891 ir
->lod_info
.lod
->accept(this);
1892 emit(MOV(dst_reg(MRF
, mrf
, ir
->lod_info
.lod
->type
, writemask
),
1894 } else if (ir
->op
== ir_txf
) {
1895 ir
->lod_info
.lod
->accept(this);
1896 emit(MOV(dst_reg(MRF
, param_base
, ir
->lod_info
.lod
->type
, WRITEMASK_W
),
1898 } else if (ir
->op
== ir_txd
) {
1899 const glsl_type
*type
= ir
->lod_info
.grad
.dPdx
->type
;
1901 ir
->lod_info
.grad
.dPdx
->accept(this);
1902 src_reg dPdx
= this->result
;
1903 ir
->lod_info
.grad
.dPdy
->accept(this);
1904 src_reg dPdy
= this->result
;
1906 if (intel
->gen
>= 5) {
1907 dPdx
.swizzle
= BRW_SWIZZLE4(SWIZZLE_X
,SWIZZLE_X
,SWIZZLE_Y
,SWIZZLE_Y
);
1908 dPdy
.swizzle
= BRW_SWIZZLE4(SWIZZLE_X
,SWIZZLE_X
,SWIZZLE_Y
,SWIZZLE_Y
);
1909 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_XZ
), dPdx
));
1910 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_YW
), dPdy
));
1913 if (ir
->type
->vector_elements
== 3) {
1914 dPdx
.swizzle
= BRW_SWIZZLE_ZZZZ
;
1915 dPdy
.swizzle
= BRW_SWIZZLE_ZZZZ
;
1916 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_X
), dPdx
));
1917 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_Y
), dPdy
));
1920 } else /* intel->gen == 4 */ {
1921 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_XYZ
), dPdx
));
1922 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_XYZ
), dPdy
));
1930 swizzle_result(ir
, src_reg(inst
->dst
), sampler
);
1934 vec4_visitor::swizzle_result(ir_texture
*ir
, src_reg orig_val
, int sampler
)
1936 this->result
= orig_val
;
1938 int s
= c
->key
.tex
.swizzles
[sampler
];
1940 if (ir
->op
== ir_txs
|| ir
->type
== glsl_type::float_type
1941 || s
== SWIZZLE_NOOP
)
1944 int zero_mask
= 0, one_mask
= 0, copy_mask
= 0;
1947 for (int i
= 0; i
< 4; i
++) {
1948 switch (GET_SWZ(s
, i
)) {
1950 zero_mask
|= (1 << i
);
1953 one_mask
|= (1 << i
);
1956 copy_mask
|= (1 << i
);
1957 swizzle
[i
] = GET_SWZ(s
, i
);
1962 this->result
= src_reg(this, ir
->type
);
1963 dst_reg
swizzled_result(this->result
);
1966 orig_val
.swizzle
= BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1967 swizzled_result
.writemask
= copy_mask
;
1968 emit(MOV(swizzled_result
, orig_val
));
1972 swizzled_result
.writemask
= zero_mask
;
1973 emit(MOV(swizzled_result
, src_reg(0.0f
)));
1977 swizzled_result
.writemask
= one_mask
;
1978 emit(MOV(swizzled_result
, src_reg(1.0f
)));
1983 vec4_visitor::visit(ir_return
*ir
)
1985 assert(!"not reached");
1989 vec4_visitor::visit(ir_discard
*ir
)
1991 assert(!"not reached");
1995 vec4_visitor::visit(ir_if
*ir
)
1997 /* Don't point the annotation at the if statement, because then it plus
1998 * the then and else blocks get printed.
2000 this->base_ir
= ir
->condition
;
2002 if (intel
->gen
== 6) {
2006 emit_bool_to_cond_code(ir
->condition
, &predicate
);
2007 emit(IF(predicate
));
2010 visit_instructions(&ir
->then_instructions
);
2012 if (!ir
->else_instructions
.is_empty()) {
2013 this->base_ir
= ir
->condition
;
2014 emit(BRW_OPCODE_ELSE
);
2016 visit_instructions(&ir
->else_instructions
);
2019 this->base_ir
= ir
->condition
;
2020 emit(BRW_OPCODE_ENDIF
);
2024 vec4_visitor::emit_ndc_computation()
2026 /* Get the position */
2027 src_reg pos
= src_reg(output_reg
[VERT_RESULT_HPOS
]);
2029 /* Build ndc coords, which are (x/w, y/w, z/w, 1/w) */
2030 dst_reg ndc
= dst_reg(this, glsl_type::vec4_type
);
2031 output_reg
[BRW_VERT_RESULT_NDC
] = ndc
;
2033 current_annotation
= "NDC";
2034 dst_reg ndc_w
= ndc
;
2035 ndc_w
.writemask
= WRITEMASK_W
;
2036 src_reg pos_w
= pos
;
2037 pos_w
.swizzle
= BRW_SWIZZLE4(SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
);
2038 emit_math(SHADER_OPCODE_RCP
, ndc_w
, pos_w
);
2040 dst_reg ndc_xyz
= ndc
;
2041 ndc_xyz
.writemask
= WRITEMASK_XYZ
;
2043 emit(MUL(ndc_xyz
, pos
, src_reg(ndc_w
)));
2047 vec4_visitor::emit_psiz_and_flags(struct brw_reg reg
)
2049 if (intel
->gen
< 6 &&
2050 ((c
->prog_data
.outputs_written
& BITFIELD64_BIT(VERT_RESULT_PSIZ
)) ||
2051 c
->key
.userclip_active
|| brw
->has_negative_rhw_bug
)) {
2052 dst_reg header1
= dst_reg(this, glsl_type::uvec4_type
);
2053 dst_reg header1_w
= header1
;
2054 header1_w
.writemask
= WRITEMASK_W
;
2057 emit(MOV(header1
, 0u));
2059 if (c
->prog_data
.outputs_written
& BITFIELD64_BIT(VERT_RESULT_PSIZ
)) {
2060 src_reg psiz
= src_reg(output_reg
[VERT_RESULT_PSIZ
]);
2062 current_annotation
= "Point size";
2063 emit(MUL(header1_w
, psiz
, src_reg((float)(1 << 11))));
2064 emit(AND(header1_w
, src_reg(header1_w
), 0x7ff << 8));
2067 current_annotation
= "Clipping flags";
2068 for (i
= 0; i
< c
->key
.nr_userclip_plane_consts
; i
++) {
2069 vec4_instruction
*inst
;
2071 inst
= emit(DP4(dst_null_f(), src_reg(output_reg
[VERT_RESULT_HPOS
]),
2072 src_reg(this->userplane
[i
])));
2073 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
2075 inst
= emit(OR(header1_w
, src_reg(header1_w
), 1u << i
));
2076 inst
->predicate
= BRW_PREDICATE_NORMAL
;
2079 /* i965 clipping workaround:
2080 * 1) Test for -ve rhw
2082 * set ndc = (0,0,0,0)
2085 * Later, clipping will detect ucp[6] and ensure the primitive is
2086 * clipped against all fixed planes.
2088 if (brw
->has_negative_rhw_bug
) {
2092 vec8(brw_null_reg()),
2094 brw_swizzle1(output_reg
[BRW_VERT_RESULT_NDC
], 3),
2097 brw_OR(p
, brw_writemask(header1
, WRITEMASK_W
), header1
, brw_imm_ud(1<<6));
2098 brw_MOV(p
, output_reg
[BRW_VERT_RESULT_NDC
], brw_imm_f(0));
2099 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
2103 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_UD
), src_reg(header1
)));
2104 } else if (intel
->gen
< 6) {
2105 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_UD
), 0u));
2107 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_D
), src_reg(0)));
2108 if (c
->prog_data
.outputs_written
& BITFIELD64_BIT(VERT_RESULT_PSIZ
)) {
2109 emit(MOV(brw_writemask(reg
, WRITEMASK_W
),
2110 src_reg(output_reg
[VERT_RESULT_PSIZ
])));
2116 vec4_visitor::emit_clip_distances(struct brw_reg reg
, int offset
)
2118 if (intel
->gen
< 6) {
2119 /* Clip distance slots are set aside in gen5, but they are not used. It
2120 * is not clear whether we actually need to set aside space for them,
2121 * but the performance cost is negligible.
2126 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
2128 * "If a linked set of shaders forming the vertex stage contains no
2129 * static write to gl_ClipVertex or gl_ClipDistance, but the
2130 * application has requested clipping against user clip planes through
2131 * the API, then the coordinate written to gl_Position is used for
2132 * comparison against the user clip planes."
2134 * This function is only called if the shader didn't write to
2135 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
2136 * if the user wrote to it; otherwise we use gl_Position.
2138 gl_vert_result clip_vertex
= VERT_RESULT_CLIP_VERTEX
;
2139 if (!(c
->prog_data
.outputs_written
2140 & BITFIELD64_BIT(VERT_RESULT_CLIP_VERTEX
))) {
2141 clip_vertex
= VERT_RESULT_HPOS
;
2144 for (int i
= 0; i
+ offset
< c
->key
.nr_userclip_plane_consts
&& i
< 4;
2146 emit(DP4(dst_reg(brw_writemask(reg
, 1 << i
)),
2147 src_reg(output_reg
[clip_vertex
]),
2148 src_reg(this->userplane
[i
+ offset
])));
2153 vec4_visitor::emit_generic_urb_slot(dst_reg reg
, int vert_result
)
2155 assert (vert_result
< VERT_RESULT_MAX
);
2156 reg
.type
= output_reg
[vert_result
].type
;
2157 current_annotation
= output_reg_annotation
[vert_result
];
2158 /* Copy the register, saturating if necessary */
2159 vec4_instruction
*inst
= emit(MOV(reg
,
2160 src_reg(output_reg
[vert_result
])));
2161 if ((vert_result
== VERT_RESULT_COL0
||
2162 vert_result
== VERT_RESULT_COL1
||
2163 vert_result
== VERT_RESULT_BFC0
||
2164 vert_result
== VERT_RESULT_BFC1
) &&
2165 c
->key
.clamp_vertex_color
) {
2166 inst
->saturate
= true;
2171 vec4_visitor::emit_urb_slot(int mrf
, int vert_result
)
2173 struct brw_reg hw_reg
= brw_message_reg(mrf
);
2174 dst_reg reg
= dst_reg(MRF
, mrf
);
2175 reg
.type
= BRW_REGISTER_TYPE_F
;
2177 switch (vert_result
) {
2178 case VERT_RESULT_PSIZ
:
2179 /* PSIZ is always in slot 0, and is coupled with other flags. */
2180 current_annotation
= "indices, point width, clip flags";
2181 emit_psiz_and_flags(hw_reg
);
2183 case BRW_VERT_RESULT_NDC
:
2184 current_annotation
= "NDC";
2185 emit(MOV(reg
, src_reg(output_reg
[BRW_VERT_RESULT_NDC
])));
2187 case BRW_VERT_RESULT_HPOS_DUPLICATE
:
2188 case VERT_RESULT_HPOS
:
2189 current_annotation
= "gl_Position";
2190 emit(MOV(reg
, src_reg(output_reg
[VERT_RESULT_HPOS
])));
2192 case VERT_RESULT_CLIP_DIST0
:
2193 case VERT_RESULT_CLIP_DIST1
:
2194 if (this->c
->key
.uses_clip_distance
) {
2195 emit_generic_urb_slot(reg
, vert_result
);
2197 current_annotation
= "user clip distances";
2198 emit_clip_distances(hw_reg
, (vert_result
- VERT_RESULT_CLIP_DIST0
) * 4);
2201 case BRW_VERT_RESULT_PAD
:
2202 /* No need to write to this slot */
2205 emit_generic_urb_slot(reg
, vert_result
);
2211 align_interleaved_urb_mlen(struct brw_context
*brw
, int mlen
)
2213 struct intel_context
*intel
= &brw
->intel
;
2215 if (intel
->gen
>= 6) {
2216 /* URB data written (does not include the message header reg) must
2217 * be a multiple of 256 bits, or 2 VS registers. See vol5c.5,
2218 * section 5.4.3.2.2: URB_INTERLEAVED.
2220 * URB entries are allocated on a multiple of 1024 bits, so an
2221 * extra 128 bits written here to make the end align to 256 is
2224 if ((mlen
% 2) != 1)
2232 * Generates the VUE payload plus the 1 or 2 URB write instructions to
2233 * complete the VS thread.
2235 * The VUE layout is documented in Volume 2a.
2238 vec4_visitor::emit_urb_writes()
2240 /* MRF 0 is reserved for the debugger, so start with message header
2245 /* In the process of generating our URB write message contents, we
2246 * may need to unspill a register or load from an array. Those
2247 * reads would use MRFs 14-15.
2249 int max_usable_mrf
= 13;
2251 /* The following assertion verifies that max_usable_mrf causes an
2252 * even-numbered amount of URB write data, which will meet gen6's
2253 * requirements for length alignment.
2255 assert ((max_usable_mrf
- base_mrf
) % 2 == 0);
2257 /* FINISHME: edgeflag */
2259 /* First mrf is the g0-based message header containing URB handles and such,
2260 * which is implied in VS_OPCODE_URB_WRITE.
2264 if (intel
->gen
< 6) {
2265 emit_ndc_computation();
2268 /* Set up the VUE data for the first URB write */
2270 for (slot
= 0; slot
< c
->prog_data
.vue_map
.num_slots
; ++slot
) {
2271 emit_urb_slot(mrf
++, c
->prog_data
.vue_map
.slot_to_vert_result
[slot
]);
2273 /* If this was max_usable_mrf, we can't fit anything more into this URB
2276 if (mrf
> max_usable_mrf
) {
2282 current_annotation
= "URB write";
2283 vec4_instruction
*inst
= emit(VS_OPCODE_URB_WRITE
);
2284 inst
->base_mrf
= base_mrf
;
2285 inst
->mlen
= align_interleaved_urb_mlen(brw
, mrf
- base_mrf
);
2286 inst
->eot
= (slot
>= c
->prog_data
.vue_map
.num_slots
);
2288 /* Optional second URB write */
2292 for (; slot
< c
->prog_data
.vue_map
.num_slots
; ++slot
) {
2293 assert(mrf
< max_usable_mrf
);
2295 emit_urb_slot(mrf
++, c
->prog_data
.vue_map
.slot_to_vert_result
[slot
]);
2298 current_annotation
= "URB write";
2299 inst
= emit(VS_OPCODE_URB_WRITE
);
2300 inst
->base_mrf
= base_mrf
;
2301 inst
->mlen
= align_interleaved_urb_mlen(brw
, mrf
- base_mrf
);
2303 /* URB destination offset. In the previous write, we got MRFs
2304 * 2-13 minus the one header MRF, so 12 regs. URB offset is in
2305 * URB row increments, and each of our MRFs is half of one of
2306 * those, since we're doing interleaved writes.
2308 inst
->offset
= (max_usable_mrf
- base_mrf
) / 2;
2313 vec4_visitor::get_scratch_offset(vec4_instruction
*inst
,
2314 src_reg
*reladdr
, int reg_offset
)
2316 /* Because we store the values to scratch interleaved like our
2317 * vertex data, we need to scale the vec4 index by 2.
2319 int message_header_scale
= 2;
2321 /* Pre-gen6, the message header uses byte offsets instead of vec4
2322 * (16-byte) offset units.
2325 message_header_scale
*= 16;
2328 src_reg index
= src_reg(this, glsl_type::int_type
);
2330 emit_before(inst
, ADD(dst_reg(index
), *reladdr
, src_reg(reg_offset
)));
2331 emit_before(inst
, MUL(dst_reg(index
),
2332 index
, src_reg(message_header_scale
)));
2336 return src_reg(reg_offset
* message_header_scale
);
2341 vec4_visitor::get_pull_constant_offset(vec4_instruction
*inst
,
2342 src_reg
*reladdr
, int reg_offset
)
2345 src_reg index
= src_reg(this, glsl_type::int_type
);
2347 emit_before(inst
, ADD(dst_reg(index
), *reladdr
, src_reg(reg_offset
)));
2349 /* Pre-gen6, the message header uses byte offsets instead of vec4
2350 * (16-byte) offset units.
2352 if (intel
->gen
< 6) {
2353 emit_before(inst
, MUL(dst_reg(index
), index
, src_reg(16)));
2358 int message_header_scale
= intel
->gen
< 6 ? 16 : 1;
2359 return src_reg(reg_offset
* message_header_scale
);
2364 * Emits an instruction before @inst to load the value named by @orig_src
2365 * from scratch space at @base_offset to @temp.
2368 vec4_visitor::emit_scratch_read(vec4_instruction
*inst
,
2369 dst_reg temp
, src_reg orig_src
,
2372 int reg_offset
= base_offset
+ orig_src
.reg_offset
;
2373 src_reg index
= get_scratch_offset(inst
, orig_src
.reladdr
, reg_offset
);
2375 emit_before(inst
, SCRATCH_READ(temp
, index
));
2379 * Emits an instruction after @inst to store the value to be written
2380 * to @orig_dst to scratch space at @base_offset, from @temp.
2383 vec4_visitor::emit_scratch_write(vec4_instruction
*inst
,
2384 src_reg temp
, dst_reg orig_dst
,
2387 int reg_offset
= base_offset
+ orig_dst
.reg_offset
;
2388 src_reg index
= get_scratch_offset(inst
, orig_dst
.reladdr
, reg_offset
);
2390 dst_reg dst
= dst_reg(brw_writemask(brw_vec8_grf(0, 0),
2391 orig_dst
.writemask
));
2392 vec4_instruction
*write
= SCRATCH_WRITE(dst
, temp
, index
);
2393 write
->predicate
= inst
->predicate
;
2394 write
->ir
= inst
->ir
;
2395 write
->annotation
= inst
->annotation
;
2396 inst
->insert_after(write
);
2400 * We can't generally support array access in GRF space, because a
2401 * single instruction's destination can only span 2 contiguous
2402 * registers. So, we send all GRF arrays that get variable index
2403 * access to scratch space.
2406 vec4_visitor::move_grf_array_access_to_scratch()
2408 int scratch_loc
[this->virtual_grf_count
];
2410 for (int i
= 0; i
< this->virtual_grf_count
; i
++) {
2411 scratch_loc
[i
] = -1;
2414 /* First, calculate the set of virtual GRFs that need to be punted
2415 * to scratch due to having any array access on them, and where in
2418 foreach_list(node
, &this->instructions
) {
2419 vec4_instruction
*inst
= (vec4_instruction
*)node
;
2421 if (inst
->dst
.file
== GRF
&& inst
->dst
.reladdr
&&
2422 scratch_loc
[inst
->dst
.reg
] == -1) {
2423 scratch_loc
[inst
->dst
.reg
] = c
->last_scratch
;
2424 c
->last_scratch
+= this->virtual_grf_sizes
[inst
->dst
.reg
] * 8 * 4;
2427 for (int i
= 0 ; i
< 3; i
++) {
2428 src_reg
*src
= &inst
->src
[i
];
2430 if (src
->file
== GRF
&& src
->reladdr
&&
2431 scratch_loc
[src
->reg
] == -1) {
2432 scratch_loc
[src
->reg
] = c
->last_scratch
;
2433 c
->last_scratch
+= this->virtual_grf_sizes
[src
->reg
] * 8 * 4;
2438 /* Now, for anything that will be accessed through scratch, rewrite
2439 * it to load/store. Note that this is a _safe list walk, because
2440 * we may generate a new scratch_write instruction after the one
2443 foreach_list_safe(node
, &this->instructions
) {
2444 vec4_instruction
*inst
= (vec4_instruction
*)node
;
2446 /* Set up the annotation tracking for new generated instructions. */
2448 current_annotation
= inst
->annotation
;
2450 if (inst
->dst
.file
== GRF
&& scratch_loc
[inst
->dst
.reg
] != -1) {
2451 src_reg temp
= src_reg(this, glsl_type::vec4_type
);
2453 emit_scratch_write(inst
, temp
, inst
->dst
, scratch_loc
[inst
->dst
.reg
]);
2455 inst
->dst
.file
= temp
.file
;
2456 inst
->dst
.reg
= temp
.reg
;
2457 inst
->dst
.reg_offset
= temp
.reg_offset
;
2458 inst
->dst
.reladdr
= NULL
;
2461 for (int i
= 0 ; i
< 3; i
++) {
2462 if (inst
->src
[i
].file
!= GRF
|| scratch_loc
[inst
->src
[i
].reg
] == -1)
2465 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
2467 emit_scratch_read(inst
, temp
, inst
->src
[i
],
2468 scratch_loc
[inst
->src
[i
].reg
]);
2470 inst
->src
[i
].file
= temp
.file
;
2471 inst
->src
[i
].reg
= temp
.reg
;
2472 inst
->src
[i
].reg_offset
= temp
.reg_offset
;
2473 inst
->src
[i
].reladdr
= NULL
;
2479 * Emits an instruction before @inst to load the value named by @orig_src
2480 * from the pull constant buffer (surface) at @base_offset to @temp.
2483 vec4_visitor::emit_pull_constant_load(vec4_instruction
*inst
,
2484 dst_reg temp
, src_reg orig_src
,
2487 int reg_offset
= base_offset
+ orig_src
.reg_offset
;
2488 src_reg index
= get_pull_constant_offset(inst
, orig_src
.reladdr
, reg_offset
);
2489 vec4_instruction
*load
;
2491 load
= new(mem_ctx
) vec4_instruction(this, VS_OPCODE_PULL_CONSTANT_LOAD
,
2493 load
->base_mrf
= 14;
2495 emit_before(inst
, load
);
2499 * Implements array access of uniforms by inserting a
2500 * PULL_CONSTANT_LOAD instruction.
2502 * Unlike temporary GRF array access (where we don't support it due to
2503 * the difficulty of doing relative addressing on instruction
2504 * destinations), we could potentially do array access of uniforms
2505 * that were loaded in GRF space as push constants. In real-world
2506 * usage we've seen, though, the arrays being used are always larger
2507 * than we could load as push constants, so just always move all
2508 * uniform array access out to a pull constant buffer.
2511 vec4_visitor::move_uniform_array_access_to_pull_constants()
2513 int pull_constant_loc
[this->uniforms
];
2515 for (int i
= 0; i
< this->uniforms
; i
++) {
2516 pull_constant_loc
[i
] = -1;
2519 /* Walk through and find array access of uniforms. Put a copy of that
2520 * uniform in the pull constant buffer.
2522 * Note that we don't move constant-indexed accesses to arrays. No
2523 * testing has been done of the performance impact of this choice.
2525 foreach_list_safe(node
, &this->instructions
) {
2526 vec4_instruction
*inst
= (vec4_instruction
*)node
;
2528 for (int i
= 0 ; i
< 3; i
++) {
2529 if (inst
->src
[i
].file
!= UNIFORM
|| !inst
->src
[i
].reladdr
)
2532 int uniform
= inst
->src
[i
].reg
;
2534 /* If this array isn't already present in the pull constant buffer,
2537 if (pull_constant_loc
[uniform
] == -1) {
2538 const float **values
= &prog_data
->param
[uniform
* 4];
2540 pull_constant_loc
[uniform
] = prog_data
->nr_pull_params
/ 4;
2542 for (int j
= 0; j
< uniform_size
[uniform
] * 4; j
++) {
2543 prog_data
->pull_param
[prog_data
->nr_pull_params
++] = values
[j
];
2547 /* Set up the annotation tracking for new generated instructions. */
2549 current_annotation
= inst
->annotation
;
2551 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
2553 emit_pull_constant_load(inst
, temp
, inst
->src
[i
],
2554 pull_constant_loc
[uniform
]);
2556 inst
->src
[i
].file
= temp
.file
;
2557 inst
->src
[i
].reg
= temp
.reg
;
2558 inst
->src
[i
].reg_offset
= temp
.reg_offset
;
2559 inst
->src
[i
].reladdr
= NULL
;
2563 /* Now there are no accesses of the UNIFORM file with a reladdr, so
2564 * no need to track them as larger-than-vec4 objects. This will be
2565 * relied on in cutting out unused uniform vectors from push
2568 split_uniform_registers();
2572 vec4_visitor::resolve_ud_negate(src_reg
*reg
)
2574 if (reg
->type
!= BRW_REGISTER_TYPE_UD
||
2578 src_reg temp
= src_reg(this, glsl_type::uvec4_type
);
2579 emit(BRW_OPCODE_MOV
, dst_reg(temp
), *reg
);
2583 vec4_visitor::vec4_visitor(struct brw_vs_compile
*c
,
2584 struct gl_shader_program
*prog
,
2585 struct brw_shader
*shader
)
2590 this->intel
= &brw
->intel
;
2591 this->ctx
= &intel
->ctx
;
2593 this->shader
= shader
;
2595 this->mem_ctx
= ralloc_context(NULL
);
2596 this->failed
= false;
2598 this->base_ir
= NULL
;
2599 this->current_annotation
= NULL
;
2602 this->vp
= (struct gl_vertex_program
*)
2603 prog
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
2604 this->prog_data
= &c
->prog_data
;
2606 this->variable_ht
= hash_table_ctor(0,
2607 hash_table_pointer_hash
,
2608 hash_table_pointer_compare
);
2610 this->virtual_grf_def
= NULL
;
2611 this->virtual_grf_use
= NULL
;
2612 this->virtual_grf_sizes
= NULL
;
2613 this->virtual_grf_count
= 0;
2614 this->virtual_grf_reg_map
= NULL
;
2615 this->virtual_grf_reg_count
= 0;
2616 this->virtual_grf_array_size
= 0;
2617 this->live_intervals_valid
= false;
2619 this->max_grf
= intel
->gen
>= 7 ? GEN7_MRF_HACK_START
: BRW_MAX_GRF
;
2624 vec4_visitor::~vec4_visitor()
2626 ralloc_free(this->mem_ctx
);
2627 hash_table_dtor(this->variable_ht
);
2632 vec4_visitor::fail(const char *format
, ...)
2642 va_start(va
, format
);
2643 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
2645 msg
= ralloc_asprintf(mem_ctx
, "VS compile failed: %s\n", msg
);
2647 this->fail_msg
= msg
;
2649 if (INTEL_DEBUG
& DEBUG_VS
) {
2650 fprintf(stderr
, "%s", msg
);
2654 } /* namespace brw */