blob: 283842c54e07e6326f16f6746a9b9a385f7c0944 [file] [log] [blame]
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001/*
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"
Ian Romanick8bde4ce2010-03-19 11:57:24 -070025#include "glsl_symbol_table.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080026#include "ir.h"
27#include "builtin_variables.h"
28
29#ifndef Elements
30#define Elements(x) (sizeof(x)/sizeof(*(x)))
31#endif
32
33static void
34add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070035 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080036{
37 /* Create a new variable declaration from the description supplied by
38 * the caller.
39 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -070040 const glsl_type *const type = symtab->get_type(proto->type);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080041
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
Ian Romanick8bde4ce2010-03-19 11:57:24 -070056 symtab->add_variable(var->name, var);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080057}
58
59
60static void
61generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070062 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080063{
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
83static void
84generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070085 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080086{
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
94static void
95generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070096 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080097{
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
112static void
113initialize_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
131void
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}