Add new constructors for ast_type_specifier
[mesa.git] / ir_variable.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "glsl_parser_extras.h"
25 #include "symbol_table.h"
26 #include "ir.h"
27 #include "builtin_variables.h"
28
29 #ifndef Elements
30 #define Elements(x) (sizeof(x)/sizeof(*(x)))
31 #endif
32
33 static void
34 add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
35 struct _mesa_symbol_table *symtab)
36 {
37 /* Create a new variable declaration from the description supplied by
38 * the caller.
39 */
40 const glsl_type *const type = (glsl_type *)
41 _mesa_symbol_table_find_symbol(symtab, 0, proto->type);
42
43 assert(type != NULL);
44
45 ir_variable *var = new ir_variable(type, proto->name);
46
47 var->mode = proto->mode;
48 if (var->mode != ir_var_out)
49 var->read_only = true;
50
51
52 /* Once the variable is created an initialized, add it to the symbol table
53 * and add the declaration to the IR stream.
54 */
55 instructions->push_tail(var);
56
57 _mesa_symbol_table_add_symbol(symtab, 0, var->name, var);
58 }
59
60
61 static void
62 generate_110_vs_variables(exec_list *instructions,
63 struct _mesa_symbol_table *symtab)
64 {
65 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
66 add_builtin_variable(& builtin_core_vs_variables[i],
67 instructions, symtab);
68 }
69
70 for (unsigned i = 0
71 ; i < Elements(builtin_110_deprecated_vs_variables)
72 ; i++) {
73 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
74 instructions, symtab);
75 }
76
77 /* FINISHME: Add support fo gl_TexCoord. The size of this array is
78 * FINISHME: implementation dependent based on the value of
79 * FINISHME: GL_MAX_TEXTURE_COORDS.
80 */
81 }
82
83
84 static void
85 generate_120_vs_variables(exec_list *instructions,
86 struct _mesa_symbol_table *symtab)
87 {
88 /* GLSL version 1.20 did not add any built-in variables in the vertex
89 * shader.
90 */
91 generate_110_vs_variables(instructions, symtab);
92 }
93
94
95 static void
96 generate_130_vs_variables(exec_list *instructions,
97 struct _mesa_symbol_table *symtab)
98 {
99 generate_120_vs_variables(instructions, symtab);
100
101 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
102 add_builtin_variable(& builtin_130_vs_variables[i],
103 instructions, symtab);
104 }
105
106 /* FINISHME: Add support fo gl_ClipDistance. The size of this array is
107 * FINISHME: implementation dependent based on the value of
108 * FINISHME: GL_MAX_CLIP_DISTANCES.
109 */
110 }
111
112
113 static void
114 initialize_vs_variables(exec_list *instructions,
115 struct _mesa_glsl_parse_state *state)
116 {
117
118 switch (state->language_version) {
119 case 110:
120 generate_110_vs_variables(instructions, state->symbols);
121 break;
122 case 120:
123 generate_120_vs_variables(instructions, state->symbols);
124 break;
125 case 130:
126 generate_130_vs_variables(instructions, state->symbols);
127 break;
128 }
129 }
130
131
132 void
133 _mesa_glsl_initialize_variables(exec_list *instructions,
134 struct _mesa_glsl_parse_state *state)
135 {
136 switch (state->target) {
137 case vertex_shader:
138 initialize_vs_variables(instructions, state);
139 break;
140 case geometry_shader:
141 case fragment_shader:
142 break;
143 }
144 }