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" |
| 18 | #include "libANGLE/renderer/gl/RenderBufferGL.h" |
| 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 | { |
| 53 | const gl::Texture *texture = GetAs<gl::TextureAttachment>(attachment)->getTexture(); |
| 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 | { |
Geoff Lang | cd69f1c | 2015-03-18 14:33:23 -0400 | [diff] [blame] | 78 | const gl::Renderbuffer *renderbuffer = GetAs<gl::RenderbufferAttachment>(attachment)->getRenderbuffer(); |
| 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 | |
| 96 | void FramebufferGL::setColorAttachment(size_t index, const gl::FramebufferAttachment *attachment) |
| 97 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 98 | if (mFramebufferID != 0) |
| 99 | { |
| 100 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 101 | BindFramebufferAttachment(mFunctions, GL_COLOR_ATTACHMENT0 + index, attachment); |
| 102 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Jamie Madill | f90353e | 2015-03-05 19:37:58 -0500 | [diff] [blame] | 105 | void FramebufferGL::setDepthAttachment(const gl::FramebufferAttachment *attachment) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 106 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 107 | if (mFramebufferID != 0) |
| 108 | { |
| 109 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 110 | BindFramebufferAttachment(mFunctions, GL_DEPTH_ATTACHMENT, attachment); |
| 111 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void FramebufferGL::setStencilAttachment(const gl::FramebufferAttachment *attachment) |
| 115 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 116 | if (mFramebufferID != 0) |
| 117 | { |
| 118 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 119 | BindFramebufferAttachment(mFunctions, GL_STENCIL_ATTACHMENT, attachment); |
| 120 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void FramebufferGL::setDepthStencilAttachment(const gl::FramebufferAttachment *attachment) |
| 124 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 125 | if (mFramebufferID != 0) |
| 126 | { |
| 127 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 128 | BindFramebufferAttachment(mFunctions, GL_DEPTH_STENCIL_ATTACHMENT, attachment); |
| 129 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void FramebufferGL::setDrawBuffers(size_t count, const GLenum *buffers) |
| 133 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 134 | if (mFramebufferID != 0) |
| 135 | { |
| 136 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 137 | mFunctions->drawBuffers(count, buffers); |
| 138 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void FramebufferGL::setReadBuffer(GLenum buffer) |
| 142 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 143 | if (mFramebufferID != 0) |
| 144 | { |
| 145 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 146 | mFunctions->readBuffer(buffer); |
| 147 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | gl::Error FramebufferGL::invalidate(size_t count, const GLenum *attachments) |
| 151 | { |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame^] | 152 | // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is available. |
| 153 | if (mFunctions->invalidateFramebuffer) |
| 154 | { |
| 155 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 156 | mFunctions->invalidateFramebuffer(GL_FRAMEBUFFER, count, attachments); |
| 157 | } |
| 158 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 159 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | gl::Error FramebufferGL::invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area) |
| 163 | { |
Geoff Lang | 64a7244 | 2015-04-01 14:43:11 -0400 | [diff] [blame^] | 164 | // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is available. |
| 165 | if (mFunctions->invalidateSubFramebuffer) |
| 166 | { |
| 167 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 168 | mFunctions->invalidateSubFramebuffer(GL_FRAMEBUFFER, count, attachments, area.x, area.y, area.width, area.height); |
| 169 | } |
| 170 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 171 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Jamie Madill | d1f5ef2 | 2015-04-01 14:17:06 -0400 | [diff] [blame] | 174 | gl::Error FramebufferGL::clear(const gl::Data &data, GLbitfield mask) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 175 | { |
Jamie Madill | d1f5ef2 | 2015-04-01 14:17:06 -0400 | [diff] [blame] | 176 | mStateManager->setClearState(*data.state, mask); |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 177 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 178 | mFunctions->clear(mask); |
| 179 | |
| 180 | return gl::Error(GL_NO_ERROR); |
| 181 | } |
| 182 | |
| 183 | static GLbitfield GetClearBufferMask(GLenum buffer) |
| 184 | { |
| 185 | switch (buffer) |
| 186 | { |
| 187 | case GL_COLOR: return GL_COLOR_BUFFER_BIT; |
| 188 | case GL_DEPTH: return GL_DEPTH_BUFFER_BIT; |
| 189 | case GL_STENCIL: return GL_STENCIL_BUFFER_BIT; |
| 190 | case GL_DEPTH_STENCIL: return GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT; |
| 191 | default: UNREACHABLE(); return 0; |
| 192 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | gl::Error FramebufferGL::clearBufferfv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLfloat *values) |
| 196 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 197 | mStateManager->setClearState(state, GetClearBufferMask(buffer)); |
| 198 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 199 | mFunctions->clearBufferfv(buffer, drawbuffer, values); |
| 200 | |
| 201 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | gl::Error FramebufferGL::clearBufferuiv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLuint *values) |
| 205 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 206 | mStateManager->setClearState(state, GetClearBufferMask(buffer)); |
| 207 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 208 | mFunctions->clearBufferuiv(buffer, drawbuffer, values); |
| 209 | |
| 210 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | gl::Error FramebufferGL::clearBufferiv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLint *values) |
| 214 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 215 | mStateManager->setClearState(state, GetClearBufferMask(buffer)); |
| 216 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 217 | mFunctions->clearBufferiv(buffer, drawbuffer, values); |
| 218 | |
| 219 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | gl::Error FramebufferGL::clearBufferfi(const gl::State &state, GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil) |
| 223 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 224 | mStateManager->setClearState(state, GetClearBufferMask(buffer)); |
| 225 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 226 | mFunctions->clearBufferfi(buffer, drawbuffer, depth, stencil); |
| 227 | |
| 228 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | GLenum FramebufferGL::getImplementationColorReadFormat() const |
| 232 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 233 | const gl::FramebufferAttachment *readAttachment = getData().getReadAttachment(); |
| 234 | GLenum internalFormat = readAttachment->getInternalFormat(); |
| 235 | const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat); |
| 236 | return internalFormatInfo.format; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | GLenum FramebufferGL::getImplementationColorReadType() 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.type; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | gl::Error FramebufferGL::readPixels(const gl::State &state, const gl::Rectangle &area, GLenum format, GLenum type, GLvoid *pixels) const |
| 248 | { |
Jamie Madill | 87de362 | 2015-03-16 10:41:44 -0400 | [diff] [blame] | 249 | const gl::PixelPackState &packState = state.getPackState(); |
| 250 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 251 | // TODO: set pack state |
Jamie Madill | 87de362 | 2015-03-16 10:41:44 -0400 | [diff] [blame] | 252 | if (packState.rowLength != 0 || packState.skipRows != 0 || packState.skipPixels != 0) |
| 253 | { |
| 254 | UNIMPLEMENTED(); |
| 255 | return gl::Error(GL_INVALID_OPERATION, "invalid pixel store parameters in readPixels"); |
| 256 | } |
| 257 | |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 258 | mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferID); |
| 259 | mFunctions->readPixels(area.x, area.y, area.width, area.height, format, type, pixels); |
| 260 | |
| 261 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | gl::Error FramebufferGL::blit(const gl::State &state, const gl::Rectangle &sourceArea, const gl::Rectangle &destArea, |
| 265 | GLbitfield mask, GLenum filter, const gl::Framebuffer *sourceFramebuffer) |
| 266 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 267 | const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer); |
| 268 | |
| 269 | mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID()); |
| 270 | mStateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID); |
| 271 | |
| 272 | mFunctions->blitFramebuffer(sourceArea.x, sourceArea.y, sourceArea.x + sourceArea.width, sourceArea.y + sourceArea.height, |
| 273 | destArea.x, destArea.y, destArea.x + destArea.width, destArea.y + destArea.height, |
| 274 | mask, filter); |
| 275 | |
| 276 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | GLenum FramebufferGL::checkStatus() const |
| 280 | { |
Geoff Lang | 4ad1709 | 2015-03-10 16:47:44 -0400 | [diff] [blame] | 281 | mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID); |
| 282 | return mFunctions->checkFramebufferStatus(GL_FRAMEBUFFER); |
| 283 | } |
| 284 | |
| 285 | GLuint FramebufferGL::getFramebufferID() const |
| 286 | { |
| 287 | return mFramebufferID; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | } |