diff options
author | BoredGuy <osome3717@gmail.com> | 2025-09-26 14:11:24 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2025-09-26 14:11:24 +0300 |
commit | 3394a279b34c81d411abe010a2f45ca60370a1eb (patch) | |
tree | 6edc08c1bc6b50da7bbc7f1d4b50d89bca1f7f53 | |
parent | a23d279dfa284c1faf13ef0100bb6434a8e83617 (diff) |
Working on RenderWindow
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | include/renderwindow.h | 21 | ||||
-rw-r--r-- | src/main.c | 215 | ||||
-rw-r--r-- | src/renderwindow.c | 52 |
4 files changed, 77 insertions, 212 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 5695ca5..c116383 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable(game ) target_link_libraries(game SDL3::SDL3 dawn::webgpu_dawn) +target_include_directories(game PRIVATE "include") if (UNIX) target_compile_options(game PRIVATE "-g") diff --git a/include/renderwindow.h b/include/renderwindow.h new file mode 100644 index 0000000..87d89b9 --- /dev/null +++ b/include/renderwindow.h @@ -0,0 +1,21 @@ +#ifndef RENDERWINDOW_H_ +#define RENDERWINDOW_H_ + +#include <SDL3/SDL.h> +#include <dawn/webgpu.h> + +typedef struct { + SDL_Window* window; + + WGPUInstance wgpuInstance; + WGPUSurface wgpuSurface; + WGPUAdapter wgpuAdapter; + WGPUDevice wgpuDevice; + WGPUSurfaceConfiguration configs; + WGPUQueue queue; +} RenderWindow; + +RenderWindow InitRenderWindow(int width, int height, const char* title); +void QuitRenderWindow(RenderWindow* renderWindow); + +#endif // RENDERWINDOW_H_ @@ -1,217 +1,8 @@ -#include <webgpu/webgpu.h> -#include <SDL3/SDL.h> -#include <stdbool.h> -#include <string.h> -#include <stdlib.h> -#include <inttypes.h> - -#include <X11/Xlib.h> - -typedef struct { - SDL_Window* window; - - WGPUInstance instance; - WGPUSurface surface; - WGPUAdapter adapter; - WGPUDevice device; - WGPUQueue queue; -} demo; - -void init_demo(demo* demo); -void free_demo(demo* demo); +#include <renderwindow.h> int main() { - demo demo = {0}; - init_demo(&demo); - - SDL_Event e; - bool running = true; - - while (running) { - while (SDL_PollEvent(&e)) { - if (e.type == SDL_EVENT_QUIT) { - running = false; - } - } - - WGPUSurfaceTexture screenTexture; - wgpuSurfaceGetCurrentTexture(demo.surface, &screenTexture); - - WGPUTextureView screenView = - wgpuTextureCreateView(screenTexture.texture, NULL); - - WGPUCommandEncoder encoder = - wgpuDeviceCreateCommandEncoder(demo.device, &(const WGPUCommandEncoderDescriptor){ - .label = {NULL, WGPU_STRLEN} - }); - - WGPURenderPassEncoder renderPass = - wgpuCommandEncoderBeginRenderPass(encoder, &(const WGPURenderPassDescriptor){ - .label = {NULL, WGPU_STRLEN}, - .colorAttachmentCount = 1, - .colorAttachments = &(const WGPURenderPassColorAttachment) { - .view = screenView, - .resolveTarget = NULL, - .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED, + RenderWindow window = InitRenderWindow(800, 600, "Hello World!"); - .loadOp = WGPULoadOp_Clear, - .storeOp = WGPUStoreOp_Store, - .clearValue = (WGPUColor) { - .r = 0.1, - .g = 0.2, - .b = 0.3, - .a = 1.0 - } - } - }); - - wgpuRenderPassEncoderEnd(renderPass); - wgpuRenderPassEncoderRelease(renderPass); - - WGPUCommandBuffer drawCommands = wgpuCommandEncoderFinish(encoder, NULL); - wgpuQueueSubmit(demo.queue, 1, &drawCommands); - wgpuSurfacePresent(demo.surface); - - wgpuCommandBufferRelease(drawCommands); - wgpuCommandEncoderRelease(encoder); - wgpuTextureViewRelease(screenView); - wgpuTextureRelease(screenTexture.texture); - } - - free_demo(&demo); - SDL_Quit(); + QuitRenderWindow(&window); return 0; } - -void handle_adapter_request -( - WGPURequestAdapterStatus status, - WGPUAdapter adapter, - WGPUStringView message, - void* userdata, - void* - ) { - demo* demo = userdata; - - if (status != WGPURequestAdapterStatus_Success) { - SDL_Log("Failed to retreive wgpu adapter, error: %s, exitting!", message.data); - exit(EXIT_FAILURE); - } - - demo->adapter = adapter; -} - -void handle_device_request -( - WGPURequestDeviceStatus status, - WGPUDevice device, - WGPUStringView message, - void* userdata, - void* - ) { - demo* demo = userdata; - - if (status != WGPURequestDeviceStatus_Success) { - SDL_Log("Failed to retreive wgpu device, error: %s, exitting!", message.data); - exit(EXIT_FAILURE); - } - - demo->device = device; -} - -void uncaptured_error_callback -( - WGPUDevice const*, - WGPUErrorType, - WGPUStringView message, - void*, void* -) { - SDL_Log("Graphics Error: %s\n", message.data); -} - -void init_demo(demo* demo) { - demo->window = - SDL_CreateWindow("WGPU Test", 800, 600, 0); - - demo->instance = wgpuCreateInstance(&(const WGPUInstanceDescriptor){ - .requiredFeatureCount = 1, - .requiredFeatures = (WGPUInstanceFeatureName[]){ - WGPUInstanceFeatureName_TimedWaitAny - } - }); - SDL_assert(demo->instance); - -#if defined(SDL_PLATFORM_LINUX) - if (strcmp(SDL_GetCurrentVideoDriver(), "x11") == 0) { - Display* xdisplay = - (Display*)SDL_GetPointerProperty(SDL_GetWindowProperties(demo->window), SDL_PROP_WINDOW_X11_DISPLAY_POINTER, NULL); - Window xwindow = - (Window)SDL_GetNumberProperty(SDL_GetWindowProperties(demo->window), SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0); - - if (xdisplay && xwindow) { - demo->surface = - wgpuInstanceCreateSurface(demo->instance, &(const WGPUSurfaceDescriptor){ - .nextInChain = (WGPUChainedStruct*)&(WGPUSurfaceSourceXlibWindow) { - .chain = (WGPUChainedStruct) { - .next = NULL, - .sType = WGPUSType_SurfaceSourceXlibWindow - }, - - .display = xdisplay, - .window = xwindow - } - }); - } - } -#endif - SDL_assert(demo->surface); - - WGPUFuture adapterFuture = - wgpuInstanceRequestAdapter(demo->instance, &(const WGPURequestAdapterOptions){ - .compatibleSurface = demo->surface, - .powerPreference = WGPUPowerPreference_LowPower - }, (WGPURequestAdapterCallbackInfo){ - .callback = handle_adapter_request, - .mode = WGPUCallbackMode_WaitAnyOnly, - .userdata1 = demo - }); - - wgpuInstanceWaitAny(demo->instance, 1, &(WGPUFutureWaitInfo){adapterFuture, false}, INT64_MAX); - SDL_assert(demo->adapter); - - WGPUFuture deviceFuture = - wgpuAdapterRequestDevice(demo->adapter, &(const WGPUDeviceDescriptor){ - .label = {"RTX 2070", WGPU_STRLEN}, - .uncapturedErrorCallbackInfo = { .callback = uncaptured_error_callback } - }, (WGPURequestDeviceCallbackInfo){ - .callback = handle_device_request, - .mode = WGPUCallbackMode_WaitAnyOnly, - .userdata1 = demo - }); - - wgpuInstanceWaitAny(demo->instance, 1, &(WGPUFutureWaitInfo){deviceFuture, false}, INT64_MAX); - SDL_assert(demo->device); - - WGPUSurfaceCapabilities caps; - wgpuSurfaceGetCapabilities(demo->surface, demo->adapter, &caps); - - WGPUSurfaceConfiguration config = (WGPUSurfaceConfiguration) { - .device = demo->device, - .format = caps.formats[0], - .usage = WGPUTextureUsage_RenderAttachment, - .width = 800, - .height = 600, - .presentMode = WGPUPresentMode_Fifo, - .alphaMode = caps.alphaModes[0] - }; - wgpuSurfaceConfigure(demo->surface, &config); - - demo->queue = wgpuDeviceGetQueue(demo->device); -} - -void free_demo(demo* demo) { - wgpuAdapterRelease(demo->adapter); - wgpuSurfaceRelease(demo->surface); - wgpuInstanceRelease(demo->instance); - SDL_DestroyWindow(demo->window); -} diff --git a/src/renderwindow.c b/src/renderwindow.c index d3bea0c..e9b7a80 100644 --- a/src/renderwindow.c +++ b/src/renderwindow.c @@ -2,6 +2,11 @@ #include <dawn/webgpu.h> #include "renderwindow.h" +#if defined(SDL_PLATFORM_LINUX) +#include <X11/Xlib.h> +#include <wayland-client.h> +#endif + RenderWindow InitRenderWindow(int width, int height, const char* title) { RenderWindow renderWindow = {0}; SDL_Init(SDL_INIT_VIDEO); @@ -12,11 +17,58 @@ RenderWindow InitRenderWindow(int width, int height, const char* title) { .requiredFeatures = (WGPUInstanceFeatureName[]) { WGPUInstanceFeatureName_TimedWaitAny } }); + SDL_PropertiesID windowProperties = SDL_GetWindowProperties(renderWindow.window); +#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 + return renderWindow; } void QuitRenderWindow(RenderWindow* renderWindow) { + wgpuSurfaceRelease(renderWindow->wgpuSurface); wgpuInstanceRelease(renderWindow->wgpuInstance); SDL_DestroyWindow(renderWindow->window); + SDL_memset(renderWindow, 0, sizeof(RenderWindow)); SDL_Quit(); } |