2
// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
3
// Use of this source code is governed by a BSD-style license that can be
4
// found in the LICENSE file.
7
// Framebuffer.cpp: Implements the gl::Framebuffer class. Implements GL framebuffer
8
// objects and related functionality. [OpenGL ES 2.0.24] section 4.4 page 105.
10
#include "libGLESv2/Framebuffer.h"
12
#include "libGLESv2/main.h"
13
#include "libGLESv2/Renderbuffer.h"
14
#include "libGLESv2/Texture.h"
15
#include "libGLESv2/utilities.h"
20
Framebuffer::Framebuffer()
22
mColorbufferType = GL_NONE;
23
mDepthbufferType = GL_NONE;
24
mStencilbufferType = GL_NONE;
27
Framebuffer::~Framebuffer()
29
mColorbufferPointer.set(NULL);
30
mDepthbufferPointer.set(NULL);
31
mStencilbufferPointer.set(NULL);
32
mNullColorbufferPointer.set(NULL);
35
Renderbuffer *Framebuffer::lookupRenderbuffer(GLenum type, GLuint handle) const
37
gl::Context *context = gl::getContext();
38
Renderbuffer *buffer = NULL;
44
else if (type == GL_RENDERBUFFER)
46
buffer = context->getRenderbuffer(handle);
48
else if (IsInternalTextureTarget(type))
50
buffer = context->getTexture(handle)->getRenderbuffer(type);
60
void Framebuffer::setColorbuffer(GLenum type, GLuint colorbuffer)
62
mColorbufferType = (colorbuffer != 0) ? type : GL_NONE;
63
mColorbufferPointer.set(lookupRenderbuffer(type, colorbuffer));
66
void Framebuffer::setDepthbuffer(GLenum type, GLuint depthbuffer)
68
mDepthbufferType = (depthbuffer != 0) ? type : GL_NONE;
69
mDepthbufferPointer.set(lookupRenderbuffer(type, depthbuffer));
72
void Framebuffer::setStencilbuffer(GLenum type, GLuint stencilbuffer)
74
mStencilbufferType = (stencilbuffer != 0) ? type : GL_NONE;
75
mStencilbufferPointer.set(lookupRenderbuffer(type, stencilbuffer));
78
void Framebuffer::detachTexture(GLuint texture)
80
if (mColorbufferPointer.id() == texture && IsInternalTextureTarget(mColorbufferType))
82
mColorbufferType = GL_NONE;
83
mColorbufferPointer.set(NULL);
86
if (mDepthbufferPointer.id() == texture && IsInternalTextureTarget(mDepthbufferType))
88
mDepthbufferType = GL_NONE;
89
mDepthbufferPointer.set(NULL);
92
if (mStencilbufferPointer.id() == texture && IsInternalTextureTarget(mStencilbufferType))
94
mStencilbufferType = GL_NONE;
95
mStencilbufferPointer.set(NULL);
99
void Framebuffer::detachRenderbuffer(GLuint renderbuffer)
101
if (mColorbufferPointer.id() == renderbuffer && mColorbufferType == GL_RENDERBUFFER)
103
mColorbufferType = GL_NONE;
104
mColorbufferPointer.set(NULL);
107
if (mDepthbufferPointer.id() == renderbuffer && mDepthbufferType == GL_RENDERBUFFER)
109
mDepthbufferType = GL_NONE;
110
mDepthbufferPointer.set(NULL);
113
if (mStencilbufferPointer.id() == renderbuffer && mStencilbufferType == GL_RENDERBUFFER)
115
mStencilbufferType = GL_NONE;
116
mStencilbufferPointer.set(NULL);
120
unsigned int Framebuffer::getRenderTargetSerial()
122
Renderbuffer *colorbuffer = mColorbufferPointer.get();
126
return colorbuffer->getSerial();
132
// Increments refcount on surface.
133
// caller must Release() the returned surface
134
IDirect3DSurface9 *Framebuffer::getRenderTarget()
136
Renderbuffer *colorbuffer = mColorbufferPointer.get();
140
return colorbuffer->getRenderTarget();
146
// Increments refcount on surface.
147
// caller must Release() the returned surface
148
IDirect3DSurface9 *Framebuffer::getDepthStencil()
150
Renderbuffer *depthstencilbuffer = mDepthbufferPointer.get();
152
if (!depthstencilbuffer)
154
depthstencilbuffer = mStencilbufferPointer.get();
157
if (depthstencilbuffer)
159
return depthstencilbuffer->getDepthStencil();
165
unsigned int Framebuffer::getDepthbufferSerial()
167
Renderbuffer *depthbuffer = mDepthbufferPointer.get();
171
return depthbuffer->getSerial();
177
unsigned int Framebuffer::getStencilbufferSerial()
179
Renderbuffer *stencilbuffer = mStencilbufferPointer.get();
183
return stencilbuffer->getSerial();
189
Renderbuffer *Framebuffer::getColorbuffer()
191
return mColorbufferPointer.get();
194
Renderbuffer *Framebuffer::getDepthbuffer()
196
return mDepthbufferPointer.get();
199
Renderbuffer *Framebuffer::getStencilbuffer()
201
return mStencilbufferPointer.get();
204
Renderbuffer *Framebuffer::getNullColorbuffer()
206
Renderbuffer *nullbuffer = mNullColorbufferPointer.get();
207
Renderbuffer *depthbuffer = getDepthbuffer();
211
ERR("Unexpected null depthbuffer for depth-only FBO.");
215
GLsizei width = depthbuffer->getWidth();
216
GLsizei height = depthbuffer->getHeight();
219
width != nullbuffer->getWidth() || height != nullbuffer->getHeight())
221
nullbuffer = new Renderbuffer(0, new Colorbuffer(width, height, GL_NONE, 0));
222
mNullColorbufferPointer.set(nullbuffer);
228
GLenum Framebuffer::getColorbufferType()
230
return mColorbufferType;
233
GLenum Framebuffer::getDepthbufferType()
235
return mDepthbufferType;
238
GLenum Framebuffer::getStencilbufferType()
240
return mStencilbufferType;
243
GLuint Framebuffer::getColorbufferHandle()
245
return mColorbufferPointer.id();
248
GLuint Framebuffer::getDepthbufferHandle()
250
return mDepthbufferPointer.id();
253
GLuint Framebuffer::getStencilbufferHandle()
255
return mStencilbufferPointer.id();
258
bool Framebuffer::hasStencil()
260
if (mStencilbufferType != GL_NONE)
262
Renderbuffer *stencilbufferObject = getStencilbuffer();
264
if (stencilbufferObject)
266
return stencilbufferObject->getStencilSize() > 0;
273
GLenum Framebuffer::completeness()
275
gl::Context *context = gl::getContext();
279
bool missingAttachment = true;
281
if (mColorbufferType != GL_NONE)
283
Renderbuffer *colorbuffer = getColorbuffer();
287
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
290
if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
292
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
295
if (mColorbufferType == GL_RENDERBUFFER)
297
if (!gl::IsColorRenderable(colorbuffer->getInternalFormat()))
299
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
302
else if (IsInternalTextureTarget(mColorbufferType))
304
GLenum internalformat = colorbuffer->getInternalFormat();
305
D3DFORMAT d3dformat = colorbuffer->getD3DFormat();
307
if (IsCompressed(internalformat) ||
308
internalformat == GL_ALPHA ||
309
internalformat == GL_LUMINANCE ||
310
internalformat == GL_LUMINANCE_ALPHA)
312
return GL_FRAMEBUFFER_UNSUPPORTED;
315
if ((dx2es::IsFloat32Format(d3dformat) && !context->supportsFloat32RenderableTextures()) ||
316
(dx2es::IsFloat16Format(d3dformat) && !context->supportsFloat16RenderableTextures()))
318
return GL_FRAMEBUFFER_UNSUPPORTED;
321
if (dx2es::IsDepthTextureFormat(d3dformat) || dx2es::IsStencilTextureFormat(d3dformat))
323
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
329
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
332
width = colorbuffer->getWidth();
333
height = colorbuffer->getHeight();
334
samples = colorbuffer->getSamples();
335
missingAttachment = false;
338
Renderbuffer *depthbuffer = NULL;
339
Renderbuffer *stencilbuffer = NULL;
341
if (mDepthbufferType != GL_NONE)
343
depthbuffer = getDepthbuffer();
347
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
350
if (depthbuffer->getWidth() == 0 || depthbuffer->getHeight() == 0)
352
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
355
if (mDepthbufferType == GL_RENDERBUFFER)
357
if (!gl::IsDepthRenderable(depthbuffer->getInternalFormat()))
359
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
362
else if (IsInternalTextureTarget(mDepthbufferType))
364
D3DFORMAT d3dformat = depthbuffer->getD3DFormat();
366
// depth texture attachments require OES/ANGLE_depth_texture
367
if (!context->supportsDepthTextures())
369
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
372
if (!dx2es::IsDepthTextureFormat(d3dformat))
374
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
380
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
383
if (missingAttachment)
385
width = depthbuffer->getWidth();
386
height = depthbuffer->getHeight();
387
samples = depthbuffer->getSamples();
388
missingAttachment = false;
390
else if (width != depthbuffer->getWidth() || height != depthbuffer->getHeight())
392
return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
394
else if (samples != depthbuffer->getSamples())
396
return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
400
if (mStencilbufferType != GL_NONE)
402
stencilbuffer = getStencilbuffer();
406
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
409
if (stencilbuffer->getWidth() == 0 || stencilbuffer->getHeight() == 0)
411
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
414
if (mStencilbufferType == GL_RENDERBUFFER)
416
if (!gl::IsStencilRenderable(stencilbuffer->getInternalFormat()))
418
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
421
else if (IsInternalTextureTarget(mStencilbufferType))
423
D3DFORMAT d3dformat = stencilbuffer->getD3DFormat();
425
// texture stencil attachments come along as part
426
// of OES_packed_depth_stencil + OES/ANGLE_depth_texture
427
if (!context->supportsDepthTextures())
429
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
432
if (!dx2es::IsStencilTextureFormat(d3dformat))
434
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
440
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
443
if (missingAttachment)
445
width = stencilbuffer->getWidth();
446
height = stencilbuffer->getHeight();
447
samples = stencilbuffer->getSamples();
448
missingAttachment = false;
450
else if (width != stencilbuffer->getWidth() || height != stencilbuffer->getHeight())
452
return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
454
else if (samples != stencilbuffer->getSamples())
456
return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
460
// if we have both a depth and stencil buffer, they must refer to the same object
461
// since we only support packed_depth_stencil and not separate depth and stencil
462
if (depthbuffer && stencilbuffer && (depthbuffer != stencilbuffer))
464
return GL_FRAMEBUFFER_UNSUPPORTED;
467
// we need to have at least one attachment to be complete
468
if (missingAttachment)
470
return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
473
return GL_FRAMEBUFFER_COMPLETE;
476
DefaultFramebuffer::DefaultFramebuffer(Colorbuffer *colorbuffer, DepthStencilbuffer *depthStencil)
478
mColorbufferPointer.set(new Renderbuffer(0, colorbuffer));
480
Renderbuffer *depthStencilRenderbuffer = new Renderbuffer(0, depthStencil);
481
mDepthbufferPointer.set(depthStencilRenderbuffer);
482
mStencilbufferPointer.set(depthStencilRenderbuffer);
484
mColorbufferType = GL_RENDERBUFFER;
485
mDepthbufferType = (depthStencilRenderbuffer->getDepthSize() != 0) ? GL_RENDERBUFFER : GL_NONE;
486
mStencilbufferType = (depthStencilRenderbuffer->getStencilSize() != 0) ? GL_RENDERBUFFER : GL_NONE;
489
int Framebuffer::getSamples()
491
if (completeness() == GL_FRAMEBUFFER_COMPLETE)
493
return getColorbuffer()->getSamples();
501
GLenum DefaultFramebuffer::completeness()
503
// The default framebuffer should always be complete
504
ASSERT(Framebuffer::completeness() == GL_FRAMEBUFFER_COMPLETE);
506
return GL_FRAMEBUFFER_COMPLETE;