render directly from mapped vertex buffer

This commit is contained in:
David Allemang
2024-07-05 15:26:20 -04:00
parent 61ce4c16d8
commit 3b5eb6efab
3 changed files with 102 additions and 17 deletions

View File

@@ -64,6 +64,9 @@ const Frame = struct {
image: vk.Image,
view: vk.ImageView,
scissor: vk.Rect2D,
pipeline: vk.Pipeline,
vertex_buffer: vk.Buffer,
index_buffer: vk.Buffer,
) !void {
_ = self;
@@ -107,7 +110,7 @@ const Frame = struct {
.resolve_image_layout = .undefined,
.load_op = .clear,
.store_op = .store,
.clear_value = .{ .color = .{ .float_32 = .{ 1, 0, 0, 1 } } },
.clear_value = .{ .color = .{ .float_32 = .{ 0, 0, 0, 1 } } },
}},
};
@@ -123,6 +126,11 @@ const Frame = struct {
cmd.beginRendering(&info);
cmd.bindPipeline(.graphics, pipeline);
cmd.bindVertexBuffers(0, 1, &.{vertex_buffer}, &.{0});
cmd.bindIndexBuffer(index_buffer, 0, .uint16);
cmd.drawIndexed(indices.len, 1, 0, 0, 0);
// todo
// vkd.cmdBindPipeline(cmdbuf, .graphics, pipeline);
// const offset = [_]vk.DeviceSize{0};
@@ -184,9 +192,6 @@ pub fn main() !void {
const ctx = im.c.igCreateContext(null) orelse return error.igCreateContextFailed;
defer im.c.igDestroyContext(ctx);
// _ = im.c.ImGui_ImplGlfw_InitForOther(@ptrCast(au.W.handle), true);
// defer im.c.ImGui_ImplGlfw_Shutdown();
const descriptorPool = try au.D.createDescriptorPool(&vk.DescriptorPoolCreateInfo{
.flags = .{ .free_descriptor_set_bit = true },
.pool_size_count = 1,
@@ -247,11 +252,56 @@ pub fn main() !void {
const cache = try au.D.createPipelineCache(&vk.PipelineCacheCreateInfo{}, null);
defer au.D.destroyPipelineCache(cache, null);
// for descriptor sets
const layout = try au.D.createPipelineLayout(&vk.PipelineLayoutCreateInfo{
// todo
.flags = .{},
.set_layout_count = 0,
.p_set_layouts = null,
.push_constant_range_count = 0,
.p_push_constant_ranges = null,
}, null);
defer au.D.destroyPipelineLayout(layout, null);
const vkalloc = au.VkAllocator.init();
const vertex_buffer = try au.D.createBuffer(&vk.BufferCreateInfo{
.size = @sizeOf(@TypeOf(vertices)),
.usage = .{ .vertex_buffer_bit = true },
.sharing_mode = .exclusive,
}, null);
defer au.D.destroyBuffer(vertex_buffer, null);
const vertex_memory = try vkalloc.alloc(
au.D.getBufferMemoryRequirements(vertex_buffer),
.{ .host_visible_bit = true, .host_coherent_bit = true },
);
defer vkalloc.free(vertex_memory);
try au.D.bindBufferMemory(vertex_buffer, vertex_memory, 0);
const vertex_data: [*]Vertex = @ptrCast(@alignCast(try au.D.mapMemory(vertex_memory, 0, vk.WHOLE_SIZE, .{})));
defer au.D.unmapMemory(vertex_memory);
@memcpy(vertex_data[0..vertices.len], &vertices);
const index_buffer = try au.D.createBuffer(&vk.BufferCreateInfo{
.size = @sizeOf(@TypeOf(indices)),
.usage = .{ .index_buffer_bit = true },
.sharing_mode = .exclusive,
}, null);
defer au.D.destroyBuffer(index_buffer, null);
const index_memory = try vkalloc.alloc(
au.D.getBufferMemoryRequirements(index_buffer),
.{ .host_visible_bit = true, .host_coherent_bit = true },
);
defer vkalloc.free(index_memory);
try au.D.bindBufferMemory(index_buffer, index_memory, 0);
const index_data: [*]Index = @ptrCast(@alignCast(try au.D.mapMemory(index_memory, 0, vk.WHOLE_SIZE, .{})));
defer au.D.unmapMemory(index_memory);
@memcpy(index_data[0..indices.len], &indices);
try au.D.deviceWaitIdle();
const gpci: vk.GraphicsPipelineCreateInfo = .{
.stage_count = 2,
.p_stages = &.{
@@ -390,6 +440,9 @@ pub fn main() !void {
image,
view,
vk.Rect2D{ .offset = .{ .x = 0, .y = 0 }, .extent = sc.cinfo.image_extent },
pipeline,
vertex_buffer,
index_buffer,
);
try cmd.endCommandBuffer();
@@ -427,18 +480,6 @@ pub fn main() !void {
try au.D.deviceWaitIdle();
// const pipeline_layout = try dev.vkd.createPipelineLayout(dev.dev, &.{
// .flags = .{},
// .set_layout_count = 0,
// .p_set_layouts = undefined,
// .push_constant_range_count = 0,
// .p_push_constant_ranges = undefined,
// }, null);
// defer dev.vkd.destroyPipelineLayout(dev.dev, pipeline_layout, null);
//
// const pipeline = try createPipeline(dev.dev, pipeline_layout, dev.format, dev.vkd);
// defer dev.vkd.destroyPipeline(dev.dev, pipeline, null);
// const vertex_buffer = try dev.vkd.createBuffer(dev.dev, &.{
// .size = @sizeOf(@TypeOf(vertices)),
// .usage = .{ .transfer_dst_bit = true, .vertex_buffer_bit = true },