Blender V5.3
draw_cache_impl_pointcloud.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_array_utils.hh"
16#include "BLI_color_types.hh"
17#include "BLI_listbase.hh"
18#include "BLI_task.hh"
19#include "BLI_utildefines.hh"
20
21#include "DNA_object_types.h"
23#include "DNA_userdef_types.h"
24
25#include "BKE_attribute.hh"
26#include "BKE_material.hh"
27#include "BKE_pointcloud.hh"
28
29#include "GPU_batch.hh"
30#include "GPU_material.hh"
31
32#include "DRW_render.hh"
33
34#include "draw_attributes.hh"
35#include "draw_cache_impl.hh"
36#include "draw_cache_inline.hh"
37#include "draw_pointcloud_private.hh" /* own include */
38
39#include "draw_defines.hh"
40
41namespace blender::draw {
42
43/* -------------------------------------------------------------------- */
46
48 /* Dot primitive types. */
49 gpu::Batch *dots;
50 /* Triangle primitive types. */
51 gpu::Batch *surface;
52 gpu::Batch **surface_per_mat;
53
54 /* Position and radius. */
56 /* Active attribute in 3D view. */
58 /* Requested attributes */
60
68
75
77};
78
81
83 gpu::Batch *edit_selection = nullptr;
84
85 /* settings to determine if cache is invalid */
87};
88
90{
91 return pointcloud.pointcloud_batch_cache;
92}
93
95{
97
98 if (cache == nullptr) {
99 return false;
100 }
102 return false;
103 }
104 return cache->is_dirty == false;
105}
106
108{
110
111 if (!cache) {
112 cache = MEM_new<PointCloudBatchCache>(__func__);
113 pointcloud.pointcloud_batch_cache = cache;
114 }
115 else {
116 cache->eval_cache = {};
117 cache->edit_selection = nullptr;
118 cache->edit_selection_indices = nullptr;
119 }
120
123 __func__);
124
125 cache->is_dirty = false;
126}
127
129{
131 if (cache == nullptr) {
132 return;
133 }
134 switch (mode) {
136 cache->is_dirty = true;
137 break;
138 default:
139 BLI_assert(0);
140 }
141}
142
144{
145 for (const int j : IndexRange(GPU_MAX_ATTR)) {
147 }
148
149 cache.eval_cache.attr_used.clear();
150}
151
153{
155 if (!cache) {
156 return;
157 }
158
163
166
167 if (cache->eval_cache.surface_per_mat) {
168 for (int i = 0; i < cache->eval_cache.mat_len; i++) {
170 }
171 }
172 MEM_SAFE_DELETE(cache->eval_cache.surface_per_mat);
173
175}
176
178{
179 if (!pointcloud_batch_cache_valid(*pointcloud)) {
180 pointcloud_batch_cache_clear(*pointcloud);
181 pointcloud_batch_cache_init(*pointcloud);
182 }
183}
184
186{
187 pointcloud_batch_cache_clear(*pointcloud);
188 MEM_delete(pointcloud->pointcloud_batch_cache);
189 pointcloud->pointcloud_batch_cache = nullptr;
190}
191
193{
195 if (!cache) {
196 return;
197 }
198
199 bool do_discard = false;
200
202 {
203 cache->eval_cache.last_attr_matching_time = ctime;
204 }
205
206 if (ctime - cache->eval_cache.last_attr_matching_time > U.vbotimeout) {
207 do_discard = true;
208 }
209
211
212 if (do_discard) {
214 }
215}
216
218
219/* -------------------------------------------------------------------- */
222
225{
226 const bke::AttributeAccessor attributes = pointcloud.attributes();
227 const Span<float3> positions = pointcloud.positions();
228 const VArray<float> radii = *attributes.lookup<float>("radius");
229 static const GPUVertFormat format = [&]() {
230 GPUVertFormat format{};
231 GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32_32_32);
232 GPU_vertformat_alias_add(&format, "pos_rad");
233 return format;
234 }();
235
237 GPU_vertbuf_init_with_format_ex(*cache.eval_cache.pos_rad, format, usage_flag);
238
239 GPU_vertbuf_data_alloc(*cache.eval_cache.pos_rad, positions.size());
240 MutableSpan<float4> vbo_data = cache.eval_cache.pos_rad->data<float4>();
241 if (radii) {
242 const VArraySpan<float> radii_span(std::move(radii));
243 threading::parallel_for(vbo_data.index_range(), 4096, [&](IndexRange range) {
244 for (const int i : range) {
245 vbo_data[i].x = positions[i].x;
246 vbo_data[i].y = positions[i].y;
247 vbo_data[i].z = positions[i].z;
248 vbo_data[i].w = radii_span[i];
249 }
250 });
251 }
252 else {
253 threading::parallel_for(vbo_data.index_range(), 4096, [&](IndexRange range) {
254 for (const int i : range) {
255 vbo_data[i].x = positions[i].x;
256 vbo_data[i].y = positions[i].y;
257 vbo_data[i].z = positions[i].z;
258 vbo_data[i].w = 0.01f;
259 }
260 });
261 }
262}
263
264static void pointcloud_extract_attribute(const PointCloud &pointcloud,
266 const StringRef name,
267 int index)
268{
269 gpu::VertBuf &attr_buf = *cache.eval_cache.attributes_buf[index];
270
271 const bke::AttributeAccessor attributes = pointcloud.attributes();
272
273 /* TODO(@kevindietrich): float4 is used for scalar attributes as the implicit conversion done
274 * by OpenGL to float4 for a scalar `s` will produce a `float4(s, 0, 0, 1)`. However, following
275 * the Blender convention, it should be `float4(s, s, s, 1)`. This could be resolved using a
276 * similar texture state swizzle to map the attribute correctly as for volume attributes, so we
277 * can control the conversion ourselves. */
279 name, bke::AttrDomain::Point, {0.0f, 0.0f, 0.0f, 1.0f});
280
281 static const GPUVertFormat format = [&]() {
282 GPUVertFormat format{};
283 GPU_vertformat_attr_add(&format, "attr", gpu::VertAttrType::SFLOAT_32_32_32_32);
284 return format;
285 }();
287 GPU_vertbuf_init_with_format_ex(attr_buf, format, usage_flag);
288 GPU_vertbuf_data_alloc(attr_buf, pointcloud.totpoint);
289
290 array_utils::copy(attribute.varray, attr_buf.data<ColorGeometry4f>());
291}
292
294
295/* -------------------------------------------------------------------- */
298
305
307 GPUMaterial **gpu_materials,
308 int mat_len)
309{
310 const bke::AttributeAccessor attributes = pointcloud->attributes();
312 VectorSet<std::string> attrs_needed;
313
314 for (GPUMaterial *gpu_material : Span<GPUMaterial *>(gpu_materials, mat_len)) {
315 ListBaseT<GPUMaterialAttribute> gpu_attrs = GPU_material_attributes(gpu_material);
316 for (GPUMaterialAttribute &gpu_attr : gpu_attrs) {
317 const StringRef name = gpu_attr.name;
318 if (!attributes.contains(name)) {
319 continue;
320 }
321 drw_attributes_add_request(&attrs_needed, name);
322 }
323 }
324
325 if (!drw_attributes_overlap(&cache->eval_cache.attr_used, &attrs_needed)) {
326 /* Some new attributes have been added, free all and start over. */
327 for (const int i : IndexRange(GPU_MAX_ATTR)) {
329 }
330 drw_attributes_merge(&cache->eval_cache.attr_used, &attrs_needed);
331 }
333
335 return cache->eval_cache.surface_per_mat;
336}
337
338gpu::Batch *pointcloud_surface_get(PointCloud *pointcloud)
339{
341 if (cache.eval_cache.surface != nullptr) {
342 return cache.eval_cache.surface;
343 }
344
347 DRW_vbo_request(nullptr, &cache.eval_cache.pos_rad);
348 return cache.eval_cache.surface;
349}
350
352
353/* -------------------------------------------------------------------- */
356
358{
360 if (pointcloud.type == PointCloudType::Points) {
362 return DRW_batch_request(&cache->eval_cache.dots);
363 }
364 return nullptr;
365}
366
372
374{
375 if (pointcloud->type == PointCloudType::GSplat) {
376 /* GSplats are a subtype of point-clouds, so we forward to their implementation. */
377 return DRW_gsplat_evaluated_attribute(pointcloud, name);
378 }
379
380 const bke::AttributeAccessor attributes = pointcloud->attributes();
382
383 if (!attributes.contains(name)) {
384 return nullptr;
385 }
386 {
387 VectorSet<std::string> requests{};
388 drw_attributes_add_request(&requests, name);
389 drw_attributes_merge(&cache.eval_cache.attr_used, &requests);
390 }
391
392 int request_i = -1;
393 for (const int i : IndexRange(cache.eval_cache.attr_used.index_range())) {
394 if (cache.eval_cache.attr_used[i] == name) {
395 request_i = i;
396 break;
397 }
398 }
399 if (request_i == -1) {
400 return nullptr;
401 }
402 return &cache.eval_cache.attributes_buf[request_i];
403}
404
406{
407 const int max_index = mask.min_array_size();
408 GPUIndexBufBuilder builder;
409 GPU_indexbuf_init(&builder, GPU_PRIM_POINTS, mask.size(), max_index);
411 mask.to_indices<int>(data.cast<int>());
412 GPU_indexbuf_build_in_place_ex(&builder, 0, max_index, false, &ibo);
413}
414
415static void build_edit_selection_indices(const PointCloud &pointcloud, gpu::IndexBuf &ibo)
416{
417 const VArray selection = *pointcloud.attributes().lookup_or_default<bool>(
418 ".selection", bke::AttrDomain::Point, true);
419 IndexMaskMemory memory;
420 const IndexMask mask = IndexMask::from_bools(selection, memory);
421 if (mask.is_empty()) {
422 return;
423 }
425}
426
428{
431
434 }
435
439 }
440
441 for (int i = 0; i < cache.eval_cache.mat_len; i++) {
443 /* TODO(fclem): Per material ranges. */
444 }
445 }
446 for (const int j : cache.eval_cache.attr_used.index_range()) {
447 DRW_vbo_request(nullptr, &cache.eval_cache.attributes_buf[j]);
448
450 pointcloud_extract_attribute(pointcloud, cache, cache.eval_cache.attr_used[j], j);
451 }
452 }
453
456 }
457
460 }
461}
462
464{
466 if (pointcloud.type == PointCloudType::Points) {
468 return DRW_batch_request(&cache->edit_selection);
469 }
470 return nullptr;
471}
472
474
475} // namespace blender::draw
General operations, lookup, etc. for materials.
General operations for point clouds.
Object is a sort of wrapper for general info.
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition GPU_batch.hh:199
#define GPU_INDEXBUF_DISCARD_SAFE(elem)
#define GPU_VERTBUF_DISCARD_SAFE(verts)
Read Blender memory allocation and freeing.
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr IndexRange()=default
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr int64_t size() const
Definition BLI_span.hh:256
IndexRange index_range() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
bool contains(StringRef name) const
GAttributeReader lookup(const StringRef name) const
MutableSpan< T > data()
Utilities for rendering attributes.
#define DRW_POINTCLOUD_STRIP_TILE_SIZE
#define attribute(slot)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void drw_attributes_add_request(VectorSet< std::string > *attrs, const StringRef name)
static PointCloudBatchCache * pointcloud_batch_cache_get(PointCloud &pointcloud)
gpu::Batch * pointcloud_surface_get(PointCloud *pointcloud)
static void pointcloud_extract_attribute(const PointCloud &pointcloud, PointCloudBatchCache &cache, const StringRef name, int index)
static void pointcloud_extract_position_and_radius(const PointCloud &pointcloud, PointCloudBatchCache &cache)
static void build_edit_selection_indices(const PointCloud &pointcloud, gpu::IndexBuf &ibo)
static void pointcloud_discard_attributes(PointCloudBatchCache &cache)
gpu::Batch * DRW_pointcloud_batch_cache_get_edit_dots(Object *ob)
void DRW_pointcloud_batch_cache_dirty_tag(PointCloud *pointcloud, int mode)
static void pointcloud_batch_cache_init(PointCloud &pointcloud)
gpu::Batch ** pointcloud_surface_shaded_get(PointCloud *pointcloud, GPUMaterial **gpu_materials, int mat_len)
gpu::Batch * DRW_pointcloud_batch_cache_get_dots(Object *ob)
static void pointcloud_batch_cache_clear(PointCloud &pointcloud)
void DRW_pointcloud_batch_cache_free_old(PointCloud *pointcloud, int ctime)
bool drw_attributes_overlap(const VectorSet< std::string > *a, const VectorSet< std::string > *b)
gpu::VertBuf ** DRW_pointcloud_evaluated_attribute(PointCloud *pointcloud, StringRef name)
gpu::VertBuf ** DRW_gsplat_evaluated_attribute(PointCloud *pointcloud, StringRef name)
void DRW_pointcloud_batch_cache_create_requested(Object *ob)
void DRW_pointcloud_batch_cache_validate(PointCloud *pointcloud)
static void index_mask_to_ibo(const IndexMask &mask, gpu::IndexBuf &ibo)
static bool pointcloud_batch_cache_valid(PointCloud &pointcloud)
void drw_attributes_merge(VectorSet< std::string > *dst, const VectorSet< std::string > *src)
void DRW_pointcloud_batch_cache_free(PointCloud *pointcloud)
gpu::VertBuf * pointcloud_position_and_radius_get(PointCloud *pointcloud)
gpu::VertBuf * DRW_pointcloud_position_and_radius_buffer_get(Object *ob)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ BKE_POINTCLOUD_BATCH_DIRTY_ALL
bool DRW_vbo_requested(gpu::VertBuf *vbo)
gpu::Batch * GPU_batch_create_procedural(GPUPrimType primitive_type, int32_t vertex_count)
Definition gpu_batch.cc:85
void GPU_vertformat_alias_add(GPUVertFormat *, StringRef alias)
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
void GPU_vertbuf_init_with_format_ex(gpu::VertBuf &verts, const GPUVertFormat &format, GPUUsageType)
bool DRW_batch_requested(gpu::Batch *batch, GPUPrimType prim_type)
@ GPU_PRIM_TRI_STRIP
VecBase< float, 4 > float4
int BKE_id_material_used_with_fallback_eval(const ID &id)
void DRW_ibo_request(gpu::Batch *batch, gpu::IndexBuf **ibo)
void GPU_indexbuf_build_in_place_ex(GPUIndexBufBuilder *builder, uint index_min, uint index_max, bool uses_restart_indices, gpu::IndexBuf *elem)
T & DRW_object_get_data_for_drawing(const Object &object)
BMesh const char void * data
ListBaseT< GPUMaterialAttribute > GPU_material_attributes(const GPUMaterial *material)
MutableSpan< uint32_t > GPU_indexbuf_get_data(GPUIndexBufBuilder *)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
gpu::Batch * DRW_batch_request(gpu::Batch **batch)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void DRW_vbo_request(gpu::Batch *batch, gpu::VertBuf **vbo)
@ GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool DRW_ibo_requested(gpu::IndexBuf *ibo)
draw::PointCloudBatchCache * pointcloud_batch_cache