From 38601dc42cd8e936cd7d3313f57fc27f687bf721 Mon Sep 17 00:00:00 2001 From: IntegratedQuantum Date: Wed, 12 Apr 2023 20:33:58 +0200 Subject: [PATCH] Reduce raymarching pixel errors and add chunkmesh gpu memory usage to the debug menu. --- assets/cubyz/shaders/chunks/chunk_fragment.fs | 3 ++- src/graphics.zig | 9 ++++++++- src/gui/windows/debug.zig | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/assets/cubyz/shaders/chunks/chunk_fragment.fs b/assets/cubyz/shaders/chunks/chunk_fragment.fs index 88dcdf32..2b38483d 100644 --- a/assets/cubyz/shaders/chunks/chunk_fragment.fs +++ b/assets/cubyz/shaders/chunks/chunk_fragment.fs @@ -101,6 +101,7 @@ RayMarchResult rayMarching(vec3 startPosition, vec3 direction) { // TODO: Mipmap vec3 tDelta = 1/direction; vec3 t2 = t1 + tDelta; tDelta = abs(tDelta); + vec3 invTDelta = intBitsToFloat(floatBitsToInt(1.0) | modelSize)/tDelta; vec3 tMax = max(t1, t2) - tDelta; if(direction.x == 0) tMax.x = 1.0/0.0; if(direction.y == 0) tMax.y = 1.0/0.0; @@ -120,7 +121,7 @@ RayMarchResult rayMarching(vec3 startPosition, vec3 direction) { // TODO: Mipmap it++; vec3 tNext = tMax + block*tDelta; total_tMax = min(tNext.x, min(tNext.y, tNext.z)); - vec3 missingSteps = floor((total_tMax - tMax)/tDelta + 0.00001); + vec3 missingSteps = floor((total_tMax - tMax)*invTDelta); voxelIndex += int(dot(missingSteps, stepInIndex)); tMax += missingSteps*tDelta; if((voxelIndex & overflowMask) != 0) diff --git a/src/graphics.zig b/src/graphics.zig index 3f06bac7..69b86921 100644 --- a/src/graphics.zig +++ b/src/graphics.zig @@ -1141,11 +1141,14 @@ pub const LargeBuffer = struct { }; ssbo: SSBO, freeBlocks: std.ArrayList(Allocation), + capacity: u32, + used: u32, pub fn init(self: *LargeBuffer, allocator: Allocator, size: u31, binding: c_uint) !void { self.ssbo = SSBO.init(); self.ssbo.createDynamicBuffer(size); self.ssbo.bind(binding); + self.capacity = size; self.freeBlocks = std.ArrayList(Allocation).init(allocator); try self.freeBlocks.append(.{.start = 0, .len = size}); @@ -1157,6 +1160,7 @@ pub const LargeBuffer = struct { } fn alloc(self: *LargeBuffer, size: u31) !Allocation { + self.used += size; var smallestBlock: ?*Allocation = null; for(self.freeBlocks.items, 0..) |*block, i| { if(size == block.len) { @@ -1175,6 +1179,7 @@ pub const LargeBuffer = struct { } pub fn free(self: *LargeBuffer, _allocation: Allocation) !void { + self.used -= _allocation.len; var allocation = _allocation; if(allocation.len == 0) return; for(self.freeBlocks.items, 0..) |*block, i| { @@ -1198,6 +1203,7 @@ pub const LargeBuffer = struct { if(newSize == allocation.len) return; if(newSize < allocation.len) { const diff = allocation.len - newSize; + self.used -= diff; // Check if there is a free block directly after: for(self.freeBlocks.items) |*block| { if(allocation.start + allocation.len == block.start and block.len + allocation.len >= newSize) { @@ -1211,10 +1217,11 @@ pub const LargeBuffer = struct { allocation.len -= diff; try self.freeBlocks.append(.{.start = allocation.start + allocation.len, .len = diff}); } else { + const diff = newSize - allocation.len; + self.used += diff; // Check if the buffer can be extended without a problem: for(self.freeBlocks.items, 0..) |*block, i| { if(allocation.start + allocation.len == block.start and block.len + allocation.len >= newSize) { - const diff = newSize - allocation.len; allocation.len += diff; if(block.len != diff) { block.start += diff; diff --git a/src/gui/windows/debug.zig b/src/gui/windows/debug.zig index f5bc4ae6..733b06b3 100644 --- a/src/gui/windows/debug.zig +++ b/src/gui/windows/debug.zig @@ -35,6 +35,8 @@ fn flawedRender() !void { y += 8; try draw.print("Queue size: {}", .{main.threadPool.loadList.size}, 0, y, 8, .left); y += 8; + try draw.print("ChunkMesh memory: {} MiB / {} MiB (fragmentation: {})", .{main.chunk.meshing.faceBuffer.used >> 20, main.chunk.meshing.faceBuffer.capacity >> 20, main.chunk.meshing.faceBuffer.freeBlocks.items.len}, 0, y, 8, .left); + y += 8; // TODO: biome y += 8; // TODO: packet loss