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