1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2024 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 // BlobCacheTest:
7*8975f5c5SAndroid Build Coastguard Worker // Unit tests for the GL_ANGLE_blob_cache extension.
8*8975f5c5SAndroid Build Coastguard Worker
9*8975f5c5SAndroid Build Coastguard Worker // Must be included first to prevent errors with "None".
10*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/ANGLETest.h"
11*8975f5c5SAndroid Build Coastguard Worker
12*8975f5c5SAndroid Build Coastguard Worker #include <map>
13*8975f5c5SAndroid Build Coastguard Worker #include <vector>
14*8975f5c5SAndroid Build Coastguard Worker
15*8975f5c5SAndroid Build Coastguard Worker #include "common/PackedEnums.h"
16*8975f5c5SAndroid Build Coastguard Worker #include "common/angleutils.h"
17*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/MultiThreadSteps.h"
18*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/gl_raii.h"
19*8975f5c5SAndroid Build Coastguard Worker #include "util/EGLWindow.h"
20*8975f5c5SAndroid Build Coastguard Worker #include "util/test_utils.h"
21*8975f5c5SAndroid Build Coastguard Worker
22*8975f5c5SAndroid Build Coastguard Worker namespace angle
23*8975f5c5SAndroid Build Coastguard Worker {
24*8975f5c5SAndroid Build Coastguard Worker constexpr char kExtName[] = "GL_ANGLE_blob_cache";
25*8975f5c5SAndroid Build Coastguard Worker
26*8975f5c5SAndroid Build Coastguard Worker enum class CacheOpResult
27*8975f5c5SAndroid Build Coastguard Worker {
28*8975f5c5SAndroid Build Coastguard Worker SetSuccess,
29*8975f5c5SAndroid Build Coastguard Worker GetNotFound,
30*8975f5c5SAndroid Build Coastguard Worker GetMemoryTooSmall,
31*8975f5c5SAndroid Build Coastguard Worker GetSuccess,
32*8975f5c5SAndroid Build Coastguard Worker ValueNotSet,
33*8975f5c5SAndroid Build Coastguard Worker EnumCount
34*8975f5c5SAndroid Build Coastguard Worker };
35*8975f5c5SAndroid Build Coastguard Worker
36*8975f5c5SAndroid Build Coastguard Worker angle::PackedEnumMap<CacheOpResult, std::string> kCacheOpToString = {
37*8975f5c5SAndroid Build Coastguard Worker {CacheOpResult::SetSuccess, "SetSuccess"},
38*8975f5c5SAndroid Build Coastguard Worker {CacheOpResult::GetNotFound, "GetNotFound"},
39*8975f5c5SAndroid Build Coastguard Worker {CacheOpResult::GetMemoryTooSmall, "GetMemoryTooSmall"},
40*8975f5c5SAndroid Build Coastguard Worker {CacheOpResult::GetSuccess, "GetSuccess"},
41*8975f5c5SAndroid Build Coastguard Worker {CacheOpResult::ValueNotSet, "ValueNotSet"},
42*8975f5c5SAndroid Build Coastguard Worker };
43*8975f5c5SAndroid Build Coastguard Worker
operator <<(std::ostream & os,CacheOpResult result)44*8975f5c5SAndroid Build Coastguard Worker std::ostream &operator<<(std::ostream &os, CacheOpResult result)
45*8975f5c5SAndroid Build Coastguard Worker {
46*8975f5c5SAndroid Build Coastguard Worker return os << kCacheOpToString[result];
47*8975f5c5SAndroid Build Coastguard Worker }
48*8975f5c5SAndroid Build Coastguard Worker
49*8975f5c5SAndroid Build Coastguard Worker struct TestUserData
50*8975f5c5SAndroid Build Coastguard Worker {
51*8975f5c5SAndroid Build Coastguard Worker std::map<std::vector<uint8_t>, std::vector<uint8_t>> cache;
52*8975f5c5SAndroid Build Coastguard Worker CacheOpResult cacheOpResult = CacheOpResult::ValueNotSet;
53*8975f5c5SAndroid Build Coastguard Worker };
54*8975f5c5SAndroid Build Coastguard Worker
SetBlob(const void * key,GLsizeiptr keySize,const void * value,GLsizeiptr valueSize,const void * userParam)55*8975f5c5SAndroid Build Coastguard Worker void GL_APIENTRY SetBlob(const void *key,
56*8975f5c5SAndroid Build Coastguard Worker GLsizeiptr keySize,
57*8975f5c5SAndroid Build Coastguard Worker const void *value,
58*8975f5c5SAndroid Build Coastguard Worker GLsizeiptr valueSize,
59*8975f5c5SAndroid Build Coastguard Worker const void *userParam)
60*8975f5c5SAndroid Build Coastguard Worker {
61*8975f5c5SAndroid Build Coastguard Worker TestUserData *data = reinterpret_cast<TestUserData *>(const_cast<void *>(userParam));
62*8975f5c5SAndroid Build Coastguard Worker
63*8975f5c5SAndroid Build Coastguard Worker std::vector<uint8_t> keyVec(keySize);
64*8975f5c5SAndroid Build Coastguard Worker memcpy(keyVec.data(), key, keySize);
65*8975f5c5SAndroid Build Coastguard Worker
66*8975f5c5SAndroid Build Coastguard Worker std::vector<uint8_t> valueVec(valueSize);
67*8975f5c5SAndroid Build Coastguard Worker memcpy(valueVec.data(), value, valueSize);
68*8975f5c5SAndroid Build Coastguard Worker
69*8975f5c5SAndroid Build Coastguard Worker data->cache[keyVec] = valueVec;
70*8975f5c5SAndroid Build Coastguard Worker
71*8975f5c5SAndroid Build Coastguard Worker data->cacheOpResult = CacheOpResult::SetSuccess;
72*8975f5c5SAndroid Build Coastguard Worker }
73*8975f5c5SAndroid Build Coastguard Worker
SetCorruptedBlob(const void * key,GLsizeiptr keySize,const void * value,GLsizeiptr valueSize,const void * userParam)74*8975f5c5SAndroid Build Coastguard Worker void GL_APIENTRY SetCorruptedBlob(const void *key,
75*8975f5c5SAndroid Build Coastguard Worker GLsizeiptr keySize,
76*8975f5c5SAndroid Build Coastguard Worker const void *value,
77*8975f5c5SAndroid Build Coastguard Worker GLsizeiptr valueSize,
78*8975f5c5SAndroid Build Coastguard Worker const void *userParam)
79*8975f5c5SAndroid Build Coastguard Worker {
80*8975f5c5SAndroid Build Coastguard Worker TestUserData *data = reinterpret_cast<TestUserData *>(const_cast<void *>(userParam));
81*8975f5c5SAndroid Build Coastguard Worker
82*8975f5c5SAndroid Build Coastguard Worker std::vector<uint8_t> keyVec(keySize);
83*8975f5c5SAndroid Build Coastguard Worker memcpy(keyVec.data(), key, keySize);
84*8975f5c5SAndroid Build Coastguard Worker
85*8975f5c5SAndroid Build Coastguard Worker std::vector<uint8_t> valueVec(valueSize);
86*8975f5c5SAndroid Build Coastguard Worker memcpy(valueVec.data(), value, valueSize);
87*8975f5c5SAndroid Build Coastguard Worker
88*8975f5c5SAndroid Build Coastguard Worker // Corrupt the data
89*8975f5c5SAndroid Build Coastguard Worker ++valueVec[valueVec.size() / 2];
90*8975f5c5SAndroid Build Coastguard Worker ++valueVec[valueVec.size() / 3];
91*8975f5c5SAndroid Build Coastguard Worker ++valueVec[valueVec.size() / 4];
92*8975f5c5SAndroid Build Coastguard Worker ++valueVec[2 * valueVec.size() / 3];
93*8975f5c5SAndroid Build Coastguard Worker ++valueVec[3 * valueVec.size() / 4];
94*8975f5c5SAndroid Build Coastguard Worker
95*8975f5c5SAndroid Build Coastguard Worker data->cache[keyVec] = valueVec;
96*8975f5c5SAndroid Build Coastguard Worker
97*8975f5c5SAndroid Build Coastguard Worker data->cacheOpResult = CacheOpResult::SetSuccess;
98*8975f5c5SAndroid Build Coastguard Worker }
99*8975f5c5SAndroid Build Coastguard Worker
GetBlob(const void * key,GLsizeiptr keySize,void * value,GLsizeiptr valueSize,const void * userParam)100*8975f5c5SAndroid Build Coastguard Worker GLsizeiptr GL_APIENTRY GetBlob(const void *key,
101*8975f5c5SAndroid Build Coastguard Worker GLsizeiptr keySize,
102*8975f5c5SAndroid Build Coastguard Worker void *value,
103*8975f5c5SAndroid Build Coastguard Worker GLsizeiptr valueSize,
104*8975f5c5SAndroid Build Coastguard Worker const void *userParam)
105*8975f5c5SAndroid Build Coastguard Worker {
106*8975f5c5SAndroid Build Coastguard Worker TestUserData *data = reinterpret_cast<TestUserData *>(const_cast<void *>(userParam));
107*8975f5c5SAndroid Build Coastguard Worker
108*8975f5c5SAndroid Build Coastguard Worker std::vector<uint8_t> keyVec(keySize);
109*8975f5c5SAndroid Build Coastguard Worker memcpy(keyVec.data(), key, keySize);
110*8975f5c5SAndroid Build Coastguard Worker
111*8975f5c5SAndroid Build Coastguard Worker auto entry = data->cache.find(keyVec);
112*8975f5c5SAndroid Build Coastguard Worker if (entry == data->cache.end())
113*8975f5c5SAndroid Build Coastguard Worker {
114*8975f5c5SAndroid Build Coastguard Worker // A compile+link operation can generate multiple queries to the cache; one per shader and
115*8975f5c5SAndroid Build Coastguard Worker // one for link. For the purposes of the test, make sure that any of these hitting the
116*8975f5c5SAndroid Build Coastguard Worker // cache is considered a success, particularly because it's valid for the pipeline cache
117*8975f5c5SAndroid Build Coastguard Worker // entry not to exist in the cache.
118*8975f5c5SAndroid Build Coastguard Worker if (data->cacheOpResult != CacheOpResult::GetSuccess)
119*8975f5c5SAndroid Build Coastguard Worker {
120*8975f5c5SAndroid Build Coastguard Worker data->cacheOpResult = CacheOpResult::GetNotFound;
121*8975f5c5SAndroid Build Coastguard Worker }
122*8975f5c5SAndroid Build Coastguard Worker return 0;
123*8975f5c5SAndroid Build Coastguard Worker }
124*8975f5c5SAndroid Build Coastguard Worker
125*8975f5c5SAndroid Build Coastguard Worker if (entry->second.size() <= static_cast<size_t>(valueSize))
126*8975f5c5SAndroid Build Coastguard Worker {
127*8975f5c5SAndroid Build Coastguard Worker memcpy(value, entry->second.data(), entry->second.size());
128*8975f5c5SAndroid Build Coastguard Worker data->cacheOpResult = CacheOpResult::GetSuccess;
129*8975f5c5SAndroid Build Coastguard Worker }
130*8975f5c5SAndroid Build Coastguard Worker else
131*8975f5c5SAndroid Build Coastguard Worker {
132*8975f5c5SAndroid Build Coastguard Worker data->cacheOpResult = CacheOpResult::GetMemoryTooSmall;
133*8975f5c5SAndroid Build Coastguard Worker }
134*8975f5c5SAndroid Build Coastguard Worker
135*8975f5c5SAndroid Build Coastguard Worker return entry->second.size();
136*8975f5c5SAndroid Build Coastguard Worker }
137*8975f5c5SAndroid Build Coastguard Worker
WaitProgramBinaryReady(GLuint program)138*8975f5c5SAndroid Build Coastguard Worker void WaitProgramBinaryReady(GLuint program)
139*8975f5c5SAndroid Build Coastguard Worker {
140*8975f5c5SAndroid Build Coastguard Worker // Using GL_ANGLE_program_binary_readiness_query, wait for post-link tasks to finish.
141*8975f5c5SAndroid Build Coastguard Worker // Otherwise, the program binary may not yet be cached. Only needed when a |set| operation is
142*8975f5c5SAndroid Build Coastguard Worker // expected.
143*8975f5c5SAndroid Build Coastguard Worker if (!IsGLExtensionEnabled("GL_ANGLE_program_binary_readiness_query"))
144*8975f5c5SAndroid Build Coastguard Worker {
145*8975f5c5SAndroid Build Coastguard Worker return;
146*8975f5c5SAndroid Build Coastguard Worker }
147*8975f5c5SAndroid Build Coastguard Worker
148*8975f5c5SAndroid Build Coastguard Worker GLint ready = false;
149*8975f5c5SAndroid Build Coastguard Worker while (!ready)
150*8975f5c5SAndroid Build Coastguard Worker {
151*8975f5c5SAndroid Build Coastguard Worker glGetProgramiv(program, GL_PROGRAM_BINARY_READY_ANGLE, &ready);
152*8975f5c5SAndroid Build Coastguard Worker angle::Sleep(0);
153*8975f5c5SAndroid Build Coastguard Worker }
154*8975f5c5SAndroid Build Coastguard Worker }
155*8975f5c5SAndroid Build Coastguard Worker
156*8975f5c5SAndroid Build Coastguard Worker class BlobCacheTest : public ANGLETest<>
157*8975f5c5SAndroid Build Coastguard Worker {
158*8975f5c5SAndroid Build Coastguard Worker protected:
BlobCacheTest()159*8975f5c5SAndroid Build Coastguard Worker BlobCacheTest() : mHasBlobCache(false)
160*8975f5c5SAndroid Build Coastguard Worker {
161*8975f5c5SAndroid Build Coastguard Worker // Force disply caching off. Blob cache functions require it.
162*8975f5c5SAndroid Build Coastguard Worker forceNewDisplay();
163*8975f5c5SAndroid Build Coastguard Worker }
164*8975f5c5SAndroid Build Coastguard Worker
testSetUp()165*8975f5c5SAndroid Build Coastguard Worker void testSetUp() override { mHasBlobCache = EnsureGLExtensionEnabled(kExtName); }
166*8975f5c5SAndroid Build Coastguard Worker
testTearDown()167*8975f5c5SAndroid Build Coastguard Worker void testTearDown() override {}
168*8975f5c5SAndroid Build Coastguard Worker
programBinaryAvailable()169*8975f5c5SAndroid Build Coastguard Worker bool programBinaryAvailable() { return IsGLExtensionEnabled("GL_OES_get_program_binary"); }
170*8975f5c5SAndroid Build Coastguard Worker
171*8975f5c5SAndroid Build Coastguard Worker bool mHasBlobCache;
172*8975f5c5SAndroid Build Coastguard Worker };
173*8975f5c5SAndroid Build Coastguard Worker
174*8975f5c5SAndroid Build Coastguard Worker // Makes sure the extension exists and works
TEST_P(BlobCacheTest,Functional)175*8975f5c5SAndroid Build Coastguard Worker TEST_P(BlobCacheTest, Functional)
176*8975f5c5SAndroid Build Coastguard Worker {
177*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
178*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
179*8975f5c5SAndroid Build Coastguard Worker
180*8975f5c5SAndroid Build Coastguard Worker EXPECT_TRUE(mHasBlobCache);
181*8975f5c5SAndroid Build Coastguard Worker
182*8975f5c5SAndroid Build Coastguard Worker TestUserData data;
183*8975f5c5SAndroid Build Coastguard Worker glBlobCacheCallbacksANGLE(SetBlob, GetBlob, &data);
184*8975f5c5SAndroid Build Coastguard Worker ASSERT_EGL_SUCCESS();
185*8975f5c5SAndroid Build Coastguard Worker
186*8975f5c5SAndroid Build Coastguard Worker constexpr char kVertexShaderSrc[] = R"(attribute vec4 aTest;
187*8975f5c5SAndroid Build Coastguard Worker attribute vec2 aPosition;
188*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
189*8975f5c5SAndroid Build Coastguard Worker void main()
190*8975f5c5SAndroid Build Coastguard Worker {
191*8975f5c5SAndroid Build Coastguard Worker vTest = aTest;
192*8975f5c5SAndroid Build Coastguard Worker gl_Position = vec4(aPosition, 0.0, 1.0);
193*8975f5c5SAndroid Build Coastguard Worker gl_PointSize = 1.0;
194*8975f5c5SAndroid Build Coastguard Worker })";
195*8975f5c5SAndroid Build Coastguard Worker
196*8975f5c5SAndroid Build Coastguard Worker constexpr char kFragmentShaderSrc[] = R"(precision mediump float;
197*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
198*8975f5c5SAndroid Build Coastguard Worker void main()
199*8975f5c5SAndroid Build Coastguard Worker {
200*8975f5c5SAndroid Build Coastguard Worker gl_FragColor = vTest;
201*8975f5c5SAndroid Build Coastguard Worker })";
202*8975f5c5SAndroid Build Coastguard Worker
203*8975f5c5SAndroid Build Coastguard Worker constexpr char kVertexShaderSrc2[] = R"(attribute vec4 aTest;
204*8975f5c5SAndroid Build Coastguard Worker attribute vec2 aPosition;
205*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
206*8975f5c5SAndroid Build Coastguard Worker void main()
207*8975f5c5SAndroid Build Coastguard Worker {
208*8975f5c5SAndroid Build Coastguard Worker vTest = aTest;
209*8975f5c5SAndroid Build Coastguard Worker gl_Position = vec4(aPosition, 1.0, 1.0);
210*8975f5c5SAndroid Build Coastguard Worker gl_PointSize = 1.0;
211*8975f5c5SAndroid Build Coastguard Worker })";
212*8975f5c5SAndroid Build Coastguard Worker
213*8975f5c5SAndroid Build Coastguard Worker constexpr char kFragmentShaderSrc2[] = R"(precision mediump float;
214*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
215*8975f5c5SAndroid Build Coastguard Worker void main()
216*8975f5c5SAndroid Build Coastguard Worker {
217*8975f5c5SAndroid Build Coastguard Worker gl_FragColor = vTest - vec4(0.0, 1.0, 0.0, 0.0);
218*8975f5c5SAndroid Build Coastguard Worker })";
219*8975f5c5SAndroid Build Coastguard Worker
220*8975f5c5SAndroid Build Coastguard Worker // Compile a shader so it puts something in the cache. Note that with Vulkan, some optional
221*8975f5c5SAndroid Build Coastguard Worker // link subtasks may run beyond link, and so the caching is delayed. An explicit wait on these
222*8975f5c5SAndroid Build Coastguard Worker // tasks is done for this reason.
223*8975f5c5SAndroid Build Coastguard Worker if (programBinaryAvailable())
224*8975f5c5SAndroid Build Coastguard Worker {
225*8975f5c5SAndroid Build Coastguard Worker ANGLE_GL_PROGRAM(program, kVertexShaderSrc, kFragmentShaderSrc);
226*8975f5c5SAndroid Build Coastguard Worker WaitProgramBinaryReady(program);
227*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
228*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
229*8975f5c5SAndroid Build Coastguard Worker
230*8975f5c5SAndroid Build Coastguard Worker // Compile the same shader again, so it would try to retrieve it from the cache
231*8975f5c5SAndroid Build Coastguard Worker program.makeRaster(kVertexShaderSrc, kFragmentShaderSrc);
232*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(program.valid());
233*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::GetSuccess, data.cacheOpResult);
234*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
235*8975f5c5SAndroid Build Coastguard Worker
236*8975f5c5SAndroid Build Coastguard Worker // Compile another shader, which should create a new entry
237*8975f5c5SAndroid Build Coastguard Worker program.makeRaster(kVertexShaderSrc2, kFragmentShaderSrc2);
238*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(program.valid());
239*8975f5c5SAndroid Build Coastguard Worker WaitProgramBinaryReady(program);
240*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
241*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
242*8975f5c5SAndroid Build Coastguard Worker
243*8975f5c5SAndroid Build Coastguard Worker // Compile the first shader again, which should still reside in the cache
244*8975f5c5SAndroid Build Coastguard Worker program.makeRaster(kVertexShaderSrc, kFragmentShaderSrc);
245*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(program.valid());
246*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::GetSuccess, data.cacheOpResult);
247*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
248*8975f5c5SAndroid Build Coastguard Worker
249*8975f5c5SAndroid Build Coastguard Worker // Make sure deleting the program doesn't result in a binary save. Regression test for a
250*8975f5c5SAndroid Build Coastguard Worker // bug where the binary was re-cached after being loaded.
251*8975f5c5SAndroid Build Coastguard Worker glUseProgram(0);
252*8975f5c5SAndroid Build Coastguard Worker program.reset();
253*8975f5c5SAndroid Build Coastguard Worker
254*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::ValueNotSet, data.cacheOpResult);
255*8975f5c5SAndroid Build Coastguard Worker }
256*8975f5c5SAndroid Build Coastguard Worker }
257*8975f5c5SAndroid Build Coastguard Worker
258*8975f5c5SAndroid Build Coastguard Worker // Makes sure the caching is always done without an explicit wait for post-link events (if any)
TEST_P(BlobCacheTest,FunctionalWithoutWait)259*8975f5c5SAndroid Build Coastguard Worker TEST_P(BlobCacheTest, FunctionalWithoutWait)
260*8975f5c5SAndroid Build Coastguard Worker {
261*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
262*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
263*8975f5c5SAndroid Build Coastguard Worker
264*8975f5c5SAndroid Build Coastguard Worker EXPECT_TRUE(mHasBlobCache);
265*8975f5c5SAndroid Build Coastguard Worker
266*8975f5c5SAndroid Build Coastguard Worker TestUserData data;
267*8975f5c5SAndroid Build Coastguard Worker glBlobCacheCallbacksANGLE(SetBlob, GetBlob, &data);
268*8975f5c5SAndroid Build Coastguard Worker ASSERT_GL_NO_ERROR();
269*8975f5c5SAndroid Build Coastguard Worker
270*8975f5c5SAndroid Build Coastguard Worker constexpr char kVertexShaderSrc[] = R"(attribute vec4 aTest;
271*8975f5c5SAndroid Build Coastguard Worker attribute vec2 aPosition;
272*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
273*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest2;
274*8975f5c5SAndroid Build Coastguard Worker void main()
275*8975f5c5SAndroid Build Coastguard Worker {
276*8975f5c5SAndroid Build Coastguard Worker vTest = aTest;
277*8975f5c5SAndroid Build Coastguard Worker vTest2 = aTest;
278*8975f5c5SAndroid Build Coastguard Worker gl_Position = vec4(aPosition, 1.0, 1.0);
279*8975f5c5SAndroid Build Coastguard Worker gl_PointSize = 1.0;
280*8975f5c5SAndroid Build Coastguard Worker })";
281*8975f5c5SAndroid Build Coastguard Worker
282*8975f5c5SAndroid Build Coastguard Worker constexpr char kFragmentShaderSrc[] = R"(precision mediump float;
283*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
284*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest2;
285*8975f5c5SAndroid Build Coastguard Worker void main()
286*8975f5c5SAndroid Build Coastguard Worker {
287*8975f5c5SAndroid Build Coastguard Worker gl_FragColor = vTest + vTest2 - vec4(0.0, 1.0, 0.0, 0.0);
288*8975f5c5SAndroid Build Coastguard Worker })";
289*8975f5c5SAndroid Build Coastguard Worker
290*8975f5c5SAndroid Build Coastguard Worker if (programBinaryAvailable())
291*8975f5c5SAndroid Build Coastguard Worker {
292*8975f5c5SAndroid Build Coastguard Worker // Make the conditions ideal for Vulkan's warm up task to match the draw call.
293*8975f5c5SAndroid Build Coastguard Worker constexpr uint32_t kSize = 1;
294*8975f5c5SAndroid Build Coastguard Worker GLTexture color;
295*8975f5c5SAndroid Build Coastguard Worker glBindTexture(GL_TEXTURE_2D, color);
296*8975f5c5SAndroid Build Coastguard Worker glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
297*8975f5c5SAndroid Build Coastguard Worker nullptr);
298*8975f5c5SAndroid Build Coastguard Worker
299*8975f5c5SAndroid Build Coastguard Worker GLFramebuffer fbo;
300*8975f5c5SAndroid Build Coastguard Worker glBindFramebuffer(GL_FRAMEBUFFER, fbo);
301*8975f5c5SAndroid Build Coastguard Worker glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
302*8975f5c5SAndroid Build Coastguard Worker ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
303*8975f5c5SAndroid Build Coastguard Worker
304*8975f5c5SAndroid Build Coastguard Worker ANGLE_GL_PROGRAM(program, kVertexShaderSrc, kFragmentShaderSrc);
305*8975f5c5SAndroid Build Coastguard Worker
306*8975f5c5SAndroid Build Coastguard Worker // First, draw with the program. In the Vulkan backend, this can lead to a wait on the warm
307*8975f5c5SAndroid Build Coastguard Worker // up task since the description matches the one needed for the draw.
308*8975f5c5SAndroid Build Coastguard Worker glUseProgram(program);
309*8975f5c5SAndroid Build Coastguard Worker glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
310*8975f5c5SAndroid Build Coastguard Worker
311*8975f5c5SAndroid Build Coastguard Worker // Delete the program to make sure caching the binary can no longer be delayed.
312*8975f5c5SAndroid Build Coastguard Worker glUseProgram(0);
313*8975f5c5SAndroid Build Coastguard Worker program.reset();
314*8975f5c5SAndroid Build Coastguard Worker
315*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
316*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
317*8975f5c5SAndroid Build Coastguard Worker }
318*8975f5c5SAndroid Build Coastguard Worker }
319*8975f5c5SAndroid Build Coastguard Worker
320*8975f5c5SAndroid Build Coastguard Worker // Tests error conditions of the APIs.
TEST_P(BlobCacheTest,NegativeAPI)321*8975f5c5SAndroid Build Coastguard Worker TEST_P(BlobCacheTest, NegativeAPI)
322*8975f5c5SAndroid Build Coastguard Worker {
323*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
324*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
325*8975f5c5SAndroid Build Coastguard Worker
326*8975f5c5SAndroid Build Coastguard Worker EXPECT_TRUE(mHasBlobCache);
327*8975f5c5SAndroid Build Coastguard Worker
328*8975f5c5SAndroid Build Coastguard Worker // Test bad arguments
329*8975f5c5SAndroid Build Coastguard Worker glBlobCacheCallbacksANGLE(SetBlob, nullptr, nullptr);
330*8975f5c5SAndroid Build Coastguard Worker EXPECT_GL_ERROR(GL_INVALID_OPERATION);
331*8975f5c5SAndroid Build Coastguard Worker
332*8975f5c5SAndroid Build Coastguard Worker glBlobCacheCallbacksANGLE(nullptr, GetBlob, nullptr);
333*8975f5c5SAndroid Build Coastguard Worker EXPECT_GL_ERROR(GL_INVALID_OPERATION);
334*8975f5c5SAndroid Build Coastguard Worker }
335*8975f5c5SAndroid Build Coastguard Worker
336*8975f5c5SAndroid Build Coastguard Worker // Regression test for including the fragment output locations in the program key.
337*8975f5c5SAndroid Build Coastguard Worker // http://anglebug.com/42263144
TEST_P(BlobCacheTest,FragmentOutputLocationKey)338*8975f5c5SAndroid Build Coastguard Worker TEST_P(BlobCacheTest, FragmentOutputLocationKey)
339*8975f5c5SAndroid Build Coastguard Worker {
340*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
341*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
342*8975f5c5SAndroid Build Coastguard Worker
343*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(!EnsureGLExtensionEnabled("GL_EXT_blend_func_extended") ||
344*8975f5c5SAndroid Build Coastguard Worker getClientMajorVersion() < 3);
345*8975f5c5SAndroid Build Coastguard Worker
346*8975f5c5SAndroid Build Coastguard Worker TestUserData data;
347*8975f5c5SAndroid Build Coastguard Worker glBlobCacheCallbacksANGLE(SetBlob, GetBlob, &data);
348*8975f5c5SAndroid Build Coastguard Worker ASSERT_GL_NO_ERROR();
349*8975f5c5SAndroid Build Coastguard Worker
350*8975f5c5SAndroid Build Coastguard Worker // Compile a shader so it puts something in the cache
351*8975f5c5SAndroid Build Coastguard Worker if (programBinaryAvailable())
352*8975f5c5SAndroid Build Coastguard Worker {
353*8975f5c5SAndroid Build Coastguard Worker glEnable(GL_SCISSOR_TEST);
354*8975f5c5SAndroid Build Coastguard Worker glScissor(0, 0, 1, 1);
355*8975f5c5SAndroid Build Coastguard Worker
356*8975f5c5SAndroid Build Coastguard Worker constexpr char kFragmentShaderSrc[] = R"(#version 300 es
357*8975f5c5SAndroid Build Coastguard Worker #extension GL_EXT_blend_func_extended : require
358*8975f5c5SAndroid Build Coastguard Worker precision mediump float;
359*8975f5c5SAndroid Build Coastguard Worker uniform vec4 src;
360*8975f5c5SAndroid Build Coastguard Worker uniform vec4 src1;
361*8975f5c5SAndroid Build Coastguard Worker out vec4 FragData;
362*8975f5c5SAndroid Build Coastguard Worker out vec4 SecondaryFragData;
363*8975f5c5SAndroid Build Coastguard Worker void main() {
364*8975f5c5SAndroid Build Coastguard Worker FragData = src;
365*8975f5c5SAndroid Build Coastguard Worker SecondaryFragData = src1;
366*8975f5c5SAndroid Build Coastguard Worker })";
367*8975f5c5SAndroid Build Coastguard Worker
368*8975f5c5SAndroid Build Coastguard Worker constexpr char kVertexShaderSrc[] = R"(#version 300 es
369*8975f5c5SAndroid Build Coastguard Worker in vec4 position;
370*8975f5c5SAndroid Build Coastguard Worker void main() {
371*8975f5c5SAndroid Build Coastguard Worker gl_Position = position;
372*8975f5c5SAndroid Build Coastguard Worker })";
373*8975f5c5SAndroid Build Coastguard Worker
374*8975f5c5SAndroid Build Coastguard Worker GLuint program = CompileProgram(kVertexShaderSrc, kFragmentShaderSrc, [](GLuint p) {
375*8975f5c5SAndroid Build Coastguard Worker glBindFragDataLocationEXT(p, 0, "FragData[0]");
376*8975f5c5SAndroid Build Coastguard Worker glBindFragDataLocationIndexedEXT(p, 0, 1, "SecondaryFragData[0]");
377*8975f5c5SAndroid Build Coastguard Worker });
378*8975f5c5SAndroid Build Coastguard Worker ASSERT_NE(0u, program);
379*8975f5c5SAndroid Build Coastguard Worker glUseProgram(program);
380*8975f5c5SAndroid Build Coastguard Worker glDrawArrays(GL_TRIANGLES, 0, 3);
381*8975f5c5SAndroid Build Coastguard Worker WaitProgramBinaryReady(program);
382*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
383*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
384*8975f5c5SAndroid Build Coastguard Worker
385*8975f5c5SAndroid Build Coastguard Worker // Re-link the program with different fragment output bindings
386*8975f5c5SAndroid Build Coastguard Worker program = CompileProgram(kVertexShaderSrc, kFragmentShaderSrc, [](GLuint p) {
387*8975f5c5SAndroid Build Coastguard Worker glBindFragDataLocationEXT(p, 0, "FragData");
388*8975f5c5SAndroid Build Coastguard Worker glBindFragDataLocationIndexedEXT(p, 0, 1, "SecondaryFragData");
389*8975f5c5SAndroid Build Coastguard Worker });
390*8975f5c5SAndroid Build Coastguard Worker ASSERT_NE(0u, program);
391*8975f5c5SAndroid Build Coastguard Worker glUseProgram(program);
392*8975f5c5SAndroid Build Coastguard Worker glDrawArrays(GL_TRIANGLES, 0, 3);
393*8975f5c5SAndroid Build Coastguard Worker WaitProgramBinaryReady(program);
394*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
395*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
396*8975f5c5SAndroid Build Coastguard Worker }
397*8975f5c5SAndroid Build Coastguard Worker }
398*8975f5c5SAndroid Build Coastguard Worker
399*8975f5c5SAndroid Build Coastguard Worker // Checks that the shader cache, which is used when this extension is available, is working
400*8975f5c5SAndroid Build Coastguard Worker // properly.
TEST_P(BlobCacheTest,ShaderCacheFunctional)401*8975f5c5SAndroid Build Coastguard Worker TEST_P(BlobCacheTest, ShaderCacheFunctional)
402*8975f5c5SAndroid Build Coastguard Worker {
403*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
404*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
405*8975f5c5SAndroid Build Coastguard Worker
406*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(!IsVulkan());
407*8975f5c5SAndroid Build Coastguard Worker
408*8975f5c5SAndroid Build Coastguard Worker TestUserData data;
409*8975f5c5SAndroid Build Coastguard Worker glBlobCacheCallbacksANGLE(SetBlob, GetBlob, &data);
410*8975f5c5SAndroid Build Coastguard Worker ASSERT_GL_NO_ERROR();
411*8975f5c5SAndroid Build Coastguard Worker
412*8975f5c5SAndroid Build Coastguard Worker constexpr char kVertexShaderSrc[] = R"(attribute vec4 aTest;
413*8975f5c5SAndroid Build Coastguard Worker attribute vec2 aPosition;
414*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
415*8975f5c5SAndroid Build Coastguard Worker void main()
416*8975f5c5SAndroid Build Coastguard Worker {
417*8975f5c5SAndroid Build Coastguard Worker vTest = aTest;
418*8975f5c5SAndroid Build Coastguard Worker gl_Position = vec4(aPosition, 0.0, 1.0);
419*8975f5c5SAndroid Build Coastguard Worker gl_PointSize = 1.0;
420*8975f5c5SAndroid Build Coastguard Worker })";
421*8975f5c5SAndroid Build Coastguard Worker
422*8975f5c5SAndroid Build Coastguard Worker constexpr char kFragmentShaderSrc[] = R"(precision mediump float;
423*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
424*8975f5c5SAndroid Build Coastguard Worker void main()
425*8975f5c5SAndroid Build Coastguard Worker {
426*8975f5c5SAndroid Build Coastguard Worker gl_FragColor = vTest;
427*8975f5c5SAndroid Build Coastguard Worker })";
428*8975f5c5SAndroid Build Coastguard Worker
429*8975f5c5SAndroid Build Coastguard Worker // Compile a shader so it puts something in the cache
430*8975f5c5SAndroid Build Coastguard Worker GLuint shaderID = CompileShader(GL_VERTEX_SHADER, kVertexShaderSrc);
431*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(shaderID != 0);
432*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
433*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
434*8975f5c5SAndroid Build Coastguard Worker glDeleteShader(shaderID);
435*8975f5c5SAndroid Build Coastguard Worker
436*8975f5c5SAndroid Build Coastguard Worker // Compile the same shader again, so it would try to retrieve it from the cache
437*8975f5c5SAndroid Build Coastguard Worker shaderID = CompileShader(GL_VERTEX_SHADER, kVertexShaderSrc);
438*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(shaderID != 0);
439*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::GetSuccess, data.cacheOpResult);
440*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
441*8975f5c5SAndroid Build Coastguard Worker glDeleteShader(shaderID);
442*8975f5c5SAndroid Build Coastguard Worker
443*8975f5c5SAndroid Build Coastguard Worker // Compile another shader, which should create a new entry
444*8975f5c5SAndroid Build Coastguard Worker shaderID = CompileShader(GL_FRAGMENT_SHADER, kFragmentShaderSrc);
445*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(shaderID != 0);
446*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
447*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
448*8975f5c5SAndroid Build Coastguard Worker glDeleteShader(shaderID);
449*8975f5c5SAndroid Build Coastguard Worker
450*8975f5c5SAndroid Build Coastguard Worker // Compile the first shader again, which should still reside in the cache
451*8975f5c5SAndroid Build Coastguard Worker shaderID = CompileShader(GL_VERTEX_SHADER, kVertexShaderSrc);
452*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(shaderID != 0);
453*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::GetSuccess, data.cacheOpResult);
454*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
455*8975f5c5SAndroid Build Coastguard Worker glDeleteShader(shaderID);
456*8975f5c5SAndroid Build Coastguard Worker }
457*8975f5c5SAndroid Build Coastguard Worker
458*8975f5c5SAndroid Build Coastguard Worker // Makes sure ANGLE recovers from corrupted cache.
TEST_P(BlobCacheTest,CacheCorruption)459*8975f5c5SAndroid Build Coastguard Worker TEST_P(BlobCacheTest, CacheCorruption)
460*8975f5c5SAndroid Build Coastguard Worker {
461*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
462*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
463*8975f5c5SAndroid Build Coastguard Worker
464*8975f5c5SAndroid Build Coastguard Worker TestUserData data;
465*8975f5c5SAndroid Build Coastguard Worker glBlobCacheCallbacksANGLE(SetCorruptedBlob, GetBlob, &data);
466*8975f5c5SAndroid Build Coastguard Worker ASSERT_GL_NO_ERROR();
467*8975f5c5SAndroid Build Coastguard Worker
468*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(!programBinaryAvailable());
469*8975f5c5SAndroid Build Coastguard Worker
470*8975f5c5SAndroid Build Coastguard Worker // Compile the program once and draw with it
471*8975f5c5SAndroid Build Coastguard Worker ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
472*8975f5c5SAndroid Build Coastguard Worker glUseProgram(program);
473*8975f5c5SAndroid Build Coastguard Worker
474*8975f5c5SAndroid Build Coastguard Worker const GLint colorUniformLocation =
475*8975f5c5SAndroid Build Coastguard Worker glGetUniformLocation(program, angle::essl1_shaders::ColorUniform());
476*8975f5c5SAndroid Build Coastguard Worker ASSERT_NE(colorUniformLocation, -1);
477*8975f5c5SAndroid Build Coastguard Worker
478*8975f5c5SAndroid Build Coastguard Worker glUniform4f(colorUniformLocation, 1, 0, 0, 1);
479*8975f5c5SAndroid Build Coastguard Worker drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
480*8975f5c5SAndroid Build Coastguard Worker EXPECT_GL_NO_ERROR();
481*8975f5c5SAndroid Build Coastguard Worker EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
482*8975f5c5SAndroid Build Coastguard Worker
483*8975f5c5SAndroid Build Coastguard Worker WaitProgramBinaryReady(program);
484*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
485*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
486*8975f5c5SAndroid Build Coastguard Worker
487*8975f5c5SAndroid Build Coastguard Worker // Compile/link the same program again, so it would try to retrieve it from the cache. GetBlob
488*8975f5c5SAndroid Build Coastguard Worker // should return success, but because the cache is corrupted by using SetCorruptedBlob, ANGLE
489*8975f5c5SAndroid Build Coastguard Worker // should redo the compile/link when Program::deserialize fails and set the blob again.
490*8975f5c5SAndroid Build Coastguard Worker program.makeRaster(essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
491*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(program.valid());
492*8975f5c5SAndroid Build Coastguard Worker glUseProgram(program);
493*8975f5c5SAndroid Build Coastguard Worker
494*8975f5c5SAndroid Build Coastguard Worker glUniform4f(colorUniformLocation, 0, 1, 0, 1);
495*8975f5c5SAndroid Build Coastguard Worker drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
496*8975f5c5SAndroid Build Coastguard Worker EXPECT_GL_NO_ERROR();
497*8975f5c5SAndroid Build Coastguard Worker EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
498*8975f5c5SAndroid Build Coastguard Worker
499*8975f5c5SAndroid Build Coastguard Worker WaitProgramBinaryReady(program);
500*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
501*8975f5c5SAndroid Build Coastguard Worker }
502*8975f5c5SAndroid Build Coastguard Worker
503*8975f5c5SAndroid Build Coastguard Worker class BlobCacheInternalRejectionTest : public BlobCacheTest
504*8975f5c5SAndroid Build Coastguard Worker {};
505*8975f5c5SAndroid Build Coastguard Worker
506*8975f5c5SAndroid Build Coastguard Worker // Makes sure ANGLE recovers from internal (backend) rejection of the program blob, while everything
507*8975f5c5SAndroid Build Coastguard Worker // seems fine to ANGLE.
TEST_P(BlobCacheInternalRejectionTest,Functional)508*8975f5c5SAndroid Build Coastguard Worker TEST_P(BlobCacheInternalRejectionTest, Functional)
509*8975f5c5SAndroid Build Coastguard Worker {
510*8975f5c5SAndroid Build Coastguard Worker TestUserData data;
511*8975f5c5SAndroid Build Coastguard Worker glBlobCacheCallbacksANGLE(SetBlob, GetBlob, &data);
512*8975f5c5SAndroid Build Coastguard Worker ASSERT_GL_NO_ERROR();
513*8975f5c5SAndroid Build Coastguard Worker
514*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(!programBinaryAvailable());
515*8975f5c5SAndroid Build Coastguard Worker
516*8975f5c5SAndroid Build Coastguard Worker // Compile the program once and draw with it
517*8975f5c5SAndroid Build Coastguard Worker ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
518*8975f5c5SAndroid Build Coastguard Worker glUseProgram(program);
519*8975f5c5SAndroid Build Coastguard Worker
520*8975f5c5SAndroid Build Coastguard Worker const GLint colorUniformLocation =
521*8975f5c5SAndroid Build Coastguard Worker glGetUniformLocation(program, angle::essl1_shaders::ColorUniform());
522*8975f5c5SAndroid Build Coastguard Worker ASSERT_NE(colorUniformLocation, -1);
523*8975f5c5SAndroid Build Coastguard Worker
524*8975f5c5SAndroid Build Coastguard Worker glUniform4f(colorUniformLocation, 1, 0, 0, 1);
525*8975f5c5SAndroid Build Coastguard Worker drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
526*8975f5c5SAndroid Build Coastguard Worker EXPECT_GL_NO_ERROR();
527*8975f5c5SAndroid Build Coastguard Worker EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
528*8975f5c5SAndroid Build Coastguard Worker
529*8975f5c5SAndroid Build Coastguard Worker WaitProgramBinaryReady(program);
530*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
531*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
532*8975f5c5SAndroid Build Coastguard Worker
533*8975f5c5SAndroid Build Coastguard Worker // Compile/link the same program again, so it would try to retrieve it from the cache. The blob
534*8975f5c5SAndroid Build Coastguard Worker // will be corrupted due to the CorruptProgramBinaryForTesting feature. GetBlob should return
535*8975f5c5SAndroid Build Coastguard Worker // success, and ANGLE would think the program is fine. After ANGLE internal updates, the
536*8975f5c5SAndroid Build Coastguard Worker // backend should reject the program binary, at which point ANGLE should redo the compile/link
537*8975f5c5SAndroid Build Coastguard Worker // and set the blob again.
538*8975f5c5SAndroid Build Coastguard Worker program.makeRaster(essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
539*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(program.valid());
540*8975f5c5SAndroid Build Coastguard Worker glUseProgram(program);
541*8975f5c5SAndroid Build Coastguard Worker
542*8975f5c5SAndroid Build Coastguard Worker glUniform4f(colorUniformLocation, 0, 1, 0, 1);
543*8975f5c5SAndroid Build Coastguard Worker drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
544*8975f5c5SAndroid Build Coastguard Worker EXPECT_GL_NO_ERROR();
545*8975f5c5SAndroid Build Coastguard Worker EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
546*8975f5c5SAndroid Build Coastguard Worker
547*8975f5c5SAndroid Build Coastguard Worker WaitProgramBinaryReady(program);
548*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
549*8975f5c5SAndroid Build Coastguard Worker }
550*8975f5c5SAndroid Build Coastguard Worker
551*8975f5c5SAndroid Build Coastguard Worker // Makes sure ANGLE recovers from internal (backend) rejection of the shader blob, while everything
552*8975f5c5SAndroid Build Coastguard Worker // seems fine to ANGLE.
TEST_P(BlobCacheInternalRejectionTest,ShaderCacheFunctional)553*8975f5c5SAndroid Build Coastguard Worker TEST_P(BlobCacheInternalRejectionTest, ShaderCacheFunctional)
554*8975f5c5SAndroid Build Coastguard Worker {
555*8975f5c5SAndroid Build Coastguard Worker ANGLE_SKIP_TEST_IF(!IsVulkan());
556*8975f5c5SAndroid Build Coastguard Worker
557*8975f5c5SAndroid Build Coastguard Worker TestUserData data;
558*8975f5c5SAndroid Build Coastguard Worker glBlobCacheCallbacksANGLE(SetBlob, GetBlob, &data);
559*8975f5c5SAndroid Build Coastguard Worker ASSERT_GL_NO_ERROR();
560*8975f5c5SAndroid Build Coastguard Worker
561*8975f5c5SAndroid Build Coastguard Worker // Compile a shader so it puts something in the cache
562*8975f5c5SAndroid Build Coastguard Worker GLuint shaderID = CompileShader(GL_VERTEX_SHADER, essl1_shaders::vs::Simple());
563*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(shaderID != 0);
564*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
565*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
566*8975f5c5SAndroid Build Coastguard Worker glDeleteShader(shaderID);
567*8975f5c5SAndroid Build Coastguard Worker
568*8975f5c5SAndroid Build Coastguard Worker // Compile another shader, which should create a new entry
569*8975f5c5SAndroid Build Coastguard Worker shaderID = CompileShader(GL_FRAGMENT_SHADER, essl1_shaders::fs::UniformColor());
570*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(shaderID != 0);
571*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
572*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
573*8975f5c5SAndroid Build Coastguard Worker glDeleteShader(shaderID);
574*8975f5c5SAndroid Build Coastguard Worker
575*8975f5c5SAndroid Build Coastguard Worker // Compile the first shader again, which should still reside in the cache, but is corrupted.
576*8975f5c5SAndroid Build Coastguard Worker // The cached entry should be discarded and compilation performed again (which sets another
577*8975f5c5SAndroid Build Coastguard Worker // entry in the cache).
578*8975f5c5SAndroid Build Coastguard Worker shaderID = CompileShader(GL_VERTEX_SHADER, essl1_shaders::vs::Simple());
579*8975f5c5SAndroid Build Coastguard Worker ASSERT_TRUE(shaderID != 0);
580*8975f5c5SAndroid Build Coastguard Worker EXPECT_EQ(CacheOpResult::SetSuccess, data.cacheOpResult);
581*8975f5c5SAndroid Build Coastguard Worker data.cacheOpResult = CacheOpResult::ValueNotSet;
582*8975f5c5SAndroid Build Coastguard Worker glDeleteShader(shaderID);
583*8975f5c5SAndroid Build Coastguard Worker }
584*8975f5c5SAndroid Build Coastguard Worker
585*8975f5c5SAndroid Build Coastguard Worker ANGLE_INSTANTIATE_TEST(BlobCacheTest,
586*8975f5c5SAndroid Build Coastguard Worker ES2_D3D9(),
587*8975f5c5SAndroid Build Coastguard Worker ES2_D3D11(),
588*8975f5c5SAndroid Build Coastguard Worker ES3_D3D11(),
589*8975f5c5SAndroid Build Coastguard Worker ES2_OPENGL(),
590*8975f5c5SAndroid Build Coastguard Worker ES3_OPENGL(),
591*8975f5c5SAndroid Build Coastguard Worker ES3_OPENGLES(),
592*8975f5c5SAndroid Build Coastguard Worker ES2_OPENGLES(),
593*8975f5c5SAndroid Build Coastguard Worker ES2_METAL(),
594*8975f5c5SAndroid Build Coastguard Worker ES3_METAL(),
595*8975f5c5SAndroid Build Coastguard Worker // Note: For the Vulkan backend, disable reads and writes for the global
596*8975f5c5SAndroid Build Coastguard Worker // pipeline cache, so it does not interfere with the test's expectations of
597*8975f5c5SAndroid Build Coastguard Worker // when the cache should and shouldn't be hit.
598*8975f5c5SAndroid Build Coastguard Worker ES2_VULKAN()
599*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::DisablePipelineCacheLoadForTesting)
600*8975f5c5SAndroid Build Coastguard Worker .disable(Feature::SyncMonolithicPipelinesToBlobCache),
601*8975f5c5SAndroid Build Coastguard Worker ES3_VULKAN_SWIFTSHADER()
602*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::DisablePipelineCacheLoadForTesting)
603*8975f5c5SAndroid Build Coastguard Worker .disable(Feature::SyncMonolithicPipelinesToBlobCache),
604*8975f5c5SAndroid Build Coastguard Worker ES3_VULKAN()
605*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::AsyncCommandQueue)
606*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::DisablePipelineCacheLoadForTesting)
607*8975f5c5SAndroid Build Coastguard Worker .disable(Feature::SyncMonolithicPipelinesToBlobCache),
608*8975f5c5SAndroid Build Coastguard Worker ES2_VULKAN_SWIFTSHADER()
609*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::AsyncCommandQueue)
610*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::DisablePipelineCacheLoadForTesting)
611*8975f5c5SAndroid Build Coastguard Worker .disable(Feature::SyncMonolithicPipelinesToBlobCache),
612*8975f5c5SAndroid Build Coastguard Worker ES2_VULKAN_SWIFTSHADER()
613*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::EnableParallelCompileAndLink)
614*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::DisablePipelineCacheLoadForTesting)
615*8975f5c5SAndroid Build Coastguard Worker .disable(Feature::SyncMonolithicPipelinesToBlobCache),
616*8975f5c5SAndroid Build Coastguard Worker ES3_VULKAN()
617*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::EnableParallelCompileAndLink)
618*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::DisablePipelineCacheLoadForTesting)
619*8975f5c5SAndroid Build Coastguard Worker .disable(Feature::SyncMonolithicPipelinesToBlobCache),
620*8975f5c5SAndroid Build Coastguard Worker ES2_VULKAN_SWIFTSHADER()
621*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::EnableParallelCompileAndLink)
622*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::AsyncCommandQueue)
623*8975f5c5SAndroid Build Coastguard Worker .enable(Feature::DisablePipelineCacheLoadForTesting)
624*8975f5c5SAndroid Build Coastguard Worker .disable(Feature::SyncMonolithicPipelinesToBlobCache));
625*8975f5c5SAndroid Build Coastguard Worker
626*8975f5c5SAndroid Build Coastguard Worker GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BlobCacheInternalRejectionTest);
627*8975f5c5SAndroid Build Coastguard Worker ANGLE_INSTANTIATE_TEST(BlobCacheInternalRejectionTest,
628*8975f5c5SAndroid Build Coastguard Worker ES2_OPENGL().enable(Feature::CorruptProgramBinaryForTesting),
629*8975f5c5SAndroid Build Coastguard Worker ES2_OPENGLES().enable(Feature::CorruptProgramBinaryForTesting));
630*8975f5c5SAndroid Build Coastguard Worker
631*8975f5c5SAndroid Build Coastguard Worker } // namespace angle
632