[Mesa-dev] [PATCH 31/87] glsl: add shader cache support for buffer blocks
Timothy Arceri
timothy.arceri at collabora.com
Wed Jul 13 02:47:26 UTC 2016
---
src/compiler/glsl/shader_cache.cpp | 157 +++++++++++++++++++++++++++++++++++++
1 file changed, 157 insertions(+)
diff --git a/src/compiler/glsl/shader_cache.cpp b/src/compiler/glsl/shader_cache.cpp
index ed1c06f..cd92e24 100644
--- a/src/compiler/glsl/shader_cache.cpp
+++ b/src/compiler/glsl/shader_cache.cpp
@@ -189,6 +189,135 @@ read_subroutines(struct blob_reader *metadata, struct gl_shader_program *prog)
}
static void
+write_buffer_block(struct blob *metadata, struct gl_uniform_block *b)
+{
+ blob_write_string(metadata, b->Name);
+ blob_write_uint32(metadata, b->NumUniforms);
+ blob_write_uint32(metadata, b->Binding);
+ blob_write_uint32(metadata, b->UniformBufferSize);
+ blob_write_uint32(metadata, b->stageref);
+
+ for (unsigned j = 0; j < b->NumUniforms; j++) {
+ blob_write_string(metadata, b->Uniforms[j].Name);
+ blob_write_string(metadata, b->Uniforms[j].IndexName);
+ encode_type_to_blob(metadata, b->Uniforms[j].Type);
+ blob_write_uint32(metadata, b->Uniforms[j].Offset);
+ }
+}
+
+static void
+write_buffer_blocks(struct blob *metadata, struct gl_shader_program *prog)
+{
+ blob_write_uint32(metadata, prog->NumUniformBlocks);
+ blob_write_uint32(metadata, prog->NumShaderStorageBlocks);
+ blob_write_intptr(metadata, (intptr_t) prog->UniformBlocks);
+ blob_write_intptr(metadata, (intptr_t) prog->ShaderStorageBlocks);
+
+ for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
+ write_buffer_block(metadata, &prog->UniformBlocks[i]);
+ }
+
+ for (unsigned i = 0; i < prog->NumShaderStorageBlocks; i++) {
+ write_buffer_block(metadata, &prog->ShaderStorageBlocks[i]);
+ }
+
+ for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
+ struct gl_linked_shader *sh = prog->_LinkedShaders[i];
+ if (!sh)
+ continue;
+
+ blob_write_uint32(metadata, sh->NumUniformBlocks);
+ blob_write_uint32(metadata, sh->NumShaderStorageBlocks);
+
+ for (unsigned j = 0; j < sh->NumUniformBlocks; j++) {
+ blob_write_intptr(metadata, (intptr_t) sh->UniformBlocks[j]);
+ }
+
+ for (unsigned j = 0; j < sh->NumShaderStorageBlocks; j++) {
+ blob_write_intptr(metadata, (intptr_t) sh->ShaderStorageBlocks[j]);
+ }
+ }
+}
+
+static void
+read_buffer_block(struct blob_reader *metadata, struct gl_uniform_block *b,
+ struct gl_shader_program *prog)
+{
+ b->Name = ralloc_strdup(prog, blob_read_string (metadata));
+ b->NumUniforms = blob_read_uint32(metadata);
+ b->Binding = blob_read_uint32(metadata);
+ b->UniformBufferSize = blob_read_uint32(metadata);
+ b->stageref = blob_read_uint32(metadata);
+
+ b->Uniforms = rzalloc_array(prog, struct gl_uniform_buffer_variable,
+ b->NumUniforms);
+ for (unsigned j = 0; j < b->NumUniforms; j++) {
+ b->Uniforms[j].Name = ralloc_strdup(prog,
+ blob_read_string (metadata));
+
+ char *index_name = blob_read_string(metadata);
+ if (strcmp(b->Uniforms[j].Name, index_name) == 0) {
+ b->Uniforms[j].IndexName = b->Uniforms[j].Name;
+ } else {
+ b->Uniforms[j].IndexName = ralloc_strdup(prog, index_name);
+ }
+
+ b->Uniforms[j].Type = decode_type_from_blob(metadata);
+ b->Uniforms[j].Offset = blob_read_uint32(metadata);
+ }
+}
+
+static void
+read_buffer_blocks(struct blob_reader *metadata,
+ struct gl_shader_program *prog)
+{
+ prog->NumUniformBlocks = blob_read_uint32(metadata);
+ prog->NumShaderStorageBlocks = blob_read_uint32(metadata);
+ intptr_t ub_base = blob_read_intptr(metadata);
+ intptr_t sb_base = blob_read_intptr(metadata);
+
+ prog->UniformBlocks = rzalloc_array(prog, struct gl_uniform_block,
+ prog->NumUniformBlocks);
+
+ prog->ShaderStorageBlocks = rzalloc_array(prog, struct gl_uniform_block,
+ prog->NumShaderStorageBlocks);
+
+ for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
+ read_buffer_block(metadata, &prog->UniformBlocks[i], prog);
+ }
+
+ for (unsigned i = 0; i < prog->NumShaderStorageBlocks; i++) {
+ read_buffer_block(metadata, &prog->ShaderStorageBlocks[i], prog);
+ }
+
+ for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
+ struct gl_linked_shader *sh = prog->_LinkedShaders[i];
+ if (!sh)
+ continue;
+
+ sh->NumUniformBlocks = blob_read_uint32(metadata);
+ sh->NumShaderStorageBlocks = blob_read_uint32(metadata);
+
+ sh->UniformBlocks =
+ rzalloc_array(prog, gl_uniform_block *, sh->NumUniformBlocks);
+ sh->ShaderStorageBlocks =
+ rzalloc_array(prog, gl_uniform_block *, sh->NumShaderStorageBlocks);
+
+ for (unsigned j = 0; j < sh->NumUniformBlocks; j++) {
+ intptr_t ub = blob_read_intptr(metadata);
+ ptrdiff_t diff = (ub - ub_base) / sizeof(gl_uniform_block);
+ sh->UniformBlocks[j] = prog->UniformBlocks + diff;
+ }
+
+ for (unsigned j = 0; j < sh->NumShaderStorageBlocks; j++) {
+ intptr_t sb = blob_read_intptr(metadata);
+ ptrdiff_t diff = (sb - sb_base) / sizeof(gl_uniform_block);
+ sh->ShaderStorageBlocks[j] = prog->ShaderStorageBlocks + diff;
+ }
+ }
+}
+
+static void
write_uniforms(struct blob *metadata, struct gl_shader_program *prog)
{
uint32_t i;
@@ -445,6 +574,24 @@ write_program_resource_data(struct blob *metadata,
blob_write_uint32(metadata, data);
break;
}
+ case GL_UNIFORM_BLOCK:
+ for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
+ if (strcmp(((gl_uniform_block *)res->Data)->Name,
+ prog->UniformBlocks[i].Name) == 0) {
+ blob_write_uint32(metadata, i);
+ break;
+ }
+ }
+ break;
+ case GL_SHADER_STORAGE_BLOCK:
+ for (unsigned i = 0; i < prog->NumShaderStorageBlocks; i++) {
+ if (strcmp(((gl_uniform_block *)res->Data)->Name,
+ prog->ShaderStorageBlocks[i].Name) == 0) {
+ blob_write_uint32(metadata, i);
+ break;
+ }
+ }
+ break;
case GL_BUFFER_VARIABLE:
case GL_VERTEX_SUBROUTINE_UNIFORM:
case GL_GEOMETRY_SUBROUTINE_UNIFORM:
@@ -506,6 +653,12 @@ read_program_resource_data(struct blob_reader *metadata,
res->Data = var;
break;
}
+ case GL_UNIFORM_BLOCK:
+ res->Data = &prog->UniformBlocks[blob_read_uint32(metadata)];
+ break;
+ case GL_SHADER_STORAGE_BLOCK:
+ res->Data = &prog->ShaderStorageBlocks[blob_read_uint32(metadata)];
+ break;
case GL_BUFFER_VARIABLE:
case GL_VERTEX_SUBROUTINE_UNIFORM:
case GL_GEOMETRY_SUBROUTINE_UNIFORM:
@@ -742,6 +895,8 @@ shader_cache_write_program_metadata(struct gl_context *ctx,
write_uniform_remap_tables(metadata, prog);
+ write_buffer_blocks(metadata, prog);
+
write_subroutines(metadata, prog);
write_program_resource_list(metadata, prog);
@@ -849,6 +1004,8 @@ shader_cache_read_program_metadata(struct gl_context *ctx,
read_uniform_remap_tables(&metadata, prog);
+ read_buffer_blocks(&metadata, prog);
+
read_subroutines(&metadata, prog);
read_program_resource_list(&metadata, prog);
--
2.7.4
More information about the mesa-dev
mailing list