blob: f2adc4f552a76c2b9d8e8627e20b1e3f85ca27e5 [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 }
Corentin Wallez086d59a2016-04-29 09:06:49 -0400578
579 // Starting from ES 3.0 stencil and depth, if present, should be the same image
580 if (data.clientVersion >= 3 && depthAttachment.isAttached() &&
581 stencilAttachment != depthAttachment)
582 {
583 return GL_FRAMEBUFFER_UNSUPPORTED;
584 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000585 }
586
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000587 // we need to have at least one attachment to be complete
588 if (missingAttachment)
589 {
590 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000591 }
592
Jamie Madillcc86d642015-11-24 13:00:07 -0500593 // In ES 2.0, all color attachments must have the same width and height.
594 // In ES 3.0, there is no such restriction.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400595 if (data.clientVersion < 3 && !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -0500596 {
597 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
598 }
599
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500600 syncState();
Jamie Madillcc86d642015-11-24 13:00:07 -0500601 if (!mImpl->checkStatus())
602 {
603 return GL_FRAMEBUFFER_UNSUPPORTED;
604 }
605
606 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000608
Austin Kinross08332632015-05-05 13:35:47 -0700609Error Framebuffer::discard(size_t count, const GLenum *attachments)
610{
611 return mImpl->discard(count, attachments);
612}
613
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500614Error Framebuffer::invalidate(size_t count, const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400615{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500616 return mImpl->invalidate(count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400617}
618
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500619Error Framebuffer::invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -0400620{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500621 return mImpl->invalidateSub(count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -0400622}
623
Jamie Madill8415b5f2016-04-26 13:41:39 -0400624Error Framebuffer::clear(rx::ContextImpl *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -0500625{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400626 if (context->getState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500627 {
628 return gl::Error(GL_NO_ERROR);
629 }
630
Jamie Madill8415b5f2016-04-26 13:41:39 -0400631 return mImpl->clear(context, mask);
Geoff Langb04dc822014-12-01 12:02:02 -0500632}
633
Jamie Madill8415b5f2016-04-26 13:41:39 -0400634Error Framebuffer::clearBufferfv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400635 GLenum buffer,
636 GLint drawbuffer,
637 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500638{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400639 if (context->getState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500640 {
641 return gl::Error(GL_NO_ERROR);
642 }
643
Jamie Madill8415b5f2016-04-26 13:41:39 -0400644 return mImpl->clearBufferfv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500645}
646
Jamie Madill8415b5f2016-04-26 13:41:39 -0400647Error Framebuffer::clearBufferuiv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400648 GLenum buffer,
649 GLint drawbuffer,
650 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500651{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400652 if (context->getState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500653 {
654 return gl::Error(GL_NO_ERROR);
655 }
656
Jamie Madill8415b5f2016-04-26 13:41:39 -0400657 return mImpl->clearBufferuiv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500658}
659
Jamie Madill8415b5f2016-04-26 13:41:39 -0400660Error Framebuffer::clearBufferiv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400661 GLenum buffer,
662 GLint drawbuffer,
663 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500664{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400665 if (context->getState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500666 {
667 return gl::Error(GL_NO_ERROR);
668 }
669
Jamie Madill8415b5f2016-04-26 13:41:39 -0400670 return mImpl->clearBufferiv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500671}
672
Jamie Madill8415b5f2016-04-26 13:41:39 -0400673Error Framebuffer::clearBufferfi(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400674 GLenum buffer,
675 GLint drawbuffer,
676 GLfloat depth,
677 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -0500678{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400679 if (context->getState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500680 {
681 return gl::Error(GL_NO_ERROR);
682 }
683
Jamie Madill8415b5f2016-04-26 13:41:39 -0400684 return mImpl->clearBufferfi(context, buffer, drawbuffer, depth, stencil);
Geoff Langb04dc822014-12-01 12:02:02 -0500685}
686
Geoff Langbce529e2014-12-01 12:48:41 -0500687GLenum Framebuffer::getImplementationColorReadFormat() const
688{
689 return mImpl->getImplementationColorReadFormat();
690}
691
692GLenum Framebuffer::getImplementationColorReadType() const
693{
694 return mImpl->getImplementationColorReadType();
695}
696
Jamie Madill8415b5f2016-04-26 13:41:39 -0400697Error Framebuffer::readPixels(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500698 const Rectangle &area,
Jamie Madill1b94d432015-08-07 13:23:23 -0400699 GLenum format,
700 GLenum type,
701 GLvoid *pixels) const
Geoff Langbce529e2014-12-01 12:48:41 -0500702{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400703 Error error = mImpl->readPixels(context, area, format, type, pixels);
Geoff Lang520c4ae2015-05-05 13:12:36 -0400704 if (error.isError())
705 {
706 return error;
707 }
708
Jamie Madill8415b5f2016-04-26 13:41:39 -0400709 Buffer *unpackBuffer = context->getState().getUnpackState().pixelBuffer.get();
Geoff Lang520c4ae2015-05-05 13:12:36 -0400710 if (unpackBuffer)
711 {
712 unpackBuffer->onPixelUnpack();
713 }
714
715 return Error(GL_NO_ERROR);
Geoff Langbce529e2014-12-01 12:48:41 -0500716}
717
Jamie Madill8415b5f2016-04-26 13:41:39 -0400718Error Framebuffer::blit(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500719 const Rectangle &sourceArea,
720 const Rectangle &destArea,
Geoff Lang242468f2015-09-24 14:15:41 -0400721 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -0400722 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -0500723{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400724 return mImpl->blit(context, sourceArea, destArea, mask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -0500725}
726
Jamie Madill9082b982016-04-27 15:21:51 -0400727int Framebuffer::getSamples(const ContextState &data) const
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000728{
Geoff Lang748f74e2014-12-01 11:25:34 -0500729 if (checkStatus(data) == GL_FRAMEBUFFER_COMPLETE)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000730 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000731 // for a complete framebuffer, all attachments must have the same sample count
732 // in this case return the first nonzero sample size
Jamie Madill48ef11b2016-04-27 15:21:52 -0400733 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000734 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400735 if (colorAttachment.isAttached())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000736 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400737 return colorAttachment.getSamples();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000738 }
739 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000740 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000741
742 return 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000743}
744
Jamie Madille261b442014-06-25 12:42:21 -0400745bool Framebuffer::hasValidDepthStencil() const
746{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400747 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -0400748}
749
Jamie Madill2d06b732015-04-20 12:53:28 -0400750void Framebuffer::setAttachment(GLenum type,
751 GLenum binding,
752 const ImageIndex &textureIndex,
753 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -0400754{
Jamie Madill2d06b732015-04-20 12:53:28 -0400755 if (binding == GL_DEPTH_STENCIL || binding == GL_DEPTH_STENCIL_ATTACHMENT)
Geoff Langab75a052014-10-15 12:56:37 -0400756 {
Geoff Langab75a052014-10-15 12:56:37 -0400757 // ensure this is a legitimate depth+stencil format
Jamie Madill375c37c2015-07-21 15:14:08 -0400758 FramebufferAttachmentObject *attachmentObj = resource;
759 if (resource)
Geoff Langab75a052014-10-15 12:56:37 -0400760 {
Jamie Madill375c37c2015-07-21 15:14:08 -0400761 FramebufferAttachment::Target target(binding, textureIndex);
762 GLenum internalFormat = resource->getAttachmentInternalFormat(target);
763 const InternalFormat &formatInfo = GetInternalFormatInfo(internalFormat);
764 if (formatInfo.depthBits == 0 || formatInfo.stencilBits == 0)
765 {
766 // Attaching nullptr detaches the current attachment.
767 attachmentObj = nullptr;
768 }
Geoff Langab75a052014-10-15 12:56:37 -0400769 }
Jamie Madill375c37c2015-07-21 15:14:08 -0400770
Jamie Madill48ef11b2016-04-27 15:21:52 -0400771 mState.mDepthAttachment.attach(type, binding, textureIndex, attachmentObj);
772 mState.mStencilAttachment.attach(type, binding, textureIndex, attachmentObj);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500773 mDirtyBits.set(DIRTY_BIT_DEPTH_ATTACHMENT);
774 mDirtyBits.set(DIRTY_BIT_STENCIL_ATTACHMENT);
Geoff Langab75a052014-10-15 12:56:37 -0400775 }
776 else
777 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400778 switch (binding)
779 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500780 case GL_DEPTH:
781 case GL_DEPTH_ATTACHMENT:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400782 mState.mDepthAttachment.attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500783 mDirtyBits.set(DIRTY_BIT_DEPTH_ATTACHMENT);
Jamie Madill2d06b732015-04-20 12:53:28 -0400784 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500785 case GL_STENCIL:
786 case GL_STENCIL_ATTACHMENT:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400787 mState.mStencilAttachment.attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500788 mDirtyBits.set(DIRTY_BIT_STENCIL_ATTACHMENT);
Jamie Madill2d06b732015-04-20 12:53:28 -0400789 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500790 case GL_BACK:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400791 mState.mColorAttachments[0].attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500792 mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0);
Jamie Madill2d06b732015-04-20 12:53:28 -0400793 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500794 default:
Jamie Madill2d06b732015-04-20 12:53:28 -0400795 {
796 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
Jamie Madill48ef11b2016-04-27 15:21:52 -0400797 ASSERT(colorIndex < mState.mColorAttachments.size());
798 mState.mColorAttachments[colorIndex].attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500799 mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
Jamie Madill2d06b732015-04-20 12:53:28 -0400800 }
801 break;
802 }
Geoff Langab75a052014-10-15 12:56:37 -0400803 }
804}
805
Jamie Madill2d06b732015-04-20 12:53:28 -0400806void Framebuffer::resetAttachment(GLenum binding)
807{
808 setAttachment(GL_NONE, binding, ImageIndex::MakeInvalid(), nullptr);
809}
810
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500811void Framebuffer::syncState() const
812{
813 if (mDirtyBits.any())
814 {
815 mImpl->syncState(mDirtyBits);
816 mDirtyBits.reset();
817 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818}
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500819
820} // namespace gl