-
Notifications
You must be signed in to change notification settings - Fork 0
chapter 10 surface details #39
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
1a95c98
4c32f52
a69271d
4f328f0
8ce3c8f
c660e6d
038bc6a
0731772
08bd761
3b18705
92327ea
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
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. Check for Slice Bounds to Prevent Runtime Panics When slicing Add assertions to ensure that the computed + 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
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return .{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .mesh = .{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .program = program, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Avoid Potential Stack Overflow with Dynamic Array Allocation
The arrays
attribute_dataandindicesare declared using the maximum possible sizes based onmax_precision, which may lead to stack overflow or excessive memory usage whenmax_precisionis large (250 in this case). Allocating large fixed-size arrays on the stack is not recommended.Consider allocating these arrays dynamically based on
num_verticesandnum_indices, which are computed from the providedprecision. This approach optimizes memory usage and prevents potential stack overflow errors.Apply this diff to allocate arrays with the exact required sizes:
Ensure that you handle allocation errors appropriately and have access to an allocator, such as
std.heap.page_allocator.