diff --git a/include/Nazara/Graphics/AbstractRenderQueue.hpp b/include/Nazara/Graphics/AbstractRenderQueue.hpp index 2a97b4c22..c3a9de2e4 100644 --- a/include/Nazara/Graphics/AbstractRenderQueue.hpp +++ b/include/Nazara/Graphics/AbstractRenderQueue.hpp @@ -9,14 +9,15 @@ #include #include +#include #include +#include class NzDrawable; class NzLight; class NzMaterial; -class NzModel; class NzSprite; -class NzSubMesh; +struct NzMeshData; class NAZARA_API NzAbstractRenderQueue : NzNonCopyable { @@ -26,8 +27,8 @@ class NAZARA_API NzAbstractRenderQueue : NzNonCopyable virtual void AddDrawable(const NzDrawable* drawable) = 0; virtual void AddLight(const NzLight* light) = 0; + virtual void AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) = 0; virtual void AddSprite(const NzSprite* sprite) = 0; - virtual void AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix) = 0; virtual void Clear(bool fully) = 0; }; diff --git a/include/Nazara/Graphics/DeferredRenderQueue.hpp b/include/Nazara/Graphics/DeferredRenderQueue.hpp index cb7f72a70..8cec0ecea 100644 --- a/include/Nazara/Graphics/DeferredRenderQueue.hpp +++ b/include/Nazara/Graphics/DeferredRenderQueue.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -29,22 +30,11 @@ class NAZARA_API NzDeferredRenderQueue : public NzAbstractRenderQueue, NzResourc void AddDrawable(const NzDrawable* drawable) override; void AddLight(const NzLight* light) override; + void AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) override; void AddSprite(const NzSprite* sprite) override; - void AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix) override; void Clear(bool fully); - struct SkeletalData - { - ///TODO - NzMatrix4f transformMatrix; - }; - - struct StaticData - { - NzMatrix4f transformMatrix; - }; - struct BatchedModelMaterialComparator { bool operator()(const NzMaterial* mat1, const NzMaterial* mat2); @@ -55,23 +45,17 @@ class NAZARA_API NzDeferredRenderQueue : public NzAbstractRenderQueue, NzResourc bool operator()(const NzMaterial* mat1, const NzMaterial* mat2); }; - struct BatchedSkeletalMeshComparator + struct MeshDataComparator { - bool operator()(const NzSkeletalMesh* subMesh1, const NzSkeletalMesh* subMesh2); + bool operator()(const NzMeshData& data1, const NzMeshData& data2); }; - struct BatchedStaticMeshComparator - { - bool operator()(const NzStaticMesh* subMesh1, const NzStaticMesh* subMesh2); - }; - - typedef std::map, BatchedSkeletalMeshComparator> BatchedSkeletalMeshContainer; - typedef std::map, BatchedStaticMeshComparator> BatchedStaticMeshContainer; - typedef std::map, BatchedModelMaterialComparator> BatchedModelContainer; + typedef std::map, MeshDataComparator> MeshInstanceContainer; + typedef std::map, BatchedModelMaterialComparator> ModelBatches; typedef std::map> BatchedSpriteContainer; typedef std::vector LightContainer; - BatchedModelContainer opaqueModels; + ModelBatches opaqueModels; BatchedSpriteContainer sprites; LightContainer directionalLights; LightContainer pointLights; diff --git a/include/Nazara/Graphics/ForwardRenderQueue.hpp b/include/Nazara/Graphics/ForwardRenderQueue.hpp index 24f4724c5..85710cd0d 100644 --- a/include/Nazara/Graphics/ForwardRenderQueue.hpp +++ b/include/Nazara/Graphics/ForwardRenderQueue.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -31,8 +32,8 @@ class NAZARA_API NzForwardRenderQueue : public NzAbstractRenderQueue, NzResource void AddDrawable(const NzDrawable* drawable) override; void AddLight(const NzLight* light) override; + void AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) override; void AddSprite(const NzSprite* sprite) override; - void AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix) override; void Clear(bool fully); @@ -42,35 +43,14 @@ class NAZARA_API NzForwardRenderQueue : public NzAbstractRenderQueue, NzResource bool OnResourceDestroy(const NzResource* resource, int index) override; void OnResourceReleased(const NzResource* resource, int index) override; - struct SkeletalData - { - ///TODO - NzMatrix4f transformMatrix; - }; - - struct StaticData - { - NzMatrix4f transformMatrix; - }; - - struct TransparentModel + struct TransparentModelData { NzMatrix4f transformMatrix; + NzMeshData meshData; NzSpheref boundingSphere; const NzMaterial* material; }; - struct TransparentSkeletalModel : public TransparentModel - { - ///TODO - }; - - struct TransparentStaticModel : public TransparentModel - { - const NzStaticMesh* mesh; - }; - - struct BatchedModelMaterialComparator { bool operator()(const NzMaterial* mat1, const NzMaterial* mat2); @@ -81,28 +61,21 @@ class NAZARA_API NzForwardRenderQueue : public NzAbstractRenderQueue, NzResource bool operator()(const NzMaterial* mat1, const NzMaterial* mat2); }; - struct BatchedSkeletalMeshComparator + struct MeshDataComparator { - bool operator()(const NzSkeletalMesh* subMesh1, const NzSkeletalMesh* subMesh2); + bool operator()(const NzMeshData& data1, const NzMeshData& data2); }; - struct BatchedStaticMeshComparator - { - bool operator()(const NzStaticMesh* subMesh1, const NzStaticMesh* subMesh2); - }; - - typedef std::map, BatchedSkeletalMeshComparator> BatchedSkeletalMeshContainer; - typedef std::map>, BatchedStaticMeshComparator> BatchedStaticMeshContainer; - typedef std::map, BatchedModelMaterialComparator> BatchedModelContainer; + typedef std::map>, MeshDataComparator> MeshInstanceContainer; + typedef std::map, BatchedModelMaterialComparator> ModelBatches; typedef std::map> BatchedSpriteContainer; typedef std::vector LightContainer; - typedef std::vector> TransparentModelContainer; + typedef std::vector TransparentModelContainer; - BatchedModelContainer opaqueModels; + ModelBatches opaqueModels; BatchedSpriteContainer sprites; - TransparentModelContainer transparentsModels; - std::vector transparentSkeletalModels; - std::vector transparentStaticModels; + TransparentModelContainer transparentModels; + std::vector transparentModelData; std::vector otherDrawables; LightContainer directionalLights; LightContainer lights; diff --git a/include/Nazara/Graphics/Model.hpp b/include/Nazara/Graphics/Model.hpp index 1db0e98e4..813394afa 100644 --- a/include/Nazara/Graphics/Model.hpp +++ b/include/Nazara/Graphics/Model.hpp @@ -9,19 +9,15 @@ #include #include -#include #include #include -#include #include struct NAZARA_API NzModelParameters { NzModelParameters(); - bool loadAnimation = true; bool loadMaterials = true; - NzAnimationParams animation; NzMaterialParams material; NzMeshParams mesh; @@ -32,7 +28,7 @@ class NzModel; using NzModelLoader = NzResourceLoader; -class NAZARA_API NzModel : public NzSceneNode, public NzUpdatable +class NAZARA_API NzModel : public NzSceneNode { friend NzModelLoader; friend class NzScene; @@ -41,7 +37,7 @@ class NAZARA_API NzModel : public NzSceneNode, public NzUpdatable NzModel(); NzModel(const NzModel& model); NzModel(NzModel&& model); - ~NzModel(); + virtual ~NzModel(); void AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const override; void AdvanceAnimation(float elapsedTime); @@ -65,6 +61,7 @@ class NAZARA_API NzModel : public NzSceneNode, public NzUpdatable bool HasAnimation() const; bool IsAnimationEnabled() const; + virtual bool IsAnimated() const; bool IsDrawable() const; void InvalidateBoundingVolume(); @@ -75,12 +72,11 @@ class NAZARA_API NzModel : public NzSceneNode, public NzUpdatable void Reset(); - bool SetAnimation(NzAnimation* animation); bool SetMaterial(const NzString& subMeshName, NzMaterial* material); void SetMaterial(unsigned int matIndex, NzMaterial* material); bool SetMaterial(unsigned int skinIndex, const NzString& subMeshName, NzMaterial* material); void SetMaterial(unsigned int skinIndex, unsigned int matIndex, NzMaterial* material); - void SetMesh(NzMesh* mesh); + virtual void SetMesh(NzMesh* mesh); bool SetSequence(const NzString& sequenceName); void SetSequence(unsigned int sequenceIndex); void SetSkin(unsigned int skin); @@ -89,26 +85,16 @@ class NAZARA_API NzModel : public NzSceneNode, public NzUpdatable NzModel& operator=(const NzModel& node); NzModel& operator=(NzModel&& node); - private: + protected: bool FrustumCull(const NzFrustumf& frustum) override; void InvalidateNode() override; - void Register() override; - void Unregister() override; - void Update() override; - void UpdateBoundingVolume() const; + virtual void UpdateBoundingVolume() const; std::vector m_materials; - NzAnimationRef m_animation; mutable NzBoundingVolumef m_boundingVolume; NzMeshRef m_mesh; - NzSkeleton m_skeleton; // Uniquement pour les animations squelettiques - const NzSequence* m_currentSequence; - bool m_animationEnabled; mutable bool m_boundingVolumeUpdated; - float m_interpolation; - unsigned int m_currentFrame; unsigned int m_matCount; - unsigned int m_nextFrame; unsigned int m_skin; unsigned int m_skinCount; diff --git a/include/Nazara/Utility/MeshData.hpp b/include/Nazara/Utility/MeshData.hpp new file mode 100644 index 000000000..f2bac177b --- /dev/null +++ b/include/Nazara/Utility/MeshData.hpp @@ -0,0 +1,22 @@ +// Copyright (C) 2014 Jérôme Leclercq +// This file is part of the "Nazara Engine - Utility module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_MESHDATA_HPP +#define NAZARA_MESHDATA_HPP + +#include + +class NzIndexBuffer; +class NzVertexBuffer; + +struct NzMeshData +{ + nzPrimitiveMode primitiveMode; + const NzIndexBuffer* indexBuffer; + const NzVertexBuffer* vertexBuffer; +}; + +#endif // NAZARA_MESHDATA_HPP diff --git a/src/Nazara/Graphics/DeferredGeometryPass.cpp b/src/Nazara/Graphics/DeferredGeometryPass.cpp index 5b2cfd5f9..e1a008493 100644 --- a/src/Nazara/Graphics/DeferredGeometryPass.cpp +++ b/src/Nazara/Graphics/DeferredGeometryPass.cpp @@ -58,10 +58,9 @@ bool NzDeferredGeometryPass::Process(const NzScene* scene, unsigned int firstWor if (used) { bool& renderQueueInstancing = std::get<1>(matIt.second); - NzDeferredRenderQueue::BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second); - NzDeferredRenderQueue::BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second); + NzDeferredRenderQueue::MeshInstanceContainer& meshInstances = std::get<2>(matIt.second); - if (!skeletalContainer.empty() || !staticContainer.empty()) + if (!meshInstances.empty()) { const NzMaterial* material = matIt.first; @@ -88,26 +87,16 @@ bool NzDeferredGeometryPass::Process(const NzScene* scene, unsigned int firstWor lastShader = shader; } - // Meshs squelettiques - /*if (!skeletalContainer.empty()) + // Meshes + for (auto& meshIt : meshInstances) { - NzRenderer::SetVertexBuffer(m_skinningBuffer); // Vertex buffer commun - for (auto& subMeshIt : container) - { - ///TODO - } - }*/ + const NzMeshData& meshData = meshIt.first; + std::vector& instances = meshIt.second; - // Meshs statiques - for (auto& subMeshIt : staticContainer) - { - const NzStaticMesh* mesh = subMeshIt.first; - std::vector& staticData = subMeshIt.second; - - if (!staticData.empty()) + if (!instances.empty()) { - const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer(); - const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer(); + const NzIndexBuffer* indexBuffer = meshData.indexBuffer; + const NzVertexBuffer* vertexBuffer = meshData.vertexBuffer; // Gestion du draw call avant la boucle de rendu std::function DrawFunc; @@ -130,49 +119,43 @@ bool NzDeferredGeometryPass::Process(const NzScene* scene, unsigned int firstWor NzRenderer::SetIndexBuffer(indexBuffer); NzRenderer::SetVertexBuffer(vertexBuffer); - nzPrimitiveMode primitiveMode = mesh->GetPrimitiveMode(); if (useInstancing) { + // On récupère le buffer d'instancing du Renderer et on le configure pour fonctionner avec des matrices NzVertexBuffer* instanceBuffer = NzRenderer::GetInstanceBuffer(); - instanceBuffer->SetVertexDeclaration(NzVertexDeclaration::Get(nzVertexLayout_Matrix4)); - unsigned int stride = instanceBuffer->GetStride(); - - const NzDeferredRenderQueue::StaticData* data = &staticData[0]; - unsigned int instanceCount = staticData.size(); - unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // Le nombre de sommets maximum avec la déclaration donnée plus hautg + const NzMatrix4f* instanceMatrices = &instances[0]; + unsigned int instanceCount = instances.size(); + unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // Le nombre de matrices que peut contenir le buffer while (instanceCount > 0) { + // On calcule le nombre d'instances que l'on pourra afficher cette fois-ci (Selon la taille du buffer d'instancing) unsigned int renderedInstanceCount = std::min(instanceCount, maxInstanceCount); instanceCount -= renderedInstanceCount; - NzBufferMapper mapper(instanceBuffer, nzBufferAccess_DiscardAndWrite, 0, renderedInstanceCount); - nzUInt8* ptr = reinterpret_cast(mapper.GetPointer()); + // On remplit l'instancing buffer avec nos matrices world + instanceBuffer->Fill(instanceMatrices, 0, renderedInstanceCount, true); + instanceMatrices += renderedInstanceCount; - for (unsigned int i = 0; i < renderedInstanceCount; ++i) - { - std::memcpy(ptr, data->transformMatrix, sizeof(float)*16); - - data++; - ptr += stride; - } - - mapper.Unmap(); - - InstancedDrawFunc(renderedInstanceCount, primitiveMode, 0, indexCount); + // Et on affiche + InstancedDrawFunc(renderedInstanceCount, meshData.primitiveMode, 0, indexCount); } } else { - for (const NzDeferredRenderQueue::StaticData& data : staticData) + // Sans instancing, on doit effectuer un drawcall pour chaque instance + // Cela reste néanmoins plus rapide que l'instancing en dessous d'un certain nombre d'instances + // À cause du temps de modification du buffer d'instancing + for (const NzMatrix4f& matrix : instances) { - NzRenderer::SetMatrix(nzMatrixType_World, data.transformMatrix); - DrawFunc(primitiveMode, 0, indexCount); + NzRenderer::SetMatrix(nzMatrixType_World, matrix); + DrawFunc(meshData.primitiveMode, 0, indexCount); } } - staticData.clear(); + + instances.clear(); } } } diff --git a/src/Nazara/Graphics/DeferredRenderQueue.cpp b/src/Nazara/Graphics/DeferredRenderQueue.cpp index c2cfc5bed..c4e393ec1 100644 --- a/src/Nazara/Graphics/DeferredRenderQueue.cpp +++ b/src/Nazara/Graphics/DeferredRenderQueue.cpp @@ -9,17 +9,15 @@ #include #include #include -#include -#include #include namespace { enum ResourceType { + ResourceType_IndexBuffer, ResourceType_Material, - ResourceType_SkeletalMesh, - ResourceType_StaticMesh + ResourceType_VertexBuffer }; } @@ -66,6 +64,45 @@ void NzDeferredRenderQueue::AddLight(const NzLight* light) m_forwardQueue->AddLight(light); } +void NzDeferredRenderQueue::AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) +{ + if (material->IsEnabled(nzRendererParameter_Blend)) + m_forwardQueue->AddMesh(material, meshData, meshAABB, transformMatrix); + else + { + ModelBatches::iterator it = opaqueModels.find(material); + if (it == opaqueModels.end()) + { + it = opaqueModels.insert(std::make_pair(material, ModelBatches::mapped_type())).first; + material->AddResourceListener(this, ResourceType_Material); + } + + bool& used = std::get<0>(it->second); + bool& enableInstancing = std::get<1>(it->second); + MeshInstanceContainer& meshMap = std::get<2>(it->second); + + used = true; + + MeshInstanceContainer::iterator it2 = meshMap.find(meshData); + if (it2 == meshMap.end()) + { + it2 = meshMap.insert(std::make_pair(meshData, MeshInstanceContainer::mapped_type())).first; + + if (meshData.indexBuffer) + meshData.indexBuffer->AddResourceListener(this, ResourceType_IndexBuffer); + + meshData.vertexBuffer->AddResourceListener(this, ResourceType_VertexBuffer); + } + + std::vector& instances = it2->second; + instances.push_back(transformMatrix); + + // Avons-nous suffisamment d'instances pour que le coût d'utilisation de l'instancing soit payé ? + if (instances.size() >= NAZARA_GRAPHICS_INSTANCING_MIN_INSTANCES_COUNT) + enableInstancing = true; // Apparemment oui, activons l'instancing avec ce matériau + } +} + void NzDeferredRenderQueue::AddSprite(const NzSprite* sprite) { #if NAZARA_GRAPHICS_SAFE @@ -91,75 +128,6 @@ void NzDeferredRenderQueue::AddSprite(const NzSprite* sprite) m_forwardQueue->AddSprite(sprite); } -void NzDeferredRenderQueue::AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix) -{ - switch (subMesh->GetAnimationType()) - { - case nzAnimationType_Skeletal: - { - ///TODO - /* - ** Il y a ici deux choses importantes à gérer: - ** -Pour commencer, la mise en cache de std::vector suffisamment grands pour contenir le résultat du skinning - ** l'objectif ici est d'éviter une allocation à chaque frame, donc de réutiliser un tableau existant - ** Note: Il faudrait évaluer aussi la possibilité de conserver le buffer d'une frame à l'autre. - ** Ceci permettant de ne pas skinner inutilement ce qui ne bouge pas, ou de skinner partiellement un mesh. - ** Il faut cependant voir où stocker ce set de buffers, qui doit être communs à toutes les RQ d'une même scène. - ** - ** -Ensuite, la possibilité de regrouper les modèles skinnés identiques, une centaine de soldats marchant au pas - ** ne devrait requérir qu'un skinning. - */ - NazaraError("Skeletal mesh not supported yet, sorry"); - break; - } - - case nzAnimationType_Static: - { - if (material->IsEnabled(nzRendererParameter_Blend)) - m_forwardQueue->AddSubMesh(material, subMesh, transformMatrix); - else - { - const NzStaticMesh* staticMesh = static_cast(subMesh); - - auto it = opaqueModels.find(material); - if (it == opaqueModels.end()) - { - it = opaqueModels.insert(std::make_pair(material, BatchedModelContainer::mapped_type())).first; - material->AddResourceListener(this, ResourceType_Material); - } - - bool& used = std::get<0>(it->second); - bool& enableInstancing = std::get<1>(it->second); - - used = true; - - auto& meshMap = std::get<3>(it->second); - - auto it2 = meshMap.find(staticMesh); - if (it2 == meshMap.end()) - { - it2 = meshMap.insert(std::make_pair(staticMesh, BatchedStaticMeshContainer::mapped_type())).first; - staticMesh->AddResourceListener(this, ResourceType_StaticMesh); - } - - std::vector& staticDataContainer = it2->second; - - unsigned int instanceCount = staticDataContainer.size() + 1; - - // Avons-nous suffisamment d'instances pour que le coût d'utilisation de l'instancing soit payé ? - if (instanceCount >= NAZARA_GRAPHICS_INSTANCING_MIN_INSTANCES_COUNT) - enableInstancing = true; // Apparemment oui, activons l'instancing avec ce matériau - - staticDataContainer.resize(instanceCount); - StaticData& data = staticDataContainer.back(); - data.transformMatrix = transformMatrix; - } - - break; - } - } -} - void NzDeferredRenderQueue::Clear(bool fully) { directionalLights.clear(); @@ -173,18 +141,15 @@ void NzDeferredRenderQueue::Clear(bool fully) const NzMaterial* material = matIt.first; material->RemoveResourceListener(this); - BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second); - for (auto& meshIt : skeletalContainer) + MeshInstanceContainer& instances = std::get<2>(matIt.second); + for (auto& instanceIt : instances) { - const NzSkeletalMesh* skeletalMesh = meshIt.first; - skeletalMesh->RemoveResourceListener(this); - } + const NzMeshData& renderData = instanceIt.first; - BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second); - for (auto& meshIt : staticContainer) - { - const NzStaticMesh* staticMesh = meshIt.first; - staticMesh->RemoveResourceListener(this); + if (renderData.indexBuffer) + renderData.indexBuffer->RemoveResourceListener(this); + + renderData.vertexBuffer->RemoveResourceListener(this); } } @@ -199,23 +164,41 @@ bool NzDeferredRenderQueue::OnResourceDestroy(const NzResource* resource, int in { switch (index) { + case ResourceType_IndexBuffer: + { + for (auto& modelPair : opaqueModels) + { + MeshInstanceContainer& meshes = std::get<2>(modelPair.second); + for (auto it = meshes.begin(); it != meshes.end();) + { + const NzMeshData& renderData = it->first; + if (renderData.indexBuffer == resource) + it = meshes.erase(it); + else + ++it; + } + } + break; + } + case ResourceType_Material: opaqueModels.erase(static_cast(resource)); break; - case ResourceType_SkeletalMesh: + case ResourceType_VertexBuffer: { - for (auto& pair : opaqueModels) - std::get<2>(pair.second).erase(static_cast(resource)); - - break; - } - - case ResourceType_StaticMesh: - { - for (auto& pair : opaqueModels) - std::get<3>(pair.second).erase(static_cast(resource)); - + for (auto& modelPair : opaqueModels) + { + MeshInstanceContainer& meshes = std::get<2>(modelPair.second); + for (auto it = meshes.begin(); it != meshes.end();) + { + const NzMeshData& renderData = it->first; + if (renderData.vertexBuffer == resource) + it = meshes.erase(it); + else + ++it; + } + } break; } } @@ -225,7 +208,58 @@ bool NzDeferredRenderQueue::OnResourceDestroy(const NzResource* resource, int in void NzDeferredRenderQueue::OnResourceReleased(const NzResource* resource, int index) { - OnResourceDestroy(resource, index); + // La ressource vient d'être libérée, nous ne pouvons donc plus utiliser la méthode traditionnelle de recherche + // des pointeurs stockés (À cause de la fonction de triage utilisant des spécificités des ressources) + + switch (index) + { + case ResourceType_IndexBuffer: + { + for (auto& modelPair : opaqueModels) + { + MeshInstanceContainer& meshes = std::get<2>(modelPair.second); + for (auto it = meshes.begin(); it != meshes.end();) + { + const NzMeshData& renderData = it->first; + if (renderData.indexBuffer == resource) + it = meshes.erase(it); + else + ++it; + } + } + break; + } + + case ResourceType_Material: + { + for (auto it = opaqueModels.begin(); it != opaqueModels.end(); ++it) + { + if (it->first == resource) + { + opaqueModels.erase(it); + break; + } + } + break; + } + + case ResourceType_VertexBuffer: + { + for (auto& modelPair : opaqueModels) + { + MeshInstanceContainer& meshes = std::get<2>(modelPair.second); + for (auto it = meshes.begin(); it != meshes.end();) + { + const NzMeshData& renderData = it->first; + if (renderData.vertexBuffer == resource) + it = meshes.erase(it); + else + ++it; + } + } + break; + } + } } bool NzDeferredRenderQueue::BatchedModelMaterialComparator::operator()(const NzMaterial* mat1, const NzMaterial* mat2) @@ -237,7 +271,6 @@ bool NzDeferredRenderQueue::BatchedModelMaterialComparator::operator()(const NzM const NzShader* shader1 = mat1->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); - if (shader1 != shader2) return shader1 < shader2; @@ -258,7 +291,6 @@ bool NzDeferredRenderQueue::BatchedSpriteMaterialComparator::operator()(const Nz const NzShader* shader1 = mat1->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); - if (shader1 != shader2) return shader1 < shader2; @@ -270,38 +302,20 @@ bool NzDeferredRenderQueue::BatchedSpriteMaterialComparator::operator()(const Nz return mat1 < mat2; } -bool NzDeferredRenderQueue::BatchedSkeletalMeshComparator::operator()(const NzSkeletalMesh* subMesh1, const NzSkeletalMesh* subMesh2) +bool NzDeferredRenderQueue::MeshDataComparator::operator()(const NzMeshData& data1, const NzMeshData& data2) { - const NzIndexBuffer* iBuffer1 = subMesh1->GetIndexBuffer(); - const NzBuffer* buffer1 = (iBuffer1) ? iBuffer1->GetBuffer() : nullptr; + const NzBuffer* buffer1; + const NzBuffer* buffer2; - const NzIndexBuffer* iBuffer2 = subMesh1->GetIndexBuffer(); - const NzBuffer* buffer2 = (iBuffer2) ? iBuffer2->GetBuffer() : nullptr; - - if (buffer1 == buffer2) - return subMesh1 < subMesh2; - else - return buffer2 < buffer2; -} - -bool NzDeferredRenderQueue::BatchedStaticMeshComparator::operator()(const NzStaticMesh* subMesh1, const NzStaticMesh* subMesh2) -{ - const NzIndexBuffer* iBuffer1 = subMesh1->GetIndexBuffer(); - const NzBuffer* buffer1 = (iBuffer1) ? iBuffer1->GetBuffer() : nullptr; - - const NzIndexBuffer* iBuffer2 = subMesh2->GetIndexBuffer(); - const NzBuffer* buffer2 = (iBuffer2) ? iBuffer2->GetBuffer() : nullptr; - - if (buffer1 == buffer2) - { - buffer1 = subMesh1->GetVertexBuffer()->GetBuffer(); - buffer2 = subMesh2->GetVertexBuffer()->GetBuffer(); - - if (buffer1 == buffer2) - return subMesh1 < subMesh2; - else - return buffer1 < buffer2; - } - else + buffer1 = (data1.indexBuffer) ? data1.indexBuffer->GetBuffer() : nullptr; + buffer2 = (data2.indexBuffer) ? data2.indexBuffer->GetBuffer() : nullptr; + if (buffer1 != buffer2) return buffer1 < buffer2; + + buffer1 = data1.vertexBuffer->GetBuffer(); + buffer2 = data2.vertexBuffer->GetBuffer(); + if (buffer1 != buffer2) + return buffer1 < buffer2; + + return data1.primitiveMode < data2.primitiveMode; } diff --git a/src/Nazara/Graphics/ForwardRenderQueue.cpp b/src/Nazara/Graphics/ForwardRenderQueue.cpp index 261421a25..3b4ee7f4e 100644 --- a/src/Nazara/Graphics/ForwardRenderQueue.cpp +++ b/src/Nazara/Graphics/ForwardRenderQueue.cpp @@ -16,9 +16,9 @@ namespace { enum ResourceType { + ResourceType_IndexBuffer, ResourceType_Material, - ResourceType_SkeletalMesh, - ResourceType_StaticMesh + ResourceType_VertexBuffer }; } @@ -68,6 +68,59 @@ void NzForwardRenderQueue::AddLight(const NzLight* light) } } +void NzForwardRenderQueue::AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) +{ + if (material->IsEnabled(nzRendererParameter_Blend)) + { + unsigned int index = transparentModelData.size(); + transparentModelData.resize(index+1); + + TransparentModelData& data = transparentModelData.back(); + data.boundingSphere = NzSpheref(transformMatrix.GetTranslation() + meshAABB.GetCenter(), meshAABB.GetSquaredRadius()); + data.material = material; + data.meshData = meshData; + data.transformMatrix = transformMatrix; + + transparentModels.push_back(index); + } + else + { + ModelBatches::iterator it = opaqueModels.find(material); + if (it == opaqueModels.end()) + { + it = opaqueModels.insert(std::make_pair(material, ModelBatches::mapped_type())).first; + material->AddResourceListener(this, ResourceType_Material); + } + + bool& used = std::get<0>(it->second); + bool& enableInstancing = std::get<1>(it->second); + MeshInstanceContainer& meshMap = std::get<2>(it->second); + + used = true; + + MeshInstanceContainer::iterator it2 = meshMap.find(meshData); + if (it2 == meshMap.end()) + { + it2 = meshMap.insert(std::make_pair(meshData, MeshInstanceContainer::mapped_type())).first; + + NzSpheref& squaredBoundingSphere = it2->second.first; + squaredBoundingSphere.Set(meshAABB.GetSquaredBoundingSphere()); + + if (meshData.indexBuffer) + meshData.indexBuffer->AddResourceListener(this, ResourceType_IndexBuffer); + + meshData.vertexBuffer->AddResourceListener(this, ResourceType_VertexBuffer); + } + + std::vector& instances = it2->second.second; + instances.push_back(transformMatrix); + + // Avons-nous suffisamment d'instances pour que le coût d'utilisation de l'instancing soit payé ? + if (instances.size() >= NAZARA_GRAPHICS_INSTANCING_MIN_INSTANCES_COUNT) + enableInstancing = true; // Apparemment oui, activons l'instancing avec ce matériau + } +} + void NzForwardRenderQueue::AddSprite(const NzSprite* sprite) { #if NAZARA_GRAPHICS_SAFE @@ -87,100 +140,13 @@ void NzForwardRenderQueue::AddSprite(const NzSprite* sprite) sprites[sprite->GetMaterial()].push_back(sprite); } -void NzForwardRenderQueue::AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix) -{ - switch (subMesh->GetAnimationType()) - { - case nzAnimationType_Skeletal: - { - ///TODO - /* - ** Il y a ici deux choses importantes à gérer: - ** -Pour commencer, la mise en cache de std::vector suffisamment grands pour contenir le résultat du skinning - ** l'objectif ici est d'éviter une allocation à chaque frame, donc de réutiliser un tableau existant - ** Note: Il faudrait évaluer aussi la possibilité de conserver le buffer d'une frame à l'autre. - ** Ceci permettant de ne pas skinner inutilement ce qui ne bouge pas, ou de skinner partiellement un mesh. - ** Il faut cependant voir où stocker ce set de buffers, qui doit être communs à toutes les RQ d'une même scène. - ** - ** -Ensuite, la possibilité de regrouper les modèles skinnés identiques, une centaine de soldats marchant au pas - ** ne devrait requérir qu'un skinning. - */ - NazaraError("Skeletal mesh not supported yet, sorry"); - break; - } - - case nzAnimationType_Static: - { - const NzStaticMesh* staticMesh = static_cast(subMesh); - - if (material->IsEnabled(nzRendererParameter_Blend)) - { - unsigned int index = transparentStaticModels.size(); - transparentStaticModels.resize(index+1); - - const NzBoxf& aabb = staticMesh->GetAABB(); - - TransparentStaticModel& data = transparentStaticModels.back(); - data.boundingSphere = NzSpheref(transformMatrix.GetTranslation() + aabb.GetCenter(), aabb.GetSquaredRadius()); - data.material = material; - data.mesh = staticMesh; - data.transformMatrix = transformMatrix; - - transparentsModels.push_back(std::make_pair(index, true)); - } - else - { - auto it = opaqueModels.find(material); - if (it == opaqueModels.end()) - { - it = opaqueModels.insert(std::make_pair(material, BatchedModelContainer::mapped_type())).first; - material->AddResourceListener(this, ResourceType_Material); - } - - bool& used = std::get<0>(it->second); - bool& enableInstancing = std::get<1>(it->second); - - used = true; - - auto& meshMap = std::get<3>(it->second); - - auto it2 = meshMap.find(staticMesh); - if (it2 == meshMap.end()) - { - it2 = meshMap.insert(std::make_pair(staticMesh, BatchedStaticMeshContainer::mapped_type())).first; - staticMesh->AddResourceListener(this, ResourceType_StaticMesh); - - NzSpheref& squaredBoundingSphere = it2->second.first; - squaredBoundingSphere.Set(staticMesh->GetAABB().GetSquaredBoundingSphere()); - ///TODO: Écouter le StaticMesh pour repérer tout changement de géométrie - } - - std::vector& staticDataContainer = it2->second.second; - - unsigned int instanceCount = staticDataContainer.size() + 1; - - // Avons-nous suffisamment d'instances pour que le coût d'utilisation de l'instancing soit payé ? - if (instanceCount >= NAZARA_GRAPHICS_INSTANCING_MIN_INSTANCES_COUNT) - enableInstancing = true; // Apparemment oui, activons l'instancing avec ce matériau - - staticDataContainer.resize(instanceCount); - StaticData& data = staticDataContainer.back(); - data.transformMatrix = transformMatrix; - } - - break; - } - } -} - void NzForwardRenderQueue::Clear(bool fully) { directionalLights.clear(); lights.clear(); otherDrawables.clear(); - transparentsModels.clear(); - transparentSkeletalModels.clear(); - transparentStaticModels.clear(); + transparentModels.clear(); + transparentModelData.clear(); if (fully) { @@ -189,18 +155,15 @@ void NzForwardRenderQueue::Clear(bool fully) const NzMaterial* material = matIt.first; material->RemoveResourceListener(this); - BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second); - for (auto& meshIt : skeletalContainer) + MeshInstanceContainer& instances = std::get<2>(matIt.second); + for (auto& instanceIt : instances) { - const NzSkeletalMesh* skeletalMesh = meshIt.first; - skeletalMesh->RemoveResourceListener(this); - } + const NzMeshData& renderData = instanceIt.first; - BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second); - for (auto& meshIt : staticContainer) - { - const NzStaticMesh* staticMesh = meshIt.first; - staticMesh->RemoveResourceListener(this); + if (renderData.indexBuffer) + renderData.indexBuffer->RemoveResourceListener(this); + + renderData.vertexBuffer->RemoveResourceListener(this); } } opaqueModels.clear(); @@ -212,15 +175,10 @@ void NzForwardRenderQueue::Sort(const NzAbstractViewer* viewer) { struct TransparentModelComparator { - bool operator()(const std::pair& index1, const std::pair& index2) + bool operator()(unsigned int index1, unsigned int index2) { - const NzSpheref& sphere1 = (index1.second) ? - queue->transparentStaticModels[index1.first].boundingSphere : - queue->transparentSkeletalModels[index1.first].boundingSphere; - - const NzSpheref& sphere2 = (index2.second) ? - queue->transparentStaticModels[index2.first].boundingSphere : - queue->transparentSkeletalModels[index2.first].boundingSphere; + const NzSpheref& sphere1 = queue->transparentModelData[index1].boundingSphere; + const NzSpheref& sphere2 = queue->transparentModelData[index2].boundingSphere; NzVector3f position1 = sphere1.GetNegativeVertex(viewerNormal); NzVector3f position2 = sphere2.GetNegativeVertex(viewerNormal); @@ -234,30 +192,48 @@ void NzForwardRenderQueue::Sort(const NzAbstractViewer* viewer) }; TransparentModelComparator comparator {this, viewer->GetFrustum().GetPlane(nzFrustumPlane_Near), viewer->GetForward()}; - std::sort(transparentsModels.begin(), transparentsModels.end(), comparator); + std::sort(transparentModels.begin(), transparentModels.end(), comparator); } bool NzForwardRenderQueue::OnResourceDestroy(const NzResource* resource, int index) { switch (index) { + case ResourceType_IndexBuffer: + { + for (auto& modelPair : opaqueModels) + { + MeshInstanceContainer& meshes = std::get<2>(modelPair.second); + for (auto it = meshes.begin(); it != meshes.end();) + { + const NzMeshData& renderData = it->first; + if (renderData.indexBuffer == resource) + it = meshes.erase(it); + else + ++it; + } + } + break; + } + case ResourceType_Material: opaqueModels.erase(static_cast(resource)); break; - case ResourceType_SkeletalMesh: + case ResourceType_VertexBuffer: { - for (auto& pair : opaqueModels) - std::get<2>(pair.second).erase(static_cast(resource)); - - break; - } - - case ResourceType_StaticMesh: - { - for (auto& pair : opaqueModels) - std::get<3>(pair.second).erase(static_cast(resource)); - + for (auto& modelPair : opaqueModels) + { + MeshInstanceContainer& meshes = std::get<2>(modelPair.second); + for (auto it = meshes.begin(); it != meshes.end();) + { + const NzMeshData& renderData = it->first; + if (renderData.vertexBuffer == resource) + it = meshes.erase(it); + else + ++it; + } + } break; } } @@ -272,7 +248,25 @@ void NzForwardRenderQueue::OnResourceReleased(const NzResource* resource, int in switch (index) { + case ResourceType_IndexBuffer: + { + for (auto& modelPair : opaqueModels) + { + MeshInstanceContainer& meshes = std::get<2>(modelPair.second); + for (auto it = meshes.begin(); it != meshes.end();) + { + const NzMeshData& renderData = it->first; + if (renderData.indexBuffer == resource) + it = meshes.erase(it); + else + ++it; + } + } + break; + } + case ResourceType_Material: + { for (auto it = opaqueModels.begin(); it != opaqueModels.end(); ++it) { if (it->first == resource) @@ -282,38 +276,20 @@ void NzForwardRenderQueue::OnResourceReleased(const NzResource* resource, int in } } break; - - case ResourceType_SkeletalMesh: - { - for (auto& pair : opaqueModels) - { - BatchedSkeletalMeshContainer& container = std::get<2>(pair.second); - - for (auto it = container.begin(); it != container.end(); ++it) - { - if (it->first == resource) - { - container.erase(it); - break; - } - } - } - break; } - case ResourceType_StaticMesh: + case ResourceType_VertexBuffer: { - for (auto& pair : opaqueModels) + for (auto& modelPair : opaqueModels) { - BatchedStaticMeshContainer& container = std::get<3 >(pair.second); - - for (auto it = container.begin(); it != container.end(); ++it) + MeshInstanceContainer& meshes = std::get<2>(modelPair.second); + for (auto it = meshes.begin(); it != meshes.end();) { - if (it->first == resource) - { - container.erase(it); - break; - } + const NzMeshData& renderData = it->first; + if (renderData.vertexBuffer == resource) + it = meshes.erase(it); + else + ++it; } } break; @@ -349,8 +325,8 @@ bool NzForwardRenderQueue::BatchedSpriteMaterialComparator::operator()(const NzM if (uberShader1 != uberShader2) return uberShader1 < uberShader2; - const NzShader* shader1 = mat1->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); - const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); + const NzShader* shader1 = mat1->GetShaderInstance()->GetShader(); + const NzShader* shader2 = mat2->GetShaderInstance()->GetShader(); if (shader1 != shader2) return shader1 < shader2; @@ -363,38 +339,20 @@ bool NzForwardRenderQueue::BatchedSpriteMaterialComparator::operator()(const NzM return mat1 < mat2; } -bool NzForwardRenderQueue::BatchedSkeletalMeshComparator::operator()(const NzSkeletalMesh* subMesh1, const NzSkeletalMesh* subMesh2) +bool NzForwardRenderQueue::MeshDataComparator::operator()(const NzMeshData& data1, const NzMeshData& data2) { - const NzIndexBuffer* iBuffer1 = subMesh1->GetIndexBuffer(); - const NzBuffer* buffer1 = (iBuffer1) ? iBuffer1->GetBuffer() : nullptr; + const NzBuffer* buffer1; + const NzBuffer* buffer2; - const NzIndexBuffer* iBuffer2 = subMesh1->GetIndexBuffer(); - const NzBuffer* buffer2 = (iBuffer2) ? iBuffer2->GetBuffer() : nullptr; - - if (buffer1 == buffer2) - return subMesh1 < subMesh2; - else - return buffer2 < buffer2; -} - -bool NzForwardRenderQueue::BatchedStaticMeshComparator::operator()(const NzStaticMesh* subMesh1, const NzStaticMesh* subMesh2) -{ - const NzIndexBuffer* iBuffer1 = subMesh1->GetIndexBuffer(); - const NzBuffer* buffer1 = (iBuffer1) ? iBuffer1->GetBuffer() : nullptr; - - const NzIndexBuffer* iBuffer2 = subMesh2->GetIndexBuffer(); - const NzBuffer* buffer2 = (iBuffer2) ? iBuffer2->GetBuffer() : nullptr; - - if (buffer1 == buffer2) - { - buffer1 = subMesh1->GetVertexBuffer()->GetBuffer(); - buffer2 = subMesh2->GetVertexBuffer()->GetBuffer(); - - if (buffer1 == buffer2) - return subMesh1 < subMesh2; - else - return buffer1 < buffer2; - } - else + buffer1 = (data1.indexBuffer) ? data1.indexBuffer->GetBuffer() : nullptr; + buffer2 = (data2.indexBuffer) ? data2.indexBuffer->GetBuffer() : nullptr; + if (buffer1 != buffer2) return buffer1 < buffer2; + + buffer1 = data1.vertexBuffer->GetBuffer(); + buffer2 = data2.vertexBuffer->GetBuffer(); + if (buffer1 != buffer2) + return buffer1 < buffer2; + + return data1.primitiveMode < data2.primitiveMode; } diff --git a/src/Nazara/Graphics/ForwardRenderTechnique.cpp b/src/Nazara/Graphics/ForwardRenderTechnique.cpp index 5789f7489..8a3ab4e2c 100644 --- a/src/Nazara/Graphics/ForwardRenderTechnique.cpp +++ b/src/Nazara/Graphics/ForwardRenderTechnique.cpp @@ -84,12 +84,12 @@ bool NzForwardRenderTechnique::Draw(const NzScene* scene) const if (!m_renderQueue.opaqueModels.empty()) DrawOpaqueModels(scene); + if (!m_renderQueue.transparentModels.empty()) + DrawTransparentModels(scene); + if (!m_renderQueue.sprites.empty()) DrawSprites(scene); - if (!m_renderQueue.transparentsModels.empty()) - DrawTransparentModels(scene); - // Les autres drawables (Exemple: Terrain) for (const NzDrawable* drawable : m_renderQueue.otherDrawables) drawable->Draw(); @@ -174,10 +174,9 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const if (used) { bool& renderQueueInstancing = std::get<1>(matIt.second); - NzForwardRenderQueue::BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second); - NzForwardRenderQueue::BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second); + NzForwardRenderQueue::MeshInstanceContainer& meshInstances = std::get<2>(matIt.second); - if (!skeletalContainer.empty() || !staticContainer.empty()) + if (!meshInstances.empty()) { const NzMaterial* material = matIt.first; @@ -203,28 +202,17 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const lastShader = shader; } - - // Meshs squelettiques - /*if (!skeletalContainer.empty()) - { - NzRenderer::SetVertexBuffer(m_skinningBuffer); // Vertex buffer commun - for (auto& subMeshIt : container) - { - ///TODO - } - }*/ - - // Meshs statiques - for (auto& subMeshIt : staticContainer) + // Meshes + for (auto& subMeshIt : meshInstances) { + const NzMeshData& meshData = subMeshIt.first; const NzSpheref& boundingSphere = subMeshIt.second.first; - const NzStaticMesh* mesh = subMeshIt.first; - std::vector& staticData = subMeshIt.second.second; + std::vector& instances = subMeshIt.second.second; - if (!staticData.empty()) + if (!instances.empty()) { - const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer(); - const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer(); + const NzIndexBuffer* indexBuffer = meshData.indexBuffer; + const NzVertexBuffer* vertexBuffer = meshData.vertexBuffer; // Gestion du draw call avant la boucle de rendu std::function DrawFunc; @@ -247,15 +235,12 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const NzRenderer::SetIndexBuffer(indexBuffer); NzRenderer::SetVertexBuffer(vertexBuffer); - nzPrimitiveMode primitiveMode = mesh->GetPrimitiveMode(); if (instancing) { + // On calcule le nombre d'instances que l'on pourra afficher cette fois-ci (Selon la taille du buffer d'instancing) NzVertexBuffer* instanceBuffer = NzRenderer::GetInstanceBuffer(); - instanceBuffer->SetVertexDeclaration(NzVertexDeclaration::Get(nzVertexLayout_Matrix4)); - unsigned int stride = instanceBuffer->GetStride(); - // Avec l'instancing, impossible de sélectionner les lumières pour chaque objet // Du coup, il n'est activé que pour les lumières directionnelles unsigned int lightCount = m_directionalLights.GetLightCount(); @@ -288,32 +273,26 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i); } - const NzForwardRenderQueue::StaticData* data = &staticData[0]; - unsigned int instanceCount = staticData.size(); - unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); + const NzMatrix4f* instanceMatrices = &instances[0]; + unsigned int instanceCount = instances.size(); + unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // On calcule le nombre d'instances que l'on pourra afficher cette fois-ci (Selon la taille du buffer d'instancing) while (instanceCount > 0) { + // On calcule le nombre d'instances que l'on pourra afficher cette fois-ci (Selon la taille du buffer d'instancing) unsigned int renderedInstanceCount = std::min(instanceCount, maxInstanceCount); instanceCount -= renderedInstanceCount; - NzBufferMapper mapper(instanceBuffer, nzBufferAccess_DiscardAndWrite, 0, renderedInstanceCount); - nzUInt8* ptr = reinterpret_cast(mapper.GetPointer()); + // On remplit l'instancing buffer avec nos matrices world + instanceBuffer->Fill(instanceMatrices, 0, renderedInstanceCount, true); + instanceMatrices += renderedInstanceCount; - for (unsigned int i = 0; i < renderedInstanceCount; ++i) - { - std::memcpy(ptr, data->transformMatrix, sizeof(float)*16); - - data++; - ptr += stride; - } - - mapper.Unmap(); - - InstancedDrawFunc(renderedInstanceCount, primitiveMode, 0, indexCount); + // Et on affiche + InstancedDrawFunc(renderedInstanceCount, meshData.primitiveMode, 0, indexCount); } } + // On n'oublie pas de désactiver le blending pour ne pas interférer sur le reste du rendu NzRenderer::Enable(nzRendererParameter_Blend, false); NzRenderer::SetDepthFunc(oldDepthFunc); } @@ -321,16 +300,16 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const { if (lightUniforms->exists) { - for (const NzForwardRenderQueue::StaticData& data : staticData) + for (const NzMatrix4f& matrix : instances) { unsigned int directionalLightCount = m_directionalLights.GetLightCount(); - unsigned int otherLightCount = m_lights.ComputeClosestLights(data.transformMatrix.GetTranslation() + boundingSphere.GetPosition(), boundingSphere.radius, m_maxLightPassPerObject*NAZARA_GRAPHICS_MAX_LIGHTPERPASS - directionalLightCount); + unsigned int otherLightCount = m_lights.ComputeClosestLights(matrix.GetTranslation() + boundingSphere.GetPosition(), boundingSphere.radius, m_maxLightPassPerObject*NAZARA_GRAPHICS_MAX_LIGHTPERPASS - directionalLightCount); unsigned int lightCount = directionalLightCount + otherLightCount; - NzRenderer::SetMatrix(nzMatrixType_World, data.transformMatrix); + NzRenderer::SetMatrix(nzMatrixType_World, matrix); unsigned int directionalLightIndex = 0; unsigned int otherLightIndex = 0; - nzRendererComparison oldDepthFunc = NzRenderer::GetDepthFunc(); + nzRendererComparison oldDepthFunc = NzRenderer::GetDepthFunc(); // Dans le cas où nous aurions à le changer unsigned int passCount = (lightCount == 0) ? 1 : (lightCount-1)/NAZARA_GRAPHICS_MAX_LIGHTPERPASS + 1; for (unsigned int pass = 0; pass < passCount; ++pass) @@ -349,6 +328,7 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const NzRenderer::SetDepthFunc(nzRendererComparison_Equal); } + // On active les lumières de cette passe-ci for (unsigned int i = 0; i < renderedLightCount; ++i) { if (directionalLightIndex >= directionalLightCount) @@ -357,10 +337,12 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const m_directionalLights.GetLight(directionalLightIndex++)->Enable(shader, lightUniforms->uniforms, lightUniforms->offset*i); } + // On désactive l'éventuel surplus for (unsigned int i = renderedLightCount; i < NAZARA_GRAPHICS_MAX_LIGHTPERPASS; ++i) NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i); - DrawFunc(primitiveMode, 0, indexCount); + // Et on passe à l'affichage + DrawFunc(meshData.primitiveMode, 0, indexCount); } NzRenderer::Enable(nzRendererParameter_Blend, false); @@ -369,14 +351,17 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const } else { - for (const NzForwardRenderQueue::StaticData& data : staticData) + // Sans instancing, on doit effectuer un drawcall pour chaque instance + // Cela reste néanmoins plus rapide que l'instancing en dessous d'un certain nombre d'instances + // À cause du temps de modification du buffer d'instancing + for (const NzMatrix4f& matrix : instances) { - NzRenderer::SetMatrix(nzMatrixType_World, data.transformMatrix); - DrawFunc(primitiveMode, 0, indexCount); + NzRenderer::SetMatrix(nzMatrixType_World, matrix); + DrawFunc(meshData.primitiveMode, 0, indexCount); } } } - staticData.clear(); + instances.clear(); } } } @@ -471,12 +456,12 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene) const const NzShader* lastShader = nullptr; unsigned int lightCount = 0; - for (const std::pair& pair : m_renderQueue.transparentsModels) + for (unsigned int index : m_renderQueue.transparentModels) { + const NzForwardRenderQueue::TransparentModelData& modelData = m_renderQueue.transparentModelData[index]; + // Matériau - const NzMaterial* material = (pair.second) ? - m_renderQueue.transparentStaticModels[pair.first].material : - m_renderQueue.transparentSkeletalModels[pair.first].material; + const NzMaterial* material = modelData.material; // On commence par appliquer du matériau (et récupérer le shader ainsi activé) const NzShader* shader = material->Apply(); @@ -501,52 +486,43 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene) const } // Mesh - if (pair.second) + const NzMatrix4f& matrix = modelData.transformMatrix; + const NzMeshData& meshData = modelData.meshData; + + const NzIndexBuffer* indexBuffer = meshData.indexBuffer; + const NzVertexBuffer* vertexBuffer = meshData.vertexBuffer; + + // Gestion du draw call avant la boucle de rendu + std::function DrawFunc; + unsigned int indexCount; + + if (indexBuffer) { - NzForwardRenderQueue::TransparentStaticModel& staticModel = m_renderQueue.transparentStaticModels[pair.first]; - - const NzMatrix4f& matrix = staticModel.transformMatrix; - const NzStaticMesh* mesh = staticModel.mesh; - - const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer(); - const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer(); - - // Gestion du draw call avant la boucle de rendu - std::function DrawFunc; - unsigned int indexCount; - - if (indexBuffer) - { - DrawFunc = NzRenderer::DrawIndexedPrimitives; - indexCount = indexBuffer->GetIndexCount(); - } - else - { - DrawFunc = NzRenderer::DrawPrimitives; - indexCount = vertexBuffer->GetVertexCount(); - } - - NzRenderer::SetIndexBuffer(indexBuffer); - NzRenderer::SetVertexBuffer(vertexBuffer); - - // Calcul des lumières les plus proches - if (lightCount < NAZARA_GRAPHICS_MAX_LIGHTPERPASS && !m_lights.IsEmpty()) - { - unsigned int count = std::min(NAZARA_GRAPHICS_MAX_LIGHTPERPASS - lightCount, m_lights.ComputeClosestLights(matrix.GetTranslation() + staticModel.boundingSphere.GetPosition(), staticModel.boundingSphere.radius, NAZARA_GRAPHICS_MAX_LIGHTPERPASS)); - for (unsigned int i = 0; i < count; ++i) - m_lights.GetResult(i)->Enable(shader, lightUniforms->uniforms, lightUniforms->offset*(lightCount++)); - } - - for (unsigned int i = lightCount; i < NAZARA_GRAPHICS_MAX_LIGHTPERPASS; ++i) - NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i); - - NzRenderer::SetMatrix(nzMatrixType_World, matrix); - DrawFunc(mesh->GetPrimitiveMode(), 0, indexCount); + DrawFunc = NzRenderer::DrawIndexedPrimitives; + indexCount = indexBuffer->GetIndexCount(); } else { - ///TODO + DrawFunc = NzRenderer::DrawPrimitives; + indexCount = vertexBuffer->GetVertexCount(); } + + NzRenderer::SetIndexBuffer(indexBuffer); + NzRenderer::SetVertexBuffer(vertexBuffer); + + // Calcul des lumières les plus proches + if (lightCount < NAZARA_GRAPHICS_MAX_LIGHTPERPASS && !m_lights.IsEmpty()) + { + unsigned int count = std::min(NAZARA_GRAPHICS_MAX_LIGHTPERPASS - lightCount, m_lights.ComputeClosestLights(matrix.GetTranslation() + modelData.boundingSphere.GetPosition(), modelData.boundingSphere.radius, NAZARA_GRAPHICS_MAX_LIGHTPERPASS)); + for (unsigned int i = 0; i < count; ++i) + m_lights.GetResult(i)->Enable(shader, lightUniforms->uniforms, lightUniforms->offset*(lightCount++)); + } + + for (unsigned int i = lightCount; i < NAZARA_GRAPHICS_MAX_LIGHTPERPASS; ++i) + NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i); + + NzRenderer::SetMatrix(nzMatrixType_World, matrix); + DrawFunc(meshData.primitiveMode, 0, indexCount); } } diff --git a/src/Nazara/Graphics/Loaders/Mesh/Loader.cpp b/src/Nazara/Graphics/Loaders/Mesh/Loader.cpp index 26c4b118e..d64597248 100644 --- a/src/Nazara/Graphics/Loaders/Mesh/Loader.cpp +++ b/src/Nazara/Graphics/Loaders/Mesh/Loader.cpp @@ -11,7 +11,7 @@ namespace { - nzTernary Check(NzInputStream& stream, const NzModelParameters& parameters) + nzTernary CheckStatic(NzInputStream& stream, const NzModelParameters& parameters) { NazaraUnused(stream); NazaraUnused(parameters); @@ -19,7 +19,7 @@ namespace return nzTernary_Unknown; } - bool Load(NzModel* model, NzInputStream& stream, const NzModelParameters& parameters) + bool LoadStatic(NzModel* model, NzInputStream& stream, const NzModelParameters& parameters) { NazaraUnused(parameters); @@ -31,6 +31,12 @@ namespace return false; } + if (mesh->IsAnimable()) + { + NazaraError("Can't load static mesh into animated model"); + return false; + } + // Nous ne pouvons plus avoir recours au smart pointeur à partir d'ici si nous voulons être exception-safe NzMesh* meshPtr = mesh.get(); @@ -38,20 +44,6 @@ namespace model->SetMesh(meshPtr); mesh.release(); - if (parameters.loadAnimation && meshPtr->IsAnimable()) - { - NzString animationPath = meshPtr->GetAnimation(); - if (!animationPath.IsEmpty()) - { - std::unique_ptr animation(new NzAnimation); - animation->SetPersistent(false); - if (animation->LoadFromFile(animationPath, parameters.animation) && model->SetAnimation(animation.get())) - animation.release(); - else - NazaraWarning("Failed to load animation"); - } - } - if (parameters.loadMaterials) { unsigned int matCount = model->GetMaterialCount(); @@ -81,10 +73,10 @@ namespace void NzLoaders_Mesh_Register() { - NzModelLoader::RegisterLoader(NzMeshLoader::IsExtensionSupported, Check, Load); + NzModelLoader::RegisterLoader(NzMeshLoader::IsExtensionSupported, CheckStatic, LoadStatic); } void NzLoaders_Mesh_Unregister() { - NzModelLoader::UnregisterLoader(NzMeshLoader::IsExtensionSupported, Check, Load); + NzModelLoader::UnregisterLoader(NzMeshLoader::IsExtensionSupported, CheckStatic, LoadStatic); } diff --git a/src/Nazara/Graphics/Model.cpp b/src/Nazara/Graphics/Model.cpp index 6f80efa13..6a6da27c7 100644 --- a/src/Nazara/Graphics/Model.cpp +++ b/src/Nazara/Graphics/Model.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include @@ -18,9 +18,6 @@ NzModelParameters::NzModelParameters() bool NzModelParameters::IsValid() const { - if (loadAnimation && !animation.IsValid()) - return false; - if (loadMaterials && !material.IsValid()) return false; @@ -28,8 +25,6 @@ bool NzModelParameters::IsValid() const } NzModel::NzModel() : -m_currentSequence(nullptr), -m_animationEnabled(true), m_boundingVolumeUpdated(true), m_matCount(0), m_skin(0), @@ -41,25 +36,16 @@ NzModel::NzModel(const NzModel& model) : NzSceneNode(model), m_materials(model.m_materials), m_boundingVolume(model.m_boundingVolume), -m_currentSequence(model.m_currentSequence), -m_animationEnabled(model.m_animationEnabled), m_boundingVolumeUpdated(model.m_boundingVolumeUpdated), -m_interpolation(model.m_interpolation), -m_currentFrame(model.m_currentFrame), m_matCount(model.m_matCount), -m_nextFrame(model.m_nextFrame), m_skin(model.m_skin), m_skinCount(model.m_skinCount) { if (model.m_mesh) { - // Nous n'avons une animation et des matériaux que si nous avons un mesh - m_animation = model.m_animation; + // Nous n'avons des matériaux que si nous avons un mesh m_mesh = model.m_mesh; m_materials = model.m_materials; - - if (m_mesh->GetAnimationType() == nzAnimationType_Skeletal) - m_skeleton = model.m_skeleton; } SetParent(model); @@ -77,64 +63,18 @@ void NzModel::AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const unsigned int submeshCount = m_mesh->GetSubMeshCount(); for (unsigned int i = 0; i < submeshCount; ++i) { - NzSubMesh* subMesh = m_mesh->GetSubMesh(i); - NzMaterial* material = m_materials[subMesh->GetMaterialIndex()]; + const NzStaticMesh* mesh = static_cast(m_mesh->GetSubMesh(i)); + NzMaterial* material = m_materials[mesh->GetMaterialIndex()]; - renderQueue->AddSubMesh(material, subMesh, transformMatrix); + NzMeshData meshData; + meshData.indexBuffer = mesh->GetIndexBuffer(); + meshData.primitiveMode = mesh->GetPrimitiveMode(); + meshData.vertexBuffer = mesh->GetVertexBuffer(); + + renderQueue->AddMesh(material, meshData, mesh->GetAABB(), transformMatrix); } } -void NzModel::AdvanceAnimation(float elapsedTime) -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_animation) - { - NazaraError("Model has no animation"); - return; - } - #endif - - m_interpolation += m_currentSequence->frameRate * elapsedTime; - while (m_interpolation > 1.f) - { - m_interpolation -= 1.f; - - unsigned lastFrame = m_currentSequence->firstFrame + m_currentSequence->frameCount - 1; - if (m_nextFrame+1 > lastFrame) - { - if (m_animation->IsLoopPointInterpolationEnabled()) - { - m_currentFrame = m_nextFrame; - m_nextFrame = m_currentSequence->firstFrame; - } - else - { - m_currentFrame = m_currentSequence->firstFrame; - m_nextFrame = m_currentFrame+1; - } - } - else - { - m_currentFrame = m_nextFrame; - m_nextFrame++; - } - } - - m_animation->AnimateSkeleton(&m_skeleton, m_currentFrame, m_nextFrame, m_interpolation); - m_boundingVolume.MakeNull(); - m_boundingVolumeUpdated = false; -} - -void NzModel::EnableAnimation(bool animation) -{ - m_animationEnabled = animation; -} - -NzAnimation* NzModel::GetAnimation() const -{ - return m_animation; -} - const NzBoundingVolumef& NzModel::GetBoundingVolume() const { #if NAZARA_GRAPHICS_SAFE @@ -254,16 +194,6 @@ nzSceneNodeType NzModel::GetSceneNodeType() const return nzSceneNodeType_Model; } -NzSkeleton* NzModel::GetSkeleton() -{ - return &m_skeleton; -} - -const NzSkeleton* NzModel::GetSkeleton() const -{ - return &m_skeleton; -} - unsigned int NzModel::GetSkin() const { return m_skin; @@ -274,14 +204,9 @@ unsigned int NzModel::GetSkinCount() const return m_skinCount; } -bool NzModel::HasAnimation() const +bool NzModel::IsAnimated() const { - return m_animation != nullptr; -} - -bool NzModel::IsAnimationEnabled() const -{ - return m_animationEnabled; + return false; } bool NzModel::IsDrawable() const @@ -312,70 +237,16 @@ bool NzModel::LoadFromStream(NzInputStream& stream, const NzModelParameters& par void NzModel::Reset() { - if (m_scene) - m_scene->UnregisterForUpdate(this); - m_matCount = 0; m_skinCount = 0; if (m_mesh) { - m_animation.Reset(); m_mesh.Reset(); m_materials.clear(); - - m_skeleton.Destroy(); } } -bool NzModel::SetAnimation(NzAnimation* animation) -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_mesh) - { - NazaraError("Model has no animation"); - return false; - } - - if (animation) - { - if (!animation->IsValid()) - { - NazaraError("Invalid animation"); - return false; - } - - if (animation->GetType() != m_mesh->GetAnimationType()) - { - NazaraError("Animation type must match mesh animation type"); - return false; - } - - if (animation->GetType() == nzAnimationType_Skeletal && animation->GetJointCount() != m_mesh->GetJointCount()) - { - NazaraError("Animation joint count must match mesh joint count"); - return false; - } - } - #endif - - m_animation = animation; - if (m_animation) - { - m_currentFrame = 0; - m_interpolation = 0.f; - - SetSequence(0); - - if (m_scene) - m_scene->RegisterForUpdate(this); - } - else if (m_scene) - m_scene->UnregisterForUpdate(this); - - return true; -} - bool NzModel::SetMaterial(const NzString& subMeshName, NzMaterial* material) { NzSubMesh* subMesh = m_mesh->GetSubMesh(subMeshName); @@ -480,25 +351,18 @@ void NzModel::SetMaterial(unsigned int skinIndex, unsigned int matIndex, NzMater void NzModel::SetMesh(NzMesh* mesh) { + #if NAZARA_GRAPHICS_SAFE + if (mesh && !mesh->IsValid()) + { + NazaraError("Invalid mesh"); + return; + } + #endif + m_mesh = mesh; if (m_mesh) { - m_boundingVolume.MakeNull(); - m_boundingVolumeUpdated = false; - - if (m_mesh->GetAnimationType() == nzAnimationType_Skeletal) - m_skeleton = *mesh->GetSkeleton(); // Copie du squelette template - - if (m_animation) - { - if (m_animation->GetJointCount() != m_mesh->GetJointCount()) - { - NazaraWarning("Animation joint count is not matching new mesh joint count"); - SetAnimation(nullptr); - } - } - m_matCount = mesh->GetMaterialCount(); m_materials.clear(); m_materials.resize(m_matCount, NzMaterial::GetDefault()); @@ -506,61 +370,12 @@ void NzModel::SetMesh(NzMesh* mesh) } else { - m_boundingVolume.MakeNull(); - m_boundingVolumeUpdated = true; m_matCount = 0; m_materials.clear(); m_skinCount = 0; - - SetAnimation(nullptr); - } -} - -bool NzModel::SetSequence(const NzString& sequenceName) -{ - ///TODO: Rendre cette erreur "safe" avec le nouveau système de gestions d'erreur (No-log) - #if NAZARA_GRAPHICS_SAFE - if (!m_animation) - { - NazaraError("Model has no animation"); - return false; - } - #endif - - const NzSequence* currentSequence = m_animation->GetSequence(sequenceName); - if (!currentSequence) - { - NazaraError("Sequence not found"); - return false; } - m_currentSequence = currentSequence; - m_nextFrame = m_currentSequence->firstFrame; - - return true; -} - -void NzModel::SetSequence(unsigned int sequenceIndex) -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_animation) - { - NazaraError("Model has no animation"); - return; - } - #endif - - const NzSequence* currentSequence = m_animation->GetSequence(sequenceIndex); - #if NAZARA_GRAPHICS_SAFE - if (!currentSequence) - { - NazaraError("Sequence not found"); - return; - } - #endif - - m_currentSequence = currentSequence; - m_nextFrame = m_currentSequence->firstFrame; + InvalidateBoundingVolume(); } void NzModel::SetSkin(unsigned int skin) @@ -594,23 +409,14 @@ NzModel& NzModel::operator=(const NzModel& node) { NzSceneNode::operator=(node); - m_animation = node.m_animation; - m_animationEnabled = node.m_animationEnabled; m_boundingVolume = node.m_boundingVolume; m_boundingVolumeUpdated = node.m_boundingVolumeUpdated; - m_currentFrame = node.m_currentFrame; - m_currentSequence = node.m_currentSequence; - m_interpolation = node.m_interpolation; m_matCount = node.m_matCount; m_materials = node.m_materials; m_mesh = node.m_mesh; - m_nextFrame = node.m_nextFrame; m_skin = node.m_skin; m_skinCount = node.m_skinCount; - if (m_mesh->GetAnimationType() == nzAnimationType_Skeletal) - m_skeleton = node.m_skeleton; - return *this; } @@ -619,22 +425,13 @@ NzModel& NzModel::operator=(NzModel&& node) NzSceneNode::operator=(node); // Ressources - m_animation = std::move(node.m_animation); m_mesh = std::move(node.m_mesh); m_materials = std::move(node.m_materials); - if (m_mesh->GetAnimationType() == nzAnimationType_Skeletal) - m_skeleton = std::move(node.m_skeleton); - // Paramètres - m_animationEnabled = node.m_animationEnabled; m_boundingVolume = node.m_boundingVolume; m_boundingVolumeUpdated = node.m_boundingVolumeUpdated; - m_currentFrame = node.m_currentFrame; - m_currentSequence = node.m_currentSequence; - m_interpolation = node.m_interpolation; m_matCount = node.m_matCount; - m_nextFrame = node.m_nextFrame; m_skin = node.m_skin; m_skinCount = node.m_skinCount; @@ -656,32 +453,10 @@ void NzModel::InvalidateNode() m_boundingVolumeUpdated = false; } -void NzModel::Register() -{ - if (m_animation) - m_scene->RegisterForUpdate(this); -} - -void NzModel::Unregister() -{ - m_scene->UnregisterForUpdate(this); -} - -void NzModel::Update() -{ - if (m_animationEnabled && m_animation) - AdvanceAnimation(m_scene->GetUpdateTime()); -} - void NzModel::UpdateBoundingVolume() const { if (m_boundingVolume.IsNull()) - { - if (m_mesh->GetAnimationType() == nzAnimationType_Skeletal) - m_boundingVolume.Set(m_skeleton.GetAABB()); - else - m_boundingVolume.Set(m_mesh->GetAABB()); - } + m_boundingVolume.Set(m_mesh->GetAABB()); if (!m_transformMatrixUpdated) UpdateTransformMatrix();