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