From 6df187df1368f4f088cfee2c5629426001a0224f Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Tue, 4 May 2021 14:26:46 -0400 Subject: [PATCH] nir/builder: add nir_pad_vector and nir_pad_vec4 util functions these pad a given value to vec4 or arbitrary number of components Reviewed-by: Jesse Natalie Reviewed-by: Jason Ekstrand Part-of: --- src/compiler/nir/nir_builder.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h index 873c912..7a81bfb 100644 --- a/src/compiler/nir/nir_builder.h +++ b/src/compiler/nir/nir_builder.h @@ -1119,6 +1119,38 @@ nir_bitcast_vector(nir_builder *b, nir_ssa_def *src, unsigned dest_bit_size) } /** + * Pad a value to N components with undefs of matching bit size. + * If the value already contains >= num_components, it is returned without change. + */ +static inline nir_ssa_def * +nir_pad_vector(nir_builder *b, nir_ssa_def *src, unsigned num_components) +{ + assert(src->num_components <= num_components); + if (src->num_components == num_components) + return src; + + nir_ssa_def *components[NIR_MAX_VEC_COMPONENTS]; + nir_ssa_def *undef = nir_ssa_undef(b, 1, src->bit_size); + unsigned i = 0; + for (; i < src->num_components; i++) + components[i] = nir_channel(b, src, i); + for (; i < num_components; i++) + components[i] = undef; + + return nir_vec(b, components, num_components); +} + +/** + * Pad a value to 4 components with undefs of matching bit size. + * If the value already contains >= 4 components, it is returned without change. + */ +static inline nir_ssa_def * +nir_pad_vec4(nir_builder *b, nir_ssa_def *src) +{ + return nir_pad_vector(b, src, 4); +} + +/** * Turns a nir_src into a nir_ssa_def * so it can be passed to * nir_build_alu()-based builder calls. * -- 2.7.4