From: Ian Romanick Date: Wed, 10 Mar 2010 18:43:16 +0000 (-0800) Subject: IR variable: Initial work to support GLSL built-in variables X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=adfb0cd7401251bef0c854ac945fce78f0ed11db;p=mesa.git IR variable: Initial work to support GLSL built-in variables --- diff --git a/Makefile.am b/Makefile.am index 60317f8b4d3..fd0d4f69469 100644 --- a/Makefile.am +++ b/Makefile.am @@ -26,7 +26,7 @@ bin_PROGRAMS = glsl glsl_SOURCES = symbol_table.c hash_table.c glsl_types.cpp \ glsl_parser.ypp glsl_lexer.lpp glsl_parser_extras.cpp \ ast_expr.cpp ast_to_hir.cpp ir.cpp hir_field_selection.cpp \ - ir_print_visitor.cpp + ir_print_visitor.cpp ir_variable.cpp BUILT_SOURCES = glsl_parser.h builtin_types.h diff --git a/ast_to_hir.cpp b/ast_to_hir.cpp index e371007ef8b..1379ec98013 100644 --- a/ast_to_hir.cpp +++ b/ast_to_hir.cpp @@ -61,6 +61,8 @@ _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) { struct simple_node *ptr; + _mesa_glsl_initialize_variables(instructions, state); + foreach (ptr, & state->translation_unit) { ((ast_node *)ptr)->hir(instructions, state); } diff --git a/builtin_variables.h b/builtin_variables.h new file mode 100644 index 00000000000..9693217e990 --- /dev/null +++ b/builtin_variables.h @@ -0,0 +1,59 @@ +/* + * Copyright © 2010 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +struct builtin_variable { + enum ir_variable_mode mode; + const char *type; + const char *name; +}; + +static const builtin_variable builtin_core_vs_variables[] = { + { ir_var_out, "vec4", "gl_Position" }, + { ir_var_out, "float", "gl_PointSize" }, +}; + +static const builtin_variable builtin_110_deprecated_vs_variables[] = { + { ir_var_in, "vec4", "gl_Vertex" }, + { ir_var_in, "vec4", "gl_Normal" }, + { ir_var_in, "vec4", "gl_Color" }, + { ir_var_in, "vec4", "gl_SecondaryColor" }, + { ir_var_in, "vec4", "gl_MultiTexCoord0" }, + { ir_var_in, "vec4", "gl_MultiTexCoord1" }, + { ir_var_in, "vec4", "gl_MultiTexCoord2" }, + { ir_var_in, "vec4", "gl_MultiTexCoord3" }, + { ir_var_in, "vec4", "gl_MultiTexCoord4" }, + { ir_var_in, "vec4", "gl_MultiTexCoord5" }, + { ir_var_in, "vec4", "gl_MultiTexCoord6" }, + { ir_var_in, "vec4", "gl_MultiTexCoord7" }, + { ir_var_in, "float", "gl_FogCoord" }, + { ir_var_out, "vec4", "gl_ClipVertex" }, + { ir_var_out, "vec4", "gl_FrontColor" }, + { ir_var_out, "vec4", "gl_BackColor" }, + { ir_var_out, "vec4", "gl_FrontSecondaryColor" }, + { ir_var_out, "vec4", "gl_BackSecondaryColor" }, + { ir_var_out, "vec4", "gl_FogFragCoord" }, +}; + +static const builtin_variable builtin_130_vs_variables[] = { + { ir_var_in, "int", "gl_VertexID" }, +}; diff --git a/ir.h b/ir.h index 8d59d21dcb3..5087e4560e4 100644 --- a/ir.h +++ b/ir.h @@ -345,3 +345,7 @@ public: } value; }; + +extern void +_mesa_glsl_initialize_variables(exec_list *instructions, + struct _mesa_glsl_parse_state *state); diff --git a/ir_variable.cpp b/ir_variable.cpp new file mode 100644 index 00000000000..e7cb43f9269 --- /dev/null +++ b/ir_variable.cpp @@ -0,0 +1,144 @@ +/* + * Copyright © 2010 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "glsl_parser_extras.h" +#include "symbol_table.h" +#include "ir.h" +#include "builtin_variables.h" + +#ifndef Elements +#define Elements(x) (sizeof(x)/sizeof(*(x))) +#endif + +static void +add_builtin_variable(const builtin_variable *proto, exec_list *instructions, + struct _mesa_symbol_table *symtab) +{ + /* Create a new variable declaration from the description supplied by + * the caller. + */ + const glsl_type *const type = (glsl_type *) + _mesa_symbol_table_find_symbol(symtab, 0, proto->type); + + assert(type != NULL); + + ir_variable *var = new ir_variable(type, proto->name); + + var->mode = proto->mode; + if (var->mode != ir_var_out) + var->read_only = true; + + + /* Once the variable is created an initialized, add it to the symbol table + * and add the declaration to the IR stream. + */ + instructions->push_tail(var); + + _mesa_symbol_table_add_symbol(symtab, 0, var->name, var); +} + + +static void +generate_110_vs_variables(exec_list *instructions, + struct _mesa_symbol_table *symtab) +{ + for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) { + add_builtin_variable(& builtin_core_vs_variables[i], + instructions, symtab); + } + + for (unsigned i = 0 + ; i < Elements(builtin_110_deprecated_vs_variables) + ; i++) { + add_builtin_variable(& builtin_110_deprecated_vs_variables[i], + instructions, symtab); + } + + /* FINISHME: Add support fo gl_TexCoord. The size of this array is + * FINISHME: implementation dependent based on the value of + * FINISHME: GL_MAX_TEXTURE_COORDS. + */ +} + + +static void +generate_120_vs_variables(exec_list *instructions, + struct _mesa_symbol_table *symtab) +{ + /* GLSL version 1.20 did not add any built-in variables in the vertex + * shader. + */ + generate_110_vs_variables(instructions, symtab); +} + + +static void +generate_130_vs_variables(exec_list *instructions, + struct _mesa_symbol_table *symtab) +{ + generate_120_vs_variables(instructions, symtab); + + for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) { + add_builtin_variable(& builtin_130_vs_variables[i], + instructions, symtab); + } + + /* FINISHME: Add support fo gl_ClipDistance. The size of this array is + * FINISHME: implementation dependent based on the value of + * FINISHME: GL_MAX_CLIP_DISTANCES. + */ +} + + +static void +initialize_vs_variables(exec_list *instructions, + struct _mesa_glsl_parse_state *state) +{ + + switch (state->language_version) { + case 110: + generate_110_vs_variables(instructions, state->symbols); + break; + case 120: + generate_120_vs_variables(instructions, state->symbols); + break; + case 130: + generate_130_vs_variables(instructions, state->symbols); + break; + } +} + + +void +_mesa_glsl_initialize_variables(exec_list *instructions, + struct _mesa_glsl_parse_state *state) +{ + switch (state->target) { + case vertex_shader: + initialize_vs_variables(instructions, state); + break; + case geometry_shader: + case fragment_shader: + break; + } +}