Improve per-object overhead in the Draw module #113771
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#113771
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Introduction
At the moment, large scenes in Blender are usually CPU-bound.
For testing purposes, I've made a simple scene that instances a triangle 100k times using geometry nodes.
I've profiled it in Workbench mode with all optional features disabled.
The triangles are not even shown in the screen so all the draw calls should be culled-out.
This is what the performance profile looks like:
I think these are the main potential areas for optimization:
DupliObject
.DupliObject
memory allocations/deallocations.Here are some possible options for improving per-object overhead in viewport rendering. They're mostly orthogonal to each other and go from the most simple local/isolated improvements to more structural/wider refactors.
Update: Related to-do with similar ideas: #92963
1. Optimize ObjectBounds::sync
We make one
BKE_mesh_bounbox_get()
call perDupliObject
.Since the
runtime.bb
reference is removed when creating the dupliobject, calling this function does way more work than it has to, taking a ~7% of the total frame time (~10% if you count the time it takes to deallocate the bounding boxes).Fixing this can be as simple as storing the
Object.data
pointer from the last synced handle and restoring a cachedObjectBounds
if it's the same.Update: #96968 and specifically #113465 should heavily improve this issue.
2. Upload less data per object
Over 9% (and more on lower-end hardware) of the frame time is spent in uploading buffer data inside
Manager::end_sync
.We could improve this by simply compressing the data we send to the GPU:
Change the object 4x4 matrix to a 3x4 matrix and compute the inverse object matrix in
draw_resource_finalize
.Upload only the bounds min/max, and compute the corners and the sphere in
draw_resource_finalize
.Move
ObjectInfos
to engine-specific storage buffers, since none of the properties are used across all engines (OBJECT_NEGATIVE_SCALE
could be uploaded along the min/max values).This would reduce the uploaded data for each object from 272 bytes to just 80.
3. Optimize
DupliObject
generationAround a third of the frame time is spent iterating the depsgraph.
A good chunk of this time comes from memory allocation/deallocation when generating
DupliObjects
and computing data we may not need.A simple solution may be to have an alternative function to
DEG_OBJECT_ITER_BEGIN/END
that takes a per-object callback instead. So each dupli generatedObject
could be allocated on the stack and sent to the callback function immediately, instead of having to add it to a linked list for later iteration.A more time consuming, but potentially better alternative could be to update the engines
sync_object
function to simply receive the original object and a list of per-dupli data.That would allow avoiding redundant work, like
GPUBatch
requests (7% of frame time) or material setups, in a more natural way.4. Avoid unnecessary re-syncs
While a more granular way of updating objects would be more complex (more on that later), we could "easily" skip re-syncs between samples of the same frame, and between frames as long as there has not been any scene update.
This wont help with playback or editing performance, but it could provide a huge speed boost for viewport navigation and EEVEE super-sampling.
We can simply skip the Manager and engines init/sync functions when possible, and update the engines so they handle per-sample update logic directly in their draw function, just like they already do for image rendering.
5. Share handles across engines/viewports
Right now we create one handle for each engine instance. So one per viewport and 2x times that once Overlay Next is ready.
We could avoid that overhead by creating the handles on the draw manager side and passing them to the engines sync function.
6. Static/Dynamic object pools
Ideally we should completely skip re-sync for objects that have not been updated.
This is the most complex one, but the good news is that one of the hardest part (indirect drawing) is already done.
This could be achieved by making a distinction between dynamic and static objects.
New/updated objects would always be dynamic, and if they have not changed after N frames, they're are moved to the static pool.
The dynamic object pool is synced every frame, while the static one is only synced when it changes.
The pool logic could be built into the
Manager
, theMainPass
and a new type of special per-object Storage Buffer, so engines don't have to manage this directly.There probably exists an issue related to this already. But an idea we have discussed in the past is to generate the dupli objects as part of dependency graph evaluation, so that it is only generated once for static objects.
It can probably stored the same way as geometry node instances, so that there is no duplication there. Memory usage is a concern, so certain data could be stored only when needed (texture coordinates), stored more compactly (matrices), or computed on demand (random hash).
If it's evaluated as part of the depsgraph and stored on objects, it also means that depsgraph tagging can be used to figure out which instances have changed since the last update.
@HooglyBoogly also mentioned replacing
DupliObjects
withbke::Instances
on the chat.My only issue with those approaches is that I don't know the depsgraph well enough to understand their full implications, and they seem more like long-term goals beyond what I could confidently implement myself right now.
Avoiding heap allocations at depsgraph iteration, on the other hand, is a fairly small change that could be easily implemented, for a big performance gain.
So, I think short-term optimizations like this have value on their own, as long as they don't make the code worse, or future improvements harder.
Reading this more closely, it should be handled by #113465. A next step to return
Bounds<float3>
will probably help too.These changes seems straightforward and don't have many design implications, that sounds good!
Personally I think adding a workaround in this area will make the situation more complicated and make it harder to change in the future.
IMO processing data that's much more like
bke::Instances
is a much better approach. Though it is a larger change, I know there are others motivated to see this area improve. AFAIK that wouldn't impact the depsgraph too much, since theDupliObject
generation has more to do with the "iteration for render engine" than the actual evaluationThis is indeed already the case with the
GeometrySet
instances component.The remaining work to be done is to change the collection and old dupli-vert instancing to create
bke::Instances
instead of (or in addition to) theDupliObject
list.Will check, but yes, that should solve it for the most part I think. 🙂
One issue I see is that it doesn't handle all object types, although it covers the ones that are more likely to be heavily instanced.
I'll make a proof of concept, but what I have in mind would hardly make any kind of future changes harder.
I agree that there may be better alternatives, but since those are not something we can implement right away, I think it's still worth improving something that can eat a third of the frame time.
Right, other object types should also cache the bounds on
object.data
, then they could use the same change.Fair enough! It's just nice to agree loosely on the proper long term approach so we can make sure we're working towards a shared goal.