Skip to content

Commit

Permalink
[progress #146] custom bindings done
Browse files Browse the repository at this point in the history
  • Loading branch information
tiawl committed Apr 6, 2024
1 parent 1b7df46 commit 3a96150
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 109 deletions.
6 changes: 3 additions & 3 deletions src/binding/imgui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub const vk = struct
{
pub const Ex = struct
{
pub fn render (draw_data: *const c.ImGui_DrawData, command_buffer: binding.vk.Command.Buffer, pipeline: binding.vk.Pipeline) void
pub fn render (draw_data: *c.ImDrawData, command_buffer: binding.vk.Command.Buffer, pipeline: binding.vk.Pipeline) void
{
c.cImGui_ImplVulkan_RenderDrawDataEx (draw_data, @ptrFromInt (@intFromEnum (command_buffer)), @ptrFromInt (@intFromEnum (pipeline)));
}
Expand Down Expand Up @@ -59,7 +59,7 @@ pub const vk = struct

fn loader (function_name: [*c] const u8, instance: ?*anyopaque) callconv (binding.vk.call_conv) ?*const fn () callconv (binding.vk.call_conv) void
{
return c.vkGetInstanceProcAddr (if (instance) |v| @as (c.VkInstance, @ptrCast (v)) else null, function_name);
return c.glfwGetInstanceProcAddress (if (instance) |v| @as (c.VkInstance, @ptrCast (v)) else null, function_name);
}

pub fn load () !void
Expand Down Expand Up @@ -122,7 +122,7 @@ pub const Col = struct

pub const DrawData = struct
{
pub fn get () *const c.ImDrawData
pub fn get () *c.ImDrawData
{
return &(c.ImGui_GetDrawData ().*);
}
Expand Down
87 changes: 87 additions & 0 deletions src/binding/vk/command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ pub const Command = extern struct
PRIMARY = c.VK_COMMAND_BUFFER_LEVEL_PRIMARY,
};

pub const Reset = extern struct
{
pub const Flags = u32;
};

pub const Usage = extern struct
{
pub const Flags = u32;
Expand Down Expand Up @@ -87,6 +92,19 @@ pub const Command = extern struct
}
}

pub fn begin_render_pass (command_buffer: @This (),
p_render_pass_begin: *const vk.RenderPass.Begin.Info,
contents: vk.Subpass.Contents) void
{
raw.prototypes.device.vkCmdBeginRenderPass (command_buffer,
p_render_pass_begin, @intFromEnum (contents));
}

pub fn end_render_pass (command_buffer: @This ()) void
{
raw.prototypes.device.vkCmdEndRenderPass (command_buffer);
}

pub fn blit_image (command_buffer: @This (), src_image: vk.Image,
src_image_layout: vk.Image.Layout, dst_image: vk.Image,
dst_image_layout: vk.Image.Layout, region_count: u32,
Expand All @@ -98,6 +116,40 @@ pub const Command = extern struct
@intFromEnum (filter));
}

pub fn bind_descriptor_sets (command_buffer: @This (),
pipeline_bind_point: vk.Pipeline.BindPoint, layout: vk.Pipeline.Layout,
first_set: u32, descriptor_set_count: u32,
p_descriptor_sets: [*] const vk.Descriptor.Set,
dynamic_offset_count: u32, p_dynamic_offsets: ?[*]const u32) void
{
raw.prototypes.device.vkCmdBindDescriptorSets (command_buffer,
@intFromEnum (pipeline_bind_point), layout, first_set,
descriptor_set_count, p_descriptor_sets, dynamic_offset_count,
p_dynamic_offsets);
}

pub fn bind_index_buffer (command_buffer: @This (), buffer: vk.Buffer,
offset: vk.Device.Size, index_type: vk.IndexType) void
{
raw.prototypes.device.vkCmdBindIndexBuffer (command_buffer, buffer,
offset, @intFromEnum (index_type));
}

pub fn bind_pipeline (command_buffer: @This (),
pipeline_bind_point: vk.Pipeline.BindPoint, pipeline: vk.Pipeline) void
{
raw.prototypes.device.vkCmdBindPipeline (command_buffer,
@intFromEnum (pipeline_bind_point), pipeline);
}

pub fn bind_vertex_buffers (command_buffer: @This (), first_binding: u32,
binding_count: u32, p_buffers: [*] const vk.Buffer,
p_offsets: [*] const vk.Device.Size) void
{
raw.prototypes.device.vkCmdBindVertexBuffers (command_buffer,
first_binding, binding_count, p_buffers, p_offsets);
}

pub fn copy_buffer (command_buffer: @This (), src_buffer: vk.Buffer,
dst_buffer: vk.Buffer, region_count: u32,
p_regions: [*] const vk.Buffer.Copy) void
Expand All @@ -116,6 +168,14 @@ pub const Command = extern struct
@intFromEnum (dst_image_layout), region_count, p_regions);
}

pub fn draw_indexed (command_buffer: @This (), index_count: u32,
instance_count: u32, first_index: u32, vertex_offset: i32,
first_instance: u32) void
{
raw.prototypes.device.vkCmdDrawIndexed (command_buffer, index_count,
instance_count, first_index, vertex_offset, first_instance);
}

pub fn pipeline_barrier (command_buffer: @This (),
src_stage_mask: vk.Pipeline.Stage.Flags,
dst_stage_mask: vk.Pipeline.Stage.Flags,
Expand All @@ -132,6 +192,33 @@ pub const Command = extern struct
p_buffer_memory_barriers, image_memory_barrier_count,
p_image_memory_barriers);
}

pub fn reset (command_buffer: @This (),
flags: vk.Command.Buffer.Reset.Flags) !void
{
const result = raw.prototypes.device.vkResetCommandBuffer (
command_buffer, flags);
if (result > 0)
{
std.debug.print ("{s} failed with {} status code\n",
.{ @typeName (@This ()) ++ "." ++ @src ().fn_name, result, });
return error.UnexpectedResult;
}
}

pub fn set_scissor (command_buffer: @This (), first_scissor: u32,
scissor_count: u32, p_scissors: [*] const vk.Rect2D) void
{
raw.prototypes.device.vkCmdSetScissor (command_buffer, first_scissor,
scissor_count, p_scissors);
}

pub fn set_viewport (command_buffer: @This (), first_viewport: u32,
viewport_count: u32, p_viewports: [*] const vk.Viewport) void
{
raw.prototypes.device.vkCmdSetViewport (command_buffer, first_viewport,
viewport_count, p_viewports);
}
};

pub const Buffers = extern struct
Expand Down
35 changes: 1 addition & 34 deletions src/binding/vk/khr.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,5 @@ pub const Present = extern struct
};
};

pub const Surface = enum (u64)
{
NULL_HANDLE = vk.NULL_HANDLE, _,
pub const Capabilities = extern struct
{
min_image_count: u32,
max_image_count: u32,
current_extent: vk.Extent2D,
min_image_extent: vk.Extent2D,
max_image_extent: vk.Extent2D,
max_image_array_layers: u32,
supported_transforms: vk.KHR.Surface.Transform.Flags,
current_transform: vk.KHR.Surface.Transform.Flags,
supported_composite_alpha: vk.KHR.CompositeAlpha.Flags,
supported_usage_flags: vk.Image.Usage.Flags,
};

pub const Format = extern struct
{
format: vk.Format,
color_space: vk.KHR.ColorSpace,
};

pub const Transform = extern struct
{
pub const Flags = u32;

pub const Bit = enum (vk.KHR.Surface.Transform.Flags)
{
IDENTITY = c.VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
};
};
};

pub const Surface = @import ("surface").Surface;
pub const Swapchain = @import ("swapchain").Swapchain;
46 changes: 46 additions & 0 deletions src/binding/vk/khr/surface.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const std = @import ("std");
const c = @import ("c");

const vk = @import ("vk");
const raw = @import ("raw");

pub const Surface = enum (u64)
{
NULL_HANDLE = vk.NULL_HANDLE, _,
pub const Capabilities = extern struct
{
min_image_count: u32,
max_image_count: u32,
current_extent: vk.Extent2D,
min_image_extent: vk.Extent2D,
max_image_extent: vk.Extent2D,
max_image_array_layers: u32,
supported_transforms: vk.KHR.Surface.Transform.Flags,
current_transform: vk.KHR.Surface.Transform.Flags,
supported_composite_alpha: vk.KHR.CompositeAlpha.Flags,
supported_usage_flags: vk.Image.Usage.Flags,
};

pub const Format = extern struct
{
format: vk.Format,
color_space: vk.KHR.ColorSpace,
};

pub const Transform = extern struct
{
pub const Flags = u32;

pub const Bit = enum (vk.KHR.Surface.Transform.Flags)
{
IDENTITY = c.VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
};
};

pub fn destroy (surface: @This (),instance: vk.Instance) void
{
const p_allocator: ?*const vk.AllocationCallbacks = null;
raw.prototypes.instance.vkDestroySurfaceKHR (instance, surface,
p_allocator);
}
};
10 changes: 5 additions & 5 deletions src/binding/vk/pipeline.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ pub const Pipeline = enum (u64)
{
NULL_HANDLE = vk.NULL_HANDLE, _,

pub const BindPoint = enum (u32)
{
GRAPHICS = c.VK_PIPELINE_BIND_POINT_GRAPHICS,
};

pub const Cache = enum (u64) { NULL_HANDLE = vk.NULL_HANDLE, _, };

pub const ColorBlend = extern struct
Expand Down Expand Up @@ -45,11 +50,6 @@ pub const Pipeline = enum (u64)
};
};

pub const BindPoint = enum (i32)
{
GRAPHICS = c.VK_PIPELINE_BIND_POINT_GRAPHICS,
};

pub const Create = extern struct
{
pub const Flags = u32;
Expand Down
2 changes: 1 addition & 1 deletion src/binding/vk/raw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const Prototypes = struct
});
};

@compileLog (field.name ++ ": " ++ @typeName (pfn));
// @compileLog (field.name ++ ": " ++ @typeName (pfn));
fields [i] = .{
.name = field.name,
.type = pfn,
Expand Down
14 changes: 14 additions & 0 deletions src/binding/vk/render_pass.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ pub const RenderPass = enum (u64)
{
NULL_HANDLE = vk.NULL_HANDLE, _,

pub const Begin = extern struct
{
pub const Info = extern struct
{
s_type: vk.StructureType = .RENDER_PASS_BEGIN_INFO,
p_next: ?*const anyopaque = null,
render_pass: vk.RenderPass,
framebuffer: vk.Framebuffer,
render_area: vk.Rect2D,
clear_value_count: u32 = 0,
p_clear_values: ?[*] const vk.Clear.Value = null,
};
};

pub const Create = extern struct
{
pub const Flags = u32;
Expand Down
34 changes: 34 additions & 0 deletions src/binding/vk/vk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,28 @@ pub const ColorComponent = extern struct
};
};

pub const Clear = extern struct
{
pub const ColorValue = extern union
{
float_32: [4] f32,
int_32: [4] i32,
uint_32: [4] u32,
};

pub const DepthStencilValue = extern struct
{
depth: f32,
stencil: u32,
};

pub const Value = extern union
{
color: vk.Clear.ColorValue,
depth_stencil: vk.Clear.DepthStencilValue,
};
};

pub const Command = @import ("command").Command;

pub const CompareOp = enum (i32)
Expand Down Expand Up @@ -226,6 +248,12 @@ pub const FrontFace = enum (i32)
pub const Graphics = @import ("pipeline").Graphics;

pub const Image = @import ("image").Image;

pub const IndexType = enum (u32)
{
UINT32 = c.VK_INDEX_TYPE_UINT32,
};

pub const Instance = @import ("instance").Instance;

pub const InternalAllocationType = enum (i32) {};
Expand Down Expand Up @@ -421,6 +449,7 @@ pub const StructureType = enum (i32)
PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO = c.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
PIPELINE_VIEWPORT_STATE_CREATE_INFO = c.VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
PRESENT_INFO_KHR = c.VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
RENDER_PASS_BEGIN_INFO = c.VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
RENDER_PASS_CREATE_INFO = c.VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
SAMPLER_CREATE_INFO = c.VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
SEMAPHORE_CREATE_INFO = c.VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
Expand Down Expand Up @@ -449,6 +478,11 @@ pub const Submit = extern struct

pub const Subpass = extern struct
{
pub const Contents = enum (u32)
{
INLINE = c.VK_SUBPASS_CONTENTS_INLINE,
};

pub const Dependency = extern struct
{
src_subpass: u32,
Expand Down
4 changes: 2 additions & 2 deletions src/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ pub const Context = struct
try self.logger.app (.DEBUG, "loop OK", .{});
}

pub fn cleanup (self: @This ()) !void
pub fn deinit (self: @This ()) !void
{
self.imgui.cleanup ();
try self.imgui.deinit ();
try self.vk.cleanup ();
try self.glfw.cleanup ();
try self.logger.app (.DEBUG, "cleanup OK", .{});
Expand Down
4 changes: 2 additions & 2 deletions src/imgui/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ pub const Context = struct
try self.logger.app (.DEBUG, "end render Imgui OK", .{});
}

pub fn cleanup (self: @This ()) void
pub fn deinit (self: @This ()) !void
{
imgui.vk.shutdown ();
imgui.glfw.shutdown ();
imgui.Context.destroy ();

try self.logger.app (.DEBUG, "cleanup Imgui OK", .{});
try self.logger.app (.DEBUG, "deinit Imgui OK", .{});
}
};
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn main () !void
var options = try Options.init (&logger);

var context = try Context.init (&logger, &options);
defer context.cleanup ();
defer context.deinit () catch {};

try context.loop (&options);
}
Loading

0 comments on commit 3a96150

Please sign in to comment.