Last updated: 2026-09-07

Vulkan Runtime

Vulkan Buffer

All API Sections

Allocate Vulkan buffers used by weights, intermediates, and staging copies.

These helpers centralize buffer creation, memory mapping, and one-shot copy utilities so the rest of the runtime can work with higher-level abstractions.

2 exports 6 methods src/vulkan/buffer.zig

2 exports shown

struct

Buffer

#
pub const Buffer = struct

Vulkan buffer allocation paired with its device memory and optional mapped pointer.

src/vulkan/buffer.zig:12

Methods

6

method

Buffer.init

#
pub fn init( instance: *const Instance, size: vk.c.VkDeviceSize, usage: vk.c.VkBufferUsageFlags, mem_properties: vk.c.VkMemoryPropertyFlags, ) !Buffer

Create a Vulkan buffer and allocate backing device memory for it.

Parameters
instance
Active Vulkan instance and logical device.
size
Buffer size in bytes.
usage
Vulkan buffer usage flags.
mem_properties
Required Vulkan memory property flags for the allocation.
Returns

A Buffer with memory bound but not automatically mapped.

Notes

Use `initStaging()` when you need an immediately mapped upload buffer.

src/vulkan/buffer.zig:31

method

Buffer.initDeviceLocal

#
pub fn initDeviceLocal(instance: *const Instance, size: vk.c.VkDeviceSize, usage: vk.c.VkBufferUsageFlags) !Buffer

Create a device-local buffer for GPU-only reads and writes.

Parameters
instance
Active Vulkan instance and logical device.
size
Buffer size in bytes.
usage
Additional Vulkan usage flags for the buffer.
Returns

A device-local buffer with transfer-destination usage enabled.

Notes

This helper automatically adds `VK_BUFFER_USAGE_TRANSFER_DST_BIT` for staging uploads.

src/vulkan/buffer.zig:104

method

Buffer.initStaging

#
pub fn initStaging(instance: *const Instance, size: vk.c.VkDeviceSize) !Buffer

Create and immediately map a host-visible staging buffer.

Parameters
instance
Active Vulkan instance and logical device.
size
Buffer size in bytes.
Returns

A staging buffer ready for CPU writes through `mapped`.

Notes

The buffer uses coherent host memory so writes do not require an explicit flush.

src/vulkan/buffer.zig:118

method

Buffer.initHostVisibleStorage

#
pub fn initHostVisibleStorage(instance: *const Instance, size: vk.c.VkDeviceSize) !Buffer

Create a host-visible storage buffer that the GPU reads in place via PCIe.

it over PCIe BAR rather than from device-local VRAM. Used to offload large rarely-touched tensors (MoE expert weights) when device memory is insufficient. Coherent memory means no explicit invalidate is needed; we only write at load time.

Parameters
instance
Active Vulkan instance and logical device.
size
Buffer size in bytes.
Returns

A storage buffer ready for direct CPU writes through `mapped`.

Notes

Memory is allocated as `HOST_VISIBLE | HOST_COHERENT`, so the GPU reads

src/vulkan/buffer.zig:147

method

Buffer.upload

#
pub fn upload(self: *const Buffer, data: []const u8) void

Copy raw bytes into a previously mapped buffer (staging or host-visible storage).

Parameters
self
Buffer whose `mapped` pointer is non-null.
data
Bytes to copy from the CPU into the mapped range.
Notes

Asserts at debug time that the buffer is mapped and `data` fits within the allocation.

src/vulkan/buffer.zig:171

method

Buffer.deinit

#
pub fn deinit(self: *Buffer) void

Destroy the Vulkan buffer, free device memory, and unmap any host-mapped pointer.

Parameters
self
Buffer to tear down; the struct fields are set to `undefined` on return.

src/vulkan/buffer.zig:179

function

copyBuffer

#
pub fn copyBuffer( instance: *const Instance, cmd_pool: vk.c.VkCommandPool, src: *const Buffer, dst: *const Buffer, size: vk.c.VkDeviceSize, ) !void

Copy bytes between two Vulkan buffers with a temporary one-shot command buffer.

Parameters

instance
Active Vulkan instance and logical device.
cmd_pool
Command pool used to allocate the temporary command buffer.
src
Source buffer.
dst
Destination buffer.
size
Number of bytes to copy.

Returns

`error.AllocCmdBufFailed` or `error.QueueSubmitFailed` when command allocation or submission fails.

Notes

This helper waits for the compute queue to go idle before returning.

src/vulkan/buffer.zig:198