X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fglsl%2Fglsl_types.cpp;h=8a34b8eb09c9e16384142e4e548d9b61d73a560f;hb=2bb91274e2cc2290ce8e790335f1e57b81d9d783;hp=40a5b6c482738b53c2da5c00688353d3eaa1ded3;hpb=42f3e7b6d7b42218feafe85a2328d8ce86fcce93;p=mesa.git diff --git a/src/glsl/glsl_types.cpp b/src/glsl/glsl_types.cpp index 40a5b6c4827..8a34b8eb09c 100644 --- a/src/glsl/glsl_types.cpp +++ b/src/glsl/glsl_types.cpp @@ -21,8 +21,9 @@ * DEALINGS IN THE SOFTWARE. */ -#include +#include #include +#include "main/core.h" /* for Elements */ #include "glsl_symbol_table.h" #include "glsl_parser_extras.h" #include "glsl_types.h" @@ -33,19 +34,19 @@ extern "C" { hash_table *glsl_type::array_types = NULL; hash_table *glsl_type::record_types = NULL; -void *glsl_type::ctx = NULL; +void *glsl_type::mem_ctx = NULL; void -glsl_type::init_talloc_type_ctx(void) +glsl_type::init_ralloc_type_ctx(void) { - if (glsl_type::ctx == NULL) { - glsl_type::ctx = talloc_init("glsl_type"); - assert(glsl_type::ctx != NULL); + if (glsl_type::mem_ctx == NULL) { + glsl_type::mem_ctx = ralloc_autofree_context(); + assert(glsl_type::mem_ctx != NULL); } } glsl_type::glsl_type(GLenum gl_type, - unsigned base_type, unsigned vector_elements, + glsl_base_type base_type, unsigned vector_elements, unsigned matrix_columns, const char *name) : gl_type(gl_type), base_type(base_type), @@ -54,8 +55,8 @@ glsl_type::glsl_type(GLenum gl_type, vector_elements(vector_elements), matrix_columns(matrix_columns), length(0) { - init_talloc_type_ctx(); - this->name = talloc_strdup(this->ctx, name); + init_ralloc_type_ctx(); + this->name = ralloc_strdup(this->mem_ctx, name); /* Neither dimension is zero or both dimensions are zero. */ assert((vector_elements == 0) == (matrix_columns == 0)); @@ -72,8 +73,8 @@ glsl_type::glsl_type(GLenum gl_type, vector_elements(0), matrix_columns(0), length(0) { - init_talloc_type_ctx(); - this->name = talloc_strdup(this->ctx, name); + init_ralloc_type_ctx(); + this->name = ralloc_strdup(this->mem_ctx, name); memset(& fields, 0, sizeof(fields)); } @@ -87,13 +88,13 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, { unsigned int i; - init_talloc_type_ctx(); - this->name = talloc_strdup(this->ctx, name); - this->fields.structure = talloc_array(this->ctx, + init_ralloc_type_ctx(); + this->name = ralloc_strdup(this->mem_ctx, name); + this->fields.structure = ralloc_array(this->mem_ctx, glsl_struct_field, length); for (i = 0; i < length; i++) { this->fields.structure[i].type = fields[i].type; - this->fields.structure[i].name = talloc_strdup(this->fields.structure, + this->fields.structure[i].name = ralloc_strdup(this->fields.structure, fields[i].name); } } @@ -110,9 +111,52 @@ add_types_to_symbol_table(glsl_symbol_table *symtab, } } +bool +glsl_type::contains_sampler() const +{ + if (this->is_array()) { + return this->fields.array->contains_sampler(); + } else if (this->is_record()) { + for (unsigned int i = 0; i < this->length; i++) { + if (this->fields.structure[i].type->contains_sampler()) + return true; + } + return false; + } else { + return this->is_sampler(); + } +} + +gl_texture_index +glsl_type::sampler_index() const +{ + const glsl_type *const t = (this->is_array()) ? this->fields.array : this; + + assert(t->is_sampler()); + + switch (t->sampler_dimensionality) { + case GLSL_SAMPLER_DIM_1D: + return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX; + case GLSL_SAMPLER_DIM_2D: + return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX; + case GLSL_SAMPLER_DIM_3D: + return TEXTURE_3D_INDEX; + case GLSL_SAMPLER_DIM_CUBE: + return TEXTURE_CUBE_INDEX; + case GLSL_SAMPLER_DIM_RECT: + return TEXTURE_RECT_INDEX; + case GLSL_SAMPLER_DIM_BUF: + return TEXTURE_BUFFER_INDEX; + case GLSL_SAMPLER_DIM_EXTERNAL: + return TEXTURE_EXTERNAL_INDEX; + default: + assert(!"Should not get here."); + return TEXTURE_BUFFER_INDEX; + } +} void -glsl_type::generate_110_types(glsl_symbol_table *symtab) +glsl_type::generate_100ES_types(glsl_symbol_table *symtab) { add_types_to_symbol_table(symtab, builtin_core_types, Elements(builtin_core_types), @@ -120,17 +164,30 @@ glsl_type::generate_110_types(glsl_symbol_table *symtab) add_types_to_symbol_table(symtab, builtin_structure_types, Elements(builtin_structure_types), false); - add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types, - Elements(builtin_110_deprecated_structure_types), + add_types_to_symbol_table(symtab, void_type, 1, false); +} + +void +glsl_type::generate_110_types(glsl_symbol_table *symtab, bool add_deprecated) +{ + generate_100ES_types(symtab); + + add_types_to_symbol_table(symtab, builtin_110_types, + Elements(builtin_110_types), false); - add_types_to_symbol_table(symtab, & void_type, 1, false); + add_types_to_symbol_table(symtab, &_sampler3D_type, 1, false); + if (add_deprecated) { + add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types, + Elements(builtin_110_deprecated_structure_types), + false); + } } void -glsl_type::generate_120_types(glsl_symbol_table *symtab) +glsl_type::generate_120_types(glsl_symbol_table *symtab, bool add_deprecated) { - generate_110_types(symtab); + generate_110_types(symtab, add_deprecated); add_types_to_symbol_table(symtab, builtin_120_types, Elements(builtin_120_types), false); @@ -138,12 +195,27 @@ glsl_type::generate_120_types(glsl_symbol_table *symtab) void -glsl_type::generate_130_types(glsl_symbol_table *symtab) +glsl_type::generate_130_types(glsl_symbol_table *symtab, bool add_deprecated) { - generate_120_types(symtab); + generate_120_types(symtab, add_deprecated); add_types_to_symbol_table(symtab, builtin_130_types, Elements(builtin_130_types), false); + generate_EXT_texture_array_types(symtab, false); +} + + +void +glsl_type::generate_140_types(glsl_symbol_table *symtab) +{ + generate_130_types(symtab, false); + + add_types_to_symbol_table(symtab, builtin_140_types, + Elements(builtin_140_types), false); + + add_types_to_symbol_table(symtab, builtin_EXT_texture_buffer_object_types, + Elements(builtin_EXT_texture_buffer_object_types), + false); } @@ -167,34 +239,71 @@ glsl_type::generate_EXT_texture_array_types(glsl_symbol_table *symtab, } +void +glsl_type::generate_OES_texture_3D_types(glsl_symbol_table *symtab, bool warn) +{ + add_types_to_symbol_table(symtab, &_sampler3D_type, 1, warn); +} + + +void +glsl_type::generate_OES_EGL_image_external_types(glsl_symbol_table *symtab, + bool warn) +{ + add_types_to_symbol_table(symtab, builtin_OES_EGL_image_external_types, + Elements(builtin_OES_EGL_image_external_types), + warn); +} + void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state) { switch (state->language_version) { + case 100: + assert(state->es_shader); + glsl_type::generate_100ES_types(state->symbols); + break; case 110: - glsl_type::generate_110_types(state->symbols); + glsl_type::generate_110_types(state->symbols, true); break; case 120: - glsl_type::generate_120_types(state->symbols); + glsl_type::generate_120_types(state->symbols, true); break; case 130: - glsl_type::generate_130_types(state->symbols); + glsl_type::generate_130_types(state->symbols, true); + break; + case 140: + glsl_type::generate_140_types(state->symbols); break; default: /* error */ break; } - if (state->ARB_texture_rectangle_enable) { + if (state->ARB_texture_rectangle_enable || + state->language_version >= 140) { glsl_type::generate_ARB_texture_rectangle_types(state->symbols, state->ARB_texture_rectangle_warn); } + if (state->OES_texture_3D_enable && state->language_version == 100) { + glsl_type::generate_OES_texture_3D_types(state->symbols, + state->OES_texture_3D_warn); + } if (state->EXT_texture_array_enable && state->language_version < 130) { // These are already included in 130; don't create twice. glsl_type::generate_EXT_texture_array_types(state->symbols, state->EXT_texture_array_warn); } + + /* We cannot check for language_version == 100 here because we need the + * types to support fixed-function program generation. But this is fine + * since the extension is never enabled for OpenGL contexts. + */ + if (state->OES_EGL_image_external_enable) { + glsl_type::generate_OES_EGL_image_external_types(state->symbols, + state->OES_EGL_image_external_warn); + } } @@ -215,6 +324,29 @@ const glsl_type *glsl_type::get_base_type() const } +const glsl_type *glsl_type::get_scalar_type() const +{ + const glsl_type *type = this; + + /* Handle arrays */ + while (type->base_type == GLSL_TYPE_ARRAY) + type = type->fields.array; + + /* Handle vectors and matrices */ + switch (type->base_type) { + case GLSL_TYPE_UINT: + return uint_type; + case GLSL_TYPE_INT: + return int_type; + case GLSL_TYPE_FLOAT: + return float_type; + default: + /* Handle everything else */ + return type; + } +} + + void _mesa_glsl_release_types(void) { @@ -227,72 +359,6 @@ _mesa_glsl_release_types(void) hash_table_dtor(glsl_type::record_types); glsl_type::record_types = NULL; } - - if (glsl_type::ctx != NULL) { - talloc_free(glsl_type::ctx); - glsl_type::ctx = NULL; - } -} - - -ir_function * -glsl_type::generate_constructor(glsl_symbol_table *symtab) const -{ - void *ctx = symtab; - - /* Generate the function name and add it to the symbol table. - */ - ir_function *const f = new(ctx) ir_function(name); - - bool added = symtab->add_function(name, f); - assert(added); - - ir_function_signature *const sig = new(ctx) ir_function_signature(this); - f->add_signature(sig); - - ir_variable **declarations = - (ir_variable **) malloc(sizeof(ir_variable *) * this->length); - for (unsigned i = 0; i < length; i++) { - char *const param_name = (char *) malloc(10); - - snprintf(param_name, 10, "p%08X", i); - - ir_variable *var = (this->base_type == GLSL_TYPE_ARRAY) - ? new(ctx) ir_variable(fields.array, param_name, ir_var_in) - : new(ctx) ir_variable(fields.structure[i].type, param_name, ir_var_in); - - declarations[i] = var; - sig->parameters.push_tail(var); - } - - /* Generate the body of the constructor. The body assigns each of the - * parameters to a portion of a local variable called __retval that has - * the same type as the constructor. After initializing __retval, - * __retval is returned. - */ - ir_variable *retval = new(ctx) ir_variable(this, "__retval", ir_var_auto); - sig->body.push_tail(retval); - - for (unsigned i = 0; i < length; i++) { - ir_dereference *const lhs = (this->base_type == GLSL_TYPE_ARRAY) - ? (ir_dereference *) new(ctx) ir_dereference_array(retval, - new(ctx) ir_constant(i)) - : (ir_dereference *) new(ctx) ir_dereference_record(retval, - fields.structure[i].name); - - ir_dereference *const rhs = new(ctx) ir_dereference_variable(declarations[i]); - ir_instruction *const assign = new(ctx) ir_assignment(lhs, rhs, NULL); - - sig->body.push_tail(assign); - } - - free(declarations); - - ir_dereference *const retref = new(ctx) ir_dereference_variable(retval); - ir_instruction *const inst = new(ctx) ir_return(retref); - sig->body.push_tail(inst); - - return f; } @@ -315,7 +381,7 @@ glsl_type::glsl_type(const glsl_type *array, unsigned length) : * NUL. */ const unsigned name_length = strlen(array->name) + 10 + 3; - char *const n = (char *) talloc_size(this->ctx, name_length); + char *const n = (char *) ralloc_size(this->mem_ctx, name_length); if (length == 0) snprintf(n, name_length, "%s[]", array->name); @@ -330,7 +396,7 @@ const glsl_type * glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns) { if (base_type == GLSL_TYPE_VOID) - return &void_type; + return void_type; if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4)) return error_type; @@ -399,13 +465,13 @@ glsl_type::get_array_instance(const glsl_type *base, unsigned array_size) * named 'foo'. */ char key[128]; - snprintf(key, sizeof(key), "%p[%u]", base, array_size); + snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size); const glsl_type *t = (glsl_type *) hash_table_find(array_types, key); if (t == NULL) { t = new glsl_type(base, array_size); - hash_table_insert(array_types, (void *) t, talloc_strdup(ctx, key)); + hash_table_insert(array_types, (void *) t, ralloc_strdup(mem_ctx, key)); } assert(t->base_type == GLSL_TYPE_ARRAY); @@ -457,7 +523,7 @@ glsl_type::record_key_hash(const void *a) break; size += snprintf(& hash_key[size], sizeof(hash_key) - size, - "%p", key->fields.structure[i].type); + "%p", (void *) key->fields.structure[i].type); } return hash_table_string_hash(& hash_key); @@ -546,3 +612,19 @@ glsl_type::component_slots() const return 0; } } + +bool +glsl_type::can_implicitly_convert_to(const glsl_type *desired) const +{ + if (this == desired) + return true; + + /* There is no conversion among matrix types. */ + if (this->matrix_columns > 1 || desired->matrix_columns > 1) + return false; + + /* int and uint can be converted to float. */ + return desired->is_float() + && this->is_integer() + && this->vector_elements == desired->vector_elements; +}