From 48690d967a7731367cda01ab8dca64e7f4c3f6b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Tue, 8 Sep 2020 04:12:12 +0200 Subject: 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() --- source/blender/gpu/GPU_context.h | 5 ++- source/blender/gpu/intern/gpu_backend.hh | 6 +-- source/blender/gpu/intern/gpu_batch.cc | 2 +- source/blender/gpu/intern/gpu_capabilities.cc | 4 +- source/blender/gpu/intern/gpu_context.cc | 40 +++++++++++-------- source/blender/gpu/intern/gpu_context_private.hh | 51 ++++++++++++++---------- source/blender/gpu/intern/gpu_framebuffer.cc | 22 +++++----- source/blender/gpu/intern/gpu_immediate.cc | 2 +- source/blender/gpu/intern/gpu_matrix.cc | 16 ++++---- source/blender/gpu/intern/gpu_shader.cc | 4 +- source/blender/gpu/intern/gpu_state.cc | 40 +++++++++---------- source/blender/gpu/intern/gpu_texture.cc | 10 ++--- source/blender/gpu/opengl/gl_backend.hh | 2 +- source/blender/gpu/opengl/gl_batch.cc | 10 ++--- source/blender/gpu/opengl/gl_context.cc | 10 ++--- source/blender/gpu/opengl/gl_context.hh | 13 ++++-- source/blender/gpu/opengl/gl_debug.cc | 2 +- source/blender/gpu/opengl/gl_drawlist.cc | 6 +-- source/blender/gpu/opengl/gl_framebuffer.cc | 12 +++--- source/blender/gpu/opengl/gl_immediate.cc | 2 +- source/blender/gpu/opengl/gl_shader.cc | 4 +- source/blender/gpu/opengl/gl_texture.cc | 6 +-- source/blender/gpu/opengl/gl_uniform_buffer.cc | 6 +-- source/blender/gpu/opengl/gl_vertex_array.cc | 2 +- source/blender/gpu/opengl/gl_vertex_buffer.cc | 4 +- 25 files changed, 153 insertions(+), 128 deletions(-) diff --git a/source/blender/gpu/GPU_context.h b/source/blender/gpu/GPU_context.h index be7e604fb96..82f13424502 100644 --- a/source/blender/gpu/GPU_context.h +++ b/source/blender/gpu/GPU_context.h @@ -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 *); diff --git a/source/blender/gpu/intern/gpu_backend.hh b/source/blender/gpu/intern/gpu_backend.hh index 1a6a6668b42..04ec82a9213 100644 --- a/source/blender/gpu/intern/gpu_backend.hh +++ b/source/blender/gpu/intern/gpu_backend.hh @@ -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; diff --git a/source/blender/gpu/intern/gpu_batch.cc b/source/blender/gpu/intern/gpu_batch.cc index d6f4f223a83..de079a89de7 100644 --- a/source/blender/gpu/intern/gpu_batch.cc +++ b/source/blender/gpu/intern/gpu_batch.cc @@ -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(gpu_batch); if (v_count == 0) { diff --git a/source/blender/gpu/intern/gpu_capabilities.cc b/source/blender/gpu/intern/gpu_capabilities.cc index 0ee25ea2569..a79ce27ba63 100644 --- a/source/blender/gpu/intern/gpu_capabilities.cc +++ b/source/blender/gpu/intern/gpu_capabilities.cc @@ -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; } /** \} */ diff --git a/source/blender/gpu/intern/gpu_context.cc b/source/blender/gpu/intern/gpu_context.cc index 8ebd49a658e..188225b3eed 100644 --- a/source/blender/gpu/intern/gpu_context.cc +++ b/source/blender/gpu/intern/gpu_context.cc @@ -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()); } /* -------------------------------------------------------------------- */ diff --git a/source/blender/gpu/intern/gpu_context_private.hh b/source/blender/gpu/intern/gpu_context_private.hh index 5344cc6fb87..bc07bea4bb1 100644 --- a/source/blender/gpu/intern/gpu_context_private.hh +++ b/source/blender/gpu/intern/gpu_context_private.hh @@ -34,22 +34,20 @@ #include "gpu_shader_private.hh" #include "gpu_state_private.hh" -#include #include -#include -#include -#include 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); - -struct GPUMatrixState *gpu_context_active_matrix_state_get(void); +/* Syntacting suggar. */ +static inline GPUContext *wrap(Context *ctx) +{ + return reinterpret_cast(ctx); +} +static inline Context *unwrap(GPUContext *ctx) +{ + return reinterpret_cast(ctx); +} +static inline const Context *unwrap(const GPUContext *ctx) +{ + return reinterpret_cast(ctx); +} + +} // namespace blender::gpu \ No newline at end of file diff --git a/source/blender/gpu/intern/gpu_framebuffer.cc b/source/blender/gpu/intern/gpu_framebuffer.cc index e548eb241cf..88779dead28 100644 --- a/source/blender/gpu/intern/gpu_framebuffer.cc +++ b/source/blender/gpu/intern/gpu_framebuffer.cc @@ -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); } diff --git a/source/blender/gpu/intern/gpu_immediate.cc b/source/blender/gpu/intern/gpu_immediate.cc index 9fc5e03a796..0a488c0dfc0 100644 --- a/source/blender/gpu/intern/gpu_immediate.cc +++ b/source/blender/gpu/intern/gpu_immediate.cc @@ -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) diff --git a/source/blender/gpu/intern/gpu_matrix.cc b/source/blender/gpu/intern/gpu_matrix.cc index cdb6d303588..0274966d4b9 100644 --- a/source/blender/gpu/intern/gpu_matrix.cc +++ b/source/blender/gpu/intern/gpu_matrix.cc @@ -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; } diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc index eb0f7935b63..1bd076f96f8 100644 --- a/source/blender/gpu/intern/gpu_shader.cc +++ b/source/blender/gpu/intern/gpu_shader.cc @@ -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(); } diff --git a/source/blender/gpu/intern/gpu_state.cc b/source/blender/gpu/intern/gpu_state.cc index 68f0c290bc6..529c8795327 100644 --- a/source/blender/gpu/intern/gpu_state.cc +++ b/source/blender/gpu/intern/gpu_state.cc @@ -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(); } /** \} */ diff --git a/source/blender/gpu/intern/gpu_texture.cc b/source/blender/gpu/intern/gpu_texture.cc index 05b97129f00..b22fd53f0f6 100644 --- a/source/blender/gpu/intern/gpu_texture.cc +++ b/source/blender/gpu/intern/gpu_texture.cc @@ -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(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(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(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) diff --git a/source/blender/gpu/opengl/gl_backend.hh b/source/blender/gpu/opengl/gl_backend.hh index 94ac1692108..231e5811b45 100644 --- a/source/blender/gpu/opengl/gl_backend.hh +++ b/source/blender/gpu/opengl/gl_backend.hh @@ -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_); }; diff --git a/source/blender/gpu/opengl/gl_batch.cc b/source/blender/gpu/opengl/gl_batch.cc index c28d3e33e65..b25bafad6a3 100644 --- a/source/blender/gpu/opengl/gl_batch.cc +++ b/source/blender/gpu/opengl/gl_batch.cc @@ -151,7 +151,7 @@ void GLVaoCache::remove(const GLShaderInterface *interface) void GLVaoCache::clear(void) { - GLContext *ctx = static_cast(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(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(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(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; diff --git a/source/blender/gpu/opengl/gl_context.cc b/source/blender/gpu/opengl/gl_context.cc index 30279ccade1..6b3b06ef12b 100644 --- a/source/blender/gpu/opengl/gl_context.cc +++ b/source/blender/gpu/opengl/gl_context.cc @@ -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 &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 { diff --git a/source/blender/gpu/opengl/gl_context.hh b/source/blender/gpu/opengl/gl_context.hh index ef0c13719e2..63014903e0a 100644 --- a/source/blender/gpu/opengl/gl_context.hh +++ b/source/blender/gpu/opengl/gl_context.hh @@ -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(GPU_context_active_get()); + return static_cast(Context::get()); + } + + static GLStateManager *state_manager_active_get() + { + GLContext *ctx = GLContext::get(); return static_cast(ctx->state_manager); }; @@ -122,6 +127,8 @@ class GLContext : public GPUContext { private: static void orphans_add(Vector &orphan_list, std::mutex &list_mutex, GLuint id); void orphans_clear(void); + + MEM_CXX_CLASS_ALLOC_FUNCS("GLContext") }; } // namespace gpu diff --git a/source/blender/gpu/opengl/gl_debug.cc b/source/blender/gpu/opengl/gl_debug.cc index d54ea0919b6..53a71516018 100644 --- a/source/blender/gpu/opengl/gl_debug.cc +++ b/source/blender/gpu/opengl/gl_debug.cc @@ -197,7 +197,7 @@ void check_gl_resources(const char *info) return; } - GLContext *ctx = static_cast(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. */ diff --git a/source/blender/gpu/opengl/gl_drawlist.cc b/source/blender/gpu/opengl/gl_drawlist.cc index 039ef18ad72..6e3b1107b9c 100644 --- a/source/blender/gpu/opengl/gl_drawlist.cc +++ b/source/blender/gpu/opengl/gl_drawlist.cc @@ -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(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 diff --git a/source/blender/gpu/opengl/gl_framebuffer.cc b/source/blender/gpu/opengl/gl_framebuffer.cc index 61270fe0acc..85fa973daff 100644 --- a/source/blender/gpu/opengl/gl_framebuffer.cc +++ b/source/blender/gpu/opengl/gl_framebuffer.cc @@ -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(GPU_context_active_get()); + context_ = GLContext::get(); state_manager_ = static_cast(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. */ diff --git a/source/blender/gpu/opengl/gl_immediate.cc b/source/blender/gpu/opengl/gl_immediate.cc index 7f12f41a598..26b1df88b64 100644 --- a/source/blender/gpu/opengl/gl_immediate.cc +++ b/source/blender/gpu/opengl/gl_immediate.cc @@ -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. */ diff --git a/source/blender/gpu/opengl/gl_shader.cc b/source/blender/gpu/opengl/gl_shader.cc index a303229fc3c..ca38efe37b5 100644 --- a/source/blender/gpu/opengl/gl_shader.cc +++ b/source/blender/gpu/opengl/gl_shader.cc @@ -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_); diff --git a/source/blender/gpu/opengl/gl_texture.cc b/source/blender/gpu/opengl/gl_texture.cc index b4a8e43c931..ae3923b960f 100644 --- a/source/blender/gpu/opengl/gl_texture.cc +++ b/source/blender/gpu/opengl/gl_texture.cc @@ -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(GPU_context_active_get()->active_fb); + GLFrameBuffer *fb = static_cast(GLContext::get()->active_fb); for (int i = 0; i < ARRAY_SIZE(fb_); i++) { if (fb_[i] == fb) { GPUAttachmentType type = fb_attachment_[i]; diff --git a/source/blender/gpu/opengl/gl_uniform_buffer.cc b/source/blender/gpu/opengl/gl_uniform_buffer.cc index a06fadf4785..62c590d289f 100644 --- a/source/blender/gpu/opengl/gl_uniform_buffer.cc +++ b/source/blender/gpu/opengl/gl_uniform_buffer.cc @@ -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(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(GPU_context_active_get())->bound_ubo_slots &= ~(1 << slot_); + GLContext::get()->bound_ubo_slots &= ~(1 << slot_); #endif slot_ = 0; } diff --git a/source/blender/gpu/opengl/gl_vertex_array.cc b/source/blender/gpu/opengl/gl_vertex_array.cc index 4e49828d39d..732221cfab3 100644 --- a/source/blender/gpu/opengl/gl_vertex_array.cc +++ b/source/blender/gpu/opengl/gl_vertex_array.cc @@ -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(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); diff --git a/source/blender/gpu/opengl/gl_vertex_buffer.cc b/source/blender/gpu/opengl/gl_vertex_buffer.cc index d56f5d1aa52..a724c94775e 100644 --- a/source/blender/gpu/opengl/gl_vertex_buffer.cc +++ b/source/blender/gpu/opengl/gl_vertex_buffer.cc @@ -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(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_); -- cgit v1.2.3