1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL (ES) Module
3 * -----------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief State change performance tests.
22 *//*--------------------------------------------------------------------*/
24 #include "glsStateChangePerfTestCases.hpp"
26 #include "tcuTestLog.hpp"
28 #include "gluDefs.hpp"
29 #include "gluRenderContext.hpp"
30 #include "gluShaderProgram.hpp"
32 #include "glwFunctions.hpp"
33 #include "glwEnums.hpp"
35 #include "deStringUtil.hpp"
65 ResultStats calculateStats (const vector<deUint64>& values)
67 ResultStats result = { 0.0, 0.0, 0.0, 0xFFFFFFFFFFFFFFFFu, 0 };
71 for (int i = 0; i < (int)values.size(); i++)
74 result.mean = ((double)sum) / (double)values.size();
76 for (int i = 0; i < (int)values.size(); i++)
78 const double val = (double)values[i];
79 result.variance += (val - result.mean) * (val - result.mean);
82 result.variance /= (double)values.size();
85 const int n = (int)(values.size()/2);
87 vector<deUint64> sortedValues = values;
89 std::sort(sortedValues.begin(), sortedValues.end());
91 result.median = (double)sortedValues[n];
94 for (int i = 0; i < (int)values.size(); i++)
96 result.min = std::min(result.min, values[i]);
97 result.max = std::max(result.max, values[i]);
104 void genIndices (vector<GLushort>& indices, int triangleCount)
106 indices.reserve(triangleCount*3);
108 for (int triangleNdx = 0; triangleNdx < triangleCount; triangleNdx++)
110 indices.push_back((GLushort)(triangleNdx*3));
111 indices.push_back((GLushort)(triangleNdx*3+1));
112 indices.push_back((GLushort)(triangleNdx*3+2));
116 void genCoords (vector<GLfloat>& coords, int triangleCount)
118 coords.reserve(triangleCount * 3 * 2);
120 for (int triangleNdx = 0; triangleNdx < triangleCount; triangleNdx++)
122 if ((triangleNdx % 2) == 0)
125 coords.push_back(-1.0f);
126 coords.push_back(-1.0f);
128 coords.push_back( 1.0f);
129 coords.push_back(-1.0f);
131 coords.push_back( 1.0f);
132 coords.push_back( 1.0f);
137 coords.push_back(-1.0f);
138 coords.push_back(-1.0f);
140 coords.push_back(-1.0f);
141 coords.push_back( 1.0f);
143 coords.push_back( 1.0f);
144 coords.push_back( 1.0f);
149 void genTextureData (vector<deUint8>& data, int width, int height)
152 data.reserve(width*height*4);
154 for (int x = 0; x < width; x++)
156 for (int y = 0; y < height; y++)
158 data.push_back((deUint8)((255*x)/width));
159 data.push_back((deUint8)((255*y)/width));
160 data.push_back((deUint8)((255*x*y)/(width*height)));
166 double calculateVariance (const vector<deUint64>& values, double avg)
170 for (int valueNdx = 0; valueNdx < (int)values.size(); valueNdx++)
172 double value = (double)values[valueNdx];
173 sum += (value - avg) * (value - avg);
176 return sum / (double)values.size();
179 deUint64 findMin (const vector<deUint64>& values)
181 deUint64 min = ~0ull;
183 for (int valueNdx = 0; valueNdx < (int)values.size(); valueNdx++)
184 min = std::min(values[valueNdx], min);
189 deUint64 findMax (const vector<deUint64>& values)
193 for (int valueNdx = 0; valueNdx < (int)values.size(); valueNdx++)
194 max = std::max(values[valueNdx], max);
199 deUint64 findMedian (const vector<deUint64>& v)
201 vector<deUint64> values = v;
202 size_t n = values.size() / 2;
204 std::nth_element(values.begin(), values.begin() + n, values.end());
211 StateChangePerformanceCase::StateChangePerformanceCase (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const char* name, const char* description, DrawType drawType, int drawCallCount, int triangleCount)
212 : tcu::TestCase (testCtx, tcu::NODETYPE_PERFORMANCE, name, description)
213 , m_renderCtx (renderCtx)
214 , m_drawType (drawType)
215 , m_iterationCount (100)
216 , m_callCount (drawCallCount)
217 , m_triangleCount (triangleCount)
221 StateChangePerformanceCase::~StateChangePerformanceCase (void)
223 StateChangePerformanceCase::deinit();
226 void StateChangePerformanceCase::init (void)
228 if (m_drawType == DRAWTYPE_INDEXED_USER_PTR)
229 genIndices(m_indices, m_triangleCount);
232 void StateChangePerformanceCase::requireIndexBuffers (int count)
234 const glw::Functions& gl = m_renderCtx.getFunctions();
236 if ((int)m_indexBuffers.size() >= count)
239 m_indexBuffers.reserve(count);
241 vector<GLushort> indices;
242 genIndices(indices, m_triangleCount);
244 while ((int)m_indexBuffers.size() < count)
248 gl.genBuffers(1, &buffer);
249 GLU_EXPECT_NO_ERROR(gl.getError(), "glGenBuffers()");
251 gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
252 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindBuffer()");
253 gl.bufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)(indices.size() * sizeof(GLushort)), &(indices[0]), GL_STATIC_DRAW);
254 GLU_EXPECT_NO_ERROR(gl.getError(), "glBufferData()");
255 gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
256 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindBuffer()");
258 m_indexBuffers.push_back(buffer);
262 void StateChangePerformanceCase::requireCoordBuffers (int count)
264 const glw::Functions& gl = m_renderCtx.getFunctions();
266 if ((int)m_coordBuffers.size() >= count)
269 m_coordBuffers.reserve(count);
271 vector<GLfloat> coords;
272 genCoords(coords, m_triangleCount);
274 while ((int)m_coordBuffers.size() < count)
278 gl.genBuffers(1, &buffer);
279 GLU_EXPECT_NO_ERROR(gl.getError(), "glGenBuffers()");
281 gl.bindBuffer(GL_ARRAY_BUFFER, buffer);
282 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindBuffer()");
283 gl.bufferData(GL_ARRAY_BUFFER, (GLsizeiptr)(coords.size() * sizeof(GLfloat)), &(coords[0]), GL_STATIC_DRAW);
284 GLU_EXPECT_NO_ERROR(gl.getError(), "glBufferData()");
285 gl.bindBuffer(GL_ARRAY_BUFFER, 0);
286 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindBuffer()");
288 m_coordBuffers.push_back(buffer);
292 void StateChangePerformanceCase::requirePrograms (int count)
294 if ((int)m_programs.size() >= count)
297 m_programs.reserve(count);
299 while ((int)m_programs.size() < count)
301 string vertexShaderSource =
302 "attribute mediump vec2 a_coord;\n"
303 "varying mediump vec2 v_texCoord;\n"
306 "\tv_texCoord = vec2(0.5) + 0.5" + de::toString(m_programs.size()) + " * a_coord.xy;\n"
307 "\tgl_Position = vec4(a_coord, 0.5, 1.0);\n"
310 string fragmentShaderSource =
311 "uniform sampler2D u_sampler;\n"
312 "varying mediump vec2 v_texCoord;\n"
315 "\tgl_FragColor = vec4(1.0" + de::toString(m_programs.size()) + " * texture2D(u_sampler, v_texCoord).xyz, 1.0);\n"
318 glu::ShaderProgram* program = new glu::ShaderProgram(m_renderCtx, glu::ProgramSources() << glu::VertexSource(vertexShaderSource) << glu::FragmentSource(fragmentShaderSource));
320 if (!program->isOk())
322 m_testCtx.getLog() << *program;
324 TCU_FAIL("Compile failed");
327 m_programs.push_back(program);
331 void StateChangePerformanceCase::requireTextures (int count)
333 const glw::Functions& gl = m_renderCtx.getFunctions();
335 const int textureWidth = 64;
336 const int textureHeight = 64;
338 if ((int)m_textures.size() >= count)
341 m_textures.reserve(count);
343 vector<deUint8> textureData;
344 genTextureData(textureData, textureWidth, textureHeight);
346 DE_ASSERT(textureData.size() == textureWidth * textureHeight * 4);
348 while ((int)m_textures.size() < count)
352 gl.genTextures(1, &texture);
353 GLU_EXPECT_NO_ERROR(gl.getError(), "glGenTextures()");
355 gl.bindTexture(GL_TEXTURE_2D, texture);
356 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture()");
358 gl.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, &(textureData[0]));
359 GLU_EXPECT_NO_ERROR(gl.getError(), "glTexImage2D()");
361 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
362 GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri()");
363 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
364 GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri()");
365 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
366 GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri()");
367 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
368 GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri()");
370 gl.bindTexture(GL_TEXTURE_2D, 0);
371 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture()");
373 m_textures.push_back(texture);
377 void StateChangePerformanceCase::requireFramebuffers (int count)
379 const glw::Functions& gl = m_renderCtx.getFunctions();
381 if ((int)m_framebuffers.size() >= count)
384 m_framebuffers.reserve(count);
386 requireRenderbuffers(count);
388 while ((int)m_framebuffers.size() < count)
392 gl.genFramebuffers(1, &framebuffer);
393 GLU_EXPECT_NO_ERROR(gl.getError(), "glGenFramebuffers()");
395 gl.bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
396 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindFramebuffer()");
398 gl.bindRenderbuffer(GL_RENDERBUFFER, m_renderbuffers[m_framebuffers.size()]);
399 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindRenderbuffer()");
401 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_renderbuffers[m_framebuffers.size()]);
402 GLU_EXPECT_NO_ERROR(gl.getError(), "glFramebufferRenderbuffer()");
404 gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
405 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindRenderbuffer()");
407 gl.bindFramebuffer(GL_FRAMEBUFFER, 0);
408 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindFramebuffer()");
410 m_framebuffers.push_back(framebuffer);
414 void StateChangePerformanceCase::requireRenderbuffers (int count)
416 const glw::Functions& gl = m_renderCtx.getFunctions();
418 if ((int)m_renderbuffers.size() >= count)
421 m_renderbuffers.reserve(count);
423 while ((int)m_renderbuffers.size() < count)
427 gl.genRenderbuffers(1, &renderbuffer);
428 GLU_EXPECT_NO_ERROR(gl.getError(), "glGenRenderbuffers()");
430 gl.bindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
431 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindRenderbuffer()");
433 gl.renderbufferStorage(GL_RENDERBUFFER, GL_RGB565, 24, 24);
434 GLU_EXPECT_NO_ERROR(gl.getError(), "glRenderbufferStorage()");
436 gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
437 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindRenderbuffer()");
439 m_renderbuffers.push_back(renderbuffer);
443 void StateChangePerformanceCase::requireSamplers (int count)
445 const glw::Functions& gl = m_renderCtx.getFunctions();
447 if ((int)m_samplers.size() >= count)
450 m_samplers.reserve(count);
452 while ((int)m_samplers.size() < count)
455 gl.genSamplers(1, &sampler);
456 GLU_EXPECT_NO_ERROR(gl.getError(), "glGenSamplers()");
457 m_samplers.push_back(sampler);
461 void StateChangePerformanceCase::requireVertexArrays (int count)
463 const glw::Functions& gl = m_renderCtx.getFunctions();
465 if ((int)m_vertexArrays.size() >= count)
468 m_vertexArrays.reserve(count);
470 while ((int)m_vertexArrays.size() < count)
473 gl.genVertexArrays(1, &vertexArray);
474 GLU_EXPECT_NO_ERROR(gl.getError(), "glGenVertexArrays()");
475 m_vertexArrays.push_back(vertexArray);
479 void StateChangePerformanceCase::deinit (void)
482 m_interleavedResults.clear();
483 m_batchedResults.clear();
486 const glw::Functions& gl = m_renderCtx.getFunctions();
488 if (!m_indexBuffers.empty())
490 gl.deleteBuffers((GLsizei)m_indexBuffers.size(), &(m_indexBuffers[0]));
491 m_indexBuffers.clear();
494 if (!m_coordBuffers.empty())
496 gl.deleteBuffers((GLsizei)m_coordBuffers.size(), &(m_coordBuffers[0]));
497 m_coordBuffers.clear();
500 if (!m_textures.empty())
502 gl.deleteTextures((GLsizei)m_textures.size(), &(m_textures[0]));
506 if (!m_framebuffers.empty())
508 gl.deleteFramebuffers((GLsizei)m_framebuffers.size(), &(m_framebuffers[0]));
509 m_framebuffers.clear();
512 if (!m_renderbuffers.empty())
514 gl.deleteRenderbuffers((GLsizei)m_renderbuffers.size(), &(m_renderbuffers[0]));
515 m_renderbuffers.clear();
518 if (!m_samplers.empty())
520 gl.deleteSamplers((GLsizei)m_samplers.size(), &m_samplers[0]);
524 if (!m_vertexArrays.empty())
526 gl.deleteVertexArrays((GLsizei)m_vertexArrays.size(), &m_vertexArrays[0]);
527 m_vertexArrays.clear();
530 for (int programNdx = 0; programNdx < (int)m_programs.size(); programNdx++)
532 delete m_programs[programNdx];
533 m_programs[programNdx] = NULL;
539 void StateChangePerformanceCase::logAndSetTestResult (void)
541 TestLog& log = m_testCtx.getLog();
543 ResultStats interleaved = calculateStats(m_interleavedResults);
544 ResultStats batched = calculateStats(m_batchedResults);
546 log << TestLog::Message << "Interleaved mean: " << interleaved.mean << TestLog::EndMessage;
547 log << TestLog::Message << "Interleaved median: " << interleaved.median << TestLog::EndMessage;
548 log << TestLog::Message << "Interleaved variance: " << interleaved.variance << TestLog::EndMessage;
549 log << TestLog::Message << "Interleaved min: " << interleaved.min << TestLog::EndMessage;
550 log << TestLog::Message << "Interleaved max: " << interleaved.max << TestLog::EndMessage;
552 log << TestLog::Message << "Batched mean: " << batched.mean << TestLog::EndMessage;
553 log << TestLog::Message << "Batched median: " << batched.median << TestLog::EndMessage;
554 log << TestLog::Message << "Batched variance: " << batched.variance << TestLog::EndMessage;
555 log << TestLog::Message << "Batched min: " << batched.min << TestLog::EndMessage;
556 log << TestLog::Message << "Batched max: " << batched.max << TestLog::EndMessage;
558 log << TestLog::Message << "Batched/Interleaved mean ratio: " << (interleaved.mean/batched.mean) << TestLog::EndMessage;
559 log << TestLog::Message << "Batched/Interleaved median ratio: " << (interleaved.median/batched.median) << TestLog::EndMessage;
561 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, de::floatToString((float)(((double)interleaved.median) / batched.median), 2).c_str());
564 tcu::TestCase::IterateResult StateChangePerformanceCase::iterate (void)
566 if (m_interleavedResults.empty() && m_batchedResults.empty())
568 TestLog& log = m_testCtx.getLog();
570 log << TestLog::Message << "Draw call count: " << m_callCount << TestLog::EndMessage;
571 log << TestLog::Message << "Per call triangle count: " << m_triangleCount << TestLog::EndMessage;
574 // \note [mika] Interleave sampling to balance effects of powerstate etc.
575 if ((int)m_interleavedResults.size() < m_iterationCount && m_batchedResults.size() >= m_interleavedResults.size())
577 const glw::Functions& gl = m_renderCtx.getFunctions();
578 deUint64 resBeginUs = 0;
579 deUint64 resEndUs = 0;
581 setupInitialState(gl);
583 GLU_EXPECT_NO_ERROR(gl.getError(), "glFinish()");
586 resBeginUs = deGetMicroseconds();
591 resEndUs = deGetMicroseconds();
592 GLU_EXPECT_NO_ERROR(gl.getError(), "glFinish()");
594 m_interleavedResults.push_back(resEndUs - resBeginUs);
598 else if ((int)m_batchedResults.size() < m_iterationCount)
600 const glw::Functions& gl = m_renderCtx.getFunctions();
601 deUint64 refBeginUs = 0;
602 deUint64 refEndUs = 0;
604 setupInitialState(gl);
606 GLU_EXPECT_NO_ERROR(gl.getError(), "glFinish()");
609 refBeginUs = deGetMicroseconds();
614 refEndUs = deGetMicroseconds();
615 GLU_EXPECT_NO_ERROR(gl.getError(), "glFinish()");
617 m_batchedResults.push_back(refEndUs - refBeginUs);
623 logAndSetTestResult();
628 void StateChangePerformanceCase::callDraw (const glw::Functions& gl)
632 case DRAWTYPE_NOT_INDEXED: gl.drawArrays(GL_TRIANGLES, 0, m_triangleCount * 3); break;
633 case DRAWTYPE_INDEXED_USER_PTR: gl.drawElements(GL_TRIANGLES, m_triangleCount * 3, GL_UNSIGNED_SHORT, &m_indices[0]); break;
634 case DRAWTYPE_INDEXED_BUFFER: gl.drawElements(GL_TRIANGLES, m_triangleCount * 3, GL_UNSIGNED_SHORT, NULL); break;
640 // StateChangeCallPerformanceCase
642 StateChangeCallPerformanceCase::StateChangeCallPerformanceCase (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const char* name, const char* description)
643 : tcu::TestCase (testCtx, tcu::NODETYPE_PERFORMANCE, name, description)
644 , m_renderCtx (renderCtx)
645 , m_iterationCount (100)
650 StateChangeCallPerformanceCase::~StateChangeCallPerformanceCase (void)
654 void StateChangeCallPerformanceCase::executeTest (void)
656 const glw::Functions& gl = m_renderCtx.getFunctions();
657 deUint64 beginTimeUs = 0;
658 deUint64 endTimeUs = 0;
660 beginTimeUs = deGetMicroseconds();
662 execCalls(gl, (int)m_results.size(), m_callCount);
664 endTimeUs = deGetMicroseconds();
666 m_results.push_back(endTimeUs - beginTimeUs);
669 void StateChangeCallPerformanceCase::logTestCase (void)
671 TestLog& log = m_testCtx.getLog();
673 log << TestLog::Message << "Iteration count: " << m_iterationCount << TestLog::EndMessage;
674 log << TestLog::Message << "Per iteration call count: " << m_callCount << TestLog::EndMessage;
677 double calculateAverage (const vector<deUint64>& values)
681 for (int valueNdx = 0; valueNdx < (int)values.size(); valueNdx++)
682 sum += values[valueNdx];
684 return ((double)sum) / (double)values.size();
687 void StateChangeCallPerformanceCase::logAndSetTestResult (void)
689 TestLog& log = m_testCtx.getLog();
691 deUint64 minUs = findMin(m_results);
692 deUint64 maxUs = findMax(m_results);
693 deUint64 medianUs = findMedian(m_results);
694 double avgIterationUs = calculateAverage(m_results);
695 double avgCallUs = avgIterationUs / m_callCount;
696 double varIteration = calculateVariance(m_results, avgIterationUs);
697 double avgMedianCallUs = ((double)medianUs)/m_callCount;
699 log << TestLog::Message << "Min iteration time: " << minUs << "us" << TestLog::EndMessage;
700 log << TestLog::Message << "Max iteration time: " << maxUs << "us" << TestLog::EndMessage;
701 log << TestLog::Message << "Average iteration time: " << avgIterationUs << "us" << TestLog::EndMessage;
702 log << TestLog::Message << "Iteration variance time: " << varIteration << TestLog::EndMessage;
703 log << TestLog::Message << "Median iteration time: " << medianUs << "us" << TestLog::EndMessage;
704 log << TestLog::Message << "Average call time: " << avgCallUs << "us" << TestLog::EndMessage;
705 log << TestLog::Message << "Average call time for median iteration: " << avgMedianCallUs << "us" << TestLog::EndMessage;
707 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, de::floatToString((float)avgMedianCallUs, 3).c_str());
710 tcu::TestCase::IterateResult StateChangeCallPerformanceCase::iterate (void)
712 if (m_results.empty())
715 if ((int)m_results.size() < m_iterationCount)
718 GLU_EXPECT_NO_ERROR(m_renderCtx.getFunctions().getError(), "Unexpected error");
723 logAndSetTestResult();