Commit: 48690d967a7731367cda01ab8dca64e7f4c3f6b5
Author: Clément Foucault
Date:   Tue Sep 8 04:12:12 2020 +0200
Branches: master
https://developer.blender.org/rB48690d967a7731367cda01ab8dca64e7f4c3f6b5

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()

===================================================================

M       source/blender/gpu/GPU_context.h
M       source/blender/gpu/intern/gpu_backend.hh
M       source/blender/gpu/intern/gpu_batch.cc
M       source/blender/gpu/intern/gpu_capabilities.cc
M       source/blender/gpu/intern/gpu_context.cc
M       source/blender/gpu/intern/gpu_context_private.hh
M       source/blender/gpu/intern/gpu_framebuffer.cc
M       source/blender/gpu/intern/gpu_immediate.cc
M       source/blender/gpu/intern/gpu_matrix.cc
M       source/blender/gpu/intern/gpu_shader.cc
M       source/blender/gpu/intern/gpu_state.cc
M       source/blender/gpu/intern/gpu_texture.cc
M       source/blender/gpu/opengl/gl_backend.hh
M       source/blender/gpu/opengl/gl_batch.cc
M       source/blender/gpu/opengl/gl_context.cc
M       source/blender/gpu/opengl/gl_context.hh
M       source/blender/gpu/opengl/gl_debug.cc
M       source/blender/gpu/opengl/gl_drawlist.cc
M       source/blender/gpu/opengl/gl_framebuffer.cc
M       source/blender/gpu/opengl/gl_immediate.cc
M       source/blender/gpu/opengl/gl_shader.cc
M       source/blender/gpu/opengl/gl_texture.cc
M       source/blender/gpu/opengl/gl_uniform_buffer.cc
M       source/blender/gpu/opengl/gl_vertex_array.cc
M       source/blender/gpu/opengl/gl_vertex_buffer.cc

===================================================================

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<Batch *>(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 <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);
-
-struct GPUMatrixState *gpu_context_active_matrix_state_get(void);
+/* 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);
+}
+
+}  // 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);
 
-  FrameB

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to