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