Merge "Fix negative eglGetCurrentSurface tests."
[platform/upstream/VK-GL-CTS.git] / modules / glshared / glsFboCompletenessTests.cpp
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program OpenGL (ES) 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 Framebuffer completeness tests.
22  *//*--------------------------------------------------------------------*/
23
24 #include "glsFboCompletenessTests.hpp"
25
26 #include "gluStrUtil.hpp"
27 #include "gluObjectWrapper.hpp"
28 #include "deStringUtil.hpp"
29
30 #include <cctype>
31 #include <iterator>
32 #include <algorithm>
33
34 using namespace glw;
35 using glu::RenderContext;
36 using glu::getFramebufferStatusName;
37 using glu::getPixelFormatName;
38 using glu::getTypeName;
39 using glu::getErrorName;
40 using glu::Framebuffer;
41 using tcu::TestCase;
42 using tcu::TestCaseGroup;
43 using tcu::TestLog;
44 using tcu::MessageBuilder;
45 using tcu::TestNode;
46 using std::string;
47 using de::toString;
48 using namespace deqp::gls::FboUtil;
49 using namespace deqp::gls::FboUtil::config;
50 typedef TestCase::IterateResult IterateResult;
51
52 namespace deqp
53 {
54 namespace gls
55 {
56 namespace fboc
57 {
58
59 namespace details
60 {
61 // \todo [2013-12-04 lauri] Place in deStrUtil.hpp?
62
63 string toLower (const string& str)
64 {
65         string ret;
66         std::transform(str.begin(), str.end(), std::inserter(ret, ret.begin()), ::tolower);
67         return ret;
68 }
69
70 // The following extensions are applicable both to ES2 and ES3.
71
72 // GL_OES_depth_texture
73 static const FormatKey s_oesDepthTextureFormats[] =
74 {
75         GLS_UNSIZED_FORMATKEY(GL_DEPTH_COMPONENT,       GL_UNSIGNED_SHORT),
76         GLS_UNSIZED_FORMATKEY(GL_DEPTH_COMPONENT,       GL_UNSIGNED_INT),
77 };
78
79 // GL_OES_packed_depth_stencil
80 static const FormatKey s_oesPackedDepthStencilSizedFormats[] =
81 {
82         GL_DEPTH24_STENCIL8,
83 };
84
85 static const FormatKey s_oesPackedDepthStencilTexFormats[] =
86 {
87         GLS_UNSIZED_FORMATKEY(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8),
88 };
89
90 // GL_OES_required_internalformat
91 static const FormatKey s_oesRequiredInternalFormatColorFormats[] =
92 {
93         // Same as ES2 RBO formats, plus RGBA8 (even without OES_rgb8_rgba8)
94         GL_RGB5_A1, GL_RGBA8, GL_RGBA4, GL_RGB565
95 };
96
97 static const FormatKey s_oesRequiredInternalFormatDepthFormats[] =
98 {
99         GL_DEPTH_COMPONENT16,
100 };
101
102 // GL_EXT_color_buffer_half_float
103 static const FormatKey s_extColorBufferHalfFloatFormats[] =
104 {
105         GL_RGBA16F, GL_RGB16F, GL_RG16F, GL_R16F,
106 };
107
108 static const FormatKey s_oesDepth24SizedFormats[] =
109 {
110         GL_DEPTH_COMPONENT24
111 };
112
113 static const FormatKey s_oesDepth32SizedFormats[] =
114 {
115         GL_DEPTH_COMPONENT32
116 };
117
118 static const FormatKey s_oesRgb8Rgba8RboFormats[] =
119 {
120         GL_RGB8,
121         GL_RGBA8,
122 };
123
124 static const FormatKey s_oesRequiredInternalFormatRgb8ColorFormat[] =
125 {
126         GL_RGB8,
127 };
128
129 static const FormatKey s_extTextureType2101010RevFormats[] =
130 {
131         GLS_UNSIZED_FORMATKEY(GL_RGBA,  GL_UNSIGNED_INT_2_10_10_10_REV),
132         GLS_UNSIZED_FORMATKEY(GL_RGB,   GL_UNSIGNED_INT_2_10_10_10_REV),
133 };
134
135 static const FormatKey s_oesRequiredInternalFormat10bitColorFormats[] =
136 {
137         GL_RGB10_A2, GL_RGB10,
138 };
139
140 static const FormatKey s_extTextureRgRboFormats[] =
141 {
142         GL_R8, GL_RG8,
143 };
144
145 static const FormatKey s_extTextureRgTexFormats[] =
146 {
147         GLS_UNSIZED_FORMATKEY(GL_RED,   GL_UNSIGNED_BYTE),
148         GLS_UNSIZED_FORMATKEY(GL_RG,    GL_UNSIGNED_BYTE),
149 };
150
151 static const FormatKey s_extTextureRgFloatTexFormats[] =
152 {
153         GLS_UNSIZED_FORMATKEY(GL_RED,   GL_FLOAT),
154         GLS_UNSIZED_FORMATKEY(GL_RG,    GL_FLOAT),
155 };
156
157 static const FormatKey s_extTextureRgHalfFloatTexFormats[] =
158 {
159         GLS_UNSIZED_FORMATKEY(GL_RED,   GL_HALF_FLOAT_OES),
160         GLS_UNSIZED_FORMATKEY(GL_RG,    GL_HALF_FLOAT_OES),
161 };
162
163 static const FormatKey s_nvPackedFloatRboFormats[] =
164 {
165         GL_R11F_G11F_B10F,
166 };
167
168 static const FormatKey s_nvPackedFloatTexFormats[] =
169 {
170         GLS_UNSIZED_FORMATKEY(GL_RGB,   GL_UNSIGNED_INT_10F_11F_11F_REV),
171 };
172
173 static const FormatKey s_extSrgbRboFormats[] =
174 {
175         GL_SRGB8_ALPHA8,
176 };
177
178 static const FormatKey s_extSrgbRenderableTexFormats[] =
179 {
180         GLS_UNSIZED_FORMATKEY(GL_SRGB_ALPHA,    GL_UNSIGNED_BYTE),
181 };
182
183 static const FormatKey s_extSrgbNonRenderableTexFormats[] =
184 {
185         GLS_UNSIZED_FORMATKEY(GL_SRGB,                  GL_UNSIGNED_BYTE),
186         GL_SRGB8,
187 };
188
189 static const FormatKey s_nvSrgbFormatsRboFormats[] =
190 {
191         GL_SRGB8,
192 };
193
194 static const FormatKey s_nvSrgbFormatsTextureFormats[] =
195 {
196         GL_SRGB8,
197
198         // The extension does not actually require any unsized format
199         // to be renderable. However, the renderablility of unsized
200         // SRGB,UBYTE internalformat-type pair is implied.
201         GLS_UNSIZED_FORMATKEY(GL_SRGB,                  GL_UNSIGNED_BYTE),
202 };
203
204 static const FormatKey s_oesRgb8Rgba8TexFormats[] =
205 {
206         GLS_UNSIZED_FORMATKEY(GL_RGB,           GL_UNSIGNED_BYTE),
207         GLS_UNSIZED_FORMATKEY(GL_RGBA,          GL_UNSIGNED_BYTE),
208 };
209
210 static const FormatKey s_extTextureSRGBR8Formats[] =
211 {
212         GL_SR8_EXT,
213 };
214
215 static const FormatKey s_extTextureSRGBRG8Formats[] =
216 {
217         GL_SRG8_EXT,
218 };
219
220 static const FormatExtEntry s_esExtFormats[] =
221 {
222         {
223                 "GL_OES_depth_texture",
224                 REQUIRED_RENDERABLE | DEPTH_RENDERABLE | TEXTURE_VALID,
225                 GLS_ARRAY_RANGE(s_oesDepthTextureFormats),
226         },
227         {
228                 "GL_OES_packed_depth_stencil",
229                 REQUIRED_RENDERABLE | DEPTH_RENDERABLE | STENCIL_RENDERABLE | RENDERBUFFER_VALID,
230                 GLS_ARRAY_RANGE(s_oesPackedDepthStencilSizedFormats)
231         },
232         {
233                 "GL_OES_packed_depth_stencil GL_OES_required_internalformat",
234                 TEXTURE_VALID,
235                 GLS_ARRAY_RANGE(s_oesPackedDepthStencilSizedFormats)
236         },
237         {
238                 "GL_OES_packed_depth_stencil",
239                 DEPTH_RENDERABLE | STENCIL_RENDERABLE | TEXTURE_VALID,
240                 GLS_ARRAY_RANGE(s_oesPackedDepthStencilTexFormats)
241         },
242         // \todo [2013-12-10 lauri] Find out if OES_texture_half_float is really a
243         // requirement on ES3 also. Or is color_buffer_half_float applicatble at
244         // all on ES3, since there's also EXT_color_buffer_float?
245         {
246                 "GL_OES_texture_half_float GL_EXT_color_buffer_half_float",
247                 REQUIRED_RENDERABLE | COLOR_RENDERABLE | RENDERBUFFER_VALID,
248                 GLS_ARRAY_RANGE(s_extColorBufferHalfFloatFormats)
249         },
250
251         // OES_required_internalformat doesn't actually specify that these are renderable,
252         // since it was written against ES 1.1.
253         {
254                 "GL_OES_required_internalformat",
255                  // Allow but don't require RGBA8 to be color-renderable if
256                  // OES_rgb8_rgba8 is not present.
257                 COLOR_RENDERABLE | TEXTURE_VALID,
258                 GLS_ARRAY_RANGE(s_oesRequiredInternalFormatColorFormats)
259         },
260         {
261                 "GL_OES_required_internalformat",
262                 DEPTH_RENDERABLE | TEXTURE_VALID,
263                 GLS_ARRAY_RANGE(s_oesRequiredInternalFormatDepthFormats)
264         },
265         {
266                 "GL_EXT_texture_rg",
267                 REQUIRED_RENDERABLE | COLOR_RENDERABLE | RENDERBUFFER_VALID,
268                 GLS_ARRAY_RANGE(s_extTextureRgRboFormats)
269         },
270         // These are not specified to be color-renderable, but the wording is
271         // exactly as ambiguous as the wording in the ES2 spec.
272         {
273                 "GL_EXT_texture_rg",
274                 COLOR_RENDERABLE | TEXTURE_VALID,
275                 GLS_ARRAY_RANGE(s_extTextureRgTexFormats)
276         },
277         {
278                 "GL_EXT_texture_rg GL_OES_texture_float",
279                 COLOR_RENDERABLE | TEXTURE_VALID,
280                 GLS_ARRAY_RANGE(s_extTextureRgFloatTexFormats)
281         },
282         {
283                 "GL_EXT_texture_rg GL_OES_texture_half_float",
284                 COLOR_RENDERABLE | TEXTURE_VALID,
285                 GLS_ARRAY_RANGE(s_extTextureRgHalfFloatTexFormats)
286         },
287
288         {
289                 "GL_NV_packed_float",
290                 COLOR_RENDERABLE | TEXTURE_VALID,
291                 GLS_ARRAY_RANGE(s_nvPackedFloatTexFormats)
292         },
293         {
294                 "GL_NV_packed_float GL_EXT_color_buffer_half_float",
295                 REQUIRED_RENDERABLE | COLOR_RENDERABLE | RENDERBUFFER_VALID,
296                 GLS_ARRAY_RANGE(s_nvPackedFloatRboFormats)
297         },
298
299         // Some Tegra drivers report GL_EXT_packed_float even for ES. Treat it as
300         // a synonym for the NV_ version.
301         {
302                 "GL_EXT_packed_float",
303                 COLOR_RENDERABLE | TEXTURE_VALID,
304                 GLS_ARRAY_RANGE(s_nvPackedFloatTexFormats)
305         },
306         {
307                 "GL_EXT_packed_float GL_EXT_color_buffer_half_float",
308                 REQUIRED_RENDERABLE | COLOR_RENDERABLE | RENDERBUFFER_VALID,
309                 GLS_ARRAY_RANGE(s_nvPackedFloatRboFormats)
310         },
311
312         {
313                 "GL_EXT_sRGB",
314                 COLOR_RENDERABLE | TEXTURE_VALID,
315                 GLS_ARRAY_RANGE(s_extSrgbRenderableTexFormats)
316         },
317         {
318                 "GL_EXT_sRGB",
319                 TEXTURE_VALID,
320                 GLS_ARRAY_RANGE(s_extSrgbNonRenderableTexFormats)
321         },
322         {
323                 "GL_EXT_sRGB",
324                 REQUIRED_RENDERABLE | COLOR_RENDERABLE | RENDERBUFFER_VALID,
325                 GLS_ARRAY_RANGE(s_extSrgbRboFormats)
326         },
327         {
328                 "GL_NV_sRGB_formats",
329                 REQUIRED_RENDERABLE | COLOR_RENDERABLE | RENDERBUFFER_VALID,
330                 GLS_ARRAY_RANGE(s_nvSrgbFormatsRboFormats)
331         },
332         {
333                 "GL_NV_sRGB_formats",
334                 REQUIRED_RENDERABLE | COLOR_RENDERABLE | TEXTURE_VALID,
335                 GLS_ARRAY_RANGE(s_nvSrgbFormatsTextureFormats)
336         },
337
338          // In Khronos bug 7333 discussion, the consensus is that these texture
339          // formats, at least, should be color-renderable. Still, that cannot be
340          // found in any extension specs, so only allow it, not require it.
341         {
342                 "GL_OES_rgb8_rgba8",
343                 COLOR_RENDERABLE | TEXTURE_VALID,
344                 GLS_ARRAY_RANGE(s_oesRgb8Rgba8TexFormats)
345         },
346         {
347                 "GL_OES_rgb8_rgba8",
348                 REQUIRED_RENDERABLE | COLOR_RENDERABLE | RENDERBUFFER_VALID,
349                 GLS_ARRAY_RANGE(s_oesRgb8Rgba8RboFormats)
350         },
351         {
352                 "GL_OES_rgb8_rgba8 GL_OES_required_internalformat",
353                 TEXTURE_VALID,
354                 GLS_ARRAY_RANGE(s_oesRequiredInternalFormatRgb8ColorFormat)
355         },
356
357         // The depth-renderability of the depth RBO formats is not explicitly
358         // spelled out, but all renderbuffer formats are meant to be renderable.
359         {
360                 "GL_OES_depth24",
361                 REQUIRED_RENDERABLE | DEPTH_RENDERABLE | RENDERBUFFER_VALID,
362                 GLS_ARRAY_RANGE(s_oesDepth24SizedFormats)
363         },
364         {
365                 "GL_OES_depth24 GL_OES_required_internalformat GL_OES_depth_texture",
366                 TEXTURE_VALID,
367                 GLS_ARRAY_RANGE(s_oesDepth24SizedFormats)
368         },
369
370         {
371                 "GL_OES_depth32",
372                 REQUIRED_RENDERABLE | DEPTH_RENDERABLE | RENDERBUFFER_VALID,
373                 GLS_ARRAY_RANGE(s_oesDepth32SizedFormats)
374         },
375         {
376                 "GL_OES_depth32 GL_OES_required_internalformat GL_OES_depth_texture",
377                 TEXTURE_VALID,
378                 GLS_ARRAY_RANGE(s_oesDepth32SizedFormats)
379         },
380
381         {
382                 "GL_EXT_texture_type_2_10_10_10_REV",
383                 TEXTURE_VALID, // explicitly unrenderable
384                 GLS_ARRAY_RANGE(s_extTextureType2101010RevFormats)
385         },
386         {
387                 "GL_EXT_texture_type_2_10_10_10_REV GL_OES_required_internalformat",
388                 TEXTURE_VALID, // explicitly unrenderable
389                 GLS_ARRAY_RANGE(s_oesRequiredInternalFormat10bitColorFormats)
390         },
391
392         {
393                 "GL_EXT_texture_sRGB_R8",
394                 TEXTURE_VALID,
395                 GLS_ARRAY_RANGE(s_extTextureSRGBR8Formats)
396         },
397         {
398                 "GL_EXT_texture_sRGB_RG8",
399                 TEXTURE_VALID,
400                 GLS_ARRAY_RANGE(s_extTextureSRGBRG8Formats)
401         },
402 };
403
404 Context::Context (TestContext& testCtx,
405                                   RenderContext& renderCtx,
406                                   CheckerFactory& factory)
407         : m_testCtx                             (testCtx)
408         , m_renderCtx                   (renderCtx)
409         , m_verifier                    (m_ctxFormats, factory)
410         , m_haveMultiColorAtts  (false)
411 {
412         FormatExtEntries extRange = GLS_ARRAY_RANGE(s_esExtFormats);
413         addExtFormats(extRange);
414 }
415
416 void Context::addFormats (FormatEntries fmtRange)
417 {
418         FboUtil::addFormats(m_minFormats, fmtRange);
419         FboUtil::addFormats(m_ctxFormats, fmtRange);
420         FboUtil::addFormats(m_maxFormats, fmtRange);
421 }
422
423 void Context::addExtFormats (FormatExtEntries extRange)
424 {
425         FboUtil::addExtFormats(m_ctxFormats, extRange, &m_renderCtx);
426         FboUtil::addExtFormats(m_maxFormats, extRange, DE_NULL);
427 }
428
429 void TestBase::pass (void)
430 {
431         m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
432 }
433
434 void TestBase::qualityWarning (const char* msg)
435 {
436         m_testCtx.setTestResult(QP_TEST_RESULT_QUALITY_WARNING, msg);
437 }
438
439 void TestBase::fail (const char* msg)
440 {
441         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, msg);
442 }
443
444 static string statusName (GLenum status)
445 {
446         const char* errorName = getErrorName(status);
447         if (status != GL_NO_ERROR && errorName != DE_NULL)
448                 return string(errorName) + " (during FBO initialization)";
449
450         const char* fbStatusName = getFramebufferStatusName(status);
451         if (fbStatusName != DE_NULL)
452                 return fbStatusName;
453
454         return "unknown value (" + toString(status) + ")";
455 }
456
457 const glw::Functions& gl (const TestBase& test)
458 {
459         return test.getContext().getRenderContext().getFunctions();
460 }
461
462 IterateResult TestBase::iterate (void)
463 {
464         glu::Framebuffer fbo(m_ctx.getRenderContext());
465         FboBuilder builder(*fbo, GL_FRAMEBUFFER, gl(*this));
466         const IterateResult ret = build(builder);
467         const StatusCodes statuses = m_ctx.getVerifier().validStatusCodes(builder);
468
469         GLenum glStatus = builder.getError();
470         if (glStatus == GL_NO_ERROR)
471                 glStatus = gl(*this).checkFramebufferStatus(GL_FRAMEBUFFER);
472
473         // \todo [2013-12-04 lauri] Check if drawing operations succeed.
474
475         StatusCodes::const_iterator it = statuses.begin();
476         GLenum err = *it++;
477         logFramebufferConfig(builder, m_testCtx.getLog());
478
479         MessageBuilder msg(&m_testCtx.getLog());
480
481         msg << "Expected ";
482         if (it != statuses.end())
483         {
484                 msg << "one of ";
485                 while (it != statuses.end())
486                 {
487                         msg << statusName(err);
488                         err = *it++;
489                         msg << (it == statuses.end() ? " or " : ", ");
490                 }
491         }
492         msg << statusName(err) << "." << TestLog::EndMessage;
493         m_testCtx.getLog() << TestLog::Message << "Received " << statusName(glStatus)
494                          << "." << TestLog::EndMessage;
495
496         if (!contains(statuses, glStatus))
497         {
498                 // The returned status value was not acceptable.
499                 if (glStatus == GL_FRAMEBUFFER_COMPLETE)
500                         fail("Framebuffer checked as complete, expected incomplete");
501                 else if (statuses.size() == 1 && contains(statuses, GL_FRAMEBUFFER_COMPLETE))
502                         fail("Framebuffer checked is incomplete, expected complete");
503                 else
504                         // An incomplete status is allowed, but not _this_ incomplete status.
505                         fail("Framebuffer checked as incomplete, but with wrong status");
506         }
507         else if (glStatus != GL_FRAMEBUFFER_COMPLETE &&
508                          contains(statuses, GL_FRAMEBUFFER_COMPLETE))
509         {
510                 qualityWarning("Framebuffer object could have checked as complete but did not.");
511         }
512         else
513                 pass();
514
515         return ret;
516 }
517
518 IterateResult TestBase::build (FboBuilder& builder)
519 {
520         DE_UNREF(builder);
521         return STOP;
522 }
523
524 ImageFormat TestBase::getDefaultFormat (GLenum attPoint, GLenum bufType) const
525 {
526         if (bufType == GL_NONE)
527         {
528                 return ImageFormat::none();
529         }
530
531         // Prefer a standard format, if there is one, but if not, use a format
532         // provided by an extension.
533         Formats formats = m_ctx.getMinFormats().getFormats(formatFlag(attPoint) |
534                                                                                                                  formatFlag(bufType));
535         Formats::const_iterator it = formats.begin();
536         if (it == formats.end())
537         {
538                 formats = m_ctx.getCtxFormats().getFormats(formatFlag(attPoint) |
539                                                                                                          formatFlag(bufType));
540                 it = formats.begin();
541         }
542         if (it == formats.end())
543                 throw tcu::NotSupportedError("Unsupported attachment kind for attachment point",
544                                                                          "", __FILE__, __LINE__);
545         return *it;
546 };
547
548 Image* makeImage (GLenum bufType, ImageFormat format,
549                                   GLsizei width, GLsizei height, FboBuilder& builder)
550 {
551         Image* image = DE_NULL;
552         switch (bufType)
553         {
554                 case GL_NONE:
555                         return DE_NULL;
556                 case GL_RENDERBUFFER:
557                         image = &builder.makeConfig<Renderbuffer>();
558                         break;
559                 case GL_TEXTURE:
560                         image = &builder.makeConfig<Texture2D>();
561                         break;
562                 default:
563                         DE_ASSERT(!"Impossible case");
564         }
565         image->internalFormat = format;
566         image->width = width;
567         image->height = height;
568         return image;
569 }
570
571 Attachment* makeAttachment (GLenum bufType, ImageFormat format,
572                                                         GLsizei width, GLsizei height, FboBuilder& builder)
573 {
574         Image* const imgCfg = makeImage (bufType, format, width, height, builder);
575         Attachment* att = DE_NULL;
576         GLuint img = 0;
577
578         if (Renderbuffer* rboCfg = dynamic_cast<Renderbuffer*>(imgCfg))
579         {
580                 img = builder.glCreateRbo(*rboCfg);
581                 att = &builder.makeConfig<RenderbufferAttachment>();
582         }
583         else if (Texture2D* texCfg = dynamic_cast<Texture2D*>(imgCfg))
584         {
585                 img = builder.glCreateTexture(*texCfg);
586                 TextureFlatAttachment& texAtt = builder.makeConfig<TextureFlatAttachment>();
587                 texAtt.texTarget = GL_TEXTURE_2D;
588                 att = &texAtt;
589         }
590         else
591         {
592                 DE_ASSERT(imgCfg == DE_NULL);
593                 return DE_NULL;
594         }
595         att->imageName = img;
596         return att;
597 }
598
599 void TestBase::attachTargetToNew (GLenum target, GLenum bufType, ImageFormat format,
600                                                                   GLsizei width, GLsizei height, FboBuilder& builder)
601 {
602         ImageFormat imgFmt = format;
603         if (imgFmt.format == GL_NONE)
604                 imgFmt = getDefaultFormat(target, bufType);
605
606         const Attachment* const att = makeAttachment(bufType, imgFmt, width, height, builder);
607         builder.glAttach(target, att);
608 }
609
610 static string formatName (ImageFormat format)
611 {
612         const string s = getPixelFormatName(format.format);
613         const string fmtStr = toLower(s.substr(3));
614
615         if (format.unsizedType != GL_NONE)
616         {
617                 const string typeStr = getTypeName(format.unsizedType);
618                 return fmtStr + "_" + toLower(typeStr.substr(3));
619         }
620
621         return fmtStr;
622 }
623
624 static string formatDesc (ImageFormat format)
625 {
626         const string fmtStr = getPixelFormatName(format.format);
627
628         if (format.unsizedType != GL_NONE)
629         {
630                 const string typeStr = getTypeName(format.unsizedType);
631                 return fmtStr + " with type " + typeStr;
632         }
633
634         return fmtStr;
635 }
636
637 struct RenderableParams
638 {
639         GLenum                          attPoint;
640         GLenum                          bufType;
641         ImageFormat             format;
642         static string           getName                         (const RenderableParams& params)
643         {
644                 return formatName(params.format);
645         }
646         static string           getDescription          (const RenderableParams& params)
647         {
648                 return formatDesc(params.format);
649         }
650 };
651
652 class RenderableTest : public ParamTest<RenderableParams>
653 {
654 public:
655                                         RenderableTest          (Context& group, const Params& params)
656                                                 : ParamTest<RenderableParams> (group, params) {}
657         IterateResult   build                           (FboBuilder& builder);
658 };
659
660 IterateResult RenderableTest::build (FboBuilder& builder)
661 {
662         attachTargetToNew(m_params.attPoint, m_params.bufType, m_params.format, 64, 64, builder);
663         return STOP;
664 }
665
666 string attTypeName (GLenum bufType)
667 {
668         switch (bufType)
669         {
670                 case GL_NONE:
671                         return "none";
672                 case GL_RENDERBUFFER:
673                         return "rbo";
674                 case GL_TEXTURE:
675                         return "tex";
676                 default:
677                         DE_ASSERT(!"Impossible case");
678         }
679         return ""; // Shut up compiler
680 }
681
682 struct AttachmentParams
683 {
684         GLenum                                          color0Kind;
685         GLenum                                          colornKind;
686         GLenum                                          depthKind;
687         GLenum                                          stencilKind;
688
689         static string           getName                 (const AttachmentParams& params);
690         static string           getDescription  (const AttachmentParams& params)
691         {
692                 return getName(params);
693         }
694 };
695
696 string AttachmentParams::getName (const AttachmentParams& params)
697 {
698         return (attTypeName(params.color0Kind) + "_" +
699                         attTypeName(params.colornKind) + "_" +
700                         attTypeName(params.depthKind) + "_" +
701                         attTypeName(params.stencilKind));
702 }
703
704 //! Test for combinations of different kinds of attachments
705 class AttachmentTest : public ParamTest<AttachmentParams>
706 {
707 public:
708                                         AttachmentTest          (Context& group, Params& params)
709                                                 : ParamTest<AttachmentParams> (group, params) {}
710
711 protected:
712         IterateResult   build                           (FboBuilder& builder);
713         void                    makeDepthAndStencil     (FboBuilder& builder);
714 };
715
716
717 void AttachmentTest::makeDepthAndStencil (FboBuilder& builder)
718 {
719         if (m_params.stencilKind == m_params.depthKind)
720         {
721                 // If there is a common stencil+depth -format, try to use a common
722                 // image for both attachments.
723                 const FormatFlags flags =
724                         DEPTH_RENDERABLE | STENCIL_RENDERABLE | formatFlag(m_params.stencilKind);
725                 const Formats& formats = m_ctx.getMinFormats().getFormats(flags);
726                 Formats::const_iterator it = formats.begin();
727                 if (it != formats.end())
728                 {
729                         const ImageFormat format = *it;
730                         Attachment* att = makeAttachment(m_params.depthKind, format, 64, 64, builder);
731                         builder.glAttach(GL_DEPTH_ATTACHMENT, att);
732                         builder.glAttach(GL_STENCIL_ATTACHMENT, att);
733                         return;
734                 }
735         }
736         // Either the kinds were separate, or a suitable format was not found.
737         // Create separate images.
738         attachTargetToNew(GL_STENCIL_ATTACHMENT, m_params.stencilKind, ImageFormat::none(),
739                                           64, 64, builder);
740         attachTargetToNew(GL_DEPTH_ATTACHMENT, m_params.depthKind, ImageFormat::none(),
741                                           64, 64, builder);
742 }
743
744 IterateResult AttachmentTest::build (FboBuilder& builder)
745 {
746         attachTargetToNew(GL_COLOR_ATTACHMENT0, m_params.color0Kind, ImageFormat::none(),
747                                           64, 64, builder);
748
749         if (m_params.colornKind != GL_NONE)
750         {
751                 TCU_CHECK_AND_THROW(NotSupportedError, m_ctx.haveMultiColorAtts(),
752                                                         "Multiple attachments not supported");
753                 GLint maxAttachments = 1;
754                 gl(*this).getIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxAttachments);
755                 GLU_EXPECT_NO_ERROR(
756                         gl(*this).getError(), "Couldn't read GL_MAX_COLOR_ATTACHMENTS");
757
758                 for (int i = 1; i < maxAttachments; i++)
759                 {
760                         attachTargetToNew(GL_COLOR_ATTACHMENT0 + i, m_params.colornKind,
761                                                           ImageFormat::none(), 64, 64, builder);
762                 }
763         }
764
765         makeDepthAndStencil(builder);
766
767         return STOP;
768 }
769
770 class EmptyImageTest : public TestBase
771 {
772 public:
773                                         EmptyImageTest  (Context& group,
774                                                                          const char* name, const char* desc)
775                                                 : TestBase      (group, name, desc) {}
776
777         IterateResult   build                   (FboBuilder& builder);
778 };
779
780 IterateResult EmptyImageTest::build (FboBuilder& builder)
781 {
782         attachTargetToNew(GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, ImageFormat::none(),
783                                           0, 0, builder);
784         return STOP;
785 }
786
787
788 class DistinctSizeTest : public TestBase
789 {
790 public:
791                                         DistinctSizeTest        (Context& group,
792                                                                                  const char* name, const char* desc)
793                                                 : TestBase              (group, name, desc) {}
794
795         IterateResult   build                           (FboBuilder& builder);
796 };
797
798 IterateResult DistinctSizeTest::build (FboBuilder& builder)
799 {
800         attachTargetToNew(GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, ImageFormat::none(),
801                                           64, 64, builder);
802         attachTargetToNew(GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, ImageFormat::none(),
803                                           128, 128, builder);
804         return STOP;
805 }
806
807 TestCaseGroup* Context::createRenderableTests (void)
808 {
809         TestCaseGroup* const renderableTests = new TestCaseGroup(
810                 m_testCtx, "renderable", "Tests for support of renderable image formats");
811
812         TestCaseGroup* const rbRenderableTests = new TestCaseGroup(
813                 m_testCtx, "renderbuffer", "Tests for renderbuffer formats");
814
815         TestCaseGroup* const texRenderableTests = new TestCaseGroup(
816                 m_testCtx, "texture", "Tests for texture formats");
817
818         static const struct AttPoint {
819                 GLenum                  attPoint;
820                 const char*     name;
821                 const char*     desc;
822         } attPoints[] =
823         {
824                 { GL_COLOR_ATTACHMENT0,         "color0",       "Tests for color attachments"   },
825                 { GL_STENCIL_ATTACHMENT,        "stencil",      "Tests for stencil attachments" },
826                 { GL_DEPTH_ATTACHMENT,          "depth",        "Tests for depth attachments"   },
827         };
828
829         // At each attachment point, iterate through all the possible formats to
830         // detect both false positives and false negatives.
831         const Formats rboFmts = m_maxFormats.getFormats(ANY_FORMAT);
832         const Formats texFmts = m_maxFormats.getFormats(ANY_FORMAT);
833
834         for (const AttPoint* it = DE_ARRAY_BEGIN(attPoints); it != DE_ARRAY_END(attPoints); it++)
835         {
836                 TestCaseGroup* const rbAttTests = new TestCaseGroup(m_testCtx, it->name, it->desc);
837                 TestCaseGroup* const texAttTests = new TestCaseGroup(m_testCtx, it->name, it->desc);
838
839                 for (Formats::const_iterator it2 = rboFmts.begin(); it2 != rboFmts.end(); it2++)
840                 {
841                         const RenderableParams params = { it->attPoint, GL_RENDERBUFFER, *it2 };
842                         rbAttTests->addChild(new RenderableTest(*this, params));
843                 }
844                 rbRenderableTests->addChild(rbAttTests);
845
846                 for (Formats::const_iterator it2 = texFmts.begin(); it2 != texFmts.end(); it2++)
847                 {
848                         const RenderableParams params = { it->attPoint, GL_TEXTURE, *it2 };
849                         texAttTests->addChild(new RenderableTest(*this, params));
850                 }
851                 texRenderableTests->addChild(texAttTests);
852         }
853         renderableTests->addChild(rbRenderableTests);
854         renderableTests->addChild(texRenderableTests);
855
856         return renderableTests;
857 }
858
859 TestCaseGroup* Context::createAttachmentTests (void)
860 {
861         TestCaseGroup* const attCombTests = new TestCaseGroup(
862                 m_testCtx, "attachment_combinations", "Tests for attachment combinations");
863
864         static const GLenum s_bufTypes[] = { GL_NONE, GL_RENDERBUFFER, GL_TEXTURE };
865         static const Range<GLenum> s_kinds = GLS_ARRAY_RANGE(s_bufTypes);
866
867         for (const GLenum* col0 = s_kinds.begin(); col0 != s_kinds.end(); ++col0)
868                 for (const GLenum* coln = s_kinds.begin(); coln != s_kinds.end(); ++coln)
869                         for (const GLenum* dep = s_kinds.begin(); dep != s_kinds.end(); ++dep)
870                                 for (const GLenum* stc = s_kinds.begin(); stc != s_kinds.end(); ++stc)
871                                 {
872                                         AttachmentParams params = { *col0, *coln, *dep, *stc };
873                                         attCombTests->addChild(new AttachmentTest(*this, params));
874                                 }
875
876         return attCombTests;
877 }
878
879 TestCaseGroup* Context::createSizeTests (void)
880 {
881         TestCaseGroup* const sizeTests = new TestCaseGroup(
882                 m_testCtx, "size", "Tests for attachment sizes");
883         sizeTests->addChild(new EmptyImageTest(
884                                                         *this, "zero",
885                                                         "Test for zero-sized image attachment"));
886         sizeTests->addChild(new DistinctSizeTest(
887                                                         *this, "distinct",
888                                                         "Test for attachments with different sizes"));
889
890         return sizeTests;
891 }
892
893 } // details
894
895 } // fboc
896 } // gls
897 } // deqp