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 | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 12 | #include "common/BitSetIterator.h" |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 13 | #include "common/Optional.h" |
Jamie Madill | c46f45d | 2015-03-31 13:20:55 -0400 | [diff] [blame] | 14 | #include "common/utilities.h" |
| 15 | #include "libANGLE/Config.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 16 | #include "libANGLE/Context.h" |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 17 | #include "libANGLE/Display.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 18 | #include "libANGLE/FramebufferAttachment.h" |
Jamie Madill | c46f45d | 2015-03-31 13:20:55 -0400 | [diff] [blame] | 19 | #include "libANGLE/Renderbuffer.h" |
| 20 | #include "libANGLE/Surface.h" |
| 21 | #include "libANGLE/Texture.h" |
| 22 | #include "libANGLE/formatutils.h" |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 23 | #include "libANGLE/renderer/ContextImpl.h" |
Geoff Lang | b5d8f23 | 2014-12-04 15:43:01 -0500 | [diff] [blame] | 24 | #include "libANGLE/renderer/FramebufferImpl.h" |
Jamie Madill | 7aea7e0 | 2016-05-10 10:39:45 -0400 | [diff] [blame] | 25 | #include "libANGLE/renderer/GLImplFactory.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 26 | #include "libANGLE/renderer/RenderbufferImpl.h" |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 27 | #include "libANGLE/renderer/SurfaceImpl.h" |
Geoff Lang | 0b7eef7 | 2014-06-12 14:10:47 -0400 | [diff] [blame] | 28 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 29 | using namespace angle; |
| 30 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 31 | namespace gl |
| 32 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 33 | |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 34 | namespace |
| 35 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 36 | |
| 37 | void BindResourceChannel(ChannelBinding *binding, FramebufferAttachmentObject *resource) |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 38 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 39 | binding->bind(resource ? resource->getDirtyChannel() : nullptr); |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 40 | } |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 41 | |
| 42 | } // anonymous namespace |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 43 | |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 44 | // This constructor is only used for default framebuffers. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 45 | FramebufferState::FramebufferState() |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 46 | : mLabel(), |
| 47 | mColorAttachments(1), |
Geoff Lang | d90d388 | 2017-03-21 10:49:54 -0400 | [diff] [blame] | 48 | mDrawBufferStates(IMPLEMENTATION_MAX_DRAW_BUFFERS, GL_NONE), |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 49 | mReadBufferState(GL_BACK), |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 50 | mDefaultWidth(0), |
| 51 | mDefaultHeight(0), |
| 52 | mDefaultSamples(0), |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 53 | mDefaultFixedSampleLocations(GL_FALSE), |
| 54 | mWebGLDepthStencilConsistent(true) |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 55 | { |
Geoff Lang | d90d388 | 2017-03-21 10:49:54 -0400 | [diff] [blame] | 56 | ASSERT(mDrawBufferStates.size() > 0); |
| 57 | mDrawBufferStates[0] = GL_BACK; |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 58 | mEnabledDrawBuffers.set(0); |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 61 | FramebufferState::FramebufferState(const Caps &caps) |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 62 | : mLabel(), |
| 63 | mColorAttachments(caps.maxColorAttachments), |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 64 | mDrawBufferStates(caps.maxDrawBuffers, GL_NONE), |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 65 | mReadBufferState(GL_COLOR_ATTACHMENT0_EXT), |
| 66 | mDefaultWidth(0), |
| 67 | mDefaultHeight(0), |
| 68 | mDefaultSamples(0), |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 69 | mDefaultFixedSampleLocations(GL_FALSE), |
| 70 | mWebGLDepthStencilConsistent(true) |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 71 | { |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 72 | ASSERT(mDrawBufferStates.size() > 0); |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 73 | mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT; |
| 74 | } |
| 75 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 76 | FramebufferState::~FramebufferState() |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 77 | { |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 80 | const std::string &FramebufferState::getLabel() |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 81 | { |
| 82 | return mLabel; |
| 83 | } |
| 84 | |
Geoff Lang | 4b7f12b | 2016-06-21 16:47:07 -0400 | [diff] [blame] | 85 | const FramebufferAttachment *FramebufferState::getAttachment(GLenum attachment) const |
| 86 | { |
| 87 | if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15) |
| 88 | { |
| 89 | return getColorAttachment(attachment - GL_COLOR_ATTACHMENT0); |
| 90 | } |
| 91 | |
| 92 | switch (attachment) |
| 93 | { |
| 94 | case GL_COLOR: |
| 95 | case GL_BACK: |
| 96 | return getColorAttachment(0); |
| 97 | case GL_DEPTH: |
| 98 | case GL_DEPTH_ATTACHMENT: |
| 99 | return getDepthAttachment(); |
| 100 | case GL_STENCIL: |
| 101 | case GL_STENCIL_ATTACHMENT: |
| 102 | return getStencilAttachment(); |
| 103 | case GL_DEPTH_STENCIL: |
| 104 | case GL_DEPTH_STENCIL_ATTACHMENT: |
| 105 | return getDepthStencilAttachment(); |
| 106 | default: |
| 107 | UNREACHABLE(); |
| 108 | return nullptr; |
| 109 | } |
| 110 | } |
| 111 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 112 | const FramebufferAttachment *FramebufferState::getReadAttachment() const |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 113 | { |
Antoine Labour | 2ec65dc | 2016-11-30 16:28:58 -0800 | [diff] [blame] | 114 | if (mReadBufferState == GL_NONE) |
| 115 | { |
| 116 | return nullptr; |
| 117 | } |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 118 | ASSERT(mReadBufferState == GL_BACK || (mReadBufferState >= GL_COLOR_ATTACHMENT0 && mReadBufferState <= GL_COLOR_ATTACHMENT15)); |
| 119 | size_t readIndex = (mReadBufferState == GL_BACK ? 0 : static_cast<size_t>(mReadBufferState - GL_COLOR_ATTACHMENT0)); |
| 120 | ASSERT(readIndex < mColorAttachments.size()); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 121 | return mColorAttachments[readIndex].isAttached() ? &mColorAttachments[readIndex] : nullptr; |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 124 | const FramebufferAttachment *FramebufferState::getFirstNonNullAttachment() const |
| 125 | { |
| 126 | auto *colorAttachment = getFirstColorAttachment(); |
| 127 | if (colorAttachment) |
| 128 | { |
| 129 | return colorAttachment; |
| 130 | } |
| 131 | return getDepthOrStencilAttachment(); |
| 132 | } |
| 133 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 134 | const FramebufferAttachment *FramebufferState::getFirstColorAttachment() const |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 135 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 136 | for (const FramebufferAttachment &colorAttachment : mColorAttachments) |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 137 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 138 | if (colorAttachment.isAttached()) |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 139 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 140 | return &colorAttachment; |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | return nullptr; |
| 145 | } |
| 146 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 147 | const FramebufferAttachment *FramebufferState::getDepthOrStencilAttachment() const |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 148 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 149 | if (mDepthAttachment.isAttached()) |
| 150 | { |
| 151 | return &mDepthAttachment; |
| 152 | } |
| 153 | if (mStencilAttachment.isAttached()) |
| 154 | { |
| 155 | return &mStencilAttachment; |
| 156 | } |
| 157 | return nullptr; |
Jamie Madill | 7147f01 | 2015-03-05 15:41:40 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Corentin Wallez | b1d0a255 | 2016-12-19 16:15:54 -0500 | [diff] [blame] | 160 | const FramebufferAttachment *FramebufferState::getStencilOrDepthStencilAttachment() const |
| 161 | { |
| 162 | if (mStencilAttachment.isAttached()) |
| 163 | { |
| 164 | return &mStencilAttachment; |
| 165 | } |
| 166 | return getDepthStencilAttachment(); |
| 167 | } |
| 168 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 169 | const FramebufferAttachment *FramebufferState::getColorAttachment(size_t colorAttachment) const |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 170 | { |
| 171 | ASSERT(colorAttachment < mColorAttachments.size()); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 172 | return mColorAttachments[colorAttachment].isAttached() ? |
| 173 | &mColorAttachments[colorAttachment] : |
| 174 | nullptr; |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 177 | const FramebufferAttachment *FramebufferState::getDepthAttachment() const |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 178 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 179 | return mDepthAttachment.isAttached() ? &mDepthAttachment : nullptr; |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 182 | const FramebufferAttachment *FramebufferState::getStencilAttachment() const |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 183 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 184 | return mStencilAttachment.isAttached() ? &mStencilAttachment : nullptr; |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 187 | const FramebufferAttachment *FramebufferState::getDepthStencilAttachment() const |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 188 | { |
| 189 | // A valid depth-stencil attachment has the same resource bound to both the |
| 190 | // depth and stencil attachment points. |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 191 | if (mDepthAttachment.isAttached() && mStencilAttachment.isAttached() && |
Jamie Madill | 44f2648 | 2016-11-18 12:49:15 -0500 | [diff] [blame] | 192 | mDepthAttachment == mStencilAttachment) |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 193 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 194 | return &mDepthAttachment; |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return nullptr; |
| 198 | } |
| 199 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 200 | bool FramebufferState::attachmentsHaveSameDimensions() const |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 201 | { |
| 202 | Optional<Extents> attachmentSize; |
| 203 | |
| 204 | auto hasMismatchedSize = [&attachmentSize](const FramebufferAttachment &attachment) |
| 205 | { |
| 206 | if (!attachment.isAttached()) |
| 207 | { |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | if (!attachmentSize.valid()) |
| 212 | { |
| 213 | attachmentSize = attachment.getSize(); |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | return (attachment.getSize() != attachmentSize.value()); |
| 218 | }; |
| 219 | |
| 220 | for (const auto &attachment : mColorAttachments) |
| 221 | { |
| 222 | if (hasMismatchedSize(attachment)) |
| 223 | { |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if (hasMismatchedSize(mDepthAttachment)) |
| 229 | { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | return !hasMismatchedSize(mStencilAttachment); |
| 234 | } |
| 235 | |
Geoff Lang | 4b7f12b | 2016-06-21 16:47:07 -0400 | [diff] [blame] | 236 | const gl::FramebufferAttachment *FramebufferState::getDrawBuffer(size_t drawBufferIdx) const |
| 237 | { |
| 238 | ASSERT(drawBufferIdx < mDrawBufferStates.size()); |
| 239 | if (mDrawBufferStates[drawBufferIdx] != GL_NONE) |
| 240 | { |
| 241 | // ES3 spec: "If the GL is bound to a draw framebuffer object, the ith buffer listed in bufs |
| 242 | // must be COLOR_ATTACHMENTi or NONE" |
| 243 | ASSERT(mDrawBufferStates[drawBufferIdx] == GL_COLOR_ATTACHMENT0 + drawBufferIdx || |
| 244 | (drawBufferIdx == 0 && mDrawBufferStates[drawBufferIdx] == GL_BACK)); |
| 245 | return getAttachment(mDrawBufferStates[drawBufferIdx]); |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | return nullptr; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | size_t FramebufferState::getDrawBufferCount() const |
| 254 | { |
| 255 | return mDrawBufferStates.size(); |
| 256 | } |
| 257 | |
Geoff Lang | b21e20d | 2016-07-19 15:35:41 -0400 | [diff] [blame] | 258 | bool FramebufferState::colorAttachmentsAreUniqueImages() const |
| 259 | { |
| 260 | for (size_t firstAttachmentIdx = 0; firstAttachmentIdx < mColorAttachments.size(); |
| 261 | firstAttachmentIdx++) |
| 262 | { |
| 263 | const gl::FramebufferAttachment &firstAttachment = mColorAttachments[firstAttachmentIdx]; |
| 264 | if (!firstAttachment.isAttached()) |
| 265 | { |
| 266 | continue; |
| 267 | } |
| 268 | |
| 269 | for (size_t secondAttachmentIdx = firstAttachmentIdx + 1; |
| 270 | secondAttachmentIdx < mColorAttachments.size(); secondAttachmentIdx++) |
| 271 | { |
| 272 | const gl::FramebufferAttachment &secondAttachment = |
| 273 | mColorAttachments[secondAttachmentIdx]; |
| 274 | if (!secondAttachment.isAttached()) |
| 275 | { |
| 276 | continue; |
| 277 | } |
| 278 | |
| 279 | if (firstAttachment == secondAttachment) |
| 280 | { |
| 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return true; |
| 287 | } |
| 288 | |
Jamie Madill | 7aea7e0 | 2016-05-10 10:39:45 -0400 | [diff] [blame] | 289 | Framebuffer::Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id) |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 290 | : mState(caps), |
| 291 | mImpl(factory->createFramebuffer(mState)), |
| 292 | mId(id), |
| 293 | mCachedStatus(), |
| 294 | mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT), |
| 295 | mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 296 | { |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 297 | ASSERT(mId != 0); |
| 298 | ASSERT(mImpl != nullptr); |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 299 | ASSERT(mState.mColorAttachments.size() == static_cast<size_t>(caps.maxColorAttachments)); |
| 300 | |
| 301 | for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex) |
| 302 | { |
| 303 | mDirtyColorAttachmentBindings.push_back(ChannelBinding( |
| 304 | this, static_cast<SignalToken>(DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex))); |
| 305 | } |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 306 | } |
| 307 | |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 308 | Framebuffer::Framebuffer(egl::Surface *surface) |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 309 | : mState(), |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 310 | mImpl(surface->getImplementation()->createDefaultFramebuffer(mState)), |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 311 | mId(0), |
| 312 | mCachedStatus(GL_FRAMEBUFFER_COMPLETE), |
| 313 | mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT), |
| 314 | mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT) |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 315 | { |
Geoff Lang | da88add | 2014-12-01 10:22:01 -0500 | [diff] [blame] | 316 | ASSERT(mImpl != nullptr); |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 317 | mDirtyColorAttachmentBindings.push_back( |
| 318 | ChannelBinding(this, static_cast<SignalToken>(DIRTY_BIT_COLOR_ATTACHMENT_0))); |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 319 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 320 | setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_BACK, gl::ImageIndex::MakeInvalid(), surface); |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 321 | |
| 322 | if (surface->getConfig()->depthSize > 0) |
| 323 | { |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 324 | setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, gl::ImageIndex::MakeInvalid(), surface); |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (surface->getConfig()->stencilSize > 0) |
| 328 | { |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 329 | setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, gl::ImageIndex::MakeInvalid(), |
| 330 | surface); |
Jamie Madill | 6f60d05 | 2017-02-22 15:20:11 -0500 | [diff] [blame] | 331 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 334 | Framebuffer::Framebuffer(rx::GLImplFactory *factory) |
| 335 | : mState(), |
| 336 | mImpl(factory->createFramebuffer(mState)), |
| 337 | mId(0), |
| 338 | mCachedStatus(GL_FRAMEBUFFER_UNDEFINED_OES), |
| 339 | mDirtyDepthAttachmentBinding(this, DIRTY_BIT_DEPTH_ATTACHMENT), |
| 340 | mDirtyStencilAttachmentBinding(this, DIRTY_BIT_STENCIL_ATTACHMENT) |
| 341 | { |
| 342 | mDirtyColorAttachmentBindings.push_back( |
| 343 | ChannelBinding(this, static_cast<SignalToken>(DIRTY_BIT_COLOR_ATTACHMENT_0))); |
| 344 | } |
| 345 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 346 | Framebuffer::~Framebuffer() |
| 347 | { |
Geoff Lang | da88add | 2014-12-01 10:22:01 -0500 | [diff] [blame] | 348 | SafeDelete(mImpl); |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 351 | void Framebuffer::destroy(const Context *context) |
| 352 | { |
| 353 | mImpl->destroy(rx::SafeGetImpl(context)); |
| 354 | } |
| 355 | |
| 356 | void Framebuffer::destroyDefault(const egl::Display *display) |
| 357 | { |
| 358 | mImpl->destroyDefault(rx::SafeGetImpl(display)); |
| 359 | } |
| 360 | |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 361 | void Framebuffer::setLabel(const std::string &label) |
| 362 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 363 | mState.mLabel = label; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | const std::string &Framebuffer::getLabel() const |
| 367 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 368 | return mState.mLabel; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 369 | } |
| 370 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 371 | void Framebuffer::detachTexture(const Context *context, GLuint textureId) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 372 | { |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 373 | detachResourceById(context, GL_TEXTURE, textureId); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 376 | void Framebuffer::detachRenderbuffer(const Context *context, GLuint renderbufferId) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 377 | { |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 378 | detachResourceById(context, GL_RENDERBUFFER, renderbufferId); |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 379 | } |
Jamie Madill | e261b44 | 2014-06-25 12:42:21 -0400 | [diff] [blame] | 380 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 381 | void Framebuffer::detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId) |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 382 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 383 | for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex) |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 384 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 385 | detachMatchingAttachment(&mState.mColorAttachments[colorIndex], resourceType, resourceId, |
| 386 | DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 389 | if (context->isWebGL1()) |
| 390 | { |
| 391 | const std::array<FramebufferAttachment *, 3> attachments = { |
| 392 | {&mState.mWebGLDepthStencilAttachment, &mState.mWebGLDepthAttachment, |
| 393 | &mState.mWebGLStencilAttachment}}; |
| 394 | for (FramebufferAttachment *attachment : attachments) |
| 395 | { |
| 396 | if (attachment->isAttached() && attachment->type() == resourceType && |
| 397 | attachment->id() == resourceId) |
| 398 | { |
| 399 | resetAttachment(context, attachment->getBinding()); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | detachMatchingAttachment(&mState.mDepthAttachment, resourceType, resourceId, |
| 406 | DIRTY_BIT_DEPTH_ATTACHMENT); |
| 407 | detachMatchingAttachment(&mState.mStencilAttachment, resourceType, resourceId, |
| 408 | DIRTY_BIT_STENCIL_ATTACHMENT); |
| 409 | } |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void Framebuffer::detachMatchingAttachment(FramebufferAttachment *attachment, |
| 413 | GLenum matchType, |
| 414 | GLuint matchId, |
| 415 | size_t dirtyBit) |
| 416 | { |
| 417 | if (attachment->isAttached() && attachment->type() == matchType && attachment->id() == matchId) |
| 418 | { |
| 419 | attachment->detach(); |
| 420 | mDirtyBits.set(dirtyBit); |
| 421 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 424 | const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 425 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 426 | return mState.getColorAttachment(colorAttachment); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 429 | const FramebufferAttachment *Framebuffer::getDepthbuffer() const |
Geoff Lang | 646559f | 2013-08-15 11:08:15 -0400 | [diff] [blame] | 430 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 431 | return mState.getDepthAttachment(); |
Geoff Lang | 646559f | 2013-08-15 11:08:15 -0400 | [diff] [blame] | 432 | } |
| 433 | |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 434 | const FramebufferAttachment *Framebuffer::getStencilbuffer() const |
| 435 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 436 | return mState.getStencilAttachment(); |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 437 | } |
| 438 | |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 439 | const FramebufferAttachment *Framebuffer::getDepthStencilBuffer() const |
| 440 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 441 | return mState.getDepthStencilAttachment(); |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | const FramebufferAttachment *Framebuffer::getDepthOrStencilbuffer() const |
daniel@transgaming.com | d2b4702 | 2012-11-28 19:40:10 +0000 | [diff] [blame] | 445 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 446 | return mState.getDepthOrStencilAttachment(); |
daniel@transgaming.com | d2b4702 | 2012-11-28 19:40:10 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Corentin Wallez | b1d0a255 | 2016-12-19 16:15:54 -0500 | [diff] [blame] | 449 | const FramebufferAttachment *Framebuffer::getStencilOrDepthStencilAttachment() const |
| 450 | { |
| 451 | return mState.getStencilOrDepthStencilAttachment(); |
| 452 | } |
| 453 | |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 454 | const FramebufferAttachment *Framebuffer::getReadColorbuffer() const |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 455 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 456 | return mState.getReadAttachment(); |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 457 | } |
| 458 | |
shannon.woods%transgaming.com@gtempaccount.com | f6863e0 | 2013-04-13 03:34:00 +0000 | [diff] [blame] | 459 | GLenum Framebuffer::getReadColorbufferType() const |
| 460 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 461 | const FramebufferAttachment *readAttachment = mState.getReadAttachment(); |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 462 | return (readAttachment != nullptr ? readAttachment->type() : GL_NONE); |
shannon.woods%transgaming.com@gtempaccount.com | f6863e0 | 2013-04-13 03:34:00 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Jamie Madill | b6bda4a | 2015-04-20 12:53:26 -0400 | [diff] [blame] | 465 | const FramebufferAttachment *Framebuffer::getFirstColorbuffer() const |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 466 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 467 | return mState.getFirstColorAttachment(); |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 470 | const FramebufferAttachment *Framebuffer::getAttachment(GLenum attachment) const |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 471 | { |
Geoff Lang | 4b7f12b | 2016-06-21 16:47:07 -0400 | [diff] [blame] | 472 | return mState.getAttachment(attachment); |
Geoff Lang | 55ba29c | 2013-07-11 16:57:53 -0400 | [diff] [blame] | 473 | } |
| 474 | |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 475 | size_t Framebuffer::getDrawbufferStateCount() const |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 476 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 477 | return mState.mDrawBufferStates.size(); |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | GLenum Framebuffer::getDrawBufferState(size_t drawBuffer) const |
| 481 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 482 | ASSERT(drawBuffer < mState.mDrawBufferStates.size()); |
| 483 | return mState.mDrawBufferStates[drawBuffer]; |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Jamie Madill | 1fbc59f | 2016-02-24 15:25:51 -0500 | [diff] [blame] | 486 | const std::vector<GLenum> &Framebuffer::getDrawBufferStates() const |
| 487 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 488 | return mState.getDrawBufferStates(); |
Jamie Madill | 1fbc59f | 2016-02-24 15:25:51 -0500 | [diff] [blame] | 489 | } |
| 490 | |
Geoff Lang | 164d54e | 2014-12-01 10:55:33 -0500 | [diff] [blame] | 491 | 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] | 492 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 493 | auto &drawStates = mState.mDrawBufferStates; |
Jamie Madill | d1405e5 | 2015-03-05 15:41:39 -0500 | [diff] [blame] | 494 | |
| 495 | ASSERT(count <= drawStates.size()); |
| 496 | std::copy(buffers, buffers + count, drawStates.begin()); |
| 497 | std::fill(drawStates.begin() + count, drawStates.end(), GL_NONE); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 498 | mDirtyBits.set(DIRTY_BIT_DRAW_BUFFERS); |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 499 | |
| 500 | mState.mEnabledDrawBuffers.reset(); |
| 501 | for (size_t index = 0; index < count; ++index) |
| 502 | { |
| 503 | if (drawStates[index] != GL_NONE && mState.mColorAttachments[index].isAttached()) |
| 504 | { |
| 505 | mState.mEnabledDrawBuffers.set(index); |
| 506 | } |
| 507 | } |
Geoff Lang | 9dd9580 | 2014-12-01 11:12:59 -0500 | [diff] [blame] | 508 | } |
| 509 | |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 510 | const FramebufferAttachment *Framebuffer::getDrawBuffer(size_t drawBuffer) const |
| 511 | { |
Geoff Lang | 4b7f12b | 2016-06-21 16:47:07 -0400 | [diff] [blame] | 512 | return mState.getDrawBuffer(drawBuffer); |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | bool Framebuffer::hasEnabledDrawBuffer() const |
| 516 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 517 | for (size_t drawbufferIdx = 0; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx) |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 518 | { |
| 519 | if (getDrawBuffer(drawbufferIdx) != nullptr) |
| 520 | { |
| 521 | return true; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | return false; |
| 526 | } |
| 527 | |
Geoff Lang | 9dd9580 | 2014-12-01 11:12:59 -0500 | [diff] [blame] | 528 | GLenum Framebuffer::getReadBufferState() const |
| 529 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 530 | return mState.mReadBufferState; |
Geoff Lang | 9dd9580 | 2014-12-01 11:12:59 -0500 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void Framebuffer::setReadBuffer(GLenum buffer) |
| 534 | { |
Jamie Madill | b885e57 | 2015-02-03 16:16:04 -0500 | [diff] [blame] | 535 | ASSERT(buffer == GL_BACK || buffer == GL_NONE || |
| 536 | (buffer >= GL_COLOR_ATTACHMENT0 && |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 537 | (buffer - GL_COLOR_ATTACHMENT0) < mState.mColorAttachments.size())); |
| 538 | mState.mReadBufferState = buffer; |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 539 | mDirtyBits.set(DIRTY_BIT_READ_BUFFER); |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 542 | size_t Framebuffer::getNumColorBuffers() const |
| 543 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 544 | return mState.mColorAttachments.size(); |
Corentin Wallez | 37c3979 | 2015-08-20 14:19:46 -0400 | [diff] [blame] | 545 | } |
| 546 | |
Jamie Madill | 0df8fe4 | 2015-11-24 16:10:24 -0500 | [diff] [blame] | 547 | bool Framebuffer::hasDepth() const |
| 548 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 549 | return (mState.mDepthAttachment.isAttached() && mState.mDepthAttachment.getDepthSize() > 0); |
Jamie Madill | 0df8fe4 | 2015-11-24 16:10:24 -0500 | [diff] [blame] | 550 | } |
| 551 | |
shannon.woods%transgaming.com@gtempaccount.com | 3b57b4f | 2013-04-13 03:28:29 +0000 | [diff] [blame] | 552 | bool Framebuffer::hasStencil() const |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 553 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 554 | return (mState.mStencilAttachment.isAttached() && |
| 555 | mState.mStencilAttachment.getStencilSize() > 0); |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 556 | } |
| 557 | |
shannonwoods@chromium.org | 24ac850 | 2013-05-30 00:01:37 +0000 | [diff] [blame] | 558 | bool Framebuffer::usingExtendedDrawBuffers() const |
| 559 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 560 | for (size_t drawbufferIdx = 1; drawbufferIdx < mState.mDrawBufferStates.size(); ++drawbufferIdx) |
shannonwoods@chromium.org | 24ac850 | 2013-05-30 00:01:37 +0000 | [diff] [blame] | 561 | { |
Geoff Lang | a15472a | 2015-08-11 11:48:03 -0400 | [diff] [blame] | 562 | if (getDrawBuffer(drawbufferIdx) != nullptr) |
shannonwoods@chromium.org | 24ac850 | 2013-05-30 00:01:37 +0000 | [diff] [blame] | 563 | { |
| 564 | return true; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | return false; |
| 569 | } |
| 570 | |
Jamie Madill | 51f40ec | 2016-06-15 14:06:00 -0400 | [diff] [blame] | 571 | GLenum Framebuffer::checkStatus(const ContextState &state) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 572 | { |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 573 | // The default framebuffer is always complete except when it is surfaceless in which |
| 574 | // case it is always unsupported. We return early because the default framebuffer may |
| 575 | // not be subject to the same rules as application FBOs. ie, it could have 0x0 size. |
Geoff Lang | 528ce3c | 2014-12-01 10:44:07 -0500 | [diff] [blame] | 576 | if (mId == 0) |
| 577 | { |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 578 | ASSERT(mCachedStatus.valid()); |
| 579 | ASSERT(mCachedStatus.value() == GL_FRAMEBUFFER_COMPLETE || |
| 580 | mCachedStatus.value() == GL_FRAMEBUFFER_UNDEFINED_OES); |
| 581 | return mCachedStatus.value(); |
Geoff Lang | 528ce3c | 2014-12-01 10:44:07 -0500 | [diff] [blame] | 582 | } |
| 583 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 584 | if (hasAnyDirtyBit() || !mCachedStatus.valid()) |
| 585 | { |
| 586 | mCachedStatus = checkStatusImpl(state); |
| 587 | } |
| 588 | |
| 589 | return mCachedStatus.value(); |
| 590 | } |
| 591 | |
| 592 | GLenum Framebuffer::checkStatusImpl(const ContextState &state) |
| 593 | { |
| 594 | ASSERT(mId != 0); |
| 595 | |
shannonwoods@chromium.org | f6fb959 | 2013-05-30 00:09:40 +0000 | [diff] [blame] | 596 | unsigned int colorbufferSize = 0; |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 597 | int samples = -1; |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 598 | bool missingAttachment = true; |
JiangYizhou | 461d9a3 | 2017-01-04 16:37:26 +0800 | [diff] [blame] | 599 | Optional<GLboolean> fixedSampleLocations; |
| 600 | bool hasRenderbuffer = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 601 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 602 | for (const FramebufferAttachment &colorAttachment : mState.mColorAttachments) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 603 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 604 | if (colorAttachment.isAttached()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 605 | { |
Jamie Madill | 6b120b9 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 606 | const Extents &size = colorAttachment.getSize(); |
| 607 | if (size.width == 0 || size.height == 0) |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 608 | { |
| 609 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 610 | } |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame] | 611 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 612 | const Format &format = colorAttachment.getFormat(); |
| 613 | const TextureCaps &formatCaps = state.getTextureCap(format.asSized()); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 614 | if (colorAttachment.type() == GL_TEXTURE) |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 615 | { |
Geoff Lang | 6cf8e1b | 2014-07-03 13:03:57 -0400 | [diff] [blame] | 616 | if (!formatCaps.renderable) |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 617 | { |
Jamie Madill | 8117678 | 2015-11-24 16:10:23 -0500 | [diff] [blame] | 618 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 621 | if (format.info->depthBits > 0 || format.info->stencilBits > 0) |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 622 | { |
| 623 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 624 | } |
Jamie Madill | 6b120b9 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 625 | |
| 626 | if (colorAttachment.layer() >= size.depth) |
| 627 | { |
| 628 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 629 | } |
Jamie Madill | 3215b20 | 2015-12-15 16:41:39 -0500 | [diff] [blame] | 630 | |
| 631 | // ES3 specifies that cube map texture attachments must be cube complete. |
| 632 | // This language is missing from the ES2 spec, but we enforce it here because some |
| 633 | // desktop OpenGL drivers also enforce this validation. |
| 634 | // TODO(jmadill): Check if OpenGL ES2 drivers enforce cube completeness. |
| 635 | const Texture *texture = colorAttachment.getTexture(); |
| 636 | ASSERT(texture); |
Olli Etuaho | e8528d8 | 2016-05-16 17:50:52 +0300 | [diff] [blame] | 637 | if (texture->getTarget() == GL_TEXTURE_CUBE_MAP && |
| 638 | !texture->getTextureState().isCubeComplete()) |
Jamie Madill | 3215b20 | 2015-12-15 16:41:39 -0500 | [diff] [blame] | 639 | { |
| 640 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 641 | } |
JiangYizhou | 461d9a3 | 2017-01-04 16:37:26 +0800 | [diff] [blame] | 642 | |
| 643 | // ES3.1 (section 9.4) requires that the value of TEXTURE_FIXED_SAMPLE_LOCATIONS |
| 644 | // should be the same for all attached textures. |
| 645 | GLboolean fixedSampleloc = colorAttachment.getTexture()->getFixedSampleLocations( |
| 646 | colorAttachment.getTextureImageIndex().type, 0); |
| 647 | if (fixedSampleLocations.valid() && fixedSampleloc != fixedSampleLocations.value()) |
| 648 | { |
| 649 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE; |
| 650 | } |
| 651 | else |
| 652 | { |
| 653 | fixedSampleLocations = fixedSampleloc; |
| 654 | } |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 655 | } |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 656 | else if (colorAttachment.type() == GL_RENDERBUFFER) |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 657 | { |
JiangYizhou | 461d9a3 | 2017-01-04 16:37:26 +0800 | [diff] [blame] | 658 | hasRenderbuffer = true; |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 659 | if (!formatCaps.renderable || format.info->depthBits > 0 || |
| 660 | format.info->stencilBits > 0) |
Jamie Madill | bb94f34 | 2014-06-23 15:23:02 -0400 | [diff] [blame] | 661 | { |
| 662 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 663 | } |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | if (!missingAttachment) |
| 667 | { |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 668 | // APPLE_framebuffer_multisample, which EXT_draw_buffers refers to, requires that |
| 669 | // all color attachments have the same number of samples for the FBO to be complete. |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 670 | if (colorAttachment.getSamples() != samples) |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 671 | { |
| 672 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT; |
| 673 | } |
| 674 | |
shannon.woods%transgaming.com@gtempaccount.com | c347152 | 2013-04-13 03:34:52 +0000 | [diff] [blame] | 675 | // in GLES 2.0, all color attachments attachments must have the same number of bitplanes |
| 676 | // in GLES 3.0, there is no such restriction |
Martin Radev | 1be913c | 2016-07-11 17:59:16 +0300 | [diff] [blame] | 677 | if (state.getClientMajorVersion() < 3) |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 678 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 679 | if (format.info->pixelBytes != colorbufferSize) |
shannon.woods%transgaming.com@gtempaccount.com | c347152 | 2013-04-13 03:34:52 +0000 | [diff] [blame] | 680 | { |
| 681 | return GL_FRAMEBUFFER_UNSUPPORTED; |
| 682 | } |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 683 | } |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 684 | } |
| 685 | else |
| 686 | { |
JiangYizhou | 461d9a3 | 2017-01-04 16:37:26 +0800 | [diff] [blame] | 687 | samples = colorAttachment.getSamples(); |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 688 | colorbufferSize = format.info->pixelBytes; |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 689 | missingAttachment = false; |
| 690 | } |
| 691 | } |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 694 | const FramebufferAttachment &depthAttachment = mState.mDepthAttachment; |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 695 | if (depthAttachment.isAttached()) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 696 | { |
Jamie Madill | 6b120b9 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 697 | const Extents &size = depthAttachment.getSize(); |
| 698 | if (size.width == 0 || size.height == 0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 699 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 700 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 703 | const Format &format = depthAttachment.getFormat(); |
| 704 | const TextureCaps &formatCaps = state.getTextureCap(format.asSized()); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 705 | if (depthAttachment.type() == GL_TEXTURE) |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 706 | { |
Geoff Lang | 6cf8e1b | 2014-07-03 13:03:57 -0400 | [diff] [blame] | 707 | if (!formatCaps.renderable) |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 708 | { |
Jamie Madill | 8117678 | 2015-11-24 16:10:23 -0500 | [diff] [blame] | 709 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 710 | } |
| 711 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 712 | if (format.info->depthBits == 0) |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 713 | { |
| 714 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 715 | } |
| 716 | } |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 717 | else if (depthAttachment.type() == GL_RENDERBUFFER) |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 718 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 719 | if (!formatCaps.renderable || format.info->depthBits == 0) |
Jamie Madill | bb94f34 | 2014-06-23 15:23:02 -0400 | [diff] [blame] | 720 | { |
| 721 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 722 | } |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | if (missingAttachment) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 726 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 727 | samples = depthAttachment.getSamples(); |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 728 | missingAttachment = false; |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 729 | } |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 730 | else if (samples != depthAttachment.getSamples()) |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 731 | { |
Sami Väisänen | a797e06 | 2016-05-12 15:23:40 +0300 | [diff] [blame] | 732 | // CHROMIUM_framebuffer_mixed_samples allows a framebuffer to be |
| 733 | // considered complete when its depth or stencil samples are a |
| 734 | // multiple of the number of color samples. |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 735 | const bool mixedSamples = state.getExtensions().framebufferMixedSamples; |
Sami Väisänen | a797e06 | 2016-05-12 15:23:40 +0300 | [diff] [blame] | 736 | if (!mixedSamples) |
| 737 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE; |
| 738 | |
| 739 | const int colorSamples = samples ? samples : 1; |
| 740 | const int depthSamples = depthAttachment.getSamples(); |
| 741 | if ((depthSamples % colorSamples) != 0) |
| 742 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE; |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 743 | } |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 746 | const FramebufferAttachment &stencilAttachment = mState.mStencilAttachment; |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 747 | if (stencilAttachment.isAttached()) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 748 | { |
Jamie Madill | 6b120b9 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 749 | const Extents &size = stencilAttachment.getSize(); |
| 750 | if (size.width == 0 || size.height == 0) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 751 | { |
| 752 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 753 | } |
| 754 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 755 | const Format &format = stencilAttachment.getFormat(); |
| 756 | const TextureCaps &formatCaps = state.getTextureCap(format.asSized()); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 757 | if (stencilAttachment.type() == GL_TEXTURE) |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 758 | { |
Geoff Lang | 6cf8e1b | 2014-07-03 13:03:57 -0400 | [diff] [blame] | 759 | if (!formatCaps.renderable) |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 760 | { |
Jamie Madill | 8117678 | 2015-11-24 16:10:23 -0500 | [diff] [blame] | 761 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 762 | } |
| 763 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 764 | if (format.info->stencilBits == 0) |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 765 | { |
| 766 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 767 | } |
| 768 | } |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 769 | else if (stencilAttachment.type() == GL_RENDERBUFFER) |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 770 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 771 | if (!formatCaps.renderable || format.info->stencilBits == 0) |
Jamie Madill | bb94f34 | 2014-06-23 15:23:02 -0400 | [diff] [blame] | 772 | { |
| 773 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 774 | } |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | if (missingAttachment) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 778 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 779 | samples = stencilAttachment.getSamples(); |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 780 | missingAttachment = false; |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 781 | } |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 782 | else if (samples != stencilAttachment.getSamples()) |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 783 | { |
Sami Väisänen | a797e06 | 2016-05-12 15:23:40 +0300 | [diff] [blame] | 784 | // see the comments in depth attachment check. |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 785 | const bool mixedSamples = state.getExtensions().framebufferMixedSamples; |
Sami Väisänen | a797e06 | 2016-05-12 15:23:40 +0300 | [diff] [blame] | 786 | if (!mixedSamples) |
| 787 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE; |
| 788 | |
| 789 | const int colorSamples = samples ? samples : 1; |
| 790 | const int stencilSamples = stencilAttachment.getSamples(); |
| 791 | if ((stencilSamples % colorSamples) != 0) |
| 792 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE; |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 793 | } |
Corentin Wallez | 086d59a | 2016-04-29 09:06:49 -0400 | [diff] [blame] | 794 | |
| 795 | // Starting from ES 3.0 stencil and depth, if present, should be the same image |
Martin Radev | 1be913c | 2016-07-11 17:59:16 +0300 | [diff] [blame] | 796 | if (state.getClientMajorVersion() >= 3 && depthAttachment.isAttached() && |
Corentin Wallez | 086d59a | 2016-04-29 09:06:49 -0400 | [diff] [blame] | 797 | stencilAttachment != depthAttachment) |
| 798 | { |
| 799 | return GL_FRAMEBUFFER_UNSUPPORTED; |
| 800 | } |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 803 | // Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL. |
| 804 | if (state.isWebGL1()) |
| 805 | { |
| 806 | if (!mState.mWebGLDepthStencilConsistent) |
| 807 | { |
| 808 | return GL_FRAMEBUFFER_UNSUPPORTED; |
| 809 | } |
| 810 | |
| 811 | if (mState.mWebGLDepthStencilAttachment.isAttached()) |
| 812 | { |
| 813 | if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 || |
| 814 | mState.mWebGLDepthStencilAttachment.getStencilSize() == 0) |
| 815 | { |
| 816 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 817 | } |
| 818 | } |
| 819 | else if (mState.mStencilAttachment.isAttached() && |
| 820 | mState.mStencilAttachment.getDepthSize() > 0) |
| 821 | { |
| 822 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 823 | } |
| 824 | else if (mState.mDepthAttachment.isAttached() && |
| 825 | mState.mDepthAttachment.getStencilSize() > 0) |
| 826 | { |
| 827 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 828 | } |
| 829 | } |
| 830 | |
JiangYizhou | 461d9a3 | 2017-01-04 16:37:26 +0800 | [diff] [blame] | 831 | // ES3.1(section 9.4) requires that if no image is attached to the |
| 832 | // framebuffer, and either the value of the framebuffer's FRAMEBUFFER_DEFAULT_WIDTH |
| 833 | // or FRAMEBUFFER_DEFAULT_HEIGHT parameters is zero, the framebuffer is |
| 834 | // considered incomplete. |
| 835 | GLint defaultWidth = mState.getDefaultWidth(); |
| 836 | GLint defaultHeight = mState.getDefaultHeight(); |
| 837 | |
| 838 | if (missingAttachment && (defaultWidth == 0 || defaultHeight == 0)) |
daniel@transgaming.com | 6b7c84c | 2012-05-31 01:14:39 +0000 | [diff] [blame] | 839 | { |
| 840 | return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT; |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 843 | // In ES 2.0, all color attachments must have the same width and height. |
| 844 | // In ES 3.0, there is no such restriction. |
Martin Radev | 1be913c | 2016-07-11 17:59:16 +0300 | [diff] [blame] | 845 | if (state.getClientMajorVersion() < 3 && !mState.attachmentsHaveSameDimensions()) |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 846 | { |
| 847 | return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS; |
| 848 | } |
| 849 | |
JiangYizhou | 461d9a3 | 2017-01-04 16:37:26 +0800 | [diff] [blame] | 850 | // ES3.1(section 9.4) requires that if the attached images are a mix of renderbuffers |
| 851 | // and textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all |
| 852 | // attached textures. |
| 853 | if (fixedSampleLocations.valid() && hasRenderbuffer && !fixedSampleLocations.value()) |
| 854 | { |
| 855 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE; |
| 856 | } |
| 857 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 858 | syncState(); |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 859 | if (!mImpl->checkStatus()) |
| 860 | { |
| 861 | return GL_FRAMEBUFFER_UNSUPPORTED; |
| 862 | } |
| 863 | |
| 864 | return GL_FRAMEBUFFER_COMPLETE; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 865 | } |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 866 | |
Austin Kinross | 0833263 | 2015-05-05 13:35:47 -0700 | [diff] [blame] | 867 | Error Framebuffer::discard(size_t count, const GLenum *attachments) |
| 868 | { |
| 869 | return mImpl->discard(count, attachments); |
| 870 | } |
| 871 | |
Geoff Lang | 9ad4bda | 2014-12-01 11:03:09 -0500 | [diff] [blame] | 872 | Error Framebuffer::invalidate(size_t count, const GLenum *attachments) |
Jamie Madill | 2d96b9e | 2014-08-29 15:46:47 -0400 | [diff] [blame] | 873 | { |
Geoff Lang | 9ad4bda | 2014-12-01 11:03:09 -0500 | [diff] [blame] | 874 | return mImpl->invalidate(count, attachments); |
Jamie Madill | 2d96b9e | 2014-08-29 15:46:47 -0400 | [diff] [blame] | 875 | } |
| 876 | |
Geoff Lang | 9ad4bda | 2014-12-01 11:03:09 -0500 | [diff] [blame] | 877 | Error Framebuffer::invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area) |
Jamie Madill | 400a441 | 2014-08-29 15:46:45 -0400 | [diff] [blame] | 878 | { |
Geoff Lang | 9ad4bda | 2014-12-01 11:03:09 -0500 | [diff] [blame] | 879 | return mImpl->invalidateSub(count, attachments, area); |
Jamie Madill | 400a441 | 2014-08-29 15:46:45 -0400 | [diff] [blame] | 880 | } |
| 881 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 882 | Error Framebuffer::clear(rx::ContextImpl *context, GLbitfield mask) |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 883 | { |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 884 | if (context->getGLState().isRasterizerDiscardEnabled()) |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 885 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 886 | return gl::NoError(); |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 887 | } |
| 888 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 889 | return mImpl->clear(context, mask); |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 890 | } |
| 891 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 892 | Error Framebuffer::clearBufferfv(rx::ContextImpl *context, |
Jamie Madill | 1b94d43 | 2015-08-07 13:23:23 -0400 | [diff] [blame] | 893 | GLenum buffer, |
| 894 | GLint drawbuffer, |
| 895 | const GLfloat *values) |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 896 | { |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 897 | if (context->getGLState().isRasterizerDiscardEnabled()) |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 898 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 899 | return gl::NoError(); |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 900 | } |
| 901 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 902 | return mImpl->clearBufferfv(context, buffer, drawbuffer, values); |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 903 | } |
| 904 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 905 | Error Framebuffer::clearBufferuiv(rx::ContextImpl *context, |
Jamie Madill | 1b94d43 | 2015-08-07 13:23:23 -0400 | [diff] [blame] | 906 | GLenum buffer, |
| 907 | GLint drawbuffer, |
| 908 | const GLuint *values) |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 909 | { |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 910 | if (context->getGLState().isRasterizerDiscardEnabled()) |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 911 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 912 | return gl::NoError(); |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 913 | } |
| 914 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 915 | return mImpl->clearBufferuiv(context, buffer, drawbuffer, values); |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 916 | } |
| 917 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 918 | Error Framebuffer::clearBufferiv(rx::ContextImpl *context, |
Jamie Madill | 1b94d43 | 2015-08-07 13:23:23 -0400 | [diff] [blame] | 919 | GLenum buffer, |
| 920 | GLint drawbuffer, |
| 921 | const GLint *values) |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 922 | { |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 923 | if (context->getGLState().isRasterizerDiscardEnabled()) |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 924 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 925 | return gl::NoError(); |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 926 | } |
| 927 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 928 | return mImpl->clearBufferiv(context, buffer, drawbuffer, values); |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 929 | } |
| 930 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 931 | Error Framebuffer::clearBufferfi(rx::ContextImpl *context, |
Jamie Madill | 1b94d43 | 2015-08-07 13:23:23 -0400 | [diff] [blame] | 932 | GLenum buffer, |
| 933 | GLint drawbuffer, |
| 934 | GLfloat depth, |
| 935 | GLint stencil) |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 936 | { |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 937 | if (context->getGLState().isRasterizerDiscardEnabled()) |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 938 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 939 | return gl::NoError(); |
Jamie Madill | 984ef41 | 2015-11-24 16:10:21 -0500 | [diff] [blame] | 940 | } |
| 941 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 942 | return mImpl->clearBufferfi(context, buffer, drawbuffer, depth, stencil); |
Geoff Lang | b04dc82 | 2014-12-01 12:02:02 -0500 | [diff] [blame] | 943 | } |
| 944 | |
Geoff Lang | bce529e | 2014-12-01 12:48:41 -0500 | [diff] [blame] | 945 | GLenum Framebuffer::getImplementationColorReadFormat() const |
| 946 | { |
| 947 | return mImpl->getImplementationColorReadFormat(); |
| 948 | } |
| 949 | |
| 950 | GLenum Framebuffer::getImplementationColorReadType() const |
| 951 | { |
| 952 | return mImpl->getImplementationColorReadType(); |
| 953 | } |
| 954 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 955 | Error Framebuffer::readPixels(rx::ContextImpl *context, |
Jamie Madill | c29968b | 2016-01-20 11:17:23 -0500 | [diff] [blame] | 956 | const Rectangle &area, |
Jamie Madill | 1b94d43 | 2015-08-07 13:23:23 -0400 | [diff] [blame] | 957 | GLenum format, |
| 958 | GLenum type, |
| 959 | GLvoid *pixels) const |
Geoff Lang | bce529e | 2014-12-01 12:48:41 -0500 | [diff] [blame] | 960 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 961 | ANGLE_TRY(mImpl->readPixels(context, area, format, type, pixels)); |
Geoff Lang | 520c4ae | 2015-05-05 13:12:36 -0400 | [diff] [blame] | 962 | |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 963 | Buffer *unpackBuffer = context->getGLState().getUnpackState().pixelBuffer.get(); |
Geoff Lang | 520c4ae | 2015-05-05 13:12:36 -0400 | [diff] [blame] | 964 | if (unpackBuffer) |
| 965 | { |
| 966 | unpackBuffer->onPixelUnpack(); |
| 967 | } |
| 968 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 969 | return NoError(); |
Geoff Lang | bce529e | 2014-12-01 12:48:41 -0500 | [diff] [blame] | 970 | } |
| 971 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 972 | Error Framebuffer::blit(rx::ContextImpl *context, |
Jamie Madill | c29968b | 2016-01-20 11:17:23 -0500 | [diff] [blame] | 973 | const Rectangle &sourceArea, |
| 974 | const Rectangle &destArea, |
Geoff Lang | 242468f | 2015-09-24 14:15:41 -0400 | [diff] [blame] | 975 | GLbitfield mask, |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 976 | GLenum filter) |
Geoff Lang | 54bd5a4 | 2014-12-01 12:51:04 -0500 | [diff] [blame] | 977 | { |
He Yunchao | 6be602d | 2016-12-22 14:33:07 +0800 | [diff] [blame] | 978 | GLbitfield blitMask = mask; |
| 979 | |
| 980 | // Note that blitting is called against draw framebuffer. |
| 981 | // See the code in gl::Context::blitFramebuffer. |
| 982 | if ((mask & GL_COLOR_BUFFER_BIT) && !hasEnabledDrawBuffer()) |
| 983 | { |
| 984 | blitMask &= ~GL_COLOR_BUFFER_BIT; |
| 985 | } |
| 986 | |
| 987 | if ((mask & GL_STENCIL_BUFFER_BIT) && mState.getStencilAttachment() == nullptr) |
| 988 | { |
| 989 | blitMask &= ~GL_STENCIL_BUFFER_BIT; |
| 990 | } |
| 991 | |
| 992 | if ((mask & GL_DEPTH_BUFFER_BIT) && mState.getDepthAttachment() == nullptr) |
| 993 | { |
| 994 | blitMask &= ~GL_DEPTH_BUFFER_BIT; |
| 995 | } |
| 996 | |
| 997 | if (!blitMask) |
| 998 | { |
| 999 | return NoError(); |
| 1000 | } |
| 1001 | |
| 1002 | return mImpl->blit(context, sourceArea, destArea, blitMask, filter); |
Geoff Lang | 54bd5a4 | 2014-12-01 12:51:04 -0500 | [diff] [blame] | 1003 | } |
| 1004 | |
Jamie Madill | 51f40ec | 2016-06-15 14:06:00 -0400 | [diff] [blame] | 1005 | int Framebuffer::getSamples(const ContextState &state) |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 1006 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1007 | if (complete(state)) |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 1008 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1009 | // For a complete framebuffer, all attachments must have the same sample count. |
| 1010 | // In this case return the first nonzero sample size. |
| 1011 | const auto *firstColorAttachment = mState.getFirstColorAttachment(); |
| 1012 | if (firstColorAttachment) |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 1013 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1014 | ASSERT(firstColorAttachment->isAttached()); |
| 1015 | return firstColorAttachment->getSamples(); |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 1016 | } |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 1017 | } |
shannon.woods%transgaming.com@gtempaccount.com | f30ccc2 | 2013-04-13 03:28:36 +0000 | [diff] [blame] | 1018 | |
| 1019 | return 0; |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 1022 | Error Framebuffer::getSamplePosition(size_t index, GLfloat *xy) const |
| 1023 | { |
| 1024 | ANGLE_TRY(mImpl->getSamplePosition(index, xy)); |
| 1025 | return gl::NoError(); |
| 1026 | } |
| 1027 | |
Jamie Madill | e261b44 | 2014-06-25 12:42:21 -0400 | [diff] [blame] | 1028 | bool Framebuffer::hasValidDepthStencil() const |
| 1029 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1030 | return mState.getDepthStencilAttachment() != nullptr; |
Jamie Madill | e261b44 | 2014-06-25 12:42:21 -0400 | [diff] [blame] | 1031 | } |
| 1032 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1033 | void Framebuffer::setAttachment(const Context *context, |
| 1034 | GLenum type, |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1035 | GLenum binding, |
| 1036 | const ImageIndex &textureIndex, |
| 1037 | FramebufferAttachmentObject *resource) |
Geoff Lang | ab75a05 | 2014-10-15 12:56:37 -0400 | [diff] [blame] | 1038 | { |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1039 | // Context may be null in unit tests. |
| 1040 | if (!context || !context->isWebGL1()) |
| 1041 | { |
| 1042 | setAttachmentImpl(type, binding, textureIndex, resource); |
| 1043 | return; |
| 1044 | } |
| 1045 | |
| 1046 | switch (binding) |
| 1047 | { |
| 1048 | case GL_DEPTH_STENCIL: |
| 1049 | case GL_DEPTH_STENCIL_ATTACHMENT: |
| 1050 | mState.mWebGLDepthStencilAttachment.attach(type, binding, textureIndex, resource); |
| 1051 | break; |
| 1052 | case GL_DEPTH: |
| 1053 | case GL_DEPTH_ATTACHMENT: |
| 1054 | mState.mWebGLDepthAttachment.attach(type, binding, textureIndex, resource); |
| 1055 | break; |
| 1056 | case GL_STENCIL: |
| 1057 | case GL_STENCIL_ATTACHMENT: |
| 1058 | mState.mWebGLStencilAttachment.attach(type, binding, textureIndex, resource); |
| 1059 | break; |
| 1060 | default: |
| 1061 | setAttachmentImpl(type, binding, textureIndex, resource); |
| 1062 | return; |
| 1063 | } |
| 1064 | |
| 1065 | commitWebGL1DepthStencilIfConsistent(); |
| 1066 | } |
| 1067 | |
| 1068 | void Framebuffer::commitWebGL1DepthStencilIfConsistent() |
| 1069 | { |
| 1070 | int count = 0; |
| 1071 | |
| 1072 | std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment, |
| 1073 | &mState.mWebGLDepthAttachment, |
| 1074 | &mState.mWebGLStencilAttachment}}; |
| 1075 | for (FramebufferAttachment *attachment : attachments) |
| 1076 | { |
| 1077 | if (attachment->isAttached()) |
| 1078 | { |
| 1079 | count++; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | mState.mWebGLDepthStencilConsistent = (count <= 1); |
| 1084 | if (!mState.mWebGLDepthStencilConsistent) |
| 1085 | { |
| 1086 | // Inconsistent. |
| 1087 | return; |
| 1088 | } |
| 1089 | |
Geoff Lang | e466c55 | 2017-03-17 15:24:12 -0400 | [diff] [blame] | 1090 | auto getImageIndexIfTextureAttachment = [](const FramebufferAttachment &attachment) { |
| 1091 | if (attachment.type() == GL_TEXTURE) |
| 1092 | { |
| 1093 | return attachment.getTextureImageIndex(); |
| 1094 | } |
| 1095 | else |
| 1096 | { |
| 1097 | return ImageIndex::MakeInvalid(); |
| 1098 | } |
| 1099 | }; |
| 1100 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1101 | if (mState.mWebGLDepthAttachment.isAttached()) |
| 1102 | { |
| 1103 | const auto &depth = mState.mWebGLDepthAttachment; |
Geoff Lang | e466c55 | 2017-03-17 15:24:12 -0400 | [diff] [blame] | 1104 | setAttachmentImpl(depth.type(), GL_DEPTH_ATTACHMENT, |
| 1105 | getImageIndexIfTextureAttachment(depth), depth.getResource()); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1106 | setAttachmentImpl(GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr); |
| 1107 | } |
| 1108 | else if (mState.mWebGLStencilAttachment.isAttached()) |
| 1109 | { |
| 1110 | const auto &stencil = mState.mWebGLStencilAttachment; |
| 1111 | setAttachmentImpl(GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr); |
Geoff Lang | e466c55 | 2017-03-17 15:24:12 -0400 | [diff] [blame] | 1112 | setAttachmentImpl(stencil.type(), GL_STENCIL_ATTACHMENT, |
| 1113 | getImageIndexIfTextureAttachment(stencil), stencil.getResource()); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1114 | } |
| 1115 | else if (mState.mWebGLDepthStencilAttachment.isAttached()) |
| 1116 | { |
| 1117 | const auto &depthStencil = mState.mWebGLDepthStencilAttachment; |
Geoff Lang | e466c55 | 2017-03-17 15:24:12 -0400 | [diff] [blame] | 1118 | setAttachmentImpl(depthStencil.type(), GL_DEPTH_ATTACHMENT, |
| 1119 | getImageIndexIfTextureAttachment(depthStencil), |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1120 | depthStencil.getResource()); |
Geoff Lang | e466c55 | 2017-03-17 15:24:12 -0400 | [diff] [blame] | 1121 | setAttachmentImpl(depthStencil.type(), GL_STENCIL_ATTACHMENT, |
| 1122 | getImageIndexIfTextureAttachment(depthStencil), |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1123 | depthStencil.getResource()); |
| 1124 | } |
| 1125 | else |
| 1126 | { |
| 1127 | setAttachmentImpl(GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr); |
| 1128 | setAttachmentImpl(GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | void Framebuffer::setAttachmentImpl(GLenum type, |
| 1133 | GLenum binding, |
| 1134 | const ImageIndex &textureIndex, |
| 1135 | FramebufferAttachmentObject *resource) |
| 1136 | { |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1137 | if (binding == GL_DEPTH_STENCIL || binding == GL_DEPTH_STENCIL_ATTACHMENT) |
Geoff Lang | ab75a05 | 2014-10-15 12:56:37 -0400 | [diff] [blame] | 1138 | { |
Geoff Lang | ab75a05 | 2014-10-15 12:56:37 -0400 | [diff] [blame] | 1139 | // ensure this is a legitimate depth+stencil format |
Jamie Madill | 375c37c | 2015-07-21 15:14:08 -0400 | [diff] [blame] | 1140 | FramebufferAttachmentObject *attachmentObj = resource; |
| 1141 | if (resource) |
Geoff Lang | ab75a05 | 2014-10-15 12:56:37 -0400 | [diff] [blame] | 1142 | { |
Jamie Madill | 375c37c | 2015-07-21 15:14:08 -0400 | [diff] [blame] | 1143 | FramebufferAttachment::Target target(binding, textureIndex); |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 1144 | const Format &format = resource->getAttachmentFormat(target); |
| 1145 | if (format.info->depthBits == 0 || format.info->stencilBits == 0) |
Jamie Madill | 375c37c | 2015-07-21 15:14:08 -0400 | [diff] [blame] | 1146 | { |
| 1147 | // Attaching nullptr detaches the current attachment. |
| 1148 | attachmentObj = nullptr; |
| 1149 | } |
Geoff Lang | ab75a05 | 2014-10-15 12:56:37 -0400 | [diff] [blame] | 1150 | } |
Jamie Madill | 375c37c | 2015-07-21 15:14:08 -0400 | [diff] [blame] | 1151 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1152 | mState.mDepthAttachment.attach(type, binding, textureIndex, attachmentObj); |
| 1153 | mState.mStencilAttachment.attach(type, binding, textureIndex, attachmentObj); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1154 | mDirtyBits.set(DIRTY_BIT_DEPTH_ATTACHMENT); |
| 1155 | mDirtyBits.set(DIRTY_BIT_STENCIL_ATTACHMENT); |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1156 | BindResourceChannel(&mDirtyDepthAttachmentBinding, resource); |
| 1157 | BindResourceChannel(&mDirtyStencilAttachmentBinding, resource); |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1158 | return; |
Geoff Lang | ab75a05 | 2014-10-15 12:56:37 -0400 | [diff] [blame] | 1159 | } |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 1160 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1161 | switch (binding) |
| 1162 | { |
| 1163 | case GL_DEPTH: |
| 1164 | case GL_DEPTH_ATTACHMENT: |
| 1165 | mState.mDepthAttachment.attach(type, binding, textureIndex, resource); |
| 1166 | mDirtyBits.set(DIRTY_BIT_DEPTH_ATTACHMENT); |
| 1167 | BindResourceChannel(&mDirtyDepthAttachmentBinding, resource); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1168 | break; |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1169 | case GL_STENCIL: |
| 1170 | case GL_STENCIL_ATTACHMENT: |
| 1171 | mState.mStencilAttachment.attach(type, binding, textureIndex, resource); |
| 1172 | mDirtyBits.set(DIRTY_BIT_STENCIL_ATTACHMENT); |
| 1173 | BindResourceChannel(&mDirtyStencilAttachmentBinding, resource); |
| 1174 | break; |
| 1175 | case GL_BACK: |
| 1176 | mState.mColorAttachments[0].attach(type, binding, textureIndex, resource); |
| 1177 | mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0); |
| 1178 | // No need for a resource binding for the default FBO, it's always complete. |
| 1179 | break; |
| 1180 | default: |
| 1181 | { |
| 1182 | size_t colorIndex = binding - GL_COLOR_ATTACHMENT0; |
| 1183 | ASSERT(colorIndex < mState.mColorAttachments.size()); |
| 1184 | mState.mColorAttachments[colorIndex].attach(type, binding, textureIndex, resource); |
| 1185 | mDirtyBits.set(DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex); |
| 1186 | BindResourceChannel(&mDirtyColorAttachmentBindings[colorIndex], resource); |
| 1187 | |
| 1188 | bool enabled = (type != GL_NONE && getDrawBufferState(colorIndex) != GL_NONE); |
| 1189 | mState.mEnabledDrawBuffers.set(colorIndex, enabled); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1190 | } |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1191 | break; |
Geoff Lang | ab75a05 | 2014-10-15 12:56:37 -0400 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1195 | void Framebuffer::resetAttachment(const Context *context, GLenum binding) |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1196 | { |
Jamie Madill | a02315b | 2017-02-23 14:14:47 -0500 | [diff] [blame] | 1197 | setAttachment(context, GL_NONE, binding, ImageIndex::MakeInvalid(), nullptr); |
Jamie Madill | 2d06b73 | 2015-04-20 12:53:28 -0400 | [diff] [blame] | 1198 | } |
| 1199 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1200 | void Framebuffer::syncState() |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1201 | { |
| 1202 | if (mDirtyBits.any()) |
| 1203 | { |
| 1204 | mImpl->syncState(mDirtyBits); |
| 1205 | mDirtyBits.reset(); |
Corentin Wallez | ccab69d | 2017-01-27 16:57:15 -0500 | [diff] [blame] | 1206 | if (mId != 0) |
| 1207 | { |
| 1208 | mCachedStatus.reset(); |
| 1209 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1210 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1211 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1212 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1213 | void Framebuffer::signal(SignalToken token) |
Jamie Madill | 51f40ec | 2016-06-15 14:06:00 -0400 | [diff] [blame] | 1214 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1215 | // TOOD(jmadill): Make this only update individual attachments to do less work. |
| 1216 | mCachedStatus.reset(); |
Jamie Madill | 51f40ec | 2016-06-15 14:06:00 -0400 | [diff] [blame] | 1217 | } |
| 1218 | |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1219 | bool Framebuffer::complete(const ContextState &state) |
Jamie Madill | 51f40ec | 2016-06-15 14:06:00 -0400 | [diff] [blame] | 1220 | { |
Jamie Madill | 362876b | 2016-06-16 14:46:59 -0400 | [diff] [blame] | 1221 | return (checkStatus(state) == GL_FRAMEBUFFER_COMPLETE); |
Jamie Madill | 51f40ec | 2016-06-15 14:06:00 -0400 | [diff] [blame] | 1222 | } |
| 1223 | |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 1224 | bool Framebuffer::formsRenderingFeedbackLoopWith(const State &state) const |
| 1225 | { |
| 1226 | const Program *program = state.getProgram(); |
| 1227 | |
| 1228 | // TODO(jmadill): Default framebuffer feedback loops. |
| 1229 | if (mId == 0) |
| 1230 | { |
| 1231 | return false; |
| 1232 | } |
| 1233 | |
| 1234 | // The bitset will skip inactive draw buffers. |
| 1235 | for (GLuint drawIndex : angle::IterateBitSet(mState.mEnabledDrawBuffers)) |
| 1236 | { |
| 1237 | const FramebufferAttachment *attachment = getDrawBuffer(drawIndex); |
| 1238 | if (attachment && attachment->type() == GL_TEXTURE) |
| 1239 | { |
| 1240 | // Validate the feedback loop. |
| 1241 | if (program->samplesFromTexture(state, attachment->id())) |
| 1242 | { |
| 1243 | return true; |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | |
Jamie Madill | 1d37bc5 | 2017-02-02 19:59:58 -0500 | [diff] [blame] | 1248 | // Validate depth-stencil feedback loop. |
| 1249 | const auto &dsState = state.getDepthStencilState(); |
| 1250 | |
| 1251 | // We can skip the feedback loop checks if depth/stencil is masked out or disabled. |
| 1252 | const FramebufferAttachment *depth = getDepthbuffer(); |
| 1253 | if (depth && depth->type() == GL_TEXTURE && dsState.depthTest && dsState.depthMask) |
| 1254 | { |
| 1255 | if (program->samplesFromTexture(state, depth->id())) |
| 1256 | { |
| 1257 | return true; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | // Note: we assume the front and back masks are the same for WebGL. |
| 1262 | const FramebufferAttachment *stencil = getStencilbuffer(); |
| 1263 | ASSERT(dsState.stencilBackWritemask == dsState.stencilWritemask); |
| 1264 | if (stencil && stencil->type() == GL_TEXTURE && dsState.stencilTest && |
| 1265 | dsState.stencilWritemask != 0) |
| 1266 | { |
| 1267 | // Skip the feedback loop check if depth/stencil point to the same resource. |
| 1268 | if (!depth || *stencil != *depth) |
| 1269 | { |
| 1270 | if (program->samplesFromTexture(state, stencil->id())) |
| 1271 | { |
| 1272 | return true; |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 1277 | return false; |
| 1278 | } |
| 1279 | |
Jamie Madill | fd3dd43 | 2017-02-02 19:59:59 -0500 | [diff] [blame] | 1280 | bool Framebuffer::formsCopyingFeedbackLoopWith(GLuint copyTextureID, |
| 1281 | GLint copyTextureLevel, |
| 1282 | GLint copyTextureLayer) const |
Jamie Madill | f695a3a | 2017-01-11 17:36:35 -0500 | [diff] [blame] | 1283 | { |
| 1284 | if (mId == 0) |
| 1285 | { |
| 1286 | // It seems impossible to form a texture copying feedback loop with the default FBO. |
| 1287 | return false; |
| 1288 | } |
| 1289 | |
| 1290 | const FramebufferAttachment *readAttachment = getReadColorbuffer(); |
| 1291 | ASSERT(readAttachment); |
| 1292 | |
| 1293 | if (readAttachment->isTextureWithId(copyTextureID)) |
| 1294 | { |
Jamie Madill | fd3dd43 | 2017-02-02 19:59:59 -0500 | [diff] [blame] | 1295 | const auto &imageIndex = readAttachment->getTextureImageIndex(); |
| 1296 | if (imageIndex.mipIndex == copyTextureLevel) |
Jamie Madill | f695a3a | 2017-01-11 17:36:35 -0500 | [diff] [blame] | 1297 | { |
Jamie Madill | fd3dd43 | 2017-02-02 19:59:59 -0500 | [diff] [blame] | 1298 | // Check 3D/Array texture layers. |
| 1299 | return imageIndex.layerIndex == ImageIndex::ENTIRE_LEVEL || |
| 1300 | copyTextureLayer == ImageIndex::ENTIRE_LEVEL || |
| 1301 | imageIndex.layerIndex == copyTextureLayer; |
Jamie Madill | f695a3a | 2017-01-11 17:36:35 -0500 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | return false; |
| 1305 | } |
| 1306 | |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 1307 | GLint Framebuffer::getDefaultWidth() const |
| 1308 | { |
| 1309 | return mState.getDefaultWidth(); |
| 1310 | } |
| 1311 | |
| 1312 | GLint Framebuffer::getDefaultHeight() const |
| 1313 | { |
| 1314 | return mState.getDefaultHeight(); |
| 1315 | } |
| 1316 | |
| 1317 | GLint Framebuffer::getDefaultSamples() const |
| 1318 | { |
| 1319 | return mState.getDefaultSamples(); |
| 1320 | } |
| 1321 | |
| 1322 | GLboolean Framebuffer::getDefaultFixedSampleLocations() const |
| 1323 | { |
| 1324 | return mState.getDefaultFixedSampleLocations(); |
| 1325 | } |
| 1326 | |
| 1327 | void Framebuffer::setDefaultWidth(GLint defaultWidth) |
| 1328 | { |
| 1329 | mState.mDefaultWidth = defaultWidth; |
| 1330 | mDirtyBits.set(DIRTY_BIT_DEFAULT_WIDTH); |
| 1331 | } |
| 1332 | |
| 1333 | void Framebuffer::setDefaultHeight(GLint defaultHeight) |
| 1334 | { |
| 1335 | mState.mDefaultHeight = defaultHeight; |
| 1336 | mDirtyBits.set(DIRTY_BIT_DEFAULT_HEIGHT); |
| 1337 | } |
| 1338 | |
| 1339 | void Framebuffer::setDefaultSamples(GLint defaultSamples) |
| 1340 | { |
| 1341 | mState.mDefaultSamples = defaultSamples; |
| 1342 | mDirtyBits.set(DIRTY_BIT_DEFAULT_SAMPLES); |
| 1343 | } |
| 1344 | |
| 1345 | void Framebuffer::setDefaultFixedSampleLocations(GLboolean defaultFixedSampleLocations) |
| 1346 | { |
| 1347 | mState.mDefaultFixedSampleLocations = defaultFixedSampleLocations; |
| 1348 | mDirtyBits.set(DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS); |
| 1349 | } |
| 1350 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1351 | } // namespace gl |