1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 2.0 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 Flush and finish tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es2fFlushFinishTests.hpp"
26 #include "gluRenderContext.hpp"
27 #include "gluObjectWrapper.hpp"
28 #include "gluShaderProgram.hpp"
29 #include "gluDrawUtil.hpp"
31 #include "glsCalibration.hpp"
33 #include "tcuTestLog.hpp"
34 #include "tcuRenderTarget.hpp"
35 #include "tcuCPUWarmup.hpp"
37 #include "glwEnums.hpp"
38 #include "glwFunctions.hpp"
40 #include "deRandom.hpp"
41 #include "deStringUtil.hpp"
59 using deqp::gls::theilSenLinearRegression;
60 using deqp::gls::LineParameters;
67 MAX_VIEWPORT_SIZE = 128,
68 MAX_SAMPLE_DURATION_US = 1000*1000,
71 MIN_DRAW_CALL_COUNT = 10,
72 MAX_DRAW_CALL_COUNT = 1<<20,
73 NUM_ITERS_IN_SHADER = 10
76 const float NO_CORR_COEF_THRESHOLD = 0.1f;
77 const float FLUSH_COEF_THRESHOLD = 0.2f;
78 const float CORRELATED_COEF_THRESHOLD = 0.5f;
80 static void busyWait (int milliseconds)
82 const deUint64 startTime = deGetMicroseconds();
87 for (int i = 0; i < 10; i++)
90 if (deGetMicroseconds()-startTime >= deUint64(1000*milliseconds))
95 class CalibrationFailedException : public std::runtime_error
98 CalibrationFailedException (const std::string& reason) : std::runtime_error(reason) {}
101 class FlushFinishCase : public TestCase
104 enum ExpectedBehavior
106 EXPECT_COEF_LESS_THAN = 0,
107 EXPECT_COEF_GREATER_THAN,
110 FlushFinishCase (Context& context,
112 const char* description,
113 ExpectedBehavior waitBehavior,
115 ExpectedBehavior readBehavior,
116 float readThreshold);
117 ~FlushFinishCase (void);
121 IterateResult iterate (void);
127 deUint64 readPixelsTime;
130 struct CalibrationParams
136 virtual void waitForGL (void) = 0;
139 FlushFinishCase (const FlushFinishCase&);
140 FlushFinishCase& operator= (const FlushFinishCase&);
142 CalibrationParams calibrate (void);
143 void analyzeResults (const std::vector<Sample>& samples, const CalibrationParams& calibrationParams);
145 void setupRenderState (void);
146 void render (int numDrawCalls);
147 void readPixels (void);
149 const ExpectedBehavior m_waitBehavior;
150 const float m_waitThreshold;
151 const ExpectedBehavior m_readBehavior;
152 const float m_readThreshold;
154 glu::ShaderProgram* m_program;
157 FlushFinishCase::FlushFinishCase (Context& context, const char* name, const char* description, ExpectedBehavior waitBehavior, float waitThreshold, ExpectedBehavior readBehavior, float readThreshold)
158 : TestCase (context, name, description)
159 , m_waitBehavior (waitBehavior)
160 , m_waitThreshold (waitThreshold)
161 , m_readBehavior (readBehavior)
162 , m_readThreshold (readThreshold)
163 , m_program (DE_NULL)
167 FlushFinishCase::~FlushFinishCase (void)
169 FlushFinishCase::deinit();
172 void FlushFinishCase::init (void)
174 DE_ASSERT(!m_program);
176 m_program = new glu::ShaderProgram(m_context.getRenderContext(),
177 glu::ProgramSources()
178 << glu::VertexSource(
179 "attribute highp vec4 a_position;\n"
180 "varying highp vec4 v_coord;\n"
183 " gl_Position = a_position;\n"
184 " v_coord = a_position;\n"
186 << glu::FragmentSource(
187 "uniform mediump int u_numIters;\n"
188 "varying mediump vec4 v_coord;\n"
191 " highp vec4 color = v_coord;\n"
192 " for (int i = 0; i < " + de::toString(int(NUM_ITERS_IN_SHADER)) + "; i++)\n"
193 " color = sin(color);\n"
194 " gl_FragColor = color;\n"
197 if (!m_program->isOk())
199 m_testCtx.getLog() << *m_program;
202 TCU_FAIL("Compile failed");
206 void FlushFinishCase::deinit (void)
212 tcu::TestLog& operator<< (tcu::TestLog& log, const FlushFinishCase::Sample& sample)
214 log << TestLog::Message << sample.numDrawCalls << " calls:\t" << sample.waitTime << " us wait,\t" << sample.readPixelsTime << " us read" << TestLog::EndMessage;
218 void FlushFinishCase::setupRenderState (void)
220 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
221 const int posLoc = gl.getAttribLocation(m_program->getProgram(), "a_position");
222 const int viewportW = de::min<int>(m_context.getRenderTarget().getWidth(), MAX_VIEWPORT_SIZE);
223 const int viewportH = de::min<int>(m_context.getRenderTarget().getHeight(), MAX_VIEWPORT_SIZE);
225 static const float s_positions[] =
233 TCU_CHECK(posLoc >= 0);
235 gl.viewport(0, 0, viewportW, viewportH);
236 gl.useProgram(m_program->getProgram());
237 gl.enableVertexAttribArray(posLoc);
238 gl.vertexAttribPointer(posLoc, 2, GL_FLOAT, GL_FALSE, 0, &s_positions[0]);
240 gl.blendFunc(GL_ONE, GL_ONE);
241 gl.blendEquation(GL_FUNC_ADD);
242 GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to set up render state");
245 void FlushFinishCase::render (int numDrawCalls)
247 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
249 const deUint8 indices[] = { 0, 1, 2, 2, 1, 3 };
251 gl.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
253 for (int ndx = 0; ndx < numDrawCalls; ndx++)
254 gl.drawElements(GL_TRIANGLES, DE_LENGTH_OF_ARRAY(indices), GL_UNSIGNED_BYTE, &indices[0]);
257 void FlushFinishCase::readPixels (void)
259 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
262 gl.readPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &tmp);
265 FlushFinishCase::CalibrationParams FlushFinishCase::calibrate (void)
267 tcu::ScopedLogSection section (m_testCtx.getLog(), "CalibrationInfo", "Calibration info");
268 CalibrationParams params;
270 // Find draw call count that results in desired maximum time.
272 deUint64 prevDuration = 0;
273 int prevDrawCount = 1;
274 int curDrawCount = 1;
276 m_testCtx.getLog() << TestLog::Message << "Calibrating maximum draw call count, target duration = " << int(MAX_SAMPLE_DURATION_US) << " us" << TestLog::EndMessage;
280 deUint64 curDuration;
283 const deUint64 startTime = deGetMicroseconds();
284 render(curDrawCount);
286 curDuration = deGetMicroseconds()-startTime;
289 m_testCtx.getLog() << TestLog::Message << "Duration with " << curDrawCount << " draw calls = " << curDuration << " us" << TestLog::EndMessage;
291 if (curDuration > MAX_SAMPLE_DURATION_US)
293 if (curDrawCount > 1)
295 // Compute final count by using linear estimation.
296 const float a = float(curDuration - prevDuration) / float(curDrawCount - prevDrawCount);
297 const float b = float(prevDuration) - a*float(prevDrawCount);
298 const float est = (float(MAX_SAMPLE_DURATION_US) - b) / a;
300 curDrawCount = de::clamp(deFloorFloatToInt32(est), 1, int(MAX_DRAW_CALL_COUNT));
302 // else: Settle on 1.
306 else if (curDrawCount >= MAX_DRAW_CALL_COUNT)
307 break; // Settle on maximum.
310 prevDrawCount = curDrawCount;
311 prevDuration = curDuration;
312 curDrawCount = curDrawCount*2;
316 params.maxDrawCalls = curDrawCount;
318 m_testCtx.getLog() << TestLog::Integer("MaxDrawCalls", "Maximum number of draw calls", "", QP_KEY_TAG_NONE, params.maxDrawCalls);
322 if (params.maxDrawCalls < MIN_DRAW_CALL_COUNT)
323 throw CalibrationFailedException("Calibration failed, maximum draw call count is too low");
328 struct CompareSampleDrawCount
330 bool operator() (const FlushFinishCase::Sample& a, const FlushFinishCase::Sample& b) const { return a.numDrawCalls < b.numDrawCalls; }
333 std::vector<Vec2> getPointsFromSamples (const std::vector<FlushFinishCase::Sample>& samples, const deUint64 FlushFinishCase::Sample::*field)
335 vector<Vec2> points(samples.size());
337 for (size_t ndx = 0; ndx < samples.size(); ndx++)
338 points[ndx] = Vec2(float(samples[ndx].numDrawCalls), float(samples[ndx].*field));
344 T getMaximumValue (const std::vector<FlushFinishCase::Sample>& samples, const T FlushFinishCase::Sample::*field)
346 DE_ASSERT(!samples.empty());
348 T maxVal = samples[0].*field;
350 for (size_t ndx = 1; ndx < samples.size(); ndx++)
351 maxVal = de::max(maxVal, samples[ndx].*field);
356 void FlushFinishCase::analyzeResults (const std::vector<Sample>& samples, const CalibrationParams& calibrationParams)
358 const vector<Vec2> waitTimes = getPointsFromSamples(samples, &Sample::waitTime);
359 const vector<Vec2> readTimes = getPointsFromSamples(samples, &Sample::readPixelsTime);
360 const LineParameters waitLine = theilSenLinearRegression(waitTimes);
361 const LineParameters readLine = theilSenLinearRegression(readTimes);
362 const float normWaitCoef = waitLine.coefficient * float(calibrationParams.maxDrawCalls) / float(MAX_SAMPLE_DURATION_US);
363 const float normReadCoef = readLine.coefficient * float(calibrationParams.maxDrawCalls) / float(MAX_SAMPLE_DURATION_US);
367 tcu::ScopedLogSection section (m_testCtx.getLog(), "Samples", "Samples");
368 vector<Sample> sortedSamples (samples.begin(), samples.end());
370 std::sort(sortedSamples.begin(), sortedSamples.end(), CompareSampleDrawCount());
372 for (vector<Sample>::const_iterator iter = sortedSamples.begin(); iter != sortedSamples.end(); ++iter)
373 m_testCtx.getLog() << *iter;
376 m_testCtx.getLog() << TestLog::Float("WaitCoefficient", "Wait coefficient", "", QP_KEY_TAG_NONE, waitLine.coefficient)
377 << TestLog::Float("ReadCoefficient", "Read coefficient", "", QP_KEY_TAG_NONE, readLine.coefficient)
378 << TestLog::Float("NormalizedWaitCoefficient", "Normalized wait coefficient", "", QP_KEY_TAG_NONE, normWaitCoef)
379 << TestLog::Float("NormalizedReadCoefficient", "Normalized read coefficient", "", QP_KEY_TAG_NONE, normReadCoef);
382 const bool waitCorrelated = normWaitCoef > CORRELATED_COEF_THRESHOLD;
383 const bool readCorrelated = normReadCoef > CORRELATED_COEF_THRESHOLD;
384 const bool waitNotCorr = normWaitCoef < NO_CORR_COEF_THRESHOLD;
385 const bool readNotCorr = normReadCoef < NO_CORR_COEF_THRESHOLD;
387 if (waitCorrelated || waitNotCorr)
388 m_testCtx.getLog() << TestLog::Message << "Wait time is" << (waitCorrelated ? "" : " NOT") << " correlated to rendering workload size." << TestLog::EndMessage;
390 m_testCtx.getLog() << TestLog::Message << "Warning: Wait time correlation to rendering workload size is unclear." << TestLog::EndMessage;
392 if (readCorrelated || readNotCorr)
393 m_testCtx.getLog() << TestLog::Message << "Read time is" << (readCorrelated ? "" : " NOT") << " correlated to rendering workload size." << TestLog::EndMessage;
395 m_testCtx.getLog() << TestLog::Message << "Warning: Read time correlation to rendering workload size is unclear." << TestLog::EndMessage;
398 for (int ndx = 0; ndx < 2; ndx++)
400 const float coef = ndx == 0 ? normWaitCoef : normReadCoef;
401 const char* name = ndx == 0 ? "wait" : "read";
402 const ExpectedBehavior behavior = ndx == 0 ? m_waitBehavior : m_readBehavior;
403 const float threshold = ndx == 0 ? m_waitThreshold : m_readThreshold;
404 const bool isOk = behavior == EXPECT_COEF_GREATER_THAN ? coef > threshold :
405 behavior == EXPECT_COEF_LESS_THAN ? coef < threshold : false;
406 const char* cmpName = behavior == EXPECT_COEF_GREATER_THAN ? "greater than" :
407 behavior == EXPECT_COEF_LESS_THAN ? "less than" : DE_NULL;
411 m_testCtx.getLog() << TestLog::Message << "ERROR: Expected " << name << " coefficient to be " << cmpName << " " << threshold << TestLog::EndMessage;
416 m_testCtx.setTestResult(allOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
417 allOk ? "Pass" : "Suspicious performance behavior");
420 FlushFinishCase::IterateResult FlushFinishCase::iterate (void)
422 vector<Sample> samples (NUM_SAMPLES);
423 CalibrationParams params;
429 // Do one full render cycle.
438 params = calibrate();
440 catch (const CalibrationFailedException& e)
442 m_testCtx.setTestResult(QP_TEST_RESULT_COMPATIBILITY_WARNING, e.what());
448 de::Random rnd (123);
450 for (size_t ndx = 0; ndx < samples.size(); ndx++)
452 const int drawCallCount = rnd.getInt(1, params.maxDrawCalls);
453 deUint64 waitStartTime;
454 deUint64 readStartTime;
455 deUint64 readFinishTime;
457 render(drawCallCount);
459 waitStartTime = deGetMicroseconds();
462 readStartTime = deGetMicroseconds();
464 readFinishTime = deGetMicroseconds();
466 samples[ndx].numDrawCalls = drawCallCount;
467 samples[ndx].waitTime = readStartTime-waitStartTime;
468 samples[ndx].readPixelsTime = readFinishTime-readStartTime;
470 if (m_testCtx.getWatchDog())
471 qpWatchDog_touch(m_testCtx.getWatchDog());
475 // Analyze - sets test case result.
476 analyzeResults(samples, params);
481 class WaitOnlyCase : public FlushFinishCase
484 WaitOnlyCase (Context& context)
485 : FlushFinishCase(context, "wait", "Wait only", EXPECT_COEF_LESS_THAN, NO_CORR_COEF_THRESHOLD, EXPECT_COEF_GREATER_THAN, -1000.0f /* practically nothing is expected */)
491 m_testCtx.getLog() << TestLog::Message << int(WAIT_TIME_MS) << " ms busy wait" << TestLog::EndMessage;
492 FlushFinishCase::init();
496 void waitForGL (void)
498 busyWait(WAIT_TIME_MS);
502 class FlushOnlyCase : public FlushFinishCase
505 FlushOnlyCase (Context& context)
506 : FlushFinishCase(context, "flush", "Flush only", EXPECT_COEF_LESS_THAN, FLUSH_COEF_THRESHOLD, EXPECT_COEF_GREATER_THAN, CORRELATED_COEF_THRESHOLD)
512 m_testCtx.getLog() << TestLog::Message << "Single call to glFlush()" << TestLog::EndMessage;
513 FlushFinishCase::init();
517 void waitForGL (void)
519 m_context.getRenderContext().getFunctions().flush();
523 class FlushWaitCase : public FlushFinishCase
526 FlushWaitCase (Context& context)
527 : FlushFinishCase(context, "flush_wait", "Wait after flushing", EXPECT_COEF_LESS_THAN, FLUSH_COEF_THRESHOLD, EXPECT_COEF_LESS_THAN, NO_CORR_COEF_THRESHOLD)
533 m_testCtx.getLog() << TestLog::Message << "glFlush() followed by " << int(WAIT_TIME_MS) << " ms busy wait" << TestLog::EndMessage;
534 FlushFinishCase::init();
538 void waitForGL (void)
540 m_context.getRenderContext().getFunctions().flush();
541 busyWait(WAIT_TIME_MS);
545 class FinishOnlyCase : public FlushFinishCase
548 FinishOnlyCase (Context& context)
549 : FlushFinishCase(context, "finish", "Finish only", EXPECT_COEF_GREATER_THAN, CORRELATED_COEF_THRESHOLD, EXPECT_COEF_LESS_THAN, NO_CORR_COEF_THRESHOLD)
555 m_testCtx.getLog() << TestLog::Message << "Single call to glFinish()" << TestLog::EndMessage;
556 FlushFinishCase::init();
560 void waitForGL (void)
562 m_context.getRenderContext().getFunctions().finish();
566 class FinishWaitCase : public FlushFinishCase
569 FinishWaitCase (Context& context)
570 : FlushFinishCase(context, "finish_wait", "Finish and wait", EXPECT_COEF_GREATER_THAN, CORRELATED_COEF_THRESHOLD, EXPECT_COEF_LESS_THAN, NO_CORR_COEF_THRESHOLD)
576 m_testCtx.getLog() << TestLog::Message << "glFinish() followed by " << int(WAIT_TIME_MS) << " ms busy wait" << TestLog::EndMessage;
577 FlushFinishCase::init();
581 void waitForGL (void)
583 m_context.getRenderContext().getFunctions().finish();
584 busyWait(WAIT_TIME_MS);
590 FlushFinishTests::FlushFinishTests (Context& context)
591 : TestCaseGroup(context, "flush_finish", "Flush and Finish tests")
595 FlushFinishTests::~FlushFinishTests (void)
599 void FlushFinishTests::init (void)
601 addChild(new WaitOnlyCase (m_context));
602 addChild(new FlushOnlyCase (m_context));
603 addChild(new FlushWaitCase (m_context));
604 addChild(new FinishOnlyCase (m_context));
605 addChild(new FinishWaitCase (m_context));