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 | |
| 24 | #include "glsl_parser_extras.h" |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 25 | #include "glsl_symbol_table.h" |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 26 | #include "ir.h" |
| 27 | #include "builtin_variables.h" |
| 28 | |
| 29 | #ifndef Elements |
| 30 | #define Elements(x) (sizeof(x)/sizeof(*(x))) |
| 31 | #endif |
| 32 | |
| 33 | static void |
| 34 | add_builtin_variable(const builtin_variable *proto, exec_list *instructions, |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 35 | glsl_symbol_table *symtab) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 36 | { |
| 37 | /* Create a new variable declaration from the description supplied by |
| 38 | * the caller. |
| 39 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 40 | const glsl_type *const type = symtab->get_type(proto->type); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 41 | |
| 42 | assert(type != NULL); |
| 43 | |
| 44 | ir_variable *var = new ir_variable(type, proto->name); |
| 45 | |
| 46 | var->mode = proto->mode; |
| 47 | if (var->mode != ir_var_out) |
| 48 | var->read_only = true; |
| 49 | |
| 50 | |
| 51 | /* Once the variable is created an initialized, add it to the symbol table |
| 52 | * and add the declaration to the IR stream. |
| 53 | */ |
| 54 | instructions->push_tail(var); |
| 55 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 56 | symtab->add_variable(var->name, var); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
| 60 | static void |
| 61 | generate_110_vs_variables(exec_list *instructions, |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 62 | glsl_symbol_table *symtab) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 63 | { |
| 64 | for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) { |
| 65 | add_builtin_variable(& builtin_core_vs_variables[i], |
| 66 | instructions, symtab); |
| 67 | } |
| 68 | |
| 69 | for (unsigned i = 0 |
| 70 | ; i < Elements(builtin_110_deprecated_vs_variables) |
| 71 | ; i++) { |
| 72 | add_builtin_variable(& builtin_110_deprecated_vs_variables[i], |
| 73 | instructions, symtab); |
| 74 | } |
| 75 | |
| 76 | /* FINISHME: Add support fo gl_TexCoord. The size of this array is |
| 77 | * FINISHME: implementation dependent based on the value of |
| 78 | * FINISHME: GL_MAX_TEXTURE_COORDS. |
| 79 | */ |
| 80 | } |
| 81 | |
| 82 | |
| 83 | static void |
| 84 | generate_120_vs_variables(exec_list *instructions, |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 85 | glsl_symbol_table *symtab) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 86 | { |
| 87 | /* GLSL version 1.20 did not add any built-in variables in the vertex |
| 88 | * shader. |
| 89 | */ |
| 90 | generate_110_vs_variables(instructions, symtab); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | static void |
| 95 | generate_130_vs_variables(exec_list *instructions, |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 96 | glsl_symbol_table *symtab) |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 97 | { |
| 98 | generate_120_vs_variables(instructions, symtab); |
| 99 | |
| 100 | for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) { |
| 101 | add_builtin_variable(& builtin_130_vs_variables[i], |
| 102 | instructions, symtab); |
| 103 | } |
| 104 | |
| 105 | /* FINISHME: Add support fo gl_ClipDistance. The size of this array is |
| 106 | * FINISHME: implementation dependent based on the value of |
| 107 | * FINISHME: GL_MAX_CLIP_DISTANCES. |
| 108 | */ |
| 109 | } |
| 110 | |
| 111 | |
| 112 | static void |
| 113 | initialize_vs_variables(exec_list *instructions, |
| 114 | struct _mesa_glsl_parse_state *state) |
| 115 | { |
| 116 | |
| 117 | switch (state->language_version) { |
| 118 | case 110: |
| 119 | generate_110_vs_variables(instructions, state->symbols); |
| 120 | break; |
| 121 | case 120: |
| 122 | generate_120_vs_variables(instructions, state->symbols); |
| 123 | break; |
| 124 | case 130: |
| 125 | generate_130_vs_variables(instructions, state->symbols); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 130 | static void |
| 131 | generate_110_fs_variables(exec_list *instructions, |
| 132 | glsl_symbol_table *symtab) |
| 133 | { |
| 134 | for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) { |
| 135 | add_builtin_variable(& builtin_core_fs_variables[i], |
| 136 | instructions, symtab); |
| 137 | } |
| 138 | |
Eric Anholt | 0f09aea | 2010-03-27 12:48:57 -0700 | [diff] [blame^] | 139 | /* FINISHME: Add support for gl_TexCoord[] */ |
| 140 | for (unsigned i = 0 |
| 141 | ; i < Elements(builtin_110_deprecated_fs_variables) |
| 142 | ; i++) { |
| 143 | add_builtin_variable(& builtin_110_deprecated_fs_variables[i], |
| 144 | instructions, symtab); |
| 145 | } |
| 146 | |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 147 | /* FINISHME: Add support for gl_FragData[GL_MAX_DRAW_BUFFERS]. */ |
| 148 | } |
| 149 | |
| 150 | static void |
| 151 | generate_120_fs_variables(exec_list *instructions, |
| 152 | glsl_symbol_table *symtab) |
| 153 | { |
| 154 | /* GLSL version 1.20 did not add any built-in variables in the fragment |
| 155 | * shader. |
| 156 | */ |
| 157 | generate_110_fs_variables(instructions, symtab); |
| 158 | } |
| 159 | |
| 160 | static void |
| 161 | generate_130_fs_variables(exec_list *instructions, |
| 162 | glsl_symbol_table *symtab) |
| 163 | { |
| 164 | generate_120_fs_variables(instructions, symtab); |
| 165 | |
| 166 | /* FINISHME: Add support fo gl_ClipDistance. The size of this array is |
| 167 | * FINISHME: implementation dependent based on the value of |
| 168 | * FINISHME: GL_MAX_CLIP_DISTANCES. |
| 169 | */ |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | initialize_fs_variables(exec_list *instructions, |
| 174 | struct _mesa_glsl_parse_state *state) |
| 175 | { |
| 176 | |
| 177 | switch (state->language_version) { |
| 178 | case 110: |
| 179 | generate_110_fs_variables(instructions, state->symbols); |
| 180 | break; |
| 181 | case 120: |
| 182 | generate_120_fs_variables(instructions, state->symbols); |
| 183 | break; |
| 184 | case 130: |
| 185 | generate_130_fs_variables(instructions, state->symbols); |
| 186 | break; |
| 187 | } |
| 188 | } |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 189 | |
| 190 | void |
| 191 | _mesa_glsl_initialize_variables(exec_list *instructions, |
| 192 | struct _mesa_glsl_parse_state *state) |
| 193 | { |
| 194 | switch (state->target) { |
| 195 | case vertex_shader: |
| 196 | initialize_vs_variables(instructions, state); |
| 197 | break; |
| 198 | case geometry_shader: |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 199 | break; |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 200 | case fragment_shader: |
Eric Anholt | b3f743a | 2010-03-25 14:48:25 -0700 | [diff] [blame] | 201 | initialize_fs_variables(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | } |