GPUShader: Make GPUShader* an opaque pointer to blender::gpu::Shader

This avoids the misleading inheritance.

Also cleanup by setting the blender::gpu::Shader as active shader to
avoid some casting.
This commit is contained in:
Clément Foucault 2020-08-21 13:48:34 +02:00
parent 1e95a7402c
commit 220fbdf593
7 changed files with 36 additions and 34 deletions

View File

@ -605,11 +605,7 @@ static bool ubo_bindings_validate(DRWShadingGroup *shgroup)
DRWPass *parent_pass = DRW_memblock_elem_from_handle(DST.vmempool->passes,
&shgroup->pass_handle);
printf("Pass : %s, Shader : %s, Block : %s, Binding %d\n",
parent_pass->name,
shgroup->shader->name,
blockname,
binding);
printf("Pass : %s, Block : %s, Binding %d\n", parent_pass->name, blockname, binding);
}
}
# endif

View File

@ -31,12 +31,8 @@ struct GPUTexture;
struct GPUUniformBuf;
struct GPUVertBuf;
/* TODO(fclem) These members should be private and the
* whole struct should just be an opaque pointer. */
typedef struct GPUShader {
/** For debugging purpose. */
char name[64];
} GPUShader;
/** Opaque type hidding blender::gpu::Shader */
typedef struct GPUShader GPUShader;
typedef enum eGPUShaderTFBType {
GPU_SHADER_TFB_NONE = 0, /* Transform feedback unsupported. */

View File

@ -29,6 +29,7 @@
#include "GPU_context.h"
#include "gpu_shader_private.hh"
#include "gpu_state_private.hh"
#include <mutex>
@ -43,7 +44,7 @@ struct GPUMatrixState;
struct GPUContext {
public:
/** State managment */
GPUShader *shader = NULL;
blender::gpu::Shader *shader = NULL;
GPUFrameBuffer *current_fbo = NULL;
GPUMatrixState *matrix_state = NULL;
blender::gpu::GPUStateManager *state_manager = NULL;

View File

@ -52,6 +52,11 @@ 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
* \{ */
@ -302,12 +307,12 @@ GPUShader *GPU_shader_create_ex(const char *vertcode,
return NULL;
};
return static_cast<GPUShader *>(shader);
return reinterpret_cast<GPUShader *>(shader);
}
void GPU_shader_free(GPUShader *shader)
{
delete static_cast<Shader *>(shader);
delete reinterpret_cast<Shader *>(shader);
}
/** \} */
@ -429,19 +434,19 @@ struct GPUShader *GPU_shader_create_from_arrays_impl(
void GPU_shader_bind(GPUShader *gpu_shader)
{
Shader *shader = static_cast<Shader *>(gpu_shader);
Shader *shader = reinterpret_cast<Shader *>(gpu_shader);
GPUContext *ctx = GPU_context_active_get();
if (ctx->shader != shader) {
ctx->shader = shader;
shader->bind();
GPU_matrix_bind(shader);
GPU_shader_set_srgb_uniform(shader);
GPU_matrix_bind(gpu_shader);
GPU_shader_set_srgb_uniform(gpu_shader);
}
if (GPU_matrix_dirty_get()) {
GPU_matrix_bind(shader);
GPU_matrix_bind(gpu_shader);
}
}
@ -450,7 +455,7 @@ void GPU_shader_unbind(void)
#ifndef NDEBUG
GPUContext *ctx = GPU_context_active_get();
if (ctx->shader) {
static_cast<Shader *>(ctx->shader)->unbind();
reinterpret_cast<Shader *>(ctx->shader)->unbind();
}
ctx->shader = NULL;
#endif
@ -466,12 +471,12 @@ void GPU_shader_unbind(void)
bool GPU_shader_transform_feedback_enable(GPUShader *shader, GPUVertBuf *vertbuf)
{
return static_cast<Shader *>(shader)->transform_feedback_enable(vertbuf);
return reinterpret_cast<Shader *>(shader)->transform_feedback_enable(vertbuf);
}
void GPU_shader_transform_feedback_disable(GPUShader *shader)
{
static_cast<Shader *>(shader)->transform_feedback_disable();
reinterpret_cast<Shader *>(shader)->transform_feedback_disable();
}
/** \} */
@ -482,48 +487,48 @@ void GPU_shader_transform_feedback_disable(GPUShader *shader)
int GPU_shader_get_uniform(GPUShader *shader, const char *name)
{
ShaderInterface *interface = static_cast<Shader *>(shader)->interface;
ShaderInterface *interface = reinterpret_cast<Shader *>(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 = static_cast<Shader *>(shader)->interface;
ShaderInterface *interface = reinterpret_cast<Shader *>(shader)->interface;
return interface->uniform_builtin((GPUUniformBuiltin)builtin);
}
int GPU_shader_get_builtin_block(GPUShader *shader, int builtin)
{
ShaderInterface *interface = static_cast<Shader *>(shader)->interface;
ShaderInterface *interface = reinterpret_cast<Shader *>(shader)->interface;
return interface->ubo_builtin((GPUUniformBlockBuiltin)builtin);
}
/* DEPRECATED. */
int GPU_shader_get_uniform_block(GPUShader *shader, const char *name)
{
ShaderInterface *interface = static_cast<Shader *>(shader)->interface;
ShaderInterface *interface = reinterpret_cast<Shader *>(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 = static_cast<Shader *>(shader)->interface;
ShaderInterface *interface = reinterpret_cast<Shader *>(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 = static_cast<Shader *>(shader)->interface;
ShaderInterface *interface = reinterpret_cast<Shader *>(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 = static_cast<Shader *>(shader)->interface;
ShaderInterface *interface = reinterpret_cast<Shader *>(shader)->interface;
const ShaderInput *attr = interface->attr_get(name);
return attr ? attr->location : -1;
}
@ -550,13 +555,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)
{
static_cast<Shader *>(shader)->uniform_float(loc, len, arraysize, value);
reinterpret_cast<Shader *>(shader)->uniform_float(loc, len, arraysize, value);
}
void GPU_shader_uniform_vector_int(
GPUShader *shader, int loc, int len, int arraysize, const int *value)
{
static_cast<Shader *>(shader)->uniform_int(loc, len, arraysize, value);
reinterpret_cast<Shader *>(shader)->uniform_int(loc, len, arraysize, value);
}
void GPU_shader_uniform_int(GPUShader *shader, int location, int value)

View File

@ -29,11 +29,15 @@
namespace blender {
namespace gpu {
class Shader : public GPUShader {
class Shader {
public:
/** Uniform & attribute locations for shader. */
ShaderInterface *interface;
protected:
/** For debugging purpose. */
char name[64];
public:
Shader(const char *name);
virtual ~Shader();

View File

@ -407,6 +407,6 @@ void VertexFormat_pack(GPUVertFormat *format)
void GPU_vertformat_from_shader(GPUVertFormat *format, const struct GPUShader *gpushader)
{
const Shader *shader = static_cast<const Shader *>(gpushader);
const Shader *shader = reinterpret_cast<const Shader *>(gpushader);
shader->vertformat_from_shader(format);
}

View File

@ -225,7 +225,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 = static_cast<Shader *>(GPU_context_active_get()->shader);
Shader *shader = GPU_context_active_get()->shader;
GLShaderInterface *interface = static_cast<GLShaderInterface *>(shader->interface);
if (interface_ != interface) {
vao_get(batch);
@ -256,7 +256,7 @@ GLuint GLVaoCache::vao_get(GPUBatch *batch)
{
this->context_check();
Shader *shader = static_cast<Shader *>(GPU_context_active_get()->shader);
Shader *shader = GPU_context_active_get()->shader;
GLShaderInterface *interface = static_cast<GLShaderInterface *>(shader->interface);
if (interface_ != interface) {
interface_ = interface;