2 * Copyright © 2018 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "gl_nir_linker.h"
26 #include "compiler/glsl/ir_uniform.h" /* for gl_uniform_storage */
27 #include "main/context.h"
28 #include "main/mtypes.h"
30 struct set_opaque_binding_closure
{
31 struct gl_shader_program
*shader_prog
;
32 struct gl_program
*prog
;
33 const nir_variable
*var
;
39 set_opaque_binding(struct set_opaque_binding_closure
*data
,
40 const struct glsl_type
*type
)
42 if (glsl_type_is_array(type
) &&
43 glsl_type_is_array(glsl_get_array_element(type
))) {
44 const struct glsl_type
*element_type
= glsl_get_array_element(type
);
46 for (unsigned int i
= 0; i
< glsl_get_length(type
); i
++)
47 set_opaque_binding(data
, element_type
);
52 if (data
->location
< 0 ||
53 data
->location
>= data
->prog
->sh
.data
->NumUniformStorage
)
56 struct gl_uniform_storage
*storage
=
57 data
->prog
->sh
.data
->UniformStorage
+ data
->location
++;
59 const unsigned elements
= MAX2(storage
->array_elements
, 1);
61 for (unsigned int i
= 0; i
< elements
; i
++)
62 storage
->storage
[i
].i
= data
->binding
++;
64 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
65 struct gl_linked_shader
*shader
= data
->shader_prog
->_LinkedShaders
[sh
];
69 if (!storage
->opaque
[sh
].active
)
72 if (glsl_type_is_sampler(storage
->type
)) {
73 for (unsigned i
= 0; i
< elements
; i
++) {
74 const unsigned index
= storage
->opaque
[sh
].index
+ i
;
76 if (storage
->is_bindless
) {
77 if (index
>= shader
->Program
->sh
.NumBindlessSamplers
)
79 shader
->Program
->sh
.BindlessSamplers
[index
].unit
=
80 storage
->storage
[i
].i
;
81 shader
->Program
->sh
.BindlessSamplers
[index
].bound
= true;
82 shader
->Program
->sh
.HasBoundBindlessSampler
= true;
84 if (index
>= ARRAY_SIZE(shader
->Program
->SamplerUnits
))
86 shader
->Program
->SamplerUnits
[index
] =
87 storage
->storage
[i
].i
;
90 } else if (glsl_type_is_image(type
)) {
91 for (unsigned i
= 0; i
< elements
; i
++) {
92 const unsigned index
= storage
->opaque
[sh
].index
+ i
;
94 if (storage
->is_bindless
) {
95 if (index
>= shader
->Program
->sh
.NumBindlessImages
)
97 shader
->Program
->sh
.BindlessImages
[index
].unit
=
98 storage
->storage
[i
].i
;
99 shader
->Program
->sh
.BindlessImages
[index
].bound
= true;
100 shader
->Program
->sh
.HasBoundBindlessImage
= true;
102 if (index
>= ARRAY_SIZE(shader
->Program
->sh
.ImageUnits
))
104 shader
->Program
->sh
.ImageUnits
[index
] =
105 storage
->storage
[i
].i
;
113 copy_constant_to_storage(union gl_constant_value
*storage
,
114 const nir_constant
*val
,
115 const struct glsl_type
*type
,
116 unsigned int boolean_true
)
118 const enum glsl_base_type base_type
= glsl_get_base_type(type
);
119 const unsigned n_columns
= glsl_get_matrix_columns(type
);
120 const unsigned n_rows
= glsl_get_vector_elements(type
);
123 for (unsigned int column
= 0; column
< n_columns
; column
++) {
124 for (unsigned int row
= 0; row
< n_rows
; row
++) {
127 storage
[i
].u
= val
->values
[column
].u32
[row
];
130 case GLSL_TYPE_SAMPLER
:
131 storage
[i
].i
= val
->values
[column
].i32
[row
];
133 case GLSL_TYPE_FLOAT
:
134 storage
[i
].f
= val
->values
[column
].f32
[row
];
136 case GLSL_TYPE_DOUBLE
:
137 case GLSL_TYPE_UINT64
:
138 case GLSL_TYPE_INT64
:
139 /* XXX need to check on big-endian */
140 memcpy(&storage
[i
* 2].u
,
141 &val
->values
[column
].f64
[row
],
145 storage
[i
].b
= val
->values
[column
].u32
[row
] ? boolean_true
: 0;
147 case GLSL_TYPE_ARRAY
:
148 case GLSL_TYPE_STRUCT
:
149 case GLSL_TYPE_IMAGE
:
150 case GLSL_TYPE_ATOMIC_UINT
:
151 case GLSL_TYPE_INTERFACE
:
153 case GLSL_TYPE_SUBROUTINE
:
154 case GLSL_TYPE_FUNCTION
:
155 case GLSL_TYPE_ERROR
:
156 case GLSL_TYPE_UINT16
:
157 case GLSL_TYPE_INT16
:
158 case GLSL_TYPE_UINT8
:
160 case GLSL_TYPE_FLOAT16
:
161 /* All other types should have already been filtered by other
162 * paths in the caller.
164 assert(!"Should not get here.");
172 struct set_uniform_initializer_closure
{
173 struct gl_shader_program
*shader_prog
;
174 struct gl_program
*prog
;
175 const nir_variable
*var
;
177 unsigned int boolean_true
;
181 set_uniform_initializer(struct set_uniform_initializer_closure
*data
,
182 const struct glsl_type
*type
,
183 const nir_constant
*val
)
185 const struct glsl_type
*t_without_array
= glsl_without_array(type
);
187 if (glsl_type_is_struct_or_ifc(type
)) {
188 for (unsigned int i
= 0; i
< glsl_get_length(type
); i
++) {
189 const struct glsl_type
*field_type
= glsl_get_struct_field(type
, i
);
190 set_uniform_initializer(data
, field_type
, val
->elements
[i
]);
195 if (glsl_type_is_struct_or_ifc(t_without_array
) ||
196 (glsl_type_is_array(type
) &&
197 glsl_type_is_array(glsl_get_array_element(type
)))) {
198 const struct glsl_type
*element_type
= glsl_get_array_element(type
);
200 for (unsigned int i
= 0; i
< glsl_get_length(type
); i
++)
201 set_uniform_initializer(data
, element_type
, val
->elements
[i
]);
206 if (data
->location
< 0 ||
207 data
->location
>= data
->prog
->sh
.data
->NumUniformStorage
)
210 struct gl_uniform_storage
*storage
=
211 data
->prog
->sh
.data
->UniformStorage
+ data
->location
++;
213 if (glsl_type_is_array(type
)) {
214 const struct glsl_type
*element_type
= glsl_get_array_element(type
);
215 const enum glsl_base_type base_type
= glsl_get_base_type(element_type
);
216 const unsigned int elements
= glsl_get_components(element_type
);
217 unsigned int idx
= 0;
218 unsigned dmul
= glsl_base_type_is_64bit(base_type
) ? 2 : 1;
220 assert(glsl_get_length(type
) >= storage
->array_elements
);
221 for (unsigned int i
= 0; i
< storage
->array_elements
; i
++) {
222 copy_constant_to_storage(&storage
->storage
[idx
],
227 idx
+= elements
* dmul
;
230 copy_constant_to_storage(storage
->storage
,
235 if (glsl_type_is_sampler(storage
->type
)) {
236 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
237 struct gl_linked_shader
*shader
=
238 data
->shader_prog
->_LinkedShaders
[sh
];
240 if (shader
&& storage
->opaque
[sh
].active
) {
241 unsigned index
= storage
->opaque
[sh
].index
;
243 shader
->Program
->SamplerUnits
[index
] = storage
->storage
[0].i
;
251 gl_nir_set_uniform_initializers(struct gl_context
*ctx
,
252 struct gl_shader_program
*prog
)
254 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
255 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
259 nir_shader
*nir
= sh
->Program
->nir
;
262 nir_foreach_variable(var
, &nir
->uniforms
) {
263 if (var
->constant_initializer
) {
264 struct set_uniform_initializer_closure data
= {
268 .location
= var
->data
.location
,
269 .boolean_true
= ctx
->Const
.UniformBooleanTrue
271 set_uniform_initializer(&data
,
273 var
->constant_initializer
);
274 } else if (var
->data
.explicit_binding
) {
275 const struct glsl_type
*without_array
=
276 glsl_without_array(var
->type
);
278 if (glsl_type_is_sampler(without_array
) ||
279 glsl_type_is_image(without_array
)) {
280 struct set_opaque_binding_closure data
= {
284 .binding
= var
->data
.binding
,
285 .location
= var
->data
.location
287 set_opaque_binding(&data
, var
->type
);