Added automatic uniforms: (Inv)TargetSize

Former-commit-id: 382351590abe98dc9ef8f6ab4ecb30c8b42806a4
This commit is contained in:
Lynix 2013-07-09 00:54:17 +02:00
parent b035852576
commit b31771471a
3 changed files with 30 additions and 11 deletions

View File

@ -181,6 +181,7 @@ enum nzShaderLanguage
enum nzShaderUniform enum nzShaderUniform
{ {
nzShaderUniform_CameraPosition, nzShaderUniform_CameraPosition,
nzShaderUniform_InvTargetSize,
nzShaderUniform_LightCount, nzShaderUniform_LightCount,
nzShaderUniform_MaterialAlphaMap, nzShaderUniform_MaterialAlphaMap,
nzShaderUniform_MaterialAlphaThreshold, nzShaderUniform_MaterialAlphaThreshold,
@ -195,6 +196,7 @@ enum nzShaderUniform
nzShaderUniform_MaterialSpecularMap, nzShaderUniform_MaterialSpecularMap,
nzShaderUniform_ProjMatrix, nzShaderUniform_ProjMatrix,
nzShaderUniform_SceneAmbient, nzShaderUniform_SceneAmbient,
nzShaderUniform_TargetSize,
nzShaderUniform_ViewMatrix, nzShaderUniform_ViewMatrix,
nzShaderUniform_ViewProjMatrix, nzShaderUniform_ViewProjMatrix,
nzShaderUniform_WorldMatrix, nzShaderUniform_WorldMatrix,

View File

@ -65,6 +65,7 @@ bool NzGLSLShader::Compile()
#define CacheUniform(name) m_uniformLocations[nzShaderUniform_##name] = GetUniformLocation(#name) #define CacheUniform(name) m_uniformLocations[nzShaderUniform_##name] = GetUniformLocation(#name)
CacheUniform(CameraPosition); CacheUniform(CameraPosition);
CacheUniform(InvTargetSize);
CacheUniform(LightCount); CacheUniform(LightCount);
CacheUniform(MaterialAlphaMap); CacheUniform(MaterialAlphaMap);
CacheUniform(MaterialAlphaThreshold); CacheUniform(MaterialAlphaThreshold);
@ -79,6 +80,7 @@ bool NzGLSLShader::Compile()
CacheUniform(MaterialSpecularMap); CacheUniform(MaterialSpecularMap);
CacheUniform(ProjMatrix); CacheUniform(ProjMatrix);
CacheUniform(SceneAmbient); CacheUniform(SceneAmbient);
CacheUniform(TargetSize);
CacheUniform(ViewMatrix); CacheUniform(ViewMatrix);
CacheUniform(ViewProjMatrix); CacheUniform(ViewProjMatrix);
CacheUniform(WorldMatrix); CacheUniform(WorldMatrix);

View File

@ -46,7 +46,6 @@ namespace
struct MatrixUnit struct MatrixUnit
{ {
NzMatrix4f matrix; NzMatrix4f matrix;
bool sent;
bool updated; bool updated;
int location; int location;
}; };
@ -84,6 +83,7 @@ namespace
const NzVertexDeclaration* s_instancingDeclaration; const NzVertexDeclaration* s_instancingDeclaration;
bool s_capabilities[nzRendererCap_Max+1]; bool s_capabilities[nzRendererCap_Max+1];
bool s_instancing; bool s_instancing;
bool s_uniformTargetSizeUpdated;
bool s_useSamplerObjects; bool s_useSamplerObjects;
bool s_useVertexArrayObjects; bool s_useVertexArrayObjects;
unsigned int s_maxRenderTarget; unsigned int s_maxRenderTarget;
@ -512,7 +512,6 @@ bool NzRenderer::Initialize()
MatrixUnit& unit = s_matrices[i]; MatrixUnit& unit = s_matrices[i];
unit.location = -1; unit.location = -1;
unit.matrix.MakeIdentity(); unit.matrix.MakeIdentity();
unit.sent = false;
unit.updated = true; unit.updated = true;
} }
@ -555,7 +554,6 @@ bool NzRenderer::Initialize()
GLint maxTextureUnits; GLint maxTextureUnits;
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
// Impossible de binder plus de texcoords que d'attributes (en sachant qu'un certain nombre est déjà pris par les autres attributs)
s_maxTextureUnit = static_cast<unsigned int>(maxTextureUnits); s_maxTextureUnit = static_cast<unsigned int>(maxTextureUnits);
} }
else else
@ -571,6 +569,7 @@ bool NzRenderer::Initialize()
s_shader = nullptr; s_shader = nullptr;
s_target = nullptr; s_target = nullptr;
s_textureUnits.resize(s_maxTextureUnit); s_textureUnits.resize(s_maxTextureUnit);
s_uniformTargetSizeUpdated = false;
s_useSamplerObjects = NzOpenGL::IsSupported(nzOpenGLExtension_SamplerObjects); s_useSamplerObjects = NzOpenGL::IsSupported(nzOpenGLExtension_SamplerObjects);
s_useVertexArrayObjects = NzOpenGL::IsSupported(nzOpenGLExtension_VertexArrayObjects); s_useVertexArrayObjects = NzOpenGL::IsSupported(nzOpenGLExtension_VertexArrayObjects);
s_vertexBuffer = nullptr; s_vertexBuffer = nullptr;
@ -1059,6 +1058,8 @@ bool NzRenderer::SetTarget(const NzRenderTarget* target)
s_target = target; s_target = target;
s_targetSize.Set(target->GetWidth(), target->GetHeight()); s_targetSize.Set(target->GetWidth(), target->GetHeight());
s_uniformTargetSizeUpdated = false;
} }
return true; return true;
@ -1067,9 +1068,9 @@ bool NzRenderer::SetTarget(const NzRenderTarget* target)
void NzRenderer::SetTexture(nzUInt8 unit, const NzTexture* texture) void NzRenderer::SetTexture(nzUInt8 unit, const NzTexture* texture)
{ {
#if NAZARA_RENDERER_SAFE #if NAZARA_RENDERER_SAFE
if (unit >= s_textureUnits.size()) if (unit >= s_maxTextureUnit)
{ {
NazaraError("Texture unit out of range (" + NzString::Number(unit) + " >= " + NzString::Number(s_textureUnits.size()) + ')'); NazaraError("Texture unit out of range (" + NzString::Number(unit) + " >= " + NzString::Number(s_maxTextureUnit) + ')');
return; return;
} }
#endif #endif
@ -1093,9 +1094,9 @@ void NzRenderer::SetTexture(nzUInt8 unit, const NzTexture* texture)
void NzRenderer::SetTextureSampler(nzUInt8 unit, const NzTextureSampler& sampler) void NzRenderer::SetTextureSampler(nzUInt8 unit, const NzTextureSampler& sampler)
{ {
#if NAZARA_RENDERER_SAFE #if NAZARA_RENDERER_SAFE
if (unit >= s_textureUnits.size()) if (unit >= s_maxTextureUnit)
{ {
NazaraError("Texture unit out of range (" + NzString::Number(unit) + " >= " + NzString::Number(s_textureUnits.size()) + ')'); NazaraError("Texture unit out of range (" + NzString::Number(unit) + " >= " + NzString::Number(s_maxTextureUnit) + ')');
return; return;
} }
#endif #endif
@ -1251,15 +1252,30 @@ bool NzRenderer::EnsureStateUpdate()
s_matrices[nzMatrixType_WorldView].location = shaderImpl->GetUniformLocation(nzShaderUniform_WorldViewMatrix); s_matrices[nzMatrixType_WorldView].location = shaderImpl->GetUniformLocation(nzShaderUniform_WorldViewMatrix);
s_matrices[nzMatrixType_WorldViewProj].location = shaderImpl->GetUniformLocation(nzShaderUniform_WorldViewProjMatrix); s_matrices[nzMatrixType_WorldViewProj].location = shaderImpl->GetUniformLocation(nzShaderUniform_WorldViewProjMatrix);
s_updateFlags |= Update_Matrices; s_uniformTargetSizeUpdated = false;
for (unsigned int i = 0; i <= nzMatrixType_Max; ++i) s_updateFlags |= Update_Matrices; // Changement de shader, on renvoie toutes les matrices demandées
s_matrices[i].sent = false; // Changement de shader, on renvoie toutes les matrices demandées
s_updateFlags &= ~Update_Shader; s_updateFlags &= ~Update_Shader;
} }
shaderImpl->BindTextures(); shaderImpl->BindTextures();
// Envoi des uniformes liées au Renderer
if (!s_uniformTargetSizeUpdated)
{
int location;
location = shaderImpl->GetUniformLocation(nzShaderUniform_InvTargetSize);
if (location != -1)
shaderImpl->SendVector(location, 1.f/NzVector2f(s_targetSize));
location = shaderImpl->GetUniformLocation(nzShaderUniform_TargetSize);
if (location != -1)
shaderImpl->SendVector(location, 1.f/NzVector2f(s_targetSize));
s_uniformTargetSizeUpdated = true;
}
if (s_updateFlags != Update_None) if (s_updateFlags != Update_None)
{ {
if (s_updateFlags & Update_Textures) if (s_updateFlags & Update_Textures)
@ -1316,7 +1332,6 @@ bool NzRenderer::EnsureStateUpdate()
UpdateMatrix(static_cast<nzMatrixType>(i)); UpdateMatrix(static_cast<nzMatrixType>(i));
shaderImpl->SendMatrix(unit.location, unit.matrix); shaderImpl->SendMatrix(unit.location, unit.matrix);
unit.sent = true;
} }
} }