yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
678de6547
master
1#ifndef SLANG_DECODER_HELPER_H 2#define SLANG_DECODER_HELPER_H 3 4#include "../../core/slang-list.h" 5#include "../util/record-format.h" 6#include "slang-com-helper.h" 7#include "slang.h" 8 9#include <stdint.h> 10 11namespace SlangRecord 12{ 13 14// This class is used to allocate memory for the type decoder 15class DecoderAllocatorSingleton 16{ 17public : 18static DecoderAllocatorSingleton * getInstance (); 19void * allocate (size_t size ); 20 ~DecoderAllocatorSingleton (); 21 22private : 23DecoderAllocatorSingleton ()= default ; 24Slang ::List < void *> m_allocations ; 25}; 26 27class DecoderBase 28{ 29public : 30virtual ~DecoderBase ()= default ; 31void * allocate (size_t size ) {return m_allocator -> allocate (size ); } 32 33protected : 34DecoderAllocatorSingleton * m_allocator = DecoderAllocatorSingleton ::getInstance (); 35}; 36 37// We don't allow pointer type to be used as a template parameter 38template < typename T ,typename U = typename std ::enable_if < !std ::is_pointer < T > ::value > ::type > 39class ValueDecoder :public DecoderBase 40{ 41public : 42T & getValue () {return m_value ; } 43 44protected : 45T m_value {}; 46}; 47 48template < typename T ,typename = typename std ::enable_if < !std ::is_pointer < T > ::value > ::type > 49class StructDecoder :public ValueDecoder < T > 50{ 51public : 52using Super = ValueDecoder < T > ; 53size_t decode (const uint8_t * buffer ,int64_t bufferSize ); 54}; 55 56// We only allow pointer type to be used as a template parameter 57template < typename T ,typename U = typename std ::enable_if < std ::is_pointer < T > ::value > ::type > 58class PointerDecoder :public DecoderBase 59{ 60public : 61void setPointer (void * data ) {m_pointer = static_cast < T > (data ); } 62T getPointer ()const {return m_pointer ; } 63void setPointerAddress (uint64_t address ) {m_pointerAddress = address ; } 64void setDataSize (size_t size ) {m_dataSize = size ; } 65size_t getDataSize ()const {return m_dataSize ; } 66 67private : 68T m_pointer {nullptr}; 69uint64_t m_pointerAddress = 0 ; 70size_t m_dataSize = 0 ; 71}; 72 73class BlobDecoder 74{ 75private : 76PointerDecoder < void *> m_blobData ; 77 78class BlobImpl :public slang ::IBlob 79 { 80public : 81// ISlangUnknown 82virtual SLANG_NO_THROW SlangResult SLANG_MCALL 83queryInterface (SlangUUID const & uuid ,void ** outObject )override 84 { 85if (uuid == ISlangUnknown ::getTypeGuid ()|| uuid == ISlangBlob ::getTypeGuid ()) 86 { 87* outObject = static_cast < ISlangBlob *> (this ); 88return SLANG_OK ; 89 } 90* outObject = nullptr ; 91return SLANG_E_NO_INTERFACE ; 92 } 93 94virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef ()override {return 1 ; } 95virtual SLANG_NO_THROW uint32_t SLANG_MCALL release ()override {return 1 ; } 96 97BlobImpl (const PointerDecoder < void *>* blobData ) 98 :m_pBlobData (blobData ) 99 { 100 } 101virtual SLANG_NO_THROW void const * SLANG_MCALL getBufferPointer ()SLANG_OVERRIDE 102 { 103return m_pBlobData -> getPointer (); 104 } 105virtual SLANG_NO_THROW size_t SLANG_MCALL getBufferSize ()SLANG_OVERRIDE 106 { 107return m_pBlobData -> getDataSize (); 108 } 109 110private : 111const PointerDecoder < void *>* m_pBlobData ; 112 }; 113 114BlobImpl m_blobImpl {& m_blobData }; 115AddressFormat m_address {0 }; 116 117public : 118size_t decode (const uint8_t * buffer ,int64_t bufferSize ); 119slang ::IBlob * getBlob () {return & m_blobImpl ; } 120}; 121 122using StringDecoder = PointerDecoder < char *> ; 123}// namespace SlangRecord 124 125#endif // SLANG_RECORD_DECODER_HELPER_H