diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-04-04 19:22:04 +0300 |
|---|---|---|
| committer | Can Celik <ogulcancelik@gmail.com> | 2026-04-05 01:23:06 +0300 |
| commit | 41dd733ca7123c9ac4bfc6c781697a660e95b052 (patch) | |
| tree | 3fc91815ca139ef2e0b281bba0abda4ef15175f4 /vendor/libghostty-vt/pkg/opengl | |
| parent | 55c138263e190a1e79d4b997bfb9397306c0ec1f (diff) | |
feat: finish ghostty pane backend integration
Diffstat (limited to 'vendor/libghostty-vt/pkg/opengl')
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/Buffer.zig | 245 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/Framebuffer.zig | 116 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/Program.zig | 137 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/Renderbuffer.zig | 56 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/Sampler.zig | 43 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/Shader.zig | 56 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/Texture.zig | 221 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/VertexArray.zig | 116 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/build.zig | 6 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/c.zig | 3 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/draw.zig | 94 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/errors.zig | 33 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/extensions.zig | 32 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/glad.zig | 45 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/main.zig | 45 | ||||
| -rw-r--r-- | vendor/libghostty-vt/pkg/opengl/primitives.zig | 18 |
16 files changed, 1266 insertions, 0 deletions
diff --git a/vendor/libghostty-vt/pkg/opengl/Buffer.zig b/vendor/libghostty-vt/pkg/opengl/Buffer.zig new file mode 100644 index 00000000..60934295 --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/Buffer.zig @@ -0,0 +1,245 @@ +const Buffer = @This(); + +const std = @import("std"); +const c = @import("c.zig").c; +const errors = @import("errors.zig"); +const glad = @import("glad.zig"); + +id: c.GLuint, + +/// Create a single buffer. +pub fn create() !Buffer { + var vbo: c.GLuint = undefined; + glad.context.GenBuffers.?(1, &vbo); + return Buffer{ .id = vbo }; +} + +/// glBindBuffer +pub fn bind(self: Buffer, target: Target) !Binding { + glad.context.BindBuffer.?(@intFromEnum(target), self.id); + return Binding{ .id = self.id, .target = target }; +} + +pub fn destroy(self: Buffer) void { + glad.context.DeleteBuffers.?(1, &self.id); +} + +pub fn bindBase(self: Buffer, target: Target, idx: c.GLuint) !void { + glad.context.BindBufferBase.?( + @intFromEnum(target), + idx, + self.id, + ); + try errors.getError(); +} + +/// Binding is a bound buffer. By using this for functions that operate +/// on bound buffers, you can easily defer unbinding and in safety-enabled +/// modes verify that unbound buffers are never accessed. +pub const Binding = struct { + id: c.GLuint, + target: Target, + + pub fn unbind(b: Binding) void { + glad.context.BindBuffer.?(@intFromEnum(b.target), 0); + } + + /// 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 fn setData( + b: Binding, + data: anytype, + usage: Usage, + ) !void { + const info = dataInfo(data); + glad.context.BufferData.?( + @intFromEnum(b.target), + info.size, + info.ptr, + @intFromEnum(usage), + ); + try errors.getError(); + } + + /// 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 fn setSubData( + b: Binding, + offset: usize, + data: anytype, + ) !void { + const info = dataInfo(data); + glad.context.BufferSubData.?( + @intFromEnum(b.target), + @intCast(offset), + info.size, + info.ptr, + ); + try errors.getError(); + } + + /// 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 fn setDataNull( + b: Binding, + comptime T: type, + usage: Usage, + ) !void { + glad.context.BufferData.?( + @intFromEnum(b.target), + @sizeOf(T), + null, + @intFromEnum(usage), + ); + try errors.getError(); + } + + /// Same as setDataNull but lets you manually specify the buffer size. + pub fn setDataNullManual( + b: Binding, + size: usize, + usage: Usage, + ) !void { + glad.context.BufferData.?( + @intFromEnum(b.target), + @intCast(size), + null, + @intFromEnum(usage), + ); + try errors.getError(); + } + + fn dataInfo(data: anytype) struct { + size: isize, + ptr: *const anyopaque, + } { + return switch (@typeInfo(@TypeOf(data))) { + .pointer => |ptr| switch (ptr.size) { + .one => .{ + .size = @sizeOf(ptr.child), + .ptr = data, + }, + .slice => .{ + .size = @intCast(@sizeOf(ptr.child) * data.len), + .ptr = data.ptr, + }, + else => { + std.log.err("invalid buffer data pointer size: {}", .{ptr.size}); + unreachable; + }, + }, + else => { + std.log.err("invalid buffer data type: {s}", .{@tagName(@typeInfo(@TypeOf(data)))}); + unreachable; + }, + }; + } + + /// Shorthand for vertexAttribPointer that is specialized towards the + /// common use case of specifying an array of homogeneous types that + /// don't need normalization. This also enables the attribute at idx. + pub fn attribute( + b: Binding, + idx: c.GLuint, + size: c.GLint, + comptime T: type, + offset: usize, + ) !void { + const info: struct { + // Type of the each component in the array. + typ: c.GLenum, + + // The byte offset between each full set of attributes. + stride: c.GLsizei, + + // The size of each component used in calculating the offset. + offset: usize, + } = switch (@typeInfo(T)) { + .Array => |ary| .{ + .typ = switch (ary.child) { + f32 => c.GL_FLOAT, + else => @compileError("unsupported array child type"), + }, + .offset = @sizeOf(ary.child), + .stride = @sizeOf(T), + }, + else => @compileError("unsupported type"), + }; + + try b.attributeAdvanced( + idx, + size, + info.typ, + false, + info.stride, + offset * info.offset, + ); + try b.enableAttribArray(idx); + } + + /// VertexAttribDivisor + pub fn attributeDivisor(_: Binding, idx: c.GLuint, divisor: c.GLuint) !void { + glad.context.VertexAttribDivisor.?(idx, divisor); + try errors.getError(); + } + + pub fn attributeAdvanced( + _: Binding, + idx: c.GLuint, + size: c.GLint, + typ: c.GLenum, + normalized: bool, + stride: c.GLsizei, + offset: usize, + ) !void { + const normalized_c: c.GLboolean = if (normalized) c.GL_TRUE else c.GL_FALSE; + const offsetPtr = if (offset > 0) + @as(*const anyopaque, @ptrFromInt(offset)) + else + null; + + glad.context.VertexAttribPointer.?(idx, size, typ, normalized_c, stride, offsetPtr); + try errors.getError(); + } + + pub fn attributeIAdvanced( + _: Binding, + idx: c.GLuint, + size: c.GLint, + typ: c.GLenum, + stride: c.GLsizei, + offset: usize, + ) !void { + const offsetPtr = if (offset > 0) + @as(*const anyopaque, @ptrFromInt(offset)) + else + null; + + glad.context.VertexAttribIPointer.?(idx, size, typ, stride, offsetPtr); + try errors.getError(); + } +}; + +/// Enum for possible binding targets. +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, + _, +}; + +/// Enum for possible buffer usages. +pub const Usage = enum(c_uint) { + stream_draw = c.GL_STREAM_DRAW, + stream_read = c.GL_STREAM_READ, + stream_copy = c.GL_STREAM_COPY, + static_draw = c.GL_STATIC_DRAW, + static_read = c.GL_STATIC_READ, + static_copy = c.GL_STATIC_COPY, + dynamic_draw = c.GL_DYNAMIC_DRAW, + dynamic_read = c.GL_DYNAMIC_READ, + dynamic_copy = c.GL_DYNAMIC_COPY, + _, +}; diff --git a/vendor/libghostty-vt/pkg/opengl/Framebuffer.zig b/vendor/libghostty-vt/pkg/opengl/Framebuffer.zig new file mode 100644 index 00000000..ea1f0d2b --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/Framebuffer.zig @@ -0,0 +1,116 @@ +const Framebuffer = @This(); + +const std = @import("std"); +const c = @import("c.zig").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))); + } +}; diff --git a/vendor/libghostty-vt/pkg/opengl/Program.zig b/vendor/libghostty-vt/pkg/opengl/Program.zig new file mode 100644 index 00000000..ee77582c --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/Program.zig @@ -0,0 +1,137 @@ +const Program = @This(); + +const std = @import("std"); +const assert = std.debug.assert; +const log = std.log.scoped(.opengl); + +const c = @import("c.zig").c; +const Shader = @import("Shader.zig"); +const errors = @import("errors.zig"); +const glad = @import("glad.zig"); + +id: c.GLuint, + +pub const Binding = struct { + pub fn unbind(_: Binding) void { + glad.context.UseProgram.?(0); + } +}; + +pub fn create() !Program { + const id = glad.context.CreateProgram.?(); + if (id == 0) try errors.mustError(); + log.debug("program created 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 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(); + + const fs = try Shader.create(c.GL_FRAGMENT_SHADER); + try fs.setSourceAndCompile(fsrc); + defer fs.destroy(); + + const p = try create(); + try p.attachShader(vs); + try p.attachShader(fs); + try p.link(); + + return p; +} + +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 fn link(p: Program) !void { + glad.context.LinkProgram.?(p.id); + + // Check if linking succeeded + var success: c_int = undefined; + glad.context.GetProgramiv.?(p.id, c.GL_LINK_STATUS, &success); + if (success == c.GL_TRUE) { + log.debug("program linked id={}", .{p.id}); + return; + } + + log.err("program link failure id={} message={s}", .{ + p.id, + std.mem.sliceTo(&p.getInfoLog(), 0), + }); + return error.CompileFailed; +} + +pub fn use(p: Program) !Binding { + glad.context.UseProgram.?(p.id); + try errors.getError(); + return .{}; +} + +pub fn uniformBlockBinding( + self: Program, + index: c.GLuint, + binding: c.GLuint, +) !void { + glad.context.UniformBlockBinding.?(self.id, index, binding); + try errors.getError(); +} + +/// Requires the program is currently in use. +pub fn setUniform( + p: Program, + n: [:0]const u8, + value: anytype, +) !void { + const loc = glad.context.GetUniformLocation.?( + p.id, + @ptrCast(n.ptr), + ); + if (loc < 0) { + return error.UniformNameInvalid; + } + try errors.getError(); + + // Perform the correct call depending on the type of the value. + switch (@TypeOf(value)) { + bool => glad.context.Uniform1i.?(loc, if (value) 1 else 0), + comptime_int => glad.context.Uniform1i.?(loc, value), + f32 => glad.context.Uniform1f.?(loc, value), + @Vector(2, f32) => glad.context.Uniform2f.?(loc, value[0], value[1]), + @Vector(3, f32) => glad.context.Uniform3f.?(loc, value[0], value[1], value[2]), + @Vector(4, f32) => glad.context.Uniform4f.?(loc, value[0], value[1], value[2], value[3]), + [4]@Vector(4, f32) => glad.context.UniformMatrix4fv.?( + loc, + 1, + c.GL_FALSE, + @ptrCast(&value), + ), + else => { + log.warn("unsupported uniform type {}", .{@TypeOf(value)}); + unreachable; + }, + } + try errors.getError(); +} + +/// getInfoLog returns the info log for this program. This attempts to +/// keep the log fully stack allocated and is therefore limited to a max +/// amount of elements. +// +// NOTE(mitchellh): we can add a dynamic version that uses an allocator +// if we ever need it. +pub fn getInfoLog(s: Program) [512]u8 { + var msg: [512]u8 = undefined; + glad.context.GetProgramInfoLog.?(s.id, msg.len, null, &msg); + return msg; +} diff --git a/vendor/libghostty-vt/pkg/opengl/Renderbuffer.zig b/vendor/libghostty-vt/pkg/opengl/Renderbuffer.zig new file mode 100644 index 00000000..ef21287f --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/Renderbuffer.zig @@ -0,0 +1,56 @@ +const Renderbuffer = @This(); + +const std = @import("std"); +const c = @import("c.zig").c; +const errors = @import("errors.zig"); +const glad = @import("glad.zig"); + +const Texture = @import("Texture.zig"); + +id: c.GLuint, + +/// Create a single buffer. +pub fn create() !Renderbuffer { + var rbo: c.GLuint = undefined; + glad.context.GenRenderbuffers.?(1, &rbo); + return .{ .id = rbo }; +} + +pub fn destroy(v: Renderbuffer) void { + glad.context.DeleteRenderbuffers.?(1, &v.id); +} + +pub fn bind(v: Renderbuffer) !Binding { + // Keep track of the previous binding so we can restore it in unbind. + var current: c.GLint = undefined; + glad.context.GetIntegerv.?(c.GL_RENDERBUFFER_BINDING, ¤t); + glad.context.BindRenderbuffer.?(c.GL_RENDERBUFFER, v.id); + return .{ .previous = @intCast(current) }; +} + +pub const Binding = struct { + previous: c.GLuint, + + pub fn unbind(self: Binding) void { + glad.context.BindRenderbuffer.?( + c.GL_RENDERBUFFER, + self.previous, + ); + } + + pub fn storage( + self: Binding, + format: Texture.InternalFormat, + width: c.GLsizei, + height: c.GLsizei, + ) !void { + _ = self; + glad.context.RenderbufferStorage.?( + c.GL_RENDERBUFFER, + @intCast(@intFromEnum(format)), + width, + height, + ); + try errors.getError(); + } +}; diff --git a/vendor/libghostty-vt/pkg/opengl/Sampler.zig b/vendor/libghostty-vt/pkg/opengl/Sampler.zig new file mode 100644 index 00000000..f5c15699 --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/Sampler.zig @@ -0,0 +1,43 @@ +const Sampler = @This(); + +const std = @import("std"); +const c = @import("c.zig").c; +const errors = @import("errors.zig"); +const glad = @import("glad.zig"); +const Texture = @import("Texture.zig"); + +id: c.GLuint, + +/// Create a single sampler. +pub fn create() errors.Error!Sampler { + var id: c.GLuint = undefined; + glad.context.GenSamplers.?(1, &id); + try errors.getError(); + return .{ .id = id }; +} + +/// glBindSampler +pub fn bind(v: Sampler, index: c_uint) !void { + glad.context.BindSampler.?(index, v.id); + try errors.getError(); +} + +pub fn parameter( + self: Sampler, + name: Texture.Parameter, + value: anytype, +) errors.Error!void { + switch (@TypeOf(value)) { + c.GLint => glad.context.SamplerParameteri.?( + self.id, + @intFromEnum(name), + value, + ), + else => unreachable, + } + try errors.getError(); +} + +pub fn destroy(v: Sampler) void { + glad.context.DeleteSamplers.?(1, &v.id); +} diff --git a/vendor/libghostty-vt/pkg/opengl/Shader.zig b/vendor/libghostty-vt/pkg/opengl/Shader.zig new file mode 100644 index 00000000..8973ace3 --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/Shader.zig @@ -0,0 +1,56 @@ +const Shader = @This(); + +const std = @import("std"); +const assert = std.debug.assert; +const log = std.log.scoped(.opengl); + +const c = @import("c.zig").c; +const errors = @import("errors.zig"); +const glad = @import("glad.zig"); + +id: c.GLuint, + +pub inline fn create(typ: c.GLenum) errors.Error!Shader { + const id = glad.context.CreateShader.?(typ); + if (id == 0) { + try errors.mustError(); + unreachable; + } + + log.debug("shader created id={}", .{id}); + return Shader{ .id = id }; +} + +/// Set the source and compile a shader. +pub inline fn setSourceAndCompile(s: Shader, source: [:0]const u8) !void { + glad.context.ShaderSource.?(s.id, 1, &@as([*c]const u8, @ptrCast(source)), null); + glad.context.CompileShader.?(s.id); + + // Check if compilation succeeded + var success: c_int = undefined; + glad.context.GetShaderiv.?(s.id, c.GL_COMPILE_STATUS, &success); + if (success == c.GL_TRUE) return; + log.err("shader compilation failure id={} message={s}", .{ + s.id, + std.mem.sliceTo(&s.getInfoLog(), 0), + }); + return error.CompileFailed; +} + +/// getInfoLog returns the info log for this shader. This attempts to +/// keep the log fully stack allocated and is therefore limited to a max +/// amount of elements. +// +// NOTE(mitchellh): we can add a dynamic version that uses an allocator +// if we ever need it. +pub inline fn getInfoLog(s: Shader) [512]u8 { + var msg: [512]u8 = undefined; + glad.context.GetShaderInfoLog.?(s.id, msg.len, null, &msg); + return msg; +} + +pub inline fn destroy(s: Shader) void { + assert(s.id != 0); + glad.context.DeleteShader.?(s.id); + log.debug("shader destroyed id={}", .{s.id}); +} diff --git a/vendor/libghostty-vt/pkg/opengl/Texture.zig b/vendor/libghostty-vt/pkg/opengl/Texture.zig new file mode 100644 index 00000000..03e79485 --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/Texture.zig @@ -0,0 +1,221 @@ +const Texture = @This(); + +const std = @import("std"); +const c = @import("c.zig").c; +const errors = @import("errors.zig"); +const glad = @import("glad.zig"); + +id: c.GLuint, + +pub fn active(index: c_uint) errors.Error!void { + glad.context.ActiveTexture.?(index + c.GL_TEXTURE0); + try errors.getError(); +} + +/// Create a single texture. +pub fn create() errors.Error!Texture { + var id: c.GLuint = undefined; + glad.context.GenTextures.?(1, &id); + try errors.getError(); + return .{ .id = id }; +} + +/// glBindTexture +pub fn bind(v: Texture, target: Target) !Binding { + glad.context.BindTexture.?(@intFromEnum(target), v.id); + try errors.getError(); + return .{ .target = target }; +} + +pub fn destroy(v: Texture) void { + glad.context.DeleteTextures.?(1, &v.id); +} + +/// Enum for possible texture binding targets. +pub const Target = enum(c_uint) { + @"1D" = c.GL_TEXTURE_1D, + @"2D" = c.GL_TEXTURE_2D, + @"3D" = c.GL_TEXTURE_3D, + @"1DArray" = c.GL_TEXTURE_1D_ARRAY, + @"2DArray" = c.GL_TEXTURE_2D_ARRAY, + Rectangle = c.GL_TEXTURE_RECTANGLE, + CubeMap = c.GL_TEXTURE_CUBE_MAP, + Buffer = c.GL_TEXTURE_BUFFER, + @"2DMultisample" = c.GL_TEXTURE_2D_MULTISAMPLE, + @"2DMultisampleArray" = c.GL_TEXTURE_2D_MULTISAMPLE_ARRAY, +}; + +/// Enum for possible texture parameters. +pub const Parameter = enum(c_uint) { + BaseLevel = c.GL_TEXTURE_BASE_LEVEL, + CompareFunc = c.GL_TEXTURE_COMPARE_FUNC, + CompareMode = c.GL_TEXTURE_COMPARE_MODE, + LodBias = c.GL_TEXTURE_LOD_BIAS, + MinFilter = c.GL_TEXTURE_MIN_FILTER, + MagFilter = c.GL_TEXTURE_MAG_FILTER, + MinLod = c.GL_TEXTURE_MIN_LOD, + MaxLod = c.GL_TEXTURE_MAX_LOD, + MaxLevel = c.GL_TEXTURE_MAX_LEVEL, + SwizzleR = c.GL_TEXTURE_SWIZZLE_R, + SwizzleG = c.GL_TEXTURE_SWIZZLE_G, + SwizzleB = c.GL_TEXTURE_SWIZZLE_B, + SwizzleA = c.GL_TEXTURE_SWIZZLE_A, + WrapS = c.GL_TEXTURE_WRAP_S, + WrapT = c.GL_TEXTURE_WRAP_T, + WrapR = c.GL_TEXTURE_WRAP_R, +}; + +/// Internal format enum for texture images. +pub const InternalFormat = enum(c_int) { + red = c.GL_RED, + rgb = c.GL_RGB8, + rgba = c.GL_RGBA8, + + srgb = c.GL_SRGB8, + srgba = c.GL_SRGB8_ALPHA8, + + rgba_compressed = c.GL_COMPRESSED_RGBA_BPTC_UNORM, + srgba_compressed = c.GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM, + + // There are so many more that I haven't filled in. + _, +}; + +/// Format for texture images +pub const Format = enum(c_uint) { + red = c.GL_RED, + rgb = c.GL_RGB, + rgba = c.GL_RGBA, + bgra = c.GL_BGRA, + + // There are so many more that I haven't filled in. + _, +}; + +/// Minification filter for textures. +pub const MinFilter = enum(c_int) { + nearest = c.GL_NEAREST, + linear = c.GL_LINEAR, + nearest_mipmap_nearest = c.GL_NEAREST_MIPMAP_NEAREST, + linear_mipmap_nearest = c.GL_LINEAR_MIPMAP_NEAREST, + nearest_mipmap_linear = c.GL_NEAREST_MIPMAP_LINEAR, + linear_mipmap_linear = c.GL_LINEAR_MIPMAP_LINEAR, +}; + +/// Magnification filter for textures. +pub const MagFilter = enum(c_int) { + nearest = c.GL_NEAREST, + linear = c.GL_LINEAR, +}; + +/// Texture coordinate wrapping mode. +pub const Wrap = enum(c_int) { + clamp_to_edge = c.GL_CLAMP_TO_EDGE, + clamp_to_border = c.GL_CLAMP_TO_BORDER, + mirrored_repeat = c.GL_MIRRORED_REPEAT, + repeat = c.GL_REPEAT, +}; + +/// Data type for texture images. +pub const DataType = enum(c_uint) { + UnsignedByte = c.GL_UNSIGNED_BYTE, + + // There are so many more that I haven't filled in. + _, +}; + +pub const Binding = struct { + target: Target, + + pub fn unbind(b: *const Binding) void { + glad.context.BindTexture.?(@intFromEnum(b.target), 0); + } + + pub fn generateMipmap(b: Binding) void { + glad.context.GenerateMipmap.?(@intFromEnum(b.target)); + } + + pub fn parameter(b: Binding, name: Parameter, value: anytype) errors.Error!void { + switch (@TypeOf(value)) { + c.GLint => glad.context.TexParameteri.?( + @intFromEnum(b.target), + @intFromEnum(name), + value, + ), + else => unreachable, + } + try errors.getError(); + } + + pub fn image2D( + b: Binding, + level: c.GLint, + internal_format: InternalFormat, + width: c.GLsizei, + height: c.GLsizei, + format: Format, + typ: DataType, + data: ?*const anyopaque, + ) errors.Error!void { + glad.context.TexImage2D.?( + @intFromEnum(b.target), + level, + @intFromEnum(internal_format), + width, + height, + 0, + @intFromEnum(format), + @intFromEnum(typ), + data, + ); + try errors.getError(); + } + + pub fn subImage2D( + b: Binding, + level: c.GLint, + xoffset: c.GLint, + yoffset: c.GLint, + width: c.GLsizei, + height: c.GLsizei, + format: Format, + typ: DataType, + data: ?*const anyopaque, + ) errors.Error!void { + glad.context.TexSubImage2D.?( + @intFromEnum(b.target), + level, + xoffset, + yoffset, + width, + height, + @intFromEnum(format), + @intFromEnum(typ), + data, + ); + try errors.getError(); + } + + pub fn copySubImage2D( + b: Binding, + level: c.GLint, + xoffset: c.GLint, + yoffset: c.GLint, + x: c.GLint, + y: c.GLint, + width: c.GLsizei, + height: c.GLsizei, + ) errors.Error!void { + glad.context.CopyTexSubImage2D.?( + @intFromEnum(b.target), + level, + xoffset, + yoffset, + x, + y, + width, + height, + ); + try errors.getError(); + } +}; diff --git a/vendor/libghostty-vt/pkg/opengl/VertexArray.zig b/vendor/libghostty-vt/pkg/opengl/VertexArray.zig new file mode 100644 index 00000000..44bf3162 --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/VertexArray.zig @@ -0,0 +1,116 @@ +const VertexArray = @This(); + +const c = @import("c.zig").c; +const glad = @import("glad.zig"); +const errors = @import("errors.zig"); + +id: c.GLuint, + +/// Create a single vertex array object. +pub fn create() !VertexArray { + var vao: c.GLuint = undefined; + glad.context.GenVertexArrays.?(1, &vao); + return VertexArray{ .id = vao }; +} + +/// glBindVertexArray +pub fn bind(v: VertexArray) !Binding { + glad.context.BindVertexArray.?(v.id); + try errors.getError(); + return .{}; +} + +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); + } + + pub fn enableAttribArray(_: Binding, idx: c.GLuint) !void { + glad.context.EnableVertexAttribArray.?(idx); + try errors.getError(); + } + + pub fn bindingDivisor(_: Binding, idx: c.GLuint, divisor: c.GLuint) !void { + glad.context.VertexBindingDivisor.?(idx, divisor); + try errors.getError(); + } + + pub fn attributeBinding( + _: Binding, + attrib_idx: c.GLuint, + binding_idx: c.GLuint, + ) !void { + glad.context.VertexAttribBinding.?(attrib_idx, binding_idx); + try errors.getError(); + } + + pub fn attributeFormat( + _: Binding, + idx: c.GLuint, + size: c.GLint, + typ: c.GLenum, + normalized: bool, + offset: c.GLuint, + ) !void { + glad.context.VertexAttribFormat.?( + idx, + size, + typ, + @intCast(@intFromBool(normalized)), + offset, + ); + try errors.getError(); + } + + pub fn attributeIFormat( + _: Binding, + idx: c.GLuint, + size: c.GLint, + typ: c.GLenum, + offset: c.GLuint, + ) !void { + glad.context.VertexAttribIFormat.?( + idx, + size, + typ, + offset, + ); + try errors.getError(); + } + + pub fn attributeLFormat( + _: Binding, + idx: c.GLuint, + size: c.GLint, + offset: c.GLuint, + ) !void { + glad.context.VertexAttribLFormat.?( + idx, + size, + c.GL_DOUBLE, + offset, + ); + try errors.getError(); + } + + pub fn bindVertexBuffer( + _: Binding, + idx: c.GLuint, + buffer: c.GLuint, + offset: c.GLintptr, + stride: c.GLsizei, + ) !void { + glad.context.BindVertexBuffer.?( + idx, + buffer, + offset, + stride, + ); + try errors.getError(); + } +}; diff --git a/vendor/libghostty-vt/pkg/opengl/build.zig b/vendor/libghostty-vt/pkg/opengl/build.zig new file mode 100644 index 00000000..f472053e --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/build.zig @@ -0,0 +1,6 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) !void { + const module = b.addModule("opengl", .{ .root_source_file = b.path("main.zig") }); + module.addIncludePath(b.path("../../vendor/glad/include")); +} diff --git a/vendor/libghostty-vt/pkg/opengl/c.zig b/vendor/libghostty-vt/pkg/opengl/c.zig new file mode 100644 index 00000000..fc95ee94 --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/c.zig @@ -0,0 +1,3 @@ +pub const c = @cImport({ + @cInclude("glad/gl.h"); +}); diff --git a/vendor/libghostty-vt/pkg/opengl/draw.zig b/vendor/libghostty-vt/pkg/opengl/draw.zig new file mode 100644 index 00000000..50110f60 --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/draw.zig @@ -0,0 +1,94 @@ +const c = @import("c.zig").c; +const errors = @import("errors.zig"); +const glad = @import("glad.zig"); +const Primitive = @import("primitives.zig").Primitive; + +pub fn clearColor(r: f32, g: f32, b: f32, a: f32) void { + glad.context.ClearColor.?(r, g, b, a); +} + +pub fn clear(mask: c.GLbitfield) void { + glad.context.Clear.?(mask); +} + +pub fn drawArrays(mode: c.GLenum, first: c.GLint, count: c.GLsizei) !void { + glad.context.DrawArrays.?(mode, first, count); + try errors.getError(); +} + +pub fn drawArraysInstanced( + mode: Primitive, + first: c.GLint, + count: c.GLsizei, + primcount: c.GLsizei, +) !void { + glad.context.DrawArraysInstanced.?( + @intCast(@intFromEnum(mode)), + first, + count, + primcount, + ); + try errors.getError(); +} + +pub fn drawElements(mode: c.GLenum, count: c.GLsizei, typ: c.GLenum, offset: usize) !void { + const offsetPtr = if (offset == 0) null else @as(*const anyopaque, @ptrFromInt(offset)); + glad.context.DrawElements.?(mode, count, typ, offsetPtr); + try errors.getError(); +} + +pub fn drawElementsInstanced( + mode: c.GLenum, + count: c.GLsizei, + typ: c.GLenum, + primcount: c.GLsizei, +) !void { + glad.context.DrawElementsInstanced.?( + mode, + count, + typ, + null, + primcount, + ); + try errors.getError(); +} + +pub fn enable(cap: c.GLenum) !void { + glad.context.Enable.?(cap); + try errors.getError(); +} + +pub fn disable(cap: c.GLenum) !void { + glad.context.Disable.?(cap); + try errors.getError(); +} + +pub fn frontFace(mode: c.GLenum) !void { + glad.context.FrontFace.?(mode); + try errors.getError(); +} + +pub fn blendFunc(sfactor: c.GLenum, dfactor: c.GLenum) !void { + glad.context.BlendFunc.?(sfactor, dfactor); + try errors.getError(); +} + +pub fn viewport(x: c.GLint, y: c.GLint, width: c.GLsizei, height: c.GLsizei) !void { + glad.context.Viewport.?(x, y, width, height); +} + +pub fn pixelStore(mode: c.GLenum, value: anytype) !void { + switch (@typeInfo(@TypeOf(value))) { + .ComptimeInt, .Int => glad.context.PixelStorei.?(mode, value), + else => unreachable, + } + try errors.getError(); +} + +pub fn finish() void { + glad.context.Finish.?(); +} + +pub fn flush() void { + glad.context.Flush.?(); +} diff --git a/vendor/libghostty-vt/pkg/opengl/errors.zig b/vendor/libghostty-vt/pkg/opengl/errors.zig new file mode 100644 index 00000000..ec046460 --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/errors.zig @@ -0,0 +1,33 @@ +const std = @import("std"); +const c = @import("c.zig").c; +const glad = @import("glad.zig"); + +pub const Error = error{ + InvalidEnum, + InvalidValue, + InvalidOperation, + InvalidFramebufferOperation, + OutOfMemory, + + Unknown, +}; + +/// getError returns the error (if any) from the last OpenGL operation. +pub fn getError() Error!void { + return switch (glad.context.GetError.?()) { + c.GL_NO_ERROR => {}, + c.GL_INVALID_ENUM => Error.InvalidEnum, + c.GL_INVALID_VALUE => Error.InvalidValue, + c.GL_INVALID_OPERATION => Error.InvalidOperation, + c.GL_INVALID_FRAMEBUFFER_OPERATION => Error.InvalidFramebufferOperation, + c.GL_OUT_OF_MEMORY => Error.OutOfMemory, + else => Error.Unknown, + }; +} + +/// mustError just calls getError but always results in an error being returned. +/// If getError has no error, then Unknown is returned. +pub fn mustError() Error!void { + try getError(); + return Error.Unknown; +} diff --git a/vendor/libghostty-vt/pkg/opengl/extensions.zig b/vendor/libghostty-vt/pkg/opengl/extensions.zig new file mode 100644 index 00000000..5fdbada9 --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/extensions.zig @@ -0,0 +1,32 @@ +const std = @import("std"); +const c = @import("c.zig").c; +const errors = @import("errors.zig"); +const glad = @import("glad.zig"); + +/// Returns the number of extensions. +pub fn len() !u32 { + var n: c.GLint = undefined; + glad.context.GetIntegerv.?(c.GL_NUM_EXTENSIONS, &n); + try errors.getError(); + return @intCast(n); +} + +/// Returns an iterator for the extensions. +pub fn iterator() !Iterator { + return Iterator{ .len = try len() }; +} + +/// Iterator for the available extensions. +pub const Iterator = struct { + /// The total number of extensions. + len: c.GLuint = 0, + i: c.GLuint = 0, + + pub fn next(self: *Iterator) !?[]const u8 { + if (self.i >= self.len) return null; + const res = glad.context.GetStringi.?(c.GL_EXTENSIONS, self.i); + try errors.getError(); + self.i += 1; + return std.mem.sliceTo(res, 0); + } +}; diff --git a/vendor/libghostty-vt/pkg/opengl/glad.zig b/vendor/libghostty-vt/pkg/opengl/glad.zig new file mode 100644 index 00000000..663e75e1 --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/glad.zig @@ -0,0 +1,45 @@ +const std = @import("std"); +const c = @import("c.zig").c; + +pub const Context = c.GladGLContext; + +/// This is the current context. Set this var manually prior to calling +/// any of this package's functions. I know its nasty to have a global but +/// this makes it match OpenGL API styles where it also operates on a +/// threadlocal global. +pub threadlocal var context: Context = undefined; + +/// Initialize Glad. This is guaranteed to succeed if no errors are returned. +/// The getProcAddress param is an anytype so that we can accept multiple +/// forms of the function depending on what we're interfacing with. +pub fn load(getProcAddress: anytype) !c_int { + const GlProc = *const fn () callconv(.c) void; + const GlfwFn = *const fn ([*:0]const u8) callconv(.c) ?GlProc; + + const res = switch (@TypeOf(getProcAddress)) { + // glfw + GlfwFn => c.gladLoadGLContext(&context, @ptrCast(getProcAddress)), + + // null proc address means that we are just loading the globally + // pointed gl functions + @TypeOf(null) => c.gladLoaderLoadGLContext(&context), + + // try as-is. If this introduces a compiler error, then add a new case. + else => c.gladLoadGLContext(&context, @ptrCast(getProcAddress)), + }; + if (res == 0) return error.GLInitFailed; + return res; +} + +pub fn unload() void { + c.gladLoaderUnloadGLContext(&context); + context = undefined; +} + +pub fn versionMajor(res: c_uint) c_uint { + return c.GLAD_VERSION_MAJOR(res); +} + +pub fn versionMinor(res: c_uint) c_uint { + return c.GLAD_VERSION_MINOR(res); +} diff --git a/vendor/libghostty-vt/pkg/opengl/main.zig b/vendor/libghostty-vt/pkg/opengl/main.zig new file mode 100644 index 00000000..2f22154c --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/main.zig @@ -0,0 +1,45 @@ +//! OpenGL bindings. +//! +//! These are purpose-built for usage within this program. While they closely +//! align with the OpenGL C APIs, they aren't meant to be general purpose, +//! they aren't meant to have 100% API coverage, and they aren't meant to +//! be hyper-performant. +//! +//! For performance-intensive or unsupported aspects of OpenGL, the C +//! API is exposed via the `c` constant. +//! +//! WARNING: Lots of performance improvements that we can make with Zig +//! comptime help. I'm deferring this until later but have some fun ideas. + +pub const c = @import("c.zig").c; +pub const glad = @import("glad.zig"); +pub const ext = @import("extensions.zig"); +pub const Buffer = @import("Buffer.zig"); +pub const Framebuffer = @import("Framebuffer.zig"); +pub const Renderbuffer = @import("Renderbuffer.zig"); +pub const Program = @import("Program.zig"); +pub const Sampler = @import("Sampler.zig"); +pub const Shader = @import("Shader.zig"); +pub const Texture = @import("Texture.zig"); +pub const VertexArray = @import("VertexArray.zig"); + +pub const errors = @import("errors.zig"); + +pub const Primitive = @import("primitives.zig").Primitive; + +const draw = @import("draw.zig"); + +pub const blendFunc = draw.blendFunc; +pub const clear = draw.clear; +pub const clearColor = draw.clearColor; +pub const drawArrays = draw.drawArrays; +pub const drawArraysInstanced = draw.drawArraysInstanced; +pub const drawElements = draw.drawElements; +pub const drawElementsInstanced = draw.drawElementsInstanced; +pub const enable = draw.enable; +pub const disable = draw.disable; +pub const frontFace = draw.frontFace; +pub const pixelStore = draw.pixelStore; +pub const viewport = draw.viewport; +pub const flush = draw.flush; +pub const finish = draw.finish; diff --git a/vendor/libghostty-vt/pkg/opengl/primitives.zig b/vendor/libghostty-vt/pkg/opengl/primitives.zig new file mode 100644 index 00000000..e12f51a6 --- /dev/null +++ b/vendor/libghostty-vt/pkg/opengl/primitives.zig @@ -0,0 +1,18 @@ +pub const c = @import("c.zig").c; + +pub const Primitive = enum(c_int) { + point = c.GL_POINTS, + line = c.GL_LINES, + line_strip = c.GL_LINE_STRIP, + triangle = c.GL_TRIANGLES, + triangle_strip = c.GL_TRIANGLE_STRIP, + + // Commented out primitive types are excluded for parity with Metal. + // + // line_loop = c.GL_LINE_LOOP, + // line_adjacency = c.GL_LINES_ADJACENCY, + // line_strip_adjacency = c.GL_LINE_STRIP_ADJACENCY, + // triangle_fan = c.GL_TRIANGLE_FAN, + // triangle_adjacency = c.GL_TRIANGLES_ADJACENCY, + // triangle_strip_adjacency = c.GL_TRIANGLE_STRIP_ADJACENCY, +}; |
