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