From: Ian Romanick Date: Thu, 25 Mar 2010 00:11:30 +0000 (-0700) Subject: Add glsl_type::get_instance method X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=3209c4e3692eaa9468aadcd21ce402e6b0d5b7dd;p=mesa.git Add glsl_type::get_instance method Gets the singleton corresponding to a particular scalar, vector, or matrix type. --- diff --git a/glsl_types.cpp b/glsl_types.cpp index 2b06a335538..6dcbba8e8cd 100644 --- a/glsl_types.cpp +++ b/glsl_types.cpp @@ -159,3 +159,59 @@ const glsl_type *glsl_type::get_base_type() const return glsl_error_type; } } + + +const glsl_type * +glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns) +{ + if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4)) + return glsl_error_type; + + + /* Treat GLSL vectors as Nx1 matrices. + */ + if (columns == 1) { + switch (base_type) { + case GLSL_TYPE_UINT: + return glsl_uint_type + (rows - 1); + case GLSL_TYPE_INT: + return glsl_int_type + (rows - 1); + case GLSL_TYPE_FLOAT: + return glsl_float_type + (rows - 1); + case GLSL_TYPE_BOOL: + return glsl_bool_type + (rows - 1); + default: + return glsl_error_type; + } + } else { + if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1)) + return glsl_error_type; + + /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following + * combinations are valid: + * + * 1 2 3 4 + * 1 + * 2 x x x + * 3 x x x + * 4 x x x + */ +#define IDX(c,r) (((c-1)*3) + (r-1)) + + switch (IDX(columns, rows)) { + case IDX(2,2): return mat2_type; + case IDX(2,3): return mat2x3_type; + case IDX(2,4): return mat2x4_type; + case IDX(3,2): return mat3x2_type; + case IDX(3,3): return mat3_type; + case IDX(3,4): return mat3x4_type; + case IDX(4,2): return mat4x2_type; + case IDX(4,3): return mat4x3_type; + case IDX(4,4): return mat4_type; + default: return glsl_error_type; + } + } + + assert(!"Should not get here."); + return glsl_error_type; +} diff --git a/glsl_types.h b/glsl_types.h index 4a92712b211..a7897719fb4 100644 --- a/glsl_types.h +++ b/glsl_types.h @@ -147,6 +147,12 @@ struct glsl_type { */ const glsl_type *get_base_type() const; + /** + * Get the instance of a built-in scalar, vector, or matrix type + */ + static const glsl_type *get_instance(unsigned base_type, unsigned rows, + unsigned columns); + /** * Query whether or not a type is a scalar (non-vector and non-matrix). */