1 /*-------------------------------------------------------------------------
2 * OpenGL Conformance Test Suite
3 * -----------------------------
5 * Copyright (c) 2014-2016 The Khronos Group Inc.
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.
22 */ /*-------------------------------------------------------------------*/
24 #include "glcSampleVariablesTests.hpp"
26 #include "deRandom.hpp"
27 #include "deStringUtil.hpp"
28 #include "gluContextInfo.hpp"
29 #include "gluDrawUtil.hpp"
30 #include "gluPixelTransfer.hpp"
31 #include "gluShaderProgram.hpp"
33 #include "glwFunctions.hpp"
34 #include "tcuCommandLine.hpp"
35 #include "tcuStringTemplate.hpp"
36 #include "tcuSurface.hpp"
37 #include "tcuTestLog.hpp"
41 static bool operator<(tcu::Vec4 const& k1, tcu::Vec4 const& k2)
47 else if (k1.y() == k2.y())
49 return k1.x() < k2.x();
65 static std::string specializeVersion(std::string const& source, glu::GLSLVersion version,
66 std::string const& sampler = "", std::string const& outType = "")
68 DE_ASSERT(version == glu::GLSL_VERSION_310_ES || version >= glu::GLSL_VERSION_400);
69 std::map<std::string, std::string> args;
70 args["VERSION_DECL"] = glu::getGLSLVersionDeclaration(version);
71 args["SAMPLER"] = sampler;
72 args["OUT_TYPE"] = outType;
73 if (version == glu::GLSL_VERSION_310_ES)
75 args["OES_SV_RQ"] = "#extension GL_OES_sample_variables : require\n";
76 args["OES_SV_EN"] = "#extension GL_OES_sample_variables : enable\n";
80 args["OES_SV_RQ"] = "";
81 args["OES_SV_EN"] = "";
83 return tcu::StringTemplate(source.c_str()).specialize(args);
86 class SampleShadingExtensionCase : public TestCase
89 SampleShadingExtensionCase(Context& context, const char* name, const char* description,
90 glu::GLSLVersion glslVersion);
91 ~SampleShadingExtensionCase();
93 IterateResult iterate();
96 glu::GLSLVersion m_glslVersion;
99 SampleShadingExtensionCase::SampleShadingExtensionCase(Context& context, const char* name, const char* description,
100 glu::GLSLVersion glslVersion)
101 : TestCase(context, name, description), m_glslVersion(glslVersion)
103 DE_ASSERT(glslVersion == glu::GLSL_VERSION_310_ES);
106 SampleShadingExtensionCase::~SampleShadingExtensionCase()
110 SampleShadingExtensionCase::IterateResult SampleShadingExtensionCase::iterate()
112 TestLog& log = m_testCtx.getLog();
114 if (!m_context.getContextInfo().isExtensionSupported("GL_OES_sample_variables"))
116 m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED, "GL_OES_sample_variables");
120 static char const* vss = "${VERSION_DECL}\n"
121 "in highp vec4 a_position;\n"
124 " gl_Position = a_position;\n"
128 static char const* fss = "${VERSION_DECL}\n"
130 "out highp vec4 o_color;\n"
133 " for (int i = 0; i < (gl_MaxSamples + 31) / 32; ++i) {\n"
134 " gl_SampleMask[i] = gl_SampleMaskIn[i];\n"
136 " o_color = vec4(gl_SampleID, gl_SamplePosition.x, gl_SamplePosition.y, 1);\n"
139 glu::ShaderProgram programRequire(m_context.getRenderContext(),
140 glu::makeVtxFragSources(specializeVersion(vss, m_glslVersion).c_str(),
141 specializeVersion(fss, m_glslVersion).c_str()));
142 log << programRequire;
143 if (!programRequire.isOk())
145 TCU_FAIL("Compile failed");
150 static char const* fss = "${VERSION_DECL}\n"
152 "out highp vec4 o_color;\n"
155 "#if !GL_OES_sample_variables\n"
158 " for (int i = 0; i < (gl_MaxSamples + 31) / 32; ++i) {\n"
159 " gl_SampleMask[i] = gl_SampleMaskIn[i];\n"
161 " o_color = vec4(gl_SampleID, gl_SamplePosition.x, gl_SamplePosition.y, 1);\n"
164 glu::ShaderProgram programEnable(m_context.getRenderContext(),
165 glu::makeVtxFragSources(specializeVersion(vss, m_glslVersion).c_str(),
166 specializeVersion(fss, m_glslVersion).c_str()));
167 log << programEnable;
168 if (!programEnable.isOk())
170 TCU_FAIL("Compile failed");
174 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
178 class SampleShadingMaskCase : public TestCase
181 SampleShadingMaskCase(Context& context, const char* name, const char* description, glu::GLSLVersion glslVersion,
182 GLenum internalFormat, tcu::TextureFormat const& texFormat, const char* sampler,
183 const char* outType, GLint samples, GLint sampleMask);
184 ~SampleShadingMaskCase();
186 IterateResult iterate();
189 glu::GLSLVersion m_glslVersion;
190 GLenum m_internalFormat;
191 tcu::TextureFormat m_texFormat;
192 std::string m_sampler;
193 std::string m_outType;
205 SampleShadingMaskCase::SampleShadingMaskCase(Context& context, const char* name, const char* description,
206 glu::GLSLVersion glslVersion, GLenum internalFormat,
207 tcu::TextureFormat const& texFormat, const char* sampler,
208 const char* outType, GLint samples, GLint sampleMask)
209 : TestCase(context, name, description)
210 , m_glslVersion(glslVersion)
211 , m_internalFormat(internalFormat)
212 , m_texFormat(texFormat)
216 , m_sampleMask(sampleMask)
218 DE_ASSERT(glslVersion == glu::GLSL_VERSION_310_ES || glslVersion >= glu::GLSL_VERSION_400);
221 SampleShadingMaskCase::~SampleShadingMaskCase()
225 SampleShadingMaskCase::IterateResult SampleShadingMaskCase::iterate()
227 TestLog& log = m_testCtx.getLog();
228 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
230 bool supportsRgba32f = false;
232 if (m_glslVersion == glu::GLSL_VERSION_310_ES &&
233 !m_context.getContextInfo().isExtensionSupported("GL_OES_sample_variables"))
235 m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED, "GL_OES_sample_variables");
239 supportsRgba32f = isContextTypeGLCore(m_context.getRenderContext().getType()) ?
241 (m_context.getContextInfo().isExtensionSupported("GL_EXT_color_buffer_float") ||
242 m_context.getContextInfo().isExtensionSupported("GL_ARB_color_buffer_float"));
244 if (m_internalFormat == GL_RGBA32F && !supportsRgba32f)
246 m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED, "Internalformat rgba32f not supported");
251 if (((m_texFormat.type == tcu::TextureFormat::FLOAT) && (m_texFormat.order == tcu::TextureFormat::RGBA)) ||
252 ((m_texFormat.type == tcu::TextureFormat::FLOAT) && (m_texFormat.order == tcu::TextureFormat::RG)) ||
253 ((m_texFormat.type == tcu::TextureFormat::FLOAT) && (m_texFormat.order == tcu::TextureFormat::R)) ||
254 ((m_texFormat.type == tcu::TextureFormat::HALF_FLOAT) && (m_texFormat.order == tcu::TextureFormat::RGBA)))
256 gl.getInternalformativ(GL_TEXTURE_2D_MULTISAMPLE, m_internalFormat, GL_SAMPLES, 1, &maxSamples);
257 if (m_samples > maxSamples)
259 m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED,
260 "Test sample count greater than samples that the format supports");
264 else if (m_texFormat.type == tcu::TextureFormat::SIGNED_INT8 ||
265 m_texFormat.type == tcu::TextureFormat::UNSIGNED_INT8)
267 gl.getIntegerv(GL_MAX_INTEGER_SAMPLES, &maxSamples);
268 if (m_samples > maxSamples)
270 m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED, "Test sample count greater than MAX_INTEGER_SAMPLES");
276 gl.getIntegerv(GL_MAX_SAMPLES, &maxSamples);
277 if (m_samples > maxSamples)
279 m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED, "Test sample count greater than MAX_SAMPLES");
284 // Create a multisample texture, or a regular texture if samples is zero.
286 gl.genTextures(1, &tex);
290 target = GL_TEXTURE_2D_MULTISAMPLE;
291 gl.bindTexture(target, tex);
292 gl.texStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, m_internalFormat, WIDTH, HEIGHT, GL_FALSE);
296 target = GL_TEXTURE_2D;
297 gl.bindTexture(target, tex);
298 gl.texStorage2D(GL_TEXTURE_2D, 1, m_internalFormat, WIDTH, HEIGHT);
299 if (m_texFormat.type == tcu::TextureFormat::SIGNED_INT8 ||
300 m_texFormat.type == tcu::TextureFormat::UNSIGNED_INT8 || m_texFormat.type == tcu::TextureFormat::FLOAT)
302 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
303 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
307 // Create a framebuffer with the texture attached and clear to "green".
309 gl.genFramebuffers(1, &fboMs);
310 gl.bindFramebuffer(GL_FRAMEBUFFER, fboMs);
311 gl.framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, tex, 0);
312 gl.viewport(0, 0, WIDTH, HEIGHT);
313 if (m_texFormat.type == tcu::TextureFormat::SIGNED_INT8)
315 GLint color[4] = { 0, 1, 0, 1 };
316 gl.clearBufferiv(GL_COLOR, 0, color);
318 else if (m_texFormat.type == tcu::TextureFormat::UNSIGNED_INT8)
320 GLuint color[4] = { 0, 1, 0, 1 };
321 gl.clearBufferuiv(GL_COLOR, 0, color);
325 GLfloat color[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
326 gl.clearBufferfv(GL_COLOR, 0, color);
329 static deUint16 const quadIndices[] = { 0, 1, 2, 2, 1, 3 };
332 // Draw a quad setting all samples to "red". We only expect "red"
333 // to be written if the sample mask bit for that sample is 1.
335 static char const* vss = "${VERSION_DECL}\n"
336 "in highp vec2 a_position;\n"
339 " gl_Position = vec4(a_position, 0.0, 1.0);\n"
342 static char const* fss = "${VERSION_DECL}\n"
344 "layout(location = 0) out highp ${OUT_TYPE} o_color;\n"
345 "uniform int u_sampleMask;\n"
348 " for (int i = 0; i < (gl_NumSamples + 31) / 32; ++i) {\n"
349 " gl_SampleMask[i] = u_sampleMask & gl_SampleMaskIn[i];\n"
351 " o_color = ${OUT_TYPE}(1, 0, 0, 1);\n"
354 glu::ShaderProgram program(
355 m_context.getRenderContext(),
356 glu::makeVtxFragSources(specializeVersion(vss, m_glslVersion, m_sampler, m_outType).c_str(),
357 specializeVersion(fss, m_glslVersion, m_sampler, m_outType).c_str()));
361 TCU_FAIL("Compile failed");
364 static float const position[] = {
365 -1.0f, -1.0f, -1.0f, +1.0f, +1.0f, -1.0f, +1.0f, +1.0f,
368 gl.useProgram(program.getProgram());
369 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_sampleMask"), m_sampleMask);
371 glu::VertexArrayBinding vertexArrays[] = {
372 glu::va::Float("a_position", 2, 4, 0, &position[0]),
374 glu::draw(m_context.getRenderContext(), program.getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays),
375 &vertexArrays[0], glu::pr::TriangleStrip(DE_LENGTH_OF_ARRAY(quadIndices), &quadIndices[0]));
377 GLU_EXPECT_NO_ERROR(gl.getError(), "Draw quad");
380 gl.bindFramebuffer(GL_FRAMEBUFFER, m_context.getRenderContext().getDefaultFramebuffer());
381 gl.deleteFramebuffers(1, &fboMs);
383 GLsizei width = WIDTH * ((m_samples) ? m_samples : 1);
386 gl.genRenderbuffers(1, &rbo);
387 gl.bindRenderbuffer(GL_RENDERBUFFER, rbo);
388 gl.renderbufferStorage(GL_RENDERBUFFER, m_internalFormat, width, HEIGHT);
391 gl.genFramebuffers(1, &fbo);
392 gl.bindFramebuffer(GL_FRAMEBUFFER, fbo);
393 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
394 gl.viewport(0, 0, width, HEIGHT);
397 // Resolve the multi-sample texture into a render-buffer sized such that
398 // the width can hold all samples of a pixel.
399 static char const* vss = "${VERSION_DECL}\n"
400 "in highp vec2 a_position;\n"
403 " gl_Position = vec4(a_position, 0.0, 1.0);\n"
406 static char const* fss = "${VERSION_DECL}\n"
407 "uniform highp ${SAMPLER} u_tex;\n"
408 "uniform highp ${SAMPLER}MS u_texMS;\n"
409 "uniform int u_samples;\n"
410 "layout(location = 0) out highp ${OUT_TYPE} o_color;\n"
413 " if (u_samples > 0) {\n"
414 " ivec2 coord = ivec2(int(gl_FragCoord.x) / u_samples, gl_FragCoord.y);\n"
415 " int sampleId = int(gl_FragCoord.x) % u_samples;\n"
416 " o_color = texelFetch(u_texMS, coord, sampleId);\n"
418 " ivec2 coord = ivec2(gl_FragCoord.x, gl_FragCoord.y);\n"
419 " o_color = texelFetch(u_tex, coord, 0);\n"
423 glu::ShaderProgram program(
424 m_context.getRenderContext(),
425 glu::makeVtxFragSources(specializeVersion(vss, m_glslVersion, m_sampler, m_outType).c_str(),
426 specializeVersion(fss, m_glslVersion, m_sampler, m_outType).c_str()));
430 TCU_FAIL("Compile failed");
433 static float const position[] = {
434 -1.0f, -1.0f, -1.0f, +1.0f, +1.0f, -1.0f, +1.0f, +1.0f,
437 gl.useProgram(program.getProgram());
438 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_samples"), m_samples);
441 // only MS sampler needed, TU 1 is not used
442 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_tex"), 1);
443 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_texMS"), 0);
447 // only non-MS sampler needed, TU 1 is not used
448 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_tex"), 0);
449 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_texMS"), 1);
452 glu::VertexArrayBinding vertexArrays[] = {
453 glu::va::Float("a_position", 2, 4, 0, &position[0]),
455 glu::draw(m_context.getRenderContext(), program.getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays),
456 &vertexArrays[0], glu::pr::TriangleStrip(DE_LENGTH_OF_ARRAY(quadIndices), &quadIndices[0]));
458 GLU_EXPECT_NO_ERROR(gl.getError(), "Draw quad");
461 tcu::TextureLevel results(m_texFormat, width, HEIGHT);
462 tcu::PixelBufferAccess pixels = results.getAccess();
463 std::vector<tcu::Vec4> result(pixels.getHeight() * pixels.getWidth());
465 if (pixels.getFormat().type == tcu::TextureFormat::SIGNED_INT8)
467 std::vector<GLint> data(pixels.getHeight() * pixels.getWidth() * 4);
468 gl.readPixels(0, 0, pixels.getWidth(), pixels.getHeight(), GL_RGBA_INTEGER, GL_INT, &data[0]);
469 for (unsigned int i = 0; i < data.size(); i += 4)
472 tcu::Vec4((GLfloat)data[i], (GLfloat)data[i + 1], (GLfloat)data[i + 2], (GLfloat)data[i + 3]);
475 else if (pixels.getFormat().type == tcu::TextureFormat::UNSIGNED_INT8)
477 std::vector<GLuint> data(pixels.getHeight() * pixels.getWidth() * 4);
478 gl.readPixels(0, 0, pixels.getWidth(), pixels.getHeight(), GL_RGBA_INTEGER, GL_UNSIGNED_INT, &data[0]);
479 for (unsigned int i = 0; i < data.size(); i += 4)
482 tcu::Vec4((GLfloat)data[i], (GLfloat)data[i + 1], (GLfloat)data[i + 2], (GLfloat)data[i + 3]);
487 glu::readPixels(m_context.getRenderContext(), 0, 0, pixels);
490 for (int y = 0; y < HEIGHT; ++y)
492 for (int x = 0; x < WIDTH; ++x)
494 GLint samples = (m_samples) ? m_samples : 1;
495 for (int sample = 0; sample < samples; ++sample)
498 if (pixels.getFormat().type == tcu::TextureFormat::SIGNED_INT8 ||
499 pixels.getFormat().type == tcu::TextureFormat::UNSIGNED_INT8)
501 pixel = result[y * WIDTH + x * samples + sample];
505 pixel = pixels.getPixel(x * samples + sample, y);
508 // Make sure only those samples where the sample mask bit is
509 // non-zero have the "red" pixel values.
510 if (!m_samples || (m_sampleMask & (1 << sample)))
512 if (pixel != tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f))
519 if (pixel != tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f))
528 gl.bindFramebuffer(GL_FRAMEBUFFER, m_context.getRenderContext().getDefaultFramebuffer());
529 gl.deleteFramebuffers(1, &fbo);
531 gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
532 gl.deleteRenderbuffers(1, &rbo);
534 gl.bindTexture(target, 0);
535 gl.deleteTextures(1, &tex);
537 m_testCtx.setTestResult(isOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL, isOk ? "Pass" : "Fail");
541 class SampleShadingPositionCase : public TestCase
544 SampleShadingPositionCase(Context& context, const char* name, const char* description, glu::GLSLVersion glslVersion,
545 GLint samples, GLboolean fixedSampleLocations);
546 ~SampleShadingPositionCase();
548 IterateResult iterate();
551 glu::GLSLVersion m_glslVersion;
553 GLboolean m_fixedSampleLocations;
563 SampleShadingPositionCase::SampleShadingPositionCase(Context& context, const char* name, const char* description,
564 glu::GLSLVersion glslVersion, GLint samples,
565 GLboolean fixedSampleLocations)
566 : TestCase(context, name, description)
567 , m_glslVersion(glslVersion)
569 , m_fixedSampleLocations(fixedSampleLocations)
571 DE_ASSERT(glslVersion == glu::GLSL_VERSION_310_ES || glslVersion >= glu::GLSL_VERSION_400);
574 SampleShadingPositionCase::~SampleShadingPositionCase()
578 SampleShadingPositionCase::IterateResult SampleShadingPositionCase::iterate()
580 TestLog& log = m_testCtx.getLog();
581 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
584 if (m_glslVersion == glu::GLSL_VERSION_310_ES &&
585 !m_context.getContextInfo().isExtensionSupported("GL_OES_sample_variables"))
587 m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED, "GL_OES_sample_variables");
592 gl.getIntegerv(GL_MAX_SAMPLES, &maxSamples);
593 if (m_samples > maxSamples)
595 m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED, "Test sample count great than MAX_SAMPLES");
599 // Create a multisample texture, or a regular texture if samples is zero.
601 gl.genTextures(1, &tex);
605 target = GL_TEXTURE_2D_MULTISAMPLE;
606 gl.bindTexture(target, tex);
607 gl.texStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, GL_RGBA8, WIDTH, HEIGHT,
608 m_fixedSampleLocations);
612 target = GL_TEXTURE_2D;
613 gl.bindTexture(target, tex);
614 gl.texStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, WIDTH, HEIGHT);
617 // Attach the texture to the framebuffer to render to it.
619 gl.genFramebuffers(1, &fboMs);
620 gl.bindFramebuffer(GL_FRAMEBUFFER, fboMs);
621 gl.framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, tex, 0);
622 gl.viewport(0, 0, WIDTH, HEIGHT);
624 // Save all the sample positions for this multisample framebuffer.
625 std::vector<tcu::Vec4> samplePositions;
628 samplePositions.resize(m_samples);
629 for (int sample = 0; sample < m_samples; ++sample)
632 gl.getMultisamplefv(GL_SAMPLE_POSITION, sample, position);
633 samplePositions[sample] = tcu::Vec4(position[0], position[1], 0.0f, 1.0f);
637 static deUint16 const quadIndices[] = { 0, 1, 2, 2, 1, 3 };
640 // Render all the sample positions to each pixel sample.
642 static char const* vss = "${VERSION_DECL}\n"
643 "in highp vec2 a_position;\n"
646 " gl_Position = vec4(a_position, 0.0, 1.0);\n"
649 static char const* fss = "${VERSION_DECL}\n"
651 "layout(location = 0) out highp vec4 o_color;\n"
654 " o_color = vec4(gl_SamplePosition, 0, 1);\n"
657 glu::ShaderProgram program(m_context.getRenderContext(),
658 glu::makeVtxFragSources(specializeVersion(vss, m_glslVersion).c_str(),
659 specializeVersion(fss, m_glslVersion).c_str()));
663 TCU_FAIL("Compile failed");
666 const float position[] = {
667 -1.0f, -1.0f, -1.0f, +1.0f, +1.0f, -1.0f, +1.0f, +1.0f,
670 gl.useProgram(program.getProgram());
672 glu::VertexArrayBinding vertexArrays[] = {
673 glu::va::Float("a_position", 2, 4, 0, &position[0]),
676 glu::draw(m_context.getRenderContext(), program.getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays),
677 &vertexArrays[0], glu::pr::TriangleStrip(DE_LENGTH_OF_ARRAY(quadIndices), &quadIndices[0]));
679 GLU_EXPECT_NO_ERROR(gl.getError(), "Draw quad");
682 gl.bindFramebuffer(GL_FRAMEBUFFER, m_context.getRenderContext().getDefaultFramebuffer());
683 gl.deleteFramebuffers(1, &fboMs);
685 // Create a regular non-multisample render buffer to resolve to multisample texture into.
686 // The width is increased to save all samples of the pixel.
688 GLsizei width = WIDTH * ((m_samples) ? m_samples : 1);
691 gl.genRenderbuffers(1, &rbo);
692 gl.bindRenderbuffer(GL_RENDERBUFFER, rbo);
693 gl.renderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, HEIGHT);
696 gl.genFramebuffers(1, &fbo);
697 gl.bindFramebuffer(GL_FRAMEBUFFER, fbo);
698 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
699 gl.viewport(0, 0, width, HEIGHT);
702 // Resolve the multisample texture to the renderbuffer.
704 static char const* vss = "${VERSION_DECL}\n"
705 "in highp vec2 a_position;\n"
708 " gl_Position = vec4(a_position, 0.0, 1.0);\n"
711 static char const* fss = "${VERSION_DECL}\n"
712 "uniform highp sampler2D u_tex;\n"
713 "uniform highp sampler2DMS u_texMS;\n"
714 "uniform int u_samples;\n"
715 "layout(location = 0) out highp vec4 o_color;\n"
718 " if (u_samples > 0) {\n"
719 " ivec2 coord = ivec2(int(gl_FragCoord.x) / u_samples, gl_FragCoord.y);\n"
720 " int sampleId = int(gl_FragCoord.x) % u_samples;\n"
721 " o_color = texelFetch(u_texMS, coord, sampleId);\n"
723 " ivec2 coord = ivec2(gl_FragCoord.x, gl_FragCoord.y);\n"
724 " o_color = texelFetch(u_tex, coord, 0);\n"
728 glu::ShaderProgram program(m_context.getRenderContext(),
729 glu::makeVtxFragSources(specializeVersion(vss, m_glslVersion).c_str(),
730 specializeVersion(fss, m_glslVersion).c_str()));
734 TCU_FAIL("Compile failed");
737 static float const position[] = {
738 -1.0f, -1.0f, -1.0f, +1.0f, +1.0f, -1.0f, +1.0f, +1.0f,
741 gl.useProgram(program.getProgram());
742 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_samples"), m_samples);
745 // only MS sampler needed, TU 1 is not used
746 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_tex"), 1);
747 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_texMS"), 0);
751 // only non-MS sampler needed, TU 1 is not used
752 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_tex"), 0);
753 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_texMS"), 1);
756 glu::VertexArrayBinding vertexArrays[] = {
757 glu::va::Float("a_position", 2, 4, 0, &position[0]),
759 glu::draw(m_context.getRenderContext(), program.getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays),
760 &vertexArrays[0], glu::pr::TriangleStrip(DE_LENGTH_OF_ARRAY(quadIndices), &quadIndices[0]));
762 GLU_EXPECT_NO_ERROR(gl.getError(), "Draw quad");
765 // Read the renderbuffer pixels and verify we get back what we're expecting.
766 tcu::TextureLevel results(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), width,
768 tcu::PixelBufferAccess pixels = results.getAccess();
769 glu::readPixels(m_context.getRenderContext(), 0, 0, pixels);
772 // If m_fixedSampleLocations are used make sure the first pixel's samples
773 // all match the SAMPLE_POSITION state saved earlier.
774 std::set<tcu::Vec4> fixedSampleLocations;
775 if (m_fixedSampleLocations)
777 for (int sample = 0; sample < m_samples; ++sample)
779 tcu::Vec4 pixel = pixels.getPixel(sample, 0);
780 fixedSampleLocations.insert(pixel);
781 if (deFloatAbs(pixel.x() - samplePositions[sample].x()) > 0.01 ||
782 deFloatAbs(pixel.y() - samplePositions[sample].y()) > 0.01)
790 // Verify all samples of every pixel to make sure each position is unique.
791 for (int y = 0; y < HEIGHT; ++y)
793 for (int x = 0; x < WIDTH; ++x)
795 std::set<tcu::Vec4> uniquePixels;
796 for (int sample = 0; sample < m_samples; ++sample)
798 uniquePixels.insert(pixels.getPixel(x * m_samples + sample, y));
800 if ((GLint)uniquePixels.size() != m_samples)
804 // For the m_fixedSampleLocations case make sure each position
805 // matches the sample positions of pixel(0, 0) saved earlier.
806 if (m_fixedSampleLocations)
808 if (fixedSampleLocations != uniquePixels)
818 // For the non-multisample case make sure all the positions are (0.5,0.5).
819 for (int y = 0; y < pixels.getHeight(); ++y)
821 for (int x = 0; x < pixels.getWidth(); ++x)
823 tcu::Vec4 pixel = pixels.getPixel(x, y);
824 if (deFloatAbs(pixel.x() - 0.5f) > 0.01 || deFloatAbs(pixel.y() - 0.5f) > 0.01 || pixel.z() != 0.0f ||
833 gl.bindFramebuffer(GL_FRAMEBUFFER, m_context.getRenderContext().getDefaultFramebuffer());
834 gl.deleteFramebuffers(1, &fbo);
836 gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
837 gl.deleteRenderbuffers(1, &rbo);
839 gl.bindTexture(target, 0);
840 gl.deleteTextures(1, &tex);
842 m_testCtx.setTestResult(isOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL, isOk ? "Pass" : "Fail");
846 SampleVariablesTests::SampleVariablesTests(Context& context, glu::GLSLVersion glslVersion)
847 : TestCaseGroup(context, "sample_variables", "Sample Variables tests"), m_glslVersion(glslVersion)
851 SampleVariablesTests::~SampleVariablesTests()
855 void SampleVariablesTests::init()
857 de::Random rnd(m_context.getTestContext().getCommandLine().getBaseSeed());
864 { "samples_0", 0 }, { "samples_1", 1 }, { "samples_2", 2 }, { "samples_4", 4 }, { "samples_8", 8 },
867 // sample_variables.extension
868 if (m_glslVersion == glu::GLSL_VERSION_310_ES)
870 tcu::TestCaseGroup* extensionCaseGroup = new tcu::TestCaseGroup(m_testCtx, "verification", "");
871 extensionCaseGroup->addChild(
872 new SampleShadingExtensionCase(m_context, "extension", "#extension verification", m_glslVersion));
873 addChild(extensionCaseGroup);
876 // sample_variables.mask
877 tcu::TestCaseGroup* maskGroup = new tcu::TestCaseGroup(m_testCtx, "mask", "gl_SampleMask tests");
882 GLenum internalFormat;
883 tcu::TextureFormat textureFormat;
887 { "rgba8", GL_RGBA8, tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), "sampler2D",
889 { "rgba8i", GL_RGBA8I, tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::SIGNED_INT8),
890 "isampler2D", "ivec4" },
891 { "rgba8ui", GL_RGBA8UI, tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNSIGNED_INT8),
892 "usampler2D", "uvec4" },
893 { "rgba32f", GL_RGBA32F, tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::FLOAT), "sampler2D",
896 for (int format = 0; format < DE_LENGTH_OF_ARRAY(formats); ++format)
898 tcu::TestCaseGroup* maskFormatGroup = new tcu::TestCaseGroup(m_testCtx, formats[format].name, "");
899 maskGroup->addChild(maskFormatGroup);
901 for (int sample = 0; sample < DE_LENGTH_OF_ARRAY(samples); ++sample)
903 tcu::TestCaseGroup* maskFormatSampleGroup = new tcu::TestCaseGroup(m_testCtx, samples[sample].name, "");
904 maskFormatGroup->addChild(maskFormatSampleGroup);
906 maskFormatSampleGroup->addChild(
907 new SampleShadingMaskCase(m_context, "mask_zero", "", m_glslVersion, formats[format].internalFormat,
908 formats[format].textureFormat, formats[format].sampler,
909 formats[format].outType, samples[sample].samples, 0));
911 for (int mask = 0; mask < SAMPLE_MASKS; ++mask)
913 std::stringstream ss;
914 ss << "mask_" << mask;
915 maskFormatSampleGroup->addChild(new SampleShadingMaskCase(
916 m_context, ss.str().c_str(), "", m_glslVersion, formats[format].internalFormat,
917 formats[format].textureFormat, formats[format].sampler, formats[format].outType,
918 samples[sample].samples, rnd.getUint32()));
923 // sample_variables.position
924 tcu::TestCaseGroup* positionGroup = new tcu::TestCaseGroup(m_testCtx, "position", "gl_SamplePosition tests");
925 addChild(positionGroup);
929 GLboolean fixedSampleLocations;
931 { "non-fixed", GL_FALSE }, { "fixed", GL_TRUE },
933 for (int j = 0; j < DE_LENGTH_OF_ARRAY(fixed); ++j)
935 tcu::TestCaseGroup* positionFixedGroup = new tcu::TestCaseGroup(m_testCtx, fixed[j].name, "");
936 positionGroup->addChild(positionFixedGroup);
937 for (int sample = 0; sample < DE_LENGTH_OF_ARRAY(samples); ++sample)
939 positionFixedGroup->addChild(new SampleShadingPositionCase(m_context, samples[sample].name, "",
940 m_glslVersion, samples[sample].samples,
941 fixed[j].fixedSampleLocations));