Skip to content
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

gpu-dawn: various windows support improvements #173

Merged
merged 5 commits into from
Feb 21, 2022
Merged
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
48 changes: 14 additions & 34 deletions gpu-dawn/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ pub const Options = struct {
// TODO(build-system): enable on Windows if we can cross compile Vulkan
vulkan: ?bool = null,

/// Defaults to true on Windows, Linux
// TODO(build-system): not respected at all currently
/// Defaults to true on Linux
desktop_gl: ?bool = null,

/// Defaults to true on Android, Linux, Windows, Emscripten
Expand Down Expand Up @@ -96,9 +95,9 @@ pub const Options = struct {
if (options.metal == null) options.metal = tag.isDarwin();
if (options.vulkan == null) options.vulkan = tag == .fuchsia or linux_desktop_like;

// TODO(build-system): respect these options / defaults
if (options.desktop_gl == null) options.desktop_gl = linux_desktop_like; // TODO(build-system): add windows
options.opengl_es = false;
// TODO(build-system): technically Dawn itself defaults desktop_gl to true on Windows.
if (options.desktop_gl == null) options.desktop_gl = linux_desktop_like;
options.opengl_es = false; // TODO(build-system): OpenGL ES
// if (options.opengl_es == null) options.opengl_es = tag == .windows or tag == .emscripten or target.isAndroid() or linux_desktop_like;
return options;
}
Expand Down Expand Up @@ -178,6 +177,9 @@ fn linkFromSource(b: *Builder, step: *std.build.LibExeObjStep, options: Options)
}

fn ensureSubmodules(allocator: std.mem.Allocator) !void {
if (std.process.getEnvVarOwned(allocator, "NO_ENSURE_SUBMODULES")) |no_ensure_submodules| {
if (std.mem.eql(u8, no_ensure_submodules, "true")) return;
} else |_| {}
const child = try std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", "--recursive" }, allocator);
child.cwd = thisDir();
child.stderr = std.io.getStdErr();
Expand Down Expand Up @@ -419,7 +421,7 @@ fn buildLibDawnCommon(b: *Builder, step: *std.build.LibExeObjStep, options: Opti
var cpp_flags = std.ArrayList([]const u8).init(b.allocator);
cpp_flags.appendSlice(flags.items) catch unreachable;
options.appendFlags(&cpp_flags, false, true) catch unreachable;
addCSourceFiles(b, lib, cpp_sources.items, cpp_flags.items);
lib.addCSourceFiles(cpp_sources.items, cpp_flags.items);
return lib;
}

Expand Down Expand Up @@ -454,7 +456,7 @@ fn buildLibDawnPlatform(b: *Builder, step: *std.build.LibExeObjStep, options: Op
cpp_sources.append(abs_path) catch unreachable;
}

addCSourceFiles(b, lib, cpp_sources.items, cpp_flags.items);
lib.addCSourceFiles(cpp_sources.items, cpp_flags.items);
return lib;
}

Expand Down Expand Up @@ -736,7 +738,7 @@ fn buildLibDawnNative(b: *Builder, step: *std.build.LibExeObjStep, options: Opti
var cpp_flags = std.ArrayList([]const u8).init(b.allocator);
cpp_flags.appendSlice(flags.items) catch unreachable;
options.appendFlags(&cpp_flags, false, true) catch unreachable;
addCSourceFiles(b, lib, cpp_sources.items, cpp_flags.items);
lib.addCSourceFiles(cpp_sources.items, cpp_flags.items);
return lib;
}

Expand Down Expand Up @@ -891,7 +893,7 @@ fn buildLibTint(b: *Builder, step: *std.build.LibExeObjStep, options: Options) *
var cpp_flags = std.ArrayList([]const u8).init(b.allocator);
cpp_flags.appendSlice(flags.items) catch unreachable;
options.appendFlags(&cpp_flags, false, true) catch unreachable;
addCSourceFiles(b, lib, cpp_sources.items, cpp_flags.items);
lib.addCSourceFiles(cpp_sources.items, cpp_flags.items);
return lib;
}

Expand Down Expand Up @@ -1024,6 +1026,7 @@ fn buildLibAbseilCpp(b: *Builder, step: *std.build.LibExeObjStep, options: Optio
include("libs/dawn"),
include("libs/dawn/third_party/abseil-cpp"),
}) catch unreachable;
if (target.os.tag == .windows) flags.append("-DABSL_FORCE_THREAD_IDENTITY_MODE=2") catch unreachable;

// absl
appendLangScannedSources(b, lib, options, .{
Expand Down Expand Up @@ -1160,7 +1163,7 @@ fn buildLibDawnUtils(b: *Builder, step: *std.build.LibExeObjStep, options: Optio
var cpp_flags = std.ArrayList([]const u8).init(b.allocator);
cpp_flags.appendSlice(flags.items) catch unreachable;
options.appendFlags(&cpp_flags, false, true) catch unreachable;
addCSourceFiles(b, lib, cpp_sources.items, cpp_flags.items);
lib.addCSourceFiles(cpp_sources.items, cpp_flags.items);
return lib;
}

Expand All @@ -1172,29 +1175,6 @@ fn thisDir() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}

// TODO(build-system): This and divideSources are needed to avoid Windows process creation argument
// length limits. This should probably be fixed in Zig itself, not worked around here.
fn addCSourceFiles(b: *Builder, step: *std.build.LibExeObjStep, sources: []const []const u8, flags: []const []const u8) void {
for (divideSources(b, sources) catch unreachable) |divided| step.addCSourceFiles(divided, flags);
}

fn divideSources(b: *Builder, sources: []const []const u8) ![]const []const []const u8 {
var divided = std.ArrayList([]const []const u8).init(b.allocator);
var current = std.ArrayList([]const u8).init(b.allocator);
var current_size: usize = 0;
for (sources) |src| {
if (current_size + src.len >= 30000) {
try divided.append(current.items);
current = std.ArrayList([]const u8).init(b.allocator);
current_size = 0;
}
current_size += src.len;
try current.append(src);
}
try divided.append(current.items);
return divided.items;
}

fn appendLangScannedSources(
b: *Builder,
step: *std.build.LibExeObjStep,
Expand Down Expand Up @@ -1244,7 +1224,7 @@ fn appendScannedSources(b: *Builder, step: *std.build.LibExeObjStep, args: struc
for (args.rel_dirs) |rel_dir| {
try scanSources(b, &sources, rel_dir, args.extensions, args.excluding, args.excluding_contains);
}
addCSourceFiles(b, step, sources.items, args.flags);
step.addCSourceFiles(sources.items, args.flags);
}

/// Scans rel_dir for sources ending with one of the provided extensions, excluding relative paths
Expand Down
2 changes: 1 addition & 1 deletion gpu-dawn/src/dawn/hello_triangle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpa.allocator();

const setup = try sample_utils.setup();
const setup = try sample_utils.setup(allocator);
const queue = c.wgpuDeviceGetQueue(setup.device);

var descriptor = std.mem.zeroes(c.WGPUSwapChainDescriptor);
Expand Down
17 changes: 13 additions & 4 deletions gpu-dawn/src/dawn/sample_utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,17 @@ const Setup = struct {
window: glfw.Window,
};

fn detectBackendType() c.WGPUBackendType {
if (std.os.getenv("WGPU_BACKEND")) |backend| {
fn getEnvVarOwned(allocator: std.mem.Allocator, key: []const u8) error{ OutOfMemory, InvalidUtf8 }!?[]u8 {
return std.process.getEnvVarOwned(allocator, key) catch |err| switch (err) {
error.EnvironmentVariableNotFound => @as(?[]u8, null),
else => |e| e,
};
}

fn detectBackendType(allocator: std.mem.Allocator) !c.WGPUBackendType {
const WGPU_BACKEND = try getEnvVarOwned(allocator, "WGPU_BACKEND");
if (WGPU_BACKEND) |backend| {
defer allocator.free(backend);
if (std.ascii.eqlIgnoreCase(backend, "opengl")) return c.WGPUBackendType_OpenGL;
if (std.ascii.eqlIgnoreCase(backend, "opengles")) return c.WGPUBackendType_OpenGLES;
if (std.ascii.eqlIgnoreCase(backend, "d3d11")) return c.WGPUBackendType_D3D11;
Expand Down Expand Up @@ -82,8 +91,8 @@ fn backendTypeString(t: c.WGPUBackendType) []const u8 {
};
}

pub fn setup() !Setup {
const backend_type = detectBackendType();
pub fn setup(allocator: std.mem.Allocator) !Setup {
const backend_type = try detectBackendType(allocator);
const cmd_buf_type = CmdBufType.none;

try glfw.init(.{});
Expand Down