OpenGL: Specialization Constants #116926
No reviewers
Labels
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
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#116926
Loading…
Reference in New Issue
No description provided.
Delete Branch "Jeroen-Bakker/blender:opengl/specialization-constants"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 keepa 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 previousGLSource that contains specialization constants is then replaced
by the new version.
WIP: OpenGL: Specialization Constantsto OpenGL: Specialization Constants@ -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_
.@ -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.
This was elaborated in the chat.
GLShader::specialization_constant_names_
so during recompilation the names can still be retrieved.@ -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.@ -21,0 +56,4 @@
Vector<const char *> update_constants_source(const StringRefNull constants_patch) const;
private:
void update_patch_index();
Defined nowhere.
@ -33,3 +75,1 @@
GLuint geom_shader_ = 0;
GLuint frag_shader_ = 0;
GLuint compute_shader_ = 0;
struct SpecializationProgram {
Rename to
GLProgram
.@ -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 haveGLShader::GLProgram::shader_program
.@ -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 was not clear in my previous comment. I think it is better to remove this
struct GLPrograms
and make its members part ofGLShader
instead. This would remove some boiler plate code that have no benefit.@ -36,0 +88,4 @@
using Key = Vector<shader::ShaderCreateInfo::SpecializationConstant::Value>;
private:
Map<Key, SpecializationProgram> entries;
Rename to
program_cache
.@ -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
.@ -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.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.@ -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.
@ -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.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.
@ -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 forconstants_declare()
anymore.Agree.
@blender-bot build