yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
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 23SLANG_IUNKNOWN_QUERY_INTERFACE 24 25SLANG_NO_THROW uint32_t SLANG_MCALL addRef ()SLANG_OVERRIDE {return 1 ; } 26SLANG_NO_THROW uint32_t SLANG_MCALL release ()SLANG_OVERRIDE {return 1 ; } 27 28// ICompressionSystem 29virtual SLANG_NO_THROW CompressionSystemType SLANG_MCALL getSystemType ()SLANG_OVERRIDE 30 { 31return CompressionSystemType ::Deflate ; 32 } 33virtual SLANG_NO_THROW SlangResult SLANG_MCALL compress ( 34const CompressionStyle * style , 35const void * src , 36size_t srcSizeInBytes , 37ISlangBlob ** outBlob )SLANG_OVERRIDE ; 38virtual SLANG_NO_THROW SlangResult SLANG_MCALL decompress ( 39const void * compressed , 40size_t compressedSizeInBytes , 41size_t decompressedSizeInBytes , 42void * outDecompressed )SLANG_OVERRIDE ; 43 44protected : 45ICompressionSystem * getInterface (const Guid & guid ); 46}; 47 48ICompressionSystem * DeflateCompressionSystemImpl ::getInterface (const Guid & guid ) 49{ 50return (guid == ISlangUnknown ::getTypeGuid ()|| guid == ICompressionSystem ::getTypeGuid ()) 51 ?static_cast < ICompressionSystem *> (this ) 52 :nullptr ; 53} 54 55SlangResult DeflateCompressionSystemImpl ::compress ( 56const CompressionStyle * style , 57const void * src , 58size_t srcSizeInBytes , 59ISlangBlob ** outBlob ) 60{ 61SLANG_UNUSED (style ); 62 63size_t compressedSizeInBytes ; 64 65const int flags = 0 ; 66void * compressed = 67tdefl_compress_mem_to_heap (src ,srcSizeInBytes ,& compressedSizeInBytes ,flags ); 68 69if (!compressed ) 70 { 71return SLANG_FAIL ; 72 } 73 74ScopedAllocation alloc ; 75alloc .attach (compressed ,compressedSizeInBytes ); 76 77auto blob = RawBlob ::moveCreate (alloc ); 78* outBlob = blob .detach (); 79return SLANG_OK ; 80} 81 82SlangResult DeflateCompressionSystemImpl ::decompress ( 83const void * compressed , 84size_t compressedSizeInBytes , 85size_t decompressedSizeInBytes , 86void * outDecompressed ) 87{ 88const int flags = TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF ; 89 90size_t size = tinfl_decompress_mem_to_mem ( 91outDecompressed , 92decompressedSizeInBytes , 93compressed , 94compressedSizeInBytes , 95flags ); 96if (size == TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ) 97 { 98return SLANG_FAIL ; 99 } 100 101return SLANG_OK ; 102} 103 104/* static */ ICompressionSystem * DeflateCompressionSystem ::getSingleton () 105{ 106static DeflateCompressionSystemImpl impl ; 107return & impl ; 108} 109 110}// namespace Slang