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 id
, ir_variable
*var
)
57 counters
= (active_atomic_counter
*)
58 realloc(counters
, sizeof(active_atomic_counter
) * (num_counters
+ 1));
60 counters
[num_counters
].id
= id
;
61 counters
[num_counters
].var
= var
;
65 active_atomic_counter
*counters
;
66 unsigned num_counters
;
67 unsigned stage_references
[MESA_SHADER_STAGES
];
72 cmp_actives(const void *a
, const void *b
)
74 const active_atomic_counter
*const first
= (active_atomic_counter
*) a
;
75 const active_atomic_counter
*const second
= (active_atomic_counter
*) b
;
77 return int(first
->var
->data
.atomic
.offset
) - int(second
->var
->data
.atomic
.offset
);
81 check_atomic_counters_overlap(const ir_variable
*x
, const ir_variable
*y
)
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()));
89 active_atomic_buffer
*
90 find_active_atomic_counters(struct gl_context
*ctx
,
91 struct gl_shader_program
*prog
,
92 unsigned *num_buffers
)
94 active_atomic_buffer
*const buffers
=
95 new active_atomic_buffer
[ctx
->Const
.MaxAtomicBufferBindings
];
99 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
100 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
104 foreach_list(node
, sh
->ir
) {
105 ir_variable
*var
= ((ir_instruction
*)node
)->as_variable();
107 if (var
&& var
->type
->contains_atomic()) {
109 bool found
= prog
->UniformHash
->get(id
, var
->name
);
112 active_atomic_buffer
*buf
= &buffers
[var
->data
.binding
];
114 /* If this is the first time the buffer is used, increment
115 * the counter of buffers used.
120 buf
->push_back(id
, var
);
122 buf
->stage_references
[i
]++;
123 buf
->size
= MAX2(buf
->size
, var
->data
.atomic
.offset
+
124 var
->type
->atomic_size());
129 for (unsigned i
= 0; i
< ctx
->Const
.MaxAtomicBufferBindings
; i
++) {
130 if (buffers
[i
].size
== 0)
133 qsort(buffers
[i
].counters
, buffers
[i
].num_counters
,
134 sizeof(active_atomic_counter
),
137 for (unsigned j
= 1; j
< buffers
[i
].num_counters
; j
++) {
138 /* If an overlapping counter found, it must be a reference to the
139 * same counter from a different shader stage.
141 if (check_atomic_counters_overlap(buffers
[i
].counters
[j
-1].var
,
142 buffers
[i
].counters
[j
].var
)
143 && strcmp(buffers
[i
].counters
[j
-1].var
->name
,
144 buffers
[i
].counters
[j
].var
->name
) != 0) {
145 linker_error(prog
, "Atomic counter %s declared at offset %d "
146 "which is already in use.",
147 buffers
[i
].counters
[j
].var
->name
,
148 buffers
[i
].counters
[j
].var
->data
.atomic
.offset
);
157 link_assign_atomic_counter_resources(struct gl_context
*ctx
,
158 struct gl_shader_program
*prog
)
160 unsigned num_buffers
;
161 active_atomic_buffer
*abs
=
162 find_active_atomic_counters(ctx
, prog
, &num_buffers
);
164 prog
->AtomicBuffers
= rzalloc_array(prog
, gl_active_atomic_buffer
,
166 prog
->NumAtomicBuffers
= num_buffers
;
169 for (unsigned binding
= 0;
170 binding
< ctx
->Const
.MaxAtomicBufferBindings
;
173 /* If the binding was not used, skip.
175 if (abs
[binding
].size
== 0)
178 active_atomic_buffer
&ab
= abs
[binding
];
179 gl_active_atomic_buffer
&mab
= prog
->AtomicBuffers
[i
];
181 /* Assign buffer-specific fields. */
182 mab
.Binding
= binding
;
183 mab
.MinimumSize
= ab
.size
;
184 mab
.Uniforms
= rzalloc_array(prog
->AtomicBuffers
, GLuint
,
186 mab
.NumUniforms
= ab
.num_counters
;
188 /* Assign counter-specific fields. */
189 for (unsigned j
= 0; j
< ab
.num_counters
; j
++) {
190 ir_variable
*const var
= ab
.counters
[j
].var
;
191 const unsigned id
= ab
.counters
[j
].id
;
192 gl_uniform_storage
*const storage
= &prog
->UniformStorage
[id
];
194 mab
.Uniforms
[j
] = id
;
195 var
->data
.atomic
.buffer_index
= i
;
196 storage
->atomic_buffer_index
= i
;
197 storage
->offset
= var
->data
.atomic
.offset
;
198 storage
->array_stride
= (var
->type
->is_array() ?
199 var
->type
->element_type()->atomic_size() : 0);
202 /* Assign stage-specific fields. */
203 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; ++j
)
204 mab
.StageReferences
[j
] =
205 (ab
.stage_references
[j
] ? GL_TRUE
: GL_FALSE
);
211 assert(i
== num_buffers
);
215 link_check_atomic_counter_resources(struct gl_context
*ctx
,
216 struct gl_shader_program
*prog
)
218 unsigned num_buffers
;
219 active_atomic_buffer
*const abs
=
220 find_active_atomic_counters(ctx
, prog
, &num_buffers
);
221 unsigned atomic_counters
[MESA_SHADER_STAGES
] = {};
222 unsigned atomic_buffers
[MESA_SHADER_STAGES
] = {};
223 unsigned total_atomic_counters
= 0;
224 unsigned total_atomic_buffers
= 0;
226 /* Sum the required resources. Note that this counts buffers and
227 * counters referenced by several shader stages multiple times
228 * against the combined limit -- That's the behavior the spec
231 for (unsigned i
= 0; i
< ctx
->Const
.MaxAtomicBufferBindings
; i
++) {
232 if (abs
[i
].size
== 0)
235 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; ++j
) {
236 const unsigned n
= abs
[i
].stage_references
[j
];
239 atomic_counters
[j
] += n
;
240 total_atomic_counters
+= n
;
242 total_atomic_buffers
++;
247 /* Check that they are within the supported limits. */
248 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
249 if (atomic_counters
[i
] > ctx
->Const
.Program
[i
].MaxAtomicCounters
)
250 linker_error(prog
, "Too many %s shader atomic counters",
251 _mesa_shader_stage_to_string(i
));
253 if (atomic_buffers
[i
] > ctx
->Const
.Program
[i
].MaxAtomicBuffers
)
254 linker_error(prog
, "Too many %s shader atomic counter buffers",
255 _mesa_shader_stage_to_string(i
));
258 if (total_atomic_counters
> ctx
->Const
.MaxCombinedAtomicCounters
)
259 linker_error(prog
, "Too many combined atomic counters");
261 if (total_atomic_buffers
> ctx
->Const
.MaxCombinedAtomicBuffers
)
262 linker_error(prog
, "Too many combined atomic buffers");