Curves: Add sculpt selection overlay

This commit adds visualization to the selection in curves sculpt mode.
Previously it was only possible to see the selection when it was
connected to a material.

In order to obstruct the users vision as little as possible, the
selected areas of the curve are left as is, but a dark overlay
is drawn over unselected areas.

To make it work, the overlay requests the selection attribute and then
ensures that the evaluation is complete for curves. Then it retrieves
the evaluated selection GPU texture and passes that to the shader.
This reuses the existing generic attribute extraction system because
there currently wouldn't be any benefits to dealing with selection
separately, and because it avoids duplication of the logic that
extracts attributes from curves and evaluates them if necessary.

Differential Revision: https://developer.blender.org/D15219
This commit is contained in:
Hans Goudey 2022-07-07 08:06:30 -05:00
parent 5d6e7df4a8
commit e3ef56ef91
Notes: blender-bot 2023-02-14 07:40:56 +01:00
Referenced by issue #96849, Visualize curves mask in curves sculpt mode
14 changed files with 299 additions and 44 deletions

View File

@ -6669,6 +6669,7 @@ class VIEW3D_PT_overlay_sculpt_curves(Panel):
overlay = view.overlay
row = layout.row(align=True)
row.active = overlay.show_overlays
row.prop(overlay, "sculpt_mode_mask_opacity", text="Selection Opacity")

View File

@ -194,6 +194,7 @@ set(SRC
engines/overlay/overlay_paint.c
engines/overlay/overlay_particle.c
engines/overlay/overlay_sculpt.c
engines/overlay/overlay_sculpt_curves.cc
engines/overlay/overlay_shader.c
engines/overlay/overlay_volume.c
engines/overlay/overlay_wireframe.c
@ -556,6 +557,8 @@ set(GLSL_SRC
engines/overlay/shaders/overlay_particle_vert.glsl
engines/overlay/shaders/overlay_point_varying_color_frag.glsl
engines/overlay/shaders/overlay_point_varying_color_varying_outline_aa_frag.glsl
engines/overlay/shaders/overlay_sculpt_curves_selection_frag.glsl
engines/overlay/shaders/overlay_sculpt_curves_selection_vert.glsl
engines/overlay/shaders/overlay_sculpt_mask_frag.glsl
engines/overlay/shaders/overlay_sculpt_mask_vert.glsl
engines/overlay/shaders/overlay_uniform_color_frag.glsl

View File

@ -192,6 +192,8 @@ static void OVERLAY_cache_init(void *vedata)
OVERLAY_edit_curves_cache_init(vedata);
break;
case CTX_MODE_SCULPT_CURVES:
OVERLAY_sculpt_curves_cache_init(vedata);
break;
case CTX_MODE_OBJECT:
break;
default:
@ -310,6 +312,8 @@ static void OVERLAY_cache_populate(void *vedata, Object *ob)
(draw_ctx->object_mode & OB_MODE_ALL_PAINT);
const bool in_sculpt_mode = (ob == draw_ctx->obact) && (ob->sculpt != NULL) &&
(ob->sculpt->mode_type == OB_MODE_SCULPT);
const bool in_curves_sculpt_mode = (ob == draw_ctx->obact) &&
(ob->mode == OB_MODE_SCULPT_CURVES);
const bool has_surface = ELEM(ob->type,
OB_MESH,
OB_CURVES_LEGACY,
@ -428,6 +432,9 @@ static void OVERLAY_cache_populate(void *vedata, Object *ob)
if (in_sculpt_mode) {
OVERLAY_sculpt_cache_populate(vedata, ob);
}
else if (in_curves_sculpt_mode) {
OVERLAY_sculpt_curves_cache_populate(vedata, ob);
}
if (draw_motion_paths) {
OVERLAY_motion_path_cache_populate(vedata, ob);
@ -591,6 +598,9 @@ static void OVERLAY_draw_scene(void *vedata)
case CTX_MODE_SCULPT:
OVERLAY_sculpt_draw(vedata);
break;
case CTX_MODE_SCULPT_CURVES:
OVERLAY_sculpt_curves_draw(vedata);
break;
case CTX_MODE_EDIT_MESH:
case CTX_MODE_POSE:
case CTX_MODE_PAINT_WEIGHT:

View File

@ -116,6 +116,7 @@ typedef struct OVERLAY_PassList {
DRWPass *particle_ps;
DRWPass *pointcloud_ps;
DRWPass *sculpt_mask_ps;
DRWPass *sculpt_curves_selection_ps;
DRWPass *volume_ps;
DRWPass *wireframe_ps;
DRWPass *wireframe_xray_ps;
@ -279,6 +280,7 @@ typedef struct OVERLAY_PrivateData {
DRWShadingGroup *particle_shapes_grp;
DRWShadingGroup *pointcloud_dots_grp;
DRWShadingGroup *sculpt_mask_grp;
DRWShadingGroup *sculpt_curves_selection_grp;
DRWShadingGroup *volume_selection_surface_grp;
DRWShadingGroup *wires_grp[2][2]; /* With and without coloring. */
DRWShadingGroup *wires_all_grp[2][2]; /* With and without coloring. */
@ -669,6 +671,10 @@ void OVERLAY_sculpt_cache_init(OVERLAY_Data *vedata);
void OVERLAY_sculpt_cache_populate(OVERLAY_Data *vedata, Object *ob);
void OVERLAY_sculpt_draw(OVERLAY_Data *vedata);
void OVERLAY_sculpt_curves_cache_init(OVERLAY_Data *vedata);
void OVERLAY_sculpt_curves_cache_populate(OVERLAY_Data *vedata, Object *ob);
void OVERLAY_sculpt_curves_draw(OVERLAY_Data *vedata);
void OVERLAY_wireframe_init(OVERLAY_Data *vedata);
void OVERLAY_wireframe_cache_init(OVERLAY_Data *vedata);
void OVERLAY_wireframe_cache_populate(OVERLAY_Data *vedata,
@ -750,6 +756,7 @@ GPUShader *OVERLAY_shader_paint_wire(void);
GPUShader *OVERLAY_shader_particle_dot(void);
GPUShader *OVERLAY_shader_particle_shape(void);
GPUShader *OVERLAY_shader_sculpt_mask(void);
GPUShader *OVERLAY_shader_sculpt_curves_selection(void);
GPUShader *OVERLAY_shader_volume_velocity(bool use_needle, bool use_mac);
GPUShader *OVERLAY_shader_volume_gridlines(bool color_with_flags, bool color_range);
GPUShader *OVERLAY_shader_wireframe(bool custom_bias);

View File

@ -0,0 +1,96 @@
/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2022 Blender Foundation. */
/** \file
* \ingroup draw_engine
*/
#include "DRW_render.h"
#include "draw_cache_impl.h"
#include "overlay_private.h"
#include "BKE_curves.hh"
void OVERLAY_sculpt_curves_cache_init(OVERLAY_Data *vedata)
{
OVERLAY_PassList *psl = vedata->psl;
OVERLAY_PrivateData *pd = vedata->stl->pd;
const DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND_ALPHA;
DRW_PASS_CREATE(psl->sculpt_curves_selection_ps, state | pd->clipping_state);
GPUShader *sh = OVERLAY_shader_sculpt_curves_selection();
pd->sculpt_curves_selection_grp = DRW_shgroup_create(sh, psl->sculpt_curves_selection_ps);
DRWShadingGroup *grp = pd->sculpt_curves_selection_grp;
/* Reuse the same mask opacity from sculpt mode, since it wasn't worth it to add a different
* property yet. */
DRW_shgroup_uniform_float_copy(grp, "selection_opacity", pd->overlay.sculpt_mode_mask_opacity);
}
static bool everything_selected(const Curves &curves_id)
{
if (!(curves_id.flag & CV_SCULPT_SELECTION_ENABLED)) {
/* When the selection is disabled, conceptually everything is selected. */
return true;
}
const blender::bke::CurvesGeometry &curves = blender::bke::CurvesGeometry::wrap(
curves_id.geometry);
blender::VArray<float> selection;
switch (curves_id.selection_domain) {
case ATTR_DOMAIN_POINT:
selection = curves.selection_point_float();
break;
case ATTR_DOMAIN_CURVE:
selection = curves.selection_curve_float();
break;
}
return selection.is_single() && selection.get_internal_single() == 1.0f;
}
void OVERLAY_sculpt_curves_cache_populate(OVERLAY_Data *vedata, Object *object)
{
OVERLAY_PrivateData *pd = vedata->stl->pd;
Curves *curves = static_cast<Curves *>(object->data);
/* As an optimization, return early if everything is selected. */
if (everything_selected(*curves)) {
return;
}
/* Retrieve the location of the texture. */
const char *name = curves->selection_domain == ATTR_DOMAIN_POINT ? ".selection_point_float" :
".selection_curve_float";
bool is_point_domain;
GPUTexture **texture = DRW_curves_texture_for_evaluated_attribute(
curves, name, &is_point_domain);
if (texture == nullptr) {
return;
}
/* Evaluate curves and their attributes if necessary. */
DRWShadingGroup *grp = DRW_shgroup_curves_create_sub(
object, pd->sculpt_curves_selection_grp, nullptr);
if (*texture == nullptr) {
return;
}
DRW_shgroup_uniform_bool_copy(grp, "is_point_domain", is_point_domain);
DRW_shgroup_uniform_texture(grp, "selection_tx", *texture);
}
void OVERLAY_sculpt_curves_draw(OVERLAY_Data *vedata)
{
OVERLAY_PassList *psl = vedata->psl;
OVERLAY_PrivateData *pd = vedata->stl->pd;
OVERLAY_FramebufferList *fbl = vedata->fbl;
if (DRW_state_is_fbo()) {
GPU_framebuffer_bind(pd->painting.in_front ? fbl->overlay_in_front_fb :
fbl->overlay_default_fb);
}
DRW_draw_pass(psl->sculpt_curves_selection_ps);
}

View File

@ -90,6 +90,7 @@ typedef struct OVERLAY_Shaders {
GPUShader *particle_shape;
GPUShader *pointcloud_dot;
GPUShader *sculpt_mask;
GPUShader *sculpt_curves_selection;
GPUShader *uniform_color;
GPUShader *volume_velocity_needle_sh;
GPUShader *volume_velocity_mac_sh;
@ -792,6 +793,18 @@ GPUShader *OVERLAY_shader_sculpt_mask(void)
return sh_data->sculpt_mask;
}
GPUShader *OVERLAY_shader_sculpt_curves_selection(void)
{
const DRWContextState *draw_ctx = DRW_context_state_get();
OVERLAY_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
if (!sh_data->sculpt_curves_selection) {
sh_data->sculpt_curves_selection = GPU_shader_create_from_info_name(
draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED ? "overlay_sculpt_curves_selection_clipped" :
"overlay_sculpt_curves_selection");
}
return sh_data->sculpt_curves_selection;
}
struct GPUShader *OVERLAY_shader_uniform_color(void)
{
const DRWContextState *draw_ctx = DRW_context_state_get();

View File

@ -0,0 +1,21 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#include "gpu_shader_create_info.hh"
GPU_SHADER_INTERFACE_INFO(overlay_sculpt_curves_selection_iface, "")
.smooth(Type::FLOAT, "mask_weight");
GPU_SHADER_CREATE_INFO(overlay_sculpt_curves_selection)
.do_static_compilation(true)
.push_constant(Type::BOOL, "is_point_domain")
.push_constant(Type::FLOAT, "selection_opacity")
.sampler(0, ImageType::FLOAT_BUFFER, "selection_tx")
.vertex_out(overlay_sculpt_curves_selection_iface)
.vertex_source("overlay_sculpt_curves_selection_vert.glsl")
.fragment_source("overlay_sculpt_curves_selection_frag.glsl")
.fragment_out(0, Type::VEC4, "out_color")
.additional_info("draw_hair", "draw_globals");
GPU_SHADER_CREATE_INFO(overlay_sculpt_curves_selection_clipped)
.do_static_compilation(true)
.additional_info("overlay_sculpt_curves_selection", "drw_clipped");

View File

@ -0,0 +1,5 @@
void main()
{
out_color = vec4(vec3(0.0), 1.0 - mask_weight);
}

View File

@ -0,0 +1,35 @@
#pragma BLENDER_REQUIRE(common_hair_lib.glsl)
#pragma BLENDER_REQUIRE(common_view_clipping_lib.glsl)
#pragma BLENDER_REQUIRE(common_view_lib.glsl)
float retrieve_selection()
{
if (is_point_domain) {
return texelFetch(selection_tx, hair_get_base_id()).r;
}
return texelFetch(selection_tx, hair_get_strand_id()).r;
}
void main()
{
bool is_persp = (ProjectionMatrix[3][3] == 0.0);
float time, thick_time, thickness;
vec3 world_pos, tan, binor;
hair_get_pos_tan_binor_time(is_persp,
ModelMatrixInverse,
ViewMatrixInverse[3].xyz,
ViewMatrixInverse[2].xyz,
world_pos,
tan,
binor,
time,
thickness,
thick_time);
gl_Position = point_world_to_ndc(world_pos);
mask_weight = 1.0 - (selection_opacity - retrieve_selection() * selection_opacity);
view_clipping_distances(world_pos);
}

View File

@ -161,6 +161,16 @@ struct GPUBatch *DRW_lattice_batch_cache_get_edit_verts(struct Lattice *lt);
int DRW_curves_material_count_get(struct Curves *curves);
/**
* Provide GPU access to a specific evaluated attribute on curves.
*
* \return A pointer to location where the texture will be
* stored, which will be filled by #DRW_shgroup_curves_create_sub.
*/
struct GPUTexture **DRW_curves_texture_for_evaluated_attribute(struct Curves *curves,
const char *name,
bool *r_is_point_domain);
struct GPUBatch *DRW_curves_batch_cache_get_edit_points(struct Curves *curves);
void DRW_curves_batch_cache_create_requested(struct Object *ob);

View File

@ -512,40 +512,41 @@ static bool curves_ensure_attributes(const Curves &curves,
ThreadMutex *render_mutex = &cache.render_mutex;
const CustomData *cd_curve = &curves.geometry.curve_data;
const CustomData *cd_point = &curves.geometry.point_data;
DRW_Attributes attrs_needed;
drw_attributes_clear(&attrs_needed);
ListBase gpu_attrs = GPU_material_attributes(gpu_material);
LISTBASE_FOREACH (GPUMaterialAttribute *, gpu_attr, &gpu_attrs) {
const char *name = gpu_attr->name;
int layer_index;
eCustomDataType type;
eAttrDomain domain;
if (drw_custom_data_match_attribute(cd_curve, name, &layer_index, &type)) {
domain = ATTR_DOMAIN_CURVE;
}
else if (drw_custom_data_match_attribute(cd_point, name, &layer_index, &type)) {
domain = ATTR_DOMAIN_POINT;
}
else {
continue;
}
drw_attributes_add_request(&attrs_needed, name, type, layer_index, domain);
}
CurvesEvalFinalCache &final_cache = cache.curves_cache.final[subdiv];
if (!drw_attributes_overlap(&final_cache.attr_used, &attrs_needed)) {
/* Some new attributes have been added, free all and start over. */
for (const int i : IndexRange(GPU_MAX_ATTR)) {
GPU_VERTBUF_DISCARD_SAFE(cache.curves_cache.proc_attributes_buf[i]);
DRW_TEXTURE_FREE_SAFE(cache.curves_cache.proc_attributes_tex[i]);
if (gpu_material) {
DRW_Attributes attrs_needed;
drw_attributes_clear(&attrs_needed);
ListBase gpu_attrs = GPU_material_attributes(gpu_material);
LISTBASE_FOREACH (GPUMaterialAttribute *, gpu_attr, &gpu_attrs) {
const char *name = gpu_attr->name;
int layer_index;
eCustomDataType type;
eAttrDomain domain;
if (drw_custom_data_match_attribute(cd_curve, name, &layer_index, &type)) {
domain = ATTR_DOMAIN_CURVE;
}
else if (drw_custom_data_match_attribute(cd_point, name, &layer_index, &type)) {
domain = ATTR_DOMAIN_POINT;
}
else {
continue;
}
drw_attributes_add_request(&attrs_needed, name, type, layer_index, domain);
}
drw_attributes_merge(&final_cache.attr_used, &attrs_needed, render_mutex);
if (!drw_attributes_overlap(&final_cache.attr_used, &attrs_needed)) {
/* Some new attributes have been added, free all and start over. */
for (const int i : IndexRange(GPU_MAX_ATTR)) {
GPU_VERTBUF_DISCARD_SAFE(cache.curves_cache.proc_attributes_buf[i]);
DRW_TEXTURE_FREE_SAFE(cache.curves_cache.proc_attributes_tex[i]);
}
drw_attributes_merge(&final_cache.attr_used, &attrs_needed, render_mutex);
}
drw_attributes_merge(&final_cache.attr_used_over_time, &attrs_needed, render_mutex);
}
drw_attributes_merge(&final_cache.attr_used_over_time, &attrs_needed, render_mutex);
bool need_tf_update = false;
@ -602,9 +603,7 @@ bool curves_ensure_procedural_data(Curves *curves,
*curves, cache.curves_cache, thickness_res, subdiv);
}
if (gpu_material) {
need_ft_update |= curves_ensure_attributes(*curves, cache, gpu_material, subdiv);
}
need_ft_update |= curves_ensure_attributes(*curves, cache, gpu_material, subdiv);
return need_ft_update;
}
@ -620,6 +619,69 @@ GPUBatch *DRW_curves_batch_cache_get_edit_points(Curves *curves)
return DRW_batch_request(&cache.edit_points);
}
static void request_attribute(Curves &curves, const char *name)
{
CurvesBatchCache &cache = curves_batch_cache_get(curves);
const DRWContextState *draw_ctx = DRW_context_state_get();
const Scene *scene = draw_ctx->scene;
const int subdiv = scene->r.hair_subdiv;
CurvesEvalFinalCache &final_cache = cache.curves_cache.final[subdiv];
DRW_Attributes attributes{};
CurveComponent component;
component.replace(&curves, GeometryOwnershipType::ReadOnly);
std::optional<AttributeMetaData> meta_data = component.attribute_get_meta_data(name);
if (!meta_data) {
return;
}
const eAttrDomain domain = meta_data->domain;
const eCustomDataType type = meta_data->data_type;
const CustomData &custom_data = domain == ATTR_DOMAIN_POINT ? curves.geometry.point_data :
curves.geometry.curve_data;
drw_attributes_add_request(
&attributes, name, type, CustomData_get_named_layer(&custom_data, type, name), domain);
drw_attributes_merge(&final_cache.attr_used, &attributes, &cache.render_mutex);
}
GPUTexture **DRW_curves_texture_for_evaluated_attribute(Curves *curves,
const char *name,
bool *r_is_point_domain)
{
CurvesBatchCache &cache = curves_batch_cache_get(*curves);
const DRWContextState *draw_ctx = DRW_context_state_get();
const Scene *scene = draw_ctx->scene;
const int subdiv = scene->r.hair_subdiv;
CurvesEvalFinalCache &final_cache = cache.curves_cache.final[subdiv];
request_attribute(*curves, name);
int request_i = -1;
for (const int i : IndexRange(final_cache.attr_used.num_requests)) {
if (STREQ(final_cache.attr_used.requests[i].attribute_name, name)) {
request_i = i;
break;
}
}
if (request_i == -1) {
*r_is_point_domain = false;
return nullptr;
}
switch (final_cache.attr_used.requests[request_i].domain) {
case ATTR_DOMAIN_POINT:
*r_is_point_domain = true;
return &final_cache.attributes_tex[request_i];
case ATTR_DOMAIN_CURVE:
*r_is_point_domain = false;
return &cache.curves_cache.proc_attributes_tex[request_i];
default:
BLI_assert_unreachable();
return nullptr;
}
}
void DRW_curves_batch_cache_create_requested(Object *ob)
{
Curves *curves = static_cast<Curves *>(ob->data);

View File

@ -270,6 +270,7 @@ static void test_overlay_glsl_shaders()
EXPECT_NE(OVERLAY_shader_particle_dot(), nullptr);
EXPECT_NE(OVERLAY_shader_particle_shape(), nullptr);
EXPECT_NE(OVERLAY_shader_sculpt_mask(), nullptr);
EXPECT_NE(OVERLAY_shader_sculpt_curves_selection(), nullptr);
EXPECT_NE(OVERLAY_shader_volume_velocity(false, false), nullptr);
EXPECT_NE(OVERLAY_shader_volume_velocity(false, true), nullptr);
EXPECT_NE(OVERLAY_shader_volume_velocity(true, false), nullptr);

View File

@ -900,21 +900,11 @@ static int select_all_exec(bContext *C, wmOperator *op)
for (Curves *curves_id : unique_curves) {
if (action == SEL_SELECT) {
/* The optimization to avoid storing the selection when everything is selected causes too
* many problems at the moment, since there is no proper visualization yet. Keep the code but
* disable it for now. */
#if 0
/* As an optimization, just remove the selection attributes when everything is selected. */
CurveComponent component;
component.replace(curves_id, GeometryOwnershipType::Editable);
component.attribute_try_delete(".selection_point_float");
component.attribute_try_delete(".selection_curve_float");
#else
CurvesGeometry &curves = CurvesGeometry::wrap(curves_id->geometry);
MutableSpan<float> selection = curves_id->selection_domain == ATTR_DOMAIN_POINT ?
curves.selection_point_float_for_write() :
curves.selection_curve_float_for_write();
selection.fill(1.0f);
#endif
}
else {
CurvesGeometry &curves = CurvesGeometry::wrap(curves_id->geometry);

View File

@ -465,6 +465,7 @@ set(SRC_SHADER_CREATE_INFOS
../draw/engines/overlay/shaders/infos/overlay_outline_info.hh
../draw/engines/overlay/shaders/infos/overlay_paint_info.hh
../draw/engines/overlay/shaders/infos/overlay_sculpt_info.hh
../draw/engines/overlay/shaders/infos/overlay_sculpt_curves_info.hh
../draw/engines/overlay/shaders/infos/overlay_volume_info.hh
../draw/engines/overlay/shaders/infos/overlay_wireframe_info.hh
../draw/engines/select/shaders/infos/select_id_info.hh