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 Sample variable tests
22 *//*--------------------------------------------------------------------*/
24 #include "es31fSampleVariableTests.hpp"
25 #include "es31fMultisampleShaderRenderCase.hpp"
26 #include "tcuSurface.hpp"
27 #include "tcuTestLog.hpp"
28 #include "tcuRenderTarget.hpp"
29 #include "tcuTextureUtil.hpp"
30 #include "tcuVectorUtil.hpp"
31 #include "tcuFormatUtil.hpp"
32 #include "tcuStringTemplate.hpp"
33 #include "gluContextInfo.hpp"
34 #include "gluShaderProgram.hpp"
35 #include "gluRenderContext.hpp"
36 #include "glwFunctions.hpp"
37 #include "glwEnums.hpp"
38 #include "deStringUtil.hpp"
56 virtual bool verify (const tcu::RGBA& testColor, const tcu::IVec2& position) const = 0;
57 virtual void logInfo (tcu::TestLog& log) const = 0;
60 class ColorVerifier : public Verifier
63 ColorVerifier (const tcu::Vec3& _color, int _threshold = 8)
64 : m_color (tcu::Vec4(_color.x(), _color.y(), _color.z(), 1.0f))
65 , m_threshold (tcu::IVec3(_threshold))
69 ColorVerifier (const tcu::Vec3& _color, tcu::IVec3 _threshold)
70 : m_color (tcu::Vec4(_color.x(), _color.y(), _color.z(), 1.0f))
71 , m_threshold (_threshold)
75 bool verify (const tcu::RGBA& testColor, const tcu::IVec2& position) const
78 return !tcu::boolAny(tcu::greaterThan(tcu::abs(m_color.toIVec().swizzle(0, 1, 2) - testColor.toIVec().swizzle(0, 1, 2)), tcu::IVec3(m_threshold)));
81 void logInfo (tcu::TestLog& log) const
83 // full threshold? print * for clarity
84 log << tcu::TestLog::Message
85 << "Expecting unicolored image, color = RGB("
86 << ((m_threshold[0] >= 255) ? ("*") : (de::toString(m_color.getRed()))) << ", "
87 << ((m_threshold[1] >= 255) ? ("*") : (de::toString(m_color.getGreen()))) << ", "
88 << ((m_threshold[2] >= 255) ? ("*") : (de::toString(m_color.getBlue()))) << ")"
89 << tcu::TestLog::EndMessage;
92 const tcu::RGBA m_color;
93 const tcu::IVec3 m_threshold;
96 class FullBlueSomeGreenVerifier : public Verifier
99 FullBlueSomeGreenVerifier (void)
103 bool verify (const tcu::RGBA& testColor, const tcu::IVec2& position) const
107 // Values from 0.0 and 1.0 are accurate
109 if (testColor.getRed() != 0)
111 if (testColor.getGreen() == 0)
113 if (testColor.getBlue() != 255)
118 void logInfo (tcu::TestLog& log) const
120 log << tcu::TestLog::Message << "Expecting color c = (0.0, x, 1.0), x > 0.0" << tcu::TestLog::EndMessage;
124 class NoRedVerifier : public Verifier
131 bool verify (const tcu::RGBA& testColor, const tcu::IVec2& position) const
134 return testColor.getRed() == 0;
137 void logInfo (tcu::TestLog& log) const
139 log << tcu::TestLog::Message << "Expecting zero-valued red channel." << tcu::TestLog::EndMessage;
143 class SampleAverageVerifier : public Verifier
146 SampleAverageVerifier (int _numSamples);
148 bool verify (const tcu::RGBA& testColor, const tcu::IVec2& position) const;
149 void logInfo (tcu::TestLog& log) const;
151 const int m_numSamples;
152 const bool m_isStatisticallySignificant;
153 float m_distanceThreshold;
156 SampleAverageVerifier::SampleAverageVerifier (int _numSamples)
157 : m_numSamples (_numSamples)
158 , m_isStatisticallySignificant (_numSamples >= 4)
159 , m_distanceThreshold (0.0f)
161 // approximate Bates distribution as normal
162 const float variance = (1.0f / (12.0f * (float)m_numSamples));
163 const float standardDeviation = deFloatSqrt(variance);
165 // 95% of means of sample positions are within 2 standard deviations if
166 // they were randomly assigned. Sample patterns are expected to be more
167 // uniform than a random pattern.
168 m_distanceThreshold = 2 * standardDeviation;
171 bool SampleAverageVerifier::verify (const tcu::RGBA& testColor, const tcu::IVec2& position) const
174 DE_ASSERT(m_isStatisticallySignificant);
176 const tcu::Vec2 avgPosition ((float)testColor.getGreen() / 255.0f, (float)testColor.getBlue() / 255.0f);
177 const tcu::Vec2 distanceFromCenter = tcu::abs(avgPosition - tcu::Vec2(0.5f, 0.5f));
179 return distanceFromCenter.x() < m_distanceThreshold && distanceFromCenter.y() < m_distanceThreshold;
182 void SampleAverageVerifier::logInfo (tcu::TestLog& log) const
184 log << tcu::TestLog::Message << "Expecting average sample position to be near the pixel center. Maximum per-axis distance " << m_distanceThreshold << tcu::TestLog::EndMessage;
187 class PartialDiscardVerifier : public Verifier
190 PartialDiscardVerifier (void)
194 bool verify (const tcu::RGBA& testColor, const tcu::IVec2& position) const
198 return (testColor.getGreen() != 0) && (testColor.getGreen() != 255);
201 void logInfo (tcu::TestLog& log) const
203 log << tcu::TestLog::Message << "Expecting color non-zero and non-saturated green channel" << tcu::TestLog::EndMessage;
207 static bool verifyImageWithVerifier (const tcu::Surface& resultImage, tcu::TestLog& log, const Verifier& verifier, bool logOnSuccess = true)
209 tcu::Surface errorMask (resultImage.getWidth(), resultImage.getHeight());
212 tcu::clear(errorMask.getAccess(), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
216 log << tcu::TestLog::Message << "Verifying image." << tcu::TestLog::EndMessage;
217 verifier.logInfo(log);
220 for (int y = 0; y < resultImage.getHeight(); ++y)
221 for (int x = 0; x < resultImage.getWidth(); ++x)
223 const tcu::RGBA color = resultImage.getPixel(x, y);
225 // verify color value is valid for this pixel position
226 if (!verifier.verify(color, tcu::IVec2(x,y)))
229 errorMask.setPixel(x, y, tcu::RGBA::red());
235 // describe the verification logic if we haven't already
237 verifier.logInfo(log);
239 log << tcu::TestLog::Message << "Image verification failed." << tcu::TestLog::EndMessage
240 << tcu::TestLog::ImageSet("Verification", "Image Verification")
241 << tcu::TestLog::Image("Result", "Result image", resultImage.getAccess())
242 << tcu::TestLog::Image("ErrorMask", "Error Mask", errorMask.getAccess())
243 << tcu::TestLog::EndImageSet;
245 else if (logOnSuccess)
247 log << tcu::TestLog::Message << "Image verification passed." << tcu::TestLog::EndMessage
248 << tcu::TestLog::ImageSet("Verification", "Image Verification")
249 << tcu::TestLog::Image("Result", "Result image", resultImage.getAccess())
250 << tcu::TestLog::EndImageSet;
256 class MultisampleRenderCase : public MultisampleShaderRenderUtil::MultisampleRenderCase
259 MultisampleRenderCase (Context& context, const char* name, const char* desc, int numSamples, RenderTarget target, int renderSize, int flags = 0);
260 virtual ~MultisampleRenderCase (void);
262 virtual void init (void);
266 MultisampleRenderCase::MultisampleRenderCase (Context& context, const char* name, const char* desc, int numSamples, RenderTarget target, int renderSize, int flags)
267 : MultisampleShaderRenderUtil::MultisampleRenderCase(context, name, desc, numSamples, target, renderSize, flags)
269 DE_ASSERT(target < TARGET_LAST);
272 MultisampleRenderCase::~MultisampleRenderCase (void)
274 MultisampleRenderCase::deinit();
277 void MultisampleRenderCase::init (void)
279 const bool supportsES32orGL45 =
280 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
281 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
282 if (!supportsES32orGL45 && !m_context.getContextInfo().isExtensionSupported("GL_OES_sample_variables"))
283 TCU_THROW(NotSupportedError, "Test requires GL_OES_sample_variables extension or a context version 3.2 or higher.");
285 MultisampleShaderRenderUtil::MultisampleRenderCase::init();
288 class NumSamplesCase : public MultisampleRenderCase
291 NumSamplesCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target);
292 ~NumSamplesCase (void);
294 std::string genFragmentSource (int numTargetSamples) const;
295 bool verifyImage (const tcu::Surface& resultImage);
304 NumSamplesCase::NumSamplesCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target)
305 : MultisampleRenderCase(context, name, desc, sampleCount, target, RENDER_SIZE)
309 NumSamplesCase::~NumSamplesCase (void)
313 std::string NumSamplesCase::genFragmentSource (int numTargetSamples) const
315 std::ostringstream buf;
316 const bool supportsES32orGL45 =
317 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
318 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
319 map<string, string> args;
320 args["GLSL_VERSION_DECL"] = (supportsES32orGL45) ? getGLSLVersionDeclaration(glu::GLSL_VERSION_320_ES) : getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
321 args["GLSL_EXTENSION"] = (supportsES32orGL45) ? "" : "#extension GL_OES_sample_variables : require";
323 buf << "${GLSL_VERSION_DECL}\n"
324 "${GLSL_EXTENSION}\n"
325 "layout(location = 0) out mediump vec4 fragColor;\n"
328 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
329 " if (gl_NumSamples == " << numTargetSamples << ")\n"
330 " fragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
333 return tcu::StringTemplate(buf.str()).specialize(args);
336 bool NumSamplesCase::verifyImage (const tcu::Surface& resultImage)
338 return verifyImageWithVerifier(resultImage, m_testCtx.getLog(), NoRedVerifier());
341 class MaxSamplesCase : public MultisampleRenderCase
344 MaxSamplesCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target);
345 ~MaxSamplesCase (void);
349 std::string genFragmentSource (int numTargetSamples) const;
350 bool verifyImage (const tcu::Surface& resultImage);
358 MaxSamplesCase::MaxSamplesCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target)
359 : MultisampleRenderCase(context, name, desc, sampleCount, target, RENDER_SIZE)
363 MaxSamplesCase::~MaxSamplesCase (void)
367 void MaxSamplesCase::preDraw (void)
369 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
370 deInt32 maxSamples = -1;
374 gl.getIntegerv(GL_MAX_SAMPLES, &maxSamples);
375 GLU_EXPECT_NO_ERROR(gl.getError(), "query GL_MAX_SAMPLES");
377 m_testCtx.getLog() << tcu::TestLog::Message << "GL_MAX_SAMPLES = " << maxSamples << tcu::TestLog::EndMessage;
382 const int maxSampleLoc = gl.getUniformLocation(m_program->getProgram(), "u_maxSamples");
383 if (maxSampleLoc == -1)
384 throw tcu::TestError("Location of u_maxSamples was -1");
386 gl.uniform1i(maxSampleLoc, maxSamples);
387 GLU_EXPECT_NO_ERROR(gl.getError(), "set u_maxSamples uniform");
389 m_testCtx.getLog() << tcu::TestLog::Message << "Set u_maxSamples = " << maxSamples << tcu::TestLog::EndMessage;
393 std::string MaxSamplesCase::genFragmentSource (int numTargetSamples) const
395 DE_UNREF(numTargetSamples);
397 std::ostringstream buf;
398 const bool supportsES32orGL45 =
399 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
400 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
401 map<string, string> args;
402 args["GLSL_VERSION_DECL"] = (supportsES32orGL45) ? getGLSLVersionDeclaration(glu::GLSL_VERSION_320_ES) : getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
403 args["GLSL_EXTENSION"] = (supportsES32orGL45) ? "" : "#extension GL_OES_sample_variables : require";
405 buf << "${GLSL_VERSION_DECL}\n"
406 "${GLSL_EXTENSION}\n"
407 "layout(location = 0) out mediump vec4 fragColor;\n"
408 "uniform mediump int u_maxSamples;\n"
411 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
412 " if (gl_MaxSamples == u_maxSamples)\n"
413 " fragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
416 return tcu::StringTemplate(buf.str()).specialize(args);
419 bool MaxSamplesCase::verifyImage (const tcu::Surface& resultImage)
421 return verifyImageWithVerifier(resultImage, m_testCtx.getLog(), NoRedVerifier());
424 class SampleIDCase : public MultisampleRenderCase
427 SampleIDCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target);
428 ~SampleIDCase (void);
433 std::string genFragmentSource (int numTargetSamples) const;
434 bool verifyImage (const tcu::Surface& resultImage);
435 bool verifySampleBuffers (const std::vector<tcu::Surface>& resultBuffers);
441 enum VerificationMode
443 VERIFY_USING_SAMPLES,
444 VERIFY_USING_SELECTION,
447 const VerificationMode m_vericationMode;
450 SampleIDCase::SampleIDCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target)
451 : MultisampleRenderCase (context, name, desc, sampleCount, target, RENDER_SIZE, MultisampleShaderRenderUtil::MultisampleRenderCase::FLAG_VERIFY_MSAA_TEXTURE_SAMPLE_BUFFERS)
452 , m_vericationMode ((target == TARGET_TEXTURE) ? (VERIFY_USING_SAMPLES) : (VERIFY_USING_SELECTION))
456 SampleIDCase::~SampleIDCase (void)
460 void SampleIDCase::init (void)
462 // log the test method and expectations
463 if (m_vericationMode == VERIFY_USING_SAMPLES)
465 << tcu::TestLog::Message
466 << "Writing gl_SampleID to the green channel of the texture and verifying texture values, expecting:\n"
467 << " 1) 0 with non-multisample targets.\n"
468 << " 2) value N at sample index N of a multisample texture\n"
469 << tcu::TestLog::EndMessage;
470 else if (m_vericationMode == VERIFY_USING_SELECTION)
472 << tcu::TestLog::Message
473 << "Selecting a single sample id for each pixel and writing color only if gl_SampleID == selected.\n"
474 << "Expecting all output pixels to be partially (multisample) or fully (singlesample) colored.\n"
475 << tcu::TestLog::EndMessage;
479 MultisampleRenderCase::init();
482 std::string SampleIDCase::genFragmentSource (int numTargetSamples) const
484 DE_ASSERT(numTargetSamples != 0);
486 std::ostringstream buf;
487 const bool supportsES32orGL45 =
488 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
489 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
490 map<string, string> args;
491 args["GLSL_VERSION_DECL"] = (supportsES32orGL45) ? getGLSLVersionDeclaration(glu::GLSL_VERSION_320_ES) : getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
492 args["GLSL_EXTENSION"] = (supportsES32orGL45) ? "" : "#extension GL_OES_sample_variables : require";
494 if (m_vericationMode == VERIFY_USING_SAMPLES)
496 // encode the id to the output, and then verify it during sampling
497 buf << "${GLSL_VERSION_DECL}\n"
498 "${GLSL_EXTENSION}\n"
499 "layout(location = 0) out mediump vec4 fragColor;\n"
502 " highp float normalizedSample = float(gl_SampleID) / float(" << numTargetSamples << ");\n"
503 " fragColor = vec4(0.0, normalizedSample, 1.0, 1.0);\n"
506 else if (m_vericationMode == VERIFY_USING_SELECTION)
508 if (numTargetSamples == 1)
510 // single sample, just verify value is 0
511 buf << "${GLSL_VERSION_DECL}\n"
512 "${GLSL_EXTENSION}\n"
513 "layout(location = 0) out mediump vec4 fragColor;\n"
516 " if (gl_SampleID == 0)\n"
517 " fragColor = vec4(0.0, 1.0, 1.0, 1.0);\n"
519 " fragColor = vec4(0.0, 0.0, 1.0, 1.0);\n"
524 // select only one sample per PIXEL
525 buf << "${GLSL_VERSION_DECL}\n"
526 "${GLSL_EXTENSION}\n"
527 "in highp vec4 v_position;\n"
528 "layout(location = 0) out mediump vec4 fragColor;\n"
531 " highp vec2 relPosition = (v_position.xy + vec2(1.0, 1.0)) / 2.0;\n"
532 " highp ivec2 pixelPos = ivec2(floor(relPosition * " << (int)RENDER_SIZE << ".0));\n"
533 " highp int selectedID = abs(pixelPos.x + 17 * pixelPos.y) % " << numTargetSamples << ";\n"
535 " if (gl_SampleID == selectedID)\n"
536 " fragColor = vec4(0.0, 1.0, 1.0, 1.0);\n"
538 " fragColor = vec4(0.0, 0.0, 1.0, 1.0);\n"
545 return tcu::StringTemplate(buf.str()).specialize(args);
548 bool SampleIDCase::verifyImage (const tcu::Surface& resultImage)
550 if (m_vericationMode == VERIFY_USING_SAMPLES)
556 else if (m_vericationMode == VERIFY_USING_SELECTION)
558 // should result in full blue and some green everywhere
559 return verifyImageWithVerifier(resultImage, m_testCtx.getLog(), FullBlueSomeGreenVerifier());
568 bool SampleIDCase::verifySampleBuffers (const std::vector<tcu::Surface>& resultBuffers)
570 // Verify all sample buffers
575 m_testCtx.getLog() << tcu::TestLog::ImageSet("SampleBuffers", "Image sample buffers");
576 for (int sampleNdx = 0; sampleNdx < (int)resultBuffers.size(); ++sampleNdx)
577 m_testCtx.getLog() << tcu::TestLog::Image("Buffer" + de::toString(sampleNdx), "Sample " + de::toString(sampleNdx), resultBuffers[sampleNdx].getAccess());
578 m_testCtx.getLog() << tcu::TestLog::EndImageSet;
581 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying sample buffers" << tcu::TestLog::EndMessage;
582 for (int sampleNdx = 0; sampleNdx < (int)resultBuffers.size(); ++sampleNdx)
584 // sample id should be sample index
585 const int threshold = 255 / 4 / m_numTargetSamples + 1;
586 const float sampleIdColor = (float)sampleNdx / (float)m_numTargetSamples;
588 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying sample " << (sampleNdx+1) << "/" << (int)resultBuffers.size() << tcu::TestLog::EndMessage;
589 allOk &= verifyImageWithVerifier(resultBuffers[sampleNdx], m_testCtx.getLog(), ColorVerifier(tcu::Vec3(0.0f, sampleIdColor, 1.0f), tcu::IVec3(1, threshold, 1)), false);
593 m_testCtx.getLog() << tcu::TestLog::Message << "Sample buffer verification failed" << tcu::TestLog::EndMessage;
598 class SamplePosDistributionCase : public MultisampleRenderCase
601 SamplePosDistributionCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target);
602 ~SamplePosDistributionCase (void);
611 std::string genFragmentSource (int numTargetSamples) const;
612 bool verifyImage (const tcu::Surface& resultImage);
613 bool verifySampleBuffers (const std::vector<tcu::Surface>& resultBuffers);
616 SamplePosDistributionCase::SamplePosDistributionCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target)
617 : MultisampleRenderCase(context, name, desc, sampleCount, target, RENDER_SIZE, MultisampleShaderRenderUtil::MultisampleRenderCase::FLAG_VERIFY_MSAA_TEXTURE_SAMPLE_BUFFERS)
621 SamplePosDistributionCase::~SamplePosDistributionCase (void)
625 void SamplePosDistributionCase::init (void)
627 // log the test method and expectations
628 if (m_renderTarget == TARGET_TEXTURE)
631 << tcu::TestLog::Message
632 << "Verifying gl_SamplePosition value:\n"
633 << " 1) With non-multisample targets: Expect the center of the pixel.\n"
634 << " 2) With multisample targets:\n"
635 << " a) Expect legal sample position.\n"
636 << " b) Sample position is unique within the set of all sample positions of a pixel.\n"
637 << " c) Sample position distribution is uniform or almost uniform.\n"
638 << tcu::TestLog::EndMessage;
643 << tcu::TestLog::Message
644 << "Verifying gl_SamplePosition value:\n"
645 << " 1) With non-multisample targets: Expect the center of the pixel.\n"
646 << " 2) With multisample targets:\n"
647 << " a) Expect legal sample position.\n"
648 << " b) Sample position distribution is uniform or almost uniform.\n"
649 << tcu::TestLog::EndMessage;
652 MultisampleRenderCase::init();
655 std::string SamplePosDistributionCase::genFragmentSource (int numTargetSamples) const
657 DE_ASSERT(numTargetSamples != 0);
658 DE_UNREF(numTargetSamples);
660 const bool multisampleTarget = (m_numRequestedSamples > 0) || (m_renderTarget == TARGET_DEFAULT && m_context.getRenderTarget().getNumSamples() > 1);
661 std::ostringstream buf;
662 const bool supportsES32orGL45 =
663 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
664 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
665 map<string, string> args;
666 args["GLSL_VERSION_DECL"] = (supportsES32orGL45) ? getGLSLVersionDeclaration(glu::GLSL_VERSION_320_ES) : getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
667 args["GLSL_EXTENSION"] = (supportsES32orGL45) ? "\n" : "#extension GL_OES_sample_variables : require\n";
669 if (multisampleTarget)
671 // encode the position to the output, use red channel as error channel
672 buf << "${GLSL_VERSION_DECL}\n"
673 "${GLSL_EXTENSION}\n"
674 "layout(location = 0) out mediump vec4 fragColor;\n"
677 " if (gl_SamplePosition.x < 0.0 || gl_SamplePosition.x > 1.0 || gl_SamplePosition.y < 0.0 || gl_SamplePosition.y > 1.0)\n"
678 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
680 " fragColor = vec4(0.0, gl_SamplePosition.x, gl_SamplePosition.y, 1.0);\n"
685 // verify value is ok
686 buf << "${GLSL_VERSION_DECL}\n"
687 "${GLSL_EXTENSION}\n"
688 "layout(location = 0) out mediump vec4 fragColor;\n"
691 " if (gl_SamplePosition.x != 0.5 || gl_SamplePosition.y != 0.5)\n"
692 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
694 " fragColor = vec4(0.0, gl_SamplePosition.x, gl_SamplePosition.y, 1.0);\n"
698 return tcu::StringTemplate(buf.str()).specialize(args);
701 bool SamplePosDistributionCase::verifyImage (const tcu::Surface& resultImage)
703 const int sampleCount = (m_renderTarget == TARGET_DEFAULT) ? (m_context.getRenderTarget().getNumSamples()) : (m_numRequestedSamples);
704 SampleAverageVerifier verifier (sampleCount);
706 // check there is nothing in the error channel
707 if (!verifyImageWithVerifier(resultImage, m_testCtx.getLog(), NoRedVerifier()))
710 // position average should be around 0.5, 0.5
711 if (verifier.m_isStatisticallySignificant && !verifyImageWithVerifier(resultImage, m_testCtx.getLog(), verifier))
712 throw MultisampleShaderRenderUtil::QualityWarning("Bias detected, sample positions are not uniformly distributed within the pixel");
717 bool SamplePosDistributionCase::verifySampleBuffers (const std::vector<tcu::Surface>& resultBuffers)
719 const int width = resultBuffers[0].getWidth();
720 const int height = resultBuffers[0].getHeight();
722 bool distibutionError = false;
724 // Check sample range, uniqueness, and distribution, log layers
726 m_testCtx.getLog() << tcu::TestLog::ImageSet("SampleBuffers", "Image sample buffers");
727 for (int sampleNdx = 0; sampleNdx < (int)resultBuffers.size(); ++sampleNdx)
728 m_testCtx.getLog() << tcu::TestLog::Image("Buffer" + de::toString(sampleNdx), "Sample " + de::toString(sampleNdx), resultBuffers[sampleNdx].getAccess());
729 m_testCtx.getLog() << tcu::TestLog::EndImageSet;
736 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying sample position range" << tcu::TestLog::EndMessage;
737 for (int sampleNdx = 0; sampleNdx < (int)resultBuffers.size(); ++sampleNdx)
739 // shader does the check, just check the shader error output (red)
740 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying sample " << (sampleNdx+1) << "/" << (int)resultBuffers.size() << tcu::TestLog::EndMessage;
741 rangeOk &= verifyImageWithVerifier(resultBuffers[sampleNdx], m_testCtx.getLog(), NoRedVerifier(), false);
748 m_testCtx.getLog() << tcu::TestLog::Message << "Sample position verification failed." << tcu::TestLog::EndMessage;
754 bool uniquenessOk = true;
755 tcu::Surface errorMask (width, height);
756 std::vector<tcu::Vec2> samplePositions (resultBuffers.size());
758 const int printFloodLimit = 5;
760 tcu::clear(errorMask.getAccess(), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
762 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying sample position uniqueness." << tcu::TestLog::EndMessage;
764 for (int y = 0; y < height; ++y)
765 for (int x = 0; x < width; ++x)
767 bool samplePosNotUnique = false;
769 for (int sampleNdx = 0; sampleNdx < (int)resultBuffers.size(); ++sampleNdx)
771 const tcu::RGBA color = resultBuffers[sampleNdx].getPixel(x, y);
772 samplePositions[sampleNdx] = tcu::Vec2((float)color.getGreen() / 255.0f, (float)color.getBlue() / 255.0f);
775 // Just check there are no two samples with same positions
776 for (int sampleNdxA = 0; sampleNdxA < (int)resultBuffers.size() && (!samplePosNotUnique || printCount < printFloodLimit); ++sampleNdxA)
777 for (int sampleNdxB = sampleNdxA+1; sampleNdxB < (int)resultBuffers.size() && (!samplePosNotUnique || printCount < printFloodLimit); ++sampleNdxB)
779 if (samplePositions[sampleNdxA] == samplePositions[sampleNdxB])
781 if (++printCount <= printFloodLimit)
784 << tcu::TestLog::Message
785 << "Pixel (" << x << ", " << y << "): Samples " << sampleNdxA << " and " << sampleNdxB << " have the same position."
786 << tcu::TestLog::EndMessage;
789 samplePosNotUnique = true;
790 uniquenessOk = false;
791 errorMask.setPixel(x, y, tcu::RGBA::red());
799 if (printCount > printFloodLimit)
801 << tcu::TestLog::Message
803 << "Omitted " << (printCount-printFloodLimit) << " error descriptions."
804 << tcu::TestLog::EndMessage;
807 << tcu::TestLog::Message << "Image verification failed." << tcu::TestLog::EndMessage
808 << tcu::TestLog::ImageSet("Verification", "Image Verification")
809 << tcu::TestLog::Image("ErrorMask", "Error Mask", errorMask.getAccess())
810 << tcu::TestLog::EndImageSet;
816 // check distribution
818 const SampleAverageVerifier verifier (m_numTargetSamples);
819 tcu::Surface errorMask (width, height);
821 const int printFloodLimit = 5;
823 tcu::clear(errorMask.getAccess(), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
825 // don't bother with small sample counts
826 if (verifier.m_isStatisticallySignificant)
828 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying sample position distribution is (nearly) unbiased." << tcu::TestLog::EndMessage;
829 verifier.logInfo(m_testCtx.getLog());
831 for (int y = 0; y < height; ++y)
832 for (int x = 0; x < width; ++x)
834 tcu::IVec3 colorSum(0, 0, 0);
838 for (int sampleNdx = 0; sampleNdx < (int)resultBuffers.size(); ++sampleNdx)
840 const tcu::RGBA color = resultBuffers[sampleNdx].getPixel(x, y);
841 colorSum.x() += color.getRed();
842 colorSum.y() += color.getBlue();
843 colorSum.z() += color.getGreen();
846 colorSum.x() /= m_numTargetSamples;
847 colorSum.y() /= m_numTargetSamples;
848 colorSum.z() /= m_numTargetSamples;
850 // verify average sample position
852 if (!verifier.verify(tcu::RGBA(colorSum.x(), colorSum.y(), colorSum.z(), 0), tcu::IVec2(x, y)))
854 if (++printCount <= printFloodLimit)
857 << tcu::TestLog::Message
858 << "Pixel (" << x << ", " << y << "): Sample distribution is biased."
859 << tcu::TestLog::EndMessage;
862 distibutionError = true;
863 errorMask.setPixel(x, y, tcu::RGBA::red());
867 // sub-verification result
868 if (distibutionError)
870 if (printCount > printFloodLimit)
872 << tcu::TestLog::Message
874 << "Omitted " << (printCount-printFloodLimit) << " error descriptions."
875 << tcu::TestLog::EndMessage;
878 << tcu::TestLog::Message << "Image verification failed." << tcu::TestLog::EndMessage
879 << tcu::TestLog::ImageSet("Verification", "Image Verification")
880 << tcu::TestLog::Image("ErrorMask", "Error Mask", errorMask.getAccess())
881 << tcu::TestLog::EndImageSet;
889 else if (distibutionError)
890 throw MultisampleShaderRenderUtil::QualityWarning("Bias detected, sample positions are not uniformly distributed within the pixel");
893 m_testCtx.getLog() << tcu::TestLog::Message << "Verification ok." << tcu::TestLog::EndMessage;
898 class SamplePosCorrectnessCase : public MultisampleRenderCase
901 SamplePosCorrectnessCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target);
902 ~SamplePosCorrectnessCase (void);
912 void postDraw (void);
914 std::string genVertexSource (int numTargetSamples) const;
915 std::string genFragmentSource (int numTargetSamples) const;
916 bool verifyImage (const tcu::Surface& resultImage);
918 bool m_useSampleQualifier;
921 SamplePosCorrectnessCase::SamplePosCorrectnessCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target)
922 : MultisampleRenderCase (context, name, desc, sampleCount, target, RENDER_SIZE)
923 , m_useSampleQualifier (false)
927 SamplePosCorrectnessCase::~SamplePosCorrectnessCase (void)
931 void SamplePosCorrectnessCase::init (void)
933 auto ctxType = m_context.getRenderContext().getType();
934 const bool isES32orGL45 = glu::contextSupports(ctxType, glu::ApiType::es(3, 2)) ||
935 glu::contextSupports(ctxType, glu::ApiType::core(4, 5));
937 // requirements: per-invocation interpolation required
938 if (!isES32orGL45 && !m_context.getContextInfo().isExtensionSupported("GL_OES_shader_multisample_interpolation") &&
939 !m_context.getContextInfo().isExtensionSupported("GL_OES_sample_shading"))
940 TCU_THROW(NotSupportedError, "Test requires GL_OES_shader_multisample_interpolation or GL_OES_sample_shading extension or a context version 3.2 or higher.");
942 // prefer to use the sample qualifier path
943 m_useSampleQualifier = m_context.getContextInfo().isExtensionSupported("GL_OES_shader_multisample_interpolation");
945 // log the test method and expectations
947 << tcu::TestLog::Message
948 << "Verifying gl_SamplePosition correctness:\n"
949 << " 1) Varying values should be sampled at the sample position.\n"
950 << " => fract(screenSpacePosition) == gl_SamplePosition\n"
951 << tcu::TestLog::EndMessage;
953 MultisampleRenderCase::init();
956 void SamplePosCorrectnessCase::preDraw (void)
958 if (!m_useSampleQualifier)
960 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
962 // use GL_OES_sample_shading to set per fragment sample invocation interpolation
963 gl.enable(GL_SAMPLE_SHADING);
964 gl.minSampleShading(1.0f);
965 GLU_EXPECT_NO_ERROR(gl.getError(), "set ratio");
967 m_testCtx.getLog() << tcu::TestLog::Message << "Enabling per-sample interpolation with GL_SAMPLE_SHADING." << tcu::TestLog::EndMessage;
971 void SamplePosCorrectnessCase::postDraw (void)
973 if (!m_useSampleQualifier)
975 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
977 gl.disable(GL_SAMPLE_SHADING);
978 gl.minSampleShading(1.0f);
979 GLU_EXPECT_NO_ERROR(gl.getError(), "set ratio");
983 std::string SamplePosCorrectnessCase::genVertexSource (int numTargetSamples) const
985 DE_UNREF(numTargetSamples);
986 const bool supportsES32orGL45 =
987 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
988 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
990 std::ostringstream buf;
991 map<string, string> args;
992 args["GLSL_VERSION_DECL"] = supportsES32orGL45 ? getGLSLVersionDeclaration(glu::GLSL_VERSION_320_ES) : getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
993 args["GLSL_EXTENSION"] = supportsES32orGL45 ? "" : m_useSampleQualifier ? "#extension GL_OES_shader_multisample_interpolation : require" : "";
995 buf << "${GLSL_VERSION_DECL}\n"
996 "${GLSL_EXTENSION}\n"
997 << "in highp vec4 a_position;\n"
998 << ((m_useSampleQualifier) ? ("sample ") : ("")) << "out highp vec4 v_position;\n"
1001 " gl_Position = a_position;\n"
1002 " v_position = a_position;\n"
1005 return tcu::StringTemplate(buf.str()).specialize(args);
1008 std::string SamplePosCorrectnessCase::genFragmentSource (int numTargetSamples) const
1010 DE_UNREF(numTargetSamples);
1012 std::ostringstream buf;
1013 const bool supportsES32orGL45 =
1014 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
1015 glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
1016 map<string, string> args;
1017 args["GLSL_VERSION_DECL"] = supportsES32orGL45 ? getGLSLVersionDeclaration(glu::GLSL_VERSION_320_ES) : getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
1018 args["GLSL_SAMPLE_EXTENSION"] = supportsES32orGL45 ? "" : "#extension GL_OES_sample_variables : require";
1019 args["GLSL_MULTISAMPLE_EXTENSION"] = supportsES32orGL45 ? "" : m_useSampleQualifier ? "#extension GL_OES_shader_multisample_interpolation : require" : "";
1021 // encode the position to the output, use red channel as error channel
1022 buf << "${GLSL_VERSION_DECL}\n"
1023 "${GLSL_SAMPLE_EXTENSION}\n"
1024 "${GLSL_MULTISAMPLE_EXTENSION}\n"
1025 << ((m_useSampleQualifier) ? ("sample ") : ("")) << "in highp vec4 v_position;\n"
1026 "layout(location = 0) out mediump vec4 fragColor;\n"
1027 "void main (void)\n"
1029 " const highp float maxDistance = 0.15625; // 4 subpixel bits. Assume 3 accurate bits + 0.03125 for other errors\n" // 0.03125 = mediump epsilon when value = 32 (RENDER_SIZE)
1031 " highp vec2 screenSpacePosition = (v_position.xy + vec2(1.0, 1.0)) / 2.0 * " << (int)RENDER_SIZE << ".0;\n"
1032 " highp ivec2 nearbyPixel = ivec2(floor(screenSpacePosition));\n"
1033 " bool allOk = false;\n"
1035 " // sample at edge + inaccuaries may cause us to round to any neighboring pixel\n"
1036 " // check all neighbors for any match\n"
1037 " for (highp int dy = -1; dy <= 1; ++dy)\n"
1038 " for (highp int dx = -1; dx <= 1; ++dx)\n"
1040 " highp ivec2 currentPixel = nearbyPixel + ivec2(dx, dy);\n"
1041 " highp vec2 candidateSamplingPos = vec2(currentPixel) + gl_SamplePosition.xy;\n"
1042 " highp vec2 positionDiff = abs(candidateSamplingPos - screenSpacePosition);\n"
1043 " if (positionDiff.x < maxDistance && positionDiff.y < maxDistance)\n"
1048 " fragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
1050 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
1053 return tcu::StringTemplate(buf.str()).specialize(args);
1056 bool SamplePosCorrectnessCase::verifyImage (const tcu::Surface& resultImage)
1058 return verifyImageWithVerifier(resultImage, m_testCtx.getLog(), NoRedVerifier());
1061 class SampleMaskBaseCase : public MultisampleRenderCase
1068 RUN_PER_TWO_SAMPLES,
1073 SampleMaskBaseCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target, int renderSize, ShaderRunMode runMode, int flags = 0);
1074 virtual ~SampleMaskBaseCase (void);
1077 virtual void init (void);
1078 virtual void preDraw (void);
1079 virtual void postDraw (void);
1080 virtual bool verifyImage (const tcu::Surface& resultImage);
1082 const ShaderRunMode m_runMode;
1085 SampleMaskBaseCase::SampleMaskBaseCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target, int renderSize, ShaderRunMode runMode, int flags)
1086 : MultisampleRenderCase (context, name, desc, sampleCount, target, renderSize, flags)
1087 , m_runMode (runMode)
1089 DE_ASSERT(runMode < RUN_LAST);
1092 SampleMaskBaseCase::~SampleMaskBaseCase (void)
1096 void SampleMaskBaseCase::init (void)
1098 const bool supportsES32orGL45 = glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
1099 contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
1100 // required extra extension
1101 if (m_runMode == RUN_PER_TWO_SAMPLES && !supportsES32orGL45 && !m_context.getContextInfo().isExtensionSupported("GL_OES_sample_shading"))
1102 TCU_THROW(NotSupportedError, "Test requires GL_OES_sample_shading extension or a context version 3.2 or higher.");
1104 MultisampleRenderCase::init();
1107 void SampleMaskBaseCase::preDraw (void)
1109 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1111 if (m_runMode == RUN_PER_TWO_SAMPLES)
1113 gl.enable(GL_SAMPLE_SHADING);
1114 gl.minSampleShading(0.5f);
1115 GLU_EXPECT_NO_ERROR(gl.getError(), "enable sample shading");
1117 m_testCtx.getLog() << tcu::TestLog::Message << "Enabled GL_SAMPLE_SHADING, value = 0.5" << tcu::TestLog::EndMessage;
1121 void SampleMaskBaseCase::postDraw (void)
1123 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1125 if (m_runMode == RUN_PER_TWO_SAMPLES)
1127 gl.disable(GL_SAMPLE_SHADING);
1128 gl.minSampleShading(1.0f);
1129 GLU_EXPECT_NO_ERROR(gl.getError(), "disable sample shading");
1133 bool SampleMaskBaseCase::verifyImage (const tcu::Surface& resultImage)
1135 // shader does the verification
1136 return verifyImageWithVerifier(resultImage, m_testCtx.getLog(), NoRedVerifier());
1139 class SampleMaskCase : public SampleMaskBaseCase
1142 SampleMaskCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target);
1143 ~SampleMaskCase (void);
1146 void preDraw (void);
1147 void postDraw (void);
1155 std::string genFragmentSource (int numTargetSamples) const;
1158 SampleMaskCase::SampleMaskCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target)
1159 : SampleMaskBaseCase(context, name, desc, sampleCount, target, RENDER_SIZE, RUN_PER_PIXEL)
1163 SampleMaskCase::~SampleMaskCase (void)
1167 void SampleMaskCase::init (void)
1169 // log the test method and expectations
1171 << tcu::TestLog::Message
1172 << "Verifying gl_SampleMaskIn value with SAMPLE_MASK state. gl_SampleMaskIn does not contain any bits set that are have been killed by SAMPLE_MASK state. Expecting:\n"
1173 << " 1) With multisample targets: gl_SampleMaskIn AND ~(SAMPLE_MASK) should be zero.\n"
1174 << " 2) With non-multisample targets: SAMPLE_MASK state is only ANDed as a multisample operation. gl_SampleMaskIn should only have its last bit set regardless of SAMPLE_MASK state.\n"
1175 << tcu::TestLog::EndMessage;
1177 SampleMaskBaseCase::init();
1180 void SampleMaskCase::preDraw (void)
1182 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1183 const bool multisampleTarget = (m_numRequestedSamples > 0) || (m_renderTarget == TARGET_DEFAULT && m_context.getRenderTarget().getNumSamples() > 1);
1184 const deUint32 fullMask = (deUint32)0xAAAAAAAAUL;
1185 const deUint32 maskMask = (1U << m_numTargetSamples) - 1;
1186 const deUint32 effectiveMask = fullMask & maskMask;
1189 gl.enable(GL_SAMPLE_MASK);
1190 gl.sampleMaski(0, effectiveMask);
1191 GLU_EXPECT_NO_ERROR(gl.getError(), "set mask");
1193 m_testCtx.getLog() << tcu::TestLog::Message << "Setting sample mask " << tcu::Format::Hex<4>(effectiveMask) << tcu::TestLog::EndMessage;
1195 // set multisample case uniforms
1196 if (multisampleTarget)
1198 const int maskLoc = gl.getUniformLocation(m_program->getProgram(), "u_sampleMask");
1200 throw tcu::TestError("Location of u_mask was -1");
1202 gl.uniform1ui(maskLoc, effectiveMask);
1203 GLU_EXPECT_NO_ERROR(gl.getError(), "set mask uniform");
1207 SampleMaskBaseCase::preDraw();
1210 void SampleMaskCase::postDraw (void)
1212 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1213 const deUint32 fullMask = (1U << m_numTargetSamples) - 1;
1215 gl.disable(GL_SAMPLE_MASK);
1216 gl.sampleMaski(0, fullMask);
1217 GLU_EXPECT_NO_ERROR(gl.getError(), "set mask");
1220 SampleMaskBaseCase::postDraw();
1223 std::string SampleMaskCase::genFragmentSource (int numTargetSamples) const
1225 DE_ASSERT(numTargetSamples != 0);
1227 const bool multisampleTarget = (m_numRequestedSamples > 0) || (m_renderTarget == TARGET_DEFAULT && m_context.getRenderTarget().getNumSamples() > 1);
1228 std::ostringstream buf;
1229 const bool supportsES32orGL45 = glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
1230 contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
1231 map<string, string> args;
1232 args["GLSL_VERSION_DECL"] = supportsES32orGL45 ? getGLSLVersionDeclaration(glu::GLSL_VERSION_320_ES) : getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
1233 args["GLSL_EXTENSION"] = supportsES32orGL45 ? "" : "#extension GL_OES_sample_variables : require";
1235 // test supports only one sample mask word
1236 if (numTargetSamples > 32)
1237 TCU_THROW(NotSupportedError, "Sample count larger than 32 is not supported.");
1239 if (multisampleTarget)
1241 buf << "${GLSL_VERSION_DECL}\n"
1242 "${GLSL_EXTENSION}\n"
1243 "layout(location = 0) out mediump vec4 fragColor;\n"
1244 "uniform highp uint u_sampleMask;\n"
1245 "void main (void)\n"
1247 " if ((uint(gl_SampleMaskIn[0]) & (~u_sampleMask)) != 0u)\n"
1248 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
1250 " fragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
1255 // non-multisample targets don't get multisample operations like ANDing with mask
1257 buf << "${GLSL_VERSION_DECL}\n"
1258 "${GLSL_EXTENSION}\n"
1259 "layout(location = 0) out mediump vec4 fragColor;\n"
1260 "uniform highp uint u_sampleMask;\n"
1261 "void main (void)\n"
1263 " if (gl_SampleMaskIn[0] != 1)\n"
1264 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
1266 " fragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
1270 return tcu::StringTemplate(buf.str()).specialize(args);
1273 class SampleMaskCountCase : public SampleMaskBaseCase
1276 SampleMaskCountCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target, ShaderRunMode runMode);
1277 ~SampleMaskCountCase (void);
1280 void preDraw (void);
1281 void postDraw (void);
1289 std::string genFragmentSource (int numTargetSamples) const;
1292 SampleMaskCountCase::SampleMaskCountCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target, ShaderRunMode runMode)
1293 : SampleMaskBaseCase(context, name, desc, sampleCount, target, RENDER_SIZE, runMode)
1295 DE_ASSERT(runMode < RUN_LAST);
1298 SampleMaskCountCase::~SampleMaskCountCase (void)
1302 void SampleMaskCountCase::init (void)
1304 // log the test method and expectations
1305 if (m_runMode == RUN_PER_PIXEL)
1307 << tcu::TestLog::Message
1308 << "Verifying gl_SampleMaskIn.\n"
1309 << " Fragment shader may be invoked [1, numSamples] times.\n"
1310 << " => gl_SampleMaskIn should have the number of bits set in range [1, numSamples]\n"
1311 << tcu::TestLog::EndMessage;
1312 else if (m_runMode == RUN_PER_SAMPLE)
1314 << tcu::TestLog::Message
1315 << "Verifying gl_SampleMaskIn.\n"
1316 << " Fragment will be invoked numSamples times.\n"
1317 << " => gl_SampleMaskIn should have only one bit set.\n"
1318 << tcu::TestLog::EndMessage;
1319 else if (m_runMode == RUN_PER_TWO_SAMPLES)
1321 << tcu::TestLog::Message
1322 << "Verifying gl_SampleMaskIn.\n"
1323 << " Fragment shader may be invoked [ceil(numSamples/2), numSamples] times.\n"
1324 << " => gl_SampleMaskIn should have the number of bits set in range [1, numSamples - ceil(numSamples/2) + 1]:\n"
1325 << tcu::TestLog::EndMessage;
1329 SampleMaskBaseCase::init();
1332 void SampleMaskCountCase::preDraw (void)
1334 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1336 if (m_runMode == RUN_PER_PIXEL)
1338 const int maxLoc = gl.getUniformLocation(m_program->getProgram(), "u_maxBitCount");
1339 const int minLoc = gl.getUniformLocation(m_program->getProgram(), "u_minBitCount");
1340 const int minBitCount = 1;
1341 const int maxBitCount = m_numTargetSamples;
1344 throw tcu::TestError("Location of u_maxBitCount was -1");
1346 throw tcu::TestError("Location of u_minBitCount was -1");
1348 gl.uniform1i(minLoc, minBitCount);
1349 gl.uniform1i(maxLoc, maxBitCount);
1350 GLU_EXPECT_NO_ERROR(gl.getError(), "set limits");
1352 m_testCtx.getLog() << tcu::TestLog::Message << "Setting minBitCount = " << minBitCount << ", maxBitCount = " << maxBitCount << tcu::TestLog::EndMessage;
1354 else if (m_runMode == RUN_PER_TWO_SAMPLES)
1356 const int maxLoc = gl.getUniformLocation(m_program->getProgram(), "u_maxBitCount");
1357 const int minLoc = gl.getUniformLocation(m_program->getProgram(), "u_minBitCount");
1359 // Worst case: all but one shader invocations get one sample, one shader invocation the rest of the samples
1360 const int minInvocationCount = ((m_numTargetSamples + 1) / 2);
1361 const int minBitCount = 1;
1362 const int maxBitCount = m_numTargetSamples - ((minInvocationCount-1) * minBitCount);
1365 throw tcu::TestError("Location of u_maxBitCount was -1");
1367 throw tcu::TestError("Location of u_minBitCount was -1");
1369 gl.uniform1i(minLoc, minBitCount);
1370 gl.uniform1i(maxLoc, maxBitCount);
1371 GLU_EXPECT_NO_ERROR(gl.getError(), "set limits");
1373 m_testCtx.getLog() << tcu::TestLog::Message << "Setting minBitCount = " << minBitCount << ", maxBitCount = " << maxBitCount << tcu::TestLog::EndMessage;
1376 SampleMaskBaseCase::preDraw();
1379 void SampleMaskCountCase::postDraw (void)
1381 SampleMaskBaseCase::postDraw();
1384 std::string SampleMaskCountCase::genFragmentSource (int numTargetSamples) const
1386 DE_ASSERT(numTargetSamples != 0);
1388 std::ostringstream buf;
1389 const bool supportsES32orGL45 = glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
1390 contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
1391 map<string, string> args;
1392 args["GLSL_VERSION_DECL"] = supportsES32orGL45 ? getGLSLVersionDeclaration(glu::GLSL_VERSION_320_ES) : getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
1393 args["GLSL_EXTENSION"] = supportsES32orGL45 ? "" : "#extension GL_OES_sample_variables : require";
1395 // test supports only one sample mask word
1396 if (numTargetSamples > 32)
1397 TCU_THROW(NotSupportedError, "Sample count larger than 32 is not supported.");
1399 // count the number of the bits in gl_SampleMask
1401 buf << "${GLSL_VERSION_DECL}\n"
1402 "${GLSL_EXTENSION}\n"
1403 "layout(location = 0) out mediump vec4 fragColor;\n";
1405 if (m_runMode != RUN_PER_SAMPLE)
1406 buf << "uniform highp int u_minBitCount;\n"
1407 "uniform highp int u_maxBitCount;\n";
1409 buf << "void main (void)\n"
1411 " mediump int maskBitCount = 0;\n"
1412 " for (int i = 0; i < 32; ++i)\n"
1413 " if (((gl_SampleMaskIn[0] >> i) & 0x01) == 0x01)\n"
1414 " ++maskBitCount;\n"
1417 if (m_runMode == RUN_PER_SAMPLE)
1419 // check the validity here
1420 buf << " // force per-sample shading\n"
1421 " highp float blue = float(gl_SampleID);\n"
1423 " if (maskBitCount != 1)\n"
1424 " fragColor = vec4(1.0, 0.0, blue, 1.0);\n"
1426 " fragColor = vec4(0.0, 1.0, blue, 1.0);\n"
1431 // check the validity here
1432 buf << " if (maskBitCount < u_minBitCount || maskBitCount > u_maxBitCount)\n"
1433 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
1435 " fragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
1439 return tcu::StringTemplate(buf.str()).specialize(args);
1442 class SampleMaskUniqueCase : public SampleMaskBaseCase
1445 SampleMaskUniqueCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target, ShaderRunMode runMode);
1446 ~SampleMaskUniqueCase (void);
1456 std::string genFragmentSource (int numTargetSamples) const;
1457 bool verifySampleBuffers (const std::vector<tcu::Surface>& resultBuffers);
1460 SampleMaskUniqueCase::SampleMaskUniqueCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target, ShaderRunMode runMode)
1461 : SampleMaskBaseCase(context, name, desc, sampleCount, target, RENDER_SIZE, runMode, MultisampleShaderRenderUtil::MultisampleRenderCase::FLAG_VERIFY_MSAA_TEXTURE_SAMPLE_BUFFERS)
1463 DE_ASSERT(runMode == RUN_PER_SAMPLE);
1464 DE_ASSERT(target == TARGET_TEXTURE);
1467 SampleMaskUniqueCase::~SampleMaskUniqueCase (void)
1471 void SampleMaskUniqueCase::init (void)
1473 // log the test method and expectations
1475 << tcu::TestLog::Message
1476 << "Verifying gl_SampleMaskIn.\n"
1477 << " Fragment will be invoked numSamples times.\n"
1478 << " => gl_SampleMaskIn should have only one bit set\n"
1479 << " => and that bit index should be unique within other fragment shader invocations of that pixel.\n"
1480 << " Writing sampleMask bit index to green channel in render shader. Verifying uniqueness in sampler shader.\n"
1481 << tcu::TestLog::EndMessage;
1483 SampleMaskBaseCase::init();
1486 std::string SampleMaskUniqueCase::genFragmentSource (int numTargetSamples) const
1488 DE_ASSERT(numTargetSamples != 0);
1490 std::ostringstream buf;
1491 const bool supportsES32orGL45 = glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
1492 contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
1493 map<string, string> args;
1494 args["GLSL_VERSION_DECL"] = supportsES32orGL45 ? getGLSLVersionDeclaration(glu::GLSL_VERSION_320_ES) : getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
1495 args["GLSL_EXTENSION"] = supportsES32orGL45 ? "" : "#extension GL_OES_sample_variables : require";
1497 // test supports only one sample mask word
1498 if (numTargetSamples > 32)
1499 TCU_THROW(NotSupportedError, "Sample count larger than 32 is not supported.");
1501 // find our sampleID by searching for unique bit.
1502 buf << "${GLSL_VERSION_DECL}\n"
1503 "${GLSL_EXTENSION}\n"
1504 "layout(location = 0) out mediump vec4 fragColor;\n"
1505 "void main (void)\n"
1507 " mediump int firstIndex = -1;\n"
1508 " for (int i = 0; i < 32; ++i)\n"
1510 " if (((gl_SampleMaskIn[0] >> i) & 0x01) == 0x01)\n"
1512 " firstIndex = i;\n"
1517 " bool notUniqueError = false;\n"
1518 " for (int i = firstIndex + 1; i < 32; ++i)\n"
1519 " if (((gl_SampleMaskIn[0] >> i) & 0x01) == 0x01)\n"
1520 " notUniqueError = true;\n"
1522 " highp float encodedSampleId = float(firstIndex) / " << numTargetSamples <<".0;\n"
1524 " // force per-sample shading\n"
1525 " highp float blue = float(gl_SampleID);\n"
1527 " if (notUniqueError)\n"
1528 " fragColor = vec4(1.0, 0.0, blue, 1.0);\n"
1530 " fragColor = vec4(0.0, encodedSampleId, blue, 1.0);\n"
1533 return tcu::StringTemplate(buf.str()).specialize(args);
1536 bool SampleMaskUniqueCase::verifySampleBuffers (const std::vector<tcu::Surface>& resultBuffers)
1538 const int width = resultBuffers[0].getWidth();
1539 const int height = resultBuffers[0].getHeight();
1544 m_testCtx.getLog() << tcu::TestLog::ImageSet("SampleBuffers", "Image sample buffers");
1545 for (int sampleNdx = 0; sampleNdx < (int)resultBuffers.size(); ++sampleNdx)
1546 m_testCtx.getLog() << tcu::TestLog::Image("Buffer" + de::toString(sampleNdx), "Sample " + de::toString(sampleNdx), resultBuffers[sampleNdx].getAccess());
1547 m_testCtx.getLog() << tcu::TestLog::EndImageSet;
1550 // check for earlier errors (in fragment shader)
1552 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying fragment shader invocation found only one set sample mask bit." << tcu::TestLog::EndMessage;
1554 for (int sampleNdx = 0; sampleNdx < (int)resultBuffers.size(); ++sampleNdx)
1556 // shader does the check, just check the shader error output (red)
1557 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying sample " << (sampleNdx+1) << "/" << (int)resultBuffers.size() << tcu::TestLog::EndMessage;
1558 allOk &= verifyImageWithVerifier(resultBuffers[sampleNdx], m_testCtx.getLog(), NoRedVerifier(), false);
1563 // can't check the uniqueness if the masks don't work at all
1564 m_testCtx.getLog() << tcu::TestLog::Message << "Could not get mask information from the rendered image, cannot continue verification." << tcu::TestLog::EndMessage;
1569 // verify index / index ranges
1571 if (m_numRequestedSamples == 0)
1573 // single sample target, expect index=0
1575 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying sample mask bit index is 0." << tcu::TestLog::EndMessage;
1577 // only check the mask index
1578 allOk &= verifyImageWithVerifier(resultBuffers[0], m_testCtx.getLog(), ColorVerifier(tcu::Vec3(0.0f, 0.0f, 0.0f), tcu::IVec3(255, 8, 255)), false);
1584 tcu::Surface errorMask (width, height);
1585 bool uniquenessOk = true;
1587 const int printFloodLimit = 5;
1588 std::vector<int> maskBitIndices (resultBuffers.size());
1590 tcu::clear(errorMask.getAccess(), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
1592 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying per-invocation sample mask bit is unique." << tcu::TestLog::EndMessage;
1594 for (int y = 0; y < height; ++y)
1595 for (int x = 0; x < width; ++x)
1597 bool maskNdxNotUnique = false;
1600 for (int sampleNdx = 0; sampleNdx < (int)resultBuffers.size(); ++sampleNdx)
1602 const tcu::RGBA color = resultBuffers[sampleNdx].getPixel(x, y);
1603 maskBitIndices[sampleNdx] = (int)deFloatRound((float)color.getGreen() / 255.0f * (float)m_numTargetSamples);
1606 // just check there are no two invocations with the same bit index
1607 for (int sampleNdxA = 0; sampleNdxA < (int)resultBuffers.size() && (!maskNdxNotUnique || printCount < printFloodLimit); ++sampleNdxA)
1608 for (int sampleNdxB = sampleNdxA+1; sampleNdxB < (int)resultBuffers.size() && (!maskNdxNotUnique || printCount < printFloodLimit); ++sampleNdxB)
1610 if (maskBitIndices[sampleNdxA] == maskBitIndices[sampleNdxB])
1612 if (++printCount <= printFloodLimit)
1615 << tcu::TestLog::Message
1616 << "Pixel (" << x << ", " << y << "): Samples " << sampleNdxA << " and " << sampleNdxB << " have the same sample mask. (Single bit at index " << maskBitIndices[sampleNdxA] << ")"
1617 << tcu::TestLog::EndMessage;
1620 maskNdxNotUnique = true;
1621 uniquenessOk = false;
1622 errorMask.setPixel(x, y, tcu::RGBA::red());
1630 if (printCount > printFloodLimit)
1632 << tcu::TestLog::Message
1634 << "Omitted " << (printCount-printFloodLimit) << " error descriptions."
1635 << tcu::TestLog::EndMessage;
1638 << tcu::TestLog::Message << "Image verification failed." << tcu::TestLog::EndMessage
1639 << tcu::TestLog::ImageSet("Verification", "Image Verification")
1640 << tcu::TestLog::Image("ErrorMask", "Error Mask", errorMask.getAccess())
1641 << tcu::TestLog::EndImageSet;
1650 class SampleMaskUniqueSetCase : public SampleMaskBaseCase
1653 SampleMaskUniqueSetCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target, ShaderRunMode runMode);
1654 ~SampleMaskUniqueSetCase (void);
1665 void preDraw (void);
1666 void postDraw (void);
1667 std::string genFragmentSource (int numTargetSamples) const;
1668 bool verifySampleBuffers (const std::vector<tcu::Surface>& resultBuffers);
1669 std::string getIterationDescription (int iteration) const;
1671 void preTest (void);
1672 void postTest (void);
1674 std::vector<tcu::Surface> m_iterationSampleBuffers;
1677 SampleMaskUniqueSetCase::SampleMaskUniqueSetCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target, ShaderRunMode runMode)
1678 : SampleMaskBaseCase(context, name, desc, sampleCount, target, RENDER_SIZE, runMode, MultisampleShaderRenderUtil::MultisampleRenderCase::FLAG_VERIFY_MSAA_TEXTURE_SAMPLE_BUFFERS)
1680 DE_ASSERT(runMode == RUN_PER_TWO_SAMPLES);
1681 DE_ASSERT(target == TARGET_TEXTURE);
1683 // high and low bits
1684 m_numIterations = 2;
1687 SampleMaskUniqueSetCase::~SampleMaskUniqueSetCase (void)
1691 void SampleMaskUniqueSetCase::init (void)
1693 // log the test method and expectations
1695 << tcu::TestLog::Message
1696 << "Verifying gl_SampleMaskIn.\n"
1697 << " Fragment shader may be invoked [ceil(numSamples/2), numSamples] times.\n"
1698 << " => Each invocation should have unique bit set\n"
1699 << " Writing highest and lowest bit index to color channels in render shader. Verifying:\n"
1700 << " 1) no other invocation contains these bits in sampler shader.\n"
1701 << " 2) number of invocations is at least ceil(numSamples/2).\n"
1702 << tcu::TestLog::EndMessage;
1704 SampleMaskBaseCase::init();
1707 void SampleMaskUniqueSetCase::deinit (void)
1709 m_iterationSampleBuffers.clear();
1712 void SampleMaskUniqueSetCase::preDraw (void)
1714 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
1715 const int selectorLoc = gl.getUniformLocation(m_program->getProgram(), "u_bitSelector");
1717 gl.uniform1ui(selectorLoc, (deUint32)m_iteration);
1718 GLU_EXPECT_NO_ERROR(gl.getError(), "set u_bitSelector");
1720 m_testCtx.getLog() << tcu::TestLog::Message << "Setting u_bitSelector = " << m_iteration << tcu::TestLog::EndMessage;
1722 SampleMaskBaseCase::preDraw();
1725 void SampleMaskUniqueSetCase::postDraw (void)
1727 SampleMaskBaseCase::postDraw();
1730 std::string SampleMaskUniqueSetCase::genFragmentSource (int numTargetSamples) const
1732 DE_ASSERT(numTargetSamples != 0);
1734 std::ostringstream buf;
1735 const bool supportsES32orGL45 = glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
1736 contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
1737 map<string, string> args;
1738 args["GLSL_VERSION_DECL"] = supportsES32orGL45 ? getGLSLVersionDeclaration(glu::GLSL_VERSION_320_ES) : getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
1739 args["GLSL_EXTENSION"] = supportsES32orGL45 ? "" : "#extension GL_OES_sample_variables : require";
1741 // test supports only one sample mask word
1742 if (numTargetSamples > 32)
1743 TCU_THROW(NotSupportedError, "Sample count larger than 32 is not supported.");
1745 // output min and max sample id
1746 buf << "${GLSL_VERSION_DECL}\n"
1747 "${GLSL_EXTENSION}\n"
1748 "uniform highp uint u_bitSelector;\n"
1749 "layout(location = 0) out mediump vec4 fragColor;\n"
1750 "void main (void)\n"
1752 " highp int selectedBits;\n"
1753 " if (u_bitSelector == 0u)\n"
1754 " selectedBits = (gl_SampleMaskIn[0] & 0xFFFF);\n"
1756 " selectedBits = ((gl_SampleMaskIn[0] >> 16) & 0xFFFF);\n"
1758 " // encode bits to color\n"
1759 " highp int redBits = selectedBits & 31;\n"
1760 " highp int greenBits = (selectedBits >> 5) & 63;\n"
1761 " highp int blueBits = (selectedBits >> 11) & 31;\n"
1763 " fragColor = vec4(float(redBits) / float(31), float(greenBits) / float(63), float(blueBits) / float(31), 1.0);\n"
1766 return tcu::StringTemplate(buf.str()).specialize(args);
1769 bool SampleMaskUniqueSetCase::verifySampleBuffers (const std::vector<tcu::Surface>& resultBuffers)
1771 // we need results from all passes to do verification. Store results and verify later (at postTest).
1773 DE_ASSERT(m_numTargetSamples == (int)resultBuffers.size());
1774 for (int ndx = 0; ndx < m_numTargetSamples; ++ndx)
1775 m_iterationSampleBuffers[m_iteration * m_numTargetSamples + ndx] = resultBuffers[ndx];
1780 std::string SampleMaskUniqueSetCase::getIterationDescription (int iteration) const
1783 return "Reading low bits";
1784 else if (iteration == 1)
1785 return "Reading high bits";
1791 void SampleMaskUniqueSetCase::preTest (void)
1793 m_iterationSampleBuffers.resize(m_numTargetSamples * 2);
1796 void SampleMaskUniqueSetCase::postTest (void)
1798 DE_ASSERT((m_iterationSampleBuffers.size() % 2) == 0);
1799 DE_ASSERT((int)m_iterationSampleBuffers.size() / 2 == m_numTargetSamples);
1801 const int width = m_iterationSampleBuffers[0].getWidth();
1802 const int height = m_iterationSampleBuffers[0].getHeight();
1804 std::vector<tcu::TextureLevel> sampleCoverage (m_numTargetSamples);
1805 const tcu::ScopedLogSection section (m_testCtx.getLog(), "Verify", "Verify masks");
1807 // convert color layers to 32 bit coverage masks, 2 passes per coverage
1809 for (int sampleNdx = 0; sampleNdx < (int)sampleCoverage.size(); ++sampleNdx)
1811 sampleCoverage[sampleNdx].setStorage(tcu::TextureFormat(tcu::TextureFormat::R, tcu::TextureFormat::UNSIGNED_INT32), width, height);
1813 for (int y = 0; y < height; ++y)
1814 for (int x = 0; x < width; ++x)
1816 const tcu::RGBA lowColor = m_iterationSampleBuffers[sampleNdx].getPixel(x, y);
1817 const tcu::RGBA highColor = m_iterationSampleBuffers[sampleNdx + (int)sampleCoverage.size()].getPixel(x, y);
1822 int redBits = (int)deFloatRound((float)lowColor.getRed() / 255.0f * 31);
1823 int greenBits = (int)deFloatRound((float)lowColor.getGreen() / 255.0f * 63);
1824 int blueBits = (int)deFloatRound((float)lowColor.getBlue() / 255.0f * 31);
1826 low = (deUint16)(redBits | (greenBits << 5) | (blueBits << 11));
1829 int redBits = (int)deFloatRound((float)highColor.getRed() / 255.0f * 31);
1830 int greenBits = (int)deFloatRound((float)highColor.getGreen() / 255.0f * 63);
1831 int blueBits = (int)deFloatRound((float)highColor.getBlue() / 255.0f * 31);
1833 high = (deUint16)(redBits | (greenBits << 5) | (blueBits << 11));
1836 sampleCoverage[sampleNdx].getAccess().setPixel(tcu::UVec4((((deUint32)high) << 16) | low, 0, 0, 0), x, y);
1842 if (m_numRequestedSamples == 0)
1844 // single sample target, expect mask = 0x01
1845 const int printFloodLimit = 5;
1848 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying sample mask is 0x00000001." << tcu::TestLog::EndMessage;
1850 for (int y = 0; y < height; ++y)
1851 for (int x = 0; x < width; ++x)
1853 deUint32 mask = sampleCoverage[0].getAccess().getPixelUint(x, y).x();
1858 if (++printCount <= printFloodLimit)
1861 << tcu::TestLog::Message
1862 << "Pixel (" << x << ", " << y << "): Invalid mask, got " << tcu::Format::Hex<8>(mask) << ", expected " << tcu::Format::Hex<8>(0x01) << "\n"
1863 << tcu::TestLog::EndMessage;
1868 if (!allOk && printCount > printFloodLimit)
1871 << tcu::TestLog::Message
1873 << "Omitted " << (printCount-printFloodLimit) << " error descriptions."
1874 << tcu::TestLog::EndMessage;
1881 bool uniquenessOk = true;
1883 const int printFloodLimit = 5;
1885 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying invocation sample masks do not share bits." << tcu::TestLog::EndMessage;
1887 for (int y = 0; y < height; ++y)
1888 for (int x = 0; x < width; ++x)
1890 bool maskBitsNotUnique = false;
1892 for (int sampleNdxA = 0; sampleNdxA < m_numTargetSamples && (!maskBitsNotUnique || printCount < printFloodLimit); ++sampleNdxA)
1893 for (int sampleNdxB = sampleNdxA+1; sampleNdxB < m_numTargetSamples && (!maskBitsNotUnique || printCount < printFloodLimit); ++sampleNdxB)
1895 const deUint32 maskA = sampleCoverage[sampleNdxA].getAccess().getPixelUint(x, y).x();
1896 const deUint32 maskB = sampleCoverage[sampleNdxB].getAccess().getPixelUint(x, y).x();
1898 // equal mask == emitted by the same invocation
1904 maskBitsNotUnique = true;
1905 uniquenessOk = false;
1907 if (++printCount <= printFloodLimit)
1910 << tcu::TestLog::Message
1911 << "Pixel (" << x << ", " << y << "):\n"
1912 << "\tSamples " << sampleNdxA << " and " << sampleNdxB << " share mask bits\n"
1913 << "\tMask" << sampleNdxA << " = " << tcu::Format::Hex<8>(maskA) << "\n"
1914 << "\tMask" << sampleNdxB << " = " << tcu::Format::Hex<8>(maskB) << "\n"
1915 << tcu::TestLog::EndMessage;
1926 if (printCount > printFloodLimit)
1928 << tcu::TestLog::Message
1930 << "Omitted " << (printCount-printFloodLimit) << " error descriptions."
1931 << tcu::TestLog::EndMessage;
1935 // check number of sample mask bit groups is valid ( == number of invocations )
1937 const deUint32 minNumInvocations = (deUint32)de::max(1, (m_numTargetSamples+1)/2);
1938 bool countOk = true;
1940 const int printFloodLimit = 5;
1942 m_testCtx.getLog() << tcu::TestLog::Message << "Verifying cardinality of separate sample mask bit sets. Expecting equal to the number of invocations, (greater or equal to " << minNumInvocations << ")" << tcu::TestLog::EndMessage;
1944 for (int y = 0; y < height; ++y)
1945 for (int x = 0; x < width; ++x)
1947 std::set<deUint32> masks;
1949 for (int maskNdx = 0; maskNdx < m_numTargetSamples; ++maskNdx)
1951 const deUint32 mask = sampleCoverage[maskNdx].getAccess().getPixelUint(x, y).x();
1955 if ((int)masks.size() < (int)minNumInvocations)
1957 if (++printCount <= printFloodLimit)
1960 << tcu::TestLog::Message
1961 << "Pixel (" << x << ", " << y << "): Pixel invocations had only " << (int)masks.size() << " separate mask sets. Expected " << minNumInvocations << " or more. Found masks:"
1962 << tcu::TestLog::EndMessage;
1964 for (std::set<deUint32>::iterator it = masks.begin(); it != masks.end(); ++it)
1966 << tcu::TestLog::Message
1967 << "\tMask: " << tcu::Format::Hex<8>(*it) << "\n"
1968 << tcu::TestLog::EndMessage;
1979 if (printCount > printFloodLimit)
1981 << tcu::TestLog::Message
1983 << "Omitted " << (printCount-printFloodLimit) << " error descriptions."
1984 << tcu::TestLog::EndMessage;
1990 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Image verification failed");
1993 class SampleMaskWriteCase : public SampleMaskBaseCase
2003 SampleMaskWriteCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target, ShaderRunMode runMode, TestMode testMode);
2004 ~SampleMaskWriteCase (void);
2007 void preDraw (void);
2008 void postDraw (void);
2016 std::string genFragmentSource (int numTargetSamples) const;
2017 bool verifyImage (const tcu::Surface& resultImage);
2019 const TestMode m_testMode;
2022 SampleMaskWriteCase::SampleMaskWriteCase (Context& context, const char* name, const char* desc, int sampleCount, RenderTarget target, ShaderRunMode runMode, TestMode testMode)
2023 : SampleMaskBaseCase (context, name, desc, sampleCount, target, RENDER_SIZE, runMode)
2024 , m_testMode (testMode)
2026 DE_ASSERT(testMode < TEST_LAST);
2029 SampleMaskWriteCase::~SampleMaskWriteCase (void)
2033 void SampleMaskWriteCase::init (void)
2035 // log the test method and expectations
2036 if (m_testMode == TEST_DISCARD)
2038 << tcu::TestLog::Message
2039 << "Discarding half of the samples using gl_SampleMask, expecting:\n"
2040 << " 1) half intensity on multisample targets (numSamples > 1)\n"
2041 << " 2) full discard on multisample targets (numSamples == 1)\n"
2042 << " 3) full intensity (no discard) on singlesample targets. (Mask is only applied as a multisample operation.)\n"
2043 << tcu::TestLog::EndMessage;
2044 else if (m_testMode == TEST_INVERSE)
2046 << tcu::TestLog::Message
2047 << "Discarding half of the samples using GL_SAMPLE_MASK, setting inverse mask in fragment shader using gl_SampleMask, expecting:\n"
2048 << " 1) full discard on multisample targets (mask & modifiedCoverge == 0)\n"
2049 << " 2) full intensity (no discard) on singlesample targets. (Mask and coverage is only applied as a multisample operation.)\n"
2050 << tcu::TestLog::EndMessage;
2054 SampleMaskBaseCase::init();
2057 void SampleMaskWriteCase::preDraw (void)
2059 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
2061 if (m_testMode == TEST_INVERSE)
2063 // set mask to 0xAAAA.., set inverse mask bit coverage in shader
2065 const int maskLoc = gl.getUniformLocation(m_program->getProgram(), "u_mask");
2066 const deUint32 mask = (deUint32)0xAAAAAAAAUL;
2069 throw tcu::TestError("Location of u_mask was -1");
2071 gl.enable(GL_SAMPLE_MASK);
2072 gl.sampleMaski(0, mask);
2073 GLU_EXPECT_NO_ERROR(gl.getError(), "set mask");
2075 gl.uniform1ui(maskLoc, mask);
2076 GLU_EXPECT_NO_ERROR(gl.getError(), "set mask uniform");
2078 m_testCtx.getLog() << tcu::TestLog::Message << "Setting sample mask " << tcu::Format::Hex<4>(mask) << tcu::TestLog::EndMessage;
2081 SampleMaskBaseCase::preDraw();
2084 void SampleMaskWriteCase::postDraw (void)
2086 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
2088 if (m_testMode == TEST_INVERSE)
2090 const deUint32 fullMask = (1U << m_numTargetSamples) - 1;
2092 gl.disable(GL_SAMPLE_MASK);
2093 gl.sampleMaski(0, fullMask);
2094 GLU_EXPECT_NO_ERROR(gl.getError(), "set mask");
2097 SampleMaskBaseCase::postDraw();
2100 std::string SampleMaskWriteCase::genFragmentSource (int numTargetSamples) const
2102 DE_ASSERT(numTargetSamples != 0);
2103 DE_UNREF(numTargetSamples);
2105 std::ostringstream buf;
2106 const bool supportsES32orGL45 = glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ||
2107 contextSupports(m_context.getRenderContext().getType(), glu::ApiType::core(4, 5));
2108 map<string, string> args;
2109 args["GLSL_VERSION_DECL"] = supportsES32orGL45 ? getGLSLVersionDeclaration(glu::GLSL_VERSION_320_ES) : getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
2110 args["GLSL_EXTENSION"] = supportsES32orGL45 ? "" : "#extension GL_OES_sample_variables : require";
2112 if (m_testMode == TEST_DISCARD)
2114 // mask out every other coverage bit
2116 buf << "${GLSL_VERSION_DECL}\n"
2117 "${GLSL_EXTENSION}\n"
2118 "layout(location = 0) out mediump vec4 fragColor;\n"
2119 "void main (void)\n"
2121 " for (int i = 0; i < gl_SampleMask.length(); ++i)\n"
2122 " gl_SampleMask[i] = int(0xAAAAAAAA);\n"
2125 if (m_runMode == RUN_PER_SAMPLE)
2126 buf << " // force per-sample shading\n"
2127 " highp float blue = float(gl_SampleID);\n"
2129 " fragColor = vec4(0.0, 1.0, blue, 1.0);\n"
2132 buf << " fragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
2135 else if (m_testMode == TEST_INVERSE)
2137 // inverse every coverage bit
2139 buf << "${GLSL_VERSION_DECL}\n"
2140 "${GLSL_EXTENSION}\n"
2141 "layout(location = 0) out mediump vec4 fragColor;\n"
2142 "uniform highp uint u_mask;\n"
2143 "void main (void)\n"
2145 " gl_SampleMask[0] = int(~u_mask);\n"
2148 if (m_runMode == RUN_PER_SAMPLE)
2149 buf << " // force per-sample shading\n"
2150 " highp float blue = float(gl_SampleID);\n"
2152 " fragColor = vec4(0.0, 1.0, blue, 1.0);\n"
2155 buf << " fragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
2161 return tcu::StringTemplate(buf.str()).specialize(args);
2164 bool SampleMaskWriteCase::verifyImage (const tcu::Surface& resultImage)
2166 const bool singleSampleTarget = m_numRequestedSamples == 0 && !(m_renderTarget == TARGET_DEFAULT && m_context.getRenderTarget().getNumSamples() > 1);
2168 if (m_testMode == TEST_DISCARD)
2170 if (singleSampleTarget)
2172 // single sample case => multisample operations are not effective => don't discard anything
2174 return verifyImageWithVerifier(resultImage, m_testCtx.getLog(), ColorVerifier(tcu::Vec3(0.0f, 1.0f, 0.0f)));
2176 else if (m_numTargetSamples == 1)
2178 // total discard, expect black
2179 return verifyImageWithVerifier(resultImage, m_testCtx.getLog(), ColorVerifier(tcu::Vec3(0.0f, 0.0f, 0.0f)));
2183 // partial discard, expect something between black and green
2184 return verifyImageWithVerifier(resultImage, m_testCtx.getLog(), PartialDiscardVerifier());
2187 else if (m_testMode == TEST_INVERSE)
2189 if (singleSampleTarget)
2191 // single sample case => multisample operations are not effective => don't discard anything
2193 return verifyImageWithVerifier(resultImage, m_testCtx.getLog(), ColorVerifier(tcu::Vec3(0.0f, 1.0f, 0.0f)));
2197 // total discard, expect black
2198 return verifyImageWithVerifier(resultImage, m_testCtx.getLog(), ColorVerifier(tcu::Vec3(0.0f, 0.0f, 0.0f)));
2210 SampleVariableTests::SampleVariableTests (Context& context)
2211 : TestCaseGroup(context, "sample_variables", "Test sample variables")
2215 SampleVariableTests::~SampleVariableTests (void)
2219 void SampleVariableTests::init (void)
2221 tcu::TestCaseGroup* const numSampleGroup = new tcu::TestCaseGroup(m_testCtx, "num_samples", "Test NumSamples");
2222 tcu::TestCaseGroup* const maxSampleGroup = new tcu::TestCaseGroup(m_testCtx, "max_samples", "Test MaxSamples");
2223 tcu::TestCaseGroup* const sampleIDGroup = new tcu::TestCaseGroup(m_testCtx, "sample_id", "Test SampleID");
2224 tcu::TestCaseGroup* const samplePosGroup = new tcu::TestCaseGroup(m_testCtx, "sample_pos", "Test SamplePosition");
2225 tcu::TestCaseGroup* const sampleMaskInGroup = new tcu::TestCaseGroup(m_testCtx, "sample_mask_in", "Test SampleMaskIn");
2226 tcu::TestCaseGroup* const sampleMaskGroup = new tcu::TestCaseGroup(m_testCtx, "sample_mask", "Test SampleMask");
2228 addChild(numSampleGroup);
2229 addChild(maxSampleGroup);
2230 addChild(sampleIDGroup);
2231 addChild(samplePosGroup);
2232 addChild(sampleMaskInGroup);
2233 addChild(sampleMaskGroup);
2235 static const struct RenderTarget
2240 MultisampleRenderCase::RenderTarget target;
2243 { "default_framebuffer", "Test with default framebuffer", 0, MultisampleRenderCase::TARGET_DEFAULT },
2244 { "singlesample_texture", "Test with singlesample texture", 0, MultisampleRenderCase::TARGET_TEXTURE },
2245 { "multisample_texture_1", "Test with multisample texture", 1, MultisampleRenderCase::TARGET_TEXTURE },
2246 { "multisample_texture_2", "Test with multisample texture", 2, MultisampleRenderCase::TARGET_TEXTURE },
2247 { "multisample_texture_4", "Test with multisample texture", 4, MultisampleRenderCase::TARGET_TEXTURE },
2248 { "multisample_texture_8", "Test with multisample texture", 8, MultisampleRenderCase::TARGET_TEXTURE },
2249 { "multisample_texture_16", "Test with multisample texture", 16, MultisampleRenderCase::TARGET_TEXTURE },
2250 { "singlesample_rbo", "Test with singlesample rbo", 0, MultisampleRenderCase::TARGET_RENDERBUFFER },
2251 { "multisample_rbo_1", "Test with multisample rbo", 1, MultisampleRenderCase::TARGET_RENDERBUFFER },
2252 { "multisample_rbo_2", "Test with multisample rbo", 2, MultisampleRenderCase::TARGET_RENDERBUFFER },
2253 { "multisample_rbo_4", "Test with multisample rbo", 4, MultisampleRenderCase::TARGET_RENDERBUFFER },
2254 { "multisample_rbo_8", "Test with multisample rbo", 8, MultisampleRenderCase::TARGET_RENDERBUFFER },
2255 { "multisample_rbo_16", "Test with multisample rbo", 16, MultisampleRenderCase::TARGET_RENDERBUFFER },
2260 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2261 numSampleGroup->addChild(new NumSamplesCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target));
2266 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2267 maxSampleGroup->addChild(new MaxSamplesCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target));
2272 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2273 sampleIDGroup->addChild(new SampleIDCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target));
2279 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "correctness", "Test SamplePos correctness");
2280 samplePosGroup->addChild(group);
2282 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2283 group->addChild(new SamplePosCorrectnessCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target));
2287 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "distribution", "Test SamplePos distribution");
2288 samplePosGroup->addChild(group);
2290 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2291 group->addChild(new SamplePosDistributionCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target));
2299 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "sample_mask", "Test with GL_SAMPLE_MASK");
2300 sampleMaskInGroup->addChild(group);
2302 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2303 group->addChild(new SampleMaskCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target));
2305 // .bit_count_per_pixel
2307 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "bit_count_per_pixel", "Test number of coverage bits");
2308 sampleMaskInGroup->addChild(group);
2310 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2311 group->addChild(new SampleMaskCountCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target, SampleMaskCountCase::RUN_PER_PIXEL));
2313 // .bit_count_per_sample
2315 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "bit_count_per_sample", "Test number of coverage bits");
2316 sampleMaskInGroup->addChild(group);
2318 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2319 group->addChild(new SampleMaskCountCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target, SampleMaskCountCase::RUN_PER_SAMPLE));
2321 // .bit_count_per_two_samples
2323 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "bit_count_per_two_samples", "Test number of coverage bits");
2324 sampleMaskInGroup->addChild(group);
2326 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2327 group->addChild(new SampleMaskCountCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target, SampleMaskCountCase::RUN_PER_TWO_SAMPLES));
2329 // .bits_unique_per_sample
2331 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "bits_unique_per_sample", "Test coverage bits");
2332 sampleMaskInGroup->addChild(group);
2334 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2335 if (targets[targetNdx].target == MultisampleRenderCase::TARGET_TEXTURE)
2336 group->addChild(new SampleMaskUniqueCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target, SampleMaskUniqueCase::RUN_PER_SAMPLE));
2338 // .bits_unique_per_two_samples
2340 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "bits_unique_per_two_samples", "Test coverage bits");
2341 sampleMaskInGroup->addChild(group);
2343 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2344 if (targets[targetNdx].target == MultisampleRenderCase::TARGET_TEXTURE)
2345 group->addChild(new SampleMaskUniqueSetCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target, SampleMaskUniqueCase::RUN_PER_TWO_SAMPLES));
2351 // .discard_half_per_pixel
2353 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "discard_half_per_pixel", "Test coverage bits");
2354 sampleMaskGroup->addChild(group);
2356 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2357 group->addChild(new SampleMaskWriteCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target, SampleMaskWriteCase::RUN_PER_PIXEL, SampleMaskWriteCase::TEST_DISCARD));
2359 // .discard_half_per_sample
2361 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "discard_half_per_sample", "Test coverage bits");
2362 sampleMaskGroup->addChild(group);
2364 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2365 group->addChild(new SampleMaskWriteCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target, SampleMaskWriteCase::RUN_PER_SAMPLE, SampleMaskWriteCase::TEST_DISCARD));
2367 // .discard_half_per_two_samples
2369 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "discard_half_per_two_samples", "Test coverage bits");
2370 sampleMaskGroup->addChild(group);
2372 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2373 group->addChild(new SampleMaskWriteCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target, SampleMaskWriteCase::RUN_PER_TWO_SAMPLES, SampleMaskWriteCase::TEST_DISCARD));
2376 // .discard_half_per_two_samples
2378 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "inverse_per_pixel", "Test coverage bits");
2379 sampleMaskGroup->addChild(group);
2381 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2382 group->addChild(new SampleMaskWriteCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target, SampleMaskWriteCase::RUN_PER_PIXEL, SampleMaskWriteCase::TEST_INVERSE));
2384 // .inverse_per_sample
2386 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "inverse_per_sample", "Test coverage bits");
2387 sampleMaskGroup->addChild(group);
2389 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2390 group->addChild(new SampleMaskWriteCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target, SampleMaskWriteCase::RUN_PER_SAMPLE, SampleMaskWriteCase::TEST_INVERSE));
2392 // .inverse_per_two_samples
2394 tcu::TestCaseGroup* const group = new tcu::TestCaseGroup(m_testCtx, "inverse_per_two_samples", "Test coverage bits");
2395 sampleMaskGroup->addChild(group);
2397 for (int targetNdx = 0; targetNdx < DE_LENGTH_OF_ARRAY(targets); ++targetNdx)
2398 group->addChild(new SampleMaskWriteCase(m_context, targets[targetNdx].name, targets[targetNdx].desc, targets[targetNdx].numSamples, targets[targetNdx].target, SampleMaskWriteCase::RUN_PER_TWO_SAMPLES, SampleMaskWriteCase::TEST_INVERSE));