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
|
// render.cpp
#include "renderer-shared.h"
#include "../../source/core/slang-math.h"
#include "d3d11/render-d3d11.h"
#include "d3d12/render-d3d12.h"
#include "open-gl/render-gl.h"
#include "vulkan/render-vk.h"
#include "cuda/render-cuda.h"
#include <cstring>
namespace gfx {
using namespace Slang;
static const IResource::DescBase s_emptyDescBase = {};
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Global Renderer Functions !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
static const uint8_t s_formatSize[] = {
0, // Unknown,
uint8_t(sizeof(float) * 4), // RGBA_Float32,
uint8_t(sizeof(float) * 3), // RGB_Float32,
uint8_t(sizeof(float) * 2), // RG_Float32,
uint8_t(sizeof(float) * 1), // R_Float32,
uint8_t(sizeof(uint32_t)), // RGBA_Unorm_UInt8,
uint8_t(sizeof(uint32_t)), // BGRA_Unorm_UInt8,
uint8_t(sizeof(uint16_t)), // R_UInt16,
uint8_t(sizeof(uint32_t)), // R_UInt32,
uint8_t(sizeof(float)), // D_Float32,
uint8_t(sizeof(uint32_t)), // D_Unorm24_S8,
};
static void _compileTimeAsserts()
{
SLANG_COMPILE_TIME_ASSERT(SLANG_COUNT_OF(s_formatSize) == int(Format::CountOf));
}
extern "C"
{
size_t SLANG_MCALL gfxGetFormatSize(Format format)
{
return s_formatSize[int(format)];
}
SLANG_GFX_API SlangResult SLANG_MCALL
gfxCreateDevice(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 (gfxCreateDevice(&newDesc, outDevice) == SLANG_OK)
return SLANG_OK;
newDesc.deviceType = DeviceType::Vulkan;
if (gfxCreateDevice(&newDesc, outDevice) == SLANG_OK)
return SLANG_OK;
newDesc.deviceType = DeviceType::DirectX11;
if (gfxCreateDevice(&newDesc, outDevice) == SLANG_OK)
return SLANG_OK;
newDesc.deviceType = DeviceType::OpenGl;
if (gfxCreateDevice(&newDesc, outDevice) == SLANG_OK)
return SLANG_OK;
return SLANG_FAIL;
}
break;
#endif
default:
return SLANG_FAIL;
}
}
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
|