2 * Copyright © 2013 Intel Corporation
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:
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
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.
24 #include "glsl_parser_extras.h"
26 #include "ir_uniform.h"
28 #include "program/hash_table.h"
29 #include "main/macros.h"
33 * Atomic counter as seen by the program.
35 struct active_atomic_counter
{
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().
45 struct active_atomic_buffer
{
46 active_atomic_buffer()
47 : counters(0), num_counters(0), stage_references(), size(0)
50 ~active_atomic_buffer()
55 void push_back(unsigned uniform_loc
, ir_variable
*var
)
57 active_atomic_counter
*new_counters
;
59 new_counters
= (active_atomic_counter
*)
60 realloc(counters
, sizeof(active_atomic_counter
) *
63 if (new_counters
== NULL
) {
64 _mesa_error_no_memory(__func__
);
68 counters
= new_counters
;
69 counters
[num_counters
].uniform_loc
= uniform_loc
;
70 counters
[num_counters
].var
= var
;
74 active_atomic_counter
*counters
;
75 unsigned num_counters
;
76 unsigned stage_references
[MESA_SHADER_STAGES
];
81 cmp_actives(const void *a
, const void *b
)
83 const active_atomic_counter
*const first
= (active_atomic_counter
*) a
;
84 const active_atomic_counter
*const second
= (active_atomic_counter
*) b
;
86 return int(first
->var
->data
.offset
) - int(second
->var
->data
.offset
);
90 check_atomic_counters_overlap(const ir_variable
*x
, const ir_variable
*y
)
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()));
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
)
105 /* FIXME: Arrays of arrays get counted separately. For example:
106 * x1[3][3][2] = 9 uniforms, 18 atomic counters
107 * x2[3][2] = 3 uniforms, 6 atomic counters
108 * x3[2] = 1 uniform, 2 atomic counters
110 * However this code marks all the counters as active even when they
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
,
120 active_atomic_buffer
*buf
= &buffers
[var
->data
.binding
];
121 gl_uniform_storage
*const storage
=
122 &prog
->UniformStorage
[*uniform_loc
];
124 /* If this is the first time the buffer is used, increment
125 * the counter of buffers used.
130 buf
->push_back(*uniform_loc
, var
);
132 /* When checking for atomic counters we should count every member in
133 * an array as an atomic counter reference.
136 buf
->stage_references
[shader_stage
] += t
->length
;
138 buf
->stage_references
[shader_stage
]++;
139 buf
->size
= MAX2(buf
->size
, *offset
+ t
->atomic_size());
141 storage
->offset
= *offset
;
142 *offset
+= t
->atomic_size();
148 active_atomic_buffer
*
149 find_active_atomic_counters(struct gl_context
*ctx
,
150 struct gl_shader_program
*prog
,
151 unsigned *num_buffers
)
153 active_atomic_buffer
*const buffers
=
154 new active_atomic_buffer
[ctx
->Const
.MaxAtomicBufferBindings
];
158 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
159 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
163 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
164 ir_variable
*var
= node
->as_variable();
166 if (var
&& var
->type
->contains_atomic()) {
167 int offset
= var
->data
.offset
;
168 unsigned uniform_loc
= var
->data
.location
;
169 process_atomic_variable(var
->type
, prog
, &uniform_loc
,
170 var
, buffers
, num_buffers
, &offset
, i
);
175 for (unsigned i
= 0; i
< ctx
->Const
.MaxAtomicBufferBindings
; i
++) {
176 if (buffers
[i
].size
== 0)
179 qsort(buffers
[i
].counters
, buffers
[i
].num_counters
,
180 sizeof(active_atomic_counter
),
183 for (unsigned j
= 1; j
< buffers
[i
].num_counters
; j
++) {
184 /* If an overlapping counter found, it must be a reference to the
185 * same counter from a different shader stage.
187 if (check_atomic_counters_overlap(buffers
[i
].counters
[j
-1].var
,
188 buffers
[i
].counters
[j
].var
)
189 && strcmp(buffers
[i
].counters
[j
-1].var
->name
,
190 buffers
[i
].counters
[j
].var
->name
) != 0) {
191 linker_error(prog
, "Atomic counter %s declared at offset %d "
192 "which is already in use.",
193 buffers
[i
].counters
[j
].var
->name
,
194 buffers
[i
].counters
[j
].var
->data
.offset
);
203 link_assign_atomic_counter_resources(struct gl_context
*ctx
,
204 struct gl_shader_program
*prog
)
206 unsigned num_buffers
;
207 unsigned num_atomic_buffers
[MESA_SHADER_STAGES
] = {};
208 active_atomic_buffer
*abs
=
209 find_active_atomic_counters(ctx
, prog
, &num_buffers
);
211 prog
->AtomicBuffers
= rzalloc_array(prog
, gl_active_atomic_buffer
,
213 prog
->NumAtomicBuffers
= num_buffers
;
216 for (unsigned binding
= 0;
217 binding
< ctx
->Const
.MaxAtomicBufferBindings
;
220 /* If the binding was not used, skip.
222 if (abs
[binding
].size
== 0)
225 active_atomic_buffer
&ab
= abs
[binding
];
226 gl_active_atomic_buffer
&mab
= prog
->AtomicBuffers
[i
];
228 /* Assign buffer-specific fields. */
229 mab
.Binding
= binding
;
230 mab
.MinimumSize
= ab
.size
;
231 mab
.Uniforms
= rzalloc_array(prog
->AtomicBuffers
, GLuint
,
233 mab
.NumUniforms
= ab
.num_counters
;
235 /* Assign counter-specific fields. */
236 for (unsigned j
= 0; j
< ab
.num_counters
; j
++) {
237 ir_variable
*const var
= ab
.counters
[j
].var
;
238 gl_uniform_storage
*const storage
=
239 &prog
->UniformStorage
[ab
.counters
[j
].uniform_loc
];
241 mab
.Uniforms
[j
] = ab
.counters
[j
].uniform_loc
;
242 if (!var
->data
.explicit_binding
)
243 var
->data
.binding
= i
;
245 storage
->atomic_buffer_index
= i
;
246 storage
->offset
= var
->data
.offset
;
247 storage
->array_stride
= (var
->type
->is_array() ?
248 var
->type
->without_array()->atomic_size() : 0);
249 if (!var
->type
->is_matrix())
250 storage
->matrix_stride
= 0;
253 /* Assign stage-specific fields. */
254 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; ++j
) {
255 if (ab
.stage_references
[j
]) {
256 mab
.StageReferences
[j
] = GL_TRUE
;
257 num_atomic_buffers
[j
]++;
259 mab
.StageReferences
[j
] = GL_FALSE
;
266 /* Store a list pointers to atomic buffers per stage and store the index
267 * to the intra-stage buffer list in uniform storage.
269 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; ++j
) {
270 if (prog
->_LinkedShaders
[j
] && num_atomic_buffers
[j
] > 0) {
271 prog
->_LinkedShaders
[j
]->NumAtomicBuffers
= num_atomic_buffers
[j
];
272 prog
->_LinkedShaders
[j
]->AtomicBuffers
=
273 rzalloc_array(prog
, gl_active_atomic_buffer
*,
274 num_atomic_buffers
[j
]);
276 unsigned intra_stage_idx
= 0;
277 for (unsigned i
= 0; i
< num_buffers
; i
++) {
278 struct gl_active_atomic_buffer
*atomic_buffer
=
279 &prog
->AtomicBuffers
[i
];
280 if (atomic_buffer
->StageReferences
[j
]) {
281 prog
->_LinkedShaders
[j
]->AtomicBuffers
[intra_stage_idx
] =
284 for (unsigned u
= 0; u
< atomic_buffer
->NumUniforms
; u
++) {
285 prog
->UniformStorage
[atomic_buffer
->Uniforms
[u
]].opaque
[j
].index
=
287 prog
->UniformStorage
[atomic_buffer
->Uniforms
[u
]].opaque
[j
].active
=
298 assert(i
== num_buffers
);
302 link_check_atomic_counter_resources(struct gl_context
*ctx
,
303 struct gl_shader_program
*prog
)
305 unsigned num_buffers
;
306 active_atomic_buffer
*const abs
=
307 find_active_atomic_counters(ctx
, prog
, &num_buffers
);
308 unsigned atomic_counters
[MESA_SHADER_STAGES
] = {};
309 unsigned atomic_buffers
[MESA_SHADER_STAGES
] = {};
310 unsigned total_atomic_counters
= 0;
311 unsigned total_atomic_buffers
= 0;
313 /* Sum the required resources. Note that this counts buffers and
314 * counters referenced by several shader stages multiple times
315 * against the combined limit -- That's the behavior the spec
318 for (unsigned i
= 0; i
< ctx
->Const
.MaxAtomicBufferBindings
; i
++) {
319 if (abs
[i
].size
== 0)
322 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; ++j
) {
323 const unsigned n
= abs
[i
].stage_references
[j
];
326 atomic_counters
[j
] += n
;
327 total_atomic_counters
+= n
;
329 total_atomic_buffers
++;
334 /* Check that they are within the supported limits. */
335 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
336 if (atomic_counters
[i
] > ctx
->Const
.Program
[i
].MaxAtomicCounters
)
337 linker_error(prog
, "Too many %s shader atomic counters",
338 _mesa_shader_stage_to_string(i
));
340 if (atomic_buffers
[i
] > ctx
->Const
.Program
[i
].MaxAtomicBuffers
)
341 linker_error(prog
, "Too many %s shader atomic counter buffers",
342 _mesa_shader_stage_to_string(i
));
345 if (total_atomic_counters
> ctx
->Const
.MaxCombinedAtomicCounters
)
346 linker_error(prog
, "Too many combined atomic counters");
348 if (total_atomic_buffers
> ctx
->Const
.MaxCombinedAtomicBuffers
)
349 linker_error(prog
, "Too many combined atomic buffers");