diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2017-10-06 15:57:21 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2017-10-06 17:25:50 +0300 |
commit | f94f141f241608f6475082603df6f15d812b1ab7 (patch) | |
tree | 94aefceac7e753c572ba38c91820bfd68a694a59 | |
parent | ea606a7847a316a82b365155f666b33e81ff4c2e (diff) |
Gawain: Add UBOs to shader interface.
-rw-r--r-- | intern/gawain/gawain/gwn_shader_interface.h | 2 | ||||
-rw-r--r-- | intern/gawain/src/gwn_shader_interface.c | 34 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader.c | 3 |
3 files changed, 37 insertions, 2 deletions
diff --git a/intern/gawain/gawain/gwn_shader_interface.h b/intern/gawain/gawain/gwn_shader_interface.h index 1411bd24e7c..cb2ecb5a132 100644 --- a/intern/gawain/gawain/gwn_shader_interface.h +++ b/intern/gawain/gawain/gwn_shader_interface.h @@ -49,6 +49,7 @@ typedef struct Gwn_ShaderInterface { uint32_t name_buffer_offset; Gwn_ShaderInput* attrib_buckets[GWN_NUM_SHADERINTERFACE_BUCKETS]; Gwn_ShaderInput* uniform_buckets[GWN_NUM_SHADERINTERFACE_BUCKETS]; + Gwn_ShaderInput* ubo_buckets[GWN_NUM_SHADERINTERFACE_BUCKETS]; Gwn_ShaderInput* builtin_uniforms[GWN_NUM_UNIFORMS]; char* name_buffer; } Gwn_ShaderInterface; @@ -58,4 +59,5 @@ void GWN_shaderinterface_discard(Gwn_ShaderInterface*); const Gwn_ShaderInput* GWN_shaderinterface_uniform(const Gwn_ShaderInterface*, const char* name); const Gwn_ShaderInput* GWN_shaderinterface_uniform_builtin(const Gwn_ShaderInterface*, Gwn_UniformBuiltin); +const Gwn_ShaderInput* GWN_shaderinterface_ubo(const Gwn_ShaderInterface*, const char* name); const Gwn_ShaderInput* GWN_shaderinterface_attr(const Gwn_ShaderInterface*, const char* name); diff --git a/intern/gawain/src/gwn_shader_interface.c b/intern/gawain/src/gwn_shader_interface.c index 292e2148c8f..f4877c17087 100644 --- a/intern/gawain/src/gwn_shader_interface.c +++ b/intern/gawain/src/gwn_shader_interface.c @@ -198,9 +198,14 @@ Gwn_ShaderInterface* GWN_shaderinterface_create(GLint program) glGetProgramiv(program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &max_attrib_name_len); glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &attrib_ct); - const uint32_t name_buffer_len = attrib_ct * max_attrib_name_len; + GLint max_ubo_name_len, ubo_ct; + glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &max_ubo_name_len); + glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &ubo_ct); + + const uint32_t name_buffer_len = attrib_ct * max_attrib_name_len + ubo_ct * max_ubo_name_len; shaderface->name_buffer = malloc(name_buffer_len); + // Attributes for (uint32_t i = 0; i < attrib_ct; ++i) { Gwn_ShaderInput* input = malloc(sizeof(Gwn_ShaderInput)); @@ -223,6 +228,27 @@ Gwn_ShaderInterface* GWN_shaderinterface_create(GLint program) #endif } + // Uniform Blocks + for (uint32_t i = 0; i < ubo_ct; ++i) + { + Gwn_ShaderInput* input = malloc(sizeof(Gwn_ShaderInput)); + GLsizei remaining_buffer = name_buffer_len - shaderface->name_buffer_offset; + char* name = shaderface->name_buffer + shaderface->name_buffer_offset; + GLsizei name_len = 0; + + glGetActiveUniformBlockName(program, i, remaining_buffer, &name_len, name); + + input->location = i; + + set_input_name(shaderface, input, name, name_len); + + shader_input_to_bucket(input, shaderface->ubo_buckets); + +#if DEBUG_SHADER_INTERFACE + printf("attrib[%u] '%s' at location %d\n", i, name, input->location); +#endif + } + return shaderface; } @@ -231,6 +257,7 @@ void GWN_shaderinterface_discard(Gwn_ShaderInterface* shaderface) // Free memory used by buckets and has entries. buckets_free(shaderface->uniform_buckets); buckets_free(shaderface->attrib_buckets); + buckets_free(shaderface->ubo_buckets); // Free memory used by name_buffer. free(shaderface->name_buffer); // Free memory used by shader interface by its self. @@ -266,6 +293,11 @@ const Gwn_ShaderInput* GWN_shaderinterface_uniform_builtin(const Gwn_ShaderInter return (input->location != -1) ? input : NULL; } +const Gwn_ShaderInput* GWN_shaderinterface_ubo(const Gwn_ShaderInterface* shaderface, const char* name) + { + return buckets_lookup(shaderface->ubo_buckets, shaderface->name_buffer, name); + } + const Gwn_ShaderInput* GWN_shaderinterface_attr(const Gwn_ShaderInterface* shaderface, const char* name) { return buckets_lookup(shaderface->attrib_buckets, shaderface->name_buffer, name); diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c index 7e0848ec071..733aafd7362 100644 --- a/source/blender/gpu/intern/gpu_shader.c +++ b/source/blender/gpu/intern/gpu_shader.c @@ -561,7 +561,8 @@ int GPU_shader_get_uniform_block(GPUShader *shader, const char *name) { BLI_assert(shader && shader->program); - return glGetUniformBlockIndex(shader->program, name); + const Gwn_ShaderInput *ubo = GWN_shaderinterface_ubo(shader->interface, name); + return ubo ? ubo->location : -1; } void *GPU_fx_shader_get_interface(GPUShader *shader) |