Graphics/DepthRender: Fix compilation

Former-commit-id: 749e3b67038cba20c46bd5570e616608200733f5
This commit is contained in:
Lynix 2015-12-09 01:11:27 +01:00
parent 9cf5e4b68c
commit 9e7452ab68
4 changed files with 720 additions and 656 deletions

View File

@ -18,31 +18,36 @@
#include <map> #include <map>
#include <tuple> #include <tuple>
class NAZARA_GRAPHICS_API NzDepthRenderQueue : public NzForwardRenderQueue namespace Nz
{ {
class NAZARA_GRAPHICS_API DepthRenderQueue : public ForwardRenderQueue
{
public: public:
NzDepthRenderQueue(); DepthRenderQueue();
~NzDepthRenderQueue() = default; ~DepthRenderQueue() = 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(int renderOrder, const Material* material, const Vector3f& position, const Vector2f& size, const Vector2f& sinCos = Vector2f(0.f, 1.f), const Color& color = Color::White) override;
void AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr = nullptr, NzSparsePtr<const NzColor> colorPtr = nullptr) override; void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const Vector2f> sinCosPtr = nullptr, SparsePtr<const Color> colorPtr = nullptr) override;
void AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr, NzSparsePtr<const float> alphaPtr) override; void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<const float> alphaPtr) override;
void AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const NzColor> colorPtr = nullptr) override; void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const Color> colorPtr = nullptr) override;
void AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const NzVector2f> sizePtr, NzSparsePtr<const float> anglePtr, NzSparsePtr<const float> alphaPtr) override; void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const float> alphaPtr) override;
void AddBillboards(const NzMaterial* material, unsigned int count, NzSparsePtr<const NzVector3f> positionPtr, NzSparsePtr<const float> sizePtr, NzSparsePtr<const NzVector2f> sinCosPtr = nullptr, NzSparsePtr<const NzColor> colorPtr = nullptr) override; void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const Vector2f> sinCosPtr = nullptr, SparsePtr<const Color> colorPtr = nullptr) 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(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<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(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const Color> 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(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const float> alphaPtr) override;
void AddDirectionalLight(const DirectionalLight& light) override; void AddDirectionalLight(const DirectionalLight& light) override;
void AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) override; void AddMesh(int renderOrder, const Material* material, const MeshData& meshData, const Boxf& meshAABB, const Matrix4f& transformMatrix) override;
void AddPointLight(const PointLight& light) override; void AddPointLight(const PointLight& light) override;
void AddSpotLight(const SpotLight& 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(int renderOrder, const Material* material, const VertexStruct_XYZ_Color_UV* vertices, unsigned int spriteCount, const Texture* overlay = nullptr) override;
private: private:
bool IsMaterialSuitable(const NzMaterial* material) const; inline bool IsMaterialSuitable(const Material* material) const;
NzMaterialRef m_baseMaterial; MaterialRef m_baseMaterial;
}; };
}
#include <Nazara/Graphics/DepthRenderQueue.inl>
#endif // NAZARA_DEPTHRENDERQUEUE_HPP #endif // NAZARA_DEPTHRENDERQUEUE_HPP

View File

@ -16,16 +16,18 @@
#include <Nazara/Utility/IndexBuffer.hpp> #include <Nazara/Utility/IndexBuffer.hpp>
#include <Nazara/Utility/VertexBuffer.hpp> #include <Nazara/Utility/VertexBuffer.hpp>
class NAZARA_GRAPHICS_API NzDepthRenderTechnique : public NzAbstractRenderTechnique namespace Nz
{ {
class NAZARA_GRAPHICS_API DepthRenderTechnique : public AbstractRenderTechnique
{
public: public:
NzDepthRenderTechnique(); DepthRenderTechnique();
~NzDepthRenderTechnique() = default; ~DepthRenderTechnique() = default;
bool Draw(const NzSceneData& sceneData) const override; bool Draw(const SceneData& sceneData) const override;
NzAbstractRenderQueue* GetRenderQueue() override; AbstractRenderQueue* GetRenderQueue() override;
nzRenderTechniqueType GetType() const override; RenderTechniqueType GetType() const override;
static bool Initialize(); static bool Initialize();
static void Uninitialize(); static void Uninitialize();
@ -33,30 +35,23 @@ class NAZARA_GRAPHICS_API NzDepthRenderTechnique : public NzAbstractRenderTechni
private: private:
struct ShaderUniforms; struct ShaderUniforms;
void DrawBasicSprites(const NzSceneData& sceneData) const; void DrawBasicSprites(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const;
void DrawBillboards(const NzSceneData& sceneData) const; void DrawBillboards(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const;
void DrawOpaqueModels(const NzSceneData& sceneData) const; void DrawOpaqueModels(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const;
const ShaderUniforms* GetShaderUniforms(const NzShader* shader) const; const ShaderUniforms* GetShaderUniforms(const Shader* shader) const;
void OnShaderInvalidated(const NzShader* shader) const; void OnShaderInvalidated(const Shader* shader) const;
struct LightIndex struct LightIndex
{ {
nzLightType type; LightType type;
float score; float score;
unsigned int index; unsigned int index;
}; };
struct ShaderUniforms struct ShaderUniforms
{ {
NazaraSlot(NzShader, OnShaderUniformInvalidated, shaderUniformInvalidatedSlot); NazaraSlot(Shader, OnShaderUniformInvalidated, shaderUniformInvalidatedSlot);
NazaraSlot(NzShader, OnShaderRelease, shaderReleaseSlot); NazaraSlot(Shader, 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 // Autre uniformes
int eyePosition; int eyePosition;
@ -64,17 +59,19 @@ class NAZARA_GRAPHICS_API NzDepthRenderTechnique : public NzAbstractRenderTechni
int textureOverlay; int textureOverlay;
}; };
mutable std::unordered_map<const NzShader*, ShaderUniforms> m_shaderUniforms; mutable std::unordered_map<const Shader*, ShaderUniforms> m_shaderUniforms;
NzBuffer m_vertexBuffer; Buffer m_vertexBuffer;
mutable NzDepthRenderQueue m_renderQueue; mutable DepthRenderQueue m_renderQueue;
NzVertexBuffer m_billboardPointBuffer; VertexBuffer m_billboardPointBuffer;
NzVertexBuffer m_spriteBuffer; VertexBuffer m_spriteBuffer;
static IndexBuffer s_quadIndexBuffer;
static VertexBuffer s_quadVertexBuffer;
static VertexDeclaration s_billboardInstanceDeclaration;
static VertexDeclaration s_billboardVertexDeclaration;
};
}
static NzIndexBuffer s_quadIndexBuffer;
static NzVertexBuffer s_quadVertexBuffer;
static NzVertexDeclaration s_billboardInstanceDeclaration;
static NzVertexDeclaration s_billboardVertexDeclaration;
};
#include <Nazara/Graphics/dEPTHRenderTechnique.inl> #include <Nazara/Graphics/dEPTHRenderTechnique.inl>

View File

@ -7,18 +7,21 @@
#include <Nazara/Graphics/Material.hpp> #include <Nazara/Graphics/Material.hpp>
#include <Nazara/Graphics/Debug.hpp> #include <Nazara/Graphics/Debug.hpp>
NzDepthRenderQueue::NzDepthRenderQueue() namespace Nz
{ {
DepthRenderQueue::DepthRenderQueue()
{
// Material // Material
m_baseMaterial = NzMaterial::New(); m_baseMaterial = Material::New();
m_baseMaterial->Enable(nzRendererParameter_ColorWrite, false); m_baseMaterial->Enable(RendererParameter_ColorWrite, false);
m_baseMaterial->Enable(nzRendererParameter_FaceCulling, false); m_baseMaterial->Enable(RendererParameter_FaceCulling, false);
//m_baseMaterial->SetFaceCulling(nzFaceSide_Front); //m_baseMaterial->SetFaceCulling(FaceSide_Front);
} }
void NzDepthRenderQueue::AddBillboard(const NzMaterial* material, const NzVector3f& position, const NzVector2f& size, const NzVector2f& sinCos, const NzColor& color) void DepthRenderQueue::AddBillboard(int renderOrder, const Material* material, const Vector3f& position, const Vector2f& size, const Vector2f& sinCos, const Color& color)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
NazaraUnused(renderOrder);
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
@ -28,12 +31,13 @@ void NzDepthRenderQueue::AddBillboard(const NzMaterial* material, const NzVector
else else
material = m_baseMaterial; material = m_baseMaterial;
NzForwardRenderQueue::AddBillboard(material, position, size, sinCos, color); ForwardRenderQueue::AddBillboard(0, 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 DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<const Color> colorPtr)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
NazaraUnused(renderOrder);
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
@ -43,12 +47,13 @@ void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int
else else
material = m_baseMaterial; material = m_baseMaterial;
NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, sinCosPtr, colorPtr); ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, sinCosPtr, colorPtr);
} }
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 DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<const float> alphaPtr)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
NazaraUnused(renderOrder);
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
@ -58,12 +63,13 @@ void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int
else else
material = m_baseMaterial; material = m_baseMaterial;
NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, sinCosPtr, alphaPtr); ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, sinCosPtr, alphaPtr);
} }
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 DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const Color> colorPtr)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
NazaraUnused(renderOrder);
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
@ -73,12 +79,13 @@ void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int
else else
material = m_baseMaterial; material = m_baseMaterial;
NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, anglePtr, colorPtr); ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, anglePtr, colorPtr);
} }
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 DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const float> alphaPtr)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
NazaraUnused(renderOrder);
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
@ -88,12 +95,13 @@ void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int
else else
material = m_baseMaterial; material = m_baseMaterial;
NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, anglePtr, alphaPtr); ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, anglePtr, alphaPtr);
} }
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 DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<const Color> colorPtr)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
NazaraUnused(renderOrder);
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
@ -103,12 +111,13 @@ void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int
else else
material = m_baseMaterial; material = m_baseMaterial;
NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, sinCosPtr, colorPtr); ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, sinCosPtr, colorPtr);
} }
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 DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<const float> alphaPtr)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
NazaraUnused(renderOrder);
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
@ -118,12 +127,13 @@ void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int
else else
material = m_baseMaterial; material = m_baseMaterial;
NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, sinCosPtr, alphaPtr); ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, sinCosPtr, alphaPtr);
} }
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 DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const Color> colorPtr)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
NazaraUnused(renderOrder);
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
@ -133,12 +143,13 @@ void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int
else else
material = m_baseMaterial; material = m_baseMaterial;
NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, anglePtr, colorPtr); ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, anglePtr, colorPtr);
} }
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 DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const float> alphaPtr)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
NazaraUnused(renderOrder);
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
return; return;
@ -148,18 +159,19 @@ void NzDepthRenderQueue::AddBillboards(const NzMaterial* material, unsigned int
else else
material = m_baseMaterial; material = m_baseMaterial;
NzForwardRenderQueue::AddBillboards(material, count, positionPtr, sizePtr, anglePtr, alphaPtr); ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, anglePtr, alphaPtr);
} }
void NzDepthRenderQueue::AddDirectionalLight(const DirectionalLight& light) void DepthRenderQueue::AddDirectionalLight(const DirectionalLight& light)
{ {
NazaraAssert(false, "Depth render queue doesn't handle lights"); NazaraAssert(false, "Depth render queue doesn't handle lights");
NazaraUnused(light); NazaraUnused(light);
} }
void NzDepthRenderQueue::AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) void DepthRenderQueue::AddMesh(int renderOrder, const Material* material, const MeshData& meshData, const Boxf& meshAABB, const Matrix4f& transformMatrix)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
NazaraUnused(renderOrder);
NazaraUnused(meshAABB); NazaraUnused(meshAABB);
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
@ -170,24 +182,25 @@ void NzDepthRenderQueue::AddMesh(const NzMaterial* material, const NzMeshData& m
else else
material = m_baseMaterial; material = m_baseMaterial;
NzForwardRenderQueue::AddMesh(material, meshData, meshAABB, transformMatrix); ForwardRenderQueue::AddMesh(0, material, meshData, meshAABB, transformMatrix);
} }
void NzDepthRenderQueue::AddPointLight(const PointLight& light) void DepthRenderQueue::AddPointLight(const PointLight& light)
{ {
NazaraAssert(false, "Depth render queue doesn't handle lights"); NazaraAssert(false, "Depth render queue doesn't handle lights");
NazaraUnused(light); NazaraUnused(light);
} }
void NzDepthRenderQueue::AddSpotLight(const SpotLight& light) void DepthRenderQueue::AddSpotLight(const SpotLight& light)
{ {
NazaraAssert(false, "Depth render queue doesn't handle lights"); NazaraAssert(false, "Depth render queue doesn't handle lights");
NazaraUnused(light); NazaraUnused(light);
} }
void NzDepthRenderQueue::AddSprites(const NzMaterial* material, const NzVertexStruct_XYZ_Color_UV* vertices, unsigned int spriteCount, const NzTexture* overlay) void DepthRenderQueue::AddSprites(int renderOrder, const Material* material, const VertexStruct_XYZ_Color_UV* vertices, unsigned int spriteCount, const Texture* overlay)
{ {
NazaraAssert(material, "Invalid material"); NazaraAssert(material, "Invalid material");
NazaraUnused(renderOrder);
NazaraUnused(overlay); NazaraUnused(overlay);
if (!IsMaterialSuitable(material)) if (!IsMaterialSuitable(material))
@ -198,12 +211,7 @@ void NzDepthRenderQueue::AddSprites(const NzMaterial* material, const NzVertexSt
else else
material = m_baseMaterial; material = m_baseMaterial;
NzForwardRenderQueue::AddSprites(material, vertices, spriteCount, overlay); ForwardRenderQueue::AddSprites(0, material, vertices, spriteCount, overlay);
}
} }
bool NzDepthRenderQueue::IsMaterialSuitable(const NzMaterial* material) const
{
NazaraAssert(material, "Invalid material");
return material->HasDepthMaterial() || (material->IsEnabled(nzRendererParameter_DepthBuffer) && material->IsEnabled(nzRendererParameter_DepthWrite) && material->IsShadowCastingEnabled());
}

View File

@ -20,97 +20,103 @@
#include <memory> #include <memory>
#include <Nazara/Graphics/Debug.hpp> #include <Nazara/Graphics/Debug.hpp>
namespace namespace Nz
{ {
namespace
{
struct BillboardPoint struct BillboardPoint
{ {
NzColor color; Color color;
NzVector3f position; Vector3f position;
NzVector2f size; Vector2f size;
NzVector2f sinCos; // must follow `size` (both will be sent as a Vector4f) Vector2f sinCos; // must follow `size` (both will be sent as a Vector4f)
NzVector2f uv; Vector2f uv;
}; };
unsigned int s_maxQuads = std::numeric_limits<nzUInt16>::max()/6; unsigned int s_maxQuads = std::numeric_limits<UInt16>::max() / 6;
unsigned int s_vertexBufferSize = 4*1024*1024; // 4 MiB unsigned int s_vertexBufferSize = 4 * 1024 * 1024; // 4 MiB
} }
NzDepthRenderTechnique::NzDepthRenderTechnique() : DepthRenderTechnique::DepthRenderTechnique() :
m_vertexBuffer(nzBufferType_Vertex) m_vertexBuffer(BufferType_Vertex)
{ {
NzErrorFlags flags(nzErrorFlag_ThrowException, true); ErrorFlags flags(ErrorFlag_ThrowException, true);
m_vertexBuffer.Create(s_vertexBufferSize, nzDataStorage_Hardware, nzBufferUsage_Dynamic); m_vertexBuffer.Create(s_vertexBufferSize, DataStorage_Hardware, BufferUsage_Dynamic);
m_billboardPointBuffer.Reset(&s_billboardVertexDeclaration, &m_vertexBuffer); m_billboardPointBuffer.Reset(&s_billboardVertexDeclaration, &m_vertexBuffer);
m_spriteBuffer.Reset(NzVertexDeclaration::Get(nzVertexLayout_XYZ_Color_UV), &m_vertexBuffer); m_spriteBuffer.Reset(VertexDeclaration::Get(VertexLayout_XYZ_Color_UV), &m_vertexBuffer);
} }
bool NzDepthRenderTechnique::Draw(const NzSceneData& sceneData) const bool DepthRenderTechnique::Draw(const SceneData& sceneData) const
{ {
NzRenderer::Enable(nzRendererParameter_DepthBuffer, true); Renderer::Enable(RendererParameter_DepthBuffer, true);
NzRenderer::Enable(nzRendererParameter_DepthWrite, true); Renderer::Enable(RendererParameter_DepthWrite, true);
NzRenderer::Clear(nzRendererBuffer_Depth); Renderer::Clear(RendererBuffer_Depth);
// Just in case the background does render depth // Just in case the background does render depth
if (sceneData.background) if (sceneData.background)
sceneData.background->Draw(sceneData.viewer); sceneData.background->Draw(sceneData.viewer);
if (!m_renderQueue.opaqueModels.empty()) for (auto& pair : m_renderQueue.layers)
DrawOpaqueModels(sceneData); {
ForwardRenderQueue::Layer& layer = pair.second;
if (!m_renderQueue.basicSprites.empty()) if (!layer.opaqueModels.empty())
DrawBasicSprites(sceneData); DrawOpaqueModels(sceneData, layer);
if (!m_renderQueue.billboards.empty()) if (!layer.basicSprites.empty())
DrawBillboards(sceneData); DrawBasicSprites(sceneData, layer);
// Other custom drawables if (!layer.billboards.empty())
for (const NzDrawable* drawable : m_renderQueue.otherDrawables) DrawBillboards(sceneData, layer);
for (const Drawable* drawable : layer.otherDrawables)
drawable->Draw(); drawable->Draw();
}
return true; return true;
} }
NzAbstractRenderQueue* NzDepthRenderTechnique::GetRenderQueue() AbstractRenderQueue* DepthRenderTechnique::GetRenderQueue()
{ {
return &m_renderQueue; return &m_renderQueue;
} }
nzRenderTechniqueType NzDepthRenderTechnique::GetType() const RenderTechniqueType DepthRenderTechnique::GetType() const
{ {
return nzRenderTechniqueType_Depth; return RenderTechniqueType_Depth;
} }
bool NzDepthRenderTechnique::Initialize() bool DepthRenderTechnique::Initialize()
{ {
try try
{ {
NzErrorFlags flags(nzErrorFlag_ThrowException, true); ErrorFlags flags(ErrorFlag_ThrowException, true);
s_quadIndexBuffer.Reset(false, s_maxQuads*6, nzDataStorage_Hardware, nzBufferUsage_Static); s_quadIndexBuffer.Reset(false, s_maxQuads * 6, DataStorage_Hardware, BufferUsage_Static);
NzBufferMapper<NzIndexBuffer> mapper(s_quadIndexBuffer, nzBufferAccess_WriteOnly); BufferMapper<IndexBuffer> mapper(s_quadIndexBuffer, BufferAccess_WriteOnly);
nzUInt16* indices = static_cast<nzUInt16*>(mapper.GetPointer()); UInt16* indices = static_cast<UInt16*>(mapper.GetPointer());
for (unsigned int i = 0; i < s_maxQuads; ++i) for (unsigned int i = 0; i < s_maxQuads; ++i)
{ {
*indices++ = i*4 + 0; *indices++ = i * 4 + 0;
*indices++ = i*4 + 2; *indices++ = i * 4 + 2;
*indices++ = i*4 + 1; *indices++ = i * 4 + 1;
*indices++ = i*4 + 2; *indices++ = i * 4 + 2;
*indices++ = i*4 + 3; *indices++ = i * 4 + 3;
*indices++ = i*4 + 1; *indices++ = i * 4 + 1;
} }
mapper.Unmap(); // Inutile de garder le buffer ouvert plus longtemps mapper.Unmap(); // Inutile de garder le buffer ouvert plus longtemps
// Quad buffer (utilisé pour l'instancing de billboard et de sprites) // Quad buffer (utilisé pour l'instancing de billboard et de sprites)
//Note: Les UV sont calculés dans le shader //Note: Les UV sont calculés dans le shader
s_quadVertexBuffer.Reset(NzVertexDeclaration::Get(nzVertexLayout_XY), 4, nzDataStorage_Hardware, nzBufferUsage_Static); s_quadVertexBuffer.Reset(VertexDeclaration::Get(VertexLayout_XY), 4, DataStorage_Hardware, BufferUsage_Static);
float vertices[2*4] = { float vertices[2 * 4] = {
-0.5f, -0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f, 0.5f,
@ -120,46 +126,46 @@ bool NzDepthRenderTechnique::Initialize()
s_quadVertexBuffer.FillRaw(vertices, 0, sizeof(vertices)); s_quadVertexBuffer.FillRaw(vertices, 0, sizeof(vertices));
// Déclaration lors du rendu des billboards par sommet // Déclaration lors du rendu des billboards par sommet
s_billboardVertexDeclaration.EnableComponent(nzVertexComponent_Color, nzComponentType_Color, NzOffsetOf(BillboardPoint, color)); s_billboardVertexDeclaration.EnableComponent(VertexComponent_Color, ComponentType_Color, NazaraOffsetOf(BillboardPoint, color));
s_billboardVertexDeclaration.EnableComponent(nzVertexComponent_Position, nzComponentType_Float3, NzOffsetOf(BillboardPoint, position)); s_billboardVertexDeclaration.EnableComponent(VertexComponent_Position, ComponentType_Float3, NazaraOffsetOf(BillboardPoint, position));
s_billboardVertexDeclaration.EnableComponent(nzVertexComponent_TexCoord, nzComponentType_Float2, NzOffsetOf(BillboardPoint, uv)); s_billboardVertexDeclaration.EnableComponent(VertexComponent_TexCoord, ComponentType_Float2, NazaraOffsetOf(BillboardPoint, uv));
s_billboardVertexDeclaration.EnableComponent(nzVertexComponent_Userdata0, nzComponentType_Float4, NzOffsetOf(BillboardPoint, size)); // Englobe sincos s_billboardVertexDeclaration.EnableComponent(VertexComponent_Userdata0, ComponentType_Float4, NazaraOffsetOf(BillboardPoint, size)); // Englobe sincos
// Declaration utilisée lors du rendu des billboards par instancing // Declaration utilisée lors du rendu des billboards par instancing
// L'avantage ici est la copie directe (std::memcpy) des données de la RenderQueue vers le buffer GPU // L'avantage ici est la copie directe (std::memcpy) des données de la RenderQueue vers le buffer GPU
s_billboardInstanceDeclaration.EnableComponent(nzVertexComponent_InstanceData0, nzComponentType_Float3, NzOffsetOf(NzForwardRenderQueue::BillboardData, center)); s_billboardInstanceDeclaration.EnableComponent(VertexComponent_InstanceData0, ComponentType_Float3, NazaraOffsetOf(ForwardRenderQueue::BillboardData, center));
s_billboardInstanceDeclaration.EnableComponent(nzVertexComponent_InstanceData1, nzComponentType_Float4, NzOffsetOf(NzForwardRenderQueue::BillboardData, size)); // Englobe sincos s_billboardInstanceDeclaration.EnableComponent(VertexComponent_InstanceData1, ComponentType_Float4, NazaraOffsetOf(ForwardRenderQueue::BillboardData, size)); // Englobe sincos
s_billboardInstanceDeclaration.EnableComponent(nzVertexComponent_InstanceData2, nzComponentType_Color, NzOffsetOf(NzForwardRenderQueue::BillboardData, color)); s_billboardInstanceDeclaration.EnableComponent(VertexComponent_InstanceData2, ComponentType_Color, NazaraOffsetOf(ForwardRenderQueue::BillboardData, color));
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
NazaraError("Failed to initialise: " + NzString(e.what())); NazaraError("Failed to initialise: " + String(e.what()));
return false; return false;
} }
return true; return true;
} }
void NzDepthRenderTechnique::Uninitialize() void DepthRenderTechnique::Uninitialize()
{ {
s_quadIndexBuffer.Reset(); s_quadIndexBuffer.Reset();
s_quadVertexBuffer.Reset(); s_quadVertexBuffer.Reset();
} }
void NzDepthRenderTechnique::DrawBasicSprites(const NzSceneData& sceneData) const void DepthRenderTechnique::DrawBasicSprites(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const
{ {
NazaraUnused(sceneData); NazaraAssert(sceneData.viewer, "Invalid viewer");
const NzShader* lastShader = nullptr; const Shader* lastShader = nullptr;
const ShaderUniforms* shaderUniforms = nullptr; const ShaderUniforms* shaderUniforms = nullptr;
NzRenderer::SetIndexBuffer(&s_quadIndexBuffer); Renderer::SetIndexBuffer(&s_quadIndexBuffer);
NzRenderer::SetMatrix(nzMatrixType_World, NzMatrix4f::Identity()); Renderer::SetMatrix(MatrixType_World, Matrix4f::Identity());
NzRenderer::SetVertexBuffer(&m_spriteBuffer); Renderer::SetVertexBuffer(&m_spriteBuffer);
for (auto& matIt : m_renderQueue.basicSprites) for (auto& matIt : layer.basicSprites)
{ {
const NzMaterial* material = matIt.first; const Material* material = matIt.first;
auto& matEntry = matIt.second; auto& matEntry = matIt.second;
if (matEntry.enabled) if (matEntry.enabled)
@ -167,25 +173,25 @@ void NzDepthRenderTechnique::DrawBasicSprites(const NzSceneData& sceneData) cons
auto& overlayMap = matEntry.overlayMap; auto& overlayMap = matEntry.overlayMap;
for (auto& overlayIt : overlayMap) for (auto& overlayIt : overlayMap)
{ {
const NzTexture* overlay = overlayIt.first; const Texture* overlay = overlayIt.first;
auto& spriteChainVector = overlayIt.second.spriteChains; auto& spriteChainVector = overlayIt.second.spriteChains;
unsigned int spriteChainCount = spriteChainVector.size(); unsigned int spriteChainCount = spriteChainVector.size();
if (spriteChainCount > 0) if (spriteChainCount > 0)
{ {
// 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é)
nzUInt32 flags = nzShaderFlags_VertexColor; UInt32 flags = ShaderFlags_VertexColor;
if (overlay) if (overlay)
flags |= nzShaderFlags_TextureOverlay; flags |= ShaderFlags_TextureOverlay;
nzUInt8 overlayUnit; UInt8 overlayUnit;
const NzShader* shader = material->Apply(flags, 0, &overlayUnit); const Shader* shader = material->Apply(flags, 0, &overlayUnit);
if (overlay) if (overlay)
{ {
overlayUnit++; overlayUnit++;
NzRenderer::SetTexture(overlayUnit, overlay); Renderer::SetTexture(overlayUnit, overlay);
NzRenderer::SetTextureSampler(overlayUnit, material->GetDiffuseSampler()); Renderer::SetTextureSampler(overlayUnit, material->GetDiffuseSampler());
} }
// Les uniformes sont conservées au sein d'un programme, inutile de les renvoyer tant qu'il ne change pas // Les uniformes sont conservées au sein d'un programme, inutile de les renvoyer tant qu'il ne change pas
@ -194,8 +200,12 @@ void NzDepthRenderTechnique::DrawBasicSprites(const NzSceneData& sceneData) cons
// Index des uniformes dans le shader // Index des uniformes dans le shader
shaderUniforms = GetShaderUniforms(shader); shaderUniforms = GetShaderUniforms(shader);
// Couleur ambiante de la scène
shader->SendColor(shaderUniforms->sceneAmbient, sceneData.ambientColor);
// Overlay // Overlay
shader->SendInteger(shaderUniforms->textureOverlay, overlayUnit); shader->SendInteger(shaderUniforms->textureOverlay, overlayUnit);
// Position de la caméra
shader->SendVector(shaderUniforms->eyePosition, sceneData.viewer->GetEyePosition());
lastShader = shader; lastShader = shader;
} }
@ -206,18 +216,18 @@ void NzDepthRenderTechnique::DrawBasicSprites(const NzSceneData& sceneData) cons
do do
{ {
// On ouvre le buffer en écriture // On ouvre le buffer en écriture
NzBufferMapper<NzVertexBuffer> vertexMapper(m_spriteBuffer, nzBufferAccess_DiscardAndWrite); BufferMapper<VertexBuffer> vertexMapper(m_spriteBuffer, BufferAccess_DiscardAndWrite);
NzVertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<NzVertexStruct_XYZ_Color_UV*>(vertexMapper.GetPointer()); VertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<VertexStruct_XYZ_Color_UV*>(vertexMapper.GetPointer());
unsigned int spriteCount = 0; unsigned int spriteCount = 0;
unsigned int maxSpriteCount = std::min(s_maxQuads, m_spriteBuffer.GetVertexCount()/4); unsigned int maxSpriteCount = std::min(s_maxQuads, m_spriteBuffer.GetVertexCount()/4);
do do
{ {
NzForwardRenderQueue::SpriteChain_XYZ_Color_UV& currentChain = spriteChainVector[spriteChain]; ForwardRenderQueue::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(VertexStruct_XYZ_Color_UV));
vertices += count*4; vertices += count*4;
spriteCount += count; spriteCount += count;
@ -234,7 +244,7 @@ void NzDepthRenderTechnique::DrawBasicSprites(const NzSceneData& sceneData) cons
vertexMapper.Unmap(); vertexMapper.Unmap();
NzRenderer::DrawIndexedPrimitives(nzPrimitiveMode_TriangleList, 0, spriteCount*6); Renderer::DrawIndexedPrimitives(PrimitiveMode_TriangleList, 0, spriteCount*6);
} }
while (spriteChain < spriteChainCount); while (spriteChain < spriteChainCount);
@ -246,32 +256,49 @@ void NzDepthRenderTechnique::DrawBasicSprites(const NzSceneData& sceneData) cons
matEntry.enabled = false; matEntry.enabled = false;
} }
} }
} }
void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const void DepthRenderTechnique::DrawBillboards(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const
{
NazaraUnused(sceneData);
if (NzRenderer::HasCapability(nzRendererCap_Instancing))
{ {
NzVertexBuffer* instanceBuffer = NzRenderer::GetInstanceBuffer(); NazaraAssert(sceneData.viewer, "Invalid viewer");
const Shader* lastShader = nullptr;
const ShaderUniforms* shaderUniforms = nullptr;
if (Renderer::HasCapability(RendererCap_Instancing))
{
VertexBuffer* instanceBuffer = Renderer::GetInstanceBuffer();
instanceBuffer->SetVertexDeclaration(&s_billboardInstanceDeclaration); instanceBuffer->SetVertexDeclaration(&s_billboardInstanceDeclaration);
NzRenderer::SetVertexBuffer(&s_quadVertexBuffer); Renderer::SetVertexBuffer(&s_quadVertexBuffer);
for (auto& matIt : m_renderQueue.billboards) for (auto& matIt : layer.billboards)
{ {
const NzMaterial* material = matIt.first; const Material* material = matIt.first;
auto& entry = matIt.second; auto& entry = matIt.second;
auto& billboardVector = entry.billboards; auto& billboardVector = entry.billboards;
unsigned int billboardCount = billboardVector.size(); unsigned int billboardCount = billboardVector.size();
if (billboardCount > 0) if (billboardCount > 0)
{ {
// On commence par appliquer du matériau // On commence par appliquer du matériau (et récupérer le shader ainsi activé)
material->Apply(nzShaderFlags_Billboard | nzShaderFlags_Instancing | nzShaderFlags_VertexColor); const Shader* shader = material->Apply(ShaderFlags_Billboard | ShaderFlags_Instancing | ShaderFlags_VertexColor);
const NzForwardRenderQueue::BillboardData* data = &billboardVector[0]; // 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);
// Couleur ambiante de la scène
shader->SendColor(shaderUniforms->sceneAmbient, sceneData.ambientColor);
// Position de la caméra
shader->SendVector(shaderUniforms->eyePosition, sceneData.viewer->GetEyePosition());
lastShader = shader;
}
const ForwardRenderQueue::BillboardData* data = &billboardVector[0];
unsigned int maxBillboardPerDraw = instanceBuffer->GetVertexCount(); unsigned int maxBillboardPerDraw = instanceBuffer->GetVertexCount();
do do
{ {
@ -281,7 +308,7 @@ void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const
instanceBuffer->Fill(data, 0, renderedBillboardCount, true); instanceBuffer->Fill(data, 0, renderedBillboardCount, true);
data += renderedBillboardCount; data += renderedBillboardCount;
NzRenderer::DrawPrimitivesInstanced(renderedBillboardCount, nzPrimitiveMode_TriangleStrip, 0, 4); Renderer::DrawPrimitivesInstanced(renderedBillboardCount, PrimitiveMode_TriangleStrip, 0, 4);
} }
while (billboardCount > 0); while (billboardCount > 0);
@ -291,22 +318,36 @@ void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const
} }
else else
{ {
NzRenderer::SetIndexBuffer(&s_quadIndexBuffer); Renderer::SetIndexBuffer(&s_quadIndexBuffer);
NzRenderer::SetVertexBuffer(&m_billboardPointBuffer); Renderer::SetVertexBuffer(&m_billboardPointBuffer);
for (auto& matIt : m_renderQueue.billboards) for (auto& matIt : layer.billboards)
{ {
const NzMaterial* material = matIt.first; const Material* material = matIt.first;
auto& entry = matIt.second; auto& entry = matIt.second;
auto& billboardVector = entry.billboards; auto& billboardVector = entry.billboards;
unsigned int billboardCount = billboardVector.size(); unsigned int billboardCount = billboardVector.size();
if (billboardCount > 0) if (billboardCount > 0)
{ {
// On commence par appliquer du matériau // On commence par appliquer du matériau (et récupérer le shader ainsi activé)
material->Apply(nzShaderFlags_Billboard | nzShaderFlags_VertexColor); const Shader* shader = material->Apply(ShaderFlags_Billboard | ShaderFlags_VertexColor);
const NzForwardRenderQueue::BillboardData* data = &billboardVector[0]; // 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);
// Couleur ambiante de la scène
shader->SendColor(shaderUniforms->sceneAmbient, sceneData.ambientColor);
// Position de la caméra
shader->SendVector(shaderUniforms->eyePosition, sceneData.viewer->GetEyePosition());
lastShader = shader;
}
const ForwardRenderQueue::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
@ -314,12 +355,12 @@ void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const
unsigned int renderedBillboardCount = std::min(billboardCount, maxBillboardPerDraw); unsigned int renderedBillboardCount = std::min(billboardCount, maxBillboardPerDraw);
billboardCount -= renderedBillboardCount; billboardCount -= renderedBillboardCount;
NzBufferMapper<NzVertexBuffer> vertexMapper(m_billboardPointBuffer, nzBufferAccess_DiscardAndWrite, 0, renderedBillboardCount*4); BufferMapper<VertexBuffer> vertexMapper(m_billboardPointBuffer, BufferAccess_DiscardAndWrite, 0, renderedBillboardCount*4);
BillboardPoint* vertices = reinterpret_cast<BillboardPoint*>(vertexMapper.GetPointer()); BillboardPoint* vertices = reinterpret_cast<BillboardPoint*>(vertexMapper.GetPointer());
for (unsigned int i = 0; i < renderedBillboardCount; ++i) for (unsigned int i = 0; i < renderedBillboardCount; ++i)
{ {
const NzForwardRenderQueue::BillboardData& billboard = *data++; const ForwardRenderQueue::BillboardData& billboard = *data++;
vertices->color = billboard.color; vertices->color = billboard.color;
vertices->position = billboard.center; vertices->position = billboard.center;
@ -352,7 +393,7 @@ void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const
vertexMapper.Unmap(); vertexMapper.Unmap();
NzRenderer::DrawIndexedPrimitives(nzPrimitiveMode_TriangleList, 0, renderedBillboardCount*6); Renderer::DrawIndexedPrimitives(PrimitiveMode_TriangleList, 0, renderedBillboardCount*6);
} }
while (billboardCount > 0); while (billboardCount > 0);
@ -360,73 +401,83 @@ void NzDepthRenderTechnique::DrawBillboards(const NzSceneData& sceneData) const
} }
} }
} }
} }
void NzDepthRenderTechnique::DrawOpaqueModels(const NzSceneData& sceneData) const void DepthRenderTechnique::DrawOpaqueModels(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const
{ {
NazaraUnused(sceneData); NazaraAssert(sceneData.viewer, "Invalid viewer");
for (auto& matIt : m_renderQueue.opaqueModels) const Shader* lastShader = nullptr;
const ShaderUniforms* shaderUniforms = nullptr;
for (auto& matIt : layer.opaqueModels)
{ {
auto& matEntry = matIt.second; auto& matEntry = matIt.second;
if (matEntry.enabled) if (matEntry.enabled)
{ {
NzForwardRenderQueue::MeshInstanceContainer& meshInstances = matEntry.meshMap; ForwardRenderQueue::MeshInstanceContainer& meshInstances = matEntry.meshMap;
if (!meshInstances.empty()) if (!meshInstances.empty())
{ {
const NzMaterial* material = matIt.first; const Material* material = matIt.first;
// Nous utilisons de l'instancing que lorsqu'aucune lumière (autre que directionnelle) n'est active bool instancing = m_instancingEnabled && matEntry.instancingEnabled;
// 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é) // On commence par appliquer du matériau (et récupérer le shader ainsi activé)
material->Apply((instancing) ? nzShaderFlags_Instancing : 0); UInt8 freeTextureUnit;
const Shader* shader = material->Apply((instancing) ? ShaderFlags_Instancing : 0, 0, &freeTextureUnit);
// 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);
lastShader = shader;
}
// Meshes // Meshes
for (auto& meshIt : meshInstances) for (auto& meshIt : meshInstances)
{ {
const NzMeshData& meshData = meshIt.first; const MeshData& meshData = meshIt.first;
auto& meshEntry = meshIt.second; auto& meshEntry = meshIt.second;
std::vector<NzMatrix4f>& instances = meshEntry.instances; const Spheref& squaredBoundingSphere = meshEntry.squaredBoundingSphere;
std::vector<Matrix4f>& instances = meshEntry.instances;
if (!instances.empty()) if (!instances.empty())
{ {
const NzIndexBuffer* indexBuffer = meshData.indexBuffer; const IndexBuffer* indexBuffer = meshData.indexBuffer;
const NzVertexBuffer* vertexBuffer = meshData.vertexBuffer; const VertexBuffer* vertexBuffer = meshData.vertexBuffer;
// Gestion du draw call avant la boucle de rendu // Gestion du draw call avant la boucle de rendu
NzRenderer::DrawCall drawFunc; Renderer::DrawCall drawFunc;
NzRenderer::DrawCallInstanced instancedDrawFunc; Renderer::DrawCallInstanced instancedDrawFunc;
unsigned int indexCount; unsigned int indexCount;
if (indexBuffer) if (indexBuffer)
{ {
drawFunc = NzRenderer::DrawIndexedPrimitives; drawFunc = Renderer::DrawIndexedPrimitives;
instancedDrawFunc = NzRenderer::DrawIndexedPrimitivesInstanced; instancedDrawFunc = Renderer::DrawIndexedPrimitivesInstanced;
indexCount = indexBuffer->GetIndexCount(); indexCount = indexBuffer->GetIndexCount();
} }
else else
{ {
drawFunc = NzRenderer::DrawPrimitives; drawFunc = Renderer::DrawPrimitives;
instancedDrawFunc = NzRenderer::DrawPrimitivesInstanced; instancedDrawFunc = Renderer::DrawPrimitivesInstanced;
indexCount = vertexBuffer->GetVertexCount(); indexCount = vertexBuffer->GetVertexCount();
} }
NzRenderer::SetIndexBuffer(indexBuffer); Renderer::SetIndexBuffer(indexBuffer);
NzRenderer::SetVertexBuffer(vertexBuffer); Renderer::SetVertexBuffer(vertexBuffer);
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) // 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(); VertexBuffer* instanceBuffer = Renderer::GetInstanceBuffer();
instanceBuffer->SetVertexDeclaration(NzVertexDeclaration::Get(nzVertexLayout_Matrix4)); instanceBuffer->SetVertexDeclaration(VertexDeclaration::Get(VertexLayout_Matrix4));
const NzMatrix4f* instanceMatrices = &instances[0]; const Matrix4f* instanceMatrices = &instances[0];
unsigned int instanceCount = instances.size(); unsigned int instanceCount = instances.size();
unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // Le nombre maximum d'instances en une fois unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // Le nombre maximum d'instances en une fois
@ -449,9 +500,9 @@ void NzDepthRenderTechnique::DrawOpaqueModels(const NzSceneData& sceneData) cons
// 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
for (const NzMatrix4f& matrix : instances) for (const Matrix4f& matrix : instances)
{ {
NzRenderer::SetMatrix(nzMatrixType_World, matrix); Renderer::SetMatrix(MatrixType_World, matrix);
drawFunc(meshData.primitiveMode, 0, indexCount); drawFunc(meshData.primitiveMode, 0, indexCount);
} }
} }
@ -465,31 +516,34 @@ void NzDepthRenderTechnique::DrawOpaqueModels(const NzSceneData& sceneData) cons
matEntry.instancingEnabled = false; matEntry.instancingEnabled = false;
} }
} }
} }
const NzDepthRenderTechnique::ShaderUniforms* NzDepthRenderTechnique::GetShaderUniforms(const NzShader* shader) const const DepthRenderTechnique::ShaderUniforms* DepthRenderTechnique::GetShaderUniforms(const Shader* shader) const
{ {
auto it = m_shaderUniforms.find(shader); auto it = m_shaderUniforms.find(shader);
if (it == m_shaderUniforms.end()) if (it == m_shaderUniforms.end())
{ {
ShaderUniforms uniforms; ShaderUniforms uniforms;
uniforms.shaderReleaseSlot.Connect(shader->OnShaderRelease, this, &NzDepthRenderTechnique::OnShaderInvalidated); uniforms.shaderReleaseSlot.Connect(shader->OnShaderRelease, this, &DepthRenderTechnique::OnShaderInvalidated);
uniforms.shaderUniformInvalidatedSlot.Connect(shader->OnShaderUniformInvalidated, this, &NzDepthRenderTechnique::OnShaderInvalidated); uniforms.shaderUniformInvalidatedSlot.Connect(shader->OnShaderUniformInvalidated, this, &DepthRenderTechnique::OnShaderInvalidated);
uniforms.eyePosition = shader->GetUniformLocation("EyePosition");
uniforms.sceneAmbient = shader->GetUniformLocation("SceneAmbient");
uniforms.textureOverlay = shader->GetUniformLocation("TextureOverlay"); uniforms.textureOverlay = shader->GetUniformLocation("TextureOverlay");
it = m_shaderUniforms.emplace(shader, std::move(uniforms)).first; it = m_shaderUniforms.emplace(shader, std::move(uniforms)).first;
} }
return &it->second; return &it->second;
} }
void NzDepthRenderTechnique::OnShaderInvalidated(const NzShader* shader) const void DepthRenderTechnique::OnShaderInvalidated(const Shader* shader) const
{ {
m_shaderUniforms.erase(shader); m_shaderUniforms.erase(shader);
} }
NzIndexBuffer NzDepthRenderTechnique::s_quadIndexBuffer; IndexBuffer DepthRenderTechnique::s_quadIndexBuffer;
NzVertexBuffer NzDepthRenderTechnique::s_quadVertexBuffer; VertexBuffer DepthRenderTechnique::s_quadVertexBuffer;
NzVertexDeclaration NzDepthRenderTechnique::s_billboardInstanceDeclaration; VertexDeclaration DepthRenderTechnique::s_billboardInstanceDeclaration;
NzVertexDeclaration NzDepthRenderTechnique::s_billboardVertexDeclaration; VertexDeclaration DepthRenderTechnique::s_billboardVertexDeclaration;
}