blob: b220f1d9787293f4148bf62e53b84e85c005a477 [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;
163 mStencilbuffer = attachment;
Geoff Lang55ba29c2013-07-11 16:57:53 -0400164 }
165}
166
Jamie Madille261b442014-06-25 12:42:21 -0400167void Framebuffer::detachTexture(GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000168{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000169 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170 {
Jamie Madille261b442014-06-25 12:42:21 -0400171 FramebufferAttachment *attachment = mColorbuffers[colorAttachment];
172
173 if (attachment && attachment->isTextureWithId(textureId))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000174 {
Jamie Madille261b442014-06-25 12:42:21 -0400175 SafeDelete(mColorbuffers[colorAttachment]);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177 }
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000178
Jamie Madille261b442014-06-25 12:42:21 -0400179 if (mDepthbuffer && mDepthbuffer->isTextureWithId(textureId))
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000180 {
Jamie Madille261b442014-06-25 12:42:21 -0400181 SafeDelete(mDepthbuffer);
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000182 }
183
Jamie Madille261b442014-06-25 12:42:21 -0400184 if (mStencilbuffer && mStencilbuffer->isTextureWithId(textureId))
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000185 {
Jamie Madille261b442014-06-25 12:42:21 -0400186 SafeDelete(mStencilbuffer);
daniel@transgaming.comfbc09532010-04-26 15:33:41 +0000187 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188}
189
Jamie Madille261b442014-06-25 12:42:21 -0400190void Framebuffer::detachRenderbuffer(GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000192 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193 {
Jamie Madille261b442014-06-25 12:42:21 -0400194 FramebufferAttachment *attachment = mColorbuffers[colorAttachment];
195
196 if (attachment && attachment->isRenderbufferWithId(renderbufferId))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000197 {
Jamie Madille261b442014-06-25 12:42:21 -0400198 SafeDelete(mColorbuffers[colorAttachment]);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000199 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200 }
201
Jamie Madille261b442014-06-25 12:42:21 -0400202 if (mDepthbuffer && mDepthbuffer->isRenderbufferWithId(renderbufferId))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203 {
Jamie Madille261b442014-06-25 12:42:21 -0400204 SafeDelete(mDepthbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205 }
206
Jamie Madille261b442014-06-25 12:42:21 -0400207 if (mStencilbuffer && mStencilbuffer->isRenderbufferWithId(renderbufferId))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000208 {
Jamie Madille261b442014-06-25 12:42:21 -0400209 SafeDelete(mStencilbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000210 }
211}
212
Jamie Madill3c7fa222014-06-05 13:08:51 -0400213FramebufferAttachment *Framebuffer::getColorbuffer(unsigned int colorAttachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000214{
215 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
Jamie Madille261b442014-06-25 12:42:21 -0400216 return mColorbuffers[colorAttachment];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000217}
218
Jamie Madill3c7fa222014-06-05 13:08:51 -0400219FramebufferAttachment *Framebuffer::getDepthbuffer() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220{
Jamie Madille261b442014-06-25 12:42:21 -0400221 return mDepthbuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222}
223
Jamie Madill3c7fa222014-06-05 13:08:51 -0400224FramebufferAttachment *Framebuffer::getStencilbuffer() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225{
Jamie Madille261b442014-06-25 12:42:21 -0400226 return mStencilbuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227}
228
Jamie Madill3c7fa222014-06-05 13:08:51 -0400229FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400230{
Jamie Madille261b442014-06-25 12:42:21 -0400231 return (hasValidDepthStencil() ? mDepthbuffer : NULL);
Geoff Lang646559f2013-08-15 11:08:15 -0400232}
233
Jamie Madill3c7fa222014-06-05 13:08:51 -0400234FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000235{
Jamie Madille261b442014-06-25 12:42:21 -0400236 FramebufferAttachment *depthstencilbuffer = mDepthbuffer;
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000237
238 if (!depthstencilbuffer)
239 {
Jamie Madille261b442014-06-25 12:42:21 -0400240 depthstencilbuffer = mStencilbuffer;
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000241 }
242
243 return depthstencilbuffer;
244}
245
Jamie Madill3c7fa222014-06-05 13:08:51 -0400246FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000247{
248 // Will require more logic if glReadBuffers is supported
Jamie Madille261b442014-06-25 12:42:21 -0400249 return mColorbuffers[0];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000250}
251
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000252GLenum Framebuffer::getReadColorbufferType() const
253{
254 // Will require more logic if glReadBuffers is supported
Jamie Madille261b442014-06-25 12:42:21 -0400255 return (mColorbuffers[0] ? mColorbuffers[0]->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000256}
257
Jamie Madill3c7fa222014-06-05 13:08:51 -0400258FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000259{
260 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
261 {
Jamie Madille261b442014-06-25 12:42:21 -0400262 if (mColorbuffers[colorAttachment])
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000263 {
Jamie Madille261b442014-06-25 12:42:21 -0400264 return mColorbuffers[colorAttachment];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000265 }
266 }
267
268 return NULL;
269}
270
271GLenum Framebuffer::getColorbufferType(unsigned int colorAttachment) const
272{
273 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
Jamie Madille261b442014-06-25 12:42:21 -0400274 return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000275}
276
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000277GLenum Framebuffer::getDepthbufferType() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000278{
Jamie Madille261b442014-06-25 12:42:21 -0400279 return (mDepthbuffer ? mDepthbuffer->type() : GL_NONE);
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000280}
281
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000282GLenum Framebuffer::getStencilbufferType() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000283{
Jamie Madille261b442014-06-25 12:42:21 -0400284 return (mStencilbuffer ? mStencilbuffer->type() : GL_NONE);
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000285}
286
Geoff Lang55ba29c2013-07-11 16:57:53 -0400287GLenum Framebuffer::getDepthStencilbufferType() const
288{
Jamie Madille261b442014-06-25 12:42:21 -0400289 return (hasValidDepthStencil() ? mDepthbuffer->type() : GL_NONE);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400290}
291
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000292GLuint Framebuffer::getColorbufferHandle(unsigned int colorAttachment) const
293{
294 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
Jamie Madille261b442014-06-25 12:42:21 -0400295 return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->id() : 0);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000296}
297
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000298GLuint Framebuffer::getDepthbufferHandle() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000299{
Jamie Madille261b442014-06-25 12:42:21 -0400300 return (mDepthbuffer ? mDepthbuffer->id() : 0);
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000301}
302
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000303GLuint Framebuffer::getStencilbufferHandle() const
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000304{
Jamie Madille261b442014-06-25 12:42:21 -0400305 return (mStencilbuffer ? mStencilbuffer->id() : 0);
daniel@transgaming.comc46c9c02010-04-23 18:34:55 +0000306}
307
Jamie Madille261b442014-06-25 12:42:21 -0400308GLuint Framebuffer::getDepthStencilbufferHandle() const
Geoff Lang55ba29c2013-07-11 16:57:53 -0400309{
Jamie Madille261b442014-06-25 12:42:21 -0400310 return (hasValidDepthStencil() ? mDepthbuffer->id() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400311}
312
Jamie Madille261b442014-06-25 12:42:21 -0400313GLint Framebuffer::getColorbufferMipLevel(unsigned int colorAttachment) const
Geoff Langc90d73a2013-07-22 16:39:23 -0400314{
315 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
Jamie Madille261b442014-06-25 12:42:21 -0400316 return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->mipLevel() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400317}
318
Jamie Madille261b442014-06-25 12:42:21 -0400319GLint Framebuffer::getDepthbufferMipLevel() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400320{
Jamie Madille261b442014-06-25 12:42:21 -0400321 return (mDepthbuffer ? mDepthbuffer->mipLevel() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400322}
323
Jamie Madille261b442014-06-25 12:42:21 -0400324GLint Framebuffer::getStencilbufferMipLevel() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400325{
Jamie Madille261b442014-06-25 12:42:21 -0400326 return (mStencilbuffer ? mStencilbuffer->mipLevel() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400327}
328
Jamie Madille261b442014-06-25 12:42:21 -0400329GLint Framebuffer::getDepthStencilbufferMipLevel() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400330{
Jamie Madille261b442014-06-25 12:42:21 -0400331 return (hasValidDepthStencil() ? mDepthbuffer->mipLevel() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400332}
333
Jamie Madille261b442014-06-25 12:42:21 -0400334GLint Framebuffer::getColorbufferLayer(unsigned int colorAttachment) const
Geoff Langc90d73a2013-07-22 16:39:23 -0400335{
336 ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
Jamie Madille261b442014-06-25 12:42:21 -0400337 return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->layer() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400338}
339
Jamie Madille261b442014-06-25 12:42:21 -0400340GLint Framebuffer::getDepthbufferLayer() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400341{
Jamie Madille261b442014-06-25 12:42:21 -0400342 return (mDepthbuffer ? mDepthbuffer->layer() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400343}
344
Jamie Madille261b442014-06-25 12:42:21 -0400345GLint Framebuffer::getStencilbufferLayer() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400346{
Jamie Madille261b442014-06-25 12:42:21 -0400347 return (mStencilbuffer ? mStencilbuffer->layer() : 0);
Geoff Langc90d73a2013-07-22 16:39:23 -0400348}
349
Jamie Madille261b442014-06-25 12:42:21 -0400350GLint Framebuffer::getDepthStencilbufferLayer() const
Geoff Langc90d73a2013-07-22 16:39:23 -0400351{
Jamie Madille261b442014-06-25 12:42:21 -0400352 return (hasValidDepthStencil() ? mDepthbuffer->layer() : 0);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400353}
354
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000355GLenum Framebuffer::getDrawBufferState(unsigned int colorAttachment) const
356{
357 return mDrawBufferStates[colorAttachment];
358}
359
360void Framebuffer::setDrawBufferState(unsigned int colorAttachment, GLenum drawBuffer)
361{
362 mDrawBufferStates[colorAttachment] = drawBuffer;
363}
364
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +0000365bool Framebuffer::isEnabledColorAttachment(unsigned int colorAttachment) const
366{
Jamie Madille261b442014-06-25 12:42:21 -0400367 return (mColorbuffers[colorAttachment] && mDrawBufferStates[colorAttachment] != GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +0000368}
369
370bool Framebuffer::hasEnabledColorAttachment() const
371{
372 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
373 {
374 if (isEnabledColorAttachment(colorAttachment))
375 {
376 return true;
377 }
378 }
379
380 return false;
381}
382
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000383bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000384{
Geoff Lange4a492b2014-06-19 14:14:41 -0400385 return (mStencilbuffer && mStencilbuffer->getStencilSize() > 0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000386}
387
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000388bool Framebuffer::usingExtendedDrawBuffers() const
389{
390 for (unsigned int colorAttachment = 1; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
391 {
392 if (isEnabledColorAttachment(colorAttachment))
393 {
394 return true;
395 }
396 }
397
398 return false;
399}
400
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000401GLenum Framebuffer::completeness() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403 int width = 0;
404 int height = 0;
shannonwoods@chromium.orgf6fb9592013-05-30 00:09:40 +0000405 unsigned int colorbufferSize = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000406 int samples = -1;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000407 bool missingAttachment = true;
shannonwoods@chromium.orgf6fb9592013-05-30 00:09:40 +0000408 GLuint clientVersion = mRenderer->getCurrentClientVersion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000409
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000410 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411 {
Jamie Madillbb94f342014-06-23 15:23:02 -0400412 const FramebufferAttachment *colorbuffer = mColorbuffers[colorAttachment];
413
414 if (colorbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000416 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000417 {
418 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
419 }
daniel@transgaming.com01868132010-08-24 19:21:17 +0000420
Geoff Langcec35902014-04-16 10:52:36 -0400421 GLenum internalformat = colorbuffer->getInternalFormat();
422 const TextureCaps &formatCaps = mRenderer->getCaps().textureCaps.get(internalformat);
Jamie Madillbb94f342014-06-23 15:23:02 -0400423 if (colorbuffer->isTexture())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000424 {
Geoff Langcec35902014-04-16 10:52:36 -0400425 if (!formatCaps.colorRendering)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000426 {
427 return GL_FRAMEBUFFER_UNSUPPORTED;
428 }
429
Geoff Lange4a492b2014-06-19 14:14:41 -0400430 if (gl::GetDepthBits(internalformat) > 0 ||
431 gl::GetStencilBits(internalformat) > 0)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000432 {
433 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
434 }
435 }
436 else
437 {
Jamie Madillbb94f342014-06-23 15:23:02 -0400438 if (!formatCaps.colorRendering)
439 {
440 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
441 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000442 }
443
444 if (!missingAttachment)
445 {
446 // all color attachments must have the same width and height
447 if (colorbuffer->getWidth() != width || colorbuffer->getHeight() != height)
448 {
449 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
450 }
451
452 // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that
453 // all color attachments have the same number of samples for the FBO to be complete.
454 if (colorbuffer->getSamples() != samples)
455 {
456 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT;
457 }
458
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +0000459 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
460 // in GLES 3.0, there is no such restriction
shannonwoods@chromium.orgf6fb9592013-05-30 00:09:40 +0000461 if (clientVersion < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000462 {
Geoff Lange4a492b2014-06-19 14:14:41 -0400463 if (gl::GetPixelBytes(colorbuffer->getInternalFormat()) != colorbufferSize)
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +0000464 {
465 return GL_FRAMEBUFFER_UNSUPPORTED;
466 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000467 }
468
469 // D3D11 does not allow for overlapping RenderTargetViews, so ensure uniqueness
470 for (unsigned int previousColorAttachment = 0; previousColorAttachment < colorAttachment; previousColorAttachment++)
471 {
Jamie Madille261b442014-06-25 12:42:21 -0400472 if (colorbuffer->id() == getColorbufferHandle(previousColorAttachment) &&
473 colorbuffer->type() == getColorbufferType(previousColorAttachment))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000474 {
475 return GL_FRAMEBUFFER_UNSUPPORTED;
476 }
477 }
478 }
479 else
480 {
481 width = colorbuffer->getWidth();
482 height = colorbuffer->getHeight();
483 samples = colorbuffer->getSamples();
Geoff Lange4a492b2014-06-19 14:14:41 -0400484 colorbufferSize = gl::GetPixelBytes(colorbuffer->getInternalFormat());
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000485 missingAttachment = false;
486 }
487 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000488 }
489
Jamie Madille261b442014-06-25 12:42:21 -0400490 if (mDepthbuffer)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000491 {
Jamie Madille261b442014-06-25 12:42:21 -0400492 if (mDepthbuffer->getWidth() == 0 || mDepthbuffer->getHeight() == 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000494 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495 }
496
Jamie Madille261b442014-06-25 12:42:21 -0400497 GLenum internalformat = mDepthbuffer->getInternalFormat();
Geoff Langcec35902014-04-16 10:52:36 -0400498 const TextureCaps &formatCaps = mRenderer->getCaps().textureCaps.get(internalformat);
Jamie Madillbb94f342014-06-23 15:23:02 -0400499 if (mDepthbuffer->isTexture())
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000500 {
Jamie Madille261b442014-06-25 12:42:21 -0400501 GLenum internalformat = mDepthbuffer->getInternalFormat();
502
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000503 // depth texture attachments require OES/ANGLE_depth_texture
Geoff Langcec35902014-04-16 10:52:36 -0400504 if (!mRenderer->getCaps().extensions.depthTextures)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000505 {
506 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
507 }
508
Geoff Langcec35902014-04-16 10:52:36 -0400509 if (!formatCaps.depthRendering)
510 {
511 return GL_FRAMEBUFFER_UNSUPPORTED;
512 }
513
Geoff Lange4a492b2014-06-19 14:14:41 -0400514 if (gl::GetDepthBits(internalformat) == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000515 {
516 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
517 }
518 }
519 else
520 {
Jamie Madillbb94f342014-06-23 15:23:02 -0400521 if (!formatCaps.depthRendering)
522 {
523 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
524 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000525 }
526
527 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000528 {
Jamie Madille261b442014-06-25 12:42:21 -0400529 width = mDepthbuffer->getWidth();
530 height = mDepthbuffer->getHeight();
531 samples = mDepthbuffer->getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000532 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000533 }
Jamie Madille261b442014-06-25 12:42:21 -0400534 else if (width != mDepthbuffer->getWidth() || height != mDepthbuffer->getHeight())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000535 {
536 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
537 }
Jamie Madille261b442014-06-25 12:42:21 -0400538 else if (samples != mDepthbuffer->getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000539 {
540 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
541 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000542 }
543
Jamie Madille261b442014-06-25 12:42:21 -0400544 if (mStencilbuffer)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000545 {
Jamie Madille261b442014-06-25 12:42:21 -0400546 if (mStencilbuffer->getWidth() == 0 || mStencilbuffer->getHeight() == 0)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000547 {
548 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
549 }
550
Jamie Madille261b442014-06-25 12:42:21 -0400551 GLenum internalformat = mStencilbuffer->getInternalFormat();
Geoff Langcec35902014-04-16 10:52:36 -0400552 const TextureCaps &formatCaps = mRenderer->getCaps().textureCaps.get(internalformat);
Jamie Madillbb94f342014-06-23 15:23:02 -0400553 if (mStencilbuffer->isTexture())
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000554 {
Jamie Madille261b442014-06-25 12:42:21 -0400555 GLenum internalformat = mStencilbuffer->getInternalFormat();
556
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000557 // texture stencil attachments come along as part
558 // of OES_packed_depth_stencil + OES/ANGLE_depth_texture
Geoff Langcec35902014-04-16 10:52:36 -0400559 if (!mRenderer->getCaps().extensions.depthTextures)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000560 {
561 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
562 }
563
Geoff Langcec35902014-04-16 10:52:36 -0400564 if (!formatCaps.stencilRendering)
565 {
566 return GL_FRAMEBUFFER_UNSUPPORTED;
567 }
568
Geoff Lange4a492b2014-06-19 14:14:41 -0400569 if (gl::GetStencilBits(internalformat) == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000570 {
571 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
572 }
573 }
574 else
575 {
Jamie Madillbb94f342014-06-23 15:23:02 -0400576 if (!formatCaps.stencilRendering)
577 {
578 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
579 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000580 }
581
582 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000583 {
Jamie Madille261b442014-06-25 12:42:21 -0400584 width = mStencilbuffer->getWidth();
585 height = mStencilbuffer->getHeight();
586 samples = mStencilbuffer->getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000587 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000588 }
Jamie Madille261b442014-06-25 12:42:21 -0400589 else if (width != mStencilbuffer->getWidth() || height != mStencilbuffer->getHeight())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000590 {
591 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
592 }
Jamie Madille261b442014-06-25 12:42:21 -0400593 else if (samples != mStencilbuffer->getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000594 {
595 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
596 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000597 }
598
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000599 // if we have both a depth and stencil buffer, they must refer to the same object
600 // since we only support packed_depth_stencil and not separate depth and stencil
Jamie Madille261b442014-06-25 12:42:21 -0400601 if (mDepthbuffer && mStencilbuffer && !hasValidDepthStencil())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000602 {
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000603 return GL_FRAMEBUFFER_UNSUPPORTED;
604 }
605
606 // we need to have at least one attachment to be complete
607 if (missingAttachment)
608 {
609 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000610 }
611
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612 return GL_FRAMEBUFFER_COMPLETE;
613}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000614
daniel@transgaming.com16418b12012-11-28 19:32:22 +0000615DefaultFramebuffer::DefaultFramebuffer(rx::Renderer *renderer, Colorbuffer *colorbuffer, DepthStencilbuffer *depthStencil)
616 : Framebuffer(renderer)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000617{
Geoff Lange4a492b2014-06-19 14:14:41 -0400618 Renderbuffer *colorRenderbuffer = new Renderbuffer(0, colorbuffer);
Jamie Madille261b442014-06-25 12:42:21 -0400619 mColorbuffers[0] = new RenderbufferAttachment(colorRenderbuffer);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000620
Geoff Lange4a492b2014-06-19 14:14:41 -0400621 Renderbuffer *depthStencilRenderbuffer = new Renderbuffer(0, depthStencil);
Jamie Madille261b442014-06-25 12:42:21 -0400622 FramebufferAttachment *depthStencilAttachment = new RenderbufferAttachment(depthStencilRenderbuffer);
623 mDepthbuffer = (depthStencilRenderbuffer->getDepthSize() != 0 ? depthStencilAttachment : NULL);
624 mStencilbuffer = (depthStencilRenderbuffer->getStencilSize() != 0 ? depthStencilAttachment : NULL);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000625
626 mDrawBufferStates[0] = GL_BACK;
627 mReadBufferState = GL_BACK;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000628}
629
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000630int Framebuffer::getSamples() const
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000631{
632 if (completeness() == GL_FRAMEBUFFER_COMPLETE)
633 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000634 // for a complete framebuffer, all attachments must have the same sample count
635 // in this case return the first nonzero sample size
636 for (unsigned int colorAttachment = 0; colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
637 {
Jamie Madille261b442014-06-25 12:42:21 -0400638 if (mColorbuffers[colorAttachment])
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000639 {
Jamie Madille261b442014-06-25 12:42:21 -0400640 return mColorbuffers[colorAttachment]->getSamples();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000641 }
642 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000643 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000644
645 return 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000646}
647
Jamie Madille261b442014-06-25 12:42:21 -0400648bool Framebuffer::hasValidDepthStencil() const
649{
650 // A valid depth-stencil attachment has the same resource bound to both the
651 // depth and stencil attachment points.
652 return (mDepthbuffer && mStencilbuffer &&
653 mDepthbuffer->type() == mStencilbuffer->type() &&
654 mDepthbuffer->id() == mStencilbuffer->id());
655}
656
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000657GLenum DefaultFramebuffer::completeness() const
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000658{
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000659 // The default framebuffer *must* always be complete, though it may not be
660 // subject to the same rules as application FBOs. ie, it could have 0x0 size.
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000661 return GL_FRAMEBUFFER_COMPLETE;
662}
663
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664}