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 "glsl/ir_uniform.h"
27 #include "program/sampler.h"
29 #define FIRST_SPILL_MRF(gen) (gen == 6 ? 21 : 13)
33 vec4_instruction::vec4_instruction(enum opcode opcode
, const dst_reg
&dst
,
34 const src_reg
&src0
, const src_reg
&src1
,
37 this->opcode
= opcode
;
42 this->saturate
= false;
43 this->force_writemask_all
= false;
44 this->no_dd_clear
= false;
45 this->no_dd_check
= false;
46 this->writes_accumulator
= false;
47 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
48 this->predicate
= BRW_PREDICATE_NONE
;
49 this->predicate_inverse
= false;
51 this->regs_written
= (dst
.file
== BAD_FILE
? 0 : 1);
52 this->shadow_compare
= false;
54 this->urb_write_flags
= BRW_URB_WRITE_NO_FLAGS
;
55 this->header_size
= 0;
56 this->flag_subreg
= 0;
60 this->annotation
= NULL
;
64 vec4_visitor::emit(vec4_instruction
*inst
)
66 inst
->ir
= this->base_ir
;
67 inst
->annotation
= this->current_annotation
;
69 this->instructions
.push_tail(inst
);
75 vec4_visitor::emit_before(bblock_t
*block
, vec4_instruction
*inst
,
76 vec4_instruction
*new_inst
)
78 new_inst
->ir
= inst
->ir
;
79 new_inst
->annotation
= inst
->annotation
;
81 inst
->insert_before(block
, new_inst
);
87 vec4_visitor::emit(enum opcode opcode
, const dst_reg
&dst
, const src_reg
&src0
,
88 const src_reg
&src1
, const src_reg
&src2
)
90 return emit(new(mem_ctx
) vec4_instruction(opcode
, dst
, src0
, src1
, src2
));
95 vec4_visitor::emit(enum opcode opcode
, const dst_reg
&dst
, const src_reg
&src0
,
98 return emit(new(mem_ctx
) vec4_instruction(opcode
, dst
, src0
, src1
));
102 vec4_visitor::emit(enum opcode opcode
, const dst_reg
&dst
, const src_reg
&src0
)
104 return emit(new(mem_ctx
) vec4_instruction(opcode
, dst
, src0
));
108 vec4_visitor::emit(enum opcode opcode
, const dst_reg
&dst
)
110 return emit(new(mem_ctx
) vec4_instruction(opcode
, dst
));
114 vec4_visitor::emit(enum opcode opcode
)
116 return emit(new(mem_ctx
) vec4_instruction(opcode
, dst_reg()));
121 vec4_visitor::op(const dst_reg &dst, const src_reg &src0) \
123 return new(mem_ctx) vec4_instruction(BRW_OPCODE_##op, dst, src0); \
128 vec4_visitor::op(const dst_reg &dst, const src_reg &src0, \
129 const src_reg &src1) \
131 return new(mem_ctx) vec4_instruction(BRW_OPCODE_##op, dst, \
135 #define ALU2_ACC(op) \
137 vec4_visitor::op(const dst_reg &dst, const src_reg &src0, \
138 const src_reg &src1) \
140 vec4_instruction *inst = new(mem_ctx) vec4_instruction( \
141 BRW_OPCODE_##op, dst, src0, src1); \
142 inst->writes_accumulator = true; \
148 vec4_visitor::op(const dst_reg &dst, const src_reg &src0, \
149 const src_reg &src1, const src_reg &src2) \
151 assert(devinfo->gen >= 6); \
152 return new(mem_ctx) vec4_instruction(BRW_OPCODE_##op, dst, \
189 /** Gen4 predicated IF. */
191 vec4_visitor::IF(enum brw_predicate predicate
)
193 vec4_instruction
*inst
;
195 inst
= new(mem_ctx
) vec4_instruction(BRW_OPCODE_IF
);
196 inst
->predicate
= predicate
;
201 /** Gen6 IF with embedded comparison. */
203 vec4_visitor::IF(src_reg src0
, src_reg src1
,
204 enum brw_conditional_mod condition
)
206 assert(devinfo
->gen
== 6);
208 vec4_instruction
*inst
;
210 resolve_ud_negate(&src0
);
211 resolve_ud_negate(&src1
);
213 inst
= new(mem_ctx
) vec4_instruction(BRW_OPCODE_IF
, dst_null_d(),
215 inst
->conditional_mod
= condition
;
221 * CMP: Sets the low bit of the destination channels with the result
222 * of the comparison, while the upper bits are undefined, and updates
223 * the flag register with the packed 16 bits of the result.
226 vec4_visitor::CMP(dst_reg dst
, src_reg src0
, src_reg src1
,
227 enum brw_conditional_mod condition
)
229 vec4_instruction
*inst
;
231 /* Take the instruction:
233 * CMP null<d> src0<f> src1<f>
235 * Original gen4 does type conversion to the destination type before
236 * comparison, producing garbage results for floating point comparisons.
238 * The destination type doesn't matter on newer generations, so we set the
239 * type to match src0 so we can compact the instruction.
241 dst
.type
= src0
.type
;
242 if (dst
.file
== HW_REG
)
243 dst
.fixed_hw_reg
.type
= dst
.type
;
245 resolve_ud_negate(&src0
);
246 resolve_ud_negate(&src1
);
248 inst
= new(mem_ctx
) vec4_instruction(BRW_OPCODE_CMP
, dst
, src0
, src1
);
249 inst
->conditional_mod
= condition
;
255 vec4_visitor::SCRATCH_READ(const dst_reg
&dst
, const src_reg
&index
)
257 vec4_instruction
*inst
;
259 inst
= new(mem_ctx
) vec4_instruction(SHADER_OPCODE_GEN4_SCRATCH_READ
,
261 inst
->base_mrf
= FIRST_SPILL_MRF(devinfo
->gen
) + 1;
268 vec4_visitor::SCRATCH_WRITE(const dst_reg
&dst
, const src_reg
&src
,
269 const src_reg
&index
)
271 vec4_instruction
*inst
;
273 inst
= new(mem_ctx
) vec4_instruction(SHADER_OPCODE_GEN4_SCRATCH_WRITE
,
275 inst
->base_mrf
= FIRST_SPILL_MRF(devinfo
->gen
);
282 vec4_visitor::emit_dp(dst_reg dst
, src_reg src0
, src_reg src1
, unsigned elements
)
284 static enum opcode dot_opcodes
[] = {
285 BRW_OPCODE_DP2
, BRW_OPCODE_DP3
, BRW_OPCODE_DP4
288 emit(dot_opcodes
[elements
- 2], dst
, src0
, src1
);
292 vec4_visitor::fix_3src_operand(const src_reg
&src
)
294 /* Using vec4 uniforms in SIMD4x2 programs is difficult. You'd like to be
295 * able to use vertical stride of zero to replicate the vec4 uniform, like
297 * g3<0;4,1>:f - [0, 4][1, 5][2, 6][3, 7]
299 * But you can't, since vertical stride is always four in three-source
300 * instructions. Instead, insert a MOV instruction to do the replication so
301 * that the three-source instruction can consume it.
304 /* The MOV is only needed if the source is a uniform or immediate. */
305 if (src
.file
!= UNIFORM
&& src
.file
!= IMM
)
308 if (src
.file
== UNIFORM
&& brw_is_single_value_swizzle(src
.swizzle
))
311 dst_reg expanded
= dst_reg(this, glsl_type::vec4_type
);
312 expanded
.type
= src
.type
;
313 emit(VEC4_OPCODE_UNPACK_UNIFORM
, expanded
, src
);
314 return src_reg(expanded
);
318 vec4_visitor::resolve_source_modifiers(const src_reg
&src
)
320 if (!src
.abs
&& !src
.negate
)
323 dst_reg resolved
= dst_reg(this, glsl_type::ivec4_type
);
324 resolved
.type
= src
.type
;
325 emit(MOV(resolved
, src
));
327 return src_reg(resolved
);
331 vec4_visitor::fix_math_operand(const src_reg
&src
)
333 if (devinfo
->gen
< 6 || devinfo
->gen
>= 8 || src
.file
== BAD_FILE
)
336 /* The gen6 math instruction ignores the source modifiers --
337 * swizzle, abs, negate, and at least some parts of the register
338 * region description.
340 * Rather than trying to enumerate all these cases, *always* expand the
341 * operand to a temp GRF for gen6.
343 * For gen7, keep the operand as-is, except if immediate, which gen7 still
347 if (devinfo
->gen
== 7 && src
.file
!= IMM
)
350 dst_reg expanded
= dst_reg(this, glsl_type::vec4_type
);
351 expanded
.type
= src
.type
;
352 emit(MOV(expanded
, src
));
353 return src_reg(expanded
);
357 vec4_visitor::emit_math(enum opcode opcode
,
359 const src_reg
&src0
, const src_reg
&src1
)
361 vec4_instruction
*math
=
362 emit(opcode
, dst
, fix_math_operand(src0
), fix_math_operand(src1
));
364 if (devinfo
->gen
== 6 && dst
.writemask
!= WRITEMASK_XYZW
) {
365 /* MATH on Gen6 must be align1, so we can't do writemasks. */
366 math
->dst
= dst_reg(this, glsl_type::vec4_type
);
367 math
->dst
.type
= dst
.type
;
368 math
= emit(MOV(dst
, src_reg(math
->dst
)));
369 } else if (devinfo
->gen
< 6) {
371 math
->mlen
= src1
.file
== BAD_FILE
? 1 : 2;
378 vec4_visitor::emit_pack_half_2x16(dst_reg dst
, src_reg src0
)
380 if (devinfo
->gen
< 7) {
381 unreachable("ir_unop_pack_half_2x16 should be lowered");
384 assert(dst
.type
== BRW_REGISTER_TYPE_UD
);
385 assert(src0
.type
== BRW_REGISTER_TYPE_F
);
387 /* From the Ivybridge PRM, Vol4, Part3, Section 6.27 f32to16:
389 * Because this instruction does not have a 16-bit floating-point type,
390 * the destination data type must be Word (W).
392 * The destination must be DWord-aligned and specify a horizontal stride
393 * (HorzStride) of 2. The 16-bit result is stored in the lower word of
394 * each destination channel and the upper word is not modified.
396 * The above restriction implies that the f32to16 instruction must use
397 * align1 mode, because only in align1 mode is it possible to specify
398 * horizontal stride. We choose here to defy the hardware docs and emit
399 * align16 instructions.
401 * (I [chadv] did attempt to emit align1 instructions for VS f32to16
402 * instructions. I was partially successful in that the code passed all
403 * tests. However, the code was dubiously correct and fragile, and the
404 * tests were not harsh enough to probe that frailty. Not trusting the
405 * code, I chose instead to remain in align16 mode in defiance of the hw
408 * I've [chadv] experimentally confirmed that, on gen7 hardware and the
409 * simulator, emitting a f32to16 in align16 mode with UD as destination
410 * data type is safe. The behavior differs from that specified in the PRM
411 * in that the upper word of each destination channel is cleared to 0.
414 dst_reg
tmp_dst(this, glsl_type::uvec2_type
);
415 src_reg
tmp_src(tmp_dst
);
418 /* Verify the undocumented behavior on which the following instructions
419 * rely. If f32to16 fails to clear the upper word of the X and Y channels,
420 * then the result of the bit-or instruction below will be incorrect.
422 * You should inspect the disasm output in order to verify that the MOV is
423 * not optimized away.
425 emit(MOV(tmp_dst
, src_reg(0x12345678u
)));
428 /* Give tmp the form below, where "." means untouched.
431 * |.|.|0x0000hhhh|0x0000llll|.|.|0x0000hhhh|0x0000llll|
433 * That the upper word of each write-channel be 0 is required for the
434 * following bit-shift and bit-or instructions to work. Note that this
435 * relies on the undocumented hardware behavior mentioned above.
437 tmp_dst
.writemask
= WRITEMASK_XY
;
438 emit(F32TO16(tmp_dst
, src0
));
440 /* Give the write-channels of dst the form:
443 tmp_src
.swizzle
= BRW_SWIZZLE_YYYY
;
444 emit(SHL(dst
, tmp_src
, src_reg(16u)));
446 /* Finally, give the write-channels of dst the form of packHalf2x16's
450 tmp_src
.swizzle
= BRW_SWIZZLE_XXXX
;
451 emit(OR(dst
, src_reg(dst
), tmp_src
));
455 vec4_visitor::emit_unpack_half_2x16(dst_reg dst
, src_reg src0
)
457 if (devinfo
->gen
< 7) {
458 unreachable("ir_unop_unpack_half_2x16 should be lowered");
461 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
462 assert(src0
.type
== BRW_REGISTER_TYPE_UD
);
464 /* From the Ivybridge PRM, Vol4, Part3, Section 6.26 f16to32:
466 * Because this instruction does not have a 16-bit floating-point type,
467 * the source data type must be Word (W). The destination type must be
470 * To use W as the source data type, we must adjust horizontal strides,
471 * which is only possible in align1 mode. All my [chadv] attempts at
472 * emitting align1 instructions for unpackHalf2x16 failed to pass the
473 * Piglit tests, so I gave up.
475 * I've verified that, on gen7 hardware and the simulator, it is safe to
476 * emit f16to32 in align16 mode with UD as source data type.
479 dst_reg
tmp_dst(this, glsl_type::uvec2_type
);
480 src_reg
tmp_src(tmp_dst
);
482 tmp_dst
.writemask
= WRITEMASK_X
;
483 emit(AND(tmp_dst
, src0
, src_reg(0xffffu
)));
485 tmp_dst
.writemask
= WRITEMASK_Y
;
486 emit(SHR(tmp_dst
, src0
, src_reg(16u)));
488 dst
.writemask
= WRITEMASK_XY
;
489 emit(F16TO32(dst
, tmp_src
));
493 vec4_visitor::emit_unpack_unorm_4x8(const dst_reg
&dst
, src_reg src0
)
495 /* Instead of splitting the 32-bit integer, shifting, and ORing it back
496 * together, we can shift it by <0, 8, 16, 24>. The packed integer immediate
497 * is not suitable to generate the shift values, but we can use the packed
498 * vector float and a type-converting MOV.
500 dst_reg
shift(this, glsl_type::uvec4_type
);
501 emit(MOV(shift
, src_reg(0x00, 0x60, 0x70, 0x78)));
503 dst_reg
shifted(this, glsl_type::uvec4_type
);
504 src0
.swizzle
= BRW_SWIZZLE_XXXX
;
505 emit(SHR(shifted
, src0
, src_reg(shift
)));
507 shifted
.type
= BRW_REGISTER_TYPE_UB
;
508 dst_reg
f(this, glsl_type::vec4_type
);
509 emit(VEC4_OPCODE_MOV_BYTES
, f
, src_reg(shifted
));
511 emit(MUL(dst
, src_reg(f
), src_reg(1.0f
/ 255.0f
)));
515 vec4_visitor::emit_unpack_snorm_4x8(const dst_reg
&dst
, src_reg src0
)
517 /* Instead of splitting the 32-bit integer, shifting, and ORing it back
518 * together, we can shift it by <0, 8, 16, 24>. The packed integer immediate
519 * is not suitable to generate the shift values, but we can use the packed
520 * vector float and a type-converting MOV.
522 dst_reg
shift(this, glsl_type::uvec4_type
);
523 emit(MOV(shift
, src_reg(0x00, 0x60, 0x70, 0x78)));
525 dst_reg
shifted(this, glsl_type::uvec4_type
);
526 src0
.swizzle
= BRW_SWIZZLE_XXXX
;
527 emit(SHR(shifted
, src0
, src_reg(shift
)));
529 shifted
.type
= BRW_REGISTER_TYPE_B
;
530 dst_reg
f(this, glsl_type::vec4_type
);
531 emit(VEC4_OPCODE_MOV_BYTES
, f
, src_reg(shifted
));
533 dst_reg
scaled(this, glsl_type::vec4_type
);
534 emit(MUL(scaled
, src_reg(f
), src_reg(1.0f
/ 127.0f
)));
536 dst_reg
max(this, glsl_type::vec4_type
);
537 emit_minmax(BRW_CONDITIONAL_GE
, max
, src_reg(scaled
), src_reg(-1.0f
));
538 emit_minmax(BRW_CONDITIONAL_L
, dst
, src_reg(max
), src_reg(1.0f
));
542 vec4_visitor::emit_pack_unorm_4x8(const dst_reg
&dst
, const src_reg
&src0
)
544 dst_reg
saturated(this, glsl_type::vec4_type
);
545 vec4_instruction
*inst
= emit(MOV(saturated
, src0
));
546 inst
->saturate
= true;
548 dst_reg
scaled(this, glsl_type::vec4_type
);
549 emit(MUL(scaled
, src_reg(saturated
), src_reg(255.0f
)));
551 dst_reg
rounded(this, glsl_type::vec4_type
);
552 emit(RNDE(rounded
, src_reg(scaled
)));
554 dst_reg
u(this, glsl_type::uvec4_type
);
555 emit(MOV(u
, src_reg(rounded
)));
558 emit(VEC4_OPCODE_PACK_BYTES
, dst
, bytes
);
562 vec4_visitor::emit_pack_snorm_4x8(const dst_reg
&dst
, const src_reg
&src0
)
564 dst_reg
max(this, glsl_type::vec4_type
);
565 emit_minmax(BRW_CONDITIONAL_GE
, max
, src0
, src_reg(-1.0f
));
567 dst_reg
min(this, glsl_type::vec4_type
);
568 emit_minmax(BRW_CONDITIONAL_L
, min
, src_reg(max
), src_reg(1.0f
));
570 dst_reg
scaled(this, glsl_type::vec4_type
);
571 emit(MUL(scaled
, src_reg(min
), src_reg(127.0f
)));
573 dst_reg
rounded(this, glsl_type::vec4_type
);
574 emit(RNDE(rounded
, src_reg(scaled
)));
576 dst_reg
i(this, glsl_type::ivec4_type
);
577 emit(MOV(i
, src_reg(rounded
)));
580 emit(VEC4_OPCODE_PACK_BYTES
, dst
, bytes
);
584 * Returns the minimum number of vec4 elements needed to pack a type.
586 * For simple types, it will return 1 (a single vec4); for matrices, the
587 * number of columns; for array and struct, the sum of the vec4_size of
588 * each of its elements; and for sampler and atomic, zero.
590 * This method is useful to calculate how much register space is needed to
591 * store a particular type.
594 type_size_vec4(const struct glsl_type
*type
)
599 switch (type
->base_type
) {
602 case GLSL_TYPE_FLOAT
:
604 if (type
->is_matrix()) {
605 return type
->matrix_columns
;
607 /* Regardless of size of vector, it gets a vec4. This is bad
608 * packing for things like floats, but otherwise arrays become a
609 * mess. Hopefully a later pass over the code can pack scalars
610 * down if appropriate.
614 case GLSL_TYPE_ARRAY
:
615 assert(type
->length
> 0);
616 return type_size_vec4(type
->fields
.array
) * type
->length
;
617 case GLSL_TYPE_STRUCT
:
619 for (i
= 0; i
< type
->length
; i
++) {
620 size
+= type_size_vec4(type
->fields
.structure
[i
].type
);
623 case GLSL_TYPE_SUBROUTINE
:
626 case GLSL_TYPE_SAMPLER
:
627 /* Samplers take up no register space, since they're baked in at
631 case GLSL_TYPE_ATOMIC_UINT
:
633 case GLSL_TYPE_IMAGE
:
634 return DIV_ROUND_UP(BRW_IMAGE_PARAM_SIZE
, 4);
636 case GLSL_TYPE_DOUBLE
:
637 case GLSL_TYPE_ERROR
:
638 case GLSL_TYPE_INTERFACE
:
639 unreachable("not reached");
645 src_reg::src_reg(class vec4_visitor
*v
, const struct glsl_type
*type
)
650 this->reg
= v
->alloc
.allocate(type_size_vec4(type
));
652 if (type
->is_array() || type
->is_record()) {
653 this->swizzle
= BRW_SWIZZLE_NOOP
;
655 this->swizzle
= brw_swizzle_for_size(type
->vector_elements
);
658 this->type
= brw_type_for_base_type(type
);
661 src_reg::src_reg(class vec4_visitor
*v
, const struct glsl_type
*type
, int size
)
668 this->reg
= v
->alloc
.allocate(type_size_vec4(type
) * size
);
670 this->swizzle
= BRW_SWIZZLE_NOOP
;
672 this->type
= brw_type_for_base_type(type
);
675 dst_reg::dst_reg(class vec4_visitor
*v
, const struct glsl_type
*type
)
680 this->reg
= v
->alloc
.allocate(type_size_vec4(type
));
682 if (type
->is_array() || type
->is_record()) {
683 this->writemask
= WRITEMASK_XYZW
;
685 this->writemask
= (1 << type
->vector_elements
) - 1;
688 this->type
= brw_type_for_base_type(type
);
692 vec4_visitor::setup_vec4_uniform_value(unsigned param_offset
,
693 const gl_constant_value
*values
,
696 static const gl_constant_value zero
= { 0 };
698 assert(param_offset
% 4 == 0);
700 for (unsigned i
= 0; i
< n
; ++i
)
701 stage_prog_data
->param
[param_offset
+ i
] = &values
[i
];
703 for (unsigned i
= n
; i
< 4; ++i
)
704 stage_prog_data
->param
[param_offset
+ i
] = &zero
;
708 vec4_visitor::emit_minmax(enum brw_conditional_mod conditionalmod
, dst_reg dst
,
709 src_reg src0
, src_reg src1
)
711 vec4_instruction
*inst
;
713 if (devinfo
->gen
>= 6) {
714 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
715 inst
->conditional_mod
= conditionalmod
;
717 emit(CMP(dst
, src0
, src1
, conditionalmod
));
719 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
720 inst
->predicate
= BRW_PREDICATE_NORMAL
;
727 vec4_visitor::emit_lrp(const dst_reg
&dst
,
728 const src_reg
&x
, const src_reg
&y
, const src_reg
&a
)
730 if (devinfo
->gen
>= 6) {
731 /* Note that the instruction's argument order is reversed from GLSL
734 return emit(LRP(dst
, fix_3src_operand(a
), fix_3src_operand(y
),
735 fix_3src_operand(x
)));
737 /* Earlier generations don't support three source operations, so we
738 * need to emit x*(1-a) + y*a.
740 dst_reg y_times_a
= dst_reg(this, glsl_type::vec4_type
);
741 dst_reg one_minus_a
= dst_reg(this, glsl_type::vec4_type
);
742 dst_reg x_times_one_minus_a
= dst_reg(this, glsl_type::vec4_type
);
743 y_times_a
.writemask
= dst
.writemask
;
744 one_minus_a
.writemask
= dst
.writemask
;
745 x_times_one_minus_a
.writemask
= dst
.writemask
;
747 emit(MUL(y_times_a
, y
, a
));
748 emit(ADD(one_minus_a
, negate(a
), src_reg(1.0f
)));
749 emit(MUL(x_times_one_minus_a
, x
, src_reg(one_minus_a
)));
750 return emit(ADD(dst
, src_reg(x_times_one_minus_a
), src_reg(y_times_a
)));
755 * Emits the instructions needed to perform a pull constant load. before_block
756 * and before_inst can be NULL in which case the instruction will be appended
757 * to the end of the instruction list.
760 vec4_visitor::emit_pull_constant_load_reg(dst_reg dst
,
763 bblock_t
*before_block
,
764 vec4_instruction
*before_inst
)
766 assert((before_inst
== NULL
&& before_block
== NULL
) ||
767 (before_inst
&& before_block
));
769 vec4_instruction
*pull
;
771 if (devinfo
->gen
>= 9) {
772 /* Gen9+ needs a message header in order to use SIMD4x2 mode */
773 src_reg
header(this, glsl_type::uvec4_type
, 2);
776 vec4_instruction(VS_OPCODE_SET_SIMD4X2_HEADER_GEN9
,
780 emit_before(before_block
, before_inst
, pull
);
784 dst_reg index_reg
= retype(offset(dst_reg(header
), 1),
786 pull
= MOV(writemask(index_reg
, WRITEMASK_X
), offset_reg
);
789 emit_before(before_block
, before_inst
, pull
);
793 pull
= new(mem_ctx
) vec4_instruction(VS_OPCODE_PULL_CONSTANT_LOAD_GEN7
,
798 pull
->header_size
= 1;
799 } else if (devinfo
->gen
>= 7) {
800 dst_reg grf_offset
= dst_reg(this, glsl_type::int_type
);
802 grf_offset
.type
= offset_reg
.type
;
804 pull
= MOV(grf_offset
, offset_reg
);
807 emit_before(before_block
, before_inst
, pull
);
811 pull
= new(mem_ctx
) vec4_instruction(VS_OPCODE_PULL_CONSTANT_LOAD_GEN7
,
814 src_reg(grf_offset
));
817 pull
= new(mem_ctx
) vec4_instruction(VS_OPCODE_PULL_CONSTANT_LOAD
,
821 pull
->base_mrf
= FIRST_SPILL_MRF(devinfo
->gen
) + 1;
826 emit_before(before_block
, before_inst
, pull
);
832 vec4_visitor::emit_uniformize(const src_reg
&src
)
834 const src_reg
chan_index(this, glsl_type::uint_type
);
835 const dst_reg dst
= retype(dst_reg(this, glsl_type::uint_type
),
838 emit(SHADER_OPCODE_FIND_LIVE_CHANNEL
, dst_reg(chan_index
))
839 ->force_writemask_all
= true;
840 emit(SHADER_OPCODE_BROADCAST
, dst
, src
, chan_index
)
841 ->force_writemask_all
= true;
847 vec4_visitor::emit_mcs_fetch(const glsl_type
*coordinate_type
,
848 src_reg coordinate
, src_reg sampler
)
850 vec4_instruction
*inst
=
851 new(mem_ctx
) vec4_instruction(SHADER_OPCODE_TXF_MCS
,
852 dst_reg(this, glsl_type::uvec4_type
));
854 inst
->src
[1] = sampler
;
858 if (devinfo
->gen
>= 9) {
859 /* Gen9+ needs a message header in order to use SIMD4x2 mode */
860 vec4_instruction
*header_inst
= new(mem_ctx
)
861 vec4_instruction(VS_OPCODE_SET_SIMD4X2_HEADER_GEN9
,
862 dst_reg(MRF
, inst
->base_mrf
));
867 inst
->header_size
= 1;
868 param_base
= inst
->base_mrf
+ 1;
871 param_base
= inst
->base_mrf
;
874 /* parameters are: u, v, r, lod; lod will always be zero due to api restrictions */
875 int coord_mask
= (1 << coordinate_type
->vector_elements
) - 1;
876 int zero_mask
= 0xf & ~coord_mask
;
878 emit(MOV(dst_reg(MRF
, param_base
, coordinate_type
, coord_mask
),
881 emit(MOV(dst_reg(MRF
, param_base
, coordinate_type
, zero_mask
),
885 return src_reg(inst
->dst
);
889 vec4_visitor::is_high_sampler(src_reg sampler
)
891 if (devinfo
->gen
< 8 && !devinfo
->is_haswell
)
894 return sampler
.file
!= IMM
|| sampler
.fixed_hw_reg
.dw1
.ud
>= 16;
898 vec4_visitor::emit_texture(ir_texture_opcode op
,
900 const glsl_type
*dest_type
,
902 int coord_components
,
903 src_reg shadow_comparitor
,
904 src_reg lod
, src_reg lod2
,
905 src_reg sample_index
,
906 uint32_t constant_offset
,
907 src_reg offset_value
,
915 case ir_tex
: opcode
= SHADER_OPCODE_TXL
; break;
916 case ir_txl
: opcode
= SHADER_OPCODE_TXL
; break;
917 case ir_txd
: opcode
= SHADER_OPCODE_TXD
; break;
918 case ir_txf
: opcode
= SHADER_OPCODE_TXF
; break;
919 case ir_txf_ms
: opcode
= SHADER_OPCODE_TXF_CMS
; break;
920 case ir_txs
: opcode
= SHADER_OPCODE_TXS
; break;
921 case ir_tg4
: opcode
= offset_value
.file
!= BAD_FILE
922 ? SHADER_OPCODE_TG4_OFFSET
: SHADER_OPCODE_TG4
; break;
923 case ir_query_levels
: opcode
= SHADER_OPCODE_TXS
; break;
924 case ir_texture_samples
: opcode
= SHADER_OPCODE_SAMPLEINFO
; break;
926 unreachable("TXB is not valid for vertex shaders.");
928 unreachable("LOD is not valid for vertex shaders.");
930 unreachable("Unrecognized tex op");
933 vec4_instruction
*inst
= new(mem_ctx
) vec4_instruction(
934 opcode
, dst_reg(this, dest_type
));
936 inst
->offset
= constant_offset
;
938 /* The message header is necessary for:
940 * - Gen9+ for selecting SIMD4x2
942 * - Gather channel selection
943 * - Sampler indices too large to fit in a 4-bit value.
944 * - Sampleinfo message - takes no parameters, but mlen = 0 is illegal
947 (devinfo
->gen
< 5 || devinfo
->gen
>= 9 ||
948 inst
->offset
!= 0 || op
== ir_tg4
||
949 op
== ir_texture_samples
||
950 is_high_sampler(sampler_reg
)) ? 1 : 0;
952 inst
->mlen
= inst
->header_size
;
953 inst
->dst
.writemask
= WRITEMASK_XYZW
;
954 inst
->shadow_compare
= shadow_comparitor
.file
!= BAD_FILE
;
956 inst
->src
[1] = sampler_reg
;
958 /* MRF for the first parameter */
959 int param_base
= inst
->base_mrf
+ inst
->header_size
;
961 if (op
== ir_txs
|| op
== ir_query_levels
) {
962 int writemask
= devinfo
->gen
== 4 ? WRITEMASK_W
: WRITEMASK_X
;
963 emit(MOV(dst_reg(MRF
, param_base
, lod
.type
, writemask
), lod
));
965 } else if (op
== ir_texture_samples
) {
966 inst
->dst
.writemask
= WRITEMASK_X
;
968 /* Load the coordinate */
969 /* FINISHME: gl_clamp_mask and saturate */
970 int coord_mask
= (1 << coord_components
) - 1;
971 int zero_mask
= 0xf & ~coord_mask
;
973 emit(MOV(dst_reg(MRF
, param_base
, coordinate
.type
, coord_mask
),
977 if (zero_mask
!= 0) {
978 emit(MOV(dst_reg(MRF
, param_base
, coordinate
.type
, zero_mask
),
981 /* Load the shadow comparitor */
982 if (shadow_comparitor
.file
!= BAD_FILE
&& op
!= ir_txd
&& (op
!= ir_tg4
|| offset_value
.file
== BAD_FILE
)) {
983 emit(MOV(dst_reg(MRF
, param_base
+ 1, shadow_comparitor
.type
,
989 /* Load the LOD info */
990 if (op
== ir_tex
|| op
== ir_txl
) {
992 if (devinfo
->gen
>= 5) {
993 mrf
= param_base
+ 1;
994 if (shadow_comparitor
.file
!= BAD_FILE
) {
995 writemask
= WRITEMASK_Y
;
996 /* mlen already incremented */
998 writemask
= WRITEMASK_X
;
1001 } else /* devinfo->gen == 4 */ {
1003 writemask
= WRITEMASK_W
;
1005 emit(MOV(dst_reg(MRF
, mrf
, lod
.type
, writemask
), lod
));
1006 } else if (op
== ir_txf
) {
1007 emit(MOV(dst_reg(MRF
, param_base
, lod
.type
, WRITEMASK_W
), lod
));
1008 } else if (op
== ir_txf_ms
) {
1009 emit(MOV(dst_reg(MRF
, param_base
+ 1, sample_index
.type
, WRITEMASK_X
),
1011 if (devinfo
->gen
>= 7) {
1012 /* MCS data is in the first channel of `mcs`, but we need to get it into
1013 * the .y channel of the second vec4 of params, so replicate .x across
1014 * the whole vec4 and then mask off everything except .y
1016 mcs
.swizzle
= BRW_SWIZZLE_XXXX
;
1017 emit(MOV(dst_reg(MRF
, param_base
+ 1, glsl_type::uint_type
, WRITEMASK_Y
),
1021 } else if (op
== ir_txd
) {
1022 const brw_reg_type type
= lod
.type
;
1024 if (devinfo
->gen
>= 5) {
1025 lod
.swizzle
= BRW_SWIZZLE4(SWIZZLE_X
,SWIZZLE_X
,SWIZZLE_Y
,SWIZZLE_Y
);
1026 lod2
.swizzle
= BRW_SWIZZLE4(SWIZZLE_X
,SWIZZLE_X
,SWIZZLE_Y
,SWIZZLE_Y
);
1027 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_XZ
), lod
));
1028 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_YW
), lod2
));
1031 if (dest_type
->vector_elements
== 3 || shadow_comparitor
.file
!= BAD_FILE
) {
1032 lod
.swizzle
= BRW_SWIZZLE_ZZZZ
;
1033 lod2
.swizzle
= BRW_SWIZZLE_ZZZZ
;
1034 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_X
), lod
));
1035 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_Y
), lod2
));
1038 if (shadow_comparitor
.file
!= BAD_FILE
) {
1039 emit(MOV(dst_reg(MRF
, param_base
+ 2,
1040 shadow_comparitor
.type
, WRITEMASK_Z
),
1041 shadow_comparitor
));
1044 } else /* devinfo->gen == 4 */ {
1045 emit(MOV(dst_reg(MRF
, param_base
+ 1, type
, WRITEMASK_XYZ
), lod
));
1046 emit(MOV(dst_reg(MRF
, param_base
+ 2, type
, WRITEMASK_XYZ
), lod2
));
1049 } else if (op
== ir_tg4
&& offset_value
.file
!= BAD_FILE
) {
1050 if (shadow_comparitor
.file
!= BAD_FILE
) {
1051 emit(MOV(dst_reg(MRF
, param_base
, shadow_comparitor
.type
, WRITEMASK_W
),
1052 shadow_comparitor
));
1055 emit(MOV(dst_reg(MRF
, param_base
+ 1, glsl_type::ivec2_type
, WRITEMASK_XY
),
1063 /* fixup num layers (z) for cube arrays: hardware returns faces * layers;
1064 * spec requires layers.
1066 if (op
== ir_txs
&& is_cube_array
) {
1067 emit_math(SHADER_OPCODE_INT_QUOTIENT
,
1068 writemask(inst
->dst
, WRITEMASK_Z
),
1069 src_reg(inst
->dst
), src_reg(6));
1072 if (devinfo
->gen
== 6 && op
== ir_tg4
) {
1073 emit_gen6_gather_wa(key_tex
->gen6_gather_wa
[sampler
], inst
->dst
);
1076 swizzle_result(op
, dest
,
1077 src_reg(inst
->dst
), sampler
, dest_type
);
1081 * Apply workarounds for Gen6 gather with UINT/SINT
1084 vec4_visitor::emit_gen6_gather_wa(uint8_t wa
, dst_reg dst
)
1089 int width
= (wa
& WA_8BIT
) ? 8 : 16;
1090 dst_reg dst_f
= dst
;
1091 dst_f
.type
= BRW_REGISTER_TYPE_F
;
1093 /* Convert from UNORM to UINT */
1094 emit(MUL(dst_f
, src_reg(dst_f
), src_reg((float)((1 << width
) - 1))));
1095 emit(MOV(dst
, src_reg(dst_f
)));
1098 /* Reinterpret the UINT value as a signed INT value by
1099 * shifting the sign bit into place, then shifting back
1102 emit(SHL(dst
, src_reg(dst
), src_reg(32 - width
)));
1103 emit(ASR(dst
, src_reg(dst
), src_reg(32 - width
)));
1108 * Set up the gather channel based on the swizzle, for gather4.
1111 vec4_visitor::gather_channel(unsigned gather_component
, uint32_t sampler
)
1113 int swiz
= GET_SWZ(key_tex
->swizzles
[sampler
], gather_component
);
1115 case SWIZZLE_X
: return 0;
1117 /* gather4 sampler is broken for green channel on RG32F --
1118 * we must ask for blue instead.
1120 if (key_tex
->gather_channel_quirk_mask
& (1 << sampler
))
1123 case SWIZZLE_Z
: return 2;
1124 case SWIZZLE_W
: return 3;
1126 unreachable("Not reached"); /* zero, one swizzles handled already */
1131 vec4_visitor::swizzle_result(ir_texture_opcode op
, dst_reg dest
,
1132 src_reg orig_val
, uint32_t sampler
,
1133 const glsl_type
*dest_type
)
1135 int s
= key_tex
->swizzles
[sampler
];
1137 dst_reg swizzled_result
= dest
;
1139 if (op
== ir_query_levels
) {
1140 /* # levels is in .w */
1141 orig_val
.swizzle
= BRW_SWIZZLE4(SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
);
1142 emit(MOV(swizzled_result
, orig_val
));
1146 if (op
== ir_txs
|| dest_type
== glsl_type::float_type
1147 || s
== SWIZZLE_NOOP
|| op
== ir_tg4
) {
1148 emit(MOV(swizzled_result
, orig_val
));
1153 int zero_mask
= 0, one_mask
= 0, copy_mask
= 0;
1154 int swizzle
[4] = {0};
1156 for (int i
= 0; i
< 4; i
++) {
1157 switch (GET_SWZ(s
, i
)) {
1159 zero_mask
|= (1 << i
);
1162 one_mask
|= (1 << i
);
1165 copy_mask
|= (1 << i
);
1166 swizzle
[i
] = GET_SWZ(s
, i
);
1172 orig_val
.swizzle
= BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1173 swizzled_result
.writemask
= copy_mask
;
1174 emit(MOV(swizzled_result
, orig_val
));
1178 swizzled_result
.writemask
= zero_mask
;
1179 emit(MOV(swizzled_result
, src_reg(0.0f
)));
1183 swizzled_result
.writemask
= one_mask
;
1184 emit(MOV(swizzled_result
, src_reg(1.0f
)));
1189 vec4_visitor::gs_emit_vertex(int stream_id
)
1191 unreachable("not reached");
1195 vec4_visitor::gs_end_primitive()
1197 unreachable("not reached");
1201 vec4_visitor::emit_untyped_atomic(unsigned atomic_op
, unsigned surf_index
,
1202 dst_reg dst
, src_reg offset
,
1203 src_reg src0
, src_reg src1
)
1207 /* Set the atomic operation offset. */
1208 emit(MOV(brw_writemask(brw_uvec_mrf(8, mlen
, 0), WRITEMASK_X
), offset
));
1211 /* Set the atomic operation arguments. */
1212 if (src0
.file
!= BAD_FILE
) {
1213 emit(MOV(brw_writemask(brw_uvec_mrf(8, mlen
, 0), WRITEMASK_X
), src0
));
1217 if (src1
.file
!= BAD_FILE
) {
1218 emit(MOV(brw_writemask(brw_uvec_mrf(8, mlen
, 0), WRITEMASK_X
), src1
));
1222 /* Emit the instruction. Note that this maps to the normal SIMD8
1223 * untyped atomic message on Ivy Bridge, but that's OK because
1224 * unused channels will be masked out.
1226 vec4_instruction
*inst
= emit(SHADER_OPCODE_UNTYPED_ATOMIC
, dst
,
1228 src_reg(surf_index
), src_reg(atomic_op
));
1233 vec4_visitor::emit_untyped_surface_read(unsigned surf_index
, dst_reg dst
,
1236 /* Set the surface read offset. */
1237 emit(MOV(brw_writemask(brw_uvec_mrf(8, 0, 0), WRITEMASK_X
), offset
));
1239 /* Emit the instruction. Note that this maps to the normal SIMD8
1240 * untyped surface read message, but that's OK because unused
1241 * channels will be masked out.
1243 vec4_instruction
*inst
= emit(SHADER_OPCODE_UNTYPED_SURFACE_READ
, dst
,
1245 src_reg(surf_index
), src_reg(1));
1250 vec4_visitor::emit_ndc_computation()
1252 /* Get the position */
1253 src_reg pos
= src_reg(output_reg
[VARYING_SLOT_POS
]);
1255 /* Build ndc coords, which are (x/w, y/w, z/w, 1/w) */
1256 dst_reg ndc
= dst_reg(this, glsl_type::vec4_type
);
1257 output_reg
[BRW_VARYING_SLOT_NDC
] = ndc
;
1259 current_annotation
= "NDC";
1260 dst_reg ndc_w
= ndc
;
1261 ndc_w
.writemask
= WRITEMASK_W
;
1262 src_reg pos_w
= pos
;
1263 pos_w
.swizzle
= BRW_SWIZZLE4(SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
, SWIZZLE_W
);
1264 emit_math(SHADER_OPCODE_RCP
, ndc_w
, pos_w
);
1266 dst_reg ndc_xyz
= ndc
;
1267 ndc_xyz
.writemask
= WRITEMASK_XYZ
;
1269 emit(MUL(ndc_xyz
, pos
, src_reg(ndc_w
)));
1273 vec4_visitor::emit_psiz_and_flags(dst_reg reg
)
1275 if (devinfo
->gen
< 6 &&
1276 ((prog_data
->vue_map
.slots_valid
& VARYING_BIT_PSIZ
) ||
1277 output_reg
[VARYING_SLOT_CLIP_DIST0
].file
!= BAD_FILE
||
1278 devinfo
->has_negative_rhw_bug
)) {
1279 dst_reg header1
= dst_reg(this, glsl_type::uvec4_type
);
1280 dst_reg header1_w
= header1
;
1281 header1_w
.writemask
= WRITEMASK_W
;
1283 emit(MOV(header1
, 0u));
1285 if (prog_data
->vue_map
.slots_valid
& VARYING_BIT_PSIZ
) {
1286 src_reg psiz
= src_reg(output_reg
[VARYING_SLOT_PSIZ
]);
1288 current_annotation
= "Point size";
1289 emit(MUL(header1_w
, psiz
, src_reg((float)(1 << 11))));
1290 emit(AND(header1_w
, src_reg(header1_w
), 0x7ff << 8));
1293 if (output_reg
[VARYING_SLOT_CLIP_DIST0
].file
!= BAD_FILE
) {
1294 current_annotation
= "Clipping flags";
1295 dst_reg flags0
= dst_reg(this, glsl_type::uint_type
);
1296 dst_reg flags1
= dst_reg(this, glsl_type::uint_type
);
1298 emit(CMP(dst_null_f(), src_reg(output_reg
[VARYING_SLOT_CLIP_DIST0
]), src_reg(0.0f
), BRW_CONDITIONAL_L
));
1299 emit(VS_OPCODE_UNPACK_FLAGS_SIMD4X2
, flags0
, src_reg(0));
1300 emit(OR(header1_w
, src_reg(header1_w
), src_reg(flags0
)));
1302 emit(CMP(dst_null_f(), src_reg(output_reg
[VARYING_SLOT_CLIP_DIST1
]), src_reg(0.0f
), BRW_CONDITIONAL_L
));
1303 emit(VS_OPCODE_UNPACK_FLAGS_SIMD4X2
, flags1
, src_reg(0));
1304 emit(SHL(flags1
, src_reg(flags1
), src_reg(4)));
1305 emit(OR(header1_w
, src_reg(header1_w
), src_reg(flags1
)));
1308 /* i965 clipping workaround:
1309 * 1) Test for -ve rhw
1311 * set ndc = (0,0,0,0)
1314 * Later, clipping will detect ucp[6] and ensure the primitive is
1315 * clipped against all fixed planes.
1317 if (devinfo
->has_negative_rhw_bug
) {
1318 src_reg ndc_w
= src_reg(output_reg
[BRW_VARYING_SLOT_NDC
]);
1319 ndc_w
.swizzle
= BRW_SWIZZLE_WWWW
;
1320 emit(CMP(dst_null_f(), ndc_w
, src_reg(0.0f
), BRW_CONDITIONAL_L
));
1321 vec4_instruction
*inst
;
1322 inst
= emit(OR(header1_w
, src_reg(header1_w
), src_reg(1u << 6)));
1323 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1324 output_reg
[BRW_VARYING_SLOT_NDC
].type
= BRW_REGISTER_TYPE_F
;
1325 inst
= emit(MOV(output_reg
[BRW_VARYING_SLOT_NDC
], src_reg(0.0f
)));
1326 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1329 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_UD
), src_reg(header1
)));
1330 } else if (devinfo
->gen
< 6) {
1331 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_UD
), 0u));
1333 emit(MOV(retype(reg
, BRW_REGISTER_TYPE_D
), src_reg(0)));
1334 if (prog_data
->vue_map
.slots_valid
& VARYING_BIT_PSIZ
) {
1335 dst_reg reg_w
= reg
;
1336 reg_w
.writemask
= WRITEMASK_W
;
1337 src_reg reg_as_src
= src_reg(output_reg
[VARYING_SLOT_PSIZ
]);
1338 reg_as_src
.type
= reg_w
.type
;
1339 reg_as_src
.swizzle
= brw_swizzle_for_size(1);
1340 emit(MOV(reg_w
, reg_as_src
));
1342 if (prog_data
->vue_map
.slots_valid
& VARYING_BIT_LAYER
) {
1343 dst_reg reg_y
= reg
;
1344 reg_y
.writemask
= WRITEMASK_Y
;
1345 reg_y
.type
= BRW_REGISTER_TYPE_D
;
1346 output_reg
[VARYING_SLOT_LAYER
].type
= reg_y
.type
;
1347 emit(MOV(reg_y
, src_reg(output_reg
[VARYING_SLOT_LAYER
])));
1349 if (prog_data
->vue_map
.slots_valid
& VARYING_BIT_VIEWPORT
) {
1350 dst_reg reg_z
= reg
;
1351 reg_z
.writemask
= WRITEMASK_Z
;
1352 reg_z
.type
= BRW_REGISTER_TYPE_D
;
1353 output_reg
[VARYING_SLOT_VIEWPORT
].type
= reg_z
.type
;
1354 emit(MOV(reg_z
, src_reg(output_reg
[VARYING_SLOT_VIEWPORT
])));
1360 vec4_visitor::emit_generic_urb_slot(dst_reg reg
, int varying
)
1362 assert(varying
< VARYING_SLOT_MAX
);
1363 assert(output_reg
[varying
].type
== reg
.type
);
1364 current_annotation
= output_reg_annotation
[varying
];
1365 /* Copy the register, saturating if necessary */
1366 return emit(MOV(reg
, src_reg(output_reg
[varying
])));
1370 vec4_visitor::emit_urb_slot(dst_reg reg
, int varying
)
1372 reg
.type
= BRW_REGISTER_TYPE_F
;
1373 output_reg
[varying
].type
= reg
.type
;
1376 case VARYING_SLOT_PSIZ
:
1378 /* PSIZ is always in slot 0, and is coupled with other flags. */
1379 current_annotation
= "indices, point width, clip flags";
1380 emit_psiz_and_flags(reg
);
1383 case BRW_VARYING_SLOT_NDC
:
1384 current_annotation
= "NDC";
1385 emit(MOV(reg
, src_reg(output_reg
[BRW_VARYING_SLOT_NDC
])));
1387 case VARYING_SLOT_POS
:
1388 current_annotation
= "gl_Position";
1389 emit(MOV(reg
, src_reg(output_reg
[VARYING_SLOT_POS
])));
1391 case VARYING_SLOT_EDGE
:
1392 /* This is present when doing unfilled polygons. We're supposed to copy
1393 * the edge flag from the user-provided vertex array
1394 * (glEdgeFlagPointer), or otherwise we'll copy from the current value
1395 * of that attribute (starts as 1.0f). This is then used in clipping to
1396 * determine which edges should be drawn as wireframe.
1398 current_annotation
= "edge flag";
1399 emit(MOV(reg
, src_reg(dst_reg(ATTR
, VERT_ATTRIB_EDGEFLAG
,
1400 glsl_type::float_type
, WRITEMASK_XYZW
))));
1402 case BRW_VARYING_SLOT_PAD
:
1403 /* No need to write to this slot */
1406 emit_generic_urb_slot(reg
, varying
);
1412 align_interleaved_urb_mlen(const struct brw_device_info
*devinfo
, int mlen
)
1414 if (devinfo
->gen
>= 6) {
1415 /* URB data written (does not include the message header reg) must
1416 * be a multiple of 256 bits, or 2 VS registers. See vol5c.5,
1417 * section 5.4.3.2.2: URB_INTERLEAVED.
1419 * URB entries are allocated on a multiple of 1024 bits, so an
1420 * extra 128 bits written here to make the end align to 256 is
1423 if ((mlen
% 2) != 1)
1432 * Generates the VUE payload plus the necessary URB write instructions to
1435 * The VUE layout is documented in Volume 2a.
1438 vec4_visitor::emit_vertex()
1440 /* MRF 0 is reserved for the debugger, so start with message header
1445 /* In the process of generating our URB write message contents, we
1446 * may need to unspill a register or load from an array. Those
1447 * reads would use MRFs 14-15.
1449 int max_usable_mrf
= FIRST_SPILL_MRF(devinfo
->gen
);
1451 /* The following assertion verifies that max_usable_mrf causes an
1452 * even-numbered amount of URB write data, which will meet gen6's
1453 * requirements for length alignment.
1455 assert ((max_usable_mrf
- base_mrf
) % 2 == 0);
1457 /* First mrf is the g0-based message header containing URB handles and
1460 emit_urb_write_header(mrf
++);
1462 if (devinfo
->gen
< 6) {
1463 emit_ndc_computation();
1466 /* We may need to split this up into several URB writes, so do them in a
1470 bool complete
= false;
1472 /* URB offset is in URB row increments, and each of our MRFs is half of
1473 * one of those, since we're doing interleaved writes.
1475 int offset
= slot
/ 2;
1478 for (; slot
< prog_data
->vue_map
.num_slots
; ++slot
) {
1479 emit_urb_slot(dst_reg(MRF
, mrf
++),
1480 prog_data
->vue_map
.slot_to_varying
[slot
]);
1482 /* If this was max_usable_mrf, we can't fit anything more into this
1483 * URB WRITE. Same thing if we reached the maximum length available.
1485 if (mrf
> max_usable_mrf
||
1486 align_interleaved_urb_mlen(devinfo
, mrf
- base_mrf
+ 1) > BRW_MAX_MSG_LENGTH
) {
1492 complete
= slot
>= prog_data
->vue_map
.num_slots
;
1493 current_annotation
= "URB write";
1494 vec4_instruction
*inst
= emit_urb_write_opcode(complete
);
1495 inst
->base_mrf
= base_mrf
;
1496 inst
->mlen
= align_interleaved_urb_mlen(devinfo
, mrf
- base_mrf
);
1497 inst
->offset
+= offset
;
1503 vec4_visitor::get_scratch_offset(bblock_t
*block
, vec4_instruction
*inst
,
1504 src_reg
*reladdr
, int reg_offset
)
1506 /* Because we store the values to scratch interleaved like our
1507 * vertex data, we need to scale the vec4 index by 2.
1509 int message_header_scale
= 2;
1511 /* Pre-gen6, the message header uses byte offsets instead of vec4
1512 * (16-byte) offset units.
1514 if (devinfo
->gen
< 6)
1515 message_header_scale
*= 16;
1518 src_reg index
= src_reg(this, glsl_type::int_type
);
1520 emit_before(block
, inst
, ADD(dst_reg(index
), *reladdr
,
1521 src_reg(reg_offset
)));
1522 emit_before(block
, inst
, MUL(dst_reg(index
), index
,
1523 src_reg(message_header_scale
)));
1527 return src_reg(reg_offset
* message_header_scale
);
1532 vec4_visitor::get_pull_constant_offset(bblock_t
* block
, vec4_instruction
*inst
,
1533 src_reg
*reladdr
, int reg_offset
)
1536 src_reg index
= src_reg(this, glsl_type::int_type
);
1538 emit_before(block
, inst
, ADD(dst_reg(index
), *reladdr
,
1539 src_reg(reg_offset
)));
1541 /* Pre-gen6, the message header uses byte offsets instead of vec4
1542 * (16-byte) offset units.
1544 if (devinfo
->gen
< 6) {
1545 emit_before(block
, inst
, MUL(dst_reg(index
), index
, src_reg(16)));
1549 } else if (devinfo
->gen
>= 8) {
1550 /* Store the offset in a GRF so we can send-from-GRF. */
1551 src_reg offset
= src_reg(this, glsl_type::int_type
);
1552 emit_before(block
, inst
, MOV(dst_reg(offset
), src_reg(reg_offset
)));
1555 int message_header_scale
= devinfo
->gen
< 6 ? 16 : 1;
1556 return src_reg(reg_offset
* message_header_scale
);
1561 * Emits an instruction before @inst to load the value named by @orig_src
1562 * from scratch space at @base_offset to @temp.
1564 * @base_offset is measured in 32-byte units (the size of a register).
1567 vec4_visitor::emit_scratch_read(bblock_t
*block
, vec4_instruction
*inst
,
1568 dst_reg temp
, src_reg orig_src
,
1571 int reg_offset
= base_offset
+ orig_src
.reg_offset
;
1572 src_reg index
= get_scratch_offset(block
, inst
, orig_src
.reladdr
,
1575 emit_before(block
, inst
, SCRATCH_READ(temp
, index
));
1579 * Emits an instruction after @inst to store the value to be written
1580 * to @orig_dst to scratch space at @base_offset, from @temp.
1582 * @base_offset is measured in 32-byte units (the size of a register).
1585 vec4_visitor::emit_scratch_write(bblock_t
*block
, vec4_instruction
*inst
,
1588 int reg_offset
= base_offset
+ inst
->dst
.reg_offset
;
1589 src_reg index
= get_scratch_offset(block
, inst
, inst
->dst
.reladdr
,
1592 /* Create a temporary register to store *inst's result in.
1594 * We have to be careful in MOVing from our temporary result register in
1595 * the scratch write. If we swizzle from channels of the temporary that
1596 * weren't initialized, it will confuse live interval analysis, which will
1597 * make spilling fail to make progress.
1599 const src_reg temp
= swizzle(retype(src_reg(this, glsl_type::vec4_type
),
1601 brw_swizzle_for_mask(inst
->dst
.writemask
));
1602 dst_reg dst
= dst_reg(brw_writemask(brw_vec8_grf(0, 0),
1603 inst
->dst
.writemask
));
1604 vec4_instruction
*write
= SCRATCH_WRITE(dst
, temp
, index
);
1605 if (inst
->opcode
!= BRW_OPCODE_SEL
)
1606 write
->predicate
= inst
->predicate
;
1607 write
->ir
= inst
->ir
;
1608 write
->annotation
= inst
->annotation
;
1609 inst
->insert_after(block
, write
);
1611 inst
->dst
.file
= temp
.file
;
1612 inst
->dst
.reg
= temp
.reg
;
1613 inst
->dst
.reg_offset
= temp
.reg_offset
;
1614 inst
->dst
.reladdr
= NULL
;
1618 * Checks if \p src and/or \p src.reladdr require a scratch read, and if so,
1619 * adds the scratch read(s) before \p inst. The function also checks for
1620 * recursive reladdr scratch accesses, issuing the corresponding scratch
1621 * loads and rewriting reladdr references accordingly.
1623 * \return \p src if it did not require a scratch load, otherwise, the
1624 * register holding the result of the scratch load that the caller should
1625 * use to rewrite src.
1628 vec4_visitor::emit_resolve_reladdr(int scratch_loc
[], bblock_t
*block
,
1629 vec4_instruction
*inst
, src_reg src
)
1631 /* Resolve recursive reladdr scratch access by calling ourselves
1635 *src
.reladdr
= emit_resolve_reladdr(scratch_loc
, block
, inst
,
1638 /* Now handle scratch access on src */
1639 if (src
.file
== GRF
&& scratch_loc
[src
.reg
] != -1) {
1640 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
1641 emit_scratch_read(block
, inst
, temp
, src
, scratch_loc
[src
.reg
]);
1643 src
.reg_offset
= temp
.reg_offset
;
1651 * We can't generally support array access in GRF space, because a
1652 * single instruction's destination can only span 2 contiguous
1653 * registers. So, we send all GRF arrays that get variable index
1654 * access to scratch space.
1657 vec4_visitor::move_grf_array_access_to_scratch()
1659 int scratch_loc
[this->alloc
.count
];
1660 memset(scratch_loc
, -1, sizeof(scratch_loc
));
1662 /* First, calculate the set of virtual GRFs that need to be punted
1663 * to scratch due to having any array access on them, and where in
1666 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
1667 if (inst
->dst
.file
== GRF
&& inst
->dst
.reladdr
) {
1668 if (scratch_loc
[inst
->dst
.reg
] == -1) {
1669 scratch_loc
[inst
->dst
.reg
] = last_scratch
;
1670 last_scratch
+= this->alloc
.sizes
[inst
->dst
.reg
];
1673 for (src_reg
*iter
= inst
->dst
.reladdr
;
1675 iter
= iter
->reladdr
) {
1676 if (iter
->file
== GRF
&& scratch_loc
[iter
->reg
] == -1) {
1677 scratch_loc
[iter
->reg
] = last_scratch
;
1678 last_scratch
+= this->alloc
.sizes
[iter
->reg
];
1683 for (int i
= 0 ; i
< 3; i
++) {
1684 for (src_reg
*iter
= &inst
->src
[i
];
1686 iter
= iter
->reladdr
) {
1687 if (iter
->file
== GRF
&& scratch_loc
[iter
->reg
] == -1) {
1688 scratch_loc
[iter
->reg
] = last_scratch
;
1689 last_scratch
+= this->alloc
.sizes
[iter
->reg
];
1695 /* Now, for anything that will be accessed through scratch, rewrite
1696 * it to load/store. Note that this is a _safe list walk, because
1697 * we may generate a new scratch_write instruction after the one
1700 foreach_block_and_inst_safe(block
, vec4_instruction
, inst
, cfg
) {
1701 /* Set up the annotation tracking for new generated instructions. */
1703 current_annotation
= inst
->annotation
;
1705 /* First handle scratch access on the dst. Notice we have to handle
1706 * the case where the dst's reladdr also points to scratch space.
1708 if (inst
->dst
.reladdr
)
1709 *inst
->dst
.reladdr
= emit_resolve_reladdr(scratch_loc
, block
, inst
,
1710 *inst
->dst
.reladdr
);
1712 /* Now that we have handled any (possibly recursive) reladdr scratch
1713 * accesses for dst we can safely do the scratch write for dst itself
1715 if (inst
->dst
.file
== GRF
&& scratch_loc
[inst
->dst
.reg
] != -1)
1716 emit_scratch_write(block
, inst
, scratch_loc
[inst
->dst
.reg
]);
1718 /* Now handle scratch access on any src. In this case, since inst->src[i]
1719 * already is a src_reg, we can just call emit_resolve_reladdr with
1720 * inst->src[i] and it will take care of handling scratch loads for
1721 * both src and src.reladdr (recursively).
1723 for (int i
= 0 ; i
< 3; i
++) {
1724 inst
->src
[i
] = emit_resolve_reladdr(scratch_loc
, block
, inst
,
1731 * Emits an instruction before @inst to load the value named by @orig_src
1732 * from the pull constant buffer (surface) at @base_offset to @temp.
1735 vec4_visitor::emit_pull_constant_load(bblock_t
*block
, vec4_instruction
*inst
,
1736 dst_reg temp
, src_reg orig_src
,
1739 int reg_offset
= base_offset
+ orig_src
.reg_offset
;
1740 src_reg index
= src_reg(prog_data
->base
.binding_table
.pull_constants_start
);
1741 src_reg offset
= get_pull_constant_offset(block
, inst
, orig_src
.reladdr
,
1744 emit_pull_constant_load_reg(temp
,
1751 * Implements array access of uniforms by inserting a
1752 * PULL_CONSTANT_LOAD instruction.
1754 * Unlike temporary GRF array access (where we don't support it due to
1755 * the difficulty of doing relative addressing on instruction
1756 * destinations), we could potentially do array access of uniforms
1757 * that were loaded in GRF space as push constants. In real-world
1758 * usage we've seen, though, the arrays being used are always larger
1759 * than we could load as push constants, so just always move all
1760 * uniform array access out to a pull constant buffer.
1763 vec4_visitor::move_uniform_array_access_to_pull_constants()
1765 int pull_constant_loc
[this->uniforms
];
1766 memset(pull_constant_loc
, -1, sizeof(pull_constant_loc
));
1767 bool nested_reladdr
;
1769 /* Walk through and find array access of uniforms. Put a copy of that
1770 * uniform in the pull constant buffer.
1772 * Note that we don't move constant-indexed accesses to arrays. No
1773 * testing has been done of the performance impact of this choice.
1776 nested_reladdr
= false;
1778 foreach_block_and_inst_safe(block
, vec4_instruction
, inst
, cfg
) {
1779 for (int i
= 0 ; i
< 3; i
++) {
1780 if (inst
->src
[i
].file
!= UNIFORM
|| !inst
->src
[i
].reladdr
)
1783 int uniform
= inst
->src
[i
].reg
;
1785 if (inst
->src
[i
].reladdr
->reladdr
)
1786 nested_reladdr
= true; /* will need another pass */
1788 /* If this array isn't already present in the pull constant buffer,
1791 if (pull_constant_loc
[uniform
] == -1) {
1792 const gl_constant_value
**values
=
1793 &stage_prog_data
->param
[uniform
* 4];
1795 pull_constant_loc
[uniform
] = stage_prog_data
->nr_pull_params
/ 4;
1797 assert(uniform
< uniform_array_size
);
1798 for (int j
= 0; j
< uniform_size
[uniform
] * 4; j
++) {
1799 stage_prog_data
->pull_param
[stage_prog_data
->nr_pull_params
++]
1804 /* Set up the annotation tracking for new generated instructions. */
1806 current_annotation
= inst
->annotation
;
1808 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
1810 emit_pull_constant_load(block
, inst
, temp
, inst
->src
[i
],
1811 pull_constant_loc
[uniform
]);
1813 inst
->src
[i
].file
= temp
.file
;
1814 inst
->src
[i
].reg
= temp
.reg
;
1815 inst
->src
[i
].reg_offset
= temp
.reg_offset
;
1816 inst
->src
[i
].reladdr
= NULL
;
1819 } while (nested_reladdr
);
1821 /* Now there are no accesses of the UNIFORM file with a reladdr, so
1822 * no need to track them as larger-than-vec4 objects. This will be
1823 * relied on in cutting out unused uniform vectors from push
1826 split_uniform_registers();
1830 vec4_visitor::resolve_ud_negate(src_reg
*reg
)
1832 if (reg
->type
!= BRW_REGISTER_TYPE_UD
||
1836 src_reg temp
= src_reg(this, glsl_type::uvec4_type
);
1837 emit(BRW_OPCODE_MOV
, dst_reg(temp
), *reg
);
1841 vec4_visitor::vec4_visitor(const struct brw_compiler
*compiler
,
1843 struct gl_program
*prog
,
1844 const struct brw_sampler_prog_key_data
*key_tex
,
1845 struct brw_vue_prog_data
*prog_data
,
1846 struct gl_shader_program
*shader_prog
,
1847 gl_shader_stage stage
,
1850 int shader_time_index
)
1851 : backend_shader(compiler
, log_data
, mem_ctx
,
1852 shader_prog
, prog
, &prog_data
->base
, stage
),
1854 prog_data(prog_data
),
1855 sanity_param_count(0),
1857 first_non_payload_grf(0),
1858 need_all_constants_in_pull_buffer(false),
1859 no_spills(no_spills
),
1860 shader_time_index(shader_time_index
),
1863 this->failed
= false;
1865 this->base_ir
= NULL
;
1866 this->current_annotation
= NULL
;
1867 memset(this->output_reg_annotation
, 0, sizeof(this->output_reg_annotation
));
1869 this->virtual_grf_start
= NULL
;
1870 this->virtual_grf_end
= NULL
;
1871 this->live_intervals
= NULL
;
1873 this->max_grf
= devinfo
->gen
>= 7 ? GEN7_MRF_HACK_START
: BRW_MAX_GRF
;
1877 /* Initialize uniform_array_size to at least 1 because pre-gen6 VS requires
1878 * at least one. See setup_uniforms() in brw_vec4.cpp.
1880 this->uniform_array_size
= 1;
1882 this->uniform_array_size
=
1883 MAX2(DIV_ROUND_UP(stage_prog_data
->nr_params
, 4), 1);
1886 this->uniform_size
= rzalloc_array(mem_ctx
, int, this->uniform_array_size
);
1889 vec4_visitor::~vec4_visitor()
1895 vec4_visitor::fail(const char *format
, ...)
1905 va_start(va
, format
);
1906 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
1908 msg
= ralloc_asprintf(mem_ctx
, "%s compile failed: %s\n", stage_abbrev
, msg
);
1910 this->fail_msg
= msg
;
1912 if (debug_enabled
) {
1913 fprintf(stderr
, "%s", msg
);
1917 } /* namespace brw */