huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*-------------------------------------------------------------------------
 * drawElements Quality Program OpenGL ES Utilities
 * ------------------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief OpenGL ES context wrapper that uses FBO as default framebuffer.
 *//*--------------------------------------------------------------------*/
 
#include "gluFboRenderContext.hpp"
#include "gluContextFactory.hpp"
#include "gluRenderConfig.hpp"
#include "glwEnums.hpp"
#include "glwFunctions.hpp"
#include "tcuCommandLine.hpp"
#include "gluTextureUtil.hpp"
#include "tcuTextureUtil.hpp"
 
#include <sstream>
 
namespace glu
{
 
static int getNumDepthBits (const tcu::TextureFormat& format)
{
   if (format.order == tcu::TextureFormat::DS)
   {
       const tcu::TextureFormat    depthOnlyFormat        = tcu::getEffectiveDepthStencilTextureFormat(format, tcu::Sampler::MODE_DEPTH);
       return tcu::getTextureFormatBitDepth(depthOnlyFormat).x();
   }
   else if (format.order == tcu::TextureFormat::D)
       return tcu::getTextureFormatBitDepth(format).x();
   else
       return 0;
}
 
static int getNumStencilBits (const tcu::TextureFormat& format)
{
   if (format.order == tcu::TextureFormat::DS)
   {
       const tcu::TextureFormat    stencilOnlyFormat        = tcu::getEffectiveDepthStencilTextureFormat(format, tcu::Sampler::MODE_STENCIL);
       return tcu::getTextureFormatBitDepth(stencilOnlyFormat).x();
   }
   else if (format.order == tcu::TextureFormat::S)
       return tcu::getTextureFormatBitDepth(format).x();
   else
       return 0;
}
 
static tcu::PixelFormat getPixelFormat (deUint32 colorFormat)
{
   const tcu::IVec4 bits = tcu::getTextureFormatBitDepth(glu::mapGLInternalFormat(colorFormat));
   return tcu::PixelFormat(bits[0], bits[1], bits[2], bits[3]);
}
 
static void getDepthStencilBits (deUint32 depthStencilFormat, int* depthBits, int* stencilBits)
{
   const tcu::TextureFormat    combinedFormat    = glu::mapGLInternalFormat(depthStencilFormat);
 
   *depthBits        = getNumDepthBits(combinedFormat);
   *stencilBits    = getNumStencilBits(combinedFormat);
}
 
deUint32 chooseColorFormat (const glu::RenderConfig& config)
{
   static const deUint32 s_formats[] =
   {
       GL_RGBA8,
       GL_RGB8,
       GL_RG8,
       GL_R8,
       GL_RGBA4,
       GL_RGB5_A1,
       GL_RGB565,
       GL_RGB5
   };
 
   for (int fmtNdx = 0; fmtNdx < DE_LENGTH_OF_ARRAY(s_formats); fmtNdx++)
   {
       const deUint32        format    = s_formats[fmtNdx];
       const tcu::IVec4    bits    = tcu::getTextureFormatBitDepth(glu::mapGLInternalFormat(format));
 
       if (config.redBits != glu::RenderConfig::DONT_CARE &&
           config.redBits != bits[0])
           continue;
 
       if (config.greenBits != glu::RenderConfig::DONT_CARE &&
           config.greenBits != bits[1])
           continue;
 
       if (config.blueBits != glu::RenderConfig::DONT_CARE &&
           config.blueBits != bits[2])
           continue;
 
       if (config.alphaBits != glu::RenderConfig::DONT_CARE &&
           config.alphaBits != bits[3])
           continue;
 
       return format;
   }
 
   return 0;
}
 
deUint32 chooseDepthStencilFormat (const glu::RenderConfig& config)
{
   static const deUint32 s_formats[] =
   {
       GL_DEPTH32F_STENCIL8,
       GL_DEPTH24_STENCIL8,
       GL_DEPTH_COMPONENT32F,
       GL_DEPTH_COMPONENT24,
       GL_DEPTH_COMPONENT16,
       GL_STENCIL_INDEX8
   };
 
   for (int fmtNdx = 0; fmtNdx < DE_LENGTH_OF_ARRAY(s_formats); fmtNdx++)
   {
       const deUint32                format            = s_formats[fmtNdx];
       const tcu::TextureFormat    combinedFormat    = glu::mapGLInternalFormat(format);
       const int                    depthBits        = getNumDepthBits(combinedFormat);
       const int                    stencilBits        = getNumStencilBits(combinedFormat);
 
       if (config.depthBits != glu::RenderConfig::DONT_CARE &&
           config.depthBits != depthBits)
           continue;
 
       if (config.stencilBits != glu::RenderConfig::DONT_CARE &&
           config.stencilBits != stencilBits)
           continue;
 
       return format;
   }
 
   return 0;
}
 
FboRenderContext::FboRenderContext (RenderContext* context, const RenderConfig& config)
   : m_context                (context)
   , m_framebuffer            (0)
   , m_colorBuffer            (0)
   , m_depthStencilBuffer    (0)
   , m_renderTarget        ()
{
   try
   {
       createFramebuffer(config);
   }
   catch (...)
   {
       destroyFramebuffer();
       throw;
   }
}
 
FboRenderContext::FboRenderContext (const ContextFactory& factory, const RenderConfig& config, const tcu::CommandLine& cmdLine)
   : m_context                (DE_NULL)
   , m_framebuffer            (0)
   , m_colorBuffer            (0)
   , m_depthStencilBuffer    (0)
   , m_renderTarget        ()
{
   try
   {
       RenderConfig nativeRenderConfig;
       nativeRenderConfig.type                = config.type;
       nativeRenderConfig.windowVisibility    = config.windowVisibility;
       // \note All other properties are defaults, mostly DONT_CARE
       m_context = factory.createContext(nativeRenderConfig, cmdLine, DE_NULL);
       createFramebuffer(config);
   }
   catch (...)
   {
       delete m_context;
       throw;
   }
}
 
FboRenderContext::~FboRenderContext (void)
{
   // \todo [2013-04-08 pyry] Do we want to destry FBO before destroying context?
   delete m_context;
}
 
void FboRenderContext::postIterate (void)
{
   // \todo [2012-11-27 pyry] Blit to default framebuffer in ES3?
   m_context->getFunctions().finish();
}
 
void FboRenderContext::makeCurrent(void)
{
   m_context->makeCurrent();
}
 
void FboRenderContext::createFramebuffer (const RenderConfig& config)
{
   DE_ASSERT(m_framebuffer == 0 && m_colorBuffer == 0 && m_depthStencilBuffer == 0);
 
   const glw::Functions&    gl                    = m_context->getFunctions();
   const deUint32            colorFormat            = chooseColorFormat(config);
   const deUint32            depthStencilFormat    = chooseDepthStencilFormat(config);
   int                        width                = config.width;
   int                        height                = config.height;
   tcu::PixelFormat        pixelFormat;
   int                        depthBits            = 0;
   int                        stencilBits            = 0;
 
   if (config.numSamples > 0 && !gl.renderbufferStorageMultisample)
       throw tcu::NotSupportedError("Multisample FBO is not supported");
 
   if (colorFormat == 0)
       throw tcu::NotSupportedError("Unsupported color attachment format");
 
   if (width == glu::RenderConfig::DONT_CARE || height == glu::RenderConfig::DONT_CARE)
   {
       int maxSize = 0;
       gl.getIntegerv(GL_MAX_RENDERBUFFER_SIZE, &maxSize);
 
       width    = (width    == glu::RenderConfig::DONT_CARE) ? maxSize : width;
       height    = (height    == glu::RenderConfig::DONT_CARE) ? maxSize : height;
   }
 
   {
       pixelFormat = getPixelFormat(colorFormat);
 
       gl.genRenderbuffers(1, &m_colorBuffer);
       gl.bindRenderbuffer(GL_RENDERBUFFER, m_colorBuffer);
 
       if (config.numSamples > 0)
           gl.renderbufferStorageMultisample(GL_RENDERBUFFER, config.numSamples, colorFormat, width, height);
       else
           gl.renderbufferStorage(GL_RENDERBUFFER, colorFormat, width, height);
 
       gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
       GLU_EXPECT_NO_ERROR(gl.getError(), "Creating color renderbuffer");
   }
 
   if (depthStencilFormat != GL_NONE)
   {
       getDepthStencilBits(depthStencilFormat, &depthBits, &stencilBits);
 
       gl.genRenderbuffers(1, &m_depthStencilBuffer);
       gl.bindRenderbuffer(GL_RENDERBUFFER, m_depthStencilBuffer);
 
       if (config.numSamples > 0)
           gl.renderbufferStorageMultisample(GL_RENDERBUFFER, config.numSamples, depthStencilFormat, width, height);
       else
           gl.renderbufferStorage(GL_RENDERBUFFER, depthStencilFormat, width, height);
 
       gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
       GLU_EXPECT_NO_ERROR(gl.getError(), "Creating depth / stencil renderbuffer");
   }
 
   gl.genFramebuffers(1, &m_framebuffer);
   gl.bindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
 
   if (m_colorBuffer)
       gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_colorBuffer);
 
   if (m_depthStencilBuffer)
   {
       if (depthBits > 0)
           gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilBuffer);
 
       if (stencilBits > 0)
           gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilBuffer);
   }
 
   GLU_EXPECT_NO_ERROR(gl.getError(), "Creating framebuffer");
 
   if (gl.checkFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
       throw tcu::NotSupportedError("Framebuffer is not complete");
 
   // Set up correct viewport for first test case.
   gl.viewport(0, 0, width, height);
 
   m_renderTarget = tcu::RenderTarget(width, height, pixelFormat, depthBits, stencilBits, config.numSamples);
}
 
void FboRenderContext::destroyFramebuffer (void)
{
   const glw::Functions& gl = m_context->getFunctions();
 
   if (m_framebuffer)
   {
       gl.deleteFramebuffers(1, &m_framebuffer);
       m_framebuffer = 0;
   }
 
   if (m_depthStencilBuffer)
   {
       gl.deleteRenderbuffers(1, &m_depthStencilBuffer);
       m_depthStencilBuffer = 0;
   }
 
   if (m_colorBuffer)
   {
       gl.deleteRenderbuffers(1, &m_colorBuffer);
       m_colorBuffer = 0;
   }
}
 
} // glu