GPUContext: Move GPUContext to gpu::Context for more consistency

This makes the GPUContext follow the same naming convention as the rest
of the module.

Also add a static getter for extra bonus style (no need for casts):
- Context::get()
- GLContext::get()
This commit is contained in:
Clément Foucault 2020-09-08 04:12:12 +02:00
parent d2e9de93b8
commit 48690d967a
25 changed files with 152 additions and 127 deletions

View File

@ -32,8 +32,6 @@
extern "C" {
#endif
typedef struct GPUContext GPUContext;
typedef enum eGPUBackendType {
GPU_BACKEND_NONE = 0,
GPU_BACKEND_OPENGL,
@ -42,6 +40,9 @@ typedef enum eGPUBackendType {
void GPU_backend_init(eGPUBackendType backend);
void GPU_backend_exit(void);
/** Opaque type hiding blender::gpu::Context. */
typedef struct GPUContext GPUContext;
GPUContext *GPU_context_create(void *ghost_window);
void GPU_context_discard(GPUContext *);

View File

@ -25,11 +25,11 @@
#pragma once
struct GPUContext;
namespace blender {
namespace gpu {
class Context;
class Batch;
class DrawList;
class FrameBuffer;
@ -48,7 +48,7 @@ class GPUBackend {
virtual void samplers_update(void) = 0;
virtual GPUContext *context_alloc(void *ghost_window) = 0;
virtual Context *context_alloc(void *ghost_window) = 0;
virtual Batch *batch_alloc(void) = 0;
virtual DrawList *drawlist_alloc(int list_length) = 0;

View File

@ -258,7 +258,7 @@ void GPU_batch_draw_instanced(GPUBatch *batch, int i_count)
void GPU_batch_draw_advanced(
GPUBatch *gpu_batch, int v_first, int v_count, int i_first, int i_count)
{
BLI_assert(GPU_context_active_get()->shader != NULL);
BLI_assert(Context::get()->shader != NULL);
Batch *batch = static_cast<Batch *>(gpu_batch);
if (v_count == 0) {

View File

@ -115,13 +115,13 @@ bool GPU_mem_stats_supported(void)
void GPU_mem_stats_get(int *totalmem, int *freemem)
{
GPU_context_active_get()->memory_statistics_get(totalmem, freemem);
Context::get()->memory_statistics_get(totalmem, freemem);
}
/* Return support for the active context + window. */
bool GPU_stereo_quadbuffer_support(void)
{
return GPU_context_active_get()->front_right != nullptr;
return Context::get()->front_right != nullptr;
}
/** \} */

View File

@ -54,20 +54,22 @@
using namespace blender::gpu;
static thread_local GPUContext *active_ctx = NULL;
static thread_local Context *active_ctx = NULL;
/* -------------------------------------------------------------------- */
/** \name GPUContext methods
/** \name gpu::Context methods
* \{ */
GPUContext::GPUContext()
namespace blender::gpu {
Context::Context()
{
thread_ = pthread_self();
is_active_ = false;
matrix_state = GPU_matrix_state_create();
}
GPUContext::~GPUContext()
Context::~Context()
{
GPU_matrix_state_discard(matrix_state);
delete state_manager;
@ -78,11 +80,18 @@ GPUContext::~GPUContext()
delete imm;
}
bool GPUContext::is_active_on_thread(void)
bool Context::is_active_on_thread(void)
{
return (this == active_ctx) && pthread_equal(pthread_self(), thread_);
}
Context *Context::get(void)
{
return active_ctx;
}
} // namespace blender::gpu
/** \} */
/* -------------------------------------------------------------------- */
@ -94,22 +103,25 @@ GPUContext *GPU_context_create(void *ghost_window)
GPU_backend_init(GPU_BACKEND_OPENGL);
}
GPUContext *ctx = GPUBackend::get()->context_alloc(ghost_window);
Context *ctx = GPUBackend::get()->context_alloc(ghost_window);
GPU_context_active_set(ctx);
return ctx;
GPU_context_active_set(wrap(ctx));
return wrap(ctx);
}
/* to be called after GPU_context_active_set(ctx_to_destroy) */
void GPU_context_discard(GPUContext *ctx)
void GPU_context_discard(GPUContext *ctx_)
{
Context *ctx = unwrap(ctx_);
delete ctx;
active_ctx = NULL;
}
/* ctx can be NULL */
void GPU_context_active_set(GPUContext *ctx)
void GPU_context_active_set(GPUContext *ctx_)
{
Context *ctx = unwrap(ctx_);
if (active_ctx) {
active_ctx->deactivate();
}
@ -123,13 +135,7 @@ void GPU_context_active_set(GPUContext *ctx)
GPUContext *GPU_context_active_get(void)
{
return active_ctx;
}
struct GPUMatrixState *gpu_context_active_matrix_state_get()
{
BLI_assert(active_ctx);
return active_ctx->matrix_state;
return wrap(Context::get());
}
/* -------------------------------------------------------------------- */

View File

@ -34,22 +34,20 @@
#include "gpu_shader_private.hh"
#include "gpu_state_private.hh"
#include <mutex>
#include <pthread.h>
#include <string.h>
#include <unordered_set>
#include <vector>
struct GPUMatrixState;
struct GPUContext {
namespace blender::gpu {
class Context {
public:
/** State management */
blender::gpu::Shader *shader = NULL;
blender::gpu::FrameBuffer *active_fb = NULL;
Shader *shader = NULL;
FrameBuffer *active_fb = NULL;
GPUMatrixState *matrix_state = NULL;
blender::gpu::GPUStateManager *state_manager = NULL;
blender::gpu::Immediate *imm = NULL;
GPUStateManager *state_manager = NULL;
Immediate *imm = NULL;
/**
* All 4 window frame-buffers.
@ -58,10 +56,10 @@ struct GPUContext {
* Front frame-buffers contains (in principle, but not always) the last frame color.
* Default frame-buffer is back_left.
*/
blender::gpu::FrameBuffer *back_left = NULL;
blender::gpu::FrameBuffer *front_left = NULL;
blender::gpu::FrameBuffer *back_right = NULL;
blender::gpu::FrameBuffer *front_right = NULL;
FrameBuffer *back_left = NULL;
FrameBuffer *front_left = NULL;
FrameBuffer *back_right = NULL;
FrameBuffer *front_right = NULL;
protected:
/** Thread on which this context is active. */
@ -71,8 +69,10 @@ struct GPUContext {
void *ghost_window_;
public:
GPUContext();
virtual ~GPUContext();
Context();
virtual ~Context();
static Context *get(void);
virtual void activate(void) = 0;
virtual void deactivate(void) = 0;
@ -85,11 +85,20 @@ struct GPUContext {
virtual void memory_statistics_get(int *total_mem, int *free_mem) = 0;
bool is_active_on_thread(void);
MEM_CXX_CLASS_ALLOC_FUNCS("GPUContext")
};
void gpu_context_active_framebuffer_set(GPUContext *ctx, struct GPUFrameBuffer *fb);
struct GPUFrameBuffer *gpu_context_active_framebuffer_get(GPUContext *ctx);
/* Syntacting suggar. */
static inline GPUContext *wrap(Context *ctx)
{
return reinterpret_cast<GPUContext *>(ctx);
}
static inline Context *unwrap(GPUContext *ctx)
{
return reinterpret_cast<Context *>(ctx);
}
static inline const Context *unwrap(const GPUContext *ctx)
{
return reinterpret_cast<const Context *>(ctx);
}
struct GPUMatrixState *gpu_context_active_matrix_state_get(void);
} // namespace blender::gpu

View File

@ -224,7 +224,7 @@ void GPU_framebuffer_bind_no_srgb(GPUFrameBuffer *gpu_fb)
*/
void GPU_backbuffer_bind(eGPUBackBuffer buffer)
{
GPUContext *ctx = GPU_context_active_get();
Context *ctx = Context::get();
if (buffer == GPU_BACKBUFFER_LEFT) {
ctx->back_left->bind(false);
@ -236,19 +236,19 @@ void GPU_backbuffer_bind(eGPUBackBuffer buffer)
void GPU_framebuffer_restore(void)
{
GPU_context_active_get()->back_left->bind(false);
Context::get()->back_left->bind(false);
}
GPUFrameBuffer *GPU_framebuffer_active_get(void)
{
GPUContext *ctx = GPU_context_active_get();
Context *ctx = Context::get();
return wrap(ctx ? ctx->active_fb : NULL);
}
/* Returns the default frame-buffer. Will always exists even if it's just a dummy. */
GPUFrameBuffer *GPU_framebuffer_back_get(void)
{
GPUContext *ctx = GPU_context_active_get();
Context *ctx = Context::get();
return wrap(ctx ? ctx->back_left : NULL);
}
@ -381,13 +381,13 @@ void GPU_framebuffer_multi_clear(GPUFrameBuffer *gpu_fb, const float (*clear_col
void GPU_clear_color(float red, float green, float blue, float alpha)
{
float clear_col[4] = {red, green, blue, alpha};
GPU_context_active_get()->active_fb->clear(GPU_COLOR_BIT, clear_col, 0.0f, 0x0);
Context::get()->active_fb->clear(GPU_COLOR_BIT, clear_col, 0.0f, 0x0);
}
void GPU_clear_depth(float depth)
{
float clear_col[4] = {0};
GPU_context_active_get()->active_fb->clear(GPU_DEPTH_BIT, clear_col, depth, 0x0);
Context::get()->active_fb->clear(GPU_DEPTH_BIT, clear_col, depth, 0x0);
}
void GPU_framebuffer_read_depth(
@ -416,7 +416,7 @@ void GPU_frontbuffer_read_pixels(
int x, int y, int w, int h, int channels, eGPUDataFormat format, void *data)
{
int rect[4] = {x, y, w, h};
GPU_context_active_get()->front_left->read(GPU_COLOR_BIT, format, rect, channels, 0, data);
Context::get()->front_left->read(GPU_COLOR_BIT, format, rect, channels, 0, data);
}
/* read_slot and write_slot are only used for color buffers. */
@ -431,7 +431,7 @@ void GPU_framebuffer_blit(GPUFrameBuffer *gpufb_read,
FrameBuffer *fb_write = unwrap(gpufb_write);
BLI_assert(blit_buffers != 0);
FrameBuffer *prev_fb = GPU_context_active_get()->active_fb;
FrameBuffer *prev_fb = Context::get()->active_fb;
#ifndef NDEBUG
GPUTexture *read_tex, *write_tex;
@ -509,7 +509,7 @@ static GPUFrameBuffer *gpuPopFrameBuffer(void)
struct GPUOffScreen {
struct {
GPUContext *ctx;
Context *ctx;
GPUFrameBuffer *fb;
} framebuffers[MAX_CTX_FB_LEN];
@ -522,7 +522,7 @@ struct GPUOffScreen {
*/
static GPUFrameBuffer *gpu_offscreen_fb_get(GPUOffScreen *ofs)
{
GPUContext *ctx = GPU_context_active_get();
Context *ctx = Context::get();
BLI_assert(ctx);
for (int i = 0; i < MAX_CTX_FB_LEN; i++) {
@ -635,7 +635,7 @@ void GPU_offscreen_unbind(GPUOffScreen *UNUSED(ofs), bool restore)
void GPU_offscreen_draw_to_screen(GPUOffScreen *ofs, int x, int y)
{
GPUContext *ctx = GPU_context_active_get();
Context *ctx = Context::get();
FrameBuffer *ofs_fb = unwrap(gpu_offscreen_fb_get(ofs));
ofs_fb->blit_to(GPU_COLOR_BIT, 0, ctx->active_fb, 0, x, y);
}

View File

@ -43,7 +43,7 @@ static thread_local Immediate *imm = NULL;
void immActivate(void)
{
imm = GPU_context_active_get()->imm;
imm = Context::get()->imm;
}
void immDeactivate(void)

View File

@ -35,6 +35,8 @@
#include "MEM_guardedalloc.h"
using namespace blender::gpu;
#define MATRIX_STACK_DEPTH 32
typedef float Mat4[4][4];
@ -59,10 +61,10 @@ typedef struct GPUMatrixState {
*/
} GPUMatrixState;
#define ModelViewStack gpu_context_active_matrix_state_get()->model_view_stack
#define ModelViewStack Context::get()->matrix_state->model_view_stack
#define ModelView ModelViewStack.stack[ModelViewStack.top]
#define ProjectionStack gpu_context_active_matrix_state_get()->projection_stack
#define ProjectionStack Context::get()->matrix_state->projection_stack
#define Projection ProjectionStack.stack[ProjectionStack.top]
GPUMatrixState *GPU_matrix_state_create(void)
@ -93,13 +95,13 @@ void GPU_matrix_state_discard(GPUMatrixState *state)
static void gpu_matrix_state_active_set_dirty(bool value)
{
GPUMatrixState *state = gpu_context_active_matrix_state_get();
GPUMatrixState *state = Context::get()->matrix_state;
state->dirty = value;
}
void GPU_matrix_reset(void)
{
GPUMatrixState *state = gpu_context_active_matrix_state_get();
GPUMatrixState *state = Context::get()->matrix_state;
state->model_view_stack.top = 0;
state->projection_stack.top = 0;
unit_m4(ModelView);
@ -686,7 +688,7 @@ void GPU_matrix_bind(GPUShader *shader)
bool GPU_matrix_dirty_get(void)
{
GPUMatrixState *state = gpu_context_active_matrix_state_get();
GPUMatrixState *state = Context::get()->matrix_state;
return state->dirty;
}
@ -699,13 +701,13 @@ BLI_STATIC_ASSERT(GPU_PY_MATRIX_STACK_LEN + 1 == MATRIX_STACK_DEPTH, "define mis
int GPU_matrix_stack_level_get_model_view(void)
{
GPUMatrixState *state = gpu_context_active_matrix_state_get();
GPUMatrixState *state = Context::get()->matrix_state;
return (int)state->model_view_stack.top;
}
int GPU_matrix_stack_level_get_projection(void)
{
GPUMatrixState *state = gpu_context_active_matrix_state_get();
GPUMatrixState *state = Context::get()->matrix_state;
return (int)state->projection_stack.top;
}

View File

@ -457,7 +457,7 @@ void GPU_shader_bind(GPUShader *gpu_shader)
{
Shader *shader = unwrap(gpu_shader);
GPUContext *ctx = GPU_context_active_get();
Context *ctx = Context::get();
if (ctx->shader != shader) {
ctx->shader = shader;
@ -474,7 +474,7 @@ void GPU_shader_bind(GPUShader *gpu_shader)
void GPU_shader_unbind(void)
{
#ifndef NDEBUG
GPUContext *ctx = GPU_context_active_get();
Context *ctx = Context::get();
if (ctx->shader) {
ctx->shader->unbind();
}

View File

@ -41,7 +41,7 @@ using namespace blender::gpu;
#define SET_STATE(_prefix, _state, _value) \
do { \
GPUStateManager *stack = GPU_context_active_get()->state_manager; \
GPUStateManager *stack = Context::get()->state_manager; \
auto &state_object = stack->_prefix##state; \
state_object._state = (_value); \
} while (0)
@ -105,7 +105,7 @@ void GPU_write_mask(eGPUWriteMask mask)
void GPU_color_mask(bool r, bool g, bool b, bool a)
{
GPUStateManager *stack = GPU_context_active_get()->state_manager;
GPUStateManager *stack = Context::get()->state_manager;
auto &state = stack->state;
uint32_t write_mask = state.write_mask;
SET_FLAG_FROM_TEST(write_mask, r, (uint32_t)GPU_WRITE_RED);
@ -117,7 +117,7 @@ void GPU_color_mask(bool r, bool g, bool b, bool a)
void GPU_depth_mask(bool depth)
{
GPUStateManager *stack = GPU_context_active_get()->state_manager;
GPUStateManager *stack = Context::get()->state_manager;
auto &state = stack->state;
uint32_t write_mask = state.write_mask;
SET_FLAG_FROM_TEST(write_mask, depth, (uint32_t)GPU_WRITE_DEPTH);
@ -142,7 +142,7 @@ void GPU_state_set(eGPUWriteMask write_mask,
eGPUStencilOp stencil_op,
eGPUProvokingVertex provoking_vert)
{
GPUStateManager *stack = GPU_context_active_get()->state_manager;
GPUStateManager *stack = Context::get()->state_manager;
auto &state = stack->state;
state.write_mask = (uint32_t)write_mask;
state.blend = (uint32_t)blend;
@ -161,7 +161,7 @@ void GPU_state_set(eGPUWriteMask write_mask,
void GPU_depth_range(float near, float far)
{
GPUStateManager *stack = GPU_context_active_get()->state_manager;
GPUStateManager *stack = Context::get()->state_manager;
auto &state = stack->mutable_state;
copy_v2_fl2(state.depth_range, near, far);
}
@ -182,7 +182,7 @@ void GPU_point_size(float size)
/* TODO remove and use program point size everywhere */
void GPU_program_point_size(bool enable)
{
GPUStateManager *stack = GPU_context_active_get()->state_manager;
GPUStateManager *stack = Context::get()->state_manager;
auto &state = stack->mutable_state;
/* Set point size sign negative to disable. */
state.point_size = fabsf(state.point_size) * (enable ? 1 : -1);
@ -190,19 +190,19 @@ void GPU_program_point_size(bool enable)
void GPU_scissor_test(bool enable)
{
GPU_context_active_get()->active_fb->scissor_test_set(enable);
Context::get()->active_fb->scissor_test_set(enable);
}
void GPU_scissor(int x, int y, int width, int height)
{
int scissor_rect[4] = {x, y, width, height};
GPU_context_active_get()->active_fb->scissor_set(scissor_rect);
Context::get()->active_fb->scissor_set(scissor_rect);
}
void GPU_viewport(int x, int y, int width, int height)
{
int viewport_rect[4] = {x, y, width, height};
GPU_context_active_get()->active_fb->viewport_set(viewport_rect);
Context::get()->active_fb->viewport_set(viewport_rect);
}
void GPU_stencil_reference_set(uint reference)
@ -228,43 +228,43 @@ void GPU_stencil_compare_mask_set(uint compare_mask)
eGPUBlend GPU_blend_get()
{
GPUState &state = GPU_context_active_get()->state_manager->state;
GPUState &state = Context::get()->state_manager->state;
return (eGPUBlend)state.blend;
}
eGPUWriteMask GPU_write_mask_get()
{
GPUState &state = GPU_context_active_get()->state_manager->state;
GPUState &state = Context::get()->state_manager->state;
return (eGPUWriteMask)state.write_mask;
}
uint GPU_stencil_mask_get()
{
GPUStateMutable &state = GPU_context_active_get()->state_manager->mutable_state;
GPUStateMutable &state = Context::get()->state_manager->mutable_state;
return state.stencil_write_mask;
}
eGPUDepthTest GPU_depth_test_get()
{
GPUState &state = GPU_context_active_get()->state_manager->state;
GPUState &state = Context::get()->state_manager->state;
return (eGPUDepthTest)state.depth_test;
}
eGPUStencilTest GPU_stencil_test_get()
{
GPUState &state = GPU_context_active_get()->state_manager->state;
GPUState &state = Context::get()->state_manager->state;
return (eGPUStencilTest)state.stencil_test;
}
void GPU_scissor_get(int coords[4])
{
GPU_context_active_get()->active_fb->scissor_get(coords);
Context::get()->active_fb->scissor_get(coords);
}
void GPU_viewport_size_get_f(float coords[4])
{
int viewport[4];
GPU_context_active_get()->active_fb->viewport_get(viewport);
Context::get()->active_fb->viewport_get(viewport);
for (int i = 0; i < 4; i++) {
coords[i] = viewport[i];
}
@ -272,12 +272,12 @@ void GPU_viewport_size_get_f(float coords[4])
void GPU_viewport_size_get_i(int coords[4])
{
GPU_context_active_get()->active_fb->viewport_get(coords);
Context::get()->active_fb->viewport_get(coords);
}
bool GPU_depth_mask_get(void)
{
GPUState &state = GPU_context_active_get()->state_manager->state;
GPUState &state = Context::get()->state_manager->state;
return (state.write_mask & GPU_WRITE_DEPTH) != 0;
}
@ -295,12 +295,12 @@ bool GPU_mipmap_enabled(void)
void GPU_flush(void)
{
GPU_context_active_get()->flush();
Context::get()->flush();
}
void GPU_finish(void)
{
GPU_context_active_get()->finish();
Context::get()->finish();
}
/** \} */

View File

@ -386,7 +386,7 @@ void GPU_texture_update(GPUTexture *tex, eGPUDataFormat data_format, const void
* Skipping pixels correctly when changing rows when doing partial update.*/
void GPU_unpack_row_length_set(uint len)
{
GPU_context_active_get()->state_manager->texture_unpack_row_length_set(len);
Context::get()->state_manager->texture_unpack_row_length_set(len);
}
/* ------ Binding ------ */
@ -398,24 +398,24 @@ void GPU_texture_bind_ex(GPUTexture *tex_,
{
Texture *tex = reinterpret_cast<Texture *>(tex_);
state = (state >= GPU_SAMPLER_MAX) ? tex->sampler_state : state;
GPU_context_active_get()->state_manager->texture_bind(tex, state, unit);
Context::get()->state_manager->texture_bind(tex, state, unit);
}
void GPU_texture_bind(GPUTexture *tex_, int unit)
{
Texture *tex = reinterpret_cast<Texture *>(tex_);
GPU_context_active_get()->state_manager->texture_bind(tex, tex->sampler_state, unit);
Context::get()->state_manager->texture_bind(tex, tex->sampler_state, unit);
}
void GPU_texture_unbind(GPUTexture *tex_)
{
Texture *tex = reinterpret_cast<Texture *>(tex_);
GPU_context_active_get()->state_manager->texture_unbind(tex);
Context::get()->state_manager->texture_unbind(tex);
}
void GPU_texture_unbind_all(void)
{
GPU_context_active_get()->state_manager->texture_unbind_all();
Context::get()->state_manager->texture_unbind_all();
}
void GPU_texture_generate_mipmap(GPUTexture *tex)

View File

@ -71,7 +71,7 @@ class GLBackend : public GPUBackend {
GLTexture::samplers_update();
};
GPUContext *context_alloc(void *ghost_window) override
Context *context_alloc(void *ghost_window) override
{
return new GLContext(ghost_window, shared_orphan_list_);
};

View File

@ -151,7 +151,7 @@ void GLVaoCache::remove(const GLShaderInterface *interface)
void GLVaoCache::clear(void)
{
GLContext *ctx = static_cast<GLContext *>(GPU_context_active_get());
GLContext *ctx = GLContext::get();
const int count = (is_dynamic_vao_count) ? dynamic_vaos.count : GPU_VAO_STATIC_LEN;
GLuint *vaos = (is_dynamic_vao_count) ? dynamic_vaos.vao_ids : static_vaos.vao_ids;
const GLShaderInterface **interfaces = (is_dynamic_vao_count) ? dynamic_vaos.interfaces :
@ -209,7 +209,7 @@ GLuint GLVaoCache::lookup(const GLShaderInterface *interface)
* Reset the cache if trying to draw in another context; */
void GLVaoCache::context_check(void)
{
GLContext *ctx = static_cast<GLContext *>(GPU_context_active_get());
GLContext *ctx = GLContext::get();
BLI_assert(ctx);
if (context_ != ctx) {
@ -228,7 +228,7 @@ GLuint GLVaoCache::base_instance_vao_get(GPUBatch *batch, int i_first)
{
this->context_check();
/* Make sure the interface is up to date. */
Shader *shader = GPU_context_active_get()->shader;
Shader *shader = GLContext::get()->shader;
GLShaderInterface *interface = static_cast<GLShaderInterface *>(shader->interface);
if (interface_ != interface) {
vao_get(batch);
@ -260,7 +260,7 @@ GLuint GLVaoCache::vao_get(GPUBatch *batch)
{
this->context_check();
Shader *shader = GPU_context_active_get()->shader;
Shader *shader = GLContext::get()->shader;
GLShaderInterface *interface = static_cast<GLShaderInterface *>(shader->interface);
if (interface_ != interface) {
interface_ = interface;
@ -298,7 +298,7 @@ GLBatch::~GLBatch()
void GLBatch::bind(int i_first)
{
GPU_context_active_get()->state_manager->apply_state();
GLContext::get()->state_manager->apply_state();
if (flag & GPU_BATCH_DIRTY) {
flag &= ~GPU_BATCH_DIRTY;

View File

@ -190,7 +190,7 @@ void GLContext::finish(void)
void GLSharedOrphanLists::orphans_clear(void)
{
/* Check if any context is active on this thread! */
BLI_assert(GPU_context_active_get());
BLI_assert(GLContext::get());
lists_mutex.lock();
if (!buffers.is_empty()) {
@ -232,7 +232,7 @@ void GLContext::orphans_add(Vector<GLuint> &orphan_list, std::mutex &list_mutex,
void GLContext::vao_free(GLuint vao_id)
{
if (this == GPU_context_active_get()) {
if (this == GLContext::get()) {
glDeleteVertexArrays(1, &vao_id);
}
else {
@ -242,7 +242,7 @@ void GLContext::vao_free(GLuint vao_id)
void GLContext::fbo_free(GLuint fbo_id)
{
if (this == GPU_context_active_get()) {
if (this == GLContext::get()) {
glDeleteFramebuffers(1, &fbo_id);
}
else {
@ -253,7 +253,7 @@ void GLContext::fbo_free(GLuint fbo_id)
void GLContext::buf_free(GLuint buf_id)
{
/* Any context can free. */
if (GPU_context_active_get()) {
if (GLContext::get()) {
glDeleteBuffers(1, &buf_id);
}
else {
@ -265,7 +265,7 @@ void GLContext::buf_free(GLuint buf_id)
void GLContext::tex_free(GLuint tex_id)
{
/* Any context can free. */
if (GPU_context_active_get()) {
if (GLContext::get()) {
glDeleteTextures(1, &tex_id);
}
else {

View File

@ -53,7 +53,7 @@ class GLSharedOrphanLists {
void orphans_clear(void);
};
class GLContext : public GPUContext {
class GLContext : public Context {
public:
/** Capabilities. */
static GLint max_texture_3d_size;
@ -103,9 +103,14 @@ class GLContext : public GPUContext {
void memory_statistics_get(int *total_mem, int *free_mem) override;
static inline GLStateManager *state_manager_active_get()
static GLContext *get()
{
GLContext *ctx = static_cast<GLContext *>(GPU_context_active_get());
return static_cast<GLContext *>(Context::get());
}
static GLStateManager *state_manager_active_get()
{
GLContext *ctx = GLContext::get();
return static_cast<GLStateManager *>(ctx->state_manager);
};
@ -122,6 +127,8 @@ class GLContext : public GPUContext {
private:
static void orphans_add(Vector<GLuint> &orphan_list, std::mutex &list_mutex, GLuint id);
void orphans_clear(void);
MEM_CXX_CLASS_ALLOC_FUNCS("GLContext")
};
} // namespace gpu

View File

@ -197,7 +197,7 @@ void check_gl_resources(const char *info)
return;
}
GLContext *ctx = static_cast<GLContext *>(GPU_context_active_get());
GLContext *ctx = GLContext::get();
ShaderInterface *interface = ctx->shader->interface;
/* NOTE: This only check binding. To be valid, the bound ubo needs to
* be big enough to feed the data range the shader awaits. */

View File

@ -93,7 +93,7 @@ GLDrawList::~GLDrawList()
void GLDrawList::init(void)
{
BLI_assert(GPU_context_active_get());
BLI_assert(GLContext::get());
BLI_assert(MDI_ENABLED);
BLI_assert(data_ == NULL);
batch_ = NULL;
@ -102,7 +102,7 @@ void GLDrawList::init(void)
if (buffer_id_ == 0) {
/* Allocate on first use. */
glGenBuffers(1, &buffer_id_);
context_ = static_cast<GLContext *>(GPU_context_active_get());
context_ = GLContext::get();
}
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer_id_);
@ -180,7 +180,7 @@ void GLDrawList::submit(void)
/* Something's wrong if we get here without MDI support. */
BLI_assert(MDI_ENABLED);
BLI_assert(data_);
BLI_assert(GPU_context_active_get()->shader != NULL);
BLI_assert(GLContext::get()->shader != NULL);
/* Only do multi-draw indirect if doing more than 2 drawcall. This avoids the overhead of
* buffer mapping if scene is not very instance friendly. BUT we also need to take into

View File

@ -78,7 +78,7 @@ GLFrameBuffer::~GLFrameBuffer()
return;
}
if (context_ == GPU_context_active_get()) {
if (context_ == GLContext::get()) {
/* Context might be partially freed. This happens when destroying the window frame-buffers. */
glDeleteFramebuffers(1, &fbo_id_);
}
@ -89,14 +89,14 @@ GLFrameBuffer::~GLFrameBuffer()
if (context_->active_fb == this && context_->back_left != this) {
/* If this assert triggers it means the frame-buffer is being freed while in use by another
* context which, by the way, is TOTALLY UNSAFE!!! */
BLI_assert(context_ == GPU_context_active_get());
BLI_assert(context_ == GLContext::get());
GPU_framebuffer_restore();
}
}
void GLFrameBuffer::init(void)
{
context_ = static_cast<GLContext *>(GPU_context_active_get());
context_ = GLContext::get();
state_manager_ = static_cast<GLStateManager *>(context_->state_manager);
glGenFramebuffers(1, &fbo_id_);
@ -274,7 +274,7 @@ void GLFrameBuffer::bind(bool enabled_srgb)
this->init();
}
if (context_ != GPU_context_active_get()) {
if (context_ != GLContext::get()) {
BLI_assert(!"Trying to use the same frame-buffer in multiple context");
return;
}
@ -320,7 +320,7 @@ void GLFrameBuffer::clear(eGPUFrameBufferBits buffers,
float clear_depth,
uint clear_stencil)
{
BLI_assert(GPU_context_active_get() == context_);
BLI_assert(GLContext::get() == context_);
BLI_assert(context_->active_fb == this);
/* Save and restore the state. */
@ -360,7 +360,7 @@ void GLFrameBuffer::clear_attachment(GPUAttachmentType type,
eGPUDataFormat data_format,
const void *clear_value)
{
BLI_assert(GPU_context_active_get() == context_);
BLI_assert(GLContext::get() == context_);
BLI_assert(context_->active_fb == this);
/* Save and restore the state. */

View File

@ -160,7 +160,7 @@ void GLImmediate::end(void)
GL_CHECK_ERROR("Immediate Post-Unmap");
if (vertex_len > 0) {
GPU_context_active_get()->state_manager->apply_state();
GLContext::get()->state_manager->apply_state();
/* We convert the offset in vertex offset from the buffer's start.
* This works because we added some padding to align the first vertex vertex. */

View File

@ -44,7 +44,7 @@ GLShader::GLShader(const char *name) : Shader(name)
{
#if 0 /* Would be nice to have, but for now the Deferred compilation \
* does not have a GPUContext. */
BLI_assert(GPU_context_active_get() != NULL);
BLI_assert(GLContext::get() != NULL);
#endif
shader_program_ = glCreateProgram();
@ -61,7 +61,7 @@ GLShader::~GLShader(void)
{
#if 0 /* Would be nice to have, but for now the Deferred compilation \
* does not have a GPUContext. */
BLI_assert(GPU_context_active_get() != NULL);
BLI_assert(GLContext::get() != NULL);
#endif
/* Invalid handles are silently ignored. */
glDeleteShader(vert_shader_);

View File

@ -44,7 +44,7 @@ namespace blender::gpu {
GLTexture::GLTexture(const char *name) : Texture(name)
{
BLI_assert(GPU_context_active_get() != NULL);
BLI_assert(GLContext::get() != NULL);
glGenTextures(1, &tex_id_);
}
@ -54,7 +54,7 @@ GLTexture::~GLTexture()
if (framebuffer_) {
GPU_framebuffer_free(framebuffer_);
}
GPUContext *ctx = GPU_context_active_get();
GLContext *ctx = GLContext::get();
if (ctx != NULL && is_bound_) {
/* This avoid errors when the texture is still inside the bound texture array. */
ctx->state_manager->texture_unbind(this);
@ -662,7 +662,7 @@ void GLTexture::check_feedback_loop(void)
if (GPU_mip_render_workaround()) {
return;
}
GLFrameBuffer *fb = static_cast<GLFrameBuffer *>(GPU_context_active_get()->active_fb);
GLFrameBuffer *fb = static_cast<GLFrameBuffer *>(GLContext::get()->active_fb);
for (int i = 0; i < ARRAY_SIZE(fb_); i++) {
if (fb_[i] == fb) {
GPUAttachmentType type = fb_attachment_[i];

View File

@ -56,7 +56,7 @@ GLUniformBuf::~GLUniformBuf()
void GLUniformBuf::init(void)
{
BLI_assert(GPU_context_active_get());
BLI_assert(GLContext::get());
glGenBuffers(1, &ubo_id_);
glBindBuffer(GL_UNIFORM_BUFFER, ubo_id_);
@ -112,7 +112,7 @@ void GLUniformBuf::bind(int slot)
#ifdef DEBUG
BLI_assert(slot < 16);
static_cast<GLContext *>(GPU_context_active_get())->bound_ubo_slots |= 1 << slot;
GLContext::get()->bound_ubo_slots |= 1 << slot;
#endif
}
@ -122,7 +122,7 @@ void GLUniformBuf::unbind(void)
/* NOTE: This only unbinds the last bound slot. */
glBindBufferBase(GL_UNIFORM_BUFFER, slot_, 0);
/* Hope that the context did not change. */
static_cast<GLContext *>(GPU_context_active_get())->bound_ubo_slots &= ~(1 << slot_);
GLContext::get()->bound_ubo_slots &= ~(1 << slot_);
#endif
slot_ = 0;
}

View File

@ -138,7 +138,7 @@ void GLVertArray::update_bindings(const GLuint vao,
if (attr_mask != 0 && GLEW_ARB_vertex_attrib_binding) {
for (uint16_t mask = 1, a = 0; a < 16; a++, mask <<= 1) {
if (attr_mask & mask) {
GLContext *ctx = static_cast<GLContext *>(GPU_context_active_get());
GLContext *ctx = GLContext::get();
/* 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, ctx->default_attr_vbo_, (intptr_t)0, (intptr_t)0);

View File

@ -52,7 +52,7 @@ void GLVertBuf::release_data(void)
void GLVertBuf::duplicate_data(VertBuf *dst_)
{
BLI_assert(GPU_context_active_get() != NULL);
BLI_assert(GLContext::get() != NULL);
GLVertBuf *src = this;
GLVertBuf *dst = static_cast<GLVertBuf *>(dst_);
@ -82,7 +82,7 @@ void GLVertBuf::upload_data(void)
void GLVertBuf::bind(void)
{
BLI_assert(GPU_context_active_get() != NULL);
BLI_assert(GLContext::get() != NULL);
if (vbo_id_ == 0) {
glGenBuffers(1, &vbo_id_);