From b85c242b53c911988c47c20fc5bea515ad8ec172 Mon Sep 17 00:00:00 2001 From: DrSocalkwe3n Date: Tue, 8 Jul 2025 10:01:50 +0600 Subject: [PATCH] * --- CMakeLists.txt | 4 +- Src/Client/Vulkan/VulkanRenderSession.cpp | 145 ++++++++++++++++++---- Src/Client/Vulkan/VulkanRenderSession.hpp | 9 +- Src/Server/GameServer.cpp | 6 +- assets/shaders/chunk/node.vert | 10 +- assets/shaders/chunk/node.vert.bin | Bin 2584 -> 2584 bytes 6 files changed, 135 insertions(+), 39 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 19c87c3..4765594 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,8 +17,8 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") # -rdy # set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg") # sanitizer -# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") -# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address") # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment") diff --git a/Src/Client/Vulkan/VulkanRenderSession.cpp b/Src/Client/Vulkan/VulkanRenderSession.cpp index 6a3c18b..cc2528a 100644 --- a/Src/Client/Vulkan/VulkanRenderSession.cpp +++ b/Src/Client/Vulkan/VulkanRenderSession.cpp @@ -199,10 +199,10 @@ void VulkanRenderSession::init(Vulkan *instance) { int width, height; bool hasAlpha; for(const char *path : { - //"tropical_rainforest_wood.png", "grass.png", + "tropical_rainforest_wood.png", "willow_wood.png", - //"xnether_blue_wood.png", + "xnether_blue_wood.png", "xnether_purple_wood.png" }) { ByteBuffer image = VK::loadPNG(getResource(std::string("textures/") + path)->makeStream().Stream, width, height, hasAlpha); @@ -619,26 +619,39 @@ void VulkanRenderSession::onChunksChange(WorldId_t worldId, const std::unordered Pos::GlobalRegion rPos = pos >> 4; Pos::bvec16u cPos = pos & 0xf; - const auto &voxels = ServerSession->Data.Worlds[worldId].Regions[rPos].Chunks[cPos.x][cPos.y][cPos.z].Voxels; + auto &buffers = table[pos]; - if(voxels.empty()) { + const auto &chunk = ServerSession->Data.Worlds[worldId].Regions[rPos].Chunks[cPos.x][cPos.y][cPos.z]; + + if(chunk.Voxels.empty()) { + std::get<0>(buffers) = nullptr; + } else { + std::vector vertexs = generateMeshForVoxelChunks(chunk.Voxels); + + if(!vertexs.empty()) { + auto &voxels = std::get<0>(buffers); + voxels = std::make_unique(VkInst, vertexs.size()*sizeof(VoxelVertexPoint)); + std::copy(vertexs.data(), vertexs.data()+vertexs.size(), (VoxelVertexPoint*) voxels->mapMemory()); + voxels->unMapMemory(); + } else { + std::get<0>(buffers) = nullptr; + } + } + + std::vector vertexs2 = generateMeshForNodeChunks(chunk.Nodes); + if(vertexs2.empty()) { + std::get<1>(buffers) = nullptr; + } else { + auto &nodes = std::get<1>(buffers); + nodes = std::make_unique(VkInst, vertexs2.size()*sizeof(NodeVertexStatic)); + std::copy(vertexs2.data(), vertexs2.data()+vertexs2.size(), (NodeVertexStatic*) nodes->mapMemory()); + nodes->unMapMemory(); + } + + if(!std::get<0>(buffers) && !std::get<1>(buffers)) { auto iter = table.find(pos); if(iter != table.end()) table.erase(iter); - } else { - Logger("Test").debug() << voxels.size(); - std::vector vertexs = generateMeshForVoxelChunks(voxels); - Logger("Test").debug() << vertexs.size(); - - if(!vertexs.empty()) { - auto &buffer = table[pos] = std::make_unique(VkInst, vertexs.size()*sizeof(VoxelVertexPoint)); - std::copy(vertexs.data(), vertexs.data()+vertexs.size(), (VoxelVertexPoint*) buffer->mapMemory()); - buffer->unMapMemory(); - } else { - auto iter = table.find(pos); - if(iter != table.end()) - table.erase(iter); - } } } @@ -725,14 +738,45 @@ void VulkanRenderSession::drawWorld(GlobalTime gTime, float dTime, VkCommandBuff glm::mat4 orig = PCO.Model; for(auto &pair : iterWorld->second) { - glm::vec3 cpos(pair.first.x, pair.first.y, pair.first.z); - PCO.Model = glm::translate(orig, cpos*16.f); - vkBuffer = *pair.second; + if(auto& voxels = std::get<0>(pair.second)) { + glm::vec3 cpos(pair.first.x, pair.first.y, pair.first.z); + PCO.Model = glm::translate(orig, cpos*16.f); + vkBuffer = *voxels; - vkCmdPushConstants(drawCmd, MainAtlas_LightMap_PipelineLayout, - VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT, offsetof(WorldPCO, Model), sizeof(WorldPCO::Model), &PCO.Model); - vkCmdBindVertexBuffers(drawCmd, 0, 1, &vkBuffer, &vkOffsets); - vkCmdDraw(drawCmd, pair.second->getSize() / sizeof(VoxelVertexPoint), 1, 0, 0); + vkCmdPushConstants(drawCmd, MainAtlas_LightMap_PipelineLayout, + VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT, offsetof(WorldPCO, Model), sizeof(WorldPCO::Model), &PCO.Model); + vkCmdBindVertexBuffers(drawCmd, 0, 1, &vkBuffer, &vkOffsets); + vkCmdDraw(drawCmd, voxels->getSize() / sizeof(VoxelVertexPoint), 1, 0, 0); + } + } + + PCO.Model = orig; + } + } + + vkCmdBindPipeline(drawCmd, VK_PIPELINE_BIND_POINT_GRAPHICS, NodeStaticOpaquePipeline); + vkCmdPushConstants(drawCmd, MainAtlas_LightMap_PipelineLayout, + VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT, 0, sizeof(WorldPCO), &PCO); + vkCmdBindDescriptorSets(drawCmd, VK_PIPELINE_BIND_POINT_GRAPHICS, + MainAtlas_LightMap_PipelineLayout, 0, 2, + (const VkDescriptorSet[]) {MainAtlasDescriptor, VoxelLightMapDescriptor}, 0, nullptr); + + { + auto iterWorld = External.ChunkVoxelMesh.find(WorldId); + if(iterWorld != External.ChunkVoxelMesh.end()) { + glm::mat4 orig = PCO.Model; + + for(auto &pair : iterWorld->second) { + if(auto& nodes = std::get<1>(pair.second)) { + glm::vec3 cpos(pair.first.x, pair.first.y, pair.first.z); + PCO.Model = glm::translate(orig, cpos*16.f); + vkBuffer = *nodes; + + vkCmdPushConstants(drawCmd, MainAtlas_LightMap_PipelineLayout, + VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT, offsetof(WorldPCO, Model), sizeof(WorldPCO::Model), &PCO.Model); + vkCmdBindVertexBuffers(drawCmd, 0, 1, &vkBuffer, &vkOffsets); + vkCmdDraw(drawCmd, nodes->getSize() / sizeof(NodeVertexStatic), 1, 0, 0); + } } PCO.Model = orig; @@ -827,6 +871,57 @@ std::vector VulkanRenderSession::generateMeshForVoxelChunks(co return out; } +std::vector VulkanRenderSession::generateMeshForNodeChunks(const Node nodes[16][16][16]) { + std::vector out; + NodeVertexStatic v; + + for(int z = 0; z < 16; z++) + for(int y = 0; y < 16; y++) + for(int x = 0; x < 16; x++) + { + if(nodes[x][y][z].Data == 0) + continue; + + v.Tex = nodes[x][y][z].NodeId; + + if((y+1) < 16 || nodes[x][y+1][z].NodeId != 0) { + v.FX = 135+x*16; + v.FY = 135+y*16+16; + v.FZ = 135+z*16; + v.TU = 0; + v.TV = 0; + out.push_back(v); + + v.FX += 15; + v.TU = 65535; + out.push_back(v); + + v.FZ += 15; + v.TV = 65535; + out.push_back(v); + + v.FX = 135+x*16; + v.FY = 135+y*16+16; + v.FZ = 135+z*16; + v.TU = 0; + v.TV = 0; + out.push_back(v); + + v.FX += 15; + v.FZ += 15; + v.TV = 65535; + v.TU = 65535; + out.push_back(v); + + v.FX -= 15; + v.TV = 0; + out.push_back(v); + } + } + + return out; +} + void VulkanRenderSession::updateDescriptor_MainAtlas() { VkDescriptorBufferInfo bufferInfo = VKCTX->MainTest; VkDescriptorImageInfo imageInfo = VKCTX->MainTest; diff --git a/Src/Client/Vulkan/VulkanRenderSession.hpp b/Src/Client/Vulkan/VulkanRenderSession.hpp index a4145d0..e6171ed 100644 --- a/Src/Client/Vulkan/VulkanRenderSession.hpp +++ b/Src/Client/Vulkan/VulkanRenderSession.hpp @@ -64,7 +64,7 @@ struct VoxelVertexPoint { struct NodeVertexStatic { uint32_t - FX : 9, FY : 9, FZ : 9, // Позиция -112 ~ 369 / 16 + FX : 9, FY : 9, FZ : 9, // Позиция 15 -120 ~ 240 360 15 / 16 N1 : 4, // Не занято LS : 1, // Масштаб карты освещения (1м/16 или 1м) Tex : 18, // Текстура @@ -133,7 +133,11 @@ class VulkanRenderSession : public IRenderSession, public IVulkanDependent { std::map ServerToAtlas; struct { - std::unordered_map>> ChunkVoxelMesh; + std::unordered_map, std::unique_ptr> // Voxels, Nodes + > + > ChunkVoxelMesh; } External; virtual void free(Vulkan *instance) override; @@ -166,6 +170,7 @@ public: void drawWorld(GlobalTime gTime, float dTime, VkCommandBuffer drawCmd); static std::vector generateMeshForVoxelChunks(const std::vector cubes); + static std::vector generateMeshForNodeChunks(const Node nodes[16][16][16]); private: void updateDescriptor_MainAtlas(); diff --git a/Src/Server/GameServer.cpp b/Src/Server/GameServer.cpp index 34dfe79..9662dde 100644 --- a/Src/Server/GameServer.cpp +++ b/Src/Server/GameServer.cpp @@ -741,7 +741,6 @@ IWorldSaveBackend::TickSyncInfo_Out GameServer::stepDatabaseSync() { IWorldSaveBackend::TickSyncInfo_In toDB; for(std::shared_ptr& cec : Game.CECs) { - break; assert(cec); // Пересчитать зоны наблюдения if(cec->CrossedBorder) { @@ -838,11 +837,8 @@ void GameServer::stepGeneratorAndLuaAsync(IWorldSaveBackend::TickSyncInfo_Out db // Синхронизация с контроллером асинхронных обработчиков луа // 2.2 и 3.1 - // Обработка шума + // Обработка шума на стороне луа for(auto& [key, region] : calculatedNoise) { - LOG.debug() << "Сгенерирован " << key.WId << ' ' << key.RegionPos.x << ' ' - << key.RegionPos.y << ' ' << key.RegionPos.z; - auto &obj = toLoadRegions[key.WId].emplace_back(key.RegionPos, World::RegionIn()).second; float *ptr = ®ion[0]; diff --git a/assets/shaders/chunk/node.vert b/assets/shaders/chunk/node.vert index ed638f3..ee0f6cd 100644 --- a/assets/shaders/chunk/node.vert +++ b/assets/shaders/chunk/node.vert @@ -1,6 +1,6 @@ #version 450 -layout(location = 0) in uvec4 Vertex; +layout(location = 0) in uvec3 Vertex; layout(location = 0) out GeometryObj { vec3 GeoPos; // Реальная позиция в мире @@ -16,7 +16,7 @@ layout(push_constant) uniform UniformBufferObject { // struct NodeVertexStatic { // uint32_t -// FX : 9, FY : 9, FZ : 9, // Позиция -112 ~ 369 / 16 +// FX : 9, FY : 9, FZ : 9, // Позиция 15 -120 ~ 240 360 15 / 16 // N1 : 4, // Не занято // LS : 1, // Масштаб карты освещения (1м/16 или 1м) // Tex : 18, // Текстура @@ -27,9 +27,9 @@ layout(push_constant) uniform UniformBufferObject { void main() { vec4 baseVec = ubo.model*vec4( - float(Vertex.x & 0x1ff) / 16.f - 7, - float((Vertex.x >> 9) & 0x1ff) / 16.f - 7, - float((Vertex.x >> 18) & 0x1ff) / 16.f - 7, + float(Vertex.x & 0x1ff) / 16.f - 135/16.f, + float((Vertex.x >> 9) & 0x1ff) / 16.f - 135/16.f, + float((Vertex.x >> 18) & 0x1ff) / 16.f - 135/16.f, 1 ); diff --git a/assets/shaders/chunk/node.vert.bin b/assets/shaders/chunk/node.vert.bin index 614f723a38f6aa3a4f5a66cd31da066a2f749ea5..be9b0fb14f047d904f5290b9990d0193176a17d3 100644 GIT binary patch delta 21 dcmbOsGDBp;A7)18$$yy(nb;jS7qF~g1prjw2QB~r delta 21 dcmbOsGDBp;A7)0D$$yy(nI1T7E?`-~3IJNx2nPTF