Graphics: Remake DepthRender[Queue|Technique]

Former-commit-id: c4d8d4d28d02822273ebe7dca3e468ea156af674
This commit is contained in:
Lynix 2015-07-05 23:43:35 +02:00
parent 6870148314
commit 1f2e810927
4 changed files with 387 additions and 428 deletions

View File

@ -18,10 +18,10 @@
#include <map> #include <map>
#include <tuple> #include <tuple>
class NAZARA_GRAPHICS_API NzDepthRenderQueue : public NzAbstractRenderQueue class NAZARA_GRAPHICS_API NzDepthRenderQueue : public NzForwardRenderQueue
{ {
public: public:
NzDepthRenderQueue() = default; NzDepthRenderQueue();
~NzDepthRenderQueue() = default; ~NzDepthRenderQueue() = default;
void AddBillboard(const NzMaterial* material, const NzVector3f& position, const NzVector2f& size, const NzVector2f& sinCos = NzVector2f(0.f, 1.f), const NzColor& color = NzColor::White) override; void AddBillboard(const NzMaterial* material, const NzVector3f& position, const NzVector2f& size, const NzVector2f& sinCos = NzVector2f(0.f, 1.f), const NzColor& color = NzColor::White) override;
@ -33,44 +33,16 @@ class NAZARA_GRAPHICS_API NzDepthRenderQueue : public NzAbstractRenderQueue
void AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr, NzSparsePtr<const float> alphaPtr) override; void AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr, NzSparsePtr<const float> alphaPtr) override;
void AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const NzColor> colorPtr = nullptr) override; void AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const NzColor> colorPtr = nullptr) override;
void AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const float> alphaPtr) override; void AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const float> alphaPtr) override;
void AddDrawable(const NzDrawable* drawable) override; void AddDirectionalLight(const DirectionalLight& light) override;
void AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) override; void AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) override;
void AddPointLight(const PointLight& light) override;
void AddSpotLight(const SpotLight& light) override;
void AddSprites(const NzMaterial* material, const NzVertexStruct_XYZ_Color_UV* vertices, unsigned int spriteCount, const NzTexture* overlay = nullptr) override; void AddSprites(const NzMaterial* material, const NzVertexStruct_XYZ_Color_UV* vertices, unsigned int spriteCount, const NzTexture* overlay = nullptr) override;
void Clear(bool fully = false);
struct BillboardData
{
NzColor color;
NzVector3f center;
NzVector2f size;
NzVector2f sinCos;
};
struct MeshInstanceEntry
{
NazaraSlot(NzIndexBuffer, OnIndexBufferRelease, indexBufferReleaseSlot);
NazaraSlot(NzVertexBuffer, OnVertexBufferRelease, vertexBufferReleaseSlot);
std::vector<NzMatrix4f> instances;
};
struct SpriteChain_XYZ_Color_UV
{
const NzVertexStruct_XYZ_Color_UV* vertices;
unsigned int spriteCount;
};
std::map<NzMeshData, MeshInstanceEntry, NzForwardRenderQueue::MeshDataComparator> meshes;
std::vector<BillboardData> billboards;
std::vector<const NzDrawable*> otherDrawables;
std::vector<SpriteChain_XYZ_Color_UV> basicSprites;
private: private:
bool IsMaterialSuitable(const NzMaterial* material) const; bool IsMaterialSuitable(const NzMaterial* material) const;
void OnIndexBufferInvalidation(const NzIndexBuffer* indexBuffer); NzMaterialRef m_baseMaterial;
void OnVertexBufferInvalidation(const NzVertexBuffer* vertexBuffer);
}; };
#endif // NAZARA_DEPTHRENDERQUEUE_HPP #endif // NAZARA_DEPTHRENDERQUEUE_HPP

View File

@ -12,7 +12,6 @@
#include <Nazara/Graphics/Config.hpp> #include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/DepthRenderQueue.hpp> #include <Nazara/Graphics/DepthRenderQueue.hpp>
#include <Nazara/Graphics/Light.hpp> #include <Nazara/Graphics/Light.hpp>
#include <Nazara/Graphics/Material.hpp>
#include <Nazara/Renderer/Shader.hpp> #include <Nazara/Renderer/Shader.hpp>
#include <Nazara/Utility/IndexBuffer.hpp> #include <Nazara/Utility/IndexBuffer.hpp>
#include <Nazara/Utility/VertexBuffer.hpp> #include <Nazara/Utility/VertexBuffer.hpp>
@ -32,22 +31,51 @@ class NAZARA_GRAPHICS_API NzDepthRenderTechnique : public NzAbstractRenderTechni
static void Uninitialize(); static void Uninitialize();
private: private:
struct ShaderUniforms;
void DrawBasicSprites(const NzSceneData& sceneData) const; void DrawBasicSprites(const NzSceneData& sceneData) const;
void DrawBillboards(const NzSceneData& sceneData) const; void DrawBillboards(const NzSceneData& sceneData) const;
void DrawOpaqueModels(const NzSceneData& sceneData) const; void DrawOpaqueModels(const NzSceneData& sceneData) const;
const ShaderUniforms* GetShaderUniforms(const NzShader* shader) const;
void OnShaderInvalidated(const NzShader* shader) const;
struct LightIndex
{
nzLightType type;
float score;
unsigned int index;
};
struct ShaderUniforms
{
NazaraSlot(NzShader, OnShaderUniformInvalidated, shaderUniformInvalidatedSlot);
NazaraSlot(NzShader, OnShaderRelease, shaderReleaseSlot);
NzLightUniforms lightUniforms;
bool hasLightUniforms;
/// Moins coûteux en mémoire que de stocker un NzLightUniforms par index de lumière,
/// à voir si ça fonctionne chez tout le monde
int lightOffset; // "Distance" entre Lights[0].type et Lights[1].type
// Autre uniformes
int eyePosition;
int sceneAmbient;
int textureOverlay;
};
mutable std::unordered_map<const NzShader*, ShaderUniforms> m_shaderUniforms;
NzBuffer m_vertexBuffer; NzBuffer m_vertexBuffer;
mutable NzDepthRenderQueue m_renderQueue; mutable NzDepthRenderQueue m_renderQueue;
NzVertexBuffer m_billboardPointBuffer; NzVertexBuffer m_billboardPointBuffer;
NzVertexBuffer m_spriteBuffer; NzVertexBuffer m_spriteBuffer;
static NzIndexBuffer s_quadIndexBuffer; static NzIndexBuffer s_quadIndexBuffer;
static NzMaterialRef s_material;
static NzVertexBuffer s_quadVertexBuffer; static NzVertexBuffer s_quadVertexBuffer;
static NzVertexDeclaration s_billboardInstanceDeclaration; static NzVertexDeclaration s_billboardInstanceDeclaration;
static NzVertexDeclaration s_billboardVertexDeclaration; static NzVertexDeclaration s_billboardVertexDeclaration;
}; };
#include <Nazara/Graphics/DepthRenderTechnique.inl> #include <Nazara/Graphics/dEPTHRenderTechnique.inl>
#endif // NAZARA_DEPTHRENDERTECHNIQUE_HPP #endif // NAZARA_DEPTHRENDERTECHNIQUE_HPP

View File

@ -7,76 +7,58 @@
#include <Nazara/Graphics/Material.hpp> #include <Nazara/Graphics/Material.hpp>
#include <Nazara/Graphics/Debug.hpp> #include <Nazara/Graphics/Debug.hpp>
///TODO: Remplacer les sinus/cosinus par une lookup table (va booster les perfs d'un bon x10) NzDepthRenderQueue::NzDepthRenderQueue()
{
// Material
m_baseMaterial = NzMaterial::New();
m_baseMaterial->Enable(nzRendererParameter_ColorWrite, false);
m_baseMaterial->Enable(nzRendererParameter_FaceCulling, false);
//m_baseMaterial->SetFaceCulling(nzFaceSide_Front);
}
void NzDepthRenderQueue::AddBillboard(const NzMaterial* material, const NzVector3f& position, const NzVector2f& size, const NzVector2f& sinCos, const NzColor& color) void NzDepthRenderQueue::AddBillboard(const NzMaterial* material, const NzVector3f& position, const NzVector2f& size, const NzVector2f& sinCos, const NzColor& color)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
if (IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
billboards.push_back(BillboardData{color, position, size, sinCos}); return;
if (material->HasDepthMaterial())
material = material->GetDepthMaterial();
else
material = m_baseMaterial;
NzForwardRenderQueue::AddBillboard(material, position, size, sinCos, color);
} }
void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr, NzSparsePtr<const NzColor> colorPtr) void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr, NzSparsePtr<const NzColor> colorPtr)
{ {
///DOC: sinCosPtr et colorPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
NzVector2f defaultSinCos(0.f, 1.f); // sin(0) = 0, cos(0) = 1 if (material->HasDepthMaterial())
material = material->GetDepthMaterial();
else
material = m_baseMaterial;
if (!sinCosPtr) NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, sinCosPtr, colorPtr);
sinCosPtr.Reset(&defaultSinCos, 0); // L'astuce ici est de mettre le stride sur zéro, rendant le pointeur immobile
if (!colorPtr)
colorPtr.Reset(&NzColor::White, 0); // Pareil
unsigned int prevSize = billboards.size();
billboards.resize(prevSize + count);
BillboardData* billboardData = &billboards[prevSize];
for (unsigned int i = 0; i < count; ++i)
{
billboardData->center = *positionPtr++;
billboardData->color = *colorPtr++;
billboardData->sinCos = *sinCosPtr++;
billboardData->size = *sizePtr++;
billboardData++;
}
} }
void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr, NzSparsePtr<const float> alphaPtr) void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr, NzSparsePtr<const float> alphaPtr)
{ {
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
NzVector2f defaultSinCos(0.f, 1.f); // sin(0) = 0, cos(0) = 1 if (material->HasDepthMaterial())
material = material->GetDepthMaterial();
else
material = m_baseMaterial;
if (!sinCosPtr) NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, sinCosPtr, alphaPtr);
sinCosPtr.Reset(&defaultSinCos, 0); // L'astuce ici est de mettre le stride sur zéro, rendant le pointeur immobile
float defaultAlpha = 1.f;
if (!alphaPtr)
alphaPtr.Reset(&defaultAlpha, 0); // Pareil
unsigned int prevSize = billboards.size();
billboards.resize(prevSize + count);
BillboardData* billboardData = &billboards[prevSize];
for (unsigned int i = 0; i < count; ++i)
{
billboardData->center = *positionPtr++;
billboardData->color = NzColor(255, 255, 255, static_cast<nzUInt8>(255.f * (*alphaPtr++)));
billboardData->sinCos = *sinCosPtr++;
billboardData->size = *sizePtr++;
billboardData++;
}
} }
void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const NzColor> colorPtr) void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const NzColor> colorPtr)
@ -86,129 +68,57 @@ void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
///DOC: sinCosPtr et colorPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White if (material->HasDepthMaterial())
float defaultRotation = 0.f; material = material->GetDepthMaterial();
else
material = m_baseMaterial;
if (!anglePtr) NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, anglePtr, colorPtr);
anglePtr.Reset(&defaultRotation, 0); // L'astuce ici est de mettre le stride sur zéro, rendant le pointeur immobile
if (!colorPtr)
colorPtr.Reset(&NzColor::White, 0); // Pareil
unsigned int prevSize = billboards.size();
billboards.resize(prevSize + count);
BillboardData* billboardData = &billboards[prevSize];
for (unsigned int i = 0; i < count; ++i)
{
float sin = std::sin(NzToRadians(*anglePtr));
float cos = std::cos(NzToRadians(*anglePtr));
anglePtr++;
billboardData->center = *positionPtr++;
billboardData->color = *colorPtr++;
billboardData->sinCos.Set(sin, cos);
billboardData->size = *sizePtr++;
billboardData++;
}
} }
void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const float> alphaPtr) void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const float> alphaPtr)
{ {
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
float defaultRotation = 0.f; if (material->HasDepthMaterial())
material = material->GetDepthMaterial();
else
material = m_baseMaterial;
if (!anglePtr) NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, anglePtr, alphaPtr);
anglePtr.Reset(&defaultRotation, 0); // L'astuce ici est de mettre le stride sur zéro, rendant le pointeur immobile
float defaultAlpha = 1.f;
if (!alphaPtr)
alphaPtr.Reset(&defaultAlpha, 0); // Pareil
unsigned int prevSize = billboards.size();
billboards.resize(prevSize + count);
BillboardData* billboardData = &billboards[prevSize];
for (unsigned int i = 0; i < count; ++i)
{
float sin = std::sin(NzToRadians(*anglePtr));
float cos = std::cos(NzToRadians(*anglePtr));
anglePtr++;
billboardData->center = *positionPtr++;
billboardData->color = NzColor(255, 255, 255, static_cast<nzUInt8>(255.f * (*alphaPtr++)));
billboardData->sinCos.Set(sin, cos);
billboardData->size = *sizePtr++;
billboardData++;
}
} }
void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr, NzSparsePtr<const NzColor> colorPtr) void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr, NzSparsePtr<const NzColor> colorPtr)
{ {
///DOC: sinCosPtr et colorPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
NzVector2f defaultSinCos(0.f, 1.f); // sin(0) = 0, cos(0) = 1 if (material->HasDepthMaterial())
material = material->GetDepthMaterial();
else
material = m_baseMaterial;
if (!sinCosPtr) NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, sinCosPtr, colorPtr);
sinCosPtr.Reset(&defaultSinCos, 0); // L'astuce ici est de mettre le stride sur zéro, rendant le pointeur immobile
if (!colorPtr)
colorPtr.Reset(&NzColor::White, 0); // Pareil
unsigned int prevSize = billboards.size();
billboards.resize(prevSize + count);
BillboardData* billboardData = &billboards[prevSize];
for (unsigned int i = 0; i < count; ++i)
{
billboardData->center = *positionPtr++;
billboardData->color = *colorPtr++;
billboardData->sinCos = *sinCosPtr++;
billboardData->size.Set(*sizePtr++);
billboardData++;
}
} }
void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr, NzSparsePtr<const float> alphaPtr) void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr, NzSparsePtr<const float> alphaPtr)
{ {
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
NzVector2f defaultSinCos(0.f, 1.f); // sin(0) = 0, cos(0) = 1 if (material->HasDepthMaterial())
material = material->GetDepthMaterial();
else
material = m_baseMaterial;
if (!sinCosPtr) NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, sinCosPtr, alphaPtr);
sinCosPtr.Reset(&defaultSinCos, 0); // L'astuce ici est de mettre le stride sur zéro, rendant le pointeur immobile
float defaultAlpha = 1.f;
if (!alphaPtr)
alphaPtr.Reset(&defaultAlpha, 0); // Pareil
unsigned int prevSize = billboards.size();
billboards.resize(prevSize + count);
BillboardData* billboardData = &billboards[prevSize];
for (unsigned int i = 0; i < count; ++i)
{
billboardData->center = *positionPtr++;
billboardData->color = NzColor(255, 255, 255, static_cast<nzUInt8>(255.f * (*alphaPtr++)));
billboardData->sinCos = *sinCosPtr++;
billboardData->size.Set(*sizePtr++);
billboardData++;
}
} }
void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const NzColor> colorPtr) void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const NzColor> colorPtr)
@ -218,80 +128,33 @@ void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
///DOC: sinCosPtr et colorPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White if (material->HasDepthMaterial())
float defaultRotation = 0.f; material = material->GetDepthMaterial();
else
material = m_baseMaterial;
if (!anglePtr) NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, anglePtr, colorPtr);
anglePtr.Reset(&defaultRotation, 0); // L'astuce ici est de mettre le stride sur zéro, rendant le pointeur immobile
if (!colorPtr)
colorPtr.Reset(&NzColor::White, 0); // Pareil
unsigned int prevSize = billboards.size();
billboards.resize(prevSize + count);
BillboardData* billboardData = &billboards[prevSize];
for (unsigned int i = 0; i < count; ++i)
{
float sin = std::sin(NzToRadians(*anglePtr));
float cos = std::cos(NzToRadians(*anglePtr));
anglePtr++;
billboardData->center = *positionPtr++;
billboardData->color = *colorPtr++;
billboardData->sinCos.Set(sin, cos);
billboardData->size.Set(*sizePtr++);
billboardData++;
}
} }
void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const float> alphaPtr) void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const float> alphaPtr)
{ {
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
float defaultRotation = 0.f; if (material->HasDepthMaterial())
material = material->GetDepthMaterial();
else
material = m_baseMaterial;
if (!anglePtr) NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, anglePtr, alphaPtr);
anglePtr.Reset(&defaultRotation, 0); // L'astuce ici est de mettre le stride sur zéro, rendant le pointeur immobile
float defaultAlpha = 1.f;
if (!alphaPtr)
alphaPtr.Reset(&defaultAlpha, 0); // Pareil
unsigned int prevSize = billboards.size();
billboards.resize(prevSize + count);
BillboardData* billboardData = &billboards[prevSize];
for (unsigned int i = 0; i < count; ++i)
{
float sin = std::sin(NzToRadians(*anglePtr));
float cos = std::cos(NzToRadians(*anglePtr));
anglePtr++;
billboardData->center = *positionPtr++;
billboardData->color = NzColor(255, 255, 255, static_cast<nzUInt8>(255.f * (*alphaPtr++)));
billboardData->sinCos.Set(sin, cos);
billboardData->size.Set(*sizePtr++);
billboardData++;
}
} }
void NzDepthRenderQueue::AddDrawable(const NzDrawable* drawable) void NzDepthRenderQueue::AddDirectionalLight(const DirectionalLight& light)
{ {
#if NAZARA_GRAPHICS_SAFE NazaraAssert(false, "Depth render queue doesn't handle lights");
if (!drawable) NazaraUnused(light);
{
NazaraError("Invalid drawable");
return;
}
#endif
otherDrawables.push_back(drawable);
} }
void NzDepthRenderQueue::AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) void NzDepthRenderQueue::AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix)
@ -302,25 +165,24 @@ void NzDepthRenderQueue::AddMesh(const NzMaterial* material, const NzMeshData& m
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
auto it = meshes.find(meshData); if (material->HasDepthMaterial())
if (it == meshes.end()) material = material->GetDepthMaterial();
{ else
MeshInstanceEntry instanceEntry; material = m_baseMaterial;
if (meshData.indexBuffer) NzForwardRenderQueue::AddMesh(material, meshData, meshAABB, transformMatrix);
instanceEntry.indexBufferReleaseSlot.Connect(meshData.indexBuffer->OnIndexBufferRelease, this, &NzDepthRenderQueue::OnIndexBufferInvalidation); }
instanceEntry.vertexBufferReleaseSlot.Connect(meshData.vertexBuffer->OnVertexBufferRelease, this, &NzDepthRenderQueue::OnVertexBufferInvalidation); void NzDepthRenderQueue::AddPointLight(const PointLight& light)
{
NazaraAssert(false, "Depth render queue doesn't handle lights");
NazaraUnused(light);
}
it = meshes.insert(std::make_pair(meshData, std::move(instanceEntry))).first; void NzDepthRenderQueue::AddSpotLight(const SpotLight& light)
} {
NazaraAssert(false, "Depth render queue doesn't handle lights");
std::vector<NzMatrix4f>& instances = it->second.instances; NazaraUnused(light);
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)
// entry.instancingEnabled = true; // Apparemment oui, activons l'instancing avec ce matériau
} }
void NzDepthRenderQueue::AddSprites(const NzMaterial* material, const NzVertexStruct_XYZ_Color_UV* vertices, unsigned int spriteCount, const NzTexture* overlay) void NzDepthRenderQueue::AddSprites(const NzMaterial* material, const NzVertexStruct_XYZ_Color_UV* vertices, unsigned int spriteCount, const NzTexture* overlay)
@ -331,49 +193,17 @@ void NzDepthRenderQueue::AddSprites(const NzMaterial* material, const NzVertexSt
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
basicSprites.push_back(SpriteChain_XYZ_Color_UV({vertices, spriteCount})); if (material->HasDepthMaterial())
} material = material->GetDepthMaterial();
else
material = m_baseMaterial;
void NzDepthRenderQueue::Clear(bool fully) NzForwardRenderQueue::AddSprites(material, vertices, spriteCount, overlay);
{
NzAbstractRenderQueue::Clear(fully);
basicSprites.clear();
billboards.clear();
otherDrawables.clear();
if (fully)
meshes.clear();
} }
bool NzDepthRenderQueue::IsMaterialSuitable(const NzMaterial* material) const bool NzDepthRenderQueue::IsMaterialSuitable(const NzMaterial* material) const
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
return material->IsEnabled(nzRendererParameter_DepthBuffer) && material->IsEnabled(nzRendererParameter_DepthWrite) && material->IsShadowCastingEnabled(); return material->HasDepthMaterial() || (material->IsEnabled(nzRendererParameter_DepthBuffer) && material->IsEnabled(nzRendererParameter_DepthWrite) && material->IsShadowCastingEnabled());
} }
void NzDepthRenderQueue::OnIndexBufferInvalidation(const NzIndexBuffer* indexBuffer)
{
for (auto it = meshes.begin(); it != meshes.end();)
{
const NzMeshData& renderData = it->first;
if (renderData.indexBuffer == indexBuffer)
it = meshes.erase(it);
else
++it;
}
}
void NzDepthRenderQueue::OnVertexBufferInvalidation(const NzVertexBuffer* vertexBuffer)
{
for (auto it = meshes.begin(); it != meshes.end();)
{
const NzMeshData& renderData = it->first;
if (renderData.vertexBuffer == vertexBuffer)
it = meshes.erase(it);
else
++it;
}
}

View File

@ -6,6 +6,7 @@
#include <Nazara/Core/ErrorFlags.hpp> #include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Core/OffsetOf.hpp> #include <Nazara/Core/OffsetOf.hpp>
#include <Nazara/Graphics/AbstractBackground.hpp> #include <Nazara/Graphics/AbstractBackground.hpp>
#include <Nazara/Graphics/AbstractViewer.hpp>
#include <Nazara/Graphics/Drawable.hpp> #include <Nazara/Graphics/Drawable.hpp>
#include <Nazara/Graphics/Light.hpp> #include <Nazara/Graphics/Light.hpp>
#include <Nazara/Graphics/Material.hpp> #include <Nazara/Graphics/Material.hpp>
@ -55,7 +56,7 @@ bool NzDepthRenderTechnique::Draw(const NzSceneData& sceneData) const
if (sceneData.background) if (sceneData.background)
sceneData.background->Draw(sceneData.viewer); sceneData.background->Draw(sceneData.viewer);
if (!m_renderQueue.meshes.empty()) if (!m_renderQueue.opaqueModels.empty())
DrawOpaqueModels(sceneData); DrawOpaqueModels(sceneData);
if (!m_renderQueue.basicSprites.empty()) if (!m_renderQueue.basicSprites.empty())
@ -78,7 +79,7 @@ NzAbstractRenderQueue* NzDepthRenderTechnique::GetRenderQueue()
nzRenderTechniqueType NzDepthRenderTechnique::GetType() const nzRenderTechniqueType NzDepthRenderTechnique::GetType() const
{ {
return nzRenderTechniqueType_BasicForward; return nzRenderTechniqueType_Depth;
} }
bool NzDepthRenderTechnique::Initialize() bool NzDepthRenderTechnique::Initialize()
@ -129,11 +130,6 @@ bool NzDepthRenderTechnique::Initialize()
s_billboardInstanceDeclaration.EnableComponent(nzVertexComponent_InstanceData0, nzComponentType_Float3, NzOffsetOf(NzForwardRenderQueue::BillboardData, center)); s_billboardInstanceDeclaration.EnableComponent(nzVertexComponent_InstanceData0, nzComponentType_Float3, NzOffsetOf(NzForwardRenderQueue::BillboardData, center));
s_billboardInstanceDeclaration.EnableComponent(nzVertexComponent_InstanceData1, nzComponentType_Float4, NzOffsetOf(NzForwardRenderQueue::BillboardData, size)); // Englobe sincos s_billboardInstanceDeclaration.EnableComponent(nzVertexComponent_InstanceData1, nzComponentType_Float4, NzOffsetOf(NzForwardRenderQueue::BillboardData, size)); // Englobe sincos
s_billboardInstanceDeclaration.EnableComponent(nzVertexComponent_InstanceData2, nzComponentType_Color, NzOffsetOf(NzForwardRenderQueue::BillboardData, color)); s_billboardInstanceDeclaration.EnableComponent(nzVertexComponent_InstanceData2, nzComponentType_Color, NzOffsetOf(NzForwardRenderQueue::BillboardData, color));
// Material
s_material = NzMaterial::New();
s_material->Enable(nzRendererParameter_ColorWrite, false);
s_material->Enable(nzRendererParameter_FaceCulling, false);
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
@ -146,7 +142,6 @@ bool NzDepthRenderTechnique::Initialize()
void NzDepthRenderTechnique::Uninitialize() void NzDepthRenderTechnique::Uninitialize()
{ {
s_material.Reset();
s_quadIndexBuffer.Reset(); s_quadIndexBuffer.Reset();
s_quadVertexBuffer.Reset(); s_quadVertexBuffer.Reset();
} }
@ -155,15 +150,55 @@ void NzDepthRenderTechnique::DrawBasicSprites(const NzSceneData& sceneData) cons
{ {
NazaraUnused(sceneData); NazaraUnused(sceneData);
const NzShader* lastShader = nullptr;
const ShaderUniforms* shaderUniforms = nullptr;
NzRenderer::SetIndexBuffer(&s_quadIndexBuffer); NzRenderer::SetIndexBuffer(&s_quadIndexBuffer);
NzRenderer::SetMatrix(nzMatrixType_World, NzMatrix4f::Identity()); NzRenderer::SetMatrix(nzMatrixType_World, NzMatrix4f::Identity());
NzRenderer::SetVertexBuffer(&m_spriteBuffer); NzRenderer::SetVertexBuffer(&m_spriteBuffer);
auto& spriteChainVector = m_renderQueue.basicSprites; for (auto& matIt : m_renderQueue.basicSprites)
{
const NzMaterial* material = matIt.first;
auto& matEntry = matIt.second;
if (matEntry.enabled)
{
auto& overlayMap = matEntry.overlayMap;
for (auto& overlayIt : overlayMap)
{
const NzTexture* overlay = overlayIt.first;
auto& spriteChainVector = overlayIt.second.spriteChains;
unsigned int spriteChainCount = spriteChainVector.size(); unsigned int spriteChainCount = spriteChainVector.size();
if (spriteChainCount > 0) if (spriteChainCount > 0)
{ {
s_material->Apply(); // On commence par appliquer du matériau (et récupérer le shader ainsi activé)
nzUInt32 flags = nzShaderFlags_VertexColor;
if (overlay)
flags |= nzShaderFlags_TextureOverlay;
nzUInt8 overlayUnit;
const NzShader* shader = material->Apply(flags, 0, &overlayUnit);
if (overlay)
{
overlayUnit++;
NzRenderer::SetTexture(overlayUnit, overlay);
NzRenderer::SetTextureSampler(overlayUnit, material->GetDiffuseSampler());
}
// Les uniformes sont conservées au sein d'un programme, inutile de les renvoyer tant qu'il ne change pas
if (shader != lastShader)
{
// Index des uniformes dans le shader
shaderUniforms = GetShaderUniforms(shader);
// Overlay
shader->SendInteger(shaderUniforms->textureOverlay, overlayUnit);
lastShader = shader;
}
unsigned int spriteChain = 0; // Quelle chaîne de sprite traitons-nous unsigned int spriteChain = 0; // Quelle chaîne de sprite traitons-nous
unsigned int spriteChainOffset = 0; // À quel offset dans la dernière chaîne nous sommes-nous arrêtés unsigned int spriteChainOffset = 0; // À quel offset dans la dernière chaîne nous sommes-nous arrêtés
@ -179,7 +214,7 @@ void NzDepthRenderTechnique::DrawBasicSprites(const NzSceneData& sceneData) cons
do do
{ {
NzDepthRenderQueue::SpriteChain_XYZ_Color_UV& currentChain = spriteChainVector[spriteChain]; NzForwardRenderQueue::SpriteChain_XYZ_Color_UV& currentChain = spriteChainVector[spriteChain];
unsigned int count = std::min(maxSpriteCount - spriteCount, currentChain.spriteCount - spriteChainOffset); unsigned int count = std::min(maxSpriteCount - spriteCount, currentChain.spriteCount - spriteChainOffset);
std::memcpy(vertices, currentChain.vertices + spriteChainOffset*4, 4*count*sizeof(NzVertexStruct_XYZ_Color_UV)); std::memcpy(vertices, currentChain.vertices + spriteChainOffset*4, 4*count*sizeof(NzVertexStruct_XYZ_Color_UV));
@ -205,6 +240,12 @@ void NzDepthRenderTechnique::DrawBasicSprites(const NzSceneData& sceneData) cons
spriteChainVector.clear(); spriteChainVector.clear();
} }
}
// On remet à zéro
matEntry.enabled = false;
}
}
} }
void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const
@ -218,13 +259,19 @@ void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const
NzRenderer::SetVertexBuffer(&s_quadVertexBuffer); NzRenderer::SetVertexBuffer(&s_quadVertexBuffer);
auto& billboardVector = m_renderQueue.billboards; for (auto& matIt : m_renderQueue.billboards)
{
const NzMaterial* material = matIt.first;
auto& entry = matIt.second;
auto& billboardVector = entry.billboards;
unsigned int billboardCount = billboardVector.size(); unsigned int billboardCount = billboardVector.size();
if (billboardCount > 0) if (billboardCount > 0)
{ {
s_material->Apply(nzShaderFlags_Billboard | nzShaderFlags_Instancing); // On commence par appliquer du matériau
material->Apply(nzShaderFlags_Billboard | nzShaderFlags_Instancing | nzShaderFlags_VertexColor);
const NzDepthRenderQueue::BillboardData* data = &billboardVector[0]; const NzForwardRenderQueue::BillboardData* data = &billboardVector[0];
unsigned int maxBillboardPerDraw = instanceBuffer->GetVertexCount(); unsigned int maxBillboardPerDraw = instanceBuffer->GetVertexCount();
do do
{ {
@ -241,19 +288,25 @@ void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const
billboardVector.clear(); billboardVector.clear();
} }
} }
}
else else
{ {
NzRenderer::SetIndexBuffer(&s_quadIndexBuffer); NzRenderer::SetIndexBuffer(&s_quadIndexBuffer);
NzRenderer::SetVertexBuffer(&m_billboardPointBuffer); NzRenderer::SetVertexBuffer(&m_billboardPointBuffer);
auto& billboardVector = m_renderQueue.billboards; for (auto& matIt : m_renderQueue.billboards)
{
const NzMaterial* material = matIt.first;
auto& entry = matIt.second;
auto& billboardVector = entry.billboards;
unsigned int billboardCount = billboardVector.size(); unsigned int billboardCount = billboardVector.size();
if (billboardCount > 0) if (billboardCount > 0)
{ {
s_material->Apply(nzShaderFlags_Billboard); // On commence par appliquer du matériau
material->Apply(nzShaderFlags_Billboard | nzShaderFlags_VertexColor);
const NzDepthRenderQueue::BillboardData* data = &billboardVector[0]; const NzForwardRenderQueue::BillboardData* data = &billboardVector[0];
unsigned int maxBillboardPerDraw = std::min(s_maxQuads, m_billboardPointBuffer.GetVertexCount()/4); unsigned int maxBillboardPerDraw = std::min(s_maxQuads, m_billboardPointBuffer.GetVertexCount()/4);
do do
@ -266,7 +319,7 @@ void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const
for (unsigned int i = 0; i < renderedBillboardCount; ++i) for (unsigned int i = 0; i < renderedBillboardCount; ++i)
{ {
const NzDepthRenderQueue::BillboardData& billboard = *data++; const NzForwardRenderQueue::BillboardData& billboard = *data++;
vertices->color = billboard.color; vertices->color = billboard.color;
vertices->position = billboard.center; vertices->position = billboard.center;
@ -306,15 +359,36 @@ void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const
billboardVector.clear(); billboardVector.clear();
} }
} }
}
} }
void NzDepthRenderTechnique::DrawOpaqueModels(const NzSceneData& sceneData) const void NzDepthRenderTechnique::DrawOpaqueModels(const NzSceneData& sceneData) const
{ {
NazaraUnused(sceneData); NazaraUnused(sceneData);
s_material->Apply(); for (auto& matIt : m_renderQueue.opaqueModels)
{
auto& matEntry = matIt.second;
for (auto& meshIt : m_renderQueue.meshes) if (matEntry.enabled)
{
NzForwardRenderQueue::MeshInstanceContainer& meshInstances = matEntry.meshMap;
if (!meshInstances.empty())
{
const NzMaterial* material = matIt.first;
// Nous utilisons de l'instancing que lorsqu'aucune lumière (autre que directionnelle) n'est active
// Ceci car l'instancing n'est pas compatible avec la recherche des lumières les plus proches
// (Le deferred shading n'a pas ce problème)
bool noPointSpotLight = m_renderQueue.pointLights.empty() && m_renderQueue.spotLights.empty();
bool instancing = m_instancingEnabled && (!material->IsLightingEnabled() || noPointSpotLight) && matEntry.instancingEnabled;
// On commence par appliquer du matériau (et récupérer le shader ainsi activé)
material->Apply((instancing) ? nzShaderFlags_Instancing : 0);
// Meshes
for (auto& meshIt : meshInstances)
{ {
const NzMeshData& meshData = meshIt.first; const NzMeshData& meshData = meshIt.first;
auto& meshEntry = meshIt.second; auto& meshEntry = meshIt.second;
@ -346,6 +420,32 @@ void NzDepthRenderTechnique::DrawOpaqueModels(const NzSceneData& sceneData) cons
NzRenderer::SetIndexBuffer(indexBuffer); NzRenderer::SetIndexBuffer(indexBuffer);
NzRenderer::SetVertexBuffer(vertexBuffer); NzRenderer::SetVertexBuffer(vertexBuffer);
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));
const NzMatrix4f* instanceMatrices = &instances[0];
unsigned int instanceCount = instances.size();
unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // Le nombre maximum d'instances en une fois
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;
// On remplit l'instancing buffer avec nos matrices world
instanceBuffer->Fill(instanceMatrices, 0, renderedInstanceCount, true);
instanceMatrices += renderedInstanceCount;
// Et on affiche
instancedDrawFunc(renderedInstanceCount, meshData.primitiveMode, 0, indexCount);
}
}
else
{
// Sans instancing, on doit effectuer un draw call pour chaque instance // Sans instancing, on doit effectuer un draw call pour chaque instance
// Cela reste néanmoins plus rapide que l'instancing en dessous d'un certain nombre d'instances // 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 // À cause du temps de modification du buffer d'instancing
@ -354,13 +454,42 @@ void NzDepthRenderTechnique::DrawOpaqueModels(const NzSceneData& sceneData) cons
NzRenderer::SetMatrix(nzMatrixType_World, matrix); NzRenderer::SetMatrix(nzMatrixType_World, matrix);
drawFunc(meshData.primitiveMode, 0, indexCount); drawFunc(meshData.primitiveMode, 0, indexCount);
} }
}
instances.clear(); instances.clear();
} }
} }
}
// Et on remet à zéro les données
matEntry.enabled = false;
matEntry.instancingEnabled = false;
}
}
}
const NzDepthRenderTechnique::ShaderUniforms* NzDepthRenderTechnique::GetShaderUniforms(const NzShader* shader) const
{
auto it = m_shaderUniforms.find(shader);
if (it == m_shaderUniforms.end())
{
ShaderUniforms uniforms;
uniforms.shaderReleaseSlot.Connect(shader->OnShaderRelease, this, &NzDepthRenderTechnique::OnShaderInvalidated);
uniforms.shaderUniformInvalidatedSlot.Connect(shader->OnShaderUniformInvalidated, this, &NzDepthRenderTechnique::OnShaderInvalidated);
uniforms.textureOverlay = shader->GetUniformLocation("TextureOverlay");
it = m_shaderUniforms.emplace(shader, std::move(uniforms)).first;
}
return &it->second;
}
void NzDepthRenderTechnique::OnShaderInvalidated(const NzShader* shader) const
{
m_shaderUniforms.erase(shader);
} }
NzIndexBuffer NzDepthRenderTechnique::s_quadIndexBuffer; NzIndexBuffer NzDepthRenderTechnique::s_quadIndexBuffer;
NzMaterialRef NzDepthRenderTechnique::s_material;
NzVertexBuffer NzDepthRenderTechnique::s_quadVertexBuffer; NzVertexBuffer NzDepthRenderTechnique::s_quadVertexBuffer;
NzVertexDeclaration NzDepthRenderTechnique::s_billboardInstanceDeclaration; NzVertexDeclaration NzDepthRenderTechnique::s_billboardInstanceDeclaration;
NzVertexDeclaration NzDepthRenderTechnique::s_billboardVertexDeclaration; NzVertexDeclaration NzDepthRenderTechnique::s_billboardVertexDeclaration;