Cleanup: GLBackend: Move buf_free and tex_free to GLContext

This makes it easier to follow.

Also removes the GL related functions inside gpu_context.cc.
This commit is contained in:
2020-09-07 20:08:25 +02:00
parent 58353834f4
commit 19f56cfe6c
10 changed files with 25 additions and 92 deletions

View File

@@ -126,58 +126,6 @@ GPUContext *GPU_context_active_get(void)
return active_ctx; return active_ctx;
} }
GLuint GPU_vao_alloc(void)
{
GLuint new_vao_id = 0;
glGenVertexArrays(1, &new_vao_id);
return new_vao_id;
}
GLuint GPU_fbo_alloc(void)
{
GLuint new_fbo_id = 0;
glGenFramebuffers(1, &new_fbo_id);
return new_fbo_id;
}
GLuint GPU_buf_alloc(void)
{
GLuint new_buffer_id = 0;
glGenBuffers(1, &new_buffer_id);
return new_buffer_id;
}
GLuint GPU_tex_alloc(void)
{
GLuint new_texture_id = 0;
glGenTextures(1, &new_texture_id);
return new_texture_id;
}
void GPU_vao_free(GLuint vao_id, GPUContext *ctx)
{
static_cast<GLContext *>(ctx)->vao_free(vao_id);
}
void GPU_fbo_free(GLuint fbo_id, GPUContext *ctx)
{
static_cast<GLContext *>(ctx)->fbo_free(fbo_id);
}
void GPU_buf_free(GLuint buf_id)
{
/* TODO avoid using backend */
GPUBackend *backend = GPUBackend::get();
static_cast<GLBackend *>(backend)->buf_free(buf_id);
}
void GPU_tex_free(GLuint tex_id)
{
/* TODO avoid using backend */
GPUBackend *backend = GPUBackend::get();
static_cast<GLBackend *>(backend)->tex_free(tex_id);
}
struct GPUMatrixState *gpu_context_active_matrix_state_get() struct GPUMatrixState *gpu_context_active_matrix_state_get()
{ {
BLI_assert(active_ctx); BLI_assert(active_ctx);

View File

@@ -83,19 +83,6 @@ struct GPUContext {
MEM_CXX_CLASS_ALLOC_FUNCS("GPUContext") MEM_CXX_CLASS_ALLOC_FUNCS("GPUContext")
}; };
/* These require a OpenGL ctx bound. */
GLuint GPU_buf_alloc(void);
GLuint GPU_tex_alloc(void);
GLuint GPU_vao_alloc(void);
GLuint GPU_fbo_alloc(void);
/* These can be called any threads even without OpenGL ctx. */
void GPU_buf_free(GLuint buf_id);
void GPU_tex_free(GLuint tex_id);
/* These two need the ctx the id was created with. */
void GPU_vao_free(GLuint vao_id, GPUContext *ctx);
void GPU_fbo_free(GLuint fbo_id, GPUContext *ctx);
void gpu_context_active_framebuffer_set(GPUContext *ctx, struct GPUFrameBuffer *fb); void gpu_context_active_framebuffer_set(GPUContext *ctx, struct GPUFrameBuffer *fb);
struct GPUFrameBuffer *gpu_context_active_framebuffer_get(GPUContext *ctx); struct GPUFrameBuffer *gpu_context_active_framebuffer_get(GPUContext *ctx);

View File

@@ -115,15 +115,10 @@ class GLBackend : public GPUBackend {
return new GLVertBuf(); return new GLVertBuf();
}; };
/* TODO remove */ GLSharedOrphanLists &shared_orphan_list_get(void)
void buf_free(GLuint buf_id);
void tex_free(GLuint tex_id);
void orphans_add(Vector<GLuint> &orphan_list, std::mutex &list_mutex, unsigned int id)
{ {
list_mutex.lock(); return shared_orphan_list_;
orphan_list.append(id); };
list_mutex.unlock();
}
private: private:
static void platform_init(void); static void platform_init(void);

View File

@@ -230,25 +230,27 @@ void GLContext::fbo_free(GLuint fbo_id)
} }
} }
void GLBackend::buf_free(GLuint buf_id) void GLContext::buf_free(GLuint buf_id)
{ {
/* Any context can free. */ /* Any context can free. */
if (GPU_context_active_get()) { if (GPU_context_active_get()) {
glDeleteBuffers(1, &buf_id); glDeleteBuffers(1, &buf_id);
} }
else { else {
orphans_add(shared_orphan_list_.buffers, shared_orphan_list_.lists_mutex, buf_id); GLSharedOrphanLists &orphan_list = GLBackend::get()->shared_orphan_list_get();
orphans_add(orphan_list.buffers, orphan_list.lists_mutex, buf_id);
} }
} }
void GLBackend::tex_free(GLuint tex_id) void GLContext::tex_free(GLuint tex_id)
{ {
/* Any context can free. */ /* Any context can free. */
if (GPU_context_active_get()) { if (GPU_context_active_get()) {
glDeleteTextures(1, &tex_id); glDeleteTextures(1, &tex_id);
} }
else { else {
orphans_add(shared_orphan_list_.textures, shared_orphan_list_.lists_mutex, tex_id); GLSharedOrphanLists &orphan_list = GLBackend::get()->shared_orphan_list_get();
orphans_add(orphan_list.textures, orphan_list.lists_mutex, tex_id);
} }
} }

View File

@@ -105,13 +105,17 @@ class GLContext : public GPUContext {
return static_cast<GLStateManager *>(ctx->state_manager); return static_cast<GLStateManager *>(ctx->state_manager);
}; };
/* TODO(fclem) these needs to become private. */ /* These need to be called with the context the id was created with. */
public:
void orphans_add(Vector<GLuint> &orphan_list, std::mutex &list_mutex, GLuint id);
void orphans_clear(void);
void vao_free(GLuint vao_id); void vao_free(GLuint vao_id);
void fbo_free(GLuint fbo_id); void fbo_free(GLuint fbo_id);
/* These can be called by any threads even without OpenGL ctx. Deletion will be delayed. */
static void buf_free(GLuint buf_id);
static void tex_free(GLuint tex_id);
/* TODO(fclem) these needs to become private. */
public:
static void orphans_add(Vector<GLuint> &orphan_list, std::mutex &list_mutex, GLuint id);
void orphans_clear(void);
void vao_cache_register(GLVaoCache *cache); void vao_cache_register(GLVaoCache *cache);
void vao_cache_unregister(GLVaoCache *cache); void vao_cache_unregister(GLVaoCache *cache);
}; };

View File

@@ -88,10 +88,7 @@ GLDrawList::GLDrawList(int length)
GLDrawList::~GLDrawList() GLDrawList::~GLDrawList()
{ {
/* TODO This ... */ GLContext::buf_free(buffer_id_);
static_cast<GLBackend *>(GPUBackend::get())->buf_free(buffer_id_);
/* ... should be this. */
// context_->buf_free(buffer_id_)
} }
void GLDrawList::init(void) void GLDrawList::init(void)

View File

@@ -21,7 +21,7 @@
* \ingroup gpu * \ingroup gpu
*/ */
#include "gl_backend.hh" #include "gl_context.hh"
#include "gl_debug.hh" #include "gl_debug.hh"
#include "gl_index_buffer.hh" #include "gl_index_buffer.hh"
@@ -30,7 +30,7 @@ namespace blender::gpu {
GLIndexBuf::~GLIndexBuf() GLIndexBuf::~GLIndexBuf()
{ {
GLBackend::get()->buf_free(ibo_id_); GLContext::buf_free(ibo_id_);
} }
void GLIndexBuf::bind(void) void GLIndexBuf::bind(void)

View File

@@ -59,7 +59,7 @@ GLTexture::~GLTexture()
/* This avoid errors when the texture is still inside the bound texture array. */ /* This avoid errors when the texture is still inside the bound texture array. */
ctx->state_manager->texture_unbind(this); ctx->state_manager->texture_unbind(this);
} }
GLBackend::get()->tex_free(tex_id_); GLContext::tex_free(tex_id_);
} }
/* Return true on success. */ /* Return true on success. */

View File

@@ -45,7 +45,7 @@ GLUniformBuf::GLUniformBuf(size_t size, const char *name) : UniformBuf(size, nam
GLUniformBuf::~GLUniformBuf() GLUniformBuf::~GLUniformBuf()
{ {
GLBackend::get()->buf_free(ubo_id_); GLContext::buf_free(ubo_id_);
} }
/** \} */ /** \} */

View File

@@ -21,7 +21,7 @@
* \ingroup gpu * \ingroup gpu
*/ */
#include "gl_backend.hh" #include "gl_context.hh"
#include "gl_vertex_buffer.hh" #include "gl_vertex_buffer.hh"
@@ -42,7 +42,7 @@ void GLVertBuf::resize_data(void)
void GLVertBuf::release_data(void) void GLVertBuf::release_data(void)
{ {
if (vbo_id_ != 0) { if (vbo_id_ != 0) {
GLBackend::get()->buf_free(vbo_id_); GLContext::buf_free(vbo_id_);
vbo_id_ = 0; vbo_id_ = 0;
memory_usage -= vbo_size_; memory_usage -= vbo_size_;
} }