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 FBO stencilbuffer tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es3fFboStencilbufferTests.hpp"
25 #include "es3fFboTestCase.hpp"
26 #include "es3fFboTestUtil.hpp"
27 #include "gluTextureUtil.hpp"
28 #include "tcuTextureUtil.hpp"
29 #include "sglrContextUtil.hpp"
30 #include "glwEnums.hpp"
47 using namespace FboTestUtil;
49 class BasicFboStencilCase : public FboTestCase
52 BasicFboStencilCase (Context& context, const char* name, const char* desc, deUint32 format, IVec2 size, bool useDepth)
53 : FboTestCase (context, name, desc)
56 , m_useDepth (useDepth)
63 checkFormatSupport(m_format);
66 void render (tcu::Surface& dst)
68 const deUint32 colorFormat = GL_RGBA8;
70 GradientShader gradShader (glu::TYPE_FLOAT_VEC4);
71 FlatColorShader flatShader (glu::TYPE_FLOAT_VEC4);
72 deUint32 flatShaderID = getCurrentContext()->createProgram(&flatShader);
73 deUint32 gradShaderID = getCurrentContext()->createProgram(&gradShader);
76 deUint32 colorRbo = 0;
77 deUint32 depthStencilRbo = 0;
80 glGenRenderbuffers(1, &colorRbo);
81 glBindRenderbuffer(GL_RENDERBUFFER, colorRbo);
82 glRenderbufferStorage(GL_RENDERBUFFER, colorFormat, m_size.x(), m_size.y());
84 // Stencil (and depth) buffer.
85 glGenRenderbuffers(1, &depthStencilRbo);
86 glBindRenderbuffer(GL_RENDERBUFFER, depthStencilRbo);
87 glRenderbufferStorage(GL_RENDERBUFFER, m_format, m_size.x(), m_size.y());
90 glGenFramebuffers(1, &fbo);
91 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
92 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRbo);
93 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthStencilRbo);
95 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthStencilRbo);
97 checkFramebufferStatus(GL_FRAMEBUFFER);
99 glViewport(0, 0, m_size.x(), m_size.y());
101 // Clear framebuffer.
102 glClearBufferfv(GL_COLOR, 0, Vec4(0.0f).getPtr());
103 glClearBufferfi(GL_DEPTH_STENCIL, 0, 1.0f, 0);
105 // Render intersecting quads - increment stencil on depth pass
106 glEnable(GL_DEPTH_TEST);
107 glEnable(GL_STENCIL_TEST);
108 glStencilFunc(GL_ALWAYS, 0, 0xffu);
109 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
111 flatShader.setColor(*getCurrentContext(), flatShaderID, Vec4(1.0f, 0.0f, 0.0f, 1.0f));
112 sglr::drawQuad(*getCurrentContext(), flatShaderID, Vec3(-1.0f, -1.0f, 0.0f), Vec3(+1.0f, +1.0f, 0.0f));
114 gradShader.setGradient(*getCurrentContext(), gradShaderID, Vec4(0.0f), Vec4(1.0f));
115 sglr::drawQuad(*getCurrentContext(), gradShaderID, Vec3(-1.0f, -1.0f, -1.0f), Vec3(+1.0f, +1.0f, +1.0f));
117 glDisable(GL_DEPTH_TEST);
119 // Draw quad with stencil test (stencil == 1 or 2 depending on depth) - decrement on stencil failure
120 glStencilFunc(GL_EQUAL, m_useDepth ? 2 : 1, 0xffu);
121 glStencilOp(GL_DECR, GL_KEEP, GL_KEEP);
123 flatShader.setColor(*getCurrentContext(), flatShaderID, Vec4(0.0f, 1.0f, 0.0f, 1.0));
124 sglr::drawQuad(*getCurrentContext(), flatShaderID, Vec3(-0.5f, -0.5f, 0.0f), Vec3(+0.5f, +0.5f, 0.0f));
126 // Draw quad with stencil test where stencil > 1 or 2 depending on depth buffer
127 glStencilFunc(GL_GREATER, m_useDepth ? 1 : 2, 0xffu);
129 flatShader.setColor(*getCurrentContext(), flatShaderID, Vec4(0.0f, 0.0f, 1.0f, 1.0f));
130 sglr::drawQuad(*getCurrentContext(), flatShaderID, Vec3(-1.0f, -1.0f, 0.0f), Vec3(+1.0f, +1.0f, 0.0f));
132 readPixels(dst, 0, 0, m_size.x(), m_size.y(), glu::mapGLInternalFormat(colorFormat), Vec4(1.0f), Vec4(0.0f));
141 class DepthStencilAttachCase : public FboTestCase
144 DepthStencilAttachCase (Context& context, const char* name, const char* desc, deUint32 attachDepth, deUint32 attachStencil)
145 : FboTestCase (context, name, desc)
146 , m_attachDepth (attachDepth)
147 , m_attachStencil (attachStencil)
149 DE_ASSERT(m_attachDepth == GL_DEPTH_ATTACHMENT || m_attachDepth == GL_DEPTH_STENCIL_ATTACHMENT || m_attachDepth == GL_NONE);
150 DE_ASSERT(m_attachStencil == GL_STENCIL_ATTACHMENT || m_attachStencil == GL_NONE);
151 DE_ASSERT(m_attachDepth != GL_DEPTH_STENCIL || m_attachStencil == GL_NONE);
155 void render (tcu::Surface& dst)
157 const deUint32 colorFormat = GL_RGBA8;
158 const deUint32 depthStencilFormat = GL_DEPTH24_STENCIL8;
159 const int width = 128;
160 const int height = 128;
161 const bool hasDepth = (m_attachDepth == GL_DEPTH_STENCIL || m_attachDepth == GL_DEPTH_ATTACHMENT);
162 // const bool hasStencil = (m_attachDepth == GL_DEPTH_STENCIL || m_attachStencil == GL_DEPTH_STENCIL_ATTACHMENT);
164 GradientShader gradShader (glu::TYPE_FLOAT_VEC4);
165 FlatColorShader flatShader (glu::TYPE_FLOAT_VEC4);
166 deUint32 flatShaderID = getCurrentContext()->createProgram(&flatShader);
167 deUint32 gradShaderID = getCurrentContext()->createProgram(&gradShader);
170 deUint32 colorRbo = 0;
171 deUint32 depthStencilRbo = 0;
174 glGenRenderbuffers(1, &colorRbo);
175 glBindRenderbuffer(GL_RENDERBUFFER, colorRbo);
176 glRenderbufferStorage(GL_RENDERBUFFER, colorFormat, width, height);
178 // Depth-stencil buffer.
179 glGenRenderbuffers(1, &depthStencilRbo);
180 glBindRenderbuffer(GL_RENDERBUFFER, depthStencilRbo);
181 glRenderbufferStorage(GL_RENDERBUFFER, depthStencilFormat, width, height);
184 glGenFramebuffers(1, &fbo);
185 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
186 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRbo);
188 if (m_attachDepth != GL_NONE)
189 glFramebufferRenderbuffer(GL_FRAMEBUFFER, m_attachDepth, GL_RENDERBUFFER, depthStencilRbo);
190 if (m_attachStencil != GL_NONE)
191 glFramebufferRenderbuffer(GL_FRAMEBUFFER, m_attachStencil, GL_RENDERBUFFER, depthStencilRbo);
194 checkFramebufferStatus(GL_FRAMEBUFFER);
196 glViewport(0, 0, width, height);
198 // Clear framebuffer.
199 glClearBufferfv(GL_COLOR, 0, Vec4(0.0f).getPtr());
200 glClearBufferfi(GL_DEPTH_STENCIL, 0, 1.0f, 0);
202 // Render intersecting quads - increment stencil on depth pass
203 glEnable(GL_DEPTH_TEST);
204 glEnable(GL_STENCIL_TEST);
205 glStencilFunc(GL_ALWAYS, 0, 0xffu);
206 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
208 flatShader.setColor(*getCurrentContext(), flatShaderID, Vec4(1.0f, 0.0f, 0.0f, 1.0f));
209 sglr::drawQuad(*getCurrentContext(), flatShaderID, Vec3(-1.0f, -1.0f, 0.0f), Vec3(+1.0f, +1.0f, 0.0f));
211 gradShader.setGradient(*getCurrentContext(), gradShaderID, Vec4(0.0f), Vec4(1.0f));
212 sglr::drawQuad(*getCurrentContext(), gradShaderID, Vec3(-1.0f, -1.0f, -1.0f), Vec3(+1.0f, +1.0f, +1.0f));
214 glDisable(GL_DEPTH_TEST);
216 // Draw quad with stencil test (stencil == 1 or 2 depending on depth) - decrement on stencil failure
217 glStencilFunc(GL_EQUAL, hasDepth ? 2 : 1, 0xffu);
218 glStencilOp(GL_DECR, GL_KEEP, GL_KEEP);
220 flatShader.setColor(*getCurrentContext(), flatShaderID, Vec4(0.0f, 1.0f, 0.0f, 1.0));
221 sglr::drawQuad(*getCurrentContext(), flatShaderID, Vec3(-0.5f, -0.5f, 0.0f), Vec3(+0.5f, +0.5f, 0.0f));
223 // Draw quad with stencil test where stencil > 1 or 2 depending on depth buffer
224 glStencilFunc(GL_GREATER, hasDepth ? 1 : 2, 0xffu);
226 flatShader.setColor(*getCurrentContext(), flatShaderID, Vec4(0.0f, 0.0f, 1.0f, 1.0f));
227 sglr::drawQuad(*getCurrentContext(), flatShaderID, Vec3(-1.0f, -1.0f, 0.0f), Vec3(+1.0f, +1.0f, 0.0f));
229 readPixels(dst, 0, 0, width, height, glu::mapGLInternalFormat(colorFormat), Vec4(1.0f), Vec4(0.0f));
233 deUint32 m_attachDepth;
234 deUint32 m_attachStencil;
237 FboStencilTests::FboStencilTests (Context& context)
238 : TestCaseGroup(context, "stencil", "FBO Stencilbuffer tests")
242 FboStencilTests::~FboStencilTests (void)
246 void FboStencilTests::init (void)
248 static const deUint32 stencilFormats[] =
250 GL_DEPTH32F_STENCIL8,
257 tcu::TestCaseGroup* basicGroup = new tcu::TestCaseGroup(m_testCtx, "basic", "Basic stencil tests");
258 addChild(basicGroup);
260 for (int fmtNdx = 0; fmtNdx < DE_LENGTH_OF_ARRAY(stencilFormats); fmtNdx++)
262 deUint32 format = stencilFormats[fmtNdx];
263 tcu::TextureFormat texFmt = glu::mapGLInternalFormat(format);
265 basicGroup->addChild(new BasicFboStencilCase(m_context, getFormatName(format), "", format, IVec2(111, 132), false));
267 if (texFmt.order == tcu::TextureFormat::DS)
268 basicGroup->addChild(new BasicFboStencilCase(m_context, (string(getFormatName(format)) + "_depth").c_str(), "", format, IVec2(111, 132), true));
274 tcu::TestCaseGroup* attachGroup = new tcu::TestCaseGroup(m_testCtx, "attach", "Attaching depth stencil");
275 addChild(attachGroup);
277 attachGroup->addChild(new DepthStencilAttachCase(m_context, "depth_only", "Only depth part of depth-stencil RBO attached", GL_DEPTH_ATTACHMENT, GL_NONE));
278 attachGroup->addChild(new DepthStencilAttachCase(m_context, "stencil_only", "Only stencil part of depth-stencil RBO attached", GL_NONE, GL_STENCIL_ATTACHMENT));
279 attachGroup->addChild(new DepthStencilAttachCase(m_context, "depth_stencil_separate", "Depth and stencil attached separately", GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT));
280 attachGroup->addChild(new DepthStencilAttachCase(m_context, "depth_stencil_attachment", "Depth and stencil attached with DEPTH_STENCIL_ATTACHMENT", GL_DEPTH_STENCIL_ATTACHMENT, GL_NONE));