vulkan-tutorial 02
This commit is contained in:
@@ -4,16 +4,26 @@ const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const vk = @import("vk");
|
||||
|
||||
const BaseDispatch = vk.BaseWrapper(.{});
|
||||
const InstanceDispatch = vk.InstanceWrapper(.{});
|
||||
const BaseDispatch = vk.BaseWrapper(.{
|
||||
.createInstance = true,
|
||||
.getInstanceProcAddr = true,
|
||||
.enumerateInstanceExtensionProperties = true,
|
||||
});
|
||||
const InstanceDispatch = vk.InstanceWrapper(.{
|
||||
.destroyInstance = true,
|
||||
});
|
||||
const DeviceDispatch = vk.DeviceWrapper(.{});
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
|
||||
vkb: BaseDispatch,
|
||||
vki: InstanceDispatch,
|
||||
vkd: DeviceDispatch,
|
||||
|
||||
window: *c.GLFWwindow,
|
||||
|
||||
instance: vk.Instance,
|
||||
|
||||
pub fn mainLoop(self: Self) void {
|
||||
while (c.glfwWindowShouldClose(self.window) == 0) : (c.glfwPollEvents()) {
|
||||
c.glfwSwapBuffers(self.window);
|
||||
@@ -29,7 +39,7 @@ fn initWindow(self: *Self) !void {
|
||||
null,
|
||||
null,
|
||||
) orelse return error.glfwCreateWindowFailed;
|
||||
errdefer self.deinitWindow();
|
||||
errdefer c.glfwDestroyWindow(self.window);
|
||||
}
|
||||
|
||||
fn deinitWindow(self: Self) void {
|
||||
@@ -37,15 +47,50 @@ fn deinitWindow(self: Self) void {
|
||||
}
|
||||
|
||||
fn initVulkan(self: *Self) !void {
|
||||
_ = self;
|
||||
self.vkb = try BaseDispatch.load(&c.glfwGetInstanceProcAddress);
|
||||
|
||||
const app_info = vk.ApplicationInfo{
|
||||
.p_application_name = "Hello World",
|
||||
.application_version = vk.makeApiVersion(0, 0, 0, 0),
|
||||
.p_engine_name = "No Engine",
|
||||
.engine_version = vk.makeApiVersion(0, 0, 0, 0),
|
||||
.api_version = vk.API_VERSION_1_3,
|
||||
};
|
||||
|
||||
var glfw_ext_count: u32 = 0;
|
||||
const glfw_exts = c.glfwGetRequiredInstanceExtensions(&glfw_ext_count);
|
||||
|
||||
const create_info = vk.InstanceCreateInfo{
|
||||
.p_application_info = &app_info,
|
||||
.enabled_extension_count = glfw_ext_count,
|
||||
.pp_enabled_extension_names = @as([*]const [*:0]const u8, @ptrCast(glfw_exts)),
|
||||
.enabled_layer_count = 0,
|
||||
};
|
||||
|
||||
self.instance = try self.vkb.createInstance(&create_info, null);
|
||||
self.vki = try InstanceDispatch.load(self.instance, self.vkb.dispatch.vkGetInstanceProcAddr);
|
||||
errdefer self.vki.destroyInstance(self.instance, null);
|
||||
|
||||
var ext_count: u32 = 0;
|
||||
_ = try self.vkb.enumerateInstanceExtensionProperties(null, &ext_count, null);
|
||||
const extensions = try self.allocator.alloc(vk.ExtensionProperties, ext_count);
|
||||
defer self.allocator.free(extensions);
|
||||
_ = try self.vkb.enumerateInstanceExtensionProperties(null, &ext_count, extensions.ptr);
|
||||
|
||||
std.debug.print("available extensions:\n", .{});
|
||||
for (extensions) |ext| {
|
||||
std.debug.print("- {s}\n", .{ext.extension_name});
|
||||
}
|
||||
}
|
||||
|
||||
fn deinitVulkan(self: Self) void {
|
||||
_ = self;
|
||||
self.vki.destroyInstance(self.instance, null);
|
||||
}
|
||||
|
||||
pub fn init() !Self {
|
||||
pub fn init(allocator: std.mem.Allocator) !Self {
|
||||
var self: Self = undefined;
|
||||
self.allocator = allocator;
|
||||
|
||||
try self.initWindow();
|
||||
errdefer self.deinitWindow();
|
||||
try self.initVulkan();
|
||||
|
@@ -55,18 +55,19 @@ const Window = @import("Window.zig");
|
||||
// };
|
||||
|
||||
pub fn main() !void {
|
||||
// var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
// const allocator = gpa.allocator();
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
defer _ = gpa.detectLeaks();
|
||||
|
||||
if (c.glfwInit() != c.GLFW_TRUE) {
|
||||
return error.GlfwInitFailed;
|
||||
}
|
||||
defer c.glfwTerminate();
|
||||
|
||||
const window = try Window.init();
|
||||
const window = try Window.init(allocator);
|
||||
defer window.deinit();
|
||||
|
||||
window.mainLoop();
|
||||
// window.mainLoop();
|
||||
|
||||
// c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API);
|
||||
// const window: *c.GLFWwindow = c.glfwCreateWindow(1280, 720, "Hello World!", null, null) orelse return error.GlfwWindowFailed;
|
||||
|
Reference in New Issue
Block a user