Overlay: Add image_plane_depth_bias shader

Move the depth bias to the shader to avoid creating extra views and
culling passes.
This commit is contained in:
Miguel Pozo
2024-11-18 16:23:30 +01:00
parent c86fd709cf
commit 5586533cc8
5 changed files with 51 additions and 33 deletions

View File

@@ -86,9 +86,6 @@ class Cameras {
/* Same as `foreground_ps_` with "View as Render" checked. */
PassMain foreground_scene_ps_ = {"foreground_scene_ps_"};
View view_reference_images = {"view_reference_images"};
float view_dist = 0.0f;
struct CallBuffers {
const SelectionType selection_type_;
CameraInstanceBuf distances_buf = {selection_type_, "camera_distances_buf"};
@@ -374,8 +371,6 @@ class Cameras {
return;
}
view_dist = state.view_dist_get(view.winmat());
call_buffers_.distances_buf.clear();
call_buffers_.frame_buf.clear();
call_buffers_.tria_buf.clear();
@@ -387,12 +382,16 @@ class Cameras {
call_buffers_.tracking_path.clear();
Empties::begin_sync(call_buffers_.empties);
float4x4 depth_bias_winmat = winmat_polygon_offset(
view.winmat(), state.view_dist_get(view.winmat()), -1.0f);
/* Init image passes. */
auto init_pass = [&](PassMain &pass, DRWState draw_state) {
pass.init();
pass.state_set(draw_state, state.clipping_plane_count);
pass.shader_set(res.shaders.image_plane.get());
pass.shader_set(res.shaders.image_plane_depth_bias.get());
pass.bind_ubo("globalsBlock", &res.globals_buf);
pass.push_constant("depth_bias_winmat", depth_bias_winmat);
res.select_bind(pass);
};
@@ -652,10 +651,8 @@ class Cameras {
return;
}
view_reference_images.sync(view.viewmat(),
winmat_polygon_offset(view.winmat(), view_dist, -1.0f));
GPU_framebuffer_bind(framebuffer);
manager.submit(foreground_ps_, view_reference_images);
manager.submit(foreground_ps_, view);
}
private:

View File

@@ -34,9 +34,6 @@ class Empties {
PassSimple ps_ = {"Empties"};
View view_reference_images = {"view_reference_images"};
float view_dist = 0.0f;
struct CallBuffers {
const SelectionType selection_type_;
EmptyInstanceBuf plain_axes_buf = {selection_type_, "plain_axes_buf"};
@@ -51,10 +48,12 @@ class Empties {
bool enabled_ = false;
float4x4 depth_bias_winmat_;
public:
Empties(const SelectionType selection_type) : call_buffers_{selection_type} {};
void begin_sync(Resources &res, const State &state, const View &view)
void begin_sync(Resources &res, const State &state, View &view)
{
enabled_ = state.space_type == SPACE_VIEW3D;
@@ -62,12 +61,14 @@ class Empties {
return;
}
view_dist = state.view_dist_get(view.winmat());
depth_bias_winmat_ = winmat_polygon_offset(
view.winmat(), state.view_dist_get(view.winmat()), -1.0f);
auto init_pass = [&](PassMain &pass, DRWState draw_state) {
pass.init();
pass.state_set(draw_state, state.clipping_plane_count);
pass.shader_set(res.shaders.image_plane.get());
pass.shader_set(res.shaders.image_plane_depth_bias.get());
pass.push_constant("depth_bias_winmat", depth_bias_winmat_);
pass.bind_ubo("globalsBlock", &res.globals_buf);
res.select_bind(pass);
};
@@ -224,11 +225,8 @@ class Empties {
GPU_framebuffer_bind(framebuffer);
view_reference_images.sync(view.viewmat(),
winmat_polygon_offset(view.winmat(), view_dist, -1.0f));
manager.submit(images_ps_, view_reference_images);
manager.submit(images_blend_ps_, view_reference_images);
manager.submit(images_ps_, view);
manager.submit(images_blend_ps_, view);
}
void draw_in_front_images(Framebuffer &framebuffer, Manager &manager, View &view)
@@ -239,10 +237,7 @@ class Empties {
GPU_framebuffer_bind(framebuffer);
view_reference_images.sync(view.viewmat(),
winmat_polygon_offset(view.winmat(), view_dist, -1.0f));
manager.submit(images_front_ps_, view_reference_images);
manager.submit(images_front_ps_, view);
}
private:
@@ -325,30 +320,38 @@ class Empties {
{
const bool in_front = state.use_in_front && (ob.dtx & OB_DRAW_IN_FRONT);
if (in_front) {
return create_subpass(state, mat, res, images_front_ps_);
return create_subpass(state, mat, res, images_front_ps_, true);
}
const char depth_mode = DRW_state_is_depth() ? char(OB_EMPTY_IMAGE_DEPTH_DEFAULT) :
ob.empty_image_depth;
switch (depth_mode) {
case OB_EMPTY_IMAGE_DEPTH_BACK:
return create_subpass(state, mat, res, images_back_ps_);
return create_subpass(state, mat, res, images_back_ps_, false);
case OB_EMPTY_IMAGE_DEPTH_FRONT:
return create_subpass(state, mat, res, images_front_ps_);
return create_subpass(state, mat, res, images_front_ps_, true);
case OB_EMPTY_IMAGE_DEPTH_DEFAULT:
default:
return use_alpha_blend ? create_subpass(state, mat, res, images_blend_ps_) : images_ps_;
return use_alpha_blend ? create_subpass(state, mat, res, images_blend_ps_, true) :
images_ps_;
}
}
static PassMain::Sub &create_subpass(const State &state,
const float4x4 &mat,
Resources &res,
PassSortable &parent)
PassMain::Sub &create_subpass(const State &state,
const float4x4 &mat,
Resources &res,
PassSortable &parent,
bool depth_bias)
{
const float3 tmp = state.camera_position - mat.location();
const float z = -math::dot(state.camera_forward, tmp);
PassMain::Sub &sub = parent.sub("Sub", z);
sub.shader_set(res.shaders.image_plane.get());
if (depth_bias) {
sub.shader_set(res.shaders.image_plane_depth_bias.get());
sub.push_constant("depth_bias_winmat", depth_bias_winmat_);
}
else {
sub.shader_set(res.shaders.image_plane.get());
}
sub.bind_ubo("globalsBlock", &res.globals_buf);
return sub;
};

View File

@@ -294,6 +294,7 @@ class ShaderModule {
ShaderPtr fluid_velocity_mac;
ShaderPtr fluid_velocity_needle;
ShaderPtr image_plane;
ShaderPtr image_plane_depth_bias;
ShaderPtr lattice_points;
ShaderPtr lattice_wire;
ShaderPtr particle_dot;

View File

@@ -573,6 +573,15 @@ ShaderModule::ShaderModule(const SelectionType selection_type, const bool clippi
"draw_view", "draw_globals", "draw_modelmat_new", "draw_resource_handle_new");
});
image_plane_depth_bias = selectable_shader(
"overlay_image", [](gpu::shader::ShaderCreateInfo &info) {
info.additional_infos_.clear();
info.additional_info(
"draw_view", "draw_globals", "draw_modelmat_new", "draw_resource_handle_new");
info.define("DEPTH_BIAS");
info.push_constant(gpu::shader::Type::MAT4, "depth_bias_winmat");
});
particle_dot = selectable_shader("overlay_particle_dot",
[](gpu::shader::ShaderCreateInfo &info) {
info.additional_infos_.clear();

View File

@@ -12,13 +12,21 @@ void main()
vec3 world_pos = point_object_to_world(pos);
if (isCameraBackground) {
/* Model matrix converts to view position to avoid jittering (see #91398). */
#ifdef DEPTH_BIAS
gl_Position = depth_bias_winmat * vec4(world_pos, 1.0);
#else
gl_Position = point_view_to_ndc(world_pos);
#endif
/* Camera background images are not really part of the 3D space.
* It makes no sense to apply clipping on them. */
view_clipping_distances_bypass();
}
else {
#ifdef DEPTH_BIAS
gl_Position = depth_bias_winmat * (ViewMatrix * vec4(world_pos, 1.0));
#else
gl_Position = point_world_to_ndc(world_pos);
#endif
view_clipping_distances(world_pos);
}