yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
faf042ecc
master
1#include "parameter-decoder.h" 2 3#include <string.h> 4 5namespace SlangRecord 6{ 7size_t ParameterDecoder ::decodeString ( 8const uint8_t * buffer , 9int64_t bufferSize , 10PointerDecoder < char *>& typeDecoder ) 11{ 12SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 13 14if (bufferSize < (int64_t )sizeof (uint32_t )) 15 { 16return 0 ; 17 } 18 19uint32_t stringLength = 0 ; 20size_t readByte = 0 ; 21readByte += decodeUint32 (buffer ,bufferSize - readByte ,stringLength ); 22 23SLANG_RECORD_ASSERT (bufferSize >= (int64_t )(readByte + stringLength )); 24 25if (stringLength == 0 ) 26 { 27return readByte ; 28 } 29 30uint8_t * data = (uint8_t * )typeDecoder .allocate (stringLength + 1 ); 31memcpy (data ,buffer + readByte ,stringLength ); 32typeDecoder .setPointer (data ); 33typeDecoder .setDataSize (stringLength + 1 ); 34return readByte + stringLength ; 35} 36 37size_t ParameterDecoder ::decodePointer ( 38const uint8_t * buffer , 39int64_t bufferSize , 40PointerDecoder < void *>& pointerDecoder ) 41{ 42SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 43 44if (bufferSize < (int64_t )sizeof (uint32_t )) 45 { 46return 0 ; 47 } 48 49uint64_t address = 0 ; 50size_t readByte = decodeAddress (buffer ,bufferSize ,address ); 51pointerDecoder .setPointerAddress (address ); 52 53uint64_t dataSize = 0 ; 54readByte += decodeUint64 (buffer + readByte ,bufferSize - readByte ,dataSize ); 55 56// return if the data size is 0 57if (dataSize == 0 ) 58 { 59return readByte ; 60 } 61 62SLANG_RECORD_ASSERT (bufferSize >= (int64_t )(readByte + dataSize )); 63 64uint8_t * data = (uint8_t * )pointerDecoder .allocate (dataSize ); 65memcpy (data ,buffer + readByte ,dataSize ); 66pointerDecoder .setPointer (data ); 67pointerDecoder .setDataSize (dataSize ); 68return readByte + dataSize ; 69} 70 71size_t ParameterDecoder ::decodeStruct ( 72const uint8_t * buffer , 73int64_t bufferSize , 74ValueDecoder < SlangGlobalSessionDesc >& sessionDesc ) 75{ 76SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 77 78if (bufferSize < (int64_t )sizeof (uint64_t )) 79 { 80return 0 ; 81 } 82 83size_t readByte = 0 ; 84SlangGlobalSessionDesc & desc = sessionDesc .getValue (); 85readByte = decodeUint32 (buffer ,bufferSize ,desc .structureSize ); 86readByte += decodeUint32 (buffer + readByte ,bufferSize - readByte ,desc .apiVersion ); 87readByte += decodeUint32 (buffer + readByte ,bufferSize - readByte ,desc .minLanguageVersion ); 88uint32_t val = 0 ; 89readByte += decodeUint32 (buffer + readByte ,bufferSize - readByte ,val ); 90desc .enableGLSL = (val != 0 ); 91 92return readByte ; 93} 94 95size_t ParameterDecoder ::decodeStruct ( 96const uint8_t * buffer , 97int64_t bufferSize , 98ValueDecoder < slang::SessionDesc >& sessionDesc ) 99{ 100SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 101 102if (bufferSize < (int64_t )sizeof (uint64_t )) 103 { 104return 0 ; 105 } 106 107size_t readByte = 0 ; 108 slang::SessionDesc & desc = sessionDesc .getValue (); 109 110uint64_t structSize = 0 ; 111readByte = decodeUint64 (buffer ,bufferSize ,structSize ); 112desc .structureSize = structSize ; 113 114readByte += decodeInt64 (buffer + readByte ,bufferSize - readByte ,desc .targetCount ); 115 116if (desc .targetCount > 0 ) 117 { 118 slang::TargetDesc * targets = 119 (slang::TargetDesc * )sessionDesc .allocate (sizeof (slang::TargetDesc )* desc .targetCount ); 120readByte += 121decodeStructArray (buffer + readByte ,bufferSize - readByte ,targets ,desc .targetCount ); 122desc .targets = targets ; 123 } 124 125readByte += decodeUint32 (buffer + readByte ,bufferSize - readByte ,desc .flags ); 126readByte += 127decodeEnumValue (buffer + readByte ,bufferSize - readByte ,desc .defaultMatrixLayoutMode ); 128readByte += decodeInt64 (buffer + readByte ,bufferSize - readByte ,desc .searchPathCount ); 129 130if (desc .searchPathCount > 0 ) 131 { 132char ** searchPaths = (char ** )sessionDesc .allocate (sizeof (char * )* desc .searchPathCount ); 133decodeStringArray ( 134buffer + readByte , 135bufferSize - readByte , 136searchPaths , 137desc .searchPathCount ); 138desc .searchPaths = searchPaths ; 139 } 140 141readByte += decodeInt64 (buffer + readByte ,bufferSize - readByte ,desc .preprocessorMacroCount ); 142if (desc .preprocessorMacroCount > 0 ) 143 { 144 slang::PreprocessorMacroDesc * macros = (slang::PreprocessorMacroDesc * )sessionDesc .allocate ( 145sizeof (slang::PreprocessorMacroDesc )* desc .preprocessorMacroCount ); 146readByte += decodeStructArray ( 147buffer + readByte , 148bufferSize - readByte , 149macros , 150desc .preprocessorMacroCount ); 151desc .preprocessorMacros = macros ; 152 } 153 154readByte += decodeBool (buffer + readByte ,bufferSize - readByte ,desc .enableEffectAnnotations ); 155readByte += decodeBool (buffer + readByte ,bufferSize - readByte ,desc .allowGLSLSyntax ); 156readByte += 157decodeUint32 (buffer + readByte ,bufferSize - readByte ,desc .compilerOptionEntryCount ); 158 159if (desc .compilerOptionEntryCount > 0 ) 160 { 161 slang::CompilerOptionEntry * entries = (slang::CompilerOptionEntry * )sessionDesc .allocate ( 162sizeof (slang::CompilerOptionEntry )* desc .compilerOptionEntryCount ); 163readByte += decodeStructArray ( 164buffer + readByte , 165bufferSize - readByte , 166entries , 167desc .compilerOptionEntryCount ); 168desc .compilerOptionEntries = entries ; 169 } 170 171return readByte ; 172} 173 174size_t ParameterDecoder ::decodeStruct ( 175const uint8_t * buffer , 176int64_t bufferSize , 177ValueDecoder < slang::PreprocessorMacroDesc >& desc ) 178{ 179SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 180 181size_t readByte = 0 ; 182PointerDecoder < char *> name ; 183PointerDecoder < char *> value ; 184 185readByte = decodeString (buffer ,bufferSize ,name ); 186readByte += decodeString (buffer + readByte ,bufferSize - readByte ,value ); 187 188desc .getValue ().name = name .getPointer (); 189desc .getValue ().value = value .getPointer (); 190 191return readByte ; 192} 193 194size_t ParameterDecoder ::decodeStruct ( 195const uint8_t * buffer , 196int64_t bufferSize , 197ValueDecoder < slang::CompilerOptionEntry >& entry ) 198{ 199SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 200 201size_t readByte = 0 ; 202readByte = decodeEnumValue (buffer ,bufferSize ,entry .getValue ().name ); 203 204ValueDecoder < slang::CompilerOptionValue > value ; 205readByte += decodeStruct (buffer + readByte ,bufferSize - readByte ,value ); 206entry .getValue ().value = value .getValue (); 207 208return readByte ; 209} 210 211size_t ParameterDecoder ::decodeStruct ( 212const uint8_t * buffer , 213int64_t bufferSize , 214ValueDecoder < slang::CompilerOptionValue >& value ) 215{ 216SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 217 218size_t readByte = 0 ; 219readByte = decodeEnumValue (buffer ,bufferSize ,value .getValue ().kind ); 220readByte += decodeInt32 (buffer + readByte ,bufferSize - readByte ,value .getValue ().intValue0 ); 221 222PointerDecoder < char *> stringValue0 ; 223readByte += decodeString (buffer + readByte ,bufferSize - readByte ,stringValue0 ); 224value .getValue ().stringValue0 = stringValue0 .getPointer (); 225 226PointerDecoder < char *> stringValue1 ; 227readByte += decodeString (buffer + readByte ,bufferSize - readByte ,stringValue1 ); 228value .getValue ().stringValue1 = stringValue1 .getPointer (); 229return 0 ; 230} 231 232size_t ParameterDecoder ::decodeStruct ( 233const uint8_t * buffer , 234int64_t bufferSize , 235ValueDecoder < slang::TargetDesc >& targetDesc ) 236{ 237SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 238 239size_t readByte = 0 ; 240uint64_t structSize = 0 ; 241readByte = decodeUint64 (buffer ,bufferSize ,structSize ); 242targetDesc .getValue ().structureSize = structSize ; 243 244readByte += 245decodeEnumValue (buffer + readByte ,bufferSize - readByte ,targetDesc .getValue ().format ); 246readByte += 247decodeEnumValue (buffer + readByte ,bufferSize - readByte ,targetDesc .getValue ().profile ); 248readByte += 249decodeEnumValue (buffer + readByte ,bufferSize - readByte ,targetDesc .getValue ().flags ); 250readByte += decodeEnumValue ( 251buffer + readByte , 252bufferSize - readByte , 253targetDesc .getValue ().floatingPointMode ); 254readByte += decodeEnumValue ( 255buffer + readByte , 256bufferSize - readByte , 257targetDesc .getValue ().lineDirectiveMode ); 258readByte += decodeBool ( 259buffer + readByte , 260bufferSize - readByte , 261targetDesc .getValue ().forceGLSLScalarBufferLayout ); 262readByte += decodeUint32 ( 263buffer + readByte , 264bufferSize - readByte , 265targetDesc .getValue ().compilerOptionEntryCount ); 266 267if (targetDesc .getValue ().compilerOptionEntryCount > 0 ) 268 { 269 slang::CompilerOptionEntry * entries = (slang::CompilerOptionEntry * )targetDesc .allocate ( 270sizeof (slang::CompilerOptionEntry )* targetDesc .getValue ().compilerOptionEntryCount ); 271readByte += decodeStructArray ( 272buffer + readByte , 273bufferSize - readByte , 274entries , 275targetDesc .getValue ().compilerOptionEntryCount ); 276targetDesc .getValue ().compilerOptionEntries = entries ; 277 } 278 279return readByte ; 280} 281 282size_t ParameterDecoder ::decodeStruct ( 283const uint8_t * buffer , 284int64_t bufferSize , 285ValueDecoder < slang::SpecializationArg >& specializationArg ) 286{ 287SLANG_RECORD_ASSERT ((buffer != nullptr )&& (bufferSize > 0 )); 288 289size_t readByte = 0 ; 290readByte = decodeEnumValue (buffer ,bufferSize ,specializationArg .getValue ().kind ); 291 292// TODO: Special handle to address decode is needed. 293uint64_t address = 0 ; 294readByte += decodeAddress (buffer + readByte ,bufferSize - readByte ,address ); 295 (void )address ; 296 297return readByte ; 298} 299}// namespace SlangRecord