-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
238 lines (202 loc) · 8.32 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (c) [email protected]
const std = @import("std");
// 1. for each vertex/fragment shader file invoke
// glslc --target-env=vulkan1.2 <input file> -o <input file>.spv
//
// 2. for each generated .spv file i want to invoke
// spirv-cross --reflect <input file>.spv --output <input file>.json
//
// 3. compile the reflect program, and invoke it on the json file.
pub const SpirvGenerator = struct {
steps: std.ArrayList(*std.Build.Step),
exe: *std.Build.Step.Compile,
b: *std.Build,
step: *std.Build.Step,
repoPath: []const u8,
installed: std.StringHashMap(bool),
glslTypes: *std.build.Module,
pub fn init(
b: *std.Build,
opts: struct {
target: std.zig.CrossTarget,
optimize: std.builtin.Mode,
repoPath: []const u8 = ".",
addInstallStep: bool = true,
},
) @This() {
var mainPath = b.fmt("{s}/main.zig", .{opts.repoPath});
var glslTypesPath = b.fmt("{s}/glslTypes.zig", .{opts.repoPath});
const exe = b.addExecutable(.{
.name = "spirv-reflect-zig",
.root_source_file = .{ .path = mainPath },
.target = opts.target,
.optimize = opts.optimize,
});
var self = @This(){
.b = b,
.steps = std.ArrayList(*std.Build.Step).init(b.allocator),
.exe = exe,
.step = b.step("spirv", "compiles all glsl files into spirv binaries and generates .zig types"),
.repoPath = opts.repoPath,
.glslTypes = b.addModule("glslTypes", .{
.source_file = .{ .path = glslTypesPath },
}),
.installed = std.StringHashMap(bool).init(b.allocator),
};
self.step.dependOn(&exe.step);
if (opts.addInstallStep) {
b.installArtifact(exe);
}
return self;
}
fn compileAndReflectGlsl(
self: *@This(),
b: *std.Build,
options: struct {
source_file: std.Build.FileSource,
output_name: []const u8,
shaderCompilerCommand: []const []const u8, // default this is glslc --target-env=vulkan1.2
shaderCompilerOutputFlag: []const u8, // in default, this is -o
},
) struct {
spv_out: std.Build.FileSource,
json_out: std.Build.FileSource,
step: *std.Build.Step,
glslcCompileStep: *std.Build.Step,
installGlslc: *std.Build.Step,
} {
const finalSpv = b.fmt("shaders/{s}.spv", .{options.output_name});
const finalJson = b.fmt("shaders/{s}.json", .{options.output_name});
//const compileStep = b.addSystemCommand(&[_][]const u8{ "glslc", "--target-env=vulkan1.2" });
const compileStep = b.addSystemCommand(options.shaderCompilerCommand);
compileStep.addFileSourceArg(options.source_file);
compileStep.addArg(options.shaderCompilerOutputFlag);
const spvOutputFile = compileStep.addOutputFileArg(finalSpv);
const jsonReflectStep = b.addSystemCommand(&[_][]const u8{"spirv-cross"});
jsonReflectStep.addFileSourceArg(spvOutputFile);
jsonReflectStep.addArg("--reflect");
jsonReflectStep.addArg("--output");
const outputJson = jsonReflectStep.addOutputFileArg(finalJson);
var reflect = b.allocator.create(std.Build.Step) catch unreachable;
reflect.* = std.Build.Step.init(.{ .id = .custom, .name = options.output_name, .owner = b, .makeFn = make });
// reflect.dependOn(&b.addInstallFile(spvOutputFile, finalSpv).step);
// reflect.dependOn(&b.addInstallFile(outputJson, finalJson).step);
reflect.dependOn(&jsonReflectStep.step);
if (self.installed.get(finalSpv)) |exists| {
_ = exists;
} else {
b.getInstallStep().dependOn(&b.addInstallFile(spvOutputFile, finalSpv).step);
self.installed.put(finalSpv, true) catch unreachable;
}
return .{
.json_out = outputJson,
.spv_out = spvOutputFile,
.step = reflect,
.glslcCompileStep = &compileStep.step,
.installGlslc = &b.addInstallFile(spvOutputFile, finalSpv).step,
};
}
fn make(_: *std.Build.Step, _: *std.Progress.Node) !void {
// just a no-op, not entirely sure how to make a custom step without
}
pub fn addShader(
self: *@This(),
options: struct {
exe: *std.build.CompileStep,
sourceFile: std.Build.FileSource,
shaderName: []const u8,
shaderCompilerCommand: []const []const u8,
shaderCompilerOutputFlag: []const u8,
embedFile: bool = false,
},
) void {
var results = self.compileAndReflectGlsl(self.b, .{
.source_file = options.sourceFile,
.output_name = options.shaderName,
.shaderCompilerCommand = options.shaderCompilerCommand,
.shaderCompilerOutputFlag = options.shaderCompilerOutputFlag,
});
var b = self.b;
var outputFile = b.fmt("reflectedTypes/{s}.zig", .{options.shaderName});
const run_cmd = b.addRunArtifact(self.exe);
run_cmd.addFileSourceArg(results.json_out);
run_cmd.addArg("-o");
const outputZigFile = run_cmd.addOutputFileArg(outputFile);
if (options.embedFile) {
var spvFile = b.fmt("{s}.spv", .{options.shaderName});
run_cmd.addArg("-e");
run_cmd.addArg(spvFile);
}
run_cmd.step.dependOn(&self.exe.step);
self.step.dependOn(&run_cmd.step);
self.step.dependOn(results.step);
self.step.dependOn(&b.addInstallFile(outputZigFile, outputFile).step);
var generatedFileRef = b.allocator.create(std.Build.GeneratedFile) catch unreachable;
generatedFileRef.* = .{
.step = self.step,
.path = b.fmt("zig-out/{s}", .{outputFile}),
};
var dependencies = self.b.allocator.alloc(std.build.ModuleDependency, 1) catch unreachable;
dependencies[0] = .{ .name = "glslTypes", .module = self.glslTypes };
var module = b.addModule(options.shaderName, .{
.source_file = outputZigFile,
.dependencies = dependencies,
});
options.exe.addModule(options.shaderName, module);
options.exe.step.dependOn(&run_cmd.step);
options.exe.step.dependOn(results.step);
options.exe.step.dependOn(results.installGlslc);
}
pub fn shader(
self: *@This(),
exe: *std.build.CompileStep,
source: []const u8,
shaderName: []const u8,
opts: struct {
shaderCompilerCommand: []const []const u8 = &.{ "glslc", "--target-env=vulkan1.2" },
shaderCompilerOutputFlag: []const u8 = "-o",
embedFile: bool = false,
},
) void {
self.addShader(.{
.exe = exe,
.sourceFile = .{ .path = source },
.shaderName = shaderName,
.shaderCompilerCommand = opts.shaderCompilerCommand,
.shaderCompilerOutputFlag = opts.shaderCompilerOutputFlag,
.embedFile = opts.embedFile,
});
}
};
// Example build function.
//
// Run zig build run to run the example
//
// zig build install to generate the CLI tool.
pub fn build(b: *std.Build) void {
var target = b.standardTargetOptions(.{});
var optimize = b.standardOptimizeOption(.{});
// ==== create the spirv compiler and generate both .spv files and .zig files ====
var spirvCompile = SpirvGenerator.init(b, .{
.target = target,
.optimize = optimize,
.repoPath = "src",
});
// This returns a module which contains the reflected.zig file which correct
// data layout
var test_vk = spirvCompile.shader("shaders/test_vk.vert", "test_vk", .{ .embedFile = true });
// ===============================================================================
// Create your executables as you normally would
const exe = b.addExecutable(.{
.name = "example",
.root_source_file = .{ .path = "example.zig" },
.target = target,
.optimize = optimize,
});
exe.addModule("test_vk", test_vk);
b.installArtifact(exe);
var run_step = b.step("run", "runs my program");
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
run_step.dependOn(&run_cmd.step);
}