Repository navigation
Expand file tree
/
Copy pathwebgpu_init_new.cpp
More file actions
120 lines (93 loc) · 3.91 KB
/
Copy pathwebgpu_init_new.cpp
File metadata and controls
120 lines (93 loc) · 3.91 KB
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
// Modern WebGPU initialization code for emdawnwebgpu
// This is a reference - will be integrated into GfxEngine.cpp
#ifdef __EMSCRIPTEN__
// Helper macros for string views
#define WGPU_STRING(s) ((WGPUStringView){s, SIZE_MAX})
std::cout << "[GfxEngine WASM] Initializing WebGPU Rendering..." << std::endl;
// Create WebGPU instance
WGPUInstanceDescriptor instanceDesc = {};
WGPUInstance instance = wgpuCreateInstance(&instanceDesc);
if (!instance) {
std::cerr << "[GfxEngine Error] Failed to create WebGPU instance!" << std::endl;
return false;
}
// Create canvas surface
WGPUEmscriptenSurfaceSourceCanvasHTMLSelector canvasSource = {};
canvasSource.chain.next = nullptr;
canvasSource.chain.sType = WGPUSType_EmscriptenSurfaceSourceCanvasHTMLSelector;
canvasSource.selector = WGPU_STRING("#canvas");
WGPUSurfaceDescriptor surfaceDesc = {};
surfaceDesc.nextInChain = &canvasSource.chain;
surfaceDesc.label = WGPU_STRING("main-surface");
m_wgpuSurface = wgpuInstanceCreateSurface(instance, &surfaceDesc);
// Request adapter
WGPURequestAdapterOptions adapterOpts = {};
adapterOpts.compatibleSurface = m_wgpuSurface;
struct AdapterUserData {
WGPUAdapter adapter = nullptr;
bool ready = false;
};
AdapterUserData adapterData;
WGPURequestAdapterCallbackInfo adapterCallbackInfo = {};
adapterCallbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
adapterCallbackInfo.callback = [](WGPURequestAdapterStatus status, WGPUAdapter adapter,
WGPUStringView message, void* userdata1, void* userdata2) {
auto* data = static_cast<AdapterUserData*>(userdata1);
if (status == WGPURequestAdapterStatus_Success) {
data->adapter = adapter;
}
data->ready = true;
};
adapterCallbackInfo.userdata1 = &adapterData;
wgpuInstanceRequestAdapter(instance, &adapterOpts, adapterCallbackInfo);
if (!adapterData.ready || !adapterData.adapter) {
std::cerr << "[GfxEngine Error] Failed to get WebGPU adapter!" << std::endl;
return false;
}
// Request device
WGPUDeviceDescriptor deviceDesc = {};
deviceDesc.label = WGPU_STRING("main-device");
struct DeviceUserData {
WGPUDevice device = nullptr;
bool ready = false;
};
DeviceUserData deviceData;
WGPURequestDeviceCallbackInfo deviceCallbackInfo = {};
deviceCallbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
deviceCallbackInfo.callback = [](WGPURequestDeviceStatus status, WGPUDevice device,
WGPUStringView message, void* userdata1, void* userdata2) {
auto* data = static_cast<DeviceUserData*>(userdata1);
if (status == WGPURequestDeviceStatus_Success) {
data->device = device;
}
data->ready = true;
};
deviceCallbackInfo.userdata1 = &deviceData;
wgpuAdapterRequestDevice(adapterData.adapter, &deviceDesc, deviceCallbackInfo);
if (!deviceData.ready || !deviceData.device) {
std::cerr << "[GfxEngine Error] Failed to get WebGPU device!" << std::endl;
return false;
}
m_wgpuDevice = deviceData.device;
// Configure surface (replaces swap chain)
WGPUSurfaceConfiguration surfaceConfig = {};
surfaceConfig.device = m_wgpuDevice;
surfaceConfig.format = WGPUTextureFormat_BGRA8Unorm;
surfaceConfig.usage = WGPUTextureUsage_RenderAttachment;
surfaceConfig.width = 1280;
surfaceConfig.height = 720;
surfaceConfig.presentMode = WGPUPresentMode_Fifo;
surfaceConfig.alphaMode = WGPUCompositeAlphaMode_Auto;
wgpuSurfaceConfigure(m_wgpuSurface, &surfaceConfig);
m_wgpuSwapChainFormat = WGPUTextureFormat_BGRA8Unorm;
// Create shader module with WGSL
WGPUShaderSourceWGSL wgslSource = {};
wgslSource.chain.next = nullptr;
wgslSource.chain.sType = WGPUSType_ShaderSourceWGSL;
wgslSource.code = WGPU_STRING(wgslShaderSrc);
WGPUShaderModuleDescriptor shaderModuleDesc = {};
shaderModuleDesc.nextInChain = &wgslSource.chain;
shaderModuleDesc.label = WGPU_STRING("pbr-shader");
m_wgpuShaderModule = wgpuDeviceCreateShaderModule(m_wgpuDevice, &shaderModuleDesc);
// ... rest of initialization continues as before ...
#endif