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