Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 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" |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 25 | #include "glsl_symbol_table.h" |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 26 | #include "ir.h" |
| 27 | #include "builtin_variables.h" |
| 28 | |
| 29 | #ifndef Elements |
| 30 | #define Elements(x) (sizeof(x)/sizeof(*(x))) |
| 31 | #endif |
| 32 | |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 33 | static ir_variable * |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 34 | add_variable(const char *name, enum ir_variable_mode mode, |
| 35 | const glsl_type *type, exec_list *instructions, |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 36 | glsl_symbol_table *symtab) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 37 | { |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 38 | ir_variable *var = new ir_variable(type, name); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 39 | |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 40 | var->mode = mode; |
Eric Anholt | 71df19f | 2010-04-19 11:10:37 -0700 | [diff] [blame^] | 41 | switch (var->mode) { |
| 42 | case ir_var_in: |
| 43 | var->shader_in = true; |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 44 | var->read_only = true; |
Eric Anholt | 71df19f | 2010-04-19 11:10:37 -0700 | [diff] [blame^] | 45 | break; |
| 46 | case ir_var_inout: |
| 47 | var->shader_in = true; |
| 48 | var->shader_out = true; |
| 49 | case ir_var_out: |
| 50 | var->shader_out = true; |
| 51 | break; |
| 52 | case ir_var_uniform: |
| 53 | var->shader_in = true; |
| 54 | var->read_only = true; |
| 55 | break; |
| 56 | default: |
| 57 | assert(0); |
| 58 | break; |
| 59 | } |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 60 | |
| 61 | /* Once the variable is created an initialized, add it to the symbol table |
| 62 | * and add the declaration to the IR stream. |
| 63 | */ |
| 64 | instructions->push_tail(var); |
| 65 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 66 | symtab->add_variable(var->name, var); |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 67 | return var; |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 70 | |
| 71 | static void |
| 72 | add_builtin_variable(const builtin_variable *proto, exec_list *instructions, |
| 73 | glsl_symbol_table *symtab) |
| 74 | { |
| 75 | /* Create a new variable declaration from the description supplied by |
| 76 | * the caller. |
| 77 | */ |
| 78 | const glsl_type *const type = symtab->get_type(proto->type); |
| 79 | |
| 80 | assert(type != NULL); |
| 81 | |
| 82 | add_variable(proto->name, proto->mode, type, instructions, symtab); |
| 83 | } |
| 84 | |
| 85 | |
Eric Anholt | 78fe3c9 | 2010-03-28 01:46:48 -0700 | [diff] [blame] | 86 | static void |
| 87 | generate_110_uniforms(exec_list *instructions, |
| 88 | glsl_symbol_table *symtab) |
| 89 | { |
| 90 | for (unsigned i = 0 |
| 91 | ; i < Elements(builtin_110_deprecated_uniforms) |
| 92 | ; i++) { |
| 93 | add_builtin_variable(& builtin_110_deprecated_uniforms[i], |
| 94 | instructions, symtab); |
| 95 | } |
| 96 | |
| 97 | /* FINISHME: Add support for gl_TextureMatrix[]. The size of this array is |
| 98 | * FINISHME: implementation dependent based on the value of |
| 99 | * FINISHME: GL_MAX_TEXTURE_COORDS. |
| 100 | */ |
| 101 | |
| 102 | /* FINISHME: Add support for gl_DepthRangeParameters */ |
| 103 | /* FINISHME: Add support for gl_ClipPlane[] */ |
| 104 | /* FINISHME: Add support for gl_PointParameters */ |
| 105 | |
| 106 | /* FINISHME: Add support for gl_MaterialParameters |
| 107 | * FINISHME: (glFrontMaterial, glBackMaterial) |
| 108 | */ |
| 109 | |
| 110 | /* FINISHME: Add support for gl_LightSource[] */ |
| 111 | /* FINISHME: Add support for gl_LightModel */ |
| 112 | /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */ |
| 113 | /* FINISHME: Add support for gl_TextureEnvColor[] */ |
| 114 | /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */ |
| 115 | /* FINISHME: Add support for gl_Fog */ |
| 116 | } |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 117 | |
| 118 | static void |
| 119 | generate_110_vs_variables(exec_list *instructions, |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 120 | glsl_symbol_table *symtab) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 121 | { |
| 122 | for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) { |
| 123 | add_builtin_variable(& builtin_core_vs_variables[i], |
| 124 | instructions, symtab); |
| 125 | } |
| 126 | |
| 127 | for (unsigned i = 0 |
| 128 | ; i < Elements(builtin_110_deprecated_vs_variables) |
| 129 | ; i++) { |
| 130 | add_builtin_variable(& builtin_110_deprecated_vs_variables[i], |
| 131 | instructions, symtab); |
| 132 | } |
Eric Anholt | 78fe3c9 | 2010-03-28 01:46:48 -0700 | [diff] [blame] | 133 | generate_110_uniforms(instructions, symtab); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 134 | |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 135 | /* FINISHME: The size of this array is implementation dependent based on the |
| 136 | * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be |
| 137 | * FINISHME: at least 2, so hard-code 2 for now. |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 138 | */ |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 139 | const glsl_type *const vec4_type = |
Ian Romanick | 1b3f47f | 2010-04-07 16:09:47 -0700 | [diff] [blame] | 140 | glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 141 | const glsl_type *const vec4_array_type = |
| 142 | glsl_type::get_array_instance(vec4_type, 2); |
| 143 | |
| 144 | add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions, |
| 145 | symtab); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | static void |
| 150 | generate_120_vs_variables(exec_list *instructions, |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 151 | glsl_symbol_table *symtab) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 152 | { |
| 153 | /* GLSL version 1.20 did not add any built-in variables in the vertex |
| 154 | * shader. |
| 155 | */ |
| 156 | generate_110_vs_variables(instructions, symtab); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | static void |
| 161 | generate_130_vs_variables(exec_list *instructions, |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 162 | glsl_symbol_table *symtab) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 163 | { |
| 164 | generate_120_vs_variables(instructions, symtab); |
| 165 | |
| 166 | for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) { |
| 167 | add_builtin_variable(& builtin_130_vs_variables[i], |
| 168 | instructions, symtab); |
| 169 | } |
| 170 | |
Eric Anholt | 271e199 | 2010-04-02 23:47:06 -0700 | [diff] [blame] | 171 | /* FINISHME: The size of this array is implementation dependent based on |
| 172 | * FINISHME: the value of GL_MAX_CLIP_DISTANCES. |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 173 | */ |
Eric Anholt | 271e199 | 2010-04-02 23:47:06 -0700 | [diff] [blame] | 174 | const glsl_type *const clip_distance_array_type = |
| 175 | glsl_type::get_array_instance(glsl_type::float_type, 8); |
| 176 | add_variable("gl_ClipDistance", ir_var_out, clip_distance_array_type, |
| 177 | instructions, symtab); |
| 178 | |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
| 182 | static void |
| 183 | initialize_vs_variables(exec_list *instructions, |
| 184 | struct _mesa_glsl_parse_state *state) |
| 185 | { |
| 186 | |
| 187 | switch (state->language_version) { |
| 188 | case 110: |
| 189 | generate_110_vs_variables(instructions, state->symbols); |
| 190 | break; |
| 191 | case 120: |
| 192 | generate_120_vs_variables(instructions, state->symbols); |
| 193 | break; |
| 194 | case 130: |
| 195 | generate_130_vs_variables(instructions, state->symbols); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 200 | static void |
| 201 | generate_110_fs_variables(exec_list *instructions, |
| 202 | glsl_symbol_table *symtab) |
| 203 | { |
| 204 | for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) { |
| 205 | add_builtin_variable(& builtin_core_fs_variables[i], |
| 206 | instructions, symtab); |
| 207 | } |
| 208 | |
Eric Anholt | 0f09aea | 2010-03-27 12:48:57 -0700 | [diff] [blame] | 209 | for (unsigned i = 0 |
| 210 | ; i < Elements(builtin_110_deprecated_fs_variables) |
| 211 | ; i++) { |
| 212 | add_builtin_variable(& builtin_110_deprecated_fs_variables[i], |
| 213 | instructions, symtab); |
| 214 | } |
Eric Anholt | 78fe3c9 | 2010-03-28 01:46:48 -0700 | [diff] [blame] | 215 | generate_110_uniforms(instructions, symtab); |
Eric Anholt | 0f09aea | 2010-03-27 12:48:57 -0700 | [diff] [blame] | 216 | |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 217 | /* FINISHME: The size of this array is implementation dependent based on the |
| 218 | * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be |
| 219 | * FINISHME: at least 2, so hard-code 2 for now. |
| 220 | */ |
| 221 | const glsl_type *const vec4_type = |
Ian Romanick | 1b3f47f | 2010-04-07 16:09:47 -0700 | [diff] [blame] | 222 | glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 223 | const glsl_type *const vec4_array_type = |
| 224 | glsl_type::get_array_instance(vec4_type, 2); |
| 225 | |
| 226 | add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions, |
| 227 | symtab); |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 230 | |
| 231 | static void |
| 232 | generate_ARB_draw_buffers_fs_variables(exec_list *instructions, |
| 233 | glsl_symbol_table *symtab, bool warn) |
| 234 | { |
| 235 | /* FINISHME: The size of this array is implementation dependent based on the |
| 236 | * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be |
| 237 | * FINISHME: at least 1, so hard-code 1 for now. |
| 238 | */ |
| 239 | const glsl_type *const vec4_type = |
| 240 | glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
| 241 | const glsl_type *const vec4_array_type = |
| 242 | glsl_type::get_array_instance(vec4_type, 1); |
| 243 | |
| 244 | ir_variable *const fd = |
| 245 | add_variable("gl_FragData", ir_var_out, vec4_array_type, instructions, |
| 246 | symtab); |
| 247 | |
| 248 | if (warn) |
| 249 | fd->warn_extension = "GL_ARB_draw_buffers"; |
| 250 | } |
| 251 | |
| 252 | |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 253 | static void |
| 254 | generate_120_fs_variables(exec_list *instructions, |
| 255 | glsl_symbol_table *symtab) |
| 256 | { |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 257 | generate_110_fs_variables(instructions, symtab); |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 258 | generate_ARB_draw_buffers_fs_variables(instructions, symtab, false); |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static void |
| 262 | generate_130_fs_variables(exec_list *instructions, |
| 263 | glsl_symbol_table *symtab) |
| 264 | { |
| 265 | generate_120_fs_variables(instructions, symtab); |
| 266 | |
Ian Romanick | 8645a95 | 2010-04-07 16:47:44 -0700 | [diff] [blame] | 267 | /* FINISHME: The size of this array is implementation dependent based on |
| 268 | * FINISHME: the value of GL_MAX_CLIP_DISTANCES. |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 269 | */ |
Ian Romanick | 8645a95 | 2010-04-07 16:47:44 -0700 | [diff] [blame] | 270 | const glsl_type *const clip_distance_array_type = |
| 271 | glsl_type::get_array_instance(glsl_type::float_type, 8); |
| 272 | add_variable("gl_ClipDistance", ir_var_in, clip_distance_array_type, |
| 273 | instructions, symtab); |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static void |
| 277 | initialize_fs_variables(exec_list *instructions, |
| 278 | struct _mesa_glsl_parse_state *state) |
| 279 | { |
| 280 | |
| 281 | switch (state->language_version) { |
| 282 | case 110: |
| 283 | generate_110_fs_variables(instructions, state->symbols); |
| 284 | break; |
| 285 | case 120: |
| 286 | generate_120_fs_variables(instructions, state->symbols); |
| 287 | break; |
| 288 | case 130: |
| 289 | generate_130_fs_variables(instructions, state->symbols); |
| 290 | break; |
| 291 | } |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 292 | |
| 293 | |
| 294 | /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we |
| 295 | * can basically ignore any extension settings for it. |
| 296 | */ |
| 297 | if (state->language_version < 120) { |
| 298 | if (state->ARB_draw_buffers_enable) { |
| 299 | generate_ARB_draw_buffers_fs_variables(instructions, state->symbols, |
| 300 | state->ARB_draw_buffers_warn); |
| 301 | } |
| 302 | } |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 303 | } |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 304 | |
| 305 | void |
| 306 | _mesa_glsl_initialize_variables(exec_list *instructions, |
| 307 | struct _mesa_glsl_parse_state *state) |
| 308 | { |
| 309 | switch (state->target) { |
| 310 | case vertex_shader: |
| 311 | initialize_vs_variables(instructions, state); |
| 312 | break; |
| 313 | case geometry_shader: |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 314 | break; |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 315 | case fragment_shader: |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 316 | initialize_fs_variables(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | } |