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" |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 13 | #include "libANGLE/Context.h" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 14 | #include "libANGLE/FramebufferAttachment.h" |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 15 | #include "libANGLE/State.h" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 16 | #include "libANGLE/angletypes.h" |
| 17 | #include "libANGLE/formatutils.h" |
Geoff Lang | 9201943 | 2017-11-20 13:09:34 -0500 | [diff] [blame] | 18 | #include "libANGLE/queryconversions.h" |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 19 | #include "libANGLE/renderer/ContextImpl.h" |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 20 | #include "libANGLE/renderer/gl/BlitGL.h" |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 21 | #include "libANGLE/renderer/gl/ClearMultiviewGL.h" |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 22 | #include "libANGLE/renderer/gl/ContextGL.h" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 23 | #include "libANGLE/renderer/gl/FunctionsGL.h" |
Jacek Caban | fa60f69 | 2015-04-27 18:23:44 +0200 | [diff] [blame] | 24 | #include "libANGLE/renderer/gl/RenderbufferGL.h" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 25 | #include "libANGLE/renderer/gl/StateManagerGL.h" |
| 26 | #include "libANGLE/renderer/gl/TextureGL.h" |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 27 | #include "libANGLE/renderer/gl/WorkaroundsGL.h" |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame] | 28 | #include "libANGLE/renderer/gl/formatutilsgl.h" |
Corentin Wallez | 886de36 | 2016-09-27 10:49:35 -0400 | [diff] [blame] | 29 | #include "libANGLE/renderer/gl/renderergl_utils.h" |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 30 | #include "platform/Platform.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 31 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 32 | using namespace gl; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 33 | using angle::CheckedNumeric; |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 34 | |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 35 | namespace rx |
| 36 | { |
| 37 | |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 38 | namespace |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 39 | { |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 40 | |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 41 | void BindFramebufferAttachment(const FunctionsGL *functions, |
| 42 | GLenum attachmentPoint, |
| 43 | const FramebufferAttachment *attachment) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 44 | { |
| 45 | if (attachment) |
| 46 | { |
| 47 | if (attachment->type() == GL_TEXTURE) |
| 48 | { |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 49 | const Texture *texture = attachment->getTexture(); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 50 | const TextureGL *textureGL = GetImplAs<TextureGL>(texture); |
| 51 | |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 52 | if (texture->getType() == TextureType::_2D || |
| 53 | texture->getType() == TextureType::_2DMultisample || |
| 54 | texture->getType() == TextureType::Rectangle) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 55 | { |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 56 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 57 | ToGLenum(texture->getType()), |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 58 | textureGL->getTextureID(), attachment->mipLevel()); |
| 59 | } |
Jiawei Shao | a880247 | 2018-05-28 11:17:47 +0800 | [diff] [blame] | 60 | else if (attachment->isLayered()) |
| 61 | { |
| 62 | TextureType textureType = texture->getType(); |
| 63 | ASSERT(textureType == TextureType::_2DArray || textureType == TextureType::_3D || |
Olli Etuaho | dff32a0 | 2018-08-28 14:35:50 +0300 | [diff] [blame] | 64 | textureType == TextureType::CubeMap || |
| 65 | textureType == TextureType::_2DMultisampleArray); |
Jiawei Shao | a880247 | 2018-05-28 11:17:47 +0800 | [diff] [blame] | 66 | functions->framebufferTexture(GL_FRAMEBUFFER, attachmentPoint, |
| 67 | textureGL->getTextureID(), attachment->mipLevel()); |
| 68 | } |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 69 | else if (texture->getType() == TextureType::CubeMap) |
| 70 | { |
| 71 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, |
| 72 | ToGLenum(attachment->cubeMapFace()), |
| 73 | textureGL->getTextureID(), attachment->mipLevel()); |
| 74 | } |
| 75 | else if (texture->getType() == TextureType::_2DArray || |
Olli Etuaho | fd16210 | 2018-08-27 16:14:57 +0300 | [diff] [blame] | 76 | texture->getType() == TextureType::_3D || |
| 77 | texture->getType() == TextureType::_2DMultisampleArray) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 78 | { |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 79 | if (attachment->getMultiviewLayout() == GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE) |
| 80 | { |
| 81 | ASSERT(functions->framebufferTexture); |
| 82 | functions->framebufferTexture(GL_FRAMEBUFFER, attachmentPoint, |
| 83 | textureGL->getTextureID(), |
| 84 | attachment->mipLevel()); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | functions->framebufferTextureLayer(GL_FRAMEBUFFER, attachmentPoint, |
| 89 | textureGL->getTextureID(), |
| 90 | attachment->mipLevel(), attachment->layer()); |
| 91 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 92 | } |
| 93 | else |
| 94 | { |
| 95 | UNREACHABLE(); |
| 96 | } |
| 97 | } |
| 98 | else if (attachment->type() == GL_RENDERBUFFER) |
| 99 | { |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 100 | const Renderbuffer *renderbuffer = attachment->getRenderbuffer(); |
Geoff Lang | cd69f1c | 2015-03-18 14:33:23 -0400 | [diff] [blame] | 101 | const RenderbufferGL *renderbufferGL = GetImplAs<RenderbufferGL>(renderbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 102 | |
Geoff Lang | cd69f1c | 2015-03-18 14:33:23 -0400 | [diff] [blame] | 103 | functions->framebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER, |
| 104 | renderbufferGL->getRenderbufferID()); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 105 | } |
| 106 | else |
| 107 | { |
| 108 | UNREACHABLE(); |
| 109 | } |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | // Unbind this attachment |
| 114 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, 0, 0); |
| 115 | } |
| 116 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 117 | |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 118 | bool AreAllLayersActive(const FramebufferAttachment &attachment) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 119 | { |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 120 | int baseViewIndex = attachment.getBaseViewIndex(); |
| 121 | if (baseViewIndex != 0) |
| 122 | { |
| 123 | return false; |
| 124 | } |
| 125 | const ImageIndex &imageIndex = attachment.getTextureImageIndex(); |
Jamie Madill | cc12937 | 2018-04-12 09:13:18 -0400 | [diff] [blame] | 126 | int numLayers = static_cast<int>( |
| 127 | attachment.getTexture()->getDepth(imageIndex.getTarget(), imageIndex.getLevelIndex())); |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 128 | return (attachment.getNumViews() == numLayers); |
| 129 | } |
| 130 | |
| 131 | bool RequiresMultiviewClear(const FramebufferState &state, bool scissorTestEnabled) |
| 132 | { |
| 133 | // Get one attachment and check whether all layers are attached. |
| 134 | const FramebufferAttachment *attachment = nullptr; |
| 135 | bool allTextureArraysAreFullyAttached = true; |
| 136 | for (const FramebufferAttachment &colorAttachment : state.getColorAttachments()) |
| 137 | { |
| 138 | if (colorAttachment.isAttached()) |
| 139 | { |
| 140 | if (colorAttachment.getMultiviewLayout() == GL_NONE) |
| 141 | { |
| 142 | return false; |
| 143 | } |
| 144 | attachment = &colorAttachment; |
| 145 | allTextureArraysAreFullyAttached = |
| 146 | allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | const FramebufferAttachment *depthAttachment = state.getDepthAttachment(); |
| 151 | if (depthAttachment) |
| 152 | { |
| 153 | if (depthAttachment->getMultiviewLayout() == GL_NONE) |
| 154 | { |
| 155 | return false; |
| 156 | } |
| 157 | attachment = depthAttachment; |
| 158 | allTextureArraysAreFullyAttached = |
| 159 | allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment); |
| 160 | } |
| 161 | const FramebufferAttachment *stencilAttachment = state.getStencilAttachment(); |
| 162 | if (stencilAttachment) |
| 163 | { |
| 164 | if (stencilAttachment->getMultiviewLayout() == GL_NONE) |
| 165 | { |
| 166 | return false; |
| 167 | } |
| 168 | attachment = stencilAttachment; |
| 169 | allTextureArraysAreFullyAttached = |
| 170 | allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment); |
| 171 | } |
| 172 | |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 173 | if (attachment == nullptr) |
| 174 | { |
| 175 | return false; |
| 176 | } |
| 177 | switch (attachment->getMultiviewLayout()) |
| 178 | { |
| 179 | case GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE: |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 180 | // If all layers of each texture array are active, then there is no need to issue a |
| 181 | // special multiview clear. |
| 182 | return !allTextureArraysAreFullyAttached; |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 183 | case GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE: |
| 184 | return (scissorTestEnabled == true); |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 185 | default: |
| 186 | UNREACHABLE(); |
| 187 | } |
| 188 | return false; |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 189 | } |
| 190 | |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 191 | } // namespace |
| 192 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 193 | FramebufferGL::FramebufferGL(const gl::FramebufferState &data, GLuint id, bool isDefault) |
| 194 | : FramebufferImpl(data), |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 195 | mFramebufferID(id), |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 196 | mIsDefault(isDefault), |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 197 | mAppliedEnabledDrawBuffers(1) |
| 198 | { |
| 199 | } |
| 200 | |
| 201 | FramebufferGL::~FramebufferGL() |
| 202 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 203 | ASSERT(mFramebufferID == 0); |
| 204 | } |
| 205 | |
| 206 | void FramebufferGL::destroy(const gl::Context *context) |
| 207 | { |
| 208 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 209 | stateManager->deleteFramebuffer(mFramebufferID); |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 210 | mFramebufferID = 0; |
| 211 | } |
| 212 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 213 | Error FramebufferGL::discard(const gl::Context *context, size_t count, const GLenum *attachments) |
Austin Kinross | 0833263 | 2015-05-05 13:35:47 -0700 | [diff] [blame] | 214 | { |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 215 | // glInvalidateFramebuffer accepts the same enums as glDiscardFramebufferEXT |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 216 | return invalidate(context, count, attachments); |
Austin Kinross | 0833263 | 2015-05-05 13:35:47 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 219 | Error FramebufferGL::invalidate(const gl::Context *context, size_t count, const GLenum *attachments) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 220 | { |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 221 | const GLenum *finalAttachmentsPtr = attachments; |
| 222 | |
| 223 | std::vector<GLenum> modifiedAttachments; |
| 224 | if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments)) |
| 225 | { |
| 226 | finalAttachmentsPtr = modifiedAttachments.data(); |
| 227 | } |
| 228 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 229 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 230 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 231 | |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 232 | // 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] | 233 | if (functions->invalidateFramebuffer) |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 234 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 235 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 236 | functions->invalidateFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count), |
| 237 | finalAttachmentsPtr); |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 238 | } |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 239 | else if (functions->discardFramebufferEXT) |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 240 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 241 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 242 | functions->discardFramebufferEXT(GL_FRAMEBUFFER, static_cast<GLsizei>(count), |
| 243 | finalAttachmentsPtr); |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 244 | } |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 245 | |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 246 | return gl::NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 247 | } |
| 248 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 249 | Error FramebufferGL::invalidateSub(const gl::Context *context, |
| 250 | size_t count, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 251 | const GLenum *attachments, |
| 252 | const gl::Rectangle &area) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 253 | { |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 254 | |
| 255 | const GLenum *finalAttachmentsPtr = attachments; |
| 256 | |
| 257 | std::vector<GLenum> modifiedAttachments; |
| 258 | if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments)) |
| 259 | { |
| 260 | finalAttachmentsPtr = modifiedAttachments.data(); |
| 261 | } |
| 262 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 263 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 264 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 265 | |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 266 | // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is |
| 267 | // available. |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 268 | if (functions->invalidateSubFramebuffer) |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 269 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 270 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 271 | functions->invalidateSubFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count), |
| 272 | finalAttachmentsPtr, area.x, area.y, area.width, |
| 273 | area.height); |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 274 | } |
| 275 | |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 276 | return gl::NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 277 | } |
| 278 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 279 | Error FramebufferGL::clear(const gl::Context *context, GLbitfield mask) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 280 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 281 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 282 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 283 | |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 284 | syncClearState(context, mask); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 285 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 286 | |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 287 | if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled())) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 288 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 289 | functions->clear(mask); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 290 | } |
| 291 | else |
| 292 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 293 | ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context); |
| 294 | multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(), |
| 295 | ClearMultiviewGL::ClearCommandType::Clear, mask, |
| 296 | GL_NONE, 0, nullptr, 0.0f, 0); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 297 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 298 | |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 299 | return gl::NoError(); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 302 | Error FramebufferGL::clearBufferfv(const gl::Context *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 303 | GLenum buffer, |
| 304 | GLint drawbuffer, |
| 305 | const GLfloat *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 306 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 307 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 308 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 309 | |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 310 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 311 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 312 | |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 313 | if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled())) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 314 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 315 | functions->clearBufferfv(buffer, drawbuffer, values); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 316 | } |
| 317 | else |
| 318 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 319 | ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context); |
| 320 | multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(), |
| 321 | ClearMultiviewGL::ClearCommandType::ClearBufferfv, |
| 322 | static_cast<GLbitfield>(0u), buffer, drawbuffer, |
| 323 | reinterpret_cast<const uint8_t *>(values), 0.0f, 0); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 324 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 325 | |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 326 | return gl::NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 327 | } |
| 328 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 329 | Error FramebufferGL::clearBufferuiv(const gl::Context *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 330 | GLenum buffer, |
| 331 | GLint drawbuffer, |
| 332 | const GLuint *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 333 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 334 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 335 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 336 | |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 337 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 338 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 339 | |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 340 | if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled())) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 341 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 342 | functions->clearBufferuiv(buffer, drawbuffer, values); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 343 | } |
| 344 | else |
| 345 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 346 | ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context); |
| 347 | multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(), |
| 348 | ClearMultiviewGL::ClearCommandType::ClearBufferuiv, |
| 349 | static_cast<GLbitfield>(0u), buffer, drawbuffer, |
| 350 | reinterpret_cast<const uint8_t *>(values), 0.0f, 0); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 351 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 352 | |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 353 | return gl::NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 356 | Error FramebufferGL::clearBufferiv(const gl::Context *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 357 | GLenum buffer, |
| 358 | GLint drawbuffer, |
| 359 | const GLint *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 360 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 361 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 362 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 363 | |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 364 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 365 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 366 | |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 367 | if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled())) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 368 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 369 | functions->clearBufferiv(buffer, drawbuffer, values); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 370 | } |
| 371 | else |
| 372 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 373 | ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context); |
| 374 | multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(), |
| 375 | ClearMultiviewGL::ClearCommandType::ClearBufferiv, |
| 376 | static_cast<GLbitfield>(0u), buffer, drawbuffer, |
| 377 | reinterpret_cast<const uint8_t *>(values), 0.0f, 0); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 378 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 379 | |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 380 | return gl::NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 381 | } |
| 382 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 383 | Error FramebufferGL::clearBufferfi(const gl::Context *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 384 | GLenum buffer, |
| 385 | GLint drawbuffer, |
| 386 | GLfloat depth, |
| 387 | GLint stencil) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 388 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 389 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 390 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 391 | |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 392 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 393 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 394 | |
Martin Radev | 61e710b | 2017-09-05 11:59:52 +0300 | [diff] [blame] | 395 | if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled())) |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 396 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 397 | functions->clearBufferfi(buffer, drawbuffer, depth, stencil); |
Martin Radev | b076193 | 2017-07-25 17:42:25 +0300 | [diff] [blame] | 398 | } |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 399 | else |
| 400 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 401 | ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context); |
| 402 | multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(), |
| 403 | ClearMultiviewGL::ClearCommandType::ClearBufferfi, |
| 404 | static_cast<GLbitfield>(0u), buffer, drawbuffer, |
| 405 | nullptr, depth, stencil); |
Martin Radev | 5e424fa | 2017-08-09 16:25:36 +0300 | [diff] [blame] | 406 | } |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 407 | |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 408 | return gl::NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 409 | } |
| 410 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 411 | GLenum FramebufferGL::getImplementationColorReadFormat(const gl::Context *context) const |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 412 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 413 | const auto *readAttachment = mState.getReadAttachment(); |
| 414 | const Format &format = readAttachment->getFormat(); |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame] | 415 | return format.info->getReadPixelsFormat(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 416 | } |
| 417 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 418 | GLenum FramebufferGL::getImplementationColorReadType(const gl::Context *context) const |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 419 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 420 | const auto *readAttachment = mState.getReadAttachment(); |
| 421 | const Format &format = readAttachment->getFormat(); |
Geoff Lang | c71ea66 | 2017-09-26 17:06:02 -0400 | [diff] [blame] | 422 | return format.info->getReadPixelsType(context->getClientVersion()); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 423 | } |
| 424 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 425 | Error FramebufferGL::readPixels(const gl::Context *context, |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 426 | const gl::Rectangle &origArea, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 427 | GLenum format, |
| 428 | GLenum type, |
Jamie Madill | d482615 | 2017-09-21 11:18:59 -0400 | [diff] [blame] | 429 | void *ptrOrOffset) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 430 | { |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 431 | ContextGL *contextGL = GetImplAs<ContextGL>(context); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 432 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 433 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 434 | const WorkaroundsGL &workarounds = GetWorkaroundsGL(context); |
| 435 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 436 | // Clip read area to framebuffer. |
| 437 | const gl::Extents fbSize = getState().getReadAttachment()->getSize(); |
| 438 | const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height); |
| 439 | gl::Rectangle area; |
| 440 | if (!ClipRectangle(origArea, fbRect, &area)) |
| 441 | { |
| 442 | // nothing to read |
| 443 | return gl::NoError(); |
| 444 | } |
| 445 | |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 446 | PixelPackState packState = context->getGLState().getPackState(); |
Corentin Wallez | 336129f | 2017-10-17 15:55:40 -0400 | [diff] [blame] | 447 | const gl::Buffer *packBuffer = |
| 448 | context->getGLState().getTargetBuffer(gl::BufferBinding::PixelPack); |
Jamie Madill | 87de362 | 2015-03-16 10:41:44 -0400 | [diff] [blame] | 449 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame] | 450 | nativegl::ReadPixelsFormat readPixelsFormat = |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 451 | nativegl::GetReadPixelsFormat(functions, workarounds, format, type); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 452 | GLenum readFormat = readPixelsFormat.format; |
| 453 | GLenum readType = readPixelsFormat.type; |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 454 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 455 | stateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferID); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 456 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 457 | bool useOverlappingRowsWorkaround = workarounds.packOverlappingRowsSeparatelyPackBuffer && |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 458 | packBuffer && packState.rowLength != 0 && |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 459 | packState.rowLength < area.width; |
| 460 | |
Rafael Cintron | 05a449a | 2018-06-20 18:08:04 -0700 | [diff] [blame] | 461 | GLubyte *pixels = static_cast<GLubyte *>(ptrOrOffset); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 462 | int leftClip = area.x - origArea.x; |
| 463 | int topClip = area.y - origArea.y; |
| 464 | if (leftClip || topClip) |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 465 | { |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 466 | // Adjust destination to match portion clipped off left and/or top. |
| 467 | const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(readFormat, readType); |
| 468 | |
| 469 | GLuint rowBytes = 0; |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 470 | ANGLE_CHECK_GL_MATH(contextGL, |
| 471 | glFormat.computeRowPitch(readType, origArea.width, packState.alignment, |
| 472 | packState.rowLength, &rowBytes)); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 473 | pixels += leftClip * glFormat.pixelBytes + topClip * rowBytes; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 476 | if (packState.rowLength == 0 && area.width != origArea.width) |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 477 | { |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 478 | // No rowLength was specified so it will derive from read width, but clipping changed the |
| 479 | // read width. Use the original width so we fill the user's buffer as they intended. |
| 480 | packState.rowLength = origArea.width; |
| 481 | } |
Corentin Wallez | 886de36 | 2016-09-27 10:49:35 -0400 | [diff] [blame] | 482 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 483 | // We want to use rowLength, but that might not be supported. |
| 484 | bool cannotSetDesiredRowLength = |
| 485 | packState.rowLength && !GetImplAs<ContextGL>(context)->getNativeExtensions().packSubimage; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 486 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 487 | if (cannotSetDesiredRowLength || useOverlappingRowsWorkaround) |
| 488 | { |
Jamie Madill | 1395134 | 2018-09-30 15:24:28 -0400 | [diff] [blame] | 489 | return readPixelsRowByRow(context, area, readFormat, readType, packState, pixels); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 490 | } |
Jamie Madill | 1395134 | 2018-09-30 15:24:28 -0400 | [diff] [blame] | 491 | |
| 492 | bool useLastRowPaddingWorkaround = false; |
| 493 | if (workarounds.packLastRowSeparatelyForPaddingInclusion) |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 494 | { |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 495 | ANGLE_TRY(ShouldApplyLastRowPaddingWorkaround( |
| 496 | contextGL, gl::Extents(area.width, area.height, 1), packState, packBuffer, readFormat, |
| 497 | readType, false, pixels, &useLastRowPaddingWorkaround)); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 498 | } |
| 499 | |
Jamie Madill | 1395134 | 2018-09-30 15:24:28 -0400 | [diff] [blame] | 500 | return readPixelsAllAtOnce(context, area, readFormat, readType, packState, pixels, |
| 501 | useLastRowPaddingWorkaround); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 502 | } |
| 503 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 504 | Error FramebufferGL::blit(const gl::Context *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 505 | const gl::Rectangle &sourceArea, |
| 506 | const gl::Rectangle &destArea, |
| 507 | GLbitfield mask, |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 508 | GLenum filter) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 509 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 510 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 511 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 512 | |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 513 | const Framebuffer *sourceFramebuffer = context->getGLState().getReadFramebuffer(); |
| 514 | const Framebuffer *destFramebuffer = context->getGLState().getDrawFramebuffer(); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 515 | |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 516 | const FramebufferAttachment *colorReadAttachment = sourceFramebuffer->getReadColorbuffer(); |
Corentin Wallez | 4596a76 | 2016-12-20 14:50:18 -0500 | [diff] [blame] | 517 | |
| 518 | GLsizei readAttachmentSamples = 0; |
| 519 | if (colorReadAttachment != nullptr) |
| 520 | { |
| 521 | readAttachmentSamples = colorReadAttachment->getSamples(); |
| 522 | } |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 523 | |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 524 | bool needManualColorBlit = false; |
| 525 | |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 526 | // TODO(cwallez) when the filter is LINEAR and both source and destination are SRGB, we |
| 527 | // could avoid doing a manual blit. |
| 528 | |
| 529 | // Prior to OpenGL 4.4 BlitFramebuffer (section 18.3.1 of GL 4.3 core profile) reads: |
| 530 | // When values are taken from the read buffer, no linearization is performed, even |
| 531 | // if the format of the buffer is SRGB. |
| 532 | // Starting from OpenGL 4.4 (section 18.3.1) it reads: |
| 533 | // When values are taken from the read buffer, if FRAMEBUFFER_SRGB is enabled and the |
| 534 | // value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the framebuffer attachment |
| 535 | // corresponding to the read buffer is SRGB, the red, green, and blue components are |
| 536 | // converted from the non-linear sRGB color space according [...]. |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 537 | { |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 538 | bool sourceSRGB = |
| 539 | colorReadAttachment != nullptr && colorReadAttachment->getColorEncoding() == GL_SRGB; |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 540 | needManualColorBlit = |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 541 | needManualColorBlit || (sourceSRGB && functions->isAtMostGL(gl::Version(4, 3))); |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 542 | } |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 543 | |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 544 | // Prior to OpenGL 4.2 BlitFramebuffer (section 4.3.2 of GL 4.1 core profile) reads: |
| 545 | // Blit operations bypass the fragment pipeline. The only fragment operations which |
| 546 | // affect a blit are the pixel ownership test and scissor test. |
| 547 | // Starting from OpenGL 4.2 (section 4.3.2) it reads: |
| 548 | // When values are written to the draw buffers, blit operations bypass the fragment |
| 549 | // pipeline. The only fragment operations which affect a blit are the pixel ownership |
| 550 | // test, the scissor test and sRGB conversion. |
| 551 | if (!needManualColorBlit) |
| 552 | { |
| 553 | bool destSRGB = false; |
| 554 | for (size_t i = 0; i < destFramebuffer->getDrawbufferStateCount(); ++i) |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 555 | { |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 556 | const FramebufferAttachment *attachment = destFramebuffer->getDrawBuffer(i); |
| 557 | if (attachment && attachment->getColorEncoding() == GL_SRGB) |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 558 | { |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 559 | destSRGB = true; |
| 560 | break; |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 561 | } |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 562 | } |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 563 | |
| 564 | needManualColorBlit = |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 565 | needManualColorBlit || (destSRGB && functions->isAtMostGL(gl::Version(4, 1))); |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // Enable FRAMEBUFFER_SRGB if needed |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 569 | stateManager->setFramebufferSRGBEnabledForFramebuffer(context, true, this); |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 570 | |
| 571 | GLenum blitMask = mask; |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 572 | if (needManualColorBlit && (mask & GL_COLOR_BUFFER_BIT) && readAttachmentSamples <= 1) |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 573 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 574 | BlitGL *blitter = GetBlitGL(context); |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 575 | ANGLE_TRY(blitter->blitColorBufferWithShader(context, sourceFramebuffer, destFramebuffer, |
| 576 | sourceArea, destArea, filter)); |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 577 | blitMask &= ~GL_COLOR_BUFFER_BIT; |
| 578 | } |
| 579 | |
| 580 | if (blitMask == 0) |
| 581 | { |
| 582 | return gl::NoError(); |
| 583 | } |
| 584 | |
| 585 | const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 586 | stateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID()); |
| 587 | stateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 588 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 589 | functions->blitFramebuffer(sourceArea.x, sourceArea.y, sourceArea.x1(), sourceArea.y1(), |
| 590 | destArea.x, destArea.y, destArea.x1(), destArea.y1(), blitMask, |
| 591 | filter); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 592 | |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 593 | return gl::NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 594 | } |
| 595 | |
Geoff Lang | 1345507 | 2018-05-09 11:24:43 -0400 | [diff] [blame] | 596 | gl::Error FramebufferGL::getSamplePosition(const gl::Context *context, |
| 597 | size_t index, |
| 598 | GLfloat *xy) const |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 599 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 600 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 601 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 602 | |
| 603 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 604 | functions->getMultisamplefv(GL_SAMPLE_POSITION, static_cast<GLuint>(index), xy); |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 605 | return gl::NoError(); |
| 606 | } |
| 607 | |
Kenneth Russell | ce8602a | 2017-10-03 18:23:08 -0700 | [diff] [blame] | 608 | bool FramebufferGL::checkStatus(const gl::Context *context) const |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 609 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 610 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 611 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 612 | |
| 613 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 614 | GLenum status = functions->checkFramebufferStatus(GL_FRAMEBUFFER); |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 615 | if (status != GL_FRAMEBUFFER_COMPLETE) |
| 616 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 617 | WARN() << "GL framebuffer returned incomplete."; |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 618 | } |
| 619 | return (status == GL_FRAMEBUFFER_COMPLETE); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 620 | } |
| 621 | |
Jamie Madill | 19fa1c6 | 2018-03-08 09:47:21 -0500 | [diff] [blame] | 622 | gl::Error FramebufferGL::syncState(const gl::Context *context, |
| 623 | const Framebuffer::DirtyBits &dirtyBits) |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 624 | { |
| 625 | // Don't need to sync state for the default FBO. |
| 626 | if (mIsDefault) |
| 627 | { |
Jamie Madill | 19fa1c6 | 2018-03-08 09:47:21 -0500 | [diff] [blame] | 628 | return gl::NoError(); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 629 | } |
| 630 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 631 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 632 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 633 | |
| 634 | stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 635 | |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 636 | // A pointer to one of the attachments for which the texture or the render buffer is not zero. |
| 637 | const FramebufferAttachment *attachment = nullptr; |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 638 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 639 | for (auto dirtyBit : dirtyBits) |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 640 | { |
| 641 | switch (dirtyBit) |
| 642 | { |
| 643 | case Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT: |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 644 | { |
| 645 | const FramebufferAttachment *newAttachment = mState.getDepthAttachment(); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 646 | BindFramebufferAttachment(functions, GL_DEPTH_ATTACHMENT, newAttachment); |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 647 | if (newAttachment) |
| 648 | { |
| 649 | attachment = newAttachment; |
| 650 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 651 | break; |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 652 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 653 | case Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT: |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 654 | { |
| 655 | const FramebufferAttachment *newAttachment = mState.getStencilAttachment(); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 656 | BindFramebufferAttachment(functions, GL_STENCIL_ATTACHMENT, newAttachment); |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 657 | if (newAttachment) |
| 658 | { |
| 659 | attachment = newAttachment; |
| 660 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 661 | break; |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 662 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 663 | case Framebuffer::DIRTY_BIT_DRAW_BUFFERS: |
| 664 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 665 | const auto &drawBuffers = mState.getDrawBufferStates(); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 666 | functions->drawBuffers(static_cast<GLsizei>(drawBuffers.size()), |
| 667 | drawBuffers.data()); |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 668 | mAppliedEnabledDrawBuffers = mState.getEnabledDrawBuffers(); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 669 | break; |
| 670 | } |
| 671 | case Framebuffer::DIRTY_BIT_READ_BUFFER: |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 672 | functions->readBuffer(mState.getReadBufferState()); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 673 | break; |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 674 | case Framebuffer::DIRTY_BIT_DEFAULT_WIDTH: |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 675 | functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_WIDTH, |
| 676 | mState.getDefaultWidth()); |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 677 | break; |
| 678 | case Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT: |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 679 | functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_HEIGHT, |
| 680 | mState.getDefaultHeight()); |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 681 | break; |
| 682 | case Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES: |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 683 | functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES, |
| 684 | mState.getDefaultSamples()); |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 685 | break; |
| 686 | case Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS: |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 687 | functions->framebufferParameteri( |
Geoff Lang | 9201943 | 2017-11-20 13:09:34 -0500 | [diff] [blame] | 688 | GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS, |
| 689 | gl::ConvertToGLBoolean(mState.getDefaultFixedSampleLocations())); |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 690 | break; |
Jiawei Shao | b1e9138 | 2018-05-17 14:33:55 +0800 | [diff] [blame] | 691 | case Framebuffer::DIRTY_BIT_DEFAULT_LAYERS: |
| 692 | functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_LAYERS_EXT, |
| 693 | mState.getDefaultLayers()); |
| 694 | break; |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 695 | default: |
| 696 | { |
| 697 | ASSERT(Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0 && |
| 698 | dirtyBit < Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX); |
| 699 | size_t index = |
| 700 | static_cast<size_t>(dirtyBit - Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0); |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 701 | const FramebufferAttachment *newAttachment = mState.getColorAttachment(index); |
| 702 | BindFramebufferAttachment( |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 703 | functions, static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + index), newAttachment); |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 704 | if (newAttachment) |
| 705 | { |
| 706 | attachment = newAttachment; |
| 707 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 708 | break; |
| 709 | } |
| 710 | } |
| 711 | } |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 712 | |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 713 | if (attachment) |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 714 | { |
Martin Radev | 7c2e6a9 | 2017-08-28 11:13:16 +0300 | [diff] [blame] | 715 | const bool isSideBySide = |
| 716 | (attachment->getMultiviewLayout() == GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 717 | stateManager->setSideBySide(isSideBySide); |
| 718 | stateManager->setViewportOffsets(attachment->getMultiviewViewportOffsets()); |
| 719 | stateManager->updateMultiviewBaseViewLayerIndexUniform(context->getGLState().getProgram(), |
| 720 | getState()); |
Martin Radev | 878c8b1 | 2017-07-28 09:51:04 +0300 | [diff] [blame] | 721 | } |
Jamie Madill | 19fa1c6 | 2018-03-08 09:47:21 -0500 | [diff] [blame] | 722 | |
| 723 | return gl::NoError(); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 724 | } |
| 725 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 726 | GLuint FramebufferGL::getFramebufferID() const |
| 727 | { |
| 728 | return mFramebufferID; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 729 | } |
| 730 | |
Geoff Lang | 1d2c41d | 2016-10-19 16:14:46 -0700 | [diff] [blame] | 731 | bool FramebufferGL::isDefault() const |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 732 | { |
Geoff Lang | 1d2c41d | 2016-10-19 16:14:46 -0700 | [diff] [blame] | 733 | return mIsDefault; |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 734 | } |
| 735 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 736 | void FramebufferGL::maskOutInactiveOutputDrawBuffers(const gl::Context *context, |
| 737 | GLenum binding, |
| 738 | DrawBufferMask maxSet) |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 739 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 740 | |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 741 | auto targetAppliedDrawBuffers = mState.getEnabledDrawBuffers() & maxSet; |
| 742 | if (mAppliedEnabledDrawBuffers != targetAppliedDrawBuffers) |
| 743 | { |
| 744 | mAppliedEnabledDrawBuffers = targetAppliedDrawBuffers; |
| 745 | |
| 746 | const auto &stateDrawBuffers = mState.getDrawBufferStates(); |
| 747 | GLsizei drawBufferCount = static_cast<GLsizei>(stateDrawBuffers.size()); |
| 748 | ASSERT(drawBufferCount <= IMPLEMENTATION_MAX_DRAW_BUFFERS); |
| 749 | |
| 750 | GLenum drawBuffers[IMPLEMENTATION_MAX_DRAW_BUFFERS]; |
| 751 | for (GLenum i = 0; static_cast<int>(i) < drawBufferCount; ++i) |
| 752 | { |
| 753 | drawBuffers[i] = targetAppliedDrawBuffers[i] ? stateDrawBuffers[i] : GL_NONE; |
| 754 | } |
| 755 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 756 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 757 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 758 | |
| 759 | stateManager->bindFramebuffer(binding, mFramebufferID); |
| 760 | functions->drawBuffers(drawBufferCount, drawBuffers); |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 764 | void FramebufferGL::syncClearState(const gl::Context *context, GLbitfield mask) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 765 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 766 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 767 | |
| 768 | if (functions->standard == STANDARD_GL_DESKTOP) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 769 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 770 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 771 | const WorkaroundsGL &workarounds = GetWorkaroundsGL(context); |
| 772 | |
| 773 | if (workarounds.doesSRGBClearsOnLinearFramebufferAttachments && |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 774 | (mask & GL_COLOR_BUFFER_BIT) != 0 && !mIsDefault) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 775 | { |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 776 | bool hasSRGBAttachment = false; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 777 | for (const auto &attachment : mState.getColorAttachments()) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 778 | { |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 779 | if (attachment.isAttached() && attachment.getColorEncoding() == GL_SRGB) |
| 780 | { |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 781 | hasSRGBAttachment = true; |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 782 | break; |
| 783 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 784 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 785 | |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 786 | stateManager->setFramebufferSRGBEnabled(context, hasSRGBAttachment); |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 787 | } |
| 788 | else |
| 789 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 790 | stateManager->setFramebufferSRGBEnabled(context, !mIsDefault); |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 791 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 795 | void FramebufferGL::syncClearBufferState(const gl::Context *context, |
| 796 | GLenum buffer, |
| 797 | GLint drawBuffer) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 798 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 799 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 800 | |
| 801 | if (functions->standard == STANDARD_GL_DESKTOP) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 802 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 803 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 804 | const WorkaroundsGL &workarounds = GetWorkaroundsGL(context); |
| 805 | |
| 806 | if (workarounds.doesSRGBClearsOnLinearFramebufferAttachments && buffer == GL_COLOR && |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 807 | !mIsDefault) |
| 808 | { |
| 809 | // If doing a clear on a color buffer, set SRGB blend enabled only if the color buffer |
| 810 | // is an SRGB format. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 811 | const auto &drawbufferState = mState.getDrawBufferStates(); |
| 812 | const auto &colorAttachments = mState.getColorAttachments(); |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 813 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 814 | const FramebufferAttachment *attachment = nullptr; |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 815 | if (drawbufferState[drawBuffer] >= GL_COLOR_ATTACHMENT0 && |
| 816 | drawbufferState[drawBuffer] < GL_COLOR_ATTACHMENT0 + colorAttachments.size()) |
| 817 | { |
| 818 | size_t attachmentIdx = |
| 819 | static_cast<size_t>(drawbufferState[drawBuffer] - GL_COLOR_ATTACHMENT0); |
| 820 | attachment = &colorAttachments[attachmentIdx]; |
| 821 | } |
| 822 | |
| 823 | if (attachment != nullptr) |
| 824 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 825 | stateManager->setFramebufferSRGBEnabled(context, |
| 826 | attachment->getColorEncoding() == GL_SRGB); |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | else |
| 830 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 831 | stateManager->setFramebufferSRGBEnabled(context, !mIsDefault); |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | } |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 835 | |
| 836 | bool FramebufferGL::modifyInvalidateAttachmentsForEmulatedDefaultFBO( |
| 837 | size_t count, |
| 838 | const GLenum *attachments, |
| 839 | std::vector<GLenum> *modifiedAttachments) const |
| 840 | { |
| 841 | bool needsModification = mIsDefault && mFramebufferID != 0; |
| 842 | if (!needsModification) |
| 843 | { |
| 844 | return false; |
| 845 | } |
| 846 | |
| 847 | modifiedAttachments->resize(count); |
| 848 | for (size_t i = 0; i < count; i++) |
| 849 | { |
| 850 | switch (attachments[i]) |
| 851 | { |
| 852 | case GL_COLOR: |
| 853 | (*modifiedAttachments)[i] = GL_COLOR_ATTACHMENT0; |
| 854 | break; |
| 855 | |
| 856 | case GL_DEPTH: |
| 857 | (*modifiedAttachments)[i] = GL_DEPTH_ATTACHMENT; |
| 858 | break; |
| 859 | |
| 860 | case GL_STENCIL: |
| 861 | (*modifiedAttachments)[i] = GL_STENCIL_ATTACHMENT; |
| 862 | break; |
| 863 | |
| 864 | default: |
| 865 | UNREACHABLE(); |
| 866 | break; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | return true; |
| 871 | } |
| 872 | |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 873 | angle::Result FramebufferGL::readPixelsRowByRow(const gl::Context *context, |
| 874 | const gl::Rectangle &area, |
| 875 | GLenum format, |
| 876 | GLenum type, |
| 877 | const gl::PixelPackState &pack, |
| 878 | GLubyte *pixels) const |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 879 | { |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 880 | ContextGL *contextGL = GetImplAs<ContextGL>(context); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 881 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 882 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 883 | |
Geoff Lang | ca27139 | 2017-04-05 12:30:00 -0400 | [diff] [blame] | 884 | const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 885 | |
| 886 | GLuint rowBytes = 0; |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 887 | ANGLE_CHECK_GL_MATH(contextGL, glFormat.computeRowPitch(type, area.width, pack.alignment, |
| 888 | pack.rowLength, &rowBytes)); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 889 | GLuint skipBytes = 0; |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 890 | ANGLE_CHECK_GL_MATH(contextGL, |
| 891 | glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes)); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 892 | |
| 893 | gl::PixelPackState directPack; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 894 | directPack.alignment = 1; |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 895 | stateManager->setPixelPackState(directPack); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 896 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 897 | pixels += skipBytes; |
| 898 | for (GLint y = area.y; y < area.y + area.height; ++y) |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 899 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 900 | functions->readPixels(area.x, y, area.width, 1, format, type, pixels); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 901 | pixels += rowBytes; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 902 | } |
| 903 | |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 904 | return angle::Result::Continue(); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 905 | } |
| 906 | |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 907 | angle::Result FramebufferGL::readPixelsAllAtOnce(const gl::Context *context, |
| 908 | const gl::Rectangle &area, |
| 909 | GLenum format, |
| 910 | GLenum type, |
| 911 | const gl::PixelPackState &pack, |
| 912 | GLubyte *pixels, |
| 913 | bool readLastRowSeparately) const |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 914 | { |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 915 | ContextGL *contextGL = GetImplAs<ContextGL>(context); |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 916 | const FunctionsGL *functions = GetFunctionsGL(context); |
| 917 | StateManagerGL *stateManager = GetStateManagerGL(context); |
| 918 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 919 | GLint height = area.height - readLastRowSeparately; |
| 920 | if (height > 0) |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 921 | { |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 922 | stateManager->setPixelPackState(pack); |
| 923 | functions->readPixels(area.x, area.y, area.width, height, format, type, pixels); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 924 | } |
| 925 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 926 | if (readLastRowSeparately) |
| 927 | { |
| 928 | const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 929 | |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 930 | GLuint rowBytes = 0; |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 931 | ANGLE_CHECK_GL_MATH(contextGL, glFormat.computeRowPitch(type, area.width, pack.alignment, |
| 932 | pack.rowLength, &rowBytes)); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 933 | GLuint skipBytes = 0; |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 934 | ANGLE_CHECK_GL_MATH(contextGL, |
| 935 | glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes)); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 936 | |
| 937 | gl::PixelPackState directPack; |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 938 | directPack.alignment = 1; |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 939 | stateManager->setPixelPackState(directPack); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 940 | |
| 941 | pixels += skipBytes + (area.height - 1) * rowBytes; |
Geoff Lang | 6110763 | 2018-05-09 11:32:46 -0400 | [diff] [blame] | 942 | functions->readPixels(area.x, area.y + area.height - 1, area.width, 1, format, type, |
| 943 | pixels); |
Frank Henigman | fa36c33 | 2017-06-09 18:44:45 -0400 | [diff] [blame] | 944 | } |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 945 | |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 946 | return angle::Result::Continue(); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 947 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 948 | } // namespace rx |