blob: d34014492d16eacdc430930b62784914f367bf1f [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 Madilla4595b82017-01-11 17:36:34 -050012#include "common/BitSetIterator.h"
Jamie Madillcc86d642015-11-24 13:00:07 -050013#include "common/Optional.h"
Jamie Madillc46f45d2015-03-31 13:20:55 -040014#include "common/utilities.h"
15#include "libANGLE/Config.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/Context.h"
Jamie Madill6c1f6712017-02-14 19:08:04 -050017#include "libANGLE/Display.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050018#include "libANGLE/FramebufferAttachment.h"
Jamie Madillc46f45d2015-03-31 13:20:55 -040019#include "libANGLE/Renderbuffer.h"
20#include "libANGLE/Surface.h"
21#include "libANGLE/Texture.h"
22#include "libANGLE/formatutils.h"
Jamie Madill8415b5f2016-04-26 13:41:39 -040023#include "libANGLE/renderer/ContextImpl.h"
Geoff Langb5d8f232014-12-04 15:43:01 -050024#include "libANGLE/renderer/FramebufferImpl.h"
Jamie Madill7aea7e02016-05-10 10:39:45 -040025#include "libANGLE/renderer/GLImplFactory.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050026#include "libANGLE/renderer/RenderbufferImpl.h"
Corentin Wallez37c39792015-08-20 14:19:46 -040027#include "libANGLE/renderer/SurfaceImpl.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040028
Jamie Madill362876b2016-06-16 14:46:59 -040029using namespace angle;
30
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031namespace gl
32{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000033
Jamie Madilld1405e52015-03-05 15:41:39 -050034namespace
35{
Jamie Madill362876b2016-06-16 14:46:59 -040036
37void BindResourceChannel(ChannelBinding *binding, FramebufferAttachmentObject *resource)
Jamie Madilld1405e52015-03-05 15:41:39 -050038{
Jamie Madill362876b2016-06-16 14:46:59 -040039 binding->bind(resource ? resource->getDirtyChannel() : nullptr);
Jamie Madilld1405e52015-03-05 15:41:39 -050040}
Jamie Madill362876b2016-06-16 14:46:59 -040041
42} // anonymous namespace
Jamie Madilld1405e52015-03-05 15:41:39 -050043
Jamie Madill6f60d052017-02-22 15:20:11 -050044// This constructor is only used for default framebuffers.
Jamie Madill48ef11b2016-04-27 15:21:52 -040045FramebufferState::FramebufferState()
Geoff Lang70d0f492015-12-10 17:45:46 -050046 : mLabel(),
47 mColorAttachments(1),
Jamie Madill6f60d052017-02-22 15:20:11 -050048 mDrawBufferStates(1, GL_BACK),
49 mReadBufferState(GL_BACK),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +080050 mDefaultWidth(0),
51 mDefaultHeight(0),
52 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -050053 mDefaultFixedSampleLocations(GL_FALSE),
54 mWebGLDepthStencilConsistent(true)
Corentin Wallez37c39792015-08-20 14:19:46 -040055{
Jamie Madilla4595b82017-01-11 17:36:34 -050056 mEnabledDrawBuffers.set(0);
Corentin Wallez37c39792015-08-20 14:19:46 -040057}
58
Jamie Madill48ef11b2016-04-27 15:21:52 -040059FramebufferState::FramebufferState(const Caps &caps)
Geoff Lang70d0f492015-12-10 17:45:46 -050060 : mLabel(),
61 mColorAttachments(caps.maxColorAttachments),
Jamie Madilld1405e52015-03-05 15:41:39 -050062 mDrawBufferStates(caps.maxDrawBuffers, GL_NONE),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +080063 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT),
64 mDefaultWidth(0),
65 mDefaultHeight(0),
66 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -050067 mDefaultFixedSampleLocations(GL_FALSE),
68 mWebGLDepthStencilConsistent(true)
Jamie Madilld1405e52015-03-05 15:41:39 -050069{
Geoff Langa15472a2015-08-11 11:48:03 -040070 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilld1405e52015-03-05 15:41:39 -050071 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
72}
73
Jamie Madill48ef11b2016-04-27 15:21:52 -040074FramebufferState::~FramebufferState()
Jamie Madilld1405e52015-03-05 15:41:39 -050075{
Jamie Madilld1405e52015-03-05 15:41:39 -050076}
77
Jamie Madill48ef11b2016-04-27 15:21:52 -040078const std::string &FramebufferState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -050079{
80 return mLabel;
81}
82
Geoff Lang4b7f12b2016-06-21 16:47:07 -040083const FramebufferAttachment *FramebufferState::getAttachment(GLenum attachment) const
84{
85 if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15)
86 {
87 return getColorAttachment(attachment - GL_COLOR_ATTACHMENT0);
88 }
89
90 switch (attachment)
91 {
92 case GL_COLOR:
93 case GL_BACK:
94 return getColorAttachment(0);
95 case GL_DEPTH:
96 case GL_DEPTH_ATTACHMENT:
97 return getDepthAttachment();
98 case GL_STENCIL:
99 case GL_STENCIL_ATTACHMENT:
100 return getStencilAttachment();
101 case GL_DEPTH_STENCIL:
102 case GL_DEPTH_STENCIL_ATTACHMENT:
103 return getDepthStencilAttachment();
104 default:
105 UNREACHABLE();
106 return nullptr;
107 }
108}
109
Jamie Madill48ef11b2016-04-27 15:21:52 -0400110const FramebufferAttachment *FramebufferState::getReadAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500111{
Antoine Labour2ec65dc2016-11-30 16:28:58 -0800112 if (mReadBufferState == GL_NONE)
113 {
114 return nullptr;
115 }
Jamie Madill7147f012015-03-05 15:41:40 -0500116 ASSERT(mReadBufferState == GL_BACK || (mReadBufferState >= GL_COLOR_ATTACHMENT0 && mReadBufferState <= GL_COLOR_ATTACHMENT15));
117 size_t readIndex = (mReadBufferState == GL_BACK ? 0 : static_cast<size_t>(mReadBufferState - GL_COLOR_ATTACHMENT0));
118 ASSERT(readIndex < mColorAttachments.size());
Jamie Madill2d06b732015-04-20 12:53:28 -0400119 return mColorAttachments[readIndex].isAttached() ? &mColorAttachments[readIndex] : nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500120}
121
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500122const FramebufferAttachment *FramebufferState::getFirstNonNullAttachment() const
123{
124 auto *colorAttachment = getFirstColorAttachment();
125 if (colorAttachment)
126 {
127 return colorAttachment;
128 }
129 return getDepthOrStencilAttachment();
130}
131
Jamie Madill48ef11b2016-04-27 15:21:52 -0400132const FramebufferAttachment *FramebufferState::getFirstColorAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500133{
Jamie Madill2d06b732015-04-20 12:53:28 -0400134 for (const FramebufferAttachment &colorAttachment : mColorAttachments)
Jamie Madill7147f012015-03-05 15:41:40 -0500135 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400136 if (colorAttachment.isAttached())
Jamie Madill7147f012015-03-05 15:41:40 -0500137 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400138 return &colorAttachment;
Jamie Madill7147f012015-03-05 15:41:40 -0500139 }
140 }
141
142 return nullptr;
143}
144
Jamie Madill48ef11b2016-04-27 15:21:52 -0400145const FramebufferAttachment *FramebufferState::getDepthOrStencilAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500146{
Jamie Madill2d06b732015-04-20 12:53:28 -0400147 if (mDepthAttachment.isAttached())
148 {
149 return &mDepthAttachment;
150 }
151 if (mStencilAttachment.isAttached())
152 {
153 return &mStencilAttachment;
154 }
155 return nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500156}
157
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500158const FramebufferAttachment *FramebufferState::getStencilOrDepthStencilAttachment() const
159{
160 if (mStencilAttachment.isAttached())
161 {
162 return &mStencilAttachment;
163 }
164 return getDepthStencilAttachment();
165}
166
Jamie Madill48ef11b2016-04-27 15:21:52 -0400167const FramebufferAttachment *FramebufferState::getColorAttachment(size_t colorAttachment) const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400168{
169 ASSERT(colorAttachment < mColorAttachments.size());
Jamie Madill2d06b732015-04-20 12:53:28 -0400170 return mColorAttachments[colorAttachment].isAttached() ?
171 &mColorAttachments[colorAttachment] :
172 nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400173}
174
Jamie Madill48ef11b2016-04-27 15:21:52 -0400175const FramebufferAttachment *FramebufferState::getDepthAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400176{
Jamie Madill2d06b732015-04-20 12:53:28 -0400177 return mDepthAttachment.isAttached() ? &mDepthAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400178}
179
Jamie Madill48ef11b2016-04-27 15:21:52 -0400180const FramebufferAttachment *FramebufferState::getStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400181{
Jamie Madill2d06b732015-04-20 12:53:28 -0400182 return mStencilAttachment.isAttached() ? &mStencilAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400183}
184
Jamie Madill48ef11b2016-04-27 15:21:52 -0400185const FramebufferAttachment *FramebufferState::getDepthStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400186{
187 // A valid depth-stencil attachment has the same resource bound to both the
188 // depth and stencil attachment points.
Jamie Madill2d06b732015-04-20 12:53:28 -0400189 if (mDepthAttachment.isAttached() && mStencilAttachment.isAttached() &&
Jamie Madill44f26482016-11-18 12:49:15 -0500190 mDepthAttachment == mStencilAttachment)
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400191 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400192 return &mDepthAttachment;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400193 }
194
195 return nullptr;
196}
197
Jamie Madill48ef11b2016-04-27 15:21:52 -0400198bool FramebufferState::attachmentsHaveSameDimensions() const
Jamie Madillcc86d642015-11-24 13:00:07 -0500199{
200 Optional<Extents> attachmentSize;
201
202 auto hasMismatchedSize = [&attachmentSize](const FramebufferAttachment &attachment)
203 {
204 if (!attachment.isAttached())
205 {
206 return false;
207 }
208
209 if (!attachmentSize.valid())
210 {
211 attachmentSize = attachment.getSize();
212 return false;
213 }
214
215 return (attachment.getSize() != attachmentSize.value());
216 };
217
218 for (const auto &attachment : mColorAttachments)
219 {
220 if (hasMismatchedSize(attachment))
221 {
222 return false;
223 }
224 }
225
226 if (hasMismatchedSize(mDepthAttachment))
227 {
228 return false;
229 }
230
231 return !hasMismatchedSize(mStencilAttachment);
232}
233
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400234const gl::FramebufferAttachment *FramebufferState::getDrawBuffer(size_t drawBufferIdx) const
235{
236 ASSERT(drawBufferIdx < mDrawBufferStates.size());
237 if (mDrawBufferStates[drawBufferIdx] != GL_NONE)
238 {
239 // ES3 spec: "If the GL is bound to a draw framebuffer object, the ith buffer listed in bufs
240 // must be COLOR_ATTACHMENTi or NONE"
241 ASSERT(mDrawBufferStates[drawBufferIdx] == GL_COLOR_ATTACHMENT0 + drawBufferIdx ||
242 (drawBufferIdx == 0 && mDrawBufferStates[drawBufferIdx] == GL_BACK));
243 return getAttachment(mDrawBufferStates[drawBufferIdx]);
244 }
245 else
246 {
247 return nullptr;
248 }
249}
250
251size_t FramebufferState::getDrawBufferCount() const
252{
253 return mDrawBufferStates.size();
254}
255
Geoff Langb21e20d2016-07-19 15:35:41 -0400256bool FramebufferState::colorAttachmentsAreUniqueImages() const
257{
258 for (size_t firstAttachmentIdx = 0; firstAttachmentIdx < mColorAttachments.size();
259 firstAttachmentIdx++)
260 {
261 const gl::FramebufferAttachment &firstAttachment = mColorAttachments[firstAttachmentIdx];
262 if (!firstAttachment.isAttached())
263 {
264 continue;
265 }
266
267 for (size_t secondAttachmentIdx = firstAttachmentIdx + 1;
268 secondAttachmentIdx < mColorAttachments.size(); secondAttachmentIdx++)
269 {
270 const gl::FramebufferAttachment &secondAttachment =
271 mColorAttachments[secondAttachmentIdx];
272 if (!secondAttachment.isAttached())
273 {
274 continue;
275 }
276
277 if (firstAttachment == secondAttachment)
278 {
279 return false;
280 }
281 }
282 }
283
284 return true;
285}
286
Jamie Madill7aea7e02016-05-10 10:39:45 -0400287Framebuffer::Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id)
Jamie Madill362876b2016-06-16 14:46:59 -0400288 : mState(caps),
289 mImpl(factory->createFramebuffer(mState)),
290 mId(id),
291 mCachedStatus(),
292 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
293 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294{
Corentin Wallez37c39792015-08-20 14:19:46 -0400295 ASSERT(mId != 0);
296 ASSERT(mImpl != nullptr);
Jamie Madill362876b2016-06-16 14:46:59 -0400297 ASSERT(mState.mColorAttachments.size() == static_cast<size_t>(caps.maxColorAttachments));
298
299 for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
300 {
301 mDirtyColorAttachmentBindings.push_back(ChannelBinding(
302 this, static_cast<SignalToken>(DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex)));
303 }
Corentin Wallez37c39792015-08-20 14:19:46 -0400304}
305
Jamie Madill6f60d052017-02-22 15:20:11 -0500306Framebuffer::Framebuffer(egl::Surface *surface)
Jamie Madill362876b2016-06-16 14:46:59 -0400307 : mState(),
Jamie Madill6f60d052017-02-22 15:20:11 -0500308 mImpl(surface->getImplementation()->createDefaultFramebuffer(mState)),
Jamie Madill362876b2016-06-16 14:46:59 -0400309 mId(0),
310 mCachedStatus(GL_FRAMEBUFFER_COMPLETE),
311 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
312 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
Corentin Wallez37c39792015-08-20 14:19:46 -0400313{
Geoff Langda88add2014-12-01 10:22:01 -0500314 ASSERT(mImpl != nullptr);
Jamie Madill362876b2016-06-16 14:46:59 -0400315 mDirtyColorAttachmentBindings.push_back(
316 ChannelBinding(this, static_cast<SignalToken>(DIRTY_BIT_COLOR_ATTACHMENT_0)));
Jamie Madill6f60d052017-02-22 15:20:11 -0500317
Jamie Madilla02315b2017-02-23 14:14:47 -0500318 setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_BACK, gl::ImageIndex::MakeInvalid(), surface);
Jamie Madill6f60d052017-02-22 15:20:11 -0500319
320 if (surface->getConfig()->depthSize > 0)
321 {
Jamie Madilla02315b2017-02-23 14:14:47 -0500322 setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, gl::ImageIndex::MakeInvalid(), surface);
Jamie Madill6f60d052017-02-22 15:20:11 -0500323 }
324
325 if (surface->getConfig()->stencilSize > 0)
326 {
Jamie Madilla02315b2017-02-23 14:14:47 -0500327 setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, gl::ImageIndex::MakeInvalid(),
328 surface);
Jamie Madill6f60d052017-02-22 15:20:11 -0500329 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330}
331
Corentin Wallezccab69d2017-01-27 16:57:15 -0500332Framebuffer::Framebuffer(rx::GLImplFactory *factory)
333 : mState(),
334 mImpl(factory->createFramebuffer(mState)),
335 mId(0),
336 mCachedStatus(GL_FRAMEBUFFER_UNDEFINED_OES),
337 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
338 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
339{
340 mDirtyColorAttachmentBindings.push_back(
341 ChannelBinding(this, static_cast<SignalToken>(DIRTY_BIT_COLOR_ATTACHMENT_0)));
342}
343
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344Framebuffer::~Framebuffer()
345{
Geoff Langda88add2014-12-01 10:22:01 -0500346 SafeDelete(mImpl);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000347}
348
Jamie Madill6c1f6712017-02-14 19:08:04 -0500349void Framebuffer::destroy(const Context *context)
350{
351 mImpl->destroy(rx::SafeGetImpl(context));
352}
353
354void Framebuffer::destroyDefault(const egl::Display *display)
355{
356 mImpl->destroyDefault(rx::SafeGetImpl(display));
357}
358
Geoff Lang70d0f492015-12-10 17:45:46 -0500359void Framebuffer::setLabel(const std::string &label)
360{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400361 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500362}
363
364const std::string &Framebuffer::getLabel() const
365{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400366 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500367}
368
Jamie Madilla02315b2017-02-23 14:14:47 -0500369void Framebuffer::detachTexture(const Context *context, GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370{
Jamie Madilla02315b2017-02-23 14:14:47 -0500371 detachResourceById(context, GL_TEXTURE, textureId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372}
373
Jamie Madilla02315b2017-02-23 14:14:47 -0500374void Framebuffer::detachRenderbuffer(const Context *context, GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000375{
Jamie Madilla02315b2017-02-23 14:14:47 -0500376 detachResourceById(context, GL_RENDERBUFFER, renderbufferId);
Jamie Madilld1405e52015-03-05 15:41:39 -0500377}
Jamie Madille261b442014-06-25 12:42:21 -0400378
Jamie Madilla02315b2017-02-23 14:14:47 -0500379void Framebuffer::detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId)
Jamie Madilld1405e52015-03-05 15:41:39 -0500380{
Jamie Madill362876b2016-06-16 14:46:59 -0400381 for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
Jamie Madilld1405e52015-03-05 15:41:39 -0500382 {
Jamie Madill362876b2016-06-16 14:46:59 -0400383 detachMatchingAttachment(&mState.mColorAttachments[colorIndex], resourceType, resourceId,
384 DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385 }
386
Jamie Madilla02315b2017-02-23 14:14:47 -0500387 if (context->isWebGL1())
388 {
389 const std::array<FramebufferAttachment *, 3> attachments = {
390 {&mState.mWebGLDepthStencilAttachment, &mState.mWebGLDepthAttachment,
391 &mState.mWebGLStencilAttachment}};
392 for (FramebufferAttachment *attachment : attachments)
393 {
394 if (attachment->isAttached() && attachment->type() == resourceType &&
395 attachment->id() == resourceId)
396 {
397 resetAttachment(context, attachment->getBinding());
398 }
399 }
400 }
401 else
402 {
403 detachMatchingAttachment(&mState.mDepthAttachment, resourceType, resourceId,
404 DIRTY_BIT_DEPTH_ATTACHMENT);
405 detachMatchingAttachment(&mState.mStencilAttachment, resourceType, resourceId,
406 DIRTY_BIT_STENCIL_ATTACHMENT);
407 }
Jamie Madill362876b2016-06-16 14:46:59 -0400408}
409
410void Framebuffer::detachMatchingAttachment(FramebufferAttachment *attachment,
411 GLenum matchType,
412 GLuint matchId,
413 size_t dirtyBit)
414{
415 if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId)
416 {
417 attachment->detach();
418 mDirtyBits.set(dirtyBit);
419 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
Corentin Wallez37c39792015-08-20 14:19:46 -0400422const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400424 return mState.getColorAttachment(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000425}
426
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400427const FramebufferAttachment *Framebuffer::getDepthbuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400428{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400429 return mState.getDepthAttachment();
Geoff Lang646559f2013-08-15 11:08:15 -0400430}
431
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400432const FramebufferAttachment *Framebuffer::getStencilbuffer() const
433{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400434 return mState.getStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400435}
436
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400437const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
438{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400439 return mState.getDepthStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400440}
441
442const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000443{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400444 return mState.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000445}
446
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500447const FramebufferAttachment *Framebuffer::getStencilOrDepthStencilAttachment() const
448{
449 return mState.getStencilOrDepthStencilAttachment();
450}
451
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400452const FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000453{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400454 return mState.getReadAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000455}
456
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000457GLenum Framebuffer::getReadColorbufferType() const
458{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400459 const FramebufferAttachment *readAttachment = mState.getReadAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400460 return (readAttachment != nullptr ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000461}
462
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400463const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000464{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400465 return mState.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000466}
467
Jamie Madill2d06b732015-04-20 12:53:28 -0400468const FramebufferAttachment *Framebuffer::getAttachment(GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000469{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400470 return mState.getAttachment(attachment);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400471}
472
Geoff Langa15472a2015-08-11 11:48:03 -0400473size_t Framebuffer::getDrawbufferStateCount() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000474{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400475 return mState.mDrawBufferStates.size();
Geoff Langa15472a2015-08-11 11:48:03 -0400476}
477
478GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const
479{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400480 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
481 return mState.mDrawBufferStates[drawBuffer];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000482}
483
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500484const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const
485{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400486 return mState.getDrawBufferStates();
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500487}
488
Geoff Lang164d54e2014-12-01 10:55:33 -0500489void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000490{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400491 auto &drawStates = mState.mDrawBufferStates;
Jamie Madilld1405e52015-03-05 15:41:39 -0500492
493 ASSERT(count <= drawStates.size());
494 std::copy(buffers, buffers + count, drawStates.begin());
495 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500496 mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS);
Jamie Madilla4595b82017-01-11 17:36:34 -0500497
498 mState.mEnabledDrawBuffers.reset();
499 for (size_t index = 0; index < count; ++index)
500 {
501 if (drawStates[index] != GL_NONE && mState.mColorAttachments[index].isAttached())
502 {
503 mState.mEnabledDrawBuffers.set(index);
504 }
505 }
Geoff Lang9dd95802014-12-01 11:12:59 -0500506}
507
Geoff Langa15472a2015-08-11 11:48:03 -0400508const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const
509{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400510 return mState.getDrawBuffer(drawBuffer);
Geoff Langa15472a2015-08-11 11:48:03 -0400511}
512
513bool Framebuffer::hasEnabledDrawBuffer() const
514{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400515 for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
Geoff Langa15472a2015-08-11 11:48:03 -0400516 {
517 if (getDrawBuffer(drawbufferIdx) != nullptr)
518 {
519 return true;
520 }
521 }
522
523 return false;
524}
525
Geoff Lang9dd95802014-12-01 11:12:59 -0500526GLenum Framebuffer::getReadBufferState() const
527{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400528 return mState.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500529}
530
531void Framebuffer::setReadBuffer(GLenum buffer)
532{
Jamie Madillb885e572015-02-03 16:16:04 -0500533 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
534 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madill48ef11b2016-04-27 15:21:52 -0400535 (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size()));
536 mState.mReadBufferState = buffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500537 mDirtyBits.set(DIRTY_BIT_READ_BUFFER);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000538}
539
Corentin Wallez37c39792015-08-20 14:19:46 -0400540size_t Framebuffer::getNumColorBuffers() const
541{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400542 return mState.mColorAttachments.size();
Corentin Wallez37c39792015-08-20 14:19:46 -0400543}
544
Jamie Madill0df8fe42015-11-24 16:10:24 -0500545bool Framebuffer::hasDepth() const
546{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400547 return (mState.mDepthAttachment.isAttached() && mState.mDepthAttachment.getDepthSize() > 0);
Jamie Madill0df8fe42015-11-24 16:10:24 -0500548}
549
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000550bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000551{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400552 return (mState.mStencilAttachment.isAttached() &&
553 mState.mStencilAttachment.getStencilSize() > 0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000554}
555
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000556bool Framebuffer::usingExtendedDrawBuffers() const
557{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400558 for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000559 {
Geoff Langa15472a2015-08-11 11:48:03 -0400560 if (getDrawBuffer(drawbufferIdx) != nullptr)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000561 {
562 return true;
563 }
564 }
565
566 return false;
567}
568
Jamie Madill51f40ec2016-06-15 14:06:00 -0400569GLenum Framebuffer::checkStatus(const ContextState &state)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570{
Corentin Wallezccab69d2017-01-27 16:57:15 -0500571 // The default framebuffer is always complete except when it is surfaceless in which
572 // case it is always unsupported. We return early because the default framebuffer may
573 // not be subject to the same rules as application FBOs. ie, it could have 0x0 size.
Geoff Lang528ce3c2014-12-01 10:44:07 -0500574 if (mId == 0)
575 {
Corentin Wallezccab69d2017-01-27 16:57:15 -0500576 ASSERT(mCachedStatus.valid());
577 ASSERT(mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE ||
578 mCachedStatus.value() == GL_FRAMEBUFFER_UNDEFINED_OES);
579 return mCachedStatus.value();
Geoff Lang528ce3c2014-12-01 10:44:07 -0500580 }
581
Jamie Madill362876b2016-06-16 14:46:59 -0400582 if (hasAnyDirtyBit() || !mCachedStatus.valid())
583 {
584 mCachedStatus = checkStatusImpl(state);
585 }
586
587 return mCachedStatus.value();
588}
589
590GLenum Framebuffer::checkStatusImpl(const ContextState &state)
591{
592 ASSERT(mId != 0);
593
shannonwoods@chromium.orgf6fb9592013-05-30 00:09:40 +0000594 unsigned int colorbufferSize = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000595 int samples = -1;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000596 bool missingAttachment = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800598 // TODO(yizhou): Check status for default framebuffer parameters.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400599 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000600 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400601 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602 {
Jamie Madill6b120b92015-11-24 13:00:07 -0500603 const Extents &size = colorAttachment.getSize();
604 if (size.width == 0 || size.height == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000605 {
606 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
607 }
daniel@transgaming.com01868132010-08-24 19:21:17 +0000608
Jamie Madilla3944d42016-07-22 22:13:26 -0400609 const Format &format = colorAttachment.getFormat();
610 const TextureCaps &formatCaps = state.getTextureCap(format.asSized());
Jamie Madill2d06b732015-04-20 12:53:28 -0400611 if (colorAttachment.type() == GL_TEXTURE)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000612 {
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400613 if (!formatCaps.renderable)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000614 {
Jamie Madill81176782015-11-24 16:10:23 -0500615 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000616 }
617
Jamie Madilla3944d42016-07-22 22:13:26 -0400618 if (format.info->depthBits > 0 || format.info->stencilBits > 0)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000619 {
620 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
621 }
Jamie Madill6b120b92015-11-24 13:00:07 -0500622
623 if (colorAttachment.layer() >= size.depth)
624 {
625 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
626 }
Jamie Madill3215b202015-12-15 16:41:39 -0500627
628 // ES3 specifies that cube map texture attachments must be cube complete.
629 // This language is missing from the ES2 spec, but we enforce it here because some
630 // desktop OpenGL drivers also enforce this validation.
631 // TODO(jmadill): Check if OpenGL ES2 drivers enforce cube completeness.
632 const Texture *texture = colorAttachment.getTexture();
633 ASSERT(texture);
Olli Etuahoe8528d82016-05-16 17:50:52 +0300634 if (texture->getTarget() == GL_TEXTURE_CUBE_MAP &&
635 !texture->getTextureState().isCubeComplete())
Jamie Madill3215b202015-12-15 16:41:39 -0500636 {
637 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
638 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000639 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400640 else if (colorAttachment.type() == GL_RENDERBUFFER)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000641 {
Jamie Madilla3944d42016-07-22 22:13:26 -0400642 if (!formatCaps.renderable || format.info->depthBits > 0 ||
643 format.info->stencilBits > 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400644 {
645 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
646 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000647 }
648
649 if (!missingAttachment)
650 {
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000651 // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that
652 // all color attachments have the same number of samples for the FBO to be complete.
Jamie Madill2d06b732015-04-20 12:53:28 -0400653 if (colorAttachment.getSamples() != samples)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000654 {
655 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT;
656 }
657
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +0000658 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
659 // in GLES 3.0, there is no such restriction
Martin Radev1be913c2016-07-11 17:59:16 +0300660 if (state.getClientMajorVersion() < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000661 {
Jamie Madilla3944d42016-07-22 22:13:26 -0400662 if (format.info->pixelBytes != colorbufferSize)
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +0000663 {
664 return GL_FRAMEBUFFER_UNSUPPORTED;
665 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000666 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000667 }
668 else
669 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400670 samples = colorAttachment.getSamples();
Jamie Madilla3944d42016-07-22 22:13:26 -0400671 colorbufferSize = format.info->pixelBytes;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000672 missingAttachment = false;
673 }
674 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000675 }
676
Jamie Madill48ef11b2016-04-27 15:21:52 -0400677 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -0400678 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000679 {
Jamie Madill6b120b92015-11-24 13:00:07 -0500680 const Extents &size = depthAttachment.getSize();
681 if (size.width == 0 || size.height == 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000683 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684 }
685
Jamie Madilla3944d42016-07-22 22:13:26 -0400686 const Format &format = depthAttachment.getFormat();
687 const TextureCaps &formatCaps = state.getTextureCap(format.asSized());
Jamie Madill2d06b732015-04-20 12:53:28 -0400688 if (depthAttachment.type() == GL_TEXTURE)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000689 {
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400690 if (!formatCaps.renderable)
Geoff Langcec35902014-04-16 10:52:36 -0400691 {
Jamie Madill81176782015-11-24 16:10:23 -0500692 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
Geoff Langcec35902014-04-16 10:52:36 -0400693 }
694
Jamie Madilla3944d42016-07-22 22:13:26 -0400695 if (format.info->depthBits == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000696 {
697 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
698 }
699 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400700 else if (depthAttachment.type() == GL_RENDERBUFFER)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000701 {
Jamie Madilla3944d42016-07-22 22:13:26 -0400702 if (!formatCaps.renderable || format.info->depthBits == 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400703 {
704 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
705 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000706 }
707
708 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000709 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400710 samples = depthAttachment.getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000711 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000712 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400713 else if (samples != depthAttachment.getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000714 {
Sami Väisänena797e062016-05-12 15:23:40 +0300715 // CHROMIUM_framebuffer_mixed_samples allows a framebuffer to be
716 // considered complete when its depth or stencil samples are a
717 // multiple of the number of color samples.
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700718 const bool mixedSamples = state.getExtensions().framebufferMixedSamples;
Sami Väisänena797e062016-05-12 15:23:40 +0300719 if (!mixedSamples)
720 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
721
722 const int colorSamples = samples ? samples : 1;
723 const int depthSamples = depthAttachment.getSamples();
724 if ((depthSamples % colorSamples) != 0)
725 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000726 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000727 }
728
Jamie Madill48ef11b2016-04-27 15:21:52 -0400729 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -0400730 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000731 {
Jamie Madill6b120b92015-11-24 13:00:07 -0500732 const Extents &size = stencilAttachment.getSize();
733 if (size.width == 0 || size.height == 0)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000734 {
735 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
736 }
737
Jamie Madilla3944d42016-07-22 22:13:26 -0400738 const Format &format = stencilAttachment.getFormat();
739 const TextureCaps &formatCaps = state.getTextureCap(format.asSized());
Jamie Madill2d06b732015-04-20 12:53:28 -0400740 if (stencilAttachment.type() == GL_TEXTURE)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000741 {
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400742 if (!formatCaps.renderable)
Geoff Langcec35902014-04-16 10:52:36 -0400743 {
Jamie Madill81176782015-11-24 16:10:23 -0500744 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
Geoff Langcec35902014-04-16 10:52:36 -0400745 }
746
Jamie Madilla3944d42016-07-22 22:13:26 -0400747 if (format.info->stencilBits == 0)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000748 {
749 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
750 }
751 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400752 else if (stencilAttachment.type() == GL_RENDERBUFFER)
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000753 {
Jamie Madilla3944d42016-07-22 22:13:26 -0400754 if (!formatCaps.renderable || format.info->stencilBits == 0)
Jamie Madillbb94f342014-06-23 15:23:02 -0400755 {
756 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
757 }
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000758 }
759
760 if (missingAttachment)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000761 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400762 samples = stencilAttachment.getSamples();
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000763 missingAttachment = false;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000764 }
Jamie Madill2d06b732015-04-20 12:53:28 -0400765 else if (samples != stencilAttachment.getSamples())
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000766 {
Sami Väisänena797e062016-05-12 15:23:40 +0300767 // see the comments in depth attachment check.
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700768 const bool mixedSamples = state.getExtensions().framebufferMixedSamples;
Sami Väisänena797e062016-05-12 15:23:40 +0300769 if (!mixedSamples)
770 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
771
772 const int colorSamples = samples ? samples : 1;
773 const int stencilSamples = stencilAttachment.getSamples();
774 if ((stencilSamples % colorSamples) != 0)
775 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000776 }
Corentin Wallez086d59a2016-04-29 09:06:49 -0400777
778 // Starting from ES 3.0 stencil and depth, if present, should be the same image
Martin Radev1be913c2016-07-11 17:59:16 +0300779 if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() &&
Corentin Wallez086d59a2016-04-29 09:06:49 -0400780 stencilAttachment != depthAttachment)
781 {
782 return GL_FRAMEBUFFER_UNSUPPORTED;
783 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000784 }
785
Jamie Madilla02315b2017-02-23 14:14:47 -0500786 // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL.
787 if (state.isWebGL1())
788 {
789 if (!mState.mWebGLDepthStencilConsistent)
790 {
791 return GL_FRAMEBUFFER_UNSUPPORTED;
792 }
793
794 if (mState.mWebGLDepthStencilAttachment.isAttached())
795 {
796 if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 ||
797 mState.mWebGLDepthStencilAttachment.getStencilSize() == 0)
798 {
799 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
800 }
801 }
802 else if (mState.mStencilAttachment.isAttached() &&
803 mState.mStencilAttachment.getDepthSize() > 0)
804 {
805 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
806 }
807 else if (mState.mDepthAttachment.isAttached() &&
808 mState.mDepthAttachment.getStencilSize() > 0)
809 {
810 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
811 }
812 }
813
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000814 // we need to have at least one attachment to be complete
815 if (missingAttachment)
816 {
817 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000818 }
819
Jamie Madillcc86d642015-11-24 13:00:07 -0500820 // In ES 2.0, all color attachments must have the same width and height.
821 // In ES 3.0, there is no such restriction.
Martin Radev1be913c2016-07-11 17:59:16 +0300822 if (state.getClientMajorVersion() < 3 && !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -0500823 {
824 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
825 }
826
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500827 syncState();
Jamie Madillcc86d642015-11-24 13:00:07 -0500828 if (!mImpl->checkStatus())
829 {
830 return GL_FRAMEBUFFER_UNSUPPORTED;
831 }
832
833 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000835
Austin Kinross08332632015-05-05 13:35:47 -0700836Error Framebuffer::discard(size_t count, const GLenum *attachments)
837{
838 return mImpl->discard(count, attachments);
839}
840
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500841Error Framebuffer::invalidate(size_t count, const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400842{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500843 return mImpl->invalidate(count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400844}
845
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500846Error Framebuffer::invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -0400847{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500848 return mImpl->invalidateSub(count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -0400849}
850
Jamie Madill8415b5f2016-04-26 13:41:39 -0400851Error Framebuffer::clear(rx::ContextImpl *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -0500852{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700853 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500854 {
Jamie Madill362876b2016-06-16 14:46:59 -0400855 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500856 }
857
Jamie Madill8415b5f2016-04-26 13:41:39 -0400858 return mImpl->clear(context, mask);
Geoff Langb04dc822014-12-01 12:02:02 -0500859}
860
Jamie Madill8415b5f2016-04-26 13:41:39 -0400861Error Framebuffer::clearBufferfv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400862 GLenum buffer,
863 GLint drawbuffer,
864 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500865{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700866 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500867 {
Jamie Madill362876b2016-06-16 14:46:59 -0400868 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500869 }
870
Jamie Madill8415b5f2016-04-26 13:41:39 -0400871 return mImpl->clearBufferfv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500872}
873
Jamie Madill8415b5f2016-04-26 13:41:39 -0400874Error Framebuffer::clearBufferuiv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400875 GLenum buffer,
876 GLint drawbuffer,
877 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500878{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700879 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500880 {
Jamie Madill362876b2016-06-16 14:46:59 -0400881 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500882 }
883
Jamie Madill8415b5f2016-04-26 13:41:39 -0400884 return mImpl->clearBufferuiv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500885}
886
Jamie Madill8415b5f2016-04-26 13:41:39 -0400887Error Framebuffer::clearBufferiv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400888 GLenum buffer,
889 GLint drawbuffer,
890 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500891{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700892 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500893 {
Jamie Madill362876b2016-06-16 14:46:59 -0400894 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500895 }
896
Jamie Madill8415b5f2016-04-26 13:41:39 -0400897 return mImpl->clearBufferiv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500898}
899
Jamie Madill8415b5f2016-04-26 13:41:39 -0400900Error Framebuffer::clearBufferfi(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400901 GLenum buffer,
902 GLint drawbuffer,
903 GLfloat depth,
904 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -0500905{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700906 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500907 {
Jamie Madill362876b2016-06-16 14:46:59 -0400908 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500909 }
910
Jamie Madill8415b5f2016-04-26 13:41:39 -0400911 return mImpl->clearBufferfi(context, buffer, drawbuffer, depth, stencil);
Geoff Langb04dc822014-12-01 12:02:02 -0500912}
913
Geoff Langbce529e2014-12-01 12:48:41 -0500914GLenum Framebuffer::getImplementationColorReadFormat() const
915{
916 return mImpl->getImplementationColorReadFormat();
917}
918
919GLenum Framebuffer::getImplementationColorReadType() const
920{
921 return mImpl->getImplementationColorReadType();
922}
923
Jamie Madill8415b5f2016-04-26 13:41:39 -0400924Error Framebuffer::readPixels(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500925 const Rectangle &area,
Jamie Madill1b94d432015-08-07 13:23:23 -0400926 GLenum format,
927 GLenum type,
928 GLvoid *pixels) const
Geoff Langbce529e2014-12-01 12:48:41 -0500929{
Jamie Madill362876b2016-06-16 14:46:59 -0400930 ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels));
Geoff Lang520c4ae2015-05-05 13:12:36 -0400931
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700932 Buffer *unpackBuffer = context->getGLState().getUnpackState().pixelBuffer.get();
Geoff Lang520c4ae2015-05-05 13:12:36 -0400933 if (unpackBuffer)
934 {
935 unpackBuffer->onPixelUnpack();
936 }
937
Jamie Madill362876b2016-06-16 14:46:59 -0400938 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -0500939}
940
Jamie Madill8415b5f2016-04-26 13:41:39 -0400941Error Framebuffer::blit(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500942 const Rectangle &sourceArea,
943 const Rectangle &destArea,
Geoff Lang242468f2015-09-24 14:15:41 -0400944 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -0400945 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -0500946{
He Yunchao6be602d2016-12-22 14:33:07 +0800947 GLbitfield blitMask = mask;
948
949 // Note that blitting is called against draw framebuffer.
950 // See the code in gl::Context::blitFramebuffer.
951 if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer())
952 {
953 blitMask &= ~GL_COLOR_BUFFER_BIT;
954 }
955
956 if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr)
957 {
958 blitMask &= ~GL_STENCIL_BUFFER_BIT;
959 }
960
961 if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr)
962 {
963 blitMask &= ~GL_DEPTH_BUFFER_BIT;
964 }
965
966 if (!blitMask)
967 {
968 return NoError();
969 }
970
971 return mImpl->blit(context, sourceArea, destArea, blitMask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -0500972}
973
Jamie Madill51f40ec2016-06-15 14:06:00 -0400974int Framebuffer::getSamples(const ContextState &state)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000975{
Jamie Madill362876b2016-06-16 14:46:59 -0400976 if (complete(state))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000977 {
Jamie Madill362876b2016-06-16 14:46:59 -0400978 // For a complete framebuffer, all attachments must have the same sample count.
979 // In this case return the first nonzero sample size.
980 const auto *firstColorAttachment = mState.getFirstColorAttachment();
981 if (firstColorAttachment)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000982 {
Jamie Madill362876b2016-06-16 14:46:59 -0400983 ASSERT(firstColorAttachment->isAttached());
984 return firstColorAttachment->getSamples();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000985 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000986 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000987
988 return 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000989}
990
Corentin Wallezccab69d2017-01-27 16:57:15 -0500991Error Framebuffer::getSamplePosition(size_t index, GLfloat *xy) const
992{
993 ANGLE_TRY(mImpl->getSamplePosition(index, xy));
994 return gl::NoError();
995}
996
Jamie Madille261b442014-06-25 12:42:21 -0400997bool Framebuffer::hasValidDepthStencil() const
998{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400999 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -04001000}
1001
Jamie Madilla02315b2017-02-23 14:14:47 -05001002void Framebuffer::setAttachment(const Context *context,
1003 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -04001004 GLenum binding,
1005 const ImageIndex &textureIndex,
1006 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -04001007{
Jamie Madilla02315b2017-02-23 14:14:47 -05001008 // Context may be null in unit tests.
1009 if (!context || !context->isWebGL1())
1010 {
1011 setAttachmentImpl(type, binding, textureIndex, resource);
1012 return;
1013 }
1014
1015 switch (binding)
1016 {
1017 case GL_DEPTH_STENCIL:
1018 case GL_DEPTH_STENCIL_ATTACHMENT:
1019 mState.mWebGLDepthStencilAttachment.attach(type, binding, textureIndex, resource);
1020 break;
1021 case GL_DEPTH:
1022 case GL_DEPTH_ATTACHMENT:
1023 mState.mWebGLDepthAttachment.attach(type, binding, textureIndex, resource);
1024 break;
1025 case GL_STENCIL:
1026 case GL_STENCIL_ATTACHMENT:
1027 mState.mWebGLStencilAttachment.attach(type, binding, textureIndex, resource);
1028 break;
1029 default:
1030 setAttachmentImpl(type, binding, textureIndex, resource);
1031 return;
1032 }
1033
1034 commitWebGL1DepthStencilIfConsistent();
1035}
1036
1037void Framebuffer::commitWebGL1DepthStencilIfConsistent()
1038{
1039 int count = 0;
1040
1041 std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment,
1042 &mState.mWebGLDepthAttachment,
1043 &mState.mWebGLStencilAttachment}};
1044 for (FramebufferAttachment *attachment : attachments)
1045 {
1046 if (attachment->isAttached())
1047 {
1048 count++;
1049 }
1050 }
1051
1052 mState.mWebGLDepthStencilConsistent = (count <= 1);
1053 if (!mState.mWebGLDepthStencilConsistent)
1054 {
1055 // Inconsistent.
1056 return;
1057 }
1058
1059 if (mState.mWebGLDepthAttachment.isAttached())
1060 {
1061 const auto &depth = mState.mWebGLDepthAttachment;
1062 setAttachmentImpl(depth.type(), GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(),
1063 depth.getResource());
1064 setAttachmentImpl(GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
1065 }
1066 else if (mState.mWebGLStencilAttachment.isAttached())
1067 {
1068 const auto &stencil = mState.mWebGLStencilAttachment;
1069 setAttachmentImpl(GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
1070 setAttachmentImpl(stencil.type(), GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(),
1071 stencil.getResource());
1072 }
1073 else if (mState.mWebGLDepthStencilAttachment.isAttached())
1074 {
1075 const auto &depthStencil = mState.mWebGLDepthStencilAttachment;
1076 setAttachmentImpl(depthStencil.type(), GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(),
1077 depthStencil.getResource());
1078 setAttachmentImpl(depthStencil.type(), GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(),
1079 depthStencil.getResource());
1080 }
1081 else
1082 {
1083 setAttachmentImpl(GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
1084 setAttachmentImpl(GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
1085 }
1086}
1087
1088void Framebuffer::setAttachmentImpl(GLenum type,
1089 GLenum binding,
1090 const ImageIndex &textureIndex,
1091 FramebufferAttachmentObject *resource)
1092{
Jamie Madill2d06b732015-04-20 12:53:28 -04001093 if (binding == GL_DEPTH_STENCIL || binding == GL_DEPTH_STENCIL_ATTACHMENT)
Geoff Langab75a052014-10-15 12:56:37 -04001094 {
Geoff Langab75a052014-10-15 12:56:37 -04001095 // ensure this is a legitimate depth+stencil format
Jamie Madill375c37c2015-07-21 15:14:08 -04001096 FramebufferAttachmentObject *attachmentObj = resource;
1097 if (resource)
Geoff Langab75a052014-10-15 12:56:37 -04001098 {
Jamie Madill375c37c2015-07-21 15:14:08 -04001099 FramebufferAttachment::Target target(binding, textureIndex);
Jamie Madilla3944d42016-07-22 22:13:26 -04001100 const Format &format = resource->getAttachmentFormat(target);
1101 if (format.info->depthBits == 0 || format.info->stencilBits == 0)
Jamie Madill375c37c2015-07-21 15:14:08 -04001102 {
1103 // Attaching nullptr detaches the current attachment.
1104 attachmentObj = nullptr;
1105 }
Geoff Langab75a052014-10-15 12:56:37 -04001106 }
Jamie Madill375c37c2015-07-21 15:14:08 -04001107
Jamie Madill48ef11b2016-04-27 15:21:52 -04001108 mState.mDepthAttachment.attach(type, binding, textureIndex, attachmentObj);
1109 mState.mStencilAttachment.attach(type, binding, textureIndex, attachmentObj);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001110 mDirtyBits.set(DIRTY_BIT_DEPTH_ATTACHMENT);
1111 mDirtyBits.set(DIRTY_BIT_STENCIL_ATTACHMENT);
Jamie Madill362876b2016-06-16 14:46:59 -04001112 BindResourceChannel(&mDirtyDepthAttachmentBinding, resource);
1113 BindResourceChannel(&mDirtyStencilAttachmentBinding, resource);
Jamie Madilla02315b2017-02-23 14:14:47 -05001114 return;
Geoff Langab75a052014-10-15 12:56:37 -04001115 }
Jamie Madilla4595b82017-01-11 17:36:34 -05001116
Jamie Madilla02315b2017-02-23 14:14:47 -05001117 switch (binding)
1118 {
1119 case GL_DEPTH:
1120 case GL_DEPTH_ATTACHMENT:
1121 mState.mDepthAttachment.attach(type, binding, textureIndex, resource);
1122 mDirtyBits.set(DIRTY_BIT_DEPTH_ATTACHMENT);
1123 BindResourceChannel(&mDirtyDepthAttachmentBinding, resource);
Jamie Madill2d06b732015-04-20 12:53:28 -04001124 break;
Jamie Madilla02315b2017-02-23 14:14:47 -05001125 case GL_STENCIL:
1126 case GL_STENCIL_ATTACHMENT:
1127 mState.mStencilAttachment.attach(type, binding, textureIndex, resource);
1128 mDirtyBits.set(DIRTY_BIT_STENCIL_ATTACHMENT);
1129 BindResourceChannel(&mDirtyStencilAttachmentBinding, resource);
1130 break;
1131 case GL_BACK:
1132 mState.mColorAttachments[0].attach(type, binding, textureIndex, resource);
1133 mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0);
1134 // No need for a resource binding for the default FBO, it's always complete.
1135 break;
1136 default:
1137 {
1138 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
1139 ASSERT(colorIndex < mState.mColorAttachments.size());
1140 mState.mColorAttachments[colorIndex].attach(type, binding, textureIndex, resource);
1141 mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
1142 BindResourceChannel(&mDirtyColorAttachmentBindings[colorIndex], resource);
1143
1144 bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE);
1145 mState.mEnabledDrawBuffers.set(colorIndex, enabled);
Jamie Madill2d06b732015-04-20 12:53:28 -04001146 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001147 break;
Geoff Langab75a052014-10-15 12:56:37 -04001148 }
1149}
1150
Jamie Madilla02315b2017-02-23 14:14:47 -05001151void Framebuffer::resetAttachment(const Context *context, GLenum binding)
Jamie Madill2d06b732015-04-20 12:53:28 -04001152{
Jamie Madilla02315b2017-02-23 14:14:47 -05001153 setAttachment(context, GL_NONE, binding, ImageIndex::MakeInvalid(), nullptr);
Jamie Madill2d06b732015-04-20 12:53:28 -04001154}
1155
Jamie Madill362876b2016-06-16 14:46:59 -04001156void Framebuffer::syncState()
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001157{
1158 if (mDirtyBits.any())
1159 {
1160 mImpl->syncState(mDirtyBits);
1161 mDirtyBits.reset();
Corentin Wallezccab69d2017-01-27 16:57:15 -05001162 if (mId != 0)
1163 {
1164 mCachedStatus.reset();
1165 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001166 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001167}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001168
Jamie Madill362876b2016-06-16 14:46:59 -04001169void Framebuffer::signal(SignalToken token)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001170{
Jamie Madill362876b2016-06-16 14:46:59 -04001171 // TOOD(jmadill): Make this only update individual attachments to do less work.
1172 mCachedStatus.reset();
Jamie Madill51f40ec2016-06-15 14:06:00 -04001173}
1174
Jamie Madill362876b2016-06-16 14:46:59 -04001175bool Framebuffer::complete(const ContextState &state)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001176{
Jamie Madill362876b2016-06-16 14:46:59 -04001177 return (checkStatus(state) == GL_FRAMEBUFFER_COMPLETE);
Jamie Madill51f40ec2016-06-15 14:06:00 -04001178}
1179
Jamie Madilla4595b82017-01-11 17:36:34 -05001180bool Framebuffer::formsRenderingFeedbackLoopWith(const State &state) const
1181{
1182 const Program *program = state.getProgram();
1183
1184 // TODO(jmadill): Default framebuffer feedback loops.
1185 if (mId == 0)
1186 {
1187 return false;
1188 }
1189
1190 // The bitset will skip inactive draw buffers.
1191 for (GLuint drawIndex : angle::IterateBitSet(mState.mEnabledDrawBuffers))
1192 {
1193 const FramebufferAttachment *attachment = getDrawBuffer(drawIndex);
1194 if (attachment && attachment->type() == GL_TEXTURE)
1195 {
1196 // Validate the feedback loop.
1197 if (program->samplesFromTexture(state, attachment->id()))
1198 {
1199 return true;
1200 }
1201 }
1202 }
1203
Jamie Madill1d37bc52017-02-02 19:59:58 -05001204 // Validate depth-stencil feedback loop.
1205 const auto &dsState = state.getDepthStencilState();
1206
1207 // We can skip the feedback loop checks if depth/stencil is masked out or disabled.
1208 const FramebufferAttachment *depth = getDepthbuffer();
1209 if (depth && depth->type() == GL_TEXTURE && dsState.depthTest && dsState.depthMask)
1210 {
1211 if (program->samplesFromTexture(state, depth->id()))
1212 {
1213 return true;
1214 }
1215 }
1216
1217 // Note: we assume the front and back masks are the same for WebGL.
1218 const FramebufferAttachment *stencil = getStencilbuffer();
1219 ASSERT(dsState.stencilBackWritemask == dsState.stencilWritemask);
1220 if (stencil && stencil->type() == GL_TEXTURE && dsState.stencilTest &&
1221 dsState.stencilWritemask != 0)
1222 {
1223 // Skip the feedback loop check if depth/stencil point to the same resource.
1224 if (!depth || *stencil != *depth)
1225 {
1226 if (program->samplesFromTexture(state, stencil->id()))
1227 {
1228 return true;
1229 }
1230 }
1231 }
1232
Jamie Madilla4595b82017-01-11 17:36:34 -05001233 return false;
1234}
1235
Jamie Madillfd3dd432017-02-02 19:59:59 -05001236bool Framebuffer::formsCopyingFeedbackLoopWith(GLuint copyTextureID,
1237 GLint copyTextureLevel,
1238 GLint copyTextureLayer) const
Jamie Madillf695a3a2017-01-11 17:36:35 -05001239{
1240 if (mId == 0)
1241 {
1242 // It seems impossible to form a texture copying feedback loop with the default FBO.
1243 return false;
1244 }
1245
1246 const FramebufferAttachment *readAttachment = getReadColorbuffer();
1247 ASSERT(readAttachment);
1248
1249 if (readAttachment->isTextureWithId(copyTextureID))
1250 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001251 const auto &imageIndex = readAttachment->getTextureImageIndex();
1252 if (imageIndex.mipIndex == copyTextureLevel)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001253 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001254 // Check 3D/Array texture layers.
1255 return imageIndex.layerIndex == ImageIndex::ENTIRE_LEVEL ||
1256 copyTextureLayer == ImageIndex::ENTIRE_LEVEL ||
1257 imageIndex.layerIndex == copyTextureLayer;
Jamie Madillf695a3a2017-01-11 17:36:35 -05001258 }
1259 }
1260 return false;
1261}
1262
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001263GLint Framebuffer::getDefaultWidth() const
1264{
1265 return mState.getDefaultWidth();
1266}
1267
1268GLint Framebuffer::getDefaultHeight() const
1269{
1270 return mState.getDefaultHeight();
1271}
1272
1273GLint Framebuffer::getDefaultSamples() const
1274{
1275 return mState.getDefaultSamples();
1276}
1277
1278GLboolean Framebuffer::getDefaultFixedSampleLocations() const
1279{
1280 return mState.getDefaultFixedSampleLocations();
1281}
1282
1283void Framebuffer::setDefaultWidth(GLint defaultWidth)
1284{
1285 mState.mDefaultWidth = defaultWidth;
1286 mDirtyBits.set(DIRTY_BIT_DEFAULT_WIDTH);
1287}
1288
1289void Framebuffer::setDefaultHeight(GLint defaultHeight)
1290{
1291 mState.mDefaultHeight = defaultHeight;
1292 mDirtyBits.set(DIRTY_BIT_DEFAULT_HEIGHT);
1293}
1294
1295void Framebuffer::setDefaultSamples(GLint defaultSamples)
1296{
1297 mState.mDefaultSamples = defaultSamples;
1298 mDirtyBits.set(DIRTY_BIT_DEFAULT_SAMPLES);
1299}
1300
1301void Framebuffer::setDefaultFixedSampleLocations(GLboolean defaultFixedSampleLocations)
1302{
1303 mState.mDefaultFixedSampleLocations = defaultFixedSampleLocations;
1304 mDirtyBits.set(DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS);
1305}
1306
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001307} // namespace gl