voxel biomes and CPU pushed blocks

This commit is contained in:
Halbear 2026-09-20 17:57:19 +01:00
parent 7fda2614d7
commit a0b00b0f1c
24 changed files with 1007 additions and 91 deletions

View file

@ -5,6 +5,8 @@ import net.halbear.Terrain4J.EngineCore.Logic.EngineInstance;
import net.halbear.Terrain4J.EngineCore.Logic.InitData;
import net.halbear.Terrain4J.EngineCore.Logic.Rendering.SkyBox;
import net.halbear.Terrain4J.EngineCore.Logic.Rendering.VulkanContext;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Computing.VoxelTerrainGeneration.Biome;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Computing.VoxelTerrainGeneration.Voxel;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Computing.VoxelTerrainGeneration.VoxelChunkGenerator;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Computing.VoxelTerrainGeneration.VoxelWorldManager;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Rendering.RenderPasses.GUI.GuiRenderer;
@ -30,6 +32,7 @@ import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Structure.SwapChain.
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Structure.SwapChain.SwapChainRender;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Util.VulkanUtils;
import net.halbear.Terrain4J.EngineCore.Threads.RenderThread;
import org.joml.Vector2i;
import org.joml.Vector3f;
import org.joml.Vector3i;
import org.lwjgl.system.MemoryStack;
@ -188,6 +191,190 @@ public class VulkanRenderer implements Renderer {
GuiRender.LoadTextures(RendererContext,initData.GuiTextures(),textureCache);
//VoxelWorldManager.Init(RendererContext);
// VoxelWorldManager.GenerateChunks(new Vector3i(0,0,0), new Vector3i(16,0,16));
Vector2i positionArr[][] = new Vector2i[4][4];
for(int x = 0; x < 4; x++){
for(int y = 0; y < 4; y++){
positionArr[x][y] = new Vector2i(x,y);
}
}
int Index = materialsCache.GetPosition("VoxelTerrain");
int Index2 = materialsCache.GetPosition("Blueleaf");
int Index3 = materialsCache.GetPosition("Blueleaf2");
Voxel grassVoxel = new Voxel(Index, 0,
new Vector2i[]{positionArr[0][1], positionArr[0][3]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[0][0], positionArr[0][0]},
new Vector2i[]{positionArr[0][0], positionArr[0][0]},
new Vector2i[]{positionArr[0][0], positionArr[0][0]},
new Vector2i[]{positionArr[0][0], positionArr[0][0]});
Voxel stoneVoxel = new Voxel(Index, 0,
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]});
Voxel grassSideVoxel = new Voxel(Index, 1,
new Vector2i[]{positionArr[1][1], positionArr[1][3]},
new Vector2i[]{positionArr[1][1], positionArr[1][3]},
new Vector2i[]{positionArr[1][1], positionArr[1][3]},
new Vector2i[]{positionArr[1][1], positionArr[1][3]},
new Vector2i[]{positionArr[1][1], positionArr[1][3]},
new Vector2i[]{positionArr[1][1], positionArr[1][3]});
Voxel Mushroom = new Voxel(Index, 1,
new Vector2i[]{positionArr[2][3], positionArr[2][3]},
new Vector2i[]{positionArr[2][3], positionArr[2][3]},
new Vector2i[]{positionArr[2][3], positionArr[2][3]},
new Vector2i[]{positionArr[2][3], positionArr[2][3]},
new Vector2i[]{positionArr[2][3], positionArr[2][3]},
new Vector2i[]{positionArr[2][3], positionArr[2][3]});
Voxel Log = new Voxel(Index, 0,
new Vector2i[]{positionArr[2][1], positionArr[2][1]},
new Vector2i[]{positionArr[2][1], positionArr[2][1]},
new Vector2i[]{positionArr[2][2], positionArr[2][2]},
new Vector2i[]{positionArr[2][2], positionArr[2][2]},
new Vector2i[]{positionArr[2][2], positionArr[2][2]},
new Vector2i[]{positionArr[2][2], positionArr[2][2]});
Voxel Leaves = new Voxel(Index, 2,
new Vector2i[]{positionArr[2][0], positionArr[2][0]},
new Vector2i[]{positionArr[2][0], positionArr[2][0]},
new Vector2i[]{positionArr[2][0], positionArr[2][0]},
new Vector2i[]{positionArr[2][0], positionArr[2][0]},
new Vector2i[]{positionArr[2][0], positionArr[2][0]},
new Vector2i[]{positionArr[2][0], positionArr[2][0]});
Voxel bluegrassVoxel = new Voxel(Index2, 0,
new Vector2i[]{positionArr[0][1], positionArr[1][3]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[0][0], positionArr[0][0]},
new Vector2i[]{positionArr[0][0], positionArr[0][0]},
new Vector2i[]{positionArr[0][0], positionArr[0][0]},
new Vector2i[]{positionArr[0][0], positionArr[0][0]});
Voxel blueDirtVoxel = new Voxel(Index2, 0,
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]},
new Vector2i[]{positionArr[1][0], positionArr[1][0]});
Voxel bluegrassSideVoxel = new Voxel(Index3, 1,
new Vector2i[]{positionArr[0][0], positionArr[1][2]},
new Vector2i[]{positionArr[0][0], positionArr[1][2]},
new Vector2i[]{positionArr[0][0], positionArr[1][2]},
new Vector2i[]{positionArr[0][0], positionArr[1][2]},
new Vector2i[]{positionArr[0][0], positionArr[1][2]},
new Vector2i[]{positionArr[0][0], positionArr[1][2]});
Voxel blueFlower1 = new Voxel(Index2, 1,
new Vector2i[]{positionArr[2][0], positionArr[2][0]},
new Vector2i[]{positionArr[2][0], positionArr[2][0]},
new Vector2i[]{positionArr[2][0], positionArr[2][0]},
new Vector2i[]{positionArr[2][0], positionArr[2][0]},
new Vector2i[]{positionArr[2][0], positionArr[2][0]},
new Vector2i[]{positionArr[2][0], positionArr[2][0]});
Voxel blueFlower2 = new Voxel(Index2, 1,
new Vector2i[]{positionArr[2][1], positionArr[2][1]},
new Vector2i[]{positionArr[2][1], positionArr[2][1]},
new Vector2i[]{positionArr[2][1], positionArr[2][1]},
new Vector2i[]{positionArr[2][1], positionArr[2][1]},
new Vector2i[]{positionArr[2][1], positionArr[2][1]},
new Vector2i[]{positionArr[2][1], positionArr[2][1]});
Voxel blueFlower3 = new Voxel(Index2, 1,
new Vector2i[]{positionArr[2][2], positionArr[2][2]},
new Vector2i[]{positionArr[2][2], positionArr[2][2]},
new Vector2i[]{positionArr[2][2], positionArr[2][2]},
new Vector2i[]{positionArr[2][2], positionArr[2][2]},
new Vector2i[]{positionArr[2][2], positionArr[2][2]},
new Vector2i[]{positionArr[2][2], positionArr[2][2]});
Voxel blueFlower4 = new Voxel(Index2, 1,
new Vector2i[]{positionArr[2][3], positionArr[2][3]},
new Vector2i[]{positionArr[2][3], positionArr[2][3]},
new Vector2i[]{positionArr[2][3], positionArr[2][3]},
new Vector2i[]{positionArr[2][3], positionArr[2][3]},
new Vector2i[]{positionArr[2][3], positionArr[2][3]},
new Vector2i[]{positionArr[2][3], positionArr[2][3]});
Voxel blueFlower5 = new Voxel(Index2, 1,
new Vector2i[]{positionArr[3][0], positionArr[3][0]},
new Vector2i[]{positionArr[3][0], positionArr[3][0]},
new Vector2i[]{positionArr[3][0], positionArr[3][0]},
new Vector2i[]{positionArr[3][0], positionArr[3][0]},
new Vector2i[]{positionArr[3][0], positionArr[3][0]},
new Vector2i[]{positionArr[3][0], positionArr[3][0]});
Voxel blueFlower6 = new Voxel(Index2, 1,
new Vector2i[]{positionArr[3][1], positionArr[3][1]},
new Vector2i[]{positionArr[3][1], positionArr[3][1]},
new Vector2i[]{positionArr[3][1], positionArr[3][1]},
new Vector2i[]{positionArr[3][1], positionArr[3][1]},
new Vector2i[]{positionArr[3][1], positionArr[3][1]},
new Vector2i[]{positionArr[3][1], positionArr[3][1]});
Voxel blueFlower7 = new Voxel(Index2, 1,
new Vector2i[]{positionArr[3][2], positionArr[3][2]},
new Vector2i[]{positionArr[3][2], positionArr[3][2]},
new Vector2i[]{positionArr[3][2], positionArr[3][2]},
new Vector2i[]{positionArr[3][2], positionArr[3][2]},
new Vector2i[]{positionArr[3][2], positionArr[3][2]},
new Vector2i[]{positionArr[3][2], positionArr[3][2]});
Voxel blueFlower8 = new Voxel(Index2, 1,
new Vector2i[]{positionArr[3][3], positionArr[3][3]},
new Vector2i[]{positionArr[3][3], positionArr[3][3]},
new Vector2i[]{positionArr[3][3], positionArr[3][3]},
new Vector2i[]{positionArr[3][3], positionArr[3][3]},
new Vector2i[]{positionArr[3][3], positionArr[3][3]},
new Vector2i[]{positionArr[3][3], positionArr[3][3]});
Voxel blueLeaves1 = new Voxel(Index3, 2,
new Vector2i[]{positionArr[0][3], positionArr[0][3]},
new Vector2i[]{positionArr[0][3], positionArr[0][3]},
new Vector2i[]{positionArr[0][3], positionArr[0][3]},
new Vector2i[]{positionArr[0][3], positionArr[0][3]},
new Vector2i[]{positionArr[0][3], positionArr[0][3]},
new Vector2i[]{positionArr[0][3], positionArr[0][3]});
Voxel blueLeaves2 = new Voxel(Index3, 2,
new Vector2i[]{positionArr[1][3], positionArr[1][3]},
new Vector2i[]{positionArr[1][3], positionArr[1][3]},
new Vector2i[]{positionArr[1][3], positionArr[1][3]},
new Vector2i[]{positionArr[1][3], positionArr[1][3]},
new Vector2i[]{positionArr[1][3], positionArr[1][3]},
new Vector2i[]{positionArr[1][3], positionArr[1][3]});
VoxelWorldManager.AddVoxel(grassVoxel, "grass");//1
VoxelWorldManager.AddVoxel(stoneVoxel, "DirtVoxel");//2
VoxelWorldManager.AddVoxel(stoneVoxel, "StoneVoxel");//3
VoxelWorldManager.AddVoxel(grassSideVoxel, "grassSideVoxel");//4
VoxelWorldManager.AddVoxel(Mushroom, "mushroom");//5
VoxelWorldManager.AddVoxel(Log, "log");//6
VoxelWorldManager.AddVoxel(Leaves, "leaves");//7
VoxelWorldManager.AddVoxel(bluegrassVoxel, "bluegrassVoxel");//8
VoxelWorldManager.AddVoxel(blueDirtVoxel, "blueDirtVoxel");//9
VoxelWorldManager.AddVoxel(bluegrassSideVoxel, "bluegrassSideVoxel");//10
VoxelWorldManager.AddVoxel(blueFlower1, "blueFlower1");//11
VoxelWorldManager.AddVoxel(blueFlower2, "blueFlower2");//12
VoxelWorldManager.AddVoxel(blueFlower3, "blueFlower3");//13
VoxelWorldManager.AddVoxel(blueFlower4, "blueFlower4");//14
VoxelWorldManager.AddVoxel(blueFlower5, "blueFlower5");//15
VoxelWorldManager.AddVoxel(blueFlower6, "blueFlower6");//16
VoxelWorldManager.AddVoxel(blueFlower7, "blueFlower7");//17
VoxelWorldManager.AddVoxel(blueFlower8, "blueFlower8");//18
VoxelWorldManager.AddVoxel(blueLeaves1, "blueLeaves1");//19
VoxelWorldManager.AddVoxel(blueLeaves2, "blueLeaves2");//20
Biome GreenBiome = new Biome(1,2,3,4,new int[]{5,6,7,4},new int[]{4,4,4,4},new int[]{4,18,16,17});
Biome Blueleaf = new Biome(8,9,3,10,new int[]{11,19,20,14},new int[]{5,6,7,8},new int[]{15,16,17,18});
VoxelWorldManager.AddBiome(Blueleaf, "blueleaf");
VoxelWorldManager.AddBiome(GreenBiome, "greenBiome");
}
public GuiRenderer GetGUIRenderer(){return GuiRender;}

View file

@ -163,8 +163,14 @@ public class GameCore implements GameLogic {
boolean createOfflinePlayer = !PrimaryRuntime.IsServer && !ClientSideNetworkUtils.Connected;
MaterialData VoxelMat = new MaterialData("VoxelTerrain","resources/EngineResources/Texture/test_cube.png","","",new Vector4f(1,1,1,1),
0,0,"",new Vector4f(),"",0,"",1,new Vector4f(),1.0f,0);
0,0,"",new Vector4f(),"resources/EngineResources/Texture/test_cube_translucency.png",0,"",1,new Vector4f(),1.0f,0);
MaterialData BlueleafMat = new MaterialData("Blueleaf","resources/EngineResources/Texture/blueleaf_set.png","","",new Vector4f(1,1,1,1),
0,0,"",new Vector4f(),"resources/EngineResources/Texture/blueleaf_set_translucent.png",0,"",1,new Vector4f(),1.0f,0);
MaterialData BlueleafMat2 = new MaterialData("Blueleaf2","resources/EngineResources/Texture/blueleaf_set_2.png","","",new Vector4f(1,1,1,1),
0,0,"",new Vector4f(),"resources/EngineResources/Texture/blueleaf_set_2_translucent.png",0,"",1,new Vector4f(),1.0f,0);
materials.add(VoxelMat);
materials.add(BlueleafMat);
materials.add(BlueleafMat2);
materials.addAll(MelonaMat);
materials.addAll(CollisionVisualisationMat);
materials.addAll(MelonaMaterial);
@ -681,11 +687,11 @@ public class GameCore implements GameLogic {
Vector3f CamRot = cam.GetRotation();
Vector3i ChunkPos = new Vector3i((int)Math.floor(CamPos.x/ (float) VoxelChunkGenerator.CHUNK_SIZE),(int)Math.floor(CamPos.y/ (float)VoxelChunkGenerator.CHUNK_SIZE),(int)Math.floor(CamPos.z/ (float)VoxelChunkGenerator.CHUNK_SIZE));
if(ChunkPos.x != VoxelWorldManager.LastPosition.x || ChunkPos.y != VoxelWorldManager.LastPosition.y || ChunkPos.z != VoxelWorldManager.LastPosition.z || CamRot.x != LastCamRotation.x || CamRot.y != LastCamRotation.y || CamRot.z != LastCamRotation.z) {
VoxelWorldManager.UnloadFarChunks(ChunkPos, 12,cam.GetViewMatrix(),projection.GetProjectionMatrix());
VoxelWorldManager.GenerateChunks(ChunkPos, new Vector3i(12,8,12),cam.GetViewMatrix(),projection.GetProjectionMatrix());
VoxelWorldManager.UnloadFarChunks(ChunkPos, 16,cam.GetViewMatrix(),projection.GetProjectionMatrix());
VoxelWorldManager.GenerateChunks(ChunkPos, new Vector3i(16,16,16),cam.GetViewMatrix(),projection.GetProjectionMatrix());
} else {
VoxelWorldManager.UnloadFarChunks(ChunkPos, 24,cam.GetViewMatrix(),projection.GetProjectionMatrix());
VoxelWorldManager.GenerateChunks(ChunkPos, new Vector3i(24,12,24),cam.GetViewMatrix(),projection.GetProjectionMatrix());
VoxelWorldManager.UnloadFarChunks(ChunkPos, 96,cam.GetViewMatrix(),projection.GetProjectionMatrix());
VoxelWorldManager.GenerateChunks(ChunkPos, new Vector3i(48,16,48),cam.GetViewMatrix(),projection.GetProjectionMatrix());
}
LastCamRotation.set(CamRot);
if(PrimaryRuntime.IsServer){

View file

@ -163,9 +163,9 @@ CreateNewClientConnection();
Logger.debug("UnThrottlingEngine");
FrameAccuracy= OriginalFrameAccuracy;
}
PrimaryThread.UpdateFrameAccuracy();
DrawingThread.UpdateFrameAccuracy();
FastThread.UpdateFrameAccuracy();
if(PrimaryThread != null)PrimaryThread.UpdateFrameAccuracy();
if(DrawingThread != null)DrawingThread.UpdateFrameAccuracy();
if(FastThread != null)FastThread.UpdateFrameAccuracy();
}
public void UpdateTickRateThreshold(){
CappedToTickRate = EngineConfig.getInstance().GetPrimaryTickCap();

View file

@ -0,0 +1,4 @@
package net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Computing.VoxelTerrainGeneration;
public record Biome(int surfaceBlock, int dirtBlock, int stoneBlock, int grassFoliage, int[] foliageBlocks, int[] tallBlocks, int[] otherFoliage) {
}

View file

@ -8,8 +8,13 @@ import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Rendering.Pipeline.P
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Rendering.Shader.*;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Structure.DeviceLayers.Device;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Util.VulkanBuffer;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.util.shaderc.Shaderc;
import java.nio.ByteBuffer;
import java.util.List;
import static org.lwjgl.util.vma.Vma.VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
import static org.lwjgl.util.vma.Vma.VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
import static org.lwjgl.vulkan.VK13.*;
@ -22,20 +27,31 @@ public class SharedVoxelTerrainResources {
public static final int TERRAIN_GENERATION_PUSH_CONSTANT_SIZE = Integer.BYTES * 8;
public static final int MAX_VOXEL_TYPES = 256;
public static final int MAX_BIOME_TYPES = 256;
public static final int VOXEL_REG_DATA_SIZE = 104;
public static final int BIOME_REG_DATA_SIZE = 64;
private static final String DESC_ID_VOXEL_GENERATION = "VOXEL_SHARED_DESC_GENERATION";
private static final String DESC_ID_RESET = "VOXEL_SHARED_DESC_RESET";
private static final String DESC_ID_MESH = "VOXEL_SHARED_DESC_MESH";
private static final String DESC_ID_FACE_GRAPHICS = "VOXEL_SHARED_DESC_FACE_GRAPHICS";
private static final String DESC_ID_VOXEL_REG = "VOXEL_REGISTRY";
private static final String DESC_ID_BIOME_REG = "BIOME_REGISTRY";
private final VoxelMeshPool meshPool;
private final VulkanBuffer voxelData;
private final VulkanBuffer counters;
private final VulkanBuffer voxelRegistryBuffer;
private final VulkanBuffer biomeRegistryBuffer;
private final DescriptorSetLayout voxelGenerationLayout;
private final DescriptorSetLayout resetLayout;
private final DescriptorSetLayout meshGenerationLayout;
private final DescriptorSetLayout faceGraphicsLayout;
private final DescriptorSetLayout voxelDeclarationLayout;
private final DescriptorSetLayout biomeDeclarationLayout;
private static final String MESH_GENERATION_GLSL = "resources/EngineResources/VoxelComputeShaders/meshGenerationShared.glsl";
private static final String VOXEL_GENERATION_GLSL = "resources/EngineResources/VoxelComputeShaders/voxelGenerationShared.glsl";
@ -58,6 +74,15 @@ public class SharedVoxelTerrainResources {
counters = new VulkanBuffer(VkCtx, COUNTER_BUFFER_SIZE, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0, 0);
voxelRegistryBuffer = new VulkanBuffer(VkCtx, (long) MAX_VOXEL_TYPES * VOXEL_REG_DATA_SIZE,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE,
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
biomeRegistryBuffer = new VulkanBuffer(VkCtx, (long) MAX_BIOME_TYPES * BIOME_REG_DATA_SIZE,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE,
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
voxelGenerationLayout = new DescriptorSetLayout(VkCtx, new DescriptorSetLayout.LayoutInformation[]{
new DescriptorSetLayout.LayoutInformation(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0, 1, VK_SHADER_STAGE_COMPUTE_BIT)
});
@ -78,6 +103,13 @@ public class SharedVoxelTerrainResources {
new DescriptorSetLayout.LayoutInformation(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,0,1,VK_SHADER_STAGE_VERTEX_BIT )
});
voxelDeclarationLayout = new DescriptorSetLayout(VkCtx, new DescriptorSetLayout.LayoutInformation[]{
new DescriptorSetLayout.LayoutInformation(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0, 1, VK_SHADER_STAGE_COMPUTE_BIT)
});
biomeDeclarationLayout = new DescriptorSetLayout(VkCtx, new DescriptorSetLayout.LayoutInformation[]{
new DescriptorSetLayout.LayoutInformation(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0, 1, VK_SHADER_STAGE_COMPUTE_BIT)
});
createDescriptorSets(VkCtx);
PushConstantsRange[] generationPushConstants = new PushConstantsRange[]{
@ -90,9 +122,11 @@ public class SharedVoxelTerrainResources {
resetPipeline = new ComputePipeline(VkCtx,resetShader,new DescriptorSetLayout[]{resetLayout},generationPushConstants);
voxelGenerationPipeline = new ComputePipeline(VkCtx,voxelShader, new DescriptorSetLayout[]{voxelGenerationLayout},generationPushConstants);
voxelGenerationPipeline = new ComputePipeline(VkCtx,voxelShader,
new DescriptorSetLayout[]{voxelGenerationLayout,biomeDeclarationLayout},generationPushConstants);
meshGenerationPipeline = new ComputePipeline( VkCtx,meshShader, new DescriptorSetLayout[]{meshGenerationLayout},generationPushConstants );
meshGenerationPipeline = new ComputePipeline(VkCtx, meshShader,
new DescriptorSetLayout[]{meshGenerationLayout, voxelDeclarationLayout}, generationPushConstants);
resetShader.CleanUp(VkCtx);
voxelShader.CleanUp(VkCtx);
@ -126,6 +160,71 @@ public class SharedVoxelTerrainResources {
DescriptorSet faceGraphicsSet = allocator.AddDescriptorSet(device, DESC_ID_FACE_GRAPHICS, faceGraphicsLayout);
faceGraphicsSet.SetBuffer(device, meshPool.facePool(), meshPool.facePool().GetRequestedSize(), 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
DescriptorSet voxelDeclareDescriptorSet = allocator.AddDescriptorSet(device, DESC_ID_VOXEL_REG, voxelDeclarationLayout);
voxelDeclareDescriptorSet.SetBuffer(device, voxelRegistryBuffer, voxelRegistryBuffer.GetRequestedSize(), 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
DescriptorSet biomeDeclareDescriptorSet = allocator.AddDescriptorSet(device, DESC_ID_BIOME_REG, biomeDeclarationLayout);
biomeDeclareDescriptorSet.SetBuffer(device, biomeRegistryBuffer, biomeRegistryBuffer.GetRequestedSize(), 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
}
public void UploadBiomeRegistry(VulkanContext VkCtx, List<Biome> biomeReg) {
int count = Math.min(biomeReg.size(), MAX_BIOME_TYPES);
long memoryAddr = biomeRegistryBuffer.MapMemory(VkCtx);
ByteBuffer buffer = MemoryUtil.memByteBuffer(memoryAddr, (int) biomeRegistryBuffer.GetRequestedSize());
for (int i = 0; i < count; i++) {
Biome biome = biomeReg.get(i);
int base = i * BIOME_REG_DATA_SIZE;
buffer.putInt(base + 0, biome.surfaceBlock());
buffer.putInt(base + 4, biome.dirtBlock());
buffer.putInt(base + 8, biome.stoneBlock());
buffer.putInt(base + 12, biome.grassFoliage());
for (int j = 0; j < 4; j++) {
buffer.putInt(base + 16 + j * 4, biome.foliageBlocks()[j]);
buffer.putInt(base + 32 + j * 4, biome.tallBlocks()[j]);
buffer.putInt(base + 48 + j * 4, biome.otherFoliage()[j]);
}
}
biomeRegistryBuffer.UnMapMemory(VkCtx);
}
public void UploadVoxelRegistry(VulkanContext VkCtx, List<Voxel> voxelReg) {
int count = Math.min(voxelReg.size(), MAX_VOXEL_TYPES);
long memoryAddr = voxelRegistryBuffer.MapMemory(VkCtx);
ByteBuffer buffer = MemoryUtil.memByteBuffer(memoryAddr, (int) voxelRegistryBuffer.GetRequestedSize());
for (int i = 0; i < count; i++) {
Voxel voxel = voxelReg.get(i);
int base = i * VOXEL_REG_DATA_SIZE;
buffer.putInt(base + 0, voxel.MaterialID());
buffer.putInt(base + 4, voxel.BlockType());
buffer.putInt(base + 8, voxel.UpUV()[0].x);
buffer.putInt(base + 12, voxel.UpUV()[0].y);
buffer.putInt(base + 16, voxel.UpUV()[1].x);
buffer.putInt(base + 20, voxel.UpUV()[1].y);
buffer.putInt(base + 24, voxel.DownUV()[0].x);
buffer.putInt(base + 28, voxel.DownUV()[0].y);
buffer.putInt(base + 32, voxel.DownUV()[1].x);
buffer.putInt(base + 36, voxel.DownUV()[1].y);
buffer.putInt(base + 40, voxel.NorthUV()[0].x);
buffer.putInt(base + 44, voxel.NorthUV()[0].y);
buffer.putInt(base + 48, voxel.NorthUV()[1].x);
buffer.putInt(base + 52, voxel.NorthUV()[1].y);
buffer.putInt(base + 56, voxel.SouthUV()[0].x);
buffer.putInt(base + 60, voxel.SouthUV()[0].y);
buffer.putInt(base + 64, voxel.SouthUV()[1].x);
buffer.putInt(base + 68, voxel.SouthUV()[1].y);
buffer.putInt(base + 72, voxel.EastUV()[0].x);
buffer.putInt(base + 76, voxel.EastUV()[0].y);
buffer.putInt(base + 80, voxel.EastUV()[1].x);
buffer.putInt(base + 84, voxel.EastUV()[1].y);
buffer.putInt(base + 88, voxel.WestUV()[0].x);
buffer.putInt(base + 92, voxel.WestUV()[0].y);
buffer.putInt(base + 96, voxel.WestUV()[1].x);
buffer.putInt(base + 100, voxel.WestUV()[1].y);
}
voxelRegistryBuffer.UnMapMemory(VkCtx);
}
public DescriptorSetLayout faceGraphicsLayout() {
@ -163,6 +262,12 @@ public class SharedVoxelTerrainResources {
public String voxelGenerationDescriptorId() {
return DESC_ID_VOXEL_GENERATION;
}
public String voxelRegDescriptorID() {
return DESC_ID_VOXEL_REG;
}
public String biomeRegDescriptorID() {
return DESC_ID_BIOME_REG;
}
public String resetDescriptorId() {
return DESC_ID_RESET;
@ -181,9 +286,13 @@ public class SharedVoxelTerrainResources {
voxelGenerationLayout.CleanUp(VkCtx);
meshGenerationLayout.CleanUp(VkCtx);
faceGraphicsLayout.CleanUp(VkCtx);
voxelDeclarationLayout.CleanUp(VkCtx);
biomeDeclarationLayout.CleanUp(VkCtx);
voxelData.cleanup(VkCtx);
counters.cleanup(VkCtx);
voxelRegistryBuffer.cleanup(VkCtx);
biomeRegistryBuffer.cleanup(VkCtx);
meshPool.cleanup(VkCtx);
}
}

View file

@ -0,0 +1,6 @@
package net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Computing.VoxelTerrainGeneration;
import org.joml.Vector2i;
public record Voxel(int MaterialID, int BlockType, Vector2i[] UpUV, Vector2i[] DownUV, Vector2i[] NorthUV, Vector2i[] SouthUV, Vector2i[] EastUV, Vector2i[] WestUV) {
}

View file

@ -38,16 +38,18 @@ public class VoxelChunkGenerator {
pushConstants.putInt(8, chunk.position().z);
pushConstants.putInt(12, chunk.slot());
pushConstants.putInt(16, faceOffset);
pushConstants.putInt(20, 0);
pushConstants.putInt(20, VoxelWorldManager.GetVoxelTypeCount());
pushConstants.putInt(24, indirectCommandIndex);
pushConstants.putInt(28, 0);
pushConstants.putInt(28, VoxelWorldManager.GetBiomeTypeCount());
LongBuffer resetDescriptorSet = stack.longs(allocator.GetDescriptorSet(
resources.resetDescriptorId()).GetVkDescriptorSet());
LongBuffer voxelDescriptorSet = stack.longs(allocator.GetDescriptorSet(
resources.voxelGenerationDescriptorId()).GetVkDescriptorSet());
LongBuffer meshDescriptorSet = stack.longs(allocator.GetDescriptorSet(
resources.meshDescriptorId()).GetVkDescriptorSet());
resources.voxelGenerationDescriptorId()).GetVkDescriptorSet(),
allocator.GetDescriptorSet(resources.biomeRegDescriptorID()).GetVkDescriptorSet());
LongBuffer meshDescriptorSet = stack.longs(
allocator.GetDescriptorSet(resources.meshDescriptorId()).GetVkDescriptorSet(),
allocator.GetDescriptorSet(resources.voxelRegDescriptorID()).GetVkDescriptorSet());
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE,resources.resetPipeline().GetVulkanPipeline());
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE,resources.resetPipeline().GetVulkanPipelineLayout(),0, resetDescriptorSet,null );

View file

@ -10,6 +10,8 @@ import org.lwjgl.vulkan.VkCommandBuffer;
import org.tinylog.Logger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -17,7 +19,7 @@ import static org.lwjgl.vulkan.VK13.*;
public class VoxelWorldManager {
public static final int CHUNK_SIZE = 16;
private static final int MAX_RESIDENT_CHUNKS = 8192 * 2;
private static final int MAX_RESIDENT_CHUNKS = 8192 * 3;
private static final int MAX_CHUNK_GENERATIONS_PER_FRAME = 128;
private static final ConcurrentLinkedQueue<Vector3i> RequestedChunks = new ConcurrentLinkedQueue<>();
@ -25,6 +27,71 @@ public class VoxelWorldManager {
private static final ConcurrentHashMap<Vector3i, Boolean> KnownChunks = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<Vector3i, VoxelChunkVisibility> CulledChunks = new ConcurrentHashMap<>();
private static final List<Voxel> VoxelRegistry = new java.util.concurrent.CopyOnWriteArrayList<>();
private static final ConcurrentHashMap<String, Integer> VoxelNameToIndex = new ConcurrentHashMap<>();
private static volatile boolean RegistryDirty = false;
private static final List<Biome> BiomeRegistry = new java.util.concurrent.CopyOnWriteArrayList<>();
private static final ConcurrentHashMap<String, Integer> BiomeNameToIndex = new ConcurrentHashMap<>();
private static volatile boolean BiomeRegistryDirty = false;
public static synchronized void AddVoxel(Voxel voxel, String name) {
Integer existing = VoxelNameToIndex.get(name);
if (existing != null) {
VoxelRegistry.set(existing, voxel);
} else {
VoxelNameToIndex.put(name, VoxelRegistry.size());
VoxelRegistry.add(voxel);
}
RegistryDirty = true;
}
public static List<Voxel> GetVoxelRegistry() {
return new ArrayList<>(VoxelRegistry);
}
public static Voxel GetVoxel(String name) {
Integer idx = VoxelNameToIndex.get(name);
return idx == null ? null : VoxelRegistry.get(idx);
}
public static int GetVoxelTypeID(String name) {
Integer idx = VoxelNameToIndex.get(name);
return idx == null ? -1 : idx + 1;
}
public static int GetVoxelTypeCount() {
return VoxelRegistry.size();
}
public static synchronized void AddBiome(Biome biome, String name) {
Integer existing = BiomeNameToIndex.get(name);
if (existing != null) {
BiomeRegistry.set(existing, biome);
} else {
BiomeNameToIndex.put(name, BiomeRegistry.size());
BiomeRegistry.add(biome);
}
BiomeRegistryDirty = true;
}
public static List<Biome> GetBiomeRegistry() {
return new ArrayList<>(BiomeRegistry);
}
public static Biome GetBiome(String name) {
Integer idx = BiomeNameToIndex.get(name);
return idx == null ? null : BiomeRegistry.get(idx);
}
public static int GetBiomeTypeID(String name) {
Integer idx = BiomeNameToIndex.get(name);
return idx == null ? -1 : idx + 1;
}
public static int GetBiomeTypeCount() {
return BiomeRegistry.size();
}
private static SharedVoxelTerrainResources Resources;
private static VoxelChunkGenerator Generator;
@ -59,7 +126,7 @@ public class VoxelWorldManager {
public static int GenerationOffset = 0;
public static Vector3i LastPosition = new Vector3i(0, 0, 0);
static int IterationX = 0;
static volatile int IterationX = 0;
static Vector3i ChunkPos = new Vector3i(0, 0, 0);
static Vector3i ChunkPos2 = new Vector3i(0, 0, 0);
@ -70,6 +137,11 @@ public class VoxelWorldManager {
private static Vector3f min = new Vector3f();
private static Vector3f min2 = new Vector3f();
public static synchronized void ResetIterationX()
{
IterationX = 0;
}
public static synchronized void GenerateChunks(Vector3i Position, Vector3i Radius, Matrix4f cameraView,Matrix4f projectionMatrix) {
projectionMatrix.mul(cameraView, viewProjectionMatrix);
@ -118,6 +190,20 @@ public class VoxelWorldManager {
Init(VkCtx);
}
if (RegistryDirty) {
RegistryDirty = false;
Resources.UploadVoxelRegistry(VkCtx, GetVoxelRegistry());
}
if (BiomeRegistryDirty) {
BiomeRegistryDirty = false;
Resources.UploadBiomeRegistry(VkCtx, GetBiomeRegistry());
}
if (VoxelRegistry.isEmpty()) {
return; // nothing to mesh against yet
}
int generatedThisFrame = 0;
while (!RequestedChunks.isEmpty() && generatedThisFrame < MAX_CHUNK_GENERATIONS_PER_FRAME) {
@ -206,19 +292,19 @@ public class VoxelWorldManager {
KnownChunks.remove(pos);
return true;
}
int dx = pos.x - centerChunk.x;
int dy = pos.y - centerChunk.y;
int dz = pos.z - centerChunk.z;
int distSq = dx * dx + dy * dy + dz * dz;
if (distSq > maxDistSq) {
RenderChunk chunk = entry.getValue();
Resources.meshPool().free(chunk);
KnownChunks.remove(pos);
return true;
}
// int dx = pos.x - centerChunk.x;
// int dy = pos.y - centerChunk.y;
// int dz = pos.z - centerChunk.z;
//
// int distSq = dx * dx + dy * dy + dz * dz;
// if (distSq > maxDistSq) {
// RenderChunk chunk = entry.getValue();
//
// Resources.meshPool().free(chunk);
// KnownChunks.remove(pos);
//
// return true;
// }
return false;
});

View file

@ -82,6 +82,8 @@ public class DeferredSceneRender implements SceneRenderer {//dynamic rendering
private static final String FRAGMENT_OPAQUE_SHADER_FILE_SPV = FRAGMENT_OPAQUE_SHADER_FILE_GLSL + ".spv";
private static final String VERTEX_SHADER_FILE_GLSL = "resources/EngineResources/shaders/scene_vertex.glsl";
private static final String VERTEX_SHADER_FILE_SPV = VERTEX_SHADER_FILE_GLSL + ".spv";
private static final String PACKED_FRAGMENT_SHADER_FILE_GLSL = "resources/EngineResources/shaders/packed_scene_fragment_deferred.glsl";
private static final String PACKED_FRAGMENT_SHADER_FILE_SPV = PACKED_FRAGMENT_SHADER_FILE_GLSL + ".spv";
private static final String PACKED_VERTEX_SHADER_FILE_GLSL = "resources/EngineResources/shaders/packed_scene_vertex.glsl";
private static final String PACKED_VERTEX_SHADER_FILE_SPV = PACKED_VERTEX_SHADER_FILE_GLSL + ".spv";
private Pipeline VkVoxelPipeline;
@ -246,11 +248,11 @@ public class DeferredSceneRender implements SceneRenderer {//dynamic rendering
private static ShaderModule[] CreateShaderModules(VulkanContext VkCtx, int Translucent){
if(EngineConfig.getInstance().RecompileShaders()){
ShaderCompiler.CompileGLSLShaderOnChange(Translucent > 2 ? PACKED_VERTEX_SHADER_FILE_GLSL : VERTEX_SHADER_FILE_GLSL, Shaderc.shaderc_glsl_vertex_shader);
ShaderCompiler.CompileGLSLShaderOnChange( Translucent == 2 ? FRAGMENT_TRANSLUCENT_SHADER_FILE_GLSL : Translucent == 1 ? FRAGMENT_OPAQUE_SHADER_FILE_GLSL : FRAGMENT_SHADER_FILE_GLSL, Shaderc.shaderc_glsl_fragment_shader);
ShaderCompiler.CompileGLSLShaderOnChange( Translucent > 2 ? PACKED_FRAGMENT_SHADER_FILE_GLSL : Translucent == 2 ? FRAGMENT_TRANSLUCENT_SHADER_FILE_GLSL : Translucent == 1 ? FRAGMENT_OPAQUE_SHADER_FILE_GLSL : FRAGMENT_SHADER_FILE_GLSL, Shaderc.shaderc_glsl_fragment_shader);
}
return new ShaderModule[]{
new ShaderModule(VkCtx, VK_SHADER_STAGE_VERTEX_BIT, Translucent > 2 ? PACKED_VERTEX_SHADER_FILE_SPV :VERTEX_SHADER_FILE_SPV,null),
new ShaderModule(VkCtx, VK_SHADER_STAGE_FRAGMENT_BIT, Translucent == 2 ? FRAGMENT_TRANSLUCENT_SHADER_FILE_SPV : Translucent == 1 ? FRAGMENT_OPAQUE_SHADER_FILE_SPV : FRAGMENT_SHADER_FILE_SPV,null)
new ShaderModule(VkCtx, VK_SHADER_STAGE_FRAGMENT_BIT, Translucent > 2 ? PACKED_FRAGMENT_SHADER_FILE_SPV : Translucent == 2 ? FRAGMENT_TRANSLUCENT_SHADER_FILE_SPV : Translucent == 1 ? FRAGMENT_OPAQUE_SHADER_FILE_SPV : FRAGMENT_SHADER_FILE_SPV,null)
};
}

View file

@ -66,6 +66,8 @@ public class ForwardSceneRender implements SceneRenderer {//dynamic rendering
public static String SkyBoxID = "SKYBOX_TEXTURE";
private static final int PUSH_CONSTANTS_SIZE = VulkanUtils.MATRIX4X4_SIZE + VulkanUtils.INT_SIZE;
private final VulkanBuffer BufferProjectionMatrix;
private static final String PACKED_FRAGMENT_SHADER_FILE_GLSL = "resources/EngineResources/shaders/packed_scene_fragment.glsl";
private static final String PACKED_FRAGMENT_SHADER_FILE_SPV = PACKED_FRAGMENT_SHADER_FILE_GLSL + ".spv";
private static final String PACKED_VERTEX_SHADER_FILE_GLSL = "resources/EngineResources/shaders/packed_scene_vertex.glsl";
private static final String PACKED_VERTEX_SHADER_FILE_SPV = PACKED_VERTEX_SHADER_FILE_GLSL + ".spv";
private Pipeline VkVoxelPipeline;
@ -293,11 +295,11 @@ public class ForwardSceneRender implements SceneRenderer {//dynamic rendering
private static ShaderModule[] CreateShaderModules(VulkanContext VkCtx, int Translucent){
if(EngineConfig.getInstance().RecompileShaders()){
ShaderCompiler.CompileGLSLShaderOnChange(Translucent > 2 ? PACKED_VERTEX_SHADER_FILE_GLSL :VERTEX_SHADER_FILE_GLSL, Shaderc.shaderc_glsl_vertex_shader);
ShaderCompiler.CompileGLSLShaderOnChange( Translucent == 2 ? FRAGMENT_TRANSLUCENT_SHADER_FILE_GLSL : Translucent == 1 ? FRAGMENT_OPAQUE_SHADER_FILE_GLSL : FRAGMENT_SHADER_FILE_GLSL, Shaderc.shaderc_glsl_fragment_shader);
ShaderCompiler.CompileGLSLShaderOnChange(Translucent > 2 ? PACKED_FRAGMENT_SHADER_FILE_GLSL : Translucent == 2 ? FRAGMENT_TRANSLUCENT_SHADER_FILE_GLSL : Translucent == 1 ? FRAGMENT_OPAQUE_SHADER_FILE_GLSL : FRAGMENT_SHADER_FILE_GLSL, Shaderc.shaderc_glsl_fragment_shader);
}
return new ShaderModule[]{
new ShaderModule(VkCtx, VK_SHADER_STAGE_VERTEX_BIT, Translucent > 2 ? PACKED_VERTEX_SHADER_FILE_SPV : VERTEX_SHADER_FILE_SPV,null),
new ShaderModule(VkCtx, VK_SHADER_STAGE_FRAGMENT_BIT, Translucent == 2 ? FRAGMENT_TRANSLUCENT_SHADER_FILE_SPV : Translucent == 1 ? FRAGMENT_OPAQUE_SHADER_FILE_SPV : FRAGMENT_SHADER_FILE_SPV,null)
new ShaderModule(VkCtx, VK_SHADER_STAGE_FRAGMENT_BIT,Translucent > 2 ? PACKED_FRAGMENT_SHADER_FILE_SPV : Translucent == 2 ? FRAGMENT_TRANSLUCENT_SHADER_FILE_SPV : Translucent == 1 ? FRAGMENT_OPAQUE_SHADER_FILE_SPV : FRAGMENT_SHADER_FILE_SPV,null)
};

View file

@ -54,6 +54,7 @@ public class Device {
}
var features12 = VkPhysicalDeviceVulkan12Features.calloc(MemStack)
.sType$Default()
.shaderSampledImageArrayNonUniformIndexing(true)
.scalarBlockLayout(true);
var features13 = VkPhysicalDeviceVulkan13Features.calloc(MemStack)