From 1b8d7f2b2ed51d70ab5b86df11584af899f416db Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Mon, 11 May 2026 00:19:31 +0200 Subject: [PATCH] handle the Occluded case directly within TryGetCurrentTexture, because it does NOT require reconfiguring the surface, and it DOES require calling glfw.PollEvents -- which others would not presumably. --- examples/boids/main.go | 4 +--- examples/cube/main.go | 4 +--- examples/triangle/main.go | 4 +--- wgpu/surface.go | 44 ++++++++++++++++++++++++--------------- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/examples/boids/main.go b/examples/boids/main.go index fcb89c1..47a34fe 100644 --- a/examples/boids/main.go +++ b/examples/boids/main.go @@ -8,7 +8,6 @@ import ( "runtime" "strconv" "strings" - "time" "github.com/go-gl/glfw/v3.4/glfw" "github.com/oliverbestmann/webgpu/wgpu" @@ -310,8 +309,7 @@ func (s *State) Render() error { nextTexture, ok := surfaceTexture.Get() if !ok { - // maybe occluded, retry layter - time.Sleep(16 * time.Millisecond) + // todo: in this case you actually want to re-configure the surface! return nil } diff --git a/examples/cube/main.go b/examples/cube/main.go index 9ef470f..a02167f 100644 --- a/examples/cube/main.go +++ b/examples/cube/main.go @@ -6,7 +6,6 @@ import ( "os" "runtime" "strings" - "time" "unsafe" "github.com/go-gl/glfw/v3.4/glfw" @@ -349,8 +348,7 @@ func (s *State) Render() error { nextTexture, ok := surfaceTexture.Get() if !ok { - // maybe occluded, retry layter - time.Sleep(16 * time.Millisecond) + // todo: in this case you actually want to re-configure the surface! return nil } diff --git a/examples/triangle/main.go b/examples/triangle/main.go index 64ef4f7..e676d6e 100644 --- a/examples/triangle/main.go +++ b/examples/triangle/main.go @@ -3,7 +3,6 @@ package main import ( "os" "runtime" - "time" "github.com/oliverbestmann/webgpu/wgpu" @@ -148,8 +147,7 @@ func (s *State) Render() error { nextTexture, ok := surfaceTexture.Get() if !ok { - // maybe occluded, retry layter - time.Sleep(16 * time.Millisecond) + // todo: in this case you actually want to re-configure the surface! return nil } diff --git a/wgpu/surface.go b/wgpu/surface.go index 52f3a54..c2b5f2c 100644 --- a/wgpu/surface.go +++ b/wgpu/surface.go @@ -29,7 +29,10 @@ import "C" import ( "errors" "runtime" + "time" "unsafe" + + "github.com/go-gl/glfw/v3.4/glfw" ) func (g *Surface) GetCapabilities(adapter *Adapter) (ret SurfaceCapabilities) { @@ -133,28 +136,35 @@ func (g *Surface) TryGetCurrentTexture() (SurfaceTexture, error) { errh := acquireErrorCallback() defer errh.Done() - surfaceTexture := C.gowebgpu_surface_get_current_texture( - g.ref, - g.device, - errh.ToPointer(), - ) - if errh.err != nil { - if surfaceTexture.texture != nil { - C.wgpuTextureRelease(surfaceTexture.texture) + for { + surfaceTexture := C.gowebgpu_surface_get_current_texture( + g.ref, + g.device, + errh.ToPointer(), + ) + if errh.err != nil { + if surfaceTexture.texture != nil { + C.wgpuTextureRelease(surfaceTexture.texture) + } + + return SurfaceTexture{}, errh.err } - return SurfaceTexture{}, errh.err - } + status := SurfaceGetCurrentTextureStatus(surfaceTexture.status) + if status == SurfaceGetCurrentTextureStatusOccluded { + time.Sleep(16 * time.Millisecond) + glfw.PollEvents() + continue + } - status := SurfaceGetCurrentTextureStatus(surfaceTexture.status) + var texture *Texture + if surfaceTexture.texture != nil { + C.wgpuDeviceAddRef(g.device) + texture = &Texture{device: g.device, ref: surfaceTexture.texture} + } - var texture *Texture - if surfaceTexture.texture != nil { - C.wgpuDeviceAddRef(g.device) - texture = &Texture{device: g.device, ref: surfaceTexture.texture} + return SurfaceTexture{Texture: texture, Status: status}, nil } - - return SurfaceTexture{Texture: texture, Status: status}, nil } func (g *Surface) Present() {