mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 19:28:49 -04:00
Write directly to the mapped buffer to avoid an extra copy of the mesh data.
This commit is contained in:
parent
f758ba4c2e
commit
6b1ba8bbe5
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user