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.
This commit is contained in:
Clément Foucault 2020-03-26 21:10:29 +01:00
parent afb1a64ccb
commit 56e0249489
Notes: blender-bot 2023-02-14 03:52:45 +01:00
Referenced by issue #75069, Lastes Blender versions break parenting in DEMO File - BMW scene
3 changed files with 42 additions and 2 deletions

View File

@ -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);

View File

@ -42,6 +42,8 @@
#include <stdlib.h>
#include <string.h>
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();
}

View File

@ -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);