Documentation
¶
Overview ¶
Package gpusdf implements GPU evaluation of signed distance fields using WebGPU.
Native builds use wgpu-native via cgo, which runs on Metal (macOS), Vulkan, D3D12 and OpenGL ES. Builds targeting the browser (GOOS=js GOARCH=wasm) use the browser's WebGPU implementation. Builds without cgo (and TinyGo builds) compile but return an error on configuration.
Types in this package follow a configure-then-use pattern: the zero value is ready to be configured and a call to Configure releases all previous resources and replaces them. GPU memory is allocated up front during Configure so evaluation does not allocate Go memory. Types are not safe for concurrent use and must not be copied after a call to Configure.
Index ¶
- Constants
- func UI(s glbuild.Shader3D, cfg UIConfig) error
- type BackoffStrategy
- type Batcher
- type ComputeConfig
- type Device
- type DeviceConfig
- type DisplaceMulti2D
- func (disp *DisplaceMulti2D) Bounds() ms2.Box
- func (disp *DisplaceMulti2D) Configure(dev *Device, programmer *glbuild.Programmer, element glbuild.Shader2D, ...) error
- func (disp *DisplaceMulti2D) Evaluate(pos []ms2.Vec, dist []float32, userData any) error
- func (disp *DisplaceMulti2D) Evaluations() uint64
- func (disp *DisplaceMulti2D) Release()
- type KernelConfig
- type Lines2D
- type Polygon
- type PowerPreference
- type SDF2
- func (s *SDF2) Bounds() ms2.Box
- func (s *SDF2) Configure(dev *Device, bb ms2.Box, cfg ComputeConfig) error
- func (s *SDF2) Evaluate(pos []ms2.Vec, dist []float32, userData any) error
- func (s *SDF2) Evaluations() uint64
- func (s *SDF2) Poll() (done bool, err error)
- func (s *SDF2) Read(dist []float32) error
- func (s *SDF2) Release()
- func (s *SDF2) Submit(pos []ms2.Vec) error
- type SDF3
- func (s *SDF3) Bounds() ms3.Box
- func (s *SDF3) Configure(dev *Device, bb ms3.Box, cfg ComputeConfig) error
- func (s *SDF3) Evaluate(pos []ms3.Vec, dist []float32, userData any) error
- func (s *SDF3) Evaluations() uint64
- func (s *SDF3) Poll() (done bool, err error)
- func (s *SDF3) Read(dist []float32) error
- func (s *SDF3) Release()
- func (s *SDF3) Submit(pos []ms3.Vec) error
- type UIConfig
Constants ¶
const ( BackoffFlagGosched = time.Duration(-1) BackoffFlagNop = time.Duration(-2) )
Flag return values for a BackoffStrategy.
Variables ¶
This section is empty.
Functions ¶
func UI ¶
UI opens an interactive visualizer that renders s on the GPU by raymarching. Drag with the left mouse button to orbit the camera around the shape and scroll to zoom. UI blocks until the window is closed or the context is done.
On native targets a window is created with GLFW, which requires UI to be called from the main thread on some operating systems (macOS). Lock the main goroutine to the main thread on initialization:
func init() { runtime.LockOSThread() }
On WebAssembly targets the shape is rendered to an HTML canvas; UI must not be called from a JavaScript callback.
Types ¶
type BackoffStrategy ¶
BackoffStrategy is the abstraction of a backoff strategy for waiting on GPU work to finish. It returns the amount of time to sleep for or a flag value:
- Returns BackoffFlagNop: Signal no yielding function should be called. Useful for when the BackoffStrategy implements its own yield.
- Returns BackoffFlagGosched: Signal runtime.Gosched should be called.
consecutiveBackoffs starts at 0 and increments by 1 every time the GPU is polled and work is not yet done.
func (BackoffStrategy) Do ¶
func (backoff BackoffStrategy) Do(consecutiveBackoffs uint)
Do applies the backoff strategy by calling backoff(consecutiveBackoffs) and then the corresponding yield function for the returned value. See BackoffStrategy.
type Batcher ¶
type Batcher struct {
// contains filtered or unexported fields
}
Batcher executes element-wise binary operations on float32 buffers on the GPU.
func (*Batcher) Configure ¶
func (b *Batcher) Configure(dev *Device, cfg KernelConfig) error
Configure releases previous GPU resources and prepares the Batcher for execution on dev.
func (*Batcher) ExecuteRawBinaryOperation ¶
ExecuteRawBinaryOperation executes the WGSL function body opSource for each element of A and B and stores the result in dst. The body has access to the f32 arguments a and b and must return a f32 value, i.e: "return min(a,b);". The compute program is recompiled only when opSource changes between calls.
type ComputeConfig ¶
type ComputeConfig struct {
// Source is the WGSL source code of the compute program, usually generated by
// [glbuild.Programmer.WriteComputeSDF3] or [glbuild.Programmer.WriteComputeSDF2].
Source []byte
// WorkgroupSize is the size of the compute workgroup in the X dimension and must match the size declared in Source:
// @compute @workgroup_size(<WorkgroupSize>, 1, 1)
WorkgroupSize int
// MaxBatch is the maximum number of positions evaluated in a single GPU dispatch. GPU buffers are sized according to MaxBatch
// during configuration. Evaluate calls with more positions are split into several dispatches. If zero a default value is used.
MaxBatch int
// ShaderObjects contains buffer data and definitions required by shader for correct evaluation.
// They are returned by the [glbuild.Programmer] shader generation methods. Data is uploaded to the GPU during configuration.
ShaderObjects []glbuild.ShaderObject
}
ComputeConfig configures a GPU SDF evaluator.
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device is a handle to a GPU device and its command queue. The zero value is ready to be configured with Device.Configure.
func (*Device) AdapterInfo ¶
AdapterInfo returns a human readable description of the GPU adapter in use.
func (*Device) Configure ¶
func (d *Device) Configure(cfg DeviceConfig) error
Configure releases any GPU resources held by the device and requests a new adapter and device according to cfg. The device is requested with the maximum limits supported by the adapter.
func (*Device) MaxStorageBuffers ¶
MaxStorageBuffers returns the maximum number of storage buffers per shader stage. Returns 0 if device is not configured.
func (*Device) MaxWorkgroupSize ¶
MaxWorkgroupSize returns the maximum compute workgroup size in the X dimension supported by the device. Returns 0 if device is not configured.
type DeviceConfig ¶
type DeviceConfig struct {
// ForceFallbackAdapter selects a software adapter (i.e: lavapipe, WARP) if available. Useful for CI.
ForceFallbackAdapter bool
// PowerPreference is a hint for adapter selection.
PowerPreference PowerPreference
// Backoff is used while waiting for GPU work to finish in blocking calls such as Evaluate.
// If nil the calling goroutine blocks inside the GPU driver until the work is done.
Backoff BackoffStrategy
}
DeviceConfig configures a Device.
type DisplaceMulti2D ¶
type DisplaceMulti2D struct {
// Displacements of the element. Uploaded to the GPU during Configure.
Displacements []ms2.Vec
// contains filtered or unexported fields
}
DisplaceMulti2D evaluates the union of copies of a 2D shape element displaced by each of Displacements on the GPU.
func (*DisplaceMulti2D) Bounds ¶
func (disp *DisplaceMulti2D) Bounds() ms2.Box
Bounds implements gleval.SDF2.
func (*DisplaceMulti2D) Configure ¶
func (disp *DisplaceMulti2D) Configure(dev *Device, programmer *glbuild.Programmer, element glbuild.Shader2D, cfg KernelConfig) error
Configure generates the program for element with programmer and uploads Displacements to the GPU. Must be called again if Displacements are modified.
func (*DisplaceMulti2D) Evaluations ¶
func (disp *DisplaceMulti2D) Evaluations() uint64
Evaluations returns total evaluations performed succesfully during disp's lifetime.
func (*DisplaceMulti2D) Release ¶
func (disp *DisplaceMulti2D) Release()
Release releases all GPU resources held by disp.
type KernelConfig ¶
type KernelConfig struct {
// WorkgroupSize is the size of the compute workgroup. If zero the device maximum (capped to 256) is used.
WorkgroupSize int
// MaxBatch is the maximum number of elements evaluated in a single GPU dispatch. If zero a default value is used.
MaxBatch int
}
KernelConfig configures the built-in GPU evaluators of this package: Batcher, Polygon, Lines2D and DisplaceMulti2D.
type Lines2D ¶
type Lines2D struct {
// Lines are the segments. Lines are uploaded to the GPU during Configure.
Lines [][2]ms2.Vec
// Width of the lines. Can be modified between evaluations.
Width float32
// contains filtered or unexported fields
}
Lines2D implements a direct SDF evaluation of a set of 2D line segments of equal width on the GPU.
func (*Lines2D) Configure ¶
func (l *Lines2D) Configure(dev *Device, cfg KernelConfig) error
Configure uploads l.Lines to the GPU and compiles the program. Must be called again if Lines are modified.
func (*Lines2D) Evaluations ¶
Evaluations returns total evaluations performed succesfully during l's lifetime.
type Polygon ¶
type Polygon struct {
// Vertices of the polygon. Vertices are uploaded to the GPU during Configure.
Vertices []ms2.Vec
// contains filtered or unexported fields
}
Polygon implements a direct polygon SDF evaluation on the GPU.
func (*Polygon) Configure ¶
func (p *Polygon) Configure(dev *Device, cfg KernelConfig) error
Configure uploads p.Vertices to the GPU and compiles the polygon program. Must be called again if Vertices are modified.
func (*Polygon) Evaluations ¶
Evaluations returns total evaluations performed succesfully during p's lifetime.
type PowerPreference ¶
type PowerPreference uint8
PowerPreference is a hint for which GPU adapter to select on systems with more than one.
const ( PowerPreferenceUndefined PowerPreference = iota PowerPreferenceLowPower PowerPreferenceHighPerformance )
type SDF2 ¶
type SDF2 struct {
// contains filtered or unexported fields
}
SDF2 is a 2D signed distance field evaluated on the GPU. It implements the gleval.SDF2 interface.
func (*SDF2) Configure ¶
Configure compiles the compute program in cfg.Source and allocates GPU memory for evaluation. bb is the bounding box of the SDF. Any previous GPU resources held by s are released.
func (*SDF2) Evaluate ¶
Evaluate evaluates the SDF at positions pos and stores the result in dist. Blocks until the GPU is done. Implements gleval.SDF2.
func (*SDF2) Evaluations ¶
Evaluations returns total evaluations performed succesfully during sdf's lifetime.
func (*SDF2) Poll ¶
Poll checks if the submitted evaluation has finished without blocking. See SDF3.Poll.
type SDF3 ¶
type SDF3 struct {
// contains filtered or unexported fields
}
SDF3 is a 3D signed distance field evaluated on the GPU. It implements the gleval.SDF3 interface.
func (*SDF3) Configure ¶
Configure compiles the compute program in cfg.Source and allocates GPU memory for evaluation. bb is the bounding box of the SDF. Any previous GPU resources held by s are released.
func (*SDF3) Evaluate ¶
Evaluate evaluates the SDF at positions pos and stores the result in dist. Blocks until the GPU is done. Implements gleval.SDF3.
func (*SDF3) Evaluations ¶
Evaluations returns total evaluations performed succesfully during sdf's lifetime.
func (*SDF3) Poll ¶
Poll checks if the submitted evaluation has finished without blocking. Returns done=true if results can be read without blocking with SDF3.Read.
func (*SDF3) Read ¶
Read waits for the submitted evaluation to finish and stores the resulting distances in dist. len(dist) must be equal to the number of positions submitted. Waiting uses the DeviceConfig Backoff strategy.
func (*SDF3) Submit ¶
Submit uploads positions to the GPU and submits evaluation of the SDF. It does not wait for the evaluation to finish. Results are retrieved with SDF3.Read. Use SDF3.Poll to check if results are ready without blocking. On WebAssembly targets Submit may block until the work is done.
type UIConfig ¶
type UIConfig struct {
// Width and Height of the window in pixels. If zero a default size is used.
// Ignored on WebAssembly targets if CanvasID references an existing canvas.
Width, Height int
// Context cancels the UI when done. May be nil.
Context context.Context
// CanvasID is the HTML id of the canvas element the visualizer renders to on WebAssembly targets.
// If empty or not found a new canvas is created and appended to the document body.
CanvasID string
}
UIConfig configures the interactive 3D shape visualizer. See UI.