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