2 * Copyright © 2018 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 DEALINGS
25 #include "linker_util.h"
26 #include "gl_nir_linker.h"
27 #include "compiler/glsl/ir_uniform.h" /* for gl_uniform_storage */
28 #include "main/context.h"
31 * This file do the common link for GLSL atomic counter uniforms, using NIR,
32 * instead of IR as the counter-part glsl/link_uniforms.cpp
35 struct active_atomic_counter_uniform
{
40 struct active_atomic_buffer
{
41 struct active_atomic_counter_uniform
*uniforms
;
42 unsigned num_uniforms
;
43 unsigned uniform_buffer_size
;
44 unsigned stage_counter_references
[MESA_SHADER_STAGES
];
49 add_atomic_counter(const void *ctx
,
50 struct active_atomic_buffer
*buffer
,
54 if (buffer
->num_uniforms
>= buffer
->uniform_buffer_size
) {
55 if (buffer
->uniform_buffer_size
== 0)
56 buffer
->uniform_buffer_size
= 1;
58 buffer
->uniform_buffer_size
*= 2;
59 buffer
->uniforms
= reralloc(ctx
,
61 struct active_atomic_counter_uniform
,
62 buffer
->uniform_buffer_size
);
65 struct active_atomic_counter_uniform
*uniform
=
66 buffer
->uniforms
+ buffer
->num_uniforms
;
67 uniform
->loc
= uniform_loc
;
69 buffer
->num_uniforms
++;
73 process_atomic_variable(const struct glsl_type
*t
,
74 struct gl_shader_program
*prog
,
75 unsigned *uniform_loc
,
77 struct active_atomic_buffer
*buffers
,
78 unsigned *num_buffers
,
80 unsigned shader_stage
)
82 /* FIXME: Arrays of arrays get counted separately. For example:
83 * x1[3][3][2] = 9 uniforms, 18 atomic counters
84 * x2[3][2] = 3 uniforms, 6 atomic counters
85 * x3[2] = 1 uniform, 2 atomic counters
87 * However this code marks all the counters as active even when they
90 if (glsl_type_is_array(t
) &&
91 glsl_type_is_array(glsl_get_array_element(t
))) {
92 for (unsigned i
= 0; i
< glsl_get_length(t
); i
++) {
93 process_atomic_variable(glsl_get_array_element(t
),
102 struct active_atomic_buffer
*buf
= buffers
+ var
->data
.binding
;
103 struct gl_uniform_storage
*const storage
=
104 &prog
->data
->UniformStorage
[*uniform_loc
];
106 /* If this is the first time the buffer is used, increment
107 * the counter of buffers used.
112 add_atomic_counter(buffers
, /* ctx */
117 /* When checking for atomic counters we should count every member in
118 * an array as an atomic counter reference.
120 if (glsl_type_is_array(t
))
121 buf
->stage_counter_references
[shader_stage
] += glsl_get_length(t
);
123 buf
->stage_counter_references
[shader_stage
]++;
124 buf
->size
= MAX2(buf
->size
, *offset
+ glsl_atomic_size(t
));
126 storage
->offset
= *offset
;
127 *offset
+= glsl_atomic_size(t
);
133 static struct active_atomic_buffer
*
134 find_active_atomic_counters(struct gl_context
*ctx
,
135 struct gl_shader_program
*prog
,
136 unsigned *num_buffers
)
138 struct active_atomic_buffer
*buffers
=
139 rzalloc_array(NULL
, /* ctx */
140 struct active_atomic_buffer
,
141 ctx
->Const
.MaxAtomicBufferBindings
);
144 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
145 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
149 nir_shader
*nir
= sh
->Program
->nir
;
151 nir_foreach_variable(var
, &nir
->uniforms
) {
152 if (!glsl_contains_atomic(var
->type
))
155 int offset
= var
->data
.offset
;
156 unsigned uniform_loc
= var
->data
.location
;
158 process_atomic_variable(var
->type
,
173 check_atomic_counters_overlap(const nir_variable
*x
, const nir_variable
*y
)
175 return ((x
->data
.offset
>= y
->data
.offset
&&
176 x
->data
.offset
< y
->data
.offset
+ glsl_atomic_size(y
->type
)) ||
177 (y
->data
.offset
>= x
->data
.offset
&&
178 y
->data
.offset
< x
->data
.offset
+ glsl_atomic_size(x
->type
)));
182 cmp_active_counter_offsets(const void *a
, const void *b
)
184 const struct active_atomic_counter_uniform
*const first
=
185 (struct active_atomic_counter_uniform
*) a
;
186 const struct active_atomic_counter_uniform
*const second
=
187 (struct active_atomic_counter_uniform
*) b
;
189 return first
->var
->data
.offset
- second
->var
->data
.offset
;
193 gl_nir_link_assign_atomic_counter_resources(struct gl_context
*ctx
,
194 struct gl_shader_program
*prog
)
196 unsigned num_buffers
;
197 unsigned num_atomic_buffers
[MESA_SHADER_STAGES
] = {0};
198 struct active_atomic_buffer
*abs
=
199 find_active_atomic_counters(ctx
, prog
, &num_buffers
);
201 prog
->data
->AtomicBuffers
=
202 rzalloc_array(prog
->data
, struct gl_active_atomic_buffer
, num_buffers
);
203 prog
->data
->NumAtomicBuffers
= num_buffers
;
205 unsigned buffer_idx
= 0;
206 for (unsigned binding
= 0;
207 binding
< ctx
->Const
.MaxAtomicBufferBindings
;
210 /* If the binding was not used, skip.
212 if (abs
[binding
].size
== 0)
215 struct active_atomic_buffer
*ab
= abs
+ binding
;
216 struct gl_active_atomic_buffer
*mab
=
217 prog
->data
->AtomicBuffers
+ buffer_idx
;
219 /* Assign buffer-specific fields. */
220 mab
->Binding
= binding
;
221 mab
->MinimumSize
= ab
->size
;
222 mab
->Uniforms
= rzalloc_array(prog
->data
->AtomicBuffers
, GLuint
,
224 mab
->NumUniforms
= ab
->num_uniforms
;
226 /* Assign counter-specific fields. */
227 for (unsigned j
= 0; j
< ab
->num_uniforms
; j
++) {
228 nir_variable
*var
= ab
->uniforms
[j
].var
;
229 struct gl_uniform_storage
*storage
=
230 &prog
->data
->UniformStorage
[ab
->uniforms
[j
].loc
];
232 mab
->Uniforms
[j
] = ab
->uniforms
[j
].loc
;
234 storage
->atomic_buffer_index
= buffer_idx
;
235 storage
->offset
= var
->data
.offset
;
236 if (glsl_type_is_array(var
->type
)) {
237 const struct glsl_type
*without_array
=
238 glsl_without_array(var
->type
);
239 storage
->array_stride
= glsl_atomic_size(without_array
);
241 storage
->array_stride
= 0;
243 if (!glsl_type_is_matrix(var
->type
))
244 storage
->matrix_stride
= 0;
247 /* Assign stage-specific fields. */
248 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; ++stage
) {
249 if (ab
->stage_counter_references
[stage
]) {
250 mab
->StageReferences
[stage
] = GL_TRUE
;
251 num_atomic_buffers
[stage
]++;
253 mab
->StageReferences
[stage
] = GL_FALSE
;
260 /* Store a list pointers to atomic buffers per stage and store the index
261 * to the intra-stage buffer list in uniform storage.
263 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; ++stage
) {
264 if (prog
->_LinkedShaders
[stage
] == NULL
||
265 num_atomic_buffers
[stage
] <= 0)
268 struct gl_program
*gl_prog
= prog
->_LinkedShaders
[stage
]->Program
;
269 gl_prog
->info
.num_abos
= num_atomic_buffers
[stage
];
270 gl_prog
->sh
.AtomicBuffers
=
271 rzalloc_array(gl_prog
,
272 struct gl_active_atomic_buffer
*,
273 num_atomic_buffers
[stage
]);
275 gl_prog
->nir
->info
.num_abos
= num_atomic_buffers
[stage
];
277 unsigned intra_stage_idx
= 0;
278 for (unsigned i
= 0; i
< num_buffers
; i
++) {
279 struct gl_active_atomic_buffer
*atomic_buffer
=
280 &prog
->data
->AtomicBuffers
[i
];
281 if (!atomic_buffer
->StageReferences
[stage
])
284 gl_prog
->sh
.AtomicBuffers
[intra_stage_idx
] = atomic_buffer
;
286 for (unsigned u
= 0; u
< atomic_buffer
->NumUniforms
; u
++) {
287 GLuint uniform_loc
= atomic_buffer
->Uniforms
[u
];
288 struct gl_opaque_uniform_index
*opaque
=
289 prog
->data
->UniformStorage
[uniform_loc
].opaque
+ stage
;
290 opaque
->index
= intra_stage_idx
;
291 opaque
->active
= true;
298 assert(buffer_idx
== num_buffers
);
304 gl_nir_link_check_atomic_counter_resources(struct gl_context
*ctx
,
305 struct gl_shader_program
*prog
)
307 unsigned num_buffers
;
308 struct active_atomic_buffer
*abs
=
309 find_active_atomic_counters(ctx
, prog
, &num_buffers
);
310 unsigned atomic_counters
[MESA_SHADER_STAGES
] = {0};
311 unsigned atomic_buffers
[MESA_SHADER_STAGES
] = {0};
312 unsigned total_atomic_counters
= 0;
313 unsigned total_atomic_buffers
= 0;
315 /* Sum the required resources. Note that this counts buffers and
316 * counters referenced by several shader stages multiple times
317 * against the combined limit -- That's the behavior the spec
320 for (unsigned i
= 0; i
< ctx
->Const
.MaxAtomicBufferBindings
; i
++) {
321 if (abs
[i
].size
== 0)
324 qsort(abs
[i
].uniforms
, abs
[i
].num_uniforms
,
325 sizeof(struct active_atomic_counter_uniform
),
326 cmp_active_counter_offsets
);
328 for (unsigned j
= 1; j
< abs
[i
].num_uniforms
; j
++) {
329 /* If an overlapping counter found, it must be a reference to the
330 * same counter from a different shader stage.
332 if (check_atomic_counters_overlap(abs
[i
].uniforms
[j
-1].var
,
333 abs
[i
].uniforms
[j
].var
)
334 && strcmp(abs
[i
].uniforms
[j
-1].var
->name
,
335 abs
[i
].uniforms
[j
].var
->name
) != 0) {
336 linker_error(prog
, "Atomic counter %s declared at offset %d "
337 "which is already in use.",
338 abs
[i
].uniforms
[j
].var
->name
,
339 abs
[i
].uniforms
[j
].var
->data
.offset
);
343 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; ++j
) {
344 const unsigned n
= abs
[i
].stage_counter_references
[j
];
347 atomic_counters
[j
] += n
;
348 total_atomic_counters
+= n
;
350 total_atomic_buffers
++;
355 /* Check that they are within the supported limits. */
356 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
357 if (atomic_counters
[i
] > ctx
->Const
.Program
[i
].MaxAtomicCounters
)
358 linker_error(prog
, "Too many %s shader atomic counters",
359 _mesa_shader_stage_to_string(i
));
361 if (atomic_buffers
[i
] > ctx
->Const
.Program
[i
].MaxAtomicBuffers
)
362 linker_error(prog
, "Too many %s shader atomic counter buffers",
363 _mesa_shader_stage_to_string(i
));
366 if (total_atomic_counters
> ctx
->Const
.MaxCombinedAtomicCounters
)
367 linker_error(prog
, "Too many combined atomic counters");
369 if (total_atomic_buffers
> ctx
->Const
.MaxCombinedAtomicBuffers
)
370 linker_error(prog
, "Too many combined atomic buffers");