From 6d6aaf2f66f67861f8abc3403cb9b4c16ccfdb24 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Tue, 20 Jun 2023 18:51:41 +0900 Subject: [PATCH 1/4] Add header guard to fragment output part --- .../Resources/Shaders/Internal/sh_Fragment_Output.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/osu.Framework/Resources/Shaders/Internal/sh_Fragment_Output.h b/osu.Framework/Resources/Shaders/Internal/sh_Fragment_Output.h index 4d4db57e81..ad5bd9b403 100644 --- a/osu.Framework/Resources/Shaders/Internal/sh_Fragment_Output.h +++ b/osu.Framework/Resources/Shaders/Internal/sh_Fragment_Output.h @@ -1,5 +1,8 @@ // Automatically included for every fragment shader. +#ifndef INTERNAL_FRAGMENT_OUTPUT_H +#define INTERNAL_FRAGMENT_OUTPUT_H + {{ fragment_output_layout }} void main() @@ -8,4 +11,6 @@ void main() // Ensure no fragment input is culled out from the shader by passing them in the output. {{ fragment_output_assignment }} -} \ No newline at end of file +} + +#endif \ No newline at end of file From 375aea7d544c0d891c97d844072f3e956494cbef Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Tue, 20 Jun 2023 19:00:59 +0900 Subject: [PATCH 2/4] Simplify creation of shader resource layouts --- .../Graphics/OpenGL/Shaders/GLShader.cs | 8 ++++- .../Graphics/Veldrid/Shaders/VeldridShader.cs | 34 ++++--------------- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs b/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs index 9a83dc605b..351d3f5f35 100644 --- a/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs +++ b/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs @@ -174,13 +174,19 @@ private protected virtual bool CompileInternal() int blockBindingIndex = 0; int textureIndex = 0; - foreach (ResourceLayoutDescription layout in compilation.Reflection.ResourceLayouts) + for (int set = 0; set < compilation.Reflection.ResourceLayouts.Length; set++) { + ResourceLayoutDescription layout = compilation.Reflection.ResourceLayouts[set]; + if (layout.Elements.Length == 0) continue; if (layout.Elements.Any(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite)) { + ResourceLayoutElementDescription textureDesc = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); + if (layout.Elements.All(e => e.Kind != ResourceKind.Sampler)) + throw new ProgramLinkingFailedException(name, $"Uniform set {set} contains a texture ({textureDesc.Name}) with no associated sampler."); + var textureElement = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); textureUniforms.Add(new Uniform(renderer, this, textureElement.Name, GL.GetUniformLocation(this, textureElement.Name)) { diff --git a/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs b/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs index 821d34c468..c203c89b72 100644 --- a/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs +++ b/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs @@ -181,41 +181,21 @@ private void compile() if (layout.Elements.Any(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite)) { - // Todo: We should enforce that a texture set contains both a texture and a sampler. - var textureElement = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); - var samplerElement = layout.Elements.First(e => e.Kind == ResourceKind.Sampler); - - textureLayouts.Add(new VeldridUniformLayout( - set, - renderer.Factory.CreateResourceLayout( - new ResourceLayoutDescription( - new ResourceLayoutElementDescription( - textureElement.Name, - ResourceKind.TextureReadOnly, - ShaderStages.Fragment), - new ResourceLayoutElementDescription( - samplerElement.Name, - ResourceKind.Sampler, - ShaderStages.Fragment))))); + ResourceLayoutElementDescription textureDesc = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); + if (layout.Elements.All(e => e.Kind != ResourceKind.Sampler)) + throw new InvalidOperationException($"Uniform set {set} contains a texture ({textureDesc.Name}) with no associated sampler."); + + textureLayouts.Add(new VeldridUniformLayout(set, renderer.Factory.CreateResourceLayout(layout))); } else if (layout.Elements[0].Kind == ResourceKind.UniformBuffer) - { - uniformLayouts[layout.Elements[0].Name] = new VeldridUniformLayout( - set, - renderer.Factory.CreateResourceLayout( - new ResourceLayoutDescription( - new ResourceLayoutElementDescription( - layout.Elements[0].Name, - ResourceKind.UniformBuffer, - ShaderStages.Fragment | ShaderStages.Vertex)))); - } + uniformLayouts[layout.Elements[0].Name] = new VeldridUniformLayout(set, renderer.Factory.CreateResourceLayout(layout)); } Logger.Log(cached ? $"🖍️ Shader {name} loaded from cache!" : $"🖍️ Shader {name} compiled!"); } - catch (SpirvCompilationException e) + catch (Exception e) { Logger.Error(e, $"🖍️ Failed to initialise shader {name}"); throw; From 1ceae4b08063faecce3874b4c46b76b822755378 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Wed, 21 Jun 2023 16:53:04 +0900 Subject: [PATCH 3/4] Remove set ID from message --- osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs | 6 ++---- osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs b/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs index 351d3f5f35..555277de44 100644 --- a/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs +++ b/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs @@ -174,10 +174,8 @@ private protected virtual bool CompileInternal() int blockBindingIndex = 0; int textureIndex = 0; - for (int set = 0; set < compilation.Reflection.ResourceLayouts.Length; set++) + foreach (ResourceLayoutDescription layout in compilation.Reflection.ResourceLayouts) { - ResourceLayoutDescription layout = compilation.Reflection.ResourceLayouts[set]; - if (layout.Elements.Length == 0) continue; @@ -185,7 +183,7 @@ private protected virtual bool CompileInternal() { ResourceLayoutElementDescription textureDesc = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); if (layout.Elements.All(e => e.Kind != ResourceKind.Sampler)) - throw new ProgramLinkingFailedException(name, $"Uniform set {set} contains a texture ({textureDesc.Name}) with no associated sampler."); + throw new ProgramLinkingFailedException(name, $"Texture {textureDesc.Name} has no associated sampler."); var textureElement = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); textureUniforms.Add(new Uniform(renderer, this, textureElement.Name, GL.GetUniformLocation(this, textureElement.Name)) diff --git a/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs b/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs index c203c89b72..6b3729f99e 100644 --- a/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs +++ b/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs @@ -183,7 +183,7 @@ private void compile() { ResourceLayoutElementDescription textureDesc = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); if (layout.Elements.All(e => e.Kind != ResourceKind.Sampler)) - throw new InvalidOperationException($"Uniform set {set} contains a texture ({textureDesc.Name}) with no associated sampler."); + throw new InvalidOperationException($"Texture {textureDesc.Name} has no associated sampler."); textureLayouts.Add(new VeldridUniformLayout(set, renderer.Factory.CreateResourceLayout(layout))); } From 53d00eea656403acb659fe29aa5e6c7f57a4db59 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Wed, 21 Jun 2023 16:54:19 +0900 Subject: [PATCH 4/4] Remove double iteration --- osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs | 6 +++--- osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs b/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs index 555277de44..73e6bc29a9 100644 --- a/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs +++ b/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs @@ -181,11 +181,11 @@ private protected virtual bool CompileInternal() if (layout.Elements.Any(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite)) { - ResourceLayoutElementDescription textureDesc = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); + ResourceLayoutElementDescription textureElement = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); + if (layout.Elements.All(e => e.Kind != ResourceKind.Sampler)) - throw new ProgramLinkingFailedException(name, $"Texture {textureDesc.Name} has no associated sampler."); + throw new ProgramLinkingFailedException(name, $"Texture {textureElement.Name} has no associated sampler."); - var textureElement = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); textureUniforms.Add(new Uniform(renderer, this, textureElement.Name, GL.GetUniformLocation(this, textureElement.Name)) { Value = textureIndex++ diff --git a/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs b/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs index 6b3729f99e..a62747a3d7 100644 --- a/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs +++ b/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs @@ -181,9 +181,10 @@ private void compile() if (layout.Elements.Any(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite)) { - ResourceLayoutElementDescription textureDesc = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); + ResourceLayoutElementDescription textureElement = layout.Elements.First(e => e.Kind == ResourceKind.TextureReadOnly || e.Kind == ResourceKind.TextureReadWrite); + if (layout.Elements.All(e => e.Kind != ResourceKind.Sampler)) - throw new InvalidOperationException($"Texture {textureDesc.Name} has no associated sampler."); + throw new InvalidOperationException($"Texture {textureElement.Name} has no associated sampler."); textureLayouts.Add(new VeldridUniformLayout(set, renderer.Factory.CreateResourceLayout(layout))); }