From 814d694160da436a2b6218976b212e52478802b7 Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Wed, 16 Jul 2014 16:51:14 -0700 Subject: [PATCH] glsl: Track matrix layout of structure fields using two bits v2: Rename GLSL_MATRIX_LAYOUT_DEFAULT to GLSL_MATRIX_LAYOUT_INHERITED. Add comments in glsl_types.h explaining the layouts. Suggested by Matt. Signed-off-by: Ian Romanick Reviewed-by: Jordan Justen Reviewed-by: Matt Turner --- src/glsl/ast_to_hir.cpp | 8 ++-- src/glsl/builtin_types.cpp | 74 +++++++++++++++++----------------- src/glsl/builtin_variables.cpp | 2 +- src/glsl/glsl_types.cpp | 32 +++++++++++---- src/glsl/glsl_types.h | 27 ++++++++++++- src/glsl/link_uniforms.cpp | 3 +- 6 files changed, 96 insertions(+), 50 deletions(-) diff --git a/src/glsl/ast_to_hir.cpp b/src/glsl/ast_to_hir.cpp index 397a575501d..89ac84e6b06 100644 --- a/src/glsl/ast_to_hir.cpp +++ b/src/glsl/ast_to_hir.cpp @@ -5204,11 +5204,13 @@ ast_process_structure_or_interface_block(exec_list *instructions, } if (field_type->without_array()->is_matrix()) { - fields[i].row_major = block_row_major; + fields[i].matrix_layout = block_row_major + ? GLSL_MATRIX_LAYOUT_ROW_MAJOR + : GLSL_MATRIX_LAYOUT_COLUMN_MAJOR; if (qual->flags.q.row_major) - fields[i].row_major = true; + fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR; else if (qual->flags.q.column_major) - fields[i].row_major = false; + fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR; } i++; diff --git a/src/glsl/builtin_types.cpp b/src/glsl/builtin_types.cpp index 404a8038a43..10fac0f81cc 100644 --- a/src/glsl/builtin_types.cpp +++ b/src/glsl/builtin_types.cpp @@ -54,64 +54,64 @@ &glsl_type::_struct_##NAME##_type; static const struct glsl_struct_field gl_DepthRangeParameters_fields[] = { - { glsl_type::float_type, "near", false, -1 }, - { glsl_type::float_type, "far", false, -1 }, - { glsl_type::float_type, "diff", false, -1 }, + { glsl_type::float_type, "near", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "far", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "diff", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, }; static const struct glsl_struct_field gl_PointParameters_fields[] = { - { glsl_type::float_type, "size", false, -1 }, - { glsl_type::float_type, "sizeMin", false, -1 }, - { glsl_type::float_type, "sizeMax", false, -1 }, - { glsl_type::float_type, "fadeThresholdSize", false, -1 }, - { glsl_type::float_type, "distanceConstantAttenuation", false, -1 }, - { glsl_type::float_type, "distanceLinearAttenuation", false, -1 }, - { glsl_type::float_type, "distanceQuadraticAttenuation", false, -1 }, + { glsl_type::float_type, "size", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "sizeMin", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "sizeMax", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "fadeThresholdSize", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "distanceConstantAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "distanceLinearAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "distanceQuadraticAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, }; static const struct glsl_struct_field gl_MaterialParameters_fields[] = { - { glsl_type::vec4_type, "emission", false, -1 }, - { glsl_type::vec4_type, "ambient", false, -1 }, - { glsl_type::vec4_type, "diffuse", false, -1 }, - { glsl_type::vec4_type, "specular", false, -1 }, - { glsl_type::float_type, "shininess", false, -1 }, + { glsl_type::vec4_type, "emission", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::vec4_type, "ambient", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::vec4_type, "diffuse", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::vec4_type, "specular", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "shininess", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, }; static const struct glsl_struct_field gl_LightSourceParameters_fields[] = { - { glsl_type::vec4_type, "ambient", false, -1 }, - { glsl_type::vec4_type, "diffuse", false, -1 }, - { glsl_type::vec4_type, "specular", false, -1 }, - { glsl_type::vec4_type, "position", false, -1 }, - { glsl_type::vec4_type, "halfVector", false, -1 }, - { glsl_type::vec3_type, "spotDirection", false, -1 }, - { glsl_type::float_type, "spotExponent", false, -1 }, - { glsl_type::float_type, "spotCutoff", false, -1 }, - { glsl_type::float_type, "spotCosCutoff", false, -1 }, - { glsl_type::float_type, "constantAttenuation", false, -1 }, - { glsl_type::float_type, "linearAttenuation", false, -1 }, - { glsl_type::float_type, "quadraticAttenuation", false, -1 }, + { glsl_type::vec4_type, "ambient", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::vec4_type, "diffuse", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::vec4_type, "specular", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::vec4_type, "position", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::vec4_type, "halfVector", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::vec3_type, "spotDirection", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "spotExponent", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "spotCutoff", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "spotCosCutoff", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "constantAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "linearAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "quadraticAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, }; static const struct glsl_struct_field gl_LightModelParameters_fields[] = { - { glsl_type::vec4_type, "ambient", false, -1 }, + { glsl_type::vec4_type, "ambient", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, }; static const struct glsl_struct_field gl_LightModelProducts_fields[] = { - { glsl_type::vec4_type, "sceneColor", false, -1 }, + { glsl_type::vec4_type, "sceneColor", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, }; static const struct glsl_struct_field gl_LightProducts_fields[] = { - { glsl_type::vec4_type, "ambient", false, -1 }, - { glsl_type::vec4_type, "diffuse", false, -1 }, - { glsl_type::vec4_type, "specular", false, -1 }, + { glsl_type::vec4_type, "ambient", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::vec4_type, "diffuse", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::vec4_type, "specular", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, }; static const struct glsl_struct_field gl_FogParameters_fields[] = { - { glsl_type::vec4_type, "color", false, -1 }, - { glsl_type::float_type, "density", false, -1 }, - { glsl_type::float_type, "start", false, -1 }, - { glsl_type::float_type, "end", false, -1 }, - { glsl_type::float_type, "scale", false, -1 }, + { glsl_type::vec4_type, "color", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "density", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "start", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "end", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, + { glsl_type::float_type, "scale", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_INHERITED, 0 }, }; #include "builtin_type_macros.h" diff --git a/src/glsl/builtin_variables.cpp b/src/glsl/builtin_variables.cpp index 4c5b9c07093..5b6f4ae6220 100644 --- a/src/glsl/builtin_variables.cpp +++ b/src/glsl/builtin_variables.cpp @@ -317,7 +317,7 @@ per_vertex_accumulator::add_field(int slot, const glsl_type *type, assert(this->num_fields < ARRAY_SIZE(this->fields)); this->fields[this->num_fields].type = type; this->fields[this->num_fields].name = name; - this->fields[this->num_fields].row_major = false; + this->fields[this->num_fields].matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED; this->fields[this->num_fields].location = slot; this->fields[this->num_fields].interpolation = INTERP_QUALIFIER_NONE; this->fields[this->num_fields].centroid = 0; diff --git a/src/glsl/glsl_types.cpp b/src/glsl/glsl_types.cpp index 50e4fe558bc..66e9b133086 100644 --- a/src/glsl/glsl_types.cpp +++ b/src/glsl/glsl_types.cpp @@ -108,7 +108,7 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, this->fields.structure[i].interpolation = fields[i].interpolation; this->fields.structure[i].centroid = fields[i].centroid; this->fields.structure[i].sample = fields[i].sample; - this->fields.structure[i].row_major = fields[i].row_major; + this->fields.structure[i].matrix_layout = fields[i].matrix_layout; } } @@ -136,7 +136,7 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, this->fields.structure[i].interpolation = fields[i].interpolation; this->fields.structure[i].centroid = fields[i].centroid; this->fields.structure[i].sample = fields[i].sample; - this->fields.structure[i].row_major = fields[i].row_major; + this->fields.structure[i].matrix_layout = fields[i].matrix_layout; } } @@ -496,8 +496,8 @@ glsl_type::record_compare(const glsl_type *b) const if (strcmp(this->fields.structure[i].name, b->fields.structure[i].name) != 0) return false; - if (this->fields.structure[i].row_major - != b->fields.structure[i].row_major) + if (this->fields.structure[i].matrix_layout + != b->fields.structure[i].matrix_layout) return false; if (this->fields.structure[i].location != b->fields.structure[i].location) @@ -826,9 +826,18 @@ glsl_type::std140_base_alignment(bool row_major) const if (this->is_record()) { unsigned base_alignment = 16; for (unsigned i = 0; i < this->length; i++) { + bool field_row_major = row_major; + const enum glsl_matrix_layout matrix_layout = + glsl_matrix_layout(this->fields.structure[i].matrix_layout); + if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) { + field_row_major = true; + } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) { + field_row_major = false; + } + const struct glsl_type *field_type = this->fields.structure[i].type; base_alignment = MAX2(base_alignment, - field_type->std140_base_alignment(row_major)); + field_type->std140_base_alignment(field_row_major)); } return base_alignment; } @@ -937,10 +946,19 @@ glsl_type::std140_size(bool row_major) const unsigned max_align = 0; for (unsigned i = 0; i < this->length; i++) { + bool field_row_major = row_major; + const enum glsl_matrix_layout matrix_layout = + glsl_matrix_layout(this->fields.structure[i].matrix_layout); + if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) { + field_row_major = true; + } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) { + field_row_major = false; + } + const struct glsl_type *field_type = this->fields.structure[i].type; - unsigned align = field_type->std140_base_alignment(row_major); + unsigned align = field_type->std140_base_alignment(field_row_major); size = glsl_align(size, align); - size += field_type->std140_size(row_major); + size += field_type->std140_size(field_row_major); max_align = MAX2(align, max_align); diff --git a/src/glsl/glsl_types.h b/src/glsl/glsl_types.h index 92578ff47ac..d545533dcd2 100644 --- a/src/glsl/glsl_types.h +++ b/src/glsl/glsl_types.h @@ -79,6 +79,27 @@ enum glsl_interface_packing { GLSL_INTERFACE_PACKING_PACKED }; +enum glsl_matrix_layout { + /** + * The layout of the matrix is inherited from the object containing the + * matrix (the top level structure or the uniform block). + */ + GLSL_MATRIX_LAYOUT_INHERITED, + + /** + * Explicit column-major layout + * + * If a uniform block doesn't have an explicit layout set, it will default + * to this layout. + */ + GLSL_MATRIX_LAYOUT_COLUMN_MAJOR, + + /** + * Row-major layout + */ + GLSL_MATRIX_LAYOUT_ROW_MAJOR +}; + #ifdef __cplusplus #include "GL/gl.h" #include "util/ralloc.h" @@ -655,7 +676,6 @@ private: struct glsl_struct_field { const struct glsl_type *type; const char *name; - bool row_major; /** * For interface blocks, gl_varying_slot corresponding to the input/output @@ -684,6 +704,11 @@ struct glsl_struct_field { */ unsigned sample:1; + /** + * Layout of the matrix. Uses glsl_matrix_layout values. + */ + unsigned matrix_layout:2; + /** * For interface blocks, it has a value if this variable uses multiple vertex * streams (as in ir_variable::stream). -1 otherwise. diff --git a/src/glsl/link_uniforms.cpp b/src/glsl/link_uniforms.cpp index cfb23effcf7..5137e3a3910 100644 --- a/src/glsl/link_uniforms.cpp +++ b/src/glsl/link_uniforms.cpp @@ -182,7 +182,8 @@ program_resource_visitor::recursion(const glsl_type *t, char **name, } recursion(t->fields.structure[i].type, name, new_length, - t->fields.structure[i].row_major, record_type, + t->fields.structure[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR, + record_type, (i + 1) == t->length); /* Only the first leaf-field of the record gets called with the -- 2.30.2