xref: /aosp_15_r20/external/mesa3d/src/mesa/main/tests/mesa_formats.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /**
25  * \name mesa_formats.cpp
26  *
27  * Verify that all mesa formats are handled in certain functions and that
28  * the format info table is sane.
29  *
30  */
31 
32 #include <gtest/gtest.h>
33 
34 #include "main/formats.h"
35 #include "main/glformats.h"
36 #include "main/format_unpack.h"
37 #include "main/format_pack.h"
38 
39 // Test fixture for Format tests.
40 class MesaFormatsTest : public ::testing::Test {
41 };
42 
43 /**
44  * Debug/test: check that all uncompressed formats are handled in the
45  * _mesa_uncompressed_format_to_type_and_comps() function. When new pixel
46  * formats are added to Mesa, that function needs to be updated.
47  */
TEST_F(MesaFormatsTest,FormatTypeAndComps)48 TEST_F(MesaFormatsTest, FormatTypeAndComps)
49 {
50    for (int fi = MESA_FORMAT_NONE + 1; fi < MESA_FORMAT_COUNT; ++fi) {
51       mesa_format f = (mesa_format) fi;
52       SCOPED_TRACE(_mesa_get_format_name(f));
53 
54       if (!_mesa_get_format_name(f))
55          continue;
56 
57       /* This function will emit a problem/warning if the format is
58        * not handled.
59        */
60       if (!_mesa_is_format_compressed(f)) {
61          /* If the datatype is zero, the format was not handled */
62           GLenum datatype = _mesa_uncompressed_format_to_type(f);
63          EXPECT_NE(datatype, (GLenum)0);
64       }
65 
66    }
67 }
68 
69 /**
70  * Do sanity checking of the format info table.
71  */
TEST_F(MesaFormatsTest,FormatSanity)72 TEST_F(MesaFormatsTest, FormatSanity)
73 {
74    for (int fi = 0; fi < MESA_FORMAT_COUNT; ++fi) {
75       mesa_format f = (mesa_format) fi;
76       SCOPED_TRACE(_mesa_get_format_name(f));
77       if (!_mesa_get_format_name(f))
78          continue;
79 
80       GLenum datatype = _mesa_get_format_datatype(f);
81       GLint r = _mesa_get_format_bits(f, GL_RED_BITS);
82       GLint g = _mesa_get_format_bits(f, GL_GREEN_BITS);
83       GLint b = _mesa_get_format_bits(f, GL_BLUE_BITS);
84       GLint a = _mesa_get_format_bits(f, GL_ALPHA_BITS);
85       GLint l = _mesa_get_format_bits(f, GL_TEXTURE_LUMINANCE_SIZE);
86       GLint i = _mesa_get_format_bits(f, GL_TEXTURE_INTENSITY_SIZE);
87 
88       /* Note: Z32_FLOAT_X24S8 has datatype of GL_NONE */
89       EXPECT_TRUE(datatype == GL_NONE ||
90                   datatype == GL_UNSIGNED_NORMALIZED ||
91                   datatype == GL_SIGNED_NORMALIZED ||
92                   datatype == GL_UNSIGNED_INT ||
93                   datatype == GL_INT ||
94                   datatype == GL_FLOAT);
95 
96       if (r > 0 && !_mesa_is_format_compressed(f)) {
97          GLint bytes = _mesa_get_format_bytes(f);
98          EXPECT_LE((r+g+b+a) / 8, bytes);
99       }
100 
101       /* Determines if the base format has a channel [rgba] or property [li].
102       * >  indicates existance
103       * == indicates non-existance
104       */
105       #define HAS_PROP(rop,gop,bop,aop,lop,iop) \
106          do { \
107             EXPECT_TRUE(r rop 0); \
108             EXPECT_TRUE(g gop 0); \
109             EXPECT_TRUE(b bop 0); \
110             EXPECT_TRUE(a aop 0); \
111             EXPECT_TRUE(l lop 0); \
112             EXPECT_TRUE(i iop 0); \
113          } while(0)
114 
115       switch (_mesa_get_format_base_format(f)) {
116       case GL_RGBA:
117          HAS_PROP(>,>,>,>,==,==);
118          break;
119       case GL_RGB:
120          HAS_PROP(>,>,>,==,==,==);
121          break;
122       case GL_RG:
123          HAS_PROP(>,>,==,==,==,==);
124          break;
125       case GL_RED:
126          HAS_PROP(>,==,==,==,==,==);
127          break;
128       case GL_LUMINANCE:
129          HAS_PROP(==,==,==,==,>,==);
130          break;
131       case GL_INTENSITY:
132          HAS_PROP(==,==,==,==,==,>);
133          break;
134       default:
135          break;
136       }
137 
138       #undef HAS_PROP
139 
140    }
141 }
142 
TEST_F(MesaFormatsTest,IntensityToRed)143 TEST_F(MesaFormatsTest, IntensityToRed)
144 {
145    EXPECT_EQ(_mesa_get_intensity_format_red(MESA_FORMAT_I_UNORM8),
146              MESA_FORMAT_R_UNORM8);
147    EXPECT_EQ(_mesa_get_intensity_format_red(MESA_FORMAT_I_SINT32),
148              MESA_FORMAT_R_SINT32);
149    EXPECT_EQ(_mesa_get_intensity_format_red(MESA_FORMAT_R8G8B8A8_UNORM),
150              MESA_FORMAT_R8G8B8A8_UNORM);
151 }
152 
fffat_wrap(GLenum format,GLenum type)153 static mesa_format fffat_wrap(GLenum format, GLenum type)
154 {
155    uint32_t f = _mesa_format_from_format_and_type(format, type);
156    if (_mesa_format_is_mesa_array_format(f))
157       f = _mesa_format_from_array_format((mesa_array_format)f);
158    return (mesa_format)f;
159 }
160 
TEST_F(MesaFormatsTest,FormatFromFormatAndType)161 TEST_F(MesaFormatsTest, FormatFromFormatAndType)
162 {
163    EXPECT_EQ(fffat_wrap(GL_RGBA, GL_SHORT),
164              MESA_FORMAT_RGBA_SNORM16);
165    EXPECT_EQ(fffat_wrap(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT),
166              MESA_FORMAT_Z_UNORM16);
167    EXPECT_EQ(fffat_wrap(GL_STENCIL_INDEX, GL_UNSIGNED_BYTE),
168              MESA_FORMAT_S_UINT8);
169 
170    /* Should return an array format, but not a proper MESA_FORMAT. */
171    EXPECT_TRUE(_mesa_format_is_mesa_array_format(_mesa_format_from_format_and_type(GL_DEPTH_COMPONENT,
172                                                                                    GL_BYTE)));
173 }
174 
TEST_F(MesaFormatsTest,FormatMatchesFormatAndType)175 TEST_F(MesaFormatsTest, FormatMatchesFormatAndType)
176 {
177    EXPECT_TRUE(_mesa_format_matches_format_and_type(MESA_FORMAT_RGBA_UNORM16,
178                                                     GL_RGBA,
179                                                     GL_UNSIGNED_SHORT, false,
180                                                     NULL));
181    EXPECT_TRUE(_mesa_format_matches_format_and_type(MESA_FORMAT_S_UINT8,
182                                                     GL_STENCIL_INDEX,
183                                                     GL_UNSIGNED_BYTE, false,
184                                                     NULL));
185    EXPECT_TRUE(_mesa_format_matches_format_and_type(MESA_FORMAT_Z_UNORM16,
186                                                     GL_DEPTH_COMPONENT,
187                                                     GL_UNSIGNED_SHORT, false,
188                                                     NULL));
189 }
190 
191 static uint32_t
test_unpack_r8i(int8_t val)192 test_unpack_r8i(int8_t val)
193 {
194    uint32_t result[4];
195    _mesa_unpack_uint_rgba_row(MESA_FORMAT_R_SINT8, 1, &val, &result);
196    return result[0];
197 }
198 
199 static uint32_t
test_unpack_r32ui(uint32_t val)200 test_unpack_r32ui(uint32_t val)
201 {
202    uint32_t result[4];
203    _mesa_unpack_uint_rgba_row(MESA_FORMAT_R_UINT32, 1, &val, &result);
204    return result[0];
205 }
206 
TEST_F(MesaFormatsTest,UnpackRGBAUintRow)207 TEST_F(MesaFormatsTest, UnpackRGBAUintRow)
208 {
209    EXPECT_EQ(test_unpack_r8i(0), 0);
210    EXPECT_EQ(test_unpack_r8i(1), 1);
211    EXPECT_EQ(test_unpack_r8i(0xff), 0xffffffff);
212    EXPECT_EQ(test_unpack_r32ui(0), 0);
213    EXPECT_EQ(test_unpack_r32ui(0xffffffff), 0xffffffff);
214 }
215 
TEST_F(MesaFormatsTest,UnpackRGBAUbyteRowRGBA32F)216 TEST_F(MesaFormatsTest, UnpackRGBAUbyteRowRGBA32F)
217 {
218    float val[4] = {0, 0.5, -1, 2};
219    uint8_t result[4];
220    _mesa_unpack_ubyte_rgba_row(MESA_FORMAT_RGBA_FLOAT32, 1, &val, &result);
221    EXPECT_EQ(result[0], 0);
222    EXPECT_EQ(result[1], 0x80);
223    EXPECT_EQ(result[2], 0);
224    EXPECT_EQ(result[3], 0xff);
225 }
226 
TEST_F(MesaFormatsTest,UnpackRGBAUbyteRowRGBA4)227 TEST_F(MesaFormatsTest, UnpackRGBAUbyteRowRGBA4)
228 {
229    uint16_t val = (1 << 0) | (0x3f << 5) | (0x10 << 11);
230    uint8_t result[4];
231    _mesa_unpack_ubyte_rgba_row(MESA_FORMAT_R5G6B5_UNORM, 1, &val, &result);
232    EXPECT_EQ(result[0], 0x08);
233    EXPECT_EQ(result[1], 0xff);
234    EXPECT_EQ(result[2], 0x84);
235    EXPECT_EQ(result[3], 0xff);
236 }
237 
238 static float
test_unpack_floatz_z32f(float val)239 test_unpack_floatz_z32f(float val)
240 {
241    float result;
242    _mesa_unpack_float_z_row(MESA_FORMAT_Z_FLOAT32, 1, &val, &result);
243    return result;
244 }
245 
TEST_F(MesaFormatsTest,UnpackFloatZRow)246 TEST_F(MesaFormatsTest, UnpackFloatZRow)
247 {
248    EXPECT_EQ(test_unpack_floatz_z32f(0.5), 0.5);
249    EXPECT_EQ(test_unpack_floatz_z32f(-1.0), -1.0);
250    EXPECT_EQ(test_unpack_floatz_z32f(2.0), 2.0);
251 }
252 
253 static uint32_t
test_unpack_uintz_z32f(float val)254 test_unpack_uintz_z32f(float val)
255 {
256    uint32_t result;
257    _mesa_unpack_uint_z_row(MESA_FORMAT_Z_FLOAT32, 1, &val, &result);
258    return result;
259 }
260 
TEST_F(MesaFormatsTest,UnpackUintZRow)261 TEST_F(MesaFormatsTest, UnpackUintZRow)
262 {
263    EXPECT_EQ(test_unpack_uintz_z32f(0.5), 0x7fffffff);
264    EXPECT_EQ(test_unpack_uintz_z32f(-1.0), 0);
265    EXPECT_EQ(test_unpack_uintz_z32f(2.0), 0xffffffff);
266 }
267 
268 /* It's easy to have precision issues packing 32-bit floats to unorm. */
TEST_F(MesaFormatsTest,PackFloatZ)269 TEST_F(MesaFormatsTest, PackFloatZ)
270 {
271    float val = 0.571428597f;
272    uint32_t result;
273    _mesa_pack_float_z_row(MESA_FORMAT_Z_UNORM32, 1, &val, &result);
274    EXPECT_EQ(result, 0x924924ff);
275 }
276 
TEST_F(MesaFormatsTest,PackUbyteRGBARounding)277 TEST_F(MesaFormatsTest, PackUbyteRGBARounding)
278 {
279    for (int i = 0; i <= 255; i++) {
280       uint8_t val[4] = {(uint8_t)i, 0, 0, 0};
281       uint16_t result;
282       _mesa_pack_ubyte_rgba_row(MESA_FORMAT_R5G6B5_UNORM, 1, val, &result);
283       EXPECT_EQ(result, (i * 31 + 127) / 255);
284    }
285 }
286