wip: extract create_device() but do not use yet
This commit is contained in:
115
src/main.zig
115
src/main.zig
@@ -52,8 +52,10 @@ const vertices = [_]Vertex{
|
|||||||
|
|
||||||
const indices = [_]Index{ 4, 5, 6, 6, 5, 7 };
|
const indices = [_]Index{ 4, 5, 6, 6, 5, 7 };
|
||||||
|
|
||||||
|
const InstancePair = std.meta.Tuple(&.{ vk.Instance, gfx.InstanceDispatch });
|
||||||
|
|
||||||
/// note: destroy with vki.destroyInstance(instance, null)
|
/// note: destroy with vki.destroyInstance(instance, null)
|
||||||
fn create_instance(vkb: gfx.BaseDispatch) !std.meta.Tuple(&.{ vk.Instance, gfx.InstanceDispatch }) {
|
fn create_instance(vkb: gfx.BaseDispatch) !InstancePair {
|
||||||
var glfw_exts_count: u32 = 0;
|
var glfw_exts_count: u32 = 0;
|
||||||
const glfw_exts = c.glfwGetRequiredInstanceExtensions(&glfw_exts_count);
|
const glfw_exts = c.glfwGetRequiredInstanceExtensions(&glfw_exts_count);
|
||||||
|
|
||||||
@@ -98,6 +100,117 @@ fn create_window(extent: vk.Extent2D, title: [*:0]const u8) !*c.GLFWwindow {
|
|||||||
) orelse error.WindowInitFailed;
|
) orelse error.WindowInitFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DevicePair = struct {
|
||||||
|
pdev: vk.PhysicalDevice,
|
||||||
|
dev: vk.Device,
|
||||||
|
graphics: vk.Queue,
|
||||||
|
present: vk.Queue,
|
||||||
|
present_sharing_mode: vk.SharingMode,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn create_device(ally: std.mem.Allocator, instance: vk.Instance, vki: gfx.InstanceDispatch, surface: vk.SurfaceKHR) !DevicePair {
|
||||||
|
const required_device_extensions = [_][*:0]const u8{
|
||||||
|
vk.extension_info.khr_swapchain.name,
|
||||||
|
vk.extension_info.khr_dynamic_rendering.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
var pdev_count: u32 = undefined;
|
||||||
|
|
||||||
|
_ = try vki.enumeratePhysicalDevices(instance, &pdev_count, null);
|
||||||
|
const pdevs = try ally.alloc(vk.PhysicalDevice, pdev_count);
|
||||||
|
defer ally.free(pdevs);
|
||||||
|
_ = try vki.enumeratePhysicalDevices(instance, &pdev_count, pdevs);
|
||||||
|
|
||||||
|
pdev_search: for (pdevs[0..pdev_count]) |pdev| {
|
||||||
|
// const props = vki.getPhysicalDeviceProperties(pdev);
|
||||||
|
// const feats = vki.getPhysicalDeviceFeatures(pdev);
|
||||||
|
|
||||||
|
var ext_prop_count: u32 = undefined;
|
||||||
|
_ = try vki.enumerateDeviceExtensionProperties(pdev, null, &ext_prop_count, null);
|
||||||
|
const ext_props = try ally.alloc(vk.ExtensionProperties, ext_prop_count);
|
||||||
|
defer ally.free(ext_props);
|
||||||
|
_ = try vki.enumerateDeviceExtensionProperties(pdev, null, &ext_prop_count, ext_props);
|
||||||
|
|
||||||
|
for (required_device_extensions) |required| {
|
||||||
|
for (ext_props) |prop| {
|
||||||
|
if (std.mem.eql(u8, std.mem.span(required), std.mem.sliceTo(&prop.extension_name, 0))) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
continue :pdev_search;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var format_count: u32 = undefined;
|
||||||
|
_ = try vki.getPhysicalDeviceSurfaceFormatsKHR(pdev, surface, &format_count, null);
|
||||||
|
if (format_count == 0) continue :pdev_search;
|
||||||
|
|
||||||
|
var present_mode_count: u32 = undefined;
|
||||||
|
_ = try vki.getPhysicalDeviceSurfacePresentModesKHR(pdev, surface, &present_mode_count, null);
|
||||||
|
if (present_mode_count == 0) continue :pdev_search;
|
||||||
|
|
||||||
|
var fam_prop_count: u32 = undefined;
|
||||||
|
vki.getPhysicalDeviceQueueFamilyProperties(pdev, &fam_prop_count, null);
|
||||||
|
const fam_props = try ally.alloc(vk.QueueFamilyProperties, fam_prop_count);
|
||||||
|
defer ally.free(fam_props);
|
||||||
|
vki.getPhysicalDeviceQueueFamilyProperties(pdev, &fam_prop_count, fam_props);
|
||||||
|
|
||||||
|
var graphics_families = std.ArrayList(u32).init(ally);
|
||||||
|
var present_families = std.ArrayList(u32).init(ally);
|
||||||
|
|
||||||
|
for (fam_props, 0..) |fam, idx| {
|
||||||
|
if (fam.queue_flags.graphics_bit) {
|
||||||
|
graphics_families.append(idx);
|
||||||
|
}
|
||||||
|
if (try vki.getPhysicalDeviceSurfaceSupportKHR(pdev, idx, surface)) {
|
||||||
|
present_families.append(idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// only choose the same family if we really have to.
|
||||||
|
|
||||||
|
// at this point, we know this pdev can support _a_ swap chain and has required extensions. try to make a
|
||||||
|
// logical device and queues out of it.
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return error.NoSuitableDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn includes(comptime T: type, us: []const T, vs: []const T) bool {
|
||||||
|
var vidx: usize = 0;
|
||||||
|
var uidx: usize = 0;
|
||||||
|
while (uidx < us.len) {
|
||||||
|
while (vidx < vs.len) : (vidx += 1) {
|
||||||
|
if (us[uidx] == vs[vidx]) break;
|
||||||
|
vidx += 1;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
uidx += 1;
|
||||||
|
vidx += 1;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
test "includes" {
|
||||||
|
const u = &.{ 0, 1, 7 };
|
||||||
|
const v = &.{11};
|
||||||
|
const w = &.{};
|
||||||
|
const x = &.{ 0, 1, 3, 5, 7, 9, 11 };
|
||||||
|
const y = &.{2};
|
||||||
|
const z = &.{ 0, 1, 3, 5, 7, 9, 11, 12 };
|
||||||
|
|
||||||
|
const full = &.{ 0, 1, 3, 5, 7, 9, 11 };
|
||||||
|
|
||||||
|
try std.testing.expectEqual(true, includes(usize, u, full));
|
||||||
|
try std.testing.expectEqual(true, includes(usize, v, full));
|
||||||
|
try std.testing.expectEqual(true, includes(usize, w, full));
|
||||||
|
try std.testing.expectEqual(true, includes(usize, x, full));
|
||||||
|
try std.testing.expectEqual(false, includes(usize, y, full));
|
||||||
|
try std.testing.expectEqual(false, includes(usize, z, full));
|
||||||
|
}
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer _ = gpa.deinit();
|
defer _ = gpa.deinit();
|
||||||
|
Reference in New Issue
Block a user