Overlay: Remove static GPUVertFormat

Makes this code threadsafe
This commit is contained in:
Clément Foucault
2025-02-17 14:53:18 +01:00
parent b73c06ada0
commit 2cd15cdfef
2 changed files with 85 additions and 83 deletions

View File

@@ -273,6 +273,30 @@ struct State {
}
};
/* Matches Vertex Format. */
struct Vertex {
float3 pos;
int vclass;
};
struct VertexWithColor {
float3 pos;
float3 color;
};
struct VertShaded {
float3 pos;
int v_class;
float3 nor;
};
/* TODO(fclem): Might be good to remove for simplicity. */
struct VertexTriple {
float2 pos0;
float2 pos1;
float2 pos2;
};
/**
* Contains all overlay generic geometry batches.
*/
@@ -360,6 +384,67 @@ class ShapeCache {
BatchPtr lightprobe_grid;
ShapeCache();
private:
GPUVertFormat format_vert = {0};
GPUVertFormat format_vert_with_color = {0};
GPUVertFormat format_vert_shaded = {0};
GPUVertFormat format_vert_triple = {0};
const GPUVertFormat &get_format(Vertex /*unused*/)
{
GPUVertFormat &format = format_vert;
if (format.attr_len != 0) {
return format;
}
GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
GPU_vertformat_attr_add(&format, "vclass", GPU_COMP_I32, 1, GPU_FETCH_INT);
return format;
}
const GPUVertFormat &get_format(VertexWithColor /*unused*/)
{
GPUVertFormat &format = format_vert_with_color;
if (format.attr_len != 0) {
return format;
}
GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
GPU_vertformat_attr_add(&format, "color", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
return format;
}
const GPUVertFormat &get_format(VertShaded /*unused*/)
{
GPUVertFormat &format = format_vert_shaded;
if (format.attr_len != 0) {
return format;
}
GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
GPU_vertformat_attr_add(&format, "vclass", GPU_COMP_I32, 1, GPU_FETCH_INT);
GPU_vertformat_attr_add(&format, "nor", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
return format;
}
const GPUVertFormat &get_format(VertexTriple /*unused*/)
{
GPUVertFormat &format = format_vert_triple;
if (format.attr_len != 0) {
return format;
}
GPU_vertformat_attr_add(&format, "pos0", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
GPU_vertformat_attr_add(&format, "pos1", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
GPU_vertformat_attr_add(&format, "pos2", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
return format;
}
/* Caller gets ownership of the #gpu::VertBuf. */
template<typename T> gpu::VertBuf *vbo_from_vector(const Vector<T> &vector)
{
gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(get_format(T()));
GPU_vertbuf_data_alloc(*vbo, vector.size());
vbo->data<T>().copy_from(vector);
return vbo;
}
};
/**

View File

@@ -13,89 +13,6 @@
namespace blender::draw::overlay {
/* Matches Vertex Format. */
struct Vertex {
float3 pos;
int vclass;
};
struct VertexWithColor {
float3 pos;
float3 color;
};
struct VertShaded {
float3 pos;
int v_class;
float3 nor;
};
/* TODO(fclem): Might be good to remove for simplicity. */
struct VertexTriple {
float2 pos0;
float2 pos1;
float2 pos2;
};
/* Caller gets ownership of the #gpu::VertBuf. */
static gpu::VertBuf *vbo_from_vector(const Vector<Vertex> &vector)
{
static GPUVertFormat format = {0};
if (format.attr_len == 0) {
GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
GPU_vertformat_attr_add(&format, "vclass", GPU_COMP_I32, 1, GPU_FETCH_INT);
}
gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(format);
GPU_vertbuf_data_alloc(*vbo, vector.size());
vbo->data<Vertex>().copy_from(vector);
return vbo;
}
static gpu::VertBuf *vbo_from_vector(Vector<VertShaded> &vector)
{
static GPUVertFormat format = {0};
if (format.attr_len == 0) {
GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
GPU_vertformat_attr_add(&format, "vclass", GPU_COMP_I32, 1, GPU_FETCH_INT);
GPU_vertformat_attr_add(&format, "nor", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
}
gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(format);
GPU_vertbuf_data_alloc(*vbo, vector.size());
vbo->data<VertShaded>().copy_from(vector);
return vbo;
}
static gpu::VertBuf *vbo_from_vector(Vector<VertexTriple> &vector)
{
static GPUVertFormat format = {0};
if (format.attr_len == 0) {
GPU_vertformat_attr_add(&format, "pos0", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
GPU_vertformat_attr_add(&format, "pos1", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
GPU_vertformat_attr_add(&format, "pos2", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
}
gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(format);
GPU_vertbuf_data_alloc(*vbo, vector.size());
vbo->data<VertexTriple>().copy_from(vector);
return vbo;
}
static gpu::VertBuf *vbo_from_vector(Vector<VertexWithColor> &vector)
{
static GPUVertFormat format = {0};
if (format.attr_len == 0) {
GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
GPU_vertformat_attr_add(&format, "color", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
}
gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(format);
GPU_vertbuf_data_alloc(*vbo, vector.size());
vbo->data<VertexWithColor>().copy_from(vector);
return vbo;
}
enum VertexClass {
VCLASS_NONE = 0,