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 | ac95f2f | 2010-06-22 10:38:52 -0700 | [diff] [blame] | 24 | #include "ir.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 "builtin_variables.h" |
| 28 | |
| 29 | #ifndef Elements |
| 30 | #define Elements(x) (sizeof(x)/sizeof(*(x))) |
| 31 | #endif |
| 32 | |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 33 | static void generate_ARB_draw_buffers_variables(exec_list *, |
| 34 | struct _mesa_glsl_parse_state *, |
| 35 | bool, _mesa_glsl_parser_targets); |
Ian Romanick | 9c4b1f2 | 2010-06-29 15:10:09 -0700 | [diff] [blame] | 36 | |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 37 | static ir_variable * |
Ian Romanick | ed0626e | 2010-06-21 11:42:57 -0700 | [diff] [blame] | 38 | add_variable(const char *name, enum ir_variable_mode mode, int slot, |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 39 | const glsl_type *type, exec_list *instructions, |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 40 | glsl_symbol_table *symtab) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 41 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 42 | ir_variable *var = new(symtab) ir_variable(type, name, mode); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 43 | |
Eric Anholt | 71df19f | 2010-04-19 11:10:37 -0700 | [diff] [blame] | 44 | switch (var->mode) { |
Ian Romanick | e2f84f0 | 2010-06-29 15:19:11 -0700 | [diff] [blame] | 45 | case ir_var_auto: |
| 46 | var->read_only = true; |
| 47 | break; |
Eric Anholt | 71df19f | 2010-04-19 11:10:37 -0700 | [diff] [blame] | 48 | case ir_var_in: |
| 49 | var->shader_in = true; |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 50 | var->read_only = true; |
Eric Anholt | 71df19f | 2010-04-19 11:10:37 -0700 | [diff] [blame] | 51 | break; |
| 52 | case ir_var_inout: |
| 53 | var->shader_in = true; |
| 54 | var->shader_out = true; |
Ian Romanick | ff236fa | 2010-04-21 15:08:08 -0700 | [diff] [blame] | 55 | break; |
Eric Anholt | 71df19f | 2010-04-19 11:10:37 -0700 | [diff] [blame] | 56 | case ir_var_out: |
| 57 | var->shader_out = true; |
| 58 | break; |
| 59 | case ir_var_uniform: |
| 60 | var->shader_in = true; |
| 61 | var->read_only = true; |
| 62 | break; |
| 63 | default: |
| 64 | assert(0); |
| 65 | break; |
| 66 | } |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 67 | |
Ian Romanick | ed0626e | 2010-06-21 11:42:57 -0700 | [diff] [blame] | 68 | var->location = slot; |
| 69 | |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 70 | /* Once the variable is created an initialized, add it to the symbol table |
| 71 | * and add the declaration to the IR stream. |
| 72 | */ |
| 73 | instructions->push_tail(var); |
| 74 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 75 | symtab->add_variable(var->name, var); |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 76 | return var; |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Eric Anholt | 85b5dba | 2010-07-28 12:23:51 -0700 | [diff] [blame] | 79 | static ir_variable * |
| 80 | add_uniform(exec_list *instructions, |
| 81 | struct _mesa_glsl_parse_state *state, |
| 82 | const char *name, const glsl_type *type) |
| 83 | { |
| 84 | return add_variable(name, ir_var_uniform, -1, type, instructions, |
| 85 | state->symbols); |
| 86 | } |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 87 | |
| 88 | static void |
| 89 | add_builtin_variable(const builtin_variable *proto, exec_list *instructions, |
| 90 | glsl_symbol_table *symtab) |
| 91 | { |
| 92 | /* Create a new variable declaration from the description supplied by |
| 93 | * the caller. |
| 94 | */ |
| 95 | const glsl_type *const type = symtab->get_type(proto->type); |
| 96 | |
| 97 | assert(type != NULL); |
| 98 | |
Ian Romanick | ed0626e | 2010-06-21 11:42:57 -0700 | [diff] [blame] | 99 | add_variable(proto->name, proto->mode, proto->slot, type, instructions, |
| 100 | symtab); |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Eric Anholt | f894669 | 2010-07-20 14:03:35 -0700 | [diff] [blame] | 103 | static void |
| 104 | add_builtin_constant(exec_list *instructions, |
| 105 | struct _mesa_glsl_parse_state *state, |
| 106 | const char *name, int value) |
| 107 | { |
| 108 | ir_variable *const var = add_variable(name, ir_var_auto, |
| 109 | -1, glsl_type::int_type, |
| 110 | instructions, state->symbols); |
| 111 | var->constant_value = new(var) ir_constant(value); |
| 112 | } |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 113 | |
Eric Anholt | 78fe3c9 | 2010-03-28 01:46:48 -0700 | [diff] [blame] | 114 | static void |
| 115 | generate_110_uniforms(exec_list *instructions, |
Ian Romanick | 127308b | 2010-07-01 13:30:50 -0700 | [diff] [blame] | 116 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | 78fe3c9 | 2010-03-28 01:46:48 -0700 | [diff] [blame] | 117 | { |
| 118 | for (unsigned i = 0 |
| 119 | ; i < Elements(builtin_110_deprecated_uniforms) |
| 120 | ; i++) { |
| 121 | add_builtin_variable(& builtin_110_deprecated_uniforms[i], |
Ian Romanick | 127308b | 2010-07-01 13:30:50 -0700 | [diff] [blame] | 122 | instructions, state->symbols); |
Eric Anholt | 78fe3c9 | 2010-03-28 01:46:48 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Eric Anholt | f894669 | 2010-07-20 14:03:35 -0700 | [diff] [blame] | 125 | add_builtin_constant(instructions, state, "gl_MaxLights", |
| 126 | state->Const.MaxLights); |
| 127 | add_builtin_constant(instructions, state, "gl_MaxClipPlanes", |
| 128 | state->Const.MaxClipPlanes); |
| 129 | add_builtin_constant(instructions, state, "gl_MaxTextureUnits", |
| 130 | state->Const.MaxTextureUnits); |
| 131 | add_builtin_constant(instructions, state, "gl_MaxTextureCoords", |
| 132 | state->Const.MaxTextureCoords); |
| 133 | add_builtin_constant(instructions, state, "gl_MaxVertexAttribs", |
| 134 | state->Const.MaxVertexAttribs); |
| 135 | add_builtin_constant(instructions, state, "gl_MaxVertexUniformComponents", |
| 136 | state->Const.MaxVertexUniformComponents); |
| 137 | add_builtin_constant(instructions, state, "gl_MaxVaryingFloats", |
| 138 | state->Const.MaxVaryingFloats); |
| 139 | add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits", |
| 140 | state->Const.MaxVertexTextureImageUnits); |
| 141 | add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits", |
| 142 | state->Const.MaxCombinedTextureImageUnits); |
| 143 | add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits", |
| 144 | state->Const.MaxTextureImageUnits); |
| 145 | add_builtin_constant(instructions, state, "gl_MaxFragmentUniformComponents", |
| 146 | state->Const.MaxFragmentUniformComponents); |
Ian Romanick | 127308b | 2010-07-01 13:30:50 -0700 | [diff] [blame] | 147 | |
Ian Romanick | 3eba593 | 2010-04-26 14:59:32 -0700 | [diff] [blame] | 148 | const glsl_type *const mat4_array_type = |
Ian Romanick | f38d15b | 2010-07-20 15:33:40 -0700 | [diff] [blame] | 149 | glsl_type::get_array_instance(glsl_type::mat4_type, |
Ian Romanick | 127308b | 2010-07-01 13:30:50 -0700 | [diff] [blame] | 150 | state->Const.MaxTextureCoords); |
Ian Romanick | 3eba593 | 2010-04-26 14:59:32 -0700 | [diff] [blame] | 151 | |
Eric Anholt | 85b5dba | 2010-07-28 12:23:51 -0700 | [diff] [blame] | 152 | add_uniform(instructions, state, "gl_TextureMatrix", mat4_array_type); |
Eric Anholt | 78fe3c9 | 2010-03-28 01:46:48 -0700 | [diff] [blame] | 153 | |
Eric Anholt | 85b5dba | 2010-07-28 12:23:51 -0700 | [diff] [blame] | 154 | add_uniform(instructions, state, "gl_DepthRangeParameters", |
| 155 | state->symbols->get_type("gl_DepthRangeParameters")); |
Eric Anholt | 78fe3c9 | 2010-03-28 01:46:48 -0700 | [diff] [blame] | 156 | |
Eric Anholt | 85b5dba | 2010-07-28 12:23:51 -0700 | [diff] [blame] | 157 | add_uniform(instructions, state, "gl_ClipPlane", |
| 158 | glsl_type::get_array_instance(glsl_type::vec4_type, |
| 159 | state->Const.MaxClipPlanes)); |
| 160 | add_uniform(instructions, state, "gl_Point", |
| 161 | state->symbols->get_type("gl_PointParameters")); |
| 162 | |
| 163 | const glsl_type *const material_parameters_type = |
| 164 | state->symbols->get_type("gl_MaterialParameters"); |
| 165 | add_uniform(instructions, state, "gl_FrontMaterial", material_parameters_type); |
| 166 | add_uniform(instructions, state, "gl_BackMaterial", material_parameters_type); |
Eric Anholt | 78fe3c9 | 2010-03-28 01:46:48 -0700 | [diff] [blame] | 167 | |
Eric Anholt | aa57943 | 2010-05-19 14:09:04 -0700 | [diff] [blame] | 168 | const glsl_type *const light_source_array_type = |
Eric Anholt | 73df636 | 2010-07-28 08:18:59 -0700 | [diff] [blame] | 169 | glsl_type::get_array_instance(state->symbols->get_type("gl_LightSourceParameters"), state->Const.MaxLights); |
Eric Anholt | aa57943 | 2010-05-19 14:09:04 -0700 | [diff] [blame] | 170 | |
Eric Anholt | 85b5dba | 2010-07-28 12:23:51 -0700 | [diff] [blame] | 171 | add_uniform(instructions, state, "gl_LightSource", light_source_array_type); |
Eric Anholt | aa57943 | 2010-05-19 14:09:04 -0700 | [diff] [blame] | 172 | |
Eric Anholt | 85b5dba | 2010-07-28 12:23:51 -0700 | [diff] [blame] | 173 | const glsl_type *const light_model_products_type = |
| 174 | state->symbols->get_type("gl_LightModelProducts"); |
| 175 | add_uniform(instructions, state, "gl_FrontLightModelProduct", |
| 176 | light_model_products_type); |
| 177 | add_uniform(instructions, state, "gl_BackLightModelProduct", |
| 178 | light_model_products_type); |
| 179 | |
| 180 | const glsl_type *const light_products_type = |
| 181 | glsl_type::get_array_instance(state->symbols->get_type("gl_LightProducts"), |
| 182 | state->Const.MaxLights); |
| 183 | add_uniform(instructions, state, "gl_FrontLightProduct", light_products_type); |
| 184 | add_uniform(instructions, state, "gl_BackLightProduct", light_products_type); |
| 185 | |
| 186 | add_uniform(instructions, state, "gl_TextureEnvColor", |
| 187 | glsl_type::get_array_instance(glsl_type::vec4_type, |
| 188 | state->Const.MaxTextureUnits)); |
| 189 | |
| 190 | const glsl_type *const texcoords_vec4 = |
| 191 | glsl_type::get_array_instance(glsl_type::vec4_type, |
| 192 | state->Const.MaxTextureCoords); |
| 193 | add_uniform(instructions, state, "gl_EyePlaneS", texcoords_vec4); |
| 194 | add_uniform(instructions, state, "gl_EyePlaneT", texcoords_vec4); |
| 195 | add_uniform(instructions, state, "gl_EyePlaneR", texcoords_vec4); |
| 196 | add_uniform(instructions, state, "gl_EyePlaneQ", texcoords_vec4); |
| 197 | add_uniform(instructions, state, "gl_ObjectPlaneS", texcoords_vec4); |
| 198 | add_uniform(instructions, state, "gl_ObjectPlaneT", texcoords_vec4); |
| 199 | add_uniform(instructions, state, "gl_ObjectPlaneR", texcoords_vec4); |
| 200 | add_uniform(instructions, state, "gl_ObjectPlaneQ", texcoords_vec4); |
| 201 | |
| 202 | add_uniform(instructions, state, "gl_Fog", |
| 203 | state->symbols->get_type("gl_FogParameters")); |
Eric Anholt | 78fe3c9 | 2010-03-28 01:46:48 -0700 | [diff] [blame] | 204 | } |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 205 | |
| 206 | static void |
| 207 | generate_110_vs_variables(exec_list *instructions, |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 208 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 209 | { |
| 210 | for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) { |
| 211 | add_builtin_variable(& builtin_core_vs_variables[i], |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 212 | instructions, state->symbols); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | for (unsigned i = 0 |
| 216 | ; i < Elements(builtin_110_deprecated_vs_variables) |
| 217 | ; i++) { |
| 218 | add_builtin_variable(& builtin_110_deprecated_vs_variables[i], |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 219 | instructions, state->symbols); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 220 | } |
Ian Romanick | 127308b | 2010-07-01 13:30:50 -0700 | [diff] [blame] | 221 | generate_110_uniforms(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 222 | |
Ian Romanick | cd00d5b | 2010-07-01 13:17:54 -0700 | [diff] [blame] | 223 | /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec: |
| 224 | * |
| 225 | * "As with all arrays, indices used to subscript gl_TexCoord must |
| 226 | * either be an integral constant expressions, or this array must be |
| 227 | * re-declared by the shader with a size. The size can be at most |
| 228 | * gl_MaxTextureCoords. Using indexes close to 0 may aid the |
| 229 | * implementation in preserving varying resources." |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 230 | */ |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 231 | const glsl_type *const vec4_array_type = |
Ian Romanick | f38d15b | 2010-07-20 15:33:40 -0700 | [diff] [blame] | 232 | glsl_type::get_array_instance(glsl_type::vec4_type, 0); |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 233 | |
Ian Romanick | ed0626e | 2010-06-21 11:42:57 -0700 | [diff] [blame] | 234 | add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type, |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 235 | instructions, state->symbols); |
| 236 | |
| 237 | generate_ARB_draw_buffers_variables(instructions, state, false, |
| 238 | vertex_shader); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | |
| 242 | static void |
| 243 | generate_120_vs_variables(exec_list *instructions, |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 244 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 245 | { |
| 246 | /* GLSL version 1.20 did not add any built-in variables in the vertex |
| 247 | * shader. |
| 248 | */ |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 249 | generate_110_vs_variables(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
| 253 | static void |
| 254 | generate_130_vs_variables(exec_list *instructions, |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 255 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 256 | { |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 257 | generate_120_vs_variables(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 258 | |
| 259 | for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) { |
| 260 | add_builtin_variable(& builtin_130_vs_variables[i], |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 261 | instructions, state->symbols); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Eric Anholt | 271e199 | 2010-04-02 23:47:06 -0700 | [diff] [blame] | 264 | const glsl_type *const clip_distance_array_type = |
Eric Anholt | 73df636 | 2010-07-28 08:18:59 -0700 | [diff] [blame] | 265 | glsl_type::get_array_instance(glsl_type::float_type, |
| 266 | state->Const.MaxClipPlanes); |
Ian Romanick | ed0626e | 2010-06-21 11:42:57 -0700 | [diff] [blame] | 267 | |
| 268 | /* FINISHME: gl_ClipDistance needs a real location assigned. */ |
| 269 | add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type, |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 270 | instructions, state->symbols); |
Eric Anholt | 271e199 | 2010-04-02 23:47:06 -0700 | [diff] [blame] | 271 | |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | static void |
| 276 | initialize_vs_variables(exec_list *instructions, |
| 277 | struct _mesa_glsl_parse_state *state) |
| 278 | { |
| 279 | |
| 280 | switch (state->language_version) { |
| 281 | case 110: |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 282 | generate_110_vs_variables(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 283 | break; |
| 284 | case 120: |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 285 | generate_120_vs_variables(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 286 | break; |
| 287 | case 130: |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 288 | generate_130_vs_variables(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 293 | static void |
| 294 | generate_110_fs_variables(exec_list *instructions, |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 295 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 296 | { |
| 297 | for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) { |
| 298 | add_builtin_variable(& builtin_core_fs_variables[i], |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 299 | instructions, state->symbols); |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Eric Anholt | 0f09aea | 2010-03-27 12:48:57 -0700 | [diff] [blame] | 302 | for (unsigned i = 0 |
| 303 | ; i < Elements(builtin_110_deprecated_fs_variables) |
| 304 | ; i++) { |
| 305 | add_builtin_variable(& builtin_110_deprecated_fs_variables[i], |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 306 | instructions, state->symbols); |
Eric Anholt | 0f09aea | 2010-03-27 12:48:57 -0700 | [diff] [blame] | 307 | } |
Ian Romanick | 127308b | 2010-07-01 13:30:50 -0700 | [diff] [blame] | 308 | generate_110_uniforms(instructions, state); |
Eric Anholt | 0f09aea | 2010-03-27 12:48:57 -0700 | [diff] [blame] | 309 | |
Ian Romanick | cd00d5b | 2010-07-01 13:17:54 -0700 | [diff] [blame] | 310 | /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec: |
| 311 | * |
| 312 | * "As with all arrays, indices used to subscript gl_TexCoord must |
| 313 | * either be an integral constant expressions, or this array must be |
| 314 | * re-declared by the shader with a size. The size can be at most |
| 315 | * gl_MaxTextureCoords. Using indexes close to 0 may aid the |
| 316 | * implementation in preserving varying resources." |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 317 | */ |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 318 | const glsl_type *const vec4_array_type = |
Ian Romanick | f38d15b | 2010-07-20 15:33:40 -0700 | [diff] [blame] | 319 | glsl_type::get_array_instance(glsl_type::vec4_type, 0); |
Ian Romanick | 3f9a73d | 2010-04-02 11:59:57 -0700 | [diff] [blame] | 320 | |
Ian Romanick | ed0626e | 2010-06-21 11:42:57 -0700 | [diff] [blame] | 321 | add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type, |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 322 | instructions, state->symbols); |
Ian Romanick | 9c4b1f2 | 2010-06-29 15:10:09 -0700 | [diff] [blame] | 323 | |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 324 | generate_ARB_draw_buffers_variables(instructions, state, false, |
| 325 | fragment_shader); |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 328 | |
| 329 | static void |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 330 | generate_ARB_draw_buffers_variables(exec_list *instructions, |
| 331 | struct _mesa_glsl_parse_state *state, |
| 332 | bool warn, _mesa_glsl_parser_targets target) |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 333 | { |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 334 | /* gl_MaxDrawBuffers is available in all shader stages. |
| 335 | */ |
Ian Romanick | e2f84f0 | 2010-06-29 15:19:11 -0700 | [diff] [blame] | 336 | ir_variable *const mdb = |
| 337 | add_variable("gl_MaxDrawBuffers", ir_var_auto, -1, |
| 338 | glsl_type::int_type, instructions, state->symbols); |
| 339 | |
| 340 | if (warn) |
| 341 | mdb->warn_extension = "GL_ARB_draw_buffers"; |
| 342 | |
| 343 | mdb->constant_value = new(mdb) |
| 344 | ir_constant(int(state->Const.MaxDrawBuffers)); |
| 345 | |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 346 | |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 347 | /* gl_FragData is only available in the fragment shader. |
| 348 | */ |
| 349 | if (target == fragment_shader) { |
| 350 | const glsl_type *const vec4_array_type = |
Ian Romanick | f38d15b | 2010-07-20 15:33:40 -0700 | [diff] [blame] | 351 | glsl_type::get_array_instance(glsl_type::vec4_type, |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 352 | state->Const.MaxDrawBuffers); |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 353 | |
Ian Romanick | 22971e9 | 2010-06-29 15:29:56 -0700 | [diff] [blame] | 354 | ir_variable *const fd = |
| 355 | add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0, |
| 356 | vec4_array_type, instructions, state->symbols); |
| 357 | |
| 358 | if (warn) |
| 359 | fd->warn_extension = "GL_ARB_draw_buffers"; |
| 360 | } |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 364 | static void |
| 365 | generate_120_fs_variables(exec_list *instructions, |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 366 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 367 | { |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 368 | generate_110_fs_variables(instructions, state); |
Eric Anholt | 152b55e | 2010-07-07 19:45:22 -0700 | [diff] [blame] | 369 | |
| 370 | for (unsigned i = 0 |
| 371 | ; i < Elements(builtin_120_fs_variables) |
| 372 | ; i++) { |
| 373 | add_builtin_variable(& builtin_120_fs_variables[i], |
| 374 | instructions, state->symbols); |
| 375 | } |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static void |
| 379 | generate_130_fs_variables(exec_list *instructions, |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 380 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 381 | { |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 382 | generate_120_fs_variables(instructions, state); |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 383 | |
Ian Romanick | 8645a95 | 2010-04-07 16:47:44 -0700 | [diff] [blame] | 384 | const glsl_type *const clip_distance_array_type = |
Eric Anholt | 73df636 | 2010-07-28 08:18:59 -0700 | [diff] [blame] | 385 | glsl_type::get_array_instance(glsl_type::float_type, |
| 386 | state->Const.MaxClipPlanes); |
Ian Romanick | ed0626e | 2010-06-21 11:42:57 -0700 | [diff] [blame] | 387 | |
| 388 | /* FINISHME: gl_ClipDistance needs a real location assigned. */ |
| 389 | add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type, |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 390 | instructions, state->symbols); |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static void |
| 394 | initialize_fs_variables(exec_list *instructions, |
| 395 | struct _mesa_glsl_parse_state *state) |
| 396 | { |
| 397 | |
| 398 | switch (state->language_version) { |
| 399 | case 110: |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 400 | generate_110_fs_variables(instructions, state); |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 401 | break; |
| 402 | case 120: |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 403 | generate_120_fs_variables(instructions, state); |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 404 | break; |
| 405 | case 130: |
Ian Romanick | 5e18b05 | 2010-06-29 14:21:05 -0700 | [diff] [blame] | 406 | generate_130_fs_variables(instructions, state); |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 407 | break; |
| 408 | } |
| 409 | } |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 410 | |
| 411 | void |
| 412 | _mesa_glsl_initialize_variables(exec_list *instructions, |
| 413 | struct _mesa_glsl_parse_state *state) |
| 414 | { |
| 415 | switch (state->target) { |
| 416 | case vertex_shader: |
| 417 | initialize_vs_variables(instructions, state); |
| 418 | break; |
| 419 | case geometry_shader: |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 420 | break; |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 421 | case fragment_shader: |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 422 | initialize_fs_variables(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 423 | break; |
Eric Anholt | 81f49a7 | 2010-04-29 17:57:28 -0700 | [diff] [blame] | 424 | case ir_shader: |
| 425 | fprintf(stderr, "ir reader has no builtin variables"); |
| 426 | exit(1); |
| 427 | break; |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 428 | } |
| 429 | } |