Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_shader_llvm_ps.c
4570 views
/*1* Copyright 2020 Advanced Micro Devices, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE.22*/2324#include "si_pipe.h"25#include "si_shader_internal.h"26#include "sid.h"2728LLVMValueRef si_get_sample_id(struct si_shader_context *ctx)29{30return si_unpack_param(ctx, ctx->args.ancillary, 8, 4);31}3233static LLVMValueRef load_sample_mask_in(struct ac_shader_abi *abi)34{35struct si_shader_context *ctx = si_shader_context_from_abi(abi);36return ac_to_integer(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args.sample_coverage));37}3839static LLVMValueRef load_sample_position(struct ac_shader_abi *abi, LLVMValueRef sample_id)40{41struct si_shader_context *ctx = si_shader_context_from_abi(abi);42LLVMValueRef desc = ac_get_arg(&ctx->ac, ctx->internal_bindings);43LLVMValueRef buf_index = LLVMConstInt(ctx->ac.i32, SI_PS_CONST_SAMPLE_POSITIONS, 0);44LLVMValueRef resource = ac_build_load_to_sgpr(&ctx->ac, desc, buf_index);4546/* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */47LLVMValueRef offset0 =48LLVMBuildMul(ctx->ac.builder, sample_id, LLVMConstInt(ctx->ac.i32, 8, 0), "");49LLVMValueRef offset1 =50LLVMBuildAdd(ctx->ac.builder, offset0, LLVMConstInt(ctx->ac.i32, 4, 0), "");5152LLVMValueRef pos[4] = {si_buffer_load_const(ctx, resource, offset0),53si_buffer_load_const(ctx, resource, offset1),54LLVMConstReal(ctx->ac.f32, 0), LLVMConstReal(ctx->ac.f32, 0)};5556return ac_build_gather_values(&ctx->ac, pos, 4);57}5859static LLVMValueRef si_nir_emit_fbfetch(struct ac_shader_abi *abi)60{61struct si_shader_context *ctx = si_shader_context_from_abi(abi);62struct ac_image_args args = {};63LLVMValueRef ptr, image, fmask;6465/* Ignore src0, because KHR_blend_func_extended disallows multiple render66* targets.67*/6869/* Load the image descriptor. */70STATIC_ASSERT(SI_PS_IMAGE_COLORBUF0 % 2 == 0);71ptr = ac_get_arg(&ctx->ac, ctx->internal_bindings);72ptr =73LLVMBuildPointerCast(ctx->ac.builder, ptr, ac_array_in_const32_addr_space(ctx->ac.v8i32), "");74image =75ac_build_load_to_sgpr(&ctx->ac, ptr, LLVMConstInt(ctx->ac.i32, SI_PS_IMAGE_COLORBUF0 / 2, 0));7677unsigned chan = 0;7879args.coords[chan++] = si_unpack_param(ctx, ctx->pos_fixed_pt, 0, 16);8081if (!ctx->shader->key.mono.u.ps.fbfetch_is_1D)82args.coords[chan++] = si_unpack_param(ctx, ctx->pos_fixed_pt, 16, 16);8384/* Get the current render target layer index. */85if (ctx->shader->key.mono.u.ps.fbfetch_layered)86args.coords[chan++] = si_unpack_param(ctx, ctx->args.ancillary, 16, 11);8788if (ctx->shader->key.mono.u.ps.fbfetch_msaa)89args.coords[chan++] = si_get_sample_id(ctx);9091if (ctx->shader->key.mono.u.ps.fbfetch_msaa && !(ctx->screen->debug_flags & DBG(NO_FMASK))) {92fmask = ac_build_load_to_sgpr(&ctx->ac, ptr,93LLVMConstInt(ctx->ac.i32, SI_PS_IMAGE_COLORBUF0_FMASK / 2, 0));9495ac_apply_fmask_to_sample(&ctx->ac, fmask, args.coords,96ctx->shader->key.mono.u.ps.fbfetch_layered);97}9899args.opcode = ac_image_load;100args.resource = image;101args.dmask = 0xf;102args.attributes = AC_FUNC_ATTR_READNONE;103104if (ctx->shader->key.mono.u.ps.fbfetch_msaa)105args.dim =106ctx->shader->key.mono.u.ps.fbfetch_layered ? ac_image_2darraymsaa : ac_image_2dmsaa;107else if (ctx->shader->key.mono.u.ps.fbfetch_is_1D)108args.dim = ctx->shader->key.mono.u.ps.fbfetch_layered ? ac_image_1darray : ac_image_1d;109else110args.dim = ctx->shader->key.mono.u.ps.fbfetch_layered ? ac_image_2darray : ac_image_2d;111112return ac_build_image_opcode(&ctx->ac, &args);113}114115static LLVMValueRef si_build_fs_interp(struct si_shader_context *ctx, unsigned attr_index,116unsigned chan, LLVMValueRef prim_mask, LLVMValueRef i,117LLVMValueRef j)118{119if (i || j) {120return ac_build_fs_interp(&ctx->ac, LLVMConstInt(ctx->ac.i32, chan, 0),121LLVMConstInt(ctx->ac.i32, attr_index, 0), prim_mask, i, j);122}123return ac_build_fs_interp_mov(&ctx->ac, LLVMConstInt(ctx->ac.i32, 2, 0), /* P0 */124LLVMConstInt(ctx->ac.i32, chan, 0),125LLVMConstInt(ctx->ac.i32, attr_index, 0), prim_mask);126}127128/**129* Interpolate a fragment shader input.130*131* @param ctx context132* @param input_index index of the input in hardware133* @param semantic_index semantic index134* @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)135* @param colors_read_mask color components read (4 bits for each color, 8 bits in total)136* @param interp_param interpolation weights (i,j)137* @param prim_mask SI_PARAM_PRIM_MASK138* @param face SI_PARAM_FRONT_FACE139* @param result the return value (4 components)140*/141static void interp_fs_color(struct si_shader_context *ctx, unsigned input_index,142unsigned semantic_index, unsigned num_interp_inputs,143unsigned colors_read_mask, LLVMValueRef interp_param,144LLVMValueRef prim_mask, LLVMValueRef face, LLVMValueRef result[4])145{146LLVMValueRef i = NULL, j = NULL;147unsigned chan;148149/* fs.constant returns the param from the middle vertex, so it's not150* really useful for flat shading. It's meant to be used for custom151* interpolation (but the intrinsic can't fetch from the other two152* vertices).153*154* Luckily, it doesn't matter, because we rely on the FLAT_SHADE state155* to do the right thing. The only reason we use fs.constant is that156* fs.interp cannot be used on integers, because they can be equal157* to NaN.158*159* When interp is false we will use fs.constant or for newer llvm,160* amdgcn.interp.mov.161*/162bool interp = interp_param != NULL;163164if (interp) {165interp_param =166LLVMBuildBitCast(ctx->ac.builder, interp_param, ctx->ac.v2f32, "");167168i = LLVMBuildExtractElement(ctx->ac.builder, interp_param, ctx->ac.i32_0, "");169j = LLVMBuildExtractElement(ctx->ac.builder, interp_param, ctx->ac.i32_1, "");170}171172if (ctx->shader->key.part.ps.prolog.color_two_side) {173LLVMValueRef is_face_positive;174175/* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",176* otherwise it's at offset "num_inputs".177*/178unsigned back_attr_offset = num_interp_inputs;179if (semantic_index == 1 && colors_read_mask & 0xf)180back_attr_offset += 1;181182is_face_positive = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE, face, ctx->ac.i32_0, "");183184for (chan = 0; chan < 4; chan++) {185LLVMValueRef front, back;186187front = si_build_fs_interp(ctx, input_index, chan, prim_mask, i, j);188back = si_build_fs_interp(ctx, back_attr_offset, chan, prim_mask, i, j);189190result[chan] = LLVMBuildSelect(ctx->ac.builder, is_face_positive, front, back, "");191}192} else {193for (chan = 0; chan < 4; chan++) {194result[chan] = si_build_fs_interp(ctx, input_index, chan, prim_mask, i, j);195}196}197}198199static void si_alpha_test(struct si_shader_context *ctx, LLVMValueRef alpha)200{201if (ctx->shader->key.part.ps.epilog.alpha_func != PIPE_FUNC_NEVER) {202static LLVMRealPredicate cond_map[PIPE_FUNC_ALWAYS + 1] = {203[PIPE_FUNC_LESS] = LLVMRealOLT, [PIPE_FUNC_EQUAL] = LLVMRealOEQ,204[PIPE_FUNC_LEQUAL] = LLVMRealOLE, [PIPE_FUNC_GREATER] = LLVMRealOGT,205[PIPE_FUNC_NOTEQUAL] = LLVMRealONE, [PIPE_FUNC_GEQUAL] = LLVMRealOGE,206};207LLVMRealPredicate cond = cond_map[ctx->shader->key.part.ps.epilog.alpha_func];208assert(cond);209210LLVMValueRef alpha_ref = LLVMGetParam(ctx->main_fn, SI_PARAM_ALPHA_REF);211if (LLVMTypeOf(alpha) == ctx->ac.f16)212alpha_ref = LLVMBuildFPTrunc(ctx->ac.builder, alpha_ref, ctx->ac.f16, "");213214LLVMValueRef alpha_pass = LLVMBuildFCmp(ctx->ac.builder, cond, alpha, alpha_ref, "");215ac_build_kill_if_false(&ctx->ac, alpha_pass);216} else {217ac_build_kill_if_false(&ctx->ac, ctx->ac.i1false);218}219}220221static LLVMValueRef si_scale_alpha_by_sample_mask(struct si_shader_context *ctx, LLVMValueRef alpha,222unsigned samplemask_param)223{224LLVMValueRef coverage;225226/* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */227coverage = LLVMGetParam(ctx->main_fn, samplemask_param);228coverage = ac_build_bit_count(&ctx->ac, ac_to_integer(&ctx->ac, coverage));229coverage = LLVMBuildUIToFP(ctx->ac.builder, coverage, ctx->ac.f32, "");230231coverage = LLVMBuildFMul(ctx->ac.builder, coverage,232LLVMConstReal(ctx->ac.f32, 1.0 / SI_NUM_SMOOTH_AA_SAMPLES), "");233234if (LLVMTypeOf(alpha) == ctx->ac.f16)235coverage = LLVMBuildFPTrunc(ctx->ac.builder, coverage, ctx->ac.f16, "");236237return LLVMBuildFMul(ctx->ac.builder, alpha, coverage, "");238}239240struct si_ps_exports {241unsigned num;242struct ac_export_args args[10];243};244245static LLVMValueRef pack_two_16bit(struct ac_llvm_context *ctx, LLVMValueRef args[2])246{247LLVMValueRef tmp = ac_build_gather_values(ctx, args, 2);248return LLVMBuildBitCast(ctx->builder, tmp, ctx->v2f16, "");249}250251static LLVMValueRef get_color_32bit(struct si_shader_context *ctx, unsigned color_type,252LLVMValueRef value)253{254switch (color_type) {255case SI_TYPE_FLOAT16:256return LLVMBuildFPExt(ctx->ac.builder, value, ctx->ac.f32, "");257case SI_TYPE_INT16:258value = ac_to_integer(&ctx->ac, value);259value = LLVMBuildSExt(ctx->ac.builder, value, ctx->ac.i32, "");260return ac_to_float(&ctx->ac, value);261case SI_TYPE_UINT16:262value = ac_to_integer(&ctx->ac, value);263value = LLVMBuildZExt(ctx->ac.builder, value, ctx->ac.i32, "");264return ac_to_float(&ctx->ac, value);265case SI_TYPE_ANY32:266return value;267}268return NULL;269}270271/* Initialize arguments for the shader export intrinsic */272static void si_llvm_init_ps_export_args(struct si_shader_context *ctx, LLVMValueRef *values,273unsigned cbuf, unsigned compacted_mrt_index,274unsigned color_type, struct ac_export_args *args)275{276const struct si_shader_key *key = &ctx->shader->key;277unsigned col_formats = key->part.ps.epilog.spi_shader_col_format;278LLVMValueRef f32undef = LLVMGetUndef(ctx->ac.f32);279unsigned spi_shader_col_format;280unsigned chan;281bool is_int8, is_int10;282283assert(cbuf < 8);284285spi_shader_col_format = (col_formats >> (cbuf * 4)) & 0xf;286is_int8 = (key->part.ps.epilog.color_is_int8 >> cbuf) & 0x1;287is_int10 = (key->part.ps.epilog.color_is_int10 >> cbuf) & 0x1;288289/* Default is 0xf. Adjusted below depending on the format. */290args->enabled_channels = 0xf; /* writemask */291292/* Specify whether the EXEC mask represents the valid mask */293args->valid_mask = 0;294295/* Specify whether this is the last export */296args->done = 0;297298/* Specify the target we are exporting */299args->target = V_008DFC_SQ_EXP_MRT + compacted_mrt_index;300301args->compr = false;302args->out[0] = f32undef;303args->out[1] = f32undef;304args->out[2] = f32undef;305args->out[3] = f32undef;306307LLVMValueRef (*packf)(struct ac_llvm_context * ctx, LLVMValueRef args[2]) = NULL;308LLVMValueRef (*packi)(struct ac_llvm_context * ctx, LLVMValueRef args[2], unsigned bits,309bool hi) = NULL;310311switch (spi_shader_col_format) {312case V_028714_SPI_SHADER_ZERO:313args->enabled_channels = 0; /* writemask */314args->target = V_008DFC_SQ_EXP_NULL;315break;316317case V_028714_SPI_SHADER_32_R:318args->enabled_channels = 1; /* writemask */319args->out[0] = get_color_32bit(ctx, color_type, values[0]);320break;321322case V_028714_SPI_SHADER_32_GR:323args->enabled_channels = 0x3; /* writemask */324args->out[0] = get_color_32bit(ctx, color_type, values[0]);325args->out[1] = get_color_32bit(ctx, color_type, values[1]);326break;327328case V_028714_SPI_SHADER_32_AR:329if (ctx->screen->info.chip_class >= GFX10) {330args->enabled_channels = 0x3; /* writemask */331args->out[0] = get_color_32bit(ctx, color_type, values[0]);332args->out[1] = get_color_32bit(ctx, color_type, values[3]);333} else {334args->enabled_channels = 0x9; /* writemask */335args->out[0] = get_color_32bit(ctx, color_type, values[0]);336args->out[3] = get_color_32bit(ctx, color_type, values[3]);337}338break;339340case V_028714_SPI_SHADER_FP16_ABGR:341if (color_type != SI_TYPE_ANY32)342packf = pack_two_16bit;343else344packf = ac_build_cvt_pkrtz_f16;345break;346347case V_028714_SPI_SHADER_UNORM16_ABGR:348if (color_type != SI_TYPE_ANY32)349packf = ac_build_cvt_pknorm_u16_f16;350else351packf = ac_build_cvt_pknorm_u16;352break;353354case V_028714_SPI_SHADER_SNORM16_ABGR:355if (color_type != SI_TYPE_ANY32)356packf = ac_build_cvt_pknorm_i16_f16;357else358packf = ac_build_cvt_pknorm_i16;359break;360361case V_028714_SPI_SHADER_UINT16_ABGR:362if (color_type != SI_TYPE_ANY32)363packf = pack_two_16bit;364else365packi = ac_build_cvt_pk_u16;366break;367368case V_028714_SPI_SHADER_SINT16_ABGR:369if (color_type != SI_TYPE_ANY32)370packf = pack_two_16bit;371else372packi = ac_build_cvt_pk_i16;373break;374375case V_028714_SPI_SHADER_32_ABGR:376for (unsigned i = 0; i < 4; i++)377args->out[i] = get_color_32bit(ctx, color_type, values[i]);378break;379}380381/* Pack f16 or norm_i16/u16. */382if (packf) {383for (chan = 0; chan < 2; chan++) {384LLVMValueRef pack_args[2] = {values[2 * chan], values[2 * chan + 1]};385LLVMValueRef packed;386387packed = packf(&ctx->ac, pack_args);388args->out[chan] = ac_to_float(&ctx->ac, packed);389}390args->compr = 1; /* COMPR flag */391}392/* Pack i16/u16. */393if (packi) {394for (chan = 0; chan < 2; chan++) {395LLVMValueRef pack_args[2] = {ac_to_integer(&ctx->ac, values[2 * chan]),396ac_to_integer(&ctx->ac, values[2 * chan + 1])};397LLVMValueRef packed;398399packed = packi(&ctx->ac, pack_args, is_int8 ? 8 : is_int10 ? 10 : 16, chan == 1);400args->out[chan] = ac_to_float(&ctx->ac, packed);401}402args->compr = 1; /* COMPR flag */403}404}405406static bool si_export_mrt_color(struct si_shader_context *ctx, LLVMValueRef *color, unsigned index,407unsigned compacted_mrt_index, unsigned samplemask_param,408bool is_last, unsigned color_type, struct si_ps_exports *exp)409{410int i;411412/* Clamp color */413if (ctx->shader->key.part.ps.epilog.clamp_color)414for (i = 0; i < 4; i++)415color[i] = ac_build_clamp(&ctx->ac, color[i]);416417/* Alpha to one */418if (ctx->shader->key.part.ps.epilog.alpha_to_one)419color[3] = LLVMConstReal(LLVMTypeOf(color[0]), 1);420421/* Alpha test */422if (index == 0 && ctx->shader->key.part.ps.epilog.alpha_func != PIPE_FUNC_ALWAYS)423si_alpha_test(ctx, color[3]);424425/* Line & polygon smoothing */426if (ctx->shader->key.part.ps.epilog.poly_line_smoothing)427color[3] = si_scale_alpha_by_sample_mask(ctx, color[3], samplemask_param);428429/* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */430if (ctx->shader->key.part.ps.epilog.last_cbuf > 0) {431struct ac_export_args args[8];432int c, last = -1;433434assert(compacted_mrt_index == 0);435436/* Get the export arguments, also find out what the last one is. */437for (c = 0; c <= ctx->shader->key.part.ps.epilog.last_cbuf; c++) {438si_llvm_init_ps_export_args(ctx, color, c, compacted_mrt_index,439color_type, &args[c]);440if (args[c].enabled_channels) {441compacted_mrt_index++;442last = c;443}444}445if (last == -1)446return false;447448/* Emit all exports. */449for (c = 0; c <= ctx->shader->key.part.ps.epilog.last_cbuf; c++) {450if (is_last && last == c) {451args[c].valid_mask = 1; /* whether the EXEC mask is valid */452args[c].done = 1; /* DONE bit */453} else if (!args[c].enabled_channels)454continue; /* unnecessary NULL export */455456memcpy(&exp->args[exp->num++], &args[c], sizeof(args[c]));457}458} else {459struct ac_export_args args;460461/* Export */462si_llvm_init_ps_export_args(ctx, color, index, compacted_mrt_index,463color_type, &args);464if (is_last) {465args.valid_mask = 1; /* whether the EXEC mask is valid */466args.done = 1; /* DONE bit */467} else if (!args.enabled_channels)468return false; /* unnecessary NULL export */469470memcpy(&exp->args[exp->num++], &args, sizeof(args));471}472return true;473}474475/**476* Return PS outputs in this order:477*478* v[0:3] = color0.xyzw479* v[4:7] = color1.xyzw480* ...481* vN+0 = Depth482* vN+1 = Stencil483* vN+2 = SampleMask484* vN+3 = SampleMaskIn (used for OpenGL smoothing)485*486* The alpha-ref SGPR is returned via its original location.487*/488static void si_llvm_return_fs_outputs(struct ac_shader_abi *abi, unsigned max_outputs,489LLVMValueRef *addrs)490{491struct si_shader_context *ctx = si_shader_context_from_abi(abi);492struct si_shader *shader = ctx->shader;493struct si_shader_info *info = &shader->selector->info;494LLVMBuilderRef builder = ctx->ac.builder;495unsigned i, j, first_vgpr, vgpr;496497LLVMValueRef color[8][4] = {};498LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;499LLVMValueRef ret;500501/* Read the output values. */502for (i = 0; i < info->num_outputs; i++) {503unsigned semantic = info->output_semantic[i];504505switch (semantic) {506case FRAG_RESULT_DEPTH:507depth = LLVMBuildLoad(builder, addrs[4 * i + 0], "");508break;509case FRAG_RESULT_STENCIL:510stencil = LLVMBuildLoad(builder, addrs[4 * i + 0], "");511break;512case FRAG_RESULT_SAMPLE_MASK:513samplemask = LLVMBuildLoad(builder, addrs[4 * i + 0], "");514break;515default:516if (semantic >= FRAG_RESULT_DATA0 && semantic <= FRAG_RESULT_DATA7) {517unsigned index = semantic - FRAG_RESULT_DATA0;518519for (j = 0; j < 4; j++) {520LLVMValueRef ptr = addrs[4 * i + j];521LLVMValueRef result = LLVMBuildLoad(builder, ptr, "");522color[index][j] = result;523}524} else {525fprintf(stderr, "Warning: Unhandled fs output type:%d\n", semantic);526}527break;528}529}530531/* Fill the return structure. */532ret = ctx->return_value;533534/* Set SGPRs. */535ret = LLVMBuildInsertValue(536builder, ret, ac_to_integer(&ctx->ac, LLVMGetParam(ctx->main_fn, SI_PARAM_ALPHA_REF)),537SI_SGPR_ALPHA_REF, "");538539/* Set VGPRs */540first_vgpr = vgpr = SI_SGPR_ALPHA_REF + 1;541for (i = 0; i < ARRAY_SIZE(color); i++) {542if (!color[i][0])543continue;544545if (LLVMTypeOf(color[i][0]) == ctx->ac.f16) {546for (j = 0; j < 2; j++) {547LLVMValueRef tmp = ac_build_gather_values(&ctx->ac, &color[i][j * 2], 2);548tmp = LLVMBuildBitCast(builder, tmp, ctx->ac.f32, "");549ret = LLVMBuildInsertValue(builder, ret, tmp, vgpr++, "");550}551vgpr += 2;552} else {553for (j = 0; j < 4; j++)554ret = LLVMBuildInsertValue(builder, ret, color[i][j], vgpr++, "");555}556}557if (depth)558ret = LLVMBuildInsertValue(builder, ret, depth, vgpr++, "");559if (stencil)560ret = LLVMBuildInsertValue(builder, ret, stencil, vgpr++, "");561if (samplemask)562ret = LLVMBuildInsertValue(builder, ret, samplemask, vgpr++, "");563564/* Add the input sample mask for smoothing at the end. */565if (vgpr < first_vgpr + PS_EPILOG_SAMPLEMASK_MIN_LOC)566vgpr = first_vgpr + PS_EPILOG_SAMPLEMASK_MIN_LOC;567ret = LLVMBuildInsertValue(builder, ret, LLVMGetParam(ctx->main_fn, SI_PARAM_SAMPLE_COVERAGE),568vgpr++, "");569570ctx->return_value = ret;571}572573static void si_llvm_emit_polygon_stipple(struct si_shader_context *ctx,574LLVMValueRef param_internal_bindings,575struct ac_arg param_pos_fixed_pt)576{577LLVMBuilderRef builder = ctx->ac.builder;578LLVMValueRef slot, desc, offset, row, bit, address[2];579580/* Use the fixed-point gl_FragCoord input.581* Since the stipple pattern is 32x32 and it repeats, just get 5 bits582* per coordinate to get the repeating effect.583*/584address[0] = si_unpack_param(ctx, param_pos_fixed_pt, 0, 5);585address[1] = si_unpack_param(ctx, param_pos_fixed_pt, 16, 5);586587/* Load the buffer descriptor. */588slot = LLVMConstInt(ctx->ac.i32, SI_PS_CONST_POLY_STIPPLE, 0);589desc = ac_build_load_to_sgpr(&ctx->ac, param_internal_bindings, slot);590591/* The stipple pattern is 32x32, each row has 32 bits. */592offset = LLVMBuildMul(builder, address[1], LLVMConstInt(ctx->ac.i32, 4, 0), "");593row = si_buffer_load_const(ctx, desc, offset);594row = ac_to_integer(&ctx->ac, row);595bit = LLVMBuildLShr(builder, row, address[0], "");596bit = LLVMBuildTrunc(builder, bit, ctx->ac.i1, "");597ac_build_kill_if_false(&ctx->ac, bit);598}599600/**601* Build the pixel shader prolog function. This handles:602* - two-side color selection and interpolation603* - overriding interpolation parameters for the API PS604* - polygon stippling605*606* All preloaded SGPRs and VGPRs are passed through unmodified unless they are607* overriden by other states. (e.g. per-sample interpolation)608* Interpolated colors are stored after the preloaded VGPRs.609*/610void si_llvm_build_ps_prolog(struct si_shader_context *ctx, union si_shader_part_key *key)611{612LLVMValueRef ret, func;613int num_returns, i, num_color_channels;614615memset(&ctx->args, 0, sizeof(ctx->args));616617/* Declare inputs. */618LLVMTypeRef return_types[AC_MAX_ARGS];619num_returns = 0;620num_color_channels = util_bitcount(key->ps_prolog.colors_read);621assert(key->ps_prolog.num_input_sgprs + key->ps_prolog.num_input_vgprs + num_color_channels <=622AC_MAX_ARGS);623for (i = 0; i < key->ps_prolog.num_input_sgprs; i++) {624ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);625return_types[num_returns++] = ctx->ac.i32;626}627628struct ac_arg pos_fixed_pt;629struct ac_arg ancillary;630struct ac_arg param_sample_mask;631for (i = 0; i < key->ps_prolog.num_input_vgprs; i++) {632struct ac_arg *arg = NULL;633if (i == key->ps_prolog.ancillary_vgpr_index) {634arg = &ancillary;635} else if (i == key->ps_prolog.ancillary_vgpr_index + 1) {636arg = ¶m_sample_mask;637} else if (i == key->ps_prolog.num_input_vgprs - 1) {638/* POS_FIXED_PT is always last. */639arg = &pos_fixed_pt;640}641ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT, arg);642return_types[num_returns++] = ctx->ac.f32;643}644645/* Declare outputs (same as inputs + add colors if needed) */646for (i = 0; i < num_color_channels; i++)647return_types[num_returns++] = ctx->ac.f32;648649/* Create the function. */650si_llvm_create_func(ctx, "ps_prolog", return_types, num_returns, 0);651func = ctx->main_fn;652653/* Copy inputs to outputs. This should be no-op, as the registers match,654* but it will prevent the compiler from overwriting them unintentionally.655*/656ret = ctx->return_value;657for (i = 0; i < ctx->args.arg_count; i++) {658LLVMValueRef p = LLVMGetParam(func, i);659ret = LLVMBuildInsertValue(ctx->ac.builder, ret, p, i, "");660}661662/* Polygon stippling. */663if (key->ps_prolog.states.poly_stipple) {664LLVMValueRef list = si_prolog_get_internal_bindings(ctx);665666si_llvm_emit_polygon_stipple(ctx, list, pos_fixed_pt);667}668669if (key->ps_prolog.states.bc_optimize_for_persp ||670key->ps_prolog.states.bc_optimize_for_linear) {671unsigned i, base = key->ps_prolog.num_input_sgprs;672LLVMValueRef center[2], centroid[2], tmp, bc_optimize;673674/* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;675* The hw doesn't compute CENTROID if the whole wave only676* contains fully-covered quads.677*678* PRIM_MASK is after user SGPRs.679*/680bc_optimize = LLVMGetParam(func, SI_PS_NUM_USER_SGPR);681bc_optimize =682LLVMBuildLShr(ctx->ac.builder, bc_optimize, LLVMConstInt(ctx->ac.i32, 31, 0), "");683bc_optimize = LLVMBuildTrunc(ctx->ac.builder, bc_optimize, ctx->ac.i1, "");684685if (key->ps_prolog.states.bc_optimize_for_persp) {686/* Read PERSP_CENTER. */687for (i = 0; i < 2; i++)688center[i] = LLVMGetParam(func, base + 2 + i);689/* Read PERSP_CENTROID. */690for (i = 0; i < 2; i++)691centroid[i] = LLVMGetParam(func, base + 4 + i);692/* Select PERSP_CENTROID. */693for (i = 0; i < 2; i++) {694tmp = LLVMBuildSelect(ctx->ac.builder, bc_optimize, center[i], centroid[i], "");695ret = LLVMBuildInsertValue(ctx->ac.builder, ret, tmp, base + 4 + i, "");696}697}698if (key->ps_prolog.states.bc_optimize_for_linear) {699/* Read LINEAR_CENTER. */700for (i = 0; i < 2; i++)701center[i] = LLVMGetParam(func, base + 8 + i);702/* Read LINEAR_CENTROID. */703for (i = 0; i < 2; i++)704centroid[i] = LLVMGetParam(func, base + 10 + i);705/* Select LINEAR_CENTROID. */706for (i = 0; i < 2; i++) {707tmp = LLVMBuildSelect(ctx->ac.builder, bc_optimize, center[i], centroid[i], "");708ret = LLVMBuildInsertValue(ctx->ac.builder, ret, tmp, base + 10 + i, "");709}710}711}712713/* Force per-sample interpolation. */714if (key->ps_prolog.states.force_persp_sample_interp) {715unsigned i, base = key->ps_prolog.num_input_sgprs;716LLVMValueRef persp_sample[2];717718/* Read PERSP_SAMPLE. */719for (i = 0; i < 2; i++)720persp_sample[i] = LLVMGetParam(func, base + i);721/* Overwrite PERSP_CENTER. */722for (i = 0; i < 2; i++)723ret = LLVMBuildInsertValue(ctx->ac.builder, ret, persp_sample[i], base + 2 + i, "");724/* Overwrite PERSP_CENTROID. */725for (i = 0; i < 2; i++)726ret = LLVMBuildInsertValue(ctx->ac.builder, ret, persp_sample[i], base + 4 + i, "");727}728if (key->ps_prolog.states.force_linear_sample_interp) {729unsigned i, base = key->ps_prolog.num_input_sgprs;730LLVMValueRef linear_sample[2];731732/* Read LINEAR_SAMPLE. */733for (i = 0; i < 2; i++)734linear_sample[i] = LLVMGetParam(func, base + 6 + i);735/* Overwrite LINEAR_CENTER. */736for (i = 0; i < 2; i++)737ret = LLVMBuildInsertValue(ctx->ac.builder, ret, linear_sample[i], base + 8 + i, "");738/* Overwrite LINEAR_CENTROID. */739for (i = 0; i < 2; i++)740ret = LLVMBuildInsertValue(ctx->ac.builder, ret, linear_sample[i], base + 10 + i, "");741}742743/* Force center interpolation. */744if (key->ps_prolog.states.force_persp_center_interp) {745unsigned i, base = key->ps_prolog.num_input_sgprs;746LLVMValueRef persp_center[2];747748/* Read PERSP_CENTER. */749for (i = 0; i < 2; i++)750persp_center[i] = LLVMGetParam(func, base + 2 + i);751/* Overwrite PERSP_SAMPLE. */752for (i = 0; i < 2; i++)753ret = LLVMBuildInsertValue(ctx->ac.builder, ret, persp_center[i], base + i, "");754/* Overwrite PERSP_CENTROID. */755for (i = 0; i < 2; i++)756ret = LLVMBuildInsertValue(ctx->ac.builder, ret, persp_center[i], base + 4 + i, "");757}758if (key->ps_prolog.states.force_linear_center_interp) {759unsigned i, base = key->ps_prolog.num_input_sgprs;760LLVMValueRef linear_center[2];761762/* Read LINEAR_CENTER. */763for (i = 0; i < 2; i++)764linear_center[i] = LLVMGetParam(func, base + 8 + i);765/* Overwrite LINEAR_SAMPLE. */766for (i = 0; i < 2; i++)767ret = LLVMBuildInsertValue(ctx->ac.builder, ret, linear_center[i], base + 6 + i, "");768/* Overwrite LINEAR_CENTROID. */769for (i = 0; i < 2; i++)770ret = LLVMBuildInsertValue(ctx->ac.builder, ret, linear_center[i], base + 10 + i, "");771}772773/* Interpolate colors. */774unsigned color_out_idx = 0;775for (i = 0; i < 2; i++) {776unsigned writemask = (key->ps_prolog.colors_read >> (i * 4)) & 0xf;777unsigned face_vgpr = key->ps_prolog.num_input_sgprs + key->ps_prolog.face_vgpr_index;778LLVMValueRef interp[2], color[4];779LLVMValueRef interp_ij = NULL, prim_mask = NULL, face = NULL;780781if (!writemask)782continue;783784/* If the interpolation qualifier is not CONSTANT (-1). */785if (key->ps_prolog.color_interp_vgpr_index[i] != -1) {786unsigned interp_vgpr =787key->ps_prolog.num_input_sgprs + key->ps_prolog.color_interp_vgpr_index[i];788789/* Get the (i,j) updated by bc_optimize handling. */790interp[0] = LLVMBuildExtractValue(ctx->ac.builder, ret, interp_vgpr, "");791interp[1] = LLVMBuildExtractValue(ctx->ac.builder, ret, interp_vgpr + 1, "");792interp_ij = ac_build_gather_values(&ctx->ac, interp, 2);793}794795/* Use the absolute location of the input. */796prim_mask = LLVMGetParam(func, SI_PS_NUM_USER_SGPR);797798if (key->ps_prolog.states.color_two_side) {799face = LLVMGetParam(func, face_vgpr);800face = ac_to_integer(&ctx->ac, face);801}802803interp_fs_color(ctx, key->ps_prolog.color_attr_index[i], i, key->ps_prolog.num_interp_inputs,804key->ps_prolog.colors_read, interp_ij, prim_mask, face, color);805806while (writemask) {807unsigned chan = u_bit_scan(&writemask);808ret = LLVMBuildInsertValue(ctx->ac.builder, ret, color[chan],809ctx->args.arg_count + color_out_idx++, "");810}811}812813/* Section 15.2.2 (Shader Inputs) of the OpenGL 4.5 (Core Profile) spec814* says:815*816* "When per-sample shading is active due to the use of a fragment817* input qualified by sample or due to the use of the gl_SampleID818* or gl_SamplePosition variables, only the bit for the current819* sample is set in gl_SampleMaskIn. When state specifies multiple820* fragment shader invocations for a given fragment, the sample821* mask for any single fragment shader invocation may specify a822* subset of the covered samples for the fragment. In this case,823* the bit corresponding to each covered sample will be set in824* exactly one fragment shader invocation."825*826* The samplemask loaded by hardware is always the coverage of the827* entire pixel/fragment, so mask bits out based on the sample ID.828*/829if (key->ps_prolog.states.samplemask_log_ps_iter) {830/* The bit pattern matches that used by fixed function fragment831* processing. */832static const uint16_t ps_iter_masks[] = {8330xffff, /* not used */8340x5555, 0x1111, 0x0101, 0x0001,835};836assert(key->ps_prolog.states.samplemask_log_ps_iter < ARRAY_SIZE(ps_iter_masks));837838uint32_t ps_iter_mask = ps_iter_masks[key->ps_prolog.states.samplemask_log_ps_iter];839LLVMValueRef sampleid = si_unpack_param(ctx, ancillary, 8, 4);840LLVMValueRef samplemask = ac_get_arg(&ctx->ac, param_sample_mask);841842samplemask = ac_to_integer(&ctx->ac, samplemask);843samplemask =844LLVMBuildAnd(ctx->ac.builder, samplemask,845LLVMBuildShl(ctx->ac.builder, LLVMConstInt(ctx->ac.i32, ps_iter_mask, false),846sampleid, ""),847"");848samplemask = ac_to_float(&ctx->ac, samplemask);849850ret = LLVMBuildInsertValue(ctx->ac.builder, ret, samplemask, param_sample_mask.arg_index, "");851}852853/* Tell LLVM to insert WQM instruction sequence when needed. */854if (key->ps_prolog.wqm) {855LLVMAddTargetDependentFunctionAttr(func, "amdgpu-ps-wqm-outputs", "");856}857858si_llvm_build_ret(ctx, ret);859}860861/**862* Build the pixel shader epilog function. This handles everything that must be863* emulated for pixel shader exports. (alpha-test, format conversions, etc)864*/865void si_llvm_build_ps_epilog(struct si_shader_context *ctx, union si_shader_part_key *key)866{867LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;868int i;869struct si_ps_exports exp = {};870871memset(&ctx->args, 0, sizeof(ctx->args));872873/* Declare input SGPRs. */874ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->internal_bindings);875ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->bindless_samplers_and_images);876ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->const_and_shader_buffers);877ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->samplers_and_images);878si_add_arg_checked(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL, SI_PARAM_ALPHA_REF);879880/* Declare input VGPRs. */881unsigned required_num_params =882ctx->args.num_sgprs_used + util_bitcount(key->ps_epilog.colors_written) * 4 +883key->ps_epilog.writes_z + key->ps_epilog.writes_stencil + key->ps_epilog.writes_samplemask;884885required_num_params =886MAX2(required_num_params, ctx->args.num_sgprs_used + PS_EPILOG_SAMPLEMASK_MIN_LOC + 1);887888while (ctx->args.arg_count < required_num_params)889ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT, NULL);890891/* Create the function. */892si_llvm_create_func(ctx, "ps_epilog", NULL, 0, 0);893/* Disable elimination of unused inputs. */894ac_llvm_add_target_dep_function_attr(ctx->main_fn, "InitialPSInputAddr", 0xffffff);895896/* Process colors. */897unsigned vgpr = ctx->args.num_sgprs_used;898unsigned colors_written = key->ps_epilog.colors_written;899int last_color_export = -1;900901/* Find the last color export. */902if (!key->ps_epilog.writes_z && !key->ps_epilog.writes_stencil &&903!key->ps_epilog.writes_samplemask) {904unsigned spi_format = key->ps_epilog.states.spi_shader_col_format;905906/* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */907if (colors_written == 0x1 && key->ps_epilog.states.last_cbuf > 0) {908/* Just set this if any of the colorbuffers are enabled. */909if (spi_format & ((1ull << (4 * (key->ps_epilog.states.last_cbuf + 1))) - 1))910last_color_export = 0;911} else {912for (i = 0; i < 8; i++)913if (colors_written & (1 << i) && (spi_format >> (i * 4)) & 0xf)914last_color_export = i;915}916}917918unsigned num_compacted_mrts = 0;919while (colors_written) {920LLVMValueRef color[4];921int output_index = u_bit_scan(&colors_written);922unsigned color_type = (key->ps_epilog.color_types >> (output_index * 2)) & 0x3;923924if (color_type != SI_TYPE_ANY32) {925for (i = 0; i < 4; i++) {926color[i] = LLVMGetParam(ctx->main_fn, vgpr + i / 2);927color[i] = LLVMBuildBitCast(ctx->ac.builder, color[i], ctx->ac.v2f16, "");928color[i] = ac_llvm_extract_elem(&ctx->ac, color[i], i % 2);929}930vgpr += 4;931} else {932for (i = 0; i < 4; i++)933color[i] = LLVMGetParam(ctx->main_fn, vgpr++);934}935936if (si_export_mrt_color(ctx, color, output_index, num_compacted_mrts,937ctx->args.arg_count - 1,938output_index == last_color_export, color_type, &exp))939num_compacted_mrts++;940}941942/* Process depth, stencil, samplemask. */943if (key->ps_epilog.writes_z)944depth = LLVMGetParam(ctx->main_fn, vgpr++);945if (key->ps_epilog.writes_stencil)946stencil = LLVMGetParam(ctx->main_fn, vgpr++);947if (key->ps_epilog.writes_samplemask)948samplemask = LLVMGetParam(ctx->main_fn, vgpr++);949950if (depth || stencil || samplemask)951ac_export_mrt_z(&ctx->ac, depth, stencil, samplemask, &exp.args[exp.num++]);952else if (last_color_export == -1)953ac_build_export_null(&ctx->ac);954955if (exp.num) {956for (unsigned i = 0; i < exp.num; i++)957ac_build_export(&ctx->ac, &exp.args[i]);958}959960/* Compile. */961LLVMBuildRetVoid(ctx->ac.builder);962}963964void si_llvm_build_monolithic_ps(struct si_shader_context *ctx, struct si_shader *shader)965{966LLVMValueRef parts[3];967unsigned num_parts = 0, main_index;968LLVMValueRef main_fn = ctx->main_fn;969970union si_shader_part_key prolog_key;971si_get_ps_prolog_key(shader, &prolog_key, false);972973if (si_need_ps_prolog(&prolog_key)) {974si_llvm_build_ps_prolog(ctx, &prolog_key);975parts[num_parts++] = ctx->main_fn;976}977978main_index = num_parts;979parts[num_parts++] = main_fn;980981union si_shader_part_key epilog_key;982si_get_ps_epilog_key(shader, &epilog_key);983si_llvm_build_ps_epilog(ctx, &epilog_key);984parts[num_parts++] = ctx->main_fn;985986si_build_wrapper_function(ctx, parts, num_parts, main_index, 0, false);987}988989void si_llvm_init_ps_callbacks(struct si_shader_context *ctx)990{991ctx->abi.emit_outputs = si_llvm_return_fs_outputs;992ctx->abi.load_sample_position = load_sample_position;993ctx->abi.load_sample_mask_in = load_sample_mask_in;994ctx->abi.emit_fbfetch = si_nir_emit_fbfetch;995}996997998