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