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
-3
View File
@@ -13,9 +13,6 @@ pub const Interface = base.Buffer;
interface: Interface,
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;
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,
.fillBuffer = fillBuffer,
.pipelineBarrier = pipelineBarrier,
.pushConstants = pushConstants,
.reset = reset,
.resetEvent = resetEvent,
.setEvent = setEvent,
@@ -150,7 +151,7 @@ pub fn beginRenderPass(interface: *Interface, render_pass: *base.RenderPass, fra
}
switch (desc.stencil_load_op) {
.clear => clear_mask = .{ .stencil_bit = true },
.clear => clear_mask.stencil_bit = true,
else => {},
}
@@ -813,6 +814,41 @@ pub fn pipelineBarrier(interface: *Interface, src_stage: vk.PipelineStageFlags,
_ = 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 {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
const allocator = self.command_allocator.allocator();
-3
View File
@@ -13,9 +13,6 @@ interface: Interface,
data: []u8,
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;
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_uniform_buffer_range = 16384,
.max_storage_buffer_range = 134217728,
.max_push_constants_size = 128,
.max_memory_allocation_count = lib.MAX_ALLOCATION_COUNT,
.max_push_constants_size = lib.PUSH_CONSTANT_SIZE,
.max_memory_allocation_count = std.math.maxInt(u32),
.max_sampler_allocation_count = 4096,
.buffer_image_granularity = 131072,
.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_heaps[0] = .{
.size = lib.PHYSICAL_DEVICE_HEAP_SIZE,
.size = std.process.totalSystemMemory() catch lib.PHYSICAL_DEVICE_FALLBACK_HEAP_SIZE,
.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_int_64 = .true,
.shader_int_16 = .true,
.texture_compression_etc2 = .true,
.texture_compression_bc = .true,
.texture_compression_etc2 = .false,
.texture_compression_bc = .false,
};
var queue_family_props = [_]vk.QueueFamilyProperties{
+2
View File
@@ -22,6 +22,7 @@ pub const COMPUTE_PIPELINE_STATE = 1;
pub const PipelineState = struct {
pipeline: ?*SoftPipeline,
sets: [base.VULKAN_MAX_DESCRIPTOR_SETS]?*SoftDescriptorSet,
push_constant_blob: [lib.PUSH_CONSTANT_SIZE]u8,
data: union {
compute: struct {},
graphics: struct {
@@ -43,6 +44,7 @@ pub fn init(self: *Self, device: *SoftDevice) void {
state.* = .{
.pipeline = null,
.sets = [_]?*SoftDescriptorSet{null} ** base.VULKAN_MAX_DESCRIPTOR_SETS,
.push_constant_blob = @splat(0),
.data = switch (i) {
GRAPHICS_PIPELINE_STATE => .{
.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)));
}
var calls: usize = 0;
fn interpolateVertexOutputs(
allocator: std.mem.Allocator,
v0: *const Renderer.Vertex,
@@ -43,6 +45,8 @@ fn interpolateVertexOutputs(
}
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;
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 rt = &shader.runtimes[data.batch_id];
try rt.populatePushConstants(data.renderer.state.push_constant_blob[0..]);
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_ATTRIBUTES = 32;
pub const PUSH_CONSTANT_SIZE = 256;
pub const MAX_IMAGE_LEVELS_1D = 15;
pub const MAX_IMAGE_LEVELS_2D = 15;
pub const MAX_IMAGE_LEVELS_3D = 12;
pub const MAX_IMAGE_LEVELS_CUBE = 15;
pub const MAX_IMAGE_ARRAY_LAYERS = 2048;
pub const PHYSICAL_DEVICE_HEAP_SIZE = 0x80000000; // 2 GiB
pub const MAX_MEMORY_ALLOCATION_SIZE = 0x80000000; // 2 GiB
pub const MAX_ALLOCATION_COUNT = 4096;
pub const PHYSICAL_DEVICE_FALLBACK_HEAP_SIZE = 0x10000000; // 256MB
pub const std_options = base.std_options;