blob: 4d5e8d4baf2b17b3e92bef3adf02fdf134351e2f [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//
Geoff Langcec35902014-04-16 10:52:36 -04003// Copyright (c) 2002-2014 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"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000014#include "common/utilities.h"
shannonwoods@chromium.orgf6fb9592013-05-30 00:09:40 +000015#include "libGLESv2/formatutils.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000016#include "libGLESv2/Texture.h"
17#include "libGLESv2/Context.h"
18#include "libGLESv2/renderer/Renderer.h"
19#include "libGLESv2/Renderbuffer.h"
Jamie Madille261b442014-06-25 12:42:21 -040020#include "libGLESv2/FramebufferAttachment.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021
22namespace gl
23{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000024
daniel@transgaming.com16418b12012-11-28 19:32:22 +000025Framebuffer::Framebuffer(rx::Renderer *renderer)
Jamie Madille261b442014-06-25 12:42:21 -040026 : mRenderer(renderer),
27 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT),
28 mDepthbuffer(NULL),
29 mStencilbuffer(NULL)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +000031 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
32 {
Jamie Madille261b442014-06-25 12:42:21 -040033 mColorbuffers[colorAttachment] = NULL;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +000034 mDrawBufferStates[colorAttachment] = GL_NONE;
35 }
36 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000037}
38
39Framebuffer::~Framebuffer()
40{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +000041 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
42 {
Jamie Madille261b442014-06-25 12:42:21 -040043 SafeDelete(mColorbuffers[colorAttachment]);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +000044 }
Jamie Madille261b442014-06-25 12:42:21 -040045 SafeDelete(mDepthbuffer);
46 SafeDelete(mStencilbuffer);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000047}
48
Jamie Madille261b442014-06-25 12:42:21 -040049FramebufferAttachment *Framebuffer::createAttachment(GLenum type, GLuint handle, GLint level, GLint layer) const
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000050{
Jamie Madill6c7b4ad2014-06-16 10:33:59 -040051 if (handle == 0)
52 {
53 return NULL;
54 }
55
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000056 gl::Context *context = gl::getContext();
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000057
Geoff Lang309c92a2013-07-25 16:23:19 -040058 switch (type)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000059 {
Geoff Lang309c92a2013-07-25 16:23:19 -040060 case GL_NONE:
61 return NULL;
62
63 case GL_RENDERBUFFER:
Jamie Madill6c7b4ad2014-06-16 10:33:59 -040064 return new RenderbufferAttachment(context->getRenderbuffer(handle));
Geoff Lang309c92a2013-07-25 16:23:19 -040065
66 case GL_TEXTURE_2D:
67 {
68 Texture *texture = context->getTexture(handle);
69 if (texture && texture->getTarget() == GL_TEXTURE_2D)
70 {
Jamie Madill6c7b4ad2014-06-16 10:33:59 -040071 Texture2D *tex2D = static_cast<Texture2D*>(texture);
72 return new Texture2DAttachment(tex2D, level);
Geoff Lang309c92a2013-07-25 16:23:19 -040073 }
74 else
75 {
76 return NULL;
77 }
78 }
79
80 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
81 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
82 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
83 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
84 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
85 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
86 {
87 Texture *texture = context->getTexture(handle);
88 if (texture && texture->getTarget() == GL_TEXTURE_CUBE_MAP)
89 {
Jamie Madill6c7b4ad2014-06-16 10:33:59 -040090 TextureCubeMap *texCube = static_cast<TextureCubeMap*>(texture);
91 return new TextureCubeMapAttachment(texCube, type, level);
Geoff Lang309c92a2013-07-25 16:23:19 -040092 }
93 else
94 {
95 return NULL;
96 }
97 }
98
99 case GL_TEXTURE_3D:
100 {
101 Texture *texture = context->getTexture(handle);
102 if (texture && texture->getTarget() == GL_TEXTURE_3D)
103 {
Jamie Madill6c7b4ad2014-06-16 10:33:59 -0400104 Texture3D *tex3D = static_cast<Texture3D*>(texture);
105 return new Texture3DAttachment(tex3D, level, layer);
Geoff Lang309c92a2013-07-25 16:23:19 -0400106 }
107 else
108 {
109 return NULL;
110 }
111 }
112
113 case GL_TEXTURE_2D_ARRAY:
114 {
115 Texture *texture = context->getTexture(handle);
116 if (texture && texture->getTarget() == GL_TEXTURE_2D_ARRAY)
117 {
Jamie Madill6c7b4ad2014-06-16 10:33:59 -0400118 Texture2DArray *tex2DArray = static_cast<Texture2DArray*>(texture);
119 return new Texture2DArrayAttachment(tex2DArray, level, layer);
Geoff Lang309c92a2013-07-25 16:23:19 -0400120 }
121 else
122 {
123 return NULL;
124 }
125 }
126
127 default:
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000128 UNREACHABLE();
Geoff Lang309c92a2013-07-25 16:23:19 -0400129 return NULL;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131}
132
Geoff Lang309c92a2013-07-25 16:23:19 -0400133void Framebuffer::setColorbuffer(unsigned int colorAttachment, GLenum type, GLuint colorbuffer, GLint level, GLint layer)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000134{
135 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
Jamie Madille261b442014-06-25 12:42:21 -0400136 SafeDelete(mColorbuffers[colorAttachment]);
137 mColorbuffers[colorAttachment] = createAttachment(type, colorbuffer, level, layer);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000138}
139
Geoff Lang309c92a2013-07-25 16:23:19 -0400140void Framebuffer::setDepthbuffer(GLenum type, GLuint depthbuffer, GLint level, GLint layer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141{
Jamie Madille261b442014-06-25 12:42:21 -0400142 SafeDelete(mDepthbuffer);
143 mDepthbuffer = createAttachment(type, depthbuffer, level, layer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144}
145
Geoff Lang309c92a2013-07-25 16:23:19 -0400146void Framebuffer::setStencilbuffer(GLenum type, GLuint stencilbuffer, GLint level, GLint layer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147{
Jamie Madille261b442014-06-25 12:42:21 -0400148 SafeDelete(mStencilbuffer);
149 mStencilbuffer = createAttachment(type, stencilbuffer, level, layer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150}
151
Geoff Lang309c92a2013-07-25 16:23:19 -0400152void Framebuffer::setDepthStencilBuffer(GLenum type, GLuint depthStencilBuffer, GLint level, GLint layer)
Geoff Lang55ba29c2013-07-11 16:57:53 -0400153{
Jamie Madille261b442014-06-25 12:42:21 -0400154 FramebufferAttachment *attachment = createAttachment(type, depthStencilBuffer, level, layer);
Jamie Madill6c7b4ad2014-06-16 10:33:59 -0400155
Jamie Madille261b442014-06-25 12:42:21 -0400156 SafeDelete(mDepthbuffer);
157 SafeDelete(mStencilbuffer);
158
159 // ensure this is a legitimate depth+stencil format
Geoff Lange4a492b2014-06-19 14:14:41 -0400160 if (attachment && attachment->getDepthSize() > 0 && attachment->getStencilSize() > 0)
Geoff Lang55ba29c2013-07-11 16:57:53 -0400161 {
Jamie Madille261b442014-06-25 12:42:21 -0400162 mDepthbuffer = attachment;
Jamie Madill28bedaf2014-06-26 13:17:22 -0400163
164 // Make a new attachment object to ensure we do not double-delete
165 // See angle issue 686
166 mStencilbuffer = createAttachment(type, depthStencilBuffer, level, layer);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400167 }
168}
169
Jamie Madille261b442014-06-25 12:42:21 -0400170void Framebuffer::detachTexture(GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000172 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173 {
Jamie Madille261b442014-06-25 12:42:21 -0400174 FramebufferAttachment *attachment = mColorbuffers[colorAttachment];
175
176 if (attachment && attachment->isTextureWithId(textureId))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000177 {
Jamie Madille261b442014-06-25 12:42:21 -0400178 SafeDelete(mColorbuffers[colorAttachment]);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180 }
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000181
Jamie Madille261b442014-06-25 12:42:21 -0400182 if (mDepthbuffer && mDepthbuffer->isTextureWithId(textureId))
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000183 {
Jamie Madille261b442014-06-25 12:42:21 -0400184 SafeDelete(mDepthbuffer);
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000185 }
186
Jamie Madille261b442014-06-25 12:42:21 -0400187 if (mStencilbuffer && mStencilbuffer->isTextureWithId(textureId))
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000188 {
Jamie Madille261b442014-06-25 12:42:21 -0400189 SafeDelete(mStencilbuffer);
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000190 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191}
192
Jamie Madille261b442014-06-25 12:42:21 -0400193void Framebuffer::detachRenderbuffer(GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000195 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000196 {
Jamie Madille261b442014-06-25 12:42:21 -0400197 FramebufferAttachment *attachment = mColorbuffers[colorAttachment];
198
199 if (attachment && attachment->isRenderbufferWithId(renderbufferId))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000200 {
Jamie Madille261b442014-06-25 12:42:21 -0400201 SafeDelete(mColorbuffers[colorAttachment]);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000202 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203 }
204
Jamie Madille261b442014-06-25 12:42:21 -0400205 if (mDepthbuffer && mDepthbuffer->isRenderbufferWithId(renderbufferId))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000206 {
Jamie Madille261b442014-06-25 12:42:21 -0400207 SafeDelete(mDepthbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000208 }
209
Jamie Madille261b442014-06-25 12:42:21 -0400210 if (mStencilbuffer && mStencilbuffer->isRenderbufferWithId(renderbufferId))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211 {
Jamie Madille261b442014-06-25 12:42:21 -0400212 SafeDelete(mStencilbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000213 }
214}
215
Jamie Madill3c7fa222014-06-05 13:08:51 -0400216FramebufferAttachment *Framebuffer::getColorbuffer(unsigned int colorAttachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000217{
218 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
Jamie Madille261b442014-06-25 12:42:21 -0400219 return mColorbuffers[colorAttachment];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000220}
221
Jamie Madill3c7fa222014-06-05 13:08:51 -0400222FramebufferAttachment *Framebuffer::getDepthbuffer() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223{
Jamie Madille261b442014-06-25 12:42:21 -0400224 return mDepthbuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225}
226
Jamie Madill3c7fa222014-06-05 13:08:51 -0400227FramebufferAttachment *Framebuffer::getStencilbuffer() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228{
Jamie Madille261b442014-06-25 12:42:21 -0400229 return mStencilbuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230}
231
Jamie Madill3c7fa222014-06-05 13:08:51 -0400232FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400233{
Jamie Madille261b442014-06-25 12:42:21 -0400234 return (hasValidDepthStencil() ? mDepthbuffer : NULL);
Geoff Lang646559f2013-08-15 11:08:15 -0400235}
236
Jamie Madill3c7fa222014-06-05 13:08:51 -0400237FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000238{
Jamie Madille261b442014-06-25 12:42:21 -0400239 FramebufferAttachment *depthstencilbuffer = mDepthbuffer;
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000240
241 if (!depthstencilbuffer)
242 {
Jamie Madille261b442014-06-25 12:42:21 -0400243 depthstencilbuffer = mStencilbuffer;
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000244 }
245
246 return depthstencilbuffer;
247}
248
Jamie Madill3c7fa222014-06-05 13:08:51 -0400249FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000250{
251 // Will require more logic if glReadBuffers is supported
Jamie Madille261b442014-06-25 12:42:21 -0400252 return mColorbuffers[0];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000253}
254
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000255GLenum Framebuffer::getReadColorbufferType() const
256{
257 // Will require more logic if glReadBuffers is supported
Jamie Madille261b442014-06-25 12:42:21 -0400258 return (mColorbuffers[0] ? mColorbuffers[0]->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000259}
260
Jamie Madill3c7fa222014-06-05 13:08:51 -0400261FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000262{
263 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
264 {
Jamie Madille261b442014-06-25 12:42:21 -0400265 if (mColorbuffers[colorAttachment])
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000266 {
Jamie Madille261b442014-06-25 12:42:21 -0400267 return mColorbuffers[colorAttachment];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000268 }
269 }
270
271 return NULL;
272}
273
274GLenum Framebuffer::getColorbufferType(unsigned int colorAttachment) const
275{
276 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
Jamie Madille261b442014-06-25 12:42:21 -0400277 return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000278}
279
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000280GLenum Framebuffer::getDepthbufferType() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000281{
Jamie Madille261b442014-06-25 12:42:21 -0400282 return (mDepthbuffer ? mDepthbuffer->type() : GL_NONE);
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000283}
284
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000285GLenum Framebuffer::getStencilbufferType() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000286{
Jamie Madille261b442014-06-25 12:42:21 -0400287 return (mStencilbuffer ? mStencilbuffer->type() : GL_NONE);
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000288}
289
Geoff Lang55ba29c2013-07-11 16:57:53 -0400290GLenum Framebuffer::getDepthStencilbufferType() const
291{
Jamie Madille261b442014-06-25 12:42:21 -0400292 return (hasValidDepthStencil() ? mDepthbuffer->type() : GL_NONE);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400293}
294
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000295GLuint Framebuffer::getColorbufferHandle(unsigned int colorAttachment) const
296{
297 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
Jamie Madille261b442014-06-25 12:42:21 -0400298 return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->id() : 0);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000299}
300
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000301GLuint Framebuffer::getDepthbufferHandle() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000302{
Jamie Madille261b442014-06-25 12:42:21 -0400303 return (mDepthbuffer ? mDepthbuffer->id() : 0);
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000304}
305
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000306GLuint Framebuffer::getStencilbufferHandle() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000307{
Jamie Madille261b442014-06-25 12:42:21 -0400308 return (mStencilbuffer ? mStencilbuffer->id() : 0);
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000309}
310
Jamie Madille261b442014-06-25 12:42:21 -0400311GLuint Framebuffer::getDepthStencilbufferHandle() const
Geoff Lang55ba29c2013-07-11 16:57:53 -0400312{
Jamie Madille261b442014-06-25 12:42:21 -0400313 return (hasValidDepthStencil() ? mDepthbuffer->id() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400314}
315
Jamie Madille261b442014-06-25 12:42:21 -0400316GLint Framebuffer::getColorbufferMipLevel(unsigned int colorAttachment) const
Geoff Langc90d73a2013-07-22 16:39:23 -0400317{
318 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
Jamie Madille261b442014-06-25 12:42:21 -0400319 return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->mipLevel() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400320}
321
Jamie Madille261b442014-06-25 12:42:21 -0400322GLint Framebuffer::getDepthbufferMipLevel() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400323{
Jamie Madille261b442014-06-25 12:42:21 -0400324 return (mDepthbuffer ? mDepthbuffer->mipLevel() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400325}
326
Jamie Madille261b442014-06-25 12:42:21 -0400327GLint Framebuffer::getStencilbufferMipLevel() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400328{
Jamie Madille261b442014-06-25 12:42:21 -0400329 return (mStencilbuffer ? mStencilbuffer->mipLevel() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400330}
331
Jamie Madille261b442014-06-25 12:42:21 -0400332GLint Framebuffer::getDepthStencilbufferMipLevel() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400333{
Jamie Madille261b442014-06-25 12:42:21 -0400334 return (hasValidDepthStencil() ? mDepthbuffer->mipLevel() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400335}
336
Jamie Madille261b442014-06-25 12:42:21 -0400337GLint Framebuffer::getColorbufferLayer(unsigned int colorAttachment) const
Geoff Langc90d73a2013-07-22 16:39:23 -0400338{
339 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
Jamie Madille261b442014-06-25 12:42:21 -0400340 return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->layer() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400341}
342
Jamie Madille261b442014-06-25 12:42:21 -0400343GLint Framebuffer::getDepthbufferLayer() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400344{
Jamie Madille261b442014-06-25 12:42:21 -0400345 return (mDepthbuffer ? mDepthbuffer->layer() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400346}
347
Jamie Madille261b442014-06-25 12:42:21 -0400348GLint Framebuffer::getStencilbufferLayer() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400349{
Jamie Madille261b442014-06-25 12:42:21 -0400350 return (mStencilbuffer ? mStencilbuffer->layer() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400351}
352
Jamie Madille261b442014-06-25 12:42:21 -0400353GLint Framebuffer::getDepthStencilbufferLayer() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400354{
Jamie Madille261b442014-06-25 12:42:21 -0400355 return (hasValidDepthStencil() ? mDepthbuffer->layer() : 0);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400356}
357
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000358GLenum Framebuffer::getDrawBufferState(unsigned int colorAttachment) const
359{
360 return mDrawBufferStates[colorAttachment];
361}
362
363void Framebuffer::setDrawBufferState(unsigned int colorAttachment, GLenum drawBuffer)
364{
365 mDrawBufferStates[colorAttachment] = drawBuffer;
366}
367
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +0000368bool Framebuffer::isEnabledColorAttachment(unsigned int colorAttachment) const
369{
Jamie Madille261b442014-06-25 12:42:21 -0400370 return (mColorbuffers[colorAttachment] && mDrawBufferStates[colorAttachment] != GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +0000371}
372
373bool Framebuffer::hasEnabledColorAttachment() const
374{
375 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
376 {
377 if (isEnabledColorAttachment(colorAttachment))
378 {
379 return true;
380 }
381 }
382
383 return false;
384}
385
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000386bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000387{
Geoff Lange4a492b2014-06-19 14:14:41 -0400388 return (mStencilbuffer && mStencilbuffer->getStencilSize() > 0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000389}
390
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000391bool Framebuffer::usingExtendedDrawBuffers() const
392{
393 for (unsigned int colorAttachment = 1; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
394 {
395 if (isEnabledColorAttachment(colorAttachment))
396 {
397 return true;
398 }
399 }
400
401 return false;
402}
403
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000404GLenum Framebuffer::completeness() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406 int width = 0;
407 int height = 0;
shannonwoods@chromium.orgf6fb9592013-05-30 00:09:40 +0000408 unsigned int colorbufferSize = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000409 int samples = -1;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000410 bool missingAttachment = true;
shannonwoods@chromium.orgf6fb9592013-05-30 00:09:40 +0000411 GLuint clientVersion = mRenderer->getCurrentClientVersion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000413 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414 {
Jamie Madillbb94f342014-06-23 15:23:02 -0400415 const FramebufferAttachment *colorbuffer = mColorbuffers[colorAttachment];
416
417 if (colorbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000419 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000420 {
421 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
422 }
daniel@transgaming.com01868132010-08-24 19:21:17 +0000423
Geoff Langcec35902014-04-16 10:52:36 -0400424 GLenum internalformat = colorbuffer->getInternalFormat();
425 const TextureCaps &formatCaps = mRenderer->getCaps().textureCaps.get(internalformat);
Jamie Madillbb94f342014-06-23 15:23:02 -0400426 if (colorbuffer->isTexture())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000427 {
Geoff Langcec35902014-04-16 10:52:36 -0400428 if (!formatCaps.colorRendering)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000429 {
430 return GL_FRAMEBUFFER_UNSUPPORTED;
431 }
432
Geoff Lange4a492b2014-06-19 14:14:41 -0400433 if (gl::GetDepthBits(internalformat) > 0 ||
434 gl::GetStencilBits(internalformat) > 0)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000435 {
436 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
437 }
438 }
439 else
440 {
Jamie Madillbb94f342014-06-23 15:23:02 -0400441 if (!formatCaps.colorRendering)
442 {
443 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
444 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000445 }
446
447 if (!missingAttachment)
448 {
449 // all color attachments must have the same width and height
450 if (colorbuffer->getWidth() != width || colorbuffer->getHeight() != height)
451 {
452 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
453 }
454
455 // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that
456 // all color attachments have the same number of samples for the FBO to be complete.
457 if (colorbuffer->getSamples() != samples)
458 {
459 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT;
460 }
461
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +0000462 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
463 // in GLES 3.0, there is no such restriction
shannonwoods@chromium.orgf6fb9592013-05-30 00:09:40 +0000464 if (clientVersion < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000465 {
Geoff Lange4a492b2014-06-19 14:14:41 -0400466 if (gl::GetPixelBytes(colorbuffer->getInternalFormat()) != colorbufferSize)
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +0000467 {
468 return GL_FRAMEBUFFER_UNSUPPORTED;
469 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000470 }
471
472 // D3D11 does not allow for overlapping RenderTargetViews, so ensure uniqueness
473 for (unsigned int previousColorAttachment = 0; previousColorAttachment < colorAttachment; previousColorAttachment++)
474 {
Jamie Madille261b442014-06-25 12:42:21 -0400475 if (colorbuffer->id() == getColorbufferHandle(previousColorAttachment) &&
476 colorbuffer->type() == getColorbufferType(previousColorAttachment))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000477 {
478 return GL_FRAMEBUFFER_UNSUPPORTED;
479 }
480 }
481 }
482 else
483 {
484 width = colorbuffer->getWidth();
485 height = colorbuffer->getHeight();
486 samples = colorbuffer->getSamples();
Geoff Lange4a492b2014-06-19 14:14:41 -0400487 colorbufferSize = gl::GetPixelBytes(colorbuffer->getInternalFormat());
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000488 missingAttachment = false;
489 }
490 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000491 }
492
Jamie Madille261b442014-06-25 12:42:21 -0400493 if (mDepthbuffer)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000494 {
Jamie Madille261b442014-06-25 12:42:21 -0400495 if (mDepthbuffer->getWidth() == 0 || mDepthbuffer->getHeight() == 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000497 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000498 }
499
Jamie Madille261b442014-06-25 12:42:21 -0400500 GLenum internalformat = mDepthbuffer->getInternalFormat();
Geoff Langcec35902014-04-16 10:52:36 -0400501 const TextureCaps &formatCaps = mRenderer->getCaps().textureCaps.get(internalformat);
Jamie Madillbb94f342014-06-23 15:23:02 -0400502 if (mDepthbuffer->isTexture())
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000503 {
Jamie Madille261b442014-06-25 12:42:21 -0400504 GLenum internalformat = mDepthbuffer->getInternalFormat();
505
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000506 // depth texture attachments require OES/ANGLE_depth_texture
Geoff Langcec35902014-04-16 10:52:36 -0400507 if (!mRenderer->getCaps().extensions.depthTextures)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000508 {
509 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
510 }
511
Geoff Langcec35902014-04-16 10:52:36 -0400512 if (!formatCaps.depthRendering)
513 {
514 return GL_FRAMEBUFFER_UNSUPPORTED;
515 }
516
Geoff Lange4a492b2014-06-19 14:14:41 -0400517 if (gl::GetDepthBits(internalformat) == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000518 {
519 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
520 }
521 }
522 else
523 {
Jamie Madillbb94f342014-06-23 15:23:02 -0400524 if (!formatCaps.depthRendering)
525 {
526 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
527 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000528 }
529
530 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000531 {
Jamie Madille261b442014-06-25 12:42:21 -0400532 width = mDepthbuffer->getWidth();
533 height = mDepthbuffer->getHeight();
534 samples = mDepthbuffer->getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000535 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000536 }
Jamie Madille261b442014-06-25 12:42:21 -0400537 else if (width != mDepthbuffer->getWidth() || height != mDepthbuffer->getHeight())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000538 {
539 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
540 }
Jamie Madille261b442014-06-25 12:42:21 -0400541 else if (samples != mDepthbuffer->getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000542 {
543 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
544 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000545 }
546
Jamie Madille261b442014-06-25 12:42:21 -0400547 if (mStencilbuffer)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000548 {
Jamie Madille261b442014-06-25 12:42:21 -0400549 if (mStencilbuffer->getWidth() == 0 || mStencilbuffer->getHeight() == 0)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000550 {
551 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
552 }
553
Jamie Madille261b442014-06-25 12:42:21 -0400554 GLenum internalformat = mStencilbuffer->getInternalFormat();
Geoff Langcec35902014-04-16 10:52:36 -0400555 const TextureCaps &formatCaps = mRenderer->getCaps().textureCaps.get(internalformat);
Jamie Madillbb94f342014-06-23 15:23:02 -0400556 if (mStencilbuffer->isTexture())
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000557 {
Jamie Madille261b442014-06-25 12:42:21 -0400558 GLenum internalformat = mStencilbuffer->getInternalFormat();
559
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000560 // texture stencil attachments come along as part
561 // of OES_packed_depth_stencil + OES/ANGLE_depth_texture
Geoff Langcec35902014-04-16 10:52:36 -0400562 if (!mRenderer->getCaps().extensions.depthTextures)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000563 {
564 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
565 }
566
Geoff Langcec35902014-04-16 10:52:36 -0400567 if (!formatCaps.stencilRendering)
568 {
569 return GL_FRAMEBUFFER_UNSUPPORTED;
570 }
571
Geoff Lange4a492b2014-06-19 14:14:41 -0400572 if (gl::GetStencilBits(internalformat) == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000573 {
574 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
575 }
576 }
577 else
578 {
Jamie Madillbb94f342014-06-23 15:23:02 -0400579 if (!formatCaps.stencilRendering)
580 {
581 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
582 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000583 }
584
585 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000586 {
Jamie Madille261b442014-06-25 12:42:21 -0400587 width = mStencilbuffer->getWidth();
588 height = mStencilbuffer->getHeight();
589 samples = mStencilbuffer->getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000590 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000591 }
Jamie Madille261b442014-06-25 12:42:21 -0400592 else if (width != mStencilbuffer->getWidth() || height != mStencilbuffer->getHeight())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000593 {
594 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
595 }
Jamie Madille261b442014-06-25 12:42:21 -0400596 else if (samples != mStencilbuffer->getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000597 {
598 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
599 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000600 }
601
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000602 // if we have both a depth and stencil buffer, they must refer to the same object
603 // since we only support packed_depth_stencil and not separate depth and stencil
Jamie Madille261b442014-06-25 12:42:21 -0400604 if (mDepthbuffer && mStencilbuffer && !hasValidDepthStencil())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000605 {
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000606 return GL_FRAMEBUFFER_UNSUPPORTED;
607 }
608
609 // we need to have at least one attachment to be complete
610 if (missingAttachment)
611 {
612 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000613 }
614
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000615 return GL_FRAMEBUFFER_COMPLETE;
616}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000617
daniel@transgaming.com16418b12012-11-28 19:32:22 +0000618DefaultFramebuffer::DefaultFramebuffer(rx::Renderer *renderer, Colorbuffer *colorbuffer, DepthStencilbuffer *depthStencil)
619 : Framebuffer(renderer)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000620{
Geoff Lange4a492b2014-06-19 14:14:41 -0400621 Renderbuffer *colorRenderbuffer = new Renderbuffer(0, colorbuffer);
Jamie Madille261b442014-06-25 12:42:21 -0400622 mColorbuffers[0] = new RenderbufferAttachment(colorRenderbuffer);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000623
Jamie Madill28bedaf2014-06-26 13:17:22 -0400624 Renderbuffer *depthStencilBuffer = new Renderbuffer(0, depthStencil);
625
626 // Make a new attachment objects to ensure we do not double-delete
627 // See angle issue 686
628 mDepthbuffer = (depthStencilBuffer->getDepthSize() != 0 ? new RenderbufferAttachment(depthStencilBuffer) : NULL);
629 mStencilbuffer = (depthStencilBuffer->getStencilSize() != 0 ? new RenderbufferAttachment(depthStencilBuffer) : NULL);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000630
631 mDrawBufferStates[0] = GL_BACK;
632 mReadBufferState = GL_BACK;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000633}
634
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000635int Framebuffer::getSamples() const
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000636{
637 if (completeness() == GL_FRAMEBUFFER_COMPLETE)
638 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000639 // for a complete framebuffer, all attachments must have the same sample count
640 // in this case return the first nonzero sample size
641 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
642 {
Jamie Madille261b442014-06-25 12:42:21 -0400643 if (mColorbuffers[colorAttachment])
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000644 {
Jamie Madille261b442014-06-25 12:42:21 -0400645 return mColorbuffers[colorAttachment]->getSamples();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000646 }
647 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000648 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000649
650 return 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000651}
652
Jamie Madille261b442014-06-25 12:42:21 -0400653bool Framebuffer::hasValidDepthStencil() const
654{
655 // A valid depth-stencil attachment has the same resource bound to both the
656 // depth and stencil attachment points.
657 return (mDepthbuffer && mStencilbuffer &&
658 mDepthbuffer->type() == mStencilbuffer->type() &&
659 mDepthbuffer->id() == mStencilbuffer->id());
660}
661
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000662GLenum DefaultFramebuffer::completeness() const
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000663{
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000664 // The default framebuffer *must* always be complete, though it may not be
665 // subject to the same rules as application FBOs. ie, it could have 0x0 size.
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000666 return GL_FRAMEBUFFER_COMPLETE;
667}
668
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669}