EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Loading...
Searching...
No Matches
echomap::SurfaceFactory Class Reference

Provides static helpers to create and bind a WebGPU Surface to a GLFW window across Wasm and non-Wasm platforms. More...

#include <SurfaceFactory.hpp>

Static Public Member Functions

static wgpu::Surface create_surface (const wgpu::Instance &instance, GLFWwindow *window)
 Creates and binds a Surface to the given GLFW window.

Static Private Member Functions

static std::unique_ptr< wgpu::ChainedStruct > get_surface_descriptor (GLFWwindow *window)
 Produce an owning container of the WebGPU Surface descriptor for the given GLFW window.

Detailed Description

Provides static helpers to create and bind a WebGPU Surface to a GLFW window across Wasm and non-Wasm platforms.

Definition at line 21 of file SurfaceFactory.hpp.

Member Function Documentation

◆ create_surface()

wgpu::Surface echomap::SurfaceFactory::create_surface ( const wgpu::Instance & instance,
GLFWwindow * window )
static

Creates and binds a Surface to the given GLFW window.

Parameters
instanceThe WebGPU Instance in which the Surface should be created.
windowThe GLFW window pointer to the target window.
Returns
A Surface registered to the Instance and bound to the GLFWwindow.
Exceptions
std::runtime_errorA Surface could not be created and bound for the given Window.

Definition at line 70 of file SurfaceFactory.cpp.

74{
75 const auto chainedDescriptor = get_surface_descriptor(window);
76 if (chainedDescriptor == nullptr)
77 throw std::runtime_error("Could not create and bind a Surface to the given window.");
78
79 wgpu::SurfaceDescriptor descriptor;
80 descriptor.nextInChain = chainedDescriptor.get();
81
82 return instance.CreateSurface(&descriptor);
83}
static std::unique_ptr< wgpu::ChainedStruct > get_surface_descriptor(GLFWwindow *window)
Produce an owning container of the WebGPU Surface descriptor for the given GLFW window.

◆ get_surface_descriptor()

std::unique_ptr< wgpu::ChainedStruct > echomap::SurfaceFactory::get_surface_descriptor ( GLFWwindow * window)
staticprivate

Produce an owning container of the WebGPU Surface descriptor for the given GLFW window.

Supported platforms:

  • Emscripten
  • Wayland
  • X11
  • Win32
  • Cocoa/Apple
Parameters
windowThe GLFW window to bind to the WebGPU surface.
Returns
An owning container detaining the chain. The next chained element is the bound Surface. If a suitable Surface could not be bound, nullptr is returned.

Definition at line 85 of file SurfaceFactory.cpp.

88{
89 // Verify that the window is valid in the runtime.
90 if (glfwGetWindowAttrib(window, GLFW_CLIENT_API) != GLFW_NO_API)
91 return nullptr;
92
93#if defined(__EMSCRIPTEN__)
95 desc->selector = "#canvas";
96 return desc;
97#else
98 // Use the GLFW_EXPOSE_NATIVE_* flags to determine which GLFW platforms we can handle.
99
100 // ReSharper disable once CppDefaultCaseNotHandledInSwitchStatement
101 switch (glfwGetPlatform()) {
102#if defined(GLFW_EXPOSE_NATIVE_WAYLAND)
103 case GLFW_PLATFORM_WAYLAND: {
105 desc->display = glfwGetWaylandDisplay();
106 desc->surface = glfwGetWaylandWindow(window);
107 return desc;
108 }
109#endif
110#if defined(GLFW_EXPOSE_NATIVE_X11)
111 case GLFW_PLATFORM_X11: {
113 desc->display = glfwGetX11Display();
114 desc->window = glfwGetX11Window(window);
115 return desc;
116 }
117#endif
118#if defined(GLFW_EXPOSE_NATIVE_WIN32)
119 case GLFW_PLATFORM_WIN32: {
121 desc->hwnd = glfwGetWin32Window(window);
122 desc->hinstance = GetModuleHandle(nullptr);
123 return desc;
124 }
125#endif
126#if defined(GLFW_EXPOSE_NATIVE_COCOA)
127 case GLFW_PLATFORM_COCOA: {
128 /*
129 * On Apple platforms, more work is required to configure the Metal layer on the Cocoa Surface through the
130 * Objective-C messaging APIs.
131 */
132 auto ns_window = glfwGetCocoaWindow(window);
133 CFRetain(ns_window);
134
135 auto view = objc_call<id>(ns_window, "contentView");
136 CFRetain(view);
137
138 objc_call<void, BOOL>(view, "setWantsLayer:", YES);
139 auto layer = objc_call<id>("CAMetalLayer", "layer");
140 auto scale_factor = objc_call<CGFloat>(ns_window, "backingScaleFactor");
141 objc_call<void, CGFloat>(layer, "setContentsScale:", scale_factor);
142 objc_call<void, id>(view, "setLayer:", layer);
143
145 desc->layer = layer;
146 CFRelease(view);
147 CFRelease(ns_window);
148
149 return desc;
150 }
151#endif
152 }
153#endif
154
155 // No handlers were enabled. This should be caught be a compile-time check.
156 // ReSharper disable once CppDFAUnreachableCode
157 return nullptr;
158}
T make_unique(T... args)

The documentation for this class was generated from the following files: