Overlay: Move text cache managment to the Overlay::Instance

This avoid legacy code inside the DRWContext.

Note that this change the draw order w.r.t. gizmos. Now the
gizmos will hide the text.

Moreover, streamline the condition for enabling text drawing
in order to fix #78971.
This commit is contained in:
Clément Foucault
2025-06-25 13:02:30 +02:00
parent e2c3d850cb
commit 1a5f29cb07
7 changed files with 30 additions and 45 deletions

View File

@@ -44,7 +44,7 @@ void Instance::init()
state.is_depth_only_drawing = ctx->is_depth();
state.is_material_select = ctx->is_material_select();
state.draw_background = ctx->options.draw_background;
state.show_text = ctx->options.draw_text;
state.show_text = false;
/* Note there might be less than 6 planes, but we always compute the 6 of them for simplicity. */
state.clipping_plane_count = clipping_enabled_ ? 6 : 0;
@@ -86,6 +86,8 @@ void Instance::init()
state.overlay = state.v3d->overlay;
state.v3d_flag = state.v3d->flag;
state.v3d_gridflag = state.v3d->gridflag;
state.show_text = !resources.is_selection() && !state.is_depth_only_drawing &&
(ctx->v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0;
}
else {
memset(&state.overlay, 0, sizeof(state.overlay));
@@ -426,10 +428,12 @@ void Instance::begin_sync()
{
/* TODO(fclem): Against design. Should not sync depending on view. */
View &view = View::default_get();
state.dt = DRW_text_cache_ensure();
state.camera_position = view.viewinv().location();
state.camera_forward = view.viewinv().z_axis();
DRW_text_cache_destroy(state.dt);
state.dt = DRW_text_cache_create();
resources.begin_sync(state.clipping_plane_count);
background.begin_sync(resources, state);
@@ -955,9 +959,22 @@ void Instance::draw_v3d(Manager &manager, View &view)
background.draw_output(resources.overlay_output_color_only_fb, manager, view);
anti_aliasing.draw_output(resources.overlay_output_color_only_fb, manager, view);
cursor.draw_output(resources.overlay_output_color_only_fb, manager, view);
draw_text(resources.overlay_output_color_only_fb);
}
}
void Instance::draw_text(Framebuffer &framebuffer)
{
if (state.show_text == false) {
return;
}
GPU_framebuffer_bind(framebuffer);
GPU_depth_test(GPU_DEPTH_NONE);
DRW_text_cache_draw(state.dt, state.region, state.v3d);
}
bool Instance::object_is_selected(const ObjectRef &ob_ref)
{
return (ob_ref.object->base_flag & BASE_SELECTED);

View File

@@ -114,6 +114,10 @@ class Instance : public DrawEngine {
Instance() : selection_type_(select::SelectionType::DISABLED){};
Instance(const SelectionType selection_type) : selection_type_(selection_type){};
~Instance()
{
DRW_text_cache_destroy(state.dt);
}
blender::StringRefNull name_get() final
{
@@ -151,6 +155,8 @@ class Instance : public DrawEngine {
void draw_v2d(Manager &manager, View &view);
void draw_v3d(Manager &manager, View &view);
void draw_text(Framebuffer &framebuffer);
void ensure_weight_ramp_texture();
};

View File

@@ -70,7 +70,6 @@ struct DrawEngine {
static constexpr int GPU_INFO_SIZE = 512; /* IMA_MAX_RENDER_TEXT_SIZE */
char info[GPU_INFO_SIZE] = {'\0'};
DRWTextStore *text_draw_cache = nullptr;
bool used = false;
@@ -275,7 +274,6 @@ struct DRWContext {
struct {
bool draw_background = false;
bool draw_text = false;
} options;
/** Convenience pointer to text_store owned by the viewport */

View File

@@ -148,12 +148,6 @@ DRWContext::DRWContext(Mode mode_,
this->object_pose = nullptr;
}
/* TODO(fclem): This belongs to the overlay engine. */
if (this->v3d != nullptr && mode == DRWContext::VIEWPORT) {
this->options.draw_text = ((this->v3d->flag2 & V3D_HIDE_OVERLAYS) == 0 &&
(this->v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0);
}
/* View layer can be lazily synced. */
BKE_view_layer_synced_ensure(this->scene, this->view_layer);
@@ -760,18 +754,7 @@ void DRWContext::engines_init_and_sync(iter_callback_t iter_callback)
view_data_active->manager->begin_sync(this->obact);
view_data_active->foreach_enabled_engine([&](DrawEngine &instance) {
/* TODO(fclem): Remove. Only there for overlay engine. */
if (instance.text_draw_cache) {
DRW_text_cache_destroy(instance.text_draw_cache);
instance.text_draw_cache = nullptr;
}
if (text_store_p == nullptr) {
text_store_p = &instance.text_draw_cache;
}
instance.begin_sync();
});
view_data_active->foreach_enabled_engine([&](DrawEngine &instance) { instance.begin_sync(); });
sync(iter_callback);
@@ -800,16 +783,6 @@ void DRWContext::engines_draw_scene()
}
}
static void drw_engines_draw_text()
{
DRWContext &ctx = drw_get();
ctx.view_data_active->foreach_enabled_engine([&](DrawEngine &instance) {
if (instance.text_draw_cache) {
DRW_text_cache_draw(instance.text_draw_cache, ctx.region, ctx.v3d);
}
});
}
void DRW_draw_region_engine_info(int xoffset, int *yoffset, int line_height)
{
DRWContext &ctx = drw_get();
@@ -1029,9 +1002,6 @@ static void drw_callbacks_post_scene(DRWContext &draw_ctx)
DRW_draw_gizmo_3d(draw_ctx.evil_C, region);
}
GPU_depth_test(GPU_DEPTH_NONE);
drw_engines_draw_text();
DRW_draw_region_info(draw_ctx.evil_C, region);
/* Annotations - temporary drawing buffer (screen-space). */
@@ -1149,10 +1119,8 @@ static void drw_callbacks_post_scene_2D(DRWContext &draw_ctx, View2D &v2d)
blender::draw::command::StateSet::set();
GPU_depth_test(GPU_DEPTH_NONE);
drw_engines_draw_text();
if (do_annotations) {
GPU_depth_test(GPU_DEPTH_NONE);
ED_annotation_draw_view2d(draw_ctx.evil_C, false);
}
}

View File

@@ -131,7 +131,7 @@ void DRW_text_cache_add(DRWTextStore *dt,
}
}
static void drw_text_cache_draw_ex(DRWTextStore *dt, ARegion *region)
static void drw_text_cache_draw_ex(const DRWTextStore *dt, const ARegion *region)
{
ViewCachedString *vos;
BLI_memiter_handle it;
@@ -198,7 +198,7 @@ static void drw_text_cache_draw_ex(DRWTextStore *dt, ARegion *region)
GPU_matrix_projection_set(original_proj);
}
void DRW_text_cache_draw(DRWTextStore *dt, ARegion *region, View3D *v3d)
void DRW_text_cache_draw(const DRWTextStore *dt, const ARegion *region, const View3D *v3d)
{
ViewCachedString *vos;
if (v3d) {
@@ -242,7 +242,7 @@ void DRW_text_cache_draw(DRWTextStore *dt, ARegion *region, View3D *v3d)
/* project first */
BLI_memiter_handle it;
BLI_memiter_iter_init(dt->cache_strings, &it);
View2D *v2d = &region->v2d;
const View2D *v2d = &region->v2d;
float viewmat[4][4];
rctf region_space = {0.0f, float(region->winx), 0.0f, float(region->winy)};
BLI_rctf_transform_calc_m4_pivot_min(&v2d->cur, &region_space, viewmat);

View File

@@ -33,7 +33,7 @@ void DRW_text_cache_add(DRWTextStore *dt,
const bool shadow = false,
const bool align_center = false);
void DRW_text_cache_draw(DRWTextStore *dt, ARegion *region, View3D *v3d);
void DRW_text_cache_draw(const DRWTextStore *dt, const ARegion *region, const View3D *v3d);
void DRW_text_edit_mesh_measure_stats(const ARegion *region,
const View3D *v3d,

View File

@@ -94,8 +94,6 @@ void DRWViewData::clear(bool free_instance_data)
if (free_instance_data) {
foreach_engine([&](DrawEngine::Pointer &ptr) {
if (ptr.instance) {
/* TODO Move where it belongs. */
DRW_text_cache_destroy(ptr.instance->text_draw_cache);
ptr.free_instance();
}
});
@@ -126,8 +124,6 @@ void DRW_view_data_free_unused(DRWViewData *view_data)
{
view_data->foreach_engine([&](DrawEngine::Pointer &ptr) {
if (ptr.instance && ptr.instance->used == false) {
/* TODO Move where it belongs. */
DRW_text_cache_destroy(ptr.instance->text_draw_cache);
ptr.free_instance();
}
});