summaryrefslogtreecommitdiff
path: root/cpp-timeplot/src/main.cpp
diff options
context:
space:
mode:
authorgrothedev <grothedev@gmail.com>2025-10-02 10:25:04 -0400
committergrothedev <grothedev@gmail.com>2025-10-02 10:25:04 -0400
commit836459dce3f50767d41978be4a2f7ac788e6a9ba (patch)
tree9fd17f4f7e0bb808f8467a14932355e3e72875ef /cpp-timeplot/src/main.cpp
parenta162c98ce54159e3e7dbe867d908ce3276b7f633 (diff)
added metrics for rust impl. having trouble with c++ atm
Diffstat (limited to 'cpp-timeplot/src/main.cpp')
-rw-r--r--cpp-timeplot/src/main.cpp105
1 files changed, 86 insertions, 19 deletions
diff --git a/cpp-timeplot/src/main.cpp b/cpp-timeplot/src/main.cpp
index 7afd3e8..35ae051 100644
--- a/cpp-timeplot/src/main.cpp
+++ b/cpp-timeplot/src/main.cpp
@@ -1,5 +1,5 @@
#include <GLFW/glfw3.h>
-#include <webgpu/webgpu.hpp>
+#include <webgpu/webgpu.h>
#include <glfw3webgpu.h>
#include <iostream>
#include <memory>
@@ -12,7 +12,8 @@ constexpr int WINDOW_HEIGHT = 720;
class Application {
public:
- Application() : window_(nullptr) {}
+ Application() : window_(nullptr), instance_(nullptr), device_(nullptr),
+ surface_(nullptr), adapter_(nullptr) {}
~Application() {
cleanup();
@@ -61,9 +62,30 @@ public:
}
private:
+ static void onAdapterRequestEnded(WGPURequestAdapterStatus status, WGPUAdapter adapter,
+ WGPUStringView message, void* userdata1, void* userdata2) {
+ if (status == WGPURequestAdapterStatus_Success) {
+ *static_cast<WGPUAdapter*>(userdata1) = adapter;
+ } else {
+ std::cerr << "Failed to get adapter: " << std::string(message.data, message.length) << std::endl;
+ }
+ }
+
+ static void onDeviceRequestEnded(WGPURequestDeviceStatus status, WGPUDevice device,
+ WGPUStringView message, void* userdata1, void* userdata2) {
+ if (status == WGPURequestDeviceStatus_Success) {
+ *static_cast<WGPUDevice*>(userdata1) = device;
+ } else {
+ std::cerr << "Failed to get device: " << std::string(message.data, message.length) << std::endl;
+ }
+ }
+
bool initWebGPU() {
// Create instance
- instance_ = wgpu::createInstance(wgpu::InstanceDescriptor{});
+ WGPUInstanceDescriptor instanceDesc = {};
+ instanceDesc.nextInChain = nullptr;
+
+ instance_ = wgpuCreateInstance(&instanceDesc);
if (!instance_) {
std::cerr << "Failed to create WebGPU instance" << std::endl;
return false;
@@ -76,23 +98,50 @@ private:
return false;
}
- // Request adapter (synchronous version)
- wgpu::RequestAdapterOptions adapterOpts{};
+ // Request adapter with callback
+ WGPURequestAdapterOptions adapterOpts = {};
+ adapterOpts.nextInChain = nullptr;
adapterOpts.compatibleSurface = surface_;
+ adapterOpts.powerPreference = WGPUPowerPreference_HighPerformance;
- wgpu::Adapter adapter = instance_.requestAdapter(adapterOpts);
- if (!adapter) {
- std::cerr << "Failed to get adapter" << std::endl;
- return false;
- }
+ WGPURequestAdapterCallbackInfo adapterCallbackInfo = {};
+ adapterCallbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
+ adapterCallbackInfo.callback = onAdapterRequestEnded;
+ adapterCallbackInfo.userdata1 = &adapter_;
+ adapterCallbackInfo.userdata2 = nullptr;
- // Request device (synchronous version)
- wgpu::DeviceDescriptor deviceDesc{};
- device_ = adapter.requestDevice(deviceDesc);
+ wgpuInstanceRequestAdapter(instance_, &adapterOpts, adapterCallbackInfo);
- if (!device_) {
- std::cerr << "Failed to get device" << std::endl;
- return false;
+ // Process events until adapter is ready
+ while (!adapter_) {
+ wgpuInstanceProcessEvents(instance_);
+ }
+
+ // Request device with callback
+ WGPUDeviceDescriptor deviceDesc = {};
+ deviceDesc.nextInChain = nullptr;
+ deviceDesc.label = {nullptr, WGPU_STRLEN};
+ deviceDesc.requiredFeatureCount = 0;
+ deviceDesc.requiredLimits = nullptr;
+ deviceDesc.defaultQueue.nextInChain = nullptr;
+ deviceDesc.defaultQueue.label = {nullptr, WGPU_STRLEN};
+ deviceDesc.deviceLostCallbackInfo.nextInChain = nullptr;
+ deviceDesc.deviceLostCallbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
+ deviceDesc.deviceLostCallbackInfo.callback = nullptr;
+ deviceDesc.uncapturedErrorCallbackInfo.nextInChain = nullptr;
+ deviceDesc.uncapturedErrorCallbackInfo.callback = nullptr;
+
+ WGPURequestDeviceCallbackInfo deviceCallbackInfo = {};
+ deviceCallbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
+ deviceCallbackInfo.callback = onDeviceRequestEnded;
+ deviceCallbackInfo.userdata1 = &device_;
+ deviceCallbackInfo.userdata2 = nullptr;
+
+ wgpuAdapterRequestDevice(adapter_, &deviceDesc, deviceCallbackInfo);
+
+ // Process events until device is ready
+ while (!device_) {
+ wgpuInstanceProcessEvents(instance_);
}
return true;
@@ -101,6 +150,23 @@ private:
void cleanup() {
renderer_.reset();
+ if (device_) {
+ wgpuDeviceRelease(device_);
+ device_ = nullptr;
+ }
+ if (adapter_) {
+ wgpuAdapterRelease(adapter_);
+ adapter_ = nullptr;
+ }
+ if (surface_) {
+ wgpuSurfaceRelease(surface_);
+ surface_ = nullptr;
+ }
+ if (instance_) {
+ wgpuInstanceRelease(instance_);
+ instance_ = nullptr;
+ }
+
if (window_) {
glfwDestroyWindow(window_);
window_ = nullptr;
@@ -125,9 +191,10 @@ private:
}
GLFWwindow* window_;
- wgpu::Instance instance_;
- wgpu::Device device_;
- wgpu::Surface surface_;
+ WGPUInstance instance_;
+ WGPUAdapter adapter_;
+ WGPUDevice device_;
+ WGPUSurface surface_;
std::unique_ptr<Renderer> renderer_;
};