Blender V5.3
draw_manager.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
16
17#include "BLI_listbase.hh"
18#include "BLI_map.hh"
19#include "BLI_sys_types.hh"
20
21#include "GPU_material.hh"
22
23#include "IMB_imbuf.hh"
24
25#include "draw_handle.hh"
26#include "draw_resource.hh"
27#include "draw_view.hh"
28
29#include "RNA_access.hh"
30
31#include <atomic>
32#include <memory>
33
34namespace blender {
35
36struct Image;
37struct ImageUser;
38
39namespace draw {
40
41/* Forward declarations. */
42
43namespace detail {
44template<typename T> class Pass;
45} // namespace detail
46
47namespace command {
48class DrawCommandBuf;
49class DrawMultiBuf;
50} // namespace command
51
54class PassSortable;
55
56class Manager {
57 using ObjectMatricesBuf = StorageArrayBuffer<ObjectMatrices, 128>;
58 using ObjectBoundsBuf = StorageArrayBuffer<ObjectBounds, 128>;
59 using ObjectInfosBuf = StorageArrayBuffer<ObjectInfos, 128>;
60 using ObjectAttributeBuf = StorageArrayBuffer<ObjectAttribute, 128>;
61 using LayerAttributeBuf = UniformArrayBuffer<LayerAttribute, 512>;
62
63 public:
70
79
81 /* Input to load textures from. */
84 bool use_tile_mapping = false;
85
86 /* Loaded textures. */
89 };
90
98
103 ObjectAttributeBuf attributes_buf;
104
109
113 LayerAttributeBuf layer_attributes_buf;
114
120
121 private:
127 Vector<std::unique_ptr<DeferredTexture>> deferred_textures_;
128
130 static std::atomic<uint32_t> global_sync_counter_;
131
132 /* Local sync counter. Used for fingerprint. Must never be null. */
133 uint32_t sync_counter_ = 1;
134
136 uint resource_len_ = 0;
138 uint attribute_len_ = 0;
139
140 Object *object_active = nullptr;
141
142 public:
144 ~Manager();
145
150 /* WORKAROUND: Instead of breaking const correctness everywhere, we only break it for this. */
152
154
158 ResourceHandleRange resource_handle(const ObjectRef &ref, float inflate_bounds = 0.0f);
164 const float4x4 *model_matrix,
165 const float3 *bounds_center,
166 const float3 *bounds_half_extent);
171 ResourceHandle resource_handle(const float4x4 &model_matrix);
177 ResourceHandle resource_handle(const float4x4 &model_matrix,
178 const float3 &bounds_center,
179 const float3 &bounds_half_extent);
184 ResourceHandle resource_handle_for_psys(const ObjectRef &ref, const float4x4 &model_matrix);
185
188 const ObjectRef &ref,
189 float inflate_bounds = 0.0f);
190
196 const ObjectRef &ref,
197 const GPUMaterial *material);
199 const ObjectRef &ref,
200 Span<GPUMaterial *> materials);
201
203
208
246
252 void compute_visibility(View &view);
256 void ensure_visibility(View &view);
265 void generate_commands(PassMain &pass, View &view);
266 void generate_commands(PassSortable &pass, View &view);
270 void generate_commands(PassSimple &pass);
271
278
284 void submit_only(PassMain &pass, View &view);
289 void submit(PassSimple &pass, View &view);
290 void submit(PassMain &pass, View &view);
291 void submit(PassSortable &pass, View &view);
295 void submit(PassSimple &pass, bool inverted_view = false);
296
300 SubmitDebugOutput submit_debug(PassSimple &pass, View &view);
301 SubmitDebugOutput submit_debug(PassMain &pass, View &view);
302
306 DataDebugOutput data_debug();
307
313 {
314 if (texture != nullptr) {
316 }
317 }
318
323 blender::ImageUser *image_user,
324 const bool use_tile_mapping)
325 {
326 deferred_textures_.append(std::make_unique<DeferredTexture>());
327 DeferredTexture &deferred = *deferred_textures_.last();
328 deferred.image = image;
329 deferred.image_user = image_user;
330 deferred.use_tile_mapping = use_tile_mapping;
331 return deferred;
332 }
333
338 {
339 return !deferred_textures_.is_empty();
340 }
341
346 {
347 return resource_len_;
348 }
349
351 void begin_sync(Object *object_active = nullptr);
352 void end_sync();
353
355
356 void debug_bind();
357 void resource_bind();
358
359 private:
360 void sync_layer_attributes();
361
362 /* Fingerprint of the manager in a certain state. Assured to not be 0.
363 * Not reliable enough for general update detection. Only to be used for debugging assertion. */
364 uint64_t fingerprint_get();
365};
366
368{
369 if (!ref.handle_.is_valid()) {
370 /* WORKAROUND: Instead of breaking const correctness everywhere, we only break it for this. */
371 const_cast<ObjectRef &>(ref).handle_ = resource_handle(ref);
372 }
373 return ref.handle_;
374}
375
376inline ResourceHandleRange Manager::resource_handle(const ObjectRef &ref, float inflate_bounds)
377{
378 bool is_active_object = ref.is_active(object_active);
379 bool is_active_edit_mode = object_active &&
380 (DRW_object_is_in_edit_mode(object_active) ||
381 ELEM(object_active->mode, OB_MODE_TEXTURE_PAINT, OB_MODE_SCULPT)) &&
382 ref.object->mode == object_active->mode;
383 if (ref.duplis_) {
384 uint start = resource_len_;
385
386 ObjectBounds proto_bounds;
387 proto_bounds.sync(*ref.object, inflate_bounds);
388
389 ObjectInfos proto_info;
390 proto_info.sync(ref, is_active_object, is_active_edit_mode);
391
392 for (const DupliObject *dupli : *ref.duplis_) {
393 const float4x4 dupli_mat(dupli->mat);
394 matrix_buf.current().get_or_resize(resource_len_).sync(dupli_mat);
395 bounds_buf.current().get_or_resize(resource_len_) = proto_bounds;
396
397 ObjectInfos &info = infos_buf.current().get_or_resize(resource_len_);
398 info = proto_info;
399 info.random = dupli->random_id * (1.0f / float(0xFFFFFFFF));
400 /* Normal offset is scaled by world transform, so it must be computed per instance. */
402 dupli_mat);
403
404 resource_len_++;
405 }
407 resource_len_ - start);
408 }
409 else {
410 matrix_buf.current().get_or_resize(resource_len_).sync(*ref.object);
411 bounds_buf.current().get_or_resize(resource_len_).sync(*ref.object, inflate_bounds);
412 infos_buf.current()
413 .get_or_resize(resource_len_)
414 .sync(ref, is_active_object, is_active_edit_mode);
415 return ResourceHandle(resource_len_++, (ref.object->transflag & OB_NEG_SCALE) != 0);
416 }
417}
418
420 const float4x4 *model_matrix,
421 const float3 *bounds_center,
422 const float3 *bounds_half_extent)
423{
424 BLI_assert(!ref.duplis_);
425 bool is_active_object = ref.is_active(object_active);
426 bool is_active_edit_mode = object_active &&
427 (DRW_object_is_in_edit_mode(object_active) ||
428 ELEM(object_active->mode, OB_MODE_TEXTURE_PAINT, OB_MODE_SCULPT)) &&
429 ref.object->mode == object_active->mode;
430 if (model_matrix) {
431 matrix_buf.current().get_or_resize(resource_len_).sync(*model_matrix);
432 }
433 else {
434 matrix_buf.current().get_or_resize(resource_len_).sync(*ref.object);
435 }
436 if (bounds_center && bounds_half_extent) {
437 bounds_buf.current().get_or_resize(resource_len_).sync(*bounds_center, *bounds_half_extent);
438 }
439 else {
440 bounds_buf.current().get_or_resize(resource_len_).sync(*ref.object);
441 }
442 infos_buf.current()
443 .get_or_resize(resource_len_)
444 .sync(ref, is_active_object, is_active_edit_mode);
445 return ResourceHandle(resource_len_++, (ref.object->transflag & OB_NEG_SCALE) != 0);
446}
447
449{
450 matrix_buf.current().get_or_resize(resource_len_).sync(model_matrix);
451 bounds_buf.current().get_or_resize(resource_len_).sync();
452 infos_buf.current().get_or_resize(resource_len_).sync();
453 return ResourceHandle(resource_len_++, false);
454}
455
457 const float3 &bounds_center,
458 const float3 &bounds_half_extent)
459{
460 matrix_buf.current().get_or_resize(resource_len_).sync(model_matrix);
461 bounds_buf.current().get_or_resize(resource_len_).sync(bounds_center, bounds_half_extent);
462 infos_buf.current().get_or_resize(resource_len_).sync();
463 return ResourceHandle(resource_len_++, false);
464}
465
467 const float4x4 &model_matrix)
468{
469 BLI_assert(!ref.duplis_);
470 bool is_active_object = ref.is_active(object_active);
471 bool is_active_edit_mode = object_active &&
472 (DRW_object_is_in_edit_mode(object_active) ||
473 ELEM(object_active->mode, OB_MODE_TEXTURE_PAINT, OB_MODE_SCULPT)) &&
474 ref.object->mode == object_active->mode;
475 matrix_buf.current().get_or_resize(resource_len_).sync(model_matrix);
476 bounds_buf.current().get_or_resize(resource_len_).sync();
477 infos_buf.current()
478 .get_or_resize(resource_len_)
479 .sync(ref, is_active_object, is_active_edit_mode);
480 return ResourceHandle(resource_len_++, (ref.object->transflag & OB_NEG_SCALE) != 0);
481}
482
484 const ObjectRef &ref,
485 float inflate_bounds)
486{
487 bounds_buf.current()[handle.index()].sync(*ref.object, inflate_bounds);
488}
489
491 const ObjectRef &ref,
492 const GPUMaterial *material)
493{
494 const GPUUniformAttrList *attr_list = GPU_material_uniform_attributes(material);
495 if (attr_list == nullptr || attr_list->count == 0) {
496 return;
497 }
498
499 int instance_index = 0;
500 for (ResourceID resource_id : handle.id_range()) {
501 ObjectInfos &infos = infos_buf.current().get_or_resize(resource_id.index());
502 infos.object_attrs_offset = attribute_len_;
503 for (const GPUUniformAttr &attr : attr_list->list) {
504 if (attributes_buf.get_or_resize(attribute_len_).sync(ref, *&attr, instance_index)) {
505 infos.object_attrs_len++;
506 attribute_len_++;
507 }
508 }
510 }
511}
512
514 const ObjectRef &ref,
515 Span<GPUMaterial *> materials)
516{
518
519 for (const GPUMaterial *mat : materials) {
521 if (attr_list == nullptr) {
522 continue;
523 }
524 for (const GPUUniformAttr &attr : attr_list->list) {
525 attributes.add(attr.hash_code, &attr);
526 }
527 }
528
529 int instance_index = 0;
530 for (ResourceID resource_id : handle.id_range()) {
531 ObjectInfos &infos = infos_buf.current().get_or_resize(resource_id.index());
532 infos.object_attrs_offset = attribute_len_;
533 for (const GPUUniformAttr *attr : attributes.values()) {
534 if (attributes_buf.get_or_resize(attribute_len_).sync(ref, *attr, instance_index)) {
535 infos.object_attrs_len++;
536 attribute_len_++;
537 }
538 }
540 }
541}
542
544 const ObjectRef &ref)
545{
546 /* TODO: Make find_property_rgba public and call it directly. */
548
550 if (IDProperty *group = RNA_struct_idprops(&ob_ptr, false)) {
551 BLI_assert(group->type == IDP_GROUP);
552 for (IDProperty &prop : group->data.group) {
553 attrs.append(prop.name);
554 }
555 }
557 if (IDProperty *group = RNA_struct_idprops(&data_ptr, false)) {
558 BLI_assert(group->type == IDP_GROUP);
559 for (IDProperty &prop : group->data.group) {
560 attrs.append(prop.name);
561 }
562 }
563
564 int instance_index = 0;
565 for (ResourceID resource_id : handle.id_range()) {
566 ObjectInfos &infos = infos_buf.current().get_or_resize(resource_id.index());
567 infos.object_attrs_offset = attribute_len_;
568 for (const char *name : attrs) {
569 if (attributes_buf.get_or_resize(attribute_len_).sync(ref, name, false, instance_index)) {
570 infos.object_attrs_len++;
571 attribute_len_++;
572 }
573 }
575 }
576}
577
579{
580 const ListBaseT<GPULayerAttr> *attr_list = GPU_material_layer_attributes(material);
581
582 if (attr_list != nullptr) {
583 for (const GPULayerAttr &attr : *attr_list) {
584 /* Since layer attributes are global to the whole render pass,
585 * this only collects a table of their names. */
586 layer_attributes.add(attr.hash_code, *&attr);
587 }
588 }
589}
590
591} // namespace draw
592
593/* TODO(@fclem): This is for testing. The manager should be passed to the engine through the
594 * callbacks. */
596
597} // namespace blender
#define ELEM(...)
ValueIterator values() const &
Definition BLI_map.hh:884
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
void append(const T &value)
LayerAttributeBuf layer_attributes_buf
void hold_texture(gpu::Texture *texture)
void begin_sync(Object *object_active=nullptr)
void register_layer_attributes(GPUMaterial *material)
SwapChain< ObjectBoundsBuf, 2 > bounds_buf
SubmitDebugOutput submit_debug(PassSimple &pass, View &view)
ResourceHandle resource_handle_for_psys(const ObjectRef &ref, const float4x4 &model_matrix)
ResourceHandleRange unique_handle_for_sculpt(const ObjectRef &ref)
void generate_commands(PassMain &pass, View &view)
SwapChain< ObjectInfosBuf, 2 > infos_buf
bool has_deferred_textures() const
ResourceHandleRange unique_handle(const ObjectRef &ref)
ObjectAttributeBuf attributes_buf
Map< uint32_t, GPULayerAttr > layer_attributes
void compute_visibility(View &view)
uint resource_handle_count() const
void warm_shader_specialization(PassMain &pass)
void submit_only(PassMain &pass, View &view)
void extract_object_attributes(ResourceHandleRange handle, const ObjectRef &ref, const GPUMaterial *material)
void ensure_visibility(View &view)
void extract_all_object_attributes(ResourceHandleRange handle, const ObjectRef &ref)
void update_handle_bounds(ResourceHandle handle, const ObjectRef &ref, float inflate_bounds=0.0f)
SwapChain< ObjectMatricesBuf, 2 > matrix_buf
Vector< gpu::Texture * > acquired_textures
void submit(PassSimple &pass, View &view)
ResourceHandleRange resource_handle(const ObjectRef &ref, float inflate_bounds=0.0f)
DeferredTexture & add_texture_deferred(blender::Image *image, blender::ImageUser *image_user, const bool use_tile_mapping)
DataDebugOutput data_debug()
bool is_active(const Object *active_object) const
#define instance_index
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
detail::Pass< command::DrawCommandBuf > PassSimple
detail::Pass< command::DrawMultiBuf > PassMain
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
PointerRNA RNA_id_pointer_create(ID *id)
MatBase< float, 4, 4 > float4x4
float shadow_terminator_normal_offset_get(const Object &object, const float4x4 &object_to_world)
unsigned int uint32_t
Definition BKE_text.h:81
@ OB_MODE_TEXTURE_PAINT
IDProperty * RNA_struct_idprops(PointerRNA *ptr, bool create)
draw::Manager * DRW_manager_get()
const GPUUniformAttrList * GPU_material_uniform_attributes(const GPUMaterial *material)
VecBase< float, 3 > float3
const ListBaseT< GPULayerAttr > * GPU_material_layer_attributes(const GPUMaterial *material)
bool DRW_object_is_in_edit_mode(const Object *ob)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bool sync(const draw::ObjectRef &ref, const GPUUniformAttr &attr, int instance_index)
float shadow_terminator_normal_offset
ListBaseT< GPUUniformAttr > list
ListBaseT< struct IDProperty > group
Definition DNA_ID.h:133
IDPropertyData data
Definition DNA_ID.h:157
eObject_TransFlag transflag