127 lines
2.7 KiB
C#
127 lines
2.7 KiB
C#
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 | path-to-input-directory> <output-directory>");
|
|
return 1;
|
|
}
|
|
|
|
string inputPath = args[0];
|
|
string outputDir = args[1];
|
|
|
|
if (!Directory.Exists(outputDir))
|
|
{
|
|
Console.WriteLine($"refreshc: Output directory ({outputDir}) does not exist");
|
|
return 1;
|
|
}
|
|
|
|
if (Directory.Exists(inputPath))
|
|
{
|
|
// Loop over and compile each file in the directory
|
|
string[] files = Directory.GetFiles(inputPath);
|
|
foreach (string file in files)
|
|
{
|
|
Console.WriteLine($"Compiling {file}");
|
|
int res = CompileShader(file, outputDir);
|
|
if (res != 0)
|
|
{
|
|
return res;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!File.Exists(inputPath))
|
|
{
|
|
Console.WriteLine($"refreshc: glsl source file or directory ({inputPath}) does not exist");
|
|
return 1;
|
|
}
|
|
|
|
int res = CompileShader(inputPath, outputDir);
|
|
if (res != 0)
|
|
{
|
|
return res;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int CompileShader(string glslPath, string outputDir)
|
|
{
|
|
int res = 0;
|
|
string shaderName = Path.GetFileNameWithoutExtension(glslPath);
|
|
string shaderType = Path.GetExtension(glslPath);
|
|
|
|
if (shaderType != ".vert" && shaderType != ".frag" && shaderType != ".comp")
|
|
{
|
|
Console.WriteLine("refreshc: Expected glsl source file with extension '.vert', '.frag', or '.comp'");
|
|
return 1;
|
|
}
|
|
|
|
string spirvPath = Path.Combine(outputDir, $"{shaderName}.spv");
|
|
res = CompileGlslToSpirv(glslPath, shaderName, spirvPath);
|
|
if (res != 0)
|
|
{
|
|
return res;
|
|
}
|
|
|
|
string hlslPath = Path.Combine(outputDir, $"{shaderName}.hlsl");
|
|
res = TranslateSpirvToHlsl(spirvPath, hlslPath);
|
|
if (res != 0)
|
|
{
|
|
return res;
|
|
}
|
|
|
|
// FIXME: Is there a cross-platform way to compile HLSL to DXBC?
|
|
|
|
#if PS5
|
|
res = TranslateHlslToPS5(hlslPath, shaderName, shaderType, outputDir);
|
|
if (res != 0)
|
|
{
|
|
return res;
|
|
}
|
|
#endif
|
|
|
|
return res;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|