1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
9 #include <GLES2/gl2ext.h>
10 #include <GLES2/gl2extchromium.h>
12 #include "testing/gtest/include/gtest/gtest.h"
17 class GLES2UtilTest
: public testing:: Test
{
22 TEST_F(GLES2UtilTest
, SafeMultiplyUint32
) {
24 EXPECT_TRUE(SafeMultiplyUint32(2u, 3u, &result
));
25 EXPECT_EQ(6u, result
);
26 EXPECT_FALSE(SafeMultiplyUint32(0x80000000u
, 2u, &result
));
27 EXPECT_EQ(0u, result
);
28 EXPECT_TRUE(SafeMultiplyUint32(0x2u
, 0x7FFFFFFFu
, &result
));
29 EXPECT_EQ(0xFFFFFFFEu
, result
);
30 EXPECT_FALSE(SafeMultiplyUint32(2u, 0x80000000u
, &result
));
31 EXPECT_EQ(0u, result
);
34 TEST_F(GLES2UtilTest
, SafeAddUint32
) {
36 EXPECT_TRUE(SafeAddUint32(2u, 3u, &result
));
37 EXPECT_EQ(5u, result
);
38 EXPECT_FALSE(SafeAddUint32(0x80000000u
, 0x80000000u
, &result
));
39 EXPECT_EQ(0u, result
);
40 EXPECT_TRUE(SafeAddUint32(0xFFFFFFFEu
, 0x1u
, &result
));
41 EXPECT_EQ(0xFFFFFFFFu
, result
);
42 EXPECT_FALSE(SafeAddUint32(0xFFFFFFFEu
, 0x2u
, &result
));
43 EXPECT_EQ(0u, result
);
44 EXPECT_TRUE(SafeAddUint32(0x1u
, 0xFFFFFFFEu
, &result
));
45 EXPECT_EQ(0xFFFFFFFFu
, result
);
46 EXPECT_FALSE(SafeAddUint32(0x2u
, 0xFFFFFFFEu
, &result
));
47 EXPECT_EQ(0u, result
);
50 TEST_F(GLES2UtilTest
, SafeAddInt32
) {
52 const int32_t kMax
= std::numeric_limits
<int32_t>::max();
53 const int32_t kMin
= std::numeric_limits
<int32_t>::min();
54 EXPECT_TRUE(SafeAddInt32(2, 3, &result
));
56 EXPECT_FALSE(SafeAddInt32(kMax
, 1, &result
));
58 EXPECT_TRUE(SafeAddInt32(kMin
+ 1, -1, &result
));
59 EXPECT_EQ(kMin
, result
);
60 EXPECT_FALSE(SafeAddInt32(kMin
, -1, &result
));
62 EXPECT_TRUE(SafeAddInt32(kMax
- 1, 1, &result
));
63 EXPECT_EQ(kMax
, result
);
64 EXPECT_FALSE(SafeAddInt32(1, kMax
, &result
));
66 EXPECT_TRUE(SafeAddInt32(-1, kMin
+ 1, &result
));
67 EXPECT_EQ(kMin
, result
);
68 EXPECT_FALSE(SafeAddInt32(-1, kMin
, &result
));
70 EXPECT_TRUE(SafeAddInt32(1, kMax
- 1, &result
));
71 EXPECT_EQ(kMax
, result
);
74 TEST_F(GLES2UtilTest
, GLGetNumValuesReturned
) {
75 EXPECT_EQ(0, util_
.GLGetNumValuesReturned(GL_COMPRESSED_TEXTURE_FORMATS
));
76 EXPECT_EQ(0, util_
.GLGetNumValuesReturned(GL_SHADER_BINARY_FORMATS
));
78 EXPECT_EQ(0, util_
.num_compressed_texture_formats());
79 EXPECT_EQ(0, util_
.num_shader_binary_formats());
81 util_
.set_num_compressed_texture_formats(1);
82 util_
.set_num_shader_binary_formats(2);
84 EXPECT_EQ(1, util_
.GLGetNumValuesReturned(GL_COMPRESSED_TEXTURE_FORMATS
));
85 EXPECT_EQ(2, util_
.GLGetNumValuesReturned(GL_SHADER_BINARY_FORMATS
));
87 EXPECT_EQ(1, util_
.num_compressed_texture_formats());
88 EXPECT_EQ(2, util_
.num_shader_binary_formats());
91 TEST_F(GLES2UtilTest
, ComputeImageDataSizesFormats
) {
92 const uint32_t kWidth
= 16;
93 const uint32_t kHeight
= 12;
95 uint32_t unpadded_row_size
;
96 uint32_t padded_row_size
;
97 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
98 kWidth
, kHeight
, GL_RGB
, GL_UNSIGNED_BYTE
, 1, &size
, &unpadded_row_size
,
100 EXPECT_EQ(kWidth
* kHeight
* 3, size
);
101 EXPECT_EQ(kWidth
* 3, padded_row_size
);
102 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
103 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
104 kWidth
, kHeight
, GL_RGBA
, GL_UNSIGNED_BYTE
, 1, &size
, &unpadded_row_size
,
106 EXPECT_EQ(kWidth
* kHeight
* 4, size
);
107 EXPECT_EQ(kWidth
* 4, padded_row_size
);
108 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
109 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
110 kWidth
, kHeight
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, 1, &size
,
111 &unpadded_row_size
, &padded_row_size
));
112 EXPECT_EQ(kWidth
* kHeight
* 1, size
);
113 EXPECT_EQ(kWidth
* 1, padded_row_size
);
114 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
115 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
116 kWidth
, kHeight
, GL_LUMINANCE_ALPHA
, GL_UNSIGNED_BYTE
, 1, &size
,
117 &unpadded_row_size
, &padded_row_size
));
118 EXPECT_EQ(kWidth
* kHeight
* 2, size
);
119 EXPECT_EQ(kWidth
* 2, padded_row_size
);
120 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
121 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
122 kWidth
, kHeight
, GL_BGRA_EXT
, GL_UNSIGNED_BYTE
, 1, &size
,
123 &unpadded_row_size
, &padded_row_size
));
124 EXPECT_EQ(kWidth
* kHeight
* 4, size
);
125 EXPECT_EQ(kWidth
* 4, padded_row_size
);
126 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
127 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
128 kWidth
, kHeight
, GL_ALPHA
, GL_UNSIGNED_BYTE
, 1, &size
, &unpadded_row_size
,
130 EXPECT_EQ(kWidth
* kHeight
* 1, size
);
131 EXPECT_EQ(kWidth
* 1, padded_row_size
);
132 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
133 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
134 kWidth
, kHeight
, GL_DEPTH_COMPONENT
, GL_UNSIGNED_SHORT
, 1, &size
,
135 &unpadded_row_size
, &padded_row_size
));
136 EXPECT_EQ(kWidth
* kHeight
* 2, size
);
137 EXPECT_EQ(kWidth
* 2, padded_row_size
);
138 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
139 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
140 kWidth
, kHeight
, GL_DEPTH_STENCIL_OES
, GL_UNSIGNED_INT_24_8_OES
, 1,
141 &size
, &unpadded_row_size
,
143 EXPECT_EQ(kWidth
* kHeight
* 4, size
);
144 EXPECT_EQ(kWidth
* 4, padded_row_size
);
145 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
148 TEST_F(GLES2UtilTest
, ComputeImageDataSizeTypes
) {
149 const uint32_t kWidth
= 16;
150 const uint32_t kHeight
= 12;
152 uint32_t unpadded_row_size
;
153 uint32_t padded_row_size
;
154 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
155 kWidth
, kHeight
, GL_RGBA
, GL_UNSIGNED_BYTE
, 1, &size
, &unpadded_row_size
,
157 EXPECT_EQ(kWidth
* kHeight
* 4, size
);
158 EXPECT_EQ(kWidth
* 4, padded_row_size
);
159 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
160 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
161 kWidth
, kHeight
, GL_RGBA
, GL_UNSIGNED_SHORT_4_4_4_4
, 1, &size
,
162 &unpadded_row_size
, &padded_row_size
));
163 EXPECT_EQ(kWidth
* kHeight
* 2, size
);
164 EXPECT_EQ(kWidth
* 2, padded_row_size
);
165 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
166 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
167 kWidth
, kHeight
, GL_RGBA
, GL_UNSIGNED_SHORT_5_5_5_1
, 1, &size
,
168 &unpadded_row_size
, &padded_row_size
));
169 EXPECT_EQ(kWidth
* kHeight
* 2, size
);
170 EXPECT_EQ(kWidth
* 2, padded_row_size
);
171 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
172 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
173 kWidth
, kHeight
, GL_RGB
, GL_UNSIGNED_SHORT_5_6_5
, 1, &size
,
174 &unpadded_row_size
, &padded_row_size
));
175 EXPECT_EQ(kWidth
* kHeight
* 2, size
);
176 EXPECT_EQ(kWidth
* 2, padded_row_size
);
177 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
178 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
179 kWidth
, kHeight
, GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, 1, &size
,
180 &unpadded_row_size
, &padded_row_size
));
181 EXPECT_EQ(kWidth
* kHeight
* 4, size
);
182 EXPECT_EQ(kWidth
* 4, padded_row_size
);
183 EXPECT_EQ(padded_row_size
, unpadded_row_size
);
186 TEST_F(GLES2UtilTest
, ComputeImageDataSizesUnpackAlignment
) {
187 const uint32_t kWidth
= 19;
188 const uint32_t kHeight
= 12;
190 uint32_t unpadded_row_size
;
191 uint32_t padded_row_size
;
192 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
193 kWidth
, kHeight
, GL_RGB
, GL_UNSIGNED_BYTE
, 1, &size
, &unpadded_row_size
,
195 EXPECT_EQ(kWidth
* kHeight
* 3, size
);
196 EXPECT_EQ(kWidth
* 3, unpadded_row_size
);
197 EXPECT_EQ(kWidth
* 3, padded_row_size
);
198 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
199 kWidth
, kHeight
, GL_RGB
, GL_UNSIGNED_BYTE
, 2, &size
, &unpadded_row_size
,
201 EXPECT_EQ((kWidth
* 3 + 1) * (kHeight
- 1) +
203 EXPECT_EQ(kWidth
* 3, unpadded_row_size
);
204 EXPECT_EQ(kWidth
* 3 + 1, padded_row_size
);
205 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
206 kWidth
, kHeight
, GL_RGB
, GL_UNSIGNED_BYTE
, 4, &size
, &unpadded_row_size
,
208 EXPECT_EQ((kWidth
* 3 + 3) * (kHeight
- 1) +
210 EXPECT_EQ(kWidth
* 3, unpadded_row_size
);
211 EXPECT_EQ(kWidth
* 3 + 3, padded_row_size
);
212 EXPECT_TRUE(GLES2Util::ComputeImageDataSizes(
213 kWidth
, kHeight
, GL_RGB
, GL_UNSIGNED_BYTE
, 8, &size
, &unpadded_row_size
,
215 EXPECT_EQ((kWidth
* 3 + 7) * (kHeight
- 1) +
217 EXPECT_EQ(kWidth
* 3, unpadded_row_size
);
218 EXPECT_EQ(kWidth
* 3 + 7, padded_row_size
);
221 TEST_F(GLES2UtilTest
, RenderbufferBytesPerPixel
) {
222 EXPECT_EQ(1u, GLES2Util::RenderbufferBytesPerPixel(GL_STENCIL_INDEX8
));
223 EXPECT_EQ(2u, GLES2Util::RenderbufferBytesPerPixel(GL_RGBA4
));
224 EXPECT_EQ(2u, GLES2Util::RenderbufferBytesPerPixel(GL_RGB565
));
225 EXPECT_EQ(2u, GLES2Util::RenderbufferBytesPerPixel(GL_RGB5_A1
));
226 EXPECT_EQ(2u, GLES2Util::RenderbufferBytesPerPixel(GL_DEPTH_COMPONENT16
));
227 EXPECT_EQ(4u, GLES2Util::RenderbufferBytesPerPixel(GL_RGB
));
228 EXPECT_EQ(4u, GLES2Util::RenderbufferBytesPerPixel(GL_RGBA
));
230 4u, GLES2Util::RenderbufferBytesPerPixel(GL_DEPTH24_STENCIL8_OES
));
231 EXPECT_EQ(4u, GLES2Util::RenderbufferBytesPerPixel(GL_RGB8_OES
));
232 EXPECT_EQ(4u, GLES2Util::RenderbufferBytesPerPixel(GL_RGBA8_OES
));
234 4u, GLES2Util::RenderbufferBytesPerPixel(GL_DEPTH_COMPONENT24_OES
));
235 EXPECT_EQ(0u, GLES2Util::RenderbufferBytesPerPixel(-1));
238 TEST_F(GLES2UtilTest
, GetChannelsForCompressedFormat
) {
239 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(GL_ETC1_RGB8_OES
));
240 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(
241 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
));
242 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(
243 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
));
244 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(
245 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
));
246 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(
247 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
));
248 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(GL_ATC_RGB_AMD
));
249 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(
250 GL_ATC_RGBA_EXPLICIT_ALPHA_AMD
));
251 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(
252 GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD
));
253 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(
254 GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG
));
255 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(
256 GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG
));
257 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(
258 GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
));
259 EXPECT_EQ(0u, GLES2Util::GetChannelsForFormat(
260 GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
));
265 void CheckParseUniformName(
267 bool expected_success
,
268 size_t expected_array_pos
,
270 bool expected_getting_array
) {
272 size_t array_pos
= 1244;
273 bool getting_array
= false;
274 bool success
= GLES2Util::ParseUniformName(
275 name
, &array_pos
, &index
, &getting_array
);
276 EXPECT_EQ(expected_success
, success
);
278 EXPECT_EQ(expected_array_pos
, array_pos
);
279 EXPECT_EQ(expected_index
, index
);
280 EXPECT_EQ(expected_getting_array
, getting_array
);
284 } // anonymous namespace
286 TEST_F(GLES2UtilTest
, ParseUniformName
) {
287 CheckParseUniformName("u_name", true, std::string::npos
, 0, false);
288 CheckParseUniformName("u_name[]", false, std::string::npos
, 0, false);
289 CheckParseUniformName("u_name]", false, std::string::npos
, 0, false);
290 CheckParseUniformName("u_name[0a]", false, std::string::npos
, 0, false);
291 CheckParseUniformName("u_name[a0]", false, std::string::npos
, 0, false);
292 CheckParseUniformName("u_name[0a0]", false, std::string::npos
, 0, false);
293 CheckParseUniformName("u_name[0]", true, 6u, 0, true);
294 CheckParseUniformName("u_name[2]", true, 6u, 2, true);
295 CheckParseUniformName("u_name[02]", true, 6u, 2, true);
296 CheckParseUniformName("u_name[20]", true, 6u, 20, true);
297 CheckParseUniformName("u_name[020]", true, 6u, 20, true);
298 CheckParseUniformName("u_name[0][0]", true, 9u, 0, true);
299 CheckParseUniformName("u_name[3][2]", true, 9u, 2, true);
300 CheckParseUniformName("u_name[03][02]", true, 10u, 2, true);
301 CheckParseUniformName("u_name[30][20]", true, 10u, 20, true);
302 CheckParseUniformName("u_name[030][020]", true, 11u, 20, true);