2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #include "main/core.h"
27 #include "ir_uniform.h"
28 #include "glsl_symbol_table.h"
29 #include "program/hash_table.h"
31 /* These functions are put in a "private" namespace instead of being marked
32 * static so that the unit tests can access them. See
33 * http://code.google.com/p/googletest/wiki/AdvancedGuide#Testing_Private_Code
38 get_storage(gl_uniform_storage
*storage
, unsigned num_storage
,
41 for (unsigned int i
= 0; i
< num_storage
; i
++) {
42 if (strcmp(name
, storage
[i
].name
) == 0)
50 copy_constant_to_storage(union gl_constant_value
*storage
,
51 const ir_constant
*val
,
52 const enum glsl_base_type base_type
,
53 const unsigned int elements
)
55 for (unsigned int i
= 0; i
< elements
; i
++) {
58 storage
[i
].u
= val
->value
.u
[i
];
61 case GLSL_TYPE_SAMPLER
:
62 storage
[i
].i
= val
->value
.i
[i
];
65 storage
[i
].f
= val
->value
.f
[i
];
68 storage
[i
].b
= int(val
->value
.b
[i
]);
71 case GLSL_TYPE_STRUCT
:
72 case GLSL_TYPE_INTERFACE
:
75 /* All other types should have already been filtered by other
76 * paths in the caller.
78 assert(!"Should not get here.");
85 set_uniform_initializer(void *mem_ctx
, gl_shader_program
*prog
,
86 const char *name
, const glsl_type
*type
,
89 if (type
->is_record()) {
90 ir_constant
*field_constant
;
92 field_constant
= (ir_constant
*)val
->components
.get_head();
94 for (unsigned int i
= 0; i
< type
->length
; i
++) {
95 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
96 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
97 type
->fields
.structure
[i
].name
);
98 set_uniform_initializer(mem_ctx
, prog
, field_name
,
99 field_type
, field_constant
);
100 field_constant
= (ir_constant
*)field_constant
->next
;
103 } else if (type
->is_array() && type
->fields
.array
->is_record()) {
104 const glsl_type
*const element_type
= type
->fields
.array
;
106 for (unsigned int i
= 0; i
< type
->length
; i
++) {
107 const char *element_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
109 set_uniform_initializer(mem_ctx
, prog
, element_name
,
110 element_type
, val
->array_elements
[i
]);
115 struct gl_uniform_storage
*const storage
=
116 get_storage(prog
->UniformStorage
,
117 prog
->NumUserUniformStorage
,
119 if (storage
== NULL
) {
120 assert(storage
!= NULL
);
124 if (val
->type
->is_array()) {
125 const enum glsl_base_type base_type
=
126 val
->array_elements
[0]->type
->base_type
;
127 const unsigned int elements
= val
->array_elements
[0]->type
->components();
128 unsigned int idx
= 0;
130 assert(val
->type
->length
>= storage
->array_elements
);
131 for (unsigned int i
= 0; i
< storage
->array_elements
; i
++) {
132 copy_constant_to_storage(& storage
->storage
[idx
],
133 val
->array_elements
[i
],
140 if (base_type
== GLSL_TYPE_SAMPLER
) {
141 for (unsigned int i
= 0; i
< storage
->array_elements
; i
++) {
142 prog
->SamplerUnits
[storage
->sampler
+ i
] = storage
->storage
[i
].i
;
146 copy_constant_to_storage(storage
->storage
,
148 val
->type
->base_type
,
149 val
->type
->components());
151 if (storage
->type
->is_sampler())
152 prog
->SamplerUnits
[storage
->sampler
] = storage
->storage
[0].i
;
155 storage
->initialized
= true;
160 link_set_uniform_initializers(struct gl_shader_program
*prog
)
162 void *mem_ctx
= NULL
;
164 for (unsigned int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
165 struct gl_shader
*shader
= prog
->_LinkedShaders
[i
];
170 foreach_list(node
, shader
->ir
) {
171 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
173 if (!var
|| var
->mode
!= ir_var_uniform
|| !var
->constant_value
)
177 mem_ctx
= ralloc_context(NULL
);
179 linker::set_uniform_initializer(mem_ctx
, prog
, var
->name
,
180 var
->type
, var
->constant_value
);
184 ralloc_free(mem_ctx
);