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