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