1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 3.1 Module
3 * -------------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Synchronization Tests
22 *//*--------------------------------------------------------------------*/
24 #include "es31fSynchronizationTests.hpp"
25 #include "tcuTestLog.hpp"
26 #include "tcuSurface.hpp"
27 #include "tcuRenderTarget.hpp"
28 #include "gluRenderContext.hpp"
29 #include "gluShaderProgram.hpp"
30 #include "gluObjectWrapper.hpp"
31 #include "gluPixelTransfer.hpp"
32 #include "gluContextInfo.hpp"
33 #include "glwFunctions.hpp"
34 #include "glwEnums.hpp"
35 #include "deStringUtil.hpp"
36 #include "deSharedPtr.hpp"
38 #include "deRandom.hpp"
52 static bool validateSortedAtomicRampAdditionValueChain (const std::vector<deUint32>& valueChain, deUint32 sumValue, int& invalidOperationNdx, deUint32& errorDelta, deUint32& errorExpected)
54 std::vector<deUint32> chainDelta(valueChain.size());
56 for (int callNdx = 0; callNdx < (int)valueChain.size(); ++callNdx)
57 chainDelta[callNdx] = ((callNdx + 1 == (int)valueChain.size()) ? (sumValue) : (valueChain[callNdx+1])) - valueChain[callNdx];
59 // chainDelta contains now the actual additions applied to the value
60 // check there exists an addition ramp form 1 to ...
61 std::sort(chainDelta.begin(), chainDelta.end());
63 for (int callNdx = 0; callNdx < (int)valueChain.size(); ++callNdx)
65 if ((int)chainDelta[callNdx] != callNdx+1)
67 invalidOperationNdx = callNdx;
68 errorDelta = chainDelta[callNdx];
69 errorExpected = callNdx+1;
78 static void readBuffer (const glw::Functions& gl, deUint32 target, int numElements, std::vector<deUint32>& result)
80 const void* ptr = gl.mapBufferRange(target, 0, (int)(sizeof(deUint32) * numElements), GL_MAP_READ_BIT);
81 GLU_EXPECT_NO_ERROR(gl.getError(), "map");
84 throw tcu::TestError("mapBufferRange returned NULL");
86 result.resize(numElements);
87 memcpy(&result[0], ptr, sizeof(deUint32) * numElements);
89 if (gl.unmapBuffer(target) == GL_FALSE)
90 throw tcu::TestError("unmapBuffer returned false");
93 static deUint32 readBufferUint32 (const glw::Functions& gl, deUint32 target)
95 std::vector<deUint32> vec;
97 readBuffer(gl, target, 1, vec);
102 //! Generate a ramp of values from 1 to numElements, and shuffle it
103 void generateShuffledRamp (int numElements, std::vector<int>& ramp)
105 de::Random rng(0xabcd);
107 // some positive (non-zero) unique values
108 ramp.resize(numElements);
109 for (int callNdx = 0; callNdx < numElements; ++callNdx)
110 ramp[callNdx] = callNdx + 1;
112 rng.shuffle(ramp.begin(), ramp.end());
115 class InterInvocationTestCase : public TestCase
128 FLAG_ALIASING_STORAGES = 0x2,
132 InterInvocationTestCase (Context& context, const char* name, const char* desc, StorageType storage, int flags = 0);
133 ~InterInvocationTestCase (void);
138 IterateResult iterate (void);
140 void runCompute (void);
141 bool verifyResults (void);
142 virtual std::string genShaderSource (void) const = 0;
145 std::string genBarrierSource (void) const;
147 const StorageType m_storage;
148 const bool m_useAtomic;
149 const bool m_aliasingStorages;
150 const bool m_syncWithGroup;
151 const int m_workWidth; // !< total work width
152 const int m_workHeight; // !< ... height
153 const int m_localWidth; // !< group width
154 const int m_localHeight; // !< group height
155 const int m_elementsPerInvocation; // !< elements accessed by a single invocation
158 glw::GLuint m_storageBuf;
159 glw::GLuint m_storageTex;
160 glw::GLuint m_resultBuf;
161 glu::ShaderProgram* m_program;
164 InterInvocationTestCase::InterInvocationTestCase (Context& context, const char* name, const char* desc, StorageType storage, int flags)
165 : TestCase (context, name, desc)
166 , m_storage (storage)
167 , m_useAtomic ((flags & FLAG_ATOMIC) != 0)
168 , m_aliasingStorages ((flags & FLAG_ALIASING_STORAGES) != 0)
169 , m_syncWithGroup ((flags & FLAG_IN_GROUP) != 0)
174 , m_elementsPerInvocation (8)
178 , m_program (DE_NULL)
180 DE_ASSERT(m_storage < STORAGE_LAST);
181 DE_ASSERT(m_localWidth*m_localHeight <= 128); // minimum MAX_COMPUTE_WORK_GROUP_INVOCATIONS value
184 InterInvocationTestCase::~InterInvocationTestCase (void)
189 void InterInvocationTestCase::init (void)
191 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
195 if (m_useAtomic && m_storage == STORAGE_IMAGE && !m_context.getContextInfo().isExtensionSupported("GL_OES_shader_image_atomic"))
196 throw tcu::NotSupportedError("Test requires GL_OES_shader_image_atomic extension");
200 m_program = new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(genShaderSource()));
201 m_testCtx.getLog() << *m_program;
202 if (!m_program->isOk())
203 throw tcu::TestError("could not build program");
207 if (m_storage == STORAGE_BUFFER)
209 const int bufferElements = m_workWidth * m_workHeight * m_elementsPerInvocation;
210 const int bufferSize = bufferElements * (int)sizeof(deUint32);
211 std::vector<deUint32> zeroBuffer (bufferElements, 0);
213 m_testCtx.getLog() << tcu::TestLog::Message << "Allocating zero-filled buffer for storage, size " << bufferElements << " elements, " << bufferSize << " bytes." << tcu::TestLog::EndMessage;
215 gl.genBuffers(1, &m_storageBuf);
216 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_storageBuf);
217 gl.bufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &zeroBuffer[0], GL_STATIC_DRAW);
218 GLU_EXPECT_NO_ERROR(gl.getError(), "gen storage buf");
220 else if (m_storage == STORAGE_IMAGE)
222 const int bufferElements = m_workWidth * m_workHeight * m_elementsPerInvocation;
223 const int bufferSize = bufferElements * (int)sizeof(deUint32);
225 m_testCtx.getLog() << tcu::TestLog::Message << "Allocating image for storage, size " << m_workWidth << "x" << m_workHeight * m_elementsPerInvocation << ", " << bufferSize << " bytes." << tcu::TestLog::EndMessage;
227 gl.genTextures(1, &m_storageTex);
228 gl.bindTexture(GL_TEXTURE_2D, m_storageTex);
229 gl.texStorage2D(GL_TEXTURE_2D, 1, GL_R32I, m_workWidth, m_workHeight * m_elementsPerInvocation);
230 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
231 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
232 GLU_EXPECT_NO_ERROR(gl.getError(), "gen storage image");
235 m_testCtx.getLog() << tcu::TestLog::Message << "Filling image with 0." << tcu::TestLog::EndMessage;
238 const std::vector<deInt32> zeroBuffer(m_workWidth * m_workHeight * m_elementsPerInvocation, 0);
239 gl.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_workWidth, m_workHeight * m_elementsPerInvocation, GL_RED_INTEGER, GL_INT, &zeroBuffer[0]);
240 GLU_EXPECT_NO_ERROR(gl.getError(), "specify image contents");
249 const int bufferElements = m_workWidth * m_workHeight;
250 const int bufferSize = bufferElements * (int)sizeof(deUint32);
251 std::vector<deInt32> negativeBuffer (bufferElements, -1);
253 m_testCtx.getLog() << tcu::TestLog::Message << "Allocating -1 filled buffer for results, size " << bufferElements << " elements, " << bufferSize << " bytes." << tcu::TestLog::EndMessage;
255 gl.genBuffers(1, &m_resultBuf);
256 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_resultBuf);
257 gl.bufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &negativeBuffer[0], GL_STATIC_DRAW);
258 GLU_EXPECT_NO_ERROR(gl.getError(), "gen storage buf");
262 void InterInvocationTestCase::deinit (void)
266 m_context.getRenderContext().getFunctions().deleteBuffers(1, &m_storageBuf);
267 m_storageBuf = DE_NULL;
272 m_context.getRenderContext().getFunctions().deleteTextures(1, &m_storageTex);
273 m_storageTex = DE_NULL;
278 m_context.getRenderContext().getFunctions().deleteBuffers(1, &m_resultBuf);
279 m_resultBuf = DE_NULL;
286 InterInvocationTestCase::IterateResult InterInvocationTestCase::iterate (void)
291 // Verify buffer contents
293 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
295 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, (std::string((m_storage == STORAGE_BUFFER) ? ("buffer") : ("image")) + " content verification failed").c_str());
300 void InterInvocationTestCase::runCompute (void)
302 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
303 const int groupsX = m_workWidth / m_localWidth;
304 const int groupsY = m_workHeight / m_localHeight;
306 DE_ASSERT((m_workWidth % m_localWidth) == 0);
307 DE_ASSERT((m_workHeight % m_localHeight) == 0);
310 << tcu::TestLog::Message
311 << "Dispatching compute.\n"
312 << " group size: " << m_localWidth << "x" << m_localHeight << "\n"
313 << " dispatch size: " << groupsX << "x" << groupsY << "\n"
314 << " total work size: " << m_workWidth << "x" << m_workHeight << "\n"
315 << tcu::TestLog::EndMessage;
317 gl.useProgram(m_program->getProgram());
320 if (m_storage == STORAGE_BUFFER && !m_aliasingStorages)
322 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_storageBuf);
323 GLU_EXPECT_NO_ERROR(gl.getError(), "bind source buf");
325 else if (m_storage == STORAGE_BUFFER && m_aliasingStorages)
327 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_storageBuf);
328 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_storageBuf);
329 GLU_EXPECT_NO_ERROR(gl.getError(), "bind source buf");
331 m_testCtx.getLog() << tcu::TestLog::Message << "Binding same buffer object to buffer storages." << tcu::TestLog::EndMessage;
333 else if (m_storage == STORAGE_IMAGE && !m_aliasingStorages)
335 gl.bindImageTexture(1, m_storageTex, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32I);
336 GLU_EXPECT_NO_ERROR(gl.getError(), "bind result buf");
338 else if (m_storage == STORAGE_IMAGE && m_aliasingStorages)
340 gl.bindImageTexture(1, m_storageTex, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32I);
341 gl.bindImageTexture(2, m_storageTex, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32I);
343 GLU_EXPECT_NO_ERROR(gl.getError(), "bind result buf");
345 m_testCtx.getLog() << tcu::TestLog::Message << "Binding same texture level to image storages." << tcu::TestLog::EndMessage;
351 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_resultBuf);
352 GLU_EXPECT_NO_ERROR(gl.getError(), "bind result buf");
355 gl.dispatchCompute(groupsX, groupsY, 1);
356 GLU_EXPECT_NO_ERROR(gl.getError(), "dispatchCompute");
359 bool InterInvocationTestCase::verifyResults (void)
361 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
362 const int errorFloodThreshold = 5;
363 int numErrorsLogged = 0;
364 const void* mapped = DE_NULL;
365 std::vector<deInt32> results (m_workWidth * m_workHeight);
368 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_resultBuf);
369 mapped = gl.mapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, m_workWidth * m_workHeight * sizeof(deInt32), GL_MAP_READ_BIT);
370 GLU_EXPECT_NO_ERROR(gl.getError(), "map buffer");
372 // copy to properly aligned array
373 deMemcpy(&results[0], mapped, m_workWidth * m_workHeight * sizeof(deUint32));
375 if (gl.unmapBuffer(GL_SHADER_STORAGE_BUFFER) != GL_TRUE)
376 throw tcu::TestError("memory map store corrupted");
379 for (int ndx = 0; ndx < (int)results.size(); ++ndx)
381 if (results[ndx] != 1)
385 if (numErrorsLogged == 0)
386 m_testCtx.getLog() << tcu::TestLog::Message << "Result buffer failed, got unexpected values.\n" << tcu::TestLog::EndMessage;
387 if (numErrorsLogged++ < errorFloodThreshold)
388 m_testCtx.getLog() << tcu::TestLog::Message << " Error at index " << ndx << ": expected 1, got " << results[ndx] << ".\n" << tcu::TestLog::EndMessage;
391 // after N errors, no point continuing verification
392 m_testCtx.getLog() << tcu::TestLog::Message << " -- too many errors, skipping verification --\n" << tcu::TestLog::EndMessage;
399 m_testCtx.getLog() << tcu::TestLog::Message << "Result buffer ok." << tcu::TestLog::EndMessage;
403 std::string InterInvocationTestCase::genBarrierSource (void) const
405 std::ostringstream buf;
409 // Wait until all invocations in this work group have their texture/buffer read/write operations complete
410 // \note We could also use memoryBarrierBuffer() or memoryBarrierImage() in place of groupMemoryBarrier() but
411 // we only require intra-workgroup synchronization.
413 << " groupMemoryBarrier();\n"
417 else if (m_storage == STORAGE_BUFFER)
419 DE_ASSERT(!m_syncWithGroup);
421 // Waiting only for data written by this invocation. Since all buffer reads and writes are
422 // processed in order (within a single invocation), we don't have to do anything.
425 else if (m_storage == STORAGE_IMAGE)
427 DE_ASSERT(!m_syncWithGroup);
429 // Waiting only for data written by this invocation. But since operations complete in undefined
430 // order, we have to wait for them to complete.
432 << " memoryBarrierImage();\n"
441 class InvocationBasicCase : public InterInvocationTestCase
444 InvocationBasicCase (Context& context, const char* name, const char* desc, StorageType storage, int flags);
446 std::string genShaderSource (void) const;
447 virtual std::string genShaderMainBlock (void) const = 0;
450 InvocationBasicCase::InvocationBasicCase (Context& context, const char* name, const char* desc, StorageType storage, int flags)
451 : InterInvocationTestCase(context, name, desc, storage, flags)
455 std::string InvocationBasicCase::genShaderSource (void) const
457 const bool useImageAtomics = m_useAtomic && m_storage == STORAGE_IMAGE;
458 std::ostringstream buf;
460 buf << "#version 310 es\n"
461 << ((useImageAtomics) ? ("#extension GL_OES_shader_image_atomic : require\n") : (""))
462 << "layout (local_size_x=" << m_localWidth << ", local_size_y=" << m_localHeight << ") in;\n"
463 << "layout(binding=0, std430) buffer Output\n"
465 << " highp int values[];\n"
468 if (m_storage == STORAGE_BUFFER)
469 buf << "layout(binding=1, std430) coherent buffer Storage\n"
471 << " highp int values[];\n"
474 << "highp int getIndex (in highp uvec2 localID, in highp int element)\n"
476 << " highp uint groupNdx = gl_NumWorkGroups.x * gl_WorkGroupID.y + gl_WorkGroupID.x;\n"
477 << " return int((localID.y * gl_NumWorkGroups.x * gl_NumWorkGroups.y * gl_WorkGroupSize.x) + (groupNdx * gl_WorkGroupSize.x) + localID.x) * " << m_elementsPerInvocation << " + element;\n"
479 else if (m_storage == STORAGE_IMAGE)
480 buf << "layout(r32i, binding=1) coherent uniform highp iimage2D u_image;\n"
482 << "highp ivec2 getCoord (in highp uvec2 localID, in highp int element)\n"
484 << " return ivec2(int(gl_WorkGroupID.x * gl_WorkGroupSize.x + localID.x), int(gl_WorkGroupID.y * gl_WorkGroupSize.y + localID.y) + element * " << m_workHeight << ");\n"
490 << "void main (void)\n"
492 << " int resultNdx = int(gl_GlobalInvocationID.y * gl_NumWorkGroups.x * gl_WorkGroupSize.x + gl_GlobalInvocationID.x);\n"
493 << " int groupNdx = int(gl_NumWorkGroups.x * gl_WorkGroupID.y + gl_WorkGroupID.x);\n"
494 << " bool allOk = true;\n"
496 << genShaderMainBlock()
498 << " sb_result.values[resultNdx] = (allOk) ? (1) : (0);\n"
504 class InvocationWriteReadCase : public InvocationBasicCase
507 InvocationWriteReadCase (Context& context, const char* name, const char* desc, StorageType storage, int flags);
509 std::string genShaderMainBlock (void) const;
512 InvocationWriteReadCase::InvocationWriteReadCase (Context& context, const char* name, const char* desc, StorageType storage, int flags)
513 : InvocationBasicCase(context, name, desc, storage, flags)
517 std::string InvocationWriteReadCase::genShaderMainBlock (void) const
519 std::ostringstream buf;
523 for (int ndx = 0; ndx < m_elementsPerInvocation; ++ndx)
525 if (m_storage == STORAGE_BUFFER && m_useAtomic)
526 buf << "\tatomicAdd(sb_store.values[getIndex(gl_LocalInvocationID.xy, " << ndx << ")], groupNdx);\n";
527 else if (m_storage == STORAGE_BUFFER && !m_useAtomic)
528 buf << "\tsb_store.values[getIndex(gl_LocalInvocationID.xy, " << ndx << ")] = groupNdx;\n";
529 else if (m_storage == STORAGE_IMAGE && m_useAtomic)
530 buf << "\timageAtomicAdd(u_image, getCoord(gl_LocalInvocationID.xy, " << ndx << "), int(groupNdx));\n";
531 else if (m_storage == STORAGE_IMAGE && !m_useAtomic)
532 buf << "\timageStore(u_image, getCoord(gl_LocalInvocationID.xy, " << ndx << "), ivec4(int(groupNdx), 0, 0, 0));\n";
539 buf << genBarrierSource();
543 for (int ndx = 0; ndx < m_elementsPerInvocation; ++ndx)
545 const std::string localID = (m_syncWithGroup) ? ("(gl_LocalInvocationID.xy + uvec2(" + de::toString(ndx+1) + ", " + de::toString(2*ndx) + ")) % gl_WorkGroupSize.xy") : ("gl_LocalInvocationID.xy");
547 if (m_storage == STORAGE_BUFFER && m_useAtomic)
548 buf << "\tallOk = allOk && (atomicExchange(sb_store.values[getIndex(" << localID << ", " << ndx << ")], 0) == groupNdx);\n";
549 else if (m_storage == STORAGE_BUFFER && !m_useAtomic)
550 buf << "\tallOk = allOk && (sb_store.values[getIndex(" << localID << ", " << ndx << ")] == groupNdx);\n";
551 else if (m_storage == STORAGE_IMAGE && m_useAtomic)
552 buf << "\tallOk = allOk && (imageAtomicExchange(u_image, getCoord(" << localID << ", " << ndx << "), 0) == groupNdx);\n";
553 else if (m_storage == STORAGE_IMAGE && !m_useAtomic)
554 buf << "\tallOk = allOk && (imageLoad(u_image, getCoord(" << localID << ", " << ndx << ")).x == groupNdx);\n";
562 class InvocationReadWriteCase : public InvocationBasicCase
565 InvocationReadWriteCase (Context& context, const char* name, const char* desc, StorageType storage, int flags);
567 std::string genShaderMainBlock (void) const;
570 InvocationReadWriteCase::InvocationReadWriteCase (Context& context, const char* name, const char* desc, StorageType storage, int flags)
571 : InvocationBasicCase(context, name, desc, storage, flags)
575 std::string InvocationReadWriteCase::genShaderMainBlock (void) const
577 std::ostringstream buf;
581 for (int ndx = 0; ndx < m_elementsPerInvocation; ++ndx)
583 const std::string localID = (m_syncWithGroup) ? ("(gl_LocalInvocationID.xy + uvec2(" + de::toString(ndx+1) + ", " + de::toString(2*ndx) + ")) % gl_WorkGroupSize.xy") : ("gl_LocalInvocationID.xy");
585 if (m_storage == STORAGE_BUFFER && m_useAtomic)
586 buf << "\tallOk = allOk && (atomicExchange(sb_store.values[getIndex(" << localID << ", " << ndx << ")], 123) == 0);\n";
587 else if (m_storage == STORAGE_BUFFER && !m_useAtomic)
588 buf << "\tallOk = allOk && (sb_store.values[getIndex(" << localID << ", " << ndx << ")] == 0);\n";
589 else if (m_storage == STORAGE_IMAGE && m_useAtomic)
590 buf << "\tallOk = allOk && (imageAtomicExchange(u_image, getCoord(" << localID << ", " << ndx << "), 123) == 0);\n";
591 else if (m_storage == STORAGE_IMAGE && !m_useAtomic)
592 buf << "\tallOk = allOk && (imageLoad(u_image, getCoord(" << localID << ", " << ndx << ")).x == 0);\n";
599 buf << genBarrierSource();
603 for (int ndx = 0; ndx < m_elementsPerInvocation; ++ndx)
605 if (m_storage == STORAGE_BUFFER && m_useAtomic)
606 buf << "\tatomicAdd(sb_store.values[getIndex(gl_LocalInvocationID.xy, " << ndx << ")], groupNdx);\n";
607 else if (m_storage == STORAGE_BUFFER && !m_useAtomic)
608 buf << "\tsb_store.values[getIndex(gl_LocalInvocationID.xy, " << ndx << ")] = groupNdx;\n";
609 else if (m_storage == STORAGE_IMAGE && m_useAtomic)
610 buf << "\timageAtomicAdd(u_image, getCoord(gl_LocalInvocationID.xy, " << ndx << "), int(groupNdx));\n";
611 else if (m_storage == STORAGE_IMAGE && !m_useAtomic)
612 buf << "\timageStore(u_image, getCoord(gl_LocalInvocationID.xy, " << ndx << "), ivec4(int(groupNdx), 0, 0, 0));\n";
620 class InvocationOverWriteCase : public InvocationBasicCase
623 InvocationOverWriteCase (Context& context, const char* name, const char* desc, StorageType storage, int flags);
625 std::string genShaderMainBlock (void) const;
628 InvocationOverWriteCase::InvocationOverWriteCase (Context& context, const char* name, const char* desc, StorageType storage, int flags)
629 : InvocationBasicCase(context, name, desc, storage, flags)
633 std::string InvocationOverWriteCase::genShaderMainBlock (void) const
635 std::ostringstream buf;
639 for (int ndx = 0; ndx < m_elementsPerInvocation; ++ndx)
641 if (m_storage == STORAGE_BUFFER && m_useAtomic)
642 buf << "\tatomicAdd(sb_store.values[getIndex(gl_LocalInvocationID.xy, " << ndx << ")], 456);\n";
643 else if (m_storage == STORAGE_BUFFER && !m_useAtomic)
644 buf << "\tsb_store.values[getIndex(gl_LocalInvocationID.xy, " << ndx << ")] = 456;\n";
645 else if (m_storage == STORAGE_IMAGE && m_useAtomic)
646 buf << "\timageAtomicAdd(u_image, getCoord(gl_LocalInvocationID.xy, " << ndx << "), 456);\n";
647 else if (m_storage == STORAGE_IMAGE && !m_useAtomic)
648 buf << "\timageStore(u_image, getCoord(gl_LocalInvocationID.xy, " << ndx << "), ivec4(456, 0, 0, 0));\n";
655 buf << genBarrierSource();
659 for (int ndx = 0; ndx < m_elementsPerInvocation; ++ndx)
661 // write another invocation's value or our own value depending on test type
662 const std::string localID = (m_syncWithGroup) ? ("(gl_LocalInvocationID.xy + uvec2(" + de::toString(ndx+4) + ", " + de::toString(3*ndx) + ")) % gl_WorkGroupSize.xy") : ("gl_LocalInvocationID.xy");
664 if (m_storage == STORAGE_BUFFER && m_useAtomic)
665 buf << "\tatomicExchange(sb_store.values[getIndex(" << localID << ", " << ndx << ")], groupNdx);\n";
666 else if (m_storage == STORAGE_BUFFER && !m_useAtomic)
667 buf << "\tsb_store.values[getIndex(" << localID << ", " << ndx << ")] = groupNdx;\n";
668 else if (m_storage == STORAGE_IMAGE && m_useAtomic)
669 buf << "\timageAtomicExchange(u_image, getCoord(" << localID << ", " << ndx << "), groupNdx);\n";
670 else if (m_storage == STORAGE_IMAGE && !m_useAtomic)
671 buf << "\timageStore(u_image, getCoord(" << localID << ", " << ndx << "), ivec4(groupNdx, 0, 0, 0));\n";
678 buf << genBarrierSource();
682 for (int ndx = 0; ndx < m_elementsPerInvocation; ++ndx)
684 // check another invocation's value or our own value depending on test type
685 const std::string localID = (m_syncWithGroup) ? ("(gl_LocalInvocationID.xy + uvec2(" + de::toString(ndx+1) + ", " + de::toString(2*ndx) + ")) % gl_WorkGroupSize.xy") : ("gl_LocalInvocationID.xy");
687 if (m_storage == STORAGE_BUFFER && m_useAtomic)
688 buf << "\tallOk = allOk && (atomicExchange(sb_store.values[getIndex(" << localID << ", " << ndx << ")], 123) == groupNdx);\n";
689 else if (m_storage == STORAGE_BUFFER && !m_useAtomic)
690 buf << "\tallOk = allOk && (sb_store.values[getIndex(" << localID << ", " << ndx << ")] == groupNdx);\n";
691 else if (m_storage == STORAGE_IMAGE && m_useAtomic)
692 buf << "\tallOk = allOk && (imageAtomicExchange(u_image, getCoord(" << localID << ", " << ndx << "), 123) == groupNdx);\n";
693 else if (m_storage == STORAGE_IMAGE && !m_useAtomic)
694 buf << "\tallOk = allOk && (imageLoad(u_image, getCoord(" << localID << ", " << ndx << ")).x == groupNdx);\n";
702 class InvocationAliasWriteCase : public InterInvocationTestCase
713 InvocationAliasWriteCase (Context& context, const char* name, const char* desc, TestType type, StorageType storage, int flags);
715 std::string genShaderSource (void) const;
717 const TestType m_type;
720 InvocationAliasWriteCase::InvocationAliasWriteCase (Context& context, const char* name, const char* desc, TestType type, StorageType storage, int flags)
721 : InterInvocationTestCase (context, name, desc, storage, flags | FLAG_ALIASING_STORAGES)
724 DE_ASSERT(type < TYPE_LAST);
727 std::string InvocationAliasWriteCase::genShaderSource (void) const
729 const bool useImageAtomics = m_useAtomic && m_storage == STORAGE_IMAGE;
730 std::ostringstream buf;
732 buf << "#version 310 es\n"
733 << ((useImageAtomics) ? ("#extension GL_OES_shader_image_atomic : require\n") : (""))
734 << "layout (local_size_x=" << m_localWidth << ", local_size_y=" << m_localHeight << ") in;\n"
735 << "layout(binding=0, std430) buffer Output\n"
737 << " highp int values[];\n"
740 if (m_storage == STORAGE_BUFFER)
741 buf << "layout(binding=1, std430) coherent buffer Storage0\n"
743 << " highp int values[];\n"
745 << "layout(binding=2, std430) coherent buffer Storage1\n"
747 << " highp int values[];\n"
750 << "highp int getIndex (in highp uvec2 localID, in highp int element)\n"
752 << " highp uint groupNdx = gl_NumWorkGroups.x * gl_WorkGroupID.y + gl_WorkGroupID.x;\n"
753 << " return int((localID.y * gl_NumWorkGroups.x * gl_NumWorkGroups.y * gl_WorkGroupSize.x) + (groupNdx * gl_WorkGroupSize.x) + localID.x) * " << m_elementsPerInvocation << " + element;\n"
755 else if (m_storage == STORAGE_IMAGE)
756 buf << "layout(r32i, binding=1) coherent uniform highp iimage2D u_image0;\n"
757 << "layout(r32i, binding=2) coherent uniform highp iimage2D u_image1;\n"
759 << "highp ivec2 getCoord (in highp uvec2 localID, in highp int element)\n"
761 << " return ivec2(int(gl_WorkGroupID.x * gl_WorkGroupSize.x + localID.x), int(gl_WorkGroupID.y * gl_WorkGroupSize.y + localID.y) + element * " << m_workHeight << ");\n"
767 << "void main (void)\n"
769 << " int resultNdx = int(gl_GlobalInvocationID.y * gl_NumWorkGroups.x * gl_WorkGroupSize.x + gl_GlobalInvocationID.x);\n"
770 << " int groupNdx = int(gl_NumWorkGroups.x * gl_WorkGroupID.y + gl_WorkGroupID.x);\n"
771 << " bool allOk = true;\n"
774 if (m_type == TYPE_OVERWRITE)
778 for (int ndx = 0; ndx < m_elementsPerInvocation; ++ndx)
780 if (m_storage == STORAGE_BUFFER && m_useAtomic)
781 buf << "\tatomicAdd(sb_store0.values[getIndex(gl_LocalInvocationID.xy, " << ndx << ")], 456);\n";
782 else if (m_storage == STORAGE_BUFFER && !m_useAtomic)
783 buf << "\tsb_store0.values[getIndex(gl_LocalInvocationID.xy, " << ndx << ")] = 456;\n";
784 else if (m_storage == STORAGE_IMAGE && m_useAtomic)
785 buf << "\timageAtomicAdd(u_image0, getCoord(gl_LocalInvocationID.xy, " << ndx << "), 456);\n";
786 else if (m_storage == STORAGE_IMAGE && !m_useAtomic)
787 buf << "\timageStore(u_image0, getCoord(gl_LocalInvocationID.xy, " << ndx << "), ivec4(456, 0, 0, 0));\n";
794 buf << genBarrierSource();
797 DE_ASSERT(m_type == TYPE_WRITE);
801 for (int ndx = 0; ndx < m_elementsPerInvocation; ++ndx)
803 const std::string localID = (m_syncWithGroup) ? ("(gl_LocalInvocationID.xy + uvec2(" + de::toString(ndx+2) + ", " + de::toString(2*ndx) + ")) % gl_WorkGroupSize.xy") : ("gl_LocalInvocationID.xy");
805 if (m_storage == STORAGE_BUFFER && m_useAtomic)
806 buf << "\tatomicExchange(sb_store1.values[getIndex(" << localID << ", " << ndx << ")], groupNdx);\n";
807 else if (m_storage == STORAGE_BUFFER && !m_useAtomic)
808 buf << "\tsb_store1.values[getIndex(" << localID << ", " << ndx << ")] = groupNdx;\n";
809 else if (m_storage == STORAGE_IMAGE && m_useAtomic)
810 buf << "\timageAtomicExchange(u_image1, getCoord(" << localID << ", " << ndx << "), groupNdx);\n";
811 else if (m_storage == STORAGE_IMAGE && !m_useAtomic)
812 buf << "\timageStore(u_image1, getCoord(" << localID << ", " << ndx << "), ivec4(groupNdx, 0, 0, 0));\n";
819 buf << genBarrierSource();
823 for (int ndx = 0; ndx < m_elementsPerInvocation; ++ndx)
825 if (m_storage == STORAGE_BUFFER && m_useAtomic)
826 buf << "\tallOk = allOk && (atomicExchange(sb_store0.values[getIndex(gl_LocalInvocationID.xy, " << ndx << ")], 123) == groupNdx);\n";
827 else if (m_storage == STORAGE_BUFFER && !m_useAtomic)
828 buf << "\tallOk = allOk && (sb_store0.values[getIndex(gl_LocalInvocationID.xy, " << ndx << ")] == groupNdx);\n";
829 else if (m_storage == STORAGE_IMAGE && m_useAtomic)
830 buf << "\tallOk = allOk && (imageAtomicExchange(u_image0, getCoord(gl_LocalInvocationID.xy, " << ndx << "), 123) == groupNdx);\n";
831 else if (m_storage == STORAGE_IMAGE && !m_useAtomic)
832 buf << "\tallOk = allOk && (imageLoad(u_image0, getCoord(gl_LocalInvocationID.xy, " << ndx << ")).x == groupNdx);\n";
840 << " sb_result.values[resultNdx] = (allOk) ? (1) : (0);\n"
854 static WriteData Generate(int targetHandle, int seed)
858 retVal.targetHandle = targetHandle;
870 static ReadData Generate(int targetHandle, int seed)
874 retVal.targetHandle = targetHandle;
885 struct WriteDataInterleaved
891 static WriteDataInterleaved Generate(int targetHandle, int seed, bool evenOdd)
893 WriteDataInterleaved retVal;
895 retVal.targetHandle = targetHandle;
897 retVal.evenOdd = evenOdd;
903 struct ReadDataInterleaved
909 static ReadDataInterleaved Generate(int targetHandle, int seed0, int seed1)
911 ReadDataInterleaved retVal;
913 retVal.targetHandle = targetHandle;
914 retVal.seed0 = seed0;
915 retVal.seed1 = seed1;
921 struct ReadMultipleData
928 static ReadMultipleData Generate(int targetHandle0, int seed0, int targetHandle1, int seed1)
930 ReadMultipleData retVal;
932 retVal.targetHandle0 = targetHandle0;
933 retVal.seed0 = seed0;
934 retVal.targetHandle1 = targetHandle1;
935 retVal.seed1 = seed1;
945 static ReadZeroData Generate(int targetHandle)
949 retVal.targetHandle = targetHandle;
957 class InterCallTestCase;
959 class InterCallOperations
962 InterCallOperations& operator<< (const op::WriteData&);
963 InterCallOperations& operator<< (const op::ReadData&);
964 InterCallOperations& operator<< (const op::Barrier&);
965 InterCallOperations& operator<< (const op::ReadMultipleData&);
966 InterCallOperations& operator<< (const op::WriteDataInterleaved&);
967 InterCallOperations& operator<< (const op::ReadDataInterleaved&);
968 InterCallOperations& operator<< (const op::ReadZeroData&);
979 TYPE_WRITE_INTERLEAVE,
980 TYPE_READ_INTERLEAVE,
993 op::ReadMultipleData readMulti;
994 op::WriteDataInterleaved writeInterleave;
995 op::ReadDataInterleaved readInterleave;
996 op::ReadZeroData readZero;
1000 friend class InterCallTestCase;
1002 std::vector<Command> m_cmds;
1005 InterCallOperations& InterCallOperations::operator<< (const op::WriteData& cmd)
1007 m_cmds.push_back(Command());
1008 m_cmds.back().type = Command::TYPE_WRITE;
1009 m_cmds.back().u_cmd.write = cmd;
1014 InterCallOperations& InterCallOperations::operator<< (const op::ReadData& cmd)
1016 m_cmds.push_back(Command());
1017 m_cmds.back().type = Command::TYPE_READ;
1018 m_cmds.back().u_cmd.read = cmd;
1023 InterCallOperations& InterCallOperations::operator<< (const op::Barrier& cmd)
1025 m_cmds.push_back(Command());
1026 m_cmds.back().type = Command::TYPE_BARRIER;
1027 m_cmds.back().u_cmd.barrier = cmd;
1032 InterCallOperations& InterCallOperations::operator<< (const op::ReadMultipleData& cmd)
1034 m_cmds.push_back(Command());
1035 m_cmds.back().type = Command::TYPE_READ_MULTIPLE;
1036 m_cmds.back().u_cmd.readMulti = cmd;
1041 InterCallOperations& InterCallOperations::operator<< (const op::WriteDataInterleaved& cmd)
1043 m_cmds.push_back(Command());
1044 m_cmds.back().type = Command::TYPE_WRITE_INTERLEAVE;
1045 m_cmds.back().u_cmd.writeInterleave = cmd;
1050 InterCallOperations& InterCallOperations::operator<< (const op::ReadDataInterleaved& cmd)
1052 m_cmds.push_back(Command());
1053 m_cmds.back().type = Command::TYPE_READ_INTERLEAVE;
1054 m_cmds.back().u_cmd.readInterleave = cmd;
1059 InterCallOperations& InterCallOperations::operator<< (const op::ReadZeroData& cmd)
1061 m_cmds.push_back(Command());
1062 m_cmds.back().type = Command::TYPE_READ_ZERO;
1063 m_cmds.back().u_cmd.readZero = cmd;
1068 class InterCallTestCase : public TestCase
1080 FLAG_USE_ATOMIC = 1,
1083 InterCallTestCase (Context& context, const char* name, const char* desc, StorageType storage, int flags, const InterCallOperations& ops);
1084 ~InterCallTestCase (void);
1089 IterateResult iterate (void);
1090 bool verifyResults (void);
1092 void runCommand (const op::WriteData& cmd, int stepNdx, int& programFriendlyName);
1093 void runCommand (const op::ReadData& cmd, int stepNdx, int& programFriendlyName, int& resultStorageFriendlyName);
1094 void runCommand (const op::Barrier&);
1095 void runCommand (const op::ReadMultipleData& cmd, int stepNdx, int& programFriendlyName, int& resultStorageFriendlyName);
1096 void runCommand (const op::WriteDataInterleaved& cmd, int stepNdx, int& programFriendlyName);
1097 void runCommand (const op::ReadDataInterleaved& cmd, int stepNdx, int& programFriendlyName, int& resultStorageFriendlyName);
1098 void runCommand (const op::ReadZeroData& cmd, int stepNdx, int& programFriendlyName, int& resultStorageFriendlyName);
1099 void runSingleRead (int targetHandle, int stepNdx, int& programFriendlyName, int& resultStorageFriendlyName);
1101 glw::GLuint genStorage (int friendlyName);
1102 glw::GLuint genResultStorage (void);
1103 glu::ShaderProgram* genWriteProgram (int seed);
1104 glu::ShaderProgram* genReadProgram (int seed);
1105 glu::ShaderProgram* genReadMultipleProgram (int seed0, int seed1);
1106 glu::ShaderProgram* genWriteInterleavedProgram (int seed, bool evenOdd);
1107 glu::ShaderProgram* genReadInterleavedProgram (int seed0, int seed1);
1108 glu::ShaderProgram* genReadZeroProgram (void);
1110 const StorageType m_storage;
1111 const int m_invocationGridSize; // !< width and height of the two dimensional work dispatch
1112 const int m_perInvocationSize; // !< number of elements accessed in single invocation
1113 const std::vector<InterCallOperations::Command> m_cmds;
1114 const bool m_useAtomic;
1115 const bool m_formatInteger;
1117 std::vector<glu::ShaderProgram*> m_operationPrograms;
1118 std::vector<glw::GLuint> m_operationResultStorages;
1119 std::map<int, glw::GLuint> m_storageIDs;
1122 InterCallTestCase::InterCallTestCase (Context& context, const char* name, const char* desc, StorageType storage, int flags, const InterCallOperations& ops)
1123 : TestCase (context, name, desc)
1124 , m_storage (storage)
1125 , m_invocationGridSize (512)
1126 , m_perInvocationSize (2)
1127 , m_cmds (ops.m_cmds)
1128 , m_useAtomic ((flags & FLAG_USE_ATOMIC) != 0)
1129 , m_formatInteger ((flags & FLAG_USE_INT) != 0)
1133 InterCallTestCase::~InterCallTestCase (void)
1138 void InterCallTestCase::init (void)
1140 int programFriendlyName = 0;
1144 if (m_useAtomic && m_storage == STORAGE_IMAGE && !m_context.getContextInfo().isExtensionSupported("GL_OES_shader_image_atomic"))
1145 throw tcu::NotSupportedError("Test requires GL_OES_shader_image_atomic extension");
1147 // generate resources and validate command list
1149 m_operationPrograms.resize(m_cmds.size(), DE_NULL);
1150 m_operationResultStorages.resize(m_cmds.size(), 0);
1152 for (int step = 0; step < (int)m_cmds.size(); ++step)
1154 switch (m_cmds[step].type)
1156 case InterCallOperations::Command::TYPE_WRITE:
1158 const op::WriteData& cmd = m_cmds[step].u_cmd.write;
1160 // new storage handle?
1161 if (m_storageIDs.find(cmd.targetHandle) == m_storageIDs.end())
1162 m_storageIDs[cmd.targetHandle] = genStorage(cmd.targetHandle);
1166 glu::ShaderProgram* program = genWriteProgram(cmd.seed);
1168 m_testCtx.getLog() << tcu::TestLog::Message << "Program #" << ++programFriendlyName << tcu::TestLog::EndMessage;
1169 m_testCtx.getLog() << *program;
1171 if (!program->isOk())
1172 throw tcu::TestError("could not build program");
1174 m_operationPrograms[step] = program;
1179 case InterCallOperations::Command::TYPE_READ:
1181 const op::ReadData& cmd = m_cmds[step].u_cmd.read;
1182 DE_ASSERT(m_storageIDs.find(cmd.targetHandle) != m_storageIDs.end());
1184 // program and result storage
1186 glu::ShaderProgram* program = genReadProgram(cmd.seed);
1188 m_testCtx.getLog() << tcu::TestLog::Message << "Program #" << ++programFriendlyName << tcu::TestLog::EndMessage;
1189 m_testCtx.getLog() << *program;
1191 if (!program->isOk())
1192 throw tcu::TestError("could not build program");
1194 m_operationPrograms[step] = program;
1195 m_operationResultStorages[step] = genResultStorage();
1200 case InterCallOperations::Command::TYPE_BARRIER:
1205 case InterCallOperations::Command::TYPE_READ_MULTIPLE:
1207 const op::ReadMultipleData& cmd = m_cmds[step].u_cmd.readMulti;
1208 DE_ASSERT(m_storageIDs.find(cmd.targetHandle0) != m_storageIDs.end());
1209 DE_ASSERT(m_storageIDs.find(cmd.targetHandle1) != m_storageIDs.end());
1213 glu::ShaderProgram* program = genReadMultipleProgram(cmd.seed0, cmd.seed1);
1215 m_testCtx.getLog() << tcu::TestLog::Message << "Program #" << ++programFriendlyName << tcu::TestLog::EndMessage;
1216 m_testCtx.getLog() << *program;
1218 if (!program->isOk())
1219 throw tcu::TestError("could not build program");
1221 m_operationPrograms[step] = program;
1222 m_operationResultStorages[step] = genResultStorage();
1227 case InterCallOperations::Command::TYPE_WRITE_INTERLEAVE:
1229 const op::WriteDataInterleaved& cmd = m_cmds[step].u_cmd.writeInterleave;
1231 // new storage handle?
1232 if (m_storageIDs.find(cmd.targetHandle) == m_storageIDs.end())
1233 m_storageIDs[cmd.targetHandle] = genStorage(cmd.targetHandle);
1237 glu::ShaderProgram* program = genWriteInterleavedProgram(cmd.seed, cmd.evenOdd);
1239 m_testCtx.getLog() << tcu::TestLog::Message << "Program #" << ++programFriendlyName << tcu::TestLog::EndMessage;
1240 m_testCtx.getLog() << *program;
1242 if (!program->isOk())
1243 throw tcu::TestError("could not build program");
1245 m_operationPrograms[step] = program;
1250 case InterCallOperations::Command::TYPE_READ_INTERLEAVE:
1252 const op::ReadDataInterleaved& cmd = m_cmds[step].u_cmd.readInterleave;
1253 DE_ASSERT(m_storageIDs.find(cmd.targetHandle) != m_storageIDs.end());
1257 glu::ShaderProgram* program = genReadInterleavedProgram(cmd.seed0, cmd.seed1);
1259 m_testCtx.getLog() << tcu::TestLog::Message << "Program #" << ++programFriendlyName << tcu::TestLog::EndMessage;
1260 m_testCtx.getLog() << *program;
1262 if (!program->isOk())
1263 throw tcu::TestError("could not build program");
1265 m_operationPrograms[step] = program;
1266 m_operationResultStorages[step] = genResultStorage();
1271 case InterCallOperations::Command::TYPE_READ_ZERO:
1273 const op::ReadZeroData& cmd = m_cmds[step].u_cmd.readZero;
1275 // new storage handle?
1276 if (m_storageIDs.find(cmd.targetHandle) == m_storageIDs.end())
1277 m_storageIDs[cmd.targetHandle] = genStorage(cmd.targetHandle);
1281 glu::ShaderProgram* program = genReadZeroProgram();
1283 m_testCtx.getLog() << tcu::TestLog::Message << "Program #" << ++programFriendlyName << tcu::TestLog::EndMessage;
1284 m_testCtx.getLog() << *program;
1286 if (!program->isOk())
1287 throw tcu::TestError("could not build program");
1289 m_operationPrograms[step] = program;
1290 m_operationResultStorages[step] = genResultStorage();
1296 DE_ASSERT(DE_FALSE);
1301 void InterCallTestCase::deinit (void)
1304 for (int ndx = 0; ndx < (int)m_operationPrograms.size(); ++ndx)
1305 delete m_operationPrograms[ndx];
1306 m_operationPrograms.clear();
1309 for (int ndx = 0; ndx < (int)m_operationResultStorages.size(); ++ndx)
1311 if (m_operationResultStorages[ndx])
1312 m_context.getRenderContext().getFunctions().deleteBuffers(1, &m_operationResultStorages[ndx]);
1314 m_operationResultStorages.clear();
1317 for (std::map<int, glw::GLuint>::const_iterator it = m_storageIDs.begin(); it != m_storageIDs.end(); ++it)
1319 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1321 if (m_storage == STORAGE_BUFFER)
1322 gl.deleteBuffers(1, &it->second);
1323 else if (m_storage == STORAGE_IMAGE)
1324 gl.deleteTextures(1, &it->second);
1326 DE_ASSERT(DE_FALSE);
1328 m_storageIDs.clear();
1331 InterCallTestCase::IterateResult InterCallTestCase::iterate (void)
1333 int programFriendlyName = 0;
1334 int resultStorageFriendlyName = 0;
1336 m_testCtx.getLog() << tcu::TestLog::Message << "Running operations:" << tcu::TestLog::EndMessage;
1340 for (int step = 0; step < (int)m_cmds.size(); ++step)
1342 switch (m_cmds[step].type)
1344 case InterCallOperations::Command::TYPE_WRITE: runCommand(m_cmds[step].u_cmd.write, step, programFriendlyName); break;
1345 case InterCallOperations::Command::TYPE_READ: runCommand(m_cmds[step].u_cmd.read, step, programFriendlyName, resultStorageFriendlyName); break;
1346 case InterCallOperations::Command::TYPE_BARRIER: runCommand(m_cmds[step].u_cmd.barrier); break;
1347 case InterCallOperations::Command::TYPE_READ_MULTIPLE: runCommand(m_cmds[step].u_cmd.readMulti, step, programFriendlyName, resultStorageFriendlyName); break;
1348 case InterCallOperations::Command::TYPE_WRITE_INTERLEAVE: runCommand(m_cmds[step].u_cmd.writeInterleave, step, programFriendlyName); break;
1349 case InterCallOperations::Command::TYPE_READ_INTERLEAVE: runCommand(m_cmds[step].u_cmd.readInterleave, step, programFriendlyName, resultStorageFriendlyName); break;
1350 case InterCallOperations::Command::TYPE_READ_ZERO: runCommand(m_cmds[step].u_cmd.readZero, step, programFriendlyName, resultStorageFriendlyName); break;
1352 DE_ASSERT(DE_FALSE);
1356 // read results from result buffers
1357 if (verifyResults())
1358 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
1360 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, (std::string((m_storage == STORAGE_BUFFER) ? ("buffer") : ("image")) + " content verification failed").c_str());
1365 bool InterCallTestCase::verifyResults (void)
1367 int resultBufferFriendlyName = 0;
1368 bool allResultsOk = true;
1369 bool anyResult = false;
1371 m_testCtx.getLog() << tcu::TestLog::Message << "Reading verifier program results" << tcu::TestLog::EndMessage;
1373 for (int step = 0; step < (int)m_cmds.size(); ++step)
1375 const int errorFloodThreshold = 5;
1376 int numErrorsLogged = 0;
1378 if (m_operationResultStorages[step])
1380 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1381 const void* mapped = DE_NULL;
1382 std::vector<deInt32> results (m_invocationGridSize * m_invocationGridSize);
1387 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_operationResultStorages[step]);
1388 mapped = gl.mapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, m_invocationGridSize * m_invocationGridSize * sizeof(deUint32), GL_MAP_READ_BIT);
1389 GLU_EXPECT_NO_ERROR(gl.getError(), "map buffer");
1391 // copy to properly aligned array
1392 deMemcpy(&results[0], mapped, m_invocationGridSize * m_invocationGridSize * sizeof(deUint32));
1394 if (gl.unmapBuffer(GL_SHADER_STORAGE_BUFFER) != GL_TRUE)
1395 throw tcu::TestError("memory map store corrupted");
1397 // check the results
1398 for (int ndx = 0; ndx < (int)results.size(); ++ndx)
1400 if (results[ndx] != 1)
1404 if (numErrorsLogged == 0)
1405 m_testCtx.getLog() << tcu::TestLog::Message << "Result storage #" << ++resultBufferFriendlyName << " failed, got unexpected values.\n" << tcu::TestLog::EndMessage;
1406 if (numErrorsLogged++ < errorFloodThreshold)
1407 m_testCtx.getLog() << tcu::TestLog::Message << " Error at index " << ndx << ": expected 1, got " << results[ndx] << ".\n" << tcu::TestLog::EndMessage;
1410 // after N errors, no point continuing verification
1411 m_testCtx.getLog() << tcu::TestLog::Message << " -- too many errors, skipping verification --\n" << tcu::TestLog::EndMessage;
1419 allResultsOk = false;
1422 m_testCtx.getLog() << tcu::TestLog::Message << "Result storage #" << ++resultBufferFriendlyName << " ok." << tcu::TestLog::EndMessage;
1426 DE_ASSERT(anyResult);
1427 DE_UNREF(anyResult);
1429 return allResultsOk;
1432 void InterCallTestCase::runCommand (const op::WriteData& cmd, int stepNdx, int& programFriendlyName)
1434 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1437 << tcu::TestLog::Message
1438 << "Running program #" << ++programFriendlyName << " to write " << ((m_storage == STORAGE_BUFFER) ? ("buffer") : ("image")) << " #" << cmd.targetHandle << ".\n"
1439 << " Dispatch size: " << m_invocationGridSize << "x" << m_invocationGridSize << "."
1440 << tcu::TestLog::EndMessage;
1442 gl.useProgram(m_operationPrograms[stepNdx]->getProgram());
1445 if (m_storage == STORAGE_BUFFER)
1447 DE_ASSERT(m_storageIDs[cmd.targetHandle]);
1449 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_storageIDs[cmd.targetHandle]);
1450 GLU_EXPECT_NO_ERROR(gl.getError(), "bind destination buffer");
1452 else if (m_storage == STORAGE_IMAGE)
1454 DE_ASSERT(m_storageIDs[cmd.targetHandle]);
1456 gl.bindImageTexture(0, m_storageIDs[cmd.targetHandle], 0, GL_FALSE, 0, (m_useAtomic) ? (GL_READ_WRITE) : (GL_WRITE_ONLY), (m_formatInteger) ? (GL_R32I) : (GL_R32F));
1457 GLU_EXPECT_NO_ERROR(gl.getError(), "bind destination image");
1460 DE_ASSERT(DE_FALSE);
1463 gl.dispatchCompute(m_invocationGridSize, m_invocationGridSize, 1);
1464 GLU_EXPECT_NO_ERROR(gl.getError(), "dispatch write");
1467 void InterCallTestCase::runCommand (const op::ReadData& cmd, int stepNdx, int& programFriendlyName, int& resultStorageFriendlyName)
1469 runSingleRead(cmd.targetHandle, stepNdx, programFriendlyName, resultStorageFriendlyName);
1472 void InterCallTestCase::runCommand (const op::Barrier& cmd)
1474 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1478 if (m_storage == STORAGE_BUFFER)
1480 m_testCtx.getLog() << tcu::TestLog::Message << "Memory Barrier\n\tbits = GL_SHADER_STORAGE_BARRIER_BIT" << tcu::TestLog::EndMessage;
1481 gl.memoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
1483 else if (m_storage == STORAGE_IMAGE)
1485 m_testCtx.getLog() << tcu::TestLog::Message << "Memory Barrier\n\tbits = GL_SHADER_IMAGE_ACCESS_BARRIER_BIT" << tcu::TestLog::EndMessage;
1486 gl.memoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
1489 DE_ASSERT(DE_FALSE);
1492 void InterCallTestCase::runCommand (const op::ReadMultipleData& cmd, int stepNdx, int& programFriendlyName, int& resultStorageFriendlyName)
1494 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1497 << tcu::TestLog::Message
1498 << "Running program #" << ++programFriendlyName << " to verify " << ((m_storage == STORAGE_BUFFER) ? ("buffers") : ("images")) << " #" << cmd.targetHandle0 << " and #" << cmd.targetHandle1 << ".\n"
1499 << " Writing results to result storage #" << ++resultStorageFriendlyName << ".\n"
1500 << " Dispatch size: " << m_invocationGridSize << "x" << m_invocationGridSize << "."
1501 << tcu::TestLog::EndMessage;
1503 gl.useProgram(m_operationPrograms[stepNdx]->getProgram());
1506 if (m_storage == STORAGE_BUFFER)
1508 DE_ASSERT(m_storageIDs[cmd.targetHandle0]);
1509 DE_ASSERT(m_storageIDs[cmd.targetHandle1]);
1511 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_storageIDs[cmd.targetHandle0]);
1512 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_storageIDs[cmd.targetHandle1]);
1513 GLU_EXPECT_NO_ERROR(gl.getError(), "bind source buffers");
1515 else if (m_storage == STORAGE_IMAGE)
1517 DE_ASSERT(m_storageIDs[cmd.targetHandle0]);
1518 DE_ASSERT(m_storageIDs[cmd.targetHandle1]);
1520 gl.bindImageTexture(1, m_storageIDs[cmd.targetHandle0], 0, GL_FALSE, 0, (m_useAtomic) ? (GL_READ_WRITE) : (GL_READ_ONLY), (m_formatInteger) ? (GL_R32I) : (GL_R32F));
1521 gl.bindImageTexture(2, m_storageIDs[cmd.targetHandle1], 0, GL_FALSE, 0, (m_useAtomic) ? (GL_READ_WRITE) : (GL_READ_ONLY), (m_formatInteger) ? (GL_R32I) : (GL_R32F));
1522 GLU_EXPECT_NO_ERROR(gl.getError(), "bind source images");
1525 DE_ASSERT(DE_FALSE);
1528 DE_ASSERT(m_operationResultStorages[stepNdx]);
1529 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_operationResultStorages[stepNdx]);
1530 GLU_EXPECT_NO_ERROR(gl.getError(), "bind result buffer");
1533 gl.dispatchCompute(m_invocationGridSize, m_invocationGridSize, 1);
1534 GLU_EXPECT_NO_ERROR(gl.getError(), "dispatch read multi");
1537 void InterCallTestCase::runCommand (const op::WriteDataInterleaved& cmd, int stepNdx, int& programFriendlyName)
1539 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1542 << tcu::TestLog::Message
1543 << "Running program #" << ++programFriendlyName << " to write " << ((m_storage == STORAGE_BUFFER) ? ("buffer") : ("image")) << " #" << cmd.targetHandle << ".\n"
1544 << " Writing to every " << ((cmd.evenOdd) ? ("even") : ("odd")) << " " << ((m_storage == STORAGE_BUFFER) ? ("element") : ("column")) << ".\n"
1545 << " Dispatch size: " << m_invocationGridSize / 2 << "x" << m_invocationGridSize << "."
1546 << tcu::TestLog::EndMessage;
1548 gl.useProgram(m_operationPrograms[stepNdx]->getProgram());
1551 if (m_storage == STORAGE_BUFFER)
1553 DE_ASSERT(m_storageIDs[cmd.targetHandle]);
1555 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_storageIDs[cmd.targetHandle]);
1556 GLU_EXPECT_NO_ERROR(gl.getError(), "bind destination buffer");
1558 else if (m_storage == STORAGE_IMAGE)
1560 DE_ASSERT(m_storageIDs[cmd.targetHandle]);
1562 gl.bindImageTexture(0, m_storageIDs[cmd.targetHandle], 0, GL_FALSE, 0, (m_useAtomic) ? (GL_READ_WRITE) : (GL_WRITE_ONLY), (m_formatInteger) ? (GL_R32I) : (GL_R32F));
1563 GLU_EXPECT_NO_ERROR(gl.getError(), "bind destination image");
1566 DE_ASSERT(DE_FALSE);
1569 gl.dispatchCompute(m_invocationGridSize / 2, m_invocationGridSize, 1);
1570 GLU_EXPECT_NO_ERROR(gl.getError(), "dispatch write");
1573 void InterCallTestCase::runCommand (const op::ReadDataInterleaved& cmd, int stepNdx, int& programFriendlyName, int& resultStorageFriendlyName)
1575 runSingleRead(cmd.targetHandle, stepNdx, programFriendlyName, resultStorageFriendlyName);
1578 void InterCallTestCase::runCommand (const op::ReadZeroData& cmd, int stepNdx, int& programFriendlyName, int& resultStorageFriendlyName)
1580 runSingleRead(cmd.targetHandle, stepNdx, programFriendlyName, resultStorageFriendlyName);
1583 void InterCallTestCase::runSingleRead (int targetHandle, int stepNdx, int& programFriendlyName, int& resultStorageFriendlyName)
1585 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1588 << tcu::TestLog::Message
1589 << "Running program #" << ++programFriendlyName << " to verify " << ((m_storage == STORAGE_BUFFER) ? ("buffer") : ("image")) << " #" << targetHandle << ".\n"
1590 << " Writing results to result storage #" << ++resultStorageFriendlyName << ".\n"
1591 << " Dispatch size: " << m_invocationGridSize << "x" << m_invocationGridSize << "."
1592 << tcu::TestLog::EndMessage;
1594 gl.useProgram(m_operationPrograms[stepNdx]->getProgram());
1597 if (m_storage == STORAGE_BUFFER)
1599 DE_ASSERT(m_storageIDs[targetHandle]);
1601 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_storageIDs[targetHandle]);
1602 GLU_EXPECT_NO_ERROR(gl.getError(), "bind source buffer");
1604 else if (m_storage == STORAGE_IMAGE)
1606 DE_ASSERT(m_storageIDs[targetHandle]);
1608 gl.bindImageTexture(1, m_storageIDs[targetHandle], 0, GL_FALSE, 0, (m_useAtomic) ? (GL_READ_WRITE) : (GL_READ_ONLY), (m_formatInteger) ? (GL_R32I) : (GL_R32F));
1609 GLU_EXPECT_NO_ERROR(gl.getError(), "bind source image");
1612 DE_ASSERT(DE_FALSE);
1615 DE_ASSERT(m_operationResultStorages[stepNdx]);
1616 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_operationResultStorages[stepNdx]);
1617 GLU_EXPECT_NO_ERROR(gl.getError(), "bind result buffer");
1620 gl.dispatchCompute(m_invocationGridSize, m_invocationGridSize, 1);
1621 GLU_EXPECT_NO_ERROR(gl.getError(), "dispatch read");
1624 glw::GLuint InterCallTestCase::genStorage (int friendlyName)
1626 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1628 if (m_storage == STORAGE_BUFFER)
1630 const int numElements = m_invocationGridSize * m_invocationGridSize * m_perInvocationSize;
1631 const int bufferSize = numElements * (int)((m_formatInteger) ? (sizeof(deInt32)) : (sizeof(glw::GLfloat)));
1632 glw::GLuint retVal = 0;
1634 m_testCtx.getLog() << tcu::TestLog::Message << "Creating buffer #" << friendlyName << ", size " << bufferSize << " bytes." << tcu::TestLog::EndMessage;
1636 gl.genBuffers(1, &retVal);
1637 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, retVal);
1639 if (m_formatInteger)
1641 const std::vector<deUint32> zeroBuffer(numElements, 0);
1642 gl.bufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &zeroBuffer[0], GL_STATIC_DRAW);
1646 const std::vector<float> zeroBuffer(numElements, 0.0f);
1647 gl.bufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &zeroBuffer[0], GL_STATIC_DRAW);
1649 GLU_EXPECT_NO_ERROR(gl.getError(), "gen buffer");
1653 else if (m_storage == STORAGE_IMAGE)
1655 const int imageWidth = m_invocationGridSize;
1656 const int imageHeight = m_invocationGridSize * m_perInvocationSize;
1657 glw::GLuint retVal = 0;
1660 << tcu::TestLog::Message
1661 << "Creating image #" << friendlyName << ", size " << imageWidth << "x" << imageHeight
1662 << ", internalformat = " << ((m_formatInteger) ? ("r32i") : ("r32f"))
1663 << ", size = " << (imageWidth*imageHeight*sizeof(deUint32)) << " bytes."
1664 << tcu::TestLog::EndMessage;
1666 gl.genTextures(1, &retVal);
1667 gl.bindTexture(GL_TEXTURE_2D, retVal);
1669 if (m_formatInteger)
1670 gl.texStorage2D(GL_TEXTURE_2D, 1, GL_R32I, imageWidth, imageHeight);
1672 gl.texStorage2D(GL_TEXTURE_2D, 1, GL_R32F, imageWidth, imageHeight);
1674 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1675 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1676 GLU_EXPECT_NO_ERROR(gl.getError(), "gen image");
1679 << tcu::TestLog::Message
1680 << "Filling image with 0"
1681 << tcu::TestLog::EndMessage;
1683 if (m_formatInteger)
1685 const std::vector<deInt32> zeroBuffer(imageWidth * imageHeight, 0);
1686 gl.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, imageWidth, imageHeight, GL_RED_INTEGER, GL_INT, &zeroBuffer[0]);
1690 const std::vector<float> zeroBuffer(imageWidth * imageHeight, 0.0f);
1691 gl.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, imageWidth, imageHeight, GL_RED, GL_FLOAT, &zeroBuffer[0]);
1694 GLU_EXPECT_NO_ERROR(gl.getError(), "specify image contents");
1700 DE_ASSERT(DE_FALSE);
1705 glw::GLuint InterCallTestCase::genResultStorage (void)
1707 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1708 glw::GLuint retVal = 0;
1710 gl.genBuffers(1, &retVal);
1711 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, retVal);
1712 gl.bufferData(GL_SHADER_STORAGE_BUFFER, m_invocationGridSize * m_invocationGridSize * sizeof(deUint32), DE_NULL, GL_STATIC_DRAW);
1713 GLU_EXPECT_NO_ERROR(gl.getError(), "gen buffer");
1718 glu::ShaderProgram* InterCallTestCase::genWriteProgram (int seed)
1720 const bool useImageAtomics = m_useAtomic && m_storage == STORAGE_IMAGE;
1721 std::ostringstream buf;
1723 buf << "#version 310 es\n"
1724 << ((useImageAtomics) ? ("#extension GL_OES_shader_image_atomic : require\n") : (""))
1725 << "layout (local_size_x = 1, local_size_y = 1) in;\n";
1727 if (m_storage == STORAGE_BUFFER)
1728 buf << "layout(binding=0, std430) " << ((m_useAtomic) ? ("coherent ") : ("")) << "buffer Buffer\n"
1730 << " highp " << ((m_formatInteger) ? ("int") : ("float")) << " values[];\n"
1732 else if (m_storage == STORAGE_IMAGE)
1733 buf << "layout(" << ((m_formatInteger) ? ("r32i") : ("r32f")) << ", binding=0) " << ((m_useAtomic) ? ("coherent ") : ("writeonly ")) << "uniform highp " << ((m_formatInteger) ? ("iimage2D") : ("image2D")) << " u_imageOut;\n";
1735 DE_ASSERT(DE_FALSE);
1738 << "void main (void)\n"
1740 << " uvec3 size = gl_NumWorkGroups * gl_WorkGroupSize;\n"
1741 << " int groupNdx = int(size.x * size.y * gl_GlobalInvocationID.z + size.x*gl_GlobalInvocationID.y + gl_GlobalInvocationID.x);\n"
1744 // Write to buffer/image m_perInvocationSize elements
1745 if (m_storage == STORAGE_BUFFER)
1747 for (int writeNdx = 0; writeNdx < m_perInvocationSize; ++writeNdx)
1750 buf << " atomicExchange(";
1754 buf << "sb_out.values[(groupNdx + " << seed + writeNdx*m_invocationGridSize*m_invocationGridSize << ") % " << m_invocationGridSize*m_invocationGridSize*m_perInvocationSize << "]";
1757 buf << ", " << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n";
1759 buf << " = " << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx);\n";
1762 else if (m_storage == STORAGE_IMAGE)
1764 for (int writeNdx = 0; writeNdx < m_perInvocationSize; ++writeNdx)
1767 buf << " imageAtomicExchange";
1769 buf << " imageStore";
1771 buf << "(u_imageOut, ivec2((int(gl_GlobalInvocationID.x) + " << (seed + writeNdx*100) << ") % " << m_invocationGridSize << ", int(gl_GlobalInvocationID.y) + " << writeNdx*m_invocationGridSize << "), ";
1774 buf << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n";
1776 buf << ((m_formatInteger) ? ("ivec4(int(groupNdx), 0, 0, 0)") : ("vec4(float(groupNdx), 0.0, 0.0, 0.0)")) << ");\n";
1780 DE_ASSERT(DE_FALSE);
1784 return new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(buf.str()));
1787 glu::ShaderProgram* InterCallTestCase::genReadProgram (int seed)
1789 const bool useImageAtomics = m_useAtomic && m_storage == STORAGE_IMAGE;
1790 std::ostringstream buf;
1792 buf << "#version 310 es\n"
1793 << ((useImageAtomics) ? ("#extension GL_OES_shader_image_atomic : require\n") : (""))
1794 << "layout (local_size_x = 1, local_size_y = 1) in;\n";
1796 if (m_storage == STORAGE_BUFFER)
1797 buf << "layout(binding=1, std430) " << ((m_useAtomic) ? ("coherent ") : ("")) << "buffer Buffer\n"
1799 << " highp " << ((m_formatInteger) ? ("int") : ("float")) << " values[];\n"
1801 else if (m_storage == STORAGE_IMAGE)
1802 buf << "layout(" << ((m_formatInteger) ? ("r32i") : ("r32f")) << ", binding=1) " << ((m_useAtomic) ? ("coherent ") : ("readonly ")) << "uniform highp " << ((m_formatInteger) ? ("iimage2D") : ("image2D")) << " u_imageIn;\n";
1804 DE_ASSERT(DE_FALSE);
1806 buf << "layout(binding=0, std430) buffer ResultBuffer\n"
1808 << " highp int resultOk[];\n"
1811 << "void main (void)\n"
1813 << " uvec3 size = gl_NumWorkGroups * gl_WorkGroupSize;\n"
1814 << " int groupNdx = int(size.x * size.y * gl_GlobalInvocationID.z + size.x*gl_GlobalInvocationID.y + gl_GlobalInvocationID.x);\n"
1815 << " " << ((m_formatInteger) ? ("int") : ("float")) << " zero = " << ((m_formatInteger) ? ("0") : ("0.0")) << ";\n"
1816 << " bool allOk = true;\n"
1821 if (m_storage == STORAGE_BUFFER)
1823 for (int readNdx = 0; readNdx < m_perInvocationSize; ++readNdx)
1826 buf << " allOk = allOk && (sb_in.values[(groupNdx + "
1827 << seed + readNdx*m_invocationGridSize*m_invocationGridSize << ") % " << m_invocationGridSize*m_invocationGridSize*m_perInvocationSize << "] == "
1828 << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n";
1830 buf << " allOk = allOk && (atomicExchange(sb_in.values[(groupNdx + "
1831 << seed + readNdx*m_invocationGridSize*m_invocationGridSize << ") % " << m_invocationGridSize*m_invocationGridSize*m_perInvocationSize << "], zero) == "
1832 << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n";
1835 else if (m_storage == STORAGE_IMAGE)
1837 for (int readNdx = 0; readNdx < m_perInvocationSize; ++readNdx)
1840 buf << " allOk = allOk && (imageLoad(u_imageIn, ivec2((gl_GlobalInvocationID.x + "
1841 << (seed + readNdx*100) << "u) % " << m_invocationGridSize << "u, gl_GlobalInvocationID.y + " << readNdx*m_invocationGridSize << "u)).x == "
1842 << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n";
1844 buf << " allOk = allOk && (imageAtomicExchange(u_imageIn, ivec2((gl_GlobalInvocationID.x + "
1845 << (seed + readNdx*100) << "u) % " << m_invocationGridSize << "u, gl_GlobalInvocationID.y + " << readNdx*m_invocationGridSize << "u), zero) == "
1846 << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n";
1850 DE_ASSERT(DE_FALSE);
1852 buf << " sb_result.resultOk[groupNdx] = (allOk) ? (1) : (0);\n"
1855 return new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(buf.str()));
1858 glu::ShaderProgram* InterCallTestCase::genReadMultipleProgram (int seed0, int seed1)
1860 const bool useImageAtomics = m_useAtomic && m_storage == STORAGE_IMAGE;
1861 std::ostringstream buf;
1863 buf << "#version 310 es\n"
1864 << ((useImageAtomics) ? ("#extension GL_OES_shader_image_atomic : require\n") : (""))
1865 << "layout (local_size_x = 1, local_size_y = 1) in;\n";
1867 if (m_storage == STORAGE_BUFFER)
1868 buf << "layout(binding=1, std430) " << ((m_useAtomic) ? ("coherent ") : ("")) << "buffer Buffer0\n"
1870 << " highp " << ((m_formatInteger) ? ("int") : ("float")) << " values[];\n"
1872 << "layout(binding=2, std430) " << ((m_useAtomic) ? ("coherent ") : ("")) << "buffer Buffer1\n"
1874 << " highp " << ((m_formatInteger) ? ("int") : ("float")) << " values[];\n"
1876 else if (m_storage == STORAGE_IMAGE)
1877 buf << "layout(" << ((m_formatInteger) ? ("r32i") : ("r32f")) << ", binding=1) " << ((m_useAtomic) ? ("coherent ") : ("readonly ")) << "uniform highp " << ((m_formatInteger) ? ("iimage2D") : ("image2D")) << " u_imageIn0;\n"
1878 << "layout(" << ((m_formatInteger) ? ("r32i") : ("r32f")) << ", binding=2) " << ((m_useAtomic) ? ("coherent ") : ("readonly ")) << "uniform highp " << ((m_formatInteger) ? ("iimage2D") : ("image2D")) << " u_imageIn1;\n";
1880 DE_ASSERT(DE_FALSE);
1882 buf << "layout(binding=0, std430) buffer ResultBuffer\n"
1884 << " highp int resultOk[];\n"
1887 << "void main (void)\n"
1889 << " uvec3 size = gl_NumWorkGroups * gl_WorkGroupSize;\n"
1890 << " int groupNdx = int(size.x * size.y * gl_GlobalInvocationID.z + size.x*gl_GlobalInvocationID.y + gl_GlobalInvocationID.x);\n"
1891 << " " << ((m_formatInteger) ? ("int") : ("float")) << " zero = " << ((m_formatInteger) ? ("0") : ("0.0")) << ";\n"
1892 << " bool allOk = true;\n"
1897 if (m_storage == STORAGE_BUFFER)
1899 for (int readNdx = 0; readNdx < m_perInvocationSize; ++readNdx)
1900 buf << " allOk = allOk && (" << ((m_useAtomic) ? ("atomicExchange(") : ("")) << "sb_in0.values[(groupNdx + " << seed0 + readNdx*m_invocationGridSize*m_invocationGridSize << ") % " << m_invocationGridSize*m_invocationGridSize*m_perInvocationSize << "]" << ((m_useAtomic) ? (", zero)") : ("")) << " == " << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n"
1901 << " allOk = allOk && (" << ((m_useAtomic) ? ("atomicExchange(") : ("")) << "sb_in1.values[(groupNdx + " << seed1 + readNdx*m_invocationGridSize*m_invocationGridSize << ") % " << m_invocationGridSize*m_invocationGridSize*m_perInvocationSize << "]" << ((m_useAtomic) ? (", zero)") : ("")) << " == " << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n";
1903 else if (m_storage == STORAGE_IMAGE)
1905 for (int readNdx = 0; readNdx < m_perInvocationSize; ++readNdx)
1906 buf << " allOk = allOk && (" << ((m_useAtomic) ? ("imageAtomicExchange") : ("imageLoad")) << "(u_imageIn0, ivec2((gl_GlobalInvocationID.x + " << (seed0 + readNdx*100) << "u) % " << m_invocationGridSize << "u, gl_GlobalInvocationID.y + " << readNdx*m_invocationGridSize << "u)" << ((m_useAtomic) ? (", zero)") : (").x")) << " == " << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n"
1907 << " allOk = allOk && (" << ((m_useAtomic) ? ("imageAtomicExchange") : ("imageLoad")) << "(u_imageIn1, ivec2((gl_GlobalInvocationID.x + " << (seed1 + readNdx*100) << "u) % " << m_invocationGridSize << "u, gl_GlobalInvocationID.y + " << readNdx*m_invocationGridSize << "u)" << ((m_useAtomic) ? (", zero)") : (").x")) << " == " << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n";
1910 DE_ASSERT(DE_FALSE);
1912 buf << " sb_result.resultOk[groupNdx] = (allOk) ? (1) : (0);\n"
1915 return new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(buf.str()));
1918 glu::ShaderProgram* InterCallTestCase::genWriteInterleavedProgram (int seed, bool evenOdd)
1920 const bool useImageAtomics = m_useAtomic && m_storage == STORAGE_IMAGE;
1921 std::ostringstream buf;
1923 buf << "#version 310 es\n"
1924 << ((useImageAtomics) ? ("#extension GL_OES_shader_image_atomic : require\n") : (""))
1925 << "layout (local_size_x = 1, local_size_y = 1) in;\n";
1927 if (m_storage == STORAGE_BUFFER)
1928 buf << "layout(binding=0, std430) " << ((m_useAtomic) ? ("coherent ") : ("")) << "buffer Buffer\n"
1930 << " highp " << ((m_formatInteger) ? ("int") : ("float")) << " values[];\n"
1932 else if (m_storage == STORAGE_IMAGE)
1933 buf << "layout(" << ((m_formatInteger) ? ("r32i") : ("r32f")) << ", binding=0) " << ((m_useAtomic) ? ("coherent ") : ("writeonly ")) << "uniform highp " << ((m_formatInteger) ? ("iimage2D") : ("image2D")) << " u_imageOut;\n";
1935 DE_ASSERT(DE_FALSE);
1938 << "void main (void)\n"
1940 << " uvec3 size = gl_NumWorkGroups * gl_WorkGroupSize;\n"
1941 << " int groupNdx = int(size.x * size.y * gl_GlobalInvocationID.z + size.x*gl_GlobalInvocationID.y + gl_GlobalInvocationID.x);\n"
1944 // Write to buffer/image m_perInvocationSize elements
1945 if (m_storage == STORAGE_BUFFER)
1947 for (int writeNdx = 0; writeNdx < m_perInvocationSize; ++writeNdx)
1950 buf << " atomicExchange(";
1954 buf << "sb_out.values[((groupNdx + " << seed + writeNdx*m_invocationGridSize*m_invocationGridSize / 2 << ") % " << m_invocationGridSize*m_invocationGridSize / 2 * m_perInvocationSize << ") * 2 + " << ((evenOdd) ? (0) : (1)) << "]";
1957 buf << ", " << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n";
1959 buf << "= " << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx);\n";
1962 else if (m_storage == STORAGE_IMAGE)
1964 for (int writeNdx = 0; writeNdx < m_perInvocationSize; ++writeNdx)
1967 buf << " imageAtomicExchange";
1969 buf << " imageStore";
1971 buf << "(u_imageOut, ivec2(((int(gl_GlobalInvocationID.x) + " << (seed + writeNdx*100) << ") % " << m_invocationGridSize / 2 << ") * 2 + " << ((evenOdd) ? (0) : (1)) << ", int(gl_GlobalInvocationID.y) + " << writeNdx*m_invocationGridSize << "), ";
1974 buf << ((m_formatInteger) ? ("int") : ("float")) << "(groupNdx));\n";
1976 buf << ((m_formatInteger) ? ("ivec4(int(groupNdx), 0, 0, 0)") : ("vec4(float(groupNdx), 0.0, 0.0, 0.0)")) << ");\n";
1980 DE_ASSERT(DE_FALSE);
1984 return new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(buf.str()));
1987 glu::ShaderProgram* InterCallTestCase::genReadInterleavedProgram (int seed0, int seed1)
1989 const bool useImageAtomics = m_useAtomic && m_storage == STORAGE_IMAGE;
1990 std::ostringstream buf;
1992 buf << "#version 310 es\n"
1993 << ((useImageAtomics) ? ("#extension GL_OES_shader_image_atomic : require\n") : (""))
1994 << "layout (local_size_x = 1, local_size_y = 1) in;\n";
1996 if (m_storage == STORAGE_BUFFER)
1997 buf << "layout(binding=1, std430) " << ((m_useAtomic) ? ("coherent ") : ("")) << "buffer Buffer\n"
1999 << " highp " << ((m_formatInteger) ? ("int") : ("float")) << " values[];\n"
2001 else if (m_storage == STORAGE_IMAGE)
2002 buf << "layout(" << ((m_formatInteger) ? ("r32i") : ("r32f")) << ", binding=1) " << ((m_useAtomic) ? ("coherent ") : ("readonly ")) << "uniform highp " << ((m_formatInteger) ? ("iimage2D") : ("image2D")) << " u_imageIn;\n";
2004 DE_ASSERT(DE_FALSE);
2006 buf << "layout(binding=0, std430) buffer ResultBuffer\n"
2008 << " highp int resultOk[];\n"
2011 << "void main (void)\n"
2013 << " uvec3 size = gl_NumWorkGroups * gl_WorkGroupSize;\n"
2014 << " int groupNdx = int(size.x * size.y * gl_GlobalInvocationID.z + size.x*gl_GlobalInvocationID.y + gl_GlobalInvocationID.x);\n"
2015 << " int interleavedGroupNdx = int((size.x >> 1U) * size.y * gl_GlobalInvocationID.z + (size.x >> 1U) * gl_GlobalInvocationID.y + (gl_GlobalInvocationID.x >> 1U));\n"
2016 << " " << ((m_formatInteger) ? ("int") : ("float")) << " zero = " << ((m_formatInteger) ? ("0") : ("0.0")) << ";\n"
2017 << " bool allOk = true;\n"
2022 if (m_storage == STORAGE_BUFFER)
2024 buf << " if (groupNdx % 2 == 0)\n"
2026 for (int readNdx = 0; readNdx < m_perInvocationSize; ++readNdx)
2027 buf << " allOk = allOk && ("
2028 << ((m_useAtomic) ? ("atomicExchange(") : ("")) << "sb_in.values[((interleavedGroupNdx + " << seed0 + readNdx*m_invocationGridSize*m_invocationGridSize / 2 << ") % " << m_invocationGridSize*m_invocationGridSize*m_perInvocationSize / 2 << ") * 2 + 0]"
2029 << ((m_useAtomic) ? (", zero)") : ("")) << " == " << ((m_formatInteger) ? ("int") : ("float")) << "(interleavedGroupNdx));\n";
2033 for (int readNdx = 0; readNdx < m_perInvocationSize; ++readNdx)
2034 buf << " allOk = allOk && ("
2035 << ((m_useAtomic) ? ("atomicExchange(") : ("")) << "sb_in.values[((interleavedGroupNdx + " << seed1 + readNdx*m_invocationGridSize*m_invocationGridSize / 2 << ") % " << m_invocationGridSize*m_invocationGridSize*m_perInvocationSize / 2 << ") * 2 + 1]"
2036 << ((m_useAtomic) ? (", zero)") : ("")) << " == " << ((m_formatInteger) ? ("int") : ("float")) << "(interleavedGroupNdx));\n";
2039 else if (m_storage == STORAGE_IMAGE)
2041 buf << " if (groupNdx % 2 == 0)\n"
2043 for (int readNdx = 0; readNdx < m_perInvocationSize; ++readNdx)
2044 buf << " allOk = allOk && ("
2045 << ((m_useAtomic) ? ("imageAtomicExchange") : ("imageLoad"))
2046 << "(u_imageIn, ivec2(((int(gl_GlobalInvocationID.x >> 1U) + " << (seed0 + readNdx*100) << ") % " << m_invocationGridSize / 2 << ") * 2 + 0, int(gl_GlobalInvocationID.y) + " << readNdx*m_invocationGridSize << ")"
2047 << ((m_useAtomic) ? (", zero)") : (").x")) << " == " << ((m_formatInteger) ? ("int") : ("float")) << "(interleavedGroupNdx));\n";
2051 for (int readNdx = 0; readNdx < m_perInvocationSize; ++readNdx)
2052 buf << " allOk = allOk && ("
2053 << ((m_useAtomic) ? ("imageAtomicExchange") : ("imageLoad"))
2054 << "(u_imageIn, ivec2(((int(gl_GlobalInvocationID.x >> 1U) + " << (seed1 + readNdx*100) << ") % " << m_invocationGridSize / 2 << ") * 2 + 1, int(gl_GlobalInvocationID.y) + " << readNdx*m_invocationGridSize << ")"
2055 << ((m_useAtomic) ? (", zero)") : (").x")) << " == " << ((m_formatInteger) ? ("int") : ("float")) << "(interleavedGroupNdx));\n";
2059 DE_ASSERT(DE_FALSE);
2061 buf << " sb_result.resultOk[groupNdx] = (allOk) ? (1) : (0);\n"
2064 return new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(buf.str()));
2067 glu::ShaderProgram* InterCallTestCase::genReadZeroProgram (void)
2069 const bool useImageAtomics = m_useAtomic && m_storage == STORAGE_IMAGE;
2070 std::ostringstream buf;
2072 buf << "#version 310 es\n"
2073 << ((useImageAtomics) ? ("#extension GL_OES_shader_image_atomic : require\n") : (""))
2074 << "layout (local_size_x = 1, local_size_y = 1) in;\n";
2076 if (m_storage == STORAGE_BUFFER)
2077 buf << "layout(binding=1, std430) " << ((m_useAtomic) ? ("coherent ") : ("")) << "buffer Buffer\n"
2079 << " highp " << ((m_formatInteger) ? ("int") : ("float")) << " values[];\n"
2081 else if (m_storage == STORAGE_IMAGE)
2082 buf << "layout(" << ((m_formatInteger) ? ("r32i") : ("r32f")) << ", binding=1) " << ((m_useAtomic) ? ("coherent ") : ("readonly ")) << "uniform highp " << ((m_formatInteger) ? ("iimage2D") : ("image2D")) << " u_imageIn;\n";
2084 DE_ASSERT(DE_FALSE);
2086 buf << "layout(binding=0, std430) buffer ResultBuffer\n"
2088 << " highp int resultOk[];\n"
2091 << "void main (void)\n"
2093 << " uvec3 size = gl_NumWorkGroups * gl_WorkGroupSize;\n"
2094 << " int groupNdx = int(size.x * size.y * gl_GlobalInvocationID.z + size.x*gl_GlobalInvocationID.y + gl_GlobalInvocationID.x);\n"
2095 << " " << ((m_formatInteger) ? ("int") : ("float")) << " anything = " << ((m_formatInteger) ? ("5") : ("5.0")) << ";\n"
2096 << " bool allOk = true;\n"
2101 if (m_storage == STORAGE_BUFFER)
2103 for (int readNdx = 0; readNdx < m_perInvocationSize; ++readNdx)
2104 buf << " allOk = allOk && ("
2105 << ((m_useAtomic) ? ("atomicExchange(") : ("")) << "sb_in.values[groupNdx * " << m_perInvocationSize << " + " << readNdx << "]"
2106 << ((m_useAtomic) ? (", anything)") : ("")) << " == " << ((m_formatInteger) ? ("0") : ("0.0")) << ");\n";
2108 else if (m_storage == STORAGE_IMAGE)
2110 for (int readNdx = 0; readNdx < m_perInvocationSize; ++readNdx)
2111 buf << " allOk = allOk && ("
2112 << ((m_useAtomic) ? ("imageAtomicExchange") : ("imageLoad")) << "(u_imageIn, ivec2(gl_GlobalInvocationID.x, gl_GlobalInvocationID.y + " << (readNdx*m_invocationGridSize) << "u)"
2113 << ((m_useAtomic) ? (", anything)") : (").x")) << " == " << ((m_formatInteger) ? ("0") : ("0.0")) << ");\n";
2116 DE_ASSERT(DE_FALSE);
2118 buf << " sb_result.resultOk[groupNdx] = (allOk) ? (1) : (0);\n"
2121 return new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(buf.str()));
2124 class SSBOConcurrentAtomicCase : public TestCase
2128 SSBOConcurrentAtomicCase (Context& context, const char* name, const char* description, int numCalls, int workSize);
2129 ~SSBOConcurrentAtomicCase (void);
2133 IterateResult iterate (void);
2136 std::string genComputeSource (void) const;
2138 const int m_numCalls;
2139 const int m_workSize;
2140 glu::ShaderProgram* m_program;
2141 deUint32 m_bufferID;
2142 std::vector<deUint32> m_intermediateResultBuffers;
2145 SSBOConcurrentAtomicCase::SSBOConcurrentAtomicCase (Context& context, const char* name, const char* description, int numCalls, int workSize)
2146 : TestCase (context, name, description)
2147 , m_numCalls (numCalls)
2148 , m_workSize (workSize)
2149 , m_program (DE_NULL)
2150 , m_bufferID (DE_NULL)
2154 SSBOConcurrentAtomicCase::~SSBOConcurrentAtomicCase (void)
2159 void SSBOConcurrentAtomicCase::init (void)
2161 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
2162 std::vector<deUint32> zeroData (m_workSize, 0);
2166 gl.genBuffers(1, &m_bufferID);
2167 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_bufferID);
2168 gl.bufferData(GL_SHADER_STORAGE_BUFFER, sizeof(deUint32) * m_workSize, &zeroData[0], GL_DYNAMIC_COPY);
2170 for (int ndx = 0; ndx < m_numCalls; ++ndx)
2172 deUint32 buffer = 0;
2174 gl.genBuffers(1, &buffer);
2175 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
2176 gl.bufferData(GL_SHADER_STORAGE_BUFFER, sizeof(deUint32) * m_workSize, &zeroData[0], GL_DYNAMIC_COPY);
2178 m_intermediateResultBuffers.push_back(buffer);
2179 GLU_EXPECT_NO_ERROR(gl.getError(), "gen buffers");
2184 m_program = new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(genComputeSource()));
2185 m_testCtx.getLog() << *m_program;
2186 if (!m_program->isOk())
2187 throw tcu::TestError("could not build program");
2190 void SSBOConcurrentAtomicCase::deinit (void)
2194 m_context.getRenderContext().getFunctions().deleteBuffers(1, &m_bufferID);
2198 for (int ndx = 0; ndx < (int)m_intermediateResultBuffers.size(); ++ndx)
2199 m_context.getRenderContext().getFunctions().deleteBuffers(1, &m_intermediateResultBuffers[ndx]);
2200 m_intermediateResultBuffers.clear();
2203 m_program = DE_NULL;
2206 TestCase::IterateResult SSBOConcurrentAtomicCase::iterate (void)
2208 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
2209 const deUint32 sumValue = (deUint32)(m_numCalls * (m_numCalls + 1) / 2);
2210 std::vector<int> deltas;
2212 // generate unique deltas
2213 generateShuffledRamp(m_numCalls, deltas);
2215 // invoke program N times, each with a different delta
2217 const int deltaLocation = gl.getUniformLocation(m_program->getProgram(), "u_atomicDelta");
2220 << tcu::TestLog::Message
2221 << "Running shader " << m_numCalls << " times.\n"
2222 << "Num groups = (" << m_workSize << ", 1, 1)\n"
2223 << "Setting u_atomicDelta to a unique value for each call.\n"
2224 << tcu::TestLog::EndMessage;
2226 if (deltaLocation == -1)
2227 throw tcu::TestError("u_atomicDelta location was -1");
2229 gl.useProgram(m_program->getProgram());
2230 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_bufferID);
2232 for (int callNdx = 0; callNdx < m_numCalls; ++callNdx)
2235 << tcu::TestLog::Message
2236 << "Call " << callNdx << ": u_atomicDelta = " << deltas[callNdx]
2237 << tcu::TestLog::EndMessage;
2239 gl.uniform1ui(deltaLocation, deltas[callNdx]);
2240 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_intermediateResultBuffers[callNdx]);
2241 gl.dispatchCompute(m_workSize, 1, 1);
2244 GLU_EXPECT_NO_ERROR(gl.getError(), "post dispatch");
2249 std::vector<deUint32> result;
2251 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying work buffer, it should be filled with value " << sumValue << tcu::TestLog::EndMessage;
2253 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_bufferID);
2254 readBuffer(gl, GL_SHADER_STORAGE_BUFFER, m_workSize, result);
2256 for (int ndx = 0; ndx < m_workSize; ++ndx)
2258 if (result[ndx] != sumValue)
2261 << tcu::TestLog::Message
2262 << "Work buffer error, at index " << ndx << " expected value " << (sumValue) << ", got " << result[ndx] << "\n"
2263 << "Work buffer contains invalid values."
2264 << tcu::TestLog::EndMessage;
2266 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Buffer contents invalid");
2271 m_testCtx.getLog() << tcu::TestLog::Message << "Work buffer contents are valid." << tcu::TestLog::EndMessage;
2276 std::vector<std::vector<deUint32> > intermediateResults (m_numCalls);
2277 std::vector<deUint32> valueChain (m_numCalls);
2279 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying intermediate results. " << tcu::TestLog::EndMessage;
2283 for (int callNdx = 0; callNdx < m_numCalls; ++callNdx)
2285 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_intermediateResultBuffers[callNdx]);
2286 readBuffer(gl, GL_SHADER_STORAGE_BUFFER, m_workSize, intermediateResults[callNdx]);
2291 for (int valueNdx = 0; valueNdx < m_workSize; ++valueNdx)
2293 int invalidOperationNdx;
2294 deUint32 errorDelta;
2295 deUint32 errorExpected;
2297 // collect result chain for each element
2298 for (int callNdx = 0; callNdx < m_numCalls; ++callNdx)
2299 valueChain[callNdx] = intermediateResults[callNdx][valueNdx];
2301 // check there exists a path from 0 to sumValue using each addition once
2302 // decompose cumulative results to addition operations (all additions positive => this works)
2304 std::sort(valueChain.begin(), valueChain.end());
2307 if (!validateSortedAtomicRampAdditionValueChain(valueChain, sumValue, invalidOperationNdx, errorDelta, errorExpected))
2310 << tcu::TestLog::Message
2311 << "Intermediate buffer error, at value index " << valueNdx << ", applied operation index " << invalidOperationNdx << ", value was increased by " << errorDelta << ", but expected " << errorExpected << ".\n"
2312 << "Intermediate buffer contains invalid values. Values at index " << valueNdx << "\n"
2313 << tcu::TestLog::EndMessage;
2315 for (int logCallNdx = 0; logCallNdx < m_numCalls; ++logCallNdx)
2316 m_testCtx.getLog() << tcu::TestLog::Message << "Value[" << logCallNdx << "] = " << intermediateResults[logCallNdx][valueNdx] << tcu::TestLog::EndMessage;
2317 m_testCtx.getLog() << tcu::TestLog::Message << "Result = " << sumValue << tcu::TestLog::EndMessage;
2319 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Buffer contents invalid");
2324 m_testCtx.getLog() << tcu::TestLog::Message << "Intermediate buffers are valid." << tcu::TestLog::EndMessage;
2327 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
2331 std::string SSBOConcurrentAtomicCase::genComputeSource (void) const
2333 std::ostringstream buf;
2335 buf << "#version 310 es\n"
2336 << "layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
2337 << "layout (binding = 1, std430) writeonly buffer IntermediateResults\n"
2339 << " highp uint values[" << m_workSize << "];\n"
2342 << "layout (binding = 2, std430) volatile buffer WorkBuffer\n"
2344 << " highp uint values[" << m_workSize << "];\n"
2346 << "uniform highp uint u_atomicDelta;\n"
2350 << " highp uint invocationIndex = gl_GlobalInvocationID.x;\n"
2351 << " sb_ires.values[invocationIndex] = atomicAdd(sb_work.values[invocationIndex], u_atomicDelta);\n"
2357 class ConcurrentAtomicCounterCase : public TestCase
2361 ConcurrentAtomicCounterCase (Context& context, const char* name, const char* description, int numCalls, int workSize);
2362 ~ConcurrentAtomicCounterCase (void);
2366 IterateResult iterate (void);
2369 std::string genComputeSource (bool evenOdd) const;
2371 const int m_numCalls;
2372 const int m_workSize;
2373 glu::ShaderProgram* m_evenProgram;
2374 glu::ShaderProgram* m_oddProgram;
2375 deUint32 m_counterBuffer;
2376 deUint32 m_intermediateResultBuffer;
2379 ConcurrentAtomicCounterCase::ConcurrentAtomicCounterCase (Context& context, const char* name, const char* description, int numCalls, int workSize)
2380 : TestCase (context, name, description)
2381 , m_numCalls (numCalls)
2382 , m_workSize (workSize)
2383 , m_evenProgram (DE_NULL)
2384 , m_oddProgram (DE_NULL)
2385 , m_counterBuffer (DE_NULL)
2386 , m_intermediateResultBuffer(DE_NULL)
2390 ConcurrentAtomicCounterCase::~ConcurrentAtomicCounterCase (void)
2395 void ConcurrentAtomicCounterCase::init (void)
2397 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
2398 const std::vector<deUint32> zeroData (m_numCalls * m_workSize, 0);
2402 gl.genBuffers(1, &m_counterBuffer);
2403 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_counterBuffer);
2404 gl.bufferData(GL_SHADER_STORAGE_BUFFER, sizeof(deUint32), &zeroData[0], GL_DYNAMIC_COPY);
2406 gl.genBuffers(1, &m_intermediateResultBuffer);
2407 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_intermediateResultBuffer);
2408 gl.bufferData(GL_SHADER_STORAGE_BUFFER, sizeof(deUint32) * m_numCalls * m_workSize, &zeroData[0], GL_DYNAMIC_COPY);
2410 GLU_EXPECT_NO_ERROR(gl.getError(), "gen buffers");
2415 const tcu::ScopedLogSection section(m_testCtx.getLog(), "EvenProgram", "Even program");
2417 m_evenProgram = new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(genComputeSource(true)));
2418 m_testCtx.getLog() << *m_evenProgram;
2419 if (!m_evenProgram->isOk())
2420 throw tcu::TestError("could not build program");
2423 const tcu::ScopedLogSection section(m_testCtx.getLog(), "OddProgram", "Odd program");
2425 m_oddProgram = new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(genComputeSource(false)));
2426 m_testCtx.getLog() << *m_oddProgram;
2427 if (!m_oddProgram->isOk())
2428 throw tcu::TestError("could not build program");
2432 void ConcurrentAtomicCounterCase::deinit (void)
2434 if (m_counterBuffer)
2436 m_context.getRenderContext().getFunctions().deleteBuffers(1, &m_counterBuffer);
2437 m_counterBuffer = 0;
2439 if (m_intermediateResultBuffer)
2441 m_context.getRenderContext().getFunctions().deleteBuffers(1, &m_intermediateResultBuffer);
2442 m_intermediateResultBuffer = 0;
2445 delete m_evenProgram;
2446 m_evenProgram = DE_NULL;
2448 delete m_oddProgram;
2449 m_oddProgram = DE_NULL;
2452 TestCase::IterateResult ConcurrentAtomicCounterCase::iterate (void)
2454 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
2456 // invoke program N times, each with a different delta
2458 const int evenCallNdxLocation = gl.getUniformLocation(m_evenProgram->getProgram(), "u_callNdx");
2459 const int oddCallNdxLocation = gl.getUniformLocation(m_oddProgram->getProgram(), "u_callNdx");
2462 << tcu::TestLog::Message
2463 << "Running shader pair (even & odd) " << m_numCalls << " times.\n"
2464 << "Num groups = (" << m_workSize << ", 1, 1)\n"
2465 << tcu::TestLog::EndMessage;
2467 if (evenCallNdxLocation == -1)
2468 throw tcu::TestError("u_callNdx location was -1");
2469 if (oddCallNdxLocation == -1)
2470 throw tcu::TestError("u_callNdx location was -1");
2472 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_intermediateResultBuffer);
2473 gl.bindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 2, m_counterBuffer);
2475 for (int callNdx = 0; callNdx < m_numCalls; ++callNdx)
2477 gl.useProgram(m_evenProgram->getProgram());
2478 gl.uniform1ui(evenCallNdxLocation, (deUint32)callNdx);
2479 gl.dispatchCompute(m_workSize, 1, 1);
2481 gl.useProgram(m_oddProgram->getProgram());
2482 gl.uniform1ui(oddCallNdxLocation, (deUint32)callNdx);
2483 gl.dispatchCompute(m_workSize, 1, 1);
2486 GLU_EXPECT_NO_ERROR(gl.getError(), "post dispatch");
2493 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying work buffer, it should be " << m_numCalls*m_workSize << tcu::TestLog::EndMessage;
2495 gl.bindBuffer(GL_ATOMIC_COUNTER_BUFFER, m_counterBuffer);
2496 result = readBufferUint32(gl, GL_ATOMIC_COUNTER_BUFFER);
2498 if ((int)result != m_numCalls*m_workSize)
2501 << tcu::TestLog::Message
2502 << "Counter buffer error, expected value " << (m_numCalls*m_workSize) << ", got " << result << "\n"
2503 << tcu::TestLog::EndMessage;
2505 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Buffer contents invalid");
2509 m_testCtx.getLog() << tcu::TestLog::Message << "Counter buffer is valid." << tcu::TestLog::EndMessage;
2514 std::vector<deUint32> intermediateResults;
2516 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying intermediate results. " << tcu::TestLog::EndMessage;
2520 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_intermediateResultBuffer);
2521 readBuffer(gl, GL_SHADER_STORAGE_BUFFER, m_numCalls * m_workSize, intermediateResults);
2525 std::sort(intermediateResults.begin(), intermediateResults.end());
2527 for (int valueNdx = 0; valueNdx < m_workSize * m_numCalls; ++valueNdx)
2529 if ((int)intermediateResults[valueNdx] != valueNdx)
2532 << tcu::TestLog::Message
2533 << "Intermediate buffer error, at value index " << valueNdx << ", expected " << valueNdx << ", got " << intermediateResults[valueNdx] << ".\n"
2534 << "Intermediate buffer contains invalid values. Intermediate results:\n"
2535 << tcu::TestLog::EndMessage;
2537 for (int logCallNdx = 0; logCallNdx < m_workSize * m_numCalls; ++logCallNdx)
2538 m_testCtx.getLog() << tcu::TestLog::Message << "Value[" << logCallNdx << "] = " << intermediateResults[logCallNdx] << tcu::TestLog::EndMessage;
2540 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Buffer contents invalid");
2545 m_testCtx.getLog() << tcu::TestLog::Message << "Intermediate buffers are valid." << tcu::TestLog::EndMessage;
2548 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
2552 std::string ConcurrentAtomicCounterCase::genComputeSource (bool evenOdd) const
2554 std::ostringstream buf;
2556 buf << "#version 310 es\n"
2557 << "layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
2558 << "layout (binding = 1, std430) writeonly buffer IntermediateResults\n"
2560 << " highp uint values[" << m_workSize * m_numCalls << "];\n"
2563 << "layout (binding = 2, offset = 0) uniform atomic_uint u_counter;\n"
2564 << "uniform highp uint u_callNdx;\n"
2568 << " highp uint dataNdx = u_callNdx * " << m_workSize << "u + gl_GlobalInvocationID.x;\n"
2569 << " if ((dataNdx % 2u) == " << ((evenOdd) ? (0) : (1)) << "u)\n"
2570 << " sb_ires.values[dataNdx] = atomicCounterIncrement(u_counter);\n"
2576 class ConcurrentImageAtomicCase : public TestCase
2580 ConcurrentImageAtomicCase (Context& context, const char* name, const char* description, int numCalls, int workSize);
2581 ~ConcurrentImageAtomicCase (void);
2585 IterateResult iterate (void);
2588 void readWorkImage (std::vector<deUint32>& result);
2590 std::string genComputeSource (void) const;
2591 std::string genImageReadSource (void) const;
2592 std::string genImageClearSource (void) const;
2594 const int m_numCalls;
2595 const int m_workSize;
2596 glu::ShaderProgram* m_program;
2597 glu::ShaderProgram* m_imageReadProgram;
2598 glu::ShaderProgram* m_imageClearProgram;
2600 std::vector<deUint32> m_intermediateResultBuffers;
2603 ConcurrentImageAtomicCase::ConcurrentImageAtomicCase (Context& context, const char* name, const char* description, int numCalls, int workSize)
2604 : TestCase (context, name, description)
2605 , m_numCalls (numCalls)
2606 , m_workSize (workSize)
2607 , m_program (DE_NULL)
2608 , m_imageReadProgram (DE_NULL)
2609 , m_imageClearProgram (DE_NULL)
2610 , m_imageID (DE_NULL)
2614 ConcurrentImageAtomicCase::~ConcurrentImageAtomicCase (void)
2619 void ConcurrentImageAtomicCase::init (void)
2621 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
2622 std::vector<deUint32> zeroData (m_workSize * m_workSize, 0);
2624 if (!m_context.getContextInfo().isExtensionSupported("GL_OES_shader_image_atomic"))
2625 throw tcu::NotSupportedError("Test requires GL_OES_shader_image_atomic");
2629 gl.genTextures(1, &m_imageID);
2630 gl.bindTexture(GL_TEXTURE_2D, m_imageID);
2631 gl.texStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, m_workSize, m_workSize);
2632 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2633 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2634 GLU_EXPECT_NO_ERROR(gl.getError(), "gen tex");
2638 for (int ndx = 0; ndx < m_numCalls; ++ndx)
2640 deUint32 buffer = 0;
2642 gl.genBuffers(1, &buffer);
2643 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
2644 gl.bufferData(GL_SHADER_STORAGE_BUFFER, sizeof(deUint32) * m_workSize * m_workSize, &zeroData[0], GL_DYNAMIC_COPY);
2646 m_intermediateResultBuffers.push_back(buffer);
2647 GLU_EXPECT_NO_ERROR(gl.getError(), "gen buffers");
2652 m_program = new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(genComputeSource()));
2653 m_testCtx.getLog() << *m_program;
2654 if (!m_program->isOk())
2655 throw tcu::TestError("could not build program");
2657 m_imageReadProgram = new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(genImageReadSource()));
2658 if (!m_imageReadProgram->isOk())
2660 const tcu::ScopedLogSection section(m_testCtx.getLog(), "ImageReadProgram", "Image read program");
2662 m_testCtx.getLog() << *m_imageReadProgram;
2663 throw tcu::TestError("could not build program");
2666 m_imageClearProgram = new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(genImageClearSource()));
2667 if (!m_imageClearProgram->isOk())
2669 const tcu::ScopedLogSection section(m_testCtx.getLog(), "ImageClearProgram", "Image read program");
2671 m_testCtx.getLog() << *m_imageClearProgram;
2672 throw tcu::TestError("could not build program");
2676 void ConcurrentImageAtomicCase::deinit (void)
2680 m_context.getRenderContext().getFunctions().deleteTextures(1, &m_imageID);
2684 for (int ndx = 0; ndx < (int)m_intermediateResultBuffers.size(); ++ndx)
2685 m_context.getRenderContext().getFunctions().deleteBuffers(1, &m_intermediateResultBuffers[ndx]);
2686 m_intermediateResultBuffers.clear();
2689 m_program = DE_NULL;
2691 delete m_imageReadProgram;
2692 m_imageReadProgram = DE_NULL;
2694 delete m_imageClearProgram;
2695 m_imageClearProgram = DE_NULL;
2698 TestCase::IterateResult ConcurrentImageAtomicCase::iterate (void)
2700 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
2701 const deUint32 sumValue = (deUint32)(m_numCalls * (m_numCalls + 1) / 2);
2702 std::vector<int> deltas;
2704 // generate unique deltas
2705 generateShuffledRamp(m_numCalls, deltas);
2709 m_testCtx.getLog() << tcu::TestLog::Message << "Clearing image contents" << tcu::TestLog::EndMessage;
2711 gl.useProgram(m_imageClearProgram->getProgram());
2712 gl.bindImageTexture(2, m_imageID, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32UI);
2713 gl.dispatchCompute(m_workSize, m_workSize, 1);
2714 gl.memoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
2716 GLU_EXPECT_NO_ERROR(gl.getError(), "clear");
2719 // invoke program N times, each with a different delta
2721 const int deltaLocation = gl.getUniformLocation(m_program->getProgram(), "u_atomicDelta");
2724 << tcu::TestLog::Message
2725 << "Running shader " << m_numCalls << " times.\n"
2726 << "Num groups = (" << m_workSize << ", " << m_workSize << ", 1)\n"
2727 << "Setting u_atomicDelta to a unique value for each call.\n"
2728 << tcu::TestLog::EndMessage;
2730 if (deltaLocation == -1)
2731 throw tcu::TestError("u_atomicDelta location was -1");
2733 gl.useProgram(m_program->getProgram());
2734 gl.bindImageTexture(2, m_imageID, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32UI);
2736 for (int callNdx = 0; callNdx < m_numCalls; ++callNdx)
2739 << tcu::TestLog::Message
2740 << "Call " << callNdx << ": u_atomicDelta = " << deltas[callNdx]
2741 << tcu::TestLog::EndMessage;
2743 gl.uniform1ui(deltaLocation, deltas[callNdx]);
2744 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_intermediateResultBuffers[callNdx]);
2745 gl.dispatchCompute(m_workSize, m_workSize, 1);
2748 GLU_EXPECT_NO_ERROR(gl.getError(), "post dispatch");
2753 std::vector<deUint32> result;
2755 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying work image, it should be filled with value " << sumValue << tcu::TestLog::EndMessage;
2757 readWorkImage(result);
2759 for (int ndx = 0; ndx < m_workSize * m_workSize; ++ndx)
2761 if (result[ndx] != sumValue)
2764 << tcu::TestLog::Message
2765 << "Work image error, at index (" << ndx % m_workSize << ", " << ndx / m_workSize << ") expected value " << (sumValue) << ", got " << result[ndx] << "\n"
2766 << "Work image contains invalid values."
2767 << tcu::TestLog::EndMessage;
2769 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Image contents invalid");
2774 m_testCtx.getLog() << tcu::TestLog::Message << "Work image contents are valid." << tcu::TestLog::EndMessage;
2779 std::vector<std::vector<deUint32> > intermediateResults (m_numCalls);
2780 std::vector<deUint32> valueChain (m_numCalls);
2781 std::vector<deUint32> chainDelta (m_numCalls);
2783 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying intermediate results. " << tcu::TestLog::EndMessage;
2787 for (int callNdx = 0; callNdx < m_numCalls; ++callNdx)
2789 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_intermediateResultBuffers[callNdx]);
2790 readBuffer(gl, GL_SHADER_STORAGE_BUFFER, m_workSize * m_workSize, intermediateResults[callNdx]);
2795 for (int valueNdx = 0; valueNdx < m_workSize; ++valueNdx)
2797 int invalidOperationNdx;
2798 deUint32 errorDelta;
2799 deUint32 errorExpected;
2801 // collect result chain for each element
2802 for (int callNdx = 0; callNdx < m_numCalls; ++callNdx)
2803 valueChain[callNdx] = intermediateResults[callNdx][valueNdx];
2805 // check there exists a path from 0 to sumValue using each addition once
2806 // decompose cumulative results to addition operations (all additions positive => this works)
2808 std::sort(valueChain.begin(), valueChain.end());
2810 for (int callNdx = 0; callNdx < m_numCalls; ++callNdx)
2811 chainDelta[callNdx] = ((callNdx + 1 == m_numCalls) ? (sumValue) : (valueChain[callNdx+1])) - valueChain[callNdx];
2813 // chainDelta contains now the actual additions applied to the value
2814 std::sort(chainDelta.begin(), chainDelta.end());
2817 if (!validateSortedAtomicRampAdditionValueChain(valueChain, sumValue, invalidOperationNdx, errorDelta, errorExpected))
2820 << tcu::TestLog::Message
2821 << "Intermediate buffer error, at index (" << valueNdx % m_workSize << ", " << valueNdx / m_workSize << "), applied operation index "
2822 << invalidOperationNdx << ", value was increased by " << errorDelta << ", but expected " << errorExpected << ".\n"
2823 << "Intermediate buffer contains invalid values. Values at index (" << valueNdx % m_workSize << ", " << valueNdx / m_workSize << ")\n"
2824 << tcu::TestLog::EndMessage;
2826 for (int logCallNdx = 0; logCallNdx < m_numCalls; ++logCallNdx)
2827 m_testCtx.getLog() << tcu::TestLog::Message << "Value[" << logCallNdx << "] = " << intermediateResults[logCallNdx][valueNdx] << tcu::TestLog::EndMessage;
2828 m_testCtx.getLog() << tcu::TestLog::Message << "Result = " << sumValue << tcu::TestLog::EndMessage;
2830 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Buffer contents invalid");
2835 m_testCtx.getLog() << tcu::TestLog::Message << "Intermediate buffers are valid." << tcu::TestLog::EndMessage;
2838 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
2842 void ConcurrentImageAtomicCase::readWorkImage (std::vector<deUint32>& result)
2844 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
2845 glu::Buffer resultBuffer (m_context.getRenderContext());
2847 // Read image to an ssbo
2850 const std::vector<deUint32> zeroData(m_workSize*m_workSize, 0);
2852 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, *resultBuffer);
2853 gl.bufferData(GL_SHADER_STORAGE_BUFFER, (int)(sizeof(deUint32) * m_workSize * m_workSize), &zeroData[0], GL_DYNAMIC_COPY);
2855 gl.memoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
2856 gl.useProgram(m_imageReadProgram->getProgram());
2858 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, *resultBuffer);
2859 gl.bindImageTexture(2, m_imageID, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
2860 gl.dispatchCompute(m_workSize, m_workSize, 1);
2862 GLU_EXPECT_NO_ERROR(gl.getError(), "read");
2867 const void* ptr = gl.mapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, (int)(sizeof(deUint32) * m_workSize * m_workSize), GL_MAP_READ_BIT);
2868 GLU_EXPECT_NO_ERROR(gl.getError(), "map");
2871 throw tcu::TestError("mapBufferRange returned NULL");
2873 result.resize(m_workSize * m_workSize);
2874 memcpy(&result[0], ptr, sizeof(deUint32) * m_workSize * m_workSize);
2876 if (gl.unmapBuffer(GL_SHADER_STORAGE_BUFFER) == GL_FALSE)
2877 throw tcu::TestError("unmapBuffer returned false");
2881 std::string ConcurrentImageAtomicCase::genComputeSource (void) const
2883 std::ostringstream buf;
2885 buf << "#version 310 es\n"
2886 << "#extension GL_OES_shader_image_atomic : require\n"
2888 << "layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
2889 << "layout (binding = 1, std430) writeonly buffer IntermediateResults\n"
2891 << " highp uint values[" << m_workSize * m_workSize << "];\n"
2894 << "layout (binding = 2, r32ui) volatile uniform highp uimage2D u_workImage;\n"
2895 << "uniform highp uint u_atomicDelta;\n"
2899 << " highp uint invocationIndex = gl_GlobalInvocationID.x + gl_GlobalInvocationID.y * uint(" << m_workSize <<");\n"
2900 << " sb_ires.values[invocationIndex] = imageAtomicAdd(u_workImage, ivec2(gl_GlobalInvocationID.xy), u_atomicDelta);\n"
2906 std::string ConcurrentImageAtomicCase::genImageReadSource (void) const
2908 std::ostringstream buf;
2910 buf << "#version 310 es\n"
2912 << "layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
2913 << "layout (binding = 1, std430) writeonly buffer ImageValues\n"
2915 << " highp uint values[" << m_workSize * m_workSize << "];\n"
2918 << "layout (binding = 2, r32ui) readonly uniform highp uimage2D u_workImage;\n"
2922 << " highp uint invocationIndex = gl_GlobalInvocationID.x + gl_GlobalInvocationID.y * uint(" << m_workSize <<");\n"
2923 << " sb_res.values[invocationIndex] = imageLoad(u_workImage, ivec2(gl_GlobalInvocationID.xy)).x;\n"
2929 std::string ConcurrentImageAtomicCase::genImageClearSource (void) const
2931 std::ostringstream buf;
2933 buf << "#version 310 es\n"
2935 << "layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
2936 << "layout (binding = 2, r32ui) writeonly uniform highp uimage2D u_workImage;\n"
2940 << " imageStore(u_workImage, ivec2(gl_GlobalInvocationID.xy), uvec4(0, 0, 0, 0));\n"
2946 class ConcurrentSSBOAtomicCounterMixedCase : public TestCase
2949 ConcurrentSSBOAtomicCounterMixedCase (Context& context, const char* name, const char* description, int numCalls, int workSize);
2950 ~ConcurrentSSBOAtomicCounterMixedCase (void);
2954 IterateResult iterate (void);
2957 std::string genSSBOComputeSource (void) const;
2958 std::string genAtomicCounterComputeSource (void) const;
2960 const int m_numCalls;
2961 const int m_workSize;
2962 deUint32 m_bufferID;
2963 glu::ShaderProgram* m_ssboAtomicProgram;
2964 glu::ShaderProgram* m_atomicCounterProgram;
2967 ConcurrentSSBOAtomicCounterMixedCase::ConcurrentSSBOAtomicCounterMixedCase (Context& context, const char* name, const char* description, int numCalls, int workSize)
2968 : TestCase (context, name, description)
2969 , m_numCalls (numCalls)
2970 , m_workSize (workSize)
2971 , m_bufferID (DE_NULL)
2972 , m_ssboAtomicProgram (DE_NULL)
2973 , m_atomicCounterProgram (DE_NULL)
2975 // SSBO atomic XORs cancel out
2976 DE_ASSERT((workSize * numCalls) % (16 * 2) == 0);
2979 ConcurrentSSBOAtomicCounterMixedCase::~ConcurrentSSBOAtomicCounterMixedCase (void)
2984 void ConcurrentSSBOAtomicCounterMixedCase::init (void)
2986 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
2987 const deUint32 zeroBuf[2] = { 0, 0 };
2991 gl.genBuffers(1, &m_bufferID);
2992 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, m_bufferID);
2993 gl.bufferData(GL_SHADER_STORAGE_BUFFER, (int)(sizeof(deUint32) * 2), zeroBuf, GL_DYNAMIC_COPY);
2995 GLU_EXPECT_NO_ERROR(gl.getError(), "gen buffers");
3000 const tcu::ScopedLogSection section(m_testCtx.getLog(), "SSBOProgram", "SSBO atomic program");
3002 m_ssboAtomicProgram = new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(genSSBOComputeSource()));
3003 m_testCtx.getLog() << *m_ssboAtomicProgram;
3004 if (!m_ssboAtomicProgram->isOk())
3005 throw tcu::TestError("could not build program");
3008 const tcu::ScopedLogSection section(m_testCtx.getLog(), "AtomicCounterProgram", "Atomic counter program");
3010 m_atomicCounterProgram = new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::ComputeSource(genAtomicCounterComputeSource()));
3011 m_testCtx.getLog() << *m_atomicCounterProgram;
3012 if (!m_atomicCounterProgram->isOk())
3013 throw tcu::TestError("could not build program");
3017 void ConcurrentSSBOAtomicCounterMixedCase::deinit (void)
3021 m_context.getRenderContext().getFunctions().deleteBuffers(1, &m_bufferID);
3025 delete m_ssboAtomicProgram;
3026 m_ssboAtomicProgram = DE_NULL;
3028 delete m_atomicCounterProgram;
3029 m_atomicCounterProgram = DE_NULL;
3032 TestCase::IterateResult ConcurrentSSBOAtomicCounterMixedCase::iterate (void)
3034 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
3036 m_testCtx.getLog() << tcu::TestLog::Message << "Testing atomic counters and SSBO atomic operations with both backed by the same buffer." << tcu::TestLog::EndMessage;
3038 // invoke programs N times
3041 << tcu::TestLog::Message
3042 << "Running SSBO atomic program and atomic counter program " << m_numCalls << " times. (interleaved)\n"
3043 << "Num groups = (" << m_workSize << ", 1, 1)\n"
3044 << tcu::TestLog::EndMessage;
3046 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_bufferID);
3047 gl.bindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 2, m_bufferID);
3049 for (int callNdx = 0; callNdx < m_numCalls; ++callNdx)
3051 gl.useProgram(m_atomicCounterProgram->getProgram());
3052 gl.dispatchCompute(m_workSize, 1, 1);
3054 gl.useProgram(m_ssboAtomicProgram->getProgram());
3055 gl.dispatchCompute(m_workSize, 1, 1);
3058 GLU_EXPECT_NO_ERROR(gl.getError(), "post dispatch");
3065 // XORs cancel out, only addition is left
3066 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying work buffer, it should be " << m_numCalls*m_workSize << tcu::TestLog::EndMessage;
3068 gl.bindBuffer(GL_ATOMIC_COUNTER_BUFFER, m_bufferID);
3069 result = readBufferUint32(gl, GL_ATOMIC_COUNTER_BUFFER);
3071 if ((int)result != m_numCalls*m_workSize)
3074 << tcu::TestLog::Message
3075 << "Buffer value error, expected value " << (m_numCalls*m_workSize) << ", got " << result << "\n"
3076 << tcu::TestLog::EndMessage;
3078 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Buffer contents invalid");
3082 m_testCtx.getLog() << tcu::TestLog::Message << "Buffer is valid." << tcu::TestLog::EndMessage;
3085 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
3089 std::string ConcurrentSSBOAtomicCounterMixedCase::genSSBOComputeSource (void) const
3091 std::ostringstream buf;
3093 buf << "#version 310 es\n"
3094 << "layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
3095 << "layout (binding = 1, std430) volatile buffer WorkBuffer\n"
3097 << " highp uint targetValue;\n"
3098 << " highp uint dummy;\n"
3103 << " // flip high bits\n"
3104 << " highp uint mask = uint(1) << (24u + (gl_GlobalInvocationID.x % 8u));\n"
3105 << " sb_work.dummy = atomicXor(sb_work.targetValue, mask);\n"
3111 std::string ConcurrentSSBOAtomicCounterMixedCase::genAtomicCounterComputeSource (void) const
3113 std::ostringstream buf;
3115 buf << "#version 310 es\n"
3116 << "layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
3118 << "layout (binding = 2, offset = 0) uniform atomic_uint u_counter;\n"
3122 << " atomicCounterIncrement(u_counter);\n"
3130 SynchronizationTests::SynchronizationTests (Context& context)
3131 : TestCaseGroup(context, "synchronization", "Synchronization tests")
3135 SynchronizationTests::~SynchronizationTests (void)
3139 void SynchronizationTests::init (void)
3141 tcu::TestCaseGroup* const inInvocationGroup = new tcu::TestCaseGroup(m_testCtx, "in_invocation", "Test intra-invocation synchronization");
3142 tcu::TestCaseGroup* const interInvocationGroup = new tcu::TestCaseGroup(m_testCtx, "inter_invocation", "Test inter-invocation synchronization");
3143 tcu::TestCaseGroup* const interCallGroup = new tcu::TestCaseGroup(m_testCtx, "inter_call", "Test inter-call synchronization");
3145 addChild(inInvocationGroup);
3146 addChild(interInvocationGroup);
3147 addChild(interCallGroup);
3149 // .in_invocation & .inter_invocation
3151 static const struct CaseConfig
3153 const char* namePrefix;
3154 const InterInvocationTestCase::StorageType storage;
3158 { "image", InterInvocationTestCase::STORAGE_IMAGE, 0 },
3159 { "image_atomic", InterInvocationTestCase::STORAGE_IMAGE, InterInvocationTestCase::FLAG_ATOMIC },
3160 { "ssbo", InterInvocationTestCase::STORAGE_BUFFER, 0 },
3161 { "ssbo_atomic", InterInvocationTestCase::STORAGE_BUFFER, InterInvocationTestCase::FLAG_ATOMIC },
3164 for (int groupNdx = 0; groupNdx < 2; ++groupNdx)
3166 tcu::TestCaseGroup* const targetGroup = (groupNdx == 0) ? (inInvocationGroup) : (interInvocationGroup);
3167 const int extraFlags = (groupNdx == 0) ? (0) : (InterInvocationTestCase::FLAG_IN_GROUP);
3169 for (int configNdx = 0; configNdx < DE_LENGTH_OF_ARRAY(configs); ++configNdx)
3171 const char* const target = (configs[configNdx].storage == InterInvocationTestCase::STORAGE_BUFFER) ? ("buffer") : ("image");
3173 targetGroup->addChild(new InvocationWriteReadCase(m_context,
3174 (std::string(configs[configNdx].namePrefix) + "_write_read").c_str(),
3175 (std::string("Write to ") + target + " and read it").c_str(),
3176 configs[configNdx].storage,
3177 configs[configNdx].flags | extraFlags));
3179 targetGroup->addChild(new InvocationReadWriteCase(m_context,
3180 (std::string(configs[configNdx].namePrefix) + "_read_write").c_str(),
3181 (std::string("Read form ") + target + " and then write to it").c_str(),
3182 configs[configNdx].storage,
3183 configs[configNdx].flags | extraFlags));
3185 targetGroup->addChild(new InvocationOverWriteCase(m_context,
3186 (std::string(configs[configNdx].namePrefix) + "_overwrite").c_str(),
3187 (std::string("Write to ") + target + " twice and read it").c_str(),
3188 configs[configNdx].storage,
3189 configs[configNdx].flags | extraFlags));
3191 targetGroup->addChild(new InvocationAliasWriteCase(m_context,
3192 (std::string(configs[configNdx].namePrefix) + "_alias_write").c_str(),
3193 (std::string("Write to aliasing ") + target + " and read it").c_str(),
3194 InvocationAliasWriteCase::TYPE_WRITE,
3195 configs[configNdx].storage,
3196 configs[configNdx].flags | extraFlags));
3198 targetGroup->addChild(new InvocationAliasWriteCase(m_context,
3199 (std::string(configs[configNdx].namePrefix) + "_alias_overwrite").c_str(),
3200 (std::string("Write to aliasing ") + target + "s and read it").c_str(),
3201 InvocationAliasWriteCase::TYPE_OVERWRITE,
3202 configs[configNdx].storage,
3203 configs[configNdx].flags | extraFlags));
3210 tcu::TestCaseGroup* const withBarrierGroup = new tcu::TestCaseGroup(m_testCtx, "with_memory_barrier", "Synchronize with memory barrier");
3211 tcu::TestCaseGroup* const withoutBarrierGroup = new tcu::TestCaseGroup(m_testCtx, "without_memory_barrier", "Synchronize without memory barrier");
3213 interCallGroup->addChild(withBarrierGroup);
3214 interCallGroup->addChild(withoutBarrierGroup);
3216 // .with_memory_barrier
3218 static const struct CaseConfig
3220 const char* namePrefix;
3221 const InterCallTestCase::StorageType storage;
3225 { "image", InterCallTestCase::STORAGE_IMAGE, 0 },
3226 { "image_atomic", InterCallTestCase::STORAGE_IMAGE, InterCallTestCase::FLAG_USE_ATOMIC | InterCallTestCase::FLAG_USE_INT },
3227 { "ssbo", InterCallTestCase::STORAGE_BUFFER, 0 },
3228 { "ssbo_atomic", InterCallTestCase::STORAGE_BUFFER, InterCallTestCase::FLAG_USE_ATOMIC | InterCallTestCase::FLAG_USE_INT },
3231 const int seed0 = 123;
3232 const int seed1 = 457;
3234 for (int configNdx = 0; configNdx < DE_LENGTH_OF_ARRAY(configs); ++configNdx)
3236 const char* const target = (configs[configNdx].storage == InterCallTestCase::STORAGE_BUFFER) ? ("buffer") : ("image");
3238 withBarrierGroup->addChild(new InterCallTestCase(m_context,
3239 (std::string(configs[configNdx].namePrefix) + "_write_read").c_str(),
3240 (std::string("Write to ") + target + " and read it").c_str(),
3241 configs[configNdx].storage,
3242 configs[configNdx].flags,
3243 InterCallOperations()
3244 << op::WriteData::Generate(1, seed0)
3246 << op::ReadData::Generate(1, seed0)));
3248 withBarrierGroup->addChild(new InterCallTestCase(m_context,
3249 (std::string(configs[configNdx].namePrefix) + "_read_write").c_str(),
3250 (std::string("Read from ") + target + " and then write to it").c_str(),
3251 configs[configNdx].storage,
3252 configs[configNdx].flags,
3253 InterCallOperations()
3254 << op::ReadZeroData::Generate(1)
3256 << op::WriteData::Generate(1, seed0)));
3258 withBarrierGroup->addChild(new InterCallTestCase(m_context,
3259 (std::string(configs[configNdx].namePrefix) + "_overwrite").c_str(),
3260 (std::string("Write to ") + target + " twice and read it").c_str(),
3261 configs[configNdx].storage,
3262 configs[configNdx].flags,
3263 InterCallOperations()
3264 << op::WriteData::Generate(1, seed0)
3266 << op::WriteData::Generate(1, seed1)
3268 << op::ReadData::Generate(1, seed1)));
3270 withBarrierGroup->addChild(new InterCallTestCase(m_context,
3271 (std::string(configs[configNdx].namePrefix) + "_multiple_write_read").c_str(),
3272 (std::string("Write to multiple ") + target + "s and read them").c_str(),
3273 configs[configNdx].storage,
3274 configs[configNdx].flags,
3275 InterCallOperations()
3276 << op::WriteData::Generate(1, seed0)
3277 << op::WriteData::Generate(2, seed1)
3279 << op::ReadMultipleData::Generate(1, seed0, 2, seed1)));
3281 withBarrierGroup->addChild(new InterCallTestCase(m_context,
3282 (std::string(configs[configNdx].namePrefix) + "_multiple_interleaved_write_read").c_str(),
3283 (std::string("Write to same ") + target + " in multiple calls and read it").c_str(),
3284 configs[configNdx].storage,
3285 configs[configNdx].flags,
3286 InterCallOperations()
3287 << op::WriteDataInterleaved::Generate(1, seed0, true)
3288 << op::WriteDataInterleaved::Generate(1, seed1, false)
3290 << op::ReadDataInterleaved::Generate(1, seed0, seed1)));
3292 withBarrierGroup->addChild(new InterCallTestCase(m_context,
3293 (std::string(configs[configNdx].namePrefix) + "_multiple_unrelated_write_read_ordered").c_str(),
3294 (std::string("Two unrelated ") + target + " write-reads").c_str(),
3295 configs[configNdx].storage,
3296 configs[configNdx].flags,
3297 InterCallOperations()
3298 << op::WriteData::Generate(1, seed0)
3299 << op::WriteData::Generate(2, seed1)
3301 << op::ReadData::Generate(1, seed0)
3302 << op::ReadData::Generate(2, seed1)));
3304 withBarrierGroup->addChild(new InterCallTestCase(m_context,
3305 (std::string(configs[configNdx].namePrefix) + "_multiple_unrelated_write_read_non_ordered").c_str(),
3306 (std::string("Two unrelated ") + target + " write-reads").c_str(),
3307 configs[configNdx].storage,
3308 configs[configNdx].flags,
3309 InterCallOperations()
3310 << op::WriteData::Generate(1, seed0)
3311 << op::WriteData::Generate(2, seed1)
3313 << op::ReadData::Generate(2, seed1)
3314 << op::ReadData::Generate(1, seed0)));
3317 // .without_memory_barrier
3319 struct InvocationConfig
3325 static const InvocationConfig ssboInvocations[] =
3331 static const InvocationConfig imageInvocations[] =
3337 static const InvocationConfig counterInvocations[] =
3343 static const int callCounts[] = { 2, 5, 100 };
3345 for (int invocationNdx = 0; invocationNdx < DE_LENGTH_OF_ARRAY(ssboInvocations); ++invocationNdx)
3346 for (int callCountNdx = 0; callCountNdx < DE_LENGTH_OF_ARRAY(callCounts); ++callCountNdx)
3347 withoutBarrierGroup->addChild(new SSBOConcurrentAtomicCase(m_context, (std::string("ssbo_atomic_dispatch_") + de::toString(callCounts[callCountNdx]) + "_calls_" + ssboInvocations[invocationNdx].name + "_invocations").c_str(), "", callCounts[callCountNdx], ssboInvocations[invocationNdx].count));
3349 for (int invocationNdx = 0; invocationNdx < DE_LENGTH_OF_ARRAY(imageInvocations); ++invocationNdx)
3350 for (int callCountNdx = 0; callCountNdx < DE_LENGTH_OF_ARRAY(callCounts); ++callCountNdx)
3351 withoutBarrierGroup->addChild(new ConcurrentImageAtomicCase(m_context, (std::string("image_atomic_dispatch_") + de::toString(callCounts[callCountNdx]) + "_calls_" + imageInvocations[invocationNdx].name + "_invocations").c_str(), "", callCounts[callCountNdx], imageInvocations[invocationNdx].count));
3353 for (int invocationNdx = 0; invocationNdx < DE_LENGTH_OF_ARRAY(counterInvocations); ++invocationNdx)
3354 for (int callCountNdx = 0; callCountNdx < DE_LENGTH_OF_ARRAY(callCounts); ++callCountNdx)
3355 withoutBarrierGroup->addChild(new ConcurrentAtomicCounterCase(m_context, (std::string("atomic_counter_dispatch_") + de::toString(callCounts[callCountNdx]) + "_calls_" + counterInvocations[invocationNdx].name + "_invocations").c_str(), "", callCounts[callCountNdx], counterInvocations[invocationNdx].count));
3357 for (int invocationNdx = 0; invocationNdx < DE_LENGTH_OF_ARRAY(counterInvocations); ++invocationNdx)
3358 for (int callCountNdx = 0; callCountNdx < DE_LENGTH_OF_ARRAY(callCounts); ++callCountNdx)
3359 withoutBarrierGroup->addChild(new ConcurrentSSBOAtomicCounterMixedCase(m_context, (std::string("ssbo_atomic_counter_mixed_dispatch_") + de::toString(callCounts[callCountNdx]) + "_calls_" + counterInvocations[invocationNdx].name + "_invocations").c_str(), "", callCounts[callCountNdx], counterInvocations[invocationNdx].count));