mesa: Refactor the entirety of _mesa_format_matches_format_and_type().
[mesa.git] / src / mesa / main / tests / mesa_formats.cpp
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
37 /**
38 * Debug/test: check that all uncompressed formats are handled in the
39 * _mesa_uncompressed_format_to_type_and_comps() function. When new pixel
40 * formats are added to Mesa, that function needs to be updated.
41 */
42 TEST(MesaFormatsTest, FormatTypeAndComps)
43 {
44 for (int fi = MESA_FORMAT_NONE + 1; fi < MESA_FORMAT_COUNT; ++fi) {
45 mesa_format f = (mesa_format) fi;
46 SCOPED_TRACE(_mesa_get_format_name(f));
47
48 /* This function will emit a problem/warning if the format is
49 * not handled.
50 */
51 if (!_mesa_is_format_compressed(f)) {
52 GLenum datatype = 0;
53 GLuint comps = 0;
54
55 /* If the datatype is zero, the format was not handled */
56 _mesa_uncompressed_format_to_type_and_comps(f, &datatype, &comps);
57 EXPECT_NE(datatype, (GLenum)0);
58 }
59
60 }
61 }
62
63 /**
64 * Do sanity checking of the format info table.
65 */
66 TEST(MesaFormatsTest, FormatSanity)
67 {
68 for (int fi = 0; fi < MESA_FORMAT_COUNT; ++fi) {
69 mesa_format f = (mesa_format) fi;
70 SCOPED_TRACE(_mesa_get_format_name(f));
71 GLenum datatype = _mesa_get_format_datatype(f);
72 GLint r = _mesa_get_format_bits(f, GL_RED_BITS);
73 GLint g = _mesa_get_format_bits(f, GL_GREEN_BITS);
74 GLint b = _mesa_get_format_bits(f, GL_BLUE_BITS);
75 GLint a = _mesa_get_format_bits(f, GL_ALPHA_BITS);
76 GLint l = _mesa_get_format_bits(f, GL_TEXTURE_LUMINANCE_SIZE);
77 GLint i = _mesa_get_format_bits(f, GL_TEXTURE_INTENSITY_SIZE);
78
79 /* Note: Z32_FLOAT_X24S8 has datatype of GL_NONE */
80 EXPECT_TRUE(datatype == GL_NONE ||
81 datatype == GL_UNSIGNED_NORMALIZED ||
82 datatype == GL_SIGNED_NORMALIZED ||
83 datatype == GL_UNSIGNED_INT ||
84 datatype == GL_INT ||
85 datatype == GL_FLOAT);
86
87 if (r > 0 && !_mesa_is_format_compressed(f)) {
88 GLint bytes = _mesa_get_format_bytes(f);
89 EXPECT_LE((r+g+b+a) / 8, bytes);
90 }
91
92 /* Determines if the base format has a channel [rgba] or property [li].
93 * > indicates existance
94 * == indicates non-existance
95 */
96 #define HAS_PROP(rop,gop,bop,aop,lop,iop) \
97 do { \
98 EXPECT_TRUE(r rop 0); \
99 EXPECT_TRUE(g gop 0); \
100 EXPECT_TRUE(b bop 0); \
101 EXPECT_TRUE(a aop 0); \
102 EXPECT_TRUE(l lop 0); \
103 EXPECT_TRUE(i iop 0); \
104 } while(0)
105
106 switch (_mesa_get_format_base_format(f)) {
107 case GL_RGBA:
108 HAS_PROP(>,>,>,>,==,==);
109 break;
110 case GL_RGB:
111 HAS_PROP(>,>,>,==,==,==);
112 break;
113 case GL_RG:
114 HAS_PROP(>,>,==,==,==,==);
115 break;
116 case GL_RED:
117 HAS_PROP(>,==,==,==,==,==);
118 break;
119 case GL_LUMINANCE:
120 HAS_PROP(==,==,==,==,>,==);
121 break;
122 case GL_INTENSITY:
123 HAS_PROP(==,==,==,==,==,>);
124 break;
125 default:
126 break;
127 }
128
129 #undef HAS_PROP
130
131 }
132 }
133
134 TEST(MesaFormatsTest, IntensityToRed)
135 {
136 EXPECT_EQ(_mesa_get_intensity_format_red(MESA_FORMAT_I_UNORM8),
137 MESA_FORMAT_R_UNORM8);
138 EXPECT_EQ(_mesa_get_intensity_format_red(MESA_FORMAT_I_SINT32),
139 MESA_FORMAT_R_SINT32);
140 EXPECT_EQ(_mesa_get_intensity_format_red(MESA_FORMAT_R8G8B8A8_UNORM),
141 MESA_FORMAT_R8G8B8A8_UNORM);
142 }
143
144 static mesa_format fffat_wrap(GLenum format, GLenum type)
145 {
146 uint32_t f = _mesa_format_from_format_and_type(format, type);
147 if (_mesa_format_is_mesa_array_format(f))
148 f = _mesa_format_from_array_format((mesa_array_format)f);
149 return (mesa_format)f;
150 }
151
152 TEST(MesaFormatsTest, FormatFromFormatAndType)
153 {
154 EXPECT_EQ(fffat_wrap(GL_RGBA, GL_SHORT),
155 MESA_FORMAT_RGBA_SNORM16);
156 EXPECT_EQ(fffat_wrap(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT),
157 MESA_FORMAT_Z_UNORM16);
158 EXPECT_EQ(fffat_wrap(GL_STENCIL_INDEX, GL_UNSIGNED_BYTE),
159 MESA_FORMAT_S_UINT8);
160
161 /* Should return an array format, but not a proper MESA_FORMAT. */
162 EXPECT_TRUE(_mesa_format_is_mesa_array_format(_mesa_format_from_format_and_type(GL_DEPTH_COMPONENT,
163 GL_BYTE)));
164 }
165
166 TEST(MesaFormatsTest, FormatMatchesFormatAndType)
167 {
168 EXPECT_TRUE(_mesa_format_matches_format_and_type(MESA_FORMAT_RGBA_UNORM16,
169 GL_RGBA,
170 GL_UNSIGNED_SHORT, false,
171 NULL));
172 EXPECT_TRUE(_mesa_format_matches_format_and_type(MESA_FORMAT_S_UINT8,
173 GL_STENCIL_INDEX,
174 GL_UNSIGNED_BYTE, false,
175 NULL));
176 EXPECT_TRUE(_mesa_format_matches_format_and_type(MESA_FORMAT_Z_UNORM16,
177 GL_DEPTH_COMPONENT,
178 GL_UNSIGNED_SHORT, false,
179 NULL));
180 }