mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-09-19 20:18:07 +00:00
This migrates all remaining uses of cImport (and addTranslateC for good measure) to using translate-c for C translation, ensuring that we are ready for when cImport is removed from the language, and also that all sources of C translation are using the same snapshot of the external package (when can then be updated when we need to fix something). A couple of notes: * A few options have been added to support the new translations, namely the ability to link libraries (passed through to linkLibrary on the Translator side) and whether or not to initialize default values (looks like cImport did this without a way to control it, but translate-c does not do it by default). * Using the new library linking option actually simplifies the process of translating a number of the C packages as we have been shipping the necessary headers for these packages already with the applicable libraries. For some of the more complex translation processes though, we still include the appropriate directories directly.
117 lines
3.3 KiB
Zig
117 lines
3.3 KiB
Zig
const Framebuffer = @This();
|
|
|
|
const std = @import("std");
|
|
const c = @import("opengl_c");
|
|
const errors = @import("errors.zig");
|
|
const glad = @import("glad.zig");
|
|
const Texture = @import("Texture.zig");
|
|
const Renderbuffer = @import("Renderbuffer.zig");
|
|
|
|
id: c.GLuint,
|
|
|
|
/// Create a single buffer.
|
|
pub fn create() !Framebuffer {
|
|
var fbo: c.GLuint = undefined;
|
|
glad.context.GenFramebuffers.?(1, &fbo);
|
|
return .{ .id = fbo };
|
|
}
|
|
|
|
pub fn destroy(v: Framebuffer) void {
|
|
glad.context.DeleteFramebuffers.?(1, &v.id);
|
|
}
|
|
|
|
pub fn bind(v: Framebuffer, target: Target) !Binding {
|
|
// The default framebuffer is documented as being zero but
|
|
// on multiple OpenGL drivers its not zero, so we grab it
|
|
// at runtime.
|
|
var current: c.GLint = undefined;
|
|
glad.context.GetIntegerv.?(c.GL_FRAMEBUFFER_BINDING, ¤t);
|
|
glad.context.BindFramebuffer.?(@intFromEnum(target), v.id);
|
|
return .{ .target = target, .previous = @intCast(current) };
|
|
}
|
|
|
|
/// Enum for possible binding targets.
|
|
pub const Target = enum(c_uint) {
|
|
framebuffer = c.GL_FRAMEBUFFER,
|
|
draw = c.GL_DRAW_FRAMEBUFFER,
|
|
read = c.GL_READ_FRAMEBUFFER,
|
|
_,
|
|
};
|
|
|
|
pub const Attachment = enum(c_uint) {
|
|
color0 = c.GL_COLOR_ATTACHMENT0,
|
|
depth = c.GL_DEPTH_ATTACHMENT,
|
|
stencil = c.GL_STENCIL_ATTACHMENT,
|
|
depth_stencil = c.GL_DEPTH_STENCIL_ATTACHMENT,
|
|
_,
|
|
};
|
|
|
|
pub const Status = enum(c_uint) {
|
|
complete = c.GL_FRAMEBUFFER_COMPLETE,
|
|
undefined = c.GL_FRAMEBUFFER_UNDEFINED,
|
|
incomplete_attachment = c.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT,
|
|
incomplete_missing_attachment = c.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT,
|
|
incomplete_draw_buffer = c.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER,
|
|
incomplete_read_buffer = c.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER,
|
|
unsupported = c.GL_FRAMEBUFFER_UNSUPPORTED,
|
|
incomplete_multisample = c.GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE,
|
|
incomplete_layer_targets = c.GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS,
|
|
_,
|
|
};
|
|
|
|
pub const Binding = struct {
|
|
target: Target,
|
|
previous: c.GLuint,
|
|
|
|
pub fn unbind(self: Binding) void {
|
|
glad.context.BindFramebuffer.?(
|
|
@intFromEnum(self.target),
|
|
self.previous,
|
|
);
|
|
}
|
|
|
|
pub fn texture2D(
|
|
self: Binding,
|
|
attachment: Attachment,
|
|
textarget: Texture.Target,
|
|
texture: Texture,
|
|
level: c.GLint,
|
|
) !void {
|
|
glad.context.FramebufferTexture2D.?(
|
|
@intFromEnum(self.target),
|
|
@intFromEnum(attachment),
|
|
@intFromEnum(textarget),
|
|
texture.id,
|
|
level,
|
|
);
|
|
try errors.getError();
|
|
}
|
|
|
|
pub fn renderbuffer(
|
|
self: Binding,
|
|
attachment: Attachment,
|
|
buffer: Renderbuffer,
|
|
) !void {
|
|
glad.context.FramebufferRenderbuffer.?(
|
|
@intFromEnum(self.target),
|
|
@intFromEnum(attachment),
|
|
c.GL_RENDERBUFFER,
|
|
buffer.id,
|
|
);
|
|
try errors.getError();
|
|
}
|
|
|
|
pub fn drawBuffers(
|
|
self: Binding,
|
|
bufs: []Attachment,
|
|
) !void {
|
|
_ = self;
|
|
glad.context.DrawBuffers.?(@intCast(bufs.len), bufs.ptr);
|
|
try errors.getError();
|
|
}
|
|
|
|
pub fn checkStatus(self: Binding) Status {
|
|
return @enumFromInt(glad.context.CheckFramebufferStatus.?(@intFromEnum(self.target)));
|
|
}
|
|
};
|