Skip to content

Commit b46fb43

Browse files
committed
basic device
1 parent ad4bc6e commit b46fb43

File tree

5 files changed

+153
-13
lines changed

5 files changed

+153
-13
lines changed

externals/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ set(CMAKE_DISABLE_FIND_PACKAGE_FFTW3 ON)
1919
set(CMAKE_DISABLE_FIND_PACKAGE_Fontconfig ON)
2020
set(CMAKE_DISABLE_FIND_PACKAGE_HarfBuzz ON)
2121
set(CMAKE_DISABLE_FIND_PACKAGE_JBIG ON)
22+
set(CMAKE_DISABLE_FIND_PACKAGE_JsonCpp ON)
2223
set(CMAKE_DISABLE_FIND_PACKAGE_Libidn2 ON)
2324
set(CMAKE_DISABLE_FIND_PACKAGE_Libpsl ON)
2425
set(CMAKE_DISABLE_FIND_PACKAGE_Libssh2 ON)
@@ -27,9 +28,12 @@ set(CMAKE_DISABLE_FIND_PACKAGE_OpenGL ON)
2728
set(CMAKE_DISABLE_FIND_PACKAGE_PNG ON)
2829
set(CMAKE_DISABLE_FIND_PACKAGE_Perl ON)
2930
set(CMAKE_DISABLE_FIND_PACKAGE_PkgConfig ON)
30-
#set(CMAKE_DISABLE_FIND_PACKAGE_Python3 ON)
31+
set(CMAKE_DISABLE_FIND_PACKAGE_Python3 ON)
3132
set(CMAKE_DISABLE_FIND_PACKAGE_PythonInterp ON)
3233
set(CMAKE_DISABLE_FIND_PACKAGE_SndFile ON)
34+
set(CMAKE_DISABLE_FIND_PACKAGE_Vulkan ON)
35+
set(CMAKE_DISABLE_FIND_PACKAGE_VulkanHeaders ON)
36+
set(CMAKE_DISABLE_FIND_PACKAGE_glslc ON)
3337
set(CMAKE_DISABLE_FIND_PACKAGE_liblzma ON)
3438

3539
# speed up repeated cmake runs

externals/dawn/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(DAWN_ENABLE_WEBGPU_ON_WEBGPU OFF CACHE INTERNAL "Enable compilation of the W
1313
set(DAWN_ENABLE_DESKTOP_GL OFF CACHE INTERNAL "Enable compilation of the OpenGL backend" FORCE)
1414
set(DAWN_ENABLE_OPENGLES OFF CACHE INTERNAL "Enable compilation of the OpenGL ES backend" FORCE)
1515
set(DAWN_ENABLE_VULKAN ON CACHE INTERNAL "Enable compilation of the Vulkan backend" FORCE)
16+
set(DAWN_FORCE_SYSTEM_COMPONENT_LOAD ON CACHE INTERNAL "Allow system component fallback" FORCE)
1617
set(DAWN_USE_WAYLAND OFF CACHE INTERNAL "Enable support for Wayland surface" FORCE)
1718
set(DAWN_USE_X11 OFF CACHE INTERNAL "Enable support for X11 surface" FORCE)
1819
set(DAWN_USE_GLFW OFF CACHE INTERNAL "Enable compilation of the GLFW windowing utils" FORCE)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef guard_graphicsDevice_erzgukm78ndf
2+
#define guard_graphicsDevice_erzgukm78ndf
3+
4+
#include <cage-engine/core.h>
5+
6+
namespace cage
7+
{
8+
class CAGE_ENGINE_API GraphicsDevice : private Immovable
9+
{
10+
public:
11+
};
12+
13+
CAGE_ENGINE_API Holder<GraphicsDevice> newGraphicsDevice();
14+
}
15+
16+
#endif

sources/include/cage-engine/highPerformanceGpuHint.h

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include <array>
2+
#include <cstring> // std::strlen
3+
4+
#include <dawn/native/DawnNative.h>
5+
#include <webgpu/webgpu_cpp.h>
6+
7+
#include <cage-core/debug.h>
8+
#include <cage-engine/graphicsDevice.h>
9+
10+
namespace cage
11+
{
12+
namespace
13+
{
14+
String conv(wgpu::StringView v)
15+
{
16+
// todo check for truncation
17+
if (!v.data)
18+
return {};
19+
if (v.length == wgpu::kStrlen)
20+
return String({ v.data, v.data + std::strlen(v.data) });
21+
return String({ v.data, v.data + v.length });
22+
}
23+
24+
void logFromInstance(wgpu::LoggingType type, wgpu::StringView message)
25+
{
26+
SeverityEnum severity = SeverityEnum::Critical;
27+
switch (type)
28+
{
29+
case wgpu::LoggingType::Verbose:
30+
severity = SeverityEnum::Note;
31+
break;
32+
case wgpu::LoggingType::Info:
33+
severity = SeverityEnum::Info;
34+
break;
35+
case wgpu::LoggingType::Warning:
36+
severity = SeverityEnum::Warning;
37+
break;
38+
case wgpu::LoggingType::Error:
39+
severity = SeverityEnum::Error;
40+
break;
41+
}
42+
CAGE_LOG(severity, "wgpu", conv(message));
43+
}
44+
45+
void lostFromDevice(const wgpu::Device &device, wgpu::DeviceLostReason reason, wgpu::StringView message)
46+
{
47+
CAGE_LOG(SeverityEnum::Info, "graphics", "device lost");
48+
CAGE_LOG(SeverityEnum::Note, "wgpu", conv(message));
49+
}
50+
51+
void errorFromDevice(const wgpu::Device &device, wgpu::ErrorType error, wgpu::StringView message)
52+
{
53+
CAGE_LOG(SeverityEnum::Error, "graphics", "device error");
54+
CAGE_LOG(SeverityEnum::Error, "wgpu", conv(message));
55+
detail::debugBreakpoint();
56+
}
57+
58+
class GraphicsDeviceImpl : public GraphicsDevice
59+
{
60+
public:
61+
wgpu::Instance instance;
62+
wgpu::Device device;
63+
wgpu::Queue queue;
64+
65+
GraphicsDeviceImpl()
66+
{
67+
{
68+
dawn::native::DawnInstanceDescriptor ex;
69+
ex.SetLoggingCallback(logFromInstance);
70+
#ifdef CAGE_DEBUG
71+
ex.backendValidationLevel = dawn::native::BackendValidationLevel::Partial;
72+
#endif // CAGE_DEBUG
73+
wgpu::InstanceDescriptor desc;
74+
desc.nextInChain = &ex;
75+
std::array<wgpu::InstanceFeatureName, 2> features = { wgpu::InstanceFeatureName::TimedWaitAny, wgpu::InstanceFeatureName::ShaderSourceSPIRV };
76+
desc.requiredFeatureCount = features.size();
77+
desc.requiredFeatures = features.data();
78+
79+
instance = wgpu::CreateInstance(&desc);
80+
if (!instance)
81+
CAGE_THROW_ERROR(Exception, "failed to create wgpu instance");
82+
}
83+
84+
wgpu::Adapter adapter;
85+
{
86+
wgpu::RequestAdapterOptions opts;
87+
opts.backendType = wgpu::BackendType::Vulkan;
88+
opts.powerPreference = wgpu::PowerPreference::HighPerformance;
89+
opts.compatibleSurface = nullptr; // todo
90+
91+
wgpu::Future future = instance.RequestAdapter(&opts, wgpu::CallbackMode::WaitAnyOnly,
92+
[&](wgpu::RequestAdapterStatus status, wgpu::Adapter adapter_, wgpu::StringView message)
93+
{
94+
adapter = adapter_;
95+
CAGE_LOG(SeverityEnum::Info, "wgpu", conv(message));
96+
});
97+
instance.WaitAny(future, m);
98+
if (!adapter)
99+
CAGE_THROW_ERROR(Exception, "failed to create wgpu adapter");
100+
101+
wgpu::AdapterInfo info;
102+
adapter.GetInfo(&info);
103+
CAGE_LOG(SeverityEnum::Info, "graphics", Stringizer() + "adapter vendor: " + conv(info.vendor));
104+
CAGE_LOG(SeverityEnum::Info, "graphics", Stringizer() + "adapter device: " + conv(info.device));
105+
CAGE_LOG(SeverityEnum::Info, "graphics", Stringizer() + "adapter description: " + conv(info.description));
106+
}
107+
108+
{
109+
wgpu::DeviceDescriptor desc;
110+
desc.SetDeviceLostCallback(wgpu::CallbackMode::AllowProcessEvents, lostFromDevice);
111+
desc.SetUncapturedErrorCallback(errorFromDevice);
112+
113+
device = adapter.CreateDevice(&desc);
114+
if (!device)
115+
CAGE_THROW_ERROR(Exception, "failed to create wgpu device");
116+
}
117+
118+
{
119+
queue = device.GetQueue();
120+
if (!queue)
121+
CAGE_THROW_ERROR(Exception, "failed to create wgpu queue");
122+
}
123+
}
124+
};
125+
}
126+
127+
Holder<GraphicsDevice> newGraphicsDevice()
128+
{
129+
return systemMemory().createImpl<GraphicsDevice, GraphicsDeviceImpl>();
130+
}
131+
}

0 commit comments

Comments
 (0)