1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsCorePrerequisites.h"
6
7namespace bs
8{
9 /** @addtogroup RenderAPI
10 * @{
11 */
12
13 /** Describes a single GPU program data (for example int, float, Vector2) parameter. */
14 struct GpuParamDataDesc
15 {
16 String name;
17 UINT32 elementSize; /**< In multiples of 4 bytes. */
18 UINT32 arraySize;
19 UINT32 arrayElementStride; /**< In multiples of 4 bytes. */
20 GpuParamDataType type;
21
22 UINT32 paramBlockSlot;
23 UINT32 paramBlockSet;
24 UINT32 gpuMemOffset; /**< In multiples of 4 bytes, or index for parameters not in a buffer. */
25 UINT32 cpuMemOffset; /**< In multiples of 4 bytes. */
26 };
27
28 /** Describes a single GPU program object (for example texture, sampler state) parameter. */
29 struct GpuParamObjectDesc
30 {
31 String name;
32 GpuParamObjectType type;
33
34 /** Slot within a set. Uniquely identifies bind location in the GPU pipeline, together with the set. */
35 UINT32 slot;
36
37 /** Uniquely identifies the bind location in the GPU pipeline, together with the slot. */
38 UINT32 set;
39
40 /** Underlying type of individual elements in the buffer or texture. */
41 GpuBufferFormat elementType = BF_UNKNOWN;
42 };
43
44 /** Describes a GPU program parameter block (collection of GPU program data parameters). */
45 struct GpuParamBlockDesc
46 {
47 String name;
48 UINT32 slot; /** Slot within a set. Uniquely identifies bind location in the GPU pipeline, together with the set. */
49 UINT32 set; /** Uniquely identifies the bind location in the GPU pipeline, together with the slot. */
50 UINT32 blockSize; /**< In multiples of 4 bytes. */
51 bool isShareable; /** True for blocks that can be shared between different GPU pipeline stages. */
52 };
53
54 /** Contains all parameter information for a GPU program, including data and object parameters, plus parameter blocks. */
55 struct BS_CORE_EXPORT GpuParamDesc : IReflectable
56 {
57 Map<String, GpuParamBlockDesc> paramBlocks;
58 Map<String, GpuParamDataDesc> params;
59
60 Map<String, GpuParamObjectDesc> samplers;
61 Map<String, GpuParamObjectDesc> textures;
62 Map<String, GpuParamObjectDesc> loadStoreTextures;
63 Map<String, GpuParamObjectDesc> buffers;
64
65 /************************************************************************/
66 /* SERIALIZATION */
67 /************************************************************************/
68 public:
69 friend class GpuParamDescRTTI;
70 static RTTITypeBase* getRTTIStatic();
71 RTTITypeBase* getRTTI() const override;
72 };
73
74 /** @} */
75
76 /** @cond SPECIALIZATIONS */
77 /** @addtogroup RTTI-Impl-Core
78 * @{
79 */
80
81 template<> struct RTTIPlainType<GpuParamDataDesc>
82 {
83 enum { id = TID_GpuParamDataDesc }; enum { hasDynamicSize = 1 };
84 static constexpr UINT32 VERSION = 1;
85
86 static void toMemory(const GpuParamDataDesc& data, char* memory)
87 {
88 UINT32 size = getDynamicSize(data);
89
90 UINT32 curSize = sizeof(UINT32);
91 memcpy(memory, &size, curSize);
92 memory += curSize;
93
94 memory = rttiWriteElem(VERSION, memory);
95
96 memory = rttiWriteElem(data.name, memory);
97 memory = rttiWriteElem(data.elementSize, memory);
98 memory = rttiWriteElem(data.arraySize, memory);
99 memory = rttiWriteElem(data.arrayElementStride, memory);
100 memory = rttiWriteElem(data.type, memory);
101
102 memory = rttiWriteElem(data.paramBlockSlot, memory);
103 memory = rttiWriteElem(data.paramBlockSet, memory);
104 memory = rttiWriteElem(data.gpuMemOffset, memory);
105 rttiWriteElem(data.cpuMemOffset, memory);
106 }
107
108 static UINT32 fromMemory(GpuParamDataDesc& data, char* memory)
109 {
110 UINT32 size;
111 memcpy(&size, memory, sizeof(UINT32));
112 memory += sizeof(UINT32);
113
114 UINT32 version = 0;
115 memory = rttiReadElem(version, memory);
116 assert(version == VERSION);
117
118 memory = rttiReadElem(data.name, memory);
119 memory = rttiReadElem(data.elementSize, memory);
120 memory = rttiReadElem(data.arraySize, memory);
121 memory = rttiReadElem(data.arrayElementStride, memory);
122 memory = rttiReadElem(data.type, memory);
123
124 memory = rttiReadElem(data.paramBlockSlot, memory);
125 memory = rttiReadElem(data.paramBlockSet, memory);
126 memory = rttiReadElem(data.gpuMemOffset, memory);
127 rttiReadElem(data.cpuMemOffset, memory);
128
129 return size;
130 }
131
132 static UINT32 getDynamicSize(const GpuParamDataDesc& data)
133 {
134 UINT32 dataSize = rttiGetElemSize(VERSION) + rttiGetElemSize(data.name) + rttiGetElemSize(data.elementSize) +
135 rttiGetElemSize(data.arraySize) + rttiGetElemSize(data.arrayElementStride) + rttiGetElemSize(data.type) +
136 rttiGetElemSize(data.paramBlockSlot) + rttiGetElemSize(data.paramBlockSet) +
137 rttiGetElemSize(data.gpuMemOffset) + rttiGetElemSize(data.cpuMemOffset) + sizeof(UINT32);
138
139 return dataSize;
140 }
141 };
142
143 template<> struct RTTIPlainType<GpuParamObjectDesc>
144 {
145 enum { id = TID_GpuParamObjectDesc }; enum { hasDynamicSize = 1 };
146 static constexpr UINT32 VERSION = 2;
147
148 static void toMemory(const GpuParamObjectDesc& data, char* memory)
149 {
150 UINT32 size = getDynamicSize(data);
151
152 UINT32 curSize = sizeof(UINT32);
153 memcpy(memory, &size, curSize);
154 memory += curSize;
155
156 memory = rttiWriteElem(VERSION, memory);
157
158 memory = rttiWriteElem(data.name, memory);
159 memory = rttiWriteElem(data.type, memory);
160 memory = rttiWriteElem(data.slot, memory);
161 memory = rttiWriteElem(data.set, memory);
162 rttiWriteElem(data.elementType, memory);
163 }
164
165 static UINT32 fromMemory(GpuParamObjectDesc& data, char* memory)
166 {
167 UINT32 size;
168 memcpy(&size, memory, sizeof(UINT32));
169 memory += sizeof(UINT32);
170
171 UINT32 version = 0;
172 memory = rttiReadElem(version, memory);
173
174 memory = rttiReadElem(data.name, memory);
175 memory = rttiReadElem(data.type, memory);
176 memory = rttiReadElem(data.slot, memory);
177 memory = rttiReadElem(data.set, memory);
178
179 if(version > 1)
180 rttiReadElem(data.elementType, memory);
181
182 return size;
183 }
184
185 static UINT32 getDynamicSize(const GpuParamObjectDesc& data)
186 {
187 UINT32 dataSize = rttiGetElemSize(VERSION) + rttiGetElemSize(data.name) + rttiGetElemSize(data.type) +
188 rttiGetElemSize(data.slot) + rttiGetElemSize(data.set) + rttiGetElemSize(data.elementType) + sizeof(UINT32);
189
190 return dataSize;
191 }
192 };
193
194 template<> struct RTTIPlainType<GpuParamBlockDesc>
195 {
196 enum { id = TID_GpuParamBlockDesc }; enum { hasDynamicSize = 1 };
197 static constexpr UINT32 VERSION = 1;
198
199 static void toMemory(const GpuParamBlockDesc& data, char* memory)
200 {
201 UINT32 size = getDynamicSize(data);
202
203 UINT32 curSize = sizeof(UINT32);
204 memcpy(memory, &size, curSize);
205 memory += curSize;
206
207 memory = rttiWriteElem(VERSION, memory);
208
209 memory = rttiWriteElem(data.name, memory);
210 memory = rttiWriteElem(data.set, memory);
211 memory = rttiWriteElem(data.slot, memory);
212 memory = rttiWriteElem(data.blockSize, memory);
213 rttiWriteElem(data.isShareable, memory);
214 }
215
216 static UINT32 fromMemory(GpuParamBlockDesc& data, char* memory)
217 {
218 UINT32 size;
219 memcpy(&size, memory, sizeof(UINT32));
220 memory += sizeof(UINT32);
221
222 UINT32 version = 0;
223 memory = rttiReadElem(version, memory);
224 assert(version == VERSION);
225
226 memory = rttiReadElem(data.name, memory);
227 memory = rttiReadElem(data.set, memory);
228 memory = rttiReadElem(data.slot, memory);
229 memory = rttiReadElem(data.blockSize, memory);
230 rttiReadElem(data.isShareable, memory);
231
232 return size;
233 }
234
235 static UINT32 getDynamicSize(const GpuParamBlockDesc& data)
236 {
237 UINT32 dataSize = rttiGetElemSize(VERSION) + rttiGetElemSize(data.name) + rttiGetElemSize(data.set) +
238 rttiGetElemSize(data.slot) + rttiGetElemSize(data.blockSize) + rttiGetElemSize(data.isShareable) +
239 sizeof(UINT32);
240
241 return dataSize;
242 }
243 };
244
245 /** @} */
246 /** @endcond */
247}
248