renderer/opengl: extract cell program state to dedicated struct

This commit is contained in:
Mitchell Hashimoto
2023-11-17 08:52:34 -08:00
parent 46dd084ee9
commit fb0929a11b
18 changed files with 250 additions and 930 deletions

View File

@@ -36,7 +36,7 @@ pub const Binding = struct {
/// Sets the data of this bound buffer. The data can be any array-like
/// type. The size of the data is automatically determined based on the type.
pub inline fn setData(
pub fn setData(
b: Binding,
data: anytype,
usage: Usage,
@@ -48,7 +48,7 @@ pub const Binding = struct {
/// Sets the data of this bound buffer. The data can be any array-like
/// type. The size of the data is automatically determined based on the type.
pub inline fn setSubData(
pub fn setSubData(
b: Binding,
offset: usize,
data: anytype,
@@ -61,7 +61,7 @@ pub const Binding = struct {
/// Sets the buffer data with a null buffer that is expected to be
/// filled in the future using subData. This requires the type just so
/// we can setup the data size.
pub inline fn setDataNull(
pub fn setDataNull(
b: Binding,
comptime T: type,
usage: Usage,
@@ -71,7 +71,7 @@ pub const Binding = struct {
}
/// Same as setDataNull but lets you manually specify the buffer size.
pub inline fn setDataNullManual(
pub fn setDataNullManual(
b: Binding,
size: usize,
usage: Usage,
@@ -106,7 +106,7 @@ pub const Binding = struct {
};
}
pub inline fn enableAttribArray(_: Binding, idx: c.GLuint) !void {
pub fn enableAttribArray(_: Binding, idx: c.GLuint) !void {
glad.context.EnableVertexAttribArray.?(idx);
}
@@ -158,7 +158,7 @@ pub const Binding = struct {
try errors.getError();
}
pub inline fn attributeAdvanced(
pub fn attributeAdvanced(
_: Binding,
idx: c.GLuint,
size: c.GLint,
@@ -177,7 +177,7 @@ pub const Binding = struct {
try errors.getError();
}
pub inline fn attributeIAdvanced(
pub fn attributeIAdvanced(
_: Binding,
idx: c.GLuint,
size: c.GLint,
@@ -194,25 +194,24 @@ pub const Binding = struct {
try errors.getError();
}
pub inline fn unbind(b: *Binding) void {
pub fn unbind(b: Binding) void {
glad.context.BindBuffer.?(@intFromEnum(b.target), 0);
b.* = undefined;
}
};
/// Create a single buffer.
pub inline fn create() !Buffer {
pub fn create() !Buffer {
var vbo: c.GLuint = undefined;
glad.context.GenBuffers.?(1, &vbo);
return Buffer{ .id = vbo };
}
/// glBindBuffer
pub inline fn bind(v: Buffer, target: Target) !Binding {
pub fn bind(v: Buffer, target: Target) !Binding {
glad.context.BindBuffer.?(@intFromEnum(target), v.id);
return Binding{ .target = target };
}
pub inline fn destroy(v: Buffer) void {
pub fn destroy(v: Buffer) void {
glad.context.DeleteBuffers.?(1, &v.id);
}

View File

@@ -11,23 +11,22 @@ const glad = @import("glad.zig");
id: c.GLuint,
const Binding = struct {
pub inline fn unbind(_: Binding) void {
pub const Binding = struct {
pub fn unbind(_: Binding) void {
glad.context.UseProgram.?(0);
}
};
pub inline fn create() !Program {
pub fn create() !Program {
const id = glad.context.CreateProgram.?();
if (id == 0) try errors.mustError();
log.debug("program created id={}", .{id});
return Program{ .id = id };
return .{ .id = id };
}
/// Create a program from a vertex and fragment shader source. This will
/// compile and link the vertex and fragment shader.
pub inline fn createVF(vsrc: [:0]const u8, fsrc: [:0]const u8) !Program {
pub fn createVF(vsrc: [:0]const u8, fsrc: [:0]const u8) !Program {
const vs = try Shader.create(c.GL_VERTEX_SHADER);
try vs.setSourceAndCompile(vsrc);
defer vs.destroy();
@@ -44,12 +43,18 @@ pub inline fn createVF(vsrc: [:0]const u8, fsrc: [:0]const u8) !Program {
return p;
}
pub inline fn attachShader(p: Program, s: Shader) !void {
pub fn destroy(p: Program) void {
assert(p.id != 0);
glad.context.DeleteProgram.?(p.id);
log.debug("program destroyed id={}", .{p.id});
}
pub fn attachShader(p: Program, s: Shader) !void {
glad.context.AttachShader.?(p.id, s.id);
try errors.getError();
}
pub inline fn link(p: Program) !void {
pub fn link(p: Program) !void {
glad.context.LinkProgram.?(p.id);
// Check if linking succeeded
@@ -67,14 +72,14 @@ pub inline fn link(p: Program) !void {
return error.CompileFailed;
}
pub inline fn use(p: Program) !Binding {
pub fn use(p: Program) !Binding {
glad.context.UseProgram.?(p.id);
try errors.getError();
return Binding{};
return .{};
}
/// Requires the program is currently in use.
pub inline fn setUniform(
pub fn setUniform(
p: Program,
n: [:0]const u8,
value: anytype,
@@ -115,14 +120,8 @@ pub inline fn setUniform(
//
// NOTE(mitchellh): we can add a dynamic version that uses an allocator
// if we ever need it.
pub inline fn getInfoLog(s: Program) [512]u8 {
pub fn getInfoLog(s: Program) [512]u8 {
var msg: [512]u8 = undefined;
glad.context.GetProgramInfoLog.?(s.id, msg.len, null, &msg);
return msg;
}
pub inline fn destroy(p: Program) void {
assert(p.id != 0);
glad.context.DeleteProgram.?(p.id);
log.debug("program destroyed id={}", .{p.id});
}

View File

@@ -7,23 +7,26 @@ const errors = @import("errors.zig");
id: c.GLuint,
/// Create a single vertex array object.
pub inline fn create() !VertexArray {
pub fn create() !VertexArray {
var vao: c.GLuint = undefined;
glad.context.GenVertexArrays.?(1, &vao);
return VertexArray{ .id = vao };
}
// Unbind any active vertex array.
pub inline fn unbind() !void {
glad.context.BindVertexArray.?(0);
}
/// glBindVertexArray
pub inline fn bind(v: VertexArray) !void {
pub fn bind(v: VertexArray) !Binding {
glad.context.BindVertexArray.?(v.id);
try errors.getError();
return .{};
}
pub inline fn destroy(v: VertexArray) void {
pub fn destroy(v: VertexArray) void {
glad.context.DeleteVertexArrays.?(1, &v.id);
}
pub const Binding = struct {
pub fn unbind(self: Binding) void {
_ = self;
glad.context.BindVertexArray.?(0);
}
};