Reduce raymarching pixel errors and add chunkmesh gpu memory usage to the debug menu.

This commit is contained in:
IntegratedQuantum 2023-04-12 20:33:58 +02:00
parent 0c3743fd02
commit 38601dc42c
3 changed files with 12 additions and 2 deletions

View File

@ -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)

View File

@ -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;

View File

@ -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