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

Cross-compilation #476

Draft
wants to merge 1 commit into
base: main
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions samples/minimal_d3d12/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(.{
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
Empty file.
26 changes: 22 additions & 4 deletions samples/minimal_d3d12/src/minimal_d3d12.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -97,17 +99,33 @@ 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;
pso_desc.RTVFormats[0] = .R8G8B8A8_UNORM;
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(
Expand Down