adding draw indexed
Build / build (push) Successful in 58s
Test / build_and_test (push) Successful in 36m31s

This commit is contained in:
2026-04-30 02:16:49 +02:00
parent 621be5db35
commit 354c9891d6
6 changed files with 207 additions and 87 deletions
+2
View File
@@ -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,
},
},
+152 -72
View File
@@ -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);
}
+10 -4
View File
@@ -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);
}