diff --git a/include/Nazara/Renderer/Enums.hpp b/include/Nazara/Renderer/Enums.hpp index 705daf6a0..31fac2fe5 100644 --- a/include/Nazara/Renderer/Enums.hpp +++ b/include/Nazara/Renderer/Enums.hpp @@ -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, diff --git a/include/Nazara/Renderer/Shader.hpp b/include/Nazara/Renderer/Shader.hpp index 4e6aa45dc..0ba93cee8 100644 --- a/include/Nazara/Renderer/Shader.hpp +++ b/include/Nazara/Renderer/Shader.hpp @@ -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 diff --git a/src/Nazara/Graphics/Scene.cpp b/src/Nazara/Graphics/Scene.cpp index 5296ea0bd..8645e487f 100644 --- a/src/Nazara/Graphics/Scene.cpp +++ b/src/Nazara/Graphics/Scene.cpp @@ -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) diff --git a/src/Nazara/Renderer/DebugDrawer.cpp b/src/Nazara/Renderer/DebugDrawer.cpp index 9ca4a1f4a..843d5e58e 100644 --- a/src/Nazara/Renderer/DebugDrawer.cpp +++ b/src/Nazara/Renderer/DebugDrawer.cpp @@ -574,7 +574,7 @@ bool NzDebugDrawer::Initialize() return false; } - colorLocation = shader->GetUniformLocation("MaterialDiffuse"); + colorLocation = shader->GetUniformLocation(nzShaderUniform_MaterialDiffuse); } // VertexDeclaration diff --git a/src/Nazara/Renderer/GLSLShader.cpp b/src/Nazara/Renderer/GLSLShader.cpp index 83ea6a22b..a3051ea20 100644 --- a/src/Nazara/Renderer/GLSLShader.cpp +++ b/src/Nazara/Renderer/GLSLShader.cpp @@ -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; diff --git a/src/Nazara/Renderer/GLSLShader.hpp b/src/Nazara/Renderer/GLSLShader.hpp index f1797652f..38ed20eb3 100644 --- a/src/Nazara/Renderer/GLSLShader.hpp +++ b/src/Nazara/Renderer/GLSLShader.hpp @@ -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 diff --git a/src/Nazara/Renderer/Material.cpp b/src/Nazara/Renderer/Material.cpp index fa2487fb8..436784dcc 100644 --- a/src/Nazara/Renderer/Material.cpp +++ b/src/Nazara/Renderer/Material.cpp @@ -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; diff --git a/src/Nazara/Renderer/Renderer.cpp b/src/Nazara/Renderer/Renderer.cpp index f9fdb4517..9cbde2d19 100644 --- a/src/Nazara/Renderer/Renderer.cpp +++ b/src/Nazara/Renderer/Renderer.cpp @@ -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) diff --git a/src/Nazara/Renderer/Shader.cpp b/src/Nazara/Renderer/Shader.cpp index 4c4e637dc..e4297a85b 100644 --- a/src/Nazara/Renderer/Shader.cpp +++ b/src/Nazara/Renderer/Shader.cpp @@ -14,13 +14,25 @@ #include #include -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 diff --git a/src/Nazara/Renderer/ShaderImpl.hpp b/src/Nazara/Renderer/ShaderImpl.hpp index dc4a96cf0..573b7c160 100644 --- a/src/Nazara/Renderer/ShaderImpl.hpp +++ b/src/Nazara/Renderer/ShaderImpl.hpp @@ -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;