Bug Summary

File:root/firefox-clang/third_party/rust/glslopt/glsl-optimizer/src/compiler/glsl_types.h
Warning:line 1377, column 27
Division by zero

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name glsl_types.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/third_party/rust/glslopt -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/root/firefox-clang/third_party/rust/glslopt -resource-dir /usr/lib/llvm-21/lib/clang/21 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -I glsl-optimizer/include -I glsl-optimizer/src/mesa -I glsl-optimizer/src/mapi -I glsl-optimizer/src/compiler -I glsl-optimizer/src/compiler/glsl -I glsl-optimizer/src/gallium/auxiliary -I glsl-optimizer/src/gallium/include -I glsl-optimizer/src -I glsl-optimizer/src/util -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/stl_wrappers -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D _GLIBCXX_ASSERTIONS -D DEBUG=1 -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -D MOZILLA_CONFIG_H -D __STDC_FORMAT_MACROS -D _GNU_SOURCE -D HAVE_ENDIAN_H -D HAVE_PTHREAD -D HAVE_TIMESPEC_GET -D MOZ_INCLUDE_MOZALLOC_H -D mozilla_throw_gcc_h -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-deprecated-this-capture -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fno-rtti -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2025-06-27-100320-3286336-1 -x c++ glsl-optimizer/src/compiler/glsl_types.cpp

glsl-optimizer/src/compiler/glsl_types.cpp

1/*
2 * Copyright © 2009 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 <stdio.h>
25#include "main/macros.h"
26#include "compiler/glsl/glsl_parser_extras.h"
27#include "glsl_types.h"
28#include "util/hash_table.h"
29#include "util/u_string.h"
30
31
32mtx_t glsl_type::hash_mutex = _MTX_INITIALIZER_NP{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } };
33hash_table *glsl_type::explicit_matrix_types = NULL__null;
34hash_table *glsl_type::array_types = NULL__null;
35hash_table *glsl_type::struct_types = NULL__null;
36hash_table *glsl_type::interface_types = NULL__null;
37hash_table *glsl_type::function_types = NULL__null;
38hash_table *glsl_type::subroutine_types = NULL__null;
39
40/* There might be multiple users for types (e.g. application using OpenGL
41 * and Vulkan simultanously or app using multiple Vulkan instances). Counter
42 * is used to make sure we don't release the types if a user is still present.
43 */
44static uint32_t glsl_type_users = 0;
45
46glsl_type::glsl_type(GLenum gl_type,
47 glsl_base_type base_type, unsigned vector_elements,
48 unsigned matrix_columns, const char *name,
49 unsigned explicit_stride, bool row_major) :
50 gl_type(gl_type),
51 base_type(base_type), sampled_type(GLSL_TYPE_VOID),
52 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
53 interface_packing(0), interface_row_major(row_major), packed(0),
54 vector_elements(vector_elements), matrix_columns(matrix_columns),
55 length(0), explicit_stride(explicit_stride)
56{
57 /* Values of these types must fit in the two bits of
58 * glsl_type::sampled_type.
59 */
60 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT) & 3) == unsigned(GLSL_TYPE_UINT))do { (void) sizeof(char [1 - 2*!((unsigned(GLSL_TYPE_UINT) &
3) == unsigned(GLSL_TYPE_UINT))]); } while (0)
;
61 STATIC_ASSERT((unsigned(GLSL_TYPE_INT) & 3) == unsigned(GLSL_TYPE_INT))do { (void) sizeof(char [1 - 2*!((unsigned(GLSL_TYPE_INT) &
3) == unsigned(GLSL_TYPE_INT))]); } while (0)
;
62 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT) & 3) == unsigned(GLSL_TYPE_FLOAT))do { (void) sizeof(char [1 - 2*!((unsigned(GLSL_TYPE_FLOAT) &
3) == unsigned(GLSL_TYPE_FLOAT))]); } while (0)
;
63
64 ASSERT_BITFIELD_SIZE(glsl_type, base_type, GLSL_TYPE_ERROR)do { glsl_type s; s.base_type = (GLSL_TYPE_ERROR); (static_cast
<bool> ((int) s.base_type == (GLSL_TYPE_ERROR) &&
"Insufficient bitfield size!") ? void (0) : __assert_fail ("(int) s.base_type == (GLSL_TYPE_ERROR) && \"Insufficient bitfield size!\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); } while (0)
;
65 ASSERT_BITFIELD_SIZE(glsl_type, sampled_type, GLSL_TYPE_ERROR)do { glsl_type s; s.sampled_type = (GLSL_TYPE_ERROR); (static_cast
<bool> ((int) s.sampled_type == (GLSL_TYPE_ERROR) &&
"Insufficient bitfield size!") ? void (0) : __assert_fail ("(int) s.sampled_type == (GLSL_TYPE_ERROR) && \"Insufficient bitfield size!\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); } while (0)
;
66 ASSERT_BITFIELD_SIZE(glsl_type, sampler_dimensionality,do { glsl_type s; s.sampler_dimensionality = (GLSL_SAMPLER_DIM_SUBPASS_MS
); (static_cast <bool> ((int) s.sampler_dimensionality ==
(GLSL_SAMPLER_DIM_SUBPASS_MS) && "Insufficient bitfield size!"
) ? void (0) : __assert_fail ("(int) s.sampler_dimensionality == (GLSL_SAMPLER_DIM_SUBPASS_MS) && \"Insufficient bitfield size!\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); } while (0)
67 GLSL_SAMPLER_DIM_SUBPASS_MS)do { glsl_type s; s.sampler_dimensionality = (GLSL_SAMPLER_DIM_SUBPASS_MS
); (static_cast <bool> ((int) s.sampler_dimensionality ==
(GLSL_SAMPLER_DIM_SUBPASS_MS) && "Insufficient bitfield size!"
) ? void (0) : __assert_fail ("(int) s.sampler_dimensionality == (GLSL_SAMPLER_DIM_SUBPASS_MS) && \"Insufficient bitfield size!\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); } while (0)
;
68
69 this->mem_ctx = ralloc_context(NULL__null);
70 assert(this->mem_ctx != NULL)(static_cast <bool> (this->mem_ctx != __null) ? void
(0) : __assert_fail ("this->mem_ctx != NULL", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
71
72 assert(name != NULL)(static_cast <bool> (name != __null) ? void (0) : __assert_fail
("name != NULL", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
73 this->name = ralloc_strdup(this->mem_ctx, name);
74
75 /* Neither dimension is zero or both dimensions are zero.
76 */
77 assert((vector_elements == 0) == (matrix_columns == 0))(static_cast <bool> ((vector_elements == 0) == (matrix_columns
== 0)) ? void (0) : __assert_fail ("(vector_elements == 0) == (matrix_columns == 0)"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
78 memset(& fields, 0, sizeof(fields));
79}
80
81glsl_type::glsl_type(GLenum gl_type, glsl_base_type base_type,
82 enum glsl_sampler_dim dim, bool shadow, bool array,
83 glsl_base_type type, const char *name) :
84 gl_type(gl_type),
85 base_type(base_type), sampled_type(type),
86 sampler_dimensionality(dim), sampler_shadow(shadow),
87 sampler_array(array), interface_packing(0),
88 interface_row_major(0), packed(0),
89 length(0), explicit_stride(0)
90{
91 this->mem_ctx = ralloc_context(NULL__null);
92 assert(this->mem_ctx != NULL)(static_cast <bool> (this->mem_ctx != __null) ? void
(0) : __assert_fail ("this->mem_ctx != NULL", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
93
94 assert(name != NULL)(static_cast <bool> (name != __null) ? void (0) : __assert_fail
("name != NULL", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
95 this->name = ralloc_strdup(this->mem_ctx, name);
96
97 memset(& fields, 0, sizeof(fields));
98
99 matrix_columns = vector_elements = 1;
100}
101
102glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
103 const char *name, bool packed) :
104 gl_type(0),
105 base_type(GLSL_TYPE_STRUCT), sampled_type(GLSL_TYPE_VOID),
106 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
107 interface_packing(0), interface_row_major(0), packed(packed),
108 vector_elements(0), matrix_columns(0),
109 length(num_fields), explicit_stride(0)
110{
111 unsigned int i;
112
113 this->mem_ctx = ralloc_context(NULL__null);
114 assert(this->mem_ctx != NULL)(static_cast <bool> (this->mem_ctx != __null) ? void
(0) : __assert_fail ("this->mem_ctx != NULL", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
115
116 assert(name != NULL)(static_cast <bool> (name != __null) ? void (0) : __assert_fail
("name != NULL", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
117 this->name = ralloc_strdup(this->mem_ctx, name);
118 /* Zero-fill to prevent spurious Valgrind errors when serializing NIR
119 * due to uninitialized unused bits in bit fields. */
120 this->fields.structure = rzalloc_array(this->mem_ctx,((glsl_struct_field *) rzalloc_array_size(this->mem_ctx, sizeof
(glsl_struct_field), length))
121 glsl_struct_field, length)((glsl_struct_field *) rzalloc_array_size(this->mem_ctx, sizeof
(glsl_struct_field), length))
;
122
123 for (i = 0; i < length; i++) {
124 this->fields.structure[i] = fields[i];
125 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
126 fields[i].name);
127 }
128}
129
130glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
131 enum glsl_interface_packing packing,
132 bool row_major, const char *name) :
133 gl_type(0),
134 base_type(GLSL_TYPE_INTERFACE), sampled_type(GLSL_TYPE_VOID),
135 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
136 interface_packing((unsigned) packing),
137 interface_row_major((unsigned) row_major), packed(0),
138 vector_elements(0), matrix_columns(0),
139 length(num_fields), explicit_stride(0)
140{
141 unsigned int i;
142
143 this->mem_ctx = ralloc_context(NULL__null);
144 assert(this->mem_ctx != NULL)(static_cast <bool> (this->mem_ctx != __null) ? void
(0) : __assert_fail ("this->mem_ctx != NULL", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
145
146 assert(name != NULL)(static_cast <bool> (name != __null) ? void (0) : __assert_fail
("name != NULL", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
147 this->name = ralloc_strdup(this->mem_ctx, name);
148 this->fields.structure = rzalloc_array(this->mem_ctx,((glsl_struct_field *) rzalloc_array_size(this->mem_ctx, sizeof
(glsl_struct_field), length))
149 glsl_struct_field, length)((glsl_struct_field *) rzalloc_array_size(this->mem_ctx, sizeof
(glsl_struct_field), length))
;
150 for (i = 0; i < length; i++) {
151 this->fields.structure[i] = fields[i];
152 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
153 fields[i].name);
154 }
155}
156
157glsl_type::glsl_type(const glsl_type *return_type,
158 const glsl_function_param *params, unsigned num_params) :
159 gl_type(0),
160 base_type(GLSL_TYPE_FUNCTION), sampled_type(GLSL_TYPE_VOID),
161 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
162 interface_packing(0), interface_row_major(0), packed(0),
163 vector_elements(0), matrix_columns(0),
164 length(num_params), explicit_stride(0)
165{
166 unsigned int i;
167
168 this->mem_ctx = ralloc_context(NULL__null);
169 assert(this->mem_ctx != NULL)(static_cast <bool> (this->mem_ctx != __null) ? void
(0) : __assert_fail ("this->mem_ctx != NULL", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
170
171 this->fields.parameters = rzalloc_array(this->mem_ctx,((glsl_function_param *) rzalloc_array_size(this->mem_ctx,
sizeof(glsl_function_param), num_params + 1))
172 glsl_function_param, num_params + 1)((glsl_function_param *) rzalloc_array_size(this->mem_ctx,
sizeof(glsl_function_param), num_params + 1))
;
173
174 /* We store the return type as the first parameter */
175 this->fields.parameters[0].type = return_type;
176 this->fields.parameters[0].in = false;
177 this->fields.parameters[0].out = true;
178
179 /* We store the i'th parameter in slot i+1 */
180 for (i = 0; i < length; i++) {
181 this->fields.parameters[i + 1].type = params[i].type;
182 this->fields.parameters[i + 1].in = params[i].in;
183 this->fields.parameters[i + 1].out = params[i].out;
184 }
185}
186
187glsl_type::glsl_type(const char *subroutine_name) :
188 gl_type(0),
189 base_type(GLSL_TYPE_SUBROUTINE), sampled_type(GLSL_TYPE_VOID),
190 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
191 interface_packing(0), interface_row_major(0), packed(0),
192 vector_elements(1), matrix_columns(1),
193 length(0), explicit_stride(0)
194{
195 this->mem_ctx = ralloc_context(NULL__null);
196 assert(this->mem_ctx != NULL)(static_cast <bool> (this->mem_ctx != __null) ? void
(0) : __assert_fail ("this->mem_ctx != NULL", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
197
198 assert(subroutine_name != NULL)(static_cast <bool> (subroutine_name != __null) ? void (
0) : __assert_fail ("subroutine_name != NULL", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
199 this->name = ralloc_strdup(this->mem_ctx, subroutine_name);
200}
201
202glsl_type::~glsl_type()
203{
204 ralloc_free(this->mem_ctx);
205}
206
207bool
208glsl_type::contains_sampler() const
209{
210 if (this->is_array()) {
211 return this->fields.array->contains_sampler();
212 } else if (this->is_struct() || this->is_interface()) {
213 for (unsigned int i = 0; i < this->length; i++) {
214 if (this->fields.structure[i].type->contains_sampler())
215 return true;
216 }
217 return false;
218 } else {
219 return this->is_sampler();
220 }
221}
222
223bool
224glsl_type::contains_array() const
225{
226 if (this->is_struct() || this->is_interface()) {
227 for (unsigned int i = 0; i < this->length; i++) {
228 if (this->fields.structure[i].type->contains_array())
229 return true;
230 }
231 return false;
232 } else {
233 return this->is_array();
234 }
235}
236
237bool
238glsl_type::contains_integer() const
239{
240 if (this->is_array()) {
241 return this->fields.array->contains_integer();
242 } else if (this->is_struct() || this->is_interface()) {
243 for (unsigned int i = 0; i < this->length; i++) {
244 if (this->fields.structure[i].type->contains_integer())
245 return true;
246 }
247 return false;
248 } else {
249 return this->is_integer();
250 }
251}
252
253bool
254glsl_type::contains_double() const
255{
256 if (this->is_array()) {
257 return this->fields.array->contains_double();
258 } else if (this->is_struct() || this->is_interface()) {
259 for (unsigned int i = 0; i < this->length; i++) {
260 if (this->fields.structure[i].type->contains_double())
261 return true;
262 }
263 return false;
264 } else {
265 return this->is_double();
266 }
267}
268
269bool
270glsl_type::contains_64bit() const
271{
272 if (this->is_array()) {
273 return this->fields.array->contains_64bit();
274 } else if (this->is_struct() || this->is_interface()) {
275 for (unsigned int i = 0; i < this->length; i++) {
276 if (this->fields.structure[i].type->contains_64bit())
277 return true;
278 }
279 return false;
280 } else {
281 return this->is_64bit();
282 }
283}
284
285bool
286glsl_type::contains_opaque() const {
287 switch (base_type) {
288 case GLSL_TYPE_SAMPLER:
289 case GLSL_TYPE_IMAGE:
290 case GLSL_TYPE_ATOMIC_UINT:
291 return true;
292 case GLSL_TYPE_ARRAY:
293 return fields.array->contains_opaque();
294 case GLSL_TYPE_STRUCT:
295 case GLSL_TYPE_INTERFACE:
296 for (unsigned int i = 0; i < length; i++) {
297 if (fields.structure[i].type->contains_opaque())
298 return true;
299 }
300 return false;
301 default:
302 return false;
303 }
304}
305
306bool
307glsl_type::contains_subroutine() const
308{
309 if (this->is_array()) {
310 return this->fields.array->contains_subroutine();
311 } else if (this->is_struct() || this->is_interface()) {
312 for (unsigned int i = 0; i < this->length; i++) {
313 if (this->fields.structure[i].type->contains_subroutine())
314 return true;
315 }
316 return false;
317 } else {
318 return this->is_subroutine();
319 }
320}
321
322gl_texture_index
323glsl_type::sampler_index() const
324{
325 const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
326
327 assert(t->is_sampler() || t->is_image())(static_cast <bool> (t->is_sampler() || t->is_image
()) ? void (0) : __assert_fail ("t->is_sampler() || t->is_image()"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
328
329 switch (t->sampler_dimensionality) {
330 case GLSL_SAMPLER_DIM_1D:
331 return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
332 case GLSL_SAMPLER_DIM_2D:
333 return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
334 case GLSL_SAMPLER_DIM_3D:
335 return TEXTURE_3D_INDEX;
336 case GLSL_SAMPLER_DIM_CUBE:
337 return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
338 case GLSL_SAMPLER_DIM_RECT:
339 return TEXTURE_RECT_INDEX;
340 case GLSL_SAMPLER_DIM_BUF:
341 return TEXTURE_BUFFER_INDEX;
342 case GLSL_SAMPLER_DIM_EXTERNAL:
343 return TEXTURE_EXTERNAL_INDEX;
344 case GLSL_SAMPLER_DIM_MS:
345 return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
346 default:
347 assert(!"Should not get here.")(static_cast <bool> (!"Should not get here.") ? void (0
) : __assert_fail ("!\"Should not get here.\"", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
348 return TEXTURE_BUFFER_INDEX;
349 }
350}
351
352bool
353glsl_type::contains_image() const
354{
355 if (this->is_array()) {
356 return this->fields.array->contains_image();
357 } else if (this->is_struct() || this->is_interface()) {
358 for (unsigned int i = 0; i < this->length; i++) {
359 if (this->fields.structure[i].type->contains_image())
360 return true;
361 }
362 return false;
363 } else {
364 return this->is_image();
365 }
366}
367
368const glsl_type *glsl_type::get_base_type() const
369{
370 switch (base_type) {
371 case GLSL_TYPE_UINT:
372 return uint_type;
373 case GLSL_TYPE_UINT16:
374 return uint16_t_type;
375 case GLSL_TYPE_UINT8:
376 return uint8_t_type;
377 case GLSL_TYPE_INT:
378 return int_type;
379 case GLSL_TYPE_INT16:
380 return int16_t_type;
381 case GLSL_TYPE_INT8:
382 return int8_t_type;
383 case GLSL_TYPE_FLOAT:
384 return float_type;
385 case GLSL_TYPE_FLOAT16:
386 return float16_t_type;
387 case GLSL_TYPE_DOUBLE:
388 return double_type;
389 case GLSL_TYPE_BOOL:
390 return bool_type;
391 case GLSL_TYPE_UINT64:
392 return uint64_t_type;
393 case GLSL_TYPE_INT64:
394 return int64_t_type;
395 default:
396 return error_type;
397 }
398}
399
400
401const glsl_type *glsl_type::get_scalar_type() const
402{
403 const glsl_type *type = this;
404
405 /* Handle arrays */
406 while (type->base_type == GLSL_TYPE_ARRAY)
407 type = type->fields.array;
408
409 const glsl_type *scalar_type = type->get_base_type();
410 if (scalar_type == error_type)
411 return type;
412
413 return scalar_type;
414}
415
416
417const glsl_type *glsl_type::get_bare_type() const
418{
419 switch (this->base_type) {
420 case GLSL_TYPE_UINT8:
421 case GLSL_TYPE_INT8:
422 case GLSL_TYPE_UINT16:
423 case GLSL_TYPE_INT16:
424 case GLSL_TYPE_FLOAT16:
425 case GLSL_TYPE_UINT:
426 case GLSL_TYPE_INT:
427 case GLSL_TYPE_FLOAT:
428 case GLSL_TYPE_BOOL:
429 case GLSL_TYPE_DOUBLE:
430 case GLSL_TYPE_UINT64:
431 case GLSL_TYPE_INT64:
432 return get_instance(this->base_type, this->vector_elements,
433 this->matrix_columns);
434
435 case GLSL_TYPE_STRUCT:
436 case GLSL_TYPE_INTERFACE: {
437 glsl_struct_field *bare_fields = new glsl_struct_field[this->length];
438 for (unsigned i = 0; i < this->length; i++) {
439 bare_fields[i].type = this->fields.structure[i].type->get_bare_type();
440 bare_fields[i].name = this->fields.structure[i].name;
441 }
442 const glsl_type *bare_type =
443 get_struct_instance(bare_fields, this->length, this->name);
444 delete[] bare_fields;
445 return bare_type;
446 }
447
448 case GLSL_TYPE_ARRAY:
449 return get_array_instance(this->fields.array->get_bare_type(),
450 this->length);
451
452 case GLSL_TYPE_SAMPLER:
453 case GLSL_TYPE_IMAGE:
454 case GLSL_TYPE_ATOMIC_UINT:
455 case GLSL_TYPE_VOID:
456 case GLSL_TYPE_SUBROUTINE:
457 case GLSL_TYPE_FUNCTION:
458 case GLSL_TYPE_ERROR:
459 return this;
460 }
461
462 unreachable("Invalid base type")do { (static_cast <bool> (!"Invalid base type") ? void (
0) : __assert_fail ("!\"Invalid base type\"", __builtin_FILE (
), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__)); __builtin_unreachable
(); } while (0)
;
463}
464
465const glsl_type *glsl_type::get_float16_type() const
466{
467 assert(this->base_type == GLSL_TYPE_FLOAT)(static_cast <bool> (this->base_type == GLSL_TYPE_FLOAT
) ? void (0) : __assert_fail ("this->base_type == GLSL_TYPE_FLOAT"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
468
469 return get_instance(GLSL_TYPE_FLOAT16,
470 this->vector_elements,
471 this->matrix_columns,
472 this->explicit_stride,
473 this->interface_row_major);
474}
475
476static void
477hash_free_type_function(struct hash_entry *entry)
478{
479 glsl_type *type = (glsl_type *) entry->data;
480
481 if (type->is_array())
482 free((void*)entry->key);
483
484 delete type;
485}
486
487void
488glsl_type_singleton_init_or_ref()
489{
490 mtx_lock(&glsl_type::hash_mutex);
491 glsl_type_users++;
492 mtx_unlock(&glsl_type::hash_mutex);
493}
494
495void
496glsl_type_singleton_decref()
497{
498 mtx_lock(&glsl_type::hash_mutex);
499 assert(glsl_type_users > 0)(static_cast <bool> (glsl_type_users > 0) ? void (0)
: __assert_fail ("glsl_type_users > 0", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
500
501 /* Do not release glsl_types if they are still used. */
502 if (--glsl_type_users) {
503 mtx_unlock(&glsl_type::hash_mutex);
504 return;
505 }
506
507 if (glsl_type::explicit_matrix_types != NULL__null) {
508 _mesa_hash_table_destroy(glsl_type::explicit_matrix_types,
509 hash_free_type_function);
510 glsl_type::explicit_matrix_types = NULL__null;
511 }
512
513 if (glsl_type::array_types != NULL__null) {
514 _mesa_hash_table_destroy(glsl_type::array_types, hash_free_type_function);
515 glsl_type::array_types = NULL__null;
516 }
517
518 if (glsl_type::struct_types != NULL__null) {
519 _mesa_hash_table_destroy(glsl_type::struct_types, hash_free_type_function);
520 glsl_type::struct_types = NULL__null;
521 }
522
523 if (glsl_type::interface_types != NULL__null) {
524 _mesa_hash_table_destroy(glsl_type::interface_types, hash_free_type_function);
525 glsl_type::interface_types = NULL__null;
526 }
527
528 if (glsl_type::function_types != NULL__null) {
529 _mesa_hash_table_destroy(glsl_type::function_types, hash_free_type_function);
530 glsl_type::function_types = NULL__null;
531 }
532
533 if (glsl_type::subroutine_types != NULL__null) {
534 _mesa_hash_table_destroy(glsl_type::subroutine_types, hash_free_type_function);
535 glsl_type::subroutine_types = NULL__null;
536 }
537
538 mtx_unlock(&glsl_type::hash_mutex);
539}
540
541
542glsl_type::glsl_type(const glsl_type *array, unsigned length,
543 unsigned explicit_stride) :
544 base_type(GLSL_TYPE_ARRAY), sampled_type(GLSL_TYPE_VOID),
545 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
546 interface_packing(0), interface_row_major(0), packed(0),
547 vector_elements(0), matrix_columns(0),
548 length(length), name(NULL__null), explicit_stride(explicit_stride)
549{
550 this->fields.array = array;
551 /* Inherit the gl type of the base. The GL type is used for
552 * uniform/statevar handling in Mesa and the arrayness of the type
553 * is represented by the size rather than the type.
554 */
555 this->gl_type = array->gl_type;
556
557 /* Allow a maximum of 10 characters for the array size. This is enough
558 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
559 * NUL.
560 */
561 const unsigned name_length = strlen(array->name) + 10 + 3;
562
563 this->mem_ctx = ralloc_context(NULL__null);
564 assert(this->mem_ctx != NULL)(static_cast <bool> (this->mem_ctx != __null) ? void
(0) : __assert_fail ("this->mem_ctx != NULL", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
565
566 char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
567
568 if (length == 0)
569 snprintf(n, name_length, "%s[]", array->name);
570 else {
571 /* insert outermost dimensions in the correct spot
572 * otherwise the dimension order will be backwards
573 */
574 const char *pos = strchr(array->name, '[');
575 if (pos) {
576 int idx = pos - array->name;
577 snprintf(n, idx+1, "%s", array->name);
578 snprintf(n + idx, name_length - idx, "[%u]%s",
579 length, array->name + idx);
580 } else {
581 snprintf(n, name_length, "%s[%u]", array->name, length);
582 }
583 }
584
585 this->name = n;
586}
587
588const glsl_type *
589glsl_type::vec(unsigned components, const glsl_type *const ts[])
590{
591 unsigned n = components;
592
593 if (components == 8)
594 n = 5;
595 else if (components == 16)
596 n = 6;
597
598 if (n == 0 || n > 6)
599 return error_type;
600
601 return ts[n - 1];
602}
603
604#define VECN(components, sname, vname)const glsl_type * glsl_type:: vname (unsigned components) { static
const glsl_type *const ts[] = { sname_type, vname2_type, vname3_type
, vname4_type, vname8_type, vname16_type, }; return glsl_type
::vec(components, ts); }
\
605const glsl_type * \
606glsl_type:: vname (unsigned components) \
607{ \
608 static const glsl_type *const ts[] = { \
609 sname ## _type, vname ## 2_type, \
610 vname ## 3_type, vname ## 4_type, \
611 vname ## 8_type, vname ## 16_type, \
612 }; \
613 return glsl_type::vec(components, ts); \
614}
615
616VECN(components, float, vec)const glsl_type * glsl_type:: vec (unsigned components) { static
const glsl_type *const ts[] = { float_type, vec2_type, vec3_type
, vec4_type, vec8_type, vec16_type, }; return glsl_type::vec(
components, ts); }
617VECN(components, float16_t, f16vec)const glsl_type * glsl_type:: f16vec (unsigned components) { static
const glsl_type *const ts[] = { float16_t_type, f16vec2_type
, f16vec3_type, f16vec4_type, f16vec8_type, f16vec16_type, };
return glsl_type::vec(components, ts); }
618VECN(components, double, dvec)const glsl_type * glsl_type:: dvec (unsigned components) { static
const glsl_type *const ts[] = { double_type, dvec2_type, dvec3_type
, dvec4_type, dvec8_type, dvec16_type, }; return glsl_type::vec
(components, ts); }
619VECN(components, int, ivec)const glsl_type * glsl_type:: ivec (unsigned components) { static
const glsl_type *const ts[] = { int_type, ivec2_type, ivec3_type
, ivec4_type, ivec8_type, ivec16_type, }; return glsl_type::vec
(components, ts); }
620VECN(components, uint, uvec)const glsl_type * glsl_type:: uvec (unsigned components) { static
const glsl_type *const ts[] = { uint_type, uvec2_type, uvec3_type
, uvec4_type, uvec8_type, uvec16_type, }; return glsl_type::vec
(components, ts); }
621VECN(components, bool, bvec)const glsl_type * glsl_type:: bvec (unsigned components) { static
const glsl_type *const ts[] = { bool_type, bvec2_type, bvec3_type
, bvec4_type, bvec8_type, bvec16_type, }; return glsl_type::vec
(components, ts); }
622VECN(components, int64_t, i64vec)const glsl_type * glsl_type:: i64vec (unsigned components) { static
const glsl_type *const ts[] = { int64_t_type, i64vec2_type, i64vec3_type
, i64vec4_type, i64vec8_type, i64vec16_type, }; return glsl_type
::vec(components, ts); }
623VECN(components, uint64_t, u64vec)const glsl_type * glsl_type:: u64vec (unsigned components) { static
const glsl_type *const ts[] = { uint64_t_type, u64vec2_type,
u64vec3_type, u64vec4_type, u64vec8_type, u64vec16_type, }; return
glsl_type::vec(components, ts); }
624VECN(components, int16_t, i16vec)const glsl_type * glsl_type:: i16vec (unsigned components) { static
const glsl_type *const ts[] = { int16_t_type, i16vec2_type, i16vec3_type
, i16vec4_type, i16vec8_type, i16vec16_type, }; return glsl_type
::vec(components, ts); }
625VECN(components, uint16_t, u16vec)const glsl_type * glsl_type:: u16vec (unsigned components) { static
const glsl_type *const ts[] = { uint16_t_type, u16vec2_type,
u16vec3_type, u16vec4_type, u16vec8_type, u16vec16_type, }; return
glsl_type::vec(components, ts); }
626VECN(components, int8_t, i8vec)const glsl_type * glsl_type:: i8vec (unsigned components) { static
const glsl_type *const ts[] = { int8_t_type, i8vec2_type, i8vec3_type
, i8vec4_type, i8vec8_type, i8vec16_type, }; return glsl_type
::vec(components, ts); }
627VECN(components, uint8_t, u8vec)const glsl_type * glsl_type:: u8vec (unsigned components) { static
const glsl_type *const ts[] = { uint8_t_type, u8vec2_type, u8vec3_type
, u8vec4_type, u8vec8_type, u8vec16_type, }; return glsl_type
::vec(components, ts); }
628
629const glsl_type *
630glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns,
631 unsigned explicit_stride, bool row_major)
632{
633 if (base_type == GLSL_TYPE_VOID) {
634 assert(explicit_stride == 0 && !row_major)(static_cast <bool> (explicit_stride == 0 && !row_major
) ? void (0) : __assert_fail ("explicit_stride == 0 && !row_major"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
635 return void_type;
636 }
637
638 /* Matrix and vector types with explicit strides have to be looked up in a
639 * table so they're handled separately.
640 */
641 if (explicit_stride > 0) {
642 const glsl_type *bare_type = get_instance(base_type, rows, columns);
643
644 assert(columns > 1 || !row_major)(static_cast <bool> (columns > 1 || !row_major) ? void
(0) : __assert_fail ("columns > 1 || !row_major", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
645
646 char name[128];
647 snprintf(name, sizeof(name), "%sx%uB%s", bare_type->name,
648 explicit_stride, row_major ? "RM" : "");
649
650 mtx_lock(&glsl_type::hash_mutex);
651 assert(glsl_type_users > 0)(static_cast <bool> (glsl_type_users > 0) ? void (0)
: __assert_fail ("glsl_type_users > 0", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
652
653 if (explicit_matrix_types == NULL__null) {
654 explicit_matrix_types =
655 _mesa_hash_table_create(NULL__null, _mesa_hash_string,
656 _mesa_key_string_equal);
657 }
658
659 const struct hash_entry *entry =
660 _mesa_hash_table_search(explicit_matrix_types, name);
661 if (entry == NULL__null) {
662 const glsl_type *t = new glsl_type(bare_type->gl_type,
663 (glsl_base_type)base_type,
664 rows, columns, name,
665 explicit_stride, row_major);
666
667 entry = _mesa_hash_table_insert(explicit_matrix_types,
668 t->name, (void *)t);
669 }
670
671 assert(((glsl_type *) entry->data)->base_type == base_type)(static_cast <bool> (((glsl_type *) entry->data)->
base_type == base_type) ? void (0) : __assert_fail ("((glsl_type *) entry->data)->base_type == base_type"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
672 assert(((glsl_type *) entry->data)->vector_elements == rows)(static_cast <bool> (((glsl_type *) entry->data)->
vector_elements == rows) ? void (0) : __assert_fail ("((glsl_type *) entry->data)->vector_elements == rows"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
673 assert(((glsl_type *) entry->data)->matrix_columns == columns)(static_cast <bool> (((glsl_type *) entry->data)->
matrix_columns == columns) ? void (0) : __assert_fail ("((glsl_type *) entry->data)->matrix_columns == columns"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
674 assert(((glsl_type *) entry->data)->explicit_stride == explicit_stride)(static_cast <bool> (((glsl_type *) entry->data)->
explicit_stride == explicit_stride) ? void (0) : __assert_fail
("((glsl_type *) entry->data)->explicit_stride == explicit_stride"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
675
676 const glsl_type *t = (const glsl_type *) entry->data;
677
678 mtx_unlock(&glsl_type::hash_mutex);
679
680 return t;
681 }
682
683 assert(!row_major)(static_cast <bool> (!row_major) ? void (0) : __assert_fail
("!row_major", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
684
685 /* Treat GLSL vectors as Nx1 matrices.
686 */
687 if (columns == 1) {
688 switch (base_type) {
689 case GLSL_TYPE_UINT:
690 return uvec(rows);
691 case GLSL_TYPE_INT:
692 return ivec(rows);
693 case GLSL_TYPE_FLOAT:
694 return vec(rows);
695 case GLSL_TYPE_FLOAT16:
696 return f16vec(rows);
697 case GLSL_TYPE_DOUBLE:
698 return dvec(rows);
699 case GLSL_TYPE_BOOL:
700 return bvec(rows);
701 case GLSL_TYPE_UINT64:
702 return u64vec(rows);
703 case GLSL_TYPE_INT64:
704 return i64vec(rows);
705 case GLSL_TYPE_UINT16:
706 return u16vec(rows);
707 case GLSL_TYPE_INT16:
708 return i16vec(rows);
709 case GLSL_TYPE_UINT8:
710 return u8vec(rows);
711 case GLSL_TYPE_INT8:
712 return i8vec(rows);
713 default:
714 return error_type;
715 }
716 } else {
717 if ((base_type != GLSL_TYPE_FLOAT &&
718 base_type != GLSL_TYPE_DOUBLE &&
719 base_type != GLSL_TYPE_FLOAT16) || (rows == 1))
720 return error_type;
721
722 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
723 * combinations are valid:
724 *
725 * 1 2 3 4
726 * 1
727 * 2 x x x
728 * 3 x x x
729 * 4 x x x
730 */
731#define IDX(c,r)(((c-1)*3) + (r-1)) (((c-1)*3) + (r-1))
732
733 switch (base_type) {
734 case GLSL_TYPE_DOUBLE: {
735 switch (IDX(columns, rows)(((columns-1)*3) + (rows-1))) {
736 case IDX(2,2)(((2 -1)*3) + (2 -1)): return dmat2_type;
737 case IDX(2,3)(((2 -1)*3) + (3 -1)): return dmat2x3_type;
738 case IDX(2,4)(((2 -1)*3) + (4 -1)): return dmat2x4_type;
739 case IDX(3,2)(((3 -1)*3) + (2 -1)): return dmat3x2_type;
740 case IDX(3,3)(((3 -1)*3) + (3 -1)): return dmat3_type;
741 case IDX(3,4)(((3 -1)*3) + (4 -1)): return dmat3x4_type;
742 case IDX(4,2)(((4 -1)*3) + (2 -1)): return dmat4x2_type;
743 case IDX(4,3)(((4 -1)*3) + (3 -1)): return dmat4x3_type;
744 case IDX(4,4)(((4 -1)*3) + (4 -1)): return dmat4_type;
745 default: return error_type;
746 }
747 }
748 case GLSL_TYPE_FLOAT: {
749 switch (IDX(columns, rows)(((columns-1)*3) + (rows-1))) {
750 case IDX(2,2)(((2 -1)*3) + (2 -1)): return mat2_type;
751 case IDX(2,3)(((2 -1)*3) + (3 -1)): return mat2x3_type;
752 case IDX(2,4)(((2 -1)*3) + (4 -1)): return mat2x4_type;
753 case IDX(3,2)(((3 -1)*3) + (2 -1)): return mat3x2_type;
754 case IDX(3,3)(((3 -1)*3) + (3 -1)): return mat3_type;
755 case IDX(3,4)(((3 -1)*3) + (4 -1)): return mat3x4_type;
756 case IDX(4,2)(((4 -1)*3) + (2 -1)): return mat4x2_type;
757 case IDX(4,3)(((4 -1)*3) + (3 -1)): return mat4x3_type;
758 case IDX(4,4)(((4 -1)*3) + (4 -1)): return mat4_type;
759 default: return error_type;
760 }
761 }
762 case GLSL_TYPE_FLOAT16: {
763 switch (IDX(columns, rows)(((columns-1)*3) + (rows-1))) {
764 case IDX(2,2)(((2 -1)*3) + (2 -1)): return f16mat2_type;
765 case IDX(2,3)(((2 -1)*3) + (3 -1)): return f16mat2x3_type;
766 case IDX(2,4)(((2 -1)*3) + (4 -1)): return f16mat2x4_type;
767 case IDX(3,2)(((3 -1)*3) + (2 -1)): return f16mat3x2_type;
768 case IDX(3,3)(((3 -1)*3) + (3 -1)): return f16mat3_type;
769 case IDX(3,4)(((3 -1)*3) + (4 -1)): return f16mat3x4_type;
770 case IDX(4,2)(((4 -1)*3) + (2 -1)): return f16mat4x2_type;
771 case IDX(4,3)(((4 -1)*3) + (3 -1)): return f16mat4x3_type;
772 case IDX(4,4)(((4 -1)*3) + (4 -1)): return f16mat4_type;
773 default: return error_type;
774 }
775 }
776 default: return error_type;
777 }
778 }
779
780 assert(!"Should not get here.")(static_cast <bool> (!"Should not get here.") ? void (0
) : __assert_fail ("!\"Should not get here.\"", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
781 return error_type;
782}
783
784const glsl_type *
785glsl_type::get_sampler_instance(enum glsl_sampler_dim dim,
786 bool shadow,
787 bool array,
788 glsl_base_type type)
789{
790 switch (type) {
791 case GLSL_TYPE_FLOAT:
792 switch (dim) {
793 case GLSL_SAMPLER_DIM_1D:
794 if (shadow)
795 return (array ? sampler1DArrayShadow_type : sampler1DShadow_type);
796 else
797 return (array ? sampler1DArray_type : sampler1D_type);
798 case GLSL_SAMPLER_DIM_2D:
799 if (shadow)
800 return (array ? sampler2DArrayShadow_type : sampler2DShadow_type);
801 else
802 return (array ? sampler2DArray_type : sampler2D_type);
803 case GLSL_SAMPLER_DIM_3D:
804 if (shadow || array)
805 return error_type;
806 else
807 return sampler3D_type;
808 case GLSL_SAMPLER_DIM_CUBE:
809 if (shadow)
810 return (array ? samplerCubeArrayShadow_type : samplerCubeShadow_type);
811 else
812 return (array ? samplerCubeArray_type : samplerCube_type);
813 case GLSL_SAMPLER_DIM_RECT:
814 if (array)
815 return error_type;
816 if (shadow)
817 return sampler2DRectShadow_type;
818 else
819 return sampler2DRect_type;
820 case GLSL_SAMPLER_DIM_BUF:
821 if (shadow || array)
822 return error_type;
823 else
824 return samplerBuffer_type;
825 case GLSL_SAMPLER_DIM_MS:
826 if (shadow)
827 return error_type;
828 return (array ? sampler2DMSArray_type : sampler2DMS_type);
829 case GLSL_SAMPLER_DIM_EXTERNAL:
830 if (shadow || array)
831 return error_type;
832 else
833 return samplerExternalOES_type;
834 case GLSL_SAMPLER_DIM_SUBPASS:
835 case GLSL_SAMPLER_DIM_SUBPASS_MS:
836 return error_type;
837 }
838 case GLSL_TYPE_INT:
839 if (shadow)
840 return error_type;
841 switch (dim) {
842 case GLSL_SAMPLER_DIM_1D:
843 return (array ? isampler1DArray_type : isampler1D_type);
844 case GLSL_SAMPLER_DIM_2D:
845 return (array ? isampler2DArray_type : isampler2D_type);
846 case GLSL_SAMPLER_DIM_3D:
847 if (array)
848 return error_type;
849 return isampler3D_type;
850 case GLSL_SAMPLER_DIM_CUBE:
851 return (array ? isamplerCubeArray_type : isamplerCube_type);
852 case GLSL_SAMPLER_DIM_RECT:
853 if (array)
854 return error_type;
855 return isampler2DRect_type;
856 case GLSL_SAMPLER_DIM_BUF:
857 if (array)
858 return error_type;
859 return isamplerBuffer_type;
860 case GLSL_SAMPLER_DIM_MS:
861 return (array ? isampler2DMSArray_type : isampler2DMS_type);
862 case GLSL_SAMPLER_DIM_EXTERNAL:
863 return error_type;
864 case GLSL_SAMPLER_DIM_SUBPASS:
865 case GLSL_SAMPLER_DIM_SUBPASS_MS:
866 return error_type;
867 }
868 case GLSL_TYPE_UINT:
869 if (shadow)
870 return error_type;
871 switch (dim) {
872 case GLSL_SAMPLER_DIM_1D:
873 return (array ? usampler1DArray_type : usampler1D_type);
874 case GLSL_SAMPLER_DIM_2D:
875 return (array ? usampler2DArray_type : usampler2D_type);
876 case GLSL_SAMPLER_DIM_3D:
877 if (array)
878 return error_type;
879 return usampler3D_type;
880 case GLSL_SAMPLER_DIM_CUBE:
881 return (array ? usamplerCubeArray_type : usamplerCube_type);
882 case GLSL_SAMPLER_DIM_RECT:
883 if (array)
884 return error_type;
885 return usampler2DRect_type;
886 case GLSL_SAMPLER_DIM_BUF:
887 if (array)
888 return error_type;
889 return usamplerBuffer_type;
890 case GLSL_SAMPLER_DIM_MS:
891 return (array ? usampler2DMSArray_type : usampler2DMS_type);
892 case GLSL_SAMPLER_DIM_EXTERNAL:
893 return error_type;
894 case GLSL_SAMPLER_DIM_SUBPASS:
895 case GLSL_SAMPLER_DIM_SUBPASS_MS:
896 return error_type;
897 }
898 default:
899 return error_type;
900 }
901
902 unreachable("switch statement above should be complete")do { (static_cast <bool> (!"switch statement above should be complete"
) ? void (0) : __assert_fail ("!\"switch statement above should be complete\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
903}
904
905const glsl_type *
906glsl_type::get_image_instance(enum glsl_sampler_dim dim,
907 bool array, glsl_base_type type)
908{
909 switch (type) {
910 case GLSL_TYPE_FLOAT:
911 switch (dim) {
912 case GLSL_SAMPLER_DIM_1D:
913 return (array ? image1DArray_type : image1D_type);
914 case GLSL_SAMPLER_DIM_2D:
915 return (array ? image2DArray_type : image2D_type);
916 case GLSL_SAMPLER_DIM_3D:
917 return image3D_type;
918 case GLSL_SAMPLER_DIM_CUBE:
919 return (array ? imageCubeArray_type : imageCube_type);
920 case GLSL_SAMPLER_DIM_RECT:
921 if (array)
922 return error_type;
923 else
924 return image2DRect_type;
925 case GLSL_SAMPLER_DIM_BUF:
926 if (array)
927 return error_type;
928 else
929 return imageBuffer_type;
930 case GLSL_SAMPLER_DIM_MS:
931 return (array ? image2DMSArray_type : image2DMS_type);
932 case GLSL_SAMPLER_DIM_SUBPASS:
933 return subpassInput_type;
934 case GLSL_SAMPLER_DIM_SUBPASS_MS:
935 return subpassInputMS_type;
936 case GLSL_SAMPLER_DIM_EXTERNAL:
937 return error_type;
938 }
939 case GLSL_TYPE_INT:
940 switch (dim) {
941 case GLSL_SAMPLER_DIM_1D:
942 return (array ? iimage1DArray_type : iimage1D_type);
943 case GLSL_SAMPLER_DIM_2D:
944 return (array ? iimage2DArray_type : iimage2D_type);
945 case GLSL_SAMPLER_DIM_3D:
946 if (array)
947 return error_type;
948 return iimage3D_type;
949 case GLSL_SAMPLER_DIM_CUBE:
950 return (array ? iimageCubeArray_type : iimageCube_type);
951 case GLSL_SAMPLER_DIM_RECT:
952 if (array)
953 return error_type;
954 return iimage2DRect_type;
955 case GLSL_SAMPLER_DIM_BUF:
956 if (array)
957 return error_type;
958 return iimageBuffer_type;
959 case GLSL_SAMPLER_DIM_MS:
960 return (array ? iimage2DMSArray_type : iimage2DMS_type);
961 case GLSL_SAMPLER_DIM_SUBPASS:
962 return isubpassInput_type;
963 case GLSL_SAMPLER_DIM_SUBPASS_MS:
964 return isubpassInputMS_type;
965 case GLSL_SAMPLER_DIM_EXTERNAL:
966 return error_type;
967 }
968 case GLSL_TYPE_UINT:
969 switch (dim) {
970 case GLSL_SAMPLER_DIM_1D:
971 return (array ? uimage1DArray_type : uimage1D_type);
972 case GLSL_SAMPLER_DIM_2D:
973 return (array ? uimage2DArray_type : uimage2D_type);
974 case GLSL_SAMPLER_DIM_3D:
975 if (array)
976 return error_type;
977 return uimage3D_type;
978 case GLSL_SAMPLER_DIM_CUBE:
979 return (array ? uimageCubeArray_type : uimageCube_type);
980 case GLSL_SAMPLER_DIM_RECT:
981 if (array)
982 return error_type;
983 return uimage2DRect_type;
984 case GLSL_SAMPLER_DIM_BUF:
985 if (array)
986 return error_type;
987 return uimageBuffer_type;
988 case GLSL_SAMPLER_DIM_MS:
989 return (array ? uimage2DMSArray_type : uimage2DMS_type);
990 case GLSL_SAMPLER_DIM_SUBPASS:
991 return usubpassInput_type;
992 case GLSL_SAMPLER_DIM_SUBPASS_MS:
993 return usubpassInputMS_type;
994 case GLSL_SAMPLER_DIM_EXTERNAL:
995 return error_type;
996 }
997 default:
998 return error_type;
999 }
1000
1001 unreachable("switch statement above should be complete")do { (static_cast <bool> (!"switch statement above should be complete"
) ? void (0) : __assert_fail ("!\"switch statement above should be complete\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
1002}
1003
1004const glsl_type *
1005glsl_type::get_array_instance(const glsl_type *base,
1006 unsigned array_size,
1007 unsigned explicit_stride)
1008{
1009 /* Generate a name using the base type pointer in the key. This is
1010 * done because the name of the base type may not be unique across
1011 * shaders. For example, two shaders may have different record types
1012 * named 'foo'.
1013 */
1014 char key[128];
1015 snprintf(key, sizeof(key), "%p[%u]x%uB", (void *) base, array_size,
1016 explicit_stride);
1017
1018 mtx_lock(&glsl_type::hash_mutex);
1019 assert(glsl_type_users > 0)(static_cast <bool> (glsl_type_users > 0) ? void (0)
: __assert_fail ("glsl_type_users > 0", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
1020
1021 if (array_types == NULL__null) {
1022 array_types = _mesa_hash_table_create(NULL__null, _mesa_hash_string,
1023 _mesa_key_string_equal);
1024 }
1025
1026 const struct hash_entry *entry = _mesa_hash_table_search(array_types, key);
1027 if (entry == NULL__null) {
1028 const glsl_type *t = new glsl_type(base, array_size, explicit_stride);
1029
1030 entry = _mesa_hash_table_insert(array_types,
1031 strdup(key),
1032 (void *) t);
1033 }
1034
1035 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY)(static_cast <bool> (((glsl_type *) entry->data)->
base_type == GLSL_TYPE_ARRAY) ? void (0) : __assert_fail ("((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1036 assert(((glsl_type *) entry->data)->length == array_size)(static_cast <bool> (((glsl_type *) entry->data)->
length == array_size) ? void (0) : __assert_fail ("((glsl_type *) entry->data)->length == array_size"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1037 assert(((glsl_type *) entry->data)->fields.array == base)(static_cast <bool> (((glsl_type *) entry->data)->
fields.array == base) ? void (0) : __assert_fail ("((glsl_type *) entry->data)->fields.array == base"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1038
1039 glsl_type *t = (glsl_type *) entry->data;
1040
1041 mtx_unlock(&glsl_type::hash_mutex);
1042
1043 return t;
1044}
1045
1046bool
1047glsl_type::compare_no_precision(const glsl_type *b) const
1048{
1049 if (this == b)
1050 return true;
1051
1052 if (this->is_array()) {
1053 if (!b->is_array() || this->length != b->length)
1054 return false;
1055
1056 const glsl_type *b_no_array = b->fields.array;
1057
1058 return this->fields.array->compare_no_precision(b_no_array);
1059 }
1060
1061 if (this->is_struct()) {
1062 if (!b->is_struct())
1063 return false;
1064 } else if (this->is_interface()) {
1065 if (!b->is_interface())
1066 return false;
1067 } else {
1068 return false;
1069 }
1070
1071 return record_compare(b,
1072 true, /* match_name */
1073 true, /* match_locations */
1074 false /* match_precision */);
1075}
1076
1077bool
1078glsl_type::record_compare(const glsl_type *b, bool match_name,
1079 bool match_locations, bool match_precision) const
1080{
1081 if (this->length != b->length)
1082 return false;
1083
1084 if (this->interface_packing != b->interface_packing)
1085 return false;
1086
1087 if (this->interface_row_major != b->interface_row_major)
1088 return false;
1089
1090 /* From the GLSL 4.20 specification (Sec 4.2):
1091 *
1092 * "Structures must have the same name, sequence of type names, and
1093 * type definitions, and field names to be considered the same type."
1094 *
1095 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
1096 *
1097 * Section 7.4.1 (Shader Interface Matching) of the OpenGL 4.30 spec says:
1098 *
1099 * "Variables or block members declared as structures are considered
1100 * to match in type if and only if structure members match in name,
1101 * type, qualification, and declaration order."
1102 */
1103 if (match_name)
1104 if (strcmp(this->name, b->name) != 0)
1105 return false;
1106
1107 for (unsigned i = 0; i < this->length; i++) {
1108 if (match_precision) {
1109 if (this->fields.structure[i].type != b->fields.structure[i].type)
1110 return false;
1111 } else {
1112 const glsl_type *ta = this->fields.structure[i].type;
1113 const glsl_type *tb = b->fields.structure[i].type;
1114 if (!ta->compare_no_precision(tb))
1115 return false;
1116 }
1117 if (strcmp(this->fields.structure[i].name,
1118 b->fields.structure[i].name) != 0)
1119 return false;
1120 if (this->fields.structure[i].matrix_layout
1121 != b->fields.structure[i].matrix_layout)
1122 return false;
1123 if (match_locations && this->fields.structure[i].location
1124 != b->fields.structure[i].location)
1125 return false;
1126 if (this->fields.structure[i].offset
1127 != b->fields.structure[i].offset)
1128 return false;
1129 if (this->fields.structure[i].interpolation
1130 != b->fields.structure[i].interpolation)
1131 return false;
1132 if (this->fields.structure[i].centroid
1133 != b->fields.structure[i].centroid)
1134 return false;
1135 if (this->fields.structure[i].sample
1136 != b->fields.structure[i].sample)
1137 return false;
1138 if (this->fields.structure[i].patch
1139 != b->fields.structure[i].patch)
1140 return false;
1141 if (this->fields.structure[i].memory_read_only
1142 != b->fields.structure[i].memory_read_only)
1143 return false;
1144 if (this->fields.structure[i].memory_write_only
1145 != b->fields.structure[i].memory_write_only)
1146 return false;
1147 if (this->fields.structure[i].memory_coherent
1148 != b->fields.structure[i].memory_coherent)
1149 return false;
1150 if (this->fields.structure[i].memory_volatile
1151 != b->fields.structure[i].memory_volatile)
1152 return false;
1153 if (this->fields.structure[i].memory_restrict
1154 != b->fields.structure[i].memory_restrict)
1155 return false;
1156 if (this->fields.structure[i].image_format
1157 != b->fields.structure[i].image_format)
1158 return false;
1159 if (match_precision &&
1160 this->fields.structure[i].precision
1161 != b->fields.structure[i].precision)
1162 return false;
1163 if (this->fields.structure[i].explicit_xfb_buffer
1164 != b->fields.structure[i].explicit_xfb_buffer)
1165 return false;
1166 if (this->fields.structure[i].xfb_buffer
1167 != b->fields.structure[i].xfb_buffer)
1168 return false;
1169 if (this->fields.structure[i].xfb_stride
1170 != b->fields.structure[i].xfb_stride)
1171 return false;
1172 }
1173
1174 return true;
1175}
1176
1177
1178bool
1179glsl_type::record_key_compare(const void *a, const void *b)
1180{
1181 const glsl_type *const key1 = (glsl_type *) a;
1182 const glsl_type *const key2 = (glsl_type *) b;
1183
1184 return strcmp(key1->name, key2->name) == 0 &&
1185 key1->record_compare(key2, true);
1186}
1187
1188
1189/**
1190 * Generate an integer hash value for a glsl_type structure type.
1191 */
1192unsigned
1193glsl_type::record_key_hash(const void *a)
1194{
1195 const glsl_type *const key = (glsl_type *) a;
1196 uintptr_t hash = key->length;
1197 unsigned retval;
1198
1199 for (unsigned i = 0; i < key->length; i++) {
1200 /* casting pointer to uintptr_t */
1201 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
1202 }
1203
1204 if (sizeof(hash) == 8)
1205 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
1206 else
1207 retval = hash;
1208
1209 return retval;
1210}
1211
1212
1213const glsl_type *
1214glsl_type::get_struct_instance(const glsl_struct_field *fields,
1215 unsigned num_fields,
1216 const char *name,
1217 bool packed)
1218{
1219 const glsl_type key(fields, num_fields, name, packed);
1220
1221 mtx_lock(&glsl_type::hash_mutex);
1222 assert(glsl_type_users > 0)(static_cast <bool> (glsl_type_users > 0) ? void (0)
: __assert_fail ("glsl_type_users > 0", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
1223
1224 if (struct_types == NULL__null) {
1225 struct_types = _mesa_hash_table_create(NULL__null, record_key_hash,
1226 record_key_compare);
1227 }
1228
1229 const struct hash_entry *entry = _mesa_hash_table_search(struct_types,
1230 &key);
1231 if (entry == NULL__null) {
1232 const glsl_type *t = new glsl_type(fields, num_fields, name, packed);
1233
1234 entry = _mesa_hash_table_insert(struct_types, t, (void *) t);
1235 }
1236
1237 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT)(static_cast <bool> (((glsl_type *) entry->data)->
base_type == GLSL_TYPE_STRUCT) ? void (0) : __assert_fail ("((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1238 assert(((glsl_type *) entry->data)->length == num_fields)(static_cast <bool> (((glsl_type *) entry->data)->
length == num_fields) ? void (0) : __assert_fail ("((glsl_type *) entry->data)->length == num_fields"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1239 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0)(static_cast <bool> (strcmp(((glsl_type *) entry->data
)->name, name) == 0) ? void (0) : __assert_fail ("strcmp(((glsl_type *) entry->data)->name, name) == 0"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1240 assert(((glsl_type *) entry->data)->packed == packed)(static_cast <bool> (((glsl_type *) entry->data)->
packed == packed) ? void (0) : __assert_fail ("((glsl_type *) entry->data)->packed == packed"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1241
1242 glsl_type *t = (glsl_type *) entry->data;
1243
1244 mtx_unlock(&glsl_type::hash_mutex);
1245
1246 return t;
1247}
1248
1249
1250const glsl_type *
1251glsl_type::get_interface_instance(const glsl_struct_field *fields,
1252 unsigned num_fields,
1253 enum glsl_interface_packing packing,
1254 bool row_major,
1255 const char *block_name)
1256{
1257 const glsl_type key(fields, num_fields, packing, row_major, block_name);
1258
1259 mtx_lock(&glsl_type::hash_mutex);
1260 assert(glsl_type_users > 0)(static_cast <bool> (glsl_type_users > 0) ? void (0)
: __assert_fail ("glsl_type_users > 0", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
1261
1262 if (interface_types == NULL__null) {
1263 interface_types = _mesa_hash_table_create(NULL__null, record_key_hash,
1264 record_key_compare);
1265 }
1266
1267 const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
1268 &key);
1269 if (entry == NULL__null) {
1270 const glsl_type *t = new glsl_type(fields, num_fields,
1271 packing, row_major, block_name);
1272
1273 entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
1274 }
1275
1276 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE)(static_cast <bool> (((glsl_type *) entry->data)->
base_type == GLSL_TYPE_INTERFACE) ? void (0) : __assert_fail (
"((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1277 assert(((glsl_type *) entry->data)->length == num_fields)(static_cast <bool> (((glsl_type *) entry->data)->
length == num_fields) ? void (0) : __assert_fail ("((glsl_type *) entry->data)->length == num_fields"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1278 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0)(static_cast <bool> (strcmp(((glsl_type *) entry->data
)->name, block_name) == 0) ? void (0) : __assert_fail ("strcmp(((glsl_type *) entry->data)->name, block_name) == 0"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1279
1280 glsl_type *t = (glsl_type *) entry->data;
1281
1282 mtx_unlock(&glsl_type::hash_mutex);
1283
1284 return t;
1285}
1286
1287const glsl_type *
1288glsl_type::get_subroutine_instance(const char *subroutine_name)
1289{
1290 const glsl_type key(subroutine_name);
1291
1292 mtx_lock(&glsl_type::hash_mutex);
1293 assert(glsl_type_users > 0)(static_cast <bool> (glsl_type_users > 0) ? void (0)
: __assert_fail ("glsl_type_users > 0", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
1294
1295 if (subroutine_types == NULL__null) {
1296 subroutine_types = _mesa_hash_table_create(NULL__null, record_key_hash,
1297 record_key_compare);
1298 }
1299
1300 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
1301 &key);
1302 if (entry == NULL__null) {
1303 const glsl_type *t = new glsl_type(subroutine_name);
1304
1305 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
1306 }
1307
1308 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE)(static_cast <bool> (((glsl_type *) entry->data)->
base_type == GLSL_TYPE_SUBROUTINE) ? void (0) : __assert_fail
("((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1309 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0)(static_cast <bool> (strcmp(((glsl_type *) entry->data
)->name, subroutine_name) == 0) ? void (0) : __assert_fail
("strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1310
1311 glsl_type *t = (glsl_type *) entry->data;
1312
1313 mtx_unlock(&glsl_type::hash_mutex);
1314
1315 return t;
1316}
1317
1318
1319static bool
1320function_key_compare(const void *a, const void *b)
1321{
1322 const glsl_type *const key1 = (glsl_type *) a;
1323 const glsl_type *const key2 = (glsl_type *) b;
1324
1325 if (key1->length != key2->length)
1326 return false;
1327
1328 return memcmp(key1->fields.parameters, key2->fields.parameters,
1329 (key1->length + 1) * sizeof(*key1->fields.parameters)) == 0;
1330}
1331
1332
1333static uint32_t
1334function_key_hash(const void *a)
1335{
1336 const glsl_type *const key = (glsl_type *) a;
1337 return _mesa_hash_data(key->fields.parameters,
1338 (key->length + 1) * sizeof(*key->fields.parameters));
1339}
1340
1341const glsl_type *
1342glsl_type::get_function_instance(const glsl_type *return_type,
1343 const glsl_function_param *params,
1344 unsigned num_params)
1345{
1346 const glsl_type key(return_type, params, num_params);
1347
1348 mtx_lock(&glsl_type::hash_mutex);
1349 assert(glsl_type_users > 0)(static_cast <bool> (glsl_type_users > 0) ? void (0)
: __assert_fail ("glsl_type_users > 0", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
1350
1351 if (function_types == NULL__null) {
1352 function_types = _mesa_hash_table_create(NULL__null, function_key_hash,
1353 function_key_compare);
1354 }
1355
1356 struct hash_entry *entry = _mesa_hash_table_search(function_types, &key);
1357 if (entry == NULL__null) {
1358 const glsl_type *t = new glsl_type(return_type, params, num_params);
1359
1360 entry = _mesa_hash_table_insert(function_types, t, (void *) t);
1361 }
1362
1363 const glsl_type *t = (const glsl_type *)entry->data;
1364
1365 assert(t->base_type == GLSL_TYPE_FUNCTION)(static_cast <bool> (t->base_type == GLSL_TYPE_FUNCTION
) ? void (0) : __assert_fail ("t->base_type == GLSL_TYPE_FUNCTION"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1366 assert(t->length == num_params)(static_cast <bool> (t->length == num_params) ? void
(0) : __assert_fail ("t->length == num_params", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
1367
1368 mtx_unlock(&glsl_type::hash_mutex);
1369
1370 return t;
1371}
1372
1373
1374const glsl_type *
1375glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
1376{
1377 if (type_a->is_matrix() && type_b->is_matrix()) {
1378 /* Matrix multiply. The columns of A must match the rows of B. Given
1379 * the other previously tested constraints, this means the vector type
1380 * of a row from A must be the same as the vector type of a column from
1381 * B.
1382 */
1383 if (type_a->row_type() == type_b->column_type()) {
1384 /* The resulting matrix has the number of columns of matrix B and
1385 * the number of rows of matrix A. We get the row count of A by
1386 * looking at the size of a vector that makes up a column. The
1387 * transpose (size of a row) is done for B.
1388 */
1389 const glsl_type *const type =
1390 get_instance(type_a->base_type,
1391 type_a->column_type()->vector_elements,
1392 type_b->row_type()->vector_elements);
1393 assert(type != error_type)(static_cast <bool> (type != error_type) ? void (0) : __assert_fail
("type != error_type", __builtin_FILE (), __builtin_LINE (),
__extension__ __PRETTY_FUNCTION__))
;
1394
1395 return type;
1396 }
1397 } else if (type_a == type_b) {
1398 return type_a;
1399 } else if (type_a->is_matrix()) {
1400 /* A is a matrix and B is a column vector. Columns of A must match
1401 * rows of B. Given the other previously tested constraints, this
1402 * means the vector type of a row from A must be the same as the
1403 * vector the type of B.
1404 */
1405 if (type_a->row_type() == type_b) {
1406 /* The resulting vector has a number of elements equal to
1407 * the number of rows of matrix A. */
1408 const glsl_type *const type =
1409 get_instance(type_a->base_type,
1410 type_a->column_type()->vector_elements,
1411 1);
1412 assert(type != error_type)(static_cast <bool> (type != error_type) ? void (0) : __assert_fail
("type != error_type", __builtin_FILE (), __builtin_LINE (),
__extension__ __PRETTY_FUNCTION__))
;
1413
1414 return type;
1415 }
1416 } else {
1417 assert(type_b->is_matrix())(static_cast <bool> (type_b->is_matrix()) ? void (0)
: __assert_fail ("type_b->is_matrix()", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
1418
1419 /* A is a row vector and B is a matrix. Columns of A must match rows
1420 * of B. Given the other previously tested constraints, this means
1421 * the type of A must be the same as the vector type of a column from
1422 * B.
1423 */
1424 if (type_a == type_b->column_type()) {
1425 /* The resulting vector has a number of elements equal to
1426 * the number of columns of matrix B. */
1427 const glsl_type *const type =
1428 get_instance(type_a->base_type,
1429 type_b->row_type()->vector_elements,
1430 1);
1431 assert(type != error_type)(static_cast <bool> (type != error_type) ? void (0) : __assert_fail
("type != error_type", __builtin_FILE (), __builtin_LINE (),
__extension__ __PRETTY_FUNCTION__))
;
1432
1433 return type;
1434 }
1435 }
1436
1437 return error_type;
1438}
1439
1440
1441const glsl_type *
1442glsl_type::field_type(const char *name) const
1443{
1444 if (this->base_type != GLSL_TYPE_STRUCT
1445 && this->base_type != GLSL_TYPE_INTERFACE)
1446 return error_type;
1447
1448 for (unsigned i = 0; i < this->length; i++) {
1449 if (strcmp(name, this->fields.structure[i].name) == 0)
1450 return this->fields.structure[i].type;
1451 }
1452
1453 return error_type;
1454}
1455
1456
1457int
1458glsl_type::field_index(const char *name) const
1459{
1460 if (this->base_type != GLSL_TYPE_STRUCT
1461 && this->base_type != GLSL_TYPE_INTERFACE)
1462 return -1;
1463
1464 for (unsigned i = 0; i < this->length; i++) {
1465 if (strcmp(name, this->fields.structure[i].name) == 0)
1466 return i;
1467 }
1468
1469 return -1;
1470}
1471
1472
1473unsigned
1474glsl_type::component_slots() const
1475{
1476 switch (this->base_type) {
1477 case GLSL_TYPE_UINT:
1478 case GLSL_TYPE_INT:
1479 case GLSL_TYPE_UINT8:
1480 case GLSL_TYPE_INT8:
1481 case GLSL_TYPE_UINT16:
1482 case GLSL_TYPE_INT16:
1483 case GLSL_TYPE_FLOAT:
1484 case GLSL_TYPE_FLOAT16:
1485 case GLSL_TYPE_BOOL:
1486 return this->components();
1487
1488 case GLSL_TYPE_DOUBLE:
1489 case GLSL_TYPE_UINT64:
1490 case GLSL_TYPE_INT64:
1491 return 2 * this->components();
1492
1493 case GLSL_TYPE_STRUCT:
1494 case GLSL_TYPE_INTERFACE: {
1495 unsigned size = 0;
1496
1497 for (unsigned i = 0; i < this->length; i++)
1498 size += this->fields.structure[i].type->component_slots();
1499
1500 return size;
1501 }
1502
1503 case GLSL_TYPE_ARRAY:
1504 return this->length * this->fields.array->component_slots();
1505
1506 case GLSL_TYPE_SAMPLER:
1507 case GLSL_TYPE_IMAGE:
1508 return 2;
1509
1510 case GLSL_TYPE_SUBROUTINE:
1511 return 1;
1512
1513 case GLSL_TYPE_FUNCTION:
1514 case GLSL_TYPE_ATOMIC_UINT:
1515 case GLSL_TYPE_VOID:
1516 case GLSL_TYPE_ERROR:
1517 break;
1518 }
1519
1520 return 0;
1521}
1522
1523unsigned
1524glsl_type::struct_location_offset(unsigned length) const
1525{
1526 unsigned offset = 0;
1527 const glsl_type *t = this->without_array();
1528 if (t->is_struct()) {
1529 assert(length <= t->length)(static_cast <bool> (length <= t->length) ? void (
0) : __assert_fail ("length <= t->length", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
1530
1531 for (unsigned i = 0; i < length; i++) {
1532 const glsl_type *st = t->fields.structure[i].type;
1533 const glsl_type *wa = st->without_array();
1534 if (wa->is_struct()) {
1535 unsigned r_offset = wa->struct_location_offset(wa->length);
1536 offset += st->is_array() ?
1537 st->arrays_of_arrays_size() * r_offset : r_offset;
1538 } else if (st->is_array() && st->fields.array->is_array()) {
1539 unsigned outer_array_size = st->length;
1540 const glsl_type *base_type = st->fields.array;
1541
1542 /* For arrays of arrays the outer arrays take up a uniform
1543 * slot for each element. The innermost array elements share a
1544 * single slot so we ignore the innermost array when calculating
1545 * the offset.
1546 */
1547 while (base_type->fields.array->is_array()) {
1548 outer_array_size = outer_array_size * base_type->length;
1549 base_type = base_type->fields.array;
1550 }
1551 offset += outer_array_size;
1552 } else {
1553 /* We dont worry about arrays here because unless the array
1554 * contains a structure or another array it only takes up a single
1555 * uniform slot.
1556 */
1557 offset += 1;
1558 }
1559 }
1560 }
1561 return offset;
1562}
1563
1564unsigned
1565glsl_type::uniform_locations() const
1566{
1567 unsigned size = 0;
1568
1569 switch (this->base_type) {
1570 case GLSL_TYPE_UINT:
1571 case GLSL_TYPE_INT:
1572 case GLSL_TYPE_FLOAT:
1573 case GLSL_TYPE_FLOAT16:
1574 case GLSL_TYPE_DOUBLE:
1575 case GLSL_TYPE_UINT16:
1576 case GLSL_TYPE_UINT8:
1577 case GLSL_TYPE_INT16:
1578 case GLSL_TYPE_INT8:
1579 case GLSL_TYPE_UINT64:
1580 case GLSL_TYPE_INT64:
1581 case GLSL_TYPE_BOOL:
1582 case GLSL_TYPE_SAMPLER:
1583 case GLSL_TYPE_IMAGE:
1584 case GLSL_TYPE_SUBROUTINE:
1585 return 1;
1586
1587 case GLSL_TYPE_STRUCT:
1588 case GLSL_TYPE_INTERFACE:
1589 for (unsigned i = 0; i < this->length; i++)
1590 size += this->fields.structure[i].type->uniform_locations();
1591 return size;
1592 case GLSL_TYPE_ARRAY:
1593 return this->length * this->fields.array->uniform_locations();
1594 default:
1595 return 0;
1596 }
1597}
1598
1599unsigned
1600glsl_type::varying_count() const
1601{
1602 unsigned size = 0;
1603
1604 switch (this->base_type) {
1605 case GLSL_TYPE_UINT:
1606 case GLSL_TYPE_INT:
1607 case GLSL_TYPE_FLOAT:
1608 case GLSL_TYPE_FLOAT16:
1609 case GLSL_TYPE_DOUBLE:
1610 case GLSL_TYPE_BOOL:
1611 case GLSL_TYPE_UINT16:
1612 case GLSL_TYPE_UINT8:
1613 case GLSL_TYPE_INT16:
1614 case GLSL_TYPE_INT8:
1615 case GLSL_TYPE_UINT64:
1616 case GLSL_TYPE_INT64:
1617 return 1;
1618
1619 case GLSL_TYPE_STRUCT:
1620 case GLSL_TYPE_INTERFACE:
1621 for (unsigned i = 0; i < this->length; i++)
1622 size += this->fields.structure[i].type->varying_count();
1623 return size;
1624 case GLSL_TYPE_ARRAY:
1625 /* Don't count innermost array elements */
1626 if (this->without_array()->is_struct() ||
1627 this->without_array()->is_interface() ||
1628 this->fields.array->is_array())
1629 return this->length * this->fields.array->varying_count();
1630 else
1631 return this->fields.array->varying_count();
1632 default:
1633 assert(!"unsupported varying type")(static_cast <bool> (!"unsupported varying type") ? void
(0) : __assert_fail ("!\"unsupported varying type\"", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
1634 return 0;
1635 }
1636}
1637
1638bool
1639glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1640 _mesa_glsl_parse_state *state) const
1641{
1642 if (this == desired)
1643 return true;
1644
1645 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1646 * state, we're doing intra-stage function linking where these checks have
1647 * already been done.
1648 */
1649 if (state && !state->has_implicit_conversions())
1650 return false;
1651
1652 /* There is no conversion among matrix types. */
1653 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1654 return false;
1655
1656 /* Vector size must match. */
1657 if (this->vector_elements != desired->vector_elements)
1658 return false;
1659
1660 /* int and uint can be converted to float. */
1661 if (desired->is_float() && this->is_integer_32())
1662 return true;
1663
1664 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1665 * can be converted to uint. Note that state may be NULL here, when
1666 * resolving function calls in the linker. By this time, all the
1667 * state-dependent checks have already happened though, so allow anything
1668 * that's allowed in any shader version.
1669 */
1670 if ((!state || state->has_implicit_uint_to_int_conversion()) &&
1671 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1672 return true;
1673
1674 /* No implicit conversions from double. */
1675 if ((!state || state->has_double()) && this->is_double())
1676 return false;
1677
1678 /* Conversions from different types to double. */
1679 if ((!state || state->has_double()) && desired->is_double()) {
1680 if (this->is_float())
1681 return true;
1682 if (this->is_integer_32())
1683 return true;
1684 }
1685
1686 return false;
1687}
1688
1689unsigned
1690glsl_type::std140_base_alignment(bool row_major) const
1691{
1692 unsigned N = is_64bit() ? 8 : 4;
1693
1694 /* (1) If the member is a scalar consuming <N> basic machine units, the
1695 * base alignment is <N>.
1696 *
1697 * (2) If the member is a two- or four-component vector with components
1698 * consuming <N> basic machine units, the base alignment is 2<N> or
1699 * 4<N>, respectively.
1700 *
1701 * (3) If the member is a three-component vector with components consuming
1702 * <N> basic machine units, the base alignment is 4<N>.
1703 */
1704 if (this->is_scalar() || this->is_vector()) {
1705 switch (this->vector_elements) {
1706 case 1:
1707 return N;
1708 case 2:
1709 return 2 * N;
1710 case 3:
1711 case 4:
1712 return 4 * N;
1713 }
1714 }
1715
1716 /* (4) If the member is an array of scalars or vectors, the base alignment
1717 * and array stride are set to match the base alignment of a single
1718 * array element, according to rules (1), (2), and (3), and rounded up
1719 * to the base alignment of a vec4. The array may have padding at the
1720 * end; the base offset of the member following the array is rounded up
1721 * to the next multiple of the base alignment.
1722 *
1723 * (6) If the member is an array of <S> column-major matrices with <C>
1724 * columns and <R> rows, the matrix is stored identically to a row of
1725 * <S>*<C> column vectors with <R> components each, according to rule
1726 * (4).
1727 *
1728 * (8) If the member is an array of <S> row-major matrices with <C> columns
1729 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1730 * row vectors with <C> components each, according to rule (4).
1731 *
1732 * (10) If the member is an array of <S> structures, the <S> elements of
1733 * the array are laid out in order, according to rule (9).
1734 */
1735 if (this->is_array()) {
1736 if (this->fields.array->is_scalar() ||
1737 this->fields.array->is_vector() ||
1738 this->fields.array->is_matrix()) {
1739 return MAX2(this->fields.array->std140_base_alignment(row_major), 16)( (this->fields.array->std140_base_alignment(row_major)
)>(16) ? (this->fields.array->std140_base_alignment(
row_major)) : (16) )
;
1740 } else {
1741 assert(this->fields.array->is_struct() ||(static_cast <bool> (this->fields.array->is_struct
() || this->fields.array->is_array()) ? void (0) : __assert_fail
("this->fields.array->is_struct() || this->fields.array->is_array()"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
1742 this->fields.array->is_array())(static_cast <bool> (this->fields.array->is_struct
() || this->fields.array->is_array()) ? void (0) : __assert_fail
("this->fields.array->is_struct() || this->fields.array->is_array()"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1743 return this->fields.array->std140_base_alignment(row_major);
1744 }
1745 }
1746
1747 /* (5) If the member is a column-major matrix with <C> columns and
1748 * <R> rows, the matrix is stored identically to an array of
1749 * <C> column vectors with <R> components each, according to
1750 * rule (4).
1751 *
1752 * (7) If the member is a row-major matrix with <C> columns and <R>
1753 * rows, the matrix is stored identically to an array of <R>
1754 * row vectors with <C> components each, according to rule (4).
1755 */
1756 if (this->is_matrix()) {
1757 const struct glsl_type *vec_type, *array_type;
1758 int c = this->matrix_columns;
1759 int r = this->vector_elements;
1760
1761 if (row_major) {
1762 vec_type = get_instance(base_type, c, 1);
1763 array_type = glsl_type::get_array_instance(vec_type, r);
1764 } else {
1765 vec_type = get_instance(base_type, r, 1);
1766 array_type = glsl_type::get_array_instance(vec_type, c);
1767 }
1768
1769 return array_type->std140_base_alignment(false);
1770 }
1771
1772 /* (9) If the member is a structure, the base alignment of the
1773 * structure is <N>, where <N> is the largest base alignment
1774 * value of any of its members, and rounded up to the base
1775 * alignment of a vec4. The individual members of this
1776 * sub-structure are then assigned offsets by applying this set
1777 * of rules recursively, where the base offset of the first
1778 * member of the sub-structure is equal to the aligned offset
1779 * of the structure. The structure may have padding at the end;
1780 * the base offset of the member following the sub-structure is
1781 * rounded up to the next multiple of the base alignment of the
1782 * structure.
1783 */
1784 if (this->is_struct()) {
1785 unsigned base_alignment = 16;
1786 for (unsigned i = 0; i < this->length; i++) {
1787 bool field_row_major = row_major;
1788 const enum glsl_matrix_layout matrix_layout =
1789 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1790 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1791 field_row_major = true;
1792 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1793 field_row_major = false;
1794 }
1795
1796 const struct glsl_type *field_type = this->fields.structure[i].type;
1797 base_alignment = MAX2(base_alignment,( (base_alignment)>(field_type->std140_base_alignment(field_row_major
)) ? (base_alignment) : (field_type->std140_base_alignment
(field_row_major)) )
1798 field_type->std140_base_alignment(field_row_major))( (base_alignment)>(field_type->std140_base_alignment(field_row_major
)) ? (base_alignment) : (field_type->std140_base_alignment
(field_row_major)) )
;
1799 }
1800 return base_alignment;
1801 }
1802
1803 assert(!"not reached")(static_cast <bool> (!"not reached") ? void (0) : __assert_fail
("!\"not reached\"", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
1804 return -1;
1805}
1806
1807unsigned
1808glsl_type::std140_size(bool row_major) const
1809{
1810 unsigned N = is_64bit() ? 8 : 4;
1811
1812 /* (1) If the member is a scalar consuming <N> basic machine units, the
1813 * base alignment is <N>.
1814 *
1815 * (2) If the member is a two- or four-component vector with components
1816 * consuming <N> basic machine units, the base alignment is 2<N> or
1817 * 4<N>, respectively.
1818 *
1819 * (3) If the member is a three-component vector with components consuming
1820 * <N> basic machine units, the base alignment is 4<N>.
1821 */
1822 if (this->is_scalar() || this->is_vector()) {
1823 assert(this->explicit_stride == 0)(static_cast <bool> (this->explicit_stride == 0) ? void
(0) : __assert_fail ("this->explicit_stride == 0", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
1824 return this->vector_elements * N;
1825 }
1826
1827 /* (5) If the member is a column-major matrix with <C> columns and
1828 * <R> rows, the matrix is stored identically to an array of
1829 * <C> column vectors with <R> components each, according to
1830 * rule (4).
1831 *
1832 * (6) If the member is an array of <S> column-major matrices with <C>
1833 * columns and <R> rows, the matrix is stored identically to a row of
1834 * <S>*<C> column vectors with <R> components each, according to rule
1835 * (4).
1836 *
1837 * (7) If the member is a row-major matrix with <C> columns and <R>
1838 * rows, the matrix is stored identically to an array of <R>
1839 * row vectors with <C> components each, according to rule (4).
1840 *
1841 * (8) If the member is an array of <S> row-major matrices with <C> columns
1842 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1843 * row vectors with <C> components each, according to rule (4).
1844 */
1845 if (this->without_array()->is_matrix()) {
1846 const struct glsl_type *element_type;
1847 const struct glsl_type *vec_type;
1848 unsigned int array_len;
1849
1850 if (this->is_array()) {
1851 element_type = this->without_array();
1852 array_len = this->arrays_of_arrays_size();
1853 } else {
1854 element_type = this;
1855 array_len = 1;
1856 }
1857
1858 if (row_major) {
1859 vec_type = get_instance(element_type->base_type,
1860 element_type->matrix_columns, 1);
1861
1862 array_len *= element_type->vector_elements;
1863 } else {
1864 vec_type = get_instance(element_type->base_type,
1865 element_type->vector_elements, 1);
1866 array_len *= element_type->matrix_columns;
1867 }
1868 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1869 array_len);
1870
1871 return array_type->std140_size(false);
1872 }
1873
1874 /* (4) If the member is an array of scalars or vectors, the base alignment
1875 * and array stride are set to match the base alignment of a single
1876 * array element, according to rules (1), (2), and (3), and rounded up
1877 * to the base alignment of a vec4. The array may have padding at the
1878 * end; the base offset of the member following the array is rounded up
1879 * to the next multiple of the base alignment.
1880 *
1881 * (10) If the member is an array of <S> structures, the <S> elements of
1882 * the array are laid out in order, according to rule (9).
1883 */
1884 if (this->is_array()) {
1885 unsigned stride;
1886 if (this->without_array()->is_struct()) {
1887 stride = this->without_array()->std140_size(row_major);
1888 } else {
1889 unsigned element_base_align =
1890 this->without_array()->std140_base_alignment(row_major);
1891 stride = MAX2(element_base_align, 16)( (element_base_align)>(16) ? (element_base_align) : (16) );
1892 }
1893
1894 unsigned size = this->arrays_of_arrays_size() * stride;
1895 assert(this->explicit_stride == 0 ||(static_cast <bool> (this->explicit_stride == 0 || size
== this->length * this->explicit_stride) ? void (0) : __assert_fail
("this->explicit_stride == 0 || size == this->length * this->explicit_stride"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
1896 size == this->length * this->explicit_stride)(static_cast <bool> (this->explicit_stride == 0 || size
== this->length * this->explicit_stride) ? void (0) : __assert_fail
("this->explicit_stride == 0 || size == this->length * this->explicit_stride"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1897 return size;
1898 }
1899
1900 /* (9) If the member is a structure, the base alignment of the
1901 * structure is <N>, where <N> is the largest base alignment
1902 * value of any of its members, and rounded up to the base
1903 * alignment of a vec4. The individual members of this
1904 * sub-structure are then assigned offsets by applying this set
1905 * of rules recursively, where the base offset of the first
1906 * member of the sub-structure is equal to the aligned offset
1907 * of the structure. The structure may have padding at the end;
1908 * the base offset of the member following the sub-structure is
1909 * rounded up to the next multiple of the base alignment of the
1910 * structure.
1911 */
1912 if (this->is_struct() || this->is_interface()) {
1913 unsigned size = 0;
1914 unsigned max_align = 0;
1915
1916 for (unsigned i = 0; i < this->length; i++) {
1917 bool field_row_major = row_major;
1918 const enum glsl_matrix_layout matrix_layout =
1919 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1920 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1921 field_row_major = true;
1922 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1923 field_row_major = false;
1924 }
1925
1926 const struct glsl_type *field_type = this->fields.structure[i].type;
1927 unsigned align = field_type->std140_base_alignment(field_row_major);
1928
1929 /* Ignore unsized arrays when calculating size */
1930 if (field_type->is_unsized_array())
1931 continue;
1932
1933 size = glsl_align(size, align);
1934 size += field_type->std140_size(field_row_major);
1935
1936 max_align = MAX2(align, max_align)( (align)>(max_align) ? (align) : (max_align) );
1937
1938 if (field_type->is_struct() && (i + 1 < this->length))
1939 size = glsl_align(size, 16);
1940 }
1941 size = glsl_align(size, MAX2(max_align, 16)( (max_align)>(16) ? (max_align) : (16) ));
1942 return size;
1943 }
1944
1945 assert(!"not reached")(static_cast <bool> (!"not reached") ? void (0) : __assert_fail
("!\"not reached\"", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
1946 return -1;
1947}
1948
1949const glsl_type *
1950glsl_type::get_explicit_std140_type(bool row_major) const
1951{
1952 if (this->is_vector() || this->is_scalar()) {
1953 return this;
1954 } else if (this->is_matrix()) {
1955 const glsl_type *vec_type;
1956 if (row_major)
1957 vec_type = get_instance(this->base_type, this->matrix_columns, 1);
1958 else
1959 vec_type = get_instance(this->base_type, this->vector_elements, 1);
1960 unsigned elem_size = vec_type->std140_size(false);
1961 unsigned stride = glsl_align(elem_size, 16);
1962 return get_instance(this->base_type, this->vector_elements,
1963 this->matrix_columns, stride, row_major);
1964 } else if (this->is_array()) {
1965 unsigned elem_size = this->fields.array->std140_size(row_major);
1966 const glsl_type *elem_type =
1967 this->fields.array->get_explicit_std140_type(row_major);
1968 unsigned stride = glsl_align(elem_size, 16);
1969 return get_array_instance(elem_type, this->length, stride);
1970 } else if (this->is_struct() || this->is_interface()) {
1971 glsl_struct_field *fields = new glsl_struct_field[this->length];
1972 unsigned offset = 0;
1973 for (unsigned i = 0; i < length; i++) {
1974 fields[i] = this->fields.structure[i];
1975
1976 bool field_row_major = row_major;
1977 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1978 field_row_major = false;
1979 } else if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1980 field_row_major = true;
1981 }
1982 fields[i].type =
1983 fields[i].type->get_explicit_std140_type(field_row_major);
1984
1985 unsigned fsize = fields[i].type->std140_size(field_row_major);
1986 unsigned falign = fields[i].type->std140_base_alignment(field_row_major);
1987 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
1988 * Layout Qualifiers":
1989 *
1990 * "The actual offset of a member is computed as follows: If
1991 * offset was declared, start with that offset, otherwise start
1992 * with the next available offset. If the resulting offset is not
1993 * a multiple of the actual alignment, increase it to the first
1994 * offset that is a multiple of the actual alignment. This results
1995 * in the actual offset the member will have."
1996 */
1997 if (fields[i].offset >= 0) {
1998 assert((unsigned)fields[i].offset >= offset)(static_cast <bool> ((unsigned)fields[i].offset >= offset
) ? void (0) : __assert_fail ("(unsigned)fields[i].offset >= offset"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1999 offset = fields[i].offset;
2000 }
2001 offset = glsl_align(offset, falign);
2002 fields[i].offset = offset;
2003 offset += fsize;
2004 }
2005
2006 const glsl_type *type;
2007 if (this->is_struct())
2008 type = get_struct_instance(fields, this->length, this->name);
2009 else
2010 type = get_interface_instance(fields, this->length,
2011 (enum glsl_interface_packing)this->interface_packing,
2012 this->interface_row_major,
2013 this->name);
2014
2015 delete[] fields;
2016 return type;
2017 } else {
2018 unreachable("Invalid type for UBO or SSBO")do { (static_cast <bool> (!"Invalid type for UBO or SSBO"
) ? void (0) : __assert_fail ("!\"Invalid type for UBO or SSBO\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
2019 }
2020}
2021
2022unsigned
2023glsl_type::std430_base_alignment(bool row_major) const
2024{
2025
2026 unsigned N = is_64bit() ? 8 : 4;
2027
2028 /* (1) If the member is a scalar consuming <N> basic machine units, the
2029 * base alignment is <N>.
2030 *
2031 * (2) If the member is a two- or four-component vector with components
2032 * consuming <N> basic machine units, the base alignment is 2<N> or
2033 * 4<N>, respectively.
2034 *
2035 * (3) If the member is a three-component vector with components consuming
2036 * <N> basic machine units, the base alignment is 4<N>.
2037 */
2038 if (this->is_scalar() || this->is_vector()) {
2039 switch (this->vector_elements) {
2040 case 1:
2041 return N;
2042 case 2:
2043 return 2 * N;
2044 case 3:
2045 case 4:
2046 return 4 * N;
2047 }
2048 }
2049
2050 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2051 *
2052 * "When using the std430 storage layout, shader storage blocks will be
2053 * laid out in buffer storage identically to uniform and shader storage
2054 * blocks using the std140 layout, except that the base alignment and
2055 * stride of arrays of scalars and vectors in rule 4 and of structures
2056 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2057 */
2058
2059 /* (1) If the member is a scalar consuming <N> basic machine units, the
2060 * base alignment is <N>.
2061 *
2062 * (2) If the member is a two- or four-component vector with components
2063 * consuming <N> basic machine units, the base alignment is 2<N> or
2064 * 4<N>, respectively.
2065 *
2066 * (3) If the member is a three-component vector with components consuming
2067 * <N> basic machine units, the base alignment is 4<N>.
2068 */
2069 if (this->is_array())
2070 return this->fields.array->std430_base_alignment(row_major);
2071
2072 /* (5) If the member is a column-major matrix with <C> columns and
2073 * <R> rows, the matrix is stored identically to an array of
2074 * <C> column vectors with <R> components each, according to
2075 * rule (4).
2076 *
2077 * (7) If the member is a row-major matrix with <C> columns and <R>
2078 * rows, the matrix is stored identically to an array of <R>
2079 * row vectors with <C> components each, according to rule (4).
2080 */
2081 if (this->is_matrix()) {
2082 const struct glsl_type *vec_type, *array_type;
2083 int c = this->matrix_columns;
2084 int r = this->vector_elements;
2085
2086 if (row_major) {
2087 vec_type = get_instance(base_type, c, 1);
2088 array_type = glsl_type::get_array_instance(vec_type, r);
2089 } else {
2090 vec_type = get_instance(base_type, r, 1);
2091 array_type = glsl_type::get_array_instance(vec_type, c);
2092 }
2093
2094 return array_type->std430_base_alignment(false);
2095 }
2096
2097 /* (9) If the member is a structure, the base alignment of the
2098 * structure is <N>, where <N> is the largest base alignment
2099 * value of any of its members, and rounded up to the base
2100 * alignment of a vec4. The individual members of this
2101 * sub-structure are then assigned offsets by applying this set
2102 * of rules recursively, where the base offset of the first
2103 * member of the sub-structure is equal to the aligned offset
2104 * of the structure. The structure may have padding at the end;
2105 * the base offset of the member following the sub-structure is
2106 * rounded up to the next multiple of the base alignment of the
2107 * structure.
2108 */
2109 if (this->is_struct()) {
2110 unsigned base_alignment = 0;
2111 for (unsigned i = 0; i < this->length; i++) {
2112 bool field_row_major = row_major;
2113 const enum glsl_matrix_layout matrix_layout =
2114 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
2115 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2116 field_row_major = true;
2117 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2118 field_row_major = false;
2119 }
2120
2121 const struct glsl_type *field_type = this->fields.structure[i].type;
2122 base_alignment = MAX2(base_alignment,( (base_alignment)>(field_type->std430_base_alignment(field_row_major
)) ? (base_alignment) : (field_type->std430_base_alignment
(field_row_major)) )
2123 field_type->std430_base_alignment(field_row_major))( (base_alignment)>(field_type->std430_base_alignment(field_row_major
)) ? (base_alignment) : (field_type->std430_base_alignment
(field_row_major)) )
;
2124 }
2125 assert(base_alignment > 0)(static_cast <bool> (base_alignment > 0) ? void (0) :
__assert_fail ("base_alignment > 0", __builtin_FILE (), __builtin_LINE
(), __extension__ __PRETTY_FUNCTION__))
;
2126 return base_alignment;
2127 }
2128 assert(!"not reached")(static_cast <bool> (!"not reached") ? void (0) : __assert_fail
("!\"not reached\"", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
2129 return -1;
2130}
2131
2132unsigned
2133glsl_type::std430_array_stride(bool row_major) const
2134{
2135 unsigned N = is_64bit() ? 8 : 4;
2136
2137 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
2138 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
2139 *
2140 * (3) If the member is a three-component vector with components consuming
2141 * <N> basic machine units, the base alignment is 4<N>.
2142 */
2143 if (this->is_vector() && this->vector_elements == 3)
2144 return 4 * N;
2145
2146 /* By default use std430_size(row_major) */
2147 unsigned stride = this->std430_size(row_major);
2148 assert(this->explicit_stride == 0 || this->explicit_stride == stride)(static_cast <bool> (this->explicit_stride == 0 || this
->explicit_stride == stride) ? void (0) : __assert_fail ("this->explicit_stride == 0 || this->explicit_stride == stride"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2149 return stride;
2150}
2151
2152/* Note that the value returned by this method is only correct if the
2153 * explit offset, and stride values are set, so only with SPIR-V shaders.
2154 * Should not be used with GLSL shaders.
2155 */
2156
2157unsigned
2158glsl_type::explicit_size(bool align_to_stride) const
2159{
2160 if (this->is_struct() || this->is_interface()) {
2161 if (this->length > 0) {
2162 unsigned size = 0;
2163
2164 for (unsigned i = 0; i < this->length; i++) {
2165 assert(this->fields.structure[i].offset >= 0)(static_cast <bool> (this->fields.structure[i].offset
>= 0) ? void (0) : __assert_fail ("this->fields.structure[i].offset >= 0"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2166 unsigned last_byte = this->fields.structure[i].offset +
2167 this->fields.structure[i].type->explicit_size();
2168 size = MAX2(size, last_byte)( (size)>(last_byte) ? (size) : (last_byte) );
2169 }
2170
2171 return size;
2172 } else {
2173 return 0;
2174 }
2175 } else if (this->is_array()) {
2176 /* From ARB_program_interface_query spec:
2177 *
2178 * "For the property of BUFFER_DATA_SIZE, then the implementation-dependent
2179 * minimum total buffer object size, in basic machine units, required to
2180 * hold all active variables associated with an active uniform block, shader
2181 * storage block, or atomic counter buffer is written to <params>. If the
2182 * final member of an active shader storage block is array with no declared
2183 * size, the minimum buffer size is computed assuming the array was declared
2184 * as an array with one element."
2185 *
2186 */
2187 if (this->is_unsized_array())
2188 return this->explicit_stride;
2189
2190 assert(this->length > 0)(static_cast <bool> (this->length > 0) ? void (0)
: __assert_fail ("this->length > 0", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
2191 unsigned elem_size = align_to_stride ? this->explicit_stride : this->fields.array->explicit_size();
2192 assert(this->explicit_stride >= elem_size)(static_cast <bool> (this->explicit_stride >= elem_size
) ? void (0) : __assert_fail ("this->explicit_stride >= elem_size"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2193
2194 return this->explicit_stride * (this->length - 1) + elem_size;
2195 } else if (this->is_matrix()) {
2196 const struct glsl_type *elem_type;
2197 unsigned length;
2198
2199 if (this->interface_row_major) {
2200 elem_type = get_instance(this->base_type,
2201 this->matrix_columns, 1);
2202 length = this->vector_elements;
2203 } else {
2204 elem_type = get_instance(this->base_type,
2205 this->vector_elements, 1);
2206 length = this->matrix_columns;
2207 }
2208
2209 unsigned elem_size = align_to_stride ? this->explicit_stride : elem_type->explicit_size();
2210
2211 assert(this->explicit_stride)(static_cast <bool> (this->explicit_stride) ? void (
0) : __assert_fail ("this->explicit_stride", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
2212 return this->explicit_stride * (length - 1) + elem_size;
2213 }
2214
2215 unsigned N = this->bit_size() / 8;
2216
2217 return this->vector_elements * N;
2218}
2219
2220unsigned
2221glsl_type::std430_size(bool row_major) const
2222{
2223 unsigned N = is_64bit() ? 8 : 4;
21
'?' condition is false
2224
2225 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2226 *
2227 * "When using the std430 storage layout, shader storage blocks will be
2228 * laid out in buffer storage identically to uniform and shader storage
2229 * blocks using the std140 layout, except that the base alignment and
2230 * stride of arrays of scalars and vectors in rule 4 and of structures
2231 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2232 */
2233 if (this->is_scalar() || this->is_vector()) {
22
Assuming the condition is false
23
Assuming the condition is false
24
Taking false branch
2234 assert(this->explicit_stride == 0)(static_cast <bool> (this->explicit_stride == 0) ? void
(0) : __assert_fail ("this->explicit_stride == 0", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
2235 return this->vector_elements * N;
2236 }
2237
2238 if (this->without_array()->is_matrix()) {
25
Assuming the condition is false
26
Taking false branch
2239 const struct glsl_type *element_type;
2240 const struct glsl_type *vec_type;
2241 unsigned int array_len;
2242
2243 if (this->is_array()) {
2244 element_type = this->without_array();
2245 array_len = this->arrays_of_arrays_size();
2246 } else {
2247 element_type = this;
2248 array_len = 1;
2249 }
2250
2251 if (row_major) {
2252 vec_type = get_instance(element_type->base_type,
2253 element_type->matrix_columns, 1);
2254
2255 array_len *= element_type->vector_elements;
2256 } else {
2257 vec_type = get_instance(element_type->base_type,
2258 element_type->vector_elements, 1);
2259 array_len *= element_type->matrix_columns;
2260 }
2261 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
2262 array_len);
2263
2264 return array_type->std430_size(false);
2265 }
2266
2267 if (this->is_array()) {
2268 unsigned stride;
2269 if (this->without_array()->is_struct())
2270 stride = this->without_array()->std430_size(row_major);
2271 else
2272 stride = this->without_array()->std430_base_alignment(row_major);
2273
2274 unsigned size = this->arrays_of_arrays_size() * stride;
2275 assert(this->explicit_stride == 0 ||(static_cast <bool> (this->explicit_stride == 0 || size
== this->length * this->explicit_stride) ? void (0) : __assert_fail
("this->explicit_stride == 0 || size == this->length * this->explicit_stride"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
2276 size == this->length * this->explicit_stride)(static_cast <bool> (this->explicit_stride == 0 || size
== this->length * this->explicit_stride) ? void (0) : __assert_fail
("this->explicit_stride == 0 || size == this->length * this->explicit_stride"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2277 return size;
2278 }
2279
2280 if (this->is_struct() || this->is_interface()) {
2281 unsigned size = 0;
2282 unsigned max_align = 0;
27
'max_align' initialized to 0
2283
2284 for (unsigned i = 0; i < this->length; i++) {
28
Assuming 'i' is >= field 'length'
29
Loop condition is false. Execution continues on line 2301
2285 bool field_row_major = row_major;
2286 const enum glsl_matrix_layout matrix_layout =
2287 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
2288 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2289 field_row_major = true;
2290 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2291 field_row_major = false;
2292 }
2293
2294 const struct glsl_type *field_type = this->fields.structure[i].type;
2295 unsigned align = field_type->std430_base_alignment(field_row_major);
2296 size = glsl_align(size, align);
2297 size += field_type->std430_size(field_row_major);
2298
2299 max_align = MAX2(align, max_align)( (align)>(max_align) ? (align) : (max_align) );
2300 }
2301 size = glsl_align(size, max_align);
30
Passing the value 0 via 2nd parameter 'align'
31
Calling 'glsl_align'
2302 return size;
2303 }
2304
2305 assert(!"not reached")(static_cast <bool> (!"not reached") ? void (0) : __assert_fail
("!\"not reached\"", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
2306 return -1;
2307}
2308
2309const glsl_type *
2310glsl_type::get_explicit_std430_type(bool row_major) const
2311{
2312 if (this->is_vector() || this->is_scalar()) {
4
Taking false branch
8
Taking false branch
12
Taking false branch
2313 return this;
2314 } else if (this->is_matrix()) {
5
Taking false branch
9
Taking false branch
13
Taking false branch
2315 const glsl_type *vec_type;
2316 if (row_major)
2317 vec_type = get_instance(this->base_type, this->matrix_columns, 1);
2318 else
2319 vec_type = get_instance(this->base_type, this->vector_elements, 1);
2320 unsigned stride = vec_type->std430_array_stride(false);
2321 return get_instance(this->base_type, this->vector_elements,
2322 this->matrix_columns, stride, row_major);
2323 } else if (this->is_array()) {
6
Taking true branch
10
Taking true branch
2324 const glsl_type *elem_type =
2325 this->fields.array->get_explicit_std430_type(row_major);
7
Calling 'glsl_type::get_explicit_std430_type'
11
Calling 'glsl_type::get_explicit_std430_type'
2326 unsigned stride = this->fields.array->std430_array_stride(row_major);
2327 return get_array_instance(elem_type, this->length, stride);
2328 } else if (this->is_struct() || this->is_interface()) {
14
Taking true branch
2329 glsl_struct_field *fields = new glsl_struct_field[this->length];
2330 unsigned offset = 0;
2331 for (unsigned i = 0; i
14.1
'i' is < field 'length'
14.1
'i' is < field 'length'
< length; i++) {
15
Loop condition is true. Entering loop body
2332 fields[i] = this->fields.structure[i];
2333
2334 bool field_row_major = row_major;
2335 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
16
Assuming field 'matrix_layout' is not equal to GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
17
Taking false branch
2336 field_row_major = false;
2337 } else if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
18
Assuming field 'matrix_layout' is not equal to GLSL_MATRIX_LAYOUT_ROW_MAJOR
19
Taking false branch
2338 field_row_major = true;
2339 }
2340 fields[i].type =
2341 fields[i].type->get_explicit_std430_type(field_row_major);
2342
2343 unsigned fsize = fields[i].type->std430_size(field_row_major);
20
Calling 'glsl_type::std430_size'
2344 unsigned falign = fields[i].type->std430_base_alignment(field_row_major);
2345 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2346 * Layout Qualifiers":
2347 *
2348 * "The actual offset of a member is computed as follows: If
2349 * offset was declared, start with that offset, otherwise start
2350 * with the next available offset. If the resulting offset is not
2351 * a multiple of the actual alignment, increase it to the first
2352 * offset that is a multiple of the actual alignment. This results
2353 * in the actual offset the member will have."
2354 */
2355 if (fields[i].offset >= 0) {
2356 assert((unsigned)fields[i].offset >= offset)(static_cast <bool> ((unsigned)fields[i].offset >= offset
) ? void (0) : __assert_fail ("(unsigned)fields[i].offset >= offset"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2357 offset = fields[i].offset;
2358 }
2359 offset = glsl_align(offset, falign);
2360 fields[i].offset = offset;
2361 offset += fsize;
2362 }
2363
2364 const glsl_type *type;
2365 if (this->is_struct())
2366 type = get_struct_instance(fields, this->length, this->name);
2367 else
2368 type = get_interface_instance(fields, this->length,
2369 (enum glsl_interface_packing)this->interface_packing,
2370 this->interface_row_major,
2371 this->name);
2372
2373 delete[] fields;
2374 return type;
2375 } else {
2376 unreachable("Invalid type for SSBO")do { (static_cast <bool> (!"Invalid type for SSBO") ? void
(0) : __assert_fail ("!\"Invalid type for SSBO\"", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__)); __builtin_unreachable
(); } while (0)
;
2377 }
2378}
2379
2380const glsl_type *
2381glsl_type::get_explicit_interface_type(bool supports_std430) const
2382{
2383 enum glsl_interface_packing packing =
2384 this->get_internal_ifc_packing(supports_std430);
2385 if (packing
0.1
'packing' is not equal to GLSL_INTERFACE_PACKING_STD140
0.1
'packing' is not equal to GLSL_INTERFACE_PACKING_STD140
== GLSL_INTERFACE_PACKING_STD140) {
1
Taking false branch
2386 return this->get_explicit_std140_type(this->interface_row_major);
2387 } else {
2388 assert(packing == GLSL_INTERFACE_PACKING_STD430)(static_cast <bool> (packing == GLSL_INTERFACE_PACKING_STD430
) ? void (0) : __assert_fail ("packing == GLSL_INTERFACE_PACKING_STD430"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2
'?' condition is true
2389 return this->get_explicit_std430_type(this->interface_row_major);
3
Calling 'glsl_type::get_explicit_std430_type'
2390 }
2391}
2392
2393/* This differs from get_explicit_std430_type() in that it:
2394 * - can size arrays slightly smaller ("stride * (len - 1) + elem_size" instead
2395 * of "stride * len")
2396 * - consumes a glsl_type_size_align_func which allows 8 and 16-bit values to be
2397 * packed more tightly
2398 * - overrides any struct field offsets but get_explicit_std430_type() tries to
2399 * respect any existing ones
2400 */
2401const glsl_type *
2402glsl_type::get_explicit_type_for_size_align(glsl_type_size_align_func type_info,
2403 unsigned *size, unsigned *alignment) const
2404{
2405 if (this->is_scalar() || this->is_vector()) {
2406 type_info(this, size, alignment);
2407 return this;
2408 } else if (this->is_array()) {
2409 unsigned elem_size, elem_align;
2410 const struct glsl_type *explicit_element =
2411 this->fields.array->get_explicit_type_for_size_align(type_info, &elem_size, &elem_align);
2412
2413 unsigned stride = align(elem_size, elem_align);
2414
2415 *size = stride * (this->length - 1) + elem_size;
2416 *alignment = elem_align;
2417 return glsl_type::get_array_instance(explicit_element, this->length, stride);
2418 } else if (this->is_struct()) {
2419 struct glsl_struct_field *fields = (struct glsl_struct_field *)
2420 malloc(sizeof(struct glsl_struct_field) * this->length);
2421
2422 *size = 0;
2423 *alignment = 0;
2424 for (unsigned i = 0; i < this->length; i++) {
2425 fields[i] = this->fields.structure[i];
2426 assert(fields[i].matrix_layout != GLSL_MATRIX_LAYOUT_ROW_MAJOR)(static_cast <bool> (fields[i].matrix_layout != GLSL_MATRIX_LAYOUT_ROW_MAJOR
) ? void (0) : __assert_fail ("fields[i].matrix_layout != GLSL_MATRIX_LAYOUT_ROW_MAJOR"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2427
2428 unsigned field_size, field_align;
2429 fields[i].type =
2430 fields[i].type->get_explicit_type_for_size_align(type_info, &field_size, &field_align);
2431 fields[i].offset = align(*size, field_align);
2432
2433 *size = fields[i].offset + field_size;
2434 *alignment = MAX2(*alignment, field_align)( (*alignment)>(field_align) ? (*alignment) : (field_align
) )
;
2435 }
2436
2437 const glsl_type *type = glsl_type::get_struct_instance(fields, this->length, this->name, false);
2438 free(fields);
2439 return type;
2440 } else if (this->is_matrix()) {
2441 unsigned col_size, col_align;
2442 type_info(this->column_type(), &col_size, &col_align);
2443 unsigned stride = align(col_size, col_align);
2444
2445 *size = this->matrix_columns * stride;
2446 *alignment = col_align;
2447 return glsl_type::get_instance(this->base_type, this->vector_elements,
2448 this->matrix_columns, stride, false);
2449 } else {
2450 unreachable("Unhandled type.")do { (static_cast <bool> (!"Unhandled type.") ? void (0
) : __assert_fail ("!\"Unhandled type.\"", __builtin_FILE (),
__builtin_LINE (), __extension__ __PRETTY_FUNCTION__)); __builtin_unreachable
(); } while (0)
;
2451 }
2452}
2453
2454unsigned
2455glsl_type::count_vec4_slots(bool is_gl_vertex_input, bool is_bindless) const
2456{
2457 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2458 *
2459 * "A scalar input counts the same amount against this limit as a vec4,
2460 * so applications may want to consider packing groups of four
2461 * unrelated float inputs together into a vector to better utilize the
2462 * capabilities of the underlying hardware. A matrix input will use up
2463 * multiple locations. The number of locations used will equal the
2464 * number of columns in the matrix."
2465 *
2466 * The spec does not explicitly say how arrays are counted. However, it
2467 * should be safe to assume the total number of slots consumed by an array
2468 * is the number of entries in the array multiplied by the number of slots
2469 * consumed by a single element of the array.
2470 *
2471 * The spec says nothing about how structs are counted, because vertex
2472 * attributes are not allowed to be (or contain) structs. However, Mesa
2473 * allows varying structs, the number of varying slots taken up by a
2474 * varying struct is simply equal to the sum of the number of slots taken
2475 * up by each element.
2476 *
2477 * Doubles are counted different depending on whether they are vertex
2478 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
2479 * take one location no matter what size they are, otherwise dvec3/4
2480 * take two locations.
2481 */
2482 switch (this->base_type) {
2483 case GLSL_TYPE_UINT:
2484 case GLSL_TYPE_INT:
2485 case GLSL_TYPE_UINT8:
2486 case GLSL_TYPE_INT8:
2487 case GLSL_TYPE_UINT16:
2488 case GLSL_TYPE_INT16:
2489 case GLSL_TYPE_FLOAT:
2490 case GLSL_TYPE_FLOAT16:
2491 case GLSL_TYPE_BOOL:
2492 return this->matrix_columns;
2493 case GLSL_TYPE_DOUBLE:
2494 case GLSL_TYPE_UINT64:
2495 case GLSL_TYPE_INT64:
2496 if (this->vector_elements > 2 && !is_gl_vertex_input)
2497 return this->matrix_columns * 2;
2498 else
2499 return this->matrix_columns;
2500 case GLSL_TYPE_STRUCT:
2501 case GLSL_TYPE_INTERFACE: {
2502 unsigned size = 0;
2503
2504 for (unsigned i = 0; i < this->length; i++) {
2505 const glsl_type *member_type = this->fields.structure[i].type;
2506 size += member_type->count_vec4_slots(is_gl_vertex_input, is_bindless);
2507 }
2508
2509 return size;
2510 }
2511
2512 case GLSL_TYPE_ARRAY: {
2513 const glsl_type *element = this->fields.array;
2514 return this->length * element->count_vec4_slots(is_gl_vertex_input,
2515 is_bindless);
2516 }
2517
2518 case GLSL_TYPE_SAMPLER:
2519 case GLSL_TYPE_IMAGE:
2520 if (!is_bindless)
2521 return 0;
2522 else
2523 return 1;
2524
2525 case GLSL_TYPE_SUBROUTINE:
2526 return 1;
2527
2528 case GLSL_TYPE_FUNCTION:
2529 case GLSL_TYPE_ATOMIC_UINT:
2530 case GLSL_TYPE_VOID:
2531 case GLSL_TYPE_ERROR:
2532 break;
2533 }
2534
2535 assert(!"Unexpected type in count_attribute_slots()")(static_cast <bool> (!"Unexpected type in count_attribute_slots()"
) ? void (0) : __assert_fail ("!\"Unexpected type in count_attribute_slots()\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2536
2537 return 0;
2538}
2539
2540unsigned
2541glsl_type::count_dword_slots(bool is_bindless) const
2542{
2543 switch (this->base_type) {
2544 case GLSL_TYPE_UINT:
2545 case GLSL_TYPE_INT:
2546 case GLSL_TYPE_FLOAT:
2547 case GLSL_TYPE_BOOL:
2548 return this->components();
2549 case GLSL_TYPE_UINT16:
2550 case GLSL_TYPE_INT16:
2551 case GLSL_TYPE_FLOAT16:
2552 return DIV_ROUND_UP(this->components(), 2)( ((this->components()) + (2) - 1) / (2) );
2553 case GLSL_TYPE_UINT8:
2554 case GLSL_TYPE_INT8:
2555 return DIV_ROUND_UP(this->components(), 4)( ((this->components()) + (4) - 1) / (4) );
2556 case GLSL_TYPE_IMAGE:
2557 case GLSL_TYPE_SAMPLER:
2558 if (!is_bindless)
2559 return 0;
2560 /* FALLTHROUGH */
2561 case GLSL_TYPE_DOUBLE:
2562 case GLSL_TYPE_UINT64:
2563 case GLSL_TYPE_INT64:
2564 return this->components() * 2;
2565 case GLSL_TYPE_ARRAY:
2566 return this->fields.array->count_dword_slots(is_bindless) *
2567 this->length;
2568
2569 case GLSL_TYPE_INTERFACE:
2570 case GLSL_TYPE_STRUCT: {
2571 unsigned size = 0;
2572 for (unsigned i = 0; i < this->length; i++) {
2573 size += this->fields.structure[i].type->count_dword_slots(is_bindless);
2574 }
2575 return size;
2576 }
2577
2578 case GLSL_TYPE_ATOMIC_UINT:
2579 return 0;
2580 case GLSL_TYPE_SUBROUTINE:
2581 return 1;
2582 case GLSL_TYPE_VOID:
2583 case GLSL_TYPE_ERROR:
2584 case GLSL_TYPE_FUNCTION:
2585 default:
2586 unreachable("invalid type in st_glsl_type_dword_size()")do { (static_cast <bool> (!"invalid type in st_glsl_type_dword_size()"
) ? void (0) : __assert_fail ("!\"invalid type in st_glsl_type_dword_size()\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
2587 }
2588
2589 return 0;
2590}
2591
2592int
2593glsl_type::coordinate_components() const
2594{
2595 enum glsl_sampler_dim dim = (enum glsl_sampler_dim)sampler_dimensionality;
2596 int size = glsl_get_sampler_dim_coordinate_components(dim);
2597
2598 /* Array textures need an additional component for the array index, except
2599 * for cubemap array images that behave like a 2D array of interleaved
2600 * cubemap faces.
2601 */
2602 if (sampler_array &&
2603 !(is_image() && sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
2604 size += 1;
2605
2606 return size;
2607}
2608
2609/**
2610 * Declarations of type flyweights (glsl_type::_foo_type) and
2611 * convenience pointers (glsl_type::foo_type).
2612 * @{
2613 */
2614#define DECL_TYPE(NAME, ...)const glsl_type glsl_type::_NAME_type = glsl_type(..., "NAME"
); const glsl_type *const glsl_type::NAME_type = &glsl_type
::_NAME_type;
\
2615 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2616 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2617
2618#define STRUCT_TYPE(NAME)
2619
2620#include "compiler/builtin_type_macros.h"
2621/** @} */
2622
2623static void
2624get_struct_type_field_and_pointer_sizes(size_t *s_field_size,
2625 size_t *s_field_ptrs)
2626{
2627 *s_field_size = sizeof(glsl_struct_field);
2628 *s_field_ptrs =
2629 sizeof(((glsl_struct_field *)0)->type) +
2630 sizeof(((glsl_struct_field *)0)->name);
2631}
2632
2633union packed_type {
2634 uint32_t u32;
2635 struct {
2636 unsigned base_type:5;
2637 unsigned interface_row_major:1;
2638 unsigned vector_elements:3;
2639 unsigned matrix_columns:3;
2640 unsigned explicit_stride:20;
2641 } basic;
2642 struct {
2643 unsigned base_type:5;
2644 unsigned dimensionality:4;
2645 unsigned shadow:1;
2646 unsigned array:1;
2647 unsigned sampled_type:2;
2648 unsigned _pad:19;
2649 } sampler;
2650 struct {
2651 unsigned base_type:5;
2652 unsigned length:13;
2653 unsigned explicit_stride:14;
2654 } array;
2655 struct {
2656 unsigned base_type:5;
2657 unsigned interface_packing_or_packed:2;
2658 unsigned interface_row_major:1;
2659 unsigned length:24;
2660 } strct;
2661};
2662
2663void
2664encode_type_to_blob(struct blob *blob, const glsl_type *type)
2665{
2666 if (!type) {
2667 blob_write_uint32(blob, 0);
2668 return;
2669 }
2670
2671 STATIC_ASSERT(sizeof(union packed_type) == 4)do { (void) sizeof(char [1 - 2*!(sizeof(union packed_type) ==
4)]); } while (0)
;
2672 union packed_type encoded;
2673 encoded.u32 = 0;
2674 encoded.basic.base_type = type->base_type;
2675
2676 switch (type->base_type) {
2677 case GLSL_TYPE_UINT:
2678 case GLSL_TYPE_INT:
2679 case GLSL_TYPE_FLOAT:
2680 case GLSL_TYPE_FLOAT16:
2681 case GLSL_TYPE_DOUBLE:
2682 case GLSL_TYPE_UINT8:
2683 case GLSL_TYPE_INT8:
2684 case GLSL_TYPE_UINT16:
2685 case GLSL_TYPE_INT16:
2686 case GLSL_TYPE_UINT64:
2687 case GLSL_TYPE_INT64:
2688 case GLSL_TYPE_BOOL:
2689 encoded.basic.interface_row_major = type->interface_row_major;
2690 assert(type->matrix_columns < 8)(static_cast <bool> (type->matrix_columns < 8) ? void
(0) : __assert_fail ("type->matrix_columns < 8", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
2691 if (type->vector_elements <= 4)
2692 encoded.basic.vector_elements = type->vector_elements;
2693 else if (type->vector_elements == 8)
2694 encoded.basic.vector_elements = 5;
2695 else if (type->vector_elements == 16)
2696 encoded.basic.vector_elements = 6;
2697 encoded.basic.matrix_columns = type->matrix_columns;
2698 encoded.basic.explicit_stride = MIN2(type->explicit_stride, 0xfffff)( (type->explicit_stride)<(0xfffff) ? (type->explicit_stride
) : (0xfffff) )
;
2699 blob_write_uint32(blob, encoded.u32);
2700 /* If we don't have enough bits for explicit_stride, store it
2701 * separately.
2702 */
2703 if (encoded.basic.explicit_stride == 0xfffff)
2704 blob_write_uint32(blob, type->explicit_stride);
2705 return;
2706 case GLSL_TYPE_SAMPLER:
2707 encoded.sampler.dimensionality = type->sampler_dimensionality;
2708 encoded.sampler.shadow = type->sampler_shadow;
2709 encoded.sampler.array = type->sampler_array;
2710 encoded.sampler.sampled_type = type->sampled_type;
2711 break;
2712 case GLSL_TYPE_SUBROUTINE:
2713 blob_write_uint32(blob, encoded.u32);
2714 blob_write_string(blob, type->name);
2715 return;
2716 case GLSL_TYPE_IMAGE:
2717 encoded.sampler.dimensionality = type->sampler_dimensionality;
2718 encoded.sampler.array = type->sampler_array;
2719 encoded.sampler.sampled_type = type->sampled_type;
2720 break;
2721 case GLSL_TYPE_ATOMIC_UINT:
2722 break;
2723 case GLSL_TYPE_ARRAY:
2724 encoded.array.length = MIN2(type->length, 0x1fff)( (type->length)<(0x1fff) ? (type->length) : (0x1fff
) )
;
2725 encoded.array.explicit_stride = MIN2(type->explicit_stride, 0x3fff)( (type->explicit_stride)<(0x3fff) ? (type->explicit_stride
) : (0x3fff) )
;
2726 blob_write_uint32(blob, encoded.u32);
2727 /* If we don't have enough bits for length or explicit_stride, store it
2728 * separately.
2729 */
2730 if (encoded.array.length == 0x1fff)
2731 blob_write_uint32(blob, type->length);
2732 if (encoded.array.explicit_stride == 0x3fff)
2733 blob_write_uint32(blob, type->explicit_stride);
2734 encode_type_to_blob(blob, type->fields.array);
2735 return;
2736 case GLSL_TYPE_STRUCT:
2737 case GLSL_TYPE_INTERFACE:
2738 encoded.strct.length = MIN2(type->length, 0xffffff)( (type->length)<(0xffffff) ? (type->length) : (0xffffff
) )
;
2739 if (type->is_interface()) {
2740 encoded.strct.interface_packing_or_packed = type->interface_packing;
2741 encoded.strct.interface_row_major = type->interface_row_major;
2742 } else {
2743 encoded.strct.interface_packing_or_packed = type->packed;
2744 }
2745 blob_write_uint32(blob, encoded.u32);
2746 blob_write_string(blob, type->name);
2747
2748 /* If we don't have enough bits for length, store it separately. */
2749 if (encoded.strct.length == 0xffffff)
2750 blob_write_uint32(blob, type->length);
2751
2752 size_t s_field_size, s_field_ptrs;
2753 get_struct_type_field_and_pointer_sizes(&s_field_size, &s_field_ptrs);
2754
2755 for (unsigned i = 0; i < type->length; i++) {
2756 encode_type_to_blob(blob, type->fields.structure[i].type);
2757 blob_write_string(blob, type->fields.structure[i].name);
2758
2759 /* Write the struct field skipping the pointers */
2760 blob_write_bytes(blob,
2761 ((char *)&type->fields.structure[i]) + s_field_ptrs,
2762 s_field_size - s_field_ptrs);
2763 }
2764 return;
2765 case GLSL_TYPE_VOID:
2766 break;
2767 case GLSL_TYPE_ERROR:
2768 default:
2769 assert(!"Cannot encode type!")(static_cast <bool> (!"Cannot encode type!") ? void (0)
: __assert_fail ("!\"Cannot encode type!\"", __builtin_FILE (
), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
2770 encoded.u32 = 0;
2771 break;
2772 }
2773
2774 blob_write_uint32(blob, encoded.u32);
2775}
2776
2777const glsl_type *
2778decode_type_from_blob(struct blob_reader *blob)
2779{
2780 union packed_type encoded;
2781 encoded.u32 = blob_read_uint32(blob);
2782
2783 if (encoded.u32 == 0) {
2784 return NULL__null;
2785 }
2786
2787 glsl_base_type base_type = (glsl_base_type)encoded.basic.base_type;
2788
2789 switch (base_type) {
2790 case GLSL_TYPE_UINT:
2791 case GLSL_TYPE_INT:
2792 case GLSL_TYPE_FLOAT:
2793 case GLSL_TYPE_FLOAT16:
2794 case GLSL_TYPE_DOUBLE:
2795 case GLSL_TYPE_UINT8:
2796 case GLSL_TYPE_INT8:
2797 case GLSL_TYPE_UINT16:
2798 case GLSL_TYPE_INT16:
2799 case GLSL_TYPE_UINT64:
2800 case GLSL_TYPE_INT64:
2801 case GLSL_TYPE_BOOL: {
2802 unsigned explicit_stride = encoded.basic.explicit_stride;
2803 if (explicit_stride == 0xfffff)
2804 explicit_stride = blob_read_uint32(blob);
2805 uint32_t vector_elements = encoded.basic.vector_elements;
2806 if (vector_elements == 5)
2807 vector_elements = 8;
2808 else if (vector_elements == 6)
2809 vector_elements = 16;
2810 return glsl_type::get_instance(base_type, encoded.basic.vector_elements,
2811 encoded.basic.matrix_columns,
2812 explicit_stride,
2813 encoded.basic.interface_row_major);
2814 }
2815 case GLSL_TYPE_SAMPLER:
2816 return glsl_type::get_sampler_instance((enum glsl_sampler_dim)encoded.sampler.dimensionality,
2817 encoded.sampler.shadow,
2818 encoded.sampler.array,
2819 (glsl_base_type) encoded.sampler.sampled_type);
2820 case GLSL_TYPE_SUBROUTINE:
2821 return glsl_type::get_subroutine_instance(blob_read_string(blob));
2822 case GLSL_TYPE_IMAGE:
2823 return glsl_type::get_image_instance((enum glsl_sampler_dim)encoded.sampler.dimensionality,
2824 encoded.sampler.array,
2825 (glsl_base_type) encoded.sampler.sampled_type);
2826 case GLSL_TYPE_ATOMIC_UINT:
2827 return glsl_type::atomic_uint_type;
2828 case GLSL_TYPE_ARRAY: {
2829 unsigned length = encoded.array.length;
2830 if (length == 0x1fff)
2831 length = blob_read_uint32(blob);
2832 unsigned explicit_stride = encoded.array.explicit_stride;
2833 if (explicit_stride == 0x3fff)
2834 explicit_stride = blob_read_uint32(blob);
2835 return glsl_type::get_array_instance(decode_type_from_blob(blob),
2836 length, explicit_stride);
2837 }
2838 case GLSL_TYPE_STRUCT:
2839 case GLSL_TYPE_INTERFACE: {
2840 char *name = blob_read_string(blob);
2841 unsigned num_fields = encoded.strct.length;
2842 if (num_fields == 0xffffff)
2843 num_fields = blob_read_uint32(blob);
2844
2845 size_t s_field_size, s_field_ptrs;
2846 get_struct_type_field_and_pointer_sizes(&s_field_size, &s_field_ptrs);
2847
2848 glsl_struct_field *fields =
2849 (glsl_struct_field *) malloc(s_field_size * num_fields);
2850 for (unsigned i = 0; i < num_fields; i++) {
2851 fields[i].type = decode_type_from_blob(blob);
2852 fields[i].name = blob_read_string(blob);
2853
2854 blob_copy_bytes(blob, ((uint8_t *) &fields[i]) + s_field_ptrs,
2855 s_field_size - s_field_ptrs);
2856 }
2857
2858 const glsl_type *t;
2859 if (base_type == GLSL_TYPE_INTERFACE) {
2860 enum glsl_interface_packing packing =
2861 (glsl_interface_packing) encoded.strct.interface_packing_or_packed;
2862 bool row_major = encoded.strct.interface_row_major;
2863 t = glsl_type::get_interface_instance(fields, num_fields, packing,
2864 row_major, name);
2865 } else {
2866 unsigned packed = encoded.strct.interface_packing_or_packed;
2867 t = glsl_type::get_struct_instance(fields, num_fields, name, packed);
2868 }
2869
2870 free(fields);
2871 return t;
2872 }
2873 case GLSL_TYPE_VOID:
2874 return glsl_type::void_type;
2875 case GLSL_TYPE_ERROR:
2876 default:
2877 assert(!"Cannot decode type!")(static_cast <bool> (!"Cannot decode type!") ? void (0)
: __assert_fail ("!\"Cannot decode type!\"", __builtin_FILE (
), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
2878 return NULL__null;
2879 }
2880}
2881
2882unsigned
2883glsl_type::cl_alignment() const
2884{
2885 /* vectors unlike arrays are aligned to their size */
2886 if (this->is_scalar() || this->is_vector())
2887 return this->cl_size();
2888 else if (this->is_array())
2889 return this->without_array()->cl_alignment();
2890 else if (this->is_struct()) {
2891 /* Packed Structs are 0x1 aligned despite their size. */
2892 if (this->packed)
2893 return 1;
2894
2895 unsigned res = 1;
2896 for (unsigned i = 0; i < this->length; ++i) {
2897 struct glsl_struct_field &field = this->fields.structure[i];
2898 res = MAX2(res, field.type->cl_alignment())( (res)>(field.type->cl_alignment()) ? (res) : (field.type
->cl_alignment()) )
;
2899 }
2900 return res;
2901 }
2902 return 1;
2903}
2904
2905unsigned
2906glsl_type::cl_size() const
2907{
2908 if (this->is_scalar()) {
2909 return glsl_base_type_get_bit_size(this->base_type) / 8;
2910 } else if (this->is_vector()) {
2911 unsigned vec_elemns = this->vector_elements == 3 ? 4 : this->vector_elements;
2912 return vec_elemns * glsl_base_type_get_bit_size(this->base_type) / 8;
2913 } else if (this->is_array()) {
2914 unsigned size = this->without_array()->cl_size();
2915 return size * this->length;
2916 } else if (this->is_struct()) {
2917 unsigned size = 0;
2918 for (unsigned i = 0; i < this->length; ++i) {
2919 struct glsl_struct_field &field = this->fields.structure[i];
2920 /* if a struct is packed, members don't get aligned */
2921 if (!this->packed)
2922 size = align(size, field.type->cl_alignment());
2923 size += field.type->cl_size();
2924 }
2925 return size;
2926 }
2927 return 1;
2928}
2929
2930extern "C" {
2931
2932int
2933glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim)
2934{
2935 switch (dim) {
2936 case GLSL_SAMPLER_DIM_1D:
2937 case GLSL_SAMPLER_DIM_BUF:
2938 return 1;
2939 case GLSL_SAMPLER_DIM_2D:
2940 case GLSL_SAMPLER_DIM_RECT:
2941 case GLSL_SAMPLER_DIM_MS:
2942 case GLSL_SAMPLER_DIM_EXTERNAL:
2943 case GLSL_SAMPLER_DIM_SUBPASS:
2944 case GLSL_SAMPLER_DIM_SUBPASS_MS:
2945 return 2;
2946 case GLSL_SAMPLER_DIM_3D:
2947 case GLSL_SAMPLER_DIM_CUBE:
2948 return 3;
2949 default:
2950 unreachable("Unknown sampler dim")do { (static_cast <bool> (!"Unknown sampler dim") ? void
(0) : __assert_fail ("!\"Unknown sampler dim\"", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__)); __builtin_unreachable
(); } while (0)
;
2951 }
2952}
2953
2954}

glsl-optimizer/src/compiler/glsl_types.h

1/* -*- c++ -*- */
2/*
3 * Copyright © 2009 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef GLSL_TYPES_H
26#define GLSL_TYPES_H
27
28#include <string.h>
29#include <assert.h>
30
31#include "shader_enums.h"
32#include "c11/threads.h"
33#include "util/blob.h"
34#include "util/format/u_format.h"
35#include "util/macros.h"
36
37#ifdef __cplusplus201703L
38#include "main/config.h"
39#endif
40
41struct glsl_type;
42
43#ifdef __cplusplus201703L
44extern "C" {
45#endif
46
47struct _mesa_glsl_parse_state;
48struct glsl_symbol_table;
49
50extern void
51glsl_type_singleton_init_or_ref();
52
53extern void
54glsl_type_singleton_decref();
55
56extern void
57_mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
58
59void encode_type_to_blob(struct blob *blob, const struct glsl_type *type);
60
61const struct glsl_type *decode_type_from_blob(struct blob_reader *blob);
62
63typedef void (*glsl_type_size_align_func)(const struct glsl_type *type,
64 unsigned *size, unsigned *align);
65
66enum glsl_base_type {
67 /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
68 * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
69 */
70 GLSL_TYPE_UINT = 0,
71 GLSL_TYPE_INT,
72 GLSL_TYPE_FLOAT,
73 GLSL_TYPE_FLOAT16,
74 GLSL_TYPE_DOUBLE,
75 GLSL_TYPE_UINT8,
76 GLSL_TYPE_INT8,
77 GLSL_TYPE_UINT16,
78 GLSL_TYPE_INT16,
79 GLSL_TYPE_UINT64,
80 GLSL_TYPE_INT64,
81 GLSL_TYPE_BOOL,
82 GLSL_TYPE_SAMPLER,
83 GLSL_TYPE_IMAGE,
84 GLSL_TYPE_ATOMIC_UINT,
85 GLSL_TYPE_STRUCT,
86 GLSL_TYPE_INTERFACE,
87 GLSL_TYPE_ARRAY,
88 GLSL_TYPE_VOID,
89 GLSL_TYPE_SUBROUTINE,
90 GLSL_TYPE_FUNCTION,
91 GLSL_TYPE_ERROR
92};
93
94/* Return the bit size of a type. Note that this differs from
95 * glsl_get_bit_size in that it returns 32 bits for bools, whereas at
96 * the NIR level we would want to return 1 bit for bools.
97 */
98static unsigned glsl_base_type_bit_size(enum glsl_base_type type)
99{
100 switch (type) {
101 case GLSL_TYPE_BOOL:
102 case GLSL_TYPE_INT:
103 case GLSL_TYPE_UINT:
104 case GLSL_TYPE_FLOAT: /* TODO handle mediump */
105 case GLSL_TYPE_SUBROUTINE:
106 return 32;
107
108 case GLSL_TYPE_FLOAT16:
109 case GLSL_TYPE_UINT16:
110 case GLSL_TYPE_INT16:
111 return 16;
112
113 case GLSL_TYPE_UINT8:
114 case GLSL_TYPE_INT8:
115 return 8;
116
117 case GLSL_TYPE_DOUBLE:
118 case GLSL_TYPE_INT64:
119 case GLSL_TYPE_UINT64:
120 case GLSL_TYPE_IMAGE:
121 case GLSL_TYPE_SAMPLER:
122 return 64;
123
124 default:
125 /* For GLSL_TYPE_STRUCT etc, it should be ok to return 0. This usually
126 * happens when calling this method through is_64bit and is_16bit
127 * methods
128 */
129 return 0;
130 }
131
132 return 0;
133}
134
135static inline bool glsl_base_type_is_16bit(enum glsl_base_type type)
136{
137 return glsl_base_type_bit_size(type) == 16;
138}
139
140static inline bool glsl_base_type_is_64bit(enum glsl_base_type type)
141{
142 return glsl_base_type_bit_size(type) == 64;
143}
144
145static inline bool glsl_base_type_is_integer(enum glsl_base_type type)
146{
147 return type == GLSL_TYPE_UINT8 ||
148 type == GLSL_TYPE_INT8 ||
149 type == GLSL_TYPE_UINT16 ||
150 type == GLSL_TYPE_INT16 ||
151 type == GLSL_TYPE_UINT ||
152 type == GLSL_TYPE_INT ||
153 type == GLSL_TYPE_UINT64 ||
154 type == GLSL_TYPE_INT64 ||
155 type == GLSL_TYPE_BOOL ||
156 type == GLSL_TYPE_SAMPLER ||
157 type == GLSL_TYPE_IMAGE;
158}
159
160static inline unsigned int
161glsl_base_type_get_bit_size(const enum glsl_base_type base_type)
162{
163 switch (base_type) {
164 case GLSL_TYPE_BOOL:
165 return 1;
166
167 case GLSL_TYPE_INT:
168 case GLSL_TYPE_UINT:
169 case GLSL_TYPE_FLOAT: /* TODO handle mediump */
170 case GLSL_TYPE_SUBROUTINE:
171 return 32;
172
173 case GLSL_TYPE_FLOAT16:
174 case GLSL_TYPE_UINT16:
175 case GLSL_TYPE_INT16:
176 return 16;
177
178 case GLSL_TYPE_UINT8:
179 case GLSL_TYPE_INT8:
180 return 8;
181
182 case GLSL_TYPE_DOUBLE:
183 case GLSL_TYPE_INT64:
184 case GLSL_TYPE_UINT64:
185 case GLSL_TYPE_IMAGE:
186 case GLSL_TYPE_SAMPLER:
187 return 64;
188
189 default:
190 unreachable("unknown base type")do { (static_cast <bool> (!"unknown base type") ? void (
0) : __assert_fail ("!\"unknown base type\"", __builtin_FILE (
), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__)); __builtin_unreachable
(); } while (0)
;
191 }
192
193 return 0;
194}
195
196static inline enum glsl_base_type
197glsl_unsigned_base_type_of(enum glsl_base_type type)
198{
199 switch (type) {
200 case GLSL_TYPE_INT:
201 return GLSL_TYPE_UINT;
202 case GLSL_TYPE_INT8:
203 return GLSL_TYPE_UINT8;
204 case GLSL_TYPE_INT16:
205 return GLSL_TYPE_UINT16;
206 case GLSL_TYPE_INT64:
207 return GLSL_TYPE_UINT64;
208 default:
209 assert(type == GLSL_TYPE_UINT ||(static_cast <bool> (type == GLSL_TYPE_UINT || type == GLSL_TYPE_UINT8
|| type == GLSL_TYPE_UINT16 || type == GLSL_TYPE_UINT64) ? void
(0) : __assert_fail ("type == GLSL_TYPE_UINT || type == GLSL_TYPE_UINT8 || type == GLSL_TYPE_UINT16 || type == GLSL_TYPE_UINT64"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
210 type == GLSL_TYPE_UINT8 ||(static_cast <bool> (type == GLSL_TYPE_UINT || type == GLSL_TYPE_UINT8
|| type == GLSL_TYPE_UINT16 || type == GLSL_TYPE_UINT64) ? void
(0) : __assert_fail ("type == GLSL_TYPE_UINT || type == GLSL_TYPE_UINT8 || type == GLSL_TYPE_UINT16 || type == GLSL_TYPE_UINT64"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
211 type == GLSL_TYPE_UINT16 ||(static_cast <bool> (type == GLSL_TYPE_UINT || type == GLSL_TYPE_UINT8
|| type == GLSL_TYPE_UINT16 || type == GLSL_TYPE_UINT64) ? void
(0) : __assert_fail ("type == GLSL_TYPE_UINT || type == GLSL_TYPE_UINT8 || type == GLSL_TYPE_UINT16 || type == GLSL_TYPE_UINT64"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
212 type == GLSL_TYPE_UINT64)(static_cast <bool> (type == GLSL_TYPE_UINT || type == GLSL_TYPE_UINT8
|| type == GLSL_TYPE_UINT16 || type == GLSL_TYPE_UINT64) ? void
(0) : __assert_fail ("type == GLSL_TYPE_UINT || type == GLSL_TYPE_UINT8 || type == GLSL_TYPE_UINT16 || type == GLSL_TYPE_UINT64"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
213 return type;
214 }
215}
216
217enum glsl_sampler_dim {
218 GLSL_SAMPLER_DIM_1D = 0,
219 GLSL_SAMPLER_DIM_2D,
220 GLSL_SAMPLER_DIM_3D,
221 GLSL_SAMPLER_DIM_CUBE,
222 GLSL_SAMPLER_DIM_RECT,
223 GLSL_SAMPLER_DIM_BUF,
224 GLSL_SAMPLER_DIM_EXTERNAL,
225 GLSL_SAMPLER_DIM_MS,
226 GLSL_SAMPLER_DIM_SUBPASS, /* for vulkan input attachments */
227 GLSL_SAMPLER_DIM_SUBPASS_MS, /* for multisampled vulkan input attachments */
228};
229
230int
231glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim);
232
233enum glsl_matrix_layout {
234 /**
235 * The layout of the matrix is inherited from the object containing the
236 * matrix (the top level structure or the uniform block).
237 */
238 GLSL_MATRIX_LAYOUT_INHERITED,
239
240 /**
241 * Explicit column-major layout
242 *
243 * If a uniform block doesn't have an explicit layout set, it will default
244 * to this layout.
245 */
246 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR,
247
248 /**
249 * Row-major layout
250 */
251 GLSL_MATRIX_LAYOUT_ROW_MAJOR
252};
253
254enum {
255 GLSL_PRECISION_NONE = 0,
256 GLSL_PRECISION_HIGH,
257 GLSL_PRECISION_MEDIUM,
258 GLSL_PRECISION_LOW
259};
260
261#ifdef __cplusplus201703L
262} /* extern "C" */
263
264#include "GL/gl.h"
265#include "util/ralloc.h"
266#include "main/menums.h" /* for gl_texture_index, C++'s enum rules are broken */
267
268struct glsl_type {
269 GLenum gl_type;
270 glsl_base_type base_type:8;
271
272 glsl_base_type sampled_type:8; /**< Type of data returned using this
273 * sampler or image. Only \c
274 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
275 * and \c GLSL_TYPE_UINT are valid.
276 */
277
278 unsigned sampler_dimensionality:4; /**< \see glsl_sampler_dim */
279 unsigned sampler_shadow:1;
280 unsigned sampler_array:1;
281 unsigned interface_packing:2;
282 unsigned interface_row_major:1;
283
284 /**
285 * For \c GLSL_TYPE_STRUCT this specifies if the struct is packed or not.
286 *
287 * Only used for Compute kernels
288 */
289 unsigned packed:1;
290
291private:
292 glsl_type() : mem_ctx(NULL__null)
293 {
294 // Dummy constructor, just for the sake of ASSERT_BITFIELD_SIZE.
295 }
296
297public:
298 /**
299 * \name Vector and matrix element counts
300 *
301 * For scalars, each of these values will be 1. For non-numeric types
302 * these will be 0.
303 */
304 /*@{*/
305 uint8_t vector_elements; /**< 1, 2, 3, or 4 vector elements. */
306 uint8_t matrix_columns; /**< 1, 2, 3, or 4 matrix columns. */
307 /*@}*/
308
309 /**
310 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
311 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
312 * elements in the structure and the number of values pointed to by
313 * \c fields.structure (below).
314 */
315 unsigned length;
316
317 /**
318 * Name of the data type
319 *
320 * Will never be \c NULL.
321 */
322 const char *name;
323
324 /**
325 * Explicit array, matrix, or vector stride. This is used to communicate
326 * explicit array layouts from SPIR-V. Should be 0 if the type has no
327 * explicit stride.
328 */
329 unsigned explicit_stride;
330
331 /**
332 * Subtype of composite data types.
333 */
334 union {
335 const struct glsl_type *array; /**< Type of array elements. */
336 struct glsl_function_param *parameters; /**< Parameters to function. */
337 struct glsl_struct_field *structure; /**< List of struct fields. */
338 } fields;
339
340 /**
341 * \name Pointers to various public type singletons
342 */
343 /*@{*/
344#undef DECL_TYPE
345#define DECL_TYPE(NAME, ...)const glsl_type glsl_type::_NAME_type = glsl_type(..., "NAME"
); const glsl_type *const glsl_type::NAME_type = &glsl_type
::_NAME_type;
\
346 static const glsl_type *const NAME##_type;
347#undef STRUCT_TYPE
348#define STRUCT_TYPE(NAME) \
349 static const glsl_type *const struct_##NAME##_type;
350#include "compiler/builtin_type_macros.h"
351 /*@}*/
352
353 /**
354 * Convenience accessors for vector types (shorter than get_instance()).
355 * @{
356 */
357 static const glsl_type *vec(unsigned components, const glsl_type *const ts[]);
358 static const glsl_type *vec(unsigned components);
359 static const glsl_type *f16vec(unsigned components);
360 static const glsl_type *dvec(unsigned components);
361 static const glsl_type *ivec(unsigned components);
362 static const glsl_type *uvec(unsigned components);
363 static const glsl_type *bvec(unsigned components);
364 static const glsl_type *i64vec(unsigned components);
365 static const glsl_type *u64vec(unsigned components);
366 static const glsl_type *i16vec(unsigned components);
367 static const glsl_type *u16vec(unsigned components);
368 static const glsl_type *i8vec(unsigned components);
369 static const glsl_type *u8vec(unsigned components);
370 /**@}*/
371
372 /**
373 * For numeric and boolean derived types returns the basic scalar type
374 *
375 * If the type is a numeric or boolean scalar, vector, or matrix type,
376 * this function gets the scalar type of the individual components. For
377 * all other types, including arrays of numeric or boolean types, the
378 * error type is returned.
379 */
380 const glsl_type *get_base_type() const;
381
382 /**
383 * Get the basic scalar type which this type aggregates.
384 *
385 * If the type is a numeric or boolean scalar, vector, or matrix, or an
386 * array of any of those, this function gets the scalar type of the
387 * individual components. For structs and arrays of structs, this function
388 * returns the struct type. For samplers and arrays of samplers, this
389 * function returns the sampler type.
390 */
391 const glsl_type *get_scalar_type() const;
392
393 /**
394 * Gets the "bare" type without any decorations or layout information.
395 */
396 const glsl_type *get_bare_type() const;
397
398 /**
399 * Gets the float16 version of this type.
400 */
401 const glsl_type *get_float16_type() const;
402
403 /**
404 * Get the instance of a built-in scalar, vector, or matrix type
405 */
406 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
407 unsigned columns,
408 unsigned explicit_stride = 0,
409 bool row_major = false);
410
411 /**
412 * Get the instance of a sampler type
413 */
414 static const glsl_type *get_sampler_instance(enum glsl_sampler_dim dim,
415 bool shadow,
416 bool array,
417 glsl_base_type type);
418
419 static const glsl_type *get_image_instance(enum glsl_sampler_dim dim,
420 bool array, glsl_base_type type);
421
422 /**
423 * Get the instance of an array type
424 */
425 static const glsl_type *get_array_instance(const glsl_type *base,
426 unsigned elements,
427 unsigned explicit_stride = 0);
428
429 /**
430 * Get the instance of a record type
431 */
432 static const glsl_type *get_struct_instance(const glsl_struct_field *fields,
433 unsigned num_fields,
434 const char *name,
435 bool packed = false);
436
437 /**
438 * Get the instance of an interface block type
439 */
440 static const glsl_type *get_interface_instance(const glsl_struct_field *fields,
441 unsigned num_fields,
442 enum glsl_interface_packing packing,
443 bool row_major,
444 const char *block_name);
445
446 /**
447 * Get the instance of an subroutine type
448 */
449 static const glsl_type *get_subroutine_instance(const char *subroutine_name);
450
451 /**
452 * Get the instance of a function type
453 */
454 static const glsl_type *get_function_instance(const struct glsl_type *return_type,
455 const glsl_function_param *parameters,
456 unsigned num_params);
457
458 /**
459 * Get the type resulting from a multiplication of \p type_a * \p type_b
460 */
461 static const glsl_type *get_mul_type(const glsl_type *type_a,
462 const glsl_type *type_b);
463
464 /**
465 * Query the total number of scalars that make up a scalar, vector or matrix
466 */
467 unsigned components() const
468 {
469 return vector_elements * matrix_columns;
470 }
471
472 /**
473 * Calculate the number of components slots required to hold this type
474 *
475 * This is used to determine how many uniform or varying locations a type
476 * might occupy.
477 */
478 unsigned component_slots() const;
479
480 /**
481 * Calculate offset between the base location of the struct in
482 * uniform storage and a struct member.
483 * For the initial call, length is the index of the member to find the
484 * offset for.
485 */
486 unsigned struct_location_offset(unsigned length) const;
487
488 /**
489 * Calculate the number of unique values from glGetUniformLocation for the
490 * elements of the type.
491 *
492 * This is used to allocate slots in the UniformRemapTable, the amount of
493 * locations may not match with actual used storage space by the driver.
494 */
495 unsigned uniform_locations() const;
496
497 /**
498 * Used to count the number of varyings contained in the type ignoring
499 * innermost array elements.
500 */
501 unsigned varying_count() const;
502
503 /**
504 * Calculate the number of vec4 slots required to hold this type.
505 *
506 * This is the underlying recursive type_size function for
507 * count_attribute_slots() (vertex inputs and varyings) but also for
508 * gallium's !PIPE_CAP_PACKED_UNIFORMS case.
509 */
510 unsigned count_vec4_slots(bool is_gl_vertex_input, bool bindless) const;
511
512 /**
513 * Calculate the number of vec4 slots required to hold this type.
514 *
515 * This is the underlying recursive type_size function for
516 * gallium's PIPE_CAP_PACKED_UNIFORMS case.
517 */
518 unsigned count_dword_slots(bool bindless) const;
519
520 /**
521 * Calculate the number of attribute slots required to hold this type
522 *
523 * This implements the language rules of GLSL 1.50 for counting the number
524 * of slots used by a vertex attribute. It also determines the number of
525 * varying slots the type will use up in the absence of varying packing
526 * (and thus, it can be used to measure the number of varying slots used by
527 * the varyings that are generated by lower_packed_varyings).
528 *
529 * For vertex shader attributes - doubles only take one slot.
530 * For inter-shader varyings - dvec3/dvec4 take two slots.
531 *
532 * Vulkan doesn’t make this distinction so the argument should always be
533 * false.
534 */
535 unsigned count_attribute_slots(bool is_gl_vertex_input) const {
536 return count_vec4_slots(is_gl_vertex_input, true);
537 }
538
539 /**
540 * Alignment in bytes of the start of this type in a std140 uniform
541 * block.
542 */
543 unsigned std140_base_alignment(bool row_major) const;
544
545 /** Size in bytes of this type in a std140 uniform block.
546 *
547 * Note that this is not GL_UNIFORM_SIZE (which is the number of
548 * elements in the array)
549 */
550 unsigned std140_size(bool row_major) const;
551
552 /**
553 * Gets an explicitly laid out type with the std140 layout.
554 */
555 const glsl_type *get_explicit_std140_type(bool row_major) const;
556
557 /**
558 * Alignment in bytes of the start of this type in a std430 shader
559 * storage block.
560 */
561 unsigned std430_base_alignment(bool row_major) const;
562
563 /**
564 * Calculate array stride in bytes of this type in a std430 shader storage
565 * block.
566 */
567 unsigned std430_array_stride(bool row_major) const;
568
569 /**
570 * Size in bytes of this type in a std430 shader storage block.
571 *
572 * Note that this is not GL_BUFFER_SIZE
573 */
574 unsigned std430_size(bool row_major) const;
575
576 /**
577 * Gets an explicitly laid out type with the std430 layout.
578 */
579 const glsl_type *get_explicit_std430_type(bool row_major) const;
580
581 /**
582 * Gets an explicitly laid out interface type.
583 */
584 const glsl_type *get_explicit_interface_type(bool supports_std430) const;
585
586 /** Returns an explicitly laid out type given a type and size/align func
587 *
588 * The size/align func is only called for scalar and vector types and the
589 * returned type is otherwise laid out in the natural way as follows:
590 *
591 * - Arrays and matrices have a stride of ALIGN(elem_size, elem_align).
592 *
593 * - Structure types have their elements in-order and as tightly packed as
594 * possible following the alignment required by the size/align func.
595 *
596 * - All composite types (structures, matrices, and arrays) have an
597 * alignment equal to the highest alighment of any member of the composite.
598 *
599 * The types returned by this function are likely not suitable for most UBO
600 * or SSBO layout because they do not add the extra array and substructure
601 * alignment that is required by std140 and std430.
602 */
603 const glsl_type *get_explicit_type_for_size_align(glsl_type_size_align_func type_info,
604 unsigned *size, unsigned *align) const;
605
606 /**
607 * Alignment in bytes of the start of this type in OpenCL memory.
608 */
609 unsigned cl_alignment() const;
610
611 /**
612 * Size in bytes of this type in OpenCL memory
613 */
614 unsigned cl_size() const;
615
616 /**
617 * Size in bytes of this type based on its explicit data.
618 *
619 * When using SPIR-V shaders (ARB_gl_spirv), memory layouts are expressed
620 * through explicit offset, stride and matrix layout, so the size
621 * can/should be computed used those values.
622 *
623 * Note that the value returned by this method is only correct if such
624 * values are set, so only with SPIR-V shaders. Should not be used with
625 * GLSL shaders.
626 */
627 unsigned explicit_size(bool align_to_stride=false) const;
628
629 /**
630 * \brief Can this type be implicitly converted to another?
631 *
632 * \return True if the types are identical or if this type can be converted
633 * to \c desired according to Section 4.1.10 of the GLSL spec.
634 *
635 * \verbatim
636 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
637 * Implicit Conversions:
638 *
639 * In some situations, an expression and its type will be implicitly
640 * converted to a different type. The following table shows all allowed
641 * implicit conversions:
642 *
643 * Type of expression | Can be implicitly converted to
644 * --------------------------------------------------
645 * int float
646 * uint
647 *
648 * ivec2 vec2
649 * uvec2
650 *
651 * ivec3 vec3
652 * uvec3
653 *
654 * ivec4 vec4
655 * uvec4
656 *
657 * There are no implicit array or structure conversions. For example,
658 * an array of int cannot be implicitly converted to an array of float.
659 * There are no implicit conversions between signed and unsigned
660 * integers.
661 * \endverbatim
662 */
663 bool can_implicitly_convert_to(const glsl_type *desired,
664 _mesa_glsl_parse_state *state) const;
665
666 /**
667 * Query whether or not a type is a scalar (non-vector and non-matrix).
668 */
669 bool is_scalar() const
670 {
671 return (vector_elements == 1)
672 && (base_type >= GLSL_TYPE_UINT)
673 && (base_type <= GLSL_TYPE_IMAGE);
674 }
675
676 /**
677 * Query whether or not a type is a vector
678 */
679 bool is_vector() const
680 {
681 return (vector_elements > 1)
682 && (matrix_columns == 1)
683 && (base_type >= GLSL_TYPE_UINT)
684 && (base_type <= GLSL_TYPE_BOOL);
685 }
686
687 /**
688 * Query whether or not a type is a matrix
689 */
690 bool is_matrix() const
691 {
692 /* GLSL only has float matrices. */
693 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT ||
694 base_type == GLSL_TYPE_DOUBLE ||
695 base_type == GLSL_TYPE_FLOAT16);
696 }
697
698 /**
699 * Query whether or not a type is a non-array numeric type
700 */
701 bool is_numeric() const
702 {
703 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_INT64);
704 }
705
706 /**
707 * Query whether or not a type is an integer.
708 */
709 bool is_integer() const
710 {
711 return glsl_base_type_is_integer(base_type);
712 }
713
714 /**
715 * Query whether or not a type is an 32-bit integer.
716 */
717 bool is_integer_32() const
718 {
719 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
720 }
721
722 /**
723 * Query whether or not a type is a 64-bit integer.
724 */
725 bool is_integer_64() const
726 {
727 return base_type == GLSL_TYPE_UINT64 || base_type == GLSL_TYPE_INT64;
728 }
729
730 /**
731 * Query whether or not a type is a 32-bit or 64-bit integer
732 */
733 bool is_integer_32_64() const
734 {
735 return is_integer_32() || is_integer_64();
736 }
737
738 /**
739 * Query whether or not type is an integral type, or for struct and array
740 * types, contains an integral type.
741 */
742 bool contains_integer() const;
743
744 /**
745 * Query whether or not type is a double type, or for struct, interface and
746 * array types, contains a double type.
747 */
748 bool contains_double() const;
749
750 /**
751 * Query whether or not type is a 64-bit type, or for struct, interface and
752 * array types, contains a double type.
753 */
754 bool contains_64bit() const;
755
756 /**
757 * Query whether or not a type is a float type
758 */
759 bool is_float() const
760 {
761 return base_type == GLSL_TYPE_FLOAT;
762 }
763
764 /**
765 * Query whether or not a type is a half-float or float type
766 */
767 bool is_float_16_32() const
768 {
769 return base_type == GLSL_TYPE_FLOAT16 || is_float();
770 }
771
772 /**
773 * Query whether or not a type is a half-float, float or double
774 */
775 bool is_float_16_32_64() const
776 {
777 return base_type == GLSL_TYPE_FLOAT16 || is_float() || is_double();
778 }
779
780 /**
781 * Query whether or not a type is a double type
782 */
783 bool is_double() const
784 {
785 return base_type == GLSL_TYPE_DOUBLE;
786 }
787
788 /**
789 * Query whether a 64-bit type takes two slots.
790 */
791 bool is_dual_slot() const
792 {
793 return is_64bit() && vector_elements > 2;
794 }
795
796 /**
797 * Query whether or not a type is 64-bit
798 */
799 bool is_64bit() const
800 {
801 return glsl_base_type_is_64bit(base_type);
802 }
803
804 /**
805 * Query whether or not a type is 16-bit
806 */
807 bool is_16bit() const
808 {
809 return glsl_base_type_is_16bit(base_type);
810 }
811
812 /**
813 * Query whether or not a type is 32-bit
814 */
815 bool is_32bit() const
816 {
817 return base_type == GLSL_TYPE_UINT ||
818 base_type == GLSL_TYPE_INT ||
819 base_type == GLSL_TYPE_FLOAT;
820 }
821
822 /**
823 * Query whether or not a type is a non-array boolean type
824 */
825 bool is_boolean() const
826 {
827 return base_type == GLSL_TYPE_BOOL;
828 }
829
830 /**
831 * Query whether or not a type is a sampler
832 */
833 bool is_sampler() const
834 {
835 return base_type == GLSL_TYPE_SAMPLER;
836 }
837
838 /**
839 * Query whether or not type is a sampler, or for struct, interface and
840 * array types, contains a sampler.
841 */
842 bool contains_sampler() const;
843
844 /**
845 * Query whether or not type is an array or for struct, interface and
846 * array types, contains an array.
847 */
848 bool contains_array() const;
849
850 /**
851 * Get the Mesa texture target index for a sampler type.
852 */
853 gl_texture_index sampler_index() const;
854
855 /**
856 * Query whether or not type is an image, or for struct, interface and
857 * array types, contains an image.
858 */
859 bool contains_image() const;
860
861 /**
862 * Query whether or not a type is an image
863 */
864 bool is_image() const
865 {
866 return base_type == GLSL_TYPE_IMAGE;
867 }
868
869 /**
870 * Query whether or not a type is an array
871 */
872 bool is_array() const
873 {
874 return base_type == GLSL_TYPE_ARRAY;
875 }
876
877 bool is_array_of_arrays() const
878 {
879 return is_array() && fields.array->is_array();
880 }
881
882 /**
883 * Query whether or not a type is a record
884 */
885 bool is_struct() const
886 {
887 return base_type == GLSL_TYPE_STRUCT;
888 }
889
890 /**
891 * Query whether or not a type is an interface
892 */
893 bool is_interface() const
894 {
895 return base_type == GLSL_TYPE_INTERFACE;
896 }
897
898 /**
899 * Query whether or not a type is the void type singleton.
900 */
901 bool is_void() const
902 {
903 return base_type == GLSL_TYPE_VOID;
904 }
905
906 /**
907 * Query whether or not a type is the error type singleton.
908 */
909 bool is_error() const
910 {
911 return base_type == GLSL_TYPE_ERROR;
912 }
913
914 /**
915 * Query if a type is unnamed/anonymous (named by the parser)
916 */
917
918 bool is_subroutine() const
919 {
920 return base_type == GLSL_TYPE_SUBROUTINE;
921 }
922 bool contains_subroutine() const;
923
924 bool is_anonymous() const
925 {
926 return !strncmp(name, "#anon", 5);
927 }
928
929 /**
930 * Get the type stripped of any arrays
931 *
932 * \return
933 * Pointer to the type of elements of the first non-array type for array
934 * types, or pointer to itself for non-array types.
935 */
936 const glsl_type *without_array() const
937 {
938 const glsl_type *t = this;
939
940 while (t->is_array())
941 t = t->fields.array;
942
943 return t;
944 }
945
946 /**
947 * Return the total number of elements in an array including the elements
948 * in arrays of arrays.
949 */
950 unsigned arrays_of_arrays_size() const
951 {
952 if (!is_array())
953 return 0;
954
955 unsigned size = length;
956 const glsl_type *base_type = fields.array;
957
958 while (base_type->is_array()) {
959 size = size * base_type->length;
960 base_type = base_type->fields.array;
961 }
962 return size;
963 }
964
965 /**
966 * Return bit size for this type.
967 */
968 unsigned bit_size() const
969 {
970 return glsl_base_type_bit_size(this->base_type);
971 }
972
973
974 /**
975 * Query whether or not a type is an atomic_uint.
976 */
977 bool is_atomic_uint() const
978 {
979 return base_type == GLSL_TYPE_ATOMIC_UINT;
980 }
981
982 /**
983 * Return the amount of atomic counter storage required for a type.
984 */
985 unsigned atomic_size() const
986 {
987 if (is_atomic_uint())
988 return ATOMIC_COUNTER_SIZE4;
989 else if (is_array())
990 return length * fields.array->atomic_size();
991 else
992 return 0;
993 }
994
995 /**
996 * Return whether a type contains any atomic counters.
997 */
998 bool contains_atomic() const
999 {
1000 return atomic_size() > 0;
1001 }
1002
1003 /**
1004 * Return whether a type contains any opaque types.
1005 */
1006 bool contains_opaque() const;
1007
1008 /**
1009 * Query the full type of a matrix row
1010 *
1011 * \return
1012 * If the type is not a matrix, \c glsl_type::error_type is returned.
1013 * Otherwise a type matching the rows of the matrix is returned.
1014 */
1015 const glsl_type *row_type() const
1016 {
1017 if (!is_matrix())
1018 return error_type;
1019
1020 if (explicit_stride && !interface_row_major)
1021 return get_instance(base_type, matrix_columns, 1, explicit_stride);
1022 else
1023 return get_instance(base_type, matrix_columns, 1);
1024 }
1025
1026 /**
1027 * Query the full type of a matrix column
1028 *
1029 * \return
1030 * If the type is not a matrix, \c glsl_type::error_type is returned.
1031 * Otherwise a type matching the columns of the matrix is returned.
1032 */
1033 const glsl_type *column_type() const
1034 {
1035 if (!is_matrix())
1036 return error_type;
1037
1038 if (explicit_stride && interface_row_major)
1039 return get_instance(base_type, vector_elements, 1, explicit_stride);
1040 else
1041 return get_instance(base_type, vector_elements, 1);
1042 }
1043
1044 /**
1045 * Get the type of a structure field
1046 *
1047 * \return
1048 * Pointer to the type of the named field. If the type is not a structure
1049 * or the named field does not exist, \c glsl_type::error_type is returned.
1050 */
1051 const glsl_type *field_type(const char *name) const;
1052
1053 /**
1054 * Get the location of a field within a record type
1055 */
1056 int field_index(const char *name) const;
1057
1058 /**
1059 * Query the number of elements in an array type
1060 *
1061 * \return
1062 * The number of elements in the array for array types or -1 for non-array
1063 * types. If the number of elements in the array has not yet been declared,
1064 * zero is returned.
1065 */
1066 int array_size() const
1067 {
1068 return is_array() ? length : -1;
1069 }
1070
1071 /**
1072 * Query whether the array size for all dimensions has been declared.
1073 */
1074 bool is_unsized_array() const
1075 {
1076 return is_array() && length == 0;
1077 }
1078
1079 /**
1080 * Return the number of coordinate components needed for this
1081 * sampler or image type.
1082 *
1083 * This is based purely on the sampler's dimensionality. For example, this
1084 * returns 1 for sampler1D, and 3 for sampler2DArray.
1085 *
1086 * Note that this is often different than actual coordinate type used in
1087 * a texturing built-in function, since those pack additional values (such
1088 * as the shadow comparator or projector) into the coordinate type.
1089 */
1090 int coordinate_components() const;
1091
1092 /**
1093 * Compares whether this type matches another type without taking into
1094 * account the precision in structures.
1095 *
1096 * This is applied recursively so that structures containing structure
1097 * members can also ignore the precision.
1098 */
1099 bool compare_no_precision(const glsl_type *b) const;
1100
1101 /**
1102 * Compare a record type against another record type.
1103 *
1104 * This is useful for matching record types declared on the same shader
1105 * stage as well as across different shader stages.
1106 * The option to not match name is needed for matching record types
1107 * declared across different shader stages.
1108 * The option to not match locations is to deal with places where the
1109 * same struct is defined in a block which has a location set on it.
1110 */
1111 bool record_compare(const glsl_type *b, bool match_name,
1112 bool match_locations = true,
1113 bool match_precision = true) const;
1114
1115 /**
1116 * Get the type interface packing.
1117 */
1118 enum glsl_interface_packing get_interface_packing() const
1119 {
1120 return (enum glsl_interface_packing)interface_packing;
1121 }
1122
1123 /**
1124 * Get the type interface packing used internally. For shared and packing
1125 * layouts this is implementation defined.
1126 */
1127 enum glsl_interface_packing get_internal_ifc_packing(bool std430_supported) const
1128 {
1129 enum glsl_interface_packing packing = this->get_interface_packing();
1130 if (packing == GLSL_INTERFACE_PACKING_STD140 ||
1131 (!std430_supported &&
1132 (packing == GLSL_INTERFACE_PACKING_SHARED ||
1133 packing == GLSL_INTERFACE_PACKING_PACKED))) {
1134 return GLSL_INTERFACE_PACKING_STD140;
1135 } else {
1136 assert(packing == GLSL_INTERFACE_PACKING_STD430 ||(static_cast <bool> (packing == GLSL_INTERFACE_PACKING_STD430
|| (std430_supported && (packing == GLSL_INTERFACE_PACKING_SHARED
|| packing == GLSL_INTERFACE_PACKING_PACKED))) ? void (0) : __assert_fail
("packing == GLSL_INTERFACE_PACKING_STD430 || (std430_supported && (packing == GLSL_INTERFACE_PACKING_SHARED || packing == GLSL_INTERFACE_PACKING_PACKED))"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
1137 (std430_supported &&(static_cast <bool> (packing == GLSL_INTERFACE_PACKING_STD430
|| (std430_supported && (packing == GLSL_INTERFACE_PACKING_SHARED
|| packing == GLSL_INTERFACE_PACKING_PACKED))) ? void (0) : __assert_fail
("packing == GLSL_INTERFACE_PACKING_STD430 || (std430_supported && (packing == GLSL_INTERFACE_PACKING_SHARED || packing == GLSL_INTERFACE_PACKING_PACKED))"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
1138 (packing == GLSL_INTERFACE_PACKING_SHARED ||(static_cast <bool> (packing == GLSL_INTERFACE_PACKING_STD430
|| (std430_supported && (packing == GLSL_INTERFACE_PACKING_SHARED
|| packing == GLSL_INTERFACE_PACKING_PACKED))) ? void (0) : __assert_fail
("packing == GLSL_INTERFACE_PACKING_STD430 || (std430_supported && (packing == GLSL_INTERFACE_PACKING_SHARED || packing == GLSL_INTERFACE_PACKING_PACKED))"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
1139 packing == GLSL_INTERFACE_PACKING_PACKED)))(static_cast <bool> (packing == GLSL_INTERFACE_PACKING_STD430
|| (std430_supported && (packing == GLSL_INTERFACE_PACKING_SHARED
|| packing == GLSL_INTERFACE_PACKING_PACKED))) ? void (0) : __assert_fail
("packing == GLSL_INTERFACE_PACKING_STD430 || (std430_supported && (packing == GLSL_INTERFACE_PACKING_SHARED || packing == GLSL_INTERFACE_PACKING_PACKED))"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1140 return GLSL_INTERFACE_PACKING_STD430;
1141 }
1142 }
1143
1144 /**
1145 * Check if the type interface is row major
1146 */
1147 bool get_interface_row_major() const
1148 {
1149 return (bool) interface_row_major;
1150 }
1151
1152 ~glsl_type();
1153
1154private:
1155
1156 static mtx_t hash_mutex;
1157
1158 /**
1159 * ralloc context for the type itself.
1160 */
1161 void *mem_ctx;
1162
1163 /** Constructor for vector and matrix types */
1164 glsl_type(GLenum gl_type,
1165 glsl_base_type base_type, unsigned vector_elements,
1166 unsigned matrix_columns, const char *name,
1167 unsigned explicit_stride = 0, bool row_major = false);
1168
1169 /** Constructor for sampler or image types */
1170 glsl_type(GLenum gl_type, glsl_base_type base_type,
1171 enum glsl_sampler_dim dim, bool shadow, bool array,
1172 glsl_base_type type, const char *name);
1173
1174 /** Constructor for record types */
1175 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
1176 const char *name, bool packed = false);
1177
1178 /** Constructor for interface types */
1179 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
1180 enum glsl_interface_packing packing,
1181 bool row_major, const char *name);
1182
1183 /** Constructor for interface types */
1184 glsl_type(const glsl_type *return_type,
1185 const glsl_function_param *params, unsigned num_params);
1186
1187 /** Constructors for array types */
1188 glsl_type(const glsl_type *array, unsigned length, unsigned explicit_stride);
1189
1190 /** Constructor for subroutine types */
1191 glsl_type(const char *name);
1192
1193 /** Hash table containing the known explicit matrix and vector types. */
1194 static struct hash_table *explicit_matrix_types;
1195
1196 /** Hash table containing the known array types. */
1197 static struct hash_table *array_types;
1198
1199 /** Hash table containing the known struct types. */
1200 static struct hash_table *struct_types;
1201
1202 /** Hash table containing the known interface types. */
1203 static struct hash_table *interface_types;
1204
1205 /** Hash table containing the known subroutine types. */
1206 static struct hash_table *subroutine_types;
1207
1208 /** Hash table containing the known function types. */
1209 static struct hash_table *function_types;
1210
1211 static bool record_key_compare(const void *a, const void *b);
1212 static unsigned record_key_hash(const void *key);
1213
1214 /**
1215 * \name Built-in type flyweights
1216 */
1217 /*@{*/
1218#undef DECL_TYPE
1219#define DECL_TYPE(NAME, ...)const glsl_type glsl_type::_NAME_type = glsl_type(..., "NAME"
); const glsl_type *const glsl_type::NAME_type = &glsl_type
::_NAME_type;
static const glsl_type _##NAME##_type;
1220#undef STRUCT_TYPE
1221#define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
1222#include "compiler/builtin_type_macros.h"
1223 /*@}*/
1224
1225 /**
1226 * \name Friend functions.
1227 *
1228 * These functions are friends because they must have C linkage and the
1229 * need to call various private methods or access various private static
1230 * data.
1231 */
1232 /*@{*/
1233 friend void glsl_type_singleton_init_or_ref(void);
1234 friend void glsl_type_singleton_decref(void);
1235 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
1236 /*@}*/
1237};
1238
1239#undef DECL_TYPE
1240#undef STRUCT_TYPE
1241#endif /* __cplusplus */
1242
1243struct glsl_struct_field {
1244 const struct glsl_type *type;
1245 const char *name;
1246
1247 /**
1248 * For interface blocks, gl_varying_slot corresponding to the input/output
1249 * if this is a built-in input/output (i.e. a member of the built-in
1250 * gl_PerVertex interface block); -1 otherwise.
1251 *
1252 * Ignored for structs.
1253 */
1254 int location;
1255
1256 /**
1257 * For interface blocks, members may have an explicit byte offset
1258 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
1259 *
1260 * Unless used for xfb_offset this field is ignored for structs.
1261 */
1262 int offset;
1263
1264 /**
1265 * For interface blocks, members may define a transform feedback buffer;
1266 * -1 otherwise.
1267 */
1268 int xfb_buffer;
1269
1270 /**
1271 * For interface blocks, members may define a transform feedback stride;
1272 * -1 otherwise.
1273 */
1274 int xfb_stride;
1275
1276 /**
1277 * For interface blocks, the interpolation mode (as in
1278 * ir_variable::interpolation). 0 otherwise.
1279 */
1280 unsigned interpolation:3;
1281
1282 /**
1283 * For interface blocks, 1 if this variable uses centroid interpolation (as
1284 * in ir_variable::centroid). 0 otherwise.
1285 */
1286 unsigned centroid:1;
1287
1288 /**
1289 * For interface blocks, 1 if this variable uses sample interpolation (as
1290 * in ir_variable::sample). 0 otherwise.
1291 */
1292 unsigned sample:1;
1293
1294 /**
1295 * Layout of the matrix. Uses glsl_matrix_layout values.
1296 */
1297 unsigned matrix_layout:2;
1298
1299 /**
1300 * For interface blocks, 1 if this variable is a per-patch input or output
1301 * (as in ir_variable::patch). 0 otherwise.
1302 */
1303 unsigned patch:1;
1304
1305 /**
1306 * Precision qualifier
1307 */
1308 unsigned precision:2;
1309
1310 /**
1311 * Memory qualifiers, applicable to buffer variables defined in shader
1312 * storage buffer objects (SSBOs)
1313 */
1314 unsigned memory_read_only:1;
1315 unsigned memory_write_only:1;
1316 unsigned memory_coherent:1;
1317 unsigned memory_volatile:1;
1318 unsigned memory_restrict:1;
1319
1320 /**
1321 * Layout format, applicable to image variables only.
1322 */
1323 enum pipe_format image_format;
1324
1325 /**
1326 * Any of the xfb_* qualifiers trigger the shader to be in transform
1327 * feedback mode so we need to keep track of whether the buffer was
1328 * explicitly set or if its just been assigned the default global value.
1329 */
1330 unsigned explicit_xfb_buffer:1;
1331
1332 unsigned implicit_sized_array:1;
1333#ifdef __cplusplus201703L
1334#define DEFAULT_CONSTRUCTORS(_type, _precision, _name) \
1335 type(_type), name(_name), location(-1), offset(-1), xfb_buffer(0), \
1336 xfb_stride(0), interpolation(0), centroid(0), \
1337 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED), patch(0), \
1338 precision(_precision), memory_read_only(0), \
1339 memory_write_only(0), memory_coherent(0), memory_volatile(0), \
1340 memory_restrict(0), image_format(PIPE_FORMAT_NONE), \
1341 explicit_xfb_buffer(0), \
1342 implicit_sized_array(0)
1343
1344 glsl_struct_field(const struct glsl_type *_type,
1345 int _precision,
1346 const char *_name)
1347 : DEFAULT_CONSTRUCTORS(_type, _precision, _name)
1348 {
1349 /* empty */
1350 }
1351
1352 glsl_struct_field(const struct glsl_type *_type, const char *_name)
1353 : DEFAULT_CONSTRUCTORS(_type, GLSL_PRECISION_NONE, _name)
1354 {
1355 /* empty */
1356 }
1357
1358 glsl_struct_field()
1359 : DEFAULT_CONSTRUCTORS(NULL__null, GLSL_PRECISION_NONE, NULL__null)
1360 {
1361 /* empty */
1362 }
1363#undef DEFAULT_CONSTRUCTORS
1364#endif
1365};
1366
1367struct glsl_function_param {
1368 const struct glsl_type *type;
1369
1370 bool in;
1371 bool out;
1372};
1373
1374static inline unsigned int
1375glsl_align(unsigned int a, unsigned int align)
1376{
1377 return (a + align - 1) / align * align;
32
Division by zero
1378}
1379
1380#endif /* GLSL_TYPES_H */