* DEALINGS IN THE SOFTWARE.
*/
-#include "main/core.h"
#include "ir.h"
#include "linker.h"
#include "ir_uniform.h"
#include "glsl_symbol_table.h"
#include "program.h"
-#include "util/string_to_uint_map.h"
+#include "string_to_uint_map.h"
#include "ir_array_refcount.h"
+#include "main/mtypes.h"
/**
* \file link_uniforms.cpp
#define UNMAPPED_UNIFORM_LOC ~0u
void
-program_resource_visitor::process(const glsl_type *type, const char *name)
+program_resource_visitor::process(const glsl_type *type, const char *name,
+ bool use_std430_as_default)
{
assert(type->without_array()->is_record()
|| type->without_array()->is_interface());
unsigned record_array_count = 1;
char *name_copy = ralloc_strdup(NULL, name);
- enum glsl_interface_packing packing = type->get_interface_packing();
+
+ enum glsl_interface_packing packing =
+ type->get_internal_ifc_packing(use_std430_as_default);
recursion(type, &name_copy, strlen(name), false, NULL, packing, false,
record_array_count, NULL);
}
void
-program_resource_visitor::process(ir_variable *var)
+program_resource_visitor::process(ir_variable *var, bool use_std430_as_default)
{
unsigned record_array_count = 1;
const bool row_major =
var->data.matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
- const enum glsl_interface_packing packing = var->get_interface_type() ?
- var->get_interface_type_packing() :
- var->type->get_interface_packing();
+ enum glsl_interface_packing packing = var->get_interface_type() ?
+ var->get_interface_type()->
+ get_internal_ifc_packing(use_std430_as_default) :
+ var->type->get_internal_ifc_packing(use_std430_as_default);
const glsl_type *t =
var->data.from_named_ifc_block ? var->get_interface_type() : var->type;
const char *field = t->fields.structure[i].name;
size_t new_length = name_length;
- if (t->fields.structure[i].type->is_record())
- this->visit_field(&t->fields.structure[i]);
-
if (t->is_interface() && t->fields.structure[i].offset != -1)
this->set_buffer_offset(t->fields.structure[i].offset);
}
}
-void
-program_resource_visitor::visit_field(const glsl_struct_field *)
-{
-}
-
void
program_resource_visitor::enter_record(const glsl_type *, const char *, bool,
const enum glsl_interface_packing)
class count_uniform_size : public program_resource_visitor {
public:
count_uniform_size(struct string_to_uint_map *map,
- struct string_to_uint_map *hidden_map)
+ struct string_to_uint_map *hidden_map,
+ bool use_std430_as_default)
: num_active_uniforms(0), num_hidden_uniforms(0), num_values(0),
num_shader_samplers(0), num_shader_images(0),
num_shader_uniform_components(0), num_shader_subroutines(0),
is_buffer_block(false), is_shader_storage(false), map(map),
- hidden_map(hidden_map), current_var(NULL)
+ hidden_map(hidden_map), current_var(NULL),
+ use_std430_as_default(use_std430_as_default)
{
/* empty */
}
this->is_shader_storage = var->is_in_shader_storage_block();
if (var->is_interface_instance())
program_resource_visitor::process(var->get_interface_type(),
- var->get_interface_type()->name);
+ var->get_interface_type()->name,
+ use_std430_as_default);
else
- program_resource_visitor::process(var);
+ program_resource_visitor::process(var, use_std430_as_default);
}
/**
* Current variable being processed.
*/
ir_variable *current_var;
+
+ bool use_std430_as_default;
};
} /* anonymous namespace */
+unsigned
+link_calculate_matrix_stride(const glsl_type *matrix, bool row_major,
+ enum glsl_interface_packing packing)
+{
+ const unsigned N = matrix->is_double() ? 8 : 4;
+ const unsigned items =
+ row_major ? matrix->matrix_columns : matrix->vector_elements;
+
+ assert(items <= 4);
+
+ /* Matrix stride for std430 mat2xY matrices are not rounded up to
+ * vec4 size.
+ *
+ * Section 7.6.2.2 "Standard Uniform Block Layout" of the OpenGL 4.3 spec
+ * says:
+ *
+ * 2. If the member is a two- or four-component vector with components
+ * consuming N basic machine units, the base alignment is 2N or 4N,
+ * respectively.
+ * ...
+ * 4. If the member is an array of scalars or vectors, the base
+ * alignment and array stride are set to match the base alignment of
+ * a single array element, according to rules (1), (2), and (3), and
+ * rounded up to the base alignment of a vec4.
+ * ...
+ * 7. If the member is a row-major matrix with C columns and R rows, the
+ * matrix is stored identically to an array of R row vectors with C
+ * components each, according to rule (4).
+ * ...
+ *
+ * When using the std430 storage layout, shader storage blocks will be
+ * laid out in buffer storage identically to uniform and shader storage
+ * blocks using the std140 layout, except that the base alignment and
+ * stride of arrays of scalars and vectors in rule 4 and of structures
+ * in rule 9 are not rounded up a multiple of the base alignment of a
+ * vec4.
+ */
+ return packing == GLSL_INTERFACE_PACKING_STD430
+ ? (items < 3 ? items * N : glsl_align(items * N, 16))
+ : glsl_align(items * N, 16);
+}
+
/**
* Class to help parcel out pieces of backing storage to uniforms
*
parcel_out_uniform_storage(struct gl_shader_program *prog,
struct string_to_uint_map *map,
struct gl_uniform_storage *uniforms,
- union gl_constant_value *values)
- : prog(prog), map(map), uniforms(uniforms), values(values),
+ union gl_constant_value *values,
+ bool use_std430_as_default)
+ : prog(prog), map(map), uniforms(uniforms),
+ use_std430_as_default(use_std430_as_default), values(values),
bindless_targets(NULL), bindless_access(NULL)
{
}
if (var->is_interface_instance()) {
ubo_byte_offset = 0;
process(var->get_interface_type(),
- var->get_interface_type()->name);
+ var->get_interface_type()->name,
+ use_std430_as_default);
} else {
const struct gl_uniform_block *const block =
&blks[buffer_block_index];
&block->Uniforms[var->data.location];
ubo_byte_offset = ubo_var->Offset;
- process(var);
+ process(var, use_std430_as_default);
}
} else {
/* Store any explicit location and reset data location so we can
this->explicit_location = current_var->data.location;
current_var->data.location = -1;
- process(var);
+ process(var, use_std430_as_default);
}
delete this->record_next_sampler;
delete this->record_next_bindless_sampler;
/* Set image access qualifiers */
const GLenum access =
- (current_var->data.memory_read_only ? GL_READ_ONLY :
- current_var->data.memory_write_only ? GL_WRITE_ONLY :
- GL_READ_WRITE);
+ current_var->data.memory_read_only ?
+ (current_var->data.memory_write_only ? GL_NONE :
+ GL_READ_ONLY) :
+ (current_var->data.memory_write_only ? GL_WRITE_ONLY :
+ GL_READ_WRITE);
if (current_var->data.bindless) {
if (!set_opaque_indices(base_type, uniform, name,
this->uniforms[id].opaque[shader_type].index = ~0;
this->uniforms[id].opaque[shader_type].active = false;
+ this->uniforms[id].active_shader_mask |= 1 << shader_type;
+
/* This assigns uniform indices to sampler and image uniforms. */
handle_samplers(base_type, &this->uniforms[id], name);
handle_images(base_type, &this->uniforms[id], name);
}
if (type->without_array()->is_matrix()) {
- const glsl_type *matrix = type->without_array();
- const unsigned N = matrix->is_double() ? 8 : 4;
- const unsigned items =
- row_major ? matrix->matrix_columns : matrix->vector_elements;
-
- assert(items <= 4);
- if (packing == GLSL_INTERFACE_PACKING_STD430)
- this->uniforms[id].matrix_stride = items < 3 ? items * N :
- glsl_align(items * N, 16);
- else
- this->uniforms[id].matrix_stride = glsl_align(items * N, 16);
+ this->uniforms[id].matrix_stride =
+ link_calculate_matrix_stride(type->without_array(),
+ row_major,
+ packing);
this->uniforms[id].row_major = row_major;
} else {
this->uniforms[id].matrix_stride = 0;
unsigned next_bindless_image;
unsigned next_subroutine;
+ bool use_std430_as_default;
+
/**
* Field counter is used to take care that uniform structures
* with explicit locations get sequential locations.
uniform_size->map->put(hidden_uniform_start + hidden_id, name);
}
-/**
- * Search through the list of empty blocks to find one that fits the current
- * uniform.
- */
-static int
-find_empty_block(struct gl_shader_program *prog,
- struct gl_uniform_storage *uniform)
-{
- const unsigned entries = MAX2(1, uniform->array_elements);
-
- foreach_list_typed(struct empty_uniform_block, block, link,
- &prog->EmptyUniformLocations) {
- /* Found a block with enough slots to fit the uniform */
- if (block->slots == entries) {
- unsigned start = block->start;
- exec_node_remove(&block->link);
- ralloc_free(block);
-
- return start;
- /* Found a block with more slots than needed. It can still be used. */
- } else if (block->slots > entries) {
- unsigned start = block->start;
- block->start += entries;
- block->slots -= entries;
-
- return start;
- }
- }
-
- return -1;
-}
-
static void
link_setup_uniform_remap_tables(struct gl_context *ctx,
struct gl_shader_program *prog)
int chosen_location = -1;
if (empty_locs)
- chosen_location = find_empty_block(prog, &prog->data->UniformStorage[i]);
+ chosen_location = link_util_find_empty_block(prog, &prog->data->UniformStorage[i]);
- /* Add new entries to the total amount of entries. */
- total_entries += entries;
+ /* Add new entries to the total amount for checking against MAX_UNIFORM-
+ * _LOCATIONS. This only applies to the default uniform block (-1),
+ * because locations of uniform block entries are not assignable.
+ */
+ if (prog->data->UniformStorage[i].block_index == -1)
+ total_entries += entries;
if (chosen_location != -1) {
empty_locs -= entries;
union gl_constant_value *data;
if (prog->data->UniformStorage == NULL) {
- prog->data->UniformStorage = rzalloc_array(prog,
+ prog->data->UniformStorage = rzalloc_array(prog->data,
struct gl_uniform_storage,
prog->data->NumUniformStorage);
data = rzalloc_array(prog->data->UniformStorage,
union gl_constant_value, num_data_slots);
+ prog->data->UniformDataDefaults =
+ rzalloc_array(prog->data->UniformStorage,
+ union gl_constant_value, num_data_slots);
} else {
data = prog->data->UniformDataSlots;
}
#endif
parcel_out_uniform_storage parcel(prog, prog->UniformHash,
- prog->data->UniformStorage, data);
+ prog->data->UniformStorage, data,
+ ctx->Const.UseSTD430AsDefaultPacking);
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
struct gl_linked_shader *shader = prog->_LinkedShaders[i];
}
}
- STATIC_ASSERT(sizeof(shader->Program->sh.SamplerTargets) ==
- sizeof(parcel.targets));
- memcpy(shader->Program->sh.SamplerTargets,
- parcel.targets,
- sizeof(shader->Program->sh.SamplerTargets));
+ STATIC_ASSERT(ARRAY_SIZE(shader->Program->sh.SamplerTargets) ==
+ ARRAY_SIZE(parcel.targets));
+ for (unsigned j = 0; j < ARRAY_SIZE(parcel.targets); j++)
+ shader->Program->sh.SamplerTargets[j] = parcel.targets[j];
}
- /* If this is a fallback compile for a cache miss we already have the
- * correct uniform mappings and we don't want to reinitialise uniforms so
- * just return now.
- */
- if (prog->data->cache_fallback)
- return;
-
#ifndef NDEBUG
for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
assert(prog->data->UniformStorage[i].storage != NULL ||
link_assign_uniform_locations(struct gl_shader_program *prog,
struct gl_context *ctx)
{
- if (!prog->data->cache_fallback) {
- ralloc_free(prog->data->UniformStorage);
- prog->data->UniformStorage = NULL;
- prog->data->NumUniformStorage = 0;
- }
+ ralloc_free(prog->data->UniformStorage);
+ prog->data->UniformStorage = NULL;
+ prog->data->NumUniformStorage = 0;
if (prog->UniformHash != NULL) {
prog->UniformHash->clear();
* glGetUniformLocation.
*/
struct string_to_uint_map *hiddenUniforms = new string_to_uint_map;
- count_uniform_size uniform_size(prog->UniformHash, hiddenUniforms);
+ count_uniform_size uniform_size(prog->UniformHash, hiddenUniforms,
+ ctx->Const.UseSTD430AsDefaultPacking);
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
struct gl_linked_shader *sh = prog->_LinkedShaders[i];