Skip to content

Commit

Permalink
spir-v opengl map combined samplers
Browse files Browse the repository at this point in the history
  • Loading branch information
aquagoose committed Jul 25, 2024
1 parent 7c7c0ed commit b95b9f7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/grabs.Core/GrabsLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ public static class GrabsLog
{
public static event OnLogMessage LogMessage = delegate { };

public static void Log(LogType type, string message)
public static void Log(Severity severity, string message)
{
LogMessage.Invoke(type, message);
LogMessage.Invoke(severity, message);
}

public enum LogType
public enum Severity
{
Verbose,
Debug,
Expand All @@ -21,5 +21,5 @@ public enum LogType
Critical
}

public delegate void OnLogMessage(LogType type, string message);
public delegate void OnLogMessage(Severity type, string message);
}
20 changes: 10 additions & 10 deletions src/grabs.Graphics.Vulkan/VkInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public VkInstance(string[] extensions, string appName = null, string engineName
PpEnabledLayerNames = pLayers
};

GrabsLog.Log(GrabsLog.LogType.Verbose, "Creating instance.");
GrabsLog.Log(GrabsLog.Severity.Verbose, "Creating instance.");
CheckResult(Vk.CreateInstance(&instanceCreateInfo, null, out Instance), "create instance");

if (!Vk.TryGetInstanceExtension(Instance, out DebugUtils))
Expand All @@ -89,7 +89,7 @@ public VkInstance(string[] extensions, string appName = null, string engineName
PfnUserCallback = new PfnDebugUtilsMessengerCallbackEXT(DebugCallback)
};

GrabsLog.Log(GrabsLog.LogType.Verbose, "Creating debug messenger.");
GrabsLog.Log(GrabsLog.Severity.Verbose, "Creating debug messenger.");
CheckResult(DebugUtils.CreateDebugUtilsMessenger(Instance, &messengerCreateInfo, null, out DebugMessenger));
}

Expand Down Expand Up @@ -158,13 +158,13 @@ private uint DebugCallback(DebugUtilsMessageSeverityFlagsEXT messageSeverity,
if (messageSeverity == DebugUtilsMessageSeverityFlagsEXT.ErrorBitExt)
throw new Exception(message);

GrabsLog.LogType type = messageSeverity switch
GrabsLog.Severity type = messageSeverity switch
{
DebugUtilsMessageSeverityFlagsEXT.None => GrabsLog.LogType.Verbose,
DebugUtilsMessageSeverityFlagsEXT.VerboseBitExt => GrabsLog.LogType.Verbose,
DebugUtilsMessageSeverityFlagsEXT.InfoBitExt => GrabsLog.LogType.Info,
DebugUtilsMessageSeverityFlagsEXT.WarningBitExt => GrabsLog.LogType.Warning,
DebugUtilsMessageSeverityFlagsEXT.ErrorBitExt => GrabsLog.LogType.Error,
DebugUtilsMessageSeverityFlagsEXT.None => GrabsLog.Severity.Verbose,
DebugUtilsMessageSeverityFlagsEXT.VerboseBitExt => GrabsLog.Severity.Verbose,
DebugUtilsMessageSeverityFlagsEXT.InfoBitExt => GrabsLog.Severity.Info,
DebugUtilsMessageSeverityFlagsEXT.WarningBitExt => GrabsLog.Severity.Warning,
DebugUtilsMessageSeverityFlagsEXT.ErrorBitExt => GrabsLog.Severity.Error,
_ => throw new ArgumentOutOfRangeException(nameof(messageSeverity), messageSeverity, null)
};

Expand All @@ -175,11 +175,11 @@ private uint DebugCallback(DebugUtilsMessageSeverityFlagsEXT messageSeverity,

public override void Dispose()
{
GrabsLog.Log(GrabsLog.LogType.Verbose, "Destroying debug messenger.");
GrabsLog.Log(GrabsLog.Severity.Verbose, "Destroying debug messenger.");
DebugUtils.DestroyDebugUtilsMessenger(Instance, DebugMessenger, null);
DebugUtils.Dispose();

GrabsLog.Log(GrabsLog.LogType.Verbose, "Destroying instance.");
GrabsLog.Log(GrabsLog.Severity.Verbose, "Destroying instance.");
Vk.DestroyInstance(Instance, null);
}
}
34 changes: 24 additions & 10 deletions src/grabs.ShaderCompiler.Spirv/SpirvCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using grabs.Core;
using grabs.Graphics;
using Silk.NET.SPIRV;
using Silk.NET.SPIRV.Cross;
Expand Down Expand Up @@ -108,8 +109,16 @@ public static unsafe string TranspileSpirv(ShaderStage stage, ShaderLanguage lan
Spirv.CompilerGetCombinedImageSamplers(compiler, &combinedSamplers, &numSamplers);
for (uint i = 0; i < (int) numSamplers; i++)
{
Spirv.CompilerSetDecoration(compiler, combinedSamplers[i].CombinedId, Decoration.Binding,
newBinding++);
uint id = combinedSamplers[i].ImageId;

uint set = Spirv.CompilerGetDecoration(compiler, id, Decoration.DescriptorSet);
uint binding = Spirv.CompilerGetDecoration(compiler, id, Decoration.Binding);

Spirv.CompilerSetDecoration(compiler, combinedSamplers[i].CombinedId, Decoration.Binding, newBinding);

AddRemapping(ref remappings, set, binding, newBinding);

newBinding++;
}

break;
Expand Down Expand Up @@ -182,7 +191,7 @@ public static unsafe string TranspileSpirv(ShaderStage stage, ShaderLanguage lan
Spirv.ContextReleaseAllocations(context);
Spirv.ContextDestroy(context);

Console.WriteLine(strResult);
GrabsLog.Log(GrabsLog.Severity.Verbose, strResult);

return strResult;
}
Expand Down Expand Up @@ -213,15 +222,20 @@ private static unsafe void RemapDescriptorBindingsForType(Compiler* compiler, Re
Spirv.CompilerUnsetDecoration(compiler, id, Decoration.DescriptorSet);
Spirv.CompilerSetDecoration(compiler, id, Decoration.Binding, newBinding);

if (!remappings.Sets.TryGetValue(currentSet, out Remapping remapping))
{
remapping = new Remapping();
remappings.Sets.Add(currentSet, remapping);
}

remapping.Bindings.Add(currentBinding, newBinding);
AddRemapping(ref remappings, currentSet, currentBinding, newBinding);

newBinding++;
}
}

private static void AddRemapping(ref DescriptorRemappings remappings, uint set, uint binding, uint newBinding)
{
if (!remappings.Sets.TryGetValue(set, out Remapping remapping))
{
remapping = new Remapping();
remappings.Sets.Add(set, remapping);
}

remapping.Bindings.Add(binding, newBinding);
}
}
8 changes: 4 additions & 4 deletions tests/grabs.Tests.Compilers/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ struct PSOutput
float4x4 View;
}
cbuffer DrawInfo : register(b0, space1)
cbuffer DrawInfo : register(b1, space0)
{
float4x4 World;
}
Texture2D Texture : register(t5, space2);
SamplerState state : register(s0, space2);
Texture2D Texture : register(t0, space1);
SamplerState state : register(s0, space1);
VSOutput Vertex(const in VSInput input)
{
Expand All @@ -62,7 +62,7 @@ PSOutput Pixel(const in VSOutput input)
""";

byte[] result = Compiler.CompileToSpirV(shaderCode, entryPoint, stage, true);
Console.WriteLine(SpirvCompiler.TranspileSpirv(stage, ShaderLanguage.Hlsl50, result, entryPoint, out DescriptorRemappings remappings));
Console.WriteLine(SpirvCompiler.TranspileSpirv(stage, ShaderLanguage.Glsl430, result, entryPoint, out DescriptorRemappings remappings));

foreach ((uint set, Remapping remapping) in remappings.Sets)
{
Expand Down

0 comments on commit b95b9f7

Please sign in to comment.