es31fShaderSharedVarTests.cpp revision 3c827367444ee418f129b2c238299f49d3264554
1/*------------------------------------------------------------------------- 2 * drawElements Quality Program OpenGL ES 3.1 Module 3 * ------------------------------------------------- 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 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 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 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. 18 * 19 *//*! 20 * \file 21 * \brief GLSL Shared variable tests. 22 *//*--------------------------------------------------------------------*/ 23 24#include "es31fShaderSharedVarTests.hpp" 25#include "es31fShaderAtomicOpTests.hpp" 26#include "gluShaderProgram.hpp" 27#include "gluShaderUtil.hpp" 28#include "gluRenderContext.hpp" 29#include "gluObjectWrapper.hpp" 30#include "gluProgramInterfaceQuery.hpp" 31#include "tcuVector.hpp" 32#include "tcuTestLog.hpp" 33#include "tcuVectorUtil.hpp" 34#include "tcuFormatUtil.hpp" 35#include "deRandom.hpp" 36#include "glwFunctions.hpp" 37#include "glwEnums.hpp" 38 39#include <algorithm> 40#include <set> 41 42namespace deqp 43{ 44namespace gles31 45{ 46namespace Functional 47{ 48 49using std::string; 50using std::vector; 51using tcu::TestLog; 52using tcu::UVec3; 53using std::set; 54using namespace glu; 55 56enum 57{ 58 MAX_VALUE_ARRAY_LENGTH = 16 // * 2 * sizeof(mat4) = 512 59}; 60 61template<typename T, int Size> 62static inline T product (const tcu::Vector<T, Size>& v) 63{ 64 T res = v[0]; 65 for (int ndx = 1; ndx < Size; ndx++) 66 res *= v[ndx]; 67 return res; 68} 69 70class SharedBasicVarCase : public TestCase 71{ 72public: 73 SharedBasicVarCase (Context& context, const char* name, DataType basicType, Precision precision, const tcu::UVec3& workGroupSize); 74 ~SharedBasicVarCase (void); 75 76 void init (void); 77 void deinit (void); 78 IterateResult iterate (void); 79 80private: 81 SharedBasicVarCase (const SharedBasicVarCase& other); 82 SharedBasicVarCase& operator= (const SharedBasicVarCase& other); 83 84 const DataType m_basicType; 85 const Precision m_precision; 86 const tcu::UVec3 m_workGroupSize; 87 88 ShaderProgram* m_program; 89}; 90 91static std::string getBasicCaseDescription (DataType basicType, Precision precision, const tcu::UVec3& workGroupSize) 92{ 93 std::ostringstream str; 94 if (precision != PRECISION_LAST) 95 str << getPrecisionName(precision) << " "; 96 str << getDataTypeName(basicType) << ", work group size = " << workGroupSize; 97 return str.str(); 98} 99 100SharedBasicVarCase::SharedBasicVarCase (Context& context, const char* name, DataType basicType, Precision precision, const tcu::UVec3& workGroupSize) 101 : TestCase (context, name, getBasicCaseDescription(basicType, precision, workGroupSize).c_str()) 102 , m_basicType (basicType) 103 , m_precision (precision) 104 , m_workGroupSize (workGroupSize) 105 , m_program (DE_NULL) 106{ 107} 108 109SharedBasicVarCase::~SharedBasicVarCase (void) 110{ 111 SharedBasicVarCase::deinit(); 112} 113 114void SharedBasicVarCase::init (void) 115{ 116 const int valArrayLength = de::min<int>(MAX_VALUE_ARRAY_LENGTH, product(m_workGroupSize)); 117 const char* precName = m_precision != glu::PRECISION_LAST ? getPrecisionName(m_precision) : ""; 118 const char* typeName = getDataTypeName(m_basicType); 119 std::ostringstream src; 120 121 src << "#version 310 es\n" 122 << "layout (local_size_x = " << m_workGroupSize[0] 123 << ", local_size_y = " << m_workGroupSize[1] 124 << ", local_size_z = " << m_workGroupSize[2] 125 << ") in;\n" 126 << "const uint LOCAL_SIZE = gl_WorkGroupSize.x*gl_WorkGroupSize.y*gl_WorkGroupSize.z;\n" 127 << "shared " << precName << " " << typeName << " s_var;\n" 128 << "uniform " << precName << " " << typeName << " u_val[" << valArrayLength << "];\n" 129 << "uniform " << precName << " " << typeName << " u_ref[" << valArrayLength << "];\n" 130 << "uniform uint u_numIters;\n" 131 << "layout(binding = 0) buffer Result\n" 132 << "{\n" 133 << " bool isOk[LOCAL_SIZE];\n" 134 << "};\n" 135 << "\n" 136 << "void main (void)\n" 137 << "{\n" 138 << " bool allOk = true;\n" 139 << " for (uint ndx = 0u; ndx < u_numIters; ndx++)\n" 140 << " {\n" 141 << " if (ndx == gl_LocalInvocationIndex)\n" 142 << " s_var = u_val[ndx%uint(u_val.length())];\n" 143 << "\n" 144 << " memoryBarrierShared();\n" 145 << "\n" 146 << " if (s_var != u_ref[ndx%uint(u_ref.length())])\n" 147 << " allOk = false;\n" 148 << " }\n" 149 << "\n" 150 << " isOk[gl_LocalInvocationIndex] = allOk;\n" 151 << "}\n"; 152 153 DE_ASSERT(!m_program); 154 m_program = new ShaderProgram(m_context.getRenderContext(), ProgramSources() << ComputeSource(src.str())); 155 156 m_testCtx.getLog() << *m_program; 157 158 if (!m_program->isOk()) 159 { 160 delete m_program; 161 m_program = DE_NULL; 162 throw tcu::TestError("Compile failed"); 163 } 164} 165 166void SharedBasicVarCase::deinit (void) 167{ 168 delete m_program; 169 m_program = DE_NULL; 170} 171 172SharedBasicVarCase::IterateResult SharedBasicVarCase::iterate (void) 173{ 174 const glw::Functions& gl = m_context.getRenderContext().getFunctions(); 175 const deUint32 program = m_program->getProgram(); 176 Buffer outputBuffer (m_context.getRenderContext()); 177 const deUint32 outBlockNdx = gl.getProgramResourceIndex(program, GL_SHADER_STORAGE_BLOCK, "Result"); 178 const InterfaceBlockInfo outBlockInfo = getProgramInterfaceBlockInfo(gl, program, GL_SHADER_STORAGE_BLOCK, outBlockNdx); 179 180 gl.useProgram(program); 181 182 // Setup input values. 183 { 184 const int numValues = (int)product(m_workGroupSize); 185 const int valLoc = gl.getUniformLocation(program, "u_val[0]"); 186 const int refLoc = gl.getUniformLocation(program, "u_ref[0]"); 187 const int iterCountLoc = gl.getUniformLocation(program, "u_numIters"); 188 const int scalarSize = getDataTypeScalarSize(m_basicType); 189 190 if (isDataTypeFloatOrVec(m_basicType)) 191 { 192 const int maxInt = m_precision == glu::PRECISION_LOWP ? 2 : 1024; 193 const int minInt = -de::min(numValues/2, maxInt); 194 vector<float> values (numValues*scalarSize); 195 196 for (int ndx = 0; ndx < (int)values.size(); ndx++) 197 values[ndx] = float(minInt + (ndx % (maxInt-minInt+1))); 198 199 for (int uNdx = 0; uNdx < 2; uNdx++) 200 { 201 const int location = uNdx == 1 ? refLoc : valLoc; 202 203 if (scalarSize == 1) gl.uniform1fv(location, numValues, &values[0]); 204 else if (scalarSize == 2) gl.uniform2fv(location, numValues, &values[0]); 205 else if (scalarSize == 3) gl.uniform3fv(location, numValues, &values[0]); 206 else if (scalarSize == 4) gl.uniform4fv(location, numValues, &values[0]); 207 } 208 } 209 else if (isDataTypeIntOrIVec(m_basicType)) 210 { 211 const int maxInt = m_precision == glu::PRECISION_LOWP ? 64 : 1024; 212 const int minInt = -de::min(numValues/2, maxInt); 213 vector<int> values (numValues*scalarSize); 214 215 for (int ndx = 0; ndx < (int)values.size(); ndx++) 216 values[ndx] = minInt + (ndx % (maxInt-minInt+1)); 217 218 for (int uNdx = 0; uNdx < 2; uNdx++) 219 { 220 const int location = uNdx == 1 ? refLoc : valLoc; 221 222 if (scalarSize == 1) gl.uniform1iv(location, numValues, &values[0]); 223 else if (scalarSize == 2) gl.uniform2iv(location, numValues, &values[0]); 224 else if (scalarSize == 3) gl.uniform3iv(location, numValues, &values[0]); 225 else if (scalarSize == 4) gl.uniform4iv(location, numValues, &values[0]); 226 } 227 } 228 else if (isDataTypeUintOrUVec(m_basicType)) 229 { 230 const deUint32 maxInt = m_precision == glu::PRECISION_LOWP ? 128 : 1024; 231 vector<deUint32> values (numValues*scalarSize); 232 233 for (int ndx = 0; ndx < (int)values.size(); ndx++) 234 values[ndx] = ndx % (maxInt+1); 235 236 for (int uNdx = 0; uNdx < 2; uNdx++) 237 { 238 const int location = uNdx == 1 ? refLoc : valLoc; 239 240 if (scalarSize == 1) gl.uniform1uiv(location, numValues, &values[0]); 241 else if (scalarSize == 2) gl.uniform2uiv(location, numValues, &values[0]); 242 else if (scalarSize == 3) gl.uniform3uiv(location, numValues, &values[0]); 243 else if (scalarSize == 4) gl.uniform4uiv(location, numValues, &values[0]); 244 } 245 } 246 else if (isDataTypeBoolOrBVec(m_basicType)) 247 { 248 de::Random rnd (0x324f); 249 vector<int> values (numValues*scalarSize); 250 251 for (int ndx = 0; ndx < (int)values.size(); ndx++) 252 values[ndx] = rnd.getBool() ? 1 : 0; 253 254 for (int uNdx = 0; uNdx < 2; uNdx++) 255 { 256 const int location = uNdx == 1 ? refLoc : valLoc; 257 258 if (scalarSize == 1) gl.uniform1iv(location, numValues, &values[0]); 259 else if (scalarSize == 2) gl.uniform2iv(location, numValues, &values[0]); 260 else if (scalarSize == 3) gl.uniform3iv(location, numValues, &values[0]); 261 else if (scalarSize == 4) gl.uniform4iv(location, numValues, &values[0]); 262 } 263 } 264 else if (isDataTypeMatrix(m_basicType)) 265 { 266 const int maxInt = m_precision == glu::PRECISION_LOWP ? 2 : 1024; 267 const int minInt = -de::min(numValues/2, maxInt); 268 vector<float> values (numValues*scalarSize); 269 270 for (int ndx = 0; ndx < (int)values.size(); ndx++) 271 values[ndx] = float(minInt + (ndx % (maxInt-minInt+1))); 272 273 for (int uNdx = 0; uNdx < 2; uNdx++) 274 { 275 const int location = uNdx == 1 ? refLoc : valLoc; 276 277 switch (m_basicType) 278 { 279 case TYPE_FLOAT_MAT2: gl.uniformMatrix2fv (location, numValues, DE_FALSE, &values[0]); break; 280 case TYPE_FLOAT_MAT2X3: gl.uniformMatrix2x3fv(location, numValues, DE_FALSE, &values[0]); break; 281 case TYPE_FLOAT_MAT2X4: gl.uniformMatrix2x4fv(location, numValues, DE_FALSE, &values[0]); break; 282 case TYPE_FLOAT_MAT3X2: gl.uniformMatrix3x2fv(location, numValues, DE_FALSE, &values[0]); break; 283 case TYPE_FLOAT_MAT3: gl.uniformMatrix3fv (location, numValues, DE_FALSE, &values[0]); break; 284 case TYPE_FLOAT_MAT3X4: gl.uniformMatrix3x4fv(location, numValues, DE_FALSE, &values[0]); break; 285 case TYPE_FLOAT_MAT4X2: gl.uniformMatrix4x2fv(location, numValues, DE_FALSE, &values[0]); break; 286 case TYPE_FLOAT_MAT4X3: gl.uniformMatrix4x3fv(location, numValues, DE_FALSE, &values[0]); break; 287 case TYPE_FLOAT_MAT4: gl.uniformMatrix4fv (location, numValues, DE_FALSE, &values[0]); break; 288 default: 289 DE_ASSERT(false); 290 } 291 } 292 } 293 294 gl.uniform1ui(iterCountLoc, product(m_workGroupSize)); 295 GLU_EXPECT_NO_ERROR(gl.getError(), "Input value setup failed"); 296 } 297 298 // Setup output buffer. 299 { 300 vector<deUint8> emptyData(outBlockInfo.dataSize); 301 std::fill(emptyData.begin(), emptyData.end(), 0); 302 303 gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, *outputBuffer); 304 gl.bufferData(GL_SHADER_STORAGE_BUFFER, outBlockInfo.dataSize, &emptyData[0], GL_STATIC_READ); 305 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, *outputBuffer); 306 GLU_EXPECT_NO_ERROR(gl.getError(), "Output buffer setup failed"); 307 } 308 309 gl.dispatchCompute(1, 1, 1); 310 311 // Read back and compare 312 { 313 const deUint32 numValues = product(m_workGroupSize); 314 const InterfaceVariableInfo outVarInfo = getProgramInterfaceVariableInfo(gl, program, GL_BUFFER_VARIABLE, outBlockInfo.activeVariables[0]); 315 const void* resPtr = gl.mapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, outBlockInfo.dataSize, GL_MAP_READ_BIT); 316 const int maxErrMsg = 10; 317 int numFailed = 0; 318 319 GLU_EXPECT_NO_ERROR(gl.getError(), "glMapBufferRange()"); 320 TCU_CHECK(resPtr); 321 322 for (deUint32 ndx = 0; ndx < numValues; ndx++) 323 { 324 const int resVal = *((const int*)((const deUint8*)resPtr + outVarInfo.offset + outVarInfo.arrayStride*ndx)); 325 326 if (resVal == 0) 327 { 328 if (numFailed < maxErrMsg) 329 m_testCtx.getLog() << TestLog::Message << "ERROR: isOk[" << ndx << "] = " << resVal << " != true" << TestLog::EndMessage; 330 else if (numFailed == maxErrMsg) 331 m_testCtx.getLog() << TestLog::Message << "..." << TestLog::EndMessage; 332 333 numFailed += 1; 334 } 335 } 336 337 gl.unmapBuffer(GL_SHADER_STORAGE_BUFFER); 338 GLU_EXPECT_NO_ERROR(gl.getError(), "glUnmapBuffer()"); 339 340 m_testCtx.getLog() << TestLog::Message << (numValues-numFailed) << " / " << numValues << " values passed" << TestLog::EndMessage; 341 342 m_testCtx.setTestResult(numFailed == 0 ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL, 343 numFailed == 0 ? "Pass" : "Comparison failed"); 344 } 345 346 return STOP; 347} 348 349ShaderSharedVarTests::ShaderSharedVarTests (Context& context) 350 : TestCaseGroup(context, "shared_var", "Shared Variable Tests") 351{ 352} 353 354ShaderSharedVarTests::~ShaderSharedVarTests (void) 355{ 356} 357 358void ShaderSharedVarTests::init (void) 359{ 360 // .basic_type 361 { 362 tcu::TestCaseGroup *const basicTypeGroup = new tcu::TestCaseGroup(m_testCtx, "basic_type", "Basic Types"); 363 addChild(basicTypeGroup); 364 365 for (int basicType = TYPE_FLOAT; basicType <= TYPE_BOOL_VEC4; basicType++) 366 { 367 if (glu::isDataTypeBoolOrBVec(DataType(basicType))) 368 { 369 const tcu::UVec3 workGroupSize (2,1,3); 370 basicTypeGroup->addChild(new SharedBasicVarCase(m_context, getDataTypeName(DataType(basicType)), DataType(basicType), PRECISION_LAST, workGroupSize)); 371 } 372 else 373 { 374 for (int precision = 0; precision < PRECISION_LAST; precision++) 375 { 376 const tcu::UVec3 workGroupSize (2,1,3); 377 const string name = string(getDataTypeName(DataType(basicType))) + "_" + getPrecisionName(Precision(precision)); 378 379 basicTypeGroup->addChild(new SharedBasicVarCase(m_context, name.c_str(), DataType(basicType), Precision(precision), workGroupSize)); 380 } 381 } 382 } 383 } 384 385 // .work_group_size 386 { 387 tcu::TestCaseGroup *const workGroupSizeGroup = new tcu::TestCaseGroup(m_testCtx, "work_group_size", "Shared Variables with Various Work Group Sizes"); 388 addChild(workGroupSizeGroup); 389 390 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "float_1_1_1", TYPE_FLOAT, PRECISION_HIGHP, tcu::UVec3(1,1,1))); 391 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "float_64_1_1", TYPE_FLOAT, PRECISION_HIGHP, tcu::UVec3(64,1,1))); 392 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "float_1_64_1", TYPE_FLOAT, PRECISION_HIGHP, tcu::UVec3(1,64,1))); 393 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "float_1_1_64", TYPE_FLOAT, PRECISION_HIGHP, tcu::UVec3(1,1,64))); 394 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "float_256_1_1", TYPE_FLOAT, PRECISION_HIGHP, tcu::UVec3(256,1,1))); 395 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "float_1_256_1", TYPE_FLOAT, PRECISION_HIGHP, tcu::UVec3(1,256,1))); 396 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "float_17_5_9", TYPE_FLOAT, PRECISION_HIGHP, tcu::UVec3(17,5,9))); 397 398 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "vec4_1_1_1", TYPE_FLOAT_VEC4, PRECISION_HIGHP, tcu::UVec3(1,1,1))); 399 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "vec4_64_1_1", TYPE_FLOAT_VEC4, PRECISION_HIGHP, tcu::UVec3(64,1,1))); 400 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "vec4_1_64_1", TYPE_FLOAT_VEC4, PRECISION_HIGHP, tcu::UVec3(1,64,1))); 401 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "vec4_1_1_64", TYPE_FLOAT_VEC4, PRECISION_HIGHP, tcu::UVec3(1,1,64))); 402 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "vec4_256_1_1", TYPE_FLOAT_VEC4, PRECISION_HIGHP, tcu::UVec3(256,1,1))); 403 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "vec4_1_256_1", TYPE_FLOAT_VEC4, PRECISION_HIGHP, tcu::UVec3(1,256,1))); 404 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "vec4_17_5_9", TYPE_FLOAT_VEC4, PRECISION_HIGHP, tcu::UVec3(17,5,9))); 405 406 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "mat4_1_1_1", TYPE_FLOAT_MAT4, PRECISION_HIGHP, tcu::UVec3(1,1,1))); 407 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "mat4_64_1_1", TYPE_FLOAT_MAT4, PRECISION_HIGHP, tcu::UVec3(64,1,1))); 408 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "mat4_1_64_1", TYPE_FLOAT_MAT4, PRECISION_HIGHP, tcu::UVec3(1,64,1))); 409 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "mat4_1_1_64", TYPE_FLOAT_MAT4, PRECISION_HIGHP, tcu::UVec3(1,1,64))); 410 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "mat4_256_1_1", TYPE_FLOAT_MAT4, PRECISION_HIGHP, tcu::UVec3(256,1,1))); 411 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "mat4_1_256_1", TYPE_FLOAT_MAT4, PRECISION_HIGHP, tcu::UVec3(1,256,1))); 412 workGroupSizeGroup->addChild(new SharedBasicVarCase(m_context, "mat4_17_5_9", TYPE_FLOAT_MAT4, PRECISION_HIGHP, tcu::UVec3(17,5,9))); 413 } 414 415 // .atomic 416 addChild(new ShaderAtomicOpTests(m_context, "atomic", ATOMIC_OPERAND_SHARED_VARIABLE)); 417} 418 419} // Functional 420} // gles31 421} // deqp 422