+ size_t new_length = name_length;
+
+ /* Append the subscript to the current variable name */
+ ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
+
+ recursion(t->fields.array, name, new_length,
+ t->fields.structure[i].row_major);
+ }
+ } else {
+ this->visit_field(t, *name, row_major);
+ }
+}
+
+void
+program_resource_visitor::visit_field(const glsl_struct_field *field)
+{
+ (void) field;
+ /* empty */
+}
+
+/**
+ * Class to help calculate the storage requirements for a set of uniforms
+ *
+ * As uniforms are added to the active set the number of active uniforms and
+ * the storage requirements for those uniforms are accumulated. The active
+ * uniforms are added the the hash table supplied to the constructor.
+ *
+ * If the same uniform is added multiple times (i.e., once for each shader
+ * target), it will only be accounted once.
+ */
+class count_uniform_size : public program_resource_visitor {
+public:
+ count_uniform_size(struct string_to_uint_map *map)
+ : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
+ num_shader_uniform_components(0), map(map)
+ {
+ /* empty */
+ }
+
+ void start_shader()
+ {
+ this->num_shader_samplers = 0;
+ this->num_shader_uniform_components = 0;
+ }
+
+ void process(ir_variable *var)
+ {
+ if (var->is_interface_instance())
+ program_resource_visitor::process(var->interface_type,
+ var->interface_type->name);
+ else
+ program_resource_visitor::process(var);
+ }
+
+ /**
+ * Total number of active uniforms counted
+ */
+ unsigned num_active_uniforms;
+
+ /**
+ * Number of data values required to back the storage for the active uniforms
+ */
+ unsigned num_values;
+
+ /**
+ * Number of samplers used
+ */
+ unsigned num_shader_samplers;
+
+ /**
+ * Number of uniforms used in the current shader
+ */
+ unsigned num_shader_uniform_components;
+
+private:
+ virtual void visit_field(const glsl_type *type, const char *name,
+ bool row_major)
+ {
+ assert(!type->is_record());
+ assert(!(type->is_array() && type->fields.array->is_record()));
+ assert(!type->is_interface());
+ assert(!(type->is_array() && type->fields.array->is_interface()));
+
+ (void) row_major;
+
+ /* Count the number of samplers regardless of whether the uniform is
+ * already in the hash table. The hash table prevents adding the same
+ * uniform for multiple shader targets, but in this case we want to
+ * count it for each shader target.
+ */
+ const unsigned values = values_for_type(type);
+ if (type->contains_sampler()) {
+ this->num_shader_samplers +=
+ type->is_array() ? type->array_size() : 1;
+ } else {
+ /* Accumulate the total number of uniform slots used by this shader.
+ * Note that samplers do not count against this limit because they
+ * don't use any storage on current hardware.
+ */
+ this->num_shader_uniform_components += values;
+ }
+
+ /* If the uniform is already in the map, there's nothing more to do.
+ */
+ unsigned id;
+ if (this->map->get(id, name))
+ return;
+
+ this->map->put(this->num_active_uniforms, name);
+
+ /* Each leaf uniform occupies one entry in the list of active
+ * uniforms.
+ */
+ this->num_active_uniforms++;
+ this->num_values += values;
+ }
+
+ struct string_to_uint_map *map;
+};
+
+/**
+ * Class to help parcel out pieces of backing storage to uniforms
+ *
+ * Each uniform processed has some range of the \c gl_constant_value
+ * structures associated with it. The association is done by finding
+ * the uniform in the \c string_to_uint_map and using the value from
+ * the map to connect that slot in the \c gl_uniform_storage table
+ * with the next available slot in the \c gl_constant_value array.
+ *
+ * \warning
+ * This class assumes that every uniform that will be processed is
+ * already in the \c string_to_uint_map. In addition, it assumes that
+ * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
+ * enough."
+ */
+class parcel_out_uniform_storage : public program_resource_visitor {
+public:
+ parcel_out_uniform_storage(struct string_to_uint_map *map,
+ struct gl_uniform_storage *uniforms,
+ union gl_constant_value *values)
+ : map(map), uniforms(uniforms), next_sampler(0), values(values)
+ {
+ memset(this->targets, 0, sizeof(this->targets));
+ }
+
+ void start_shader()
+ {
+ this->shader_samplers_used = 0;
+ this->shader_shadow_samplers = 0;
+ }
+
+ void set_and_process(struct gl_shader_program *prog,
+ ir_variable *var)
+ {
+ ubo_block_index = -1;
+ if (var->is_in_uniform_block()) {
+ if (var->is_interface_instance() && var->type->is_array()) {
+ unsigned l = strlen(var->interface_type->name);
+
+ for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
+ if (strncmp(var->interface_type->name,
+ prog->UniformBlocks[i].Name,
+ l) == 0
+ && prog->UniformBlocks[i].Name[l] == '[') {
+ ubo_block_index = i;
+ break;
+ }
+ }
+ } else {
+ for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
+ if (strcmp(var->interface_type->name,
+ prog->UniformBlocks[i].Name) == 0) {
+ ubo_block_index = i;
+ break;
+ }
+ }
+ }
+ assert(ubo_block_index != -1);
+
+ /* Uniform blocks that were specified with an instance name must be
+ * handled a little bit differently. The name of the variable is the
+ * name used to reference the uniform block instead of being the name
+ * of a variable within the block. Therefore, searching for the name
+ * within the block will fail.
+ */
+ if (var->is_interface_instance()) {
+ ubo_byte_offset = 0;
+ ubo_row_major = false;
+ } else {
+ const struct gl_uniform_block *const block =
+ &prog->UniformBlocks[ubo_block_index];
+
+ assert(var->location != -1);
+
+ const struct gl_uniform_buffer_variable *const ubo_var =
+ &block->Uniforms[var->location];
+
+ ubo_row_major = ubo_var->RowMajor;
+ ubo_byte_offset = ubo_var->Offset;
+ }
+
+ if (var->is_interface_instance())
+ process(var->interface_type, var->interface_type->name);
+ else
+ process(var);
+ } else
+ process(var);
+ }
+
+ int ubo_block_index;
+ int ubo_byte_offset;
+ bool ubo_row_major;
+
+private:
+ virtual void visit_field(const glsl_type *type, const char *name,
+ bool row_major)
+ {
+ assert(!type->is_record());
+ assert(!(type->is_array() && type->fields.array->is_record()));
+ assert(!type->is_interface());
+ assert(!(type->is_array() && type->fields.array->is_interface()));
+
+ (void) row_major;
+
+ unsigned id;
+ bool found = this->map->get(id, name);
+ assert(found);
+
+ if (!found)
+ return;
+
+ /* If there is already storage associated with this uniform, it means
+ * that it was set while processing an earlier shader stage. For
+ * example, we may be processing the uniform in the fragment shader, but
+ * the uniform was already processed in the vertex shader.
+ */
+ if (this->uniforms[id].storage != NULL) {
+ /* If the uniform already has storage set from another shader stage,
+ * mark the samplers used for this shader stage.
+ */
+ if (type->contains_sampler()) {
+ const unsigned count = MAX2(1, this->uniforms[id].array_elements);
+ const unsigned shadow = (type->is_array())
+ ? type->fields.array->sampler_shadow : type->sampler_shadow;
+
+ for (unsigned i = 0; i < count; i++) {
+ const unsigned s = this->uniforms[id].sampler + i;
+
+ this->shader_samplers_used |= 1U << s;
+ this->shader_shadow_samplers |= shadow << s;
+ }
+ }
+
+ return;
+ }
+
+ const glsl_type *base_type;
+ if (type->is_array()) {
+ this->uniforms[id].array_elements = type->length;
+ base_type = type->fields.array;
+ } else {
+ this->uniforms[id].array_elements = 0;
+ base_type = type;
+ }
+
+ if (base_type->is_sampler()) {
+ this->uniforms[id].sampler = this->next_sampler;
+
+ /* Increment the sampler by 1 for non-arrays and by the number of
+ * array elements for arrays.
+ */
+ this->next_sampler += MAX2(1, this->uniforms[id].array_elements);
+
+ const gl_texture_index target = base_type->sampler_index();
+ const unsigned shadow = base_type->sampler_shadow;
+ for (unsigned i = this->uniforms[id].sampler
+ ; i < MIN2(this->next_sampler, MAX_SAMPLERS)
+ ; i++) {
+ this->targets[i] = target;
+ this->shader_samplers_used |= 1U << i;
+ this->shader_shadow_samplers |= shadow << i;
+ }
+
+ } else {
+ this->uniforms[id].sampler = ~0;
+ }
+
+ this->uniforms[id].name = ralloc_strdup(this->uniforms, name);
+ this->uniforms[id].type = base_type;
+ this->uniforms[id].initialized = 0;
+ this->uniforms[id].num_driver_storage = 0;
+ this->uniforms[id].driver_storage = NULL;
+ this->uniforms[id].storage = this->values;
+ if (this->ubo_block_index != -1) {
+ this->uniforms[id].block_index = this->ubo_block_index;
+
+ unsigned alignment = type->std140_base_alignment(ubo_row_major);
+ this->ubo_byte_offset = glsl_align(this->ubo_byte_offset, alignment);
+ this->uniforms[id].offset = this->ubo_byte_offset;
+ this->ubo_byte_offset += type->std140_size(ubo_row_major);
+
+ if (type->is_array()) {
+ this->uniforms[id].array_stride =
+ glsl_align(type->fields.array->std140_size(ubo_row_major), 16);
+ } else {
+ this->uniforms[id].array_stride = 0;
+ }
+
+ if (type->is_matrix() ||
+ (type->is_array() && type->fields.array->is_matrix())) {
+ this->uniforms[id].matrix_stride = 16;
+ this->uniforms[id].row_major = ubo_row_major;
+ } else {
+ this->uniforms[id].matrix_stride = 0;
+ this->uniforms[id].row_major = false;
+ }
+ } else {
+ this->uniforms[id].block_index = -1;
+ this->uniforms[id].offset = -1;
+ this->uniforms[id].array_stride = -1;
+ this->uniforms[id].matrix_stride = -1;
+ this->uniforms[id].row_major = false;
+ }