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 | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 21 | |
| 22 | namespace rx |
| 23 | { |
| 24 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 25 | FramebufferGL::FramebufferGL(const gl::Framebuffer::Data &data, const FunctionsGL *functions, StateManagerGL *stateManager, bool isDefault) |
| 26 | : FramebufferImpl(data), |
| 27 | mFunctions(functions), |
| 28 | mStateManager(stateManager), |
| 29 | mFramebufferID(0) |
| 30 | { |
| 31 | if (!isDefault) |
| 32 | { |
| 33 | mFunctions->genFramebuffers(1, &mFramebufferID); |
| 34 | } |
| 35 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 36 | |
| 37 | FramebufferGL::~FramebufferGL() |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 38 | { |
| 39 | if (mFramebufferID != 0) |
| 40 | { |
| 41 | mFunctions->deleteFramebuffers(1, &mFramebufferID); |
| 42 | mFramebufferID = 0; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | static void BindFramebufferAttachment(const FunctionsGL *functions, GLenum attachmentPoint, |
| 47 | const gl::FramebufferAttachment *attachment) |
| 48 | { |
| 49 | if (attachment) |
| 50 | { |
| 51 | if (attachment->type() == GL_TEXTURE) |
| 52 | { |
Jamie Madill | 5160ec1 | 2015-04-14 08:13:48 -0400 | [diff] [blame] | 53 | const gl::Texture *texture = attachment->getTexture(); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 54 | const TextureGL *textureGL = GetImplAs<TextureGL>(texture); |
| 55 | |
| 56 | if (texture->getTarget() == GL_TEXTURE_2D) |
| 57 | { |
| 58 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, |
| 59 | textureGL->getTextureID(), attachment->mipLevel()); |
| 60 | } |
| 61 | else if (texture->getTarget() == GL_TEXTURE_CUBE_MAP) |
| 62 | { |
| 63 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, attachment->cubeMapFace(), |
| 64 | textureGL->getTextureID(), attachment->mipLevel()); |
| 65 | } |
| 66 | else if (texture->getTarget() == GL_TEXTURE_2D_ARRAY || texture->getTarget() == GL_TEXTURE_3D) |
| 67 | { |
| 68 | functions->framebufferTextureLayer(GL_FRAMEBUFFER, attachmentPoint, textureGL->getTextureID(), |
| 69 | attachment->mipLevel(), attachment->layer()); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | UNREACHABLE(); |
| 74 | } |
| 75 | } |
| 76 | else if (attachment->type() == GL_RENDERBUFFER) |
| 77 | { |
Jamie Madill | 5160ec1 | 2015-04-14 08:13:48 -0400 | [diff] [blame] | 78 | const gl::Renderbuffer *renderbuffer = attachment->getRenderbuffer(); |
Geoff Lang | cd69f1c | 2015-03-18 14:33:23 -0400 | [diff] [blame] | 79 | const RenderbufferGL *renderbufferGL = GetImplAs<RenderbufferGL>(renderbuffer); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 80 | |
Geoff Lang | cd69f1c | 2015-03-18 14:33:23 -0400 | [diff] [blame] | 81 | functions->framebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER, |
| 82 | renderbufferGL->getRenderbufferID()); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 83 | } |
| 84 | else |
| 85 | { |
| 86 | UNREACHABLE(); |
| 87 | } |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | // Unbind this attachment |
| 92 | functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, 0, 0); |
| 93 | } |
| 94 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 95 | |
Jamie Madill | 7d75e2b | 2015-04-30 09:42:18 -0400 | [diff] [blame^] | 96 | void FramebufferGL::onUpdateColorAttachment(size_t index) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 97 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 98 | if (mFramebufferID != 0) |
| 99 | { |
| 100 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Jamie Madill | 7d75e2b | 2015-04-30 09:42:18 -0400 | [diff] [blame^] | 101 | BindFramebufferAttachment(mFunctions, |
| 102 | GL_COLOR_ATTACHMENT0 + index, |
| 103 | mData.getColorAttachment(static_cast<unsigned int>(index))); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 104 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Jamie Madill | 7d75e2b | 2015-04-30 09:42:18 -0400 | [diff] [blame^] | 107 | void FramebufferGL::onUpdateDepthAttachment() |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 108 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 109 | if (mFramebufferID != 0) |
| 110 | { |
| 111 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Jamie Madill | 7d75e2b | 2015-04-30 09:42:18 -0400 | [diff] [blame^] | 112 | BindFramebufferAttachment(mFunctions, |
| 113 | GL_DEPTH_ATTACHMENT, |
| 114 | mData.getDepthAttachment()); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 115 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Jamie Madill | 7d75e2b | 2015-04-30 09:42:18 -0400 | [diff] [blame^] | 118 | void FramebufferGL::onUpdateStencilAttachment() |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 119 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 120 | if (mFramebufferID != 0) |
| 121 | { |
| 122 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Jamie Madill | 7d75e2b | 2015-04-30 09:42:18 -0400 | [diff] [blame^] | 123 | BindFramebufferAttachment(mFunctions, |
| 124 | GL_STENCIL_ATTACHMENT, |
| 125 | mData.getStencilAttachment()); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 126 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Jamie Madill | 7d75e2b | 2015-04-30 09:42:18 -0400 | [diff] [blame^] | 129 | void FramebufferGL::onUpdateDepthStencilAttachment() |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 130 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 131 | if (mFramebufferID != 0) |
| 132 | { |
| 133 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
Jamie Madill | 7d75e2b | 2015-04-30 09:42:18 -0400 | [diff] [blame^] | 134 | BindFramebufferAttachment(mFunctions, |
| 135 | GL_DEPTH_STENCIL_ATTACHMENT, |
| 136 | mData.getDepthStencilAttachment()); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 137 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void FramebufferGL::setDrawBuffers(size_t count, const GLenum *buffers) |
| 141 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 142 | if (mFramebufferID != 0) |
| 143 | { |
| 144 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 145 | mFunctions->drawBuffers(count, buffers); |
| 146 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void FramebufferGL::setReadBuffer(GLenum buffer) |
| 150 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 151 | if (mFramebufferID != 0) |
| 152 | { |
| 153 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 154 | mFunctions->readBuffer(buffer); |
| 155 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | gl::Error FramebufferGL::invalidate(size_t count, const GLenum *attachments) |
| 159 | { |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 160 | // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is available. |
| 161 | if (mFunctions->invalidateFramebuffer) |
| 162 | { |
| 163 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 164 | mFunctions->invalidateFramebuffer(GL_FRAMEBUFFER, count, attachments); |
| 165 | } |
| 166 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 167 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | gl::Error FramebufferGL::invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area) |
| 171 | { |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame] | 172 | // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is available. |
| 173 | if (mFunctions->invalidateSubFramebuffer) |
| 174 | { |
| 175 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 176 | mFunctions->invalidateSubFramebuffer(GL_FRAMEBUFFER, count, attachments, area.x, area.y, area.width, area.height); |
| 177 | } |
| 178 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 179 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 180 | } |
| 181 | |
Jamie Madill | d1f5ef2 | 2015-04-01 14:17:06 -0400 | [diff] [blame] | 182 | gl::Error FramebufferGL::clear(const gl::Data &data, GLbitfield mask) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 183 | { |
Jamie Madill | d1f5ef2 | 2015-04-01 14:17:06 -0400 | [diff] [blame] | 184 | mStateManager->setClearState(*data.state, mask); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 185 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 186 | mFunctions->clear(mask); |
| 187 | |
| 188 | return gl::Error(GL_NO_ERROR); |
| 189 | } |
| 190 | |
| 191 | static GLbitfield GetClearBufferMask(GLenum buffer) |
| 192 | { |
| 193 | switch (buffer) |
| 194 | { |
| 195 | case GL_COLOR: return GL_COLOR_BUFFER_BIT; |
| 196 | case GL_DEPTH: return GL_DEPTH_BUFFER_BIT; |
| 197 | case GL_STENCIL: return GL_STENCIL_BUFFER_BIT; |
| 198 | case GL_DEPTH_STENCIL: return GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT; |
| 199 | default: UNREACHABLE(); return 0; |
| 200 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | gl::Error FramebufferGL::clearBufferfv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLfloat *values) |
| 204 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 205 | mStateManager->setClearState(state, GetClearBufferMask(buffer)); |
| 206 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 207 | mFunctions->clearBufferfv(buffer, drawbuffer, values); |
| 208 | |
| 209 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | gl::Error FramebufferGL::clearBufferuiv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLuint *values) |
| 213 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 214 | mStateManager->setClearState(state, GetClearBufferMask(buffer)); |
| 215 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 216 | mFunctions->clearBufferuiv(buffer, drawbuffer, values); |
| 217 | |
| 218 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | gl::Error FramebufferGL::clearBufferiv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLint *values) |
| 222 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 223 | mStateManager->setClearState(state, GetClearBufferMask(buffer)); |
| 224 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 225 | mFunctions->clearBufferiv(buffer, drawbuffer, values); |
| 226 | |
| 227 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | gl::Error FramebufferGL::clearBufferfi(const gl::State &state, GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil) |
| 231 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 232 | mStateManager->setClearState(state, GetClearBufferMask(buffer)); |
| 233 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 234 | mFunctions->clearBufferfi(buffer, drawbuffer, depth, stencil); |
| 235 | |
| 236 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | GLenum FramebufferGL::getImplementationColorReadFormat() const |
| 240 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 241 | const gl::FramebufferAttachment *readAttachment = getData().getReadAttachment(); |
| 242 | GLenum internalFormat = readAttachment->getInternalFormat(); |
| 243 | const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat); |
| 244 | return internalFormatInfo.format; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | GLenum FramebufferGL::getImplementationColorReadType() const |
| 248 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 249 | const gl::FramebufferAttachment *readAttachment = getData().getReadAttachment(); |
| 250 | GLenum internalFormat = readAttachment->getInternalFormat(); |
| 251 | const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat); |
| 252 | return internalFormatInfo.type; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | gl::Error FramebufferGL::readPixels(const gl::State &state, const gl::Rectangle &area, GLenum format, GLenum type, GLvoid *pixels) const |
| 256 | { |
Jamie Madill | 87de362 | 2015-03-16 10:41:44 -0400 | [diff] [blame] | 257 | const gl::PixelPackState &packState = state.getPackState(); |
| 258 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 259 | // TODO: set pack state |
Jamie Madill | 87de362 | 2015-03-16 10:41:44 -0400 | [diff] [blame] | 260 | if (packState.rowLength != 0 || packState.skipRows != 0 || packState.skipPixels != 0) |
| 261 | { |
| 262 | UNIMPLEMENTED(); |
| 263 | return gl::Error(GL_INVALID_OPERATION, "invalid pixel store parameters in readPixels"); |
| 264 | } |
| 265 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 266 | mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferID); |
| 267 | mFunctions->readPixels(area.x, area.y, area.width, area.height, format, type, pixels); |
| 268 | |
| 269 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | gl::Error FramebufferGL::blit(const gl::State &state, const gl::Rectangle &sourceArea, const gl::Rectangle &destArea, |
| 273 | GLbitfield mask, GLenum filter, const gl::Framebuffer *sourceFramebuffer) |
| 274 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 275 | const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer); |
| 276 | |
| 277 | mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID()); |
| 278 | mStateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID); |
| 279 | |
| 280 | mFunctions->blitFramebuffer(sourceArea.x, sourceArea.y, sourceArea.x + sourceArea.width, sourceArea.y + sourceArea.height, |
| 281 | destArea.x, destArea.y, destArea.x + destArea.width, destArea.y + destArea.height, |
| 282 | mask, filter); |
| 283 | |
| 284 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | GLenum FramebufferGL::checkStatus() const |
| 288 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 289 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 290 | return mFunctions->checkFramebufferStatus(GL_FRAMEBUFFER); |
| 291 | } |
| 292 | |
| 293 | GLuint FramebufferGL::getFramebufferID() const |
| 294 | { |
| 295 | return mFramebufferID; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | } |