minimal vulkan + glfw
This commit is contained in:
25
src/c.zig
Normal file
25
src/c.zig
Normal file
@@ -0,0 +1,25 @@
|
||||
pub usingnamespace @cImport({
|
||||
@cDefine("GLFW_INCLUDE_NONE", {});
|
||||
@cInclude("GLFW/glfw3.h");
|
||||
});
|
||||
|
||||
const vk = @import("vk");
|
||||
const c = @This();
|
||||
|
||||
pub extern fn glfwGetInstanceProcAddress(
|
||||
instance: vk.Instance,
|
||||
procname: [*:0]const u8,
|
||||
) vk.PfnVoidFunction;
|
||||
|
||||
pub extern fn glfwGetPhysicalDevicePresentationSupport(
|
||||
instance: vk.Instance,
|
||||
pdev: vk.PhysicalDevice,
|
||||
queuefamily: u32,
|
||||
) c_int;
|
||||
|
||||
pub extern fn glfwCreateWindowSurface(
|
||||
instance: vk.Instance,
|
||||
window: *c.GLFWwindow,
|
||||
allocation_callbacks: ?*const vk.AllocationCallbacks,
|
||||
surface: *vk.SurfaceKHR,
|
||||
) vk.Result;
|
75
src/main.zig
Normal file
75
src/main.zig
Normal file
@@ -0,0 +1,75 @@
|
||||
const std = @import("std");
|
||||
|
||||
const c = @import("c.zig");
|
||||
|
||||
const vk = @import("vk");
|
||||
|
||||
const BaseDispatch = vk.BaseWrapper(.{
|
||||
.createInstance = true,
|
||||
.getInstanceProcAddr = true,
|
||||
});
|
||||
|
||||
const InstanceDispatch = vk.InstanceWrapper(.{
|
||||
.destroyInstance = true,
|
||||
});
|
||||
|
||||
const Context = struct {
|
||||
vkb: BaseDispatch,
|
||||
vki: InstanceDispatch,
|
||||
|
||||
instance: vk.Instance,
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, app_name: [*:0]const u8, window: *c.GLFWwindow) !Context {
|
||||
_ = allocator;
|
||||
_ = window;
|
||||
|
||||
var self: Context = undefined;
|
||||
self.vkb = try BaseDispatch.load(c.glfwGetInstanceProcAddress);
|
||||
|
||||
var glfw_exts_count: u32 = 0;
|
||||
const glfw_exts = c.glfwGetRequiredInstanceExtensions(&glfw_exts_count);
|
||||
|
||||
const app_info = vk.ApplicationInfo{
|
||||
.p_application_name = app_name,
|
||||
.application_version = vk.makeApiVersion(0, 0, 0, 0),
|
||||
.p_engine_name = app_name,
|
||||
.engine_version = vk.makeApiVersion(0, 0, 0, 0),
|
||||
.api_version = vk.API_VERSION_1_2,
|
||||
};
|
||||
|
||||
self.instance = try self.vkb.createInstance(&.{
|
||||
.p_application_info = &app_info,
|
||||
.enabled_extension_count = glfw_exts_count,
|
||||
.pp_enabled_extension_names = @as([*]const [*:0]const u8, @ptrCast(glfw_exts)),
|
||||
}, null);
|
||||
|
||||
self.vki = try InstanceDispatch.load(self.instance, self.vkb.dispatch.vkGetInstanceProcAddr);
|
||||
errdefer self.vki.destroyInstance(self.instance, null);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
pub fn deinit(self: Context) void {
|
||||
self.vki.destroyInstance(self.instance, null);
|
||||
}
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
if (c.glfwInit() != c.GLFW_TRUE) {
|
||||
return error.GlfwInitFailed;
|
||||
}
|
||||
defer c.glfwTerminate();
|
||||
|
||||
const window: *c.GLFWwindow = c.glfwCreateWindow(1280, 720, "Hello World!", null, null) orelse return error.GlfwWindowFailed;
|
||||
defer c.glfwDestroyWindow(window);
|
||||
|
||||
const ctx = try Context.init(allocator, "content", window);
|
||||
defer ctx.deinit();
|
||||
|
||||
while (c.glfwWindowShouldClose(window) == 0) : (c.glfwPollEvents()) {
|
||||
c.glfwSwapBuffers(window);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user