• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2018 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 // BGRATextureTest.cpp: Tests GLES1-specific usage of BGRA textures
8 
9 #include "test_utils/ANGLETest.h"
10 #include "test_utils/gl_raii.h"
11 
12 #include "util/random_utils.h"
13 
14 #include <stdint.h>
15 
16 using namespace angle;
17 
18 class BGRATextureTest : public ANGLETest<>
19 {
20   protected:
BGRATextureTest()21     BGRATextureTest()
22     {
23         setWindowWidth(32);
24         setWindowHeight(32);
25         setConfigRedBits(8);
26         setConfigGreenBits(8);
27         setConfigBlueBits(8);
28         setConfigAlphaBits(8);
29         setConfigDepthBits(24);
30     }
31 
rgbswap(GLColor x)32     GLColor rgbswap(GLColor x)
33     {
34         GLColor y;
35         y.B = x.R;
36         y.G = x.G;
37         y.R = x.B;
38         y.A = x.A;
39         return y;
40     }
41 
clearGLColor(GLColor color)42     void clearGLColor(GLColor color)
43     {
44         Vector4 colorF = color.toNormalizedVector();
45         glClearColor(colorF.x(), colorF.y(), colorF.z(), colorF.w());
46     }
47 };
48 
49 // Test that BGRA sampling samples color components and alpha correctly.
TEST_P(BGRATextureTest,BGRATextureSampling)50 TEST_P(BGRATextureTest, BGRATextureSampling)
51 {
52     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_texture_format_BGRA8888"));
53 
54     struct Vertex
55     {
56         GLfloat position[3];
57         GLfloat uv[2];
58     };
59 
60     glEnable(GL_TEXTURE_2D);
61 
62     std::array<GLColor, 4> testImage = {
63         GLColor::cyan,
64         GLColor::yellow,
65         GLColor::magenta,
66         GLColor(1, 2, 3, 0),
67     };
68 
69     GLTexture texRGBA;
70     {
71         glBindTexture(GL_TEXTURE_2D, texRGBA);
72         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
73         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
74         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
75         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
76 
77         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
78                      testImage.data());
79         EXPECT_GL_NO_ERROR();
80     }
81 
82     GLTexture texBGRA;
83     {
84         std::array<GLColor, 4> testImage2 = testImage;
85         for (auto &color : testImage2)
86         {
87             color = rgbswap(color);
88         }
89 
90         glBindTexture(GL_TEXTURE_2D, texBGRA);
91         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
92         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
93         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
94         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
95 
96         glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, 2, 2, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
97                      testImage2.data());
98         EXPECT_GL_NO_ERROR();
99     }
100 
101     GLColor clearColor = GLColor(102, 102, 102, 255);
102 
103     clearGLColor(clearColor);
104     glClear(GL_COLOR_BUFFER_BIT);
105 
106     glEnable(GL_ALPHA_TEST);
107     glAlphaFunc(GL_GEQUAL, 0.9f);
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     std::array<GLuint, 8> draws = {
130         texRGBA.get(), texBGRA.get(), texRGBA.get(), texRGBA.get(),
131         texBGRA.get(), texBGRA.get(), texRGBA.get(), texBGRA.get(),
132     };
133 
134     for (auto tex : draws)
135     {
136         glBindTexture(GL_TEXTURE_2D, tex);
137 
138         glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());
139         EXPECT_GL_NO_ERROR();
140 
141         EXPECT_PIXEL_COLOR_NEAR(8, 8, testImage[0], 0);
142         EXPECT_PIXEL_COLOR_NEAR(24, 8, testImage[1], 0);
143         EXPECT_PIXEL_COLOR_NEAR(8, 24, testImage[2], 0);
144         EXPECT_PIXEL_COLOR_NEAR(24, 24, clearColor, 0);
145     }
146 }
147 
148 ANGLE_INSTANTIATE_TEST_ES1(BGRATextureTest);
149