diff options
author | LaG1924 <lag1924@gmail.com> | 2022-12-15 11:53:56 +0100 |
---|---|---|
committer | LaG1924 <lag1924@gmail.com> | 2022-12-15 11:53:56 +0100 |
commit | 12923669214ba73a809f4e221a634244888b0146 (patch) | |
tree | 9fe8a85f8fc9a5e3fa9f4e6a9418144090595730 /src | |
parent | Fixed OpenSans font downloading (diff) | |
download | AltCraft-12923669214ba73a809f4e221a634244888b0146.tar AltCraft-12923669214ba73a809f4e221a634244888b0146.tar.gz AltCraft-12923669214ba73a809f4e221a634244888b0146.tar.bz2 AltCraft-12923669214ba73a809f4e221a634244888b0146.tar.lz AltCraft-12923669214ba73a809f4e221a634244888b0146.tar.xz AltCraft-12923669214ba73a809f4e221a634244888b0146.tar.zst AltCraft-12923669214ba73a809f4e221a634244888b0146.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/RendererSection.cpp | 54 | ||||
-rw-r--r-- | src/RendererSection.hpp | 25 | ||||
-rw-r--r-- | src/RendererSectionData.cpp | 36 | ||||
-rw-r--r-- | src/RendererSectionData.hpp | 3 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 69 | ||||
-rw-r--r-- | src/RendererWorld.hpp | 6 |
6 files changed, 134 insertions, 59 deletions
diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index 429a8bd..7ea74df 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -9,16 +9,26 @@ #include "RendererSectionData.hpp" -RendererSection::RendererSection(const RendererSectionData& data, std::shared_ptr<Gal::Pipeline> pipeline, std::shared_ptr<Gal::BufferBinding> bufferBinding) { +RendererSection::RendererSection(const RendererSectionData& data, + std::shared_ptr<Gal::Pipeline> solidPipeline, + std::shared_ptr<Gal::BufferBinding> solidBufferBinding, + std::shared_ptr<Gal::Pipeline> liquidPipeline, + std::shared_ptr<Gal::BufferBinding> liquidBufferBinding) { OPTICK_EVENT(); auto gal = Gal::GetImplementation(); - buffer = gal->CreateBuffer(); - pipelineInstance = pipeline->CreateInstance({ - {bufferBinding, buffer} + solidBuffer = gal->CreateBuffer(); + solidPipelineInstance = solidPipeline->CreateInstance({ + {solidBufferBinding, solidBuffer} }); - pipelineInstance->SetInstancesCount(4); + solidPipelineInstance->SetInstancesCount(4); + + liquidBuffer = gal->CreateBuffer(); + liquidPipelineInstance = liquidPipeline->CreateInstance({ + {liquidBufferBinding, liquidBuffer} + }); + liquidPipelineInstance->SetInstancesCount(4); UpdateData(data); } @@ -32,18 +42,27 @@ RendererSection::~RendererSection() { } -void swap(RendererSection & lhs, RendererSection & rhs) { - std::swap(lhs.pipelineInstance, rhs.pipelineInstance); - std::swap(lhs.buffer, rhs.buffer); - std::swap(lhs.hash, rhs.hash); - std::swap(lhs.numOfFaces, rhs.numOfFaces); - std::swap(lhs.sectionPos, rhs.sectionPos); +void RendererSection::RenderSolid() { + OPTICK_EVENT(); + solidPipelineInstance->Activate(); + solidPipelineInstance->Render(0, solidFacesCount); } -void RendererSection::Render() { +void RendererSection::RenderLiquid() { OPTICK_EVENT(); - pipelineInstance->Activate(); - pipelineInstance->Render(0, numOfFaces); + liquidPipelineInstance->Activate(); + liquidPipelineInstance->Render(0, liquidFacesCount); +} + +void swap(RendererSection & lhs, RendererSection & rhs) { + std::swap(lhs.solidPipelineInstance, rhs.solidPipelineInstance); + std::swap(lhs.solidBuffer, rhs.solidBuffer); + std::swap(lhs.liquidPipelineInstance, rhs.liquidPipelineInstance); + std::swap(lhs.liquidBuffer, rhs.liquidBuffer); + std::swap(lhs.hash, rhs.hash); + std::swap(lhs.solidFacesCount, rhs.solidFacesCount); + std::swap(lhs.liquidFacesCount, rhs.liquidFacesCount); + std::swap(lhs.sectionPos, rhs.sectionPos); } Vector RendererSection::GetPosition() { @@ -57,9 +76,12 @@ size_t RendererSection::GetHash() { void RendererSection::UpdateData(const RendererSectionData & data) { OPTICK_EVENT(); - buffer->SetData({ reinterpret_cast<const std::byte*>(data.vertices.data()), reinterpret_cast<const std::byte*>(data.vertices.data() + data.vertices.size())}); + solidBuffer->SetData({ reinterpret_cast<const std::byte*>(data.solidVertices.data()), reinterpret_cast<const std::byte*>(data.solidVertices.data() + data.solidVertices.size())}); + solidFacesCount = data.solidVertices.size(); + + liquidBuffer->SetData({ reinterpret_cast<const std::byte*>(data.liquidVertices.data()), reinterpret_cast<const std::byte*>(data.liquidVertices.data() + data.liquidVertices.size()) }); + liquidFacesCount = data.liquidVertices.size(); - numOfFaces = data.vertices.size(); sectionPos = data.sectionPos; hash = data.hash; } diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp index 0a03f44..8125e4e 100644 --- a/src/RendererSection.hpp +++ b/src/RendererSection.hpp @@ -7,26 +7,39 @@ class RenderState; class RendererSectionData; class RendererSection { - std::shared_ptr<Gal::PipelineInstance> pipelineInstance; - std::shared_ptr<Gal::Buffer> buffer; - size_t hash; + std::shared_ptr<Gal::PipelineInstance> solidPipelineInstance; + std::shared_ptr<Gal::Buffer> solidBuffer; + std::shared_ptr<Gal::PipelineInstance> liquidPipelineInstance; + std::shared_ptr<Gal::Buffer> liquidBuffer; Vector sectionPos; + size_t hash = 0; + size_t solidFacesCount = 0; + size_t liquidFacesCount = 0; RendererSection(const RendererSection &other) = delete; public: - RendererSection(const RendererSectionData& data, std::shared_ptr<Gal::Pipeline> pipeline, std::shared_ptr<Gal::BufferBinding> bufferBinding); + RendererSection( + const RendererSectionData& data, + std::shared_ptr<Gal::Pipeline> solidPipeline, + std::shared_ptr<Gal::BufferBinding> solidBufferBinding, + std::shared_ptr<Gal::Pipeline> liquidPipeline, + std::shared_ptr<Gal::BufferBinding> liquidBufferBinding); RendererSection(RendererSection &&other); ~RendererSection(); - void Render(); + void RenderSolid(); + + void RenderLiquid(); Vector GetPosition(); size_t GetHash(); - size_t numOfFaces; + inline size_t GetSolidFacesCount() { return solidFacesCount; } + + inline size_t GetLiquidFacesCount() { return liquidFacesCount; } friend void swap(RendererSection &lhs, RendererSection &rhs); diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 255f40f..fdd961d 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -79,8 +79,8 @@ void AddFacesByBlockModel(RendererSectionData& data, const BlockFaces& model, co continue; } - data.vertices.emplace_back(); - VertexData& vertexData = data.vertices.back(); + data.solidVertices.emplace_back(); + VertexData& vertexData = data.solidVertices.back(); glm::mat4 transformed = transform * model.transform * face.transform; vertexData.positions[0] = transformed * glm::vec4(0, 0, 0, 1); @@ -161,7 +161,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI if (liquidFalling) { if (!neighborsLiquids[FaceDirection::down]) { addedFaces++; - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * glm::vec4(0, 0, 0, 1); vertex.positions[1] = transform * glm::vec4(1, 0, 0, 1); vertex.positions[2] = transform * glm::vec4(1, 0, 1, 1); @@ -170,7 +170,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI if (!neighborsLiquids[FaceDirection::up]) { addedFaces++; - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * glm::vec4(0, 1, 0, 1); vertex.positions[1] = transform * glm::vec4(0, 1, 1, 1); vertex.positions[2] = transform * glm::vec4(1, 1, 1, 1); @@ -179,7 +179,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI if (!neighborsLiquids[FaceDirection::north]) { addedFaces++; - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * glm::vec4(1, 0, 0, 1); vertex.positions[1] = transform * glm::vec4(0, 0, 0, 1); vertex.positions[2] = transform * glm::vec4(0, 1, 0, 1); @@ -188,7 +188,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI if (!neighborsLiquids[FaceDirection::south]) { addedFaces++; - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * glm::vec4(0, 0, 1, 1); vertex.positions[1] = transform * glm::vec4(1, 0, 1, 1); vertex.positions[2] = transform * glm::vec4(1, 1, 1, 1); @@ -197,7 +197,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI if (!neighborsLiquids[FaceDirection::west]) { addedFaces++; - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * glm::vec4(0, 0, 0, 1); vertex.positions[1] = transform * glm::vec4(0, 0, 1, 1); vertex.positions[2] = transform * glm::vec4(0, 1, 1, 1); @@ -206,7 +206,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI if (!neighborsLiquids[FaceDirection::east]) { addedFaces++; - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * glm::vec4(1, 0, 1, 1); vertex.positions[1] = transform * glm::vec4(1, 0, 0, 1); vertex.positions[2] = transform * glm::vec4(1, 1, 0, 1); @@ -231,7 +231,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI if (!neighborsLiquids[FaceDirection::down]) { addedFaces++; - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * glm::vec4(0, 0, 0, 1); vertex.positions[1] = transform * glm::vec4(1, 0, 0, 1); vertex.positions[2] = transform * glm::vec4(1, 0, 1, 1); @@ -267,7 +267,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI break; } - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * flowMat * nwCorner; vertex.positions[1] = transform * flowMat * swCorner; vertex.positions[2] = transform * flowMat * seCorner; @@ -292,7 +292,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI if (!neighborsLiquids[FaceDirection::north]) { addedFaces++; - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * glm::vec4(1, 0, 0, 1); vertex.positions[1] = transform * glm::vec4(0, 0, 0, 1); vertex.positions[2] = transform * nwCorner; @@ -301,7 +301,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI if (!neighborsLiquids[FaceDirection::south]) { addedFaces++; - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * glm::vec4(0, 0, 1, 1); vertex.positions[1] = transform * glm::vec4(1, 0, 1, 1); vertex.positions[2] = transform * seCorner; @@ -310,7 +310,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI if (!neighborsLiquids[FaceDirection::west]) { addedFaces++; - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * glm::vec4(0, 0, 0, 1); vertex.positions[1] = transform * glm::vec4(0, 0, 1, 1); vertex.positions[2] = transform * swCorner; @@ -319,7 +319,7 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI if (!neighborsLiquids[FaceDirection::east]) { addedFaces++; - VertexData& vertex = data.vertices.emplace_back(); + VertexData& vertex = data.liquidVertices.emplace_back(); vertex.positions[0] = transform * glm::vec4(1, 0, 1, 1); vertex.positions[1] = transform * glm::vec4(1, 0, 0, 1); vertex.positions[2] = transform * neCorner; @@ -334,8 +334,8 @@ void AddLiquidFacesByBlockModel(RendererSectionData& data, const BlockId& blockI glm::vec2 lightness; lightness.x = light.self; lightness.y = skyLight.self; - for (size_t i = data.vertices.size() - addedFaces; i < data.vertices.size(); i++) { - VertexData& vertex = data.vertices[i]; + for (size_t i = data.liquidVertices.size() - addedFaces; i < data.liquidVertices.size(); i++) { + VertexData& vertex = data.liquidVertices[i]; if (glm::length(vertex.normal) < 0.5f) { vertex.uvs[0] = TransformTextureCoord(flowData.texture, glm::vec2(0, 0), flowData.frames); @@ -462,7 +462,9 @@ RendererSectionData ParseSection(const SectionsData §ions, bool smoothLighti } } } - data.vertices.shrink_to_fit(); + + data.solidVertices.shrink_to_fit(); + data.liquidVertices.shrink_to_fit(); return data; } diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index 0f9ade6..ac69bff 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -68,7 +68,8 @@ struct VertexData { }; struct RendererSectionData { - std::vector<VertexData> vertices; + std::vector<VertexData> solidVertices; + std::vector<VertexData> liquidVertices; size_t hash = 0; Vector sectionPos; bool forced = false; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index af177d7..26c1f69 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -189,7 +189,7 @@ RendererWorld::RendererWorld(std::shared_ptr<Gal::Framebuffer> target, bool deff } it->second.UpdateData(parsing[id].renderer); } else - sections.emplace(std::make_pair(parsing[id].renderer.sectionPos, RendererSection(parsing[id].renderer, sectionsPipeline, sectionsBufferBinding))); + sections.emplace(std::make_pair(parsing[id].renderer.sectionPos, RendererSection(parsing[id].renderer, solidSectionsPipeline, solidSectionsBufferBinding, liquidSectionsPipeline, liquidSectionsBufferBinding))); parsing[id] = RendererWorld::SectionParsing(); }); @@ -267,7 +267,8 @@ RendererWorld::RendererWorld(std::shared_ptr<Gal::Framebuffer> target, bool deff RendererWorld::~RendererWorld() { size_t faces = 0; for (auto& it : sections) { - faces += it.second.numOfFaces; + faces += it.second.GetSolidFacesCount(); + faces += it.second.GetLiquidFacesCount(); } LOG(INFO) << "Total faces to render: " << faces; isRunning = false; @@ -342,7 +343,6 @@ void RendererWorld::Render(float screenRatio) { auto rawGlobalTime = (std::chrono::high_resolution_clock::now() - globalTimeStart); float globalTime = rawGlobalTime.count() / 1000000000.0f; globalSpb->Get<GlobalShaderParameters>()->globalTime = globalTime; - sectionsPipeline->Activate(); Frustum frustum(projView); renderList.clear(); @@ -362,14 +362,20 @@ void RendererWorld::Render(float screenRatio) { continue; } renderList.push_back(section.first); - renderedFaces += section.second.numOfFaces; + renderedFaces += section.second.GetSolidFacesCount(); + renderedFaces += section.second.GetLiquidFacesCount(); } glm::vec3 playerChunk(GetGameState()->GetPlayer()->pos / 16); std::sort(renderList.begin(), renderList.end(), [playerChunk](const Vector& lhs, const Vector& rhs) { return glm::distance2(lhs.glm(), playerChunk) < glm::distance2(rhs.glm(), playerChunk); }); + solidSectionsPipeline->Activate(); for (const auto& renderPos : renderList) { - sections.at(renderPos).Render(); + sections.at(renderPos).RenderSolid(); + } + liquidSectionsPipeline->Activate(); + for (const auto& renderPos : renderList) { + sections.at(renderPos).RenderLiquid(); } DebugInfo::culledSections = culledSections; DebugInfo::renderFaces = renderedFaces; @@ -418,14 +424,24 @@ void RendererWorld::Render(float screenRatio) { } void RendererWorld::PrepareRender(std::shared_ptr<Gal::Framebuffer> target, bool defferedShading) { - std::string sectionVertexSource, sectionPixelSource; + std::string solidSectionVertexSource, solidSectionPixelSource; { auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/face"); - sectionVertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); + solidSectionVertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); auto pixelAsset = defferedShading ? AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/face") : AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/fwd_face"); - sectionPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); + solidSectionPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); + } + + std::string liquidSectionVertexSource, liquidSectionPixelSource; + { + auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/liquid_face"); + liquidSectionVertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); + + auto pixelAsset = defferedShading ? AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/liquid_face") : + AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/fwd_liquid_face"); + liquidSectionPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); } std::string entitiesVertexSource, entitiesPixelSource; @@ -450,13 +466,13 @@ void RendererWorld::PrepareRender(std::shared_ptr<Gal::Framebuffer> target, bool auto gal = Gal::GetImplementation(); { - auto sectionsPLC = gal->CreatePipelineConfig(); - sectionsPLC->SetTarget(target); - sectionsPLC->AddStaticTexture("textureAtlas", AssetManager::GetTextureAtlas()); - sectionsPLC->SetVertexShader(gal->LoadVertexShader(sectionVertexSource)); - sectionsPLC->SetPixelShader(gal->LoadPixelShader(sectionPixelSource)); - sectionsPLC->SetPrimitive(Gal::Primitive::TriangleFan); - sectionsBufferBinding = sectionsPLC->BindVertexBuffer({ + auto solidSectionPLC = gal->CreatePipelineConfig(); + solidSectionPLC->SetTarget(target); + solidSectionPLC->AddStaticTexture("textureAtlas", AssetManager::GetTextureAtlas()); + solidSectionPLC->SetVertexShader(gal->LoadVertexShader(solidSectionVertexSource)); + solidSectionPLC->SetPixelShader(gal->LoadPixelShader(solidSectionPixelSource)); + solidSectionPLC->SetPrimitive(Gal::Primitive::TriangleFan); + solidSectionsBufferBinding = solidSectionPLC->BindVertexBuffer({ {"pos", Gal::Type::Vec3, 4, 1}, {"uv", Gal::Type::Vec2, 4, 1}, {"light", Gal::Type::Vec2, 4, 1}, @@ -464,9 +480,28 @@ void RendererWorld::PrepareRender(std::shared_ptr<Gal::Framebuffer> target, bool {"color", Gal::Type::Vec3, 1, 1}, {"layerAnimationAo", Gal::Type::Vec3, 1, 1}, }); - sectionsPipeline = gal->BuildPipeline(sectionsPLC); + solidSectionsPipeline = gal->BuildPipeline(solidSectionPLC); } - + + { + auto liquidSectionPLC = gal->CreatePipelineConfig(); + liquidSectionPLC->SetTarget(target); + liquidSectionPLC->AddStaticTexture("textureAtlas", AssetManager::GetTextureAtlas()); + liquidSectionPLC->SetVertexShader(gal->LoadVertexShader(liquidSectionVertexSource)); + liquidSectionPLC->SetPixelShader(gal->LoadPixelShader(liquidSectionPixelSource)); + liquidSectionPLC->SetPrimitive(Gal::Primitive::TriangleFan); + liquidSectionPLC->SetBlending(Gal::Blending::Additive); + liquidSectionsBufferBinding = liquidSectionPLC->BindVertexBuffer({ + {"pos", Gal::Type::Vec3, 4, 1}, + {"uv", Gal::Type::Vec2, 4, 1}, + {"light", Gal::Type::Vec2, 4, 1}, + {"normal", Gal::Type::Vec3, 1, 1}, + {"color", Gal::Type::Vec3, 1, 1}, + {"layerAnimationAo", Gal::Type::Vec3, 1, 1}, + }); + liquidSectionsPipeline = gal->BuildPipeline(liquidSectionPLC); + } + { auto entitiesPLC = gal->CreatePipelineConfig(); entitiesPLC->SetTarget(target); diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index 438c022..13d9739 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -43,8 +43,10 @@ class RendererWorld { std::map<Vector, RendererSection> sections; void UpdateAllSections(VectorF playerPos); std::chrono::time_point<std::chrono::high_resolution_clock> globalTimeStart; - std::shared_ptr<Gal::Pipeline> sectionsPipeline; - std::shared_ptr<Gal::BufferBinding> sectionsBufferBinding; + std::shared_ptr<Gal::Pipeline> solidSectionsPipeline; + std::shared_ptr<Gal::BufferBinding> solidSectionsBufferBinding; + std::shared_ptr<Gal::Pipeline> liquidSectionsPipeline; + std::shared_ptr<Gal::BufferBinding> liquidSectionsBufferBinding; //Entities std::vector<RendererEntity> entities; std::shared_ptr<Gal::Pipeline> entitiesPipeline; |