yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Yong HeLanguage version + tuple syntax. (#7230)faf042ecc

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