-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
164 lines (144 loc) · 5.46 KB
/
utils.cpp
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
#include "utils.h"
#include <iostream>
#include <cassert>
#include <fstream>
#include <vector>
namespace {
std::string adapterTypeToString(WGPUAdapterType type) {
switch (type) {
case WGPUAdapterType_DiscreteGPU:
return "Discrete GPU";
case WGPUAdapterType_IntegratedGPU:
return "Integrated GPU";
case WGPUAdapterType_CPU:
return "CPU";
case WGPUAdapterType_Unknown:
return "Unknown";
case WGPUAdapterType_Force32:
return "Force32";
default:
return "Invalid";
}
}
std::string adapterBackendToString(WGPUBackendType backend) {
switch (backend) {
case WGPUBackendType_Undefined:
return "Undefined";
case WGPUBackendType_Null:
return "Null";
case WGPUBackendType_WebGPU:
return "WebGPU";
case WGPUBackendType_D3D11:
return "D3D11";
case WGPUBackendType_D3D12:
return "D3D12";
case WGPUBackendType_Metal:
return "Metal";
case WGPUBackendType_Vulkan:
return "Vulkan";
case WGPUBackendType_OpenGL:
return "OpenGL";
case WGPUBackendType_OpenGLES:
return "OpenGLES";
case WGPUBackendType_Force32:
return "Force32";
default:
return "Invalid";
}
}
}
namespace Utils {
WGPUAdapter requestAdapter(WGPUInstance instance,
const WGPURequestAdapterOptions *options) {
struct UserData {
WGPUAdapter adapter = nullptr;
bool requestEnded = false;
};
UserData userData;
auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status,
WGPUAdapter adapter, const char *message,
void *pUserData) {
UserData &userData = *reinterpret_cast<UserData *>(pUserData);
if (status == WGPURequestAdapterStatus_Success) {
userData.adapter = adapter;
} else {
std::cout << "Could not get WebGPU adapter: " << message << std::endl;
}
userData.requestEnded = true;
};
wgpuInstanceRequestAdapter(instance, options, onAdapterRequestEnded,
(void *)&userData);
assert(userData.requestEnded);
return userData.adapter;
}
WGPUDevice requestDevice(WGPUAdapter adapter, const WGPUDeviceDescriptor *descriptor)
{
struct UserData {
WGPUDevice device = nullptr;
bool requestEnded = false;
};
UserData userData;
auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status,
WGPUDevice device,
const char *message,
void *pUserData){
UserData& userData = *reinterpret_cast<UserData*>(pUserData);
if (status == WGPURequestDeviceStatus_Success) {
userData.device = device;
} else {
std::cout << "Could not get WebGPU device: " << message << std::endl;
}
userData.requestEnded = true;
};
wgpuAdapterRequestDevice(adapter, descriptor, onDeviceRequestEnded, (void*)&userData);
assert(userData.requestEnded);
// Get adapter properties
WGPUAdapterProperties properties{};
wgpuAdapterGetProperties(adapter, &properties);
std::cout << "Adapter name: " << properties.name << std::endl;
std::cout << "Adapter vendor: " << properties.vendorName << std::endl;
std::cout << "Adapter device id: " << properties.deviceID << std::endl;
std::cout << "Adapter driver version: " << properties.driverDescription << std::endl;
std::cout << "Adapter type: " << adapterTypeToString(properties.adapterType) << std::endl;
std::cout << "Adapter backend type: " << adapterBackendToString(properties.backendType) << std::endl;
std::cout << "Adapter compatibility mode: " << properties.compatibilityMode << std::endl;
return userData.device;
}
WGPUShaderModule loadShaderModule(const std::filesystem::path& filePath, WGPUDevice device)
{
std::ifstream file(filePath);
if(!file.is_open()){
std::cerr << "Could not open file: " << filePath << std::endl;
return nullptr;
}
file.seekg(0, std::ios::end);
std::string buffer(file.tellg(), ' ');
file.seekg(0);
file.read(buffer.data(), buffer.size());
WGPUShaderModuleWGSLDescriptor shaderCodeDesc = {};
shaderCodeDesc.chain.next = nullptr;
shaderCodeDesc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
shaderCodeDesc.code = buffer.data();
WGPUShaderModuleDescriptor shaderDesc{};
shaderDesc.nextInChain = &shaderCodeDesc.chain;
return wgpuDeviceCreateShaderModule(device, &shaderDesc);
}
WGPUBindGroupLayoutEntry createDefaultBindingLayout ()
{
WGPUBindGroupLayoutEntry bindingLayout;
bindingLayout.buffer.nextInChain = nullptr;
bindingLayout.buffer.type = WGPUBufferBindingType_Undefined;
bindingLayout.buffer.hasDynamicOffset = false;
bindingLayout.sampler.nextInChain = nullptr;
bindingLayout.sampler.type = WGPUSamplerBindingType_Undefined;
bindingLayout.storageTexture.nextInChain = nullptr;
bindingLayout.storageTexture.access = WGPUStorageTextureAccess_Undefined;
bindingLayout.storageTexture.format = WGPUTextureFormat_Undefined;
bindingLayout.storageTexture.viewDimension = WGPUTextureViewDimension_Undefined;
bindingLayout.texture.nextInChain = nullptr;
bindingLayout.texture.multisampled = false;
bindingLayout.texture.sampleType = WGPUTextureSampleType_Undefined;
bindingLayout.texture.viewDimension = WGPUTextureViewDimension_Undefined;
return bindingLayout;
}
} // namespace Utils