Jarvis, Optimise my shit

This commit is contained in:
Halbear 2026-09-25 23:37:22 +01:00
parent d1abde919d
commit 9424b29d0a
50 changed files with 1469 additions and 1232 deletions

View file

@ -3,10 +3,10 @@
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
const int CHUNK_SIZE = 16;
const int CHUNK_AREA = CHUNK_SIZE * CHUNK_SIZE;
const int VOXEL_COUNT = CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE;
const int SCRATCH_SIZE = CHUNK_SIZE + 2;
const int SCRATCH_AREA = SCRATCH_SIZE * SCRATCH_SIZE;
const int SCRATCH_VOXEL_COUNT = SCRATCH_SIZE * SCRATCH_SIZE * SCRATCH_SIZE;
const uint VERTICES_PER_FACE = 6u;
const uint MAX_VISIBLE_FACES_PER_CHUNK = uint(CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE * 6);
layout(std430, binding = 0) readonly buffer VoxelData {
@ -17,19 +17,8 @@ layout(std430, binding = 1) buffer FaceBuffer {
uint faces[];
};
struct DrawCommand {
uint vertexCount;
uint instanceCount;
uint firstVertex;
uint firstInstance;
};
layout(std430, binding = 2) buffer DrawCommands {
DrawCommand drawCmds[];
};
layout(std430, binding = 3) buffer Counters {
uint faceCount;
layout(std430, binding = 2) buffer Counters {
uint faceCount[];
} counters;
struct Voxel {
@ -52,23 +41,23 @@ layout(push_constant) uniform ChunkInfo {
int slot;
uint faceOffset;
uint voxelTypeCount;
uint indirectCommandIndex;
uint biomeCount;
uint structureCount;
uint worldSeed;
};
uint flatten(ivec3 pos) {
return uint((pos.x * CHUNK_AREA) + (pos.y * CHUNK_SIZE) + pos.z);
pos += ivec3(1);
return uint(slot * SCRATCH_VOXEL_COUNT + pos.x * SCRATCH_AREA + pos.y * SCRATCH_SIZE + pos.z);
}
bool isSeeThrough(Voxel voxel, uint type) {
if (type == 0u || voxel.BlockType == 3u || type > voxelTypeCount) return true;
return voxelReg.materials[type - 1u].BlockType >= 1u;
}
uint getVoxel(ivec3 pos) {
if (pos.x < 0 || pos.x >= CHUNK_SIZE) return 0u;
if (pos.y < 0 || pos.y >= CHUNK_SIZE) return 0u;
if (pos.z < 0 || pos.z >= CHUNK_SIZE) return 0u;
if (pos.x < -1 || pos.x > CHUNK_SIZE) return 0u;
if (pos.y < -1 || pos.y > CHUNK_SIZE) return 0u;
if (pos.z < -1 || pos.z > CHUNK_SIZE) return 0u;
return voxels[flatten(pos)];
}
@ -175,7 +164,7 @@ void emitFace(ivec3 voxelPos, uint faceIndex, uint voxelType) {
return;
}
uint localFace = atomicAdd(counters.faceCount, 1u);
uint localFace = atomicAdd(counters.faceCount[slot], 1u);
if (localFace >= MAX_VISIBLE_FACES_PER_CHUNK) {
return;
@ -183,8 +172,6 @@ void emitFace(ivec3 voxelPos, uint faceIndex, uint voxelType) {
Voxel voxel = voxelReg.materials[voxelType - 1u];
faces[faceOffset + localFace] = packFace(uvec3(voxelPos), faceIndex, voxel);
atomicAdd(drawCmds[indirectCommandIndex].vertexCount, VERTICES_PER_FACE);
}
void main() {

View file

@ -2,19 +2,8 @@
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
struct DrawCommand {
uint vertexCount;
uint instanceCount;
uint firstVertex;
uint firstInstance;
};
layout(std430, binding = 0) buffer DrawCommands {
DrawCommand drawCmds[];
};
layout(std430, binding = 1) buffer Counters {
uint faceCount;
layout(std430, binding = 0) buffer Counters {
uint faceCount[];
} counters;
layout(push_constant) uniform ChunkInfo {
@ -22,17 +11,11 @@ layout(push_constant) uniform ChunkInfo {
int slot;
uint faceOffset;
uint voxelTypeCount;
uint indirectCommandIndex;
uint biomeCount;
uint structureCount;
uint worldSeed;
};
void main() {
drawCmds[indirectCommandIndex].vertexCount = 0u;
drawCmds[indirectCommandIndex].instanceCount = 1u;
drawCmds[indirectCommandIndex].firstVertex = faceOffset * 6u;
drawCmds[indirectCommandIndex].firstInstance = 0u;
counters.faceCount = 0u;
counters.faceCount[slot] = 0u;
}

View file

@ -2,7 +2,9 @@
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
const int CHUNK_SIZE = 16;
const int CHUNK_AREA = CHUNK_SIZE * CHUNK_SIZE;
const int SCRATCH_SIZE = CHUNK_SIZE + 2;
const int SCRATCH_AREA = SCRATCH_SIZE * SCRATCH_SIZE;
const int SCRATCH_VOXEL_COUNT = SCRATCH_SIZE * SCRATCH_SIZE * SCRATCH_SIZE;
const int STRUCTURE_CELL_SIZE = 16;
const int REPLACE_AIR_ONLY = 1;
const int REPLACE_FOLIAGE = 2;
@ -14,7 +16,6 @@ layout(push_constant) uniform ChunkInfo {
int slot;
uint faceOffset;
uint voxelTypeCount;
uint indirectCommandIndex;
uint biomeCount;
uint structureCount;
uint worldSeed;
@ -453,35 +454,44 @@ StructureVoxelResult getStructureVoxelAt(ivec3 worldPos, uint biome) {
ivec3 baseCell = getStructureCell3D(worldPos, spacing);
for (int ox = -1; ox <= 1; ox++) {
for (int oz = -1; oz <= 1; oz++) {
ivec3 cell = baseCell + ivec3(ox, 0, oz);
for (int oy = -1; oy <= 1; oy++) {
for (int oz = -1; oz <= 1; oz++) {
ivec3 cell = baseCell + ivec3(ox, oy, oz);
uint seed = hash31(cell + ivec3(0, int(structureId) * 97, 0)) ^ worldSeed;
uint seed = hash31(cell + ivec3(0, int(structureId) * 97, 0)) ^ worldSeed;
if (!cellSpawnsStructure(seed, s)) {
continue;
}
if (!cellSpawnsStructure(seed, s)) {
continue;
}
ivec2 anchorXZ = getStructureAnchorXZ(cell.xz, seed, s.spawnSpacing);
ivec2 anchorXZ = getStructureAnchorXZ(cell.xz, seed, s.spawnSpacing);
int referenceY = cell.y * 75;
float anchorHeight = getSurfaceHeight(anchorXZ.x, anchorXZ.y, referenceY);
int anchorY = int(floor(anchorHeight)) + 1;
// Match getStructureBlock's uncentered footprint before sampling terrain.
ivec2 localXZ = worldPos.xz - anchorXZ;
if (localXZ.x < 0 || localXZ.x >= int(s.sizeX) ||
localXZ.y < 0 || localXZ.y >= int(s.sizeZ)) {
continue;
}
ivec3 origin = ivec3(anchorXZ.x, anchorY, anchorXZ.y);
int referenceY = cell.y * 75;
float anchorHeight = getSurfaceHeight(int(anchorXZ.x - s.sizeX/2), int(anchorXZ.y - s.sizeZ/2), referenceY);
int anchorY = int(floor(anchorHeight)) + 1;
if (!isValidStructureAnchor(origin, structureId, biome)) {
continue;
}
ivec3 origin = ivec3(anchorXZ.x, anchorY, anchorXZ.y);
ivec3 local = worldPos - origin;
if (!isValidStructureAnchor(origin, structureId, biome)) {
continue;
}
uint block = getStructureBlock(structureId, local);
ivec3 local = worldPos - origin;
if (block != 0u) {
result.block = block;
result.flags = s.flags;
return result;
uint block = getStructureBlock(structureId, local);
if (block != 0u) {
result.block = block;
result.flags = s.flags;
return result;
}
}
}
}
@ -491,10 +501,13 @@ StructureVoxelResult getStructureVoxelAt(ivec3 worldPos, uint biome) {
}
void main() {
ivec3 localPos = ivec3(gl_GlobalInvocationID.xyz);
if (any(greaterThanEqual(gl_GlobalInvocationID.xyz, uvec3(SCRATCH_SIZE)))) {
return;
}
ivec3 localPos = ivec3(gl_GlobalInvocationID.xyz) - ivec3(1);
ivec3 worldPos = chunkPos * CHUNK_SIZE + localPos;
uint biome = GetBiome(vec3(worldPos.z, worldPos.y, worldPos.x) * 0.05, biomeCount);
uint biome = GetBiome(vec3(worldPos.z, worldPos.y, worldPos.x) * 0.005, biomeCount);
Biome currentBiome = BiomeReg.biomes[biome];
float YZone = floor(worldPos.y/150.0)*150.0;
@ -577,6 +590,7 @@ void main() {
}
}
uint index = uint((localPos.x * CHUNK_AREA) + (localPos.y * CHUNK_SIZE) + localPos.z);
uint index = uint(slot * SCRATCH_VOXEL_COUNT + (localPos.x + 1) * SCRATCH_AREA +
(localPos.y + 1) * SCRATCH_SIZE + localPos.z + 1);
voxels[index] = voxelType;
}