1#ifndef _GLSCALIBRATION_HPP
2#define _GLSCALIBRATION_HPP
3/*-------------------------------------------------------------------------
4 * drawElements Quality Program OpenGL (ES) Module
5 * -----------------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Calibration tools.
24 *//*--------------------------------------------------------------------*/
25
26#include "tcuDefs.hpp"
27#include "tcuTestCase.hpp"
28#include "tcuTestLog.hpp"
29#include "tcuVector.hpp"
30#include "gluRenderContext.hpp"
31
32#include <limits>
33
34namespace deqp
35{
36namespace gls
37{
38
39struct LineParameters
40{
41	float offset;
42	float coefficient;
43
44	LineParameters (float offset_, float coefficient_) : offset(offset_), coefficient(coefficient_) {}
45};
46
47// Basic Theil-Sen linear estimate. Calculates median of all possible slope coefficients through two of the data points
48// and median of offsets corresponding with the median slope
49LineParameters theilSenLinearRegression (const std::vector<tcu::Vec2>& dataPoints);
50
51struct LineParametersWithConfidence
52{
53	float offset;
54	float offsetConfidenceUpper;
55	float offsetConfidenceLower;
56
57	float coefficient;
58	float coefficientConfidenceUpper;
59	float coefficientConfidenceLower;
60
61	float confidence;
62};
63
64// Median-of-medians version of Theil-Sen estimate. Calculates median of medians of slopes through a point and all other points.
65// Confidence interval is given as the range that contains the given fraction of all slopes/offsets
66LineParametersWithConfidence theilSenSiegelLinearRegression (const std::vector<tcu::Vec2>& dataPoints, float reportedConfidence);
67
68struct MeasureState
69{
70	MeasureState (void)
71		: maxNumFrames			(0)
72		, frameShortcutTime		(std::numeric_limits<float>::infinity())
73		, numDrawCalls			(0)
74	{
75	}
76
77	void		clear				(void);
78	void		start				(int maxNumFrames, float frameShortcutTime, int numDrawCalls);
79
80	bool		isDone				(void) const;
81	deUint64	getTotalTime		(void) const;
82
83	int						maxNumFrames;
84	float					frameShortcutTime;
85	int						numDrawCalls;
86	std::vector<deUint64>	frameTimes;
87};
88
89struct CalibrateIteration
90{
91	CalibrateIteration (int numDrawCalls_, float frameTime_)
92		: numDrawCalls	(numDrawCalls_)
93		, frameTime		(frameTime_)
94	{
95	}
96
97	CalibrateIteration (void)
98		: numDrawCalls	(0)
99		, frameTime		(0.0f)
100	{
101	}
102
103	int		numDrawCalls;
104	float	frameTime;
105};
106
107struct CalibratorParameters
108{
109	CalibratorParameters (int		numInitialCalls_,
110						  int		maxCalibrateIterationFrames_,			//!< Maximum (and default) number of frames per one calibrate iteration.
111						  float		calibrateIterationShortcutThresholdMs_,	//!< If the times of two consecutive frames exceed this, stop the iteration even if maxCalibrateIterationFrames isn't reached.
112						  int		maxCalibrateIterations_,
113						  float		targetFrameTimeMs_,
114						  float		frameTimeCapMs_,
115						  float		targetMeasureDurationMs_)
116		: numInitialCalls						(numInitialCalls_)
117		, maxCalibrateIterationFrames			(maxCalibrateIterationFrames_)
118		, calibrateIterationShortcutThreshold	(1000.0f*calibrateIterationShortcutThresholdMs_)
119		, maxCalibrateIterations				(maxCalibrateIterations_)
120		, targetFrameTimeUs						(1000.0f*targetFrameTimeMs_)
121		, frameTimeCapUs						(1000.0f*frameTimeCapMs_)
122		, targetMeasureDurationUs				(1000.0f*targetMeasureDurationMs_)
123	{
124	}
125
126	int		numInitialCalls;
127	int		maxCalibrateIterationFrames;
128	float	calibrateIterationShortcutThreshold;
129	int		maxCalibrateIterations;
130	float	targetFrameTimeUs;
131	float	frameTimeCapUs;
132	float	targetMeasureDurationUs;
133};
134
135class TheilSenCalibrator
136{
137public:
138	enum State
139	{
140		STATE_RECOMPUTE_PARAMS = 0,
141		STATE_MEASURE,
142		STATE_FINISHED,
143
144		STATE_LAST
145	};
146
147											TheilSenCalibrator		(void);
148											TheilSenCalibrator		(const CalibratorParameters& params);
149											~TheilSenCalibrator		(void);
150
151	void									clear					(void);
152	void									clear					(const CalibratorParameters& params);
153
154	State									getState				(void) const;
155	int										getCallCount			(void) const { return m_measureState.numDrawCalls;	}
156
157	// Should be called when getState() returns STATE_RECOMPUTE_PARAMS
158	void									recomputeParameters		(void);
159
160	// Should be called when getState() returns STATE_MEASURE
161	void									recordIteration			(deUint64 frameTime);
162
163	const CalibratorParameters&				getParameters			(void) const { return m_params;					}
164	const MeasureState&						getMeasureState			(void) const { return m_measureState;			}
165	const std::vector<CalibrateIteration>&	getCalibrationInfo		(void) const { return m_calibrateIterations;	}
166
167private:
168	enum InternalState
169	{
170		INTERNALSTATE_CALIBRATING = 0,
171		INTERNALSTATE_RUNNING,
172		INTERNALSTATE_FINISHED,
173
174		INTERNALSTATE_LAST
175	};
176
177	CalibratorParameters					m_params;
178
179	InternalState							m_state;
180	MeasureState							m_measureState;
181
182	std::vector<CalibrateIteration>			m_calibrateIterations;
183};
184
185void logCalibrationInfo (tcu::TestLog& log, const TheilSenCalibrator& calibrator);
186
187} // gls
188} // deqp
189
190#endif // _GLSCALIBRATION_HPP
191