yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
8000e0ede
master
1#ifndef PARAMETER_DECODER_H 2#define PARAMETER_DECODER_H 3 4#include "../util/record-format.h" 5#include "../util/record-utility.h" 6#include "decoder-helper.h" 7#include "slang.h" 8 9#include <cinttypes> 10#include <cstdlib> 11#include <cstring> 12#include <vector> 13 14namespace SlangRecord 15{ 16class ParameterDecoder 17{ 18public : 19static size_t decodeInt8 (const uint8_t * buffer ,int64_t bufferSize ,int8_t & value ) 20 { 21return decodeValue (buffer ,bufferSize ,value ); 22 } 23static size_t decodeUint8 (const uint8_t * buffer ,int64_t bufferSize ,uint8_t & value ) 24 { 25return decodeValue (buffer ,bufferSize ,value ); 26 } 27static size_t decodeInt16 (const uint8_t * buffer ,int64_t bufferSize ,int16_t & value ) 28 { 29return decodeValue (buffer ,bufferSize ,value ); 30 } 31static size_t decodeUint16 (const uint8_t * buffer ,int64_t bufferSize ,uint16_t & value ) 32 { 33return decodeValue (buffer ,bufferSize ,value ); 34 } 35static size_t decodeInt32 (const uint8_t * buffer ,int64_t bufferSize ,int32_t & value ) 36 { 37return decodeValue (buffer ,bufferSize ,value ); 38 } 39static size_t decodeUint32 (const uint8_t * buffer ,int64_t bufferSize ,uint32_t & value ) 40 { 41return decodeValue (buffer ,bufferSize ,value ); 42 } 43static size_t decodeInt64 (const uint8_t * buffer ,int64_t bufferSize ,int64_t & value ) 44 { 45return decodeValue (buffer ,bufferSize ,value ); 46 } 47static size_t decodeUint64 (const uint8_t * buffer ,int64_t bufferSize ,uint64_t & value ) 48 { 49return decodeValue (buffer ,bufferSize ,value ); 50 } 51static size_t decodeFloat (const uint8_t * buffer ,int64_t bufferSize ,float & value ) 52 { 53return decodeValue (buffer ,bufferSize ,value ); 54 } 55static size_t decodeDouble (const uint8_t * buffer ,int64_t bufferSize ,double & value ) 56 { 57return decodeValue (buffer ,bufferSize ,value ); 58 } 59static size_t decodeBool (const uint8_t * buffer ,int64_t bufferSize ,bool & value ) 60 { 61return decodeValue (buffer ,bufferSize ,value ); 62 } 63 64template < typename T > 65static size_t decodeEnumValue (const uint8_t * buffer ,size_t bufferSize ,T & value ) 66 { 67uint32_t decodedValue ; 68size_t readByte = decodeValue (buffer ,bufferSize ,decodedValue ); 69value = static_cast < T > (decodedValue ); 70return readByte ; 71 } 72 73static size_t decodeString ( 74const uint8_t * buffer , 75int64_t bufferSize , 76PointerDecoder < char *>& typeDecoder ); 77 78static size_t decodePointer ( 79const uint8_t * buffer , 80int64_t bufferSize , 81PointerDecoder < void *>& pointerDecoder ); 82 83static size_t decodeAddress ( 84const uint8_t * buffer , 85int64_t bufferSize , 86SlangRecord ::AddressFormat & address ) 87 { 88return decodeValue (buffer ,bufferSize ,address ); 89 } 90static size_t decodeStruct ( 91const uint8_t * buffer , 92int64_t bufferSize , 93ValueDecoder < SlangGlobalSessionDesc >& sessionDesc ); 94static size_t decodeStruct ( 95const uint8_t * buffer , 96int64_t bufferSize , 97ValueDecoder < slang ::SessionDesc >& sessionDesc ); 98static size_t decodeStruct ( 99const uint8_t * buffer , 100int64_t bufferSize , 101ValueDecoder < slang ::PreprocessorMacroDesc >& desc ); 102static size_t decodeStruct ( 103const uint8_t * buffer , 104int64_t bufferSize , 105ValueDecoder < slang ::CompilerOptionEntry >& entry ); 106static size_t decodeStruct ( 107const uint8_t * buffer , 108int64_t bufferSize , 109ValueDecoder < slang ::CompilerOptionValue >& value ); 110static size_t decodeStruct ( 111const uint8_t * buffer , 112int64_t bufferSize , 113ValueDecoder < slang ::TargetDesc >& targetDesc ); 114static size_t decodeStruct ( 115const uint8_t * buffer , 116int64_t bufferSize , 117ValueDecoder < slang ::SpecializationArg >& specializationArg ); 118 119template < typename T > 120static size_t decodeValueArray ( 121const uint8_t * buffer , 122int64_t bufferSize , 123ValueDecoder < T >* valueArray , 124size_t count ) 125 { 126if (count == 0 && bufferSize == 0 ) 127 { 128return 0 ; 129 } 130 131size_t readByte = 0 ; 132for (size_t i = 0 ;i < count ;++ i ) 133 { 134readByte += decodeValue (buffer + readByte ,bufferSize - readByte ,valueArray [i ]); 135 } 136return readByte ; 137 } 138 139static size_t decodeStringArray ( 140const uint8_t * buffer , 141int64_t bufferSize , 142char ** outputArray , 143size_t count ) 144 { 145if (count == 0 && bufferSize == 0 ) 146 { 147return 0 ; 148 } 149SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 150 151size_t readByte = 0 ; 152for (uint32_t i = 0 ;i < count ;i ++ ) 153 { 154PointerDecoder < char *> item ; 155readByte += decodeString (buffer + readByte ,bufferSize - readByte ,item ); 156 157// Copy the search path 158outputArray [i ]= item .getPointer (); 159 } 160return readByte ; 161 } 162 163template < typename T > 164static size_t decodeStructArray ( 165const uint8_t * buffer , 166int64_t bufferSize , 167T * outputArray , 168size_t count ) 169 { 170if (count == 0 && bufferSize == 0 ) 171 { 172return 0 ; 173 } 174SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 175 176size_t bufferRead = 0 ; 177for (size_t i = 0 ;i < count ;++ i ) 178 { 179ValueDecoder < T > item ; 180bufferRead += decodeStruct (buffer + bufferRead ,bufferSize - bufferRead ,item ); 181outputArray [i ]= item .getValue (); 182 } 183return bufferRead ; 184 } 185 186static size_t decodeAddressArray ( 187const uint8_t * buffer , 188int64_t bufferSize , 189uint64_t * addressArray , 190size_t count ) 191 { 192if (count == 0 && bufferSize == 0 ) 193 { 194return 0 ; 195 } 196SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 197 198size_t bufferRead = 0 ; 199for (size_t i = 0 ;i < count ;++ i ) 200 { 201bufferRead += 202decodeAddress (buffer + bufferRead ,bufferSize - bufferRead ,addressArray [i ]); 203 } 204 205return bufferRead ; 206 } 207 208 209private : 210template < typename T > 211static size_t decodeValue (const uint8_t * buffer ,int64_t bufferSize ,T & value ) 212 { 213SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 214 215int64_t dataSize = sizeof (T ); 216 217SLANG_RECORD_ASSERT (bufferSize >=dataSize ); 218 219size_t bytesRead = 0 ; 220bytesRead = dataSize ; 221memcpy (& value ,buffer ,dataSize ); 222 223return bytesRead ; 224 } 225}; 226}// namespace SlangRecord 227 228#endif // PARAMETER_DECODER_H