• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 Advanced Micro Devices, Inc.
3  * All Rights Reserved.
4  *
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:
11  *
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
14  * Software.
15  *
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.
23  */
24 
25 #include "si_pipe.h"
26 #include "si_shader_internal.h"
27 #include "sid.h"
28 
si_get_sample_id(struct si_shader_context * ctx)29 LLVMValueRef si_get_sample_id(struct si_shader_context *ctx)
30 {
31    return si_unpack_param(ctx, ctx->args.ancillary, 8, 4);
32 }
33 
load_sample_mask_in(struct ac_shader_abi * abi)34 static LLVMValueRef load_sample_mask_in(struct ac_shader_abi *abi)
35 {
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));
38 }
39 
load_sample_position(struct ac_shader_abi * abi,LLVMValueRef sample_id)40 static LLVMValueRef load_sample_position(struct ac_shader_abi *abi, LLVMValueRef sample_id)
41 {
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);
46 
47    /* offset = sample_id * 8  (8 = 2 floats containing samplepos.xy) */
48    LLVMValueRef offset0 =
49       LLVMBuildMul(ctx->ac.builder, sample_id, LLVMConstInt(ctx->ac.i32, 8, 0), "");
50    LLVMValueRef offset1 =
51       LLVMBuildAdd(ctx->ac.builder, offset0, LLVMConstInt(ctx->ac.i32, 4, 0), "");
52 
53    LLVMValueRef pos[4] = {si_buffer_load_const(ctx, resource, offset0),
54                           si_buffer_load_const(ctx, resource, offset1),
55                           LLVMConstReal(ctx->ac.f32, 0), LLVMConstReal(ctx->ac.f32, 0)};
56 
57    return ac_build_gather_values(&ctx->ac, pos, 4);
58 }
59 
si_nir_emit_fbfetch(struct ac_shader_abi * abi)60 static LLVMValueRef si_nir_emit_fbfetch(struct ac_shader_abi *abi)
61 {
62    struct si_shader_context *ctx = si_shader_context_from_abi(abi);
63    struct ac_image_args args = {};
64    LLVMValueRef ptr, image, fmask;
65 
66    /* Ignore src0, because KHR_blend_func_extended disallows multiple render
67     * targets.
68     */
69 
70    /* Load the image descriptor. */
71    STATIC_ASSERT(SI_PS_IMAGE_COLORBUF0 % 2 == 0);
72    ptr = ac_get_arg(&ctx->ac, ctx->rw_buffers);
73    ptr =
74       LLVMBuildPointerCast(ctx->ac.builder, ptr, ac_array_in_const32_addr_space(ctx->ac.v8i32), "");
75    image =
76       ac_build_load_to_sgpr(&ctx->ac, ptr, LLVMConstInt(ctx->ac.i32, SI_PS_IMAGE_COLORBUF0 / 2, 0));
77 
78    unsigned chan = 0;
79 
80    args.coords[chan++] = si_unpack_param(ctx, ctx->pos_fixed_pt, 0, 16);
81 
82    if (!ctx->shader->key.mono.u.ps.fbfetch_is_1D)
83       args.coords[chan++] = si_unpack_param(ctx, ctx->pos_fixed_pt, 16, 16);
84 
85    /* Get the current render target layer index. */
86    if (ctx->shader->key.mono.u.ps.fbfetch_layered)
87       args.coords[chan++] = si_unpack_param(ctx, ctx->args.ancillary, 16, 11);
88 
89    if (ctx->shader->key.mono.u.ps.fbfetch_msaa)
90       args.coords[chan++] = si_get_sample_id(ctx);
91 
92    if (ctx->shader->key.mono.u.ps.fbfetch_msaa && !(ctx->screen->debug_flags & DBG(NO_FMASK))) {
93       fmask = ac_build_load_to_sgpr(&ctx->ac, ptr,
94                                     LLVMConstInt(ctx->ac.i32, SI_PS_IMAGE_COLORBUF0_FMASK / 2, 0));
95 
96       ac_apply_fmask_to_sample(&ctx->ac, fmask, args.coords,
97                                ctx->shader->key.mono.u.ps.fbfetch_layered);
98    }
99 
100    args.opcode = ac_image_load;
101    args.resource = image;
102    args.dmask = 0xf;
103    args.attributes = AC_FUNC_ATTR_READNONE;
104 
105    if (ctx->shader->key.mono.u.ps.fbfetch_msaa)
106       args.dim =
107          ctx->shader->key.mono.u.ps.fbfetch_layered ? ac_image_2darraymsaa : ac_image_2dmsaa;
108    else if (ctx->shader->key.mono.u.ps.fbfetch_is_1D)
109       args.dim = ctx->shader->key.mono.u.ps.fbfetch_layered ? ac_image_1darray : ac_image_1d;
110    else
111       args.dim = ctx->shader->key.mono.u.ps.fbfetch_layered ? ac_image_2darray : ac_image_2d;
112 
113    return ac_build_image_opcode(&ctx->ac, &args);
114 }
115 
si_build_fs_interp(struct si_shader_context * ctx,unsigned attr_index,unsigned chan,LLVMValueRef prim_mask,LLVMValueRef i,LLVMValueRef j)116 static LLVMValueRef si_build_fs_interp(struct si_shader_context *ctx, unsigned attr_index,
117                                        unsigned chan, LLVMValueRef prim_mask, LLVMValueRef i,
118                                        LLVMValueRef j)
119 {
120    if (i || j) {
121       return ac_build_fs_interp(&ctx->ac, LLVMConstInt(ctx->ac.i32, chan, 0),
122                                 LLVMConstInt(ctx->ac.i32, attr_index, 0), prim_mask, i, j);
123    }
124    return ac_build_fs_interp_mov(&ctx->ac, LLVMConstInt(ctx->ac.i32, 2, 0), /* P0 */
125                                  LLVMConstInt(ctx->ac.i32, chan, 0),
126                                  LLVMConstInt(ctx->ac.i32, attr_index, 0), prim_mask);
127 }
128 
129 /**
130  * Interpolate a fragment shader input.
131  *
132  * @param ctx		context
133  * @param input_index		index of the input in hardware
134  * @param semantic_index	semantic index
135  * @param num_interp_inputs	number of all interpolated inputs (= BCOLOR offset)
136  * @param colors_read_mask	color components read (4 bits for each color, 8 bits in total)
137  * @param interp_param		interpolation weights (i,j)
138  * @param prim_mask		SI_PARAM_PRIM_MASK
139  * @param face			SI_PARAM_FRONT_FACE
140  * @param result		the return value (4 components)
141  */
interp_fs_color(struct si_shader_context * ctx,unsigned input_index,unsigned semantic_index,unsigned num_interp_inputs,unsigned colors_read_mask,LLVMValueRef interp_param,LLVMValueRef prim_mask,LLVMValueRef face,LLVMValueRef result[4])142 static void interp_fs_color(struct si_shader_context *ctx, unsigned input_index,
143                             unsigned semantic_index, unsigned num_interp_inputs,
144                             unsigned colors_read_mask, LLVMValueRef interp_param,
145                             LLVMValueRef prim_mask, LLVMValueRef face, LLVMValueRef result[4])
146 {
147    LLVMValueRef i = NULL, j = NULL;
148    unsigned chan;
149 
150    /* fs.constant returns the param from the middle vertex, so it's not
151     * really useful for flat shading. It's meant to be used for custom
152     * interpolation (but the intrinsic can't fetch from the other two
153     * vertices).
154     *
155     * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
156     * to do the right thing. The only reason we use fs.constant is that
157     * fs.interp cannot be used on integers, because they can be equal
158     * to NaN.
159     *
160     * When interp is false we will use fs.constant or for newer llvm,
161     * amdgcn.interp.mov.
162     */
163    bool interp = interp_param != NULL;
164 
165    if (interp) {
166       interp_param =
167          LLVMBuildBitCast(ctx->ac.builder, interp_param, ctx->ac.v2f32, "");
168 
169       i = LLVMBuildExtractElement(ctx->ac.builder, interp_param, ctx->ac.i32_0, "");
170       j = LLVMBuildExtractElement(ctx->ac.builder, interp_param, ctx->ac.i32_1, "");
171    }
172 
173    if (ctx->shader->key.part.ps.prolog.color_two_side) {
174       LLVMValueRef is_face_positive;
175 
176       /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
177        * otherwise it's at offset "num_inputs".
178        */
179       unsigned back_attr_offset = num_interp_inputs;
180       if (semantic_index == 1 && colors_read_mask & 0xf)
181          back_attr_offset += 1;
182 
183       is_face_positive = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE, face, ctx->ac.i32_0, "");
184 
185       for (chan = 0; chan < 4; chan++) {
186          LLVMValueRef front, back;
187 
188          front = si_build_fs_interp(ctx, input_index, chan, prim_mask, i, j);
189          back = si_build_fs_interp(ctx, back_attr_offset, chan, prim_mask, i, j);
190 
191          result[chan] = LLVMBuildSelect(ctx->ac.builder, is_face_positive, front, back, "");
192       }
193    } else {
194       for (chan = 0; chan < 4; chan++) {
195          result[chan] = si_build_fs_interp(ctx, input_index, chan, prim_mask, i, j);
196       }
197    }
198 }
199 
si_alpha_test(struct si_shader_context * ctx,LLVMValueRef alpha)200 static void si_alpha_test(struct si_shader_context *ctx, LLVMValueRef alpha)
201 {
202    if (ctx->shader->key.part.ps.epilog.alpha_func != PIPE_FUNC_NEVER) {
203       static LLVMRealPredicate cond_map[PIPE_FUNC_ALWAYS + 1] = {
204          [PIPE_FUNC_LESS] = LLVMRealOLT,     [PIPE_FUNC_EQUAL] = LLVMRealOEQ,
205          [PIPE_FUNC_LEQUAL] = LLVMRealOLE,   [PIPE_FUNC_GREATER] = LLVMRealOGT,
206          [PIPE_FUNC_NOTEQUAL] = LLVMRealONE, [PIPE_FUNC_GEQUAL] = LLVMRealOGE,
207       };
208       LLVMRealPredicate cond = cond_map[ctx->shader->key.part.ps.epilog.alpha_func];
209       assert(cond);
210 
211       LLVMValueRef alpha_ref = LLVMGetParam(ctx->main_fn, SI_PARAM_ALPHA_REF);
212       if (LLVMTypeOf(alpha) == ctx->ac.f16)
213          alpha_ref = LLVMBuildFPTrunc(ctx->ac.builder, alpha_ref, ctx->ac.f16, "");
214 
215       LLVMValueRef alpha_pass = LLVMBuildFCmp(ctx->ac.builder, cond, alpha, alpha_ref, "");
216       ac_build_kill_if_false(&ctx->ac, alpha_pass);
217    } else {
218       ac_build_kill_if_false(&ctx->ac, ctx->ac.i1false);
219    }
220 }
221 
si_scale_alpha_by_sample_mask(struct si_shader_context * ctx,LLVMValueRef alpha,unsigned samplemask_param)222 static LLVMValueRef si_scale_alpha_by_sample_mask(struct si_shader_context *ctx, LLVMValueRef alpha,
223                                                   unsigned samplemask_param)
224 {
225    LLVMValueRef coverage;
226 
227    /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
228    coverage = LLVMGetParam(ctx->main_fn, samplemask_param);
229    coverage = ac_to_integer(&ctx->ac, coverage);
230 
231    coverage = ac_build_intrinsic(&ctx->ac, "llvm.ctpop.i32", ctx->ac.i32, &coverage, 1,
232                                  AC_FUNC_ATTR_READNONE);
233 
234    coverage = LLVMBuildUIToFP(ctx->ac.builder, coverage, ctx->ac.f32, "");
235 
236    coverage = LLVMBuildFMul(ctx->ac.builder, coverage,
237                             LLVMConstReal(ctx->ac.f32, 1.0 / SI_NUM_SMOOTH_AA_SAMPLES), "");
238 
239    if (LLVMTypeOf(alpha) == ctx->ac.f16)
240       coverage = LLVMBuildFPTrunc(ctx->ac.builder, coverage, ctx->ac.f16, "");
241 
242    return LLVMBuildFMul(ctx->ac.builder, alpha, coverage, "");
243 }
244 
245 struct si_ps_exports {
246    unsigned num;
247    struct ac_export_args args[10];
248 };
249 
pack_two_16bit(struct ac_llvm_context * ctx,LLVMValueRef args[2])250 static LLVMValueRef pack_two_16bit(struct ac_llvm_context *ctx, LLVMValueRef args[2])
251 {
252    LLVMValueRef tmp = ac_build_gather_values(ctx, args, 2);
253    return LLVMBuildBitCast(ctx->builder, tmp, ctx->v2f16, "");
254 }
255 
get_color_32bit(struct si_shader_context * ctx,unsigned color_type,LLVMValueRef value)256 static LLVMValueRef get_color_32bit(struct si_shader_context *ctx, unsigned color_type,
257                                     LLVMValueRef value)
258 {
259    switch (color_type) {
260    case SI_TYPE_FLOAT16:
261       return LLVMBuildFPExt(ctx->ac.builder, value, ctx->ac.f32, "");
262    case SI_TYPE_INT16:
263       value = ac_to_integer(&ctx->ac, value);
264       value = LLVMBuildSExt(ctx->ac.builder, value, ctx->ac.i32, "");
265       return ac_to_float(&ctx->ac, value);
266    case SI_TYPE_UINT16:
267       value = ac_to_integer(&ctx->ac, value);
268       value = LLVMBuildZExt(ctx->ac.builder, value, ctx->ac.i32, "");
269       return ac_to_float(&ctx->ac, value);
270    case SI_TYPE_ANY32:
271       return value;
272    }
273    return NULL;
274 }
275 
276 /* Initialize arguments for the shader export intrinsic */
si_llvm_init_ps_export_args(struct si_shader_context * ctx,LLVMValueRef * values,unsigned cbuf,unsigned compacted_mrt_index,unsigned color_type,struct ac_export_args * args)277 static void si_llvm_init_ps_export_args(struct si_shader_context *ctx, LLVMValueRef *values,
278                                         unsigned cbuf, unsigned compacted_mrt_index,
279                                         unsigned color_type, struct ac_export_args *args)
280 {
281    const struct si_shader_key *key = &ctx->shader->key;
282    unsigned col_formats = key->part.ps.epilog.spi_shader_col_format;
283    LLVMValueRef f32undef = LLVMGetUndef(ctx->ac.f32);
284    unsigned spi_shader_col_format;
285    unsigned chan;
286    bool is_int8, is_int10;
287 
288    assert(cbuf < 8);
289 
290    spi_shader_col_format = (col_formats >> (cbuf * 4)) & 0xf;
291    is_int8 = (key->part.ps.epilog.color_is_int8 >> cbuf) & 0x1;
292    is_int10 = (key->part.ps.epilog.color_is_int10 >> cbuf) & 0x1;
293 
294    /* Default is 0xf. Adjusted below depending on the format. */
295    args->enabled_channels = 0xf; /* writemask */
296 
297    /* Specify whether the EXEC mask represents the valid mask */
298    args->valid_mask = 0;
299 
300    /* Specify whether this is the last export */
301    args->done = 0;
302 
303    /* Specify the target we are exporting */
304    args->target = V_008DFC_SQ_EXP_MRT + compacted_mrt_index;
305 
306    args->compr = false;
307    args->out[0] = f32undef;
308    args->out[1] = f32undef;
309    args->out[2] = f32undef;
310    args->out[3] = f32undef;
311 
312    LLVMValueRef (*packf)(struct ac_llvm_context * ctx, LLVMValueRef args[2]) = NULL;
313    LLVMValueRef (*packi)(struct ac_llvm_context * ctx, LLVMValueRef args[2], unsigned bits,
314                          bool hi) = NULL;
315 
316    switch (spi_shader_col_format) {
317    case V_028714_SPI_SHADER_ZERO:
318       args->enabled_channels = 0; /* writemask */
319       args->target = V_008DFC_SQ_EXP_NULL;
320       break;
321 
322    case V_028714_SPI_SHADER_32_R:
323       args->enabled_channels = 1; /* writemask */
324       args->out[0] = get_color_32bit(ctx, color_type, values[0]);
325       break;
326 
327    case V_028714_SPI_SHADER_32_GR:
328       args->enabled_channels = 0x3; /* writemask */
329       args->out[0] = get_color_32bit(ctx, color_type, values[0]);
330       args->out[1] = get_color_32bit(ctx, color_type, values[1]);
331       break;
332 
333    case V_028714_SPI_SHADER_32_AR:
334       if (ctx->screen->info.chip_class >= GFX10) {
335          args->enabled_channels = 0x3; /* writemask */
336          args->out[0] = get_color_32bit(ctx, color_type, values[0]);
337          args->out[1] = get_color_32bit(ctx, color_type, values[3]);
338       } else {
339          args->enabled_channels = 0x9; /* writemask */
340          args->out[0] = get_color_32bit(ctx, color_type, values[0]);
341          args->out[3] = get_color_32bit(ctx, color_type, values[3]);
342       }
343       break;
344 
345    case V_028714_SPI_SHADER_FP16_ABGR:
346       if (color_type != SI_TYPE_ANY32)
347          packf = pack_two_16bit;
348       else
349          packf = ac_build_cvt_pkrtz_f16;
350       break;
351 
352    case V_028714_SPI_SHADER_UNORM16_ABGR:
353       if (color_type != SI_TYPE_ANY32)
354          packf = ac_build_cvt_pknorm_u16_f16;
355       else
356          packf = ac_build_cvt_pknorm_u16;
357       break;
358 
359    case V_028714_SPI_SHADER_SNORM16_ABGR:
360       if (color_type != SI_TYPE_ANY32)
361          packf = ac_build_cvt_pknorm_i16_f16;
362       else
363          packf = ac_build_cvt_pknorm_i16;
364       break;
365 
366    case V_028714_SPI_SHADER_UINT16_ABGR:
367       if (color_type != SI_TYPE_ANY32)
368          packf = pack_two_16bit;
369       else
370          packi = ac_build_cvt_pk_u16;
371       break;
372 
373    case V_028714_SPI_SHADER_SINT16_ABGR:
374       if (color_type != SI_TYPE_ANY32)
375          packf = pack_two_16bit;
376       else
377          packi = ac_build_cvt_pk_i16;
378       break;
379 
380    case V_028714_SPI_SHADER_32_ABGR:
381       for (unsigned i = 0; i < 4; i++)
382          args->out[i] = get_color_32bit(ctx, color_type, values[i]);
383       break;
384    }
385 
386    /* Pack f16 or norm_i16/u16. */
387    if (packf) {
388       for (chan = 0; chan < 2; chan++) {
389          LLVMValueRef pack_args[2] = {values[2 * chan], values[2 * chan + 1]};
390          LLVMValueRef packed;
391 
392          packed = packf(&ctx->ac, pack_args);
393          args->out[chan] = ac_to_float(&ctx->ac, packed);
394       }
395       args->compr = 1; /* COMPR flag */
396    }
397    /* Pack i16/u16. */
398    if (packi) {
399       for (chan = 0; chan < 2; chan++) {
400          LLVMValueRef pack_args[2] = {ac_to_integer(&ctx->ac, values[2 * chan]),
401                                       ac_to_integer(&ctx->ac, values[2 * chan + 1])};
402          LLVMValueRef packed;
403 
404          packed = packi(&ctx->ac, pack_args, is_int8 ? 8 : is_int10 ? 10 : 16, chan == 1);
405          args->out[chan] = ac_to_float(&ctx->ac, packed);
406       }
407       args->compr = 1; /* COMPR flag */
408    }
409 }
410 
si_export_mrt_color(struct si_shader_context * ctx,LLVMValueRef * color,unsigned index,unsigned compacted_mrt_index,unsigned samplemask_param,bool is_last,unsigned color_type,struct si_ps_exports * exp)411 static bool si_export_mrt_color(struct si_shader_context *ctx, LLVMValueRef *color, unsigned index,
412                                 unsigned compacted_mrt_index, unsigned samplemask_param,
413                                 bool is_last, unsigned color_type, struct si_ps_exports *exp)
414 {
415    int i;
416 
417    /* Clamp color */
418    if (ctx->shader->key.part.ps.epilog.clamp_color)
419       for (i = 0; i < 4; i++)
420          color[i] = ac_build_clamp(&ctx->ac, color[i]);
421 
422    /* Alpha to one */
423    if (ctx->shader->key.part.ps.epilog.alpha_to_one)
424       color[3] = LLVMConstReal(LLVMTypeOf(color[0]), 1);
425 
426    /* Alpha test */
427    if (index == 0 && ctx->shader->key.part.ps.epilog.alpha_func != PIPE_FUNC_ALWAYS)
428       si_alpha_test(ctx, color[3]);
429 
430    /* Line & polygon smoothing */
431    if (ctx->shader->key.part.ps.epilog.poly_line_smoothing)
432       color[3] = si_scale_alpha_by_sample_mask(ctx, color[3], samplemask_param);
433 
434    /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
435    if (ctx->shader->key.part.ps.epilog.last_cbuf > 0) {
436       struct ac_export_args args[8];
437       int c, last = -1;
438 
439       assert(compacted_mrt_index == 0);
440 
441       /* Get the export arguments, also find out what the last one is. */
442       for (c = 0; c <= ctx->shader->key.part.ps.epilog.last_cbuf; c++) {
443          si_llvm_init_ps_export_args(ctx, color, c, compacted_mrt_index,
444                                      color_type, &args[c]);
445          if (args[c].enabled_channels) {
446             compacted_mrt_index++;
447             last = c;
448          }
449       }
450       if (last == -1)
451          return false;
452 
453       /* Emit all exports. */
454       for (c = 0; c <= ctx->shader->key.part.ps.epilog.last_cbuf; c++) {
455          if (is_last && last == c) {
456             args[c].valid_mask = 1; /* whether the EXEC mask is valid */
457             args[c].done = 1;       /* DONE bit */
458          } else if (!args[c].enabled_channels)
459             continue; /* unnecessary NULL export */
460 
461          memcpy(&exp->args[exp->num++], &args[c], sizeof(args[c]));
462       }
463    } else {
464       struct ac_export_args args;
465 
466       /* Export */
467       si_llvm_init_ps_export_args(ctx, color, index, compacted_mrt_index,
468                                   color_type, &args);
469       if (is_last) {
470          args.valid_mask = 1; /* whether the EXEC mask is valid */
471          args.done = 1;       /* DONE bit */
472       } else if (!args.enabled_channels)
473          return false; /* unnecessary NULL export */
474 
475       memcpy(&exp->args[exp->num++], &args, sizeof(args));
476    }
477    return true;
478 }
479 
480 /**
481  * Return PS outputs in this order:
482  *
483  * v[0:3] = color0.xyzw
484  * v[4:7] = color1.xyzw
485  * ...
486  * vN+0 = Depth
487  * vN+1 = Stencil
488  * vN+2 = SampleMask
489  * vN+3 = SampleMaskIn (used for OpenGL smoothing)
490  *
491  * The alpha-ref SGPR is returned via its original location.
492  */
si_llvm_return_fs_outputs(struct ac_shader_abi * abi,unsigned max_outputs,LLVMValueRef * addrs)493 static void si_llvm_return_fs_outputs(struct ac_shader_abi *abi, unsigned max_outputs,
494                                       LLVMValueRef *addrs)
495 {
496    struct si_shader_context *ctx = si_shader_context_from_abi(abi);
497    struct si_shader *shader = ctx->shader;
498    struct si_shader_info *info = &shader->selector->info;
499    LLVMBuilderRef builder = ctx->ac.builder;
500    unsigned i, j, first_vgpr, vgpr;
501 
502    LLVMValueRef color[8][4] = {};
503    LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
504    LLVMValueRef ret;
505 
506    /* Read the output values. */
507    for (i = 0; i < info->num_outputs; i++) {
508       unsigned semantic = info->output_semantic[i];
509 
510       switch (semantic) {
511       case FRAG_RESULT_DEPTH:
512          depth = LLVMBuildLoad(builder, addrs[4 * i + 0], "");
513          break;
514       case FRAG_RESULT_STENCIL:
515          stencil = LLVMBuildLoad(builder, addrs[4 * i + 0], "");
516          break;
517       case FRAG_RESULT_SAMPLE_MASK:
518          samplemask = LLVMBuildLoad(builder, addrs[4 * i + 0], "");
519          break;
520       default:
521          if (semantic >= FRAG_RESULT_DATA0 && semantic <= FRAG_RESULT_DATA7) {
522             unsigned index = semantic - FRAG_RESULT_DATA0;
523 
524             for (j = 0; j < 4; j++) {
525                LLVMValueRef ptr = addrs[4 * i + j];
526                LLVMValueRef result = LLVMBuildLoad(builder, ptr, "");
527                color[index][j] = result;
528             }
529          } else {
530             fprintf(stderr, "Warning: Unhandled fs output type:%d\n", semantic);
531          }
532          break;
533       }
534    }
535 
536    /* Fill the return structure. */
537    ret = ctx->return_value;
538 
539    /* Set SGPRs. */
540    ret = LLVMBuildInsertValue(
541       builder, ret, ac_to_integer(&ctx->ac, LLVMGetParam(ctx->main_fn, SI_PARAM_ALPHA_REF)),
542       SI_SGPR_ALPHA_REF, "");
543 
544    /* Set VGPRs */
545    first_vgpr = vgpr = SI_SGPR_ALPHA_REF + 1;
546    for (i = 0; i < ARRAY_SIZE(color); i++) {
547       if (!color[i][0])
548          continue;
549 
550       if (LLVMTypeOf(color[i][0]) == ctx->ac.f16) {
551          for (j = 0; j < 2; j++) {
552             LLVMValueRef tmp = ac_build_gather_values(&ctx->ac, &color[i][j * 2], 2);
553             tmp = LLVMBuildBitCast(builder, tmp, ctx->ac.f32, "");
554             ret = LLVMBuildInsertValue(builder, ret, tmp, vgpr++, "");
555          }
556          vgpr += 2;
557       } else {
558          for (j = 0; j < 4; j++)
559             ret = LLVMBuildInsertValue(builder, ret, color[i][j], vgpr++, "");
560       }
561    }
562    if (depth)
563       ret = LLVMBuildInsertValue(builder, ret, depth, vgpr++, "");
564    if (stencil)
565       ret = LLVMBuildInsertValue(builder, ret, stencil, vgpr++, "");
566    if (samplemask)
567       ret = LLVMBuildInsertValue(builder, ret, samplemask, vgpr++, "");
568 
569    /* Add the input sample mask for smoothing at the end. */
570    if (vgpr < first_vgpr + PS_EPILOG_SAMPLEMASK_MIN_LOC)
571       vgpr = first_vgpr + PS_EPILOG_SAMPLEMASK_MIN_LOC;
572    ret = LLVMBuildInsertValue(builder, ret, LLVMGetParam(ctx->main_fn, SI_PARAM_SAMPLE_COVERAGE),
573                               vgpr++, "");
574 
575    ctx->return_value = ret;
576 }
577 
si_llvm_emit_polygon_stipple(struct si_shader_context * ctx,LLVMValueRef param_rw_buffers,struct ac_arg param_pos_fixed_pt)578 static void si_llvm_emit_polygon_stipple(struct si_shader_context *ctx,
579                                          LLVMValueRef param_rw_buffers,
580                                          struct ac_arg param_pos_fixed_pt)
581 {
582    LLVMBuilderRef builder = ctx->ac.builder;
583    LLVMValueRef slot, desc, offset, row, bit, address[2];
584 
585    /* Use the fixed-point gl_FragCoord input.
586     * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
587     * per coordinate to get the repeating effect.
588     */
589    address[0] = si_unpack_param(ctx, param_pos_fixed_pt, 0, 5);
590    address[1] = si_unpack_param(ctx, param_pos_fixed_pt, 16, 5);
591 
592    /* Load the buffer descriptor. */
593    slot = LLVMConstInt(ctx->ac.i32, SI_PS_CONST_POLY_STIPPLE, 0);
594    desc = ac_build_load_to_sgpr(&ctx->ac, param_rw_buffers, slot);
595 
596    /* The stipple pattern is 32x32, each row has 32 bits. */
597    offset = LLVMBuildMul(builder, address[1], LLVMConstInt(ctx->ac.i32, 4, 0), "");
598    row = si_buffer_load_const(ctx, desc, offset);
599    row = ac_to_integer(&ctx->ac, row);
600    bit = LLVMBuildLShr(builder, row, address[0], "");
601    bit = LLVMBuildTrunc(builder, bit, ctx->ac.i1, "");
602    ac_build_kill_if_false(&ctx->ac, bit);
603 }
604 
605 /**
606  * Build the pixel shader prolog function. This handles:
607  * - two-side color selection and interpolation
608  * - overriding interpolation parameters for the API PS
609  * - polygon stippling
610  *
611  * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
612  * overriden by other states. (e.g. per-sample interpolation)
613  * Interpolated colors are stored after the preloaded VGPRs.
614  */
si_llvm_build_ps_prolog(struct si_shader_context * ctx,union si_shader_part_key * key)615 void si_llvm_build_ps_prolog(struct si_shader_context *ctx, union si_shader_part_key *key)
616 {
617    LLVMValueRef ret, func;
618    int num_returns, i, num_color_channels;
619 
620    memset(&ctx->args, 0, sizeof(ctx->args));
621 
622    /* Declare inputs. */
623    LLVMTypeRef return_types[AC_MAX_ARGS];
624    num_returns = 0;
625    num_color_channels = util_bitcount(key->ps_prolog.colors_read);
626    assert(key->ps_prolog.num_input_sgprs + key->ps_prolog.num_input_vgprs + num_color_channels <=
627           AC_MAX_ARGS);
628    for (i = 0; i < key->ps_prolog.num_input_sgprs; i++) {
629       ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
630       return_types[num_returns++] = ctx->ac.i32;
631    }
632 
633    struct ac_arg pos_fixed_pt;
634    struct ac_arg ancillary;
635    struct ac_arg param_sample_mask;
636    for (i = 0; i < key->ps_prolog.num_input_vgprs; i++) {
637       struct ac_arg *arg = NULL;
638       if (i == key->ps_prolog.ancillary_vgpr_index) {
639          arg = &ancillary;
640       } else if (i == key->ps_prolog.ancillary_vgpr_index + 1) {
641          arg = &param_sample_mask;
642       } else if (i == key->ps_prolog.num_input_vgprs - 1) {
643          /* POS_FIXED_PT is always last. */
644          arg = &pos_fixed_pt;
645       }
646       ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT, arg);
647       return_types[num_returns++] = ctx->ac.f32;
648    }
649 
650    /* Declare outputs (same as inputs + add colors if needed) */
651    for (i = 0; i < num_color_channels; i++)
652       return_types[num_returns++] = ctx->ac.f32;
653 
654    /* Create the function. */
655    si_llvm_create_func(ctx, "ps_prolog", return_types, num_returns, 0);
656    func = ctx->main_fn;
657 
658    /* Copy inputs to outputs. This should be no-op, as the registers match,
659     * but it will prevent the compiler from overwriting them unintentionally.
660     */
661    ret = ctx->return_value;
662    for (i = 0; i < ctx->args.arg_count; i++) {
663       LLVMValueRef p = LLVMGetParam(func, i);
664       ret = LLVMBuildInsertValue(ctx->ac.builder, ret, p, i, "");
665    }
666 
667    /* Polygon stippling. */
668    if (key->ps_prolog.states.poly_stipple) {
669       LLVMValueRef list = si_prolog_get_rw_buffers(ctx);
670 
671       si_llvm_emit_polygon_stipple(ctx, list, pos_fixed_pt);
672    }
673 
674    if (key->ps_prolog.states.bc_optimize_for_persp ||
675        key->ps_prolog.states.bc_optimize_for_linear) {
676       unsigned i, base = key->ps_prolog.num_input_sgprs;
677       LLVMValueRef center[2], centroid[2], tmp, bc_optimize;
678 
679       /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
680        * The hw doesn't compute CENTROID if the whole wave only
681        * contains fully-covered quads.
682        *
683        * PRIM_MASK is after user SGPRs.
684        */
685       bc_optimize = LLVMGetParam(func, SI_PS_NUM_USER_SGPR);
686       bc_optimize =
687          LLVMBuildLShr(ctx->ac.builder, bc_optimize, LLVMConstInt(ctx->ac.i32, 31, 0), "");
688       bc_optimize = LLVMBuildTrunc(ctx->ac.builder, bc_optimize, ctx->ac.i1, "");
689 
690       if (key->ps_prolog.states.bc_optimize_for_persp) {
691          /* Read PERSP_CENTER. */
692          for (i = 0; i < 2; i++)
693             center[i] = LLVMGetParam(func, base + 2 + i);
694          /* Read PERSP_CENTROID. */
695          for (i = 0; i < 2; i++)
696             centroid[i] = LLVMGetParam(func, base + 4 + i);
697          /* Select PERSP_CENTROID. */
698          for (i = 0; i < 2; i++) {
699             tmp = LLVMBuildSelect(ctx->ac.builder, bc_optimize, center[i], centroid[i], "");
700             ret = LLVMBuildInsertValue(ctx->ac.builder, ret, tmp, base + 4 + i, "");
701          }
702       }
703       if (key->ps_prolog.states.bc_optimize_for_linear) {
704          /* Read LINEAR_CENTER. */
705          for (i = 0; i < 2; i++)
706             center[i] = LLVMGetParam(func, base + 8 + i);
707          /* Read LINEAR_CENTROID. */
708          for (i = 0; i < 2; i++)
709             centroid[i] = LLVMGetParam(func, base + 10 + i);
710          /* Select LINEAR_CENTROID. */
711          for (i = 0; i < 2; i++) {
712             tmp = LLVMBuildSelect(ctx->ac.builder, bc_optimize, center[i], centroid[i], "");
713             ret = LLVMBuildInsertValue(ctx->ac.builder, ret, tmp, base + 10 + i, "");
714          }
715       }
716    }
717 
718    /* Force per-sample interpolation. */
719    if (key->ps_prolog.states.force_persp_sample_interp) {
720       unsigned i, base = key->ps_prolog.num_input_sgprs;
721       LLVMValueRef persp_sample[2];
722 
723       /* Read PERSP_SAMPLE. */
724       for (i = 0; i < 2; i++)
725          persp_sample[i] = LLVMGetParam(func, base + i);
726       /* Overwrite PERSP_CENTER. */
727       for (i = 0; i < 2; i++)
728          ret = LLVMBuildInsertValue(ctx->ac.builder, ret, persp_sample[i], base + 2 + i, "");
729       /* Overwrite PERSP_CENTROID. */
730       for (i = 0; i < 2; i++)
731          ret = LLVMBuildInsertValue(ctx->ac.builder, ret, persp_sample[i], base + 4 + i, "");
732    }
733    if (key->ps_prolog.states.force_linear_sample_interp) {
734       unsigned i, base = key->ps_prolog.num_input_sgprs;
735       LLVMValueRef linear_sample[2];
736 
737       /* Read LINEAR_SAMPLE. */
738       for (i = 0; i < 2; i++)
739          linear_sample[i] = LLVMGetParam(func, base + 6 + i);
740       /* Overwrite LINEAR_CENTER. */
741       for (i = 0; i < 2; i++)
742          ret = LLVMBuildInsertValue(ctx->ac.builder, ret, linear_sample[i], base + 8 + i, "");
743       /* Overwrite LINEAR_CENTROID. */
744       for (i = 0; i < 2; i++)
745          ret = LLVMBuildInsertValue(ctx->ac.builder, ret, linear_sample[i], base + 10 + i, "");
746    }
747 
748    /* Force center interpolation. */
749    if (key->ps_prolog.states.force_persp_center_interp) {
750       unsigned i, base = key->ps_prolog.num_input_sgprs;
751       LLVMValueRef persp_center[2];
752 
753       /* Read PERSP_CENTER. */
754       for (i = 0; i < 2; i++)
755          persp_center[i] = LLVMGetParam(func, base + 2 + i);
756       /* Overwrite PERSP_SAMPLE. */
757       for (i = 0; i < 2; i++)
758          ret = LLVMBuildInsertValue(ctx->ac.builder, ret, persp_center[i], base + i, "");
759       /* Overwrite PERSP_CENTROID. */
760       for (i = 0; i < 2; i++)
761          ret = LLVMBuildInsertValue(ctx->ac.builder, ret, persp_center[i], base + 4 + i, "");
762    }
763    if (key->ps_prolog.states.force_linear_center_interp) {
764       unsigned i, base = key->ps_prolog.num_input_sgprs;
765       LLVMValueRef linear_center[2];
766 
767       /* Read LINEAR_CENTER. */
768       for (i = 0; i < 2; i++)
769          linear_center[i] = LLVMGetParam(func, base + 8 + i);
770       /* Overwrite LINEAR_SAMPLE. */
771       for (i = 0; i < 2; i++)
772          ret = LLVMBuildInsertValue(ctx->ac.builder, ret, linear_center[i], base + 6 + i, "");
773       /* Overwrite LINEAR_CENTROID. */
774       for (i = 0; i < 2; i++)
775          ret = LLVMBuildInsertValue(ctx->ac.builder, ret, linear_center[i], base + 10 + i, "");
776    }
777 
778    /* Interpolate colors. */
779    unsigned color_out_idx = 0;
780    for (i = 0; i < 2; i++) {
781       unsigned writemask = (key->ps_prolog.colors_read >> (i * 4)) & 0xf;
782       unsigned face_vgpr = key->ps_prolog.num_input_sgprs + key->ps_prolog.face_vgpr_index;
783       LLVMValueRef interp[2], color[4];
784       LLVMValueRef interp_ij = NULL, prim_mask = NULL, face = NULL;
785 
786       if (!writemask)
787          continue;
788 
789       /* If the interpolation qualifier is not CONSTANT (-1). */
790       if (key->ps_prolog.color_interp_vgpr_index[i] != -1) {
791          unsigned interp_vgpr =
792             key->ps_prolog.num_input_sgprs + key->ps_prolog.color_interp_vgpr_index[i];
793 
794          /* Get the (i,j) updated by bc_optimize handling. */
795          interp[0] = LLVMBuildExtractValue(ctx->ac.builder, ret, interp_vgpr, "");
796          interp[1] = LLVMBuildExtractValue(ctx->ac.builder, ret, interp_vgpr + 1, "");
797          interp_ij = ac_build_gather_values(&ctx->ac, interp, 2);
798       }
799 
800       /* Use the absolute location of the input. */
801       prim_mask = LLVMGetParam(func, SI_PS_NUM_USER_SGPR);
802 
803       if (key->ps_prolog.states.color_two_side) {
804          face = LLVMGetParam(func, face_vgpr);
805          face = ac_to_integer(&ctx->ac, face);
806       }
807 
808       interp_fs_color(ctx, key->ps_prolog.color_attr_index[i], i, key->ps_prolog.num_interp_inputs,
809                       key->ps_prolog.colors_read, interp_ij, prim_mask, face, color);
810 
811       while (writemask) {
812          unsigned chan = u_bit_scan(&writemask);
813          ret = LLVMBuildInsertValue(ctx->ac.builder, ret, color[chan],
814                                     ctx->args.arg_count + color_out_idx++, "");
815       }
816    }
817 
818    /* Section 15.2.2 (Shader Inputs) of the OpenGL 4.5 (Core Profile) spec
819     * says:
820     *
821     *    "When per-sample shading is active due to the use of a fragment
822     *     input qualified by sample or due to the use of the gl_SampleID
823     *     or gl_SamplePosition variables, only the bit for the current
824     *     sample is set in gl_SampleMaskIn. When state specifies multiple
825     *     fragment shader invocations for a given fragment, the sample
826     *     mask for any single fragment shader invocation may specify a
827     *     subset of the covered samples for the fragment. In this case,
828     *     the bit corresponding to each covered sample will be set in
829     *     exactly one fragment shader invocation."
830     *
831     * The samplemask loaded by hardware is always the coverage of the
832     * entire pixel/fragment, so mask bits out based on the sample ID.
833     */
834    if (key->ps_prolog.states.samplemask_log_ps_iter) {
835       /* The bit pattern matches that used by fixed function fragment
836        * processing. */
837       static const uint16_t ps_iter_masks[] = {
838          0xffff, /* not used */
839          0x5555, 0x1111, 0x0101, 0x0001,
840       };
841       assert(key->ps_prolog.states.samplemask_log_ps_iter < ARRAY_SIZE(ps_iter_masks));
842 
843       uint32_t ps_iter_mask = ps_iter_masks[key->ps_prolog.states.samplemask_log_ps_iter];
844       LLVMValueRef sampleid = si_unpack_param(ctx, ancillary, 8, 4);
845       LLVMValueRef samplemask = ac_get_arg(&ctx->ac, param_sample_mask);
846 
847       samplemask = ac_to_integer(&ctx->ac, samplemask);
848       samplemask =
849          LLVMBuildAnd(ctx->ac.builder, samplemask,
850                       LLVMBuildShl(ctx->ac.builder, LLVMConstInt(ctx->ac.i32, ps_iter_mask, false),
851                                    sampleid, ""),
852                       "");
853       samplemask = ac_to_float(&ctx->ac, samplemask);
854 
855       ret = LLVMBuildInsertValue(ctx->ac.builder, ret, samplemask, param_sample_mask.arg_index, "");
856    }
857 
858    /* Tell LLVM to insert WQM instruction sequence when needed. */
859    if (key->ps_prolog.wqm) {
860       LLVMAddTargetDependentFunctionAttr(func, "amdgpu-ps-wqm-outputs", "");
861    }
862 
863    si_llvm_build_ret(ctx, ret);
864 }
865 
866 /**
867  * Build the pixel shader epilog function. This handles everything that must be
868  * emulated for pixel shader exports. (alpha-test, format conversions, etc)
869  */
si_llvm_build_ps_epilog(struct si_shader_context * ctx,union si_shader_part_key * key)870 void si_llvm_build_ps_epilog(struct si_shader_context *ctx, union si_shader_part_key *key)
871 {
872    LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
873    int i;
874    struct si_ps_exports exp = {};
875 
876    memset(&ctx->args, 0, sizeof(ctx->args));
877 
878    /* Declare input SGPRs. */
879    ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->rw_buffers);
880    ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->bindless_samplers_and_images);
881    ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->const_and_shader_buffers);
882    ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->samplers_and_images);
883    si_add_arg_checked(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL, SI_PARAM_ALPHA_REF);
884 
885    /* Declare input VGPRs. */
886    unsigned required_num_params =
887       ctx->args.num_sgprs_used + util_bitcount(key->ps_epilog.colors_written) * 4 +
888       key->ps_epilog.writes_z + key->ps_epilog.writes_stencil + key->ps_epilog.writes_samplemask;
889 
890    required_num_params =
891       MAX2(required_num_params, ctx->args.num_sgprs_used + PS_EPILOG_SAMPLEMASK_MIN_LOC + 1);
892 
893    while (ctx->args.arg_count < required_num_params)
894       ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT, NULL);
895 
896    /* Create the function. */
897    si_llvm_create_func(ctx, "ps_epilog", NULL, 0, 0);
898    /* Disable elimination of unused inputs. */
899    ac_llvm_add_target_dep_function_attr(ctx->main_fn, "InitialPSInputAddr", 0xffffff);
900 
901    /* Process colors. */
902    unsigned vgpr = ctx->args.num_sgprs_used;
903    unsigned colors_written = key->ps_epilog.colors_written;
904    int last_color_export = -1;
905 
906    /* Find the last color export. */
907    if (!key->ps_epilog.writes_z && !key->ps_epilog.writes_stencil &&
908        !key->ps_epilog.writes_samplemask) {
909       unsigned spi_format = key->ps_epilog.states.spi_shader_col_format;
910 
911       /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
912       if (colors_written == 0x1 && key->ps_epilog.states.last_cbuf > 0) {
913          /* Just set this if any of the colorbuffers are enabled. */
914          if (spi_format & ((1ull << (4 * (key->ps_epilog.states.last_cbuf + 1))) - 1))
915             last_color_export = 0;
916       } else {
917          for (i = 0; i < 8; i++)
918             if (colors_written & (1 << i) && (spi_format >> (i * 4)) & 0xf)
919                last_color_export = i;
920       }
921    }
922 
923    unsigned num_compacted_mrts = 0;
924    while (colors_written) {
925       LLVMValueRef color[4];
926       int output_index = u_bit_scan(&colors_written);
927       unsigned color_type = (key->ps_epilog.color_types >> (output_index * 2)) & 0x3;
928 
929       if (color_type != SI_TYPE_ANY32) {
930          for (i = 0; i < 4; i++) {
931             color[i] = LLVMGetParam(ctx->main_fn, vgpr + i / 2);
932             color[i] = LLVMBuildBitCast(ctx->ac.builder, color[i], ctx->ac.v2f16, "");
933             color[i] = ac_llvm_extract_elem(&ctx->ac, color[i], i % 2);
934          }
935          vgpr += 4;
936       } else {
937          for (i = 0; i < 4; i++)
938             color[i] = LLVMGetParam(ctx->main_fn, vgpr++);
939       }
940 
941       if (si_export_mrt_color(ctx, color, output_index, num_compacted_mrts,
942                               ctx->args.arg_count - 1,
943                               output_index == last_color_export, color_type, &exp))
944          num_compacted_mrts++;
945    }
946 
947    /* Process depth, stencil, samplemask. */
948    if (key->ps_epilog.writes_z)
949       depth = LLVMGetParam(ctx->main_fn, vgpr++);
950    if (key->ps_epilog.writes_stencil)
951       stencil = LLVMGetParam(ctx->main_fn, vgpr++);
952    if (key->ps_epilog.writes_samplemask)
953       samplemask = LLVMGetParam(ctx->main_fn, vgpr++);
954 
955    if (depth || stencil || samplemask)
956       ac_export_mrt_z(&ctx->ac, depth, stencil, samplemask, &exp.args[exp.num++]);
957    else if (last_color_export == -1)
958       ac_build_export_null(&ctx->ac);
959 
960    if (exp.num) {
961       for (unsigned i = 0; i < exp.num; i++)
962          ac_build_export(&ctx->ac, &exp.args[i]);
963    }
964 
965    /* Compile. */
966    LLVMBuildRetVoid(ctx->ac.builder);
967 }
968 
si_llvm_build_monolithic_ps(struct si_shader_context * ctx,struct si_shader * shader)969 void si_llvm_build_monolithic_ps(struct si_shader_context *ctx, struct si_shader *shader)
970 {
971    LLVMValueRef parts[3];
972    unsigned num_parts = 0, main_index;
973    LLVMValueRef main_fn = ctx->main_fn;
974 
975    union si_shader_part_key prolog_key;
976    si_get_ps_prolog_key(shader, &prolog_key, false);
977 
978    if (si_need_ps_prolog(&prolog_key)) {
979       si_llvm_build_ps_prolog(ctx, &prolog_key);
980       parts[num_parts++] = ctx->main_fn;
981    }
982 
983    main_index = num_parts;
984    parts[num_parts++] = main_fn;
985 
986    union si_shader_part_key epilog_key;
987    si_get_ps_epilog_key(shader, &epilog_key);
988    si_llvm_build_ps_epilog(ctx, &epilog_key);
989    parts[num_parts++] = ctx->main_fn;
990 
991    si_build_wrapper_function(ctx, parts, num_parts, main_index, 0);
992 }
993 
si_llvm_init_ps_callbacks(struct si_shader_context * ctx)994 void si_llvm_init_ps_callbacks(struct si_shader_context *ctx)
995 {
996    ctx->abi.emit_outputs = si_llvm_return_fs_outputs;
997    ctx->abi.load_sample_position = load_sample_position;
998    ctx->abi.load_sample_mask_in = load_sample_mask_in;
999    ctx->abi.emit_fbfetch = si_nir_emit_fbfetch;
1000 }
1001