Skip to content

feat(gpu): adding linux-drm #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ pub fn build(b: *std.Build) void {
},
});

if (target.result.os.tag == .linux) {
const libdrm = b.dependency("libdrm", .{
.target = target,
.optimize = optimize,
});

module.addImport("libdrm", libdrm.module("libdrm"));
}

const autodoc_test = b.addObject(.{
.name = "phantom",
.root_module = module,
Expand Down
4 changes: 4 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
.url = "https://codeberg.org/ifreund/zig-wayland/archive/f3c5d503e540ada8cbcb056420de240af0c094f7.tar.gz",
.hash = "wayland-0.4.0-dev-lQa1kjfIAQCmhhQu3xF0KH-94-TzeMXOqfnP0-Dg6Wyy",
},
.libdrm = .{
.url = "https://github.com/MidstallSoftware/libdrm.zig/archive/d020314c5fcd78d64bdeecda3a42d0e43522f9af.tar.gz",
.hash = "libdrm-0.1.0-iX14LQFJAQAEHpJCxO9os_WFkU-GVD1kuLKkWnGqQRxz",
},
},
.fingerprint = 0x6896f6aeaa10b7f9,
}
3 changes: 3 additions & 0 deletions lib/phantom/gpu.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
pub const backend = @import("gpu/backend.zig");

pub const Connector = @import("gpu/Connector.zig");
pub const Device = @import("gpu/Device.zig");
pub const Provider = @import("gpu/Provider.zig");
pub const Texture = @import("gpu/Texture.zig");

test {
_ = backend;
_ = Connector;
_ = Device;
_ = Provider;
Expand Down
4 changes: 2 additions & 2 deletions lib/phantom/gpu/Device.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const Connector = @import("Connector.zig");
const Self = @This();

pub const VTable = struct {
getConnectors: *const fn (*anyopaque) anyerror![]*Connector,
getConnectors: *const fn (*anyopaque) anyerror![]const *Connector,
destroy: *const fn (*anyopaque) void,
};

ptr: *anyopaque,
vtable: *const VTable,

pub inline fn getConnectors(self: *Self) anyerror![]*Connector {
pub inline fn getConnectors(self: *Self) anyerror![]const *Connector {
return self.vtable.getConnectors(self.ptr);
}

Expand Down
9 changes: 9 additions & 0 deletions lib/phantom/gpu/backend.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const builtin = @import("builtin");

pub const linux_drm = @import("backend/linux_drm.zig");

test {
if (builtin.os.tag == .linux) {
_ = linux_drm;
}
}
11 changes: 11 additions & 0 deletions lib/phantom/gpu/backend/linux_drm.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! GPU backend for Linux using the Direct Rendering Manager subsystem.

pub const Connector = @import("linux_drm/Connector.zig");
pub const Device = @import("linux_drm/Device.zig");
pub const Provider = @import("linux_drm/Provider.zig");

test {
_ = Connector;
_ = Device;
_ = Provider;
}
Empty file.
58 changes: 58 additions & 0 deletions lib/phantom/gpu/backend/linux_drm/Device.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const libdrm = @import("libdrm");
const Connector = @import("../../Connector.zig");
const Device = @import("../../Device.zig");
const Self = @This();

allocator: Allocator,
node: libdrm.Node,
base: Device,

pub fn create(alloc: Allocator, node: libdrm.Node) !*Device {
const self = try alloc.create(Self);
errdefer alloc.destroy(self);

self.* = .{
.allocator = alloc,
.node = node,
.base = .{
.ptr = self,
.vtable = &.{
.getConnectors = impl_getConnectors,
.destroy = impl_destroy,
},
},
};
return &self.base;
}

pub fn getConnectors(self: *Self) ![]const *Connector {
var list = std.ArrayList(*Connector).init(self.allocator);
defer list.deinit();

const modeCardRes = try self.node.getModeCardRes();
defer modeCardRes.deinit(self.allocator);

if (modeCardRes.connectorIds()) |connectorIds| {
for (connectorIds) |connectorId| {
std.debug.print("{}\n", .{connectorId});
}
}
return try list.toOwnedSlice();
}

pub fn destroy(self: *Self) void {
self.node.deinit();
self.allocator.destroy(self);
}

fn impl_getConnectors(ptr: *anyopaque) anyerror![]const *Connector {
const self: *Self = @alignCast(@ptrCast(ptr));
return self.getConnectors();
}

fn impl_destroy(ptr: *anyopaque) void {
const self: *Self = @alignCast(@ptrCast(ptr));
return self.destroy();
}
81 changes: 81 additions & 0 deletions lib/phantom/gpu/backend/linux_drm/Provider.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const libdrm = @import("libdrm");
const Provider = @import("../../Provider.zig");
const Device = @import("../../Device.zig");
const LinuxDrmDevice = @import("Device.zig");
const Self = @This();

allocator: Allocator,
base: Provider,

pub fn create(alloc: Allocator) !*Provider {
const self = try alloc.create(Self);
errdefer alloc.destroy(self);

self.* = .{
.allocator = alloc,
.base = .{
.ptr = self,
.vtable = &.{
.getDevices = impl_getDevices,
.destroy = impl_destroy,
},
},
};
return &self.base;
}

pub fn getDevices(self: *Self) anyerror![]const *Device {
var list = std.ArrayList(*Device).init(self.allocator);
defer list.deinit();

var iter = libdrm.Node.Iterator.init(self.allocator, .primary);
while (iter.next()) |node| {
const device = try LinuxDrmDevice.create(self.allocator, node);
errdefer device.destroy();

try list.append(device);
}

return try list.toOwnedSlice();
}

pub fn destroy(self: *Self) void {
self.allocator.destroy(self);
}

fn impl_getDevices(ptr: *anyopaque) anyerror![]const *Device {
const self: *Self = @alignCast(@ptrCast(ptr));
return self.getDevices();
}

fn impl_destroy(ptr: *anyopaque) void {
const self: *Self = @alignCast(@ptrCast(ptr));
return self.destroy();
}

test {
const provider = try create(std.testing.allocator);
defer provider.destroy();

const devices = try provider.getDevices();
defer {
for (devices) |dev| dev.destroy();
std.testing.allocator.free(devices);
}

if (devices.len == 0) return error.SkipZigTest;

std.debug.print("{any}\n", .{devices});

for (devices) |dev| {
const connectors = dev.getConnectors() catch continue;
defer {
for (connectors) |conn| conn.destroy();
std.testing.allocator.free(connectors);
}

std.debug.print("{any}\n", .{connectors});
}
}