xref: /aosp_15_r20/external/angle/src/tests/gl_tests/ActiveTextureCacheTest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2020 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 // ActiveTextureCacheTest.cpp: Regression tests of ANGLE's ActiveTextureCache inside gl::State.
7 
8 #include "test_utils/ANGLETest.h"
9 #include "test_utils/gl_raii.h"
10 
11 namespace angle
12 {
13 
14 class ActiveTextureCacheTest : public ANGLETest<>
15 {
16   protected:
ActiveTextureCacheTest()17     ActiveTextureCacheTest()
18     {
19         setWindowWidth(128);
20         setWindowHeight(128);
21         setConfigRedBits(8);
22         setConfigGreenBits(8);
23         setConfigBlueBits(8);
24         setConfigAlphaBits(8);
25     }
26 
testSetUp()27     void testSetUp() override
28     {
29         constexpr char kVS[] =
30             "precision highp float;\n"
31             "\n"
32             "void main()\n"
33             "{\n"
34             "   gl_Position = vec4(0.0, 0.0, 0.0, 0.0);\n"
35             "}\n";
36 
37         constexpr char k2DFS[] =
38             "precision highp float;\n"
39             "uniform sampler2D tex2D;\n"
40             "uniform samplerCube texCube;\n"
41             "\n"
42             "void main()\n"
43             "{\n"
44             "   gl_FragColor = texture2D(tex2D, vec2(0.0, 0.0)) + textureCube(texCube, vec3(0.0, "
45             "0.0, 0.0));\n"
46             "}\n";
47 
48         mProgram = CompileProgram(kVS, k2DFS);
49         ASSERT_NE(0u, mProgram);
50 
51         m2DTextureLocation = glGetUniformLocation(mProgram, "tex2D");
52         ASSERT_NE(-1, m2DTextureLocation);
53 
54         mCubeTextureLocation = glGetUniformLocation(mProgram, "texCube");
55         ASSERT_NE(-1, mCubeTextureLocation);
56     }
57 
testTearDown()58     void testTearDown() override { glDeleteProgram(mProgram); }
59 
60     GLuint mProgram            = 0;
61     GLint m2DTextureLocation   = -1;
62     GLint mCubeTextureLocation = -1;
63 };
64 
65 // Regression test for a bug that causes the ActiveTexturesCache to get out of sync with the
66 // currently bound textures when changing program uniforms in such a way that the program becomes
67 // invalid.
TEST_P(ActiveTextureCacheTest,UniformChangeUpdatesActiveTextureCache)68 TEST_P(ActiveTextureCacheTest, UniformChangeUpdatesActiveTextureCache)
69 {
70     glUseProgram(mProgram);
71 
72     // Generate two textures and reset the texture binding
73     GLuint tex0 = 0;
74     glGenTextures(1, &tex0);
75     glBindTexture(GL_TEXTURE_2D, tex0);
76     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
77 
78     GLuint tex1 = 0;
79     glGenTextures(1, &tex1);
80     glBindTexture(GL_TEXTURE_2D, tex1);
81     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
82     glBindTexture(GL_TEXTURE_2D, 0);
83 
84     // Set the active texture to 1 and bind tex0.
85     glActiveTexture(GL_TEXTURE1);
86     glBindTexture(GL_TEXTURE_2D, tex0);
87 
88     // Point the program's 2D sampler at texture binding 1. The texture will be added to the
89     // ActiveTexturesCache because it matches the program's sampler type for this texture binding.
90     glUniform1i(m2DTextureLocation, 1);
91 
92     // Point the program's cube sampler to texture binding 1 as well. This causes the program's
93     // samplers become invalid and the ActiveTexturesCache is NOT updated.
94     glUniform1i(mCubeTextureLocation, 1);
95 
96     // Bind tex1. ActiveTexturesCache is NOT updated (still contains tex0). The current texture
97     // bindings do not match ActiveTexturesCache's state.
98     glBindTexture(GL_TEXTURE_2D, tex1);
99 
100     // Delete tex0. The ActiveTexturesCache entry that points to tex0 is not cleared because tex0 is
101     // not currently bound.
102     glDeleteTextures(1, &tex0);
103 
104     // Use-after-free occurs during context destruction when the ActiveTexturesCache is cleared.
105 }
106 
107 // Use this to select which configurations (e.g. which renderer, which GLES major version) these
108 // tests should be run against.
109 ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(ActiveTextureCacheTest);
110 
111 }  // namespace angle
112