From 354c9891d636bc18702c52876607a9427494b587 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Thu, 30 Apr 2026 02:16:49 +0200 Subject: [PATCH] adding draw indexed --- src/soft/SoftCommandBuffer.zig | 39 ++++- src/soft/device/Device.zig | 2 + src/soft/device/Renderer.zig | 224 +++++++++++++++++--------- src/soft/device/vertex_dispatcher.zig | 14 +- src/vulkan/CommandBuffer.zig | 5 + src/vulkan/lib_vulkan.zig | 10 +- 6 files changed, 207 insertions(+), 87 deletions(-) diff --git a/src/soft/SoftCommandBuffer.zig b/src/soft/SoftCommandBuffer.zig index 71840b6..19186f8 100644 --- a/src/soft/SoftCommandBuffer.zig +++ b/src/soft/SoftCommandBuffer.zig @@ -60,6 +60,7 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v .dispatch = dispatch, .dispatchIndirect = dispatchIndirect, .draw = draw, + .drawIndexed = drawIndexed, .end = end, .endRenderPass = endRenderPass, .executeCommands = executeCommands, @@ -240,8 +241,11 @@ pub fn bindIndexBuffer(interface: *Interface, buffer: *base.Buffer, offset: usiz pub fn execute(context: *anyopaque, device: *ExecutionDevice) VkError!void { const impl: *Impl = @ptrCast(@alignCast(context)); - _ = impl; - _ = device; + device.pipeline_states[ExecutionDevice.GRAPHICS_PIPELINE_STATE].data.graphics.index_buffer = .{ + .buffer = impl.buffer, + .offset = impl.offset, + .index_type = impl.index_type, + }; } }; @@ -557,6 +561,37 @@ pub fn dispatchIndirect(interface: *Interface, buffer: *base.Buffer, offset: vk. self.commands.append(allocator, .{ .ptr = cmd, .vtable = &.{ .execute = CommandImpl.execute } }) catch return VkError.OutOfHostMemory; } +pub fn drawIndexed(interface: *Interface, index_count: usize, instance_count: usize, first_index: usize, vertex_offset: usize, first_instance: usize) VkError!void { + const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); + const allocator = self.command_allocator.allocator(); + + const CommandImpl = struct { + const Impl = @This(); + + index_count: usize, + first_index: usize, + instance_count: usize, + first_instance: usize, + vertex_offset: usize, + + pub fn execute(context: *anyopaque, device: *ExecutionDevice) VkError!void { + const impl: *Impl = @ptrCast(@alignCast(context)); + try device.renderer.drawIndexed(impl.index_count, impl.instance_count, impl.first_index, impl.first_instance, impl.vertex_offset); + } + }; + + const cmd = allocator.create(CommandImpl) catch return VkError.OutOfHostMemory; + errdefer allocator.destroy(cmd); + cmd.* = .{ + .index_count = index_count, + .first_index = first_index, + .instance_count = instance_count, + .first_instance = first_instance, + .vertex_offset = vertex_offset, + }; + self.commands.append(allocator, .{ .ptr = cmd, .vtable = &.{ .execute = CommandImpl.execute } }) catch return VkError.OutOfHostMemory; +} + pub fn draw(interface: *Interface, vertex_count: usize, instance_count: usize, first_vertex: usize, first_instance: usize) VkError!void { const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); const allocator = self.command_allocator.allocator(); diff --git a/src/soft/device/Device.zig b/src/soft/device/Device.zig index 8d9fdf1..4a341f1 100644 --- a/src/soft/device/Device.zig +++ b/src/soft/device/Device.zig @@ -25,6 +25,7 @@ pub const PipelineState = struct { data: union { compute: struct {}, graphics: struct { + index_buffer: Renderer.IndexBuffer, vertex_buffers: [lib.MAX_VERTEX_INPUT_BINDINGS]Renderer.VertexBuffer, }, }, @@ -45,6 +46,7 @@ pub fn init(self: *Self, device: *SoftDevice) void { .data = switch (i) { GRAPHICS_PIPELINE_STATE => .{ .graphics = .{ + .index_buffer = undefined, .vertex_buffers = undefined, }, }, diff --git a/src/soft/device/Renderer.zig b/src/soft/device/Renderer.zig index 92372cd..069c4a0 100644 --- a/src/soft/device/Renderer.zig +++ b/src/soft/device/Renderer.zig @@ -32,6 +32,12 @@ pub const VertexBuffer = struct { size: usize, }; +pub const IndexBuffer = struct { + buffer: *const SoftBuffer, + offset: usize, + index_type: vk.IndexType, +}; + pub const DynamicState = struct { viewports: ?[]const vk.Viewport, scissor: ?[]vk.Rect2D, @@ -55,6 +61,19 @@ pub const Fragment = struct { pub const DrawCall = struct { vertices: []Vertex, fragments: []Fragment, + + pub fn init(allocator: std.mem.Allocator, vertex_count: usize, instance_count: usize) VkError!@This() { + const self: @This() = .{ + .vertices = allocator.alloc(Vertex, vertex_count * instance_count) catch return VkError.OutOfDeviceMemory, + .fragments = undefined, + }; + + for (self.vertices) |*vertex| { + @memset(vertex.outputs[0..], null); + } + + return self; + } }; device: *SoftDevice, @@ -81,13 +100,12 @@ pub fn init(device: *SoftDevice, state: *PipelineState) Self { pub fn draw(self: *Self, vertex_count: usize, instance_count: usize, first_vertex: usize, first_instance: usize) VkError!void { const io = self.device.interface.io(); - const render_target_view: *base.ImageView = (self.framebuffer orelse return).interface.attachments[0]; - const render_target: *SoftImage = @alignCast(@fieldParentPtr("interface", render_target_view.image)); - var arena: std.heap.ArenaAllocator = .init(self.device.device_allocator.allocator()); defer arena.deinit(); const allocator = arena.allocator(); + var draw_call = try DrawCall.init(allocator, vertex_count, instance_count); + const timer = std.Io.Timestamp.now(io, .real); defer if (comptime base.config.logs != .none) { const duration = timer.untilNow(io, .real); @@ -95,58 +113,48 @@ pub fn draw(self: *Self, vertex_count: usize, instance_count: usize, first_verte std.log.scoped(.SoftwareRenderer).debug("Drawcall stats:\n> Took {d}us\n> Allocated {d} KB", .{ ms, @divTrunc(arena.queryCapacity(), 1000) }); }; - var draw_call: DrawCall = .{ - .vertices = allocator.alloc(Vertex, vertex_count * instance_count) catch return VkError.OutOfDeviceMemory, - .fragments = undefined, - }; - - for (draw_call.vertices) |*vertex| { - @memset(vertex.outputs[0..], null); - } - - self.vertexShaderStage(allocator, &draw_call, vertex_count, instance_count) catch |err| { + self.vertexShaderStage(allocator, &draw_call, vertex_count, instance_count, first_vertex, first_instance, null) catch |err| { std.log.scoped(.@"Vertex stage").err("catched a '{s}'", .{@errorName(err)}); if (@errorReturnTrace()) |trace| { std.debug.dumpErrorReturnTrace(trace); } }; - try self.primitiveAssemblyStage(&draw_call); - try self.rasterizationStage(allocator, &draw_call); + try self.postVertexDraw(allocator, &draw_call); +} - self.fragmentShaderStage(&draw_call) catch |err| { - std.log.scoped(.@"Fragment stage").err("catched a '{s}'", .{@errorName(err)}); +pub fn drawIndexed(self: *Self, index_count: usize, instance_count: usize, first_index: usize, first_instance: usize, vertex_offset: usize) VkError!void { + const io = self.device.interface.io(); + + var arena: std.heap.ArenaAllocator = .init(self.device.device_allocator.allocator()); + defer arena.deinit(); + const allocator = arena.allocator(); + + var draw_call = try DrawCall.init(allocator, index_count, instance_count); + const indices = try self.readIndexBuffer(allocator, index_count, first_index, vertex_offset); + + const timer = std.Io.Timestamp.now(io, .real); + defer if (comptime base.config.logs != .none) { + const duration = timer.untilNow(io, .real); + const ms = duration.toMicroseconds(); + std.log.scoped(.SoftwareRenderer).debug("Drawcall indexed stats:\n> Took {d}us\n> Allocated {d} KB", .{ ms, @divTrunc(arena.queryCapacity(), 1000) }); + }; + + self.vertexShaderStage(allocator, &draw_call, index_count, instance_count, 0, first_instance, indices) catch |err| { + std.log.scoped(.@"Vertex stage").err("catched a '{s}'", .{@errorName(err)}); if (@errorReturnTrace()) |trace| { std.debug.dumpErrorReturnTrace(trace); } }; - for (draw_call.fragments) |fragment| { - try render_target.writeFloat4( - .{ - .x = @intFromFloat(fragment.position[0]), - .y = @intFromFloat(fragment.position[1]), - .z = @intFromFloat(fragment.position[2]), - }, - .{ - .aspect_mask = render_target_view.subresource_range.aspect_mask, - .mip_level = render_target_view.subresource_range.base_mip_level, - .array_layer = render_target_view.subresource_range.base_array_layer, - }, - render_target_view.format, - fragment.color, - ); - } - - _ = first_vertex; - _ = first_instance; + try self.postVertexDraw(allocator, &draw_call); } pub fn deinit(self: *Self) void { _ = self; } -fn vertexShaderStage(self: *Self, allocator: std.mem.Allocator, draw_call: *DrawCall, vertex_count: usize, instance_count: usize) !void { +fn vertexShaderStage(self: *Self, allocator: std.mem.Allocator, draw_call: *DrawCall, vertex_count: usize, instance_count: usize, first_vertex: usize, first_instance: usize, indices: ?[]const u32) !void { const pipeline = self.state.pipeline orelse return; const batch_size = (pipeline.stages.getPtr(.vertex) orelse return).runtimes.len; @@ -160,6 +168,9 @@ fn vertexShaderStage(self: *Self, allocator: std.mem.Allocator, draw_call: *Draw .batch_id = batch_id, .batch_size = batch_size, .vertex_count = vertex_count, + .first_vertex = first_vertex, + .first_instance = first_instance, + .indices = indices, .instance_index = instance_index, .draw_call = draw_call, }; @@ -170,6 +181,38 @@ fn vertexShaderStage(self: *Self, allocator: std.mem.Allocator, draw_call: *Draw wg.await(self.device.interface.io()) catch return VkError.DeviceLost; } +fn postVertexDraw(self: *Self, allocator: std.mem.Allocator, draw_call: *DrawCall) VkError!void { + const render_target_view: *base.ImageView = (self.framebuffer orelse return).interface.attachments[0]; + const render_target: *SoftImage = @alignCast(@fieldParentPtr("interface", render_target_view.image)); + + try self.primitiveAssemblyStage(draw_call); + try self.rasterizationStage(allocator, draw_call); + + self.fragmentShaderStage(draw_call) catch |err| { + std.log.scoped(.@"Fragment stage").err("catched a '{s}'", .{@errorName(err)}); + if (@errorReturnTrace()) |trace| { + std.debug.dumpErrorReturnTrace(trace); + } + }; + + for (draw_call.fragments) |fragment| { + render_target.writeFloat4( + .{ + .x = @intFromFloat(fragment.position[0]), + .y = @intFromFloat(fragment.position[1]), + .z = 0, + }, + .{ + .aspect_mask = render_target_view.subresource_range.aspect_mask, + .mip_level = render_target_view.subresource_range.base_mip_level, + .array_layer = render_target_view.subresource_range.base_array_layer, + }, + render_target_view.format, + fragment.color, + ) catch {}; + } +} + fn primitiveAssemblyStage(self: *Self, draw_call: *DrawCall) VkError!void { const viewport = blk: { const pipeline_data = &(self.state.pipeline orelse return VkError.InvalidPipelineDrv).interface.mode.graphics; @@ -251,41 +294,6 @@ fn rasterizationStage(self: *Self, allocator: std.mem.Allocator, draw_call: *Dra draw_call.fragments = fragments.toOwnedSlice(allocator) catch return VkError.OutOfDeviceMemory; } -fn triangleArea2(v0: *const Vertex, v1: *const Vertex, v2: *const Vertex) f32 { - const x0 = v0.position[0]; - const y0 = v0.position[1]; - const x1 = v1.position[0]; - const y1 = v1.position[1]; - const x2 = v2.position[0]; - const y2 = v2.position[1]; - - return ((x1 - x0) * (y2 - y0)) - ((y1 - y0) * (x2 - x0)); -} - -fn triangleIsCulled(self: *Self, v0: *const Vertex, v1: *const Vertex, v2: *const Vertex) VkError!bool { - const pipeline_data = (self.state.pipeline orelse return VkError.InvalidHandleDrv).interface.mode.graphics; - const rasterization = pipeline_data.rasterization; - const cull_mode = rasterization.cull_mode; - - if (!cull_mode.front_bit and !cull_mode.back_bit) - return false; - - if (cull_mode.front_bit and cull_mode.back_bit) - return true; - - const area = triangleArea2(v0, v1, v2); - if (area == 0.0) - return true; - - const front_face = switch (rasterization.front_face) { - .counter_clockwise => area < 0.0, - .clockwise => area > 0.0, - else => return false, - }; - - return (cull_mode.front_bit and front_face) or (cull_mode.back_bit and !front_face); -} - fn rasterizeTriangle( self: *Self, allocator: std.mem.Allocator, @@ -333,3 +341,75 @@ fn fragmentShaderStage(self: *Self, draw_call: *DrawCall) !void { } wg.await(self.device.interface.io()) catch return VkError.DeviceLost; } + +fn readIndexBuffer(self: *Self, allocator: std.mem.Allocator, index_count: usize, first_index: usize, vertex_offset: usize) VkError![]u32 { + const index_buffer = self.state.data.graphics.index_buffer; + const buffer = index_buffer.buffer; + const buffer_memory = if (buffer.interface.memory) |memory| memory else return VkError.InvalidDeviceMemoryDrv; + const index_size = indexTypeSize(index_buffer.index_type) orelse { + base.unsupported("index type {any}", .{index_buffer.index_type}); + return VkError.Unknown; + }; + + const byte_offset = buffer.interface.offset + index_buffer.offset + (first_index * index_size); + const byte_size = index_count * index_size; + const index_memory: []const u8 = @as([*]const u8, @ptrCast(@alignCast(try buffer_memory.map(byte_offset, byte_size))))[0..byte_size]; + + const indices = allocator.alloc(u32, index_count) catch return VkError.OutOfDeviceMemory; + for (indices, 0..) |*index, i| { + const offset = i * index_size; + const raw_index: u32 = switch (index_size) { + 1 => index_memory[offset], + 2 => std.mem.readInt(u16, index_memory[offset..][0..2], .little), + 4 => @intCast(std.mem.readInt(u32, index_memory[offset..][0..4], .little)), + else => unreachable, + }; + index.* = @intCast(vertex_offset + raw_index); + } + + return indices; +} + +fn indexTypeSize(index_type: vk.IndexType) ?usize { + return switch (index_type) { + .uint8 => 1, + .uint16 => 2, + .uint32 => 4, + else => null, + }; +} + +fn triangleArea2(v0: *const Vertex, v1: *const Vertex, v2: *const Vertex) f32 { + const x0 = v0.position[0]; + const y0 = v0.position[1]; + const x1 = v1.position[0]; + const y1 = v1.position[1]; + const x2 = v2.position[0]; + const y2 = v2.position[1]; + + return ((x1 - x0) * (y2 - y0)) - ((y1 - y0) * (x2 - x0)); +} + +fn triangleIsCulled(self: *Self, v0: *const Vertex, v1: *const Vertex, v2: *const Vertex) VkError!bool { + const pipeline_data = (self.state.pipeline orelse return VkError.InvalidHandleDrv).interface.mode.graphics; + const rasterization = pipeline_data.rasterization; + const cull_mode = rasterization.cull_mode; + + if (!cull_mode.front_bit and !cull_mode.back_bit) + return false; + + if (cull_mode.front_bit and cull_mode.back_bit) + return true; + + const area = triangleArea2(v0, v1, v2); + if (area == 0.0) + return true; + + const front_face = switch (rasterization.front_face) { + .counter_clockwise => area < 0.0, + .clockwise => area > 0.0, + else => return false, + }; + + return (cull_mode.front_bit and front_face) or (cull_mode.back_bit and !front_face); +} diff --git a/src/soft/device/vertex_dispatcher.zig b/src/soft/device/vertex_dispatcher.zig index 05d01de..3e9a088 100644 --- a/src/soft/device/vertex_dispatcher.zig +++ b/src/soft/device/vertex_dispatcher.zig @@ -18,6 +18,9 @@ pub const RunData = struct { batch_id: usize, batch_size: usize, vertex_count: usize, + first_vertex: usize, + first_instance: usize, + indices: ?[]const u32, instance_index: usize, draw_call: *Renderer.DrawCall, }; @@ -41,7 +44,10 @@ inline fn run(data: RunData) !void { var invocation_index: usize = data.batch_id; while (invocation_index < data.vertex_count) : (invocation_index += data.batch_size) { - setupBuiltins(rt, invocation_index, data.instance_index) catch |err| switch (err) { + const vertex_index = if (data.indices) |indices| indices[invocation_index] else data.first_vertex + invocation_index; + const instance_index = data.first_instance + data.instance_index; + + setupBuiltins(rt, vertex_index, instance_index) catch |err| switch (err) { SpvRuntimeError.NotFound => {}, else => return err, }; @@ -56,7 +62,7 @@ inline fn run(data: RunData) !void { const buffer = vertex_buffer.buffer; const buffer_memory_size = base.format.texelSize(attribute.format); const buffer_memory = if (buffer.interface.memory) |memory| memory else return VkError.InvalidDeviceMemoryDrv; - const offset = buffer.interface.offset + (binding_info.stride * invocation_index) + attribute.offset; + const offset = buffer.interface.offset + vertex_buffer.offset + (binding_info.stride * vertex_index) + attribute.offset; const buffer_memory_map: []u8 = @as([*]u8, @ptrCast(@alignCast(try buffer_memory.map(offset, buffer_memory_size))))[0..buffer_memory_size]; @@ -89,7 +95,7 @@ inline fn run(data: RunData) !void { } } -fn setupBuiltins(rt: *spv.Runtime, invocation_index: usize, instance_index: usize) !void { - try rt.writeBuiltIn(std.mem.asBytes(&invocation_index), .VertexIndex); +fn setupBuiltins(rt: *spv.Runtime, vertex_index: usize, instance_index: usize) !void { + try rt.writeBuiltIn(std.mem.asBytes(&vertex_index), .VertexIndex); try rt.writeBuiltIn(std.mem.asBytes(&instance_index), .InstanceIndex); } diff --git a/src/vulkan/CommandBuffer.zig b/src/vulkan/CommandBuffer.zig index 2b482b2..8d8e357 100644 --- a/src/vulkan/CommandBuffer.zig +++ b/src/vulkan/CommandBuffer.zig @@ -54,6 +54,7 @@ pub const DispatchTable = struct { dispatch: *const fn (*Self, u32, u32, u32) VkError!void, dispatchIndirect: *const fn (*Self, *Buffer, vk.DeviceSize) VkError!void, draw: *const fn (*Self, usize, usize, usize, usize) VkError!void, + drawIndexed: *const fn (*Self, usize, usize, usize, usize, usize) VkError!void, end: *const fn (*Self) VkError!void, endRenderPass: *const fn (*Self) VkError!void, executeCommands: *const fn (*Self, *Self) VkError!void, @@ -214,6 +215,10 @@ pub inline fn draw(self: *Self, vertex_count: usize, instance_count: usize, firs try self.dispatch_table.draw(self, vertex_count, instance_count, first_vertex, first_instance); } +pub inline fn drawIndexed(self: *Self, index_count: usize, instance_count: usize, first_index: usize, vertex_offset: usize, first_instance: usize) VkError!void { + try self.dispatch_table.drawIndexed(self, index_count, instance_count, first_index, vertex_offset, first_instance); +} + pub inline fn endRenderPass(self: *Self) VkError!void { try self.dispatch_table.endRenderPass(self); } diff --git a/src/vulkan/lib_vulkan.zig b/src/vulkan/lib_vulkan.zig index d969e54..cf9f616 100644 --- a/src/vulkan/lib_vulkan.zig +++ b/src/vulkan/lib_vulkan.zig @@ -1822,15 +1822,7 @@ pub export fn strollCmdDrawIndexed(p_cmd: vk.CommandBuffer, index_count: u32, in defer entryPointEndLogTrace(); const cmd = Dispatchable(CommandBuffer).fromHandleObject(p_cmd) catch |err| return errorLogger(err); - - notImplementedWarning(); - - _ = cmd; - _ = index_count; - _ = instance_count; - _ = first_index; - _ = vertex_offset; - _ = first_instance; + cmd.drawIndexed(index_count, instance_count, first_index, vertex_offset, first_instance) catch |err| return errorLogger(err); } pub export fn strollCmdDrawIndexedIndirect(p_cmd: vk.CommandBuffer, p_buffer: vk.Buffer, offset: vk.DeviceSize, count: u32, stride: u32) callconv(vk.vulkan_call_conv) void {