blob: 75627f02ffb6a8565483b59bd6cdd81732d3ed8b [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 Madill7aea7e02016-05-10 10:39:45 -040023#include "libANGLE/renderer/GLImplFactory.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
Jamie Madill362876b2016-06-16 14:46:59 -040027using namespace angle;
28
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029namespace gl
30{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000031
Jamie Madilld1405e52015-03-05 15:41:39 -050032namespace
33{
Jamie Madill362876b2016-06-16 14:46:59 -040034
35void BindResourceChannel(ChannelBinding *binding, FramebufferAttachmentObject *resource)
Jamie Madilld1405e52015-03-05 15:41:39 -050036{
Jamie Madill362876b2016-06-16 14:46:59 -040037 binding->bind(resource ? resource->getDirtyChannel() : nullptr);
Jamie Madilld1405e52015-03-05 15:41:39 -050038}
Jamie Madill362876b2016-06-16 14:46:59 -040039
40} // anonymous namespace
Jamie Madilld1405e52015-03-05 15:41:39 -050041
Jamie Madill48ef11b2016-04-27 15:21:52 -040042FramebufferState::FramebufferState()
Geoff Lang70d0f492015-12-10 17:45:46 -050043 : mLabel(),
44 mColorAttachments(1),
Corentin Wallez37c39792015-08-20 14:19:46 -040045 mDrawBufferStates(1, GL_NONE),
46 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT)
47{
48 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
49}
50
Jamie Madill48ef11b2016-04-27 15:21:52 -040051FramebufferState::FramebufferState(const Caps &caps)
Geoff Lang70d0f492015-12-10 17:45:46 -050052 : mLabel(),
53 mColorAttachments(caps.maxColorAttachments),
Jamie Madilld1405e52015-03-05 15:41:39 -050054 mDrawBufferStates(caps.maxDrawBuffers, GL_NONE),
55 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT)
56{
Geoff Langa15472a2015-08-11 11:48:03 -040057 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilld1405e52015-03-05 15:41:39 -050058 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
59}
60
Jamie Madill48ef11b2016-04-27 15:21:52 -040061FramebufferState::~FramebufferState()
Jamie Madilld1405e52015-03-05 15:41:39 -050062{
Jamie Madilld1405e52015-03-05 15:41:39 -050063}
64
Jamie Madill48ef11b2016-04-27 15:21:52 -040065const std::string &FramebufferState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -050066{
67 return mLabel;
68}
69
Geoff Lang4b7f12b2016-06-21 16:47:07 -040070const FramebufferAttachment *FramebufferState::getAttachment(GLenum attachment) const
71{
72 if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15)
73 {
74 return getColorAttachment(attachment - GL_COLOR_ATTACHMENT0);
75 }
76
77 switch (attachment)
78 {
79 case GL_COLOR:
80 case GL_BACK:
81 return getColorAttachment(0);
82 case GL_DEPTH:
83 case GL_DEPTH_ATTACHMENT:
84 return getDepthAttachment();
85 case GL_STENCIL:
86 case GL_STENCIL_ATTACHMENT:
87 return getStencilAttachment();
88 case GL_DEPTH_STENCIL:
89 case GL_DEPTH_STENCIL_ATTACHMENT:
90 return getDepthStencilAttachment();
91 default:
92 UNREACHABLE();
93 return nullptr;
94 }
95}
96
Jamie Madill48ef11b2016-04-27 15:21:52 -040097const FramebufferAttachment *FramebufferState::getReadAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -050098{
99 ASSERT(mReadBufferState == GL_BACK || (mReadBufferState >= GL_COLOR_ATTACHMENT0 && mReadBufferState <= GL_COLOR_ATTACHMENT15));
100 size_t readIndex = (mReadBufferState == GL_BACK ? 0 : static_cast<size_t>(mReadBufferState - GL_COLOR_ATTACHMENT0));
101 ASSERT(readIndex < mColorAttachments.size());
Jamie Madill2d06b732015-04-20 12:53:28 -0400102 return mColorAttachments[readIndex].isAttached() ? &mColorAttachments[readIndex] : nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500103}
104
Jamie Madill48ef11b2016-04-27 15:21:52 -0400105const FramebufferAttachment *FramebufferState::getFirstColorAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500106{
Jamie Madill2d06b732015-04-20 12:53:28 -0400107 for (const FramebufferAttachment &colorAttachment : mColorAttachments)
Jamie Madill7147f012015-03-05 15:41:40 -0500108 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400109 if (colorAttachment.isAttached())
Jamie Madill7147f012015-03-05 15:41:40 -0500110 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400111 return &colorAttachment;
Jamie Madill7147f012015-03-05 15:41:40 -0500112 }
113 }
114
115 return nullptr;
116}
117
Jamie Madill48ef11b2016-04-27 15:21:52 -0400118const FramebufferAttachment *FramebufferState::getDepthOrStencilAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500119{
Jamie Madill2d06b732015-04-20 12:53:28 -0400120 if (mDepthAttachment.isAttached())
121 {
122 return &mDepthAttachment;
123 }
124 if (mStencilAttachment.isAttached())
125 {
126 return &mStencilAttachment;
127 }
128 return nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500129}
130
Jamie Madill48ef11b2016-04-27 15:21:52 -0400131const FramebufferAttachment *FramebufferState::getColorAttachment(size_t colorAttachment) const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400132{
133 ASSERT(colorAttachment < mColorAttachments.size());
Jamie Madill2d06b732015-04-20 12:53:28 -0400134 return mColorAttachments[colorAttachment].isAttached() ?
135 &mColorAttachments[colorAttachment] :
136 nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400137}
138
Jamie Madill48ef11b2016-04-27 15:21:52 -0400139const FramebufferAttachment *FramebufferState::getDepthAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400140{
Jamie Madill2d06b732015-04-20 12:53:28 -0400141 return mDepthAttachment.isAttached() ? &mDepthAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400142}
143
Jamie Madill48ef11b2016-04-27 15:21:52 -0400144const FramebufferAttachment *FramebufferState::getStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400145{
Jamie Madill2d06b732015-04-20 12:53:28 -0400146 return mStencilAttachment.isAttached() ? &mStencilAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400147}
148
Jamie Madill48ef11b2016-04-27 15:21:52 -0400149const FramebufferAttachment *FramebufferState::getDepthStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400150{
151 // A valid depth-stencil attachment has the same resource bound to both the
152 // depth and stencil attachment points.
Jamie Madill2d06b732015-04-20 12:53:28 -0400153 if (mDepthAttachment.isAttached() && mStencilAttachment.isAttached() &&
154 mDepthAttachment.type() == mStencilAttachment.type() &&
155 mDepthAttachment.id() == mStencilAttachment.id())
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400156 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400157 return &mDepthAttachment;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400158 }
159
160 return nullptr;
161}
162
Jamie Madill48ef11b2016-04-27 15:21:52 -0400163bool FramebufferState::attachmentsHaveSameDimensions() const
Jamie Madillcc86d642015-11-24 13:00:07 -0500164{
165 Optional<Extents> attachmentSize;
166
167 auto hasMismatchedSize = [&attachmentSize](const FramebufferAttachment &attachment)
168 {
169 if (!attachment.isAttached())
170 {
171 return false;
172 }
173
174 if (!attachmentSize.valid())
175 {
176 attachmentSize = attachment.getSize();
177 return false;
178 }
179
180 return (attachment.getSize() != attachmentSize.value());
181 };
182
183 for (const auto &attachment : mColorAttachments)
184 {
185 if (hasMismatchedSize(attachment))
186 {
187 return false;
188 }
189 }
190
191 if (hasMismatchedSize(mDepthAttachment))
192 {
193 return false;
194 }
195
196 return !hasMismatchedSize(mStencilAttachment);
197}
198
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400199const gl::FramebufferAttachment *FramebufferState::getDrawBuffer(size_t drawBufferIdx) const
200{
201 ASSERT(drawBufferIdx < mDrawBufferStates.size());
202 if (mDrawBufferStates[drawBufferIdx] != GL_NONE)
203 {
204 // ES3 spec: "If the GL is bound to a draw framebuffer object, the ith buffer listed in bufs
205 // must be COLOR_ATTACHMENTi or NONE"
206 ASSERT(mDrawBufferStates[drawBufferIdx] == GL_COLOR_ATTACHMENT0 + drawBufferIdx ||
207 (drawBufferIdx == 0 && mDrawBufferStates[drawBufferIdx] == GL_BACK));
208 return getAttachment(mDrawBufferStates[drawBufferIdx]);
209 }
210 else
211 {
212 return nullptr;
213 }
214}
215
216size_t FramebufferState::getDrawBufferCount() const
217{
218 return mDrawBufferStates.size();
219}
220
Jamie Madill7aea7e02016-05-10 10:39:45 -0400221Framebuffer::Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id)
Jamie Madill362876b2016-06-16 14:46:59 -0400222 : mState(caps),
223 mImpl(factory->createFramebuffer(mState)),
224 mId(id),
225 mCachedStatus(),
226 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
227 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228{
Corentin Wallez37c39792015-08-20 14:19:46 -0400229 ASSERT(mId != 0);
230 ASSERT(mImpl != nullptr);
Jamie Madill362876b2016-06-16 14:46:59 -0400231 ASSERT(mState.mColorAttachments.size() == static_cast<size_t>(caps.maxColorAttachments));
232
233 for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
234 {
235 mDirtyColorAttachmentBindings.push_back(ChannelBinding(
236 this, static_cast<SignalToken>(DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex)));
237 }
Corentin Wallez37c39792015-08-20 14:19:46 -0400238}
239
240Framebuffer::Framebuffer(rx::SurfaceImpl *surface)
Jamie Madill362876b2016-06-16 14:46:59 -0400241 : mState(),
242 mImpl(surface->createDefaultFramebuffer(mState)),
243 mId(0),
244 mCachedStatus(GL_FRAMEBUFFER_COMPLETE),
245 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
246 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
Corentin Wallez37c39792015-08-20 14:19:46 -0400247{
Geoff Langda88add2014-12-01 10:22:01 -0500248 ASSERT(mImpl != nullptr);
Jamie Madill362876b2016-06-16 14:46:59 -0400249 mDirtyColorAttachmentBindings.push_back(
250 ChannelBinding(this, static_cast<SignalToken>(DIRTY_BIT_COLOR_ATTACHMENT_0)));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251}
252
253Framebuffer::~Framebuffer()
254{
Geoff Langda88add2014-12-01 10:22:01 -0500255 SafeDelete(mImpl);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000256}
257
Geoff Lang70d0f492015-12-10 17:45:46 -0500258void Framebuffer::setLabel(const std::string &label)
259{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400260 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500261}
262
263const std::string &Framebuffer::getLabel() const
264{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400265 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500266}
267
Jamie Madille261b442014-06-25 12:42:21 -0400268void Framebuffer::detachTexture(GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269{
Jamie Madilld1405e52015-03-05 15:41:39 -0500270 detachResourceById(GL_TEXTURE, textureId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271}
272
Jamie Madille261b442014-06-25 12:42:21 -0400273void Framebuffer::detachRenderbuffer(GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000274{
Jamie Madilld1405e52015-03-05 15:41:39 -0500275 detachResourceById(GL_RENDERBUFFER, renderbufferId);
276}
Jamie Madille261b442014-06-25 12:42:21 -0400277
Jamie Madilld1405e52015-03-05 15:41:39 -0500278void Framebuffer::detachResourceById(GLenum resourceType, GLuint resourceId)
279{
Jamie Madill362876b2016-06-16 14:46:59 -0400280 for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
Jamie Madilld1405e52015-03-05 15:41:39 -0500281 {
Jamie Madill362876b2016-06-16 14:46:59 -0400282 detachMatchingAttachment(&mState.mColorAttachments[colorIndex], resourceType, resourceId,
283 DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284 }
285
Jamie Madill362876b2016-06-16 14:46:59 -0400286 detachMatchingAttachment(&mState.mDepthAttachment, resourceType, resourceId,
287 DIRTY_BIT_DEPTH_ATTACHMENT);
288 detachMatchingAttachment(&mState.mStencilAttachment, resourceType, resourceId,
289 DIRTY_BIT_STENCIL_ATTACHMENT);
290}
291
292void Framebuffer::detachMatchingAttachment(FramebufferAttachment *attachment,
293 GLenum matchType,
294 GLuint matchId,
295 size_t dirtyBit)
296{
297 if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId)
298 {
299 attachment->detach();
300 mDirtyBits.set(dirtyBit);
301 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302}
303
Corentin Wallez37c39792015-08-20 14:19:46 -0400304const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400306 return mState.getColorAttachment(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307}
308
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400309const FramebufferAttachment *Framebuffer::getDepthbuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400310{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400311 return mState.getDepthAttachment();
Geoff Lang646559f2013-08-15 11:08:15 -0400312}
313
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400314const FramebufferAttachment *Framebuffer::getStencilbuffer() const
315{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400316 return mState.getStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400317}
318
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400319const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
320{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400321 return mState.getDepthStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400322}
323
324const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000325{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400326 return mState.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000327}
328
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400329const FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000330{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400331 return mState.getReadAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000332}
333
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000334GLenum Framebuffer::getReadColorbufferType() const
335{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400336 const FramebufferAttachment *readAttachment = mState.getReadAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400337 return (readAttachment != nullptr ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000338}
339
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400340const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000341{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400342 return mState.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000343}
344
Jamie Madill2d06b732015-04-20 12:53:28 -0400345const FramebufferAttachment *Framebuffer::getAttachment(GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000346{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400347 return mState.getAttachment(attachment);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400348}
349
Geoff Langa15472a2015-08-11 11:48:03 -0400350size_t Framebuffer::getDrawbufferStateCount() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000351{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400352 return mState.mDrawBufferStates.size();
Geoff Langa15472a2015-08-11 11:48:03 -0400353}
354
355GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const
356{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400357 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
358 return mState.mDrawBufferStates[drawBuffer];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000359}
360
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500361const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const
362{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400363 return mState.getDrawBufferStates();
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500364}
365
Geoff Lang164d54e2014-12-01 10:55:33 -0500366void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000367{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400368 auto &drawStates = mState.mDrawBufferStates;
Jamie Madilld1405e52015-03-05 15:41:39 -0500369
370 ASSERT(count <= drawStates.size());
371 std::copy(buffers, buffers + count, drawStates.begin());
372 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500373 mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS);
Geoff Lang9dd95802014-12-01 11:12:59 -0500374}
375
Geoff Langa15472a2015-08-11 11:48:03 -0400376const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const
377{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400378 return mState.getDrawBuffer(drawBuffer);
Geoff Langa15472a2015-08-11 11:48:03 -0400379}
380
381bool Framebuffer::hasEnabledDrawBuffer() const
382{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400383 for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
Geoff Langa15472a2015-08-11 11:48:03 -0400384 {
385 if (getDrawBuffer(drawbufferIdx) != nullptr)
386 {
387 return true;
388 }
389 }
390
391 return false;
392}
393
Geoff Lang9dd95802014-12-01 11:12:59 -0500394GLenum Framebuffer::getReadBufferState() const
395{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400396 return mState.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500397}
398
399void Framebuffer::setReadBuffer(GLenum buffer)
400{
Jamie Madillb885e572015-02-03 16:16:04 -0500401 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
402 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madill48ef11b2016-04-27 15:21:52 -0400403 (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size()));
404 mState.mReadBufferState = buffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500405 mDirtyBits.set(DIRTY_BIT_READ_BUFFER);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000406}
407
Corentin Wallez37c39792015-08-20 14:19:46 -0400408size_t Framebuffer::getNumColorBuffers() const
409{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400410 return mState.mColorAttachments.size();
Corentin Wallez37c39792015-08-20 14:19:46 -0400411}
412
Jamie Madill0df8fe42015-11-24 16:10:24 -0500413bool Framebuffer::hasDepth() const
414{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400415 return (mState.mDepthAttachment.isAttached() && mState.mDepthAttachment.getDepthSize() > 0);
Jamie Madill0df8fe42015-11-24 16:10:24 -0500416}
417
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000418bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000419{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400420 return (mState.mStencilAttachment.isAttached() &&
421 mState.mStencilAttachment.getStencilSize() > 0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000422}
423
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000424bool Framebuffer::usingExtendedDrawBuffers() const
425{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400426 for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000427 {
Geoff Langa15472a2015-08-11 11:48:03 -0400428 if (getDrawBuffer(drawbufferIdx) != nullptr)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000429 {
430 return true;
431 }
432 }
433
434 return false;
435}
436
Jamie Madill51f40ec2016-06-15 14:06:00 -0400437GLenum Framebuffer::checkStatus(const ContextState &state)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000438{
Geoff Lang528ce3c2014-12-01 10:44:07 -0500439 // The default framebuffer *must* always be complete, though it may not be
440 // subject to the same rules as application FBOs. ie, it could have 0x0 size.
441 if (mId == 0)
442 {
443 return GL_FRAMEBUFFER_COMPLETE;
444 }
445
Jamie Madill362876b2016-06-16 14:46:59 -0400446 if (hasAnyDirtyBit() || !mCachedStatus.valid())
447 {
448 mCachedStatus = checkStatusImpl(state);
449 }
450
451 return mCachedStatus.value();
452}
453
454GLenum Framebuffer::checkStatusImpl(const ContextState &state)
455{
456 ASSERT(mId != 0);
457
shannonwoods@chromium.orgf6fb9592013-05-30 00:09:40 +0000458 unsigned int colorbufferSize = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000459 int samples = -1;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000460 bool missingAttachment = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000461
Jamie Madill48ef11b2016-04-27 15:21:52 -0400462 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000463 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400464 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465 {
Jamie Madill6b120b92015-11-24 13:00:07 -0500466 const Extents &size = colorAttachment.getSize();
467 if (size.width == 0 || size.height == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000468 {
469 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
470 }
daniel@transgaming.com01868132010-08-24 19:21:17 +0000471
Jamie Madill2d06b732015-04-20 12:53:28 -0400472 GLenum internalformat = colorAttachment.getInternalFormat();
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700473 const TextureCaps &formatCaps = state.getTextureCap(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400474 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
Jamie Madill2d06b732015-04-20 12:53:28 -0400475 if (colorAttachment.type() == GL_TEXTURE)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000476 {
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400477 if (!formatCaps.renderable)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000478 {
Jamie Madill81176782015-11-24 16:10:23 -0500479 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000480 }
481
Geoff Lang5d601382014-07-22 15:14:06 -0400482 if (formatInfo.depthBits > 0 || formatInfo.stencilBits > 0)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000483 {
484 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
485 }
Jamie Madill6b120b92015-11-24 13:00:07 -0500486
487 if (colorAttachment.layer() >= size.depth)
488 {
489 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
490 }
Jamie Madill3215b202015-12-15 16:41:39 -0500491
492 // ES3 specifies that cube map texture attachments must be cube complete.
493 // This language is missing from the ES2 spec, but we enforce it here because some
494 // desktop OpenGL drivers also enforce this validation.
495 // TODO(jmadill): Check if OpenGL ES2 drivers enforce cube completeness.
496 const Texture *texture = colorAttachment.getTexture();
497 ASSERT(texture);
Olli Etuahoe8528d82016-05-16 17:50:52 +0300498 if (texture->getTarget() == GL_TEXTURE_CUBE_MAP &&
499 !texture->getTextureState().isCubeComplete())
Jamie Madill3215b202015-12-15 16:41:39 -0500500 {
501 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
502 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000503 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400504 else if (colorAttachment.type() == GL_RENDERBUFFER)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000505 {
Geoff Lang5d601382014-07-22 15:14:06 -0400506 if (!formatCaps.renderable || formatInfo.depthBits > 0 || formatInfo.stencilBits > 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400507 {
508 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
509 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000510 }
511
512 if (!missingAttachment)
513 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000514 // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that
515 // all color attachments have the same number of samples for the FBO to be complete.
Jamie Madill2d06b732015-04-20 12:53:28 -0400516 if (colorAttachment.getSamples() != samples)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000517 {
518 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT;
519 }
520
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +0000521 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
522 // in GLES 3.0, there is no such restriction
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700523 if (state.getClientVersion() < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000524 {
Geoff Lang5d601382014-07-22 15:14:06 -0400525 if (formatInfo.pixelBytes != colorbufferSize)
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +0000526 {
527 return GL_FRAMEBUFFER_UNSUPPORTED;
528 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000529 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000530 }
531 else
532 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400533 samples = colorAttachment.getSamples();
Geoff Lang5d601382014-07-22 15:14:06 -0400534 colorbufferSize = formatInfo.pixelBytes;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000535 missingAttachment = false;
536 }
537 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000538 }
539
Jamie Madill48ef11b2016-04-27 15:21:52 -0400540 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -0400541 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000542 {
Jamie Madill6b120b92015-11-24 13:00:07 -0500543 const Extents &size = depthAttachment.getSize();
544 if (size.width == 0 || size.height == 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000546 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547 }
548
Jamie Madill2d06b732015-04-20 12:53:28 -0400549 GLenum internalformat = depthAttachment.getInternalFormat();
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700550 const TextureCaps &formatCaps = state.getTextureCap(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400551 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
Jamie Madill2d06b732015-04-20 12:53:28 -0400552 if (depthAttachment.type() == GL_TEXTURE)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000553 {
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000554 // depth texture attachments require OES/ANGLE_depth_texture
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700555 if (!state.getExtensions().depthTextures)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000556 {
557 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
558 }
559
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400560 if (!formatCaps.renderable)
Geoff Langcec35902014-04-16 10:52:36 -0400561 {
Jamie Madill81176782015-11-24 16:10:23 -0500562 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
Geoff Langcec35902014-04-16 10:52:36 -0400563 }
564
Geoff Lang5d601382014-07-22 15:14:06 -0400565 if (formatInfo.depthBits == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000566 {
567 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
568 }
569 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400570 else if (depthAttachment.type() == GL_RENDERBUFFER)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000571 {
Geoff Lang5d601382014-07-22 15:14:06 -0400572 if (!formatCaps.renderable || formatInfo.depthBits == 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400573 {
574 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
575 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000576 }
577
578 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000579 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400580 samples = depthAttachment.getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000581 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000582 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400583 else if (samples != depthAttachment.getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000584 {
Sami Väisänena797e062016-05-12 15:23:40 +0300585 // CHROMIUM_framebuffer_mixed_samples allows a framebuffer to be
586 // considered complete when its depth or stencil samples are a
587 // multiple of the number of color samples.
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700588 const bool mixedSamples = state.getExtensions().framebufferMixedSamples;
Sami Väisänena797e062016-05-12 15:23:40 +0300589 if (!mixedSamples)
590 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
591
592 const int colorSamples = samples ? samples : 1;
593 const int depthSamples = depthAttachment.getSamples();
594 if ((depthSamples % colorSamples) != 0)
595 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000596 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000597 }
598
Jamie Madill48ef11b2016-04-27 15:21:52 -0400599 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -0400600 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000601 {
Jamie Madill6b120b92015-11-24 13:00:07 -0500602 const Extents &size = stencilAttachment.getSize();
603 if (size.width == 0 || size.height == 0)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000604 {
605 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
606 }
607
Jamie Madill2d06b732015-04-20 12:53:28 -0400608 GLenum internalformat = stencilAttachment.getInternalFormat();
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700609 const TextureCaps &formatCaps = state.getTextureCap(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400610 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
Jamie Madill2d06b732015-04-20 12:53:28 -0400611 if (stencilAttachment.type() == GL_TEXTURE)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000612 {
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000613 // texture stencil attachments come along as part
614 // of OES_packed_depth_stencil + OES/ANGLE_depth_texture
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700615 if (!state.getExtensions().depthTextures)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000616 {
617 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
618 }
619
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400620 if (!formatCaps.renderable)
Geoff Langcec35902014-04-16 10:52:36 -0400621 {
Jamie Madill81176782015-11-24 16:10:23 -0500622 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
Geoff Langcec35902014-04-16 10:52:36 -0400623 }
624
Geoff Lang5d601382014-07-22 15:14:06 -0400625 if (formatInfo.stencilBits == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000626 {
627 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
628 }
629 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400630 else if (stencilAttachment.type() == GL_RENDERBUFFER)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000631 {
Geoff Lang5d601382014-07-22 15:14:06 -0400632 if (!formatCaps.renderable || formatInfo.stencilBits == 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400633 {
634 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
635 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000636 }
637
638 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000639 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400640 samples = stencilAttachment.getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000641 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000642 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400643 else if (samples != stencilAttachment.getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000644 {
Sami Väisänena797e062016-05-12 15:23:40 +0300645 // see the comments in depth attachment check.
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700646 const bool mixedSamples = state.getExtensions().framebufferMixedSamples;
Sami Väisänena797e062016-05-12 15:23:40 +0300647 if (!mixedSamples)
648 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
649
650 const int colorSamples = samples ? samples : 1;
651 const int stencilSamples = stencilAttachment.getSamples();
652 if ((stencilSamples % colorSamples) != 0)
653 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000654 }
Corentin Wallez086d59a2016-04-29 09:06:49 -0400655
656 // Starting from ES 3.0 stencil and depth, if present, should be the same image
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700657 if (state.getClientVersion() >= 3 && depthAttachment.isAttached() &&
Corentin Wallez086d59a2016-04-29 09:06:49 -0400658 stencilAttachment != depthAttachment)
659 {
660 return GL_FRAMEBUFFER_UNSUPPORTED;
661 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000662 }
663
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000664 // we need to have at least one attachment to be complete
665 if (missingAttachment)
666 {
667 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000668 }
669
Jamie Madillcc86d642015-11-24 13:00:07 -0500670 // In ES 2.0, all color attachments must have the same width and height.
671 // In ES 3.0, there is no such restriction.
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700672 if (state.getClientVersion() < 3 && !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -0500673 {
674 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
675 }
676
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500677 syncState();
Jamie Madillcc86d642015-11-24 13:00:07 -0500678 if (!mImpl->checkStatus())
679 {
680 return GL_FRAMEBUFFER_UNSUPPORTED;
681 }
682
683 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000685
Austin Kinross08332632015-05-05 13:35:47 -0700686Error Framebuffer::discard(size_t count, const GLenum *attachments)
687{
688 return mImpl->discard(count, attachments);
689}
690
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500691Error Framebuffer::invalidate(size_t count, const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400692{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500693 return mImpl->invalidate(count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400694}
695
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500696Error Framebuffer::invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -0400697{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500698 return mImpl->invalidateSub(count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -0400699}
700
Jamie Madill8415b5f2016-04-26 13:41:39 -0400701Error Framebuffer::clear(rx::ContextImpl *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -0500702{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700703 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500704 {
Jamie Madill362876b2016-06-16 14:46:59 -0400705 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500706 }
707
Jamie Madill8415b5f2016-04-26 13:41:39 -0400708 return mImpl->clear(context, mask);
Geoff Langb04dc822014-12-01 12:02:02 -0500709}
710
Jamie Madill8415b5f2016-04-26 13:41:39 -0400711Error Framebuffer::clearBufferfv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400712 GLenum buffer,
713 GLint drawbuffer,
714 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500715{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700716 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500717 {
Jamie Madill362876b2016-06-16 14:46:59 -0400718 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500719 }
720
Jamie Madill8415b5f2016-04-26 13:41:39 -0400721 return mImpl->clearBufferfv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500722}
723
Jamie Madill8415b5f2016-04-26 13:41:39 -0400724Error Framebuffer::clearBufferuiv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400725 GLenum buffer,
726 GLint drawbuffer,
727 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500728{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700729 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500730 {
Jamie Madill362876b2016-06-16 14:46:59 -0400731 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500732 }
733
Jamie Madill8415b5f2016-04-26 13:41:39 -0400734 return mImpl->clearBufferuiv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500735}
736
Jamie Madill8415b5f2016-04-26 13:41:39 -0400737Error Framebuffer::clearBufferiv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400738 GLenum buffer,
739 GLint drawbuffer,
740 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500741{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700742 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500743 {
Jamie Madill362876b2016-06-16 14:46:59 -0400744 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500745 }
746
Jamie Madill8415b5f2016-04-26 13:41:39 -0400747 return mImpl->clearBufferiv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500748}
749
Jamie Madill8415b5f2016-04-26 13:41:39 -0400750Error Framebuffer::clearBufferfi(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400751 GLenum buffer,
752 GLint drawbuffer,
753 GLfloat depth,
754 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -0500755{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700756 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500757 {
Jamie Madill362876b2016-06-16 14:46:59 -0400758 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500759 }
760
Jamie Madill8415b5f2016-04-26 13:41:39 -0400761 return mImpl->clearBufferfi(context, buffer, drawbuffer, depth, stencil);
Geoff Langb04dc822014-12-01 12:02:02 -0500762}
763
Geoff Langbce529e2014-12-01 12:48:41 -0500764GLenum Framebuffer::getImplementationColorReadFormat() const
765{
766 return mImpl->getImplementationColorReadFormat();
767}
768
769GLenum Framebuffer::getImplementationColorReadType() const
770{
771 return mImpl->getImplementationColorReadType();
772}
773
Jamie Madill8415b5f2016-04-26 13:41:39 -0400774Error Framebuffer::readPixels(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500775 const Rectangle &area,
Jamie Madill1b94d432015-08-07 13:23:23 -0400776 GLenum format,
777 GLenum type,
778 GLvoid *pixels) const
Geoff Langbce529e2014-12-01 12:48:41 -0500779{
Jamie Madill362876b2016-06-16 14:46:59 -0400780 ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels));
Geoff Lang520c4ae2015-05-05 13:12:36 -0400781
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700782 Buffer *unpackBuffer = context->getGLState().getUnpackState().pixelBuffer.get();
Geoff Lang520c4ae2015-05-05 13:12:36 -0400783 if (unpackBuffer)
784 {
785 unpackBuffer->onPixelUnpack();
786 }
787
Jamie Madill362876b2016-06-16 14:46:59 -0400788 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -0500789}
790
Jamie Madill8415b5f2016-04-26 13:41:39 -0400791Error Framebuffer::blit(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500792 const Rectangle &sourceArea,
793 const Rectangle &destArea,
Geoff Lang242468f2015-09-24 14:15:41 -0400794 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -0400795 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -0500796{
Jamie Madill8415b5f2016-04-26 13:41:39 -0400797 return mImpl->blit(context, sourceArea, destArea, mask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -0500798}
799
Jamie Madill51f40ec2016-06-15 14:06:00 -0400800int Framebuffer::getSamples(const ContextState &state)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000801{
Jamie Madill362876b2016-06-16 14:46:59 -0400802 if (complete(state))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000803 {
Jamie Madill362876b2016-06-16 14:46:59 -0400804 // For a complete framebuffer, all attachments must have the same sample count.
805 // In this case return the first nonzero sample size.
806 const auto *firstColorAttachment = mState.getFirstColorAttachment();
807 if (firstColorAttachment)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000808 {
Jamie Madill362876b2016-06-16 14:46:59 -0400809 ASSERT(firstColorAttachment->isAttached());
810 return firstColorAttachment->getSamples();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000811 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000812 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000813
814 return 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000815}
816
Jamie Madille261b442014-06-25 12:42:21 -0400817bool Framebuffer::hasValidDepthStencil() const
818{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400819 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -0400820}
821
Jamie Madill2d06b732015-04-20 12:53:28 -0400822void Framebuffer::setAttachment(GLenum type,
823 GLenum binding,
824 const ImageIndex &textureIndex,
825 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -0400826{
Jamie Madill2d06b732015-04-20 12:53:28 -0400827 if (binding == GL_DEPTH_STENCIL || binding == GL_DEPTH_STENCIL_ATTACHMENT)
Geoff Langab75a052014-10-15 12:56:37 -0400828 {
Geoff Langab75a052014-10-15 12:56:37 -0400829 // ensure this is a legitimate depth+stencil format
Jamie Madill375c37c2015-07-21 15:14:08 -0400830 FramebufferAttachmentObject *attachmentObj = resource;
831 if (resource)
Geoff Langab75a052014-10-15 12:56:37 -0400832 {
Jamie Madill375c37c2015-07-21 15:14:08 -0400833 FramebufferAttachment::Target target(binding, textureIndex);
834 GLenum internalFormat = resource->getAttachmentInternalFormat(target);
835 const InternalFormat &formatInfo = GetInternalFormatInfo(internalFormat);
836 if (formatInfo.depthBits == 0 || formatInfo.stencilBits == 0)
837 {
838 // Attaching nullptr detaches the current attachment.
839 attachmentObj = nullptr;
840 }
Geoff Langab75a052014-10-15 12:56:37 -0400841 }
Jamie Madill375c37c2015-07-21 15:14:08 -0400842
Jamie Madill48ef11b2016-04-27 15:21:52 -0400843 mState.mDepthAttachment.attach(type, binding, textureIndex, attachmentObj);
844 mState.mStencilAttachment.attach(type, binding, textureIndex, attachmentObj);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500845 mDirtyBits.set(DIRTY_BIT_DEPTH_ATTACHMENT);
846 mDirtyBits.set(DIRTY_BIT_STENCIL_ATTACHMENT);
Jamie Madill362876b2016-06-16 14:46:59 -0400847 BindResourceChannel(&mDirtyDepthAttachmentBinding, resource);
848 BindResourceChannel(&mDirtyStencilAttachmentBinding, resource);
Geoff Langab75a052014-10-15 12:56:37 -0400849 }
850 else
851 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400852 switch (binding)
853 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500854 case GL_DEPTH:
855 case GL_DEPTH_ATTACHMENT:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400856 mState.mDepthAttachment.attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500857 mDirtyBits.set(DIRTY_BIT_DEPTH_ATTACHMENT);
Jamie Madill362876b2016-06-16 14:46:59 -0400858 BindResourceChannel(&mDirtyDepthAttachmentBinding, resource);
859 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500860 case GL_STENCIL:
861 case GL_STENCIL_ATTACHMENT:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400862 mState.mStencilAttachment.attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500863 mDirtyBits.set(DIRTY_BIT_STENCIL_ATTACHMENT);
Jamie Madill362876b2016-06-16 14:46:59 -0400864 BindResourceChannel(&mDirtyStencilAttachmentBinding, resource);
865 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500866 case GL_BACK:
Jamie Madill48ef11b2016-04-27 15:21:52 -0400867 mState.mColorAttachments[0].attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500868 mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0);
Jamie Madill362876b2016-06-16 14:46:59 -0400869 // No need for a resource binding for the default FBO, it's always complete.
870 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500871 default:
Jamie Madill2d06b732015-04-20 12:53:28 -0400872 {
873 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
Jamie Madill48ef11b2016-04-27 15:21:52 -0400874 ASSERT(colorIndex < mState.mColorAttachments.size());
875 mState.mColorAttachments[colorIndex].attach(type, binding, textureIndex, resource);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500876 mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
Jamie Madill362876b2016-06-16 14:46:59 -0400877 BindResourceChannel(&mDirtyColorAttachmentBindings[colorIndex], resource);
Jamie Madill2d06b732015-04-20 12:53:28 -0400878 }
879 break;
880 }
Geoff Langab75a052014-10-15 12:56:37 -0400881 }
882}
883
Jamie Madill2d06b732015-04-20 12:53:28 -0400884void Framebuffer::resetAttachment(GLenum binding)
885{
886 setAttachment(GL_NONE, binding, ImageIndex::MakeInvalid(), nullptr);
887}
888
Jamie Madill362876b2016-06-16 14:46:59 -0400889void Framebuffer::syncState()
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500890{
891 if (mDirtyBits.any())
892 {
893 mImpl->syncState(mDirtyBits);
894 mDirtyBits.reset();
Jamie Madill362876b2016-06-16 14:46:59 -0400895 mCachedStatus.reset();
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500896 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897}
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500898
Jamie Madill362876b2016-06-16 14:46:59 -0400899void Framebuffer::signal(SignalToken token)
Jamie Madill51f40ec2016-06-15 14:06:00 -0400900{
Jamie Madill362876b2016-06-16 14:46:59 -0400901 // TOOD(jmadill): Make this only update individual attachments to do less work.
902 mCachedStatus.reset();
Jamie Madill51f40ec2016-06-15 14:06:00 -0400903}
904
Jamie Madill362876b2016-06-16 14:46:59 -0400905bool Framebuffer::complete(const ContextState &state)
Jamie Madill51f40ec2016-06-15 14:06:00 -0400906{
Jamie Madill362876b2016-06-16 14:46:59 -0400907 return (checkStatus(state) == GL_FRAMEBUFFER_COMPLETE);
Jamie Madill51f40ec2016-06-15 14:06:00 -0400908}
909
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500910} // namespace gl