Merge pull request #12 from ashpil/master

camel -> snake for command enums + fixes
This commit is contained in:
Robin Voetter
2021-06-27 18:08:41 +02:00
committed by GitHub
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: Each wrapper struct can be called with an array of the appropriate enums:
```zig ```zig
const vk = @import("vulkan"); const vk = @import("vulkan");
const BaseDispatch = vk.BaseWrapper([_]vk.BaseCommand { const BaseDispatch = vk.BaseWrapper(.{
.create_instance, .CreateInstance,
}); });
``` ```
The wrapper struct then provides wrapper functions for each function pointer in the dispatch struct: 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 required_device_extensions = [_][]const u8{vk.extension_info.khr_swapchain.name};
const BaseDispatch = vk.BaseWrapper([_]vk.BaseCommand{ const BaseDispatch = vk.BaseWrapper(.{
.create_instance, .CreateInstance,
}); });
const InstanceDispatch = vk.InstanceWrapper([_]vk.InstanceCommand{ const InstanceDispatch = vk.InstanceWrapper(.{
.destroy_instance, .DestroyInstance,
.create_device, .CreateDevice,
.destroy_surface_khr, .DestroySurfaceKHR,
.enumerate_physical_devices, .EnumeratePhysicalDevices,
.get_physical_device_properties, .GetPhysicalDeviceProperties,
.enumerate_device_extension_properties, .EnumerateDeviceExtensionProperties,
.get_physical_device_surface_formats_khr, .GetPhysicalDeviceSurfaceFormatsKHR,
.get_physical_device_surface_present_modes_khr, .GetPhysicalDeviceSurfacePresentModesKHR,
.get_physical_device_surface_capabilities_khr, .GetPhysicalDeviceSurfaceCapabilitiesKHR,
.get_physical_device_queue_family_properties, .GetPhysicalDeviceQueueFamilyProperties,
.get_physical_device_surface_support_khr, .GetPhysicalDeviceSurfaceSupportKHR,
.get_physical_device_memory_properties, .GetPhysicalDeviceMemoryProperties,
.get_device_proc_addr, .GetDeviceProcAddr,
}); });
const DeviceDispatch = vk.DeviceWrapper([_]vk.DeviceCommand{ const DeviceDispatch = vk.DeviceWrapper(.{
.destroy_device, .DestroyDevice,
.get_device_queue, .GetDeviceQueue,
.create_semaphore, .CreateSemaphore,
.create_fence, .CreateFence,
.create_image_view, .CreateImageView,
.destroy_image_view, .DestroyImageView,
.destroy_semaphore, .DestroySemaphore,
.destroy_fence, .DestroyFence,
.get_swapchain_images_khr, .GetSwapchainImagesKHR,
.create_swapchain_khr, .CreateSwapchainKHR,
.destroy_swapchain_khr, .DestroySwapchainKHR,
.acquire_next_image_khr, .AcquireNextImageKHR,
.device_wait_idle, .DeviceWaitIdle,
.wait_for_fences, .WaitForFences,
.reset_fences, .ResetFences,
.queue_submit, .QueueSubmit,
.queue_present_khr, .QueuePresentKHR,
.create_command_pool, .CreateCommandPool,
.destroy_command_pool, .DestroyCommandPool,
.allocate_command_buffers, .AllocateCommandBuffers,
.free_command_buffers, .FreeCommandBuffers,
.queue_wait_idle, .QueueWaitIdle,
.create_shader_module, .CreateShaderModule,
.destroy_shader_module, .DestroyShaderModule,
.create_pipeline_layout, .CreatePipelineLayout,
.destroy_pipeline_layout, .DestroyPipelineLayout,
.create_render_pass, .CreateRenderPass,
.destroy_render_pass, .DestroyRenderPass,
.create_graphics_pipelines, .CreateGraphicsPipelines,
.destroy_pipeline, .DestroyPipeline,
.create_framebuffer, .CreateFramebuffer,
.destroy_framebuffer, .DestroyFramebuffer,
.begin_command_buffer, .BeginCommandBuffer,
.end_command_buffer, .EndCommandBuffer,
.allocate_memory, .AllocateMemory,
.free_memory, .FreeMemory,
.create_buffer, .CreateBuffer,
.destroy_buffer, .DestroyBuffer,
.get_buffer_memory_requirements, .GetBufferMemoryRequirements,
.map_memory, .MapMemory,
.unmap_memory, .UnmapMemory,
.bind_buffer_memory, .BindBufferMemory,
.cmd_begin_render_pass, .CmdBeginRenderPass,
.cmd_end_render_pass, .CmdEndRenderPass,
.cmd_bind_pipeline, .CmdBindPipeline,
.cmd_draw, .CmdDraw,
.cmd_set_viewport, .CmdSetViewport,
.cmd_set_scissor, .CmdSetScissor,
.cmd_bind_vertex_buffers, .CmdBindVertexBuffers,
.cmd_copy_buffer, .CmdCopyBuffer,
}); });
pub const GraphicsContext = struct { pub const GraphicsContext = struct {

View File

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