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