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