2 * Copyright 2020 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "si_shader_internal.h"
29 LLVMValueRef
si_get_sample_id(struct si_shader_context
*ctx
)
31 return si_unpack_param(ctx
, ctx
->args
.ancillary
, 8, 4);
34 static LLVMValueRef
load_sample_mask_in(struct ac_shader_abi
*abi
)
36 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
37 return ac_to_integer(&ctx
->ac
, ac_get_arg(&ctx
->ac
, ctx
->args
.sample_coverage
));
40 static LLVMValueRef
load_sample_position(struct ac_shader_abi
*abi
, LLVMValueRef sample_id
)
42 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
43 LLVMValueRef desc
= ac_get_arg(&ctx
->ac
, ctx
->rw_buffers
);
44 LLVMValueRef buf_index
= LLVMConstInt(ctx
->ac
.i32
, SI_PS_CONST_SAMPLE_POSITIONS
, 0);
45 LLVMValueRef resource
= ac_build_load_to_sgpr(&ctx
->ac
, desc
, buf_index
);
47 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
48 LLVMValueRef offset0
= LLVMBuildMul(ctx
->ac
.builder
, sample_id
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
49 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->ac
.builder
, offset0
, LLVMConstInt(ctx
->ac
.i32
, 4, 0), "");
51 LLVMValueRef pos
[4] = {
52 si_buffer_load_const(ctx
, resource
, offset0
),
53 si_buffer_load_const(ctx
, resource
, offset1
),
54 LLVMConstReal(ctx
->ac
.f32
, 0),
55 LLVMConstReal(ctx
->ac
.f32
, 0)
58 return ac_build_gather_values(&ctx
->ac
, pos
, 4);
61 static LLVMValueRef
si_nir_emit_fbfetch(struct ac_shader_abi
*abi
)
63 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
64 struct ac_image_args args
= {};
65 LLVMValueRef ptr
, image
, fmask
;
67 /* Ignore src0, because KHR_blend_func_extended disallows multiple render
71 /* Load the image descriptor. */
72 STATIC_ASSERT(SI_PS_IMAGE_COLORBUF0
% 2 == 0);
73 ptr
= ac_get_arg(&ctx
->ac
, ctx
->rw_buffers
);
74 ptr
= LLVMBuildPointerCast(ctx
->ac
.builder
, ptr
,
75 ac_array_in_const32_addr_space(ctx
->ac
.v8i32
), "");
76 image
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
77 LLVMConstInt(ctx
->ac
.i32
, SI_PS_IMAGE_COLORBUF0
/ 2, 0));
81 args
.coords
[chan
++] = si_unpack_param(ctx
, ctx
->pos_fixed_pt
, 0, 16);
83 if (!ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
84 args
.coords
[chan
++] = si_unpack_param(ctx
, ctx
->pos_fixed_pt
, 16, 16);
86 /* Get the current render target layer index. */
87 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
)
88 args
.coords
[chan
++] = si_unpack_param(ctx
, ctx
->args
.ancillary
, 16, 11);
90 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
91 args
.coords
[chan
++] = si_get_sample_id(ctx
);
93 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
&&
94 !(ctx
->screen
->debug_flags
& DBG(NO_FMASK
))) {
95 fmask
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
96 LLVMConstInt(ctx
->ac
.i32
, SI_PS_IMAGE_COLORBUF0_FMASK
/ 2, 0));
98 ac_apply_fmask_to_sample(&ctx
->ac
, fmask
, args
.coords
,
99 ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
);
102 args
.opcode
= ac_image_load
;
103 args
.resource
= image
;
105 args
.attributes
= AC_FUNC_ATTR_READNONE
;
107 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
108 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
109 ac_image_2darraymsaa
: ac_image_2dmsaa
;
110 else if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
111 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
112 ac_image_1darray
: ac_image_1d
;
114 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
115 ac_image_2darray
: ac_image_2d
;
117 return ac_build_image_opcode(&ctx
->ac
, &args
);
120 static LLVMValueRef
si_build_fs_interp(struct si_shader_context
*ctx
,
121 unsigned attr_index
, unsigned chan
,
122 LLVMValueRef prim_mask
,
123 LLVMValueRef i
, LLVMValueRef j
)
126 return ac_build_fs_interp(&ctx
->ac
,
127 LLVMConstInt(ctx
->ac
.i32
, chan
, 0),
128 LLVMConstInt(ctx
->ac
.i32
, attr_index
, 0),
131 return ac_build_fs_interp_mov(&ctx
->ac
,
132 LLVMConstInt(ctx
->ac
.i32
, 2, 0), /* P0 */
133 LLVMConstInt(ctx
->ac
.i32
, chan
, 0),
134 LLVMConstInt(ctx
->ac
.i32
, attr_index
, 0),
139 * Interpolate a fragment shader input.
142 * @param input_index index of the input in hardware
143 * @param semantic_name TGSI_SEMANTIC_*
144 * @param semantic_index semantic index
145 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
146 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
147 * @param interp_param interpolation weights (i,j)
148 * @param prim_mask SI_PARAM_PRIM_MASK
149 * @param face SI_PARAM_FRONT_FACE
150 * @param result the return value (4 components)
152 static void interp_fs_color(struct si_shader_context
*ctx
,
153 unsigned input_index
,
154 unsigned semantic_index
,
155 unsigned num_interp_inputs
,
156 unsigned colors_read_mask
,
157 LLVMValueRef interp_param
,
158 LLVMValueRef prim_mask
,
160 LLVMValueRef result
[4])
162 LLVMValueRef i
= NULL
, j
= NULL
;
165 /* fs.constant returns the param from the middle vertex, so it's not
166 * really useful for flat shading. It's meant to be used for custom
167 * interpolation (but the intrinsic can't fetch from the other two
170 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
171 * to do the right thing. The only reason we use fs.constant is that
172 * fs.interp cannot be used on integers, because they can be equal
175 * When interp is false we will use fs.constant or for newer llvm,
178 bool interp
= interp_param
!= NULL
;
181 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
,
182 LLVMVectorType(ctx
->ac
.f32
, 2), "");
184 i
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
186 j
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
190 if (ctx
->shader
->key
.part
.ps
.prolog
.color_two_side
) {
191 LLVMValueRef is_face_positive
;
193 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
194 * otherwise it's at offset "num_inputs".
196 unsigned back_attr_offset
= num_interp_inputs
;
197 if (semantic_index
== 1 && colors_read_mask
& 0xf)
198 back_attr_offset
+= 1;
200 is_face_positive
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
201 face
, ctx
->ac
.i32_0
, "");
203 for (chan
= 0; chan
< 4; chan
++) {
204 LLVMValueRef front
, back
;
206 front
= si_build_fs_interp(ctx
,
209 back
= si_build_fs_interp(ctx
,
210 back_attr_offset
, chan
,
213 result
[chan
] = LLVMBuildSelect(ctx
->ac
.builder
,
220 for (chan
= 0; chan
< 4; chan
++) {
221 result
[chan
] = si_build_fs_interp(ctx
,
228 static void si_alpha_test(struct si_shader_context
*ctx
, LLVMValueRef alpha
)
230 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
) {
231 static LLVMRealPredicate cond_map
[PIPE_FUNC_ALWAYS
+ 1] = {
232 [PIPE_FUNC_LESS
] = LLVMRealOLT
,
233 [PIPE_FUNC_EQUAL
] = LLVMRealOEQ
,
234 [PIPE_FUNC_LEQUAL
] = LLVMRealOLE
,
235 [PIPE_FUNC_GREATER
] = LLVMRealOGT
,
236 [PIPE_FUNC_NOTEQUAL
] = LLVMRealONE
,
237 [PIPE_FUNC_GEQUAL
] = LLVMRealOGE
,
239 LLVMRealPredicate cond
= cond_map
[ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
];
242 LLVMValueRef alpha_ref
= LLVMGetParam(ctx
->main_fn
,
244 LLVMValueRef alpha_pass
=
245 LLVMBuildFCmp(ctx
->ac
.builder
, cond
, alpha
, alpha_ref
, "");
246 ac_build_kill_if_false(&ctx
->ac
, alpha_pass
);
248 ac_build_kill_if_false(&ctx
->ac
, ctx
->ac
.i1false
);
252 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct si_shader_context
*ctx
,
254 unsigned samplemask_param
)
256 LLVMValueRef coverage
;
258 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
259 coverage
= LLVMGetParam(ctx
->main_fn
,
261 coverage
= ac_to_integer(&ctx
->ac
, coverage
);
263 coverage
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32",
265 &coverage
, 1, AC_FUNC_ATTR_READNONE
);
267 coverage
= LLVMBuildUIToFP(ctx
->ac
.builder
, coverage
,
270 coverage
= LLVMBuildFMul(ctx
->ac
.builder
, coverage
,
271 LLVMConstReal(ctx
->ac
.f32
,
272 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
274 return LLVMBuildFMul(ctx
->ac
.builder
, alpha
, coverage
, "");
277 struct si_ps_exports
{
279 struct ac_export_args args
[10];
282 static void si_export_mrt_z(struct si_shader_context
*ctx
,
283 LLVMValueRef depth
, LLVMValueRef stencil
,
284 LLVMValueRef samplemask
, struct si_ps_exports
*exp
)
286 struct ac_export_args args
;
288 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
290 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
293 /* Initialize arguments for the shader export intrinsic */
294 static void si_llvm_init_ps_export_args(struct si_shader_context
*ctx
,
295 LLVMValueRef
*values
,
297 struct ac_export_args
*args
)
299 const struct si_shader_key
*key
= &ctx
->shader
->key
;
300 unsigned col_formats
= key
->part
.ps
.epilog
.spi_shader_col_format
;
301 LLVMValueRef f32undef
= LLVMGetUndef(ctx
->ac
.f32
);
302 unsigned spi_shader_col_format
;
304 bool is_int8
, is_int10
;
305 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
307 assert(cbuf
>= 0 && cbuf
< 8);
309 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
310 is_int8
= (key
->part
.ps
.epilog
.color_is_int8
>> cbuf
) & 0x1;
311 is_int10
= (key
->part
.ps
.epilog
.color_is_int10
>> cbuf
) & 0x1;
313 /* Default is 0xf. Adjusted below depending on the format. */
314 args
->enabled_channels
= 0xf; /* writemask */
316 /* Specify whether the EXEC mask represents the valid mask */
317 args
->valid_mask
= 0;
319 /* Specify whether this is the last export */
322 /* Specify the target we are exporting */
323 args
->target
= target
;
326 args
->out
[0] = f32undef
;
327 args
->out
[1] = f32undef
;
328 args
->out
[2] = f32undef
;
329 args
->out
[3] = f32undef
;
331 LLVMValueRef (*packf
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2]) = NULL
;
332 LLVMValueRef (*packi
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2],
333 unsigned bits
, bool hi
) = NULL
;
335 switch (spi_shader_col_format
) {
336 case V_028714_SPI_SHADER_ZERO
:
337 args
->enabled_channels
= 0; /* writemask */
338 args
->target
= V_008DFC_SQ_EXP_NULL
;
341 case V_028714_SPI_SHADER_32_R
:
342 args
->enabled_channels
= 1; /* writemask */
343 args
->out
[0] = values
[0];
346 case V_028714_SPI_SHADER_32_GR
:
347 args
->enabled_channels
= 0x3; /* writemask */
348 args
->out
[0] = values
[0];
349 args
->out
[1] = values
[1];
352 case V_028714_SPI_SHADER_32_AR
:
353 if (ctx
->screen
->info
.chip_class
>= GFX10
) {
354 args
->enabled_channels
= 0x3; /* writemask */
355 args
->out
[0] = values
[0];
356 args
->out
[1] = values
[3];
358 args
->enabled_channels
= 0x9; /* writemask */
359 args
->out
[0] = values
[0];
360 args
->out
[3] = values
[3];
364 case V_028714_SPI_SHADER_FP16_ABGR
:
365 packf
= ac_build_cvt_pkrtz_f16
;
368 case V_028714_SPI_SHADER_UNORM16_ABGR
:
369 packf
= ac_build_cvt_pknorm_u16
;
372 case V_028714_SPI_SHADER_SNORM16_ABGR
:
373 packf
= ac_build_cvt_pknorm_i16
;
376 case V_028714_SPI_SHADER_UINT16_ABGR
:
377 packi
= ac_build_cvt_pk_u16
;
380 case V_028714_SPI_SHADER_SINT16_ABGR
:
381 packi
= ac_build_cvt_pk_i16
;
384 case V_028714_SPI_SHADER_32_ABGR
:
385 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
389 /* Pack f16 or norm_i16/u16. */
391 for (chan
= 0; chan
< 2; chan
++) {
392 LLVMValueRef pack_args
[2] = {
398 packed
= packf(&ctx
->ac
, pack_args
);
399 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
401 args
->compr
= 1; /* COMPR flag */
405 for (chan
= 0; chan
< 2; chan
++) {
406 LLVMValueRef pack_args
[2] = {
407 ac_to_integer(&ctx
->ac
, values
[2 * chan
]),
408 ac_to_integer(&ctx
->ac
, values
[2 * chan
+ 1])
412 packed
= packi(&ctx
->ac
, pack_args
,
413 is_int8
? 8 : is_int10
? 10 : 16,
415 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
417 args
->compr
= 1; /* COMPR flag */
421 static void si_export_mrt_color(struct si_shader_context
*ctx
,
422 LLVMValueRef
*color
, unsigned index
,
423 unsigned samplemask_param
,
424 bool is_last
, struct si_ps_exports
*exp
)
429 if (ctx
->shader
->key
.part
.ps
.epilog
.clamp_color
)
430 for (i
= 0; i
< 4; i
++)
431 color
[i
] = ac_build_clamp(&ctx
->ac
, color
[i
]);
434 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_to_one
)
435 color
[3] = ctx
->ac
.f32_1
;
439 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
)
440 si_alpha_test(ctx
, color
[3]);
442 /* Line & polygon smoothing */
443 if (ctx
->shader
->key
.part
.ps
.epilog
.poly_line_smoothing
)
444 color
[3] = si_scale_alpha_by_sample_mask(ctx
, color
[3],
447 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
448 if (ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
> 0) {
449 struct ac_export_args args
[8];
452 /* Get the export arguments, also find out what the last one is. */
453 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
454 si_llvm_init_ps_export_args(ctx
, color
,
455 V_008DFC_SQ_EXP_MRT
+ c
, &args
[c
]);
456 if (args
[c
].enabled_channels
)
460 /* Emit all exports. */
461 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
462 if (is_last
&& last
== c
) {
463 args
[c
].valid_mask
= 1; /* whether the EXEC mask is valid */
464 args
[c
].done
= 1; /* DONE bit */
465 } else if (!args
[c
].enabled_channels
)
466 continue; /* unnecessary NULL export */
468 memcpy(&exp
->args
[exp
->num
++], &args
[c
], sizeof(args
[c
]));
471 struct ac_export_args args
;
474 si_llvm_init_ps_export_args(ctx
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
477 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
478 args
.done
= 1; /* DONE bit */
479 } else if (!args
.enabled_channels
)
480 return; /* unnecessary NULL export */
482 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
486 static void si_emit_ps_exports(struct si_shader_context
*ctx
,
487 struct si_ps_exports
*exp
)
489 for (unsigned i
= 0; i
< exp
->num
; i
++)
490 ac_build_export(&ctx
->ac
, &exp
->args
[i
]);
494 * Return PS outputs in this order:
496 * v[0:3] = color0.xyzw
497 * v[4:7] = color1.xyzw
502 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
504 * The alpha-ref SGPR is returned via its original location.
506 static void si_llvm_return_fs_outputs(struct ac_shader_abi
*abi
,
507 unsigned max_outputs
,
510 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
511 struct si_shader
*shader
= ctx
->shader
;
512 struct si_shader_info
*info
= &shader
->selector
->info
;
513 LLVMBuilderRef builder
= ctx
->ac
.builder
;
514 unsigned i
, j
, first_vgpr
, vgpr
;
516 LLVMValueRef color
[8][4] = {};
517 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
520 if (ctx
->postponed_kill
)
521 ac_build_kill_if_false(&ctx
->ac
, LLVMBuildLoad(builder
, ctx
->postponed_kill
, ""));
523 /* Read the output values. */
524 for (i
= 0; i
< info
->num_outputs
; i
++) {
525 unsigned semantic_name
= info
->output_semantic_name
[i
];
526 unsigned semantic_index
= info
->output_semantic_index
[i
];
528 switch (semantic_name
) {
529 case TGSI_SEMANTIC_COLOR
:
530 assert(semantic_index
< 8);
531 for (j
= 0; j
< 4; j
++) {
532 LLVMValueRef ptr
= addrs
[4 * i
+ j
];
533 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
534 color
[semantic_index
][j
] = result
;
537 case TGSI_SEMANTIC_POSITION
:
538 depth
= LLVMBuildLoad(builder
,
539 addrs
[4 * i
+ 0], "");
541 case TGSI_SEMANTIC_STENCIL
:
542 stencil
= LLVMBuildLoad(builder
,
543 addrs
[4 * i
+ 0], "");
545 case TGSI_SEMANTIC_SAMPLEMASK
:
546 samplemask
= LLVMBuildLoad(builder
,
547 addrs
[4 * i
+ 0], "");
550 fprintf(stderr
, "Warning: GFX6 unhandled fs output type:%d\n",
555 /* Fill the return structure. */
556 ret
= ctx
->return_value
;
559 ret
= LLVMBuildInsertValue(builder
, ret
,
560 ac_to_integer(&ctx
->ac
,
561 LLVMGetParam(ctx
->main_fn
,
562 SI_PARAM_ALPHA_REF
)),
563 SI_SGPR_ALPHA_REF
, "");
566 first_vgpr
= vgpr
= SI_SGPR_ALPHA_REF
+ 1;
567 for (i
= 0; i
< ARRAY_SIZE(color
); i
++) {
571 for (j
= 0; j
< 4; j
++)
572 ret
= LLVMBuildInsertValue(builder
, ret
, color
[i
][j
], vgpr
++, "");
575 ret
= LLVMBuildInsertValue(builder
, ret
, depth
, vgpr
++, "");
577 ret
= LLVMBuildInsertValue(builder
, ret
, stencil
, vgpr
++, "");
579 ret
= LLVMBuildInsertValue(builder
, ret
, samplemask
, vgpr
++, "");
581 /* Add the input sample mask for smoothing at the end. */
582 if (vgpr
< first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
)
583 vgpr
= first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
;
584 ret
= LLVMBuildInsertValue(builder
, ret
,
585 LLVMGetParam(ctx
->main_fn
,
586 SI_PARAM_SAMPLE_COVERAGE
), vgpr
++, "");
588 ctx
->return_value
= ret
;
591 static void si_llvm_emit_polygon_stipple(struct si_shader_context
*ctx
,
592 LLVMValueRef param_rw_buffers
,
593 struct ac_arg param_pos_fixed_pt
)
595 LLVMBuilderRef builder
= ctx
->ac
.builder
;
596 LLVMValueRef slot
, desc
, offset
, row
, bit
, address
[2];
598 /* Use the fixed-point gl_FragCoord input.
599 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
600 * per coordinate to get the repeating effect.
602 address
[0] = si_unpack_param(ctx
, param_pos_fixed_pt
, 0, 5);
603 address
[1] = si_unpack_param(ctx
, param_pos_fixed_pt
, 16, 5);
605 /* Load the buffer descriptor. */
606 slot
= LLVMConstInt(ctx
->ac
.i32
, SI_PS_CONST_POLY_STIPPLE
, 0);
607 desc
= ac_build_load_to_sgpr(&ctx
->ac
, param_rw_buffers
, slot
);
609 /* The stipple pattern is 32x32, each row has 32 bits. */
610 offset
= LLVMBuildMul(builder
, address
[1],
611 LLVMConstInt(ctx
->ac
.i32
, 4, 0), "");
612 row
= si_buffer_load_const(ctx
, desc
, offset
);
613 row
= ac_to_integer(&ctx
->ac
, row
);
614 bit
= LLVMBuildLShr(builder
, row
, address
[0], "");
615 bit
= LLVMBuildTrunc(builder
, bit
, ctx
->ac
.i1
, "");
616 ac_build_kill_if_false(&ctx
->ac
, bit
);
619 static void si_llvm_emit_kill(struct ac_shader_abi
*abi
, LLVMValueRef visible
)
621 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
622 LLVMBuilderRef builder
= ctx
->ac
.builder
;
624 if (ctx
->shader
->selector
->force_correct_derivs_after_kill
) {
625 /* Kill immediately while maintaining WQM. */
626 ac_build_kill_if_false(&ctx
->ac
,
627 ac_build_wqm_vote(&ctx
->ac
, visible
));
629 LLVMValueRef mask
= LLVMBuildLoad(builder
, ctx
->postponed_kill
, "");
630 mask
= LLVMBuildAnd(builder
, mask
, visible
, "");
631 LLVMBuildStore(builder
, mask
, ctx
->postponed_kill
);
635 ac_build_kill_if_false(&ctx
->ac
, visible
);
639 * Build the pixel shader prolog function. This handles:
640 * - two-side color selection and interpolation
641 * - overriding interpolation parameters for the API PS
642 * - polygon stippling
644 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
645 * overriden by other states. (e.g. per-sample interpolation)
646 * Interpolated colors are stored after the preloaded VGPRs.
648 void si_llvm_build_ps_prolog(struct si_shader_context
*ctx
,
649 union si_shader_part_key
*key
)
651 LLVMValueRef ret
, func
;
652 int num_returns
, i
, num_color_channels
;
654 memset(&ctx
->args
, 0, sizeof(ctx
->args
));
656 /* Declare inputs. */
657 LLVMTypeRef return_types
[AC_MAX_ARGS
];
659 num_color_channels
= util_bitcount(key
->ps_prolog
.colors_read
);
660 assert(key
->ps_prolog
.num_input_sgprs
+
661 key
->ps_prolog
.num_input_vgprs
+
662 num_color_channels
<= AC_MAX_ARGS
);
663 for (i
= 0; i
< key
->ps_prolog
.num_input_sgprs
; i
++) {
664 ac_add_arg(&ctx
->args
, AC_ARG_SGPR
, 1, AC_ARG_INT
, NULL
);
665 return_types
[num_returns
++] = ctx
->ac
.i32
;
669 struct ac_arg pos_fixed_pt
;
670 struct ac_arg ancillary
;
671 struct ac_arg param_sample_mask
;
672 for (i
= 0; i
< key
->ps_prolog
.num_input_vgprs
; i
++) {
673 struct ac_arg
*arg
= NULL
;
674 if (i
== key
->ps_prolog
.ancillary_vgpr_index
) {
676 } else if (i
== key
->ps_prolog
.ancillary_vgpr_index
+ 1) {
677 arg
= ¶m_sample_mask
;
678 } else if (i
== key
->ps_prolog
.num_input_vgprs
- 1) {
679 /* POS_FIXED_PT is always last. */
682 ac_add_arg(&ctx
->args
, AC_ARG_VGPR
, 1, AC_ARG_FLOAT
, arg
);
683 return_types
[num_returns
++] = ctx
->ac
.f32
;
686 /* Declare outputs (same as inputs + add colors if needed) */
687 for (i
= 0; i
< num_color_channels
; i
++)
688 return_types
[num_returns
++] = ctx
->ac
.f32
;
690 /* Create the function. */
691 si_llvm_create_func(ctx
, "ps_prolog", return_types
, num_returns
, 0);
694 /* Copy inputs to outputs. This should be no-op, as the registers match,
695 * but it will prevent the compiler from overwriting them unintentionally.
697 ret
= ctx
->return_value
;
698 for (i
= 0; i
< ctx
->args
.arg_count
; i
++) {
699 LLVMValueRef p
= LLVMGetParam(func
, i
);
700 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
, i
, "");
703 /* Polygon stippling. */
704 if (key
->ps_prolog
.states
.poly_stipple
) {
705 LLVMValueRef list
= si_prolog_get_rw_buffers(ctx
);
707 si_llvm_emit_polygon_stipple(ctx
, list
, pos_fixed_pt
);
710 if (key
->ps_prolog
.states
.bc_optimize_for_persp
||
711 key
->ps_prolog
.states
.bc_optimize_for_linear
) {
712 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
713 LLVMValueRef center
[2], centroid
[2], tmp
, bc_optimize
;
715 /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
716 * The hw doesn't compute CENTROID if the whole wave only
717 * contains fully-covered quads.
719 * PRIM_MASK is after user SGPRs.
721 bc_optimize
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
722 bc_optimize
= LLVMBuildLShr(ctx
->ac
.builder
, bc_optimize
,
723 LLVMConstInt(ctx
->ac
.i32
, 31, 0), "");
724 bc_optimize
= LLVMBuildTrunc(ctx
->ac
.builder
, bc_optimize
,
727 if (key
->ps_prolog
.states
.bc_optimize_for_persp
) {
728 /* Read PERSP_CENTER. */
729 for (i
= 0; i
< 2; i
++)
730 center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
731 /* Read PERSP_CENTROID. */
732 for (i
= 0; i
< 2; i
++)
733 centroid
[i
] = LLVMGetParam(func
, base
+ 4 + i
);
734 /* Select PERSP_CENTROID. */
735 for (i
= 0; i
< 2; i
++) {
736 tmp
= LLVMBuildSelect(ctx
->ac
.builder
, bc_optimize
,
737 center
[i
], centroid
[i
], "");
738 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
739 tmp
, base
+ 4 + i
, "");
742 if (key
->ps_prolog
.states
.bc_optimize_for_linear
) {
743 /* Read LINEAR_CENTER. */
744 for (i
= 0; i
< 2; i
++)
745 center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
746 /* Read LINEAR_CENTROID. */
747 for (i
= 0; i
< 2; i
++)
748 centroid
[i
] = LLVMGetParam(func
, base
+ 10 + i
);
749 /* Select LINEAR_CENTROID. */
750 for (i
= 0; i
< 2; i
++) {
751 tmp
= LLVMBuildSelect(ctx
->ac
.builder
, bc_optimize
,
752 center
[i
], centroid
[i
], "");
753 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
754 tmp
, base
+ 10 + i
, "");
759 /* Force per-sample interpolation. */
760 if (key
->ps_prolog
.states
.force_persp_sample_interp
) {
761 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
762 LLVMValueRef persp_sample
[2];
764 /* Read PERSP_SAMPLE. */
765 for (i
= 0; i
< 2; i
++)
766 persp_sample
[i
] = LLVMGetParam(func
, base
+ i
);
767 /* Overwrite PERSP_CENTER. */
768 for (i
= 0; i
< 2; i
++)
769 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
770 persp_sample
[i
], base
+ 2 + i
, "");
771 /* Overwrite PERSP_CENTROID. */
772 for (i
= 0; i
< 2; i
++)
773 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
774 persp_sample
[i
], base
+ 4 + i
, "");
776 if (key
->ps_prolog
.states
.force_linear_sample_interp
) {
777 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
778 LLVMValueRef linear_sample
[2];
780 /* Read LINEAR_SAMPLE. */
781 for (i
= 0; i
< 2; i
++)
782 linear_sample
[i
] = LLVMGetParam(func
, base
+ 6 + i
);
783 /* Overwrite LINEAR_CENTER. */
784 for (i
= 0; i
< 2; i
++)
785 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
786 linear_sample
[i
], base
+ 8 + i
, "");
787 /* Overwrite LINEAR_CENTROID. */
788 for (i
= 0; i
< 2; i
++)
789 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
790 linear_sample
[i
], base
+ 10 + i
, "");
793 /* Force center interpolation. */
794 if (key
->ps_prolog
.states
.force_persp_center_interp
) {
795 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
796 LLVMValueRef persp_center
[2];
798 /* Read PERSP_CENTER. */
799 for (i
= 0; i
< 2; i
++)
800 persp_center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
801 /* Overwrite PERSP_SAMPLE. */
802 for (i
= 0; i
< 2; i
++)
803 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
804 persp_center
[i
], base
+ i
, "");
805 /* Overwrite PERSP_CENTROID. */
806 for (i
= 0; i
< 2; i
++)
807 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
808 persp_center
[i
], base
+ 4 + i
, "");
810 if (key
->ps_prolog
.states
.force_linear_center_interp
) {
811 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
812 LLVMValueRef linear_center
[2];
814 /* Read LINEAR_CENTER. */
815 for (i
= 0; i
< 2; i
++)
816 linear_center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
817 /* Overwrite LINEAR_SAMPLE. */
818 for (i
= 0; i
< 2; i
++)
819 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
820 linear_center
[i
], base
+ 6 + i
, "");
821 /* Overwrite LINEAR_CENTROID. */
822 for (i
= 0; i
< 2; i
++)
823 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
824 linear_center
[i
], base
+ 10 + i
, "");
827 /* Interpolate colors. */
828 unsigned color_out_idx
= 0;
829 for (i
= 0; i
< 2; i
++) {
830 unsigned writemask
= (key
->ps_prolog
.colors_read
>> (i
* 4)) & 0xf;
831 unsigned face_vgpr
= key
->ps_prolog
.num_input_sgprs
+
832 key
->ps_prolog
.face_vgpr_index
;
833 LLVMValueRef interp
[2], color
[4];
834 LLVMValueRef interp_ij
= NULL
, prim_mask
= NULL
, face
= NULL
;
839 /* If the interpolation qualifier is not CONSTANT (-1). */
840 if (key
->ps_prolog
.color_interp_vgpr_index
[i
] != -1) {
841 unsigned interp_vgpr
= key
->ps_prolog
.num_input_sgprs
+
842 key
->ps_prolog
.color_interp_vgpr_index
[i
];
844 /* Get the (i,j) updated by bc_optimize handling. */
845 interp
[0] = LLVMBuildExtractValue(ctx
->ac
.builder
, ret
,
847 interp
[1] = LLVMBuildExtractValue(ctx
->ac
.builder
, ret
,
848 interp_vgpr
+ 1, "");
849 interp_ij
= ac_build_gather_values(&ctx
->ac
, interp
, 2);
852 /* Use the absolute location of the input. */
853 prim_mask
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
855 if (key
->ps_prolog
.states
.color_two_side
) {
856 face
= LLVMGetParam(func
, face_vgpr
);
857 face
= ac_to_integer(&ctx
->ac
, face
);
861 key
->ps_prolog
.color_attr_index
[i
], i
,
862 key
->ps_prolog
.num_interp_inputs
,
863 key
->ps_prolog
.colors_read
, interp_ij
,
864 prim_mask
, face
, color
);
867 unsigned chan
= u_bit_scan(&writemask
);
868 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, color
[chan
],
869 ctx
->args
.arg_count
+ color_out_idx
++, "");
873 /* Section 15.2.2 (Shader Inputs) of the OpenGL 4.5 (Core Profile) spec
876 * "When per-sample shading is active due to the use of a fragment
877 * input qualified by sample or due to the use of the gl_SampleID
878 * or gl_SamplePosition variables, only the bit for the current
879 * sample is set in gl_SampleMaskIn. When state specifies multiple
880 * fragment shader invocations for a given fragment, the sample
881 * mask for any single fragment shader invocation may specify a
882 * subset of the covered samples for the fragment. In this case,
883 * the bit corresponding to each covered sample will be set in
884 * exactly one fragment shader invocation."
886 * The samplemask loaded by hardware is always the coverage of the
887 * entire pixel/fragment, so mask bits out based on the sample ID.
889 if (key
->ps_prolog
.states
.samplemask_log_ps_iter
) {
890 /* The bit pattern matches that used by fixed function fragment
892 static const uint16_t ps_iter_masks
[] = {
893 0xffff, /* not used */
899 assert(key
->ps_prolog
.states
.samplemask_log_ps_iter
< ARRAY_SIZE(ps_iter_masks
));
901 uint32_t ps_iter_mask
= ps_iter_masks
[key
->ps_prolog
.states
.samplemask_log_ps_iter
];
902 LLVMValueRef sampleid
= si_unpack_param(ctx
, ancillary
, 8, 4);
903 LLVMValueRef samplemask
= ac_get_arg(&ctx
->ac
, param_sample_mask
);
905 samplemask
= ac_to_integer(&ctx
->ac
, samplemask
);
906 samplemask
= LLVMBuildAnd(
909 LLVMBuildShl(ctx
->ac
.builder
,
910 LLVMConstInt(ctx
->ac
.i32
, ps_iter_mask
, false),
913 samplemask
= ac_to_float(&ctx
->ac
, samplemask
);
915 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, samplemask
,
916 param_sample_mask
.arg_index
, "");
919 /* Tell LLVM to insert WQM instruction sequence when needed. */
920 if (key
->ps_prolog
.wqm
) {
921 LLVMAddTargetDependentFunctionAttr(func
,
922 "amdgpu-ps-wqm-outputs", "");
925 si_llvm_build_ret(ctx
, ret
);
929 * Build the pixel shader epilog function. This handles everything that must be
930 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
932 void si_llvm_build_ps_epilog(struct si_shader_context
*ctx
,
933 union si_shader_part_key
*key
)
935 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
937 struct si_ps_exports exp
= {};
939 memset(&ctx
->args
, 0, sizeof(ctx
->args
));
941 /* Declare input SGPRs. */
942 ac_add_arg(&ctx
->args
, AC_ARG_SGPR
, 1, AC_ARG_INT
, &ctx
->rw_buffers
);
943 ac_add_arg(&ctx
->args
, AC_ARG_SGPR
, 1, AC_ARG_INT
,
944 &ctx
->bindless_samplers_and_images
);
945 ac_add_arg(&ctx
->args
, AC_ARG_SGPR
, 1, AC_ARG_INT
,
946 &ctx
->const_and_shader_buffers
);
947 ac_add_arg(&ctx
->args
, AC_ARG_SGPR
, 1, AC_ARG_INT
,
948 &ctx
->samplers_and_images
);
949 si_add_arg_checked(&ctx
->args
, AC_ARG_SGPR
, 1, AC_ARG_FLOAT
,
950 NULL
, SI_PARAM_ALPHA_REF
);
952 /* Declare input VGPRs. */
953 unsigned required_num_params
=
954 ctx
->args
.num_sgprs_used
+
955 util_bitcount(key
->ps_epilog
.colors_written
) * 4 +
956 key
->ps_epilog
.writes_z
+
957 key
->ps_epilog
.writes_stencil
+
958 key
->ps_epilog
.writes_samplemask
;
960 required_num_params
= MAX2(required_num_params
,
961 ctx
->args
.num_sgprs_used
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
963 while (ctx
->args
.arg_count
< required_num_params
)
964 ac_add_arg(&ctx
->args
, AC_ARG_VGPR
, 1, AC_ARG_FLOAT
, NULL
);
966 /* Create the function. */
967 si_llvm_create_func(ctx
, "ps_epilog", NULL
, 0, 0);
968 /* Disable elimination of unused inputs. */
969 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
970 "InitialPSInputAddr", 0xffffff);
972 /* Process colors. */
973 unsigned vgpr
= ctx
->args
.num_sgprs_used
;
974 unsigned colors_written
= key
->ps_epilog
.colors_written
;
975 int last_color_export
= -1;
977 /* Find the last color export. */
978 if (!key
->ps_epilog
.writes_z
&&
979 !key
->ps_epilog
.writes_stencil
&&
980 !key
->ps_epilog
.writes_samplemask
) {
981 unsigned spi_format
= key
->ps_epilog
.states
.spi_shader_col_format
;
983 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
984 if (colors_written
== 0x1 && key
->ps_epilog
.states
.last_cbuf
> 0) {
985 /* Just set this if any of the colorbuffers are enabled. */
987 ((1ull << (4 * (key
->ps_epilog
.states
.last_cbuf
+ 1))) - 1))
988 last_color_export
= 0;
990 for (i
= 0; i
< 8; i
++)
991 if (colors_written
& (1 << i
) &&
992 (spi_format
>> (i
* 4)) & 0xf)
993 last_color_export
= i
;
997 while (colors_written
) {
998 LLVMValueRef color
[4];
999 int mrt
= u_bit_scan(&colors_written
);
1001 for (i
= 0; i
< 4; i
++)
1002 color
[i
] = LLVMGetParam(ctx
->main_fn
, vgpr
++);
1004 si_export_mrt_color(ctx
, color
, mrt
,
1005 ctx
->args
.arg_count
- 1,
1006 mrt
== last_color_export
, &exp
);
1009 /* Process depth, stencil, samplemask. */
1010 if (key
->ps_epilog
.writes_z
)
1011 depth
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
1012 if (key
->ps_epilog
.writes_stencil
)
1013 stencil
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
1014 if (key
->ps_epilog
.writes_samplemask
)
1015 samplemask
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
1017 if (depth
|| stencil
|| samplemask
)
1018 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
, &exp
);
1019 else if (last_color_export
== -1)
1020 ac_build_export_null(&ctx
->ac
);
1023 si_emit_ps_exports(ctx
, &exp
);
1026 LLVMBuildRetVoid(ctx
->ac
.builder
);
1029 void si_llvm_build_monolithic_ps(struct si_shader_context
*ctx
,
1030 struct si_shader
*shader
)
1032 LLVMValueRef parts
[3];
1033 unsigned num_parts
= 0, main_index
;
1035 union si_shader_part_key prolog_key
;
1036 si_get_ps_prolog_key(shader
, &prolog_key
, false);
1038 if (si_need_ps_prolog(&prolog_key
)) {
1039 si_llvm_build_ps_prolog(ctx
, &prolog_key
);
1040 parts
[num_parts
++] = ctx
->main_fn
;
1043 main_index
= num_parts
;
1044 parts
[num_parts
++] = ctx
->main_fn
;
1046 union si_shader_part_key epilog_key
;
1047 si_get_ps_epilog_key(shader
, &epilog_key
);
1048 si_llvm_build_ps_epilog(ctx
, &epilog_key
);
1049 parts
[num_parts
++] = ctx
->main_fn
;
1051 si_build_wrapper_function(ctx
, parts
, num_parts
, main_index
, 0);
1054 void si_llvm_init_ps_callbacks(struct si_shader_context
*ctx
)
1056 ctx
->abi
.emit_outputs
= si_llvm_return_fs_outputs
;
1057 ctx
->abi
.load_sample_position
= load_sample_position
;
1058 ctx
->abi
.load_sample_mask_in
= load_sample_mask_in
;
1059 ctx
->abi
.emit_fbfetch
= si_nir_emit_fbfetch
;
1060 ctx
->abi
.emit_kill
= si_llvm_emit_kill
;