Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2015 The ANGLE Project Authors. All rights reserved. |
| 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 | // FramebufferGL.cpp: Implements the class methods for FramebufferGL. |
| 8 | |
| 9 | #include "libANGLE/renderer/gl/FramebufferGL.h" |
| 10 | |
Jamie Madill | 20e005b | 2017-04-07 14:19:22 -0400 | [diff] [blame] | 11 | #include "common/bitset_utils.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 12 | #include "common/debug.h" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 13 | #include "libANGLE/FramebufferAttachment.h" |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 14 | #include "libANGLE/State.h" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 15 | #include "libANGLE/angletypes.h" |
| 16 | #include "libANGLE/formatutils.h" |
Geoff Lang | 9201943 | 2017-11-20 13:09:34 -0500 | [diff] [blame] | 17 | #include "libANGLE/queryconversions.h" |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/ContextImpl.h" |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 19 | #include "libANGLE/renderer/gl/BlitGL.h" |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 20 | #include "libANGLE/renderer/gl/ClearMultiviewGL.h" |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 21 | #include "libANGLE/renderer/gl/ContextGL.h" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 22 | #include "libANGLE/renderer/gl/FunctionsGL.h" |
Jacek Caban | fa60f69 | 2015-04-27 18:23:44 +0200 | [diff] [blame] | 23 | #include "libANGLE/renderer/gl/RenderbufferGL.h" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 24 | #include "libANGLE/renderer/gl/StateManagerGL.h" |
| 25 | #include "libANGLE/renderer/gl/TextureGL.h" |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame] | 26 | #include "libANGLE/renderer/gl/formatutilsgl.h" |
Corentin Wallez | 886de36 | 2016-09-27 10:49:35 -0400 | [diff] [blame] | 27 | #include "libANGLE/renderer/gl/renderergl_utils.h" |
Jonah Ryan-Davis | beb0eb2 | 2019-06-14 15:10:33 -0400 | [diff] [blame] | 28 | #include "platform/FeaturesGL.h" |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 29 | #include "platform/Platform.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 30 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 31 | using namespace gl; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 32 | using angle::CheckedNumeric; |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 33 | |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 34 | namespace rx |
| 35 | { |
| 36 | |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 37 | namespace |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 38 | { |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 39 | |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 40 | void BindFramebufferAttachment(const FunctionsGL *functions, |
| 41 | GLenum attachmentPoint, |
| 42 | const FramebufferAttachment *attachment) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 43 | { |
| 44 | if (attachment) |
| 45 | { |
| 46 | if (attachment->type() == GL_TEXTURE) |
| 47 | { |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 48 | const Texture *texture = attachment->getTexture(); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 49 | const TextureGL *textureGL = GetImplAs<TextureGL>(texture); |
| 50 | |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 51 | if (texture->getType() == TextureType::_2D || |
| 52 | texture->getType() == TextureType::_2DMultisample || |
| 53 | texture->getType() == TextureType::Rectangle) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 54 | { |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 55 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 56 | ToGLenum(texture->getType()), |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 57 | textureGL->getTextureID(), attachment->mipLevel()); |
| 58 | } |
Jiawei Shao | a880247 | 2018-05-28 11:17:47 +0800 | [diff] [blame] | 59 | else if (attachment->isLayered()) |
| 60 | { |
| 61 | TextureType textureType = texture->getType(); |
| 62 | ASSERT(textureType == TextureType::_2DArray || textureType == TextureType::_3D || |
Olli Etuaho | dff32a0 | 2018-08-28 14:35:50 +0300 | [diff] [blame] | 63 | textureType == TextureType::CubeMap || |
| 64 | textureType == TextureType::_2DMultisampleArray); |
Jiawei Shao | a880247 | 2018-05-28 11:17:47 +0800 | [diff] [blame] | 65 | functions->framebufferTexture(GL_FRAMEBUFFER, attachmentPoint, |
| 66 | textureGL->getTextureID(), attachment->mipLevel()); |
| 67 | } |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 68 | else if (texture->getType() == TextureType::CubeMap) |
| 69 | { |
| 70 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, |
| 71 | ToGLenum(attachment->cubeMapFace()), |
| 72 | textureGL->getTextureID(), attachment->mipLevel()); |
| 73 | } |
| 74 | else if (texture->getType() == TextureType::_2DArray || |
Olli Etuaho | fd16210 | 2018-08-27 16:14:57 +0300 | [diff] [blame] | 75 | texture->getType() == TextureType::_3D || |
| 76 | texture->getType() == TextureType::_2DMultisampleArray) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 77 | { |
Mingyu Hu | 7d64c48 | 2019-03-12 14:27:40 -0700 | [diff] [blame] | 78 | if (attachment->isMultiview()) |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 79 | { |
| 80 | ASSERT(functions->framebufferTexture); |
| 81 | functions->framebufferTexture(GL_FRAMEBUFFER, attachmentPoint, |
| 82 | textureGL->getTextureID(), |
| 83 | attachment->mipLevel()); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | functions->framebufferTextureLayer(GL_FRAMEBUFFER, attachmentPoint, |
| 88 | textureGL->getTextureID(), |
| 89 | attachment->mipLevel(), attachment->layer()); |
| 90 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 91 | } |
| 92 | else |
| 93 | { |
| 94 | UNREACHABLE(); |
| 95 | } |
| 96 | } |
| 97 | else if (attachment->type() == GL_RENDERBUFFER) |
| 98 | { |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 99 | const Renderbuffer *renderbuffer = attachment->getRenderbuffer(); |
Geoff Lang | cd69f1c | 2015-03-18 14:33:23 -0400 | [diff] [blame] | 100 | const RenderbufferGL *renderbufferGL = GetImplAs<RenderbufferGL>(renderbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 101 | |
Geoff Lang | cd69f1c | 2015-03-18 14:33:23 -0400 | [diff] [blame] | 102 | functions->framebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER, |
| 103 | renderbufferGL->getRenderbufferID()); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 104 | } |
| 105 | else |
| 106 | { |
| 107 | UNREACHABLE(); |
| 108 | } |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | // Unbind this attachment |
| 113 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, 0, 0); |
| 114 | } |
| 115 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 116 | |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 117 | bool AreAllLayersActive(const FramebufferAttachment &attachment) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 118 | { |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 119 | int baseViewIndex = attachment.getBaseViewIndex(); |
| 120 | if (baseViewIndex != 0) |
| 121 | { |
| 122 | return false; |
| 123 | } |
| 124 | const ImageIndex &imageIndex = attachment.getTextureImageIndex(); |
Jamie Madill | cc12937 | 2018-04-12 09:13:18 -0400 | [diff] [blame] | 125 | int numLayers = static_cast<int>( |
| 126 | attachment.getTexture()->getDepth(imageIndex.getTarget(), imageIndex.getLevelIndex())); |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 127 | return (attachment.getNumViews() == numLayers); |
| 128 | } |
| 129 | |
| 130 | bool RequiresMultiviewClear(const FramebufferState &state, bool scissorTestEnabled) |
| 131 | { |
| 132 | // Get one attachment and check whether all layers are attached. |
| 133 | const FramebufferAttachment *attachment = nullptr; |
| 134 | bool allTextureArraysAreFullyAttached = true; |
| 135 | for (const FramebufferAttachment &colorAttachment : state.getColorAttachments()) |
| 136 | { |
| 137 | if (colorAttachment.isAttached()) |
| 138 | { |
Mingyu Hu | 7d64c48 | 2019-03-12 14:27:40 -0700 | [diff] [blame] | 139 | if (!colorAttachment.isMultiview()) |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 140 | { |
| 141 | return false; |
| 142 | } |
| 143 | attachment = &colorAttachment; |
| 144 | allTextureArraysAreFullyAttached = |
| 145 | allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | const FramebufferAttachment *depthAttachment = state.getDepthAttachment(); |
| 150 | if (depthAttachment) |
| 151 | { |
Mingyu Hu | 7d64c48 | 2019-03-12 14:27:40 -0700 | [diff] [blame] | 152 | if (!depthAttachment->isMultiview()) |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 153 | { |
| 154 | return false; |
| 155 | } |
| 156 | attachment = depthAttachment; |
| 157 | allTextureArraysAreFullyAttached = |
| 158 | allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment); |
| 159 | } |
| 160 | const FramebufferAttachment *stencilAttachment = state.getStencilAttachment(); |
| 161 | if (stencilAttachment) |
| 162 | { |
Mingyu Hu | 7d64c48 | 2019-03-12 14:27:40 -0700 | [diff] [blame] | 163 | if (!stencilAttachment->isMultiview()) |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 164 | { |
| 165 | return false; |
| 166 | } |
| 167 | attachment = stencilAttachment; |
| 168 | allTextureArraysAreFullyAttached = |
| 169 | allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment); |
| 170 | } |
| 171 | |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 172 | if (attachment == nullptr) |
| 173 | { |
| 174 | return false; |
| 175 | } |
Mingyu Hu | 7d64c48 | 2019-03-12 14:27:40 -0700 | [diff] [blame] | 176 | if (attachment->isMultiview()) |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 177 | { |
Mingyu Hu | 7d64c48 | 2019-03-12 14:27:40 -0700 | [diff] [blame] | 178 | // If all layers of each texture array are active, then there is no need to issue a |
| 179 | // special multiview clear. |
| 180 | return !allTextureArraysAreFullyAttached; |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 181 | } |
| 182 | return false; |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 183 | } |
| 184 | |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 185 | } // namespace |
| 186 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 187 | FramebufferGL::FramebufferGL(const gl::FramebufferState &data, GLuint id, bool isDefault) |
| 188 | : FramebufferImpl(data), |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 189 | mFramebufferID(id), |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 190 | mIsDefault(isDefault), |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 191 | mAppliedEnabledDrawBuffers(1) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 192 | {} |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 193 | |
| 194 | FramebufferGL::~FramebufferGL() |
| 195 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 196 | ASSERT(mFramebufferID == 0); |
| 197 | } |
| 198 | |
| 199 | void FramebufferGL::destroy(const gl::Context *context) |
| 200 | { |
| 201 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 202 | stateManager->deleteFramebuffer(mFramebufferID); |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 203 | mFramebufferID = 0; |
| 204 | } |
| 205 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 206 | angle::Result FramebufferGL::discard(const gl::Context *context, |
| 207 | size_t count, |
| 208 | const GLenum *attachments) |
Austin Kinross | 0833263 | 2015-05-05 13:35:47 -0700 | [diff] [blame] | 209 | { |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 210 | // glInvalidateFramebuffer accepts the same enums as glDiscardFramebufferEXT |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 211 | return invalidate(context, count, attachments); |
Austin Kinross | 0833263 | 2015-05-05 13:35:47 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 214 | angle::Result FramebufferGL::invalidate(const gl::Context *context, |
| 215 | size_t count, |
| 216 | const GLenum *attachments) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 217 | { |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 218 | const GLenum *finalAttachmentsPtr = attachments; |
| 219 | |
| 220 | std::vector<GLenum> modifiedAttachments; |
| 221 | if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments)) |
| 222 | { |
| 223 | finalAttachmentsPtr = modifiedAttachments.data(); |
| 224 | } |
| 225 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 226 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 227 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 228 | |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 229 | // Since this function is just a hint, only call a native function if it exists. |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 230 | if (functions->invalidateFramebuffer) |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 231 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 232 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 233 | functions->invalidateFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count), |
| 234 | finalAttachmentsPtr); |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 235 | } |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 236 | else if (functions->discardFramebufferEXT) |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 237 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 238 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 239 | functions->discardFramebufferEXT(GL_FRAMEBUFFER, static_cast<GLsizei>(count), |
| 240 | finalAttachmentsPtr); |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 241 | } |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 242 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 243 | return angle::Result::Continue; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 244 | } |
| 245 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 246 | angle::Result FramebufferGL::invalidateSub(const gl::Context *context, |
| 247 | size_t count, |
| 248 | const GLenum *attachments, |
| 249 | const gl::Rectangle &area) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 250 | { |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 251 | |
| 252 | const GLenum *finalAttachmentsPtr = attachments; |
| 253 | |
| 254 | std::vector<GLenum> modifiedAttachments; |
| 255 | if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments)) |
| 256 | { |
| 257 | finalAttachmentsPtr = modifiedAttachments.data(); |
| 258 | } |
| 259 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 260 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 261 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 262 | |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 263 | // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is |
| 264 | // available. |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 265 | if (functions->invalidateSubFramebuffer) |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 266 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 267 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 268 | functions->invalidateSubFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count), |
| 269 | finalAttachmentsPtr, area.x, area.y, area.width, |
| 270 | area.height); |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 273 | return angle::Result::Continue; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 274 | } |
| 275 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 276 | angle::Result FramebufferGL::clear(const gl::Context *context, GLbitfield mask) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 277 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 278 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 279 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 280 | |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 281 | syncClearState(context, mask); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 282 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 283 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 284 | if (!RequiresMultiviewClear(mState, context->getState().isScissorTestEnabled())) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 285 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 286 | functions->clear(mask); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 287 | } |
| 288 | else |
| 289 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 290 | ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context); |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 291 | multiviewClearer->clearMultiviewFBO(mState, context->getState().getScissor(), |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 292 | ClearMultiviewGL::ClearCommandType::Clear, mask, |
| 293 | GL_NONE, 0, nullptr, 0.0f, 0); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 294 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 295 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 296 | return angle::Result::Continue; |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 299 | angle::Result FramebufferGL::clearBufferfv(const gl::Context *context, |
| 300 | GLenum buffer, |
| 301 | GLint drawbuffer, |
| 302 | const GLfloat *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 303 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 304 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 305 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 306 | |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 307 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 308 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 309 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 310 | if (!RequiresMultiviewClear(mState, context->getState().isScissorTestEnabled())) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 311 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 312 | functions->clearBufferfv(buffer, drawbuffer, values); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 313 | } |
| 314 | else |
| 315 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 316 | ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context); |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 317 | multiviewClearer->clearMultiviewFBO(mState, context->getState().getScissor(), |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 318 | ClearMultiviewGL::ClearCommandType::ClearBufferfv, |
| 319 | static_cast<GLbitfield>(0u), buffer, drawbuffer, |
| 320 | reinterpret_cast<const uint8_t *>(values), 0.0f, 0); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 321 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 322 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 323 | return angle::Result::Continue; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 324 | } |
| 325 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 326 | angle::Result FramebufferGL::clearBufferuiv(const gl::Context *context, |
| 327 | GLenum buffer, |
| 328 | GLint drawbuffer, |
| 329 | const GLuint *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 330 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 331 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 332 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 333 | |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 334 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 335 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 336 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 337 | if (!RequiresMultiviewClear(mState, context->getState().isScissorTestEnabled())) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 338 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 339 | functions->clearBufferuiv(buffer, drawbuffer, values); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 340 | } |
| 341 | else |
| 342 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 343 | ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context); |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 344 | multiviewClearer->clearMultiviewFBO(mState, context->getState().getScissor(), |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 345 | ClearMultiviewGL::ClearCommandType::ClearBufferuiv, |
| 346 | static_cast<GLbitfield>(0u), buffer, drawbuffer, |
| 347 | reinterpret_cast<const uint8_t *>(values), 0.0f, 0); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 348 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 349 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 350 | return angle::Result::Continue; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 353 | angle::Result FramebufferGL::clearBufferiv(const gl::Context *context, |
| 354 | GLenum buffer, |
| 355 | GLint drawbuffer, |
| 356 | const GLint *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 357 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 358 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 359 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 360 | |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 361 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 362 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 363 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 364 | if (!RequiresMultiviewClear(mState, context->getState().isScissorTestEnabled())) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 365 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 366 | functions->clearBufferiv(buffer, drawbuffer, values); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 367 | } |
| 368 | else |
| 369 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 370 | ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context); |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 371 | multiviewClearer->clearMultiviewFBO(mState, context->getState().getScissor(), |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 372 | ClearMultiviewGL::ClearCommandType::ClearBufferiv, |
| 373 | static_cast<GLbitfield>(0u), buffer, drawbuffer, |
| 374 | reinterpret_cast<const uint8_t *>(values), 0.0f, 0); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 375 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 376 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 377 | return angle::Result::Continue; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 378 | } |
| 379 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 380 | angle::Result FramebufferGL::clearBufferfi(const gl::Context *context, |
| 381 | GLenum buffer, |
| 382 | GLint drawbuffer, |
| 383 | GLfloat depth, |
| 384 | GLint stencil) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 385 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 386 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 387 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 388 | |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 389 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 390 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 391 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 392 | if (!RequiresMultiviewClear(mState, context->getState().isScissorTestEnabled())) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 393 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 394 | functions->clearBufferfi(buffer, drawbuffer, depth, stencil); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 395 | } |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 396 | else |
| 397 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 398 | ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context); |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 399 | multiviewClearer->clearMultiviewFBO(mState, context->getState().getScissor(), |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 400 | ClearMultiviewGL::ClearCommandType::ClearBufferfi, |
| 401 | static_cast<GLbitfield>(0u), buffer, drawbuffer, |
| 402 | nullptr, depth, stencil); |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 403 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 404 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 405 | return angle::Result::Continue; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 406 | } |
| 407 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 408 | GLenum FramebufferGL::getImplementationColorReadFormat(const gl::Context *context) const |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 409 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 410 | const auto *readAttachment = mState.getReadAttachment(); |
| 411 | const Format &format = readAttachment->getFormat(); |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame] | 412 | return format.info->getReadPixelsFormat(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 413 | } |
| 414 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 415 | GLenum FramebufferGL::getImplementationColorReadType(const gl::Context *context) const |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 416 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 417 | const auto *readAttachment = mState.getReadAttachment(); |
| 418 | const Format &format = readAttachment->getFormat(); |
Geoff Lang | c71ea66 | 2017-09-26 17:06:02 -0400 | [diff] [blame] | 419 | return format.info->getReadPixelsType(context->getClientVersion()); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 420 | } |
| 421 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 422 | angle::Result FramebufferGL::readPixels(const gl::Context *context, |
| 423 | const gl::Rectangle &area, |
| 424 | GLenum format, |
| 425 | GLenum type, |
| 426 | void *pixels) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 427 | { |
Jamie Madill | 4e71b2b | 2019-07-08 13:23:38 -0400 | [diff] [blame] | 428 | ContextGL *contextGL = GetImplAs<ContextGL>(context); |
| 429 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 430 | StateManagerGL *stateManager = GetStateManagerGL(context); |
Jonah Ryan-Davis | beb0eb2 | 2019-06-14 15:10:33 -0400 | [diff] [blame] | 431 | const angle::FeaturesGL &features = GetFeaturesGL(context); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 432 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 433 | // Clip read area to framebuffer. |
| 434 | const gl::Extents fbSize = getState().getReadAttachment()->getSize(); |
| 435 | const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height); |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 436 | gl::Rectangle clippedArea; |
| 437 | if (!ClipRectangle(area, fbRect, &clippedArea)) |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 438 | { |
| 439 | // nothing to read |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 440 | return angle::Result::Continue; |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 441 | } |
| 442 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 443 | PixelPackState packState = context->getState().getPackState(); |
Corentin Wallez | 336129f | 2017-10-17 15:55:40 -0400 | [diff] [blame] | 444 | const gl::Buffer *packBuffer = |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 445 | context->getState().getTargetBuffer(gl::BufferBinding::PixelPack); |
Jamie Madill | 87de362 | 2015-03-16 10:41:44 -0400 | [diff] [blame] | 446 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame] | 447 | nativegl::ReadPixelsFormat readPixelsFormat = |
Jonah Ryan-Davis | beb0eb2 | 2019-06-14 15:10:33 -0400 | [diff] [blame] | 448 | nativegl::GetReadPixelsFormat(functions, features, format, type); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 449 | GLenum readFormat = readPixelsFormat.format; |
| 450 | GLenum readType = readPixelsFormat.type; |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 451 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 452 | stateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferID); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 453 | |
Jonah Ryan-Davis | beb0eb2 | 2019-06-14 15:10:33 -0400 | [diff] [blame] | 454 | bool useOverlappingRowsWorkaround = features.packOverlappingRowsSeparatelyPackBuffer.enabled && |
| 455 | packBuffer && packState.rowLength != 0 && |
| 456 | packState.rowLength < clippedArea.width; |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 457 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 458 | GLubyte *outPtr = static_cast<GLubyte *>(pixels); |
| 459 | int leftClip = clippedArea.x - area.x; |
| 460 | int topClip = clippedArea.y - area.y; |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 461 | if (leftClip || topClip) |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 462 | { |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 463 | // Adjust destination to match portion clipped off left and/or top. |
| 464 | const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(readFormat, readType); |
| 465 | |
| 466 | GLuint rowBytes = 0; |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 467 | ANGLE_CHECK_GL_MATH(contextGL, |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 468 | glFormat.computeRowPitch(readType, area.width, packState.alignment, |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 469 | packState.rowLength, &rowBytes)); |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 470 | outPtr += leftClip * glFormat.pixelBytes + topClip * rowBytes; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 471 | } |
| 472 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 473 | if (packState.rowLength == 0 && clippedArea.width != area.width) |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 474 | { |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 475 | // No rowLength was specified so it will derive from read width, but clipping changed the |
| 476 | // read width. Use the original width so we fill the user's buffer as they intended. |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 477 | packState.rowLength = area.width; |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 478 | } |
Corentin Wallez | 886de36 | 2016-09-27 10:49:35 -0400 | [diff] [blame] | 479 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 480 | // We want to use rowLength, but that might not be supported. |
| 481 | bool cannotSetDesiredRowLength = |
| 482 | packState.rowLength && !GetImplAs<ContextGL>(context)->getNativeExtensions().packSubimage; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 483 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 484 | if (cannotSetDesiredRowLength || useOverlappingRowsWorkaround) |
| 485 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 486 | return readPixelsRowByRow(context, clippedArea, readFormat, readType, packState, outPtr); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 487 | } |
Jamie Madill | 1395134 | 2018-09-30 15:24:28 -0400 | [diff] [blame] | 488 | |
| 489 | bool useLastRowPaddingWorkaround = false; |
Jonah Ryan-Davis | beb0eb2 | 2019-06-14 15:10:33 -0400 | [diff] [blame] | 490 | if (features.packLastRowSeparatelyForPaddingInclusion.enabled) |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 491 | { |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 492 | ANGLE_TRY(ShouldApplyLastRowPaddingWorkaround( |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 493 | contextGL, gl::Extents(clippedArea.width, clippedArea.height, 1), packState, packBuffer, |
| 494 | readFormat, readType, false, outPtr, &useLastRowPaddingWorkaround)); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 495 | } |
| 496 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 497 | return readPixelsAllAtOnce(context, clippedArea, readFormat, readType, packState, outPtr, |
Jamie Madill | 1395134 | 2018-09-30 15:24:28 -0400 | [diff] [blame] | 498 | useLastRowPaddingWorkaround); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 499 | } |
| 500 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 501 | angle::Result FramebufferGL::blit(const gl::Context *context, |
| 502 | const gl::Rectangle &sourceArea, |
| 503 | const gl::Rectangle &destArea, |
| 504 | GLbitfield mask, |
| 505 | GLenum filter) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 506 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 507 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 508 | StateManagerGL *stateManager = GetStateManagerGL(context); |
Jonah Ryan-Davis | 7151fe5 | 2019-07-17 15:15:27 -0400 | [diff] [blame] | 509 | const angle::FeaturesGL &features = GetFeaturesGL(context); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 510 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 511 | const Framebuffer *sourceFramebuffer = context->getState().getReadFramebuffer(); |
| 512 | const Framebuffer *destFramebuffer = context->getState().getDrawFramebuffer(); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 513 | |
Jamie Madill | 4e71b2b | 2019-07-08 13:23:38 -0400 | [diff] [blame] | 514 | const FramebufferAttachment *colorReadAttachment = sourceFramebuffer->getReadColorAttachment(); |
Corentin Wallez | 4596a76 | 2016-12-20 14:50:18 -0500 | [diff] [blame] | 515 | |
| 516 | GLsizei readAttachmentSamples = 0; |
| 517 | if (colorReadAttachment != nullptr) |
| 518 | { |
| 519 | readAttachmentSamples = colorReadAttachment->getSamples(); |
| 520 | } |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 521 | |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 522 | bool needManualColorBlit = false; |
| 523 | |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 524 | // TODO(cwallez) when the filter is LINEAR and both source and destination are SRGB, we |
| 525 | // could avoid doing a manual blit. |
| 526 | |
| 527 | // Prior to OpenGL 4.4 BlitFramebuffer (section 18.3.1 of GL 4.3 core profile) reads: |
| 528 | // When values are taken from the read buffer, no linearization is performed, even |
| 529 | // if the format of the buffer is SRGB. |
| 530 | // Starting from OpenGL 4.4 (section 18.3.1) it reads: |
| 531 | // When values are taken from the read buffer, if FRAMEBUFFER_SRGB is enabled and the |
| 532 | // value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the framebuffer attachment |
| 533 | // corresponding to the read buffer is SRGB, the red, green, and blue components are |
| 534 | // converted from the non-linear sRGB color space according [...]. |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 535 | { |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 536 | bool sourceSRGB = |
| 537 | colorReadAttachment != nullptr && colorReadAttachment->getColorEncoding() == GL_SRGB; |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 538 | needManualColorBlit = |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 539 | needManualColorBlit || (sourceSRGB && functions->isAtMostGL(gl::Version(4, 3))); |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 540 | } |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 541 | |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 542 | // Prior to OpenGL 4.2 BlitFramebuffer (section 4.3.2 of GL 4.1 core profile) reads: |
| 543 | // Blit operations bypass the fragment pipeline. The only fragment operations which |
| 544 | // affect a blit are the pixel ownership test and scissor test. |
| 545 | // Starting from OpenGL 4.2 (section 4.3.2) it reads: |
| 546 | // When values are written to the draw buffers, blit operations bypass the fragment |
| 547 | // pipeline. The only fragment operations which affect a blit are the pixel ownership |
| 548 | // test, the scissor test and sRGB conversion. |
| 549 | if (!needManualColorBlit) |
| 550 | { |
| 551 | bool destSRGB = false; |
| 552 | for (size_t i = 0; i < destFramebuffer->getDrawbufferStateCount(); ++i) |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 553 | { |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 554 | const FramebufferAttachment *attachment = destFramebuffer->getDrawBuffer(i); |
| 555 | if (attachment && attachment->getColorEncoding() == GL_SRGB) |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 556 | { |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 557 | destSRGB = true; |
| 558 | break; |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 559 | } |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 560 | } |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 561 | |
| 562 | needManualColorBlit = |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 563 | needManualColorBlit || (destSRGB && functions->isAtMostGL(gl::Version(4, 1))); |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | // Enable FRAMEBUFFER_SRGB if needed |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 567 | stateManager->setFramebufferSRGBEnabledForFramebuffer(context, true, this); |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 568 | |
| 569 | GLenum blitMask = mask; |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 570 | if (needManualColorBlit && (mask & GL_COLOR_BUFFER_BIT) && readAttachmentSamples <= 1) |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 571 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 572 | BlitGL *blitter = GetBlitGL(context); |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 573 | ANGLE_TRY(blitter->blitColorBufferWithShader(context, sourceFramebuffer, destFramebuffer, |
| 574 | sourceArea, destArea, filter)); |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 575 | blitMask &= ~GL_COLOR_BUFFER_BIT; |
| 576 | } |
| 577 | |
| 578 | if (blitMask == 0) |
| 579 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 580 | return angle::Result::Continue; |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 584 | stateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID()); |
| 585 | stateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 586 | |
Jonah Ryan-Davis | 7151fe5 | 2019-07-17 15:15:27 -0400 | [diff] [blame] | 587 | if (features.adjustSrcDstRegionBlitFramebuffer.enabled) |
| 588 | { |
| 589 | gl::Rectangle newSourceArea; |
| 590 | gl::Rectangle newDestArea; |
| 591 | // This workaround is taken from chromium: http://crbug.com/830046 |
| 592 | if (adjustSrcDstRegion(context, sourceArea, destArea, &newSourceArea, &newDestArea) == |
| 593 | angle::Result::Continue) |
| 594 | { |
| 595 | functions->blitFramebuffer(newSourceArea.x, newSourceArea.y, newSourceArea.x1(), |
| 596 | newSourceArea.y1(), newDestArea.x, newDestArea.y, |
| 597 | newDestArea.x1(), newDestArea.y1(), blitMask, filter); |
| 598 | } |
| 599 | } |
| 600 | else |
| 601 | { |
| 602 | functions->blitFramebuffer(sourceArea.x, sourceArea.y, sourceArea.x1(), sourceArea.y1(), |
| 603 | destArea.x, destArea.y, destArea.x1(), destArea.y1(), blitMask, |
| 604 | filter); |
| 605 | } |
| 606 | |
| 607 | return angle::Result::Continue; |
| 608 | } |
| 609 | |
| 610 | angle::Result FramebufferGL::adjustSrcDstRegion(const gl::Context *context, |
| 611 | const gl::Rectangle &sourceArea, |
| 612 | const gl::Rectangle &destArea, |
| 613 | gl::Rectangle *newSourceArea, |
| 614 | gl::Rectangle *newDestArea) |
| 615 | { |
| 616 | const Framebuffer *sourceFramebuffer = context->getState().getReadFramebuffer(); |
| 617 | const Framebuffer *destFramebuffer = context->getState().getDrawFramebuffer(); |
| 618 | |
| 619 | gl::Extents readSize = sourceFramebuffer->getExtents(); |
| 620 | gl::Extents drawSize = destFramebuffer->getExtents(); |
| 621 | |
| 622 | CheckedNumeric<GLint> sourceWidthTemp = sourceArea.x1(); |
| 623 | sourceWidthTemp -= sourceArea.x; |
| 624 | CheckedNumeric<GLint> sourceHeightTemp = sourceArea.y1(); |
| 625 | sourceHeightTemp -= sourceArea.y; |
| 626 | CheckedNumeric<GLint> destWidthTemp = destArea.x1(); |
| 627 | destWidthTemp -= destArea.x; |
| 628 | CheckedNumeric<GLint> destHeightTemp = destArea.y1(); |
| 629 | destHeightTemp -= destArea.y; |
| 630 | |
| 631 | GLint sourceX = sourceArea.x1() > sourceArea.x ? sourceArea.x : sourceArea.x1(); |
| 632 | GLint sourceY = sourceArea.y1() > sourceArea.y ? sourceArea.y : sourceArea.y1(); |
| 633 | GLuint sourceWidth = angle::base::checked_cast<GLuint>(sourceWidthTemp.Abs().ValueOrDefault(0)); |
| 634 | GLuint sourceHeight = |
| 635 | angle::base::checked_cast<GLuint>(sourceHeightTemp.Abs().ValueOrDefault(0)); |
| 636 | |
| 637 | GLint destX = destArea.x1() > destArea.x ? destArea.x : destArea.x1(); |
| 638 | GLint destY = destArea.y1() > destArea.y ? destArea.y : destArea.y1(); |
| 639 | GLuint destWidth = angle::base::checked_cast<GLuint>(destWidthTemp.Abs().ValueOrDefault(0)); |
| 640 | GLuint destHeight = angle::base::checked_cast<GLuint>(destHeightTemp.Abs().ValueOrDefault(0)); |
| 641 | |
| 642 | if (destWidth == 0 || sourceWidth == 0 || destHeight == 0 || sourceHeight == 0) |
| 643 | { |
| 644 | return angle::Result::Stop; |
| 645 | } |
| 646 | |
| 647 | gl::Rectangle sourceBounds(0, 0, readSize.width, readSize.height); |
| 648 | gl::Rectangle sourceRegion(sourceX, sourceY, sourceWidth, sourceHeight); |
| 649 | |
| 650 | gl::Rectangle destBounds(0, 0, drawSize.width, drawSize.height); |
| 651 | gl::Rectangle destRegion(destX, destY, destWidth, destHeight); |
| 652 | |
| 653 | if (!ClipRectangle(destBounds, destRegion, nullptr)) |
| 654 | { |
| 655 | return angle::Result::Stop; |
| 656 | } |
| 657 | |
| 658 | bool xFlipped = ((sourceArea.x1() > sourceArea.x) && (destArea.x1() < destArea.x)) || |
| 659 | ((sourceArea.x1() < sourceArea.x) && (destArea.x1() > destArea.x)); |
| 660 | bool yFlipped = ((sourceArea.y1() > sourceArea.y) && (destArea.y1() < destArea.y)) || |
| 661 | ((sourceArea.y1() < sourceArea.y) && (destArea.y1() > destArea.y)); |
| 662 | |
| 663 | if (!destBounds.encloses(destRegion)) |
| 664 | { |
| 665 | // destRegion is not within destBounds. We want to adjust it to a |
| 666 | // reasonable size. This is done by halving the destRegion until it is at |
| 667 | // most twice the size of the framebuffer. We cut it in half instead |
| 668 | // of arbitrarily shrinking it to fit so that we don't end up with |
| 669 | // non-power-of-two scale factors which could mess up pixel interpolation. |
| 670 | // Naively clipping the dst rect and then proportionally sizing the |
| 671 | // src rect yields incorrect results. |
| 672 | |
| 673 | GLuint destXHalvings = 0; |
| 674 | GLuint destYHalvings = 0; |
| 675 | GLint destOriginX = destX; |
| 676 | GLint destOriginY = destY; |
| 677 | |
| 678 | GLint destClippedWidth = destRegion.width; |
| 679 | while (destClippedWidth > 2 * destBounds.width) |
| 680 | { |
| 681 | destClippedWidth = destClippedWidth / 2; |
| 682 | destXHalvings++; |
| 683 | } |
| 684 | |
| 685 | GLint destClippedHeight = destRegion.height; |
| 686 | while (destClippedHeight > 2 * destBounds.height) |
| 687 | { |
| 688 | destClippedHeight = destClippedHeight / 2; |
| 689 | destYHalvings++; |
| 690 | } |
| 691 | |
| 692 | // Before this block, we check that the two rectangles intersect. |
| 693 | // Now, compute the location of a new region origin such that we use the |
| 694 | // scaled dimensions but the new region has the same intersection as the |
| 695 | // original region. |
| 696 | |
| 697 | GLint left = destRegion.x0(); |
| 698 | GLint right = destRegion.x1(); |
| 699 | GLint top = destRegion.y0(); |
| 700 | GLint bottom = destRegion.y1(); |
| 701 | |
| 702 | GLint extraXOffset = 0; |
| 703 | if (left >= 0 && left < destBounds.width) |
| 704 | { |
| 705 | // Left edge is in-bounds |
| 706 | destOriginX = destX; |
| 707 | } |
| 708 | else if (right > 0 && right <= destBounds.width) |
| 709 | { |
| 710 | // Right edge is in-bounds |
| 711 | destOriginX = right - destClippedWidth; |
| 712 | } |
| 713 | else |
| 714 | { |
| 715 | // Region completely spans bounds |
| 716 | extraXOffset = (destRegion.width - destClippedWidth) / 2; |
| 717 | destOriginX = destX + extraXOffset; |
| 718 | } |
| 719 | |
| 720 | GLint extraYOffset = 0; |
| 721 | if (top >= 0 && top < destBounds.height) |
| 722 | { |
| 723 | // Top edge is in-bounds |
| 724 | destOriginY = destY; |
| 725 | } |
| 726 | else if (bottom > 0 && bottom <= destBounds.height) |
| 727 | { |
| 728 | // Bottom edge is in-bounds |
| 729 | destOriginY = bottom - destClippedHeight; |
| 730 | } |
| 731 | else |
| 732 | { |
| 733 | // Region completely spans bounds |
| 734 | extraYOffset = (destRegion.height - destClippedHeight) / 2; |
| 735 | destOriginY = destY + extraYOffset; |
| 736 | } |
| 737 | |
| 738 | destRegion = gl::Rectangle(destOriginX, destOriginY, destClippedWidth, destClippedHeight); |
| 739 | |
| 740 | // Offsets from the bottom left corner of the original region to |
| 741 | // the bottom left corner of the clipped region. |
| 742 | // This value (after it is scaled) is the respective offset we will apply |
| 743 | // to the src origin. |
| 744 | |
| 745 | CheckedNumeric<GLuint> checkedXOffset(destRegion.x - destX - extraXOffset / 2); |
| 746 | CheckedNumeric<GLuint> checkedYOffset(destRegion.y - destY - extraYOffset / 2); |
| 747 | |
| 748 | // if X/Y is reversed, use the top/right out-of-bounds region to compute |
| 749 | // the origin offset instead of the left/bottom out-of-bounds region |
| 750 | if (xFlipped) |
| 751 | { |
| 752 | checkedXOffset = (destX + destWidth - destRegion.x1() + extraXOffset / 2); |
| 753 | } |
| 754 | if (yFlipped) |
| 755 | { |
| 756 | checkedYOffset = (destY + destHeight - destRegion.y1() + extraYOffset / 2); |
| 757 | } |
| 758 | |
| 759 | // These offsets should never overflow |
| 760 | GLuint xOffset, yOffset; |
| 761 | if (!checkedXOffset.AssignIfValid(&xOffset) || !checkedYOffset.AssignIfValid(&yOffset)) |
| 762 | { |
| 763 | UNREACHABLE(); |
| 764 | return angle::Result::Stop; |
| 765 | } |
| 766 | |
| 767 | // Adjust the src region by the same factor |
| 768 | sourceRegion = gl::Rectangle( |
| 769 | sourceX + (xOffset >> destXHalvings), sourceY + (yOffset >> destYHalvings), |
| 770 | sourceRegion.width >> destXHalvings, sourceRegion.height >> destYHalvings); |
| 771 | |
| 772 | // if the src was scaled to 0, set it to 1 so the src is non-empty |
| 773 | if (sourceRegion.width == 0) |
| 774 | { |
| 775 | sourceRegion.width = 1; |
| 776 | } |
| 777 | if (sourceRegion.height == 0) |
| 778 | { |
| 779 | sourceRegion.height = 1; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | if (!sourceBounds.encloses(sourceRegion)) |
| 784 | { |
| 785 | // sourceRegion is not within sourceBounds. We want to adjust it to a |
| 786 | // reasonable size. This is done by halving the sourceRegion until it is at |
| 787 | // most twice the size of the framebuffer. We cut it in half instead |
| 788 | // of arbitrarily shrinking it to fit so that we don't end up with |
| 789 | // non-power-of-two scale factors which could mess up pixel interpolation. |
| 790 | // Naively clipping the source rect and then proportionally sizing the |
| 791 | // dest rect yields incorrect results. |
| 792 | |
| 793 | GLuint sourceXHalvings = 0; |
| 794 | GLuint sourceYHalvings = 0; |
| 795 | GLint sourceOriginX = sourceX; |
| 796 | GLint sourceOriginY = sourceY; |
| 797 | |
| 798 | GLint sourceClippedWidth = sourceRegion.width; |
| 799 | while (sourceClippedWidth > 2 * sourceBounds.width) |
| 800 | { |
| 801 | sourceClippedWidth = sourceClippedWidth / 2; |
| 802 | sourceXHalvings++; |
| 803 | } |
| 804 | |
| 805 | GLint sourceClippedHeight = sourceRegion.height; |
| 806 | while (sourceClippedHeight > 2 * sourceBounds.height) |
| 807 | { |
| 808 | sourceClippedHeight = sourceClippedHeight / 2; |
| 809 | sourceYHalvings++; |
| 810 | } |
| 811 | |
| 812 | // Before this block, we check that the two rectangles intersect. |
| 813 | // Now, compute the location of a new region origin such that we use the |
| 814 | // scaled dimensions but the new region has the same intersection as the |
| 815 | // original region. |
| 816 | |
| 817 | GLint left = sourceRegion.x0(); |
| 818 | GLint right = sourceRegion.x1(); |
| 819 | GLint top = sourceRegion.y0(); |
| 820 | GLint bottom = sourceRegion.y1(); |
| 821 | |
| 822 | GLint extraXOffset = 0; |
| 823 | if (left >= 0 && left < sourceBounds.width) |
| 824 | { |
| 825 | // Left edge is in-bounds |
| 826 | sourceOriginX = sourceX; |
| 827 | } |
| 828 | else if (right > 0 && right <= sourceBounds.width) |
| 829 | { |
| 830 | // Right edge is in-bounds |
| 831 | sourceOriginX = right - sourceClippedWidth; |
| 832 | } |
| 833 | else |
| 834 | { |
| 835 | // Region completely spans bounds |
| 836 | extraXOffset = (sourceRegion.width - sourceClippedWidth) / 2; |
| 837 | sourceOriginX = sourceX + extraXOffset; |
| 838 | } |
| 839 | |
| 840 | GLint extraYOffset = 0; |
| 841 | if (top >= 0 && top < sourceBounds.height) |
| 842 | { |
| 843 | // Top edge is in-bounds |
| 844 | sourceOriginY = sourceY; |
| 845 | } |
| 846 | else if (bottom > 0 && bottom <= sourceBounds.height) |
| 847 | { |
| 848 | // Bottom edge is in-bounds |
| 849 | sourceOriginY = bottom - sourceClippedHeight; |
| 850 | } |
| 851 | else |
| 852 | { |
| 853 | // Region completely spans bounds |
| 854 | extraYOffset = (sourceRegion.height - sourceClippedHeight) / 2; |
| 855 | sourceOriginY = sourceY + extraYOffset; |
| 856 | } |
| 857 | |
| 858 | sourceRegion = |
| 859 | gl::Rectangle(sourceOriginX, sourceOriginY, sourceClippedWidth, sourceClippedHeight); |
| 860 | |
| 861 | // Offsets from the bottom left corner of the original region to |
| 862 | // the bottom left corner of the clipped region. |
| 863 | // This value (after it is scaled) is the respective offset we will apply |
| 864 | // to the dest origin. |
| 865 | |
| 866 | CheckedNumeric<GLuint> checkedXOffset(sourceRegion.x - sourceX - extraXOffset / 2); |
| 867 | CheckedNumeric<GLuint> checkedYOffset(sourceRegion.y - sourceY - extraYOffset / 2); |
| 868 | |
| 869 | // if X/Y is reversed, use the top/right out-of-bounds region to compute |
| 870 | // the origin offset instead of the left/bottom out-of-bounds region |
| 871 | if (xFlipped) |
| 872 | { |
| 873 | checkedXOffset = (sourceX + sourceWidth - sourceRegion.x1() + extraXOffset / 2); |
| 874 | } |
| 875 | if (yFlipped) |
| 876 | { |
| 877 | checkedYOffset = (sourceY + sourceHeight - sourceRegion.y1() + extraYOffset / 2); |
| 878 | } |
| 879 | |
| 880 | // These offsets should never overflow |
| 881 | GLuint xOffset, yOffset; |
| 882 | if (!checkedXOffset.AssignIfValid(&xOffset) || !checkedYOffset.AssignIfValid(&yOffset)) |
| 883 | { |
| 884 | UNREACHABLE(); |
| 885 | return angle::Result::Stop; |
| 886 | } |
| 887 | |
| 888 | // Adjust the dest region by the same factor |
| 889 | destRegion = gl::Rectangle( |
| 890 | destX + (xOffset >> sourceXHalvings), destY + (yOffset >> sourceYHalvings), |
| 891 | destRegion.width >> sourceXHalvings, destRegion.height >> sourceYHalvings); |
| 892 | } |
| 893 | // Set the src and dst endpoints. If they were previously flipped, |
| 894 | // set them as flipped. |
| 895 | *newSourceArea = gl::Rectangle( |
| 896 | sourceArea.x0() < sourceArea.x1() ? sourceRegion.x0() : sourceRegion.x1(), |
| 897 | sourceArea.y0() < sourceArea.y1() ? sourceRegion.y0() : sourceRegion.y1(), |
| 898 | sourceArea.x0() < sourceArea.x1() ? sourceRegion.width : -sourceRegion.width, |
| 899 | sourceArea.y0() < sourceArea.y1() ? sourceRegion.height : -sourceRegion.height); |
| 900 | |
| 901 | *newDestArea = |
| 902 | gl::Rectangle(destArea.x0() < destArea.x1() ? destRegion.x0() : destRegion.x1(), |
| 903 | destArea.y0() < destArea.y1() ? destRegion.y0() : destRegion.y1(), |
| 904 | destArea.x0() < destArea.x1() ? destRegion.width : -destRegion.width, |
| 905 | destArea.y0() < destArea.y1() ? destRegion.height : -destRegion.height); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 906 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 907 | return angle::Result::Continue; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 908 | } |
| 909 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 910 | angle::Result FramebufferGL::getSamplePosition(const gl::Context *context, |
| 911 | size_t index, |
| 912 | GLfloat *xy) const |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 913 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 914 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 915 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 916 | |
| 917 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 918 | functions->getMultisamplefv(GL_SAMPLE_POSITION, static_cast<GLuint>(index), xy); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 919 | return angle::Result::Continue; |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 920 | } |
| 921 | |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 922 | bool FramebufferGL::shouldSyncStateBeforeCheckStatus() const |
| 923 | { |
| 924 | return true; |
| 925 | } |
| 926 | |
Kenneth Russell | ce8602a | 2017-10-03 18:23:08 -0700 | [diff] [blame] | 927 | bool FramebufferGL::checkStatus(const gl::Context *context) const |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 928 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 929 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 930 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 931 | |
| 932 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 933 | GLenum status = functions->checkFramebufferStatus(GL_FRAMEBUFFER); |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 934 | if (status != GL_FRAMEBUFFER_COMPLETE) |
| 935 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 936 | WARN() << "GL framebuffer returned incomplete."; |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 937 | } |
| 938 | return (status == GL_FRAMEBUFFER_COMPLETE); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 939 | } |
| 940 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 941 | angle::Result FramebufferGL::syncState(const gl::Context *context, |
| 942 | const gl::Framebuffer::DirtyBits &dirtyBits) |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 943 | { |
| 944 | // Don't need to sync state for the default FBO. |
| 945 | if (mIsDefault) |
| 946 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 947 | return angle::Result::Continue; |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 948 | } |
| 949 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 950 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 951 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 952 | |
| 953 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 954 | |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 955 | // A pointer to one of the attachments for which the texture or the render buffer is not zero. |
| 956 | const FramebufferAttachment *attachment = nullptr; |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 957 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 958 | for (auto dirtyBit : dirtyBits) |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 959 | { |
| 960 | switch (dirtyBit) |
| 961 | { |
| 962 | case Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT: |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 963 | { |
| 964 | const FramebufferAttachment *newAttachment = mState.getDepthAttachment(); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 965 | BindFramebufferAttachment(functions, GL_DEPTH_ATTACHMENT, newAttachment); |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 966 | if (newAttachment) |
| 967 | { |
| 968 | attachment = newAttachment; |
| 969 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 970 | break; |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 971 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 972 | case Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT: |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 973 | { |
| 974 | const FramebufferAttachment *newAttachment = mState.getStencilAttachment(); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 975 | BindFramebufferAttachment(functions, GL_STENCIL_ATTACHMENT, newAttachment); |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 976 | if (newAttachment) |
| 977 | { |
| 978 | attachment = newAttachment; |
| 979 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 980 | break; |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 981 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 982 | case Framebuffer::DIRTY_BIT_DRAW_BUFFERS: |
| 983 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 984 | const auto &drawBuffers = mState.getDrawBufferStates(); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 985 | functions->drawBuffers(static_cast<GLsizei>(drawBuffers.size()), |
| 986 | drawBuffers.data()); |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 987 | mAppliedEnabledDrawBuffers = mState.getEnabledDrawBuffers(); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 988 | break; |
| 989 | } |
| 990 | case Framebuffer::DIRTY_BIT_READ_BUFFER: |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 991 | functions->readBuffer(mState.getReadBufferState()); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 992 | break; |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 993 | case Framebuffer::DIRTY_BIT_DEFAULT_WIDTH: |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 994 | functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_WIDTH, |
| 995 | mState.getDefaultWidth()); |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 996 | break; |
| 997 | case Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT: |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 998 | functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_HEIGHT, |
| 999 | mState.getDefaultHeight()); |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 1000 | break; |
| 1001 | case Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES: |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1002 | functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES, |
| 1003 | mState.getDefaultSamples()); |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 1004 | break; |
| 1005 | case Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS: |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1006 | functions->framebufferParameteri( |
Geoff Lang | 9201943 | 2017-11-20 13:09:34 -0500 | [diff] [blame] | 1007 | GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS, |
| 1008 | gl::ConvertToGLBoolean(mState.getDefaultFixedSampleLocations())); |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 1009 | break; |
Jiawei Shao | b1e9138 | 2018-05-17 14:33:55 +0800 | [diff] [blame] | 1010 | case Framebuffer::DIRTY_BIT_DEFAULT_LAYERS: |
| 1011 | functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_LAYERS_EXT, |
| 1012 | mState.getDefaultLayers()); |
| 1013 | break; |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1014 | default: |
| 1015 | { |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 1016 | static_assert(Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0, "FB dirty bits"); |
| 1017 | if (dirtyBit < Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX) |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 1018 | { |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 1019 | size_t index = |
| 1020 | static_cast<size_t>(dirtyBit - Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0); |
| 1021 | const FramebufferAttachment *newAttachment = mState.getColorAttachment(index); |
| 1022 | BindFramebufferAttachment(functions, |
| 1023 | static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + index), |
| 1024 | newAttachment); |
| 1025 | if (newAttachment) |
| 1026 | { |
| 1027 | attachment = newAttachment; |
| 1028 | } |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 1029 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1030 | break; |
| 1031 | } |
| 1032 | } |
| 1033 | } |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 1034 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 1035 | if (attachment && mState.id() == context->getState().getDrawFramebuffer()->id()) |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 1036 | { |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 1037 | stateManager->updateMultiviewBaseViewLayerIndexUniform(context->getState().getProgram(), |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1038 | getState()); |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 1039 | } |
Jamie Madill | 19fa1c6 | 2018-03-08 09:47:21 -0500 | [diff] [blame] | 1040 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1041 | return angle::Result::Continue; |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1042 | } |
| 1043 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 1044 | GLuint FramebufferGL::getFramebufferID() const |
| 1045 | { |
| 1046 | return mFramebufferID; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 1047 | } |
| 1048 | |
Geoff Lang | 1d2c41d | 2016-10-19 16:14:46 -0700 | [diff] [blame] | 1049 | bool FramebufferGL::isDefault() const |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1050 | { |
Geoff Lang | 1d2c41d | 2016-10-19 16:14:46 -0700 | [diff] [blame] | 1051 | return mIsDefault; |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1052 | } |
| 1053 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 1054 | void FramebufferGL::syncClearState(const gl::Context *context, GLbitfield mask) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1055 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1056 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 1057 | |
| 1058 | if (functions->standard == STANDARD_GL_DESKTOP) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1059 | { |
Jamie Madill | 4e71b2b | 2019-07-08 13:23:38 -0400 | [diff] [blame] | 1060 | StateManagerGL *stateManager = GetStateManagerGL(context); |
Jonah Ryan-Davis | beb0eb2 | 2019-06-14 15:10:33 -0400 | [diff] [blame] | 1061 | const angle::FeaturesGL &features = GetFeaturesGL(context); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1062 | |
Jonah Ryan-Davis | beb0eb2 | 2019-06-14 15:10:33 -0400 | [diff] [blame] | 1063 | if (features.doesSRGBClearsOnLinearFramebufferAttachments.enabled && |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 1064 | (mask & GL_COLOR_BUFFER_BIT) != 0 && !mIsDefault) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1065 | { |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 1066 | bool hasSRGBAttachment = false; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1067 | for (const auto &attachment : mState.getColorAttachments()) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1068 | { |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 1069 | if (attachment.isAttached() && attachment.getColorEncoding() == GL_SRGB) |
| 1070 | { |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 1071 | hasSRGBAttachment = true; |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 1072 | break; |
| 1073 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1074 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1075 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1076 | stateManager->setFramebufferSRGBEnabled(context, hasSRGBAttachment); |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 1077 | } |
| 1078 | else |
| 1079 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1080 | stateManager->setFramebufferSRGBEnabled(context, !mIsDefault); |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 1081 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 1085 | void FramebufferGL::syncClearBufferState(const gl::Context *context, |
| 1086 | GLenum buffer, |
| 1087 | GLint drawBuffer) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1088 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1089 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 1090 | |
| 1091 | if (functions->standard == STANDARD_GL_DESKTOP) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1092 | { |
Jamie Madill | 4e71b2b | 2019-07-08 13:23:38 -0400 | [diff] [blame] | 1093 | StateManagerGL *stateManager = GetStateManagerGL(context); |
Jonah Ryan-Davis | beb0eb2 | 2019-06-14 15:10:33 -0400 | [diff] [blame] | 1094 | const angle::FeaturesGL &features = GetFeaturesGL(context); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1095 | |
Jonah Ryan-Davis | beb0eb2 | 2019-06-14 15:10:33 -0400 | [diff] [blame] | 1096 | if (features.doesSRGBClearsOnLinearFramebufferAttachments.enabled && buffer == GL_COLOR && |
| 1097 | !mIsDefault) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1098 | { |
| 1099 | // If doing a clear on a color buffer, set SRGB blend enabled only if the color buffer |
| 1100 | // is an SRGB format. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1101 | const auto &drawbufferState = mState.getDrawBufferStates(); |
| 1102 | const auto &colorAttachments = mState.getColorAttachments(); |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1103 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1104 | const FramebufferAttachment *attachment = nullptr; |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1105 | if (drawbufferState[drawBuffer] >= GL_COLOR_ATTACHMENT0 && |
| 1106 | drawbufferState[drawBuffer] < GL_COLOR_ATTACHMENT0 + colorAttachments.size()) |
| 1107 | { |
| 1108 | size_t attachmentIdx = |
| 1109 | static_cast<size_t>(drawbufferState[drawBuffer] - GL_COLOR_ATTACHMENT0); |
| 1110 | attachment = &colorAttachments[attachmentIdx]; |
| 1111 | } |
| 1112 | |
| 1113 | if (attachment != nullptr) |
| 1114 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1115 | stateManager->setFramebufferSRGBEnabled(context, |
| 1116 | attachment->getColorEncoding() == GL_SRGB); |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | else |
| 1120 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1121 | stateManager->setFramebufferSRGBEnabled(context, !mIsDefault); |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | } |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 1125 | |
| 1126 | bool FramebufferGL::modifyInvalidateAttachmentsForEmulatedDefaultFBO( |
| 1127 | size_t count, |
| 1128 | const GLenum *attachments, |
| 1129 | std::vector<GLenum> *modifiedAttachments) const |
| 1130 | { |
| 1131 | bool needsModification = mIsDefault && mFramebufferID != 0; |
| 1132 | if (!needsModification) |
| 1133 | { |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | modifiedAttachments->resize(count); |
| 1138 | for (size_t i = 0; i < count; i++) |
| 1139 | { |
| 1140 | switch (attachments[i]) |
| 1141 | { |
| 1142 | case GL_COLOR: |
| 1143 | (*modifiedAttachments)[i] = GL_COLOR_ATTACHMENT0; |
| 1144 | break; |
| 1145 | |
| 1146 | case GL_DEPTH: |
| 1147 | (*modifiedAttachments)[i] = GL_DEPTH_ATTACHMENT; |
| 1148 | break; |
| 1149 | |
| 1150 | case GL_STENCIL: |
| 1151 | (*modifiedAttachments)[i] = GL_STENCIL_ATTACHMENT; |
| 1152 | break; |
| 1153 | |
| 1154 | default: |
| 1155 | UNREACHABLE(); |
| 1156 | break; |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | return true; |
| 1161 | } |
| 1162 | |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 1163 | angle::Result FramebufferGL::readPixelsRowByRow(const gl::Context *context, |
| 1164 | const gl::Rectangle &area, |
| 1165 | GLenum format, |
| 1166 | GLenum type, |
| 1167 | const gl::PixelPackState &pack, |
| 1168 | GLubyte *pixels) const |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1169 | { |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 1170 | ContextGL *contextGL = GetImplAs<ContextGL>(context); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1171 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 1172 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 1173 | |
Geoff Lang | ca27139 | 2017-04-05 12:30:00 -0400 | [diff] [blame] | 1174 | const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 1175 | |
| 1176 | GLuint rowBytes = 0; |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 1177 | ANGLE_CHECK_GL_MATH(contextGL, glFormat.computeRowPitch(type, area.width, pack.alignment, |
| 1178 | pack.rowLength, &rowBytes)); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1179 | GLuint skipBytes = 0; |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 1180 | ANGLE_CHECK_GL_MATH(contextGL, |
| 1181 | glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes)); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1182 | |
| 1183 | gl::PixelPackState directPack; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1184 | directPack.alignment = 1; |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1185 | stateManager->setPixelPackState(directPack); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1186 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 1187 | pixels += skipBytes; |
| 1188 | for (GLint y = area.y; y < area.y + area.height; ++y) |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1189 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1190 | functions->readPixels(area.x, y, area.width, 1, format, type, pixels); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 1191 | pixels += rowBytes; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1192 | } |
| 1193 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1194 | return angle::Result::Continue; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1195 | } |
| 1196 | |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 1197 | angle::Result FramebufferGL::readPixelsAllAtOnce(const gl::Context *context, |
| 1198 | const gl::Rectangle &area, |
| 1199 | GLenum format, |
| 1200 | GLenum type, |
| 1201 | const gl::PixelPackState &pack, |
| 1202 | GLubyte *pixels, |
| 1203 | bool readLastRowSeparately) const |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1204 | { |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 1205 | ContextGL *contextGL = GetImplAs<ContextGL>(context); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1206 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 1207 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 1208 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 1209 | GLint height = area.height - readLastRowSeparately; |
| 1210 | if (height > 0) |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1211 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1212 | stateManager->setPixelPackState(pack); |
| 1213 | functions->readPixels(area.x, area.y, area.width, height, format, type, pixels); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1214 | } |
| 1215 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 1216 | if (readLastRowSeparately) |
| 1217 | { |
| 1218 | const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1219 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 1220 | GLuint rowBytes = 0; |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 1221 | ANGLE_CHECK_GL_MATH(contextGL, glFormat.computeRowPitch(type, area.width, pack.alignment, |
| 1222 | pack.rowLength, &rowBytes)); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 1223 | GLuint skipBytes = 0; |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame] | 1224 | ANGLE_CHECK_GL_MATH(contextGL, |
| 1225 | glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes)); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 1226 | |
| 1227 | gl::PixelPackState directPack; |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 1228 | directPack.alignment = 1; |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1229 | stateManager->setPixelPackState(directPack); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 1230 | |
| 1231 | pixels += skipBytes + (area.height - 1) * rowBytes; |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 1232 | functions->readPixels(area.x, area.y + area.height - 1, area.width, 1, format, type, |
| 1233 | pixels); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 1234 | } |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1235 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1236 | return angle::Result::Continue; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 1237 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 1238 | } // namespace rx |