diff --git a/gpu-dawn/build.zig b/gpu-dawn/build.zig index e75459311e..70a43f0fe5 100644 --- a/gpu-dawn/build.zig +++ b/gpu-dawn/build.zig @@ -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 @@ -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; } @@ -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(); @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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, .{ @@ -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; } @@ -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, @@ -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 diff --git a/gpu-dawn/src/dawn/hello_triangle.zig b/gpu-dawn/src/dawn/hello_triangle.zig index 74b5237003..4a48886419 100644 --- a/gpu-dawn/src/dawn/hello_triangle.zig +++ b/gpu-dawn/src/dawn/hello_triangle.zig @@ -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); diff --git a/gpu-dawn/src/dawn/sample_utils.zig b/gpu-dawn/src/dawn/sample_utils.zig index 73aad500f8..8cf58753af 100644 --- a/gpu-dawn/src/dawn/sample_utils.zig +++ b/gpu-dawn/src/dawn/sample_utils.zig @@ -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; @@ -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(.{});