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
25 #include "glsl/ir_uniform.h"
27 #include "program/sampler.h"
32 vec4_instruction::vec4_instruction(vec4_visitor
*v
,
33 enum opcode opcode
, dst_reg dst
,
34 src_reg src0
, src_reg src1
, src_reg src2
)
36 this->opcode
= opcode
;
41 this->saturate
= false;
42 this->force_writemask_all
= false;
43 this->no_dd_clear
= false;
44 this->no_dd_check
= false;
45 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
47 this->texture_offset
= 0;
49 this->shadow_compare
= false;
50 this->ir
= v
->base_ir
;
51 this->urb_write_flags
= BRW_URB_WRITE_NO_FLAGS
;
52 this->header_present
= false;
56 this->annotation
= v
->current_annotation
;
60 vec4_visitor::emit(vec4_instruction
*inst
)
62 this->instructions
.push_tail(inst
);
68 vec4_visitor::emit_before(vec4_instruction
*inst
, vec4_instruction
*new_inst
)
70 new_inst
->ir
= inst
->ir
;
71 new_inst
->annotation
= inst
->annotation
;
73 inst
->insert_before(new_inst
);
79 vec4_visitor::emit(enum opcode opcode
, dst_reg dst
,
80 src_reg src0
, src_reg src1
, src_reg src2
)
82 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst
,
88 vec4_visitor::emit(enum opcode opcode
, dst_reg dst
, src_reg src0
, src_reg src1
)
90 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst
, src0
, src1
));
94 vec4_visitor::emit(enum opcode opcode
, dst_reg dst
, src_reg src0
)
96 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst
, src0
));
100 vec4_visitor::emit(enum opcode opcode
, dst_reg dst
)
102 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst
));
106 vec4_visitor::emit(enum opcode opcode
)
108 return emit(new(mem_ctx
) vec4_instruction(this, opcode
, dst_reg()));
113 vec4_visitor::op(dst_reg dst, src_reg src0) \
115 return new(mem_ctx) vec4_instruction(this, BRW_OPCODE_##op, dst, \
121 vec4_visitor::op(dst_reg dst, src_reg src0, src_reg src1) \
123 return new(mem_ctx) vec4_instruction(this, BRW_OPCODE_##op, dst, \
129 vec4_visitor::op(dst_reg dst, src_reg src0, src_reg src1, src_reg src2)\
131 assert(brw->gen >= 6); \
132 return new(mem_ctx) vec4_instruction(this, BRW_OPCODE_##op, dst, \
168 /** Gen4 predicated IF. */
170 vec4_visitor::IF(uint32_t predicate
)
172 vec4_instruction
*inst
;
174 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_IF
);
175 inst
->predicate
= predicate
;
180 /** Gen6 IF with embedded comparison. */
182 vec4_visitor::IF(src_reg src0
, src_reg src1
, uint32_t condition
)
184 assert(brw
->gen
== 6);
186 vec4_instruction
*inst
;
188 resolve_ud_negate(&src0
);
189 resolve_ud_negate(&src1
);
191 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_IF
, dst_null_d(),
193 inst
->conditional_mod
= condition
;
199 * CMP: Sets the low bit of the destination channels with the result
200 * of the comparison, while the upper bits are undefined, and updates
201 * the flag register with the packed 16 bits of the result.
204 vec4_visitor::CMP(dst_reg dst
, src_reg src0
, src_reg src1
, uint32_t condition
)
206 vec4_instruction
*inst
;
208 /* original gen4 does type conversion to the destination type
209 * before before comparison, producing garbage results for floating
213 dst
.type
= src0
.type
;
214 if (dst
.file
== HW_REG
)
215 dst
.fixed_hw_reg
.type
= dst
.type
;
218 resolve_ud_negate(&src0
);
219 resolve_ud_negate(&src1
);
221 inst
= new(mem_ctx
) vec4_instruction(this, BRW_OPCODE_CMP
, dst
, src0
, src1
);
222 inst
->conditional_mod
= condition
;
228 vec4_visitor::SCRATCH_READ(dst_reg dst
, src_reg index
)
230 vec4_instruction
*inst
;
232 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_GEN4_SCRATCH_READ
,
241 vec4_visitor::SCRATCH_WRITE(dst_reg dst
, src_reg src
, src_reg index
)
243 vec4_instruction
*inst
;
245 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_GEN4_SCRATCH_WRITE
,
254 vec4_visitor::emit_dp(dst_reg dst
, src_reg src0
, src_reg src1
, unsigned elements
)
256 static enum opcode dot_opcodes
[] = {
257 BRW_OPCODE_DP2
, BRW_OPCODE_DP3
, BRW_OPCODE_DP4
260 emit(dot_opcodes
[elements
- 2], dst
, src0
, src1
);
264 vec4_visitor::fix_3src_operand(src_reg src
)
266 /* Using vec4 uniforms in SIMD4x2 programs is difficult. You'd like to be
267 * able to use vertical stride of zero to replicate the vec4 uniform, like
269 * g3<0;4,1>:f - [0, 4][1, 5][2, 6][3, 7]
271 * But you can't, since vertical stride is always four in three-source
272 * instructions. Instead, insert a MOV instruction to do the replication so
273 * that the three-source instruction can consume it.
276 /* The MOV is only needed if the source is a uniform or immediate. */
277 if (src
.file
!= UNIFORM
&& src
.file
!= IMM
)
280 if (src
.file
== UNIFORM
&& brw_is_single_value_swizzle(src
.swizzle
))
283 dst_reg expanded
= dst_reg(this, glsl_type::vec4_type
);
284 expanded
.type
= src
.type
;
285 emit(MOV(expanded
, src
));
286 return src_reg(expanded
);
290 vec4_visitor::fix_math_operand(src_reg src
)
292 /* The gen6 math instruction ignores the source modifiers --
293 * swizzle, abs, negate, and at least some parts of the register
294 * region description.
296 * Rather than trying to enumerate all these cases, *always* expand the
297 * operand to a temp GRF for gen6.
299 * For gen7, keep the operand as-is, except if immediate, which gen7 still
303 if (brw
->gen
== 7 && src
.file
!= IMM
)
306 dst_reg expanded
= dst_reg(this, glsl_type::vec4_type
);
307 expanded
.type
= src
.type
;
308 emit(MOV(expanded
, src
));
309 return src_reg(expanded
);
313 vec4_visitor::emit_math1_gen6(enum opcode opcode
, dst_reg dst
, src_reg src
)
315 src
= fix_math_operand(src
);
317 if (dst
.writemask
!= WRITEMASK_XYZW
) {
318 /* The gen6 math instruction must be align1, so we can't do
321 dst_reg temp_dst
= dst_reg(this, glsl_type::vec4_type
);
323 emit(opcode
, temp_dst
, src
);
325 emit(MOV(dst
, src_reg(temp_dst
)));
327 emit(opcode
, dst
, src
);
332 vec4_visitor::emit_math1_gen4(enum opcode opcode
, dst_reg dst
, src_reg src
)
334 vec4_instruction
*inst
= emit(opcode
, dst
, src
);
340 vec4_visitor::emit_math(opcode opcode
, dst_reg dst
, src_reg src
)
343 case SHADER_OPCODE_RCP
:
344 case SHADER_OPCODE_RSQ
:
345 case SHADER_OPCODE_SQRT
:
346 case SHADER_OPCODE_EXP2
:
347 case SHADER_OPCODE_LOG2
:
348 case SHADER_OPCODE_SIN
:
349 case SHADER_OPCODE_COS
:
352 assert(!"not reached: bad math opcode");
357 return emit_math1_gen6(opcode
, dst
, src
);
359 return emit_math1_gen4(opcode
, dst
, src
);
364 vec4_visitor::emit_math2_gen6(enum opcode opcode
,
365 dst_reg dst
, src_reg src0
, src_reg src1
)
367 src0
= fix_math_operand(src0
);
368 src1
= fix_math_operand(src1
);
370 if (dst
.writemask
!= WRITEMASK_XYZW
) {
371 /* The gen6 math instruction must be align1, so we can't do
374 dst_reg temp_dst
= dst_reg(this, glsl_type::vec4_type
);
375 temp_dst
.type
= dst
.type
;
377 emit(opcode
, temp_dst
, src0
, src1
);
379 emit(MOV(dst
, src_reg(temp_dst
)));
381 emit(opcode
, dst
, src0
, src1
);
386 vec4_visitor::emit_math2_gen4(enum opcode opcode
,
387 dst_reg dst
, src_reg src0
, src_reg src1
)
389 vec4_instruction
*inst
= emit(opcode
, dst
, src0
, src1
);
395 vec4_visitor::emit_math(enum opcode opcode
,
396 dst_reg dst
, src_reg src0
, src_reg src1
)
399 case SHADER_OPCODE_POW
:
400 case SHADER_OPCODE_INT_QUOTIENT
:
401 case SHADER_OPCODE_INT_REMAINDER
:
404 assert(!"not reached: unsupported binary math opcode");
409 return emit_math2_gen6(opcode
, dst
, src0
, src1
);
411 return emit_math2_gen4(opcode
, dst
, src0
, src1
);
416 vec4_visitor::emit_pack_half_2x16(dst_reg dst
, src_reg src0
)
419 assert(!"ir_unop_pack_half_2x16 should be lowered");
421 assert(dst
.type
== BRW_REGISTER_TYPE_UD
);
422 assert(src0
.type
== BRW_REGISTER_TYPE_F
);
424 /* From the Ivybridge PRM, Vol4, Part3, Section 6.27 f32to16:
426 * Because this instruction does not have a 16-bit floating-point type,
427 * the destination data type must be Word (W).
429 * The destination must be DWord-aligned and specify a horizontal stride
430 * (HorzStride) of 2. The 16-bit result is stored in the lower word of
431 * each destination channel and the upper word is not modified.
433 * The above restriction implies that the f32to16 instruction must use
434 * align1 mode, because only in align1 mode is it possible to specify
435 * horizontal stride. We choose here to defy the hardware docs and emit
436 * align16 instructions.
438 * (I [chadv] did attempt to emit align1 instructions for VS f32to16
439 * instructions. I was partially successful in that the code passed all
440 * tests. However, the code was dubiously correct and fragile, and the
441 * tests were not harsh enough to probe that frailty. Not trusting the
442 * code, I chose instead to remain in align16 mode in defiance of the hw
445 * I've [chadv] experimentally confirmed that, on gen7 hardware and the
446 * simulator, emitting a f32to16 in align16 mode with UD as destination
447 * data type is safe. The behavior differs from that specified in the PRM
448 * in that the upper word of each destination channel is cleared to 0.
451 dst_reg
tmp_dst(this, glsl_type::uvec2_type
);
452 src_reg
tmp_src(tmp_dst
);
455 /* Verify the undocumented behavior on which the following instructions
456 * rely. If f32to16 fails to clear the upper word of the X and Y channels,
457 * then the result of the bit-or instruction below will be incorrect.
459 * You should inspect the disasm output in order to verify that the MOV is
460 * not optimized away.
462 emit(MOV(tmp_dst
, src_reg(0x12345678u
)));
465 /* Give tmp the form below, where "." means untouched.
468 * |.|.|0x0000hhhh|0x0000llll|.|.|0x0000hhhh|0x0000llll|
470 * That the upper word of each write-channel be 0 is required for the
471 * following bit-shift and bit-or instructions to work. Note that this
472 * relies on the undocumented hardware behavior mentioned above.
474 tmp_dst
.writemask
= WRITEMASK_XY
;
475 emit(F32TO16(tmp_dst
, src0
));
477 /* Give the write-channels of dst the form:
480 tmp_src
.swizzle
= BRW_SWIZZLE_YYYY
;
481 emit(SHL(dst
, tmp_src
, src_reg(16u)));
483 /* Finally, give the write-channels of dst the form of packHalf2x16's
487 tmp_src
.swizzle
= BRW_SWIZZLE_XXXX
;
488 emit(OR(dst
, src_reg(dst
), tmp_src
));
492 vec4_visitor::emit_unpack_half_2x16(dst_reg dst
, src_reg src0
)
495 assert(!"ir_unop_unpack_half_2x16 should be lowered");
497 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
498 assert(src0
.type
== BRW_REGISTER_TYPE_UD
);
500 /* From the Ivybridge PRM, Vol4, Part3, Section 6.26 f16to32:
502 * Because this instruction does not have a 16-bit floating-point type,
503 * the source data type must be Word (W). The destination type must be
506 * To use W as the source data type, we must adjust horizontal strides,
507 * which is only possible in align1 mode. All my [chadv] attempts at
508 * emitting align1 instructions for unpackHalf2x16 failed to pass the
509 * Piglit tests, so I gave up.
511 * I've verified that, on gen7 hardware and the simulator, it is safe to
512 * emit f16to32 in align16 mode with UD as source data type.
515 dst_reg
tmp_dst(this, glsl_type::uvec2_type
);
516 src_reg
tmp_src(tmp_dst
);
518 tmp_dst
.writemask
= WRITEMASK_X
;
519 emit(AND(tmp_dst
, src0
, src_reg(0xffffu
)));
521 tmp_dst
.writemask
= WRITEMASK_Y
;
522 emit(SHR(tmp_dst
, src0
, src_reg(16u)));
524 dst
.writemask
= WRITEMASK_XY
;
525 emit(F16TO32(dst
, tmp_src
));
529 vec4_visitor::visit_instructions(const exec_list
*list
)
531 foreach_list(node
, list
) {
532 ir_instruction
*ir
= (ir_instruction
*)node
;
541 type_size(const struct glsl_type
*type
)
546 switch (type
->base_type
) {
549 case GLSL_TYPE_FLOAT
:
551 if (type
->is_matrix()) {
552 return type
->matrix_columns
;
554 /* Regardless of size of vector, it gets a vec4. This is bad
555 * packing for things like floats, but otherwise arrays become a
556 * mess. Hopefully a later pass over the code can pack scalars
557 * down if appropriate.
561 case GLSL_TYPE_ARRAY
:
562 assert(type
->length
> 0);
563 return type_size(type
->fields
.array
) * type
->length
;
564 case GLSL_TYPE_STRUCT
:
566 for (i
= 0; i
< type
->length
; i
++) {
567 size
+= type_size(type
->fields
.structure
[i
].type
);
570 case GLSL_TYPE_SAMPLER
:
571 /* Samplers take up one slot in UNIFORMS[], but they're baked in
575 case GLSL_TYPE_ATOMIC_UINT
:
577 case GLSL_TYPE_IMAGE
:
579 case GLSL_TYPE_ERROR
:
580 case GLSL_TYPE_INTERFACE
:
589 vec4_visitor::virtual_grf_alloc(int size
)
591 if (virtual_grf_array_size
<= virtual_grf_count
) {
592 if (virtual_grf_array_size
== 0)
593 virtual_grf_array_size
= 16;
595 virtual_grf_array_size
*= 2;
596 virtual_grf_sizes
= reralloc(mem_ctx
, virtual_grf_sizes
, int,
597 virtual_grf_array_size
);
598 virtual_grf_reg_map
= reralloc(mem_ctx
, virtual_grf_reg_map
, int,
599 virtual_grf_array_size
);
601 virtual_grf_reg_map
[virtual_grf_count
] = virtual_grf_reg_count
;
602 virtual_grf_reg_count
+= size
;
603 virtual_grf_sizes
[virtual_grf_count
] = size
;
604 return virtual_grf_count
++;
607 src_reg::src_reg(class vec4_visitor
*v
, const struct glsl_type
*type
)
612 this->reg
= v
->virtual_grf_alloc(type_size(type
));
614 if (type
->is_array() || type
->is_record()) {
615 this->swizzle
= BRW_SWIZZLE_NOOP
;
617 this->swizzle
= swizzle_for_size(type
->vector_elements
);
620 this->type
= brw_type_for_base_type(type
);
623 dst_reg::dst_reg(class vec4_visitor
*v
, const struct glsl_type
*type
)
628 this->reg
= v
->virtual_grf_alloc(type_size(type
));
630 if (type
->is_array() || type
->is_record()) {
631 this->writemask
= WRITEMASK_XYZW
;
633 this->writemask
= (1 << type
->vector_elements
) - 1;
636 this->type
= brw_type_for_base_type(type
);
639 /* Our support for uniforms is piggy-backed on the struct
640 * gl_fragment_program, because that's where the values actually
641 * get stored, rather than in some global gl_shader_program uniform
645 vec4_visitor::setup_uniform_values(ir_variable
*ir
)
647 int namelen
= strlen(ir
->name
);
649 /* The data for our (non-builtin) uniforms is stored in a series of
650 * gl_uniform_driver_storage structs for each subcomponent that
651 * glGetUniformLocation() could name. We know it's been set up in the same
652 * order we'd walk the type, so walk the list of storage and find anything
653 * with our name, or the prefix of a component that starts with our name.
655 for (unsigned u
= 0; u
< shader_prog
->NumUserUniformStorage
; u
++) {
656 struct gl_uniform_storage
*storage
= &shader_prog
->UniformStorage
[u
];
658 if (strncmp(ir
->name
, storage
->name
, namelen
) != 0 ||
659 (storage
->name
[namelen
] != 0 &&
660 storage
->name
[namelen
] != '.' &&
661 storage
->name
[namelen
] != '[')) {
665 gl_constant_value
*components
= storage
->storage
;
666 unsigned vector_count
= (MAX2(storage
->array_elements
, 1) *
667 storage
->type
->matrix_columns
);
669 for (unsigned s
= 0; s
< vector_count
; s
++) {
670 assert(uniforms
< uniform_array_size
);
671 uniform_vector_size
[uniforms
] = storage
->type
->vector_elements
;
674 for (i
= 0; i
< uniform_vector_size
[uniforms
]; i
++) {
675 stage_prog_data
->param
[uniforms
* 4 + i
] = &components
->f
;
679 static float zero
= 0;
680 stage_prog_data
->param
[uniforms
* 4 + i
] = &zero
;
689 vec4_visitor::setup_uniform_clipplane_values()
691 gl_clip_plane
*clip_planes
= brw_select_clip_planes(ctx
);
693 for (int i
= 0; i
< key
->nr_userclip_plane_consts
; ++i
) {
694 assert(this->uniforms
< uniform_array_size
);
695 this->uniform_vector_size
[this->uniforms
] = 4;
696 this->userplane
[i
] = dst_reg(UNIFORM
, this->uniforms
);
697 this->userplane
[i
].type
= BRW_REGISTER_TYPE_F
;
698 for (int j
= 0; j
< 4; ++j
) {
699 stage_prog_data
->param
[this->uniforms
* 4 + j
] = &clip_planes
[i
][j
];
705 /* Our support for builtin uniforms is even scarier than non-builtin.
706 * It sits on top of the PROG_STATE_VAR parameters that are
707 * automatically updated from GL context state.
710 vec4_visitor::setup_builtin_uniform_values(ir_variable
*ir
)
712 const ir_state_slot
*const slots
= ir
->state_slots
;
713 assert(ir
->state_slots
!= NULL
);
715 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
716 /* This state reference has already been setup by ir_to_mesa,
717 * but we'll get the same index back here. We can reference
718 * ParameterValues directly, since unlike brw_fs.cpp, we never
719 * add new state references during compile.
721 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
722 (gl_state_index
*)slots
[i
].tokens
);
723 float *values
= &this->prog
->Parameters
->ParameterValues
[index
][0].f
;
725 assert(this->uniforms
< uniform_array_size
);
726 this->uniform_vector_size
[this->uniforms
] = 0;
727 /* Add each of the unique swizzled channels of the element.
728 * This will end up matching the size of the glsl_type of this field.
731 for (unsigned int j
= 0; j
< 4; j
++) {
732 int swiz
= GET_SWZ(slots
[i
].swizzle
, j
);
735 stage_prog_data
->param
[this->uniforms
* 4 + j
] = &values
[swiz
];
736 assert(this->uniforms
< uniform_array_size
);
737 if (swiz
<= last_swiz
)
738 this->uniform_vector_size
[this->uniforms
]++;
745 vec4_visitor::variable_storage(ir_variable
*var
)
747 return (dst_reg
*)hash_table_find(this->variable_ht
, var
);
751 vec4_visitor::emit_bool_to_cond_code(ir_rvalue
*ir
, uint32_t *predicate
)
753 ir_expression
*expr
= ir
->as_expression();
755 *predicate
= BRW_PREDICATE_NORMAL
;
759 vec4_instruction
*inst
;
761 assert(expr
->get_num_operands() <= 2);
762 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
763 expr
->operands
[i
]->accept(this);
764 op
[i
] = this->result
;
766 resolve_ud_negate(&op
[i
]);
769 switch (expr
->operation
) {
770 case ir_unop_logic_not
:
771 inst
= emit(AND(dst_null_d(), op
[0], src_reg(1)));
772 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
775 case ir_binop_logic_xor
:
776 inst
= emit(XOR(dst_null_d(), op
[0], op
[1]));
777 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
780 case ir_binop_logic_or
:
781 inst
= emit(OR(dst_null_d(), op
[0], op
[1]));
782 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
785 case ir_binop_logic_and
:
786 inst
= emit(AND(dst_null_d(), op
[0], op
[1]));
787 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
792 emit(CMP(dst_null_d(), op
[0], src_reg(0.0f
), BRW_CONDITIONAL_NZ
));
794 inst
= emit(MOV(dst_null_f(), op
[0]));
795 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
801 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
803 inst
= emit(MOV(dst_null_d(), op
[0]));
804 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
808 case ir_binop_all_equal
:
809 inst
= emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
810 *predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
813 case ir_binop_any_nequal
:
814 inst
= emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
815 *predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
819 inst
= emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
820 *predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
823 case ir_binop_greater
:
824 case ir_binop_gequal
:
826 case ir_binop_lequal
:
828 case ir_binop_nequal
:
829 emit(CMP(dst_null_d(), op
[0], op
[1],
830 brw_conditional_for_comparison(expr
->operation
)));
834 assert(!"not reached");
842 resolve_ud_negate(&this->result
);
845 vec4_instruction
*inst
= emit(AND(dst_null_d(),
846 this->result
, src_reg(1)));
847 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
849 vec4_instruction
*inst
= emit(MOV(dst_null_d(), this->result
));
850 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
855 * Emit a gen6 IF statement with the comparison folded into the IF
859 vec4_visitor::emit_if_gen6(ir_if
*ir
)
861 ir_expression
*expr
= ir
->condition
->as_expression();
867 assert(expr
->get_num_operands() <= 2);
868 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
869 expr
->operands
[i
]->accept(this);
870 op
[i
] = this->result
;
873 switch (expr
->operation
) {
874 case ir_unop_logic_not
:
875 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_Z
));
878 case ir_binop_logic_xor
:
879 emit(IF(op
[0], op
[1], BRW_CONDITIONAL_NZ
));
882 case ir_binop_logic_or
:
883 temp
= dst_reg(this, glsl_type::bool_type
);
884 emit(OR(temp
, op
[0], op
[1]));
885 emit(IF(src_reg(temp
), src_reg(0), BRW_CONDITIONAL_NZ
));
888 case ir_binop_logic_and
:
889 temp
= dst_reg(this, glsl_type::bool_type
);
890 emit(AND(temp
, op
[0], op
[1]));
891 emit(IF(src_reg(temp
), src_reg(0), BRW_CONDITIONAL_NZ
));
895 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
899 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
902 case ir_binop_greater
:
903 case ir_binop_gequal
:
905 case ir_binop_lequal
:
907 case ir_binop_nequal
:
908 emit(IF(op
[0], op
[1],
909 brw_conditional_for_comparison(expr
->operation
)));
912 case ir_binop_all_equal
:
913 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
914 emit(IF(BRW_PREDICATE_ALIGN16_ALL4H
));
917 case ir_binop_any_nequal
:
918 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
919 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H
));
923 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
924 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H
));
928 assert(!"not reached");
929 emit(IF(op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
935 ir
->condition
->accept(this);
937 emit(IF(this->result
, src_reg(0), BRW_CONDITIONAL_NZ
));
941 vec4_visitor::visit(ir_variable
*ir
)
945 if (variable_storage(ir
))
948 switch (ir
->data
.mode
) {
949 case ir_var_shader_in
:
950 reg
= new(mem_ctx
) dst_reg(ATTR
, ir
->data
.location
);
953 case ir_var_shader_out
:
954 reg
= new(mem_ctx
) dst_reg(this, ir
->type
);
956 for (int i
= 0; i
< type_size(ir
->type
); i
++) {
957 output_reg
[ir
->data
.location
+ i
] = *reg
;
958 output_reg
[ir
->data
.location
+ i
].reg_offset
= i
;
959 output_reg
[ir
->data
.location
+ i
].type
=
960 brw_type_for_base_type(ir
->type
->get_scalar_type());
961 output_reg_annotation
[ir
->data
.location
+ i
] = ir
->name
;
966 case ir_var_temporary
:
967 reg
= new(mem_ctx
) dst_reg(this, ir
->type
);
971 reg
= new(this->mem_ctx
) dst_reg(UNIFORM
, this->uniforms
);
973 /* Thanks to the lower_ubo_reference pass, we will see only
974 * ir_binop_ubo_load expressions and not ir_dereference_variable for UBO
975 * variables, so no need for them to be in variable_ht.
977 * Atomic counters take no uniform storage, no need to do
980 if (ir
->is_in_uniform_block() || ir
->type
->contains_atomic())
983 /* Track how big the whole uniform variable is, in case we need to put a
984 * copy of its data into pull constants for array access.
986 assert(this->uniforms
< uniform_array_size
);
987 this->uniform_size
[this->uniforms
] = type_size(ir
->type
);
989 if (!strncmp(ir
->name
, "gl_", 3)) {
990 setup_builtin_uniform_values(ir
);
992 setup_uniform_values(ir
);
996 case ir_var_system_value
:
997 reg
= make_reg_for_system_value(ir
);
1001 assert(!"not reached");
1004 reg
->type
= brw_type_for_base_type(ir
->type
);
1005 hash_table_insert(this->variable_ht
, reg
, ir
);
1009 vec4_visitor::visit(ir_loop
*ir
)
1011 /* We don't want debugging output to print the whole body of the
1012 * loop as the annotation.
1014 this->base_ir
= NULL
;
1016 emit(BRW_OPCODE_DO
);
1018 visit_instructions(&ir
->body_instructions
);
1020 emit(BRW_OPCODE_WHILE
);
1024 vec4_visitor::visit(ir_loop_jump
*ir
)
1027 case ir_loop_jump::jump_break
:
1028 emit(BRW_OPCODE_BREAK
);
1030 case ir_loop_jump::jump_continue
:
1031 emit(BRW_OPCODE_CONTINUE
);
1038 vec4_visitor::visit(ir_function_signature
*ir
)
1045 vec4_visitor::visit(ir_function
*ir
)
1047 /* Ignore function bodies other than main() -- we shouldn't see calls to
1048 * them since they should all be inlined.
1050 if (strcmp(ir
->name
, "main") == 0) {
1051 const ir_function_signature
*sig
;
1054 sig
= ir
->matching_signature(NULL
, &empty
);
1058 visit_instructions(&sig
->body
);
1063 vec4_visitor::try_emit_sat(ir_expression
*ir
)
1065 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
1069 sat_src
->accept(this);
1070 src_reg src
= this->result
;
1072 this->result
= src_reg(this, ir
->type
);
1073 vec4_instruction
*inst
;
1074 inst
= emit(MOV(dst_reg(this->result
), src
));
1075 inst
->saturate
= true;
1081 vec4_visitor::try_emit_mad(ir_expression
*ir
, int mul_arg
)
1083 /* 3-src instructions were introduced in gen6. */
1087 /* MAD can only handle floating-point data. */
1088 if (ir
->type
->base_type
!= GLSL_TYPE_FLOAT
)
1091 ir_rvalue
*nonmul
= ir
->operands
[1 - mul_arg
];
1092 ir_expression
*mul
= ir
->operands
[mul_arg
]->as_expression();
1094 if (!mul
|| mul
->operation
!= ir_binop_mul
)
1097 nonmul
->accept(this);
1098 src_reg src0
= fix_3src_operand(this->result
);
1100 mul
->operands
[0]->accept(this);
1101 src_reg src1
= fix_3src_operand(this->result
);
1103 mul
->operands
[1]->accept(this);
1104 src_reg src2
= fix_3src_operand(this->result
);
1106 this->result
= src_reg(this, ir
->type
);
1107 emit(BRW_OPCODE_MAD
, dst_reg(this->result
), src0
, src1
, src2
);
1113 vec4_visitor::emit_bool_comparison(unsigned int op
,
1114 dst_reg dst
, src_reg src0
, src_reg src1
)
1116 /* original gen4 does destination conversion before comparison. */
1118 dst
.type
= src0
.type
;
1120 emit(CMP(dst
, src0
, src1
, brw_conditional_for_comparison(op
)));
1122 dst
.type
= BRW_REGISTER_TYPE_D
;
1123 emit(AND(dst
, src_reg(dst
), src_reg(0x1)));
1127 vec4_visitor::emit_minmax(uint32_t conditionalmod
, dst_reg dst
,
1128 src_reg src0
, src_reg src1
)
1130 vec4_instruction
*inst
;
1132 if (brw
->gen
>= 6) {
1133 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
1134 inst
->conditional_mod
= conditionalmod
;
1136 emit(CMP(dst
, src0
, src1
, conditionalmod
));
1138 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
1139 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1144 vec4_visitor::emit_lrp(const dst_reg
&dst
,
1145 const src_reg
&x
, const src_reg
&y
, const src_reg
&a
)
1147 if (brw
->gen
>= 6) {
1148 /* Note that the instruction's argument order is reversed from GLSL
1152 fix_3src_operand(a
), fix_3src_operand(y
), fix_3src_operand(x
)));
1154 /* Earlier generations don't support three source operations, so we
1155 * need to emit x*(1-a) + y*a.
1157 * A better way to do this would be:
1158 * ADD one_minus_a, negate(a), 1.0f
1160 * MAC dst, x, one_minus_a
1161 * but we would need to support MAC and implicit accumulator.
1163 dst_reg y_times_a
= dst_reg(this, glsl_type::vec4_type
);
1164 dst_reg one_minus_a
= dst_reg(this, glsl_type::vec4_type
);
1165 dst_reg x_times_one_minus_a
= dst_reg(this, glsl_type::vec4_type
);
1166 y_times_a
.writemask
= dst
.writemask
;
1167 one_minus_a
.writemask
= dst
.writemask
;
1168 x_times_one_minus_a
.writemask
= dst
.writemask
;
1170 emit(MUL(y_times_a
, y
, a
));
1171 emit(ADD(one_minus_a
, negate(a
), src_reg(1.0f
)));
1172 emit(MUL(x_times_one_minus_a
, x
, src_reg(one_minus_a
)));
1173 emit(ADD(dst
, src_reg(x_times_one_minus_a
), src_reg(y_times_a
)));
1178 vec4_visitor::visit(ir_expression
*ir
)
1180 unsigned int operand
;
1181 src_reg op
[Elements(ir
->operands
)];
1184 vec4_instruction
*inst
;
1186 if (try_emit_sat(ir
))
1189 if (ir
->operation
== ir_binop_add
) {
1190 if (try_emit_mad(ir
, 0) || try_emit_mad(ir
, 1))
1194 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1195 this->result
.file
= BAD_FILE
;
1196 ir
->operands
[operand
]->accept(this);
1197 if (this->result
.file
== BAD_FILE
) {
1198 fprintf(stderr
, "Failed to get tree for expression operand:\n");
1199 ir
->operands
[operand
]->fprint(stderr
);
1202 op
[operand
] = this->result
;
1204 /* Matrix expression operands should have been broken down to vector
1205 * operations already.
1207 assert(!ir
->operands
[operand
]->type
->is_matrix());
1210 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1211 if (ir
->operands
[1]) {
1212 vector_elements
= MAX2(vector_elements
,
1213 ir
->operands
[1]->type
->vector_elements
);
1216 this->result
.file
= BAD_FILE
;
1218 /* Storage for our result. Ideally for an assignment we'd be using
1219 * the actual storage for the result here, instead.
1221 result_src
= src_reg(this, ir
->type
);
1222 /* convenience for the emit functions below. */
1223 result_dst
= dst_reg(result_src
);
1224 /* If nothing special happens, this is the result. */
1225 this->result
= result_src
;
1226 /* Limit writes to the channels that will be used by result_src later.
1227 * This does limit this temp's use as a temporary for multi-instruction
1230 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1232 switch (ir
->operation
) {
1233 case ir_unop_logic_not
:
1234 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
1235 * ones complement of the whole register, not just bit 0.
1237 emit(XOR(result_dst
, op
[0], src_reg(1)));
1240 op
[0].negate
= !op
[0].negate
;
1241 emit(MOV(result_dst
, op
[0]));
1245 op
[0].negate
= false;
1246 emit(MOV(result_dst
, op
[0]));
1250 if (ir
->type
->is_float()) {
1251 /* AND(val, 0x80000000) gives the sign bit.
1253 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1256 emit(CMP(dst_null_f(), op
[0], src_reg(0.0f
), BRW_CONDITIONAL_NZ
));
1258 op
[0].type
= BRW_REGISTER_TYPE_UD
;
1259 result_dst
.type
= BRW_REGISTER_TYPE_UD
;
1260 emit(AND(result_dst
, op
[0], src_reg(0x80000000u
)));
1262 inst
= emit(OR(result_dst
, src_reg(result_dst
), src_reg(0x3f800000u
)));
1263 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1265 this->result
.type
= BRW_REGISTER_TYPE_F
;
1267 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
1268 * -> non-negative val generates 0x00000000.
1269 * Predicated OR sets 1 if val is positive.
1271 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_G
));
1273 emit(ASR(result_dst
, op
[0], src_reg(31)));
1275 inst
= emit(OR(result_dst
, src_reg(result_dst
), src_reg(1)));
1276 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1281 emit_math(SHADER_OPCODE_RCP
, result_dst
, op
[0]);
1285 emit_math(SHADER_OPCODE_EXP2
, result_dst
, op
[0]);
1288 emit_math(SHADER_OPCODE_LOG2
, result_dst
, op
[0]);
1292 assert(!"not reached: should be handled by ir_explog_to_explog2");
1295 case ir_unop_sin_reduced
:
1296 emit_math(SHADER_OPCODE_SIN
, result_dst
, op
[0]);
1299 case ir_unop_cos_reduced
:
1300 emit_math(SHADER_OPCODE_COS
, result_dst
, op
[0]);
1305 assert(!"derivatives not valid in vertex shader");
1308 case ir_unop_bitfield_reverse
:
1309 emit(BFREV(result_dst
, op
[0]));
1311 case ir_unop_bit_count
:
1312 emit(CBIT(result_dst
, op
[0]));
1314 case ir_unop_find_msb
: {
1315 src_reg temp
= src_reg(this, glsl_type::uint_type
);
1317 inst
= emit(FBH(dst_reg(temp
), op
[0]));
1318 inst
->dst
.writemask
= WRITEMASK_XYZW
;
1320 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1321 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1322 * subtract the result from 31 to convert the MSB count into an LSB count.
1325 /* FBH only supports UD type for dst, so use a MOV to convert UD to D. */
1326 temp
.swizzle
= BRW_SWIZZLE_NOOP
;
1327 emit(MOV(result_dst
, temp
));
1329 src_reg src_tmp
= src_reg(result_dst
);
1330 emit(CMP(dst_null_d(), src_tmp
, src_reg(-1), BRW_CONDITIONAL_NZ
));
1332 src_tmp
.negate
= true;
1333 inst
= emit(ADD(result_dst
, src_tmp
, src_reg(31)));
1334 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1337 case ir_unop_find_lsb
:
1338 emit(FBL(result_dst
, op
[0]));
1342 assert(!"not reached: should be handled by lower_noise");
1346 emit(ADD(result_dst
, op
[0], op
[1]));
1349 assert(!"not reached: should be handled by ir_sub_to_add_neg");
1353 if (brw
->gen
< 8 && ir
->type
->is_integer()) {
1354 /* For integer multiplication, the MUL uses the low 16 bits of one of
1355 * the operands (src0 through SNB, src1 on IVB and later). The MACH
1356 * accumulates in the contribution of the upper 16 bits of that
1357 * operand. If we can determine that one of the args is in the low
1358 * 16 bits, though, we can just emit a single MUL.
1360 if (ir
->operands
[0]->is_uint16_constant()) {
1362 emit(MUL(result_dst
, op
[0], op
[1]));
1364 emit(MUL(result_dst
, op
[1], op
[0]));
1365 } else if (ir
->operands
[1]->is_uint16_constant()) {
1367 emit(MUL(result_dst
, op
[1], op
[0]));
1369 emit(MUL(result_dst
, op
[0], op
[1]));
1371 struct brw_reg acc
= retype(brw_acc_reg(), result_dst
.type
);
1373 emit(MUL(acc
, op
[0], op
[1]));
1374 emit(MACH(dst_null_d(), op
[0], op
[1]));
1375 emit(MOV(result_dst
, src_reg(acc
)));
1378 emit(MUL(result_dst
, op
[0], op
[1]));
1381 case ir_binop_imul_high
: {
1382 struct brw_reg acc
= retype(brw_acc_reg(), result_dst
.type
);
1384 emit(MUL(acc
, op
[0], op
[1]));
1385 emit(MACH(result_dst
, op
[0], op
[1]));
1389 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
1390 assert(ir
->type
->is_integer());
1391 emit_math(SHADER_OPCODE_INT_QUOTIENT
, result_dst
, op
[0], op
[1]);
1393 case ir_binop_carry
: {
1394 struct brw_reg acc
= retype(brw_acc_reg(), BRW_REGISTER_TYPE_UD
);
1396 emit(ADDC(dst_null_ud(), op
[0], op
[1]));
1397 emit(MOV(result_dst
, src_reg(acc
)));
1400 case ir_binop_borrow
: {
1401 struct brw_reg acc
= retype(brw_acc_reg(), BRW_REGISTER_TYPE_UD
);
1403 emit(SUBB(dst_null_ud(), op
[0], op
[1]));
1404 emit(MOV(result_dst
, src_reg(acc
)));
1408 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
1409 assert(ir
->type
->is_integer());
1410 emit_math(SHADER_OPCODE_INT_REMAINDER
, result_dst
, op
[0], op
[1]);
1414 case ir_binop_greater
:
1415 case ir_binop_lequal
:
1416 case ir_binop_gequal
:
1417 case ir_binop_equal
:
1418 case ir_binop_nequal
: {
1419 emit(CMP(result_dst
, op
[0], op
[1],
1420 brw_conditional_for_comparison(ir
->operation
)));
1421 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1425 case ir_binop_all_equal
:
1426 /* "==" operator producing a scalar boolean. */
1427 if (ir
->operands
[0]->type
->is_vector() ||
1428 ir
->operands
[1]->type
->is_vector()) {
1429 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_Z
));
1430 emit(MOV(result_dst
, src_reg(0)));
1431 inst
= emit(MOV(result_dst
, src_reg(1)));
1432 inst
->predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
1434 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_Z
));
1435 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1438 case ir_binop_any_nequal
:
1439 /* "!=" operator producing a scalar boolean. */
1440 if (ir
->operands
[0]->type
->is_vector() ||
1441 ir
->operands
[1]->type
->is_vector()) {
1442 emit(CMP(dst_null_d(), op
[0], op
[1], BRW_CONDITIONAL_NZ
));
1444 emit(MOV(result_dst
, src_reg(0)));
1445 inst
= emit(MOV(result_dst
, src_reg(1)));
1446 inst
->predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
1448 emit(CMP(result_dst
, op
[0], op
[1], BRW_CONDITIONAL_NZ
));
1449 emit(AND(result_dst
, result_src
, src_reg(0x1)));
1454 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
1455 emit(MOV(result_dst
, src_reg(0)));
1457 inst
= emit(MOV(result_dst
, src_reg(1)));
1458 inst
->predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
1461 case ir_binop_logic_xor
:
1462 emit(XOR(result_dst
, op
[0], op
[1]));
1465 case ir_binop_logic_or
:
1466 emit(OR(result_dst
, op
[0], op
[1]));
1469 case ir_binop_logic_and
:
1470 emit(AND(result_dst
, op
[0], op
[1]));
1474 assert(ir
->operands
[0]->type
->is_vector());
1475 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1476 emit_dp(result_dst
, op
[0], op
[1], ir
->operands
[0]->type
->vector_elements
);
1480 emit_math(SHADER_OPCODE_SQRT
, result_dst
, op
[0]);
1483 emit_math(SHADER_OPCODE_RSQ
, result_dst
, op
[0]);
1486 case ir_unop_bitcast_i2f
:
1487 case ir_unop_bitcast_u2f
:
1488 this->result
= op
[0];
1489 this->result
.type
= BRW_REGISTER_TYPE_F
;
1492 case ir_unop_bitcast_f2i
:
1493 this->result
= op
[0];
1494 this->result
.type
= BRW_REGISTER_TYPE_D
;
1497 case ir_unop_bitcast_f2u
:
1498 this->result
= op
[0];
1499 this->result
.type
= BRW_REGISTER_TYPE_UD
;
1510 emit(MOV(result_dst
, op
[0]));
1514 emit(CMP(result_dst
, op
[0], src_reg(0.0f
), BRW_CONDITIONAL_NZ
));
1515 emit(AND(result_dst
, result_src
, src_reg(1)));
1520 emit(RNDZ(result_dst
, op
[0]));
1523 op
[0].negate
= !op
[0].negate
;
1524 inst
= emit(RNDD(result_dst
, op
[0]));
1525 this->result
.negate
= true;
1528 inst
= emit(RNDD(result_dst
, op
[0]));
1531 inst
= emit(FRC(result_dst
, op
[0]));
1533 case ir_unop_round_even
:
1534 emit(RNDE(result_dst
, op
[0]));
1538 emit_minmax(BRW_CONDITIONAL_L
, result_dst
, op
[0], op
[1]);
1541 emit_minmax(BRW_CONDITIONAL_G
, result_dst
, op
[0], op
[1]);
1545 emit_math(SHADER_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1548 case ir_unop_bit_not
:
1549 inst
= emit(NOT(result_dst
, op
[0]));
1551 case ir_binop_bit_and
:
1552 inst
= emit(AND(result_dst
, op
[0], op
[1]));
1554 case ir_binop_bit_xor
:
1555 inst
= emit(XOR(result_dst
, op
[0], op
[1]));
1557 case ir_binop_bit_or
:
1558 inst
= emit(OR(result_dst
, op
[0], op
[1]));
1561 case ir_binop_lshift
:
1562 inst
= emit(SHL(result_dst
, op
[0], op
[1]));
1565 case ir_binop_rshift
:
1566 if (ir
->type
->base_type
== GLSL_TYPE_INT
)
1567 inst
= emit(ASR(result_dst
, op
[0], op
[1]));
1569 inst
= emit(SHR(result_dst
, op
[0], op
[1]));
1573 emit(BFI1(result_dst
, op
[0], op
[1]));
1576 case ir_binop_ubo_load
: {
1577 ir_constant
*uniform_block
= ir
->operands
[0]->as_constant();
1578 ir_constant
*const_offset_ir
= ir
->operands
[1]->as_constant();
1579 unsigned const_offset
= const_offset_ir
? const_offset_ir
->value
.u
[0] : 0;
1582 /* Now, load the vector from that offset. */
1583 assert(ir
->type
->is_vector() || ir
->type
->is_scalar());
1585 src_reg packed_consts
= src_reg(this, glsl_type::vec4_type
);
1586 packed_consts
.type
= result
.type
;
1587 src_reg surf_index
=
1588 src_reg(prog_data
->base
.binding_table
.ubo_start
+ uniform_block
->value
.u
[0]);
1589 if (const_offset_ir
) {
1590 if (brw
->gen
>= 8) {
1591 /* Store the offset in a GRF so we can send-from-GRF. */
1592 offset
= src_reg(this, glsl_type::int_type
);
1593 emit(MOV(dst_reg(offset
), src_reg(const_offset
/ 16)));
1595 /* Immediates are fine on older generations since they'll be moved
1596 * to a (potentially fake) MRF at the generator level.
1598 offset
= src_reg(const_offset
/ 16);
1601 offset
= src_reg(this, glsl_type::uint_type
);
1602 emit(SHR(dst_reg(offset
), op
[1], src_reg(4)));
1605 if (brw
->gen
>= 7) {
1606 dst_reg grf_offset
= dst_reg(this, glsl_type::int_type
);
1607 grf_offset
.type
= offset
.type
;
1609 emit(MOV(grf_offset
, offset
));
1611 emit(new(mem_ctx
) vec4_instruction(this,
1612 VS_OPCODE_PULL_CONSTANT_LOAD_GEN7
,
1613 dst_reg(packed_consts
),
1615 src_reg(grf_offset
)));
1617 vec4_instruction
*pull
=
1618 emit(new(mem_ctx
) vec4_instruction(this,
1619 VS_OPCODE_PULL_CONSTANT_LOAD
,
1620 dst_reg(packed_consts
),
1623 pull
->base_mrf
= 14;
1627 packed_consts
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1628 packed_consts
.swizzle
+= BRW_SWIZZLE4(const_offset
% 16 / 4,
1629 const_offset
% 16 / 4,
1630 const_offset
% 16 / 4,
1631 const_offset
% 16 / 4);
1633 /* UBO bools are any nonzero int. We store bools as either 0 or 1. */
1634 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1635 emit(CMP(result_dst
, packed_consts
, src_reg(0u),
1636 BRW_CONDITIONAL_NZ
));
1637 emit(AND(result_dst
, result
, src_reg(0x1)));
1639 emit(MOV(result_dst
, packed_consts
));
1644 case ir_binop_vector_extract
:
1645 assert(!"should have been lowered by vec_index_to_cond_assign");
1649 op
[0] = fix_3src_operand(op
[0]);
1650 op
[1] = fix_3src_operand(op
[1]);
1651 op
[2] = fix_3src_operand(op
[2]);
1652 /* Note that the instruction's argument order is reversed from GLSL
1655 emit(MAD(result_dst
, op
[2], op
[1], op
[0]));
1659 emit_lrp(result_dst
, op
[0], op
[1], op
[2]);
1663 emit(CMP(dst_null_d(), op
[0], src_reg(0), BRW_CONDITIONAL_NZ
));
1664 inst
= emit(BRW_OPCODE_SEL
, result_dst
, op
[1], op
[2]);
1665 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1669 op
[0] = fix_3src_operand(op
[0]);
1670 op
[1] = fix_3src_operand(op
[1]);
1671 op
[2] = fix_3src_operand(op
[2]);
1672 emit(BFI2(result_dst
, op
[0], op
[1], op
[2]));
1675 case ir_triop_bitfield_extract
:
1676 op
[0] = fix_3src_operand(op
[0]);
1677 op
[1] = fix_3src_operand(op
[1]);
1678 op
[2] = fix_3src_operand(op
[2]);
1679 /* Note that the instruction's argument order is reversed from GLSL
1682 emit(BFE(result_dst
, op
[2], op
[1], op
[0]));
1685 case ir_triop_vector_insert
:
1686 assert(!"should have been lowered by lower_vector_insert");
1689 case ir_quadop_bitfield_insert
:
1690 assert(!"not reached: should be handled by "
1691 "bitfield_insert_to_bfm_bfi\n");
1694 case ir_quadop_vector
:
1695 assert(!"not reached: should be handled by lower_quadop_vector");
1698 case ir_unop_pack_half_2x16
:
1699 emit_pack_half_2x16(result_dst
, op
[0]);
1701 case ir_unop_unpack_half_2x16
:
1702 emit_unpack_half_2x16(result_dst
, op
[0]);
1704 case ir_unop_pack_snorm_2x16
:
1705 case ir_unop_pack_snorm_4x8
:
1706 case ir_unop_pack_unorm_2x16
:
1707 case ir_unop_pack_unorm_4x8
:
1708 case ir_unop_unpack_snorm_2x16
:
1709 case ir_unop_unpack_snorm_4x8
:
1710 case ir_unop_unpack_unorm_2x16
:
1711 case ir_unop_unpack_unorm_4x8
:
1712 assert(!"not reached: should be handled by lower_packing_builtins");
1714 case ir_unop_unpack_half_2x16_split_x
:
1715 case ir_unop_unpack_half_2x16_split_y
:
1716 case ir_binop_pack_half_2x16_split
:
1717 assert(!"not reached: should not occur in vertex shader");
1719 case ir_binop_ldexp
:
1720 assert(!"not reached: should be handled by ldexp_to_arith()");
1727 vec4_visitor::visit(ir_swizzle
*ir
)
1733 /* Note that this is only swizzles in expressions, not those on the left
1734 * hand side of an assignment, which do write masking. See ir_assignment
1738 ir
->val
->accept(this);
1740 assert(src
.file
!= BAD_FILE
);
1742 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1745 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1748 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1751 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1754 swizzle
[i
] = BRW_GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1758 for (; i
< 4; i
++) {
1759 /* Replicate the last channel out. */
1760 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1763 src
.swizzle
= BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1769 vec4_visitor::visit(ir_dereference_variable
*ir
)
1771 const struct glsl_type
*type
= ir
->type
;
1772 dst_reg
*reg
= variable_storage(ir
->var
);
1775 fail("Failed to find variable storage for %s\n", ir
->var
->name
);
1776 this->result
= src_reg(brw_null_reg());
1780 this->result
= src_reg(*reg
);
1782 /* System values get their swizzle from the dst_reg writemask */
1783 if (ir
->var
->data
.mode
== ir_var_system_value
)
1786 if (type
->is_scalar() || type
->is_vector() || type
->is_matrix())
1787 this->result
.swizzle
= swizzle_for_size(type
->vector_elements
);
1792 vec4_visitor::compute_array_stride(ir_dereference_array
*ir
)
1794 /* Under normal circumstances array elements are stored consecutively, so
1795 * the stride is equal to the size of the array element.
1797 return type_size(ir
->type
);
1802 vec4_visitor::visit(ir_dereference_array
*ir
)
1804 ir_constant
*constant_index
;
1806 int array_stride
= compute_array_stride(ir
);
1808 constant_index
= ir
->array_index
->constant_expression_value();
1810 ir
->array
->accept(this);
1813 if (constant_index
) {
1814 src
.reg_offset
+= constant_index
->value
.i
[0] * array_stride
;
1816 /* Variable index array dereference. It eats the "vec4" of the
1817 * base of the array and an index that offsets the Mesa register
1820 ir
->array_index
->accept(this);
1824 if (array_stride
== 1) {
1825 index_reg
= this->result
;
1827 index_reg
= src_reg(this, glsl_type::int_type
);
1829 emit(MUL(dst_reg(index_reg
), this->result
, src_reg(array_stride
)));
1833 src_reg temp
= src_reg(this, glsl_type::int_type
);
1835 emit(ADD(dst_reg(temp
), *src
.reladdr
, index_reg
));
1840 src
.reladdr
= ralloc(mem_ctx
, src_reg
);
1841 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1844 /* If the type is smaller than a vec4, replicate the last channel out. */
1845 if (ir
->type
->is_scalar() || ir
->type
->is_vector() || ir
->type
->is_matrix())
1846 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1848 src
.swizzle
= BRW_SWIZZLE_NOOP
;
1849 src
.type
= brw_type_for_base_type(ir
->type
);
1855 vec4_visitor::visit(ir_dereference_record
*ir
)
1858 const glsl_type
*struct_type
= ir
->record
->type
;
1861 ir
->record
->accept(this);
1863 for (i
= 0; i
< struct_type
->length
; i
++) {
1864 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1866 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1869 /* If the type is smaller than a vec4, replicate the last channel out. */
1870 if (ir
->type
->is_scalar() || ir
->type
->is_vector() || ir
->type
->is_matrix())
1871 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1873 this->result
.swizzle
= BRW_SWIZZLE_NOOP
;
1874 this->result
.type
= brw_type_for_base_type(ir
->type
);
1876 this->result
.reg_offset
+= offset
;
1880 * We want to be careful in assignment setup to hit the actual storage
1881 * instead of potentially using a temporary like we might with the
1882 * ir_dereference handler.
1885 get_assignment_lhs(ir_dereference
*ir
, vec4_visitor
*v
)
1887 /* The LHS must be a dereference. If the LHS is a variable indexed array
1888 * access of a vector, it must be separated into a series conditional moves
1889 * before reaching this point (see ir_vec_index_to_cond_assign).
1891 assert(ir
->as_dereference());
1892 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1894 assert(!deref_array
->array
->type
->is_vector());
1897 /* Use the rvalue deref handler for the most part. We'll ignore
1898 * swizzles in it and write swizzles using writemask, though.
1901 return dst_reg(v
->result
);
1905 vec4_visitor::emit_block_move(dst_reg
*dst
, src_reg
*src
,
1906 const struct glsl_type
*type
, uint32_t predicate
)
1908 if (type
->base_type
== GLSL_TYPE_STRUCT
) {
1909 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1910 emit_block_move(dst
, src
, type
->fields
.structure
[i
].type
, predicate
);
1915 if (type
->is_array()) {
1916 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1917 emit_block_move(dst
, src
, type
->fields
.array
, predicate
);
1922 if (type
->is_matrix()) {
1923 const struct glsl_type
*vec_type
;
1925 vec_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
1926 type
->vector_elements
, 1);
1928 for (int i
= 0; i
< type
->matrix_columns
; i
++) {
1929 emit_block_move(dst
, src
, vec_type
, predicate
);
1934 assert(type
->is_scalar() || type
->is_vector());
1936 dst
->type
= brw_type_for_base_type(type
);
1937 src
->type
= dst
->type
;
1939 dst
->writemask
= (1 << type
->vector_elements
) - 1;
1941 src
->swizzle
= swizzle_for_size(type
->vector_elements
);
1943 vec4_instruction
*inst
= emit(MOV(*dst
, *src
));
1944 inst
->predicate
= predicate
;
1951 /* If the RHS processing resulted in an instruction generating a
1952 * temporary value, and it would be easy to rewrite the instruction to
1953 * generate its result right into the LHS instead, do so. This ends
1954 * up reliably removing instructions where it can be tricky to do so
1955 * later without real UD chain information.
1958 vec4_visitor::try_rewrite_rhs_to_dst(ir_assignment
*ir
,
1961 vec4_instruction
*pre_rhs_inst
,
1962 vec4_instruction
*last_rhs_inst
)
1964 /* This could be supported, but it would take more smarts. */
1968 if (pre_rhs_inst
== last_rhs_inst
)
1969 return false; /* No instructions generated to work with. */
1971 /* Make sure the last instruction generated our source reg. */
1972 if (src
.file
!= GRF
||
1973 src
.file
!= last_rhs_inst
->dst
.file
||
1974 src
.reg
!= last_rhs_inst
->dst
.reg
||
1975 src
.reg_offset
!= last_rhs_inst
->dst
.reg_offset
||
1979 last_rhs_inst
->predicate
!= BRW_PREDICATE_NONE
)
1982 /* Check that that last instruction fully initialized the channels
1983 * we want to use, in the order we want to use them. We could
1984 * potentially reswizzle the operands of many instructions so that
1985 * we could handle out of order channels, but don't yet.
1988 for (unsigned i
= 0; i
< 4; i
++) {
1989 if (dst
.writemask
& (1 << i
)) {
1990 if (!(last_rhs_inst
->dst
.writemask
& (1 << i
)))
1993 if (BRW_GET_SWZ(src
.swizzle
, i
) != i
)
1998 /* Success! Rewrite the instruction. */
1999 last_rhs_inst
->dst
.file
= dst
.file
;
2000 last_rhs_inst
->dst
.reg
= dst
.reg
;
2001 last_rhs_inst
->dst
.reg_offset
= dst
.reg_offset
;
2002 last_rhs_inst
->dst
.reladdr
= dst
.reladdr
;
2003 last_rhs_inst
->dst
.writemask
&= dst
.writemask
;
2009 vec4_visitor::visit(ir_assignment
*ir
)
2011 dst_reg dst
= get_assignment_lhs(ir
->lhs
, this);
2012 uint32_t predicate
= BRW_PREDICATE_NONE
;
2014 if (!ir
->lhs
->type
->is_scalar() &&
2015 !ir
->lhs
->type
->is_vector()) {
2016 ir
->rhs
->accept(this);
2017 src_reg src
= this->result
;
2019 if (ir
->condition
) {
2020 emit_bool_to_cond_code(ir
->condition
, &predicate
);
2023 /* emit_block_move doesn't account for swizzles in the source register.
2024 * This should be ok, since the source register is a structure or an
2025 * array, and those can't be swizzled. But double-check to be sure.
2027 assert(src
.swizzle
==
2028 (ir
->rhs
->type
->is_matrix()
2029 ? swizzle_for_size(ir
->rhs
->type
->vector_elements
)
2030 : BRW_SWIZZLE_NOOP
));
2032 emit_block_move(&dst
, &src
, ir
->rhs
->type
, predicate
);
2036 /* Now we're down to just a scalar/vector with writemasks. */
2039 vec4_instruction
*pre_rhs_inst
, *last_rhs_inst
;
2040 pre_rhs_inst
= (vec4_instruction
*)this->instructions
.get_tail();
2042 ir
->rhs
->accept(this);
2044 last_rhs_inst
= (vec4_instruction
*)this->instructions
.get_tail();
2046 src_reg src
= this->result
;
2049 int first_enabled_chan
= 0;
2052 assert(ir
->lhs
->type
->is_vector() ||
2053 ir
->lhs
->type
->is_scalar());
2054 dst
.writemask
= ir
->write_mask
;
2056 for (int i
= 0; i
< 4; i
++) {
2057 if (dst
.writemask
& (1 << i
)) {
2058 first_enabled_chan
= BRW_GET_SWZ(src
.swizzle
, i
);
2063 /* Swizzle a small RHS vector into the channels being written.
2065 * glsl ir treats write_mask as dictating how many channels are
2066 * present on the RHS while in our instructions we need to make
2067 * those channels appear in the slots of the vec4 they're written to.
2069 for (int i
= 0; i
< 4; i
++) {
2070 if (dst
.writemask
& (1 << i
))
2071 swizzles
[i
] = BRW_GET_SWZ(src
.swizzle
, src_chan
++);
2073 swizzles
[i
] = first_enabled_chan
;
2075 src
.swizzle
= BRW_SWIZZLE4(swizzles
[0], swizzles
[1],
2076 swizzles
[2], swizzles
[3]);
2078 if (try_rewrite_rhs_to_dst(ir
, dst
, src
, pre_rhs_inst
, last_rhs_inst
)) {
2082 if (ir
->condition
) {
2083 emit_bool_to_cond_code(ir
->condition
, &predicate
);
2086 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
2087 vec4_instruction
*inst
= emit(MOV(dst
, src
));
2088 inst
->predicate
= predicate
;
2096 vec4_visitor::emit_constant_values(dst_reg
*dst
, ir_constant
*ir
)
2098 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
2099 foreach_list(node
, &ir
->components
) {
2100 ir_constant
*field_value
= (ir_constant
*)node
;
2102 emit_constant_values(dst
, field_value
);
2107 if (ir
->type
->is_array()) {
2108 for (unsigned int i
= 0; i
< ir
->type
->length
; i
++) {
2109 emit_constant_values(dst
, ir
->array_elements
[i
]);
2114 if (ir
->type
->is_matrix()) {
2115 for (int i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
2116 float *vec
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
2118 for (int j
= 0; j
< ir
->type
->vector_elements
; j
++) {
2119 dst
->writemask
= 1 << j
;
2120 dst
->type
= BRW_REGISTER_TYPE_F
;
2122 emit(MOV(*dst
, src_reg(vec
[j
])));
2129 int remaining_writemask
= (1 << ir
->type
->vector_elements
) - 1;
2131 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2132 if (!(remaining_writemask
& (1 << i
)))
2135 dst
->writemask
= 1 << i
;
2136 dst
->type
= brw_type_for_base_type(ir
->type
);
2138 /* Find other components that match the one we're about to
2139 * write. Emits fewer instructions for things like vec4(0.5,
2142 for (int j
= i
+ 1; j
< ir
->type
->vector_elements
; j
++) {
2143 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
2144 if (ir
->value
.b
[i
] == ir
->value
.b
[j
])
2145 dst
->writemask
|= (1 << j
);
2147 /* u, i, and f storage all line up, so no need for a
2148 * switch case for comparing each type.
2150 if (ir
->value
.u
[i
] == ir
->value
.u
[j
])
2151 dst
->writemask
|= (1 << j
);
2155 switch (ir
->type
->base_type
) {
2156 case GLSL_TYPE_FLOAT
:
2157 emit(MOV(*dst
, src_reg(ir
->value
.f
[i
])));
2160 emit(MOV(*dst
, src_reg(ir
->value
.i
[i
])));
2162 case GLSL_TYPE_UINT
:
2163 emit(MOV(*dst
, src_reg(ir
->value
.u
[i
])));
2165 case GLSL_TYPE_BOOL
:
2166 emit(MOV(*dst
, src_reg(ir
->value
.b
[i
])));
2169 assert(!"Non-float/uint/int/bool constant");
2173 remaining_writemask
&= ~dst
->writemask
;
2179 vec4_visitor::visit(ir_constant
*ir
)
2181 dst_reg dst
= dst_reg(this, ir
->type
);
2182 this->result
= src_reg(dst
);
2184 emit_constant_values(&dst
, ir
);
2188 vec4_visitor::visit_atomic_counter_intrinsic(ir_call
*ir
)
2190 ir_dereference
*deref
= static_cast<ir_dereference
*>(
2191 ir
->actual_parameters
.get_head());
2192 ir_variable
*location
= deref
->variable_referenced();
2193 unsigned surf_index
= (prog_data
->base
.binding_table
.abo_start
+
2194 location
->data
.atomic
.buffer_index
);
2196 /* Calculate the surface offset */
2197 src_reg
offset(this, glsl_type::uint_type
);
2198 ir_dereference_array
*deref_array
= deref
->as_dereference_array();
2200 deref_array
->array_index
->accept(this);
2202 src_reg
tmp(this, glsl_type::uint_type
);
2203 emit(MUL(dst_reg(tmp
), this->result
, ATOMIC_COUNTER_SIZE
));
2204 emit(ADD(dst_reg(offset
), tmp
, location
->data
.atomic
.offset
));
2206 offset
= location
->data
.atomic
.offset
;
2209 /* Emit the appropriate machine instruction */
2210 const char *callee
= ir
->callee
->function_name();
2211 dst_reg dst
= get_assignment_lhs(ir
->return_deref
, this);
2213 if (!strcmp("__intrinsic_atomic_read", callee
)) {
2214 emit_untyped_surface_read(surf_index
, dst
, offset
);
2216 } else if (!strcmp("__intrinsic_atomic_increment", callee
)) {
2217 emit_untyped_atomic(BRW_AOP_INC
, surf_index
, dst
, offset
,
2218 src_reg(), src_reg());
2220 } else if (!strcmp("__intrinsic_atomic_predecrement", callee
)) {
2221 emit_untyped_atomic(BRW_AOP_PREDEC
, surf_index
, dst
, offset
,
2222 src_reg(), src_reg());
2227 vec4_visitor::visit(ir_call
*ir
)
2229 const char *callee
= ir
->callee
->function_name();
2231 if (!strcmp("__intrinsic_atomic_read", callee
) ||
2232 !strcmp("__intrinsic_atomic_increment", callee
) ||
2233 !strcmp("__intrinsic_atomic_predecrement", callee
)) {
2234 visit_atomic_counter_intrinsic(ir
);
2236 assert(!"Unsupported intrinsic.");
2241 vec4_visitor::emit_mcs_fetch(ir_texture
*ir
, src_reg coordinate
, int sampler
)
2243 vec4_instruction
*inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXF_MCS
);
2246 inst
->sampler
= sampler
;
2247 inst
->dst
= dst_reg(this, glsl_type::uvec4_type
);
2248 inst
->dst
.writemask
= WRITEMASK_XYZW
;
2250 /* parameters are: u, v, r, lod; lod will always be zero due to api restrictions */
2251 int param_base
= inst
->base_mrf
;
2252 int coord_mask
= (1 << ir
->coordinate
->type
->vector_elements
) - 1;
2253 int zero_mask
= 0xf & ~coord_mask
;
2255 emit(MOV(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, coord_mask
),
2258 emit(MOV(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, zero_mask
),
2262 return src_reg(inst
->dst
);
2266 vec4_visitor::visit(ir_texture
*ir
)
2269 _mesa_get_sampler_uniform_value(ir
->sampler
, shader_prog
, prog
);
2271 /* When tg4 is used with the degenerate ZERO/ONE swizzles, don't bother
2272 * emitting anything other than setting up the constant result.
2274 if (ir
->op
== ir_tg4
) {
2275 ir_constant
*chan
= ir
->lod_info
.component
->as_constant();
2276 int swiz
= GET_SWZ(key
->tex
.swizzles
[sampler
], chan
->value
.i
[0]);
2277 if (swiz
== SWIZZLE_ZERO
|| swiz
== SWIZZLE_ONE
) {
2278 dst_reg
result(this, ir
->type
);
2279 this->result
= src_reg(result
);
2280 emit(MOV(result
, src_reg(swiz
== SWIZZLE_ONE
? 1.0f
: 0.0f
)));
2285 /* Should be lowered by do_lower_texture_projection */
2286 assert(!ir
->projector
);
2288 /* Should be lowered */
2289 assert(!ir
->offset
|| !ir
->offset
->type
->is_array());
2291 /* Generate code to compute all the subexpression trees. This has to be
2292 * done before loading any values into MRFs for the sampler message since
2293 * generating these values may involve SEND messages that need the MRFs.
2296 if (ir
->coordinate
) {
2297 ir
->coordinate
->accept(this);
2298 coordinate
= this->result
;
2301 src_reg shadow_comparitor
;
2302 if (ir
->shadow_comparitor
) {
2303 ir
->shadow_comparitor
->accept(this);
2304 shadow_comparitor
= this->result
;
2307 bool has_nonconstant_offset
= ir
->offset
&& !ir
->offset
->as_constant();
2308 src_reg offset_value
;
2309 if (has_nonconstant_offset
) {
2310 ir
->offset
->accept(this);
2311 offset_value
= src_reg(this->result
);
2314 const glsl_type
*lod_type
= NULL
, *sample_index_type
= NULL
;
2315 src_reg lod
, dPdx
, dPdy
, sample_index
, mcs
;
2318 lod
= src_reg(0.0f
);
2319 lod_type
= glsl_type::float_type
;
2324 ir
->lod_info
.lod
->accept(this);
2326 lod_type
= ir
->lod_info
.lod
->type
;
2328 case ir_query_levels
:
2330 lod_type
= glsl_type::int_type
;
2333 ir
->lod_info
.sample_index
->accept(this);
2334 sample_index
= this->result
;
2335 sample_index_type
= ir
->lod_info
.sample_index
->type
;
2337 if (brw
->gen
>= 7 && key
->tex
.compressed_multisample_layout_mask
& (1<<sampler
))
2338 mcs
= emit_mcs_fetch(ir
, coordinate
, sampler
);
2343 ir
->lod_info
.grad
.dPdx
->accept(this);
2344 dPdx
= this->result
;
2346 ir
->lod_info
.grad
.dPdy
->accept(this);
2347 dPdy
= this->result
;
2349 lod_type
= ir
->lod_info
.grad
.dPdx
->type
;
2357 vec4_instruction
*inst
= NULL
;
2361 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXL
);
2364 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXD
);
2367 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXF
);
2370 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXF_CMS
);
2373 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXS
);
2376 if (has_nonconstant_offset
)
2377 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TG4_OFFSET
);
2379 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TG4
);
2381 case ir_query_levels
:
2382 inst
= new(mem_ctx
) vec4_instruction(this, SHADER_OPCODE_TXS
);
2385 assert(!"TXB is not valid for vertex shaders.");
2388 assert(!"LOD is not valid for vertex shaders.");
2391 assert(!"Unrecognized tex op");
2394 if (ir
->offset
!= NULL
&& ir
->op
!= ir_txf
)
2395 inst
->texture_offset
= brw_texture_offset(ctx
, ir
->offset
->as_constant());
2397 /* Stuff the channel select bits in the top of the texture offset */
2398 if (ir
->op
== ir_tg4
)
2399 inst
->texture_offset
|= gather_channel(ir
, sampler
) << 16;
2401 /* The message header is necessary for:
2404 * - Gather channel selection
2405 * - Sampler indices too large to fit in a 4-bit value.
2407 inst
->header_present
=
2408 brw
->gen
< 5 || inst
->texture_offset
!= 0 || ir
->op
== ir_tg4
||
2411 inst
->mlen
= inst
->header_present
+ 1; /* always at least one */
2412 inst
->sampler
= sampler
;
2413 inst
->dst
= dst_reg(this, ir
->type
);
2414 inst
->dst
.writemask
= WRITEMASK_XYZW
;
2415 inst
->shadow_compare
= ir
->shadow_comparitor
!= NULL
;
2417 /* MRF for the first parameter */
2418 int param_base
= inst
->base_mrf
+ inst
->header_present
;
2420 if (ir
->op
== ir_txs
|| ir
->op
== ir_query_levels
) {
2421 int writemask
= brw
->gen
== 4 ? WRITEMASK_W
: WRITEMASK_X
;
2422 emit(MOV(dst_reg(MRF
, param_base
, lod_type
, writemask
), lod
));
2424 /* Load the coordinate */
2425 /* FINISHME: gl_clamp_mask and saturate */
2426 int coord_mask
= (1 << ir
->coordinate
->type
->vector_elements
) - 1;
2427 int zero_mask
= 0xf & ~coord_mask
;
2429 emit(MOV(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, coord_mask
),
2432 if (zero_mask
!= 0) {
2433 emit(MOV(dst_reg(MRF
, param_base
, ir
->coordinate
->type
, zero_mask
),
2436 /* Load the shadow comparitor */
2437 if (ir
->shadow_comparitor
&& ir
->op
!= ir_txd
&& (ir
->op
!= ir_tg4
|| !has_nonconstant_offset
)) {
2438 emit(MOV(dst_reg(MRF
, param_base
+ 1, ir
->shadow_comparitor
->type
,
2440 shadow_comparitor
));
2444 /* Load the LOD info */
2445 if (ir
->op
== ir_tex
|| ir
->op
== ir_txl
) {
2447 if (brw
->gen
>= 5) {
2448 mrf
= param_base
+ 1;
2449 if (ir
->shadow_comparitor
) {
2450 writemask
= WRITEMASK_Y
;
2451 /* mlen already incremented */
2453 writemask
= WRITEMASK_X
;
2456 } else /* brw->gen == 4 */ {
2458 writemask
= WRITEMASK_W
;
2460 emit(MOV(dst_reg(MRF
, mrf
, lod_type
, writemask
), lod
));
2461 } else if (ir
->op
== ir_txf
) {
2462 emit(MOV(dst_reg(MRF
, param_base
, lod_type
, WRITEMASK_W
), lod
));
2463 } else if (ir
->op
== ir_txf_ms
) {
2464 emit(MOV(dst_reg(MRF
, param_base
+ 1, sample_index_type
, WRITEMASK_X
),
2467 /* MCS data is in the first channel of `mcs`, but we need to get it into
2468 * the .y channel of the second vec4 of params, so replicate .x across
2469 * the whole vec4 and then mask off everything except .y
2471 mcs
.swizzle
= BRW_SWIZZLE_XXXX
;
2472 emit(MOV(dst_reg(MRF
, param_base
+ 1, glsl_type::uint_type
, WRITEMASK_Y
),
2475 } else if (ir
->op
== ir_txd
) {
2476 const glsl_type
*type
= lod_type
;
2478 if (brw
->gen
>= 5) {
2479 dPdx
.swizzle
= BRW_SWIZZLE4(SWIZZLE_X
,SWIZZLE_X
,SWIZZLE_Y
,SWIZZLE_Y
);
2480 dPdy
.swizzle
= BRW_SWIZZLE4(SWIZZLE_X
,SWIZZLE_X
,SWIZZLE_Y
,SWIZZLE_Y
);
2481 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_XZ
), dPdx
));
2482 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_YW
), dPdy
));
2485 if (ir
->type
->vector_elements
== 3 || ir
->shadow_comparitor
) {
2486 dPdx
.swizzle
= BRW_SWIZZLE_ZZZZ
;
2487 dPdy
.swizzle
= BRW_SWIZZLE_ZZZZ
;
2488 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_X
), dPdx
));
2489 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_Y
), dPdy
));
2492 if (ir
->shadow_comparitor
) {
2493 emit(MOV(dst_reg(MRF
, param_base
+ 2,
2494 ir
->shadow_comparitor
->type
, WRITEMASK_Z
),
2495 shadow_comparitor
));
2498 } else /* brw->gen == 4 */ {
2499 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_XYZ
), dPdx
));
2500 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_XYZ
), dPdy
));
2503 } else if (ir
->op
== ir_tg4
&& has_nonconstant_offset
) {
2504 if (ir
->shadow_comparitor
) {
2505 emit(MOV(dst_reg(MRF
, param_base
, ir
->shadow_comparitor
->type
, WRITEMASK_W
),
2506 shadow_comparitor
));
2509 emit(MOV(dst_reg(MRF
, param_base
+ 1, glsl_type::ivec2_type
, WRITEMASK_XY
),
2517 /* fixup num layers (z) for cube arrays: hardware returns faces * layers;
2518 * spec requires layers.
2520 if (ir
->op
== ir_txs
) {
2521 glsl_type
const *type
= ir
->sampler
->type
;
2522 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
&&
2523 type
->sampler_array
) {
2524 emit_math(SHADER_OPCODE_INT_QUOTIENT
,
2525 writemask(inst
->dst
, WRITEMASK_Z
),
2526 src_reg(inst
->dst
), src_reg(6));
2530 if (brw
->gen
== 6 && ir
->op
== ir_tg4
) {
2531 emit_gen6_gather_wa(key
->tex
.gen6_gather_wa
[sampler
], inst
->dst
);
2534 swizzle_result(ir
, src_reg(inst
->dst
), sampler
);
2538 * Apply workarounds for Gen6 gather with UINT/SINT
2541 vec4_visitor::emit_gen6_gather_wa(uint8_t wa
, dst_reg dst
)
2546 int width
= (wa
& WA_8BIT
) ? 8 : 16;
2547 dst_reg dst_f
= dst
;
2548 dst_f
.type
= BRW_REGISTER_TYPE_F
;
2550 /* Convert from UNORM to UINT */
2551 emit(MUL(dst_f
, src_reg(dst_f
), src_reg((float)((1 << width
) - 1))));
2552 emit(MOV(dst
, src_reg(dst_f
)));
2555 /* Reinterpret the UINT value as a signed INT value by
2556 * shifting the sign bit into place, then shifting back
2559 emit(SHL(dst
, src_reg(dst
), src_reg(32 - width
)));
2560 emit(ASR(dst
, src_reg(dst
), src_reg(32 - width
)));
2565 * Set up the gather channel based on the swizzle, for gather4.
2568 vec4_visitor::gather_channel(ir_texture
*ir
, int sampler
)
2570 ir_constant
*chan
= ir
->lod_info
.component
->as_constant();
2571 int swiz
= GET_SWZ(key
->tex
.swizzles
[sampler
], chan
->value
.i
[0]);
2573 case SWIZZLE_X
: return 0;
2575 /* gather4 sampler is broken for green channel on RG32F --
2576 * we must ask for blue instead.
2578 if (key
->tex
.gather_channel_quirk_mask
& (1<<sampler
))
2581 case SWIZZLE_Z
: return 2;
2582 case SWIZZLE_W
: return 3;
2584 assert(!"Not reached"); /* zero, one swizzles handled already */
2590 vec4_visitor::swizzle_result(ir_texture
*ir
, src_reg orig_val
, int sampler
)
2592 int s
= key
->tex
.swizzles
[sampler
];
2594 this->result
= src_reg(this, ir
->type
);
2595 dst_reg
swizzled_result(this->result
);
2597 if (ir
->op
== ir_query_levels
) {
2598 /* # levels is in .w */
2599 orig_val
.swizzle
= BRW_SWIZZLE4(SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
);
2600 emit(MOV(swizzled_result
, orig_val
));
2604 if (ir
->op
== ir_txs
|| ir
->type
== glsl_type::float_type
2605 || s
== SWIZZLE_NOOP
|| ir
->op
== ir_tg4
) {
2606 emit(MOV(swizzled_result
, orig_val
));
2611 int zero_mask
= 0, one_mask
= 0, copy_mask
= 0;
2612 int swizzle
[4] = {0};
2614 for (int i
= 0; i
< 4; i
++) {
2615 switch (GET_SWZ(s
, i
)) {
2617 zero_mask
|= (1 << i
);
2620 one_mask
|= (1 << i
);
2623 copy_mask
|= (1 << i
);
2624 swizzle
[i
] = GET_SWZ(s
, i
);
2630 orig_val
.swizzle
= BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2631 swizzled_result
.writemask
= copy_mask
;
2632 emit(MOV(swizzled_result
, orig_val
));
2636 swizzled_result
.writemask
= zero_mask
;
2637 emit(MOV(swizzled_result
, src_reg(0.0f
)));
2641 swizzled_result
.writemask
= one_mask
;
2642 emit(MOV(swizzled_result
, src_reg(1.0f
)));
2647 vec4_visitor::visit(ir_return
*ir
)
2649 assert(!"not reached");
2653 vec4_visitor::visit(ir_discard
*ir
)
2655 assert(!"not reached");
2659 vec4_visitor::visit(ir_if
*ir
)
2661 /* Don't point the annotation at the if statement, because then it plus
2662 * the then and else blocks get printed.
2664 this->base_ir
= ir
->condition
;
2666 if (brw
->gen
== 6) {
2670 emit_bool_to_cond_code(ir
->condition
, &predicate
);
2671 emit(IF(predicate
));
2674 visit_instructions(&ir
->then_instructions
);
2676 if (!ir
->else_instructions
.is_empty()) {
2677 this->base_ir
= ir
->condition
;
2678 emit(BRW_OPCODE_ELSE
);
2680 visit_instructions(&ir
->else_instructions
);
2683 this->base_ir
= ir
->condition
;
2684 emit(BRW_OPCODE_ENDIF
);
2688 vec4_visitor::visit(ir_emit_vertex
*)
2690 assert(!"not reached");
2694 vec4_visitor::visit(ir_end_primitive
*)
2696 assert(!"not reached");
2700 vec4_visitor::emit_untyped_atomic(unsigned atomic_op
, unsigned surf_index
,
2701 dst_reg dst
, src_reg offset
,
2702 src_reg src0
, src_reg src1
)
2706 /* Set the atomic operation offset. */
2707 emit(MOV(brw_writemask(brw_uvec_mrf(8, mlen
, 0), WRITEMASK_X
), offset
));
2710 /* Set the atomic operation arguments. */
2711 if (src0
.file
!= BAD_FILE
) {
2712 emit(MOV(brw_writemask(brw_uvec_mrf(8, mlen
, 0), WRITEMASK_X
), src0
));
2716 if (src1
.file
!= BAD_FILE
) {
2717 emit(MOV(brw_writemask(brw_uvec_mrf(8, mlen
, 0), WRITEMASK_X
), src1
));
2721 /* Emit the instruction. Note that this maps to the normal SIMD8
2722 * untyped atomic message on Ivy Bridge, but that's OK because
2723 * unused channels will be masked out.
2725 vec4_instruction
*inst
= emit(SHADER_OPCODE_UNTYPED_ATOMIC
, dst
,
2726 src_reg(atomic_op
), src_reg(surf_index
));
2732 vec4_visitor::emit_untyped_surface_read(unsigned surf_index
, dst_reg dst
,
2735 /* Set the surface read offset. */
2736 emit(MOV(brw_writemask(brw_uvec_mrf(8, 0, 0), WRITEMASK_X
), offset
));
2738 /* Emit the instruction. Note that this maps to the normal SIMD8
2739 * untyped surface read message, but that's OK because unused
2740 * channels will be masked out.
2742 vec4_instruction
*inst
= emit(SHADER_OPCODE_UNTYPED_SURFACE_READ
,
2743 dst
, src_reg(surf_index
));
2749 vec4_visitor::emit_ndc_computation()
2751 /* Get the position */
2752 src_reg pos
= src_reg(output_reg
[VARYING_SLOT_POS
]);
2754 /* Build ndc coords, which are (x/w, y/w, z/w, 1/w) */
2755 dst_reg ndc
= dst_reg(this, glsl_type::vec4_type
);
2756 output_reg
[BRW_VARYING_SLOT_NDC
] = ndc
;
2758 current_annotation
= "NDC";
2759 dst_reg ndc_w
= ndc
;
2760 ndc_w
.writemask
= WRITEMASK_W
;
2761 src_reg pos_w
= pos
;
2762 pos_w
.swizzle
= BRW_SWIZZLE4(SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
);
2763 emit_math(SHADER_OPCODE_RCP
, ndc_w
, pos_w
);
2765 dst_reg ndc_xyz
= ndc
;
2766 ndc_xyz
.writemask
= WRITEMASK_XYZ
;
2768 emit(MUL(ndc_xyz
, pos
, src_reg(ndc_w
)));
2772 vec4_visitor::emit_psiz_and_flags(struct brw_reg reg
)
2775 ((prog_data
->vue_map
.slots_valid
& VARYING_BIT_PSIZ
) ||
2776 key
->userclip_active
|| brw
->has_negative_rhw_bug
)) {
2777 dst_reg header1
= dst_reg(this, glsl_type::uvec4_type
);
2778 dst_reg header1_w
= header1
;
2779 header1_w
.writemask
= WRITEMASK_W
;
2781 emit(MOV(header1
, 0u));
2783 if (prog_data
->vue_map
.slots_valid
& VARYING_BIT_PSIZ
) {
2784 src_reg psiz
= src_reg(output_reg
[VARYING_SLOT_PSIZ
]);
2786 current_annotation
= "Point size";
2787 emit(MUL(header1_w
, psiz
, src_reg((float)(1 << 11))));
2788 emit(AND(header1_w
, src_reg(header1_w
), 0x7ff << 8));
2791 if (key
->userclip_active
) {
2792 current_annotation
= "Clipping flags";
2793 dst_reg flags0
= dst_reg(this, glsl_type::uint_type
);
2794 dst_reg flags1
= dst_reg(this, glsl_type::uint_type
);
2796 emit(CMP(dst_null_f(), src_reg(output_reg
[VARYING_SLOT_CLIP_DIST0
]), src_reg(0.0f
), BRW_CONDITIONAL_L
));
2797 emit(VS_OPCODE_UNPACK_FLAGS_SIMD4X2
, flags0
, src_reg(0));
2798 emit(OR(header1_w
, src_reg(header1_w
), src_reg(flags0
)));
2800 emit(CMP(dst_null_f(), src_reg(output_reg
[VARYING_SLOT_CLIP_DIST1
]), src_reg(0.0f
), BRW_CONDITIONAL_L
));
2801 emit(VS_OPCODE_UNPACK_FLAGS_SIMD4X2
, flags1
, src_reg(0));
2802 emit(SHL(flags1
, src_reg(flags1
), src_reg(4)));
2803 emit(OR(header1_w
, src_reg(header1_w
), src_reg(flags1
)));
2806 /* i965 clipping workaround:
2807 * 1) Test for -ve rhw
2809 * set ndc = (0,0,0,0)
2812 * Later, clipping will detect ucp[6] and ensure the primitive is
2813 * clipped against all fixed planes.
2815 if (brw
->has_negative_rhw_bug
) {
2816 src_reg ndc_w
= src_reg(output_reg
[BRW_VARYING_SLOT_NDC
]);
2817 ndc_w
.swizzle
= BRW_SWIZZLE_WWWW
;
2818 emit(CMP(dst_null_f(), ndc_w
, src_reg(0.0f
), BRW_CONDITIONAL_L
));
2819 vec4_instruction
*inst
;
2820 inst
= emit(OR(header1_w
, src_reg(header1_w
), src_reg(1u << 6)));
2821 inst
->predicate
= BRW_PREDICATE_NORMAL
;
2822 inst
= emit(MOV(output_reg
[BRW_VARYING_SLOT_NDC
], src_reg(0.0f
)));
2823 inst
->predicate
= BRW_PREDICATE_NORMAL
;
2826 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_UD
), src_reg(header1
)));
2827 } else if (brw
->gen
< 6) {
2828 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_UD
), 0u));
2830 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_D
), src_reg(0)));
2831 if (prog_data
->vue_map
.slots_valid
& VARYING_BIT_PSIZ
) {
2832 emit(MOV(brw_writemask(reg
, WRITEMASK_W
),
2833 src_reg(output_reg
[VARYING_SLOT_PSIZ
])));
2835 if (prog_data
->vue_map
.slots_valid
& VARYING_BIT_LAYER
) {
2836 emit(MOV(retype(brw_writemask(reg
, WRITEMASK_Y
), BRW_REGISTER_TYPE_D
),
2837 src_reg(output_reg
[VARYING_SLOT_LAYER
])));
2839 if (prog_data
->vue_map
.slots_valid
& VARYING_BIT_VIEWPORT
) {
2840 emit(MOV(retype(brw_writemask(reg
, WRITEMASK_Z
), BRW_REGISTER_TYPE_D
),
2841 src_reg(output_reg
[VARYING_SLOT_VIEWPORT
])));
2847 vec4_visitor::emit_clip_distances(dst_reg reg
, int offset
)
2849 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
2851 * "If a linked set of shaders forming the vertex stage contains no
2852 * static write to gl_ClipVertex or gl_ClipDistance, but the
2853 * application has requested clipping against user clip planes through
2854 * the API, then the coordinate written to gl_Position is used for
2855 * comparison against the user clip planes."
2857 * This function is only called if the shader didn't write to
2858 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
2859 * if the user wrote to it; otherwise we use gl_Position.
2861 gl_varying_slot clip_vertex
= VARYING_SLOT_CLIP_VERTEX
;
2862 if (!(prog_data
->vue_map
.slots_valid
& VARYING_BIT_CLIP_VERTEX
)) {
2863 clip_vertex
= VARYING_SLOT_POS
;
2866 for (int i
= 0; i
+ offset
< key
->nr_userclip_plane_consts
&& i
< 4;
2868 reg
.writemask
= 1 << i
;
2870 src_reg(output_reg
[clip_vertex
]),
2871 src_reg(this->userplane
[i
+ offset
])));
2876 vec4_visitor::emit_generic_urb_slot(dst_reg reg
, int varying
)
2878 assert (varying
< VARYING_SLOT_MAX
);
2879 reg
.type
= output_reg
[varying
].type
;
2880 current_annotation
= output_reg_annotation
[varying
];
2881 /* Copy the register, saturating if necessary */
2882 vec4_instruction
*inst
= emit(MOV(reg
,
2883 src_reg(output_reg
[varying
])));
2884 if ((varying
== VARYING_SLOT_COL0
||
2885 varying
== VARYING_SLOT_COL1
||
2886 varying
== VARYING_SLOT_BFC0
||
2887 varying
== VARYING_SLOT_BFC1
) &&
2888 key
->clamp_vertex_color
) {
2889 inst
->saturate
= true;
2894 vec4_visitor::emit_urb_slot(int mrf
, int varying
)
2896 struct brw_reg hw_reg
= brw_message_reg(mrf
);
2897 dst_reg reg
= dst_reg(MRF
, mrf
);
2898 reg
.type
= BRW_REGISTER_TYPE_F
;
2901 case VARYING_SLOT_PSIZ
:
2902 /* PSIZ is always in slot 0, and is coupled with other flags. */
2903 current_annotation
= "indices, point width, clip flags";
2904 emit_psiz_and_flags(hw_reg
);
2906 case BRW_VARYING_SLOT_NDC
:
2907 current_annotation
= "NDC";
2908 emit(MOV(reg
, src_reg(output_reg
[BRW_VARYING_SLOT_NDC
])));
2910 case VARYING_SLOT_POS
:
2911 current_annotation
= "gl_Position";
2912 emit(MOV(reg
, src_reg(output_reg
[VARYING_SLOT_POS
])));
2914 case VARYING_SLOT_EDGE
:
2915 /* This is present when doing unfilled polygons. We're supposed to copy
2916 * the edge flag from the user-provided vertex array
2917 * (glEdgeFlagPointer), or otherwise we'll copy from the current value
2918 * of that attribute (starts as 1.0f). This is then used in clipping to
2919 * determine which edges should be drawn as wireframe.
2921 current_annotation
= "edge flag";
2922 emit(MOV(reg
, src_reg(dst_reg(ATTR
, VERT_ATTRIB_EDGEFLAG
,
2923 glsl_type::float_type
, WRITEMASK_XYZW
))));
2925 case BRW_VARYING_SLOT_PAD
:
2926 /* No need to write to this slot */
2929 emit_generic_urb_slot(reg
, varying
);
2935 align_interleaved_urb_mlen(struct brw_context
*brw
, int mlen
)
2937 if (brw
->gen
>= 6) {
2938 /* URB data written (does not include the message header reg) must
2939 * be a multiple of 256 bits, or 2 VS registers. See vol5c.5,
2940 * section 5.4.3.2.2: URB_INTERLEAVED.
2942 * URB entries are allocated on a multiple of 1024 bits, so an
2943 * extra 128 bits written here to make the end align to 256 is
2946 if ((mlen
% 2) != 1)
2955 * Generates the VUE payload plus the necessary URB write instructions to
2958 * The VUE layout is documented in Volume 2a.
2961 vec4_visitor::emit_vertex()
2963 /* MRF 0 is reserved for the debugger, so start with message header
2968 /* In the process of generating our URB write message contents, we
2969 * may need to unspill a register or load from an array. Those
2970 * reads would use MRFs 14-15.
2972 int max_usable_mrf
= 13;
2974 /* The following assertion verifies that max_usable_mrf causes an
2975 * even-numbered amount of URB write data, which will meet gen6's
2976 * requirements for length alignment.
2978 assert ((max_usable_mrf
- base_mrf
) % 2 == 0);
2980 /* First mrf is the g0-based message header containing URB handles and
2983 emit_urb_write_header(mrf
++);
2986 emit_ndc_computation();
2989 /* Lower legacy ff and ClipVertex clipping to clip distances */
2990 if (key
->userclip_active
&& !prog
->UsesClipDistanceOut
) {
2991 current_annotation
= "user clip distances";
2993 output_reg
[VARYING_SLOT_CLIP_DIST0
] = dst_reg(this, glsl_type::vec4_type
);
2994 output_reg
[VARYING_SLOT_CLIP_DIST1
] = dst_reg(this, glsl_type::vec4_type
);
2996 emit_clip_distances(output_reg
[VARYING_SLOT_CLIP_DIST0
], 0);
2997 emit_clip_distances(output_reg
[VARYING_SLOT_CLIP_DIST1
], 4);
3000 /* We may need to split this up into several URB writes, so do them in a
3004 bool complete
= false;
3006 /* URB offset is in URB row increments, and each of our MRFs is half of
3007 * one of those, since we're doing interleaved writes.
3009 int offset
= slot
/ 2;
3012 for (; slot
< prog_data
->vue_map
.num_slots
; ++slot
) {
3013 emit_urb_slot(mrf
++, prog_data
->vue_map
.slot_to_varying
[slot
]);
3015 /* If this was max_usable_mrf, we can't fit anything more into this
3018 if (mrf
> max_usable_mrf
) {
3024 complete
= slot
>= prog_data
->vue_map
.num_slots
;
3025 current_annotation
= "URB write";
3026 vec4_instruction
*inst
= emit_urb_write_opcode(complete
);
3027 inst
->base_mrf
= base_mrf
;
3028 inst
->mlen
= align_interleaved_urb_mlen(brw
, mrf
- base_mrf
);
3029 inst
->offset
+= offset
;
3035 vec4_visitor::get_scratch_offset(vec4_instruction
*inst
,
3036 src_reg
*reladdr
, int reg_offset
)
3038 /* Because we store the values to scratch interleaved like our
3039 * vertex data, we need to scale the vec4 index by 2.
3041 int message_header_scale
= 2;
3043 /* Pre-gen6, the message header uses byte offsets instead of vec4
3044 * (16-byte) offset units.
3047 message_header_scale
*= 16;
3050 src_reg index
= src_reg(this, glsl_type::int_type
);
3052 emit_before(inst
, ADD(dst_reg(index
), *reladdr
, src_reg(reg_offset
)));
3053 emit_before(inst
, MUL(dst_reg(index
),
3054 index
, src_reg(message_header_scale
)));
3058 return src_reg(reg_offset
* message_header_scale
);
3063 vec4_visitor::get_pull_constant_offset(vec4_instruction
*inst
,
3064 src_reg
*reladdr
, int reg_offset
)
3067 src_reg index
= src_reg(this, glsl_type::int_type
);
3069 emit_before(inst
, ADD(dst_reg(index
), *reladdr
, src_reg(reg_offset
)));
3071 /* Pre-gen6, the message header uses byte offsets instead of vec4
3072 * (16-byte) offset units.
3075 emit_before(inst
, MUL(dst_reg(index
), index
, src_reg(16)));
3079 } else if (brw
->gen
>= 8) {
3080 /* Store the offset in a GRF so we can send-from-GRF. */
3081 src_reg offset
= src_reg(this, glsl_type::int_type
);
3082 emit_before(inst
, MOV(dst_reg(offset
), src_reg(reg_offset
)));
3085 int message_header_scale
= brw
->gen
< 6 ? 16 : 1;
3086 return src_reg(reg_offset
* message_header_scale
);
3091 * Emits an instruction before @inst to load the value named by @orig_src
3092 * from scratch space at @base_offset to @temp.
3094 * @base_offset is measured in 32-byte units (the size of a register).
3097 vec4_visitor::emit_scratch_read(vec4_instruction
*inst
,
3098 dst_reg temp
, src_reg orig_src
,
3101 int reg_offset
= base_offset
+ orig_src
.reg_offset
;
3102 src_reg index
= get_scratch_offset(inst
, orig_src
.reladdr
, reg_offset
);
3104 emit_before(inst
, SCRATCH_READ(temp
, index
));
3108 * Emits an instruction after @inst to store the value to be written
3109 * to @orig_dst to scratch space at @base_offset, from @temp.
3111 * @base_offset is measured in 32-byte units (the size of a register).
3114 vec4_visitor::emit_scratch_write(vec4_instruction
*inst
, int base_offset
)
3116 int reg_offset
= base_offset
+ inst
->dst
.reg_offset
;
3117 src_reg index
= get_scratch_offset(inst
, inst
->dst
.reladdr
, reg_offset
);
3119 /* Create a temporary register to store *inst's result in.
3121 * We have to be careful in MOVing from our temporary result register in
3122 * the scratch write. If we swizzle from channels of the temporary that
3123 * weren't initialized, it will confuse live interval analysis, which will
3124 * make spilling fail to make progress.
3126 src_reg temp
= src_reg(this, glsl_type::vec4_type
);
3127 temp
.type
= inst
->dst
.type
;
3128 int first_writemask_chan
= ffs(inst
->dst
.writemask
) - 1;
3130 for (int i
= 0; i
< 4; i
++)
3131 if (inst
->dst
.writemask
& (1 << i
))
3134 swizzles
[i
] = first_writemask_chan
;
3135 temp
.swizzle
= BRW_SWIZZLE4(swizzles
[0], swizzles
[1],
3136 swizzles
[2], swizzles
[3]);
3138 dst_reg dst
= dst_reg(brw_writemask(brw_vec8_grf(0, 0),
3139 inst
->dst
.writemask
));
3140 vec4_instruction
*write
= SCRATCH_WRITE(dst
, temp
, index
);
3141 write
->predicate
= inst
->predicate
;
3142 write
->ir
= inst
->ir
;
3143 write
->annotation
= inst
->annotation
;
3144 inst
->insert_after(write
);
3146 inst
->dst
.file
= temp
.file
;
3147 inst
->dst
.reg
= temp
.reg
;
3148 inst
->dst
.reg_offset
= temp
.reg_offset
;
3149 inst
->dst
.reladdr
= NULL
;
3153 * We can't generally support array access in GRF space, because a
3154 * single instruction's destination can only span 2 contiguous
3155 * registers. So, we send all GRF arrays that get variable index
3156 * access to scratch space.
3159 vec4_visitor::move_grf_array_access_to_scratch()
3161 int scratch_loc
[this->virtual_grf_count
];
3163 for (int i
= 0; i
< this->virtual_grf_count
; i
++) {
3164 scratch_loc
[i
] = -1;
3167 /* First, calculate the set of virtual GRFs that need to be punted
3168 * to scratch due to having any array access on them, and where in
3171 foreach_list(node
, &this->instructions
) {
3172 vec4_instruction
*inst
= (vec4_instruction
*)node
;
3174 if (inst
->dst
.file
== GRF
&& inst
->dst
.reladdr
&&
3175 scratch_loc
[inst
->dst
.reg
] == -1) {
3176 scratch_loc
[inst
->dst
.reg
] = c
->last_scratch
;
3177 c
->last_scratch
+= this->virtual_grf_sizes
[inst
->dst
.reg
];
3180 for (int i
= 0 ; i
< 3; i
++) {
3181 src_reg
*src
= &inst
->src
[i
];
3183 if (src
->file
== GRF
&& src
->reladdr
&&
3184 scratch_loc
[src
->reg
] == -1) {
3185 scratch_loc
[src
->reg
] = c
->last_scratch
;
3186 c
->last_scratch
+= this->virtual_grf_sizes
[src
->reg
];
3191 /* Now, for anything that will be accessed through scratch, rewrite
3192 * it to load/store. Note that this is a _safe list walk, because
3193 * we may generate a new scratch_write instruction after the one
3196 foreach_list_safe(node
, &this->instructions
) {
3197 vec4_instruction
*inst
= (vec4_instruction
*)node
;
3199 /* Set up the annotation tracking for new generated instructions. */
3201 current_annotation
= inst
->annotation
;
3203 if (inst
->dst
.file
== GRF
&& scratch_loc
[inst
->dst
.reg
] != -1) {
3204 emit_scratch_write(inst
, scratch_loc
[inst
->dst
.reg
]);
3207 for (int i
= 0 ; i
< 3; i
++) {
3208 if (inst
->src
[i
].file
!= GRF
|| scratch_loc
[inst
->src
[i
].reg
] == -1)
3211 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
3213 emit_scratch_read(inst
, temp
, inst
->src
[i
],
3214 scratch_loc
[inst
->src
[i
].reg
]);
3216 inst
->src
[i
].file
= temp
.file
;
3217 inst
->src
[i
].reg
= temp
.reg
;
3218 inst
->src
[i
].reg_offset
= temp
.reg_offset
;
3219 inst
->src
[i
].reladdr
= NULL
;
3225 * Emits an instruction before @inst to load the value named by @orig_src
3226 * from the pull constant buffer (surface) at @base_offset to @temp.
3229 vec4_visitor::emit_pull_constant_load(vec4_instruction
*inst
,
3230 dst_reg temp
, src_reg orig_src
,
3233 int reg_offset
= base_offset
+ orig_src
.reg_offset
;
3234 src_reg index
= src_reg(prog_data
->base
.binding_table
.pull_constants_start
);
3235 src_reg offset
= get_pull_constant_offset(inst
, orig_src
.reladdr
, reg_offset
);
3236 vec4_instruction
*load
;
3238 if (brw
->gen
>= 7) {
3239 dst_reg grf_offset
= dst_reg(this, glsl_type::int_type
);
3240 grf_offset
.type
= offset
.type
;
3241 emit_before(inst
, MOV(grf_offset
, offset
));
3243 load
= new(mem_ctx
) vec4_instruction(this,
3244 VS_OPCODE_PULL_CONSTANT_LOAD_GEN7
,
3245 temp
, index
, src_reg(grf_offset
));
3247 load
= new(mem_ctx
) vec4_instruction(this, VS_OPCODE_PULL_CONSTANT_LOAD
,
3248 temp
, index
, offset
);
3249 load
->base_mrf
= 14;
3252 emit_before(inst
, load
);
3256 * Implements array access of uniforms by inserting a
3257 * PULL_CONSTANT_LOAD instruction.
3259 * Unlike temporary GRF array access (where we don't support it due to
3260 * the difficulty of doing relative addressing on instruction
3261 * destinations), we could potentially do array access of uniforms
3262 * that were loaded in GRF space as push constants. In real-world
3263 * usage we've seen, though, the arrays being used are always larger
3264 * than we could load as push constants, so just always move all
3265 * uniform array access out to a pull constant buffer.
3268 vec4_visitor::move_uniform_array_access_to_pull_constants()
3270 int pull_constant_loc
[this->uniforms
];
3272 for (int i
= 0; i
< this->uniforms
; i
++) {
3273 pull_constant_loc
[i
] = -1;
3276 /* Walk through and find array access of uniforms. Put a copy of that
3277 * uniform in the pull constant buffer.
3279 * Note that we don't move constant-indexed accesses to arrays. No
3280 * testing has been done of the performance impact of this choice.
3282 foreach_list_safe(node
, &this->instructions
) {
3283 vec4_instruction
*inst
= (vec4_instruction
*)node
;
3285 for (int i
= 0 ; i
< 3; i
++) {
3286 if (inst
->src
[i
].file
!= UNIFORM
|| !inst
->src
[i
].reladdr
)
3289 int uniform
= inst
->src
[i
].reg
;
3291 /* If this array isn't already present in the pull constant buffer,
3294 if (pull_constant_loc
[uniform
] == -1) {
3295 const float **values
= &stage_prog_data
->param
[uniform
* 4];
3297 pull_constant_loc
[uniform
] = stage_prog_data
->nr_pull_params
/ 4;
3299 assert(uniform
< uniform_array_size
);
3300 for (int j
= 0; j
< uniform_size
[uniform
] * 4; j
++) {
3301 stage_prog_data
->pull_param
[stage_prog_data
->nr_pull_params
++]
3306 /* Set up the annotation tracking for new generated instructions. */
3308 current_annotation
= inst
->annotation
;
3310 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
3312 emit_pull_constant_load(inst
, temp
, inst
->src
[i
],
3313 pull_constant_loc
[uniform
]);
3315 inst
->src
[i
].file
= temp
.file
;
3316 inst
->src
[i
].reg
= temp
.reg
;
3317 inst
->src
[i
].reg_offset
= temp
.reg_offset
;
3318 inst
->src
[i
].reladdr
= NULL
;
3322 /* Now there are no accesses of the UNIFORM file with a reladdr, so
3323 * no need to track them as larger-than-vec4 objects. This will be
3324 * relied on in cutting out unused uniform vectors from push
3327 split_uniform_registers();
3331 vec4_visitor::resolve_ud_negate(src_reg
*reg
)
3333 if (reg
->type
!= BRW_REGISTER_TYPE_UD
||
3337 src_reg temp
= src_reg(this, glsl_type::uvec4_type
);
3338 emit(BRW_OPCODE_MOV
, dst_reg(temp
), *reg
);
3342 vec4_visitor::vec4_visitor(struct brw_context
*brw
,
3343 struct brw_vec4_compile
*c
,
3344 struct gl_program
*prog
,
3345 const struct brw_vec4_prog_key
*key
,
3346 struct brw_vec4_prog_data
*prog_data
,
3347 struct gl_shader_program
*shader_prog
,
3348 gl_shader_stage stage
,
3352 shader_time_shader_type st_base
,
3353 shader_time_shader_type st_written
,
3354 shader_time_shader_type st_reset
)
3355 : backend_visitor(brw
, shader_prog
, prog
, &prog_data
->base
, stage
),
3358 prog_data(prog_data
),
3359 sanity_param_count(0),
3361 first_non_payload_grf(0),
3362 need_all_constants_in_pull_buffer(false),
3363 debug_flag(debug_flag
),
3364 no_spills(no_spills
),
3366 st_written(st_written
),
3369 this->mem_ctx
= mem_ctx
;
3370 this->failed
= false;
3372 this->base_ir
= NULL
;
3373 this->current_annotation
= NULL
;
3374 memset(this->output_reg_annotation
, 0, sizeof(this->output_reg_annotation
));
3376 this->variable_ht
= hash_table_ctor(0,
3377 hash_table_pointer_hash
,
3378 hash_table_pointer_compare
);
3380 this->virtual_grf_start
= NULL
;
3381 this->virtual_grf_end
= NULL
;
3382 this->virtual_grf_sizes
= NULL
;
3383 this->virtual_grf_count
= 0;
3384 this->virtual_grf_reg_map
= NULL
;
3385 this->virtual_grf_reg_count
= 0;
3386 this->virtual_grf_array_size
= 0;
3387 this->live_intervals_valid
= false;
3389 this->max_grf
= brw
->gen
>= 7 ? GEN7_MRF_HACK_START
: BRW_MAX_GRF
;
3393 /* Initialize uniform_array_size to at least 1 because pre-gen6 VS requires
3394 * at least one. See setup_uniforms() in brw_vec4.cpp.
3396 this->uniform_array_size
= 1;
3398 this->uniform_array_size
= MAX2(stage_prog_data
->nr_params
, 1);
3401 this->uniform_size
= rzalloc_array(mem_ctx
, int, this->uniform_array_size
);
3402 this->uniform_vector_size
= rzalloc_array(mem_ctx
, int, this->uniform_array_size
);
3405 vec4_visitor::~vec4_visitor()
3407 hash_table_dtor(this->variable_ht
);
3412 vec4_visitor::fail(const char *format
, ...)
3422 va_start(va
, format
);
3423 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
3425 msg
= ralloc_asprintf(mem_ctx
, "vec4 compile failed: %s\n", msg
);
3427 this->fail_msg
= msg
;
3430 fprintf(stderr
, "%s", msg
);
3434 } /* namespace brw */