blob: 60494c0f8f9d783cf0d833765b233c873697d5a8 [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
74 const InternalFormat &format = *attachment.getFormat().info;
Yuly Novikovf15f8862018-06-04 18:59:41 -040075 if (attachment.type() == GL_RENDERBUFFER &&
76 !format.renderbufferSupport(context->getClientVersion(), context->getExtensions()))
77 {
78 return false;
79 }
80 if (attachment.type() == GL_TEXTURE &&
81 !format.textureAttachmentSupport(context->getClientVersion(), context->getExtensions()))
Geoff Lang9f10b772017-05-16 15:51:03 -040082 {
83 return false;
84 }
85
86 if (attachment.type() == GL_TEXTURE)
87 {
Jiawei Shaoa8802472018-05-28 11:17:47 +080088 // [EXT_geometry_shader] Section 9.4.1, "Framebuffer Completeness"
89 // If <image> is a three-dimensional texture or a two-dimensional array texture and the
90 // attachment is not layered, the selected layer is less than the depth or layer count,
91 // respectively, of the texture.
92 if (!attachment.isLayered())
Geoff Lang9f10b772017-05-16 15:51:03 -040093 {
Jiawei Shaoa8802472018-05-28 11:17:47 +080094 if (attachment.layer() >= size.depth)
95 {
96 return false;
97 }
98 }
99 // If <image> is a three-dimensional texture or a two-dimensional array texture and the
100 // attachment is layered, the depth or layer count, respectively, of the texture is less
101 // than or equal to the value of MAX_FRAMEBUFFER_LAYERS_EXT.
102 else
103 {
104 if (static_cast<GLuint>(size.depth) >= context->getCaps().maxFramebufferLayers)
105 {
106 return false;
107 }
Geoff Lang9f10b772017-05-16 15:51:03 -0400108 }
109
110 // ES3 specifies that cube map texture attachments must be cube complete.
111 // This language is missing from the ES2 spec, but we enforce it here because some
112 // desktop OpenGL drivers also enforce this validation.
113 // TODO(jmadill): Check if OpenGL ES2 drivers enforce cube completeness.
114 const Texture *texture = attachment.getTexture();
115 ASSERT(texture);
Corentin Wallez99d492c2018-02-27 15:17:10 -0500116 if (texture->getType() == TextureType::CubeMap &&
Geoff Lang9f10b772017-05-16 15:51:03 -0400117 !texture->getTextureState().isCubeComplete())
118 {
119 return false;
120 }
Geoff Lang857c09d2017-05-16 15:55:04 -0400121
122 if (!texture->getImmutableFormat())
123 {
124 GLuint attachmentMipLevel = static_cast<GLuint>(attachment.mipLevel());
125
126 // From the ES 3.0 spec, pg 213:
127 // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of
128 // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture,
129 // then the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL must be in the
130 // range[levelbase, q], where levelbase is the value of TEXTURE_BASE_LEVEL and q is
131 // the effective maximum texture level defined in the Mipmapping discussion of
132 // section 3.8.10.4.
133 if (attachmentMipLevel < texture->getBaseLevel() ||
134 attachmentMipLevel > texture->getMipmapMaxLevel())
135 {
136 return false;
137 }
138
139 // Form the ES 3.0 spec, pg 213/214:
140 // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of
141 // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture and
142 // the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL is not levelbase, then the
143 // texture must be mipmap complete, and if FRAMEBUFFER_ATTACHMENT_OBJECT_NAME names
144 // a cubemap texture, the texture must also be cube complete.
145 if (attachmentMipLevel != texture->getBaseLevel() && !texture->isMipmapComplete())
146 {
147 return false;
148 }
149 }
Geoff Lang9f10b772017-05-16 15:51:03 -0400150 }
151
152 return true;
153};
154
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400155bool CheckAttachmentSampleCompleteness(const Context *context,
156 const FramebufferAttachment &attachment,
157 bool colorAttachment,
158 Optional<int> *samples,
Geoff Lang92019432017-11-20 13:09:34 -0500159 Optional<bool> *fixedSampleLocations)
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400160{
161 ASSERT(attachment.isAttached());
162
163 if (attachment.type() == GL_TEXTURE)
164 {
165 const Texture *texture = attachment.getTexture();
166 ASSERT(texture);
167
168 const ImageIndex &attachmentImageIndex = attachment.getTextureImageIndex();
Jiawei Shaoa8802472018-05-28 11:17:47 +0800169 bool fixedSampleloc = texture->getAttachmentFixedSampleLocations(attachmentImageIndex);
Geoff Langa1b9d5a2017-05-18 11:22:27 -0400170 if (fixedSampleLocations->valid() && fixedSampleloc != fixedSampleLocations->value())
171 {
172 return false;
173 }
174 else
175 {
176 *fixedSampleLocations = fixedSampleloc;
177 }
178 }
179
180 if (samples->valid())
181 {
182 if (attachment.getSamples() != samples->value())
183 {
184 if (colorAttachment)
185 {
186 // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that
187 // all color attachments have the same number of samples for the FBO to be complete.
188 return false;
189 }
190 else
191 {
192 // CHROMIUM_framebuffer_mixed_samples allows a framebuffer to be considered complete
193 // when its depth or stencil samples are a multiple of the number of color samples.
194 if (!context->getExtensions().framebufferMixedSamples)
195 {
196 return false;
197 }
198
199 if ((attachment.getSamples() % std::max(samples->value(), 1)) != 0)
200 {
201 return false;
202 }
203 }
204 }
205 }
206 else
207 {
208 *samples = attachment.getSamples();
209 }
210
211 return true;
212}
213
Jamie Madill05b35b22017-10-03 09:01:44 -0400214// Needed to index into the attachment arrays/bitsets.
Jamie Madill682efdc2017-10-03 14:10:29 -0400215static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500216 Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX,
Jamie Madill05b35b22017-10-03 09:01:44 -0400217 "Framebuffer Dirty bit mismatch");
Jamie Madill682efdc2017-10-03 14:10:29 -0400218static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500219 Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madill05b35b22017-10-03 09:01:44 -0400220 "Framebuffer Dirty bit mismatch");
Jamie Madill682efdc2017-10-03 14:10:29 -0400221static_assert(static_cast<size_t>(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS + 1) ==
Jamie Madilld4442552018-02-27 22:03:47 -0500222 Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madill05b35b22017-10-03 09:01:44 -0400223 "Framebuffer Dirty bit mismatch");
224
225Error InitAttachment(const Context *context, FramebufferAttachment *attachment)
226{
227 ASSERT(attachment->isAttached());
228 if (attachment->initState() == InitState::MayNeedInit)
229 {
230 ANGLE_TRY(attachment->initializeContents(context));
231 }
232 return NoError();
233}
234
235bool IsColorMaskedOut(const BlendState &blend)
236{
237 return (!blend.colorMaskRed && !blend.colorMaskGreen && !blend.colorMaskBlue &&
238 !blend.colorMaskAlpha);
239}
240
241bool IsDepthMaskedOut(const DepthStencilState &depthStencil)
242{
243 return !depthStencil.depthMask;
244}
245
246bool IsStencilMaskedOut(const DepthStencilState &depthStencil)
247{
248 return ((depthStencil.stencilMask & depthStencil.stencilWritemask) == 0);
249}
250
251bool IsClearBufferMaskedOut(const Context *context, GLenum buffer)
252{
253 switch (buffer)
254 {
255 case GL_COLOR:
256 return IsColorMaskedOut(context->getGLState().getBlendState());
257 case GL_DEPTH:
258 return IsDepthMaskedOut(context->getGLState().getDepthStencilState());
259 case GL_STENCIL:
260 return IsStencilMaskedOut(context->getGLState().getDepthStencilState());
261 case GL_DEPTH_STENCIL:
262 return IsDepthMaskedOut(context->getGLState().getDepthStencilState()) &&
263 IsStencilMaskedOut(context->getGLState().getDepthStencilState());
264 default:
265 UNREACHABLE();
266 return true;
267 }
268}
269
Jamie Madill362876b2016-06-16 14:46:59 -0400270} // anonymous namespace
Jamie Madilld1405e52015-03-05 15:41:39 -0500271
Jamie Madill6f60d052017-02-22 15:20:11 -0500272// This constructor is only used for default framebuffers.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400273FramebufferState::FramebufferState()
Jamie Madill2274b652018-05-31 10:56:08 -0400274 : mId(0),
275 mLabel(),
Geoff Lang70d0f492015-12-10 17:45:46 -0500276 mColorAttachments(1),
Corentin Walleze7557742017-06-01 13:09:57 -0400277 mDrawBufferStates(1, GL_BACK),
Jamie Madill6f60d052017-02-22 15:20:11 -0500278 mReadBufferState(GL_BACK),
Brandon Jones76746f92017-11-22 11:44:41 -0800279 mDrawBufferTypeMask(),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800280 mDefaultWidth(0),
281 mDefaultHeight(0),
282 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500283 mDefaultFixedSampleLocations(GL_FALSE),
Jiawei Shaob1e91382018-05-17 14:33:55 +0800284 mDefaultLayers(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500285 mWebGLDepthStencilConsistent(true)
Corentin Wallez37c39792015-08-20 14:19:46 -0400286{
Geoff Langd90d3882017-03-21 10:49:54 -0400287 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilla4595b82017-01-11 17:36:34 -0500288 mEnabledDrawBuffers.set(0);
Corentin Wallez37c39792015-08-20 14:19:46 -0400289}
290
Jamie Madill2274b652018-05-31 10:56:08 -0400291FramebufferState::FramebufferState(const Caps &caps, GLuint id)
292 : mId(id),
293 mLabel(),
Geoff Lang70d0f492015-12-10 17:45:46 -0500294 mColorAttachments(caps.maxColorAttachments),
Jamie Madilld1405e52015-03-05 15:41:39 -0500295 mDrawBufferStates(caps.maxDrawBuffers, GL_NONE),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800296 mReadBufferState(GL_COLOR_ATTACHMENT0_EXT),
Brandon Jones76746f92017-11-22 11:44:41 -0800297 mDrawBufferTypeMask(),
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800298 mDefaultWidth(0),
299 mDefaultHeight(0),
300 mDefaultSamples(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500301 mDefaultFixedSampleLocations(GL_FALSE),
Jiawei Shaob1e91382018-05-17 14:33:55 +0800302 mDefaultLayers(0),
Jamie Madilla02315b2017-02-23 14:14:47 -0500303 mWebGLDepthStencilConsistent(true)
Jamie Madilld1405e52015-03-05 15:41:39 -0500304{
Jamie Madill2274b652018-05-31 10:56:08 -0400305 ASSERT(mId != 0);
Geoff Langa15472a2015-08-11 11:48:03 -0400306 ASSERT(mDrawBufferStates.size() > 0);
Jamie Madilld1405e52015-03-05 15:41:39 -0500307 mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
308}
309
Jamie Madill48ef11b2016-04-27 15:21:52 -0400310FramebufferState::~FramebufferState()
Jamie Madilld1405e52015-03-05 15:41:39 -0500311{
Jamie Madilld1405e52015-03-05 15:41:39 -0500312}
313
Jamie Madill48ef11b2016-04-27 15:21:52 -0400314const std::string &FramebufferState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500315{
316 return mLabel;
317}
318
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800319const FramebufferAttachment *FramebufferState::getAttachment(const Context *context,
320 GLenum attachment) const
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400321{
322 if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15)
323 {
324 return getColorAttachment(attachment - GL_COLOR_ATTACHMENT0);
325 }
326
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800327 // WebGL1 allows a developer to query for attachment parameters even when "inconsistant" (i.e.
328 // multiple conflicting attachment points) and requires us to return the framebuffer attachment
329 // associated with WebGL.
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400330 switch (attachment)
331 {
332 case GL_COLOR:
333 case GL_BACK:
334 return getColorAttachment(0);
335 case GL_DEPTH:
336 case GL_DEPTH_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800337 if (context->isWebGL1())
338 {
339 return getWebGLDepthAttachment();
340 }
341 else
342 {
343 return getDepthAttachment();
344 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400345 case GL_STENCIL:
346 case GL_STENCIL_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800347 if (context->isWebGL1())
348 {
349 return getWebGLStencilAttachment();
350 }
351 else
352 {
353 return getStencilAttachment();
354 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400355 case GL_DEPTH_STENCIL:
356 case GL_DEPTH_STENCIL_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800357 if (context->isWebGL1())
358 {
359 return getWebGLDepthStencilAttachment();
360 }
361 else
362 {
363 return getDepthStencilAttachment();
364 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400365 default:
366 UNREACHABLE();
367 return nullptr;
368 }
369}
370
Jamie Madill05b35b22017-10-03 09:01:44 -0400371size_t FramebufferState::getReadIndex() const
Jamie Madill7147f012015-03-05 15:41:40 -0500372{
Jamie Madill231c7f52017-04-26 13:45:37 -0400373 ASSERT(mReadBufferState == GL_BACK ||
374 (mReadBufferState >= GL_COLOR_ATTACHMENT0 && mReadBufferState <= GL_COLOR_ATTACHMENT15));
375 size_t readIndex = (mReadBufferState == GL_BACK
376 ? 0
377 : static_cast<size_t>(mReadBufferState - GL_COLOR_ATTACHMENT0));
Jamie Madill7147f012015-03-05 15:41:40 -0500378 ASSERT(readIndex < mColorAttachments.size());
Jamie Madill05b35b22017-10-03 09:01:44 -0400379 return readIndex;
380}
381
382const FramebufferAttachment *FramebufferState::getReadAttachment() const
383{
384 if (mReadBufferState == GL_NONE)
385 {
386 return nullptr;
387 }
388 size_t readIndex = getReadIndex();
Jamie Madill2d06b732015-04-20 12:53:28 -0400389 return mColorAttachments[readIndex].isAttached() ? &mColorAttachments[readIndex] : nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500390}
391
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500392const FramebufferAttachment *FramebufferState::getFirstNonNullAttachment() const
393{
394 auto *colorAttachment = getFirstColorAttachment();
395 if (colorAttachment)
396 {
397 return colorAttachment;
398 }
399 return getDepthOrStencilAttachment();
400}
401
Jamie Madill48ef11b2016-04-27 15:21:52 -0400402const FramebufferAttachment *FramebufferState::getFirstColorAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500403{
Jamie Madill2d06b732015-04-20 12:53:28 -0400404 for (const FramebufferAttachment &colorAttachment : mColorAttachments)
Jamie Madill7147f012015-03-05 15:41:40 -0500405 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400406 if (colorAttachment.isAttached())
Jamie Madill7147f012015-03-05 15:41:40 -0500407 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400408 return &colorAttachment;
Jamie Madill7147f012015-03-05 15:41:40 -0500409 }
410 }
411
412 return nullptr;
413}
414
Jamie Madill48ef11b2016-04-27 15:21:52 -0400415const FramebufferAttachment *FramebufferState::getDepthOrStencilAttachment() const
Jamie Madill7147f012015-03-05 15:41:40 -0500416{
Jamie Madill2d06b732015-04-20 12:53:28 -0400417 if (mDepthAttachment.isAttached())
418 {
419 return &mDepthAttachment;
420 }
421 if (mStencilAttachment.isAttached())
422 {
423 return &mStencilAttachment;
424 }
425 return nullptr;
Jamie Madill7147f012015-03-05 15:41:40 -0500426}
427
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500428const FramebufferAttachment *FramebufferState::getStencilOrDepthStencilAttachment() const
429{
430 if (mStencilAttachment.isAttached())
431 {
432 return &mStencilAttachment;
433 }
434 return getDepthStencilAttachment();
435}
436
Jamie Madill48ef11b2016-04-27 15:21:52 -0400437const FramebufferAttachment *FramebufferState::getColorAttachment(size_t colorAttachment) const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400438{
439 ASSERT(colorAttachment < mColorAttachments.size());
Jamie Madill231c7f52017-04-26 13:45:37 -0400440 return mColorAttachments[colorAttachment].isAttached() ? &mColorAttachments[colorAttachment]
441 : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400442}
443
Jamie Madill48ef11b2016-04-27 15:21:52 -0400444const FramebufferAttachment *FramebufferState::getDepthAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400445{
Jamie Madill2d06b732015-04-20 12:53:28 -0400446 return mDepthAttachment.isAttached() ? &mDepthAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400447}
448
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800449const FramebufferAttachment *FramebufferState::getWebGLDepthAttachment() const
450{
451 return mWebGLDepthAttachment.isAttached() ? &mWebGLDepthAttachment : nullptr;
452}
453
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800454const FramebufferAttachment *FramebufferState::getWebGLDepthStencilAttachment() const
455{
456 return mWebGLDepthStencilAttachment.isAttached() ? &mWebGLDepthStencilAttachment : nullptr;
457}
458
Jamie Madill48ef11b2016-04-27 15:21:52 -0400459const FramebufferAttachment *FramebufferState::getStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400460{
Jamie Madill2d06b732015-04-20 12:53:28 -0400461 return mStencilAttachment.isAttached() ? &mStencilAttachment : nullptr;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400462}
463
Bryan Bernhart (Intel Americas Inc)5f198102017-12-12 14:21:39 -0800464const FramebufferAttachment *FramebufferState::getWebGLStencilAttachment() const
465{
466 return mWebGLStencilAttachment.isAttached() ? &mWebGLStencilAttachment : nullptr;
467}
468
Jamie Madill48ef11b2016-04-27 15:21:52 -0400469const FramebufferAttachment *FramebufferState::getDepthStencilAttachment() const
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400470{
471 // A valid depth-stencil attachment has the same resource bound to both the
472 // depth and stencil attachment points.
Jamie Madill2d06b732015-04-20 12:53:28 -0400473 if (mDepthAttachment.isAttached() && mStencilAttachment.isAttached() &&
Jamie Madill44f26482016-11-18 12:49:15 -0500474 mDepthAttachment == mStencilAttachment)
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400475 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400476 return &mDepthAttachment;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400477 }
478
479 return nullptr;
480}
481
Jamie Madill48ef11b2016-04-27 15:21:52 -0400482bool FramebufferState::attachmentsHaveSameDimensions() const
Jamie Madillcc86d642015-11-24 13:00:07 -0500483{
484 Optional<Extents> attachmentSize;
485
Jamie Madill231c7f52017-04-26 13:45:37 -0400486 auto hasMismatchedSize = [&attachmentSize](const FramebufferAttachment &attachment) {
Jamie Madillcc86d642015-11-24 13:00:07 -0500487 if (!attachment.isAttached())
488 {
489 return false;
490 }
491
492 if (!attachmentSize.valid())
493 {
494 attachmentSize = attachment.getSize();
495 return false;
496 }
497
Jeff Gilbert8f8edd62017-10-31 14:26:30 -0700498 const auto &prevSize = attachmentSize.value();
499 const auto &curSize = attachment.getSize();
500 return (curSize.width != prevSize.width || curSize.height != prevSize.height);
Jamie Madillcc86d642015-11-24 13:00:07 -0500501 };
502
503 for (const auto &attachment : mColorAttachments)
504 {
505 if (hasMismatchedSize(attachment))
506 {
507 return false;
508 }
509 }
510
511 if (hasMismatchedSize(mDepthAttachment))
512 {
513 return false;
514 }
515
516 return !hasMismatchedSize(mStencilAttachment);
517}
518
Luc Ferron5bdf8bd2018-06-20 09:51:37 -0400519bool FramebufferState::hasSeparateDepthAndStencilAttachments() const
520{
521 // if we have both a depth and stencil buffer, they must refer to the same object
522 // since we only support packed_depth_stencil and not separate depth and stencil
523 return (getDepthAttachment() != nullptr && getStencilAttachment() != nullptr &&
524 getDepthStencilAttachment() == nullptr);
525}
526
Jamie Madilld4442552018-02-27 22:03:47 -0500527const FramebufferAttachment *FramebufferState::getDrawBuffer(size_t drawBufferIdx) const
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400528{
529 ASSERT(drawBufferIdx < mDrawBufferStates.size());
530 if (mDrawBufferStates[drawBufferIdx] != GL_NONE)
531 {
532 // ES3 spec: "If the GL is bound to a draw framebuffer object, the ith buffer listed in bufs
533 // must be COLOR_ATTACHMENTi or NONE"
534 ASSERT(mDrawBufferStates[drawBufferIdx] == GL_COLOR_ATTACHMENT0 + drawBufferIdx ||
535 (drawBufferIdx == 0 && mDrawBufferStates[drawBufferIdx] == GL_BACK));
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800536
537 if (mDrawBufferStates[drawBufferIdx] == GL_BACK)
538 {
539 return getColorAttachment(0);
540 }
541 else
542 {
543 return getColorAttachment(mDrawBufferStates[drawBufferIdx] - GL_COLOR_ATTACHMENT0);
544 }
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400545 }
546 else
547 {
548 return nullptr;
549 }
550}
551
552size_t FramebufferState::getDrawBufferCount() const
553{
554 return mDrawBufferStates.size();
555}
556
Geoff Langb21e20d2016-07-19 15:35:41 -0400557bool FramebufferState::colorAttachmentsAreUniqueImages() const
558{
559 for (size_t firstAttachmentIdx = 0; firstAttachmentIdx < mColorAttachments.size();
560 firstAttachmentIdx++)
561 {
Jamie Madilld4442552018-02-27 22:03:47 -0500562 const FramebufferAttachment &firstAttachment = mColorAttachments[firstAttachmentIdx];
Geoff Langb21e20d2016-07-19 15:35:41 -0400563 if (!firstAttachment.isAttached())
564 {
565 continue;
566 }
567
568 for (size_t secondAttachmentIdx = firstAttachmentIdx + 1;
569 secondAttachmentIdx < mColorAttachments.size(); secondAttachmentIdx++)
570 {
Jamie Madilld4442552018-02-27 22:03:47 -0500571 const FramebufferAttachment &secondAttachment = mColorAttachments[secondAttachmentIdx];
Geoff Langb21e20d2016-07-19 15:35:41 -0400572 if (!secondAttachment.isAttached())
573 {
574 continue;
575 }
576
577 if (firstAttachment == secondAttachment)
578 {
579 return false;
580 }
581 }
582 }
583
584 return true;
585}
586
Jamie Madill9c335862017-07-18 11:51:38 -0400587bool FramebufferState::hasDepth() const
588{
589 return (mDepthAttachment.isAttached() && mDepthAttachment.getDepthSize() > 0);
590}
591
592bool FramebufferState::hasStencil() const
593{
594 return (mStencilAttachment.isAttached() && mStencilAttachment.getStencilSize() > 0);
595}
596
Martin Radev5c00d0d2017-08-07 10:06:59 +0300597GLsizei FramebufferState::getNumViews() const
598{
599 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
600 if (attachment == nullptr)
601 {
602 return FramebufferAttachment::kDefaultNumViews;
603 }
604 return attachment->getNumViews();
605}
606
607const std::vector<Offset> *FramebufferState::getViewportOffsets() const
608{
609 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
610 if (attachment == nullptr)
611 {
612 return nullptr;
613 }
614 return &attachment->getMultiviewViewportOffsets();
615}
616
617GLenum FramebufferState::getMultiviewLayout() const
618{
619 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
620 if (attachment == nullptr)
621 {
622 return GL_NONE;
623 }
624 return attachment->getMultiviewLayout();
625}
626
Martin Radev4e619f52017-08-09 11:50:06 +0300627int FramebufferState::getBaseViewIndex() const
628{
629 const FramebufferAttachment *attachment = getFirstNonNullAttachment();
630 if (attachment == nullptr)
631 {
632 return GL_NONE;
633 }
634 return attachment->getBaseViewIndex();
635}
636
Jamie Madill05b35b22017-10-03 09:01:44 -0400637Box FramebufferState::getDimensions() const
638{
639 ASSERT(attachmentsHaveSameDimensions());
640 ASSERT(getFirstNonNullAttachment() != nullptr);
641 Extents extents = getFirstNonNullAttachment()->getSize();
642 return Box(0, 0, 0, extents.width, extents.height, extents.depth);
643}
644
Jamie Madill7aea7e02016-05-10 10:39:45 -0400645Framebuffer::Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id)
Jamie Madill2274b652018-05-31 10:56:08 -0400646 : mState(caps, id),
Jamie Madill362876b2016-06-16 14:46:59 -0400647 mImpl(factory->createFramebuffer(mState)),
Jamie Madill362876b2016-06-16 14:46:59 -0400648 mCachedStatus(),
649 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
650 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651{
Corentin Wallez37c39792015-08-20 14:19:46 -0400652 ASSERT(mImpl != nullptr);
Jamie Madill362876b2016-06-16 14:46:59 -0400653 ASSERT(mState.mColorAttachments.size() == static_cast<size_t>(caps.maxColorAttachments));
654
Jamie Madill1e5499d2017-04-05 11:22:16 -0400655 for (uint32_t colorIndex = 0;
656 colorIndex < static_cast<uint32_t>(mState.mColorAttachments.size()); ++colorIndex)
Jamie Madill362876b2016-06-16 14:46:59 -0400657 {
Jamie Madill1e5499d2017-04-05 11:22:16 -0400658 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
Jamie Madill362876b2016-06-16 14:46:59 -0400659 }
Corentin Wallez37c39792015-08-20 14:19:46 -0400660}
661
Geoff Langbf7b95d2018-05-01 16:48:21 -0400662Framebuffer::Framebuffer(const Context *context, egl::Surface *surface)
Jamie Madill362876b2016-06-16 14:46:59 -0400663 : mState(),
Geoff Langbf7b95d2018-05-01 16:48:21 -0400664 mImpl(surface->getImplementation()->createDefaultFramebuffer(context, mState)),
Jamie Madill362876b2016-06-16 14:46:59 -0400665 mCachedStatus(GL_FRAMEBUFFER_COMPLETE),
666 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
667 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
Corentin Wallez37c39792015-08-20 14:19:46 -0400668{
Geoff Langda88add2014-12-01 10:22:01 -0500669 ASSERT(mImpl != nullptr);
Jamie Madill1e5499d2017-04-05 11:22:16 -0400670 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Jamie Madill6f60d052017-02-22 15:20:11 -0500671
Geoff Langbf7b95d2018-05-01 16:48:21 -0400672 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400673 FramebufferAttachment::kDefaultNumViews,
Martin Radev5dae57b2017-07-14 16:15:55 +0300674 FramebufferAttachment::kDefaultBaseViewIndex,
675 FramebufferAttachment::kDefaultMultiviewLayout,
676 FramebufferAttachment::kDefaultViewportOffsets);
Jamie Madill6f60d052017-02-22 15:20:11 -0500677
678 if (surface->getConfig()->depthSize > 0)
679 {
Geoff Langbf7b95d2018-05-01 16:48:21 -0400680 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400681 FramebufferAttachment::kDefaultNumViews,
Jamie Madilld4442552018-02-27 22:03:47 -0500682 FramebufferAttachment::kDefaultBaseViewIndex,
683 FramebufferAttachment::kDefaultMultiviewLayout,
684 FramebufferAttachment::kDefaultViewportOffsets);
Jamie Madill6f60d052017-02-22 15:20:11 -0500685 }
686
687 if (surface->getConfig()->stencilSize > 0)
688 {
Geoff Langbf7b95d2018-05-01 16:48:21 -0400689 setAttachmentImpl(context, GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, ImageIndex(), surface,
Jamie Madillcc129372018-04-12 09:13:18 -0400690 FramebufferAttachment::kDefaultNumViews,
691 FramebufferAttachment::kDefaultBaseViewIndex,
692 FramebufferAttachment::kDefaultMultiviewLayout,
693 FramebufferAttachment::kDefaultViewportOffsets);
Jamie Madill6f60d052017-02-22 15:20:11 -0500694 }
Brandon Jones76746f92017-11-22 11:44:41 -0800695 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(0), 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696}
697
Corentin Wallezccab69d2017-01-27 16:57:15 -0500698Framebuffer::Framebuffer(rx::GLImplFactory *factory)
699 : mState(),
700 mImpl(factory->createFramebuffer(mState)),
Corentin Wallezccab69d2017-01-27 16:57:15 -0500701 mCachedStatus(GL_FRAMEBUFFER_UNDEFINED_OES),
702 mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT),
703 mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT)
704{
Jamie Madill1e5499d2017-04-05 11:22:16 -0400705 mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0);
Brandon Jones76746f92017-11-22 11:44:41 -0800706 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(0), 0);
Corentin Wallezccab69d2017-01-27 16:57:15 -0500707}
708
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000709Framebuffer::~Framebuffer()
710{
Geoff Langda88add2014-12-01 10:22:01 -0500711 SafeDelete(mImpl);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000712}
713
Jamie Madill4928b7c2017-06-20 12:57:39 -0400714void Framebuffer::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500715{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400716 for (auto &attachment : mState.mColorAttachments)
717 {
718 attachment.detach(context);
719 }
720 mState.mDepthAttachment.detach(context);
721 mState.mStencilAttachment.detach(context);
722 mState.mWebGLDepthAttachment.detach(context);
723 mState.mWebGLStencilAttachment.detach(context);
724 mState.mWebGLDepthStencilAttachment.detach(context);
725
Jamie Madillc564c072017-06-01 12:45:42 -0400726 mImpl->destroy(context);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500727}
728
Geoff Lang70d0f492015-12-10 17:45:46 -0500729void Framebuffer::setLabel(const std::string &label)
730{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400731 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500732}
733
734const std::string &Framebuffer::getLabel() const
735{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400736 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500737}
738
Jamie Madill8693bdb2017-09-02 15:32:14 -0400739bool Framebuffer::detachTexture(const Context *context, GLuint textureId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400741 return detachResourceById(context, GL_TEXTURE, textureId);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742}
743
Jamie Madill8693bdb2017-09-02 15:32:14 -0400744bool Framebuffer::detachRenderbuffer(const Context *context, GLuint renderbufferId)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400746 return detachResourceById(context, GL_RENDERBUFFER, renderbufferId);
Jamie Madilld1405e52015-03-05 15:41:39 -0500747}
Jamie Madille261b442014-06-25 12:42:21 -0400748
Jamie Madill8693bdb2017-09-02 15:32:14 -0400749bool Framebuffer::detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId)
Jamie Madilld1405e52015-03-05 15:41:39 -0500750{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400751 bool found = false;
752
Jamie Madill362876b2016-06-16 14:46:59 -0400753 for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
Jamie Madilld1405e52015-03-05 15:41:39 -0500754 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400755 if (detachMatchingAttachment(context, &mState.mColorAttachments[colorIndex], resourceType,
756 resourceId, DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex))
757 {
758 found = true;
759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760 }
761
Jamie Madilla02315b2017-02-23 14:14:47 -0500762 if (context->isWebGL1())
763 {
764 const std::array<FramebufferAttachment *, 3> attachments = {
765 {&mState.mWebGLDepthStencilAttachment, &mState.mWebGLDepthAttachment,
766 &mState.mWebGLStencilAttachment}};
767 for (FramebufferAttachment *attachment : attachments)
768 {
769 if (attachment->isAttached() && attachment->type() == resourceType &&
770 attachment->id() == resourceId)
771 {
772 resetAttachment(context, attachment->getBinding());
Jamie Madill8693bdb2017-09-02 15:32:14 -0400773 found = true;
Jamie Madilla02315b2017-02-23 14:14:47 -0500774 }
775 }
776 }
777 else
778 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400779 if (detachMatchingAttachment(context, &mState.mDepthAttachment, resourceType, resourceId,
780 DIRTY_BIT_DEPTH_ATTACHMENT))
781 {
782 found = true;
783 }
784 if (detachMatchingAttachment(context, &mState.mStencilAttachment, resourceType, resourceId,
785 DIRTY_BIT_STENCIL_ATTACHMENT))
786 {
787 found = true;
788 }
Jamie Madilla02315b2017-02-23 14:14:47 -0500789 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400790
791 return found;
Jamie Madill362876b2016-06-16 14:46:59 -0400792}
793
Jamie Madill8693bdb2017-09-02 15:32:14 -0400794bool Framebuffer::detachMatchingAttachment(const Context *context,
Jamie Madill4928b7c2017-06-20 12:57:39 -0400795 FramebufferAttachment *attachment,
Jamie Madill362876b2016-06-16 14:46:59 -0400796 GLenum matchType,
797 GLuint matchId,
798 size_t dirtyBit)
799{
800 if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId)
801 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400802 attachment->detach(context);
Jamie Madill362876b2016-06-16 14:46:59 -0400803 mDirtyBits.set(dirtyBit);
Jamie Madill05b35b22017-10-03 09:01:44 -0400804 mState.mResourceNeedsInit.set(dirtyBit, false);
Jamie Madill8693bdb2017-09-02 15:32:14 -0400805 return true;
Jamie Madill362876b2016-06-16 14:46:59 -0400806 }
Jamie Madill8693bdb2017-09-02 15:32:14 -0400807
808 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809}
810
Corentin Wallez37c39792015-08-20 14:19:46 -0400811const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400813 return mState.getColorAttachment(colorAttachment);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814}
815
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400816const FramebufferAttachment *Framebuffer::getDepthbuffer() const
Geoff Lang646559f2013-08-15 11:08:15 -0400817{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400818 return mState.getDepthAttachment();
Geoff Lang646559f2013-08-15 11:08:15 -0400819}
820
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400821const FramebufferAttachment *Framebuffer::getStencilbuffer() const
822{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400823 return mState.getStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400824}
825
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400826const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const
827{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400828 return mState.getDepthStencilAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400829}
830
831const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000832{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400833 return mState.getDepthOrStencilAttachment();
daniel@transgaming.comd2b47022012-11-28 19:40:10 +0000834}
835
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500836const FramebufferAttachment *Framebuffer::getStencilOrDepthStencilAttachment() const
837{
838 return mState.getStencilOrDepthStencilAttachment();
839}
840
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400841const FramebufferAttachment *Framebuffer::getReadColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000842{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400843 return mState.getReadAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000844}
845
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000846GLenum Framebuffer::getReadColorbufferType() const
847{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400848 const FramebufferAttachment *readAttachment = mState.getReadAttachment();
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400849 return (readAttachment != nullptr ? readAttachment->type() : GL_NONE);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000850}
851
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400852const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000853{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400854 return mState.getFirstColorAttachment();
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000855}
856
Jamie Madill6dd06ea2017-07-19 13:47:55 -0400857const FramebufferAttachment *Framebuffer::getFirstNonNullAttachment() const
858{
859 return mState.getFirstNonNullAttachment();
860}
861
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800862const FramebufferAttachment *Framebuffer::getAttachment(const Context *context,
863 GLenum attachment) const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000864{
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -0800865 return mState.getAttachment(context, attachment);
Geoff Lang55ba29c2013-07-11 16:57:53 -0400866}
867
Geoff Langa15472a2015-08-11 11:48:03 -0400868size_t Framebuffer::getDrawbufferStateCount() const
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000869{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400870 return mState.mDrawBufferStates.size();
Geoff Langa15472a2015-08-11 11:48:03 -0400871}
872
873GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const
874{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400875 ASSERT(drawBuffer < mState.mDrawBufferStates.size());
876 return mState.mDrawBufferStates[drawBuffer];
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000877}
878
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500879const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const
880{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400881 return mState.getDrawBufferStates();
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500882}
883
Geoff Lang164d54e2014-12-01 10:55:33 -0500884void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000885{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400886 auto &drawStates = mState.mDrawBufferStates;
Jamie Madilld1405e52015-03-05 15:41:39 -0500887
888 ASSERT(count <= drawStates.size());
889 std::copy(buffers, buffers + count, drawStates.begin());
890 std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500891 mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS);
Jamie Madilla4595b82017-01-11 17:36:34 -0500892
893 mState.mEnabledDrawBuffers.reset();
Brandon Jones76746f92017-11-22 11:44:41 -0800894 mState.mDrawBufferTypeMask.reset();
895
Jamie Madilla4595b82017-01-11 17:36:34 -0500896 for (size_t index = 0; index < count; ++index)
897 {
Brandon Jones76746f92017-11-22 11:44:41 -0800898 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(index), index);
899
Jamie Madilla4595b82017-01-11 17:36:34 -0500900 if (drawStates[index] != GL_NONE && mState.mColorAttachments[index].isAttached())
901 {
902 mState.mEnabledDrawBuffers.set(index);
903 }
904 }
Geoff Lang9dd95802014-12-01 11:12:59 -0500905}
906
Geoff Langa15472a2015-08-11 11:48:03 -0400907const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const
908{
Geoff Lang4b7f12b2016-06-21 16:47:07 -0400909 return mState.getDrawBuffer(drawBuffer);
Geoff Langa15472a2015-08-11 11:48:03 -0400910}
911
Geoff Lange0cff192017-05-30 13:04:56 -0400912GLenum Framebuffer::getDrawbufferWriteType(size_t drawBuffer) const
913{
914 const FramebufferAttachment *attachment = mState.getDrawBuffer(drawBuffer);
915 if (attachment == nullptr)
916 {
917 return GL_NONE;
918 }
919
920 GLenum componentType = attachment->getFormat().info->componentType;
921 switch (componentType)
922 {
923 case GL_INT:
924 case GL_UNSIGNED_INT:
925 return componentType;
926
927 default:
928 return GL_FLOAT;
929 }
930}
931
Brandon Jonesc405ae72017-12-06 14:15:03 -0800932ComponentTypeMask Framebuffer::getDrawBufferTypeMask() const
Brandon Jones76746f92017-11-22 11:44:41 -0800933{
934 return mState.mDrawBufferTypeMask;
935}
936
937DrawBufferMask Framebuffer::getDrawBufferMask() const
938{
939 return mState.mEnabledDrawBuffers;
940}
941
Geoff Langa15472a2015-08-11 11:48:03 -0400942bool Framebuffer::hasEnabledDrawBuffer() const
943{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400944 for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
Geoff Langa15472a2015-08-11 11:48:03 -0400945 {
946 if (getDrawBuffer(drawbufferIdx) != nullptr)
947 {
948 return true;
949 }
950 }
951
952 return false;
953}
954
Geoff Lang9dd95802014-12-01 11:12:59 -0500955GLenum Framebuffer::getReadBufferState() const
956{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400957 return mState.mReadBufferState;
Geoff Lang9dd95802014-12-01 11:12:59 -0500958}
959
960void Framebuffer::setReadBuffer(GLenum buffer)
961{
Jamie Madillb885e572015-02-03 16:16:04 -0500962 ASSERT(buffer == GL_BACK || buffer == GL_NONE ||
963 (buffer >= GL_COLOR_ATTACHMENT0 &&
Jamie Madill48ef11b2016-04-27 15:21:52 -0400964 (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size()));
965 mState.mReadBufferState = buffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500966 mDirtyBits.set(DIRTY_BIT_READ_BUFFER);
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000967}
968
Corentin Wallez37c39792015-08-20 14:19:46 -0400969size_t Framebuffer::getNumColorBuffers() const
970{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400971 return mState.mColorAttachments.size();
Corentin Wallez37c39792015-08-20 14:19:46 -0400972}
973
Jamie Madill0df8fe42015-11-24 16:10:24 -0500974bool Framebuffer::hasDepth() const
975{
Jamie Madill9c335862017-07-18 11:51:38 -0400976 return mState.hasDepth();
Jamie Madill0df8fe42015-11-24 16:10:24 -0500977}
978
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000979bool Framebuffer::hasStencil() const
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000980{
Jamie Madill9c335862017-07-18 11:51:38 -0400981 return mState.hasStencil();
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000982}
983
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000984bool Framebuffer::usingExtendedDrawBuffers() const
985{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400986 for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000987 {
Geoff Langa15472a2015-08-11 11:48:03 -0400988 if (getDrawBuffer(drawbufferIdx) != nullptr)
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000989 {
990 return true;
991 }
992 }
993
994 return false;
995}
996
Geoff Lang9aded172017-04-05 11:07:56 -0400997void Framebuffer::invalidateCompletenessCache()
998{
Jamie Madill2274b652018-05-31 10:56:08 -0400999 if (mState.mId != 0)
Geoff Lang9aded172017-04-05 11:07:56 -04001000 {
1001 mCachedStatus.reset();
1002 }
1003}
1004
Jamie Madill427064d2018-04-13 16:20:34 -04001005GLenum Framebuffer::checkStatus(const Context *context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006{
Corentin Wallezccab69d2017-01-27 16:57:15 -05001007 // The default framebuffer is always complete except when it is surfaceless in which
1008 // case it is always unsupported. We return early because the default framebuffer may
1009 // not be subject to the same rules as application FBOs. ie, it could have 0x0 size.
Jamie Madill2274b652018-05-31 10:56:08 -04001010 if (mState.mId == 0)
Geoff Lang528ce3c2014-12-01 10:44:07 -05001011 {
Corentin Wallezccab69d2017-01-27 16:57:15 -05001012 ASSERT(mCachedStatus.valid());
1013 ASSERT(mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE ||
1014 mCachedStatus.value() == GL_FRAMEBUFFER_UNDEFINED_OES);
Jamie Madill427064d2018-04-13 16:20:34 -04001015 return mCachedStatus.value();
Geoff Lang528ce3c2014-12-01 10:44:07 -05001016 }
1017
Jamie Madill362876b2016-06-16 14:46:59 -04001018 if (hasAnyDirtyBit() || !mCachedStatus.valid())
1019 {
Jamie Madille98b1b52018-03-08 09:47:23 -05001020 mCachedStatus = checkStatusWithGLFrontEnd(context);
1021
1022 if (mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE)
1023 {
Jamie Madill427064d2018-04-13 16:20:34 -04001024 Error err = syncState(context);
1025 if (err.isError())
1026 {
1027 context->handleError(err);
1028 return GetDefaultReturnValue<EntryPoint::CheckFramebufferStatus, GLenum>();
1029 }
Jamie Madille98b1b52018-03-08 09:47:23 -05001030 if (!mImpl->checkStatus(context))
1031 {
1032 mCachedStatus = GL_FRAMEBUFFER_UNSUPPORTED;
1033 }
1034 }
Jamie Madill362876b2016-06-16 14:46:59 -04001035 }
1036
Jamie Madill427064d2018-04-13 16:20:34 -04001037 return mCachedStatus.value();
Jamie Madill362876b2016-06-16 14:46:59 -04001038}
1039
Jamie Madille98b1b52018-03-08 09:47:23 -05001040GLenum Framebuffer::checkStatusWithGLFrontEnd(const Context *context)
Jamie Madill362876b2016-06-16 14:46:59 -04001041{
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001042 const ContextState &state = context->getContextState();
1043
Jamie Madill2274b652018-05-31 10:56:08 -04001044 ASSERT(mState.mId != 0);
Jamie Madill362876b2016-06-16 14:46:59 -04001045
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001046 bool hasAttachments = false;
1047 Optional<unsigned int> colorbufferSize;
1048 Optional<int> samples;
Geoff Lang92019432017-11-20 13:09:34 -05001049 Optional<bool> fixedSampleLocations;
JiangYizhou461d9a32017-01-04 16:37:26 +08001050 bool hasRenderbuffer = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001051
Martin Radev9bc9a322017-07-21 14:28:17 +03001052 const FramebufferAttachment *firstAttachment = getFirstNonNullAttachment();
1053
Jiawei Shaoa8802472018-05-28 11:17:47 +08001054 Optional<bool> isLayered;
1055 Optional<TextureType> colorAttachmentsTextureType;
1056
Jamie Madill48ef11b2016-04-27 15:21:52 -04001057 for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058 {
Jamie Madill2d06b732015-04-20 12:53:28 -04001059 if (colorAttachment.isAttached())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001061 if (!CheckAttachmentCompleteness(context, colorAttachment))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001062 {
1063 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1064 }
daniel@transgaming.com01868132010-08-24 19:21:17 +00001065
Geoff Lang677bb6f2017-04-05 12:40:40 -04001066 const InternalFormat &format = *colorAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001067 if (format.depthBits > 0 || format.stencilBits > 0)
1068 {
1069 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1070 }
1071
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001072 if (!CheckAttachmentSampleCompleteness(context, colorAttachment, true, &samples,
1073 &fixedSampleLocations))
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001074 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001075 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001076 }
1077
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001078 // in GLES 2.0, all color attachments attachments must have the same number of bitplanes
1079 // in GLES 3.0, there is no such restriction
1080 if (state.getClientMajorVersion() < 3)
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001081 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001082 if (colorbufferSize.valid())
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001083 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001084 if (format.pixelBytes != colorbufferSize.value())
shannon.woods%transgaming.com@gtempaccount.comc3471522013-04-13 03:34:52 +00001085 {
1086 return GL_FRAMEBUFFER_UNSUPPORTED;
1087 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001088 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001089 else
1090 {
1091 colorbufferSize = format.pixelBytes;
1092 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001093 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001094
Martin Radev9bc9a322017-07-21 14:28:17 +03001095 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &colorAttachment))
1096 {
1097 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1098 }
1099
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001100 hasRenderbuffer = hasRenderbuffer || (colorAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001101
1102 if (!hasAttachments)
1103 {
1104 isLayered = colorAttachment.isLayered();
1105 if (isLayered.value())
1106 {
1107 colorAttachmentsTextureType = colorAttachment.getTextureImageIndex().getType();
1108 }
1109 hasAttachments = true;
1110 }
1111 else
1112 {
1113 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1114 // If any framebuffer attachment is layered, all populated attachments
1115 // must be layered. Additionally, all populated color attachments must
1116 // be from textures of the same target. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1117 ASSERT(isLayered.valid());
1118 if (isLayered.value() != colorAttachment.isLayered())
1119 {
1120 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1121 }
1122 else if (isLayered.value())
1123 {
1124 ASSERT(colorAttachmentsTextureType.valid());
1125 if (colorAttachmentsTextureType.value() !=
1126 colorAttachment.getTextureImageIndex().getType())
1127 {
1128 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1129 }
1130 }
1131 }
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00001132 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001133 }
1134
Jamie Madill48ef11b2016-04-27 15:21:52 -04001135 const FramebufferAttachment &depthAttachment = mState.mDepthAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001136 if (depthAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001137 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001138 if (!CheckAttachmentCompleteness(context, depthAttachment))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001139 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001140 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141 }
1142
Geoff Lang677bb6f2017-04-05 12:40:40 -04001143 const InternalFormat &format = *depthAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001144 if (format.depthBits == 0)
1145 {
1146 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001147 }
1148
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001149 if (!CheckAttachmentSampleCompleteness(context, depthAttachment, false, &samples,
1150 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001151 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001152 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001153 }
Sami Väisänena797e062016-05-12 15:23:40 +03001154
Martin Radev9bc9a322017-07-21 14:28:17 +03001155 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &depthAttachment))
1156 {
1157 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1158 }
1159
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001160 hasRenderbuffer = hasRenderbuffer || (depthAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001161
1162 if (!hasAttachments)
1163 {
1164 isLayered = depthAttachment.isLayered();
1165 hasAttachments = true;
1166 }
1167 else
1168 {
1169 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1170 // If any framebuffer attachment is layered, all populated attachments
1171 // must be layered. {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1172 ASSERT(isLayered.valid());
1173 if (isLayered.value() != depthAttachment.isLayered())
1174 {
1175 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1176 }
1177 }
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001178 }
1179
Jamie Madill48ef11b2016-04-27 15:21:52 -04001180 const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment;
Jamie Madill2d06b732015-04-20 12:53:28 -04001181 if (stencilAttachment.isAttached())
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001182 {
Geoff Lang9f10b772017-05-16 15:51:03 -04001183 if (!CheckAttachmentCompleteness(context, stencilAttachment))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001184 {
1185 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1186 }
1187
Geoff Lang677bb6f2017-04-05 12:40:40 -04001188 const InternalFormat &format = *stencilAttachment.getFormat().info;
Geoff Lang677bb6f2017-04-05 12:40:40 -04001189 if (format.stencilBits == 0)
1190 {
1191 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001192 }
1193
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001194 if (!CheckAttachmentSampleCompleteness(context, stencilAttachment, false, &samples,
1195 &fixedSampleLocations))
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001196 {
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001197 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001198 }
Corentin Wallez086d59a2016-04-29 09:06:49 -04001199
Martin Radev9bc9a322017-07-21 14:28:17 +03001200 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &stencilAttachment))
1201 {
1202 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1203 }
1204
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001205 hasRenderbuffer = hasRenderbuffer || (stencilAttachment.type() == GL_RENDERBUFFER);
Jiawei Shaoa8802472018-05-28 11:17:47 +08001206
1207 if (!hasAttachments)
1208 {
1209 hasAttachments = true;
1210 }
1211 else
1212 {
1213 // [EXT_geometry_shader] section 9.4.1, "Framebuffer Completeness"
1214 // If any framebuffer attachment is layered, all populated attachments
1215 // must be layered.
1216 // {FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT }
1217 ASSERT(isLayered.valid());
1218 if (isLayered.value() != stencilAttachment.isLayered())
1219 {
1220 return GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT;
1221 }
1222 }
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001223 }
1224
1225 // Starting from ES 3.0 stencil and depth, if present, should be the same image
1226 if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() &&
1227 stencilAttachment.isAttached() && stencilAttachment != depthAttachment)
1228 {
1229 return GL_FRAMEBUFFER_UNSUPPORTED;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001230 }
1231
Jamie Madilla02315b2017-02-23 14:14:47 -05001232 // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL.
1233 if (state.isWebGL1())
1234 {
1235 if (!mState.mWebGLDepthStencilConsistent)
1236 {
1237 return GL_FRAMEBUFFER_UNSUPPORTED;
1238 }
1239
1240 if (mState.mWebGLDepthStencilAttachment.isAttached())
1241 {
1242 if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 ||
1243 mState.mWebGLDepthStencilAttachment.getStencilSize() == 0)
1244 {
1245 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1246 }
Martin Radev9bc9a322017-07-21 14:28:17 +03001247
1248 if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment,
1249 &mState.mWebGLDepthStencilAttachment))
1250 {
1251 return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE;
1252 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001253 }
1254 else if (mState.mStencilAttachment.isAttached() &&
1255 mState.mStencilAttachment.getDepthSize() > 0)
1256 {
1257 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1258 }
1259 else if (mState.mDepthAttachment.isAttached() &&
1260 mState.mDepthAttachment.getStencilSize() > 0)
1261 {
1262 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1263 }
1264 }
1265
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001266 // ES3.1(section 9.4) requires that if no image is attached to the framebuffer, and either the
1267 // value of the framebuffer's FRAMEBUFFER_DEFAULT_WIDTH or FRAMEBUFFER_DEFAULT_HEIGHT parameters
1268 // is zero, the framebuffer is considered incomplete.
JiangYizhou461d9a32017-01-04 16:37:26 +08001269 GLint defaultWidth = mState.getDefaultWidth();
1270 GLint defaultHeight = mState.getDefaultHeight();
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001271 if (!hasAttachments && (defaultWidth == 0 || defaultHeight == 0))
daniel@transgaming.com6b7c84c2012-05-31 01:14:39 +00001272 {
1273 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001274 }
1275
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001276 // In ES 2.0 and WebGL, all color attachments must have the same width and height.
Jamie Madillcc86d642015-11-24 13:00:07 -05001277 // In ES 3.0, there is no such restriction.
Geoff Langa0e0aeb2017-04-12 15:06:29 -04001278 if ((state.getClientMajorVersion() < 3 || state.getExtensions().webglCompatibility) &&
1279 !mState.attachmentsHaveSameDimensions())
Jamie Madillcc86d642015-11-24 13:00:07 -05001280 {
1281 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
1282 }
1283
Geoff Langa1b9d5a2017-05-18 11:22:27 -04001284 // ES3.1(section 9.4) requires that if the attached images are a mix of renderbuffers and
1285 // textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all attached textures.
JiangYizhou461d9a32017-01-04 16:37:26 +08001286 if (fixedSampleLocations.valid() && hasRenderbuffer && !fixedSampleLocations.value())
1287 {
1288 return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1289 }
1290
Kenneth Russellce8602a2017-10-03 18:23:08 -07001291 // The WebGL conformance tests implicitly define that all framebuffer
1292 // attachments must be unique. For example, the same level of a texture can
1293 // not be attached to two different color attachments.
1294 if (state.getExtensions().webglCompatibility)
1295 {
1296 if (!mState.colorAttachmentsAreUniqueImages())
1297 {
1298 return GL_FRAMEBUFFER_UNSUPPORTED;
1299 }
1300 }
1301
Jamie Madillcc86d642015-11-24 13:00:07 -05001302 return GL_FRAMEBUFFER_COMPLETE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001303}
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001304
Jamie Madill4928b7c2017-06-20 12:57:39 -04001305Error Framebuffer::discard(const Context *context, size_t count, const GLenum *attachments)
Austin Kinross08332632015-05-05 13:35:47 -07001306{
Jamie Madill05b35b22017-10-03 09:01:44 -04001307 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1308 // can be no-ops, so we should probably do that to ensure consistency.
1309 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1310
Jamie Madill4928b7c2017-06-20 12:57:39 -04001311 return mImpl->discard(context, count, attachments);
Austin Kinross08332632015-05-05 13:35:47 -07001312}
1313
Jamie Madill4928b7c2017-06-20 12:57:39 -04001314Error Framebuffer::invalidate(const Context *context, size_t count, const GLenum *attachments)
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001315{
Jamie Madill05b35b22017-10-03 09:01:44 -04001316 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1317 // can be no-ops, so we should probably do that to ensure consistency.
1318 // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL.
1319
Jamie Madill4928b7c2017-06-20 12:57:39 -04001320 return mImpl->invalidate(context, count, attachments);
Jamie Madill2d96b9e2014-08-29 15:46:47 -04001321}
1322
Jamie Madill05b35b22017-10-03 09:01:44 -04001323bool Framebuffer::partialClearNeedsInit(const Context *context,
1324 bool color,
1325 bool depth,
1326 bool stencil)
1327{
1328 const auto &glState = context->getGLState();
1329
1330 if (!glState.isRobustResourceInitEnabled())
1331 {
1332 return false;
1333 }
1334
1335 // Scissors can affect clearing.
1336 // TODO(jmadill): Check for complete scissor overlap.
1337 if (glState.isScissorTestEnabled())
1338 {
1339 return true;
1340 }
1341
1342 // If colors masked, we must clear before we clear. Do a simple check.
1343 // TODO(jmadill): Filter out unused color channels from the test.
1344 if (color)
1345 {
1346 const auto &blend = glState.getBlendState();
1347 if (!(blend.colorMaskRed && blend.colorMaskGreen && blend.colorMaskBlue &&
1348 blend.colorMaskAlpha))
1349 {
1350 return true;
1351 }
1352 }
1353
1354 const auto &depthStencil = glState.getDepthStencilState();
1355 ASSERT(depthStencil.stencilBackMask == depthStencil.stencilMask);
1356 if (stencil && depthStencil.stencilMask != depthStencil.stencilWritemask)
1357 {
1358 return true;
1359 }
1360
1361 return false;
1362}
1363
Jamie Madill4928b7c2017-06-20 12:57:39 -04001364Error Framebuffer::invalidateSub(const Context *context,
1365 size_t count,
1366 const GLenum *attachments,
Jamie Madilld4442552018-02-27 22:03:47 -05001367 const Rectangle &area)
Jamie Madill400a4412014-08-29 15:46:45 -04001368{
Jamie Madill05b35b22017-10-03 09:01:44 -04001369 // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations
1370 // can be no-ops, so we should probably do that to ensure consistency.
1371 // TODO(jmadill): Make a invalidate no-op in WebGL 2.0.
1372
Jamie Madill4928b7c2017-06-20 12:57:39 -04001373 return mImpl->invalidateSub(context, count, attachments, area);
Jamie Madill400a4412014-08-29 15:46:45 -04001374}
1375
Jamie Madilld4442552018-02-27 22:03:47 -05001376Error Framebuffer::clear(const Context *context, GLbitfield mask)
Geoff Langb04dc822014-12-01 12:02:02 -05001377{
Jamie Madill05b35b22017-10-03 09:01:44 -04001378 const auto &glState = context->getGLState();
1379 if (glState.isRasterizerDiscardEnabled())
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->clear(context, mask));
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::clearBufferfv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001390 GLenum buffer,
1391 GLint drawbuffer,
1392 const GLfloat *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->clearBufferfv(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::clearBufferuiv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001406 GLenum buffer,
1407 GLint drawbuffer,
1408 const GLuint *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->clearBufferuiv(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::clearBufferiv(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001422 GLenum buffer,
1423 GLint drawbuffer,
1424 const GLint *values)
Geoff Langb04dc822014-12-01 12:02:02 -05001425{
Jamie Madill05b35b22017-10-03 09:01:44 -04001426 if (context->getGLState().isRasterizerDiscardEnabled() ||
1427 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001428 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001429 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001430 }
1431
Jamie Madill05b35b22017-10-03 09:01:44 -04001432 ANGLE_TRY(mImpl->clearBufferiv(context, buffer, drawbuffer, values));
1433
Jamie Madill05b35b22017-10-03 09:01:44 -04001434 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001435}
1436
Jamie Madilld4442552018-02-27 22:03:47 -05001437Error Framebuffer::clearBufferfi(const Context *context,
Jamie Madill1b94d432015-08-07 13:23:23 -04001438 GLenum buffer,
1439 GLint drawbuffer,
1440 GLfloat depth,
1441 GLint stencil)
Geoff Langb04dc822014-12-01 12:02:02 -05001442{
Jamie Madill05b35b22017-10-03 09:01:44 -04001443 if (context->getGLState().isRasterizerDiscardEnabled() ||
1444 IsClearBufferMaskedOut(context, buffer))
Jamie Madill984ef412015-11-24 16:10:21 -05001445 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001446 return NoError();
Jamie Madill984ef412015-11-24 16:10:21 -05001447 }
1448
Jamie Madill05b35b22017-10-03 09:01:44 -04001449 ANGLE_TRY(mImpl->clearBufferfi(context, buffer, drawbuffer, depth, stencil));
1450
Jamie Madill05b35b22017-10-03 09:01:44 -04001451 return NoError();
Geoff Langb04dc822014-12-01 12:02:02 -05001452}
1453
Jamie Madill690c8eb2018-03-12 15:20:03 -04001454Error Framebuffer::getImplementationColorReadFormat(const Context *context, GLenum *formatOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001455{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001456 ANGLE_TRY(syncState(context));
1457 *formatOut = mImpl->getImplementationColorReadFormat(context);
1458 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -05001459}
1460
Jamie Madill690c8eb2018-03-12 15:20:03 -04001461Error Framebuffer::getImplementationColorReadType(const Context *context, GLenum *typeOut)
Geoff Langbce529e2014-12-01 12:48:41 -05001462{
Jamie Madill690c8eb2018-03-12 15:20:03 -04001463 ANGLE_TRY(syncState(context));
1464 *typeOut = mImpl->getImplementationColorReadType(context);
1465 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -05001466}
1467
Jamie Madilld4442552018-02-27 22:03:47 -05001468Error Framebuffer::readPixels(const Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001469 const Rectangle &area,
Jamie Madill1b94d432015-08-07 13:23:23 -04001470 GLenum format,
1471 GLenum type,
Jamie Madill05b35b22017-10-03 09:01:44 -04001472 void *pixels)
Geoff Langbce529e2014-12-01 12:48:41 -05001473{
Jamie Madill05b35b22017-10-03 09:01:44 -04001474 ANGLE_TRY(ensureReadAttachmentInitialized(context, GL_COLOR_BUFFER_BIT));
Jamie Madill362876b2016-06-16 14:46:59 -04001475 ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels));
Geoff Lang520c4ae2015-05-05 13:12:36 -04001476
Jamie Madilld4442552018-02-27 22:03:47 -05001477 Buffer *unpackBuffer = context->getGLState().getTargetBuffer(BufferBinding::PixelUnpack);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001478 if (unpackBuffer)
1479 {
Jamie Madill09463932018-04-04 05:26:59 -04001480 unpackBuffer->onPixelPack(context);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001481 }
1482
Jamie Madill362876b2016-06-16 14:46:59 -04001483 return NoError();
Geoff Langbce529e2014-12-01 12:48:41 -05001484}
1485
Jamie Madilld4442552018-02-27 22:03:47 -05001486Error Framebuffer::blit(const Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001487 const Rectangle &sourceArea,
1488 const Rectangle &destArea,
Geoff Lang242468f2015-09-24 14:15:41 -04001489 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -04001490 GLenum filter)
Geoff Lang54bd5a42014-12-01 12:51:04 -05001491{
He Yunchao6be602d2016-12-22 14:33:07 +08001492 GLbitfield blitMask = mask;
1493
1494 // Note that blitting is called against draw framebuffer.
1495 // See the code in gl::Context::blitFramebuffer.
1496 if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer())
1497 {
1498 blitMask &= ~GL_COLOR_BUFFER_BIT;
1499 }
1500
1501 if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr)
1502 {
1503 blitMask &= ~GL_STENCIL_BUFFER_BIT;
1504 }
1505
1506 if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr)
1507 {
1508 blitMask &= ~GL_DEPTH_BUFFER_BIT;
1509 }
1510
1511 if (!blitMask)
1512 {
1513 return NoError();
1514 }
1515
Jamie Madill05b35b22017-10-03 09:01:44 -04001516 auto *sourceFBO = context->getGLState().getReadFramebuffer();
1517 ANGLE_TRY(sourceFBO->ensureReadAttachmentInitialized(context, blitMask));
1518
1519 // TODO(jmadill): Only clear if not the full FBO dimensions, and only specified bitmask.
1520 ANGLE_TRY(ensureDrawAttachmentsInitialized(context));
1521
He Yunchao6be602d2016-12-22 14:33:07 +08001522 return mImpl->blit(context, sourceArea, destArea, blitMask, filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -05001523}
1524
Luc Ferronbf6dc372018-06-28 15:24:19 -04001525bool Framebuffer::isDefault() const
1526{
1527 return id() == 0;
1528}
1529
Jamie Madill427064d2018-04-13 16:20:34 -04001530int Framebuffer::getSamples(const Context *context)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001531{
Jamie Madill427064d2018-04-13 16:20:34 -04001532 return (isComplete(context) ? getCachedSamples(context) : 0);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001533}
1534
Jamie Madill9c335862017-07-18 11:51:38 -04001535int Framebuffer::getCachedSamples(const Context *context)
1536{
Jamie Madill5b772312018-03-08 20:28:32 -05001537 ASSERT(mCachedStatus.valid() && mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE);
1538
Jamie Madill9c335862017-07-18 11:51:38 -04001539 // For a complete framebuffer, all attachments must have the same sample count.
1540 // In this case return the first nonzero sample size.
1541 const auto *firstNonNullAttachment = mState.getFirstNonNullAttachment();
1542 if (firstNonNullAttachment)
1543 {
1544 ASSERT(firstNonNullAttachment->isAttached());
1545 return firstNonNullAttachment->getSamples();
1546 }
1547
1548 // No attachments found.
1549 return 0;
1550}
1551
Geoff Lang13455072018-05-09 11:24:43 -04001552Error Framebuffer::getSamplePosition(const Context *context, size_t index, GLfloat *xy) const
Corentin Wallezccab69d2017-01-27 16:57:15 -05001553{
Geoff Lang13455072018-05-09 11:24:43 -04001554 ANGLE_TRY(mImpl->getSamplePosition(context, index, xy));
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001555 return NoError();
Corentin Wallezccab69d2017-01-27 16:57:15 -05001556}
1557
Jamie Madille261b442014-06-25 12:42:21 -04001558bool Framebuffer::hasValidDepthStencil() const
1559{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001560 return mState.getDepthStencilAttachment() != nullptr;
Jamie Madille261b442014-06-25 12:42:21 -04001561}
1562
Jamie Madilla02315b2017-02-23 14:14:47 -05001563void Framebuffer::setAttachment(const Context *context,
1564 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -04001565 GLenum binding,
1566 const ImageIndex &textureIndex,
1567 FramebufferAttachmentObject *resource)
Geoff Langab75a052014-10-15 12:56:37 -04001568{
Martin Radev5dae57b2017-07-14 16:15:55 +03001569 setAttachment(context, type, binding, textureIndex, resource,
1570 FramebufferAttachment::kDefaultNumViews,
1571 FramebufferAttachment::kDefaultBaseViewIndex,
1572 FramebufferAttachment::kDefaultMultiviewLayout,
1573 FramebufferAttachment::kDefaultViewportOffsets);
1574}
1575
1576void Framebuffer::setAttachment(const Context *context,
1577 GLenum type,
1578 GLenum binding,
1579 const ImageIndex &textureIndex,
1580 FramebufferAttachmentObject *resource,
1581 GLsizei numViews,
1582 GLuint baseViewIndex,
1583 GLenum multiviewLayout,
1584 const GLint *viewportOffsets)
1585{
Jamie Madilla02315b2017-02-23 14:14:47 -05001586 // Context may be null in unit tests.
1587 if (!context || !context->isWebGL1())
1588 {
Martin Radev5dae57b2017-07-14 16:15:55 +03001589 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1590 multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001591 return;
1592 }
1593
1594 switch (binding)
1595 {
1596 case GL_DEPTH_STENCIL:
1597 case GL_DEPTH_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001598 mState.mWebGLDepthStencilAttachment.attach(context, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001599 resource, numViews, baseViewIndex,
1600 multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001601 break;
1602 case GL_DEPTH:
1603 case GL_DEPTH_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001604 mState.mWebGLDepthAttachment.attach(context, type, binding, textureIndex, resource,
1605 numViews, baseViewIndex, multiviewLayout,
1606 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001607 break;
1608 case GL_STENCIL:
1609 case GL_STENCIL_ATTACHMENT:
Martin Radev5dae57b2017-07-14 16:15:55 +03001610 mState.mWebGLStencilAttachment.attach(context, type, binding, textureIndex, resource,
1611 numViews, baseViewIndex, multiviewLayout,
1612 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001613 break;
1614 default:
Martin Radev5dae57b2017-07-14 16:15:55 +03001615 setAttachmentImpl(context, type, binding, textureIndex, resource, numViews,
1616 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001617 return;
1618 }
1619
Martin Radev5dae57b2017-07-14 16:15:55 +03001620 commitWebGL1DepthStencilIfConsistent(context, numViews, baseViewIndex, multiviewLayout,
1621 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001622}
1623
Martin Radev82ef7742017-08-08 17:44:58 +03001624void Framebuffer::setAttachmentMultiviewLayered(const Context *context,
1625 GLenum type,
1626 GLenum binding,
1627 const ImageIndex &textureIndex,
1628 FramebufferAttachmentObject *resource,
1629 GLsizei numViews,
1630 GLint baseViewIndex)
1631{
1632 setAttachment(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1633 GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE,
1634 FramebufferAttachment::kDefaultViewportOffsets);
1635}
1636
Martin Radev5dae57b2017-07-14 16:15:55 +03001637void Framebuffer::setAttachmentMultiviewSideBySide(const Context *context,
1638 GLenum type,
1639 GLenum binding,
1640 const ImageIndex &textureIndex,
1641 FramebufferAttachmentObject *resource,
1642 GLsizei numViews,
1643 const GLint *viewportOffsets)
1644{
1645 setAttachment(context, type, binding, textureIndex, resource, numViews,
1646 FramebufferAttachment::kDefaultBaseViewIndex,
1647 GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE, viewportOffsets);
1648}
1649
1650void Framebuffer::commitWebGL1DepthStencilIfConsistent(const Context *context,
1651 GLsizei numViews,
1652 GLuint baseViewIndex,
1653 GLenum multiviewLayout,
1654 const GLint *viewportOffsets)
Jamie Madilla02315b2017-02-23 14:14:47 -05001655{
1656 int count = 0;
1657
1658 std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment,
1659 &mState.mWebGLDepthAttachment,
1660 &mState.mWebGLStencilAttachment}};
1661 for (FramebufferAttachment *attachment : attachments)
1662 {
1663 if (attachment->isAttached())
1664 {
1665 count++;
1666 }
1667 }
1668
1669 mState.mWebGLDepthStencilConsistent = (count <= 1);
1670 if (!mState.mWebGLDepthStencilConsistent)
1671 {
1672 // Inconsistent.
1673 return;
1674 }
1675
Geoff Lange466c552017-03-17 15:24:12 -04001676 auto getImageIndexIfTextureAttachment = [](const FramebufferAttachment &attachment) {
1677 if (attachment.type() == GL_TEXTURE)
1678 {
1679 return attachment.getTextureImageIndex();
1680 }
1681 else
1682 {
Jamie Madillcc129372018-04-12 09:13:18 -04001683 return ImageIndex();
Geoff Lange466c552017-03-17 15:24:12 -04001684 }
1685 };
1686
Jamie Madilla02315b2017-02-23 14:14:47 -05001687 if (mState.mWebGLDepthAttachment.isAttached())
1688 {
1689 const auto &depth = mState.mWebGLDepthAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001690 setAttachmentImpl(context, depth.type(), GL_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001691 getImageIndexIfTextureAttachment(depth), depth.getResource(), numViews,
1692 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madillcc129372018-04-12 09:13:18 -04001693 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
1694 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001695 }
1696 else if (mState.mWebGLStencilAttachment.isAttached())
1697 {
1698 const auto &stencil = mState.mWebGLStencilAttachment;
Jamie Madillcc129372018-04-12 09:13:18 -04001699 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
1700 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001701 setAttachmentImpl(context, stencil.type(), GL_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001702 getImageIndexIfTextureAttachment(stencil), stencil.getResource(),
1703 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001704 }
1705 else if (mState.mWebGLDepthStencilAttachment.isAttached())
1706 {
1707 const auto &depthStencil = mState.mWebGLDepthStencilAttachment;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001708 setAttachmentImpl(context, depthStencil.type(), GL_DEPTH_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001709 getImageIndexIfTextureAttachment(depthStencil),
Martin Radev5dae57b2017-07-14 16:15:55 +03001710 depthStencil.getResource(), numViews, baseViewIndex, multiviewLayout,
1711 viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001712 setAttachmentImpl(context, depthStencil.type(), GL_STENCIL_ATTACHMENT,
Geoff Lange466c552017-03-17 15:24:12 -04001713 getImageIndexIfTextureAttachment(depthStencil),
Martin Radev5dae57b2017-07-14 16:15:55 +03001714 depthStencil.getResource(), numViews, baseViewIndex, multiviewLayout,
1715 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001716 }
1717 else
1718 {
Jamie Madillcc129372018-04-12 09:13:18 -04001719 setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex(), nullptr, numViews,
1720 baseViewIndex, multiviewLayout, viewportOffsets);
1721 setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex(), nullptr, numViews,
1722 baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001723 }
1724}
1725
Jamie Madill4928b7c2017-06-20 12:57:39 -04001726void Framebuffer::setAttachmentImpl(const Context *context,
1727 GLenum type,
Jamie Madilla02315b2017-02-23 14:14:47 -05001728 GLenum binding,
1729 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001730 FramebufferAttachmentObject *resource,
1731 GLsizei numViews,
1732 GLuint baseViewIndex,
1733 GLenum multiviewLayout,
1734 const GLint *viewportOffsets)
Jamie Madilla02315b2017-02-23 14:14:47 -05001735{
Jamie Madilla02315b2017-02-23 14:14:47 -05001736 switch (binding)
1737 {
Jamie Madillb8126692017-04-05 11:22:17 -04001738 case GL_DEPTH_STENCIL:
1739 case GL_DEPTH_STENCIL_ATTACHMENT:
1740 {
1741 // ensure this is a legitimate depth+stencil format
1742 FramebufferAttachmentObject *attachmentObj = resource;
1743 if (resource)
1744 {
Jamie Madill4fd95d52017-04-05 11:22:18 -04001745 const Format &format = resource->getAttachmentFormat(binding, textureIndex);
Jamie Madillb8126692017-04-05 11:22:17 -04001746 if (format.info->depthBits == 0 || format.info->stencilBits == 0)
1747 {
1748 // Attaching nullptr detaches the current attachment.
1749 attachmentObj = nullptr;
1750 }
1751 }
1752
Jamie Madill4928b7c2017-06-20 12:57:39 -04001753 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001754 &mDirtyDepthAttachmentBinding, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001755 attachmentObj, numViews, baseViewIndex, multiviewLayout,
1756 viewportOffsets);
Jamie Madill4928b7c2017-06-20 12:57:39 -04001757 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Jamie Madillb8126692017-04-05 11:22:17 -04001758 &mDirtyStencilAttachmentBinding, type, binding, textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001759 attachmentObj, numViews, baseViewIndex, multiviewLayout,
1760 viewportOffsets);
Jamie Madill42975642017-10-12 12:31:51 -04001761 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001762 }
1763
Jamie Madilla02315b2017-02-23 14:14:47 -05001764 case GL_DEPTH:
1765 case GL_DEPTH_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001766 updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001767 &mDirtyDepthAttachmentBinding, type, binding, textureIndex, resource,
1768 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madill2d06b732015-04-20 12:53:28 -04001769 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001770
Jamie Madilla02315b2017-02-23 14:14:47 -05001771 case GL_STENCIL:
1772 case GL_STENCIL_ATTACHMENT:
Jamie Madill4928b7c2017-06-20 12:57:39 -04001773 updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT,
Martin Radev5dae57b2017-07-14 16:15:55 +03001774 &mDirtyStencilAttachmentBinding, type, binding, textureIndex, resource,
1775 numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001776 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001777
Jamie Madilla02315b2017-02-23 14:14:47 -05001778 case GL_BACK:
Geoff Lang8170eab2017-09-21 13:59:04 -04001779 updateAttachment(context, &mState.mColorAttachments[0], DIRTY_BIT_COLOR_ATTACHMENT_0,
1780 &mDirtyColorAttachmentBindings[0], type, binding, textureIndex,
1781 resource, numViews, baseViewIndex, multiviewLayout, viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001782 break;
Jamie Madillb8126692017-04-05 11:22:17 -04001783
Jamie Madilla02315b2017-02-23 14:14:47 -05001784 default:
1785 {
1786 size_t colorIndex = binding - GL_COLOR_ATTACHMENT0;
1787 ASSERT(colorIndex < mState.mColorAttachments.size());
Jamie Madillb8126692017-04-05 11:22:17 -04001788 size_t dirtyBit = DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001789 updateAttachment(context, &mState.mColorAttachments[colorIndex], dirtyBit,
Jamie Madillb8126692017-04-05 11:22:17 -04001790 &mDirtyColorAttachmentBindings[colorIndex], type, binding,
Martin Radev5dae57b2017-07-14 16:15:55 +03001791 textureIndex, resource, numViews, baseViewIndex, multiviewLayout,
1792 viewportOffsets);
Jamie Madilla02315b2017-02-23 14:14:47 -05001793
Corentin Walleze7557742017-06-01 13:09:57 -04001794 // TODO(jmadill): ASSERT instead of checking the attachment exists in
1795 // formsRenderingFeedbackLoopWith
Jamie Madilla02315b2017-02-23 14:14:47 -05001796 bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE);
1797 mState.mEnabledDrawBuffers.set(colorIndex, enabled);
Brandon Jones76746f92017-11-22 11:44:41 -08001798 mState.mDrawBufferTypeMask.setIndex(getDrawbufferWriteType(colorIndex), colorIndex);
Jamie Madill2d06b732015-04-20 12:53:28 -04001799 }
Jamie Madilla02315b2017-02-23 14:14:47 -05001800 break;
Geoff Langab75a052014-10-15 12:56:37 -04001801 }
Jamie Madill42975642017-10-12 12:31:51 -04001802
1803 mAttachedTextures.reset();
Geoff Langab75a052014-10-15 12:56:37 -04001804}
1805
Jamie Madill4928b7c2017-06-20 12:57:39 -04001806void Framebuffer::updateAttachment(const Context *context,
1807 FramebufferAttachment *attachment,
Jamie Madillb8126692017-04-05 11:22:17 -04001808 size_t dirtyBit,
Jamie Madilld4442552018-02-27 22:03:47 -05001809 angle::ObserverBinding *onDirtyBinding,
Jamie Madillb8126692017-04-05 11:22:17 -04001810 GLenum type,
1811 GLenum binding,
1812 const ImageIndex &textureIndex,
Martin Radev5dae57b2017-07-14 16:15:55 +03001813 FramebufferAttachmentObject *resource,
1814 GLsizei numViews,
1815 GLuint baseViewIndex,
1816 GLenum multiviewLayout,
1817 const GLint *viewportOffsets)
Jamie Madillb8126692017-04-05 11:22:17 -04001818{
Martin Radev5dae57b2017-07-14 16:15:55 +03001819 attachment->attach(context, type, binding, textureIndex, resource, numViews, baseViewIndex,
1820 multiviewLayout, viewportOffsets);
Jamie Madillb8126692017-04-05 11:22:17 -04001821 mDirtyBits.set(dirtyBit);
Jamie Madill05b35b22017-10-03 09:01:44 -04001822 mState.mResourceNeedsInit.set(dirtyBit, attachment->initState() == InitState::MayNeedInit);
Jamie Madill888081d2018-02-27 00:24:46 -05001823 onDirtyBinding->bind(resource ? resource->getSubject() : nullptr);
Jamie Madille98b1b52018-03-08 09:47:23 -05001824
1825 invalidateCompletenessCache();
Jamie Madillb8126692017-04-05 11:22:17 -04001826}
1827
Jamie Madilla02315b2017-02-23 14:14:47 -05001828void Framebuffer::resetAttachment(const Context *context, GLenum binding)
Jamie Madill2d06b732015-04-20 12:53:28 -04001829{
Jamie Madillcc129372018-04-12 09:13:18 -04001830 setAttachment(context, GL_NONE, binding, ImageIndex(), nullptr);
Jamie Madill2d06b732015-04-20 12:53:28 -04001831}
1832
Jamie Madill19fa1c62018-03-08 09:47:21 -05001833Error Framebuffer::syncState(const Context *context)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001834{
1835 if (mDirtyBits.any())
1836 {
Jamie Madill888081d2018-02-27 00:24:46 -05001837 mDirtyBitsGuard = mDirtyBits;
Jamie Madill19fa1c62018-03-08 09:47:21 -05001838 ANGLE_TRY(mImpl->syncState(context, mDirtyBits));
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001839 mDirtyBits.reset();
Jamie Madill888081d2018-02-27 00:24:46 -05001840 mDirtyBitsGuard.reset();
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001841 }
Jamie Madill19fa1c62018-03-08 09:47:21 -05001842 return NoError();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843}
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001844
Jamie Madilld4442552018-02-27 22:03:47 -05001845void Framebuffer::onSubjectStateChange(const Context *context,
1846 angle::SubjectIndex index,
1847 angle::SubjectMessage message)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001848{
Jamie Madill888081d2018-02-27 00:24:46 -05001849 if (message == angle::SubjectMessage::DEPENDENT_DIRTY_BITS)
1850 {
1851 ASSERT(!mDirtyBitsGuard.valid() || mDirtyBitsGuard.value().test(index));
1852 mDirtyBits.set(index);
1853 context->getGLState().setFramebufferDirty(this);
1854 return;
1855 }
1856
Geoff Lang8170eab2017-09-21 13:59:04 -04001857 // Only reset the cached status if this is not the default framebuffer. The default framebuffer
1858 // will still use this channel to mark itself dirty.
Jamie Madill2274b652018-05-31 10:56:08 -04001859 if (mState.mId != 0)
Geoff Lang8170eab2017-09-21 13:59:04 -04001860 {
1861 // TOOD(jmadill): Make this only update individual attachments to do less work.
1862 mCachedStatus.reset();
1863 }
Jamie Madill05b35b22017-10-03 09:01:44 -04001864
Jamie Madilld4442552018-02-27 22:03:47 -05001865 FramebufferAttachment *attachment = getAttachmentFromSubjectIndex(index);
1866
Jamie Madill05b35b22017-10-03 09:01:44 -04001867 // Mark the appropriate init flag.
Jamie Madilld4442552018-02-27 22:03:47 -05001868 mState.mResourceNeedsInit.set(index, attachment->initState() == InitState::MayNeedInit);
1869}
1870
1871FramebufferAttachment *Framebuffer::getAttachmentFromSubjectIndex(angle::SubjectIndex index)
1872{
1873 switch (index)
1874 {
1875 case DIRTY_BIT_DEPTH_ATTACHMENT:
1876 return &mState.mDepthAttachment;
1877 case DIRTY_BIT_STENCIL_ATTACHMENT:
1878 return &mState.mStencilAttachment;
1879 default:
1880 size_t colorIndex = (index - DIRTY_BIT_COLOR_ATTACHMENT_0);
1881 ASSERT(colorIndex < mState.mColorAttachments.size());
1882 return &mState.mColorAttachments[colorIndex];
1883 }
Jamie Madill51f40ec2016-06-15 14:06:00 -04001884}
1885
Jamie Madill427064d2018-04-13 16:20:34 -04001886bool Framebuffer::isComplete(const Context *context)
Jamie Madill51f40ec2016-06-15 14:06:00 -04001887{
Jamie Madill427064d2018-04-13 16:20:34 -04001888 return (checkStatus(context) == GL_FRAMEBUFFER_COMPLETE);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001889}
1890
Jamie Madilla4595b82017-01-11 17:36:34 -05001891bool Framebuffer::formsRenderingFeedbackLoopWith(const State &state) const
1892{
1893 const Program *program = state.getProgram();
1894
1895 // TODO(jmadill): Default framebuffer feedback loops.
Jamie Madill2274b652018-05-31 10:56:08 -04001896 if (mState.mId == 0)
Jamie Madilla4595b82017-01-11 17:36:34 -05001897 {
1898 return false;
1899 }
1900
1901 // The bitset will skip inactive draw buffers.
Jamie Madill6de51852017-04-12 09:53:01 -04001902 for (size_t drawIndex : mState.mEnabledDrawBuffers)
Jamie Madilla4595b82017-01-11 17:36:34 -05001903 {
Jamie Madill5aca1932017-07-21 12:22:01 -04001904 const FramebufferAttachment &attachment = mState.mColorAttachments[drawIndex];
1905 ASSERT(attachment.isAttached());
1906 if (attachment.type() == GL_TEXTURE)
Jamie Madilla4595b82017-01-11 17:36:34 -05001907 {
1908 // Validate the feedback loop.
Jamie Madill5aca1932017-07-21 12:22:01 -04001909 if (program->samplesFromTexture(state, attachment.id()))
Jamie Madilla4595b82017-01-11 17:36:34 -05001910 {
1911 return true;
1912 }
1913 }
1914 }
1915
Jamie Madill1d37bc52017-02-02 19:59:58 -05001916 // Validate depth-stencil feedback loop.
1917 const auto &dsState = state.getDepthStencilState();
1918
1919 // We can skip the feedback loop checks if depth/stencil is masked out or disabled.
1920 const FramebufferAttachment *depth = getDepthbuffer();
1921 if (depth && depth->type() == GL_TEXTURE && dsState.depthTest && dsState.depthMask)
1922 {
1923 if (program->samplesFromTexture(state, depth->id()))
1924 {
1925 return true;
1926 }
1927 }
1928
Jamie Madill1d37bc52017-02-02 19:59:58 -05001929 const FramebufferAttachment *stencil = getStencilbuffer();
Ken Russellb9f92502018-01-27 19:00:26 -08001930 if (dsState.stencilTest && stencil)
Jamie Madill1d37bc52017-02-02 19:59:58 -05001931 {
Ken Russellb9f92502018-01-27 19:00:26 -08001932 GLuint stencilSize = stencil->getStencilSize();
1933 ASSERT(stencilSize <= 8);
1934 GLuint maxStencilValue = (1 << stencilSize) - 1;
1935 // We assume the front and back masks are the same for WebGL.
1936 ASSERT((dsState.stencilBackWritemask & maxStencilValue) ==
1937 (dsState.stencilWritemask & maxStencilValue));
1938 if (stencil->type() == GL_TEXTURE && dsState.stencilWritemask != 0)
Jamie Madill1d37bc52017-02-02 19:59:58 -05001939 {
Ken Russellb9f92502018-01-27 19:00:26 -08001940 // Skip the feedback loop check if depth/stencil point to the same resource.
1941 if (!depth || *stencil != *depth)
Jamie Madill1d37bc52017-02-02 19:59:58 -05001942 {
Ken Russellb9f92502018-01-27 19:00:26 -08001943 if (program->samplesFromTexture(state, stencil->id()))
1944 {
1945 return true;
1946 }
Jamie Madill1d37bc52017-02-02 19:59:58 -05001947 }
1948 }
1949 }
1950
Jamie Madilla4595b82017-01-11 17:36:34 -05001951 return false;
1952}
1953
Jamie Madillfd3dd432017-02-02 19:59:59 -05001954bool Framebuffer::formsCopyingFeedbackLoopWith(GLuint copyTextureID,
1955 GLint copyTextureLevel,
1956 GLint copyTextureLayer) const
Jamie Madillf695a3a2017-01-11 17:36:35 -05001957{
Jamie Madill2274b652018-05-31 10:56:08 -04001958 if (mState.mId == 0)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001959 {
1960 // It seems impossible to form a texture copying feedback loop with the default FBO.
1961 return false;
1962 }
1963
1964 const FramebufferAttachment *readAttachment = getReadColorbuffer();
1965 ASSERT(readAttachment);
1966
1967 if (readAttachment->isTextureWithId(copyTextureID))
1968 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001969 const auto &imageIndex = readAttachment->getTextureImageIndex();
Jamie Madillcc129372018-04-12 09:13:18 -04001970 if (imageIndex.getLevelIndex() == copyTextureLevel)
Jamie Madillf695a3a2017-01-11 17:36:35 -05001971 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05001972 // Check 3D/Array texture layers.
Jamie Madillcc129372018-04-12 09:13:18 -04001973 return !imageIndex.hasLayer() || copyTextureLayer == ImageIndex::kEntireLevel ||
1974 imageIndex.getLayerIndex() == copyTextureLayer;
Jamie Madillf695a3a2017-01-11 17:36:35 -05001975 }
1976 }
1977 return false;
1978}
1979
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001980GLint Framebuffer::getDefaultWidth() const
1981{
1982 return mState.getDefaultWidth();
1983}
1984
1985GLint Framebuffer::getDefaultHeight() const
1986{
1987 return mState.getDefaultHeight();
1988}
1989
1990GLint Framebuffer::getDefaultSamples() const
1991{
1992 return mState.getDefaultSamples();
1993}
1994
Geoff Lang92019432017-11-20 13:09:34 -05001995bool Framebuffer::getDefaultFixedSampleLocations() const
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08001996{
1997 return mState.getDefaultFixedSampleLocations();
1998}
1999
Jiawei Shaob1e91382018-05-17 14:33:55 +08002000GLint Framebuffer::getDefaultLayers() const
2001{
2002 return mState.getDefaultLayers();
2003}
2004
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002005void Framebuffer::setDefaultWidth(GLint defaultWidth)
2006{
2007 mState.mDefaultWidth = defaultWidth;
2008 mDirtyBits.set(DIRTY_BIT_DEFAULT_WIDTH);
Jamie Madille98b1b52018-03-08 09:47:23 -05002009 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002010}
2011
2012void Framebuffer::setDefaultHeight(GLint defaultHeight)
2013{
2014 mState.mDefaultHeight = defaultHeight;
2015 mDirtyBits.set(DIRTY_BIT_DEFAULT_HEIGHT);
Jamie Madille98b1b52018-03-08 09:47:23 -05002016 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002017}
2018
2019void Framebuffer::setDefaultSamples(GLint defaultSamples)
2020{
2021 mState.mDefaultSamples = defaultSamples;
2022 mDirtyBits.set(DIRTY_BIT_DEFAULT_SAMPLES);
Jamie Madille98b1b52018-03-08 09:47:23 -05002023 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002024}
2025
Geoff Lang92019432017-11-20 13:09:34 -05002026void Framebuffer::setDefaultFixedSampleLocations(bool defaultFixedSampleLocations)
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002027{
2028 mState.mDefaultFixedSampleLocations = defaultFixedSampleLocations;
2029 mDirtyBits.set(DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS);
Jamie Madille98b1b52018-03-08 09:47:23 -05002030 invalidateCompletenessCache();
JiangYizhouf7bbc8a2016-11-16 09:57:22 +08002031}
2032
Jiawei Shaob1e91382018-05-17 14:33:55 +08002033void Framebuffer::setDefaultLayers(GLint defaultLayers)
2034{
2035 mState.mDefaultLayers = defaultLayers;
2036 mDirtyBits.set(DIRTY_BIT_DEFAULT_LAYERS);
2037}
2038
Martin Radev14a26ae2017-07-24 15:56:29 +03002039GLsizei Framebuffer::getNumViews() const
2040{
Martin Radev5c00d0d2017-08-07 10:06:59 +03002041 return mState.getNumViews();
Martin Radev14a26ae2017-07-24 15:56:29 +03002042}
2043
Martin Radev4e619f52017-08-09 11:50:06 +03002044GLint Framebuffer::getBaseViewIndex() const
2045{
2046 return mState.getBaseViewIndex();
2047}
2048
Martin Radev878c8b12017-07-28 09:51:04 +03002049const std::vector<Offset> *Framebuffer::getViewportOffsets() const
2050{
Martin Radev5c00d0d2017-08-07 10:06:59 +03002051 return mState.getViewportOffsets();
Martin Radev878c8b12017-07-28 09:51:04 +03002052}
2053
2054GLenum Framebuffer::getMultiviewLayout() const
2055{
Martin Radev5c00d0d2017-08-07 10:06:59 +03002056 return mState.getMultiviewLayout();
Martin Radev878c8b12017-07-28 09:51:04 +03002057}
2058
Geoff Langd4fff502017-09-22 11:28:28 -04002059Error Framebuffer::ensureClearAttachmentsInitialized(const Context *context, GLbitfield mask)
2060{
2061 const auto &glState = context->getGLState();
2062 if (!context->isRobustResourceInitEnabled() || glState.isRasterizerDiscardEnabled())
2063 {
2064 return NoError();
2065 }
2066
Geoff Langa36483f2018-03-09 16:11:21 -05002067 const BlendState &blend = glState.getBlendState();
2068 const DepthStencilState &depthStencil = glState.getDepthStencilState();
Geoff Langd4fff502017-09-22 11:28:28 -04002069
2070 bool color = (mask & GL_COLOR_BUFFER_BIT) != 0 && !IsColorMaskedOut(blend);
2071 bool depth = (mask & GL_DEPTH_BUFFER_BIT) != 0 && !IsDepthMaskedOut(depthStencil);
2072 bool stencil = (mask & GL_STENCIL_BUFFER_BIT) != 0 && !IsStencilMaskedOut(depthStencil);
2073
2074 if (!color && !depth && !stencil)
2075 {
2076 return NoError();
2077 }
2078
2079 if (partialClearNeedsInit(context, color, depth, stencil))
2080 {
2081 ANGLE_TRY(ensureDrawAttachmentsInitialized(context));
2082 }
2083
2084 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2085 // still be marked initialized. This simplifies design, allowing this method to be called before
2086 // the clear.
2087 markDrawAttachmentsInitialized(color, depth, stencil);
2088
2089 return NoError();
2090}
2091
2092Error Framebuffer::ensureClearBufferAttachmentsInitialized(const Context *context,
2093 GLenum buffer,
2094 GLint drawbuffer)
2095{
2096 if (!context->isRobustResourceInitEnabled() ||
2097 context->getGLState().isRasterizerDiscardEnabled() ||
2098 IsClearBufferMaskedOut(context, buffer))
2099 {
2100 return NoError();
2101 }
2102
2103 if (partialBufferClearNeedsInit(context, buffer))
2104 {
2105 ANGLE_TRY(ensureBufferInitialized(context, buffer, drawbuffer));
2106 }
2107
2108 // If the impl encounters an error during a a full (non-partial) clear, the attachments will
2109 // still be marked initialized. This simplifies design, allowing this method to be called before
2110 // the clear.
2111 markBufferInitialized(buffer, drawbuffer);
2112
2113 return NoError();
2114}
2115
Jamie Madill05b35b22017-10-03 09:01:44 -04002116Error Framebuffer::ensureDrawAttachmentsInitialized(const Context *context)
2117{
2118 if (!context->isRobustResourceInitEnabled())
2119 {
2120 return NoError();
2121 }
2122
2123 // Note: we don't actually filter by the draw attachment enum. Just init everything.
2124 for (size_t bit : mState.mResourceNeedsInit)
2125 {
2126 switch (bit)
2127 {
2128 case DIRTY_BIT_DEPTH_ATTACHMENT:
2129 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2130 break;
2131 case DIRTY_BIT_STENCIL_ATTACHMENT:
2132 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2133 break;
2134 default:
2135 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bit]));
2136 break;
2137 }
2138 }
2139
2140 mState.mResourceNeedsInit.reset();
2141 return NoError();
2142}
2143
2144Error Framebuffer::ensureReadAttachmentInitialized(const Context *context, GLbitfield blitMask)
2145{
2146 if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none())
2147 {
2148 return NoError();
2149 }
2150
2151 if ((blitMask & GL_COLOR_BUFFER_BIT) != 0 && mState.mReadBufferState != GL_NONE)
2152 {
2153 size_t readIndex = mState.getReadIndex();
2154 if (mState.mResourceNeedsInit[readIndex])
2155 {
2156 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[readIndex]));
2157 mState.mResourceNeedsInit.reset(readIndex);
2158 }
2159 }
2160
2161 if ((blitMask & GL_DEPTH_BUFFER_BIT) != 0 && hasDepth())
2162 {
2163 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2164 {
2165 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2166 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2167 }
2168 }
2169
2170 if ((blitMask & GL_STENCIL_BUFFER_BIT) != 0 && hasStencil())
2171 {
2172 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2173 {
2174 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2175 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2176 }
2177 }
2178
2179 return NoError();
2180}
2181
2182void Framebuffer::markDrawAttachmentsInitialized(bool color, bool depth, bool stencil)
2183{
2184 // Mark attachments as initialized.
2185 if (color)
2186 {
2187 for (auto colorIndex : mState.mEnabledDrawBuffers)
2188 {
2189 auto &colorAttachment = mState.mColorAttachments[colorIndex];
2190 ASSERT(colorAttachment.isAttached());
2191 colorAttachment.setInitState(InitState::Initialized);
2192 mState.mResourceNeedsInit.reset(colorIndex);
2193 }
2194 }
2195
2196 if (depth && mState.mDepthAttachment.isAttached())
2197 {
2198 mState.mDepthAttachment.setInitState(InitState::Initialized);
2199 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2200 }
2201
2202 if (stencil && mState.mStencilAttachment.isAttached())
2203 {
2204 mState.mStencilAttachment.setInitState(InitState::Initialized);
2205 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2206 }
2207}
2208
2209void Framebuffer::markBufferInitialized(GLenum bufferType, GLint bufferIndex)
2210{
2211 switch (bufferType)
2212 {
2213 case GL_COLOR:
2214 {
2215 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2216 if (mState.mColorAttachments[bufferIndex].isAttached())
2217 {
2218 mState.mColorAttachments[bufferIndex].setInitState(InitState::Initialized);
2219 mState.mResourceNeedsInit.reset(bufferIndex);
2220 }
2221 break;
2222 }
2223 case GL_DEPTH:
2224 {
2225 if (mState.mDepthAttachment.isAttached())
2226 {
2227 mState.mDepthAttachment.setInitState(InitState::Initialized);
2228 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2229 }
2230 break;
2231 }
2232 case GL_STENCIL:
2233 {
2234 if (mState.mStencilAttachment.isAttached())
2235 {
2236 mState.mStencilAttachment.setInitState(InitState::Initialized);
2237 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2238 }
2239 break;
2240 }
2241 case GL_DEPTH_STENCIL:
2242 {
2243 if (mState.mDepthAttachment.isAttached())
2244 {
2245 mState.mDepthAttachment.setInitState(InitState::Initialized);
2246 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2247 }
2248 if (mState.mStencilAttachment.isAttached())
2249 {
2250 mState.mStencilAttachment.setInitState(InitState::Initialized);
2251 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2252 }
2253 break;
2254 }
2255 default:
2256 UNREACHABLE();
2257 break;
2258 }
2259}
2260
2261Box Framebuffer::getDimensions() const
2262{
2263 return mState.getDimensions();
2264}
2265
2266Error Framebuffer::ensureBufferInitialized(const Context *context,
2267 GLenum bufferType,
2268 GLint bufferIndex)
2269{
2270 ASSERT(context->isRobustResourceInitEnabled());
2271
2272 if (mState.mResourceNeedsInit.none())
2273 {
2274 return NoError();
2275 }
2276
2277 switch (bufferType)
2278 {
2279 case GL_COLOR:
2280 {
2281 ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size()));
2282 if (mState.mResourceNeedsInit[bufferIndex])
2283 {
2284 ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bufferIndex]));
2285 mState.mResourceNeedsInit.reset(bufferIndex);
2286 }
2287 break;
2288 }
2289 case GL_DEPTH:
2290 {
2291 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2292 {
2293 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2294 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2295 }
2296 break;
2297 }
2298 case GL_STENCIL:
2299 {
2300 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2301 {
2302 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2303 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2304 }
2305 break;
2306 }
2307 case GL_DEPTH_STENCIL:
2308 {
2309 if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT])
2310 {
2311 ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment));
2312 mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT);
2313 }
2314 if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT])
2315 {
2316 ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment));
2317 mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT);
2318 }
2319 break;
2320 }
2321 default:
2322 UNREACHABLE();
2323 break;
2324 }
2325
2326 return NoError();
2327}
2328
2329bool Framebuffer::partialBufferClearNeedsInit(const Context *context, GLenum bufferType)
2330{
2331 if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none())
2332 {
2333 return false;
2334 }
2335
2336 switch (bufferType)
2337 {
2338 case GL_COLOR:
2339 return partialClearNeedsInit(context, true, false, false);
2340 case GL_DEPTH:
2341 return partialClearNeedsInit(context, false, true, false);
2342 case GL_STENCIL:
2343 return partialClearNeedsInit(context, false, false, true);
2344 case GL_DEPTH_STENCIL:
2345 return partialClearNeedsInit(context, false, true, true);
2346 default:
2347 UNREACHABLE();
2348 return false;
2349 }
2350}
2351
Jamie Madill42975642017-10-12 12:31:51 -04002352bool Framebuffer::hasTextureAttachment(const Texture *texture) const
2353{
2354 if (!mAttachedTextures.valid())
2355 {
2356 std::set<const FramebufferAttachmentObject *> attachedTextures;
2357
2358 for (const auto &colorAttachment : mState.mColorAttachments)
2359 {
2360 if (colorAttachment.isAttached() && colorAttachment.type() == GL_TEXTURE)
2361 {
2362 attachedTextures.insert(colorAttachment.getResource());
2363 }
2364 }
2365
2366 if (mState.mDepthAttachment.isAttached() && mState.mDepthAttachment.type() == GL_TEXTURE)
2367 {
2368 attachedTextures.insert(mState.mDepthAttachment.getResource());
2369 }
2370
2371 if (mState.mStencilAttachment.isAttached() &&
2372 mState.mStencilAttachment.type() == GL_TEXTURE)
2373 {
2374 attachedTextures.insert(mState.mStencilAttachment.getResource());
2375 }
2376
2377 mAttachedTextures = std::move(attachedTextures);
2378 }
2379
2380 return (mAttachedTextures.value().count(texture) > 0);
2381}
2382
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002383} // namespace gl