From b62b2fa4b9c773705b20678810419d40559aae19 Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Fri, 15 Oct 2021 16:26:32 -0500 Subject: [PATCH] compiler/types: Add a wrap_in_arrays helper This has been copied+pasted 3 times now. Reviewed-by: Jesse Natalie Part-of: --- src/compiler/glsl/glsl_to_nir.cpp | 13 +------------ src/compiler/nir/nir_split_vars.c | 15 +-------------- src/compiler/nir_types.cpp | 13 +++++++++++++ src/compiler/nir_types.h | 3 +++ src/compiler/spirv/spirv_to_nir.c | 17 ++--------------- 5 files changed, 20 insertions(+), 41 deletions(-) diff --git a/src/compiler/glsl/glsl_to_nir.cpp b/src/compiler/glsl/glsl_to_nir.cpp index 30edad871f8..7ab447dcf06 100644 --- a/src/compiler/glsl/glsl_to_nir.cpp +++ b/src/compiler/glsl/glsl_to_nir.cpp @@ -432,17 +432,6 @@ nir_visitor::constant_copy(ir_constant *ir, void *mem_ctx) return ret; } -static const glsl_type * -wrap_type_in_array(const glsl_type *elem_type, const glsl_type *array_type) -{ - if (!array_type->is_array()) - return elem_type; - - elem_type = wrap_type_in_array(elem_type, array_type->fields.array); - - return glsl_type::get_array_instance(elem_type, array_type->length); -} - static unsigned get_nir_how_declared(unsigned how_declared) { @@ -588,7 +577,7 @@ nir_visitor::visit(ir_variable *ir) /* If the type contains the interface, wrap the explicit type in the * right number of arrays. */ - var->type = wrap_type_in_array(explicit_ifc_type, ir->type); + var->type = glsl_type_wrap_in_arrays(explicit_ifc_type, ir->type); } else { /* Otherwise, this variable is one entry in the interface */ UNUSED bool found = false; diff --git a/src/compiler/nir/nir_split_vars.c b/src/compiler/nir/nir_split_vars.c index 3b8482f7204..fbe44287c7f 100644 --- a/src/compiler/nir/nir_split_vars.c +++ b/src/compiler/nir/nir_split_vars.c @@ -78,19 +78,6 @@ struct field { nir_variable *var; }; -static const struct glsl_type * -wrap_type_in_array(const struct glsl_type *type, - const struct glsl_type *array_type) -{ - if (!glsl_type_is_array(array_type)) - return type; - - const struct glsl_type *elem_type = - wrap_type_in_array(type, glsl_get_array_element(array_type)); - assert(glsl_get_explicit_stride(array_type) == 0); - return glsl_array_type(elem_type, glsl_get_length(array_type), 0); -} - static int num_array_levels_in_array_of_vector_type(const struct glsl_type *type) { @@ -141,7 +128,7 @@ init_field_for_type(struct field *field, struct field *parent, } else { const struct glsl_type *var_type = type; for (struct field *f = field->parent; f; f = f->parent) - var_type = wrap_type_in_array(var_type, f->type); + var_type = glsl_type_wrap_in_arrays(var_type, f->type); nir_variable_mode mode = state->base_var->data.mode; if (mode == nir_var_function_temp) { diff --git a/src/compiler/nir_types.cpp b/src/compiler/nir_types.cpp index 048a03d523f..b069e92c40e 100644 --- a/src/compiler/nir_types.cpp +++ b/src/compiler/nir_types.cpp @@ -1048,6 +1048,19 @@ glsl_get_explicit_type_for_size_align(const struct glsl_type *type, return type->get_explicit_type_for_size_align(type_info, size, align); } +const struct glsl_type * +glsl_type_wrap_in_arrays(const struct glsl_type *type, + const struct glsl_type *arrays) +{ + if (!glsl_type_is_array(arrays)) + return type; + + const glsl_type *elem_type = + glsl_type_wrap_in_arrays(type, glsl_get_array_element(arrays)); + return glsl_array_type(elem_type, glsl_get_length(arrays), + glsl_get_explicit_stride(arrays)); +} + const struct glsl_type * glsl_type_replace_vec3_with_vec4(const struct glsl_type *type) { diff --git a/src/compiler/nir_types.h b/src/compiler/nir_types.h index ee1e1321bb2..70347995579 100644 --- a/src/compiler/nir_types.h +++ b/src/compiler/nir_types.h @@ -248,6 +248,9 @@ const struct glsl_type *glsl_get_explicit_type_for_size_align(const struct glsl_ glsl_type_size_align_func type_info, unsigned *size, unsigned *align); +const struct glsl_type *glsl_type_wrap_in_arrays(const struct glsl_type *type, + const struct glsl_type *arrays); + const struct glsl_type *glsl_type_replace_vec3_with_vec4(const struct glsl_type *type); unsigned glsl_type_get_sampler_count(const struct glsl_type *type); diff --git a/src/compiler/spirv/spirv_to_nir.c b/src/compiler/spirv/spirv_to_nir.c index 99be91e4d2b..84944c51eb5 100644 --- a/src/compiler/spirv/spirv_to_nir.c +++ b/src/compiler/spirv/spirv_to_nir.c @@ -853,19 +853,6 @@ vtn_type_copy(struct vtn_builder *b, struct vtn_type *src) return dest; } -static const struct glsl_type * -wrap_type_in_array(const struct glsl_type *type, - const struct glsl_type *array_type) -{ - if (!glsl_type_is_array(array_type)) - return type; - - const struct glsl_type *elem_type = - wrap_type_in_array(type, glsl_get_array_element(array_type)); - return glsl_array_type(elem_type, glsl_get_length(array_type), - glsl_get_explicit_stride(array_type)); -} - static bool vtn_type_needs_explicit_layout(struct vtn_builder *b, struct vtn_type *type, enum vtn_variable_mode mode) @@ -907,7 +894,7 @@ vtn_type_get_nir_type(struct vtn_builder *b, struct vtn_type *type, vtn_fail_if(glsl_without_array(type->type) != glsl_uint_type(), "Variables in the AtomicCounter storage class should be " "(possibly arrays of arrays of) uint."); - return wrap_type_in_array(glsl_atomic_uint_type(), type->type); + return glsl_type_wrap_in_arrays(glsl_atomic_uint_type(), type->type); } if (mode == vtn_variable_mode_uniform) { @@ -968,7 +955,7 @@ vtn_type_get_nir_type(struct vtn_builder *b, struct vtn_type *type, if (mode == vtn_variable_mode_image) { struct vtn_type *image_type = vtn_type_without_array(type); vtn_assert(image_type->base_type == vtn_base_type_image); - return wrap_type_in_array(image_type->glsl_image, type->type); + return glsl_type_wrap_in_arrays(image_type->glsl_image, type->type); } /* Layout decorations are allowed but ignored in certain conditions,