summaryrefslogtreecommitdiff
path: root/src/renderer/opengl/Pipeline.zig
blob: c3d414ff26b5b8f05e95dd34d44aa6f3a788592f (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
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
//! Wrapper for handling render pipelines.
const Self = @This();

const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const builtin = @import("builtin");
const gl = @import("opengl");

const OpenGL = @import("../OpenGL.zig");
const Texture = @import("Texture.zig");
const Buffer = @import("buffer.zig").Buffer;

const log = std.log.scoped(.opengl);

/// Options for initializing a render pipeline.
pub const Options = struct {
    /// GLSL source of the vertex function
    vertex_fn: [:0]const u8,
    /// GLSL source of the fragment function
    fragment_fn: [:0]const u8,

    /// Vertex step function
    step_fn: StepFunction = .per_vertex,

    /// Whether to enable blending.
    blending_enabled: bool = true,

    pub const StepFunction = enum {
        constant,
        per_vertex,
        per_instance,
    };
};

program: gl.Program,

fbo: gl.Framebuffer,

vao: gl.VertexArray,

stride: usize,

blending_enabled: bool,

pub fn init(comptime VertexAttributes: ?type, opts: Options) !Self {
    // Load and compile our shaders.
    const program = try gl.Program.createVF(
        opts.vertex_fn,
        opts.fragment_fn,
    );
    errdefer program.destroy();

    const pbind = try program.use();
    defer pbind.unbind();

    const fbo = try gl.Framebuffer.create();
    errdefer fbo.destroy();
    const fbobind = try fbo.bind(.framebuffer);
    defer fbobind.unbind();

    const vao = try gl.VertexArray.create();
    errdefer vao.destroy();
    const vaobind = try vao.bind();
    defer vaobind.unbind();

    if (VertexAttributes) |VA| try autoAttribute(VA, vaobind, opts.step_fn);

    return .{
        .program = program,
        .fbo = fbo,
        .vao = vao,
        .stride = if (VertexAttributes) |VA| @sizeOf(VA) else 0,
        .blending_enabled = opts.blending_enabled,
    };
}

pub fn deinit(self: *const Self) void {
    self.program.destroy();
}

fn autoAttribute(
    T: type,
    vaobind: gl.VertexArray.Binding,
    step_fn: Options.StepFunction,
) !void {
    const divisor: gl.c.GLuint = switch (step_fn) {
        .per_vertex => 0,
        .per_instance => 1,
        .constant => std.math.maxInt(gl.c.GLuint),
    };

    inline for (@typeInfo(T).@"struct".fields, 0..) |field, i| {
        try vaobind.enableAttribArray(i);
        try vaobind.attributeBinding(i, 0);
        try vaobind.bindingDivisor(i, divisor);

        const offset = @offsetOf(T, field.name);

        const FT = switch (@typeInfo(field.type)) {
            .@"struct" => |s| s.backing_integer.?,
            .@"enum" => |e| e.tag_type,
            else => field.type,
        };

        const size, const IT = switch (@typeInfo(FT)) {
            .array => |a| .{ a.len, a.child },
            else => .{ 1, FT },
        };

        try switch (IT) {
            u8 => vaobind.attributeIFormat(
                i,
                size,
                gl.c.GL_UNSIGNED_BYTE,
                offset,
            ),
            u16 => vaobind.attributeIFormat(
                i,
                size,
                gl.c.GL_UNSIGNED_SHORT,
                offset,
            ),
            u32 => vaobind.attributeIFormat(
                i,
                size,
                gl.c.GL_UNSIGNED_INT,
                offset,
            ),
            i8 => vaobind.attributeIFormat(
                i,
                size,
                gl.c.GL_BYTE,
                offset,
            ),
            i16 => vaobind.attributeIFormat(
                i,
                size,
                gl.c.GL_SHORT,
                offset,
            ),
            i32 => vaobind.attributeIFormat(
                i,
                size,
                gl.c.GL_INT,
                offset,
            ),
            f16 => vaobind.attributeFormat(
                i,
                size,
                gl.c.GL_HALF_FLOAT,
                false,
                offset,
            ),
            f32 => vaobind.attributeFormat(
                i,
                size,
                gl.c.GL_FLOAT,
                false,
                offset,
            ),
            f64 => vaobind.attributeLFormat(
                i,
                size,
                offset,
            ),
            else => unreachable,
        };
    }
}