git.s-ol.nu openxPriments / 2bbdf50
render using OpenXR transform s-ol 4 months ago
4 changed file(s) with 94 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
66 [submodule "lib/DiligentEngine"]
77 path = lib/DiligentEngine
88 url = https://github.com/DiligentGraphics/DiligentEngine.git
9 [submodule "lib/glm"]
10 path = lib/glm
11 url = https://github.com/ziglang-contrib/glm.git
1616 exe.linkSystemLibrary("vulkan");
1717 exe.linkSystemLibrary("openxr_loader");
1818 exe.linkSystemLibrary("GraphicsEngineVk");
19
20 exe.addPackagePath("glm", "lib/glm/glm.zig");
1921
2022 const gen_dg = dggen.DgGenerateStep.init(b, "lib/DiligentEngine", "dg.zig");
2123 gen_dg.addDeps(exe, "Debug");
0 const std = @import("std");
1 const vkgen = @import("lib/vulkan/generator/index.zig");
2 const xrgen = @import("lib/openxr/generator/index.zig");
3 const dggen = @import("lib/diligent/index.zig");
4 const Step = std.build.Step;
5 const Builder = std.build.Builder;
6
7 pub fn build(b: *Builder) void {
8 const target = b.standardTargetOptions(.{});
9 const mode = b.standardReleaseOptions();
10
11 const exe = b.addExecutable("openxPriments", "src/main.zig");
12 exe.setTarget(target);
13 exe.setBuildMode(mode);
14 exe.install();
15 exe.linkSystemLibrary("c");
16 exe.linkSystemLibrary("vulkan");
17 exe.linkSystemLibrary("openxr_loader");
18 exe.linkSystemLibrary("GraphicsEngineVk");
19
20 exe.addPackagePath("glm", "lib/glm/glm.zig");
21
22 const gen_dg = dggen.DgGenerateStep.init(b, "lib/DiligentEngine", "dg.zig");
23 gen_dg.addDeps(exe, "Debug");
24
25 const vk_xml_path = b.option([]const u8, "vulkan-registry", "Override the path to the Vulkan registry") orelse "res/vk.xml";
26 const gen_vk = vkgen.VkGenerateStep.init(b, vk_xml_path, "vk.zig");
27 exe.step.dependOn(&gen_vk.step);
28 exe.addPackage(gen_vk.package);
29
30 const xr_xml_path = b.option([]const u8, "openxr-registry", "Override the path to the OpenXR registry") orelse "res/xr.xml";
31 const gen_xr = xrgen.XrGenerateStep.init(b, xr_xml_path, "xr.zig");
32 const dep_list = [_]std.build.Pkg{gen_vk.package};
33 gen_xr.package.dependencies = dep_list[0..];
34 exe.step.dependOn(&gen_xr.step);
35 exe.addPackage(gen_xr.package);
36
37 const run_cmd = exe.run();
38 run_cmd.step.dependOn(b.getInstallStep());
39 const run_step = b.step("run", "Run the example example");
40 run_step.dependOn(&run_cmd.step);
41
42 const renderdoc = b.option(bool, "renderdoc", "Enable Renderdoc debugging") orelse false;
43 exe.addBuildOption(bool, "renderdoc", renderdoc);
44 if (renderdoc) {
45 exe.linkSystemLibrary("renderdoc");
46 run_cmd.setEnvironmentVariable("VK_INSTANCE_LAYERS", "VK_LAYER_RENDERDOC_Capture");
47 }
48 }
00 const std = @import("std");
11 const dg = @import("diligent");
2 const glm = @import("glm");
23 const graphics = @import("graphics.zig");
34 usingnamespace @import("xrvk.zig");
45
78 \\
89 \\void main() {
910 \\ vec4 positions[3] = vec4[3](
10 \\ vec4(-0.5, -0.5, 0.0, 1.0),
11 \\ vec4(0.0, 0.5, 0.0, 1.0),
12 \\ vec4(0.5, -0.5, 0.0, 1.0)
11 \\ vec4(-0.5, 1, -2.0, 1.0),
12 \\ vec4( 0.0, 2, -2.0, 1.0),
13 \\ vec4( 0.5, 1, -2.0, 1.0)
1314 \\ );
1415 \\ vec3 colors[3] = vec3[3](
1516 \\ vec3(1.0, 0.0, 0.0),
6465 \\}
6566 ;
6667
68 fn vec2vec(v: xr.Vector3f) glm.Vec3 {
69 return glm.Vec3.init([_]f32{ v.x, v.y, v.z });
70 }
71
72 fn quat2mat(q: xr.Quaternionf) glm.Mat4 {
73 const x = q.x;
74 const y = q.y;
75 const z = q.z;
76 const w = q.w;
77 const x2 = q.x * q.x;
78 const y2 = q.y * q.y;
79 const z2 = q.z * q.z;
80
81 // zig fmt: off
82 return glm.Mat4.init([_][4]f32{
83 [_]f32{ 1 - 2 * y2 - 2 * z2, 2 * x * y - 2 * z * w, 2 * x * z + 2 * y * w, 0 },
84 [_]f32{ 2 * x * y + 2 * z * w, 1 - 2 * x2 - 2 * z2, 2 * y * z - 2 * x * w, 0 },
85 [_]f32{ 2 * x * z - 2 * y * w, 2 * y * z + 2 * x * w, 1 - 2 * x2 - 2 * y2, 0 },
86 [_]f32{ 0, 0, 0, 1 },
87 });
88 // zig fmt: on
89 }
90
6791 pub const Scene = struct {
6892 viewports: []dg.Viewport,
6993 scissors: []dg.Rect,
78102 swc: dg.ISwapChainVk_Wrapper,
79103
80104 const ViewUBO = packed struct {
81 projection: [16]f32,
82 modelview: [16]f32,
105 projection: glm.Mat4,
106 modelview: glm.Mat4,
83107 };
84108
85109 pub fn init(gfx: *const graphics.Graphics) !Scene {
116140 };
117141
118142 self.views[i] = .{
119 .projection = [_]f32{
120 1, 0, 0, 0,
121 0, 1, 0, 0,
122 0, 0, 1, 0,
123 0, 0, 0, 1,
124 },
125 .modelview = [_]f32{
126 1, 0, 0, 0,
127 0, 1, 0, 0,
128 0, 0, 1, 0,
129 0, 0, 0, 1,
130 },
143 .projection = glm.Mat4.IDENTITY,
144 .modelview = glm.Mat4.IDENTITY,
131145 };
132146 }
133147
281295
282296 pub fn render(self: *const Scene, views: []const xr.View) !void {
283297 // update uniform buffer
284 for (views) |view, i| {
285 self.views[i].modelview[3] = view.pose.position.x / 3.0;
298 for (self.views) |*view, i| {
299 const fov = views[i].fov;
300 const viewport = self.viewports[i];
301 const fovY = fov.angle_up - fov.angle_down;
302 const aspect = viewport.Width / viewport.Height;
303 view.projection = glm.perspective(fovY, aspect, 0.1, 100);
304
305 const pose = views[i].pose;
306 view.modelview = quat2mat(pose.orientation).mul(glm.translation(vec2vec(pose.position).mulScalar(-1)));
286307 }
287308 try self.updateBufferSlice(ViewUBO, self.views_ubo, self.views);
288309