Blender V5.3
overlay_attribute_viewer.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BKE_curves.hh"
12#include "BKE_geometry_set.hh"
13#include "DNA_curve_types.h"
15
16#include "GPU_batch.hh"
17#include "draw_cache.hh"
18#include "draw_cache_impl.hh"
19#include "overlay_base.hh"
20
21namespace blender::draw::overlay {
22
28 private:
29 PassMain ps_ = {"attribute_viewer_ps_"};
30
31 PassMain::Sub *mesh_sub_ = nullptr;
32 PassMain::Sub *pointcloud_sub_ = nullptr;
33 PassMain::Sub *gsplat_sub_ = nullptr;
34 PassMain::Sub *curve_sub_ = nullptr;
35 PassMain::Sub *curves_sub_ = nullptr;
36 PassMain::Sub *instance_sub_ = nullptr;
37
38 public:
39 void begin_sync(Resources &res, const State &state) final
40 {
41 ps_.init();
42 enabled_ = state.is_space_v3d() && !res.is_selection() && state.show_attribute_viewer();
43 if (!enabled_) {
44 return;
45 };
46 ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
47 ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
49 state.clipping_plane_count);
50
51 auto create_sub = [&](const char *name, gpu::Shader *shader) {
52 auto &sub = ps_.sub(name);
53 sub.shader_set(shader);
54 return ⊂
55 };
56
57 mesh_sub_ = create_sub("mesh", res.shaders->attribute_viewer_mesh.get());
58 pointcloud_sub_ = create_sub("pointcloud", res.shaders->attribute_viewer_pointcloud.get());
59 gsplat_sub_ = create_sub("gsplat", res.shaders->attribute_viewer_gsplat.get());
60 curve_sub_ = create_sub("curve", res.shaders->attribute_viewer_curve.get());
61 curves_sub_ = create_sub("curves", res.shaders->attribute_viewer_curves.get());
62 instance_sub_ = create_sub("instance", res.shaders->uniform_color.get());
63 }
64
65 void object_sync(Manager &manager,
66 const ObjectRef &ob_ref,
67 Resources & /*res*/,
68 const State &state) final
69 {
70 const bool is_preview = ob_ref.preview_base_geometry() != nullptr;
71 if (!enabled_ || !is_preview) {
72 return;
73 }
74
75 if (ob_ref.preview_instance_index() >= 0) {
76 const auto &instances =
77 *ob_ref.preview_base_geometry()->get_component<bke::InstancesComponent>();
78 if (const std::optional<bke::AttributeMetaData> meta_data =
79 instances.attributes()->lookup_meta_data(".viewer"))
80 {
81 if (attribute_type_supports_viewer_overlay(meta_data->data_type)) {
82 populate_for_instance(ob_ref, state, manager);
83 return;
84 }
85 }
86 }
87 populate_for_geometry(ob_ref, state, manager);
88 }
89
90 void pre_draw(Manager &manager, View &view) final
91 {
92 if (!enabled_) {
93 return;
94 }
95
96 manager.generate_commands(ps_, view);
97 }
98
99 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
100 {
101 if (!enabled_) {
102 return;
103 }
104
105 GPU_framebuffer_bind(framebuffer);
106 manager.submit_only(ps_, view);
107 }
108
109 private:
110 void populate_for_instance(const ObjectRef &ob_ref, const State &state, Manager &manager)
111 {
112 Object &object = *ob_ref.object;
113 const bke::GeometrySet &base_geometry = *ob_ref.preview_base_geometry();
114 const bke::InstancesComponent &instances =
115 *base_geometry.get_component<bke::InstancesComponent>();
116 const bke::AttributeAccessor instance_attributes = *instances.attributes();
117 const VArray attribute = *instance_attributes.lookup<ColorGeometry4f>(".viewer");
118 if (!attribute) {
119 return;
120 }
122 color.a *= state.overlay.viewer_attribute_opacity;
123 switch (object.type) {
124 case OB_MESH: {
125 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
126 {
127 gpu::Batch *batch = DRW_cache_mesh_surface_get(&object);
128 auto &sub = *instance_sub_;
129 sub.push_constant("ucolor", float4(color));
130 sub.draw(batch, res_handle);
131 }
132 if (gpu::Batch *batch = DRW_cache_mesh_loose_edges_get(&object)) {
133 auto &sub = *instance_sub_;
134 sub.push_constant("ucolor", float4(color));
135 sub.draw(batch, res_handle);
136 }
137 break;
138 }
139 case OB_POINTCLOUD: {
140 auto &sub = *instance_sub_;
141 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
142 gpu::Batch *batch = pointcloud_sub_pass_setup(sub, ob_ref, res_handle, nullptr);
143 sub.push_constant("ucolor", float4(color));
144 sub.draw(batch, res_handle);
145 break;
146 }
147 case OB_CURVES_LEGACY: {
148 gpu::Batch *batch = DRW_cache_curve_edge_wire_get(&object);
149 auto &sub = *instance_sub_;
150 sub.push_constant("ucolor", float4(color));
151 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
152 sub.draw(batch, res_handle);
153 break;
154 }
155 case OB_CURVES: {
156 /* Not supported yet because instances of this type are currently drawn as legacy curves.
157 */
158 break;
159 }
160 default:
161 break;
162 }
163 }
164
165 static bool attribute_type_supports_viewer_overlay(const bke::AttrType data_type)
166 {
168 }
169
170 void populate_for_geometry(const ObjectRef &ob_ref, const State &state, Manager &manager)
171 {
172 const float opacity = state.overlay.viewer_attribute_opacity;
173 Object &object = *ob_ref.object;
174 switch (object.type) {
175 case OB_MESH: {
177 if (const std::optional<bke::AttributeMetaData> meta_data =
178 mesh.attributes().lookup_meta_data(".viewer"))
179 {
180 if (attribute_type_supports_viewer_overlay(meta_data->data_type)) {
181 gpu::Batch *batch = DRW_cache_mesh_surface_viewer_attribute_get(&object);
182 auto &sub = *mesh_sub_;
183 sub.push_constant("opacity", opacity);
184 sub.draw(batch, manager.unique_handle(ob_ref));
185 }
186 }
187 break;
188 }
189 case OB_POINTCLOUD: {
191 if (const std::optional<bke::AttributeMetaData> meta_data =
192 pointcloud.attributes().lookup_meta_data(".viewer"))
193 {
194 if (attribute_type_supports_viewer_overlay(meta_data->data_type)) {
195 gpu::VertBuf **vertbuf = DRW_pointcloud_evaluated_attribute(&pointcloud, ".viewer");
196
197 /* Avoid trying to bind an empty `vertbuf` which causes assert / undefined behavior. */
198 if (pointcloud.totpoint > 0 && vertbuf != nullptr) {
199 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
200 auto &sub = (pointcloud.type == PointCloudType::GSplat) ? *gsplat_sub_ :
201 *pointcloud_sub_;
202 gpu::Batch *batch = pointcloud_sub_pass_setup(sub, ob_ref, res_handle, nullptr);
203 sub.push_constant("opacity", opacity);
204 sub.bind_texture("attribute_tx", vertbuf);
205 sub.draw(batch, res_handle);
206 }
207 }
208 }
209 break;
210 }
211 case OB_CURVES_LEGACY: {
213 if (curve.curve_eval) {
214 const bke::CurvesGeometry &curves = curve.curve_eval->geometry.wrap();
215 if (const std::optional<bke::AttributeMetaData> meta_data =
216 curves.attributes().lookup_meta_data(".viewer"))
217 {
218 if (attribute_type_supports_viewer_overlay(meta_data->data_type)) {
219 gpu::Batch *batch = DRW_cache_curve_edge_wire_viewer_attribute_get(&object);
220 auto &sub = *curve_sub_;
221 sub.push_constant("opacity", opacity);
222 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
223 sub.draw(batch, res_handle);
224 }
225 }
226 }
227 break;
228 }
229 case OB_CURVES: {
231 const bke::CurvesGeometry &curves = curves_id.geometry.wrap();
232 if (const std::optional<bke::AttributeMetaData> meta_data =
233 curves.attributes().lookup_meta_data(".viewer"))
234 {
235 if (attribute_type_supports_viewer_overlay(meta_data->data_type)) {
236 bool is_point_domain;
237 bool is_valid;
239 &curves_id, ".viewer", is_point_domain, is_valid);
240 if (is_valid) {
241 auto &sub = *curves_sub_;
242 const char *error = nullptr;
243 /* The error string will always have been printed by the engine already.
244 * No need to display it twice. */
245 gpu::Batch *batch = curves_sub_pass_setup(sub, state.scene, ob_ref.object, error);
246 sub.push_constant("opacity", opacity);
247 sub.push_constant("is_point_domain", is_point_domain);
248 sub.bind_texture("color_tx", texture);
249 sub.draw(batch, manager.unique_handle(ob_ref));
250 }
251 }
252 }
253 break;
254 }
255 default:
256 break;
257 }
258 }
259};
260} // namespace blender::draw::overlay
Low-level operations for curves.
#define ELEM(...)
AttributeSet attributes
std::optional< AttributeAccessor > attributes() const final
ResourceHandleRange unique_handle(const ObjectRef &ref)
int preview_instance_index() const
const bke::GeometrySet * preview_base_geometry() const
void push_constant(const char *name, const float &data)
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
void begin_sync(Resources &res, const State &state) final
void object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &, const State &state) final
void pre_draw(Manager &manager, View &view) final
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
#define attribute(slot)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
const int state
gpu::Batch * DRW_cache_curve_edge_wire_viewer_attribute_get(Object *ob)
detail::Pass< command::DrawMultiBuf > PassMain
gpu::Batch * DRW_cache_mesh_loose_edges_get(Object *ob)
gpu::Batch * curves_sub_pass_setup(PassMain::Sub &ps, const Scene *scene, Object *ob, const char *&r_error, GPUMaterial *gpu_material=nullptr)
gpu::Batch * DRW_cache_mesh_surface_viewer_attribute_get(Object *ob)
gpu::VertBuf ** DRW_pointcloud_evaluated_attribute(PointCloud *pointcloud, StringRef name)
gpu::Batch * DRW_cache_curve_edge_wire_get(Object *ob)
gpu::Batch * pointcloud_sub_pass_setup(PassMain::Sub &sub_ps, const ObjectRef &ob_ref, const ResourceHandleRange &res_handle, GPUMaterial *gpu_material=nullptr)
gpu::Batch * DRW_cache_mesh_surface_get(Object *ob)
gpu::VertBufPtr & DRW_curves_texture_for_evaluated_attribute(Curves *curves, StringRef name, bool &r_is_point_domain, bool &r_valid_attribute)
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
VecBase< float, 4 > float4
T & DRW_object_get_data_for_drawing(const Object &object)
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:57
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
const NodeType * type
Definition graph/node.h:179
const GeometryComponent * get_component(GeometryComponent::Type component_type) const