Use glsl_type::get_instance instead of _mesa_glsl_get_vector_type
[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 "glsl_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 glsl_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 = symtab->get_type(proto->type);
41
42 assert(type != NULL);
43
44 ir_variable *var = new ir_variable(type, proto->name);
45
46 var->mode = proto->mode;
47 if (var->mode != ir_var_out)
48 var->read_only = true;
49
50
51 /* Once the variable is created an initialized, add it to the symbol table
52 * and add the declaration to the IR stream.
53 */
54 instructions->push_tail(var);
55
56 symtab->add_variable(var->name, var);
57 }
58
59
60 static void
61 generate_110_vs_variables(exec_list *instructions,
62 glsl_symbol_table *symtab)
63 {
64 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
65 add_builtin_variable(& builtin_core_vs_variables[i],
66 instructions, symtab);
67 }
68
69 for (unsigned i = 0
70 ; i < Elements(builtin_110_deprecated_vs_variables)
71 ; i++) {
72 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
73 instructions, symtab);
74 }
75
76 /* FINISHME: Add support fo gl_TexCoord. The size of this array is
77 * FINISHME: implementation dependent based on the value of
78 * FINISHME: GL_MAX_TEXTURE_COORDS.
79 */
80 }
81
82
83 static void
84 generate_120_vs_variables(exec_list *instructions,
85 glsl_symbol_table *symtab)
86 {
87 /* GLSL version 1.20 did not add any built-in variables in the vertex
88 * shader.
89 */
90 generate_110_vs_variables(instructions, symtab);
91 }
92
93
94 static void
95 generate_130_vs_variables(exec_list *instructions,
96 glsl_symbol_table *symtab)
97 {
98 generate_120_vs_variables(instructions, symtab);
99
100 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
101 add_builtin_variable(& builtin_130_vs_variables[i],
102 instructions, symtab);
103 }
104
105 /* FINISHME: Add support fo gl_ClipDistance. The size of this array is
106 * FINISHME: implementation dependent based on the value of
107 * FINISHME: GL_MAX_CLIP_DISTANCES.
108 */
109 }
110
111
112 static void
113 initialize_vs_variables(exec_list *instructions,
114 struct _mesa_glsl_parse_state *state)
115 {
116
117 switch (state->language_version) {
118 case 110:
119 generate_110_vs_variables(instructions, state->symbols);
120 break;
121 case 120:
122 generate_120_vs_variables(instructions, state->symbols);
123 break;
124 case 130:
125 generate_130_vs_variables(instructions, state->symbols);
126 break;
127 }
128 }
129
130
131 void
132 _mesa_glsl_initialize_variables(exec_list *instructions,
133 struct _mesa_glsl_parse_state *state)
134 {
135 switch (state->target) {
136 case vertex_shader:
137 initialize_vs_variables(instructions, state);
138 break;
139 case geometry_shader:
140 case fragment_shader:
141 break;
142 }
143 }