Hash :
fc5aef40
Author :
Date :
2016-12-19T15:23:49
Add a matrix uniforms perf test. This test also adds a mode which controls if the uniform data changes frame-to-frame. Could be useful when we test removing redundant data checking. BUG=angleproject:1385 BUG=angleproject:1390 BUG=angleproject:1671 Change-Id: I936702b83f3d7cd97918542b06ecc1372884b412 Reviewed-on: https://chromium-review.googlesource.com/422348 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
//
// Copyright (c) 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// UniformsBenchmark:
// Performance test for setting uniform data.
//
#include "ANGLEPerfTest.h"
#include <iostream>
#include <random>
#include <sstream>
#include "Matrix.h"
#include "shader_utils.h"
using namespace angle;
namespace
{
// Controls when we call glUniform, if the data is the same as last frame.
enum DataMode
{
UPDATE,
REPEAT,
};
// TODO(jmadill): Use an ANGLE enum for this?
enum DataType
{
VEC4,
MAT4,
};
struct UniformsParams final : public RenderTestParams
{
UniformsParams()
{
// Common default params
majorVersion = 2;
minorVersion = 0;
windowWidth = 720;
windowHeight = 720;
}
std::string suffix() const override;
size_t numVertexUniforms = 200;
size_t numFragmentUniforms = 200;
DataType dataType = DataType::VEC4;
DataMode dataMode = DataMode::REPEAT;
// static parameters
size_t iterations = 4;
};
std::ostream &operator<<(std::ostream &os, const UniformsParams ¶ms)
{
os << params.suffix().substr(1);
return os;
}
std::string UniformsParams::suffix() const
{
std::stringstream strstr;
strstr << RenderTestParams::suffix();
if (dataType == DataType::VEC4)
{
strstr << "_" << numVertexUniforms << "_vertex_uniforms";
strstr << "_" << numFragmentUniforms << "_fragment_uniforms";
}
else if (dataMode == DataMode::REPEAT)
{
strstr << "_matrix_repeating";
}
else
{
strstr << "_matrix_updating";
}
return strstr.str();
}
class UniformsBenchmark : public ANGLERenderTest,
public ::testing::WithParamInterface<UniformsParams>
{
public:
UniformsBenchmark();
void initializeBenchmark() override;
void destroyBenchmark() override;
void drawBenchmark() override;
private:
void initShaders();
void initVertexBuffer();
void initTextures();
GLuint mProgram;
std::vector<GLuint> mUniformLocations;
std::vector<Matrix4> mMatrixData[2];
};
std::vector<Matrix4> GenMatrixData(size_t count, int parity)
{
std::vector<Matrix4> data;
// Very simple matrix data allocation scheme.
for (size_t index = 0; index < count; ++index)
{
Matrix4 mat;
for (int row = 0; row < 4; ++row)
{
for (int col = 0; col < 4; ++col)
{
mat.data[row * 4 + col] = (row * col + parity) % 2 == 0 ? 1.0f : -1.0f;
}
}
data.push_back(mat);
}
return data;
}
UniformsBenchmark::UniformsBenchmark() : ANGLERenderTest("Uniforms", GetParam()), mProgram(0u)
{
}
void UniformsBenchmark::initializeBenchmark()
{
const auto ¶ms = GetParam();
ASSERT_GT(params.iterations, 0u);
// Verify the uniform counts are within the limits
GLint maxVertexUniforms, maxFragmentUniforms;
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &maxVertexUniforms);
glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &maxFragmentUniforms);
if (params.numVertexUniforms > static_cast<size_t>(maxVertexUniforms))
{
FAIL() << "Vertex uniform count (" << params.numVertexUniforms << ")"
<< " exceeds maximum vertex uniform count: " << maxVertexUniforms << std::endl;
}
if (params.numFragmentUniforms > static_cast<size_t>(maxFragmentUniforms))
{
FAIL() << "Fragment uniform count (" << params.numFragmentUniforms << ")"
<< " exceeds maximum fragment uniform count: " << maxFragmentUniforms << std::endl;
}
initShaders();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
if (params.dataType == DataType::MAT4)
{
size_t count = params.numVertexUniforms + params.numFragmentUniforms;
mMatrixData[0] = GenMatrixData(count, 0);
if (params.dataMode == DataMode::REPEAT)
{
mMatrixData[1] = GenMatrixData(count, 0);
}
else
{
mMatrixData[1] = GenMatrixData(count, 1);
}
}
ASSERT_GL_NO_ERROR();
}
std::string GetUniformLocationName(size_t idx, bool vertexShader)
{
std::stringstream strstr;
strstr << (vertexShader ? "vs" : "fs") << "_u_" << idx;
return strstr.str();
}
void UniformsBenchmark::initShaders()
{
const auto ¶ms = GetParam();
bool isMatrix = (params.dataType == DataType::MAT4);
std::stringstream vstrstr;
vstrstr << "precision mediump float;\n";
std::string typeString = isMatrix ? "mat4" : "vec4";
std::string constVector = "const vec4 one = vec4(1, 1, 1, 1);\n";
if (isMatrix)
{
vstrstr << constVector;
}
for (size_t i = 0; i < params.numVertexUniforms; i++)
{
vstrstr << "uniform " << typeString << " " << GetUniformLocationName(i, true) << ";\n";
}
vstrstr << "void main()\n"
"{\n"
" gl_Position = vec4(0, 0, 0, 0);\n";
for (size_t i = 0; i < params.numVertexUniforms; i++)
{
vstrstr << " gl_Position += " << GetUniformLocationName(i, true);
if (isMatrix)
{
vstrstr << " * one";
}
vstrstr << ";\n";
}
vstrstr << "}";
std::stringstream fstrstr;
fstrstr << "precision mediump float;\n";
if (isMatrix)
{
fstrstr << constVector;
}
for (size_t i = 0; i < params.numFragmentUniforms; i++)
{
fstrstr << "uniform " << typeString << " " << GetUniformLocationName(i, false) << ";\n";
}
fstrstr << "void main()\n"
"{\n"
" gl_FragColor = vec4(0, 0, 0, 0);\n";
for (size_t i = 0; i < params.numFragmentUniforms; i++)
{
fstrstr << " gl_FragColor += " << GetUniformLocationName(i, false);
if (isMatrix)
{
fstrstr << " * one";
}
fstrstr << ";\n";
}
fstrstr << "}";
mProgram = CompileProgram(vstrstr.str(), fstrstr.str());
ASSERT_NE(0u, mProgram);
for (size_t i = 0; i < params.numVertexUniforms; ++i)
{
GLint location = glGetUniformLocation(mProgram, GetUniformLocationName(i, true).c_str());
ASSERT_NE(-1, location);
mUniformLocations.push_back(location);
}
for (size_t i = 0; i < params.numFragmentUniforms; ++i)
{
GLint location = glGetUniformLocation(mProgram, GetUniformLocationName(i, false).c_str());
ASSERT_NE(-1, location);
mUniformLocations.push_back(location);
}
// Use the program object
glUseProgram(mProgram);
}
void UniformsBenchmark::destroyBenchmark()
{
glDeleteProgram(mProgram);
}
void UniformsBenchmark::drawBenchmark()
{
const auto ¶ms = GetParam();
size_t frameIndex = 0;
for (size_t it = 0; it < params.iterations; ++it, frameIndex = (frameIndex == 0 ? 1 : 0))
{
for (size_t uniform = 0; uniform < mUniformLocations.size(); ++uniform)
{
if (params.dataType == DataType::MAT4)
{
glUniformMatrix4fv(mUniformLocations[uniform], 1, GL_FALSE,
mMatrixData[frameIndex][uniform].data);
}
else
{
float value = static_cast<float>(uniform);
glUniform4f(mUniformLocations[uniform], value, value, value, value);
}
}
glDrawArrays(GL_TRIANGLES, 0, 3);
}
ASSERT_GL_NO_ERROR();
}
using namespace egl_platform;
UniformsParams VectorUniforms(const EGLPlatformParameters &egl)
{
UniformsParams params;
params.eglParameters = egl;
return params;
}
UniformsParams MatrixUniforms(const EGLPlatformParameters &egl, DataMode dataMode)
{
UniformsParams params;
params.eglParameters = egl;
params.dataType = DataType::MAT4;
params.dataMode = dataMode;
return params;
}
} // anonymous namespace
TEST_P(UniformsBenchmark, Run)
{
run();
}
ANGLE_INSTANTIATE_TEST(UniformsBenchmark,
VectorUniforms(D3D11()),
VectorUniforms(D3D9()),
VectorUniforms(OPENGL()),
MatrixUniforms(D3D11(), DataMode::REPEAT),
MatrixUniforms(D3D11(), DataMode::UPDATE),
MatrixUniforms(OPENGL(), DataMode::REPEAT),
MatrixUniforms(OPENGL(), DataMode::UPDATE));