Eevee: GPU Material node graph optimization. #104536

Merged
Clément Foucault merged 4 commits from Jason-Fielder/blender:NodeGraphOptimization_v3 into main 2023-02-14 21:51:14 +01:00
Member

Certain material node graphs can be very expensive to run. This feature aims to produce secondary GPUPass shaders within a GPUMaterial which provide optimal runtime performance. Such optimizations include baking constant data into the shader source directly, allowing the compiler to propogate constants and perform aggressive optimization upfront.

As optimizations can result in reduction of shader editor and animation interactivity, optimized pass generation and compilation is deferred until all outstanding compilations have completed. Optimization is also delayed util a material has remained unmodified for a set period of time, to reduce excessive compilation. The original variant of the material shader is kept to maintain interactivity.

Also adding a new concept to gpu::Shader allowing assignment of a parent shader from which a shader can pull PSO descriptors and any required metadata for asynchronous shader cache warming. This enables fully asynchronous shader optimization, without runtime hitching, while also reducing runtime hitching for standard materials, by using PSO descriptors from default materials, ahead of rendering.

Further shader graph optimizations are likely also possible with this architecture. Certain scenes, such as Wanderer benefit significantly. Viewport performance for this scene is 2-3x faster on Apple-silicon based GPUs.

Authored by Apple: Michael Parkin-White

Ref T96261

Certain material node graphs can be very expensive to run. This feature aims to produce secondary GPUPass shaders within a GPUMaterial which provide optimal runtime performance. Such optimizations include baking constant data into the shader source directly, allowing the compiler to propogate constants and perform aggressive optimization upfront. As optimizations can result in reduction of shader editor and animation interactivity, optimized pass generation and compilation is deferred until all outstanding compilations have completed. Optimization is also delayed util a material has remained unmodified for a set period of time, to reduce excessive compilation. The original variant of the material shader is kept to maintain interactivity. Also adding a new concept to gpu::Shader allowing assignment of a parent shader from which a shader can pull PSO descriptors and any required metadata for asynchronous shader cache warming. This enables fully asynchronous shader optimization, without runtime hitching, while also reducing runtime hitching for standard materials, by using PSO descriptors from default materials, ahead of rendering. Further shader graph optimizations are likely also possible with this architecture. Certain scenes, such as Wanderer benefit significantly. Viewport performance for this scene is 2-3x faster on Apple-silicon based GPUs. Authored by Apple: Michael Parkin-White Ref T96261
Jason Fielder added 1 commit 2023-02-09 19:20:54 +01:00
Certain material node graphs can be very expensive to run. This feature aims to produce secondary GPUPass shaders within a GPUMaterial which provide optimal runtime performance. Such optimizations include baking constant data into the shader source directly, allowing the compiler to propogate constants and perform aggressive optimization upfront.

As optimizations can result in reduction of shader editor and animation interactivity, optimized pass generation and compilation is deferred until all outstanding compilations have completed. Optimization is also delayed util a material has remained unmodified for a set period of time, to reduce excessive compilation. The original variant of the material shader is kept to maintain interactivity.

Also adding a new concept to gpu::Shader allowing assignment of a parent shader from which a shader can pull PSO descriptors and any required metadata for asynchronous shader cache warming. This enables fully asynchronous shader optimization, without runtime hitching, while also reducing runtime hitching for standard materials, by using PSO descriptors from default materials, ahead of rendering.

Further shader graph optimizations are likely also possible with this architecture. Certain scenes, such as Wanderer benefit significantly. Viewport performance for this scene is 2-3x faster on Apple-silicon based GPUs.

Authored by Apple: Michael Parkin-White

Ref T96261
Jason Fielder requested review from Clément Foucault 2023-02-09 19:22:16 +01:00
Brecht Van Lommel added this to the Viewport & EEVEE project 2023-02-13 09:13:00 +01:00
Clément Foucault reviewed 2023-02-13 09:13:44 +01:00
@ -1394,2 +1393,2 @@
break;
case GPU_MAT_QUEUED:
case GPU_MAT_SUCCESS: {
/* Detemrine optimization status. */

typo

typo
fclem marked this conversation as resolved
Clément Foucault approved these changes 2023-02-13 22:01:37 +01:00
Clément Foucault left a comment
Member

I think the patch is clear and usage is all good.

I just would like to avoid printing stuff like Async compilation complete. Begin PSO warm USING DEFAULT MATERIAL. even for opengl, when it is clearly disabled.

I think the patch is clear and usage is all good. I just would like to avoid printing stuff like `Async compilation complete. Begin PSO warm USING DEFAULT MATERIAL.` even for opengl, when it is clearly disabled.
@ -1397,2 +1402,2 @@
mat = EEVEE_material_default_get(scene, ma, options);
break;
GPUMaterial *default_mat = EEVEE_material_default_get(scene, ma, options);
/* Mark pending material with its default material for future cache warming.*/

How does that work? Material can have different resources based on their flag (see EEVEE_material_bind_resources(à). Unless this is only for the vertex inputs and fragment outputs, these should be the same.

How does that work? Material **can** have different resources based on their flag (see `EEVEE_material_bind_resources(à`). Unless this is only for the vertex inputs and fragment outputs, these should be the same.
First-time contributor

The match rate of PSOs from the default material's to the regular materials is not 100%, however, from outputting the PSO descriptor hashes, there were matches with the corresponding default material more often than there were not.

Though yes, when resource inputs change their structure (e.g. differing geometry layout), these matches do not always line up.

But as you mention, for these cases, the PSO descriptor only requires vertex input data structure and fragment output format, along with a few other state properties such as blending, colour channel masking etc; however these usually appear to be consistent for a given material type in EEVEE, as those are generally dependent on the type of pass, more so than the material being rendered(?).

For Metal specifically, most state is dynamic, e.g. resource bindings, viewport/scissor, depth-stencil etc; so these parameters do not need to be part of the PSO descriptors.

I limited the cache warming for EEVEE materials from default materials to only one PSO, rather than all, as these additional ones may not end up being used, so this seemed the best bang-for-buck for reducing runtime stuttering, while keeping material compilation fast.

It's likely possible to be able to determine which PSOs will and wont be useful up front, and perhaps this code can evolve over time using contextual data from the materials to determine how effective cache warming will be. So perhaps based on certain flags, this step can be skipped. It still appears to be a net benefit for a good portion of materials however.

The match rate of PSOs from the default material's to the regular materials is not 100%, however, from outputting the PSO descriptor hashes, there were matches with the corresponding default material more often than there were not. Though yes, when resource inputs change their structure (e.g. differing geometry layout), these matches do not always line up. But as you mention, for these cases, the PSO descriptor only requires vertex input data structure and fragment output format, along with a few other state properties such as blending, colour channel masking etc; however these usually appear to be consistent for a given material type in EEVEE, as those are generally dependent on the type of pass, more so than the material being rendered(?). For Metal specifically, most state is dynamic, e.g. resource bindings, viewport/scissor, depth-stencil etc; so these parameters do not need to be part of the PSO descriptors. I limited the cache warming for EEVEE materials from default materials to only one PSO, rather than all, as these additional ones may not end up being used, so this seemed the best bang-for-buck for reducing runtime stuttering, while keeping material compilation fast. It's likely possible to be able to determine which PSOs will and wont be useful up front, and perhaps this code can evolve over time using contextual data from the materials to determine how effective cache warming will be. So perhaps based on certain flags, this step can be skipped. It still appears to be a net benefit for a good portion of materials however.

Thanks, I understand better now. But then the documentation about this is rather lacking. It is not stated that the parent material is just a template and that the actual final shader can actually differ from it (in terms of interface / pipeline state).

Thanks, I understand better now. But then the documentation about this is rather lacking. It is not stated that the parent material is just a template and that the actual final shader can actually differ from it (in terms of interface / pipeline state).
First-time contributor

Will improve the documentation.
Where would be the most appropriate location for this? In the header, or alongside the usage here?

Will improve the documentation. Where would be the most appropriate location for this? In the header, or alongside the usage here?

In the header.

In the header.
@ -275,2 +286,4 @@
bool GPU_material_optimization_ready(GPUMaterial *mat);
/**
* Store reference to default material for async PSO cache warming.

Maybe note what is the expected status of both parameters. I guess material can be in any state, whereas default_material should be compiled?

Maybe note what is the expected status of both parameters. I guess `material` can be in any state, whereas `default_material` should be compiled?
First-time contributor

Yep, can add this clarification.
The function wont fail if this is not set, async warming will just be skipped, but based on the control flow, there should never be a situation where this is not true.

Yep, can add this clarification. The function wont fail if this is not set, async warming will just be skipped, but based on the control flow, there should never be a situation where this is not true.
fclem marked this conversation as resolved
@ -109,0 +109,4 @@
/* Shader cache warming. Cache can be warmed using PSO descriptors
* from a specified parent shader. */
void GPU_shader_set_parent(GPUShader *shader, GPUShader *parent);
void GPU_shader_warm_cache(GPUShader *shader, int limit);

Document this function.

Document this function.
fclem marked this conversation as resolved

As a side note, I think the patch is in mergeable state, that's why I approved it. But would like to see the small fixes be done first.

As a side note, I think the patch is in mergeable state, that's why I approved it. But would like to see the small fixes be done first.
First-time contributor

Will address all feedback and resubmit, thanks!

Will address all feedback and resubmit, thanks!
Jason Fielder added 1 commit 2023-02-14 18:42:40 +01:00
Eevee: GPU Material node graph optimization.
Some checks failed
buildbot/vexp-code-patch-coordinator Build done.
b14c5cda89
Certain material node graphs can be very expensive to run. This feature aims to produce secondary GPUPass shaders within a GPUMaterial which provide optimal runtime performance. Such optimizations include baking constant data into the shader source directly, allowing the compiler to propogate constants and perform aggressive optimization upfront.

As optimizations can result in reduction of shader editor and animation interactivity, optimized pass generation and compilation is deferred until all outstanding compilations have completed. Optimization is also delayed util a material has remained unmodified for a set period of time, to reduce excessive compilation. The original variant of the material shader is kept to maintain interactivity.

Also adding a new concept to gpu::Shader allowing assignment of a parent shader from which a shader can pull PSO descriptors and any required metadata for asynchronous shader cache warming. This enables fully asynchronous shader optimization, without runtime hitching, while also reducing runtime hitching for standard materials, by using PSO descriptors from default materials, ahead of rendering.

Further shader graph optimizations are likely also possible with this architecture. Certain scenes, such as Wanderer benefit significantly. Viewport performance for this scene is 2-3x faster on Apple-silicon based GPUs.

PR Feedback Addressed.

Authored by Apple: Michael Parkin-White

Related to #96261
Clément Foucault approved these changes 2023-02-14 19:29:08 +01:00

@blender-bot build

@blender-bot build
Clément Foucault added 1 commit 2023-02-14 19:32:15 +01:00
Clément Foucault added 1 commit 2023-02-14 19:58:15 +01:00
Clément Foucault merged commit 7b9d1cb51f into main 2023-02-14 21:51:14 +01:00

Since this is committed the assert

BLI_assert(material != default_material);

in the function 755│ void GPU_material_set_default(GPUMaterial *material, GPUMaterial *default_material) (gpu_material.c ) fails for me.

Since this is committed the assert BLI_assert(material != default_material); in the function `755│ void GPU_material_set_default(GPUMaterial *material, GPUMaterial *default_material)` (gpu_material.c ) fails for me.
Jeroen Bakker added this to the 3.5 milestone 2023-02-15 08:23:51 +01:00
First-time contributor

Since this is committed the assert

BLI_assert(material != default_material);

in the function 755│ void GPU_material_set_default(GPUMaterial *material, GPUMaterial *default_material) (gpu_material.c ) fails for me.

Will see if I can repro and submit a fix. Thanks for raising.

> Since this is committed the assert > > BLI_assert(material != default_material); > > in the function `755│ void GPU_material_set_default(GPUMaterial *material, GPUMaterial *default_material)` (gpu_material.c ) fails for me. > > Will see if I can repro and submit a fix. Thanks for raising.

Here is a test file for the assert.

Here is a test file for the assert.
879 KiB

This still bugs quite hard for me. I don't even need a test file.

Just opening the default scene and switching to the shader workspace in a debug build will quit with:

BLI_assert failed: source/blender/gpu/intern/gpu_shader.cc:510, GPU_shader_set_parent(), at 'shader != parent'
This still bugs quite hard for me. I don't even need a test file. Just opening the default scene and switching to the shader workspace in a debug build will quit with: ``` BLI_assert failed: source/blender/gpu/intern/gpu_shader.cc:510, GPU_shader_set_parent(), at 'shader != parent' ```
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
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
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
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
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
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
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
4 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#104536
No description provided.