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