1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
// render.cpp
#include "renderer-shared.h"
#include "../../source/core/slang-math.h"
#include "d3d11/render-d3d11.h"
#include "open-gl/render-gl.h"
#include "vulkan/render-vk.h"
#include "cuda/render-cuda.h"
#include "cpu/render-cpu.h"
#include "debug-layer.h"
#include <cstring>
namespace gfx {
using namespace Slang;
Result SLANG_MCALL createD3D12Device(const IDevice::Desc* desc, IDevice** outDevice);
static bool debugLayerEnabled = false;
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Global Renderer Functions !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
#define GFX_FORMAT_SIZE(name, blockSizeInBytes, pixelsPerBlock) {blockSizeInBytes, pixelsPerBlock},
static const uint32_t s_formatSizeInfo[][2] =
{
GFX_FORMAT(GFX_FORMAT_SIZE)
};
static bool _checkFormat()
{
Index value = 0;
Index count = 0;
// Check the values are in the same order
#define GFX_FORMAT_CHECK(name, blockSizeInBytes, pixelsPerblock) count += Index(Index(Format::name) == value++);
GFX_FORMAT(GFX_FORMAT_CHECK)
const bool r = (count == Index(Format::CountOf));
SLANG_ASSERT(r);
return r;
}
// We don't make static because we will get a warning that it's unused
static const bool _checkFormatResult = _checkFormat();
struct FormatInfoMap
{
FormatInfoMap()
{
// Set all to nothing initially
for (auto& info : m_infos)
{
info.channelCount = 0;
info.channelType = SLANG_SCALAR_TYPE_NONE;
}
set(Format::R32G32B32A32_TYPELESS, SLANG_SCALAR_TYPE_UINT32, 4);
set(Format::R32G32B32_TYPELESS, SLANG_SCALAR_TYPE_UINT32, 3);
set(Format::R32G32_TYPELESS, SLANG_SCALAR_TYPE_UINT32, 2);
set(Format::R32_TYPELESS, SLANG_SCALAR_TYPE_UINT32, 1);
set(Format::R16G16B16A16_TYPELESS, SLANG_SCALAR_TYPE_UINT16, 4);
set(Format::R16G16_TYPELESS, SLANG_SCALAR_TYPE_UINT16, 2);
set(Format::R16_TYPELESS, SLANG_SCALAR_TYPE_UINT16, 1);
set(Format::R8G8B8A8_TYPELESS, SLANG_SCALAR_TYPE_UINT8, 4);
set(Format::R8G8_TYPELESS, SLANG_SCALAR_TYPE_UINT8, 2);
set(Format::R8_TYPELESS, SLANG_SCALAR_TYPE_UINT8, 1);
set(Format::B8G8R8A8_TYPELESS, SLANG_SCALAR_TYPE_UINT8, 4);
set(Format::R32G32B32A32_FLOAT, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::R32G32B32_FLOAT, SLANG_SCALAR_TYPE_FLOAT32, 3);
set(Format::R32G32_FLOAT, SLANG_SCALAR_TYPE_FLOAT32, 2);
set(Format::R32_FLOAT, SLANG_SCALAR_TYPE_FLOAT32, 1);
set(Format::R16G16B16A16_FLOAT, SLANG_SCALAR_TYPE_FLOAT16, 4);
set(Format::R16G16_FLOAT, SLANG_SCALAR_TYPE_FLOAT16, 2);
set(Format::R16_FLOAT, SLANG_SCALAR_TYPE_FLOAT16, 1);
set(Format::R32G32B32A32_UINT, SLANG_SCALAR_TYPE_UINT32, 4);
set(Format::R32G32B32_UINT, SLANG_SCALAR_TYPE_UINT32, 3);
set(Format::R32G32_UINT, SLANG_SCALAR_TYPE_UINT32, 2);
set(Format::R32_UINT, SLANG_SCALAR_TYPE_UINT32, 1);
set(Format::R16G16B16A16_UINT, SLANG_SCALAR_TYPE_UINT16, 4);
set(Format::R16G16_UINT, SLANG_SCALAR_TYPE_UINT16, 2);
set(Format::R16_UINT, SLANG_SCALAR_TYPE_UINT16, 1);
set(Format::R8G8B8A8_UINT, SLANG_SCALAR_TYPE_UINT8, 4);
set(Format::R8G8_UINT, SLANG_SCALAR_TYPE_UINT8, 2);
set(Format::R8_UINT, SLANG_SCALAR_TYPE_UINT8, 1);
set(Format::R32G32B32A32_SINT, SLANG_SCALAR_TYPE_INT32, 4);
set(Format::R32G32B32_SINT, SLANG_SCALAR_TYPE_INT32, 3);
set(Format::R32G32_SINT, SLANG_SCALAR_TYPE_INT32, 2);
set(Format::R32_SINT, SLANG_SCALAR_TYPE_INT32, 1);
set(Format::R16G16B16A16_SINT, SLANG_SCALAR_TYPE_INT16, 4);
set(Format::R16G16_SINT, SLANG_SCALAR_TYPE_INT16, 2);
set(Format::R16_SINT, SLANG_SCALAR_TYPE_INT16, 1);
set(Format::R8G8B8A8_SINT, SLANG_SCALAR_TYPE_INT8, 4);
set(Format::R8G8_SINT, SLANG_SCALAR_TYPE_INT8, 2);
set(Format::R8_SINT, SLANG_SCALAR_TYPE_INT8, 1);
set(Format::R16G16B16A16_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::R16G16_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 2);
set(Format::R16_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 1);
set(Format::R8G8B8A8_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::R8G8B8A8_UNORM_SRGB, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::R8G8_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 2);
set(Format::R8_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 1);
set(Format::B8G8R8A8_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::B8G8R8A8_UNORM_SRGB, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::B8G8R8X8_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::B8G8R8X8_UNORM_SRGB, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::R16G16B16A16_SNORM, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::R16G16_SNORM, SLANG_SCALAR_TYPE_FLOAT32, 2);
set(Format::R16_SNORM, SLANG_SCALAR_TYPE_FLOAT32, 1);
set(Format::R8G8B8A8_SNORM, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::R8G8_SNORM, SLANG_SCALAR_TYPE_FLOAT32, 2);
set(Format::R8_SNORM, SLANG_SCALAR_TYPE_FLOAT32, 1);
set(Format::D32_FLOAT, SLANG_SCALAR_TYPE_FLOAT32, 1);
set(Format::D16_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 1);
set(Format::B4G4R4A4_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::B5G6R5_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 3);
set(Format::B5G5R5A1_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::R9G9B9E5_SHAREDEXP, SLANG_SCALAR_TYPE_FLOAT32, 3);
set(Format::R10G10B10A2_TYPELESS, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::R10G10B10A2_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 4);
set(Format::R10G10B10A2_UINT, SLANG_SCALAR_TYPE_UINT32, 4);
set(Format::R11G11B10_FLOAT, SLANG_SCALAR_TYPE_FLOAT32, 3);
set(Format::BC1_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 4, 4, 4);
set(Format::BC1_UNORM_SRGB, SLANG_SCALAR_TYPE_FLOAT32, 4, 4, 4);
set(Format::BC2_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 4, 4, 4);
set(Format::BC2_UNORM_SRGB, SLANG_SCALAR_TYPE_FLOAT32, 4, 4, 4);
set(Format::BC3_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 4, 4, 4);
set(Format::BC3_UNORM_SRGB, SLANG_SCALAR_TYPE_FLOAT32, 4, 4, 4);
set(Format::BC4_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 1, 4, 4);
set(Format::BC4_SNORM, SLANG_SCALAR_TYPE_FLOAT32, 1, 4, 4);
set(Format::BC5_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 2, 4, 4);
set(Format::BC5_SNORM, SLANG_SCALAR_TYPE_FLOAT32, 2, 4, 4);
set(Format::BC6H_UF16, SLANG_SCALAR_TYPE_FLOAT32, 3, 4, 4);
set(Format::BC6H_SF16, SLANG_SCALAR_TYPE_FLOAT32, 3, 4, 4);
set(Format::BC7_UNORM, SLANG_SCALAR_TYPE_FLOAT32, 4, 4, 4);
set(Format::BC7_UNORM_SRGB, SLANG_SCALAR_TYPE_FLOAT32, 4, 4, 4);
}
void set(Format format, SlangScalarType type, Index channelCount, uint32_t blockWidth = 1, uint32_t blockHeight = 1)
{
FormatInfo& info = m_infos[Index(format)];
info.channelCount = uint8_t(channelCount);
info.channelType = uint8_t(type);
auto sizeInfo = s_formatSizeInfo[Index(format)];
info.blockSizeInBytes = sizeInfo[0];
info.pixelsPerBlock = sizeInfo[1];
info.blockWidth = blockWidth;
info.blockHeight = blockHeight;
}
const FormatInfo& get(Format format) const { return m_infos[Index(format)]; }
FormatInfo m_infos[Index(Format::CountOf)];
};
static const FormatInfoMap s_formatInfoMap;
static void _compileTimeAsserts()
{
SLANG_COMPILE_TIME_ASSERT(SLANG_COUNT_OF(s_formatSizeInfo) == int(Format::CountOf));
}
extern "C"
{
SLANG_GFX_API bool gfxIsCompressedFormat(Format format)
{
switch (format)
{
case Format::BC1_UNORM:
case Format::BC1_UNORM_SRGB:
case Format::BC2_UNORM:
case Format::BC2_UNORM_SRGB:
case Format::BC3_UNORM:
case Format::BC3_UNORM_SRGB:
case Format::BC4_UNORM:
case Format::BC4_SNORM:
case Format::BC5_UNORM:
case Format::BC5_SNORM:
case Format::BC6H_UF16:
case Format::BC6H_SF16:
case Format::BC7_UNORM:
case Format::BC7_UNORM_SRGB:
return true;
default:
return false;
}
}
SLANG_GFX_API bool gfxIsTypelessFormat(Format format)
{
switch (format)
{
case Format::R32G32B32A32_TYPELESS:
case Format::R32G32B32_TYPELESS:
case Format::R32G32_TYPELESS:
case Format::R32_TYPELESS:
case Format::R16G16B16A16_TYPELESS:
case Format::R16G16_TYPELESS:
case Format::R16_TYPELESS:
case Format::R8G8B8A8_TYPELESS:
case Format::R8G8_TYPELESS:
case Format::R8_TYPELESS:
case Format::B8G8R8A8_TYPELESS:
case Format::R10G10B10A2_TYPELESS:
return true;
default:
return false;
}
}
SLANG_GFX_API SlangResult gfxGetFormatInfo(Format format, FormatInfo* outInfo)
{
*outInfo = s_formatInfoMap.get(format);
return SLANG_OK;
}
SlangResult _createDevice(const IDevice::Desc* desc, IDevice** outDevice)
{
switch (desc->deviceType)
{
#if SLANG_WINDOWS_FAMILY
case DeviceType::DirectX11:
{
return createD3D11Device(desc, outDevice);
}
case DeviceType::DirectX12:
{
return createD3D12Device(desc, outDevice);
}
case DeviceType::OpenGl:
{
return createGLDevice(desc, outDevice);
}
case DeviceType::Vulkan:
{
return createVKDevice(desc, outDevice);
}
case DeviceType::CUDA:
{
return createCUDADevice(desc, outDevice);
}
case DeviceType::Default:
{
IDevice::Desc newDesc = *desc;
newDesc.deviceType = DeviceType::DirectX12;
if (_createDevice(&newDesc, outDevice) == SLANG_OK)
return SLANG_OK;
newDesc.deviceType = DeviceType::Vulkan;
if (_createDevice(&newDesc, outDevice) == SLANG_OK)
return SLANG_OK;
newDesc.deviceType = DeviceType::DirectX11;
if (_createDevice(&newDesc, outDevice) == SLANG_OK)
return SLANG_OK;
newDesc.deviceType = DeviceType::OpenGl;
if (_createDevice(&newDesc, outDevice) == SLANG_OK)
return SLANG_OK;
return SLANG_FAIL;
}
break;
#elif SLANG_LINUX_FAMILY && !defined(__CYGWIN__)
case DeviceType::Default:
case DeviceType::Vulkan:
{
return createVKDevice(desc, outDevice);
}
case DeviceType::CUDA:
{
return createCUDADevice(desc, outDevice);
}
#endif
case DeviceType::CPU:
{
return createCPUDevice(desc, outDevice);
}
break;
default:
return SLANG_FAIL;
}
}
SLANG_GFX_API SlangResult SLANG_MCALL
gfxCreateDevice(const IDevice::Desc* desc, IDevice** outDevice)
{
ComPtr<IDevice> innerDevice;
auto resultCode = _createDevice(desc, innerDevice.writeRef());
if (SLANG_FAILED(resultCode))
return resultCode;
if (!debugLayerEnabled)
{
returnComPtr(outDevice, innerDevice);
return resultCode;
}
RefPtr<DebugDevice> debugDevice = new DebugDevice();
debugDevice->baseObject = innerDevice;
returnComPtr(outDevice, debugDevice);
return resultCode;
}
SLANG_GFX_API SlangResult SLANG_MCALL gfxSetDebugCallback(IDebugCallback* callback)
{
_getDebugCallback() = callback;
return SLANG_OK;
}
SLANG_GFX_API void SLANG_MCALL gfxEnableDebugLayer()
{
debugLayerEnabled = true;
}
const char* SLANG_MCALL gfxGetDeviceTypeName(DeviceType type)
{
switch (type)
{
case gfx::DeviceType::Unknown:
return "Unknown";
case gfx::DeviceType::Default:
return "Default";
case gfx::DeviceType::DirectX11:
return "DirectX11";
case gfx::DeviceType::DirectX12:
return "DirectX12";
case gfx::DeviceType::OpenGl:
return "OpenGL";
case gfx::DeviceType::Vulkan:
return "Vulkan";
case gfx::DeviceType::CPU:
return "CPU";
case gfx::DeviceType::CUDA:
return "CUDA";
default:
return "?";
}
}
void SLANG_MCALL gfxGetIdentityProjection(ProjectionStyle style, float projMatrix[16])
{
switch (style)
{
case ProjectionStyle::DirectX:
case ProjectionStyle::OpenGl:
{
static const float kIdentity[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
::memcpy(projMatrix, kIdentity, sizeof(kIdentity));
break;
}
case ProjectionStyle::Vulkan:
{
static const float kIdentity[] = {1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
::memcpy(projMatrix, kIdentity, sizeof(kIdentity));
break;
}
default:
{
assert(!"Not handled");
}
}
}
}
} // renderer_test
|