blob: f0be4a1fdcd0dcc311e92f1672e13472e6dde5c6 [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);
Geoff Langee244c72019-05-06 10:30:18 -0400656
657 // Ensure the backend has a chance to synchronize its content for a new backbuffer.
658 mDirtyBits.set(DIRTY_BIT_COLOR_BUFFER_CONTENTS_0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659}
660
Corentin Wallezccab69d2017-01-27 16:57:15 -0500661Framebuffer::Framebuffer(rx::GLImplFactory *factory)
662 : mState(),
663 mImpl(factory->createFramebuffer(mState)),
Corentin Wallezccab69d2017-01-27 16:57:15 -0500664 mCachedStatus(GL_FRAMEBUFFER_UNDEFINED_OES),
665 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
666 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
667{
Jamie Madill1e5499d2017-04-05 11:22:16 -0400668 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Jamie Madill6e18a232019-01-16 13:27:14 -0500669 SetComponentTypeMask(getDrawbufferWriteType(0), 0, &mState.mDrawBufferTypeMask);
Corentin Wallezccab69d2017-01-27 16:57:15 -0500670}
671
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000672Framebuffer::~Framebuffer()
673{
Geoff Langda88add2014-12-01 10:22:01 -0500674 SafeDelete(mImpl);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000675}
676
Jamie Madill4928b7c2017-06-20 12:57:39 -0400677void Framebuffer::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500678{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400679 for (auto &attachment : mState.mColorAttachments)
680 {
681 attachment.detach(context);
682 }
683 mState.mDepthAttachment.detach(context);
684 mState.mStencilAttachment.detach(context);
685 mState.mWebGLDepthAttachment.detach(context);
686 mState.mWebGLStencilAttachment.detach(context);
687 mState.mWebGLDepthStencilAttachment.detach(context);
688
Jamie Madillc564c072017-06-01 12:45:42 -0400689 mImpl->destroy(context);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500690}
691
Jamie Madille90d4ee2018-11-28 14:04:00 -0500692void Framebuffer::setLabel(const Context *context, const std::string &label)
Geoff Lang70d0f492015-12-10 17:45:46 -0500693{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400694 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500695}
696
697const std::string &Framebuffer::getLabel() const
698{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400699 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500700}
701
Jamie Madill8693bdb2017-09-02 15:32:14 -0400702bool Framebuffer::detachTexture(const Context *context, GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000703{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400704 return detachResourceById(context, GL_TEXTURE, textureId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000705}
706
Jamie Madill8693bdb2017-09-02 15:32:14 -0400707bool Framebuffer::detachRenderbuffer(const Context *context, GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000708{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400709 return detachResourceById(context, GL_RENDERBUFFER, renderbufferId);
Jamie Madilld1405e52015-03-05 15:41:39 -0500710}
Jamie Madille261b442014-06-25 12:42:21 -0400711
Jamie Madill8693bdb2017-09-02 15:32:14 -0400712bool Framebuffer::detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId)
Jamie Madilld1405e52015-03-05 15:41:39 -0500713{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400714 bool found = false;
715
Jamie Madill362876b2016-06-16 14:46:59 -0400716 for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
Jamie Madilld1405e52015-03-05 15:41:39 -0500717 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400718 if (detachMatchingAttachment(context, &mState.mColorAttachments[colorIndex], resourceType,
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300719 resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400720 {
721 found = true;
722 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723 }
724
Jamie Madilla02315b2017-02-23 14:14:47 -0500725 if (context->isWebGL1())
726 {
727 const std::array<FramebufferAttachment *, 3> attachments = {
728 {&mState.mWebGLDepthStencilAttachment, &mState.mWebGLDepthAttachment,
729 &mState.mWebGLStencilAttachment}};
730 for (FramebufferAttachment *attachment : attachments)
731 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300732 if (detachMatchingAttachment(context, attachment, resourceType, resourceId))
Jamie Madilla02315b2017-02-23 14:14:47 -0500733 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400734 found = true;
Jamie Madilla02315b2017-02-23 14:14:47 -0500735 }
736 }
737 }
738 else
739 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300740 if (detachMatchingAttachment(context, &mState.mDepthAttachment, resourceType, resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400741 {
742 found = true;
743 }
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300744 if (detachMatchingAttachment(context, &mState.mStencilAttachment, resourceType, resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400745 {
746 found = true;
747 }
Jamie Madilla02315b2017-02-23 14:14:47 -0500748 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400749
750 return found;
Jamie Madill362876b2016-06-16 14:46:59 -0400751}
752
Jamie Madill8693bdb2017-09-02 15:32:14 -0400753bool Framebuffer::detachMatchingAttachment(const Context *context,
Jamie Madill4928b7c2017-06-20 12:57:39 -0400754 FramebufferAttachment *attachment,
Jamie Madill362876b2016-06-16 14:46:59 -0400755 GLenum matchType,
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300756 GLuint matchId)
Jamie Madill362876b2016-06-16 14:46:59 -0400757{
758 if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId)
759 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300760 // We go through resetAttachment to make sure that all the required bookkeeping will be done
761 // such as updating enabled draw buffer state.
762 resetAttachment(context, attachment->getBinding());
Jamie Madill8693bdb2017-09-02 15:32:14 -0400763 return true;
Jamie Madill362876b2016-06-16 14:46:59 -0400764 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400765
766 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000767}
768
Corentin Wallez37c39792015-08-20 14:19:46 -0400769const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400771 return mState.getColorAttachment(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000772}
773
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400774const FramebufferAttachment *Framebuffer::getDepthbuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400775{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400776 return mState.getDepthAttachment();
Geoff Lang646559f2013-08-15 11:08:15 -0400777}
778
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400779const FramebufferAttachment *Framebuffer::getStencilbuffer() const
780{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400781 return mState.getStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400782}
783
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400784const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
785{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400786 return mState.getDepthStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400787}
788
789const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000790{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400791 return mState.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000792}
793
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500794const FramebufferAttachment *Framebuffer::getStencilOrDepthStencilAttachment() const
795{
796 return mState.getStencilOrDepthStencilAttachment();
797}
798
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400799const FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000800{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400801 return mState.getReadAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000802}
803
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000804GLenum Framebuffer::getReadColorbufferType() const
805{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400806 const FramebufferAttachment *readAttachment = mState.getReadAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400807 return (readAttachment != nullptr ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000808}
809
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400810const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000811{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400812 return mState.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000813}
814
Jamie Madill6dd06ea2017-07-19 13:47:55 -0400815const FramebufferAttachment *Framebuffer::getFirstNonNullAttachment() const
816{
817 return mState.getFirstNonNullAttachment();
818}
819
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800820const FramebufferAttachment *Framebuffer::getAttachment(const Context *context,
821 GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000822{
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800823 return mState.getAttachment(context, attachment);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400824}
825
Geoff Langa15472a2015-08-11 11:48:03 -0400826size_t Framebuffer::getDrawbufferStateCount() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000827{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400828 return mState.mDrawBufferStates.size();
Geoff Langa15472a2015-08-11 11:48:03 -0400829}
830
831GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const
832{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400833 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
834 return mState.mDrawBufferStates[drawBuffer];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000835}
836
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500837const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const
838{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400839 return mState.getDrawBufferStates();
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500840}
841
Geoff Lang164d54e2014-12-01 10:55:33 -0500842void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000843{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400844 auto &drawStates = mState.mDrawBufferStates;
Jamie Madilld1405e52015-03-05 15:41:39 -0500845
846 ASSERT(count <= drawStates.size());
847 std::copy(buffers, buffers + count, drawStates.begin());
848 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500849 mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS);
Jamie Madilla4595b82017-01-11 17:36:34 -0500850
851 mState.mEnabledDrawBuffers.reset();
Brandon Jones76746f92017-11-22 11:44:41 -0800852 mState.mDrawBufferTypeMask.reset();
853
Jamie Madilla4595b82017-01-11 17:36:34 -0500854 for (size_t index = 0; index < count; ++index)
855 {
Jamie Madill6e18a232019-01-16 13:27:14 -0500856 SetComponentTypeMask(getDrawbufferWriteType(index), index, &mState.mDrawBufferTypeMask);
Brandon Jones76746f92017-11-22 11:44:41 -0800857
Jamie Madilla4595b82017-01-11 17:36:34 -0500858 if (drawStates[index] != GL_NONE && mState.mColorAttachments[index].isAttached())
859 {
860 mState.mEnabledDrawBuffers.set(index);
861 }
862 }
Geoff Lang9dd95802014-12-01 11:12:59 -0500863}
864
Geoff Langa15472a2015-08-11 11:48:03 -0400865const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const
866{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400867 return mState.getDrawBuffer(drawBuffer);
Geoff Langa15472a2015-08-11 11:48:03 -0400868}
869
Jamie Madill6e18a232019-01-16 13:27:14 -0500870ComponentType Framebuffer::getDrawbufferWriteType(size_t drawBuffer) const
Geoff Lange0cff192017-05-30 13:04:56 -0400871{
872 const FramebufferAttachment *attachment = mState.getDrawBuffer(drawBuffer);
873 if (attachment == nullptr)
874 {
Jamie Madill6e18a232019-01-16 13:27:14 -0500875 return ComponentType::NoType;
Geoff Lange0cff192017-05-30 13:04:56 -0400876 }
877
878 GLenum componentType = attachment->getFormat().info->componentType;
879 switch (componentType)
880 {
881 case GL_INT:
Jamie Madill6e18a232019-01-16 13:27:14 -0500882 return ComponentType::Int;
Geoff Lange0cff192017-05-30 13:04:56 -0400883 case GL_UNSIGNED_INT:
Jamie Madill6e18a232019-01-16 13:27:14 -0500884 return ComponentType::UnsignedInt;
Geoff Lange0cff192017-05-30 13:04:56 -0400885
886 default:
Jamie Madill6e18a232019-01-16 13:27:14 -0500887 return ComponentType::Float;
Geoff Lange0cff192017-05-30 13:04:56 -0400888 }
889}
890
Brandon Jonesc405ae72017-12-06 14:15:03 -0800891ComponentTypeMask Framebuffer::getDrawBufferTypeMask() const
Brandon Jones76746f92017-11-22 11:44:41 -0800892{
893 return mState.mDrawBufferTypeMask;
894}
895
896DrawBufferMask Framebuffer::getDrawBufferMask() const
897{
898 return mState.mEnabledDrawBuffers;
899}
900
Geoff Langa15472a2015-08-11 11:48:03 -0400901bool Framebuffer::hasEnabledDrawBuffer() const
902{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400903 for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
Geoff Langa15472a2015-08-11 11:48:03 -0400904 {
905 if (getDrawBuffer(drawbufferIdx) != nullptr)
906 {
907 return true;
908 }
909 }
910
911 return false;
912}
913
Geoff Lang9dd95802014-12-01 11:12:59 -0500914GLenum Framebuffer::getReadBufferState() const
915{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400916 return mState.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500917}
918
919void Framebuffer::setReadBuffer(GLenum buffer)
920{
Jamie Madillb885e572015-02-03 16:16:04 -0500921 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
922 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madill48ef11b2016-04-27 15:21:52 -0400923 (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size()));
924 mState.mReadBufferState = buffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500925 mDirtyBits.set(DIRTY_BIT_READ_BUFFER);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000926}
927
Corentin Wallez37c39792015-08-20 14:19:46 -0400928size_t Framebuffer::getNumColorBuffers() const
929{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400930 return mState.mColorAttachments.size();
Corentin Wallez37c39792015-08-20 14:19:46 -0400931}
932
Jamie Madill0df8fe42015-11-24 16:10:24 -0500933bool Framebuffer::hasDepth() const
934{
Jamie Madill9c335862017-07-18 11:51:38 -0400935 return mState.hasDepth();
Jamie Madill0df8fe42015-11-24 16:10:24 -0500936}
937
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000938bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000939{
Jamie Madill9c335862017-07-18 11:51:38 -0400940 return mState.hasStencil();
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000941}
942
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000943bool Framebuffer::usingExtendedDrawBuffers() const
944{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400945 for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000946 {
Geoff Langa15472a2015-08-11 11:48:03 -0400947 if (getDrawBuffer(drawbufferIdx) != nullptr)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000948 {
949 return true;
950 }
951 }
952
953 return false;
954}
955
Jamie Madill124f78c2019-06-18 11:48:24 -0400956void Framebuffer::invalidateCompletenessCache()
Geoff Lang9aded172017-04-05 11:07:56 -0400957{
Jamie Madill2274b652018-05-31 10:56:08 -0400958 if (mState.mId != 0)
Geoff Lang9aded172017-04-05 11:07:56 -0400959 {
960 mCachedStatus.reset();
961 }
Jamie Madill124f78c2019-06-18 11:48:24 -0400962 onStateChange(angle::SubjectMessage::DirtyBitsFlagged);
Geoff Lang9aded172017-04-05 11:07:56 -0400963}
964
Jamie Madillcc73f242018-08-01 11:34:48 -0400965GLenum Framebuffer::checkStatusImpl(const Context *context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966{
Jamie Madillcc73f242018-08-01 11:34:48 -0400967 ASSERT(!isDefault());
968 ASSERT(hasAnyDirtyBit() || !mCachedStatus.valid());
Geoff Lang528ce3c2014-12-01 10:44:07 -0500969
Jamie Madillcc73f242018-08-01 11:34:48 -0400970 mCachedStatus = checkStatusWithGLFrontEnd(context);
Jamie Madille98b1b52018-03-08 09:47:23 -0500971
Jamie Madillcc73f242018-08-01 11:34:48 -0400972 if (mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE)
973 {
Jamie Madill67220092019-05-20 11:12:53 -0400974 // We can skip syncState on several back-ends.
975 if (mImpl->shouldSyncStateBeforeCheckStatus())
Jamie Madille98b1b52018-03-08 09:47:23 -0500976 {
Jamie Madill67220092019-05-20 11:12:53 -0400977 angle::Result err = syncState(context);
978 if (err != angle::Result::Continue)
979 {
980 return 0;
981 }
Jamie Madillcc73f242018-08-01 11:34:48 -0400982 }
Jamie Madill67220092019-05-20 11:12:53 -0400983
Jamie Madillcc73f242018-08-01 11:34:48 -0400984 if (!mImpl->checkStatus(context))
985 {
986 mCachedStatus = GL_FRAMEBUFFER_UNSUPPORTED;
Jamie Madille98b1b52018-03-08 09:47:23 -0500987 }
Jamie Madill362876b2016-06-16 14:46:59 -0400988 }
989
Jamie Madill427064d2018-04-13 16:20:34 -0400990 return mCachedStatus.value();
Jamie Madill362876b2016-06-16 14:46:59 -0400991}
992
Jamie Madille98b1b52018-03-08 09:47:23 -0500993GLenum Framebuffer::checkStatusWithGLFrontEnd(const Context *context)
Jamie Madill362876b2016-06-16 14:46:59 -0400994{
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500995 const State &state = context->getState();
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400996
Jamie Madill2274b652018-05-31 10:56:08 -0400997 ASSERT(mState.mId != 0);
Jamie Madill362876b2016-06-16 14:46:59 -0400998
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400999 bool hasAttachments = false;
1000 Optional<unsigned int> colorbufferSize;
1001 Optional<int> samples;
Geoff Lang92019432017-11-20 13:09:34 -05001002 Optional<bool> fixedSampleLocations;
JiangYizhou461d9a32017-01-04 16:37:26 +08001003 bool hasRenderbuffer = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004
Martin Radev9bc9a322017-07-21 14:28:17 +03001005 const FramebufferAttachment *firstAttachment = getFirstNonNullAttachment();
1006
Jiawei Shaoa8802472018-05-28 11:17:47 +08001007 Optional<bool> isLayered;
1008 Optional<TextureType> colorAttachmentsTextureType;
1009
Jamie Madill48ef11b2016-04-27 15:21:52 -04001010 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011 {
Jamie Madill2d06b732015-04-20 12:53:28 -04001012 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001014 if (!CheckAttachmentCompleteness(context, colorAttachment))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001015 {
1016 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1017 }
daniel@transgaming.com01868132010-08-24 19:21:17 +00001018
Geoff Lang677bb6f2017-04-05 12:40:40 -04001019 const InternalFormat &format = *colorAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001020 if (format.depthBits > 0 || format.stencilBits > 0)
1021 {
1022 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1023 }
1024
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001025 if (!CheckAttachmentSampleCompleteness(context, colorAttachment, true, &samples,
1026 &fixedSampleLocations))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001027 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001028 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001029 }
1030
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001031 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
1032 // in GLES 3.0, there is no such restriction
1033 if (state.getClientMajorVersion() < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001034 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001035 if (colorbufferSize.valid())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001036 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001037 if (format.pixelBytes != colorbufferSize.value())
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +00001038 {
1039 return GL_FRAMEBUFFER_UNSUPPORTED;
1040 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001041 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001042 else
1043 {
1044 colorbufferSize = format.pixelBytes;
1045 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001046 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001047
Martin Radev9bc9a322017-07-21 14:28:17 +03001048 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &colorAttachment))
1049 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001050 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001051 }
1052
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001053 hasRenderbuffer = hasRenderbuffer || (colorAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001054
1055 if (!hasAttachments)
1056 {
1057 isLayered = colorAttachment.isLayered();
1058 if (isLayered.value())
1059 {
1060 colorAttachmentsTextureType = colorAttachment.getTextureImageIndex().getType();
1061 }
1062 hasAttachments = true;
1063 }
1064 else
1065 {
1066 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1067 // If any framebuffer attachment is layered, all populated attachments
1068 // must be layered. Additionally, all populated color attachments must
1069 // be from textures of the same target. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1070 ASSERT(isLayered.valid());
1071 if (isLayered.value() != colorAttachment.isLayered())
1072 {
1073 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1074 }
1075 else if (isLayered.value())
1076 {
1077 ASSERT(colorAttachmentsTextureType.valid());
1078 if (colorAttachmentsTextureType.value() !=
1079 colorAttachment.getTextureImageIndex().getType())
1080 {
1081 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1082 }
1083 }
1084 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001085 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001086 }
1087
Jamie Madill48ef11b2016-04-27 15:21:52 -04001088 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001089 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001090 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001091 if (!CheckAttachmentCompleteness(context, depthAttachment))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001093 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094 }
1095
Geoff Lang677bb6f2017-04-05 12:40:40 -04001096 const InternalFormat &format = *depthAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001097 if (format.depthBits == 0)
1098 {
1099 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001100 }
1101
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001102 if (!CheckAttachmentSampleCompleteness(context, depthAttachment, false, &samples,
1103 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001104 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001105 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001106 }
Sami Väisänena797e062016-05-12 15:23:40 +03001107
Martin Radev9bc9a322017-07-21 14:28:17 +03001108 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &depthAttachment))
1109 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001110 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001111 }
1112
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001113 hasRenderbuffer = hasRenderbuffer || (depthAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001114
1115 if (!hasAttachments)
1116 {
1117 isLayered = depthAttachment.isLayered();
1118 hasAttachments = true;
1119 }
1120 else
1121 {
1122 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1123 // If any framebuffer attachment is layered, all populated attachments
1124 // must be layered. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1125 ASSERT(isLayered.valid());
1126 if (isLayered.value() != depthAttachment.isLayered())
1127 {
1128 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1129 }
1130 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001131 }
1132
Jamie Madill48ef11b2016-04-27 15:21:52 -04001133 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001134 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001135 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001136 if (!CheckAttachmentCompleteness(context, stencilAttachment))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001137 {
1138 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1139 }
1140
Geoff Lang677bb6f2017-04-05 12:40:40 -04001141 const InternalFormat &format = *stencilAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001142 if (format.stencilBits == 0)
1143 {
1144 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001145 }
1146
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001147 if (!CheckAttachmentSampleCompleteness(context, stencilAttachment, false, &samples,
1148 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001149 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001150 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001151 }
Corentin Wallez086d59a2016-04-29 09:06:49 -04001152
Martin Radev9bc9a322017-07-21 14:28:17 +03001153 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &stencilAttachment))
1154 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001155 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001156 }
1157
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001158 hasRenderbuffer = hasRenderbuffer || (stencilAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001159
1160 if (!hasAttachments)
1161 {
1162 hasAttachments = true;
1163 }
1164 else
1165 {
1166 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1167 // If any framebuffer attachment is layered, all populated attachments
1168 // must be layered.
1169 // {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1170 ASSERT(isLayered.valid());
1171 if (isLayered.value() != stencilAttachment.isLayered())
1172 {
1173 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1174 }
1175 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001176 }
1177
1178 // Starting from ES 3.0 stencil and depth, if present, should be the same image
1179 if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() &&
1180 stencilAttachment.isAttached() && stencilAttachment != depthAttachment)
1181 {
1182 return GL_FRAMEBUFFER_UNSUPPORTED;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001183 }
1184
Jamie Madilla02315b2017-02-23 14:14:47 -05001185 // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL.
1186 if (state.isWebGL1())
1187 {
1188 if (!mState.mWebGLDepthStencilConsistent)
1189 {
1190 return GL_FRAMEBUFFER_UNSUPPORTED;
1191 }
1192
1193 if (mState.mWebGLDepthStencilAttachment.isAttached())
1194 {
1195 if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 ||
1196 mState.mWebGLDepthStencilAttachment.getStencilSize() == 0)
1197 {
1198 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1199 }
Martin Radev9bc9a322017-07-21 14:28:17 +03001200
1201 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment,
1202 &mState.mWebGLDepthStencilAttachment))
1203 {
Mingyu Hu7d64c482019-03-12 14:27:40 -07001204 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
Martin Radev9bc9a322017-07-21 14:28:17 +03001205 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001206 }
1207 else if (mState.mStencilAttachment.isAttached() &&
1208 mState.mStencilAttachment.getDepthSize() > 0)
1209 {
1210 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1211 }
1212 else if (mState.mDepthAttachment.isAttached() &&
1213 mState.mDepthAttachment.getStencilSize() > 0)
1214 {
1215 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1216 }
1217 }
1218
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001219 // ES3.1(section 9.4) requires that if no image is attached to the framebuffer, and either the
1220 // value of the framebuffer's FRAMEBUFFER_DEFAULT_WIDTH or FRAMEBUFFER_DEFAULT_HEIGHT parameters
1221 // is zero, the framebuffer is considered incomplete.
JiangYizhou461d9a32017-01-04 16:37:26 +08001222 GLint defaultWidth = mState.getDefaultWidth();
1223 GLint defaultHeight = mState.getDefaultHeight();
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001224 if (!hasAttachments && (defaultWidth == 0 || defaultHeight == 0))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001225 {
1226 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001227 }
1228
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001229 // In ES 2.0 and WebGL, all color attachments must have the same width and height.
Jamie Madillcc86d642015-11-24 13:00:07 -05001230 // In ES 3.0, there is no such restriction.
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001231 if ((state.getClientMajorVersion() < 3 || state.getExtensions().webglCompatibility) &&
1232 !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -05001233 {
1234 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
1235 }
1236
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001237 // ES3.1(section 9.4) requires that if the attached images are a mix of renderbuffers and
1238 // textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all attached textures.
JiangYizhou461d9a32017-01-04 16:37:26 +08001239 if (fixedSampleLocations.valid() && hasRenderbuffer && !fixedSampleLocations.value())
1240 {
1241 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1242 }
1243
Kenneth Russellce8602a2017-10-03 18:23:08 -07001244 // The WebGL conformance tests implicitly define that all framebuffer
1245 // attachments must be unique. For example, the same level of a texture can
1246 // not be attached to two different color attachments.
1247 if (state.getExtensions().webglCompatibility)
1248 {
1249 if (!mState.colorAttachmentsAreUniqueImages())
1250 {
1251 return GL_FRAMEBUFFER_UNSUPPORTED;
1252 }
1253 }
1254
Jamie Madillcc86d642015-11-24 13:00:07 -05001255 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001256}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001257
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001258angle::Result Framebuffer::discard(const Context *context, size_t count, const GLenum *attachments)
Austin Kinross08332632015-05-05 13:35:47 -07001259{
Jamie Madill05b35b22017-10-03 09:01:44 -04001260 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1261 // can be no-ops, so we should probably do that to ensure consistency.
1262 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1263
Jamie Madill4928b7c2017-06-20 12:57:39 -04001264 return mImpl->discard(context, count, attachments);
Austin Kinross08332632015-05-05 13:35:47 -07001265}
1266
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001267angle::Result Framebuffer::invalidate(const Context *context,
1268 size_t count,
1269 const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001270{
Jamie Madill05b35b22017-10-03 09:01:44 -04001271 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1272 // can be no-ops, so we should probably do that to ensure consistency.
1273 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1274
Jamie Madill4928b7c2017-06-20 12:57:39 -04001275 return mImpl->invalidate(context, count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001276}
1277
Jamie Madill05b35b22017-10-03 09:01:44 -04001278bool Framebuffer::partialClearNeedsInit(const Context *context,
1279 bool color,
1280 bool depth,
1281 bool stencil)
1282{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001283 const auto &glState = context->getState();
Jamie Madill05b35b22017-10-03 09:01:44 -04001284
1285 if (!glState.isRobustResourceInitEnabled())
1286 {
1287 return false;
1288 }
1289
1290 // Scissors can affect clearing.
1291 // TODO(jmadill): Check for complete scissor overlap.
1292 if (glState.isScissorTestEnabled())
1293 {
1294 return true;
1295 }
1296
1297 // If colors masked, we must clear before we clear. Do a simple check.
1298 // TODO(jmadill): Filter out unused color channels from the test.
1299 if (color)
1300 {
1301 const auto &blend = glState.getBlendState();
1302 if (!(blend.colorMaskRed && blend.colorMaskGreen && blend.colorMaskBlue &&
1303 blend.colorMaskAlpha))
1304 {
1305 return true;
1306 }
1307 }
1308
1309 const auto &depthStencil = glState.getDepthStencilState();
Yuly Novikov21edf3d2018-07-23 16:44:16 -04001310 if (stencil && (depthStencil.stencilMask != depthStencil.stencilWritemask ||
1311 depthStencil.stencilBackMask != depthStencil.stencilBackWritemask))
Jamie Madill05b35b22017-10-03 09:01:44 -04001312 {
1313 return true;
1314 }
1315
1316 return false;
1317}
1318
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001319angle::Result Framebuffer::invalidateSub(const Context *context,
1320 size_t count,
1321 const GLenum *attachments,
1322 const Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -04001323{
Jamie Madill05b35b22017-10-03 09:01:44 -04001324 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1325 // can be no-ops, so we should probably do that to ensure consistency.
1326 // TODO(jmadill): Make a invalidate no-op in WebGL 2.0.
1327
Jamie Madill4928b7c2017-06-20 12:57:39 -04001328 return mImpl->invalidateSub(context, count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -04001329}
1330
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001331angle::Result Framebuffer::clear(const Context *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -05001332{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001333 const auto &glState = context->getState();
Jamie Madill05b35b22017-10-03 09:01:44 -04001334 if (glState.isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -05001335 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001336 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001337 }
1338
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001339 // Remove clear bits that are ineffective. An effective clear changes at least one fragment. If
1340 // color/depth/stencil masks make the clear ineffective we skip it altogether.
1341
1342 // If all color channels are masked, don't attempt to clear color.
1343 if (context->getState().getBlendState().allChannelsMasked())
1344 {
1345 mask &= ~GL_COLOR_BUFFER_BIT;
1346 }
1347
1348 // If depth write is disabled, don't attempt to clear depth.
1349 if (!context->getState().getDepthStencilState().depthMask)
1350 {
1351 mask &= ~GL_DEPTH_BUFFER_BIT;
1352 }
1353
1354 // If all stencil bits are masked, don't attempt to clear stencil.
1355 if (context->getState().getDepthStencilState().stencilWritemask == 0)
1356 {
1357 mask &= ~GL_STENCIL_BUFFER_BIT;
1358 }
1359
1360 if (mask != 0)
1361 {
1362 ANGLE_TRY(mImpl->clear(context, mask));
1363 }
Jamie Madill05b35b22017-10-03 09:01:44 -04001364
Jamie Madill7c985f52018-11-29 18:16:17 -05001365 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001366}
1367
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001368angle::Result Framebuffer::clearBufferfv(const Context *context,
1369 GLenum buffer,
1370 GLint drawbuffer,
1371 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001372{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001373 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001374 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001375 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001376 }
1377
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001378 if (buffer == GL_DEPTH)
1379 {
1380 // If depth write is disabled, don't attempt to clear depth.
1381 if (!context->getState().getDepthStencilState().depthMask)
1382 {
1383 return angle::Result::Continue;
1384 }
1385 }
1386 else
1387 {
1388 // If all color channels are masked, don't attempt to clear color.
1389 if (context->getState().getBlendState().allChannelsMasked())
1390 {
1391 return angle::Result::Continue;
1392 }
1393 }
1394
Jamie Madill05b35b22017-10-03 09:01:44 -04001395 ANGLE_TRY(mImpl->clearBufferfv(context, buffer, drawbuffer, values));
1396
Jamie Madill7c985f52018-11-29 18:16:17 -05001397 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001398}
1399
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001400angle::Result Framebuffer::clearBufferuiv(const Context *context,
1401 GLenum buffer,
1402 GLint drawbuffer,
1403 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001404{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001405 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001406 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001407 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001408 }
1409
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001410 // If all color channels are masked, don't attempt to clear color.
1411 if (context->getState().getBlendState().allChannelsMasked())
1412 {
1413 return angle::Result::Continue;
1414 }
1415
Jamie Madill05b35b22017-10-03 09:01:44 -04001416 ANGLE_TRY(mImpl->clearBufferuiv(context, buffer, drawbuffer, values));
1417
Jamie Madill7c985f52018-11-29 18:16:17 -05001418 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001419}
1420
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001421angle::Result Framebuffer::clearBufferiv(const Context *context,
1422 GLenum buffer,
1423 GLint drawbuffer,
1424 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001425{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001426 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001427 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001428 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001429 }
1430
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001431 if (buffer == GL_STENCIL)
1432 {
1433 // If all stencil bits are masked, don't attempt to clear stencil.
1434 if (context->getState().getDepthStencilState().stencilWritemask == 0)
1435 {
1436 return angle::Result::Continue;
1437 }
1438 }
1439 else
1440 {
1441 // If all color channels are masked, don't attempt to clear color.
1442 if (context->getState().getBlendState().allChannelsMasked())
1443 {
1444 return angle::Result::Continue;
1445 }
1446 }
1447
Jamie Madill05b35b22017-10-03 09:01:44 -04001448 ANGLE_TRY(mImpl->clearBufferiv(context, buffer, drawbuffer, values));
1449
Jamie Madill7c985f52018-11-29 18:16:17 -05001450 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001451}
1452
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001453angle::Result Framebuffer::clearBufferfi(const Context *context,
1454 GLenum buffer,
1455 GLint drawbuffer,
1456 GLfloat depth,
1457 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -05001458{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001459 if (context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001460 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001461 return angle::Result::Continue;
Jamie Madill984ef412015-11-24 16:10:21 -05001462 }
1463
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001464 bool clearDepth = context->getState().getDepthStencilState().depthMask;
1465 bool clearStencil = context->getState().getDepthStencilState().stencilWritemask != 0;
1466
1467 if (clearDepth && clearStencil)
1468 {
1469 ASSERT(buffer == GL_DEPTH_STENCIL);
1470 ANGLE_TRY(mImpl->clearBufferfi(context, GL_DEPTH_STENCIL, drawbuffer, depth, stencil));
1471 }
1472 else if (clearDepth && !clearStencil)
1473 {
1474 ANGLE_TRY(mImpl->clearBufferfv(context, GL_DEPTH, drawbuffer, &depth));
1475 }
1476 else if (!clearDepth && clearStencil)
1477 {
1478 ANGLE_TRY(mImpl->clearBufferiv(context, GL_STENCIL, drawbuffer, &stencil));
1479 }
Jamie Madill05b35b22017-10-03 09:01:44 -04001480
Jamie Madill7c985f52018-11-29 18:16:17 -05001481 return angle::Result::Continue;
Geoff Langb04dc822014-12-01 12:02:02 -05001482}
1483
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001484angle::Result Framebuffer::getImplementationColorReadFormat(const Context *context,
1485 GLenum *formatOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001486{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001487 ANGLE_TRY(syncState(context));
1488 *formatOut = mImpl->getImplementationColorReadFormat(context);
Jamie Madill7c985f52018-11-29 18:16:17 -05001489 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001490}
1491
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001492angle::Result Framebuffer::getImplementationColorReadType(const Context *context, GLenum *typeOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001493{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001494 ANGLE_TRY(syncState(context));
1495 *typeOut = mImpl->getImplementationColorReadType(context);
Jamie Madill7c985f52018-11-29 18:16:17 -05001496 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001497}
1498
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001499angle::Result Framebuffer::readPixels(const Context *context,
1500 const Rectangle &area,
1501 GLenum format,
1502 GLenum type,
1503 void *pixels)
Geoff Langbce529e2014-12-01 12:48:41 -05001504{
Jamie Madill362876b2016-06-16 14:46:59 -04001505 ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels));
Geoff Lang520c4ae2015-05-05 13:12:36 -04001506
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001507 Buffer *unpackBuffer = context->getState().getTargetBuffer(BufferBinding::PixelUnpack);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001508 if (unpackBuffer)
1509 {
Jamie Madill124f78c2019-06-18 11:48:24 -04001510 unpackBuffer->onPixelPack();
Geoff Lang520c4ae2015-05-05 13:12:36 -04001511 }
1512
Jamie Madill7c985f52018-11-29 18:16:17 -05001513 return angle::Result::Continue;
Geoff Langbce529e2014-12-01 12:48:41 -05001514}
1515
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001516angle::Result Framebuffer::blit(const Context *context,
1517 const Rectangle &sourceArea,
1518 const Rectangle &destArea,
1519 GLbitfield mask,
1520 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -05001521{
He Yunchao6be602d2016-12-22 14:33:07 +08001522 GLbitfield blitMask = mask;
1523
1524 // Note that blitting is called against draw framebuffer.
1525 // See the code in gl::Context::blitFramebuffer.
1526 if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer())
1527 {
1528 blitMask &= ~GL_COLOR_BUFFER_BIT;
1529 }
1530
1531 if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr)
1532 {
1533 blitMask &= ~GL_STENCIL_BUFFER_BIT;
1534 }
1535
1536 if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr)
1537 {
1538 blitMask &= ~GL_DEPTH_BUFFER_BIT;
1539 }
1540
1541 if (!blitMask)
1542 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001543 return angle::Result::Continue;
He Yunchao6be602d2016-12-22 14:33:07 +08001544 }
1545
1546 return mImpl->blit(context, sourceArea, destArea, blitMask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -05001547}
1548
Luc Ferronbf6dc372018-06-28 15:24:19 -04001549bool Framebuffer::isDefault() const
1550{
1551 return id() == 0;
1552}
1553
Jamie Madill427064d2018-04-13 16:20:34 -04001554int Framebuffer::getSamples(const Context *context)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001555{
Jamie Madill427064d2018-04-13 16:20:34 -04001556 return (isComplete(context) ? getCachedSamples(context) : 0);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001557}
1558
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -04001559int Framebuffer::getCachedSamples(const Context *context) const
Jamie Madill9c335862017-07-18 11:51:38 -04001560{
Jamie Madill5b772312018-03-08 20:28:32 -05001561 ASSERT(mCachedStatus.valid() && mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE);
1562
Jamie Madill9c335862017-07-18 11:51:38 -04001563 // For a complete framebuffer, all attachments must have the same sample count.
1564 // In this case return the first nonzero sample size.
1565 const auto *firstNonNullAttachment = mState.getFirstNonNullAttachment();
1566 if (firstNonNullAttachment)
1567 {
1568 ASSERT(firstNonNullAttachment->isAttached());
1569 return firstNonNullAttachment->getSamples();
1570 }
1571
1572 // No attachments found.
1573 return 0;
1574}
1575
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001576angle::Result Framebuffer::getSamplePosition(const Context *context,
1577 size_t index,
1578 GLfloat *xy) const
Corentin Wallezccab69d2017-01-27 16:57:15 -05001579{
Geoff Lang13455072018-05-09 11:24:43 -04001580 ANGLE_TRY(mImpl->getSamplePosition(context, index, xy));
Jamie Madill7c985f52018-11-29 18:16:17 -05001581 return angle::Result::Continue;
Corentin Wallezccab69d2017-01-27 16:57:15 -05001582}
1583
Jamie Madille261b442014-06-25 12:42:21 -04001584bool Framebuffer::hasValidDepthStencil() const
1585{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001586 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -04001587}
1588
Jamie Madilla02315b2017-02-23 14:14:47 -05001589void Framebuffer::setAttachment(const Context *context,
1590 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -04001591 GLenum binding,
1592 const ImageIndex &textureIndex,
1593 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -04001594{
Martin Radev5dae57b2017-07-14 16:15:55 +03001595 setAttachment(context, type, binding, textureIndex, resource,
1596 FramebufferAttachment::kDefaultNumViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001597 FramebufferAttachment::kDefaultBaseViewIndex, false);
Martin Radev5dae57b2017-07-14 16:15:55 +03001598}
1599
1600void Framebuffer::setAttachment(const Context *context,
1601 GLenum type,
1602 GLenum binding,
1603 const ImageIndex &textureIndex,
1604 FramebufferAttachmentObject *resource,
1605 GLsizei numViews,
1606 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001607 bool isMultiview)
Martin Radev5dae57b2017-07-14 16:15:55 +03001608{
Jamie Madilla02315b2017-02-23 14:14:47 -05001609 // Context may be null in unit tests.
1610 if (!context || !context->isWebGL1())
1611 {
Martin Radev5dae57b2017-07-14 16:15:55 +03001612 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001613 isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001614 return;
1615 }
1616
1617 switch (binding)
1618 {
1619 case GL_DEPTH_STENCIL:
1620 case GL_DEPTH_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001621 mState.mWebGLDepthStencilAttachment.attach(context, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001622 resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001623 isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001624 break;
1625 case GL_DEPTH:
1626 case GL_DEPTH_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001627 mState.mWebGLDepthAttachment.attach(context, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001628 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001629 break;
1630 case GL_STENCIL:
1631 case GL_STENCIL_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001632 mState.mWebGLStencilAttachment.attach(context, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001633 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001634 break;
1635 default:
Martin Radev5dae57b2017-07-14 16:15:55 +03001636 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001637 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001638 return;
1639 }
1640
Mingyu Hu7d64c482019-03-12 14:27:40 -07001641 commitWebGL1DepthStencilIfConsistent(context, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001642}
1643
Mingyu Hu7d64c482019-03-12 14:27:40 -07001644void Framebuffer::setAttachmentMultiview(const Context *context,
1645 GLenum type,
1646 GLenum binding,
1647 const ImageIndex &textureIndex,
1648 FramebufferAttachmentObject *resource,
1649 GLsizei numViews,
1650 GLint baseViewIndex)
Martin Radev82ef7742017-08-08 17:44:58 +03001651{
Mingyu Hu7d64c482019-03-12 14:27:40 -07001652 setAttachment(context, type, binding, textureIndex, resource, numViews, baseViewIndex, true);
Martin Radev5dae57b2017-07-14 16:15:55 +03001653}
1654
1655void Framebuffer::commitWebGL1DepthStencilIfConsistent(const Context *context,
1656 GLsizei numViews,
1657 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001658 bool isMultiview)
Jamie Madilla02315b2017-02-23 14:14:47 -05001659{
1660 int count = 0;
1661
1662 std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment,
1663 &mState.mWebGLDepthAttachment,
1664 &mState.mWebGLStencilAttachment}};
1665 for (FramebufferAttachment *attachment : attachments)
1666 {
1667 if (attachment->isAttached())
1668 {
1669 count++;
1670 }
1671 }
1672
1673 mState.mWebGLDepthStencilConsistent = (count <= 1);
1674 if (!mState.mWebGLDepthStencilConsistent)
1675 {
1676 // Inconsistent.
1677 return;
1678 }
1679
Geoff Lange466c552017-03-17 15:24:12 -04001680 auto getImageIndexIfTextureAttachment = [](const FramebufferAttachment &attachment) {
1681 if (attachment.type() == GL_TEXTURE)
1682 {
1683 return attachment.getTextureImageIndex();
1684 }
1685 else
1686 {
Jamie Madillcc129372018-04-12 09:13:18 -04001687 return ImageIndex();
Geoff Lange466c552017-03-17 15:24:12 -04001688 }
1689 };
1690
Jamie Madilla02315b2017-02-23 14:14:47 -05001691 if (mState.mWebGLDepthAttachment.isAttached())
1692 {
1693 const auto &depth = mState.mWebGLDepthAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001694 setAttachmentImpl(context, depth.type(), GL_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001695 getImageIndexIfTextureAttachment(depth), depth.getResource(), numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001696 baseViewIndex, isMultiview);
Jamie Madillcc129372018-04-12 09:13:18 -04001697 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001698 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001699 }
1700 else if (mState.mWebGLStencilAttachment.isAttached())
1701 {
1702 const auto &stencil = mState.mWebGLStencilAttachment;
Jamie Madillcc129372018-04-12 09:13:18 -04001703 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001704 baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001705 setAttachmentImpl(context, stencil.type(), GL_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001706 getImageIndexIfTextureAttachment(stencil), stencil.getResource(),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001707 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001708 }
1709 else if (mState.mWebGLDepthStencilAttachment.isAttached())
1710 {
1711 const auto &depthStencil = mState.mWebGLDepthStencilAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001712 setAttachmentImpl(context, depthStencil.type(), GL_DEPTH_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001713 getImageIndexIfTextureAttachment(depthStencil),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001714 depthStencil.getResource(), numViews, baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001715 setAttachmentImpl(context, depthStencil.type(), GL_STENCIL_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001716 getImageIndexIfTextureAttachment(depthStencil),
Mingyu Hu7d64c482019-03-12 14:27:40 -07001717 depthStencil.getResource(), numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001718 }
1719 else
1720 {
Jamie Madillcc129372018-04-12 09:13:18 -04001721 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001722 baseViewIndex, isMultiview);
Jamie Madillcc129372018-04-12 09:13:18 -04001723 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001724 baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001725 }
1726}
1727
Jamie Madill4928b7c2017-06-20 12:57:39 -04001728void Framebuffer::setAttachmentImpl(const Context *context,
1729 GLenum type,
Jamie Madilla02315b2017-02-23 14:14:47 -05001730 GLenum binding,
1731 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001732 FramebufferAttachmentObject *resource,
1733 GLsizei numViews,
1734 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001735 bool isMultiview)
Jamie Madilla02315b2017-02-23 14:14:47 -05001736{
Jamie Madilla02315b2017-02-23 14:14:47 -05001737 switch (binding)
1738 {
Jamie Madillb8126692017-04-05 11:22:17 -04001739 case GL_DEPTH_STENCIL:
1740 case GL_DEPTH_STENCIL_ATTACHMENT:
1741 {
1742 // ensure this is a legitimate depth+stencil format
1743 FramebufferAttachmentObject *attachmentObj = resource;
1744 if (resource)
1745 {
Jamie Madill4fd95d52017-04-05 11:22:18 -04001746 const Format &format = resource->getAttachmentFormat(binding, textureIndex);
Jamie Madillb8126692017-04-05 11:22:17 -04001747 if (format.info->depthBits == 0 || format.info->stencilBits == 0)
1748 {
1749 // Attaching nullptr detaches the current attachment.
1750 attachmentObj = nullptr;
1751 }
1752 }
1753
Jamie Madill4928b7c2017-06-20 12:57:39 -04001754 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001755 &mDirtyDepthAttachmentBinding, type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001756 attachmentObj, numViews, baseViewIndex, isMultiview);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001757 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001758 &mDirtyStencilAttachmentBinding, type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001759 attachmentObj, numViews, baseViewIndex, isMultiview);
Jamie Madill42975642017-10-12 12:31:51 -04001760 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001761 }
1762
Jamie Madilla02315b2017-02-23 14:14:47 -05001763 case GL_DEPTH:
1764 case GL_DEPTH_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001765 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001766 &mDirtyDepthAttachmentBinding, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001767 numViews, baseViewIndex, isMultiview);
Jamie Madill2d06b732015-04-20 12:53:28 -04001768 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001769
Jamie Madilla02315b2017-02-23 14:14:47 -05001770 case GL_STENCIL:
1771 case GL_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001772 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001773 &mDirtyStencilAttachmentBinding, type, binding, textureIndex, resource,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001774 numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001775 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001776
Jamie Madilla02315b2017-02-23 14:14:47 -05001777 case GL_BACK:
Geoff Lang8170eab2017-09-21 13:59:04 -04001778 updateAttachment(context, &mState.mColorAttachments[0], DIRTY_BIT_COLOR_ATTACHMENT_0,
1779 &mDirtyColorAttachmentBindings[0], type, binding, textureIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001780 resource, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001781 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001782
Jamie Madilla02315b2017-02-23 14:14:47 -05001783 default:
1784 {
1785 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
1786 ASSERT(colorIndex < mState.mColorAttachments.size());
Jamie Madillb8126692017-04-05 11:22:17 -04001787 size_t dirtyBit = DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001788 updateAttachment(context, &mState.mColorAttachments[colorIndex], dirtyBit,
Jamie Madillb8126692017-04-05 11:22:17 -04001789 &mDirtyColorAttachmentBindings[colorIndex], type, binding,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001790 textureIndex, resource, numViews, baseViewIndex, isMultiview);
Jamie Madilla02315b2017-02-23 14:14:47 -05001791
shrekshaoc87e0052019-02-21 11:40:28 -08001792 if (!resource)
1793 {
shrekshao8413fab2019-04-04 17:13:18 -07001794 mColorAttachmentBits.reset(colorIndex);
shrekshaoc87e0052019-02-21 11:40:28 -08001795 mFloat32ColorAttachmentBits.reset(colorIndex);
1796 }
1797 else
1798 {
shrekshao8413fab2019-04-04 17:13:18 -07001799 mColorAttachmentBits.set(colorIndex);
shrekshaoc87e0052019-02-21 11:40:28 -08001800 updateFloat32ColorAttachmentBits(
1801 colorIndex, resource->getAttachmentFormat(binding, textureIndex).info);
1802 }
1803
Corentin Walleze7557742017-06-01 13:09:57 -04001804 // TODO(jmadill): ASSERT instead of checking the attachment exists in
1805 // formsRenderingFeedbackLoopWith
Jamie Madilla02315b2017-02-23 14:14:47 -05001806 bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE);
1807 mState.mEnabledDrawBuffers.set(colorIndex, enabled);
Jamie Madill6e18a232019-01-16 13:27:14 -05001808 SetComponentTypeMask(getDrawbufferWriteType(colorIndex), colorIndex,
1809 &mState.mDrawBufferTypeMask);
Jamie Madill2d06b732015-04-20 12:53:28 -04001810 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001811 break;
Geoff Langab75a052014-10-15 12:56:37 -04001812 }
1813}
1814
Jamie Madill4928b7c2017-06-20 12:57:39 -04001815void Framebuffer::updateAttachment(const Context *context,
1816 FramebufferAttachment *attachment,
Jamie Madillb8126692017-04-05 11:22:17 -04001817 size_t dirtyBit,
Jamie Madilld4442552018-02-27 22:03:47 -05001818 angle::ObserverBinding *onDirtyBinding,
Jamie Madillb8126692017-04-05 11:22:17 -04001819 GLenum type,
1820 GLenum binding,
1821 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001822 FramebufferAttachmentObject *resource,
1823 GLsizei numViews,
1824 GLuint baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001825 bool isMultiview)
Jamie Madillb8126692017-04-05 11:22:17 -04001826{
Martin Radev5dae57b2017-07-14 16:15:55 +03001827 attachment->attach(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
Mingyu Hu7d64c482019-03-12 14:27:40 -07001828 isMultiview);
Jamie Madillb8126692017-04-05 11:22:17 -04001829 mDirtyBits.set(dirtyBit);
Jamie Madill05b35b22017-10-03 09:01:44 -04001830 mState.mResourceNeedsInit.set(dirtyBit, attachment->initState() == InitState::MayNeedInit);
Jamie Madill66f0d2c2018-11-30 15:25:36 -05001831 onDirtyBinding->bind(resource);
Jamie Madille98b1b52018-03-08 09:47:23 -05001832
Jamie Madill124f78c2019-06-18 11:48:24 -04001833 invalidateCompletenessCache();
Jamie Madillb8126692017-04-05 11:22:17 -04001834}
1835
Jamie Madilla02315b2017-02-23 14:14:47 -05001836void Framebuffer::resetAttachment(const Context *context, GLenum binding)
Jamie Madill2d06b732015-04-20 12:53:28 -04001837{
Jamie Madillcc129372018-04-12 09:13:18 -04001838 setAttachment(context, GL_NONE, binding, ImageIndex(), nullptr);
Jamie Madill2d06b732015-04-20 12:53:28 -04001839}
1840
Jamie Madill6f755b22018-10-09 12:48:54 -04001841angle::Result Framebuffer::syncState(const Context *context)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001842{
1843 if (mDirtyBits.any())
1844 {
Jamie Madill888081d2018-02-27 00:24:46 -05001845 mDirtyBitsGuard = mDirtyBits;
Jamie Madill19fa1c62018-03-08 09:47:21 -05001846 ANGLE_TRY(mImpl->syncState(context, mDirtyBits));
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001847 mDirtyBits.reset();
Jamie Madill888081d2018-02-27 00:24:46 -05001848 mDirtyBitsGuard.reset();
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001849 }
Jamie Madill7c985f52018-11-29 18:16:17 -05001850 return angle::Result::Continue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001851}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001852
Jamie Madill124f78c2019-06-18 11:48:24 -04001853void Framebuffer::onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001854{
Jamie Madille4faae22019-05-10 08:27:00 -04001855 if (message != angle::SubjectMessage::SubjectChanged)
Jamie Madillf668a4b2018-09-23 17:01:20 -04001856 {
Jamie Madill67220092019-05-20 11:12:53 -04001857 // This can be triggered by SubImage calls for Textures.
1858 if (message == angle::SubjectMessage::ContentsChanged)
1859 {
1860 mDirtyBits.set(DIRTY_BIT_COLOR_BUFFER_CONTENTS_0 + index);
Jamie Madill124f78c2019-06-18 11:48:24 -04001861 onStateChange(angle::SubjectMessage::DirtyBitsFlagged);
Jamie Madill67220092019-05-20 11:12:53 -04001862 return;
1863 }
1864
Jamie Madillf668a4b2018-09-23 17:01:20 -04001865 // This can be triggered by the GL back-end TextureGL class.
Jamie Madille4faae22019-05-10 08:27:00 -04001866 ASSERT(message == angle::SubjectMessage::DirtyBitsFlagged);
Jamie Madillf668a4b2018-09-23 17:01:20 -04001867 return;
1868 }
Jamie Madill55e57f92018-09-18 11:32:43 -04001869
1870 ASSERT(!mDirtyBitsGuard.valid() || mDirtyBitsGuard.value().test(index));
1871 mDirtyBits.set(index);
Jamie Madill888081d2018-02-27 00:24:46 -05001872
Jamie Madill124f78c2019-06-18 11:48:24 -04001873 invalidateCompletenessCache();
Jamie Madill05b35b22017-10-03 09:01:44 -04001874
Jamie Madilld4442552018-02-27 22:03:47 -05001875 FramebufferAttachment *attachment = getAttachmentFromSubjectIndex(index);
1876
Jamie Madill05b35b22017-10-03 09:01:44 -04001877 // Mark the appropriate init flag.
Jamie Madilld4442552018-02-27 22:03:47 -05001878 mState.mResourceNeedsInit.set(index, attachment->initState() == InitState::MayNeedInit);
shrekshaoc87e0052019-02-21 11:40:28 -08001879
1880 // Update mFloat32ColorAttachmentBits Cache
1881 if (index < DIRTY_BIT_COLOR_ATTACHMENT_MAX)
1882 {
1883 ASSERT(index != DIRTY_BIT_DEPTH_ATTACHMENT);
1884 ASSERT(index != DIRTY_BIT_STENCIL_ATTACHMENT);
1885 updateFloat32ColorAttachmentBits(index - DIRTY_BIT_COLOR_ATTACHMENT_0,
1886 attachment->getFormat().info);
1887 }
Jamie Madilld4442552018-02-27 22:03:47 -05001888}
1889
1890FramebufferAttachment *Framebuffer::getAttachmentFromSubjectIndex(angle::SubjectIndex index)
1891{
1892 switch (index)
1893 {
1894 case DIRTY_BIT_DEPTH_ATTACHMENT:
1895 return &mState.mDepthAttachment;
1896 case DIRTY_BIT_STENCIL_ATTACHMENT:
1897 return &mState.mStencilAttachment;
1898 default:
1899 size_t colorIndex = (index - DIRTY_BIT_COLOR_ATTACHMENT_0);
1900 ASSERT(colorIndex < mState.mColorAttachments.size());
1901 return &mState.mColorAttachments[colorIndex];
1902 }
Jamie Madill51f40ec2016-06-15 14:06:00 -04001903}
1904
shrekshao8413fab2019-04-04 17:13:18 -07001905bool Framebuffer::formsRenderingFeedbackLoopWith(const Context *context) const
Jamie Madilla4595b82017-01-11 17:36:34 -05001906{
shrekshao8413fab2019-04-04 17:13:18 -07001907 const State &state = context->getState();
Jamie Madill785e8a02018-10-04 17:42:00 -04001908 const Program *program = state.getProgram();
Jamie Madilla4595b82017-01-11 17:36:34 -05001909
1910 // TODO(jmadill): Default framebuffer feedback loops.
Jamie Madill2274b652018-05-31 10:56:08 -04001911 if (mState.mId == 0)
Jamie Madilla4595b82017-01-11 17:36:34 -05001912 {
1913 return false;
1914 }
1915
shrekshao8413fab2019-04-04 17:13:18 -07001916 const FramebufferAttachment *depth = getDepthbuffer();
1917 const FramebufferAttachment *stencil = getStencilbuffer();
1918
1919 const bool checkDepth = depth && depth->type() == GL_TEXTURE;
1920 // Skip the feedback loop check for stencil if depth/stencil point to the same resource.
1921 const bool checkStencil =
1922 (stencil && stencil->type() == GL_TEXTURE) && (!depth || *stencil != *depth);
1923
1924 const gl::ActiveTextureMask &activeTextures = program->getActiveSamplersMask();
1925 const gl::ActiveTexturePointerArray &textures = state.getActiveTexturesCache();
1926
1927 for (size_t textureUnit : activeTextures)
Jamie Madilla4595b82017-01-11 17:36:34 -05001928 {
shrekshao8413fab2019-04-04 17:13:18 -07001929 Texture *texture = textures[textureUnit];
1930
1931 if (texture == nullptr)
Jamie Madilla4595b82017-01-11 17:36:34 -05001932 {
shrekshao8413fab2019-04-04 17:13:18 -07001933 continue;
1934 }
1935
1936 // Depth and stencil attachment form feedback loops
1937 // Regardless of if enabled or masked.
1938 if (checkDepth)
1939 {
1940 if (texture->id() == depth->id())
Jamie Madilla4595b82017-01-11 17:36:34 -05001941 {
1942 return true;
1943 }
1944 }
Jamie Madilla4595b82017-01-11 17:36:34 -05001945
shrekshao8413fab2019-04-04 17:13:18 -07001946 if (checkStencil)
Jamie Madill1d37bc52017-02-02 19:59:58 -05001947 {
shrekshao8413fab2019-04-04 17:13:18 -07001948 if (texture->id() == stencil->id())
Jamie Madill1d37bc52017-02-02 19:59:58 -05001949 {
Jamie Madill38fe6842018-09-19 07:20:00 -04001950 return true;
Jamie Madill1d37bc52017-02-02 19:59:58 -05001951 }
1952 }
shrekshao8413fab2019-04-04 17:13:18 -07001953
1954 // Check if any color attachment forms a feedback loop.
1955 for (size_t drawIndex : mColorAttachmentBits)
1956 {
1957 const FramebufferAttachment &attachment = mState.mColorAttachments[drawIndex];
1958 ASSERT(attachment.isAttached());
1959
1960 if (attachment.isTextureWithId(texture->id()))
1961 {
1962 // TODO(jmadill): Check for appropriate overlap.
1963 return true;
1964 }
1965 }
Jamie Madill1d37bc52017-02-02 19:59:58 -05001966 }
1967
Jamie Madilla4595b82017-01-11 17:36:34 -05001968 return false;
1969}
1970
Jamie Madillfd3dd432017-02-02 19:59:59 -05001971bool Framebuffer::formsCopyingFeedbackLoopWith(GLuint copyTextureID,
1972 GLint copyTextureLevel,
1973 GLint copyTextureLayer) const
Jamie Madillf695a3a2017-01-11 17:36:35 -05001974{
Jamie Madill2274b652018-05-31 10:56:08 -04001975 if (mState.mId == 0)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001976 {
1977 // It seems impossible to form a texture copying feedback loop with the default FBO.
1978 return false;
1979 }
1980
1981 const FramebufferAttachment *readAttachment = getReadColorbuffer();
1982 ASSERT(readAttachment);
1983
1984 if (readAttachment->isTextureWithId(copyTextureID))
1985 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001986 const auto &imageIndex = readAttachment->getTextureImageIndex();
Jamie Madillcc129372018-04-12 09:13:18 -04001987 if (imageIndex.getLevelIndex() == copyTextureLevel)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001988 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001989 // Check 3D/Array texture layers.
Jamie Madillcc129372018-04-12 09:13:18 -04001990 return !imageIndex.hasLayer() || copyTextureLayer == ImageIndex::kEntireLevel ||
1991 imageIndex.getLayerIndex() == copyTextureLayer;
Jamie Madillf695a3a2017-01-11 17:36:35 -05001992 }
1993 }
1994 return false;
1995}
1996
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001997GLint Framebuffer::getDefaultWidth() const
1998{
1999 return mState.getDefaultWidth();
2000}
2001
2002GLint Framebuffer::getDefaultHeight() const
2003{
2004 return mState.getDefaultHeight();
2005}
2006
2007GLint Framebuffer::getDefaultSamples() const
2008{
2009 return mState.getDefaultSamples();
2010}
2011
Geoff Lang92019432017-11-20 13:09:34 -05002012bool Framebuffer::getDefaultFixedSampleLocations() const
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002013{
2014 return mState.getDefaultFixedSampleLocations();
2015}
2016
Jiawei Shaob1e91382018-05-17 14:33:55 +08002017GLint Framebuffer::getDefaultLayers() const
2018{
2019 return mState.getDefaultLayers();
2020}
2021
Jamie Madillb983a4b2018-08-01 11:34:51 -04002022void Framebuffer::setDefaultWidth(const Context *context, GLint defaultWidth)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002023{
2024 mState.mDefaultWidth = defaultWidth;
2025 mDirtyBits.set(DIRTY_BIT_DEFAULT_WIDTH);
Jamie Madill124f78c2019-06-18 11:48:24 -04002026 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002027}
2028
Jamie Madillb983a4b2018-08-01 11:34:51 -04002029void Framebuffer::setDefaultHeight(const Context *context, GLint defaultHeight)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002030{
2031 mState.mDefaultHeight = defaultHeight;
2032 mDirtyBits.set(DIRTY_BIT_DEFAULT_HEIGHT);
Jamie Madill124f78c2019-06-18 11:48:24 -04002033 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002034}
2035
Jamie Madillb983a4b2018-08-01 11:34:51 -04002036void Framebuffer::setDefaultSamples(const Context *context, GLint defaultSamples)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002037{
2038 mState.mDefaultSamples = defaultSamples;
2039 mDirtyBits.set(DIRTY_BIT_DEFAULT_SAMPLES);
Jamie Madill124f78c2019-06-18 11:48:24 -04002040 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002041}
2042
Jamie Madillb983a4b2018-08-01 11:34:51 -04002043void Framebuffer::setDefaultFixedSampleLocations(const Context *context,
2044 bool defaultFixedSampleLocations)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002045{
2046 mState.mDefaultFixedSampleLocations = defaultFixedSampleLocations;
2047 mDirtyBits.set(DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS);
Jamie Madill124f78c2019-06-18 11:48:24 -04002048 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002049}
2050
Jiawei Shaob1e91382018-05-17 14:33:55 +08002051void Framebuffer::setDefaultLayers(GLint defaultLayers)
2052{
2053 mState.mDefaultLayers = defaultLayers;
2054 mDirtyBits.set(DIRTY_BIT_DEFAULT_LAYERS);
2055}
2056
Martin Radev14a26ae2017-07-24 15:56:29 +03002057GLsizei Framebuffer::getNumViews() const
2058{
Martin Radev5c00d0d2017-08-07 10:06:59 +03002059 return mState.getNumViews();
Martin Radev14a26ae2017-07-24 15:56:29 +03002060}
2061
Martin Radev4e619f52017-08-09 11:50:06 +03002062GLint Framebuffer::getBaseViewIndex() const
2063{
2064 return mState.getBaseViewIndex();
2065}
2066
Mingyu Hu7d64c482019-03-12 14:27:40 -07002067bool Framebuffer::isMultiview() const
Martin Radev878c8b12017-07-28 09:51:04 +03002068{
Mingyu Hu7d64c482019-03-12 14:27:40 -07002069 return mState.isMultiview();
Martin Radev878c8b12017-07-28 09:51:04 +03002070}
2071
Olli Etuaho8acb1b62018-07-30 16:20:54 +03002072bool Framebuffer::readDisallowedByMultiview() const
2073{
Mingyu Hu7d64c482019-03-12 14:27:40 -07002074 return (mState.isMultiview() && mState.getNumViews() > 1);
Olli Etuaho8acb1b62018-07-30 16:20:54 +03002075}
2076
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002077angle::Result Framebuffer::ensureClearAttachmentsInitialized(const Context *context,
2078 GLbitfield mask)
Geoff Langd4fff502017-09-22 11:28:28 -04002079{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05002080 const auto &glState = context->getState();
Geoff Langd4fff502017-09-22 11:28:28 -04002081 if (!context->isRobustResourceInitEnabled() || glState.isRasterizerDiscardEnabled())
2082 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002083 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002084 }
2085
Geoff Langa36483f2018-03-09 16:11:21 -05002086 const BlendState &blend = glState.getBlendState();
2087 const DepthStencilState &depthStencil = glState.getDepthStencilState();
Geoff Langd4fff502017-09-22 11:28:28 -04002088
2089 bool color = (mask & GL_COLOR_BUFFER_BIT) != 0 && !IsColorMaskedOut(blend);
2090 bool depth = (mask & GL_DEPTH_BUFFER_BIT) != 0 && !IsDepthMaskedOut(depthStencil);
2091 bool stencil = (mask & GL_STENCIL_BUFFER_BIT) != 0 && !IsStencilMaskedOut(depthStencil);
2092
2093 if (!color && !depth && !stencil)
2094 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002095 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002096 }
2097
2098 if (partialClearNeedsInit(context, color, depth, stencil))
2099 {
2100 ANGLE_TRY(ensureDrawAttachmentsInitialized(context));
2101 }
2102
2103 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2104 // still be marked initialized. This simplifies design, allowing this method to be called before
2105 // the clear.
2106 markDrawAttachmentsInitialized(color, depth, stencil);
2107
Jamie Madill7c985f52018-11-29 18:16:17 -05002108 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002109}
2110
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002111angle::Result Framebuffer::ensureClearBufferAttachmentsInitialized(const Context *context,
2112 GLenum buffer,
2113 GLint drawbuffer)
Geoff Langd4fff502017-09-22 11:28:28 -04002114{
2115 if (!context->isRobustResourceInitEnabled() ||
Jamie Madillc3dc5d42018-12-30 12:12:04 -05002116 context->getState().isRasterizerDiscardEnabled() || IsClearBufferMaskedOut(context, buffer))
Geoff Langd4fff502017-09-22 11:28:28 -04002117 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002118 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002119 }
2120
2121 if (partialBufferClearNeedsInit(context, buffer))
2122 {
2123 ANGLE_TRY(ensureBufferInitialized(context, buffer, drawbuffer));
2124 }
2125
2126 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2127 // still be marked initialized. This simplifies design, allowing this method to be called before
2128 // the clear.
2129 markBufferInitialized(buffer, drawbuffer);
2130
Jamie Madill7c985f52018-11-29 18:16:17 -05002131 return angle::Result::Continue;
Geoff Langd4fff502017-09-22 11:28:28 -04002132}
2133
Jamie Madill6f755b22018-10-09 12:48:54 -04002134angle::Result Framebuffer::ensureDrawAttachmentsInitialized(const Context *context)
Jamie Madill05b35b22017-10-03 09:01:44 -04002135{
2136 if (!context->isRobustResourceInitEnabled())
2137 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002138 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002139 }
2140
2141 // Note: we don't actually filter by the draw attachment enum. Just init everything.
2142 for (size_t bit : mState.mResourceNeedsInit)
2143 {
2144 switch (bit)
2145 {
2146 case DIRTY_BIT_DEPTH_ATTACHMENT:
2147 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2148 break;
2149 case DIRTY_BIT_STENCIL_ATTACHMENT:
2150 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2151 break;
2152 default:
2153 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bit]));
2154 break;
2155 }
2156 }
2157
2158 mState.mResourceNeedsInit.reset();
Jamie Madill7c985f52018-11-29 18:16:17 -05002159 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002160}
2161
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002162angle::Result Framebuffer::ensureReadAttachmentsInitialized(const Context *context)
Jamie Madill05b35b22017-10-03 09:01:44 -04002163{
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002164 ASSERT(context->isRobustResourceInitEnabled());
2165
2166 if (mState.mResourceNeedsInit.none())
Jamie Madill05b35b22017-10-03 09:01:44 -04002167 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002168 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002169 }
2170
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002171 if (mState.mReadBufferState != GL_NONE)
Jamie Madill05b35b22017-10-03 09:01:44 -04002172 {
2173 size_t readIndex = mState.getReadIndex();
2174 if (mState.mResourceNeedsInit[readIndex])
2175 {
2176 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[readIndex]));
2177 mState.mResourceNeedsInit.reset(readIndex);
2178 }
2179 }
2180
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002181 // Conservatively init depth since it can be read by BlitFramebuffer.
2182 if (hasDepth())
Jamie Madill05b35b22017-10-03 09:01:44 -04002183 {
2184 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2185 {
2186 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2187 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2188 }
2189 }
2190
Jamie Madill0a1eeb82019-05-13 13:53:18 -04002191 // Conservatively init stencil since it can be read by BlitFramebuffer.
2192 if (hasStencil())
Jamie Madill05b35b22017-10-03 09:01:44 -04002193 {
2194 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2195 {
2196 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2197 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2198 }
2199 }
2200
Jamie Madill7c985f52018-11-29 18:16:17 -05002201 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002202}
2203
2204void Framebuffer::markDrawAttachmentsInitialized(bool color, bool depth, bool stencil)
2205{
2206 // Mark attachments as initialized.
2207 if (color)
2208 {
2209 for (auto colorIndex : mState.mEnabledDrawBuffers)
2210 {
2211 auto &colorAttachment = mState.mColorAttachments[colorIndex];
2212 ASSERT(colorAttachment.isAttached());
2213 colorAttachment.setInitState(InitState::Initialized);
2214 mState.mResourceNeedsInit.reset(colorIndex);
2215 }
2216 }
2217
2218 if (depth && mState.mDepthAttachment.isAttached())
2219 {
2220 mState.mDepthAttachment.setInitState(InitState::Initialized);
2221 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2222 }
2223
2224 if (stencil && mState.mStencilAttachment.isAttached())
2225 {
2226 mState.mStencilAttachment.setInitState(InitState::Initialized);
2227 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2228 }
2229}
2230
2231void Framebuffer::markBufferInitialized(GLenum bufferType, GLint bufferIndex)
2232{
2233 switch (bufferType)
2234 {
2235 case GL_COLOR:
2236 {
2237 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2238 if (mState.mColorAttachments[bufferIndex].isAttached())
2239 {
2240 mState.mColorAttachments[bufferIndex].setInitState(InitState::Initialized);
2241 mState.mResourceNeedsInit.reset(bufferIndex);
2242 }
2243 break;
2244 }
2245 case GL_DEPTH:
2246 {
2247 if (mState.mDepthAttachment.isAttached())
2248 {
2249 mState.mDepthAttachment.setInitState(InitState::Initialized);
2250 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2251 }
2252 break;
2253 }
2254 case GL_STENCIL:
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 case GL_DEPTH_STENCIL:
2264 {
2265 if (mState.mDepthAttachment.isAttached())
2266 {
2267 mState.mDepthAttachment.setInitState(InitState::Initialized);
2268 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2269 }
2270 if (mState.mStencilAttachment.isAttached())
2271 {
2272 mState.mStencilAttachment.setInitState(InitState::Initialized);
2273 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2274 }
2275 break;
2276 }
2277 default:
2278 UNREACHABLE();
2279 break;
2280 }
2281}
2282
2283Box Framebuffer::getDimensions() const
2284{
2285 return mState.getDimensions();
2286}
2287
Jamie Madill64b7c4f2018-10-19 11:38:04 -04002288angle::Result Framebuffer::ensureBufferInitialized(const Context *context,
2289 GLenum bufferType,
2290 GLint bufferIndex)
Jamie Madill05b35b22017-10-03 09:01:44 -04002291{
2292 ASSERT(context->isRobustResourceInitEnabled());
2293
2294 if (mState.mResourceNeedsInit.none())
2295 {
Jamie Madill7c985f52018-11-29 18:16:17 -05002296 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002297 }
2298
2299 switch (bufferType)
2300 {
2301 case GL_COLOR:
2302 {
2303 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2304 if (mState.mResourceNeedsInit[bufferIndex])
2305 {
2306 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bufferIndex]));
2307 mState.mResourceNeedsInit.reset(bufferIndex);
2308 }
2309 break;
2310 }
2311 case GL_DEPTH:
2312 {
2313 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2314 {
2315 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2316 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2317 }
2318 break;
2319 }
2320 case GL_STENCIL:
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 case GL_DEPTH_STENCIL:
2330 {
2331 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2332 {
2333 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2334 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2335 }
2336 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2337 {
2338 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2339 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2340 }
2341 break;
2342 }
2343 default:
2344 UNREACHABLE();
2345 break;
2346 }
2347
Jamie Madill7c985f52018-11-29 18:16:17 -05002348 return angle::Result::Continue;
Jamie Madill05b35b22017-10-03 09:01:44 -04002349}
2350
2351bool Framebuffer::partialBufferClearNeedsInit(const Context *context, GLenum bufferType)
2352{
2353 if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none())
2354 {
2355 return false;
2356 }
2357
2358 switch (bufferType)
2359 {
2360 case GL_COLOR:
2361 return partialClearNeedsInit(context, true, false, false);
2362 case GL_DEPTH:
2363 return partialClearNeedsInit(context, false, true, false);
2364 case GL_STENCIL:
2365 return partialClearNeedsInit(context, false, false, true);
2366 case GL_DEPTH_STENCIL:
2367 return partialClearNeedsInit(context, false, true, true);
2368 default:
2369 UNREACHABLE();
2370 return false;
2371 }
2372}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002373} // namespace gl