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.
This commit is contained in:
Clément Foucault 2020-09-08 03:34:47 +02:00
parent 33b25b6a9e
commit d2e9de93b8
16 changed files with 134 additions and 80 deletions

View File

@ -32,14 +32,15 @@ extern "C" {
struct GPUBatch;
typedef void *GPUDrawList; /* Opaque pointer. */
/** Opaque type hiding blender::gpu::DrawList. */
typedef struct GPUDrawList GPUDrawList;
/* Create a list with at least length drawcalls. Length can affect performance. */
GPUDrawList GPU_draw_list_create(int length);
void GPU_draw_list_discard(GPUDrawList list);
GPUDrawList *GPU_draw_list_create(int length);
void GPU_draw_list_discard(GPUDrawList *list);
void GPU_draw_list_append(GPUDrawList list, GPUBatch *batch, int i_first, int i_count);
void GPU_draw_list_submit(GPUDrawList list);
void GPU_draw_list_append(GPUDrawList *list, GPUBatch *batch, int i_first, int i_count);
void GPU_draw_list_submit(GPUDrawList *list);
#ifdef __cplusplus
}

View File

@ -54,10 +54,8 @@ typedef enum eGPUBackBuffer {
GPU_BACKBUFFER_RIGHT,
} eGPUBackBuffer;
/** Opaque pointer hiding blender::gpu::FrameBuffer. */
typedef struct GPUFrameBuffer {
void *dummy;
} GPUFrameBuffer;
/** Opaque type hiding blender::gpu::FrameBuffer. */
typedef struct GPUFrameBuffer GPUFrameBuffer;
typedef struct GPUOffScreen GPUOffScreen;

View File

@ -31,11 +31,7 @@
extern "C" {
#endif
/**
* IMPORTANT: Do not allocate manually as the real struct is bigger (i.e: GLIndexBuf). This is only
* the common and "public" part of the struct. Use the provided allocator.
* TODO(fclem) Make the content of this struct hidden and expose getters/setters.
**/
/** Opaque type hiding blender::gpu::IndexBuf. */
typedef struct GPUIndexBuf GPUIndexBuf;
GPUIndexBuf *GPU_indexbuf_calloc(void);

View File

@ -36,6 +36,8 @@ struct MovieClipUser;
struct PreviewImage;
struct GPUFrameBuffer;
/** Opaque type hiding blender::gpu::Texture. */
typedef struct GPUTexture GPUTexture;
/* GPU Samplers state

View File

@ -36,10 +36,8 @@ extern "C" {
struct ListBase;
/** Opaque pointer hiding blender::gpu::UniformBuf. */
typedef struct GPUUniformBuf {
void *dummy;
} GPUUniformBuf;
/** Opaque type hiding blender::gpu::UniformBuf. */
typedef struct GPUUniformBuf GPUUniformBuf;
GPUUniformBuf *GPU_uniformbuf_create_ex(size_t size, const void *data, const char *name);
GPUUniformBuf *GPU_uniformbuf_create_from_list(struct ListBase *inputs, const char *name);

View File

@ -61,6 +61,7 @@ typedef enum {
GPU_USAGE_DYNAMIC,
} GPUUsageType;
/** Opaque type hiding blender::gpu::VertBuf. */
typedef struct GPUVertBuf GPUVertBuf;
GPUVertBuf *GPU_vertbuf_calloc(void);

View File

@ -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<DrawList *>(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<DrawList *>(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<DrawList *>(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<DrawList *>(list);
DrawList *list_ptr = unwrap(list);
list_ptr->submit();
}

View File

@ -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<GPUDrawList *>(vert);
}
static inline DrawList *unwrap(GPUDrawList *vert)
{
return reinterpret_cast<DrawList *>(vert);
}
static inline const DrawList *unwrap(const GPUDrawList *vert)
{
return reinterpret_cast<const DrawList *>(vert);
}
} // namespace gpu
} // namespace blender

View File

@ -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<FrameBuffer *>(gpu_fb);
delete unwrap(gpu_fb);
}
/* ---------- Binding ----------- */
void GPU_framebuffer_bind(GPUFrameBuffer *gpu_fb)
{
FrameBuffer *fb = reinterpret_cast<FrameBuffer *>(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<FrameBuffer *>(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<GPUFrameBuffer *>(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<GPUFrameBuffer *>(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<FrameBuffer *>(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<Texture *>(attachment.tex);
GPUAttachmentType type = tex->attachment_type(slot);
reinterpret_cast<FrameBuffer *>(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<FrameBuffer *>(gpu_fb);
reinterpret_cast<Texture *>(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<FrameBuffer *>(gpu_fb);
FrameBuffer *fb = unwrap(gpu_fb);
const GPUAttachment &depth_attachment = config[0];
Span<GPUAttachment> 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<FrameBuffer *>(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<FrameBuffer *>(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<FrameBuffer *>(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<FrameBuffer *>(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<FrameBuffer *>(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<FrameBuffer *>(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<FrameBuffer *>(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<FrameBuffer *>(gpufb_read);
FrameBuffer *fb_write = reinterpret_cast<FrameBuffer *>(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<FrameBuffer *>(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<GPUFrameBuffer *>(fb));
gpuPushFrameBuffer(fb);
}
reinterpret_cast<FrameBuffer *>(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<FrameBuffer *>(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);
}

View File

@ -209,6 +209,20 @@ class FrameBuffer {
};
};
/* Syntacting suggar. */
static inline GPUFrameBuffer *wrap(FrameBuffer *vert)
{
return reinterpret_cast<GPUFrameBuffer *>(vert);
}
static inline FrameBuffer *unwrap(GPUFrameBuffer *vert)
{
return reinterpret_cast<FrameBuffer *>(vert);
}
static inline const FrameBuffer *unwrap(const GPUFrameBuffer *vert)
{
return reinterpret_cast<const FrameBuffer *>(vert);
}
#undef DEBUG_NAME_LEN
} // namespace gpu

View File

@ -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<GPUShader *>(shader);
return wrap(shader);
}
void GPU_shader_free(GPUShader *shader)
{
delete reinterpret_cast<Shader *>(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<Shader *>(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<Shader *>(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 *>(shader)->transform_feedback_enable(vertbuf);
return unwrap(shader)->transform_feedback_enable(vertbuf);
}
void GPU_shader_transform_feedback_disable(GPUShader *shader)
{
reinterpret_cast<Shader *>(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 *>(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 *>(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 *>(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 *>(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 *>(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 *>(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 *>(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 *>(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 *>(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)

View File

@ -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<const char *> sources, char *log, const char *stage);
};
/* Syntacting suggar. */
static inline GPUShader *wrap(Shader *vert)
{
return reinterpret_cast<GPUShader *>(vert);
}
static inline Shader *unwrap(GPUShader *vert)
{
return reinterpret_cast<Shader *>(vert);
}
static inline const Shader *unwrap(const GPUShader *vert)
{
return reinterpret_cast<const Shader *>(vert);
}
} // namespace gpu
} // namespace blender

View File

@ -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. */

View File

@ -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<GPUUniformBuf *>(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<GPUUniformBuf *>(ubo);
return wrap(ubo);
}
void GPU_uniformbuf_free(GPUUniformBuf *ubo)
{
delete reinterpret_cast<UniformBuf *>(ubo);
delete unwrap(ubo);
}
void GPU_uniformbuf_update(GPUUniformBuf *ubo, const void *data)
{
reinterpret_cast<UniformBuf *>(ubo)->update(data);
unwrap(ubo)->update(data);
}
void GPU_uniformbuf_bind(GPUUniformBuf *ubo, int slot)
{
reinterpret_cast<UniformBuf *>(ubo)->bind(slot);
unwrap(ubo)->bind(slot);
}
void GPU_uniformbuf_unbind(GPUUniformBuf *ubo)
{
reinterpret_cast<UniformBuf *>(ubo)->unbind();
unwrap(ubo)->unbind();
}
void GPU_uniformbuf_unbind_all(void)

View File

@ -63,6 +63,20 @@ class UniformBuf {
}
};
/* Syntacting suggar. */
static inline GPUUniformBuf *wrap(UniformBuf *vert)
{
return reinterpret_cast<GPUUniformBuf *>(vert);
}
static inline UniformBuf *unwrap(GPUUniformBuf *vert)
{
return reinterpret_cast<UniformBuf *>(vert);
}
static inline const UniformBuf *unwrap(const GPUUniformBuf *vert)
{
return reinterpret_cast<const UniformBuf *>(vert);
}
#undef DEBUG_NAME_LEN
} // namespace gpu

View File

@ -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;