Add LZ4 compression to WebGPU shaders

This commit is contained in:
Wojtek Figat
2026-03-04 09:15:53 +01:00
parent 075727ab53
commit aff8090adb
6 changed files with 49 additions and 3 deletions

View File

@@ -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);
}