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 Stencil texturing tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es31fStencilTexturingTests.hpp"
26 #include "gluStrUtil.hpp"
27 #include "gluObjectWrapper.hpp"
28 #include "gluRenderContext.hpp"
29 #include "gluShaderProgram.hpp"
30 #include "gluDrawUtil.hpp"
31 #include "gluPixelTransfer.hpp"
32 #include "gluTextureUtil.hpp"
33 #include "gluContextInfo.hpp"
35 #include "glsTextureTestUtil.hpp"
37 #include "tcuVector.hpp"
38 #include "tcuTexture.hpp"
39 #include "tcuTextureUtil.hpp"
40 #include "tcuTestLog.hpp"
41 #include "tcuTexLookupVerifier.hpp"
43 #include "glwFunctions.hpp"
44 #include "glwEnums.hpp"
46 #include "deStringUtil.hpp"
61 using tcu::TextureLevel;
62 using tcu::TextureFormat;
67 static void genTestRects (vector<IVec4>& rects, int width, int height)
70 int curHeight = height;
75 rects.push_back(IVec4(width-curWidth, height-curHeight, curWidth, curHeight));
77 DE_ASSERT(curWidth >= 1 && curHeight >= 1);
78 if (curWidth == 1 && curHeight == 1)
80 else if (curHeight > 1 && ((ndx%2) == 0 || curWidth == 1))
89 static void rectsToTriangles (const vector<IVec4>& rects, int width, int height, vector<Vec2>& positions, vector<deUint16>& indices)
91 const float w = float(width);
92 const float h = float(height);
94 positions.resize(rects.size()*4);
95 indices.resize(rects.size()*6);
97 for (int rectNdx = 0; rectNdx < (int)rects.size(); rectNdx++)
99 const int rx = rects[rectNdx].x();
100 const int ry = rects[rectNdx].y();
101 const int rw = rects[rectNdx].z();
102 const int rh = rects[rectNdx].w();
104 const float x0 = float(rx*2)/w - 1.0f;
105 const float x1 = float((rx+rw)*2)/w - 1.0f;
106 const float y0 = float(ry*2)/h - 1.0f;
107 const float y1 = float((ry+rh)*2)/h - 1.0f;
109 positions[rectNdx*4 + 0] = Vec2(x0, y0);
110 positions[rectNdx*4 + 1] = Vec2(x1, y0);
111 positions[rectNdx*4 + 2] = Vec2(x0, y1);
112 positions[rectNdx*4 + 3] = Vec2(x1, y1);
114 indices[rectNdx*6 + 0] = (deUint16)(rectNdx*4 + 0);
115 indices[rectNdx*6 + 1] = (deUint16)(rectNdx*4 + 1);
116 indices[rectNdx*6 + 2] = (deUint16)(rectNdx*4 + 2);
117 indices[rectNdx*6 + 3] = (deUint16)(rectNdx*4 + 2);
118 indices[rectNdx*6 + 4] = (deUint16)(rectNdx*4 + 1);
119 indices[rectNdx*6 + 5] = (deUint16)(rectNdx*4 + 3);
123 static void drawTestPattern (const glu::RenderContext& renderCtx, int width, int height)
125 const glu::ShaderProgram program(renderCtx, glu::ProgramSources()
126 << glu::VertexSource(
128 "in highp vec4 a_position;\n"
131 " gl_Position = a_position;\n"
133 << glu::FragmentSource(
135 "void main (void) {}\n"));
137 const glw::Functions& gl = renderCtx.getFunctions();
139 vector<Vec2> positions;
140 vector<deUint16> indices;
143 throw tcu::TestError("Compile failed");
145 gl.useProgram (program.getProgram());
146 gl.viewport (0, 0, width, height);
147 gl.clear (GL_STENCIL_BUFFER_BIT);
148 gl.enable (GL_STENCIL_TEST);
149 gl.stencilOp (GL_KEEP, GL_KEEP, GL_INCR_WRAP);
150 gl.stencilFunc (GL_ALWAYS, 0, ~0u);
151 GLU_EXPECT_NO_ERROR(gl.getError(), "State setup failed");
153 genTestRects (rects, width, height);
154 rectsToTriangles(rects, width, height, positions, indices);
157 const glu::VertexArrayBinding posBinding = glu::va::Float("a_position", 2, (int)positions.size(), 0, positions[0].getPtr());
158 glu::draw(renderCtx, program.getProgram(), 1, &posBinding, glu::pr::Triangles((int)indices.size(), &indices[0]));
161 gl.disable(GL_STENCIL_TEST);
164 static void renderTestPatternReference (const tcu::PixelBufferAccess& dst)
166 const int stencilBits = tcu::getTextureFormatBitDepth(tcu::getEffectiveDepthStencilAccess(dst, tcu::Sampler::MODE_STENCIL).getFormat()).x();
167 const deUint32 stencilMask = (1u<<stencilBits)-1u;
170 DE_ASSERT(dst.getFormat().order == TextureFormat::S || dst.getFormat().order == TextureFormat::DS);
172 // clear depth and stencil
173 if (dst.getFormat().order == TextureFormat::DS)
174 tcu::clearDepth(dst, 0.0f);
175 tcu::clearStencil(dst, 0u);
177 genTestRects(rects, dst.getWidth(), dst.getHeight());
179 for (vector<IVec4>::const_iterator rectIter = rects.begin(); rectIter != rects.end(); ++rectIter)
181 const int x0 = rectIter->x();
182 const int y0 = rectIter->y();
183 const int x1 = x0+rectIter->z();
184 const int y1 = y0+rectIter->w();
186 for (int y = y0; y < y1; y++)
188 for (int x = x0; x < x1; x++)
190 const int oldVal = dst.getPixStencil(x, y);
191 const int newVal = (oldVal+1)&stencilMask;
193 dst.setPixStencil(newVal, x, y);
199 static void blitStencilToColor2D (const glu::RenderContext& renderCtx, deUint32 srcTex, int width, int height)
201 const glu::ShaderProgram program(renderCtx, glu::ProgramSources()
202 << glu::VertexSource(
204 "in highp vec4 a_position;\n"
205 "in highp vec2 a_texCoord;\n"
206 "out highp vec2 v_texCoord;\n"
209 " gl_Position = a_position;\n"
210 " v_texCoord = a_texCoord;\n"
212 << glu::FragmentSource(
214 "uniform highp usampler2D u_sampler;\n"
215 "in highp vec2 v_texCoord;\n"
216 "layout(location = 0) out highp uint o_stencil;\n"
219 " o_stencil = texture(u_sampler, v_texCoord).x;\n"
222 const float positions[] =
229 const float texCoord[] =
236 const glu::VertexArrayBinding vertexArrays[] =
238 glu::va::Float("a_position", 2, 4, 0, &positions[0]),
239 glu::va::Float("a_texCoord", 2, 4, 0, &texCoord[0])
241 const deUint8 indices[] = { 0, 1, 2, 2, 1, 3 };
243 const glw::Functions& gl = renderCtx.getFunctions();
246 throw tcu::TestError("Compile failed");
248 gl.activeTexture(GL_TEXTURE0);
249 gl.bindTexture(GL_TEXTURE_2D, srcTex);
250 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
251 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
252 gl.texParameteri(GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_STENCIL_INDEX);
253 GLU_EXPECT_NO_ERROR(gl.getError(), "Texture state setup failed");
255 gl.useProgram(program.getProgram());
256 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_sampler"), 0);
257 GLU_EXPECT_NO_ERROR(gl.getError(), "Program setup failed");
259 gl.viewport(0, 0, width, height);
260 glu::draw(renderCtx, program.getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), &vertexArrays[0],
261 glu::pr::Triangles(DE_LENGTH_OF_ARRAY(indices), &indices[0]));
264 static void blitStencilToColor2DArray (const glu::RenderContext& renderCtx, deUint32 srcTex, int width, int height, int level)
266 const glu::ShaderProgram program(renderCtx, glu::ProgramSources()
267 << glu::VertexSource(
269 "in highp vec4 a_position;\n"
270 "in highp vec3 a_texCoord;\n"
271 "out highp vec3 v_texCoord;\n"
274 " gl_Position = a_position;\n"
275 " v_texCoord = a_texCoord;\n"
277 << glu::FragmentSource(
279 "uniform highp usampler2DArray u_sampler;\n"
280 "in highp vec3 v_texCoord;\n"
281 "layout(location = 0) out highp uint o_stencil;\n"
284 " o_stencil = texture(u_sampler, v_texCoord).x;\n"
287 const float positions[] =
294 const float texCoord[] =
296 0.0f, 0.0f, float(level),
297 1.0f, 0.0f, float(level),
298 0.0f, 1.0f, float(level),
299 1.0f, 1.0f, float(level)
301 const glu::VertexArrayBinding vertexArrays[] =
303 glu::va::Float("a_position", 2, 4, 0, &positions[0]),
304 glu::va::Float("a_texCoord", 3, 4, 0, &texCoord[0])
306 const deUint8 indices[] = { 0, 1, 2, 2, 1, 3 };
308 const glw::Functions& gl = renderCtx.getFunctions();
311 throw tcu::TestError("Compile failed");
313 gl.activeTexture(GL_TEXTURE0);
314 gl.bindTexture(GL_TEXTURE_2D_ARRAY, srcTex);
315 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
316 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
317 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_STENCIL_INDEX);
318 GLU_EXPECT_NO_ERROR(gl.getError(), "Texture state setup failed");
320 gl.useProgram(program.getProgram());
321 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_sampler"), 0);
322 GLU_EXPECT_NO_ERROR(gl.getError(), "Program setup failed");
324 gl.viewport(0, 0, width, height);
325 glu::draw(renderCtx, program.getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), &vertexArrays[0],
326 glu::pr::Triangles(DE_LENGTH_OF_ARRAY(indices), &indices[0]));
329 static void blitStencilToColorCube (const glu::RenderContext& renderCtx, deUint32 srcTex, const float* texCoord, int width, int height)
331 const glu::ShaderProgram program(renderCtx, glu::ProgramSources()
332 << glu::VertexSource(
334 "in highp vec4 a_position;\n"
335 "in highp vec3 a_texCoord;\n"
336 "out highp vec3 v_texCoord;\n"
339 " gl_Position = a_position;\n"
340 " v_texCoord = a_texCoord;\n"
342 << glu::FragmentSource(
344 "uniform highp usamplerCube u_sampler;\n"
345 "in highp vec3 v_texCoord;\n"
346 "layout(location = 0) out highp vec4 o_color;\n"
349 " o_color.x = float(texture(u_sampler, v_texCoord).x) / 255.0;\n"
350 " o_color.yzw = vec3(0.0, 0.0, 1.0);\n"
353 const float positions[] =
360 const glu::VertexArrayBinding vertexArrays[] =
362 glu::va::Float("a_position", 2, 4, 0, &positions[0]),
363 glu::va::Float("a_texCoord", 3, 4, 0, texCoord)
365 const deUint8 indices[] = { 0, 1, 2, 2, 1, 3 };
367 const glw::Functions& gl = renderCtx.getFunctions();
370 throw tcu::TestError("Compile failed");
372 gl.activeTexture(GL_TEXTURE0);
373 gl.bindTexture(GL_TEXTURE_CUBE_MAP, srcTex);
374 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
375 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
376 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_STENCIL_INDEX);
377 GLU_EXPECT_NO_ERROR(gl.getError(), "Texture state setup failed");
379 gl.useProgram(program.getProgram());
380 gl.uniform1i(gl.getUniformLocation(program.getProgram(), "u_sampler"), 0);
381 GLU_EXPECT_NO_ERROR(gl.getError(), "Program setup failed");
383 gl.viewport(0, 0, width, height);
384 glu::draw(renderCtx, program.getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), &vertexArrays[0],
385 glu::pr::Triangles(DE_LENGTH_OF_ARRAY(indices), &indices[0]));
388 static inline tcu::ConstPixelBufferAccess stencilToRedAccess (const tcu::ConstPixelBufferAccess& access)
390 DE_ASSERT(access.getFormat() == TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8));
391 return tcu::ConstPixelBufferAccess(TextureFormat(TextureFormat::R, TextureFormat::UNSIGNED_INT8), access.getSize(), access.getPitch(), access.getDataPtr());
394 static bool compareStencilToRed (tcu::TestLog& log, const tcu::ConstPixelBufferAccess& stencilRef, const tcu::ConstPixelBufferAccess& result)
396 const int maxPrints = 10;
399 DE_ASSERT(stencilRef.getFormat().order == TextureFormat::S);
400 DE_ASSERT(stencilRef.getWidth() == result.getWidth() && stencilRef.getHeight() == result.getHeight());
402 for (int y = 0; y < stencilRef.getHeight(); y++)
404 for (int x = 0; x < stencilRef.getWidth(); x++)
406 const int ref = stencilRef.getPixStencil(x, y);
407 const int res = result.getPixelInt(x, y).x();
411 if (numFailed < maxPrints)
412 log << TestLog::Message << "ERROR: Expected " << ref << ", got " << res << " at (" << x << ", " << y << ")" << TestLog::EndMessage;
413 else if (numFailed == maxPrints)
414 log << TestLog::Message << "..." << TestLog::EndMessage;
421 log << TestLog::Message << "Found " << numFailed << " faulty pixels, comparison " << (numFailed == 0 ? "passed." : "FAILED!") << TestLog::EndMessage;
423 log << TestLog::ImageSet("ComparisonResult", "Image comparison result")
424 << TestLog::Image("Result", "Result stencil buffer", result);
427 log << TestLog::Image("Reference", "Reference stencil buffer", stencilToRedAccess(stencilRef));
429 log << TestLog::EndImageSet;
431 return numFailed == 0;
434 static bool compareRedChannel (tcu::TestLog& log, const tcu::ConstPixelBufferAccess& result, int reference)
436 const int maxPrints = 10;
439 for (int y = 0; y < result.getHeight(); y++)
441 for (int x = 0; x < result.getWidth(); x++)
443 const int res = result.getPixelInt(x, y).x();
445 if (reference != res)
447 if (numFailed < maxPrints)
448 log << TestLog::Message << "ERROR: Expected " << reference << ", got " << res << " at (" << x << ", " << y << ")" << TestLog::EndMessage;
449 else if (numFailed == maxPrints)
450 log << TestLog::Message << "..." << TestLog::EndMessage;
457 log << TestLog::Message << "Found " << numFailed << " faulty pixels, comparison " << (numFailed == 0 ? "passed." : "FAILED!") << TestLog::EndMessage;
459 log << TestLog::ImageSet("ComparisonResult", "Image comparison result")
460 << TestLog::Image("Result", "Result stencil buffer", result);
462 log << TestLog::EndImageSet;
464 return numFailed == 0;
467 static void stencilToUnorm8 (const tcu::TextureCube& src, tcu::TextureCube& dst)
469 for (int levelNdx = 0; levelNdx < src.getNumLevels(); levelNdx++)
471 for (int faceNdx = 0; faceNdx < tcu::CUBEFACE_LAST; faceNdx++)
473 const tcu::CubeFace face = tcu::CubeFace(faceNdx);
475 if (!src.isLevelEmpty(face, levelNdx))
477 dst.allocLevel(face, levelNdx);
479 const tcu::ConstPixelBufferAccess srcLevel = src.getLevelFace(levelNdx, face);
480 const tcu::PixelBufferAccess dstLevel = dst.getLevelFace(levelNdx, face);
482 for (int y = 0; y < src.getSize(); y++)
483 for (int x = 0; x < src.getSize(); x++)
484 dstLevel.setPixel(Vec4(float(srcLevel.getPixStencil(x, y)) / 255.f, 0.f, 0.f, 1.f), x, y);
490 static void checkDepthStencilFormatSupport (const glu::ContextInfo& ctxInfo, deUint32 format)
492 if (format == GL_STENCIL_INDEX8)
494 const char* reqExt = "GL_OES_texture_stencil8";
495 if (!ctxInfo.isExtensionSupported(reqExt))
496 throw tcu::NotSupportedError(glu::getTextureFormatStr(format).toString() + " requires " + reqExt);
500 DE_ASSERT(format == GL_DEPTH32F_STENCIL8 || format == GL_DEPTH24_STENCIL8);
504 static void checkFramebufferStatus (const glw::Functions& gl)
506 const deUint32 status = gl.checkFramebufferStatus(GL_FRAMEBUFFER);
508 if (status == GL_FRAMEBUFFER_UNSUPPORTED)
509 throw tcu::NotSupportedError("Unsupported framebuffer configuration");
510 else if (status != GL_FRAMEBUFFER_COMPLETE)
511 throw tcu::TestError("Incomplete framebuffer: " + glu::getFramebufferStatusStr(status).toString());
514 class UploadTex2DCase : public TestCase
517 UploadTex2DCase (Context& context, const char* name, deUint32 format)
518 : TestCase (context, name, glu::getTextureFormatName(format))
523 IterateResult iterate (void)
525 const glu::RenderContext& renderCtx = m_context.getRenderContext();
526 const glw::Functions& gl = renderCtx.getFunctions();
527 const int width = 129;
528 const int height = 113;
529 glu::Framebuffer fbo (renderCtx);
530 glu::Renderbuffer colorBuf (renderCtx);
531 glu::Texture depthStencilTex (renderCtx);
532 TextureLevel uploadLevel (glu::mapGLInternalFormat(m_format), width, height);
533 TextureLevel readLevel (TextureFormat(TextureFormat::RGBA, TextureFormat::UNSIGNED_INT32), width, height);
534 TextureLevel stencilOnlyLevel (TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8), width, height);
536 checkDepthStencilFormatSupport(m_context.getContextInfo(), m_format);
538 renderTestPatternReference(uploadLevel);
539 renderTestPatternReference(stencilOnlyLevel);
541 gl.bindTexture(GL_TEXTURE_2D, *depthStencilTex);
542 gl.texStorage2D(GL_TEXTURE_2D, 1, m_format, width, height);
543 glu::texSubImage2D(renderCtx, GL_TEXTURE_2D, 0, 0, 0, uploadLevel);
544 GLU_EXPECT_NO_ERROR(gl.getError(), "Uploading texture data failed");
546 gl.bindRenderbuffer(GL_RENDERBUFFER, *colorBuf);
547 gl.renderbufferStorage(GL_RENDERBUFFER, GL_R32UI, width, height);
549 gl.bindFramebuffer(GL_FRAMEBUFFER, *fbo);
550 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *colorBuf);
551 checkFramebufferStatus(gl);
553 blitStencilToColor2D(renderCtx, *depthStencilTex, width, height);
554 glu::readPixels(renderCtx, 0, 0, readLevel);
557 const bool compareOk = compareStencilToRed(m_testCtx.getLog(), stencilOnlyLevel, readLevel);
558 m_testCtx.setTestResult(compareOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
559 compareOk ? "Pass" : "Image comparison failed");
566 const deUint32 m_format;
569 class UploadTex2DArrayCase : public TestCase
572 UploadTex2DArrayCase (Context& context, const char* name, deUint32 format)
573 : TestCase (context, name, glu::getTextureFormatName(format))
578 IterateResult iterate (void)
580 const glu::RenderContext& renderCtx = m_context.getRenderContext();
581 const glw::Functions& gl = renderCtx.getFunctions();
582 const int width = 41;
583 const int height = 13;
584 const int levels = 7;
585 const int ptrnLevel = 3;
586 glu::Framebuffer fbo (renderCtx);
587 glu::Renderbuffer colorBuf (renderCtx);
588 glu::Texture depthStencilTex (renderCtx);
589 TextureLevel uploadLevel (glu::mapGLInternalFormat(m_format), width, height, levels);
591 checkDepthStencilFormatSupport(m_context.getContextInfo(), m_format);
593 for (int levelNdx = 0; levelNdx < levels; levelNdx++)
595 const tcu::PixelBufferAccess levelAccess = tcu::getSubregion(uploadLevel.getAccess(), 0, 0, levelNdx, width, height, 1);
597 if (levelNdx == ptrnLevel)
598 renderTestPatternReference(levelAccess);
600 tcu::clearStencil(levelAccess, levelNdx);
603 gl.bindTexture(GL_TEXTURE_2D_ARRAY, *depthStencilTex);
604 gl.texStorage3D(GL_TEXTURE_2D_ARRAY, 1, m_format, width, height, levels);
605 glu::texSubImage3D(renderCtx, GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, uploadLevel);
606 GLU_EXPECT_NO_ERROR(gl.getError(), "Uploading texture data failed");
608 gl.bindRenderbuffer(GL_RENDERBUFFER, *colorBuf);
609 gl.renderbufferStorage(GL_RENDERBUFFER, GL_R32UI, width, height);
611 gl.bindFramebuffer(GL_FRAMEBUFFER, *fbo);
612 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *colorBuf);
613 checkFramebufferStatus(gl);
616 TextureLevel readLevel (TextureFormat(TextureFormat::RGBA, TextureFormat::UNSIGNED_INT32), width, height);
617 bool allLevelsOk = true;
619 for (int levelNdx = 0; levelNdx < levels; levelNdx++)
621 tcu::ScopedLogSection section(m_testCtx.getLog(), "Level" + de::toString(levelNdx), "Level " + de::toString(levelNdx));
624 blitStencilToColor2DArray(renderCtx, *depthStencilTex, width, height, levelNdx);
625 glu::readPixels(renderCtx, 0, 0, readLevel);
627 if (levelNdx == ptrnLevel)
629 TextureLevel reference(TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8), width, height);
630 renderTestPatternReference(reference);
632 levelOk = compareStencilToRed(m_testCtx.getLog(), reference, readLevel);
635 levelOk = compareRedChannel(m_testCtx.getLog(), readLevel, levelNdx);
644 m_testCtx.setTestResult(allLevelsOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
645 allLevelsOk ? "Pass" : "Image comparison failed");
652 const deUint32 m_format;
655 class UploadTexCubeCase : public TestCase
658 UploadTexCubeCase (Context& context, const char* name, deUint32 format)
659 : TestCase (context, name, glu::getTextureFormatName(format))
664 IterateResult iterate (void)
666 const glu::RenderContext& renderCtx = m_context.getRenderContext();
667 const glw::Functions& gl = renderCtx.getFunctions();
669 const int renderWidth = 128;
670 const int renderHeight = 128;
671 vector<float> texCoord;
672 glu::Framebuffer fbo (renderCtx);
673 glu::Renderbuffer colorBuf (renderCtx);
674 glu::Texture depthStencilTex (renderCtx);
675 tcu::TextureCube texData (glu::mapGLInternalFormat(m_format), size);
676 tcu::TextureLevel result (TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8), renderWidth, renderHeight);
678 checkDepthStencilFormatSupport(m_context.getContextInfo(), m_format);
680 for (int faceNdx = 0; faceNdx < tcu::CUBEFACE_LAST; faceNdx++)
682 const tcu::CubeFace face = tcu::CubeFace(faceNdx);
683 const int stencilVal = 42*faceNdx;
685 texData.allocLevel(face, 0);
686 tcu::clearStencil(texData.getLevelFace(0, face), stencilVal);
689 glu::TextureTestUtil::computeQuadTexCoordCube(texCoord, tcu::CUBEFACE_NEGATIVE_X, Vec2(-1.5f, -1.3f), Vec2(1.3f, 1.4f));
691 gl.bindTexture(GL_TEXTURE_CUBE_MAP, *depthStencilTex);
692 gl.texStorage2D(GL_TEXTURE_CUBE_MAP, 1, m_format, size, size);
694 for (int faceNdx = 0; faceNdx < tcu::CUBEFACE_LAST; faceNdx++)
695 glu::texSubImage2D(renderCtx, glu::getGLCubeFace(tcu::CubeFace(faceNdx)), 0, 0, 0, texData.getLevelFace(0, tcu::CubeFace(faceNdx)));
697 GLU_EXPECT_NO_ERROR(gl.getError(), "Uploading texture data failed");
699 gl.bindRenderbuffer(GL_RENDERBUFFER, *colorBuf);
700 gl.renderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, renderWidth, renderHeight);
702 gl.bindFramebuffer(GL_FRAMEBUFFER, *fbo);
703 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *colorBuf);
704 checkFramebufferStatus(gl);
706 blitStencilToColorCube(renderCtx, *depthStencilTex, &texCoord[0], renderWidth, renderHeight);
707 glu::readPixels(renderCtx, 0, 0, result);
710 using namespace glu::TextureTestUtil;
712 tcu::TextureCube redTex (TextureFormat(TextureFormat::R, TextureFormat::UNORM_INT8), size);
713 const ReferenceParams sampleParams (TEXTURETYPE_CUBE, tcu::Sampler(tcu::Sampler::CLAMP_TO_EDGE,
714 tcu::Sampler::CLAMP_TO_EDGE,
715 tcu::Sampler::CLAMP_TO_EDGE,
716 tcu::Sampler::NEAREST,
717 tcu::Sampler::NEAREST));
718 tcu::LookupPrecision lookupPrec;
719 tcu::LodPrecision lodPrec (tcu::LodPrecision::RULE_OPENGL);
722 lookupPrec.colorMask = tcu::BVec4(true, true, true, true);
723 lookupPrec.colorThreshold = tcu::computeFixedPointThreshold(IVec4(8, 8, 8, 8));
724 lookupPrec.coordBits = tcu::IVec3(22, 22, 22);
725 lookupPrec.uvwBits = tcu::IVec3(5, 5, 0);
727 lodPrec.derivateBits = 16;
729 stencilToUnorm8(texData, redTex);
731 compareOk = verifyTextureResult(m_testCtx, result, redTex, &texCoord[0], sampleParams, lookupPrec, lodPrec, tcu::PixelFormat(8, 8, 8, 8));
733 m_testCtx.setTestResult(compareOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
734 compareOk ? "Pass" : "Image comparison failed");
741 const deUint32 m_format;
744 class RenderTex2DCase : public TestCase
747 RenderTex2DCase (Context& context, const char* name, deUint32 format)
748 : TestCase (context, name, glu::getTextureFormatName(format))
753 IterateResult iterate (void)
755 const glu::RenderContext& renderCtx = m_context.getRenderContext();
756 const glw::Functions& gl = renderCtx.getFunctions();
757 const int width = 117;
758 const int height = 193;
759 glu::Framebuffer fbo (renderCtx);
760 glu::Renderbuffer colorBuf (renderCtx);
761 glu::Texture depthStencilTex (renderCtx);
762 TextureLevel result (TextureFormat(TextureFormat::RGBA, TextureFormat::UNSIGNED_INT32), width, height);
763 TextureLevel reference (TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8), width, height);
765 checkDepthStencilFormatSupport(m_context.getContextInfo(), m_format);
767 gl.bindRenderbuffer(GL_RENDERBUFFER, *colorBuf);
768 gl.renderbufferStorage(GL_RENDERBUFFER, GL_R32UI, width, height);
770 gl.bindTexture(GL_TEXTURE_2D, *depthStencilTex);
771 gl.texStorage2D(GL_TEXTURE_2D, 1, m_format, width, height);
773 gl.bindFramebuffer(GL_FRAMEBUFFER, *fbo);
774 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *colorBuf);
775 gl.framebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, *depthStencilTex, 0);
776 checkFramebufferStatus(gl);
778 drawTestPattern(renderCtx, width, height);
780 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
781 checkFramebufferStatus(gl);
783 blitStencilToColor2D(renderCtx, *depthStencilTex, width, height);
784 glu::readPixels(renderCtx, 0, 0, result.getAccess());
786 renderTestPatternReference(reference);
789 const bool compareOk = compareStencilToRed(m_testCtx.getLog(), reference, result);
790 m_testCtx.setTestResult(compareOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
791 compareOk ? "Pass" : "Image comparison failed");
798 const deUint32 m_format;
801 class ClearTex2DCase : public TestCase
804 ClearTex2DCase (Context& context, const char* name, deUint32 format)
805 : TestCase (context, name, glu::getTextureFormatName(format))
810 IterateResult iterate (void)
812 const glu::RenderContext& renderCtx = m_context.getRenderContext();
813 const glw::Functions& gl = renderCtx.getFunctions();
814 const int width = 125;
815 const int height = 117;
816 const int cellSize = 8;
817 glu::Framebuffer fbo (renderCtx);
818 glu::Renderbuffer colorBuf (renderCtx);
819 glu::Texture depthStencilTex (renderCtx);
820 TextureLevel result (TextureFormat(TextureFormat::RGBA, TextureFormat::UNSIGNED_INT32), width, height);
821 TextureLevel reference (TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8), width, height);
823 checkDepthStencilFormatSupport(m_context.getContextInfo(), m_format);
825 gl.bindRenderbuffer(GL_RENDERBUFFER, *colorBuf);
826 gl.renderbufferStorage(GL_RENDERBUFFER, GL_R32UI, width, height);
828 gl.bindTexture(GL_TEXTURE_2D, *depthStencilTex);
829 gl.texStorage2D(GL_TEXTURE_2D, 1, m_format, width, height);
831 gl.bindFramebuffer(GL_FRAMEBUFFER, *fbo);
832 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *colorBuf);
833 gl.framebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, *depthStencilTex, 0);
834 checkFramebufferStatus(gl);
836 gl.enable(GL_SCISSOR_TEST);
838 for (int y = 0; y < height; y += cellSize)
840 for (int x = 0; x < width; x += cellSize)
842 const int clearW = de::min(cellSize, width-x);
843 const int clearH = de::min(cellSize, height-y);
844 const int stencil = int((deInt32Hash(x) ^ deInt32Hash(y)) & 0xff);
846 gl.clearStencil(stencil);
847 gl.scissor(x, y, clearW, clearH);
848 gl.clear(GL_STENCIL_BUFFER_BIT);
850 tcu::clearStencil(tcu::getSubregion(reference.getAccess(), x, y, clearW, clearH), stencil);
854 gl.disable(GL_SCISSOR_TEST);
856 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
857 checkFramebufferStatus(gl);
859 blitStencilToColor2D(renderCtx, *depthStencilTex, width, height);
860 glu::readPixels(renderCtx, 0, 0, result.getAccess());
863 const bool compareOk = compareStencilToRed(m_testCtx.getLog(), reference, result);
864 m_testCtx.setTestResult(compareOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
865 compareOk ? "Pass" : "Image comparison failed");
872 const deUint32 m_format;
875 class CompareModeCase : public TestCase
878 CompareModeCase (Context& context, const char* name, deUint32 format)
879 : TestCase (context, name, glu::getTextureFormatName(format))
884 IterateResult iterate (void)
886 const glu::RenderContext& renderCtx = m_context.getRenderContext();
887 const glw::Functions& gl = renderCtx.getFunctions();
888 const int width = 64;
889 const int height = 64;
890 glu::Framebuffer fbo (renderCtx);
891 glu::Renderbuffer colorBuf (renderCtx);
892 glu::Texture depthStencilTex (renderCtx);
893 TextureLevel uploadLevel (glu::mapGLInternalFormat(m_format), width, height);
894 TextureLevel readLevel (TextureFormat(TextureFormat::RGBA, TextureFormat::UNSIGNED_INT32), width, height);
895 TextureLevel stencilOnlyLevel (TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8), width, height);
897 checkDepthStencilFormatSupport(m_context.getContextInfo(), m_format);
899 m_testCtx.getLog() << TestLog::Message << "NOTE: Texture compare mode has no effect when reading stencil values." << TestLog::EndMessage;
901 renderTestPatternReference(uploadLevel);
902 renderTestPatternReference(stencilOnlyLevel);
904 gl.bindTexture(GL_TEXTURE_2D, *depthStencilTex);
905 gl.texStorage2D(GL_TEXTURE_2D, 1, m_format, width, height);
906 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
907 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
908 glu::texSubImage2D(renderCtx, GL_TEXTURE_2D, 0, 0, 0, uploadLevel);
909 GLU_EXPECT_NO_ERROR(gl.getError(), "Uploading texture data failed");
911 gl.bindRenderbuffer(GL_RENDERBUFFER, *colorBuf);
912 gl.renderbufferStorage(GL_RENDERBUFFER, GL_R32UI, width, height);
914 gl.bindFramebuffer(GL_FRAMEBUFFER, *fbo);
915 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *colorBuf);
916 checkFramebufferStatus(gl);
918 blitStencilToColor2D(renderCtx, *depthStencilTex, width, height);
919 glu::readPixels(renderCtx, 0, 0, readLevel);
922 const bool compareOk = compareStencilToRed(m_testCtx.getLog(), stencilOnlyLevel, readLevel);
923 m_testCtx.setTestResult(compareOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
924 compareOk ? "Pass" : "Image comparison failed");
931 const deUint32 m_format;
934 class BaseLevelCase : public TestCase
937 BaseLevelCase (Context& context, const char* name, deUint32 format)
938 : TestCase (context, name, glu::getTextureFormatName(format))
943 IterateResult iterate (void)
945 const glu::RenderContext& renderCtx = m_context.getRenderContext();
946 const glw::Functions& gl = renderCtx.getFunctions();
947 const int width = 128;
948 const int height = 128;
949 const int levelNdx = 2;
950 const int levelWidth = width>>levelNdx;
951 const int levelHeight = height>>levelNdx;
952 glu::Framebuffer fbo (renderCtx);
953 glu::Renderbuffer colorBuf (renderCtx);
954 glu::Texture depthStencilTex (renderCtx);
955 TextureLevel uploadLevel (glu::mapGLInternalFormat(m_format), levelWidth, levelHeight);
956 TextureLevel readLevel (TextureFormat(TextureFormat::RGBA, TextureFormat::UNSIGNED_INT32), levelWidth, levelHeight);
957 TextureLevel stencilOnlyLevel (TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8), levelWidth, levelHeight);
959 checkDepthStencilFormatSupport(m_context.getContextInfo(), m_format);
961 m_testCtx.getLog() << TestLog::Message << "GL_TEXTURE_BASE_LEVEL = " << levelNdx << TestLog::EndMessage;
963 renderTestPatternReference(uploadLevel);
964 renderTestPatternReference(stencilOnlyLevel);
966 gl.bindTexture(GL_TEXTURE_2D, *depthStencilTex);
967 gl.texStorage2D(GL_TEXTURE_2D, deLog2Floor32(de::max(width, height))+1, m_format, width, height);
968 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, levelNdx);
969 glu::texSubImage2D(renderCtx, GL_TEXTURE_2D, levelNdx, 0, 0, uploadLevel);
970 GLU_EXPECT_NO_ERROR(gl.getError(), "Uploading texture data failed");
972 gl.bindRenderbuffer(GL_RENDERBUFFER, *colorBuf);
973 gl.renderbufferStorage(GL_RENDERBUFFER, GL_R32UI, levelWidth, levelHeight);
975 gl.bindFramebuffer(GL_FRAMEBUFFER, *fbo);
976 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *colorBuf);
977 checkFramebufferStatus(gl);
979 blitStencilToColor2D(renderCtx, *depthStencilTex, levelWidth, levelHeight);
980 glu::readPixels(renderCtx, 0, 0, readLevel);
983 const bool compareOk = compareStencilToRed(m_testCtx.getLog(), stencilOnlyLevel, readLevel);
984 m_testCtx.setTestResult(compareOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
985 compareOk ? "Pass" : "Image comparison failed");
992 const deUint32 m_format;
997 StencilTexturingTests::StencilTexturingTests (Context& context)
998 : TestCaseGroup(context, "stencil_texturing", "Stencil texturing tests")
1002 StencilTexturingTests::~StencilTexturingTests (void)
1006 void StencilTexturingTests::init (void)
1010 tcu::TestCaseGroup* const formatGroup = new tcu::TestCaseGroup(m_testCtx, "format", "Formats");
1011 addChild(formatGroup);
1013 formatGroup->addChild(new UploadTex2DCase (m_context, "depth32f_stencil8_2d", GL_DEPTH32F_STENCIL8));
1014 formatGroup->addChild(new UploadTex2DArrayCase (m_context, "depth32f_stencil8_2d_array", GL_DEPTH32F_STENCIL8));
1015 formatGroup->addChild(new UploadTexCubeCase (m_context, "depth32f_stencil8_cube", GL_DEPTH32F_STENCIL8));
1016 formatGroup->addChild(new UploadTex2DCase (m_context, "depth24_stencil8_2d", GL_DEPTH24_STENCIL8));
1017 formatGroup->addChild(new UploadTex2DArrayCase (m_context, "depth24_stencil8_2d_array", GL_DEPTH24_STENCIL8));
1018 formatGroup->addChild(new UploadTexCubeCase (m_context, "depth24_stencil8_cube", GL_DEPTH24_STENCIL8));
1020 // OES_texture_stencil8
1021 formatGroup->addChild(new UploadTex2DCase (m_context, "stencil_index8_2d", GL_STENCIL_INDEX8));
1022 formatGroup->addChild(new UploadTex2DArrayCase (m_context, "stencil_index8_2d_array", GL_STENCIL_INDEX8));
1023 formatGroup->addChild(new UploadTexCubeCase (m_context, "stencil_index8_cube", GL_STENCIL_INDEX8));
1028 tcu::TestCaseGroup* const readRenderGroup = new tcu::TestCaseGroup(m_testCtx, "render", "Read rendered stencil values");
1029 addChild(readRenderGroup);
1031 readRenderGroup->addChild(new ClearTex2DCase (m_context, "depth32f_stencil8_clear", GL_DEPTH32F_STENCIL8));
1032 readRenderGroup->addChild(new RenderTex2DCase (m_context, "depth32f_stencil8_draw", GL_DEPTH32F_STENCIL8));
1033 readRenderGroup->addChild(new ClearTex2DCase (m_context, "depth24_stencil8_clear", GL_DEPTH24_STENCIL8));
1034 readRenderGroup->addChild(new RenderTex2DCase (m_context, "depth24_stencil8_draw", GL_DEPTH24_STENCIL8));
1039 tcu::TestCaseGroup* const miscGroup = new tcu::TestCaseGroup(m_testCtx, "misc", "Misc cases");
1040 addChild(miscGroup);
1042 miscGroup->addChild(new CompareModeCase (m_context, "compare_mode_effect", GL_DEPTH24_STENCIL8));
1043 miscGroup->addChild(new BaseLevelCase (m_context, "base_level", GL_DEPTH24_STENCIL8));