blob: 010c9b9bdbde00413dd72f154b4b565d3cf80913 [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"
Jamie Madillc46f45d2015-03-31 13:20:55 -040011
Jamie Madillcc86d642015-11-24 13:00:07 -050012#include "common/Optional.h"
Jamie Madillc46f45d2015-03-31 13:20:55 -040013#include "common/utilities.h"
14#include "libANGLE/Config.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050015#include "libANGLE/Context.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/FramebufferAttachment.h"
Jamie Madillc46f45d2015-03-31 13:20:55 -040017#include "libANGLE/Renderbuffer.h"
18#include "libANGLE/Surface.h"
19#include "libANGLE/Texture.h"
20#include "libANGLE/formatutils.h"
Jamie Madill8415b5f2016-04-26 13:41:39 -040021#include "libANGLE/renderer/ContextImpl.h"
Geoff Langb5d8f232014-12-04 15:43:01 -050022#include "libANGLE/renderer/FramebufferImpl.h"
Jamie Madill48115b62015-03-16 10:46:57 -040023#include "libANGLE/renderer/ImplFactory.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050024#include "libANGLE/renderer/RenderbufferImpl.h"
Corentin Wallez37c39792015-08-20 14:19:46 -040025#include "libANGLE/renderer/SurfaceImpl.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040026
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027namespace gl
28{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000029
Jamie Madilld1405e52015-03-05 15:41:39 -050030namespace
31{
Jamie Madill2d06b732015-04-20 12:53:28 -040032void DetachMatchingAttachment(FramebufferAttachment *attachment, GLenum matchType, GLuint matchId)
Jamie Madilld1405e52015-03-05 15:41:39 -050033{
Jamie Madill2d06b732015-04-20 12:53:28 -040034 if (attachment->isAttached() &&
35 attachment->type() == matchType &&
36 attachment->id() == matchId)
Jamie Madilld1405e52015-03-05 15:41:39 -050037 {
Jamie Madill2d06b732015-04-20 12:53:28 -040038 attachment->detach();
Jamie Madilld1405e52015-03-05 15:41:39 -050039 }
40}
41}
42
Jamie Madill48ef11b2016-04-27 15:21:52 -040043FramebufferState::FramebufferState()
Geoff Lang70d0f492015-12-10 17:45:46 -050044 : mLabel(),
45 mColorAttachments(1),
Corentin Wallez37c39792015-08-20 14:19:46 -040046 mDrawBufferStates(1, GL_NONE),
47 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT)
48{
49 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
50}
51
Jamie Madill48ef11b2016-04-27 15:21:52 -040052FramebufferState::FramebufferState(const Caps &caps)
Geoff Lang70d0f492015-12-10 17:45:46 -050053 : mLabel(),
54 mColorAttachments(caps.maxColorAttachments),
Jamie Madilld1405e52015-03-05 15:41:39 -050055 mDrawBufferStates(caps.maxDrawBuffers, GL_NONE),
56 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT)
57{
Geoff Langa15472a2015-08-11 11:48:03 -040058 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilld1405e52015-03-05 15:41:39 -050059 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
60}
61
Jamie Madill48ef11b2016-04-27 15:21:52 -040062FramebufferState::~FramebufferState()
Jamie Madilld1405e52015-03-05 15:41:39 -050063{
Jamie Madilld1405e52015-03-05 15:41:39 -050064}
65
Jamie Madill48ef11b2016-04-27 15:21:52 -040066const std::string &FramebufferState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -050067{
68 return mLabel;
69}
70
Jamie Madill48ef11b2016-04-27 15:21:52 -040071const FramebufferAttachment *FramebufferState::getReadAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -050072{
73 ASSERT(mReadBufferState == GL_BACK || (mReadBufferState >= GL_COLOR_ATTACHMENT0 && mReadBufferState <= GL_COLOR_ATTACHMENT15));
74 size_t readIndex = (mReadBufferState == GL_BACK ? 0 : static_cast<size_t>(mReadBufferState - GL_COLOR_ATTACHMENT0));
75 ASSERT(readIndex < mColorAttachments.size());
Jamie Madill2d06b732015-04-20 12:53:28 -040076 return mColorAttachments[readIndex].isAttached() ? &mColorAttachments[readIndex] : nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -050077}
78
Jamie Madill48ef11b2016-04-27 15:21:52 -040079const FramebufferAttachment *FramebufferState::getFirstColorAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -050080{
Jamie Madill2d06b732015-04-20 12:53:28 -040081 for (const FramebufferAttachment &colorAttachment : mColorAttachments)
Jamie Madill7147f012015-03-05 15:41:40 -050082 {
Jamie Madill2d06b732015-04-20 12:53:28 -040083 if (colorAttachment.isAttached())
Jamie Madill7147f012015-03-05 15:41:40 -050084 {
Jamie Madill2d06b732015-04-20 12:53:28 -040085 return &colorAttachment;
Jamie Madill7147f012015-03-05 15:41:40 -050086 }
87 }
88
89 return nullptr;
90}
91
Jamie Madill48ef11b2016-04-27 15:21:52 -040092const FramebufferAttachment *FramebufferState::getDepthOrStencilAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -050093{
Jamie Madill2d06b732015-04-20 12:53:28 -040094 if (mDepthAttachment.isAttached())
95 {
96 return &mDepthAttachment;
97 }
98 if (mStencilAttachment.isAttached())
99 {
100 return &mStencilAttachment;
101 }
102 return nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500103}
104
Jamie Madill48ef11b2016-04-27 15:21:52 -0400105const FramebufferAttachment *FramebufferState::getColorAttachment(size_t colorAttachment) const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400106{
107 ASSERT(colorAttachment < mColorAttachments.size());
Jamie Madill2d06b732015-04-20 12:53:28 -0400108 return mColorAttachments[colorAttachment].isAttached() ?
109 &mColorAttachments[colorAttachment] :
110 nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400111}
112
Jamie Madill48ef11b2016-04-27 15:21:52 -0400113const FramebufferAttachment *FramebufferState::getDepthAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400114{
Jamie Madill2d06b732015-04-20 12:53:28 -0400115 return mDepthAttachment.isAttached() ? &mDepthAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400116}
117
Jamie Madill48ef11b2016-04-27 15:21:52 -0400118const FramebufferAttachment *FramebufferState::getStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400119{
Jamie Madill2d06b732015-04-20 12:53:28 -0400120 return mStencilAttachment.isAttached() ? &mStencilAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400121}
122
Jamie Madill48ef11b2016-04-27 15:21:52 -0400123const FramebufferAttachment *FramebufferState::getDepthStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400124{
125 // A valid depth-stencil attachment has the same resource bound to both the
126 // depth and stencil attachment points.
Jamie Madill2d06b732015-04-20 12:53:28 -0400127 if (mDepthAttachment.isAttached() && mStencilAttachment.isAttached() &&
128 mDepthAttachment.type() == mStencilAttachment.type() &&
129 mDepthAttachment.id() == mStencilAttachment.id())
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400130 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400131 return &mDepthAttachment;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400132 }
133
134 return nullptr;
135}
136
Jamie Madill48ef11b2016-04-27 15:21:52 -0400137bool FramebufferState::attachmentsHaveSameDimensions() const
Jamie Madillcc86d642015-11-24 13:00:07 -0500138{
139 Optional<Extents> attachmentSize;
140
141 auto hasMismatchedSize = [&attachmentSize](const FramebufferAttachment &attachment)
142 {
143 if (!attachment.isAttached())
144 {
145 return false;
146 }
147
148 if (!attachmentSize.valid())
149 {
150 attachmentSize = attachment.getSize();
151 return false;
152 }
153
154 return (attachment.getSize() != attachmentSize.value());
155 };
156
157 for (const auto &attachment : mColorAttachments)
158 {
159 if (hasMismatchedSize(attachment))
160 {
161 return false;
162 }
163 }
164
165 if (hasMismatchedSize(mDepthAttachment))
166 {
167 return false;
168 }
169
170 return !hasMismatchedSize(mStencilAttachment);
171}
172
Jamie Madill48115b62015-03-16 10:46:57 -0400173Framebuffer::Framebuffer(const Caps &caps, rx::ImplFactory *factory, GLuint id)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400174 : mState(caps), mImpl(factory->createFramebuffer(mState)), mId(id)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175{
Corentin Wallez37c39792015-08-20 14:19:46 -0400176 ASSERT(mId != 0);
177 ASSERT(mImpl != nullptr);
178}
179
180Framebuffer::Framebuffer(rx::SurfaceImpl *surface)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400181 : mState(), mImpl(surface->createDefaultFramebuffer(mState)), mId(0)
Corentin Wallez37c39792015-08-20 14:19:46 -0400182{
Geoff Langda88add2014-12-01 10:22:01 -0500183 ASSERT(mImpl != nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184}
185
186Framebuffer::~Framebuffer()
187{
Geoff Langda88add2014-12-01 10:22:01 -0500188 SafeDelete(mImpl);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000189}
190
Geoff Lang70d0f492015-12-10 17:45:46 -0500191void Framebuffer::setLabel(const std::string &label)
192{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400193 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500194}
195
196const std::string &Framebuffer::getLabel() const
197{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400198 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500199}
200
Jamie Madille261b442014-06-25 12:42:21 -0400201void Framebuffer::detachTexture(GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000202{
Jamie Madilld1405e52015-03-05 15:41:39 -0500203 detachResourceById(GL_TEXTURE, textureId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204}
205
Jamie Madille261b442014-06-25 12:42:21 -0400206void Framebuffer::detachRenderbuffer(GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207{
Jamie Madilld1405e52015-03-05 15:41:39 -0500208 detachResourceById(GL_RENDERBUFFER, renderbufferId);
209}
Jamie Madille261b442014-06-25 12:42:21 -0400210
Jamie Madilld1405e52015-03-05 15:41:39 -0500211void Framebuffer::detachResourceById(GLenum resourceType, GLuint resourceId)
212{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400213 for (auto &colorAttachment : mState.mColorAttachments)
Jamie Madilld1405e52015-03-05 15:41:39 -0500214 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400215 DetachMatchingAttachment(&colorAttachment, resourceType, resourceId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216 }
217
Jamie Madill48ef11b2016-04-27 15:21:52 -0400218 DetachMatchingAttachment(&mState.mDepthAttachment, resourceType, resourceId);
219 DetachMatchingAttachment(&mState.mStencilAttachment, resourceType, resourceId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220}
221
Corentin Wallez37c39792015-08-20 14:19:46 -0400222const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400224 return mState.getColorAttachment(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225}
226
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400227const FramebufferAttachment *Framebuffer::getDepthbuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400228{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400229 return mState.getDepthAttachment();
Geoff Lang646559f2013-08-15 11:08:15 -0400230}
231
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400232const FramebufferAttachment *Framebuffer::getStencilbuffer() const
233{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400234 return mState.getStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400235}
236
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400237const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
238{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400239 return mState.getDepthStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400240}
241
242const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000243{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400244 return mState.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000245}
246
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400247const FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000248{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400249 return mState.getReadAttachment();
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{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400254 const FramebufferAttachment *readAttachment = mState.getReadAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400255 return (readAttachment != nullptr ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000256}
257
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400258const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000259{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400260 return mState.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000261}
262
Jamie Madill2d06b732015-04-20 12:53:28 -0400263const FramebufferAttachment *Framebuffer::getAttachment(GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000264{
Jamie Madille92a3542014-07-03 10:38:58 -0400265 if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15)
266 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400267 return mState.getColorAttachment(attachment - GL_COLOR_ATTACHMENT0);
Jamie Madille92a3542014-07-03 10:38:58 -0400268 }
269 else
270 {
271 switch (attachment)
272 {
Geoff Lang528ce3c2014-12-01 10:44:07 -0500273 case GL_COLOR:
274 case GL_BACK:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400275 return mState.getColorAttachment(0);
Geoff Lang528ce3c2014-12-01 10:44:07 -0500276 case GL_DEPTH:
Jamie Madille92a3542014-07-03 10:38:58 -0400277 case GL_DEPTH_ATTACHMENT:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400278 return mState.getDepthAttachment();
Geoff Lang528ce3c2014-12-01 10:44:07 -0500279 case GL_STENCIL:
Jamie Madille92a3542014-07-03 10:38:58 -0400280 case GL_STENCIL_ATTACHMENT:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400281 return mState.getStencilAttachment();
Geoff Lang528ce3c2014-12-01 10:44:07 -0500282 case GL_DEPTH_STENCIL:
Jamie Madille92a3542014-07-03 10:38:58 -0400283 case GL_DEPTH_STENCIL_ATTACHMENT:
284 return getDepthStencilBuffer();
285 default:
286 UNREACHABLE();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400287 return nullptr;
Jamie Madille92a3542014-07-03 10:38:58 -0400288 }
289 }
Geoff Lang55ba29c2013-07-11 16:57:53 -0400290}
291
Geoff Langa15472a2015-08-11 11:48:03 -0400292size_t Framebuffer::getDrawbufferStateCount() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000293{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400294 return mState.mDrawBufferStates.size();
Geoff Langa15472a2015-08-11 11:48:03 -0400295}
296
297GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const
298{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400299 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
300 return mState.mDrawBufferStates[drawBuffer];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000301}
302
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500303const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const
304{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400305 return mState.getDrawBufferStates();
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500306}
307
Geoff Lang164d54e2014-12-01 10:55:33 -0500308void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000309{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400310 auto &drawStates = mState.mDrawBufferStates;
Jamie Madilld1405e52015-03-05 15:41:39 -0500311
312 ASSERT(count <= drawStates.size());
313 std::copy(buffers, buffers + count, drawStates.begin());
314 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500315 mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS);
Geoff Lang9dd95802014-12-01 11:12:59 -0500316}
317
Geoff Langa15472a2015-08-11 11:48:03 -0400318const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const
319{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400320 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
321 if (mState.mDrawBufferStates[drawBuffer] != GL_NONE)
Geoff Langa15472a2015-08-11 11:48:03 -0400322 {
323 // ES3 spec: "If the GL is bound to a draw framebuffer object, the ith buffer listed in bufs
324 // must be COLOR_ATTACHMENTi or NONE"
Jamie Madill48ef11b2016-04-27 15:21:52 -0400325 ASSERT(mState.mDrawBufferStates[drawBuffer] == GL_COLOR_ATTACHMENT0 + drawBuffer ||
326 (drawBuffer == 0 && mState.mDrawBufferStates[drawBuffer] == GL_BACK));
327 return getAttachment(mState.mDrawBufferStates[drawBuffer]);
Geoff Langa15472a2015-08-11 11:48:03 -0400328 }
329 else
330 {
331 return nullptr;
332 }
333}
334
335bool Framebuffer::hasEnabledDrawBuffer() const
336{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400337 for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
Geoff Langa15472a2015-08-11 11:48:03 -0400338 {
339 if (getDrawBuffer(drawbufferIdx) != nullptr)
340 {
341 return true;
342 }
343 }
344
345 return false;
346}
347
Geoff Lang9dd95802014-12-01 11:12:59 -0500348GLenum Framebuffer::getReadBufferState() const
349{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400350 return mState.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500351}
352
353void Framebuffer::setReadBuffer(GLenum buffer)
354{
Jamie Madillb885e572015-02-03 16:16:04 -0500355 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
356 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madill48ef11b2016-04-27 15:21:52 -0400357 (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size()));
358 mState.mReadBufferState = buffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500359 mDirtyBits.set(DIRTY_BIT_READ_BUFFER);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000360}
361
Corentin Wallez37c39792015-08-20 14:19:46 -0400362size_t Framebuffer::getNumColorBuffers() const
363{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400364 return mState.mColorAttachments.size();
Corentin Wallez37c39792015-08-20 14:19:46 -0400365}
366
Jamie Madill0df8fe42015-11-24 16:10:24 -0500367bool Framebuffer::hasDepth() const
368{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400369 return (mState.mDepthAttachment.isAttached() && mState.mDepthAttachment.getDepthSize() > 0);
Jamie Madill0df8fe42015-11-24 16:10:24 -0500370}
371
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000372bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000373{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400374 return (mState.mStencilAttachment.isAttached() &&
375 mState.mStencilAttachment.getStencilSize() > 0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000376}
377
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000378bool Framebuffer::usingExtendedDrawBuffers() const
379{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400380 for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000381 {
Geoff Langa15472a2015-08-11 11:48:03 -0400382 if (getDrawBuffer(drawbufferIdx) != nullptr)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000383 {
384 return true;
385 }
386 }
387
388 return false;
389}
390
Jamie Madill9082b982016-04-27 15:21:51 -0400391GLenum Framebuffer::checkStatus(const ContextState &data) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392{
Geoff Lang528ce3c2014-12-01 10:44:07 -0500393 // The default framebuffer *must* always be complete, though it may not be
394 // subject to the same rules as application FBOs. ie, it could have 0x0 size.
395 if (mId == 0)
396 {
397 return GL_FRAMEBUFFER_COMPLETE;
398 }
399
shannonwoods@chromium.orgf6fb9592013-05-30 00:09:40 +0000400 unsigned int colorbufferSize = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000401 int samples = -1;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000402 bool missingAttachment = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403
Jamie Madill48ef11b2016-04-27 15:21:52 -0400404 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400406 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407 {
Jamie Madill6b120b92015-11-24 13:00:07 -0500408 const Extents &size = colorAttachment.getSize();
409 if (size.width == 0 || size.height == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000410 {
411 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
412 }
daniel@transgaming.com01868132010-08-24 19:21:17 +0000413
Jamie Madill2d06b732015-04-20 12:53:28 -0400414 GLenum internalformat = colorAttachment.getInternalFormat();
Jamie Madill48faf802014-11-06 15:27:22 -0500415 const TextureCaps &formatCaps = data.textureCaps->get(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400416 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
Jamie Madill2d06b732015-04-20 12:53:28 -0400417 if (colorAttachment.type() == GL_TEXTURE)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000418 {
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400419 if (!formatCaps.renderable)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000420 {
Jamie Madill81176782015-11-24 16:10:23 -0500421 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000422 }
423
Geoff Lang5d601382014-07-22 15:14:06 -0400424 if (formatInfo.depthBits > 0 || formatInfo.stencilBits > 0)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000425 {
426 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
427 }
Jamie Madill6b120b92015-11-24 13:00:07 -0500428
429 if (colorAttachment.layer() >= size.depth)
430 {
431 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
432 }
Jamie Madill3215b202015-12-15 16:41:39 -0500433
434 // ES3 specifies that cube map texture attachments must be cube complete.
435 // This language is missing from the ES2 spec, but we enforce it here because some
436 // desktop OpenGL drivers also enforce this validation.
437 // TODO(jmadill): Check if OpenGL ES2 drivers enforce cube completeness.
438 const Texture *texture = colorAttachment.getTexture();
439 ASSERT(texture);
440 if (texture->getTarget() == GL_TEXTURE_CUBE_MAP && !texture->isCubeComplete())
441 {
442 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
443 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000444 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400445 else if (colorAttachment.type() == GL_RENDERBUFFER)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000446 {
Geoff Lang5d601382014-07-22 15:14:06 -0400447 if (!formatCaps.renderable || formatInfo.depthBits > 0 || formatInfo.stencilBits > 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400448 {
449 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
450 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000451 }
452
453 if (!missingAttachment)
454 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000455 // 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.
Jamie Madill2d06b732015-04-20 12:53:28 -0400457 if (colorAttachment.getSamples() != samples)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000458 {
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
Jamie Madill48faf802014-11-06 15:27:22 -0500464 if (data.clientVersion < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000465 {
Geoff Lang5d601382014-07-22 15:14:06 -0400466 if (formatInfo.pixelBytes != 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 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000471 }
472 else
473 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400474 samples = colorAttachment.getSamples();
Geoff Lang5d601382014-07-22 15:14:06 -0400475 colorbufferSize = formatInfo.pixelBytes;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000476 missingAttachment = false;
477 }
478 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000479 }
480
Jamie Madill48ef11b2016-04-27 15:21:52 -0400481 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -0400482 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000483 {
Jamie Madill6b120b92015-11-24 13:00:07 -0500484 const Extents &size = depthAttachment.getSize();
485 if (size.width == 0 || size.height == 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000487 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488 }
489
Jamie Madill2d06b732015-04-20 12:53:28 -0400490 GLenum internalformat = depthAttachment.getInternalFormat();
Jamie Madill48faf802014-11-06 15:27:22 -0500491 const TextureCaps &formatCaps = data.textureCaps->get(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400492 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
Jamie Madill2d06b732015-04-20 12:53:28 -0400493 if (depthAttachment.type() == GL_TEXTURE)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000494 {
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000495 // depth texture attachments require OES/ANGLE_depth_texture
Jamie Madill48faf802014-11-06 15:27:22 -0500496 if (!data.extensions->depthTextures)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000497 {
498 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
499 }
500
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400501 if (!formatCaps.renderable)
Geoff Langcec35902014-04-16 10:52:36 -0400502 {
Jamie Madill81176782015-11-24 16:10:23 -0500503 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
Geoff Langcec35902014-04-16 10:52:36 -0400504 }
505
Geoff Lang5d601382014-07-22 15:14:06 -0400506 if (formatInfo.depthBits == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000507 {
508 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
509 }
510 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400511 else if (depthAttachment.type() == GL_RENDERBUFFER)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000512 {
Geoff Lang5d601382014-07-22 15:14:06 -0400513 if (!formatCaps.renderable || formatInfo.depthBits == 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400514 {
515 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
516 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000517 }
518
519 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000520 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400521 samples = depthAttachment.getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000522 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000523 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400524 else if (samples != depthAttachment.getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000525 {
526 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
527 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000528 }
529
Jamie Madill48ef11b2016-04-27 15:21:52 -0400530 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -0400531 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000532 {
Jamie Madill6b120b92015-11-24 13:00:07 -0500533 const Extents &size = stencilAttachment.getSize();
534 if (size.width == 0 || size.height == 0)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000535 {
536 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
537 }
538
Jamie Madill2d06b732015-04-20 12:53:28 -0400539 GLenum internalformat = stencilAttachment.getInternalFormat();
Jamie Madill48faf802014-11-06 15:27:22 -0500540 const TextureCaps &formatCaps = data.textureCaps->get(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400541 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
Jamie Madill2d06b732015-04-20 12:53:28 -0400542 if (stencilAttachment.type() == GL_TEXTURE)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000543 {
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000544 // texture stencil attachments come along as part
545 // of OES_packed_depth_stencil + OES/ANGLE_depth_texture
Jamie Madill48faf802014-11-06 15:27:22 -0500546 if (!data.extensions->depthTextures)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000547 {
548 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
549 }
550
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400551 if (!formatCaps.renderable)
Geoff Langcec35902014-04-16 10:52:36 -0400552 {
Jamie Madill81176782015-11-24 16:10:23 -0500553 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
Geoff Langcec35902014-04-16 10:52:36 -0400554 }
555
Geoff Lang5d601382014-07-22 15:14:06 -0400556 if (formatInfo.stencilBits == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000557 {
558 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
559 }
560 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400561 else if (stencilAttachment.type() == GL_RENDERBUFFER)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000562 {
Geoff Lang5d601382014-07-22 15:14:06 -0400563 if (!formatCaps.renderable || formatInfo.stencilBits == 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400564 {
565 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
566 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000567 }
568
569 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000570 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400571 samples = stencilAttachment.getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000572 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000573 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400574 else if (samples != stencilAttachment.getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000575 {
576 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
577 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000578 }
579
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000580 // we need to have at least one attachment to be complete
581 if (missingAttachment)
582 {
583 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000584 }
585
Jamie Madillcc86d642015-11-24 13:00:07 -0500586 // In ES 2.0, all color attachments must have the same width and height.
587 // In ES 3.0, there is no such restriction.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400588 if (data.clientVersion < 3 && !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -0500589 {
590 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
591 }
592
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500593 syncState();
Jamie Madillcc86d642015-11-24 13:00:07 -0500594 if (!mImpl->checkStatus())
595 {
596 return GL_FRAMEBUFFER_UNSUPPORTED;
597 }
598
599 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000600}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000601
Austin Kinross08332632015-05-05 13:35:47 -0700602Error Framebuffer::discard(size_t count, const GLenum *attachments)
603{
604 return mImpl->discard(count, attachments);
605}
606
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500607Error Framebuffer::invalidate(size_t count, const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400608{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500609 return mImpl->invalidate(count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400610}
611
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500612Error Framebuffer::invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -0400613{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500614 return mImpl->invalidateSub(count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -0400615}
616
Jamie Madill8415b5f2016-04-26 13:41:39 -0400617Error Framebuffer::clear(rx::ContextImpl *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -0500618{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400619 if (context->getState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500620 {
621 return gl::Error(GL_NO_ERROR);
622 }
623
Jamie Madill8415b5f2016-04-26 13:41:39 -0400624 return mImpl->clear(context, mask);
Geoff Langb04dc822014-12-01 12:02:02 -0500625}
626
Jamie Madill8415b5f2016-04-26 13:41:39 -0400627Error Framebuffer::clearBufferfv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400628 GLenum buffer,
629 GLint drawbuffer,
630 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500631{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400632 if (context->getState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500633 {
634 return gl::Error(GL_NO_ERROR);
635 }
636
Jamie Madill8415b5f2016-04-26 13:41:39 -0400637 return mImpl->clearBufferfv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500638}
639
Jamie Madill8415b5f2016-04-26 13:41:39 -0400640Error Framebuffer::clearBufferuiv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400641 GLenum buffer,
642 GLint drawbuffer,
643 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500644{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400645 if (context->getState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500646 {
647 return gl::Error(GL_NO_ERROR);
648 }
649
Jamie Madill8415b5f2016-04-26 13:41:39 -0400650 return mImpl->clearBufferuiv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500651}
652
Jamie Madill8415b5f2016-04-26 13:41:39 -0400653Error Framebuffer::clearBufferiv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400654 GLenum buffer,
655 GLint drawbuffer,
656 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500657{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400658 if (context->getState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500659 {
660 return gl::Error(GL_NO_ERROR);
661 }
662
Jamie Madill8415b5f2016-04-26 13:41:39 -0400663 return mImpl->clearBufferiv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500664}
665
Jamie Madill8415b5f2016-04-26 13:41:39 -0400666Error Framebuffer::clearBufferfi(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400667 GLenum buffer,
668 GLint drawbuffer,
669 GLfloat depth,
670 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -0500671{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400672 if (context->getState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500673 {
674 return gl::Error(GL_NO_ERROR);
675 }
676
Jamie Madill8415b5f2016-04-26 13:41:39 -0400677 return mImpl->clearBufferfi(context, buffer, drawbuffer, depth, stencil);
Geoff Langb04dc822014-12-01 12:02:02 -0500678}
679
Geoff Langbce529e2014-12-01 12:48:41 -0500680GLenum Framebuffer::getImplementationColorReadFormat() const
681{
682 return mImpl->getImplementationColorReadFormat();
683}
684
685GLenum Framebuffer::getImplementationColorReadType() const
686{
687 return mImpl->getImplementationColorReadType();
688}
689
Jamie Madill8415b5f2016-04-26 13:41:39 -0400690Error Framebuffer::readPixels(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500691 const Rectangle &area,
Jamie Madill1b94d432015-08-07 13:23:23 -0400692 GLenum format,
693 GLenum type,
694 GLvoid *pixels) const
Geoff Langbce529e2014-12-01 12:48:41 -0500695{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400696 Error error = mImpl->readPixels(context, area, format, type, pixels);
Geoff Lang520c4ae2015-05-05 13:12:36 -0400697 if (error.isError())
698 {
699 return error;
700 }
701
Jamie Madill8415b5f2016-04-26 13:41:39 -0400702 Buffer *unpackBuffer = context->getState().getUnpackState().pixelBuffer.get();
Geoff Lang520c4ae2015-05-05 13:12:36 -0400703 if (unpackBuffer)
704 {
705 unpackBuffer->onPixelUnpack();
706 }
707
708 return Error(GL_NO_ERROR);
Geoff Langbce529e2014-12-01 12:48:41 -0500709}
710
Jamie Madill8415b5f2016-04-26 13:41:39 -0400711Error Framebuffer::blit(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500712 const Rectangle &sourceArea,
713 const Rectangle &destArea,
Geoff Lang242468f2015-09-24 14:15:41 -0400714 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -0400715 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -0500716{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400717 return mImpl->blit(context, sourceArea, destArea, mask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -0500718}
719
Jamie Madill9082b982016-04-27 15:21:51 -0400720int Framebuffer::getSamples(const ContextState &data) const
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000721{
Geoff Lang748f74e2014-12-01 11:25:34 -0500722 if (checkStatus(data) == GL_FRAMEBUFFER_COMPLETE)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000723 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000724 // for a complete framebuffer, all attachments must have the same sample count
725 // in this case return the first nonzero sample size
Jamie Madill48ef11b2016-04-27 15:21:52 -0400726 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000727 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400728 if (colorAttachment.isAttached())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000729 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400730 return colorAttachment.getSamples();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000731 }
732 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000733 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000734
735 return 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000736}
737
Jamie Madille261b442014-06-25 12:42:21 -0400738bool Framebuffer::hasValidDepthStencil() const
739{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400740 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -0400741}
742
Jamie Madill2d06b732015-04-20 12:53:28 -0400743void Framebuffer::setAttachment(GLenum type,
744 GLenum binding,
745 const ImageIndex &textureIndex,
746 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -0400747{
Jamie Madill2d06b732015-04-20 12:53:28 -0400748 if (binding == GL_DEPTH_STENCIL || binding == GL_DEPTH_STENCIL_ATTACHMENT)
Geoff Langab75a052014-10-15 12:56:37 -0400749 {
Geoff Langab75a052014-10-15 12:56:37 -0400750 // ensure this is a legitimate depth+stencil format
Jamie Madill375c37c2015-07-21 15:14:08 -0400751 FramebufferAttachmentObject *attachmentObj = resource;
752 if (resource)
Geoff Langab75a052014-10-15 12:56:37 -0400753 {
Jamie Madill375c37c2015-07-21 15:14:08 -0400754 FramebufferAttachment::Target target(binding, textureIndex);
755 GLenum internalFormat = resource->getAttachmentInternalFormat(target);
756 const InternalFormat &formatInfo = GetInternalFormatInfo(internalFormat);
757 if (formatInfo.depthBits == 0 || formatInfo.stencilBits == 0)
758 {
759 // Attaching nullptr detaches the current attachment.
760 attachmentObj = nullptr;
761 }
Geoff Langab75a052014-10-15 12:56:37 -0400762 }
Jamie Madill375c37c2015-07-21 15:14:08 -0400763
Jamie Madill48ef11b2016-04-27 15:21:52 -0400764 mState.mDepthAttachment.attach(type, binding, textureIndex, attachmentObj);
765 mState.mStencilAttachment.attach(type, binding, textureIndex, attachmentObj);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500766 mDirtyBits.set(DIRTY_BIT_DEPTH_ATTACHMENT);
767 mDirtyBits.set(DIRTY_BIT_STENCIL_ATTACHMENT);
Geoff Langab75a052014-10-15 12:56:37 -0400768 }
769 else
770 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400771 switch (binding)
772 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500773 case GL_DEPTH:
774 case GL_DEPTH_ATTACHMENT:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400775 mState.mDepthAttachment.attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500776 mDirtyBits.set(DIRTY_BIT_DEPTH_ATTACHMENT);
Jamie Madill2d06b732015-04-20 12:53:28 -0400777 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500778 case GL_STENCIL:
779 case GL_STENCIL_ATTACHMENT:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400780 mState.mStencilAttachment.attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500781 mDirtyBits.set(DIRTY_BIT_STENCIL_ATTACHMENT);
Jamie Madill2d06b732015-04-20 12:53:28 -0400782 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500783 case GL_BACK:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400784 mState.mColorAttachments[0].attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500785 mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0);
Jamie Madill2d06b732015-04-20 12:53:28 -0400786 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500787 default:
Jamie Madill2d06b732015-04-20 12:53:28 -0400788 {
789 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
Jamie Madill48ef11b2016-04-27 15:21:52 -0400790 ASSERT(colorIndex < mState.mColorAttachments.size());
791 mState.mColorAttachments[colorIndex].attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500792 mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
Jamie Madill2d06b732015-04-20 12:53:28 -0400793 }
794 break;
795 }
Geoff Langab75a052014-10-15 12:56:37 -0400796 }
797}
798
Jamie Madill2d06b732015-04-20 12:53:28 -0400799void Framebuffer::resetAttachment(GLenum binding)
800{
801 setAttachment(GL_NONE, binding, ImageIndex::MakeInvalid(), nullptr);
802}
803
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500804void Framebuffer::syncState() const
805{
806 if (mDirtyBits.any())
807 {
808 mImpl->syncState(mDirtyBits);
809 mDirtyBits.reset();
810 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811}
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500812
813} // namespace gl