Path: blob/21.2-virgl/src/compiler/glsl/ast_array_index.cpp
4547 views
/*1* Copyright © 2010 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*/2223#include "ast.h"24#include "compiler/glsl_types.h"25#include "ir.h"2627void28ast_array_specifier::print(void) const29{30foreach_list_typed (ast_node, array_dimension, link, &this->array_dimensions) {31printf("[ ");32if (((ast_expression*)array_dimension)->oper != ast_unsized_array_dim)33array_dimension->print();34printf("] ");35}36}3738/**39* If \c ir is a reference to an array for which we are tracking the max array40* element accessed, track that the given element has been accessed.41* Otherwise do nothing.42*43* This function also checks whether the array is a built-in array whose44* maximum size is too small to accommodate the given index, and if so uses45* loc and state to report the error.46*/47static void48update_max_array_access(ir_rvalue *ir, int idx, YYLTYPE *loc,49struct _mesa_glsl_parse_state *state)50{51if (ir_dereference_variable *deref_var = ir->as_dereference_variable()) {52ir_variable *var = deref_var->var;53if (idx > (int)var->data.max_array_access) {54var->data.max_array_access = idx;5556/* Check whether this access will, as a side effect, implicitly cause57* the size of a built-in array to be too large.58*/59check_builtin_array_max_size(var->name, idx+1, *loc, state);60}61} else if (ir_dereference_record *deref_record =62ir->as_dereference_record()) {63/* There are three possibilities we need to consider:64*65* - Accessing an element of an array that is a member of a named66* interface block (e.g. ifc.foo[i])67*68* - Accessing an element of an array that is a member of a named69* interface block array (e.g. ifc[j].foo[i]).70*71* - Accessing an element of an array that is a member of a named72* interface block array of arrays (e.g. ifc[j][k].foo[i]).73*/74ir_dereference_variable *deref_var =75deref_record->record->as_dereference_variable();76if (deref_var == NULL) {77ir_dereference_array *deref_array =78deref_record->record->as_dereference_array();79ir_dereference_array *deref_array_prev = NULL;80while (deref_array != NULL) {81deref_array_prev = deref_array;82deref_array = deref_array->array->as_dereference_array();83}84if (deref_array_prev != NULL)85deref_var = deref_array_prev->array->as_dereference_variable();86}8788if (deref_var != NULL) {89if (deref_var->var->is_interface_instance()) {90unsigned field_idx = deref_record->field_idx;91assert(field_idx < deref_var->var->get_interface_type()->length);9293int *const max_ifc_array_access =94deref_var->var->get_max_ifc_array_access();9596assert(max_ifc_array_access != NULL);9798if (idx > max_ifc_array_access[field_idx]) {99max_ifc_array_access[field_idx] = idx;100101/* Check whether this access will, as a side effect, implicitly102* cause the size of a built-in array to be too large.103*/104const char *field_name =105deref_record->record->type->fields.structure[field_idx].name;106check_builtin_array_max_size(field_name, idx+1, *loc, state);107}108}109}110}111}112113114static int115get_implicit_array_size(struct _mesa_glsl_parse_state *state,116ir_rvalue *array)117{118ir_variable *var = array->variable_referenced();119120/* Inputs in control shader are implicitly sized121* to the maximum patch size.122*/123if (state->stage == MESA_SHADER_TESS_CTRL &&124var->data.mode == ir_var_shader_in) {125return state->Const.MaxPatchVertices;126}127128/* Non-patch inputs in evaluation shader are implicitly sized129* to the maximum patch size.130*/131if (state->stage == MESA_SHADER_TESS_EVAL &&132var->data.mode == ir_var_shader_in &&133!var->data.patch) {134return state->Const.MaxPatchVertices;135}136137return 0;138}139140141ir_rvalue *142_mesa_ast_array_index_to_hir(void *mem_ctx,143struct _mesa_glsl_parse_state *state,144ir_rvalue *array, ir_rvalue *idx,145YYLTYPE &loc, YYLTYPE &idx_loc)146{147if (!array->type->is_error()148&& !array->type->is_array()149&& !array->type->is_matrix()150&& !array->type->is_vector()) {151_mesa_glsl_error(& idx_loc, state,152"cannot dereference non-array / non-matrix / "153"non-vector");154}155156if (!idx->type->is_error()) {157if (!idx->type->is_integer_32()) {158_mesa_glsl_error(& idx_loc, state, "array index must be integer type");159} else if (!idx->type->is_scalar()) {160_mesa_glsl_error(& idx_loc, state, "array index must be scalar");161}162}163164/* If the array index is a constant expression and the array has a165* declared size, ensure that the access is in-bounds. If the array166* index is not a constant expression, ensure that the array has a167* declared size.168*/169ir_constant *const const_index = idx->constant_expression_value(mem_ctx);170if (const_index != NULL && idx->type->is_integer_32()) {171const int idx = const_index->value.i[0];172const char *type_name = "error";173unsigned bound = 0;174175/* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:176*177* "It is illegal to declare an array with a size, and then178* later (in the same shader) index the same array with an179* integral constant expression greater than or equal to the180* declared size. It is also illegal to index an array with a181* negative constant expression."182*/183if (array->type->is_matrix()) {184if (array->type->row_type()->vector_elements <= idx) {185type_name = "matrix";186bound = array->type->row_type()->vector_elements;187}188} else if (array->type->is_vector()) {189if (array->type->vector_elements <= idx) {190type_name = "vector";191bound = array->type->vector_elements;192}193} else {194/* glsl_type::array_size() returns -1 for non-array types. This means195* that we don't need to verify that the type is an array before196* doing the bounds checking.197*/198if ((array->type->array_size() > 0)199&& (array->type->array_size() <= idx)) {200type_name = "array";201bound = array->type->array_size();202}203}204205if (bound > 0) {206_mesa_glsl_error(& loc, state, "%s index must be < %u",207type_name, bound);208} else if (idx < 0) {209_mesa_glsl_error(& loc, state, "%s index must be >= 0", type_name);210}211212if (array->type->is_array())213update_max_array_access(array, idx, &loc, state);214} else if (const_index == NULL && array->type->is_array()) {215if (array->type->is_unsized_array()) {216int implicit_size = get_implicit_array_size(state, array);217if (implicit_size) {218ir_variable *v = array->whole_variable_referenced();219if (v != NULL)220v->data.max_array_access = implicit_size - 1;221}222else if (state->stage == MESA_SHADER_TESS_CTRL &&223array->variable_referenced()->data.mode == ir_var_shader_out &&224!array->variable_referenced()->data.patch) {225/* Tessellation control shader output non-patch arrays are226* initially unsized. Despite that, they are allowed to be227* indexed with a non-constant expression (typically228* "gl_InvocationID"). The array size will be determined229* by the linker.230*/231}232else if (array->variable_referenced()->data.mode !=233ir_var_shader_storage) {234_mesa_glsl_error(&loc, state, "unsized array index must be constant");235} else {236/* Unsized array non-constant indexing on SSBO is allowed only for237* the last member of the SSBO definition.238*/239ir_variable *var = array->variable_referenced();240const glsl_type *iface_type = var->get_interface_type();241int field_index = iface_type->field_index(var->name);242/* Field index can be < 0 for instance arrays */243if (field_index >= 0 &&244field_index != (int) iface_type->length - 1) {245_mesa_glsl_error(&loc, state, "Indirect access on unsized "246"array is limited to the last member of "247"SSBO.");248}249}250} else if (array->type->without_array()->is_interface()251&& ((array->variable_referenced()->data.mode == ir_var_uniform252&& !state->is_version(400, 320)253&& !state->ARB_gpu_shader5_enable254&& !state->EXT_gpu_shader5_enable255&& !state->OES_gpu_shader5_enable) ||256(array->variable_referenced()->data.mode == ir_var_shader_storage257&& !state->is_version(400, 0)258&& !state->ARB_gpu_shader5_enable))) {259/* Page 50 in section 4.3.9 of the OpenGL ES 3.10 spec says:260*261* "All indices used to index a uniform or shader storage block262* array must be constant integral expressions."263*264* But OES_gpu_shader5 (and ESSL 3.20) relax this to allow indexing265* on uniform blocks but not shader storage blocks.266*267*/268_mesa_glsl_error(&loc, state, "%s block array index must be constant",269array->variable_referenced()->data.mode270== ir_var_uniform ? "uniform" : "shader storage");271} else {272/* whole_variable_referenced can return NULL if the array is a273* member of a structure. In this case it is safe to not update274* the max_array_access field because it is never used for fields275* of structures.276*/277ir_variable *v = array->whole_variable_referenced();278if (v != NULL)279v->data.max_array_access = array->type->array_size() - 1;280}281282/* From page 23 (29 of the PDF) of the GLSL 1.30 spec:283*284* "Samplers aggregated into arrays within a shader (using square285* brackets [ ]) can only be indexed with integral constant286* expressions [...]."287*288* This restriction was added in GLSL 1.30. Shaders using earlier289* version of the language should not be rejected by the compiler290* front-end for using this construct. This allows useful things such291* as using a loop counter as the index to an array of samplers. If the292* loop in unrolled, the code should compile correctly. Instead, emit a293* warning.294*295* In GLSL 4.00 / ARB_gpu_shader5, this requirement is relaxed again to allow296* indexing with dynamically uniform expressions. Note that these are not297* required to be uniforms or expressions based on them, but merely that the298* values must not diverge between shader invocations run together. If the299* values *do* diverge, then the behavior of the operation requiring a300* dynamically uniform expression is undefined.301*302* From section 4.1.7 of the ARB_bindless_texture spec:303*304* "Samplers aggregated into arrays within a shader (using square305* brackets []) can be indexed with arbitrary integer expressions."306*/307if (array->type->without_array()->is_sampler()) {308if (!state->is_version(400, 320) &&309!state->ARB_gpu_shader5_enable &&310!state->EXT_gpu_shader5_enable &&311!state->OES_gpu_shader5_enable &&312!state->has_bindless()) {313if (state->is_version(130, 300))314_mesa_glsl_error(&loc, state,315"sampler arrays indexed with non-constant "316"expressions are forbidden in GLSL %s "317"and later",318state->es_shader ? "ES 3.00" : "1.30");319else if (state->es_shader)320_mesa_glsl_warning(&loc, state,321"sampler arrays indexed with non-constant "322"expressions will be forbidden in GLSL "323"3.00 and later");324else325_mesa_glsl_warning(&loc, state,326"sampler arrays indexed with non-constant "327"expressions will be forbidden in GLSL "328"1.30 and later");329}330}331332/* From page 27 of the GLSL ES 3.1 specification:333*334* "When aggregated into arrays within a shader, images can only be335* indexed with a constant integral expression."336*337* On the other hand the desktop GL specification extension allows338* non-constant indexing of image arrays, but behavior is left undefined339* in cases where the indexing expression is not dynamically uniform.340*/341if (state->es_shader && array->type->without_array()->is_image()) {342_mesa_glsl_error(&loc, state,343"image arrays indexed with non-constant "344"expressions are forbidden in GLSL ES.");345}346}347348/* After performing all of the error checking, generate the IR for the349* expression.350*/351if (array->type->is_array()352|| array->type->is_matrix()353|| array->type->is_vector()) {354return new(mem_ctx) ir_dereference_array(array, idx);355} else if (array->type->is_error()) {356return array;357} else {358ir_rvalue *result = new(mem_ctx) ir_dereference_array(array, idx);359result->type = glsl_type::error_type;360361return result;362}363}364365366