camel -> snake for command enums + fixes

This commit is contained in:
ashpil
2021-06-26 23:23:50 -04:00
committed by Robin Voetter
parent 0e65efd9d6
commit 8f10cec149
3 changed files with 87 additions and 102 deletions

View File

@@ -70,8 +70,8 @@ For each function, a wrapper is generated into one of three structs:
Each wrapper struct can be called with an array of the appropriate enums:
```zig
const vk = @import("vulkan");
const BaseDispatch = vk.BaseWrapper([_]vk.BaseCommand {
.create_instance,
const BaseDispatch = vk.BaseWrapper(.{
.CreateInstance,
});
```
The wrapper struct then provides wrapper functions for each function pointer in the dispatch struct:

View File

@@ -5,77 +5,77 @@ const Allocator = std.mem.Allocator;
const required_device_extensions = [_][]const u8{vk.extension_info.khr_swapchain.name};
const BaseDispatch = vk.BaseWrapper([_]vk.BaseCommand{
.create_instance,
const BaseDispatch = vk.BaseWrapper(.{
.CreateInstance,
});
const InstanceDispatch = vk.InstanceWrapper([_]vk.InstanceCommand{
.destroy_instance,
.create_device,
.destroy_surface_khr,
.enumerate_physical_devices,
.get_physical_device_properties,
.enumerate_device_extension_properties,
.get_physical_device_surface_formats_khr,
.get_physical_device_surface_present_modes_khr,
.get_physical_device_surface_capabilities_khr,
.get_physical_device_queue_family_properties,
.get_physical_device_surface_support_khr,
.get_physical_device_memory_properties,
.get_device_proc_addr,
const InstanceDispatch = vk.InstanceWrapper(.{
.DestroyInstance,
.CreateDevice,
.DestroySurfaceKHR,
.EnumeratePhysicalDevices,
.GetPhysicalDeviceProperties,
.EnumerateDeviceExtensionProperties,
.GetPhysicalDeviceSurfaceFormatsKHR,
.GetPhysicalDeviceSurfacePresentModesKHR,
.GetPhysicalDeviceSurfaceCapabilitiesKHR,
.GetPhysicalDeviceQueueFamilyProperties,
.GetPhysicalDeviceSurfaceSupportKHR,
.GetPhysicalDeviceMemoryProperties,
.GetDeviceProcAddr,
});
const DeviceDispatch = vk.DeviceWrapper([_]vk.DeviceCommand{
.destroy_device,
.get_device_queue,
.create_semaphore,
.create_fence,
.create_image_view,
.destroy_image_view,
.destroy_semaphore,
.destroy_fence,
.get_swapchain_images_khr,
.create_swapchain_khr,
.destroy_swapchain_khr,
.acquire_next_image_khr,
.device_wait_idle,
.wait_for_fences,
.reset_fences,
.queue_submit,
.queue_present_khr,
.create_command_pool,
.destroy_command_pool,
.allocate_command_buffers,
.free_command_buffers,
.queue_wait_idle,
.create_shader_module,
.destroy_shader_module,
.create_pipeline_layout,
.destroy_pipeline_layout,
.create_render_pass,
.destroy_render_pass,
.create_graphics_pipelines,
.destroy_pipeline,
.create_framebuffer,
.destroy_framebuffer,
.begin_command_buffer,
.end_command_buffer,
.allocate_memory,
.free_memory,
.create_buffer,
.destroy_buffer,
.get_buffer_memory_requirements,
.map_memory,
.unmap_memory,
.bind_buffer_memory,
.cmd_begin_render_pass,
.cmd_end_render_pass,
.cmd_bind_pipeline,
.cmd_draw,
.cmd_set_viewport,
.cmd_set_scissor,
.cmd_bind_vertex_buffers,
.cmd_copy_buffer,
const DeviceDispatch = vk.DeviceWrapper(.{
.DestroyDevice,
.GetDeviceQueue,
.CreateSemaphore,
.CreateFence,
.CreateImageView,
.DestroyImageView,
.DestroySemaphore,
.DestroyFence,
.GetSwapchainImagesKHR,
.CreateSwapchainKHR,
.DestroySwapchainKHR,
.AcquireNextImageKHR,
.DeviceWaitIdle,
.WaitForFences,
.ResetFences,
.QueueSubmit,
.QueuePresentKHR,
.CreateCommandPool,
.DestroyCommandPool,
.AllocateCommandBuffers,
.FreeCommandBuffers,
.QueueWaitIdle,
.CreateShaderModule,
.DestroyShaderModule,
.CreatePipelineLayout,
.DestroyPipelineLayout,
.CreateRenderPass,
.DestroyRenderPass,
.CreateGraphicsPipelines,
.DestroyPipeline,
.CreateFramebuffer,
.DestroyFramebuffer,
.BeginCommandBuffer,
.EndCommandBuffer,
.AllocateMemory,
.FreeMemory,
.CreateBuffer,
.DestroyBuffer,
.GetBufferMemoryRequirements,
.MapMemory,
.UnmapMemory,
.BindBufferMemory,
.CmdBeginRenderPass,
.CmdEndRenderPass,
.CmdBindPipeline,
.CmdDraw,
.CmdSetViewport,
.CmdSetScissor,
.CmdBindVertexBuffers,
.CmdCopyBuffer,
});
pub const GraphicsContext = struct {

View File

@@ -449,33 +449,16 @@ fn Renderer(comptime WriterType: type) type {
if (decl.decl_type == .command) {
const command = decl.decl_type.command;
if (classifyCommandDispatch(decl.name, command) == dispatch_type) {
try self.writeIdentifierWithCase(.snake, trimVkNamespace(decl.name));
try self.writer.writeAll(",\n");
try self.writer.print("{s},\n", .{ trimVkNamespace(decl.name) });
}
}
}
try self.writer.writeAll("};\n\n");
try self.writer.print(
\\fn snakeToCamel{s}(cmd: {s}Command) [:0]const u8 {{
\\ return switch(cmd) {{
, .{dispatch_type_name, dispatch_type_name});
for (self.registry.decls) |decl| {
if (decl.decl_type == .command) {
const command = decl.decl_type.command;
if (classifyCommandDispatch(decl.name, command) == dispatch_type) {
const trimmed_name = trimVkNamespace(decl.name);
try self.writer.writeAll(".");
try self.writeIdentifierWithCase(.snake, trimmed_name);
try self.writer.print(" => \"{s}\",\n", .{ trimmed_name });
}
}
}
try self.writer.writeAll(
\\ };
\\}
);
\\fn {s}CommandToString(cmd: {s}Command) []const u8 {{
\\ return std.meta.tagName(cmd);
\\}}
, .{ dispatch_type_name, dispatch_type_name });
}
fn renderCopyright(self: *Self) !void {
@@ -978,23 +961,25 @@ fn Renderer(comptime WriterType: type) type {
\\pub fn {s}Wrapper(comptime cmds: anytype) type {{
\\ comptime var fields: [cmds.len]std.builtin.TypeInfo.StructField = undefined;
\\ inline for (cmds) |cmd, i| {{
\\ const cmd_camel_case = snakeToCamel{s}(cmd);
\\ const cmd_type_name = "Pfn" ++ cmd_camel_case;
\\ const cmd_name = {s}CommandToString(cmd);
\\ const cmd_type_name = "Pfn" ++ cmd_name;
\\ const cmd_type = @field(GlobalScope, cmd_type_name);
\\ fields[i] = .{{
\\ .name = "vk" ++ cmd_camel_case,
\\ .name = "vk" ++ cmd_name,
\\ .field_type = cmd_type,
\\ .default_value = @as(?cmd_type, null),
\\ .default_value = null,
\\ .is_comptime = false,
\\ .alignment = @alignOf(*cmd_type),
\\ }};
\\ }}
\\ const Dispatch = @Type(.{{ .Struct = .{{
\\ const Dispatch = @Type(.{{
\\ .Struct = .{{
\\ .layout = .Auto,
\\ .fields = &fields,
\\ .decls = &[_]std.builtin.TypeInfo.Declaration{{}},
\\ .is_tuple = false,
\\ }}}});
\\ }},
\\ }});
\\ return struct {{
\\ dispatch: Dispatch,
\\
@@ -1035,7 +1020,7 @@ fn Renderer(comptime WriterType: type) type {
\\ var self: Self = undefined;
\\ inline for (std.meta.fields(Dispatch)) |field| {{
\\ const name = @ptrCast([*:0]const u8, field.name ++ "\x00");
\\ const cmd_ptr = loader({s}name) orelse return error.InvalidLoader;
\\ const cmd_ptr = loader({s}name) orelse return error.CommandLoadFailure;
\\ @field(self.dispatch, field.name) = @ptrCast(field.field_type, cmd_ptr);
\\ }}
\\ return self;