Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ jobs:
- run: go test -count=1 ./...
env:
CGO_ENABLED: "0"
# Config.GPU on Win32: a real window on this runner, drawn by gpu2d's Direct3D 11
# renderer on WARP (no GPU here) and presented through a flip-model swap
# chain; the back buffer, read just before the swap chain presents it, must be
# the CPU path's frame, through a resize, and a refused frame must drop the
# window to GDI. What a screen shows is not observed.
- name: Live GPU window (WARP, flip swap chain)
env:
CGO_ENABLED: "0"
WINDOW_WIN32_GPU: "1"
run: go test -count=1 -v -run TestLiveGPUWindow ./internal/win32/
- name: Live display enumeration (no window, no interactive session)
env:
CGO_ENABLED: "0"
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,27 @@ Under lavapipe the GPU path costs more CPU time than the CPU path (about ten
times) and more wall time (35 to 52 %): there is no GPU to take the work. The
fractal run (RADV) that decides whether it pays on a real one is pending.

**On the GPU on Windows (`Config.GPU`, opt-in):**
- **Drawing:** the same recording through gpupainter into a gpu2d list, drawn by
gpu2d's **Direct3D 11** back-end (`gpu2d/d3d11`, over go-mswin/d3d11), and
copied by a second kernel into the back buffer of a **flip-model swap chain**
(`FLIP_DISCARD`, `R8G8B8A8_UNORM`, back buffers used as UAVs) on the window's own
HWND: the frame never returns to the CPU. While the swap chain exists `WM_PAINT`
only validates the update region, because a flip-model window shows nothing but
its swap chain.
- **Proof, and what it is not:** `TestLiveGPUWindowPresentsTheCPUFrame` runs on
`windows-latest`, with a real window and gpu2d's **WARP** (software) device:
the back buffer, read through staging just before the swap chain presents it,
equals the frame `PixelPainter` draws from the same widgets, byte for byte,
before and after a resize. Nothing observed what a screen showed (a hosted
runner has no one to look), and **no Windows GPU has run it**: whether a
given GPU's driver accepts a UAV on a flip-model back buffer is unknown (the
documentation neither promises nor forbids it; WARP accepts it) and is
gpu2d's parity measurement to make. A frame the device refuses, or a device
that will not start, leaves the window on the GDI path, with `GPUError()`
saying why; both are tested. Child controls (the native-control seam) over a
swap chain have not been tried.

### Windows Win32/GDI backend (`windows`)

On Windows `Open` declares **Per-Monitor-V2 DPI awareness**, registers a window
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/ebitengine/purego v0.11.3
github.com/go-appdirs/outdir v0.3.0
github.com/go-freedesktop/x11 v0.3.0
github.com/go-gfx/gpu2d v0.9.2
github.com/go-gfx/gpu2d v0.11.0
github.com/go-gtk/gtk4 v0.9.0
github.com/go-macos/appkit v0.9.0
github.com/go-macos/metal v0.6.0
Expand All @@ -30,6 +30,7 @@ require (
github.com/go-gfx/vulkan v0.2.1 // indirect
github.com/go-icons/iconoir v0.3.0 // indirect
github.com/go-images/images v0.1.1-0.20261026203936-4ac989d310a1 // indirect
github.com/go-mswin/d3d11 v0.3.0 // indirect
github.com/go-opentype/fonts v0.14.0 // indirect
github.com/go-opentype/shape v0.7.0 // indirect
github.com/go-richdoc/richdoc v0.7.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ github.com/go-freedesktop/x11 v0.3.0 h1:DAZN7uAt8eQOyav90VduelaEJyTdWlAZjOTZOKvu
github.com/go-freedesktop/x11 v0.3.0/go.mod h1:6GsirPySzG8JmIhqUYc0bDcMfrXuaGf9iRsOWSP8JsU=
github.com/go-gfx/gfx v0.41.0 h1:MVEnory0Zx8ypzuVDrTIWz4Cekv8hn5ilcYg3glJPrk=
github.com/go-gfx/gfx v0.41.0/go.mod h1:4OenoKrgdht6UXYf5SV8w2tb41hfAdy4QoxJRPsRDz4=
github.com/go-gfx/gpu2d v0.9.2 h1:HittjwReXNOE8RVxIPW1HOwRC7i9ugsNC1SZGPODz5c=
github.com/go-gfx/gpu2d v0.9.2/go.mod h1:xZWTUuYv617mHfkH7CuamDessnKAtFyBFrYqB6akgXM=
github.com/go-gfx/gpu2d v0.11.0 h1:YaA+WRv1F3E5gs9nAy2FxmD5u/l38QpKxD6txSGG0yI=
github.com/go-gfx/gpu2d v0.11.0/go.mod h1:Qc9VYnOd4C0jMsY9aEwalrCPvm11FM1L10UG83n3x80=
github.com/go-gfx/vulkan v0.2.1 h1:a6eysWKAUcqB8aDh/4Z+gDrY56+6TaMUu+47d6pZN18=
github.com/go-gfx/vulkan v0.2.1/go.mod h1:IYZuEIwS3vtystKZRIp+DtuPWX3Nfq86yMC4gJ5ebhw=
github.com/go-gfx/wgsl v0.8.0 h1:zU4tEy9k2HQ2pQIgEuQqpPlpfPi5M7O8QbAzoSs6EZ0=
github.com/go-gfx/wgsl v0.8.0/go.mod h1:Fzzw0jXvB+zxCqmTDwvcu3B7PkIVJBcygl2dyDyDAgQ=
github.com/go-gfx/wgsl v0.9.0 h1:6luCFFUwJOPpdkLYGnUOMl2Por2YTb/0bDLdJdpaFtw=
github.com/go-gfx/wgsl v0.9.0/go.mod h1:Fzzw0jXvB+zxCqmTDwvcu3B7PkIVJBcygl2dyDyDAgQ=
github.com/go-gtk/gtk4 v0.9.0 h1:5Efx48jeWL1LTmKIqqPcseSoa4U8Pf0Sbzo/rU4nmGA=
github.com/go-gtk/gtk4 v0.9.0/go.mod h1:BTpReN9ONh5ZO4p2ETjt8n7km3yEYOFW/Q9HprOC13g=
github.com/go-icons/iconoir v0.3.0 h1:kHG2sU5IsCvtBS1JICuXgfFo5f8UfwY1m7KYMVy97CI=
Expand All @@ -33,6 +33,8 @@ github.com/go-macos/objc v0.12.0 h1:/WmB4C1CQd5RkqBRvnHNljEFtOTvytPA9fkD87xuJVQ=
github.com/go-macos/objc v0.12.0/go.mod h1:ao+SmTFBrkZdP+488hFTrSFvCtJ36McKKhNNOlcFXrQ=
github.com/go-macos/virtualdisplay v0.5.0 h1:V2TMkAXLUCYqZSfkfIz7YQkoh2+4/bz+ja4hvYoJut8=
github.com/go-macos/virtualdisplay v0.5.0/go.mod h1:3hgrYzzH17hfjFnsZKq/kg0bIuB3mu2FK60BudhBqRc=
github.com/go-mswin/d3d11 v0.3.0 h1:97vwif+uFbTBvOicU1acAb+CqfN1yKIeZBGAlTm77fY=
github.com/go-mswin/d3d11 v0.3.0/go.mod h1:uYoPcvGm3f/S0WDrdrilWO5QagbXaSPPKRjnIzcDedw=
github.com/go-mswin/win32 v0.6.0 h1:gapeT3H5/V8eS61WP4r8oqA4tN09qkU1QXmjgOu5VpE=
github.com/go-mswin/win32 v0.6.0/go.mod h1:JoPSHnB0eccheGPKPdQyj4jPbBfKANYXrYl/l6xEwxs=
github.com/go-opentype/fonts v0.14.0 h1:WHw7k9gkHT1pu9B2yyvd0FWyQqn/4NKFQoKhvMYt3Mo=
Expand Down
165 changes: 165 additions & 0 deletions internal/win32/gpu_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright (c) the go-widgets/window authors. All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause

//go:build windows

package win32

import (
"github.com/go-gfx/gpu2d"
"github.com/go-gfx/gpu2d/d3d11"
"github.com/go-widgets/gpupainter"
"github.com/go-widgets/toolkit"
)

// gpuPath draws frames on the GPU (Config.GPU), as the Cocoa back-end's does: the
// widgets record into a gpu2d list through gpupainter, gpu2d's Direct3D 11
// renderer draws it, and a second kernel copies the result into the back buffer
// of a flip-model swap chain on the window's own HWND (go-gfx/gpu2d Present,
// go-mswin/d3d11 v0.3.0). The frame never comes back to the CPU and WM_PAINT has
// nothing to blit.
//
// A flip-model swap chain owns what its window shows ("only Direct3D content in
// flip model swap chains ... are visible"), so while it exists onPaint only
// validates the update region; when the path stops, the swap chain is released
// before GDI draws again.
//
// Opt-in, as on macOS: the Direct3D 11 back-end is proven on WARP in CI and on no
// GPU (go-gfx/gpu2d docs/NATIVE.md, Decisions taken).
type gpuPath struct {
r *d3d11.Renderer
sc *d3d11.SwapChain
list *gpu2d.List
paint *gpupainter.Painter
}

// gpuOpener is the seam a test replaces to make the GPU path fail to start, or
// to ask for the software device (WARP) on a machine whose adapter is not the
// one being proven. By default it is the default hardware adapter.
var gpuOpener = d3d11.New

// gpuCapture, when set by a test, receives a copy of each frame's back buffer as
// the device holds it just before the swap chain presents it (the frame the
// window is about to show), with the framebuffer size. It is the proof that the
// presented frame is the CPU path's, with no desktop to take a screenshot from.
var gpuCapture func(frame []byte, w, h int)

// gpuFault, when set by a test, is asked before each present: an error stops the
// path as a lost device would.
var gpuFault func() error

// startGPU switches the window to the GPU path, if it was asked for. Any failure
// (no adapter, a device that cannot make a flip swap chain with UAV back
// buffers, a frame too large) leaves the CPU path in place and is recorded in
// GPUError. It runs once the window exists, from Run.
func (w *Window) startGPU() {
if !w.wantGPU || w.gpu != nil || w.hwnd == 0 {
return
}
r, err := gpuOpener(w.w, w.h)
if err != nil {
w.gpuErr = err
return
}
sc, err := r.NewSwapChain(w.hwnd)
if err != nil {
r.Close()
w.gpuErr = err
return
}
list, err := gpu2d.NewList(w.w, w.h)
if err != nil {
sc.Close()
r.Close()
w.gpuErr = err
return
}
w.gpu = &gpuPath{r: r, sc: sc, list: list, paint: gpupainter.New(list)}
w.gpuErr = nil
}

// stopGPU releases the swap chain and the device, and gives the window back to
// GDI. The window draws on the CPU afterwards.
func (w *Window) stopGPU() {
g := w.gpu
if g == nil {
return
}
w.gpu = nil
g.sc.Close()
g.r.Close()
// Direct3D defers the destruction of what it was using; a flip swap chain
// released but not flushed would still own the window.
}

// drawGPU records the whole frame: the background, then the root.
func (w *Window) drawGPU() {
g := w.gpu
g.list.Reset()
bg := w.theme.Background
g.list.FillRect(gpu2d.Rect{W: w.w, H: w.h}, gpu2d.RGBA{R: bg.R, G: bg.G, B: bg.B, A: bg.A})
g.paint.Reset(g.list)
if w.root != nil {
w.root.SetBounds(toolkit.Rect{X: 0, Y: 0, W: w.w, H: w.h})
w.root.Draw(g.paint, w.theme)
}
g.paint.Flush()
}

// presentGPU sends the recorded frame to the swap chain. A frame the device
// refuses drops the window to the CPU path for good, and that frame is drawn
// there instead.
func (w *Window) presentGPU() {
g := w.gpu
err := g.list.Err()
if err == nil && gpuFault != nil {
err = gpuFault()
}
if err == nil {
if gpuCapture != nil {
frame := make([]byte, 4*w.w*w.h)
if err = g.r.PresentAndRead(g.list, g.sc, frame); err == nil {
gpuCapture(frame, w.w, w.h)
}
} else {
err = g.r.Present(g.list, g.sc)
}
}
if err != nil {
w.gpuErr = err
w.stopGPU()
w.draw()
w.invalidateAll()
}
}

// resizeGPU follows a new framebuffer size.
func (w *Window) resizeGPU() {
g := w.gpu
if g == nil {
return
}
list, err := gpu2d.NewList(w.w, w.h)
if err == nil {
err = g.r.Resize(w.w, w.h)
}
if err == nil {
err = g.sc.Resize(w.w, w.h)
}
if err != nil {
w.gpuErr = err
w.stopGPU()
w.draw()
w.invalidateAll()
return
}
g.list = list
}

// GPU reports whether frames are being drawn on the GPU.
func (w *Window) GPU() bool { return w.gpu != nil }

// GPUError is why a window that asked for the GPU is not using it: nil when it
// is, or when it never asked.
func (w *Window) GPUError() error { return w.gpuErr }
Loading
Loading