blob: b577d22691ade5dc5816a461cd5c94893ecfa73e [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Langcec35902014-04-16 10:52:36 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Framebuffer.cpp: Implements the gl::Framebuffer class. Implements GL framebuffer
8// objects and related functionality. [OpenGL ES 2.0.24] section 4.4 page 105.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Framebuffer.h"
Jamie Madillc46f45d2015-03-31 13:20:55 -040011
Jamie Madillcc86d642015-11-24 13:00:07 -050012#include "common/Optional.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040013#include "common/bitset_utils.h"
Jamie Madillc46f45d2015-03-31 13:20:55 -040014#include "common/utilities.h"
15#include "libANGLE/Config.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/Context.h"
Jamie Madill6c1f6712017-02-14 19:08:04 -050017#include "libANGLE/Display.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050018#include "libANGLE/FramebufferAttachment.h"
Jamie Madillc46f45d2015-03-31 13:20:55 -040019#include "libANGLE/Renderbuffer.h"
20#include "libANGLE/Surface.h"
21#include "libANGLE/Texture.h"
Brandon Jones76746f92017-11-22 11:44:41 -080022#include "libANGLE/angletypes.h"
Jamie Madillc46f45d2015-03-31 13:20:55 -040023#include "libANGLE/formatutils.h"
Jamie Madill8415b5f2016-04-26 13:41:39 -040024#include "libANGLE/renderer/ContextImpl.h"
Geoff Langb5d8f232014-12-04 15:43:01 -050025#include "libANGLE/renderer/FramebufferImpl.h"
Jamie Madill7aea7e02016-05-10 10:39:45 -040026#include "libANGLE/renderer/GLImplFactory.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050027#include "libANGLE/renderer/RenderbufferImpl.h"
Corentin Wallez37c39792015-08-20 14:19:46 -040028#include "libANGLE/renderer/SurfaceImpl.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040029
Jamie Madill362876b2016-06-16 14:46:59 -040030using namespace angle;
31
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032namespace gl
33{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +000034
Jamie Madilld1405e52015-03-05 15:41:39 -050035namespace
36{
Jamie Madill362876b2016-06-16 14:46:59 -040037
Martin Radev9bc9a322017-07-21 14:28:17 +030038bool CheckMultiviewStateMatchesForCompleteness(const FramebufferAttachment *firstAttachment,
39 const FramebufferAttachment *secondAttachment)
40{
41 ASSERT(firstAttachment && secondAttachment);
42 ASSERT(firstAttachment->isAttached() && secondAttachment->isAttached());
43
44 if (firstAttachment->getNumViews() != secondAttachment->getNumViews())
45 {
46 return false;
47 }
48 if (firstAttachment->getBaseViewIndex() != secondAttachment->getBaseViewIndex())
49 {
50 return false;
51 }
52 if (firstAttachment->getMultiviewLayout() != secondAttachment->getMultiviewLayout())
53 {
54 return false;
55 }
56 if (firstAttachment->getMultiviewViewportOffsets() !=
57 secondAttachment->getMultiviewViewportOffsets())
58 {
59 return false;
60 }
61 return true;
62}
63
Geoff Lang9f10b772017-05-16 15:51:03 -040064bool CheckAttachmentCompleteness(const Context *context, const FramebufferAttachment &attachment)
65{
66 ASSERT(attachment.isAttached());
67
68 const Extents &size = attachment.getSize();
69 if (size.width == 0 || size.height == 0)
70 {
71 return false;
72 }
73
Yuly Novikov2eb54072018-08-22 16:41:26 -040074 if (!attachment.isRenderable(context))
Geoff Lang9f10b772017-05-16 15:51:03 -040075 {
76 return false;
77 }
78
79 if (attachment.type() == GL_TEXTURE)
80 {
Jiawei Shaoa8802472018-05-28 11:17:47 +080081 // [EXT_geometry_shader] Section 9.4.1, "Framebuffer Completeness"
82 // If <image> is a three-dimensional texture or a two-dimensional array texture and the
83 // attachment is not layered, the selected layer is less than the depth or layer count,
84 // respectively, of the texture.
85 if (!attachment.isLayered())
Geoff Lang9f10b772017-05-16 15:51:03 -040086 {
Jiawei Shaoa8802472018-05-28 11:17:47 +080087 if (attachment.layer() >= size.depth)
88 {
89 return false;
90 }
91 }
92 // If <image> is a three-dimensional texture or a two-dimensional array texture and the
93 // attachment is layered, the depth or layer count, respectively, of the texture is less
94 // than or equal to the value of MAX_FRAMEBUFFER_LAYERS_EXT.
95 else
96 {
97 if (static_cast<GLuint>(size.depth) >= context->getCaps().maxFramebufferLayers)
98 {
99 return false;
100 }
Geoff Lang9f10b772017-05-16 15:51:03 -0400101 }
102
103 // ES3 specifies that cube map texture attachments must be cube complete.
104 // This language is missing from the ES2 spec, but we enforce it here because some
105 // desktop OpenGL drivers also enforce this validation.
106 // TODO(jmadill): Check if OpenGL ES2 drivers enforce cube completeness.
107 const Texture *texture = attachment.getTexture();
108 ASSERT(texture);
Corentin Wallez99d492c2018-02-27 15:17:10 -0500109 if (texture->getType() == TextureType::CubeMap &&
Geoff Lang9f10b772017-05-16 15:51:03 -0400110 !texture->getTextureState().isCubeComplete())
111 {
112 return false;
113 }
Geoff Lang857c09d2017-05-16 15:55:04 -0400114
115 if (!texture->getImmutableFormat())
116 {
117 GLuint attachmentMipLevel = static_cast<GLuint>(attachment.mipLevel());
118
119 // From the ES 3.0 spec, pg 213:
120 // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of
121 // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture,
122 // then the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL must be in the
123 // range[levelbase, q], where levelbase is the value of TEXTURE_BASE_LEVEL and q is
124 // the effective maximum texture level defined in the Mipmapping discussion of
125 // section 3.8.10.4.
126 if (attachmentMipLevel < texture->getBaseLevel() ||
127 attachmentMipLevel > texture->getMipmapMaxLevel())
128 {
129 return false;
130 }
131
132 // Form the ES 3.0 spec, pg 213/214:
133 // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of
134 // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture and
135 // the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL is not levelbase, then the
136 // texture must be mipmap complete, and if FRAMEBUFFER_ATTACHMENT_OBJECT_NAME names
137 // a cubemap texture, the texture must also be cube complete.
138 if (attachmentMipLevel != texture->getBaseLevel() && !texture->isMipmapComplete())
139 {
140 return false;
141 }
142 }
Geoff Lang9f10b772017-05-16 15:51:03 -0400143 }
144
145 return true;
146};
147
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400148bool CheckAttachmentSampleCompleteness(const Context *context,
149 const FramebufferAttachment &attachment,
150 bool colorAttachment,
151 Optional<int> *samples,
Geoff Lang92019432017-11-20 13:09:34 -0500152 Optional<bool> *fixedSampleLocations)
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400153{
154 ASSERT(attachment.isAttached());
155
156 if (attachment.type() == GL_TEXTURE)
157 {
158 const Texture *texture = attachment.getTexture();
159 ASSERT(texture);
160
161 const ImageIndex &attachmentImageIndex = attachment.getTextureImageIndex();
Jiawei Shaoa8802472018-05-28 11:17:47 +0800162 bool fixedSampleloc = texture->getAttachmentFixedSampleLocations(attachmentImageIndex);
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400163 if (fixedSampleLocations->valid() && fixedSampleloc != fixedSampleLocations->value())
164 {
165 return false;
166 }
167 else
168 {
169 *fixedSampleLocations = fixedSampleloc;
170 }
171 }
172
173 if (samples->valid())
174 {
175 if (attachment.getSamples() != samples->value())
176 {
177 if (colorAttachment)
178 {
179 // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that
180 // all color attachments have the same number of samples for the FBO to be complete.
181 return false;
182 }
183 else
184 {
185 // CHROMIUM_framebuffer_mixed_samples allows a framebuffer to be considered complete
186 // when its depth or stencil samples are a multiple of the number of color samples.
187 if (!context->getExtensions().framebufferMixedSamples)
188 {
189 return false;
190 }
191
192 if ((attachment.getSamples() % std::max(samples->value(), 1)) != 0)
193 {
194 return false;
195 }
196 }
197 }
198 }
199 else
200 {
201 *samples = attachment.getSamples();
202 }
203
204 return true;
205}
206
Jamie Madill05b35b22017-10-03 09:01:44 -0400207// Needed to index into the attachment arrays/bitsets.
Jamie Madill682efdc2017-10-03 14:10:29 -0400208static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500209 Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX,
Jamie Madill05b35b22017-10-03 09:01:44 -0400210 "Framebuffer Dirty bit mismatch");
Jamie Madill682efdc2017-10-03 14:10:29 -0400211static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500212 Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madill05b35b22017-10-03 09:01:44 -0400213 "Framebuffer Dirty bit mismatch");
Jamie Madill682efdc2017-10-03 14:10:29 -0400214static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS + 1) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500215 Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madill05b35b22017-10-03 09:01:44 -0400216 "Framebuffer Dirty bit mismatch");
217
Jamie Madill6f755b22018-10-09 12:48:54 -0400218angle::Result InitAttachment(const Context *context, FramebufferAttachment *attachment)
Jamie Madill05b35b22017-10-03 09:01:44 -0400219{
220 ASSERT(attachment->isAttached());
221 if (attachment->initState() == InitState::MayNeedInit)
222 {
223 ANGLE_TRY(attachment->initializeContents(context));
224 }
Jamie Madill6f755b22018-10-09 12:48:54 -0400225 return angle::Result::Continue();
Jamie Madill05b35b22017-10-03 09:01:44 -0400226}
227
228bool IsColorMaskedOut(const BlendState &blend)
229{
230 return (!blend.colorMaskRed && !blend.colorMaskGreen && !blend.colorMaskBlue &&
231 !blend.colorMaskAlpha);
232}
233
234bool IsDepthMaskedOut(const DepthStencilState &depthStencil)
235{
236 return !depthStencil.depthMask;
237}
238
239bool IsStencilMaskedOut(const DepthStencilState &depthStencil)
240{
241 return ((depthStencil.stencilMask & depthStencil.stencilWritemask) == 0);
242}
243
244bool IsClearBufferMaskedOut(const Context *context, GLenum buffer)
245{
246 switch (buffer)
247 {
248 case GL_COLOR:
249 return IsColorMaskedOut(context->getGLState().getBlendState());
250 case GL_DEPTH:
251 return IsDepthMaskedOut(context->getGLState().getDepthStencilState());
252 case GL_STENCIL:
253 return IsStencilMaskedOut(context->getGLState().getDepthStencilState());
254 case GL_DEPTH_STENCIL:
255 return IsDepthMaskedOut(context->getGLState().getDepthStencilState()) &&
256 IsStencilMaskedOut(context->getGLState().getDepthStencilState());
257 default:
258 UNREACHABLE();
259 return true;
260 }
261}
262
Jamie Madill362876b2016-06-16 14:46:59 -0400263} // anonymous namespace
Jamie Madilld1405e52015-03-05 15:41:39 -0500264
Jamie Madill6f60d052017-02-22 15:20:11 -0500265// This constructor is only used for default framebuffers.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400266FramebufferState::FramebufferState()
Jamie Madill2274b652018-05-31 10:56:08 -0400267 : mId(0),
268 mLabel(),
Geoff Lang70d0f492015-12-10 17:45:46 -0500269 mColorAttachments(1),
Corentin Walleze7557742017-06-01 13:09:57 -0400270 mDrawBufferStates(1, GL_BACK),
Jamie Madill6f60d052017-02-22 15:20:11 -0500271 mReadBufferState(GL_BACK),
Brandon Jones76746f92017-11-22 11:44:41 -0800272 mDrawBufferTypeMask(),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800273 mDefaultWidth(0),
274 mDefaultHeight(0),
275 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500276 mDefaultFixedSampleLocations(GL_FALSE),
Jiawei Shaob1e91382018-05-17 14:33:55 +0800277 mDefaultLayers(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500278 mWebGLDepthStencilConsistent(true)
Corentin Wallez37c39792015-08-20 14:19:46 -0400279{
Geoff Langd90d3882017-03-21 10:49:54 -0400280 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilla4595b82017-01-11 17:36:34 -0500281 mEnabledDrawBuffers.set(0);
Corentin Wallez37c39792015-08-20 14:19:46 -0400282}
283
Jamie Madill2274b652018-05-31 10:56:08 -0400284FramebufferState::FramebufferState(const Caps &caps, GLuint id)
285 : mId(id),
286 mLabel(),
Geoff Lang70d0f492015-12-10 17:45:46 -0500287 mColorAttachments(caps.maxColorAttachments),
Jamie Madilld1405e52015-03-05 15:41:39 -0500288 mDrawBufferStates(caps.maxDrawBuffers, GL_NONE),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800289 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT),
Brandon Jones76746f92017-11-22 11:44:41 -0800290 mDrawBufferTypeMask(),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800291 mDefaultWidth(0),
292 mDefaultHeight(0),
293 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500294 mDefaultFixedSampleLocations(GL_FALSE),
Jiawei Shaob1e91382018-05-17 14:33:55 +0800295 mDefaultLayers(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500296 mWebGLDepthStencilConsistent(true)
Jamie Madilld1405e52015-03-05 15:41:39 -0500297{
Jamie Madill2274b652018-05-31 10:56:08 -0400298 ASSERT(mId != 0);
Geoff Langa15472a2015-08-11 11:48:03 -0400299 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilld1405e52015-03-05 15:41:39 -0500300 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
301}
302
Jamie Madill48ef11b2016-04-27 15:21:52 -0400303FramebufferState::~FramebufferState()
Jamie Madilld1405e52015-03-05 15:41:39 -0500304{
Jamie Madilld1405e52015-03-05 15:41:39 -0500305}
306
Jamie Madill48ef11b2016-04-27 15:21:52 -0400307const std::string &FramebufferState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500308{
309 return mLabel;
310}
311
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800312const FramebufferAttachment *FramebufferState::getAttachment(const Context *context,
313 GLenum attachment) const
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400314{
315 if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15)
316 {
317 return getColorAttachment(attachment - GL_COLOR_ATTACHMENT0);
318 }
319
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800320 // WebGL1 allows a developer to query for attachment parameters even when "inconsistant" (i.e.
321 // multiple conflicting attachment points) and requires us to return the framebuffer attachment
322 // associated with WebGL.
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400323 switch (attachment)
324 {
325 case GL_COLOR:
326 case GL_BACK:
327 return getColorAttachment(0);
328 case GL_DEPTH:
329 case GL_DEPTH_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800330 if (context->isWebGL1())
331 {
332 return getWebGLDepthAttachment();
333 }
334 else
335 {
336 return getDepthAttachment();
337 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400338 case GL_STENCIL:
339 case GL_STENCIL_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800340 if (context->isWebGL1())
341 {
342 return getWebGLStencilAttachment();
343 }
344 else
345 {
346 return getStencilAttachment();
347 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400348 case GL_DEPTH_STENCIL:
349 case GL_DEPTH_STENCIL_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800350 if (context->isWebGL1())
351 {
352 return getWebGLDepthStencilAttachment();
353 }
354 else
355 {
356 return getDepthStencilAttachment();
357 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400358 default:
359 UNREACHABLE();
360 return nullptr;
361 }
362}
363
Jamie Madill05b35b22017-10-03 09:01:44 -0400364size_t FramebufferState::getReadIndex() const
Jamie Madill7147f012015-03-05 15:41:40 -0500365{
Jamie Madill231c7f52017-04-26 13:45:37 -0400366 ASSERT(mReadBufferState == GL_BACK ||
367 (mReadBufferState >= GL_COLOR_ATTACHMENT0 && mReadBufferState <= GL_COLOR_ATTACHMENT15));
368 size_t readIndex = (mReadBufferState == GL_BACK
369 ? 0
370 : static_cast<size_t>(mReadBufferState - GL_COLOR_ATTACHMENT0));
Jamie Madill7147f012015-03-05 15:41:40 -0500371 ASSERT(readIndex < mColorAttachments.size());
Jamie Madill05b35b22017-10-03 09:01:44 -0400372 return readIndex;
373}
374
375const FramebufferAttachment *FramebufferState::getReadAttachment() const
376{
377 if (mReadBufferState == GL_NONE)
378 {
379 return nullptr;
380 }
381 size_t readIndex = getReadIndex();
Jamie Madill2d06b732015-04-20 12:53:28 -0400382 return mColorAttachments[readIndex].isAttached() ? &mColorAttachments[readIndex] : nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500383}
384
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500385const FramebufferAttachment *FramebufferState::getFirstNonNullAttachment() const
386{
387 auto *colorAttachment = getFirstColorAttachment();
388 if (colorAttachment)
389 {
390 return colorAttachment;
391 }
392 return getDepthOrStencilAttachment();
393}
394
Jamie Madill48ef11b2016-04-27 15:21:52 -0400395const FramebufferAttachment *FramebufferState::getFirstColorAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500396{
Jamie Madill2d06b732015-04-20 12:53:28 -0400397 for (const FramebufferAttachment &colorAttachment : mColorAttachments)
Jamie Madill7147f012015-03-05 15:41:40 -0500398 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400399 if (colorAttachment.isAttached())
Jamie Madill7147f012015-03-05 15:41:40 -0500400 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400401 return &colorAttachment;
Jamie Madill7147f012015-03-05 15:41:40 -0500402 }
403 }
404
405 return nullptr;
406}
407
Jamie Madill48ef11b2016-04-27 15:21:52 -0400408const FramebufferAttachment *FramebufferState::getDepthOrStencilAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500409{
Jamie Madill2d06b732015-04-20 12:53:28 -0400410 if (mDepthAttachment.isAttached())
411 {
412 return &mDepthAttachment;
413 }
414 if (mStencilAttachment.isAttached())
415 {
416 return &mStencilAttachment;
417 }
418 return nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500419}
420
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500421const FramebufferAttachment *FramebufferState::getStencilOrDepthStencilAttachment() const
422{
423 if (mStencilAttachment.isAttached())
424 {
425 return &mStencilAttachment;
426 }
427 return getDepthStencilAttachment();
428}
429
Jamie Madill48ef11b2016-04-27 15:21:52 -0400430const FramebufferAttachment *FramebufferState::getColorAttachment(size_t colorAttachment) const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400431{
432 ASSERT(colorAttachment < mColorAttachments.size());
Jamie Madill231c7f52017-04-26 13:45:37 -0400433 return mColorAttachments[colorAttachment].isAttached() ? &mColorAttachments[colorAttachment]
434 : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400435}
436
Jamie Madill48ef11b2016-04-27 15:21:52 -0400437const FramebufferAttachment *FramebufferState::getDepthAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400438{
Jamie Madill2d06b732015-04-20 12:53:28 -0400439 return mDepthAttachment.isAttached() ? &mDepthAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400440}
441
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800442const FramebufferAttachment *FramebufferState::getWebGLDepthAttachment() const
443{
444 return mWebGLDepthAttachment.isAttached() ? &mWebGLDepthAttachment : nullptr;
445}
446
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800447const FramebufferAttachment *FramebufferState::getWebGLDepthStencilAttachment() const
448{
449 return mWebGLDepthStencilAttachment.isAttached() ? &mWebGLDepthStencilAttachment : nullptr;
450}
451
Jamie Madill48ef11b2016-04-27 15:21:52 -0400452const FramebufferAttachment *FramebufferState::getStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400453{
Jamie Madill2d06b732015-04-20 12:53:28 -0400454 return mStencilAttachment.isAttached() ? &mStencilAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400455}
456
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800457const FramebufferAttachment *FramebufferState::getWebGLStencilAttachment() const
458{
459 return mWebGLStencilAttachment.isAttached() ? &mWebGLStencilAttachment : nullptr;
460}
461
Jamie Madill48ef11b2016-04-27 15:21:52 -0400462const FramebufferAttachment *FramebufferState::getDepthStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400463{
464 // A valid depth-stencil attachment has the same resource bound to both the
465 // depth and stencil attachment points.
Jamie Madill2d06b732015-04-20 12:53:28 -0400466 if (mDepthAttachment.isAttached() && mStencilAttachment.isAttached() &&
Jamie Madill44f26482016-11-18 12:49:15 -0500467 mDepthAttachment == mStencilAttachment)
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400468 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400469 return &mDepthAttachment;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400470 }
471
472 return nullptr;
473}
474
Jamie Madill48ef11b2016-04-27 15:21:52 -0400475bool FramebufferState::attachmentsHaveSameDimensions() const
Jamie Madillcc86d642015-11-24 13:00:07 -0500476{
477 Optional<Extents> attachmentSize;
478
Jamie Madill231c7f52017-04-26 13:45:37 -0400479 auto hasMismatchedSize = [&attachmentSize](const FramebufferAttachment &attachment) {
Jamie Madillcc86d642015-11-24 13:00:07 -0500480 if (!attachment.isAttached())
481 {
482 return false;
483 }
484
485 if (!attachmentSize.valid())
486 {
487 attachmentSize = attachment.getSize();
488 return false;
489 }
490
Jeff Gilbert8f8edd62017-10-31 14:26:30 -0700491 const auto &prevSize = attachmentSize.value();
492 const auto &curSize = attachment.getSize();
493 return (curSize.width != prevSize.width || curSize.height != prevSize.height);
Jamie Madillcc86d642015-11-24 13:00:07 -0500494 };
495
496 for (const auto &attachment : mColorAttachments)
497 {
498 if (hasMismatchedSize(attachment))
499 {
500 return false;
501 }
502 }
503
504 if (hasMismatchedSize(mDepthAttachment))
505 {
506 return false;
507 }
508
509 return !hasMismatchedSize(mStencilAttachment);
510}
511
Luc Ferron5bdf8bd2018-06-20 09:51:37 -0400512bool FramebufferState::hasSeparateDepthAndStencilAttachments() const
513{
514 // if we have both a depth and stencil buffer, they must refer to the same object
515 // since we only support packed_depth_stencil and not separate depth and stencil
516 return (getDepthAttachment() != nullptr && getStencilAttachment() != nullptr &&
517 getDepthStencilAttachment() == nullptr);
518}
519
Jamie Madilld4442552018-02-27 22:03:47 -0500520const FramebufferAttachment *FramebufferState::getDrawBuffer(size_t drawBufferIdx) const
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400521{
522 ASSERT(drawBufferIdx < mDrawBufferStates.size());
523 if (mDrawBufferStates[drawBufferIdx] != GL_NONE)
524 {
525 // ES3 spec: "If the GL is bound to a draw framebuffer object, the ith buffer listed in bufs
526 // must be COLOR_ATTACHMENTi or NONE"
527 ASSERT(mDrawBufferStates[drawBufferIdx] == GL_COLOR_ATTACHMENT0 + drawBufferIdx ||
528 (drawBufferIdx == 0 && mDrawBufferStates[drawBufferIdx] == GL_BACK));
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800529
530 if (mDrawBufferStates[drawBufferIdx] == GL_BACK)
531 {
532 return getColorAttachment(0);
533 }
534 else
535 {
536 return getColorAttachment(mDrawBufferStates[drawBufferIdx] - GL_COLOR_ATTACHMENT0);
537 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400538 }
539 else
540 {
541 return nullptr;
542 }
543}
544
545size_t FramebufferState::getDrawBufferCount() const
546{
547 return mDrawBufferStates.size();
548}
549
Geoff Langb21e20d2016-07-19 15:35:41 -0400550bool FramebufferState::colorAttachmentsAreUniqueImages() const
551{
552 for (size_t firstAttachmentIdx = 0; firstAttachmentIdx < mColorAttachments.size();
553 firstAttachmentIdx++)
554 {
Jamie Madilld4442552018-02-27 22:03:47 -0500555 const FramebufferAttachment &firstAttachment = mColorAttachments[firstAttachmentIdx];
Geoff Langb21e20d2016-07-19 15:35:41 -0400556 if (!firstAttachment.isAttached())
557 {
558 continue;
559 }
560
561 for (size_t secondAttachmentIdx = firstAttachmentIdx + 1;
562 secondAttachmentIdx < mColorAttachments.size(); secondAttachmentIdx++)
563 {
Jamie Madilld4442552018-02-27 22:03:47 -0500564 const FramebufferAttachment &secondAttachment = mColorAttachments[secondAttachmentIdx];
Geoff Langb21e20d2016-07-19 15:35:41 -0400565 if (!secondAttachment.isAttached())
566 {
567 continue;
568 }
569
570 if (firstAttachment == secondAttachment)
571 {
572 return false;
573 }
574 }
575 }
576
577 return true;
578}
579
Jamie Madill9c335862017-07-18 11:51:38 -0400580bool FramebufferState::hasDepth() const
581{
582 return (mDepthAttachment.isAttached() && mDepthAttachment.getDepthSize() > 0);
583}
584
585bool FramebufferState::hasStencil() const
586{
587 return (mStencilAttachment.isAttached() && mStencilAttachment.getStencilSize() > 0);
588}
589
Martin Radev5c00d0d2017-08-07 10:06:59 +0300590GLsizei FramebufferState::getNumViews() const
591{
592 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
593 if (attachment == nullptr)
594 {
595 return FramebufferAttachment::kDefaultNumViews;
596 }
597 return attachment->getNumViews();
598}
599
600const std::vector<Offset> *FramebufferState::getViewportOffsets() const
601{
602 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
603 if (attachment == nullptr)
604 {
605 return nullptr;
606 }
607 return &attachment->getMultiviewViewportOffsets();
608}
609
610GLenum FramebufferState::getMultiviewLayout() const
611{
612 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
613 if (attachment == nullptr)
614 {
615 return GL_NONE;
616 }
617 return attachment->getMultiviewLayout();
618}
619
Martin Radev4e619f52017-08-09 11:50:06 +0300620int FramebufferState::getBaseViewIndex() const
621{
622 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
623 if (attachment == nullptr)
624 {
625 return GL_NONE;
626 }
627 return attachment->getBaseViewIndex();
628}
629
Jamie Madill05b35b22017-10-03 09:01:44 -0400630Box FramebufferState::getDimensions() const
631{
632 ASSERT(attachmentsHaveSameDimensions());
633 ASSERT(getFirstNonNullAttachment() != nullptr);
634 Extents extents = getFirstNonNullAttachment()->getSize();
635 return Box(0, 0, 0, extents.width, extents.height, extents.depth);
636}
637
Jamie Madill7aea7e02016-05-10 10:39:45 -0400638Framebuffer::Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id)
Jamie Madill2274b652018-05-31 10:56:08 -0400639 : mState(caps, id),
Jamie Madill362876b2016-06-16 14:46:59 -0400640 mImpl(factory->createFramebuffer(mState)),
Jamie Madill362876b2016-06-16 14:46:59 -0400641 mCachedStatus(),
642 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
643 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644{
Corentin Wallez37c39792015-08-20 14:19:46 -0400645 ASSERT(mImpl != nullptr);
Jamie Madill362876b2016-06-16 14:46:59 -0400646 ASSERT(mState.mColorAttachments.size() == static_cast<size_t>(caps.maxColorAttachments));
647
Jamie Madill1e5499d2017-04-05 11:22:16 -0400648 for (uint32_t colorIndex = 0;
649 colorIndex < static_cast<uint32_t>(mState.mColorAttachments.size()); ++colorIndex)
Jamie Madill362876b2016-06-16 14:46:59 -0400650 {
Jamie Madill1e5499d2017-04-05 11:22:16 -0400651 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
Jamie Madill362876b2016-06-16 14:46:59 -0400652 }
Corentin Wallez37c39792015-08-20 14:19:46 -0400653}
654
Geoff Langbf7b95d2018-05-01 16:48:21 -0400655Framebuffer::Framebuffer(const Context *context, egl::Surface *surface)
Jamie Madill362876b2016-06-16 14:46:59 -0400656 : mState(),
Geoff Langbf7b95d2018-05-01 16:48:21 -0400657 mImpl(surface->getImplementation()->createDefaultFramebuffer(context, mState)),
Jamie Madill362876b2016-06-16 14:46:59 -0400658 mCachedStatus(GL_FRAMEBUFFER_COMPLETE),
659 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
660 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
Corentin Wallez37c39792015-08-20 14:19:46 -0400661{
Geoff Langda88add2014-12-01 10:22:01 -0500662 ASSERT(mImpl != nullptr);
Jamie Madill1e5499d2017-04-05 11:22:16 -0400663 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Jamie Madill6f60d052017-02-22 15:20:11 -0500664
Geoff Langbf7b95d2018-05-01 16:48:21 -0400665 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400666 FramebufferAttachment::kDefaultNumViews,
Martin Radev5dae57b2017-07-14 16:15:55 +0300667 FramebufferAttachment::kDefaultBaseViewIndex,
668 FramebufferAttachment::kDefaultMultiviewLayout,
669 FramebufferAttachment::kDefaultViewportOffsets);
Jamie Madill6f60d052017-02-22 15:20:11 -0500670
671 if (surface->getConfig()->depthSize > 0)
672 {
Geoff Langbf7b95d2018-05-01 16:48:21 -0400673 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400674 FramebufferAttachment::kDefaultNumViews,
Jamie Madilld4442552018-02-27 22:03:47 -0500675 FramebufferAttachment::kDefaultBaseViewIndex,
676 FramebufferAttachment::kDefaultMultiviewLayout,
677 FramebufferAttachment::kDefaultViewportOffsets);
Jamie Madill6f60d052017-02-22 15:20:11 -0500678 }
679
680 if (surface->getConfig()->stencilSize > 0)
681 {
Geoff Langbf7b95d2018-05-01 16:48:21 -0400682 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400683 FramebufferAttachment::kDefaultNumViews,
684 FramebufferAttachment::kDefaultBaseViewIndex,
685 FramebufferAttachment::kDefaultMultiviewLayout,
686 FramebufferAttachment::kDefaultViewportOffsets);
Jamie Madill6f60d052017-02-22 15:20:11 -0500687 }
Brandon Jones76746f92017-11-22 11:44:41 -0800688 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(0), 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000689}
690
Corentin Wallezccab69d2017-01-27 16:57:15 -0500691Framebuffer::Framebuffer(rx::GLImplFactory *factory)
692 : mState(),
693 mImpl(factory->createFramebuffer(mState)),
Corentin Wallezccab69d2017-01-27 16:57:15 -0500694 mCachedStatus(GL_FRAMEBUFFER_UNDEFINED_OES),
695 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
696 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
697{
Jamie Madill1e5499d2017-04-05 11:22:16 -0400698 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Brandon Jones76746f92017-11-22 11:44:41 -0800699 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(0), 0);
Corentin Wallezccab69d2017-01-27 16:57:15 -0500700}
701
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000702Framebuffer::~Framebuffer()
703{
Geoff Langda88add2014-12-01 10:22:01 -0500704 SafeDelete(mImpl);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000705}
706
Jamie Madill4928b7c2017-06-20 12:57:39 -0400707void Framebuffer::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500708{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400709 for (auto &attachment : mState.mColorAttachments)
710 {
711 attachment.detach(context);
712 }
713 mState.mDepthAttachment.detach(context);
714 mState.mStencilAttachment.detach(context);
715 mState.mWebGLDepthAttachment.detach(context);
716 mState.mWebGLStencilAttachment.detach(context);
717 mState.mWebGLDepthStencilAttachment.detach(context);
718
Jamie Madillc564c072017-06-01 12:45:42 -0400719 mImpl->destroy(context);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500720}
721
Geoff Lang70d0f492015-12-10 17:45:46 -0500722void Framebuffer::setLabel(const std::string &label)
723{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400724 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500725}
726
727const std::string &Framebuffer::getLabel() const
728{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400729 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500730}
731
Jamie Madill8693bdb2017-09-02 15:32:14 -0400732bool Framebuffer::detachTexture(const Context *context, GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000733{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400734 return detachResourceById(context, GL_TEXTURE, textureId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735}
736
Jamie Madill8693bdb2017-09-02 15:32:14 -0400737bool Framebuffer::detachRenderbuffer(const Context *context, GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400739 return detachResourceById(context, GL_RENDERBUFFER, renderbufferId);
Jamie Madilld1405e52015-03-05 15:41:39 -0500740}
Jamie Madille261b442014-06-25 12:42:21 -0400741
Jamie Madill8693bdb2017-09-02 15:32:14 -0400742bool Framebuffer::detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId)
Jamie Madilld1405e52015-03-05 15:41:39 -0500743{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400744 bool found = false;
745
Jamie Madill362876b2016-06-16 14:46:59 -0400746 for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
Jamie Madilld1405e52015-03-05 15:41:39 -0500747 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400748 if (detachMatchingAttachment(context, &mState.mColorAttachments[colorIndex], resourceType,
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300749 resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400750 {
751 found = true;
752 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753 }
754
Jamie Madilla02315b2017-02-23 14:14:47 -0500755 if (context->isWebGL1())
756 {
757 const std::array<FramebufferAttachment *, 3> attachments = {
758 {&mState.mWebGLDepthStencilAttachment, &mState.mWebGLDepthAttachment,
759 &mState.mWebGLStencilAttachment}};
760 for (FramebufferAttachment *attachment : attachments)
761 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300762 if (detachMatchingAttachment(context, attachment, resourceType, resourceId))
Jamie Madilla02315b2017-02-23 14:14:47 -0500763 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400764 found = true;
Jamie Madilla02315b2017-02-23 14:14:47 -0500765 }
766 }
767 }
768 else
769 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300770 if (detachMatchingAttachment(context, &mState.mDepthAttachment, resourceType, resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400771 {
772 found = true;
773 }
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300774 if (detachMatchingAttachment(context, &mState.mStencilAttachment, resourceType, resourceId))
Jamie Madill8693bdb2017-09-02 15:32:14 -0400775 {
776 found = true;
777 }
Jamie Madilla02315b2017-02-23 14:14:47 -0500778 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400779
780 return found;
Jamie Madill362876b2016-06-16 14:46:59 -0400781}
782
Jamie Madill8693bdb2017-09-02 15:32:14 -0400783bool Framebuffer::detachMatchingAttachment(const Context *context,
Jamie Madill4928b7c2017-06-20 12:57:39 -0400784 FramebufferAttachment *attachment,
Jamie Madill362876b2016-06-16 14:46:59 -0400785 GLenum matchType,
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300786 GLuint matchId)
Jamie Madill362876b2016-06-16 14:46:59 -0400787{
788 if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId)
789 {
Olli Etuaho4ebd8f32018-09-20 11:12:46 +0300790 // We go through resetAttachment to make sure that all the required bookkeeping will be done
791 // such as updating enabled draw buffer state.
792 resetAttachment(context, attachment->getBinding());
Jamie Madill8693bdb2017-09-02 15:32:14 -0400793 return true;
Jamie Madill362876b2016-06-16 14:46:59 -0400794 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400795
796 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797}
798
Corentin Wallez37c39792015-08-20 14:19:46 -0400799const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000800{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400801 return mState.getColorAttachment(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802}
803
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400804const FramebufferAttachment *Framebuffer::getDepthbuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400805{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400806 return mState.getDepthAttachment();
Geoff Lang646559f2013-08-15 11:08:15 -0400807}
808
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400809const FramebufferAttachment *Framebuffer::getStencilbuffer() const
810{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400811 return mState.getStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400812}
813
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400814const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
815{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400816 return mState.getDepthStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400817}
818
819const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000820{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400821 return mState.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000822}
823
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500824const FramebufferAttachment *Framebuffer::getStencilOrDepthStencilAttachment() const
825{
826 return mState.getStencilOrDepthStencilAttachment();
827}
828
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400829const FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000830{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400831 return mState.getReadAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000832}
833
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000834GLenum Framebuffer::getReadColorbufferType() const
835{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400836 const FramebufferAttachment *readAttachment = mState.getReadAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400837 return (readAttachment != nullptr ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000838}
839
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400840const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000841{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400842 return mState.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000843}
844
Jamie Madill6dd06ea2017-07-19 13:47:55 -0400845const FramebufferAttachment *Framebuffer::getFirstNonNullAttachment() const
846{
847 return mState.getFirstNonNullAttachment();
848}
849
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800850const FramebufferAttachment *Framebuffer::getAttachment(const Context *context,
851 GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000852{
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800853 return mState.getAttachment(context, attachment);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400854}
855
Geoff Langa15472a2015-08-11 11:48:03 -0400856size_t Framebuffer::getDrawbufferStateCount() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000857{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400858 return mState.mDrawBufferStates.size();
Geoff Langa15472a2015-08-11 11:48:03 -0400859}
860
861GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const
862{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400863 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
864 return mState.mDrawBufferStates[drawBuffer];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000865}
866
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500867const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const
868{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400869 return mState.getDrawBufferStates();
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500870}
871
Geoff Lang164d54e2014-12-01 10:55:33 -0500872void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000873{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400874 auto &drawStates = mState.mDrawBufferStates;
Jamie Madilld1405e52015-03-05 15:41:39 -0500875
876 ASSERT(count <= drawStates.size());
877 std::copy(buffers, buffers + count, drawStates.begin());
878 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500879 mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS);
Jamie Madilla4595b82017-01-11 17:36:34 -0500880
881 mState.mEnabledDrawBuffers.reset();
Brandon Jones76746f92017-11-22 11:44:41 -0800882 mState.mDrawBufferTypeMask.reset();
883
Jamie Madilla4595b82017-01-11 17:36:34 -0500884 for (size_t index = 0; index < count; ++index)
885 {
Brandon Jones76746f92017-11-22 11:44:41 -0800886 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(index), index);
887
Jamie Madilla4595b82017-01-11 17:36:34 -0500888 if (drawStates[index] != GL_NONE && mState.mColorAttachments[index].isAttached())
889 {
890 mState.mEnabledDrawBuffers.set(index);
891 }
892 }
Geoff Lang9dd95802014-12-01 11:12:59 -0500893}
894
Geoff Langa15472a2015-08-11 11:48:03 -0400895const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const
896{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400897 return mState.getDrawBuffer(drawBuffer);
Geoff Langa15472a2015-08-11 11:48:03 -0400898}
899
Geoff Lange0cff192017-05-30 13:04:56 -0400900GLenum Framebuffer::getDrawbufferWriteType(size_t drawBuffer) const
901{
902 const FramebufferAttachment *attachment = mState.getDrawBuffer(drawBuffer);
903 if (attachment == nullptr)
904 {
905 return GL_NONE;
906 }
907
908 GLenum componentType = attachment->getFormat().info->componentType;
909 switch (componentType)
910 {
911 case GL_INT:
912 case GL_UNSIGNED_INT:
913 return componentType;
914
915 default:
916 return GL_FLOAT;
917 }
918}
919
Brandon Jonesc405ae72017-12-06 14:15:03 -0800920ComponentTypeMask Framebuffer::getDrawBufferTypeMask() const
Brandon Jones76746f92017-11-22 11:44:41 -0800921{
922 return mState.mDrawBufferTypeMask;
923}
924
925DrawBufferMask Framebuffer::getDrawBufferMask() const
926{
927 return mState.mEnabledDrawBuffers;
928}
929
Geoff Langa15472a2015-08-11 11:48:03 -0400930bool Framebuffer::hasEnabledDrawBuffer() const
931{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400932 for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
Geoff Langa15472a2015-08-11 11:48:03 -0400933 {
934 if (getDrawBuffer(drawbufferIdx) != nullptr)
935 {
936 return true;
937 }
938 }
939
940 return false;
941}
942
Geoff Lang9dd95802014-12-01 11:12:59 -0500943GLenum Framebuffer::getReadBufferState() const
944{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400945 return mState.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500946}
947
948void Framebuffer::setReadBuffer(GLenum buffer)
949{
Jamie Madillb885e572015-02-03 16:16:04 -0500950 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
951 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madill48ef11b2016-04-27 15:21:52 -0400952 (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size()));
953 mState.mReadBufferState = buffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500954 mDirtyBits.set(DIRTY_BIT_READ_BUFFER);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000955}
956
Corentin Wallez37c39792015-08-20 14:19:46 -0400957size_t Framebuffer::getNumColorBuffers() const
958{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400959 return mState.mColorAttachments.size();
Corentin Wallez37c39792015-08-20 14:19:46 -0400960}
961
Jamie Madill0df8fe42015-11-24 16:10:24 -0500962bool Framebuffer::hasDepth() const
963{
Jamie Madill9c335862017-07-18 11:51:38 -0400964 return mState.hasDepth();
Jamie Madill0df8fe42015-11-24 16:10:24 -0500965}
966
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000967bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000968{
Jamie Madill9c335862017-07-18 11:51:38 -0400969 return mState.hasStencil();
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000970}
971
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000972bool Framebuffer::usingExtendedDrawBuffers() const
973{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400974 for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000975 {
Geoff Langa15472a2015-08-11 11:48:03 -0400976 if (getDrawBuffer(drawbufferIdx) != nullptr)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000977 {
978 return true;
979 }
980 }
981
982 return false;
983}
984
Jamie Madillb983a4b2018-08-01 11:34:51 -0400985void Framebuffer::invalidateCompletenessCache(const Context *context)
Geoff Lang9aded172017-04-05 11:07:56 -0400986{
Jamie Madill2274b652018-05-31 10:56:08 -0400987 if (mState.mId != 0)
Geoff Lang9aded172017-04-05 11:07:56 -0400988 {
989 mCachedStatus.reset();
Jamie Madilld84b6732018-09-06 15:54:35 -0400990 onStateChange(context, angle::SubjectMessage::CONTENTS_CHANGED);
Geoff Lang9aded172017-04-05 11:07:56 -0400991 }
992}
993
Jamie Madillcc73f242018-08-01 11:34:48 -0400994GLenum Framebuffer::checkStatusImpl(const Context *context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995{
Jamie Madillcc73f242018-08-01 11:34:48 -0400996 ASSERT(!isDefault());
997 ASSERT(hasAnyDirtyBit() || !mCachedStatus.valid());
Geoff Lang528ce3c2014-12-01 10:44:07 -0500998
Jamie Madillcc73f242018-08-01 11:34:48 -0400999 mCachedStatus = checkStatusWithGLFrontEnd(context);
Jamie Madille98b1b52018-03-08 09:47:23 -05001000
Jamie Madillcc73f242018-08-01 11:34:48 -04001001 if (mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE)
1002 {
1003 Error err = syncState(context);
1004 if (err.isError())
Jamie Madille98b1b52018-03-08 09:47:23 -05001005 {
Jamie Madillcc73f242018-08-01 11:34:48 -04001006 // TODO(jmadill): Remove when refactor complete. http://anglebug.com/2491
1007 const_cast<Context *>(context)->handleError(err);
1008 return GetDefaultReturnValue<EntryPoint::CheckFramebufferStatus, GLenum>();
1009 }
1010 if (!mImpl->checkStatus(context))
1011 {
1012 mCachedStatus = GL_FRAMEBUFFER_UNSUPPORTED;
Jamie Madille98b1b52018-03-08 09:47:23 -05001013 }
Jamie Madill362876b2016-06-16 14:46:59 -04001014 }
1015
Jamie Madill427064d2018-04-13 16:20:34 -04001016 return mCachedStatus.value();
Jamie Madill362876b2016-06-16 14:46:59 -04001017}
1018
Jamie Madille98b1b52018-03-08 09:47:23 -05001019GLenum Framebuffer::checkStatusWithGLFrontEnd(const Context *context)
Jamie Madill362876b2016-06-16 14:46:59 -04001020{
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001021 const ContextState &state = context->getContextState();
1022
Jamie Madill2274b652018-05-31 10:56:08 -04001023 ASSERT(mState.mId != 0);
Jamie Madill362876b2016-06-16 14:46:59 -04001024
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001025 bool hasAttachments = false;
1026 Optional<unsigned int> colorbufferSize;
1027 Optional<int> samples;
Geoff Lang92019432017-11-20 13:09:34 -05001028 Optional<bool> fixedSampleLocations;
JiangYizhou461d9a32017-01-04 16:37:26 +08001029 bool hasRenderbuffer = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030
Martin Radev9bc9a322017-07-21 14:28:17 +03001031 const FramebufferAttachment *firstAttachment = getFirstNonNullAttachment();
1032
Jiawei Shaoa8802472018-05-28 11:17:47 +08001033 Optional<bool> isLayered;
1034 Optional<TextureType> colorAttachmentsTextureType;
1035
Jamie Madill48ef11b2016-04-27 15:21:52 -04001036 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037 {
Jamie Madill2d06b732015-04-20 12:53:28 -04001038 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001040 if (!CheckAttachmentCompleteness(context, colorAttachment))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001041 {
1042 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1043 }
daniel@transgaming.com01868132010-08-24 19:21:17 +00001044
Geoff Lang677bb6f2017-04-05 12:40:40 -04001045 const InternalFormat &format = *colorAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001046 if (format.depthBits > 0 || format.stencilBits > 0)
1047 {
1048 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1049 }
1050
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001051 if (!CheckAttachmentSampleCompleteness(context, colorAttachment, true, &samples,
1052 &fixedSampleLocations))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001053 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001054 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001055 }
1056
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001057 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
1058 // in GLES 3.0, there is no such restriction
1059 if (state.getClientMajorVersion() < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001060 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001061 if (colorbufferSize.valid())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001062 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001063 if (format.pixelBytes != colorbufferSize.value())
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +00001064 {
1065 return GL_FRAMEBUFFER_UNSUPPORTED;
1066 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001067 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001068 else
1069 {
1070 colorbufferSize = format.pixelBytes;
1071 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001072 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001073
Martin Radev9bc9a322017-07-21 14:28:17 +03001074 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &colorAttachment))
1075 {
1076 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1077 }
1078
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001079 hasRenderbuffer = hasRenderbuffer || (colorAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001080
1081 if (!hasAttachments)
1082 {
1083 isLayered = colorAttachment.isLayered();
1084 if (isLayered.value())
1085 {
1086 colorAttachmentsTextureType = colorAttachment.getTextureImageIndex().getType();
1087 }
1088 hasAttachments = true;
1089 }
1090 else
1091 {
1092 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1093 // If any framebuffer attachment is layered, all populated attachments
1094 // must be layered. Additionally, all populated color attachments must
1095 // be from textures of the same target. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1096 ASSERT(isLayered.valid());
1097 if (isLayered.value() != colorAttachment.isLayered())
1098 {
1099 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1100 }
1101 else if (isLayered.value())
1102 {
1103 ASSERT(colorAttachmentsTextureType.valid());
1104 if (colorAttachmentsTextureType.value() !=
1105 colorAttachment.getTextureImageIndex().getType())
1106 {
1107 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1108 }
1109 }
1110 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001111 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001112 }
1113
Jamie Madill48ef11b2016-04-27 15:21:52 -04001114 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001115 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001116 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001117 if (!CheckAttachmentCompleteness(context, depthAttachment))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001119 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001120 }
1121
Geoff Lang677bb6f2017-04-05 12:40:40 -04001122 const InternalFormat &format = *depthAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001123 if (format.depthBits == 0)
1124 {
1125 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001126 }
1127
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001128 if (!CheckAttachmentSampleCompleteness(context, depthAttachment, false, &samples,
1129 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001130 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001131 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001132 }
Sami Väisänena797e062016-05-12 15:23:40 +03001133
Martin Radev9bc9a322017-07-21 14:28:17 +03001134 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &depthAttachment))
1135 {
1136 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1137 }
1138
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001139 hasRenderbuffer = hasRenderbuffer || (depthAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001140
1141 if (!hasAttachments)
1142 {
1143 isLayered = depthAttachment.isLayered();
1144 hasAttachments = true;
1145 }
1146 else
1147 {
1148 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1149 // If any framebuffer attachment is layered, all populated attachments
1150 // must be layered. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1151 ASSERT(isLayered.valid());
1152 if (isLayered.value() != depthAttachment.isLayered())
1153 {
1154 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1155 }
1156 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001157 }
1158
Jamie Madill48ef11b2016-04-27 15:21:52 -04001159 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001160 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001161 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001162 if (!CheckAttachmentCompleteness(context, stencilAttachment))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001163 {
1164 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1165 }
1166
Geoff Lang677bb6f2017-04-05 12:40:40 -04001167 const InternalFormat &format = *stencilAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001168 if (format.stencilBits == 0)
1169 {
1170 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001171 }
1172
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001173 if (!CheckAttachmentSampleCompleteness(context, stencilAttachment, false, &samples,
1174 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001175 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001176 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001177 }
Corentin Wallez086d59a2016-04-29 09:06:49 -04001178
Martin Radev9bc9a322017-07-21 14:28:17 +03001179 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &stencilAttachment))
1180 {
1181 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1182 }
1183
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001184 hasRenderbuffer = hasRenderbuffer || (stencilAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001185
1186 if (!hasAttachments)
1187 {
1188 hasAttachments = true;
1189 }
1190 else
1191 {
1192 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1193 // If any framebuffer attachment is layered, all populated attachments
1194 // must be layered.
1195 // {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1196 ASSERT(isLayered.valid());
1197 if (isLayered.value() != stencilAttachment.isLayered())
1198 {
1199 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1200 }
1201 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001202 }
1203
1204 // Starting from ES 3.0 stencil and depth, if present, should be the same image
1205 if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() &&
1206 stencilAttachment.isAttached() && stencilAttachment != depthAttachment)
1207 {
1208 return GL_FRAMEBUFFER_UNSUPPORTED;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001209 }
1210
Jamie Madilla02315b2017-02-23 14:14:47 -05001211 // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL.
1212 if (state.isWebGL1())
1213 {
1214 if (!mState.mWebGLDepthStencilConsistent)
1215 {
1216 return GL_FRAMEBUFFER_UNSUPPORTED;
1217 }
1218
1219 if (mState.mWebGLDepthStencilAttachment.isAttached())
1220 {
1221 if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 ||
1222 mState.mWebGLDepthStencilAttachment.getStencilSize() == 0)
1223 {
1224 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1225 }
Martin Radev9bc9a322017-07-21 14:28:17 +03001226
1227 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment,
1228 &mState.mWebGLDepthStencilAttachment))
1229 {
1230 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1231 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001232 }
1233 else if (mState.mStencilAttachment.isAttached() &&
1234 mState.mStencilAttachment.getDepthSize() > 0)
1235 {
1236 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1237 }
1238 else if (mState.mDepthAttachment.isAttached() &&
1239 mState.mDepthAttachment.getStencilSize() > 0)
1240 {
1241 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1242 }
1243 }
1244
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001245 // ES3.1(section 9.4) requires that if no image is attached to the framebuffer, and either the
1246 // value of the framebuffer's FRAMEBUFFER_DEFAULT_WIDTH or FRAMEBUFFER_DEFAULT_HEIGHT parameters
1247 // is zero, the framebuffer is considered incomplete.
JiangYizhou461d9a32017-01-04 16:37:26 +08001248 GLint defaultWidth = mState.getDefaultWidth();
1249 GLint defaultHeight = mState.getDefaultHeight();
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001250 if (!hasAttachments && (defaultWidth == 0 || defaultHeight == 0))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001251 {
1252 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001253 }
1254
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001255 // In ES 2.0 and WebGL, all color attachments must have the same width and height.
Jamie Madillcc86d642015-11-24 13:00:07 -05001256 // In ES 3.0, there is no such restriction.
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001257 if ((state.getClientMajorVersion() < 3 || state.getExtensions().webglCompatibility) &&
1258 !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -05001259 {
1260 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
1261 }
1262
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001263 // ES3.1(section 9.4) requires that if the attached images are a mix of renderbuffers and
1264 // textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all attached textures.
JiangYizhou461d9a32017-01-04 16:37:26 +08001265 if (fixedSampleLocations.valid() && hasRenderbuffer && !fixedSampleLocations.value())
1266 {
1267 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1268 }
1269
Kenneth Russellce8602a2017-10-03 18:23:08 -07001270 // The WebGL conformance tests implicitly define that all framebuffer
1271 // attachments must be unique. For example, the same level of a texture can
1272 // not be attached to two different color attachments.
1273 if (state.getExtensions().webglCompatibility)
1274 {
1275 if (!mState.colorAttachmentsAreUniqueImages())
1276 {
1277 return GL_FRAMEBUFFER_UNSUPPORTED;
1278 }
1279 }
1280
Jamie Madillcc86d642015-11-24 13:00:07 -05001281 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001282}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001283
Jamie Madill4928b7c2017-06-20 12:57:39 -04001284Error Framebuffer::discard(const Context *context, size_t count, const GLenum *attachments)
Austin Kinross08332632015-05-05 13:35:47 -07001285{
Jamie Madill05b35b22017-10-03 09:01:44 -04001286 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1287 // can be no-ops, so we should probably do that to ensure consistency.
1288 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1289
Jamie Madill4928b7c2017-06-20 12:57:39 -04001290 return mImpl->discard(context, count, attachments);
Austin Kinross08332632015-05-05 13:35:47 -07001291}
1292
Jamie Madill4928b7c2017-06-20 12:57:39 -04001293Error Framebuffer::invalidate(const Context *context, size_t count, const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001294{
Jamie Madill05b35b22017-10-03 09:01:44 -04001295 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1296 // can be no-ops, so we should probably do that to ensure consistency.
1297 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1298
Jamie Madill4928b7c2017-06-20 12:57:39 -04001299 return mImpl->invalidate(context, count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001300}
1301
Jamie Madill05b35b22017-10-03 09:01:44 -04001302bool Framebuffer::partialClearNeedsInit(const Context *context,
1303 bool color,
1304 bool depth,
1305 bool stencil)
1306{
1307 const auto &glState = context->getGLState();
1308
1309 if (!glState.isRobustResourceInitEnabled())
1310 {
1311 return false;
1312 }
1313
1314 // Scissors can affect clearing.
1315 // TODO(jmadill): Check for complete scissor overlap.
1316 if (glState.isScissorTestEnabled())
1317 {
1318 return true;
1319 }
1320
1321 // If colors masked, we must clear before we clear. Do a simple check.
1322 // TODO(jmadill): Filter out unused color channels from the test.
1323 if (color)
1324 {
1325 const auto &blend = glState.getBlendState();
1326 if (!(blend.colorMaskRed && blend.colorMaskGreen && blend.colorMaskBlue &&
1327 blend.colorMaskAlpha))
1328 {
1329 return true;
1330 }
1331 }
1332
1333 const auto &depthStencil = glState.getDepthStencilState();
Yuly Novikov21edf3d2018-07-23 16:44:16 -04001334 if (stencil && (depthStencil.stencilMask != depthStencil.stencilWritemask ||
1335 depthStencil.stencilBackMask != depthStencil.stencilBackWritemask))
Jamie Madill05b35b22017-10-03 09:01:44 -04001336 {
1337 return true;
1338 }
1339
1340 return false;
1341}
1342
Jamie Madill4928b7c2017-06-20 12:57:39 -04001343Error Framebuffer::invalidateSub(const Context *context,
1344 size_t count,
1345 const GLenum *attachments,
Jamie Madilld4442552018-02-27 22:03:47 -05001346 const Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -04001347{
Jamie Madill05b35b22017-10-03 09:01:44 -04001348 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1349 // can be no-ops, so we should probably do that to ensure consistency.
1350 // TODO(jmadill): Make a invalidate no-op in WebGL 2.0.
1351
Jamie Madill4928b7c2017-06-20 12:57:39 -04001352 return mImpl->invalidateSub(context, count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -04001353}
1354
Jamie Madilld4442552018-02-27 22:03:47 -05001355Error Framebuffer::clear(const Context *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -05001356{
Jamie Madill05b35b22017-10-03 09:01:44 -04001357 const auto &glState = context->getGLState();
1358 if (glState.isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -05001359 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001360 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001361 }
1362
Jamie Madill05b35b22017-10-03 09:01:44 -04001363 ANGLE_TRY(mImpl->clear(context, mask));
1364
Jamie Madill05b35b22017-10-03 09:01:44 -04001365 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001366}
1367
Jamie Madilld4442552018-02-27 22:03:47 -05001368Error Framebuffer::clearBufferfv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001369 GLenum buffer,
1370 GLint drawbuffer,
1371 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001372{
Jamie Madill05b35b22017-10-03 09:01:44 -04001373 if (context->getGLState().isRasterizerDiscardEnabled() ||
1374 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001375 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001376 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001377 }
1378
Jamie Madill05b35b22017-10-03 09:01:44 -04001379 ANGLE_TRY(mImpl->clearBufferfv(context, buffer, drawbuffer, values));
1380
Jamie Madill05b35b22017-10-03 09:01:44 -04001381 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001382}
1383
Jamie Madilld4442552018-02-27 22:03:47 -05001384Error Framebuffer::clearBufferuiv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001385 GLenum buffer,
1386 GLint drawbuffer,
1387 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001388{
Jamie Madill05b35b22017-10-03 09:01:44 -04001389 if (context->getGLState().isRasterizerDiscardEnabled() ||
1390 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001391 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001392 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001393 }
1394
Jamie Madill05b35b22017-10-03 09:01:44 -04001395 ANGLE_TRY(mImpl->clearBufferuiv(context, buffer, drawbuffer, values));
1396
Jamie Madill05b35b22017-10-03 09:01:44 -04001397 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001398}
1399
Jamie Madilld4442552018-02-27 22:03:47 -05001400Error Framebuffer::clearBufferiv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001401 GLenum buffer,
1402 GLint drawbuffer,
1403 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001404{
Jamie Madill05b35b22017-10-03 09:01:44 -04001405 if (context->getGLState().isRasterizerDiscardEnabled() ||
1406 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001407 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001408 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001409 }
1410
Jamie Madill05b35b22017-10-03 09:01:44 -04001411 ANGLE_TRY(mImpl->clearBufferiv(context, buffer, drawbuffer, values));
1412
Jamie Madill05b35b22017-10-03 09:01:44 -04001413 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001414}
1415
Jamie Madilld4442552018-02-27 22:03:47 -05001416Error Framebuffer::clearBufferfi(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001417 GLenum buffer,
1418 GLint drawbuffer,
1419 GLfloat depth,
1420 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -05001421{
Jamie Madill05b35b22017-10-03 09:01:44 -04001422 if (context->getGLState().isRasterizerDiscardEnabled() ||
1423 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001424 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001425 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001426 }
1427
Jamie Madill05b35b22017-10-03 09:01:44 -04001428 ANGLE_TRY(mImpl->clearBufferfi(context, buffer, drawbuffer, depth, stencil));
1429
Jamie Madill05b35b22017-10-03 09:01:44 -04001430 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001431}
1432
Jamie Madill690c8eb2018-03-12 15:20:03 -04001433Error Framebuffer::getImplementationColorReadFormat(const Context *context, GLenum *formatOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001434{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001435 ANGLE_TRY(syncState(context));
1436 *formatOut = mImpl->getImplementationColorReadFormat(context);
1437 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -05001438}
1439
Jamie Madill690c8eb2018-03-12 15:20:03 -04001440Error Framebuffer::getImplementationColorReadType(const Context *context, GLenum *typeOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001441{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001442 ANGLE_TRY(syncState(context));
1443 *typeOut = mImpl->getImplementationColorReadType(context);
1444 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -05001445}
1446
Jamie Madilld4442552018-02-27 22:03:47 -05001447Error Framebuffer::readPixels(const Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001448 const Rectangle &area,
Jamie Madill1b94d432015-08-07 13:23:23 -04001449 GLenum format,
1450 GLenum type,
Jamie Madill05b35b22017-10-03 09:01:44 -04001451 void *pixels)
Geoff Langbce529e2014-12-01 12:48:41 -05001452{
Jamie Madill05b35b22017-10-03 09:01:44 -04001453 ANGLE_TRY(ensureReadAttachmentInitialized(context, GL_COLOR_BUFFER_BIT));
Jamie Madill362876b2016-06-16 14:46:59 -04001454 ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels));
Geoff Lang520c4ae2015-05-05 13:12:36 -04001455
Jamie Madilld4442552018-02-27 22:03:47 -05001456 Buffer *unpackBuffer = context->getGLState().getTargetBuffer(BufferBinding::PixelUnpack);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001457 if (unpackBuffer)
1458 {
Jamie Madill09463932018-04-04 05:26:59 -04001459 unpackBuffer->onPixelPack(context);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001460 }
1461
Jamie Madill362876b2016-06-16 14:46:59 -04001462 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -05001463}
1464
Jamie Madilld4442552018-02-27 22:03:47 -05001465Error Framebuffer::blit(const Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001466 const Rectangle &sourceArea,
1467 const Rectangle &destArea,
Geoff Lang242468f2015-09-24 14:15:41 -04001468 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -04001469 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -05001470{
He Yunchao6be602d2016-12-22 14:33:07 +08001471 GLbitfield blitMask = mask;
1472
1473 // Note that blitting is called against draw framebuffer.
1474 // See the code in gl::Context::blitFramebuffer.
1475 if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer())
1476 {
1477 blitMask &= ~GL_COLOR_BUFFER_BIT;
1478 }
1479
1480 if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr)
1481 {
1482 blitMask &= ~GL_STENCIL_BUFFER_BIT;
1483 }
1484
1485 if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr)
1486 {
1487 blitMask &= ~GL_DEPTH_BUFFER_BIT;
1488 }
1489
1490 if (!blitMask)
1491 {
1492 return NoError();
1493 }
1494
Jamie Madill05b35b22017-10-03 09:01:44 -04001495 auto *sourceFBO = context->getGLState().getReadFramebuffer();
1496 ANGLE_TRY(sourceFBO->ensureReadAttachmentInitialized(context, blitMask));
1497
1498 // TODO(jmadill): Only clear if not the full FBO dimensions, and only specified bitmask.
1499 ANGLE_TRY(ensureDrawAttachmentsInitialized(context));
1500
He Yunchao6be602d2016-12-22 14:33:07 +08001501 return mImpl->blit(context, sourceArea, destArea, blitMask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -05001502}
1503
Luc Ferronbf6dc372018-06-28 15:24:19 -04001504bool Framebuffer::isDefault() const
1505{
1506 return id() == 0;
1507}
1508
Jamie Madill427064d2018-04-13 16:20:34 -04001509int Framebuffer::getSamples(const Context *context)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001510{
Jamie Madill427064d2018-04-13 16:20:34 -04001511 return (isComplete(context) ? getCachedSamples(context) : 0);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001512}
1513
Jamie Madill9c335862017-07-18 11:51:38 -04001514int Framebuffer::getCachedSamples(const Context *context)
1515{
Jamie Madill5b772312018-03-08 20:28:32 -05001516 ASSERT(mCachedStatus.valid() && mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE);
1517
Jamie Madill9c335862017-07-18 11:51:38 -04001518 // For a complete framebuffer, all attachments must have the same sample count.
1519 // In this case return the first nonzero sample size.
1520 const auto *firstNonNullAttachment = mState.getFirstNonNullAttachment();
1521 if (firstNonNullAttachment)
1522 {
1523 ASSERT(firstNonNullAttachment->isAttached());
1524 return firstNonNullAttachment->getSamples();
1525 }
1526
1527 // No attachments found.
1528 return 0;
1529}
1530
Geoff Lang13455072018-05-09 11:24:43 -04001531Error Framebuffer::getSamplePosition(const Context *context, size_t index, GLfloat *xy) const
Corentin Wallezccab69d2017-01-27 16:57:15 -05001532{
Geoff Lang13455072018-05-09 11:24:43 -04001533 ANGLE_TRY(mImpl->getSamplePosition(context, index, xy));
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001534 return NoError();
Corentin Wallezccab69d2017-01-27 16:57:15 -05001535}
1536
Jamie Madille261b442014-06-25 12:42:21 -04001537bool Framebuffer::hasValidDepthStencil() const
1538{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001539 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -04001540}
1541
Jamie Madilla02315b2017-02-23 14:14:47 -05001542void Framebuffer::setAttachment(const Context *context,
1543 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -04001544 GLenum binding,
1545 const ImageIndex &textureIndex,
1546 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -04001547{
Martin Radev5dae57b2017-07-14 16:15:55 +03001548 setAttachment(context, type, binding, textureIndex, resource,
1549 FramebufferAttachment::kDefaultNumViews,
1550 FramebufferAttachment::kDefaultBaseViewIndex,
1551 FramebufferAttachment::kDefaultMultiviewLayout,
1552 FramebufferAttachment::kDefaultViewportOffsets);
1553}
1554
1555void Framebuffer::setAttachment(const Context *context,
1556 GLenum type,
1557 GLenum binding,
1558 const ImageIndex &textureIndex,
1559 FramebufferAttachmentObject *resource,
1560 GLsizei numViews,
1561 GLuint baseViewIndex,
1562 GLenum multiviewLayout,
1563 const GLint *viewportOffsets)
1564{
Jamie Madilla02315b2017-02-23 14:14:47 -05001565 // Context may be null in unit tests.
1566 if (!context || !context->isWebGL1())
1567 {
Martin Radev5dae57b2017-07-14 16:15:55 +03001568 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1569 multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001570 return;
1571 }
1572
1573 switch (binding)
1574 {
1575 case GL_DEPTH_STENCIL:
1576 case GL_DEPTH_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001577 mState.mWebGLDepthStencilAttachment.attach(context, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001578 resource, numViews, baseViewIndex,
1579 multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001580 break;
1581 case GL_DEPTH:
1582 case GL_DEPTH_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001583 mState.mWebGLDepthAttachment.attach(context, type, binding, textureIndex, resource,
1584 numViews, baseViewIndex, multiviewLayout,
1585 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001586 break;
1587 case GL_STENCIL:
1588 case GL_STENCIL_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001589 mState.mWebGLStencilAttachment.attach(context, type, binding, textureIndex, resource,
1590 numViews, baseViewIndex, multiviewLayout,
1591 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001592 break;
1593 default:
Martin Radev5dae57b2017-07-14 16:15:55 +03001594 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews,
1595 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001596 return;
1597 }
1598
Martin Radev5dae57b2017-07-14 16:15:55 +03001599 commitWebGL1DepthStencilIfConsistent(context, numViews, baseViewIndex, multiviewLayout,
1600 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001601}
1602
Martin Radev82ef7742017-08-08 17:44:58 +03001603void Framebuffer::setAttachmentMultiviewLayered(const Context *context,
1604 GLenum type,
1605 GLenum binding,
1606 const ImageIndex &textureIndex,
1607 FramebufferAttachmentObject *resource,
1608 GLsizei numViews,
1609 GLint baseViewIndex)
1610{
1611 setAttachment(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1612 GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE,
1613 FramebufferAttachment::kDefaultViewportOffsets);
1614}
1615
Martin Radev5dae57b2017-07-14 16:15:55 +03001616void Framebuffer::setAttachmentMultiviewSideBySide(const Context *context,
1617 GLenum type,
1618 GLenum binding,
1619 const ImageIndex &textureIndex,
1620 FramebufferAttachmentObject *resource,
1621 GLsizei numViews,
1622 const GLint *viewportOffsets)
1623{
1624 setAttachment(context, type, binding, textureIndex, resource, numViews,
1625 FramebufferAttachment::kDefaultBaseViewIndex,
1626 GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE, viewportOffsets);
1627}
1628
1629void Framebuffer::commitWebGL1DepthStencilIfConsistent(const Context *context,
1630 GLsizei numViews,
1631 GLuint baseViewIndex,
1632 GLenum multiviewLayout,
1633 const GLint *viewportOffsets)
Jamie Madilla02315b2017-02-23 14:14:47 -05001634{
1635 int count = 0;
1636
1637 std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment,
1638 &mState.mWebGLDepthAttachment,
1639 &mState.mWebGLStencilAttachment}};
1640 for (FramebufferAttachment *attachment : attachments)
1641 {
1642 if (attachment->isAttached())
1643 {
1644 count++;
1645 }
1646 }
1647
1648 mState.mWebGLDepthStencilConsistent = (count <= 1);
1649 if (!mState.mWebGLDepthStencilConsistent)
1650 {
1651 // Inconsistent.
1652 return;
1653 }
1654
Geoff Lange466c552017-03-17 15:24:12 -04001655 auto getImageIndexIfTextureAttachment = [](const FramebufferAttachment &attachment) {
1656 if (attachment.type() == GL_TEXTURE)
1657 {
1658 return attachment.getTextureImageIndex();
1659 }
1660 else
1661 {
Jamie Madillcc129372018-04-12 09:13:18 -04001662 return ImageIndex();
Geoff Lange466c552017-03-17 15:24:12 -04001663 }
1664 };
1665
Jamie Madilla02315b2017-02-23 14:14:47 -05001666 if (mState.mWebGLDepthAttachment.isAttached())
1667 {
1668 const auto &depth = mState.mWebGLDepthAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001669 setAttachmentImpl(context, depth.type(), GL_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001670 getImageIndexIfTextureAttachment(depth), depth.getResource(), numViews,
1671 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madillcc129372018-04-12 09:13:18 -04001672 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
1673 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001674 }
1675 else if (mState.mWebGLStencilAttachment.isAttached())
1676 {
1677 const auto &stencil = mState.mWebGLStencilAttachment;
Jamie Madillcc129372018-04-12 09:13:18 -04001678 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
1679 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001680 setAttachmentImpl(context, stencil.type(), GL_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001681 getImageIndexIfTextureAttachment(stencil), stencil.getResource(),
1682 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001683 }
1684 else if (mState.mWebGLDepthStencilAttachment.isAttached())
1685 {
1686 const auto &depthStencil = mState.mWebGLDepthStencilAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001687 setAttachmentImpl(context, depthStencil.type(), GL_DEPTH_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001688 getImageIndexIfTextureAttachment(depthStencil),
Martin Radev5dae57b2017-07-14 16:15:55 +03001689 depthStencil.getResource(), numViews, baseViewIndex, multiviewLayout,
1690 viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001691 setAttachmentImpl(context, depthStencil.type(), GL_STENCIL_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001692 getImageIndexIfTextureAttachment(depthStencil),
Martin Radev5dae57b2017-07-14 16:15:55 +03001693 depthStencil.getResource(), numViews, baseViewIndex, multiviewLayout,
1694 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001695 }
1696 else
1697 {
Jamie Madillcc129372018-04-12 09:13:18 -04001698 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
1699 baseViewIndex, multiviewLayout, viewportOffsets);
1700 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
1701 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001702 }
1703}
1704
Jamie Madill4928b7c2017-06-20 12:57:39 -04001705void Framebuffer::setAttachmentImpl(const Context *context,
1706 GLenum type,
Jamie Madilla02315b2017-02-23 14:14:47 -05001707 GLenum binding,
1708 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001709 FramebufferAttachmentObject *resource,
1710 GLsizei numViews,
1711 GLuint baseViewIndex,
1712 GLenum multiviewLayout,
1713 const GLint *viewportOffsets)
Jamie Madilla02315b2017-02-23 14:14:47 -05001714{
Jamie Madilla02315b2017-02-23 14:14:47 -05001715 switch (binding)
1716 {
Jamie Madillb8126692017-04-05 11:22:17 -04001717 case GL_DEPTH_STENCIL:
1718 case GL_DEPTH_STENCIL_ATTACHMENT:
1719 {
1720 // ensure this is a legitimate depth+stencil format
1721 FramebufferAttachmentObject *attachmentObj = resource;
1722 if (resource)
1723 {
Jamie Madill4fd95d52017-04-05 11:22:18 -04001724 const Format &format = resource->getAttachmentFormat(binding, textureIndex);
Jamie Madillb8126692017-04-05 11:22:17 -04001725 if (format.info->depthBits == 0 || format.info->stencilBits == 0)
1726 {
1727 // Attaching nullptr detaches the current attachment.
1728 attachmentObj = nullptr;
1729 }
1730 }
1731
Jamie Madill4928b7c2017-06-20 12:57:39 -04001732 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001733 &mDirtyDepthAttachmentBinding, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001734 attachmentObj, numViews, baseViewIndex, multiviewLayout,
1735 viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001736 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001737 &mDirtyStencilAttachmentBinding, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001738 attachmentObj, numViews, baseViewIndex, multiviewLayout,
1739 viewportOffsets);
Jamie Madill42975642017-10-12 12:31:51 -04001740 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001741 }
1742
Jamie Madilla02315b2017-02-23 14:14:47 -05001743 case GL_DEPTH:
1744 case GL_DEPTH_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001745 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001746 &mDirtyDepthAttachmentBinding, type, binding, textureIndex, resource,
1747 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madill2d06b732015-04-20 12:53:28 -04001748 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001749
Jamie Madilla02315b2017-02-23 14:14:47 -05001750 case GL_STENCIL:
1751 case GL_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001752 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001753 &mDirtyStencilAttachmentBinding, type, binding, textureIndex, resource,
1754 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001755 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001756
Jamie Madilla02315b2017-02-23 14:14:47 -05001757 case GL_BACK:
Geoff Lang8170eab2017-09-21 13:59:04 -04001758 updateAttachment(context, &mState.mColorAttachments[0], DIRTY_BIT_COLOR_ATTACHMENT_0,
1759 &mDirtyColorAttachmentBindings[0], type, binding, textureIndex,
1760 resource, numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001761 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001762
Jamie Madilla02315b2017-02-23 14:14:47 -05001763 default:
1764 {
1765 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
1766 ASSERT(colorIndex < mState.mColorAttachments.size());
Jamie Madillb8126692017-04-05 11:22:17 -04001767 size_t dirtyBit = DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001768 updateAttachment(context, &mState.mColorAttachments[colorIndex], dirtyBit,
Jamie Madillb8126692017-04-05 11:22:17 -04001769 &mDirtyColorAttachmentBindings[colorIndex], type, binding,
Martin Radev5dae57b2017-07-14 16:15:55 +03001770 textureIndex, resource, numViews, baseViewIndex, multiviewLayout,
1771 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001772
Corentin Walleze7557742017-06-01 13:09:57 -04001773 // TODO(jmadill): ASSERT instead of checking the attachment exists in
1774 // formsRenderingFeedbackLoopWith
Jamie Madilla02315b2017-02-23 14:14:47 -05001775 bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE);
1776 mState.mEnabledDrawBuffers.set(colorIndex, enabled);
Brandon Jones76746f92017-11-22 11:44:41 -08001777 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(colorIndex), colorIndex);
Jamie Madill2d06b732015-04-20 12:53:28 -04001778 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001779 break;
Geoff Langab75a052014-10-15 12:56:37 -04001780 }
1781}
1782
Jamie Madill4928b7c2017-06-20 12:57:39 -04001783void Framebuffer::updateAttachment(const Context *context,
1784 FramebufferAttachment *attachment,
Jamie Madillb8126692017-04-05 11:22:17 -04001785 size_t dirtyBit,
Jamie Madilld4442552018-02-27 22:03:47 -05001786 angle::ObserverBinding *onDirtyBinding,
Jamie Madillb8126692017-04-05 11:22:17 -04001787 GLenum type,
1788 GLenum binding,
1789 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001790 FramebufferAttachmentObject *resource,
1791 GLsizei numViews,
1792 GLuint baseViewIndex,
1793 GLenum multiviewLayout,
1794 const GLint *viewportOffsets)
Jamie Madillb8126692017-04-05 11:22:17 -04001795{
Martin Radev5dae57b2017-07-14 16:15:55 +03001796 attachment->attach(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1797 multiviewLayout, viewportOffsets);
Jamie Madillb8126692017-04-05 11:22:17 -04001798 mDirtyBits.set(dirtyBit);
Jamie Madill05b35b22017-10-03 09:01:44 -04001799 mState.mResourceNeedsInit.set(dirtyBit, attachment->initState() == InitState::MayNeedInit);
Jamie Madill888081d2018-02-27 00:24:46 -05001800 onDirtyBinding->bind(resource ? resource->getSubject() : nullptr);
Jamie Madille98b1b52018-03-08 09:47:23 -05001801
Jamie Madillb983a4b2018-08-01 11:34:51 -04001802 invalidateCompletenessCache(context);
Jamie Madillb8126692017-04-05 11:22:17 -04001803}
1804
Jamie Madilla02315b2017-02-23 14:14:47 -05001805void Framebuffer::resetAttachment(const Context *context, GLenum binding)
Jamie Madill2d06b732015-04-20 12:53:28 -04001806{
Jamie Madillcc129372018-04-12 09:13:18 -04001807 setAttachment(context, GL_NONE, binding, ImageIndex(), nullptr);
Jamie Madill2d06b732015-04-20 12:53:28 -04001808}
1809
Jamie Madill6f755b22018-10-09 12:48:54 -04001810angle::Result Framebuffer::syncState(const Context *context)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001811{
1812 if (mDirtyBits.any())
1813 {
Jamie Madill888081d2018-02-27 00:24:46 -05001814 mDirtyBitsGuard = mDirtyBits;
Jamie Madill19fa1c62018-03-08 09:47:21 -05001815 ANGLE_TRY(mImpl->syncState(context, mDirtyBits));
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001816 mDirtyBits.reset();
Jamie Madill888081d2018-02-27 00:24:46 -05001817 mDirtyBitsGuard.reset();
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001818 }
Jamie Madill6f755b22018-10-09 12:48:54 -04001819 return angle::Result::Continue();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001821
Jamie Madilld4442552018-02-27 22:03:47 -05001822void Framebuffer::onSubjectStateChange(const Context *context,
1823 angle::SubjectIndex index,
1824 angle::SubjectMessage message)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001825{
Jamie Madillf668a4b2018-09-23 17:01:20 -04001826 if (message != angle::SubjectMessage::STORAGE_CHANGED)
1827 {
1828 // This can be triggered by the GL back-end TextureGL class.
1829 ASSERT(message == angle::SubjectMessage::DEPENDENT_DIRTY_BITS);
1830 return;
1831 }
Jamie Madill55e57f92018-09-18 11:32:43 -04001832
1833 ASSERT(!mDirtyBitsGuard.valid() || mDirtyBitsGuard.value().test(index));
1834 mDirtyBits.set(index);
1835 onStateChange(context, angle::SubjectMessage::STORAGE_CHANGED);
Jamie Madill888081d2018-02-27 00:24:46 -05001836
Jamie Madillb983a4b2018-08-01 11:34:51 -04001837 invalidateCompletenessCache(context);
Jamie Madill05b35b22017-10-03 09:01:44 -04001838
Jamie Madilld4442552018-02-27 22:03:47 -05001839 FramebufferAttachment *attachment = getAttachmentFromSubjectIndex(index);
1840
Jamie Madill05b35b22017-10-03 09:01:44 -04001841 // Mark the appropriate init flag.
Jamie Madilld4442552018-02-27 22:03:47 -05001842 mState.mResourceNeedsInit.set(index, attachment->initState() == InitState::MayNeedInit);
1843}
1844
1845FramebufferAttachment *Framebuffer::getAttachmentFromSubjectIndex(angle::SubjectIndex index)
1846{
1847 switch (index)
1848 {
1849 case DIRTY_BIT_DEPTH_ATTACHMENT:
1850 return &mState.mDepthAttachment;
1851 case DIRTY_BIT_STENCIL_ATTACHMENT:
1852 return &mState.mStencilAttachment;
1853 default:
1854 size_t colorIndex = (index - DIRTY_BIT_COLOR_ATTACHMENT_0);
1855 ASSERT(colorIndex < mState.mColorAttachments.size());
1856 return &mState.mColorAttachments[colorIndex];
1857 }
Jamie Madill51f40ec2016-06-15 14:06:00 -04001858}
1859
Jamie Madilla4595b82017-01-11 17:36:34 -05001860bool Framebuffer::formsRenderingFeedbackLoopWith(const State &state) const
1861{
Jamie Madill785e8a02018-10-04 17:42:00 -04001862 const Program *program = state.getProgram();
Jamie Madilla4595b82017-01-11 17:36:34 -05001863
1864 // TODO(jmadill): Default framebuffer feedback loops.
Jamie Madill2274b652018-05-31 10:56:08 -04001865 if (mState.mId == 0)
Jamie Madilla4595b82017-01-11 17:36:34 -05001866 {
1867 return false;
1868 }
1869
1870 // The bitset will skip inactive draw buffers.
Jamie Madill6de51852017-04-12 09:53:01 -04001871 for (size_t drawIndex : mState.mEnabledDrawBuffers)
Jamie Madilla4595b82017-01-11 17:36:34 -05001872 {
Jamie Madill5aca1932017-07-21 12:22:01 -04001873 const FramebufferAttachment &attachment = mState.mColorAttachments[drawIndex];
1874 ASSERT(attachment.isAttached());
1875 if (attachment.type() == GL_TEXTURE)
Jamie Madilla4595b82017-01-11 17:36:34 -05001876 {
1877 // Validate the feedback loop.
Jamie Madill5aca1932017-07-21 12:22:01 -04001878 if (program->samplesFromTexture(state, attachment.id()))
Jamie Madilla4595b82017-01-11 17:36:34 -05001879 {
1880 return true;
1881 }
1882 }
1883 }
1884
Jamie Madill38fe6842018-09-19 07:20:00 -04001885 // Validate depth-stencil feedback loop. This is independent of Depth/Stencil state.
Jamie Madill1d37bc52017-02-02 19:59:58 -05001886 const FramebufferAttachment *depth = getDepthbuffer();
Jamie Madill38fe6842018-09-19 07:20:00 -04001887 if (depth && depth->type() == GL_TEXTURE)
Jamie Madill1d37bc52017-02-02 19:59:58 -05001888 {
1889 if (program->samplesFromTexture(state, depth->id()))
1890 {
1891 return true;
1892 }
1893 }
1894
Jamie Madill1d37bc52017-02-02 19:59:58 -05001895 const FramebufferAttachment *stencil = getStencilbuffer();
Jamie Madill38fe6842018-09-19 07:20:00 -04001896 if (stencil && stencil->type() == GL_TEXTURE)
Jamie Madill1d37bc52017-02-02 19:59:58 -05001897 {
Jamie Madill38fe6842018-09-19 07:20:00 -04001898 // Skip the feedback loop check if depth/stencil point to the same resource.
1899 if (!depth || *stencil != *depth)
Jamie Madill1d37bc52017-02-02 19:59:58 -05001900 {
Jamie Madill38fe6842018-09-19 07:20:00 -04001901 if (program->samplesFromTexture(state, stencil->id()))
Jamie Madill1d37bc52017-02-02 19:59:58 -05001902 {
Jamie Madill38fe6842018-09-19 07:20:00 -04001903 return true;
Jamie Madill1d37bc52017-02-02 19:59:58 -05001904 }
1905 }
1906 }
1907
Jamie Madilla4595b82017-01-11 17:36:34 -05001908 return false;
1909}
1910
Jamie Madillfd3dd432017-02-02 19:59:59 -05001911bool Framebuffer::formsCopyingFeedbackLoopWith(GLuint copyTextureID,
1912 GLint copyTextureLevel,
1913 GLint copyTextureLayer) const
Jamie Madillf695a3a2017-01-11 17:36:35 -05001914{
Jamie Madill2274b652018-05-31 10:56:08 -04001915 if (mState.mId == 0)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001916 {
1917 // It seems impossible to form a texture copying feedback loop with the default FBO.
1918 return false;
1919 }
1920
1921 const FramebufferAttachment *readAttachment = getReadColorbuffer();
1922 ASSERT(readAttachment);
1923
1924 if (readAttachment->isTextureWithId(copyTextureID))
1925 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001926 const auto &imageIndex = readAttachment->getTextureImageIndex();
Jamie Madillcc129372018-04-12 09:13:18 -04001927 if (imageIndex.getLevelIndex() == copyTextureLevel)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001928 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001929 // Check 3D/Array texture layers.
Jamie Madillcc129372018-04-12 09:13:18 -04001930 return !imageIndex.hasLayer() || copyTextureLayer == ImageIndex::kEntireLevel ||
1931 imageIndex.getLayerIndex() == copyTextureLayer;
Jamie Madillf695a3a2017-01-11 17:36:35 -05001932 }
1933 }
1934 return false;
1935}
1936
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001937GLint Framebuffer::getDefaultWidth() const
1938{
1939 return mState.getDefaultWidth();
1940}
1941
1942GLint Framebuffer::getDefaultHeight() const
1943{
1944 return mState.getDefaultHeight();
1945}
1946
1947GLint Framebuffer::getDefaultSamples() const
1948{
1949 return mState.getDefaultSamples();
1950}
1951
Geoff Lang92019432017-11-20 13:09:34 -05001952bool Framebuffer::getDefaultFixedSampleLocations() const
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001953{
1954 return mState.getDefaultFixedSampleLocations();
1955}
1956
Jiawei Shaob1e91382018-05-17 14:33:55 +08001957GLint Framebuffer::getDefaultLayers() const
1958{
1959 return mState.getDefaultLayers();
1960}
1961
Jamie Madillb983a4b2018-08-01 11:34:51 -04001962void Framebuffer::setDefaultWidth(const Context *context, GLint defaultWidth)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001963{
1964 mState.mDefaultWidth = defaultWidth;
1965 mDirtyBits.set(DIRTY_BIT_DEFAULT_WIDTH);
Jamie Madillb983a4b2018-08-01 11:34:51 -04001966 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001967}
1968
Jamie Madillb983a4b2018-08-01 11:34:51 -04001969void Framebuffer::setDefaultHeight(const Context *context, GLint defaultHeight)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001970{
1971 mState.mDefaultHeight = defaultHeight;
1972 mDirtyBits.set(DIRTY_BIT_DEFAULT_HEIGHT);
Jamie Madillb983a4b2018-08-01 11:34:51 -04001973 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001974}
1975
Jamie Madillb983a4b2018-08-01 11:34:51 -04001976void Framebuffer::setDefaultSamples(const Context *context, GLint defaultSamples)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001977{
1978 mState.mDefaultSamples = defaultSamples;
1979 mDirtyBits.set(DIRTY_BIT_DEFAULT_SAMPLES);
Jamie Madillb983a4b2018-08-01 11:34:51 -04001980 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001981}
1982
Jamie Madillb983a4b2018-08-01 11:34:51 -04001983void Framebuffer::setDefaultFixedSampleLocations(const Context *context,
1984 bool defaultFixedSampleLocations)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001985{
1986 mState.mDefaultFixedSampleLocations = defaultFixedSampleLocations;
1987 mDirtyBits.set(DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS);
Jamie Madillb983a4b2018-08-01 11:34:51 -04001988 invalidateCompletenessCache(context);
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001989}
1990
Jiawei Shaob1e91382018-05-17 14:33:55 +08001991void Framebuffer::setDefaultLayers(GLint defaultLayers)
1992{
1993 mState.mDefaultLayers = defaultLayers;
1994 mDirtyBits.set(DIRTY_BIT_DEFAULT_LAYERS);
1995}
1996
Martin Radev14a26ae2017-07-24 15:56:29 +03001997GLsizei Framebuffer::getNumViews() const
1998{
Martin Radev5c00d0d2017-08-07 10:06:59 +03001999 return mState.getNumViews();
Martin Radev14a26ae2017-07-24 15:56:29 +03002000}
2001
Martin Radev4e619f52017-08-09 11:50:06 +03002002GLint Framebuffer::getBaseViewIndex() const
2003{
2004 return mState.getBaseViewIndex();
2005}
2006
Martin Radev878c8b12017-07-28 09:51:04 +03002007const std::vector<Offset> *Framebuffer::getViewportOffsets() const
2008{
Martin Radev5c00d0d2017-08-07 10:06:59 +03002009 return mState.getViewportOffsets();
Martin Radev878c8b12017-07-28 09:51:04 +03002010}
2011
2012GLenum Framebuffer::getMultiviewLayout() const
2013{
Martin Radev5c00d0d2017-08-07 10:06:59 +03002014 return mState.getMultiviewLayout();
Martin Radev878c8b12017-07-28 09:51:04 +03002015}
2016
Olli Etuaho8acb1b62018-07-30 16:20:54 +03002017bool Framebuffer::readDisallowedByMultiview() const
2018{
2019 return (mState.getMultiviewLayout() != GL_NONE && mState.getNumViews() > 1) ||
2020 mState.getMultiviewLayout() == GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE;
2021}
2022
Geoff Langd4fff502017-09-22 11:28:28 -04002023Error Framebuffer::ensureClearAttachmentsInitialized(const Context *context, GLbitfield mask)
2024{
2025 const auto &glState = context->getGLState();
2026 if (!context->isRobustResourceInitEnabled() || glState.isRasterizerDiscardEnabled())
2027 {
2028 return NoError();
2029 }
2030
Geoff Langa36483f2018-03-09 16:11:21 -05002031 const BlendState &blend = glState.getBlendState();
2032 const DepthStencilState &depthStencil = glState.getDepthStencilState();
Geoff Langd4fff502017-09-22 11:28:28 -04002033
2034 bool color = (mask & GL_COLOR_BUFFER_BIT) != 0 && !IsColorMaskedOut(blend);
2035 bool depth = (mask & GL_DEPTH_BUFFER_BIT) != 0 && !IsDepthMaskedOut(depthStencil);
2036 bool stencil = (mask & GL_STENCIL_BUFFER_BIT) != 0 && !IsStencilMaskedOut(depthStencil);
2037
2038 if (!color && !depth && !stencil)
2039 {
2040 return NoError();
2041 }
2042
2043 if (partialClearNeedsInit(context, color, depth, stencil))
2044 {
2045 ANGLE_TRY(ensureDrawAttachmentsInitialized(context));
2046 }
2047
2048 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2049 // still be marked initialized. This simplifies design, allowing this method to be called before
2050 // the clear.
2051 markDrawAttachmentsInitialized(color, depth, stencil);
2052
2053 return NoError();
2054}
2055
2056Error Framebuffer::ensureClearBufferAttachmentsInitialized(const Context *context,
2057 GLenum buffer,
2058 GLint drawbuffer)
2059{
2060 if (!context->isRobustResourceInitEnabled() ||
2061 context->getGLState().isRasterizerDiscardEnabled() ||
2062 IsClearBufferMaskedOut(context, buffer))
2063 {
2064 return NoError();
2065 }
2066
2067 if (partialBufferClearNeedsInit(context, buffer))
2068 {
2069 ANGLE_TRY(ensureBufferInitialized(context, buffer, drawbuffer));
2070 }
2071
2072 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2073 // still be marked initialized. This simplifies design, allowing this method to be called before
2074 // the clear.
2075 markBufferInitialized(buffer, drawbuffer);
2076
2077 return NoError();
2078}
2079
Jamie Madill6f755b22018-10-09 12:48:54 -04002080angle::Result Framebuffer::ensureDrawAttachmentsInitialized(const Context *context)
Jamie Madill05b35b22017-10-03 09:01:44 -04002081{
2082 if (!context->isRobustResourceInitEnabled())
2083 {
Jamie Madill6f755b22018-10-09 12:48:54 -04002084 return angle::Result::Continue();
Jamie Madill05b35b22017-10-03 09:01:44 -04002085 }
2086
2087 // Note: we don't actually filter by the draw attachment enum. Just init everything.
2088 for (size_t bit : mState.mResourceNeedsInit)
2089 {
2090 switch (bit)
2091 {
2092 case DIRTY_BIT_DEPTH_ATTACHMENT:
2093 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2094 break;
2095 case DIRTY_BIT_STENCIL_ATTACHMENT:
2096 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2097 break;
2098 default:
2099 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bit]));
2100 break;
2101 }
2102 }
2103
2104 mState.mResourceNeedsInit.reset();
Jamie Madill6f755b22018-10-09 12:48:54 -04002105 return angle::Result::Continue();
Jamie Madill05b35b22017-10-03 09:01:44 -04002106}
2107
2108Error Framebuffer::ensureReadAttachmentInitialized(const Context *context, GLbitfield blitMask)
2109{
2110 if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none())
2111 {
2112 return NoError();
2113 }
2114
2115 if ((blitMask & GL_COLOR_BUFFER_BIT) != 0 && mState.mReadBufferState != GL_NONE)
2116 {
2117 size_t readIndex = mState.getReadIndex();
2118 if (mState.mResourceNeedsInit[readIndex])
2119 {
2120 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[readIndex]));
2121 mState.mResourceNeedsInit.reset(readIndex);
2122 }
2123 }
2124
2125 if ((blitMask & GL_DEPTH_BUFFER_BIT) != 0 && hasDepth())
2126 {
2127 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2128 {
2129 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2130 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2131 }
2132 }
2133
2134 if ((blitMask & GL_STENCIL_BUFFER_BIT) != 0 && hasStencil())
2135 {
2136 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2137 {
2138 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2139 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2140 }
2141 }
2142
2143 return NoError();
2144}
2145
2146void Framebuffer::markDrawAttachmentsInitialized(bool color, bool depth, bool stencil)
2147{
2148 // Mark attachments as initialized.
2149 if (color)
2150 {
2151 for (auto colorIndex : mState.mEnabledDrawBuffers)
2152 {
2153 auto &colorAttachment = mState.mColorAttachments[colorIndex];
2154 ASSERT(colorAttachment.isAttached());
2155 colorAttachment.setInitState(InitState::Initialized);
2156 mState.mResourceNeedsInit.reset(colorIndex);
2157 }
2158 }
2159
2160 if (depth && mState.mDepthAttachment.isAttached())
2161 {
2162 mState.mDepthAttachment.setInitState(InitState::Initialized);
2163 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2164 }
2165
2166 if (stencil && mState.mStencilAttachment.isAttached())
2167 {
2168 mState.mStencilAttachment.setInitState(InitState::Initialized);
2169 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2170 }
2171}
2172
2173void Framebuffer::markBufferInitialized(GLenum bufferType, GLint bufferIndex)
2174{
2175 switch (bufferType)
2176 {
2177 case GL_COLOR:
2178 {
2179 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2180 if (mState.mColorAttachments[bufferIndex].isAttached())
2181 {
2182 mState.mColorAttachments[bufferIndex].setInitState(InitState::Initialized);
2183 mState.mResourceNeedsInit.reset(bufferIndex);
2184 }
2185 break;
2186 }
2187 case GL_DEPTH:
2188 {
2189 if (mState.mDepthAttachment.isAttached())
2190 {
2191 mState.mDepthAttachment.setInitState(InitState::Initialized);
2192 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2193 }
2194 break;
2195 }
2196 case GL_STENCIL:
2197 {
2198 if (mState.mStencilAttachment.isAttached())
2199 {
2200 mState.mStencilAttachment.setInitState(InitState::Initialized);
2201 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2202 }
2203 break;
2204 }
2205 case GL_DEPTH_STENCIL:
2206 {
2207 if (mState.mDepthAttachment.isAttached())
2208 {
2209 mState.mDepthAttachment.setInitState(InitState::Initialized);
2210 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2211 }
2212 if (mState.mStencilAttachment.isAttached())
2213 {
2214 mState.mStencilAttachment.setInitState(InitState::Initialized);
2215 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2216 }
2217 break;
2218 }
2219 default:
2220 UNREACHABLE();
2221 break;
2222 }
2223}
2224
2225Box Framebuffer::getDimensions() const
2226{
2227 return mState.getDimensions();
2228}
2229
2230Error Framebuffer::ensureBufferInitialized(const Context *context,
2231 GLenum bufferType,
2232 GLint bufferIndex)
2233{
2234 ASSERT(context->isRobustResourceInitEnabled());
2235
2236 if (mState.mResourceNeedsInit.none())
2237 {
2238 return NoError();
2239 }
2240
2241 switch (bufferType)
2242 {
2243 case GL_COLOR:
2244 {
2245 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2246 if (mState.mResourceNeedsInit[bufferIndex])
2247 {
2248 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bufferIndex]));
2249 mState.mResourceNeedsInit.reset(bufferIndex);
2250 }
2251 break;
2252 }
2253 case GL_DEPTH:
2254 {
2255 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2256 {
2257 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2258 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2259 }
2260 break;
2261 }
2262 case GL_STENCIL:
2263 {
2264 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2265 {
2266 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2267 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2268 }
2269 break;
2270 }
2271 case GL_DEPTH_STENCIL:
2272 {
2273 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2274 {
2275 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2276 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2277 }
2278 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2279 {
2280 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2281 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2282 }
2283 break;
2284 }
2285 default:
2286 UNREACHABLE();
2287 break;
2288 }
2289
2290 return NoError();
2291}
2292
2293bool Framebuffer::partialBufferClearNeedsInit(const Context *context, GLenum bufferType)
2294{
2295 if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none())
2296 {
2297 return false;
2298 }
2299
2300 switch (bufferType)
2301 {
2302 case GL_COLOR:
2303 return partialClearNeedsInit(context, true, false, false);
2304 case GL_DEPTH:
2305 return partialClearNeedsInit(context, false, true, false);
2306 case GL_STENCIL:
2307 return partialClearNeedsInit(context, false, false, true);
2308 case GL_DEPTH_STENCIL:
2309 return partialClearNeedsInit(context, false, true, true);
2310 default:
2311 UNREACHABLE();
2312 return false;
2313 }
2314}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002315} // namespace gl