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