Graphics/Shader: Make texture units statics

Provides better performances and prevents the sampler type bug to happen
This commit is contained in:
Lynix
2016-12-18 01:13:46 +01:00
parent 1a8805aad1
commit 682dab32b3
10 changed files with 203 additions and 183 deletions

View File

@@ -148,6 +148,26 @@ namespace Nz
ShaderFlags_Max = ShaderFlags_VertexColor * 2 - 1
};
enum TextureMap
{
TextureMap_Alpha,
TextureMap_Diffuse,
TextureMap_Emissive,
TextureMap_Height,
TextureMap_ReflectionCube,
TextureMap_Normal,
TextureMap_Overlay,
TextureMap_Shadow2D_1,
TextureMap_Shadow2D_2,
TextureMap_Shadow2D_3,
TextureMap_ShadowCube_1,
TextureMap_ShadowCube_2,
TextureMap_ShadowCube_3,
TextureMap_Specular,
TextureMap_Max = TextureMap_Specular
};
}
#endif // NAZARA_ENUMS_GRAPHICS_HPP

View File

@@ -46,7 +46,7 @@ namespace Nz
void DrawTransparentModels(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const;
const ShaderUniforms* GetShaderUniforms(const Shader* shader) const;
void OnShaderInvalidated(const Shader* shader) const;
void SendLightUniforms(const Shader* shader, const LightUniforms& uniforms, unsigned int index, unsigned int uniformOffset, UInt8 availableTextureUnit) const;
void SendLightUniforms(const Shader* shader, const LightUniforms& uniforms, unsigned int index, unsigned int lightIndex, unsigned int uniformOffset) const;
static float ComputeDirectionalLightScore(const Spheref& object, const AbstractRenderQueue::DirectionalLight& light);
static float ComputePointLightScore(const Spheref& object, const AbstractRenderQueue::PointLight& light);

View File

@@ -6,135 +6,6 @@
namespace Nz
{
/*!
* \brief Sens the uniforms for light
*
* \param shader Shader to send uniforms to
* \param uniforms Uniforms to send
* \param index Index of the light
* \param uniformOffset Offset for the uniform
* \param availableTextureUnit Unit texture available
*/
inline void ForwardRenderTechnique::SendLightUniforms(const Shader* shader, const LightUniforms& uniforms, unsigned int index, unsigned int uniformOffset, UInt8 availableTextureUnit) const
{
// If anyone got a better idea..
int dummyCubemap = Renderer::GetMaxTextureUnits() - 1;
int dummyTexture = Renderer::GetMaxTextureUnits() - 2;
if (index < m_lights.size())
{
const LightIndex& lightIndex = m_lights[index];
shader->SendInteger(uniforms.locations.type + uniformOffset, lightIndex.type); //< Sends the light type
switch (lightIndex.type)
{
case LightType_Directional:
{
const auto& light = m_renderQueue.directionalLights[lightIndex.index];
shader->SendColor(uniforms.locations.color + uniformOffset, light.color);
shader->SendVector(uniforms.locations.factors + uniformOffset, Vector2f(light.ambientFactor, light.diffuseFactor));
shader->SendVector(uniforms.locations.parameters1 + uniformOffset, Vector4f(light.direction));
if (uniforms.locations.shadowMapping != -1)
shader->SendBoolean(uniforms.locations.shadowMapping + uniformOffset, light.shadowMap != nullptr);
if (light.shadowMap)
{
Renderer::SetTexture(availableTextureUnit, light.shadowMap);
Renderer::SetTextureSampler(availableTextureUnit, s_shadowSampler);
if (uniforms.locations.lightViewProjMatrix != -1)
shader->SendMatrix(uniforms.locations.lightViewProjMatrix + index, light.transformMatrix);
if (uniforms.locations.directionalSpotLightShadowMap != -1)
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, availableTextureUnit);
}
else if (uniforms.locations.directionalSpotLightShadowMap != -1)
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, dummyTexture);
if (uniforms.locations.directionalSpotLightShadowMap != -1)
shader->SendInteger(uniforms.locations.pointLightShadowMap + index, dummyCubemap);
break;
}
case LightType_Point:
{
const auto& light = m_renderQueue.pointLights[lightIndex.index];
shader->SendColor(uniforms.locations.color + uniformOffset, light.color);
shader->SendVector(uniforms.locations.factors + uniformOffset, Vector2f(light.ambientFactor, light.diffuseFactor));
shader->SendVector(uniforms.locations.parameters1 + uniformOffset, Vector4f(light.position, light.attenuation));
shader->SendVector(uniforms.locations.parameters2 + uniformOffset, Vector4f(0.f, 0.f, 0.f, light.invRadius));
if (uniforms.locations.shadowMapping != -1)
shader->SendBoolean(uniforms.locations.shadowMapping + uniformOffset, light.shadowMap != nullptr);
if (light.shadowMap)
{
Renderer::SetTexture(availableTextureUnit, light.shadowMap);
Renderer::SetTextureSampler(availableTextureUnit, s_shadowSampler);
if (uniforms.locations.pointLightShadowMap != -1)
shader->SendInteger(uniforms.locations.pointLightShadowMap + index, availableTextureUnit);
}
else if (uniforms.locations.pointLightShadowMap != -1)
shader->SendInteger(uniforms.locations.pointLightShadowMap + index, dummyCubemap);
if (uniforms.locations.directionalSpotLightShadowMap != -1)
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, dummyTexture);
break;
}
case LightType_Spot:
{
const auto& light = m_renderQueue.spotLights[lightIndex.index];
shader->SendColor(uniforms.locations.color + uniformOffset, light.color);
shader->SendVector(uniforms.locations.factors + uniformOffset, Vector2f(light.ambientFactor, light.diffuseFactor));
shader->SendVector(uniforms.locations.parameters1 + uniformOffset, Vector4f(light.position, light.attenuation));
shader->SendVector(uniforms.locations.parameters2 + uniformOffset, Vector4f(light.direction, light.invRadius));
shader->SendVector(uniforms.locations.parameters3 + uniformOffset, Vector2f(light.innerAngleCosine, light.outerAngleCosine));
if (uniforms.locations.shadowMapping != -1)
shader->SendBoolean(uniforms.locations.shadowMapping + uniformOffset, light.shadowMap != nullptr);
if (light.shadowMap)
{
Renderer::SetTexture(availableTextureUnit, light.shadowMap);
Renderer::SetTextureSampler(availableTextureUnit, s_shadowSampler);
if (uniforms.locations.lightViewProjMatrix != -1)
shader->SendMatrix(uniforms.locations.lightViewProjMatrix + index, light.transformMatrix);
if (uniforms.locations.directionalSpotLightShadowMap != -1)
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, availableTextureUnit);
}
else if (uniforms.locations.directionalSpotLightShadowMap != -1)
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, dummyTexture);
if (uniforms.locations.pointLightShadowMap != -1)
shader->SendInteger(uniforms.locations.pointLightShadowMap + index, dummyCubemap);
break;
}
}
}
else
{
if (uniforms.locations.type != -1)
shader->SendInteger(uniforms.locations.type + uniformOffset, -1); //< Disable the light in the shader
if (uniforms.locations.directionalSpotLightShadowMap != -1)
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, dummyTexture);
if (uniforms.locations.pointLightShadowMap != -1)
shader->SendInteger(uniforms.locations.pointLightShadowMap + index, dummyCubemap);
}
}
/*!
* \brief Computes the score for directional light
* \return 0.f

View File

@@ -102,13 +102,11 @@ namespace Nz
{
int type;
int color;
int directionalSpotLightShadowMap;
int factors;
int lightViewProjMatrix;
int parameters1;
int parameters2;
int parameters3;
int pointLightShadowMap;
int shadowMapping;
};

View File

@@ -64,7 +64,7 @@ namespace Nz
inline Material(const Material& material);
inline ~Material();
void Apply(const MaterialPipeline::Instance& instance, UInt8 textureUnit = 0, UInt8* lastUsedUnit = nullptr) const;
void Apply(const MaterialPipeline::Instance& instance) const;
void BuildFromParameters(const ParameterList& matData, const MaterialParams& matParams = MaterialParams());
@@ -174,6 +174,7 @@ namespace Nz
inline Material& operator=(const Material& material);
inline static MaterialRef GetDefault();
inline static int GetTextureUnit(TextureMap textureMap);
template<typename... Args> static MaterialRef New(Args&&... args);
// Signals:
@@ -207,6 +208,7 @@ namespace Nz
float m_alphaThreshold;
float m_shininess;
static std::array<int, TextureMap_Max + 1> s_textureUnits;
static MaterialLibrary::LibraryMap s_library;
static MaterialLoader::LoaderList s_loaders;
static MaterialManager::ManagerMap s_managerMap;

View File

@@ -1356,6 +1356,11 @@ namespace Nz
return s_defaultMaterial;
}
inline int Material::GetTextureUnit(TextureMap textureMap)
{
return s_textureUnits[textureMap];
}
inline void Material::InvalidatePipeline()
{
m_pipelineUpdated = false;