Improve per-object overhead in the Draw module #113771

Open
opened 2023-10-16 13:03:19 +02:00 by Miguel Pozo · 5 comments
Member

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:

275026427-53a76b84-015d-4f8b-8c5c-d8c58addaa3c

I think these are the main potential areas for optimization:

  • Redundant operations for each DupliObject.
  • Per DupliObject memory allocations/deallocations.
  • CPU to GPU bandwidth.
  • Lack of caching between samples/frames.

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 per DupliObject.
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 cached ObjectBounds 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 generation

Around 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 generated Object 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, the MainPass and a new type of special per-object Storage Buffer, so engines don't have to manage this directly.

## 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: ![275026427-53a76b84-015d-4f8b-8c5c-d8c58addaa3c](/attachments/2f6d8017-9dc9-47ee-bc78-910a70ead926) I think these are the main potential areas for optimization: * Redundant operations for each `DupliObject`. * Per `DupliObject` memory allocations/deallocations. * CPU to GPU bandwidth. * Lack of caching between samples/frames. 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 per `DupliObject`. 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 cached `ObjectBounds` 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` generation Around 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 generated `Object` 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`, the `MainPass` and a new type of special per-object Storage Buffer, so engines don't have to manage this directly.
Miguel Pozo added the
Interest
Dependency Graph
Module
EEVEE & Viewport
Type
Design
labels 2023-10-16 13:03:20 +02:00
Miguel Pozo added this to the EEVEE & Viewport project 2023-10-16 13:03:22 +02:00

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.

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.
Author
Member

@HooglyBoogly also mentioned replacing DupliObjects with bke::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.

@HooglyBoogly also mentioned replacing `DupliObjects` with `bke::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.
Member
  1. Optimize ObjectBounds::sync

Reading this more closely, it should be handled by #113465. A next step to return Bounds<float3> will probably help too.

  1. Upload less data per object

These changes seems straightforward and don't have many design implications, that sounds good!

  1. Optimize DupliObject generation

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 the DupliObject generation has more to do with the "iteration for render engine" than the actual evaluation

If it's evaluated as part of the depsgraph and stored on objects

This 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) the DupliObject list.

>1. Optimize ObjectBounds::sync Reading this more closely, it should be handled by #113465. A next step to return `Bounds<float3>` will probably help too. >2. Upload less data per object These changes seems straightforward and don't have many design implications, that sounds good! >3. Optimize DupliObject generation 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 the `DupliObject` generation has more to do with the "iteration for render engine" than the actual evaluation >If it's evaluated as part of the depsgraph and stored on objects This 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) the `DupliObject` list.
Author
Member

Reading this more closely, it should be handled by #113465.

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.

Personally I think adding a workaround in this area will make the situation more complicated and make it harder to change in the future.

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.

> Reading this more closely, it should be handled by #113465. 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. > Personally I think adding a workaround in this area will make the situation more complicated and make it harder to change in the future. 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.
Member

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.

Right, other object types should also cache the bounds on object.data, then they could use the same change.

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.

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.

> 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. Right, other object types should also cache the bounds on `object.data`, then they could use the same change. >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. 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.
Sign in to join this conversation.
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 Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#113771
No description provided.