+
+/**
+ * Merges a uniform block into an array of uniform blocks that may or
+ * may not already contain a copy of it.
+ *
+ * Returns the index of the new block in the array.
+ */
+int
+link_cross_validate_uniform_block(void *mem_ctx,
+ struct gl_uniform_block **linked_blocks,
+ unsigned int *num_linked_blocks,
+ struct gl_uniform_block *new_block)
+{
+ for (unsigned int i = 0; i < *num_linked_blocks; i++) {
+ struct gl_uniform_block *old_block = &(*linked_blocks)[i];
+
+ if (strcmp(old_block->Name, new_block->Name) == 0)
+ return link_uniform_blocks_are_compatible(old_block, new_block)
+ ? i : -1;
+ }
+
+ *linked_blocks = reralloc(mem_ctx, *linked_blocks,
+ struct gl_uniform_block,
+ *num_linked_blocks + 1);
+ int linked_block_index = (*num_linked_blocks)++;
+ struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
+
+ memcpy(linked_block, new_block, sizeof(*new_block));
+ linked_block->Uniforms = ralloc_array(*linked_blocks,
+ struct gl_uniform_buffer_variable,
+ linked_block->NumUniforms);
+
+ memcpy(linked_block->Uniforms,
+ new_block->Uniforms,
+ sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
+
+ for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
+ struct gl_uniform_buffer_variable *ubo_var =
+ &linked_block->Uniforms[i];
+
+ if (ubo_var->Name == ubo_var->IndexName) {
+ ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
+ ubo_var->IndexName = ubo_var->Name;
+ } else {
+ ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
+ ubo_var->IndexName = ralloc_strdup(*linked_blocks, ubo_var->IndexName);
+ }
+ }
+
+ return linked_block_index;
+}
+
+/**
+ * Walks the IR and update the references to uniform blocks in the
+ * ir_variables to point at linked shader's list (previously, they
+ * would point at the uniform block list in one of the pre-linked
+ * shaders).
+ */
+static void
+link_update_uniform_buffer_variables(struct gl_shader *shader)
+{
+ foreach_list(node, shader->ir) {
+ ir_variable *const var = ((ir_instruction *) node)->as_variable();
+
+ if ((var == NULL) || !var->is_in_uniform_block())
+ continue;
+
+ assert(var->mode == ir_var_uniform);
+
+ if (var->is_interface_instance()) {
+ var->location = 0;
+ continue;
+ }
+
+ bool found = false;
+ char sentinel = '\0';
+
+ if (var->type->is_record()) {
+ sentinel = '.';
+ } else if (var->type->is_array()
+ && var->type->fields.array->is_record()) {
+ sentinel = '[';
+ }
+
+ const unsigned l = strlen(var->name);
+ for (unsigned i = 0; i < shader->NumUniformBlocks; i++) {
+ for (unsigned j = 0; j < shader->UniformBlocks[i].NumUniforms; j++) {
+ if (sentinel) {
+ const char *begin = shader->UniformBlocks[i].Uniforms[j].Name;
+ const char *end = strchr(begin, sentinel);
+
+ if (end == NULL)
+ continue;
+
+ if (l != (end - begin))
+ continue;
+
+ if (strncmp(var->name, begin, l) == 0) {
+ found = true;
+ var->location = j;
+ break;
+ }
+ } else if (!strcmp(var->name,
+ shader->UniformBlocks[i].Uniforms[j].Name)) {
+ found = true;
+ var->location = j;
+ break;
+ }
+ }
+ if (found)
+ break;
+ }
+ assert(found);
+ }
+}
+
+void
+link_assign_uniform_block_offsets(struct gl_shader *shader)
+{
+ for (unsigned b = 0; b < shader->NumUniformBlocks; b++) {
+ struct gl_uniform_block *block = &shader->UniformBlocks[b];
+
+ unsigned offset = 0;
+ for (unsigned int i = 0; i < block->NumUniforms; i++) {
+ struct gl_uniform_buffer_variable *ubo_var = &block->Uniforms[i];
+ const struct glsl_type *type = ubo_var->Type;
+
+ unsigned alignment = type->std140_base_alignment(ubo_var->RowMajor);
+ unsigned size = type->std140_size(ubo_var->RowMajor);
+
+ offset = glsl_align(offset, alignment);
+ ubo_var->Offset = offset;
+ offset += size;
+ }
+
+ /* From the GL_ARB_uniform_buffer_object spec:
+ *
+ * "For uniform blocks laid out according to [std140] rules,
+ * the minimum buffer object size returned by the
+ * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
+ * offset of the last basic machine unit consumed by the
+ * last uniform of the uniform block (including any
+ * end-of-array or end-of-structure padding), adding one,
+ * and rounding up to the next multiple of the base
+ * alignment required for a vec4."
+ */
+ block->UniformBufferSize = glsl_align(offset, 16);
+ }
+}
+
+void
+link_assign_uniform_locations(struct gl_shader_program *prog)
+{
+ ralloc_free(prog->UniformStorage);
+ prog->UniformStorage = NULL;
+ prog->NumUserUniformStorage = 0;
+
+ if (prog->UniformHash != NULL) {
+ prog->UniformHash->clear();
+ } else {
+ prog->UniformHash = new string_to_uint_map;
+ }
+
+ /* Uniforms that lack an initializer in the shader code have an initial
+ * value of zero. This includes sampler uniforms.
+ *
+ * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
+ *
+ * "The link time initial value is either the value of the variable's
+ * initializer, if present, or 0 if no initializer is present. Sampler
+ * types cannot have initializers."
+ */
+ memset(prog->SamplerUnits, 0, sizeof(prog->SamplerUnits));
+
+ /* First pass: Count the uniform resources used by the user-defined
+ * uniforms. While this happens, each active uniform will have an index
+ * assigned to it.
+ *
+ * Note: this is *NOT* the index that is returned to the application by
+ * glGetUniformLocation.
+ */
+ count_uniform_size uniform_size(prog->UniformHash);
+ for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
+ if (prog->_LinkedShaders[i] == NULL)
+ continue;
+
+ link_update_uniform_buffer_variables(prog->_LinkedShaders[i]);
+
+ /* Reset various per-shader target counts.
+ */
+ uniform_size.start_shader();
+
+ foreach_list(node, prog->_LinkedShaders[i]->ir) {
+ ir_variable *const var = ((ir_instruction *) node)->as_variable();
+
+ if ((var == NULL) || (var->mode != ir_var_uniform))
+ continue;
+
+ /* FINISHME: Update code to process built-in uniforms!
+ */
+ if (strncmp("gl_", var->name, 3) == 0) {
+ uniform_size.num_shader_uniform_components +=
+ var->type->component_slots();
+ continue;
+ }
+
+ uniform_size.process(var);
+ }
+
+ prog->_LinkedShaders[i]->num_samplers = uniform_size.num_shader_samplers;
+ prog->_LinkedShaders[i]->num_uniform_components =
+ uniform_size.num_shader_uniform_components;
+ }
+
+ const unsigned num_user_uniforms = uniform_size.num_active_uniforms;
+ const unsigned num_data_slots = uniform_size.num_values;
+
+ /* On the outside chance that there were no uniforms, bail out.
+ */
+ if (num_user_uniforms == 0)
+ return;
+
+ struct gl_uniform_storage *uniforms =
+ rzalloc_array(prog, struct gl_uniform_storage, num_user_uniforms);
+ union gl_constant_value *data =
+ rzalloc_array(uniforms, union gl_constant_value, num_data_slots);
+#ifndef NDEBUG
+ union gl_constant_value *data_end = &data[num_data_slots];
+#endif
+
+ parcel_out_uniform_storage parcel(prog->UniformHash, uniforms, data);
+
+ for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
+ if (prog->_LinkedShaders[i] == NULL)
+ continue;
+
+ /* Reset various per-shader target counts.
+ */
+ parcel.start_shader();
+
+ foreach_list(node, prog->_LinkedShaders[i]->ir) {
+ ir_variable *const var = ((ir_instruction *) node)->as_variable();
+
+ if ((var == NULL) || (var->mode != ir_var_uniform))
+ continue;
+
+ /* FINISHME: Update code to process built-in uniforms!
+ */
+ if (strncmp("gl_", var->name, 3) == 0)
+ continue;
+
+ parcel.set_and_process(prog, var);
+ }
+
+ prog->_LinkedShaders[i]->active_samplers = parcel.shader_samplers_used;
+ prog->_LinkedShaders[i]->shadow_samplers = parcel.shader_shadow_samplers;
+ }
+
+ assert(sizeof(prog->SamplerTargets) == sizeof(parcel.targets));
+ memcpy(prog->SamplerTargets, parcel.targets, sizeof(prog->SamplerTargets));
+
+#ifndef NDEBUG
+ for (unsigned i = 0; i < num_user_uniforms; i++) {
+ assert(uniforms[i].storage != NULL);
+ }
+
+ assert(parcel.values == data_end);
+#endif
+
+ prog->NumUserUniformStorage = num_user_uniforms;
+ prog->UniformStorage = uniforms;
+
+ link_set_uniform_initializers(prog);
+
+ return;
+}