gpu

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 21 Imported by: 1 module, 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.

Documentation

Overview

Package gpu renders on the GPU through Vulkan, into memory shared with the compositor (or a display) as dmabufs: what a frame draws is handed over by file descriptor, with no copy. lys-ui's windows, lys-shell's surfaces and lys-wm's output all draw through it.

Vulkan loads at runtime (third_party/vk, pure Go, no cgo). Open fails with ErrNoVulkan or ErrNoDevice where there is no loader or no device that draws; callers then draw on the CPU (package render). A device that cannot export dmabufs still renders offscreen (NewOffscreen); its NewTarget fails with ErrNoDmabuf.

A Device and everything made from it belong to one goroutine.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoDevice = errors.New("gpu: no Vulkan device draws")

ErrNoDevice reports that no Vulkan device has a graphics queue.

View Source
var ErrNoDmabuf = errors.New("gpu: the device does not export dmabufs")

ErrNoDmabuf reports a device that cannot render into dmabufs.

View Source
var ErrNoVideoEncoder = errors.New("gpu: the device has no video encoder")

ErrNoVideoEncoder reports a device that does not encode the codec asked for: no video encode queue (lavapipe, older radv and anv), or one without that codec.

View Source
var ErrNoVulkan = errors.New("gpu: no Vulkan loader")

ErrNoVulkan reports that the Vulkan loader (libvulkan) is missing.

Functions

This section is empty.

Types

type Buffer

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

Buffer is an image whose memory is a dmabuf this device allocates for others to write and read, a screencast frame a compositor copies into, and never draws into itself: it asks only for transfer and sampling, so layouts a device cannot render to (NVIDIA's linear) are on offer (ExportModifiers).

func (*Buffer) Close

func (b *Buffer) Close()

Close releases the buffer and its fd; closing it again, or after its device, does nothing.

func (*Buffer) Dmabuf

func (b *Buffer) Dmabuf() dmabuf.Buffer

Dmabuf describes the buffer's memory. Its fd is the buffer's, open until Close.

func (*Buffer) ReadPixels

func (b *Buffer) ReadPixels() (*image.RGBA, error)

ReadPixels copies what the buffer holds (the frame a compositor last copied into it) back into memory as RGBA, opaque when its format has no alpha: screenshots and tests.

type Codec

type Codec int

Codec is a video coding format the encoder writes.

const (
	H264 Codec = iota
	H265
)

The codecs.

func (Codec) String

func (c Codec) String() string

type Device

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

Device is a Vulkan device rendering into dmabufs.

func Open

func Open() (*Device, error)

Open picks the first Vulkan device with a graphics queue that exports dmabufs, else the first with a graphics queue.

func OpenNode

func OpenNode(dev uint64) (*Device, error)

OpenNode opens the Vulkan device behind the DRM node dev, its device number (st_rdev, as of /dev/dri/renderD128 or /dev/dri/card0): the device a dmabuf consumer negotiated, which must export dmabufs. linux-dmabuf's main device may name either node of a GPU.

func (*Device) Close

func (d *Device) Close()

Close releases the device and the targets still open on it: a target closed after is already closed. Closing it again does nothing.

func (*Device) Codecs

func (d *Device) Codecs() []Codec

Codecs are the codecs the device encodes; none on a device without a video encode queue.

func (*Device) ExportModifiers

func (d *Device) ExportModifiers(format uint32) ([]uint64, error)

ExportModifiers are the single-plane DRM modifiers the device allocates format in for a Buffer: what to intersect with the ones a consumer imports before NewBuffer.

func (*Device) Exports

func (d *Device) Exports() bool

Exports reports whether the device renders into dmabufs, for NewTarget.

func (*Device) Modifiers

func (d *Device) Modifiers(format uint32) ([]uint64, error)

Modifiers are the DRM modifiers the device renders format into as a single-plane color attachment: what to intersect with the ones a consumer imports before NewTarget.

func (*Device) Name

func (d *Device) Name() string

Name is the device's name, "NVIDIA GeForce RTX 3060 Ti" or "llvmpipe (LLVM 21.1.8, 256 bits)".

func (*Device) NewBuffer

func (d *Device) NewBuffer(width, height int, format uint32, offered []uint64) (*Buffer, error)

NewBuffer allocates a width x height Buffer in format (a DRM fourcc: ARGB8888 or XRGB8888) whose layout the driver picks among offered, the modifiers the consumer imports, that the device exports format in (ExportModifiers). None in common, as with only the implicit dmabuf.ModifierInvalid on offer, is an error naming both lists.

func (*Device) NewEncoder

func (d *Device) NewEncoder(cfg EncoderConfig) (*Encoder, error)

NewEncoder makes an encoder for cfg: ErrNoVideoEncoder when the device does not encode cfg.Codec, an error naming the limit when it cannot encode cfg's size.

func (*Device) NewOffscreen

func (d *Device) NewOffscreen(width, height int) (*Target, error)

NewOffscreen allocates a width x height target in device memory no other process imports: a frame rendered for its pixels (ReadPixels), a screenshot or a test.

func (*Device) NewTarget

func (d *Device) NewTarget(width, height int, format uint32, modifiers []uint64) (*Target, error)

NewTarget allocates a width x height image in format (a DRM fourcc: dmabuf.FormatARGB8888, or FormatXRGB8888 with the same bytes) whose layout the driver picks among modifiers, the ones the consumer imports, that the device renders format in (Modifiers): linear when both take it. None in common, as with only the implicit dmabuf.ModifierInvalid on offer, is an error naming both lists.

func (*Device) Render

func (d *Device) Render(l *render.List, t *Target) error

Render draws l onto t and hands t back to its consumer. l is recorded at t's size; its layers keep their GPU surfaces on this device across frames.

type Encoder

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

Encoder encodes frames into H.264 or H.265 on the device's video queue. A frame's pixels come from a Buffer a compositor copied a screen into: a compute pass converts them to NV12, and the video queue encodes that while the next frame converts. Like the Device, it belongs to one goroutine.

func (*Encoder) Close

func (e *Encoder) Close()

Close waits for the frames in flight, dropping their packets, and releases the encoder; closing it again does nothing.

func (*Encoder) Encode

func (e *Encoder) Encode(src *Buffer, pts time.Duration) ([]Packet, error)

Encode converts src's pixels (a Buffer the size the encoder was made for) and queues them for encoding, presented at pts; it returns the packets of the frames that finished meanwhile, oldest first. src is free to take the next frame when Encode returns.

func (*Encoder) Flush

func (e *Encoder) Flush() ([]Packet, error)

Flush waits for the frames in flight and returns their packets, oldest first.

func (*Encoder) ParameterSets

func (e *Encoder) ParameterSets() [][]byte

ParameterSets are the stream's parameter sets as NAL units without start codes: the SPS and the PPS for H.264, the VPS, SPS and PPS for H.265.

func (*Encoder) SetOverlay

func (e *Encoder) SetOverlay(img *image.RGBA, dst image.Rectangle) error

SetOverlay draws img over every following frame's pixels, scaled (linear filtering) to fill dst in frame coordinates, opaque, before the NV12 conversion; nil removes it. img is uploaded here, so the caller may reuse it once SetOverlay returns. dst is clipped to the frame; an empty dst, or one off the frame, is an error.

type EncoderConfig

type EncoderConfig struct {
	Codec Codec
	// Width and Height are the frame size. The stream is coded in the
	// device's units and at least its smallest size, the padding's
	// pixels the edge's repeated and cropped off on decode; an odd size
	// decodes one pixel larger, its last row or column twice.
	Width, Height int
	// FrameRate is the frames a second the stream runs at, for its
	// level; frames may come at any time.
	FrameRate int
	// KeyInterval is the frames from one keyframe to the next.
	KeyInterval int
	// Source is the region of the buffer Encode reads, in the buffer's
	// own orientation; the zero rectangle reads all of it.
	Source image.Rectangle
	// Transform is the captured output's wl_output transform: the
	// buffer holds the output's pixels so turned, and the encoder turns
	// them back, so the frame shows what the output shows. Width and
	// Height are the frame's, turned: a 90 or 270 degree transform
	// swaps the source region's.
	Transform Transform
	// QP is the constant quantiser, 0 (best) to 51. A device without
	// constant-QP coding runs its own rate control at a bitrate
	// matching QP 20 on desktop content instead.
	QP int
}

EncoderConfig is a video stream's shape.

type Packet

type Packet struct {
	Data []byte
	PTS  time.Duration
	Key  bool
}

Packet is one encoded frame: an Annex-B access unit (start-code delimited NAL units, without the parameter sets, which ParameterSets gives), the time it was handed in at, and whether it is a keyframe (an IDR).

type Target

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

Target is an image the GPU renders into whose memory is a dmabuf: a window's frame handed to the compositor, or an output's scanout buffer. Its layout (the DRM modifier) is one the caller offered, as the consumer imports it. Between frames it belongs to the consumer.

func (*Target) Close

func (t *Target) Close()

Close releases the target and its dmabuf fd; closing it again, or after its device, does nothing.

func (*Target) Dmabuf

func (t *Target) Dmabuf() dmabuf.Buffer

Dmabuf describes the target's memory for its consumer. Its fd is the target's, open until Close; an offscreen target has none.

func (*Target) ReadPixels

func (t *Target) ReadPixels() (*image.RGBA, error)

ReadPixels copies the target back into memory as premultiplied RGBA: tests and screenshots.

func (*Target) Size

func (t *Target) Size() image.Point

Size is the target's size in pixels.

type Transform

type Transform int

Transform is a wl_output transform: how an output's pixels are turned in its buffer.

const (
	TransformNormal Transform = iota
	Transform90
	Transform180
	Transform270
	TransformFlipped
	TransformFlipped90
	TransformFlipped180
	TransformFlipped270
)

The transforms, valued as wl_output's.

Jump to

Keyboard shortcuts

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