From ee71e0d83ab82c40c3a48bc24dab6019639beca2 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Fri, 16 Sep 2022 10:14:15 -0600 Subject: [PATCH] llvmpipe: asst. clean-ups in lp_bld_interp.c Signed-off-by: Brian Paul Part-of: --- src/gallium/drivers/llvmpipe/lp_bld_interp.c | 142 ++++++++++++------- 1 file changed, 91 insertions(+), 51 deletions(-) diff --git a/src/gallium/drivers/llvmpipe/lp_bld_interp.c b/src/gallium/drivers/llvmpipe/lp_bld_interp.c index f5c4a2105b4..3109b551348 100644 --- a/src/gallium/drivers/llvmpipe/lp_bld_interp.c +++ b/src/gallium/drivers/llvmpipe/lp_bld_interp.c @@ -121,19 +121,19 @@ static const unsigned char quad_offset_y[16] = {0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, static void attrib_name(LLVMValueRef val, unsigned attrib, unsigned chan, const char *suffix) { - if(attrib == 0) + if (attrib == 0) lp_build_name(val, "pos.%c%s", "xyzw"[chan], suffix); else lp_build_name(val, "input%u.%c%s", attrib - 1, "xyzw"[chan], suffix); } + static void calc_offsets(struct lp_build_context *coeff_bld, unsigned quad_start_index, LLVMValueRef *pixoffx, LLVMValueRef *pixoffy) { - unsigned i; unsigned num_pix = coeff_bld->type.length; struct gallivm_state *gallivm = coeff_bld->gallivm; LLVMBuilderRef builder = coeff_bld->gallivm->builder; @@ -142,7 +142,7 @@ calc_offsets(struct lp_build_context *coeff_bld, *pixoffx = coeff_bld->undef; *pixoffy = coeff_bld->undef; - for (i = 0; i < num_pix; i++) { + for (unsigned i = 0; i < num_pix; i++) { nr = lp_build_const_int32(gallivm, i); pixxf = lp_build_const_float(gallivm, quad_offset_x[i % num_pix] + (quad_start_index & 1) * 2); @@ -153,6 +153,7 @@ calc_offsets(struct lp_build_context *coeff_bld, } } + static void calc_centroid_offsets(struct lp_build_interp_soa_context *bld, struct gallivm_state *gallivm, @@ -194,25 +195,31 @@ calc_centroid_offsets(struct lp_build_interp_soa_context *bld, *centroid_y = lp_build_select(coeff_bld, s_mask_and, pix_center_offset, centroid_y_offset); } + /* Note: this assumes the pointer to elem_type is in address space 0 */ static LLVMValueRef -load_casted(LLVMBuilderRef builder, LLVMTypeRef elem_type, LLVMValueRef ptr, const char *name) { +load_casted(LLVMBuilderRef builder, LLVMTypeRef elem_type, + LLVMValueRef ptr, const char *name) { ptr = LLVMBuildBitCast(builder, ptr, LLVMPointerType(elem_type, 0), name); return LLVMBuildLoad2(builder, elem_type, ptr, name); } + static LLVMValueRef indexed_load(LLVMBuilderRef builder, LLVMTypeRef gep_type, - LLVMTypeRef elem_type, LLVMValueRef ptr, LLVMValueRef index, const char *name) { + LLVMTypeRef elem_type, LLVMValueRef ptr, + LLVMValueRef index, const char *name) { ptr = LLVMBuildGEP2(builder, gep_type, ptr, &index, 1, name); return load_casted(builder, elem_type, ptr, name); } + /* Much easier, and significantly less instructions in the per-stamp * part (less than half) but overall more instructions so a loss if * most quads are active. Might be a win though with larger vectors. * No ability to do per-quad divide (doable but not implemented) - * Could be made to work with passed in pixel offsets (i.e. active quad merging). + * Could be made to work with passed in pixel offsets (i.e. active quad + * merging). */ static void coeffs_init_simple(struct lp_build_interp_soa_context *bld, @@ -224,9 +231,8 @@ coeffs_init_simple(struct lp_build_interp_soa_context *bld, struct lp_build_context *setup_bld = &bld->setup_bld; struct gallivm_state *gallivm = coeff_bld->gallivm; LLVMBuilderRef builder = gallivm->builder; - unsigned attrib; - for (attrib = 0; attrib < bld->num_attribs; ++attrib) { + for (unsigned attrib = 0; attrib < bld->num_attribs; ++attrib) { /* * always fetch all 4 values for performance/simplicity * Note: we do that here because it seems to generate better @@ -253,15 +259,18 @@ coeffs_init_simple(struct lp_build_interp_soa_context *bld, FALLTHROUGH; case LP_INTERP_LINEAR: - dadxaos = indexed_load(builder, fs_elem_type, setup_bld->vec_type, dadx_ptr, index, ""); - dadyaos = indexed_load(builder, fs_elem_type, setup_bld->vec_type, dady_ptr, index, ""); + dadxaos = indexed_load(builder, fs_elem_type, + setup_bld->vec_type, dadx_ptr, index, ""); + dadyaos = indexed_load(builder, fs_elem_type, + setup_bld->vec_type, dady_ptr, index, ""); attrib_name(dadxaos, attrib, 0, ".dadxaos"); attrib_name(dadyaos, attrib, 0, ".dadyaos"); FALLTHROUGH; case LP_INTERP_CONSTANT: case LP_INTERP_FACING: - a0aos = indexed_load(builder, fs_elem_type, setup_bld->vec_type, a0_ptr, index, ""); + a0aos = indexed_load(builder, fs_elem_type, + setup_bld->vec_type, a0_ptr, index, ""); attrib_name(a0aos, attrib, 0, ".a0aos"); break; @@ -279,6 +288,7 @@ coeffs_init_simple(struct lp_build_interp_soa_context *bld, } } + /** * Interpolate the shader input attribute values. * This is called for each (group of) quad(s). @@ -297,18 +307,20 @@ attribs_update_simple(struct lp_build_interp_soa_context *bld, struct lp_build_context *coeff_bld = &bld->coeff_bld; struct lp_build_context *setup_bld = &bld->setup_bld; LLVMValueRef oow = NULL; - unsigned attrib; LLVMValueRef pixoffx; LLVMValueRef pixoffy; LLVMValueRef ptr; - LLVMValueRef pix_center_offset = lp_build_const_vec(gallivm, coeff_bld->type, 0.5); + LLVMValueRef pix_center_offset = lp_build_const_vec(gallivm, + coeff_bld->type, 0.5); /* could do this with code-generated passed in pixel offsets too */ assert(loop_iter); - ptr = LLVMBuildGEP2(builder, bld->store_elem_type, bld->xoffset_store, &loop_iter, 1, ""); + ptr = LLVMBuildGEP2(builder, bld->store_elem_type, bld->xoffset_store, + &loop_iter, 1, ""); pixoffx = LLVMBuildLoad2(builder, bld->store_elem_type, ptr, ""); - ptr = LLVMBuildGEP2(builder, bld->store_elem_type, bld->yoffset_store, &loop_iter, 1, ""); + ptr = LLVMBuildGEP2(builder, bld->store_elem_type, bld->yoffset_store, + &loop_iter, 1, ""); pixoffy = LLVMBuildLoad2(builder, bld->store_elem_type, ptr, ""); pixoffx = LLVMBuildFAdd(builder, pixoffx, @@ -316,21 +328,19 @@ attribs_update_simple(struct lp_build_interp_soa_context *bld, pixoffy = LLVMBuildFAdd(builder, pixoffy, lp_build_broadcast_scalar(coeff_bld, bld->y), ""); - for (attrib = start; attrib < end; attrib++) { + for (unsigned attrib = start; attrib < end; attrib++) { const unsigned mask = bld->mask[attrib]; const enum lp_interp interp = bld->interp[attrib]; const enum tgsi_interpolate_loc loc = bld->interp_loc[attrib]; - unsigned chan; - for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { + for (unsigned chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { if (mask & (1 << chan)) { - LLVMValueRef index; + LLVMValueRef index = lp_build_const_int32(gallivm, chan); LLVMValueRef dadx = coeff_bld->zero; LLVMValueRef dady = coeff_bld->zero; LLVMValueRef a = coeff_bld->zero; LLVMValueRef chan_pixoffx = pixoffx, chan_pixoffy = pixoffy; - index = lp_build_const_int32(gallivm, chan); switch (interp) { case LP_INTERP_PERSPECTIVE: FALLTHROUGH; @@ -373,7 +383,8 @@ attribs_update_simple(struct lp_build_interp_soa_context *bld, if (bld->coverage_samples > 1) { LLVMValueRef xoffset = pix_center_offset; LLVMValueRef yoffset = pix_center_offset; - if (loc == TGSI_INTERPOLATE_LOC_SAMPLE || (attrib == 0 && chan == 2 && sample_id)) { + if (loc == TGSI_INTERPOLATE_LOC_SAMPLE || + (attrib == 0 && chan == 2 && sample_id)) { LLVMValueRef x_val_idx = LLVMBuildMul(gallivm->builder, sample_id, lp_build_const_int32(gallivm, 2), ""); LLVMValueRef y_val_idx = LLVMBuildAdd(gallivm->builder, x_val_idx, lp_build_const_int32(gallivm, 1), ""); @@ -441,6 +452,7 @@ attribs_update_simple(struct lp_build_interp_soa_context *bld, } } + static LLVMValueRef lp_build_interp_soa_indirect(struct lp_build_interp_soa_context *bld, struct gallivm_state *gallivm, @@ -455,15 +467,24 @@ lp_build_interp_soa_indirect(struct lp_build_interp_soa_context *bld, LLVMValueRef dadx = coeff_bld->zero; LLVMValueRef dady = coeff_bld->zero; LLVMValueRef a = coeff_bld->zero; + LLVMTypeRef u8ptr = + LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); - LLVMTypeRef u8ptr = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); - - indir_index = LLVMBuildAdd(builder, indir_index, lp_build_const_int_vec(gallivm, coeff_bld->type, attrib), ""); - LLVMValueRef index = LLVMBuildMul(builder, indir_index, lp_build_const_int_vec(gallivm, coeff_bld->type, 4), ""); - index = LLVMBuildAdd(builder, index, lp_build_const_int_vec(gallivm, coeff_bld->type, chan), ""); + indir_index = LLVMBuildAdd(builder, indir_index, + lp_build_const_int_vec(gallivm, coeff_bld->type, + attrib), ""); + LLVMValueRef index = LLVMBuildMul(builder, indir_index, + lp_build_const_int_vec(gallivm, + coeff_bld->type, + 4), ""); + index = LLVMBuildAdd(builder, index, + lp_build_const_int_vec(gallivm, + coeff_bld->type, chan), ""); /* size up to byte indices */ - index = LLVMBuildMul(builder, index, lp_build_const_int_vec(gallivm, coeff_bld->type, 4), ""); + index = LLVMBuildMul(builder, index, + lp_build_const_int_vec(gallivm, coeff_bld->type, 4), + ""); struct lp_type dst_type = coeff_bld->type; dst_type.length = 1; @@ -471,18 +492,23 @@ lp_build_interp_soa_indirect(struct lp_build_interp_soa_context *bld, case LP_INTERP_PERSPECTIVE: FALLTHROUGH; case LP_INTERP_LINEAR: - dadx = lp_build_gather(gallivm, coeff_bld->type.length, coeff_bld->type.width, dst_type, - true, LLVMBuildBitCast(builder, bld->dadx_ptr, u8ptr, ""), index, false); + true, LLVMBuildBitCast(builder, bld->dadx_ptr, + u8ptr, ""), + index, false); dady = lp_build_gather(gallivm, coeff_bld->type.length, coeff_bld->type.width, dst_type, - true, LLVMBuildBitCast(builder, bld->dady_ptr, u8ptr, ""), index, false); + true, LLVMBuildBitCast(builder, bld->dady_ptr, + u8ptr, ""), + index, false); a = lp_build_gather(gallivm, coeff_bld->type.length, coeff_bld->type.width, dst_type, - true, LLVMBuildBitCast(builder, bld->a0_ptr, u8ptr, ""), index, false); + true, LLVMBuildBitCast(builder, bld->a0_ptr, + u8ptr, ""), + index, false); /* * a = a0 + (x * dadx + y * dady) @@ -503,7 +529,9 @@ lp_build_interp_soa_indirect(struct lp_build_interp_soa_context *bld, case LP_INTERP_FACING: a = lp_build_gather(gallivm, coeff_bld->type.length, coeff_bld->type.width, dst_type, - true, LLVMBuildBitCast(builder, bld->a0_ptr, u8ptr, ""), index, false); + true, LLVMBuildBitCast(builder, bld->a0_ptr, + u8ptr, ""), + index, false); break; default: assert(0); @@ -512,6 +540,7 @@ lp_build_interp_soa_indirect(struct lp_build_interp_soa_context *bld, return a; } + LLVMValueRef lp_build_interp_soa(struct lp_build_interp_soa_context *bld, struct gallivm_state *gallivm, @@ -533,9 +562,11 @@ lp_build_interp_soa(struct lp_build_interp_soa_context *bld, /* could do this with code-generated passed in pixel offsets too */ assert(loop_iter); - ptr = LLVMBuildGEP2(builder, bld->store_elem_type, bld->xoffset_store, &loop_iter, 1, ""); + ptr = LLVMBuildGEP2(builder, bld->store_elem_type, bld->xoffset_store, + &loop_iter, 1, ""); pixoffx = LLVMBuildLoad2(builder, bld->store_elem_type, ptr, ""); - ptr = LLVMBuildGEP2(builder, bld->store_elem_type, bld->yoffset_store, &loop_iter, 1, ""); + ptr = LLVMBuildGEP2(builder, bld->store_elem_type, bld->yoffset_store, + &loop_iter, 1, ""); pixoffy = LLVMBuildLoad2(builder, bld->store_elem_type, ptr, ""); pixoffx = LLVMBuildFAdd(builder, pixoffx, @@ -543,7 +574,8 @@ lp_build_interp_soa(struct lp_build_interp_soa_context *bld, pixoffy = LLVMBuildFAdd(builder, pixoffy, lp_build_broadcast_scalar(coeff_bld, bld->y), ""); - LLVMValueRef pix_center_offset = lp_build_const_vec(gallivm, coeff_bld->type, 0.5); + LLVMValueRef pix_center_offset = lp_build_const_vec(gallivm, + coeff_bld->type, 0.5); if (loc == TGSI_INTERPOLATE_LOC_CENTER) { if (bld->coverage_samples > 1) { @@ -558,11 +590,15 @@ lp_build_interp_soa(struct lp_build_interp_soa_context *bld, pixoffy = LLVMBuildFAdd(builder, pixoffy, offsets[1], ""); } else if (loc == TGSI_INTERPOLATE_LOC_SAMPLE) { - LLVMValueRef x_val_idx = LLVMBuildMul(gallivm->builder, offsets[0], lp_build_const_int_vec(gallivm, bld->coeff_bld.type, 2 * 4), ""); - LLVMValueRef y_val_idx = LLVMBuildAdd(gallivm->builder, x_val_idx, lp_build_const_int_vec(gallivm, bld->coeff_bld.type, 4), ""); + LLVMValueRef x_val_idx = LLVMBuildMul(gallivm->builder, offsets[0], + lp_build_const_int_vec(gallivm, bld->coeff_bld.type, 2 * 4), ""); + LLVMValueRef y_val_idx = LLVMBuildAdd(gallivm->builder, x_val_idx, + lp_build_const_int_vec(gallivm, bld->coeff_bld.type, 4), ""); - LLVMValueRef base_ptr = LLVMBuildBitCast(gallivm->builder, bld->sample_pos_array, - LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0), ""); + LLVMValueRef base_ptr = + LLVMBuildBitCast(gallivm->builder, + bld->sample_pos_array, + LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0), ""); LLVMValueRef xoffset = lp_build_gather(gallivm, bld->coeff_bld.type.length, bld->coeff_bld.type.width, @@ -656,6 +692,7 @@ lp_build_interp_soa(struct lp_build_interp_soa_context *bld, return a; } + /** * Generate the position vectors. * @@ -775,20 +812,20 @@ lp_build_interp_soa_init(struct lp_build_interp_soa_context *bld, LLVMValueRef ptr; bld->store_elem_type = lp_build_vec_type(gallivm, type); - bld->xoffset_store = lp_build_array_alloca(gallivm, - bld->store_elem_type, - lp_build_const_int32(gallivm, num_loops), - ""); - bld->yoffset_store = lp_build_array_alloca(gallivm, - bld->store_elem_type, - lp_build_const_int32(gallivm, num_loops), - ""); + bld->xoffset_store = + lp_build_array_alloca(gallivm, bld->store_elem_type, + lp_build_const_int32(gallivm, num_loops), ""); + bld->yoffset_store = + lp_build_array_alloca(gallivm, bld->store_elem_type, + lp_build_const_int32(gallivm, num_loops), ""); for (i = 0; i < num_loops; i++) { index = lp_build_const_int32(gallivm, i); calc_offsets(&bld->coeff_bld, i*type.length/4, &pixoffx, &pixoffy); - ptr = LLVMBuildGEP2(builder, bld->store_elem_type, bld->xoffset_store, &index, 1, ""); + ptr = LLVMBuildGEP2(builder, bld->store_elem_type, + bld->xoffset_store, &index, 1, ""); LLVMBuildStore(builder, pixoffx, ptr); - ptr = LLVMBuildGEP2(builder, bld->store_elem_type, bld->yoffset_store, &index, 1, ""); + ptr = LLVMBuildGEP2(builder, bld->store_elem_type, + bld->yoffset_store, &index, 1, ""); LLVMBuildStore(builder, pixoffy, ptr); } } @@ -808,15 +845,18 @@ lp_build_interp_soa_update_inputs_dyn(struct lp_build_interp_soa_context *bld, LLVMValueRef mask_store, LLVMValueRef sample_id) { - attribs_update_simple(bld, gallivm, quad_start_index, mask_type, mask_store, sample_id, 1, bld->num_attribs); + attribs_update_simple(bld, gallivm, quad_start_index, mask_type, + mask_store, sample_id, 1, bld->num_attribs); } + void lp_build_interp_soa_update_pos_dyn(struct lp_build_interp_soa_context *bld, struct gallivm_state *gallivm, LLVMValueRef quad_start_index, LLVMValueRef sample_id) { - attribs_update_simple(bld, gallivm, quad_start_index, NULL, NULL, sample_id, 0, 1); + attribs_update_simple(bld, gallivm, quad_start_index, + NULL, NULL, sample_id, 0, 1); }