From 56e0249489ce89954a54d5006ddf4147dd02e208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Thu, 26 Mar 2020 21:10:29 +0100 Subject: GPU: Add workaround for faulty default attrib values on some drivers On some drivers, the default values is not respected correctly. To workaround this we create a small VBO that contains only 1 vec4 worth of data and just bind it using glBindVertexBuffer to ensure 0 stride. This fixes T75069 Instances not rendered correctly by workbench. --- source/blender/gpu/GPU_shader_interface.h | 2 ++ source/blender/gpu/intern/gpu_batch.c | 39 ++++++++++++++++++++++-- source/blender/gpu/intern/gpu_shader_interface.c | 3 ++ 3 files changed, 42 insertions(+), 2 deletions(-) (limited to 'source/blender/gpu') diff --git a/source/blender/gpu/GPU_shader_interface.h b/source/blender/gpu/GPU_shader_interface.h index 8b0d25e51a3..7a8900997d0 100644 --- a/source/blender/gpu/GPU_shader_interface.h +++ b/source/blender/gpu/GPU_shader_interface.h @@ -88,6 +88,8 @@ typedef struct GPUShaderInterface { char *name_buffer; struct GPUBatch **batches; /* references to batches using this interface */ uint batches_len; + /** All enabled attribs in this shader. Used to set default values for unbound attribs. */ + uint16_t enabled_attrib_mask; } GPUShaderInterface; GPUShaderInterface *GPU_shaderinterface_create(int32_t program_id); diff --git a/source/blender/gpu/intern/gpu_batch.c b/source/blender/gpu/intern/gpu_batch.c index 6bc3cd27130..fcfa4b7f0af 100644 --- a/source/blender/gpu/intern/gpu_batch.c +++ b/source/blender/gpu/intern/gpu_batch.c @@ -42,6 +42,8 @@ #include #include +static GLuint g_default_attrib_vbo = 0; + static void batch_update_program_bindings(GPUBatch *batch, uint i_first); void GPU_batch_vao_cache_clear(GPUBatch *batch) @@ -414,6 +416,7 @@ void gpu_batch_remove_interface_ref(GPUBatch *batch, const GPUShaderInterface *i static void create_bindings(GPUVertBuf *verts, const GPUShaderInterface *interface, + uint16_t *attrib_mask, uint v_first, const bool use_instancing) { @@ -446,6 +449,8 @@ static void create_bindings(GPUVertBuf *verts, continue; } + *attrib_mask &= ~(1 << input->location); + if (a->comp_len == 16 || a->comp_len == 12 || a->comp_len == 8) { #if TRUST_NO_ONE assert(a->fetch_mode == GPU_FETCH_FLOAT); @@ -487,17 +492,35 @@ static void create_bindings(GPUVertBuf *verts, static void batch_update_program_bindings(GPUBatch *batch, uint i_first) { + uint16_t attrib_mask = batch->interface->enabled_attrib_mask; + /* Reverse order so first vbos have more prevalence (in term of attrib override). */ for (int v = GPU_BATCH_VBO_MAX_LEN - 1; v > -1; v--) { if (batch->verts[v] != NULL) { - create_bindings(batch->verts[v], batch->interface, 0, false); + create_bindings(batch->verts[v], batch->interface, &attrib_mask, 0, false); } } + for (int v = GPU_BATCH_INST_VBO_MAX_LEN - 1; v > -1; v--) { if (batch->inst[v]) { - create_bindings(batch->inst[v], batch->interface, i_first, true); + create_bindings(batch->inst[v], batch->interface, &attrib_mask, i_first, true); + } + } + + if (attrib_mask != 0 && GLEW_ARB_vertex_attrib_binding) { + for (uint16_t mask = 1, a = 0; a < 16; a++, mask <<= 1) { + if (attrib_mask & mask) { + /* This replaces glVertexAttrib4f(a, 0.0f, 0.0f, 0.0f, 1.0f); with a more modern style. + * Fix issues for some drivers (see T75069). */ + glBindVertexBuffer(a, g_default_attrib_vbo, (intptr_t)0, (intptr_t)0); + + glEnableVertexAttribArray(a); + glVertexAttribFormat(a, 4, GL_FLOAT, GL_FALSE, 0); + glVertexAttribBinding(a, a); + } } } + if (batch->elem) { GPU_indexbuf_use(batch->elem); } @@ -1002,11 +1025,23 @@ void GPU_batch_program_set_imm_shader(GPUBatch *batch) void gpu_batch_init(void) { + if (g_default_attrib_vbo == 0) { + g_default_attrib_vbo = GPU_buf_alloc(); + + float default_attrib_data[4] = {0.0f, 0.0f, 0.0f, 1.0f}; + glBindBuffer(GL_ARRAY_BUFFER, g_default_attrib_vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 4, default_attrib_data, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } + gpu_batch_presets_init(); } void gpu_batch_exit(void) { + GPU_buf_free(g_default_attrib_vbo); + g_default_attrib_vbo = 0; + gpu_batch_presets_exit(); } diff --git a/source/blender/gpu/intern/gpu_shader_interface.c b/source/blender/gpu/intern/gpu_shader_interface.c index f23a0a438d6..349a7217456 100644 --- a/source/blender/gpu/intern/gpu_shader_interface.c +++ b/source/blender/gpu/intern/gpu_shader_interface.c @@ -236,6 +236,7 @@ GPUShaderInterface *GPU_shaderinterface_create(int32_t program) shaderface->name_buffer = MEM_mallocN(name_buffer_len, "name_buffer"); /* Attributes */ + shaderface->enabled_attrib_mask = 0; for (uint32_t i = 0; i < attr_len; i++) { GPUShaderInput *input = MEM_mallocN(sizeof(GPUShaderInput), "GPUShaderInput Attr"); GLsizei remaining_buffer = name_buffer_len - shaderface->name_buffer_offset; @@ -255,6 +256,8 @@ GPUShaderInterface *GPU_shaderinterface_create(int32_t program) input->location = glGetAttribLocation(program, name); + shaderface->enabled_attrib_mask |= (1 << input->location); + set_input_name(shaderface, input, name, name_len); shader_input_to_bucket(input, shaderface->attr_buckets); -- cgit v1.2.3