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