1/*-------------------------------------------------------------------------
2 * drawElements Internal Test Module
3 * ---------------------------------
4 *
5 * Copyright 2015 The Android Open Source Project
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 *//*!
20 * \file
21 * \brief 8-bit sRGB conversion test.
22 *//*--------------------------------------------------------------------*/
23
24#include "ditSRGB8ConversionTest.hpp"
25
26#include "tcuFloat.hpp"
27#include "tcuTestLog.hpp"
28#include "tcuTextureUtil.hpp"
29#include "tcuVectorUtil.hpp"
30
31namespace dit
32{
33namespace
34{
35
36deUint32 calculateDiscreteFloatDistance (float a, float b)
37{
38	const deUint32		au		= tcu::Float32(a).bits();
39	const deUint32		bu		= tcu::Float32(b).bits();
40
41	const bool			asign	= (au & (0x1u << 31u)) != 0u;
42	const bool			bsign	= (bu & (0x1u << 31u)) != 0u;
43
44	const deUint32		avalue	= (au & ((0x1u << 31u) - 1u));
45	const deUint32		bvalue	= (bu & ((0x1u << 31u) - 1u));
46
47	if (asign != bsign)
48		return avalue + bvalue + 1u;
49	else if (avalue < bvalue)
50		return bvalue - avalue;
51	else
52		return avalue - bvalue;
53}
54
55const tcu::UVec4 calculateDiscreteFloatDistance (const tcu::Vec4& ref, const tcu::Vec4& res)
56{
57	return tcu::UVec4(calculateDiscreteFloatDistance(ref[0], res[0]), calculateDiscreteFloatDistance(ref[1], res[1]), calculateDiscreteFloatDistance(ref[2], res[2]), calculateDiscreteFloatDistance(ref[3], res[3]));
58}
59
60class SRGB8ConversionTest : public tcu::TestCase
61{
62public:
63	SRGB8ConversionTest (tcu::TestContext& context)
64		: tcu::TestCase	(context, "srgb8", "SRGB8 conversion test")
65	{
66	}
67
68	IterateResult iterate (void)
69	{
70		bool			isOk	= true;
71		tcu::TestLog&	log		= m_testCtx.getLog();
72
73		for (int i = 0; i < 256; i++)
74		{
75			const tcu::UVec4	src					(i);
76			const tcu::Vec4		res					(tcu::sRGBA8ToLinear(src));
77			const tcu::Vec4		ref					(tcu::sRGBToLinear(src.cast<float>() / tcu::Vec4(255.0f)));
78			const tcu::Vec4		diff				(res - ref);
79			const tcu::UVec4	discreteFloatDiff	(calculateDiscreteFloatDistance(ref, res));
80
81			if (tcu::anyNotEqual(res, ref))
82				log << tcu::TestLog::Message << i << ", Res: " << res << ", Ref: " << ref << ", Diff: " << diff << ", Discrete float diff: " << discreteFloatDiff << tcu::TestLog::EndMessage;
83
84			if (tcu::boolAny(tcu::greaterThan(discreteFloatDiff, tcu::UVec4(1u))))
85				isOk = false;
86		}
87
88		if (isOk)
89			m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
90		else
91			m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got ulp diffs greater than one.");
92
93		return STOP;
94	}
95};
96
97} // anonymous
98
99tcu::TestCase* createSRGB8ConversionTest (tcu::TestContext& context)
100{
101	return new SRGB8ConversionTest(context);
102}
103
104} // dit
105