git.s-ol.nu openxPriments / a61d96f
better DiligentEngine GLTF interface s-ol 2 years ago
1 changed file(s) with 32 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
00 const std = @import("std");
11 const mem = std.mem;
22
3 fn getImports(interfaces: []const []const u8) type {
3 fn getImports(headers: []const []const u8) type {
44 return @cImport({
55 @cDefine("DILIGENT_C_API", "1");
66 @cDefine("PLATFORM_LINUX", "1");
77 @cInclude("vulkan/vulkan_core.h");
8 for (interfaces) |interface| {
9 @cInclude(interface);
8 for (headers) |header| {
9 @cInclude(header);
1010 }
1111 });
1212 }
1313
14 fn generateCImport(writer: anytype, interfaces: []const []const u8) !void {
14 fn generateCImport(writer: anytype, headers: []const []const u8) !void {
1515 try writer.writeAll(
1616 \\usingnamespace @cImport({
1717 \\ @cDefine("DILIGENT_C_API", "1");
1818 \\ @cDefine("PLATFORM_LINUX", "1");
1919 \\ @cInclude("vulkan/vulkan_core.h");
2020 );
21 for (interfaces) |interface| {
22 try writer.print("\n @cInclude(\"{}\");", .{interface});
21 for (headers) |header| {
22 try writer.print("\n @cInclude(\"{}\");", .{header});
2323 }
2424 try writer.writeAll("\n});\n");
2525 }
9494 try writer.writeAll("};\n");
9595 }
9696
97 pub fn generate(comptime interfaces: []const []const u8, writer: anytype) !void {
98 const c = getImports(interfaces);
99 try generateCImport(writer, interfaces);
97 pub fn generate(
98 comptime headers: []const []const u8,
99 comptime interfaces: []const []const u8,
100 writer: anytype,
101 ) !void {
102 const c = getImports(headers);
103 try generateCImport(writer, headers);
100104
101105 inline for (interfaces) |interface| {
102106 @setEvalBranchQuota(10000);
103107
104 // e.g. "Graphics/GraphicsEngineVulkan/interface/EngineFactoryVk.h"
105 const name = comptime blk: {
106 const basename = std.fs.path.basenamePosix(interface);
107 var name_buffer = [_]u8{0} ** 256;
108 break :blk try std.fmt.bufPrint(name_buffer[0..], "I{}", .{basename[0 .. basename.len - 2]});
109 };
110
111 if (@hasDecl(c, name)) {
112 try generateWrapper(@field(c, name), name, writer);
113 }
108 try generateWrapper(@field(c, interface), interface, writer);
114109 }
115110 }
116111
117112 pub fn main() !void {
118 comptime const interfaces = [_][]const u8{
113 comptime const headers = [_][]const u8{
119114 "Graphics/GraphicsEngine/interface/DeviceContext.h",
120115 "Graphics/GraphicsEngine/interface/PipelineState.h",
121116 "Graphics/GraphicsEngine/interface/ShaderResourceVariable.h",
128123 "Graphics/GraphicsEngineVulkan/interface/SwapChainVk.h",
129124 "TextureLoader/interface/TextureLoader.h",
130125 "TextureLoader/interface/TextureUtilities.h",
131 "AssetLoader/interface/GLTFModel.h",
126 "AssetLoader/interface/GLTFLoader.h",
132127 "GLTF_PBR_Renderer/interface/GLTF_PBR_Renderer.h",
128 };
129
130 comptime const interfaces = [_][]const u8{
131 "IDeviceContext",
132 "IPipelineState",
133 "IShaderResourceVariable",
134 "IShaderResourceBinding",
135 "IPipelineResourceSignature",
136 "IBuffer",
137 "ITexture",
138 "IRenderDeviceVk",
139 "IEngineFactoryVk",
140 "ISwapChainVk",
141 "IGLTFModel",
142 "IGLTF_PBR_Renderer",
133143 };
134144
135145 const first_arg = std.os.argv[1];
136146 const output_file = try std.fs.createFileAbsolute(first_arg[0..mem.len(first_arg)], .{ .truncate = true });
137147 defer output_file.close();
138148
139 try generate(interfaces[0..], output_file.writer());
149 try generate(headers[0..], interfaces[0..], output_file.writer());
140150 }