1 //
2 // Copyright 2016 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 // SRGBFramebufferTest.cpp: Tests of sRGB framebuffer functionality.
8 
9 #include "test_utils/ANGLETest.h"
10 #include "test_utils/gl_raii.h"
11 
12 namespace angle
13 {
14 
15 class SRGBFramebufferTest : public ANGLETest
16 {
17   protected:
SRGBFramebufferTest()18     SRGBFramebufferTest()
19     {
20         setWindowWidth(128);
21         setWindowHeight(128);
22         setConfigRedBits(8);
23         setConfigGreenBits(8);
24         setConfigBlueBits(8);
25         setConfigAlphaBits(8);
26     }
27 
SetUp()28     void SetUp() override
29     {
30         ANGLETest::SetUp();
31 
32         const std::string vs =
33             "precision highp float;\n"
34             "attribute vec4 position;\n"
35             "void main()\n"
36             "{\n"
37             "   gl_Position = vec4(position.xy, 0.0, 1.0);\n"
38             "}\n";
39 
40         const std::string fs =
41             "precision highp float;\n"
42             "uniform vec4 color;\n"
43             "void main()\n"
44             "{\n"
45             "   gl_FragColor = color;\n"
46             "}\n";
47 
48         mProgram = CompileProgram(vs, fs);
49         ASSERT_NE(0u, mProgram);
50 
51         mColorLocation = glGetUniformLocation(mProgram, "color");
52         ASSERT_NE(-1, mColorLocation);
53     }
54 
TearDown()55     void TearDown() override
56     {
57         glDeleteProgram(mProgram);
58 
59         ANGLETest::TearDown();
60     }
61 
62     GLuint mProgram      = 0;
63     GLint mColorLocation = -1;
64 };
65 
66 // Test basic validation of GL_EXT_sRGB_write_control
TEST_P(SRGBFramebufferTest,Validation)67 TEST_P(SRGBFramebufferTest, Validation)
68 {
69     GLenum expectedError =
70         extensionEnabled("GL_EXT_sRGB_write_control") ? GL_NO_ERROR : GL_INVALID_ENUM;
71 
72     GLboolean value = GL_FALSE;
73     glEnable(GL_FRAMEBUFFER_SRGB_EXT);
74     EXPECT_GL_ERROR(expectedError);
75 
76     glGetBooleanv(GL_FRAMEBUFFER_SRGB_EXT, &value);
77     EXPECT_GL_ERROR(expectedError);
78     if (expectedError == GL_NO_ERROR)
79     {
80         EXPECT_EQ(GL_TRUE, value);
81     }
82 
83     glDisable(GL_FRAMEBUFFER_SRGB_EXT);
84     EXPECT_GL_ERROR(expectedError);
85 
86     glGetBooleanv(GL_FRAMEBUFFER_SRGB_EXT, &value);
87     EXPECT_GL_ERROR(expectedError);
88     if (expectedError == GL_NO_ERROR)
89     {
90         EXPECT_EQ(GL_FALSE, value);
91     }
92 }
93 
94 // Test basic functionality of GL_EXT_sRGB_write_control
TEST_P(SRGBFramebufferTest,BasicUsage)95 TEST_P(SRGBFramebufferTest, BasicUsage)
96 {
97     if (!extensionEnabled("GL_EXT_sRGB_write_control") ||
98         (!extensionEnabled("GL_EXT_sRGB") && getClientMajorVersion() < 3))
99     {
100         std::cout
101             << "Test skipped because GL_EXT_sRGB_write_control and GL_EXT_sRGB are not available."
102             << std::endl;
103         return;
104     }
105 
106     GLColor linearColor(64, 127, 191, 255);
107     GLColor srgbColor(13, 54, 133, 255);
108 
109     GLTexture texture;
110     glBindTexture(GL_TEXTURE_2D, texture.get());
111     glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA_EXT, 1, 1, 0, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE,
112                  nullptr);
113 
114     GLFramebuffer framebuffer;
115     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
116     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.get(), 0);
117 
118     glUseProgram(mProgram);
119     glUniform4fv(mColorLocation, 1, srgbColor.toNormalizedVector().data());
120 
121     glEnable(GL_FRAMEBUFFER_SRGB_EXT);
122     drawQuad(mProgram, "position", 0.5f);
123     EXPECT_PIXEL_COLOR_NEAR(0, 0, linearColor, 1.0);
124 
125     glDisable(GL_FRAMEBUFFER_SRGB_EXT);
126     drawQuad(mProgram, "position", 0.5f);
127     EXPECT_PIXEL_COLOR_NEAR(0, 0, srgbColor, 1.0);
128 }
129 
130 // Use this to select which configurations (e.g. which renderer, which GLES major version) these
131 // tests should be run against.
132 ANGLE_INSTANTIATE_TEST(SRGBFramebufferTest,
133                        ES2_D3D9(),
134                        ES2_D3D11(),
135                        ES3_D3D11(),
136                        ES2_OPENGL(),
137                        ES3_OPENGL(),
138                        ES2_OPENGLES(),
139                        ES3_OPENGLES());
140 
141 }  // namespace angle
142