|
| 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