implementing proper pipeline cache
Test / build_and_test (push) Successful in 2m3s
Build / build (push) Successful in 2m21s

This commit is contained in:
2026-06-13 23:08:12 +02:00
parent a8ba6fd14f
commit 57b5533195
13 changed files with 823 additions and 218 deletions
+74 -24
View File
@@ -12,7 +12,7 @@ const Self = @This();
pub const ObjectType: vk.ObjectType = .pipeline_cache;
owner: *Device,
data_available: bool,
data: []u8,
vtable: *const VTable,
@@ -24,55 +24,105 @@ pub const Header = vk.PipelineCacheHeaderVersionOne;
pub fn init(device: *Device, allocator: std.mem.Allocator, info: *const vk.PipelineCacheCreateInfo) VkError!Self {
_ = allocator;
const has_initial_data = info.initial_data_size != 0 or info.p_initial_data != null;
if (info.initial_data_size != 0 and info.p_initial_data == null) {
return VkError.ValidationFailed;
}
const initial_data = if (info.p_initial_data) |ptr|
@as([*]const u8, @ptrCast(ptr))[0..info.initial_data_size]
else
&.{};
const data_allocator = device.host_allocator.allocator();
const data = if (isCompatibleInitialData(device, initial_data))
data_allocator.dupe(u8, initial_data) catch return VkError.OutOfHostMemory
else
createEmptyData(device, data_allocator) catch return VkError.OutOfHostMemory;
return .{
.owner = device,
.data_available = !has_initial_data or info.initial_data_size >= dataSize(),
.data = data,
.vtable = undefined,
};
}
pub inline fn destroy(self: *Self, allocator: std.mem.Allocator) void {
pub fn destroy(self: *Self, allocator: std.mem.Allocator) void {
self.owner.host_allocator.allocator().free(self.data);
self.vtable.destroy(self, allocator);
}
pub fn merge(self: *Self) VkError!void {
_ = self;
pub fn merge(self: *Self, source: *const Self) VkError!void {
if (source.data.len <= @sizeOf(Header)) {
return;
}
if (!isCompatibleInitialData(self.owner, source.data)) {
return;
}
const old_len = self.data.len;
const source_payload = source.data[@sizeOf(Header)..];
self.data = self.owner.host_allocator.allocator().realloc(self.data, old_len + source_payload.len) catch return VkError.OutOfHostMemory;
@memcpy(self.data[old_len..], source_payload);
}
pub fn appendPayload(self: *Self, payload: []const u8) VkError!void {
if (payload.len == 0) {
return;
}
const old_len = self.data.len;
self.data = self.owner.host_allocator.allocator().realloc(self.data, old_len + payload.len) catch return VkError.OutOfHostMemory;
@memcpy(self.data[old_len..], payload);
}
pub fn getData(self: *Self, data: ?[]u8) vk.Result {
if (!self.data_available) {
return .success;
}
const cache_header = self.header();
const bytes = std.mem.asBytes(&cache_header);
if (data) |dst| {
if (dst.len < bytes.len) {
const written = @min(dst.len, self.data.len);
@memcpy(dst[0..written], self.data[0..written]);
if (written < self.data.len) {
return .incomplete;
}
@memcpy(dst[0..bytes.len], bytes);
}
return .success;
}
pub inline fn dataSize() usize {
return @sizeOf(Header);
}
pub inline fn availableDataSize(self: *const Self) usize {
return if (self.data_available) dataSize() else 0;
return self.data.len;
}
fn header(self: *const Self) Header {
fn makeHeader(device: *const Device) Header {
return .{
.header_size = @sizeOf(Header),
.header_version = .one,
.vendor_id = @intCast(root.VULKAN_VENDOR_ID),
.device_id = self.owner.physical_device.props.device_id,
.pipeline_cache_uuid = self.owner.physical_device.props.pipeline_cache_uuid,
.device_id = device.physical_device.props.device_id,
.pipeline_cache_uuid = device.physical_device.props.pipeline_cache_uuid,
};
}
fn createEmptyData(device: *const Device, allocator: std.mem.Allocator) VkError![]u8 {
const cache_header = makeHeader(device);
return allocator.dupe(u8, std.mem.asBytes(&cache_header)) catch VkError.OutOfHostMemory;
}
fn isCompatibleInitialData(device: *const Device, initial_data: []const u8) bool {
if (initial_data.len == 0) {
return false;
}
if (initial_data.len < @sizeOf(Header)) {
return false;
}
const cache_header = readHeader(initial_data);
const expected = makeHeader(device);
return cache_header.header_size == @sizeOf(Header) and
cache_header.header_version == .one and
cache_header.vendor_id == expected.vendor_id and
cache_header.device_id == expected.device_id and
std.mem.eql(u8, &cache_header.pipeline_cache_uuid, &expected.pipeline_cache_uuid);
}
inline fn readHeader(bytes: []const u8) Header {
return std.mem.bytesToValue(Header, bytes);
}