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