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 "main/macros.h"
32 * Atomic counter uniform as seen by the program.
34 struct active_atomic_counter_uniform
{
40 * Atomic counter buffer referenced by the program. There is a one
41 * to one correspondence between these and the objects that can be
42 * queried using glGetActiveAtomicCounterBufferiv().
44 struct active_atomic_buffer
{
45 active_atomic_buffer()
46 : uniforms(0), num_uniforms(0), stage_counter_references(), size(0)
49 ~active_atomic_buffer()
54 void push_back(unsigned uniform_loc
, ir_variable
*var
)
56 active_atomic_counter_uniform
*new_uniforms
;
58 new_uniforms
= (active_atomic_counter_uniform
*)
59 realloc(uniforms
, sizeof(active_atomic_counter_uniform
) *
62 if (new_uniforms
== NULL
) {
63 _mesa_error_no_memory(__func__
);
67 uniforms
= new_uniforms
;
68 uniforms
[num_uniforms
].uniform_loc
= uniform_loc
;
69 uniforms
[num_uniforms
].var
= var
;
73 active_atomic_counter_uniform
*uniforms
;
74 unsigned num_uniforms
;
75 unsigned stage_counter_references
[MESA_SHADER_STAGES
];
80 cmp_actives(const void *a
, const void *b
)
82 const active_atomic_counter_uniform
*const first
= (active_atomic_counter_uniform
*) a
;
83 const active_atomic_counter_uniform
*const second
= (active_atomic_counter_uniform
*) b
;
85 return int(first
->var
->data
.offset
) - int(second
->var
->data
.offset
);
89 check_atomic_counters_overlap(const ir_variable
*x
, const ir_variable
*y
)
91 return ((x
->data
.offset
>= y
->data
.offset
&&
92 x
->data
.offset
< y
->data
.offset
+ y
->type
->atomic_size()) ||
93 (y
->data
.offset
>= x
->data
.offset
&&
94 y
->data
.offset
< x
->data
.offset
+ x
->type
->atomic_size()));
98 process_atomic_variable(const glsl_type
*t
, struct gl_shader_program
*prog
,
99 unsigned *uniform_loc
, ir_variable
*var
,
100 active_atomic_buffer
*const buffers
,
101 unsigned *num_buffers
, int *offset
,
102 const unsigned shader_stage
)
104 /* FIXME: Arrays of arrays get counted separately. For example:
105 * x1[3][3][2] = 9 uniforms, 18 atomic counters
106 * x2[3][2] = 3 uniforms, 6 atomic counters
107 * x3[2] = 1 uniform, 2 atomic counters
109 * However this code marks all the counters as active even when they
112 if (t
->is_array() && t
->fields
.array
->is_array()) {
113 for (unsigned i
= 0; i
< t
->length
; i
++) {
114 process_atomic_variable(t
->fields
.array
, prog
, uniform_loc
,
115 var
, buffers
, num_buffers
, offset
,
119 active_atomic_buffer
*buf
= &buffers
[var
->data
.binding
];
120 gl_uniform_storage
*const storage
=
121 &prog
->data
->UniformStorage
[*uniform_loc
];
123 /* If this is the first time the buffer is used, increment
124 * the counter of buffers used.
129 buf
->push_back(*uniform_loc
, var
);
131 /* When checking for atomic counters we should count every member in
132 * an array as an atomic counter reference.
135 buf
->stage_counter_references
[shader_stage
] += t
->length
;
137 buf
->stage_counter_references
[shader_stage
]++;
138 buf
->size
= MAX2(buf
->size
, *offset
+ t
->atomic_size());
140 storage
->offset
= *offset
;
141 *offset
+= t
->atomic_size();
147 active_atomic_buffer
*
148 find_active_atomic_counters(struct gl_context
*ctx
,
149 struct gl_shader_program
*prog
,
150 unsigned *num_buffers
)
152 active_atomic_buffer
*const buffers
=
153 new active_atomic_buffer
[ctx
->Const
.MaxAtomicBufferBindings
];
157 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
158 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
162 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
163 ir_variable
*var
= node
->as_variable();
165 if (var
&& var
->type
->contains_atomic()) {
166 int offset
= var
->data
.offset
;
167 unsigned uniform_loc
= var
->data
.location
;
168 process_atomic_variable(var
->type
, prog
, &uniform_loc
,
169 var
, buffers
, num_buffers
, &offset
, i
);
174 for (unsigned i
= 0; i
< ctx
->Const
.MaxAtomicBufferBindings
; i
++) {
175 if (buffers
[i
].size
== 0)
178 qsort(buffers
[i
].uniforms
, buffers
[i
].num_uniforms
,
179 sizeof(active_atomic_counter_uniform
),
182 for (unsigned j
= 1; j
< buffers
[i
].num_uniforms
; j
++) {
183 /* If an overlapping counter found, it must be a reference to the
184 * same counter from a different shader stage.
186 if (check_atomic_counters_overlap(buffers
[i
].uniforms
[j
-1].var
,
187 buffers
[i
].uniforms
[j
].var
)
188 && strcmp(buffers
[i
].uniforms
[j
-1].var
->name
,
189 buffers
[i
].uniforms
[j
].var
->name
) != 0) {
190 linker_error(prog
, "Atomic counter %s declared at offset %d "
191 "which is already in use.",
192 buffers
[i
].uniforms
[j
].var
->name
,
193 buffers
[i
].uniforms
[j
].var
->data
.offset
);
202 link_assign_atomic_counter_resources(struct gl_context
*ctx
,
203 struct gl_shader_program
*prog
)
205 unsigned num_buffers
;
206 unsigned num_atomic_buffers
[MESA_SHADER_STAGES
] = {};
207 active_atomic_buffer
*abs
=
208 find_active_atomic_counters(ctx
, prog
, &num_buffers
);
210 prog
->data
->AtomicBuffers
= rzalloc_array(prog
, gl_active_atomic_buffer
,
212 prog
->data
->NumAtomicBuffers
= num_buffers
;
215 for (unsigned binding
= 0;
216 binding
< ctx
->Const
.MaxAtomicBufferBindings
;
219 /* If the binding was not used, skip.
221 if (abs
[binding
].size
== 0)
224 active_atomic_buffer
&ab
= abs
[binding
];
225 gl_active_atomic_buffer
&mab
= prog
->data
->AtomicBuffers
[i
];
227 /* Assign buffer-specific fields. */
228 mab
.Binding
= binding
;
229 mab
.MinimumSize
= ab
.size
;
230 mab
.Uniforms
= rzalloc_array(prog
->data
->AtomicBuffers
, GLuint
,
232 mab
.NumUniforms
= ab
.num_uniforms
;
234 /* Assign counter-specific fields. */
235 for (unsigned j
= 0; j
< ab
.num_uniforms
; j
++) {
236 ir_variable
*const var
= ab
.uniforms
[j
].var
;
237 gl_uniform_storage
*const storage
=
238 &prog
->data
->UniformStorage
[ab
.uniforms
[j
].uniform_loc
];
240 mab
.Uniforms
[j
] = ab
.uniforms
[j
].uniform_loc
;
241 if (!var
->data
.explicit_binding
)
242 var
->data
.binding
= i
;
244 storage
->atomic_buffer_index
= i
;
245 storage
->offset
= var
->data
.offset
;
246 storage
->array_stride
= (var
->type
->is_array() ?
247 var
->type
->without_array()->atomic_size() : 0);
248 if (!var
->type
->is_matrix())
249 storage
->matrix_stride
= 0;
252 /* Assign stage-specific fields. */
253 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; ++j
) {
254 if (ab
.stage_counter_references
[j
]) {
255 mab
.StageReferences
[j
] = GL_TRUE
;
256 num_atomic_buffers
[j
]++;
258 mab
.StageReferences
[j
] = GL_FALSE
;
265 /* Store a list pointers to atomic buffers per stage and store the index
266 * to the intra-stage buffer list in uniform storage.
268 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; ++j
) {
269 if (prog
->_LinkedShaders
[j
] && num_atomic_buffers
[j
] > 0) {
270 struct gl_program
*gl_prog
= prog
->_LinkedShaders
[j
]->Program
;
271 gl_prog
->info
.num_abos
= num_atomic_buffers
[j
];
272 gl_prog
->sh
.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
->data
->AtomicBuffers
[i
];
280 if (atomic_buffer
->StageReferences
[j
]) {
281 gl_prog
->sh
.AtomicBuffers
[intra_stage_idx
] = atomic_buffer
;
283 for (unsigned u
= 0; u
< atomic_buffer
->NumUniforms
; u
++) {
284 prog
->data
->UniformStorage
[atomic_buffer
->Uniforms
[u
]].opaque
[j
].index
=
286 prog
->data
->UniformStorage
[atomic_buffer
->Uniforms
[u
]].opaque
[j
].active
=
297 assert(i
== num_buffers
);
301 link_check_atomic_counter_resources(struct gl_context
*ctx
,
302 struct gl_shader_program
*prog
)
304 unsigned num_buffers
;
305 active_atomic_buffer
*const abs
=
306 find_active_atomic_counters(ctx
, prog
, &num_buffers
);
307 unsigned atomic_counters
[MESA_SHADER_STAGES
] = {};
308 unsigned atomic_buffers
[MESA_SHADER_STAGES
] = {};
309 unsigned total_atomic_counters
= 0;
310 unsigned total_atomic_buffers
= 0;
312 /* Sum the required resources. Note that this counts buffers and
313 * counters referenced by several shader stages multiple times
314 * against the combined limit -- That's the behavior the spec
317 for (unsigned i
= 0; i
< ctx
->Const
.MaxAtomicBufferBindings
; i
++) {
318 if (abs
[i
].size
== 0)
321 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; ++j
) {
322 const unsigned n
= abs
[i
].stage_counter_references
[j
];
325 atomic_counters
[j
] += n
;
326 total_atomic_counters
+= n
;
328 total_atomic_buffers
++;
333 /* Check that they are within the supported limits. */
334 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
335 if (atomic_counters
[i
] > ctx
->Const
.Program
[i
].MaxAtomicCounters
)
336 linker_error(prog
, "Too many %s shader atomic counters",
337 _mesa_shader_stage_to_string(i
));
339 if (atomic_buffers
[i
] > ctx
->Const
.Program
[i
].MaxAtomicBuffers
)
340 linker_error(prog
, "Too many %s shader atomic counter buffers",
341 _mesa_shader_stage_to_string(i
));
344 if (total_atomic_counters
> ctx
->Const
.MaxCombinedAtomicCounters
)
345 linker_error(prog
, "Too many combined atomic counters");
347 if (total_atomic_buffers
> ctx
->Const
.MaxCombinedAtomicBuffers
)
348 linker_error(prog
, "Too many combined atomic buffers");