yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
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 19SLANG_IUNKNOWN_QUERY_INTERFACE 20SLANG_NO_THROW uint32_t SLANG_MCALL addRef ()SLANG_OVERRIDE {return 1 ; } 21SLANG_NO_THROW uint32_t SLANG_MCALL release ()SLANG_OVERRIDE {return 1 ; } 22 23// ICompressionSystem 24virtual SLANG_NO_THROW CompressionSystemType SLANG_MCALL getSystemType ()SLANG_OVERRIDE 25 { 26return CompressionSystemType ::LZ4 ; 27 } 28virtual SLANG_NO_THROW SlangResult SLANG_MCALL compress ( 29const CompressionStyle * style , 30const void * src , 31size_t srcSizeInBytes , 32ISlangBlob ** outBlob )SLANG_OVERRIDE ; 33virtual SLANG_NO_THROW SlangResult SLANG_MCALL decompress ( 34const void * compressed , 35size_t compressedSizeInBytes , 36size_t decompressedSizeInBytes , 37void * outDecompressed )SLANG_OVERRIDE ; 38 39protected : 40ICompressionSystem * getInterface (const Guid & guid ); 41}; 42 43ICompressionSystem * LZ4CompressionSystemImpl ::getInterface (const Guid & guid ) 44{ 45return (guid == ISlangUnknown ::getTypeGuid ()|| guid == ICompressionSystem ::getTypeGuid ()) 46 ?static_cast < ICompressionSystem *> (this ) 47 :nullptr ; 48} 49 50SlangResult LZ4CompressionSystemImpl ::compress ( 51const CompressionStyle * style , 52const void * src , 53size_t srcSizeInBytes , 54ISlangBlob ** outBlob ) 55{ 56SLANG_UNUSED (style ); 57const size_t compressedBound = LZ4_compressBound (int (srcSizeInBytes )); 58 59ScopedAllocation alloc ; 60void * compressedData = alloc .allocate (compressedBound ); 61 62const int compressedSize = LZ4_compress_default ( 63 (const char * )src , 64 (char * )compressedData , 65int (srcSizeInBytes ), 66int (compressedBound )); 67alloc .reallocate (compressedSize ); 68 69auto blob = RawBlob ::moveCreate (alloc ); 70 71* outBlob = blob .detach (); 72return SLANG_OK ; 73} 74 75SlangResult LZ4CompressionSystemImpl ::decompress ( 76const void * compressed , 77size_t compressedSizeInBytes , 78size_t decompressedSizeInBytes , 79void * outDecompressed ) 80{ 81const int decompressedSize = LZ4_decompress_safe ( 82 (const char * )compressed , 83 (char * )outDecompressed , 84int (compressedSizeInBytes ), 85int (decompressedSizeInBytes )); 86SLANG_UNUSED (decompressedSize ); 87SLANG_ASSERT (size_t (decompressedSize )== decompressedSizeInBytes ); 88return SLANG_OK ; 89} 90 91/* static */ ICompressionSystem * LZ4CompressionSystem ::getSingleton () 92{ 93static LZ4CompressionSystemImpl impl ; 94return & impl ; 95} 96 97}// namespace Slang