Write directly to the mapped buffer to avoid an extra copy of the mesh data.

This commit is contained in:
IntegratedQuantum 2023-11-29 12:48:33 +01:00
parent f758ba4c2e
commit 6b1ba8bbe5
2 changed files with 23 additions and 3 deletions

View File

@ -704,8 +704,8 @@ pub const meshing = struct {
} }
offset += neighborLen; offset += neighborLen;
} }
const fullBuffer = try main.stackAllocator.alloc(FaceData, len); const fullBuffer = try faceBuffer.allocateAndMapRange(len, &self.bufferAllocation);
defer main.stackAllocator.free(fullBuffer); defer faceBuffer.unmapRange(fullBuffer);
@memcpy(fullBuffer[0..self.coreLen], self.completeList[0..self.coreLen]); @memcpy(fullBuffer[0..self.coreLen], self.completeList[0..self.coreLen]);
var i: usize = self.coreLen; var i: usize = self.coreLen;
for(0..6) |n| { for(0..6) |n| {
@ -713,7 +713,6 @@ pub const meshing = struct {
i += list[n].len; i += list[n].len;
} }
self.vertexCount = @intCast(6*fullBuffer.len); self.vertexCount = @intCast(6*fullBuffer.len);
try faceBuffer.uploadData(fullBuffer, &self.bufferAllocation); // TODO: Avoid the extra copy by writing to the memory-mapped buffer directly.
self.wasChanged = true; self.wasChanged = true;
} }

View File

@ -1323,6 +1323,27 @@ pub fn LargeBuffer(comptime Entry: type) type {
try self.fencedFreeLists[self.activeFence].append(allocation); try self.fencedFreeLists[self.activeFence].append(allocation);
} }
/// Must unmap after use!
pub fn allocateAndMapRange(self: *Self, len: usize, allocation: *SubAllocation) ![]Entry {
try self.free(allocation.*);
if(len == 0) {
allocation.len = 0;
return &.{};
}
allocation.* = try self.alloc(@intCast(len));
c.glBindBuffer(c.GL_SHADER_STORAGE_BUFFER, self.ssbo.bufferID);
const ptr: [*]Entry = @ptrCast(@alignCast(
c.glMapBufferRange(c.GL_SHADER_STORAGE_BUFFER, allocation.start*@sizeOf(Entry), allocation.len*@sizeOf(Entry), c.GL_MAP_WRITE_BIT | c.GL_MAP_INVALIDATE_RANGE_BIT)
));
return ptr[0..len];
}
pub fn unmapRange(self: *Self, range: []Entry) void {
if(range.len == 0) return;
c.glBindBuffer(c.GL_SHADER_STORAGE_BUFFER, self.ssbo.bufferID);
std.debug.assert(c.glUnmapBuffer(c.GL_SHADER_STORAGE_BUFFER) == c.GL_TRUE);
}
pub fn uploadData(self: *Self, data: []Entry, allocation: *SubAllocation) !void { pub fn uploadData(self: *Self, data: []Entry, allocation: *SubAllocation) !void {
try self.free(allocation.*); try self.free(allocation.*);
if(data.len == 0) { if(data.len == 0) {