1 /*-------------------------------------------------------------------------
2 * OpenGL Conformance Test Suite
3 * -----------------------------
5 * Copyright (c) 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 */ /*-------------------------------------------------------------------*/
26 * \file gl4cPostDepthCoverageTests.cpp
27 * \brief Conformance tests for the GL_ARB_post_depth_coverage functionality.
28 */ /*-------------------------------------------------------------------*/
30 #include "gl4cPostDepthCoverageTests.hpp"
31 #include "gluContextInfo.hpp"
32 #include "gluDefs.hpp"
33 #include "gluDrawUtil.hpp"
34 #include "gluShaderProgram.hpp"
35 #include "glwEnums.hpp"
36 #include "glwFunctions.hpp"
37 #include "tcuTestLog.hpp"
44 static const char* c_commonVertShader = "#version 450\n"
47 "in vec2 inTexCoord;\n"
48 "out vec2 texCoord;\n"
52 " texCoord = inTexCoord;\n"
53 " gl_Position = vec4(vertex, 1);\n"
58 * @param context Rendering context
60 PostDepthShaderCase::PostDepthShaderCase(deqp::Context& context)
61 : TestCase(context, "PostDepthShader",
62 "Verifies if shader functionality added in ARB_post_depth_coverage extension works as expected")
64 /* Left blank intentionally */
67 /** Stub deinit method. */
68 void PostDepthShaderCase::deinit()
72 /** Stub init method */
73 void PostDepthShaderCase::init()
75 if (!m_context.getContextInfo().isExtensionSupported("GL_ARB_post_depth_coverage"))
76 throw tcu::NotSupportedError("GL_ARB_post_depth_coverage not supported");
78 m_vertShader = c_commonVertShader;
80 m_fragShader1 = "#version 450\n"
82 "#extension GL_ARB_post_depth_coverage : require\n"
84 "out vec4 fragColor;\n"
88 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
91 m_fragShader2 = "#version 450\n"
93 "#extension GL_ARB_post_depth_coverage : enable\n"
95 "#ifndef GL_ARB_post_depth_coverage\n"
96 " #error GL_ARB_post_depth_coverage not defined\n"
98 " #if (GL_ARB_post_depth_coverage != 1)\n"
99 " #error GL_ARB_post_depth_coverage wrong defined\n"
101 "#endif // GL_ARB_post_depth_coverage\n"
103 "out vec4 fragColor;\n"
107 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
110 m_fragShader3 = "#version 450\n"
112 "#extension GL_ARB_post_depth_coverage : enable\n"
114 "layout(early_fragment_tests) in;\n"
115 "layout(post_depth_coverage) in;\n"
117 "out vec4 fragColor;\n"
121 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
124 m_fragShader4 = "#version 450\n"
126 "#extension GL_ARB_post_depth_coverage : enable\n"
128 "layout(post_depth_coverage) in;\n"
130 "out vec4 fragColor;\n"
134 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
138 /** Executes test iteration.
140 * @return Returns STOP when test has finished executing, CONTINUE if more iterations are needed.
142 tcu::TestNode::IterateResult PostDepthShaderCase::iterate()
144 const Functions& gl = m_context.getRenderContext().getFunctions();
146 ProgramSources sources1 = makeVtxFragSources(m_vertShader, m_fragShader1);
147 ShaderProgram program1(gl, sources1);
149 if (!program1.isOk())
151 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail checking extension in shader");
152 m_testCtx.getLog() << tcu::TestLog::Message << "Vertex: " << program1.getShaderInfo(SHADERTYPE_VERTEX).infoLog
154 << "Fragment: " << program1.getShaderInfo(SHADERTYPE_FRAGMENT).infoLog << "\n"
155 << "Program: " << program1.getProgramInfo().infoLog << tcu::TestLog::EndMessage;
160 ProgramSources sources2 = makeVtxFragSources(m_vertShader, m_fragShader2);
161 ShaderProgram program2(gl, sources2);
163 if (!program2.isOk())
165 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail checking preprocessor directives in shader");
166 m_testCtx.getLog() << tcu::TestLog::Message << "Vertex: " << program2.getShaderInfo(SHADERTYPE_VERTEX).infoLog
168 << "Fragment: " << program2.getShaderInfo(SHADERTYPE_FRAGMENT).infoLog << "\n"
169 << "Program: " << program2.getProgramInfo().infoLog << tcu::TestLog::EndMessage;
173 ProgramSources sources3 = makeVtxFragSources(m_vertShader, m_fragShader3);
174 ShaderProgram program3(gl, sources3);
176 if (!program3.isOk())
178 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail checking first layout setup in shader");
179 m_testCtx.getLog() << tcu::TestLog::Message << "Vertex: " << program3.getShaderInfo(SHADERTYPE_VERTEX).infoLog
181 << "Fragment: " << program3.getShaderInfo(SHADERTYPE_FRAGMENT).infoLog << "\n"
182 << "Program: " << program3.getProgramInfo().infoLog << tcu::TestLog::EndMessage;
186 ProgramSources sources4 = makeVtxFragSources(m_vertShader, m_fragShader4);
187 ShaderProgram program4(gl, sources4);
189 if (!program4.isOk())
191 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail checking second layout setup in shader");
192 m_testCtx.getLog() << tcu::TestLog::Message << "Vertex: " << program4.getShaderInfo(SHADERTYPE_VERTEX).infoLog
194 << "Fragment: " << program4.getShaderInfo(SHADERTYPE_FRAGMENT).infoLog << "\n"
195 << "Program: " << program4.getProgramInfo().infoLog << tcu::TestLog::EndMessage;
199 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
205 * @param context Rendering context
207 PostDepthSampleMaskCase::PostDepthSampleMaskCase(deqp::Context& context)
208 : TestCase(context, "PostDepthSampleMask", "Verifies multisampling with depth test and stencil test functionality "
209 "added in ARB_post_depth_coverage extension")
214 , m_depthStencilRenderbuffer(0)
216 /* Left blank intentionally */
219 /** Stub deinit method. */
220 void PostDepthSampleMaskCase::deinit()
222 const Functions& gl = m_context.getRenderContext().getFunctions();
224 gl.bindFramebuffer(GL_FRAMEBUFFER, 0);
227 gl.deleteFramebuffers(1, &m_framebufferMS);
229 gl.deleteFramebuffers(1, &m_framebuffer);
232 gl.deleteTextures(1, &m_textureMS);
234 gl.deleteTextures(1, &m_texture);
236 if (m_depthStencilRenderbuffer)
237 gl.deleteRenderbuffers(1, &m_depthStencilRenderbuffer);
240 /** Stub init method */
241 void PostDepthSampleMaskCase::init()
243 if (!m_context.getContextInfo().isExtensionSupported("GL_ARB_post_depth_coverage"))
244 throw tcu::NotSupportedError("GL_ARB_post_depth_coverage not supported");
246 m_vertShader = c_commonVertShader;
248 m_fragShader1a = "#version 450\n"
250 "#extension GL_ARB_post_depth_coverage : enable\n"
252 "in vec2 texCoord;\n"
253 "out vec4 fragColor;\n"
257 " int samp1 = (gl_SampleMaskIn[0] & 0x1);\n"
258 " int samp2 = (gl_SampleMaskIn[0] & 0x2) / 2;\n"
259 " int samp3 = (gl_SampleMaskIn[0] & 0x4) / 4;\n"
260 " int samp4 = (gl_SampleMaskIn[0] & 0x8) / 8;\n"
261 " fragColor = vec4(samp1, samp2, samp3, samp4);\n"
264 m_fragShader1b = "#version 450\n"
266 "#extension GL_ARB_post_depth_coverage : enable\n"
267 "layout(post_depth_coverage) in;\n"
269 "in vec2 texCoord;\n"
270 "out vec4 fragColor;\n"
274 " int samp1 = (gl_SampleMaskIn[0] & 0x1);\n"
275 " int samp2 = (gl_SampleMaskIn[0] & 0x2) / 2;\n"
276 " int samp3 = (gl_SampleMaskIn[0] & 0x4) / 4;\n"
277 " int samp4 = (gl_SampleMaskIn[0] & 0x8) / 8;\n"
278 " fragColor = vec4(samp1, samp2, samp3, samp4);\n"
281 m_fragShader2 = "#version 450\n"
283 "in vec2 texCoord;\n"
284 "out vec4 fragColor;\n"
286 "uniform sampler2DMS texture;\n"
290 " int samp = int(texCoord.y * 4);\n"
291 " fragColor = texelFetch(texture, ivec2(0, 0), samp);\n"
294 const Functions& gl = m_context.getRenderContext().getFunctions();
296 gl.genRenderbuffers(1, &m_depthStencilRenderbuffer);
297 gl.bindRenderbuffer(GL_RENDERBUFFER, m_depthStencilRenderbuffer);
298 gl.renderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH_STENCIL, 1, 1);
299 GLU_EXPECT_NO_ERROR(gl.getError(), "glRenderbufferStorageMultisample");
301 gl.genTextures(1, &m_textureMS);
302 gl.bindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_textureMS);
303 gl.texStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA8, 1, 1, GL_TRUE);
304 GLU_EXPECT_NO_ERROR(gl.getError(), "glTexStorage2DMultisample");
306 gl.genFramebuffers(1, &m_framebufferMS);
307 gl.bindFramebuffer(GL_FRAMEBUFFER, m_framebufferMS);
308 gl.framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, m_textureMS, 0);
309 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilRenderbuffer);
310 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilRenderbuffer);
312 gl.genTextures(1, &m_texture);
313 gl.bindTexture(GL_TEXTURE_2D, m_texture);
314 gl.texStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 4);
316 gl.genFramebuffers(1, &m_framebuffer);
317 gl.bindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
318 gl.framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture, 0);
321 /** Executes test iteration.
323 * @return Returns STOP when test has finished executing, CONTINUE if more iterations are needed.
325 tcu::TestNode::IterateResult PostDepthSampleMaskCase::iterate()
327 const Functions& gl = m_context.getRenderContext().getFunctions();
329 const GLfloat texCoord[] = {
330 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
333 const GLfloat verticesNear[] = {
334 -1.0f, -1.0f, 0.25f, 0.4f, -1.0f, 0.25f, -1.0f, 0.4f, 0.25f,
337 const GLfloat verticesFar[] = {
338 -1.0f, -1.0f, 0.75f, 3.0f, -1.0f, 0.75f, -1.0f, 3.0f, 0.75f,
341 const GLfloat verticesPost[] = {
342 -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
345 const GLuint indicesPre[] = { 0, 1, 2 };
347 const GLuint indicesPost[] = { 0, 1, 2, 1, 2, 3 };
349 glu::VertexArrayBinding vertexArraysNear[] = { glu::va::Float("vertex", 3, 3, 0, verticesNear),
350 glu::va::Float("inTexCoord", 2, 3, 0, texCoord) };
352 glu::VertexArrayBinding vertexArraysFar[] = { glu::va::Float("vertex", 3, 3, 0, verticesFar),
353 glu::va::Float("inTexCoord", 2, 3, 0, texCoord) };
355 glu::VertexArrayBinding vertexArraysPost[] = { glu::va::Float("vertex", 3, 4, 0, verticesPost),
356 glu::va::Float("inTexCoord", 2, 4, 0, texCoord) };
359 ProgramSources sources1a = makeVtxFragSources(m_vertShader, m_fragShader1a);
360 ShaderProgram program1a(gl, sources1a);
362 if (!program1a.isOk())
364 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Shader program fail (non post_depth_coverage).");
365 m_testCtx.getLog() << tcu::TestLog::Message << "Vertex: " << program1a.getShaderInfo(SHADERTYPE_VERTEX).infoLog
367 << "Fragment: " << program1a.getShaderInfo(SHADERTYPE_FRAGMENT).infoLog << "\n"
368 << "Program: " << program1a.getProgramInfo().infoLog << tcu::TestLog::EndMessage;
372 ProgramSources sources1b = makeVtxFragSources(m_vertShader, m_fragShader1b);
373 ShaderProgram program1b(gl, sources1b);
375 if (!program1b.isOk())
377 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Shader program fail (post_depth_coverage).");
378 m_testCtx.getLog() << tcu::TestLog::Message << "PostDepthCoverage: enabled\n"
379 << "Vertex: " << program1b.getShaderInfo(SHADERTYPE_VERTEX).infoLog << "\n"
380 << "Fragment: " << program1b.getShaderInfo(SHADERTYPE_FRAGMENT).infoLog << "\n"
381 << "Program: " << program1b.getProgramInfo().infoLog << tcu::TestLog::EndMessage;
385 ProgramSources sources2 = makeVtxFragSources(m_vertShader, m_fragShader2);
386 ShaderProgram program2(gl, sources2);
388 if (!program2.isOk())
390 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Shader program fail (samples reader).");
391 m_testCtx.getLog() << tcu::TestLog::Message << "Vertex: " << program2.getShaderInfo(SHADERTYPE_VERTEX).infoLog
393 << "Fragment: " << program2.getShaderInfo(SHADERTYPE_FRAGMENT).infoLog << "\n"
394 << "Program: " << program2.getProgramInfo().infoLog << tcu::TestLog::EndMessage;
398 gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f);
401 //Iterate through all cases
402 for (int bufferCase = BUFFERCASE_FIRST; bufferCase <= BUFFERCASE_LAST; ++bufferCase)
403 for (int pdcCase = PDCCASE_FIRST; pdcCase <= PDCCASE_LAST; ++pdcCase)
406 if (pdcCase == PDCCASE_DISABLED)
407 program = program1a.getProgram();
408 else if (pdcCase == PDCCASE_ENABLED)
409 program = program1b.getProgram();
411 gl.useProgram(program);
413 //Firstible use multisampled framebuffer
414 gl.bindFramebuffer(GL_FRAMEBUFFER, m_framebufferMS);
415 gl.viewport(0, 0, 1, 1);
417 gl.enable(GL_MULTISAMPLE);
419 //Check which samples should be covered by first draw - calculate expected sample mask
420 GLboolean expectedMask[4];
422 for (GLint i = 0; i < 4; ++i)
424 GLfloat samplePos[2];
425 gl.getMultisamplefv(GL_SAMPLE_POSITION, i, &samplePos[0]);
426 GLU_EXPECT_NO_ERROR(gl.getError(), "glGetMultisamplefv");
427 if (pdcCase == PDCCASE_ENABLED && (samplePos[0] + samplePos[1]) < 0.7f)
428 expectedMask[i] = false;
430 expectedMask[i] = true;
433 if (bufferCase == BUFFERCASE_DEPTH)
434 gl.enable(GL_DEPTH_TEST);
435 else if (bufferCase == BUFFERCASE_STENCIL)
436 gl.enable(GL_STENCIL_TEST);
438 gl.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
440 if (bufferCase == BUFFERCASE_STENCIL)
442 gl.stencilFunc(GL_ALWAYS, 1, 0xFF);
443 gl.stencilOp(GL_ZERO, GL_REPLACE, GL_REPLACE);
444 gl.stencilMask(0xFF);
447 //Draw near primitive
448 glu::draw(m_context.getRenderContext(), program, DE_LENGTH_OF_ARRAY(vertexArraysNear), vertexArraysNear,
449 glu::pr::TriangleStrip(DE_LENGTH_OF_ARRAY(indicesPre), indicesPre));
451 gl.clear(GL_COLOR_BUFFER_BIT);
453 if (bufferCase == BUFFERCASE_STENCIL)
455 gl.stencilFunc(GL_NOTEQUAL, 1, 0xFF);
456 gl.stencilMask(0x00);
460 glu::draw(m_context.getRenderContext(), program, DE_LENGTH_OF_ARRAY(vertexArraysFar), vertexArraysFar,
461 glu::pr::TriangleStrip(DE_LENGTH_OF_ARRAY(indicesPre), indicesPre));
463 gl.disable(GL_DEPTH_TEST);
464 if (bufferCase == BUFFERCASE_DEPTH)
465 gl.disable(GL_DEPTH_TEST);
466 else if (bufferCase == BUFFERCASE_STENCIL)
467 gl.disable(GL_STENCIL_TEST);
469 gl.bindFramebuffer(GL_FRAMEBUFFER, 0);
471 gl.useProgram(program2.getProgram());
473 gl.bindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
474 gl.viewport(0, 0, 1, 4);
476 //Pass multisampled texture as a uniform
477 gl.activeTexture(GL_TEXTURE0);
478 gl.bindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_textureMS);
479 gl.uniform1i(gl.getUniformLocation(program2.getProgram(), "texture"), 0);
481 gl.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
483 //Draw multisampled texture to framebuffer
484 glu::draw(m_context.getRenderContext(), program2.getProgram(), DE_LENGTH_OF_ARRAY(vertexArraysPost),
485 vertexArraysPost, glu::pr::TriangleStrip(DE_LENGTH_OF_ARRAY(indicesPost), indicesPost));
487 gl.disable(GL_MULTISAMPLE);
489 //Read data from framebuffer
490 GLubyte pixels[4 * 4];
491 deMemset(pixels, 0, 4 * 4);
493 gl.readPixels(0, 0, 1, 4, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
494 GLU_EXPECT_NO_ERROR(gl.getError(), "glReadPixels");
497 GLboolean compiledMask[4];
498 deMemset(compiledMask, 0, 4);
499 for (int i = 0; i < 4; ++i)
501 for (int n = 0; n < 4; ++n)
502 compiledMask[n] = compiledMask[n] || (pixels[4 * i + n] != 0);
505 for (int n = 0; n < 4; ++n)
507 if (expectedMask[n] != compiledMask[n])
509 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
510 m_testCtx.getLog() << tcu::TestLog::Message << "Wrong results for "
511 << (bufferCase == BUFFERCASE_DEPTH ? "BUFFERCASE_DEPTH" :
512 "BUFFERCASE_DEPTH_STENCIL")
514 << (pdcCase == PDCCASE_DISABLED ? "PDCCASE_DISABLED" : "PDCCASE_ENABLED")
515 << tcu::TestLog::EndMessage;
521 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
527 * @param context Rendering context.
529 PostDepthCoverage::PostDepthCoverage(deqp::Context& context)
530 : TestCaseGroup(context, "post_depth_coverage_tests",
531 "Verify conformance of CTS_ARB_post_depth_coverage implementation")
535 /** Initializes the test group contents. */
536 void PostDepthCoverage::init()
538 addChild(new PostDepthShaderCase(m_context));
539 addChild(new PostDepthSampleMaskCase(m_context));
542 } /* gl4cts namespace */