blob: 7e27bb77fb027846e10e33658b26abe63de71efd [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Langcec35902014-04-16 10:52:36 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
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.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Framebuffer.h"
11#include "libANGLE/formatutils.h"
12#include "libANGLE/Texture.h"
13#include "libANGLE/Context.h"
14#include "libANGLE/Renderbuffer.h"
15#include "libANGLE/FramebufferAttachment.h"
Geoff Langb5d8f232014-12-04 15:43:01 -050016#include "libANGLE/renderer/FramebufferImpl.h"
Jamie Madill48115b62015-03-16 10:46:57 -040017#include "libANGLE/renderer/ImplFactory.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050018#include "libANGLE/renderer/RenderbufferImpl.h"
19#include "libANGLE/renderer/Workarounds.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040020
21#include "common/utilities.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022
23namespace gl
24{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000025
Jamie Madilld1405e52015-03-05 15:41:39 -050026namespace
27{
28void DeleteMatchingAttachment(FramebufferAttachment *&attachment, GLenum matchType, GLuint matchId)
29{
30 if (attachment && attachment->type() == matchType && attachment->id() == matchId)
31 {
32 SafeDelete(attachment);
33 }
34}
35}
36
37Framebuffer::Data::Data(const Caps &caps)
38 : mColorAttachments(caps.maxColorAttachments, nullptr),
39 mDepthAttachment(nullptr),
40 mStencilAttachment(nullptr),
41 mDrawBufferStates(caps.maxDrawBuffers, GL_NONE),
42 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT)
43{
44 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
45}
46
47Framebuffer::Data::~Data()
48{
49 for (auto &colorAttachment : mColorAttachments)
50 {
51 SafeDelete(colorAttachment);
52 }
53 SafeDelete(mDepthAttachment);
54 SafeDelete(mStencilAttachment);
55}
56
Jamie Madill7147f012015-03-05 15:41:40 -050057FramebufferAttachment *Framebuffer::Data::getReadAttachment() const
58{
59 ASSERT(mReadBufferState == GL_BACK || (mReadBufferState >= GL_COLOR_ATTACHMENT0 && mReadBufferState <= GL_COLOR_ATTACHMENT15));
60 size_t readIndex = (mReadBufferState == GL_BACK ? 0 : static_cast<size_t>(mReadBufferState - GL_COLOR_ATTACHMENT0));
61 ASSERT(readIndex < mColorAttachments.size());
62 return mColorAttachments[readIndex];
63}
64
65FramebufferAttachment *Framebuffer::Data::getFirstColorAttachment() const
66{
67 for (FramebufferAttachment *colorAttachment : mColorAttachments)
68 {
69 if (colorAttachment != nullptr)
70 {
71 return colorAttachment;
72 }
73 }
74
75 return nullptr;
76}
77
78FramebufferAttachment *Framebuffer::Data::getDepthOrStencilAttachment() const
79{
80 return (mDepthAttachment != nullptr ? mDepthAttachment : mStencilAttachment);
81}
82
Jamie Madill48115b62015-03-16 10:46:57 -040083Framebuffer::Framebuffer(const Caps &caps, rx::ImplFactory *factory, GLuint id)
Jamie Madilld1405e52015-03-05 15:41:39 -050084 : mData(caps),
Geoff Lang4ad17092015-03-10 16:47:44 -040085 mImpl(nullptr),
Jamie Madilld1405e52015-03-05 15:41:39 -050086 mId(id)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087{
Geoff Lang4ad17092015-03-10 16:47:44 -040088 if (mId == 0)
89 {
90 mImpl = factory->createDefaultFramebuffer(mData);
91 }
92 else
93 {
94 mImpl = factory->createFramebuffer(mData);
95 }
Geoff Langda88add2014-12-01 10:22:01 -050096 ASSERT(mImpl != nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097}
98
99Framebuffer::~Framebuffer()
100{
Geoff Langda88add2014-12-01 10:22:01 -0500101 SafeDelete(mImpl);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000102}
103
Jamie Madille261b442014-06-25 12:42:21 -0400104void Framebuffer::detachTexture(GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105{
Jamie Madilld1405e52015-03-05 15:41:39 -0500106 detachResourceById(GL_TEXTURE, textureId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107}
108
Jamie Madille261b442014-06-25 12:42:21 -0400109void Framebuffer::detachRenderbuffer(GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110{
Jamie Madilld1405e52015-03-05 15:41:39 -0500111 detachResourceById(GL_RENDERBUFFER, renderbufferId);
112}
Jamie Madille261b442014-06-25 12:42:21 -0400113
Jamie Madilld1405e52015-03-05 15:41:39 -0500114void Framebuffer::detachResourceById(GLenum resourceType, GLuint resourceId)
115{
116 for (auto &colorAttachment : mData.mColorAttachments)
117 {
118 DeleteMatchingAttachment(colorAttachment, resourceType, resourceId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 }
120
Jamie Madilld1405e52015-03-05 15:41:39 -0500121 DeleteMatchingAttachment(mData.mDepthAttachment, resourceType, resourceId);
122 DeleteMatchingAttachment(mData.mStencilAttachment, resourceType, resourceId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123}
124
Jamie Madill3c7fa222014-06-05 13:08:51 -0400125FramebufferAttachment *Framebuffer::getColorbuffer(unsigned int colorAttachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000126{
Jamie Madilld1405e52015-03-05 15:41:39 -0500127 ASSERT(colorAttachment < mData.mColorAttachments.size());
128 return mData.mColorAttachments[colorAttachment];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000129}
130
Jamie Madill3c7fa222014-06-05 13:08:51 -0400131FramebufferAttachment *Framebuffer::getDepthbuffer() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132{
Jamie Madilld1405e52015-03-05 15:41:39 -0500133 return mData.mDepthAttachment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134}
135
Jamie Madill3c7fa222014-06-05 13:08:51 -0400136FramebufferAttachment *Framebuffer::getStencilbuffer() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137{
Jamie Madilld1405e52015-03-05 15:41:39 -0500138 return mData.mStencilAttachment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139}
140
Jamie Madill3c7fa222014-06-05 13:08:51 -0400141FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400142{
Jamie Madilld1405e52015-03-05 15:41:39 -0500143 return (hasValidDepthStencil() ? mData.mDepthAttachment : NULL);
Geoff Lang646559f2013-08-15 11:08:15 -0400144}
145
Jamie Madill3c7fa222014-06-05 13:08:51 -0400146FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000147{
Jamie Madill7147f012015-03-05 15:41:40 -0500148 return mData.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000149}
150
Jamie Madill3c7fa222014-06-05 13:08:51 -0400151FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000152{
Jamie Madill7147f012015-03-05 15:41:40 -0500153 return mData.getReadAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000154}
155
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000156GLenum Framebuffer::getReadColorbufferType() const
157{
Jamie Madill7147f012015-03-05 15:41:40 -0500158 FramebufferAttachment *readAttachment = mData.getReadAttachment();
Jamie Madillb885e572015-02-03 16:16:04 -0500159 return (readAttachment ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000160}
161
Jamie Madill3c7fa222014-06-05 13:08:51 -0400162FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000163{
Jamie Madill7147f012015-03-05 15:41:40 -0500164 return mData.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000165}
166
Jamie Madille92a3542014-07-03 10:38:58 -0400167FramebufferAttachment *Framebuffer::getAttachment(GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000168{
Jamie Madille92a3542014-07-03 10:38:58 -0400169 if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15)
170 {
171 return getColorbuffer(attachment - GL_COLOR_ATTACHMENT0);
172 }
173 else
174 {
175 switch (attachment)
176 {
Geoff Lang528ce3c2014-12-01 10:44:07 -0500177 case GL_COLOR:
178 case GL_BACK:
179 return getColorbuffer(0);
180 case GL_DEPTH:
Jamie Madille92a3542014-07-03 10:38:58 -0400181 case GL_DEPTH_ATTACHMENT:
182 return getDepthbuffer();
Geoff Lang528ce3c2014-12-01 10:44:07 -0500183 case GL_STENCIL:
Jamie Madille92a3542014-07-03 10:38:58 -0400184 case GL_STENCIL_ATTACHMENT:
185 return getStencilbuffer();
Geoff Lang528ce3c2014-12-01 10:44:07 -0500186 case GL_DEPTH_STENCIL:
Jamie Madille92a3542014-07-03 10:38:58 -0400187 case GL_DEPTH_STENCIL_ATTACHMENT:
188 return getDepthStencilBuffer();
189 default:
190 UNREACHABLE();
191 return NULL;
192 }
193 }
Geoff Lang55ba29c2013-07-11 16:57:53 -0400194}
195
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000196GLenum Framebuffer::getDrawBufferState(unsigned int colorAttachment) const
197{
Jamie Madilld1405e52015-03-05 15:41:39 -0500198 ASSERT(colorAttachment < mData.mDrawBufferStates.size());
199 return mData.mDrawBufferStates[colorAttachment];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000200}
201
Geoff Lang164d54e2014-12-01 10:55:33 -0500202void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000203{
Jamie Madilld1405e52015-03-05 15:41:39 -0500204 auto &drawStates = mData.mDrawBufferStates;
205
206 ASSERT(count <= drawStates.size());
207 std::copy(buffers, buffers + count, drawStates.begin());
208 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Geoff Lang9dd95802014-12-01 11:12:59 -0500209 mImpl->setDrawBuffers(count, buffers);
210}
211
212GLenum Framebuffer::getReadBufferState() const
213{
Jamie Madilld1405e52015-03-05 15:41:39 -0500214 return mData.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500215}
216
217void Framebuffer::setReadBuffer(GLenum buffer)
218{
Jamie Madillb885e572015-02-03 16:16:04 -0500219 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
220 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madilld1405e52015-03-05 15:41:39 -0500221 (buffer - GL_COLOR_ATTACHMENT0) < mData.mColorAttachments.size()));
222 mData.mReadBufferState = buffer;
Geoff Lang9dd95802014-12-01 11:12:59 -0500223 mImpl->setReadBuffer(buffer);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000224}
225
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +0000226bool Framebuffer::isEnabledColorAttachment(unsigned int colorAttachment) const
227{
Jamie Madilld1405e52015-03-05 15:41:39 -0500228 ASSERT(colorAttachment < mData.mColorAttachments.size());
229 return (mData.mColorAttachments[colorAttachment] &&
230 mData.mDrawBufferStates[colorAttachment] != GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +0000231}
232
233bool Framebuffer::hasEnabledColorAttachment() const
234{
Jamie Madilld1405e52015-03-05 15:41:39 -0500235 for (size_t colorAttachment = 0; colorAttachment < mData.mColorAttachments.size(); ++colorAttachment)
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +0000236 {
237 if (isEnabledColorAttachment(colorAttachment))
238 {
239 return true;
240 }
241 }
242
243 return false;
244}
245
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000246bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000247{
Jamie Madilld1405e52015-03-05 15:41:39 -0500248 return (mData.mStencilAttachment && mData.mStencilAttachment->getStencilSize() > 0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000249}
250
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000251bool Framebuffer::usingExtendedDrawBuffers() const
252{
Jamie Madilld1405e52015-03-05 15:41:39 -0500253 for (size_t colorAttachment = 1; colorAttachment < mData.mColorAttachments.size(); ++colorAttachment)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000254 {
255 if (isEnabledColorAttachment(colorAttachment))
256 {
257 return true;
258 }
259 }
260
261 return false;
262}
263
Geoff Lang748f74e2014-12-01 11:25:34 -0500264GLenum Framebuffer::checkStatus(const gl::Data &data) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265{
Geoff Lang528ce3c2014-12-01 10:44:07 -0500266 // The default framebuffer *must* always be complete, though it may not be
267 // subject to the same rules as application FBOs. ie, it could have 0x0 size.
268 if (mId == 0)
269 {
270 return GL_FRAMEBUFFER_COMPLETE;
271 }
272
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000273 int width = 0;
274 int height = 0;
shannonwoods@chromium.orgf6fb9592013-05-30 00:09:40 +0000275 unsigned int colorbufferSize = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000276 int samples = -1;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000277 bool missingAttachment = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278
Jamie Madilld1405e52015-03-05 15:41:39 -0500279 for (const FramebufferAttachment *colorAttachment : mData.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500281 if (colorAttachment != nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500283 if (colorAttachment->getWidth() == 0 || colorAttachment->getHeight() == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000284 {
285 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
286 }
daniel@transgaming.com01868132010-08-24 19:21:17 +0000287
Jamie Madilld1405e52015-03-05 15:41:39 -0500288 GLenum internalformat = colorAttachment->getInternalFormat();
Jamie Madill48faf802014-11-06 15:27:22 -0500289 const TextureCaps &formatCaps = data.textureCaps->get(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400290 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
Jamie Madilld1405e52015-03-05 15:41:39 -0500291 if (colorAttachment->type() == GL_TEXTURE)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000292 {
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400293 if (!formatCaps.renderable)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000294 {
295 return GL_FRAMEBUFFER_UNSUPPORTED;
296 }
297
Geoff Lang5d601382014-07-22 15:14:06 -0400298 if (formatInfo.depthBits > 0 || formatInfo.stencilBits > 0)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000299 {
300 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
301 }
302 }
Jamie Madilld1405e52015-03-05 15:41:39 -0500303 else if (colorAttachment->type() == GL_RENDERBUFFER)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000304 {
Geoff Lang5d601382014-07-22 15:14:06 -0400305 if (!formatCaps.renderable || formatInfo.depthBits > 0 || formatInfo.stencilBits > 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400306 {
307 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
308 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000309 }
310
311 if (!missingAttachment)
312 {
313 // all color attachments must have the same width and height
Jamie Madilld1405e52015-03-05 15:41:39 -0500314 if (colorAttachment->getWidth() != width || colorAttachment->getHeight() != height)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000315 {
316 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
317 }
318
319 // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that
320 // all color attachments have the same number of samples for the FBO to be complete.
Jamie Madilld1405e52015-03-05 15:41:39 -0500321 if (colorAttachment->getSamples() != samples)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000322 {
323 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT;
324 }
325
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +0000326 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
327 // in GLES 3.0, there is no such restriction
Jamie Madill48faf802014-11-06 15:27:22 -0500328 if (data.clientVersion < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000329 {
Geoff Lang5d601382014-07-22 15:14:06 -0400330 if (formatInfo.pixelBytes != colorbufferSize)
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +0000331 {
332 return GL_FRAMEBUFFER_UNSUPPORTED;
333 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000334 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000335 }
336 else
337 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500338 width = colorAttachment->getWidth();
339 height = colorAttachment->getHeight();
340 samples = colorAttachment->getSamples();
Geoff Lang5d601382014-07-22 15:14:06 -0400341 colorbufferSize = formatInfo.pixelBytes;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000342 missingAttachment = false;
343 }
344 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000345 }
346
Jamie Madilld1405e52015-03-05 15:41:39 -0500347 const FramebufferAttachment *depthAttachment = mData.mDepthAttachment;
348 if (depthAttachment != nullptr)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000349 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500350 if (depthAttachment->getWidth() == 0 || depthAttachment->getHeight() == 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000352 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353 }
354
Jamie Madilld1405e52015-03-05 15:41:39 -0500355 GLenum internalformat = depthAttachment->getInternalFormat();
Jamie Madill48faf802014-11-06 15:27:22 -0500356 const TextureCaps &formatCaps = data.textureCaps->get(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400357 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
Jamie Madilld1405e52015-03-05 15:41:39 -0500358 if (depthAttachment->type() == GL_TEXTURE)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000359 {
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000360 // depth texture attachments require OES/ANGLE_depth_texture
Jamie Madill48faf802014-11-06 15:27:22 -0500361 if (!data.extensions->depthTextures)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000362 {
363 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
364 }
365
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400366 if (!formatCaps.renderable)
Geoff Langcec35902014-04-16 10:52:36 -0400367 {
368 return GL_FRAMEBUFFER_UNSUPPORTED;
369 }
370
Geoff Lang5d601382014-07-22 15:14:06 -0400371 if (formatInfo.depthBits == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000372 {
373 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
374 }
375 }
Jamie Madilld1405e52015-03-05 15:41:39 -0500376 else if (depthAttachment->type() == GL_RENDERBUFFER)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000377 {
Geoff Lang5d601382014-07-22 15:14:06 -0400378 if (!formatCaps.renderable || formatInfo.depthBits == 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400379 {
380 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
381 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000382 }
383
384 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000385 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500386 width = depthAttachment->getWidth();
387 height = depthAttachment->getHeight();
388 samples = depthAttachment->getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000389 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000390 }
Jamie Madilld1405e52015-03-05 15:41:39 -0500391 else if (width != depthAttachment->getWidth() || height != depthAttachment->getHeight())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000392 {
393 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
394 }
Jamie Madilld1405e52015-03-05 15:41:39 -0500395 else if (samples != depthAttachment->getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000396 {
397 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
398 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000399 }
400
Jamie Madilld1405e52015-03-05 15:41:39 -0500401 const FramebufferAttachment *stencilAttachment = mData.mStencilAttachment;
402 if (stencilAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000403 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500404 if (stencilAttachment->getWidth() == 0 || stencilAttachment->getHeight() == 0)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000405 {
406 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
407 }
408
Jamie Madilld1405e52015-03-05 15:41:39 -0500409 GLenum internalformat = stencilAttachment->getInternalFormat();
Jamie Madill48faf802014-11-06 15:27:22 -0500410 const TextureCaps &formatCaps = data.textureCaps->get(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400411 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
Jamie Madilld1405e52015-03-05 15:41:39 -0500412 if (stencilAttachment->type() == GL_TEXTURE)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000413 {
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000414 // texture stencil attachments come along as part
415 // of OES_packed_depth_stencil + OES/ANGLE_depth_texture
Jamie Madill48faf802014-11-06 15:27:22 -0500416 if (!data.extensions->depthTextures)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000417 {
418 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
419 }
420
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400421 if (!formatCaps.renderable)
Geoff Langcec35902014-04-16 10:52:36 -0400422 {
423 return GL_FRAMEBUFFER_UNSUPPORTED;
424 }
425
Geoff Lang5d601382014-07-22 15:14:06 -0400426 if (formatInfo.stencilBits == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000427 {
428 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
429 }
430 }
Jamie Madilld1405e52015-03-05 15:41:39 -0500431 else if (stencilAttachment->type() == GL_RENDERBUFFER)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000432 {
Geoff Lang5d601382014-07-22 15:14:06 -0400433 if (!formatCaps.renderable || formatInfo.stencilBits == 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400434 {
435 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
436 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000437 }
438
439 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000440 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500441 width = stencilAttachment->getWidth();
442 height = stencilAttachment->getHeight();
443 samples = stencilAttachment->getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000444 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000445 }
Jamie Madilld1405e52015-03-05 15:41:39 -0500446 else if (width != stencilAttachment->getWidth() || height != stencilAttachment->getHeight())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000447 {
448 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
449 }
Jamie Madilld1405e52015-03-05 15:41:39 -0500450 else if (samples != stencilAttachment->getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000451 {
452 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
453 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000454 }
455
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000456 // if we have both a depth and stencil buffer, they must refer to the same object
457 // since we only support packed_depth_stencil and not separate depth and stencil
Jamie Madilld1405e52015-03-05 15:41:39 -0500458 if (depthAttachment && stencilAttachment && !hasValidDepthStencil())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000459 {
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000460 return GL_FRAMEBUFFER_UNSUPPORTED;
461 }
462
463 // we need to have at least one attachment to be complete
464 if (missingAttachment)
465 {
466 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000467 }
468
Geoff Lang748f74e2014-12-01 11:25:34 -0500469 return mImpl->checkStatus();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000470}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000471
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500472Error Framebuffer::invalidate(size_t count, const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400473{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500474 return mImpl->invalidate(count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400475}
476
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500477Error Framebuffer::invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -0400478{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500479 return mImpl->invalidateSub(count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -0400480}
481
Jamie Madilld1f5ef22015-04-01 14:17:06 -0400482Error Framebuffer::clear(const gl::Data &data, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -0500483{
Jamie Madilld1f5ef22015-04-01 14:17:06 -0400484 return mImpl->clear(data, mask);
Geoff Langb04dc822014-12-01 12:02:02 -0500485}
486
487Error Framebuffer::clearBufferfv(const State &state, GLenum buffer, GLint drawbuffer, const GLfloat *values)
488{
489 return mImpl->clearBufferfv(state, buffer, drawbuffer, values);
490}
491
492Error Framebuffer::clearBufferuiv(const State &state, GLenum buffer, GLint drawbuffer, const GLuint *values)
493{
494 return mImpl->clearBufferuiv(state, buffer, drawbuffer, values);
495}
496
497Error Framebuffer::clearBufferiv(const State &state, GLenum buffer, GLint drawbuffer, const GLint *values)
498{
499 return mImpl->clearBufferiv(state, buffer, drawbuffer, values);
500}
501
502Error Framebuffer::clearBufferfi(const State &state, GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil)
503{
504 return mImpl->clearBufferfi(state, buffer, drawbuffer, depth, stencil);
505}
506
Geoff Langbce529e2014-12-01 12:48:41 -0500507GLenum Framebuffer::getImplementationColorReadFormat() const
508{
509 return mImpl->getImplementationColorReadFormat();
510}
511
512GLenum Framebuffer::getImplementationColorReadType() const
513{
514 return mImpl->getImplementationColorReadType();
515}
516
517Error Framebuffer::readPixels(const gl::State &state, const gl::Rectangle &area, GLenum format, GLenum type, GLvoid *pixels) const
518{
519 return mImpl->readPixels(state, area, format, type, pixels);
520}
521
Geoff Lang54bd5a42014-12-01 12:51:04 -0500522Error Framebuffer::blit(const gl::State &state, const gl::Rectangle &sourceArea, const gl::Rectangle &destArea,
523 GLbitfield mask, GLenum filter, const gl::Framebuffer *sourceFramebuffer)
524{
525 return mImpl->blit(state, sourceArea, destArea, mask, filter, sourceFramebuffer);
526}
527
Jamie Madill48faf802014-11-06 15:27:22 -0500528int Framebuffer::getSamples(const gl::Data &data) const
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000529{
Geoff Lang748f74e2014-12-01 11:25:34 -0500530 if (checkStatus(data) == GL_FRAMEBUFFER_COMPLETE)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000531 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000532 // for a complete framebuffer, all attachments must have the same sample count
533 // in this case return the first nonzero sample size
Jamie Madilld1405e52015-03-05 15:41:39 -0500534 for (const FramebufferAttachment *colorAttachment : mData.mColorAttachments)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000535 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500536 if (colorAttachment != nullptr)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000537 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500538 return colorAttachment->getSamples();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000539 }
540 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000541 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000542
543 return 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000544}
545
Jamie Madille261b442014-06-25 12:42:21 -0400546bool Framebuffer::hasValidDepthStencil() const
547{
548 // A valid depth-stencil attachment has the same resource bound to both the
549 // depth and stencil attachment points.
Jamie Madilld1405e52015-03-05 15:41:39 -0500550 return (mData.mDepthAttachment && mData.mStencilAttachment &&
551 mData.mDepthAttachment->type() == mData.mStencilAttachment->type() &&
552 mData.mDepthAttachment->id() == mData.mStencilAttachment->id());
Jamie Madille261b442014-06-25 12:42:21 -0400553}
554
Geoff Langab75a052014-10-15 12:56:37 -0400555void Framebuffer::setTextureAttachment(GLenum attachment, Texture *texture, const ImageIndex &imageIndex)
556{
557 setAttachment(attachment, new TextureAttachment(attachment, texture, imageIndex));
558}
559
560void Framebuffer::setRenderbufferAttachment(GLenum attachment, Renderbuffer *renderbuffer)
561{
562 setAttachment(attachment, new RenderbufferAttachment(attachment, renderbuffer));
563}
564
565void Framebuffer::setNULLAttachment(GLenum attachment)
566{
567 setAttachment(attachment, NULL);
568}
569
570void Framebuffer::setAttachment(GLenum attachment, FramebufferAttachment *attachmentObj)
571{
Jamie Madilld1405e52015-03-05 15:41:39 -0500572 if (attachment >= GL_COLOR_ATTACHMENT0 && attachment < (GL_COLOR_ATTACHMENT0 + mData.mColorAttachments.size()))
Geoff Langab75a052014-10-15 12:56:37 -0400573 {
574 size_t colorAttachment = attachment - GL_COLOR_ATTACHMENT0;
Jamie Madilld1405e52015-03-05 15:41:39 -0500575 SafeDelete(mData.mColorAttachments[colorAttachment]);
576 mData.mColorAttachments[colorAttachment] = attachmentObj;
Geoff Lang9dd95802014-12-01 11:12:59 -0500577 mImpl->setColorAttachment(colorAttachment, attachmentObj);
Geoff Langab75a052014-10-15 12:56:37 -0400578 }
Geoff Lang528ce3c2014-12-01 10:44:07 -0500579 else if (attachment == GL_BACK)
580 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500581 SafeDelete(mData.mColorAttachments[0]);
582 mData.mColorAttachments[0] = attachmentObj;
Geoff Lang9dd95802014-12-01 11:12:59 -0500583 mImpl->setColorAttachment(0, attachmentObj);
Geoff Lang528ce3c2014-12-01 10:44:07 -0500584 }
585 else if (attachment == GL_DEPTH_ATTACHMENT || attachment == GL_DEPTH)
Geoff Langab75a052014-10-15 12:56:37 -0400586 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500587 SafeDelete(mData.mDepthAttachment);
588 mData.mDepthAttachment = attachmentObj;
Jamie Madillf90353e2015-03-05 19:37:58 -0500589 mImpl->setDepthAttachment(attachmentObj);
Geoff Langab75a052014-10-15 12:56:37 -0400590 }
Geoff Lang528ce3c2014-12-01 10:44:07 -0500591 else if (attachment == GL_STENCIL_ATTACHMENT || attachment == GL_STENCIL)
Geoff Langab75a052014-10-15 12:56:37 -0400592 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500593 SafeDelete(mData.mStencilAttachment);
594 mData.mStencilAttachment = attachmentObj;
Geoff Lang9dd95802014-12-01 11:12:59 -0500595 mImpl->setStencilAttachment(attachmentObj);
Geoff Langab75a052014-10-15 12:56:37 -0400596 }
Geoff Lang528ce3c2014-12-01 10:44:07 -0500597 else if (attachment == GL_DEPTH_STENCIL_ATTACHMENT || attachment == GL_DEPTH_STENCIL)
Geoff Langab75a052014-10-15 12:56:37 -0400598 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500599 SafeDelete(mData.mDepthAttachment);
600 SafeDelete(mData.mStencilAttachment);
Geoff Langab75a052014-10-15 12:56:37 -0400601
602 // ensure this is a legitimate depth+stencil format
603 if (attachmentObj && attachmentObj->getDepthSize() > 0 && attachmentObj->getStencilSize() > 0)
604 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500605 mData.mDepthAttachment = attachmentObj;
Jamie Madillf90353e2015-03-05 19:37:58 -0500606 mImpl->setDepthAttachment(attachmentObj);
Geoff Langab75a052014-10-15 12:56:37 -0400607
608 // Make a new attachment object to ensure we do not double-delete
609 // See angle issue 686
Geoff Lang6a1e6b92014-11-06 10:42:45 -0500610 if (attachmentObj->type() == GL_TEXTURE)
Geoff Langab75a052014-10-15 12:56:37 -0400611 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500612 mData.mStencilAttachment = new TextureAttachment(GL_DEPTH_STENCIL_ATTACHMENT, attachmentObj->getTexture(),
613 *attachmentObj->getTextureImageIndex());
614 mImpl->setStencilAttachment(mData.mStencilAttachment);
Geoff Langab75a052014-10-15 12:56:37 -0400615 }
Geoff Lang6a1e6b92014-11-06 10:42:45 -0500616 else if (attachmentObj->type() == GL_RENDERBUFFER)
Geoff Langab75a052014-10-15 12:56:37 -0400617 {
Jamie Madilld1405e52015-03-05 15:41:39 -0500618 mData.mStencilAttachment = new RenderbufferAttachment(GL_DEPTH_STENCIL_ATTACHMENT, attachmentObj->getRenderbuffer());
619 mImpl->setStencilAttachment(mData.mStencilAttachment);
Geoff Langab75a052014-10-15 12:56:37 -0400620 }
Geoff Lang6a1e6b92014-11-06 10:42:45 -0500621 else
622 {
623 UNREACHABLE();
624 }
Geoff Langab75a052014-10-15 12:56:37 -0400625 }
626 }
627 else
628 {
629 UNREACHABLE();
630 }
631}
632
Jamie Madill48115b62015-03-16 10:46:57 -0400633DefaultFramebuffer::DefaultFramebuffer(const Caps &caps, rx::ImplFactory *factory, egl::Surface *surface)
634 : Framebuffer(caps, factory, 0)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000635{
Jamie Madill48115b62015-03-16 10:46:57 -0400636 rx::DefaultAttachmentImpl *colorAttachment = factory->createDefaultAttachment(GL_BACK, surface);
637 rx::DefaultAttachmentImpl *depthAttachment = factory->createDefaultAttachment(GL_DEPTH, surface);
638 rx::DefaultAttachmentImpl *stencilAttachment = factory->createDefaultAttachment(GL_STENCIL, surface);
Jamie Madilld1405e52015-03-05 15:41:39 -0500639
Geoff Lang528ce3c2014-12-01 10:44:07 -0500640 ASSERT(colorAttachment);
641 setAttachment(GL_BACK, new DefaultAttachment(GL_BACK, colorAttachment));
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000642
Geoff Lang528ce3c2014-12-01 10:44:07 -0500643 if (depthAttachment)
Jamie Madille92a3542014-07-03 10:38:58 -0400644 {
Geoff Lang528ce3c2014-12-01 10:44:07 -0500645 setAttachment(GL_DEPTH, new DefaultAttachment(GL_DEPTH, depthAttachment));
Jamie Madille92a3542014-07-03 10:38:58 -0400646 }
Geoff Lang528ce3c2014-12-01 10:44:07 -0500647 if (stencilAttachment)
648 {
649 setAttachment(GL_STENCIL, new DefaultAttachment(GL_STENCIL, stencilAttachment));
650 }
651
Geoff Lang9dd95802014-12-01 11:12:59 -0500652 GLenum drawBufferState = GL_BACK;
653 setDrawBuffers(1, &drawBufferState);
654
655 setReadBuffer(GL_BACK);
Jamie Madille92a3542014-07-03 10:38:58 -0400656}
657
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000658}