Split up chunk.zig and renderer.zig

I'm getting tired of scrolling around in those large files.
This commit is contained in:
IntegratedQuantum 2024-01-15 18:55:33 +01:00
parent 5160f22e5c
commit 25e4ba6b84
9 changed files with 2231 additions and 2200 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1851,33 +1851,33 @@ pub fn generateBlockTexture(blockType: u16) !Texture {
if(block.transparent()) { if(block.transparent()) {
c.glBlendEquation(c.GL_FUNC_ADD); c.glBlendEquation(c.GL_FUNC_ADD);
c.glBlendFunc(c.GL_ONE, c.GL_SRC1_COLOR); c.glBlendFunc(c.GL_ONE, c.GL_SRC1_COLOR);
main.chunk.meshing.bindTransparentShaderAndUniforms(projMatrix, .{1, 1, 1}); main.renderer.chunk_meshing.bindTransparentShaderAndUniforms(projMatrix, .{1, 1, 1});
} else { } else {
if(block.mode().model(block).modelIndex == 0) { if(block.mode().model(block).modelIndex == 0) {
main.chunk.meshing.bindShaderAndUniforms(projMatrix, .{1, 1, 1}); main.renderer.chunk_meshing.bindShaderAndUniforms(projMatrix, .{1, 1, 1});
} else { } else {
std.log.err("TODO: Item textures for non-cube models.", .{}); std.log.err("TODO: Item textures for non-cube models.", .{});
} }
} }
const uniforms = if(block.transparent()) &main.chunk.meshing.transparentUniforms else &main.chunk.meshing.uniforms; const uniforms = if(block.transparent()) &main.renderer.chunk_meshing.transparentUniforms else &main.renderer.chunk_meshing.uniforms;
var faceData: [6]main.chunk.meshing.FaceData = undefined; var faceData: [6]main.renderer.chunk_meshing.FaceData = undefined;
var faces: u8 = 0; var faces: u8 = 0;
if(block.hasBackFace()) { if(block.hasBackFace()) {
faceData[2] = main.chunk.meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirPosX, 1, 1, 1, true); faceData[2] = main.renderer.chunk_meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirPosX, 1, 1, 1, true);
faceData[1] = main.chunk.meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirUp, 1, 1, 1, true); faceData[1] = main.renderer.chunk_meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirUp, 1, 1, 1, true);
faceData[0] = main.chunk.meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirPosZ, 1, 1, 1, true); faceData[0] = main.renderer.chunk_meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirPosZ, 1, 1, 1, true);
faces += 3; faces += 3;
} }
faceData[faces + 0] = main.chunk.meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirPosX, 1+1, 1, 1, false); faceData[faces + 0] = main.renderer.chunk_meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirPosX, 1+1, 1, 1, false);
faceData[faces + 1] = main.chunk.meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirUp, 1, 1+1, 1, false); faceData[faces + 1] = main.renderer.chunk_meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirUp, 1, 1+1, 1, false);
faceData[faces + 2] = main.chunk.meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirPosZ, 1, 1, 1+1, false); faceData[faces + 2] = main.renderer.chunk_meshing.ChunkMesh.constructFaceData(block, main.chunk.Neighbors.dirPosZ, 1, 1, 1+1, false);
faces += 3; faces += 3;
for(faceData[0..faces]) |*face| { for(faceData[0..faces]) |*face| {
@memset(&face.light, ~@as(u32, 0)); @memset(&face.light, ~@as(u32, 0));
} }
var allocation: SubAllocation = .{.start = 0, .len = 0}; var allocation: SubAllocation = .{.start = 0, .len = 0};
try main.chunk.meshing.faceBuffer.uploadData(faceData[0..faces], &allocation); try main.renderer.chunk_meshing.faceBuffer.uploadData(faceData[0..faces], &allocation);
c.glUniform3f(uniforms.modelPosition, -65.5 - 1.5, -92.631 - 1.5, -65.5 - 1.5); c.glUniform3f(uniforms.modelPosition, -65.5 - 1.5, -92.631 - 1.5, -65.5 - 1.5);
c.glUniform1i(uniforms.visibilityMask, 0xff); c.glUniform1i(uniforms.visibilityMask, 0xff);
@ -1907,7 +1907,7 @@ pub fn generateBlockTexture(blockType: u16) !Texture {
c.glBindFramebuffer(c.GL_FRAMEBUFFER, 0); c.glBindFramebuffer(c.GL_FRAMEBUFFER, 0);
try main.chunk.meshing.faceBuffer.free(allocation); try main.renderer.chunk_meshing.faceBuffer.free(allocation);
c.glViewport(0, 0, main.Window.width, main.Window.height); c.glViewport(0, 0, main.Window.width, main.Window.height);
c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA); c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA);
return texture; return texture;

View File

@ -40,14 +40,14 @@ fn flawedRender() !void {
y += 8; y += 8;
try draw.print("Queue size: {}", .{main.threadPool.queueSize()}, 0, y, 8, .left); try draw.print("Queue size: {}", .{main.threadPool.queueSize()}, 0, y, 8, .left);
y += 8; y += 8;
try draw.print("Mesh Queue size: {}", .{main.renderer.RenderStructure.updatableList.items.len}, 0, y, 8, .left); try draw.print("Mesh Queue size: {}", .{main.renderer.mesh_storage.updatableList.items.len}, 0, y, 8, .left);
y += 8; y += 8;
{ {
const faceDataSize: usize = @sizeOf(main.chunk.meshing.FaceData); const faceDataSize: usize = @sizeOf(main.renderer.chunk_meshing.FaceData);
const size: usize = main.chunk.meshing.faceBuffer.capacity*faceDataSize; const size: usize = main.renderer.chunk_meshing.faceBuffer.capacity*faceDataSize;
const used: usize = main.chunk.meshing.faceBuffer.used*faceDataSize; const used: usize = main.renderer.chunk_meshing.faceBuffer.used*faceDataSize;
var largestFreeBlock: usize = 0; var largestFreeBlock: usize = 0;
for(main.chunk.meshing.faceBuffer.freeBlocks.items) |freeBlock| { for(main.renderer.chunk_meshing.faceBuffer.freeBlocks.items) |freeBlock| {
largestFreeBlock = @max(largestFreeBlock, freeBlock.len); largestFreeBlock = @max(largestFreeBlock, freeBlock.len);
} }
const fragmentation = size - used - largestFreeBlock*faceDataSize; const fragmentation = size - used - largestFreeBlock*faceDataSize;
@ -56,7 +56,7 @@ fn flawedRender() !void {
} }
try draw.print("Biome: {s}", .{main.game.world.?.playerBiome.load(.Monotonic).id}, 0, y, 8, .left); try draw.print("Biome: {s}", .{main.game.world.?.playerBiome.load(.Monotonic).id}, 0, y, 8, .left);
y += 8; y += 8;
try draw.print("Opaque faces: {}, Transparent faces: {}", .{main.chunk.meshing.quadsDrawn, main.chunk.meshing.transparentQuadsDrawn}, 0, y, 8, .left); try draw.print("Opaque faces: {}, Transparent faces: {}", .{main.renderer.chunk_meshing.quadsDrawn, main.renderer.chunk_meshing.transparentQuadsDrawn}, 0, y, 8, .left);
y += 8; y += 8;
} }
} }

View File

@ -722,17 +722,11 @@ pub fn main() !void {
try blocks.meshes.init(); try blocks.meshes.init();
defer blocks.meshes.deinit(); defer blocks.meshes.deinit();
try chunk.meshing.init();
defer chunk.meshing.deinit();
try renderer.init(); try renderer.init();
defer renderer.deinit(); defer renderer.deinit();
try network.init(); try network.init();
try renderer.RenderStructure.init();
defer renderer.RenderStructure.deinit();
try entity.ClientEntityManager.init(); try entity.ClientEntityManager.init();
defer entity.ClientEntityManager.deinit(); defer entity.ClientEntityManager.deinit();

View File

@ -747,7 +747,7 @@ pub const Protocols = struct {
block.* = Block.fromInt(std.mem.readInt(u32, data[0..4], .big)); block.* = Block.fromInt(std.mem.readInt(u32, data[0..4], .big));
data = data[4..]; data = data[4..];
} }
try renderer.RenderStructure.updateChunkMesh(ch); try renderer.mesh_storage.updateChunkMesh(ch);
} }
fn sendChunkOverTheNetwork(conn: *Connection, ch: *chunk.Chunk) Allocator.Error!void { fn sendChunkOverTheNetwork(conn: *Connection, ch: *chunk.Chunk) Allocator.Error!void {
var uncompressedData: [@sizeOf(@TypeOf(ch.blocks))]u8 = undefined; // TODO: #15280 var uncompressedData: [@sizeOf(@TypeOf(ch.blocks))]u8 = undefined; // TODO: #15280
@ -769,7 +769,7 @@ pub const Protocols = struct {
const chunkCopy = try main.globalAllocator.create(chunk.Chunk); const chunkCopy = try main.globalAllocator.create(chunk.Chunk);
chunkCopy.init(ch.pos); chunkCopy.init(ch.pos);
@memcpy(&chunkCopy.blocks, &ch.blocks); @memcpy(&chunkCopy.blocks, &ch.blocks);
try renderer.RenderStructure.updateChunkMesh(chunkCopy); try renderer.mesh_storage.updateChunkMesh(chunkCopy);
} }
pub fn sendChunk(conn: *Connection, ch: *chunk.Chunk) Allocator.Error!void { pub fn sendChunk(conn: *Connection, ch: *chunk.Chunk) Allocator.Error!void {
if(conn.user.?.isLocal) { if(conn.user.?.isLocal) {
@ -854,7 +854,7 @@ pub const Protocols = struct {
if(conn.user != null) { if(conn.user != null) {
// TODO: Handle block update from the client. // TODO: Handle block update from the client.
} else { } else {
try renderer.RenderStructure.updateBlock(x, y, z, newBlock); try renderer.mesh_storage.updateBlock(x, y, z, newBlock);
} }
} }
pub fn send(conn: *Connection, x: i32, y: i32, z: i32, newBlock: Block) !void { pub fn send(conn: *Connection, x: i32, y: i32, z: i32, newBlock: Block) !void {
@ -1280,7 +1280,7 @@ pub const Protocols = struct {
val.* = std.mem.readInt(i16, data[0..2], .big); val.* = std.mem.readInt(i16, data[0..2], .big);
data = data[2..]; data = data[2..];
} }
try renderer.RenderStructure.updateLightMap(map); try renderer.mesh_storage.updateLightMap(map);
} }
pub fn sendLightMap(conn: *Connection, map: *main.server.terrain.LightMap.LightMapFragment) Allocator.Error!void { pub fn sendLightMap(conn: *Connection, map: *main.server.terrain.LightMap.LightMapFragment) Allocator.Error!void {
var uncompressedData: [@sizeOf(@TypeOf(map.startHeight))]u8 = undefined; // TODO: #15280 var uncompressedData: [@sizeOf(@TypeOf(map.startHeight))]u8 = undefined; // TODO: #15280

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,8 @@ const Atomic = std.atomic.Value;
const main = @import("root"); const main = @import("root");
const blocks = main.blocks; const blocks = main.blocks;
const chunk = main.chunk; const chunk = main.chunk;
const chunk_meshing = @import("chunk_meshing.zig");
const mesh_storage = @import("mesh_storage.zig");
const Channel = enum(u8) { const Channel = enum(u8) {
sun_red = 0, sun_red = 0,
@ -56,7 +58,7 @@ pub const ChannelChunk = struct {
}; };
const ChunkEntries = struct { const ChunkEntries = struct {
mesh: ?*chunk.meshing.ChunkMesh, mesh: ?*chunk_meshing.ChunkMesh,
entries: std.ArrayListUnmanaged(PositionEntry), entries: std.ArrayListUnmanaged(PositionEntry),
}; };
@ -95,13 +97,13 @@ pub const ChannelChunk = struct {
} }
} }
self.mutex.unlock(); self.mutex.unlock();
if(main.renderer.RenderStructure.getMeshAndIncreaseRefCount(self.ch.pos)) |mesh| { if(mesh_storage.getMeshAndIncreaseRefCount(self.ch.pos)) |mesh| {
try mesh.scheduleLightRefreshAndDecreaseRefCount(); try mesh.scheduleLightRefreshAndDecreaseRefCount();
} }
for(0..6) |neighbor| { for(0..6) |neighbor| {
if(neighborLists[neighbor].items.len == 0) continue; if(neighborLists[neighbor].items.len == 0) continue;
const neighborMesh = main.renderer.RenderStructure.getNeighborAndIncreaseRefCount(self.ch.pos, self.ch.pos.voxelSize, @intCast(neighbor)) orelse continue; const neighborMesh = mesh_storage.getNeighborAndIncreaseRefCount(self.ch.pos, self.ch.pos.voxelSize, @intCast(neighbor)) orelse continue;
defer neighborMesh.decreaseRefCount(); defer neighborMesh.decreaseRefCount();
try neighborMesh.lightingData[@intFromEnum(self.channel)].propagateFromNeighbor(neighborLists[neighbor].items); try neighborMesh.lightingData[@intFromEnum(self.channel)].propagateFromNeighbor(neighborLists[neighbor].items);
} }
@ -150,13 +152,13 @@ pub const ChannelChunk = struct {
} }
} }
self.mutex.unlock(); self.mutex.unlock();
if(main.renderer.RenderStructure.getMeshAndIncreaseRefCount(self.ch.pos)) |mesh| { if(mesh_storage.getMeshAndIncreaseRefCount(self.ch.pos)) |mesh| {
try mesh.scheduleLightRefreshAndDecreaseRefCount(); try mesh.scheduleLightRefreshAndDecreaseRefCount();
} }
for(0..6) |neighbor| { for(0..6) |neighbor| {
if(neighborLists[neighbor].items.len == 0) continue; if(neighborLists[neighbor].items.len == 0) continue;
const neighborMesh = main.renderer.RenderStructure.getNeighborAndIncreaseRefCount(self.ch.pos, self.ch.pos.voxelSize, @intCast(neighbor)) orelse continue; const neighborMesh = mesh_storage.getNeighborAndIncreaseRefCount(self.ch.pos, self.ch.pos.voxelSize, @intCast(neighbor)) orelse continue;
try constructiveEntries.append(main.globalAllocator, .{ try constructiveEntries.append(main.globalAllocator, .{
.mesh = neighborMesh, .mesh = neighborMesh,
.entries = try neighborMesh.lightingData[@intFromEnum(self.channel)].propagateDestructiveFromNeighbor(neighborLists[neighbor].items, constructiveEntries), .entries = try neighborMesh.lightingData[@intFromEnum(self.channel)].propagateDestructiveFromNeighbor(neighborLists[neighbor].items, constructiveEntries),
@ -231,7 +233,7 @@ pub const ChannelChunk = struct {
const otherX = x+%chunk.Neighbors.relX[neighbor] & chunk.chunkMask; const otherX = x+%chunk.Neighbors.relX[neighbor] & chunk.chunkMask;
const otherY = y+%chunk.Neighbors.relY[neighbor] & chunk.chunkMask; const otherY = y+%chunk.Neighbors.relY[neighbor] & chunk.chunkMask;
const otherZ = z+%chunk.Neighbors.relZ[neighbor] & chunk.chunkMask; const otherZ = z+%chunk.Neighbors.relZ[neighbor] & chunk.chunkMask;
const neighborMesh = main.renderer.RenderStructure.getNeighborAndIncreaseRefCount(self.ch.pos, self.ch.pos.voxelSize, @intCast(neighbor)) orelse continue; const neighborMesh = mesh_storage.getNeighborAndIncreaseRefCount(self.ch.pos, self.ch.pos.voxelSize, @intCast(neighbor)) orelse continue;
defer neighborMesh.decreaseRefCount(); defer neighborMesh.decreaseRefCount();
const neighborLightChunk = &neighborMesh.lightingData[@intFromEnum(self.channel)]; const neighborLightChunk = &neighborMesh.lightingData[@intFromEnum(self.channel)];
const index = chunk.getIndex(x, y, z); const index = chunk.getIndex(x, y, z);

File diff suppressed because it is too large Load Diff