blob: 64acc8a4cd38200b76d1421b09477872aa274a44 [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 }
Mingyu Hu7d64c482019-03-12 14:27:40 -070052 if (firstAttachment->isMultiview() != secondAttachment->isMultiview())
Martin Radev9bc9a322017-07-21 14:28:17 +030053 {
54 return false;
55 }
56 return true;
57}
58
Geoff Lang9f10b772017-05-16 15:51:03 -040059bool CheckAttachmentCompleteness(const Context *context, const FramebufferAttachment &attachment)
60{
61 ASSERT(attachment.isAttached());
62
63 const Extents &size = attachment.getSize();
64 if (size.width == 0 || size.height == 0)
65 {
66 return false;
67 }
68
Yuly Novikov2eb54072018-08-22 16:41:26 -040069 if (!attachment.isRenderable(context))
Geoff Lang9f10b772017-05-16 15:51:03 -040070 {
71 return false;
72 }
73
74 if (attachment.type() == GL_TEXTURE)
75 {
Jiawei Shaoa8802472018-05-28 11:17:47 +080076 // [EXT_geometry_shader] Section 9.4.1, "Framebuffer Completeness"
77 // If <image> is a three-dimensional texture or a two-dimensional array texture and the
78 // attachment is not layered, the selected layer is less than the depth or layer count,
79 // respectively, of the texture.
80 if (!attachment.isLayered())
Geoff Lang9f10b772017-05-16 15:51:03 -040081 {
Jiawei Shaoa8802472018-05-28 11:17:47 +080082 if (attachment.layer() >= size.depth)
83 {
84 return false;
85 }
86 }
87 // If <image> is a three-dimensional texture or a two-dimensional array texture and the
88 // attachment is layered, the depth or layer count, respectively, of the texture is less
89 // than or equal to the value of MAX_FRAMEBUFFER_LAYERS_EXT.
90 else
91 {
92 if (static_cast<GLuint>(size.depth) >= context->getCaps().maxFramebufferLayers)
93 {
94 return false;
95 }
Geoff Lang9f10b772017-05-16 15:51:03 -040096 }
97
98 // ES3 specifies that cube map texture attachments must be cube complete.
99 // This language is missing from the ES2 spec, but we enforce it here because some
100 // desktop OpenGL drivers also enforce this validation.
101 // TODO(jmadill): Check if OpenGL ES2 drivers enforce cube completeness.
102 const Texture *texture = attachment.getTexture();
103 ASSERT(texture);
Corentin Wallez99d492c2018-02-27 15:17:10 -0500104 if (texture->getType() == TextureType::CubeMap &&
Geoff Lang9f10b772017-05-16 15:51:03 -0400105 !texture->getTextureState().isCubeComplete())
106 {
107 return false;
108 }
Geoff Lang857c09d2017-05-16 15:55:04 -0400109
110 if (!texture->getImmutableFormat())
111 {
112 GLuint attachmentMipLevel = static_cast<GLuint>(attachment.mipLevel());
113
114 // From the ES 3.0 spec, pg 213:
115 // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of
116 // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture,
117 // then the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL must be in the
118 // range[levelbase, q], where levelbase is the value of TEXTURE_BASE_LEVEL and q is
119 // the effective maximum texture level defined in the Mipmapping discussion of
120 // section 3.8.10.4.
121 if (attachmentMipLevel < texture->getBaseLevel() ||
122 attachmentMipLevel > texture->getMipmapMaxLevel())
123 {
124 return false;
125 }
126
127 // Form the ES 3.0 spec, pg 213/214:
128 // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of
129 // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture and
130 // the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL is not levelbase, then the
131 // texture must be mipmap complete, and if FRAMEBUFFER_ATTACHMENT_OBJECT_NAME names
132 // a cubemap texture, the texture must also be cube complete.
133 if (attachmentMipLevel != texture->getBaseLevel() && !texture->isMipmapComplete())
134 {
135 return false;
136 }
137 }
Geoff Lang9f10b772017-05-16 15:51:03 -0400138 }
139
140 return true;
Jamie Madillc09ae152019-02-01 14:16:32 -0500141}
Geoff Lang9f10b772017-05-16 15:51:03 -0400142
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400143bool CheckAttachmentSampleCompleteness(const Context *context,
144 const FramebufferAttachment &attachment,
145 bool colorAttachment,
146 Optional<int> *samples,
Geoff Lang92019432017-11-20 13:09:34 -0500147 Optional<bool> *fixedSampleLocations)
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400148{
149 ASSERT(attachment.isAttached());
150
151 if (attachment.type() == GL_TEXTURE)
152 {
153 const Texture *texture = attachment.getTexture();
154 ASSERT(texture);
155
156 const ImageIndex &attachmentImageIndex = attachment.getTextureImageIndex();
Jiawei Shaoa8802472018-05-28 11:17:47 +0800157 bool fixedSampleloc = texture->getAttachmentFixedSampleLocations(attachmentImageIndex);
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400158 if (fixedSampleLocations->valid() && fixedSampleloc != fixedSampleLocations->value())
159 {
160 return false;
161 }
162 else
163 {
164 *fixedSampleLocations = fixedSampleloc;
165 }
166 }
167
168 if (samples->valid())
169 {
170 if (attachment.getSamples() != samples->value())
171 {
172 if (colorAttachment)
173 {
174 // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that
175 // all color attachments have the same number of samples for the FBO to be complete.
176 return false;
177 }
178 else
179 {
180 // CHROMIUM_framebuffer_mixed_samples allows a framebuffer to be considered complete
181 // when its depth or stencil samples are a multiple of the number of color samples.
182 if (!context->getExtensions().framebufferMixedSamples)
183 {
184 return false;
185 }
186
187 if ((attachment.getSamples() % std::max(samples->value(), 1)) != 0)
188 {
189 return false;
190 }
191 }
192 }
193 }
194 else
195 {
196 *samples = attachment.getSamples();
197 }
198
199 return true;
200}
201
Jamie Madill05b35b22017-10-03 09:01:44 -0400202// Needed to index into the attachment arrays/bitsets.
Jamie Madill682efdc2017-10-03 14:10:29 -0400203static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500204 Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX,
Jamie Madill05b35b22017-10-03 09:01:44 -0400205 "Framebuffer Dirty bit mismatch");
Jamie Madill682efdc2017-10-03 14:10:29 -0400206static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500207 Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madill05b35b22017-10-03 09:01:44 -0400208 "Framebuffer Dirty bit mismatch");
Jamie Madill682efdc2017-10-03 14:10:29 -0400209static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS + 1) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500210 Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madill05b35b22017-10-03 09:01:44 -0400211 "Framebuffer Dirty bit mismatch");
212
Jamie Madill6f755b22018-10-09 12:48:54 -0400213angle::Result InitAttachment(const Context *context, FramebufferAttachment *attachment)
Jamie Madill05b35b22017-10-03 09:01:44 -0400214{
215 ASSERT(attachment->isAttached());
216 if (attachment->initState() == InitState::MayNeedInit)
217 {
218 ANGLE_TRY(attachment->initializeContents(context));
219 }
Jamie Madill7c985f52018-11-29 18:16:17 -0500220 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -0400221}
222
223bool IsColorMaskedOut(const BlendState &blend)
224{
225 return (!blend.colorMaskRed && !blend.colorMaskGreen && !blend.colorMaskBlue &&
226 !blend.colorMaskAlpha);
227}
228
229bool IsDepthMaskedOut(const DepthStencilState &depthStencil)
230{
231 return !depthStencil.depthMask;
232}
233
234bool IsStencilMaskedOut(const DepthStencilState &depthStencil)
235{
236 return ((depthStencil.stencilMask & depthStencil.stencilWritemask) == 0);
237}
238
239bool IsClearBufferMaskedOut(const Context *context, GLenum buffer)
240{
241 switch (buffer)
242 {
243 case GL_COLOR:
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500244 return IsColorMaskedOut(context->getState().getBlendState());
Jamie Madill05b35b22017-10-03 09:01:44 -0400245 case GL_DEPTH:
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500246 return IsDepthMaskedOut(context->getState().getDepthStencilState());
Jamie Madill05b35b22017-10-03 09:01:44 -0400247 case GL_STENCIL:
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500248 return IsStencilMaskedOut(context->getState().getDepthStencilState());
Jamie Madill05b35b22017-10-03 09:01:44 -0400249 case GL_DEPTH_STENCIL:
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500250 return IsDepthMaskedOut(context->getState().getDepthStencilState()) &&
251 IsStencilMaskedOut(context->getState().getDepthStencilState());
Jamie Madill05b35b22017-10-03 09:01:44 -0400252 default:
253 UNREACHABLE();
254 return true;
255 }
256}
257
Jamie Madill362876b2016-06-16 14:46:59 -0400258} // anonymous namespace
Jamie Madilld1405e52015-03-05 15:41:39 -0500259
Jamie Madill6f60d052017-02-22 15:20:11 -0500260// This constructor is only used for default framebuffers.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400261FramebufferState::FramebufferState()
Jamie Madill2274b652018-05-31 10:56:08 -0400262 : mId(0),
263 mLabel(),
Geoff Lang70d0f492015-12-10 17:45:46 -0500264 mColorAttachments(1),
Corentin Walleze7557742017-06-01 13:09:57 -0400265 mDrawBufferStates(1, GL_BACK),
Jamie Madill6f60d052017-02-22 15:20:11 -0500266 mReadBufferState(GL_BACK),
Brandon Jones76746f92017-11-22 11:44:41 -0800267 mDrawBufferTypeMask(),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800268 mDefaultWidth(0),
269 mDefaultHeight(0),
270 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500271 mDefaultFixedSampleLocations(GL_FALSE),
Jiawei Shaob1e91382018-05-17 14:33:55 +0800272 mDefaultLayers(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500273 mWebGLDepthStencilConsistent(true)
Corentin Wallez37c39792015-08-20 14:19:46 -0400274{
Geoff Langd90d3882017-03-21 10:49:54 -0400275 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilla4595b82017-01-11 17:36:34 -0500276 mEnabledDrawBuffers.set(0);
Corentin Wallez37c39792015-08-20 14:19:46 -0400277}
278
Jamie Madill2274b652018-05-31 10:56:08 -0400279FramebufferState::FramebufferState(const Caps &caps, GLuint id)
280 : mId(id),
281 mLabel(),
Geoff Lang70d0f492015-12-10 17:45:46 -0500282 mColorAttachments(caps.maxColorAttachments),
Jamie Madilld1405e52015-03-05 15:41:39 -0500283 mDrawBufferStates(caps.maxDrawBuffers, GL_NONE),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800284 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT),
Brandon Jones76746f92017-11-22 11:44:41 -0800285 mDrawBufferTypeMask(),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800286 mDefaultWidth(0),
287 mDefaultHeight(0),
288 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500289 mDefaultFixedSampleLocations(GL_FALSE),
Jiawei Shaob1e91382018-05-17 14:33:55 +0800290 mDefaultLayers(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500291 mWebGLDepthStencilConsistent(true)
Jamie Madilld1405e52015-03-05 15:41:39 -0500292{
Jamie Madill2274b652018-05-31 10:56:08 -0400293 ASSERT(mId != 0);
Geoff Langa15472a2015-08-11 11:48:03 -0400294 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilld1405e52015-03-05 15:41:39 -0500295 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
296}
297
Jamie Madillb980c562018-11-27 11:34:27 -0500298FramebufferState::~FramebufferState() {}
Jamie Madilld1405e52015-03-05 15:41:39 -0500299
Jamie Madill48ef11b2016-04-27 15:21:52 -0400300const std::string &FramebufferState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500301{
302 return mLabel;
303}
304
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800305const FramebufferAttachment *FramebufferState::getAttachment(const Context *context,
306 GLenum attachment) const
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400307{
308 if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15)
309 {
310 return getColorAttachment(attachment - GL_COLOR_ATTACHMENT0);
311 }
312
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800313 // WebGL1 allows a developer to query for attachment parameters even when "inconsistant" (i.e.
314 // multiple conflicting attachment points) and requires us to return the framebuffer attachment
315 // associated with WebGL.
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400316 switch (attachment)
317 {
318 case GL_COLOR:
319 case GL_BACK:
320 return getColorAttachment(0);
321 case GL_DEPTH:
322 case GL_DEPTH_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800323 if (context->isWebGL1())
324 {
325 return getWebGLDepthAttachment();
326 }
327 else
328 {
329 return getDepthAttachment();
330 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400331 case GL_STENCIL:
332 case GL_STENCIL_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800333 if (context->isWebGL1())
334 {
335 return getWebGLStencilAttachment();
336 }
337 else
338 {
339 return getStencilAttachment();
340 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400341 case GL_DEPTH_STENCIL:
342 case GL_DEPTH_STENCIL_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800343 if (context->isWebGL1())
344 {
345 return getWebGLDepthStencilAttachment();
346 }
347 else
348 {
349 return getDepthStencilAttachment();
350 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400351 default:
352 UNREACHABLE();
353 return nullptr;
354 }
355}
356
Jamie Madill05b35b22017-10-03 09:01:44 -0400357size_t FramebufferState::getReadIndex() const
Jamie Madill7147f012015-03-05 15:41:40 -0500358{
Jamie Madill231c7f52017-04-26 13:45:37 -0400359 ASSERT(mReadBufferState == GL_BACK ||
360 (mReadBufferState >= GL_COLOR_ATTACHMENT0 && mReadBufferState <= GL_COLOR_ATTACHMENT15));
361 size_t readIndex = (mReadBufferState == GL_BACK
362 ? 0
363 : static_cast<size_t>(mReadBufferState - GL_COLOR_ATTACHMENT0));
Jamie Madill7147f012015-03-05 15:41:40 -0500364 ASSERT(readIndex < mColorAttachments.size());
Jamie Madill05b35b22017-10-03 09:01:44 -0400365 return readIndex;
366}
367
368const FramebufferAttachment *FramebufferState::getReadAttachment() const
369{
370 if (mReadBufferState == GL_NONE)
371 {
372 return nullptr;
373 }
374 size_t readIndex = getReadIndex();
Jamie Madill2d06b732015-04-20 12:53:28 -0400375 return mColorAttachments[readIndex].isAttached() ? &mColorAttachments[readIndex] : nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500376}
377
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500378const FramebufferAttachment *FramebufferState::getFirstNonNullAttachment() const
379{
380 auto *colorAttachment = getFirstColorAttachment();
381 if (colorAttachment)
382 {
383 return colorAttachment;
384 }
385 return getDepthOrStencilAttachment();
386}
387
Jamie Madill48ef11b2016-04-27 15:21:52 -0400388const FramebufferAttachment *FramebufferState::getFirstColorAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500389{
Jamie Madill2d06b732015-04-20 12:53:28 -0400390 for (const FramebufferAttachment &colorAttachment : mColorAttachments)
Jamie Madill7147f012015-03-05 15:41:40 -0500391 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400392 if (colorAttachment.isAttached())
Jamie Madill7147f012015-03-05 15:41:40 -0500393 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400394 return &colorAttachment;
Jamie Madill7147f012015-03-05 15:41:40 -0500395 }
396 }
397
398 return nullptr;
399}
400
Jamie Madill48ef11b2016-04-27 15:21:52 -0400401const FramebufferAttachment *FramebufferState::getDepthOrStencilAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500402{
Jamie Madill2d06b732015-04-20 12:53:28 -0400403 if (mDepthAttachment.isAttached())
404 {
405 return &mDepthAttachment;
406 }
407 if (mStencilAttachment.isAttached())
408 {
409 return &mStencilAttachment;
410 }
411 return nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500412}
413
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500414const FramebufferAttachment *FramebufferState::getStencilOrDepthStencilAttachment() const
415{
416 if (mStencilAttachment.isAttached())
417 {
418 return &mStencilAttachment;
419 }
420 return getDepthStencilAttachment();
421}
422
Jamie Madill48ef11b2016-04-27 15:21:52 -0400423const FramebufferAttachment *FramebufferState::getColorAttachment(size_t colorAttachment) const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400424{
425 ASSERT(colorAttachment < mColorAttachments.size());
Jamie Madill231c7f52017-04-26 13:45:37 -0400426 return mColorAttachments[colorAttachment].isAttached() ? &mColorAttachments[colorAttachment]
427 : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400428}
429
Jamie Madill48ef11b2016-04-27 15:21:52 -0400430const FramebufferAttachment *FramebufferState::getDepthAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400431{
Jamie Madill2d06b732015-04-20 12:53:28 -0400432 return mDepthAttachment.isAttached() ? &mDepthAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400433}
434
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800435const FramebufferAttachment *FramebufferState::getWebGLDepthAttachment() const
436{
437 return mWebGLDepthAttachment.isAttached() ? &mWebGLDepthAttachment : nullptr;
438}
439
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800440const FramebufferAttachment *FramebufferState::getWebGLDepthStencilAttachment() const
441{
442 return mWebGLDepthStencilAttachment.isAttached() ? &mWebGLDepthStencilAttachment : nullptr;
443}
444
Jamie Madill48ef11b2016-04-27 15:21:52 -0400445const FramebufferAttachment *FramebufferState::getStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400446{
Jamie Madill2d06b732015-04-20 12:53:28 -0400447 return mStencilAttachment.isAttached() ? &mStencilAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400448}
449
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800450const FramebufferAttachment *FramebufferState::getWebGLStencilAttachment() const
451{
452 return mWebGLStencilAttachment.isAttached() ? &mWebGLStencilAttachment : nullptr;
453}
454
Jamie Madill48ef11b2016-04-27 15:21:52 -0400455const FramebufferAttachment *FramebufferState::getDepthStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400456{
457 // A valid depth-stencil attachment has the same resource bound to both the
458 // depth and stencil attachment points.
Jamie Madill2d06b732015-04-20 12:53:28 -0400459 if (mDepthAttachment.isAttached() && mStencilAttachment.isAttached() &&
Jamie Madill44f26482016-11-18 12:49:15 -0500460 mDepthAttachment == mStencilAttachment)
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400461 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400462 return &mDepthAttachment;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400463 }
464
465 return nullptr;
466}
467
Jamie Madill48ef11b2016-04-27 15:21:52 -0400468bool FramebufferState::attachmentsHaveSameDimensions() const
Jamie Madillcc86d642015-11-24 13:00:07 -0500469{
470 Optional<Extents> attachmentSize;
471
Jamie Madill231c7f52017-04-26 13:45:37 -0400472 auto hasMismatchedSize = [&attachmentSize](const FramebufferAttachment &attachment) {
Jamie Madillcc86d642015-11-24 13:00:07 -0500473 if (!attachment.isAttached())
474 {
475 return false;
476 }
477
478 if (!attachmentSize.valid())
479 {
480 attachmentSize = attachment.getSize();
481 return false;
482 }
483
Jeff Gilbert8f8edd62017-10-31 14:26:30 -0700484 const auto &prevSize = attachmentSize.value();
485 const auto &curSize = attachment.getSize();
486 return (curSize.width != prevSize.width || curSize.height != prevSize.height);
Jamie Madillcc86d642015-11-24 13:00:07 -0500487 };
488
489 for (const auto &attachment : mColorAttachments)
490 {
491 if (hasMismatchedSize(attachment))
492 {
493 return false;
494 }
495 }
496
497 if (hasMismatchedSize(mDepthAttachment))
498 {
499 return false;
500 }
501
502 return !hasMismatchedSize(mStencilAttachment);
503}
504
Luc Ferron5bdf8bd2018-06-20 09:51:37 -0400505bool FramebufferState::hasSeparateDepthAndStencilAttachments() const
506{
507 // if we have both a depth and stencil buffer, they must refer to the same object
508 // since we only support packed_depth_stencil and not separate depth and stencil
509 return (getDepthAttachment() != nullptr && getStencilAttachment() != nullptr &&
510 getDepthStencilAttachment() == nullptr);
511}
512
Jamie Madilld4442552018-02-27 22:03:47 -0500513const FramebufferAttachment *FramebufferState::getDrawBuffer(size_t drawBufferIdx) const
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400514{
515 ASSERT(drawBufferIdx < mDrawBufferStates.size());
516 if (mDrawBufferStates[drawBufferIdx] != GL_NONE)
517 {
518 // ES3 spec: "If the GL is bound to a draw framebuffer object, the ith buffer listed in bufs
519 // must be COLOR_ATTACHMENTi or NONE"
520 ASSERT(mDrawBufferStates[drawBufferIdx] == GL_COLOR_ATTACHMENT0 + drawBufferIdx ||
521 (drawBufferIdx == 0 && mDrawBufferStates[drawBufferIdx] == GL_BACK));
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800522
523 if (mDrawBufferStates[drawBufferIdx] == GL_BACK)
524 {
525 return getColorAttachment(0);
526 }
527 else
528 {
529 return getColorAttachment(mDrawBufferStates[drawBufferIdx] - GL_COLOR_ATTACHMENT0);
530 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400531 }
532 else
533 {
534 return nullptr;
535 }
536}
537
538size_t FramebufferState::getDrawBufferCount() const
539{
540 return mDrawBufferStates.size();
541}
542
Geoff Langb21e20d2016-07-19 15:35:41 -0400543bool FramebufferState::colorAttachmentsAreUniqueImages() const
544{
545 for (size_t firstAttachmentIdx = 0; firstAttachmentIdx < mColorAttachments.size();
546 firstAttachmentIdx++)
547 {
Jamie Madilld4442552018-02-27 22:03:47 -0500548 const FramebufferAttachment &firstAttachment = mColorAttachments[firstAttachmentIdx];
Geoff Langb21e20d2016-07-19 15:35:41 -0400549 if (!firstAttachment.isAttached())
550 {
551 continue;
552 }
553
554 for (size_t secondAttachmentIdx = firstAttachmentIdx + 1;
555 secondAttachmentIdx < mColorAttachments.size(); secondAttachmentIdx++)
556 {
Jamie Madilld4442552018-02-27 22:03:47 -0500557 const FramebufferAttachment &secondAttachment = mColorAttachments[secondAttachmentIdx];
Geoff Langb21e20d2016-07-19 15:35:41 -0400558 if (!secondAttachment.isAttached())
559 {
560 continue;
561 }
562
563 if (firstAttachment == secondAttachment)
564 {
565 return false;
566 }
567 }
568 }
569
570 return true;
571}
572
Jamie Madill9c335862017-07-18 11:51:38 -0400573bool FramebufferState::hasDepth() const
574{
575 return (mDepthAttachment.isAttached() && mDepthAttachment.getDepthSize() > 0);
576}
577
578bool FramebufferState::hasStencil() const
579{
580 return (mStencilAttachment.isAttached() && mStencilAttachment.getStencilSize() > 0);
581}
582
Mingyu Hu7d64c482019-03-12 14:27:40 -0700583bool FramebufferState::isMultiview() const
Martin Radev5c00d0d2017-08-07 10:06:59 +0300584{
585 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
586 if (attachment == nullptr)
587 {
Mingyu Hu7d64c482019-03-12 14:27:40 -0700588 return false;
Martin Radev5c00d0d2017-08-07 10:06:59 +0300589 }
Mingyu Hu7d64c482019-03-12 14:27:40 -0700590 return attachment->isMultiview();
Martin Radev5c00d0d2017-08-07 10:06:59 +0300591}
592
Martin Radev4e619f52017-08-09 11:50:06 +0300593int FramebufferState::getBaseViewIndex() const
594{
595 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
596 if (attachment == nullptr)
597 {
598 return GL_NONE;
599 }
600 return attachment->getBaseViewIndex();
601}
602
Jamie Madill05b35b22017-10-03 09:01:44 -0400603Box FramebufferState::getDimensions() const
604{
Jonah Ryan-Davis7151fe52019-07-17 15:15:27 -0400605 Extents extents = getExtents();
606 return Box(0, 0, 0, extents.width, extents.height, extents.depth);
607}
608
609Extents FramebufferState::getExtents() const
610{
Jamie Madill05b35b22017-10-03 09:01:44 -0400611 ASSERT(attachmentsHaveSameDimensions());
612 ASSERT(getFirstNonNullAttachment() != nullptr);
Jonah Ryan-Davis7151fe52019-07-17 15:15:27 -0400613 return getFirstNonNullAttachment()->getSize();
Jamie Madill05b35b22017-10-03 09:01:44 -0400614}
615
Jamie Madill7aea7e02016-05-10 10:39:45 -0400616Framebuffer::Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id)
Jamie Madill2274b652018-05-31 10:56:08 -0400617 : mState(caps, id),
Jamie Madill362876b2016-06-16 14:46:59 -0400618 mImpl(factory->createFramebuffer(mState)),
Jamie Madill362876b2016-06-16 14:46:59 -0400619 mCachedStatus(),
620 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
621 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622{
Corentin Wallez37c39792015-08-20 14:19:46 -0400623 ASSERT(mImpl != nullptr);
Jamie Madill362876b2016-06-16 14:46:59 -0400624 ASSERT(mState.mColorAttachments.size() == static_cast<size_t>(caps.maxColorAttachments));
625
Jamie Madill1e5499d2017-04-05 11:22:16 -0400626 for (uint32_t colorIndex = 0;
627 colorIndex < static_cast<uint32_t>(mState.mColorAttachments.size()); ++colorIndex)
Jamie Madill362876b2016-06-16 14:46:59 -0400628 {
Jamie Madill1e5499d2017-04-05 11:22:16 -0400629 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
Jamie Madill362876b2016-06-16 14:46:59 -0400630 }
Corentin Wallez37c39792015-08-20 14:19:46 -0400631}
632
Geoff Langbf7b95d2018-05-01 16:48:21 -0400633Framebuffer::Framebuffer(const Context *context, egl::Surface *surface)
Jamie Madill362876b2016-06-16 14:46:59 -0400634 : mState(),
Geoff Langbf7b95d2018-05-01 16:48:21 -0400635 mImpl(surface->getImplementation()->createDefaultFramebuffer(context, mState)),
Jamie Madill362876b2016-06-16 14:46:59 -0400636 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
Geoff Langbf7b95d2018-05-01 16:48:21 -0400643 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400644 FramebufferAttachment::kDefaultNumViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -0700645 FramebufferAttachment::kDefaultBaseViewIndex, false);
Jamie Madill6f60d052017-02-22 15:20:11 -0500646
647 if (surface->getConfig()->depthSize > 0)
648 {
Geoff Langbf7b95d2018-05-01 16:48:21 -0400649 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400650 FramebufferAttachment::kDefaultNumViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -0700651 FramebufferAttachment::kDefaultBaseViewIndex, false);
Jamie Madill6f60d052017-02-22 15:20:11 -0500652 }
653
654 if (surface->getConfig()->stencilSize > 0)
655 {
Geoff Langbf7b95d2018-05-01 16:48:21 -0400656 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400657 FramebufferAttachment::kDefaultNumViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -0700658 FramebufferAttachment::kDefaultBaseViewIndex, false);
Jamie Madill6f60d052017-02-22 15:20:11 -0500659 }
Jamie Madill6e18a232019-01-16 13:27:14 -0500660 SetComponentTypeMask(getDrawbufferWriteType(0), 0, &mState.mDrawBufferTypeMask);
Geoff Langee244c72019-05-06 10:30:18 -0400661
662 // Ensure the backend has a chance to synchronize its content for a new backbuffer.
663 mDirtyBits.set(DIRTY_BIT_COLOR_BUFFER_CONTENTS_0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664}
665
Corentin Wallezccab69d2017-01-27 16:57:15 -0500666Framebuffer::Framebuffer(rx::GLImplFactory *factory)
667 : mState(),
668 mImpl(factory->createFramebuffer(mState)),
Corentin Wallezccab69d2017-01-27 16:57:15 -0500669 mCachedStatus(GL_FRAMEBUFFER_UNDEFINED_OES),
670 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
671 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
672{
Jamie Madill1e5499d2017-04-05 11:22:16 -0400673 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Jamie Madill6e18a232019-01-16 13:27:14 -0500674 SetComponentTypeMask(getDrawbufferWriteType(0), 0, &mState.mDrawBufferTypeMask);
Corentin Wallezccab69d2017-01-27 16:57:15 -0500675}
676
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000677Framebuffer::~Framebuffer()
678{
Geoff Langda88add2014-12-01 10:22:01 -0500679 SafeDelete(mImpl);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000680}
681
Jamie Madill4928b7c2017-06-20 12:57:39 -0400682void Framebuffer::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500683{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400684 for (auto &attachment : mState.mColorAttachments)
685 {
686 attachment.detach(context);
687 }
688 mState.mDepthAttachment.detach(context);
689 mState.mStencilAttachment.detach(context);
690 mState.mWebGLDepthAttachment.detach(context);
691 mState.mWebGLStencilAttachment.detach(context);
692 mState.mWebGLDepthStencilAttachment.detach(context);
693
Jamie Madillc564c072017-06-01 12:45:42 -0400694 mImpl->destroy(context);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500695}
696
Jamie Madille90d4ee2018-11-28 14:04:00 -0500697void Framebuffer::setLabel(const Context *context, const std::string &label)
Geoff Lang70d0f492015-12-10 17:45:46 -0500698{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400699 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500700}
701
702const std::string &Framebuffer::getLabel() const
703{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400704 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500705}
706
Jamie Madill8693bdb2017-09-02 15:32:14 -0400707bool Framebuffer::detachTexture(const Context *context, GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000708{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400709 return detachResourceById(context, GL_TEXTURE, textureId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000710}
711
Jamie Madill8693bdb2017-09-02 15:32:14 -0400712bool Framebuffer::detachRenderbuffer(const Context *context, GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000713{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400714 return detachResourceById(context, GL_RENDERBUFFER, renderbufferId);
Jamie Madilld1405e52015-03-05 15:41:39 -0500715}
Jamie Madille261b442014-06-25 12:42:21 -0400716
Jamie Madill8693bdb2017-09-02 15:32:14 -0400717bool Framebuffer::detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId)
Jamie Madilld1405e52015-03-05 15:41:39 -0500718{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400719 bool found = false;
720
Jamie Madill362876b2016-06-16 14:46:59 -0400721 for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
Jamie Madilld1405e52015-03-05 15:41:39 -0500722 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400723 if (detachMatchingAttachment(context, &mState.mColorAttachments[colorIndex], resourceType,
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300724 resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400725 {
726 found = true;
727 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000728 }
729
Jamie Madilla02315b2017-02-23 14:14:47 -0500730 if (context->isWebGL1())
731 {
732 const std::array<FramebufferAttachment *, 3> attachments = {
733 {&mState.mWebGLDepthStencilAttachment, &mState.mWebGLDepthAttachment,
734 &mState.mWebGLStencilAttachment}};
735 for (FramebufferAttachment *attachment : attachments)
736 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300737 if (detachMatchingAttachment(context, attachment, resourceType, resourceId))
Jamie Madilla02315b2017-02-23 14:14:47 -0500738 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400739 found = true;
Jamie Madilla02315b2017-02-23 14:14:47 -0500740 }
741 }
742 }
743 else
744 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300745 if (detachMatchingAttachment(context, &mState.mDepthAttachment, resourceType, resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400746 {
747 found = true;
748 }
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300749 if (detachMatchingAttachment(context, &mState.mStencilAttachment, resourceType, resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400750 {
751 found = true;
752 }
Jamie Madilla02315b2017-02-23 14:14:47 -0500753 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400754
755 return found;
Jamie Madill362876b2016-06-16 14:46:59 -0400756}
757
Jamie Madill8693bdb2017-09-02 15:32:14 -0400758bool Framebuffer::detachMatchingAttachment(const Context *context,
Jamie Madill4928b7c2017-06-20 12:57:39 -0400759 FramebufferAttachment *attachment,
Jamie Madill362876b2016-06-16 14:46:59 -0400760 GLenum matchType,
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300761 GLuint matchId)
Jamie Madill362876b2016-06-16 14:46:59 -0400762{
763 if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId)
764 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300765 // We go through resetAttachment to make sure that all the required bookkeeping will be done
766 // such as updating enabled draw buffer state.
767 resetAttachment(context, attachment->getBinding());
Jamie Madill8693bdb2017-09-02 15:32:14 -0400768 return true;
Jamie Madill362876b2016-06-16 14:46:59 -0400769 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400770
771 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000772}
773
Jamie Madill4e71b2b2019-07-08 13:23:38 -0400774const FramebufferAttachment *Framebuffer::getColorAttachment(size_t colorAttachment) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400776 return mState.getColorAttachment(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000777}
778
Jamie Madill4e71b2b2019-07-08 13:23:38 -0400779const FramebufferAttachment *Framebuffer::getDepthAttachment() const
Geoff Lang646559f2013-08-15 11:08:15 -0400780{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400781 return mState.getDepthAttachment();
Geoff Lang646559f2013-08-15 11:08:15 -0400782}
783
Jamie Madill4e71b2b2019-07-08 13:23:38 -0400784const FramebufferAttachment *Framebuffer::getStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400785{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400786 return mState.getStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400787}
788
Jamie Madill4e71b2b2019-07-08 13:23:38 -0400789const FramebufferAttachment *Framebuffer::getDepthStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400790{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400791 return mState.getDepthStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400792}
793
Jamie Madill4e71b2b2019-07-08 13:23:38 -0400794const FramebufferAttachment *Framebuffer::getDepthOrStencilAttachment() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000795{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400796 return mState.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000797}
798
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500799const FramebufferAttachment *Framebuffer::getStencilOrDepthStencilAttachment() const
800{
801 return mState.getStencilOrDepthStencilAttachment();
802}
803
Jamie Madill4e71b2b2019-07-08 13:23:38 -0400804const FramebufferAttachment *Framebuffer::getReadColorAttachment() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000805{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400806 return mState.getReadAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000807}
808
Jamie Madill4e71b2b2019-07-08 13:23:38 -0400809GLenum Framebuffer::getReadColorAttachmentType() const
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000810{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400811 const FramebufferAttachment *readAttachment = mState.getReadAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400812 return (readAttachment != nullptr ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000813}
814
Jamie Madill4e71b2b2019-07-08 13:23:38 -0400815const FramebufferAttachment *Framebuffer::getFirstColorAttachment() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000816{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400817 return mState.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000818}
819
Jamie Madill6dd06ea2017-07-19 13:47:55 -0400820const FramebufferAttachment *Framebuffer::getFirstNonNullAttachment() const
821{
822 return mState.getFirstNonNullAttachment();
823}
824
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800825const FramebufferAttachment *Framebuffer::getAttachment(const Context *context,
826 GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000827{
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800828 return mState.getAttachment(context, attachment);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400829}
830
Geoff Langa15472a2015-08-11 11:48:03 -0400831size_t Framebuffer::getDrawbufferStateCount() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000832{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400833 return mState.mDrawBufferStates.size();
Geoff Langa15472a2015-08-11 11:48:03 -0400834}
835
836GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const
837{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400838 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
839 return mState.mDrawBufferStates[drawBuffer];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000840}
841
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500842const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const
843{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400844 return mState.getDrawBufferStates();
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500845}
846
Geoff Lang164d54e2014-12-01 10:55:33 -0500847void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000848{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400849 auto &drawStates = mState.mDrawBufferStates;
Jamie Madilld1405e52015-03-05 15:41:39 -0500850
851 ASSERT(count <= drawStates.size());
852 std::copy(buffers, buffers + count, drawStates.begin());
853 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500854 mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS);
Jamie Madilla4595b82017-01-11 17:36:34 -0500855
856 mState.mEnabledDrawBuffers.reset();
Brandon Jones76746f92017-11-22 11:44:41 -0800857 mState.mDrawBufferTypeMask.reset();
858
Jamie Madilla4595b82017-01-11 17:36:34 -0500859 for (size_t index = 0; index < count; ++index)
860 {
Jamie Madill6e18a232019-01-16 13:27:14 -0500861 SetComponentTypeMask(getDrawbufferWriteType(index), index, &mState.mDrawBufferTypeMask);
Brandon Jones76746f92017-11-22 11:44:41 -0800862
Jamie Madilla4595b82017-01-11 17:36:34 -0500863 if (drawStates[index] != GL_NONE && mState.mColorAttachments[index].isAttached())
864 {
865 mState.mEnabledDrawBuffers.set(index);
866 }
867 }
Geoff Lang9dd95802014-12-01 11:12:59 -0500868}
869
Geoff Langa15472a2015-08-11 11:48:03 -0400870const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const
871{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400872 return mState.getDrawBuffer(drawBuffer);
Geoff Langa15472a2015-08-11 11:48:03 -0400873}
874
Jamie Madill6e18a232019-01-16 13:27:14 -0500875ComponentType Framebuffer::getDrawbufferWriteType(size_t drawBuffer) const
Geoff Lange0cff192017-05-30 13:04:56 -0400876{
877 const FramebufferAttachment *attachment = mState.getDrawBuffer(drawBuffer);
878 if (attachment == nullptr)
879 {
Jamie Madill6e18a232019-01-16 13:27:14 -0500880 return ComponentType::NoType;
Geoff Lange0cff192017-05-30 13:04:56 -0400881 }
882
883 GLenum componentType = attachment->getFormat().info->componentType;
884 switch (componentType)
885 {
886 case GL_INT:
Jamie Madill6e18a232019-01-16 13:27:14 -0500887 return ComponentType::Int;
Geoff Lange0cff192017-05-30 13:04:56 -0400888 case GL_UNSIGNED_INT:
Jamie Madill6e18a232019-01-16 13:27:14 -0500889 return ComponentType::UnsignedInt;
Geoff Lange0cff192017-05-30 13:04:56 -0400890
891 default:
Jamie Madill6e18a232019-01-16 13:27:14 -0500892 return ComponentType::Float;
Geoff Lange0cff192017-05-30 13:04:56 -0400893 }
894}
895
Brandon Jonesc405ae72017-12-06 14:15:03 -0800896ComponentTypeMask Framebuffer::getDrawBufferTypeMask() const
Brandon Jones76746f92017-11-22 11:44:41 -0800897{
898 return mState.mDrawBufferTypeMask;
899}
900
901DrawBufferMask Framebuffer::getDrawBufferMask() const
902{
903 return mState.mEnabledDrawBuffers;
904}
905
Geoff Langa15472a2015-08-11 11:48:03 -0400906bool Framebuffer::hasEnabledDrawBuffer() const
907{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400908 for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
Geoff Langa15472a2015-08-11 11:48:03 -0400909 {
910 if (getDrawBuffer(drawbufferIdx) != nullptr)
911 {
912 return true;
913 }
914 }
915
916 return false;
917}
918
Geoff Lang9dd95802014-12-01 11:12:59 -0500919GLenum Framebuffer::getReadBufferState() const
920{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400921 return mState.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500922}
923
924void Framebuffer::setReadBuffer(GLenum buffer)
925{
Jamie Madillb885e572015-02-03 16:16:04 -0500926 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
927 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madill48ef11b2016-04-27 15:21:52 -0400928 (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size()));
929 mState.mReadBufferState = buffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500930 mDirtyBits.set(DIRTY_BIT_READ_BUFFER);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000931}
932
Jamie Madill4e71b2b2019-07-08 13:23:38 -0400933size_t Framebuffer::getNumColorAttachments() const
Corentin Wallez37c39792015-08-20 14:19:46 -0400934{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400935 return mState.mColorAttachments.size();
Corentin Wallez37c39792015-08-20 14:19:46 -0400936}
937
Jamie Madill0df8fe42015-11-24 16:10:24 -0500938bool Framebuffer::hasDepth() const
939{
Jamie Madill9c335862017-07-18 11:51:38 -0400940 return mState.hasDepth();
Jamie Madill0df8fe42015-11-24 16:10:24 -0500941}
942
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000943bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000944{
Jamie Madill9c335862017-07-18 11:51:38 -0400945 return mState.hasStencil();
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000946}
947
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000948bool Framebuffer::usingExtendedDrawBuffers() const
949{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400950 for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000951 {
Geoff Langa15472a2015-08-11 11:48:03 -0400952 if (getDrawBuffer(drawbufferIdx) != nullptr)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000953 {
954 return true;
955 }
956 }
957
958 return false;
959}
960
Jamie Madill124f78c2019-06-18 11:48:24 -0400961void Framebuffer::invalidateCompletenessCache()
Geoff Lang9aded172017-04-05 11:07:56 -0400962{
Jamie Madill2274b652018-05-31 10:56:08 -0400963 if (mState.mId != 0)
Geoff Lang9aded172017-04-05 11:07:56 -0400964 {
965 mCachedStatus.reset();
966 }
Jamie Madill124f78c2019-06-18 11:48:24 -0400967 onStateChange(angle::SubjectMessage::DirtyBitsFlagged);
Geoff Lang9aded172017-04-05 11:07:56 -0400968}
969
Jamie Madillcc73f242018-08-01 11:34:48 -0400970GLenum Framebuffer::checkStatusImpl(const Context *context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971{
Jamie Madillcc73f242018-08-01 11:34:48 -0400972 ASSERT(!isDefault());
973 ASSERT(hasAnyDirtyBit() || !mCachedStatus.valid());
Geoff Lang528ce3c2014-12-01 10:44:07 -0500974
Jamie Madillcc73f242018-08-01 11:34:48 -0400975 mCachedStatus = checkStatusWithGLFrontEnd(context);
Jamie Madille98b1b52018-03-08 09:47:23 -0500976
Jamie Madillcc73f242018-08-01 11:34:48 -0400977 if (mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE)
978 {
Jamie Madill67220092019-05-20 11:12:53 -0400979 // We can skip syncState on several back-ends.
980 if (mImpl->shouldSyncStateBeforeCheckStatus())
Jamie Madille98b1b52018-03-08 09:47:23 -0500981 {
Jamie Madill67220092019-05-20 11:12:53 -0400982 angle::Result err = syncState(context);
983 if (err != angle::Result::Continue)
984 {
985 return 0;
986 }
Jamie Madillcc73f242018-08-01 11:34:48 -0400987 }
Jamie Madill67220092019-05-20 11:12:53 -0400988
Jamie Madillcc73f242018-08-01 11:34:48 -0400989 if (!mImpl->checkStatus(context))
990 {
991 mCachedStatus = GL_FRAMEBUFFER_UNSUPPORTED;
Jamie Madille98b1b52018-03-08 09:47:23 -0500992 }
Jamie Madill362876b2016-06-16 14:46:59 -0400993 }
994
Jamie Madill427064d2018-04-13 16:20:34 -0400995 return mCachedStatus.value();
Jamie Madill362876b2016-06-16 14:46:59 -0400996}
997
Jamie Madille98b1b52018-03-08 09:47:23 -0500998GLenum Framebuffer::checkStatusWithGLFrontEnd(const Context *context)
Jamie Madill362876b2016-06-16 14:46:59 -0400999{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001000 const State &state = context->getState();
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001001
Jamie Madill2274b652018-05-31 10:56:08 -04001002 ASSERT(mState.mId != 0);
Jamie Madill362876b2016-06-16 14:46:59 -04001003
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001004 bool hasAttachments = false;
1005 Optional<unsigned int> colorbufferSize;
1006 Optional<int> samples;
Geoff Lang92019432017-11-20 13:09:34 -05001007 Optional<bool> fixedSampleLocations;
JiangYizhou461d9a32017-01-04 16:37:26 +08001008 bool hasRenderbuffer = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009
Martin Radev9bc9a322017-07-21 14:28:17 +03001010 const FramebufferAttachment *firstAttachment = getFirstNonNullAttachment();
1011
Jiawei Shaoa8802472018-05-28 11:17:47 +08001012 Optional<bool> isLayered;
1013 Optional<TextureType> colorAttachmentsTextureType;
1014
Jamie Madill48ef11b2016-04-27 15:21:52 -04001015 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016 {
Jamie Madill2d06b732015-04-20 12:53:28 -04001017 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001019 if (!CheckAttachmentCompleteness(context, colorAttachment))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001020 {
1021 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1022 }
daniel@transgaming.com01868132010-08-24 19:21:17 +00001023
Geoff Lang677bb6f2017-04-05 12:40:40 -04001024 const InternalFormat &format = *colorAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001025 if (format.depthBits > 0 || format.stencilBits > 0)
1026 {
1027 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1028 }
1029
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001030 if (!CheckAttachmentSampleCompleteness(context, colorAttachment, true, &samples,
1031 &fixedSampleLocations))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001032 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001033 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001034 }
1035
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001036 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
1037 // in GLES 3.0, there is no such restriction
1038 if (state.getClientMajorVersion() < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001039 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001040 if (colorbufferSize.valid())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001041 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001042 if (format.pixelBytes != colorbufferSize.value())
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +00001043 {
1044 return GL_FRAMEBUFFER_UNSUPPORTED;
1045 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001046 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001047 else
1048 {
1049 colorbufferSize = format.pixelBytes;
1050 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001051 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001052
Martin Radev9bc9a322017-07-21 14:28:17 +03001053 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &colorAttachment))
1054 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001055 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001056 }
1057
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001058 hasRenderbuffer = hasRenderbuffer || (colorAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001059
1060 if (!hasAttachments)
1061 {
1062 isLayered = colorAttachment.isLayered();
1063 if (isLayered.value())
1064 {
1065 colorAttachmentsTextureType = colorAttachment.getTextureImageIndex().getType();
1066 }
1067 hasAttachments = true;
1068 }
1069 else
1070 {
1071 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1072 // If any framebuffer attachment is layered, all populated attachments
1073 // must be layered. Additionally, all populated color attachments must
1074 // be from textures of the same target. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1075 ASSERT(isLayered.valid());
1076 if (isLayered.value() != colorAttachment.isLayered())
1077 {
1078 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1079 }
1080 else if (isLayered.value())
1081 {
1082 ASSERT(colorAttachmentsTextureType.valid());
1083 if (colorAttachmentsTextureType.value() !=
1084 colorAttachment.getTextureImageIndex().getType())
1085 {
1086 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1087 }
1088 }
1089 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001090 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001091 }
1092
Jamie Madill48ef11b2016-04-27 15:21:52 -04001093 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001094 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001095 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001096 if (!CheckAttachmentCompleteness(context, depthAttachment))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001098 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099 }
1100
Geoff Lang677bb6f2017-04-05 12:40:40 -04001101 const InternalFormat &format = *depthAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001102 if (format.depthBits == 0)
1103 {
1104 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001105 }
1106
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001107 if (!CheckAttachmentSampleCompleteness(context, depthAttachment, false, &samples,
1108 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001109 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001110 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001111 }
Sami Väisänena797e062016-05-12 15:23:40 +03001112
Martin Radev9bc9a322017-07-21 14:28:17 +03001113 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &depthAttachment))
1114 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001115 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001116 }
1117
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001118 hasRenderbuffer = hasRenderbuffer || (depthAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001119
1120 if (!hasAttachments)
1121 {
1122 isLayered = depthAttachment.isLayered();
1123 hasAttachments = true;
1124 }
1125 else
1126 {
1127 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1128 // If any framebuffer attachment is layered, all populated attachments
1129 // must be layered. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1130 ASSERT(isLayered.valid());
1131 if (isLayered.value() != depthAttachment.isLayered())
1132 {
1133 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1134 }
1135 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001136 }
1137
Jamie Madill48ef11b2016-04-27 15:21:52 -04001138 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001139 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001140 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001141 if (!CheckAttachmentCompleteness(context, stencilAttachment))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001142 {
1143 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1144 }
1145
Geoff Lang677bb6f2017-04-05 12:40:40 -04001146 const InternalFormat &format = *stencilAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001147 if (format.stencilBits == 0)
1148 {
1149 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001150 }
1151
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001152 if (!CheckAttachmentSampleCompleteness(context, stencilAttachment, false, &samples,
1153 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001154 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001155 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001156 }
Corentin Wallez086d59a2016-04-29 09:06:49 -04001157
Martin Radev9bc9a322017-07-21 14:28:17 +03001158 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &stencilAttachment))
1159 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001160 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001161 }
1162
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001163 hasRenderbuffer = hasRenderbuffer || (stencilAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001164
1165 if (!hasAttachments)
1166 {
1167 hasAttachments = true;
1168 }
1169 else
1170 {
1171 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1172 // If any framebuffer attachment is layered, all populated attachments
1173 // must be layered.
1174 // {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1175 ASSERT(isLayered.valid());
1176 if (isLayered.value() != stencilAttachment.isLayered())
1177 {
1178 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1179 }
1180 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001181 }
1182
1183 // Starting from ES 3.0 stencil and depth, if present, should be the same image
1184 if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() &&
1185 stencilAttachment.isAttached() && stencilAttachment != depthAttachment)
1186 {
1187 return GL_FRAMEBUFFER_UNSUPPORTED;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001188 }
1189
Jamie Madilla02315b2017-02-23 14:14:47 -05001190 // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL.
1191 if (state.isWebGL1())
1192 {
1193 if (!mState.mWebGLDepthStencilConsistent)
1194 {
1195 return GL_FRAMEBUFFER_UNSUPPORTED;
1196 }
1197
1198 if (mState.mWebGLDepthStencilAttachment.isAttached())
1199 {
1200 if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 ||
1201 mState.mWebGLDepthStencilAttachment.getStencilSize() == 0)
1202 {
1203 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1204 }
Martin Radev9bc9a322017-07-21 14:28:17 +03001205
1206 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment,
1207 &mState.mWebGLDepthStencilAttachment))
1208 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001209 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001210 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001211 }
1212 else if (mState.mStencilAttachment.isAttached() &&
1213 mState.mStencilAttachment.getDepthSize() > 0)
1214 {
1215 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1216 }
1217 else if (mState.mDepthAttachment.isAttached() &&
1218 mState.mDepthAttachment.getStencilSize() > 0)
1219 {
1220 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1221 }
1222 }
1223
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001224 // ES3.1(section 9.4) requires that if no image is attached to the framebuffer, and either the
1225 // value of the framebuffer's FRAMEBUFFER_DEFAULT_WIDTH or FRAMEBUFFER_DEFAULT_HEIGHT parameters
1226 // is zero, the framebuffer is considered incomplete.
JiangYizhou461d9a32017-01-04 16:37:26 +08001227 GLint defaultWidth = mState.getDefaultWidth();
1228 GLint defaultHeight = mState.getDefaultHeight();
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001229 if (!hasAttachments && (defaultWidth == 0 || defaultHeight == 0))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001230 {
1231 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001232 }
1233
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001234 // In ES 2.0 and WebGL, all color attachments must have the same width and height.
Jamie Madillcc86d642015-11-24 13:00:07 -05001235 // In ES 3.0, there is no such restriction.
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001236 if ((state.getClientMajorVersion() < 3 || state.getExtensions().webglCompatibility) &&
1237 !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -05001238 {
1239 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
1240 }
1241
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001242 // ES3.1(section 9.4) requires that if the attached images are a mix of renderbuffers and
1243 // textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all attached textures.
JiangYizhou461d9a32017-01-04 16:37:26 +08001244 if (fixedSampleLocations.valid() && hasRenderbuffer && !fixedSampleLocations.value())
1245 {
1246 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1247 }
1248
Kenneth Russellce8602a2017-10-03 18:23:08 -07001249 // The WebGL conformance tests implicitly define that all framebuffer
1250 // attachments must be unique. For example, the same level of a texture can
1251 // not be attached to two different color attachments.
1252 if (state.getExtensions().webglCompatibility)
1253 {
1254 if (!mState.colorAttachmentsAreUniqueImages())
1255 {
1256 return GL_FRAMEBUFFER_UNSUPPORTED;
1257 }
1258 }
1259
Jamie Madillcc86d642015-11-24 13:00:07 -05001260 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001262
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001263angle::Result Framebuffer::discard(const Context *context, size_t count, const GLenum *attachments)
Austin Kinross08332632015-05-05 13:35:47 -07001264{
Jamie Madill05b35b22017-10-03 09:01:44 -04001265 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1266 // can be no-ops, so we should probably do that to ensure consistency.
1267 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1268
Jamie Madill4928b7c2017-06-20 12:57:39 -04001269 return mImpl->discard(context, count, attachments);
Austin Kinross08332632015-05-05 13:35:47 -07001270}
1271
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001272angle::Result Framebuffer::invalidate(const Context *context,
1273 size_t count,
1274 const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001275{
Jamie Madill05b35b22017-10-03 09:01:44 -04001276 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1277 // can be no-ops, so we should probably do that to ensure consistency.
1278 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1279
Jamie Madill4928b7c2017-06-20 12:57:39 -04001280 return mImpl->invalidate(context, count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001281}
1282
Jamie Madill05b35b22017-10-03 09:01:44 -04001283bool Framebuffer::partialClearNeedsInit(const Context *context,
1284 bool color,
1285 bool depth,
1286 bool stencil)
1287{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001288 const auto &glState = context->getState();
Jamie Madill05b35b22017-10-03 09:01:44 -04001289
1290 if (!glState.isRobustResourceInitEnabled())
1291 {
1292 return false;
1293 }
1294
1295 // Scissors can affect clearing.
1296 // TODO(jmadill): Check for complete scissor overlap.
1297 if (glState.isScissorTestEnabled())
1298 {
1299 return true;
1300 }
1301
1302 // If colors masked, we must clear before we clear. Do a simple check.
1303 // TODO(jmadill): Filter out unused color channels from the test.
1304 if (color)
1305 {
1306 const auto &blend = glState.getBlendState();
1307 if (!(blend.colorMaskRed && blend.colorMaskGreen && blend.colorMaskBlue &&
1308 blend.colorMaskAlpha))
1309 {
1310 return true;
1311 }
1312 }
1313
1314 const auto &depthStencil = glState.getDepthStencilState();
Yuly Novikov21edf3d2018-07-23 16:44:16 -04001315 if (stencil && (depthStencil.stencilMask != depthStencil.stencilWritemask ||
1316 depthStencil.stencilBackMask != depthStencil.stencilBackWritemask))
Jamie Madill05b35b22017-10-03 09:01:44 -04001317 {
1318 return true;
1319 }
1320
1321 return false;
1322}
1323
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001324angle::Result Framebuffer::invalidateSub(const Context *context,
1325 size_t count,
1326 const GLenum *attachments,
1327 const Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -04001328{
Jamie Madill05b35b22017-10-03 09:01:44 -04001329 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1330 // can be no-ops, so we should probably do that to ensure consistency.
1331 // TODO(jmadill): Make a invalidate no-op in WebGL 2.0.
1332
Jamie Madill4928b7c2017-06-20 12:57:39 -04001333 return mImpl->invalidateSub(context, count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -04001334}
1335
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001336angle::Result Framebuffer::clear(const Context *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -05001337{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001338 const auto &glState = context->getState();
Jamie Madill05b35b22017-10-03 09:01:44 -04001339 if (glState.isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -05001340 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001341 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001342 }
1343
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001344 // Remove clear bits that are ineffective. An effective clear changes at least one fragment. If
1345 // color/depth/stencil masks make the clear ineffective we skip it altogether.
1346
1347 // If all color channels are masked, don't attempt to clear color.
1348 if (context->getState().getBlendState().allChannelsMasked())
1349 {
1350 mask &= ~GL_COLOR_BUFFER_BIT;
1351 }
1352
1353 // If depth write is disabled, don't attempt to clear depth.
1354 if (!context->getState().getDepthStencilState().depthMask)
1355 {
1356 mask &= ~GL_DEPTH_BUFFER_BIT;
1357 }
1358
1359 // If all stencil bits are masked, don't attempt to clear stencil.
1360 if (context->getState().getDepthStencilState().stencilWritemask == 0)
1361 {
1362 mask &= ~GL_STENCIL_BUFFER_BIT;
1363 }
1364
1365 if (mask != 0)
1366 {
1367 ANGLE_TRY(mImpl->clear(context, mask));
1368 }
Jamie Madill05b35b22017-10-03 09:01:44 -04001369
Jamie Madill7c985f52018-11-29 18:16:17 -05001370 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001371}
1372
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001373angle::Result Framebuffer::clearBufferfv(const Context *context,
1374 GLenum buffer,
1375 GLint drawbuffer,
1376 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001377{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001378 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001379 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001380 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001381 }
1382
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001383 if (buffer == GL_DEPTH)
1384 {
1385 // If depth write is disabled, don't attempt to clear depth.
1386 if (!context->getState().getDepthStencilState().depthMask)
1387 {
1388 return angle::Result::Continue;
1389 }
1390 }
1391 else
1392 {
1393 // If all color channels are masked, don't attempt to clear color.
1394 if (context->getState().getBlendState().allChannelsMasked())
1395 {
1396 return angle::Result::Continue;
1397 }
1398 }
1399
Jamie Madill05b35b22017-10-03 09:01:44 -04001400 ANGLE_TRY(mImpl->clearBufferfv(context, buffer, drawbuffer, values));
1401
Jamie Madill7c985f52018-11-29 18:16:17 -05001402 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001403}
1404
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001405angle::Result Framebuffer::clearBufferuiv(const Context *context,
1406 GLenum buffer,
1407 GLint drawbuffer,
1408 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001409{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001410 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001411 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001412 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001413 }
1414
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001415 // If all color channels are masked, don't attempt to clear color.
1416 if (context->getState().getBlendState().allChannelsMasked())
1417 {
1418 return angle::Result::Continue;
1419 }
1420
Jamie Madill05b35b22017-10-03 09:01:44 -04001421 ANGLE_TRY(mImpl->clearBufferuiv(context, buffer, drawbuffer, values));
1422
Jamie Madill7c985f52018-11-29 18:16:17 -05001423 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001424}
1425
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001426angle::Result Framebuffer::clearBufferiv(const Context *context,
1427 GLenum buffer,
1428 GLint drawbuffer,
1429 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001430{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001431 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001432 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001433 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001434 }
1435
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001436 if (buffer == GL_STENCIL)
1437 {
1438 // If all stencil bits are masked, don't attempt to clear stencil.
1439 if (context->getState().getDepthStencilState().stencilWritemask == 0)
1440 {
1441 return angle::Result::Continue;
1442 }
1443 }
1444 else
1445 {
1446 // If all color channels are masked, don't attempt to clear color.
1447 if (context->getState().getBlendState().allChannelsMasked())
1448 {
1449 return angle::Result::Continue;
1450 }
1451 }
1452
Jamie Madill05b35b22017-10-03 09:01:44 -04001453 ANGLE_TRY(mImpl->clearBufferiv(context, buffer, drawbuffer, values));
1454
Jamie Madill7c985f52018-11-29 18:16:17 -05001455 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001456}
1457
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001458angle::Result Framebuffer::clearBufferfi(const Context *context,
1459 GLenum buffer,
1460 GLint drawbuffer,
1461 GLfloat depth,
1462 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -05001463{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001464 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001465 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001466 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001467 }
1468
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001469 bool clearDepth = context->getState().getDepthStencilState().depthMask;
1470 bool clearStencil = context->getState().getDepthStencilState().stencilWritemask != 0;
1471
1472 if (clearDepth && clearStencil)
1473 {
1474 ASSERT(buffer == GL_DEPTH_STENCIL);
1475 ANGLE_TRY(mImpl->clearBufferfi(context, GL_DEPTH_STENCIL, drawbuffer, depth, stencil));
1476 }
1477 else if (clearDepth && !clearStencil)
1478 {
1479 ANGLE_TRY(mImpl->clearBufferfv(context, GL_DEPTH, drawbuffer, &depth));
1480 }
1481 else if (!clearDepth && clearStencil)
1482 {
1483 ANGLE_TRY(mImpl->clearBufferiv(context, GL_STENCIL, drawbuffer, &stencil));
1484 }
Jamie Madill05b35b22017-10-03 09:01:44 -04001485
Jamie Madill7c985f52018-11-29 18:16:17 -05001486 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001487}
1488
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001489angle::Result Framebuffer::getImplementationColorReadFormat(const Context *context,
1490 GLenum *formatOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001491{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001492 ANGLE_TRY(syncState(context));
1493 *formatOut = mImpl->getImplementationColorReadFormat(context);
Jamie Madill7c985f52018-11-29 18:16:17 -05001494 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001495}
1496
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001497angle::Result Framebuffer::getImplementationColorReadType(const Context *context, GLenum *typeOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001498{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001499 ANGLE_TRY(syncState(context));
1500 *typeOut = mImpl->getImplementationColorReadType(context);
Jamie Madill7c985f52018-11-29 18:16:17 -05001501 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001502}
1503
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001504angle::Result Framebuffer::readPixels(const Context *context,
1505 const Rectangle &area,
1506 GLenum format,
1507 GLenum type,
1508 void *pixels)
Geoff Langbce529e2014-12-01 12:48:41 -05001509{
Jamie Madill362876b2016-06-16 14:46:59 -04001510 ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels));
Geoff Lang520c4ae2015-05-05 13:12:36 -04001511
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001512 Buffer *unpackBuffer = context->getState().getTargetBuffer(BufferBinding::PixelUnpack);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001513 if (unpackBuffer)
1514 {
Jamie Madill124f78c2019-06-18 11:48:24 -04001515 unpackBuffer->onPixelPack();
Geoff Lang520c4ae2015-05-05 13:12:36 -04001516 }
1517
Jamie Madill7c985f52018-11-29 18:16:17 -05001518 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001519}
1520
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001521angle::Result Framebuffer::blit(const Context *context,
1522 const Rectangle &sourceArea,
1523 const Rectangle &destArea,
1524 GLbitfield mask,
1525 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -05001526{
He Yunchao6be602d2016-12-22 14:33:07 +08001527 GLbitfield blitMask = mask;
1528
1529 // Note that blitting is called against draw framebuffer.
1530 // See the code in gl::Context::blitFramebuffer.
1531 if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer())
1532 {
1533 blitMask &= ~GL_COLOR_BUFFER_BIT;
1534 }
1535
1536 if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr)
1537 {
1538 blitMask &= ~GL_STENCIL_BUFFER_BIT;
1539 }
1540
1541 if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr)
1542 {
1543 blitMask &= ~GL_DEPTH_BUFFER_BIT;
1544 }
1545
1546 if (!blitMask)
1547 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001548 return angle::Result::Continue;
He Yunchao6be602d2016-12-22 14:33:07 +08001549 }
1550
1551 return mImpl->blit(context, sourceArea, destArea, blitMask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -05001552}
1553
Luc Ferronbf6dc372018-06-28 15:24:19 -04001554bool Framebuffer::isDefault() const
1555{
1556 return id() == 0;
1557}
1558
Jamie Madill427064d2018-04-13 16:20:34 -04001559int Framebuffer::getSamples(const Context *context)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001560{
Jamie Madill427064d2018-04-13 16:20:34 -04001561 return (isComplete(context) ? getCachedSamples(context) : 0);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001562}
1563
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -04001564int Framebuffer::getCachedSamples(const Context *context) const
Jamie Madill9c335862017-07-18 11:51:38 -04001565{
Jamie Madill5b772312018-03-08 20:28:32 -05001566 ASSERT(mCachedStatus.valid() && mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE);
1567
Jamie Madill9c335862017-07-18 11:51:38 -04001568 // For a complete framebuffer, all attachments must have the same sample count.
1569 // In this case return the first nonzero sample size.
1570 const auto *firstNonNullAttachment = mState.getFirstNonNullAttachment();
1571 if (firstNonNullAttachment)
1572 {
1573 ASSERT(firstNonNullAttachment->isAttached());
1574 return firstNonNullAttachment->getSamples();
1575 }
1576
1577 // No attachments found.
1578 return 0;
1579}
1580
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001581angle::Result Framebuffer::getSamplePosition(const Context *context,
1582 size_t index,
1583 GLfloat *xy) const
Corentin Wallezccab69d2017-01-27 16:57:15 -05001584{
Geoff Lang13455072018-05-09 11:24:43 -04001585 ANGLE_TRY(mImpl->getSamplePosition(context, index, xy));
Jamie Madill7c985f52018-11-29 18:16:17 -05001586 return angle::Result::Continue;
Corentin Wallezccab69d2017-01-27 16:57:15 -05001587}
1588
Jamie Madille261b442014-06-25 12:42:21 -04001589bool Framebuffer::hasValidDepthStencil() const
1590{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001591 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -04001592}
1593
Jamie Madilla02315b2017-02-23 14:14:47 -05001594void Framebuffer::setAttachment(const Context *context,
1595 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -04001596 GLenum binding,
1597 const ImageIndex &textureIndex,
1598 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -04001599{
Martin Radev5dae57b2017-07-14 16:15:55 +03001600 setAttachment(context, type, binding, textureIndex, resource,
1601 FramebufferAttachment::kDefaultNumViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001602 FramebufferAttachment::kDefaultBaseViewIndex, false);
Martin Radev5dae57b2017-07-14 16:15:55 +03001603}
1604
1605void Framebuffer::setAttachment(const Context *context,
1606 GLenum type,
1607 GLenum binding,
1608 const ImageIndex &textureIndex,
1609 FramebufferAttachmentObject *resource,
1610 GLsizei numViews,
1611 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001612 bool isMultiview)
Martin Radev5dae57b2017-07-14 16:15:55 +03001613{
Jamie Madilla02315b2017-02-23 14:14:47 -05001614 // Context may be null in unit tests.
1615 if (!context || !context->isWebGL1())
1616 {
Martin Radev5dae57b2017-07-14 16:15:55 +03001617 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001618 isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001619 return;
1620 }
1621
1622 switch (binding)
1623 {
1624 case GL_DEPTH_STENCIL:
1625 case GL_DEPTH_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001626 mState.mWebGLDepthStencilAttachment.attach(context, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001627 resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001628 isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001629 break;
1630 case GL_DEPTH:
1631 case GL_DEPTH_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001632 mState.mWebGLDepthAttachment.attach(context, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001633 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001634 break;
1635 case GL_STENCIL:
1636 case GL_STENCIL_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001637 mState.mWebGLStencilAttachment.attach(context, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001638 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001639 break;
1640 default:
Martin Radev5dae57b2017-07-14 16:15:55 +03001641 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001642 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001643 return;
1644 }
1645
Mingyu Hu7d64c482019-03-12 14:27:40 -07001646 commitWebGL1DepthStencilIfConsistent(context, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001647}
1648
Mingyu Hu7d64c482019-03-12 14:27:40 -07001649void Framebuffer::setAttachmentMultiview(const Context *context,
1650 GLenum type,
1651 GLenum binding,
1652 const ImageIndex &textureIndex,
1653 FramebufferAttachmentObject *resource,
1654 GLsizei numViews,
1655 GLint baseViewIndex)
Martin Radev82ef7742017-08-08 17:44:58 +03001656{
Mingyu Hu7d64c482019-03-12 14:27:40 -07001657 setAttachment(context, type, binding, textureIndex, resource, numViews, baseViewIndex, true);
Martin Radev5dae57b2017-07-14 16:15:55 +03001658}
1659
1660void Framebuffer::commitWebGL1DepthStencilIfConsistent(const Context *context,
1661 GLsizei numViews,
1662 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001663 bool isMultiview)
Jamie Madilla02315b2017-02-23 14:14:47 -05001664{
1665 int count = 0;
1666
1667 std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment,
1668 &mState.mWebGLDepthAttachment,
1669 &mState.mWebGLStencilAttachment}};
1670 for (FramebufferAttachment *attachment : attachments)
1671 {
1672 if (attachment->isAttached())
1673 {
1674 count++;
1675 }
1676 }
1677
1678 mState.mWebGLDepthStencilConsistent = (count <= 1);
1679 if (!mState.mWebGLDepthStencilConsistent)
1680 {
1681 // Inconsistent.
1682 return;
1683 }
1684
Geoff Lange466c552017-03-17 15:24:12 -04001685 auto getImageIndexIfTextureAttachment = [](const FramebufferAttachment &attachment) {
1686 if (attachment.type() == GL_TEXTURE)
1687 {
1688 return attachment.getTextureImageIndex();
1689 }
1690 else
1691 {
Jamie Madillcc129372018-04-12 09:13:18 -04001692 return ImageIndex();
Geoff Lange466c552017-03-17 15:24:12 -04001693 }
1694 };
1695
Jamie Madilla02315b2017-02-23 14:14:47 -05001696 if (mState.mWebGLDepthAttachment.isAttached())
1697 {
1698 const auto &depth = mState.mWebGLDepthAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001699 setAttachmentImpl(context, depth.type(), GL_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001700 getImageIndexIfTextureAttachment(depth), depth.getResource(), numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001701 baseViewIndex, isMultiview);
Jamie Madillcc129372018-04-12 09:13:18 -04001702 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001703 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001704 }
1705 else if (mState.mWebGLStencilAttachment.isAttached())
1706 {
1707 const auto &stencil = mState.mWebGLStencilAttachment;
Jamie Madillcc129372018-04-12 09:13:18 -04001708 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001709 baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001710 setAttachmentImpl(context, stencil.type(), GL_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001711 getImageIndexIfTextureAttachment(stencil), stencil.getResource(),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001712 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001713 }
1714 else if (mState.mWebGLDepthStencilAttachment.isAttached())
1715 {
1716 const auto &depthStencil = mState.mWebGLDepthStencilAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001717 setAttachmentImpl(context, depthStencil.type(), GL_DEPTH_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001718 getImageIndexIfTextureAttachment(depthStencil),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001719 depthStencil.getResource(), numViews, baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001720 setAttachmentImpl(context, depthStencil.type(), GL_STENCIL_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001721 getImageIndexIfTextureAttachment(depthStencil),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001722 depthStencil.getResource(), numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001723 }
1724 else
1725 {
Jamie Madillcc129372018-04-12 09:13:18 -04001726 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001727 baseViewIndex, isMultiview);
Jamie Madillcc129372018-04-12 09:13:18 -04001728 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001729 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001730 }
1731}
1732
Jamie Madill4928b7c2017-06-20 12:57:39 -04001733void Framebuffer::setAttachmentImpl(const Context *context,
1734 GLenum type,
Jamie Madilla02315b2017-02-23 14:14:47 -05001735 GLenum binding,
1736 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001737 FramebufferAttachmentObject *resource,
1738 GLsizei numViews,
1739 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001740 bool isMultiview)
Jamie Madilla02315b2017-02-23 14:14:47 -05001741{
Jamie Madilla02315b2017-02-23 14:14:47 -05001742 switch (binding)
1743 {
Jamie Madillb8126692017-04-05 11:22:17 -04001744 case GL_DEPTH_STENCIL:
1745 case GL_DEPTH_STENCIL_ATTACHMENT:
1746 {
1747 // ensure this is a legitimate depth+stencil format
1748 FramebufferAttachmentObject *attachmentObj = resource;
1749 if (resource)
1750 {
Jamie Madill4fd95d52017-04-05 11:22:18 -04001751 const Format &format = resource->getAttachmentFormat(binding, textureIndex);
Jamie Madillb8126692017-04-05 11:22:17 -04001752 if (format.info->depthBits == 0 || format.info->stencilBits == 0)
1753 {
1754 // Attaching nullptr detaches the current attachment.
1755 attachmentObj = nullptr;
1756 }
1757 }
1758
Jamie Madill4928b7c2017-06-20 12:57:39 -04001759 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001760 &mDirtyDepthAttachmentBinding, type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001761 attachmentObj, numViews, baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001762 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001763 &mDirtyStencilAttachmentBinding, type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001764 attachmentObj, numViews, baseViewIndex, isMultiview);
Jamie Madill42975642017-10-12 12:31:51 -04001765 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001766 }
1767
Jamie Madilla02315b2017-02-23 14:14:47 -05001768 case GL_DEPTH:
1769 case GL_DEPTH_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001770 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001771 &mDirtyDepthAttachmentBinding, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001772 numViews, baseViewIndex, isMultiview);
Jamie Madill2d06b732015-04-20 12:53:28 -04001773 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001774
Jamie Madilla02315b2017-02-23 14:14:47 -05001775 case GL_STENCIL:
1776 case GL_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001777 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001778 &mDirtyStencilAttachmentBinding, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001779 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001780 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001781
Jamie Madilla02315b2017-02-23 14:14:47 -05001782 case GL_BACK:
Geoff Lang8170eab2017-09-21 13:59:04 -04001783 updateAttachment(context, &mState.mColorAttachments[0], DIRTY_BIT_COLOR_ATTACHMENT_0,
1784 &mDirtyColorAttachmentBindings[0], type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001785 resource, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001786 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001787
Jamie Madilla02315b2017-02-23 14:14:47 -05001788 default:
1789 {
1790 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
1791 ASSERT(colorIndex < mState.mColorAttachments.size());
Jamie Madillb8126692017-04-05 11:22:17 -04001792 size_t dirtyBit = DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001793 updateAttachment(context, &mState.mColorAttachments[colorIndex], dirtyBit,
Jamie Madillb8126692017-04-05 11:22:17 -04001794 &mDirtyColorAttachmentBindings[colorIndex], type, binding,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001795 textureIndex, resource, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001796
shrekshaoc87e0052019-02-21 11:40:28 -08001797 if (!resource)
1798 {
shrekshao8413fab2019-04-04 17:13:18 -07001799 mColorAttachmentBits.reset(colorIndex);
shrekshaoc87e0052019-02-21 11:40:28 -08001800 mFloat32ColorAttachmentBits.reset(colorIndex);
1801 }
1802 else
1803 {
shrekshao8413fab2019-04-04 17:13:18 -07001804 mColorAttachmentBits.set(colorIndex);
shrekshaoc87e0052019-02-21 11:40:28 -08001805 updateFloat32ColorAttachmentBits(
1806 colorIndex, resource->getAttachmentFormat(binding, textureIndex).info);
1807 }
1808
Corentin Walleze7557742017-06-01 13:09:57 -04001809 // TODO(jmadill): ASSERT instead of checking the attachment exists in
1810 // formsRenderingFeedbackLoopWith
Jamie Madilla02315b2017-02-23 14:14:47 -05001811 bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE);
1812 mState.mEnabledDrawBuffers.set(colorIndex, enabled);
Jamie Madill6e18a232019-01-16 13:27:14 -05001813 SetComponentTypeMask(getDrawbufferWriteType(colorIndex), colorIndex,
1814 &mState.mDrawBufferTypeMask);
Jamie Madill2d06b732015-04-20 12:53:28 -04001815 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001816 break;
Geoff Langab75a052014-10-15 12:56:37 -04001817 }
1818}
1819
Jamie Madill4928b7c2017-06-20 12:57:39 -04001820void Framebuffer::updateAttachment(const Context *context,
1821 FramebufferAttachment *attachment,
Jamie Madillb8126692017-04-05 11:22:17 -04001822 size_t dirtyBit,
Jamie Madilld4442552018-02-27 22:03:47 -05001823 angle::ObserverBinding *onDirtyBinding,
Jamie Madillb8126692017-04-05 11:22:17 -04001824 GLenum type,
1825 GLenum binding,
1826 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001827 FramebufferAttachmentObject *resource,
1828 GLsizei numViews,
1829 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001830 bool isMultiview)
Jamie Madillb8126692017-04-05 11:22:17 -04001831{
Martin Radev5dae57b2017-07-14 16:15:55 +03001832 attachment->attach(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001833 isMultiview);
Jamie Madillb8126692017-04-05 11:22:17 -04001834 mDirtyBits.set(dirtyBit);
Jamie Madill05b35b22017-10-03 09:01:44 -04001835 mState.mResourceNeedsInit.set(dirtyBit, attachment->initState() == InitState::MayNeedInit);
Jamie Madill66f0d2c2018-11-30 15:25:36 -05001836 onDirtyBinding->bind(resource);
Jamie Madille98b1b52018-03-08 09:47:23 -05001837
Jamie Madill124f78c2019-06-18 11:48:24 -04001838 invalidateCompletenessCache();
Jamie Madillb8126692017-04-05 11:22:17 -04001839}
1840
Jamie Madilla02315b2017-02-23 14:14:47 -05001841void Framebuffer::resetAttachment(const Context *context, GLenum binding)
Jamie Madill2d06b732015-04-20 12:53:28 -04001842{
Jamie Madillcc129372018-04-12 09:13:18 -04001843 setAttachment(context, GL_NONE, binding, ImageIndex(), nullptr);
Jamie Madill2d06b732015-04-20 12:53:28 -04001844}
1845
Jamie Madill6f755b22018-10-09 12:48:54 -04001846angle::Result Framebuffer::syncState(const Context *context)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001847{
1848 if (mDirtyBits.any())
1849 {
Jamie Madill888081d2018-02-27 00:24:46 -05001850 mDirtyBitsGuard = mDirtyBits;
Jamie Madill19fa1c62018-03-08 09:47:21 -05001851 ANGLE_TRY(mImpl->syncState(context, mDirtyBits));
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001852 mDirtyBits.reset();
Jamie Madill888081d2018-02-27 00:24:46 -05001853 mDirtyBitsGuard.reset();
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001854 }
Jamie Madill7c985f52018-11-29 18:16:17 -05001855 return angle::Result::Continue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001857
Jamie Madill124f78c2019-06-18 11:48:24 -04001858void Framebuffer::onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001859{
Jamie Madille4faae22019-05-10 08:27:00 -04001860 if (message != angle::SubjectMessage::SubjectChanged)
Jamie Madillf668a4b2018-09-23 17:01:20 -04001861 {
Jamie Madill67220092019-05-20 11:12:53 -04001862 // This can be triggered by SubImage calls for Textures.
1863 if (message == angle::SubjectMessage::ContentsChanged)
1864 {
1865 mDirtyBits.set(DIRTY_BIT_COLOR_BUFFER_CONTENTS_0 + index);
Jamie Madill124f78c2019-06-18 11:48:24 -04001866 onStateChange(angle::SubjectMessage::DirtyBitsFlagged);
Jamie Madill67220092019-05-20 11:12:53 -04001867 return;
1868 }
1869
Jamie Madillf668a4b2018-09-23 17:01:20 -04001870 // This can be triggered by the GL back-end TextureGL class.
Jamie Madille4faae22019-05-10 08:27:00 -04001871 ASSERT(message == angle::SubjectMessage::DirtyBitsFlagged);
Jamie Madillf668a4b2018-09-23 17:01:20 -04001872 return;
1873 }
Jamie Madill55e57f92018-09-18 11:32:43 -04001874
1875 ASSERT(!mDirtyBitsGuard.valid() || mDirtyBitsGuard.value().test(index));
1876 mDirtyBits.set(index);
Jamie Madill888081d2018-02-27 00:24:46 -05001877
Jamie Madill124f78c2019-06-18 11:48:24 -04001878 invalidateCompletenessCache();
Jamie Madill05b35b22017-10-03 09:01:44 -04001879
Jamie Madilld4442552018-02-27 22:03:47 -05001880 FramebufferAttachment *attachment = getAttachmentFromSubjectIndex(index);
1881
Jamie Madill05b35b22017-10-03 09:01:44 -04001882 // Mark the appropriate init flag.
Jamie Madilld4442552018-02-27 22:03:47 -05001883 mState.mResourceNeedsInit.set(index, attachment->initState() == InitState::MayNeedInit);
shrekshaoc87e0052019-02-21 11:40:28 -08001884
1885 // Update mFloat32ColorAttachmentBits Cache
1886 if (index < DIRTY_BIT_COLOR_ATTACHMENT_MAX)
1887 {
1888 ASSERT(index != DIRTY_BIT_DEPTH_ATTACHMENT);
1889 ASSERT(index != DIRTY_BIT_STENCIL_ATTACHMENT);
1890 updateFloat32ColorAttachmentBits(index - DIRTY_BIT_COLOR_ATTACHMENT_0,
1891 attachment->getFormat().info);
1892 }
Jamie Madilld4442552018-02-27 22:03:47 -05001893}
1894
1895FramebufferAttachment *Framebuffer::getAttachmentFromSubjectIndex(angle::SubjectIndex index)
1896{
1897 switch (index)
1898 {
1899 case DIRTY_BIT_DEPTH_ATTACHMENT:
1900 return &mState.mDepthAttachment;
1901 case DIRTY_BIT_STENCIL_ATTACHMENT:
1902 return &mState.mStencilAttachment;
1903 default:
1904 size_t colorIndex = (index - DIRTY_BIT_COLOR_ATTACHMENT_0);
1905 ASSERT(colorIndex < mState.mColorAttachments.size());
1906 return &mState.mColorAttachments[colorIndex];
1907 }
Jamie Madill51f40ec2016-06-15 14:06:00 -04001908}
1909
shrekshao8413fab2019-04-04 17:13:18 -07001910bool Framebuffer::formsRenderingFeedbackLoopWith(const Context *context) const
Jamie Madilla4595b82017-01-11 17:36:34 -05001911{
shrekshao8413fab2019-04-04 17:13:18 -07001912 const State &state = context->getState();
Jamie Madill785e8a02018-10-04 17:42:00 -04001913 const Program *program = state.getProgram();
Jamie Madilla4595b82017-01-11 17:36:34 -05001914
1915 // TODO(jmadill): Default framebuffer feedback loops.
Jamie Madill2274b652018-05-31 10:56:08 -04001916 if (mState.mId == 0)
Jamie Madilla4595b82017-01-11 17:36:34 -05001917 {
1918 return false;
1919 }
1920
Jamie Madill4e71b2b2019-07-08 13:23:38 -04001921 const FramebufferAttachment *depth = getDepthAttachment();
1922 const FramebufferAttachment *stencil = getStencilAttachment();
shrekshao8413fab2019-04-04 17:13:18 -07001923
1924 const bool checkDepth = depth && depth->type() == GL_TEXTURE;
1925 // Skip the feedback loop check for stencil if depth/stencil point to the same resource.
1926 const bool checkStencil =
1927 (stencil && stencil->type() == GL_TEXTURE) && (!depth || *stencil != *depth);
1928
1929 const gl::ActiveTextureMask &activeTextures = program->getActiveSamplersMask();
1930 const gl::ActiveTexturePointerArray &textures = state.getActiveTexturesCache();
1931
1932 for (size_t textureUnit : activeTextures)
Jamie Madilla4595b82017-01-11 17:36:34 -05001933 {
shrekshao8413fab2019-04-04 17:13:18 -07001934 Texture *texture = textures[textureUnit];
1935
1936 if (texture == nullptr)
Jamie Madilla4595b82017-01-11 17:36:34 -05001937 {
shrekshao8413fab2019-04-04 17:13:18 -07001938 continue;
1939 }
1940
1941 // Depth and stencil attachment form feedback loops
1942 // Regardless of if enabled or masked.
1943 if (checkDepth)
1944 {
1945 if (texture->id() == depth->id())
Jamie Madilla4595b82017-01-11 17:36:34 -05001946 {
1947 return true;
1948 }
1949 }
Jamie Madilla4595b82017-01-11 17:36:34 -05001950
shrekshao8413fab2019-04-04 17:13:18 -07001951 if (checkStencil)
Jamie Madill1d37bc52017-02-02 19:59:58 -05001952 {
shrekshao8413fab2019-04-04 17:13:18 -07001953 if (texture->id() == stencil->id())
Jamie Madill1d37bc52017-02-02 19:59:58 -05001954 {
Jamie Madill38fe6842018-09-19 07:20:00 -04001955 return true;
Jamie Madill1d37bc52017-02-02 19:59:58 -05001956 }
1957 }
shrekshao8413fab2019-04-04 17:13:18 -07001958
1959 // Check if any color attachment forms a feedback loop.
1960 for (size_t drawIndex : mColorAttachmentBits)
1961 {
1962 const FramebufferAttachment &attachment = mState.mColorAttachments[drawIndex];
1963 ASSERT(attachment.isAttached());
1964
1965 if (attachment.isTextureWithId(texture->id()))
1966 {
1967 // TODO(jmadill): Check for appropriate overlap.
1968 return true;
1969 }
1970 }
Jamie Madill1d37bc52017-02-02 19:59:58 -05001971 }
1972
Jamie Madilla4595b82017-01-11 17:36:34 -05001973 return false;
1974}
1975
Jamie Madillfd3dd432017-02-02 19:59:59 -05001976bool Framebuffer::formsCopyingFeedbackLoopWith(GLuint copyTextureID,
1977 GLint copyTextureLevel,
1978 GLint copyTextureLayer) const
Jamie Madillf695a3a2017-01-11 17:36:35 -05001979{
Jamie Madill2274b652018-05-31 10:56:08 -04001980 if (mState.mId == 0)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001981 {
1982 // It seems impossible to form a texture copying feedback loop with the default FBO.
1983 return false;
1984 }
1985
Jamie Madill4e71b2b2019-07-08 13:23:38 -04001986 const FramebufferAttachment *readAttachment = getReadColorAttachment();
Jamie Madillf695a3a2017-01-11 17:36:35 -05001987 ASSERT(readAttachment);
1988
1989 if (readAttachment->isTextureWithId(copyTextureID))
1990 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001991 const auto &imageIndex = readAttachment->getTextureImageIndex();
Jamie Madillcc129372018-04-12 09:13:18 -04001992 if (imageIndex.getLevelIndex() == copyTextureLevel)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001993 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001994 // Check 3D/Array texture layers.
Jamie Madillcc129372018-04-12 09:13:18 -04001995 return !imageIndex.hasLayer() || copyTextureLayer == ImageIndex::kEntireLevel ||
1996 imageIndex.getLayerIndex() == copyTextureLayer;
Jamie Madillf695a3a2017-01-11 17:36:35 -05001997 }
1998 }
1999 return false;
2000}
2001
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002002GLint Framebuffer::getDefaultWidth() const
2003{
2004 return mState.getDefaultWidth();
2005}
2006
2007GLint Framebuffer::getDefaultHeight() const
2008{
2009 return mState.getDefaultHeight();
2010}
2011
2012GLint Framebuffer::getDefaultSamples() const
2013{
2014 return mState.getDefaultSamples();
2015}
2016
Geoff Lang92019432017-11-20 13:09:34 -05002017bool Framebuffer::getDefaultFixedSampleLocations() const
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002018{
2019 return mState.getDefaultFixedSampleLocations();
2020}
2021
Jiawei Shaob1e91382018-05-17 14:33:55 +08002022GLint Framebuffer::getDefaultLayers() const
2023{
2024 return mState.getDefaultLayers();
2025}
2026
Jamie Madillb983a4b2018-08-01 11:34:51 -04002027void Framebuffer::setDefaultWidth(const Context *context, GLint defaultWidth)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002028{
2029 mState.mDefaultWidth = defaultWidth;
2030 mDirtyBits.set(DIRTY_BIT_DEFAULT_WIDTH);
Jamie Madill124f78c2019-06-18 11:48:24 -04002031 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002032}
2033
Jamie Madillb983a4b2018-08-01 11:34:51 -04002034void Framebuffer::setDefaultHeight(const Context *context, GLint defaultHeight)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002035{
2036 mState.mDefaultHeight = defaultHeight;
2037 mDirtyBits.set(DIRTY_BIT_DEFAULT_HEIGHT);
Jamie Madill124f78c2019-06-18 11:48:24 -04002038 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002039}
2040
Jamie Madillb983a4b2018-08-01 11:34:51 -04002041void Framebuffer::setDefaultSamples(const Context *context, GLint defaultSamples)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002042{
2043 mState.mDefaultSamples = defaultSamples;
2044 mDirtyBits.set(DIRTY_BIT_DEFAULT_SAMPLES);
Jamie Madill124f78c2019-06-18 11:48:24 -04002045 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002046}
2047
Jamie Madillb983a4b2018-08-01 11:34:51 -04002048void Framebuffer::setDefaultFixedSampleLocations(const Context *context,
2049 bool defaultFixedSampleLocations)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002050{
2051 mState.mDefaultFixedSampleLocations = defaultFixedSampleLocations;
2052 mDirtyBits.set(DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS);
Jamie Madill124f78c2019-06-18 11:48:24 -04002053 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002054}
2055
Jiawei Shaob1e91382018-05-17 14:33:55 +08002056void Framebuffer::setDefaultLayers(GLint defaultLayers)
2057{
2058 mState.mDefaultLayers = defaultLayers;
2059 mDirtyBits.set(DIRTY_BIT_DEFAULT_LAYERS);
2060}
2061
Martin Radev14a26ae2017-07-24 15:56:29 +03002062GLsizei Framebuffer::getNumViews() const
2063{
Martin Radev5c00d0d2017-08-07 10:06:59 +03002064 return mState.getNumViews();
Martin Radev14a26ae2017-07-24 15:56:29 +03002065}
2066
Martin Radev4e619f52017-08-09 11:50:06 +03002067GLint Framebuffer::getBaseViewIndex() const
2068{
2069 return mState.getBaseViewIndex();
2070}
2071
Mingyu Hu7d64c482019-03-12 14:27:40 -07002072bool Framebuffer::isMultiview() const
Martin Radev878c8b12017-07-28 09:51:04 +03002073{
Mingyu Hu7d64c482019-03-12 14:27:40 -07002074 return mState.isMultiview();
Martin Radev878c8b12017-07-28 09:51:04 +03002075}
2076
Olli Etuaho8acb1b62018-07-30 16:20:54 +03002077bool Framebuffer::readDisallowedByMultiview() const
2078{
Mingyu Hu7d64c482019-03-12 14:27:40 -07002079 return (mState.isMultiview() && mState.getNumViews() > 1);
Olli Etuaho8acb1b62018-07-30 16:20:54 +03002080}
2081
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002082angle::Result Framebuffer::ensureClearAttachmentsInitialized(const Context *context,
2083 GLbitfield mask)
Geoff Langd4fff502017-09-22 11:28:28 -04002084{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05002085 const auto &glState = context->getState();
Geoff Langd4fff502017-09-22 11:28:28 -04002086 if (!context->isRobustResourceInitEnabled() || glState.isRasterizerDiscardEnabled())
2087 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002088 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002089 }
2090
Geoff Langa36483f2018-03-09 16:11:21 -05002091 const BlendState &blend = glState.getBlendState();
2092 const DepthStencilState &depthStencil = glState.getDepthStencilState();
Geoff Langd4fff502017-09-22 11:28:28 -04002093
2094 bool color = (mask & GL_COLOR_BUFFER_BIT) != 0 && !IsColorMaskedOut(blend);
2095 bool depth = (mask & GL_DEPTH_BUFFER_BIT) != 0 && !IsDepthMaskedOut(depthStencil);
2096 bool stencil = (mask & GL_STENCIL_BUFFER_BIT) != 0 && !IsStencilMaskedOut(depthStencil);
2097
2098 if (!color && !depth && !stencil)
2099 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002100 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002101 }
2102
2103 if (partialClearNeedsInit(context, color, depth, stencil))
2104 {
2105 ANGLE_TRY(ensureDrawAttachmentsInitialized(context));
2106 }
2107
2108 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2109 // still be marked initialized. This simplifies design, allowing this method to be called before
2110 // the clear.
2111 markDrawAttachmentsInitialized(color, depth, stencil);
2112
Jamie Madill7c985f52018-11-29 18:16:17 -05002113 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002114}
2115
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002116angle::Result Framebuffer::ensureClearBufferAttachmentsInitialized(const Context *context,
2117 GLenum buffer,
2118 GLint drawbuffer)
Geoff Langd4fff502017-09-22 11:28:28 -04002119{
2120 if (!context->isRobustResourceInitEnabled() ||
Jamie Madillc3dc5d42018-12-30 12:12:04 -05002121 context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Geoff Langd4fff502017-09-22 11:28:28 -04002122 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002123 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002124 }
2125
2126 if (partialBufferClearNeedsInit(context, buffer))
2127 {
2128 ANGLE_TRY(ensureBufferInitialized(context, buffer, drawbuffer));
2129 }
2130
2131 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2132 // still be marked initialized. This simplifies design, allowing this method to be called before
2133 // the clear.
2134 markBufferInitialized(buffer, drawbuffer);
2135
Jamie Madill7c985f52018-11-29 18:16:17 -05002136 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002137}
2138
Jamie Madill6f755b22018-10-09 12:48:54 -04002139angle::Result Framebuffer::ensureDrawAttachmentsInitialized(const Context *context)
Jamie Madill05b35b22017-10-03 09:01:44 -04002140{
2141 if (!context->isRobustResourceInitEnabled())
2142 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002143 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002144 }
2145
2146 // Note: we don't actually filter by the draw attachment enum. Just init everything.
2147 for (size_t bit : mState.mResourceNeedsInit)
2148 {
2149 switch (bit)
2150 {
2151 case DIRTY_BIT_DEPTH_ATTACHMENT:
2152 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2153 break;
2154 case DIRTY_BIT_STENCIL_ATTACHMENT:
2155 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2156 break;
2157 default:
2158 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bit]));
2159 break;
2160 }
2161 }
2162
2163 mState.mResourceNeedsInit.reset();
Jamie Madill7c985f52018-11-29 18:16:17 -05002164 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002165}
2166
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002167angle::Result Framebuffer::ensureReadAttachmentsInitialized(const Context *context)
Jamie Madill05b35b22017-10-03 09:01:44 -04002168{
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002169 ASSERT(context->isRobustResourceInitEnabled());
2170
2171 if (mState.mResourceNeedsInit.none())
Jamie Madill05b35b22017-10-03 09:01:44 -04002172 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002173 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002174 }
2175
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002176 if (mState.mReadBufferState != GL_NONE)
Jamie Madill05b35b22017-10-03 09:01:44 -04002177 {
2178 size_t readIndex = mState.getReadIndex();
2179 if (mState.mResourceNeedsInit[readIndex])
2180 {
2181 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[readIndex]));
2182 mState.mResourceNeedsInit.reset(readIndex);
2183 }
2184 }
2185
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002186 // Conservatively init depth since it can be read by BlitFramebuffer.
2187 if (hasDepth())
Jamie Madill05b35b22017-10-03 09:01:44 -04002188 {
2189 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2190 {
2191 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2192 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2193 }
2194 }
2195
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002196 // Conservatively init stencil since it can be read by BlitFramebuffer.
2197 if (hasStencil())
Jamie Madill05b35b22017-10-03 09:01:44 -04002198 {
2199 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2200 {
2201 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2202 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2203 }
2204 }
2205
Jamie Madill7c985f52018-11-29 18:16:17 -05002206 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002207}
2208
2209void Framebuffer::markDrawAttachmentsInitialized(bool color, bool depth, bool stencil)
2210{
2211 // Mark attachments as initialized.
2212 if (color)
2213 {
2214 for (auto colorIndex : mState.mEnabledDrawBuffers)
2215 {
2216 auto &colorAttachment = mState.mColorAttachments[colorIndex];
2217 ASSERT(colorAttachment.isAttached());
2218 colorAttachment.setInitState(InitState::Initialized);
2219 mState.mResourceNeedsInit.reset(colorIndex);
2220 }
2221 }
2222
2223 if (depth && mState.mDepthAttachment.isAttached())
2224 {
2225 mState.mDepthAttachment.setInitState(InitState::Initialized);
2226 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2227 }
2228
2229 if (stencil && mState.mStencilAttachment.isAttached())
2230 {
2231 mState.mStencilAttachment.setInitState(InitState::Initialized);
2232 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2233 }
2234}
2235
2236void Framebuffer::markBufferInitialized(GLenum bufferType, GLint bufferIndex)
2237{
2238 switch (bufferType)
2239 {
2240 case GL_COLOR:
2241 {
2242 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2243 if (mState.mColorAttachments[bufferIndex].isAttached())
2244 {
2245 mState.mColorAttachments[bufferIndex].setInitState(InitState::Initialized);
2246 mState.mResourceNeedsInit.reset(bufferIndex);
2247 }
2248 break;
2249 }
2250 case GL_DEPTH:
2251 {
2252 if (mState.mDepthAttachment.isAttached())
2253 {
2254 mState.mDepthAttachment.setInitState(InitState::Initialized);
2255 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2256 }
2257 break;
2258 }
2259 case GL_STENCIL:
2260 {
2261 if (mState.mStencilAttachment.isAttached())
2262 {
2263 mState.mStencilAttachment.setInitState(InitState::Initialized);
2264 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2265 }
2266 break;
2267 }
2268 case GL_DEPTH_STENCIL:
2269 {
2270 if (mState.mDepthAttachment.isAttached())
2271 {
2272 mState.mDepthAttachment.setInitState(InitState::Initialized);
2273 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2274 }
2275 if (mState.mStencilAttachment.isAttached())
2276 {
2277 mState.mStencilAttachment.setInitState(InitState::Initialized);
2278 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2279 }
2280 break;
2281 }
2282 default:
2283 UNREACHABLE();
2284 break;
2285 }
2286}
2287
2288Box Framebuffer::getDimensions() const
2289{
2290 return mState.getDimensions();
2291}
2292
Jonah Ryan-Davis7151fe52019-07-17 15:15:27 -04002293Extents Framebuffer::getExtents() const
2294{
2295 return mState.getExtents();
2296}
2297
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002298angle::Result Framebuffer::ensureBufferInitialized(const Context *context,
2299 GLenum bufferType,
2300 GLint bufferIndex)
Jamie Madill05b35b22017-10-03 09:01:44 -04002301{
2302 ASSERT(context->isRobustResourceInitEnabled());
2303
2304 if (mState.mResourceNeedsInit.none())
2305 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002306 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002307 }
2308
2309 switch (bufferType)
2310 {
2311 case GL_COLOR:
2312 {
2313 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2314 if (mState.mResourceNeedsInit[bufferIndex])
2315 {
2316 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bufferIndex]));
2317 mState.mResourceNeedsInit.reset(bufferIndex);
2318 }
2319 break;
2320 }
2321 case GL_DEPTH:
2322 {
2323 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2324 {
2325 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2326 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2327 }
2328 break;
2329 }
2330 case GL_STENCIL:
2331 {
2332 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2333 {
2334 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2335 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2336 }
2337 break;
2338 }
2339 case GL_DEPTH_STENCIL:
2340 {
2341 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2342 {
2343 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2344 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2345 }
2346 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2347 {
2348 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2349 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2350 }
2351 break;
2352 }
2353 default:
2354 UNREACHABLE();
2355 break;
2356 }
2357
Jamie Madill7c985f52018-11-29 18:16:17 -05002358 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002359}
2360
2361bool Framebuffer::partialBufferClearNeedsInit(const Context *context, GLenum bufferType)
2362{
2363 if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none())
2364 {
2365 return false;
2366 }
2367
2368 switch (bufferType)
2369 {
2370 case GL_COLOR:
2371 return partialClearNeedsInit(context, true, false, false);
2372 case GL_DEPTH:
2373 return partialClearNeedsInit(context, false, true, false);
2374 case GL_STENCIL:
2375 return partialClearNeedsInit(context, false, false, true);
2376 case GL_DEPTH_STENCIL:
2377 return partialClearNeedsInit(context, false, true, true);
2378 default:
2379 UNREACHABLE();
2380 return false;
2381 }
2382}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002383} // namespace gl