From d2e9de93b8d1d6cd45abce8164d0f92af8f636d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Tue, 8 Sep 2020 03:34:47 +0200 Subject: GPU: Cleanup implementation casts - Use the syntactic wrap/unwrap method to make code more readable. - Update comment about hidden struct behind opaque types. - Cleanup GPUDrawList type. --- source/blender/gpu/intern/gpu_drawlist.cc | 16 +++---- source/blender/gpu/intern/gpu_drawlist_private.hh | 16 +++++++ source/blender/gpu/intern/gpu_framebuffer.cc | 51 ++++++++++------------ .../blender/gpu/intern/gpu_framebuffer_private.hh | 14 ++++++ source/blender/gpu/intern/gpu_shader.cc | 35 +++++++-------- source/blender/gpu/intern/gpu_shader_private.hh | 16 ++++++- source/blender/gpu/intern/gpu_texture_private.hh | 4 ++ source/blender/gpu/intern/gpu_uniform_buffer.cc | 12 ++--- .../gpu/intern/gpu_uniform_buffer_private.hh | 14 ++++++ .../gpu/intern/gpu_vertex_buffer_private.hh | 4 ++ 10 files changed, 120 insertions(+), 62 deletions(-) (limited to 'source/blender/gpu/intern') diff --git a/source/blender/gpu/intern/gpu_drawlist.cc b/source/blender/gpu/intern/gpu_drawlist.cc index 7b807a2fa80..ecea4f7c5e4 100644 --- a/source/blender/gpu/intern/gpu_drawlist.cc +++ b/source/blender/gpu/intern/gpu_drawlist.cc @@ -34,26 +34,26 @@ using namespace blender::gpu; -GPUDrawList GPU_draw_list_create(int list_length) +GPUDrawList *GPU_draw_list_create(int list_length) { DrawList *list_ptr = GPUBackend::get()->drawlist_alloc(list_length); - return reinterpret_cast(list_ptr); + return wrap(list_ptr); } -void GPU_draw_list_discard(GPUDrawList list) +void GPU_draw_list_discard(GPUDrawList *list) { - DrawList *list_ptr = reinterpret_cast(list); + DrawList *list_ptr = unwrap(list); delete list_ptr; } -void GPU_draw_list_append(GPUDrawList list, GPUBatch *batch, int i_first, int i_count) +void GPU_draw_list_append(GPUDrawList *list, GPUBatch *batch, int i_first, int i_count) { - DrawList *list_ptr = reinterpret_cast(list); + DrawList *list_ptr = unwrap(list); list_ptr->append(batch, i_first, i_count); } -void GPU_draw_list_submit(GPUDrawList list) +void GPU_draw_list_submit(GPUDrawList *list) { - DrawList *list_ptr = reinterpret_cast(list); + DrawList *list_ptr = unwrap(list); list_ptr->submit(); } diff --git a/source/blender/gpu/intern/gpu_drawlist_private.hh b/source/blender/gpu/intern/gpu_drawlist_private.hh index ddb09fb0c89..fd223c3f255 100644 --- a/source/blender/gpu/intern/gpu_drawlist_private.hh +++ b/source/blender/gpu/intern/gpu_drawlist_private.hh @@ -25,6 +25,8 @@ #include "MEM_guardedalloc.h" +#include "GPU_drawlist.h" + namespace blender { namespace gpu { @@ -40,5 +42,19 @@ class DrawList { virtual void submit() = 0; }; +/* Syntacting suggar. */ +static inline GPUDrawList *wrap(DrawList *vert) +{ + return reinterpret_cast(vert); +} +static inline DrawList *unwrap(GPUDrawList *vert) +{ + return reinterpret_cast(vert); +} +static inline const DrawList *unwrap(const GPUDrawList *vert) +{ + return reinterpret_cast(vert); +} + } // namespace gpu } // namespace blender diff --git a/source/blender/gpu/intern/gpu_framebuffer.cc b/source/blender/gpu/intern/gpu_framebuffer.cc index 05cc8a30a43..e548eb241cf 100644 --- a/source/blender/gpu/intern/gpu_framebuffer.cc +++ b/source/blender/gpu/intern/gpu_framebuffer.cc @@ -194,21 +194,20 @@ GPUFrameBuffer *GPU_framebuffer_create(const char *name) { /* We generate the FB object later at first use in order to * create the frame-buffer in the right opengl context. */ - return (GPUFrameBuffer *)GPUBackend::get()->framebuffer_alloc(name); + return wrap(GPUBackend::get()->framebuffer_alloc(name)); } void GPU_framebuffer_free(GPUFrameBuffer *gpu_fb) { - delete reinterpret_cast(gpu_fb); + delete unwrap(gpu_fb); } /* ---------- Binding ----------- */ void GPU_framebuffer_bind(GPUFrameBuffer *gpu_fb) { - FrameBuffer *fb = reinterpret_cast(gpu_fb); const bool enable_srgb = true; - fb->bind(enable_srgb); + unwrap(gpu_fb)->bind(enable_srgb); } /** @@ -216,9 +215,8 @@ void GPU_framebuffer_bind(GPUFrameBuffer *gpu_fb) */ void GPU_framebuffer_bind_no_srgb(GPUFrameBuffer *gpu_fb) { - FrameBuffer *fb = reinterpret_cast(gpu_fb); const bool enable_srgb = false; - fb->bind(enable_srgb); + unwrap(gpu_fb)->bind(enable_srgb); } /** @@ -244,14 +242,14 @@ void GPU_framebuffer_restore(void) GPUFrameBuffer *GPU_framebuffer_active_get(void) { GPUContext *ctx = GPU_context_active_get(); - return reinterpret_cast(ctx ? ctx->active_fb : NULL); + 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(); - return reinterpret_cast(ctx ? ctx->back_left : NULL); + return wrap(ctx ? ctx->back_left : NULL); } bool GPU_framebuffer_bound(GPUFrameBuffer *gpu_fb) @@ -263,14 +261,14 @@ bool GPU_framebuffer_bound(GPUFrameBuffer *gpu_fb) bool GPU_framebuffer_check_valid(GPUFrameBuffer *gpu_fb, char err_out[256]) { - return reinterpret_cast(gpu_fb)->check(err_out); + return unwrap(gpu_fb)->check(err_out); } void GPU_framebuffer_texture_attach_ex(GPUFrameBuffer *gpu_fb, GPUAttachment attachment, int slot) { Texture *tex = reinterpret_cast(attachment.tex); GPUAttachmentType type = tex->attachment_type(slot); - reinterpret_cast(gpu_fb)->attachment_set(type, attachment); + unwrap(gpu_fb)->attachment_set(type, attachment); } void GPU_framebuffer_texture_attach(GPUFrameBuffer *fb, GPUTexture *tex, int slot, int mip) @@ -293,10 +291,9 @@ void GPU_framebuffer_texture_cubeface_attach( GPU_framebuffer_texture_attach_ex(fb, attachment, slot); } -void GPU_framebuffer_texture_detach(GPUFrameBuffer *gpu_fb, GPUTexture *tex) +void GPU_framebuffer_texture_detach(GPUFrameBuffer *fb, GPUTexture *tex) { - FrameBuffer *fb = reinterpret_cast(gpu_fb); - reinterpret_cast(tex)->detach_from(fb); + unwrap(tex)->detach_from(unwrap(fb)); } /** @@ -309,7 +306,7 @@ void GPU_framebuffer_config_array(GPUFrameBuffer *gpu_fb, const GPUAttachment *config, int config_len) { - FrameBuffer *fb = reinterpret_cast(gpu_fb); + FrameBuffer *fb = unwrap(gpu_fb); const GPUAttachment &depth_attachment = config[0]; Span color_attachments(config + 1, config_len - 1); @@ -346,12 +343,12 @@ void GPU_framebuffer_config_array(GPUFrameBuffer *gpu_fb, void GPU_framebuffer_viewport_set(GPUFrameBuffer *gpu_fb, int x, int y, int width, int height) { int viewport_rect[4] = {x, y, width, height}; - reinterpret_cast(gpu_fb)->viewport_set(viewport_rect); + unwrap(gpu_fb)->viewport_set(viewport_rect); } void GPU_framebuffer_viewport_get(GPUFrameBuffer *gpu_fb, int r_viewport[4]) { - reinterpret_cast(gpu_fb)->viewport_get(r_viewport); + unwrap(gpu_fb)->viewport_get(r_viewport); } /** @@ -359,7 +356,7 @@ void GPU_framebuffer_viewport_get(GPUFrameBuffer *gpu_fb, int r_viewport[4]) */ void GPU_framebuffer_viewport_reset(GPUFrameBuffer *gpu_fb) { - reinterpret_cast(gpu_fb)->viewport_reset(); + unwrap(gpu_fb)->viewport_reset(); } /* ---------- Framebuffer Operations ----------- */ @@ -370,7 +367,7 @@ void GPU_framebuffer_clear(GPUFrameBuffer *gpu_fb, float clear_depth, uint clear_stencil) { - reinterpret_cast(gpu_fb)->clear(buffers, clear_col, clear_depth, clear_stencil); + unwrap(gpu_fb)->clear(buffers, clear_col, clear_depth, clear_stencil); } /** @@ -378,7 +375,7 @@ void GPU_framebuffer_clear(GPUFrameBuffer *gpu_fb, */ void GPU_framebuffer_multi_clear(GPUFrameBuffer *gpu_fb, const float (*clear_cols)[4]) { - reinterpret_cast(gpu_fb)->clear_multi(clear_cols); + unwrap(gpu_fb)->clear_multi(clear_cols); } void GPU_clear_color(float red, float green, float blue, float alpha) @@ -397,7 +394,7 @@ void GPU_framebuffer_read_depth( GPUFrameBuffer *gpu_fb, int x, int y, int w, int h, eGPUDataFormat format, void *data) { int rect[4] = {x, y, w, h}; - reinterpret_cast(gpu_fb)->read(GPU_DEPTH_BIT, format, rect, 1, 1, data); + unwrap(gpu_fb)->read(GPU_DEPTH_BIT, format, rect, 1, 1, data); } void GPU_framebuffer_read_color(GPUFrameBuffer *gpu_fb, @@ -411,7 +408,7 @@ void GPU_framebuffer_read_color(GPUFrameBuffer *gpu_fb, void *data) { int rect[4] = {x, y, w, h}; - reinterpret_cast(gpu_fb)->read(GPU_COLOR_BIT, format, rect, channels, slot, data); + unwrap(gpu_fb)->read(GPU_COLOR_BIT, format, rect, channels, slot, data); } /* TODO(fclem) rename to read_color. */ @@ -430,8 +427,8 @@ void GPU_framebuffer_blit(GPUFrameBuffer *gpufb_read, int write_slot, eGPUFrameBufferBits blit_buffers) { - FrameBuffer *fb_read = reinterpret_cast(gpufb_read); - FrameBuffer *fb_write = reinterpret_cast(gpufb_write); + FrameBuffer *fb_read = unwrap(gpufb_read); + FrameBuffer *fb_write = unwrap(gpufb_write); BLI_assert(blit_buffers != 0); FrameBuffer *prev_fb = GPU_context_active_get()->active_fb; @@ -473,7 +470,7 @@ void GPU_framebuffer_recursive_downsample(GPUFrameBuffer *gpu_fb, void (*callback)(void *userData, int level), void *userData) { - reinterpret_cast(gpu_fb)->recursive_downsample(max_lvl, callback, userData); + unwrap(gpu_fb)->recursive_downsample(max_lvl, callback, userData); } /** \} */ @@ -616,9 +613,9 @@ void GPU_offscreen_bind(GPUOffScreen *ofs, bool save) { if (save) { GPUFrameBuffer *fb = GPU_framebuffer_active_get(); - gpuPushFrameBuffer(reinterpret_cast(fb)); + gpuPushFrameBuffer(fb); } - reinterpret_cast(gpu_offscreen_fb_get(ofs))->bind(false); + unwrap(gpu_offscreen_fb_get(ofs))->bind(false); } void GPU_offscreen_unbind(GPUOffScreen *UNUSED(ofs), bool restore) @@ -639,7 +636,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(); - FrameBuffer *ofs_fb = reinterpret_cast(gpu_offscreen_fb_get(ofs)); + 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_framebuffer_private.hh b/source/blender/gpu/intern/gpu_framebuffer_private.hh index 81507f4111a..da44773039b 100644 --- a/source/blender/gpu/intern/gpu_framebuffer_private.hh +++ b/source/blender/gpu/intern/gpu_framebuffer_private.hh @@ -209,6 +209,20 @@ class FrameBuffer { }; }; +/* Syntacting suggar. */ +static inline GPUFrameBuffer *wrap(FrameBuffer *vert) +{ + return reinterpret_cast(vert); +} +static inline FrameBuffer *unwrap(GPUFrameBuffer *vert) +{ + return reinterpret_cast(vert); +} +static inline const FrameBuffer *unwrap(const GPUFrameBuffer *vert) +{ + return reinterpret_cast(vert); +} + #undef DEBUG_NAME_LEN } // namespace gpu diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc index 38acc773c49..eb0f7935b63 100644 --- a/source/blender/gpu/intern/gpu_shader.cc +++ b/source/blender/gpu/intern/gpu_shader.cc @@ -52,11 +52,6 @@ extern "C" char datatoc_gpu_shader_colorspace_lib_glsl[]; using namespace blender; using namespace blender::gpu; -/** Opaque type hidding blender::gpu::Shader */ -struct GPUShader { - char _pad[1]; -}; - /* -------------------------------------------------------------------- */ /** \name Debug functions * \{ */ @@ -333,12 +328,12 @@ GPUShader *GPU_shader_create_ex(const char *vertcode, return NULL; }; - return reinterpret_cast(shader); + return wrap(shader); } void GPU_shader_free(GPUShader *shader) { - delete reinterpret_cast(shader); + delete unwrap(shader); } /** \} */ @@ -460,7 +455,7 @@ struct GPUShader *GPU_shader_create_from_arrays_impl( void GPU_shader_bind(GPUShader *gpu_shader) { - Shader *shader = reinterpret_cast(gpu_shader); + Shader *shader = unwrap(gpu_shader); GPUContext *ctx = GPU_context_active_get(); @@ -481,7 +476,7 @@ void GPU_shader_unbind(void) #ifndef NDEBUG GPUContext *ctx = GPU_context_active_get(); if (ctx->shader) { - reinterpret_cast(ctx->shader)->unbind(); + ctx->shader->unbind(); } ctx->shader = NULL; #endif @@ -497,12 +492,12 @@ void GPU_shader_unbind(void) bool GPU_shader_transform_feedback_enable(GPUShader *shader, GPUVertBuf *vertbuf) { - return reinterpret_cast(shader)->transform_feedback_enable(vertbuf); + return unwrap(shader)->transform_feedback_enable(vertbuf); } void GPU_shader_transform_feedback_disable(GPUShader *shader) { - reinterpret_cast(shader)->transform_feedback_disable(); + unwrap(shader)->transform_feedback_disable(); } /** \} */ @@ -513,48 +508,48 @@ void GPU_shader_transform_feedback_disable(GPUShader *shader) int GPU_shader_get_uniform(GPUShader *shader, const char *name) { - ShaderInterface *interface = reinterpret_cast(shader)->interface; + ShaderInterface *interface = unwrap(shader)->interface; const ShaderInput *uniform = interface->uniform_get(name); return uniform ? uniform->location : -1; } int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin) { - ShaderInterface *interface = reinterpret_cast(shader)->interface; + ShaderInterface *interface = unwrap(shader)->interface; return interface->uniform_builtin((GPUUniformBuiltin)builtin); } int GPU_shader_get_builtin_block(GPUShader *shader, int builtin) { - ShaderInterface *interface = reinterpret_cast(shader)->interface; + ShaderInterface *interface = unwrap(shader)->interface; return interface->ubo_builtin((GPUUniformBlockBuiltin)builtin); } /* DEPRECATED. */ int GPU_shader_get_uniform_block(GPUShader *shader, const char *name) { - ShaderInterface *interface = reinterpret_cast(shader)->interface; + ShaderInterface *interface = unwrap(shader)->interface; const ShaderInput *ubo = interface->ubo_get(name); return ubo ? ubo->location : -1; } int GPU_shader_get_uniform_block_binding(GPUShader *shader, const char *name) { - ShaderInterface *interface = reinterpret_cast(shader)->interface; + ShaderInterface *interface = unwrap(shader)->interface; const ShaderInput *ubo = interface->ubo_get(name); return ubo ? ubo->binding : -1; } int GPU_shader_get_texture_binding(GPUShader *shader, const char *name) { - ShaderInterface *interface = reinterpret_cast(shader)->interface; + ShaderInterface *interface = unwrap(shader)->interface; const ShaderInput *tex = interface->uniform_get(name); return tex ? tex->binding : -1; } int GPU_shader_get_attribute(GPUShader *shader, const char *name) { - ShaderInterface *interface = reinterpret_cast(shader)->interface; + ShaderInterface *interface = unwrap(shader)->interface; const ShaderInput *attr = interface->attr_get(name); return attr ? attr->location : -1; } @@ -581,13 +576,13 @@ int GPU_shader_get_program(GPUShader *UNUSED(shader)) void GPU_shader_uniform_vector( GPUShader *shader, int loc, int len, int arraysize, const float *value) { - reinterpret_cast(shader)->uniform_float(loc, len, arraysize, value); + unwrap(shader)->uniform_float(loc, len, arraysize, value); } void GPU_shader_uniform_vector_int( GPUShader *shader, int loc, int len, int arraysize, const int *value) { - reinterpret_cast(shader)->uniform_int(loc, len, arraysize, value); + unwrap(shader)->uniform_int(loc, len, arraysize, value); } void GPU_shader_uniform_int(GPUShader *shader, int location, int value) diff --git a/source/blender/gpu/intern/gpu_shader_private.hh b/source/blender/gpu/intern/gpu_shader_private.hh index fa086892760..b7acc0f9353 100644 --- a/source/blender/gpu/intern/gpu_shader_private.hh +++ b/source/blender/gpu/intern/gpu_shader_private.hh @@ -23,8 +23,8 @@ #include "BLI_span.hh" #include "GPU_shader.h" -#include "gpu_vertex_buffer_private.hh" #include "gpu_shader_interface.hh" +#include "gpu_vertex_buffer_private.hh" namespace blender { namespace gpu { @@ -73,6 +73,20 @@ class Shader { void print_errors(Span sources, char *log, const char *stage); }; +/* Syntacting suggar. */ +static inline GPUShader *wrap(Shader *vert) +{ + return reinterpret_cast(vert); +} +static inline Shader *unwrap(GPUShader *vert) +{ + return reinterpret_cast(vert); +} +static inline const Shader *unwrap(const GPUShader *vert) +{ + return reinterpret_cast(vert); +} + } // namespace gpu } // namespace blender diff --git a/source/blender/gpu/intern/gpu_texture_private.hh b/source/blender/gpu/intern/gpu_texture_private.hh index 19022b228b2..04156632c5e 100644 --- a/source/blender/gpu/intern/gpu_texture_private.hh +++ b/source/blender/gpu/intern/gpu_texture_private.hh @@ -68,6 +68,10 @@ ENUM_OPERATORS(eGPUTextureType) /* Maximum number of FBOs a texture can be attached to. */ #define GPU_TEX_MAX_FBO_ATTACHED 14 +/** + * Implementation of Textures. + * Base class which is then specialized for each implementation (GL, VK, ...). + **/ class Texture { public: /** Internal Sampler state. */ diff --git a/source/blender/gpu/intern/gpu_uniform_buffer.cc b/source/blender/gpu/intern/gpu_uniform_buffer.cc index 4926a5fa2dc..2dea98f03ca 100644 --- a/source/blender/gpu/intern/gpu_uniform_buffer.cc +++ b/source/blender/gpu/intern/gpu_uniform_buffer.cc @@ -198,7 +198,7 @@ GPUUniformBuf *GPU_uniformbuf_create_ex(size_t size, const void *data, const cha if (data != NULL) { ubo->update(data); } - return reinterpret_cast(ubo); + return wrap(ubo); } /** @@ -222,27 +222,27 @@ GPUUniformBuf *GPU_uniformbuf_create_from_list(ListBase *inputs, const char *nam UniformBuf *ubo = GPUBackend::get()->uniformbuf_alloc(buffer_size, name); /* Defer data upload. */ ubo->attach_data(data); - return reinterpret_cast(ubo); + return wrap(ubo); } void GPU_uniformbuf_free(GPUUniformBuf *ubo) { - delete reinterpret_cast(ubo); + delete unwrap(ubo); } void GPU_uniformbuf_update(GPUUniformBuf *ubo, const void *data) { - reinterpret_cast(ubo)->update(data); + unwrap(ubo)->update(data); } void GPU_uniformbuf_bind(GPUUniformBuf *ubo, int slot) { - reinterpret_cast(ubo)->bind(slot); + unwrap(ubo)->bind(slot); } void GPU_uniformbuf_unbind(GPUUniformBuf *ubo) { - reinterpret_cast(ubo)->unbind(); + unwrap(ubo)->unbind(); } void GPU_uniformbuf_unbind_all(void) diff --git a/source/blender/gpu/intern/gpu_uniform_buffer_private.hh b/source/blender/gpu/intern/gpu_uniform_buffer_private.hh index cf6447ccd37..00d10776864 100644 --- a/source/blender/gpu/intern/gpu_uniform_buffer_private.hh +++ b/source/blender/gpu/intern/gpu_uniform_buffer_private.hh @@ -63,6 +63,20 @@ class UniformBuf { } }; +/* Syntacting suggar. */ +static inline GPUUniformBuf *wrap(UniformBuf *vert) +{ + return reinterpret_cast(vert); +} +static inline UniformBuf *unwrap(GPUUniformBuf *vert) +{ + return reinterpret_cast(vert); +} +static inline const UniformBuf *unwrap(const GPUUniformBuf *vert) +{ + return reinterpret_cast(vert); +} + #undef DEBUG_NAME_LEN } // namespace gpu diff --git a/source/blender/gpu/intern/gpu_vertex_buffer_private.hh b/source/blender/gpu/intern/gpu_vertex_buffer_private.hh index 61af0a215a9..f1de0a2ac96 100644 --- a/source/blender/gpu/intern/gpu_vertex_buffer_private.hh +++ b/source/blender/gpu/intern/gpu_vertex_buffer_private.hh @@ -29,6 +29,10 @@ namespace blender::gpu { +/** + * Implementation of Vertex Buffers. + * Base class which is then specialized for each implementation (GL, VK, ...). + **/ class VertBuf { public: static size_t memory_usage; -- cgit v1.2.3