Add support for compiling all files in a directory

shadercompiler
Caleb Cornett 2023-01-15 15:04:28 -05:00
parent 2b773101c8
commit 8a0d4472b4
1 changed files with 55 additions and 34 deletions

View File

@ -8,67 +8,88 @@ partial class Program
{ {
if (args.Length < 2) if (args.Length < 2)
{ {
Console.WriteLine("Usage: refreshc <path-to-glsl> <output-directory>"); Console.WriteLine("Usage: refreshc <path-to-glsl | path-to-input-directory> <output-directory>");
return 1; return 1;
} }
string glslPath = args[0]; string inputPath = args[0];
string outputDir = args[1]; string outputDir = args[1];
if (!ValidateArgs(glslPath, outputDir)) if (!Directory.Exists(outputDir))
{ {
Console.WriteLine($"refreshc: Output directory ({outputDir}) does not exist");
return 1; 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 shaderName = Path.GetFileNameWithoutExtension(glslPath);
string shaderType = Path.GetExtension(glslPath); string shaderType = Path.GetExtension(glslPath);
string spirvPath = Path.Combine(outputDir, $"{shaderName}.spv");
if (CompileGlslToSpirv(glslPath, shaderName, spirvPath) != 0) if (shaderType != ".vert" && shaderType != ".frag" && shaderType != ".comp")
{ {
Console.WriteLine("refreshc: Expected glsl source file with extension '.vert', '.frag', or '.comp'");
return 1; 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"); string hlslPath = Path.Combine(outputDir, $"{shaderName}.hlsl");
if (TranslateSpirvToHlsl(spirvPath, hlslPath) != 0) res = TranslateSpirvToHlsl(spirvPath, hlslPath);
if (res != 0)
{ {
return 1; return res;
} }
// FIXME: Is there a cross-platform way to compile HLSL to DXBC? // FIXME: Is there a cross-platform way to compile HLSL to DXBC?
#if PS5 #if PS5
if (TranslateHlslToPS5(hlslPath, shaderName, shaderType, outputDir) != 0) res = TranslateHlslToPS5(hlslPath, shaderName, shaderType, outputDir);
if (res != 0)
{ {
return 1; return res;
} }
#endif #endif
return 0; return res;
}
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) static int CompileGlslToSpirv(string glslPath, string shaderName, string outputPath)