blob: 19f8a657d37d4f4cc671cfd7add0eae82de38752 [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{
605 ASSERT(attachmentsHaveSameDimensions());
606 ASSERT(getFirstNonNullAttachment() != nullptr);
607 Extents extents = getFirstNonNullAttachment()->getSize();
608 return Box(0, 0, 0, extents.width, extents.height, extents.depth);
609}
610
Jamie Madill7aea7e02016-05-10 10:39:45 -0400611Framebuffer::Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id)
Jamie Madill2274b652018-05-31 10:56:08 -0400612 : mState(caps, id),
Jamie Madill362876b2016-06-16 14:46:59 -0400613 mImpl(factory->createFramebuffer(mState)),
Jamie Madill362876b2016-06-16 14:46:59 -0400614 mCachedStatus(),
615 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
616 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617{
Corentin Wallez37c39792015-08-20 14:19:46 -0400618 ASSERT(mImpl != nullptr);
Jamie Madill362876b2016-06-16 14:46:59 -0400619 ASSERT(mState.mColorAttachments.size() == static_cast<size_t>(caps.maxColorAttachments));
620
Jamie Madill1e5499d2017-04-05 11:22:16 -0400621 for (uint32_t colorIndex = 0;
622 colorIndex < static_cast<uint32_t>(mState.mColorAttachments.size()); ++colorIndex)
Jamie Madill362876b2016-06-16 14:46:59 -0400623 {
Jamie Madill1e5499d2017-04-05 11:22:16 -0400624 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
Jamie Madill362876b2016-06-16 14:46:59 -0400625 }
Corentin Wallez37c39792015-08-20 14:19:46 -0400626}
627
Geoff Langbf7b95d2018-05-01 16:48:21 -0400628Framebuffer::Framebuffer(const Context *context, egl::Surface *surface)
Jamie Madill362876b2016-06-16 14:46:59 -0400629 : mState(),
Geoff Langbf7b95d2018-05-01 16:48:21 -0400630 mImpl(surface->getImplementation()->createDefaultFramebuffer(context, mState)),
Jamie Madill362876b2016-06-16 14:46:59 -0400631 mCachedStatus(GL_FRAMEBUFFER_COMPLETE),
632 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
633 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
Corentin Wallez37c39792015-08-20 14:19:46 -0400634{
Geoff Langda88add2014-12-01 10:22:01 -0500635 ASSERT(mImpl != nullptr);
Jamie Madill1e5499d2017-04-05 11:22:16 -0400636 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Jamie Madill6f60d052017-02-22 15:20:11 -0500637
Geoff Langbf7b95d2018-05-01 16:48:21 -0400638 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400639 FramebufferAttachment::kDefaultNumViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -0700640 FramebufferAttachment::kDefaultBaseViewIndex, false);
Jamie Madill6f60d052017-02-22 15:20:11 -0500641
642 if (surface->getConfig()->depthSize > 0)
643 {
Geoff Langbf7b95d2018-05-01 16:48:21 -0400644 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400645 FramebufferAttachment::kDefaultNumViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -0700646 FramebufferAttachment::kDefaultBaseViewIndex, false);
Jamie Madill6f60d052017-02-22 15:20:11 -0500647 }
648
649 if (surface->getConfig()->stencilSize > 0)
650 {
Geoff Langbf7b95d2018-05-01 16:48:21 -0400651 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400652 FramebufferAttachment::kDefaultNumViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -0700653 FramebufferAttachment::kDefaultBaseViewIndex, false);
Jamie Madill6f60d052017-02-22 15:20:11 -0500654 }
Jamie Madill6e18a232019-01-16 13:27:14 -0500655 SetComponentTypeMask(getDrawbufferWriteType(0), 0, &mState.mDrawBufferTypeMask);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000656}
657
Corentin Wallezccab69d2017-01-27 16:57:15 -0500658Framebuffer::Framebuffer(rx::GLImplFactory *factory)
659 : mState(),
660 mImpl(factory->createFramebuffer(mState)),
Corentin Wallezccab69d2017-01-27 16:57:15 -0500661 mCachedStatus(GL_FRAMEBUFFER_UNDEFINED_OES),
662 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
663 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
664{
Jamie Madill1e5499d2017-04-05 11:22:16 -0400665 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Jamie Madill6e18a232019-01-16 13:27:14 -0500666 SetComponentTypeMask(getDrawbufferWriteType(0), 0, &mState.mDrawBufferTypeMask);
Corentin Wallezccab69d2017-01-27 16:57:15 -0500667}
668
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669Framebuffer::~Framebuffer()
670{
Geoff Langda88add2014-12-01 10:22:01 -0500671 SafeDelete(mImpl);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000672}
673
Jamie Madill4928b7c2017-06-20 12:57:39 -0400674void Framebuffer::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500675{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400676 for (auto &attachment : mState.mColorAttachments)
677 {
678 attachment.detach(context);
679 }
680 mState.mDepthAttachment.detach(context);
681 mState.mStencilAttachment.detach(context);
682 mState.mWebGLDepthAttachment.detach(context);
683 mState.mWebGLStencilAttachment.detach(context);
684 mState.mWebGLDepthStencilAttachment.detach(context);
685
Jamie Madillc564c072017-06-01 12:45:42 -0400686 mImpl->destroy(context);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500687}
688
Jamie Madille90d4ee2018-11-28 14:04:00 -0500689void Framebuffer::setLabel(const Context *context, const std::string &label)
Geoff Lang70d0f492015-12-10 17:45:46 -0500690{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400691 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500692}
693
694const std::string &Framebuffer::getLabel() const
695{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400696 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500697}
698
Jamie Madill8693bdb2017-09-02 15:32:14 -0400699bool Framebuffer::detachTexture(const Context *context, GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400701 return detachResourceById(context, GL_TEXTURE, textureId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000702}
703
Jamie Madill8693bdb2017-09-02 15:32:14 -0400704bool Framebuffer::detachRenderbuffer(const Context *context, GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000705{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400706 return detachResourceById(context, GL_RENDERBUFFER, renderbufferId);
Jamie Madilld1405e52015-03-05 15:41:39 -0500707}
Jamie Madille261b442014-06-25 12:42:21 -0400708
Jamie Madill8693bdb2017-09-02 15:32:14 -0400709bool Framebuffer::detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId)
Jamie Madilld1405e52015-03-05 15:41:39 -0500710{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400711 bool found = false;
712
Jamie Madill362876b2016-06-16 14:46:59 -0400713 for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
Jamie Madilld1405e52015-03-05 15:41:39 -0500714 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400715 if (detachMatchingAttachment(context, &mState.mColorAttachments[colorIndex], resourceType,
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300716 resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400717 {
718 found = true;
719 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000720 }
721
Jamie Madilla02315b2017-02-23 14:14:47 -0500722 if (context->isWebGL1())
723 {
724 const std::array<FramebufferAttachment *, 3> attachments = {
725 {&mState.mWebGLDepthStencilAttachment, &mState.mWebGLDepthAttachment,
726 &mState.mWebGLStencilAttachment}};
727 for (FramebufferAttachment *attachment : attachments)
728 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300729 if (detachMatchingAttachment(context, attachment, resourceType, resourceId))
Jamie Madilla02315b2017-02-23 14:14:47 -0500730 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400731 found = true;
Jamie Madilla02315b2017-02-23 14:14:47 -0500732 }
733 }
734 }
735 else
736 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300737 if (detachMatchingAttachment(context, &mState.mDepthAttachment, resourceType, resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400738 {
739 found = true;
740 }
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300741 if (detachMatchingAttachment(context, &mState.mStencilAttachment, resourceType, resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400742 {
743 found = true;
744 }
Jamie Madilla02315b2017-02-23 14:14:47 -0500745 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400746
747 return found;
Jamie Madill362876b2016-06-16 14:46:59 -0400748}
749
Jamie Madill8693bdb2017-09-02 15:32:14 -0400750bool Framebuffer::detachMatchingAttachment(const Context *context,
Jamie Madill4928b7c2017-06-20 12:57:39 -0400751 FramebufferAttachment *attachment,
Jamie Madill362876b2016-06-16 14:46:59 -0400752 GLenum matchType,
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300753 GLuint matchId)
Jamie Madill362876b2016-06-16 14:46:59 -0400754{
755 if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId)
756 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300757 // We go through resetAttachment to make sure that all the required bookkeeping will be done
758 // such as updating enabled draw buffer state.
759 resetAttachment(context, attachment->getBinding());
Jamie Madill8693bdb2017-09-02 15:32:14 -0400760 return true;
Jamie Madill362876b2016-06-16 14:46:59 -0400761 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400762
763 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764}
765
Corentin Wallez37c39792015-08-20 14:19:46 -0400766const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000767{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400768 return mState.getColorAttachment(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000769}
770
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400771const FramebufferAttachment *Framebuffer::getDepthbuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400772{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400773 return mState.getDepthAttachment();
Geoff Lang646559f2013-08-15 11:08:15 -0400774}
775
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400776const FramebufferAttachment *Framebuffer::getStencilbuffer() const
777{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400778 return mState.getStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400779}
780
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400781const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
782{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400783 return mState.getDepthStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400784}
785
786const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000787{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400788 return mState.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000789}
790
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500791const FramebufferAttachment *Framebuffer::getStencilOrDepthStencilAttachment() const
792{
793 return mState.getStencilOrDepthStencilAttachment();
794}
795
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400796const FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000797{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400798 return mState.getReadAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000799}
800
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000801GLenum Framebuffer::getReadColorbufferType() const
802{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400803 const FramebufferAttachment *readAttachment = mState.getReadAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400804 return (readAttachment != nullptr ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000805}
806
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400807const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000808{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400809 return mState.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000810}
811
Jamie Madill6dd06ea2017-07-19 13:47:55 -0400812const FramebufferAttachment *Framebuffer::getFirstNonNullAttachment() const
813{
814 return mState.getFirstNonNullAttachment();
815}
816
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800817const FramebufferAttachment *Framebuffer::getAttachment(const Context *context,
818 GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000819{
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800820 return mState.getAttachment(context, attachment);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400821}
822
Geoff Langa15472a2015-08-11 11:48:03 -0400823size_t Framebuffer::getDrawbufferStateCount() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000824{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400825 return mState.mDrawBufferStates.size();
Geoff Langa15472a2015-08-11 11:48:03 -0400826}
827
828GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const
829{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400830 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
831 return mState.mDrawBufferStates[drawBuffer];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000832}
833
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500834const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const
835{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400836 return mState.getDrawBufferStates();
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500837}
838
Geoff Lang164d54e2014-12-01 10:55:33 -0500839void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000840{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400841 auto &drawStates = mState.mDrawBufferStates;
Jamie Madilld1405e52015-03-05 15:41:39 -0500842
843 ASSERT(count <= drawStates.size());
844 std::copy(buffers, buffers + count, drawStates.begin());
845 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500846 mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS);
Jamie Madilla4595b82017-01-11 17:36:34 -0500847
848 mState.mEnabledDrawBuffers.reset();
Brandon Jones76746f92017-11-22 11:44:41 -0800849 mState.mDrawBufferTypeMask.reset();
850
Jamie Madilla4595b82017-01-11 17:36:34 -0500851 for (size_t index = 0; index < count; ++index)
852 {
Jamie Madill6e18a232019-01-16 13:27:14 -0500853 SetComponentTypeMask(getDrawbufferWriteType(index), index, &mState.mDrawBufferTypeMask);
Brandon Jones76746f92017-11-22 11:44:41 -0800854
Jamie Madilla4595b82017-01-11 17:36:34 -0500855 if (drawStates[index] != GL_NONE && mState.mColorAttachments[index].isAttached())
856 {
857 mState.mEnabledDrawBuffers.set(index);
858 }
859 }
Geoff Lang9dd95802014-12-01 11:12:59 -0500860}
861
Geoff Langa15472a2015-08-11 11:48:03 -0400862const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const
863{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400864 return mState.getDrawBuffer(drawBuffer);
Geoff Langa15472a2015-08-11 11:48:03 -0400865}
866
Jamie Madill6e18a232019-01-16 13:27:14 -0500867ComponentType Framebuffer::getDrawbufferWriteType(size_t drawBuffer) const
Geoff Lange0cff192017-05-30 13:04:56 -0400868{
869 const FramebufferAttachment *attachment = mState.getDrawBuffer(drawBuffer);
870 if (attachment == nullptr)
871 {
Jamie Madill6e18a232019-01-16 13:27:14 -0500872 return ComponentType::NoType;
Geoff Lange0cff192017-05-30 13:04:56 -0400873 }
874
875 GLenum componentType = attachment->getFormat().info->componentType;
876 switch (componentType)
877 {
878 case GL_INT:
Jamie Madill6e18a232019-01-16 13:27:14 -0500879 return ComponentType::Int;
Geoff Lange0cff192017-05-30 13:04:56 -0400880 case GL_UNSIGNED_INT:
Jamie Madill6e18a232019-01-16 13:27:14 -0500881 return ComponentType::UnsignedInt;
Geoff Lange0cff192017-05-30 13:04:56 -0400882
883 default:
Jamie Madill6e18a232019-01-16 13:27:14 -0500884 return ComponentType::Float;
Geoff Lange0cff192017-05-30 13:04:56 -0400885 }
886}
887
Brandon Jonesc405ae72017-12-06 14:15:03 -0800888ComponentTypeMask Framebuffer::getDrawBufferTypeMask() const
Brandon Jones76746f92017-11-22 11:44:41 -0800889{
890 return mState.mDrawBufferTypeMask;
891}
892
893DrawBufferMask Framebuffer::getDrawBufferMask() const
894{
895 return mState.mEnabledDrawBuffers;
896}
897
Geoff Langa15472a2015-08-11 11:48:03 -0400898bool Framebuffer::hasEnabledDrawBuffer() const
899{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400900 for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
Geoff Langa15472a2015-08-11 11:48:03 -0400901 {
902 if (getDrawBuffer(drawbufferIdx) != nullptr)
903 {
904 return true;
905 }
906 }
907
908 return false;
909}
910
Geoff Lang9dd95802014-12-01 11:12:59 -0500911GLenum Framebuffer::getReadBufferState() const
912{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400913 return mState.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500914}
915
916void Framebuffer::setReadBuffer(GLenum buffer)
917{
Jamie Madillb885e572015-02-03 16:16:04 -0500918 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
919 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madill48ef11b2016-04-27 15:21:52 -0400920 (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size()));
921 mState.mReadBufferState = buffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500922 mDirtyBits.set(DIRTY_BIT_READ_BUFFER);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000923}
924
Corentin Wallez37c39792015-08-20 14:19:46 -0400925size_t Framebuffer::getNumColorBuffers() const
926{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400927 return mState.mColorAttachments.size();
Corentin Wallez37c39792015-08-20 14:19:46 -0400928}
929
Jamie Madill0df8fe42015-11-24 16:10:24 -0500930bool Framebuffer::hasDepth() const
931{
Jamie Madill9c335862017-07-18 11:51:38 -0400932 return mState.hasDepth();
Jamie Madill0df8fe42015-11-24 16:10:24 -0500933}
934
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000935bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000936{
Jamie Madill9c335862017-07-18 11:51:38 -0400937 return mState.hasStencil();
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000938}
939
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000940bool Framebuffer::usingExtendedDrawBuffers() const
941{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400942 for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000943 {
Geoff Langa15472a2015-08-11 11:48:03 -0400944 if (getDrawBuffer(drawbufferIdx) != nullptr)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000945 {
946 return true;
947 }
948 }
949
950 return false;
951}
952
Jamie Madillb983a4b2018-08-01 11:34:51 -0400953void Framebuffer::invalidateCompletenessCache(const Context *context)
Geoff Lang9aded172017-04-05 11:07:56 -0400954{
Jamie Madill2274b652018-05-31 10:56:08 -0400955 if (mState.mId != 0)
Geoff Lang9aded172017-04-05 11:07:56 -0400956 {
957 mCachedStatus.reset();
958 }
Jamie Madill0a1eeb82019-05-13 13:53:18 -0400959 onStateChange(context, angle::SubjectMessage::DirtyBitsFlagged);
Geoff Lang9aded172017-04-05 11:07:56 -0400960}
961
Jamie Madillcc73f242018-08-01 11:34:48 -0400962GLenum Framebuffer::checkStatusImpl(const Context *context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963{
Jamie Madillcc73f242018-08-01 11:34:48 -0400964 ASSERT(!isDefault());
965 ASSERT(hasAnyDirtyBit() || !mCachedStatus.valid());
Geoff Lang528ce3c2014-12-01 10:44:07 -0500966
Jamie Madillcc73f242018-08-01 11:34:48 -0400967 mCachedStatus = checkStatusWithGLFrontEnd(context);
Jamie Madille98b1b52018-03-08 09:47:23 -0500968
Jamie Madillcc73f242018-08-01 11:34:48 -0400969 if (mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE)
970 {
Jamie Madill67220092019-05-20 11:12:53 -0400971 // We can skip syncState on several back-ends.
972 if (mImpl->shouldSyncStateBeforeCheckStatus())
Jamie Madille98b1b52018-03-08 09:47:23 -0500973 {
Jamie Madill67220092019-05-20 11:12:53 -0400974 angle::Result err = syncState(context);
975 if (err != angle::Result::Continue)
976 {
977 return 0;
978 }
Jamie Madillcc73f242018-08-01 11:34:48 -0400979 }
Jamie Madill67220092019-05-20 11:12:53 -0400980
Jamie Madillcc73f242018-08-01 11:34:48 -0400981 if (!mImpl->checkStatus(context))
982 {
983 mCachedStatus = GL_FRAMEBUFFER_UNSUPPORTED;
Jamie Madille98b1b52018-03-08 09:47:23 -0500984 }
Jamie Madill362876b2016-06-16 14:46:59 -0400985 }
986
Jamie Madill427064d2018-04-13 16:20:34 -0400987 return mCachedStatus.value();
Jamie Madill362876b2016-06-16 14:46:59 -0400988}
989
Jamie Madille98b1b52018-03-08 09:47:23 -0500990GLenum Framebuffer::checkStatusWithGLFrontEnd(const Context *context)
Jamie Madill362876b2016-06-16 14:46:59 -0400991{
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500992 const State &state = context->getState();
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400993
Jamie Madill2274b652018-05-31 10:56:08 -0400994 ASSERT(mState.mId != 0);
Jamie Madill362876b2016-06-16 14:46:59 -0400995
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400996 bool hasAttachments = false;
997 Optional<unsigned int> colorbufferSize;
998 Optional<int> samples;
Geoff Lang92019432017-11-20 13:09:34 -0500999 Optional<bool> fixedSampleLocations;
JiangYizhou461d9a32017-01-04 16:37:26 +08001000 bool hasRenderbuffer = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001
Martin Radev9bc9a322017-07-21 14:28:17 +03001002 const FramebufferAttachment *firstAttachment = getFirstNonNullAttachment();
1003
Jiawei Shaoa8802472018-05-28 11:17:47 +08001004 Optional<bool> isLayered;
1005 Optional<TextureType> colorAttachmentsTextureType;
1006
Jamie Madill48ef11b2016-04-27 15:21:52 -04001007 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008 {
Jamie Madill2d06b732015-04-20 12:53:28 -04001009 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001011 if (!CheckAttachmentCompleteness(context, colorAttachment))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001012 {
1013 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1014 }
daniel@transgaming.com01868132010-08-24 19:21:17 +00001015
Geoff Lang677bb6f2017-04-05 12:40:40 -04001016 const InternalFormat &format = *colorAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001017 if (format.depthBits > 0 || format.stencilBits > 0)
1018 {
1019 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1020 }
1021
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001022 if (!CheckAttachmentSampleCompleteness(context, colorAttachment, true, &samples,
1023 &fixedSampleLocations))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001024 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001025 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001026 }
1027
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001028 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
1029 // in GLES 3.0, there is no such restriction
1030 if (state.getClientMajorVersion() < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001031 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001032 if (colorbufferSize.valid())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001033 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001034 if (format.pixelBytes != colorbufferSize.value())
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +00001035 {
1036 return GL_FRAMEBUFFER_UNSUPPORTED;
1037 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001038 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001039 else
1040 {
1041 colorbufferSize = format.pixelBytes;
1042 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001043 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001044
Martin Radev9bc9a322017-07-21 14:28:17 +03001045 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &colorAttachment))
1046 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001047 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001048 }
1049
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001050 hasRenderbuffer = hasRenderbuffer || (colorAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001051
1052 if (!hasAttachments)
1053 {
1054 isLayered = colorAttachment.isLayered();
1055 if (isLayered.value())
1056 {
1057 colorAttachmentsTextureType = colorAttachment.getTextureImageIndex().getType();
1058 }
1059 hasAttachments = true;
1060 }
1061 else
1062 {
1063 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1064 // If any framebuffer attachment is layered, all populated attachments
1065 // must be layered. Additionally, all populated color attachments must
1066 // be from textures of the same target. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1067 ASSERT(isLayered.valid());
1068 if (isLayered.value() != colorAttachment.isLayered())
1069 {
1070 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1071 }
1072 else if (isLayered.value())
1073 {
1074 ASSERT(colorAttachmentsTextureType.valid());
1075 if (colorAttachmentsTextureType.value() !=
1076 colorAttachment.getTextureImageIndex().getType())
1077 {
1078 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1079 }
1080 }
1081 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001082 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001083 }
1084
Jamie Madill48ef11b2016-04-27 15:21:52 -04001085 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001086 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001087 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001088 if (!CheckAttachmentCompleteness(context, depthAttachment))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001090 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091 }
1092
Geoff Lang677bb6f2017-04-05 12:40:40 -04001093 const InternalFormat &format = *depthAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001094 if (format.depthBits == 0)
1095 {
1096 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001097 }
1098
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001099 if (!CheckAttachmentSampleCompleteness(context, depthAttachment, false, &samples,
1100 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001101 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001102 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001103 }
Sami Väisänena797e062016-05-12 15:23:40 +03001104
Martin Radev9bc9a322017-07-21 14:28:17 +03001105 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &depthAttachment))
1106 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001107 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001108 }
1109
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001110 hasRenderbuffer = hasRenderbuffer || (depthAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001111
1112 if (!hasAttachments)
1113 {
1114 isLayered = depthAttachment.isLayered();
1115 hasAttachments = true;
1116 }
1117 else
1118 {
1119 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1120 // If any framebuffer attachment is layered, all populated attachments
1121 // must be layered. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1122 ASSERT(isLayered.valid());
1123 if (isLayered.value() != depthAttachment.isLayered())
1124 {
1125 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1126 }
1127 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001128 }
1129
Jamie Madill48ef11b2016-04-27 15:21:52 -04001130 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001131 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001132 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001133 if (!CheckAttachmentCompleteness(context, stencilAttachment))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001134 {
1135 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1136 }
1137
Geoff Lang677bb6f2017-04-05 12:40:40 -04001138 const InternalFormat &format = *stencilAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001139 if (format.stencilBits == 0)
1140 {
1141 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001142 }
1143
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001144 if (!CheckAttachmentSampleCompleteness(context, stencilAttachment, false, &samples,
1145 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001146 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001147 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001148 }
Corentin Wallez086d59a2016-04-29 09:06:49 -04001149
Martin Radev9bc9a322017-07-21 14:28:17 +03001150 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &stencilAttachment))
1151 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001152 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001153 }
1154
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001155 hasRenderbuffer = hasRenderbuffer || (stencilAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001156
1157 if (!hasAttachments)
1158 {
1159 hasAttachments = true;
1160 }
1161 else
1162 {
1163 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1164 // If any framebuffer attachment is layered, all populated attachments
1165 // must be layered.
1166 // {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1167 ASSERT(isLayered.valid());
1168 if (isLayered.value() != stencilAttachment.isLayered())
1169 {
1170 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1171 }
1172 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001173 }
1174
1175 // Starting from ES 3.0 stencil and depth, if present, should be the same image
1176 if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() &&
1177 stencilAttachment.isAttached() && stencilAttachment != depthAttachment)
1178 {
1179 return GL_FRAMEBUFFER_UNSUPPORTED;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001180 }
1181
Jamie Madilla02315b2017-02-23 14:14:47 -05001182 // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL.
1183 if (state.isWebGL1())
1184 {
1185 if (!mState.mWebGLDepthStencilConsistent)
1186 {
1187 return GL_FRAMEBUFFER_UNSUPPORTED;
1188 }
1189
1190 if (mState.mWebGLDepthStencilAttachment.isAttached())
1191 {
1192 if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 ||
1193 mState.mWebGLDepthStencilAttachment.getStencilSize() == 0)
1194 {
1195 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1196 }
Martin Radev9bc9a322017-07-21 14:28:17 +03001197
1198 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment,
1199 &mState.mWebGLDepthStencilAttachment))
1200 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001201 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001202 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001203 }
1204 else if (mState.mStencilAttachment.isAttached() &&
1205 mState.mStencilAttachment.getDepthSize() > 0)
1206 {
1207 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1208 }
1209 else if (mState.mDepthAttachment.isAttached() &&
1210 mState.mDepthAttachment.getStencilSize() > 0)
1211 {
1212 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1213 }
1214 }
1215
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001216 // ES3.1(section 9.4) requires that if no image is attached to the framebuffer, and either the
1217 // value of the framebuffer's FRAMEBUFFER_DEFAULT_WIDTH or FRAMEBUFFER_DEFAULT_HEIGHT parameters
1218 // is zero, the framebuffer is considered incomplete.
JiangYizhou461d9a32017-01-04 16:37:26 +08001219 GLint defaultWidth = mState.getDefaultWidth();
1220 GLint defaultHeight = mState.getDefaultHeight();
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001221 if (!hasAttachments && (defaultWidth == 0 || defaultHeight == 0))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001222 {
1223 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001224 }
1225
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001226 // In ES 2.0 and WebGL, all color attachments must have the same width and height.
Jamie Madillcc86d642015-11-24 13:00:07 -05001227 // In ES 3.0, there is no such restriction.
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001228 if ((state.getClientMajorVersion() < 3 || state.getExtensions().webglCompatibility) &&
1229 !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -05001230 {
1231 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
1232 }
1233
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001234 // ES3.1(section 9.4) requires that if the attached images are a mix of renderbuffers and
1235 // textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all attached textures.
JiangYizhou461d9a32017-01-04 16:37:26 +08001236 if (fixedSampleLocations.valid() && hasRenderbuffer && !fixedSampleLocations.value())
1237 {
1238 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1239 }
1240
Kenneth Russellce8602a2017-10-03 18:23:08 -07001241 // The WebGL conformance tests implicitly define that all framebuffer
1242 // attachments must be unique. For example, the same level of a texture can
1243 // not be attached to two different color attachments.
1244 if (state.getExtensions().webglCompatibility)
1245 {
1246 if (!mState.colorAttachmentsAreUniqueImages())
1247 {
1248 return GL_FRAMEBUFFER_UNSUPPORTED;
1249 }
1250 }
1251
Jamie Madillcc86d642015-11-24 13:00:07 -05001252 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001253}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001254
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001255angle::Result Framebuffer::discard(const Context *context, size_t count, const GLenum *attachments)
Austin Kinross08332632015-05-05 13:35:47 -07001256{
Jamie Madill05b35b22017-10-03 09:01:44 -04001257 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1258 // can be no-ops, so we should probably do that to ensure consistency.
1259 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1260
Jamie Madill4928b7c2017-06-20 12:57:39 -04001261 return mImpl->discard(context, count, attachments);
Austin Kinross08332632015-05-05 13:35:47 -07001262}
1263
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001264angle::Result Framebuffer::invalidate(const Context *context,
1265 size_t count,
1266 const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001267{
Jamie Madill05b35b22017-10-03 09:01:44 -04001268 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1269 // can be no-ops, so we should probably do that to ensure consistency.
1270 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1271
Jamie Madill4928b7c2017-06-20 12:57:39 -04001272 return mImpl->invalidate(context, count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001273}
1274
Jamie Madill05b35b22017-10-03 09:01:44 -04001275bool Framebuffer::partialClearNeedsInit(const Context *context,
1276 bool color,
1277 bool depth,
1278 bool stencil)
1279{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001280 const auto &glState = context->getState();
Jamie Madill05b35b22017-10-03 09:01:44 -04001281
1282 if (!glState.isRobustResourceInitEnabled())
1283 {
1284 return false;
1285 }
1286
1287 // Scissors can affect clearing.
1288 // TODO(jmadill): Check for complete scissor overlap.
1289 if (glState.isScissorTestEnabled())
1290 {
1291 return true;
1292 }
1293
1294 // If colors masked, we must clear before we clear. Do a simple check.
1295 // TODO(jmadill): Filter out unused color channels from the test.
1296 if (color)
1297 {
1298 const auto &blend = glState.getBlendState();
1299 if (!(blend.colorMaskRed && blend.colorMaskGreen && blend.colorMaskBlue &&
1300 blend.colorMaskAlpha))
1301 {
1302 return true;
1303 }
1304 }
1305
1306 const auto &depthStencil = glState.getDepthStencilState();
Yuly Novikov21edf3d2018-07-23 16:44:16 -04001307 if (stencil && (depthStencil.stencilMask != depthStencil.stencilWritemask ||
1308 depthStencil.stencilBackMask != depthStencil.stencilBackWritemask))
Jamie Madill05b35b22017-10-03 09:01:44 -04001309 {
1310 return true;
1311 }
1312
1313 return false;
1314}
1315
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001316angle::Result Framebuffer::invalidateSub(const Context *context,
1317 size_t count,
1318 const GLenum *attachments,
1319 const Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -04001320{
Jamie Madill05b35b22017-10-03 09:01:44 -04001321 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1322 // can be no-ops, so we should probably do that to ensure consistency.
1323 // TODO(jmadill): Make a invalidate no-op in WebGL 2.0.
1324
Jamie Madill4928b7c2017-06-20 12:57:39 -04001325 return mImpl->invalidateSub(context, count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -04001326}
1327
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001328angle::Result Framebuffer::clear(const Context *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -05001329{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001330 const auto &glState = context->getState();
Jamie Madill05b35b22017-10-03 09:01:44 -04001331 if (glState.isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -05001332 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001333 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001334 }
1335
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001336 // Remove clear bits that are ineffective. An effective clear changes at least one fragment. If
1337 // color/depth/stencil masks make the clear ineffective we skip it altogether.
1338
1339 // If all color channels are masked, don't attempt to clear color.
1340 if (context->getState().getBlendState().allChannelsMasked())
1341 {
1342 mask &= ~GL_COLOR_BUFFER_BIT;
1343 }
1344
1345 // If depth write is disabled, don't attempt to clear depth.
1346 if (!context->getState().getDepthStencilState().depthMask)
1347 {
1348 mask &= ~GL_DEPTH_BUFFER_BIT;
1349 }
1350
1351 // If all stencil bits are masked, don't attempt to clear stencil.
1352 if (context->getState().getDepthStencilState().stencilWritemask == 0)
1353 {
1354 mask &= ~GL_STENCIL_BUFFER_BIT;
1355 }
1356
1357 if (mask != 0)
1358 {
1359 ANGLE_TRY(mImpl->clear(context, mask));
1360 }
Jamie Madill05b35b22017-10-03 09:01:44 -04001361
Jamie Madill7c985f52018-11-29 18:16:17 -05001362 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001363}
1364
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001365angle::Result Framebuffer::clearBufferfv(const Context *context,
1366 GLenum buffer,
1367 GLint drawbuffer,
1368 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001369{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001370 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001371 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001372 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001373 }
1374
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001375 if (buffer == GL_DEPTH)
1376 {
1377 // If depth write is disabled, don't attempt to clear depth.
1378 if (!context->getState().getDepthStencilState().depthMask)
1379 {
1380 return angle::Result::Continue;
1381 }
1382 }
1383 else
1384 {
1385 // If all color channels are masked, don't attempt to clear color.
1386 if (context->getState().getBlendState().allChannelsMasked())
1387 {
1388 return angle::Result::Continue;
1389 }
1390 }
1391
Jamie Madill05b35b22017-10-03 09:01:44 -04001392 ANGLE_TRY(mImpl->clearBufferfv(context, buffer, drawbuffer, values));
1393
Jamie Madill7c985f52018-11-29 18:16:17 -05001394 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001395}
1396
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001397angle::Result Framebuffer::clearBufferuiv(const Context *context,
1398 GLenum buffer,
1399 GLint drawbuffer,
1400 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001401{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001402 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001403 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001404 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001405 }
1406
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001407 // If all color channels are masked, don't attempt to clear color.
1408 if (context->getState().getBlendState().allChannelsMasked())
1409 {
1410 return angle::Result::Continue;
1411 }
1412
Jamie Madill05b35b22017-10-03 09:01:44 -04001413 ANGLE_TRY(mImpl->clearBufferuiv(context, buffer, drawbuffer, values));
1414
Jamie Madill7c985f52018-11-29 18:16:17 -05001415 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001416}
1417
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001418angle::Result Framebuffer::clearBufferiv(const Context *context,
1419 GLenum buffer,
1420 GLint drawbuffer,
1421 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001422{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001423 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001424 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001425 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001426 }
1427
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001428 if (buffer == GL_STENCIL)
1429 {
1430 // If all stencil bits are masked, don't attempt to clear stencil.
1431 if (context->getState().getDepthStencilState().stencilWritemask == 0)
1432 {
1433 return angle::Result::Continue;
1434 }
1435 }
1436 else
1437 {
1438 // If all color channels are masked, don't attempt to clear color.
1439 if (context->getState().getBlendState().allChannelsMasked())
1440 {
1441 return angle::Result::Continue;
1442 }
1443 }
1444
Jamie Madill05b35b22017-10-03 09:01:44 -04001445 ANGLE_TRY(mImpl->clearBufferiv(context, buffer, drawbuffer, values));
1446
Jamie Madill7c985f52018-11-29 18:16:17 -05001447 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001448}
1449
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001450angle::Result Framebuffer::clearBufferfi(const Context *context,
1451 GLenum buffer,
1452 GLint drawbuffer,
1453 GLfloat depth,
1454 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -05001455{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001456 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001457 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001458 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001459 }
1460
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001461 bool clearDepth = context->getState().getDepthStencilState().depthMask;
1462 bool clearStencil = context->getState().getDepthStencilState().stencilWritemask != 0;
1463
1464 if (clearDepth && clearStencil)
1465 {
1466 ASSERT(buffer == GL_DEPTH_STENCIL);
1467 ANGLE_TRY(mImpl->clearBufferfi(context, GL_DEPTH_STENCIL, drawbuffer, depth, stencil));
1468 }
1469 else if (clearDepth && !clearStencil)
1470 {
1471 ANGLE_TRY(mImpl->clearBufferfv(context, GL_DEPTH, drawbuffer, &depth));
1472 }
1473 else if (!clearDepth && clearStencil)
1474 {
1475 ANGLE_TRY(mImpl->clearBufferiv(context, GL_STENCIL, drawbuffer, &stencil));
1476 }
Jamie Madill05b35b22017-10-03 09:01:44 -04001477
Jamie Madill7c985f52018-11-29 18:16:17 -05001478 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001479}
1480
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001481angle::Result Framebuffer::getImplementationColorReadFormat(const Context *context,
1482 GLenum *formatOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001483{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001484 ANGLE_TRY(syncState(context));
1485 *formatOut = mImpl->getImplementationColorReadFormat(context);
Jamie Madill7c985f52018-11-29 18:16:17 -05001486 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001487}
1488
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001489angle::Result Framebuffer::getImplementationColorReadType(const Context *context, GLenum *typeOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001490{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001491 ANGLE_TRY(syncState(context));
1492 *typeOut = mImpl->getImplementationColorReadType(context);
Jamie Madill7c985f52018-11-29 18:16:17 -05001493 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001494}
1495
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001496angle::Result Framebuffer::readPixels(const Context *context,
1497 const Rectangle &area,
1498 GLenum format,
1499 GLenum type,
1500 void *pixels)
Geoff Langbce529e2014-12-01 12:48:41 -05001501{
Jamie Madill362876b2016-06-16 14:46:59 -04001502 ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels));
Geoff Lang520c4ae2015-05-05 13:12:36 -04001503
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001504 Buffer *unpackBuffer = context->getState().getTargetBuffer(BufferBinding::PixelUnpack);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001505 if (unpackBuffer)
1506 {
Jamie Madill09463932018-04-04 05:26:59 -04001507 unpackBuffer->onPixelPack(context);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001508 }
1509
Jamie Madill7c985f52018-11-29 18:16:17 -05001510 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001511}
1512
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001513angle::Result Framebuffer::blit(const Context *context,
1514 const Rectangle &sourceArea,
1515 const Rectangle &destArea,
1516 GLbitfield mask,
1517 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -05001518{
He Yunchao6be602d2016-12-22 14:33:07 +08001519 GLbitfield blitMask = mask;
1520
1521 // Note that blitting is called against draw framebuffer.
1522 // See the code in gl::Context::blitFramebuffer.
1523 if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer())
1524 {
1525 blitMask &= ~GL_COLOR_BUFFER_BIT;
1526 }
1527
1528 if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr)
1529 {
1530 blitMask &= ~GL_STENCIL_BUFFER_BIT;
1531 }
1532
1533 if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr)
1534 {
1535 blitMask &= ~GL_DEPTH_BUFFER_BIT;
1536 }
1537
1538 if (!blitMask)
1539 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001540 return angle::Result::Continue;
He Yunchao6be602d2016-12-22 14:33:07 +08001541 }
1542
1543 return mImpl->blit(context, sourceArea, destArea, blitMask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -05001544}
1545
Luc Ferronbf6dc372018-06-28 15:24:19 -04001546bool Framebuffer::isDefault() const
1547{
1548 return id() == 0;
1549}
1550
Jamie Madill427064d2018-04-13 16:20:34 -04001551int Framebuffer::getSamples(const Context *context)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001552{
Jamie Madill427064d2018-04-13 16:20:34 -04001553 return (isComplete(context) ? getCachedSamples(context) : 0);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001554}
1555
Jamie Madill9c335862017-07-18 11:51:38 -04001556int Framebuffer::getCachedSamples(const Context *context)
1557{
Jamie Madill5b772312018-03-08 20:28:32 -05001558 ASSERT(mCachedStatus.valid() && mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE);
1559
Jamie Madill9c335862017-07-18 11:51:38 -04001560 // For a complete framebuffer, all attachments must have the same sample count.
1561 // In this case return the first nonzero sample size.
1562 const auto *firstNonNullAttachment = mState.getFirstNonNullAttachment();
1563 if (firstNonNullAttachment)
1564 {
1565 ASSERT(firstNonNullAttachment->isAttached());
1566 return firstNonNullAttachment->getSamples();
1567 }
1568
1569 // No attachments found.
1570 return 0;
1571}
1572
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001573angle::Result Framebuffer::getSamplePosition(const Context *context,
1574 size_t index,
1575 GLfloat *xy) const
Corentin Wallezccab69d2017-01-27 16:57:15 -05001576{
Geoff Lang13455072018-05-09 11:24:43 -04001577 ANGLE_TRY(mImpl->getSamplePosition(context, index, xy));
Jamie Madill7c985f52018-11-29 18:16:17 -05001578 return angle::Result::Continue;
Corentin Wallezccab69d2017-01-27 16:57:15 -05001579}
1580
Jamie Madille261b442014-06-25 12:42:21 -04001581bool Framebuffer::hasValidDepthStencil() const
1582{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001583 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -04001584}
1585
Jamie Madilla02315b2017-02-23 14:14:47 -05001586void Framebuffer::setAttachment(const Context *context,
1587 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -04001588 GLenum binding,
1589 const ImageIndex &textureIndex,
1590 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -04001591{
Martin Radev5dae57b2017-07-14 16:15:55 +03001592 setAttachment(context, type, binding, textureIndex, resource,
1593 FramebufferAttachment::kDefaultNumViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001594 FramebufferAttachment::kDefaultBaseViewIndex, false);
Martin Radev5dae57b2017-07-14 16:15:55 +03001595}
1596
1597void Framebuffer::setAttachment(const Context *context,
1598 GLenum type,
1599 GLenum binding,
1600 const ImageIndex &textureIndex,
1601 FramebufferAttachmentObject *resource,
1602 GLsizei numViews,
1603 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001604 bool isMultiview)
Martin Radev5dae57b2017-07-14 16:15:55 +03001605{
Jamie Madilla02315b2017-02-23 14:14:47 -05001606 // Context may be null in unit tests.
1607 if (!context || !context->isWebGL1())
1608 {
Martin Radev5dae57b2017-07-14 16:15:55 +03001609 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001610 isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001611 return;
1612 }
1613
1614 switch (binding)
1615 {
1616 case GL_DEPTH_STENCIL:
1617 case GL_DEPTH_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001618 mState.mWebGLDepthStencilAttachment.attach(context, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001619 resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001620 isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001621 break;
1622 case GL_DEPTH:
1623 case GL_DEPTH_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001624 mState.mWebGLDepthAttachment.attach(context, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001625 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001626 break;
1627 case GL_STENCIL:
1628 case GL_STENCIL_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001629 mState.mWebGLStencilAttachment.attach(context, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001630 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001631 break;
1632 default:
Martin Radev5dae57b2017-07-14 16:15:55 +03001633 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001634 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001635 return;
1636 }
1637
Mingyu Hu7d64c482019-03-12 14:27:40 -07001638 commitWebGL1DepthStencilIfConsistent(context, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001639}
1640
Mingyu Hu7d64c482019-03-12 14:27:40 -07001641void Framebuffer::setAttachmentMultiview(const Context *context,
1642 GLenum type,
1643 GLenum binding,
1644 const ImageIndex &textureIndex,
1645 FramebufferAttachmentObject *resource,
1646 GLsizei numViews,
1647 GLint baseViewIndex)
Martin Radev82ef7742017-08-08 17:44:58 +03001648{
Mingyu Hu7d64c482019-03-12 14:27:40 -07001649 setAttachment(context, type, binding, textureIndex, resource, numViews, baseViewIndex, true);
Martin Radev5dae57b2017-07-14 16:15:55 +03001650}
1651
1652void Framebuffer::commitWebGL1DepthStencilIfConsistent(const Context *context,
1653 GLsizei numViews,
1654 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001655 bool isMultiview)
Jamie Madilla02315b2017-02-23 14:14:47 -05001656{
1657 int count = 0;
1658
1659 std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment,
1660 &mState.mWebGLDepthAttachment,
1661 &mState.mWebGLStencilAttachment}};
1662 for (FramebufferAttachment *attachment : attachments)
1663 {
1664 if (attachment->isAttached())
1665 {
1666 count++;
1667 }
1668 }
1669
1670 mState.mWebGLDepthStencilConsistent = (count <= 1);
1671 if (!mState.mWebGLDepthStencilConsistent)
1672 {
1673 // Inconsistent.
1674 return;
1675 }
1676
Geoff Lange466c552017-03-17 15:24:12 -04001677 auto getImageIndexIfTextureAttachment = [](const FramebufferAttachment &attachment) {
1678 if (attachment.type() == GL_TEXTURE)
1679 {
1680 return attachment.getTextureImageIndex();
1681 }
1682 else
1683 {
Jamie Madillcc129372018-04-12 09:13:18 -04001684 return ImageIndex();
Geoff Lange466c552017-03-17 15:24:12 -04001685 }
1686 };
1687
Jamie Madilla02315b2017-02-23 14:14:47 -05001688 if (mState.mWebGLDepthAttachment.isAttached())
1689 {
1690 const auto &depth = mState.mWebGLDepthAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001691 setAttachmentImpl(context, depth.type(), GL_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001692 getImageIndexIfTextureAttachment(depth), depth.getResource(), numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001693 baseViewIndex, isMultiview);
Jamie Madillcc129372018-04-12 09:13:18 -04001694 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001695 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001696 }
1697 else if (mState.mWebGLStencilAttachment.isAttached())
1698 {
1699 const auto &stencil = mState.mWebGLStencilAttachment;
Jamie Madillcc129372018-04-12 09:13:18 -04001700 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001701 baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001702 setAttachmentImpl(context, stencil.type(), GL_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001703 getImageIndexIfTextureAttachment(stencil), stencil.getResource(),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001704 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001705 }
1706 else if (mState.mWebGLDepthStencilAttachment.isAttached())
1707 {
1708 const auto &depthStencil = mState.mWebGLDepthStencilAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001709 setAttachmentImpl(context, depthStencil.type(), GL_DEPTH_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001710 getImageIndexIfTextureAttachment(depthStencil),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001711 depthStencil.getResource(), numViews, baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001712 setAttachmentImpl(context, depthStencil.type(), GL_STENCIL_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001713 getImageIndexIfTextureAttachment(depthStencil),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001714 depthStencil.getResource(), numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001715 }
1716 else
1717 {
Jamie Madillcc129372018-04-12 09:13:18 -04001718 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001719 baseViewIndex, isMultiview);
Jamie Madillcc129372018-04-12 09:13:18 -04001720 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001721 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001722 }
1723}
1724
Jamie Madill4928b7c2017-06-20 12:57:39 -04001725void Framebuffer::setAttachmentImpl(const Context *context,
1726 GLenum type,
Jamie Madilla02315b2017-02-23 14:14:47 -05001727 GLenum binding,
1728 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001729 FramebufferAttachmentObject *resource,
1730 GLsizei numViews,
1731 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001732 bool isMultiview)
Jamie Madilla02315b2017-02-23 14:14:47 -05001733{
Jamie Madilla02315b2017-02-23 14:14:47 -05001734 switch (binding)
1735 {
Jamie Madillb8126692017-04-05 11:22:17 -04001736 case GL_DEPTH_STENCIL:
1737 case GL_DEPTH_STENCIL_ATTACHMENT:
1738 {
1739 // ensure this is a legitimate depth+stencil format
1740 FramebufferAttachmentObject *attachmentObj = resource;
1741 if (resource)
1742 {
Jamie Madill4fd95d52017-04-05 11:22:18 -04001743 const Format &format = resource->getAttachmentFormat(binding, textureIndex);
Jamie Madillb8126692017-04-05 11:22:17 -04001744 if (format.info->depthBits == 0 || format.info->stencilBits == 0)
1745 {
1746 // Attaching nullptr detaches the current attachment.
1747 attachmentObj = nullptr;
1748 }
1749 }
1750
Jamie Madill4928b7c2017-06-20 12:57:39 -04001751 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001752 &mDirtyDepthAttachmentBinding, type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001753 attachmentObj, numViews, baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001754 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001755 &mDirtyStencilAttachmentBinding, type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001756 attachmentObj, numViews, baseViewIndex, isMultiview);
Jamie Madill42975642017-10-12 12:31:51 -04001757 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001758 }
1759
Jamie Madilla02315b2017-02-23 14:14:47 -05001760 case GL_DEPTH:
1761 case GL_DEPTH_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001762 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001763 &mDirtyDepthAttachmentBinding, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001764 numViews, baseViewIndex, isMultiview);
Jamie Madill2d06b732015-04-20 12:53:28 -04001765 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001766
Jamie Madilla02315b2017-02-23 14:14:47 -05001767 case GL_STENCIL:
1768 case GL_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001769 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001770 &mDirtyStencilAttachmentBinding, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001771 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001772 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001773
Jamie Madilla02315b2017-02-23 14:14:47 -05001774 case GL_BACK:
Geoff Lang8170eab2017-09-21 13:59:04 -04001775 updateAttachment(context, &mState.mColorAttachments[0], DIRTY_BIT_COLOR_ATTACHMENT_0,
1776 &mDirtyColorAttachmentBindings[0], type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001777 resource, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001778 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001779
Jamie Madilla02315b2017-02-23 14:14:47 -05001780 default:
1781 {
1782 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
1783 ASSERT(colorIndex < mState.mColorAttachments.size());
Jamie Madillb8126692017-04-05 11:22:17 -04001784 size_t dirtyBit = DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001785 updateAttachment(context, &mState.mColorAttachments[colorIndex], dirtyBit,
Jamie Madillb8126692017-04-05 11:22:17 -04001786 &mDirtyColorAttachmentBindings[colorIndex], type, binding,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001787 textureIndex, resource, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001788
shrekshaoc87e0052019-02-21 11:40:28 -08001789 if (!resource)
1790 {
shrekshao8413fab2019-04-04 17:13:18 -07001791 mColorAttachmentBits.reset(colorIndex);
shrekshaoc87e0052019-02-21 11:40:28 -08001792 mFloat32ColorAttachmentBits.reset(colorIndex);
1793 }
1794 else
1795 {
shrekshao8413fab2019-04-04 17:13:18 -07001796 mColorAttachmentBits.set(colorIndex);
shrekshaoc87e0052019-02-21 11:40:28 -08001797 updateFloat32ColorAttachmentBits(
1798 colorIndex, resource->getAttachmentFormat(binding, textureIndex).info);
1799 }
1800
Corentin Walleze7557742017-06-01 13:09:57 -04001801 // TODO(jmadill): ASSERT instead of checking the attachment exists in
1802 // formsRenderingFeedbackLoopWith
Jamie Madilla02315b2017-02-23 14:14:47 -05001803 bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE);
1804 mState.mEnabledDrawBuffers.set(colorIndex, enabled);
Jamie Madill6e18a232019-01-16 13:27:14 -05001805 SetComponentTypeMask(getDrawbufferWriteType(colorIndex), colorIndex,
1806 &mState.mDrawBufferTypeMask);
Jamie Madill2d06b732015-04-20 12:53:28 -04001807 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001808 break;
Geoff Langab75a052014-10-15 12:56:37 -04001809 }
1810}
1811
Jamie Madill4928b7c2017-06-20 12:57:39 -04001812void Framebuffer::updateAttachment(const Context *context,
1813 FramebufferAttachment *attachment,
Jamie Madillb8126692017-04-05 11:22:17 -04001814 size_t dirtyBit,
Jamie Madilld4442552018-02-27 22:03:47 -05001815 angle::ObserverBinding *onDirtyBinding,
Jamie Madillb8126692017-04-05 11:22:17 -04001816 GLenum type,
1817 GLenum binding,
1818 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001819 FramebufferAttachmentObject *resource,
1820 GLsizei numViews,
1821 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001822 bool isMultiview)
Jamie Madillb8126692017-04-05 11:22:17 -04001823{
Martin Radev5dae57b2017-07-14 16:15:55 +03001824 attachment->attach(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001825 isMultiview);
Jamie Madillb8126692017-04-05 11:22:17 -04001826 mDirtyBits.set(dirtyBit);
Jamie Madill05b35b22017-10-03 09:01:44 -04001827 mState.mResourceNeedsInit.set(dirtyBit, attachment->initState() == InitState::MayNeedInit);
Jamie Madill66f0d2c2018-11-30 15:25:36 -05001828 onDirtyBinding->bind(resource);
Jamie Madille98b1b52018-03-08 09:47:23 -05001829
Jamie Madillb983a4b2018-08-01 11:34:51 -04001830 invalidateCompletenessCache(context);
Jamie Madillb8126692017-04-05 11:22:17 -04001831}
1832
Jamie Madilla02315b2017-02-23 14:14:47 -05001833void Framebuffer::resetAttachment(const Context *context, GLenum binding)
Jamie Madill2d06b732015-04-20 12:53:28 -04001834{
Jamie Madillcc129372018-04-12 09:13:18 -04001835 setAttachment(context, GL_NONE, binding, ImageIndex(), nullptr);
Jamie Madill2d06b732015-04-20 12:53:28 -04001836}
1837
Jamie Madill6f755b22018-10-09 12:48:54 -04001838angle::Result Framebuffer::syncState(const Context *context)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001839{
1840 if (mDirtyBits.any())
1841 {
Jamie Madill888081d2018-02-27 00:24:46 -05001842 mDirtyBitsGuard = mDirtyBits;
Jamie Madill19fa1c62018-03-08 09:47:21 -05001843 ANGLE_TRY(mImpl->syncState(context, mDirtyBits));
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001844 mDirtyBits.reset();
Jamie Madill888081d2018-02-27 00:24:46 -05001845 mDirtyBitsGuard.reset();
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001846 }
Jamie Madill7c985f52018-11-29 18:16:17 -05001847 return angle::Result::Continue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001848}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001849
Jamie Madilld4442552018-02-27 22:03:47 -05001850void Framebuffer::onSubjectStateChange(const Context *context,
1851 angle::SubjectIndex index,
1852 angle::SubjectMessage message)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001853{
Jamie Madille4faae22019-05-10 08:27:00 -04001854 if (message != angle::SubjectMessage::SubjectChanged)
Jamie Madillf668a4b2018-09-23 17:01:20 -04001855 {
Jamie Madill67220092019-05-20 11:12:53 -04001856 // This can be triggered by SubImage calls for Textures.
1857 if (message == angle::SubjectMessage::ContentsChanged)
1858 {
1859 mDirtyBits.set(DIRTY_BIT_COLOR_BUFFER_CONTENTS_0 + index);
1860 onStateChange(context, angle::SubjectMessage::DirtyBitsFlagged);
1861 return;
1862 }
1863
Jamie Madillf668a4b2018-09-23 17:01:20 -04001864 // This can be triggered by the GL back-end TextureGL class.
Jamie Madille4faae22019-05-10 08:27:00 -04001865 ASSERT(message == angle::SubjectMessage::DirtyBitsFlagged);
Jamie Madillf668a4b2018-09-23 17:01:20 -04001866 return;
1867 }
Jamie Madill55e57f92018-09-18 11:32:43 -04001868
1869 ASSERT(!mDirtyBitsGuard.valid() || mDirtyBitsGuard.value().test(index));
1870 mDirtyBits.set(index);
Jamie Madill888081d2018-02-27 00:24:46 -05001871
Jamie Madillb983a4b2018-08-01 11:34:51 -04001872 invalidateCompletenessCache(context);
Jamie Madill05b35b22017-10-03 09:01:44 -04001873
Jamie Madilld4442552018-02-27 22:03:47 -05001874 FramebufferAttachment *attachment = getAttachmentFromSubjectIndex(index);
1875
Jamie Madill05b35b22017-10-03 09:01:44 -04001876 // Mark the appropriate init flag.
Jamie Madilld4442552018-02-27 22:03:47 -05001877 mState.mResourceNeedsInit.set(index, attachment->initState() == InitState::MayNeedInit);
shrekshaoc87e0052019-02-21 11:40:28 -08001878
1879 // Update mFloat32ColorAttachmentBits Cache
1880 if (index < DIRTY_BIT_COLOR_ATTACHMENT_MAX)
1881 {
1882 ASSERT(index != DIRTY_BIT_DEPTH_ATTACHMENT);
1883 ASSERT(index != DIRTY_BIT_STENCIL_ATTACHMENT);
1884 updateFloat32ColorAttachmentBits(index - DIRTY_BIT_COLOR_ATTACHMENT_0,
1885 attachment->getFormat().info);
1886 }
Jamie Madilld4442552018-02-27 22:03:47 -05001887}
1888
1889FramebufferAttachment *Framebuffer::getAttachmentFromSubjectIndex(angle::SubjectIndex index)
1890{
1891 switch (index)
1892 {
1893 case DIRTY_BIT_DEPTH_ATTACHMENT:
1894 return &mState.mDepthAttachment;
1895 case DIRTY_BIT_STENCIL_ATTACHMENT:
1896 return &mState.mStencilAttachment;
1897 default:
1898 size_t colorIndex = (index - DIRTY_BIT_COLOR_ATTACHMENT_0);
1899 ASSERT(colorIndex < mState.mColorAttachments.size());
1900 return &mState.mColorAttachments[colorIndex];
1901 }
Jamie Madill51f40ec2016-06-15 14:06:00 -04001902}
1903
shrekshao8413fab2019-04-04 17:13:18 -07001904bool Framebuffer::formsRenderingFeedbackLoopWith(const Context *context) const
Jamie Madilla4595b82017-01-11 17:36:34 -05001905{
shrekshao8413fab2019-04-04 17:13:18 -07001906 const State &state = context->getState();
Jamie Madill785e8a02018-10-04 17:42:00 -04001907 const Program *program = state.getProgram();
Jamie Madilla4595b82017-01-11 17:36:34 -05001908
1909 // TODO(jmadill): Default framebuffer feedback loops.
Jamie Madill2274b652018-05-31 10:56:08 -04001910 if (mState.mId == 0)
Jamie Madilla4595b82017-01-11 17:36:34 -05001911 {
1912 return false;
1913 }
1914
shrekshao8413fab2019-04-04 17:13:18 -07001915 const FramebufferAttachment *depth = getDepthbuffer();
1916 const FramebufferAttachment *stencil = getStencilbuffer();
1917
1918 const bool checkDepth = depth && depth->type() == GL_TEXTURE;
1919 // Skip the feedback loop check for stencil if depth/stencil point to the same resource.
1920 const bool checkStencil =
1921 (stencil && stencil->type() == GL_TEXTURE) && (!depth || *stencil != *depth);
1922
1923 const gl::ActiveTextureMask &activeTextures = program->getActiveSamplersMask();
1924 const gl::ActiveTexturePointerArray &textures = state.getActiveTexturesCache();
1925
1926 for (size_t textureUnit : activeTextures)
Jamie Madilla4595b82017-01-11 17:36:34 -05001927 {
shrekshao8413fab2019-04-04 17:13:18 -07001928 Texture *texture = textures[textureUnit];
1929
1930 if (texture == nullptr)
Jamie Madilla4595b82017-01-11 17:36:34 -05001931 {
shrekshao8413fab2019-04-04 17:13:18 -07001932 continue;
1933 }
1934
1935 // Depth and stencil attachment form feedback loops
1936 // Regardless of if enabled or masked.
1937 if (checkDepth)
1938 {
1939 if (texture->id() == depth->id())
Jamie Madilla4595b82017-01-11 17:36:34 -05001940 {
1941 return true;
1942 }
1943 }
Jamie Madilla4595b82017-01-11 17:36:34 -05001944
shrekshao8413fab2019-04-04 17:13:18 -07001945 if (checkStencil)
Jamie Madill1d37bc52017-02-02 19:59:58 -05001946 {
shrekshao8413fab2019-04-04 17:13:18 -07001947 if (texture->id() == stencil->id())
Jamie Madill1d37bc52017-02-02 19:59:58 -05001948 {
Jamie Madill38fe6842018-09-19 07:20:00 -04001949 return true;
Jamie Madill1d37bc52017-02-02 19:59:58 -05001950 }
1951 }
shrekshao8413fab2019-04-04 17:13:18 -07001952
1953 // Check if any color attachment forms a feedback loop.
1954 for (size_t drawIndex : mColorAttachmentBits)
1955 {
1956 const FramebufferAttachment &attachment = mState.mColorAttachments[drawIndex];
1957 ASSERT(attachment.isAttached());
1958
1959 if (attachment.isTextureWithId(texture->id()))
1960 {
1961 // TODO(jmadill): Check for appropriate overlap.
1962 return true;
1963 }
1964 }
Jamie Madill1d37bc52017-02-02 19:59:58 -05001965 }
1966
Jamie Madilla4595b82017-01-11 17:36:34 -05001967 return false;
1968}
1969
Jamie Madillfd3dd432017-02-02 19:59:59 -05001970bool Framebuffer::formsCopyingFeedbackLoopWith(GLuint copyTextureID,
1971 GLint copyTextureLevel,
1972 GLint copyTextureLayer) const
Jamie Madillf695a3a2017-01-11 17:36:35 -05001973{
Jamie Madill2274b652018-05-31 10:56:08 -04001974 if (mState.mId == 0)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001975 {
1976 // It seems impossible to form a texture copying feedback loop with the default FBO.
1977 return false;
1978 }
1979
1980 const FramebufferAttachment *readAttachment = getReadColorbuffer();
1981 ASSERT(readAttachment);
1982
1983 if (readAttachment->isTextureWithId(copyTextureID))
1984 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001985 const auto &imageIndex = readAttachment->getTextureImageIndex();
Jamie Madillcc129372018-04-12 09:13:18 -04001986 if (imageIndex.getLevelIndex() == copyTextureLevel)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001987 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001988 // Check 3D/Array texture layers.
Jamie Madillcc129372018-04-12 09:13:18 -04001989 return !imageIndex.hasLayer() || copyTextureLayer == ImageIndex::kEntireLevel ||
1990 imageIndex.getLayerIndex() == copyTextureLayer;
Jamie Madillf695a3a2017-01-11 17:36:35 -05001991 }
1992 }
1993 return false;
1994}
1995
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001996GLint Framebuffer::getDefaultWidth() const
1997{
1998 return mState.getDefaultWidth();
1999}
2000
2001GLint Framebuffer::getDefaultHeight() const
2002{
2003 return mState.getDefaultHeight();
2004}
2005
2006GLint Framebuffer::getDefaultSamples() const
2007{
2008 return mState.getDefaultSamples();
2009}
2010
Geoff Lang92019432017-11-20 13:09:34 -05002011bool Framebuffer::getDefaultFixedSampleLocations() const
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002012{
2013 return mState.getDefaultFixedSampleLocations();
2014}
2015
Jiawei Shaob1e91382018-05-17 14:33:55 +08002016GLint Framebuffer::getDefaultLayers() const
2017{
2018 return mState.getDefaultLayers();
2019}
2020
Jamie Madillb983a4b2018-08-01 11:34:51 -04002021void Framebuffer::setDefaultWidth(const Context *context, GLint defaultWidth)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002022{
2023 mState.mDefaultWidth = defaultWidth;
2024 mDirtyBits.set(DIRTY_BIT_DEFAULT_WIDTH);
Jamie Madillb983a4b2018-08-01 11:34:51 -04002025 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002026}
2027
Jamie Madillb983a4b2018-08-01 11:34:51 -04002028void Framebuffer::setDefaultHeight(const Context *context, GLint defaultHeight)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002029{
2030 mState.mDefaultHeight = defaultHeight;
2031 mDirtyBits.set(DIRTY_BIT_DEFAULT_HEIGHT);
Jamie Madillb983a4b2018-08-01 11:34:51 -04002032 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002033}
2034
Jamie Madillb983a4b2018-08-01 11:34:51 -04002035void Framebuffer::setDefaultSamples(const Context *context, GLint defaultSamples)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002036{
2037 mState.mDefaultSamples = defaultSamples;
2038 mDirtyBits.set(DIRTY_BIT_DEFAULT_SAMPLES);
Jamie Madillb983a4b2018-08-01 11:34:51 -04002039 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002040}
2041
Jamie Madillb983a4b2018-08-01 11:34:51 -04002042void Framebuffer::setDefaultFixedSampleLocations(const Context *context,
2043 bool defaultFixedSampleLocations)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002044{
2045 mState.mDefaultFixedSampleLocations = defaultFixedSampleLocations;
2046 mDirtyBits.set(DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS);
Jamie Madillb983a4b2018-08-01 11:34:51 -04002047 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002048}
2049
Jiawei Shaob1e91382018-05-17 14:33:55 +08002050void Framebuffer::setDefaultLayers(GLint defaultLayers)
2051{
2052 mState.mDefaultLayers = defaultLayers;
2053 mDirtyBits.set(DIRTY_BIT_DEFAULT_LAYERS);
2054}
2055
Martin Radev14a26ae2017-07-24 15:56:29 +03002056GLsizei Framebuffer::getNumViews() const
2057{
Martin Radev5c00d0d2017-08-07 10:06:59 +03002058 return mState.getNumViews();
Martin Radev14a26ae2017-07-24 15:56:29 +03002059}
2060
Martin Radev4e619f52017-08-09 11:50:06 +03002061GLint Framebuffer::getBaseViewIndex() const
2062{
2063 return mState.getBaseViewIndex();
2064}
2065
Mingyu Hu7d64c482019-03-12 14:27:40 -07002066bool Framebuffer::isMultiview() const
Martin Radev878c8b12017-07-28 09:51:04 +03002067{
Mingyu Hu7d64c482019-03-12 14:27:40 -07002068 return mState.isMultiview();
Martin Radev878c8b12017-07-28 09:51:04 +03002069}
2070
Olli Etuaho8acb1b62018-07-30 16:20:54 +03002071bool Framebuffer::readDisallowedByMultiview() const
2072{
Mingyu Hu7d64c482019-03-12 14:27:40 -07002073 return (mState.isMultiview() && mState.getNumViews() > 1);
Olli Etuaho8acb1b62018-07-30 16:20:54 +03002074}
2075
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002076angle::Result Framebuffer::ensureClearAttachmentsInitialized(const Context *context,
2077 GLbitfield mask)
Geoff Langd4fff502017-09-22 11:28:28 -04002078{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05002079 const auto &glState = context->getState();
Geoff Langd4fff502017-09-22 11:28:28 -04002080 if (!context->isRobustResourceInitEnabled() || glState.isRasterizerDiscardEnabled())
2081 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002082 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002083 }
2084
Geoff Langa36483f2018-03-09 16:11:21 -05002085 const BlendState &blend = glState.getBlendState();
2086 const DepthStencilState &depthStencil = glState.getDepthStencilState();
Geoff Langd4fff502017-09-22 11:28:28 -04002087
2088 bool color = (mask & GL_COLOR_BUFFER_BIT) != 0 && !IsColorMaskedOut(blend);
2089 bool depth = (mask & GL_DEPTH_BUFFER_BIT) != 0 && !IsDepthMaskedOut(depthStencil);
2090 bool stencil = (mask & GL_STENCIL_BUFFER_BIT) != 0 && !IsStencilMaskedOut(depthStencil);
2091
2092 if (!color && !depth && !stencil)
2093 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002094 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002095 }
2096
2097 if (partialClearNeedsInit(context, color, depth, stencil))
2098 {
2099 ANGLE_TRY(ensureDrawAttachmentsInitialized(context));
2100 }
2101
2102 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2103 // still be marked initialized. This simplifies design, allowing this method to be called before
2104 // the clear.
2105 markDrawAttachmentsInitialized(color, depth, stencil);
2106
Jamie Madill7c985f52018-11-29 18:16:17 -05002107 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002108}
2109
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002110angle::Result Framebuffer::ensureClearBufferAttachmentsInitialized(const Context *context,
2111 GLenum buffer,
2112 GLint drawbuffer)
Geoff Langd4fff502017-09-22 11:28:28 -04002113{
2114 if (!context->isRobustResourceInitEnabled() ||
Jamie Madillc3dc5d42018-12-30 12:12:04 -05002115 context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Geoff Langd4fff502017-09-22 11:28:28 -04002116 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002117 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002118 }
2119
2120 if (partialBufferClearNeedsInit(context, buffer))
2121 {
2122 ANGLE_TRY(ensureBufferInitialized(context, buffer, drawbuffer));
2123 }
2124
2125 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2126 // still be marked initialized. This simplifies design, allowing this method to be called before
2127 // the clear.
2128 markBufferInitialized(buffer, drawbuffer);
2129
Jamie Madill7c985f52018-11-29 18:16:17 -05002130 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002131}
2132
Jamie Madill6f755b22018-10-09 12:48:54 -04002133angle::Result Framebuffer::ensureDrawAttachmentsInitialized(const Context *context)
Jamie Madill05b35b22017-10-03 09:01:44 -04002134{
2135 if (!context->isRobustResourceInitEnabled())
2136 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002137 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002138 }
2139
2140 // Note: we don't actually filter by the draw attachment enum. Just init everything.
2141 for (size_t bit : mState.mResourceNeedsInit)
2142 {
2143 switch (bit)
2144 {
2145 case DIRTY_BIT_DEPTH_ATTACHMENT:
2146 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2147 break;
2148 case DIRTY_BIT_STENCIL_ATTACHMENT:
2149 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2150 break;
2151 default:
2152 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bit]));
2153 break;
2154 }
2155 }
2156
2157 mState.mResourceNeedsInit.reset();
Jamie Madill7c985f52018-11-29 18:16:17 -05002158 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002159}
2160
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002161angle::Result Framebuffer::ensureReadAttachmentsInitialized(const Context *context)
Jamie Madill05b35b22017-10-03 09:01:44 -04002162{
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002163 ASSERT(context->isRobustResourceInitEnabled());
2164
2165 if (mState.mResourceNeedsInit.none())
Jamie Madill05b35b22017-10-03 09:01:44 -04002166 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002167 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002168 }
2169
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002170 if (mState.mReadBufferState != GL_NONE)
Jamie Madill05b35b22017-10-03 09:01:44 -04002171 {
2172 size_t readIndex = mState.getReadIndex();
2173 if (mState.mResourceNeedsInit[readIndex])
2174 {
2175 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[readIndex]));
2176 mState.mResourceNeedsInit.reset(readIndex);
2177 }
2178 }
2179
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002180 // Conservatively init depth since it can be read by BlitFramebuffer.
2181 if (hasDepth())
Jamie Madill05b35b22017-10-03 09:01:44 -04002182 {
2183 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2184 {
2185 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2186 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2187 }
2188 }
2189
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002190 // Conservatively init stencil since it can be read by BlitFramebuffer.
2191 if (hasStencil())
Jamie Madill05b35b22017-10-03 09:01:44 -04002192 {
2193 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2194 {
2195 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2196 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2197 }
2198 }
2199
Jamie Madill7c985f52018-11-29 18:16:17 -05002200 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002201}
2202
2203void Framebuffer::markDrawAttachmentsInitialized(bool color, bool depth, bool stencil)
2204{
2205 // Mark attachments as initialized.
2206 if (color)
2207 {
2208 for (auto colorIndex : mState.mEnabledDrawBuffers)
2209 {
2210 auto &colorAttachment = mState.mColorAttachments[colorIndex];
2211 ASSERT(colorAttachment.isAttached());
2212 colorAttachment.setInitState(InitState::Initialized);
2213 mState.mResourceNeedsInit.reset(colorIndex);
2214 }
2215 }
2216
2217 if (depth && mState.mDepthAttachment.isAttached())
2218 {
2219 mState.mDepthAttachment.setInitState(InitState::Initialized);
2220 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2221 }
2222
2223 if (stencil && mState.mStencilAttachment.isAttached())
2224 {
2225 mState.mStencilAttachment.setInitState(InitState::Initialized);
2226 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2227 }
2228}
2229
2230void Framebuffer::markBufferInitialized(GLenum bufferType, GLint bufferIndex)
2231{
2232 switch (bufferType)
2233 {
2234 case GL_COLOR:
2235 {
2236 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2237 if (mState.mColorAttachments[bufferIndex].isAttached())
2238 {
2239 mState.mColorAttachments[bufferIndex].setInitState(InitState::Initialized);
2240 mState.mResourceNeedsInit.reset(bufferIndex);
2241 }
2242 break;
2243 }
2244 case GL_DEPTH:
2245 {
2246 if (mState.mDepthAttachment.isAttached())
2247 {
2248 mState.mDepthAttachment.setInitState(InitState::Initialized);
2249 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2250 }
2251 break;
2252 }
2253 case GL_STENCIL:
2254 {
2255 if (mState.mStencilAttachment.isAttached())
2256 {
2257 mState.mStencilAttachment.setInitState(InitState::Initialized);
2258 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2259 }
2260 break;
2261 }
2262 case GL_DEPTH_STENCIL:
2263 {
2264 if (mState.mDepthAttachment.isAttached())
2265 {
2266 mState.mDepthAttachment.setInitState(InitState::Initialized);
2267 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2268 }
2269 if (mState.mStencilAttachment.isAttached())
2270 {
2271 mState.mStencilAttachment.setInitState(InitState::Initialized);
2272 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2273 }
2274 break;
2275 }
2276 default:
2277 UNREACHABLE();
2278 break;
2279 }
2280}
2281
2282Box Framebuffer::getDimensions() const
2283{
2284 return mState.getDimensions();
2285}
2286
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002287angle::Result Framebuffer::ensureBufferInitialized(const Context *context,
2288 GLenum bufferType,
2289 GLint bufferIndex)
Jamie Madill05b35b22017-10-03 09:01:44 -04002290{
2291 ASSERT(context->isRobustResourceInitEnabled());
2292
2293 if (mState.mResourceNeedsInit.none())
2294 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002295 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002296 }
2297
2298 switch (bufferType)
2299 {
2300 case GL_COLOR:
2301 {
2302 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2303 if (mState.mResourceNeedsInit[bufferIndex])
2304 {
2305 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bufferIndex]));
2306 mState.mResourceNeedsInit.reset(bufferIndex);
2307 }
2308 break;
2309 }
2310 case GL_DEPTH:
2311 {
2312 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2313 {
2314 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2315 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2316 }
2317 break;
2318 }
2319 case GL_STENCIL:
2320 {
2321 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2322 {
2323 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2324 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2325 }
2326 break;
2327 }
2328 case GL_DEPTH_STENCIL:
2329 {
2330 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2331 {
2332 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2333 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2334 }
2335 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2336 {
2337 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2338 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2339 }
2340 break;
2341 }
2342 default:
2343 UNREACHABLE();
2344 break;
2345 }
2346
Jamie Madill7c985f52018-11-29 18:16:17 -05002347 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002348}
2349
2350bool Framebuffer::partialBufferClearNeedsInit(const Context *context, GLenum bufferType)
2351{
2352 if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none())
2353 {
2354 return false;
2355 }
2356
2357 switch (bufferType)
2358 {
2359 case GL_COLOR:
2360 return partialClearNeedsInit(context, true, false, false);
2361 case GL_DEPTH:
2362 return partialClearNeedsInit(context, false, true, false);
2363 case GL_STENCIL:
2364 return partialClearNeedsInit(context, false, false, true);
2365 case GL_DEPTH_STENCIL:
2366 return partialClearNeedsInit(context, false, true, true);
2367 default:
2368 UNREACHABLE();
2369 return false;
2370 }
2371}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002372} // namespace gl