blob: 56ac49e9c3503f0d4e19967dd5fddb502181a060 [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 Madill231c7f52017-04-26 13:45:37 -040013#include "common/bitset_utils.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
Jamie Madill1e5499d2017-04-05 11:22:16 -040037void BindResourceChannel(OnAttachmentDirtyBinding *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
Geoff Lang9f10b772017-05-16 15:51:03 -040042bool CheckAttachmentCompleteness(const Context *context, const FramebufferAttachment &attachment)
43{
44 ASSERT(attachment.isAttached());
45
46 const Extents &size = attachment.getSize();
47 if (size.width == 0 || size.height == 0)
48 {
49 return false;
50 }
51
52 const InternalFormat &format = *attachment.getFormat().info;
53 if (!format.renderSupport(context->getClientVersion(), context->getExtensions()))
54 {
55 return false;
56 }
57
58 if (attachment.type() == GL_TEXTURE)
59 {
60 if (attachment.layer() >= size.depth)
61 {
62 return false;
63 }
64
65 // ES3 specifies that cube map texture attachments must be cube complete.
66 // This language is missing from the ES2 spec, but we enforce it here because some
67 // desktop OpenGL drivers also enforce this validation.
68 // TODO(jmadill): Check if OpenGL ES2 drivers enforce cube completeness.
69 const Texture *texture = attachment.getTexture();
70 ASSERT(texture);
71 if (texture->getTarget() == GL_TEXTURE_CUBE_MAP &&
72 !texture->getTextureState().isCubeComplete())
73 {
74 return false;
75 }
Geoff Lang857c09d2017-05-16 15:55:04 -040076
77 if (!texture->getImmutableFormat())
78 {
79 GLuint attachmentMipLevel = static_cast<GLuint>(attachment.mipLevel());
80
81 // From the ES 3.0 spec, pg 213:
82 // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of
83 // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture,
84 // then the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL must be in the
85 // range[levelbase, q], where levelbase is the value of TEXTURE_BASE_LEVEL and q is
86 // the effective maximum texture level defined in the Mipmapping discussion of
87 // section 3.8.10.4.
88 if (attachmentMipLevel < texture->getBaseLevel() ||
89 attachmentMipLevel > texture->getMipmapMaxLevel())
90 {
91 return false;
92 }
93
94 // Form the ES 3.0 spec, pg 213/214:
95 // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of
96 // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture and
97 // the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL is not levelbase, then the
98 // texture must be mipmap complete, and if FRAMEBUFFER_ATTACHMENT_OBJECT_NAME names
99 // a cubemap texture, the texture must also be cube complete.
100 if (attachmentMipLevel != texture->getBaseLevel() && !texture->isMipmapComplete())
101 {
102 return false;
103 }
104 }
Geoff Lang9f10b772017-05-16 15:51:03 -0400105 }
106
107 return true;
108};
109
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400110bool CheckAttachmentSampleCompleteness(const Context *context,
111 const FramebufferAttachment &attachment,
112 bool colorAttachment,
113 Optional<int> *samples,
114 Optional<GLboolean> *fixedSampleLocations)
115{
116 ASSERT(attachment.isAttached());
117
118 if (attachment.type() == GL_TEXTURE)
119 {
120 const Texture *texture = attachment.getTexture();
121 ASSERT(texture);
122
123 const ImageIndex &attachmentImageIndex = attachment.getTextureImageIndex();
124
125 // ES3.1 (section 9.4) requires that the value of TEXTURE_FIXED_SAMPLE_LOCATIONS should be
126 // the same for all attached textures.
127 GLboolean fixedSampleloc = texture->getFixedSampleLocations(attachmentImageIndex.type,
128 attachmentImageIndex.mipIndex);
129 if (fixedSampleLocations->valid() && fixedSampleloc != fixedSampleLocations->value())
130 {
131 return false;
132 }
133 else
134 {
135 *fixedSampleLocations = fixedSampleloc;
136 }
137 }
138
139 if (samples->valid())
140 {
141 if (attachment.getSamples() != samples->value())
142 {
143 if (colorAttachment)
144 {
145 // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that
146 // all color attachments have the same number of samples for the FBO to be complete.
147 return false;
148 }
149 else
150 {
151 // CHROMIUM_framebuffer_mixed_samples allows a framebuffer to be considered complete
152 // when its depth or stencil samples are a multiple of the number of color samples.
153 if (!context->getExtensions().framebufferMixedSamples)
154 {
155 return false;
156 }
157
158 if ((attachment.getSamples() % std::max(samples->value(), 1)) != 0)
159 {
160 return false;
161 }
162 }
163 }
164 }
165 else
166 {
167 *samples = attachment.getSamples();
168 }
169
170 return true;
171}
172
Jamie Madill362876b2016-06-16 14:46:59 -0400173} // anonymous namespace
Jamie Madilld1405e52015-03-05 15:41:39 -0500174
Jamie Madill6f60d052017-02-22 15:20:11 -0500175// This constructor is only used for default framebuffers.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400176FramebufferState::FramebufferState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500177 : mLabel(),
178 mColorAttachments(1),
Geoff Langd90d3882017-03-21 10:49:54 -0400179 mDrawBufferStates(IMPLEMENTATION_MAX_DRAW_BUFFERS, GL_NONE),
Jamie Madill6f60d052017-02-22 15:20:11 -0500180 mReadBufferState(GL_BACK),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800181 mDefaultWidth(0),
182 mDefaultHeight(0),
183 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500184 mDefaultFixedSampleLocations(GL_FALSE),
185 mWebGLDepthStencilConsistent(true)
Corentin Wallez37c39792015-08-20 14:19:46 -0400186{
Geoff Langd90d3882017-03-21 10:49:54 -0400187 ASSERT(mDrawBufferStates.size() > 0);
188 mDrawBufferStates[0] = GL_BACK;
Jamie Madilla4595b82017-01-11 17:36:34 -0500189 mEnabledDrawBuffers.set(0);
Corentin Wallez37c39792015-08-20 14:19:46 -0400190}
191
Jamie Madill48ef11b2016-04-27 15:21:52 -0400192FramebufferState::FramebufferState(const Caps &caps)
Geoff Lang70d0f492015-12-10 17:45:46 -0500193 : mLabel(),
194 mColorAttachments(caps.maxColorAttachments),
Jamie Madilld1405e52015-03-05 15:41:39 -0500195 mDrawBufferStates(caps.maxDrawBuffers, GL_NONE),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800196 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT),
197 mDefaultWidth(0),
198 mDefaultHeight(0),
199 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500200 mDefaultFixedSampleLocations(GL_FALSE),
201 mWebGLDepthStencilConsistent(true)
Jamie Madilld1405e52015-03-05 15:41:39 -0500202{
Geoff Langa15472a2015-08-11 11:48:03 -0400203 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilld1405e52015-03-05 15:41:39 -0500204 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
205}
206
Jamie Madill48ef11b2016-04-27 15:21:52 -0400207FramebufferState::~FramebufferState()
Jamie Madilld1405e52015-03-05 15:41:39 -0500208{
Jamie Madilld1405e52015-03-05 15:41:39 -0500209}
210
Jamie Madill48ef11b2016-04-27 15:21:52 -0400211const std::string &FramebufferState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500212{
213 return mLabel;
214}
215
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400216const FramebufferAttachment *FramebufferState::getAttachment(GLenum attachment) const
217{
218 if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15)
219 {
220 return getColorAttachment(attachment - GL_COLOR_ATTACHMENT0);
221 }
222
223 switch (attachment)
224 {
225 case GL_COLOR:
226 case GL_BACK:
227 return getColorAttachment(0);
228 case GL_DEPTH:
229 case GL_DEPTH_ATTACHMENT:
230 return getDepthAttachment();
231 case GL_STENCIL:
232 case GL_STENCIL_ATTACHMENT:
233 return getStencilAttachment();
234 case GL_DEPTH_STENCIL:
235 case GL_DEPTH_STENCIL_ATTACHMENT:
236 return getDepthStencilAttachment();
237 default:
238 UNREACHABLE();
239 return nullptr;
240 }
241}
242
Jamie Madill48ef11b2016-04-27 15:21:52 -0400243const FramebufferAttachment *FramebufferState::getReadAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500244{
Antoine Labour2ec65dc2016-11-30 16:28:58 -0800245 if (mReadBufferState == GL_NONE)
246 {
247 return nullptr;
248 }
Jamie Madill231c7f52017-04-26 13:45:37 -0400249 ASSERT(mReadBufferState == GL_BACK ||
250 (mReadBufferState >= GL_COLOR_ATTACHMENT0 && mReadBufferState <= GL_COLOR_ATTACHMENT15));
251 size_t readIndex = (mReadBufferState == GL_BACK
252 ? 0
253 : static_cast<size_t>(mReadBufferState - GL_COLOR_ATTACHMENT0));
Jamie Madill7147f012015-03-05 15:41:40 -0500254 ASSERT(readIndex < mColorAttachments.size());
Jamie Madill2d06b732015-04-20 12:53:28 -0400255 return mColorAttachments[readIndex].isAttached() ? &mColorAttachments[readIndex] : nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500256}
257
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500258const FramebufferAttachment *FramebufferState::getFirstNonNullAttachment() const
259{
260 auto *colorAttachment = getFirstColorAttachment();
261 if (colorAttachment)
262 {
263 return colorAttachment;
264 }
265 return getDepthOrStencilAttachment();
266}
267
Jamie Madill48ef11b2016-04-27 15:21:52 -0400268const FramebufferAttachment *FramebufferState::getFirstColorAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500269{
Jamie Madill2d06b732015-04-20 12:53:28 -0400270 for (const FramebufferAttachment &colorAttachment : mColorAttachments)
Jamie Madill7147f012015-03-05 15:41:40 -0500271 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400272 if (colorAttachment.isAttached())
Jamie Madill7147f012015-03-05 15:41:40 -0500273 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400274 return &colorAttachment;
Jamie Madill7147f012015-03-05 15:41:40 -0500275 }
276 }
277
278 return nullptr;
279}
280
Jamie Madill48ef11b2016-04-27 15:21:52 -0400281const FramebufferAttachment *FramebufferState::getDepthOrStencilAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500282{
Jamie Madill2d06b732015-04-20 12:53:28 -0400283 if (mDepthAttachment.isAttached())
284 {
285 return &mDepthAttachment;
286 }
287 if (mStencilAttachment.isAttached())
288 {
289 return &mStencilAttachment;
290 }
291 return nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500292}
293
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500294const FramebufferAttachment *FramebufferState::getStencilOrDepthStencilAttachment() const
295{
296 if (mStencilAttachment.isAttached())
297 {
298 return &mStencilAttachment;
299 }
300 return getDepthStencilAttachment();
301}
302
Jamie Madill48ef11b2016-04-27 15:21:52 -0400303const FramebufferAttachment *FramebufferState::getColorAttachment(size_t colorAttachment) const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400304{
305 ASSERT(colorAttachment < mColorAttachments.size());
Jamie Madill231c7f52017-04-26 13:45:37 -0400306 return mColorAttachments[colorAttachment].isAttached() ? &mColorAttachments[colorAttachment]
307 : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400308}
309
Jamie Madill48ef11b2016-04-27 15:21:52 -0400310const FramebufferAttachment *FramebufferState::getDepthAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400311{
Jamie Madill2d06b732015-04-20 12:53:28 -0400312 return mDepthAttachment.isAttached() ? &mDepthAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400313}
314
Jamie Madill48ef11b2016-04-27 15:21:52 -0400315const FramebufferAttachment *FramebufferState::getStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400316{
Jamie Madill2d06b732015-04-20 12:53:28 -0400317 return mStencilAttachment.isAttached() ? &mStencilAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400318}
319
Jamie Madill48ef11b2016-04-27 15:21:52 -0400320const FramebufferAttachment *FramebufferState::getDepthStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400321{
322 // A valid depth-stencil attachment has the same resource bound to both the
323 // depth and stencil attachment points.
Jamie Madill2d06b732015-04-20 12:53:28 -0400324 if (mDepthAttachment.isAttached() && mStencilAttachment.isAttached() &&
Jamie Madill44f26482016-11-18 12:49:15 -0500325 mDepthAttachment == mStencilAttachment)
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400326 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400327 return &mDepthAttachment;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400328 }
329
330 return nullptr;
331}
332
Jamie Madill48ef11b2016-04-27 15:21:52 -0400333bool FramebufferState::attachmentsHaveSameDimensions() const
Jamie Madillcc86d642015-11-24 13:00:07 -0500334{
335 Optional<Extents> attachmentSize;
336
Jamie Madill231c7f52017-04-26 13:45:37 -0400337 auto hasMismatchedSize = [&attachmentSize](const FramebufferAttachment &attachment) {
Jamie Madillcc86d642015-11-24 13:00:07 -0500338 if (!attachment.isAttached())
339 {
340 return false;
341 }
342
343 if (!attachmentSize.valid())
344 {
345 attachmentSize = attachment.getSize();
346 return false;
347 }
348
349 return (attachment.getSize() != attachmentSize.value());
350 };
351
352 for (const auto &attachment : mColorAttachments)
353 {
354 if (hasMismatchedSize(attachment))
355 {
356 return false;
357 }
358 }
359
360 if (hasMismatchedSize(mDepthAttachment))
361 {
362 return false;
363 }
364
365 return !hasMismatchedSize(mStencilAttachment);
366}
367
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400368const gl::FramebufferAttachment *FramebufferState::getDrawBuffer(size_t drawBufferIdx) const
369{
370 ASSERT(drawBufferIdx < mDrawBufferStates.size());
371 if (mDrawBufferStates[drawBufferIdx] != GL_NONE)
372 {
373 // ES3 spec: "If the GL is bound to a draw framebuffer object, the ith buffer listed in bufs
374 // must be COLOR_ATTACHMENTi or NONE"
375 ASSERT(mDrawBufferStates[drawBufferIdx] == GL_COLOR_ATTACHMENT0 + drawBufferIdx ||
376 (drawBufferIdx == 0 && mDrawBufferStates[drawBufferIdx] == GL_BACK));
377 return getAttachment(mDrawBufferStates[drawBufferIdx]);
378 }
379 else
380 {
381 return nullptr;
382 }
383}
384
385size_t FramebufferState::getDrawBufferCount() const
386{
387 return mDrawBufferStates.size();
388}
389
Geoff Langb21e20d2016-07-19 15:35:41 -0400390bool FramebufferState::colorAttachmentsAreUniqueImages() const
391{
392 for (size_t firstAttachmentIdx = 0; firstAttachmentIdx < mColorAttachments.size();
393 firstAttachmentIdx++)
394 {
395 const gl::FramebufferAttachment &firstAttachment = mColorAttachments[firstAttachmentIdx];
396 if (!firstAttachment.isAttached())
397 {
398 continue;
399 }
400
401 for (size_t secondAttachmentIdx = firstAttachmentIdx + 1;
402 secondAttachmentIdx < mColorAttachments.size(); secondAttachmentIdx++)
403 {
404 const gl::FramebufferAttachment &secondAttachment =
405 mColorAttachments[secondAttachmentIdx];
406 if (!secondAttachment.isAttached())
407 {
408 continue;
409 }
410
411 if (firstAttachment == secondAttachment)
412 {
413 return false;
414 }
415 }
416 }
417
418 return true;
419}
420
Jamie Madill7aea7e02016-05-10 10:39:45 -0400421Framebuffer::Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id)
Jamie Madill362876b2016-06-16 14:46:59 -0400422 : mState(caps),
423 mImpl(factory->createFramebuffer(mState)),
424 mId(id),
425 mCachedStatus(),
426 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
427 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428{
Corentin Wallez37c39792015-08-20 14:19:46 -0400429 ASSERT(mId != 0);
430 ASSERT(mImpl != nullptr);
Jamie Madill362876b2016-06-16 14:46:59 -0400431 ASSERT(mState.mColorAttachments.size() == static_cast<size_t>(caps.maxColorAttachments));
432
Jamie Madill1e5499d2017-04-05 11:22:16 -0400433 for (uint32_t colorIndex = 0;
434 colorIndex < static_cast<uint32_t>(mState.mColorAttachments.size()); ++colorIndex)
Jamie Madill362876b2016-06-16 14:46:59 -0400435 {
Jamie Madill1e5499d2017-04-05 11:22:16 -0400436 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
Jamie Madill362876b2016-06-16 14:46:59 -0400437 }
Corentin Wallez37c39792015-08-20 14:19:46 -0400438}
439
Jamie Madill6f60d052017-02-22 15:20:11 -0500440Framebuffer::Framebuffer(egl::Surface *surface)
Jamie Madill362876b2016-06-16 14:46:59 -0400441 : mState(),
Jamie Madill6f60d052017-02-22 15:20:11 -0500442 mImpl(surface->getImplementation()->createDefaultFramebuffer(mState)),
Jamie Madill362876b2016-06-16 14:46:59 -0400443 mId(0),
444 mCachedStatus(GL_FRAMEBUFFER_COMPLETE),
445 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
446 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
Corentin Wallez37c39792015-08-20 14:19:46 -0400447{
Geoff Langda88add2014-12-01 10:22:01 -0500448 ASSERT(mImpl != nullptr);
Jamie Madill1e5499d2017-04-05 11:22:16 -0400449 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Jamie Madill6f60d052017-02-22 15:20:11 -0500450
Jamie Madilla02315b2017-02-23 14:14:47 -0500451 setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_BACK, gl::ImageIndex::MakeInvalid(), surface);
Jamie Madill6f60d052017-02-22 15:20:11 -0500452
453 if (surface->getConfig()->depthSize > 0)
454 {
Jamie Madilla02315b2017-02-23 14:14:47 -0500455 setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, gl::ImageIndex::MakeInvalid(), surface);
Jamie Madill6f60d052017-02-22 15:20:11 -0500456 }
457
458 if (surface->getConfig()->stencilSize > 0)
459 {
Jamie Madilla02315b2017-02-23 14:14:47 -0500460 setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, gl::ImageIndex::MakeInvalid(),
461 surface);
Jamie Madill6f60d052017-02-22 15:20:11 -0500462 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000463}
464
Corentin Wallezccab69d2017-01-27 16:57:15 -0500465Framebuffer::Framebuffer(rx::GLImplFactory *factory)
466 : mState(),
467 mImpl(factory->createFramebuffer(mState)),
468 mId(0),
469 mCachedStatus(GL_FRAMEBUFFER_UNDEFINED_OES),
470 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
471 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
472{
Jamie Madill1e5499d2017-04-05 11:22:16 -0400473 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Corentin Wallezccab69d2017-01-27 16:57:15 -0500474}
475
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000476Framebuffer::~Framebuffer()
477{
Geoff Langda88add2014-12-01 10:22:01 -0500478 SafeDelete(mImpl);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000479}
480
Jamie Madill6c1f6712017-02-14 19:08:04 -0500481void Framebuffer::destroy(const Context *context)
482{
483 mImpl->destroy(rx::SafeGetImpl(context));
484}
485
486void Framebuffer::destroyDefault(const egl::Display *display)
487{
488 mImpl->destroyDefault(rx::SafeGetImpl(display));
489}
490
Geoff Lang70d0f492015-12-10 17:45:46 -0500491void Framebuffer::setLabel(const std::string &label)
492{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400493 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500494}
495
496const std::string &Framebuffer::getLabel() const
497{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400498 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500499}
500
Jamie Madilla02315b2017-02-23 14:14:47 -0500501void Framebuffer::detachTexture(const Context *context, GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502{
Jamie Madilla02315b2017-02-23 14:14:47 -0500503 detachResourceById(context, GL_TEXTURE, textureId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000504}
505
Jamie Madilla02315b2017-02-23 14:14:47 -0500506void Framebuffer::detachRenderbuffer(const Context *context, GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507{
Jamie Madilla02315b2017-02-23 14:14:47 -0500508 detachResourceById(context, GL_RENDERBUFFER, renderbufferId);
Jamie Madilld1405e52015-03-05 15:41:39 -0500509}
Jamie Madille261b442014-06-25 12:42:21 -0400510
Jamie Madilla02315b2017-02-23 14:14:47 -0500511void Framebuffer::detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId)
Jamie Madilld1405e52015-03-05 15:41:39 -0500512{
Jamie Madill362876b2016-06-16 14:46:59 -0400513 for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
Jamie Madilld1405e52015-03-05 15:41:39 -0500514 {
Jamie Madill362876b2016-06-16 14:46:59 -0400515 detachMatchingAttachment(&mState.mColorAttachments[colorIndex], resourceType, resourceId,
516 DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517 }
518
Jamie Madilla02315b2017-02-23 14:14:47 -0500519 if (context->isWebGL1())
520 {
521 const std::array<FramebufferAttachment *, 3> attachments = {
522 {&mState.mWebGLDepthStencilAttachment, &mState.mWebGLDepthAttachment,
523 &mState.mWebGLStencilAttachment}};
524 for (FramebufferAttachment *attachment : attachments)
525 {
526 if (attachment->isAttached() && attachment->type() == resourceType &&
527 attachment->id() == resourceId)
528 {
529 resetAttachment(context, attachment->getBinding());
530 }
531 }
532 }
533 else
534 {
535 detachMatchingAttachment(&mState.mDepthAttachment, resourceType, resourceId,
536 DIRTY_BIT_DEPTH_ATTACHMENT);
537 detachMatchingAttachment(&mState.mStencilAttachment, resourceType, resourceId,
538 DIRTY_BIT_STENCIL_ATTACHMENT);
539 }
Jamie Madill362876b2016-06-16 14:46:59 -0400540}
541
542void Framebuffer::detachMatchingAttachment(FramebufferAttachment *attachment,
543 GLenum matchType,
544 GLuint matchId,
545 size_t dirtyBit)
546{
547 if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId)
548 {
549 attachment->detach();
550 mDirtyBits.set(dirtyBit);
551 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552}
553
Corentin Wallez37c39792015-08-20 14:19:46 -0400554const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000555{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400556 return mState.getColorAttachment(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557}
558
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400559const FramebufferAttachment *Framebuffer::getDepthbuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400560{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400561 return mState.getDepthAttachment();
Geoff Lang646559f2013-08-15 11:08:15 -0400562}
563
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400564const FramebufferAttachment *Framebuffer::getStencilbuffer() const
565{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400566 return mState.getStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400567}
568
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400569const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
570{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400571 return mState.getDepthStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400572}
573
574const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000575{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400576 return mState.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000577}
578
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500579const FramebufferAttachment *Framebuffer::getStencilOrDepthStencilAttachment() const
580{
581 return mState.getStencilOrDepthStencilAttachment();
582}
583
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400584const FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000585{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400586 return mState.getReadAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000587}
588
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000589GLenum Framebuffer::getReadColorbufferType() const
590{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400591 const FramebufferAttachment *readAttachment = mState.getReadAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400592 return (readAttachment != nullptr ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000593}
594
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400595const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000596{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400597 return mState.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000598}
599
Jamie Madill2d06b732015-04-20 12:53:28 -0400600const FramebufferAttachment *Framebuffer::getAttachment(GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000601{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400602 return mState.getAttachment(attachment);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400603}
604
Geoff Langa15472a2015-08-11 11:48:03 -0400605size_t Framebuffer::getDrawbufferStateCount() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000606{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400607 return mState.mDrawBufferStates.size();
Geoff Langa15472a2015-08-11 11:48:03 -0400608}
609
610GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const
611{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400612 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
613 return mState.mDrawBufferStates[drawBuffer];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000614}
615
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500616const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const
617{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400618 return mState.getDrawBufferStates();
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500619}
620
Geoff Lang164d54e2014-12-01 10:55:33 -0500621void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000622{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400623 auto &drawStates = mState.mDrawBufferStates;
Jamie Madilld1405e52015-03-05 15:41:39 -0500624
625 ASSERT(count <= drawStates.size());
626 std::copy(buffers, buffers + count, drawStates.begin());
627 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500628 mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS);
Jamie Madilla4595b82017-01-11 17:36:34 -0500629
630 mState.mEnabledDrawBuffers.reset();
631 for (size_t index = 0; index < count; ++index)
632 {
633 if (drawStates[index] != GL_NONE && mState.mColorAttachments[index].isAttached())
634 {
635 mState.mEnabledDrawBuffers.set(index);
636 }
637 }
Geoff Lang9dd95802014-12-01 11:12:59 -0500638}
639
Geoff Langa15472a2015-08-11 11:48:03 -0400640const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const
641{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400642 return mState.getDrawBuffer(drawBuffer);
Geoff Langa15472a2015-08-11 11:48:03 -0400643}
644
645bool Framebuffer::hasEnabledDrawBuffer() const
646{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400647 for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
Geoff Langa15472a2015-08-11 11:48:03 -0400648 {
649 if (getDrawBuffer(drawbufferIdx) != nullptr)
650 {
651 return true;
652 }
653 }
654
655 return false;
656}
657
Geoff Lang9dd95802014-12-01 11:12:59 -0500658GLenum Framebuffer::getReadBufferState() const
659{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400660 return mState.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500661}
662
663void Framebuffer::setReadBuffer(GLenum buffer)
664{
Jamie Madillb885e572015-02-03 16:16:04 -0500665 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
666 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madill48ef11b2016-04-27 15:21:52 -0400667 (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size()));
668 mState.mReadBufferState = buffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500669 mDirtyBits.set(DIRTY_BIT_READ_BUFFER);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000670}
671
Corentin Wallez37c39792015-08-20 14:19:46 -0400672size_t Framebuffer::getNumColorBuffers() const
673{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400674 return mState.mColorAttachments.size();
Corentin Wallez37c39792015-08-20 14:19:46 -0400675}
676
Jamie Madill0df8fe42015-11-24 16:10:24 -0500677bool Framebuffer::hasDepth() const
678{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400679 return (mState.mDepthAttachment.isAttached() && mState.mDepthAttachment.getDepthSize() > 0);
Jamie Madill0df8fe42015-11-24 16:10:24 -0500680}
681
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000682bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000683{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400684 return (mState.mStencilAttachment.isAttached() &&
685 mState.mStencilAttachment.getStencilSize() > 0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000686}
687
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000688bool Framebuffer::usingExtendedDrawBuffers() const
689{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400690 for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000691 {
Geoff Langa15472a2015-08-11 11:48:03 -0400692 if (getDrawBuffer(drawbufferIdx) != nullptr)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000693 {
694 return true;
695 }
696 }
697
698 return false;
699}
700
Geoff Lang9aded172017-04-05 11:07:56 -0400701void Framebuffer::invalidateCompletenessCache()
702{
703 if (mId != 0)
704 {
705 mCachedStatus.reset();
706 }
707}
708
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400709GLenum Framebuffer::checkStatus(const Context *context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000710{
Corentin Wallezccab69d2017-01-27 16:57:15 -0500711 // The default framebuffer is always complete except when it is surfaceless in which
712 // case it is always unsupported. We return early because the default framebuffer may
713 // not be subject to the same rules as application FBOs. ie, it could have 0x0 size.
Geoff Lang528ce3c2014-12-01 10:44:07 -0500714 if (mId == 0)
715 {
Corentin Wallezccab69d2017-01-27 16:57:15 -0500716 ASSERT(mCachedStatus.valid());
717 ASSERT(mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE ||
718 mCachedStatus.value() == GL_FRAMEBUFFER_UNDEFINED_OES);
719 return mCachedStatus.value();
Geoff Lang528ce3c2014-12-01 10:44:07 -0500720 }
721
Jamie Madill362876b2016-06-16 14:46:59 -0400722 if (hasAnyDirtyBit() || !mCachedStatus.valid())
723 {
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400724 mCachedStatus = checkStatusImpl(context);
Jamie Madill362876b2016-06-16 14:46:59 -0400725 }
726
727 return mCachedStatus.value();
728}
729
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400730GLenum Framebuffer::checkStatusImpl(const Context *context)
Jamie Madill362876b2016-06-16 14:46:59 -0400731{
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400732 const ContextState &state = context->getContextState();
733
Jamie Madill362876b2016-06-16 14:46:59 -0400734 ASSERT(mId != 0);
735
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400736 bool hasAttachments = false;
737 Optional<unsigned int> colorbufferSize;
738 Optional<int> samples;
JiangYizhou461d9a32017-01-04 16:37:26 +0800739 Optional<GLboolean> fixedSampleLocations;
740 bool hasRenderbuffer = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741
Jamie Madill48ef11b2016-04-27 15:21:52 -0400742 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400744 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745 {
Geoff Lang9f10b772017-05-16 15:51:03 -0400746 if (!CheckAttachmentCompleteness(context, colorAttachment))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000747 {
748 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
749 }
daniel@transgaming.com01868132010-08-24 19:21:17 +0000750
Geoff Lang677bb6f2017-04-05 12:40:40 -0400751 const InternalFormat &format = *colorAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -0400752 if (format.depthBits > 0 || format.stencilBits > 0)
753 {
754 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
755 }
756
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400757 if (!CheckAttachmentSampleCompleteness(context, colorAttachment, true, &samples,
758 &fixedSampleLocations))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000759 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400760 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000761 }
762
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400763 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
764 // in GLES 3.0, there is no such restriction
765 if (state.getClientMajorVersion() < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000766 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400767 if (colorbufferSize.valid())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000768 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400769 if (format.pixelBytes != colorbufferSize.value())
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +0000770 {
771 return GL_FRAMEBUFFER_UNSUPPORTED;
772 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000773 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400774 else
775 {
776 colorbufferSize = format.pixelBytes;
777 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000778 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400779
780 hasRenderbuffer = hasRenderbuffer || (colorAttachment.type() == GL_RENDERBUFFER);
781 hasAttachments = true;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000782 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000783 }
784
Jamie Madill48ef11b2016-04-27 15:21:52 -0400785 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -0400786 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000787 {
Geoff Lang9f10b772017-05-16 15:51:03 -0400788 if (!CheckAttachmentCompleteness(context, depthAttachment))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000790 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791 }
792
Geoff Lang677bb6f2017-04-05 12:40:40 -0400793 const InternalFormat &format = *depthAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -0400794 if (format.depthBits == 0)
795 {
796 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000797 }
798
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400799 if (!CheckAttachmentSampleCompleteness(context, depthAttachment, false, &samples,
800 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000801 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400802 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000803 }
Sami Väisänena797e062016-05-12 15:23:40 +0300804
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400805 hasRenderbuffer = hasRenderbuffer || (depthAttachment.type() == GL_RENDERBUFFER);
806 hasAttachments = true;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000807 }
808
Jamie Madill48ef11b2016-04-27 15:21:52 -0400809 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -0400810 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000811 {
Geoff Lang9f10b772017-05-16 15:51:03 -0400812 if (!CheckAttachmentCompleteness(context, stencilAttachment))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000813 {
814 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
815 }
816
Geoff Lang677bb6f2017-04-05 12:40:40 -0400817 const InternalFormat &format = *stencilAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -0400818 if (format.stencilBits == 0)
819 {
820 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000821 }
822
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400823 if (!CheckAttachmentSampleCompleteness(context, stencilAttachment, false, &samples,
824 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000825 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400826 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000827 }
Corentin Wallez086d59a2016-04-29 09:06:49 -0400828
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400829 hasRenderbuffer = hasRenderbuffer || (stencilAttachment.type() == GL_RENDERBUFFER);
830 hasAttachments = true;
831 }
832
833 // Starting from ES 3.0 stencil and depth, if present, should be the same image
834 if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() &&
835 stencilAttachment.isAttached() && stencilAttachment != depthAttachment)
836 {
837 return GL_FRAMEBUFFER_UNSUPPORTED;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000838 }
839
Jamie Madilla02315b2017-02-23 14:14:47 -0500840 // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL.
841 if (state.isWebGL1())
842 {
843 if (!mState.mWebGLDepthStencilConsistent)
844 {
845 return GL_FRAMEBUFFER_UNSUPPORTED;
846 }
847
848 if (mState.mWebGLDepthStencilAttachment.isAttached())
849 {
850 if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 ||
851 mState.mWebGLDepthStencilAttachment.getStencilSize() == 0)
852 {
853 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
854 }
855 }
856 else if (mState.mStencilAttachment.isAttached() &&
857 mState.mStencilAttachment.getDepthSize() > 0)
858 {
859 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
860 }
861 else if (mState.mDepthAttachment.isAttached() &&
862 mState.mDepthAttachment.getStencilSize() > 0)
863 {
864 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
865 }
866 }
867
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400868 // ES3.1(section 9.4) requires that if no image is attached to the framebuffer, and either the
869 // value of the framebuffer's FRAMEBUFFER_DEFAULT_WIDTH or FRAMEBUFFER_DEFAULT_HEIGHT parameters
870 // is zero, the framebuffer is considered incomplete.
JiangYizhou461d9a32017-01-04 16:37:26 +0800871 GLint defaultWidth = mState.getDefaultWidth();
872 GLint defaultHeight = mState.getDefaultHeight();
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400873 if (!hasAttachments && (defaultWidth == 0 || defaultHeight == 0))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +0000874 {
875 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000876 }
877
Geoff Langa0e0aeb2017-04-12 15:06:29 -0400878 // In ES 2.0 and WebGL, all color attachments must have the same width and height.
Jamie Madillcc86d642015-11-24 13:00:07 -0500879 // In ES 3.0, there is no such restriction.
Geoff Langa0e0aeb2017-04-12 15:06:29 -0400880 if ((state.getClientMajorVersion() < 3 || state.getExtensions().webglCompatibility) &&
881 !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -0500882 {
883 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
884 }
885
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400886 // ES3.1(section 9.4) requires that if the attached images are a mix of renderbuffers and
887 // textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all attached textures.
JiangYizhou461d9a32017-01-04 16:37:26 +0800888 if (fixedSampleLocations.valid() && hasRenderbuffer && !fixedSampleLocations.value())
889 {
890 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
891 }
892
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400893 syncState(context);
Jamie Madillcc86d642015-11-24 13:00:07 -0500894 if (!mImpl->checkStatus())
895 {
896 return GL_FRAMEBUFFER_UNSUPPORTED;
897 }
898
899 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000901
Austin Kinross08332632015-05-05 13:35:47 -0700902Error Framebuffer::discard(size_t count, const GLenum *attachments)
903{
904 return mImpl->discard(count, attachments);
905}
906
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500907Error Framebuffer::invalidate(size_t count, const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400908{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500909 return mImpl->invalidate(count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -0400910}
911
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500912Error Framebuffer::invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -0400913{
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500914 return mImpl->invalidateSub(count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -0400915}
916
Jamie Madill8415b5f2016-04-26 13:41:39 -0400917Error Framebuffer::clear(rx::ContextImpl *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -0500918{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700919 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500920 {
Jamie Madill362876b2016-06-16 14:46:59 -0400921 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500922 }
923
Jamie Madill8415b5f2016-04-26 13:41:39 -0400924 return mImpl->clear(context, mask);
Geoff Langb04dc822014-12-01 12:02:02 -0500925}
926
Jamie Madill8415b5f2016-04-26 13:41:39 -0400927Error Framebuffer::clearBufferfv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400928 GLenum buffer,
929 GLint drawbuffer,
930 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500931{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700932 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500933 {
Jamie Madill362876b2016-06-16 14:46:59 -0400934 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500935 }
936
Jamie Madill8415b5f2016-04-26 13:41:39 -0400937 return mImpl->clearBufferfv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500938}
939
Jamie Madill8415b5f2016-04-26 13:41:39 -0400940Error Framebuffer::clearBufferuiv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400941 GLenum buffer,
942 GLint drawbuffer,
943 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500944{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700945 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500946 {
Jamie Madill362876b2016-06-16 14:46:59 -0400947 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500948 }
949
Jamie Madill8415b5f2016-04-26 13:41:39 -0400950 return mImpl->clearBufferuiv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500951}
952
Jamie Madill8415b5f2016-04-26 13:41:39 -0400953Error Framebuffer::clearBufferiv(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400954 GLenum buffer,
955 GLint drawbuffer,
956 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -0500957{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700958 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500959 {
Jamie Madill362876b2016-06-16 14:46:59 -0400960 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500961 }
962
Jamie Madill8415b5f2016-04-26 13:41:39 -0400963 return mImpl->clearBufferiv(context, buffer, drawbuffer, values);
Geoff Langb04dc822014-12-01 12:02:02 -0500964}
965
Jamie Madill8415b5f2016-04-26 13:41:39 -0400966Error Framebuffer::clearBufferfi(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400967 GLenum buffer,
968 GLint drawbuffer,
969 GLfloat depth,
970 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -0500971{
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700972 if (context->getGLState().isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -0500973 {
Jamie Madill362876b2016-06-16 14:46:59 -0400974 return gl::NoError();
Jamie Madill984ef412015-11-24 16:10:21 -0500975 }
976
Jamie Madill8415b5f2016-04-26 13:41:39 -0400977 return mImpl->clearBufferfi(context, buffer, drawbuffer, depth, stencil);
Geoff Langb04dc822014-12-01 12:02:02 -0500978}
979
Geoff Langbce529e2014-12-01 12:48:41 -0500980GLenum Framebuffer::getImplementationColorReadFormat() const
981{
982 return mImpl->getImplementationColorReadFormat();
983}
984
985GLenum Framebuffer::getImplementationColorReadType() const
986{
987 return mImpl->getImplementationColorReadType();
988}
989
Jamie Madill8415b5f2016-04-26 13:41:39 -0400990Error Framebuffer::readPixels(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500991 const Rectangle &area,
Jamie Madill1b94d432015-08-07 13:23:23 -0400992 GLenum format,
993 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400994 void *pixels) const
Geoff Langbce529e2014-12-01 12:48:41 -0500995{
Jamie Madill362876b2016-06-16 14:46:59 -0400996 ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels));
Geoff Lang520c4ae2015-05-05 13:12:36 -0400997
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700998 Buffer *unpackBuffer = context->getGLState().getUnpackState().pixelBuffer.get();
Geoff Lang520c4ae2015-05-05 13:12:36 -0400999 if (unpackBuffer)
1000 {
1001 unpackBuffer->onPixelUnpack();
1002 }
1003
Jamie Madill362876b2016-06-16 14:46:59 -04001004 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -05001005}
1006
Jamie Madill8415b5f2016-04-26 13:41:39 -04001007Error Framebuffer::blit(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001008 const Rectangle &sourceArea,
1009 const Rectangle &destArea,
Geoff Lang242468f2015-09-24 14:15:41 -04001010 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -04001011 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -05001012{
He Yunchao6be602d2016-12-22 14:33:07 +08001013 GLbitfield blitMask = mask;
1014
1015 // Note that blitting is called against draw framebuffer.
1016 // See the code in gl::Context::blitFramebuffer.
1017 if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer())
1018 {
1019 blitMask &= ~GL_COLOR_BUFFER_BIT;
1020 }
1021
1022 if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr)
1023 {
1024 blitMask &= ~GL_STENCIL_BUFFER_BIT;
1025 }
1026
1027 if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr)
1028 {
1029 blitMask &= ~GL_DEPTH_BUFFER_BIT;
1030 }
1031
1032 if (!blitMask)
1033 {
1034 return NoError();
1035 }
1036
1037 return mImpl->blit(context, sourceArea, destArea, blitMask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -05001038}
1039
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001040int Framebuffer::getSamples(const Context *context)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001041{
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001042 if (complete(context))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001043 {
Jamie Madill362876b2016-06-16 14:46:59 -04001044 // For a complete framebuffer, all attachments must have the same sample count.
1045 // In this case return the first nonzero sample size.
1046 const auto *firstColorAttachment = mState.getFirstColorAttachment();
1047 if (firstColorAttachment)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001048 {
Jamie Madill362876b2016-06-16 14:46:59 -04001049 ASSERT(firstColorAttachment->isAttached());
1050 return firstColorAttachment->getSamples();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001051 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001052 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001053
1054 return 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001055}
1056
Corentin Wallezccab69d2017-01-27 16:57:15 -05001057Error Framebuffer::getSamplePosition(size_t index, GLfloat *xy) const
1058{
1059 ANGLE_TRY(mImpl->getSamplePosition(index, xy));
1060 return gl::NoError();
1061}
1062
Jamie Madille261b442014-06-25 12:42:21 -04001063bool Framebuffer::hasValidDepthStencil() const
1064{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001065 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -04001066}
1067
Jamie Madilla02315b2017-02-23 14:14:47 -05001068void Framebuffer::setAttachment(const Context *context,
1069 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -04001070 GLenum binding,
1071 const ImageIndex &textureIndex,
1072 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -04001073{
Jamie Madilla02315b2017-02-23 14:14:47 -05001074 // Context may be null in unit tests.
1075 if (!context || !context->isWebGL1())
1076 {
1077 setAttachmentImpl(type, binding, textureIndex, resource);
1078 return;
1079 }
1080
1081 switch (binding)
1082 {
1083 case GL_DEPTH_STENCIL:
1084 case GL_DEPTH_STENCIL_ATTACHMENT:
1085 mState.mWebGLDepthStencilAttachment.attach(type, binding, textureIndex, resource);
1086 break;
1087 case GL_DEPTH:
1088 case GL_DEPTH_ATTACHMENT:
1089 mState.mWebGLDepthAttachment.attach(type, binding, textureIndex, resource);
1090 break;
1091 case GL_STENCIL:
1092 case GL_STENCIL_ATTACHMENT:
1093 mState.mWebGLStencilAttachment.attach(type, binding, textureIndex, resource);
1094 break;
1095 default:
1096 setAttachmentImpl(type, binding, textureIndex, resource);
1097 return;
1098 }
1099
1100 commitWebGL1DepthStencilIfConsistent();
1101}
1102
1103void Framebuffer::commitWebGL1DepthStencilIfConsistent()
1104{
1105 int count = 0;
1106
1107 std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment,
1108 &mState.mWebGLDepthAttachment,
1109 &mState.mWebGLStencilAttachment}};
1110 for (FramebufferAttachment *attachment : attachments)
1111 {
1112 if (attachment->isAttached())
1113 {
1114 count++;
1115 }
1116 }
1117
1118 mState.mWebGLDepthStencilConsistent = (count <= 1);
1119 if (!mState.mWebGLDepthStencilConsistent)
1120 {
1121 // Inconsistent.
1122 return;
1123 }
1124
Geoff Lange466c552017-03-17 15:24:12 -04001125 auto getImageIndexIfTextureAttachment = [](const FramebufferAttachment &attachment) {
1126 if (attachment.type() == GL_TEXTURE)
1127 {
1128 return attachment.getTextureImageIndex();
1129 }
1130 else
1131 {
1132 return ImageIndex::MakeInvalid();
1133 }
1134 };
1135
Jamie Madilla02315b2017-02-23 14:14:47 -05001136 if (mState.mWebGLDepthAttachment.isAttached())
1137 {
1138 const auto &depth = mState.mWebGLDepthAttachment;
Geoff Lange466c552017-03-17 15:24:12 -04001139 setAttachmentImpl(depth.type(), GL_DEPTH_ATTACHMENT,
1140 getImageIndexIfTextureAttachment(depth), depth.getResource());
Jamie Madilla02315b2017-02-23 14:14:47 -05001141 setAttachmentImpl(GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
1142 }
1143 else if (mState.mWebGLStencilAttachment.isAttached())
1144 {
1145 const auto &stencil = mState.mWebGLStencilAttachment;
1146 setAttachmentImpl(GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
Geoff Lange466c552017-03-17 15:24:12 -04001147 setAttachmentImpl(stencil.type(), GL_STENCIL_ATTACHMENT,
1148 getImageIndexIfTextureAttachment(stencil), stencil.getResource());
Jamie Madilla02315b2017-02-23 14:14:47 -05001149 }
1150 else if (mState.mWebGLDepthStencilAttachment.isAttached())
1151 {
1152 const auto &depthStencil = mState.mWebGLDepthStencilAttachment;
Geoff Lange466c552017-03-17 15:24:12 -04001153 setAttachmentImpl(depthStencil.type(), GL_DEPTH_ATTACHMENT,
1154 getImageIndexIfTextureAttachment(depthStencil),
Jamie Madilla02315b2017-02-23 14:14:47 -05001155 depthStencil.getResource());
Geoff Lange466c552017-03-17 15:24:12 -04001156 setAttachmentImpl(depthStencil.type(), GL_STENCIL_ATTACHMENT,
1157 getImageIndexIfTextureAttachment(depthStencil),
Jamie Madilla02315b2017-02-23 14:14:47 -05001158 depthStencil.getResource());
1159 }
1160 else
1161 {
1162 setAttachmentImpl(GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
1163 setAttachmentImpl(GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
1164 }
1165}
1166
1167void Framebuffer::setAttachmentImpl(GLenum type,
1168 GLenum binding,
1169 const ImageIndex &textureIndex,
1170 FramebufferAttachmentObject *resource)
1171{
Jamie Madilla02315b2017-02-23 14:14:47 -05001172 switch (binding)
1173 {
Jamie Madillb8126692017-04-05 11:22:17 -04001174 case GL_DEPTH_STENCIL:
1175 case GL_DEPTH_STENCIL_ATTACHMENT:
1176 {
1177 // ensure this is a legitimate depth+stencil format
1178 FramebufferAttachmentObject *attachmentObj = resource;
1179 if (resource)
1180 {
Jamie Madill4fd95d52017-04-05 11:22:18 -04001181 const Format &format = resource->getAttachmentFormat(binding, textureIndex);
Jamie Madillb8126692017-04-05 11:22:17 -04001182 if (format.info->depthBits == 0 || format.info->stencilBits == 0)
1183 {
1184 // Attaching nullptr detaches the current attachment.
1185 attachmentObj = nullptr;
1186 }
1187 }
1188
1189 updateAttachment(&mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
1190 &mDirtyDepthAttachmentBinding, type, binding, textureIndex,
1191 attachmentObj);
1192 updateAttachment(&mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
1193 &mDirtyStencilAttachmentBinding, type, binding, textureIndex,
1194 attachmentObj);
1195 return;
1196 }
1197
Jamie Madilla02315b2017-02-23 14:14:47 -05001198 case GL_DEPTH:
1199 case GL_DEPTH_ATTACHMENT:
Jamie Madillb8126692017-04-05 11:22:17 -04001200 updateAttachment(&mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
1201 &mDirtyDepthAttachmentBinding, type, binding, textureIndex, resource);
Jamie Madill2d06b732015-04-20 12:53:28 -04001202 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001203
Jamie Madilla02315b2017-02-23 14:14:47 -05001204 case GL_STENCIL:
1205 case GL_STENCIL_ATTACHMENT:
Jamie Madillb8126692017-04-05 11:22:17 -04001206 updateAttachment(&mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
1207 &mDirtyStencilAttachmentBinding, type, binding, textureIndex,
1208 resource);
Jamie Madilla02315b2017-02-23 14:14:47 -05001209 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001210
Jamie Madilla02315b2017-02-23 14:14:47 -05001211 case GL_BACK:
1212 mState.mColorAttachments[0].attach(type, binding, textureIndex, resource);
1213 mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0);
1214 // No need for a resource binding for the default FBO, it's always complete.
1215 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001216
Jamie Madilla02315b2017-02-23 14:14:47 -05001217 default:
1218 {
1219 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
1220 ASSERT(colorIndex < mState.mColorAttachments.size());
Jamie Madillb8126692017-04-05 11:22:17 -04001221 size_t dirtyBit = DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex;
1222 updateAttachment(&mState.mColorAttachments[colorIndex], dirtyBit,
1223 &mDirtyColorAttachmentBindings[colorIndex], type, binding,
1224 textureIndex, resource);
Jamie Madilla02315b2017-02-23 14:14:47 -05001225
1226 bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE);
1227 mState.mEnabledDrawBuffers.set(colorIndex, enabled);
Jamie Madill2d06b732015-04-20 12:53:28 -04001228 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001229 break;
Geoff Langab75a052014-10-15 12:56:37 -04001230 }
1231}
1232
Jamie Madillb8126692017-04-05 11:22:17 -04001233void Framebuffer::updateAttachment(FramebufferAttachment *attachment,
1234 size_t dirtyBit,
1235 OnAttachmentDirtyBinding *onDirtyBinding,
1236 GLenum type,
1237 GLenum binding,
1238 const ImageIndex &textureIndex,
1239 FramebufferAttachmentObject *resource)
1240{
1241 attachment->attach(type, binding, textureIndex, resource);
1242 mDirtyBits.set(dirtyBit);
1243 BindResourceChannel(onDirtyBinding, resource);
1244}
1245
Jamie Madilla02315b2017-02-23 14:14:47 -05001246void Framebuffer::resetAttachment(const Context *context, GLenum binding)
Jamie Madill2d06b732015-04-20 12:53:28 -04001247{
Jamie Madilla02315b2017-02-23 14:14:47 -05001248 setAttachment(context, GL_NONE, binding, ImageIndex::MakeInvalid(), nullptr);
Jamie Madill2d06b732015-04-20 12:53:28 -04001249}
1250
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001251void Framebuffer::syncState(const Context *context)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001252{
1253 if (mDirtyBits.any())
1254 {
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001255 mImpl->syncState(rx::SafeGetImpl(context), mDirtyBits);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001256 mDirtyBits.reset();
Corentin Wallezccab69d2017-01-27 16:57:15 -05001257 if (mId != 0)
1258 {
1259 mCachedStatus.reset();
1260 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001261 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001262}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001263
Jamie Madill1e5499d2017-04-05 11:22:16 -04001264void Framebuffer::signal(uint32_t token)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001265{
Jamie Madill362876b2016-06-16 14:46:59 -04001266 // TOOD(jmadill): Make this only update individual attachments to do less work.
1267 mCachedStatus.reset();
Jamie Madill51f40ec2016-06-15 14:06:00 -04001268}
1269
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001270bool Framebuffer::complete(const Context *context)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001271{
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001272 return (checkStatus(context) == GL_FRAMEBUFFER_COMPLETE);
1273}
1274
1275bool Framebuffer::cachedComplete() const
1276{
1277 return (mCachedStatus.valid() && mCachedStatus == GL_FRAMEBUFFER_COMPLETE);
Jamie Madill51f40ec2016-06-15 14:06:00 -04001278}
1279
Jamie Madilla4595b82017-01-11 17:36:34 -05001280bool Framebuffer::formsRenderingFeedbackLoopWith(const State &state) const
1281{
1282 const Program *program = state.getProgram();
1283
1284 // TODO(jmadill): Default framebuffer feedback loops.
1285 if (mId == 0)
1286 {
1287 return false;
1288 }
1289
1290 // The bitset will skip inactive draw buffers.
Jamie Madill6de51852017-04-12 09:53:01 -04001291 for (size_t drawIndex : mState.mEnabledDrawBuffers)
Jamie Madilla4595b82017-01-11 17:36:34 -05001292 {
1293 const FramebufferAttachment *attachment = getDrawBuffer(drawIndex);
1294 if (attachment && attachment->type() == GL_TEXTURE)
1295 {
1296 // Validate the feedback loop.
1297 if (program->samplesFromTexture(state, attachment->id()))
1298 {
1299 return true;
1300 }
1301 }
1302 }
1303
Jamie Madill1d37bc52017-02-02 19:59:58 -05001304 // Validate depth-stencil feedback loop.
1305 const auto &dsState = state.getDepthStencilState();
1306
1307 // We can skip the feedback loop checks if depth/stencil is masked out or disabled.
1308 const FramebufferAttachment *depth = getDepthbuffer();
1309 if (depth && depth->type() == GL_TEXTURE && dsState.depthTest && dsState.depthMask)
1310 {
1311 if (program->samplesFromTexture(state, depth->id()))
1312 {
1313 return true;
1314 }
1315 }
1316
1317 // Note: we assume the front and back masks are the same for WebGL.
1318 const FramebufferAttachment *stencil = getStencilbuffer();
1319 ASSERT(dsState.stencilBackWritemask == dsState.stencilWritemask);
1320 if (stencil && stencil->type() == GL_TEXTURE && dsState.stencilTest &&
1321 dsState.stencilWritemask != 0)
1322 {
1323 // Skip the feedback loop check if depth/stencil point to the same resource.
1324 if (!depth || *stencil != *depth)
1325 {
1326 if (program->samplesFromTexture(state, stencil->id()))
1327 {
1328 return true;
1329 }
1330 }
1331 }
1332
Jamie Madilla4595b82017-01-11 17:36:34 -05001333 return false;
1334}
1335
Jamie Madillfd3dd432017-02-02 19:59:59 -05001336bool Framebuffer::formsCopyingFeedbackLoopWith(GLuint copyTextureID,
1337 GLint copyTextureLevel,
1338 GLint copyTextureLayer) const
Jamie Madillf695a3a2017-01-11 17:36:35 -05001339{
1340 if (mId == 0)
1341 {
1342 // It seems impossible to form a texture copying feedback loop with the default FBO.
1343 return false;
1344 }
1345
1346 const FramebufferAttachment *readAttachment = getReadColorbuffer();
1347 ASSERT(readAttachment);
1348
1349 if (readAttachment->isTextureWithId(copyTextureID))
1350 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001351 const auto &imageIndex = readAttachment->getTextureImageIndex();
1352 if (imageIndex.mipIndex == copyTextureLevel)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001353 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001354 // Check 3D/Array texture layers.
1355 return imageIndex.layerIndex == ImageIndex::ENTIRE_LEVEL ||
1356 copyTextureLayer == ImageIndex::ENTIRE_LEVEL ||
1357 imageIndex.layerIndex == copyTextureLayer;
Jamie Madillf695a3a2017-01-11 17:36:35 -05001358 }
1359 }
1360 return false;
1361}
1362
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001363GLint Framebuffer::getDefaultWidth() const
1364{
1365 return mState.getDefaultWidth();
1366}
1367
1368GLint Framebuffer::getDefaultHeight() const
1369{
1370 return mState.getDefaultHeight();
1371}
1372
1373GLint Framebuffer::getDefaultSamples() const
1374{
1375 return mState.getDefaultSamples();
1376}
1377
1378GLboolean Framebuffer::getDefaultFixedSampleLocations() const
1379{
1380 return mState.getDefaultFixedSampleLocations();
1381}
1382
1383void Framebuffer::setDefaultWidth(GLint defaultWidth)
1384{
1385 mState.mDefaultWidth = defaultWidth;
1386 mDirtyBits.set(DIRTY_BIT_DEFAULT_WIDTH);
1387}
1388
1389void Framebuffer::setDefaultHeight(GLint defaultHeight)
1390{
1391 mState.mDefaultHeight = defaultHeight;
1392 mDirtyBits.set(DIRTY_BIT_DEFAULT_HEIGHT);
1393}
1394
1395void Framebuffer::setDefaultSamples(GLint defaultSamples)
1396{
1397 mState.mDefaultSamples = defaultSamples;
1398 mDirtyBits.set(DIRTY_BIT_DEFAULT_SAMPLES);
1399}
1400
1401void Framebuffer::setDefaultFixedSampleLocations(GLboolean defaultFixedSampleLocations)
1402{
1403 mState.mDefaultFixedSampleLocations = defaultFixedSampleLocations;
1404 mDirtyBits.set(DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS);
1405}
1406
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001407// TODO(jmadill): Remove this kludge.
1408GLenum Framebuffer::checkStatus(const ValidationContext *context)
1409{
1410 return checkStatus(static_cast<const Context *>(context));
1411}
1412
1413int Framebuffer::getSamples(const ValidationContext *context)
1414{
1415 return getSamples(static_cast<const Context *>(context));
1416}
1417
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001418} // namespace gl