yum-mirror/slang

Making it easier to work with shaders

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

Yong HeCache and reuse glsl module. (#6152)8000e0ede

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