Skip to content

Commit

Permalink
Fix shader cache on Vulkan when geometry shaders are inserted (#3868)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdkchan authored and italomandara committed Nov 20, 2022
1 parent ba62660 commit abb865d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Ryujinx.Graphics.Gpu/Shader/DiskCache/DiskCacheHostStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DiskCacheHostStorage
private const ushort FileFormatVersionMajor = 1;
private const ushort FileFormatVersionMinor = 2;
private const uint FileFormatVersionPacked = ((uint)FileFormatVersionMajor << 16) | FileFormatVersionMinor;
private const uint CodeGenVersion = 3866;
private const uint CodeGenVersion = 3868;

private const string SharedTocFileName = "shared.toc";
private const string SharedDataFileName = "shared.data";
Expand Down Expand Up @@ -379,7 +379,7 @@ public void LoadShaders(GpuContext context, ParallelDiskCacheLoader loader)

if (context.Capabilities.Api == TargetApi.Vulkan)
{
ShaderSource[] shaderSources = ShaderBinarySerializer.Unpack(shaders, hostCode, isCompute);
ShaderSource[] shaderSources = ShaderBinarySerializer.Unpack(shaders, hostCode);

hostProgram = context.Renderer.CreateProgram(shaderSources, shaderInfo);
}
Expand Down
36 changes: 26 additions & 10 deletions Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Shader;
using Ryujinx.Graphics.Shader.Translation;
using System;
using System.Collections.Generic;
using System.IO;

Expand All @@ -12,38 +14,52 @@ public static byte[] Pack(ShaderSource[] sources)
using MemoryStream output = new MemoryStream();
using BinaryWriter writer = new BinaryWriter(output);

writer.Write(sources.Length);

for (int i = 0; i < sources.Length; i++)
{
writer.Write((int)sources[i].Stage);
writer.Write(sources[i].BinaryCode.Length);
writer.Write(sources[i].BinaryCode);
}

return output.ToArray();
}

public static ShaderSource[] Unpack(CachedShaderStage[] stages, byte[] code, bool compute)
public static ShaderSource[] Unpack(CachedShaderStage[] stages, byte[] code)
{
using MemoryStream input = new MemoryStream(code);
using BinaryReader reader = new BinaryReader(input);

List<ShaderSource> output = new List<ShaderSource>();

for (int i = compute ? 0 : 1; i < stages.Length; i++)
{
CachedShaderStage stage = stages[i];

if (stage == null)
{
continue;
}
int count = reader.ReadInt32();

for (int i = 0; i < count; i++)
{
ShaderStage stage = (ShaderStage)reader.ReadInt32();
int binaryCodeLength = reader.ReadInt32();
byte[] binaryCode = reader.ReadBytes(binaryCodeLength);

output.Add(new ShaderSource(binaryCode, ShaderCache.GetBindings(stage.Info), stage.Info.Stage, TargetLanguage.Spirv));
output.Add(new ShaderSource(binaryCode, GetBindings(stages, stage), stage, TargetLanguage.Spirv));
}

return output.ToArray();
}

private static ShaderBindings GetBindings(CachedShaderStage[] stages, ShaderStage stage)
{
for (int i = 0; i < stages.Length; i++)
{
CachedShaderStage currentStage = stages[i];

if (currentStage != null && currentStage.Info.Stage == stage && currentStage.Info != null)
{
return ShaderCache.GetBindings(currentStage.Info);
}
}

return new ShaderBindings(Array.Empty<int>(), Array.Empty<int>(), Array.Empty<int>(), Array.Empty<int>());
}
}
}

0 comments on commit abb865d

Please sign in to comment.