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