yum-mirror/slang

Making it easier to work with shaders

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

Ellie Hermaszewskaformatf65d756bf

master
2.9 KiB110 linesraw
1#include "slang-deflate-compression-system.h"
2
3#include "slang-com-helper.h"
4#include "slang-com-ptr.h"
5
6// We don't want compress #define to clash
7#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES 1
8
9#include "slang-blob.h"
10
11#include <miniz.h>
12
13namespace Slang
14{
15
16// Allocate static const storage for the various interface IDs that the Slang API needs to expose
17
18class DeflateCompressionSystemImpl : public RefObject, public ICompressionSystem
19{
20public:
21    // ISlangUnknown
22    // override ref counting, as singleton
23    SLANG_IUNKNOWN_QUERY_INTERFACE
24
25    SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; }
26    SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; }
27
28    // ICompressionSystem
29    virtual SLANG_NO_THROW CompressionSystemType SLANG_MCALL getSystemType() SLANG_OVERRIDE
30    {
31        return CompressionSystemType::Deflate;
32    }
33    virtual SLANG_NO_THROW SlangResult SLANG_MCALL compress(
34        const CompressionStyle* style,
35        const void* src,
36        size_t srcSizeInBytes,
37        ISlangBlob** outBlob) SLANG_OVERRIDE;
38    virtual SLANG_NO_THROW SlangResult SLANG_MCALL decompress(
39        const void* compressed,
40        size_t compressedSizeInBytes,
41        size_t decompressedSizeInBytes,
42        void* outDecompressed) SLANG_OVERRIDE;
43
44protected:
45    ICompressionSystem* getInterface(const Guid& guid);
46};
47
48ICompressionSystem* DeflateCompressionSystemImpl::getInterface(const Guid& guid)
49{
50    return (guid == ISlangUnknown::getTypeGuid() || guid == ICompressionSystem::getTypeGuid())
51               ? static_cast<ICompressionSystem*>(this)
52               : nullptr;
53}
54
55SlangResult DeflateCompressionSystemImpl::compress(
56    const CompressionStyle* style,
57    const void* src,
58    size_t srcSizeInBytes,
59    ISlangBlob** outBlob)
60{
61    SLANG_UNUSED(style);
62
63    size_t compressedSizeInBytes;
64
65    const int flags = 0;
66    void* compressed =
67        tdefl_compress_mem_to_heap(src, srcSizeInBytes, &compressedSizeInBytes, flags);
68
69    if (!compressed)
70    {
71        return SLANG_FAIL;
72    }
73
74    ScopedAllocation alloc;
75    alloc.attach(compressed, compressedSizeInBytes);
76
77    auto blob = RawBlob::moveCreate(alloc);
78    *outBlob = blob.detach();
79    return SLANG_OK;
80}
81
82SlangResult DeflateCompressionSystemImpl::decompress(
83    const void* compressed,
84    size_t compressedSizeInBytes,
85    size_t decompressedSizeInBytes,
86    void* outDecompressed)
87{
88    const int flags = TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
89
90    size_t size = tinfl_decompress_mem_to_mem(
91        outDecompressed,
92        decompressedSizeInBytes,
93        compressed,
94        compressedSizeInBytes,
95        flags);
96    if (size == TINFL_DECOMPRESS_MEM_TO_MEM_FAILED)
97    {
98        return SLANG_FAIL;
99    }
100
101    return SLANG_OK;
102}
103
104/* static */ ICompressionSystem* DeflateCompressionSystem::getSingleton()
105{
106    static DeflateCompressionSystemImpl impl;
107    return &impl;
108}
109
110} // namespace Slang