Add LZ4 compression to WebGPU shaders
This commit is contained in:
@@ -33,6 +33,9 @@ public class ShaderCompilerWebGPU : ShaderCompiler
|
||||
|
||||
options.PublicDefinitions.Add("COMPILE_WITH_WEBGPU_SHADER_COMPILER");
|
||||
options.PublicDependencies.Add("ShaderCompilerVulkan");
|
||||
options.PrivateDependencies.Add("glslang");
|
||||
options.PrivateDependencies.Add("spirv-tools");
|
||||
options.PrivateDependencies.Add("lz4");
|
||||
|
||||
// Deploy tint executable as a dependency for the shader compilation from SPIR-V into WGSL
|
||||
// Tint compiler from: https://github.com/google/dawn/releases
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "Engine/Platform/FileSystem.h"
|
||||
#include <ThirdParty/glslang/SPIRV/SpvTools.h>
|
||||
#include <ThirdParty/spirv-tools/libspirv.hpp>
|
||||
#include <ThirdParty/LZ4/lz4.h>
|
||||
|
||||
ShaderCompilerWebGPU::ShaderCompilerWebGPU(ShaderProfile profile)
|
||||
: ShaderCompilerVulkan(profile)
|
||||
@@ -90,6 +91,21 @@ bool ShaderCompilerWebGPU::Write(ShaderCompilationContext* context, ShaderFuncti
|
||||
FileSystem::DeleteFile(inputFile);
|
||||
FileSystem::DeleteFile(outputFile);
|
||||
|
||||
// Compress
|
||||
const int32 srcSize = wgsl.Length() + 1;
|
||||
const int32 maxSize = LZ4_compressBound(srcSize);
|
||||
Array<byte> wgslCompressed;
|
||||
wgslCompressed.Resize(maxSize + sizeof(int32));
|
||||
const int32 dstSize = LZ4_compress_default(wgsl.Get(), (char*)wgslCompressed.Get() + sizeof(int32), srcSize, maxSize);
|
||||
if (dstSize > 0)
|
||||
{
|
||||
wgslCompressed.Resize(dstSize + sizeof(int32));
|
||||
*(int32*)wgslCompressed.Get() = srcSize; // Store original size in the beginning to decompress it
|
||||
header.Type = SpirvShaderHeader::Types::WGSL_LZ4;
|
||||
return WriteShaderFunctionPermutation(_context, meta, permutationIndex, bindings, &header, sizeof(header), wgslCompressed.Get(), wgslCompressed.Count());
|
||||
}
|
||||
|
||||
|
||||
header.Type = SpirvShaderHeader::Types::WGSL;
|
||||
return WriteShaderFunctionPermutation(_context, meta, permutationIndex, bindings, &header, sizeof(header), wgsl.Get(), wgsl.Length() + 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user