Cycles Procedural API and faster scene update #79174

Closed
opened 2020-07-22 21:57:26 +02:00 by Kévin Dietrich · 20 comments

This design task is for discussing ideas regarding procedural nodes and improvements to the scene update for faster interactive rendering.

Procedural API

A procedural is a node that can create geometry nodes in the scene before rendering. Such nodes can be used to import data from Alembic or USD files, or even a custom file format, without Cycles needing to explicitly know about those formats. The API for such a node can be as simple as deriving a base class and overriding a virtual function as suggested in D3089 or it could be more granular with various functions to know what they would create (e.g. number and types of nodes). Internally, the procedural nodes will use the Cycles API (#79131) to create or update data.

With procedurals the current approach to updating the scene data would need to be changed. Currently, scene updates start by iterating over the geometry nodes and their shaders before calling Scene::device_update(), but procedurals would need to be updated first.

Managing the procedurals, and the nodes that they create is bit unclear at the moment. When working on updating D3089, I run into issues with the Blender scene synchronization stage where Cycles objects not known by the synchronization system would be deleted between frames, or during viewport updates in interactive rendering. I did add some mechanisms to preserve those objects but it is not a nice approach I think.

Faster scene update

Scene update is too slow for real time applications or interactive rendering. Currently, Cycles is using a very conservative approach to updating scene data, especially for mesh nodes: if anything is changed, the entire device memory is freed and recreated. Ideally, if we only changed the vertices of a mesh, without using any displacement, we should be able to just copy the new data directly into the device memory, and recreate the BVH. For mesh objects, the device memory may also be recreated if displacement is used and has been applied, which leads to a double update of the device memory for every frame.

To perform the updates, we iterate over all of the objects and shaders in the scene in a certain order to account for dependencies between them. Because of the dependencies, we may iterate multiple times over the same container. For example, mesh objects are iterated over multiple times during a single update :

  • once for defining which shaders they used
  • once for updating their flags
  • once for generating their data
  • once for loading the images they use for displacement
  • once for generating displacement
  • once for handling volumetric data
  • once for generating their BVHs

And a few more times to gather metrics to allocate and copy their data to the device. Most of the times though, after the first frame has been rendered, these loops skip over the meshes that do not need an update.

To accelerate data processing, temporary multithreaded task pools are used in various places. For example, there exist dedicated task pools for loading 3d volume textures, images, or compiling SVM shaders.

To improve performance regarding scene updates, we could reduce the number of loops, parallelize object updates, and avoid unnecessary memory reallocations and data copying.

What needs to be known to granularize updates for geometry objects:

  • if vertices have changed (-> rebuild BVH, recompute normals and displacement if needed)
  • if vertex count has changed (-> recreate device memory, rebuild BVH, normals, displacement)
  • if an attribute has changed (-> what is the attribute affecting?)
  • if an image or a shader affecting displacement has changed
  • if displacement parameters have changed (-> recompute displacement, BVH, device_memory, normals)
  • if the triangles shaders changed (-> copy the new data to the memory)
  • other?

Those flags would be set automatically when modifying data through the API to free clients from setting them directly (#79131).

Ideas for improvements

  • a data oriented approach

Separate geometries in different arrays based on what needs to be done : geometries_to_displace, geometries_needing_bvh, gometries_needing_volumes, etc. And process arrays separately. This would apply to all the managers: each one would keep a list of managed objects that were modified.

  • a task system approach

This is the system that best fits a case where procedurals can recursively generate other procedurals.

Scene updates could be handled by a multithreaded task sheduler, replacing the various specific task pools currently used, and making the entire process multithreaded.

Tasks would be initially created for the objects (and the procedurals) which need to be updated. These would create other tasks, if needed, for the shaders that they use, and these in turn would create tasks for loading images, etc. This way, we would remove a lot of the manual iterations over containers to know what is needed. This essentially creates a dependency graph and processes it at the same time.

Tasks can be granular, and some node types can have multiple tasks. A mesh node could have one task for its general data update (computing normals, etc.) one for creating displacement data, and a final one to compute its BVH.

Dependencies between tasks can be noted: a displacement task requiring an image to be processed should wait for the image data to be available. The task scheduler can also segregate tasks by type and hand tasks to worker threads in an order similar to that presently used in Scene::device_update, i.e. image tasks would be processed before displacement tasks.

When all tasks are done, and data about what they did has been gathered, we can then at once decide whether to recreate the device memory or override it with new data and hand it over to the device for rendering.

This design task is for discussing ideas regarding procedural nodes and improvements to the scene update for faster interactive rendering. **Procedural API** A procedural is a node that can create geometry nodes in the scene before rendering. Such nodes can be used to import data from Alembic or USD files, or even a custom file format, without Cycles needing to explicitly know about those formats. The API for such a node can be as simple as deriving a base class and overriding a virtual function as suggested in [D3089](https://archive.blender.org/developer/D3089) or it could be more granular with various functions to know what they would create (e.g. number and types of nodes). Internally, the procedural nodes will use the Cycles API (#79131) to create or update data. With procedurals the current approach to updating the scene data would need to be changed. Currently, scene updates start by iterating over the geometry nodes and their shaders before calling `Scene::device_update()`, but procedurals would need to be updated first. Managing the procedurals, and the nodes that they create is bit unclear at the moment. When working on updating [D3089](https://archive.blender.org/developer/D3089), I run into issues with the Blender scene synchronization stage where Cycles objects not known by the synchronization system would be deleted between frames, or during viewport updates in interactive rendering. I did add some mechanisms to preserve those objects but it is not a nice approach I think. **Faster scene update** Scene update is too slow for real time applications or interactive rendering. Currently, Cycles is using a very conservative approach to updating scene data, especially for mesh nodes: if anything is changed, the entire device memory is freed and recreated. Ideally, if we only changed the vertices of a mesh, without using any displacement, we should be able to just copy the new data directly into the device memory, and recreate the BVH. For mesh objects, the device memory may also be recreated if displacement is used and has been applied, which leads to a double update of the device memory for every frame. To perform the updates, we iterate over all of the objects and shaders in the scene in a certain order to account for dependencies between them. Because of the dependencies, we may iterate multiple times over the same container. For example, mesh objects are iterated over multiple times during a single update : - once for defining which shaders they used - once for updating their flags - once for generating their data - once for loading the images they use for displacement - once for generating displacement - once for handling volumetric data - once for generating their BVHs And a few more times to gather metrics to allocate and copy their data to the device. Most of the times though, after the first frame has been rendered, these loops skip over the meshes that do not need an update. To accelerate data processing, temporary multithreaded task pools are used in various places. For example, there exist dedicated task pools for loading 3d volume textures, images, or compiling SVM shaders. To improve performance regarding scene updates, we could reduce the number of loops, parallelize object updates, and avoid unnecessary memory reallocations and data copying. What needs to be known to granularize updates for geometry objects: - if vertices have changed (-> rebuild BVH, recompute normals and displacement if needed) - if vertex count has changed (-> recreate device memory, rebuild BVH, normals, displacement) - if an attribute has changed (-> what is the attribute affecting?) - if an image or a shader affecting displacement has changed - if displacement parameters have changed (-> recompute displacement, BVH, device_memory, normals) - if the triangles shaders changed (-> copy the new data to the memory) - other? Those flags would be set automatically when modifying data through the API to free clients from setting them directly (#79131). **Ideas for improvements** - a data oriented approach Separate geometries in different arrays based on what needs to be done : geometries_to_displace, geometries_needing_bvh, gometries_needing_volumes, etc. And process arrays separately. This would apply to all the managers: each one would keep a list of managed objects that were modified. - a task system approach This is the system that best fits a case where procedurals can recursively generate other procedurals. Scene updates could be handled by a multithreaded task sheduler, replacing the various specific task pools currently used, and making the entire process multithreaded. Tasks would be initially created for the objects (and the procedurals) which need to be updated. These would create other tasks, if needed, for the shaders that they use, and these in turn would create tasks for loading images, etc. This way, we would remove a lot of the manual iterations over containers to know what is needed. This essentially creates a dependency graph and processes it at the same time. Tasks can be granular, and some node types can have multiple tasks. A mesh node could have one task for its general data update (computing normals, etc.) one for creating displacement data, and a final one to compute its BVH. Dependencies between tasks can be noted: a displacement task requiring an image to be processed should wait for the image data to be available. The task scheduler can also segregate tasks by type and hand tasks to worker threads in an order similar to that presently used in Scene::device_update, i.e. image tasks would be processed before displacement tasks. When all tasks are done, and data about what they did has been gathered, we can then at once decide whether to recreate the device memory or override it with new data and hand it over to the device for rendering.
Author
Member

Added subscribers: @kevindietrich, @fx

Added subscribers: @kevindietrich, @fx
Contributor

Added subscriber: @KenzieMac130

Added subscriber: @KenzieMac130

Added subscriber: @BartekMoniewski

Added subscriber: @BartekMoniewski

This issue was referenced by blender/cycles@50fe70268a

This issue was referenced by blender/cycles@50fe70268a0e63f3750e33a22e95345d9201db1b

This issue was referenced by edd1164575

This issue was referenced by edd1164575feefda103c73119a98cbd994e53141

This issue was referenced by blender/cycles@d4567df044

This issue was referenced by blender/cycles@d4567df044118c9d39125a28845344a8cf4ce88e

This issue was referenced by 527f8b32b3

This issue was referenced by 527f8b32b32187f754e5b176db6377736f9cb8ff

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

This issue was referenced by blender/cycles@588eb94148

This issue was referenced by blender/cycles@588eb94148e3ea62457b666496cbfc3919c44860

This issue was referenced by 31a620b942

This issue was referenced by 31a620b9420cab6292b0aa1ea21c9dd1cf70b8bc
Member

Added subscriber: @EAW

Added subscriber: @EAW

This issue was referenced by blender/cycles@0a013f4a0a

This issue was referenced by blender/cycles@0a013f4a0a93206b686461930ebfb82ca40d4764

This issue was referenced by bbe6d44928

This issue was referenced by bbe6d44928235cd4a5cfbeaf1a1de78ed861bb92

This issue was referenced by 2e67191c86

This issue was referenced by 2e67191c861f2cb148f05af116114e7332b8e789

This issue was referenced by b64f0fab06

This issue was referenced by b64f0fab068169a7c379be728aae8994eb893b18

Added subscriber: @SteffenD

Added subscriber: @SteffenD

This issue was referenced by 51862c8445

This issue was referenced by 51862c8445c8d5f573d1c86780db9e0972ef14dd

Added subscriber: @brecht

Added subscriber: @brecht

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Brecht Van Lommel self-assigned this 2021-08-20 16:40:47 +02:00

We can consider this project finished now, thanks @kevindietrich for the many optimizations. There will be a blog post on code.blender.org later summarizing the results.

Optimizations are of course always happening, and the Alembic procedural may be incrementally improved further, but this particular project is now finished.

We can consider this project finished now, thanks @kevindietrich for the many optimizations. There will be a blog post on code.blender.org later summarizing the results. Optimizations are of course always happening, and the Alembic procedural may be incrementally improved further, but this particular project is now finished.
Thomas Dinges added this to the 2.90 milestone 2023-02-08 16:26:15 +01:00
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 project
No Assignees
7 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#79174
No description provided.