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 | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 11 | #include "common/BitSetIterator.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 12 | #include "common/debug.h" |
Jamie Madill | 9082b98 | 2016-04-27 15:21:51 -0400 | [diff] [blame] | 13 | #include "libANGLE/ContextState.h" |
Jamie Madill | 87de362 | 2015-03-16 10:41:44 -0400 | [diff] [blame] | 14 | #include "libANGLE/State.h" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 15 | #include "libANGLE/FramebufferAttachment.h" |
| 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" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 19 | #include "libANGLE/renderer/gl/FunctionsGL.h" |
Jacek Caban | fa60f69 | 2015-04-27 18:23:44 +0200 | [diff] [blame] | 20 | #include "libANGLE/renderer/gl/RenderbufferGL.h" |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 21 | #include "libANGLE/renderer/gl/StateManagerGL.h" |
| 22 | #include "libANGLE/renderer/gl/TextureGL.h" |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 23 | #include "libANGLE/renderer/gl/WorkaroundsGL.h" |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 24 | #include "libANGLE/renderer/gl/formatutilsgl.h" |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 25 | #include "platform/Platform.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 26 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 27 | using namespace gl; |
| 28 | |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 29 | namespace rx |
| 30 | { |
| 31 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 32 | FramebufferGL::FramebufferGL(const FramebufferState &state, |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 33 | const FunctionsGL *functions, |
| 34 | StateManagerGL *stateManager, |
| 35 | const WorkaroundsGL &workarounds, |
| 36 | bool isDefault) |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 37 | : FramebufferImpl(state), |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 38 | mFunctions(functions), |
| 39 | mStateManager(stateManager), |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 40 | mWorkarounds(workarounds), |
Corentin Wallez | 6ab01b9 | 2015-08-03 10:16:36 -0700 | [diff] [blame] | 41 | mFramebufferID(0), |
| 42 | mIsDefault(isDefault) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 43 | { |
Corentin Wallez | 6ab01b9 | 2015-08-03 10:16:36 -0700 | [diff] [blame] | 44 | if (!mIsDefault) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 45 | { |
| 46 | mFunctions->genFramebuffers(1, &mFramebufferID); |
| 47 | } |
| 48 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 49 | |
Corentin Wallez | 86f8dd7 | 2015-08-12 12:37:48 -0700 | [diff] [blame] | 50 | FramebufferGL::FramebufferGL(GLuint id, |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 51 | const FramebufferState &state, |
Corentin Wallez | 86f8dd7 | 2015-08-12 12:37:48 -0700 | [diff] [blame] | 52 | const FunctionsGL *functions, |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 53 | const WorkaroundsGL &workarounds, |
Corentin Wallez | 86f8dd7 | 2015-08-12 12:37:48 -0700 | [diff] [blame] | 54 | StateManagerGL *stateManager) |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 55 | : FramebufferImpl(state), |
Corentin Wallez | 86f8dd7 | 2015-08-12 12:37:48 -0700 | [diff] [blame] | 56 | mFunctions(functions), |
| 57 | mStateManager(stateManager), |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 58 | mWorkarounds(workarounds), |
Corentin Wallez | 86f8dd7 | 2015-08-12 12:37:48 -0700 | [diff] [blame] | 59 | mFramebufferID(id), |
| 60 | mIsDefault(true) |
| 61 | { |
| 62 | } |
| 63 | |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 64 | FramebufferGL::~FramebufferGL() |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 65 | { |
Geoff Lang | 1eb708e | 2015-05-04 14:58:23 -0400 | [diff] [blame] | 66 | mStateManager->deleteFramebuffer(mFramebufferID); |
| 67 | mFramebufferID = 0; |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 70 | static void BindFramebufferAttachment(const FunctionsGL *functions, |
| 71 | GLenum attachmentPoint, |
| 72 | const FramebufferAttachment *attachment) |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 73 | { |
| 74 | if (attachment) |
| 75 | { |
| 76 | if (attachment->type() == GL_TEXTURE) |
| 77 | { |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 78 | const Texture *texture = attachment->getTexture(); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 79 | const TextureGL *textureGL = GetImplAs<TextureGL>(texture); |
| 80 | |
| 81 | if (texture->getTarget() == GL_TEXTURE_2D) |
| 82 | { |
| 83 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, |
| 84 | textureGL->getTextureID(), attachment->mipLevel()); |
| 85 | } |
| 86 | else if (texture->getTarget() == GL_TEXTURE_CUBE_MAP) |
| 87 | { |
| 88 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, attachment->cubeMapFace(), |
| 89 | textureGL->getTextureID(), attachment->mipLevel()); |
| 90 | } |
| 91 | else if (texture->getTarget() == GL_TEXTURE_2D_ARRAY || texture->getTarget() == GL_TEXTURE_3D) |
| 92 | { |
| 93 | functions->framebufferTextureLayer(GL_FRAMEBUFFER, attachmentPoint, textureGL->getTextureID(), |
| 94 | attachment->mipLevel(), attachment->layer()); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | UNREACHABLE(); |
| 99 | } |
| 100 | } |
| 101 | else if (attachment->type() == GL_RENDERBUFFER) |
| 102 | { |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 103 | const Renderbuffer *renderbuffer = attachment->getRenderbuffer(); |
Geoff Lang | cd69f1c | 2015-03-18 14:33:23 -0400 | [diff] [blame] | 104 | const RenderbufferGL *renderbufferGL = GetImplAs<RenderbufferGL>(renderbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 105 | |
Geoff Lang | cd69f1c | 2015-03-18 14:33:23 -0400 | [diff] [blame] | 106 | functions->framebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER, |
| 107 | renderbufferGL->getRenderbufferID()); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 108 | } |
| 109 | else |
| 110 | { |
| 111 | UNREACHABLE(); |
| 112 | } |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | // Unbind this attachment |
| 117 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, 0, 0); |
| 118 | } |
| 119 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 120 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 121 | Error FramebufferGL::discard(size_t count, const GLenum *attachments) |
Austin Kinross | 0833263 | 2015-05-05 13:35:47 -0700 | [diff] [blame] | 122 | { |
| 123 | UNIMPLEMENTED(); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 124 | return Error(GL_INVALID_OPERATION); |
Austin Kinross | 0833263 | 2015-05-05 13:35:47 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 127 | Error FramebufferGL::invalidate(size_t count, const GLenum *attachments) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 128 | { |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 129 | // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is available. |
| 130 | if (mFunctions->invalidateFramebuffer) |
| 131 | { |
| 132 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 133 | mFunctions->invalidateFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count), attachments); |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 136 | return Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 139 | Error FramebufferGL::invalidateSub(size_t count, |
| 140 | const GLenum *attachments, |
| 141 | const gl::Rectangle &area) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 142 | { |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 143 | // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is available. |
| 144 | if (mFunctions->invalidateSubFramebuffer) |
| 145 | { |
| 146 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 147 | mFunctions->invalidateSubFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count), |
| 148 | attachments, area.x, area.y, area.width, area.height); |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 151 | return Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 154 | Error FramebufferGL::clear(ContextImpl *context, GLbitfield mask) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 155 | { |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 156 | syncClearState(mask); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 157 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 158 | mFunctions->clear(mask); |
| 159 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 160 | return Error(GL_NO_ERROR); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 163 | Error FramebufferGL::clearBufferfv(ContextImpl *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 164 | GLenum buffer, |
| 165 | GLint drawbuffer, |
| 166 | const GLfloat *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 167 | { |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 168 | syncClearBufferState(buffer, drawbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 169 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 170 | mFunctions->clearBufferfv(buffer, drawbuffer, values); |
| 171 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 172 | return Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 175 | Error FramebufferGL::clearBufferuiv(ContextImpl *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 176 | GLenum buffer, |
| 177 | GLint drawbuffer, |
| 178 | const GLuint *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 179 | { |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 180 | syncClearBufferState(buffer, drawbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 181 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 182 | mFunctions->clearBufferuiv(buffer, drawbuffer, values); |
| 183 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 184 | return Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 185 | } |
| 186 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 187 | Error FramebufferGL::clearBufferiv(ContextImpl *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 188 | GLenum buffer, |
| 189 | GLint drawbuffer, |
| 190 | const GLint *values) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 191 | { |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 192 | syncClearBufferState(buffer, drawbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 193 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 194 | mFunctions->clearBufferiv(buffer, drawbuffer, values); |
| 195 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 196 | return Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 199 | Error FramebufferGL::clearBufferfi(ContextImpl *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 200 | GLenum buffer, |
| 201 | GLint drawbuffer, |
| 202 | GLfloat depth, |
| 203 | GLint stencil) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 204 | { |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 205 | syncClearBufferState(buffer, drawbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 206 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 207 | mFunctions->clearBufferfi(buffer, drawbuffer, depth, stencil); |
| 208 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 209 | return Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | GLenum FramebufferGL::getImplementationColorReadFormat() const |
| 213 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 214 | const auto *readAttachment = mState.getReadAttachment(); |
| 215 | const Format &format = readAttachment->getFormat(); |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 216 | return format.info->getReadPixelsFormat(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | GLenum FramebufferGL::getImplementationColorReadType() const |
| 220 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 221 | const auto *readAttachment = mState.getReadAttachment(); |
| 222 | const Format &format = readAttachment->getFormat(); |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 223 | return format.info->getReadPixelsType(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 224 | } |
| 225 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 226 | Error FramebufferGL::readPixels(ContextImpl *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 227 | const gl::Rectangle &area, |
| 228 | GLenum format, |
| 229 | GLenum type, |
| 230 | GLvoid *pixels) const |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 231 | { |
Geoff Lang | da34d00 | 2015-09-04 11:08:59 -0400 | [diff] [blame] | 232 | // TODO: don't sync the pixel pack state here once the dirty bits contain the pixel pack buffer |
| 233 | // binding |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 234 | const PixelPackState &packState = context->getGLState().getPackState(); |
Geoff Lang | da34d00 | 2015-09-04 11:08:59 -0400 | [diff] [blame] | 235 | mStateManager->setPixelPackState(packState); |
Jamie Madill | 87de362 | 2015-03-16 10:41:44 -0400 | [diff] [blame] | 236 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 237 | mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferID); |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 238 | |
| 239 | nativegl::ReadPixelsFormat readPixelsFormat = |
| 240 | nativegl::GetReadPixelsFormat(mFunctions, mWorkarounds, format, type); |
| 241 | mFunctions->readPixels(area.x, area.y, area.width, area.height, readPixelsFormat.format, |
| 242 | readPixelsFormat.type, pixels); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 243 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 244 | return Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 245 | } |
| 246 | |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 247 | Error FramebufferGL::blit(ContextImpl *context, |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 248 | const gl::Rectangle &sourceArea, |
| 249 | const gl::Rectangle &destArea, |
| 250 | GLbitfield mask, |
Jamie Madill | 8415b5f | 2016-04-26 13:41:39 -0400 | [diff] [blame] | 251 | GLenum filter) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 252 | { |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 253 | const Framebuffer *sourceFramebuffer = context->getGLState().getReadFramebuffer(); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 254 | const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer); |
| 255 | |
| 256 | mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID()); |
| 257 | mStateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID); |
| 258 | |
Jamie Madill | 2da819e | 2015-12-03 15:53:19 -0500 | [diff] [blame] | 259 | mFunctions->blitFramebuffer(sourceArea.x, sourceArea.y, sourceArea.x1(), sourceArea.y1(), |
| 260 | destArea.x, destArea.y, destArea.x1(), destArea.y1(), mask, filter); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 261 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 262 | return Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 263 | } |
| 264 | |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 265 | bool FramebufferGL::checkStatus() const |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 266 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 267 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Jamie Madill | cc86d64 | 2015-11-24 13:00:07 -0500 | [diff] [blame] | 268 | GLenum status = mFunctions->checkFramebufferStatus(GL_FRAMEBUFFER); |
| 269 | if (status != GL_FRAMEBUFFER_COMPLETE) |
| 270 | { |
| 271 | ANGLEPlatformCurrent()->logWarning("GL framebuffer returned incomplete."); |
| 272 | } |
| 273 | return (status == GL_FRAMEBUFFER_COMPLETE); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 274 | } |
| 275 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 276 | void FramebufferGL::syncState(const Framebuffer::DirtyBits &dirtyBits) |
| 277 | { |
| 278 | // Don't need to sync state for the default FBO. |
| 279 | if (mIsDefault) |
| 280 | { |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 285 | |
| 286 | for (auto dirtyBit : angle::IterateBitSet(dirtyBits)) |
| 287 | { |
| 288 | switch (dirtyBit) |
| 289 | { |
| 290 | case Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT: |
| 291 | BindFramebufferAttachment(mFunctions, GL_DEPTH_ATTACHMENT, |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 292 | mState.getDepthAttachment()); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 293 | break; |
| 294 | case Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT: |
| 295 | BindFramebufferAttachment(mFunctions, GL_STENCIL_ATTACHMENT, |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 296 | mState.getStencilAttachment()); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 297 | break; |
| 298 | case Framebuffer::DIRTY_BIT_DRAW_BUFFERS: |
| 299 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 300 | const auto &drawBuffers = mState.getDrawBufferStates(); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 301 | mFunctions->drawBuffers(static_cast<GLsizei>(drawBuffers.size()), |
| 302 | drawBuffers.data()); |
| 303 | break; |
| 304 | } |
| 305 | case Framebuffer::DIRTY_BIT_READ_BUFFER: |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 306 | mFunctions->readBuffer(mState.getReadBufferState()); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 307 | break; |
| 308 | default: |
| 309 | { |
| 310 | ASSERT(Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0 && |
| 311 | dirtyBit < Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX); |
| 312 | size_t index = |
| 313 | static_cast<size_t>(dirtyBit - Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0); |
| 314 | BindFramebufferAttachment(mFunctions, |
| 315 | static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + index), |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 316 | mState.getColorAttachment(index)); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 323 | GLuint FramebufferGL::getFramebufferID() const |
| 324 | { |
| 325 | return mFramebufferID; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 326 | } |
| 327 | |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 328 | void FramebufferGL::syncDrawState() const |
| 329 | { |
| 330 | if (mFunctions->standard == STANDARD_GL_DESKTOP) |
| 331 | { |
| 332 | // Enable SRGB blending for all framebuffers except the default framebuffer on Desktop |
| 333 | // OpenGL. |
| 334 | // When SRGB blending is enabled, only SRGB capable formats will use it but the default |
| 335 | // framebuffer will always use it if it is enabled. |
| 336 | // TODO(geofflang): Update this when the framebuffer binding dirty changes, when it exists. |
| 337 | mStateManager->setFramebufferSRGBEnabled(!mIsDefault); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | void FramebufferGL::syncClearState(GLbitfield mask) |
| 342 | { |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 343 | if (mFunctions->standard == STANDARD_GL_DESKTOP) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 344 | { |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 345 | if (mWorkarounds.doesSRGBClearsOnLinearFramebufferAttachments && |
| 346 | (mask & GL_COLOR_BUFFER_BIT) != 0 && !mIsDefault) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 347 | { |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 348 | bool hasSRBAttachment = false; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 349 | for (const auto &attachment : mState.getColorAttachments()) |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 350 | { |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 351 | if (attachment.isAttached() && attachment.getColorEncoding() == GL_SRGB) |
| 352 | { |
| 353 | hasSRBAttachment = true; |
| 354 | break; |
| 355 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 356 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 357 | |
Frank Henigman | a3d333c | 2016-03-22 22:09:14 -0400 | [diff] [blame] | 358 | mStateManager->setFramebufferSRGBEnabled(hasSRBAttachment); |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | mStateManager->setFramebufferSRGBEnabled(!mIsDefault); |
| 363 | } |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | void FramebufferGL::syncClearBufferState(GLenum buffer, GLint drawBuffer) |
| 368 | { |
| 369 | if (mFunctions->standard == STANDARD_GL_DESKTOP) |
| 370 | { |
| 371 | if (mWorkarounds.doesSRGBClearsOnLinearFramebufferAttachments && buffer == GL_COLOR && |
| 372 | !mIsDefault) |
| 373 | { |
| 374 | // If doing a clear on a color buffer, set SRGB blend enabled only if the color buffer |
| 375 | // is an SRGB format. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 376 | const auto &drawbufferState = mState.getDrawBufferStates(); |
| 377 | const auto &colorAttachments = mState.getColorAttachments(); |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 378 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 379 | const FramebufferAttachment *attachment = nullptr; |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 380 | if (drawbufferState[drawBuffer] >= GL_COLOR_ATTACHMENT0 && |
| 381 | drawbufferState[drawBuffer] < GL_COLOR_ATTACHMENT0 + colorAttachments.size()) |
| 382 | { |
| 383 | size_t attachmentIdx = |
| 384 | static_cast<size_t>(drawbufferState[drawBuffer] - GL_COLOR_ATTACHMENT0); |
| 385 | attachment = &colorAttachments[attachmentIdx]; |
| 386 | } |
| 387 | |
| 388 | if (attachment != nullptr) |
| 389 | { |
| 390 | mStateManager->setFramebufferSRGBEnabled(attachment->getColorEncoding() == GL_SRGB); |
| 391 | } |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | mStateManager->setFramebufferSRGBEnabled(!mIsDefault); |
| 396 | } |
| 397 | } |
| 398 | } |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 399 | } // namespace rx |