WIP: GPU: Add batch shader compilation API and per backend ShaderCompiler #121471

Draft
Miguel Pozo wants to merge 23 commits from pragma37/blender:pull-parallel-shader-compilation into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

Allow GPU backends to support multithreaded async shader compilation by providing their own gpu::ShaderCompiler implementation.

Add new functions to the GPU_shader API to allow async shader compilation:

BatchHandle GPU_shader_batch_create_from_infos(blender::Span<GPUShaderCreateInfo *> infos);
bool GPU_shader_batch_is_ready(BatchHandle handle);
blender::Vector<GPUShader *> GPU_shader_batch_finalize(BatchHandle &handle);

And their equivalent to GPU_material:

BatchHandle GPU_material_batch_compile(blender::Span<GPUMaterial *> mats);
bool GPU_material_batch_is_ready(BatchHandle handle);
void GPU_material_batch_finalize(BatchHandle &handle, blender::Span<GPUMaterial *> mats);

Implement a GLShaderCompiler that uses GL_KHR/ARB_parallel_shader_compile.
To avoid shader compilation blocking Blender, there's only one global GLShaderCompiler that always runs from the main context at begin_frame, and yields when too much time has passed.

Current issues:

  • Since OpenGL shaders are compiled on begin_frame, the user code must request redraws until the compilation has finished.
  • The Nvidia driver seems to have issues when too many shaders are compiled at once. To avoid these issues, the material compiler requests shaders in smaller (32 items) batches. We might want to handle this from the ShaderCompiler itself in the future.
  • Since materials are compiled in batches, the number of remaining materials reported by EEVEE only changes once a full batch has been compiled.
  • This doesn't work for specialization constants (Probably ok?).

Material compile times

Scene Before (Next) After (Next) Before (Legacy) After (Legacy)
Mr Elephant 20s 22s 16s 7s
Barbershop 1m:55s 50s 2m:40s 1m:50s

The time improvements seem to be very case-dependent, and for the Mr Elephant scene in EEVEE Next it ends up being slightly slower.
But it brings a ~2x improvement in most of the tested cases.

Allow GPU backends to support multithreaded async shader compilation by providing their own `gpu::ShaderCompiler` implementation. Add new functions to the GPU_shader API to allow async shader compilation: ```cpp BatchHandle GPU_shader_batch_create_from_infos(blender::Span<GPUShaderCreateInfo *> infos); bool GPU_shader_batch_is_ready(BatchHandle handle); blender::Vector<GPUShader *> GPU_shader_batch_finalize(BatchHandle &handle); ``` And their equivalent to GPU_material: ```cpp BatchHandle GPU_material_batch_compile(blender::Span<GPUMaterial *> mats); bool GPU_material_batch_is_ready(BatchHandle handle); void GPU_material_batch_finalize(BatchHandle &handle, blender::Span<GPUMaterial *> mats); ``` Implement a `GLShaderCompiler` that uses `GL_KHR/ARB_parallel_shader_compile`. To avoid shader compilation blocking Blender, there's only one global `GLShaderCompiler` that always runs from the main context at `begin_frame`, and yields when too much time has passed. Current issues: - Since OpenGL shaders are compiled on `begin_frame`, the user code must request redraws until the compilation has finished. - The Nvidia driver seems to have issues when too many shaders are compiled at once. To avoid these issues, the material compiler requests shaders in smaller (32 items) batches. We might want to handle this from the ShaderCompiler itself in the future. - Since materials are compiled in batches, the number of remaining materials reported by EEVEE only changes once a full batch has been compiled. - This doesn't work for specialization constants (Probably ok?). --- ### Material compile times | Scene | Before (Next) | After (Next) | Before (Legacy) | After (Legacy) | | --- | --- | --- | --- | --- | | Mr Elephant | 20s | 22s | 16s | 7s | | Barbershop | 1m:55s | 50s | 2m:40s | 1m:50s | The time improvements seem to be very case-dependent, and for the Mr Elephant scene in EEVEE Next it ends up being slightly slower. But it brings a ~2x improvement in most of the tested cases.
Miguel Pozo added the
Module
EEVEE & Viewport
label 2024-05-06 12:21:24 +02:00
Miguel Pozo added 10 commits 2024-05-06 12:21:39 +02:00
Miguel Pozo changed title from (WIP) GPU: Add batch shader compilation API and per backend ShaderCompiler to WIP: GPU: Add batch shader compilation API and per backend ShaderCompiler 2024-05-06 12:22:24 +02:00
Miguel Pozo added 5 commits 2024-05-06 16:54:12 +02:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
0d0925d765
Add docstrings
Miguel Pozo requested review from Jeroen Bakker 2024-05-06 17:49:07 +02:00
Miguel Pozo requested review from Clément Foucault 2024-05-06 17:49:08 +02:00
Author
Member

@blender-bot build +gpu

@blender-bot build +gpu
Miguel Pozo added 1 commit 2024-05-06 18:24:29 +02:00
Member

Looking at the API, it would be useful for OpenGL. Metal and Vulkan have separated the frontend and backend compilation stages which will only perform the frontend stage when batching. The frontend stage is fairly light. I think that the backend stage (pipelines) should be implemented in each backend differently and outside the responsibility of the proposed API (as it now is).

For the material batch compilation it might be useful due to their special usecases. How would this API handle the 'optimized' version of a material. Would that be the same batch, or become a different batch.

Looking at the API, it would be useful for OpenGL. Metal and Vulkan have separated the frontend and backend compilation stages which will only perform the frontend stage when batching. The frontend stage is fairly light. I think that the backend stage (pipelines) should be implemented in each backend differently and outside the responsibility of the proposed API (as it now is). For the material batch compilation it might be useful due to their special usecases. How would this API handle the 'optimized' version of a material. Would that be the same batch, or become a different batch.
Jeroen Bakker reviewed 2024-05-07 09:21:31 +02:00
@ -1545,0 +1645,4 @@
bool GLShaderCompiler::batch_is_ready(BatchHandle handle)
{
mutex.lock();
Member

can use std::scoped_lock

can use `std::scoped_lock`
pragma37 marked this conversation as resolved
Jeroen Bakker reviewed 2024-05-07 09:25:30 +02:00
@ -1235,0 +1247,4 @@
if (GLContext::khr_parallel_shader_compile_support) {
glGetProgramiv(program_id, GL_COMPLETION_STATUS_KHR, &is_ready);
}
else if (GLContext::arb_parallel_shader_compile_support) {
Member

Both share the same status code (0x91B1) so might be moved into a single 'parallel_shader_compile_support` flag in context.

Both share the same status code (0x91B1) so might be moved into a single 'parallel_shader_compile_support` flag in context.
pragma37 marked this conversation as resolved
Miguel Pozo added 2 commits 2024-05-07 16:44:49 +02:00
Author
Member

Looking at the API, it would be useful for OpenGL. Metal and Vulkan have separated the frontend and backend compilation stages which will only perform the frontend stage when batching. The frontend stage is fairly light. I think that the backend stage (pipelines) should be implemented in each backend differently and outside the responsibility of the proposed API (as it now is).

So in "modern" GAPI backends, there's not any upfront pipeline compilation at all?
Won't compile times become an even worse problem with Vulkan+Nvidia then?

For the material batch compilation it might be useful due to their special usecases. How would this API handle the 'optimized' version of a material. Would that be the same batch, or become a different batch.

I've made a test for using unoptimized variants and I've implemented the optimization step as a separate batch. I think it makes more sense with the current API, since optimized materials use a separate Shader and not all materials do the optimization step.
a148563ac6
I didn't include it in this PR since I thought it was large enough already, and wouldn't make any practical change since the optimization step is never used on OpenGL.

> Looking at the API, it would be useful for OpenGL. Metal and Vulkan have separated the frontend and backend compilation stages which will only perform the frontend stage when batching. The frontend stage is fairly light. I think that the backend stage (pipelines) should be implemented in each backend differently and outside the responsibility of the proposed API (as it now is). So in "modern" GAPI backends, there's not any upfront pipeline compilation at all? Won't compile times become an even worse problem with Vulkan+Nvidia then? > For the material batch compilation it might be useful due to their special usecases. How would this API handle the 'optimized' version of a material. Would that be the same batch, or become a different batch. I've made a test for using unoptimized variants and I've implemented the optimization step as a separate batch. I think it makes more sense with the current API, since optimized materials use a separate Shader and not all materials do the optimization step. https://projects.blender.org/pragma37/blender/commit/a148563ac6b15fa6ae43589c118b86f650a3aef2 I didn't include it in this PR since I thought it was large enough already, and wouldn't make any practical change since the optimization step is never used on OpenGL.
Member

We have to find out. But on the other side we have more parameters to balance between performance and compilation times. LTO can be disabled, pipeline stages can be controlled individually (and reused between shaders). Caches can be stored to disk. Dynamic rendering reduces the amount of recompilations.

But yeah frontend compilation might become a file load operation in the end for the static shaders.

I’m not experienced with vulkan +Nvidia. So there is uncertainty in my answers

We have to find out. But on the other side we have more parameters to balance between performance and compilation times. LTO can be disabled, pipeline stages can be controlled individually (and reused between shaders). Caches can be stored to disk. Dynamic rendering reduces the amount of recompilations. But yeah frontend compilation might become a file load operation in the end for the static shaders. I’m not experienced with vulkan +Nvidia. So there is uncertainty in my answers
Miguel Pozo added a new dependency 2024-05-07 21:21:37 +02:00

I get a lot of leaked memory blocks. I removed the duplicated ones for clarity. Just opening an EEVEE demo file and closing it is enough to reproduce.

Error: Not freed memory blocks: 52, total unfreed memory 0.115364 MB
GLShader len: 1768 0x7f966fce3040
GLShaderInterface len: 256 0x7f96d197e140
name_buffer len: 6824 0x7f9670505438

Regarding backend compilation (PSO precaching), Michael had the idea of specifying the pipeline option in the create infos to prewarm the PSO cache with the most likely used variant(s). They are likely to be the same for each pipeline so it should be easy. But I wouldn't worry about this right now.

Performance

I gave it a spin but it doesn't look good on AMD + Mesa.
I only counted the materials compilation (time from first shaded frame). So static / pipeline shaders are not part of these timings.

EEVEE

Before After
Tree creature 13s 21s
Wanderer 8s 12s
Mr. Elephant 10s 18s
Rain Restaurant 6s 14s

Note that with the patch applied, I had to refresh the viewport for it to finish otherwise it could take more than 1min before the viewport refreshes.

EEVEE Next

Before After
Tree creature 16s 16s
Wanderer 7s 9s
Mr. Elephant 10s *15s
Rain Restaurant 7s 13s

*: broken, not sure if representative, could be worse in practice.

Not sure why EEVEE Next is not impacted by the same slowdown.

I get a lot of leaked memory blocks. I removed the duplicated ones for clarity. Just opening an EEVEE demo file and closing it is enough to reproduce. ``` Error: Not freed memory blocks: 52, total unfreed memory 0.115364 MB GLShader len: 1768 0x7f966fce3040 GLShaderInterface len: 256 0x7f96d197e140 name_buffer len: 6824 0x7f9670505438 ``` Regarding backend compilation (PSO precaching), Michael had the idea of specifying the pipeline option in the create infos to prewarm the PSO cache with the most likely used variant(s). They are likely to be the same for each pipeline so it should be easy. But I wouldn't worry about this right now. ### Performance I gave it a spin but it doesn't look good on AMD + Mesa. I only counted the materials compilation (time from first shaded frame). So static / pipeline shaders are not part of these timings. #### EEVEE | | Before | After | |------------------|--------|-------| | Tree creature | 13s | 21s | | Wanderer | 8s | 12s | | Mr. Elephant | 10s | 18s | | Rain Restaurant | 6s | 14s | Note that with the patch applied, I had to refresh the viewport for it to finish otherwise it could take more than 1min before the viewport refreshes. #### EEVEE Next | | Before | After | |------------------|--------|-------| | Tree creature | 16s | 16s | | Wanderer | 7s | 9s | | Mr. Elephant | 10s | \*15s | | Rain Restaurant | 7s | 13s | \*: broken, not sure if representative, could be worse in practice. Not sure why EEVEE Next is not impacted by the same slowdown.
Author
Member

I get a lot of leaked memory blocks

Maybe some batches are not being requested.

I gave it a spin but it doesn't look good on AMD + Mesa.

😣

Could you check different values for glMaxShaderCompilerThreads to see if that helps?
Also, could you try to disable the extensions to see if it also affects compile times negatively?
Ie. just remove this:

  GLContext::arb_parallel_shader_compile_support = epoxy_has_gl_extension(
      "GL_ARB_parallel_shader_compile");
  GLContext::khr_parallel_shader_compile_support = epoxy_has_gl_extension(
      "GL_KHR_parallel_shader_compile");
  GLContext::parallel_shader_compile_support = GLContext::khr_parallel_shader_compile_support ||
                                               GLContext::arb_parallel_shader_compile_support;

Note that with the patch applied, I had to refresh the viewport for it to finish otherwise it could take more than 1min before the viewport refreshes.

That's weird, EEVEE should be requesting refreshes.
Edit: Well, EEVEE-Next requests updates, for EEVEE Legacy that's expected, yes.

> I get a lot of leaked memory blocks Maybe some batches are not being requested. > I gave it a spin but it doesn't look good on AMD + Mesa. 😣 Could you check different values for `glMaxShaderCompilerThreads` to see if that helps? Also, could you try to disable the extensions to see if it also affects compile times negatively? Ie. just remove this: ``` GLContext::arb_parallel_shader_compile_support = epoxy_has_gl_extension( "GL_ARB_parallel_shader_compile"); GLContext::khr_parallel_shader_compile_support = epoxy_has_gl_extension( "GL_KHR_parallel_shader_compile"); GLContext::parallel_shader_compile_support = GLContext::khr_parallel_shader_compile_support || GLContext::arb_parallel_shader_compile_support; ``` > Note that with the patch applied, I had to refresh the viewport for it to finish otherwise it could take more than 1min before the viewport refreshes. That's weird, EEVEE should be requesting refreshes. Edit: Well, EEVEE-Next requests updates, for EEVEE Legacy that's expected, yes.
Miguel Pozo added 1 commit 2024-05-08 16:44:54 +02:00
Miguel Pozo added 2 commits 2024-05-08 21:18:10 +02:00
Miguel Pozo added 1 commit 2024-05-09 16:58:15 +02:00
Author
Member

I fixed the leaks and the refresh issue with EEVEE Legacy.

For reference, these are my compile times on EEVEE Next in other files:

Scene Before After
Rain Restaurant 10s 10s
Tree Creature 34s 18s
Wanderer 11s 5s

BTW, I've also made another PR to parallelize the engine static shaders compilation (#121542).
I have not marked it for review yet since it depends on this one, but I'm linking it here so you're aware of it.

I fixed the leaks and the refresh issue with EEVEE Legacy. For reference, these are my compile times on EEVEE Next in other files: | Scene | Before | After | | --- | --- | --- | | Rain Restaurant | 10s | 10s | | Tree Creature | 34s | 18s | | Wanderer | 11s | 5s | BTW, I've also made another PR to parallelize the engine static shaders compilation (#121542). I have not marked it for review yet since it depends on this one, but I'm linking it here so you're aware of it.

Even without the extension, I get the same slowdown. So I believe the extension is not correctly used on Mesa's OpengGL implementation (it is reported to support both GL_ARB_parallel_shader_compile and GL_KHR_parallel_shader_compile).

Even without the extension, I get the same slowdown. So I believe the extension is not correctly used on Mesa's OpengGL implementation (it is reported to support both `GL_ARB_parallel_shader_compile` and `GL_KHR_parallel_shader_compile`).
Miguel Pozo added 1 commit 2024-05-09 19:18:28 +02:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
3b41b7b99e
Disable parallel compilation on Mesa drivers
Author
Member

@blender-bot package

@blender-bot package
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR121471) when ready.
First-time contributor

Windows10 / RTX2060M / Studio Driver 552.22 (Apr 16 2024)

  • master is build from 09.05
  • init is first frame when text "Compiling shaders (x remaining)" appears
Engine Scene init (master) Total (master) First Frame (Build) Total (Build)
EEVEE Legacy Barbershop 0:06 5:34 0:04 2:04
EEVEE Legacy 2.93 Dinosaurs 0:03 0:23 0:03 0:11
EEVEE Barbershop 1:00 5:04 1:00 3:00
EEVEE 2.93 Dinosaurs 0:52 1:22 1:21 1:22
Windows10 / RTX2060M / Studio Driver 552.22 (Apr 16 2024) * master is build from 09.05 * init is first frame when text "Compiling shaders (x remaining)" appears | Engine| Scene | init (master) | Total (master) | First Frame (Build) | Total (Build) | | -------- | -------- | -------- |-------- | -------- |-------- | |EEVEE Legacy| Barbershop | 0:06 | 5:34 | 0:04 | 2:04 | | EEVEE Legacy| 2.93 Dinosaurs| 0:03 | 0:23 | 0:03 | 0:11 | | EEVEE | Barbershop | 1:00 | 5:04 | 1:00 | 3:00 | | EEVEE | 2.93 Dinosaurs| 0:52 | 1:22 | 1:21 | 1:22 |
Some checks failed
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
This pull request is marked as a work in progress.
This branch is out-of-date with the base branch

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u pull-parallel-shader-compilation:pragma37-pull-parallel-shader-compilation
git checkout pragma37-pull-parallel-shader-compilation
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
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Reference: blender/blender#121471
No description provided.