Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pkg/opengl/Buffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ pub const Target = enum(c_uint) {
array = c.GL_ARRAY_BUFFER,
element_array = c.GL_ELEMENT_ARRAY_BUFFER,
uniform = c.GL_UNIFORM_BUFFER,
storage = c.GL_SHADER_STORAGE_BUFFER,
_,
};

Expand Down
14 changes: 14 additions & 0 deletions pkg/opengl/Texture.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub const InternalFormat = enum(c_int) {
red = c.GL_RED,
rgb = c.GL_RGB8,
rgba = c.GL_RGBA8,
r32ui = c.GL_R32UI,

srgb = c.GL_SRGB8,
srgba = c.GL_SRGB8_ALPHA8,
Expand Down Expand Up @@ -147,6 +148,19 @@ pub const Binding = struct {
try errors.getError();
}

pub fn buffer(
b: Binding,
internal_format: InternalFormat,
id: c.GLuint,
) errors.Error!void {
glad.context.TexBuffer.?(
@intFromEnum(b.target),
@intCast(@intFromEnum(internal_format)),
id,
);
try errors.getError();
}

pub fn image2D(
b: Binding,
level: c.GLint,
Expand Down
1 change: 0 additions & 1 deletion src/renderer/Metal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ pub inline fn bufferOptions(self: Metal) bufferpkg.Options {
};
}

pub const instanceBufferOptions = bufferOptions;
pub const uniformBufferOptions = bufferOptions;
pub const fgBufferOptions = bufferOptions;
pub const bgBufferOptions = bufferOptions;
Expand Down
1 change: 0 additions & 1 deletion src/renderer/OpenGL.zig
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ pub inline fn bufferOptions(self: OpenGL) bufferpkg.Options {
};
}

pub const instanceBufferOptions = bufferOptions;
pub const uniformBufferOptions = bufferOptions;
pub const fgBufferOptions = bufferOptions;
pub const bgBufferOptions = bufferOptions;
Expand Down
1 change: 0 additions & 1 deletion src/renderer/generic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,6 @@ pub fn Renderer(comptime GraphicsAPI: type) type {
pass.step(.{
.pipeline = self.shaders.pipelines.bg_color,
.uniforms = frame.uniforms.buffer,
.buffers = &.{ null, frame.cells_bg.buffer },
.draw = .{ .type = .triangle, .vertex_count = 3 },
});
}
Expand Down
32 changes: 32 additions & 0 deletions src/renderer/opengl/Pipeline.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,27 @@ pub const Options = struct {
/// Whether to enable blending.
blending_enabled: bool = true,

/// Texture buffer binding for `buffers[1]`.
buffer_texture: ?BufferTexture = null,

pub const BufferTexture = struct {
unit: gl.c.GLuint,
internal_format: gl.Texture.InternalFormat,
};

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

const BufferTextureBinding = struct {
texture: gl.Texture,
unit: gl.c.GLuint,
internal_format: gl.Texture.InternalFormat,
};

program: gl.Program,

fbo: gl.Framebuffer,
Expand All @@ -37,6 +51,8 @@ stride: usize,

blending_enabled: bool,

buffer_texture: ?BufferTextureBinding = null,

pub fn init(comptime VertexAttributes: ?type, opts: Options) !Self {
// Load and compile our shaders.
const program = try gl.Program.createVF(
Expand All @@ -60,16 +76,32 @@ pub fn init(comptime VertexAttributes: ?type, opts: Options) !Self {

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

const buffer_texture: ?BufferTextureBinding = if (opts.buffer_texture) |cfg| texture: {
const texture = try gl.Texture.create();
errdefer texture.destroy();

break :texture .{
.texture = texture,
.unit = cfg.unit,
.internal_format = cfg.internal_format,
};
} else null;

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

pub fn deinit(self: *const Self) void {
if (self.buffer_texture) |tbo| {
tbo.texture.destroy();
}

self.program.destroy();
}

Expand Down
15 changes: 8 additions & 7 deletions src/renderer/opengl/RenderPass.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const Sampler = @import("Sampler.zig");
const Target = @import("Target.zig");
const Texture = @import("Texture.zig");
const Pipeline = @import("Pipeline.zig");
const Buffer = @import("buffer.zig").Buffer;

/// Options for beginning a render pass.
pub const Options = struct {
Expand Down Expand Up @@ -101,24 +100,26 @@ pub fn step(self: *Self, s: Step) void {
_ = tex.texture.bind(tex.target) catch return;
};

// Bind `buffers[1]` as a texture buffer if this pipeline uses one.
if (s.pipeline.buffer_texture) |tbo| {
gl.Texture.active(tbo.unit) catch return;
const tbind = tbo.texture.bind(.Buffer) catch return;
tbind.buffer(tbo.internal_format, s.buffers[1].?.id) catch return;
}

// Bind relevant samplers.
for (s.samplers, 0..) |s_, i| if (s_) |sampler| {
_ = sampler.sampler.bind(@intCast(i)) catch return;
};

// Bind 0th buffer as the vertex buffer,
// and bind the rest as storage buffers.
// Bind the 0th buffer as the vertex buffer.
if (s.buffers.len > 0) {
if (s.buffers[0]) |vbo| vaobind.bindVertexBuffer(
0,
vbo.id,
0,
@intCast(s.pipeline.stride),
) catch return;

for (s.buffers[1..], 1..) |b, i| if (b) |buf| {
_ = buf.bindBase(.storage, @intCast(i)) catch return;
};
}

if (s.pipeline.blending_enabled) {
Expand Down
12 changes: 11 additions & 1 deletion src/renderer/opengl/shaders.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ const pipeline_descs: []const struct { [:0]const u8, PipelineDescription } =
.vertex_fn = loadShaderCode("../shaders/glsl/full_screen.v.glsl"),
.fragment_fn = loadShaderCode("../shaders/glsl/cell_bg.f.glsl"),
.blending_enabled = true,
.buffer_texture = .{
.unit = 2,
.internal_format = .r32ui,
},
} },
.{ "cell_text", .{
.vertex_attributes = CellText,
.vertex_fn = loadShaderCode("../shaders/glsl/cell_text.v.glsl"),
.fragment_fn = loadShaderCode("../shaders/glsl/cell_text.f.glsl"),
.step_fn = .per_instance,
.blending_enabled = true,
.buffer_texture = .{
.unit = 2,
.internal_format = .r32ui,
},
} },
.{ "image", .{
.vertex_attributes = Image,
Expand All @@ -50,13 +58,15 @@ const PipelineDescription = struct {
fragment_fn: [:0]const u8,
step_fn: Pipeline.Options.StepFunction = .per_vertex,
blending_enabled: bool = true,
buffer_texture: ?Pipeline.Options.BufferTexture = null,

fn initPipeline(self: PipelineDescription) !Pipeline {
return try .init(self.vertex_attributes, .{
.vertex_fn = self.vertex_fn,
.fragment_fn = self.fragment_fn,
.step_fn = self.step_fn,
.blending_enabled = self.blending_enabled,
.buffer_texture = self.buffer_texture,
});
}
};
Expand All @@ -81,7 +91,7 @@ const PipelineCollection = t: {
} });
};

/// This contains the state for the shaders used by the Metal renderer.
/// This contains the state for the shaders used by the OpenGL renderer.
pub const Shaders = struct {
/// Collection of available render pipelines.
pipelines: PipelineCollection,
Expand Down
13 changes: 6 additions & 7 deletions src/renderer/shaders/glsl/cell_bg.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ layout(origin_upper_left) in vec4 gl_FragCoord;
// Must declare this output for some versions of OpenGL.
layout(location = 0) out vec4 out_FragColor;

layout(binding = 1, std430) readonly buffer bg_cells {
uint cells[];
};
layout(binding = 2) uniform usamplerBuffer cells;

vec4 cell_bg() {
uvec2 grid_size = unpack2u16(grid_size_packed_2u16);
Expand Down Expand Up @@ -48,10 +46,11 @@ vec4 cell_bg() {
}

// Load the color for the cell.
vec4 cell_color = load_color(
unpack4u8(cells[grid_pos.y * grid_size.x + grid_pos.x]),
use_linear_blending
);
uint cell = texelFetch(
cells,
grid_pos.y * int(grid_size.x) + grid_pos.x
).r;
vec4 cell_color = load_color(unpack4u8(cell), use_linear_blending);

return cell_color;
}
Expand Down
13 changes: 6 additions & 7 deletions src/renderer/shaders/glsl/cell_text.v.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ out CellTextVertexOut {
vec2 tex_coord;
} out_data;

layout(binding = 1, std430) readonly buffer bg_cells {
uint bg_colors[];
};
layout(binding = 2) uniform usamplerBuffer bg_colors;

void main() {
uvec2 grid_size = unpack2u16(grid_size_packed_2u16);
Expand Down Expand Up @@ -116,10 +114,11 @@ void main() {
// make it easier to handle minimum contrast calculations.
out_data.color = load_color(color, true);
// Get the BG color
out_data.bg_color = load_color(
unpack4u8(bg_colors[grid_pos.y * grid_size.x + grid_pos.x]),
true
);
uint bg_color = texelFetch(
bg_colors,
int(grid_pos.y * grid_size.x + grid_pos.x)
).r;
out_data.bg_color = load_color(unpack4u8(bg_color), true);
// Blend it with the global bg color
vec4 global_bg = load_color(
unpack4u8(bg_color_packed_4u8),
Expand Down