#include #include #include #include "renderwindow.h" #if defined(SDL_PLATFORM_LINUX) #include #include #endif #if defined(SDL_PLATFORM_WINDOWS) #include #endif void LogWindowProperties(RenderWindow* win); void HandleAdapterRequest ( WGPURequestAdapterStatus status, WGPUAdapter wgpuAdapter, WGPUStringView message, void* userdata1, void* ) { if (status != WGPURequestAdapterStatus_Success) { SDL_Log("Failed to initialize adapter, error %s!\n", message.data); return; } RenderWindow* renderWindow = userdata1; renderWindow->wgpuAdapter = wgpuAdapter; } void HandleDeviceRequest ( WGPURequestDeviceStatus status, WGPUDevice wgpuDevice, WGPUStringView message, void* userdata1, void* ) { if (status != WGPURequestDeviceStatus_Success) { SDL_Log("Failed to initialize adapter, error %s!\n", message.data); return; } RenderWindow* renderWindow = userdata1; renderWindow->wgpuDevice = wgpuDevice; } RenderWindow InitRenderWindow(int width, int height, const char* title) { RenderWindow renderWindow = {0}; SDL_Init(SDL_INIT_VIDEO); SDL_Log("Initialized Video!\n"); renderWindow.window = SDL_CreateWindow(title, width, height, 0); LogWindowProperties(&renderWindow); renderWindow.wgpuInstance = wgpuCreateInstance(&(const WGPUInstanceDescriptor){ .requiredFeatureCount = 1, .requiredFeatures = (WGPUInstanceFeatureName[]) { WGPUInstanceFeatureName_TimedWaitAny } }); SDL_assert(renderWindow.wgpuInstance); SDL_PropertiesID windowProperties = SDL_GetWindowProperties(renderWindow.window); #if defined(SDL_PLATFORM_WINDOWS) HWND hwnd = (HWND)SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL); HINSTANCE hinstance = (HINSTANCE)SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_WIN32_INSTANCE_POINTER, NULL); if (hwnd && hinstance) { renderWindow.wgpuSurface = wgpuInstanceCreateSurface(renderWindow.wgpuInstance, &(const WGPUSurfaceDescriptor){ .nextInChain = (WGPUChainedStruct*)&(const WGPUSurfaceSourceWindowsHWND) { .chain = (WGPUChainedStruct) { .next = NULL, .sType = WGPUSType_SurfaceSourceWindowsHWND } }, .hwnd = hwnd, .hinstance = hinstance }); } #endif #if defined(SDL_PLATFORM_LINUX) if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "x11") == 0) { Display* xdisplay = (Display *)SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, NULL); Window xwindow = (Window)SDL_GetNumberProperty(windowProperties, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0); if (xdisplay && xwindow) { renderWindow.wgpuSurface = wgpuInstanceCreateSurface(renderWindow.wgpuInstance, &(const WGPUSurfaceDescriptor){ .nextInChain = (WGPUChainedStruct*)&(const WGPUSurfaceSourceXlibWindow) { .chain = (WGPUChainedStruct) { .next = NULL, .sType = WGPUSType_SurfaceSourceXlibWindow }, .display = xdisplay, .window = xwindow } }); } } else if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0) { struct wl_display* wl_display = (struct wl_display*)SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, NULL); struct wl_surface* wl_surface = (struct wl_surface*)SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, NULL); if (wl_display && wl_surface) { renderWindow.wgpuSurface = wgpuInstanceCreateSurface(renderWindow.wgpuInstance, &(const WGPUSurfaceDescriptor){ .nextInChain = (WGPUChainedStruct*)&(const WGPUSurfaceSourceWaylandSurface) { .chain = (WGPUChainedStruct) { .next = NULL, .sType = WGPUSType_SurfaceSourceWaylandSurface }, .display = wl_display, .surface = wl_surface } }); } } #endif SDL_assert(renderWindow.wgpuSurface); WGPUFuture adapterFuture = wgpuInstanceRequestAdapter(renderWindow.wgpuInstance, &(const WGPURequestAdapterOptions){ .compatibleSurface = renderWindow.wgpuSurface, .powerPreference = WGPUPowerPreference_LowPower, }, (const WGPURequestAdapterCallbackInfo){ .callback = HandleAdapterRequest, .mode = WGPUCallbackMode_WaitAnyOnly, .userdata1 = &renderWindow }); wgpuInstanceWaitAny (renderWindow.wgpuInstance, 1, &(WGPUFutureWaitInfo){adapterFuture,false}, INT64_MAX ); SDL_assert(renderWindow.wgpuInstance); return renderWindow; } void QuitRenderWindow(RenderWindow* renderWindow) { wgpuAdapterRelease(renderWindow->wgpuAdapter); wgpuSurfaceRelease(renderWindow->wgpuSurface); wgpuInstanceRelease(renderWindow->wgpuInstance); SDL_DestroyWindow(renderWindow->window); SDL_memset(renderWindow, 0, sizeof(RenderWindow)); SDL_Quit(); } void LogWindowProperties(RenderWindow* renderWindow) { SDL_Window* window = renderWindow->window; SDL_Log("WINDOW:\n"); SDL_Log("\tBACKEND:%s", SDL_GetCurrentVideoDriver()); int width, height; SDL_GetWindowSize(window, &width, &height); SDL_Log("\tWIDTH:%d\n", width); SDL_Log("\tHEIGHT:%d\n", height); }