blob: 1087c1786b01206ed9b2b13e602b00ac4c194978 [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"
Brandon Jones76746f92017-11-22 11:44:41 -080022#include "libANGLE/angletypes.h"
Jamie Madillc46f45d2015-03-31 13:20:55 -040023#include "libANGLE/formatutils.h"
Jamie Madill8415b5f2016-04-26 13:41:39 -040024#include "libANGLE/renderer/ContextImpl.h"
Geoff Langb5d8f232014-12-04 15:43:01 -050025#include "libANGLE/renderer/FramebufferImpl.h"
Jamie Madill7aea7e02016-05-10 10:39:45 -040026#include "libANGLE/renderer/GLImplFactory.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050027#include "libANGLE/renderer/RenderbufferImpl.h"
Corentin Wallez37c39792015-08-20 14:19:46 -040028#include "libANGLE/renderer/SurfaceImpl.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040029
Jamie Madill362876b2016-06-16 14:46:59 -040030using namespace angle;
31
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032namespace gl
33{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000034
Jamie Madilld1405e52015-03-05 15:41:39 -050035namespace
36{
Jamie Madill362876b2016-06-16 14:46:59 -040037
Martin Radev9bc9a322017-07-21 14:28:17 +030038bool CheckMultiviewStateMatchesForCompleteness(const FramebufferAttachment *firstAttachment,
39 const FramebufferAttachment *secondAttachment)
40{
41 ASSERT(firstAttachment && secondAttachment);
42 ASSERT(firstAttachment->isAttached() && secondAttachment->isAttached());
43
44 if (firstAttachment->getNumViews() != secondAttachment->getNumViews())
45 {
46 return false;
47 }
48 if (firstAttachment->getBaseViewIndex() != secondAttachment->getBaseViewIndex())
49 {
50 return false;
51 }
52 if (firstAttachment->getMultiviewLayout() != secondAttachment->getMultiviewLayout())
53 {
54 return false;
55 }
56 if (firstAttachment->getMultiviewViewportOffsets() !=
57 secondAttachment->getMultiviewViewportOffsets())
58 {
59 return false;
60 }
61 return true;
62}
63
Geoff Lang9f10b772017-05-16 15:51:03 -040064bool CheckAttachmentCompleteness(const Context *context, const FramebufferAttachment &attachment)
65{
66 ASSERT(attachment.isAttached());
67
68 const Extents &size = attachment.getSize();
69 if (size.width == 0 || size.height == 0)
70 {
71 return false;
72 }
73
74 const InternalFormat &format = *attachment.getFormat().info;
75 if (!format.renderSupport(context->getClientVersion(), context->getExtensions()))
76 {
77 return false;
78 }
79
80 if (attachment.type() == GL_TEXTURE)
81 {
82 if (attachment.layer() >= size.depth)
83 {
84 return false;
85 }
86
87 // ES3 specifies that cube map texture attachments must be cube complete.
88 // This language is missing from the ES2 spec, but we enforce it here because some
89 // desktop OpenGL drivers also enforce this validation.
90 // TODO(jmadill): Check if OpenGL ES2 drivers enforce cube completeness.
91 const Texture *texture = attachment.getTexture();
92 ASSERT(texture);
Corentin Wallez99d492c2018-02-27 15:17:10 -050093 if (texture->getType() == TextureType::CubeMap &&
Geoff Lang9f10b772017-05-16 15:51:03 -040094 !texture->getTextureState().isCubeComplete())
95 {
96 return false;
97 }
Geoff Lang857c09d2017-05-16 15:55:04 -040098
99 if (!texture->getImmutableFormat())
100 {
101 GLuint attachmentMipLevel = static_cast<GLuint>(attachment.mipLevel());
102
103 // From the ES 3.0 spec, pg 213:
104 // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of
105 // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture,
106 // then the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL must be in the
107 // range[levelbase, q], where levelbase is the value of TEXTURE_BASE_LEVEL and q is
108 // the effective maximum texture level defined in the Mipmapping discussion of
109 // section 3.8.10.4.
110 if (attachmentMipLevel < texture->getBaseLevel() ||
111 attachmentMipLevel > texture->getMipmapMaxLevel())
112 {
113 return false;
114 }
115
116 // Form the ES 3.0 spec, pg 213/214:
117 // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of
118 // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture and
119 // the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL is not levelbase, then the
120 // texture must be mipmap complete, and if FRAMEBUFFER_ATTACHMENT_OBJECT_NAME names
121 // a cubemap texture, the texture must also be cube complete.
122 if (attachmentMipLevel != texture->getBaseLevel() && !texture->isMipmapComplete())
123 {
124 return false;
125 }
126 }
Geoff Lang9f10b772017-05-16 15:51:03 -0400127 }
128
129 return true;
130};
131
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400132bool CheckAttachmentSampleCompleteness(const Context *context,
133 const FramebufferAttachment &attachment,
134 bool colorAttachment,
135 Optional<int> *samples,
Geoff Lang92019432017-11-20 13:09:34 -0500136 Optional<bool> *fixedSampleLocations)
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400137{
138 ASSERT(attachment.isAttached());
139
140 if (attachment.type() == GL_TEXTURE)
141 {
142 const Texture *texture = attachment.getTexture();
143 ASSERT(texture);
144
145 const ImageIndex &attachmentImageIndex = attachment.getTextureImageIndex();
146
147 // ES3.1 (section 9.4) requires that the value of TEXTURE_FIXED_SAMPLE_LOCATIONS should be
148 // the same for all attached textures.
Geoff Langb52fac02018-02-21 15:45:35 -0500149 bool fixedSampleloc = texture->getFixedSampleLocations(attachmentImageIndex.target,
Geoff Lang92019432017-11-20 13:09:34 -0500150 attachmentImageIndex.mipIndex);
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400151 if (fixedSampleLocations->valid() && fixedSampleloc != fixedSampleLocations->value())
152 {
153 return false;
154 }
155 else
156 {
157 *fixedSampleLocations = fixedSampleloc;
158 }
159 }
160
161 if (samples->valid())
162 {
163 if (attachment.getSamples() != samples->value())
164 {
165 if (colorAttachment)
166 {
167 // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that
168 // all color attachments have the same number of samples for the FBO to be complete.
169 return false;
170 }
171 else
172 {
173 // CHROMIUM_framebuffer_mixed_samples allows a framebuffer to be considered complete
174 // when its depth or stencil samples are a multiple of the number of color samples.
175 if (!context->getExtensions().framebufferMixedSamples)
176 {
177 return false;
178 }
179
180 if ((attachment.getSamples() % std::max(samples->value(), 1)) != 0)
181 {
182 return false;
183 }
184 }
185 }
186 }
187 else
188 {
189 *samples = attachment.getSamples();
190 }
191
192 return true;
193}
194
Jamie Madill05b35b22017-10-03 09:01:44 -0400195// Needed to index into the attachment arrays/bitsets.
Jamie Madill682efdc2017-10-03 14:10:29 -0400196static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500197 Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX,
Jamie Madill05b35b22017-10-03 09:01:44 -0400198 "Framebuffer Dirty bit mismatch");
Jamie Madill682efdc2017-10-03 14:10:29 -0400199static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500200 Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madill05b35b22017-10-03 09:01:44 -0400201 "Framebuffer Dirty bit mismatch");
Jamie Madill682efdc2017-10-03 14:10:29 -0400202static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS + 1) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500203 Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madill05b35b22017-10-03 09:01:44 -0400204 "Framebuffer Dirty bit mismatch");
205
206Error InitAttachment(const Context *context, FramebufferAttachment *attachment)
207{
208 ASSERT(attachment->isAttached());
209 if (attachment->initState() == InitState::MayNeedInit)
210 {
211 ANGLE_TRY(attachment->initializeContents(context));
212 }
213 return NoError();
214}
215
216bool IsColorMaskedOut(const BlendState &blend)
217{
218 return (!blend.colorMaskRed && !blend.colorMaskGreen && !blend.colorMaskBlue &&
219 !blend.colorMaskAlpha);
220}
221
222bool IsDepthMaskedOut(const DepthStencilState &depthStencil)
223{
224 return !depthStencil.depthMask;
225}
226
227bool IsStencilMaskedOut(const DepthStencilState &depthStencil)
228{
229 return ((depthStencil.stencilMask & depthStencil.stencilWritemask) == 0);
230}
231
232bool IsClearBufferMaskedOut(const Context *context, GLenum buffer)
233{
234 switch (buffer)
235 {
236 case GL_COLOR:
237 return IsColorMaskedOut(context->getGLState().getBlendState());
238 case GL_DEPTH:
239 return IsDepthMaskedOut(context->getGLState().getDepthStencilState());
240 case GL_STENCIL:
241 return IsStencilMaskedOut(context->getGLState().getDepthStencilState());
242 case GL_DEPTH_STENCIL:
243 return IsDepthMaskedOut(context->getGLState().getDepthStencilState()) &&
244 IsStencilMaskedOut(context->getGLState().getDepthStencilState());
245 default:
246 UNREACHABLE();
247 return true;
248 }
249}
250
Jamie Madill362876b2016-06-16 14:46:59 -0400251} // anonymous namespace
Jamie Madilld1405e52015-03-05 15:41:39 -0500252
Jamie Madill6f60d052017-02-22 15:20:11 -0500253// This constructor is only used for default framebuffers.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400254FramebufferState::FramebufferState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500255 : mLabel(),
256 mColorAttachments(1),
Corentin Walleze7557742017-06-01 13:09:57 -0400257 mDrawBufferStates(1, GL_BACK),
Jamie Madill6f60d052017-02-22 15:20:11 -0500258 mReadBufferState(GL_BACK),
Brandon Jones76746f92017-11-22 11:44:41 -0800259 mDrawBufferTypeMask(),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800260 mDefaultWidth(0),
261 mDefaultHeight(0),
262 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500263 mDefaultFixedSampleLocations(GL_FALSE),
264 mWebGLDepthStencilConsistent(true)
Corentin Wallez37c39792015-08-20 14:19:46 -0400265{
Geoff Langd90d3882017-03-21 10:49:54 -0400266 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilla4595b82017-01-11 17:36:34 -0500267 mEnabledDrawBuffers.set(0);
Corentin Wallez37c39792015-08-20 14:19:46 -0400268}
269
Jamie Madill48ef11b2016-04-27 15:21:52 -0400270FramebufferState::FramebufferState(const Caps &caps)
Geoff Lang70d0f492015-12-10 17:45:46 -0500271 : mLabel(),
272 mColorAttachments(caps.maxColorAttachments),
Jamie Madilld1405e52015-03-05 15:41:39 -0500273 mDrawBufferStates(caps.maxDrawBuffers, GL_NONE),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800274 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT),
Brandon Jones76746f92017-11-22 11:44:41 -0800275 mDrawBufferTypeMask(),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800276 mDefaultWidth(0),
277 mDefaultHeight(0),
278 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500279 mDefaultFixedSampleLocations(GL_FALSE),
280 mWebGLDepthStencilConsistent(true)
Jamie Madilld1405e52015-03-05 15:41:39 -0500281{
Geoff Langa15472a2015-08-11 11:48:03 -0400282 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilld1405e52015-03-05 15:41:39 -0500283 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
284}
285
Jamie Madill48ef11b2016-04-27 15:21:52 -0400286FramebufferState::~FramebufferState()
Jamie Madilld1405e52015-03-05 15:41:39 -0500287{
Jamie Madilld1405e52015-03-05 15:41:39 -0500288}
289
Jamie Madill48ef11b2016-04-27 15:21:52 -0400290const std::string &FramebufferState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500291{
292 return mLabel;
293}
294
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800295const FramebufferAttachment *FramebufferState::getAttachment(const Context *context,
296 GLenum attachment) const
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400297{
298 if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15)
299 {
300 return getColorAttachment(attachment - GL_COLOR_ATTACHMENT0);
301 }
302
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800303 // WebGL1 allows a developer to query for attachment parameters even when "inconsistant" (i.e.
304 // multiple conflicting attachment points) and requires us to return the framebuffer attachment
305 // associated with WebGL.
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400306 switch (attachment)
307 {
308 case GL_COLOR:
309 case GL_BACK:
310 return getColorAttachment(0);
311 case GL_DEPTH:
312 case GL_DEPTH_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800313 if (context->isWebGL1())
314 {
315 return getWebGLDepthAttachment();
316 }
317 else
318 {
319 return getDepthAttachment();
320 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400321 case GL_STENCIL:
322 case GL_STENCIL_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800323 if (context->isWebGL1())
324 {
325 return getWebGLStencilAttachment();
326 }
327 else
328 {
329 return getStencilAttachment();
330 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400331 case GL_DEPTH_STENCIL:
332 case GL_DEPTH_STENCIL_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800333 if (context->isWebGL1())
334 {
335 return getWebGLDepthStencilAttachment();
336 }
337 else
338 {
339 return getDepthStencilAttachment();
340 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400341 default:
342 UNREACHABLE();
343 return nullptr;
344 }
345}
346
Jamie Madill05b35b22017-10-03 09:01:44 -0400347size_t FramebufferState::getReadIndex() const
Jamie Madill7147f012015-03-05 15:41:40 -0500348{
Jamie Madill231c7f52017-04-26 13:45:37 -0400349 ASSERT(mReadBufferState == GL_BACK ||
350 (mReadBufferState >= GL_COLOR_ATTACHMENT0 && mReadBufferState <= GL_COLOR_ATTACHMENT15));
351 size_t readIndex = (mReadBufferState == GL_BACK
352 ? 0
353 : static_cast<size_t>(mReadBufferState - GL_COLOR_ATTACHMENT0));
Jamie Madill7147f012015-03-05 15:41:40 -0500354 ASSERT(readIndex < mColorAttachments.size());
Jamie Madill05b35b22017-10-03 09:01:44 -0400355 return readIndex;
356}
357
358const FramebufferAttachment *FramebufferState::getReadAttachment() const
359{
360 if (mReadBufferState == GL_NONE)
361 {
362 return nullptr;
363 }
364 size_t readIndex = getReadIndex();
Jamie Madill2d06b732015-04-20 12:53:28 -0400365 return mColorAttachments[readIndex].isAttached() ? &mColorAttachments[readIndex] : nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500366}
367
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500368const FramebufferAttachment *FramebufferState::getFirstNonNullAttachment() const
369{
370 auto *colorAttachment = getFirstColorAttachment();
371 if (colorAttachment)
372 {
373 return colorAttachment;
374 }
375 return getDepthOrStencilAttachment();
376}
377
Jamie Madill48ef11b2016-04-27 15:21:52 -0400378const FramebufferAttachment *FramebufferState::getFirstColorAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500379{
Jamie Madill2d06b732015-04-20 12:53:28 -0400380 for (const FramebufferAttachment &colorAttachment : mColorAttachments)
Jamie Madill7147f012015-03-05 15:41:40 -0500381 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400382 if (colorAttachment.isAttached())
Jamie Madill7147f012015-03-05 15:41:40 -0500383 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400384 return &colorAttachment;
Jamie Madill7147f012015-03-05 15:41:40 -0500385 }
386 }
387
388 return nullptr;
389}
390
Jamie Madill48ef11b2016-04-27 15:21:52 -0400391const FramebufferAttachment *FramebufferState::getDepthOrStencilAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500392{
Jamie Madill2d06b732015-04-20 12:53:28 -0400393 if (mDepthAttachment.isAttached())
394 {
395 return &mDepthAttachment;
396 }
397 if (mStencilAttachment.isAttached())
398 {
399 return &mStencilAttachment;
400 }
401 return nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500402}
403
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500404const FramebufferAttachment *FramebufferState::getStencilOrDepthStencilAttachment() const
405{
406 if (mStencilAttachment.isAttached())
407 {
408 return &mStencilAttachment;
409 }
410 return getDepthStencilAttachment();
411}
412
Jamie Madill48ef11b2016-04-27 15:21:52 -0400413const FramebufferAttachment *FramebufferState::getColorAttachment(size_t colorAttachment) const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400414{
415 ASSERT(colorAttachment < mColorAttachments.size());
Jamie Madill231c7f52017-04-26 13:45:37 -0400416 return mColorAttachments[colorAttachment].isAttached() ? &mColorAttachments[colorAttachment]
417 : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400418}
419
Jamie Madill48ef11b2016-04-27 15:21:52 -0400420const FramebufferAttachment *FramebufferState::getDepthAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400421{
Jamie Madill2d06b732015-04-20 12:53:28 -0400422 return mDepthAttachment.isAttached() ? &mDepthAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400423}
424
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800425const FramebufferAttachment *FramebufferState::getWebGLDepthAttachment() const
426{
427 return mWebGLDepthAttachment.isAttached() ? &mWebGLDepthAttachment : nullptr;
428}
429
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800430const FramebufferAttachment *FramebufferState::getWebGLDepthStencilAttachment() const
431{
432 return mWebGLDepthStencilAttachment.isAttached() ? &mWebGLDepthStencilAttachment : nullptr;
433}
434
Jamie Madill48ef11b2016-04-27 15:21:52 -0400435const FramebufferAttachment *FramebufferState::getStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400436{
Jamie Madill2d06b732015-04-20 12:53:28 -0400437 return mStencilAttachment.isAttached() ? &mStencilAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400438}
439
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800440const FramebufferAttachment *FramebufferState::getWebGLStencilAttachment() const
441{
442 return mWebGLStencilAttachment.isAttached() ? &mWebGLStencilAttachment : nullptr;
443}
444
Jamie Madill48ef11b2016-04-27 15:21:52 -0400445const FramebufferAttachment *FramebufferState::getDepthStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400446{
447 // A valid depth-stencil attachment has the same resource bound to both the
448 // depth and stencil attachment points.
Jamie Madill2d06b732015-04-20 12:53:28 -0400449 if (mDepthAttachment.isAttached() && mStencilAttachment.isAttached() &&
Jamie Madill44f26482016-11-18 12:49:15 -0500450 mDepthAttachment == mStencilAttachment)
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400451 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400452 return &mDepthAttachment;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400453 }
454
455 return nullptr;
456}
457
Jamie Madill48ef11b2016-04-27 15:21:52 -0400458bool FramebufferState::attachmentsHaveSameDimensions() const
Jamie Madillcc86d642015-11-24 13:00:07 -0500459{
460 Optional<Extents> attachmentSize;
461
Jamie Madill231c7f52017-04-26 13:45:37 -0400462 auto hasMismatchedSize = [&attachmentSize](const FramebufferAttachment &attachment) {
Jamie Madillcc86d642015-11-24 13:00:07 -0500463 if (!attachment.isAttached())
464 {
465 return false;
466 }
467
468 if (!attachmentSize.valid())
469 {
470 attachmentSize = attachment.getSize();
471 return false;
472 }
473
Jeff Gilbert8f8edd62017-10-31 14:26:30 -0700474 const auto &prevSize = attachmentSize.value();
475 const auto &curSize = attachment.getSize();
476 return (curSize.width != prevSize.width || curSize.height != prevSize.height);
Jamie Madillcc86d642015-11-24 13:00:07 -0500477 };
478
479 for (const auto &attachment : mColorAttachments)
480 {
481 if (hasMismatchedSize(attachment))
482 {
483 return false;
484 }
485 }
486
487 if (hasMismatchedSize(mDepthAttachment))
488 {
489 return false;
490 }
491
492 return !hasMismatchedSize(mStencilAttachment);
493}
494
Jamie Madilld4442552018-02-27 22:03:47 -0500495const FramebufferAttachment *FramebufferState::getDrawBuffer(size_t drawBufferIdx) const
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400496{
497 ASSERT(drawBufferIdx < mDrawBufferStates.size());
498 if (mDrawBufferStates[drawBufferIdx] != GL_NONE)
499 {
500 // ES3 spec: "If the GL is bound to a draw framebuffer object, the ith buffer listed in bufs
501 // must be COLOR_ATTACHMENTi or NONE"
502 ASSERT(mDrawBufferStates[drawBufferIdx] == GL_COLOR_ATTACHMENT0 + drawBufferIdx ||
503 (drawBufferIdx == 0 && mDrawBufferStates[drawBufferIdx] == GL_BACK));
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800504
505 if (mDrawBufferStates[drawBufferIdx] == GL_BACK)
506 {
507 return getColorAttachment(0);
508 }
509 else
510 {
511 return getColorAttachment(mDrawBufferStates[drawBufferIdx] - GL_COLOR_ATTACHMENT0);
512 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400513 }
514 else
515 {
516 return nullptr;
517 }
518}
519
520size_t FramebufferState::getDrawBufferCount() const
521{
522 return mDrawBufferStates.size();
523}
524
Geoff Langb21e20d2016-07-19 15:35:41 -0400525bool FramebufferState::colorAttachmentsAreUniqueImages() const
526{
527 for (size_t firstAttachmentIdx = 0; firstAttachmentIdx < mColorAttachments.size();
528 firstAttachmentIdx++)
529 {
Jamie Madilld4442552018-02-27 22:03:47 -0500530 const FramebufferAttachment &firstAttachment = mColorAttachments[firstAttachmentIdx];
Geoff Langb21e20d2016-07-19 15:35:41 -0400531 if (!firstAttachment.isAttached())
532 {
533 continue;
534 }
535
536 for (size_t secondAttachmentIdx = firstAttachmentIdx + 1;
537 secondAttachmentIdx < mColorAttachments.size(); secondAttachmentIdx++)
538 {
Jamie Madilld4442552018-02-27 22:03:47 -0500539 const FramebufferAttachment &secondAttachment = mColorAttachments[secondAttachmentIdx];
Geoff Langb21e20d2016-07-19 15:35:41 -0400540 if (!secondAttachment.isAttached())
541 {
542 continue;
543 }
544
545 if (firstAttachment == secondAttachment)
546 {
547 return false;
548 }
549 }
550 }
551
552 return true;
553}
554
Jamie Madill9c335862017-07-18 11:51:38 -0400555bool FramebufferState::hasDepth() const
556{
557 return (mDepthAttachment.isAttached() && mDepthAttachment.getDepthSize() > 0);
558}
559
560bool FramebufferState::hasStencil() const
561{
562 return (mStencilAttachment.isAttached() && mStencilAttachment.getStencilSize() > 0);
563}
564
Martin Radev5c00d0d2017-08-07 10:06:59 +0300565GLsizei FramebufferState::getNumViews() const
566{
567 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
568 if (attachment == nullptr)
569 {
570 return FramebufferAttachment::kDefaultNumViews;
571 }
572 return attachment->getNumViews();
573}
574
575const std::vector<Offset> *FramebufferState::getViewportOffsets() const
576{
577 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
578 if (attachment == nullptr)
579 {
580 return nullptr;
581 }
582 return &attachment->getMultiviewViewportOffsets();
583}
584
585GLenum FramebufferState::getMultiviewLayout() const
586{
587 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
588 if (attachment == nullptr)
589 {
590 return GL_NONE;
591 }
592 return attachment->getMultiviewLayout();
593}
594
Martin Radev4e619f52017-08-09 11:50:06 +0300595int FramebufferState::getBaseViewIndex() const
596{
597 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
598 if (attachment == nullptr)
599 {
600 return GL_NONE;
601 }
602 return attachment->getBaseViewIndex();
603}
604
Jamie Madill05b35b22017-10-03 09:01:44 -0400605Box FramebufferState::getDimensions() const
606{
607 ASSERT(attachmentsHaveSameDimensions());
608 ASSERT(getFirstNonNullAttachment() != nullptr);
609 Extents extents = getFirstNonNullAttachment()->getSize();
610 return Box(0, 0, 0, extents.width, extents.height, extents.depth);
611}
612
Jamie Madill7aea7e02016-05-10 10:39:45 -0400613Framebuffer::Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id)
Jamie Madill362876b2016-06-16 14:46:59 -0400614 : mState(caps),
615 mImpl(factory->createFramebuffer(mState)),
616 mId(id),
617 mCachedStatus(),
618 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
619 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620{
Corentin Wallez37c39792015-08-20 14:19:46 -0400621 ASSERT(mId != 0);
622 ASSERT(mImpl != nullptr);
Jamie Madill362876b2016-06-16 14:46:59 -0400623 ASSERT(mState.mColorAttachments.size() == static_cast<size_t>(caps.maxColorAttachments));
624
Jamie Madill1e5499d2017-04-05 11:22:16 -0400625 for (uint32_t colorIndex = 0;
626 colorIndex < static_cast<uint32_t>(mState.mColorAttachments.size()); ++colorIndex)
Jamie Madill362876b2016-06-16 14:46:59 -0400627 {
Jamie Madill1e5499d2017-04-05 11:22:16 -0400628 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
Jamie Madill362876b2016-06-16 14:46:59 -0400629 }
Corentin Wallez37c39792015-08-20 14:19:46 -0400630}
631
Jamie Madill4928b7c2017-06-20 12:57:39 -0400632Framebuffer::Framebuffer(const egl::Display *display, egl::Surface *surface)
Jamie Madill362876b2016-06-16 14:46:59 -0400633 : mState(),
Jamie Madill6f60d052017-02-22 15:20:11 -0500634 mImpl(surface->getImplementation()->createDefaultFramebuffer(mState)),
Jamie Madill362876b2016-06-16 14:46:59 -0400635 mId(0),
636 mCachedStatus(GL_FRAMEBUFFER_COMPLETE),
637 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
638 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
Corentin Wallez37c39792015-08-20 14:19:46 -0400639{
Geoff Langda88add2014-12-01 10:22:01 -0500640 ASSERT(mImpl != nullptr);
Jamie Madill1e5499d2017-04-05 11:22:16 -0400641 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Jamie Madill6f60d052017-02-22 15:20:11 -0500642
Jamie Madill4928b7c2017-06-20 12:57:39 -0400643 const Context *proxyContext = display->getProxyContext();
644
Jamie Madilld4442552018-02-27 22:03:47 -0500645 setAttachmentImpl(proxyContext, GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex::MakeInvalid(),
Martin Radev5dae57b2017-07-14 16:15:55 +0300646 surface, FramebufferAttachment::kDefaultNumViews,
647 FramebufferAttachment::kDefaultBaseViewIndex,
648 FramebufferAttachment::kDefaultMultiviewLayout,
649 FramebufferAttachment::kDefaultViewportOffsets);
Jamie Madill6f60d052017-02-22 15:20:11 -0500650
651 if (surface->getConfig()->depthSize > 0)
652 {
Jamie Madilld4442552018-02-27 22:03:47 -0500653 setAttachmentImpl(proxyContext, GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, ImageIndex::MakeInvalid(),
654 surface, FramebufferAttachment::kDefaultNumViews,
655 FramebufferAttachment::kDefaultBaseViewIndex,
656 FramebufferAttachment::kDefaultMultiviewLayout,
657 FramebufferAttachment::kDefaultViewportOffsets);
Jamie Madill6f60d052017-02-22 15:20:11 -0500658 }
659
660 if (surface->getConfig()->stencilSize > 0)
661 {
Jamie Madilld4442552018-02-27 22:03:47 -0500662 setAttachmentImpl(
663 proxyContext, GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, ImageIndex::MakeInvalid(), surface,
664 FramebufferAttachment::kDefaultNumViews, FramebufferAttachment::kDefaultBaseViewIndex,
665 FramebufferAttachment::kDefaultMultiviewLayout,
666 FramebufferAttachment::kDefaultViewportOffsets);
Jamie Madill6f60d052017-02-22 15:20:11 -0500667 }
Brandon Jones76746f92017-11-22 11:44:41 -0800668 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(0), 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669}
670
Corentin Wallezccab69d2017-01-27 16:57:15 -0500671Framebuffer::Framebuffer(rx::GLImplFactory *factory)
672 : mState(),
673 mImpl(factory->createFramebuffer(mState)),
674 mId(0),
675 mCachedStatus(GL_FRAMEBUFFER_UNDEFINED_OES),
676 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
677 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
678{
Jamie Madill1e5499d2017-04-05 11:22:16 -0400679 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Brandon Jones76746f92017-11-22 11:44:41 -0800680 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(0), 0);
Corentin Wallezccab69d2017-01-27 16:57:15 -0500681}
682
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683Framebuffer::~Framebuffer()
684{
Geoff Langda88add2014-12-01 10:22:01 -0500685 SafeDelete(mImpl);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000686}
687
Jamie Madill4928b7c2017-06-20 12:57:39 -0400688void Framebuffer::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500689{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400690 for (auto &attachment : mState.mColorAttachments)
691 {
692 attachment.detach(context);
693 }
694 mState.mDepthAttachment.detach(context);
695 mState.mStencilAttachment.detach(context);
696 mState.mWebGLDepthAttachment.detach(context);
697 mState.mWebGLStencilAttachment.detach(context);
698 mState.mWebGLDepthStencilAttachment.detach(context);
699
Jamie Madillc564c072017-06-01 12:45:42 -0400700 mImpl->destroy(context);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500701}
702
703void Framebuffer::destroyDefault(const egl::Display *display)
704{
Jamie Madillc564c072017-06-01 12:45:42 -0400705 mImpl->destroyDefault(display);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500706}
707
Geoff Lang70d0f492015-12-10 17:45:46 -0500708void Framebuffer::setLabel(const std::string &label)
709{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400710 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500711}
712
713const std::string &Framebuffer::getLabel() const
714{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400715 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500716}
717
Jamie Madill8693bdb2017-09-02 15:32:14 -0400718bool Framebuffer::detachTexture(const Context *context, GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000719{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400720 return detachResourceById(context, GL_TEXTURE, textureId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000721}
722
Jamie Madill8693bdb2017-09-02 15:32:14 -0400723bool Framebuffer::detachRenderbuffer(const Context *context, GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400725 return detachResourceById(context, GL_RENDERBUFFER, renderbufferId);
Jamie Madilld1405e52015-03-05 15:41:39 -0500726}
Jamie Madille261b442014-06-25 12:42:21 -0400727
Jamie Madill8693bdb2017-09-02 15:32:14 -0400728bool Framebuffer::detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId)
Jamie Madilld1405e52015-03-05 15:41:39 -0500729{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400730 bool found = false;
731
Jamie Madill362876b2016-06-16 14:46:59 -0400732 for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
Jamie Madilld1405e52015-03-05 15:41:39 -0500733 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400734 if (detachMatchingAttachment(context, &mState.mColorAttachments[colorIndex], resourceType,
735 resourceId, DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex))
736 {
737 found = true;
738 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739 }
740
Jamie Madilla02315b2017-02-23 14:14:47 -0500741 if (context->isWebGL1())
742 {
743 const std::array<FramebufferAttachment *, 3> attachments = {
744 {&mState.mWebGLDepthStencilAttachment, &mState.mWebGLDepthAttachment,
745 &mState.mWebGLStencilAttachment}};
746 for (FramebufferAttachment *attachment : attachments)
747 {
748 if (attachment->isAttached() && attachment->type() == resourceType &&
749 attachment->id() == resourceId)
750 {
751 resetAttachment(context, attachment->getBinding());
Jamie Madill8693bdb2017-09-02 15:32:14 -0400752 found = true;
Jamie Madilla02315b2017-02-23 14:14:47 -0500753 }
754 }
755 }
756 else
757 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400758 if (detachMatchingAttachment(context, &mState.mDepthAttachment, resourceType, resourceId,
759 DIRTY_BIT_DEPTH_ATTACHMENT))
760 {
761 found = true;
762 }
763 if (detachMatchingAttachment(context, &mState.mStencilAttachment, resourceType, resourceId,
764 DIRTY_BIT_STENCIL_ATTACHMENT))
765 {
766 found = true;
767 }
Jamie Madilla02315b2017-02-23 14:14:47 -0500768 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400769
770 return found;
Jamie Madill362876b2016-06-16 14:46:59 -0400771}
772
Jamie Madill8693bdb2017-09-02 15:32:14 -0400773bool Framebuffer::detachMatchingAttachment(const Context *context,
Jamie Madill4928b7c2017-06-20 12:57:39 -0400774 FramebufferAttachment *attachment,
Jamie Madill362876b2016-06-16 14:46:59 -0400775 GLenum matchType,
776 GLuint matchId,
777 size_t dirtyBit)
778{
779 if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId)
780 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400781 attachment->detach(context);
Jamie Madill362876b2016-06-16 14:46:59 -0400782 mDirtyBits.set(dirtyBit);
Jamie Madill05b35b22017-10-03 09:01:44 -0400783 mState.mResourceNeedsInit.set(dirtyBit, false);
Jamie Madill8693bdb2017-09-02 15:32:14 -0400784 return true;
Jamie Madill362876b2016-06-16 14:46:59 -0400785 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400786
787 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788}
789
Corentin Wallez37c39792015-08-20 14:19:46 -0400790const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400792 return mState.getColorAttachment(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793}
794
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400795const FramebufferAttachment *Framebuffer::getDepthbuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400796{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400797 return mState.getDepthAttachment();
Geoff Lang646559f2013-08-15 11:08:15 -0400798}
799
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400800const FramebufferAttachment *Framebuffer::getStencilbuffer() const
801{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400802 return mState.getStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400803}
804
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400805const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
806{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400807 return mState.getDepthStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400808}
809
810const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000811{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400812 return mState.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000813}
814
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500815const FramebufferAttachment *Framebuffer::getStencilOrDepthStencilAttachment() const
816{
817 return mState.getStencilOrDepthStencilAttachment();
818}
819
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400820const FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000821{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400822 return mState.getReadAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000823}
824
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000825GLenum Framebuffer::getReadColorbufferType() const
826{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400827 const FramebufferAttachment *readAttachment = mState.getReadAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400828 return (readAttachment != nullptr ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000829}
830
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400831const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000832{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400833 return mState.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000834}
835
Jamie Madill6dd06ea2017-07-19 13:47:55 -0400836const FramebufferAttachment *Framebuffer::getFirstNonNullAttachment() const
837{
838 return mState.getFirstNonNullAttachment();
839}
840
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800841const FramebufferAttachment *Framebuffer::getAttachment(const Context *context,
842 GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000843{
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800844 return mState.getAttachment(context, attachment);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400845}
846
Geoff Langa15472a2015-08-11 11:48:03 -0400847size_t Framebuffer::getDrawbufferStateCount() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000848{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400849 return mState.mDrawBufferStates.size();
Geoff Langa15472a2015-08-11 11:48:03 -0400850}
851
852GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const
853{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400854 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
855 return mState.mDrawBufferStates[drawBuffer];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000856}
857
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500858const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const
859{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400860 return mState.getDrawBufferStates();
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500861}
862
Geoff Lang164d54e2014-12-01 10:55:33 -0500863void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000864{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400865 auto &drawStates = mState.mDrawBufferStates;
Jamie Madilld1405e52015-03-05 15:41:39 -0500866
867 ASSERT(count <= drawStates.size());
868 std::copy(buffers, buffers + count, drawStates.begin());
869 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500870 mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS);
Jamie Madilla4595b82017-01-11 17:36:34 -0500871
872 mState.mEnabledDrawBuffers.reset();
Brandon Jones76746f92017-11-22 11:44:41 -0800873 mState.mDrawBufferTypeMask.reset();
874
Jamie Madilla4595b82017-01-11 17:36:34 -0500875 for (size_t index = 0; index < count; ++index)
876 {
Brandon Jones76746f92017-11-22 11:44:41 -0800877 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(index), index);
878
Jamie Madilla4595b82017-01-11 17:36:34 -0500879 if (drawStates[index] != GL_NONE && mState.mColorAttachments[index].isAttached())
880 {
881 mState.mEnabledDrawBuffers.set(index);
882 }
883 }
Geoff Lang9dd95802014-12-01 11:12:59 -0500884}
885
Geoff Langa15472a2015-08-11 11:48:03 -0400886const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const
887{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400888 return mState.getDrawBuffer(drawBuffer);
Geoff Langa15472a2015-08-11 11:48:03 -0400889}
890
Geoff Lange0cff192017-05-30 13:04:56 -0400891GLenum Framebuffer::getDrawbufferWriteType(size_t drawBuffer) const
892{
893 const FramebufferAttachment *attachment = mState.getDrawBuffer(drawBuffer);
894 if (attachment == nullptr)
895 {
896 return GL_NONE;
897 }
898
899 GLenum componentType = attachment->getFormat().info->componentType;
900 switch (componentType)
901 {
902 case GL_INT:
903 case GL_UNSIGNED_INT:
904 return componentType;
905
906 default:
907 return GL_FLOAT;
908 }
909}
910
Brandon Jonesc405ae72017-12-06 14:15:03 -0800911ComponentTypeMask Framebuffer::getDrawBufferTypeMask() const
Brandon Jones76746f92017-11-22 11:44:41 -0800912{
913 return mState.mDrawBufferTypeMask;
914}
915
916DrawBufferMask Framebuffer::getDrawBufferMask() const
917{
918 return mState.mEnabledDrawBuffers;
919}
920
Geoff Langa15472a2015-08-11 11:48:03 -0400921bool Framebuffer::hasEnabledDrawBuffer() const
922{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400923 for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
Geoff Langa15472a2015-08-11 11:48:03 -0400924 {
925 if (getDrawBuffer(drawbufferIdx) != nullptr)
926 {
927 return true;
928 }
929 }
930
931 return false;
932}
933
Geoff Lang9dd95802014-12-01 11:12:59 -0500934GLenum Framebuffer::getReadBufferState() const
935{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400936 return mState.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500937}
938
939void Framebuffer::setReadBuffer(GLenum buffer)
940{
Jamie Madillb885e572015-02-03 16:16:04 -0500941 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
942 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madill48ef11b2016-04-27 15:21:52 -0400943 (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size()));
944 mState.mReadBufferState = buffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500945 mDirtyBits.set(DIRTY_BIT_READ_BUFFER);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000946}
947
Corentin Wallez37c39792015-08-20 14:19:46 -0400948size_t Framebuffer::getNumColorBuffers() const
949{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400950 return mState.mColorAttachments.size();
Corentin Wallez37c39792015-08-20 14:19:46 -0400951}
952
Jamie Madill0df8fe42015-11-24 16:10:24 -0500953bool Framebuffer::hasDepth() const
954{
Jamie Madill9c335862017-07-18 11:51:38 -0400955 return mState.hasDepth();
Jamie Madill0df8fe42015-11-24 16:10:24 -0500956}
957
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000958bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000959{
Jamie Madill9c335862017-07-18 11:51:38 -0400960 return mState.hasStencil();
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000961}
962
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000963bool Framebuffer::usingExtendedDrawBuffers() const
964{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400965 for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000966 {
Geoff Langa15472a2015-08-11 11:48:03 -0400967 if (getDrawBuffer(drawbufferIdx) != nullptr)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000968 {
969 return true;
970 }
971 }
972
973 return false;
974}
975
Geoff Lang9aded172017-04-05 11:07:56 -0400976void Framebuffer::invalidateCompletenessCache()
977{
978 if (mId != 0)
979 {
980 mCachedStatus.reset();
981 }
982}
983
Jamie Madille98b1b52018-03-08 09:47:23 -0500984Error Framebuffer::checkStatus(const Context *context, GLenum *statusOut)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985{
Corentin Wallezccab69d2017-01-27 16:57:15 -0500986 // The default framebuffer is always complete except when it is surfaceless in which
987 // case it is always unsupported. We return early because the default framebuffer may
988 // not be subject to the same rules as application FBOs. ie, it could have 0x0 size.
Geoff Lang528ce3c2014-12-01 10:44:07 -0500989 if (mId == 0)
990 {
Corentin Wallezccab69d2017-01-27 16:57:15 -0500991 ASSERT(mCachedStatus.valid());
992 ASSERT(mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE ||
993 mCachedStatus.value() == GL_FRAMEBUFFER_UNDEFINED_OES);
Jamie Madille98b1b52018-03-08 09:47:23 -0500994 *statusOut = mCachedStatus.value();
995 return NoError();
Geoff Lang528ce3c2014-12-01 10:44:07 -0500996 }
997
Jamie Madill362876b2016-06-16 14:46:59 -0400998 if (hasAnyDirtyBit() || !mCachedStatus.valid())
999 {
Jamie Madille98b1b52018-03-08 09:47:23 -05001000 mCachedStatus = checkStatusWithGLFrontEnd(context);
1001
1002 if (mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE)
1003 {
1004 ANGLE_TRY(syncState(context));
1005 if (!mImpl->checkStatus(context))
1006 {
1007 mCachedStatus = GL_FRAMEBUFFER_UNSUPPORTED;
1008 }
1009 }
Jamie Madill362876b2016-06-16 14:46:59 -04001010 }
1011
Jamie Madille98b1b52018-03-08 09:47:23 -05001012 *statusOut = mCachedStatus.value();
1013 return NoError();
Jamie Madill362876b2016-06-16 14:46:59 -04001014}
1015
Jamie Madille98b1b52018-03-08 09:47:23 -05001016GLenum Framebuffer::checkStatusWithGLFrontEnd(const Context *context)
Jamie Madill362876b2016-06-16 14:46:59 -04001017{
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001018 const ContextState &state = context->getContextState();
1019
Jamie Madill362876b2016-06-16 14:46:59 -04001020 ASSERT(mId != 0);
1021
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001022 bool hasAttachments = false;
1023 Optional<unsigned int> colorbufferSize;
1024 Optional<int> samples;
Geoff Lang92019432017-11-20 13:09:34 -05001025 Optional<bool> fixedSampleLocations;
JiangYizhou461d9a32017-01-04 16:37:26 +08001026 bool hasRenderbuffer = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027
Martin Radev9bc9a322017-07-21 14:28:17 +03001028 const FramebufferAttachment *firstAttachment = getFirstNonNullAttachment();
1029
Jamie Madill48ef11b2016-04-27 15:21:52 -04001030 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031 {
Jamie Madill2d06b732015-04-20 12:53:28 -04001032 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001034 if (!CheckAttachmentCompleteness(context, colorAttachment))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001035 {
1036 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1037 }
daniel@transgaming.com01868132010-08-24 19:21:17 +00001038
Geoff Lang677bb6f2017-04-05 12:40:40 -04001039 const InternalFormat &format = *colorAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001040 if (format.depthBits > 0 || format.stencilBits > 0)
1041 {
1042 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1043 }
1044
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001045 if (!CheckAttachmentSampleCompleteness(context, colorAttachment, true, &samples,
1046 &fixedSampleLocations))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001047 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001048 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001049 }
1050
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001051 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
1052 // in GLES 3.0, there is no such restriction
1053 if (state.getClientMajorVersion() < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001054 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001055 if (colorbufferSize.valid())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001056 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001057 if (format.pixelBytes != colorbufferSize.value())
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +00001058 {
1059 return GL_FRAMEBUFFER_UNSUPPORTED;
1060 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001061 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001062 else
1063 {
1064 colorbufferSize = format.pixelBytes;
1065 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001066 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001067
Martin Radev9bc9a322017-07-21 14:28:17 +03001068 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &colorAttachment))
1069 {
1070 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1071 }
1072
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001073 hasRenderbuffer = hasRenderbuffer || (colorAttachment.type() == GL_RENDERBUFFER);
1074 hasAttachments = true;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001075 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001076 }
1077
Jamie Madill48ef11b2016-04-27 15:21:52 -04001078 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001079 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001080 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001081 if (!CheckAttachmentCompleteness(context, depthAttachment))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001083 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084 }
1085
Geoff Lang677bb6f2017-04-05 12:40:40 -04001086 const InternalFormat &format = *depthAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001087 if (format.depthBits == 0)
1088 {
1089 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001090 }
1091
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001092 if (!CheckAttachmentSampleCompleteness(context, depthAttachment, false, &samples,
1093 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001094 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001095 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001096 }
Sami Väisänena797e062016-05-12 15:23:40 +03001097
Martin Radev9bc9a322017-07-21 14:28:17 +03001098 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &depthAttachment))
1099 {
1100 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1101 }
1102
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001103 hasRenderbuffer = hasRenderbuffer || (depthAttachment.type() == GL_RENDERBUFFER);
1104 hasAttachments = true;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001105 }
1106
Jamie Madill48ef11b2016-04-27 15:21:52 -04001107 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001108 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001109 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001110 if (!CheckAttachmentCompleteness(context, stencilAttachment))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001111 {
1112 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1113 }
1114
Geoff Lang677bb6f2017-04-05 12:40:40 -04001115 const InternalFormat &format = *stencilAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001116 if (format.stencilBits == 0)
1117 {
1118 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001119 }
1120
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001121 if (!CheckAttachmentSampleCompleteness(context, stencilAttachment, false, &samples,
1122 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001123 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001124 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001125 }
Corentin Wallez086d59a2016-04-29 09:06:49 -04001126
Martin Radev9bc9a322017-07-21 14:28:17 +03001127 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &stencilAttachment))
1128 {
1129 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1130 }
1131
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001132 hasRenderbuffer = hasRenderbuffer || (stencilAttachment.type() == GL_RENDERBUFFER);
1133 hasAttachments = true;
1134 }
1135
1136 // Starting from ES 3.0 stencil and depth, if present, should be the same image
1137 if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() &&
1138 stencilAttachment.isAttached() && stencilAttachment != depthAttachment)
1139 {
1140 return GL_FRAMEBUFFER_UNSUPPORTED;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001141 }
1142
Jamie Madilla02315b2017-02-23 14:14:47 -05001143 // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL.
1144 if (state.isWebGL1())
1145 {
1146 if (!mState.mWebGLDepthStencilConsistent)
1147 {
1148 return GL_FRAMEBUFFER_UNSUPPORTED;
1149 }
1150
1151 if (mState.mWebGLDepthStencilAttachment.isAttached())
1152 {
1153 if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 ||
1154 mState.mWebGLDepthStencilAttachment.getStencilSize() == 0)
1155 {
1156 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1157 }
Martin Radev9bc9a322017-07-21 14:28:17 +03001158
1159 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment,
1160 &mState.mWebGLDepthStencilAttachment))
1161 {
1162 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1163 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001164 }
1165 else if (mState.mStencilAttachment.isAttached() &&
1166 mState.mStencilAttachment.getDepthSize() > 0)
1167 {
1168 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1169 }
1170 else if (mState.mDepthAttachment.isAttached() &&
1171 mState.mDepthAttachment.getStencilSize() > 0)
1172 {
1173 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1174 }
1175 }
1176
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001177 // ES3.1(section 9.4) requires that if no image is attached to the framebuffer, and either the
1178 // value of the framebuffer's FRAMEBUFFER_DEFAULT_WIDTH or FRAMEBUFFER_DEFAULT_HEIGHT parameters
1179 // is zero, the framebuffer is considered incomplete.
JiangYizhou461d9a32017-01-04 16:37:26 +08001180 GLint defaultWidth = mState.getDefaultWidth();
1181 GLint defaultHeight = mState.getDefaultHeight();
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001182 if (!hasAttachments && (defaultWidth == 0 || defaultHeight == 0))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001183 {
1184 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001185 }
1186
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001187 // In ES 2.0 and WebGL, all color attachments must have the same width and height.
Jamie Madillcc86d642015-11-24 13:00:07 -05001188 // In ES 3.0, there is no such restriction.
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001189 if ((state.getClientMajorVersion() < 3 || state.getExtensions().webglCompatibility) &&
1190 !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -05001191 {
1192 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
1193 }
1194
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001195 // ES3.1(section 9.4) requires that if the attached images are a mix of renderbuffers and
1196 // textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all attached textures.
JiangYizhou461d9a32017-01-04 16:37:26 +08001197 if (fixedSampleLocations.valid() && hasRenderbuffer && !fixedSampleLocations.value())
1198 {
1199 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1200 }
1201
Kenneth Russellce8602a2017-10-03 18:23:08 -07001202 // The WebGL conformance tests implicitly define that all framebuffer
1203 // attachments must be unique. For example, the same level of a texture can
1204 // not be attached to two different color attachments.
1205 if (state.getExtensions().webglCompatibility)
1206 {
1207 if (!mState.colorAttachmentsAreUniqueImages())
1208 {
1209 return GL_FRAMEBUFFER_UNSUPPORTED;
1210 }
1211 }
1212
Jamie Madillcc86d642015-11-24 13:00:07 -05001213 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001214}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001215
Jamie Madill4928b7c2017-06-20 12:57:39 -04001216Error Framebuffer::discard(const Context *context, size_t count, const GLenum *attachments)
Austin Kinross08332632015-05-05 13:35:47 -07001217{
Jamie Madill05b35b22017-10-03 09:01:44 -04001218 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1219 // can be no-ops, so we should probably do that to ensure consistency.
1220 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1221
Jamie Madill4928b7c2017-06-20 12:57:39 -04001222 return mImpl->discard(context, count, attachments);
Austin Kinross08332632015-05-05 13:35:47 -07001223}
1224
Jamie Madill4928b7c2017-06-20 12:57:39 -04001225Error Framebuffer::invalidate(const Context *context, size_t count, const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001226{
Jamie Madill05b35b22017-10-03 09:01:44 -04001227 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1228 // can be no-ops, so we should probably do that to ensure consistency.
1229 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1230
Jamie Madill4928b7c2017-06-20 12:57:39 -04001231 return mImpl->invalidate(context, count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001232}
1233
Jamie Madill05b35b22017-10-03 09:01:44 -04001234bool Framebuffer::partialClearNeedsInit(const Context *context,
1235 bool color,
1236 bool depth,
1237 bool stencil)
1238{
1239 const auto &glState = context->getGLState();
1240
1241 if (!glState.isRobustResourceInitEnabled())
1242 {
1243 return false;
1244 }
1245
1246 // Scissors can affect clearing.
1247 // TODO(jmadill): Check for complete scissor overlap.
1248 if (glState.isScissorTestEnabled())
1249 {
1250 return true;
1251 }
1252
1253 // If colors masked, we must clear before we clear. Do a simple check.
1254 // TODO(jmadill): Filter out unused color channels from the test.
1255 if (color)
1256 {
1257 const auto &blend = glState.getBlendState();
1258 if (!(blend.colorMaskRed && blend.colorMaskGreen && blend.colorMaskBlue &&
1259 blend.colorMaskAlpha))
1260 {
1261 return true;
1262 }
1263 }
1264
1265 const auto &depthStencil = glState.getDepthStencilState();
1266 ASSERT(depthStencil.stencilBackMask == depthStencil.stencilMask);
1267 if (stencil && depthStencil.stencilMask != depthStencil.stencilWritemask)
1268 {
1269 return true;
1270 }
1271
1272 return false;
1273}
1274
Jamie Madill4928b7c2017-06-20 12:57:39 -04001275Error Framebuffer::invalidateSub(const Context *context,
1276 size_t count,
1277 const GLenum *attachments,
Jamie Madilld4442552018-02-27 22:03:47 -05001278 const Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -04001279{
Jamie Madill05b35b22017-10-03 09:01:44 -04001280 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1281 // can be no-ops, so we should probably do that to ensure consistency.
1282 // TODO(jmadill): Make a invalidate no-op in WebGL 2.0.
1283
Jamie Madill4928b7c2017-06-20 12:57:39 -04001284 return mImpl->invalidateSub(context, count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -04001285}
1286
Jamie Madilld4442552018-02-27 22:03:47 -05001287Error Framebuffer::clear(const Context *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -05001288{
Jamie Madill05b35b22017-10-03 09:01:44 -04001289 const auto &glState = context->getGLState();
1290 if (glState.isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -05001291 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001292 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001293 }
1294
Jamie Madill05b35b22017-10-03 09:01:44 -04001295 ANGLE_TRY(mImpl->clear(context, mask));
1296
Jamie Madill05b35b22017-10-03 09:01:44 -04001297 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001298}
1299
Jamie Madilld4442552018-02-27 22:03:47 -05001300Error Framebuffer::clearBufferfv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001301 GLenum buffer,
1302 GLint drawbuffer,
1303 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001304{
Jamie Madill05b35b22017-10-03 09:01:44 -04001305 if (context->getGLState().isRasterizerDiscardEnabled() ||
1306 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001307 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001308 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001309 }
1310
Jamie Madill05b35b22017-10-03 09:01:44 -04001311 ANGLE_TRY(mImpl->clearBufferfv(context, buffer, drawbuffer, values));
1312
Jamie Madill05b35b22017-10-03 09:01:44 -04001313 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001314}
1315
Jamie Madilld4442552018-02-27 22:03:47 -05001316Error Framebuffer::clearBufferuiv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001317 GLenum buffer,
1318 GLint drawbuffer,
1319 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001320{
Jamie Madill05b35b22017-10-03 09:01:44 -04001321 if (context->getGLState().isRasterizerDiscardEnabled() ||
1322 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001323 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001324 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001325 }
1326
Jamie Madill05b35b22017-10-03 09:01:44 -04001327 ANGLE_TRY(mImpl->clearBufferuiv(context, buffer, drawbuffer, values));
1328
Jamie Madill05b35b22017-10-03 09:01:44 -04001329 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001330}
1331
Jamie Madilld4442552018-02-27 22:03:47 -05001332Error Framebuffer::clearBufferiv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001333 GLenum buffer,
1334 GLint drawbuffer,
1335 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001336{
Jamie Madill05b35b22017-10-03 09:01:44 -04001337 if (context->getGLState().isRasterizerDiscardEnabled() ||
1338 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001339 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001340 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001341 }
1342
Jamie Madill05b35b22017-10-03 09:01:44 -04001343 ANGLE_TRY(mImpl->clearBufferiv(context, buffer, drawbuffer, values));
1344
Jamie Madill05b35b22017-10-03 09:01:44 -04001345 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001346}
1347
Jamie Madilld4442552018-02-27 22:03:47 -05001348Error Framebuffer::clearBufferfi(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001349 GLenum buffer,
1350 GLint drawbuffer,
1351 GLfloat depth,
1352 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -05001353{
Jamie Madill05b35b22017-10-03 09:01:44 -04001354 if (context->getGLState().isRasterizerDiscardEnabled() ||
1355 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001356 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001357 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001358 }
1359
Jamie Madill05b35b22017-10-03 09:01:44 -04001360 ANGLE_TRY(mImpl->clearBufferfi(context, buffer, drawbuffer, depth, stencil));
1361
Jamie Madill05b35b22017-10-03 09:01:44 -04001362 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001363}
1364
Jamie Madill4928b7c2017-06-20 12:57:39 -04001365GLenum Framebuffer::getImplementationColorReadFormat(const Context *context) const
Geoff Langbce529e2014-12-01 12:48:41 -05001366{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001367 return mImpl->getImplementationColorReadFormat(context);
Geoff Langbce529e2014-12-01 12:48:41 -05001368}
1369
Jamie Madill4928b7c2017-06-20 12:57:39 -04001370GLenum Framebuffer::getImplementationColorReadType(const Context *context) const
Geoff Langbce529e2014-12-01 12:48:41 -05001371{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001372 return mImpl->getImplementationColorReadType(context);
Geoff Langbce529e2014-12-01 12:48:41 -05001373}
1374
Jamie Madilld4442552018-02-27 22:03:47 -05001375Error Framebuffer::readPixels(const Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001376 const Rectangle &area,
Jamie Madill1b94d432015-08-07 13:23:23 -04001377 GLenum format,
1378 GLenum type,
Jamie Madill05b35b22017-10-03 09:01:44 -04001379 void *pixels)
Geoff Langbce529e2014-12-01 12:48:41 -05001380{
Jamie Madill05b35b22017-10-03 09:01:44 -04001381 ANGLE_TRY(ensureReadAttachmentInitialized(context, GL_COLOR_BUFFER_BIT));
Jamie Madill362876b2016-06-16 14:46:59 -04001382 ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels));
Geoff Lang520c4ae2015-05-05 13:12:36 -04001383
Jamie Madilld4442552018-02-27 22:03:47 -05001384 Buffer *unpackBuffer = context->getGLState().getTargetBuffer(BufferBinding::PixelUnpack);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001385 if (unpackBuffer)
1386 {
1387 unpackBuffer->onPixelUnpack();
1388 }
1389
Jamie Madill362876b2016-06-16 14:46:59 -04001390 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -05001391}
1392
Jamie Madilld4442552018-02-27 22:03:47 -05001393Error Framebuffer::blit(const Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001394 const Rectangle &sourceArea,
1395 const Rectangle &destArea,
Geoff Lang242468f2015-09-24 14:15:41 -04001396 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -04001397 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -05001398{
He Yunchao6be602d2016-12-22 14:33:07 +08001399 GLbitfield blitMask = mask;
1400
1401 // Note that blitting is called against draw framebuffer.
1402 // See the code in gl::Context::blitFramebuffer.
1403 if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer())
1404 {
1405 blitMask &= ~GL_COLOR_BUFFER_BIT;
1406 }
1407
1408 if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr)
1409 {
1410 blitMask &= ~GL_STENCIL_BUFFER_BIT;
1411 }
1412
1413 if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr)
1414 {
1415 blitMask &= ~GL_DEPTH_BUFFER_BIT;
1416 }
1417
1418 if (!blitMask)
1419 {
1420 return NoError();
1421 }
1422
Jamie Madill05b35b22017-10-03 09:01:44 -04001423 auto *sourceFBO = context->getGLState().getReadFramebuffer();
1424 ANGLE_TRY(sourceFBO->ensureReadAttachmentInitialized(context, blitMask));
1425
1426 // TODO(jmadill): Only clear if not the full FBO dimensions, and only specified bitmask.
1427 ANGLE_TRY(ensureDrawAttachmentsInitialized(context));
1428
He Yunchao6be602d2016-12-22 14:33:07 +08001429 return mImpl->blit(context, sourceArea, destArea, blitMask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -05001430}
1431
Jamie Madille98b1b52018-03-08 09:47:23 -05001432Error Framebuffer::getSamples(const Context *context, int *samplesOut)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001433{
Jamie Madille98b1b52018-03-08 09:47:23 -05001434 bool completeness = false;
1435 ANGLE_TRY(isComplete(context, &completeness));
1436 *samplesOut = completeness ? getCachedSamples(context) : 0;
1437 return NoError();
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001438}
1439
Jamie Madill9c335862017-07-18 11:51:38 -04001440int Framebuffer::getCachedSamples(const Context *context)
1441{
Jamie Madill5b772312018-03-08 20:28:32 -05001442 ASSERT(mCachedStatus.valid() && mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE);
1443
Jamie Madill9c335862017-07-18 11:51:38 -04001444 // For a complete framebuffer, all attachments must have the same sample count.
1445 // In this case return the first nonzero sample size.
1446 const auto *firstNonNullAttachment = mState.getFirstNonNullAttachment();
1447 if (firstNonNullAttachment)
1448 {
1449 ASSERT(firstNonNullAttachment->isAttached());
1450 return firstNonNullAttachment->getSamples();
1451 }
1452
1453 // No attachments found.
1454 return 0;
1455}
1456
Corentin Wallezccab69d2017-01-27 16:57:15 -05001457Error Framebuffer::getSamplePosition(size_t index, GLfloat *xy) const
1458{
1459 ANGLE_TRY(mImpl->getSamplePosition(index, xy));
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001460 return NoError();
Corentin Wallezccab69d2017-01-27 16:57:15 -05001461}
1462
Jamie Madille261b442014-06-25 12:42:21 -04001463bool Framebuffer::hasValidDepthStencil() const
1464{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001465 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -04001466}
1467
Jamie Madilla02315b2017-02-23 14:14:47 -05001468void Framebuffer::setAttachment(const Context *context,
1469 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -04001470 GLenum binding,
1471 const ImageIndex &textureIndex,
1472 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -04001473{
Martin Radev5dae57b2017-07-14 16:15:55 +03001474 setAttachment(context, type, binding, textureIndex, resource,
1475 FramebufferAttachment::kDefaultNumViews,
1476 FramebufferAttachment::kDefaultBaseViewIndex,
1477 FramebufferAttachment::kDefaultMultiviewLayout,
1478 FramebufferAttachment::kDefaultViewportOffsets);
1479}
1480
1481void Framebuffer::setAttachment(const Context *context,
1482 GLenum type,
1483 GLenum binding,
1484 const ImageIndex &textureIndex,
1485 FramebufferAttachmentObject *resource,
1486 GLsizei numViews,
1487 GLuint baseViewIndex,
1488 GLenum multiviewLayout,
1489 const GLint *viewportOffsets)
1490{
Jamie Madilla02315b2017-02-23 14:14:47 -05001491 // Context may be null in unit tests.
1492 if (!context || !context->isWebGL1())
1493 {
Martin Radev5dae57b2017-07-14 16:15:55 +03001494 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1495 multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001496 return;
1497 }
1498
1499 switch (binding)
1500 {
1501 case GL_DEPTH_STENCIL:
1502 case GL_DEPTH_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001503 mState.mWebGLDepthStencilAttachment.attach(context, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001504 resource, numViews, baseViewIndex,
1505 multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001506 break;
1507 case GL_DEPTH:
1508 case GL_DEPTH_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001509 mState.mWebGLDepthAttachment.attach(context, type, binding, textureIndex, resource,
1510 numViews, baseViewIndex, multiviewLayout,
1511 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001512 break;
1513 case GL_STENCIL:
1514 case GL_STENCIL_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001515 mState.mWebGLStencilAttachment.attach(context, type, binding, textureIndex, resource,
1516 numViews, baseViewIndex, multiviewLayout,
1517 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001518 break;
1519 default:
Martin Radev5dae57b2017-07-14 16:15:55 +03001520 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews,
1521 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001522 return;
1523 }
1524
Martin Radev5dae57b2017-07-14 16:15:55 +03001525 commitWebGL1DepthStencilIfConsistent(context, numViews, baseViewIndex, multiviewLayout,
1526 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001527}
1528
Martin Radev82ef7742017-08-08 17:44:58 +03001529void Framebuffer::setAttachmentMultiviewLayered(const Context *context,
1530 GLenum type,
1531 GLenum binding,
1532 const ImageIndex &textureIndex,
1533 FramebufferAttachmentObject *resource,
1534 GLsizei numViews,
1535 GLint baseViewIndex)
1536{
1537 setAttachment(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1538 GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE,
1539 FramebufferAttachment::kDefaultViewportOffsets);
1540}
1541
Martin Radev5dae57b2017-07-14 16:15:55 +03001542void Framebuffer::setAttachmentMultiviewSideBySide(const Context *context,
1543 GLenum type,
1544 GLenum binding,
1545 const ImageIndex &textureIndex,
1546 FramebufferAttachmentObject *resource,
1547 GLsizei numViews,
1548 const GLint *viewportOffsets)
1549{
1550 setAttachment(context, type, binding, textureIndex, resource, numViews,
1551 FramebufferAttachment::kDefaultBaseViewIndex,
1552 GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE, viewportOffsets);
1553}
1554
1555void Framebuffer::commitWebGL1DepthStencilIfConsistent(const Context *context,
1556 GLsizei numViews,
1557 GLuint baseViewIndex,
1558 GLenum multiviewLayout,
1559 const GLint *viewportOffsets)
Jamie Madilla02315b2017-02-23 14:14:47 -05001560{
1561 int count = 0;
1562
1563 std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment,
1564 &mState.mWebGLDepthAttachment,
1565 &mState.mWebGLStencilAttachment}};
1566 for (FramebufferAttachment *attachment : attachments)
1567 {
1568 if (attachment->isAttached())
1569 {
1570 count++;
1571 }
1572 }
1573
1574 mState.mWebGLDepthStencilConsistent = (count <= 1);
1575 if (!mState.mWebGLDepthStencilConsistent)
1576 {
1577 // Inconsistent.
1578 return;
1579 }
1580
Geoff Lange466c552017-03-17 15:24:12 -04001581 auto getImageIndexIfTextureAttachment = [](const FramebufferAttachment &attachment) {
1582 if (attachment.type() == GL_TEXTURE)
1583 {
1584 return attachment.getTextureImageIndex();
1585 }
1586 else
1587 {
1588 return ImageIndex::MakeInvalid();
1589 }
1590 };
1591
Jamie Madilla02315b2017-02-23 14:14:47 -05001592 if (mState.mWebGLDepthAttachment.isAttached())
1593 {
1594 const auto &depth = mState.mWebGLDepthAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001595 setAttachmentImpl(context, depth.type(), GL_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001596 getImageIndexIfTextureAttachment(depth), depth.getResource(), numViews,
1597 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001598 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(),
Martin Radev5dae57b2017-07-14 16:15:55 +03001599 nullptr, numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001600 }
1601 else if (mState.mWebGLStencilAttachment.isAttached())
1602 {
1603 const auto &stencil = mState.mWebGLStencilAttachment;
Martin Radev5dae57b2017-07-14 16:15:55 +03001604 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr,
1605 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001606 setAttachmentImpl(context, stencil.type(), GL_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001607 getImageIndexIfTextureAttachment(stencil), stencil.getResource(),
1608 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001609 }
1610 else if (mState.mWebGLDepthStencilAttachment.isAttached())
1611 {
1612 const auto &depthStencil = mState.mWebGLDepthStencilAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001613 setAttachmentImpl(context, depthStencil.type(), GL_DEPTH_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001614 getImageIndexIfTextureAttachment(depthStencil),
Martin Radev5dae57b2017-07-14 16:15:55 +03001615 depthStencil.getResource(), numViews, baseViewIndex, multiviewLayout,
1616 viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001617 setAttachmentImpl(context, depthStencil.type(), GL_STENCIL_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001618 getImageIndexIfTextureAttachment(depthStencil),
Martin Radev5dae57b2017-07-14 16:15:55 +03001619 depthStencil.getResource(), numViews, baseViewIndex, multiviewLayout,
1620 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001621 }
1622 else
1623 {
Martin Radev5dae57b2017-07-14 16:15:55 +03001624 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr,
1625 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001626 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(),
Martin Radev5dae57b2017-07-14 16:15:55 +03001627 nullptr, numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001628 }
1629}
1630
Jamie Madill4928b7c2017-06-20 12:57:39 -04001631void Framebuffer::setAttachmentImpl(const Context *context,
1632 GLenum type,
Jamie Madilla02315b2017-02-23 14:14:47 -05001633 GLenum binding,
1634 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001635 FramebufferAttachmentObject *resource,
1636 GLsizei numViews,
1637 GLuint baseViewIndex,
1638 GLenum multiviewLayout,
1639 const GLint *viewportOffsets)
Jamie Madilla02315b2017-02-23 14:14:47 -05001640{
Jamie Madilla02315b2017-02-23 14:14:47 -05001641 switch (binding)
1642 {
Jamie Madillb8126692017-04-05 11:22:17 -04001643 case GL_DEPTH_STENCIL:
1644 case GL_DEPTH_STENCIL_ATTACHMENT:
1645 {
1646 // ensure this is a legitimate depth+stencil format
1647 FramebufferAttachmentObject *attachmentObj = resource;
1648 if (resource)
1649 {
Jamie Madill4fd95d52017-04-05 11:22:18 -04001650 const Format &format = resource->getAttachmentFormat(binding, textureIndex);
Jamie Madillb8126692017-04-05 11:22:17 -04001651 if (format.info->depthBits == 0 || format.info->stencilBits == 0)
1652 {
1653 // Attaching nullptr detaches the current attachment.
1654 attachmentObj = nullptr;
1655 }
1656 }
1657
Jamie Madill4928b7c2017-06-20 12:57:39 -04001658 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001659 &mDirtyDepthAttachmentBinding, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001660 attachmentObj, numViews, baseViewIndex, multiviewLayout,
1661 viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001662 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001663 &mDirtyStencilAttachmentBinding, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001664 attachmentObj, numViews, baseViewIndex, multiviewLayout,
1665 viewportOffsets);
Jamie Madill42975642017-10-12 12:31:51 -04001666 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001667 }
1668
Jamie Madilla02315b2017-02-23 14:14:47 -05001669 case GL_DEPTH:
1670 case GL_DEPTH_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001671 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001672 &mDirtyDepthAttachmentBinding, type, binding, textureIndex, resource,
1673 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madill2d06b732015-04-20 12:53:28 -04001674 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001675
Jamie Madilla02315b2017-02-23 14:14:47 -05001676 case GL_STENCIL:
1677 case GL_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001678 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001679 &mDirtyStencilAttachmentBinding, type, binding, textureIndex, resource,
1680 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001681 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001682
Jamie Madilla02315b2017-02-23 14:14:47 -05001683 case GL_BACK:
Geoff Lang8170eab2017-09-21 13:59:04 -04001684 updateAttachment(context, &mState.mColorAttachments[0], DIRTY_BIT_COLOR_ATTACHMENT_0,
1685 &mDirtyColorAttachmentBindings[0], type, binding, textureIndex,
1686 resource, numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001687 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001688
Jamie Madilla02315b2017-02-23 14:14:47 -05001689 default:
1690 {
1691 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
1692 ASSERT(colorIndex < mState.mColorAttachments.size());
Jamie Madillb8126692017-04-05 11:22:17 -04001693 size_t dirtyBit = DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001694 updateAttachment(context, &mState.mColorAttachments[colorIndex], dirtyBit,
Jamie Madillb8126692017-04-05 11:22:17 -04001695 &mDirtyColorAttachmentBindings[colorIndex], type, binding,
Martin Radev5dae57b2017-07-14 16:15:55 +03001696 textureIndex, resource, numViews, baseViewIndex, multiviewLayout,
1697 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001698
Corentin Walleze7557742017-06-01 13:09:57 -04001699 // TODO(jmadill): ASSERT instead of checking the attachment exists in
1700 // formsRenderingFeedbackLoopWith
Jamie Madilla02315b2017-02-23 14:14:47 -05001701 bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE);
1702 mState.mEnabledDrawBuffers.set(colorIndex, enabled);
Brandon Jones76746f92017-11-22 11:44:41 -08001703 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(colorIndex), colorIndex);
Jamie Madill2d06b732015-04-20 12:53:28 -04001704 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001705 break;
Geoff Langab75a052014-10-15 12:56:37 -04001706 }
Jamie Madill42975642017-10-12 12:31:51 -04001707
1708 mAttachedTextures.reset();
Geoff Langab75a052014-10-15 12:56:37 -04001709}
1710
Jamie Madill4928b7c2017-06-20 12:57:39 -04001711void Framebuffer::updateAttachment(const Context *context,
1712 FramebufferAttachment *attachment,
Jamie Madillb8126692017-04-05 11:22:17 -04001713 size_t dirtyBit,
Jamie Madilld4442552018-02-27 22:03:47 -05001714 angle::ObserverBinding *onDirtyBinding,
Jamie Madillb8126692017-04-05 11:22:17 -04001715 GLenum type,
1716 GLenum binding,
1717 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001718 FramebufferAttachmentObject *resource,
1719 GLsizei numViews,
1720 GLuint baseViewIndex,
1721 GLenum multiviewLayout,
1722 const GLint *viewportOffsets)
Jamie Madillb8126692017-04-05 11:22:17 -04001723{
Martin Radev5dae57b2017-07-14 16:15:55 +03001724 attachment->attach(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1725 multiviewLayout, viewportOffsets);
Jamie Madillb8126692017-04-05 11:22:17 -04001726 mDirtyBits.set(dirtyBit);
Jamie Madill05b35b22017-10-03 09:01:44 -04001727 mState.mResourceNeedsInit.set(dirtyBit, attachment->initState() == InitState::MayNeedInit);
Jamie Madill888081d2018-02-27 00:24:46 -05001728 onDirtyBinding->bind(resource ? resource->getSubject() : nullptr);
Jamie Madille98b1b52018-03-08 09:47:23 -05001729
1730 invalidateCompletenessCache();
Jamie Madillb8126692017-04-05 11:22:17 -04001731}
1732
Jamie Madilla02315b2017-02-23 14:14:47 -05001733void Framebuffer::resetAttachment(const Context *context, GLenum binding)
Jamie Madill2d06b732015-04-20 12:53:28 -04001734{
Jamie Madilla02315b2017-02-23 14:14:47 -05001735 setAttachment(context, GL_NONE, binding, ImageIndex::MakeInvalid(), nullptr);
Jamie Madill2d06b732015-04-20 12:53:28 -04001736}
1737
Jamie Madill19fa1c62018-03-08 09:47:21 -05001738Error Framebuffer::syncState(const Context *context)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001739{
1740 if (mDirtyBits.any())
1741 {
Jamie Madill888081d2018-02-27 00:24:46 -05001742 mDirtyBitsGuard = mDirtyBits;
Jamie Madill19fa1c62018-03-08 09:47:21 -05001743 ANGLE_TRY(mImpl->syncState(context, mDirtyBits));
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001744 mDirtyBits.reset();
Jamie Madill888081d2018-02-27 00:24:46 -05001745 mDirtyBitsGuard.reset();
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001746 }
Jamie Madill19fa1c62018-03-08 09:47:21 -05001747 return NoError();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001749
Jamie Madilld4442552018-02-27 22:03:47 -05001750void Framebuffer::onSubjectStateChange(const Context *context,
1751 angle::SubjectIndex index,
1752 angle::SubjectMessage message)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001753{
Jamie Madill888081d2018-02-27 00:24:46 -05001754 if (message == angle::SubjectMessage::DEPENDENT_DIRTY_BITS)
1755 {
1756 ASSERT(!mDirtyBitsGuard.valid() || mDirtyBitsGuard.value().test(index));
1757 mDirtyBits.set(index);
1758 context->getGLState().setFramebufferDirty(this);
1759 return;
1760 }
1761
Geoff Lang8170eab2017-09-21 13:59:04 -04001762 // Only reset the cached status if this is not the default framebuffer. The default framebuffer
1763 // will still use this channel to mark itself dirty.
1764 if (mId != 0)
1765 {
1766 // TOOD(jmadill): Make this only update individual attachments to do less work.
1767 mCachedStatus.reset();
1768 }
Jamie Madill05b35b22017-10-03 09:01:44 -04001769
Jamie Madilld4442552018-02-27 22:03:47 -05001770 FramebufferAttachment *attachment = getAttachmentFromSubjectIndex(index);
1771
Jamie Madill05b35b22017-10-03 09:01:44 -04001772 // Mark the appropriate init flag.
Jamie Madilld4442552018-02-27 22:03:47 -05001773 mState.mResourceNeedsInit.set(index, attachment->initState() == InitState::MayNeedInit);
1774}
1775
1776FramebufferAttachment *Framebuffer::getAttachmentFromSubjectIndex(angle::SubjectIndex index)
1777{
1778 switch (index)
1779 {
1780 case DIRTY_BIT_DEPTH_ATTACHMENT:
1781 return &mState.mDepthAttachment;
1782 case DIRTY_BIT_STENCIL_ATTACHMENT:
1783 return &mState.mStencilAttachment;
1784 default:
1785 size_t colorIndex = (index - DIRTY_BIT_COLOR_ATTACHMENT_0);
1786 ASSERT(colorIndex < mState.mColorAttachments.size());
1787 return &mState.mColorAttachments[colorIndex];
1788 }
Jamie Madill51f40ec2016-06-15 14:06:00 -04001789}
1790
Jamie Madille98b1b52018-03-08 09:47:23 -05001791Error Framebuffer::isComplete(const Context *context, bool *completeOut)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001792{
Jamie Madille98b1b52018-03-08 09:47:23 -05001793 GLenum status = GL_NONE;
1794 ANGLE_TRY(checkStatus(context, &status));
1795 *completeOut = (status == GL_FRAMEBUFFER_COMPLETE);
1796 return NoError();
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001797}
1798
Jamie Madilla4595b82017-01-11 17:36:34 -05001799bool Framebuffer::formsRenderingFeedbackLoopWith(const State &state) const
1800{
1801 const Program *program = state.getProgram();
1802
1803 // TODO(jmadill): Default framebuffer feedback loops.
1804 if (mId == 0)
1805 {
1806 return false;
1807 }
1808
1809 // The bitset will skip inactive draw buffers.
Jamie Madill6de51852017-04-12 09:53:01 -04001810 for (size_t drawIndex : mState.mEnabledDrawBuffers)
Jamie Madilla4595b82017-01-11 17:36:34 -05001811 {
Jamie Madill5aca1932017-07-21 12:22:01 -04001812 const FramebufferAttachment &attachment = mState.mColorAttachments[drawIndex];
1813 ASSERT(attachment.isAttached());
1814 if (attachment.type() == GL_TEXTURE)
Jamie Madilla4595b82017-01-11 17:36:34 -05001815 {
1816 // Validate the feedback loop.
Jamie Madill5aca1932017-07-21 12:22:01 -04001817 if (program->samplesFromTexture(state, attachment.id()))
Jamie Madilla4595b82017-01-11 17:36:34 -05001818 {
1819 return true;
1820 }
1821 }
1822 }
1823
Jamie Madill1d37bc52017-02-02 19:59:58 -05001824 // Validate depth-stencil feedback loop.
1825 const auto &dsState = state.getDepthStencilState();
1826
1827 // We can skip the feedback loop checks if depth/stencil is masked out or disabled.
1828 const FramebufferAttachment *depth = getDepthbuffer();
1829 if (depth && depth->type() == GL_TEXTURE && dsState.depthTest && dsState.depthMask)
1830 {
1831 if (program->samplesFromTexture(state, depth->id()))
1832 {
1833 return true;
1834 }
1835 }
1836
1837 // Note: we assume the front and back masks are the same for WebGL.
1838 const FramebufferAttachment *stencil = getStencilbuffer();
1839 ASSERT(dsState.stencilBackWritemask == dsState.stencilWritemask);
1840 if (stencil && stencil->type() == GL_TEXTURE && dsState.stencilTest &&
1841 dsState.stencilWritemask != 0)
1842 {
1843 // Skip the feedback loop check if depth/stencil point to the same resource.
1844 if (!depth || *stencil != *depth)
1845 {
1846 if (program->samplesFromTexture(state, stencil->id()))
1847 {
1848 return true;
1849 }
1850 }
1851 }
1852
Jamie Madilla4595b82017-01-11 17:36:34 -05001853 return false;
1854}
1855
Jamie Madillfd3dd432017-02-02 19:59:59 -05001856bool Framebuffer::formsCopyingFeedbackLoopWith(GLuint copyTextureID,
1857 GLint copyTextureLevel,
1858 GLint copyTextureLayer) const
Jamie Madillf695a3a2017-01-11 17:36:35 -05001859{
1860 if (mId == 0)
1861 {
1862 // It seems impossible to form a texture copying feedback loop with the default FBO.
1863 return false;
1864 }
1865
1866 const FramebufferAttachment *readAttachment = getReadColorbuffer();
1867 ASSERT(readAttachment);
1868
1869 if (readAttachment->isTextureWithId(copyTextureID))
1870 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001871 const auto &imageIndex = readAttachment->getTextureImageIndex();
1872 if (imageIndex.mipIndex == copyTextureLevel)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001873 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001874 // Check 3D/Array texture layers.
1875 return imageIndex.layerIndex == ImageIndex::ENTIRE_LEVEL ||
1876 copyTextureLayer == ImageIndex::ENTIRE_LEVEL ||
1877 imageIndex.layerIndex == copyTextureLayer;
Jamie Madillf695a3a2017-01-11 17:36:35 -05001878 }
1879 }
1880 return false;
1881}
1882
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001883GLint Framebuffer::getDefaultWidth() const
1884{
1885 return mState.getDefaultWidth();
1886}
1887
1888GLint Framebuffer::getDefaultHeight() const
1889{
1890 return mState.getDefaultHeight();
1891}
1892
1893GLint Framebuffer::getDefaultSamples() const
1894{
1895 return mState.getDefaultSamples();
1896}
1897
Geoff Lang92019432017-11-20 13:09:34 -05001898bool Framebuffer::getDefaultFixedSampleLocations() const
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001899{
1900 return mState.getDefaultFixedSampleLocations();
1901}
1902
1903void Framebuffer::setDefaultWidth(GLint defaultWidth)
1904{
1905 mState.mDefaultWidth = defaultWidth;
1906 mDirtyBits.set(DIRTY_BIT_DEFAULT_WIDTH);
Jamie Madille98b1b52018-03-08 09:47:23 -05001907 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001908}
1909
1910void Framebuffer::setDefaultHeight(GLint defaultHeight)
1911{
1912 mState.mDefaultHeight = defaultHeight;
1913 mDirtyBits.set(DIRTY_BIT_DEFAULT_HEIGHT);
Jamie Madille98b1b52018-03-08 09:47:23 -05001914 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001915}
1916
1917void Framebuffer::setDefaultSamples(GLint defaultSamples)
1918{
1919 mState.mDefaultSamples = defaultSamples;
1920 mDirtyBits.set(DIRTY_BIT_DEFAULT_SAMPLES);
Jamie Madille98b1b52018-03-08 09:47:23 -05001921 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001922}
1923
Geoff Lang92019432017-11-20 13:09:34 -05001924void Framebuffer::setDefaultFixedSampleLocations(bool defaultFixedSampleLocations)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001925{
1926 mState.mDefaultFixedSampleLocations = defaultFixedSampleLocations;
1927 mDirtyBits.set(DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS);
Jamie Madille98b1b52018-03-08 09:47:23 -05001928 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001929}
1930
Martin Radev14a26ae2017-07-24 15:56:29 +03001931GLsizei Framebuffer::getNumViews() const
1932{
Martin Radev5c00d0d2017-08-07 10:06:59 +03001933 return mState.getNumViews();
Martin Radev14a26ae2017-07-24 15:56:29 +03001934}
1935
Martin Radev4e619f52017-08-09 11:50:06 +03001936GLint Framebuffer::getBaseViewIndex() const
1937{
1938 return mState.getBaseViewIndex();
1939}
1940
Martin Radev878c8b12017-07-28 09:51:04 +03001941const std::vector<Offset> *Framebuffer::getViewportOffsets() const
1942{
Martin Radev5c00d0d2017-08-07 10:06:59 +03001943 return mState.getViewportOffsets();
Martin Radev878c8b12017-07-28 09:51:04 +03001944}
1945
1946GLenum Framebuffer::getMultiviewLayout() const
1947{
Martin Radev5c00d0d2017-08-07 10:06:59 +03001948 return mState.getMultiviewLayout();
Martin Radev878c8b12017-07-28 09:51:04 +03001949}
1950
Geoff Langd4fff502017-09-22 11:28:28 -04001951Error Framebuffer::ensureClearAttachmentsInitialized(const Context *context, GLbitfield mask)
1952{
1953 const auto &glState = context->getGLState();
1954 if (!context->isRobustResourceInitEnabled() || glState.isRasterizerDiscardEnabled())
1955 {
1956 return NoError();
1957 }
1958
Geoff Langa36483f2018-03-09 16:11:21 -05001959 const BlendState &blend = glState.getBlendState();
1960 const DepthStencilState &depthStencil = glState.getDepthStencilState();
Geoff Langd4fff502017-09-22 11:28:28 -04001961
1962 bool color = (mask & GL_COLOR_BUFFER_BIT) != 0 && !IsColorMaskedOut(blend);
1963 bool depth = (mask & GL_DEPTH_BUFFER_BIT) != 0 && !IsDepthMaskedOut(depthStencil);
1964 bool stencil = (mask & GL_STENCIL_BUFFER_BIT) != 0 && !IsStencilMaskedOut(depthStencil);
1965
1966 if (!color && !depth && !stencil)
1967 {
1968 return NoError();
1969 }
1970
1971 if (partialClearNeedsInit(context, color, depth, stencil))
1972 {
1973 ANGLE_TRY(ensureDrawAttachmentsInitialized(context));
1974 }
1975
1976 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
1977 // still be marked initialized. This simplifies design, allowing this method to be called before
1978 // the clear.
1979 markDrawAttachmentsInitialized(color, depth, stencil);
1980
1981 return NoError();
1982}
1983
1984Error Framebuffer::ensureClearBufferAttachmentsInitialized(const Context *context,
1985 GLenum buffer,
1986 GLint drawbuffer)
1987{
1988 if (!context->isRobustResourceInitEnabled() ||
1989 context->getGLState().isRasterizerDiscardEnabled() ||
1990 IsClearBufferMaskedOut(context, buffer))
1991 {
1992 return NoError();
1993 }
1994
1995 if (partialBufferClearNeedsInit(context, buffer))
1996 {
1997 ANGLE_TRY(ensureBufferInitialized(context, buffer, drawbuffer));
1998 }
1999
2000 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2001 // still be marked initialized. This simplifies design, allowing this method to be called before
2002 // the clear.
2003 markBufferInitialized(buffer, drawbuffer);
2004
2005 return NoError();
2006}
2007
Jamie Madill05b35b22017-10-03 09:01:44 -04002008Error Framebuffer::ensureDrawAttachmentsInitialized(const Context *context)
2009{
2010 if (!context->isRobustResourceInitEnabled())
2011 {
2012 return NoError();
2013 }
2014
2015 // Note: we don't actually filter by the draw attachment enum. Just init everything.
2016 for (size_t bit : mState.mResourceNeedsInit)
2017 {
2018 switch (bit)
2019 {
2020 case DIRTY_BIT_DEPTH_ATTACHMENT:
2021 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2022 break;
2023 case DIRTY_BIT_STENCIL_ATTACHMENT:
2024 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2025 break;
2026 default:
2027 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bit]));
2028 break;
2029 }
2030 }
2031
2032 mState.mResourceNeedsInit.reset();
2033 return NoError();
2034}
2035
2036Error Framebuffer::ensureReadAttachmentInitialized(const Context *context, GLbitfield blitMask)
2037{
2038 if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none())
2039 {
2040 return NoError();
2041 }
2042
2043 if ((blitMask & GL_COLOR_BUFFER_BIT) != 0 && mState.mReadBufferState != GL_NONE)
2044 {
2045 size_t readIndex = mState.getReadIndex();
2046 if (mState.mResourceNeedsInit[readIndex])
2047 {
2048 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[readIndex]));
2049 mState.mResourceNeedsInit.reset(readIndex);
2050 }
2051 }
2052
2053 if ((blitMask & GL_DEPTH_BUFFER_BIT) != 0 && hasDepth())
2054 {
2055 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2056 {
2057 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2058 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2059 }
2060 }
2061
2062 if ((blitMask & GL_STENCIL_BUFFER_BIT) != 0 && hasStencil())
2063 {
2064 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2065 {
2066 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2067 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2068 }
2069 }
2070
2071 return NoError();
2072}
2073
2074void Framebuffer::markDrawAttachmentsInitialized(bool color, bool depth, bool stencil)
2075{
2076 // Mark attachments as initialized.
2077 if (color)
2078 {
2079 for (auto colorIndex : mState.mEnabledDrawBuffers)
2080 {
2081 auto &colorAttachment = mState.mColorAttachments[colorIndex];
2082 ASSERT(colorAttachment.isAttached());
2083 colorAttachment.setInitState(InitState::Initialized);
2084 mState.mResourceNeedsInit.reset(colorIndex);
2085 }
2086 }
2087
2088 if (depth && mState.mDepthAttachment.isAttached())
2089 {
2090 mState.mDepthAttachment.setInitState(InitState::Initialized);
2091 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2092 }
2093
2094 if (stencil && mState.mStencilAttachment.isAttached())
2095 {
2096 mState.mStencilAttachment.setInitState(InitState::Initialized);
2097 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2098 }
2099}
2100
2101void Framebuffer::markBufferInitialized(GLenum bufferType, GLint bufferIndex)
2102{
2103 switch (bufferType)
2104 {
2105 case GL_COLOR:
2106 {
2107 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2108 if (mState.mColorAttachments[bufferIndex].isAttached())
2109 {
2110 mState.mColorAttachments[bufferIndex].setInitState(InitState::Initialized);
2111 mState.mResourceNeedsInit.reset(bufferIndex);
2112 }
2113 break;
2114 }
2115 case GL_DEPTH:
2116 {
2117 if (mState.mDepthAttachment.isAttached())
2118 {
2119 mState.mDepthAttachment.setInitState(InitState::Initialized);
2120 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2121 }
2122 break;
2123 }
2124 case GL_STENCIL:
2125 {
2126 if (mState.mStencilAttachment.isAttached())
2127 {
2128 mState.mStencilAttachment.setInitState(InitState::Initialized);
2129 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2130 }
2131 break;
2132 }
2133 case GL_DEPTH_STENCIL:
2134 {
2135 if (mState.mDepthAttachment.isAttached())
2136 {
2137 mState.mDepthAttachment.setInitState(InitState::Initialized);
2138 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2139 }
2140 if (mState.mStencilAttachment.isAttached())
2141 {
2142 mState.mStencilAttachment.setInitState(InitState::Initialized);
2143 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2144 }
2145 break;
2146 }
2147 default:
2148 UNREACHABLE();
2149 break;
2150 }
2151}
2152
2153Box Framebuffer::getDimensions() const
2154{
2155 return mState.getDimensions();
2156}
2157
2158Error Framebuffer::ensureBufferInitialized(const Context *context,
2159 GLenum bufferType,
2160 GLint bufferIndex)
2161{
2162 ASSERT(context->isRobustResourceInitEnabled());
2163
2164 if (mState.mResourceNeedsInit.none())
2165 {
2166 return NoError();
2167 }
2168
2169 switch (bufferType)
2170 {
2171 case GL_COLOR:
2172 {
2173 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2174 if (mState.mResourceNeedsInit[bufferIndex])
2175 {
2176 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bufferIndex]));
2177 mState.mResourceNeedsInit.reset(bufferIndex);
2178 }
2179 break;
2180 }
2181 case GL_DEPTH:
2182 {
2183 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2184 {
2185 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2186 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2187 }
2188 break;
2189 }
2190 case GL_STENCIL:
2191 {
2192 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2193 {
2194 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2195 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2196 }
2197 break;
2198 }
2199 case GL_DEPTH_STENCIL:
2200 {
2201 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2202 {
2203 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2204 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2205 }
2206 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2207 {
2208 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2209 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2210 }
2211 break;
2212 }
2213 default:
2214 UNREACHABLE();
2215 break;
2216 }
2217
2218 return NoError();
2219}
2220
2221bool Framebuffer::partialBufferClearNeedsInit(const Context *context, GLenum bufferType)
2222{
2223 if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none())
2224 {
2225 return false;
2226 }
2227
2228 switch (bufferType)
2229 {
2230 case GL_COLOR:
2231 return partialClearNeedsInit(context, true, false, false);
2232 case GL_DEPTH:
2233 return partialClearNeedsInit(context, false, true, false);
2234 case GL_STENCIL:
2235 return partialClearNeedsInit(context, false, false, true);
2236 case GL_DEPTH_STENCIL:
2237 return partialClearNeedsInit(context, false, true, true);
2238 default:
2239 UNREACHABLE();
2240 return false;
2241 }
2242}
2243
Jamie Madill42975642017-10-12 12:31:51 -04002244bool Framebuffer::hasTextureAttachment(const Texture *texture) const
2245{
2246 if (!mAttachedTextures.valid())
2247 {
2248 std::set<const FramebufferAttachmentObject *> attachedTextures;
2249
2250 for (const auto &colorAttachment : mState.mColorAttachments)
2251 {
2252 if (colorAttachment.isAttached() && colorAttachment.type() == GL_TEXTURE)
2253 {
2254 attachedTextures.insert(colorAttachment.getResource());
2255 }
2256 }
2257
2258 if (mState.mDepthAttachment.isAttached() && mState.mDepthAttachment.type() == GL_TEXTURE)
2259 {
2260 attachedTextures.insert(mState.mDepthAttachment.getResource());
2261 }
2262
2263 if (mState.mStencilAttachment.isAttached() &&
2264 mState.mStencilAttachment.type() == GL_TEXTURE)
2265 {
2266 attachedTextures.insert(mState.mStencilAttachment.getResource());
2267 }
2268
2269 mAttachedTextures = std::move(attachedTextures);
2270 }
2271
2272 return (mAttachedTextures.value().count(texture) > 0);
2273}
2274
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002275} // namespace gl