Skip to content

Commit

Permalink
fix webgpu example
Browse files Browse the repository at this point in the history
  • Loading branch information
ColleagueRiley committed Oct 20, 2024
1 parent 2b68f8a commit 7a7795b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ endif

examples/webgpu/webgpu: examples/webgpu/webgpu.c RGFW.h
ifeq ($(CC),emcc) # web ASM
emcc main.c -I. --shell-file ./webasm/shell.html -s USE_WEBGPU=1 -o $@$(EXT)
emcc $< -I. --shell-file ./webasm/shell.html -s USE_WEBGPU=1 -o $@$(EXT)
else
@echo webgpu is not supported on $(detected_OS)
endif
Expand Down
35 changes: 17 additions & 18 deletions examples/webgpu/webgpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ static state_t state;
//--------------------------------------------------

// callbacks
static int resize(int, const EmscriptenUiEvent*, void*);
static void draw();

// helper functions
Expand Down Expand Up @@ -111,8 +110,13 @@ int main(int argc, const char* argv[]) {
//-----------------
// init
//-----------------
resize(0, NULL, NULL); // set size and create swapchain
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, false, (em_ui_callback_func)resize);
if (state.wgpu.swapchain) {
wgpuSwapChainRelease(state.wgpu.swapchain);
state.wgpu.swapchain = NULL;
}

state.wgpu.swapchain = create_swapchain();
}

//-----------------
// setup pipeline
Expand Down Expand Up @@ -255,7 +259,16 @@ int main(int argc, const char* argv[]) {
//-----------------

while (RGFW_window_shouldClose(win) == RGFW_FALSE) {
RGFW_window_checkEvent(win);
while (RGFW_window_checkEvent(win)) {
if (win->event.type == RGFW_windowResized) {
if (state.wgpu.swapchain) {
wgpuSwapChainRelease(state.wgpu.swapchain);
state.wgpu.swapchain = NULL;
}

state.wgpu.swapchain = create_swapchain();
}
}
draw();
}

Expand Down Expand Up @@ -323,20 +336,6 @@ void draw() {
wgpuTextureViewRelease(back_buffer);
}

// resize callback
int resize(int event_type, const EmscriptenUiEvent* ui_event, void* user_data) {
(void)event_type, (void)ui_event, (void)user_data; // unused

if (state.wgpu.swapchain) {
wgpuSwapChainRelease(state.wgpu.swapchain);
state.wgpu.swapchain = NULL;
}

state.wgpu.swapchain = create_swapchain();

return 1;
}

// helper functions
WGPUSwapChain create_swapchain() {
WGPUSurface surface = wgpuInstanceCreateSurface(win->src.ctx, &(WGPUSurfaceDescriptor){
Expand Down

0 comments on commit 7a7795b

Please sign in to comment.