Blender V5.3
draw_handle.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
21
22#include "BKE_duplilist.hh"
23#include "draw_shader_shared.hh"
24
25/* random */
26#include "BLI_hash_c.hh"
27
28/* find_rgba_attribute */
29#include "GPU_material.hh"
30
31/* particles_matrix */
32#include "BLI_math_matrix.hh"
34
35/* parent_is_in_edit_paint_mode */
36#include "BKE_context.hh"
37#include "BKE_paint.hh"
38#include "DNA_layer_types.h"
39#include "DRW_render.hh"
40
41/* ObjectKey */
43
44namespace blender {
45
47
48namespace draw {
49
51
60struct ResourceID {
62
63 ResourceID() = default;
64 ResourceID(uint raw_) : raw(raw_) {};
65 ResourceID(uint index, bool inverted_handedness)
66 {
67 raw = index;
68 SET_FLAG_FROM_TEST(raw, inverted_handedness, 0x80000000u);
69 }
70
72 {
73 return (raw & 0x80000000u) != 0;
74 }
75
76 uint index() const
77 {
78 return (raw & 0x7FFFFFFFu);
79 }
80};
81
88 /* First handle in the range. */
90 /* Number of handles in the range. */
92
93 ResourceIDRange() = default;
95 ResourceIDRange(ResourceID first_id, uint len) : first(first_id), count(len) {}
96
98 {
100 }
101
103 {
104 BLI_assert(count > 0);
105 BLI_assert(first.raw != 0 || count == 1);
106 return {first.raw, count};
107 }
108};
109
117class ResourceHandle {
118 friend class Manager;
120
121 ResourceID id_ = {};
122
123 ResourceHandle(uint raw) : id_(raw) {}
124 ResourceHandle(uint index, bool inverted_handedness) : id_(index, inverted_handedness) {}
125
126 public:
127 ResourceHandle() = default;
128
129 bool is_valid() const
130 {
131 return id_.raw != 0;
132 }
133
135 {
136 return id_.has_inverted_handedness();
137 }
138
139 uint raw() const
140 {
141 return id_.raw;
142 }
143
144 uint index() const
145 {
146 return id_.index();
147 }
148
149 operator ResourceID() const
150 {
152 return id_;
153 }
154};
155
160class ResourceHandleRange {
161 friend class Manager;
162 friend class ResourceHandle;
163
164 ResourceIDRange id_ = {};
165
166 ResourceHandleRange(ResourceHandle first_handle, uint len) : id_(first_handle.id_, len) {}
167
168 public:
170 ResourceHandleRange(ResourceHandle handle) : id_(handle.id_) {}
171
172 bool is_valid() const
173 {
174 return id_.first.raw != 0;
175 }
176
178 {
179 return id_.has_inverted_handedness();
180 }
181
183 {
184 return id_.id_range();
185 }
186
187 operator ResourceIDRange() const
188 {
190 return id_;
191 }
192
193 /* Returns a single handle within this range.
194 * May be required for passes that require per-instance setups,
195 * so their drawing can't be batched into a single draw call. */
197 {
198 BLI_assert(index < id_.count);
199 return ResourceHandle(id_.first.index() + index, id_.first.has_inverted_handedness());
200 }
201
202 operator ResourceHandle() const
203 {
204 BLI_assert(id_.count == 1);
205 return ResourceHandle(id_.first.raw);
206 }
207
208 uint raw() const
209 {
210 BLI_assert(id_.count == 1);
211 return id_.first.raw;
212 }
213
214 uint index() const
215 {
216 BLI_assert(id_.count == 1);
217 return id_.first.index();
218 }
219};
220
221/* TODO(fclem): Move to somewhere more appropriated after cleaning up the header dependencies. */
223 friend class Manager;
224 friend class ObjectKey;
225 friend DupliCacheManager;
226
227 private:
229 DupliObject *const dupli_object_ = nullptr;
231 Object *const dupli_parent_ = nullptr;
232
234 const VectorList<DupliObject *> *duplis_ = nullptr;
235
237 ResourceHandleRange handle_ = {};
238 ResourceHandleRange sculpt_handle_ = {};
239
240 /* For ParticleSystems of the main object. */
241 uint sub_key_ = 0;
242
243 public:
245
246 explicit ObjectRef(Object *ob,
247 Object *dupli_parent = nullptr,
248 DupliObject *dupli_object = nullptr);
249 explicit ObjectRef(Object &ob, Object *dupli_parent, const VectorList<DupliObject *> &duplis);
250 explicit ObjectRef(const ObjectRef &ob_ref, uint sub_key) : ObjectRef(ob_ref)
251 {
252 sub_key_ = sub_key;
253 };
254
255 /* Is the object coming from a Dupli system. */
256 bool is_dupli() const
257 {
258 return dupli_parent_ != nullptr;
259 }
260
261 bool is_active(const Object *active_object) const
262 {
263 return (dupli_parent_ ? dupli_parent_ : object) == active_object;
264 }
265
266 bool is_range() const
267 {
268 return duplis_ != nullptr;
269 }
270
271 int instances_count() const
272 {
273 if (duplis_) {
274 return duplis_->size();
275 }
276 return 1;
277 }
278
280 {
281 if (is_range()) {
282 return float4x4((*duplis_)[instance_index]->mat);
283 }
284 else {
286 return object->object_to_world();
287 }
288 }
289
291 {
293 return object_to_world(0);
294 }
295
296 float random(int instance_index) const
297 {
298 if (instance_index != 0) {
300 return (*duplis_)[instance_index]->random_id * (1.0f / float(0xFFFFFFFF));
301 }
302
303 if (dupli_parent_ == nullptr) {
304 /* TODO(fclem): this is rather costly to do at draw time. Maybe we can
305 * put it in ob->runtime and make depsgraph ensure it is up to date. */
306 return BLI_hash_int_2d(BLI_hash_string(object->id.name + 2), 0) * (1.0f / float(0xFFFFFFFF));
307 }
308 return dupli_object_->random_id * (1.0f / float(0xFFFFFFFF));
309 }
310
311 bool find_rgba_attribute(const char *name,
312 bool use_dupli,
313 int instance_index,
314 float r_value[4]) const
315 {
316 if (instance_index != 0) {
318 if (use_dupli) {
319 /* If requesting instance data, check the parent particle system and object. */
321 object, (*duplis_)[instance_index], dupli_parent_, name, r_value);
322 }
323 }
324
325 /* If requesting instance data, check the parent particle system and object. */
326 if (use_dupli) {
328 object, dupli_object_, dupli_parent_, name, r_value);
329 }
330 return BKE_object_dupli_find_rgba_attribute(object, nullptr, nullptr, name, r_value);
331 }
332
333 bool find_rgba_attribute(const GPUUniformAttr &attr, int instance_index, float r_value[4]) const
334 {
335 return find_rgba_attribute(attr.name, attr.use_dupli, instance_index, r_value);
336 }
337
339 {
340 const LightLinking *light_linking = object->light_linking;
341 return (light_linking) ? light_linking->runtime.light_set_membership : LIGHT_LINK_MASK_ALL;
342 }
343
345 {
346 if (dupli_parent_) {
347 const LightLinking *parent_light_linking = dupli_parent_->light_linking;
348 if (parent_light_linking && parent_light_linking->runtime.receiver_light_set) {
349 return parent_light_linking->runtime.receiver_light_set;
350 }
351 }
352
353 const LightLinking *light_linking = object->light_linking;
354 return (light_linking) ? light_linking->runtime.receiver_light_set : 0;
355 }
356
358 {
359 const LightLinking *light_linking = object->light_linking;
360 return (light_linking) ? light_linking->runtime.shadow_set_membership : LIGHT_LINK_MASK_ALL;
361 }
362
364 {
365 if (dupli_parent_) {
366 const LightLinking *parent_light_linking = dupli_parent_->light_linking;
367 if (parent_light_linking && parent_light_linking->runtime.blocker_shadow_set) {
368 return parent_light_linking->runtime.blocker_shadow_set;
369 }
370 }
371
372 const LightLinking *light_linking = object->light_linking;
373 return (light_linking) ? light_linking->runtime.blocker_shadow_set : 0;
374 }
375
376 uint recalc_flags(uint64_t last_update) const
377 {
378 /* TODO: There should also be a way to get the min last_update for all objects in the range. */
379 auto get_flags = [&](const bke::ObjectRuntime &runtime) {
380 uint flags = 0;
381 SET_FLAG_FROM_TEST(flags, runtime.last_update_transform > last_update, ID_RECALC_TRANSFORM);
382 SET_FLAG_FROM_TEST(flags, runtime.last_update_geometry > last_update, ID_RECALC_GEOMETRY);
383 SET_FLAG_FROM_TEST(flags, runtime.last_update_shading > last_update, ID_RECALC_SHADING);
384 return flags;
385 };
386
387 uint flags = get_flags(*object->runtime);
388 if (dupli_parent_) {
389 flags |= get_flags(*dupli_parent_->runtime);
390 }
391
392 return flags;
393 }
394
395 /* Particle data are stored in world space. If an object is instanced, the associated particle
396 * systems need to be offset appropriately. */
398 {
399 /* Objects with particles don't support instancing optimizations yet. */
401
402 /* TODO: Pass particle systems as a separate ObRef? */
403 float4x4 dupli_mat = float4x4::identity();
404 if (dupli_parent_ && dupli_object_) {
405 if (dupli_object_->type & OB_DUPLICOLLECTION) {
406 Collection *collection = dupli_parent_->instance_collection;
407 if (collection != nullptr) {
408 dupli_mat[3] -= float4(float3(collection->instance_offset), 0.0f);
409 }
410 dupli_mat = dupli_parent_->object_to_world() * dupli_mat;
411 }
412 else {
413 dupli_mat = object->object_to_world() * math::invert(dupli_object_->ob->object_to_world());
414 }
415 }
416 return dupli_mat;
417 }
418
420 {
421 if (dupli_object_) {
422 return dupli_object_->preview_instance_index;
423 }
424 return -1;
425 }
426
428 {
429 if (dupli_object_) {
430 return dupli_object_->preview_base_geometry;
431 }
432 return nullptr;
433 }
434
435 bool parent_is_in_edit_paint_mode(const Object *active_object,
436 eObjectMode ob_mode,
437 eContextObjectMode ctx_mode) const
438 {
439 /* TODO: Deduplicate code with Overlay engine.
440 * Move to BKE ? Or check if T72490 is still relevant. */
441
442 if (!dupli_parent_ || active_object != dupli_parent_) {
443 return false;
444 }
445
446 if (dupli_parent_->runtime->sculpt_session &&
447 (dupli_parent_->runtime->sculpt_session->mode_type == OB_MODE_SCULPT))
448 {
449 return true;
450 }
451
453 return true;
454 }
455
456 if (DRW_object_is_in_edit_mode(dupli_parent_)) {
457 /* Also check for context mode as the object mode is not 100% reliable. (see T72490) */
458 switch (dupli_parent_->type) {
459 case OB_MESH:
460 return ctx_mode == CTX_MODE_EDIT_MESH;
461 case OB_ARMATURE:
462 return ctx_mode == CTX_MODE_EDIT_ARMATURE;
463 case OB_CURVES_LEGACY:
464 return ctx_mode == CTX_MODE_EDIT_CURVE;
465 case OB_SURF:
466 return ctx_mode == CTX_MODE_EDIT_SURFACE;
467 case OB_LATTICE:
468 return ctx_mode == CTX_MODE_EDIT_LATTICE;
469 case OB_MBALL:
470 return ctx_mode == CTX_MODE_EDIT_METABALL;
471 case OB_FONT:
472 return ctx_mode == CTX_MODE_EDIT_TEXT;
473 case OB_CURVES:
474 return ctx_mode == CTX_MODE_EDIT_CURVES;
475 case OB_POINTCLOUD:
476 return ctx_mode == CTX_MODE_EDIT_POINTCLOUD;
477 case OB_GREASE_PENCIL:
478 return ctx_mode == CTX_MODE_EDIT_GREASE_PENCIL;
479 case OB_VOLUME:
480 /* No edit mode yet. */
481 return false;
482 default:
483 return false;
484 }
485 }
486 return false;
487 }
488};
489
490/* -------------------------------------------------------------------- */
496
499 uint64_t hash_value_ = 0;
501 Object *ob_ = nullptr;
503 Object *parent_ = nullptr;
505 int id_[MAX_DUPLI_RECUR];
507 int sub_key_ = 0;
508
509 public:
510 ObjectKey() = default;
511
513 {
514 ob_ = DEG_get_original(ob_ref.object);
515 hash_value_ = get_default_hash(ob_);
516
517 if (DupliObject *dupli = instance_index ? (*ob_ref.duplis_)[instance_index] :
518 ob_ref.dupli_object_)
519 {
520 parent_ = ob_ref.dupli_parent_;
521 hash_value_ = get_default_hash(hash_value_, get_default_hash(parent_));
522 for (int i : IndexRange(MAX_DUPLI_RECUR)) {
523 id_[i] = dupli->persistent_id[i];
524 if (id_[i] == std::numeric_limits<int>::max()) {
525 break;
526 }
527 hash_value_ = get_default_hash(hash_value_, get_default_hash(id_[i]));
528 }
529 }
530
531 if (ob_ref.sub_key_ != 0) {
532 sub_key_ = ob_ref.sub_key_;
533 hash_value_ = get_default_hash(hash_value_, get_default_hash(sub_key_));
534 }
535 }
536
537 ObjectKey(const ObjectRef &ob_ref) : ObjectKey(ob_ref, 0)
538 {
539 BLI_assert(!ob_ref.is_range());
540 }
541
542 /* Special handles that will have nullptr object.
543 * Used for inserting helper items inside the hash-maps without creating a dummy #Object. */
544 explicit ObjectKey(int key)
545 {
546 sub_key_ = key;
547 hash_value_ = get_default_hash(ob_);
548 hash_value_ = get_default_hash(hash_value_, get_default_hash(sub_key_));
549 }
550
552 {
553 return hash_value_;
554 }
555
556 bool operator==(const ObjectKey &k) const
557 {
558 if (hash_value_ != k.hash_value_) {
559 return false;
560 }
561 if (ob_ != k.ob_) {
562 return false;
563 }
564 if (parent_ != k.parent_) {
565 return false;
566 }
567 if (sub_key_ != k.sub_key_) {
568 return false;
569 }
570 if (parent_) {
571 for (int i : IndexRange(MAX_DUPLI_RECUR)) {
572 if (id_[i] != k.id_[i]) {
573 return false;
574 }
575 if (id_[i] == std::numeric_limits<int>::max()) {
576 break;
577 }
578 }
579 }
580 return true;
581 }
582};
583
585
586}; // namespace draw
587
588} // namespace blender
Object groups, one object can be in many groups at once.
#define OB_MODE_ALL_PAINT_MESH
#define OB_MODE_ALL_PAINT_GPENCIL
#define SET_FLAG_FROM_TEST(value, test, flag)
constexpr IndexRange()=default
ObjectKey(const ObjectRef &ob_ref, int instance_index)
bool operator==(const ObjectKey &k) const
uint64_t hash() const
ObjectKey(const ObjectRef &ob_ref)
bool parent_is_in_edit_paint_mode(const Object *active_object, eObjectMode ob_mode, eContextObjectMode ctx_mode) const
ObjectRef(const ObjectRef &ob_ref, uint sub_key)
bool is_active(const Object *active_object) const
uint64_t get_light_set_membership() const
ObjectRef(Object *ob, Object *dupli_parent=nullptr, DupliObject *dupli_object=nullptr)
uint8_t get_receiver_light_set() const
uint recalc_flags(uint64_t last_update) const
float4x4 object_to_world() const
int preview_instance_index() const
float4x4 particles_matrix() const
uint64_t get_shadow_set_membership() const
bool find_rgba_attribute(const GPUUniformAttr &attr, int instance_index, float r_value[4]) const
const bke::GeometrySet * preview_base_geometry() const
uint8_t get_blocker_shadow_set() const
bool find_rgba_attribute(const char *name, bool use_dupli, int instance_index, float r_value[4]) const
float4x4 object_to_world(int instance_index) const
float random(int instance_index) const
ResourceHandleRange(ResourceHandle handle)
ResourceHandle sub_handle(int index) const
#define instance_index
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
static constexpr uint64_t LIGHT_LINK_MASK_ALL
CartesianBasis invert(const CartesianBasis &basis)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint
Definition BKE_text.h:55
BLI_INLINE unsigned int BLI_hash_int_2d(unsigned int kx, unsigned int ky)
Definition BLI_hash_c.hh:53
bool BKE_object_dupli_find_rgba_attribute(const Object *ob, const DupliObject *dupli, const Object *dupli_parent, const char *name, float r_value[4])
MatBase< float, 4, 4 > float4x4
@ CTX_MODE_EDIT_TEXT
@ CTX_MODE_EDIT_SURFACE
@ CTX_MODE_EDIT_ARMATURE
@ CTX_MODE_EDIT_CURVES
@ CTX_MODE_EDIT_LATTICE
@ CTX_MODE_EDIT_POINTCLOUD
@ CTX_MODE_EDIT_MESH
@ CTX_MODE_EDIT_METABALL
@ CTX_MODE_EDIT_GREASE_PENCIL
@ CTX_MODE_EDIT_CURVE
VecBase< float, 4 > float4
unsigned int uint32_t
Definition BKE_text.h:81
unsigned char uint8_t
Definition BKE_text.h:79
constexpr int MAX_DUPLI_RECUR
T * DEG_get_original(T *id)
BLI_INLINE unsigned int BLI_hash_string(const char *str)
Definition BLI_hash_c.hh:69
@ ID_RECALC_SHADING
Definition DNA_ID.h:1094
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
VecBase< float, 3 > float3
bool DRW_object_is_in_edit_mode(const Object *ob)
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
char name[258]
Definition DNA_ID.h:433
LightLinkingRuntime runtime
bke::ObjectRuntime * runtime
ResourceIDRange(ResourceID first_id, uint len)
bool has_inverted_handedness() const
ResourceID(uint index, bool inverted_handedness)