-
Notifications
You must be signed in to change notification settings - Fork 0
support texture binding when bindless is not available #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,22 @@ | ||||
disable_bindless: bool = false, | ||||
|
||||
const Args = @This(); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary use of The line Apply this diff to remove the redundant line: -const Args = @This(); Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai that is not redundant, this is an anonymous file struct and I have to use a reference via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @btipling, thank you for the clarification, and I apologize for the oversight. You're correct that in an anonymous file struct, using (/._.)/ Learnings added
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) Args { | ||||
const arg_values = std.process.argsAlloc(allocator) catch @panic("OOM"); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle errors gracefully without panicking. Using Apply this diff to handle the error properly: - const arg_values = std.process.argsAlloc(allocator) catch @panic("OOM");
+ const arg_values = try std.process.argsAlloc(allocator); Update the function signature to return an error: -pub fn init(allocator: std.mem.Allocator) Args {
+pub fn init(allocator: std.mem.Allocator) !Args { And ensure you handle the error when calling
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai I panic on oom intentionally There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @btipling, understood. If panicking on OOM is intentional in your codebase, then there's no need to change it. Thank you for the clarification. Learnings added
|
||||
defer std.process.argsFree(allocator, arg_values); | ||||
var rv: Args = .{}; | ||||
for (arg_values) |av| { | ||||
std.debug.print("{s}\n", .{av}); | ||||
if (std.mem.eql(u8, av, "--disable_bindless")) { | ||||
rv.disable_bindless = true; | ||||
} | ||||
if (std.mem.eql(u8, av, "--help")) { | ||||
std.debug.print("--disable_bindless to disable bindless textures\n", .{}); | ||||
} | ||||
Comment on lines
+14
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using Using separate Apply this diff to use if (std.mem.eql(u8, av, "--disable_bindless")) {
rv.disable_bindless = true;
}
- if (std.mem.eql(u8, av, "--help")) {
+ else if (std.mem.eql(u8, av, "--help")) {
std.debug.print("--disable_bindless to disable bindless textures\n");
}
|
||||
} | ||||
std.debug.print("args.disable_bindless: {any}\n", .{rv.disable_bindless}); | ||||
return rv; | ||||
} | ||||
Comment on lines
+5
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Place For better readability and to follow Zig conventions, the Apply this diff to move the import statement: +const std = @import("std");
+
pub fn init(allocator: std.mem.Allocator) Args {
const arg_values = std.process.argsAlloc(allocator) catch @panic("OOM");
defer std.process.argsFree(allocator, arg_values);
var rv: Args = .{};
for (arg_values) |av| {
- std.debug.print("{s}\n", .{av});
if (std.mem.eql(u8, av, "--disable_bindless")) {
rv.disable_bindless = true;
}
if (std.mem.eql(u8, av, "--help")) {
- std.debug.print("--disable_bindless to disable bindless textures\n", .{});
+ std.debug.print("--disable_bindless to disable bindless textures\n");
}
}
std.debug.print("args.disable_bindless: {}\n", .{rv.disable_bindless});
return rv;
}
-
-const std = @import("std");
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai I prefer imports at the bottom. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @btipling, understood. I'll keep that in mind for future reviews. (_/) Learnings added
|
||||
|
||||
const std = @import("std"); |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,45 +4,68 @@ handle: c.GLuint64 = 0, | |||||||||||||||||||
uniform: Uniform = undefined, | ||||||||||||||||||||
wrap_s: c.GLint = c.GL_CLAMP_TO_EDGE, | ||||||||||||||||||||
wrap_t: c.GLint = c.GL_CLAMP_TO_EDGE, | ||||||||||||||||||||
disable_bindless: bool = false, | ||||||||||||||||||||
|
||||||||||||||||||||
const Texture = @This(); | ||||||||||||||||||||
|
||||||||||||||||||||
pub const TextureError = error{ | ||||||||||||||||||||
BindlessHandleCreationFailed, | ||||||||||||||||||||
BindlessNotSupported, | ||||||||||||||||||||
}; | ||||||||||||||||||||
|
||||||||||||||||||||
pub fn init(image: *assets.Image, program: u32, uniform_name: []const u8) TextureError!Texture { | ||||||||||||||||||||
var t: Texture = .{}; | ||||||||||||||||||||
return t.setup(image, program, uniform_name); | ||||||||||||||||||||
pub fn frag_shader(tx: ?Texture) Shader.fragment_shader_type { | ||||||||||||||||||||
if (tx) |t| if (!t.disable_bindless) return .bindless; | ||||||||||||||||||||
return .texture; | ||||||||||||||||||||
Comment on lines
+15
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify the To improve readability, consider combining the conditions in the Apply this diff to simplify the function: pub fn frag_shader(tx: ?Texture) Shader.fragment_shader_type {
- if (tx) |t| if (!t.disable_bindless) return .bindless;
+ if (tx and !tx.disable_bindless) {
+ return .bindless;
+ }
return .texture;
} Committable suggestion
Suggested change
|
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
pub fn setup(self: *Texture, image: *assets.Image, program: u32, uniform_name: []const u8) TextureError!Texture { | ||||||||||||||||||||
pub fn init(disable_bindless: bool) TextureError!Texture { | ||||||||||||||||||||
var t: Texture = .{ | ||||||||||||||||||||
.disable_bindless = disable_bindless, | ||||||||||||||||||||
}; | ||||||||||||||||||||
|
||||||||||||||||||||
if (c.glfwExtensionSupported("GL_ARB_bindless_texture") != 1 or c.glfwExtensionSupported("GL_ARB_bindless_texture") != 1) { | ||||||||||||||||||||
return TextureError.BindlessNotSupported; | ||||||||||||||||||||
t.disable_bindless = true; | ||||||||||||||||||||
} | ||||||||||||||||||||
return t; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
pub fn setup(self: *Texture, image: ?*assets.Image, program: u32, uniform_name: []const u8) TextureError!void { | ||||||||||||||||||||
var name: u32 = undefined; | ||||||||||||||||||||
c.glCreateTextures(c.GL_TEXTURE_2D, 1, @ptrCast(&name)); | ||||||||||||||||||||
c.glTextureParameteri(name, c.GL_TEXTURE_WRAP_S, self.wrap_s); | ||||||||||||||||||||
c.glTextureParameteri(name, c.GL_TEXTURE_WRAP_T, self.wrap_t); | ||||||||||||||||||||
c.glTextureParameteri(name, c.GL_TEXTURE_MIN_FILTER, c.GL_LINEAR_MIPMAP_LINEAR); | ||||||||||||||||||||
c.glTextureParameteri(name, c.GL_TEXTURE_MAG_FILTER, c.GL_LINEAR); | ||||||||||||||||||||
const w: f32 = @floatFromInt(image.width); | ||||||||||||||||||||
const h: f32 = @floatFromInt(image.height); | ||||||||||||||||||||
const mip_map_levels: c.GLsizei = @intFromFloat(@ceil(@log2(@max(w, h)))); | ||||||||||||||||||||
c.glTextureStorage2D(name, mip_map_levels, c.GL_RGBA8, @intCast(image.width), @intCast(image.height)); | ||||||||||||||||||||
c.glTextureSubImage2D( | ||||||||||||||||||||
name, | ||||||||||||||||||||
0, | ||||||||||||||||||||
0, | ||||||||||||||||||||
0, | ||||||||||||||||||||
@intCast(image.width), | ||||||||||||||||||||
@intCast(image.height), | ||||||||||||||||||||
c.GL_RGBA, | ||||||||||||||||||||
c.GL_UNSIGNED_BYTE, | ||||||||||||||||||||
image.stb_data.ptr, | ||||||||||||||||||||
); | ||||||||||||||||||||
if (image) |img| { | ||||||||||||||||||||
const w: f32 = @floatFromInt(img.width); | ||||||||||||||||||||
const h: f32 = @floatFromInt(img.height); | ||||||||||||||||||||
const mip_map_levels: c.GLsizei = @intFromFloat(@ceil(@log2(@max(w, h)))); | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix calculation of mipmap levels to prevent off-by-one error. The calculation for Adjust the calculation as follows: - const mip_map_levels: c.GLsizei = @intFromFloat(@ceil(@log2(@max(w, h))));
+ const mip_map_levels: c.GLsizei = @intFromFloat(@floor(@log2(@max(w, h)))) + 1; Committable suggestion
Suggested change
|
||||||||||||||||||||
c.glTextureStorage2D(name, mip_map_levels, c.GL_RGBA8, @intCast(img.width), @intCast(img.height)); | ||||||||||||||||||||
c.glTextureSubImage2D( | ||||||||||||||||||||
name, | ||||||||||||||||||||
0, | ||||||||||||||||||||
0, | ||||||||||||||||||||
0, | ||||||||||||||||||||
@intCast(img.width), | ||||||||||||||||||||
@intCast(img.height), | ||||||||||||||||||||
c.GL_RGBA, | ||||||||||||||||||||
c.GL_UNSIGNED_BYTE, | ||||||||||||||||||||
img.stb_data.ptr, | ||||||||||||||||||||
); | ||||||||||||||||||||
} else { | ||||||||||||||||||||
const magenta_rgba_color = [4]u8{ 255, 0, 255, 255 }; | ||||||||||||||||||||
c.glTextureStorage2D(name, 1, c.GL_RGBA8, 1, 1); | ||||||||||||||||||||
c.glTextureSubImage2D( | ||||||||||||||||||||
name, | ||||||||||||||||||||
0, | ||||||||||||||||||||
0, | ||||||||||||||||||||
0, | ||||||||||||||||||||
1, | ||||||||||||||||||||
1, | ||||||||||||||||||||
c.GL_RGBA, | ||||||||||||||||||||
c.GL_UNSIGNED_BYTE, | ||||||||||||||||||||
&magenta_rgba_color, | ||||||||||||||||||||
); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
c.glGenerateTextureMipmap(name); | ||||||||||||||||||||
if (c.glfwExtensionSupported("GL_EXT_texture_filter_anisotropic") == 1) { | ||||||||||||||||||||
|
@@ -53,6 +76,11 @@ pub fn setup(self: *Texture, image: *assets.Image, program: u32, uniform_name: [ | |||||||||||||||||||
|
||||||||||||||||||||
self.name = name; | ||||||||||||||||||||
|
||||||||||||||||||||
self.uniform = Uniform.init(program, uniform_name); | ||||||||||||||||||||
|
||||||||||||||||||||
if (self.disable_bindless) { | ||||||||||||||||||||
return; | ||||||||||||||||||||
} | ||||||||||||||||||||
// Generate bindless handle | ||||||||||||||||||||
self.handle = c.glGetTextureHandleARB(self.name); | ||||||||||||||||||||
if (self.handle == 0) { | ||||||||||||||||||||
|
@@ -62,9 +90,7 @@ pub fn setup(self: *Texture, image: *assets.Image, program: u32, uniform_name: [ | |||||||||||||||||||
// Make the texture resident | ||||||||||||||||||||
c.glMakeTextureHandleResidentARB(self.handle); | ||||||||||||||||||||
|
||||||||||||||||||||
self.uniform = Uniform.init(program, uniform_name); | ||||||||||||||||||||
|
||||||||||||||||||||
return self.*; | ||||||||||||||||||||
return; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
pub fn makeNonResident(self: Texture) void { | ||||||||||||||||||||
|
@@ -81,10 +107,15 @@ pub fn deinit(self: Texture) void { | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
pub fn bind(self: Texture) void { | ||||||||||||||||||||
if (self.disable_bindless) { | ||||||||||||||||||||
c.glBindTextureUnit(self.texture_unit, self.name); | ||||||||||||||||||||
return; | ||||||||||||||||||||
} | ||||||||||||||||||||
self.uniform.setUniformHandleui64ARB(self.handle); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
const std = @import("std"); | ||||||||||||||||||||
const c = @import("../c.zig").c; | ||||||||||||||||||||
const Uniform = @import("Uniform.zig"); | ||||||||||||||||||||
const assets = @import("../assets/assets.zig"); | ||||||||||||||||||||
const Shader = @import("Shader.zig"); |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -88,11 +88,13 @@ pub fn updateCamera(_: *TexturedPyramid) void {} | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
pub fn renderParallepiped(self: *TexturedPyramid) void { | ||||||||||||||||||||||||||||||
const prog = rhi.createProgram(); | ||||||||||||||||||||||||||||||
self.brick_texture = rhi.Texture.init(self.ctx.args.disable_bindless) catch null; | ||||||||||||||||||||||||||||||
self.ice_texture = rhi.Texture.init(self.ctx.args.disable_bindless) catch null; | ||||||||||||||||||||||||||||||
Comment on lines
+91
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error logging when initializing textures Currently, if Apply this diff to add error logging: self.brick_texture = rhi.Texture.init(self.ctx.args.disable_bindless) catch |err| {
+ std.debug.print("Failed to initialize brick texture: {}\n", .{err});
null
};
self.ice_texture = rhi.Texture.init(self.ctx.args.disable_bindless) catch |err| {
+ std.debug.print("Failed to initialize ice texture: {}\n", .{err});
null
};
|
||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
var s: rhi.Shader = .{ | ||||||||||||||||||||||||||||||
.program = prog, | ||||||||||||||||||||||||||||||
.instance_data = true, | ||||||||||||||||||||||||||||||
.fragment_shader = .bindless, | ||||||||||||||||||||||||||||||
.fragment_shader = rhi.Texture.frag_shader(self.brick_texture), | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential null If Apply this diff to add a null check: var s: rhi.Shader = .{
.program = prog,
.instance_data = true,
- .fragment_shader = rhi.Texture.frag_shader(self.brick_texture),
+ .fragment_shader = if (self.brick_texture) |bt| {
+ rhi.Texture.frag_shader(bt)
+ } else {
+ rhi.Shader.default_frag_shader, // Replace with an appropriate default shader
+ },
};
|
||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
const partials = [_][]const u8{vertex_shader}; | ||||||||||||||||||||||||||||||
s.attach(self.allocator, @ptrCast(partials[0..])); | ||||||||||||||||||||||||||||||
|
@@ -119,17 +121,15 @@ pub fn renderParallepiped(self: *TexturedPyramid) void { | |||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
self.view_camera.addProgram(prog, "f_mvp"); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if (self.ctx.textures_loader.loadAsset("cgpoc\\luna\\brick1.jpg") catch null) |img| { | ||||||||||||||||||||||||||||||
self.brick_texture = rhi.Texture.init(img, prog, "f_samp") catch null; | ||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||
std.debug.print("no brick image\n", .{}); | ||||||||||||||||||||||||||||||
if (self.brick_texture) |*bt| { | ||||||||||||||||||||||||||||||
bt.setup(self.ctx.textures_loader.loadAsset("cgpoc\\luna\\brick1.jpg") catch null, prog, "f_samp") catch { | ||||||||||||||||||||||||||||||
self.brick_texture = null; | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
Comment on lines
+125
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verify asset loading before calling If Apply this diff to add a null check: if (self.brick_texture) |*bt| {
- bt.setup(self.ctx.textures_loader.loadAsset("cgpoc\\luna\\brick1.jpg") catch null, prog, "f_samp") catch {
+ if (self.ctx.textures_loader.loadAsset("cgpoc\\luna\\brick1.jpg")) |asset| {
+ bt.setup(asset, prog, "f_samp") catch |err| {
+ std.debug.print("Failed to set up brick texture: {}\n", .{err});
+ self.brick_texture = null;
+ };
+ } else {
+ std.debug.print("Failed to load brick texture asset\n", .{});
+ self.brick_texture = null;
+ }
} Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if (self.ctx.textures_loader.loadAsset("cgpoc\\luna\\ice.jpg") catch null) |img| { | ||||||||||||||||||||||||||||||
self.ice_texture = rhi.Texture.init(img, prog, "f_samp") catch null; | ||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||
std.debug.print("no ice image\n", .{}); | ||||||||||||||||||||||||||||||
if (self.ice_texture) |*it| { | ||||||||||||||||||||||||||||||
it.setup(self.ctx.textures_loader.loadAsset("cgpoc\\luna\\ice.jpg") catch null, prog, "f_samp") catch { | ||||||||||||||||||||||||||||||
self.ice_texture = null; | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
Comment on lines
+130
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verify asset loading before calling Similarly, ensure that Apply this diff to add a null check: if (self.ice_texture) |*it| {
- it.setup(self.ctx.textures_loader.loadAsset("cgpoc\\luna\\ice.jpg") catch null, prog, "f_samp") catch {
+ if (self.ctx.textures_loader.loadAsset("cgpoc\\luna\\ice.jpg")) |asset| {
+ it.setup(asset, prog, "f_samp") catch |err| {
+ std.debug.print("Failed to set up ice texture: {}\n", .{err});
+ self.ice_texture = null;
+ };
+ } else {
+ std.debug.print("Failed to load ice texture asset\n", .{});
+ self.ice_texture = null;
+ }
} Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
self.pyramid = pyramid; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,11 +65,12 @@ pub fn updateCamera(_: *Earth) void {} | |
|
||
pub fn renderSphere(self: *Earth) void { | ||
const prog = rhi.createProgram(); | ||
self.earth_texture = rhi.Texture.init(self.ctx.args.disable_bindless) catch null; | ||
btipling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var s: rhi.Shader = .{ | ||
.program = prog, | ||
.instance_data = true, | ||
.fragment_shader = .bindless, | ||
.fragment_shader = rhi.Texture.frag_shader(self.earth_texture), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure If Apply this diff to safely assign the fragment shader: var s: rhi.Shader = .{
.program = prog,
.instance_data = true,
- .fragment_shader = rhi.Texture.frag_shader(self.earth_texture),
+ .fragment_shader = if (self.earth_texture) |et| rhi.Texture.frag_shader(et) else default_fragment_shader,
}; Ensure that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai frag_shader handles nulls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @btipling, thank you for the clarification. I've noted that (_/) Learnings added
|
||
}; | ||
const partials = [_][]const u8{vertex_shader}; | ||
s.attach(self.allocator, @ptrCast(partials[0..])); | ||
|
@@ -95,13 +96,12 @@ pub fn renderSphere(self: *Earth) void { | |
false, | ||
), | ||
}; | ||
self.view_camera.addProgram(prog, "f_mvp"); | ||
|
||
if (self.ctx.textures_loader.loadAsset("cgpoc\\earth.jpg") catch null) |img| { | ||
self.earth_texture = rhi.Texture.init(img, prog, "f_samp") catch null; | ||
} else { | ||
std.debug.print("no earth image\n", .{}); | ||
if (self.earth_texture) |*bt| { | ||
bt.setup(self.ctx.textures_loader.loadAsset("cgpoc\\earth.jpg") catch null, prog, "f_samp") catch { | ||
self.earth_texture = null; | ||
}; | ||
btipling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
self.view_camera.addProgram(prog, "f_mvp"); | ||
self.sphere = sphere; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -74,11 +74,12 @@ pub fn renderShuttle(self: *Shuttle) void { | |||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const prog = rhi.createProgram(); | ||||||||||||||||||||||||
self.shuttle_texture = rhi.Texture.init(self.ctx.args.disable_bindless) catch null; | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log errors when initializing shuttle texture fails Currently, if Apply this diff to log the error: self.shuttle_texture = rhi.Texture.init(self.ctx.args.disable_bindless) catch |e| {
+ std.debug.print("Failed to initialize shuttle texture: {}\n", .{e});
null
}; Committable suggestion
Suggested change
|
||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
var s: rhi.Shader = .{ | ||||||||||||||||||||||||
.program = prog, | ||||||||||||||||||||||||
.instance_data = true, | ||||||||||||||||||||||||
.fragment_shader = .bindless, | ||||||||||||||||||||||||
.fragment_shader = rhi.Texture.frag_shader(self.shuttle_texture), | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential null
Apply this diff to handle the .fragment_shader = if (self.shuttle_texture) |texture| {
rhi.Texture.frag_shader(texture)
} else {
+ rhi.default_frag_shader()
}, Assuming
|
||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
const partials = [_][]const u8{vertex_shader}; | ||||||||||||||||||||||||
s.attach(self.allocator, @ptrCast(partials[0..])); | ||||||||||||||||||||||||
|
@@ -97,14 +98,13 @@ pub fn renderShuttle(self: *Shuttle) void { | |||||||||||||||||||||||
}; | ||||||||||||||||||||||||
i_datas[0] = i_data; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
if (self.shuttle_texture) |*bt| { | ||||||||||||||||||||||||
bt.setup(self.ctx.textures_loader.loadAsset("cgpoc\\NasaShuttle\\spstob_1.jpg") catch null, prog, "f_samp") catch { | ||||||||||||||||||||||||
self.shuttle_texture = null; | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+101
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider refactoring texture initialization and setup into a helper function To improve code readability and maintainability, consider encapsulating the texture initialization and setup logic into a helper function. This would reduce code duplication and provide a single point of modification for texture handling. Example: fn initializeShuttleTexture(self: *Shuttle) void {
self.shuttle_texture = rhi.Texture.init(self.ctx.args.disable_bindless) catch |e| {
std.debug.print("Failed to initialize shuttle texture: {}\n", .{e});
null
};
if (self.shuttle_texture) |*bt| {
bt.setup(
self.ctx.textures_loader.loadAsset("cgpoc\\NasaShuttle\\spstob_1.jpg") catch null,
prog,
"f_samp",
) catch |e| {
self.shuttle_texture = null;
std.debug.print("Failed to setup shuttle texture: {}\n", .{e});
};
}
} Then call this function in + self.initializeShuttleTexture(); Log errors when shuttle texture setup fails When Apply this diff to log the error: bt.setup(self.ctx.textures_loader.loadAsset("cgpoc\\NasaShuttle\\spstob_1.jpg") catch null, prog, "f_samp") catch |e| {
self.shuttle_texture = null;
+ std.debug.print("Failed to setup shuttle texture: {}\n", .{e});
}; Committable suggestion
Suggested change
|
||||||||||||||||||||||||
const shuttle_object: object.object = shuttle_model.toObject(prog, i_datas[0..]); | ||||||||||||||||||||||||
self.view_camera.addProgram(prog, "f_mvp"); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if (self.ctx.textures_loader.loadAsset("cgpoc\\NasaShuttle\\spstob_1.jpg") catch null) |img| { | ||||||||||||||||||||||||
self.shuttle_texture = rhi.Texture.init(img, prog, "f_samp") catch null; | ||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||
std.debug.print("no shuttle image\n", .{}); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
self.shuttle = shuttle_object; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define the
Args
struct properly.The field declaration
disable_bindless: bool = false,
is not enclosed within a struct. To represent command-line arguments effectively, you should define anArgs
struct that contains this field.Apply this diff to define the
Args
struct:Committable suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai My approach is using the file struct feature of zig and it is defined properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@btipling, thank you for the clarification. I understand that you're using the file struct feature of Zig, which allows the file itself to act as a struct. I'll keep this in mind for future reviews.
(_/)
( •_•)
/ >📝
Learnings added
Learnings used