[core] change program and uniforms only if it changed

This commit is contained in:
ExMix 2014-10-18 10:25:25 +03:00 committed by Alex Zolotarev
parent 359071ac92
commit 4806b6c4c1
3 changed files with 95 additions and 27 deletions

View file

@ -38,7 +38,11 @@ namespace graphics
void RenderContext::setProgram(shared_ptr<Program> const & prg)
{
m_program = prg;
if (m_program != prg)
{
m_program = prg;
m_program->riseChangedFlag();
}
}
ProgramManager * RenderContext::programManager()

View file

@ -106,7 +106,11 @@ namespace graphics
Uniform & f = it->second;
f.m_type = EFloat;
f.m_data.m_floatVal[0] = v0;
if (f.m_data.m_floatVal[0] != v0)
{
f.m_data.m_floatVal[0] = v0;
f.m_changed = true;
}
}
void Program::setParam(ESemantic sem, float v0, float v1)
@ -116,8 +120,13 @@ namespace graphics
Uniform & f = it->second;
f.m_type = EFloatVec2;
f.m_data.m_floatVal[0] = v0;
f.m_data.m_floatVal[1] = v1;
if (f.m_data.m_floatVal[0] != v0 ||
f.m_data.m_floatVal[1] != v1)
{
f.m_data.m_floatVal[0] = v0;
f.m_data.m_floatVal[1] = v1;
f.m_changed = true;
}
}
void Program::setParam(ESemantic sem, float v0, float v1, float v2)
@ -127,9 +136,15 @@ namespace graphics
Uniform & f = it->second;
f.m_type = EFloatVec3;
f.m_data.m_floatVal[0] = v0;
f.m_data.m_floatVal[1] = v1;
f.m_data.m_floatVal[2] = v2;
if (f.m_data.m_floatVal[0] != v0 ||
f.m_data.m_floatVal[1] != v1 ||
f.m_data.m_floatVal[2] != v2)
{
f.m_data.m_floatVal[0] = v0;
f.m_data.m_floatVal[1] = v1;
f.m_data.m_floatVal[2] = v2;
f.m_changed = true;
}
}
void Program::setParam(ESemantic sem, float v0, float v1, float v2, float v3)
@ -139,10 +154,17 @@ namespace graphics
Uniform & f = it->second;
f.m_type = EFloatVec4;
f.m_data.m_floatVal[0] = v0;
f.m_data.m_floatVal[1] = v1;
f.m_data.m_floatVal[2] = v2;
f.m_data.m_floatVal[3] = v3;
if (f.m_data.m_floatVal[0] != v0 ||
f.m_data.m_floatVal[1] != v1 ||
f.m_data.m_floatVal[2] != v2 ||
f.m_data.m_floatVal[3] != v3)
{
f.m_data.m_floatVal[0] = v0;
f.m_data.m_floatVal[1] = v1;
f.m_data.m_floatVal[2] = v2;
f.m_data.m_floatVal[3] = v3;
f.m_changed = true;
}
}
void Program::setParam(ESemantic sem, int v0)
@ -152,7 +174,11 @@ namespace graphics
Uniform & f = it->second;
f.m_type = EInteger;
f.m_data.m_intVal[0] = v0;
if (f.m_data.m_intVal[0] != v0)
{
f.m_data.m_intVal[0] = v0;
f.m_changed = true;
}
}
void Program::setParam(ESemantic sem, int v0, int v1)
@ -162,8 +188,13 @@ namespace graphics
Uniform & f = it->second;
f.m_type = EIntegerVec2;
f.m_data.m_intVal[0] = v0;
f.m_data.m_intVal[1] = v1;
if (f.m_data.m_intVal[0] != v0 ||
f.m_data.m_intVal[1] != v1)
{
f.m_data.m_intVal[0] = v0;
f.m_data.m_intVal[1] = v1;
f.m_changed = true;
}
}
void Program::setParam(ESemantic sem, int v0, int v1, int v2)
@ -173,9 +204,15 @@ namespace graphics
Uniform & f = it->second;
f.m_type = EIntegerVec3;
f.m_data.m_intVal[0] = v0;
f.m_data.m_intVal[1] = v1;
f.m_data.m_intVal[2] = v2;
if (f.m_data.m_intVal[0] != v0 ||
f.m_data.m_intVal[1] != v1 ||
f.m_data.m_intVal[2] != v2)
{
f.m_data.m_intVal[0] = v0;
f.m_data.m_intVal[1] = v1;
f.m_data.m_intVal[2] = v2;
f.m_changed = true;
}
}
void Program::setParam(ESemantic sem, int v0, int v1, int v2, int v3)
@ -185,10 +222,17 @@ namespace graphics
Uniform & f = it->second;
f.m_type = EIntegerVec4;
f.m_data.m_intVal[0] = v0;
f.m_data.m_intVal[1] = v1;
f.m_data.m_intVal[2] = v2;
f.m_data.m_intVal[3] = v3;
if (f.m_data.m_intVal[0] != v0 ||
f.m_data.m_intVal[1] != v1 ||
f.m_data.m_intVal[2] != v2 ||
f.m_data.m_intVal[3] != v3)
{
f.m_data.m_intVal[0] = v0;
f.m_data.m_intVal[1] = v1;
f.m_data.m_intVal[2] = v2;
f.m_data.m_intVal[3] = v3;
f.m_changed = true;
}
}
template <unsigned N>
@ -201,7 +245,11 @@ namespace graphics
Uniform & f = it->second;
f.m_type = dt;
copy(&m(0, 0), &m(0, 0) + N * N, f.m_data.m_matVal);
if (!equal(&m(0, 0), &m(0, 0) + N * N, f.m_data.m_matVal))
{
copy(&m(0, 0), &m(0, 0) + N * N, f.m_data.m_matVal);
f.m_changed = true;
}
}
void Program::setParam(ESemantic sem, math::Matrix<float, 2, 2> const & m)
@ -274,11 +322,14 @@ namespace graphics
void Program::applyUniforms()
{
/// setting all uniforms
for (TUniforms::const_iterator it = m_uniforms.begin();
it != m_uniforms.end();
++it)
typedef pair<ESemantic const, Uniform> TNode;
for (TNode & node : m_uniforms)
{
Uniform const & u = it->second;
Uniform & u = node.second;
if (!u.m_changed)
continue;
u.m_changed = false;
switch (u.m_type)
{
@ -354,7 +405,12 @@ namespace graphics
void Program::makeCurrent()
{
OGLCHECK(glUseProgramFn(m_handle));
if (m_changed)
{
OGLCHECK(glUseProgramFn(m_handle));
m_changed = false;
}
m_storage.m_vertices->makeCurrent();
@ -364,6 +420,11 @@ namespace graphics
applyUniforms();
}
void Program::riseChangedFlag()
{
m_changed = true;
}
}
}

View file

@ -36,6 +36,7 @@ namespace graphics
} m_data;
EDataType m_type;
GLint m_handle;
bool m_changed = true;
};
struct Attribute
@ -47,6 +48,7 @@ namespace graphics
size_t m_stride;
};
bool m_changed = true;
GLuint m_handle;
typedef map<ESemantic, Uniform> TUniforms;
@ -93,6 +95,7 @@ namespace graphics
void setStorage(Storage const & storage);
void makeCurrent();
void riseChangedFlag();
};
}
}