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