style(renderer): explicit error sets

pull/7620/head
Qwerasd 2025-06-20 15:16:31 -06:00
parent 9d00018f8b
commit ea7a91e2ba
5 changed files with 42 additions and 25 deletions

View File

@ -7,15 +7,16 @@ const glad = @import("glad.zig");
id: c.GLuint, id: c.GLuint,
pub fn active(index: c_uint) !void { pub fn active(index: c_uint) errors.Error!void {
glad.context.ActiveTexture.?(index + c.GL_TEXTURE0); glad.context.ActiveTexture.?(index + c.GL_TEXTURE0);
try errors.getError(); try errors.getError();
} }
/// Create a single texture. /// Create a single texture.
pub fn create() !Texture { pub fn create() errors.Error!Texture {
var id: c.GLuint = undefined; var id: c.GLuint = undefined;
glad.context.GenTextures.?(1, &id); glad.context.GenTextures.?(1, &id);
try errors.getError();
return .{ .id = id }; return .{ .id = id };
} }
@ -107,7 +108,7 @@ pub const Binding = struct {
glad.context.GenerateMipmap.?(@intFromEnum(b.target)); glad.context.GenerateMipmap.?(@intFromEnum(b.target));
} }
pub fn parameter(b: Binding, name: Parameter, value: anytype) !void { pub fn parameter(b: Binding, name: Parameter, value: anytype) errors.Error!void {
switch (@TypeOf(value)) { switch (@TypeOf(value)) {
c.GLint => glad.context.TexParameteri.?( c.GLint => glad.context.TexParameteri.?(
@intFromEnum(b.target), @intFromEnum(b.target),
@ -129,7 +130,7 @@ pub const Binding = struct {
format: Format, format: Format,
typ: DataType, typ: DataType,
data: ?*const anyopaque, data: ?*const anyopaque,
) !void { ) errors.Error!void {
glad.context.TexImage2D.?( glad.context.TexImage2D.?(
@intFromEnum(b.target), @intFromEnum(b.target),
level, level,
@ -154,7 +155,7 @@ pub const Binding = struct {
format: Format, format: Format,
typ: DataType, typ: DataType,
data: ?*const anyopaque, data: ?*const anyopaque,
) !void { ) errors.Error!void {
glad.context.TexSubImage2D.?( glad.context.TexSubImage2D.?(
@intFromEnum(b.target), @intFromEnum(b.target),
level, level,
@ -178,7 +179,7 @@ pub const Binding = struct {
y: c.GLint, y: c.GLint,
width: c.GLsizei, width: c.GLsizei,
height: c.GLsizei, height: c.GLsizei,
) !void { ) errors.Error!void {
glad.context.CopyTexSubImage2D.?( glad.context.CopyTexSubImage2D.?(
@intFromEnum(b.target), @intFromEnum(b.target),
level, level,

View File

@ -284,14 +284,17 @@ pub inline fn textureOptions(self: Metal) Texture.Options {
} }
/// Initializes a Texture suitable for the provided font atlas. /// Initializes a Texture suitable for the provided font atlas.
pub fn initAtlasTexture(self: *const Metal, atlas: *const font.Atlas) !Texture { pub fn initAtlasTexture(
self: *const Metal,
atlas: *const font.Atlas,
) Texture.Error!Texture {
const pixel_format: mtl.MTLPixelFormat = switch (atlas.format) { const pixel_format: mtl.MTLPixelFormat = switch (atlas.format) {
.grayscale => .r8unorm, .grayscale => .r8unorm,
.rgba => .bgra8unorm, .rgba => .bgra8unorm,
else => @panic("unsupported atlas format for Metal texture"), else => @panic("unsupported atlas format for Metal texture"),
}; };
return Texture.init( return try Texture.init(
.{ .{
.device = self.device, .device = self.device,
.pixel_format = pixel_format, .pixel_format = pixel_format,

View File

@ -384,7 +384,10 @@ pub inline fn textureOptions(self: OpenGL) Texture.Options {
} }
/// Initializes a Texture suitable for the provided font atlas. /// Initializes a Texture suitable for the provided font atlas.
pub fn initAtlasTexture(self: *const OpenGL, atlas: *const font.Atlas) !Texture { pub fn initAtlasTexture(
self: *const OpenGL,
atlas: *const font.Atlas,
) Texture.Error!Texture {
_ = self; _ = self;
const format: gl.Texture.Format, const internal_format: gl.Texture.InternalFormat = const format: gl.Texture.Format, const internal_format: gl.Texture.InternalFormat =
switch (atlas.format) { switch (atlas.format) {
@ -393,7 +396,7 @@ pub fn initAtlasTexture(self: *const OpenGL, atlas: *const font.Atlas) !Texture
else => @panic("unsupported atlas format for OpenGL texture"), else => @panic("unsupported atlas format for OpenGL texture"),
}; };
return Texture.init( return try Texture.init(
.{ .{
.format = format, .format = format,
.internal_format = internal_format, .internal_format = internal_format,

View File

@ -31,13 +31,18 @@ height: usize,
/// Bytes per pixel for this texture. /// Bytes per pixel for this texture.
bpp: usize, bpp: usize,
pub const Error = error{
/// A Metal API call failed.
MetalFailed,
};
/// Initialize a texture /// Initialize a texture
pub fn init( pub fn init(
opts: Options, opts: Options,
width: usize, width: usize,
height: usize, height: usize,
data: ?[]const u8, data: ?[]const u8,
) !Self { ) Error!Self {
// Create our descriptor // Create our descriptor
const desc = init: { const desc = init: {
const Class = objc.getClass("MTLTextureDescriptor").?; const Class = objc.getClass("MTLTextureDescriptor").?;
@ -90,7 +95,7 @@ pub fn replaceRegion(
width: usize, width: usize,
height: usize, height: usize,
data: []const u8, data: []const u8,
) !void { ) error{}!void {
self.texture.msgSend( self.texture.msgSend(
void, void,
objc.sel("replaceRegion:mipmapLevel:withBytes:bytesPerRow:"), objc.sel("replaceRegion:mipmapLevel:withBytes:bytesPerRow:"),

View File

@ -31,23 +31,28 @@ format: gl.Texture.Format,
/// Target for this texture. /// Target for this texture.
target: gl.Texture.Target, target: gl.Texture.Target,
pub const Error = error{
/// An OpenGL API call failed.
OpenGLFailed,
};
/// Initialize a texture /// Initialize a texture
pub fn init( pub fn init(
opts: Options, opts: Options,
width: usize, width: usize,
height: usize, height: usize,
data: ?[]const u8, data: ?[]const u8,
) !Self { ) Error!Self {
const tex = try gl.Texture.create(); const tex = gl.Texture.create() catch return error.OpenGLFailed;
errdefer tex.destroy(); errdefer tex.destroy();
{ {
const texbind = try tex.bind(opts.target); const texbind = tex.bind(opts.target) catch return error.OpenGLFailed;
defer texbind.unbind(); defer texbind.unbind();
try texbind.parameter(.WrapS, gl.c.GL_CLAMP_TO_EDGE); texbind.parameter(.WrapS, gl.c.GL_CLAMP_TO_EDGE) catch return error.OpenGLFailed;
try texbind.parameter(.WrapT, gl.c.GL_CLAMP_TO_EDGE); texbind.parameter(.WrapT, gl.c.GL_CLAMP_TO_EDGE) catch return error.OpenGLFailed;
try texbind.parameter(.MinFilter, gl.c.GL_LINEAR); texbind.parameter(.MinFilter, gl.c.GL_LINEAR) catch return error.OpenGLFailed;
try texbind.parameter(.MagFilter, gl.c.GL_LINEAR); texbind.parameter(.MagFilter, gl.c.GL_LINEAR) catch return error.OpenGLFailed;
try texbind.image2D( texbind.image2D(
0, 0,
opts.internal_format, opts.internal_format,
@intCast(width), @intCast(width),
@ -56,7 +61,7 @@ pub fn init(
opts.format, opts.format,
.UnsignedByte, .UnsignedByte,
if (data) |d| @ptrCast(d.ptr) else null, if (data) |d| @ptrCast(d.ptr) else null,
); ) catch return error.OpenGLFailed;
} }
return .{ return .{
@ -82,10 +87,10 @@ pub fn replaceRegion(
width: usize, width: usize,
height: usize, height: usize,
data: []const u8, data: []const u8,
) !void { ) Error!void {
const texbind = try self.texture.bind(self.target); const texbind = self.texture.bind(self.target) catch return error.OpenGLFailed;
defer texbind.unbind(); defer texbind.unbind();
try texbind.subImage2D( texbind.subImage2D(
0, 0,
@intCast(x), @intCast(x),
@intCast(y), @intCast(y),
@ -94,5 +99,5 @@ pub fn replaceRegion(
self.format, self.format,
.UnsignedByte, .UnsignedByte,
data.ptr, data.ptr,
); ) catch return error.OpenGLFailed;
} }