Documentation
¶
Overview ¶
Package vulkan is a pure-Go (no cgo) binding to the part of Vulkan a compute workload needs: the loader, an instance with the optional Khronos validation layer, a physical-device pick, one device and one compute queue, host-visible coherent buffers, shader modules from SPIR-V words, compute pipelines with their descriptor sets, command buffers, dispatch and fences.
It calls the system loader libvulkan.so.1 through purego, so a binary built with CGO_ENABLED=0 can be cross-compiled and copied to a machine that has a Vulkan driver. It is Linux 64-bit only (amd64, arm64); elsewhere Open returns ErrUnsupported. There is no window-system integration and no graphics pipeline.
Threading: a queue is externally synchronised, so a Device and everything made from it belong to one goroutine at a time.
Close order: close buffers, pipelines, shaders, command buffers and fences before their Device, and the Device before the Instance.
Index ¶
- Constants
- Variables
- func Pick(infos []DeviceInfo, f Filter) (int, error)
- type Binding
- type Buffer
- type BufferUsage
- type CommandBuffer
- type DescriptorKind
- type Device
- func (d *Device) Close() error
- func (d *Device) Info() DeviceInfo
- func (d *Device) NewBuffer(size uint64, usage BufferUsage) (*Buffer, error)
- func (d *Device) NewCommandBuffer() (*CommandBuffer, error)
- func (d *Device) NewComputePipeline(sh *Shader, entry string, bindings []Binding) (*Pipeline, error)
- func (d *Device) NewDeviceBuffer(size uint64, usage BufferUsage) (*Buffer, error)
- func (d *Device) NewReadbackBuffer(size uint64, usage BufferUsage) (*Buffer, error)
- func (d *Device) NewShader(words []uint32) (*Shader, error)
- func (d *Device) RobustBufferAccess() bool
- func (d *Device) Submit(c *CommandBuffer) (*Fence, error)
- type DeviceInfo
- type DeviceType
- type Error
- type Fence
- type Filter
- type Instance
- type Options
- type PhysicalDevice
- type Pipeline
- type Result
- type Shader
- type Version
Constants ¶
const ( UsageStorage = BufferUsage(vk.BufferUsageStorageBufferBit) UsageUniform = BufferUsage(vk.BufferUsageUniformBufferBit) UsageTransferSrc = BufferUsage(vk.BufferUsageTransferSrcBit) UsageTransferDst = BufferUsage(vk.BufferUsageTransferDstBit) )
The usages a compute workload needs.
const ( DeviceTypeOther = DeviceType(vk.PhysicalDeviceTypeOther) DeviceTypeIntegrated = DeviceType(vk.PhysicalDeviceTypeIntegratedGpu) DeviceTypeDiscrete = DeviceType(vk.PhysicalDeviceTypeDiscreteGpu) DeviceTypeVirtual = DeviceType(vk.PhysicalDeviceTypeVirtualGpu) DeviceTypeCPU = DeviceType(vk.PhysicalDeviceTypeCpu) )
The device types, in the registry's numbering.
const ( VendorAMD uint32 = 0x1002 VendorNVIDIA uint32 = 0x10de VendorIntel uint32 = 0x8086 )
Well-known PCI vendor identifiers.
const ( StorageBuffer = DescriptorKind(vk.DescriptorTypeStorageBuffer) UniformBuffer = DescriptorKind(vk.DescriptorTypeUniformBuffer) )
The kinds a compute shader here uses.
Variables ¶
var ( ErrOutOfHostMemory = &Error{Result: Result(vk.ErrorOutOfHostMemory)} ErrOutOfDeviceMemory = &Error{Result: Result(vk.ErrorOutOfDeviceMemory)} ErrInitializationFailed = &Error{Result: Result(vk.ErrorInitializationFailed)} ErrDeviceLost = &Error{Result: Result(vk.ErrorDeviceLost)} ErrMemoryMapFailed = &Error{Result: Result(vk.ErrorMemoryMapFailed)} ErrLayerNotPresent = &Error{Result: Result(vk.ErrorLayerNotPresent)} ErrExtensionNotPresent = &Error{Result: Result(vk.ErrorExtensionNotPresent)} ErrFeatureNotPresent = &Error{Result: Result(vk.ErrorFeatureNotPresent)} ErrIncompatibleDriver = &Error{Result: Result(vk.ErrorIncompatibleDriver)} ErrTooManyObjects = &Error{Result: Result(vk.ErrorTooManyObjects)} ErrFragmentedPool = &Error{Result: Result(vk.ErrorFragmentedPool)} ErrOutOfPoolMemory = &Error{Result: Result(vk.ErrorOutOfPoolMemory)} ErrUnknown = &Error{Result: Result(vk.ErrorUnknown)} )
The VkResult errors a compute client can meet. errors.Is(err, ErrDeviceLost) is true for any *Error that carries that code.
var ( // ErrUnsupported means this platform has no binding (it is Linux, 64-bit // only) or the Vulkan loader library is not installed. ErrUnsupported = errors.ErrUnsupported // ErrNoDevice means no physical device matches the filter. ErrNoDevice = errors.New("vulkan: no matching physical device") // ErrTooLarge means a size exceeds a limit of the device or of the process. ErrTooLarge = errors.New("vulkan: size exceeds a device limit") // ErrInvalid means an argument the binding refuses before calling Vulkan. ErrInvalid = errors.New("vulkan: invalid argument") // ErrNoMemoryType means the device offers no host-visible coherent memory // for the buffer. ErrNoMemoryType = errors.New("vulkan: no host-visible coherent memory type") // ErrClosed means the object, or the device it belongs to, was closed. ErrClosed = errors.New("vulkan: object is closed") )
Errors of this package that are not a VkResult.
Functions ¶
Types ¶
type Binding ¶
type Binding struct {
Binding uint32
Kind DescriptorKind
}
Binding declares one binding of descriptor set 0.
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a VkBuffer. A buffer from NewBuffer is bound to host-visible, host-coherent memory that stays mapped for its whole life; one from NewDeviceBuffer is in device-local memory and is never mapped.
func (*Buffer) Bytes ¶
Bytes is the mapped memory: len(Bytes()) is the size asked for. It is valid until Close, and writes by the host are visible to the device at the next submission (the memory is coherent). It is nil for a device buffer.
type CommandBuffer ¶
type CommandBuffer struct {
// contains filtered or unexported fields
}
CommandBuffer is a primary command buffer with a command pool of its own.
func (*CommandBuffer) Barrier ¶ added in v0.2.0
func (c *CommandBuffer) Barrier() error
Barrier records a memory dependency from every compute-shader and transfer write submitted to the queue before it to every compute-shader and transfer read or write submitted after it: a global VkMemoryBarrier with srcStageMask COMPUTE_SHADER|TRANSFER, srcAccessMask SHADER_WRITE|TRANSFER_WRITE, dstStageMask COMPUTE_SHADER|TRANSFER and dstAccessMask SHADER_READ|SHADER_WRITE|TRANSFER_READ|TRANSFER_WRITE.
Record it before a Dispatch or Copy that reads or overwrites what an earlier one wrote, whether that one is in this command buffer or in a command buffer submitted before it on the same queue and still running. Without it nothing orders them: queue submission order only orders the start of commands, so two submissions can overlap, and the later one can read stale data (a read-after-write hazard) or have its write overwritten (write-after-write).
Why this and no semaphore: the Vulkan specification, vkCmdPipelineBarrier, says that outside a render pass "the first synchronization scope includes all commands that occur earlier in submission order" and "the second synchronization scope includes all commands that occur later in submission order", each limited to the stages of the masks, and that the barrier "defines a memory dependency between commands that were submitted to the same queue before it" and after it. Both submissions here are on one queue, so a barrier at the start of the second one orders its dispatch after the first one's writes, with no semaphore and no host wait.
func (*CommandBuffer) Begin ¶
func (c *CommandBuffer) Begin() error
Begin starts recording. Beginning an already recording buffer is ErrInvalid. The buffer is recorded for one submission at a time (reset by Begin).
func (*CommandBuffer) Close ¶
func (c *CommandBuffer) Close()
Close destroys the command pool, which frees the buffer. It is safe to call twice, and a no-op once the device is closed. No submission may be pending.
func (*CommandBuffer) Copy ¶ added in v0.2.0
func (c *CommandBuffer) Copy(dst, src *Buffer, size uint64) error
Copy records a copy of size bytes from the start of src to the start of dst, then a barrier that makes the written bytes visible to the host (so a host buffer can be read after the fence). src needs UsageTransferSrc and dst UsageTransferDst. Order a Copy after a Dispatch that wrote src, or before a Dispatch that reads dst, with Barrier.
func (*CommandBuffer) Dispatch ¶
func (c *CommandBuffer) Dispatch(p *Pipeline, x, y, z uint32) error
Dispatch records: bind the pipeline and its descriptor set, dispatch x*y*z workgroups, then a barrier that makes the shader's writes visible to the host, so the mapped buffers can be read after the fence. The counts are checked against maxComputeWorkGroupCount.
type DescriptorKind ¶
type DescriptorKind int32
DescriptorKind is the kind of resource bound at a binding.
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device is a VkDevice with one compute queue.
func (*Device) Close ¶
Close waits for the queue to go idle and destroys the device. The caller has closed everything made from it. It is safe to call twice.
func (*Device) NewBuffer ¶
func (d *Device) NewBuffer(size uint64, usage BufferUsage) (*Buffer, error)
NewBuffer creates a buffer of size bytes in host-visible, host-coherent memory, mapped. The size is checked against the device limits first; every handle made on the way is destroyed on an error.
func (*Device) NewCommandBuffer ¶
func (d *Device) NewCommandBuffer() (*CommandBuffer, error)
NewCommandBuffer creates a command pool on the compute queue family and one command buffer from it. On an error nothing is left alive.
func (*Device) NewComputePipeline ¶
func (d *Device) NewComputePipeline(sh *Shader, entry string, bindings []Binding) (*Pipeline, error)
NewComputePipeline builds the descriptor set layout for bindings, the pipeline layout, the pipeline for entry point entry of sh, and a descriptor set. On an error nothing is left alive.
func (*Device) NewDeviceBuffer ¶ added in v0.2.0
func (d *Device) NewDeviceBuffer(size uint64, usage BufferUsage) (*Buffer, error)
NewDeviceBuffer creates a buffer of size bytes in device-local memory that the host does not map: Bytes is nil. Fill it and read it back with CommandBuffer.Copy from and to a host buffer, which must have UsageTransferSrc or UsageTransferDst respectively; give the device buffer the same usage the copy needs (UsageTransferDst to be filled, UsageTransferSrc to be read) plus UsageStorage or UsageUniform for a shader. On a device with one memory type that is both device-local and host-visible (integrated GPUs, lavapipe) this is the same memory as NewBuffer's, unmapped; on a discrete GPU it is VRAM that the CPU cannot touch. The size limits are NewBuffer's.
func (*Device) NewReadbackBuffer ¶ added in v0.3.0
func (d *Device) NewReadbackBuffer(size uint64, usage BufferUsage) (*Buffer, error)
NewReadbackBuffer is NewBuffer for a buffer the CPU will READ: it prefers host-cached memory. NewBuffer's first host-visible, host-coherent type is, on a discrete GPU, write-combined: the CPU writes it fast and reads it at a few hundred MB/s (an uncached read per cache line). A cached type is slower for the GPU to write over PCIe than for nothing, but the CPU reads it at memory speed, which is what a download staging buffer wants. It is coherent too, so no invalidate is needed. Where the device has no cached type this is NewBuffer.
func (*Device) NewShader ¶
NewShader creates a shader module from SPIR-V words (for instance from go-gfx/wgsl's spirv.Module). The words are checked for the magic number and a sane length; the driver (and the validation layer) check the rest.
func (*Device) RobustBufferAccess ¶ added in v0.2.0
RobustBufferAccess reports whether the device was created with the robustBufferAccess feature enabled: it is whenever the device offers it.
type DeviceInfo ¶
type DeviceInfo struct {
Index int // position in the enumeration
Name string
Type DeviceType
VendorID uint32
DeviceID uint32
APIVersion Version
DriverVersion uint32 // vendor-specific encoding; Mesa drivers use Version's
DriverName string // from VkPhysicalDeviceDriverProperties
DriverInfo string
// Limits the binding checks before it allocates or dispatches.
MaxStorageBufferRange uint32
MaxUniformBufferRange uint32
MaxMemoryAllocationSize uint64
MaxComputeWorkGroupCount [3]uint32
// Optional features and properties, reported so that a client can use them.
//
// RobustBufferAccess is the device's robustBufferAccess feature: offered,
// and enabled by PhysicalDevice.Open whenever it is (Device.RobustBufferAccess).
// RoundingModeRTEFloat32 is shaderRoundingModeRTEFloat32 of the Vulkan 1.2
// float-controls properties: a SPIR-V module may declare the RoundingModeRTE
// execution mode for 32-bit floats only where it is true
// (VUID-RuntimeSpirv-shaderRoundingModeRTEFloat32-06303). It is false on a
// device that does not report the properties.
RobustBufferAccess bool
RoundingModeRTEFloat32 bool
}
DeviceInfo is what a physical device reports.
type Error ¶
Error is a Vulkan call that returned a negative VkResult. Use errors.Is with the sentinels below to test for one code, or errors.As to read the call.
type Fence ¶
type Fence struct {
// contains filtered or unexported fields
}
Fence is a VkFence that signals when a submission completes.
type Filter ¶
type Filter struct {
VendorID uint32 // 0 for any vendor
Types []DeviceType // empty for any type
}
Filter narrows Pick. The zero Filter accepts every device.
type Instance ¶
type Instance struct {
// contains filtered or unexported fields
}
Instance is a VkInstance.
func (*Instance) Close ¶
Close destroys the instance and closes the loader. It is safe to call twice.
func (*Instance) Devices ¶
func (in *Instance) Devices() ([]*PhysicalDevice, error)
Devices lists the physical devices.
type Options ¶
type Options struct {
// ApplicationName is reported to the driver. It may be empty.
ApplicationName string
// Validation enables VK_LAYER_KHRONOS_validation. When the layer is not
// installed Open fails with ErrLayerNotPresent: a validation run that
// silently validated nothing would prove nothing.
Validation bool
}
Options configures Open.
type PhysicalDevice ¶
type PhysicalDevice struct {
Info DeviceInfo
// contains filtered or unexported fields
}
PhysicalDevice is a VkPhysicalDevice. It needs no Close.
func (*PhysicalDevice) Open ¶
func (pd *PhysicalDevice) Open() (*Device, error)
Open creates a device with one compute queue on this physical device.
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline is a compute pipeline with its layout, and one descriptor set (set 0) allocated from a pool of its own.
func (*Pipeline) Bind ¶
Bind points a binding of the pipeline's descriptor set at a whole buffer. The binding must be declared by the pipeline, and the buffer must have the usage that kind of binding needs. Bind before recording the dispatch that uses it, and not while a submission that uses the set is in flight.
type Result ¶
type Result int32
Result is a VkResult. Negative values are errors, zero is success, positive values are successful statuses (VK_NOT_READY, VK_TIMEOUT, VK_INCOMPLETE).
type Shader ¶
type Shader struct {
// contains filtered or unexported fields
}
Shader is a VkShaderModule.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
vkgen
command
Command vkgen generates the Go mirrors of the Vulkan structures that go-gfx/vulkan uses from the Khronos registry (vk.xml).
|
Command vkgen generates the Go mirrors of the Vulkan structures that go-gfx/vulkan uses from the Khronos registry (vk.xml). |
|
internal
|
|
|
native
Package native opens the system Vulkan loader, libvulkan.so.1, through purego and resolves the entry points of internal/vk's function tables.
|
Package native opens the system Vulkan loader, libvulkan.so.1, through purego and resolves the entry points of internal/vk's function tables. |
|
vkgen
Package vkgen reads the Khronos registry (vk.xml) and generates the Go mirrors of the Vulkan structures, enumerations and handles that go-gfx/vulkan calls.
|
Package vkgen reads the Khronos registry (vk.xml) and generates the Go mirrors of the Vulkan structures, enumerations and handles that go-gfx/vulkan calls. |