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