DRW: Move clipping planes to their own UBO

This is part of the effor to simplify the View struct in order to implement
multiview rendering.
This commit is contained in:
Clément Foucault
2022-10-07 00:06:27 +02:00
parent 223f970407
commit a945cf4d0f
17 changed files with 41 additions and 37 deletions

View File

@@ -26,7 +26,7 @@ GPU_SHADER_CREATE_INFO(gpencil_geometry)
.sampler(1, ImageType::FLOAT_2D, "gpStrokeTexture")
.sampler(2, ImageType::DEPTH_2D, "gpSceneDepthTexture")
.sampler(3, ImageType::FLOAT_2D, "gpMaskTexture")
.uniform_buf(2, "gpMaterial", "materials[GPENCIL_MATERIAL_BUFFER_LEN]", Frequency::BATCH)
.uniform_buf(4, "gpMaterial", "materials[GPENCIL_MATERIAL_BUFFER_LEN]", Frequency::BATCH)
.uniform_buf(3, "gpLight", "lights[GPENCIL_LIGHT_BUFFER_LEN]", Frequency::BATCH)
/* Per Object */
.push_constant(Type::VEC3, "gpNormal")

View File

@@ -110,12 +110,12 @@ GPU_SHADER_CREATE_INFO(overlay_extra_wire)
GPU_SHADER_CREATE_INFO(overlay_extra_wire_select)
.do_static_compilation(true)
.define("SELECT_EDGES")
.additional_info("overlay_extra_wire", "drw_clipped");
.additional_info("overlay_extra_wire");
GPU_SHADER_CREATE_INFO(overlay_extra_wire_object)
.do_static_compilation(true)
.define("OBJECT_WIRE")
.additional_info("overlay_extra_wire", "drw_clipped");
.additional_info("overlay_extra_wire");
GPU_SHADER_CREATE_INFO(overlay_extra_wire_select_clipped)
.do_static_compilation(true)

View File

@@ -132,6 +132,7 @@ struct DRW_Global {
struct GPUTexture *weight_ramp;
struct GPUUniformBuf *view_ubo;
struct GPUUniformBuf *clipping_ubo;
};
extern struct DRW_Global G_draw;

View File

@@ -13,6 +13,7 @@
#define DRW_VIEW_UBO_SLOT 0
#define DRW_VIEW_CULLING_UBO_SLOT 1
#define DRW_CLIPPING_UBO_SLOT 2
#define DRW_RESOURCE_ID_SLOT 11
#define DRW_OBJ_MAT_SLOT 10

View File

@@ -612,6 +612,11 @@ static void drw_manager_init(DRWManager *dst, GPUViewport *viewport, const int s
G_draw.view_ubo = GPU_uniformbuf_create_ex(sizeof(ViewInfos), NULL, "G_draw.view_ubo");
}
if (G_draw.clipping_ubo == NULL) {
G_draw.clipping_ubo = GPU_uniformbuf_create_ex(
sizeof(float4) * 6, NULL, "G_draw.clipping_ubo");
}
if (dst->draw_list == NULL) {
dst->draw_list = GPU_draw_list_create(DRW_DRAWLIST_LEN);
}
@@ -3079,6 +3084,7 @@ void DRW_engines_free(void)
DRW_UBO_FREE_SAFE(G_draw.block_ubo);
DRW_UBO_FREE_SAFE(G_draw.view_ubo);
DRW_UBO_FREE_SAFE(G_draw.clipping_ubo);
DRW_TEXTURE_FREE_SAFE(G_draw.ramp);
DRW_TEXTURE_FREE_SAFE(G_draw.weight_ramp);

View File

@@ -443,6 +443,8 @@ struct DRWView {
ViewInfos storage;
float4 clip_planes[6];
float4x4 persmat;
float4x4 persinv;
/** Number of active clip planes. */

View File

@@ -1629,6 +1629,7 @@ static void drw_shgroup_init(DRWShadingGroup *shgroup, GPUShader *shader)
shgroup->uniforms = nullptr;
shgroup->uniform_attrs = nullptr;
int clipping_ubo_location = GPU_shader_get_builtin_block(shader, GPU_UNIFORM_BLOCK_DRW_CLIPPING);
int view_ubo_location = GPU_shader_get_builtin_block(shader, GPU_UNIFORM_BLOCK_VIEW);
int model_ubo_location = GPU_shader_get_builtin_block(shader, GPU_UNIFORM_BLOCK_MODEL);
int info_ubo_location = GPU_shader_get_builtin_block(shader, GPU_UNIFORM_BLOCK_INFO);
@@ -1702,6 +1703,16 @@ static void drw_shgroup_init(DRWShadingGroup *shgroup, GPUShader *shader)
shgroup, view_ubo_location, DRW_UNIFORM_BLOCK, G_draw.view_ubo, GPU_SAMPLER_DEFAULT, 0, 1);
}
if (clipping_ubo_location) {
drw_shgroup_uniform_create_ex(shgroup,
clipping_ubo_location,
DRW_UNIFORM_BLOCK,
G_draw.clipping_ubo,
GPU_SAMPLER_DEFAULT,
0,
1);
}
#ifdef DEBUG
int debug_print_location = GPU_shader_get_builtin_ssbo(shader, GPU_STORAGE_BUFFER_DEBUG_PRINT);
if (debug_print_location != -1) {
@@ -2144,8 +2155,6 @@ static void draw_view_matrix_state_update(DRWView *view,
mul_m4_m4m4(view->persmat.values, winmat, viewmat);
invert_m4_m4(view->persinv.values, view->persmat.values);
const bool is_persp = (winmat[3][3] == 0.0f);
}
DRWView *DRW_view_create(const float viewmat[4][4],
@@ -2305,7 +2314,7 @@ void DRW_view_clip_planes_set(DRWView *view, float (*planes)[4], int plane_len)
BLI_assert(plane_len <= MAX_CLIP_PLANES);
view->clip_planes_len = plane_len;
if (plane_len > 0) {
memcpy(view->storage.clip_planes, planes, sizeof(float[4]) * plane_len);
memcpy(view->clip_planes, planes, sizeof(float[4]) * plane_len);
}
}

View File

@@ -1155,6 +1155,7 @@ static void drw_update_view(const float viewport_size[2])
/* TODO(fclem): update a big UBO and only bind ranges here. */
GPU_uniformbuf_update(G_draw.view_ubo, &DST.view_active->storage);
GPU_uniformbuf_update(G_draw.clipping_ubo, &DST.view_active->clip_planes);
/* TODO: get rid of this. */
DST.view_storage_cpy = DST.view_active->storage;

View File

@@ -67,8 +67,6 @@ struct ViewInfos {
float4x4 winmat;
float4x4 wininv;
float4 clip_planes[6];
float2 viewport_size;
float2 viewport_size_inverse;
@@ -89,7 +87,6 @@ BLI_STATIC_ASSERT_ALIGN(ViewInfos, 16)
# define ViewMatrixInverse drw_view.viewinv
# define ProjectionMatrix drw_view.winmat
# define ProjectionMatrixInverse drw_view.wininv
# define clipPlanes drw_view.clip_planes
#endif
/** \} */

View File

@@ -203,15 +203,6 @@ void View::frustum_culling_sphere_calc(const BoundBox &bbox, BoundSphere &bspher
}
}
void View::set_clip_planes(Span<float4> planes)
{
BLI_assert(planes.size() <= ARRAY_SIZE(data_.clip_planes));
int i = 0;
for (const auto &plane : planes) {
data_.clip_planes[i++] = plane;
}
}
void View::update_viewport_size()
{
float4 viewport;

View File

@@ -50,8 +50,6 @@ class View {
this->sync(view_mat, win_mat);
}
void set_clip_planes(Span<float4> planes);
void sync(const float4x4 &view_mat, const float4x4 &win_mat);
bool is_persp() const

View File

@@ -7,12 +7,12 @@ void view_clipping_distances(vec3 wpos)
{
# ifdef USE_WORLD_CLIP_PLANES
vec4 pos_4d = vec4(wpos, 1.0);
gl_ClipDistance[0] = dot(drw_view.clip_planes[0], pos_4d);
gl_ClipDistance[1] = dot(drw_view.clip_planes[1], pos_4d);
gl_ClipDistance[2] = dot(drw_view.clip_planes[2], pos_4d);
gl_ClipDistance[3] = dot(drw_view.clip_planes[3], pos_4d);
gl_ClipDistance[4] = dot(drw_view.clip_planes[4], pos_4d);
gl_ClipDistance[5] = dot(drw_view.clip_planes[5], pos_4d);
gl_ClipDistance[0] = dot(drw_clipping[0], pos_4d);
gl_ClipDistance[1] = dot(drw_clipping[1], pos_4d);
gl_ClipDistance[2] = dot(drw_clipping[2], pos_4d);
gl_ClipDistance[3] = dot(drw_clipping[3], pos_4d);
gl_ClipDistance[4] = dot(drw_clipping[4], pos_4d);
gl_ClipDistance[5] = dot(drw_clipping[5], pos_4d);
# endif
}

View File

@@ -15,8 +15,6 @@ layout(std140) uniform viewBlock
mat4 ViewMatrixInverse;
mat4 ProjectionMatrix;
mat4 ProjectionMatrixInverse;
vec4 clipPlanes[6];
};
#endif /* USE_GPU_SHADER_CREATE_INFO */
@@ -38,12 +36,6 @@ vec3 cameraVec(vec3 P)
}
#define viewCameraVec(vP) ((ProjectionMatrix[3][3] == 0.0) ? normalize(-vP) : vec3(0.0, 0.0, 1.0))
#ifdef world_clip_planes_calc_clip_distance
# undef world_clip_planes_calc_clip_distance
# define world_clip_planes_calc_clip_distance(p) \
_world_clip_planes_calc_clip_distance(p, clipPlanes)
#endif
#ifdef COMMON_GLOBALS_LIB
/* TODO move to overlay engine. */
float mul_project_m4_v3_zfac(in vec3 co)

View File

@@ -13,11 +13,11 @@ GPU_SHADER_CREATE_INFO(draw_object_infos)
GPU_SHADER_CREATE_INFO(draw_volume_infos)
.typedef_source("draw_shader_shared.h")
.uniform_buf(2, "VolumeInfos", "drw_volume", Frequency::BATCH);
.uniform_buf(3, "VolumeInfos", "drw_volume", Frequency::BATCH);
GPU_SHADER_CREATE_INFO(draw_curves_infos)
.typedef_source("draw_shader_shared.h")
.uniform_buf(2, "CurvesInfos", "drw_curves", Frequency::BATCH);
.uniform_buf(3, "CurvesInfos", "drw_curves", Frequency::BATCH);
GPU_SHADER_CREATE_INFO(draw_object_infos_new)
.typedef_source("draw_shader_shared.h")

View File

@@ -75,7 +75,10 @@ GPU_SHADER_CREATE_INFO(draw_modelmat_instanced_attr)
/** \name Draw View
* \{ */
GPU_SHADER_CREATE_INFO(drw_clipped).define("USE_WORLD_CLIP_PLANES");
GPU_SHADER_CREATE_INFO(drw_clipped)
/* TODO(fclem): Move to engine side. */
.uniform_buf(DRW_CLIPPING_UBO_SLOT, "vec4", "drw_clipping[6]", Frequency::PASS)
.define("USE_WORLD_CLIP_PLANES");
/** \} */

View File

@@ -145,6 +145,7 @@ typedef enum {
GPU_UNIFORM_BLOCK_DRW_VIEW,
GPU_UNIFORM_BLOCK_DRW_MODEL,
GPU_UNIFORM_BLOCK_DRW_INFOS,
GPU_UNIFORM_BLOCK_DRW_CLIPPING,
GPU_NUM_UNIFORM_BLOCKS, /* Special value, denotes number of builtin uniforms block. */
} GPUUniformBlockBuiltin;

View File

@@ -228,6 +228,8 @@ inline const char *ShaderInterface::builtin_uniform_block_name(GPUUniformBlockBu
return "drw_matrices";
case GPU_UNIFORM_BLOCK_DRW_INFOS:
return "drw_infos";
case GPU_UNIFORM_BLOCK_DRW_CLIPPING:
return "drw_clipping";
default:
return nullptr;
}