yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
2.8 KiB97 linesraw
1#include "slang-lz4-compression-system.h"
2
3#include "slang-blob.h"
4#include "slang-com-helper.h"
5#include "slang-com-ptr.h"
6
7#include <lz4.h>
8
9namespace Slang
10{
11
12// Allocate static const storage for the various interface IDs that the Slang API needs to expose
13
14class LZ4CompressionSystemImpl : public RefObject, public ICompressionSystem
15{
16public:
17    // ISlangUnknown
18    // override ref counting, as singleton
19    SLANG_IUNKNOWN_QUERY_INTERFACE
20    SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; }
21    SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; }
22
23    // ICompressionSystem
24    virtual SLANG_NO_THROW CompressionSystemType SLANG_MCALL getSystemType() SLANG_OVERRIDE
25    {
26        return CompressionSystemType::LZ4;
27    }
28    virtual SLANG_NO_THROW SlangResult SLANG_MCALL compress(
29        const CompressionStyle* style,
30        const void* src,
31        size_t srcSizeInBytes,
32        ISlangBlob** outBlob) SLANG_OVERRIDE;
33    virtual SLANG_NO_THROW SlangResult SLANG_MCALL decompress(
34        const void* compressed,
35        size_t compressedSizeInBytes,
36        size_t decompressedSizeInBytes,
37        void* outDecompressed) SLANG_OVERRIDE;
38
39protected:
40    ICompressionSystem* getInterface(const Guid& guid);
41};
42
43ICompressionSystem* LZ4CompressionSystemImpl::getInterface(const Guid& guid)
44{
45    return (guid == ISlangUnknown::getTypeGuid() || guid == ICompressionSystem::getTypeGuid())
46               ? static_cast<ICompressionSystem*>(this)
47               : nullptr;
48}
49
50SlangResult LZ4CompressionSystemImpl::compress(
51    const CompressionStyle* style,
52    const void* src,
53    size_t srcSizeInBytes,
54    ISlangBlob** outBlob)
55{
56    SLANG_UNUSED(style);
57    const size_t compressedBound = LZ4_compressBound(int(srcSizeInBytes));
58
59    ScopedAllocation alloc;
60    void* compressedData = alloc.allocate(compressedBound);
61
62    const int compressedSize = LZ4_compress_default(
63        (const char*)src,
64        (char*)compressedData,
65        int(srcSizeInBytes),
66        int(compressedBound));
67    alloc.reallocate(compressedSize);
68
69    auto blob = RawBlob::moveCreate(alloc);
70
71    *outBlob = blob.detach();
72    return SLANG_OK;
73}
74
75SlangResult LZ4CompressionSystemImpl::decompress(
76    const void* compressed,
77    size_t compressedSizeInBytes,
78    size_t decompressedSizeInBytes,
79    void* outDecompressed)
80{
81    const int decompressedSize = LZ4_decompress_safe(
82        (const char*)compressed,
83        (char*)outDecompressed,
84        int(compressedSizeInBytes),
85        int(decompressedSizeInBytes));
86    SLANG_UNUSED(decompressedSize);
87    SLANG_ASSERT(size_t(decompressedSize) == decompressedSizeInBytes);
88    return SLANG_OK;
89}
90
91/* static */ ICompressionSystem* LZ4CompressionSystem::getSingleton()
92{
93    static LZ4CompressionSystemImpl impl;
94    return &impl;
95}
96
97} // namespace Slang