Added new abstract renderqueue rendering
Former-commit-id: 35e66174ccc3de3b43571f12a149e6d3c59cc2c9
This commit is contained in:
parent
d09f6def2b
commit
c901b5808e
|
|
@ -9,14 +9,15 @@
|
||||||
|
|
||||||
#include <Nazara/Prerequesites.hpp>
|
#include <Nazara/Prerequesites.hpp>
|
||||||
#include <Nazara/Core/NonCopyable.hpp>
|
#include <Nazara/Core/NonCopyable.hpp>
|
||||||
|
#include <Nazara/Math/Box.hpp>
|
||||||
#include <Nazara/Math/Matrix4.hpp>
|
#include <Nazara/Math/Matrix4.hpp>
|
||||||
|
#include <Nazara/Utility/Enums.hpp>
|
||||||
|
|
||||||
class NzDrawable;
|
class NzDrawable;
|
||||||
class NzLight;
|
class NzLight;
|
||||||
class NzMaterial;
|
class NzMaterial;
|
||||||
class NzModel;
|
|
||||||
class NzSprite;
|
class NzSprite;
|
||||||
class NzSubMesh;
|
struct NzMeshData;
|
||||||
|
|
||||||
class NAZARA_API NzAbstractRenderQueue : NzNonCopyable
|
class NAZARA_API NzAbstractRenderQueue : NzNonCopyable
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +27,8 @@ class NAZARA_API NzAbstractRenderQueue : NzNonCopyable
|
||||||
|
|
||||||
virtual void AddDrawable(const NzDrawable* drawable) = 0;
|
virtual void AddDrawable(const NzDrawable* drawable) = 0;
|
||||||
virtual void AddLight(const NzLight* light) = 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 AddSprite(const NzSprite* sprite) = 0;
|
||||||
virtual void AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix) = 0;
|
|
||||||
|
|
||||||
virtual void Clear(bool fully) = 0;
|
virtual void Clear(bool fully) = 0;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
#include <Nazara/Graphics/AbstractRenderQueue.hpp>
|
#include <Nazara/Graphics/AbstractRenderQueue.hpp>
|
||||||
#include <Nazara/Math/Box.hpp>
|
#include <Nazara/Math/Box.hpp>
|
||||||
#include <Nazara/Math/Matrix4.hpp>
|
#include <Nazara/Math/Matrix4.hpp>
|
||||||
|
#include <Nazara/Utility/MeshData.hpp>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
|
|
@ -29,22 +30,11 @@ class NAZARA_API NzDeferredRenderQueue : public NzAbstractRenderQueue, NzResourc
|
||||||
|
|
||||||
void AddDrawable(const NzDrawable* drawable) override;
|
void AddDrawable(const NzDrawable* drawable) override;
|
||||||
void AddLight(const NzLight* light) 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 AddSprite(const NzSprite* sprite) override;
|
||||||
void AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix) override;
|
|
||||||
|
|
||||||
void Clear(bool fully);
|
void Clear(bool fully);
|
||||||
|
|
||||||
struct SkeletalData
|
|
||||||
{
|
|
||||||
///TODO
|
|
||||||
NzMatrix4f transformMatrix;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct StaticData
|
|
||||||
{
|
|
||||||
NzMatrix4f transformMatrix;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct BatchedModelMaterialComparator
|
struct BatchedModelMaterialComparator
|
||||||
{
|
{
|
||||||
bool operator()(const NzMaterial* mat1, const NzMaterial* mat2);
|
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);
|
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
|
typedef std::map<NzMeshData, std::vector<NzMatrix4f>, MeshDataComparator> MeshInstanceContainer;
|
||||||
{
|
typedef std::map<const NzMaterial*, std::tuple<bool, bool, MeshInstanceContainer>, BatchedModelMaterialComparator> ModelBatches;
|
||||||
bool operator()(const NzStaticMesh* subMesh1, const NzStaticMesh* subMesh2);
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef std::map<const NzSkeletalMesh*, std::vector<SkeletalData>, BatchedSkeletalMeshComparator> BatchedSkeletalMeshContainer;
|
|
||||||
typedef std::map<const NzStaticMesh*, std::vector<StaticData>, BatchedStaticMeshComparator> BatchedStaticMeshContainer;
|
|
||||||
typedef std::map<const NzMaterial*, std::tuple<bool, bool, BatchedSkeletalMeshContainer, BatchedStaticMeshContainer>, BatchedModelMaterialComparator> BatchedModelContainer;
|
|
||||||
typedef std::map<const NzMaterial*, std::vector<const NzSprite*>> BatchedSpriteContainer;
|
typedef std::map<const NzMaterial*, std::vector<const NzSprite*>> BatchedSpriteContainer;
|
||||||
typedef std::vector<const NzLight*> LightContainer;
|
typedef std::vector<const NzLight*> LightContainer;
|
||||||
|
|
||||||
BatchedModelContainer opaqueModels;
|
ModelBatches opaqueModels;
|
||||||
BatchedSpriteContainer sprites;
|
BatchedSpriteContainer sprites;
|
||||||
LightContainer directionalLights;
|
LightContainer directionalLights;
|
||||||
LightContainer pointLights;
|
LightContainer pointLights;
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
#include <Nazara/Graphics/AbstractRenderQueue.hpp>
|
#include <Nazara/Graphics/AbstractRenderQueue.hpp>
|
||||||
#include <Nazara/Math/Box.hpp>
|
#include <Nazara/Math/Box.hpp>
|
||||||
#include <Nazara/Math/Matrix4.hpp>
|
#include <Nazara/Math/Matrix4.hpp>
|
||||||
|
#include <Nazara/Utility/MeshData.hpp>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
|
|
@ -31,8 +32,8 @@ class NAZARA_API NzForwardRenderQueue : public NzAbstractRenderQueue, NzResource
|
||||||
|
|
||||||
void AddDrawable(const NzDrawable* drawable) override;
|
void AddDrawable(const NzDrawable* drawable) override;
|
||||||
void AddLight(const NzLight* light) 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 AddSprite(const NzSprite* sprite) override;
|
||||||
void AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix) override;
|
|
||||||
|
|
||||||
void Clear(bool fully);
|
void Clear(bool fully);
|
||||||
|
|
||||||
|
|
@ -42,35 +43,14 @@ class NAZARA_API NzForwardRenderQueue : public NzAbstractRenderQueue, NzResource
|
||||||
bool OnResourceDestroy(const NzResource* resource, int index) override;
|
bool OnResourceDestroy(const NzResource* resource, int index) override;
|
||||||
void OnResourceReleased(const NzResource* resource, int index) override;
|
void OnResourceReleased(const NzResource* resource, int index) override;
|
||||||
|
|
||||||
struct SkeletalData
|
struct TransparentModelData
|
||||||
{
|
|
||||||
///TODO
|
|
||||||
NzMatrix4f transformMatrix;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct StaticData
|
|
||||||
{
|
|
||||||
NzMatrix4f transformMatrix;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TransparentModel
|
|
||||||
{
|
{
|
||||||
NzMatrix4f transformMatrix;
|
NzMatrix4f transformMatrix;
|
||||||
|
NzMeshData meshData;
|
||||||
NzSpheref boundingSphere;
|
NzSpheref boundingSphere;
|
||||||
const NzMaterial* material;
|
const NzMaterial* material;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TransparentSkeletalModel : public TransparentModel
|
|
||||||
{
|
|
||||||
///TODO
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TransparentStaticModel : public TransparentModel
|
|
||||||
{
|
|
||||||
const NzStaticMesh* mesh;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
struct BatchedModelMaterialComparator
|
struct BatchedModelMaterialComparator
|
||||||
{
|
{
|
||||||
bool operator()(const NzMaterial* mat1, const NzMaterial* mat2);
|
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);
|
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
|
typedef std::map<NzMeshData, std::pair<NzSpheref, std::vector<NzMatrix4f>>, MeshDataComparator> MeshInstanceContainer;
|
||||||
{
|
typedef std::map<const NzMaterial*, std::tuple<bool, bool, MeshInstanceContainer>, BatchedModelMaterialComparator> ModelBatches;
|
||||||
bool operator()(const NzStaticMesh* subMesh1, const NzStaticMesh* subMesh2);
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef std::map<const NzSkeletalMesh*, std::vector<SkeletalData>, BatchedSkeletalMeshComparator> BatchedSkeletalMeshContainer;
|
|
||||||
typedef std::map<const NzStaticMesh*, std::pair<NzSpheref, std::vector<StaticData>>, BatchedStaticMeshComparator> BatchedStaticMeshContainer;
|
|
||||||
typedef std::map<const NzMaterial*, std::tuple<bool, bool, BatchedSkeletalMeshContainer, BatchedStaticMeshContainer>, BatchedModelMaterialComparator> BatchedModelContainer;
|
|
||||||
typedef std::map<const NzMaterial*, std::vector<const NzSprite*>> BatchedSpriteContainer;
|
typedef std::map<const NzMaterial*, std::vector<const NzSprite*>> BatchedSpriteContainer;
|
||||||
typedef std::vector<const NzLight*> LightContainer;
|
typedef std::vector<const NzLight*> LightContainer;
|
||||||
typedef std::vector<std::pair<unsigned int, bool>> TransparentModelContainer;
|
typedef std::vector<unsigned int> TransparentModelContainer;
|
||||||
|
|
||||||
BatchedModelContainer opaqueModels;
|
ModelBatches opaqueModels;
|
||||||
BatchedSpriteContainer sprites;
|
BatchedSpriteContainer sprites;
|
||||||
TransparentModelContainer transparentsModels;
|
TransparentModelContainer transparentModels;
|
||||||
std::vector<TransparentSkeletalModel> transparentSkeletalModels;
|
std::vector<TransparentModelData> transparentModelData;
|
||||||
std::vector<TransparentStaticModel> transparentStaticModels;
|
|
||||||
std::vector<const NzDrawable*> otherDrawables;
|
std::vector<const NzDrawable*> otherDrawables;
|
||||||
LightContainer directionalLights;
|
LightContainer directionalLights;
|
||||||
LightContainer lights;
|
LightContainer lights;
|
||||||
|
|
|
||||||
|
|
@ -58,10 +58,9 @@ bool NzDeferredGeometryPass::Process(const NzScene* scene, unsigned int firstWor
|
||||||
if (used)
|
if (used)
|
||||||
{
|
{
|
||||||
bool& renderQueueInstancing = std::get<1>(matIt.second);
|
bool& renderQueueInstancing = std::get<1>(matIt.second);
|
||||||
NzDeferredRenderQueue::BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second);
|
NzDeferredRenderQueue::MeshInstanceContainer& meshInstances = std::get<2>(matIt.second);
|
||||||
NzDeferredRenderQueue::BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second);
|
|
||||||
|
|
||||||
if (!skeletalContainer.empty() || !staticContainer.empty())
|
if (!meshInstances.empty())
|
||||||
{
|
{
|
||||||
const NzMaterial* material = matIt.first;
|
const NzMaterial* material = matIt.first;
|
||||||
|
|
||||||
|
|
@ -88,26 +87,16 @@ bool NzDeferredGeometryPass::Process(const NzScene* scene, unsigned int firstWor
|
||||||
lastShader = shader;
|
lastShader = shader;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Meshs squelettiques
|
// Meshes
|
||||||
/*if (!skeletalContainer.empty())
|
for (auto& meshIt : meshInstances)
|
||||||
{
|
{
|
||||||
NzRenderer::SetVertexBuffer(m_skinningBuffer); // Vertex buffer commun
|
const NzMeshData& meshData = meshIt.first;
|
||||||
for (auto& subMeshIt : container)
|
std::vector<NzMatrix4f>& instances = meshIt.second;
|
||||||
{
|
|
||||||
///TODO
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// Meshs statiques
|
if (!instances.empty())
|
||||||
for (auto& subMeshIt : staticContainer)
|
|
||||||
{
|
{
|
||||||
const NzStaticMesh* mesh = subMeshIt.first;
|
const NzIndexBuffer* indexBuffer = meshData.indexBuffer;
|
||||||
std::vector<NzDeferredRenderQueue::StaticData>& staticData = subMeshIt.second;
|
const NzVertexBuffer* vertexBuffer = meshData.vertexBuffer;
|
||||||
|
|
||||||
if (!staticData.empty())
|
|
||||||
{
|
|
||||||
const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer();
|
|
||||||
const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer();
|
|
||||||
|
|
||||||
// Gestion du draw call avant la boucle de rendu
|
// Gestion du draw call avant la boucle de rendu
|
||||||
std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc;
|
std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc;
|
||||||
|
|
@ -130,49 +119,43 @@ bool NzDeferredGeometryPass::Process(const NzScene* scene, unsigned int firstWor
|
||||||
NzRenderer::SetIndexBuffer(indexBuffer);
|
NzRenderer::SetIndexBuffer(indexBuffer);
|
||||||
NzRenderer::SetVertexBuffer(vertexBuffer);
|
NzRenderer::SetVertexBuffer(vertexBuffer);
|
||||||
|
|
||||||
nzPrimitiveMode primitiveMode = mesh->GetPrimitiveMode();
|
|
||||||
if (useInstancing)
|
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();
|
NzVertexBuffer* instanceBuffer = NzRenderer::GetInstanceBuffer();
|
||||||
|
|
||||||
instanceBuffer->SetVertexDeclaration(NzVertexDeclaration::Get(nzVertexLayout_Matrix4));
|
instanceBuffer->SetVertexDeclaration(NzVertexDeclaration::Get(nzVertexLayout_Matrix4));
|
||||||
|
|
||||||
unsigned int stride = instanceBuffer->GetStride();
|
const NzMatrix4f* instanceMatrices = &instances[0];
|
||||||
|
unsigned int instanceCount = instances.size();
|
||||||
const NzDeferredRenderQueue::StaticData* data = &staticData[0];
|
unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // Le nombre de matrices que peut contenir le buffer
|
||||||
unsigned int instanceCount = staticData.size();
|
|
||||||
unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // Le nombre de sommets maximum avec la déclaration donnée plus hautg
|
|
||||||
|
|
||||||
while (instanceCount > 0)
|
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);
|
unsigned int renderedInstanceCount = std::min(instanceCount, maxInstanceCount);
|
||||||
instanceCount -= renderedInstanceCount;
|
instanceCount -= renderedInstanceCount;
|
||||||
|
|
||||||
NzBufferMapper<NzVertexBuffer> mapper(instanceBuffer, nzBufferAccess_DiscardAndWrite, 0, renderedInstanceCount);
|
// On remplit l'instancing buffer avec nos matrices world
|
||||||
nzUInt8* ptr = reinterpret_cast<nzUInt8*>(mapper.GetPointer());
|
instanceBuffer->Fill(instanceMatrices, 0, renderedInstanceCount, true);
|
||||||
|
instanceMatrices += renderedInstanceCount;
|
||||||
|
|
||||||
for (unsigned int i = 0; i < renderedInstanceCount; ++i)
|
// Et on affiche
|
||||||
{
|
InstancedDrawFunc(renderedInstanceCount, meshData.primitiveMode, 0, indexCount);
|
||||||
std::memcpy(ptr, data->transformMatrix, sizeof(float)*16);
|
|
||||||
|
|
||||||
data++;
|
|
||||||
ptr += stride;
|
|
||||||
}
|
|
||||||
|
|
||||||
mapper.Unmap();
|
|
||||||
|
|
||||||
InstancedDrawFunc(renderedInstanceCount, primitiveMode, 0, indexCount);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
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);
|
NzRenderer::SetMatrix(nzMatrixType_World, matrix);
|
||||||
DrawFunc(primitiveMode, 0, indexCount);
|
DrawFunc(meshData.primitiveMode, 0, indexCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
staticData.clear();
|
|
||||||
|
instances.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,17 +9,15 @@
|
||||||
#include <Nazara/Graphics/Material.hpp>
|
#include <Nazara/Graphics/Material.hpp>
|
||||||
#include <Nazara/Graphics/Model.hpp>
|
#include <Nazara/Graphics/Model.hpp>
|
||||||
#include <Nazara/Graphics/Sprite.hpp>
|
#include <Nazara/Graphics/Sprite.hpp>
|
||||||
#include <Nazara/Utility/SkeletalMesh.hpp>
|
|
||||||
#include <Nazara/Utility/StaticMesh.hpp>
|
|
||||||
#include <Nazara/Graphics/Debug.hpp>
|
#include <Nazara/Graphics/Debug.hpp>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
enum ResourceType
|
enum ResourceType
|
||||||
{
|
{
|
||||||
|
ResourceType_IndexBuffer,
|
||||||
ResourceType_Material,
|
ResourceType_Material,
|
||||||
ResourceType_SkeletalMesh,
|
ResourceType_VertexBuffer
|
||||||
ResourceType_StaticMesh
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,6 +64,45 @@ void NzDeferredRenderQueue::AddLight(const NzLight* light)
|
||||||
m_forwardQueue->AddLight(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<NzMatrix4f>& 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)
|
void NzDeferredRenderQueue::AddSprite(const NzSprite* sprite)
|
||||||
{
|
{
|
||||||
#if NAZARA_GRAPHICS_SAFE
|
#if NAZARA_GRAPHICS_SAFE
|
||||||
|
|
@ -91,75 +128,6 @@ void NzDeferredRenderQueue::AddSprite(const NzSprite* sprite)
|
||||||
m_forwardQueue->AddSprite(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<const NzStaticMesh*>(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<StaticData>& 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)
|
void NzDeferredRenderQueue::Clear(bool fully)
|
||||||
{
|
{
|
||||||
directionalLights.clear();
|
directionalLights.clear();
|
||||||
|
|
@ -173,18 +141,15 @@ void NzDeferredRenderQueue::Clear(bool fully)
|
||||||
const NzMaterial* material = matIt.first;
|
const NzMaterial* material = matIt.first;
|
||||||
material->RemoveResourceListener(this);
|
material->RemoveResourceListener(this);
|
||||||
|
|
||||||
BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second);
|
MeshInstanceContainer& instances = std::get<2>(matIt.second);
|
||||||
for (auto& meshIt : skeletalContainer)
|
for (auto& instanceIt : instances)
|
||||||
{
|
{
|
||||||
const NzSkeletalMesh* skeletalMesh = meshIt.first;
|
const NzMeshData& renderData = instanceIt.first;
|
||||||
skeletalMesh->RemoveResourceListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second);
|
if (renderData.indexBuffer)
|
||||||
for (auto& meshIt : staticContainer)
|
renderData.indexBuffer->RemoveResourceListener(this);
|
||||||
{
|
|
||||||
const NzStaticMesh* staticMesh = meshIt.first;
|
renderData.vertexBuffer->RemoveResourceListener(this);
|
||||||
staticMesh->RemoveResourceListener(this);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -199,23 +164,41 @@ bool NzDeferredRenderQueue::OnResourceDestroy(const NzResource* resource, int in
|
||||||
{
|
{
|
||||||
switch (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:
|
case ResourceType_Material:
|
||||||
opaqueModels.erase(static_cast<const NzMaterial*>(resource));
|
opaqueModels.erase(static_cast<const NzMaterial*>(resource));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ResourceType_SkeletalMesh:
|
case ResourceType_VertexBuffer:
|
||||||
{
|
{
|
||||||
for (auto& pair : opaqueModels)
|
for (auto& modelPair : opaqueModels)
|
||||||
std::get<2>(pair.second).erase(static_cast<const NzSkeletalMesh*>(resource));
|
{
|
||||||
|
MeshInstanceContainer& meshes = std::get<2>(modelPair.second);
|
||||||
break;
|
for (auto it = meshes.begin(); it != meshes.end();)
|
||||||
|
{
|
||||||
|
const NzMeshData& renderData = it->first;
|
||||||
|
if (renderData.vertexBuffer == resource)
|
||||||
|
it = meshes.erase(it);
|
||||||
|
else
|
||||||
|
++it;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case ResourceType_StaticMesh:
|
|
||||||
{
|
|
||||||
for (auto& pair : opaqueModels)
|
|
||||||
std::get<3>(pair.second).erase(static_cast<const NzStaticMesh*>(resource));
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -225,7 +208,58 @@ bool NzDeferredRenderQueue::OnResourceDestroy(const NzResource* resource, int in
|
||||||
|
|
||||||
void NzDeferredRenderQueue::OnResourceReleased(const NzResource* resource, int index)
|
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)
|
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* shader1 = mat1->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
|
||||||
const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
|
const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
|
||||||
|
|
||||||
if (shader1 != shader2)
|
if (shader1 != shader2)
|
||||||
return 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* shader1 = mat1->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
|
||||||
const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
|
const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
|
||||||
|
|
||||||
if (shader1 != shader2)
|
if (shader1 != shader2)
|
||||||
return shader1 < shader2;
|
return shader1 < shader2;
|
||||||
|
|
||||||
|
|
@ -270,38 +302,20 @@ bool NzDeferredRenderQueue::BatchedSpriteMaterialComparator::operator()(const Nz
|
||||||
return mat1 < mat2;
|
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;
|
||||||
const NzBuffer* buffer1 = (iBuffer1) ? iBuffer1->GetBuffer() : nullptr;
|
const NzBuffer* buffer2;
|
||||||
|
|
||||||
const NzIndexBuffer* iBuffer2 = subMesh1->GetIndexBuffer();
|
buffer1 = (data1.indexBuffer) ? data1.indexBuffer->GetBuffer() : nullptr;
|
||||||
const NzBuffer* buffer2 = (iBuffer2) ? iBuffer2->GetBuffer() : nullptr;
|
buffer2 = (data2.indexBuffer) ? data2.indexBuffer->GetBuffer() : nullptr;
|
||||||
|
if (buffer1 != buffer2)
|
||||||
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;
|
return buffer1 < buffer2;
|
||||||
}
|
|
||||||
else
|
buffer1 = data1.vertexBuffer->GetBuffer();
|
||||||
|
buffer2 = data2.vertexBuffer->GetBuffer();
|
||||||
|
if (buffer1 != buffer2)
|
||||||
return buffer1 < buffer2;
|
return buffer1 < buffer2;
|
||||||
|
|
||||||
|
return data1.primitiveMode < data2.primitiveMode;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,9 @@ namespace
|
||||||
{
|
{
|
||||||
enum ResourceType
|
enum ResourceType
|
||||||
{
|
{
|
||||||
|
ResourceType_IndexBuffer,
|
||||||
ResourceType_Material,
|
ResourceType_Material,
|
||||||
ResourceType_SkeletalMesh,
|
ResourceType_VertexBuffer
|
||||||
ResourceType_StaticMesh
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -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<NzMatrix4f>& 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)
|
void NzForwardRenderQueue::AddSprite(const NzSprite* sprite)
|
||||||
{
|
{
|
||||||
#if NAZARA_GRAPHICS_SAFE
|
#if NAZARA_GRAPHICS_SAFE
|
||||||
|
|
@ -87,100 +140,13 @@ void NzForwardRenderQueue::AddSprite(const NzSprite* sprite)
|
||||||
sprites[sprite->GetMaterial()].push_back(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<const NzStaticMesh*>(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<StaticData>& 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)
|
void NzForwardRenderQueue::Clear(bool fully)
|
||||||
{
|
{
|
||||||
directionalLights.clear();
|
directionalLights.clear();
|
||||||
lights.clear();
|
lights.clear();
|
||||||
otherDrawables.clear();
|
otherDrawables.clear();
|
||||||
transparentsModels.clear();
|
transparentModels.clear();
|
||||||
transparentSkeletalModels.clear();
|
transparentModelData.clear();
|
||||||
transparentStaticModels.clear();
|
|
||||||
|
|
||||||
if (fully)
|
if (fully)
|
||||||
{
|
{
|
||||||
|
|
@ -189,18 +155,15 @@ void NzForwardRenderQueue::Clear(bool fully)
|
||||||
const NzMaterial* material = matIt.first;
|
const NzMaterial* material = matIt.first;
|
||||||
material->RemoveResourceListener(this);
|
material->RemoveResourceListener(this);
|
||||||
|
|
||||||
BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second);
|
MeshInstanceContainer& instances = std::get<2>(matIt.second);
|
||||||
for (auto& meshIt : skeletalContainer)
|
for (auto& instanceIt : instances)
|
||||||
{
|
{
|
||||||
const NzSkeletalMesh* skeletalMesh = meshIt.first;
|
const NzMeshData& renderData = instanceIt.first;
|
||||||
skeletalMesh->RemoveResourceListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second);
|
if (renderData.indexBuffer)
|
||||||
for (auto& meshIt : staticContainer)
|
renderData.indexBuffer->RemoveResourceListener(this);
|
||||||
{
|
|
||||||
const NzStaticMesh* staticMesh = meshIt.first;
|
renderData.vertexBuffer->RemoveResourceListener(this);
|
||||||
staticMesh->RemoveResourceListener(this);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
opaqueModels.clear();
|
opaqueModels.clear();
|
||||||
|
|
@ -212,15 +175,10 @@ void NzForwardRenderQueue::Sort(const NzAbstractViewer* viewer)
|
||||||
{
|
{
|
||||||
struct TransparentModelComparator
|
struct TransparentModelComparator
|
||||||
{
|
{
|
||||||
bool operator()(const std::pair<unsigned int, bool>& index1, const std::pair<unsigned int, bool>& index2)
|
bool operator()(unsigned int index1, unsigned int index2)
|
||||||
{
|
{
|
||||||
const NzSpheref& sphere1 = (index1.second) ?
|
const NzSpheref& sphere1 = queue->transparentModelData[index1].boundingSphere;
|
||||||
queue->transparentStaticModels[index1.first].boundingSphere :
|
const NzSpheref& sphere2 = queue->transparentModelData[index2].boundingSphere;
|
||||||
queue->transparentSkeletalModels[index1.first].boundingSphere;
|
|
||||||
|
|
||||||
const NzSpheref& sphere2 = (index2.second) ?
|
|
||||||
queue->transparentStaticModels[index2.first].boundingSphere :
|
|
||||||
queue->transparentSkeletalModels[index2.first].boundingSphere;
|
|
||||||
|
|
||||||
NzVector3f position1 = sphere1.GetNegativeVertex(viewerNormal);
|
NzVector3f position1 = sphere1.GetNegativeVertex(viewerNormal);
|
||||||
NzVector3f position2 = sphere2.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()};
|
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)
|
bool NzForwardRenderQueue::OnResourceDestroy(const NzResource* resource, int index)
|
||||||
{
|
{
|
||||||
switch (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:
|
case ResourceType_Material:
|
||||||
opaqueModels.erase(static_cast<const NzMaterial*>(resource));
|
opaqueModels.erase(static_cast<const NzMaterial*>(resource));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ResourceType_SkeletalMesh:
|
case ResourceType_VertexBuffer:
|
||||||
{
|
{
|
||||||
for (auto& pair : opaqueModels)
|
for (auto& modelPair : opaqueModels)
|
||||||
std::get<2>(pair.second).erase(static_cast<const NzSkeletalMesh*>(resource));
|
{
|
||||||
|
MeshInstanceContainer& meshes = std::get<2>(modelPair.second);
|
||||||
break;
|
for (auto it = meshes.begin(); it != meshes.end();)
|
||||||
|
{
|
||||||
|
const NzMeshData& renderData = it->first;
|
||||||
|
if (renderData.vertexBuffer == resource)
|
||||||
|
it = meshes.erase(it);
|
||||||
|
else
|
||||||
|
++it;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case ResourceType_StaticMesh:
|
|
||||||
{
|
|
||||||
for (auto& pair : opaqueModels)
|
|
||||||
std::get<3>(pair.second).erase(static_cast<const NzStaticMesh*>(resource));
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -272,7 +248,25 @@ void NzForwardRenderQueue::OnResourceReleased(const NzResource* resource, int in
|
||||||
|
|
||||||
switch (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:
|
case ResourceType_Material:
|
||||||
|
{
|
||||||
for (auto it = opaqueModels.begin(); it != opaqueModels.end(); ++it)
|
for (auto it = opaqueModels.begin(); it != opaqueModels.end(); ++it)
|
||||||
{
|
{
|
||||||
if (it->first == resource)
|
if (it->first == resource)
|
||||||
|
|
@ -282,38 +276,20 @@ void NzForwardRenderQueue::OnResourceReleased(const NzResource* resource, int in
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
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);
|
MeshInstanceContainer& meshes = std::get<2>(modelPair.second);
|
||||||
|
for (auto it = meshes.begin(); it != meshes.end();)
|
||||||
for (auto it = container.begin(); it != container.end(); ++it)
|
|
||||||
{
|
{
|
||||||
if (it->first == resource)
|
const NzMeshData& renderData = it->first;
|
||||||
{
|
if (renderData.vertexBuffer == resource)
|
||||||
container.erase(it);
|
it = meshes.erase(it);
|
||||||
break;
|
else
|
||||||
}
|
++it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -349,8 +325,8 @@ bool NzForwardRenderQueue::BatchedSpriteMaterialComparator::operator()(const NzM
|
||||||
if (uberShader1 != uberShader2)
|
if (uberShader1 != uberShader2)
|
||||||
return uberShader1 < uberShader2;
|
return uberShader1 < uberShader2;
|
||||||
|
|
||||||
const NzShader* shader1 = mat1->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
|
const NzShader* shader1 = mat1->GetShaderInstance()->GetShader();
|
||||||
const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
|
const NzShader* shader2 = mat2->GetShaderInstance()->GetShader();
|
||||||
|
|
||||||
if (shader1 != shader2)
|
if (shader1 != shader2)
|
||||||
return shader1 < shader2;
|
return shader1 < shader2;
|
||||||
|
|
@ -363,38 +339,20 @@ bool NzForwardRenderQueue::BatchedSpriteMaterialComparator::operator()(const NzM
|
||||||
return mat1 < mat2;
|
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;
|
||||||
const NzBuffer* buffer1 = (iBuffer1) ? iBuffer1->GetBuffer() : nullptr;
|
const NzBuffer* buffer2;
|
||||||
|
|
||||||
const NzIndexBuffer* iBuffer2 = subMesh1->GetIndexBuffer();
|
buffer1 = (data1.indexBuffer) ? data1.indexBuffer->GetBuffer() : nullptr;
|
||||||
const NzBuffer* buffer2 = (iBuffer2) ? iBuffer2->GetBuffer() : nullptr;
|
buffer2 = (data2.indexBuffer) ? data2.indexBuffer->GetBuffer() : nullptr;
|
||||||
|
if (buffer1 != buffer2)
|
||||||
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;
|
return buffer1 < buffer2;
|
||||||
}
|
|
||||||
else
|
buffer1 = data1.vertexBuffer->GetBuffer();
|
||||||
|
buffer2 = data2.vertexBuffer->GetBuffer();
|
||||||
|
if (buffer1 != buffer2)
|
||||||
return buffer1 < buffer2;
|
return buffer1 < buffer2;
|
||||||
|
|
||||||
|
return data1.primitiveMode < data2.primitiveMode;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -84,12 +84,12 @@ bool NzForwardRenderTechnique::Draw(const NzScene* scene) const
|
||||||
if (!m_renderQueue.opaqueModels.empty())
|
if (!m_renderQueue.opaqueModels.empty())
|
||||||
DrawOpaqueModels(scene);
|
DrawOpaqueModels(scene);
|
||||||
|
|
||||||
|
if (!m_renderQueue.transparentModels.empty())
|
||||||
|
DrawTransparentModels(scene);
|
||||||
|
|
||||||
if (!m_renderQueue.sprites.empty())
|
if (!m_renderQueue.sprites.empty())
|
||||||
DrawSprites(scene);
|
DrawSprites(scene);
|
||||||
|
|
||||||
if (!m_renderQueue.transparentsModels.empty())
|
|
||||||
DrawTransparentModels(scene);
|
|
||||||
|
|
||||||
// Les autres drawables (Exemple: Terrain)
|
// Les autres drawables (Exemple: Terrain)
|
||||||
for (const NzDrawable* drawable : m_renderQueue.otherDrawables)
|
for (const NzDrawable* drawable : m_renderQueue.otherDrawables)
|
||||||
drawable->Draw();
|
drawable->Draw();
|
||||||
|
|
@ -174,10 +174,9 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
|
||||||
if (used)
|
if (used)
|
||||||
{
|
{
|
||||||
bool& renderQueueInstancing = std::get<1>(matIt.second);
|
bool& renderQueueInstancing = std::get<1>(matIt.second);
|
||||||
NzForwardRenderQueue::BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second);
|
NzForwardRenderQueue::MeshInstanceContainer& meshInstances = std::get<2>(matIt.second);
|
||||||
NzForwardRenderQueue::BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second);
|
|
||||||
|
|
||||||
if (!skeletalContainer.empty() || !staticContainer.empty())
|
if (!meshInstances.empty())
|
||||||
{
|
{
|
||||||
const NzMaterial* material = matIt.first;
|
const NzMaterial* material = matIt.first;
|
||||||
|
|
||||||
|
|
@ -203,28 +202,17 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
|
||||||
lastShader = shader;
|
lastShader = shader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Meshes
|
||||||
// Meshs squelettiques
|
for (auto& subMeshIt : meshInstances)
|
||||||
/*if (!skeletalContainer.empty())
|
|
||||||
{
|
|
||||||
NzRenderer::SetVertexBuffer(m_skinningBuffer); // Vertex buffer commun
|
|
||||||
for (auto& subMeshIt : container)
|
|
||||||
{
|
|
||||||
///TODO
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// Meshs statiques
|
|
||||||
for (auto& subMeshIt : staticContainer)
|
|
||||||
{
|
{
|
||||||
|
const NzMeshData& meshData = subMeshIt.first;
|
||||||
const NzSpheref& boundingSphere = subMeshIt.second.first;
|
const NzSpheref& boundingSphere = subMeshIt.second.first;
|
||||||
const NzStaticMesh* mesh = subMeshIt.first;
|
std::vector<NzMatrix4f>& instances = subMeshIt.second.second;
|
||||||
std::vector<NzForwardRenderQueue::StaticData>& staticData = subMeshIt.second.second;
|
|
||||||
|
|
||||||
if (!staticData.empty())
|
if (!instances.empty())
|
||||||
{
|
{
|
||||||
const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer();
|
const NzIndexBuffer* indexBuffer = meshData.indexBuffer;
|
||||||
const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer();
|
const NzVertexBuffer* vertexBuffer = meshData.vertexBuffer;
|
||||||
|
|
||||||
// Gestion du draw call avant la boucle de rendu
|
// Gestion du draw call avant la boucle de rendu
|
||||||
std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc;
|
std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc;
|
||||||
|
|
@ -247,15 +235,12 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
|
||||||
NzRenderer::SetIndexBuffer(indexBuffer);
|
NzRenderer::SetIndexBuffer(indexBuffer);
|
||||||
NzRenderer::SetVertexBuffer(vertexBuffer);
|
NzRenderer::SetVertexBuffer(vertexBuffer);
|
||||||
|
|
||||||
nzPrimitiveMode primitiveMode = mesh->GetPrimitiveMode();
|
|
||||||
if (instancing)
|
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();
|
NzVertexBuffer* instanceBuffer = NzRenderer::GetInstanceBuffer();
|
||||||
|
|
||||||
instanceBuffer->SetVertexDeclaration(NzVertexDeclaration::Get(nzVertexLayout_Matrix4));
|
instanceBuffer->SetVertexDeclaration(NzVertexDeclaration::Get(nzVertexLayout_Matrix4));
|
||||||
|
|
||||||
unsigned int stride = instanceBuffer->GetStride();
|
|
||||||
|
|
||||||
// Avec l'instancing, impossible de sélectionner les lumières pour chaque objet
|
// 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
|
// Du coup, il n'est activé que pour les lumières directionnelles
|
||||||
unsigned int lightCount = m_directionalLights.GetLightCount();
|
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);
|
NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i);
|
||||||
}
|
}
|
||||||
|
|
||||||
const NzForwardRenderQueue::StaticData* data = &staticData[0];
|
const NzMatrix4f* instanceMatrices = &instances[0];
|
||||||
unsigned int instanceCount = staticData.size();
|
unsigned int instanceCount = instances.size();
|
||||||
unsigned int maxInstanceCount = instanceBuffer->GetVertexCount();
|
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)
|
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);
|
unsigned int renderedInstanceCount = std::min(instanceCount, maxInstanceCount);
|
||||||
instanceCount -= renderedInstanceCount;
|
instanceCount -= renderedInstanceCount;
|
||||||
|
|
||||||
NzBufferMapper<NzVertexBuffer> mapper(instanceBuffer, nzBufferAccess_DiscardAndWrite, 0, renderedInstanceCount);
|
// On remplit l'instancing buffer avec nos matrices world
|
||||||
nzUInt8* ptr = reinterpret_cast<nzUInt8*>(mapper.GetPointer());
|
instanceBuffer->Fill(instanceMatrices, 0, renderedInstanceCount, true);
|
||||||
|
instanceMatrices += renderedInstanceCount;
|
||||||
|
|
||||||
for (unsigned int i = 0; i < renderedInstanceCount; ++i)
|
// Et on affiche
|
||||||
{
|
InstancedDrawFunc(renderedInstanceCount, meshData.primitiveMode, 0, indexCount);
|
||||||
std::memcpy(ptr, data->transformMatrix, sizeof(float)*16);
|
|
||||||
|
|
||||||
data++;
|
|
||||||
ptr += stride;
|
|
||||||
}
|
|
||||||
|
|
||||||
mapper.Unmap();
|
|
||||||
|
|
||||||
InstancedDrawFunc(renderedInstanceCount, 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::Enable(nzRendererParameter_Blend, false);
|
||||||
NzRenderer::SetDepthFunc(oldDepthFunc);
|
NzRenderer::SetDepthFunc(oldDepthFunc);
|
||||||
}
|
}
|
||||||
|
|
@ -321,16 +300,16 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
|
||||||
{
|
{
|
||||||
if (lightUniforms->exists)
|
if (lightUniforms->exists)
|
||||||
{
|
{
|
||||||
for (const NzForwardRenderQueue::StaticData& data : staticData)
|
for (const NzMatrix4f& matrix : instances)
|
||||||
{
|
{
|
||||||
unsigned int directionalLightCount = m_directionalLights.GetLightCount();
|
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;
|
unsigned int lightCount = directionalLightCount + otherLightCount;
|
||||||
|
|
||||||
NzRenderer::SetMatrix(nzMatrixType_World, data.transformMatrix);
|
NzRenderer::SetMatrix(nzMatrixType_World, matrix);
|
||||||
unsigned int directionalLightIndex = 0;
|
unsigned int directionalLightIndex = 0;
|
||||||
unsigned int otherLightIndex = 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;
|
unsigned int passCount = (lightCount == 0) ? 1 : (lightCount-1)/NAZARA_GRAPHICS_MAX_LIGHTPERPASS + 1;
|
||||||
for (unsigned int pass = 0; pass < passCount; ++pass)
|
for (unsigned int pass = 0; pass < passCount; ++pass)
|
||||||
|
|
@ -349,6 +328,7 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
|
||||||
NzRenderer::SetDepthFunc(nzRendererComparison_Equal);
|
NzRenderer::SetDepthFunc(nzRendererComparison_Equal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// On active les lumières de cette passe-ci
|
||||||
for (unsigned int i = 0; i < renderedLightCount; ++i)
|
for (unsigned int i = 0; i < renderedLightCount; ++i)
|
||||||
{
|
{
|
||||||
if (directionalLightIndex >= directionalLightCount)
|
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);
|
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)
|
for (unsigned int i = renderedLightCount; i < NAZARA_GRAPHICS_MAX_LIGHTPERPASS; ++i)
|
||||||
NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*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);
|
NzRenderer::Enable(nzRendererParameter_Blend, false);
|
||||||
|
|
@ -369,14 +351,17 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
|
||||||
}
|
}
|
||||||
else
|
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);
|
NzRenderer::SetMatrix(nzMatrixType_World, matrix);
|
||||||
DrawFunc(primitiveMode, 0, indexCount);
|
DrawFunc(meshData.primitiveMode, 0, indexCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
staticData.clear();
|
instances.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -471,12 +456,12 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene) const
|
||||||
const NzShader* lastShader = nullptr;
|
const NzShader* lastShader = nullptr;
|
||||||
unsigned int lightCount = 0;
|
unsigned int lightCount = 0;
|
||||||
|
|
||||||
for (const std::pair<unsigned int, bool>& pair : m_renderQueue.transparentsModels)
|
for (unsigned int index : m_renderQueue.transparentModels)
|
||||||
{
|
{
|
||||||
|
const NzForwardRenderQueue::TransparentModelData& modelData = m_renderQueue.transparentModelData[index];
|
||||||
|
|
||||||
// Matériau
|
// Matériau
|
||||||
const NzMaterial* material = (pair.second) ?
|
const NzMaterial* material = modelData.material;
|
||||||
m_renderQueue.transparentStaticModels[pair.first].material :
|
|
||||||
m_renderQueue.transparentSkeletalModels[pair.first].material;
|
|
||||||
|
|
||||||
// On commence par appliquer du matériau (et récupérer le shader ainsi activé)
|
// On commence par appliquer du matériau (et récupérer le shader ainsi activé)
|
||||||
const NzShader* shader = material->Apply();
|
const NzShader* shader = material->Apply();
|
||||||
|
|
@ -501,15 +486,11 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mesh
|
// Mesh
|
||||||
if (pair.second)
|
const NzMatrix4f& matrix = modelData.transformMatrix;
|
||||||
{
|
const NzMeshData& meshData = modelData.meshData;
|
||||||
NzForwardRenderQueue::TransparentStaticModel& staticModel = m_renderQueue.transparentStaticModels[pair.first];
|
|
||||||
|
|
||||||
const NzMatrix4f& matrix = staticModel.transformMatrix;
|
const NzIndexBuffer* indexBuffer = meshData.indexBuffer;
|
||||||
const NzStaticMesh* mesh = staticModel.mesh;
|
const NzVertexBuffer* vertexBuffer = meshData.vertexBuffer;
|
||||||
|
|
||||||
const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer();
|
|
||||||
const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer();
|
|
||||||
|
|
||||||
// Gestion du draw call avant la boucle de rendu
|
// Gestion du draw call avant la boucle de rendu
|
||||||
std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc;
|
std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc;
|
||||||
|
|
@ -532,7 +513,7 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene) const
|
||||||
// Calcul des lumières les plus proches
|
// Calcul des lumières les plus proches
|
||||||
if (lightCount < NAZARA_GRAPHICS_MAX_LIGHTPERPASS && !m_lights.IsEmpty())
|
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));
|
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)
|
for (unsigned int i = 0; i < count; ++i)
|
||||||
m_lights.GetResult(i)->Enable(shader, lightUniforms->uniforms, lightUniforms->offset*(lightCount++));
|
m_lights.GetResult(i)->Enable(shader, lightUniforms->uniforms, lightUniforms->offset*(lightCount++));
|
||||||
}
|
}
|
||||||
|
|
@ -541,12 +522,7 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene) const
|
||||||
NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i);
|
NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i);
|
||||||
|
|
||||||
NzRenderer::SetMatrix(nzMatrixType_World, matrix);
|
NzRenderer::SetMatrix(nzMatrixType_World, matrix);
|
||||||
DrawFunc(mesh->GetPrimitiveMode(), 0, indexCount);
|
DrawFunc(meshData.primitiveMode, 0, indexCount);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
///TODO
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue