Blender V5.3
draw_manager.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_userdef_types.h"
10
11#include "BKE_image.hh"
12#include "BKE_image_gpu.hh"
13#include "BKE_paint.hh"
14#include "BKE_paint_bvh.hh"
15
16#include "BLI_math_base_c.hh"
17#include "BLI_task.hh"
18
19#include "GPU_compute.hh"
20#include "GPU_shader.hh"
21#include "GPU_texture.hh"
22
23#include "draw_common.hh"
25#include "draw_debug.hh"
26#include "draw_defines.hh"
27#include "draw_manager.hh"
28#include "draw_pass.hh"
29#include "draw_shader.hh"
30
31namespace blender::draw {
32
33std::atomic<uint32_t> Manager::global_sync_counter_ = 1;
34
36{
38 /* Decrease refcount and free if 0. */
40 }
41}
42
43void Manager::begin_sync(Object *object_active)
44{
45 /* Add 2 to always have a non-null number even in case of overflow. */
46 sync_counter_ = (global_sync_counter_ += 2);
47
48 matrix_buf.swap();
49 bounds_buf.swap();
50 infos_buf.swap();
51
52 matrix_buf.current().trim_to_next_power_of_2(resource_len_);
53 bounds_buf.current().trim_to_next_power_of_2(resource_len_);
54 infos_buf.current().trim_to_next_power_of_2(resource_len_);
56
57 /* TODO: This means the reference is kept until further redraw or manager tear-down. Instead,
58 * they should be released after each draw loop. But for now, mimics old DRW behavior. */
60 /* Decrease refcount and free if 0. */
62 }
63
64 acquired_textures.clear();
65 deferred_textures_.clear();
66 layer_attributes.clear();
67
68/* For some reason, if this uninitialized data pattern was enabled (ie release asserts enabled),
69 * The viewport just gives up rendering objects on ARM64 devices. Possibly Mesa GLOn12-related. */
70#if !defined(NDEBUG) && !defined(_M_ARM64)
71 /* Detect uninitialized data. */
72 memset(matrix_buf.current().data(),
73 0xF0,
74 matrix_buf.current().size() * sizeof(*matrix_buf.current().data()));
75 memset(bounds_buf.current().data(),
76 0xF0,
77 matrix_buf.current().size() * sizeof(*bounds_buf.current().data()));
78 memset(infos_buf.current().data(),
79 0xF0,
80 matrix_buf.current().size() * sizeof(*infos_buf.current().data()));
81#endif
82 resource_len_ = 0;
83 attribute_len_ = 0;
84 /* TODO(fclem): Resize buffers if too big, but with an hysteresis threshold. */
85
86 this->object_active = object_active;
87
88 /* Init the 0 resource. */
90}
91
92void Manager::sync_layer_attributes()
93{
94 /* Sort the attribute IDs - the shaders use binary search. */
95 Vector<uint32_t> id_list;
96
97 id_list.reserve(layer_attributes.size());
98
99 for (uint32_t id : layer_attributes.keys()) {
100 id_list.append(id);
101 }
102
103 std::ranges::sort(id_list);
104
105 /* Look up the attributes. */
106 int count = 0, size = layer_attributes_buf.end() - layer_attributes_buf.begin();
107
108 for (uint32_t id : id_list) {
109 if (layer_attributes_buf[count].sync(
110 drw_get().scene, drw_get().view_layer, layer_attributes.lookup(id)))
111 {
112 /* Check if the buffer is full. */
113 if (++count == size) {
114 break;
115 }
116 }
117 }
118
119 layer_attributes_buf[0].buffer_length = count;
120}
121
123{
124 if (deferred_textures_.is_empty()) {
125 return;
126 }
127
128 GPU_debug_group_begin("Texture Loading");
129
130 /* Load files from disk in a multithreaded manner. Allow better parallelism. */
131 threading::parallel_for(deferred_textures_.index_range(), 1, [&](const IndexRange range) {
132 for (const int i : range) {
133 DeferredTexture &deferred = *deferred_textures_[i];
134 BKE_image_get_tile(deferred.image, 0);
135 threading::isolate_task([&]() {
136 ImBuf *imbuf = BKE_image_acquire_ibuf(deferred.image, deferred.image_user, nullptr);
137 BKE_image_release_ibuf(deferred.image, imbuf, nullptr);
138 });
139 }
140 });
141
142 /* Avoid any leftover bind before GPU texture creation which could cause assert
143 * about missing specialization constants. */
145
146 /* Upload to the GPU (create gpu::Texture). This part still requires a valid GPU context and
147 * is not easily parallelized. */
148 for (std::unique_ptr<DeferredTexture> &deferred : deferred_textures_) {
150 deferred->image, deferred->image_user, deferred->use_tile_mapping, false);
151 deferred->texture = textures.texture;
152 deferred->tile_mapping = textures.tile_mapping;
153 hold_texture(textures.texture);
154 hold_texture(textures.tile_mapping);
155 }
156
158}
159
161{
162 GPU_debug_group_begin("Manager.end_sync");
163
165
166 sync_layer_attributes();
167
168 matrix_buf.current().push_update();
169 bounds_buf.current().push_update();
170 infos_buf.current().push_update();
173
174 /* Useful for debugging the following resource finalize. But will trigger the drawing of the GPU
175 * debug draw/print buffers for every frame. Not nice for performance. */
176 // debug_bind();
177
179
180 /* Dispatch compute to finalize the resources on GPU. Save a bit of CPU time. */
181 uint thread_groups = divide_ceil_u(resource_len_, DRW_FINALIZE_GROUP_SIZE);
184 GPU_shader_uniform_1i(shader, "resource_len", resource_len_);
188 GPU_compute_dispatch(shader, thread_groups, 1, 1);
190
192
194}
195
197{
199 if (gpu_buf == nullptr) {
200 return;
201 }
203#ifndef DISABLE_DEBUG_SHADER_PRINT_BARRIER
204 /* Add a barrier to allow multiple shader writing to the same buffer. */
206#endif
207}
208
216
217uint64_t Manager::fingerprint_get()
218{
219 /* Covers new sync cycle, added resources and different #Manager. */
220 return sync_counter_ | (uint64_t(resource_len_) << 32);
221}
222
224{
225 if (ref.sculpt_handle_.is_valid()) {
226 return ref.sculpt_handle_;
227 }
230 const float3 center = math::midpoint(bounds.min, bounds.max);
231 const float3 half_extent = bounds.max - center;
232 /* WORKAROUND: Instead of breaking const correctness everywhere, we only break it for this. */
233 const_cast<ObjectRef &>(ref).sculpt_handle_ = resource_handle(
234 ref, nullptr, &center, &half_extent);
235 return ref.sculpt_handle_;
236}
237
239{
240 bool freeze_culling = (USER_DEVELOPER_TOOL_TEST(&U, use_viewport_debug) && drw_get().v3d &&
242
243 BLI_assert_msg(view.manager_fingerprint_ != this->fingerprint_get(),
244 "Resources did not changed, no need to update");
245
246 view.manager_fingerprint_ = this->fingerprint_get();
247
248 view.bind();
250 bounds_buf.current(), infos_buf.current(), resource_len_, freeze_culling);
251}
252
254{
255 if (view.manager_fingerprint_ != this->fingerprint_get()) {
256 compute_visibility(view);
257 }
258}
259
261{
262 if (pass.is_empty()) {
263 return;
264 }
265
266 BLI_assert_msg((pass.manager_fingerprint_ != this->fingerprint_get()) ||
267 (pass.view_fingerprint_ != view.fingerprint_get()),
268 "Resources and view did not changed no need to update");
269 BLI_assert_msg((view.manager_fingerprint_ == this->fingerprint_get()) &&
270 (view.fingerprint_get() != 0),
271 "Resources or view changed, but compute_visibility was not called");
272
273 pass.manager_fingerprint_ = this->fingerprint_get();
275
276 pass.draw_commands_buf_.generate_commands(pass.headers_,
277 pass.commands_,
280 view.view_len_,
281 pass.use_custom_ids);
282}
283
285{
286 if (pass.is_empty()) {
287 return;
288 }
289
290 pass.sort();
291 generate_commands(static_cast<PassMain &>(pass), view);
292}
293
295{
296 if (pass.is_empty()) {
297 return;
298 }
299
300 BLI_assert_msg(pass.manager_fingerprint_ != this->fingerprint_get(),
301 "Resources did not changed since last generate_command, no need to update");
302 pass.manager_fingerprint_ = this->fingerprint_get();
303
304 pass.draw_commands_buf_.generate_commands(pass.headers_, pass.commands_, pass.sub_passes_);
305}
306
308{
309 if (pass.is_empty()) {
310 return;
311 }
314}
315
317{
318 if (pass.is_empty()) {
319 return;
320 }
323}
324
326{
327 if (pass.is_empty()) {
328 return;
329 }
330
331 BLI_assert_msg(view.manager_fingerprint_ != 0, "compute_visibility was not called on this view");
332 BLI_assert_msg(view.manager_fingerprint_ == this->fingerprint_get(),
333 "Resources changed since last compute_visibility");
334 BLI_assert_msg(pass.manager_fingerprint_ != 0, "generate_command was not called on this pass");
335 BLI_assert_msg(pass.manager_fingerprint_ == this->fingerprint_get(),
336 "Resources changed since last generate_command");
337 /* The function generate_commands needs to be called for each view this pass is going to be
338 * submitted with. This is because the commands are stored inside the pass and not per view. */
340 "View have changed since last generate_commands or "
341 "submitting with a different view");
342
343 debug_bind();
344
346 state.inverted_view = view.is_inverted();
347
348 view.bind();
349 pass.draw_commands_buf_.bind(state);
350
352
353 pass.submit(state);
354
355 state.cleanup();
356}
357
358void Manager::submit(PassMain &pass, View &view)
359{
360 if (pass.is_empty()) {
361 return;
362 }
363
364 if (view.manager_fingerprint_ != this->fingerprint_get()) {
365 compute_visibility(view);
366 }
367
368 if (pass.manager_fingerprint_ != this->fingerprint_get() ||
369 pass.view_fingerprint_ != view.fingerprint_get())
370 {
371 generate_commands(pass, view);
372 }
373
374 this->submit_only(pass, view);
375}
376
378{
379 if (pass.is_empty()) {
380 return;
381 }
382
383 pass.sort();
384
385 this->submit(static_cast<PassMain &>(pass), view);
386}
387
388void Manager::submit(PassSimple &pass, bool inverted_view)
389{
390 if (pass.is_empty()) {
391 return;
392 }
393
394 if (!pass.has_generated_commands()) {
395 generate_commands(pass);
396 }
397
398 debug_bind();
399
401 state.inverted_view = inverted_view;
402
403 pass.draw_commands_buf_.bind(state);
404
406
407 pass.submit(state);
408
409 state.cleanup();
410}
411
413{
414 if (pass.is_empty()) {
415 return;
416 }
417
418 debug_bind();
419
420 view.bind();
421
422 this->submit(pass, view.is_inverted());
423}
424
426{
427 submit(pass, view);
428
429 pass.draw_commands_buf_.resource_id_buf_.read();
430
432 output.resource_id = {pass.draw_commands_buf_.resource_id_buf_.data(),
433 pass.draw_commands_buf_.resource_id_count_};
434 /* There is no visibility data for PassSimple. */
435 output.visibility = {static_cast<uint *>(view.get_visibility_buffer().data()), 0};
436 return output;
437}
438
440{
441 submit(pass, view);
442
444
445 pass.draw_commands_buf_.resource_id_buf_.read();
447
449 output.resource_id = {pass.draw_commands_buf_.resource_id_buf_.data(),
450 pass.draw_commands_buf_.resource_id_count_};
451 output.visibility = {static_cast<uint *>(view.get_visibility_buffer().data()),
452 divide_ceil_u(resource_len_, 32)};
453 return output;
454}
455
457{
458 matrix_buf.current().read();
459 bounds_buf.current().read();
460 infos_buf.current().read();
461
463 output.matrices = {matrix_buf.current().data(), resource_len_};
464 output.bounds = {bounds_buf.current().data(), resource_len_};
465 output.infos = {infos_buf.current().data(), resource_len_};
466 return output;
467}
468
469} // namespace blender::draw
A BVH for high poly meshes.
#define USER_DEVELOPER_TOOL_TEST(userdef, member)
#define BLI_assert_msg(a, msg)
#define output
void append(const T &value)
void reserve(const int64_t min_capacity)
gpu::StorageBuf * gpu_draw_buf_get()
Definition draw_debug.cc:59
static DebugDraw & get()
LayerAttributeBuf layer_attributes_buf
void begin_sync(Object *object_active=nullptr)
SwapChain< ObjectBoundsBuf, 2 > bounds_buf
SubmitDebugOutput submit_debug(PassSimple &pass, View &view)
ResourceHandleRange unique_handle_for_sculpt(const ObjectRef &ref)
void generate_commands(PassMain &pass, View &view)
SwapChain< ObjectInfosBuf, 2 > infos_buf
ObjectAttributeBuf attributes_buf
Map< uint32_t, GPULayerAttr > layer_attributes
void compute_visibility(View &view)
void warm_shader_specialization(PassMain &pass)
void submit_only(PassMain &pass, View &view)
void ensure_visibility(View &view)
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)
DataDebugOutput data_debug()
void trim_to_next_power_of_2(int64_t required_size)
bool is_inverted(int view_id=0) const
Definition draw_view.hh:99
virtual void compute_visibility(ObjectBoundsBuf &bounds, ObjectInfosBuf &infos, uint resource_len, bool debug_freeze)
Definition draw_view.cc:257
virtual VisibilityBuf & get_visibility_buffer()
Definition draw_view.cc:312
uint64_t manager_fingerprint_
Definition draw_view.hh:57
int visibility_word_per_draw() const
Definition draw_view.hh:178
uint64_t fingerprint_get() const
Definition draw_view.hh:261
Vector< command::Header, 0 > headers_
Definition draw_pass.hh:139
void warm_shader_specialization(command::RecordingState &state) const
Definition draw_pass.hh:710
SubPassVector< PassBase< DrawCommandBufType > > & sub_passes_
Definition draw_pass.hh:145
void submit(command::RecordingState &state) const
Definition draw_pass.hh:764
Vector< command::Undetermined, 0 > commands_
Definition draw_pass.hh:141
DrawCommandBufType & draw_commands_buf_
Definition draw_pass.hh:143
Simple API to draw debug shapes and log in the viewport.
#define DRW_FINALIZE_GROUP_SIZE
#define DRW_DEBUG_DRAW_SLOT
#define DRW_OBJ_INFOS_SLOT
#define DRW_OBJ_ATTR_SLOT
#define DRW_OBJ_MAT_SLOT
#define DRW_LAYER_ATTR_UBO_SLOT
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
const int state
pbvh::Tree * pbvh_get(Object &object)
Bounds< float3 > bounds_get(const Tree &pbvh)
Definition pbvh.cc:1590
detail::Pass< command::DrawCommandBuf > PassSimple
detail::Pass< command::DrawMultiBuf > PassMain
T midpoint(const T &a, const T &b)
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
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint
Definition BKE_text.h:55
ImageGPUTextures BKE_image_acquire_gpu_material_texture(Image *image, ImageUser *iuser, const bool use_tile_mapping, const bool try_only)
Definition image_gpu.cc:762
DRWContext & drw_get()
void GPU_compute_dispatch(gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const gpu::shader::SpecializationConstants *constants_state=nullptr)
gpu::Shader * DRW_shader_draw_resource_finalize_get()
int GPU_shader_get_ssbo_binding(gpu::Shader *shader, const char *name)
unsigned int uint32_t
Definition BKE_text.h:81
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
BMesh const char void * data
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
@ GPU_BARRIER_BUFFER_UPDATE
Definition GPU_state.hh:59
@ GPU_BARRIER_SHADER_STORAGE
Definition GPU_state.hh:51
void GPU_storagebuf_bind(gpu::StorageBuf *ssbo, int slot)
void DRW_submission_end()
void GPU_uniformbuf_bind(gpu::UniformBuf *ubo, int slot)
void GPU_debug_group_end()
Definition gpu_debug.cc:34
void GPU_texture_free(gpu::Texture *texture)
@ V3D_DEBUG_FREEZE_CULLING
MINLINE uint divide_ceil_u(uint a, uint b)
void DRW_submission_start()
VecBase< float, 3 > float3
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23