Track if uniform data has been updated to avoid extra Map calls.
Compare all uniform data to the values currently stored in the uniform and
only mark the uniform as dirty if the data is new. This saves lots of
buffer map calls when the same data is set on a uniform many times.
BUG=260069
Change-Id: Ic4df8a276a81b074211712ff50e5cc4d0d9bb612
Reviewed-on: https://chromium-review.googlesource.com/199346
Reviewed-by: Nicolas Capens <nicolascapens@chromium.org>
Tested-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libGLESv2/ProgramBinary.cpp b/src/libGLESv2/ProgramBinary.cpp
index a0742ab..2aa3719 100644
--- a/src/libGLESv2/ProgramBinary.cpp
+++ b/src/libGLESv2/ProgramBinary.cpp
@@ -474,6 +474,16 @@
}
template <typename T>
+static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
+{
+ ASSERT(dest != NULL);
+ ASSERT(dirtyFlag != NULL);
+
+ *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
+ *dest = source;
+}
+
+template <typename T>
bool ProgramBinary::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
{
if (location < 0 || location >= (int)mUniformIndex.size())
@@ -485,7 +495,6 @@
const GLenum targetBoolType = UniformBoolVectorType(targetUniformType);
LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
- targetUniform->dirty = true;
int elementCount = targetUniform->elementCount();
@@ -502,11 +511,11 @@
{
for (int c = 0; c < components; c++)
{
- target[c] = v[c];
+ SetIfDirty(target + c, v[c], &targetUniform->dirty);
}
for (int c = components; c < 4; c++)
{
- target[c] = 0;
+ SetIfDirty(target + c, T(0), &targetUniform->dirty);
}
target += 4;
v += components;
@@ -520,11 +529,11 @@
{
for (int c = 0; c < components; c++)
{
- boolParams[c] = (v[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
+ SetIfDirty(boolParams + c, (v[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
}
for (int c = components; c < 4; c++)
{
- boolParams[c] = GL_FALSE;
+ SetIfDirty(boolParams + c, GL_FALSE, &targetUniform->dirty);
}
boolParams += 4;
v += components;
@@ -559,8 +568,9 @@
}
template<typename T>
-void transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
+bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
{
+ bool dirty = false;
int copyWidth = std::min(targetHeight, srcWidth);
int copyHeight = std::min(targetWidth, srcHeight);
@@ -568,7 +578,7 @@
{
for (int y = 0; y < copyHeight; y++)
{
- target[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
+ SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
}
}
// clear unfilled right side
@@ -576,7 +586,7 @@
{
for (int x = copyHeight; x < targetWidth; x++)
{
- target[y * targetWidth + x] = static_cast<T>(0);
+ SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
// clear unfilled bottom.
@@ -584,14 +594,17 @@
{
for (int x = 0; x < targetWidth; x++)
{
- target[y * targetWidth + x] = static_cast<T>(0);
+ SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
+
+ return dirty;
}
template<typename T>
-void expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
+bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
{
+ bool dirty = false;
int copyWidth = std::min(targetWidth, srcWidth);
int copyHeight = std::min(targetHeight, srcHeight);
@@ -599,7 +612,7 @@
{
for (int x = 0; x < copyWidth; x++)
{
- target[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
+ SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
}
}
// clear unfilled right side
@@ -607,7 +620,7 @@
{
for (int x = copyWidth; x < targetWidth; x++)
{
- target[y * targetWidth + x] = static_cast<T>(0);
+ SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
// clear unfilled bottom.
@@ -615,9 +628,11 @@
{
for (int x = 0; x < targetWidth; x++)
{
- target[y * targetWidth + x] = static_cast<T>(0);
+ SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
+
+ return dirty;
}
template <int cols, int rows>
@@ -629,7 +644,6 @@
}
LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
- targetUniform->dirty = true;
if (targetUniform->type != targetUniformType)
{
@@ -650,11 +664,11 @@
// Internally store matrices as transposed versions to accomodate HLSL matrix indexing
if (transpose == GL_FALSE)
{
- transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols);
+ targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
}
else
{
- expandMatrix<GLfloat>(target, value, 4, rows, cols, rows);
+ targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
}
target += targetMatrixStride;
value += cols * rows;
@@ -716,7 +730,6 @@
}
LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
- targetUniform->dirty = true;
int elementCount = targetUniform->elementCount();
@@ -731,10 +744,10 @@
for (int i = 0; i < count; i++)
{
- target[0] = v[0];
- target[1] = 0;
- target[2] = 0;
- target[3] = 0;
+ SetIfDirty(target + 0, v[0], &targetUniform->dirty);
+ SetIfDirty(target + 1, 0, &targetUniform->dirty);
+ SetIfDirty(target + 2, 0, &targetUniform->dirty);
+ SetIfDirty(target + 3, 0, &targetUniform->dirty);
target += 4;
v += 1;
}
@@ -745,10 +758,10 @@
for (int i = 0; i < count; i++)
{
- boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
- boolParams[1] = GL_FALSE;
- boolParams[2] = GL_FALSE;
- boolParams[3] = GL_FALSE;
+ SetIfDirty(boolParams + 0, (v[0] == 0) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
+ SetIfDirty(boolParams + 1, GL_FALSE, &targetUniform->dirty);
+ SetIfDirty(boolParams + 2, GL_FALSE, &targetUniform->dirty);
+ SetIfDirty(boolParams + 3, GL_FALSE, &targetUniform->dirty);
boolParams += 4;
v += 1;
}