Skip to content

Commit

Permalink
Switched shader compression algorithm from MSZIP to LZ4-HC
Browse files Browse the repository at this point in the history
As reported in the issue #42, compression support is missing from the
Wine’s implementation of cabinet.dll, as of Wine version 8.3.
  • Loading branch information
Const-me committed Mar 14, 2023
1 parent 1b2aa95 commit 66e297d
Show file tree
Hide file tree
Showing 10 changed files with 3,632 additions and 39 deletions.
1 change: 1 addition & 0 deletions Tools/CompressShaders/Cabinet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace CompressShaders
/// <para>If you wonder why not gzip — because the OS doesn’t include an API for that, at least not an API usable from C or C++.<br/>
/// .NET standard library includes gzip algorithm, but we don't want Whisper.dll to depend on .NET.</para>
/// </remarks>
[Obsolete]
static class Cabinet
{
/// <summary>Compression algorithm</summary>
Expand Down
2 changes: 1 addition & 1 deletion Tools/CompressShaders/CompressShaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static void writePayload( string root, FoundShaders shaders, out int cbSource, o
offsets.Add( (int)ms.Length );

byte[] dxbc = ms.ToArray();
byte[] compressed = Cabinet.compressBuffer( dxbc );
byte[] compressed = LZ4.compressBuffer( dxbc );
cbSource = dxbc.Length;
cbCompressed = compressed.Length;

Expand Down
3 changes: 3 additions & 0 deletions Tools/CompressShaders/CompressShaders.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.5" />
</ItemGroup>
</Project>
26 changes: 26 additions & 0 deletions Tools/CompressShaders/LZ4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace CompressShaders;
using K4os.Compression.LZ4;

/// <summary>Lossless data compressor which uses LZ4-HC compressor</summary>
/// <seealso href="https://github.com/lz4/lz4" />
/// <seealso href="https://github.com/MiloszKrajewski/K4os.Compression.LZ4" />
static class LZ4
{
// compression speed drops rapidly when not using FAST mode, while decompression speed stays the same
// Actually, it is usually faster for high compression levels as there is less data to process
// https://github.com/MiloszKrajewski/K4os.Compression.LZ4#compression-levels
const LZ4Level compressionLevel = LZ4Level.L12_MAX;

public static byte[] compressBuffer( byte[] src )
{
int maxLength = LZ4Codec.MaximumOutputSize( src.Length );
byte[] output = new byte[ maxLength ];
int cb = LZ4Codec.Encode( src, output, compressionLevel );
if( cb > 0 )
{
Array.Resize( ref output, cb );
return output;
}
throw new ApplicationException( $"LZ4Codec.Encode failed with status {cb}" );
}
}
45 changes: 7 additions & 38 deletions Whisper/D3D/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include "shaders.h"
#include "startup.h"
#include "device.h"
#include <compressapi.h>
#pragma comment( lib, "Cabinet.lib" )
#include "../Utils/LZ4/lz4.h"

namespace
{
Expand All @@ -13,38 +12,6 @@ namespace
#include "shaderData-Release.inl"
#endif

constexpr DWORD compressionAlgorithm = COMPRESS_ALGORITHM_MSZIP;

class Decompressor
{
DECOMPRESSOR_HANDLE handle = nullptr;

public:

HRESULT create()
{
if( CreateDecompressor( compressionAlgorithm, nullptr, &handle ) )
return S_OK;
return HRESULT_FROM_WIN32( GetLastError() );
}

HRESULT decompress( const uint8_t* src, size_t compressedLength, void* dest, size_t origLength ) const
{
if( Decompress( handle, src, compressedLength, dest, origLength, nullptr ) )
return S_OK;
return HRESULT_FROM_WIN32( GetLastError() );
}

~Decompressor()
{
if( nullptr != handle )
{
CloseDecompressor( handle );
handle = nullptr;
}
}
};

static std::vector<CComPtr<ID3D11ComputeShader>> s_shaders;
}

Expand All @@ -65,10 +32,12 @@ HRESULT DirectCompute::createComputeShaders()
return E_OUTOFMEMORY;
}

Decompressor decomp;
CHECK( decomp.create() );

decomp.decompress( s_compressedShaders.data(), s_compressedShaders.size(), dxbc.data(), cbDecompressedLength );
const int lz4Status = LZ4_decompress_safe( (const char*)s_compressedShaders.data(), (char*)dxbc.data(), (int)s_compressedShaders.size(), (int)cbDecompressedLength );
if( lz4Status != (int)cbDecompressedLength )
{
logError( u8"LZ4_decompress_safe failed with status %i", lz4Status );
return PLA_E_CABAPI_FAILURE;
}
ID3D11Device* const dev = device();

const auto& blobs = gpuInfo.wave64() ? s_shaderBlobs64 : s_shaderBlobs32;
Expand Down
24 changes: 24 additions & 0 deletions Whisper/Utils/LZ4/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
LZ4 Library
Copyright (c) 2011-2020, Yann Collet
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading

0 comments on commit 66e297d

Please sign in to comment.