Added ShaderUniform (Faster GetUniformLocation)

Former-commit-id: c8a01eccde07fc3b2aac46c5b974f8406ad949f0
This commit is contained in:
Lynix 2013-05-11 14:03:22 +02:00
parent 991d841d57
commit 28f46a5c90
10 changed files with 106 additions and 28 deletions

View File

@ -170,6 +170,30 @@ enum nzShaderLanguage
nzShaderLanguage_Max = nzShaderLanguage_GLSL
};
enum nzShaderUniform
{
nzShaderUniform_CameraPosition,
nzShaderUniform_LightCount,
nzShaderUniform_MaterialAmbient,
nzShaderUniform_MaterialDiffuse,
nzShaderUniform_MaterialDiffuseMap,
nzShaderUniform_MaterialEmissiveMap,
nzShaderUniform_MaterialHeightMap,
nzShaderUniform_MaterialNormalMap,
nzShaderUniform_MaterialShininess,
nzShaderUniform_MaterialSpecular,
nzShaderUniform_MaterialSpecularMap,
nzShaderUniform_ProjMatrix,
nzShaderUniform_SceneAmbient,
nzShaderUniform_ViewMatrix,
nzShaderUniform_ViewProjMatrix,
nzShaderUniform_WorldMatrix,
nzShaderUniform_WorldViewMatrix,
nzShaderUniform_WorldViewProjMatrix,
nzShaderUniform_Max = nzShaderUniform_WorldViewProjMatrix
};
enum nzShaderType
{
nzShaderType_Fragment,

View File

@ -32,7 +32,7 @@ class NAZARA_API NzShader : public NzResource, NzNonCopyable
friend class NzRenderer;
public:
NzShader() = default;
NzShader();
NzShader(nzShaderLanguage language);
NzShader(NzShader&& shader);
~NzShader();
@ -47,6 +47,7 @@ class NAZARA_API NzShader : public NzResource, NzNonCopyable
nzShaderLanguage GetLanguage() const;
NzString GetSourceCode(nzShaderType type) const;
int GetUniformLocation(const NzString& name) const;
int GetUniformLocation(nzShaderUniform uniform) const;
bool HasUniform(const NzString& name) const;
@ -80,9 +81,9 @@ class NAZARA_API NzShader : public NzResource, NzNonCopyable
static bool IsTypeSupported(nzShaderType type);
private:
nzUInt32 m_flags = nzShaderFlags_None;
NzShaderImpl* m_impl = nullptr;
bool m_compiled = false;
nzUInt32 m_flags;
NzShaderImpl* m_impl;
bool m_compiled;
};
#endif // NAZARA_SHADER_HPP

View File

@ -125,17 +125,17 @@ void NzScene::Draw()
matIt.first->Apply(shader);
// Position de la caméra
int camPosLocation = shader->GetUniformLocation("CameraPosition");
int camPosLocation = shader->GetUniformLocation(nzShaderUniform_CameraPosition);
if (camPosLocation != -1)
shader->SendVector(camPosLocation, m_impl->activeCamera->GetPosition());
// Couleur ambiante de la scène
int sceneAmbientLocation = shader->GetUniformLocation("SceneAmbient");
int sceneAmbientLocation = shader->GetUniformLocation(nzShaderUniform_SceneAmbient);
if (sceneAmbientLocation != -1)
shader->SendColor(sceneAmbientLocation, m_impl->ambientColor);
// Gestion des lumières (D'abord directionnelles)
int lightCountLocation = shader->GetUniformLocation("LightCount");
int lightCountLocation = shader->GetUniformLocation(nzShaderUniform_LightCount);
unsigned int lightIndex = 0;
if (lightCountLocation != -1)
@ -216,17 +216,17 @@ void NzScene::Draw()
bool instancing = shader->GetFlags() & nzShaderFlags_Instancing;
// Position de la caméra
int camPosLocation = shader->GetUniformLocation("CameraPosition");
int camPosLocation = shader->GetUniformLocation(nzShaderUniform_CameraPosition);
if (camPosLocation != -1)
shader->SendVector(camPosLocation, m_impl->activeCamera->GetPosition());
// Couleur ambiante de la scène
int sceneAmbientLocation = shader->GetUniformLocation("SceneAmbient");
int sceneAmbientLocation = shader->GetUniformLocation(nzShaderUniform_SceneAmbient);
if (sceneAmbientLocation != -1)
shader->SendColor(sceneAmbientLocation, m_impl->ambientColor);
// Gestion des lumières (D'abord directionnelles)
int lightCountLocation = shader->GetUniformLocation("LightCount");
int lightCountLocation = shader->GetUniformLocation(nzShaderUniform_LightCount);
unsigned int lightIndex = 0;
if (lightCountLocation != -1)

View File

@ -574,7 +574,7 @@ bool NzDebugDrawer::Initialize()
return false;
}
colorLocation = shader->GetUniformLocation("MaterialDiffuse");
colorLocation = shader->GetUniformLocation(nzShaderUniform_MaterialDiffuse);
}
// VertexDeclaration

View File

@ -61,6 +61,25 @@ bool NzGLSLShader::Compile()
static NzString successStr("Linkage successful");
m_log = successStr;
m_uniformLocations[nzShaderUniform_CameraPosition] = GetUniformLocation("CameraPosition");
m_uniformLocations[nzShaderUniform_LightCount] = GetUniformLocation("LightCount");
m_uniformLocations[nzShaderUniform_MaterialAmbient] = GetUniformLocation("MaterialAmbient");
m_uniformLocations[nzShaderUniform_MaterialDiffuse] = GetUniformLocation("MaterialDiffuse");
m_uniformLocations[nzShaderUniform_MaterialDiffuseMap] = GetUniformLocation("MaterialDiffuseMap");
m_uniformLocations[nzShaderUniform_MaterialEmissiveMap] = GetUniformLocation("MaterialEmissiveMap");
m_uniformLocations[nzShaderUniform_MaterialHeightMap] = GetUniformLocation("MaterialHeightMap");
m_uniformLocations[nzShaderUniform_MaterialNormalMap] = GetUniformLocation("MaterialNormalMap");
m_uniformLocations[nzShaderUniform_MaterialShininess] = GetUniformLocation("MaterialShininess");
m_uniformLocations[nzShaderUniform_MaterialSpecular] = GetUniformLocation("MaterialSpecular");
m_uniformLocations[nzShaderUniform_MaterialSpecularMap] = GetUniformLocation("MaterialSpecularMap");
m_uniformLocations[nzShaderUniform_ProjMatrix] = GetUniformLocation("ProjMatrix");
m_uniformLocations[nzShaderUniform_SceneAmbient] = GetUniformLocation("SceneAmbient");
m_uniformLocations[nzShaderUniform_ViewMatrix] = GetUniformLocation("ViewMatrix");
m_uniformLocations[nzShaderUniform_ViewProjMatrix] = GetUniformLocation("ViewProjMatrix");
m_uniformLocations[nzShaderUniform_WorldMatrix] = GetUniformLocation("WorldMatrix");
m_uniformLocations[nzShaderUniform_WorldViewMatrix] = GetUniformLocation("WorldViewMatrix");
m_uniformLocations[nzShaderUniform_WorldViewProjMatrix] = GetUniformLocation("WorldViewProjMatrix");
return true;
}
else
@ -193,6 +212,11 @@ int NzGLSLShader::GetUniformLocation(const NzString& name) const
return id;
}
int NzGLSLShader::GetUniformLocation(nzShaderUniform uniform) const
{
return m_uniformLocations[uniform];
}
bool NzGLSLShader::IsLoaded(nzShaderType type) const
{
return m_shaders[type] != 0;

View File

@ -34,6 +34,7 @@ class NzGLSLShader : public NzShaderImpl, NzResourceListener
nzShaderLanguage GetLanguage() const;
NzString GetSourceCode(nzShaderType type) const;
int GetUniformLocation(const NzString& name) const;
int GetUniformLocation(nzShaderUniform uniform) const;
bool IsLoaded(nzShaderType type) const;
@ -73,6 +74,7 @@ class NzGLSLShader : public NzShaderImpl, NzResourceListener
GLuint m_shaders[nzShaderType_Max+1];
NzShader* m_parent;
NzString m_log;
int m_uniformLocations[nzShaderUniform_Max+1];
};
#endif // NAZARA_GLSLSHADER_HPPs

View File

@ -41,10 +41,10 @@ NzMaterial::NzMaterial(NzMaterial&& material)
void NzMaterial::Apply(const NzShader* shader) const
{
int ambientColorLocation = shader->GetUniformLocation("MaterialAmbient");
int diffuseColorLocation = shader->GetUniformLocation("MaterialDiffuse");
int shininessLocation = shader->GetUniformLocation("MaterialShininess");
int specularColorLocation = shader->GetUniformLocation("MaterialSpecular");
int ambientColorLocation = shader->GetUniformLocation(nzShaderUniform_MaterialAmbient);
int diffuseColorLocation = shader->GetUniformLocation(nzShaderUniform_MaterialDiffuse);
int shininessLocation = shader->GetUniformLocation(nzShaderUniform_MaterialShininess);
int specularColorLocation = shader->GetUniformLocation(nzShaderUniform_MaterialSpecular);
if (ambientColorLocation != -1)
shader->SendColor(ambientColorLocation, m_ambientColor);
@ -54,7 +54,7 @@ void NzMaterial::Apply(const NzShader* shader) const
if (m_diffuseMap)
{
int diffuseMapLocation = shader->GetUniformLocation("MaterialDiffuseMap");
int diffuseMapLocation = shader->GetUniformLocation(nzShaderUniform_MaterialDiffuseMap);
if (diffuseMapLocation != -1)
{
nzUInt8 textureUnit;
@ -67,7 +67,7 @@ void NzMaterial::Apply(const NzShader* shader) const
if (m_emissiveMap)
{
int emissiveMapLocation = shader->GetUniformLocation("MaterialEmissiveMap");
int emissiveMapLocation = shader->GetUniformLocation(nzShaderUniform_MaterialEmissiveMap);
if (emissiveMapLocation != -1)
{
nzUInt8 textureUnit;
@ -80,7 +80,7 @@ void NzMaterial::Apply(const NzShader* shader) const
if (m_heightMap)
{
int heightMapLocation = shader->GetUniformLocation("MaterialHeightMap");
int heightMapLocation = shader->GetUniformLocation(nzShaderUniform_MaterialHeightMap);
if (heightMapLocation != -1)
{
nzUInt8 textureUnit;
@ -93,7 +93,7 @@ void NzMaterial::Apply(const NzShader* shader) const
if (m_normalMap)
{
int normalMapLocation = shader->GetUniformLocation("MaterialNormalMap");
int normalMapLocation = shader->GetUniformLocation(nzShaderUniform_MaterialNormalMap);
if (normalMapLocation != -1)
{
nzUInt8 textureUnit;
@ -112,7 +112,7 @@ void NzMaterial::Apply(const NzShader* shader) const
if (m_specularMap)
{
int specularMapLocation = shader->GetUniformLocation("MaterialSpecularMap");
int specularMapLocation = shader->GetUniformLocation(nzShaderUniform_MaterialSpecularMap);
if (specularMapLocation != -1)
{
nzUInt8 textureUnit;

View File

@ -1451,13 +1451,13 @@ bool NzRenderer::EnsureStateUpdate()
shaderImpl->BindTextures();
// Récupération des indices des variables uniformes (-1 si la variable n'existe pas)
s_matrixLocation[nzMatrixType_Projection] = shaderImpl->GetUniformLocation("ProjMatrix");
s_matrixLocation[nzMatrixType_View] = shaderImpl->GetUniformLocation("ViewMatrix");
s_matrixLocation[nzMatrixType_World] = shaderImpl->GetUniformLocation("WorldMatrix");
s_matrixLocation[nzMatrixType_Projection] = shaderImpl->GetUniformLocation(nzShaderUniform_ProjMatrix);
s_matrixLocation[nzMatrixType_View] = shaderImpl->GetUniformLocation(nzShaderUniform_ViewMatrix);
s_matrixLocation[nzMatrixType_World] = shaderImpl->GetUniformLocation(nzShaderUniform_WorldMatrix);
s_matrixLocation[nzMatrixCombination_ViewProj] = shaderImpl->GetUniformLocation("ViewProjMatrix");
s_matrixLocation[nzMatrixCombination_WorldView] = shaderImpl->GetUniformLocation("WorldViewMatrix");
s_matrixLocation[nzMatrixCombination_WorldViewProj] = shaderImpl->GetUniformLocation("WorldViewProjMatrix");
s_matrixLocation[nzMatrixCombination_ViewProj] = shaderImpl->GetUniformLocation(nzShaderUniform_ViewProjMatrix);
s_matrixLocation[nzMatrixCombination_WorldView] = shaderImpl->GetUniformLocation(nzShaderUniform_WorldViewMatrix);
s_matrixLocation[nzMatrixCombination_WorldViewProj] = shaderImpl->GetUniformLocation(nzShaderUniform_WorldViewProjMatrix);
s_updateFlags |= Update_Matrices;
for (unsigned int i = 0; i < totalMatrixCount; ++i)

View File

@ -14,13 +14,25 @@
#include <stdexcept>
#include <Nazara/Renderer/Debug.hpp>
NzShader::NzShader(nzShaderLanguage language)
NzShader::NzShader() :
m_flags(nzShaderFlags_None),
m_impl(nullptr),
m_compiled(false)
{
}
NzShader::NzShader(nzShaderLanguage language) :
m_flags(nzShaderFlags_None),
m_impl(nullptr),
m_compiled(false)
{
Create(language);
}
NzShader::NzShader(NzShader&& shader) :
m_impl(shader.m_impl)
m_flags(shader.m_flags),
m_impl(shader.m_impl),
m_compiled(shader.m_compiled)
{
shader.m_impl = nullptr;
}
@ -75,6 +87,7 @@ bool NzShader::Compile()
if (m_impl->Compile())
{
m_compiled = true;
return true;
}
else
@ -172,6 +185,19 @@ int NzShader::GetUniformLocation(const NzString& name) const
return m_impl->GetUniformLocation(name);
}
int NzShader::GetUniformLocation(nzShaderUniform uniform) const
{
#ifdef NAZARA_DEBUG
if (uniform > nzShaderUniform_Max)
{
NazaraError("Shader uniform out of enum");
return -1;
}
#endif
return m_impl->GetUniformLocation(uniform);
}
bool NzShader::HasUniform(const NzString& name) const
{
#if NAZARA_RENDERER_SAFE

View File

@ -29,6 +29,7 @@ class NzShaderImpl
virtual nzShaderLanguage GetLanguage() const = 0;
virtual NzString GetSourceCode(nzShaderType type) const = 0;
virtual int GetUniformLocation(const NzString& name) const = 0;
virtual int GetUniformLocation(nzShaderUniform uniform) const = 0;
virtual bool IsLoaded(nzShaderType type) const = 0;