You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is an extension for the great SDL2 library for using it with WebGPU native. It was written as part of the Learn WebGPU for native C++ tutorial series.
Given an SDL window, SDL_GetWGPUSurface returns a WebGPU surface that corresponds to the window's backend. This is a process that is highly platform-specific, which is why I believe it belongs to SDL.
Usage
Your project must link to an implementation of WebGPU (providing webgpu.h) and of course to SDL. Then:
Option A If you use CMake, you can simply include this project as a subdirectory with add_subdirectory(sdl2webgpu) (see the content of CMakeLists.txt).
Option B Just copy sdl2webgpu.h and sdl2webgpu.c to your project's source tree. On MacOS, you must add the compile option -x objective-c and the link libraries -framework Cocoa, -framework CoreVideo, -framework IOKit, and -framework QuartzCore. For iOS, do similarly but use -framework UIKit instead of -framework Cocoa.
Example
Thanks to this extension it is possible to simply write a fully cross-platform WebGPU hello world:
#defineSDL_MAIN_HANDLED#include"sdl2webgpu.h"#include<SDL2/SDL.h>#include<webgpu/webgpu.h>#include<stdio.h>intmain(intargc, char*argv[]) {
// Init WebGPUWGPUInstanceDescriptordesc;
desc.nextInChain=NULL;
WGPUInstanceinstance=wgpuCreateInstance(&desc);
// Init SDLSDL_Init(SDL_INIT_VIDEO);
SDL_Window*window=SDL_CreateWindow("Learn WebGPU", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
// Here we create our WebGPU surface from the window!WGPUSurfacesurface=SDL_GetWGPUSurface(instance, window);
printf("surface = %p", surface);
// Wait for closeSDL_Eventevent;
while (1)
while (SDL_PollEvent(&event))
if (event.type==SDL_QUIT)
break;
// Terminate SDLSDL_DestroyWindow(window);
SDL_Quit();
return0;
}
NB The linking process depends on the implementation of WebGPU that you are using. You can find detailed instructions for the wgpu-native implementation in this Hello WebGPU chapter. You may also check out examples/CMakeLists.txt.