glsl: Get rid of hardcoded arrays of shader target names.
[mesa.git] / src / glsl / link_atomics.cpp
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
24 #include "glsl_parser_extras.h"
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 {
36 unsigned id;
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
55 void push_back(unsigned id, ir_variable *var)
56 {
57 counters = (active_atomic_counter *)
58 realloc(counters, sizeof(active_atomic_counter) * (num_counters + 1));
59
60 counters[num_counters].id = id;
61 counters[num_counters].var = var;
62 num_counters++;
63 }
64
65 active_atomic_counter *counters;
66 unsigned num_counters;
67 unsigned stage_references[MESA_SHADER_TYPES];
68 unsigned size;
69 };
70
71 int
72 cmp_actives(const void *a, const void *b)
73 {
74 const active_atomic_counter *const first = (active_atomic_counter *) a;
75 const active_atomic_counter *const second = (active_atomic_counter *) b;
76
77 return int(first->var->data.atomic.offset) - int(second->var->data.atomic.offset);
78 }
79
80 bool
81 check_atomic_counters_overlap(const ir_variable *x, const ir_variable *y)
82 {
83 return ((x->data.atomic.offset >= y->data.atomic.offset &&
84 x->data.atomic.offset < y->data.atomic.offset + y->type->atomic_size()) ||
85 (y->data.atomic.offset >= x->data.atomic.offset &&
86 y->data.atomic.offset < x->data.atomic.offset + x->type->atomic_size()));
87 }
88
89 active_atomic_buffer *
90 find_active_atomic_counters(struct gl_context *ctx,
91 struct gl_shader_program *prog,
92 unsigned *num_buffers)
93 {
94 active_atomic_buffer *const buffers =
95 new active_atomic_buffer[ctx->Const.MaxAtomicBufferBindings];
96
97 *num_buffers = 0;
98
99 for (unsigned i = 0; i < MESA_SHADER_TYPES; ++i) {
100 struct gl_shader *sh = prog->_LinkedShaders[i];
101 if (sh == NULL)
102 continue;
103
104 foreach_list(node, sh->ir) {
105 ir_variable *var = ((ir_instruction *)node)->as_variable();
106
107 if (var && var->type->contains_atomic()) {
108 unsigned id;
109 bool found = prog->UniformHash->get(id, var->name);
110 assert(found);
111 active_atomic_buffer *buf = &buffers[var->data.binding];
112
113 /* If this is the first time the buffer is used, increment
114 * the counter of buffers used.
115 */
116 if (buf->size == 0)
117 (*num_buffers)++;
118
119 buf->push_back(id, var);
120
121 buf->stage_references[i]++;
122 buf->size = MAX2(buf->size, var->data.atomic.offset +
123 var->type->atomic_size());
124 }
125 }
126 }
127
128 for (unsigned i = 0; i < ctx->Const.MaxAtomicBufferBindings; i++) {
129 if (buffers[i].size == 0)
130 continue;
131
132 qsort(buffers[i].counters, buffers[i].num_counters,
133 sizeof(active_atomic_counter),
134 cmp_actives);
135
136 for (unsigned j = 1; j < buffers[i].num_counters; j++) {
137 /* If an overlapping counter found, it must be a reference to the
138 * same counter from a different shader stage.
139 */
140 if (check_atomic_counters_overlap(buffers[i].counters[j-1].var,
141 buffers[i].counters[j].var)
142 && strcmp(buffers[i].counters[j-1].var->name,
143 buffers[i].counters[j].var->name) != 0) {
144 linker_error(prog, "Atomic counter %s declared at offset %d "
145 "which is already in use.",
146 buffers[i].counters[j].var->name,
147 buffers[i].counters[j].var->data.atomic.offset);
148 }
149 }
150 }
151 return buffers;
152 }
153 }
154
155 void
156 link_assign_atomic_counter_resources(struct gl_context *ctx,
157 struct gl_shader_program *prog)
158 {
159 unsigned num_buffers;
160 active_atomic_buffer *abs =
161 find_active_atomic_counters(ctx, prog, &num_buffers);
162
163 prog->AtomicBuffers = rzalloc_array(prog, gl_active_atomic_buffer,
164 num_buffers);
165 prog->NumAtomicBuffers = num_buffers;
166
167 unsigned i = 0;
168 for (unsigned binding = 0;
169 binding < ctx->Const.MaxAtomicBufferBindings;
170 binding++) {
171
172 /* If the binding was not used, skip.
173 */
174 if (abs[binding].size == 0)
175 continue;
176
177 active_atomic_buffer &ab = abs[binding];
178 gl_active_atomic_buffer &mab = prog->AtomicBuffers[i];
179
180 /* Assign buffer-specific fields. */
181 mab.Binding = binding;
182 mab.MinimumSize = ab.size;
183 mab.Uniforms = rzalloc_array(prog->AtomicBuffers, GLuint,
184 ab.num_counters);
185 mab.NumUniforms = ab.num_counters;
186
187 /* Assign counter-specific fields. */
188 for (unsigned j = 0; j < ab.num_counters; j++) {
189 ir_variable *const var = ab.counters[j].var;
190 const unsigned id = ab.counters[j].id;
191 gl_uniform_storage *const storage = &prog->UniformStorage[id];
192
193 mab.Uniforms[j] = id;
194 var->data.atomic.buffer_index = i;
195 storage->atomic_buffer_index = i;
196 storage->offset = var->data.atomic.offset;
197 storage->array_stride = (var->type->is_array() ?
198 var->type->element_type()->atomic_size() : 0);
199 }
200
201 /* Assign stage-specific fields. */
202 for (unsigned j = 0; j < MESA_SHADER_TYPES; ++j)
203 mab.StageReferences[j] =
204 (ab.stage_references[j] ? GL_TRUE : GL_FALSE);
205
206 i++;
207 }
208
209 delete [] abs;
210 assert(i == num_buffers);
211 }
212
213 void
214 link_check_atomic_counter_resources(struct gl_context *ctx,
215 struct gl_shader_program *prog)
216 {
217 STATIC_ASSERT(MESA_SHADER_TYPES == 3);
218 const unsigned max_atomic_counters[MESA_SHADER_TYPES] = {
219 ctx->Const.VertexProgram.MaxAtomicCounters,
220 ctx->Const.GeometryProgram.MaxAtomicCounters,
221 ctx->Const.FragmentProgram.MaxAtomicCounters
222 };
223 const unsigned max_atomic_buffers[MESA_SHADER_TYPES] = {
224 ctx->Const.VertexProgram.MaxAtomicBuffers,
225 ctx->Const.GeometryProgram.MaxAtomicBuffers,
226 ctx->Const.FragmentProgram.MaxAtomicBuffers
227 };
228 unsigned num_buffers;
229 active_atomic_buffer *const abs =
230 find_active_atomic_counters(ctx, prog, &num_buffers);
231 unsigned atomic_counters[MESA_SHADER_TYPES] = {};
232 unsigned atomic_buffers[MESA_SHADER_TYPES] = {};
233 unsigned total_atomic_counters = 0;
234 unsigned total_atomic_buffers = 0;
235
236 /* Sum the required resources. Note that this counts buffers and
237 * counters referenced by several shader stages multiple times
238 * against the combined limit -- That's the behavior the spec
239 * requires.
240 */
241 for (unsigned i = 0; i < ctx->Const.MaxAtomicBufferBindings; i++) {
242 if (abs[i].size == 0)
243 continue;
244
245 for (unsigned j = 0; j < MESA_SHADER_TYPES; ++j) {
246 const unsigned n = abs[i].stage_references[j];
247
248 if (n) {
249 atomic_counters[j] += n;
250 total_atomic_counters += n;
251 atomic_buffers[j]++;
252 total_atomic_buffers++;
253 }
254 }
255 }
256
257 /* Check that they are within the supported limits. */
258 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
259 if (atomic_counters[i] > max_atomic_counters[i])
260 linker_error(prog, "Too many %s shader atomic counters",
261 _mesa_shader_type_to_string(i));
262
263 if (atomic_buffers[i] > max_atomic_buffers[i])
264 linker_error(prog, "Too many %s shader atomic counter buffers",
265 _mesa_shader_type_to_string(i));
266 }
267
268 if (total_atomic_counters > ctx->Const.MaxCombinedAtomicCounters)
269 linker_error(prog, "Too many combined atomic counters");
270
271 if (total_atomic_buffers > ctx->Const.MaxCombinedAtomicBuffers)
272 linker_error(prog, "Too many combined atomic buffers");
273
274 delete [] abs;
275 }