diff --git a/samples/minimal_d3d12/build.zig b/samples/minimal_d3d12/build.zig index d07075f64..ee5d37ed3 100644 --- a/samples/minimal_d3d12/build.zig +++ b/samples/minimal_d3d12/build.zig @@ -2,6 +2,7 @@ const builtin = @import("builtin"); const std = @import("std"); const Options = @import("../../build.zig").Options; +const content_dir = "minimal_d3d12_content/"; pub fn build(b: *std.Build, options: Options) *std.Build.Step.Compile { const exe = b.addExecutable(.{ @@ -18,10 +19,23 @@ pub fn build(b: *std.Build, options: Options) *std.Build.Step.Compile { }); exe.root_module.addImport("zwin32", zwin32.module("root")); + const exe_options = b.addOptions(); + exe.root_module.addOptions("build_options", exe_options); + exe_options.addOption([]const u8, "content_dir", content_dir); + + const install_content_step = b.addInstallDirectory(.{ + .source_dir = .{ .path = thisDir() ++ "/" ++ content_dir }, + .install_dir = .{ .custom = "" }, + .install_subdir = "bin/" ++ content_dir, + }); if (builtin.os.tag == .windows or builtin.os.tag == .linux) { - const dxc_step = buildShaders(b); + const dxc_step = buildShaders( + b, + ); exe.step.dependOn(dxc_step); + install_content_step.step.dependOn(dxc_step); } + exe.step.dependOn(&install_content_step.step); // This is needed to export symbols from an .exe file. // We export D3D12SDKVersion and D3D12SDKPath symbols which @@ -37,8 +51,8 @@ pub fn build(b: *std.Build, options: Options) *std.Build.Step.Compile { fn buildShaders(b: *std.Build) *std.Build.Step { const dxc_step = b.step("minimal_d3d12-dxc", "Build shaders for 'minimal d3d12' demo"); - makeDxcCmd(b, dxc_step, "src/minimal_d3d12.hlsl", "vsMain", "minimal_d3d12.vs.cso", "vs", ""); - makeDxcCmd(b, dxc_step, "src/minimal_d3d12.hlsl", "psMain", "minimal_d3d12.ps.cso", "ps", ""); + makeDxcCmd(b, dxc_step, "src/minimal_d3d12.hlsl", "vsMain", "../" ++ content_dir ++ "minimal_d3d12.vs.cso", "vs", ""); + makeDxcCmd(b, dxc_step, "src/minimal_d3d12.hlsl", "psMain", "../" ++ content_dir ++ "minimal_d3d12.ps.cso", "ps", ""); return dxc_step; } diff --git a/samples/minimal_d3d12/minimal_d3d12_content/.gitkeep b/samples/minimal_d3d12/minimal_d3d12_content/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/samples/minimal_d3d12/src/minimal_d3d12.zig b/samples/minimal_d3d12/src/minimal_d3d12.zig index b63ed443e..fa5d45096 100644 --- a/samples/minimal_d3d12/src/minimal_d3d12.zig +++ b/samples/minimal_d3d12/src/minimal_d3d12.zig @@ -86,6 +86,8 @@ fn createWindow(width: u32, height: u32) w32.HWND { } pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + _ = w32.CoInitializeEx(null, w32.COINIT_MULTITHREADED); defer w32.CoUninitialize(); @@ -97,8 +99,24 @@ pub fn main() !void { defer dx12.deinit(); const root_signature: *d3d12.IRootSignature, const pipeline: *d3d12.IPipelineState = blk: { - const vs_cso = @embedFile("./minimal_d3d12.vs.cso"); - const ps_cso = @embedFile("./minimal_d3d12.ps.cso"); + var arena = std.heap.ArenaAllocator.init(gpa.allocator()); + defer arena.deinit(); + + const self_exe_dir_path = std.fs.selfExeDirPathAlloc(arena.allocator()) catch unreachable; + + const ps_cso = readVsCode: { + const abspath = std.fs.path.join(arena.allocator(), &.{ self_exe_dir_path, "/minimal_d3d12.vs.cso" }) catch unreachable; + const vs_file = std.fs.openFileAbsolute(abspath, .{}) catch unreachable; + defer vs_file.close(); + break :readVsCode vs_file.reader().readAllAlloc(arena.allocator(), 256 * 1024) catch unreachable; + }; + + const vs_cso = readPsCode: { + const abspath = std.fs.path.join(arena.allocator(), &.{ self_exe_dir_path, "/minimal_d3d12.ps.cso" }) catch unreachable; + const ps_file = std.fs.openFileAbsolute(abspath, .{}) catch unreachable; + defer ps_file.close(); + break :readPsCode ps_file.reader().readAllAlloc(arena.allocator(), 256 * 1024) catch unreachable; + }; var pso_desc = d3d12.GRAPHICS_PIPELINE_STATE_DESC.initDefault(); pso_desc.DepthStencilState.DepthEnable = w32.FALSE; @@ -106,8 +124,8 @@ pub fn main() !void { pso_desc.NumRenderTargets = 1; pso_desc.BlendState.RenderTarget[0].RenderTargetWriteMask = 0xf; pso_desc.PrimitiveTopologyType = .TRIANGLE; - pso_desc.VS = .{ .pShaderBytecode = vs_cso, .BytecodeLength = vs_cso.len }; - pso_desc.PS = .{ .pShaderBytecode = ps_cso, .BytecodeLength = ps_cso.len }; + pso_desc.VS = .{ .pShaderBytecode = vs_cso.ptr, .BytecodeLength = vs_cso.len }; + pso_desc.PS = .{ .pShaderBytecode = ps_cso.ptr, .BytecodeLength = ps_cso.len }; var root_signature: *d3d12.IRootSignature = undefined; hrPanicOnFail(dx12.device.CreateRootSignature(