OpenGL: Specialization Constants #116926

Merged
Jeroen Bakker merged 23 commits from Jeroen-Bakker/blender:opengl/specialization-constants into main 2024-01-12 14:28:58 +01:00
Member

This PR adds support for specialization constants for the OpenGL
backend. The minimum OpenGL version we are targetting doesn't
have native support for specialization constants. We simulate this
by keeping track of shader programs for each set of specialization
constants that are being used.

Specialization constants can be used to reduce shader complexity
and improve performance as less registry and/or spilling is done.

This requires the ability to recompile GLShaders. In order to do this
we need to keep track of the sources that are used when the shader
was compiled. For static sources we only store references
(GLSource::source_ref), for dynamically generated sources we keep
a copy of the source (GLSource::source).

When recompiling the shader GLSL source-code is generated for the
constants stored in Shader::constants. When compiling the previous
GLSource that contains specialization constants is then replaced
by the new version.

classDiagram
    class GLShader {
        vertex_sources: GLSources
        fragment_sources: GLSources
        geometry_sources: GLSources
        compute_sources: GLSources
    }
    class GLSource {
        source: std::string
        source_ref: blender:StringRefNull
    }
    class GLSources {
    }
    class GLProgram {
        program_id: GLuint
        vert_shader: GLuint
        geom_shader: GLuint
        frag_shader: GLuint
        compute_shader: GLuint
    }

    GLShader *--> GLSources
    GLShader*--> GLSources
    GLShader *--> GLSources
    GLShader *--> GLSources
    GLSources *--> GLSource: entries
    GLShader *-->GLProgram: program_cache
    GLShader *..>GLProgram: program_active
This PR adds support for specialization constants for the OpenGL backend. The minimum OpenGL version we are targetting doesn't have native support for specialization constants. We simulate this by keeping track of shader programs for each set of specialization constants that are being used. Specialization constants can be used to reduce shader complexity and improve performance as less registry and/or spilling is done. This requires the ability to recompile GLShaders. In order to do this we need to keep track of the sources that are used when the shader was compiled. For static sources we only store references (`GLSource::source_ref`), for dynamically generated sources we keep a copy of the source (`GLSource::source`). When recompiling the shader GLSL source-code is generated for the constants stored in `Shader::constants`. When compiling the previous GLSource that contains specialization constants is then replaced by the new version. ```mermaid classDiagram class GLShader { vertex_sources: GLSources fragment_sources: GLSources geometry_sources: GLSources compute_sources: GLSources } class GLSource { source: std::string source_ref: blender:StringRefNull } class GLSources { } class GLProgram { program_id: GLuint vert_shader: GLuint geom_shader: GLuint frag_shader: GLuint compute_shader: GLuint } GLShader *--> GLSources GLShader*--> GLSources GLShader *--> GLSources GLShader *--> GLSources GLSources *--> GLSource: entries GLShader *-->GLProgram: program_cache GLShader *..>GLProgram: program_active ```
Jeroen Bakker added 1 commit 2024-01-09 09:51:49 +01:00
cea4c6eff5 WIP: OpenGL: Specialization Constants
This PR adds support for specialization constants for the OpenGL
backend.

**TODO**

* [ ] Don't store sources when there is no specialization constants
      in the shader interface
* [ ] Separate constants from resources as this would be the only
      part of the shader that is recreated.
Jeroen Bakker added the
Interest
OpenGL
label 2024-01-09 09:52:04 +01:00
Jeroen Bakker added this to the 4.1 milestone 2024-01-09 09:52:11 +01:00
Jeroen Bakker added this to the EEVEE & Viewport project 2024-01-09 09:52:13 +01:00
Jeroen Bakker self-assigned this 2024-01-09 09:52:17 +01:00
Jeroen Bakker added 1 commit 2024-01-09 12:28:38 +01:00
67a7427156 Regular shaders should work, but got an unexpected error.
Committed in order to validate if this also occurs in main
Jeroen Bakker added 2 commits 2024-01-09 14:40:28 +01:00
Jeroen Bakker added 2 commits 2024-01-09 16:13:15 +01:00
Jeroen Bakker requested review from Clément Foucault 2024-01-09 16:13:52 +01:00
Jeroen Bakker added 2 commits 2024-01-11 07:56:10 +01:00
Jeroen Bakker added 1 commit 2024-01-11 08:14:04 +01:00
Jeroen Bakker changed title from WIP: OpenGL: Specialization Constants to OpenGL: Specialization Constants 2024-01-11 08:41:17 +01:00
Jeroen Bakker added 1 commit 2024-01-11 09:11:31 +01:00
Clément Foucault requested changes 2024-01-11 11:17:20 +01:00
@ -123,6 +123,11 @@ class ShaderInterface {
return input_lookup(
inputs_ + attr_len_ + ubo_len_ + uniform_len_ + ssbo_len_, constant_len_, name);
}
inline const ShaderInput *constant_get(const int binding) const

Would rather remove and only use the specialization_constants_.

Would rather remove and only use the `specialization_constants_`.
Jeroen-Bakker marked this conversation as resolved
@ -678,0 +658,4 @@
for (int constant_index : IndexRange(constants.types.size())) {
const char *name = nullptr;
/*
* When compiling GL shaders with specialization constants there is a dependency cycle.

I don't see the point of using the shader interface here. The specialization_constants_ array is initialized before compilation for this exact case.

I don't see the point of using the shader interface here. The specialization_constants_ array is initialized before compilation for this exact case.
Author
Member

This was elaborated in the chat.

  • Access to shader create info with specialization constant names are only available the first time.
  • When recompiling access to shader create info isn't possible
  • (References to ) names will be stored in GLShader::specialization_constant_names_ so during recompilation the names can still be retrieved.
This was elaborated in the chat. * Access to shader create info with specialization constant names are only available the first time. * When recompiling access to shader create info isn't possible * (References to ) names will be stored in `GLShader::specialization_constant_names_` so during recompilation the names can still be retrieved.
Jeroen-Bakker marked this conversation as resolved
@ -1412,0 +1568,4 @@
}
}
void GLShader::SpecializationPrograms::ensure_program_linked(GLShader &shader)

I would rather have a getter that returns the program_id. That would be more Object oriented.

I would rather have a getter that returns the `program_id`. That would be more Object oriented.
Jeroen-Bakker marked this conversation as resolved
@ -21,0 +56,4 @@
Vector<const char *> update_constants_source(const StringRefNull constants_patch) const;
private:
void update_patch_index();

Defined nowhere.

Defined nowhere.
Jeroen-Bakker marked this conversation as resolved
@ -33,3 +75,1 @@
GLuint geom_shader_ = 0;
GLuint frag_shader_ = 0;
GLuint compute_shader_ = 0;
struct SpecializationProgram {

Rename to GLProgram.

Rename to `GLProgram`.
Jeroen-Bakker marked this conversation as resolved
@ -35,1 +75,3 @@
GLuint compute_shader_ = 0;
struct SpecializationProgram {
/** Handle for program. */
GLuint shader_program = 0;

Use program_id_ so that it is in line with other GL classes (GLTexture, GLFramebuffer ...). Otherwise it's a bit confusing to have GLShader::GLProgram::shader_program.

Use `program_id_` so that it is in line with other GL classes (`GLTexture`, `GLFramebuffer` ...). Otherwise it's a bit confusing to have `GLShader::GLProgram::shader_program`.
Jeroen-Bakker marked this conversation as resolved
@ -36,0 +84,4 @@
void link(Shader &shader);
};
struct SpecializationPrograms {

I think this is making the code more confusing with passing the GLShader around.

I think this is making the code more confusing with passing the GLShader around.

I was not clear in my previous comment. I think it is better to remove this struct GLPrograms and make its members part of GLShader instead. This would remove some boiler plate code that have no benefit.

I was not clear in my previous comment. I think it is better to remove this `struct GLPrograms` and make its members part of `GLShader` instead. This would remove some boiler plate code that have no benefit.
Jeroen-Bakker marked this conversation as resolved
@ -36,0 +88,4 @@
using Key = Vector<shader::ShaderCreateInfo::SpecializationConstant::Value>;
private:
Map<Key, SpecializationProgram> entries;

Rename to program_cache.

Rename to `program_cache`.
Jeroen-Bakker marked this conversation as resolved
@ -36,0 +95,4 @@
* Points to the active specialization program. When binding a shader the active shader is
* setup.
*/
SpecializationProgram *active = nullptr;

Rename to program_active.

Rename to `program_active`.
Jeroen-Bakker marked this conversation as resolved
Jeroen Bakker added 2 commits 2024-01-11 15:09:30 +01:00
Jeroen Bakker added 1 commit 2024-01-11 15:14:49 +01:00
Jeroen Bakker requested review from Clément Foucault 2024-01-11 15:19:40 +01:00
Clément Foucault requested changes 2024-01-12 07:23:53 +01:00
@ -51,6 +149,7 @@ class GLShader : public Shader {
void warm_cache(int /*limit*/) override{};
std::string resources_declare(const shader::ShaderCreateInfo &info) const override;
std::string constants_declare(const shader::ShaderCreateInfo &info) override;

should be const too. I don't see it changing the shader.

should be `const` too. I don't see it changing the shader.
Author
Member

In the chat we spoke that resources_declare would build up the specialization_constants_names. Looking closer I did it in constants_declare as it fits better. I had to remove the const from the function because of this.

Other solution we briefly discussed was to pass ShaderCreateInfo as parameter to Backend::shader_alloc. I prefer adding a new virtual function as the control flow would then be easier to read.

In the chat we spoke that resources_declare would build up the specialization_constants_names. Looking closer I did it in constants_declare as it fits better. I had to remove the const from the function because of this. Other solution we briefly discussed was to pass ShaderCreateInfo as parameter to `Backend::shader_alloc`. I prefer adding a new virtual function as the control flow would then be easier to read.
Jeroen-Bakker marked this conversation as resolved
Jeroen Bakker added 1 commit 2024-01-12 07:42:18 +01:00
Jeroen Bakker added 1 commit 2024-01-12 08:26:11 +01:00
Clément Foucault requested changes 2024-01-12 08:29:16 +01:00
@ -678,0 +660,4 @@
}
/* Add an identifier that where the specialization constants will be added. */
ss << "\n/* GPU_GL_SPECIALIZATION_CONSTANTS. */\n";

Maybe instead of adding this and doing a string lookup, maybe just reserve the second slot for constants. Just like the first slot is for extensions and defines.

Maybe instead of adding this and doing a string lookup, maybe just reserve the second slot for constants. Just like the first slot is for extensions and defines.
Jeroen-Bakker marked this conversation as resolved
Clément Foucault reviewed 2024-01-12 08:41:18 +01:00
@ -462,8 +467,13 @@ void GPU_shader_bind(GPUShader *gpu_shader)
shader->bind();
GPU_matrix_bind(gpu_shader);
Shader::set_srgb_uniform(gpu_shader);
shader->constants.is_dirty = false;

I am also wondering if we could avoid the shader->constants.is_dirty flag. shader->bind() could be always called before the if branch and the backend could decide to bind new program or not.

I am also wondering if we could avoid the `shader->constants.is_dirty` flag. `shader->bind()` could be always called before the if branch and the backend could decide to bind new program or not.
Author
Member

Yes, but it is a different behavior than the current one and might introduce some issues I cannot oversee at this moment on Metal. Would perhaps do that outside this PR so we can track issues more easily.

Even for the vulkan backend I would like the bind to be called in order to check if pipeline needs to be recreated.

Yes, but it is a different behavior than the current one and might introduce some issues I cannot oversee at this moment on Metal. Would perhaps do that outside this PR so we can track issues more easily. Even for the vulkan backend I would like the bind to be called in order to check if pipeline needs to be recreated.
fclem marked this conversation as resolved
Jeroen Bakker added 1 commit 2024-01-12 11:07:48 +01:00
Jeroen Bakker added 1 commit 2024-01-12 11:12:35 +01:00
Clément Foucault requested changes 2024-01-12 11:39:41 +01:00
@ -324,6 +327,7 @@ GPUShader *GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
Vector<const char *> sources;
standard_defines(sources);
sources[SOURCES_INDEX_SPECIALIZATION_CONSTANTS] = constants.c_str();

Can be added directly in the backend now just like with glsl_patch_get(). No need for constants_declare() anymore.

Can be added directly in the backend now just like with `glsl_patch_get()`. No need for `constants_declare()` anymore.
Author
Member

Agree.

Agree.
Jeroen-Bakker marked this conversation as resolved
Jeroen Bakker added 2 commits 2024-01-12 11:52:42 +01:00
Jeroen Bakker added 1 commit 2024-01-12 12:05:06 +01:00
Jeroen Bakker requested review from Clément Foucault 2024-01-12 12:05:51 +01:00
Jeroen Bakker added 1 commit 2024-01-12 12:10:49 +01:00
Clément Foucault approved these changes 2024-01-12 12:48:27 +01:00
Jeroen Bakker added 1 commit 2024-01-12 13:35:53 +01:00
Jeroen Bakker added 1 commit 2024-01-12 13:40:58 +01:00
buildbot/vexp-code-patch-lint 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-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
5d5f2dc704
Remove comment
Author
Member

@blender-bot build

@blender-bot build
Jeroen Bakker merged commit 4ac0267567 into main 2024-01-12 14:28:58 +01:00
Sign in to join this conversation.
No reviewers
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
2 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#116926
No description provided.