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