sdlgpu

package module
v0.2.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

meui-render-sdlgpu

SDL3 GPU backend for MeUI. One device implementation targets Vulkan, Metal and Direct3D 12 through SDL's native GPU abstraction.

Canvas renderer

NewRenderer / NewRendererForDevice implement graphics.Device. Draw calls are recorded into a frame and submitted by Present; cmd/canvas-demo drives a real MeUI Canvas through the backend.

Implemented:

  • real SDL GPU device creation and native driver selection (vulkan, metal, direct3d12);
  • window creation/claim, swapchain acquisition, clear and present;
  • SPIR-V/MSL/DXIL shader creation from generated meui-shaders artifacts;
  • pipelines for triangles, rects, simplified SDF and glyph text;
  • vertex/uniform uploads, sampler/texture binding and push uniforms;
  • RGBA texture uploads through transfer buffers;
  • real strings through the core font/atlas path;
  • triangles, rectangles, rounded SDF rectangles and textured image quads.

Runtime verification

TestOffscreenClear creates a native GPU device, a 64×64 color target, runs a render pass, submits it and waits. It passes on GitHub Actions for:

Platform Driver
Ubuntu 24.04 + Mesa Vulkan
macOS arm64 Metal
Windows Server 2025 Direct3D 12

Workflow: .github/workflows/runtime.yml in this repository. This proves native command submission, not an interactive window/swapchain on every host.

Limitations

  • The full SDF/image pipeline with texture arrays is not used by SDL GPU; the simplified sdf_simple variant avoids a Vulkan pipeline-compiler crash in SDL.
  • Rotation-aware presentation paths and offscreen path/arc tessellation are not implemented.
  • Metal/D3D12 are tested offscreen; full window presentation still needs a platform host smoke test.
go run ./cmd/canvas-demo -frames 3
go run ./cmd/sdlgpu-demo

MIT.

Documentation

Overview

Package sdlgpu is the SDL3 GPU backend for MeUI. It creates a native GPU device through SDL (Vulkan, Metal or D3D12) and presents frames through a claimed window.

This is deliberately a separate module from meui-render-gl: it does not use OpenGL or EGL, and it selects its shaders from the generated artifacts in meui-shaders.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer added in v0.1.2

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is a GPU vertex or uniform buffer.

type Device

type Device struct {
	// contains filtered or unexported fields
}

Device owns one SDL GPU device and the windows claimed for presentation.

func New

func New(debug bool) (*Device, error)

New creates a GPU device, trying every shader format SDL can provide. SDL picks the native backend (Vulkan/Metal/D3D12) from the platform.

func (*Device) Claim

func (d *Device) Claim(window unsafe.Pointer) error

Claim makes a window presentable through this device.

func (*Device) Clear

func (d *Device) Clear(window unsafe.Pointer, c FColor) error

Clear acquires a swapchain, clears it and submits. It is the smallest real frame and is used to validate the device/window path before the full batch renderer lands.

func (*Device) Close

func (d *Device) Close()

Close waits for the GPU and destroys the device.

func (*Device) CreateNearestSampler added in v0.1.7

func (d *Device) CreateNearestSampler() (*Sampler, error)

CreateNearestSampler creates a clamp-to-edge nearest sampler.

func (*Device) CreatePipeline added in v0.1.2

func (d *Device) CreatePipeline(vs, fs *Shader, window unsafe.Pointer) (*Pipeline, error)

CreatePipeline builds a pipeline for the tri shader layout.

func (*Device) CreateRectPipeline added in v0.1.3

func (d *Device) CreateRectPipeline(vs, fs *Shader, window unsafe.Pointer) (*Pipeline, error)

CreateRectPipeline builds a pipeline for rect.vert/frag: one float2 vertex.

func (*Device) CreateSDFPipeline added in v0.1.6

func (d *Device) CreateSDFPipeline(vs, fs *Shader, window unsafe.Pointer) (*Pipeline, error)

CreateSDFPipeline builds the 16-attribute SDF pipeline. It is paired with the simplified solid SDF fragment (no textures or clip loop).

func (*Device) CreateShader added in v0.1.1

func (d *Device) CreateShader(stage int, format uint32, entry string, code []byte, uniformBuffers uint32) (*Shader, error)

CreateShader uploads one stage. The caller owns the code slice for the call; SDL copies it into the backend shader object.

func (*Device) CreateShaderFromLibrary added in v0.2.2

func (d *Device) CreateShaderFromLibrary(stage int, base string, samplers, uniforms uint32) (*Shader, error)

CreateShaderFromLibrary reads one stage from meui-shaders for the native driver and creates the SDL shader. Entry points are vs_main/fs_main.

func (*Device) CreateShaderWithSamplers added in v0.1.7

func (d *Device) CreateShaderWithSamplers(stage int, format uint32, entry string, code []byte, samplers, uniformBuffers uint32) (*Shader, error)

CreateShaderWithSamplers is CreateShader with an explicit sampler count.

func (*Device) CreateTextPipeline added in v0.1.7

func (d *Device) CreateTextPipeline(vs, fs *Shader, window unsafe.Pointer) (*Pipeline, error)

CreateTextPipeline builds the glyph atlas pipeline (5 vertex attributes).

func (*Device) CreateTexture2D added in v0.1.7

func (d *Device) CreateTexture2D(w, h uint32) (*Texture, error)

CreateTexture2D creates an uninitialised sampled 2D texture.

func (*Device) CreateWindow

func (d *Device) CreateWindow(title string, w, h int32) unsafe.Pointer

CreateWindow creates a plain SDL window for GPU presentation. Unlike meui-sdl's InitWindow it creates no OpenGL context.

func (*Device) DestroyWindow

func (d *Device) DestroyWindow(win unsafe.Pointer)

DestroyWindow destroys a window from CreateWindow.

func (*Device) DrawRect added in v0.1.3

func (d *Device) DrawRect(window unsafe.Pointer, p *Pipeline, x, y, w, h float32, color [4]float32) error

DrawRect presents a solid rectangle with the rect pipeline.

func (*Device) DrawSDFRect added in v0.1.6

func (d *Device) DrawSDFRect(window unsafe.Pointer, p *Pipeline, radius, x, y, w, h float32, color [4]float32) error

DrawSDFRect presents one solid rounded rectangle through the simplified SDF shader.

func (*Device) DrawString added in v0.1.9

func (d *Device) DrawString(window unsafe.Pointer, p *Pipeline, x, y float32, text string, f graphics.Font, col graphics.Color) error

DrawString rasterises a string with the core graphics font, uploads the result as an RGBA texture and draws it through the text pipeline. This is the CPU-rasterised path used until a shared glyph atlas lands.

func (*Device) DrawTextQuad added in v0.1.7

func (d *Device) DrawTextQuad(window unsafe.Pointer, p *Pipeline, tex *Texture, samp *Sampler, x, y, w, h float32, color [4]float32) error

DrawTextQuad presents one textured quad through the text shader. With a dummy atlas this validates the sampler path; real glyph uploads come next.

func (*Device) DrawTriangle added in v0.1.2

func (d *Device) DrawTriangle(window unsafe.Pointer, p *Pipeline, vb *Buffer, uniform []float32) error

DrawTriangle presents a single triangle using the tri shader. It is the first end-to-end shader/pipeline/vertex/uniform draw.

func (*Device) Driver

func (d *Device) Driver() string

Driver reports the native backend ("vulkan", "metal", "d3d12").

func (*Device) NewBuffer added in v0.1.2

func (d *Device) NewBuffer(usage uint32, data []byte) (*Buffer, error)

NewBuffer uploads bytes into a GPU buffer with the given usage flags.

func (*Device) OffscreenClear added in v0.2.4

func (d *Device) OffscreenClear(c FColor) error

OffscreenClear creates a color-target texture, clears it in a GPU render pass and waits for completion. It validates the native backend on machines with no display server and is used by the cross-platform runtime smoke test.

func (*Device) ReleaseBuffer added in v0.1.2

func (d *Device) ReleaseBuffer(b *Buffer)

Release destroys a buffer.

func (*Device) ReleasePipeline added in v0.1.2

func (d *Device) ReleasePipeline(p *Pipeline)

Release destroys a pipeline.

func (*Device) ReleaseSampler added in v0.1.7

func (d *Device) ReleaseSampler(s *Sampler)

func (*Device) ReleaseShader added in v0.1.1

func (d *Device) ReleaseShader(s *Shader)

Release destroys a shader.

func (*Device) ReleaseTexture added in v0.1.7

func (d *Device) ReleaseTexture(t *Texture)

func (*Device) ReleaseWindow

func (d *Device) ReleaseWindow(window unsafe.Pointer)

ReleaseWindow releases a claimed window.

func (*Device) ShaderTarget added in v0.2.2

func (d *Device) ShaderTarget() (dir string, format uint32)

ShaderTarget reports the shader directory and SDL shader format for the native driver. It is how callers pick MSL, DXIL or SPIR-V without a platform build tag.

func (*Device) UploadRGBA added in v0.1.8

func (d *Device) UploadRGBA(t *Texture, pixels []byte) error

UploadRGBA uploads tightly packed RGBA8 pixels into a 2D texture.

type FColor

type FColor struct{ R, G, B, A float32 }

FColor is a linear RGBA colour.

type Pipeline added in v0.1.2

type Pipeline struct {
	// contains filtered or unexported fields
}

Pipeline is an SDL GPU graphics pipeline.

type Renderer added in v0.2.0

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer adapts the SDL GPU pipelines to graphics.Device. Draw calls are recorded into a frame and submitted together by Present.

func NewRenderer added in v0.2.0

func NewRenderer(window unsafe.Pointer, width, height int) (*Renderer, error)

NewRenderer creates the device, claims the window and builds the three pipelines used by the Canvas adapter.

func NewRendererForDevice added in v0.2.0

func NewRendererForDevice(d *Device, window unsafe.Pointer, width, height int) (*Renderer, error)

NewRendererForDevice builds a renderer against an already-claimed device and window. The caller owns the device lifetime.

func (*Renderer) Clear added in v0.2.0

func (r *Renderer) Clear(col uint32)

func (*Renderer) ClearWithViewport added in v0.2.0

func (r *Renderer) ClearWithViewport(col uint32, x, y, w, h int32)

func (*Renderer) ClipBounds added in v0.2.0

func (r *Renderer) ClipBounds() graphics.Rect

func (*Renderer) ClipDepth added in v0.2.0

func (r *Renderer) ClipDepth() int

func (*Renderer) ClipPath added in v0.2.0

func (r *Renderer) ClipPath(path *graphics.Path) bool

func (*Renderer) ClipRect added in v0.2.0

func (r *Renderer) ClipRect(rect graphics.Rect)

func (*Renderer) Close added in v0.2.0

func (r *Renderer) Close()

func (*Renderer) DrawArc added in v0.2.0

func (r *Renderer) DrawArc(x, y, w, h, start, sweep float32, p *graphics.Paint)

func (*Renderer) DrawCircle added in v0.2.0

func (r *Renderer) DrawCircle(cx, cy, rad float32, p *graphics.Paint)

func (*Renderer) DrawDashedLine added in v0.2.0

func (r *Renderer) DrawDashedLine(x1, y1, x2, y2 float32, dash []float32, phase float32, p *graphics.Paint)

func (*Renderer) DrawImage added in v0.2.0

func (r *Renderer) DrawImage(img *graphics.Image, rect graphics.Rect, p *graphics.Paint)

func (*Renderer) DrawLine added in v0.2.0

func (r *Renderer) DrawLine(x1, y1, x2, y2 float32, p *graphics.Paint)

func (*Renderer) DrawOval added in v0.2.0

func (r *Renderer) DrawOval(x, y, w, h float32, p *graphics.Paint)

func (*Renderer) DrawPath added in v0.2.0

func (r *Renderer) DrawPath(path *graphics.Path, p *graphics.Paint)

func (*Renderer) DrawRect added in v0.2.0

func (r *Renderer) DrawRect(x, y, w, h float32, p *graphics.Paint)

func (*Renderer) DrawRoundedRect added in v0.2.0

func (r *Renderer) DrawRoundedRect(rect graphics.Rect, rx, ry float32, p *graphics.Paint)

func (*Renderer) DrawSimpleString added in v0.2.0

func (r *Renderer) DrawSimpleString(text string, x, y float32, f graphics.Font, p *graphics.Paint)

func (*Renderer) DrawText added in v0.2.0

func (r *Renderer) DrawText(text string, x, y float32, f graphics.Font, p *graphics.Paint)

func (*Renderer) DropShadow added in v0.2.0

func (r *Renderer) DropShadow(rect graphics.Rect, p *graphics.Paint, dx, dy, radius float32)

func (*Renderer) GetMatrix added in v0.2.0

func (r *Renderer) GetMatrix() graphics.Matrix

func (*Renderer) Height added in v0.2.0

func (r *Renderer) Height() float32

func (*Renderer) IsClipEmpty added in v0.2.0

func (r *Renderer) IsClipEmpty() bool

func (*Renderer) Present added in v0.2.0

func (r *Renderer) Present() error

Present submits the recorded frame.

func (*Renderer) QuickRejectRect added in v0.2.0

func (r *Renderer) QuickRejectRect(rect graphics.Rect) bool

func (*Renderer) RenderOffscreen added in v0.2.0

func (r *Renderer) RenderOffscreen(width, height int, draw func(*graphics.Canvas)) (*graphics.Image, error)

func (*Renderer) ResetClip added in v0.2.0

func (r *Renderer) ResetClip()

func (*Renderer) Restore added in v0.2.0

func (r *Renderer) Restore()

func (*Renderer) RestoreToCount added in v0.2.0

func (r *Renderer) RestoreToCount(n int)

func (*Renderer) Rotate added in v0.2.0

func (r *Renderer) Rotate(deg float32)

func (*Renderer) Save added in v0.2.0

func (r *Renderer) Save() int

func (*Renderer) Scale added in v0.2.0

func (r *Renderer) Scale(sx, sy float32)

func (*Renderer) Skew added in v0.2.0

func (r *Renderer) Skew(kx, ky float32)

func (*Renderer) TransformRect added in v0.2.0

func (r *Renderer) TransformRect(x, y, w, h float32) (float32, float32, float32, float32)

func (*Renderer) Translate added in v0.2.0

func (r *Renderer) Translate(dx, dy float32)

func (*Renderer) Width added in v0.2.0

func (r *Renderer) Width() float32

type Sampler added in v0.1.7

type Sampler struct {
	// contains filtered or unexported fields
}

type Shader added in v0.1.1

type Shader struct {
	// contains filtered or unexported fields
}

Shader is an SDL GPU shader object.

type Texture added in v0.1.7

type Texture struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis
cmd
canvas-demo command
Command canvas-demo drives a MeUI Canvas through the SDL GPU renderer.
Command canvas-demo drives a MeUI Canvas through the SDL GPU renderer.
sdlgpu-demo command
Command sdlgpu-demo presents a moving rectangle through the SDL3 GPU backend.
Command sdlgpu-demo presents a moving rectangle through the SDL3 GPU backend.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL