1 //
2 // Copyright 2022 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // PalettedTextureTest.cpp: Tests paletted texture decompression
8
9 #include "test_utils/ANGLETest.h"
10 #include "test_utils/gl_raii.h"
11
12 #include "common/matrix_utils.h"
13 #include "common/vector_utils.h"
14 #include "util/random_utils.h"
15
16 #include <stdint.h>
17
18 #include <vector>
19
20 using namespace angle;
21
22 class PalettedTextureTest : public ANGLETest<>
23 {
24 protected:
PalettedTextureTest()25 PalettedTextureTest()
26 {
27 setWindowWidth(32);
28 setWindowHeight(32);
29 setConfigRedBits(8);
30 setConfigGreenBits(8);
31 setConfigBlueBits(8);
32 setConfigAlphaBits(8);
33 setConfigDepthBits(24);
34 }
35 };
36
37 // Check that paletted formats are reported as supported.
TEST_P(PalettedTextureTest,PalettedFormatsAreSupported)38 TEST_P(PalettedTextureTest, PalettedFormatsAreSupported)
39 {
40 ANGLE_SKIP_TEST_IF(!IsVulkan());
41
42 std::vector<GLint> mustSupportFormats{
43 GL_PALETTE4_RGB8_OES, GL_PALETTE4_RGBA8_OES, GL_PALETTE4_R5_G6_B5_OES,
44 GL_PALETTE4_RGBA4_OES, GL_PALETTE4_RGB5_A1_OES, GL_PALETTE8_RGB8_OES,
45 GL_PALETTE8_RGBA8_OES, GL_PALETTE8_R5_G6_B5_OES, GL_PALETTE8_RGBA4_OES,
46 GL_PALETTE8_RGB5_A1_OES,
47 };
48
49 GLint numSupportedFormats;
50 glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numSupportedFormats);
51
52 std::vector<GLint> supportedFormats(numSupportedFormats, 0);
53 glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, supportedFormats.data());
54
55 for (GLint format : mustSupportFormats)
56 {
57 EXPECT_TRUE(std::find(supportedFormats.begin(), supportedFormats.end(), format) !=
58 supportedFormats.end());
59 }
60 }
61
62 // Check that sampling a paletted texture works.
TEST_P(PalettedTextureTest,PalettedTextureSampling)63 TEST_P(PalettedTextureTest, PalettedTextureSampling)
64 {
65 ANGLE_SKIP_TEST_IF(!IsVulkan());
66
67 struct Vertex
68 {
69 GLfloat position[3];
70 GLfloat uv[2];
71 };
72
73 glEnable(GL_TEXTURE_2D);
74
75 GLTexture texture;
76 glBindTexture(GL_TEXTURE_2D, texture);
77 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
81
82 struct TestImage
83 {
84 GLColor palette[16];
85 uint8_t texels[2]; // 2x2, each texel is 4-bit
86 };
87
88 TestImage testImage = {
89 {
90 GLColor::cyan,
91 GLColor::yellow,
92 GLColor::magenta,
93 GLColor::red,
94 },
95 {
96 0x01,
97 0x23,
98 },
99 };
100 glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_PALETTE4_RGBA8_OES, 2, 2, 0, sizeof testImage,
101 &testImage);
102 EXPECT_GL_NO_ERROR();
103
104 glClearColor(0.4f, 0.4f, 0.4f, 1.0f);
105 glClear(GL_COLOR_BUFFER_BIT);
106
107 for (int i = 0; i < 10; i++)
108 {
109 glMatrixMode(GL_PROJECTION);
110 glLoadIdentity();
111 glFrustumf(-1, 1, -1, 1, 5.0, 60.0);
112
113 glMatrixMode(GL_MODELVIEW);
114 glLoadIdentity();
115 glTranslatef(0.0f, 0.0f, -8.0f);
116
117 glEnableClientState(GL_VERTEX_ARRAY);
118 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
119
120 std::vector<Vertex> vertices = {
121 {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f}},
122 {{-1.0f, 1.0f, 0.0f}, {0.0f, 1.0f}},
123 {{1.0f, -1.0f, 0.0f}, {1.0f, 0.0f}},
124 {{1.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
125 };
126 glVertexPointer(3, GL_FLOAT, sizeof vertices[0], &vertices[0].position);
127 glTexCoordPointer(2, GL_FLOAT, sizeof vertices[0], &vertices[0].uv);
128
129 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());
130 EXPECT_GL_NO_ERROR();
131
132 EXPECT_PIXEL_COLOR_NEAR(8, 8, testImage.palette[0], 0);
133 EXPECT_PIXEL_COLOR_NEAR(24, 8, testImage.palette[1], 0);
134 EXPECT_PIXEL_COLOR_NEAR(8, 24, testImage.palette[2], 0);
135 EXPECT_PIXEL_COLOR_NEAR(24, 24, testImage.palette[3], 0);
136
137 swapBuffers();
138 }
139 }
140
141 ANGLE_INSTANTIATE_TEST_ES1(PalettedTextureTest);
142