blob: 12e805f89ba3a2bc6c83856163c4849000de37b8 [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
218Error InitAttachment(const Context *context, FramebufferAttachment *attachment)
219{
220 ASSERT(attachment->isAttached());
221 if (attachment->initState() == InitState::MayNeedInit)
222 {
223 ANGLE_TRY(attachment->initializeContents(context));
224 }
225 return NoError();
226}
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,
749 resourceId, DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex))
750 {
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 {
762 if (attachment->isAttached() && attachment->type() == resourceType &&
763 attachment->id() == resourceId)
764 {
765 resetAttachment(context, attachment->getBinding());
Jamie Madill8693bdb2017-09-02 15:32:14 -0400766 found = true;
Jamie Madilla02315b2017-02-23 14:14:47 -0500767 }
768 }
769 }
770 else
771 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400772 if (detachMatchingAttachment(context, &mState.mDepthAttachment, resourceType, resourceId,
773 DIRTY_BIT_DEPTH_ATTACHMENT))
774 {
775 found = true;
776 }
777 if (detachMatchingAttachment(context, &mState.mStencilAttachment, resourceType, resourceId,
778 DIRTY_BIT_STENCIL_ATTACHMENT))
779 {
780 found = true;
781 }
Jamie Madilla02315b2017-02-23 14:14:47 -0500782 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400783
784 return found;
Jamie Madill362876b2016-06-16 14:46:59 -0400785}
786
Jamie Madill8693bdb2017-09-02 15:32:14 -0400787bool Framebuffer::detachMatchingAttachment(const Context *context,
Jamie Madill4928b7c2017-06-20 12:57:39 -0400788 FramebufferAttachment *attachment,
Jamie Madill362876b2016-06-16 14:46:59 -0400789 GLenum matchType,
790 GLuint matchId,
791 size_t dirtyBit)
792{
793 if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId)
794 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400795 attachment->detach(context);
Jamie Madill362876b2016-06-16 14:46:59 -0400796 mDirtyBits.set(dirtyBit);
Jamie Madill05b35b22017-10-03 09:01:44 -0400797 mState.mResourceNeedsInit.set(dirtyBit, false);
Jamie Madill8693bdb2017-09-02 15:32:14 -0400798 return true;
Jamie Madill362876b2016-06-16 14:46:59 -0400799 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400800
801 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802}
803
Corentin Wallez37c39792015-08-20 14:19:46 -0400804const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400806 return mState.getColorAttachment(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807}
808
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400809const FramebufferAttachment *Framebuffer::getDepthbuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400810{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400811 return mState.getDepthAttachment();
Geoff Lang646559f2013-08-15 11:08:15 -0400812}
813
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400814const FramebufferAttachment *Framebuffer::getStencilbuffer() const
815{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400816 return mState.getStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400817}
818
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400819const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
820{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400821 return mState.getDepthStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400822}
823
824const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000825{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400826 return mState.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000827}
828
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500829const FramebufferAttachment *Framebuffer::getStencilOrDepthStencilAttachment() const
830{
831 return mState.getStencilOrDepthStencilAttachment();
832}
833
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400834const FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000835{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400836 return mState.getReadAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000837}
838
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000839GLenum Framebuffer::getReadColorbufferType() const
840{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400841 const FramebufferAttachment *readAttachment = mState.getReadAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400842 return (readAttachment != nullptr ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000843}
844
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400845const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000846{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400847 return mState.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000848}
849
Jamie Madill6dd06ea2017-07-19 13:47:55 -0400850const FramebufferAttachment *Framebuffer::getFirstNonNullAttachment() const
851{
852 return mState.getFirstNonNullAttachment();
853}
854
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800855const FramebufferAttachment *Framebuffer::getAttachment(const Context *context,
856 GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000857{
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800858 return mState.getAttachment(context, attachment);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400859}
860
Geoff Langa15472a2015-08-11 11:48:03 -0400861size_t Framebuffer::getDrawbufferStateCount() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000862{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400863 return mState.mDrawBufferStates.size();
Geoff Langa15472a2015-08-11 11:48:03 -0400864}
865
866GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const
867{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400868 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
869 return mState.mDrawBufferStates[drawBuffer];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000870}
871
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500872const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const
873{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400874 return mState.getDrawBufferStates();
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500875}
876
Geoff Lang164d54e2014-12-01 10:55:33 -0500877void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000878{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400879 auto &drawStates = mState.mDrawBufferStates;
Jamie Madilld1405e52015-03-05 15:41:39 -0500880
881 ASSERT(count <= drawStates.size());
882 std::copy(buffers, buffers + count, drawStates.begin());
883 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500884 mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS);
Jamie Madilla4595b82017-01-11 17:36:34 -0500885
886 mState.mEnabledDrawBuffers.reset();
Brandon Jones76746f92017-11-22 11:44:41 -0800887 mState.mDrawBufferTypeMask.reset();
888
Jamie Madilla4595b82017-01-11 17:36:34 -0500889 for (size_t index = 0; index < count; ++index)
890 {
Brandon Jones76746f92017-11-22 11:44:41 -0800891 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(index), index);
892
Jamie Madilla4595b82017-01-11 17:36:34 -0500893 if (drawStates[index] != GL_NONE && mState.mColorAttachments[index].isAttached())
894 {
895 mState.mEnabledDrawBuffers.set(index);
896 }
897 }
Geoff Lang9dd95802014-12-01 11:12:59 -0500898}
899
Geoff Langa15472a2015-08-11 11:48:03 -0400900const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const
901{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400902 return mState.getDrawBuffer(drawBuffer);
Geoff Langa15472a2015-08-11 11:48:03 -0400903}
904
Geoff Lange0cff192017-05-30 13:04:56 -0400905GLenum Framebuffer::getDrawbufferWriteType(size_t drawBuffer) const
906{
907 const FramebufferAttachment *attachment = mState.getDrawBuffer(drawBuffer);
908 if (attachment == nullptr)
909 {
910 return GL_NONE;
911 }
912
913 GLenum componentType = attachment->getFormat().info->componentType;
914 switch (componentType)
915 {
916 case GL_INT:
917 case GL_UNSIGNED_INT:
918 return componentType;
919
920 default:
921 return GL_FLOAT;
922 }
923}
924
Brandon Jonesc405ae72017-12-06 14:15:03 -0800925ComponentTypeMask Framebuffer::getDrawBufferTypeMask() const
Brandon Jones76746f92017-11-22 11:44:41 -0800926{
927 return mState.mDrawBufferTypeMask;
928}
929
930DrawBufferMask Framebuffer::getDrawBufferMask() const
931{
932 return mState.mEnabledDrawBuffers;
933}
934
Geoff Langa15472a2015-08-11 11:48:03 -0400935bool Framebuffer::hasEnabledDrawBuffer() const
936{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400937 for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
Geoff Langa15472a2015-08-11 11:48:03 -0400938 {
939 if (getDrawBuffer(drawbufferIdx) != nullptr)
940 {
941 return true;
942 }
943 }
944
945 return false;
946}
947
Geoff Lang9dd95802014-12-01 11:12:59 -0500948GLenum Framebuffer::getReadBufferState() const
949{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400950 return mState.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500951}
952
953void Framebuffer::setReadBuffer(GLenum buffer)
954{
Jamie Madillb885e572015-02-03 16:16:04 -0500955 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
956 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madill48ef11b2016-04-27 15:21:52 -0400957 (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size()));
958 mState.mReadBufferState = buffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500959 mDirtyBits.set(DIRTY_BIT_READ_BUFFER);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000960}
961
Corentin Wallez37c39792015-08-20 14:19:46 -0400962size_t Framebuffer::getNumColorBuffers() const
963{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400964 return mState.mColorAttachments.size();
Corentin Wallez37c39792015-08-20 14:19:46 -0400965}
966
Jamie Madill0df8fe42015-11-24 16:10:24 -0500967bool Framebuffer::hasDepth() const
968{
Jamie Madill9c335862017-07-18 11:51:38 -0400969 return mState.hasDepth();
Jamie Madill0df8fe42015-11-24 16:10:24 -0500970}
971
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000972bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000973{
Jamie Madill9c335862017-07-18 11:51:38 -0400974 return mState.hasStencil();
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000975}
976
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000977bool Framebuffer::usingExtendedDrawBuffers() const
978{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400979 for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000980 {
Geoff Langa15472a2015-08-11 11:48:03 -0400981 if (getDrawBuffer(drawbufferIdx) != nullptr)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000982 {
983 return true;
984 }
985 }
986
987 return false;
988}
989
Jamie Madillb983a4b2018-08-01 11:34:51 -0400990void Framebuffer::invalidateCompletenessCache(const Context *context)
Geoff Lang9aded172017-04-05 11:07:56 -0400991{
Jamie Madill2274b652018-05-31 10:56:08 -0400992 if (mState.mId != 0)
Geoff Lang9aded172017-04-05 11:07:56 -0400993 {
994 mCachedStatus.reset();
Jamie Madilld84b6732018-09-06 15:54:35 -0400995 onStateChange(context, angle::SubjectMessage::CONTENTS_CHANGED);
Geoff Lang9aded172017-04-05 11:07:56 -0400996 }
997}
998
Jamie Madillcc73f242018-08-01 11:34:48 -0400999GLenum Framebuffer::checkStatusImpl(const Context *context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000{
Jamie Madillcc73f242018-08-01 11:34:48 -04001001 ASSERT(!isDefault());
1002 ASSERT(hasAnyDirtyBit() || !mCachedStatus.valid());
Geoff Lang528ce3c2014-12-01 10:44:07 -05001003
Jamie Madillcc73f242018-08-01 11:34:48 -04001004 mCachedStatus = checkStatusWithGLFrontEnd(context);
Jamie Madille98b1b52018-03-08 09:47:23 -05001005
Jamie Madillcc73f242018-08-01 11:34:48 -04001006 if (mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE)
1007 {
1008 Error err = syncState(context);
1009 if (err.isError())
Jamie Madille98b1b52018-03-08 09:47:23 -05001010 {
Jamie Madillcc73f242018-08-01 11:34:48 -04001011 // TODO(jmadill): Remove when refactor complete. http://anglebug.com/2491
1012 const_cast<Context *>(context)->handleError(err);
1013 return GetDefaultReturnValue<EntryPoint::CheckFramebufferStatus, GLenum>();
1014 }
1015 if (!mImpl->checkStatus(context))
1016 {
1017 mCachedStatus = GL_FRAMEBUFFER_UNSUPPORTED;
Jamie Madille98b1b52018-03-08 09:47:23 -05001018 }
Jamie Madill362876b2016-06-16 14:46:59 -04001019 }
1020
Jamie Madill427064d2018-04-13 16:20:34 -04001021 return mCachedStatus.value();
Jamie Madill362876b2016-06-16 14:46:59 -04001022}
1023
Jamie Madille98b1b52018-03-08 09:47:23 -05001024GLenum Framebuffer::checkStatusWithGLFrontEnd(const Context *context)
Jamie Madill362876b2016-06-16 14:46:59 -04001025{
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001026 const ContextState &state = context->getContextState();
1027
Jamie Madill2274b652018-05-31 10:56:08 -04001028 ASSERT(mState.mId != 0);
Jamie Madill362876b2016-06-16 14:46:59 -04001029
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001030 bool hasAttachments = false;
1031 Optional<unsigned int> colorbufferSize;
1032 Optional<int> samples;
Geoff Lang92019432017-11-20 13:09:34 -05001033 Optional<bool> fixedSampleLocations;
JiangYizhou461d9a32017-01-04 16:37:26 +08001034 bool hasRenderbuffer = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035
Martin Radev9bc9a322017-07-21 14:28:17 +03001036 const FramebufferAttachment *firstAttachment = getFirstNonNullAttachment();
1037
Jiawei Shaoa8802472018-05-28 11:17:47 +08001038 Optional<bool> isLayered;
1039 Optional<TextureType> colorAttachmentsTextureType;
1040
Jamie Madill48ef11b2016-04-27 15:21:52 -04001041 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042 {
Jamie Madill2d06b732015-04-20 12:53:28 -04001043 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001045 if (!CheckAttachmentCompleteness(context, colorAttachment))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001046 {
1047 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1048 }
daniel@transgaming.com01868132010-08-24 19:21:17 +00001049
Geoff Lang677bb6f2017-04-05 12:40:40 -04001050 const InternalFormat &format = *colorAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001051 if (format.depthBits > 0 || format.stencilBits > 0)
1052 {
1053 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1054 }
1055
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001056 if (!CheckAttachmentSampleCompleteness(context, colorAttachment, true, &samples,
1057 &fixedSampleLocations))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001058 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001059 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001060 }
1061
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001062 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
1063 // in GLES 3.0, there is no such restriction
1064 if (state.getClientMajorVersion() < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001065 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001066 if (colorbufferSize.valid())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001067 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001068 if (format.pixelBytes != colorbufferSize.value())
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +00001069 {
1070 return GL_FRAMEBUFFER_UNSUPPORTED;
1071 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001072 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001073 else
1074 {
1075 colorbufferSize = format.pixelBytes;
1076 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001077 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001078
Martin Radev9bc9a322017-07-21 14:28:17 +03001079 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &colorAttachment))
1080 {
1081 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1082 }
1083
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001084 hasRenderbuffer = hasRenderbuffer || (colorAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001085
1086 if (!hasAttachments)
1087 {
1088 isLayered = colorAttachment.isLayered();
1089 if (isLayered.value())
1090 {
1091 colorAttachmentsTextureType = colorAttachment.getTextureImageIndex().getType();
1092 }
1093 hasAttachments = true;
1094 }
1095 else
1096 {
1097 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1098 // If any framebuffer attachment is layered, all populated attachments
1099 // must be layered. Additionally, all populated color attachments must
1100 // be from textures of the same target. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1101 ASSERT(isLayered.valid());
1102 if (isLayered.value() != colorAttachment.isLayered())
1103 {
1104 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1105 }
1106 else if (isLayered.value())
1107 {
1108 ASSERT(colorAttachmentsTextureType.valid());
1109 if (colorAttachmentsTextureType.value() !=
1110 colorAttachment.getTextureImageIndex().getType())
1111 {
1112 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1113 }
1114 }
1115 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001116 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001117 }
1118
Jamie Madill48ef11b2016-04-27 15:21:52 -04001119 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001120 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001121 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001122 if (!CheckAttachmentCompleteness(context, depthAttachment))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001123 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001124 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001125 }
1126
Geoff Lang677bb6f2017-04-05 12:40:40 -04001127 const InternalFormat &format = *depthAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001128 if (format.depthBits == 0)
1129 {
1130 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001131 }
1132
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001133 if (!CheckAttachmentSampleCompleteness(context, depthAttachment, false, &samples,
1134 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001135 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001136 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001137 }
Sami Väisänena797e062016-05-12 15:23:40 +03001138
Martin Radev9bc9a322017-07-21 14:28:17 +03001139 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &depthAttachment))
1140 {
1141 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1142 }
1143
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001144 hasRenderbuffer = hasRenderbuffer || (depthAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001145
1146 if (!hasAttachments)
1147 {
1148 isLayered = depthAttachment.isLayered();
1149 hasAttachments = true;
1150 }
1151 else
1152 {
1153 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1154 // If any framebuffer attachment is layered, all populated attachments
1155 // must be layered. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1156 ASSERT(isLayered.valid());
1157 if (isLayered.value() != depthAttachment.isLayered())
1158 {
1159 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1160 }
1161 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001162 }
1163
Jamie Madill48ef11b2016-04-27 15:21:52 -04001164 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001165 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001166 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001167 if (!CheckAttachmentCompleteness(context, stencilAttachment))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001168 {
1169 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1170 }
1171
Geoff Lang677bb6f2017-04-05 12:40:40 -04001172 const InternalFormat &format = *stencilAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001173 if (format.stencilBits == 0)
1174 {
1175 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001176 }
1177
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001178 if (!CheckAttachmentSampleCompleteness(context, stencilAttachment, false, &samples,
1179 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001180 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001181 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001182 }
Corentin Wallez086d59a2016-04-29 09:06:49 -04001183
Martin Radev9bc9a322017-07-21 14:28:17 +03001184 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &stencilAttachment))
1185 {
1186 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1187 }
1188
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001189 hasRenderbuffer = hasRenderbuffer || (stencilAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001190
1191 if (!hasAttachments)
1192 {
1193 hasAttachments = true;
1194 }
1195 else
1196 {
1197 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1198 // If any framebuffer attachment is layered, all populated attachments
1199 // must be layered.
1200 // {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1201 ASSERT(isLayered.valid());
1202 if (isLayered.value() != stencilAttachment.isLayered())
1203 {
1204 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1205 }
1206 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001207 }
1208
1209 // Starting from ES 3.0 stencil and depth, if present, should be the same image
1210 if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() &&
1211 stencilAttachment.isAttached() && stencilAttachment != depthAttachment)
1212 {
1213 return GL_FRAMEBUFFER_UNSUPPORTED;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001214 }
1215
Jamie Madilla02315b2017-02-23 14:14:47 -05001216 // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL.
1217 if (state.isWebGL1())
1218 {
1219 if (!mState.mWebGLDepthStencilConsistent)
1220 {
1221 return GL_FRAMEBUFFER_UNSUPPORTED;
1222 }
1223
1224 if (mState.mWebGLDepthStencilAttachment.isAttached())
1225 {
1226 if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 ||
1227 mState.mWebGLDepthStencilAttachment.getStencilSize() == 0)
1228 {
1229 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1230 }
Martin Radev9bc9a322017-07-21 14:28:17 +03001231
1232 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment,
1233 &mState.mWebGLDepthStencilAttachment))
1234 {
1235 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1236 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001237 }
1238 else if (mState.mStencilAttachment.isAttached() &&
1239 mState.mStencilAttachment.getDepthSize() > 0)
1240 {
1241 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1242 }
1243 else if (mState.mDepthAttachment.isAttached() &&
1244 mState.mDepthAttachment.getStencilSize() > 0)
1245 {
1246 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1247 }
1248 }
1249
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001250 // ES3.1(section 9.4) requires that if no image is attached to the framebuffer, and either the
1251 // value of the framebuffer's FRAMEBUFFER_DEFAULT_WIDTH or FRAMEBUFFER_DEFAULT_HEIGHT parameters
1252 // is zero, the framebuffer is considered incomplete.
JiangYizhou461d9a32017-01-04 16:37:26 +08001253 GLint defaultWidth = mState.getDefaultWidth();
1254 GLint defaultHeight = mState.getDefaultHeight();
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001255 if (!hasAttachments && (defaultWidth == 0 || defaultHeight == 0))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001256 {
1257 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001258 }
1259
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001260 // In ES 2.0 and WebGL, all color attachments must have the same width and height.
Jamie Madillcc86d642015-11-24 13:00:07 -05001261 // In ES 3.0, there is no such restriction.
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001262 if ((state.getClientMajorVersion() < 3 || state.getExtensions().webglCompatibility) &&
1263 !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -05001264 {
1265 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
1266 }
1267
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001268 // ES3.1(section 9.4) requires that if the attached images are a mix of renderbuffers and
1269 // textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all attached textures.
JiangYizhou461d9a32017-01-04 16:37:26 +08001270 if (fixedSampleLocations.valid() && hasRenderbuffer && !fixedSampleLocations.value())
1271 {
1272 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1273 }
1274
Kenneth Russellce8602a2017-10-03 18:23:08 -07001275 // The WebGL conformance tests implicitly define that all framebuffer
1276 // attachments must be unique. For example, the same level of a texture can
1277 // not be attached to two different color attachments.
1278 if (state.getExtensions().webglCompatibility)
1279 {
1280 if (!mState.colorAttachmentsAreUniqueImages())
1281 {
1282 return GL_FRAMEBUFFER_UNSUPPORTED;
1283 }
1284 }
1285
Jamie Madillcc86d642015-11-24 13:00:07 -05001286 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001288
Jamie Madill4928b7c2017-06-20 12:57:39 -04001289Error Framebuffer::discard(const Context *context, size_t count, const GLenum *attachments)
Austin Kinross08332632015-05-05 13:35:47 -07001290{
Jamie Madill05b35b22017-10-03 09:01:44 -04001291 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1292 // can be no-ops, so we should probably do that to ensure consistency.
1293 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1294
Jamie Madill4928b7c2017-06-20 12:57:39 -04001295 return mImpl->discard(context, count, attachments);
Austin Kinross08332632015-05-05 13:35:47 -07001296}
1297
Jamie Madill4928b7c2017-06-20 12:57:39 -04001298Error Framebuffer::invalidate(const Context *context, size_t count, const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001299{
Jamie Madill05b35b22017-10-03 09:01:44 -04001300 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1301 // can be no-ops, so we should probably do that to ensure consistency.
1302 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1303
Jamie Madill4928b7c2017-06-20 12:57:39 -04001304 return mImpl->invalidate(context, count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001305}
1306
Jamie Madill05b35b22017-10-03 09:01:44 -04001307bool Framebuffer::partialClearNeedsInit(const Context *context,
1308 bool color,
1309 bool depth,
1310 bool stencil)
1311{
1312 const auto &glState = context->getGLState();
1313
1314 if (!glState.isRobustResourceInitEnabled())
1315 {
1316 return false;
1317 }
1318
1319 // Scissors can affect clearing.
1320 // TODO(jmadill): Check for complete scissor overlap.
1321 if (glState.isScissorTestEnabled())
1322 {
1323 return true;
1324 }
1325
1326 // If colors masked, we must clear before we clear. Do a simple check.
1327 // TODO(jmadill): Filter out unused color channels from the test.
1328 if (color)
1329 {
1330 const auto &blend = glState.getBlendState();
1331 if (!(blend.colorMaskRed && blend.colorMaskGreen && blend.colorMaskBlue &&
1332 blend.colorMaskAlpha))
1333 {
1334 return true;
1335 }
1336 }
1337
1338 const auto &depthStencil = glState.getDepthStencilState();
Yuly Novikov21edf3d2018-07-23 16:44:16 -04001339 if (stencil && (depthStencil.stencilMask != depthStencil.stencilWritemask ||
1340 depthStencil.stencilBackMask != depthStencil.stencilBackWritemask))
Jamie Madill05b35b22017-10-03 09:01:44 -04001341 {
1342 return true;
1343 }
1344
1345 return false;
1346}
1347
Jamie Madill4928b7c2017-06-20 12:57:39 -04001348Error Framebuffer::invalidateSub(const Context *context,
1349 size_t count,
1350 const GLenum *attachments,
Jamie Madilld4442552018-02-27 22:03:47 -05001351 const Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -04001352{
Jamie Madill05b35b22017-10-03 09:01:44 -04001353 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1354 // can be no-ops, so we should probably do that to ensure consistency.
1355 // TODO(jmadill): Make a invalidate no-op in WebGL 2.0.
1356
Jamie Madill4928b7c2017-06-20 12:57:39 -04001357 return mImpl->invalidateSub(context, count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -04001358}
1359
Jamie Madilld4442552018-02-27 22:03:47 -05001360Error Framebuffer::clear(const Context *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -05001361{
Jamie Madill05b35b22017-10-03 09:01:44 -04001362 const auto &glState = context->getGLState();
1363 if (glState.isRasterizerDiscardEnabled())
Jamie Madill984ef412015-11-24 16:10:21 -05001364 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001365 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001366 }
1367
Jamie Madill05b35b22017-10-03 09:01:44 -04001368 ANGLE_TRY(mImpl->clear(context, mask));
1369
Jamie Madill05b35b22017-10-03 09:01:44 -04001370 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001371}
1372
Jamie Madilld4442552018-02-27 22:03:47 -05001373Error Framebuffer::clearBufferfv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001374 GLenum buffer,
1375 GLint drawbuffer,
1376 const GLfloat *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001377{
Jamie Madill05b35b22017-10-03 09:01:44 -04001378 if (context->getGLState().isRasterizerDiscardEnabled() ||
1379 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001380 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001381 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001382 }
1383
Jamie Madill05b35b22017-10-03 09:01:44 -04001384 ANGLE_TRY(mImpl->clearBufferfv(context, buffer, drawbuffer, values));
1385
Jamie Madill05b35b22017-10-03 09:01:44 -04001386 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001387}
1388
Jamie Madilld4442552018-02-27 22:03:47 -05001389Error Framebuffer::clearBufferuiv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001390 GLenum buffer,
1391 GLint drawbuffer,
1392 const GLuint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001393{
Jamie Madill05b35b22017-10-03 09:01:44 -04001394 if (context->getGLState().isRasterizerDiscardEnabled() ||
1395 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001396 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001397 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001398 }
1399
Jamie Madill05b35b22017-10-03 09:01:44 -04001400 ANGLE_TRY(mImpl->clearBufferuiv(context, buffer, drawbuffer, values));
1401
Jamie Madill05b35b22017-10-03 09:01:44 -04001402 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001403}
1404
Jamie Madilld4442552018-02-27 22:03:47 -05001405Error Framebuffer::clearBufferiv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001406 GLenum buffer,
1407 GLint drawbuffer,
1408 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001409{
Jamie Madill05b35b22017-10-03 09:01:44 -04001410 if (context->getGLState().isRasterizerDiscardEnabled() ||
1411 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001412 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001413 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001414 }
1415
Jamie Madill05b35b22017-10-03 09:01:44 -04001416 ANGLE_TRY(mImpl->clearBufferiv(context, buffer, drawbuffer, values));
1417
Jamie Madill05b35b22017-10-03 09:01:44 -04001418 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001419}
1420
Jamie Madilld4442552018-02-27 22:03:47 -05001421Error Framebuffer::clearBufferfi(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001422 GLenum buffer,
1423 GLint drawbuffer,
1424 GLfloat depth,
1425 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -05001426{
Jamie Madill05b35b22017-10-03 09:01:44 -04001427 if (context->getGLState().isRasterizerDiscardEnabled() ||
1428 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001429 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001430 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001431 }
1432
Jamie Madill05b35b22017-10-03 09:01:44 -04001433 ANGLE_TRY(mImpl->clearBufferfi(context, buffer, drawbuffer, depth, stencil));
1434
Jamie Madill05b35b22017-10-03 09:01:44 -04001435 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001436}
1437
Jamie Madill690c8eb2018-03-12 15:20:03 -04001438Error Framebuffer::getImplementationColorReadFormat(const Context *context, GLenum *formatOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001439{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001440 ANGLE_TRY(syncState(context));
1441 *formatOut = mImpl->getImplementationColorReadFormat(context);
1442 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -05001443}
1444
Jamie Madill690c8eb2018-03-12 15:20:03 -04001445Error Framebuffer::getImplementationColorReadType(const Context *context, GLenum *typeOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001446{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001447 ANGLE_TRY(syncState(context));
1448 *typeOut = mImpl->getImplementationColorReadType(context);
1449 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -05001450}
1451
Jamie Madilld4442552018-02-27 22:03:47 -05001452Error Framebuffer::readPixels(const Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001453 const Rectangle &area,
Jamie Madill1b94d432015-08-07 13:23:23 -04001454 GLenum format,
1455 GLenum type,
Jamie Madill05b35b22017-10-03 09:01:44 -04001456 void *pixels)
Geoff Langbce529e2014-12-01 12:48:41 -05001457{
Jamie Madill05b35b22017-10-03 09:01:44 -04001458 ANGLE_TRY(ensureReadAttachmentInitialized(context, GL_COLOR_BUFFER_BIT));
Jamie Madill362876b2016-06-16 14:46:59 -04001459 ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels));
Geoff Lang520c4ae2015-05-05 13:12:36 -04001460
Jamie Madilld4442552018-02-27 22:03:47 -05001461 Buffer *unpackBuffer = context->getGLState().getTargetBuffer(BufferBinding::PixelUnpack);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001462 if (unpackBuffer)
1463 {
Jamie Madill09463932018-04-04 05:26:59 -04001464 unpackBuffer->onPixelPack(context);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001465 }
1466
Jamie Madill362876b2016-06-16 14:46:59 -04001467 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -05001468}
1469
Jamie Madilld4442552018-02-27 22:03:47 -05001470Error Framebuffer::blit(const Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001471 const Rectangle &sourceArea,
1472 const Rectangle &destArea,
Geoff Lang242468f2015-09-24 14:15:41 -04001473 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -04001474 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -05001475{
He Yunchao6be602d2016-12-22 14:33:07 +08001476 GLbitfield blitMask = mask;
1477
1478 // Note that blitting is called against draw framebuffer.
1479 // See the code in gl::Context::blitFramebuffer.
1480 if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer())
1481 {
1482 blitMask &= ~GL_COLOR_BUFFER_BIT;
1483 }
1484
1485 if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr)
1486 {
1487 blitMask &= ~GL_STENCIL_BUFFER_BIT;
1488 }
1489
1490 if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr)
1491 {
1492 blitMask &= ~GL_DEPTH_BUFFER_BIT;
1493 }
1494
1495 if (!blitMask)
1496 {
1497 return NoError();
1498 }
1499
Jamie Madill05b35b22017-10-03 09:01:44 -04001500 auto *sourceFBO = context->getGLState().getReadFramebuffer();
1501 ANGLE_TRY(sourceFBO->ensureReadAttachmentInitialized(context, blitMask));
1502
1503 // TODO(jmadill): Only clear if not the full FBO dimensions, and only specified bitmask.
1504 ANGLE_TRY(ensureDrawAttachmentsInitialized(context));
1505
He Yunchao6be602d2016-12-22 14:33:07 +08001506 return mImpl->blit(context, sourceArea, destArea, blitMask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -05001507}
1508
Luc Ferronbf6dc372018-06-28 15:24:19 -04001509bool Framebuffer::isDefault() const
1510{
1511 return id() == 0;
1512}
1513
Jamie Madill427064d2018-04-13 16:20:34 -04001514int Framebuffer::getSamples(const Context *context)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001515{
Jamie Madill427064d2018-04-13 16:20:34 -04001516 return (isComplete(context) ? getCachedSamples(context) : 0);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001517}
1518
Jamie Madill9c335862017-07-18 11:51:38 -04001519int Framebuffer::getCachedSamples(const Context *context)
1520{
Jamie Madill5b772312018-03-08 20:28:32 -05001521 ASSERT(mCachedStatus.valid() && mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE);
1522
Jamie Madill9c335862017-07-18 11:51:38 -04001523 // For a complete framebuffer, all attachments must have the same sample count.
1524 // In this case return the first nonzero sample size.
1525 const auto *firstNonNullAttachment = mState.getFirstNonNullAttachment();
1526 if (firstNonNullAttachment)
1527 {
1528 ASSERT(firstNonNullAttachment->isAttached());
1529 return firstNonNullAttachment->getSamples();
1530 }
1531
1532 // No attachments found.
1533 return 0;
1534}
1535
Geoff Lang13455072018-05-09 11:24:43 -04001536Error Framebuffer::getSamplePosition(const Context *context, size_t index, GLfloat *xy) const
Corentin Wallezccab69d2017-01-27 16:57:15 -05001537{
Geoff Lang13455072018-05-09 11:24:43 -04001538 ANGLE_TRY(mImpl->getSamplePosition(context, index, xy));
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001539 return NoError();
Corentin Wallezccab69d2017-01-27 16:57:15 -05001540}
1541
Jamie Madille261b442014-06-25 12:42:21 -04001542bool Framebuffer::hasValidDepthStencil() const
1543{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001544 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -04001545}
1546
Jamie Madilla02315b2017-02-23 14:14:47 -05001547void Framebuffer::setAttachment(const Context *context,
1548 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -04001549 GLenum binding,
1550 const ImageIndex &textureIndex,
1551 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -04001552{
Martin Radev5dae57b2017-07-14 16:15:55 +03001553 setAttachment(context, type, binding, textureIndex, resource,
1554 FramebufferAttachment::kDefaultNumViews,
1555 FramebufferAttachment::kDefaultBaseViewIndex,
1556 FramebufferAttachment::kDefaultMultiviewLayout,
1557 FramebufferAttachment::kDefaultViewportOffsets);
1558}
1559
1560void Framebuffer::setAttachment(const Context *context,
1561 GLenum type,
1562 GLenum binding,
1563 const ImageIndex &textureIndex,
1564 FramebufferAttachmentObject *resource,
1565 GLsizei numViews,
1566 GLuint baseViewIndex,
1567 GLenum multiviewLayout,
1568 const GLint *viewportOffsets)
1569{
Jamie Madilla02315b2017-02-23 14:14:47 -05001570 // Context may be null in unit tests.
1571 if (!context || !context->isWebGL1())
1572 {
Martin Radev5dae57b2017-07-14 16:15:55 +03001573 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1574 multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001575 return;
1576 }
1577
1578 switch (binding)
1579 {
1580 case GL_DEPTH_STENCIL:
1581 case GL_DEPTH_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001582 mState.mWebGLDepthStencilAttachment.attach(context, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001583 resource, numViews, baseViewIndex,
1584 multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001585 break;
1586 case GL_DEPTH:
1587 case GL_DEPTH_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001588 mState.mWebGLDepthAttachment.attach(context, type, binding, textureIndex, resource,
1589 numViews, baseViewIndex, multiviewLayout,
1590 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001591 break;
1592 case GL_STENCIL:
1593 case GL_STENCIL_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001594 mState.mWebGLStencilAttachment.attach(context, type, binding, textureIndex, resource,
1595 numViews, baseViewIndex, multiviewLayout,
1596 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001597 break;
1598 default:
Martin Radev5dae57b2017-07-14 16:15:55 +03001599 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews,
1600 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001601 return;
1602 }
1603
Martin Radev5dae57b2017-07-14 16:15:55 +03001604 commitWebGL1DepthStencilIfConsistent(context, numViews, baseViewIndex, multiviewLayout,
1605 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001606}
1607
Martin Radev82ef7742017-08-08 17:44:58 +03001608void Framebuffer::setAttachmentMultiviewLayered(const Context *context,
1609 GLenum type,
1610 GLenum binding,
1611 const ImageIndex &textureIndex,
1612 FramebufferAttachmentObject *resource,
1613 GLsizei numViews,
1614 GLint baseViewIndex)
1615{
1616 setAttachment(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1617 GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE,
1618 FramebufferAttachment::kDefaultViewportOffsets);
1619}
1620
Martin Radev5dae57b2017-07-14 16:15:55 +03001621void Framebuffer::setAttachmentMultiviewSideBySide(const Context *context,
1622 GLenum type,
1623 GLenum binding,
1624 const ImageIndex &textureIndex,
1625 FramebufferAttachmentObject *resource,
1626 GLsizei numViews,
1627 const GLint *viewportOffsets)
1628{
1629 setAttachment(context, type, binding, textureIndex, resource, numViews,
1630 FramebufferAttachment::kDefaultBaseViewIndex,
1631 GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE, viewportOffsets);
1632}
1633
1634void Framebuffer::commitWebGL1DepthStencilIfConsistent(const Context *context,
1635 GLsizei numViews,
1636 GLuint baseViewIndex,
1637 GLenum multiviewLayout,
1638 const GLint *viewportOffsets)
Jamie Madilla02315b2017-02-23 14:14:47 -05001639{
1640 int count = 0;
1641
1642 std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment,
1643 &mState.mWebGLDepthAttachment,
1644 &mState.mWebGLStencilAttachment}};
1645 for (FramebufferAttachment *attachment : attachments)
1646 {
1647 if (attachment->isAttached())
1648 {
1649 count++;
1650 }
1651 }
1652
1653 mState.mWebGLDepthStencilConsistent = (count <= 1);
1654 if (!mState.mWebGLDepthStencilConsistent)
1655 {
1656 // Inconsistent.
1657 return;
1658 }
1659
Geoff Lange466c552017-03-17 15:24:12 -04001660 auto getImageIndexIfTextureAttachment = [](const FramebufferAttachment &attachment) {
1661 if (attachment.type() == GL_TEXTURE)
1662 {
1663 return attachment.getTextureImageIndex();
1664 }
1665 else
1666 {
Jamie Madillcc129372018-04-12 09:13:18 -04001667 return ImageIndex();
Geoff Lange466c552017-03-17 15:24:12 -04001668 }
1669 };
1670
Jamie Madilla02315b2017-02-23 14:14:47 -05001671 if (mState.mWebGLDepthAttachment.isAttached())
1672 {
1673 const auto &depth = mState.mWebGLDepthAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001674 setAttachmentImpl(context, depth.type(), GL_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001675 getImageIndexIfTextureAttachment(depth), depth.getResource(), numViews,
1676 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madillcc129372018-04-12 09:13:18 -04001677 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
1678 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001679 }
1680 else if (mState.mWebGLStencilAttachment.isAttached())
1681 {
1682 const auto &stencil = mState.mWebGLStencilAttachment;
Jamie Madillcc129372018-04-12 09:13:18 -04001683 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
1684 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001685 setAttachmentImpl(context, stencil.type(), GL_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001686 getImageIndexIfTextureAttachment(stencil), stencil.getResource(),
1687 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001688 }
1689 else if (mState.mWebGLDepthStencilAttachment.isAttached())
1690 {
1691 const auto &depthStencil = mState.mWebGLDepthStencilAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001692 setAttachmentImpl(context, depthStencil.type(), GL_DEPTH_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001693 getImageIndexIfTextureAttachment(depthStencil),
Martin Radev5dae57b2017-07-14 16:15:55 +03001694 depthStencil.getResource(), numViews, baseViewIndex, multiviewLayout,
1695 viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001696 setAttachmentImpl(context, depthStencil.type(), GL_STENCIL_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001697 getImageIndexIfTextureAttachment(depthStencil),
Martin Radev5dae57b2017-07-14 16:15:55 +03001698 depthStencil.getResource(), numViews, baseViewIndex, multiviewLayout,
1699 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001700 }
1701 else
1702 {
Jamie Madillcc129372018-04-12 09:13:18 -04001703 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
1704 baseViewIndex, multiviewLayout, viewportOffsets);
1705 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
1706 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001707 }
1708}
1709
Jamie Madill4928b7c2017-06-20 12:57:39 -04001710void Framebuffer::setAttachmentImpl(const Context *context,
1711 GLenum type,
Jamie Madilla02315b2017-02-23 14:14:47 -05001712 GLenum binding,
1713 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001714 FramebufferAttachmentObject *resource,
1715 GLsizei numViews,
1716 GLuint baseViewIndex,
1717 GLenum multiviewLayout,
1718 const GLint *viewportOffsets)
Jamie Madilla02315b2017-02-23 14:14:47 -05001719{
Jamie Madilla02315b2017-02-23 14:14:47 -05001720 switch (binding)
1721 {
Jamie Madillb8126692017-04-05 11:22:17 -04001722 case GL_DEPTH_STENCIL:
1723 case GL_DEPTH_STENCIL_ATTACHMENT:
1724 {
1725 // ensure this is a legitimate depth+stencil format
1726 FramebufferAttachmentObject *attachmentObj = resource;
1727 if (resource)
1728 {
Jamie Madill4fd95d52017-04-05 11:22:18 -04001729 const Format &format = resource->getAttachmentFormat(binding, textureIndex);
Jamie Madillb8126692017-04-05 11:22:17 -04001730 if (format.info->depthBits == 0 || format.info->stencilBits == 0)
1731 {
1732 // Attaching nullptr detaches the current attachment.
1733 attachmentObj = nullptr;
1734 }
1735 }
1736
Jamie Madill4928b7c2017-06-20 12:57:39 -04001737 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001738 &mDirtyDepthAttachmentBinding, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001739 attachmentObj, numViews, baseViewIndex, multiviewLayout,
1740 viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001741 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001742 &mDirtyStencilAttachmentBinding, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001743 attachmentObj, numViews, baseViewIndex, multiviewLayout,
1744 viewportOffsets);
Jamie Madill42975642017-10-12 12:31:51 -04001745 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001746 }
1747
Jamie Madilla02315b2017-02-23 14:14:47 -05001748 case GL_DEPTH:
1749 case GL_DEPTH_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001750 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001751 &mDirtyDepthAttachmentBinding, type, binding, textureIndex, resource,
1752 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madill2d06b732015-04-20 12:53:28 -04001753 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001754
Jamie Madilla02315b2017-02-23 14:14:47 -05001755 case GL_STENCIL:
1756 case GL_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001757 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001758 &mDirtyStencilAttachmentBinding, type, binding, textureIndex, resource,
1759 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001760 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001761
Jamie Madilla02315b2017-02-23 14:14:47 -05001762 case GL_BACK:
Geoff Lang8170eab2017-09-21 13:59:04 -04001763 updateAttachment(context, &mState.mColorAttachments[0], DIRTY_BIT_COLOR_ATTACHMENT_0,
1764 &mDirtyColorAttachmentBindings[0], type, binding, textureIndex,
1765 resource, numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001766 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001767
Jamie Madilla02315b2017-02-23 14:14:47 -05001768 default:
1769 {
1770 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
1771 ASSERT(colorIndex < mState.mColorAttachments.size());
Jamie Madillb8126692017-04-05 11:22:17 -04001772 size_t dirtyBit = DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001773 updateAttachment(context, &mState.mColorAttachments[colorIndex], dirtyBit,
Jamie Madillb8126692017-04-05 11:22:17 -04001774 &mDirtyColorAttachmentBindings[colorIndex], type, binding,
Martin Radev5dae57b2017-07-14 16:15:55 +03001775 textureIndex, resource, numViews, baseViewIndex, multiviewLayout,
1776 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001777
Corentin Walleze7557742017-06-01 13:09:57 -04001778 // TODO(jmadill): ASSERT instead of checking the attachment exists in
1779 // formsRenderingFeedbackLoopWith
Jamie Madilla02315b2017-02-23 14:14:47 -05001780 bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE);
1781 mState.mEnabledDrawBuffers.set(colorIndex, enabled);
Brandon Jones76746f92017-11-22 11:44:41 -08001782 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(colorIndex), colorIndex);
Jamie Madill2d06b732015-04-20 12:53:28 -04001783 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001784 break;
Geoff Langab75a052014-10-15 12:56:37 -04001785 }
1786}
1787
Jamie Madill4928b7c2017-06-20 12:57:39 -04001788void Framebuffer::updateAttachment(const Context *context,
1789 FramebufferAttachment *attachment,
Jamie Madillb8126692017-04-05 11:22:17 -04001790 size_t dirtyBit,
Jamie Madilld4442552018-02-27 22:03:47 -05001791 angle::ObserverBinding *onDirtyBinding,
Jamie Madillb8126692017-04-05 11:22:17 -04001792 GLenum type,
1793 GLenum binding,
1794 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001795 FramebufferAttachmentObject *resource,
1796 GLsizei numViews,
1797 GLuint baseViewIndex,
1798 GLenum multiviewLayout,
1799 const GLint *viewportOffsets)
Jamie Madillb8126692017-04-05 11:22:17 -04001800{
Martin Radev5dae57b2017-07-14 16:15:55 +03001801 attachment->attach(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1802 multiviewLayout, viewportOffsets);
Jamie Madillb8126692017-04-05 11:22:17 -04001803 mDirtyBits.set(dirtyBit);
Jamie Madill05b35b22017-10-03 09:01:44 -04001804 mState.mResourceNeedsInit.set(dirtyBit, attachment->initState() == InitState::MayNeedInit);
Jamie Madill888081d2018-02-27 00:24:46 -05001805 onDirtyBinding->bind(resource ? resource->getSubject() : nullptr);
Jamie Madille98b1b52018-03-08 09:47:23 -05001806
Jamie Madillb983a4b2018-08-01 11:34:51 -04001807 invalidateCompletenessCache(context);
Jamie Madillb8126692017-04-05 11:22:17 -04001808}
1809
Jamie Madilla02315b2017-02-23 14:14:47 -05001810void Framebuffer::resetAttachment(const Context *context, GLenum binding)
Jamie Madill2d06b732015-04-20 12:53:28 -04001811{
Jamie Madillcc129372018-04-12 09:13:18 -04001812 setAttachment(context, GL_NONE, binding, ImageIndex(), nullptr);
Jamie Madill2d06b732015-04-20 12:53:28 -04001813}
1814
Jamie Madill19fa1c62018-03-08 09:47:21 -05001815Error Framebuffer::syncState(const Context *context)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001816{
1817 if (mDirtyBits.any())
1818 {
Jamie Madill888081d2018-02-27 00:24:46 -05001819 mDirtyBitsGuard = mDirtyBits;
Jamie Madill19fa1c62018-03-08 09:47:21 -05001820 ANGLE_TRY(mImpl->syncState(context, mDirtyBits));
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001821 mDirtyBits.reset();
Jamie Madill888081d2018-02-27 00:24:46 -05001822 mDirtyBitsGuard.reset();
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001823 }
Jamie Madill19fa1c62018-03-08 09:47:21 -05001824 return NoError();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001826
Jamie Madilld4442552018-02-27 22:03:47 -05001827void Framebuffer::onSubjectStateChange(const Context *context,
1828 angle::SubjectIndex index,
1829 angle::SubjectMessage message)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001830{
Jamie Madill55e57f92018-09-18 11:32:43 -04001831 ASSERT(message == angle::SubjectMessage::STORAGE_CHANGED);
1832
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{
1862 const Program *program = state.getProgram();
1863
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 Madill05b35b22017-10-03 09:01:44 -04002080Error Framebuffer::ensureDrawAttachmentsInitialized(const Context *context)
2081{
2082 if (!context->isRobustResourceInitEnabled())
2083 {
2084 return NoError();
2085 }
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();
2105 return NoError();
2106}
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