Added additional config to CompilePass in IShaderCompiler

This commit is contained in:
2025-12-27 15:14:06 +09:00
parent f988c34b3d
commit c9be05fc60
5 changed files with 48 additions and 26 deletions

View File

@@ -49,7 +49,7 @@ internal sealed partial class DxcShaderCompiler
};
}
private static List<string> GetCompilerArguments(ref readonly CompilerConfig config)
private static List<string> GetCompilerArguments(ref readonly ShaderCompilationConfig config)
{
var argsArray = new List<string>
{
@@ -242,17 +242,13 @@ internal sealed unsafe partial class DxcShaderCompiler : IShaderCompiler
}
}
public Result<ShaderCompileResult> Compile(ref readonly CompilerConfig config, Allocator allocator)
public Result<ShaderCompileResult> Compile(ref readonly ShaderCompilationConfig config, Allocator allocator)
{
ObjectDisposedException.ThrowIf(_disposed, this);
using ComPtr<IDxcIncludeHandler> includeHandler = default;
using ComPtr<IDxcBlobEncoding> sourceBlob = default;
// Create DXC _compiler.Get() and _utils.Get()
var dxccID = CLSID.CLSID_DxcCompiler;
var dxcuID = CLSID.CLSID_DxcUtils;
ThrowIfFailed(_utils.Get()->CreateDefaultIncludeHandler(includeHandler.GetAddressOf()));
// Create source blob
@@ -344,7 +340,9 @@ internal sealed unsafe partial class DxcShaderCompiler : IShaderCompiler
}
}
public Result<GraphicsCompiledResult> CompilePass(IPassDescriptor descriptor, string? generatedCodePath)
// TODO: This should be shader variant specific compile instead of pass specific.
// TODO: Build final shader code in memory before compiling.
public Result<GraphicsCompiledResult> CompilePass(IPassDescriptor descriptor, ref readonly ShaderCompilationConfig additionalConfig, string? generatedCodePath)
{
ObjectDisposedException.ThrowIf(_disposed, this);
@@ -353,20 +351,23 @@ internal sealed unsafe partial class DxcShaderCompiler : IShaderCompiler
return Result.Failure("FullPassDescriptor expected.");
}
var fullDefines = fullDescriptor.defines ?? new List<string>();
fullDefines.AddRange(additionalConfig.defines);
ShaderCompileResult tsResult = default;
var tsEntry = fullDescriptor.taskShader;
if (tsEntry.IsCreated)
{
var config = new CompilerConfig
var config = new ShaderCompilationConfig
{
defines = fullDescriptor.defines.AsSpan(),
defines = fullDefines.AsSpan(),
include = generatedCodePath,
shaderPath = tsEntry.shader,
entryPoint = tsEntry.entry,
stage = ShaderStage.TaskShader,
tier = CompilerTier.Tier0,
optimizeLevel = CompilerOptimizeLevel.O3,
options = CompilerOption.KeepReflections,
tier = additionalConfig.tier,
optimizeLevel = additionalConfig.optimizeLevel,
options = additionalConfig.options,
};
var result = Compile(ref config, Allocator.Persistent);
@@ -382,16 +383,16 @@ internal sealed unsafe partial class DxcShaderCompiler : IShaderCompiler
var msEntry = fullDescriptor.meshShader;
if (msEntry.IsCreated)
{
var config = new CompilerConfig
var config = new ShaderCompilationConfig
{
defines = fullDescriptor.defines.AsSpan(),
defines = fullDefines.AsSpan(),
include = generatedCodePath,
shaderPath = msEntry.shader,
entryPoint = msEntry.entry,
stage = ShaderStage.MeshShader,
tier = CompilerTier.Tier0,
optimizeLevel = CompilerOptimizeLevel.O3,
options = CompilerOption.KeepReflections,
tier = additionalConfig.tier,
optimizeLevel = additionalConfig.optimizeLevel,
options = additionalConfig.options,
};
var result = Compile(ref config, Allocator.Persistent);
@@ -411,16 +412,16 @@ internal sealed unsafe partial class DxcShaderCompiler : IShaderCompiler
var psEntry = fullDescriptor.pixelShader;
if (psEntry.IsCreated)
{
var config = new CompilerConfig
var config = new ShaderCompilationConfig
{
defines = fullDescriptor.defines.AsSpan(),
defines = fullDefines.AsSpan(),
include = generatedCodePath,
shaderPath = psEntry.shader,
entryPoint = psEntry.entry,
stage = ShaderStage.PixelShader,
tier = CompilerTier.Tier0,
optimizeLevel = CompilerOptimizeLevel.O3,
options = CompilerOption.KeepReflections,
tier = additionalConfig.tier,
optimizeLevel = additionalConfig.optimizeLevel,
options = additionalConfig.options,
};
var result = Compile(ref config, Allocator.Persistent);

View File

@@ -36,6 +36,7 @@ public partial struct Shader
private static int s_nextPropertyID = 0;
private static readonly Dictionary<string, int> s_keywordNameToID = new Dictionary<string, int>();
private static readonly Dictionary<int, string> s_keywordIDToName = new Dictionary<int, string>();
private static int s_nextkeywordID = 0;
public static Identifier<ShaderPass> GetPassID(string passName)
@@ -68,9 +69,20 @@ public partial struct Shader
id = s_nextkeywordID++;
}
s_keywordIDToName[id] = keywordName;
return id;
}
public static string? GetKeywordName(int keywordID)
{
if (s_keywordIDToName.TryGetValue(keywordID, out var name))
{
return name;
}
return null;
}
// TODO: Global keywords
}