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