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" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 20 | #include "libANGLE/renderer/gl/FunctionsGL.h" |
Jacek Caban | fa60f69 | 2015-04-27 18:23:44 +0200 | [diff] [blame] | 21 | #include "libANGLE/renderer/gl/RenderbufferGL.h" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 22 | #include "libANGLE/renderer/gl/StateManagerGL.h" |
| 23 | #include "libANGLE/renderer/gl/TextureGL.h" |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 24 | #include "libANGLE/renderer/gl/WorkaroundsGL.h" |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame] | 25 | #include "libANGLE/renderer/gl/formatutilsgl.h" |
Corentin Wallez | 886de36 | 2016-09-27 10:49:35 -0400 | [diff] [blame] | 26 | #include "libANGLE/renderer/gl/renderergl_utils.h" |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 27 | #include "platform/Platform.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 28 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 29 | using namespace gl; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 30 | using angle::CheckedNumeric; |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 31 | |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 32 | namespace rx |
| 33 | { |
| 34 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 35 | FramebufferGL::FramebufferGL(const FramebufferState &state, |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 36 | const FunctionsGL *functions, |
| 37 | StateManagerGL *stateManager, |
| 38 | const WorkaroundsGL &workarounds, |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 39 | BlitGL *blitter, |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 40 | bool isDefault) |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 41 | : FramebufferImpl(state), |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 42 | mFunctions(functions), |
| 43 | mStateManager(stateManager), |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 44 | mWorkarounds(workarounds), |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 45 | mBlitter(blitter), |
Corentin Wallez | 6ab01b9 | 2015-08-03 10:16:36 -0700 | [diff] [blame] | 46 | mFramebufferID(0), |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 47 | mIsDefault(isDefault), |
| 48 | mAppliedEnabledDrawBuffers(1) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 49 | { |
Corentin Wallez | 6ab01b9 | 2015-08-03 10:16:36 -0700 | [diff] [blame] | 50 | if (!mIsDefault) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 51 | { |
| 52 | mFunctions->genFramebuffers(1, &mFramebufferID); |
| 53 | } |
| 54 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 55 | |
Corentin Wallez | 86f8dd7 | 2015-08-12 12:37:48 -0700 | [diff] [blame] | 56 | FramebufferGL::FramebufferGL(GLuint id, |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 57 | const FramebufferState &state, |
Corentin Wallez | 86f8dd7 | 2015-08-12 12:37:48 -0700 | [diff] [blame] | 58 | const FunctionsGL *functions, |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 59 | const WorkaroundsGL &workarounds, |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 60 | BlitGL *blitter, |
Corentin Wallez | 86f8dd7 | 2015-08-12 12:37:48 -0700 | [diff] [blame] | 61 | StateManagerGL *stateManager) |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 62 | : FramebufferImpl(state), |
Corentin Wallez | 86f8dd7 | 2015-08-12 12:37:48 -0700 | [diff] [blame] | 63 | mFunctions(functions), |
| 64 | mStateManager(stateManager), |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 65 | mWorkarounds(workarounds), |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 66 | mBlitter(blitter), |
Corentin Wallez | 86f8dd7 | 2015-08-12 12:37:48 -0700 | [diff] [blame] | 67 | mFramebufferID(id), |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 68 | mIsDefault(true), |
| 69 | mAppliedEnabledDrawBuffers(1) |
Corentin Wallez | 86f8dd7 | 2015-08-12 12:37:48 -0700 | [diff] [blame] | 70 | { |
| 71 | } |
| 72 | |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 73 | FramebufferGL::~FramebufferGL() |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 74 | { |
Geoff Lang | 1eb708e | 2015-05-04 14:58:23 -0400 | [diff] [blame] | 75 | mStateManager->deleteFramebuffer(mFramebufferID); |
| 76 | mFramebufferID = 0; |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 79 | static void BindFramebufferAttachment(const FunctionsGL *functions, |
| 80 | GLenum attachmentPoint, |
| 81 | const FramebufferAttachment *attachment) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 82 | { |
| 83 | if (attachment) |
| 84 | { |
| 85 | if (attachment->type() == GL_TEXTURE) |
| 86 | { |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 87 | const Texture *texture = attachment->getTexture(); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 88 | const TextureGL *textureGL = GetImplAs<TextureGL>(texture); |
| 89 | |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 90 | if (texture->getTarget() == GL_TEXTURE_2D || |
| 91 | texture->getTarget() == GL_TEXTURE_2D_MULTISAMPLE) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 92 | { |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 93 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, |
| 94 | texture->getTarget(), textureGL->getTextureID(), |
| 95 | attachment->mipLevel()); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 96 | } |
| 97 | else if (texture->getTarget() == GL_TEXTURE_CUBE_MAP) |
| 98 | { |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 99 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, |
| 100 | attachment->cubeMapFace(), |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 101 | textureGL->getTextureID(), attachment->mipLevel()); |
| 102 | } |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 103 | else if (texture->getTarget() == GL_TEXTURE_2D_ARRAY || |
| 104 | texture->getTarget() == GL_TEXTURE_3D) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 105 | { |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 106 | functions->framebufferTextureLayer(GL_FRAMEBUFFER, attachmentPoint, |
| 107 | textureGL->getTextureID(), |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 108 | attachment->mipLevel(), attachment->layer()); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | UNREACHABLE(); |
| 113 | } |
| 114 | } |
| 115 | else if (attachment->type() == GL_RENDERBUFFER) |
| 116 | { |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 117 | const Renderbuffer *renderbuffer = attachment->getRenderbuffer(); |
Geoff Lang | cd69f1c | 2015-03-18 14:33:23 -0400 | [diff] [blame] | 118 | const RenderbufferGL *renderbufferGL = GetImplAs<RenderbufferGL>(renderbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 119 | |
Geoff Lang | cd69f1c | 2015-03-18 14:33:23 -0400 | [diff] [blame] | 120 | functions->framebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER, |
| 121 | renderbufferGL->getRenderbufferID()); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 122 | } |
| 123 | else |
| 124 | { |
| 125 | UNREACHABLE(); |
| 126 | } |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | // Unbind this attachment |
| 131 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, 0, 0); |
| 132 | } |
| 133 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 134 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 135 | Error FramebufferGL::discard(size_t count, const GLenum *attachments) |
Austin Kinross | 0833263 | 2015-05-05 13:35:47 -0700 | [diff] [blame] | 136 | { |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 137 | // glInvalidateFramebuffer accepts the same enums as glDiscardFramebufferEXT |
| 138 | return invalidate(count, attachments); |
Austin Kinross | 0833263 | 2015-05-05 13:35:47 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 141 | Error FramebufferGL::invalidate(size_t count, const GLenum *attachments) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 142 | { |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 143 | const GLenum *finalAttachmentsPtr = attachments; |
| 144 | |
| 145 | std::vector<GLenum> modifiedAttachments; |
| 146 | if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments)) |
| 147 | { |
| 148 | finalAttachmentsPtr = modifiedAttachments.data(); |
| 149 | } |
| 150 | |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 151 | // 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] | 152 | if (mFunctions->invalidateFramebuffer) |
| 153 | { |
| 154 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 155 | mFunctions->invalidateFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count), |
| 156 | finalAttachmentsPtr); |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 157 | } |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 158 | else if (mFunctions->discardFramebuffer) |
| 159 | { |
| 160 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 161 | mFunctions->discardFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count), |
| 162 | finalAttachmentsPtr); |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 163 | } |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 164 | |
Geoff Lang | 57ce9ea | 2016-11-24 12:03:14 -0500 | [diff] [blame] | 165 | return gl::NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 168 | Error FramebufferGL::invalidateSub(size_t count, |
| 169 | const GLenum *attachments, |
| 170 | const gl::Rectangle &area) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 171 | { |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 172 | |
| 173 | const GLenum *finalAttachmentsPtr = attachments; |
| 174 | |
| 175 | std::vector<GLenum> modifiedAttachments; |
| 176 | if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments)) |
| 177 | { |
| 178 | finalAttachmentsPtr = modifiedAttachments.data(); |
| 179 | } |
| 180 | |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 181 | // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is |
| 182 | // available. |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 183 | if (mFunctions->invalidateSubFramebuffer) |
| 184 | { |
| 185 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 186 | mFunctions->invalidateSubFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count), |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 187 | finalAttachmentsPtr, area.x, area.y, area.width, |
| 188 | area.height); |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 189 | } |
| 190 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 191 | return NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 194 | Error FramebufferGL::clear(const gl::Context *context, GLbitfield mask) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 195 | { |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 196 | syncClearState(context, mask); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 197 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 198 | mFunctions->clear(mask); |
| 199 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 200 | return NoError(); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 203 | Error FramebufferGL::clearBufferfv(const gl::Context *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 204 | GLenum buffer, |
| 205 | GLint drawbuffer, |
| 206 | const GLfloat *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 207 | { |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 208 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 209 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 210 | mFunctions->clearBufferfv(buffer, drawbuffer, values); |
| 211 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 212 | return NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 213 | } |
| 214 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 215 | Error FramebufferGL::clearBufferuiv(const gl::Context *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 216 | GLenum buffer, |
| 217 | GLint drawbuffer, |
| 218 | const GLuint *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 219 | { |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 220 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 221 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 222 | mFunctions->clearBufferuiv(buffer, drawbuffer, values); |
| 223 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 224 | return NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 227 | Error FramebufferGL::clearBufferiv(const gl::Context *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 228 | GLenum buffer, |
| 229 | GLint drawbuffer, |
| 230 | const GLint *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 231 | { |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 232 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 233 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 234 | mFunctions->clearBufferiv(buffer, drawbuffer, values); |
| 235 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 236 | return NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 237 | } |
| 238 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 239 | Error FramebufferGL::clearBufferfi(const gl::Context *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 240 | GLenum buffer, |
| 241 | GLint drawbuffer, |
| 242 | GLfloat depth, |
| 243 | GLint stencil) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 244 | { |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 245 | syncClearBufferState(context, buffer, drawbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 246 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 247 | mFunctions->clearBufferfi(buffer, drawbuffer, depth, stencil); |
| 248 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 249 | return NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | GLenum FramebufferGL::getImplementationColorReadFormat() const |
| 253 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 254 | const auto *readAttachment = mState.getReadAttachment(); |
| 255 | const Format &format = readAttachment->getFormat(); |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame] | 256 | return format.info->getReadPixelsFormat(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | GLenum FramebufferGL::getImplementationColorReadType() const |
| 260 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 261 | const auto *readAttachment = mState.getReadAttachment(); |
| 262 | const Format &format = readAttachment->getFormat(); |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame] | 263 | return format.info->getReadPixelsType(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 264 | } |
| 265 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 266 | Error FramebufferGL::readPixels(const gl::Context *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 267 | const gl::Rectangle &area, |
| 268 | GLenum format, |
| 269 | GLenum type, |
Jamie Madill | 876429b | 2017-04-20 15:46:24 -0400 | [diff] [blame] | 270 | void *pixels) const |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 271 | { |
Geoff Lang | da34d00 | 2015-09-04 11:08:59 -0400 | [diff] [blame] | 272 | // TODO: don't sync the pixel pack state here once the dirty bits contain the pixel pack buffer |
| 273 | // binding |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 274 | const PixelPackState &packState = context->getGLState().getPackState(); |
Geoff Lang | da34d00 | 2015-09-04 11:08:59 -0400 | [diff] [blame] | 275 | mStateManager->setPixelPackState(packState); |
Jamie Madill | 87de362 | 2015-03-16 10:41:44 -0400 | [diff] [blame] | 276 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame] | 277 | nativegl::ReadPixelsFormat readPixelsFormat = |
| 278 | nativegl::GetReadPixelsFormat(mFunctions, mWorkarounds, format, type); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 279 | GLenum readFormat = readPixelsFormat.format; |
| 280 | GLenum readType = readPixelsFormat.type; |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 281 | |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 282 | mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferID); |
| 283 | |
| 284 | if (mWorkarounds.packOverlappingRowsSeparatelyPackBuffer && packState.pixelBuffer.get() && |
| 285 | packState.rowLength != 0 && packState.rowLength < area.width) |
| 286 | { |
| 287 | return readPixelsRowByRowWorkaround(area, readFormat, readType, packState, pixels); |
| 288 | } |
| 289 | |
| 290 | if (mWorkarounds.packLastRowSeparatelyForPaddingInclusion) |
| 291 | { |
Corentin Wallez | 886de36 | 2016-09-27 10:49:35 -0400 | [diff] [blame] | 292 | gl::Extents size(area.width, area.height, 1); |
| 293 | |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 294 | bool apply; |
Corentin Wallez | 886de36 | 2016-09-27 10:49:35 -0400 | [diff] [blame] | 295 | ANGLE_TRY_RESULT(ShouldApplyLastRowPaddingWorkaround(size, packState, readFormat, readType, |
| 296 | false, pixels), |
| 297 | apply); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 298 | |
| 299 | if (apply) |
| 300 | { |
| 301 | return readPixelsPaddingWorkaround(area, readFormat, readType, packState, pixels); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | mFunctions->readPixels(area.x, area.y, area.width, area.height, readFormat, readType, pixels); |
| 306 | |
| 307 | return gl::NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 310 | Error FramebufferGL::blit(const gl::Context *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 311 | const gl::Rectangle &sourceArea, |
| 312 | const gl::Rectangle &destArea, |
| 313 | GLbitfield mask, |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 314 | GLenum filter) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 315 | { |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 316 | const Framebuffer *sourceFramebuffer = context->getGLState().getReadFramebuffer(); |
| 317 | const Framebuffer *destFramebuffer = context->getGLState().getDrawFramebuffer(); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 318 | |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 319 | const FramebufferAttachment *colorReadAttachment = sourceFramebuffer->getReadColorbuffer(); |
Corentin Wallez | 4596a76 | 2016-12-20 14:50:18 -0500 | [diff] [blame] | 320 | |
| 321 | GLsizei readAttachmentSamples = 0; |
| 322 | if (colorReadAttachment != nullptr) |
| 323 | { |
| 324 | readAttachmentSamples = colorReadAttachment->getSamples(); |
| 325 | } |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 326 | |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 327 | bool needManualColorBlit = false; |
| 328 | |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 329 | // TODO(cwallez) when the filter is LINEAR and both source and destination are SRGB, we |
| 330 | // could avoid doing a manual blit. |
| 331 | |
| 332 | // Prior to OpenGL 4.4 BlitFramebuffer (section 18.3.1 of GL 4.3 core profile) reads: |
| 333 | // When values are taken from the read buffer, no linearization is performed, even |
| 334 | // if the format of the buffer is SRGB. |
| 335 | // Starting from OpenGL 4.4 (section 18.3.1) it reads: |
| 336 | // When values are taken from the read buffer, if FRAMEBUFFER_SRGB is enabled and the |
| 337 | // value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the framebuffer attachment |
| 338 | // corresponding to the read buffer is SRGB, the red, green, and blue components are |
| 339 | // converted from the non-linear sRGB color space according [...]. |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 340 | { |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 341 | bool sourceSRGB = |
| 342 | colorReadAttachment != nullptr && colorReadAttachment->getColorEncoding() == GL_SRGB; |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 343 | needManualColorBlit = |
| 344 | needManualColorBlit || (sourceSRGB && mFunctions->isAtMostGL(gl::Version(4, 3))); |
| 345 | } |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 346 | |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 347 | // Prior to OpenGL 4.2 BlitFramebuffer (section 4.3.2 of GL 4.1 core profile) reads: |
| 348 | // Blit operations bypass the fragment pipeline. The only fragment operations which |
| 349 | // affect a blit are the pixel ownership test and scissor test. |
| 350 | // Starting from OpenGL 4.2 (section 4.3.2) it reads: |
| 351 | // When values are written to the draw buffers, blit operations bypass the fragment |
| 352 | // pipeline. The only fragment operations which affect a blit are the pixel ownership |
| 353 | // test, the scissor test and sRGB conversion. |
| 354 | if (!needManualColorBlit) |
| 355 | { |
| 356 | bool destSRGB = false; |
| 357 | for (size_t i = 0; i < destFramebuffer->getDrawbufferStateCount(); ++i) |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 358 | { |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 359 | const FramebufferAttachment *attachment = destFramebuffer->getDrawBuffer(i); |
| 360 | if (attachment && attachment->getColorEncoding() == GL_SRGB) |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 361 | { |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 362 | destSRGB = true; |
| 363 | break; |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 364 | } |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 365 | } |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 366 | |
| 367 | needManualColorBlit = |
| 368 | needManualColorBlit || (destSRGB && mFunctions->isAtMostGL(gl::Version(4, 1))); |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // Enable FRAMEBUFFER_SRGB if needed |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 372 | mStateManager->setFramebufferSRGBEnabledForFramebuffer(context->getContextState(), true, this); |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 373 | |
| 374 | GLenum blitMask = mask; |
Corentin Wallez | 6898b35 | 2016-11-10 11:41:15 -0500 | [diff] [blame] | 375 | if (needManualColorBlit && (mask & GL_COLOR_BUFFER_BIT) && readAttachmentSamples <= 1) |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 376 | { |
| 377 | ANGLE_TRY(mBlitter->blitColorBufferWithShader(sourceFramebuffer, destFramebuffer, |
| 378 | sourceArea, destArea, filter)); |
| 379 | blitMask &= ~GL_COLOR_BUFFER_BIT; |
| 380 | } |
| 381 | |
| 382 | if (blitMask == 0) |
| 383 | { |
| 384 | return gl::NoError(); |
| 385 | } |
| 386 | |
| 387 | const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 388 | mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID()); |
| 389 | mStateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID); |
| 390 | |
Jamie Madill | 2da819e | 2015-12-03 15:53:19 -0500 | [diff] [blame] | 391 | mFunctions->blitFramebuffer(sourceArea.x, sourceArea.y, sourceArea.x1(), sourceArea.y1(), |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 392 | destArea.x, destArea.y, destArea.x1(), destArea.y1(), blitMask, |
| 393 | filter); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 394 | |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 395 | return gl::NoError(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 396 | } |
| 397 | |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 398 | gl::Error FramebufferGL::getSamplePosition(size_t index, GLfloat *xy) const |
| 399 | { |
| 400 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 401 | mFunctions->getMultisamplefv(GL_SAMPLE_POSITION, static_cast<GLuint>(index), xy); |
| 402 | return gl::NoError(); |
| 403 | } |
| 404 | |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 405 | bool FramebufferGL::checkStatus() const |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 406 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 407 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 408 | GLenum status = mFunctions->checkFramebufferStatus(GL_FRAMEBUFFER); |
| 409 | if (status != GL_FRAMEBUFFER_COMPLETE) |
| 410 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 411 | WARN() << "GL framebuffer returned incomplete."; |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 412 | } |
| 413 | return (status == GL_FRAMEBUFFER_COMPLETE); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 416 | void FramebufferGL::syncState(const gl::Context *context, const Framebuffer::DirtyBits &dirtyBits) |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 417 | { |
| 418 | // Don't need to sync state for the default FBO. |
| 419 | if (mIsDefault) |
| 420 | { |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 425 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 426 | for (auto dirtyBit : dirtyBits) |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 427 | { |
| 428 | switch (dirtyBit) |
| 429 | { |
| 430 | case Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT: |
| 431 | BindFramebufferAttachment(mFunctions, GL_DEPTH_ATTACHMENT, |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 432 | mState.getDepthAttachment()); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 433 | break; |
| 434 | case Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT: |
| 435 | BindFramebufferAttachment(mFunctions, GL_STENCIL_ATTACHMENT, |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 436 | mState.getStencilAttachment()); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 437 | break; |
| 438 | case Framebuffer::DIRTY_BIT_DRAW_BUFFERS: |
| 439 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 440 | const auto &drawBuffers = mState.getDrawBufferStates(); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 441 | mFunctions->drawBuffers(static_cast<GLsizei>(drawBuffers.size()), |
| 442 | drawBuffers.data()); |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 443 | mAppliedEnabledDrawBuffers = mState.getEnabledDrawBuffers(); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 444 | break; |
| 445 | } |
| 446 | case Framebuffer::DIRTY_BIT_READ_BUFFER: |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 447 | mFunctions->readBuffer(mState.getReadBufferState()); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 448 | break; |
JiangYizhou | f7bbc8a | 2016-11-16 09:57:22 +0800 | [diff] [blame] | 449 | case Framebuffer::DIRTY_BIT_DEFAULT_WIDTH: |
| 450 | mFunctions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_WIDTH, |
| 451 | mState.getDefaultWidth()); |
| 452 | break; |
| 453 | case Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT: |
| 454 | mFunctions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_HEIGHT, |
| 455 | mState.getDefaultHeight()); |
| 456 | break; |
| 457 | case Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES: |
| 458 | mFunctions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES, |
| 459 | mState.getDefaultSamples()); |
| 460 | break; |
| 461 | case Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS: |
| 462 | mFunctions->framebufferParameteri(GL_FRAMEBUFFER, |
| 463 | GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS, |
| 464 | mState.getDefaultFixedSampleLocations()); |
| 465 | break; |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 466 | default: |
| 467 | { |
| 468 | ASSERT(Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0 && |
| 469 | dirtyBit < Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX); |
| 470 | size_t index = |
| 471 | static_cast<size_t>(dirtyBit - Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0); |
| 472 | BindFramebufferAttachment(mFunctions, |
| 473 | static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + index), |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 474 | mState.getColorAttachment(index)); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 475 | break; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 481 | GLuint FramebufferGL::getFramebufferID() const |
| 482 | { |
| 483 | return mFramebufferID; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 484 | } |
| 485 | |
Geoff Lang | 1d2c41d | 2016-10-19 16:14:46 -0700 | [diff] [blame] | 486 | bool FramebufferGL::isDefault() const |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 487 | { |
Geoff Lang | 1d2c41d | 2016-10-19 16:14:46 -0700 | [diff] [blame] | 488 | return mIsDefault; |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 489 | } |
| 490 | |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 491 | void FramebufferGL::maskOutInactiveOutputDrawBuffers(DrawBufferMask maxSet) |
| 492 | { |
| 493 | auto targetAppliedDrawBuffers = mState.getEnabledDrawBuffers() & maxSet; |
| 494 | if (mAppliedEnabledDrawBuffers != targetAppliedDrawBuffers) |
| 495 | { |
| 496 | mAppliedEnabledDrawBuffers = targetAppliedDrawBuffers; |
| 497 | |
| 498 | const auto &stateDrawBuffers = mState.getDrawBufferStates(); |
| 499 | GLsizei drawBufferCount = static_cast<GLsizei>(stateDrawBuffers.size()); |
| 500 | ASSERT(drawBufferCount <= IMPLEMENTATION_MAX_DRAW_BUFFERS); |
| 501 | |
| 502 | GLenum drawBuffers[IMPLEMENTATION_MAX_DRAW_BUFFERS]; |
| 503 | for (GLenum i = 0; static_cast<int>(i) < drawBufferCount; ++i) |
| 504 | { |
| 505 | drawBuffers[i] = targetAppliedDrawBuffers[i] ? stateDrawBuffers[i] : GL_NONE; |
| 506 | } |
| 507 | |
| 508 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 509 | mFunctions->drawBuffers(drawBufferCount, drawBuffers); |
| 510 | } |
| 511 | } |
| 512 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 513 | void FramebufferGL::syncClearState(const gl::Context *context, GLbitfield mask) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 514 | { |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 515 | if (mFunctions->standard == STANDARD_GL_DESKTOP) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 516 | { |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 517 | if (mWorkarounds.doesSRGBClearsOnLinearFramebufferAttachments && |
| 518 | (mask & GL_COLOR_BUFFER_BIT) != 0 && !mIsDefault) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 519 | { |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 520 | bool hasSRGBAttachment = false; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 521 | for (const auto &attachment : mState.getColorAttachments()) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 522 | { |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 523 | if (attachment.isAttached() && attachment.getColorEncoding() == GL_SRGB) |
| 524 | { |
Corentin Wallez | 26a717b | 2016-09-27 08:45:42 -0700 | [diff] [blame] | 525 | hasSRGBAttachment = true; |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 526 | break; |
| 527 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 528 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 529 | |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 530 | mStateManager->setFramebufferSRGBEnabled(context->getContextState(), hasSRGBAttachment); |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 531 | } |
| 532 | else |
| 533 | { |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 534 | mStateManager->setFramebufferSRGBEnabled(context->getContextState(), !mIsDefault); |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 535 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 539 | void FramebufferGL::syncClearBufferState(const gl::Context *context, |
| 540 | GLenum buffer, |
| 541 | GLint drawBuffer) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 542 | { |
| 543 | if (mFunctions->standard == STANDARD_GL_DESKTOP) |
| 544 | { |
| 545 | if (mWorkarounds.doesSRGBClearsOnLinearFramebufferAttachments && buffer == GL_COLOR && |
| 546 | !mIsDefault) |
| 547 | { |
| 548 | // If doing a clear on a color buffer, set SRGB blend enabled only if the color buffer |
| 549 | // is an SRGB format. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 550 | const auto &drawbufferState = mState.getDrawBufferStates(); |
| 551 | const auto &colorAttachments = mState.getColorAttachments(); |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 552 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 553 | const FramebufferAttachment *attachment = nullptr; |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 554 | if (drawbufferState[drawBuffer] >= GL_COLOR_ATTACHMENT0 && |
| 555 | drawbufferState[drawBuffer] < GL_COLOR_ATTACHMENT0 + colorAttachments.size()) |
| 556 | { |
| 557 | size_t attachmentIdx = |
| 558 | static_cast<size_t>(drawbufferState[drawBuffer] - GL_COLOR_ATTACHMENT0); |
| 559 | attachment = &colorAttachments[attachmentIdx]; |
| 560 | } |
| 561 | |
| 562 | if (attachment != nullptr) |
| 563 | { |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 564 | mStateManager->setFramebufferSRGBEnabled(context->getContextState(), |
| 565 | attachment->getColorEncoding() == GL_SRGB); |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | else |
| 569 | { |
Frank Henigman | 308d745 | 2017-02-15 22:51:21 -0500 | [diff] [blame] | 570 | mStateManager->setFramebufferSRGBEnabled(context->getContextState(), !mIsDefault); |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | } |
Geoff Lang | 005a701 | 2017-03-27 13:17:34 -0400 | [diff] [blame] | 574 | |
| 575 | bool FramebufferGL::modifyInvalidateAttachmentsForEmulatedDefaultFBO( |
| 576 | size_t count, |
| 577 | const GLenum *attachments, |
| 578 | std::vector<GLenum> *modifiedAttachments) const |
| 579 | { |
| 580 | bool needsModification = mIsDefault && mFramebufferID != 0; |
| 581 | if (!needsModification) |
| 582 | { |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | modifiedAttachments->resize(count); |
| 587 | for (size_t i = 0; i < count; i++) |
| 588 | { |
| 589 | switch (attachments[i]) |
| 590 | { |
| 591 | case GL_COLOR: |
| 592 | (*modifiedAttachments)[i] = GL_COLOR_ATTACHMENT0; |
| 593 | break; |
| 594 | |
| 595 | case GL_DEPTH: |
| 596 | (*modifiedAttachments)[i] = GL_DEPTH_ATTACHMENT; |
| 597 | break; |
| 598 | |
| 599 | case GL_STENCIL: |
| 600 | (*modifiedAttachments)[i] = GL_STENCIL_ATTACHMENT; |
| 601 | break; |
| 602 | |
| 603 | default: |
| 604 | UNREACHABLE(); |
| 605 | break; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | return true; |
| 610 | } |
| 611 | |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 612 | gl::Error FramebufferGL::readPixelsRowByRowWorkaround(const gl::Rectangle &area, |
| 613 | GLenum format, |
| 614 | GLenum type, |
| 615 | const gl::PixelPackState &pack, |
Jamie Madill | 876429b | 2017-04-20 15:46:24 -0400 | [diff] [blame] | 616 | void *pixels) const |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 617 | { |
| 618 | intptr_t offset = reinterpret_cast<intptr_t>(pixels); |
| 619 | |
Geoff Lang | ca27139 | 2017-04-05 12:30:00 -0400 | [diff] [blame] | 620 | const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type); |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 621 | GLuint rowBytes = 0; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 622 | ANGLE_TRY_RESULT(glFormat.computeRowPitch(type, area.width, pack.alignment, pack.rowLength), |
| 623 | rowBytes); |
| 624 | GLuint skipBytes = 0; |
Corentin Wallez | 886de36 | 2016-09-27 10:49:35 -0400 | [diff] [blame] | 625 | ANGLE_TRY_RESULT(glFormat.computeSkipBytes(rowBytes, 0, pack, false), skipBytes); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 626 | |
| 627 | gl::PixelPackState directPack; |
| 628 | directPack.pixelBuffer = pack.pixelBuffer; |
| 629 | directPack.alignment = 1; |
| 630 | mStateManager->setPixelPackState(directPack); |
| 631 | directPack.pixelBuffer.set(nullptr); |
| 632 | |
| 633 | offset += skipBytes; |
| 634 | for (GLint row = 0; row < area.height; ++row) |
| 635 | { |
| 636 | mFunctions->readPixels(area.x, row + area.y, area.width, 1, format, type, |
Jamie Madill | 876429b | 2017-04-20 15:46:24 -0400 | [diff] [blame] | 637 | reinterpret_cast<void *>(offset)); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 638 | offset += row * rowBytes; |
| 639 | } |
| 640 | |
| 641 | return gl::NoError(); |
| 642 | } |
| 643 | |
| 644 | gl::Error FramebufferGL::readPixelsPaddingWorkaround(const gl::Rectangle &area, |
| 645 | GLenum format, |
| 646 | GLenum type, |
| 647 | const gl::PixelPackState &pack, |
Jamie Madill | 876429b | 2017-04-20 15:46:24 -0400 | [diff] [blame] | 648 | void *pixels) const |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 649 | { |
Geoff Lang | ca27139 | 2017-04-05 12:30:00 -0400 | [diff] [blame] | 650 | const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type); |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 651 | GLuint rowBytes = 0; |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 652 | ANGLE_TRY_RESULT(glFormat.computeRowPitch(type, area.width, pack.alignment, pack.rowLength), |
| 653 | rowBytes); |
| 654 | GLuint skipBytes = 0; |
Corentin Wallez | 886de36 | 2016-09-27 10:49:35 -0400 | [diff] [blame] | 655 | ANGLE_TRY_RESULT(glFormat.computeSkipBytes(rowBytes, 0, pack, false), skipBytes); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 656 | |
| 657 | // Get all by the last row |
| 658 | if (area.height > 1) |
| 659 | { |
| 660 | mFunctions->readPixels(area.x, area.y, area.width, area.height - 1, format, type, pixels); |
| 661 | } |
| 662 | |
| 663 | // Get the last row manually |
| 664 | gl::PixelPackState directPack; |
| 665 | directPack.pixelBuffer = pack.pixelBuffer; |
| 666 | directPack.alignment = 1; |
| 667 | mStateManager->setPixelPackState(directPack); |
| 668 | directPack.pixelBuffer.set(nullptr); |
| 669 | |
| 670 | intptr_t lastRowOffset = |
| 671 | reinterpret_cast<intptr_t>(pixels) + skipBytes + (area.height - 1) * rowBytes; |
| 672 | mFunctions->readPixels(area.x, area.y + area.height - 1, area.width, 1, format, type, |
Jamie Madill | 876429b | 2017-04-20 15:46:24 -0400 | [diff] [blame] | 673 | reinterpret_cast<void *>(lastRowOffset)); |
Corentin Wallez | 9a8d366 | 2016-09-22 12:18:29 -0400 | [diff] [blame] | 674 | |
| 675 | return gl::NoError(); |
| 676 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 677 | } // namespace rx |