Skip to content
Merged
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
6 changes: 3 additions & 3 deletions src/foundations/assets/Obj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ data: []u8 = undefined,
file_name: []const u8 = undefined,
vertices: [max_vertices][3]f32 = undefined,
texture_coordinates: [max_vertices][2]f32 = undefined,
normals: [max_vertices][3]f32 = undefined,
normal: [max_vertices][3]f32 = undefined,
num_vertices: usize = 0,
indicies: [max_indicies][3][3]usize = undefined,
num_indicies: usize = 0,
Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn init(self: *Obj, _: std.mem.Allocator, data: []u8, file_name: []const u8)
const x: f32 = std.fmt.parseFloat(f32, nit.next() orelse "") catch @panic("invalid float");
const y: f32 = std.fmt.parseFloat(f32, nit.next() orelse "") catch @panic("invalid float");
const z: f32 = std.fmt.parseFloat(f32, nit.next() orelse "") catch @panic("invalid float");
self.normals[n_index] = .{ x, y, z };
self.normal[n_index] = .{ x, y, z };
n_index += 1;
},
else => {
Expand Down Expand Up @@ -80,7 +80,7 @@ pub fn toObject(self: *Obj, prog: u32, i_datas: []rhi.instanceData) object.objec
const p = self.vertices[index_data[j][0]];
attribute_data[current_index] = .{
.position = p,
.normals = self.normals[index_data[j][2]],
.normal = self.normal[index_data[j][2]],
.texture_coords = self.texture_coordinates[index_data[j][1]],
};
indices[current_index] = @intCast(current_index);
Expand Down
2 changes: 1 addition & 1 deletion src/foundations/assets/loader.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const app_name: []const u8 = "foundations_game_engine";
pub const max_file_size: usize = 4096 << 11;
pub const max_file_size: usize = 4096 << 12;

pub const LoaderError = error{
FileNotFound,
Expand Down
2 changes: 1 addition & 1 deletion src/foundations/object/ObjectCircle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn init(
rhi_data[i] = .{
.position = d.positions[i],
.color = .{ 1, 0, 1, 1 },
.normals = .{ 1, 0, 0 },
.normal = .{ 1, 0, 0 },
};
}
const vao_buf = rhi.attachInstancedBuffer(rhi_data[0..], instance_data);
Expand Down
6 changes: 3 additions & 3 deletions src/foundations/object/ObjectCone.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ fn data() struct { attribute_data: [num_vertices]rhi.attributeData, indices: [nu
const tri = math.geometry.Triangle.init(p0, p1, p2);
attribute_data[offset + 0] = .{
.position = tri.p0,
.normals = tri.normal,
.normal = tri.normal,
};
indices[offset + 0] = uoffset + 0;
attribute_data[uoffset + 1] = .{
.position = tri.p1,
.normals = tri.normal,
.normal = tri.normal,
};
indices[offset + 1] = uoffset + 1;
attribute_data[uoffset + 2] = .{
.position = tri.p2,
.normals = tri.normal,
.normal = tri.normal,
};
indices[offset + 2] = uoffset + 2;
offset += 3;
Expand Down
4 changes: 2 additions & 2 deletions src/foundations/object/ObjectCube.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub const default_positions: [36][3]f32 = .{
.{ -1, 1, -1 },
};

pub const normals: [36][3]f32 = .{
pub const normal: [36][3]f32 = .{
// z pos
.{ 0, 0, 1 },
.{ 0, 0, 1 },
Expand Down Expand Up @@ -120,7 +120,7 @@ pub fn init(
data[i] = .{
.position = positions[i],
.color = color,
.normals = normals[i],
.normal = normal[i],
};
}
const vao_buf = rhi.attachBuffer(data[0..]);
Expand Down
10 changes: 5 additions & 5 deletions src/foundations/object/ObjectCylinder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Cylinder = @This();

const num_sides = 160;
const num_vertices: usize = 4 * num_sides;
const num_indices: usize = 6 * num_sides; // because normals
const num_indices: usize = 6 * num_sides; // because normal

pub fn init(
program: u32,
Expand Down Expand Up @@ -114,22 +114,22 @@ fn addSurface(
s_data[offset] = .{
.position = sp0,
.color = color.debug_color,
.normals = n,
.normal = n,
};
s_data[offset + 1] = .{
.position = sp1,
.color = color.debug_color,
.normals = n,
.normal = n,
};
s_data[offset + 2] = .{
.position = sp2,
.color = color.debug_color,
.normals = n,
.normal = n,
};
s_data[offset + 3] = .{
.position = sp3,
.color = color.debug_color,
.normals = n,
.normal = n,
};
return offset + 4;
}
Expand Down
6 changes: 3 additions & 3 deletions src/foundations/object/ObjectInstancedTriangle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ pub fn init(
const triangle = math.geometry.Triangle.init(p0, p1, p2);
attribute_data[0] = .{
.position = triangle.p0,
.normals = triangle.normal,
.normal = triangle.normal,
};
attribute_data[1] = .{
.position = triangle.p1,
.normals = triangle.normal,
.normal = triangle.normal,
};
attribute_data[2] = .{
.position = triangle.p2,
.normals = triangle.normal,
.normal = triangle.normal,
};

const indices: [3]u32 = .{ 0, 1, 2 };
Expand Down
10 changes: 5 additions & 5 deletions src/foundations/object/ObjectParallelepiped.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ indices: [num_indices]u32,
const Parallelepied = @This();

const num_vertices: usize = 24;
const num_indices: usize = 36; // because normals
const num_indices: usize = 36; // because normal

pub const pp: math.geometry.Parallelepiped = .{
.v0 = .{ 1, 0, 0 },
Expand Down Expand Up @@ -246,25 +246,25 @@ fn addSurface(
s_data[offset] = .{
.position = sp0,
.color = color.debug_color,
.normals = n,
.normal = n,
.texture_coords = tc1,
};
s_data[offset + 1] = .{
.position = sp1,
.color = color.debug_color,
.normals = n,
.normal = n,
.texture_coords = tc2,
};
s_data[offset + 2] = .{
.position = sp2,
.color = color.debug_color,
.normals = n,
.normal = n,
.texture_coords = tc3,
};
s_data[offset + 3] = .{
.position = sp3,
.color = color.debug_color,
.normals = n,
.normal = n,
.texture_coords = tc4,
};
return offset + 4;
Expand Down
14 changes: 7 additions & 7 deletions src/foundations/object/ObjectPyramid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,19 @@ fn addSurface(
s_data[offset] = .{
.position = sp0,
.color = color.debug_color,
.normals = n,
.normal = n,
.texture_coords = texture_coords[0],
};
s_data[offset + 1] = .{
.position = sp1,
.color = color.debug_color,
.normals = n,
.normal = n,
.texture_coords = texture_coords[1],
};
s_data[offset + 2] = .{
.position = sp2,
.color = color.debug_color,
.normals = n,
.normal = n,
.texture_coords = texture_coords[2],
};
return offset + 3;
Expand Down Expand Up @@ -167,25 +167,25 @@ fn addBottomSurface(
s_data[offset] = .{
.position = sp0,
.color = color.debug_color,
.normals = n,
.normal = n,
.texture_coords = texture_coords[0],
};
s_data[offset + 1] = .{
.position = sp1,
.color = color.debug_color,
.normals = n,
.normal = n,
.texture_coords = texture_coords[1],
};
s_data[offset + 2] = .{
.position = sp2,
.color = color.debug_color,
.normals = n,
.normal = n,
.texture_coords = texture_coords[2],
};
s_data[offset + 3] = .{
.position = sp3,
.color = color.debug_color,
.normals = n,
.normal = n,
.texture_coords = texture_coords[3],
};
return offset + 4;
Expand Down
2 changes: 1 addition & 1 deletion src/foundations/object/ObjectQuad.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn initInstanced(
rhi_data[i] = .{
.position = positions[i],
.color = .{ 1, 0, 1, 1 },
.normals = .{ 1, 0, 0 },
.normal = .{ 1, 0, 0 },
};
}
const vao_buf = rhi.attachInstancedBuffer(rhi_data[0..], instance_data);
Expand Down
50 changes: 32 additions & 18 deletions src/foundations/object/ObjectSphere.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,33 @@ vertex_data_size: usize,
instance_data_stride: usize,

const Sphere = @This();
const precision: usize = 48;
const precision_f: f32 = @floatFromInt(precision);
const num_vertices = (precision + 1) * (precision + 1);
const num_indices = precision * precision * 6;
const default_precision: usize = 48;
const max_precision: usize = 250;
const max_num_vertices = (max_precision + 1) * (max_precision + 1);
const max_num_indices = max_precision * max_precision * 6;
Comment on lines +6 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid Potential Stack Overflow with Dynamic Array Allocation

The arrays attribute_data and indices are declared using the maximum possible sizes based on max_precision, which may lead to stack overflow or excessive memory usage when max_precision is large (250 in this case). Allocating large fixed-size arrays on the stack is not recommended.

Consider allocating these arrays dynamically based on num_vertices and num_indices, which are computed from the provided precision. This approach optimizes memory usage and prevents potential stack overflow errors.

Apply this diff to allocate arrays with the exact required sizes:

-    var attribute_data: [max_num_vertices]rhi.attributeData = undefined;
-    var indices: [max_num_indices]u32 = undefined;
+    var attribute_data = try std.heap.page_allocator.alloc(rhi.attributeData, num_vertices);
+    defer std.heap.page_allocator.free(attribute_data);
+    var indices = try std.heap.page_allocator.alloc(u32, num_indices);
+    defer std.heap.page_allocator.free(indices);

Ensure that you handle allocation errors appropriately and have access to an allocator, such as std.heap.page_allocator.

Committable suggestion was skipped due to low confidence.


pub fn init(
program: u32,
instance_data: []rhi.instanceData,
wireframe: bool,
) Sphere {
var d = data();
return initWithPrecision(program, instance_data, wireframe, default_precision);
}

const vao_buf = rhi.attachInstancedBuffer(d.attribute_data[0..], instance_data);
const ebo = rhi.initEBO(@ptrCast(d.indices[0..]), vao_buf.vao);
pub fn initWithPrecision(
program: u32,
instance_data: []rhi.instanceData,
wireframe: bool,
precision: usize,
) Sphere {
const num_vertices = (precision + 1) * (precision + 1);
const num_indices = precision * precision * 6;
var attribute_data: [max_num_vertices]rhi.attributeData = undefined;
var indices: [max_num_indices]u32 = undefined;
data(&attribute_data, &indices, precision);

const vao_buf = rhi.attachInstancedBuffer(attribute_data[0..num_vertices], instance_data);
const ebo = rhi.initEBO(@ptrCast(indices[0..num_indices]), vao_buf.vao);
Comment on lines +19 to +32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Check for Slice Bounds to Prevent Runtime Panics

When slicing attribute_data and indices arrays using [0..num_vertices] and [0..num_indices], there is a risk of exceeding the bounds if num_vertices or num_indices are greater than max_num_vertices or max_num_indices. This could lead to runtime panics due to out-of-bounds access.

Add assertions to ensure that the computed num_vertices and num_indices do not exceed the maximum sizes:

+    std.debug.assert(num_vertices <= max_num_vertices);
+    std.debug.assert(num_indices <= max_num_indices);

Alternatively, if you adopt dynamic allocation as suggested earlier, this issue would be mitigated.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub fn initWithPrecision(
program: u32,
instance_data: []rhi.instanceData,
wireframe: bool,
precision: usize,
) Sphere {
const num_vertices = (precision + 1) * (precision + 1);
const num_indices = precision * precision * 6;
var attribute_data: [max_num_vertices]rhi.attributeData = undefined;
var indices: [max_num_indices]u32 = undefined;
data(&attribute_data, &indices, precision);
const vao_buf = rhi.attachInstancedBuffer(attribute_data[0..num_vertices], instance_data);
const ebo = rhi.initEBO(@ptrCast(indices[0..num_indices]), vao_buf.vao);
pub fn initWithPrecision(
program: u32,
instance_data: []rhi.instanceData,
wireframe: bool,
precision: usize,
) Sphere {
const num_vertices = (precision + 1) * (precision + 1);
const num_indices = precision * precision * 6;
std.debug.assert(num_vertices <= max_num_vertices);
std.debug.assert(num_indices <= max_num_indices);
var attribute_data: [max_num_vertices]rhi.attributeData = undefined;
var indices: [max_num_indices]u32 = undefined;
data(&attribute_data, &indices, precision);
const vao_buf = rhi.attachInstancedBuffer(attribute_data[0..num_vertices], instance_data);
const ebo = rhi.initEBO(@ptrCast(indices[0..num_indices]), vao_buf.vao);

return .{
.mesh = .{
.program = program,
Expand All @@ -42,11 +55,9 @@ pub fn updateInstanceAt(self: Sphere, index: usize, instance_data: rhi.instanceD
rhi.updateInstanceData(self.mesh.buffer, self.vertex_data_size, self.instance_data_stride, index, instance_data);
}

fn data() struct { attribute_data: [num_vertices]rhi.attributeData, indices: [num_indices]u32 } {
var attribute_data: [num_vertices]rhi.attributeData = undefined;
var indices: [num_indices]u32 = undefined;

fn data(attribute_data: []rhi.attributeData, indices: []u32, precision: usize) void {
const p_index: usize = precision + 1;
const precision_f: f32 = @floatFromInt(precision);

for (0..p_index) |i| {
const i_f: f32 = @floatFromInt(i);
Expand All @@ -60,21 +71,24 @@ fn data() struct { attribute_data: [num_vertices]rhi.attributeData, indices: [nu
const pos: [3]f32 = .{ x, y, z };
const index: usize = i * p_index + j;

//TODO: support tangents in vertex attributes
// calculate tangent vector
var tangent: [3]f32 = undefined;
const normal: math.vector.vec3 = .{ x, y, z };
var tangent: [4]f32 = undefined;
if ((math.float.equal_e(x, 1.0) and math.float.equal_e(y, 0.0) and math.float.equal_e(z, 0.0)) or
(math.float.equal_e(x, -1.0) and math.float.equal_e(y, 0.0) and math.float.equal_e(z, 0.0)))
{
tangent = .{ 0, -1, 0 };
tangent = .{ 0, 1, 0, 1 };
} else {
tangent = .{ 1, 0, 0 };
const up: math.vector.vec3 = .{ -1, 0, 0 };
const t: math.vector.vec3 = math.vector.normalize(math.vector.crossProduct(up, normal));
const w: f32 = if (math.vector.dotProduct(math.vector.crossProduct(normal, t), up) < 0) 1 else -1;
tangent = .{ t[0], t[1], t[2], w };
}

attribute_data[index] = .{
.position = pos,
.normals = .{ x, y, z },
.normal = math.vector.normalize(normal),
.texture_coords = .{ 1.0 - j_f / precision_f, 1.0 - i_f / precision_f },
.tangent = tangent,
};
}
}
Expand Down Expand Up @@ -110,7 +124,7 @@ fn data() struct { attribute_data: [num_vertices]rhi.attributeData, indices: [nu
}
}

return .{ .attribute_data = attribute_data, .indices = indices };
return;
}

const std = @import("std");
Expand Down
2 changes: 1 addition & 1 deletion src/foundations/object/ObjectStrip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn init(
rhi_data[i] = .{
.position = positions[i],
.color = .{ 1, 0, 1, 1 },
.normals = .{ 1, 0, 0 },
.normal = .{ 1, 0, 0 },
};
}
const vao_buf = rhi.attachInstancedBuffer(rhi_data[0..], instance_data);
Expand Down
10 changes: 5 additions & 5 deletions src/foundations/object/ObjectTorus.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ fn data() struct { attribute_data: [num_vertices]rhi.attributeData, indices: [nu
tv = math.matrix.transformVector(m, tv);
const t_tangent: math.vector.vec3 = .{ tv[0], tv[1], tv[2] };
const s_tangent: math.vector.vec3 = .{ 0, 1, 0 };
const normals = math.vector.normalize(math.vector.crossProduct(t_tangent, s_tangent));
const normal = math.vector.normalize(math.vector.crossProduct(t_tangent, s_tangent));
attribute_data[i] = .{
.position = vertex,
.normals = normals,
.normal = normal,
.texture_coords = texture_coords,
};
}
Expand All @@ -95,13 +95,13 @@ fn data() struct { attribute_data: [num_vertices]rhi.attributeData, indices: [nu
// TODO: support tangents in vertex attributes
// s and t tangents need to be created here

const n = ad.normals;
const n = ad.normal;
var n1: math.vector.vec4 = .{ n[0], n[1], n[2], 0 };
n1 = math.matrix.transformVector(m, n1);
const normals: math.vector.vec3 = .{ n1[0], n1[1], n1[2] };
const normal: math.vector.vec3 = .{ n1[0], n1[1], n1[2] };
attribute_data[index] = .{
.position = vertex,
.normals = math.vector.normalize(normals),
.normal = math.vector.normalize(normal),
.texture_coords = texture_coords,
};
}
Expand Down
Loading