daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // 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 Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 10 | #include "libANGLE/Framebuffer.h" |
Jamie Madill | c46f45d | 2015-03-31 13:20:55 -0400 | [diff] [blame] | 11 | |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 12 | #include "common/Optional.h" |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 13 | #include "common/bitset_utils.h" |
Jamie Madill | c46f45d | 2015-03-31 13:20:55 -0400 | [diff] [blame] | 14 | #include "common/utilities.h" |
| 15 | #include "libANGLE/Config.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 16 | #include "libANGLE/Context.h" |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 17 | #include "libANGLE/Display.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 18 | #include "libANGLE/FramebufferAttachment.h" |
Jamie Madill | c46f45d | 2015-03-31 13:20:55 -0400 | [diff] [blame] | 19 | #include "libANGLE/Renderbuffer.h" |
| 20 | #include "libANGLE/Surface.h" |
| 21 | #include "libANGLE/Texture.h" |
| 22 | #include "libANGLE/formatutils.h" |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 23 | #include "libANGLE/renderer/ContextImpl.h" |
Geoff Lang | b5d8f23 | 2014-12-04 15:43:01 -0500 | [diff] [blame] | 24 | #include "libANGLE/renderer/FramebufferImpl.h" |
Jamie Madill | 7aea7e0 | 2016-05-10 10:39:45 -0400 | [diff] [blame] | 25 | #include "libANGLE/renderer/GLImplFactory.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 26 | #include "libANGLE/renderer/RenderbufferImpl.h" |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 27 | #include "libANGLE/renderer/SurfaceImpl.h" |
Geoff Lang | 0b7eef7 | 2014-06-12 14:10:47 -0400 | [diff] [blame] | 28 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 29 | using namespace angle; |
| 30 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 31 | namespace gl |
| 32 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 33 | |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 34 | namespace |
| 35 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 36 | |
Jamie Madill | 1e5499d | 2017-04-05 11:22:16 -0400 | [diff] [blame] | 37 | void BindResourceChannel(OnAttachmentDirtyBinding *binding, FramebufferAttachmentObject *resource) |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 38 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 39 | binding->bind(resource ? resource->getDirtyChannel() : nullptr); |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 40 | } |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 41 | |
Martin Radev | 9bc9a32 | 2017-07-21 14:28:17 +0300 | [diff] [blame] | 42 | bool CheckMultiviewStateMatchesForCompleteness(const FramebufferAttachment *firstAttachment, |
| 43 | const FramebufferAttachment *secondAttachment) |
| 44 | { |
| 45 | ASSERT(firstAttachment && secondAttachment); |
| 46 | ASSERT(firstAttachment->isAttached() && secondAttachment->isAttached()); |
| 47 | |
| 48 | if (firstAttachment->getNumViews() != secondAttachment->getNumViews()) |
| 49 | { |
| 50 | return false; |
| 51 | } |
| 52 | if (firstAttachment->getBaseViewIndex() != secondAttachment->getBaseViewIndex()) |
| 53 | { |
| 54 | return false; |
| 55 | } |
| 56 | if (firstAttachment->getMultiviewLayout() != secondAttachment->getMultiviewLayout()) |
| 57 | { |
| 58 | return false; |
| 59 | } |
| 60 | if (firstAttachment->getMultiviewViewportOffsets() != |
| 61 | secondAttachment->getMultiviewViewportOffsets()) |
| 62 | { |
| 63 | return false; |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
Geoff Lang | 9f10b77 | 2017-05-16 15:51:03 -0400 | [diff] [blame] | 68 | bool CheckAttachmentCompleteness(const Context *context, const FramebufferAttachment &attachment) |
| 69 | { |
| 70 | ASSERT(attachment.isAttached()); |
| 71 | |
| 72 | const Extents &size = attachment.getSize(); |
| 73 | if (size.width == 0 || size.height == 0) |
| 74 | { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | const InternalFormat &format = *attachment.getFormat().info; |
| 79 | if (!format.renderSupport(context->getClientVersion(), context->getExtensions())) |
| 80 | { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if (attachment.type() == GL_TEXTURE) |
| 85 | { |
| 86 | if (attachment.layer() >= size.depth) |
| 87 | { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | // ES3 specifies that cube map texture attachments must be cube complete. |
| 92 | // This language is missing from the ES2 spec, but we enforce it here because some |
| 93 | // desktop OpenGL drivers also enforce this validation. |
| 94 | // TODO(jmadill): Check if OpenGL ES2 drivers enforce cube completeness. |
| 95 | const Texture *texture = attachment.getTexture(); |
| 96 | ASSERT(texture); |
| 97 | if (texture->getTarget() == GL_TEXTURE_CUBE_MAP && |
| 98 | !texture->getTextureState().isCubeComplete()) |
| 99 | { |
| 100 | return false; |
| 101 | } |
Geoff Lang | 857c09d | 2017-05-16 15:55:04 -0400 | [diff] [blame] | 102 | |
| 103 | if (!texture->getImmutableFormat()) |
| 104 | { |
| 105 | GLuint attachmentMipLevel = static_cast<GLuint>(attachment.mipLevel()); |
| 106 | |
| 107 | // From the ES 3.0 spec, pg 213: |
| 108 | // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of |
| 109 | // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture, |
| 110 | // then the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL must be in the |
| 111 | // range[levelbase, q], where levelbase is the value of TEXTURE_BASE_LEVEL and q is |
| 112 | // the effective maximum texture level defined in the Mipmapping discussion of |
| 113 | // section 3.8.10.4. |
| 114 | if (attachmentMipLevel < texture->getBaseLevel() || |
| 115 | attachmentMipLevel > texture->getMipmapMaxLevel()) |
| 116 | { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | // Form the ES 3.0 spec, pg 213/214: |
| 121 | // If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is TEXTURE and the value of |
| 122 | // FRAMEBUFFER_ATTACHMENT_OBJECT_NAME does not name an immutable-format texture and |
| 123 | // the value of FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL is not levelbase, then the |
| 124 | // texture must be mipmap complete, and if FRAMEBUFFER_ATTACHMENT_OBJECT_NAME names |
| 125 | // a cubemap texture, the texture must also be cube complete. |
| 126 | if (attachmentMipLevel != texture->getBaseLevel() && !texture->isMipmapComplete()) |
| 127 | { |
| 128 | return false; |
| 129 | } |
| 130 | } |
Geoff Lang | 9f10b77 | 2017-05-16 15:51:03 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | return true; |
| 134 | }; |
| 135 | |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 136 | bool CheckAttachmentSampleCompleteness(const Context *context, |
| 137 | const FramebufferAttachment &attachment, |
| 138 | bool colorAttachment, |
| 139 | Optional<int> *samples, |
| 140 | Optional<GLboolean> *fixedSampleLocations) |
| 141 | { |
| 142 | ASSERT(attachment.isAttached()); |
| 143 | |
| 144 | if (attachment.type() == GL_TEXTURE) |
| 145 | { |
| 146 | const Texture *texture = attachment.getTexture(); |
| 147 | ASSERT(texture); |
| 148 | |
| 149 | const ImageIndex &attachmentImageIndex = attachment.getTextureImageIndex(); |
| 150 | |
| 151 | // ES3.1 (section 9.4) requires that the value of TEXTURE_FIXED_SAMPLE_LOCATIONS should be |
| 152 | // the same for all attached textures. |
| 153 | GLboolean fixedSampleloc = texture->getFixedSampleLocations(attachmentImageIndex.type, |
| 154 | attachmentImageIndex.mipIndex); |
| 155 | if (fixedSampleLocations->valid() && fixedSampleloc != fixedSampleLocations->value()) |
| 156 | { |
| 157 | return false; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | *fixedSampleLocations = fixedSampleloc; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (samples->valid()) |
| 166 | { |
| 167 | if (attachment.getSamples() != samples->value()) |
| 168 | { |
| 169 | if (colorAttachment) |
| 170 | { |
| 171 | // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that |
| 172 | // all color attachments have the same number of samples for the FBO to be complete. |
| 173 | return false; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | // CHROMIUM_framebuffer_mixed_samples allows a framebuffer to be considered complete |
| 178 | // when its depth or stencil samples are a multiple of the number of color samples. |
| 179 | if (!context->getExtensions().framebufferMixedSamples) |
| 180 | { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | if ((attachment.getSamples() % std::max(samples->value(), 1)) != 0) |
| 185 | { |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | *samples = attachment.getSamples(); |
| 194 | } |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 199 | // Needed to index into the attachment arrays/bitsets. |
| 200 | static_assert(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS == |
| 201 | gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX, |
| 202 | "Framebuffer Dirty bit mismatch"); |
| 203 | static_assert(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS == |
| 204 | gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT, |
| 205 | "Framebuffer Dirty bit mismatch"); |
| 206 | static_assert(IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS + 1 == |
| 207 | gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT, |
| 208 | "Framebuffer Dirty bit mismatch"); |
| 209 | |
| 210 | Error InitAttachment(const Context *context, FramebufferAttachment *attachment) |
| 211 | { |
| 212 | ASSERT(attachment->isAttached()); |
| 213 | if (attachment->initState() == InitState::MayNeedInit) |
| 214 | { |
| 215 | ANGLE_TRY(attachment->initializeContents(context)); |
| 216 | } |
| 217 | return NoError(); |
| 218 | } |
| 219 | |
| 220 | bool IsColorMaskedOut(const BlendState &blend) |
| 221 | { |
| 222 | return (!blend.colorMaskRed && !blend.colorMaskGreen && !blend.colorMaskBlue && |
| 223 | !blend.colorMaskAlpha); |
| 224 | } |
| 225 | |
| 226 | bool IsDepthMaskedOut(const DepthStencilState &depthStencil) |
| 227 | { |
| 228 | return !depthStencil.depthMask; |
| 229 | } |
| 230 | |
| 231 | bool IsStencilMaskedOut(const DepthStencilState &depthStencil) |
| 232 | { |
| 233 | return ((depthStencil.stencilMask & depthStencil.stencilWritemask) == 0); |
| 234 | } |
| 235 | |
| 236 | bool IsClearBufferMaskedOut(const Context *context, GLenum buffer) |
| 237 | { |
| 238 | switch (buffer) |
| 239 | { |
| 240 | case GL_COLOR: |
| 241 | return IsColorMaskedOut(context->getGLState().getBlendState()); |
| 242 | case GL_DEPTH: |
| 243 | return IsDepthMaskedOut(context->getGLState().getDepthStencilState()); |
| 244 | case GL_STENCIL: |
| 245 | return IsStencilMaskedOut(context->getGLState().getDepthStencilState()); |
| 246 | case GL_DEPTH_STENCIL: |
| 247 | return IsDepthMaskedOut(context->getGLState().getDepthStencilState()) && |
| 248 | IsStencilMaskedOut(context->getGLState().getDepthStencilState()); |
| 249 | default: |
| 250 | UNREACHABLE(); |
| 251 | return true; |
| 252 | } |
| 253 | } |
| 254 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 255 | } // anonymous namespace |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 256 | |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 257 | // This constructor is only used for default framebuffers. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 258 | FramebufferState::FramebufferState() |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 259 | : mLabel(), |
| 260 | mColorAttachments(1), |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 261 | mDrawBufferStates(1, GL_BACK), |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 262 | mReadBufferState(GL_BACK), |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 263 | mDefaultWidth(0), |
| 264 | mDefaultHeight(0), |
| 265 | mDefaultSamples(0), |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 266 | mDefaultFixedSampleLocations(GL_FALSE), |
| 267 | mWebGLDepthStencilConsistent(true) |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 268 | { |
Geoff Lang | d90d388 | 2017-03-21 10:49:54 -0400 | [diff] [blame] | 269 | ASSERT(mDrawBufferStates.size() > 0); |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 270 | mEnabledDrawBuffers.set(0); |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 273 | FramebufferState::FramebufferState(const Caps &caps) |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 274 | : mLabel(), |
| 275 | mColorAttachments(caps.maxColorAttachments), |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 276 | mDrawBufferStates(caps.maxDrawBuffers, GL_NONE), |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 277 | mReadBufferState(GL_COLOR_ATTACHMENT0_EXT), |
| 278 | mDefaultWidth(0), |
| 279 | mDefaultHeight(0), |
| 280 | mDefaultSamples(0), |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 281 | mDefaultFixedSampleLocations(GL_FALSE), |
| 282 | mWebGLDepthStencilConsistent(true) |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 283 | { |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 284 | ASSERT(mDrawBufferStates.size() > 0); |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 285 | mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT; |
| 286 | } |
| 287 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 288 | FramebufferState::~FramebufferState() |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 289 | { |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 290 | } |
| 291 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 292 | const std::string &FramebufferState::getLabel() |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 293 | { |
| 294 | return mLabel; |
| 295 | } |
| 296 | |
Geoff Lang | 4b7f12b | 2016-06-21 16:47:07 -0400 | [diff] [blame] | 297 | const FramebufferAttachment *FramebufferState::getAttachment(GLenum attachment) const |
| 298 | { |
| 299 | if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15) |
| 300 | { |
| 301 | return getColorAttachment(attachment - GL_COLOR_ATTACHMENT0); |
| 302 | } |
| 303 | |
| 304 | switch (attachment) |
| 305 | { |
| 306 | case GL_COLOR: |
| 307 | case GL_BACK: |
| 308 | return getColorAttachment(0); |
| 309 | case GL_DEPTH: |
| 310 | case GL_DEPTH_ATTACHMENT: |
| 311 | return getDepthAttachment(); |
| 312 | case GL_STENCIL: |
| 313 | case GL_STENCIL_ATTACHMENT: |
| 314 | return getStencilAttachment(); |
| 315 | case GL_DEPTH_STENCIL: |
| 316 | case GL_DEPTH_STENCIL_ATTACHMENT: |
| 317 | return getDepthStencilAttachment(); |
| 318 | default: |
| 319 | UNREACHABLE(); |
| 320 | return nullptr; |
| 321 | } |
| 322 | } |
| 323 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 324 | size_t FramebufferState::getReadIndex() const |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 325 | { |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 326 | ASSERT(mReadBufferState == GL_BACK || |
| 327 | (mReadBufferState >= GL_COLOR_ATTACHMENT0 && mReadBufferState <= GL_COLOR_ATTACHMENT15)); |
| 328 | size_t readIndex = (mReadBufferState == GL_BACK |
| 329 | ? 0 |
| 330 | : static_cast<size_t>(mReadBufferState - GL_COLOR_ATTACHMENT0)); |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 331 | ASSERT(readIndex < mColorAttachments.size()); |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 332 | return readIndex; |
| 333 | } |
| 334 | |
| 335 | const FramebufferAttachment *FramebufferState::getReadAttachment() const |
| 336 | { |
| 337 | if (mReadBufferState == GL_NONE) |
| 338 | { |
| 339 | return nullptr; |
| 340 | } |
| 341 | size_t readIndex = getReadIndex(); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 342 | return mColorAttachments[readIndex].isAttached() ? &mColorAttachments[readIndex] : nullptr; |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 343 | } |
| 344 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 345 | const FramebufferAttachment *FramebufferState::getFirstNonNullAttachment() const |
| 346 | { |
| 347 | auto *colorAttachment = getFirstColorAttachment(); |
| 348 | if (colorAttachment) |
| 349 | { |
| 350 | return colorAttachment; |
| 351 | } |
| 352 | return getDepthOrStencilAttachment(); |
| 353 | } |
| 354 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 355 | const FramebufferAttachment *FramebufferState::getFirstColorAttachment() const |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 356 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 357 | for (const FramebufferAttachment &colorAttachment : mColorAttachments) |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 358 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 359 | if (colorAttachment.isAttached()) |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 360 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 361 | return &colorAttachment; |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | return nullptr; |
| 366 | } |
| 367 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 368 | const FramebufferAttachment *FramebufferState::getDepthOrStencilAttachment() const |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 369 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 370 | if (mDepthAttachment.isAttached()) |
| 371 | { |
| 372 | return &mDepthAttachment; |
| 373 | } |
| 374 | if (mStencilAttachment.isAttached()) |
| 375 | { |
| 376 | return &mStencilAttachment; |
| 377 | } |
| 378 | return nullptr; |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 379 | } |
| 380 | |
Corentin Wallez | b1d0a255 | 2016-12-19 16:15:54 -0500 | [diff] [blame] | 381 | const FramebufferAttachment *FramebufferState::getStencilOrDepthStencilAttachment() const |
| 382 | { |
| 383 | if (mStencilAttachment.isAttached()) |
| 384 | { |
| 385 | return &mStencilAttachment; |
| 386 | } |
| 387 | return getDepthStencilAttachment(); |
| 388 | } |
| 389 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 390 | const FramebufferAttachment *FramebufferState::getColorAttachment(size_t colorAttachment) const |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 391 | { |
| 392 | ASSERT(colorAttachment < mColorAttachments.size()); |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 393 | return mColorAttachments[colorAttachment].isAttached() ? &mColorAttachments[colorAttachment] |
| 394 | : nullptr; |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 395 | } |
| 396 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 397 | const FramebufferAttachment *FramebufferState::getDepthAttachment() const |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 398 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 399 | return mDepthAttachment.isAttached() ? &mDepthAttachment : nullptr; |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 400 | } |
| 401 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 402 | const FramebufferAttachment *FramebufferState::getStencilAttachment() const |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 403 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 404 | return mStencilAttachment.isAttached() ? &mStencilAttachment : nullptr; |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 405 | } |
| 406 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 407 | const FramebufferAttachment *FramebufferState::getDepthStencilAttachment() const |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 408 | { |
| 409 | // A valid depth-stencil attachment has the same resource bound to both the |
| 410 | // depth and stencil attachment points. |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 411 | if (mDepthAttachment.isAttached() && mStencilAttachment.isAttached() && |
Jamie Madill | 44f2648 | 2016-11-18 12:49:15 -0500 | [diff] [blame] | 412 | mDepthAttachment == mStencilAttachment) |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 413 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 414 | return &mDepthAttachment; |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | return nullptr; |
| 418 | } |
| 419 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 420 | bool FramebufferState::attachmentsHaveSameDimensions() const |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 421 | { |
| 422 | Optional<Extents> attachmentSize; |
| 423 | |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 424 | auto hasMismatchedSize = [&attachmentSize](const FramebufferAttachment &attachment) { |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 425 | if (!attachment.isAttached()) |
| 426 | { |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | if (!attachmentSize.valid()) |
| 431 | { |
| 432 | attachmentSize = attachment.getSize(); |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | return (attachment.getSize() != attachmentSize.value()); |
| 437 | }; |
| 438 | |
| 439 | for (const auto &attachment : mColorAttachments) |
| 440 | { |
| 441 | if (hasMismatchedSize(attachment)) |
| 442 | { |
| 443 | return false; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (hasMismatchedSize(mDepthAttachment)) |
| 448 | { |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | return !hasMismatchedSize(mStencilAttachment); |
| 453 | } |
| 454 | |
Geoff Lang | 4b7f12b | 2016-06-21 16:47:07 -0400 | [diff] [blame] | 455 | const gl::FramebufferAttachment *FramebufferState::getDrawBuffer(size_t drawBufferIdx) const |
| 456 | { |
| 457 | ASSERT(drawBufferIdx < mDrawBufferStates.size()); |
| 458 | if (mDrawBufferStates[drawBufferIdx] != GL_NONE) |
| 459 | { |
| 460 | // ES3 spec: "If the GL is bound to a draw framebuffer object, the ith buffer listed in bufs |
| 461 | // must be COLOR_ATTACHMENTi or NONE" |
| 462 | ASSERT(mDrawBufferStates[drawBufferIdx] == GL_COLOR_ATTACHMENT0 + drawBufferIdx || |
| 463 | (drawBufferIdx == 0 && mDrawBufferStates[drawBufferIdx] == GL_BACK)); |
| 464 | return getAttachment(mDrawBufferStates[drawBufferIdx]); |
| 465 | } |
| 466 | else |
| 467 | { |
| 468 | return nullptr; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | size_t FramebufferState::getDrawBufferCount() const |
| 473 | { |
| 474 | return mDrawBufferStates.size(); |
| 475 | } |
| 476 | |
Geoff Lang | b21e20d | 2016-07-19 15:35:41 -0400 | [diff] [blame] | 477 | bool FramebufferState::colorAttachmentsAreUniqueImages() const |
| 478 | { |
| 479 | for (size_t firstAttachmentIdx = 0; firstAttachmentIdx < mColorAttachments.size(); |
| 480 | firstAttachmentIdx++) |
| 481 | { |
| 482 | const gl::FramebufferAttachment &firstAttachment = mColorAttachments[firstAttachmentIdx]; |
| 483 | if (!firstAttachment.isAttached()) |
| 484 | { |
| 485 | continue; |
| 486 | } |
| 487 | |
| 488 | for (size_t secondAttachmentIdx = firstAttachmentIdx + 1; |
| 489 | secondAttachmentIdx < mColorAttachments.size(); secondAttachmentIdx++) |
| 490 | { |
| 491 | const gl::FramebufferAttachment &secondAttachment = |
| 492 | mColorAttachments[secondAttachmentIdx]; |
| 493 | if (!secondAttachment.isAttached()) |
| 494 | { |
| 495 | continue; |
| 496 | } |
| 497 | |
| 498 | if (firstAttachment == secondAttachment) |
| 499 | { |
| 500 | return false; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | return true; |
| 506 | } |
| 507 | |
Jamie Madill | 9c33586 | 2017-07-18 11:51:38 -0400 | [diff] [blame] | 508 | bool FramebufferState::hasDepth() const |
| 509 | { |
| 510 | return (mDepthAttachment.isAttached() && mDepthAttachment.getDepthSize() > 0); |
| 511 | } |
| 512 | |
| 513 | bool FramebufferState::hasStencil() const |
| 514 | { |
| 515 | return (mStencilAttachment.isAttached() && mStencilAttachment.getStencilSize() > 0); |
| 516 | } |
| 517 | |
Martin Radev | 5c00d0d | 2017-08-07 10:06:59 +0300 | [diff] [blame] | 518 | GLsizei FramebufferState::getNumViews() const |
| 519 | { |
| 520 | const FramebufferAttachment *attachment = getFirstNonNullAttachment(); |
| 521 | if (attachment == nullptr) |
| 522 | { |
| 523 | return FramebufferAttachment::kDefaultNumViews; |
| 524 | } |
| 525 | return attachment->getNumViews(); |
| 526 | } |
| 527 | |
| 528 | const std::vector<Offset> *FramebufferState::getViewportOffsets() const |
| 529 | { |
| 530 | const FramebufferAttachment *attachment = getFirstNonNullAttachment(); |
| 531 | if (attachment == nullptr) |
| 532 | { |
| 533 | return nullptr; |
| 534 | } |
| 535 | return &attachment->getMultiviewViewportOffsets(); |
| 536 | } |
| 537 | |
| 538 | GLenum FramebufferState::getMultiviewLayout() const |
| 539 | { |
| 540 | const FramebufferAttachment *attachment = getFirstNonNullAttachment(); |
| 541 | if (attachment == nullptr) |
| 542 | { |
| 543 | return GL_NONE; |
| 544 | } |
| 545 | return attachment->getMultiviewLayout(); |
| 546 | } |
| 547 | |
Martin Radev | 4e619f5 | 2017-08-09 11:50:06 +0300 | [diff] [blame] | 548 | int FramebufferState::getBaseViewIndex() const |
| 549 | { |
| 550 | const FramebufferAttachment *attachment = getFirstNonNullAttachment(); |
| 551 | if (attachment == nullptr) |
| 552 | { |
| 553 | return GL_NONE; |
| 554 | } |
| 555 | return attachment->getBaseViewIndex(); |
| 556 | } |
| 557 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 558 | Box FramebufferState::getDimensions() const |
| 559 | { |
| 560 | ASSERT(attachmentsHaveSameDimensions()); |
| 561 | ASSERT(getFirstNonNullAttachment() != nullptr); |
| 562 | Extents extents = getFirstNonNullAttachment()->getSize(); |
| 563 | return Box(0, 0, 0, extents.width, extents.height, extents.depth); |
| 564 | } |
| 565 | |
Jamie Madill | 7aea7e0 | 2016-05-10 10:39:45 -0400 | [diff] [blame] | 566 | Framebuffer::Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id) |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 567 | : mState(caps), |
| 568 | mImpl(factory->createFramebuffer(mState)), |
| 569 | mId(id), |
| 570 | mCachedStatus(), |
| 571 | mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT), |
| 572 | mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 573 | { |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 574 | ASSERT(mId != 0); |
| 575 | ASSERT(mImpl != nullptr); |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 576 | ASSERT(mState.mColorAttachments.size() == static_cast<size_t>(caps.maxColorAttachments)); |
| 577 | |
Jamie Madill | 1e5499d | 2017-04-05 11:22:16 -0400 | [diff] [blame] | 578 | for (uint32_t colorIndex = 0; |
| 579 | colorIndex < static_cast<uint32_t>(mState.mColorAttachments.size()); ++colorIndex) |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 580 | { |
Jamie Madill | 1e5499d | 2017-04-05 11:22:16 -0400 | [diff] [blame] | 581 | mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex); |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 582 | } |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 583 | } |
| 584 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 585 | Framebuffer::Framebuffer(const egl::Display *display, egl::Surface *surface) |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 586 | : mState(), |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 587 | mImpl(surface->getImplementation()->createDefaultFramebuffer(mState)), |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 588 | mId(0), |
| 589 | mCachedStatus(GL_FRAMEBUFFER_COMPLETE), |
| 590 | mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT), |
| 591 | mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT) |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 592 | { |
Geoff Lang | da88add | 2014-12-01 10:22:01 -0500 | [diff] [blame] | 593 | ASSERT(mImpl != nullptr); |
Jamie Madill | 1e5499d | 2017-04-05 11:22:16 -0400 | [diff] [blame] | 594 | mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0); |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 595 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 596 | const Context *proxyContext = display->getProxyContext(); |
| 597 | |
| 598 | setAttachmentImpl(proxyContext, GL_FRAMEBUFFER_DEFAULT, GL_BACK, gl::ImageIndex::MakeInvalid(), |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 599 | surface, FramebufferAttachment::kDefaultNumViews, |
| 600 | FramebufferAttachment::kDefaultBaseViewIndex, |
| 601 | FramebufferAttachment::kDefaultMultiviewLayout, |
| 602 | FramebufferAttachment::kDefaultViewportOffsets); |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 603 | |
| 604 | if (surface->getConfig()->depthSize > 0) |
| 605 | { |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 606 | setAttachmentImpl( |
| 607 | proxyContext, GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, gl::ImageIndex::MakeInvalid(), surface, |
| 608 | FramebufferAttachment::kDefaultNumViews, FramebufferAttachment::kDefaultBaseViewIndex, |
| 609 | FramebufferAttachment::kDefaultMultiviewLayout, |
| 610 | FramebufferAttachment::kDefaultViewportOffsets); |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | if (surface->getConfig()->stencilSize > 0) |
| 614 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 615 | setAttachmentImpl(proxyContext, GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 616 | gl::ImageIndex::MakeInvalid(), surface, |
| 617 | FramebufferAttachment::kDefaultNumViews, |
| 618 | FramebufferAttachment::kDefaultBaseViewIndex, |
| 619 | FramebufferAttachment::kDefaultMultiviewLayout, |
| 620 | FramebufferAttachment::kDefaultViewportOffsets); |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 621 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 624 | Framebuffer::Framebuffer(rx::GLImplFactory *factory) |
| 625 | : mState(), |
| 626 | mImpl(factory->createFramebuffer(mState)), |
| 627 | mId(0), |
| 628 | mCachedStatus(GL_FRAMEBUFFER_UNDEFINED_OES), |
| 629 | mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT), |
| 630 | mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT) |
| 631 | { |
Jamie Madill | 1e5499d | 2017-04-05 11:22:16 -0400 | [diff] [blame] | 632 | mDirtyColorAttachmentBindings.emplace_back(this, DIRTY_BIT_COLOR_ATTACHMENT_0); |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 633 | } |
| 634 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 635 | Framebuffer::~Framebuffer() |
| 636 | { |
Geoff Lang | da88add | 2014-12-01 10:22:01 -0500 | [diff] [blame] | 637 | SafeDelete(mImpl); |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 640 | void Framebuffer::onDestroy(const Context *context) |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 641 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 642 | for (auto &attachment : mState.mColorAttachments) |
| 643 | { |
| 644 | attachment.detach(context); |
| 645 | } |
| 646 | mState.mDepthAttachment.detach(context); |
| 647 | mState.mStencilAttachment.detach(context); |
| 648 | mState.mWebGLDepthAttachment.detach(context); |
| 649 | mState.mWebGLStencilAttachment.detach(context); |
| 650 | mState.mWebGLDepthStencilAttachment.detach(context); |
| 651 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 652 | mImpl->destroy(context); |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | void Framebuffer::destroyDefault(const egl::Display *display) |
| 656 | { |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 657 | mImpl->destroyDefault(display); |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 658 | } |
| 659 | |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 660 | void Framebuffer::setLabel(const std::string &label) |
| 661 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 662 | mState.mLabel = label; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | const std::string &Framebuffer::getLabel() const |
| 666 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 667 | return mState.mLabel; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 668 | } |
| 669 | |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 670 | bool Framebuffer::detachTexture(const Context *context, GLuint textureId) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 671 | { |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 672 | return detachResourceById(context, GL_TEXTURE, textureId); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 675 | bool Framebuffer::detachRenderbuffer(const Context *context, GLuint renderbufferId) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 676 | { |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 677 | return detachResourceById(context, GL_RENDERBUFFER, renderbufferId); |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 678 | } |
Jamie Madill | e261b44 | 2014-06-25 12:42:21 -0400 | [diff] [blame] | 679 | |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 680 | bool Framebuffer::detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId) |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 681 | { |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 682 | bool found = false; |
| 683 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 684 | for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex) |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 685 | { |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 686 | if (detachMatchingAttachment(context, &mState.mColorAttachments[colorIndex], resourceType, |
| 687 | resourceId, DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex)) |
| 688 | { |
| 689 | found = true; |
| 690 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 693 | if (context->isWebGL1()) |
| 694 | { |
| 695 | const std::array<FramebufferAttachment *, 3> attachments = { |
| 696 | {&mState.mWebGLDepthStencilAttachment, &mState.mWebGLDepthAttachment, |
| 697 | &mState.mWebGLStencilAttachment}}; |
| 698 | for (FramebufferAttachment *attachment : attachments) |
| 699 | { |
| 700 | if (attachment->isAttached() && attachment->type() == resourceType && |
| 701 | attachment->id() == resourceId) |
| 702 | { |
| 703 | resetAttachment(context, attachment->getBinding()); |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 704 | found = true; |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | } |
| 708 | else |
| 709 | { |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 710 | if (detachMatchingAttachment(context, &mState.mDepthAttachment, resourceType, resourceId, |
| 711 | DIRTY_BIT_DEPTH_ATTACHMENT)) |
| 712 | { |
| 713 | found = true; |
| 714 | } |
| 715 | if (detachMatchingAttachment(context, &mState.mStencilAttachment, resourceType, resourceId, |
| 716 | DIRTY_BIT_STENCIL_ATTACHMENT)) |
| 717 | { |
| 718 | found = true; |
| 719 | } |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 720 | } |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 721 | |
| 722 | return found; |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 723 | } |
| 724 | |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 725 | bool Framebuffer::detachMatchingAttachment(const Context *context, |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 726 | FramebufferAttachment *attachment, |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 727 | GLenum matchType, |
| 728 | GLuint matchId, |
| 729 | size_t dirtyBit) |
| 730 | { |
| 731 | if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId) |
| 732 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 733 | attachment->detach(context); |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 734 | mDirtyBits.set(dirtyBit); |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 735 | mState.mResourceNeedsInit.set(dirtyBit, false); |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 736 | return true; |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 737 | } |
Jamie Madill | 8693bdb | 2017-09-02 15:32:14 -0400 | [diff] [blame] | 738 | |
| 739 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 742 | const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 743 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 744 | return mState.getColorAttachment(colorAttachment); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 747 | const FramebufferAttachment *Framebuffer::getDepthbuffer() const |
Geoff Lang | 646559f | 2013-08-15 11:08:15 -0400 | [diff] [blame] | 748 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 749 | return mState.getDepthAttachment(); |
Geoff Lang | 646559f | 2013-08-15 11:08:15 -0400 | [diff] [blame] | 750 | } |
| 751 | |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 752 | const FramebufferAttachment *Framebuffer::getStencilbuffer() const |
| 753 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 754 | return mState.getStencilAttachment(); |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 755 | } |
| 756 | |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 757 | const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const |
| 758 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 759 | return mState.getDepthStencilAttachment(); |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const |
daniel@transgaming.com | d2b4702 | 2012-11-28 19:40:10 +0000 | [diff] [blame] | 763 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 764 | return mState.getDepthOrStencilAttachment(); |
daniel@transgaming.com | d2b4702 | 2012-11-28 19:40:10 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Corentin Wallez | b1d0a255 | 2016-12-19 16:15:54 -0500 | [diff] [blame] | 767 | const FramebufferAttachment *Framebuffer::getStencilOrDepthStencilAttachment() const |
| 768 | { |
| 769 | return mState.getStencilOrDepthStencilAttachment(); |
| 770 | } |
| 771 | |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 772 | const FramebufferAttachment *Framebuffer::getReadColorbuffer() const |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 773 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 774 | return mState.getReadAttachment(); |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 775 | } |
| 776 | |
shannon.woods%transgaming.com@gtempaccount.com | f6863e0 | 2013-04-13 03:34:00 +0000 | [diff] [blame] | 777 | GLenum Framebuffer::getReadColorbufferType() const |
| 778 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 779 | const FramebufferAttachment *readAttachment = mState.getReadAttachment(); |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 780 | return (readAttachment != nullptr ? readAttachment->type() : GL_NONE); |
shannon.woods%transgaming.com@gtempaccount.com | f6863e0 | 2013-04-13 03:34:00 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 783 | const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 784 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 785 | return mState.getFirstColorAttachment(); |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Jamie Madill | 6dd06ea | 2017-07-19 13:47:55 -0400 | [diff] [blame] | 788 | const FramebufferAttachment *Framebuffer::getFirstNonNullAttachment() const |
| 789 | { |
| 790 | return mState.getFirstNonNullAttachment(); |
| 791 | } |
| 792 | |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 793 | const FramebufferAttachment *Framebuffer::getAttachment(GLenum attachment) const |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 794 | { |
Geoff Lang | 4b7f12b | 2016-06-21 16:47:07 -0400 | [diff] [blame] | 795 | return mState.getAttachment(attachment); |
Geoff Lang | 55ba29c | 2013-07-11 16:57:53 -0400 | [diff] [blame] | 796 | } |
| 797 | |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 798 | size_t Framebuffer::getDrawbufferStateCount() const |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 799 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 800 | return mState.mDrawBufferStates.size(); |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const |
| 804 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 805 | ASSERT(drawBuffer < mState.mDrawBufferStates.size()); |
| 806 | return mState.mDrawBufferStates[drawBuffer]; |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Jamie Madill | 1fbc59f | 2016-02-24 15:25:51 -0500 | [diff] [blame] | 809 | const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const |
| 810 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 811 | return mState.getDrawBufferStates(); |
Jamie Madill | 1fbc59f | 2016-02-24 15:25:51 -0500 | [diff] [blame] | 812 | } |
| 813 | |
Geoff Lang | 164d54e | 2014-12-01 10:55:33 -0500 | [diff] [blame] | 814 | void Framebuffer::setDrawBuffers(size_t count, const GLenum *buffers) |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 815 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 816 | auto &drawStates = mState.mDrawBufferStates; |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 817 | |
| 818 | ASSERT(count <= drawStates.size()); |
| 819 | std::copy(buffers, buffers + count, drawStates.begin()); |
| 820 | std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 821 | mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS); |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 822 | |
| 823 | mState.mEnabledDrawBuffers.reset(); |
| 824 | for (size_t index = 0; index < count; ++index) |
| 825 | { |
| 826 | if (drawStates[index] != GL_NONE && mState.mColorAttachments[index].isAttached()) |
| 827 | { |
| 828 | mState.mEnabledDrawBuffers.set(index); |
| 829 | } |
| 830 | } |
Geoff Lang | 9dd9580 | 2014-12-01 11:12:59 -0500 | [diff] [blame] | 831 | } |
| 832 | |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 833 | const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const |
| 834 | { |
Geoff Lang | 4b7f12b | 2016-06-21 16:47:07 -0400 | [diff] [blame] | 835 | return mState.getDrawBuffer(drawBuffer); |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 836 | } |
| 837 | |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 838 | GLenum Framebuffer::getDrawbufferWriteType(size_t drawBuffer) const |
| 839 | { |
| 840 | const FramebufferAttachment *attachment = mState.getDrawBuffer(drawBuffer); |
| 841 | if (attachment == nullptr) |
| 842 | { |
| 843 | return GL_NONE; |
| 844 | } |
| 845 | |
| 846 | GLenum componentType = attachment->getFormat().info->componentType; |
| 847 | switch (componentType) |
| 848 | { |
| 849 | case GL_INT: |
| 850 | case GL_UNSIGNED_INT: |
| 851 | return componentType; |
| 852 | |
| 853 | default: |
| 854 | return GL_FLOAT; |
| 855 | } |
| 856 | } |
| 857 | |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 858 | bool Framebuffer::hasEnabledDrawBuffer() const |
| 859 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 860 | for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx) |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 861 | { |
| 862 | if (getDrawBuffer(drawbufferIdx) != nullptr) |
| 863 | { |
| 864 | return true; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | return false; |
| 869 | } |
| 870 | |
Geoff Lang | 9dd9580 | 2014-12-01 11:12:59 -0500 | [diff] [blame] | 871 | GLenum Framebuffer::getReadBufferState() const |
| 872 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 873 | return mState.mReadBufferState; |
Geoff Lang | 9dd9580 | 2014-12-01 11:12:59 -0500 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | void Framebuffer::setReadBuffer(GLenum buffer) |
| 877 | { |
Jamie Madill | b885e57 | 2015-02-03 16:16:04 -0500 | [diff] [blame] | 878 | ASSERT(buffer == GL_BACK || buffer == GL_NONE || |
| 879 | (buffer >= GL_COLOR_ATTACHMENT0 && |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 880 | (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size())); |
| 881 | mState.mReadBufferState = buffer; |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 882 | mDirtyBits.set(DIRTY_BIT_READ_BUFFER); |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 885 | size_t Framebuffer::getNumColorBuffers() const |
| 886 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 887 | return mState.mColorAttachments.size(); |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 888 | } |
| 889 | |
Jamie Madill | 0df8fe4 | 2015-11-24 16:10:24 -0500 | [diff] [blame] | 890 | bool Framebuffer::hasDepth() const |
| 891 | { |
Jamie Madill | 9c33586 | 2017-07-18 11:51:38 -0400 | [diff] [blame] | 892 | return mState.hasDepth(); |
Jamie Madill | 0df8fe4 | 2015-11-24 16:10:24 -0500 | [diff] [blame] | 893 | } |
| 894 | |
shannon.woods%transgaming.com@gtempaccount.com | 3b57b4f | 2013-04-13 03:28:29 +0000 | [diff] [blame] | 895 | bool Framebuffer::hasStencil() const |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 896 | { |
Jamie Madill | 9c33586 | 2017-07-18 11:51:38 -0400 | [diff] [blame] | 897 | return mState.hasStencil(); |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 898 | } |
| 899 | |
shannonwoods@chromium.org | 24ac850 | 2013-05-30 00:01:37 +0000 | [diff] [blame] | 900 | bool Framebuffer::usingExtendedDrawBuffers() const |
| 901 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 902 | for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx) |
shannonwoods@chromium.org | 24ac850 | 2013-05-30 00:01:37 +0000 | [diff] [blame] | 903 | { |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 904 | if (getDrawBuffer(drawbufferIdx) != nullptr) |
shannonwoods@chromium.org | 24ac850 | 2013-05-30 00:01:37 +0000 | [diff] [blame] | 905 | { |
| 906 | return true; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | return false; |
| 911 | } |
| 912 | |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 913 | void Framebuffer::invalidateCompletenessCache() |
| 914 | { |
| 915 | if (mId != 0) |
| 916 | { |
| 917 | mCachedStatus.reset(); |
| 918 | } |
| 919 | } |
| 920 | |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 921 | GLenum Framebuffer::checkStatus(const Context *context) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 922 | { |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 923 | // The default framebuffer is always complete except when it is surfaceless in which |
| 924 | // case it is always unsupported. We return early because the default framebuffer may |
| 925 | // not be subject to the same rules as application FBOs. ie, it could have 0x0 size. |
Geoff Lang | 528ce3c | 2014-12-01 10:44:07 -0500 | [diff] [blame] | 926 | if (mId == 0) |
| 927 | { |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 928 | ASSERT(mCachedStatus.valid()); |
| 929 | ASSERT(mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE || |
| 930 | mCachedStatus.value() == GL_FRAMEBUFFER_UNDEFINED_OES); |
| 931 | return mCachedStatus.value(); |
Geoff Lang | 528ce3c | 2014-12-01 10:44:07 -0500 | [diff] [blame] | 932 | } |
| 933 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 934 | if (hasAnyDirtyBit() || !mCachedStatus.valid()) |
| 935 | { |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 936 | mCachedStatus = checkStatusImpl(context); |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | return mCachedStatus.value(); |
| 940 | } |
| 941 | |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 942 | GLenum Framebuffer::checkStatusImpl(const Context *context) |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 943 | { |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 944 | const ContextState &state = context->getContextState(); |
| 945 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 946 | ASSERT(mId != 0); |
| 947 | |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 948 | bool hasAttachments = false; |
| 949 | Optional<unsigned int> colorbufferSize; |
| 950 | Optional<int> samples; |
JiangYizhou | 461d9a3 | 2017-01-04 16:37:26 +0800 | [diff] [blame] | 951 | Optional<GLboolean> fixedSampleLocations; |
| 952 | bool hasRenderbuffer = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 953 | |
Martin Radev | 9bc9a32 | 2017-07-21 14:28:17 +0300 | [diff] [blame] | 954 | const FramebufferAttachment *firstAttachment = getFirstNonNullAttachment(); |
| 955 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 956 | for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 957 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 958 | if (colorAttachment.isAttached()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 959 | { |
Geoff Lang | 9f10b77 | 2017-05-16 15:51:03 -0400 | [diff] [blame] | 960 | if (!CheckAttachmentCompleteness(context, colorAttachment)) |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 961 | { |
| 962 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 963 | } |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame] | 964 | |
Geoff Lang | 677bb6f | 2017-04-05 12:40:40 -0400 | [diff] [blame] | 965 | const InternalFormat &format = *colorAttachment.getFormat().info; |
Geoff Lang | 677bb6f | 2017-04-05 12:40:40 -0400 | [diff] [blame] | 966 | if (format.depthBits > 0 || format.stencilBits > 0) |
| 967 | { |
| 968 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 969 | } |
| 970 | |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 971 | if (!CheckAttachmentSampleCompleteness(context, colorAttachment, true, &samples, |
| 972 | &fixedSampleLocations)) |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 973 | { |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 974 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE; |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 977 | // in GLES 2.0, all color attachments attachments must have the same number of bitplanes |
| 978 | // in GLES 3.0, there is no such restriction |
| 979 | if (state.getClientMajorVersion() < 3) |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 980 | { |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 981 | if (colorbufferSize.valid()) |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 982 | { |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 983 | if (format.pixelBytes != colorbufferSize.value()) |
shannon.woods%transgaming.com@gtempaccount.com | c347152 | 2013-04-13 03:34:52 +0000 | [diff] [blame] | 984 | { |
| 985 | return GL_FRAMEBUFFER_UNSUPPORTED; |
| 986 | } |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 987 | } |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 988 | else |
| 989 | { |
| 990 | colorbufferSize = format.pixelBytes; |
| 991 | } |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 992 | } |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 993 | |
Martin Radev | 9bc9a32 | 2017-07-21 14:28:17 +0300 | [diff] [blame] | 994 | if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &colorAttachment)) |
| 995 | { |
| 996 | return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE; |
| 997 | } |
| 998 | |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 999 | hasRenderbuffer = hasRenderbuffer || (colorAttachment.type() == GL_RENDERBUFFER); |
| 1000 | hasAttachments = true; |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 1001 | } |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1004 | const FramebufferAttachment &depthAttachment = mState.mDepthAttachment; |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1005 | if (depthAttachment.isAttached()) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1006 | { |
Geoff Lang | 9f10b77 | 2017-05-16 15:51:03 -0400 | [diff] [blame] | 1007 | if (!CheckAttachmentCompleteness(context, depthAttachment)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1008 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1009 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Geoff Lang | 677bb6f | 2017-04-05 12:40:40 -0400 | [diff] [blame] | 1012 | const InternalFormat &format = *depthAttachment.getFormat().info; |
Geoff Lang | 677bb6f | 2017-04-05 12:40:40 -0400 | [diff] [blame] | 1013 | if (format.depthBits == 0) |
| 1014 | { |
| 1015 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 1018 | if (!CheckAttachmentSampleCompleteness(context, depthAttachment, false, &samples, |
| 1019 | &fixedSampleLocations)) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1020 | { |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 1021 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE; |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1022 | } |
Sami Väisänen | a797e06 | 2016-05-12 15:23:40 +0300 | [diff] [blame] | 1023 | |
Martin Radev | 9bc9a32 | 2017-07-21 14:28:17 +0300 | [diff] [blame] | 1024 | if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &depthAttachment)) |
| 1025 | { |
| 1026 | return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE; |
| 1027 | } |
| 1028 | |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 1029 | hasRenderbuffer = hasRenderbuffer || (depthAttachment.type() == GL_RENDERBUFFER); |
| 1030 | hasAttachments = true; |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1033 | const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment; |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1034 | if (stencilAttachment.isAttached()) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1035 | { |
Geoff Lang | 9f10b77 | 2017-05-16 15:51:03 -0400 | [diff] [blame] | 1036 | if (!CheckAttachmentCompleteness(context, stencilAttachment)) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1037 | { |
| 1038 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 1039 | } |
| 1040 | |
Geoff Lang | 677bb6f | 2017-04-05 12:40:40 -0400 | [diff] [blame] | 1041 | const InternalFormat &format = *stencilAttachment.getFormat().info; |
Geoff Lang | 677bb6f | 2017-04-05 12:40:40 -0400 | [diff] [blame] | 1042 | if (format.stencilBits == 0) |
| 1043 | { |
| 1044 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 1047 | if (!CheckAttachmentSampleCompleteness(context, stencilAttachment, false, &samples, |
| 1048 | &fixedSampleLocations)) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1049 | { |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 1050 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE; |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 1051 | } |
Corentin Wallez | 086d59a | 2016-04-29 09:06:49 -0400 | [diff] [blame] | 1052 | |
Martin Radev | 9bc9a32 | 2017-07-21 14:28:17 +0300 | [diff] [blame] | 1053 | if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, &stencilAttachment)) |
| 1054 | { |
| 1055 | return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE; |
| 1056 | } |
| 1057 | |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 1058 | hasRenderbuffer = hasRenderbuffer || (stencilAttachment.type() == GL_RENDERBUFFER); |
| 1059 | hasAttachments = true; |
| 1060 | } |
| 1061 | |
| 1062 | // Starting from ES 3.0 stencil and depth, if present, should be the same image |
| 1063 | if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() && |
| 1064 | stencilAttachment.isAttached() && stencilAttachment != depthAttachment) |
| 1065 | { |
| 1066 | return GL_FRAMEBUFFER_UNSUPPORTED; |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1069 | // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL. |
| 1070 | if (state.isWebGL1()) |
| 1071 | { |
| 1072 | if (!mState.mWebGLDepthStencilConsistent) |
| 1073 | { |
| 1074 | return GL_FRAMEBUFFER_UNSUPPORTED; |
| 1075 | } |
| 1076 | |
| 1077 | if (mState.mWebGLDepthStencilAttachment.isAttached()) |
| 1078 | { |
| 1079 | if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 || |
| 1080 | mState.mWebGLDepthStencilAttachment.getStencilSize() == 0) |
| 1081 | { |
| 1082 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 1083 | } |
Martin Radev | 9bc9a32 | 2017-07-21 14:28:17 +0300 | [diff] [blame] | 1084 | |
| 1085 | if (!CheckMultiviewStateMatchesForCompleteness(firstAttachment, |
| 1086 | &mState.mWebGLDepthStencilAttachment)) |
| 1087 | { |
| 1088 | return GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_ANGLE; |
| 1089 | } |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1090 | } |
| 1091 | else if (mState.mStencilAttachment.isAttached() && |
| 1092 | mState.mStencilAttachment.getDepthSize() > 0) |
| 1093 | { |
| 1094 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 1095 | } |
| 1096 | else if (mState.mDepthAttachment.isAttached() && |
| 1097 | mState.mDepthAttachment.getStencilSize() > 0) |
| 1098 | { |
| 1099 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 1100 | } |
| 1101 | } |
| 1102 | |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 1103 | // ES3.1(section 9.4) requires that if no image is attached to the framebuffer, and either the |
| 1104 | // value of the framebuffer's FRAMEBUFFER_DEFAULT_WIDTH or FRAMEBUFFER_DEFAULT_HEIGHT parameters |
| 1105 | // is zero, the framebuffer is considered incomplete. |
JiangYizhou | 461d9a3 | 2017-01-04 16:37:26 +0800 | [diff] [blame] | 1106 | GLint defaultWidth = mState.getDefaultWidth(); |
| 1107 | GLint defaultHeight = mState.getDefaultHeight(); |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 1108 | if (!hasAttachments && (defaultWidth == 0 || defaultHeight == 0)) |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 1109 | { |
| 1110 | return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT; |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Geoff Lang | a0e0aeb | 2017-04-12 15:06:29 -0400 | [diff] [blame] | 1113 | // In ES 2.0 and WebGL, all color attachments must have the same width and height. |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 1114 | // In ES 3.0, there is no such restriction. |
Geoff Lang | a0e0aeb | 2017-04-12 15:06:29 -0400 | [diff] [blame] | 1115 | if ((state.getClientMajorVersion() < 3 || state.getExtensions().webglCompatibility) && |
| 1116 | !mState.attachmentsHaveSameDimensions()) |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 1117 | { |
| 1118 | return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS; |
| 1119 | } |
| 1120 | |
Geoff Lang | a1b9d5a | 2017-05-18 11:22:27 -0400 | [diff] [blame] | 1121 | // ES3.1(section 9.4) requires that if the attached images are a mix of renderbuffers and |
| 1122 | // textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all attached textures. |
JiangYizhou | 461d9a3 | 2017-01-04 16:37:26 +0800 | [diff] [blame] | 1123 | if (fixedSampleLocations.valid() && hasRenderbuffer && !fixedSampleLocations.value()) |
| 1124 | { |
| 1125 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE; |
| 1126 | } |
| 1127 | |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 1128 | syncState(context); |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 1129 | if (!mImpl->checkStatus()) |
| 1130 | { |
| 1131 | return GL_FRAMEBUFFER_UNSUPPORTED; |
| 1132 | } |
| 1133 | |
| 1134 | return GL_FRAMEBUFFER_COMPLETE; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1135 | } |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1136 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1137 | Error Framebuffer::discard(const Context *context, size_t count, const GLenum *attachments) |
Austin Kinross | 0833263 | 2015-05-05 13:35:47 -0700 | [diff] [blame] | 1138 | { |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1139 | // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations |
| 1140 | // can be no-ops, so we should probably do that to ensure consistency. |
| 1141 | // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL. |
| 1142 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1143 | return mImpl->discard(context, count, attachments); |
Austin Kinross | 0833263 | 2015-05-05 13:35:47 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1146 | Error Framebuffer::invalidate(const Context *context, size_t count, const GLenum *attachments) |
Jamie Madill | 2d96b9e | 2014-08-29 15:46:47 -0400 | [diff] [blame] | 1147 | { |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1148 | // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations |
| 1149 | // can be no-ops, so we should probably do that to ensure consistency. |
| 1150 | // TODO(jmadill): WebGL behaviour, and robust resource init behaviour without WebGL. |
| 1151 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1152 | return mImpl->invalidate(context, count, attachments); |
Jamie Madill | 2d96b9e | 2014-08-29 15:46:47 -0400 | [diff] [blame] | 1153 | } |
| 1154 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1155 | bool Framebuffer::partialClearNeedsInit(const Context *context, |
| 1156 | bool color, |
| 1157 | bool depth, |
| 1158 | bool stencil) |
| 1159 | { |
| 1160 | const auto &glState = context->getGLState(); |
| 1161 | |
| 1162 | if (!glState.isRobustResourceInitEnabled()) |
| 1163 | { |
| 1164 | return false; |
| 1165 | } |
| 1166 | |
| 1167 | // Scissors can affect clearing. |
| 1168 | // TODO(jmadill): Check for complete scissor overlap. |
| 1169 | if (glState.isScissorTestEnabled()) |
| 1170 | { |
| 1171 | return true; |
| 1172 | } |
| 1173 | |
| 1174 | // If colors masked, we must clear before we clear. Do a simple check. |
| 1175 | // TODO(jmadill): Filter out unused color channels from the test. |
| 1176 | if (color) |
| 1177 | { |
| 1178 | const auto &blend = glState.getBlendState(); |
| 1179 | if (!(blend.colorMaskRed && blend.colorMaskGreen && blend.colorMaskBlue && |
| 1180 | blend.colorMaskAlpha)) |
| 1181 | { |
| 1182 | return true; |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | const auto &depthStencil = glState.getDepthStencilState(); |
| 1187 | ASSERT(depthStencil.stencilBackMask == depthStencil.stencilMask); |
| 1188 | if (stencil && depthStencil.stencilMask != depthStencil.stencilWritemask) |
| 1189 | { |
| 1190 | return true; |
| 1191 | } |
| 1192 | |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1196 | Error Framebuffer::invalidateSub(const Context *context, |
| 1197 | size_t count, |
| 1198 | const GLenum *attachments, |
| 1199 | const gl::Rectangle &area) |
Jamie Madill | 400a441 | 2014-08-29 15:46:45 -0400 | [diff] [blame] | 1200 | { |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1201 | // Back-ends might make the contents of the FBO undefined. In WebGL 2.0, invalidate operations |
| 1202 | // can be no-ops, so we should probably do that to ensure consistency. |
| 1203 | // TODO(jmadill): Make a invalidate no-op in WebGL 2.0. |
| 1204 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1205 | return mImpl->invalidateSub(context, count, attachments, area); |
Jamie Madill | 400a441 | 2014-08-29 15:46:45 -0400 | [diff] [blame] | 1206 | } |
| 1207 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 1208 | Error Framebuffer::clear(const gl::Context *context, GLbitfield mask) |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 1209 | { |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1210 | const auto &glState = context->getGLState(); |
| 1211 | if (glState.isRasterizerDiscardEnabled()) |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 1212 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 1213 | return NoError(); |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 1214 | } |
| 1215 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1216 | const auto &blend = glState.getBlendState(); |
| 1217 | const auto &depthStencil = glState.getDepthStencilState(); |
| 1218 | |
| 1219 | bool color = (mask & GL_COLOR_BUFFER_BIT) != 0 && !IsColorMaskedOut(blend); |
| 1220 | bool depth = (mask & GL_DEPTH_BUFFER_BIT) != 0 && !IsDepthMaskedOut(depthStencil); |
| 1221 | bool stencil = (mask & GL_STENCIL_BUFFER_BIT) != 0 && !IsStencilMaskedOut(depthStencil); |
| 1222 | |
| 1223 | if (partialClearNeedsInit(context, color, depth, stencil)) |
| 1224 | { |
| 1225 | ANGLE_TRY(ensureDrawAttachmentsInitialized(context)); |
| 1226 | } |
| 1227 | |
| 1228 | ANGLE_TRY(mImpl->clear(context, mask)); |
| 1229 | |
| 1230 | if (glState.isRobustResourceInitEnabled()) |
| 1231 | { |
| 1232 | markDrawAttachmentsInitialized(color, depth, stencil); |
| 1233 | } |
| 1234 | |
| 1235 | return NoError(); |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 1236 | } |
| 1237 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 1238 | Error Framebuffer::clearBufferfv(const gl::Context *context, |
Jamie Madill | 1b94d43 | 2015-08-07 13:23:23 -0400 | [diff] [blame] | 1239 | GLenum buffer, |
| 1240 | GLint drawbuffer, |
| 1241 | const GLfloat *values) |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 1242 | { |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1243 | if (context->getGLState().isRasterizerDiscardEnabled() || |
| 1244 | IsClearBufferMaskedOut(context, buffer)) |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 1245 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 1246 | return NoError(); |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 1247 | } |
| 1248 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1249 | if (partialBufferClearNeedsInit(context, buffer)) |
| 1250 | { |
| 1251 | ANGLE_TRY(ensureBufferInitialized(context, buffer, drawbuffer)); |
| 1252 | } |
| 1253 | |
| 1254 | ANGLE_TRY(mImpl->clearBufferfv(context, buffer, drawbuffer, values)); |
| 1255 | |
| 1256 | if (context->isRobustResourceInitEnabled()) |
| 1257 | { |
| 1258 | markBufferInitialized(buffer, drawbuffer); |
| 1259 | } |
| 1260 | return NoError(); |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 1261 | } |
| 1262 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 1263 | Error Framebuffer::clearBufferuiv(const gl::Context *context, |
Jamie Madill | 1b94d43 | 2015-08-07 13:23:23 -0400 | [diff] [blame] | 1264 | GLenum buffer, |
| 1265 | GLint drawbuffer, |
| 1266 | const GLuint *values) |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 1267 | { |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1268 | if (context->getGLState().isRasterizerDiscardEnabled() || |
| 1269 | IsClearBufferMaskedOut(context, buffer)) |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 1270 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 1271 | return NoError(); |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 1272 | } |
| 1273 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1274 | if (partialBufferClearNeedsInit(context, buffer)) |
| 1275 | { |
| 1276 | ANGLE_TRY(ensureBufferInitialized(context, buffer, drawbuffer)); |
| 1277 | } |
| 1278 | |
| 1279 | ANGLE_TRY(mImpl->clearBufferuiv(context, buffer, drawbuffer, values)); |
| 1280 | |
| 1281 | if (context->isRobustResourceInitEnabled()) |
| 1282 | { |
| 1283 | markBufferInitialized(buffer, drawbuffer); |
| 1284 | } |
| 1285 | return NoError(); |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 1286 | } |
| 1287 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 1288 | Error Framebuffer::clearBufferiv(const gl::Context *context, |
Jamie Madill | 1b94d43 | 2015-08-07 13:23:23 -0400 | [diff] [blame] | 1289 | GLenum buffer, |
| 1290 | GLint drawbuffer, |
| 1291 | const GLint *values) |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 1292 | { |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1293 | if (context->getGLState().isRasterizerDiscardEnabled() || |
| 1294 | IsClearBufferMaskedOut(context, buffer)) |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 1295 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 1296 | return NoError(); |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 1297 | } |
| 1298 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1299 | if (partialBufferClearNeedsInit(context, buffer)) |
| 1300 | { |
| 1301 | ANGLE_TRY(ensureBufferInitialized(context, buffer, drawbuffer)); |
| 1302 | } |
| 1303 | |
| 1304 | ANGLE_TRY(mImpl->clearBufferiv(context, buffer, drawbuffer, values)); |
| 1305 | |
| 1306 | if (context->isRobustResourceInitEnabled()) |
| 1307 | { |
| 1308 | markBufferInitialized(buffer, drawbuffer); |
| 1309 | } |
| 1310 | return NoError(); |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 1311 | } |
| 1312 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 1313 | Error Framebuffer::clearBufferfi(const gl::Context *context, |
Jamie Madill | 1b94d43 | 2015-08-07 13:23:23 -0400 | [diff] [blame] | 1314 | GLenum buffer, |
| 1315 | GLint drawbuffer, |
| 1316 | GLfloat depth, |
| 1317 | GLint stencil) |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 1318 | { |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1319 | if (context->getGLState().isRasterizerDiscardEnabled() || |
| 1320 | IsClearBufferMaskedOut(context, buffer)) |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 1321 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 1322 | return NoError(); |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 1323 | } |
| 1324 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1325 | if (partialBufferClearNeedsInit(context, buffer)) |
| 1326 | { |
| 1327 | ANGLE_TRY(ensureBufferInitialized(context, buffer, drawbuffer)); |
| 1328 | } |
| 1329 | |
| 1330 | ANGLE_TRY(mImpl->clearBufferfi(context, buffer, drawbuffer, depth, stencil)); |
| 1331 | |
| 1332 | if (context->isRobustResourceInitEnabled()) |
| 1333 | { |
| 1334 | markBufferInitialized(buffer, drawbuffer); |
| 1335 | } |
| 1336 | return NoError(); |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 1337 | } |
| 1338 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1339 | GLenum Framebuffer::getImplementationColorReadFormat(const Context *context) const |
Geoff Lang | bce529e | 2014-12-01 12:48:41 -0500 | [diff] [blame] | 1340 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1341 | return mImpl->getImplementationColorReadFormat(context); |
Geoff Lang | bce529e | 2014-12-01 12:48:41 -0500 | [diff] [blame] | 1342 | } |
| 1343 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1344 | GLenum Framebuffer::getImplementationColorReadType(const Context *context) const |
Geoff Lang | bce529e | 2014-12-01 12:48:41 -0500 | [diff] [blame] | 1345 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1346 | return mImpl->getImplementationColorReadType(context); |
Geoff Lang | bce529e | 2014-12-01 12:48:41 -0500 | [diff] [blame] | 1347 | } |
| 1348 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 1349 | Error Framebuffer::readPixels(const gl::Context *context, |
Jamie Madill | c29968b | 2016-01-20 11:17:23 -0500 | [diff] [blame] | 1350 | const Rectangle &area, |
Jamie Madill | 1b94d43 | 2015-08-07 13:23:23 -0400 | [diff] [blame] | 1351 | GLenum format, |
| 1352 | GLenum type, |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1353 | void *pixels) |
Geoff Lang | bce529e | 2014-12-01 12:48:41 -0500 | [diff] [blame] | 1354 | { |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1355 | ANGLE_TRY(ensureReadAttachmentInitialized(context, GL_COLOR_BUFFER_BIT)); |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1356 | ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels)); |
Geoff Lang | 520c4ae | 2015-05-05 13:12:36 -0400 | [diff] [blame] | 1357 | |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 1358 | Buffer *unpackBuffer = context->getGLState().getUnpackState().pixelBuffer.get(); |
Geoff Lang | 520c4ae | 2015-05-05 13:12:36 -0400 | [diff] [blame] | 1359 | if (unpackBuffer) |
| 1360 | { |
| 1361 | unpackBuffer->onPixelUnpack(); |
| 1362 | } |
| 1363 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1364 | return NoError(); |
Geoff Lang | bce529e | 2014-12-01 12:48:41 -0500 | [diff] [blame] | 1365 | } |
| 1366 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 1367 | Error Framebuffer::blit(const gl::Context *context, |
Jamie Madill | c29968b | 2016-01-20 11:17:23 -0500 | [diff] [blame] | 1368 | const Rectangle &sourceArea, |
| 1369 | const Rectangle &destArea, |
Geoff Lang | 242468f | 2015-09-24 14:15:41 -0400 | [diff] [blame] | 1370 | GLbitfield mask, |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 1371 | GLenum filter) |
Geoff Lang | 54bd5a4 | 2014-12-01 12:51:04 -0500 | [diff] [blame] | 1372 | { |
He Yunchao | 6be602d | 2016-12-22 14:33:07 +0800 | [diff] [blame] | 1373 | GLbitfield blitMask = mask; |
| 1374 | |
| 1375 | // Note that blitting is called against draw framebuffer. |
| 1376 | // See the code in gl::Context::blitFramebuffer. |
| 1377 | if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer()) |
| 1378 | { |
| 1379 | blitMask &= ~GL_COLOR_BUFFER_BIT; |
| 1380 | } |
| 1381 | |
| 1382 | if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr) |
| 1383 | { |
| 1384 | blitMask &= ~GL_STENCIL_BUFFER_BIT; |
| 1385 | } |
| 1386 | |
| 1387 | if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr) |
| 1388 | { |
| 1389 | blitMask &= ~GL_DEPTH_BUFFER_BIT; |
| 1390 | } |
| 1391 | |
| 1392 | if (!blitMask) |
| 1393 | { |
| 1394 | return NoError(); |
| 1395 | } |
| 1396 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1397 | auto *sourceFBO = context->getGLState().getReadFramebuffer(); |
| 1398 | ANGLE_TRY(sourceFBO->ensureReadAttachmentInitialized(context, blitMask)); |
| 1399 | |
| 1400 | // TODO(jmadill): Only clear if not the full FBO dimensions, and only specified bitmask. |
| 1401 | ANGLE_TRY(ensureDrawAttachmentsInitialized(context)); |
| 1402 | |
He Yunchao | 6be602d | 2016-12-22 14:33:07 +0800 | [diff] [blame] | 1403 | return mImpl->blit(context, sourceArea, destArea, blitMask, filter); |
Geoff Lang | 54bd5a4 | 2014-12-01 12:51:04 -0500 | [diff] [blame] | 1404 | } |
| 1405 | |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 1406 | int Framebuffer::getSamples(const Context *context) |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 1407 | { |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 1408 | if (complete(context)) |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 1409 | { |
Jamie Madill | 9c33586 | 2017-07-18 11:51:38 -0400 | [diff] [blame] | 1410 | return getCachedSamples(context); |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 1411 | } |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 1412 | |
| 1413 | return 0; |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Jamie Madill | 9c33586 | 2017-07-18 11:51:38 -0400 | [diff] [blame] | 1416 | int Framebuffer::getCachedSamples(const Context *context) |
| 1417 | { |
| 1418 | // For a complete framebuffer, all attachments must have the same sample count. |
| 1419 | // In this case return the first nonzero sample size. |
| 1420 | const auto *firstNonNullAttachment = mState.getFirstNonNullAttachment(); |
| 1421 | if (firstNonNullAttachment) |
| 1422 | { |
| 1423 | ASSERT(firstNonNullAttachment->isAttached()); |
| 1424 | return firstNonNullAttachment->getSamples(); |
| 1425 | } |
| 1426 | |
| 1427 | // No attachments found. |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 1431 | Error Framebuffer::getSamplePosition(size_t index, GLfloat *xy) const |
| 1432 | { |
| 1433 | ANGLE_TRY(mImpl->getSamplePosition(index, xy)); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 1434 | return NoError(); |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 1435 | } |
| 1436 | |
Jamie Madill | e261b44 | 2014-06-25 12:42:21 -0400 | [diff] [blame] | 1437 | bool Framebuffer::hasValidDepthStencil() const |
| 1438 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1439 | return mState.getDepthStencilAttachment() != nullptr; |
Jamie Madill | e261b44 | 2014-06-25 12:42:21 -0400 | [diff] [blame] | 1440 | } |
| 1441 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1442 | void Framebuffer::setAttachment(const Context *context, |
| 1443 | GLenum type, |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1444 | GLenum binding, |
| 1445 | const ImageIndex &textureIndex, |
| 1446 | FramebufferAttachmentObject *resource) |
Geoff Lang | ab75a05 | 2014-10-15 12:56:37 -0400 | [diff] [blame] | 1447 | { |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1448 | setAttachment(context, type, binding, textureIndex, resource, |
| 1449 | FramebufferAttachment::kDefaultNumViews, |
| 1450 | FramebufferAttachment::kDefaultBaseViewIndex, |
| 1451 | FramebufferAttachment::kDefaultMultiviewLayout, |
| 1452 | FramebufferAttachment::kDefaultViewportOffsets); |
| 1453 | } |
| 1454 | |
| 1455 | void Framebuffer::setAttachment(const Context *context, |
| 1456 | GLenum type, |
| 1457 | GLenum binding, |
| 1458 | const ImageIndex &textureIndex, |
| 1459 | FramebufferAttachmentObject *resource, |
| 1460 | GLsizei numViews, |
| 1461 | GLuint baseViewIndex, |
| 1462 | GLenum multiviewLayout, |
| 1463 | const GLint *viewportOffsets) |
| 1464 | { |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1465 | // Context may be null in unit tests. |
| 1466 | if (!context || !context->isWebGL1()) |
| 1467 | { |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1468 | setAttachmentImpl(context, type, binding, textureIndex, resource, numViews, baseViewIndex, |
| 1469 | multiviewLayout, viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | switch (binding) |
| 1474 | { |
| 1475 | case GL_DEPTH_STENCIL: |
| 1476 | case GL_DEPTH_STENCIL_ATTACHMENT: |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1477 | mState.mWebGLDepthStencilAttachment.attach(context, type, binding, textureIndex, |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1478 | resource, numViews, baseViewIndex, |
| 1479 | multiviewLayout, viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1480 | break; |
| 1481 | case GL_DEPTH: |
| 1482 | case GL_DEPTH_ATTACHMENT: |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1483 | mState.mWebGLDepthAttachment.attach(context, type, binding, textureIndex, resource, |
| 1484 | numViews, baseViewIndex, multiviewLayout, |
| 1485 | viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1486 | break; |
| 1487 | case GL_STENCIL: |
| 1488 | case GL_STENCIL_ATTACHMENT: |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1489 | mState.mWebGLStencilAttachment.attach(context, type, binding, textureIndex, resource, |
| 1490 | numViews, baseViewIndex, multiviewLayout, |
| 1491 | viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1492 | break; |
| 1493 | default: |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1494 | setAttachmentImpl(context, type, binding, textureIndex, resource, numViews, |
| 1495 | baseViewIndex, multiviewLayout, viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1496 | return; |
| 1497 | } |
| 1498 | |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1499 | commitWebGL1DepthStencilIfConsistent(context, numViews, baseViewIndex, multiviewLayout, |
| 1500 | viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1501 | } |
| 1502 | |
Martin Radev | 82ef774 | 2017-08-08 17:44:58 +0300 | [diff] [blame] | 1503 | void Framebuffer::setAttachmentMultiviewLayered(const Context *context, |
| 1504 | GLenum type, |
| 1505 | GLenum binding, |
| 1506 | const ImageIndex &textureIndex, |
| 1507 | FramebufferAttachmentObject *resource, |
| 1508 | GLsizei numViews, |
| 1509 | GLint baseViewIndex) |
| 1510 | { |
| 1511 | setAttachment(context, type, binding, textureIndex, resource, numViews, baseViewIndex, |
| 1512 | GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE, |
| 1513 | FramebufferAttachment::kDefaultViewportOffsets); |
| 1514 | } |
| 1515 | |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1516 | void Framebuffer::setAttachmentMultiviewSideBySide(const Context *context, |
| 1517 | GLenum type, |
| 1518 | GLenum binding, |
| 1519 | const ImageIndex &textureIndex, |
| 1520 | FramebufferAttachmentObject *resource, |
| 1521 | GLsizei numViews, |
| 1522 | const GLint *viewportOffsets) |
| 1523 | { |
| 1524 | setAttachment(context, type, binding, textureIndex, resource, numViews, |
| 1525 | FramebufferAttachment::kDefaultBaseViewIndex, |
| 1526 | GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE, viewportOffsets); |
| 1527 | } |
| 1528 | |
| 1529 | void Framebuffer::commitWebGL1DepthStencilIfConsistent(const Context *context, |
| 1530 | GLsizei numViews, |
| 1531 | GLuint baseViewIndex, |
| 1532 | GLenum multiviewLayout, |
| 1533 | const GLint *viewportOffsets) |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1534 | { |
| 1535 | int count = 0; |
| 1536 | |
| 1537 | std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment, |
| 1538 | &mState.mWebGLDepthAttachment, |
| 1539 | &mState.mWebGLStencilAttachment}}; |
| 1540 | for (FramebufferAttachment *attachment : attachments) |
| 1541 | { |
| 1542 | if (attachment->isAttached()) |
| 1543 | { |
| 1544 | count++; |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | mState.mWebGLDepthStencilConsistent = (count <= 1); |
| 1549 | if (!mState.mWebGLDepthStencilConsistent) |
| 1550 | { |
| 1551 | // Inconsistent. |
| 1552 | return; |
| 1553 | } |
| 1554 | |
Geoff Lang | e466c55 | 2017-03-17 15:24:12 -0400 | [diff] [blame] | 1555 | auto getImageIndexIfTextureAttachment = [](const FramebufferAttachment &attachment) { |
| 1556 | if (attachment.type() == GL_TEXTURE) |
| 1557 | { |
| 1558 | return attachment.getTextureImageIndex(); |
| 1559 | } |
| 1560 | else |
| 1561 | { |
| 1562 | return ImageIndex::MakeInvalid(); |
| 1563 | } |
| 1564 | }; |
| 1565 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1566 | if (mState.mWebGLDepthAttachment.isAttached()) |
| 1567 | { |
| 1568 | const auto &depth = mState.mWebGLDepthAttachment; |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1569 | setAttachmentImpl(context, depth.type(), GL_DEPTH_ATTACHMENT, |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1570 | getImageIndexIfTextureAttachment(depth), depth.getResource(), numViews, |
| 1571 | baseViewIndex, multiviewLayout, viewportOffsets); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1572 | setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(), |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1573 | nullptr, numViews, baseViewIndex, multiviewLayout, viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1574 | } |
| 1575 | else if (mState.mWebGLStencilAttachment.isAttached()) |
| 1576 | { |
| 1577 | const auto &stencil = mState.mWebGLStencilAttachment; |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1578 | setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr, |
| 1579 | numViews, baseViewIndex, multiviewLayout, viewportOffsets); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1580 | setAttachmentImpl(context, stencil.type(), GL_STENCIL_ATTACHMENT, |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1581 | getImageIndexIfTextureAttachment(stencil), stencil.getResource(), |
| 1582 | numViews, baseViewIndex, multiviewLayout, viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1583 | } |
| 1584 | else if (mState.mWebGLDepthStencilAttachment.isAttached()) |
| 1585 | { |
| 1586 | const auto &depthStencil = mState.mWebGLDepthStencilAttachment; |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1587 | setAttachmentImpl(context, depthStencil.type(), GL_DEPTH_ATTACHMENT, |
Geoff Lang | e466c55 | 2017-03-17 15:24:12 -0400 | [diff] [blame] | 1588 | getImageIndexIfTextureAttachment(depthStencil), |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1589 | depthStencil.getResource(), numViews, baseViewIndex, multiviewLayout, |
| 1590 | viewportOffsets); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1591 | setAttachmentImpl(context, depthStencil.type(), GL_STENCIL_ATTACHMENT, |
Geoff Lang | e466c55 | 2017-03-17 15:24:12 -0400 | [diff] [blame] | 1592 | getImageIndexIfTextureAttachment(depthStencil), |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1593 | depthStencil.getResource(), numViews, baseViewIndex, multiviewLayout, |
| 1594 | viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1595 | } |
| 1596 | else |
| 1597 | { |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1598 | setAttachmentImpl(context, GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr, |
| 1599 | numViews, baseViewIndex, multiviewLayout, viewportOffsets); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1600 | setAttachmentImpl(context, GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(), |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1601 | nullptr, numViews, baseViewIndex, multiviewLayout, viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1602 | } |
| 1603 | } |
| 1604 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1605 | void Framebuffer::setAttachmentImpl(const Context *context, |
| 1606 | GLenum type, |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1607 | GLenum binding, |
| 1608 | const ImageIndex &textureIndex, |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1609 | FramebufferAttachmentObject *resource, |
| 1610 | GLsizei numViews, |
| 1611 | GLuint baseViewIndex, |
| 1612 | GLenum multiviewLayout, |
| 1613 | const GLint *viewportOffsets) |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1614 | { |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1615 | switch (binding) |
| 1616 | { |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1617 | case GL_DEPTH_STENCIL: |
| 1618 | case GL_DEPTH_STENCIL_ATTACHMENT: |
| 1619 | { |
| 1620 | // ensure this is a legitimate depth+stencil format |
| 1621 | FramebufferAttachmentObject *attachmentObj = resource; |
| 1622 | if (resource) |
| 1623 | { |
Jamie Madill | 4fd95d5 | 2017-04-05 11:22:18 -0400 | [diff] [blame] | 1624 | const Format &format = resource->getAttachmentFormat(binding, textureIndex); |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1625 | if (format.info->depthBits == 0 || format.info->stencilBits == 0) |
| 1626 | { |
| 1627 | // Attaching nullptr detaches the current attachment. |
| 1628 | attachmentObj = nullptr; |
| 1629 | } |
| 1630 | } |
| 1631 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1632 | updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT, |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1633 | &mDirtyDepthAttachmentBinding, type, binding, textureIndex, |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1634 | attachmentObj, numViews, baseViewIndex, multiviewLayout, |
| 1635 | viewportOffsets); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1636 | updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT, |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1637 | &mDirtyStencilAttachmentBinding, type, binding, textureIndex, |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1638 | attachmentObj, numViews, baseViewIndex, multiviewLayout, |
| 1639 | viewportOffsets); |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1640 | return; |
| 1641 | } |
| 1642 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1643 | case GL_DEPTH: |
| 1644 | case GL_DEPTH_ATTACHMENT: |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1645 | updateAttachment(context, &mState.mDepthAttachment, DIRTY_BIT_DEPTH_ATTACHMENT, |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1646 | &mDirtyDepthAttachmentBinding, type, binding, textureIndex, resource, |
| 1647 | numViews, baseViewIndex, multiviewLayout, viewportOffsets); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1648 | break; |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1649 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1650 | case GL_STENCIL: |
| 1651 | case GL_STENCIL_ATTACHMENT: |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1652 | updateAttachment(context, &mState.mStencilAttachment, DIRTY_BIT_STENCIL_ATTACHMENT, |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1653 | &mDirtyStencilAttachmentBinding, type, binding, textureIndex, resource, |
| 1654 | numViews, baseViewIndex, multiviewLayout, viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1655 | break; |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1656 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1657 | case GL_BACK: |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1658 | mState.mColorAttachments[0].attach(context, type, binding, textureIndex, resource, |
| 1659 | numViews, baseViewIndex, multiviewLayout, |
| 1660 | viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1661 | mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0); |
| 1662 | // No need for a resource binding for the default FBO, it's always complete. |
| 1663 | break; |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1664 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1665 | default: |
| 1666 | { |
| 1667 | size_t colorIndex = binding - GL_COLOR_ATTACHMENT0; |
| 1668 | ASSERT(colorIndex < mState.mColorAttachments.size()); |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1669 | size_t dirtyBit = DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex; |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1670 | updateAttachment(context, &mState.mColorAttachments[colorIndex], dirtyBit, |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1671 | &mDirtyColorAttachmentBindings[colorIndex], type, binding, |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1672 | textureIndex, resource, numViews, baseViewIndex, multiviewLayout, |
| 1673 | viewportOffsets); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1674 | |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 1675 | // TODO(jmadill): ASSERT instead of checking the attachment exists in |
| 1676 | // formsRenderingFeedbackLoopWith |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1677 | bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE); |
| 1678 | mState.mEnabledDrawBuffers.set(colorIndex, enabled); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1679 | } |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1680 | break; |
Geoff Lang | ab75a05 | 2014-10-15 12:56:37 -0400 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1684 | void Framebuffer::updateAttachment(const Context *context, |
| 1685 | FramebufferAttachment *attachment, |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1686 | size_t dirtyBit, |
| 1687 | OnAttachmentDirtyBinding *onDirtyBinding, |
| 1688 | GLenum type, |
| 1689 | GLenum binding, |
| 1690 | const ImageIndex &textureIndex, |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1691 | FramebufferAttachmentObject *resource, |
| 1692 | GLsizei numViews, |
| 1693 | GLuint baseViewIndex, |
| 1694 | GLenum multiviewLayout, |
| 1695 | const GLint *viewportOffsets) |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1696 | { |
Martin Radev | 5dae57b | 2017-07-14 16:15:55 +0300 | [diff] [blame] | 1697 | attachment->attach(context, type, binding, textureIndex, resource, numViews, baseViewIndex, |
| 1698 | multiviewLayout, viewportOffsets); |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1699 | mDirtyBits.set(dirtyBit); |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1700 | mState.mResourceNeedsInit.set(dirtyBit, attachment->initState() == InitState::MayNeedInit); |
Jamie Madill | b812669 | 2017-04-05 11:22:17 -0400 | [diff] [blame] | 1701 | BindResourceChannel(onDirtyBinding, resource); |
| 1702 | } |
| 1703 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1704 | void Framebuffer::resetAttachment(const Context *context, GLenum binding) |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1705 | { |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1706 | setAttachment(context, GL_NONE, binding, ImageIndex::MakeInvalid(), nullptr); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1707 | } |
| 1708 | |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 1709 | void Framebuffer::syncState(const Context *context) |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1710 | { |
| 1711 | if (mDirtyBits.any()) |
| 1712 | { |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 1713 | mImpl->syncState(context, mDirtyBits); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1714 | mDirtyBits.reset(); |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 1715 | if (mId != 0) |
| 1716 | { |
| 1717 | mCachedStatus.reset(); |
| 1718 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1719 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1720 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1721 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1722 | void Framebuffer::signal(size_t dirtyBit, InitState state) |
Jamie Madill | 51f40ec | 2016-06-15 14:06:00 -0400 | [diff] [blame] | 1723 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1724 | // TOOD(jmadill): Make this only update individual attachments to do less work. |
| 1725 | mCachedStatus.reset(); |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1726 | |
| 1727 | // Mark the appropriate init flag. |
| 1728 | mState.mResourceNeedsInit.set(dirtyBit, state == InitState::MayNeedInit); |
Jamie Madill | 51f40ec | 2016-06-15 14:06:00 -0400 | [diff] [blame] | 1729 | } |
| 1730 | |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 1731 | bool Framebuffer::complete(const Context *context) |
Jamie Madill | 51f40ec | 2016-06-15 14:06:00 -0400 | [diff] [blame] | 1732 | { |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 1733 | return (checkStatus(context) == GL_FRAMEBUFFER_COMPLETE); |
| 1734 | } |
| 1735 | |
| 1736 | bool Framebuffer::cachedComplete() const |
| 1737 | { |
| 1738 | return (mCachedStatus.valid() && mCachedStatus == GL_FRAMEBUFFER_COMPLETE); |
Jamie Madill | 51f40ec | 2016-06-15 14:06:00 -0400 | [diff] [blame] | 1739 | } |
| 1740 | |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 1741 | bool Framebuffer::formsRenderingFeedbackLoopWith(const State &state) const |
| 1742 | { |
| 1743 | const Program *program = state.getProgram(); |
| 1744 | |
| 1745 | // TODO(jmadill): Default framebuffer feedback loops. |
| 1746 | if (mId == 0) |
| 1747 | { |
| 1748 | return false; |
| 1749 | } |
| 1750 | |
| 1751 | // The bitset will skip inactive draw buffers. |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1752 | for (size_t drawIndex : mState.mEnabledDrawBuffers) |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 1753 | { |
Jamie Madill | 5aca193 | 2017-07-21 12:22:01 -0400 | [diff] [blame] | 1754 | const FramebufferAttachment &attachment = mState.mColorAttachments[drawIndex]; |
| 1755 | ASSERT(attachment.isAttached()); |
| 1756 | if (attachment.type() == GL_TEXTURE) |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 1757 | { |
| 1758 | // Validate the feedback loop. |
Jamie Madill | 5aca193 | 2017-07-21 12:22:01 -0400 | [diff] [blame] | 1759 | if (program->samplesFromTexture(state, attachment.id())) |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 1760 | { |
| 1761 | return true; |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | |
Jamie Madill | 1d37bc5 | 2017-02-02 19:59:58 -0500 | [diff] [blame] | 1766 | // Validate depth-stencil feedback loop. |
| 1767 | const auto &dsState = state.getDepthStencilState(); |
| 1768 | |
| 1769 | // We can skip the feedback loop checks if depth/stencil is masked out or disabled. |
| 1770 | const FramebufferAttachment *depth = getDepthbuffer(); |
| 1771 | if (depth && depth->type() == GL_TEXTURE && dsState.depthTest && dsState.depthMask) |
| 1772 | { |
| 1773 | if (program->samplesFromTexture(state, depth->id())) |
| 1774 | { |
| 1775 | return true; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | // Note: we assume the front and back masks are the same for WebGL. |
| 1780 | const FramebufferAttachment *stencil = getStencilbuffer(); |
| 1781 | ASSERT(dsState.stencilBackWritemask == dsState.stencilWritemask); |
| 1782 | if (stencil && stencil->type() == GL_TEXTURE && dsState.stencilTest && |
| 1783 | dsState.stencilWritemask != 0) |
| 1784 | { |
| 1785 | // Skip the feedback loop check if depth/stencil point to the same resource. |
| 1786 | if (!depth || *stencil != *depth) |
| 1787 | { |
| 1788 | if (program->samplesFromTexture(state, stencil->id())) |
| 1789 | { |
| 1790 | return true; |
| 1791 | } |
| 1792 | } |
| 1793 | } |
| 1794 | |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 1795 | return false; |
| 1796 | } |
| 1797 | |
Jamie Madill | fd3dd43 | 2017-02-02 19:59:59 -0500 | [diff] [blame] | 1798 | bool Framebuffer::formsCopyingFeedbackLoopWith(GLuint copyTextureID, |
| 1799 | GLint copyTextureLevel, |
| 1800 | GLint copyTextureLayer) const |
Jamie Madill | f695a3a | 2017-01-11 17:36:35 -0500 | [diff] [blame] | 1801 | { |
| 1802 | if (mId == 0) |
| 1803 | { |
| 1804 | // It seems impossible to form a texture copying feedback loop with the default FBO. |
| 1805 | return false; |
| 1806 | } |
| 1807 | |
| 1808 | const FramebufferAttachment *readAttachment = getReadColorbuffer(); |
| 1809 | ASSERT(readAttachment); |
| 1810 | |
| 1811 | if (readAttachment->isTextureWithId(copyTextureID)) |
| 1812 | { |
Jamie Madill | fd3dd43 | 2017-02-02 19:59:59 -0500 | [diff] [blame] | 1813 | const auto &imageIndex = readAttachment->getTextureImageIndex(); |
| 1814 | if (imageIndex.mipIndex == copyTextureLevel) |
Jamie Madill | f695a3a | 2017-01-11 17:36:35 -0500 | [diff] [blame] | 1815 | { |
Jamie Madill | fd3dd43 | 2017-02-02 19:59:59 -0500 | [diff] [blame] | 1816 | // Check 3D/Array texture layers. |
| 1817 | return imageIndex.layerIndex == ImageIndex::ENTIRE_LEVEL || |
| 1818 | copyTextureLayer == ImageIndex::ENTIRE_LEVEL || |
| 1819 | imageIndex.layerIndex == copyTextureLayer; |
Jamie Madill | f695a3a | 2017-01-11 17:36:35 -0500 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | return false; |
| 1823 | } |
| 1824 | |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 1825 | GLint Framebuffer::getDefaultWidth() const |
| 1826 | { |
| 1827 | return mState.getDefaultWidth(); |
| 1828 | } |
| 1829 | |
| 1830 | GLint Framebuffer::getDefaultHeight() const |
| 1831 | { |
| 1832 | return mState.getDefaultHeight(); |
| 1833 | } |
| 1834 | |
| 1835 | GLint Framebuffer::getDefaultSamples() const |
| 1836 | { |
| 1837 | return mState.getDefaultSamples(); |
| 1838 | } |
| 1839 | |
| 1840 | GLboolean Framebuffer::getDefaultFixedSampleLocations() const |
| 1841 | { |
| 1842 | return mState.getDefaultFixedSampleLocations(); |
| 1843 | } |
| 1844 | |
| 1845 | void Framebuffer::setDefaultWidth(GLint defaultWidth) |
| 1846 | { |
| 1847 | mState.mDefaultWidth = defaultWidth; |
| 1848 | mDirtyBits.set(DIRTY_BIT_DEFAULT_WIDTH); |
| 1849 | } |
| 1850 | |
| 1851 | void Framebuffer::setDefaultHeight(GLint defaultHeight) |
| 1852 | { |
| 1853 | mState.mDefaultHeight = defaultHeight; |
| 1854 | mDirtyBits.set(DIRTY_BIT_DEFAULT_HEIGHT); |
| 1855 | } |
| 1856 | |
| 1857 | void Framebuffer::setDefaultSamples(GLint defaultSamples) |
| 1858 | { |
| 1859 | mState.mDefaultSamples = defaultSamples; |
| 1860 | mDirtyBits.set(DIRTY_BIT_DEFAULT_SAMPLES); |
| 1861 | } |
| 1862 | |
| 1863 | void Framebuffer::setDefaultFixedSampleLocations(GLboolean defaultFixedSampleLocations) |
| 1864 | { |
| 1865 | mState.mDefaultFixedSampleLocations = defaultFixedSampleLocations; |
| 1866 | mDirtyBits.set(DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS); |
| 1867 | } |
| 1868 | |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 1869 | // TODO(jmadill): Remove this kludge. |
| 1870 | GLenum Framebuffer::checkStatus(const ValidationContext *context) |
| 1871 | { |
| 1872 | return checkStatus(static_cast<const Context *>(context)); |
| 1873 | } |
| 1874 | |
| 1875 | int Framebuffer::getSamples(const ValidationContext *context) |
| 1876 | { |
| 1877 | return getSamples(static_cast<const Context *>(context)); |
| 1878 | } |
| 1879 | |
Martin Radev | 14a26ae | 2017-07-24 15:56:29 +0300 | [diff] [blame] | 1880 | GLsizei Framebuffer::getNumViews() const |
| 1881 | { |
Martin Radev | 5c00d0d | 2017-08-07 10:06:59 +0300 | [diff] [blame] | 1882 | return mState.getNumViews(); |
Martin Radev | 14a26ae | 2017-07-24 15:56:29 +0300 | [diff] [blame] | 1883 | } |
| 1884 | |
Martin Radev | 4e619f5 | 2017-08-09 11:50:06 +0300 | [diff] [blame] | 1885 | GLint Framebuffer::getBaseViewIndex() const |
| 1886 | { |
| 1887 | return mState.getBaseViewIndex(); |
| 1888 | } |
| 1889 | |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 1890 | const std::vector<Offset> *Framebuffer::getViewportOffsets() const |
| 1891 | { |
Martin Radev | 5c00d0d | 2017-08-07 10:06:59 +0300 | [diff] [blame] | 1892 | return mState.getViewportOffsets(); |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | GLenum Framebuffer::getMultiviewLayout() const |
| 1896 | { |
Martin Radev | 5c00d0d | 2017-08-07 10:06:59 +0300 | [diff] [blame] | 1897 | return mState.getMultiviewLayout(); |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 1898 | } |
| 1899 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame^] | 1900 | Error Framebuffer::ensureDrawAttachmentsInitialized(const Context *context) |
| 1901 | { |
| 1902 | if (!context->isRobustResourceInitEnabled()) |
| 1903 | { |
| 1904 | return NoError(); |
| 1905 | } |
| 1906 | |
| 1907 | // Note: we don't actually filter by the draw attachment enum. Just init everything. |
| 1908 | for (size_t bit : mState.mResourceNeedsInit) |
| 1909 | { |
| 1910 | switch (bit) |
| 1911 | { |
| 1912 | case DIRTY_BIT_DEPTH_ATTACHMENT: |
| 1913 | ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment)); |
| 1914 | break; |
| 1915 | case DIRTY_BIT_STENCIL_ATTACHMENT: |
| 1916 | ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment)); |
| 1917 | break; |
| 1918 | default: |
| 1919 | ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bit])); |
| 1920 | break; |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | mState.mResourceNeedsInit.reset(); |
| 1925 | return NoError(); |
| 1926 | } |
| 1927 | |
| 1928 | Error Framebuffer::ensureReadAttachmentInitialized(const Context *context, GLbitfield blitMask) |
| 1929 | { |
| 1930 | if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none()) |
| 1931 | { |
| 1932 | return NoError(); |
| 1933 | } |
| 1934 | |
| 1935 | if ((blitMask & GL_COLOR_BUFFER_BIT) != 0 && mState.mReadBufferState != GL_NONE) |
| 1936 | { |
| 1937 | size_t readIndex = mState.getReadIndex(); |
| 1938 | if (mState.mResourceNeedsInit[readIndex]) |
| 1939 | { |
| 1940 | ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[readIndex])); |
| 1941 | mState.mResourceNeedsInit.reset(readIndex); |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | if ((blitMask & GL_DEPTH_BUFFER_BIT) != 0 && hasDepth()) |
| 1946 | { |
| 1947 | if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT]) |
| 1948 | { |
| 1949 | ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment)); |
| 1950 | mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT); |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | if ((blitMask & GL_STENCIL_BUFFER_BIT) != 0 && hasStencil()) |
| 1955 | { |
| 1956 | if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT]) |
| 1957 | { |
| 1958 | ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment)); |
| 1959 | mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT); |
| 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | return NoError(); |
| 1964 | } |
| 1965 | |
| 1966 | void Framebuffer::markDrawAttachmentsInitialized(bool color, bool depth, bool stencil) |
| 1967 | { |
| 1968 | // Mark attachments as initialized. |
| 1969 | if (color) |
| 1970 | { |
| 1971 | for (auto colorIndex : mState.mEnabledDrawBuffers) |
| 1972 | { |
| 1973 | auto &colorAttachment = mState.mColorAttachments[colorIndex]; |
| 1974 | ASSERT(colorAttachment.isAttached()); |
| 1975 | colorAttachment.setInitState(InitState::Initialized); |
| 1976 | mState.mResourceNeedsInit.reset(colorIndex); |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | if (depth && mState.mDepthAttachment.isAttached()) |
| 1981 | { |
| 1982 | mState.mDepthAttachment.setInitState(InitState::Initialized); |
| 1983 | mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT); |
| 1984 | } |
| 1985 | |
| 1986 | if (stencil && mState.mStencilAttachment.isAttached()) |
| 1987 | { |
| 1988 | mState.mStencilAttachment.setInitState(InitState::Initialized); |
| 1989 | mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT); |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | void Framebuffer::markBufferInitialized(GLenum bufferType, GLint bufferIndex) |
| 1994 | { |
| 1995 | switch (bufferType) |
| 1996 | { |
| 1997 | case GL_COLOR: |
| 1998 | { |
| 1999 | ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size())); |
| 2000 | if (mState.mColorAttachments[bufferIndex].isAttached()) |
| 2001 | { |
| 2002 | mState.mColorAttachments[bufferIndex].setInitState(InitState::Initialized); |
| 2003 | mState.mResourceNeedsInit.reset(bufferIndex); |
| 2004 | } |
| 2005 | break; |
| 2006 | } |
| 2007 | case GL_DEPTH: |
| 2008 | { |
| 2009 | if (mState.mDepthAttachment.isAttached()) |
| 2010 | { |
| 2011 | mState.mDepthAttachment.setInitState(InitState::Initialized); |
| 2012 | mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT); |
| 2013 | } |
| 2014 | break; |
| 2015 | } |
| 2016 | case GL_STENCIL: |
| 2017 | { |
| 2018 | if (mState.mStencilAttachment.isAttached()) |
| 2019 | { |
| 2020 | mState.mStencilAttachment.setInitState(InitState::Initialized); |
| 2021 | mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT); |
| 2022 | } |
| 2023 | break; |
| 2024 | } |
| 2025 | case GL_DEPTH_STENCIL: |
| 2026 | { |
| 2027 | if (mState.mDepthAttachment.isAttached()) |
| 2028 | { |
| 2029 | mState.mDepthAttachment.setInitState(InitState::Initialized); |
| 2030 | mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT); |
| 2031 | } |
| 2032 | if (mState.mStencilAttachment.isAttached()) |
| 2033 | { |
| 2034 | mState.mStencilAttachment.setInitState(InitState::Initialized); |
| 2035 | mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT); |
| 2036 | } |
| 2037 | break; |
| 2038 | } |
| 2039 | default: |
| 2040 | UNREACHABLE(); |
| 2041 | break; |
| 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | Box Framebuffer::getDimensions() const |
| 2046 | { |
| 2047 | return mState.getDimensions(); |
| 2048 | } |
| 2049 | |
| 2050 | Error Framebuffer::ensureBufferInitialized(const Context *context, |
| 2051 | GLenum bufferType, |
| 2052 | GLint bufferIndex) |
| 2053 | { |
| 2054 | ASSERT(context->isRobustResourceInitEnabled()); |
| 2055 | |
| 2056 | if (mState.mResourceNeedsInit.none()) |
| 2057 | { |
| 2058 | return NoError(); |
| 2059 | } |
| 2060 | |
| 2061 | switch (bufferType) |
| 2062 | { |
| 2063 | case GL_COLOR: |
| 2064 | { |
| 2065 | ASSERT(bufferIndex < static_cast<GLint>(mState.mColorAttachments.size())); |
| 2066 | if (mState.mResourceNeedsInit[bufferIndex]) |
| 2067 | { |
| 2068 | ANGLE_TRY(InitAttachment(context, &mState.mColorAttachments[bufferIndex])); |
| 2069 | mState.mResourceNeedsInit.reset(bufferIndex); |
| 2070 | } |
| 2071 | break; |
| 2072 | } |
| 2073 | case GL_DEPTH: |
| 2074 | { |
| 2075 | if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT]) |
| 2076 | { |
| 2077 | ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment)); |
| 2078 | mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT); |
| 2079 | } |
| 2080 | break; |
| 2081 | } |
| 2082 | case GL_STENCIL: |
| 2083 | { |
| 2084 | if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT]) |
| 2085 | { |
| 2086 | ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment)); |
| 2087 | mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT); |
| 2088 | } |
| 2089 | break; |
| 2090 | } |
| 2091 | case GL_DEPTH_STENCIL: |
| 2092 | { |
| 2093 | if (mState.mResourceNeedsInit[DIRTY_BIT_DEPTH_ATTACHMENT]) |
| 2094 | { |
| 2095 | ANGLE_TRY(InitAttachment(context, &mState.mDepthAttachment)); |
| 2096 | mState.mResourceNeedsInit.reset(DIRTY_BIT_DEPTH_ATTACHMENT); |
| 2097 | } |
| 2098 | if (mState.mResourceNeedsInit[DIRTY_BIT_STENCIL_ATTACHMENT]) |
| 2099 | { |
| 2100 | ANGLE_TRY(InitAttachment(context, &mState.mStencilAttachment)); |
| 2101 | mState.mResourceNeedsInit.reset(DIRTY_BIT_STENCIL_ATTACHMENT); |
| 2102 | } |
| 2103 | break; |
| 2104 | } |
| 2105 | default: |
| 2106 | UNREACHABLE(); |
| 2107 | break; |
| 2108 | } |
| 2109 | |
| 2110 | return NoError(); |
| 2111 | } |
| 2112 | |
| 2113 | bool Framebuffer::partialBufferClearNeedsInit(const Context *context, GLenum bufferType) |
| 2114 | { |
| 2115 | if (!context->isRobustResourceInitEnabled() || mState.mResourceNeedsInit.none()) |
| 2116 | { |
| 2117 | return false; |
| 2118 | } |
| 2119 | |
| 2120 | switch (bufferType) |
| 2121 | { |
| 2122 | case GL_COLOR: |
| 2123 | return partialClearNeedsInit(context, true, false, false); |
| 2124 | case GL_DEPTH: |
| 2125 | return partialClearNeedsInit(context, false, true, false); |
| 2126 | case GL_STENCIL: |
| 2127 | return partialClearNeedsInit(context, false, false, true); |
| 2128 | case GL_DEPTH_STENCIL: |
| 2129 | return partialClearNeedsInit(context, false, true, true); |
| 2130 | default: |
| 2131 | UNREACHABLE(); |
| 2132 | return false; |
| 2133 | } |
| 2134 | } |
| 2135 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 2136 | } // namespace gl |