xref: /aosp_15_r20/external/deqp/modules/internal/ditSRGB8ConversionTest.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
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 
31 namespace dit
32 {
33 namespace
34 {
35 
calculateDiscreteFloatDistance(float a,float b)36 uint32_t calculateDiscreteFloatDistance(float a, float b)
37 {
38     const uint32_t au = tcu::Float32(a).bits();
39     const uint32_t 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 uint32_t avalue = (au & ((0x1u << 31u) - 1u));
45     const uint32_t 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 
calculateDiscreteFloatDistance(const tcu::Vec4 & ref,const tcu::Vec4 & res)55 const 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]),
58                       calculateDiscreteFloatDistance(ref[2], res[2]), calculateDiscreteFloatDistance(ref[3], res[3]));
59 }
60 
61 class SRGB8ConversionTest : public tcu::TestCase
62 {
63 public:
SRGB8ConversionTest(tcu::TestContext & context)64     SRGB8ConversionTest(tcu::TestContext &context) : tcu::TestCase(context, "srgb8", "SRGB8 conversion test")
65     {
66     }
67 
iterate(void)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
83                     << ", Discrete float diff: " << discreteFloatDiff << tcu::TestLog::EndMessage;
84 
85             if (tcu::boolAny(tcu::greaterThan(discreteFloatDiff, tcu::UVec4(1u))))
86                 isOk = false;
87         }
88 
89         if (isOk)
90             m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
91         else
92             m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got ulp diffs greater than one.");
93 
94         return STOP;
95     }
96 };
97 
98 } // namespace
99 
createSRGB8ConversionTest(tcu::TestContext & context)100 tcu::TestCase *createSRGB8ConversionTest(tcu::TestContext &context)
101 {
102     return new SRGB8ConversionTest(context);
103 }
104 
105 } // namespace dit
106