1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 3.1 Module
3 * -------------------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 *//*!
20 * \file
21 * \brief Base class for FBO tests.
22 *//*--------------------------------------------------------------------*/
23
24 #include "es31fFboTestCase.hpp"
25 #include "es31fFboTestUtil.hpp"
26 #include "tcuTestLog.hpp"
27 #include "tcuImageCompare.hpp"
28 #include "tcuRenderTarget.hpp"
29 #include "sglrGLContext.hpp"
30 #include "sglrReferenceContext.hpp"
31 #include "gluStrUtil.hpp"
32 #include "gluContextInfo.hpp"
33 #include "deRandom.hpp"
34 #include "glwEnums.hpp"
35 #include "glwFunctions.hpp"
36
37 #include <algorithm>
38
39 namespace deqp
40 {
41 namespace gles31
42 {
43 namespace Functional
44 {
45
46 using tcu::TestLog;
47 using std::string;
48
FboTestCase(Context & context,const char * name,const char * description,bool useScreenSizedViewport)49 FboTestCase::FboTestCase (Context& context, const char* name, const char* description, bool useScreenSizedViewport)
50 : TestCase (context, name, description)
51 , m_viewportWidth (useScreenSizedViewport ? context.getRenderTarget().getWidth() : 128)
52 , m_viewportHeight (useScreenSizedViewport ? context.getRenderTarget().getHeight() : 128)
53 {
54 }
55
~FboTestCase(void)56 FboTestCase::~FboTestCase (void)
57 {
58 }
59
iterate(void)60 FboTestCase::IterateResult FboTestCase::iterate (void)
61 {
62 glu::RenderContext& renderCtx = TestCase::m_context.getRenderContext();
63 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
64 TestLog& log = m_testCtx.getLog();
65
66 // Viewport.
67 de::Random rnd (deStringHash(getName()));
68 int width = deMin32(renderTarget.getWidth(), m_viewportWidth);
69 int height = deMin32(renderTarget.getHeight(), m_viewportHeight);
70 int x = rnd.getInt(0, renderTarget.getWidth() - width);
71 int y = rnd.getInt(0, renderTarget.getHeight() - height);
72
73 // Surface format and storage is choosen by render().
74 tcu::Surface reference;
75 tcu::Surface result;
76
77 // Call preCheck() that can throw exception if some requirement is not met.
78 preCheck();
79
80 log << TestLog::Message << "Rendering with GL driver" << TestLog::EndMessage;
81
82 // Render using GLES3.1
83 try
84 {
85 sglr::GLContext context(renderCtx, log, 0, tcu::IVec4(x, y, width, height));
86 setContext(&context);
87 render(result);
88
89 // Check error.
90 deUint32 err = glGetError();
91 if (err != GL_NO_ERROR)
92 throw glu::Error(err, glu::getErrorStr(err).toString().c_str(), DE_NULL, __FILE__, __LINE__);
93
94 setContext(DE_NULL);
95 }
96 catch (const FboTestUtil::FboIncompleteException& e)
97 {
98 if (e.getReason() == GL_FRAMEBUFFER_UNSUPPORTED)
99 {
100 log << e;
101 m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED, "Not supported");
102 return STOP;
103 }
104 else
105 throw;
106 }
107
108 log << TestLog::Message << "Rendering reference image" << TestLog::EndMessage;
109
110 // Render reference.
111 {
112 sglr::ReferenceContextBuffers buffers (tcu::PixelFormat(8,8,8,renderTarget.getPixelFormat().alphaBits?8:0), renderTarget.getDepthBits(), renderTarget.getStencilBits(), width, height);
113 sglr::ReferenceContext context (sglr::ReferenceContextLimits(renderCtx), buffers.getColorbuffer(), buffers.getDepthbuffer(), buffers.getStencilbuffer());
114
115 setContext(&context);
116 render(reference);
117 setContext(DE_NULL);
118 }
119
120 bool isOk = compare(reference, result);
121 m_testCtx.setTestResult(isOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
122 isOk ? "Pass" : "Image comparison failed");
123 return STOP;
124 }
125
compare(const tcu::Surface & reference,const tcu::Surface & result)126 bool FboTestCase::compare (const tcu::Surface& reference, const tcu::Surface& result)
127 {
128 return tcu::fuzzyCompare(m_testCtx.getLog(), "Result", "Image comparison result", reference, result, 0.05f, tcu::COMPARE_LOG_RESULT);
129 }
130
readPixels(tcu::Surface & dst,int x,int y,int width,int height,const tcu::TextureFormat & format,const tcu::Vec4 & scale,const tcu::Vec4 & bias)131 void FboTestCase::readPixels (tcu::Surface& dst, int x, int y, int width, int height, const tcu::TextureFormat& format, const tcu::Vec4& scale, const tcu::Vec4& bias)
132 {
133 FboTestUtil::readPixels(*getCurrentContext(), dst, x, y, width, height, format, scale, bias);
134 }
135
readPixels(tcu::Surface & dst,int x,int y,int width,int height)136 void FboTestCase::readPixels (tcu::Surface& dst, int x, int y, int width, int height)
137 {
138 getCurrentContext()->readPixels(dst, x, y, width, height);
139 }
140
checkFramebufferStatus(deUint32 target)141 void FboTestCase::checkFramebufferStatus (deUint32 target)
142 {
143 deUint32 status = glCheckFramebufferStatus(target);
144 if (status != GL_FRAMEBUFFER_COMPLETE)
145 throw FboTestUtil::FboIncompleteException(status, __FILE__, __LINE__);
146 }
147
checkError(void)148 void FboTestCase::checkError (void)
149 {
150 deUint32 err = glGetError();
151 if (err != GL_NO_ERROR)
152 throw glu::Error((int)err, (string("Got ") + glu::getErrorStr(err).toString()).c_str(), DE_NULL, __FILE__, __LINE__);
153 }
154
isRequiredFormat(deUint32 format,glu::RenderContext & renderContext)155 static bool isRequiredFormat (deUint32 format, glu::RenderContext& renderContext)
156 {
157 const bool supportsES32 = glu::contextSupports(renderContext.getType(), glu::ApiType::es(3, 2));
158 const bool supportsGL45 = glu::contextSupports(renderContext.getType(), glu::ApiType::core(4, 5));
159 switch (format)
160 {
161 // Color-renderable formats
162 case GL_RGBA32I:
163 case GL_RGBA32UI:
164 case GL_RGBA16I:
165 case GL_RGBA16UI:
166 case GL_RGBA8:
167 case GL_RGBA8I:
168 case GL_RGBA8UI:
169 case GL_SRGB8_ALPHA8:
170 case GL_RGB10_A2:
171 case GL_RGB10_A2UI:
172 case GL_RGBA4:
173 case GL_RGB5_A1:
174 case GL_RGB8:
175 case GL_RGB565:
176 case GL_RG32I:
177 case GL_RG32UI:
178 case GL_RG16I:
179 case GL_RG16UI:
180 case GL_RG8:
181 case GL_RG8I:
182 case GL_RG8UI:
183 case GL_R32I:
184 case GL_R32UI:
185 case GL_R16I:
186 case GL_R16UI:
187 case GL_R8:
188 case GL_R8I:
189 case GL_R8UI:
190 return true;
191
192 // Depth formats
193 case GL_DEPTH_COMPONENT32F:
194 case GL_DEPTH_COMPONENT24:
195 case GL_DEPTH_COMPONENT16:
196 return true;
197
198 // Depth+stencil formats
199 case GL_DEPTH32F_STENCIL8:
200 case GL_DEPTH24_STENCIL8:
201 return true;
202
203 // Stencil formats
204 case GL_STENCIL_INDEX8:
205 return true;
206
207 // Float format
208 case GL_RGBA32F:
209 case GL_R11F_G11F_B10F:
210 case GL_RG32F:
211 case GL_R32F:
212 case GL_RGBA16F:
213 case GL_RG16F:
214 case GL_R16F:
215 return supportsES32 || supportsGL45;
216 case GL_RGB16F:
217 return supportsGL45;
218
219 default:
220 return false;
221 }
222 }
223
getEnablingExtensions(deUint32 format,glu::RenderContext & renderContext)224 static std::vector<std::string> getEnablingExtensions (deUint32 format, glu::RenderContext& renderContext)
225 {
226 const bool supportsES32 = glu::contextSupports(renderContext.getType(), glu::ApiType::es(3, 2));
227 std::vector<std::string> out;
228
229 DE_ASSERT(!isRequiredFormat(format, renderContext));
230
231 switch (format)
232 {
233 case GL_RGB16F:
234 out.push_back("GL_EXT_color_buffer_half_float");
235 break;
236
237 case GL_RGB32F:
238 out.push_back("GL_EXT_color_buffer_float");
239 break;
240
241 case GL_RGBA16F:
242 case GL_RG16F:
243 case GL_R16F:
244 if (!supportsES32)
245 out.push_back("GL_EXT_color_buffer_half_float");
246 break;
247
248 case GL_RGBA32F:
249 case GL_R11F_G11F_B10F:
250 case GL_RG32F:
251 case GL_R32F:
252 if (!supportsES32)
253 out.push_back("GL_EXT_color_buffer_float");
254 break;
255
256 case GL_R16:
257 case GL_RG16:
258 case GL_RGBA16:
259 out.push_back("GL_EXT_texture_norm16");
260 break;
261
262 default:
263 break;
264 }
265
266 return out;
267 }
268
isAnyExtensionSupported(Context & context,const std::vector<std::string> & requiredExts)269 static bool isAnyExtensionSupported (Context& context, const std::vector<std::string>& requiredExts)
270 {
271 for (std::vector<std::string>::const_iterator iter = requiredExts.begin(); iter != requiredExts.end(); iter++)
272 {
273 const std::string& extension = *iter;
274
275 if (context.getContextInfo().isExtensionSupported(extension.c_str()))
276 return true;
277 }
278
279 return false;
280 }
281
checkFormatSupport(deUint32 sizedFormat)282 void FboTestCase::checkFormatSupport (deUint32 sizedFormat)
283 {
284 const bool isCoreFormat = isRequiredFormat(sizedFormat, m_context.getRenderContext());
285 const std::vector<std::string> requiredExts = (!isCoreFormat) ? getEnablingExtensions(sizedFormat, m_context.getRenderContext()) : std::vector<std::string>();
286
287 // Check that we don't try to use invalid formats.
288 DE_ASSERT(isCoreFormat || !requiredExts.empty());
289
290 if (!requiredExts.empty() && !isAnyExtensionSupported(m_context, requiredExts))
291 throw tcu::NotSupportedError("Format not supported");
292 }
293
getMinimumSampleCount(deUint32 format)294 static int getMinimumSampleCount (deUint32 format)
295 {
296 switch (format)
297 {
298 // Core formats
299 case GL_RGBA32I:
300 case GL_RGBA32UI:
301 case GL_RGBA16I:
302 case GL_RGBA16UI:
303 case GL_RGBA8:
304 case GL_RGBA8I:
305 case GL_RGBA8UI:
306 case GL_SRGB8_ALPHA8:
307 case GL_RGB10_A2:
308 case GL_RGB10_A2UI:
309 case GL_RGBA4:
310 case GL_RGB5_A1:
311 case GL_RGB8:
312 case GL_RGB565:
313 case GL_RG32I:
314 case GL_RG32UI:
315 case GL_RG16I:
316 case GL_RG16UI:
317 case GL_RG8:
318 case GL_RG8I:
319 case GL_RG8UI:
320 case GL_R32I:
321 case GL_R32UI:
322 case GL_R16I:
323 case GL_R16UI:
324 case GL_R8:
325 case GL_R8I:
326 case GL_R8UI:
327 case GL_DEPTH_COMPONENT32F:
328 case GL_DEPTH_COMPONENT24:
329 case GL_DEPTH_COMPONENT16:
330 case GL_DEPTH32F_STENCIL8:
331 case GL_DEPTH24_STENCIL8:
332 case GL_STENCIL_INDEX8:
333 return 4;
334
335 // GL_EXT_color_buffer_float
336 case GL_R11F_G11F_B10F:
337 case GL_RG16F:
338 case GL_R16F:
339 return 4;
340
341 case GL_RGBA32F:
342 case GL_RGBA16F:
343 case GL_RG32F:
344 case GL_R32F:
345 return 0;
346
347 // GL_EXT_color_buffer_half_float
348 case GL_RGB16F:
349 return 0;
350
351 default:
352 DE_FATAL("Unknown format");
353 return 0;
354 }
355 }
356
querySampleCounts(const glw::Functions & gl,deUint32 format)357 static std::vector<int> querySampleCounts (const glw::Functions& gl, deUint32 format)
358 {
359 int numSampleCounts = 0;
360 std::vector<int> sampleCounts;
361
362 gl.getInternalformativ(GL_RENDERBUFFER, format, GL_NUM_SAMPLE_COUNTS, 1, &numSampleCounts);
363
364 if (numSampleCounts > 0)
365 {
366 sampleCounts.resize(numSampleCounts);
367 gl.getInternalformativ(GL_RENDERBUFFER, format, GL_SAMPLES, (glw::GLsizei)sampleCounts.size(), &sampleCounts[0]);
368 }
369
370 GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to query sample counts for format");
371
372 return sampleCounts;
373 }
374
checkSampleCount(deUint32 sizedFormat,int numSamples)375 void FboTestCase::checkSampleCount (deUint32 sizedFormat, int numSamples)
376 {
377 const int minSampleCount = getMinimumSampleCount(sizedFormat);
378
379 if (numSamples > minSampleCount)
380 {
381 // Exceeds spec-mandated minimum - need to check.
382 const std::vector<int> supportedSampleCounts = querySampleCounts(m_context.getRenderContext().getFunctions(), sizedFormat);
383
384 if (std::find(supportedSampleCounts.begin(), supportedSampleCounts.end(), numSamples) == supportedSampleCounts.end())
385 throw tcu::NotSupportedError("Sample count not supported");
386 }
387 }
388
clearColorBuffer(const tcu::TextureFormat & format,const tcu::Vec4 & value)389 void FboTestCase::clearColorBuffer (const tcu::TextureFormat& format, const tcu::Vec4& value)
390 {
391 FboTestUtil::clearColorBuffer(*getCurrentContext(), format, value);
392 }
393
394 } // Functional
395 } // gles31
396 } // deqp
397