From fd1da0f7f5c343b2910c1eaf164f5865105ec778 Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Fri, 26 May 2023 21:23:57 -0700 Subject: [PATCH] compiler/types: Extract get_explicit_matrix_instance() function Reviewed-by: Kenneth Graunke Part-of: --- src/compiler/glsl_types.cpp | 104 ++++++++++++++++++++---------------- src/compiler/glsl_types.h | 4 ++ 2 files changed, 62 insertions(+), 46 deletions(-) diff --git a/src/compiler/glsl_types.cpp b/src/compiler/glsl_types.cpp index 6fe79314577..a083a0be8e8 100644 --- a/src/compiler/glsl_types.cpp +++ b/src/compiler/glsl_types.cpp @@ -643,52 +643,9 @@ glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns, * looked up in a table so they're handled separately. */ if (explicit_stride > 0 || explicit_alignment > 0) { - if (explicit_alignment > 0) { - assert(util_is_power_of_two_nonzero(explicit_alignment)); - assert(explicit_stride % explicit_alignment == 0); - } - - const glsl_type *bare_type = get_instance(base_type, rows, columns); - - assert(columns > 1 || (rows > 1 && !row_major)); - - char name[128]; - snprintf(name, sizeof(name), "%sx%ua%uB%s", bare_type->name, - explicit_stride, explicit_alignment, row_major ? "RM" : ""); - const uint32_t name_hash = _mesa_hash_string(name); - - simple_mtx_lock(&glsl_type::hash_mutex); - assert(glsl_type_users > 0); - - if (explicit_matrix_types == NULL) { - explicit_matrix_types = - _mesa_hash_table_create(NULL, _mesa_hash_string, - _mesa_key_string_equal); - } - - const struct hash_entry *entry = - _mesa_hash_table_search_pre_hashed(explicit_matrix_types, name_hash, name); - if (entry == NULL) { - const glsl_type *t = new glsl_type(bare_type->gl_type, - (glsl_base_type)base_type, - rows, columns, name, - explicit_stride, row_major, - explicit_alignment); - - entry = _mesa_hash_table_insert_pre_hashed(explicit_matrix_types, - name_hash, t->name, (void *)t); - } - - auto t = (const glsl_type *) entry->data; - simple_mtx_unlock(&glsl_type::hash_mutex); - - assert(t->base_type == base_type); - assert(t->vector_elements == rows); - assert(t->matrix_columns == columns); - assert(t->explicit_stride == explicit_stride); - assert(t->explicit_alignment == explicit_alignment); - - return t; + return get_explicit_matrix_instance(base_type, rows, columns, + explicit_stride, row_major, + explicit_alignment); } assert(!row_major); @@ -792,6 +749,61 @@ glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns, return error_type; } +const glsl_type * +glsl_type::get_explicit_matrix_instance(unsigned int base_type, unsigned int rows, unsigned int columns, + unsigned int explicit_stride, bool row_major, unsigned int explicit_alignment) +{ + assert(explicit_stride > 0 || explicit_alignment > 0); + assert(base_type != GLSL_TYPE_VOID); + + if (explicit_alignment > 0) { + assert(util_is_power_of_two_nonzero(explicit_alignment)); + assert(explicit_stride % explicit_alignment == 0); + } + + const glsl_type *bare_type = get_instance(base_type, rows, columns); + + assert(columns > 1 || (rows > 1 && !row_major)); + + char name[128]; + snprintf(name, sizeof(name), "%sx%ua%uB%s", bare_type->name, + explicit_stride, explicit_alignment, row_major ? "RM" : ""); + const uint32_t name_hash = _mesa_hash_string(name); + + simple_mtx_lock(&glsl_type::hash_mutex); + assert(glsl_type_users > 0); + + if (explicit_matrix_types == NULL) { + explicit_matrix_types = + _mesa_hash_table_create(NULL, _mesa_hash_string, + _mesa_key_string_equal); + } + + const struct hash_entry *entry = + _mesa_hash_table_search_pre_hashed(explicit_matrix_types, name_hash, name); + if (entry == NULL) { + const glsl_type *t = new glsl_type(bare_type->gl_type, + (glsl_base_type)base_type, + rows, columns, name, + explicit_stride, row_major, + explicit_alignment); + + entry = _mesa_hash_table_insert_pre_hashed(explicit_matrix_types, + name_hash, t->name, (void *)t); + } + + auto t = (const glsl_type *) entry->data; + simple_mtx_unlock(&glsl_type::hash_mutex); + + assert(t->base_type == base_type); + assert(t->vector_elements == rows); + assert(t->matrix_columns == columns); + assert(t->explicit_stride == explicit_stride); + assert(t->explicit_alignment == explicit_alignment); + + return t; +} + const glsl_type * glsl_type::get_sampler_instance(enum glsl_sampler_dim dim, bool shadow, diff --git a/src/compiler/glsl_types.h b/src/compiler/glsl_types.h index 96ed6eaec06..8657e00e8e8 100644 --- a/src/compiler/glsl_types.h +++ b/src/compiler/glsl_types.h @@ -1328,6 +1328,10 @@ private: friend void glsl_type_singleton_decref(void); friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *); /*@}*/ + + static const glsl_type *get_explicit_matrix_instance(unsigned int base_type, unsigned int rows, unsigned int columns, + unsigned int explicit_stride, bool row_major, + unsigned int explicit_alignment); }; #undef DECL_TYPE