blob: 6c2101a9c7f026866988c075795df98418adb4cb [file] [log] [blame]
Geoff Langf9a6f082015-01-22 13:32:49 -05001//
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 Madilld1f5ef22015-04-01 14:17:06 -040012#include "libANGLE/Data.h"
Jamie Madill87de3622015-03-16 10:41:44 -040013#include "libANGLE/State.h"
Geoff Lang4ad17092015-03-10 16:47:44 -040014#include "libANGLE/FramebufferAttachment.h"
15#include "libANGLE/angletypes.h"
16#include "libANGLE/formatutils.h"
17#include "libANGLE/renderer/gl/FunctionsGL.h"
Jacek Cabanfa60f692015-04-27 18:23:44 +020018#include "libANGLE/renderer/gl/RenderbufferGL.h"
Geoff Lang4ad17092015-03-10 16:47:44 -040019#include "libANGLE/renderer/gl/StateManagerGL.h"
20#include "libANGLE/renderer/gl/TextureGL.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050021
22namespace rx
23{
24
Geoff Lang4ad17092015-03-10 16:47:44 -040025FramebufferGL::FramebufferGL(const gl::Framebuffer::Data &data, const FunctionsGL *functions, StateManagerGL *stateManager, bool isDefault)
26 : FramebufferImpl(data),
27 mFunctions(functions),
28 mStateManager(stateManager),
Corentin Wallez6ab01b92015-08-03 10:16:36 -070029 mFramebufferID(0),
30 mIsDefault(isDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -040031{
Corentin Wallez6ab01b92015-08-03 10:16:36 -070032 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -040033 {
34 mFunctions->genFramebuffers(1, &mFramebufferID);
35 }
36}
Geoff Langf9a6f082015-01-22 13:32:49 -050037
38FramebufferGL::~FramebufferGL()
Geoff Lang4ad17092015-03-10 16:47:44 -040039{
Geoff Lang1eb708e2015-05-04 14:58:23 -040040 mStateManager->deleteFramebuffer(mFramebufferID);
41 mFramebufferID = 0;
Geoff Lang4ad17092015-03-10 16:47:44 -040042}
43
44static void BindFramebufferAttachment(const FunctionsGL *functions, GLenum attachmentPoint,
45 const gl::FramebufferAttachment *attachment)
46{
47 if (attachment)
48 {
49 if (attachment->type() == GL_TEXTURE)
50 {
Jamie Madill5160ec12015-04-14 08:13:48 -040051 const gl::Texture *texture = attachment->getTexture();
Geoff Lang4ad17092015-03-10 16:47:44 -040052 const TextureGL *textureGL = GetImplAs<TextureGL>(texture);
53
54 if (texture->getTarget() == GL_TEXTURE_2D)
55 {
56 functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D,
57 textureGL->getTextureID(), attachment->mipLevel());
58 }
59 else if (texture->getTarget() == GL_TEXTURE_CUBE_MAP)
60 {
61 functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, attachment->cubeMapFace(),
62 textureGL->getTextureID(), attachment->mipLevel());
63 }
64 else if (texture->getTarget() == GL_TEXTURE_2D_ARRAY || texture->getTarget() == GL_TEXTURE_3D)
65 {
66 functions->framebufferTextureLayer(GL_FRAMEBUFFER, attachmentPoint, textureGL->getTextureID(),
67 attachment->mipLevel(), attachment->layer());
68 }
69 else
70 {
71 UNREACHABLE();
72 }
73 }
74 else if (attachment->type() == GL_RENDERBUFFER)
75 {
Jamie Madill5160ec12015-04-14 08:13:48 -040076 const gl::Renderbuffer *renderbuffer = attachment->getRenderbuffer();
Geoff Langcd69f1c2015-03-18 14:33:23 -040077 const RenderbufferGL *renderbufferGL = GetImplAs<RenderbufferGL>(renderbuffer);
Geoff Lang4ad17092015-03-10 16:47:44 -040078
Geoff Langcd69f1c2015-03-18 14:33:23 -040079 functions->framebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER,
80 renderbufferGL->getRenderbufferID());
Geoff Lang4ad17092015-03-10 16:47:44 -040081 }
82 else
83 {
84 UNREACHABLE();
85 }
86 }
87 else
88 {
89 // Unbind this attachment
90 functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, 0, 0);
91 }
92}
Geoff Langf9a6f082015-01-22 13:32:49 -050093
Jamie Madill7d75e2b2015-04-30 09:42:18 -040094void FramebufferGL::onUpdateColorAttachment(size_t index)
Geoff Langf9a6f082015-01-22 13:32:49 -050095{
Corentin Wallez6ab01b92015-08-03 10:16:36 -070096 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -040097 {
98 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madillb1956432015-08-12 17:35:20 +000099 BindFramebufferAttachment(mFunctions,
100 GL_COLOR_ATTACHMENT0 + index,
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400101 mData.getColorAttachment(static_cast<unsigned int>(index)));
Geoff Lang4ad17092015-03-10 16:47:44 -0400102 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500103}
104
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400105void FramebufferGL::onUpdateDepthAttachment()
Geoff Langf9a6f082015-01-22 13:32:49 -0500106{
Corentin Wallez6ab01b92015-08-03 10:16:36 -0700107 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -0400108 {
109 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400110 BindFramebufferAttachment(mFunctions,
111 GL_DEPTH_ATTACHMENT,
112 mData.getDepthAttachment());
Geoff Lang4ad17092015-03-10 16:47:44 -0400113 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500114}
115
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400116void FramebufferGL::onUpdateStencilAttachment()
Geoff Langf9a6f082015-01-22 13:32:49 -0500117{
Corentin Wallez6ab01b92015-08-03 10:16:36 -0700118 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -0400119 {
120 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400121 BindFramebufferAttachment(mFunctions,
122 GL_STENCIL_ATTACHMENT,
123 mData.getStencilAttachment());
Geoff Lang4ad17092015-03-10 16:47:44 -0400124 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500125}
126
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400127void FramebufferGL::onUpdateDepthStencilAttachment()
Geoff Langf9a6f082015-01-22 13:32:49 -0500128{
Corentin Wallez6ab01b92015-08-03 10:16:36 -0700129 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -0400130 {
131 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400132 BindFramebufferAttachment(mFunctions,
133 GL_DEPTH_STENCIL_ATTACHMENT,
134 mData.getDepthStencilAttachment());
Geoff Lang4ad17092015-03-10 16:47:44 -0400135 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500136}
137
138void FramebufferGL::setDrawBuffers(size_t count, const GLenum *buffers)
139{
Corentin Wallez6ab01b92015-08-03 10:16:36 -0700140 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -0400141 {
142 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madillb1956432015-08-12 17:35:20 +0000143 mFunctions->drawBuffers(count, buffers);
Geoff Lang4ad17092015-03-10 16:47:44 -0400144 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500145}
146
147void FramebufferGL::setReadBuffer(GLenum buffer)
148{
Corentin Wallez6ab01b92015-08-03 10:16:36 -0700149 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -0400150 {
151 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
152 mFunctions->readBuffer(buffer);
153 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500154}
155
Austin Kinross08332632015-05-05 13:35:47 -0700156gl::Error FramebufferGL::discard(size_t count, const GLenum *attachments)
157{
158 UNIMPLEMENTED();
159 return gl::Error(GL_INVALID_OPERATION);
160}
161
Geoff Langf9a6f082015-01-22 13:32:49 -0500162gl::Error FramebufferGL::invalidate(size_t count, const GLenum *attachments)
163{
Geoff Lang64a72442015-04-01 14:43:11 -0400164 // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is available.
165 if (mFunctions->invalidateFramebuffer)
166 {
167 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madillb1956432015-08-12 17:35:20 +0000168 mFunctions->invalidateFramebuffer(GL_FRAMEBUFFER, count, attachments);
Geoff Lang64a72442015-04-01 14:43:11 -0400169 }
170
Geoff Lang4ad17092015-03-10 16:47:44 -0400171 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500172}
173
174gl::Error FramebufferGL::invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area)
175{
Geoff Lang64a72442015-04-01 14:43:11 -0400176 // 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);
Jamie Madillb1956432015-08-12 17:35:20 +0000180 mFunctions->invalidateSubFramebuffer(GL_FRAMEBUFFER, count, attachments, area.x, area.y, area.width, area.height);
Geoff Lang64a72442015-04-01 14:43:11 -0400181 }
182
Geoff Lang4ad17092015-03-10 16:47:44 -0400183 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500184}
185
Jamie Madilld1f5ef22015-04-01 14:17:06 -0400186gl::Error FramebufferGL::clear(const gl::Data &data, GLbitfield mask)
Geoff Langf9a6f082015-01-22 13:32:49 -0500187{
Geoff Lang4ad17092015-03-10 16:47:44 -0400188 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
189 mFunctions->clear(mask);
190
191 return gl::Error(GL_NO_ERROR);
192}
193
Geoff Langf9a6f082015-01-22 13:32:49 -0500194gl::Error FramebufferGL::clearBufferfv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLfloat *values)
195{
Geoff Lang4ad17092015-03-10 16:47:44 -0400196 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
197 mFunctions->clearBufferfv(buffer, drawbuffer, values);
198
199 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500200}
201
202gl::Error FramebufferGL::clearBufferuiv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLuint *values)
203{
Geoff Lang4ad17092015-03-10 16:47:44 -0400204 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
205 mFunctions->clearBufferuiv(buffer, drawbuffer, values);
206
207 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500208}
209
210gl::Error FramebufferGL::clearBufferiv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLint *values)
211{
Geoff Lang4ad17092015-03-10 16:47:44 -0400212 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
213 mFunctions->clearBufferiv(buffer, drawbuffer, values);
214
215 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500216}
217
218gl::Error FramebufferGL::clearBufferfi(const gl::State &state, GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil)
219{
Geoff Lang4ad17092015-03-10 16:47:44 -0400220 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
221 mFunctions->clearBufferfi(buffer, drawbuffer, depth, stencil);
222
223 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500224}
225
226GLenum FramebufferGL::getImplementationColorReadFormat() const
227{
Geoff Lang4ad17092015-03-10 16:47:44 -0400228 const gl::FramebufferAttachment *readAttachment = getData().getReadAttachment();
229 GLenum internalFormat = readAttachment->getInternalFormat();
230 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
231 return internalFormatInfo.format;
Geoff Langf9a6f082015-01-22 13:32:49 -0500232}
233
234GLenum FramebufferGL::getImplementationColorReadType() const
235{
Geoff Lang4ad17092015-03-10 16:47:44 -0400236 const gl::FramebufferAttachment *readAttachment = getData().getReadAttachment();
237 GLenum internalFormat = readAttachment->getInternalFormat();
238 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
239 return internalFormatInfo.type;
Geoff Langf9a6f082015-01-22 13:32:49 -0500240}
241
242gl::Error FramebufferGL::readPixels(const gl::State &state, const gl::Rectangle &area, GLenum format, GLenum type, GLvoid *pixels) const
243{
Jamie Madill87de3622015-03-16 10:41:44 -0400244 const gl::PixelPackState &packState = state.getPackState();
Geoff Langafce8292015-06-05 16:25:16 -0400245 if (packState.pixelBuffer.get() != nullptr)
Jamie Madill87de3622015-03-16 10:41:44 -0400246 {
247 UNIMPLEMENTED();
Jamie Madill87de3622015-03-16 10:41:44 -0400248 }
249
Geoff Lang4ad17092015-03-10 16:47:44 -0400250 mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferID);
251 mFunctions->readPixels(area.x, area.y, area.width, area.height, format, type, pixels);
252
253 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500254}
255
256gl::Error FramebufferGL::blit(const gl::State &state, const gl::Rectangle &sourceArea, const gl::Rectangle &destArea,
257 GLbitfield mask, GLenum filter, const gl::Framebuffer *sourceFramebuffer)
258{
Geoff Lang4ad17092015-03-10 16:47:44 -0400259 const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer);
260
261 mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID());
262 mStateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID);
263
264 mFunctions->blitFramebuffer(sourceArea.x, sourceArea.y, sourceArea.x + sourceArea.width, sourceArea.y + sourceArea.height,
265 destArea.x, destArea.y, destArea.x + destArea.width, destArea.y + destArea.height,
266 mask, filter);
267
268 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500269}
270
271GLenum FramebufferGL::checkStatus() const
272{
Geoff Lang4ad17092015-03-10 16:47:44 -0400273 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
274 return mFunctions->checkFramebufferStatus(GL_FRAMEBUFFER);
275}
276
277GLuint FramebufferGL::getFramebufferID() const
278{
279 return mFramebufferID;
Geoff Langf9a6f082015-01-22 13:32:49 -0500280}
281
282}