diff --git a/src/au.zig b/src/au.zig index 79e1a37..658fc98 100644 --- a/src/au.zig +++ b/src/au.zig @@ -7,6 +7,7 @@ const c = @import("c.zig"); pub const Bus = @import("au/Bus.zig"); pub const SwapChain = @import("au/SwapChain.zig"); pub const Flights = @import("au/flights.zig").Flights; +pub const VkAllocator = @import("au/VkAllocator.zig"); pub const use_debug_messenger = switch (builtin.mode) { .Debug, .ReleaseSafe => true, diff --git a/src/au/VkAllocator.zig b/src/au/VkAllocator.zig new file mode 100644 index 0000000..0533c65 --- /dev/null +++ b/src/au/VkAllocator.zig @@ -0,0 +1,43 @@ +const std = @import("std"); +const vk = @import("vk"); +const au = @import("../au.zig"); + +const Self = @This(); + +props: vk.PhysicalDeviceMemoryProperties, + +pub fn init() Self { + return .{ + .props = au.I.getPhysicalDeviceMemoryProperties(au.device_config.pdev), + }; +} + +pub fn heaps(self: Self) []const vk.MemoryHeap { + return self.props.memory_heaps[0..self.props.memory_heap_count]; +} + +pub fn types(self: Self) []const vk.MemoryType { + return self.props.memory_types[0..self.props.memory_type_count]; +} + +pub fn alloc(self: Self, reqs: vk.MemoryRequirements, flags: vk.MemoryPropertyFlags) !vk.DeviceMemory { + const memory_type_bits: std.bit_set.IntegerBitSet(vk.MAX_MEMORY_TYPES) = .{ + .mask = reqs.memory_type_bits, + }; + + for (self.types(), 0..) |typ, idx| { + if (!memory_type_bits.isSet(idx)) continue; + if (!typ.property_flags.contains(flags)) continue; + + return try au.D.allocateMemory(&.{ + .allocation_size = reqs.size, + .memory_type_index = @intCast(idx), + }, null); + } + + return error.NoSuitableMemoryType; +} + +pub fn free(_: Self, memory: vk.DeviceMemory) void { + au.D.freeMemory(memory, null); +} diff --git a/src/main.zig b/src/main.zig index eb4d34c..af3c540 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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 },