blob: 98a38b1658d0df83a4bfed2193480fba0c7ba6fe [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002//
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +00003// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Framebuffer.cpp: Implements the gl::Framebuffer class. Implements GL framebuffer
9// objects and related functionality. [OpenGL ES 2.0.24] section 4.4 page 105.
10
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000012
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000013#include "libGLESv2/main.h"
daniel@transgaming.com93a81472010-04-20 18:52:58 +000014#include "libGLESv2/utilities.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000015#include "libGLESv2/Texture.h"
16#include "libGLESv2/Context.h"
17#include "libGLESv2/renderer/Renderer.h"
18#include "libGLESv2/Renderbuffer.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019
20namespace gl
21{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000022
daniel@transgaming.com16418b12012-11-28 19:32:22 +000023Framebuffer::Framebuffer(rx::Renderer *renderer)
24 : mRenderer(renderer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +000026 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
27 {
28 mColorbufferTypes[colorAttachment] = GL_NONE;
29 mDrawBufferStates[colorAttachment] = GL_NONE;
30 }
31 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
32 mReadBufferState = GL_COLOR_ATTACHMENT0_EXT;
33
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034 mDepthbufferType = GL_NONE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035 mStencilbufferType = GL_NONE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036}
37
38Framebuffer::~Framebuffer()
39{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +000040 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
41 {
42 mColorbufferPointers[colorAttachment].set(NULL);
43 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000044 mDepthbufferPointer.set(NULL);
45 mStencilbufferPointer.set(NULL);
46}
47
48Renderbuffer *Framebuffer::lookupRenderbuffer(GLenum type, GLuint handle) const
49{
50 gl::Context *context = gl::getContext();
51 Renderbuffer *buffer = NULL;
52
53 if (type == GL_NONE)
54 {
55 buffer = NULL;
56 }
57 else if (type == GL_RENDERBUFFER)
58 {
59 buffer = context->getRenderbuffer(handle);
60 }
apatrick@chromium.org551022e2012-01-23 19:56:54 +000061 else if (IsInternalTextureTarget(type))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000062 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +000063 buffer = context->getTexture(handle)->getRenderbuffer(type);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000064 }
65 else
66 {
67 UNREACHABLE();
68 }
69
70 return buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000071}
72
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +000073void Framebuffer::setColorbuffer(unsigned int colorAttachment, GLenum type, GLuint colorbuffer)
74{
75 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
76 mColorbufferTypes[colorAttachment] = (colorbuffer != 0) ? type : GL_NONE;
77 mColorbufferPointers[colorAttachment].set(lookupRenderbuffer(type, colorbuffer));
78}
79
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080void Framebuffer::setDepthbuffer(GLenum type, GLuint depthbuffer)
81{
daniel@transgaming.com2fa45512011-10-04 18:43:18 +000082 mDepthbufferType = (depthbuffer != 0) ? type : GL_NONE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000083 mDepthbufferPointer.set(lookupRenderbuffer(type, depthbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084}
85
86void Framebuffer::setStencilbuffer(GLenum type, GLuint stencilbuffer)
87{
daniel@transgaming.com2fa45512011-10-04 18:43:18 +000088 mStencilbufferType = (stencilbuffer != 0) ? type : GL_NONE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000089 mStencilbufferPointer.set(lookupRenderbuffer(type, stencilbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090}
91
92void Framebuffer::detachTexture(GLuint texture)
93{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +000094 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +000096 if (mColorbufferPointers[colorAttachment].id() == texture && IsInternalTextureTarget(mColorbufferTypes[colorAttachment]))
97 {
98 mColorbufferTypes[colorAttachment] = GL_NONE;
99 mColorbufferPointers[colorAttachment].set(NULL);
100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101 }
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000102
apatrick@chromium.org551022e2012-01-23 19:56:54 +0000103 if (mDepthbufferPointer.id() == texture && IsInternalTextureTarget(mDepthbufferType))
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000104 {
105 mDepthbufferType = GL_NONE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000106 mDepthbufferPointer.set(NULL);
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000107 }
108
apatrick@chromium.org551022e2012-01-23 19:56:54 +0000109 if (mStencilbufferPointer.id() == texture && IsInternalTextureTarget(mStencilbufferType))
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000110 {
111 mStencilbufferType = GL_NONE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000112 mStencilbufferPointer.set(NULL);
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114}
115
116void Framebuffer::detachRenderbuffer(GLuint renderbuffer)
117{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000118 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000120 if (mColorbufferPointers[colorAttachment].id() == renderbuffer && mColorbufferTypes[colorAttachment] == GL_RENDERBUFFER)
121 {
122 mColorbufferTypes[colorAttachment] = GL_NONE;
123 mColorbufferPointers[colorAttachment].set(NULL);
124 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125 }
126
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000127 if (mDepthbufferPointer.id() == renderbuffer && mDepthbufferType == GL_RENDERBUFFER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128 {
129 mDepthbufferType = GL_NONE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000130 mDepthbufferPointer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131 }
132
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000133 if (mStencilbufferPointer.id() == renderbuffer && mStencilbufferType == GL_RENDERBUFFER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134 {
135 mStencilbufferType = GL_NONE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000136 mStencilbufferPointer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 }
138}
139
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000140unsigned int Framebuffer::getRenderTargetSerial(unsigned int colorAttachment) const
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000141{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000142 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
143
144 Renderbuffer *colorbuffer = mColorbufferPointers[colorAttachment].get();
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000145
146 if (colorbuffer)
147 {
148 return colorbuffer->getSerial();
149 }
150
151 return 0;
152}
153
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000154unsigned int Framebuffer::getDepthbufferSerial() const
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000155{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000156 Renderbuffer *depthbuffer = mDepthbufferPointer.get();
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000157
158 if (depthbuffer)
159 {
160 return depthbuffer->getSerial();
161 }
162
163 return 0;
164}
165
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000166unsigned int Framebuffer::getStencilbufferSerial() const
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +0000167{
168 Renderbuffer *stencilbuffer = mStencilbufferPointer.get();
169
170 if (stencilbuffer)
171 {
172 return stencilbuffer->getSerial();
173 }
174
175 return 0;
176}
177
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000178Renderbuffer *Framebuffer::getColorbuffer(unsigned int colorAttachment) const
179{
180 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
181 return mColorbufferPointers[colorAttachment].get();
182}
183
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000184Renderbuffer *Framebuffer::getDepthbuffer() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000185{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000186 return mDepthbufferPointer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187}
188
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000189Renderbuffer *Framebuffer::getStencilbuffer() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000190{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000191 return mStencilbufferPointer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000192}
193
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000194Renderbuffer *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000195{
196 Renderbuffer *depthstencilbuffer = mDepthbufferPointer.get();
197
198 if (!depthstencilbuffer)
199 {
200 depthstencilbuffer = mStencilbufferPointer.get();
201 }
202
203 return depthstencilbuffer;
204}
205
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000206Renderbuffer *Framebuffer::getReadColorbuffer() const
207{
208 // Will require more logic if glReadBuffers is supported
209 return mColorbufferPointers[0].get();
210}
211
212Renderbuffer *Framebuffer::getFirstColorBuffer() const
213{
214 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
215 {
216 if (mColorbufferTypes[colorAttachment] != GL_NONE)
217 {
218 return mColorbufferPointers[colorAttachment].get();
219 }
220 }
221
222 return NULL;
223}
224
225GLenum Framebuffer::getColorbufferType(unsigned int colorAttachment) const
226{
227 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
228 return mColorbufferTypes[colorAttachment];
229}
230
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000231GLenum Framebuffer::getDepthbufferType() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000232{
233 return mDepthbufferType;
234}
235
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000236GLenum Framebuffer::getStencilbufferType() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000237{
238 return mStencilbufferType;
239}
240
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000241GLuint Framebuffer::getColorbufferHandle(unsigned int colorAttachment) const
242{
243 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
244 return mColorbufferPointers[colorAttachment].id();
245}
246
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000247GLuint Framebuffer::getDepthbufferHandle() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000248{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000249 return mDepthbufferPointer.id();
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000250}
251
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000252GLuint Framebuffer::getStencilbufferHandle() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000253{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000254 return mStencilbufferPointer.id();
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000255}
256
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000257GLenum Framebuffer::getDrawBufferState(unsigned int colorAttachment) const
258{
259 return mDrawBufferStates[colorAttachment];
260}
261
262void Framebuffer::setDrawBufferState(unsigned int colorAttachment, GLenum drawBuffer)
263{
264 mDrawBufferStates[colorAttachment] = drawBuffer;
265}
266
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +0000267bool Framebuffer::isEnabledColorAttachment(unsigned int colorAttachment) const
268{
269 return (mColorbufferTypes[colorAttachment] != GL_NONE && mDrawBufferStates[colorAttachment] != GL_NONE);
270}
271
272bool Framebuffer::hasEnabledColorAttachment() const
273{
274 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
275 {
276 if (isEnabledColorAttachment(colorAttachment))
277 {
278 return true;
279 }
280 }
281
282 return false;
283}
284
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000285bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000286{
287 if (mStencilbufferType != GL_NONE)
288 {
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000289 const Renderbuffer *stencilbufferObject = getStencilbuffer();
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000290
291 if (stencilbufferObject)
292 {
293 return stencilbufferObject->getStencilSize() > 0;
294 }
295 }
296
297 return false;
298}
299
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000300GLenum Framebuffer::completeness() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302 int width = 0;
303 int height = 0;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000304 int colorbufferSize = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000305 int samples = -1;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000306 bool missingAttachment = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000308 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000310 if (mColorbufferTypes[colorAttachment] != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000312 const Renderbuffer *colorbuffer = getColorbuffer(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000314 if (!colorbuffer)
daniel@transgaming.comedc19182010-10-15 17:57:55 +0000315 {
316 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
317 }
daniel@transgaming.comd885df02012-05-31 01:14:36 +0000318
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000319 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000320 {
321 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
322 }
daniel@transgaming.com01868132010-08-24 19:21:17 +0000323
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000324 if (mColorbufferTypes[colorAttachment] == GL_RENDERBUFFER)
325 {
326 if (!gl::IsColorRenderable(colorbuffer->getInternalFormat()))
327 {
328 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
329 }
330 }
331 else if (IsInternalTextureTarget(mColorbufferTypes[colorAttachment]))
332 {
333 GLint internalformat = colorbuffer->getInternalFormat();
334 GLenum format = gl::ExtractFormat(internalformat);
335
336 if (IsCompressed(format) ||
337 format == GL_ALPHA ||
338 format == GL_LUMINANCE ||
339 format == GL_LUMINANCE_ALPHA)
340 {
341 return GL_FRAMEBUFFER_UNSUPPORTED;
342 }
343
344 bool filtering, renderable;
345
346 if ((gl::IsFloat32Format(internalformat) && !mRenderer->getFloat32TextureSupport(&filtering, &renderable)) ||
347 (gl::IsFloat16Format(internalformat) && !mRenderer->getFloat16TextureSupport(&filtering, &renderable)))
348 {
349 return GL_FRAMEBUFFER_UNSUPPORTED;
350 }
351
352 if (gl::IsDepthTexture(internalformat) || gl::IsStencilTexture(internalformat))
353 {
354 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
355 }
356 }
357 else
358 {
359 UNREACHABLE();
360 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
361 }
362
363 if (!missingAttachment)
364 {
365 // all color attachments must have the same width and height
366 if (colorbuffer->getWidth() != width || colorbuffer->getHeight() != height)
367 {
368 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
369 }
370
371 // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that
372 // all color attachments have the same number of samples for the FBO to be complete.
373 if (colorbuffer->getSamples() != samples)
374 {
375 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT;
376 }
377
378 // all color attachments attachments must have the same number of bitplanes
379 if (gl::ComputePixelSize(colorbuffer->getInternalFormat()) != colorbufferSize)
380 {
381 return GL_FRAMEBUFFER_UNSUPPORTED;
382 }
383
384 // D3D11 does not allow for overlapping RenderTargetViews, so ensure uniqueness
385 for (unsigned int previousColorAttachment = 0; previousColorAttachment < colorAttachment; previousColorAttachment++)
386 {
387 if (mColorbufferPointers[colorAttachment].get() == mColorbufferPointers[previousColorAttachment].get())
388 {
389 return GL_FRAMEBUFFER_UNSUPPORTED;
390 }
391 }
392 }
393 else
394 {
395 width = colorbuffer->getWidth();
396 height = colorbuffer->getHeight();
397 samples = colorbuffer->getSamples();
398 colorbufferSize = gl::ComputePixelSize(colorbuffer->getInternalFormat());
399 missingAttachment = false;
400 }
401 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000402 }
403
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000404 const Renderbuffer *depthbuffer = NULL;
405 const Renderbuffer *stencilbuffer = NULL;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000406
407 if (mDepthbufferType != GL_NONE)
408 {
409 depthbuffer = getDepthbuffer();
410
411 if (!depthbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000413 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414 }
415
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000416 if (depthbuffer->getWidth() == 0 || depthbuffer->getHeight() == 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000418 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
419 }
420
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000421 if (mDepthbufferType == GL_RENDERBUFFER)
422 {
423 if (!gl::IsDepthRenderable(depthbuffer->getInternalFormat()))
424 {
425 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
426 }
427 }
428 else if (IsInternalTextureTarget(mDepthbufferType))
429 {
daniel@transgaming.com8e91d252012-10-17 18:22:44 +0000430 GLint internalformat = depthbuffer->getInternalFormat();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000431
432 // depth texture attachments require OES/ANGLE_depth_texture
daniel@transgaming.comea32d482012-11-28 19:33:18 +0000433 if (!mRenderer->getDepthTextureSupport())
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000434 {
435 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
436 }
437
daniel@transgaming.com8e91d252012-10-17 18:22:44 +0000438 if (!gl::IsDepthTexture(internalformat))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000439 {
440 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
441 }
442 }
443 else
444 {
445 UNREACHABLE();
446 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
447 }
448
449 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000450 {
451 width = depthbuffer->getWidth();
452 height = depthbuffer->getHeight();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000453 samples = depthbuffer->getSamples();
454 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000455 }
456 else if (width != depthbuffer->getWidth() || height != depthbuffer->getHeight())
457 {
458 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
459 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000460 else if (samples != depthbuffer->getSamples())
461 {
462 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
463 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000464 }
465
466 if (mStencilbufferType != GL_NONE)
467 {
468 stencilbuffer = getStencilbuffer();
469
470 if (!stencilbuffer)
471 {
472 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
473 }
474
475 if (stencilbuffer->getWidth() == 0 || stencilbuffer->getHeight() == 0)
476 {
477 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
478 }
479
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000480 if (mStencilbufferType == GL_RENDERBUFFER)
481 {
482 if (!gl::IsStencilRenderable(stencilbuffer->getInternalFormat()))
483 {
484 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
485 }
486 }
487 else if (IsInternalTextureTarget(mStencilbufferType))
488 {
daniel@transgaming.com8e91d252012-10-17 18:22:44 +0000489 GLint internalformat = stencilbuffer->getInternalFormat();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000490
491 // texture stencil attachments come along as part
492 // of OES_packed_depth_stencil + OES/ANGLE_depth_texture
daniel@transgaming.comea32d482012-11-28 19:33:18 +0000493 if (!mRenderer->getDepthTextureSupport())
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000494 {
495 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
496 }
497
daniel@transgaming.com8e91d252012-10-17 18:22:44 +0000498 if (!gl::IsStencilTexture(internalformat))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000499 {
500 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
501 }
502 }
503 else
504 {
505 UNREACHABLE();
506 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
507 }
508
509 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000510 {
511 width = stencilbuffer->getWidth();
512 height = stencilbuffer->getHeight();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000513 samples = stencilbuffer->getSamples();
514 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000515 }
516 else if (width != stencilbuffer->getWidth() || height != stencilbuffer->getHeight())
517 {
518 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
519 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000520 else if (samples != stencilbuffer->getSamples())
521 {
522 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
523 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000524 }
525
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000526 // if we have both a depth and stencil buffer, they must refer to the same object
527 // since we only support packed_depth_stencil and not separate depth and stencil
528 if (depthbuffer && stencilbuffer && (depthbuffer != stencilbuffer))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000529 {
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000530 return GL_FRAMEBUFFER_UNSUPPORTED;
531 }
532
533 // we need to have at least one attachment to be complete
534 if (missingAttachment)
535 {
536 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000537 }
538
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539 return GL_FRAMEBUFFER_COMPLETE;
540}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000541
daniel@transgaming.com16418b12012-11-28 19:32:22 +0000542DefaultFramebuffer::DefaultFramebuffer(rx::Renderer *renderer, Colorbuffer *colorbuffer, DepthStencilbuffer *depthStencil)
543 : Framebuffer(renderer)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000544{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000545 mColorbufferPointers[0].set(new Renderbuffer(mRenderer, 0, colorbuffer));
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000546
daniel@transgaming.com70062c92012-11-28 19:32:30 +0000547 Renderbuffer *depthStencilRenderbuffer = new Renderbuffer(mRenderer, 0, depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000548 mDepthbufferPointer.set(depthStencilRenderbuffer);
549 mStencilbufferPointer.set(depthStencilRenderbuffer);
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000550
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000551 mColorbufferTypes[0] = GL_RENDERBUFFER;
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000552 mDepthbufferType = (depthStencilRenderbuffer->getDepthSize() != 0) ? GL_RENDERBUFFER : GL_NONE;
553 mStencilbufferType = (depthStencilRenderbuffer->getStencilSize() != 0) ? GL_RENDERBUFFER : GL_NONE;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000554
555 mDrawBufferStates[0] = GL_BACK;
556 mReadBufferState = GL_BACK;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000557}
558
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000559int Framebuffer::getSamples() const
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000560{
561 if (completeness() == GL_FRAMEBUFFER_COMPLETE)
562 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000563 // for a complete framebuffer, all attachments must have the same sample count
564 // in this case return the first nonzero sample size
565 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
566 {
567 if (mColorbufferTypes[colorAttachment] != GL_NONE)
568 {
569 return getColorbuffer(colorAttachment)->getSamples();
570 }
571 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000572 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000573
574 return 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000575}
576
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000577GLenum DefaultFramebuffer::completeness() const
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000578{
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000579 // The default framebuffer *must* always be complete, though it may not be
580 // subject to the same rules as application FBOs. ie, it could have 0x0 size.
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000581 return GL_FRAMEBUFFER_COMPLETE;
582}
583
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000584}