Skip to content

Commit

Permalink
[progress #146] custom bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
tiawl committed Apr 4, 2024
1 parent 82b561a commit 67c1ed5
Show file tree
Hide file tree
Showing 16 changed files with 429 additions and 169 deletions.
87 changes: 87 additions & 0 deletions src/binding/vk/buffer.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const std = @import ("std");
const c = @import ("c");

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

pub const Buffer = enum (u64)
{
NULL_HANDLE = vk.NULL_HANDLE, _,

pub const Copy = extern struct
{
src_offset: vk.Device.Size,
dst_offset: vk.Device.Size,
size: vk.Device.Size,
};

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

pub const Info = extern struct
{
s_type: vk.StructureType = .BUFFER_CREATE_INFO,
p_next: ?*const anyopaque = null,
flags: vk.Buffer.Create.Flags = 0,
size: vk.Device.Size,
usage: vk.Buffer.Usage.Flags,
sharing_mode: vk.SharingMode,
queue_family_index_count: u32 = 0,
p_queue_family_indices: ?[*] const u32 = null,
};
};

pub const Memory = extern struct
{
pub const Requirements = extern struct
{
pub fn get (device: vk.Device, buffer: vk.Buffer) vk.Memory.Requirements
{
var memory_requirements: vk.Memory.Requirements = undefined;
raw.prototypes.device.vkGetBufferMemoryRequirements (device, buffer, &memory_requirements);
return memory_requirements;
}
};

pub fn bind (device: vk.Device, buffer: vk.Buffer, memory: vk.Device.Memory, memory_offset: vk.Device.Size) !void
{
const result = raw.prototypes.device.vkBindBufferMemory (device, buffer, memory, memory_offset);
if (result > 0)
{
std.debug.print ("{s} failed with {} status code\n", .{ @typeName (@This ()) ++ "." ++ @src ().fn_name, result, });
return error.UnexpectedResult;
}
}
};

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

pub const Bit = enum (vk.Buffer.Usage.Flags)
{
INDEX_BUFFER = c.VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
TRANSFER_DST = c.VK_BUFFER_USAGE_TRANSFER_DST_BIT,
TRANSFER_SRC = c.VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VERTEX_BUFFER = c.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
};
};

pub fn create (device: vk.Device, p_create_info: *const vk.Buffer.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !@This ()
{
var buffer: vk.Buffer = undefined;
const result = raw.prototypes.device.vkCreateBuffer (device, p_create_info, p_allocator, &buffer);
if (result > 0)
{
std.debug.print ("{s} failed with {} status code\n", .{ @typeName (@This ()) ++ "." ++ @src ().fn_name, result, });
return error.UnexpectedResult;
}
return buffer;
}

pub fn destroy (buffer: @This (), device: vk.Device, p_allocator: ?*const vk.AllocationCallbacks) void
{
raw.prototypes.device.vkDestroyBuffer (device, buffer, p_allocator);
}
};
105 changes: 102 additions & 3 deletions src/binding/vk/command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,106 @@ const raw = @import ("raw");

pub const Command = extern struct
{
pub const Buffer = enum (usize) { NULL_HANDLE = vk.NULL_HANDLE, _, };
pub const Buffer = enum (usize)
{
NULL_HANDLE = vk.NULL_HANDLE, _,

pub const Allocate = extern struct
{
pub const Info = extern struct
{
s_type: vk.StructureType = .COMMAND_BUFFER_ALLOCATE_INFO,
p_next: ?*const anyopaque = null,
command_pool: vk.Command.Pool,
level: vk.Command.Buffer.Level,
command_buffer_count: u32,
};
};

pub const Begin = extern struct
{
pub const Info = extern struct
{
s_type: vk.StructureType = .COMMAND_BUFFER_BEGIN_INFO,
p_next: ?*const anyopaque = null,
flags: vk.Command.Buffer.Usage.Flags = 0,
p_inheritance_info: ?*const vk.Command.Buffer.Inheritance.Info = null,
};
};

pub const Inheritance = extern struct
{
pub const Info = extern struct
{
s_type: vk.StructureType = .COMMAND_BUFFER_INHERITANCE_INFO,
p_next: ?*const anyopaque = null,
render_pass: vk.RenderPass = .NULL_HANDLE,
subpass: u32,
framebuffer: vk.Framebuffer = .NULL_HANDLE,
occlusion_query_enable: vk.Bool32,
query_flags: vk.Query.Control.Flags = 0,
pipeline_statistics: vk.Query.PipelineStatistic.Flags = 0,
};
};

pub const Level = enum (i32)
{
PRIMARY = c.VK_COMMAND_BUFFER_LEVEL_PRIMARY,
};

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

pub const Bit = enum (vk.Command.Buffer.Usage.Flags)
{
ONE_TIME_SUBMIT = c.VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
};
};

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

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

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
{
raw.prototypes.device.vkCmdCopyBuffer (command_buffer, src_buffer, dst_buffer, region_count, p_regions);
}
};

pub const Buffers = extern struct
{
pub fn allocate (device: vk.Device, p_allocate_info: *const vk.Command.Buffer.Allocate.Info, p_command_buffers: [*] vk.Command.Buffer) !void
{
const result = raw.prototypes.device.vkAllocateCommandBuffers (device, p_allocate_info, p_command_buffers);
if (result > 0)
{
std.debug.print ("{s} failed with {} status code\n", .{ @typeName (@This ()) ++ "." ++ @src ().fn_name, result, });
return error.UnexpectedResult;
}
}

pub fn free (device: vk.Device, command_pool: vk.Command.Pool, command_buffer_count: u32, p_command_buffers: [*] const vk.Command.Buffer) void
{
raw.prototypes.device.vkFreeCommandBuffers (device, command_pool, command_buffer_count, p_command_buffers);
}
};

pub const Pool = enum (u64)
{
Expand All @@ -31,7 +130,7 @@ pub const Command = extern struct
};
};

pub fn create (device: vk.Device, p_create_info: *const vk.Command.Pool.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !vk.Command.Pool
pub fn create (device: vk.Device, p_create_info: *const vk.Command.Pool.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !@This ()
{
var command_pool: vk.Command.Pool = undefined;
const result = raw.prototypes.device.vkCreateCommandPool (device, p_create_info, p_allocator, &command_pool);
Expand All @@ -43,7 +142,7 @@ pub const Command = extern struct
return command_pool;
}

pub fn destroy (device: vk.Device, command_pool: vk.Command.Pool, p_allocator: ?*const vk.AllocationCallbacks) void
pub fn destroy (command_pool: @This (), device: vk.Device, p_allocator: ?*const vk.AllocationCallbacks) void
{
raw.prototypes.device.vkDestroyCommandPool (device, command_pool, p_allocator);
}
Expand Down
4 changes: 2 additions & 2 deletions src/binding/vk/descriptor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub const Descriptor = extern struct
p_bindings: ?[*] const vk.Descriptor.Set.Layout.Binding = null,
};
};
pub fn create (device: vk.Device, p_create_info: *const vk.Descriptor.Set.Layout.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !vk.Descriptor.Set.Layout
pub fn create (device: vk.Device, p_create_info: *const vk.Descriptor.Set.Layout.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !@This ()
{
var set_layout: vk.Descriptor.Set.Layout = undefined;
const result = raw.prototypes.device.vkCreateDescriptorSetLayout (device, p_create_info, p_allocator, &set_layout);
Expand All @@ -50,7 +50,7 @@ pub const Descriptor = extern struct
return set_layout;
}

pub fn destroy (device: vk.Device, descriptor_set_layout: vk.Descriptor.Set.Layout, p_allocator: ?*const vk.AllocationCallbacks) void
pub fn destroy (descriptor_set_layout: @This (), device: vk.Device, p_allocator: ?*const vk.AllocationCallbacks) void
{
raw.prototypes.device.vkDestroyDescriptorSetLayout(device, descriptor_set_layout, p_allocator);
}
Expand Down
31 changes: 29 additions & 2 deletions src/binding/vk/device.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,34 @@ const raw = @import ("raw");
pub const Device = enum (usize)
{
NULL_HANDLE = vk.NULL_HANDLE, _,
pub const Memory = enum (u64) { NULL_HANDLE = vk.NULL_HANDLE, _, };

pub const Memory = enum (u64)
{
NULL_HANDLE = vk.NULL_HANDLE, _,

pub fn free (memory: @This (), device: vk.Device, p_allocator: ?*const vk.AllocationCallbacks) void
{
raw.prototypes.device.vkFreeMemory (device, memory, p_allocator);
}

pub fn map (memory: @This (), device: vk.Device, offset: vk.Device.Size, size: vk.Device.Size, flags: vk.Memory.Map.Flags) !?*anyopaque
{
var pp_data: ?*anyopaque = undefined;
const result = raw.prototypes.device.vkMapMemory (device, memory, offset, size, flags, &pp_data);
if (result > 0)
{
std.debug.print ("{s} failed with {} status code\n", .{ @typeName (@This ()) ++ "." ++ @src ().fn_name, result, });
return error.UnexpectedResult;
}
return pp_data;
}

pub fn unmap (memory: @This (), device: vk.Device) void
{
raw.prototypes.device.vkUnmapMemory (device, memory);
}
};

pub const Size = u64;

pub fn load (self: @This ()) !void
Expand All @@ -21,7 +48,7 @@ pub const Device = enum (usize)
}
}

pub fn create (physical_device: vk.PhysicalDevice, p_create_info: *const vk.Device.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !vk.Device
pub fn create (physical_device: vk.PhysicalDevice, p_create_info: *const vk.Device.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !@This ()
{
var device: vk.Device = undefined;
const result = raw.prototypes.instance.vkCreateDevice (physical_device, p_create_info, p_allocator, &device);
Expand Down
4 changes: 2 additions & 2 deletions src/binding/vk/framebuffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub const Framebuffer = enum (u64)
};
};

pub fn create (device: vk.Device, p_create_info: *const vk.Framebuffer.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !vk.Framebuffer
pub fn create (device: vk.Device, p_create_info: *const vk.Framebuffer.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !@This ()
{
var framebuffer: vk.Framebuffer = undefined;
const result = raw.prototypes.device.vkCreateFramebuffer (device, p_create_info, p_allocator, &framebuffer);
Expand All @@ -38,7 +38,7 @@ pub const Framebuffer = enum (u64)
return framebuffer;
}

pub fn destroy (device: vk.Device, framebuffer: vk.Framebuffer, p_allocator: ?*const vk.AllocationCallbacks) void
pub fn destroy (framebuffer: @This (), device: vk.Device, p_allocator: ?*const vk.AllocationCallbacks) void
{
raw.prototypes.device.vkDestroyFramebuffer (device, framebuffer, p_allocator);
}
Expand Down
8 changes: 4 additions & 4 deletions src/binding/vk/image.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub const Image = enum (u64)
{
NULL_HANDLE = vk.NULL_HANDLE, _,

pub fn create (device: vk.Device, p_create_info: *const vk.Image.View.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !vk.Image.View
pub fn create (device: vk.Device, p_create_info: *const vk.Image.View.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !@This ()
{
var view: vk.Image.View = undefined;
const result = raw.prototypes.device.vkCreateImageView (device, p_create_info, p_allocator, &view);
Expand All @@ -121,7 +121,7 @@ pub const Image = enum (u64)
return view;
}

pub fn destroy (device: vk.Device, image_view: vk.Image.View, p_allocator: ?*const vk.AllocationCallbacks) void
pub fn destroy (image_view: @This (),device: vk.Device, p_allocator: ?*const vk.AllocationCallbacks) void
{
raw.prototypes.device.vkDestroyImageView (device, image_view, p_allocator);
}
Expand Down Expand Up @@ -151,7 +151,7 @@ pub const Image = enum (u64)
};
};

pub fn create (device: vk.Device, p_create_info: *const vk.Image.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !vk.Image
pub fn create (device: vk.Device, p_create_info: *const vk.Image.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !@This ()
{
var image: vk.Image = undefined;
const result = raw.prototypes.device.vkCreateImage (device, p_create_info, p_allocator, &image);
Expand All @@ -163,7 +163,7 @@ pub const Image = enum (u64)
return image;
}

pub fn destroy (device: vk.Device, image: vk.Image, p_allocator: ?*const vk.AllocationCallbacks) void
pub fn destroy (image: @This (), device: vk.Device, p_allocator: ?*const vk.AllocationCallbacks) void
{
raw.prototypes.device.vkDestroyImage (device, image, p_allocator);
}
Expand Down
4 changes: 2 additions & 2 deletions src/binding/vk/khr/swapchain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub const Swapchain = enum (u64)
{
NULL_HANDLE = vk.NULL_HANDLE, _,

pub fn create (device: vk.Device, p_create_info: *const vk.KHR.Swapchain.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !vk.KHR.Swapchain
pub fn create (device: vk.Device, p_create_info: *const vk.KHR.Swapchain.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !@This ()
{
var swapchain: vk.KHR.Swapchain = undefined;
const result = raw.prototypes.device.vkCreateSwapchainKHR (device, p_create_info, p_allocator, &swapchain);
Expand All @@ -20,7 +20,7 @@ pub const Swapchain = enum (u64)
return swapchain;
}

pub fn destroy (device: vk.Device, swapchain: vk.KHR.Swapchain, p_allocator: ?*const vk. AllocationCallbacks) void
pub fn destroy (swapchain: @This (), device: vk.Device, p_allocator: ?*const vk. AllocationCallbacks) void
{
raw.prototypes.device.vkDestroySwapchainKHR (device, swapchain, p_allocator);
}
Expand Down
10 changes: 5 additions & 5 deletions src/binding/vk/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ pub const Memory = extern struct
flags: vk.Memory.Heap.Flags = 0,
};

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

pub const Property = extern struct
{
pub const Flags = u32;
Expand Down Expand Up @@ -61,9 +66,4 @@ pub const Memory = extern struct
}
return memory;
}

pub fn free (device: vk.Device, memory: vk.Device.Memory, p_allocator: ?*const vk.AllocationCallbacks) void
{
raw.prototypes.device.vkFreeMemory (device, memory, p_allocator);
}
};
6 changes: 3 additions & 3 deletions src/binding/vk/physical_device.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub const PhysicalDevice = enum (usize)
variable_multisample_rate: vk.Bool32 = vk.FALSE,
inherited_queries: vk.Bool32 = vk.FALSE,

pub fn get (physical_device: vk.PhysicalDevice) vk.PhysicalDevice.Features
pub fn get (physical_device: vk.PhysicalDevice) @This ()
{
var features: vk.PhysicalDevice.Features = undefined;
raw.prototypes.instance.vkGetPhysicalDeviceFeatures (physical_device, &features);
Expand Down Expand Up @@ -206,7 +206,7 @@ pub const PhysicalDevice = enum (usize)
memory_heap_count: u32,
memory_heaps: [vk.MAX_MEMORY_HEAPS] vk.Memory.Heap,

pub fn get (physical_device: vk.PhysicalDevice) vk.PhysicalDevice.Memory.Properties
pub fn get (physical_device: vk.PhysicalDevice) @This ()
{
var memory_properties: vk.PhysicalDevice.Memory.Properties = undefined;
raw.prototypes.instance.vkGetPhysicalDeviceMemoryProperties (physical_device, &memory_properties);
Expand All @@ -227,7 +227,7 @@ pub const PhysicalDevice = enum (usize)
limits: vk.PhysicalDevice.Limits,
sparse_properties: vk.PhysicalDevice.SparseProperties,

pub fn get (physical_device: vk.PhysicalDevice) vk.PhysicalDevice.Properties
pub fn get (physical_device: vk.PhysicalDevice) @This ()
{
var properties: vk.PhysicalDevice.Properties = undefined;
raw.prototypes.instance.vkGetPhysicalDeviceProperties (physical_device, &properties);
Expand Down
Loading

0 comments on commit 67c1ed5

Please sign in to comment.