-
Notifications
You must be signed in to change notification settings - Fork 424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add disk cache for shader compilations #5829
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dee306b
Add shader compilation cache
smoogipoo cac31ce
Nullable-annotate GLShader and GLShaderPart
smoogipoo 1ce6167
Fix CI inspections
smoogipoo 6d25076
Fix another CI inspection
smoogipoo 7efde6b
Update log output to show whether a shader was compiled or cached
peppy 5a5c6d5
Merge branch 'master' into spirv-cache
peppy 779bf7a
Merge branch 'master' into spirv-cache
bdach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using osu.Framework.Extensions.ObjectExtensions; | ||
using osu.Framework.Graphics.OpenGL.Buffers; | ||
using osu.Framework.Graphics.Rendering; | ||
using osu.Framework.Graphics.Shaders; | ||
|
@@ -42,14 +40,14 @@ internal class GLShader : IShader | |
|
||
private readonly GLShaderPart vertexPart; | ||
private readonly GLShaderPart fragmentPart; | ||
private readonly VertexFragmentCompilationResult crossCompileResult; | ||
private readonly VertexFragmentShaderCompilation compilation; | ||
|
||
internal GLShader(GLRenderer renderer, string name, GLShaderPart[] parts, IUniformBuffer<GlobalUniformData> globalUniformBuffer) | ||
internal GLShader(GLRenderer renderer, string name, GLShaderPart[] parts, IUniformBuffer<GlobalUniformData> globalUniformBuffer, ShaderCompilationStore compilationStore) | ||
{ | ||
this.renderer = renderer; | ||
this.name = name; | ||
this.globalUniformBuffer = globalUniformBuffer; | ||
this.parts = parts.Where(p => p != null).ToArray(); | ||
this.parts = parts; | ||
|
||
vertexPart = parts.Single(p => p.Type == ShaderType.VertexShader); | ||
fragmentPart = parts.Single(p => p.Type == ShaderType.FragmentShader); | ||
|
@@ -59,9 +57,9 @@ internal GLShader(GLRenderer renderer, string name, GLShaderPart[] parts, IUnifo | |
try | ||
{ | ||
// Shaders are in "Vulkan GLSL" format. They need to be cross-compiled to GLSL. | ||
crossCompileResult = SpirvCompilation.CompileVertexFragment( | ||
Encoding.UTF8.GetBytes(vertexPart.GetRawText()), | ||
Encoding.UTF8.GetBytes(fragmentPart.GetRawText()), | ||
compilation = compilationStore.CompileVertexFragment( | ||
vertexPart.GetRawText(), | ||
fragmentPart.GetRawText(), | ||
renderer.IsEmbedded ? CrossCompileTarget.ESSL : CrossCompileTarget.GLSL); | ||
} | ||
catch (Exception e) | ||
|
@@ -158,8 +156,8 @@ public virtual void BindUniformBlock(string blockName, IUniformBuffer buffer) | |
|
||
private protected virtual bool CompileInternal() | ||
{ | ||
vertexPart.Compile(crossCompileResult.VertexShader); | ||
fragmentPart.Compile(crossCompileResult.FragmentShader); | ||
vertexPart.Compile(compilation.VertexText); | ||
fragmentPart.Compile(compilation.FragmentText); | ||
|
||
foreach (GLShaderPart p in parts) | ||
GL.AttachShader(this, p); | ||
|
@@ -176,7 +174,7 @@ private protected virtual bool CompileInternal() | |
int blockBindingIndex = 0; | ||
int textureIndex = 0; | ||
|
||
foreach (ResourceLayoutDescription layout in crossCompileResult.Reflection.ResourceLayouts) | ||
foreach (ResourceLayoutDescription layout in compilation.Reflection.ResourceLayouts) | ||
{ | ||
if (layout.Elements.Length == 0) | ||
continue; | ||
|
@@ -226,15 +224,16 @@ public void Dispose() | |
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!IsDisposed) | ||
{ | ||
IsDisposed = true; | ||
if (IsDisposed) | ||
return; | ||
|
||
shaderCompileDelegate?.Cancel(); | ||
IsDisposed = true; | ||
|
||
if (programID != -1) | ||
DeleteProgram(this); | ||
} | ||
if (shaderCompileDelegate.IsNotNull()) | ||
shaderCompileDelegate.Cancel(); | ||
|
||
if (programID != -1) | ||
DeleteProgram(this); | ||
} | ||
|
||
#endregion | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
|
@@ -31,7 +29,7 @@ internal class GLShaderPart : IShaderPart | |
|
||
private int partID = -1; | ||
|
||
public GLShaderPart(IRenderer renderer, string name, byte[] data, ShaderType type, IShaderStore store) | ||
public GLShaderPart(IRenderer renderer, string name, byte[]? data, ShaderType type, IShaderStore store) | ||
{ | ||
this.renderer = renderer; | ||
this.store = store; | ||
|
@@ -62,10 +60,10 @@ public GLShaderPart(IRenderer renderer, string name, byte[] data, ShaderType typ | |
shaderCodes[i] = uniform_pattern.Replace(shaderCodes[i], match => $"{match.Groups[1].Value}set = {int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture) + 1}{match.Groups[3].Value}"); | ||
} | ||
|
||
private string loadFile(byte[] bytes, bool mainFile) | ||
private string loadFile(byte[]? bytes, bool mainFile) | ||
{ | ||
if (bytes == null) | ||
return null; | ||
return string.Empty; | ||
|
||
using (MemoryStream ms = new MemoryStream(bytes)) | ||
using (StreamReader sr = new StreamReader(ms)) | ||
|
@@ -74,7 +72,7 @@ private string loadFile(byte[] bytes, bool mainFile) | |
|
||
while (sr.Peek() != -1) | ||
{ | ||
string line = sr.ReadLine(); | ||
string? line = sr.ReadLine(); | ||
|
||
if (string.IsNullOrEmpty(line)) | ||
{ | ||
|
@@ -123,10 +121,10 @@ private string loadFile(byte[] bytes, bool mainFile) | |
|
||
if (!string.IsNullOrEmpty(backbufferCode)) | ||
{ | ||
string realMainName = "real_main_" + Guid.NewGuid().ToString("N"); | ||
const string real_main_name = "__internal_real_main"; | ||
|
||
backbufferCode = backbufferCode.Replace("{{ real_main }}", realMainName); | ||
code = Regex.Replace(code, @"void main\((.*)\)", $"void {realMainName}()") + backbufferCode + '\n'; | ||
backbufferCode = backbufferCode.Replace("{{ real_main }}", real_main_name); | ||
code = Regex.Replace(code, @"void main\((.*)\)", $"void {real_main_name}()") + backbufferCode + '\n'; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
osu.Framework/Graphics/Shaders/ComputeProgramCompilation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using Veldrid.SPIRV; | ||
|
||
namespace osu.Framework.Graphics.Shaders | ||
{ | ||
public class ComputeProgramCompilation | ||
{ | ||
/// <summary> | ||
/// Whether this compilation was retrieved from cache. | ||
/// </summary> | ||
public bool WasCached { get; set; } | ||
|
||
/// <summary> | ||
/// The SpirV bytes for the program. | ||
/// </summary> | ||
public byte[] ProgramBytes { get; set; } = null!; | ||
|
||
/// <summary> | ||
/// The cross-compiled program text. | ||
/// </summary> | ||
public string ProgramText { get; set; } = null!; | ||
|
||
/// <summary> | ||
/// A reflection of the shader program, describing the layout of resources. | ||
/// </summary> | ||
public SpirvReflection Reflection { get; set; } = null!; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this main name change significant in any way? I'm not sure what the guid was doing, and blame doesn't help, since it was added in, of all things, b9406dd, which just barely predates me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's significant by keeping the shader code deterministic. The guid was only present to avoid naming collisions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right, good point. I'm assuming the naming collisions you mention are no longer relevant. Makes sense 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__internal_
should be as effective as guid, and if not then we could suffix an incrementing value like__internal_real_main_1
, but I doubt that'll happen.One case I can realistically think of is if we end up wanting multiple
main
trampolines, but there's no support for that right now.