implementing push constants

This commit is contained in:
2026-05-12 03:01:17 +02:00
parent 215486ac34
commit faae8e86e0
13 changed files with 81 additions and 46 deletions
+7 -7
View File
@@ -30,16 +30,16 @@
.hash = "cpuinfo-0.0.1-RLgIQYrTMgGqfQMOd1nAa2EuglXOh5gR9bNzwMzQTemt", .hash = "cpuinfo-0.0.1-RLgIQYrTMgGqfQMOd1nAa2EuglXOh5gR9bNzwMzQTemt",
.lazy = true, .lazy = true,
}, },
.SPIRV_Interpreter = .{
.url = "git+https://git.kbz8.me/kbz_8/SPIRV-Interpreter#9d20363ae852e1b400cb62508cb672bcfd5b3716",
.hash = "SPIRV_Interpreter-0.0.1-ajmpn6QuBQDDfo3uv6HauRc4DLNp2b0pZkfwyuzF-w9d",
.lazy = true,
},
//.SPIRV_Interpreter = .{ //.SPIRV_Interpreter = .{
// // For development // .url = "git+https://git.kbz8.me/kbz_8/SPIRV-Interpreter#ca33cfe3e997503208f031d270018c10d0611989",
// .path = "../SPIRV-Interpreter", // .hash = "SPIRV_Interpreter-0.0.1-ajmpnwBCBQBqUKcAUTwaaxbvYmX2s0KFzF_9Bc_ntqs4",
// .lazy = true, // .lazy = true,
//}, //},
.SPIRV_Interpreter = .{
// For development
.path = "git+https://git.kbz8.me/kbz_8/SPIRV-Interpreter#3139f3cfdd844818ab42ebce1d430a1f524025d5",
.lazy = true,
},
}, },
.paths = .{ .paths = .{
-3
View File
@@ -13,9 +13,6 @@ pub const Interface = base.Buffer;
interface: Interface, interface: Interface,
pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const vk.BufferCreateInfo) VkError!*Self { pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const vk.BufferCreateInfo) VkError!*Self {
if (info.size > lib.MAX_MEMORY_ALLOCATION_SIZE)
return VkError.OutOfDeviceMemory;
const self = allocator.create(Self) catch return VkError.OutOfHostMemory; const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
errdefer allocator.destroy(self); errdefer allocator.destroy(self);
+37 -1
View File
@@ -68,6 +68,7 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
.executeCommands = executeCommands, .executeCommands = executeCommands,
.fillBuffer = fillBuffer, .fillBuffer = fillBuffer,
.pipelineBarrier = pipelineBarrier, .pipelineBarrier = pipelineBarrier,
.pushConstants = pushConstants,
.reset = reset, .reset = reset,
.resetEvent = resetEvent, .resetEvent = resetEvent,
.setEvent = setEvent, .setEvent = setEvent,
@@ -150,7 +151,7 @@ pub fn beginRenderPass(interface: *Interface, render_pass: *base.RenderPass, fra
} }
switch (desc.stencil_load_op) { switch (desc.stencil_load_op) {
.clear => clear_mask = .{ .stencil_bit = true }, .clear => clear_mask.stencil_bit = true,
else => {}, else => {},
} }
@@ -813,6 +814,41 @@ pub fn pipelineBarrier(interface: *Interface, src_stage: vk.PipelineStageFlags,
_ = image_barriers; _ = image_barriers;
} }
pub fn pushConstants(interface: *Interface, stages: vk.ShaderStageFlags, offset: u32, blob: []const u8) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
const allocator = self.command_allocator.allocator();
const CommandImpl = struct {
const Impl = @This();
stages: vk.ShaderStageFlags,
offset: u32,
blob: []const u8,
pub fn execute(context: *anyopaque, device: *ExecutionDevice) VkError!void {
const impl: *Impl = @ptrCast(@alignCast(context));
const size = @min(lib.PUSH_CONSTANT_SIZE - impl.offset, impl.blob.len);
// TODO: pipeline layout offset
if (impl.stages.vertex_bit or impl.stages.fragment_bit) {
@memcpy(device.pipeline_states[ExecutionDevice.GRAPHICS_PIPELINE_STATE].push_constant_blob[impl.offset..size], impl.blob[0..size]);
}
if (impl.stages.compute_bit) {
@memcpy(device.pipeline_states[ExecutionDevice.COMPUTE_PIPELINE_STATE].push_constant_blob[impl.offset..size], impl.blob[0..size]);
}
}
};
const cmd = allocator.create(CommandImpl) catch return VkError.OutOfHostMemory;
errdefer allocator.destroy(cmd);
cmd.* = .{
.stages = stages,
.offset = offset,
.blob = allocator.dupe(u8, blob) catch return VkError.OutOfHostMemory, // Will be freed on cmdbuf reset or destroy
};
self.commands.append(allocator, .{ .ptr = cmd, .vtable = &.{ .execute = CommandImpl.execute } }) catch return VkError.OutOfHostMemory;
}
pub fn resetEvent(interface: *Interface, event: *base.Event, stage: vk.PipelineStageFlags) VkError!void { pub fn resetEvent(interface: *Interface, event: *base.Event, stage: vk.PipelineStageFlags) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
const allocator = self.command_allocator.allocator(); const allocator = self.command_allocator.allocator();
-3
View File
@@ -13,9 +13,6 @@ interface: Interface,
data: []u8, data: []u8,
pub fn create(device: *SoftDevice, allocator: std.mem.Allocator, size: vk.DeviceSize, memory_type_index: u32) VkError!*Self { pub fn create(device: *SoftDevice, allocator: std.mem.Allocator, size: vk.DeviceSize, memory_type_index: u32) VkError!*Self {
if (size > lib.MAX_MEMORY_ALLOCATION_SIZE)
return VkError.OutOfDeviceMemory;
const self = allocator.create(Self) catch return VkError.OutOfHostMemory; const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
errdefer allocator.destroy(self); errdefer allocator.destroy(self);
+5 -5
View File
@@ -68,8 +68,8 @@ pub fn create(allocator: std.mem.Allocator, instance: *base.Instance) VkError!*S
.max_texel_buffer_elements = 65536, .max_texel_buffer_elements = 65536,
.max_uniform_buffer_range = 16384, .max_uniform_buffer_range = 16384,
.max_storage_buffer_range = 134217728, .max_storage_buffer_range = 134217728,
.max_push_constants_size = 128, .max_push_constants_size = lib.PUSH_CONSTANT_SIZE,
.max_memory_allocation_count = lib.MAX_ALLOCATION_COUNT, .max_memory_allocation_count = std.math.maxInt(u32),
.max_sampler_allocation_count = 4096, .max_sampler_allocation_count = 4096,
.buffer_image_granularity = 131072, .buffer_image_granularity = 131072,
.sparse_address_space_size = 0, .sparse_address_space_size = 0,
@@ -180,7 +180,7 @@ pub fn create(allocator: std.mem.Allocator, instance: *base.Instance) VkError!*S
}; };
interface.mem_props.memory_heap_count = 1; interface.mem_props.memory_heap_count = 1;
interface.mem_props.memory_heaps[0] = .{ interface.mem_props.memory_heaps[0] = .{
.size = lib.PHYSICAL_DEVICE_HEAP_SIZE, .size = std.process.totalSystemMemory() catch lib.PHYSICAL_DEVICE_FALLBACK_HEAP_SIZE,
.flags = .{ .device_local_bit = true }, .flags = .{ .device_local_bit = true },
}; };
@@ -189,8 +189,8 @@ pub fn create(allocator: std.mem.Allocator, instance: *base.Instance) VkError!*S
.shader_float_64 = .true, .shader_float_64 = .true,
.shader_int_64 = .true, .shader_int_64 = .true,
.shader_int_16 = .true, .shader_int_16 = .true,
.texture_compression_etc2 = .true, .texture_compression_etc2 = .false,
.texture_compression_bc = .true, .texture_compression_bc = .false,
}; };
var queue_family_props = [_]vk.QueueFamilyProperties{ var queue_family_props = [_]vk.QueueFamilyProperties{
+2
View File
@@ -22,6 +22,7 @@ pub const COMPUTE_PIPELINE_STATE = 1;
pub const PipelineState = struct { pub const PipelineState = struct {
pipeline: ?*SoftPipeline, pipeline: ?*SoftPipeline,
sets: [base.VULKAN_MAX_DESCRIPTOR_SETS]?*SoftDescriptorSet, sets: [base.VULKAN_MAX_DESCRIPTOR_SETS]?*SoftDescriptorSet,
push_constant_blob: [lib.PUSH_CONSTANT_SIZE]u8,
data: union { data: union {
compute: struct {}, compute: struct {},
graphics: struct { graphics: struct {
@@ -43,6 +44,7 @@ pub fn init(self: *Self, device: *SoftDevice) void {
state.* = .{ state.* = .{
.pipeline = null, .pipeline = null,
.sets = [_]?*SoftDescriptorSet{null} ** base.VULKAN_MAX_DESCRIPTOR_SETS, .sets = [_]?*SoftDescriptorSet{null} ** base.VULKAN_MAX_DESCRIPTOR_SETS,
.push_constant_blob = @splat(0),
.data = switch (i) { .data = switch (i) {
GRAPHICS_PIPELINE_STATE => .{ GRAPHICS_PIPELINE_STATE => .{
.graphics = .{ .graphics = .{
+4
View File
@@ -21,6 +21,8 @@ fn interpolateF32x4(value0: F32x4, value1: F32x4, value2: F32x4, b0: f32, b1: f3
return (value0 * @as(F32x4, @splat(b0))) + (value1 * @as(F32x4, @splat(b1))) + (value2 * @as(F32x4, @splat(b2))); return (value0 * @as(F32x4, @splat(b0))) + (value1 * @as(F32x4, @splat(b1))) + (value2 * @as(F32x4, @splat(b2)));
} }
var calls: usize = 0;
fn interpolateVertexOutputs( fn interpolateVertexOutputs(
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
v0: *const Renderer.Vertex, v0: *const Renderer.Vertex,
@@ -43,6 +45,8 @@ fn interpolateVertexOutputs(
} }
const len = @min(out0.blob.len, out1.blob.len, out2.blob.len); const len = @min(out0.blob.len, out1.blob.len, out2.blob.len);
calls += 1;
std.debug.print("test {d}\n", .{calls});
const input = allocator.alloc(u8, len) catch return VkError.OutOfDeviceMemory; const input = allocator.alloc(u8, len) catch return VkError.OutOfDeviceMemory;
var byte_index: usize = 0; var byte_index: usize = 0;
+1
View File
@@ -39,6 +39,7 @@ inline fn run(data: RunData) !void {
const shader = data.pipeline.stages.getPtrAssertContains(.vertex); const shader = data.pipeline.stages.getPtrAssertContains(.vertex);
const rt = &shader.runtimes[data.batch_id]; const rt = &shader.runtimes[data.batch_id];
try rt.populatePushConstants(data.renderer.state.push_constant_blob[0..]);
const entry = try rt.getEntryPointByName(shader.entry); const entry = try rt.getEntryPointByName(shader.entry);
+3 -3
View File
@@ -60,15 +60,15 @@ pub const MIN_STORAGE_BUFFER_ALIGNMENT = 256;
pub const MAX_VERTEX_INPUT_BINDINGS = 16; pub const MAX_VERTEX_INPUT_BINDINGS = 16;
pub const MAX_VERTEX_INPUT_ATTRIBUTES = 32; pub const MAX_VERTEX_INPUT_ATTRIBUTES = 32;
pub const PUSH_CONSTANT_SIZE = 256;
pub const MAX_IMAGE_LEVELS_1D = 15; pub const MAX_IMAGE_LEVELS_1D = 15;
pub const MAX_IMAGE_LEVELS_2D = 15; pub const MAX_IMAGE_LEVELS_2D = 15;
pub const MAX_IMAGE_LEVELS_3D = 12; pub const MAX_IMAGE_LEVELS_3D = 12;
pub const MAX_IMAGE_LEVELS_CUBE = 15; pub const MAX_IMAGE_LEVELS_CUBE = 15;
pub const MAX_IMAGE_ARRAY_LAYERS = 2048; pub const MAX_IMAGE_ARRAY_LAYERS = 2048;
pub const PHYSICAL_DEVICE_HEAP_SIZE = 0x80000000; // 2 GiB pub const PHYSICAL_DEVICE_FALLBACK_HEAP_SIZE = 0x10000000; // 256MB
pub const MAX_MEMORY_ALLOCATION_SIZE = 0x80000000; // 2 GiB
pub const MAX_ALLOCATION_COUNT = 4096;
pub const std_options = base.std_options; pub const std_options = base.std_options;
+5
View File
@@ -62,6 +62,7 @@ pub const DispatchTable = struct {
executeCommands: *const fn (*Self, *Self) VkError!void, executeCommands: *const fn (*Self, *Self) VkError!void,
fillBuffer: *const fn (*Self, *Buffer, vk.DeviceSize, vk.DeviceSize, u32) VkError!void, fillBuffer: *const fn (*Self, *Buffer, vk.DeviceSize, vk.DeviceSize, u32) VkError!void,
pipelineBarrier: *const fn (*Self, vk.PipelineStageFlags, vk.PipelineStageFlags, vk.DependencyFlags, []const vk.MemoryBarrier, []const vk.BufferMemoryBarrier, []const vk.ImageMemoryBarrier) VkError!void, pipelineBarrier: *const fn (*Self, vk.PipelineStageFlags, vk.PipelineStageFlags, vk.DependencyFlags, []const vk.MemoryBarrier, []const vk.BufferMemoryBarrier, []const vk.ImageMemoryBarrier) VkError!void,
pushConstants: *const fn (*Self, vk.ShaderStageFlags, u32, []const u8) VkError!void,
reset: *const fn (*Self, vk.CommandBufferResetFlags) VkError!void, reset: *const fn (*Self, vk.CommandBufferResetFlags) VkError!void,
resetEvent: *const fn (*Self, *Event, vk.PipelineStageFlags) VkError!void, resetEvent: *const fn (*Self, *Event, vk.PipelineStageFlags) VkError!void,
setEvent: *const fn (*Self, *Event, vk.PipelineStageFlags) VkError!void, setEvent: *const fn (*Self, *Event, vk.PipelineStageFlags) VkError!void,
@@ -253,6 +254,10 @@ pub inline fn pipelineBarrier(
try self.dispatch_table.pipelineBarrier(self, src_stage, dst_stage, dependency, memory_barriers, buffer_barriers, image_barriers); try self.dispatch_table.pipelineBarrier(self, src_stage, dst_stage, dependency, memory_barriers, buffer_barriers, image_barriers);
} }
pub inline fn pushConstants(self: *Self, stages: vk.ShaderStageFlags, offset: u32, blob: []const u8) VkError!void {
try self.dispatch_table.pushConstants(self, stages, offset, blob);
}
pub inline fn resetEvent(self: *Self, event: *Event, stage: vk.PipelineStageFlags) VkError!void { pub inline fn resetEvent(self: *Self, event: *Event, stage: vk.PipelineStageFlags) VkError!void {
try self.dispatch_table.resetEvent(self, event, stage); try self.dispatch_table.resetEvent(self, event, stage);
} }
+14 -2
View File
@@ -1,12 +1,13 @@
const std = @import("std"); const std = @import("std");
const vk = @import("vulkan"); const vk = @import("vulkan");
const NonDispatchable = @import("NonDispatchable.zig"); const NonDispatchable = @import("NonDispatchable.zig").NonDispatchable;
const VkError = @import("error_set.zig").VkError; const VkError = @import("error_set.zig").VkError;
const Device = @import("Device.zig"); const Device = @import("Device.zig");
const PipelineCache = @import("PipelineCache.zig"); const PipelineCache = @import("PipelineCache.zig");
const PipelineLayout = @import("PipelineLayout.zig");
const Self = @This(); const Self = @This();
pub const ObjectType: vk.ObjectType = .pipeline; pub const ObjectType: vk.ObjectType = .pipeline;
@@ -28,6 +29,7 @@ owner: *Device,
vtable: *const VTable, vtable: *const VTable,
bind_point: vk.PipelineBindPoint, bind_point: vk.PipelineBindPoint,
stages: vk.ShaderStageFlags, stages: vk.ShaderStageFlags,
layout: *PipelineLayout,
mode: union(enum) { mode: union(enum) {
compute: struct {}, compute: struct {},
graphics: struct { graphics: struct {
@@ -55,14 +57,18 @@ pub const VTable = struct {
}; };
pub fn initCompute(device: *Device, allocator: std.mem.Allocator, cache: ?*PipelineCache, info: *const vk.ComputePipelineCreateInfo) VkError!Self { pub fn initCompute(device: *Device, allocator: std.mem.Allocator, cache: ?*PipelineCache, info: *const vk.ComputePipelineCreateInfo) VkError!Self {
_ = allocator;
_ = cache; _ = cache;
const layout = try NonDispatchable(PipelineLayout).fromHandleObject(info.layout);
layout.ref();
errdefer layout.unref(allocator);
return .{ return .{
.owner = device, .owner = device,
.vtable = undefined, .vtable = undefined,
.bind_point = .compute, .bind_point = .compute,
.stages = info.stage.stage, .stages = info.stage.stage,
.layout = layout,
.mode = .{ .compute = .{} }, .mode = .{ .compute = .{} },
}; };
} }
@@ -70,6 +76,10 @@ pub fn initCompute(device: *Device, allocator: std.mem.Allocator, cache: ?*Pipel
pub fn initGraphics(device: *Device, allocator: std.mem.Allocator, cache: ?*PipelineCache, info: *const vk.GraphicsPipelineCreateInfo) VkError!Self { pub fn initGraphics(device: *Device, allocator: std.mem.Allocator, cache: ?*PipelineCache, info: *const vk.GraphicsPipelineCreateInfo) VkError!Self {
_ = cache; _ = cache;
const layout = try NonDispatchable(PipelineLayout).fromHandleObject(info.layout);
layout.ref();
errdefer layout.unref(allocator);
var stages: vk.ShaderStageFlags = .{}; var stages: vk.ShaderStageFlags = .{};
if (info.p_stages) |p_stages| { if (info.p_stages) |p_stages| {
for (p_stages[0..info.stage_count]) |stage| { for (p_stages[0..info.stage_count]) |stage| {
@@ -82,6 +92,7 @@ pub fn initGraphics(device: *Device, allocator: std.mem.Allocator, cache: ?*Pipe
.vtable = undefined, .vtable = undefined,
.bind_point = .graphics, .bind_point = .graphics,
.stages = stages, .stages = stages,
.layout = layout,
.mode = .{ .mode = .{
.graphics = .{ .graphics = .{
.input_assembly = .{ .input_assembly = .{
@@ -172,5 +183,6 @@ pub inline fn destroy(self: *Self, allocator: std.mem.Allocator) void {
} }
}, },
} }
self.layout.unref(allocator);
self.vtable.destroy(self, allocator); self.vtable.destroy(self, allocator);
} }
-13
View File
@@ -23,19 +23,6 @@ dynamic_descriptor_offsets: [lib.VULKAN_MAX_DESCRIPTOR_SETS]usize,
push_ranges_count: usize, push_ranges_count: usize,
push_ranges: [lib.VULKAN_MAX_PUSH_CONSTANT_RANGES]vk.PushConstantRange, push_ranges: [lib.VULKAN_MAX_PUSH_CONSTANT_RANGES]vk.PushConstantRange,
/// Mesa's common Vulkan runtime states:
///
/// It's often necessary to store a pointer to the descriptor set layout in
/// the descriptor so that any entrypoint which has access to a descriptor
/// set also has the layout. While layouts are often passed into various
/// entrypoints, they're notably missing from vkUpdateDescriptorSets(). In
/// order to implement descriptor writes, you either need to stash a pointer
/// to the descriptor set layout in the descriptor set or you need to copy
/// all of the relevant information. Storing a pointer is a lot cheaper.
///
/// Because descriptor set layout lifetimes and descriptor set lifetimes are
/// not guaranteed to coincide, we have to reference count if we're going to
/// do this.
ref_count: std.atomic.Value(usize), ref_count: std.atomic.Value(usize),
vtable: *const VTable, vtable: *const VTable,
+3 -9
View File
@@ -1957,20 +1957,14 @@ pub export fn strollCmdPipelineBarrier(
) catch |err| return errorLogger(err); ) catch |err| return errorLogger(err);
} }
pub export fn strollCmdPushConstants(p_cmd: vk.CommandBuffer, layout: vk.PipelineLayout, flags: vk.ShaderStageFlags, offset: u32, size: u32, values: *const anyopaque) callconv(vk.vulkan_call_conv) void { pub export fn strollCmdPushConstants(p_cmd: vk.CommandBuffer, layout: vk.PipelineLayout, flags: vk.ShaderStageFlags, offset: u32, size: u32, data: [*]const u8) callconv(vk.vulkan_call_conv) void {
entryPointBeginLogTrace(.vkCmdPushConstants); entryPointBeginLogTrace(.vkCmdPushConstants);
defer entryPointEndLogTrace(); defer entryPointEndLogTrace();
const cmd = Dispatchable(CommandBuffer).fromHandleObject(p_cmd) catch |err| return errorLogger(err); const cmd = Dispatchable(CommandBuffer).fromHandleObject(p_cmd) catch |err| return errorLogger(err);
cmd.pushConstants(flags, offset, data[0..size]) catch |err| return errorLogger(err);
notImplementedWarning(); _ = layout; // Pipelines embed their layout which is more trustworthy
_ = cmd;
_ = layout;
_ = flags;
_ = offset;
_ = size;
_ = values;
} }
pub export fn strollCmdResetQueryPool(p_cmd: vk.CommandBuffer, p_pool: vk.QueryPool, first: u32, count: u32) callconv(vk.vulkan_call_conv) void { pub export fn strollCmdResetQueryPool(p_cmd: vk.CommandBuffer, p_pool: vk.QueryPool, first: u32, count: u32) callconv(vk.vulkan_call_conv) void {