blob: a2c0784a30d04c76c61c1dd0d30cadb9c7fc4c0c [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 Langafd7f0a2015-09-09 15:33:31 -040021#include "libANGLE/renderer/gl/WorkaroundsGL.h"
Jamie Madillcc86d642015-11-24 13:00:07 -050022#include "platform/Platform.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050023
24namespace rx
25{
26
Geoff Langafd7f0a2015-09-09 15:33:31 -040027FramebufferGL::FramebufferGL(const gl::Framebuffer::Data &data,
28 const FunctionsGL *functions,
29 StateManagerGL *stateManager,
30 const WorkaroundsGL &workarounds,
31 bool isDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -040032 : FramebufferImpl(data),
33 mFunctions(functions),
34 mStateManager(stateManager),
Geoff Langafd7f0a2015-09-09 15:33:31 -040035 mWorkarounds(workarounds),
Corentin Wallez6ab01b92015-08-03 10:16:36 -070036 mFramebufferID(0),
37 mIsDefault(isDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -040038{
Corentin Wallez6ab01b92015-08-03 10:16:36 -070039 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -040040 {
41 mFunctions->genFramebuffers(1, &mFramebufferID);
42 }
43}
Geoff Langf9a6f082015-01-22 13:32:49 -050044
Corentin Wallez86f8dd72015-08-12 12:37:48 -070045FramebufferGL::FramebufferGL(GLuint id,
46 const gl::Framebuffer::Data &data,
47 const FunctionsGL *functions,
Geoff Langafd7f0a2015-09-09 15:33:31 -040048 const WorkaroundsGL &workarounds,
Corentin Wallez86f8dd72015-08-12 12:37:48 -070049 StateManagerGL *stateManager)
50 : FramebufferImpl(data),
51 mFunctions(functions),
52 mStateManager(stateManager),
Geoff Langafd7f0a2015-09-09 15:33:31 -040053 mWorkarounds(workarounds),
Corentin Wallez86f8dd72015-08-12 12:37:48 -070054 mFramebufferID(id),
55 mIsDefault(true)
56{
57}
58
Geoff Langf9a6f082015-01-22 13:32:49 -050059FramebufferGL::~FramebufferGL()
Geoff Lang4ad17092015-03-10 16:47:44 -040060{
Geoff Lang1eb708e2015-05-04 14:58:23 -040061 mStateManager->deleteFramebuffer(mFramebufferID);
62 mFramebufferID = 0;
Geoff Lang4ad17092015-03-10 16:47:44 -040063}
64
65static 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 Madill5160ec12015-04-14 08:13:48 -040072 const gl::Texture *texture = attachment->getTexture();
Geoff Lang4ad17092015-03-10 16:47:44 -040073 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 Madill5160ec12015-04-14 08:13:48 -040097 const gl::Renderbuffer *renderbuffer = attachment->getRenderbuffer();
Geoff Langcd69f1c2015-03-18 14:33:23 -040098 const RenderbufferGL *renderbufferGL = GetImplAs<RenderbufferGL>(renderbuffer);
Geoff Lang4ad17092015-03-10 16:47:44 -040099
Geoff Langcd69f1c2015-03-18 14:33:23 -0400100 functions->framebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER,
101 renderbufferGL->getRenderbufferID());
Geoff Lang4ad17092015-03-10 16:47:44 -0400102 }
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 Langf9a6f082015-01-22 13:32:49 -0500114
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400115void FramebufferGL::onUpdateColorAttachment(size_t index)
Geoff Langf9a6f082015-01-22 13:32:49 -0500116{
Corentin Wallez6ab01b92015-08-03 10:16:36 -0700117 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -0400118 {
119 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700120 BindFramebufferAttachment(mFunctions, GL_COLOR_ATTACHMENT0 + static_cast<GLenum>(index),
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400121 mData.getColorAttachment(static_cast<unsigned int>(index)));
Geoff Lang4ad17092015-03-10 16:47:44 -0400122 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500123}
124
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400125void FramebufferGL::onUpdateDepthAttachment()
Geoff Langf9a6f082015-01-22 13:32:49 -0500126{
Corentin Wallez6ab01b92015-08-03 10:16:36 -0700127 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -0400128 {
129 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400130 BindFramebufferAttachment(mFunctions,
131 GL_DEPTH_ATTACHMENT,
132 mData.getDepthAttachment());
Geoff Lang4ad17092015-03-10 16:47:44 -0400133 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500134}
135
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400136void FramebufferGL::onUpdateStencilAttachment()
Geoff Langf9a6f082015-01-22 13:32:49 -0500137{
Corentin Wallez6ab01b92015-08-03 10:16:36 -0700138 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -0400139 {
140 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400141 BindFramebufferAttachment(mFunctions,
142 GL_STENCIL_ATTACHMENT,
143 mData.getStencilAttachment());
Geoff Lang4ad17092015-03-10 16:47:44 -0400144 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500145}
146
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400147void FramebufferGL::onUpdateDepthStencilAttachment()
Geoff Langf9a6f082015-01-22 13:32:49 -0500148{
Corentin Wallez6ab01b92015-08-03 10:16:36 -0700149 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -0400150 {
151 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madill7d75e2b2015-04-30 09:42:18 -0400152 BindFramebufferAttachment(mFunctions,
153 GL_DEPTH_STENCIL_ATTACHMENT,
154 mData.getDepthStencilAttachment());
Geoff Lang4ad17092015-03-10 16:47:44 -0400155 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500156}
157
158void FramebufferGL::setDrawBuffers(size_t count, const GLenum *buffers)
159{
Corentin Wallez6ab01b92015-08-03 10:16:36 -0700160 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -0400161 {
162 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700163 mFunctions->drawBuffers(static_cast<GLsizei>(count), buffers);
Geoff Lang4ad17092015-03-10 16:47:44 -0400164 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500165}
166
167void FramebufferGL::setReadBuffer(GLenum buffer)
168{
Corentin Wallez6ab01b92015-08-03 10:16:36 -0700169 if (!mIsDefault)
Geoff Lang4ad17092015-03-10 16:47:44 -0400170 {
171 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
172 mFunctions->readBuffer(buffer);
173 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500174}
175
Austin Kinross08332632015-05-05 13:35:47 -0700176gl::Error FramebufferGL::discard(size_t count, const GLenum *attachments)
177{
178 UNIMPLEMENTED();
179 return gl::Error(GL_INVALID_OPERATION);
180}
181
Geoff Langf9a6f082015-01-22 13:32:49 -0500182gl::Error FramebufferGL::invalidate(size_t count, const GLenum *attachments)
183{
Geoff Lang64a72442015-04-01 14:43:11 -0400184 // 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 Partin4d61f7e2015-08-12 10:56:50 -0700188 mFunctions->invalidateFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count), attachments);
Geoff Lang64a72442015-04-01 14:43:11 -0400189 }
190
Geoff Lang4ad17092015-03-10 16:47:44 -0400191 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500192}
193
194gl::Error FramebufferGL::invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area)
195{
Geoff Lang64a72442015-04-01 14:43:11 -0400196 // 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 Partin4d61f7e2015-08-12 10:56:50 -0700200 mFunctions->invalidateSubFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count),
201 attachments, area.x, area.y, area.width, area.height);
Geoff Lang64a72442015-04-01 14:43:11 -0400202 }
203
Geoff Lang4ad17092015-03-10 16:47:44 -0400204 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500205}
206
Jamie Madilld1f5ef22015-04-01 14:17:06 -0400207gl::Error FramebufferGL::clear(const gl::Data &data, GLbitfield mask)
Geoff Langf9a6f082015-01-22 13:32:49 -0500208{
Geoff Langafd7f0a2015-09-09 15:33:31 -0400209 syncClearState(mask);
Geoff Lang4ad17092015-03-10 16:47:44 -0400210 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
211 mFunctions->clear(mask);
212
213 return gl::Error(GL_NO_ERROR);
214}
215
Geoff Langf9a6f082015-01-22 13:32:49 -0500216gl::Error FramebufferGL::clearBufferfv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLfloat *values)
217{
Geoff Langafd7f0a2015-09-09 15:33:31 -0400218 syncClearBufferState(buffer, drawbuffer);
Geoff Lang4ad17092015-03-10 16:47:44 -0400219 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
220 mFunctions->clearBufferfv(buffer, drawbuffer, values);
221
222 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500223}
224
225gl::Error FramebufferGL::clearBufferuiv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLuint *values)
226{
Geoff Langafd7f0a2015-09-09 15:33:31 -0400227 syncClearBufferState(buffer, drawbuffer);
Geoff Lang4ad17092015-03-10 16:47:44 -0400228 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
229 mFunctions->clearBufferuiv(buffer, drawbuffer, values);
230
231 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500232}
233
234gl::Error FramebufferGL::clearBufferiv(const gl::State &state, GLenum buffer, GLint drawbuffer, const GLint *values)
235{
Geoff Langafd7f0a2015-09-09 15:33:31 -0400236 syncClearBufferState(buffer, drawbuffer);
Geoff Lang4ad17092015-03-10 16:47:44 -0400237 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
238 mFunctions->clearBufferiv(buffer, drawbuffer, values);
239
240 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500241}
242
243gl::Error FramebufferGL::clearBufferfi(const gl::State &state, GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil)
244{
Geoff Langafd7f0a2015-09-09 15:33:31 -0400245 syncClearBufferState(buffer, drawbuffer);
Geoff Lang4ad17092015-03-10 16:47:44 -0400246 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
247 mFunctions->clearBufferfi(buffer, drawbuffer, depth, stencil);
248
249 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500250}
251
252GLenum FramebufferGL::getImplementationColorReadFormat() const
253{
Geoff Lang4ad17092015-03-10 16:47:44 -0400254 const gl::FramebufferAttachment *readAttachment = getData().getReadAttachment();
255 GLenum internalFormat = readAttachment->getInternalFormat();
256 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
257 return internalFormatInfo.format;
Geoff Langf9a6f082015-01-22 13:32:49 -0500258}
259
260GLenum FramebufferGL::getImplementationColorReadType() const
261{
Geoff Lang4ad17092015-03-10 16:47:44 -0400262 const gl::FramebufferAttachment *readAttachment = getData().getReadAttachment();
263 GLenum internalFormat = readAttachment->getInternalFormat();
264 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
265 return internalFormatInfo.type;
Geoff Langf9a6f082015-01-22 13:32:49 -0500266}
267
268gl::Error FramebufferGL::readPixels(const gl::State &state, const gl::Rectangle &area, GLenum format, GLenum type, GLvoid *pixels) const
269{
Geoff Langda34d002015-09-04 11:08:59 -0400270 // TODO: don't sync the pixel pack state here once the dirty bits contain the pixel pack buffer
271 // binding
Jamie Madill87de3622015-03-16 10:41:44 -0400272 const gl::PixelPackState &packState = state.getPackState();
Geoff Langda34d002015-09-04 11:08:59 -0400273 mStateManager->setPixelPackState(packState);
Jamie Madill87de3622015-03-16 10:41:44 -0400274
Geoff Lang4ad17092015-03-10 16:47:44 -0400275 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 Langf9a6f082015-01-22 13:32:49 -0500279}
280
281gl::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 Lang4ad17092015-03-10 16:47:44 -0400284 const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer);
285
286 mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID());
287 mStateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID);
288
Jamie Madill2da819e2015-12-03 15:53:19 -0500289 mFunctions->blitFramebuffer(sourceArea.x, sourceArea.y, sourceArea.x1(), sourceArea.y1(),
290 destArea.x, destArea.y, destArea.x1(), destArea.y1(), mask, filter);
Geoff Lang4ad17092015-03-10 16:47:44 -0400291
292 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500293}
294
Jamie Madillcc86d642015-11-24 13:00:07 -0500295bool FramebufferGL::checkStatus() const
Geoff Langf9a6f082015-01-22 13:32:49 -0500296{
Geoff Lang4ad17092015-03-10 16:47:44 -0400297 mStateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madillcc86d642015-11-24 13:00:07 -0500298 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 Lang4ad17092015-03-10 16:47:44 -0400304}
305
306GLuint FramebufferGL::getFramebufferID() const
307{
308 return mFramebufferID;
Geoff Langf9a6f082015-01-22 13:32:49 -0500309}
310
Geoff Langafd7f0a2015-09-09 15:33:31 -0400311void 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
324void 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
347void 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 Langf9a6f082015-01-22 13:32:49 -0500379}