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