1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 3.0 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 Depth and stencil clear tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es3fDepthStencilClearTests.hpp"
26 #include "gluShaderProgram.hpp"
27 #include "gluPixelTransfer.hpp"
28 #include "gluRenderContext.hpp"
30 #include "tcuTestLog.hpp"
31 #include "tcuTexture.hpp"
32 #include "tcuTextureUtil.hpp"
33 #include "tcuImageCompare.hpp"
34 #include "tcuSurface.hpp"
35 #include "tcuRenderTarget.hpp"
37 #include "deRandom.hpp"
41 #include "glwFunctions.hpp"
42 #include "glwEnums.hpp"
73 , scissor (0, 0, 0, 0)
90 tcu::TextureFormat getDepthFormat (int depthBits)
94 case 8: return tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::UNORM_INT8);
95 case 16: return tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::UNORM_INT16);
96 case 24: return tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::UNORM_INT24);
97 case 32: return tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::FLOAT);
99 TCU_FAIL("Can't map depth buffer format");
103 tcu::TextureFormat getStencilFormat (int stencilBits)
107 case 8: return tcu::TextureFormat(tcu::TextureFormat::S, tcu::TextureFormat::UNSIGNED_INT8);
108 case 16: return tcu::TextureFormat(tcu::TextureFormat::S, tcu::TextureFormat::UNSIGNED_INT16);
109 case 24: return tcu::TextureFormat(tcu::TextureFormat::S, tcu::TextureFormat::UNSIGNED_INT24);
110 case 32: return tcu::TextureFormat(tcu::TextureFormat::S, tcu::TextureFormat::UNSIGNED_INT32);
112 TCU_FAIL("Can't map depth buffer format");
118 class DepthStencilClearCase : public TestCase
121 DepthStencilClearCase (Context& context, const char* name, const char* description, int numIters, int numClears, bool depth, bool stencil, bool scissor, bool masked);
122 ~DepthStencilClearCase (void);
127 IterateResult iterate (void);
130 void generateClears (vector<Clear>& dst, deUint32 seed);
131 void renderGL (tcu::Surface& dst, const vector<Clear>& clears);
132 void renderReference (tcu::Surface& dst, const vector<Clear>& clears);
142 glu::ShaderProgram* m_visProgram;
145 DepthStencilClearCase::DepthStencilClearCase (Context& context, const char* name, const char* description, int numIters, int numClears, bool depth, bool stencil, bool scissor, bool masked)
146 : TestCase (context, name, description)
147 , m_testDepth (depth)
148 , m_testStencil (stencil)
149 , m_testScissor (scissor)
151 , m_numIters (numIters)
152 , m_numClears (numClears)
154 , m_visProgram (DE_NULL)
158 DepthStencilClearCase::~DepthStencilClearCase (void)
160 DepthStencilClearCase::deinit();
163 void DepthStencilClearCase::init (void)
165 TestLog& log = m_testCtx.getLog();
167 m_visProgram = new glu::ShaderProgram(m_context.getRenderContext(), glu::makeVtxFragSources(
170 "in highp vec4 a_position;\n"
173 " gl_Position = a_position;\n"
178 "uniform mediump vec4 u_color;\n"
179 "layout(location = 0) out mediump vec4 o_color;\n"
182 " o_color = u_color;\n"
185 if (!m_visProgram->isOk())
187 log << *m_visProgram;
189 m_visProgram = DE_NULL;
190 TCU_FAIL("Compile failed");
193 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
196 void DepthStencilClearCase::deinit (void)
199 m_visProgram = DE_NULL;
202 DepthStencilClearCase::IterateResult DepthStencilClearCase::iterate (void)
204 const tcu::RenderTarget& renderTarget = m_context.getRenderTarget();
205 int width = renderTarget.getWidth();
206 int height = renderTarget.getHeight();
207 tcu::Surface result (width, height);
208 tcu::Surface reference (width, height);
209 tcu::RGBA threshold = renderTarget.getPixelFormat().getColorThreshold() + tcu::RGBA(1,1,1,1);
210 vector<Clear> clears;
212 if ((m_testDepth && renderTarget.getDepthBits() == 0) ||
213 (m_testStencil && renderTarget.getStencilBits() == 0))
214 throw tcu::NotSupportedError("No depth/stencil buffers", "", __FILE__, __LINE__);
216 generateClears(clears, deStringHash(getName())^deInt32Hash(m_curIter));
217 renderGL(result, clears);
218 renderReference(reference, clears);
220 bool isLastIter = m_curIter+1 == m_numIters;
221 bool isOk = tcu::pixelThresholdCompare(m_testCtx.getLog(), "Result", "Image comparison result", reference, result, threshold, isLastIter ? tcu::COMPARE_LOG_RESULT : tcu::COMPARE_LOG_ON_ERROR);
224 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Image comparison failed");
227 return isLastIter || !isOk ? STOP : CONTINUE;
230 void DepthStencilClearCase::generateClears (vector<Clear>& clears, deUint32 seed)
232 const tcu::RenderTarget& renderTarget = m_context.getRenderContext().getRenderTarget();
233 int width = renderTarget.getWidth();
234 int height = renderTarget.getHeight();
235 de::Random rnd (seed);
237 clears.resize(m_numClears);
239 for (vector<Clear>::iterator clear = clears.begin(); clear != clears.end(); clear++)
243 int w = rnd.getInt(1, width);
244 int h = rnd.getInt(1, height);
245 int x = rnd.getInt(0, width-w);
246 int y = rnd.getInt(0, height-h);
248 clear->useScissor = true; // \todo [pyry] Should we randomize?
249 clear->scissor = tcu::IVec4(x, y, w, h);
252 clear->useScissor = false;
254 clear->clearDepth = rnd.getFloat(-0.2f, 1.2f);
255 clear->clearStencil = rnd.getUint32();
257 clear->depthMask = m_masked ? rnd.getBool() : true;
258 clear->stencilMask = m_masked ? rnd.getUint32() : 0xffffffffu;
260 if (m_testDepth && m_testStencil)
262 switch (rnd.getInt(0, 2))
264 case 0: clear->clearMask = GL_DEPTH_BUFFER_BIT; break;
265 case 1: clear->clearMask = GL_STENCIL_BUFFER_BIT; break;
266 case 2: clear->clearMask = GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT; break;
269 else if (m_testDepth)
270 clear->clearMask = GL_DEPTH_BUFFER_BIT;
273 DE_ASSERT(m_testStencil);
274 clear->clearMask = GL_STENCIL_BUFFER_BIT;
279 void DepthStencilClearCase::renderGL (tcu::Surface& dst, const vector<Clear>& clears)
281 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
282 int colorLoc = gl.getUniformLocation(m_visProgram->getProgram(), "u_color");
283 int positionLoc = gl.getAttribLocation(m_visProgram->getProgram(), "a_position");
284 static const deUint8 indices[] = { 0, 1, 2, 2, 1, 3 };
286 // Clear with default values.
287 gl.clearDepthf (1.0f);
289 gl.clearColor (1.0f, 0.0f, 0.0f, 1.0f);
290 gl.clear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
292 GLU_EXPECT_NO_ERROR(gl.getError(), "Before clears");
294 for (vector<Clear>::const_iterator clear = clears.begin(); clear != clears.end(); clear++)
296 if (clear->useScissor)
298 gl.enable(GL_SCISSOR_TEST);
299 gl.scissor(clear->scissor.x(), clear->scissor.y(), clear->scissor.z(), clear->scissor.w());
303 gl.clearDepthf (clear->clearDepth);
304 gl.clearStencil (clear->clearStencil);
307 gl.depthMask (clear->depthMask ? GL_TRUE : GL_FALSE);
308 gl.stencilMask (clear->stencilMask);
311 gl.clear (clear->clearMask);
313 if (clear->useScissor)
314 gl.disable(GL_SCISSOR_TEST);
317 // Restore default masks.
318 gl.depthMask (GL_TRUE);
319 gl.stencilMask (0xffffffffu);
321 GLU_EXPECT_NO_ERROR(gl.getError(), "After clears");
323 gl.useProgram (m_visProgram->getProgram());
324 gl.enableVertexAttribArray (positionLoc);
326 // Visualize depth / stencil buffers.
329 int numSteps = DEPTH_STEPS;
330 float step = 2.0f / (float)numSteps;
332 gl.enable (GL_DEPTH_TEST);
333 gl.depthFunc(GL_LESS);
334 gl.depthMask(GL_FALSE);
335 gl.colorMask(GL_FALSE, GL_FALSE, GL_TRUE, GL_FALSE);
337 for (int ndx = 0; ndx < numSteps; ndx++)
339 float d = -1.0f + step*(float)ndx;
340 float c = (float)ndx / (float)(numSteps-1);
349 gl.uniform4f (colorLoc, 0.0f, 0.0f, c, 1.0f);
350 gl.vertexAttribPointer (positionLoc, 3, GL_FLOAT, GL_FALSE, 0, &pos[0]);
351 gl.drawElements (GL_TRIANGLES, DE_LENGTH_OF_ARRAY(indices), GL_UNSIGNED_BYTE, &indices[0]);
354 gl.disable (GL_DEPTH_TEST);
355 gl.depthMask(GL_TRUE);
357 GLU_EXPECT_NO_ERROR(gl.getError(), "After depth visualization");
362 int numSteps = STENCIL_STEPS;
363 int numValues = (1 << TestCase::m_context.getRenderContext().getRenderTarget().getStencilBits()); // 2^bits
364 int step = numValues / numSteps;
366 gl.enable (GL_STENCIL_TEST);
367 gl.stencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
368 gl.colorMask (GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE);
370 static const float pos[] =
377 gl.vertexAttribPointer(positionLoc, 2, GL_FLOAT, GL_FALSE, 0, &pos[0]);
379 for (int ndx = 0; ndx < numSteps; ndx++)
382 float c = (float)ndx / (float)(numSteps-1);
384 gl.stencilFunc (GL_LEQUAL, s, 0xffu);
385 gl.uniform4f (colorLoc, 0.0f, c, 0.0f, 1.0f);
386 gl.drawElements (GL_TRIANGLES, DE_LENGTH_OF_ARRAY(indices), GL_UNSIGNED_BYTE, &indices[0]);
389 gl.disable(GL_STENCIL_TEST);
391 GLU_EXPECT_NO_ERROR(gl.getError(), "After stencil visualization");
394 // Restore color mask (changed by visualization).
395 gl.colorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
397 glu::readPixels(m_context.getRenderContext(), 0, 0, dst.getAccess());
400 void DepthStencilClearCase::renderReference (tcu::Surface& dst, const vector<Clear>& clears)
402 glu::RenderContext& renderCtx = TestCase::m_context.getRenderContext();
403 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
405 // Clear surface to red.
406 tcu::clear(dst.getAccess(), tcu::RGBA::red().toVec());
410 // Simulated depth buffer span.
411 tcu::TextureLevel depthBufRow (getDepthFormat(renderTarget.getDepthBits()), dst.getWidth(), 1, 1);
412 tcu::PixelBufferAccess rowAccess = depthBufRow.getAccess();
414 for (int y = 0; y < dst.getHeight(); y++)
416 // Clear to default value.
417 for (int x = 0; x < rowAccess.getWidth(); x++)
418 rowAccess.setPixel(Vec4(1.0f), x, 0);
421 for (vector<Clear>::const_iterator clear = clears.begin(); clear != clears.end(); clear++)
423 // Clear / mask test.
424 if ((clear->clearMask & GL_DEPTH_BUFFER_BIT) == 0 || !clear->depthMask)
427 tcu::IVec4 clearRect = clear->useScissor ? clear->scissor : tcu::IVec4(0, 0, dst.getWidth(), dst.getHeight());
429 // Intersection test.
430 if (!de::inBounds(y, clearRect.y(), clearRect.y()+clearRect.w()))
433 for (int x = clearRect.x(); x < clearRect.x()+clearRect.z(); x++)
434 rowAccess.setPixDepth(de::clamp(clear->clearDepth, 0.0f, 1.0f), x, 0);
438 for (int x = 0; x < dst.getWidth(); x++)
440 float depth = rowAccess.getPixDepth(x, 0);
441 float step = deFloatFloor(depth * (float)DEPTH_STEPS) / (float)(DEPTH_STEPS-1);
442 tcu::RGBA oldColor = dst.getPixel(x, y);
443 tcu::RGBA newColor = tcu::RGBA(oldColor.getRed(), oldColor.getGreen(), deClamp32(deRoundFloatToInt32(step * 255.0f), 0, 255), oldColor.getAlpha());
445 dst.setPixel(x, y, newColor);
452 // Simulated stencil buffer span.
453 int stencilBits = renderTarget.getStencilBits();
454 tcu::TextureLevel depthBufRow (getStencilFormat(stencilBits), dst.getWidth(), 1, 1);
455 tcu::PixelBufferAccess rowAccess = depthBufRow.getAccess();
456 deUint32 bufMask = (1u<<stencilBits)-1;
458 for (int y = 0; y < dst.getHeight(); y++)
460 // Clear to default value.
461 for (int x = 0; x < rowAccess.getWidth(); x++)
462 rowAccess.setPixel(tcu::UVec4(0), x, 0);
465 for (vector<Clear>::const_iterator clear = clears.begin(); clear != clears.end(); clear++)
467 // Clear / mask test.
468 if ((clear->clearMask & GL_STENCIL_BUFFER_BIT) == 0 || clear->stencilMask == 0)
471 tcu::IVec4 clearRect = clear->useScissor ? clear->scissor : tcu::IVec4(0, 0, dst.getWidth(), dst.getHeight());
473 // Intersection test.
474 if (!de::inBounds(y, clearRect.y(), clearRect.y()+clearRect.w()))
477 for (int x = clearRect.x(); x < clearRect.x()+clearRect.z(); x++)
479 deUint32 oldVal = rowAccess.getPixStencil(x, 0);
480 deUint32 newVal = ((oldVal & ~clear->stencilMask) | (clear->clearStencil & clear->stencilMask)) & bufMask;
481 rowAccess.setPixStencil(newVal, x, 0);
486 for (int x = 0; x < dst.getWidth(); x++)
488 deUint32 stencil = rowAccess.getPixStencil(x, 0);
489 float step = (float)(stencil / ((1u<<stencilBits) / (deUint32)STENCIL_STEPS)) / (float)(STENCIL_STEPS-1);
490 tcu::RGBA oldColor = dst.getPixel(x, y);
491 tcu::RGBA newColor = tcu::RGBA(oldColor.getRed(), deClamp32(deRoundFloatToInt32(step * 255.0f), 0, 255), oldColor.getBlue(), oldColor.getAlpha());
493 dst.setPixel(x, y, newColor);
499 DepthStencilClearTests::DepthStencilClearTests (Context& context)
500 : TestCaseGroup(context, "depth_stencil_clear", "Depth and stencil clear tests")
504 void DepthStencilClearTests::init (void)
506 // iters clears depth stencil scissor masked
507 addChild(new DepthStencilClearCase(m_context, "depth", "", 4, 2, true, false, false, false));
508 addChild(new DepthStencilClearCase(m_context, "depth_scissored", "", 4, 16, true, false, true, false));
509 addChild(new DepthStencilClearCase(m_context, "depth_scissored_masked", "", 4, 16, true, false, true, true));
511 addChild(new DepthStencilClearCase(m_context, "stencil", "", 4, 2, false, true, false, false));
512 addChild(new DepthStencilClearCase(m_context, "stencil_masked", "", 4, 8, false, true, false, true));
513 addChild(new DepthStencilClearCase(m_context, "stencil_scissored", "", 4, 16, false, true, true, false));
514 addChild(new DepthStencilClearCase(m_context, "stencil_scissored_masked", "", 4, 16, false, true, true, true));
516 addChild(new DepthStencilClearCase(m_context, "depth_stencil", "", 4, 2, true, true, false, false));
517 addChild(new DepthStencilClearCase(m_context, "depth_stencil_masked", "", 4, 8, true, true, false, true));
518 addChild(new DepthStencilClearCase(m_context, "depth_stencil_scissored", "", 4, 16, true, true, true, false));
519 addChild(new DepthStencilClearCase(m_context, "depth_stencil_scissored_masked", "", 4, 16, true, true, true, true));