blob: 652bcfd919e6b8ae7caf62002db0b3ce08dfbf04 [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 Madill77abad82018-10-25 17:03:48 -0400971 angle::Result err = syncState(context);
Jamie Madill7c985f52018-11-29 18:16:17 -0500972 if (err != angle::Result::Continue)
Jamie Madille98b1b52018-03-08 09:47:23 -0500973 {
Jamie Madill77abad82018-10-25 17:03:48 -0400974 return 0;
Jamie Madillcc73f242018-08-01 11:34:48 -0400975 }
976 if (!mImpl->checkStatus(context))
977 {
978 mCachedStatus = GL_FRAMEBUFFER_UNSUPPORTED;
Jamie Madille98b1b52018-03-08 09:47:23 -0500979 }
Jamie Madill362876b2016-06-16 14:46:59 -0400980 }
981
Jamie Madill427064d2018-04-13 16:20:34 -0400982 return mCachedStatus.value();
Jamie Madill362876b2016-06-16 14:46:59 -0400983}
984
Jamie Madille98b1b52018-03-08 09:47:23 -0500985GLenum Framebuffer::checkStatusWithGLFrontEnd(const Context *context)
Jamie Madill362876b2016-06-16 14:46:59 -0400986{
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500987 const State &state = context->getState();
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400988
Jamie Madill2274b652018-05-31 10:56:08 -0400989 ASSERT(mState.mId != 0);
Jamie Madill362876b2016-06-16 14:46:59 -0400990
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400991 bool hasAttachments = false;
992 Optional<unsigned int> colorbufferSize;
993 Optional<int> samples;
Geoff Lang92019432017-11-20 13:09:34 -0500994 Optional<bool> fixedSampleLocations;
JiangYizhou461d9a32017-01-04 16:37:26 +0800995 bool hasRenderbuffer = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996
Martin Radev9bc9a322017-07-21 14:28:17 +0300997 const FramebufferAttachment *firstAttachment = getFirstNonNullAttachment();
998
Jiawei Shaoa8802472018-05-28 11:17:47 +0800999 Optional<bool> isLayered;
1000 Optional<TextureType> colorAttachmentsTextureType;
1001
Jamie Madill48ef11b2016-04-27 15:21:52 -04001002 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003 {
Jamie Madill2d06b732015-04-20 12:53:28 -04001004 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001006 if (!CheckAttachmentCompleteness(context, colorAttachment))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001007 {
1008 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1009 }
daniel@transgaming.com01868132010-08-24 19:21:17 +00001010
Geoff Lang677bb6f2017-04-05 12:40:40 -04001011 const InternalFormat &format = *colorAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001012 if (format.depthBits > 0 || format.stencilBits > 0)
1013 {
1014 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1015 }
1016
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001017 if (!CheckAttachmentSampleCompleteness(context, colorAttachment, true, &samples,
1018 &fixedSampleLocations))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001019 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001020 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001021 }
1022
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001023 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
1024 // in GLES 3.0, there is no such restriction
1025 if (state.getClientMajorVersion() < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001026 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001027 if (colorbufferSize.valid())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001028 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001029 if (format.pixelBytes != colorbufferSize.value())
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +00001030 {
1031 return GL_FRAMEBUFFER_UNSUPPORTED;
1032 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001033 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001034 else
1035 {
1036 colorbufferSize = format.pixelBytes;
1037 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001038 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001039
Martin Radev9bc9a322017-07-21 14:28:17 +03001040 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &colorAttachment))
1041 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001042 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001043 }
1044
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001045 hasRenderbuffer = hasRenderbuffer || (colorAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001046
1047 if (!hasAttachments)
1048 {
1049 isLayered = colorAttachment.isLayered();
1050 if (isLayered.value())
1051 {
1052 colorAttachmentsTextureType = colorAttachment.getTextureImageIndex().getType();
1053 }
1054 hasAttachments = true;
1055 }
1056 else
1057 {
1058 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1059 // If any framebuffer attachment is layered, all populated attachments
1060 // must be layered. Additionally, all populated color attachments must
1061 // be from textures of the same target. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1062 ASSERT(isLayered.valid());
1063 if (isLayered.value() != colorAttachment.isLayered())
1064 {
1065 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1066 }
1067 else if (isLayered.value())
1068 {
1069 ASSERT(colorAttachmentsTextureType.valid());
1070 if (colorAttachmentsTextureType.value() !=
1071 colorAttachment.getTextureImageIndex().getType())
1072 {
1073 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1074 }
1075 }
1076 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001077 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001078 }
1079
Jamie Madill48ef11b2016-04-27 15:21:52 -04001080 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001081 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001082 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001083 if (!CheckAttachmentCompleteness(context, depthAttachment))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001085 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086 }
1087
Geoff Lang677bb6f2017-04-05 12:40:40 -04001088 const InternalFormat &format = *depthAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001089 if (format.depthBits == 0)
1090 {
1091 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001092 }
1093
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001094 if (!CheckAttachmentSampleCompleteness(context, depthAttachment, false, &samples,
1095 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001096 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001097 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001098 }
Sami Väisänena797e062016-05-12 15:23:40 +03001099
Martin Radev9bc9a322017-07-21 14:28:17 +03001100 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &depthAttachment))
1101 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001102 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001103 }
1104
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001105 hasRenderbuffer = hasRenderbuffer || (depthAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001106
1107 if (!hasAttachments)
1108 {
1109 isLayered = depthAttachment.isLayered();
1110 hasAttachments = true;
1111 }
1112 else
1113 {
1114 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1115 // If any framebuffer attachment is layered, all populated attachments
1116 // must be layered. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1117 ASSERT(isLayered.valid());
1118 if (isLayered.value() != depthAttachment.isLayered())
1119 {
1120 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1121 }
1122 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001123 }
1124
Jamie Madill48ef11b2016-04-27 15:21:52 -04001125 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001126 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001127 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001128 if (!CheckAttachmentCompleteness(context, stencilAttachment))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001129 {
1130 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1131 }
1132
Geoff Lang677bb6f2017-04-05 12:40:40 -04001133 const InternalFormat &format = *stencilAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001134 if (format.stencilBits == 0)
1135 {
1136 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001137 }
1138
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001139 if (!CheckAttachmentSampleCompleteness(context, stencilAttachment, false, &samples,
1140 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001141 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001142 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001143 }
Corentin Wallez086d59a2016-04-29 09:06:49 -04001144
Martin Radev9bc9a322017-07-21 14:28:17 +03001145 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &stencilAttachment))
1146 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001147 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001148 }
1149
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001150 hasRenderbuffer = hasRenderbuffer || (stencilAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001151
1152 if (!hasAttachments)
1153 {
1154 hasAttachments = true;
1155 }
1156 else
1157 {
1158 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1159 // If any framebuffer attachment is layered, all populated attachments
1160 // must be layered.
1161 // {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1162 ASSERT(isLayered.valid());
1163 if (isLayered.value() != stencilAttachment.isLayered())
1164 {
1165 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1166 }
1167 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001168 }
1169
1170 // Starting from ES 3.0 stencil and depth, if present, should be the same image
1171 if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() &&
1172 stencilAttachment.isAttached() && stencilAttachment != depthAttachment)
1173 {
1174 return GL_FRAMEBUFFER_UNSUPPORTED;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001175 }
1176
Jamie Madilla02315b2017-02-23 14:14:47 -05001177 // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL.
1178 if (state.isWebGL1())
1179 {
1180 if (!mState.mWebGLDepthStencilConsistent)
1181 {
1182 return GL_FRAMEBUFFER_UNSUPPORTED;
1183 }
1184
1185 if (mState.mWebGLDepthStencilAttachment.isAttached())
1186 {
1187 if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 ||
1188 mState.mWebGLDepthStencilAttachment.getStencilSize() == 0)
1189 {
1190 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1191 }
Martin Radev9bc9a322017-07-21 14:28:17 +03001192
1193 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment,
1194 &mState.mWebGLDepthStencilAttachment))
1195 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001196 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001197 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001198 }
1199 else if (mState.mStencilAttachment.isAttached() &&
1200 mState.mStencilAttachment.getDepthSize() > 0)
1201 {
1202 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1203 }
1204 else if (mState.mDepthAttachment.isAttached() &&
1205 mState.mDepthAttachment.getStencilSize() > 0)
1206 {
1207 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1208 }
1209 }
1210
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001211 // ES3.1(section 9.4) requires that if no image is attached to the framebuffer, and either the
1212 // value of the framebuffer's FRAMEBUFFER_DEFAULT_WIDTH or FRAMEBUFFER_DEFAULT_HEIGHT parameters
1213 // is zero, the framebuffer is considered incomplete.
JiangYizhou461d9a32017-01-04 16:37:26 +08001214 GLint defaultWidth = mState.getDefaultWidth();
1215 GLint defaultHeight = mState.getDefaultHeight();
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001216 if (!hasAttachments && (defaultWidth == 0 || defaultHeight == 0))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001217 {
1218 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001219 }
1220
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001221 // In ES 2.0 and WebGL, all color attachments must have the same width and height.
Jamie Madillcc86d642015-11-24 13:00:07 -05001222 // In ES 3.0, there is no such restriction.
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001223 if ((state.getClientMajorVersion() < 3 || state.getExtensions().webglCompatibility) &&
1224 !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -05001225 {
1226 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
1227 }
1228
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001229 // ES3.1(section 9.4) requires that if the attached images are a mix of renderbuffers and
1230 // textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all attached textures.
JiangYizhou461d9a32017-01-04 16:37:26 +08001231 if (fixedSampleLocations.valid() && hasRenderbuffer && !fixedSampleLocations.value())
1232 {
1233 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1234 }
1235
Kenneth Russellce8602a2017-10-03 18:23:08 -07001236 // The WebGL conformance tests implicitly define that all framebuffer
1237 // attachments must be unique. For example, the same level of a texture can
1238 // not be attached to two different color attachments.
1239 if (state.getExtensions().webglCompatibility)
1240 {
1241 if (!mState.colorAttachmentsAreUniqueImages())
1242 {
1243 return GL_FRAMEBUFFER_UNSUPPORTED;
1244 }
1245 }
1246
Jamie Madillcc86d642015-11-24 13:00:07 -05001247 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001248}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001249
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001250angle::Result Framebuffer::discard(const Context *context, size_t count, const GLenum *attachments)
Austin Kinross08332632015-05-05 13:35:47 -07001251{
Jamie Madill05b35b22017-10-03 09:01:44 -04001252 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1253 // can be no-ops, so we should probably do that to ensure consistency.
1254 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1255
Jamie Madill4928b7c2017-06-20 12:57:39 -04001256 return mImpl->discard(context, count, attachments);
Austin Kinross08332632015-05-05 13:35:47 -07001257}
1258
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001259angle::Result Framebuffer::invalidate(const Context *context,
1260 size_t count,
1261 const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001262{
Jamie Madill05b35b22017-10-03 09:01:44 -04001263 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1264 // can be no-ops, so we should probably do that to ensure consistency.
1265 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1266
Jamie Madill4928b7c2017-06-20 12:57:39 -04001267 return mImpl->invalidate(context, count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001268}
1269
Jamie Madill05b35b22017-10-03 09:01:44 -04001270bool Framebuffer::partialClearNeedsInit(const Context *context,
1271 bool color,
1272 bool depth,
1273 bool stencil)
1274{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001275 const auto &glState = context->getState();
Jamie Madill05b35b22017-10-03 09:01:44 -04001276
1277 if (!glState.isRobustResourceInitEnabled())
1278 {
1279 return false;
1280 }
1281
1282 // Scissors can affect clearing.
1283 // TODO(jmadill): Check for complete scissor overlap.
1284 if (glState.isScissorTestEnabled())
1285 {
1286 return true;
1287 }
1288
1289 // If colors masked, we must clear before we clear. Do a simple check.
1290 // TODO(jmadill): Filter out unused color channels from the test.
1291 if (color)
1292 {
1293 const auto &blend = glState.getBlendState();
1294 if (!(blend.colorMaskRed && blend.colorMaskGreen && blend.colorMaskBlue &&
1295 blend.colorMaskAlpha))
1296 {
1297 return true;
1298 }
1299 }
1300
1301 const auto &depthStencil = glState.getDepthStencilState();
Yuly Novikov21edf3d2018-07-23 16:44:16 -04001302 if (stencil && (depthStencil.stencilMask != depthStencil.stencilWritemask ||
1303 depthStencil.stencilBackMask != depthStencil.stencilBackWritemask))
Jamie Madill05b35b22017-10-03 09:01:44 -04001304 {
1305 return true;
1306 }
1307
1308 return false;
1309}
1310
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001311angle::Result Framebuffer::invalidateSub(const Context *context,
1312 size_t count,
1313 const GLenum *attachments,
1314 const Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -04001315{
Jamie Madill05b35b22017-10-03 09:01:44 -04001316 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1317 // can be no-ops, so we should probably do that to ensure consistency.
1318 // TODO(jmadill): Make a invalidate no-op in WebGL 2.0.
1319
Jamie Madill4928b7c2017-06-20 12:57:39 -04001320 return mImpl->invalidateSub(context, count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -04001321}
1322
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001323angle::Result Framebuffer::clear(const Context *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -05001324{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001325 const auto &glState = context->getState();
Jamie Madill05b35b22017-10-03 09:01:44 -04001326 if (glState.isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -05001327 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001328 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001329 }
1330
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001331 // Remove clear bits that are ineffective. An effective clear changes at least one fragment. If
1332 // color/depth/stencil masks make the clear ineffective we skip it altogether.
1333
1334 // If all color channels are masked, don't attempt to clear color.
1335 if (context->getState().getBlendState().allChannelsMasked())
1336 {
1337 mask &= ~GL_COLOR_BUFFER_BIT;
1338 }
1339
1340 // If depth write is disabled, don't attempt to clear depth.
1341 if (!context->getState().getDepthStencilState().depthMask)
1342 {
1343 mask &= ~GL_DEPTH_BUFFER_BIT;
1344 }
1345
1346 // If all stencil bits are masked, don't attempt to clear stencil.
1347 if (context->getState().getDepthStencilState().stencilWritemask == 0)
1348 {
1349 mask &= ~GL_STENCIL_BUFFER_BIT;
1350 }
1351
1352 if (mask != 0)
1353 {
1354 ANGLE_TRY(mImpl->clear(context, mask));
1355 }
Jamie Madill05b35b22017-10-03 09:01:44 -04001356
Jamie Madill7c985f52018-11-29 18:16:17 -05001357 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001358}
1359
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001360angle::Result Framebuffer::clearBufferfv(const Context *context,
1361 GLenum buffer,
1362 GLint drawbuffer,
1363 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001364{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001365 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001366 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001367 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001368 }
1369
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001370 if (buffer == GL_DEPTH)
1371 {
1372 // If depth write is disabled, don't attempt to clear depth.
1373 if (!context->getState().getDepthStencilState().depthMask)
1374 {
1375 return angle::Result::Continue;
1376 }
1377 }
1378 else
1379 {
1380 // If all color channels are masked, don't attempt to clear color.
1381 if (context->getState().getBlendState().allChannelsMasked())
1382 {
1383 return angle::Result::Continue;
1384 }
1385 }
1386
Jamie Madill05b35b22017-10-03 09:01:44 -04001387 ANGLE_TRY(mImpl->clearBufferfv(context, buffer, drawbuffer, values));
1388
Jamie Madill7c985f52018-11-29 18:16:17 -05001389 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001390}
1391
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001392angle::Result Framebuffer::clearBufferuiv(const Context *context,
1393 GLenum buffer,
1394 GLint drawbuffer,
1395 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001396{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001397 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001398 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001399 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001400 }
1401
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001402 // If all color channels are masked, don't attempt to clear color.
1403 if (context->getState().getBlendState().allChannelsMasked())
1404 {
1405 return angle::Result::Continue;
1406 }
1407
Jamie Madill05b35b22017-10-03 09:01:44 -04001408 ANGLE_TRY(mImpl->clearBufferuiv(context, buffer, drawbuffer, values));
1409
Jamie Madill7c985f52018-11-29 18:16:17 -05001410 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001411}
1412
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001413angle::Result Framebuffer::clearBufferiv(const Context *context,
1414 GLenum buffer,
1415 GLint drawbuffer,
1416 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001417{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001418 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001419 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001420 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001421 }
1422
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001423 if (buffer == GL_STENCIL)
1424 {
1425 // If all stencil bits are masked, don't attempt to clear stencil.
1426 if (context->getState().getDepthStencilState().stencilWritemask == 0)
1427 {
1428 return angle::Result::Continue;
1429 }
1430 }
1431 else
1432 {
1433 // If all color channels are masked, don't attempt to clear color.
1434 if (context->getState().getBlendState().allChannelsMasked())
1435 {
1436 return angle::Result::Continue;
1437 }
1438 }
1439
Jamie Madill05b35b22017-10-03 09:01:44 -04001440 ANGLE_TRY(mImpl->clearBufferiv(context, buffer, drawbuffer, values));
1441
Jamie Madill7c985f52018-11-29 18:16:17 -05001442 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001443}
1444
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001445angle::Result Framebuffer::clearBufferfi(const Context *context,
1446 GLenum buffer,
1447 GLint drawbuffer,
1448 GLfloat depth,
1449 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -05001450{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001451 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001452 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001453 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001454 }
1455
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001456 bool clearDepth = context->getState().getDepthStencilState().depthMask;
1457 bool clearStencil = context->getState().getDepthStencilState().stencilWritemask != 0;
1458
1459 if (clearDepth && clearStencil)
1460 {
1461 ASSERT(buffer == GL_DEPTH_STENCIL);
1462 ANGLE_TRY(mImpl->clearBufferfi(context, GL_DEPTH_STENCIL, drawbuffer, depth, stencil));
1463 }
1464 else if (clearDepth && !clearStencil)
1465 {
1466 ANGLE_TRY(mImpl->clearBufferfv(context, GL_DEPTH, drawbuffer, &depth));
1467 }
1468 else if (!clearDepth && clearStencil)
1469 {
1470 ANGLE_TRY(mImpl->clearBufferiv(context, GL_STENCIL, drawbuffer, &stencil));
1471 }
Jamie Madill05b35b22017-10-03 09:01:44 -04001472
Jamie Madill7c985f52018-11-29 18:16:17 -05001473 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001474}
1475
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001476angle::Result Framebuffer::getImplementationColorReadFormat(const Context *context,
1477 GLenum *formatOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001478{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001479 ANGLE_TRY(syncState(context));
1480 *formatOut = mImpl->getImplementationColorReadFormat(context);
Jamie Madill7c985f52018-11-29 18:16:17 -05001481 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001482}
1483
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001484angle::Result Framebuffer::getImplementationColorReadType(const Context *context, GLenum *typeOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001485{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001486 ANGLE_TRY(syncState(context));
1487 *typeOut = mImpl->getImplementationColorReadType(context);
Jamie Madill7c985f52018-11-29 18:16:17 -05001488 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001489}
1490
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001491angle::Result Framebuffer::readPixels(const Context *context,
1492 const Rectangle &area,
1493 GLenum format,
1494 GLenum type,
1495 void *pixels)
Geoff Langbce529e2014-12-01 12:48:41 -05001496{
Jamie Madill362876b2016-06-16 14:46:59 -04001497 ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels));
Geoff Lang520c4ae2015-05-05 13:12:36 -04001498
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001499 Buffer *unpackBuffer = context->getState().getTargetBuffer(BufferBinding::PixelUnpack);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001500 if (unpackBuffer)
1501 {
Jamie Madill09463932018-04-04 05:26:59 -04001502 unpackBuffer->onPixelPack(context);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001503 }
1504
Jamie Madill7c985f52018-11-29 18:16:17 -05001505 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001506}
1507
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001508angle::Result Framebuffer::blit(const Context *context,
1509 const Rectangle &sourceArea,
1510 const Rectangle &destArea,
1511 GLbitfield mask,
1512 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -05001513{
He Yunchao6be602d2016-12-22 14:33:07 +08001514 GLbitfield blitMask = mask;
1515
1516 // Note that blitting is called against draw framebuffer.
1517 // See the code in gl::Context::blitFramebuffer.
1518 if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer())
1519 {
1520 blitMask &= ~GL_COLOR_BUFFER_BIT;
1521 }
1522
1523 if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr)
1524 {
1525 blitMask &= ~GL_STENCIL_BUFFER_BIT;
1526 }
1527
1528 if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr)
1529 {
1530 blitMask &= ~GL_DEPTH_BUFFER_BIT;
1531 }
1532
1533 if (!blitMask)
1534 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001535 return angle::Result::Continue;
He Yunchao6be602d2016-12-22 14:33:07 +08001536 }
1537
1538 return mImpl->blit(context, sourceArea, destArea, blitMask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -05001539}
1540
Luc Ferronbf6dc372018-06-28 15:24:19 -04001541bool Framebuffer::isDefault() const
1542{
1543 return id() == 0;
1544}
1545
Jamie Madill427064d2018-04-13 16:20:34 -04001546int Framebuffer::getSamples(const Context *context)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001547{
Jamie Madill427064d2018-04-13 16:20:34 -04001548 return (isComplete(context) ? getCachedSamples(context) : 0);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001549}
1550
Jamie Madill9c335862017-07-18 11:51:38 -04001551int Framebuffer::getCachedSamples(const Context *context)
1552{
Jamie Madill5b772312018-03-08 20:28:32 -05001553 ASSERT(mCachedStatus.valid() && mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE);
1554
Jamie Madill9c335862017-07-18 11:51:38 -04001555 // For a complete framebuffer, all attachments must have the same sample count.
1556 // In this case return the first nonzero sample size.
1557 const auto *firstNonNullAttachment = mState.getFirstNonNullAttachment();
1558 if (firstNonNullAttachment)
1559 {
1560 ASSERT(firstNonNullAttachment->isAttached());
1561 return firstNonNullAttachment->getSamples();
1562 }
1563
1564 // No attachments found.
1565 return 0;
1566}
1567
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001568angle::Result Framebuffer::getSamplePosition(const Context *context,
1569 size_t index,
1570 GLfloat *xy) const
Corentin Wallezccab69d2017-01-27 16:57:15 -05001571{
Geoff Lang13455072018-05-09 11:24:43 -04001572 ANGLE_TRY(mImpl->getSamplePosition(context, index, xy));
Jamie Madill7c985f52018-11-29 18:16:17 -05001573 return angle::Result::Continue;
Corentin Wallezccab69d2017-01-27 16:57:15 -05001574}
1575
Jamie Madille261b442014-06-25 12:42:21 -04001576bool Framebuffer::hasValidDepthStencil() const
1577{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001578 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -04001579}
1580
Jamie Madilla02315b2017-02-23 14:14:47 -05001581void Framebuffer::setAttachment(const Context *context,
1582 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -04001583 GLenum binding,
1584 const ImageIndex &textureIndex,
1585 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -04001586{
Martin Radev5dae57b2017-07-14 16:15:55 +03001587 setAttachment(context, type, binding, textureIndex, resource,
1588 FramebufferAttachment::kDefaultNumViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001589 FramebufferAttachment::kDefaultBaseViewIndex, false);
Martin Radev5dae57b2017-07-14 16:15:55 +03001590}
1591
1592void Framebuffer::setAttachment(const Context *context,
1593 GLenum type,
1594 GLenum binding,
1595 const ImageIndex &textureIndex,
1596 FramebufferAttachmentObject *resource,
1597 GLsizei numViews,
1598 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001599 bool isMultiview)
Martin Radev5dae57b2017-07-14 16:15:55 +03001600{
Jamie Madilla02315b2017-02-23 14:14:47 -05001601 // Context may be null in unit tests.
1602 if (!context || !context->isWebGL1())
1603 {
Martin Radev5dae57b2017-07-14 16:15:55 +03001604 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001605 isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001606 return;
1607 }
1608
1609 switch (binding)
1610 {
1611 case GL_DEPTH_STENCIL:
1612 case GL_DEPTH_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001613 mState.mWebGLDepthStencilAttachment.attach(context, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001614 resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001615 isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001616 break;
1617 case GL_DEPTH:
1618 case GL_DEPTH_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001619 mState.mWebGLDepthAttachment.attach(context, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001620 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001621 break;
1622 case GL_STENCIL:
1623 case GL_STENCIL_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001624 mState.mWebGLStencilAttachment.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 default:
Martin Radev5dae57b2017-07-14 16:15:55 +03001628 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001629 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001630 return;
1631 }
1632
Mingyu Hu7d64c482019-03-12 14:27:40 -07001633 commitWebGL1DepthStencilIfConsistent(context, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001634}
1635
Mingyu Hu7d64c482019-03-12 14:27:40 -07001636void Framebuffer::setAttachmentMultiview(const Context *context,
1637 GLenum type,
1638 GLenum binding,
1639 const ImageIndex &textureIndex,
1640 FramebufferAttachmentObject *resource,
1641 GLsizei numViews,
1642 GLint baseViewIndex)
Martin Radev82ef7742017-08-08 17:44:58 +03001643{
Mingyu Hu7d64c482019-03-12 14:27:40 -07001644 setAttachment(context, type, binding, textureIndex, resource, numViews, baseViewIndex, true);
Martin Radev5dae57b2017-07-14 16:15:55 +03001645}
1646
1647void Framebuffer::commitWebGL1DepthStencilIfConsistent(const Context *context,
1648 GLsizei numViews,
1649 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001650 bool isMultiview)
Jamie Madilla02315b2017-02-23 14:14:47 -05001651{
1652 int count = 0;
1653
1654 std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment,
1655 &mState.mWebGLDepthAttachment,
1656 &mState.mWebGLStencilAttachment}};
1657 for (FramebufferAttachment *attachment : attachments)
1658 {
1659 if (attachment->isAttached())
1660 {
1661 count++;
1662 }
1663 }
1664
1665 mState.mWebGLDepthStencilConsistent = (count <= 1);
1666 if (!mState.mWebGLDepthStencilConsistent)
1667 {
1668 // Inconsistent.
1669 return;
1670 }
1671
Geoff Lange466c552017-03-17 15:24:12 -04001672 auto getImageIndexIfTextureAttachment = [](const FramebufferAttachment &attachment) {
1673 if (attachment.type() == GL_TEXTURE)
1674 {
1675 return attachment.getTextureImageIndex();
1676 }
1677 else
1678 {
Jamie Madillcc129372018-04-12 09:13:18 -04001679 return ImageIndex();
Geoff Lange466c552017-03-17 15:24:12 -04001680 }
1681 };
1682
Jamie Madilla02315b2017-02-23 14:14:47 -05001683 if (mState.mWebGLDepthAttachment.isAttached())
1684 {
1685 const auto &depth = mState.mWebGLDepthAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001686 setAttachmentImpl(context, depth.type(), GL_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001687 getImageIndexIfTextureAttachment(depth), depth.getResource(), numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001688 baseViewIndex, isMultiview);
Jamie Madillcc129372018-04-12 09:13:18 -04001689 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001690 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001691 }
1692 else if (mState.mWebGLStencilAttachment.isAttached())
1693 {
1694 const auto &stencil = mState.mWebGLStencilAttachment;
Jamie Madillcc129372018-04-12 09:13:18 -04001695 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001696 baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001697 setAttachmentImpl(context, stencil.type(), GL_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001698 getImageIndexIfTextureAttachment(stencil), stencil.getResource(),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001699 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001700 }
1701 else if (mState.mWebGLDepthStencilAttachment.isAttached())
1702 {
1703 const auto &depthStencil = mState.mWebGLDepthStencilAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001704 setAttachmentImpl(context, depthStencil.type(), GL_DEPTH_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001705 getImageIndexIfTextureAttachment(depthStencil),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001706 depthStencil.getResource(), numViews, baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001707 setAttachmentImpl(context, depthStencil.type(), GL_STENCIL_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001708 getImageIndexIfTextureAttachment(depthStencil),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001709 depthStencil.getResource(), numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001710 }
1711 else
1712 {
Jamie Madillcc129372018-04-12 09:13:18 -04001713 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001714 baseViewIndex, isMultiview);
Jamie Madillcc129372018-04-12 09:13:18 -04001715 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001716 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001717 }
1718}
1719
Jamie Madill4928b7c2017-06-20 12:57:39 -04001720void Framebuffer::setAttachmentImpl(const Context *context,
1721 GLenum type,
Jamie Madilla02315b2017-02-23 14:14:47 -05001722 GLenum binding,
1723 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001724 FramebufferAttachmentObject *resource,
1725 GLsizei numViews,
1726 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001727 bool isMultiview)
Jamie Madilla02315b2017-02-23 14:14:47 -05001728{
Jamie Madilla02315b2017-02-23 14:14:47 -05001729 switch (binding)
1730 {
Jamie Madillb8126692017-04-05 11:22:17 -04001731 case GL_DEPTH_STENCIL:
1732 case GL_DEPTH_STENCIL_ATTACHMENT:
1733 {
1734 // ensure this is a legitimate depth+stencil format
1735 FramebufferAttachmentObject *attachmentObj = resource;
1736 if (resource)
1737 {
Jamie Madill4fd95d52017-04-05 11:22:18 -04001738 const Format &format = resource->getAttachmentFormat(binding, textureIndex);
Jamie Madillb8126692017-04-05 11:22:17 -04001739 if (format.info->depthBits == 0 || format.info->stencilBits == 0)
1740 {
1741 // Attaching nullptr detaches the current attachment.
1742 attachmentObj = nullptr;
1743 }
1744 }
1745
Jamie Madill4928b7c2017-06-20 12:57:39 -04001746 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001747 &mDirtyDepthAttachmentBinding, type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001748 attachmentObj, numViews, baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001749 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001750 &mDirtyStencilAttachmentBinding, type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001751 attachmentObj, numViews, baseViewIndex, isMultiview);
Jamie Madill42975642017-10-12 12:31:51 -04001752 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001753 }
1754
Jamie Madilla02315b2017-02-23 14:14:47 -05001755 case GL_DEPTH:
1756 case GL_DEPTH_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001757 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001758 &mDirtyDepthAttachmentBinding, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001759 numViews, baseViewIndex, isMultiview);
Jamie Madill2d06b732015-04-20 12:53:28 -04001760 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001761
Jamie Madilla02315b2017-02-23 14:14:47 -05001762 case GL_STENCIL:
1763 case GL_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001764 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001765 &mDirtyStencilAttachmentBinding, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001766 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001767 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001768
Jamie Madilla02315b2017-02-23 14:14:47 -05001769 case GL_BACK:
Geoff Lang8170eab2017-09-21 13:59:04 -04001770 updateAttachment(context, &mState.mColorAttachments[0], DIRTY_BIT_COLOR_ATTACHMENT_0,
1771 &mDirtyColorAttachmentBindings[0], type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001772 resource, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001773 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001774
Jamie Madilla02315b2017-02-23 14:14:47 -05001775 default:
1776 {
1777 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
1778 ASSERT(colorIndex < mState.mColorAttachments.size());
Jamie Madillb8126692017-04-05 11:22:17 -04001779 size_t dirtyBit = DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001780 updateAttachment(context, &mState.mColorAttachments[colorIndex], dirtyBit,
Jamie Madillb8126692017-04-05 11:22:17 -04001781 &mDirtyColorAttachmentBindings[colorIndex], type, binding,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001782 textureIndex, resource, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001783
shrekshaoc87e0052019-02-21 11:40:28 -08001784 if (!resource)
1785 {
shrekshao8413fab2019-04-04 17:13:18 -07001786 mColorAttachmentBits.reset(colorIndex);
shrekshaoc87e0052019-02-21 11:40:28 -08001787 mFloat32ColorAttachmentBits.reset(colorIndex);
1788 }
1789 else
1790 {
shrekshao8413fab2019-04-04 17:13:18 -07001791 mColorAttachmentBits.set(colorIndex);
shrekshaoc87e0052019-02-21 11:40:28 -08001792 updateFloat32ColorAttachmentBits(
1793 colorIndex, resource->getAttachmentFormat(binding, textureIndex).info);
1794 }
1795
Corentin Walleze7557742017-06-01 13:09:57 -04001796 // TODO(jmadill): ASSERT instead of checking the attachment exists in
1797 // formsRenderingFeedbackLoopWith
Jamie Madilla02315b2017-02-23 14:14:47 -05001798 bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE);
1799 mState.mEnabledDrawBuffers.set(colorIndex, enabled);
Jamie Madill6e18a232019-01-16 13:27:14 -05001800 SetComponentTypeMask(getDrawbufferWriteType(colorIndex), colorIndex,
1801 &mState.mDrawBufferTypeMask);
Jamie Madill2d06b732015-04-20 12:53:28 -04001802 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001803 break;
Geoff Langab75a052014-10-15 12:56:37 -04001804 }
1805}
1806
Jamie Madill4928b7c2017-06-20 12:57:39 -04001807void Framebuffer::updateAttachment(const Context *context,
1808 FramebufferAttachment *attachment,
Jamie Madillb8126692017-04-05 11:22:17 -04001809 size_t dirtyBit,
Jamie Madilld4442552018-02-27 22:03:47 -05001810 angle::ObserverBinding *onDirtyBinding,
Jamie Madillb8126692017-04-05 11:22:17 -04001811 GLenum type,
1812 GLenum binding,
1813 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001814 FramebufferAttachmentObject *resource,
1815 GLsizei numViews,
1816 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001817 bool isMultiview)
Jamie Madillb8126692017-04-05 11:22:17 -04001818{
Martin Radev5dae57b2017-07-14 16:15:55 +03001819 attachment->attach(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001820 isMultiview);
Jamie Madillb8126692017-04-05 11:22:17 -04001821 mDirtyBits.set(dirtyBit);
Jamie Madill05b35b22017-10-03 09:01:44 -04001822 mState.mResourceNeedsInit.set(dirtyBit, attachment->initState() == InitState::MayNeedInit);
Jamie Madill66f0d2c2018-11-30 15:25:36 -05001823 onDirtyBinding->bind(resource);
Jamie Madille98b1b52018-03-08 09:47:23 -05001824
Jamie Madillb983a4b2018-08-01 11:34:51 -04001825 invalidateCompletenessCache(context);
Jamie Madillb8126692017-04-05 11:22:17 -04001826}
1827
Jamie Madilla02315b2017-02-23 14:14:47 -05001828void Framebuffer::resetAttachment(const Context *context, GLenum binding)
Jamie Madill2d06b732015-04-20 12:53:28 -04001829{
Jamie Madillcc129372018-04-12 09:13:18 -04001830 setAttachment(context, GL_NONE, binding, ImageIndex(), nullptr);
Jamie Madill2d06b732015-04-20 12:53:28 -04001831}
1832
Jamie Madill6f755b22018-10-09 12:48:54 -04001833angle::Result Framebuffer::syncState(const Context *context)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001834{
1835 if (mDirtyBits.any())
1836 {
Jamie Madill888081d2018-02-27 00:24:46 -05001837 mDirtyBitsGuard = mDirtyBits;
Jamie Madill19fa1c62018-03-08 09:47:21 -05001838 ANGLE_TRY(mImpl->syncState(context, mDirtyBits));
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001839 mDirtyBits.reset();
Jamie Madill888081d2018-02-27 00:24:46 -05001840 mDirtyBitsGuard.reset();
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001841 }
Jamie Madill7c985f52018-11-29 18:16:17 -05001842 return angle::Result::Continue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001844
Jamie Madilld4442552018-02-27 22:03:47 -05001845void Framebuffer::onSubjectStateChange(const Context *context,
1846 angle::SubjectIndex index,
1847 angle::SubjectMessage message)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001848{
Jamie Madille4faae22019-05-10 08:27:00 -04001849 if (message != angle::SubjectMessage::SubjectChanged)
Jamie Madillf668a4b2018-09-23 17:01:20 -04001850 {
1851 // This can be triggered by the GL back-end TextureGL class.
Jamie Madille4faae22019-05-10 08:27:00 -04001852 ASSERT(message == angle::SubjectMessage::DirtyBitsFlagged);
Jamie Madillf668a4b2018-09-23 17:01:20 -04001853 return;
1854 }
Jamie Madill55e57f92018-09-18 11:32:43 -04001855
1856 ASSERT(!mDirtyBitsGuard.valid() || mDirtyBitsGuard.value().test(index));
1857 mDirtyBits.set(index);
Jamie Madill888081d2018-02-27 00:24:46 -05001858
Jamie Madillb983a4b2018-08-01 11:34:51 -04001859 invalidateCompletenessCache(context);
Jamie Madill05b35b22017-10-03 09:01:44 -04001860
Jamie Madilld4442552018-02-27 22:03:47 -05001861 FramebufferAttachment *attachment = getAttachmentFromSubjectIndex(index);
1862
Jamie Madill05b35b22017-10-03 09:01:44 -04001863 // Mark the appropriate init flag.
Jamie Madilld4442552018-02-27 22:03:47 -05001864 mState.mResourceNeedsInit.set(index, attachment->initState() == InitState::MayNeedInit);
shrekshaoc87e0052019-02-21 11:40:28 -08001865
1866 // Update mFloat32ColorAttachmentBits Cache
1867 if (index < DIRTY_BIT_COLOR_ATTACHMENT_MAX)
1868 {
1869 ASSERT(index != DIRTY_BIT_DEPTH_ATTACHMENT);
1870 ASSERT(index != DIRTY_BIT_STENCIL_ATTACHMENT);
1871 updateFloat32ColorAttachmentBits(index - DIRTY_BIT_COLOR_ATTACHMENT_0,
1872 attachment->getFormat().info);
1873 }
Jamie Madilld4442552018-02-27 22:03:47 -05001874}
1875
1876FramebufferAttachment *Framebuffer::getAttachmentFromSubjectIndex(angle::SubjectIndex index)
1877{
1878 switch (index)
1879 {
1880 case DIRTY_BIT_DEPTH_ATTACHMENT:
1881 return &mState.mDepthAttachment;
1882 case DIRTY_BIT_STENCIL_ATTACHMENT:
1883 return &mState.mStencilAttachment;
1884 default:
1885 size_t colorIndex = (index - DIRTY_BIT_COLOR_ATTACHMENT_0);
1886 ASSERT(colorIndex < mState.mColorAttachments.size());
1887 return &mState.mColorAttachments[colorIndex];
1888 }
Jamie Madill51f40ec2016-06-15 14:06:00 -04001889}
1890
shrekshao8413fab2019-04-04 17:13:18 -07001891bool Framebuffer::formsRenderingFeedbackLoopWith(const Context *context) const
Jamie Madilla4595b82017-01-11 17:36:34 -05001892{
shrekshao8413fab2019-04-04 17:13:18 -07001893 const State &state = context->getState();
Jamie Madill785e8a02018-10-04 17:42:00 -04001894 const Program *program = state.getProgram();
Jamie Madilla4595b82017-01-11 17:36:34 -05001895
1896 // TODO(jmadill): Default framebuffer feedback loops.
Jamie Madill2274b652018-05-31 10:56:08 -04001897 if (mState.mId == 0)
Jamie Madilla4595b82017-01-11 17:36:34 -05001898 {
1899 return false;
1900 }
1901
shrekshao8413fab2019-04-04 17:13:18 -07001902 const FramebufferAttachment *depth = getDepthbuffer();
1903 const FramebufferAttachment *stencil = getStencilbuffer();
1904
1905 const bool checkDepth = depth && depth->type() == GL_TEXTURE;
1906 // Skip the feedback loop check for stencil if depth/stencil point to the same resource.
1907 const bool checkStencil =
1908 (stencil && stencil->type() == GL_TEXTURE) && (!depth || *stencil != *depth);
1909
1910 const gl::ActiveTextureMask &activeTextures = program->getActiveSamplersMask();
1911 const gl::ActiveTexturePointerArray &textures = state.getActiveTexturesCache();
1912
1913 for (size_t textureUnit : activeTextures)
Jamie Madilla4595b82017-01-11 17:36:34 -05001914 {
shrekshao8413fab2019-04-04 17:13:18 -07001915 Texture *texture = textures[textureUnit];
1916
1917 if (texture == nullptr)
Jamie Madilla4595b82017-01-11 17:36:34 -05001918 {
shrekshao8413fab2019-04-04 17:13:18 -07001919 continue;
1920 }
1921
1922 // Depth and stencil attachment form feedback loops
1923 // Regardless of if enabled or masked.
1924 if (checkDepth)
1925 {
1926 if (texture->id() == depth->id())
Jamie Madilla4595b82017-01-11 17:36:34 -05001927 {
1928 return true;
1929 }
1930 }
Jamie Madilla4595b82017-01-11 17:36:34 -05001931
shrekshao8413fab2019-04-04 17:13:18 -07001932 if (checkStencil)
Jamie Madill1d37bc52017-02-02 19:59:58 -05001933 {
shrekshao8413fab2019-04-04 17:13:18 -07001934 if (texture->id() == stencil->id())
Jamie Madill1d37bc52017-02-02 19:59:58 -05001935 {
Jamie Madill38fe6842018-09-19 07:20:00 -04001936 return true;
Jamie Madill1d37bc52017-02-02 19:59:58 -05001937 }
1938 }
shrekshao8413fab2019-04-04 17:13:18 -07001939
1940 // Check if any color attachment forms a feedback loop.
1941 for (size_t drawIndex : mColorAttachmentBits)
1942 {
1943 const FramebufferAttachment &attachment = mState.mColorAttachments[drawIndex];
1944 ASSERT(attachment.isAttached());
1945
1946 if (attachment.isTextureWithId(texture->id()))
1947 {
1948 // TODO(jmadill): Check for appropriate overlap.
1949 return true;
1950 }
1951 }
Jamie Madill1d37bc52017-02-02 19:59:58 -05001952 }
1953
Jamie Madilla4595b82017-01-11 17:36:34 -05001954 return false;
1955}
1956
Jamie Madillfd3dd432017-02-02 19:59:59 -05001957bool Framebuffer::formsCopyingFeedbackLoopWith(GLuint copyTextureID,
1958 GLint copyTextureLevel,
1959 GLint copyTextureLayer) const
Jamie Madillf695a3a2017-01-11 17:36:35 -05001960{
Jamie Madill2274b652018-05-31 10:56:08 -04001961 if (mState.mId == 0)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001962 {
1963 // It seems impossible to form a texture copying feedback loop with the default FBO.
1964 return false;
1965 }
1966
1967 const FramebufferAttachment *readAttachment = getReadColorbuffer();
1968 ASSERT(readAttachment);
1969
1970 if (readAttachment->isTextureWithId(copyTextureID))
1971 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001972 const auto &imageIndex = readAttachment->getTextureImageIndex();
Jamie Madillcc129372018-04-12 09:13:18 -04001973 if (imageIndex.getLevelIndex() == copyTextureLevel)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001974 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001975 // Check 3D/Array texture layers.
Jamie Madillcc129372018-04-12 09:13:18 -04001976 return !imageIndex.hasLayer() || copyTextureLayer == ImageIndex::kEntireLevel ||
1977 imageIndex.getLayerIndex() == copyTextureLayer;
Jamie Madillf695a3a2017-01-11 17:36:35 -05001978 }
1979 }
1980 return false;
1981}
1982
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001983GLint Framebuffer::getDefaultWidth() const
1984{
1985 return mState.getDefaultWidth();
1986}
1987
1988GLint Framebuffer::getDefaultHeight() const
1989{
1990 return mState.getDefaultHeight();
1991}
1992
1993GLint Framebuffer::getDefaultSamples() const
1994{
1995 return mState.getDefaultSamples();
1996}
1997
Geoff Lang92019432017-11-20 13:09:34 -05001998bool Framebuffer::getDefaultFixedSampleLocations() const
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001999{
2000 return mState.getDefaultFixedSampleLocations();
2001}
2002
Jiawei Shaob1e91382018-05-17 14:33:55 +08002003GLint Framebuffer::getDefaultLayers() const
2004{
2005 return mState.getDefaultLayers();
2006}
2007
Jamie Madillb983a4b2018-08-01 11:34:51 -04002008void Framebuffer::setDefaultWidth(const Context *context, GLint defaultWidth)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002009{
2010 mState.mDefaultWidth = defaultWidth;
2011 mDirtyBits.set(DIRTY_BIT_DEFAULT_WIDTH);
Jamie Madillb983a4b2018-08-01 11:34:51 -04002012 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002013}
2014
Jamie Madillb983a4b2018-08-01 11:34:51 -04002015void Framebuffer::setDefaultHeight(const Context *context, GLint defaultHeight)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002016{
2017 mState.mDefaultHeight = defaultHeight;
2018 mDirtyBits.set(DIRTY_BIT_DEFAULT_HEIGHT);
Jamie Madillb983a4b2018-08-01 11:34:51 -04002019 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002020}
2021
Jamie Madillb983a4b2018-08-01 11:34:51 -04002022void Framebuffer::setDefaultSamples(const Context *context, GLint defaultSamples)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002023{
2024 mState.mDefaultSamples = defaultSamples;
2025 mDirtyBits.set(DIRTY_BIT_DEFAULT_SAMPLES);
Jamie Madillb983a4b2018-08-01 11:34:51 -04002026 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002027}
2028
Jamie Madillb983a4b2018-08-01 11:34:51 -04002029void Framebuffer::setDefaultFixedSampleLocations(const Context *context,
2030 bool defaultFixedSampleLocations)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002031{
2032 mState.mDefaultFixedSampleLocations = defaultFixedSampleLocations;
2033 mDirtyBits.set(DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS);
Jamie Madillb983a4b2018-08-01 11:34:51 -04002034 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002035}
2036
Jiawei Shaob1e91382018-05-17 14:33:55 +08002037void Framebuffer::setDefaultLayers(GLint defaultLayers)
2038{
2039 mState.mDefaultLayers = defaultLayers;
2040 mDirtyBits.set(DIRTY_BIT_DEFAULT_LAYERS);
2041}
2042
Martin Radev14a26ae2017-07-24 15:56:29 +03002043GLsizei Framebuffer::getNumViews() const
2044{
Martin Radev5c00d0d2017-08-07 10:06:59 +03002045 return mState.getNumViews();
Martin Radev14a26ae2017-07-24 15:56:29 +03002046}
2047
Martin Radev4e619f52017-08-09 11:50:06 +03002048GLint Framebuffer::getBaseViewIndex() const
2049{
2050 return mState.getBaseViewIndex();
2051}
2052
Mingyu Hu7d64c482019-03-12 14:27:40 -07002053bool Framebuffer::isMultiview() const
Martin Radev878c8b12017-07-28 09:51:04 +03002054{
Mingyu Hu7d64c482019-03-12 14:27:40 -07002055 return mState.isMultiview();
Martin Radev878c8b12017-07-28 09:51:04 +03002056}
2057
Olli Etuaho8acb1b62018-07-30 16:20:54 +03002058bool Framebuffer::readDisallowedByMultiview() const
2059{
Mingyu Hu7d64c482019-03-12 14:27:40 -07002060 return (mState.isMultiview() && mState.getNumViews() > 1);
Olli Etuaho8acb1b62018-07-30 16:20:54 +03002061}
2062
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002063angle::Result Framebuffer::ensureClearAttachmentsInitialized(const Context *context,
2064 GLbitfield mask)
Geoff Langd4fff502017-09-22 11:28:28 -04002065{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05002066 const auto &glState = context->getState();
Geoff Langd4fff502017-09-22 11:28:28 -04002067 if (!context->isRobustResourceInitEnabled() || glState.isRasterizerDiscardEnabled())
2068 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002069 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002070 }
2071
Geoff Langa36483f2018-03-09 16:11:21 -05002072 const BlendState &blend = glState.getBlendState();
2073 const DepthStencilState &depthStencil = glState.getDepthStencilState();
Geoff Langd4fff502017-09-22 11:28:28 -04002074
2075 bool color = (mask & GL_COLOR_BUFFER_BIT) != 0 && !IsColorMaskedOut(blend);
2076 bool depth = (mask & GL_DEPTH_BUFFER_BIT) != 0 && !IsDepthMaskedOut(depthStencil);
2077 bool stencil = (mask & GL_STENCIL_BUFFER_BIT) != 0 && !IsStencilMaskedOut(depthStencil);
2078
2079 if (!color && !depth && !stencil)
2080 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002081 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002082 }
2083
2084 if (partialClearNeedsInit(context, color, depth, stencil))
2085 {
2086 ANGLE_TRY(ensureDrawAttachmentsInitialized(context));
2087 }
2088
2089 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2090 // still be marked initialized. This simplifies design, allowing this method to be called before
2091 // the clear.
2092 markDrawAttachmentsInitialized(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
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002097angle::Result Framebuffer::ensureClearBufferAttachmentsInitialized(const Context *context,
2098 GLenum buffer,
2099 GLint drawbuffer)
Geoff Langd4fff502017-09-22 11:28:28 -04002100{
2101 if (!context->isRobustResourceInitEnabled() ||
Jamie Madillc3dc5d42018-12-30 12:12:04 -05002102 context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Geoff Langd4fff502017-09-22 11:28:28 -04002103 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002104 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002105 }
2106
2107 if (partialBufferClearNeedsInit(context, buffer))
2108 {
2109 ANGLE_TRY(ensureBufferInitialized(context, buffer, drawbuffer));
2110 }
2111
2112 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2113 // still be marked initialized. This simplifies design, allowing this method to be called before
2114 // the clear.
2115 markBufferInitialized(buffer, drawbuffer);
2116
Jamie Madill7c985f52018-11-29 18:16:17 -05002117 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002118}
2119
Jamie Madill6f755b22018-10-09 12:48:54 -04002120angle::Result Framebuffer::ensureDrawAttachmentsInitialized(const Context *context)
Jamie Madill05b35b22017-10-03 09:01:44 -04002121{
2122 if (!context->isRobustResourceInitEnabled())
2123 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002124 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002125 }
2126
2127 // Note: we don't actually filter by the draw attachment enum. Just init everything.
2128 for (size_t bit : mState.mResourceNeedsInit)
2129 {
2130 switch (bit)
2131 {
2132 case DIRTY_BIT_DEPTH_ATTACHMENT:
2133 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2134 break;
2135 case DIRTY_BIT_STENCIL_ATTACHMENT:
2136 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2137 break;
2138 default:
2139 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bit]));
2140 break;
2141 }
2142 }
2143
2144 mState.mResourceNeedsInit.reset();
Jamie Madill7c985f52018-11-29 18:16:17 -05002145 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002146}
2147
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002148angle::Result Framebuffer::ensureReadAttachmentsInitialized(const Context *context)
Jamie Madill05b35b22017-10-03 09:01:44 -04002149{
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002150 ASSERT(context->isRobustResourceInitEnabled());
2151
2152 if (mState.mResourceNeedsInit.none())
Jamie Madill05b35b22017-10-03 09:01:44 -04002153 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002154 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002155 }
2156
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002157 if (mState.mReadBufferState != GL_NONE)
Jamie Madill05b35b22017-10-03 09:01:44 -04002158 {
2159 size_t readIndex = mState.getReadIndex();
2160 if (mState.mResourceNeedsInit[readIndex])
2161 {
2162 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[readIndex]));
2163 mState.mResourceNeedsInit.reset(readIndex);
2164 }
2165 }
2166
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002167 // Conservatively init depth since it can be read by BlitFramebuffer.
2168 if (hasDepth())
Jamie Madill05b35b22017-10-03 09:01:44 -04002169 {
2170 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2171 {
2172 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2173 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2174 }
2175 }
2176
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002177 // Conservatively init stencil since it can be read by BlitFramebuffer.
2178 if (hasStencil())
Jamie Madill05b35b22017-10-03 09:01:44 -04002179 {
2180 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2181 {
2182 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2183 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2184 }
2185 }
2186
Jamie Madill7c985f52018-11-29 18:16:17 -05002187 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002188}
2189
2190void Framebuffer::markDrawAttachmentsInitialized(bool color, bool depth, bool stencil)
2191{
2192 // Mark attachments as initialized.
2193 if (color)
2194 {
2195 for (auto colorIndex : mState.mEnabledDrawBuffers)
2196 {
2197 auto &colorAttachment = mState.mColorAttachments[colorIndex];
2198 ASSERT(colorAttachment.isAttached());
2199 colorAttachment.setInitState(InitState::Initialized);
2200 mState.mResourceNeedsInit.reset(colorIndex);
2201 }
2202 }
2203
2204 if (depth && mState.mDepthAttachment.isAttached())
2205 {
2206 mState.mDepthAttachment.setInitState(InitState::Initialized);
2207 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2208 }
2209
2210 if (stencil && mState.mStencilAttachment.isAttached())
2211 {
2212 mState.mStencilAttachment.setInitState(InitState::Initialized);
2213 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2214 }
2215}
2216
2217void Framebuffer::markBufferInitialized(GLenum bufferType, GLint bufferIndex)
2218{
2219 switch (bufferType)
2220 {
2221 case GL_COLOR:
2222 {
2223 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2224 if (mState.mColorAttachments[bufferIndex].isAttached())
2225 {
2226 mState.mColorAttachments[bufferIndex].setInitState(InitState::Initialized);
2227 mState.mResourceNeedsInit.reset(bufferIndex);
2228 }
2229 break;
2230 }
2231 case GL_DEPTH:
2232 {
2233 if (mState.mDepthAttachment.isAttached())
2234 {
2235 mState.mDepthAttachment.setInitState(InitState::Initialized);
2236 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2237 }
2238 break;
2239 }
2240 case GL_STENCIL:
2241 {
2242 if (mState.mStencilAttachment.isAttached())
2243 {
2244 mState.mStencilAttachment.setInitState(InitState::Initialized);
2245 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2246 }
2247 break;
2248 }
2249 case GL_DEPTH_STENCIL:
2250 {
2251 if (mState.mDepthAttachment.isAttached())
2252 {
2253 mState.mDepthAttachment.setInitState(InitState::Initialized);
2254 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2255 }
2256 if (mState.mStencilAttachment.isAttached())
2257 {
2258 mState.mStencilAttachment.setInitState(InitState::Initialized);
2259 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2260 }
2261 break;
2262 }
2263 default:
2264 UNREACHABLE();
2265 break;
2266 }
2267}
2268
2269Box Framebuffer::getDimensions() const
2270{
2271 return mState.getDimensions();
2272}
2273
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002274angle::Result Framebuffer::ensureBufferInitialized(const Context *context,
2275 GLenum bufferType,
2276 GLint bufferIndex)
Jamie Madill05b35b22017-10-03 09:01:44 -04002277{
2278 ASSERT(context->isRobustResourceInitEnabled());
2279
2280 if (mState.mResourceNeedsInit.none())
2281 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002282 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002283 }
2284
2285 switch (bufferType)
2286 {
2287 case GL_COLOR:
2288 {
2289 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2290 if (mState.mResourceNeedsInit[bufferIndex])
2291 {
2292 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bufferIndex]));
2293 mState.mResourceNeedsInit.reset(bufferIndex);
2294 }
2295 break;
2296 }
2297 case GL_DEPTH:
2298 {
2299 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2300 {
2301 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2302 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2303 }
2304 break;
2305 }
2306 case GL_STENCIL:
2307 {
2308 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2309 {
2310 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2311 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2312 }
2313 break;
2314 }
2315 case GL_DEPTH_STENCIL:
2316 {
2317 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2318 {
2319 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2320 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2321 }
2322 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2323 {
2324 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2325 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2326 }
2327 break;
2328 }
2329 default:
2330 UNREACHABLE();
2331 break;
2332 }
2333
Jamie Madill7c985f52018-11-29 18:16:17 -05002334 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002335}
2336
2337bool Framebuffer::partialBufferClearNeedsInit(const Context *context, GLenum bufferType)
2338{
2339 if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none())
2340 {
2341 return false;
2342 }
2343
2344 switch (bufferType)
2345 {
2346 case GL_COLOR:
2347 return partialClearNeedsInit(context, true, false, false);
2348 case GL_DEPTH:
2349 return partialClearNeedsInit(context, false, true, false);
2350 case GL_STENCIL:
2351 return partialClearNeedsInit(context, false, false, true);
2352 case GL_DEPTH_STENCIL:
2353 return partialClearNeedsInit(context, false, true, true);
2354 default:
2355 UNREACHABLE();
2356 return false;
2357 }
2358}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002359} // namespace gl