Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 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 | |
Paul Berry | d343e3d | 2013-12-17 09:46:08 -0800 | [diff] [blame] | 24 | #include "glsl_parser_extras.h" |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 25 | #include "ir.h" |
| 26 | #include "ir_uniform.h" |
| 27 | #include "linker.h" |
| 28 | #include "program/hash_table.h" |
| 29 | #include "main/macros.h" |
| 30 | |
| 31 | namespace { |
| 32 | /* |
| 33 | * Atomic counter as seen by the program. |
| 34 | */ |
| 35 | struct active_atomic_counter { |
Timothy Arceri | 2d7a98d | 2015-08-30 21:08:22 +1000 | [diff] [blame] | 36 | unsigned uniform_loc; |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 37 | ir_variable *var; |
| 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * Atomic counter buffer referenced by the program. There is a one |
| 42 | * to one correspondence between these and the objects that can be |
| 43 | * queried using glGetActiveAtomicCounterBufferiv(). |
| 44 | */ |
| 45 | struct active_atomic_buffer { |
| 46 | active_atomic_buffer() |
| 47 | : counters(0), num_counters(0), stage_references(), size(0) |
| 48 | {} |
| 49 | |
| 50 | ~active_atomic_buffer() |
| 51 | { |
| 52 | free(counters); |
| 53 | } |
| 54 | |
Timothy Arceri | 2d7a98d | 2015-08-30 21:08:22 +1000 | [diff] [blame] | 55 | void push_back(unsigned uniform_loc, ir_variable *var) |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 56 | { |
Juha-Pekka Heikkila | 568c545 | 2014-05-07 12:38:07 +0300 | [diff] [blame] | 57 | active_atomic_counter *new_counters; |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 58 | |
Juha-Pekka Heikkila | 568c545 | 2014-05-07 12:38:07 +0300 | [diff] [blame] | 59 | new_counters = (active_atomic_counter *) |
| 60 | realloc(counters, sizeof(active_atomic_counter) * |
| 61 | (num_counters + 1)); |
| 62 | |
| 63 | if (new_counters == NULL) { |
| 64 | _mesa_error_no_memory(__func__); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | counters = new_counters; |
Timothy Arceri | 2d7a98d | 2015-08-30 21:08:22 +1000 | [diff] [blame] | 69 | counters[num_counters].uniform_loc = uniform_loc; |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 70 | counters[num_counters].var = var; |
| 71 | num_counters++; |
| 72 | } |
| 73 | |
| 74 | active_atomic_counter *counters; |
| 75 | unsigned num_counters; |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 76 | unsigned stage_references[MESA_SHADER_STAGES]; |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 77 | unsigned size; |
| 78 | }; |
| 79 | |
| 80 | int |
| 81 | cmp_actives(const void *a, const void *b) |
| 82 | { |
| 83 | const active_atomic_counter *const first = (active_atomic_counter *) a; |
| 84 | const active_atomic_counter *const second = (active_atomic_counter *) b; |
| 85 | |
Timothy Arceri | 0d4cd04 | 2015-12-29 21:02:56 +1100 | [diff] [blame] | 86 | return int(first->var->data.offset) - int(second->var->data.offset); |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | bool |
| 90 | check_atomic_counters_overlap(const ir_variable *x, const ir_variable *y) |
| 91 | { |
Timothy Arceri | 0d4cd04 | 2015-12-29 21:02:56 +1100 | [diff] [blame] | 92 | return ((x->data.offset >= y->data.offset && |
| 93 | x->data.offset < y->data.offset + y->type->atomic_size()) || |
| 94 | (y->data.offset >= x->data.offset && |
| 95 | y->data.offset < x->data.offset + x->type->atomic_size())); |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Timothy Arceri | 2d7a98d | 2015-08-30 21:08:22 +1000 | [diff] [blame] | 98 | void |
| 99 | process_atomic_variable(const glsl_type *t, struct gl_shader_program *prog, |
| 100 | unsigned *uniform_loc, ir_variable *var, |
| 101 | active_atomic_buffer *const buffers, |
| 102 | unsigned *num_buffers, int *offset, |
| 103 | const unsigned shader_stage) |
| 104 | { |
| 105 | /* FIXME: Arrays of arrays get counted separately. For example: |
| 106 | * x1[3][3][2] = 9 counters |
| 107 | * x2[3][2] = 3 counters |
| 108 | * x3[2] = 1 counter |
| 109 | * |
| 110 | * However this code marks all the counters as active even when they |
| 111 | * might not be used. |
| 112 | */ |
| 113 | if (t->is_array() && t->fields.array->is_array()) { |
| 114 | for (unsigned i = 0; i < t->length; i++) { |
| 115 | process_atomic_variable(t->fields.array, prog, uniform_loc, |
| 116 | var, buffers, num_buffers, offset, |
| 117 | shader_stage); |
| 118 | } |
| 119 | } else { |
| 120 | active_atomic_buffer *buf = &buffers[var->data.binding]; |
| 121 | gl_uniform_storage *const storage = |
| 122 | &prog->UniformStorage[*uniform_loc]; |
| 123 | |
| 124 | /* If this is the first time the buffer is used, increment |
| 125 | * the counter of buffers used. |
| 126 | */ |
| 127 | if (buf->size == 0) |
| 128 | (*num_buffers)++; |
| 129 | |
| 130 | buf->push_back(*uniform_loc, var); |
| 131 | |
| 132 | buf->stage_references[shader_stage]++; |
| 133 | buf->size = MAX2(buf->size, *offset + t->atomic_size()); |
| 134 | |
| 135 | storage->offset = *offset; |
| 136 | *offset += t->atomic_size(); |
| 137 | |
| 138 | (*uniform_loc)++; |
| 139 | } |
| 140 | } |
| 141 | |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 142 | active_atomic_buffer * |
| 143 | find_active_atomic_counters(struct gl_context *ctx, |
| 144 | struct gl_shader_program *prog, |
| 145 | unsigned *num_buffers) |
| 146 | { |
| 147 | active_atomic_buffer *const buffers = |
| 148 | new active_atomic_buffer[ctx->Const.MaxAtomicBufferBindings]; |
| 149 | |
| 150 | *num_buffers = 0; |
| 151 | |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 152 | for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) { |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 153 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 154 | if (sh == NULL) |
| 155 | continue; |
| 156 | |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 157 | foreach_in_list(ir_instruction, node, sh->ir) { |
| 158 | ir_variable *var = node->as_variable(); |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 159 | |
| 160 | if (var && var->type->contains_atomic()) { |
Timothy Arceri | 0d4cd04 | 2015-12-29 21:02:56 +1100 | [diff] [blame] | 161 | int offset = var->data.offset; |
Timothy Arceri | 2d7a98d | 2015-08-30 21:08:22 +1000 | [diff] [blame] | 162 | unsigned uniform_loc = var->data.location; |
| 163 | process_atomic_variable(var->type, prog, &uniform_loc, |
| 164 | var, buffers, num_buffers, &offset, i); |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | for (unsigned i = 0; i < ctx->Const.MaxAtomicBufferBindings; i++) { |
| 170 | if (buffers[i].size == 0) |
| 171 | continue; |
| 172 | |
| 173 | qsort(buffers[i].counters, buffers[i].num_counters, |
| 174 | sizeof(active_atomic_counter), |
| 175 | cmp_actives); |
| 176 | |
| 177 | for (unsigned j = 1; j < buffers[i].num_counters; j++) { |
| 178 | /* If an overlapping counter found, it must be a reference to the |
| 179 | * same counter from a different shader stage. |
| 180 | */ |
| 181 | if (check_atomic_counters_overlap(buffers[i].counters[j-1].var, |
| 182 | buffers[i].counters[j].var) |
| 183 | && strcmp(buffers[i].counters[j-1].var->name, |
| 184 | buffers[i].counters[j].var->name) != 0) { |
| 185 | linker_error(prog, "Atomic counter %s declared at offset %d " |
| 186 | "which is already in use.", |
| 187 | buffers[i].counters[j].var->name, |
Timothy Arceri | 0d4cd04 | 2015-12-29 21:02:56 +1100 | [diff] [blame] | 188 | buffers[i].counters[j].var->data.offset); |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | return buffers; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | link_assign_atomic_counter_resources(struct gl_context *ctx, |
| 198 | struct gl_shader_program *prog) |
| 199 | { |
| 200 | unsigned num_buffers; |
Timothy Arceri | a3d0359 | 2015-10-27 06:58:15 +1100 | [diff] [blame] | 201 | unsigned num_atomic_buffers[MESA_SHADER_STAGES] = {}; |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 202 | active_atomic_buffer *abs = |
| 203 | find_active_atomic_counters(ctx, prog, &num_buffers); |
| 204 | |
| 205 | prog->AtomicBuffers = rzalloc_array(prog, gl_active_atomic_buffer, |
| 206 | num_buffers); |
| 207 | prog->NumAtomicBuffers = num_buffers; |
| 208 | |
| 209 | unsigned i = 0; |
| 210 | for (unsigned binding = 0; |
| 211 | binding < ctx->Const.MaxAtomicBufferBindings; |
| 212 | binding++) { |
| 213 | |
| 214 | /* If the binding was not used, skip. |
| 215 | */ |
| 216 | if (abs[binding].size == 0) |
| 217 | continue; |
| 218 | |
| 219 | active_atomic_buffer &ab = abs[binding]; |
| 220 | gl_active_atomic_buffer &mab = prog->AtomicBuffers[i]; |
| 221 | |
| 222 | /* Assign buffer-specific fields. */ |
| 223 | mab.Binding = binding; |
| 224 | mab.MinimumSize = ab.size; |
| 225 | mab.Uniforms = rzalloc_array(prog->AtomicBuffers, GLuint, |
| 226 | ab.num_counters); |
| 227 | mab.NumUniforms = ab.num_counters; |
| 228 | |
| 229 | /* Assign counter-specific fields. */ |
| 230 | for (unsigned j = 0; j < ab.num_counters; j++) { |
| 231 | ir_variable *const var = ab.counters[j].var; |
Timothy Arceri | 2d7a98d | 2015-08-30 21:08:22 +1000 | [diff] [blame] | 232 | gl_uniform_storage *const storage = |
| 233 | &prog->UniformStorage[ab.counters[j].uniform_loc]; |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 234 | |
Timothy Arceri | 2d7a98d | 2015-08-30 21:08:22 +1000 | [diff] [blame] | 235 | mab.Uniforms[j] = ab.counters[j].uniform_loc; |
Ian Romanick | c0cd5be | 2014-07-14 15:48:36 -0700 | [diff] [blame] | 236 | if (!var->data.explicit_binding) |
| 237 | var->data.binding = i; |
| 238 | |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 239 | storage->atomic_buffer_index = i; |
Timothy Arceri | 0d4cd04 | 2015-12-29 21:02:56 +1100 | [diff] [blame] | 240 | storage->offset = var->data.offset; |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 241 | storage->array_stride = (var->type->is_array() ? |
Timothy Arceri | d67515b | 2015-04-30 20:45:54 +1000 | [diff] [blame] | 242 | var->type->without_array()->atomic_size() : 0); |
Tapani Pälli | f2fe607 | 2015-11-02 13:36:19 +0200 | [diff] [blame] | 243 | if (!var->type->is_matrix()) |
| 244 | storage->matrix_stride = 0; |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* Assign stage-specific fields. */ |
Timothy Arceri | a3d0359 | 2015-10-27 06:58:15 +1100 | [diff] [blame] | 248 | for (unsigned j = 0; j < MESA_SHADER_STAGES; ++j) { |
| 249 | if (ab.stage_references[j]) { |
| 250 | mab.StageReferences[j] = GL_TRUE; |
| 251 | num_atomic_buffers[j]++; |
| 252 | } else { |
| 253 | mab.StageReferences[j] = GL_FALSE; |
| 254 | } |
| 255 | } |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 256 | |
| 257 | i++; |
| 258 | } |
| 259 | |
Timothy Arceri | a3d0359 | 2015-10-27 06:58:15 +1100 | [diff] [blame] | 260 | /* Store a list pointers to atomic buffers per stage and store the index |
| 261 | * to the intra-stage buffer list in uniform storage. |
| 262 | */ |
| 263 | for (unsigned j = 0; j < MESA_SHADER_STAGES; ++j) { |
| 264 | if (prog->_LinkedShaders[j] && num_atomic_buffers[j] > 0) { |
| 265 | prog->_LinkedShaders[j]->NumAtomicBuffers = num_atomic_buffers[j]; |
| 266 | prog->_LinkedShaders[j]->AtomicBuffers = |
| 267 | rzalloc_array(prog, gl_active_atomic_buffer *, |
| 268 | num_atomic_buffers[j]); |
| 269 | |
| 270 | unsigned intra_stage_idx = 0; |
| 271 | for (unsigned i = 0; i < num_buffers; i++) { |
| 272 | struct gl_active_atomic_buffer *atomic_buffer = |
| 273 | &prog->AtomicBuffers[i]; |
| 274 | if (atomic_buffer->StageReferences[j]) { |
| 275 | prog->_LinkedShaders[j]->AtomicBuffers[intra_stage_idx] = |
| 276 | atomic_buffer; |
| 277 | |
| 278 | for (unsigned u = 0; u < atomic_buffer->NumUniforms; u++) { |
| 279 | prog->UniformStorage[atomic_buffer->Uniforms[u]].opaque[j].index = |
| 280 | intra_stage_idx; |
| 281 | prog->UniformStorage[atomic_buffer->Uniforms[u]].opaque[j].active = |
| 282 | true; |
| 283 | } |
| 284 | |
| 285 | intra_stage_idx++; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 291 | delete [] abs; |
| 292 | assert(i == num_buffers); |
| 293 | } |
| 294 | |
| 295 | void |
| 296 | link_check_atomic_counter_resources(struct gl_context *ctx, |
| 297 | struct gl_shader_program *prog) |
| 298 | { |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 299 | unsigned num_buffers; |
| 300 | active_atomic_buffer *const abs = |
| 301 | find_active_atomic_counters(ctx, prog, &num_buffers); |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 302 | unsigned atomic_counters[MESA_SHADER_STAGES] = {}; |
| 303 | unsigned atomic_buffers[MESA_SHADER_STAGES] = {}; |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 304 | unsigned total_atomic_counters = 0; |
| 305 | unsigned total_atomic_buffers = 0; |
| 306 | |
| 307 | /* Sum the required resources. Note that this counts buffers and |
| 308 | * counters referenced by several shader stages multiple times |
| 309 | * against the combined limit -- That's the behavior the spec |
| 310 | * requires. |
| 311 | */ |
| 312 | for (unsigned i = 0; i < ctx->Const.MaxAtomicBufferBindings; i++) { |
| 313 | if (abs[i].size == 0) |
| 314 | continue; |
| 315 | |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 316 | for (unsigned j = 0; j < MESA_SHADER_STAGES; ++j) { |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 317 | const unsigned n = abs[i].stage_references[j]; |
| 318 | |
| 319 | if (n) { |
| 320 | atomic_counters[j] += n; |
| 321 | total_atomic_counters += n; |
| 322 | atomic_buffers[j]++; |
| 323 | total_atomic_buffers++; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /* Check that they are within the supported limits. */ |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 329 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
Paul Berry | bce8bc0 | 2014-01-08 10:17:01 -0800 | [diff] [blame] | 330 | if (atomic_counters[i] > ctx->Const.Program[i].MaxAtomicCounters) |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 331 | linker_error(prog, "Too many %s shader atomic counters", |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 332 | _mesa_shader_stage_to_string(i)); |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 333 | |
Paul Berry | bce8bc0 | 2014-01-08 10:17:01 -0800 | [diff] [blame] | 334 | if (atomic_buffers[i] > ctx->Const.Program[i].MaxAtomicBuffers) |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 335 | linker_error(prog, "Too many %s shader atomic counter buffers", |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 336 | _mesa_shader_stage_to_string(i)); |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | if (total_atomic_counters > ctx->Const.MaxCombinedAtomicCounters) |
| 340 | linker_error(prog, "Too many combined atomic counters"); |
| 341 | |
| 342 | if (total_atomic_buffers > ctx->Const.MaxCombinedAtomicBuffers) |
| 343 | linker_error(prog, "Too many combined atomic buffers"); |
| 344 | |
| 345 | delete [] abs; |
| 346 | } |