adding draw indexed
This commit is contained in:
@@ -60,6 +60,7 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
|
|||||||
.dispatch = dispatch,
|
.dispatch = dispatch,
|
||||||
.dispatchIndirect = dispatchIndirect,
|
.dispatchIndirect = dispatchIndirect,
|
||||||
.draw = draw,
|
.draw = draw,
|
||||||
|
.drawIndexed = drawIndexed,
|
||||||
.end = end,
|
.end = end,
|
||||||
.endRenderPass = endRenderPass,
|
.endRenderPass = endRenderPass,
|
||||||
.executeCommands = executeCommands,
|
.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 {
|
pub fn execute(context: *anyopaque, device: *ExecutionDevice) VkError!void {
|
||||||
const impl: *Impl = @ptrCast(@alignCast(context));
|
const impl: *Impl = @ptrCast(@alignCast(context));
|
||||||
_ = impl;
|
device.pipeline_states[ExecutionDevice.GRAPHICS_PIPELINE_STATE].data.graphics.index_buffer = .{
|
||||||
_ = device;
|
.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;
|
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 {
|
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 self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||||
const allocator = self.command_allocator.allocator();
|
const allocator = self.command_allocator.allocator();
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ pub const PipelineState = struct {
|
|||||||
data: union {
|
data: union {
|
||||||
compute: struct {},
|
compute: struct {},
|
||||||
graphics: struct {
|
graphics: struct {
|
||||||
|
index_buffer: Renderer.IndexBuffer,
|
||||||
vertex_buffers: [lib.MAX_VERTEX_INPUT_BINDINGS]Renderer.VertexBuffer,
|
vertex_buffers: [lib.MAX_VERTEX_INPUT_BINDINGS]Renderer.VertexBuffer,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -45,6 +46,7 @@ pub fn init(self: *Self, device: *SoftDevice) void {
|
|||||||
.data = switch (i) {
|
.data = switch (i) {
|
||||||
GRAPHICS_PIPELINE_STATE => .{
|
GRAPHICS_PIPELINE_STATE => .{
|
||||||
.graphics = .{
|
.graphics = .{
|
||||||
|
.index_buffer = undefined,
|
||||||
.vertex_buffers = undefined,
|
.vertex_buffers = undefined,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
+152
-72
@@ -32,6 +32,12 @@ pub const VertexBuffer = struct {
|
|||||||
size: usize,
|
size: usize,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const IndexBuffer = struct {
|
||||||
|
buffer: *const SoftBuffer,
|
||||||
|
offset: usize,
|
||||||
|
index_type: vk.IndexType,
|
||||||
|
};
|
||||||
|
|
||||||
pub const DynamicState = struct {
|
pub const DynamicState = struct {
|
||||||
viewports: ?[]const vk.Viewport,
|
viewports: ?[]const vk.Viewport,
|
||||||
scissor: ?[]vk.Rect2D,
|
scissor: ?[]vk.Rect2D,
|
||||||
@@ -55,6 +61,19 @@ pub const Fragment = struct {
|
|||||||
pub const DrawCall = struct {
|
pub const DrawCall = struct {
|
||||||
vertices: []Vertex,
|
vertices: []Vertex,
|
||||||
fragments: []Fragment,
|
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,
|
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 {
|
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 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());
|
var arena: std.heap.ArenaAllocator = .init(self.device.device_allocator.allocator());
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
const allocator = arena.allocator();
|
const allocator = arena.allocator();
|
||||||
|
|
||||||
|
var draw_call = try DrawCall.init(allocator, vertex_count, instance_count);
|
||||||
|
|
||||||
const timer = std.Io.Timestamp.now(io, .real);
|
const timer = std.Io.Timestamp.now(io, .real);
|
||||||
defer if (comptime base.config.logs != .none) {
|
defer if (comptime base.config.logs != .none) {
|
||||||
const duration = timer.untilNow(io, .real);
|
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) });
|
std.log.scoped(.SoftwareRenderer).debug("Drawcall stats:\n> Took {d}us\n> Allocated {d} KB", .{ ms, @divTrunc(arena.queryCapacity(), 1000) });
|
||||||
};
|
};
|
||||||
|
|
||||||
var draw_call: DrawCall = .{
|
self.vertexShaderStage(allocator, &draw_call, vertex_count, instance_count, first_vertex, first_instance, null) catch |err| {
|
||||||
.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| {
|
|
||||||
std.log.scoped(.@"Vertex stage").err("catched a '{s}'", .{@errorName(err)});
|
std.log.scoped(.@"Vertex stage").err("catched a '{s}'", .{@errorName(err)});
|
||||||
if (@errorReturnTrace()) |trace| {
|
if (@errorReturnTrace()) |trace| {
|
||||||
std.debug.dumpErrorReturnTrace(trace);
|
std.debug.dumpErrorReturnTrace(trace);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
try self.primitiveAssemblyStage(&draw_call);
|
try self.postVertexDraw(allocator, &draw_call);
|
||||||
try self.rasterizationStage(allocator, &draw_call);
|
}
|
||||||
|
|
||||||
self.fragmentShaderStage(&draw_call) catch |err| {
|
pub fn drawIndexed(self: *Self, index_count: usize, instance_count: usize, first_index: usize, first_instance: usize, vertex_offset: usize) VkError!void {
|
||||||
std.log.scoped(.@"Fragment stage").err("catched a '{s}'", .{@errorName(err)});
|
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| {
|
if (@errorReturnTrace()) |trace| {
|
||||||
std.debug.dumpErrorReturnTrace(trace);
|
std.debug.dumpErrorReturnTrace(trace);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
for (draw_call.fragments) |fragment| {
|
try self.postVertexDraw(allocator, &draw_call);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
_ = self;
|
_ = 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 pipeline = self.state.pipeline orelse return;
|
||||||
const batch_size = (pipeline.stages.getPtr(.vertex) orelse return).runtimes.len;
|
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_id = batch_id,
|
||||||
.batch_size = batch_size,
|
.batch_size = batch_size,
|
||||||
.vertex_count = vertex_count,
|
.vertex_count = vertex_count,
|
||||||
|
.first_vertex = first_vertex,
|
||||||
|
.first_instance = first_instance,
|
||||||
|
.indices = indices,
|
||||||
.instance_index = instance_index,
|
.instance_index = instance_index,
|
||||||
.draw_call = draw_call,
|
.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;
|
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 {
|
fn primitiveAssemblyStage(self: *Self, draw_call: *DrawCall) VkError!void {
|
||||||
const viewport = blk: {
|
const viewport = blk: {
|
||||||
const pipeline_data = &(self.state.pipeline orelse return VkError.InvalidPipelineDrv).interface.mode.graphics;
|
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;
|
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(
|
fn rasterizeTriangle(
|
||||||
self: *Self,
|
self: *Self,
|
||||||
allocator: std.mem.Allocator,
|
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;
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ pub const RunData = struct {
|
|||||||
batch_id: usize,
|
batch_id: usize,
|
||||||
batch_size: usize,
|
batch_size: usize,
|
||||||
vertex_count: usize,
|
vertex_count: usize,
|
||||||
|
first_vertex: usize,
|
||||||
|
first_instance: usize,
|
||||||
|
indices: ?[]const u32,
|
||||||
instance_index: usize,
|
instance_index: usize,
|
||||||
draw_call: *Renderer.DrawCall,
|
draw_call: *Renderer.DrawCall,
|
||||||
};
|
};
|
||||||
@@ -41,7 +44,10 @@ inline fn run(data: RunData) !void {
|
|||||||
|
|
||||||
var invocation_index: usize = data.batch_id;
|
var invocation_index: usize = data.batch_id;
|
||||||
while (invocation_index < data.vertex_count) : (invocation_index += data.batch_size) {
|
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 => {},
|
SpvRuntimeError.NotFound => {},
|
||||||
else => return err,
|
else => return err,
|
||||||
};
|
};
|
||||||
@@ -56,7 +62,7 @@ inline fn run(data: RunData) !void {
|
|||||||
const buffer = vertex_buffer.buffer;
|
const buffer = vertex_buffer.buffer;
|
||||||
const buffer_memory_size = base.format.texelSize(attribute.format);
|
const buffer_memory_size = base.format.texelSize(attribute.format);
|
||||||
const buffer_memory = if (buffer.interface.memory) |memory| memory else return VkError.InvalidDeviceMemoryDrv;
|
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];
|
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 {
|
fn setupBuiltins(rt: *spv.Runtime, vertex_index: usize, instance_index: usize) !void {
|
||||||
try rt.writeBuiltIn(std.mem.asBytes(&invocation_index), .VertexIndex);
|
try rt.writeBuiltIn(std.mem.asBytes(&vertex_index), .VertexIndex);
|
||||||
try rt.writeBuiltIn(std.mem.asBytes(&instance_index), .InstanceIndex);
|
try rt.writeBuiltIn(std.mem.asBytes(&instance_index), .InstanceIndex);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ pub const DispatchTable = struct {
|
|||||||
dispatch: *const fn (*Self, u32, u32, u32) VkError!void,
|
dispatch: *const fn (*Self, u32, u32, u32) VkError!void,
|
||||||
dispatchIndirect: *const fn (*Self, *Buffer, vk.DeviceSize) VkError!void,
|
dispatchIndirect: *const fn (*Self, *Buffer, vk.DeviceSize) VkError!void,
|
||||||
draw: *const fn (*Self, usize, usize, usize, usize) 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,
|
end: *const fn (*Self) VkError!void,
|
||||||
endRenderPass: *const fn (*Self) VkError!void,
|
endRenderPass: *const fn (*Self) VkError!void,
|
||||||
executeCommands: *const fn (*Self, *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);
|
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 {
|
pub inline fn endRenderPass(self: *Self) VkError!void {
|
||||||
try self.dispatch_table.endRenderPass(self);
|
try self.dispatch_table.endRenderPass(self);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1822,15 +1822,7 @@ pub export fn strollCmdDrawIndexed(p_cmd: vk.CommandBuffer, index_count: u32, in
|
|||||||
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.drawIndexed(index_count, instance_count, first_index, vertex_offset, first_instance) catch |err| return errorLogger(err);
|
||||||
notImplementedWarning();
|
|
||||||
|
|
||||||
_ = cmd;
|
|
||||||
_ = index_count;
|
|
||||||
_ = instance_count;
|
|
||||||
_ = first_index;
|
|
||||||
_ = vertex_offset;
|
|
||||||
_ = first_instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user