xref: /aosp_15_r20/external/angle/src/tests/gl_tests/KTXCompressedTextureTest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2022 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker 
7*8975f5c5SAndroid Build Coastguard Worker // KTXCompressedTextureTest.cpp: Tests of reading compressed texture stored in
8*8975f5c5SAndroid Build Coastguard Worker // .ktx formats
9*8975f5c5SAndroid Build Coastguard Worker 
10*8975f5c5SAndroid Build Coastguard Worker #include "image_util/loadimage.h"
11*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/ANGLETest.h"
12*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/gl_raii.h"
13*8975f5c5SAndroid Build Coastguard Worker 
14*8975f5c5SAndroid Build Coastguard Worker #include "media/pixel.inc"
15*8975f5c5SAndroid Build Coastguard Worker 
16*8975f5c5SAndroid Build Coastguard Worker using namespace angle;
17*8975f5c5SAndroid Build Coastguard Worker 
18*8975f5c5SAndroid Build Coastguard Worker class KTXCompressedTextureTest : public ANGLETest<>
19*8975f5c5SAndroid Build Coastguard Worker {
20*8975f5c5SAndroid Build Coastguard Worker   protected:
KTXCompressedTextureTest()21*8975f5c5SAndroid Build Coastguard Worker     KTXCompressedTextureTest()
22*8975f5c5SAndroid Build Coastguard Worker     {
23*8975f5c5SAndroid Build Coastguard Worker         setWindowWidth(768);
24*8975f5c5SAndroid Build Coastguard Worker         setWindowHeight(512);
25*8975f5c5SAndroid Build Coastguard Worker         setConfigRedBits(8);
26*8975f5c5SAndroid Build Coastguard Worker         setConfigGreenBits(8);
27*8975f5c5SAndroid Build Coastguard Worker         setConfigBlueBits(8);
28*8975f5c5SAndroid Build Coastguard Worker         setConfigAlphaBits(8);
29*8975f5c5SAndroid Build Coastguard Worker     }
30*8975f5c5SAndroid Build Coastguard Worker };
31*8975f5c5SAndroid Build Coastguard Worker 
32*8975f5c5SAndroid Build Coastguard Worker // Verify that ANGLE can store and sample the ETC1 compressed texture stored in
33*8975f5c5SAndroid Build Coastguard Worker // KTX container
TEST_P(KTXCompressedTextureTest,CompressedTexImageETC1)34*8975f5c5SAndroid Build Coastguard Worker TEST_P(KTXCompressedTextureTest, CompressedTexImageETC1)
35*8975f5c5SAndroid Build Coastguard Worker {
36*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_compressed_ETC1_RGB8_texture"));
37*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(textureProgram, essl1_shaders::vs::Texture2D(),
38*8975f5c5SAndroid Build Coastguard Worker                      essl1_shaders::fs::Texture2D());
39*8975f5c5SAndroid Build Coastguard Worker     glUseProgram(textureProgram);
40*8975f5c5SAndroid Build Coastguard Worker     GLTexture compressedETC1Texture;
41*8975f5c5SAndroid Build Coastguard Worker     glBindTexture(GL_TEXTURE_2D, compressedETC1Texture);
42*8975f5c5SAndroid Build Coastguard Worker     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
43*8975f5c5SAndroid Build Coastguard Worker     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
44*8975f5c5SAndroid Build Coastguard Worker     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
45*8975f5c5SAndroid Build Coastguard Worker     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
46*8975f5c5SAndroid Build Coastguard Worker     glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_ETC1_RGB8_OES, ktx_etc1_width, ktx_etc1_height, 0,
47*8975f5c5SAndroid Build Coastguard Worker                            ktx_etc1_size, ktx_etc1_data);
48*8975f5c5SAndroid Build Coastguard Worker     EXPECT_GL_NO_ERROR();
49*8975f5c5SAndroid Build Coastguard Worker     GLint textureUniformLocation =
50*8975f5c5SAndroid Build Coastguard Worker         glGetUniformLocation(textureProgram, essl1_shaders::Texture2DUniform());
51*8975f5c5SAndroid Build Coastguard Worker     glUniform1i(textureUniformLocation, 0);
52*8975f5c5SAndroid Build Coastguard Worker     drawQuad(textureProgram, essl1_shaders::PositionAttrib(), 0.5f);
53*8975f5c5SAndroid Build Coastguard Worker     EXPECT_GL_NO_ERROR();
54*8975f5c5SAndroid Build Coastguard Worker 
55*8975f5c5SAndroid Build Coastguard Worker     // Uncompress ETC1 texture data to RGBA texture data
56*8975f5c5SAndroid Build Coastguard Worker     constexpr size_t kDecompressedPixelCount    = ktx_etc1_width * ktx_etc1_height;
57*8975f5c5SAndroid Build Coastguard Worker     constexpr size_t kDecompressedBytesPerPixel = 4;
58*8975f5c5SAndroid Build Coastguard Worker     std::vector<GLubyte> decompressedTextureData(
59*8975f5c5SAndroid Build Coastguard Worker         kDecompressedPixelCount * kDecompressedBytesPerPixel, 0);
60*8975f5c5SAndroid Build Coastguard Worker     LoadETC1RGB8ToRGBA8({}, ktx_etc1_width, ktx_etc1_height, 1, ktx_etc1_data,
61*8975f5c5SAndroid Build Coastguard Worker                         ktx_etc1_width / 4 * 8, 0, decompressedTextureData.data(),
62*8975f5c5SAndroid Build Coastguard Worker                         kDecompressedBytesPerPixel * ktx_etc1_width, 0);
63*8975f5c5SAndroid Build Coastguard Worker 
64*8975f5c5SAndroid Build Coastguard Worker     constexpr size_t kComparePixelX = ktx_etc1_width / 2;
65*8975f5c5SAndroid Build Coastguard Worker     constexpr size_t kComparePixelY = ktx_etc1_height / 2;
66*8975f5c5SAndroid Build Coastguard Worker     constexpr size_t kDecompressedPixelIndex =
67*8975f5c5SAndroid Build Coastguard Worker         ktx_etc1_width * kDecompressedBytesPerPixel * (kComparePixelY) +
68*8975f5c5SAndroid Build Coastguard Worker         kComparePixelX * kDecompressedBytesPerPixel;
69*8975f5c5SAndroid Build Coastguard Worker 
70*8975f5c5SAndroid Build Coastguard Worker     const GLColor expect(decompressedTextureData[kDecompressedPixelIndex],
71*8975f5c5SAndroid Build Coastguard Worker                          decompressedTextureData[kDecompressedPixelIndex + 1],
72*8975f5c5SAndroid Build Coastguard Worker                          decompressedTextureData[kDecompressedPixelIndex + 2],
73*8975f5c5SAndroid Build Coastguard Worker                          decompressedTextureData[kDecompressedPixelIndex + 3]);
74*8975f5c5SAndroid Build Coastguard Worker 
75*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_COLOR_EQ(kComparePixelX, kComparePixelY, expect);
76*8975f5c5SAndroid Build Coastguard Worker     EXPECT_GL_NO_ERROR();
77*8975f5c5SAndroid Build Coastguard Worker }
78*8975f5c5SAndroid Build Coastguard Worker 
79*8975f5c5SAndroid Build Coastguard Worker ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(KTXCompressedTextureTest);
80