summaryrefslogtreecommitdiffstats
path: root/src/renderdoc.zig
blob: 3e9e1135b54a8e2d245343ee78f4a64dfaa2e563 (plain)
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
const options = @import("build_options");
const std = @import("std");

usingnamespace if (options.renderdoc)
    struct {
        const c = @cImport({
            @cInclude("renderdoc_app.h");
            @cInclude("dlfcn.h");
        });

        fn fix_fields(comptime T: type) type {
            const TypeId = std.builtin.TypeId;
            var ti = @typeInfo(T);

            const num_fields = ti.Struct.fields.len;
            var fields = [_]TypeId.StructField{undefined} ** num_fields;
            for (ti.Struct.fields) |field, i| {
                var field_ti = @typeInfo(field.field_type);

                switch (field_ti) {
                    .Optional => |opt| {
                        fields[i] = TypeId.StructField{
                            .name = field.name,
                            .field_type = opt.child,
                            .default_value = @as(opt.child, fields[i].default_value),
                            .is_comptime = field.is_comptime,
                            .alignment = field.alignment,
                        };
                    },
                    else => {
                        fields[i] = field;
                    },
                }
            }

            ti.Struct.fields = fields[0..];
            return @Type(.{
                .Struct = .{
                    .layout = .Extern,
                    .fields = fields,
                    .decls = [_]TypeId.Declaration{},
                    .is_tuple = false,
                },
            });
        }

        const API = struct {
            StartFrameCapture: fn (c.RENDERDOC_DevicePointer, c.RENDERDOC_WindowHandle) callconv(.C) void,
            EndFrameCapture: fn (c.RENDERDOC_DevicePointer, c.RENDERDOC_WindowHandle) callconv(.C) u32,
        };

        extern fn RENDERDOC_GetAPI(c.RENDERDOC_Version, [*c]?*c_void) callconv(.C) c_int;

        pub var api: ?API = null;

        pub fn load_api() !void {
            var tmp_api: ?*c.RENDERDOC_API_1_1_2 = null;
            if (RENDERDOC_GetAPI(.eRENDERDOC_API_Version_1_1_2, @ptrCast([*c]?*c_void, &tmp_api)) != 1) {
                return error.Unknown;
            }

            if (tmp_api) |_api| {
                var local_api: API = undefined;
                var have_all: bool = true;
                if (_api.StartFrameCapture) |fp| {
                    local_api.StartFrameCapture = fp;
                } else {
                    return error.NoStart;
                }

                if (_api.EndFrameCapture) |fp| {
                    local_api.EndFrameCapture = fp;
                } else {
                    return error.NoEnd;
                }

                api = local_api;
            } else {
                return error.ApiEmpty;
            }
        }
    }
else
    struct {
        pub const api: ?u32 = null;

        pub fn load_api() !void {
            if (false)
                return error.NonError;
        }
    };