Added shader cross-compiler using glslc and spirv-cross
continuous-integration/drone/pr Build is passing Details

pull/34/head
Caleb Cornett 2023-01-14 23:34:55 -05:00
parent f7250ab12a
commit 2b773101c8
2 changed files with 114 additions and 0 deletions

105
shadercompiler/Program.cs Normal file
View File

@ -0,0 +1,105 @@
using System;
using System.IO;
using System.Diagnostics;
partial class Program
{
public static int Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Usage: refreshc <path-to-glsl> <output-directory>");
return 1;
}
string glslPath = args[0];
string outputDir = args[1];
if (!ValidateArgs(glslPath, outputDir))
{
return 1;
}
string shaderName = Path.GetFileNameWithoutExtension(glslPath);
string shaderType = Path.GetExtension(glslPath);
string spirvPath = Path.Combine(outputDir, $"{shaderName}.spv");
if (CompileGlslToSpirv(glslPath, shaderName, spirvPath) != 0)
{
return 1;
}
string hlslPath = Path.Combine(outputDir, $"{shaderName}.hlsl");
if (TranslateSpirvToHlsl(spirvPath, hlslPath) != 0)
{
return 1;
}
// FIXME: Is there a cross-platform way to compile HLSL to DXBC?
#if PS5
if (TranslateHlslToPS5(hlslPath, shaderName, shaderType, outputDir) != 0)
{
return 1;
}
#endif
return 0;
}
static bool ValidateArgs(string glslPath, string outputDir)
{
if (!File.Exists(glslPath))
{
Console.WriteLine($"refreshc: glsl source file ({glslPath}) does not exist");
return false;
}
string ext = Path.GetExtension(glslPath);
if (ext != ".vert" && ext != ".frag" && ext != ".comp")
{
Console.WriteLine("refreshc: Expected glsl source file with extension '.vert', '.frag', or '.comp'");
return false;
}
if (!Directory.Exists(outputDir))
{
Console.WriteLine($"refreshc: Output directory ({outputDir}) does not exist");
return false;
}
return true;
}
static int CompileGlslToSpirv(string glslPath, string shaderName, string outputPath)
{
Process glslc = Process.Start(
"glslc",
$"{glslPath} -o {outputPath}"
);
glslc.WaitForExit();
if (glslc.ExitCode != 0)
{
Console.WriteLine($"refreshc: Could not compile GLSL code");
return 1;
}
return 0;
}
static int TranslateSpirvToHlsl(string spirvPath, string outputPath)
{
Process spirvcross = Process.Start(
"spirv-cross",
$"{spirvPath} --hlsl --shader-model 50 --output {outputPath}"
);
spirvcross.WaitForExit();
if (spirvcross.ExitCode != 0)
{
Console.WriteLine($"refreshc: Could not translate SPIR-V to HLSL");
return 1;
}
return 0;
}
}

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetName>refreshc</TargetName>
</PropertyGroup>
</Project>