blob: c0ff1ea7af00d8e75ce8f99a866dd0273a30b0c9 [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
Jamie Madill20e005b2017-04-07 14:19:22 -040011#include "common/bitset_utils.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050012#include "common/debug.h"
Jamie Madillc564c072017-06-01 12:45:42 -040013#include "libANGLE/Context.h"
Geoff Lang4ad17092015-03-10 16:47:44 -040014#include "libANGLE/FramebufferAttachment.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040015#include "libANGLE/State.h"
Geoff Lang4ad17092015-03-10 16:47:44 -040016#include "libANGLE/angletypes.h"
17#include "libANGLE/formatutils.h"
Geoff Lang92019432017-11-20 13:09:34 -050018#include "libANGLE/queryconversions.h"
Jamie Madill8415b5f2016-04-26 13:41:39 -040019#include "libANGLE/renderer/ContextImpl.h"
Corentin Wallez26a717b2016-09-27 08:45:42 -070020#include "libANGLE/renderer/gl/BlitGL.h"
Martin Radev5e424fa2017-08-09 16:25:36 +030021#include "libANGLE/renderer/gl/ClearMultiviewGL.h"
Frank Henigmanfa36c332017-06-09 18:44:45 -040022#include "libANGLE/renderer/gl/ContextGL.h"
Geoff Lang4ad17092015-03-10 16:47:44 -040023#include "libANGLE/renderer/gl/FunctionsGL.h"
Jacek Cabanfa60f692015-04-27 18:23:44 +020024#include "libANGLE/renderer/gl/RenderbufferGL.h"
Geoff Lang4ad17092015-03-10 16:47:44 -040025#include "libANGLE/renderer/gl/StateManagerGL.h"
26#include "libANGLE/renderer/gl/TextureGL.h"
Geoff Langafd7f0a2015-09-09 15:33:31 -040027#include "libANGLE/renderer/gl/WorkaroundsGL.h"
Geoff Langf607c602016-09-21 11:46:48 -040028#include "libANGLE/renderer/gl/formatutilsgl.h"
Corentin Wallez886de362016-09-27 10:49:35 -040029#include "libANGLE/renderer/gl/renderergl_utils.h"
Jamie Madillcc86d642015-11-24 13:00:07 -050030#include "platform/Platform.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050031
Jamie Madill60ec6ea2016-01-22 15:27:19 -050032using namespace gl;
Corentin Wallez9a8d3662016-09-22 12:18:29 -040033using angle::CheckedNumeric;
Jamie Madill60ec6ea2016-01-22 15:27:19 -050034
Geoff Langf9a6f082015-01-22 13:32:49 -050035namespace rx
36{
37
Martin Radev878c8b12017-07-28 09:51:04 +030038namespace
Geoff Lang4ad17092015-03-10 16:47:44 -040039{
Geoff Langf9a6f082015-01-22 13:32:49 -050040
Martin Radev878c8b12017-07-28 09:51:04 +030041void BindFramebufferAttachment(const FunctionsGL *functions,
42 GLenum attachmentPoint,
43 const FramebufferAttachment *attachment)
Geoff Lang4ad17092015-03-10 16:47:44 -040044{
45 if (attachment)
46 {
47 if (attachment->type() == GL_TEXTURE)
48 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -050049 const Texture *texture = attachment->getTexture();
Geoff Lang4ad17092015-03-10 16:47:44 -040050 const TextureGL *textureGL = GetImplAs<TextureGL>(texture);
51
Corentin Wallez99d492c2018-02-27 15:17:10 -050052 if (texture->getType() == TextureType::_2D ||
53 texture->getType() == TextureType::_2DMultisample ||
54 texture->getType() == TextureType::Rectangle)
Geoff Lang4ad17092015-03-10 16:47:44 -040055 {
JiangYizhoubddc46b2016-12-09 09:50:51 +080056 functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint,
Corentin Wallez99d492c2018-02-27 15:17:10 -050057 ToGLenum(texture->getType()),
Geoff Lang4ad17092015-03-10 16:47:44 -040058 textureGL->getTextureID(), attachment->mipLevel());
59 }
Jiawei Shaoa8802472018-05-28 11:17:47 +080060 else if (attachment->isLayered())
61 {
62 TextureType textureType = texture->getType();
63 ASSERT(textureType == TextureType::_2DArray || textureType == TextureType::_3D ||
Olli Etuahodff32a02018-08-28 14:35:50 +030064 textureType == TextureType::CubeMap ||
65 textureType == TextureType::_2DMultisampleArray);
Jiawei Shaoa8802472018-05-28 11:17:47 +080066 functions->framebufferTexture(GL_FRAMEBUFFER, attachmentPoint,
67 textureGL->getTextureID(), attachment->mipLevel());
68 }
Corentin Wallez99d492c2018-02-27 15:17:10 -050069 else if (texture->getType() == TextureType::CubeMap)
70 {
71 functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint,
72 ToGLenum(attachment->cubeMapFace()),
73 textureGL->getTextureID(), attachment->mipLevel());
74 }
75 else if (texture->getType() == TextureType::_2DArray ||
Olli Etuahofd162102018-08-27 16:14:57 +030076 texture->getType() == TextureType::_3D ||
77 texture->getType() == TextureType::_2DMultisampleArray)
Geoff Lang4ad17092015-03-10 16:47:44 -040078 {
Martin Radev5e424fa2017-08-09 16:25:36 +030079 if (attachment->getMultiviewLayout() == GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE)
80 {
81 ASSERT(functions->framebufferTexture);
82 functions->framebufferTexture(GL_FRAMEBUFFER, attachmentPoint,
83 textureGL->getTextureID(),
84 attachment->mipLevel());
85 }
86 else
87 {
88 functions->framebufferTextureLayer(GL_FRAMEBUFFER, attachmentPoint,
89 textureGL->getTextureID(),
90 attachment->mipLevel(), attachment->layer());
91 }
Geoff Lang4ad17092015-03-10 16:47:44 -040092 }
93 else
94 {
95 UNREACHABLE();
96 }
97 }
98 else if (attachment->type() == GL_RENDERBUFFER)
99 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500100 const Renderbuffer *renderbuffer = attachment->getRenderbuffer();
Geoff Langcd69f1c2015-03-18 14:33:23 -0400101 const RenderbufferGL *renderbufferGL = GetImplAs<RenderbufferGL>(renderbuffer);
Geoff Lang4ad17092015-03-10 16:47:44 -0400102
Geoff Langcd69f1c2015-03-18 14:33:23 -0400103 functions->framebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER,
104 renderbufferGL->getRenderbufferID());
Geoff Lang4ad17092015-03-10 16:47:44 -0400105 }
106 else
107 {
108 UNREACHABLE();
109 }
110 }
111 else
112 {
113 // Unbind this attachment
114 functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, 0, 0);
115 }
116}
Geoff Langf9a6f082015-01-22 13:32:49 -0500117
Martin Radev61e710b2017-09-05 11:59:52 +0300118bool AreAllLayersActive(const FramebufferAttachment &attachment)
Martin Radevb0761932017-07-25 17:42:25 +0300119{
Martin Radev61e710b2017-09-05 11:59:52 +0300120 int baseViewIndex = attachment.getBaseViewIndex();
121 if (baseViewIndex != 0)
122 {
123 return false;
124 }
125 const ImageIndex &imageIndex = attachment.getTextureImageIndex();
Jamie Madillcc129372018-04-12 09:13:18 -0400126 int numLayers = static_cast<int>(
127 attachment.getTexture()->getDepth(imageIndex.getTarget(), imageIndex.getLevelIndex()));
Martin Radev61e710b2017-09-05 11:59:52 +0300128 return (attachment.getNumViews() == numLayers);
129}
130
131bool RequiresMultiviewClear(const FramebufferState &state, bool scissorTestEnabled)
132{
133 // Get one attachment and check whether all layers are attached.
134 const FramebufferAttachment *attachment = nullptr;
135 bool allTextureArraysAreFullyAttached = true;
136 for (const FramebufferAttachment &colorAttachment : state.getColorAttachments())
137 {
138 if (colorAttachment.isAttached())
139 {
140 if (colorAttachment.getMultiviewLayout() == GL_NONE)
141 {
142 return false;
143 }
144 attachment = &colorAttachment;
145 allTextureArraysAreFullyAttached =
146 allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment);
147 }
148 }
149
150 const FramebufferAttachment *depthAttachment = state.getDepthAttachment();
151 if (depthAttachment)
152 {
153 if (depthAttachment->getMultiviewLayout() == GL_NONE)
154 {
155 return false;
156 }
157 attachment = depthAttachment;
158 allTextureArraysAreFullyAttached =
159 allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment);
160 }
161 const FramebufferAttachment *stencilAttachment = state.getStencilAttachment();
162 if (stencilAttachment)
163 {
164 if (stencilAttachment->getMultiviewLayout() == GL_NONE)
165 {
166 return false;
167 }
168 attachment = stencilAttachment;
169 allTextureArraysAreFullyAttached =
170 allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment);
171 }
172
Martin Radev5e424fa2017-08-09 16:25:36 +0300173 if (attachment == nullptr)
174 {
175 return false;
176 }
177 switch (attachment->getMultiviewLayout())
178 {
179 case GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE:
Martin Radev61e710b2017-09-05 11:59:52 +0300180 // If all layers of each texture array are active, then there is no need to issue a
181 // special multiview clear.
182 return !allTextureArraysAreFullyAttached;
Martin Radev5e424fa2017-08-09 16:25:36 +0300183 case GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE:
184 return (scissorTestEnabled == true);
Martin Radev5e424fa2017-08-09 16:25:36 +0300185 default:
186 UNREACHABLE();
187 }
188 return false;
Martin Radevb0761932017-07-25 17:42:25 +0300189}
190
Martin Radev878c8b12017-07-28 09:51:04 +0300191} // namespace
192
Geoff Lang61107632018-05-09 11:32:46 -0400193FramebufferGL::FramebufferGL(const gl::FramebufferState &data, GLuint id, bool isDefault)
194 : FramebufferImpl(data),
Martin Radev878c8b12017-07-28 09:51:04 +0300195 mFramebufferID(id),
Geoff Lang61107632018-05-09 11:32:46 -0400196 mIsDefault(isDefault),
Martin Radev878c8b12017-07-28 09:51:04 +0300197 mAppliedEnabledDrawBuffers(1)
198{
199}
200
201FramebufferGL::~FramebufferGL()
202{
Geoff Lang61107632018-05-09 11:32:46 -0400203 ASSERT(mFramebufferID == 0);
204}
205
206void FramebufferGL::destroy(const gl::Context *context)
207{
208 StateManagerGL *stateManager = GetStateManagerGL(context);
209 stateManager->deleteFramebuffer(mFramebufferID);
Martin Radev878c8b12017-07-28 09:51:04 +0300210 mFramebufferID = 0;
211}
212
Jamie Madill4928b7c2017-06-20 12:57:39 -0400213Error FramebufferGL::discard(const gl::Context *context, size_t count, const GLenum *attachments)
Austin Kinross08332632015-05-05 13:35:47 -0700214{
Geoff Lang57ce9ea2016-11-24 12:03:14 -0500215 // glInvalidateFramebuffer accepts the same enums as glDiscardFramebufferEXT
Jamie Madill4928b7c2017-06-20 12:57:39 -0400216 return invalidate(context, count, attachments);
Austin Kinross08332632015-05-05 13:35:47 -0700217}
218
Jamie Madill4928b7c2017-06-20 12:57:39 -0400219Error FramebufferGL::invalidate(const gl::Context *context, size_t count, const GLenum *attachments)
Geoff Langf9a6f082015-01-22 13:32:49 -0500220{
Geoff Lang005a7012017-03-27 13:17:34 -0400221 const GLenum *finalAttachmentsPtr = attachments;
222
223 std::vector<GLenum> modifiedAttachments;
224 if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments))
225 {
226 finalAttachmentsPtr = modifiedAttachments.data();
227 }
228
Geoff Lang61107632018-05-09 11:32:46 -0400229 const FunctionsGL *functions = GetFunctionsGL(context);
230 StateManagerGL *stateManager = GetStateManagerGL(context);
231
Geoff Lang57ce9ea2016-11-24 12:03:14 -0500232 // Since this function is just a hint, only call a native function if it exists.
Geoff Lang61107632018-05-09 11:32:46 -0400233 if (functions->invalidateFramebuffer)
Geoff Lang64a72442015-04-01 14:43:11 -0400234 {
Geoff Lang61107632018-05-09 11:32:46 -0400235 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
236 functions->invalidateFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count),
237 finalAttachmentsPtr);
Geoff Lang64a72442015-04-01 14:43:11 -0400238 }
Geoff Lang61107632018-05-09 11:32:46 -0400239 else if (functions->discardFramebufferEXT)
Geoff Lang57ce9ea2016-11-24 12:03:14 -0500240 {
Geoff Lang61107632018-05-09 11:32:46 -0400241 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
242 functions->discardFramebufferEXT(GL_FRAMEBUFFER, static_cast<GLsizei>(count),
243 finalAttachmentsPtr);
Geoff Lang57ce9ea2016-11-24 12:03:14 -0500244 }
Geoff Lang64a72442015-04-01 14:43:11 -0400245
Geoff Lang57ce9ea2016-11-24 12:03:14 -0500246 return gl::NoError();
Geoff Langf9a6f082015-01-22 13:32:49 -0500247}
248
Jamie Madill4928b7c2017-06-20 12:57:39 -0400249Error FramebufferGL::invalidateSub(const gl::Context *context,
250 size_t count,
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500251 const GLenum *attachments,
252 const gl::Rectangle &area)
Geoff Langf9a6f082015-01-22 13:32:49 -0500253{
Geoff Lang005a7012017-03-27 13:17:34 -0400254
255 const GLenum *finalAttachmentsPtr = attachments;
256
257 std::vector<GLenum> modifiedAttachments;
258 if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments))
259 {
260 finalAttachmentsPtr = modifiedAttachments.data();
261 }
262
Geoff Lang61107632018-05-09 11:32:46 -0400263 const FunctionsGL *functions = GetFunctionsGL(context);
264 StateManagerGL *stateManager = GetStateManagerGL(context);
265
Jamie Madill231c7f52017-04-26 13:45:37 -0400266 // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is
267 // available.
Geoff Lang61107632018-05-09 11:32:46 -0400268 if (functions->invalidateSubFramebuffer)
Geoff Lang64a72442015-04-01 14:43:11 -0400269 {
Geoff Lang61107632018-05-09 11:32:46 -0400270 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
271 functions->invalidateSubFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count),
272 finalAttachmentsPtr, area.x, area.y, area.width,
273 area.height);
Geoff Lang64a72442015-04-01 14:43:11 -0400274 }
275
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500276 return gl::NoError();
Geoff Langf9a6f082015-01-22 13:32:49 -0500277}
278
Jamie Madillc564c072017-06-01 12:45:42 -0400279Error FramebufferGL::clear(const gl::Context *context, GLbitfield mask)
Geoff Langf9a6f082015-01-22 13:32:49 -0500280{
Geoff Lang61107632018-05-09 11:32:46 -0400281 const FunctionsGL *functions = GetFunctionsGL(context);
282 StateManagerGL *stateManager = GetStateManagerGL(context);
283
Frank Henigman308d7452017-02-15 22:51:21 -0500284 syncClearState(context, mask);
Geoff Lang61107632018-05-09 11:32:46 -0400285 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Martin Radevb0761932017-07-25 17:42:25 +0300286
Martin Radev61e710b2017-09-05 11:59:52 +0300287 if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled()))
Martin Radevb0761932017-07-25 17:42:25 +0300288 {
Geoff Lang61107632018-05-09 11:32:46 -0400289 functions->clear(mask);
Martin Radevb0761932017-07-25 17:42:25 +0300290 }
291 else
292 {
Geoff Lang61107632018-05-09 11:32:46 -0400293 ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
294 multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(),
295 ClearMultiviewGL::ClearCommandType::Clear, mask,
296 GL_NONE, 0, nullptr, 0.0f, 0);
Martin Radevb0761932017-07-25 17:42:25 +0300297 }
Geoff Lang4ad17092015-03-10 16:47:44 -0400298
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500299 return gl::NoError();
Geoff Lang4ad17092015-03-10 16:47:44 -0400300}
301
Jamie Madillc564c072017-06-01 12:45:42 -0400302Error FramebufferGL::clearBufferfv(const gl::Context *context,
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500303 GLenum buffer,
304 GLint drawbuffer,
305 const GLfloat *values)
Geoff Langf9a6f082015-01-22 13:32:49 -0500306{
Geoff Lang61107632018-05-09 11:32:46 -0400307 const FunctionsGL *functions = GetFunctionsGL(context);
308 StateManagerGL *stateManager = GetStateManagerGL(context);
309
Frank Henigman308d7452017-02-15 22:51:21 -0500310 syncClearBufferState(context, buffer, drawbuffer);
Geoff Lang61107632018-05-09 11:32:46 -0400311 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Martin Radevb0761932017-07-25 17:42:25 +0300312
Martin Radev61e710b2017-09-05 11:59:52 +0300313 if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled()))
Martin Radevb0761932017-07-25 17:42:25 +0300314 {
Geoff Lang61107632018-05-09 11:32:46 -0400315 functions->clearBufferfv(buffer, drawbuffer, values);
Martin Radevb0761932017-07-25 17:42:25 +0300316 }
317 else
318 {
Geoff Lang61107632018-05-09 11:32:46 -0400319 ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
320 multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(),
321 ClearMultiviewGL::ClearCommandType::ClearBufferfv,
322 static_cast<GLbitfield>(0u), buffer, drawbuffer,
323 reinterpret_cast<const uint8_t *>(values), 0.0f, 0);
Martin Radevb0761932017-07-25 17:42:25 +0300324 }
Geoff Lang4ad17092015-03-10 16:47:44 -0400325
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500326 return gl::NoError();
Geoff Langf9a6f082015-01-22 13:32:49 -0500327}
328
Jamie Madillc564c072017-06-01 12:45:42 -0400329Error FramebufferGL::clearBufferuiv(const gl::Context *context,
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500330 GLenum buffer,
331 GLint drawbuffer,
332 const GLuint *values)
Geoff Langf9a6f082015-01-22 13:32:49 -0500333{
Geoff Lang61107632018-05-09 11:32:46 -0400334 const FunctionsGL *functions = GetFunctionsGL(context);
335 StateManagerGL *stateManager = GetStateManagerGL(context);
336
Frank Henigman308d7452017-02-15 22:51:21 -0500337 syncClearBufferState(context, buffer, drawbuffer);
Geoff Lang61107632018-05-09 11:32:46 -0400338 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Martin Radevb0761932017-07-25 17:42:25 +0300339
Martin Radev61e710b2017-09-05 11:59:52 +0300340 if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled()))
Martin Radevb0761932017-07-25 17:42:25 +0300341 {
Geoff Lang61107632018-05-09 11:32:46 -0400342 functions->clearBufferuiv(buffer, drawbuffer, values);
Martin Radevb0761932017-07-25 17:42:25 +0300343 }
344 else
345 {
Geoff Lang61107632018-05-09 11:32:46 -0400346 ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
347 multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(),
348 ClearMultiviewGL::ClearCommandType::ClearBufferuiv,
349 static_cast<GLbitfield>(0u), buffer, drawbuffer,
350 reinterpret_cast<const uint8_t *>(values), 0.0f, 0);
Martin Radevb0761932017-07-25 17:42:25 +0300351 }
Geoff Lang4ad17092015-03-10 16:47:44 -0400352
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500353 return gl::NoError();
Geoff Langf9a6f082015-01-22 13:32:49 -0500354}
355
Jamie Madillc564c072017-06-01 12:45:42 -0400356Error FramebufferGL::clearBufferiv(const gl::Context *context,
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500357 GLenum buffer,
358 GLint drawbuffer,
359 const GLint *values)
Geoff Langf9a6f082015-01-22 13:32:49 -0500360{
Geoff Lang61107632018-05-09 11:32:46 -0400361 const FunctionsGL *functions = GetFunctionsGL(context);
362 StateManagerGL *stateManager = GetStateManagerGL(context);
363
Frank Henigman308d7452017-02-15 22:51:21 -0500364 syncClearBufferState(context, buffer, drawbuffer);
Geoff Lang61107632018-05-09 11:32:46 -0400365 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Martin Radevb0761932017-07-25 17:42:25 +0300366
Martin Radev61e710b2017-09-05 11:59:52 +0300367 if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled()))
Martin Radevb0761932017-07-25 17:42:25 +0300368 {
Geoff Lang61107632018-05-09 11:32:46 -0400369 functions->clearBufferiv(buffer, drawbuffer, values);
Martin Radevb0761932017-07-25 17:42:25 +0300370 }
371 else
372 {
Geoff Lang61107632018-05-09 11:32:46 -0400373 ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
374 multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(),
375 ClearMultiviewGL::ClearCommandType::ClearBufferiv,
376 static_cast<GLbitfield>(0u), buffer, drawbuffer,
377 reinterpret_cast<const uint8_t *>(values), 0.0f, 0);
Martin Radevb0761932017-07-25 17:42:25 +0300378 }
Geoff Lang4ad17092015-03-10 16:47:44 -0400379
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500380 return gl::NoError();
Geoff Langf9a6f082015-01-22 13:32:49 -0500381}
382
Jamie Madillc564c072017-06-01 12:45:42 -0400383Error FramebufferGL::clearBufferfi(const gl::Context *context,
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500384 GLenum buffer,
385 GLint drawbuffer,
386 GLfloat depth,
387 GLint stencil)
Geoff Langf9a6f082015-01-22 13:32:49 -0500388{
Geoff Lang61107632018-05-09 11:32:46 -0400389 const FunctionsGL *functions = GetFunctionsGL(context);
390 StateManagerGL *stateManager = GetStateManagerGL(context);
391
Frank Henigman308d7452017-02-15 22:51:21 -0500392 syncClearBufferState(context, buffer, drawbuffer);
Geoff Lang61107632018-05-09 11:32:46 -0400393 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Martin Radevb0761932017-07-25 17:42:25 +0300394
Martin Radev61e710b2017-09-05 11:59:52 +0300395 if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled()))
Martin Radevb0761932017-07-25 17:42:25 +0300396 {
Geoff Lang61107632018-05-09 11:32:46 -0400397 functions->clearBufferfi(buffer, drawbuffer, depth, stencil);
Martin Radevb0761932017-07-25 17:42:25 +0300398 }
Martin Radev5e424fa2017-08-09 16:25:36 +0300399 else
400 {
Geoff Lang61107632018-05-09 11:32:46 -0400401 ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
402 multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(),
403 ClearMultiviewGL::ClearCommandType::ClearBufferfi,
404 static_cast<GLbitfield>(0u), buffer, drawbuffer,
405 nullptr, depth, stencil);
Martin Radev5e424fa2017-08-09 16:25:36 +0300406 }
Geoff Lang4ad17092015-03-10 16:47:44 -0400407
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500408 return gl::NoError();
Geoff Langf9a6f082015-01-22 13:32:49 -0500409}
410
Jamie Madill4928b7c2017-06-20 12:57:39 -0400411GLenum FramebufferGL::getImplementationColorReadFormat(const gl::Context *context) const
Geoff Langf9a6f082015-01-22 13:32:49 -0500412{
Jamie Madilla3944d42016-07-22 22:13:26 -0400413 const auto *readAttachment = mState.getReadAttachment();
414 const Format &format = readAttachment->getFormat();
Geoff Langf607c602016-09-21 11:46:48 -0400415 return format.info->getReadPixelsFormat();
Geoff Langf9a6f082015-01-22 13:32:49 -0500416}
417
Jamie Madill4928b7c2017-06-20 12:57:39 -0400418GLenum FramebufferGL::getImplementationColorReadType(const gl::Context *context) const
Geoff Langf9a6f082015-01-22 13:32:49 -0500419{
Jamie Madilla3944d42016-07-22 22:13:26 -0400420 const auto *readAttachment = mState.getReadAttachment();
421 const Format &format = readAttachment->getFormat();
Geoff Langc71ea662017-09-26 17:06:02 -0400422 return format.info->getReadPixelsType(context->getClientVersion());
Geoff Langf9a6f082015-01-22 13:32:49 -0500423}
424
Jamie Madillc564c072017-06-01 12:45:42 -0400425Error FramebufferGL::readPixels(const gl::Context *context,
Frank Henigmanfa36c332017-06-09 18:44:45 -0400426 const gl::Rectangle &origArea,
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500427 GLenum format,
428 GLenum type,
Jamie Madilld4826152017-09-21 11:18:59 -0400429 void *ptrOrOffset)
Geoff Langf9a6f082015-01-22 13:32:49 -0500430{
Jamie Madille39e8f42018-10-05 08:17:38 -0400431 ContextGL *contextGL = GetImplAs<ContextGL>(context);
Geoff Lang61107632018-05-09 11:32:46 -0400432 const FunctionsGL *functions = GetFunctionsGL(context);
433 StateManagerGL *stateManager = GetStateManagerGL(context);
434 const WorkaroundsGL &workarounds = GetWorkaroundsGL(context);
435
Frank Henigmanfa36c332017-06-09 18:44:45 -0400436 // Clip read area to framebuffer.
437 const gl::Extents fbSize = getState().getReadAttachment()->getSize();
438 const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height);
439 gl::Rectangle area;
440 if (!ClipRectangle(origArea, fbRect, &area))
441 {
442 // nothing to read
443 return gl::NoError();
444 }
445
Corentin Wallezcda6af12017-10-30 19:20:37 -0400446 PixelPackState packState = context->getGLState().getPackState();
Corentin Wallez336129f2017-10-17 15:55:40 -0400447 const gl::Buffer *packBuffer =
448 context->getGLState().getTargetBuffer(gl::BufferBinding::PixelPack);
Jamie Madill87de3622015-03-16 10:41:44 -0400449
Geoff Langf607c602016-09-21 11:46:48 -0400450 nativegl::ReadPixelsFormat readPixelsFormat =
Geoff Lang61107632018-05-09 11:32:46 -0400451 nativegl::GetReadPixelsFormat(functions, workarounds, format, type);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400452 GLenum readFormat = readPixelsFormat.format;
453 GLenum readType = readPixelsFormat.type;
Geoff Lang4ad17092015-03-10 16:47:44 -0400454
Geoff Lang61107632018-05-09 11:32:46 -0400455 stateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferID);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400456
Geoff Lang61107632018-05-09 11:32:46 -0400457 bool useOverlappingRowsWorkaround = workarounds.packOverlappingRowsSeparatelyPackBuffer &&
Corentin Wallezcda6af12017-10-30 19:20:37 -0400458 packBuffer && packState.rowLength != 0 &&
Frank Henigmanfa36c332017-06-09 18:44:45 -0400459 packState.rowLength < area.width;
460
Rafael Cintron05a449a2018-06-20 18:08:04 -0700461 GLubyte *pixels = static_cast<GLubyte *>(ptrOrOffset);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400462 int leftClip = area.x - origArea.x;
463 int topClip = area.y - origArea.y;
464 if (leftClip || topClip)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400465 {
Frank Henigmanfa36c332017-06-09 18:44:45 -0400466 // Adjust destination to match portion clipped off left and/or top.
467 const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(readFormat, readType);
468
469 GLuint rowBytes = 0;
Jamie Madille39e8f42018-10-05 08:17:38 -0400470 ANGLE_CHECK_GL_MATH(contextGL,
471 glFormat.computeRowPitch(readType, origArea.width, packState.alignment,
472 packState.rowLength, &rowBytes));
Frank Henigmanfa36c332017-06-09 18:44:45 -0400473 pixels += leftClip * glFormat.pixelBytes + topClip * rowBytes;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400474 }
475
Frank Henigmanfa36c332017-06-09 18:44:45 -0400476 if (packState.rowLength == 0 && area.width != origArea.width)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400477 {
Frank Henigmanfa36c332017-06-09 18:44:45 -0400478 // No rowLength was specified so it will derive from read width, but clipping changed the
479 // read width. Use the original width so we fill the user's buffer as they intended.
480 packState.rowLength = origArea.width;
481 }
Corentin Wallez886de362016-09-27 10:49:35 -0400482
Frank Henigmanfa36c332017-06-09 18:44:45 -0400483 // We want to use rowLength, but that might not be supported.
484 bool cannotSetDesiredRowLength =
485 packState.rowLength && !GetImplAs<ContextGL>(context)->getNativeExtensions().packSubimage;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400486
Frank Henigmanfa36c332017-06-09 18:44:45 -0400487 if (cannotSetDesiredRowLength || useOverlappingRowsWorkaround)
488 {
Jamie Madill13951342018-09-30 15:24:28 -0400489 return readPixelsRowByRow(context, area, readFormat, readType, packState, pixels);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400490 }
Jamie Madill13951342018-09-30 15:24:28 -0400491
492 bool useLastRowPaddingWorkaround = false;
493 if (workarounds.packLastRowSeparatelyForPaddingInclusion)
Frank Henigmanfa36c332017-06-09 18:44:45 -0400494 {
Jamie Madille39e8f42018-10-05 08:17:38 -0400495 ANGLE_TRY(ShouldApplyLastRowPaddingWorkaround(
496 contextGL, gl::Extents(area.width, area.height, 1), packState, packBuffer, readFormat,
497 readType, false, pixels, &useLastRowPaddingWorkaround));
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400498 }
499
Jamie Madill13951342018-09-30 15:24:28 -0400500 return readPixelsAllAtOnce(context, area, readFormat, readType, packState, pixels,
501 useLastRowPaddingWorkaround);
Geoff Langf9a6f082015-01-22 13:32:49 -0500502}
503
Jamie Madillc564c072017-06-01 12:45:42 -0400504Error FramebufferGL::blit(const gl::Context *context,
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500505 const gl::Rectangle &sourceArea,
506 const gl::Rectangle &destArea,
507 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -0400508 GLenum filter)
Geoff Langf9a6f082015-01-22 13:32:49 -0500509{
Geoff Lang61107632018-05-09 11:32:46 -0400510 const FunctionsGL *functions = GetFunctionsGL(context);
511 StateManagerGL *stateManager = GetStateManagerGL(context);
512
Jamie Madill231c7f52017-04-26 13:45:37 -0400513 const Framebuffer *sourceFramebuffer = context->getGLState().getReadFramebuffer();
514 const Framebuffer *destFramebuffer = context->getGLState().getDrawFramebuffer();
Geoff Lang4ad17092015-03-10 16:47:44 -0400515
Corentin Wallez6898b352016-11-10 11:41:15 -0500516 const FramebufferAttachment *colorReadAttachment = sourceFramebuffer->getReadColorbuffer();
Corentin Wallez4596a762016-12-20 14:50:18 -0500517
518 GLsizei readAttachmentSamples = 0;
519 if (colorReadAttachment != nullptr)
520 {
521 readAttachmentSamples = colorReadAttachment->getSamples();
522 }
Corentin Wallez6898b352016-11-10 11:41:15 -0500523
Corentin Wallez26a717b2016-09-27 08:45:42 -0700524 bool needManualColorBlit = false;
525
Corentin Wallez6898b352016-11-10 11:41:15 -0500526 // TODO(cwallez) when the filter is LINEAR and both source and destination are SRGB, we
527 // could avoid doing a manual blit.
528
529 // Prior to OpenGL 4.4 BlitFramebuffer (section 18.3.1 of GL 4.3 core profile) reads:
530 // When values are taken from the read buffer, no linearization is performed, even
531 // if the format of the buffer is SRGB.
532 // Starting from OpenGL 4.4 (section 18.3.1) it reads:
533 // When values are taken from the read buffer, if FRAMEBUFFER_SRGB is enabled and the
534 // value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the framebuffer attachment
535 // corresponding to the read buffer is SRGB, the red, green, and blue components are
536 // converted from the non-linear sRGB color space according [...].
Corentin Wallez26a717b2016-09-27 08:45:42 -0700537 {
Jamie Madill231c7f52017-04-26 13:45:37 -0400538 bool sourceSRGB =
539 colorReadAttachment != nullptr && colorReadAttachment->getColorEncoding() == GL_SRGB;
Corentin Wallez6898b352016-11-10 11:41:15 -0500540 needManualColorBlit =
Geoff Lang61107632018-05-09 11:32:46 -0400541 needManualColorBlit || (sourceSRGB && functions->isAtMostGL(gl::Version(4, 3)));
Corentin Wallez6898b352016-11-10 11:41:15 -0500542 }
Corentin Wallez26a717b2016-09-27 08:45:42 -0700543
Corentin Wallez6898b352016-11-10 11:41:15 -0500544 // Prior to OpenGL 4.2 BlitFramebuffer (section 4.3.2 of GL 4.1 core profile) reads:
545 // Blit operations bypass the fragment pipeline. The only fragment operations which
546 // affect a blit are the pixel ownership test and scissor test.
547 // Starting from OpenGL 4.2 (section 4.3.2) it reads:
548 // When values are written to the draw buffers, blit operations bypass the fragment
549 // pipeline. The only fragment operations which affect a blit are the pixel ownership
550 // test, the scissor test and sRGB conversion.
551 if (!needManualColorBlit)
552 {
553 bool destSRGB = false;
554 for (size_t i = 0; i < destFramebuffer->getDrawbufferStateCount(); ++i)
Corentin Wallez26a717b2016-09-27 08:45:42 -0700555 {
Corentin Wallez6898b352016-11-10 11:41:15 -0500556 const FramebufferAttachment *attachment = destFramebuffer->getDrawBuffer(i);
557 if (attachment && attachment->getColorEncoding() == GL_SRGB)
Corentin Wallez26a717b2016-09-27 08:45:42 -0700558 {
Corentin Wallez6898b352016-11-10 11:41:15 -0500559 destSRGB = true;
560 break;
Corentin Wallez26a717b2016-09-27 08:45:42 -0700561 }
Corentin Wallez26a717b2016-09-27 08:45:42 -0700562 }
Corentin Wallez6898b352016-11-10 11:41:15 -0500563
564 needManualColorBlit =
Geoff Lang61107632018-05-09 11:32:46 -0400565 needManualColorBlit || (destSRGB && functions->isAtMostGL(gl::Version(4, 1)));
Corentin Wallez26a717b2016-09-27 08:45:42 -0700566 }
567
568 // Enable FRAMEBUFFER_SRGB if needed
Geoff Lang61107632018-05-09 11:32:46 -0400569 stateManager->setFramebufferSRGBEnabledForFramebuffer(context, true, this);
Corentin Wallez26a717b2016-09-27 08:45:42 -0700570
571 GLenum blitMask = mask;
Corentin Wallez6898b352016-11-10 11:41:15 -0500572 if (needManualColorBlit && (mask & GL_COLOR_BUFFER_BIT) && readAttachmentSamples <= 1)
Corentin Wallez26a717b2016-09-27 08:45:42 -0700573 {
Geoff Lang61107632018-05-09 11:32:46 -0400574 BlitGL *blitter = GetBlitGL(context);
Jamie Madille39e8f42018-10-05 08:17:38 -0400575 ANGLE_TRY(blitter->blitColorBufferWithShader(context, sourceFramebuffer, destFramebuffer,
576 sourceArea, destArea, filter));
Corentin Wallez26a717b2016-09-27 08:45:42 -0700577 blitMask &= ~GL_COLOR_BUFFER_BIT;
578 }
579
580 if (blitMask == 0)
581 {
582 return gl::NoError();
583 }
584
585 const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer);
Geoff Lang61107632018-05-09 11:32:46 -0400586 stateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID());
587 stateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID);
Geoff Lang4ad17092015-03-10 16:47:44 -0400588
Geoff Lang61107632018-05-09 11:32:46 -0400589 functions->blitFramebuffer(sourceArea.x, sourceArea.y, sourceArea.x1(), sourceArea.y1(),
590 destArea.x, destArea.y, destArea.x1(), destArea.y1(), blitMask,
591 filter);
Geoff Lang4ad17092015-03-10 16:47:44 -0400592
Corentin Wallez26a717b2016-09-27 08:45:42 -0700593 return gl::NoError();
Geoff Langf9a6f082015-01-22 13:32:49 -0500594}
595
Geoff Lang13455072018-05-09 11:24:43 -0400596gl::Error FramebufferGL::getSamplePosition(const gl::Context *context,
597 size_t index,
598 GLfloat *xy) const
JiangYizhoubddc46b2016-12-09 09:50:51 +0800599{
Geoff Lang61107632018-05-09 11:32:46 -0400600 const FunctionsGL *functions = GetFunctionsGL(context);
601 StateManagerGL *stateManager = GetStateManagerGL(context);
602
603 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
604 functions->getMultisamplefv(GL_SAMPLE_POSITION, static_cast<GLuint>(index), xy);
JiangYizhoubddc46b2016-12-09 09:50:51 +0800605 return gl::NoError();
606}
607
Kenneth Russellce8602a2017-10-03 18:23:08 -0700608bool FramebufferGL::checkStatus(const gl::Context *context) const
Geoff Langf9a6f082015-01-22 13:32:49 -0500609{
Geoff Lang61107632018-05-09 11:32:46 -0400610 const FunctionsGL *functions = GetFunctionsGL(context);
611 StateManagerGL *stateManager = GetStateManagerGL(context);
612
613 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
614 GLenum status = functions->checkFramebufferStatus(GL_FRAMEBUFFER);
Jamie Madillcc86d642015-11-24 13:00:07 -0500615 if (status != GL_FRAMEBUFFER_COMPLETE)
616 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500617 WARN() << "GL framebuffer returned incomplete.";
Jamie Madillcc86d642015-11-24 13:00:07 -0500618 }
619 return (status == GL_FRAMEBUFFER_COMPLETE);
Geoff Lang4ad17092015-03-10 16:47:44 -0400620}
621
Jamie Madill19fa1c62018-03-08 09:47:21 -0500622gl::Error FramebufferGL::syncState(const gl::Context *context,
623 const Framebuffer::DirtyBits &dirtyBits)
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500624{
625 // Don't need to sync state for the default FBO.
626 if (mIsDefault)
627 {
Jamie Madill19fa1c62018-03-08 09:47:21 -0500628 return gl::NoError();
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500629 }
630
Geoff Lang61107632018-05-09 11:32:46 -0400631 const FunctionsGL *functions = GetFunctionsGL(context);
632 StateManagerGL *stateManager = GetStateManagerGL(context);
633
634 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500635
Martin Radev7c2e6a92017-08-28 11:13:16 +0300636 // A pointer to one of the attachments for which the texture or the render buffer is not zero.
637 const FramebufferAttachment *attachment = nullptr;
Martin Radev878c8b12017-07-28 09:51:04 +0300638
Jamie Madill6de51852017-04-12 09:53:01 -0400639 for (auto dirtyBit : dirtyBits)
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500640 {
641 switch (dirtyBit)
642 {
643 case Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT:
Martin Radev7c2e6a92017-08-28 11:13:16 +0300644 {
645 const FramebufferAttachment *newAttachment = mState.getDepthAttachment();
Geoff Lang61107632018-05-09 11:32:46 -0400646 BindFramebufferAttachment(functions, GL_DEPTH_ATTACHMENT, newAttachment);
Martin Radev7c2e6a92017-08-28 11:13:16 +0300647 if (newAttachment)
648 {
649 attachment = newAttachment;
650 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500651 break;
Martin Radev7c2e6a92017-08-28 11:13:16 +0300652 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500653 case Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT:
Martin Radev7c2e6a92017-08-28 11:13:16 +0300654 {
655 const FramebufferAttachment *newAttachment = mState.getStencilAttachment();
Geoff Lang61107632018-05-09 11:32:46 -0400656 BindFramebufferAttachment(functions, GL_STENCIL_ATTACHMENT, newAttachment);
Martin Radev7c2e6a92017-08-28 11:13:16 +0300657 if (newAttachment)
658 {
659 attachment = newAttachment;
660 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500661 break;
Martin Radev7c2e6a92017-08-28 11:13:16 +0300662 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500663 case Framebuffer::DIRTY_BIT_DRAW_BUFFERS:
664 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400665 const auto &drawBuffers = mState.getDrawBufferStates();
Geoff Lang61107632018-05-09 11:32:46 -0400666 functions->drawBuffers(static_cast<GLsizei>(drawBuffers.size()),
667 drawBuffers.data());
Corentin Walleze7557742017-06-01 13:09:57 -0400668 mAppliedEnabledDrawBuffers = mState.getEnabledDrawBuffers();
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500669 break;
670 }
671 case Framebuffer::DIRTY_BIT_READ_BUFFER:
Geoff Lang61107632018-05-09 11:32:46 -0400672 functions->readBuffer(mState.getReadBufferState());
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500673 break;
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800674 case Framebuffer::DIRTY_BIT_DEFAULT_WIDTH:
Geoff Lang61107632018-05-09 11:32:46 -0400675 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_WIDTH,
676 mState.getDefaultWidth());
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800677 break;
678 case Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT:
Geoff Lang61107632018-05-09 11:32:46 -0400679 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_HEIGHT,
680 mState.getDefaultHeight());
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800681 break;
682 case Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES:
Geoff Lang61107632018-05-09 11:32:46 -0400683 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES,
684 mState.getDefaultSamples());
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800685 break;
686 case Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS:
Geoff Lang61107632018-05-09 11:32:46 -0400687 functions->framebufferParameteri(
Geoff Lang92019432017-11-20 13:09:34 -0500688 GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS,
689 gl::ConvertToGLBoolean(mState.getDefaultFixedSampleLocations()));
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800690 break;
Jiawei Shaob1e91382018-05-17 14:33:55 +0800691 case Framebuffer::DIRTY_BIT_DEFAULT_LAYERS:
692 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_LAYERS_EXT,
693 mState.getDefaultLayers());
694 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500695 default:
696 {
697 ASSERT(Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0 &&
698 dirtyBit < Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX);
699 size_t index =
700 static_cast<size_t>(dirtyBit - Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0);
Martin Radev7c2e6a92017-08-28 11:13:16 +0300701 const FramebufferAttachment *newAttachment = mState.getColorAttachment(index);
702 BindFramebufferAttachment(
Geoff Lang61107632018-05-09 11:32:46 -0400703 functions, static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + index), newAttachment);
Martin Radev7c2e6a92017-08-28 11:13:16 +0300704 if (newAttachment)
705 {
706 attachment = newAttachment;
707 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500708 break;
709 }
710 }
711 }
Martin Radev878c8b12017-07-28 09:51:04 +0300712
Martin Radev7c2e6a92017-08-28 11:13:16 +0300713 if (attachment)
Martin Radev878c8b12017-07-28 09:51:04 +0300714 {
Martin Radev7c2e6a92017-08-28 11:13:16 +0300715 const bool isSideBySide =
716 (attachment->getMultiviewLayout() == GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE);
Geoff Lang61107632018-05-09 11:32:46 -0400717 stateManager->setSideBySide(isSideBySide);
718 stateManager->setViewportOffsets(attachment->getMultiviewViewportOffsets());
719 stateManager->updateMultiviewBaseViewLayerIndexUniform(context->getGLState().getProgram(),
720 getState());
Martin Radev878c8b12017-07-28 09:51:04 +0300721 }
Jamie Madill19fa1c62018-03-08 09:47:21 -0500722
723 return gl::NoError();
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500724}
725
Geoff Lang4ad17092015-03-10 16:47:44 -0400726GLuint FramebufferGL::getFramebufferID() const
727{
728 return mFramebufferID;
Geoff Langf9a6f082015-01-22 13:32:49 -0500729}
730
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700731bool FramebufferGL::isDefault() const
Geoff Langafd7f0a2015-09-09 15:33:31 -0400732{
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700733 return mIsDefault;
Geoff Langafd7f0a2015-09-09 15:33:31 -0400734}
735
Geoff Lang61107632018-05-09 11:32:46 -0400736void FramebufferGL::maskOutInactiveOutputDrawBuffers(const gl::Context *context,
737 GLenum binding,
738 DrawBufferMask maxSet)
Corentin Walleze7557742017-06-01 13:09:57 -0400739{
Geoff Lang61107632018-05-09 11:32:46 -0400740
Corentin Walleze7557742017-06-01 13:09:57 -0400741 auto targetAppliedDrawBuffers = mState.getEnabledDrawBuffers() & maxSet;
742 if (mAppliedEnabledDrawBuffers != targetAppliedDrawBuffers)
743 {
744 mAppliedEnabledDrawBuffers = targetAppliedDrawBuffers;
745
746 const auto &stateDrawBuffers = mState.getDrawBufferStates();
747 GLsizei drawBufferCount = static_cast<GLsizei>(stateDrawBuffers.size());
748 ASSERT(drawBufferCount <= IMPLEMENTATION_MAX_DRAW_BUFFERS);
749
750 GLenum drawBuffers[IMPLEMENTATION_MAX_DRAW_BUFFERS];
751 for (GLenum i = 0; static_cast<int>(i) < drawBufferCount; ++i)
752 {
753 drawBuffers[i] = targetAppliedDrawBuffers[i] ? stateDrawBuffers[i] : GL_NONE;
754 }
755
Geoff Lang61107632018-05-09 11:32:46 -0400756 const FunctionsGL *functions = GetFunctionsGL(context);
757 StateManagerGL *stateManager = GetStateManagerGL(context);
758
759 stateManager->bindFramebuffer(binding, mFramebufferID);
760 functions->drawBuffers(drawBufferCount, drawBuffers);
Corentin Walleze7557742017-06-01 13:09:57 -0400761 }
762}
763
Jamie Madillc564c072017-06-01 12:45:42 -0400764void FramebufferGL::syncClearState(const gl::Context *context, GLbitfield mask)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400765{
Geoff Lang61107632018-05-09 11:32:46 -0400766 const FunctionsGL *functions = GetFunctionsGL(context);
767
768 if (functions->standard == STANDARD_GL_DESKTOP)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400769 {
Geoff Lang61107632018-05-09 11:32:46 -0400770 StateManagerGL *stateManager = GetStateManagerGL(context);
771 const WorkaroundsGL &workarounds = GetWorkaroundsGL(context);
772
773 if (workarounds.doesSRGBClearsOnLinearFramebufferAttachments &&
Frank Henigmana3d333c2016-03-22 22:09:14 -0400774 (mask & GL_COLOR_BUFFER_BIT) != 0 && !mIsDefault)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400775 {
Corentin Wallez26a717b2016-09-27 08:45:42 -0700776 bool hasSRGBAttachment = false;
Jamie Madill48ef11b2016-04-27 15:21:52 -0400777 for (const auto &attachment : mState.getColorAttachments())
Geoff Langafd7f0a2015-09-09 15:33:31 -0400778 {
Frank Henigmana3d333c2016-03-22 22:09:14 -0400779 if (attachment.isAttached() && attachment.getColorEncoding() == GL_SRGB)
780 {
Corentin Wallez26a717b2016-09-27 08:45:42 -0700781 hasSRGBAttachment = true;
Frank Henigmana3d333c2016-03-22 22:09:14 -0400782 break;
783 }
Geoff Langafd7f0a2015-09-09 15:33:31 -0400784 }
Geoff Langafd7f0a2015-09-09 15:33:31 -0400785
Geoff Lang61107632018-05-09 11:32:46 -0400786 stateManager->setFramebufferSRGBEnabled(context, hasSRGBAttachment);
Frank Henigmana3d333c2016-03-22 22:09:14 -0400787 }
788 else
789 {
Geoff Lang61107632018-05-09 11:32:46 -0400790 stateManager->setFramebufferSRGBEnabled(context, !mIsDefault);
Frank Henigmana3d333c2016-03-22 22:09:14 -0400791 }
Geoff Langafd7f0a2015-09-09 15:33:31 -0400792 }
793}
794
Jamie Madillc564c072017-06-01 12:45:42 -0400795void FramebufferGL::syncClearBufferState(const gl::Context *context,
796 GLenum buffer,
797 GLint drawBuffer)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400798{
Geoff Lang61107632018-05-09 11:32:46 -0400799 const FunctionsGL *functions = GetFunctionsGL(context);
800
801 if (functions->standard == STANDARD_GL_DESKTOP)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400802 {
Geoff Lang61107632018-05-09 11:32:46 -0400803 StateManagerGL *stateManager = GetStateManagerGL(context);
804 const WorkaroundsGL &workarounds = GetWorkaroundsGL(context);
805
806 if (workarounds.doesSRGBClearsOnLinearFramebufferAttachments && buffer == GL_COLOR &&
Geoff Langafd7f0a2015-09-09 15:33:31 -0400807 !mIsDefault)
808 {
809 // If doing a clear on a color buffer, set SRGB blend enabled only if the color buffer
810 // is an SRGB format.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400811 const auto &drawbufferState = mState.getDrawBufferStates();
812 const auto &colorAttachments = mState.getColorAttachments();
Geoff Langafd7f0a2015-09-09 15:33:31 -0400813
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500814 const FramebufferAttachment *attachment = nullptr;
Geoff Langafd7f0a2015-09-09 15:33:31 -0400815 if (drawbufferState[drawBuffer] >= GL_COLOR_ATTACHMENT0 &&
816 drawbufferState[drawBuffer] < GL_COLOR_ATTACHMENT0 + colorAttachments.size())
817 {
818 size_t attachmentIdx =
819 static_cast<size_t>(drawbufferState[drawBuffer] - GL_COLOR_ATTACHMENT0);
820 attachment = &colorAttachments[attachmentIdx];
821 }
822
823 if (attachment != nullptr)
824 {
Geoff Lang61107632018-05-09 11:32:46 -0400825 stateManager->setFramebufferSRGBEnabled(context,
826 attachment->getColorEncoding() == GL_SRGB);
Geoff Langafd7f0a2015-09-09 15:33:31 -0400827 }
828 }
829 else
830 {
Geoff Lang61107632018-05-09 11:32:46 -0400831 stateManager->setFramebufferSRGBEnabled(context, !mIsDefault);
Geoff Langafd7f0a2015-09-09 15:33:31 -0400832 }
833 }
834}
Geoff Lang005a7012017-03-27 13:17:34 -0400835
836bool FramebufferGL::modifyInvalidateAttachmentsForEmulatedDefaultFBO(
837 size_t count,
838 const GLenum *attachments,
839 std::vector<GLenum> *modifiedAttachments) const
840{
841 bool needsModification = mIsDefault && mFramebufferID != 0;
842 if (!needsModification)
843 {
844 return false;
845 }
846
847 modifiedAttachments->resize(count);
848 for (size_t i = 0; i < count; i++)
849 {
850 switch (attachments[i])
851 {
852 case GL_COLOR:
853 (*modifiedAttachments)[i] = GL_COLOR_ATTACHMENT0;
854 break;
855
856 case GL_DEPTH:
857 (*modifiedAttachments)[i] = GL_DEPTH_ATTACHMENT;
858 break;
859
860 case GL_STENCIL:
861 (*modifiedAttachments)[i] = GL_STENCIL_ATTACHMENT;
862 break;
863
864 default:
865 UNREACHABLE();
866 break;
867 }
868 }
869
870 return true;
871}
872
Jamie Madille39e8f42018-10-05 08:17:38 -0400873angle::Result FramebufferGL::readPixelsRowByRow(const gl::Context *context,
874 const gl::Rectangle &area,
875 GLenum format,
876 GLenum type,
877 const gl::PixelPackState &pack,
878 GLubyte *pixels) const
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400879{
Jamie Madille39e8f42018-10-05 08:17:38 -0400880 ContextGL *contextGL = GetImplAs<ContextGL>(context);
Geoff Lang61107632018-05-09 11:32:46 -0400881 const FunctionsGL *functions = GetFunctionsGL(context);
882 StateManagerGL *stateManager = GetStateManagerGL(context);
883
Geoff Langca271392017-04-05 12:30:00 -0400884 const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400885
886 GLuint rowBytes = 0;
Jamie Madille39e8f42018-10-05 08:17:38 -0400887 ANGLE_CHECK_GL_MATH(contextGL, glFormat.computeRowPitch(type, area.width, pack.alignment,
888 pack.rowLength, &rowBytes));
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400889 GLuint skipBytes = 0;
Jamie Madille39e8f42018-10-05 08:17:38 -0400890 ANGLE_CHECK_GL_MATH(contextGL,
891 glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes));
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400892
893 gl::PixelPackState directPack;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400894 directPack.alignment = 1;
Geoff Lang61107632018-05-09 11:32:46 -0400895 stateManager->setPixelPackState(directPack);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400896
Frank Henigmanfa36c332017-06-09 18:44:45 -0400897 pixels += skipBytes;
898 for (GLint y = area.y; y < area.y + area.height; ++y)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400899 {
Geoff Lang61107632018-05-09 11:32:46 -0400900 functions->readPixels(area.x, y, area.width, 1, format, type, pixels);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400901 pixels += rowBytes;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400902 }
903
Jamie Madille39e8f42018-10-05 08:17:38 -0400904 return angle::Result::Continue();
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400905}
906
Jamie Madille39e8f42018-10-05 08:17:38 -0400907angle::Result FramebufferGL::readPixelsAllAtOnce(const gl::Context *context,
908 const gl::Rectangle &area,
909 GLenum format,
910 GLenum type,
911 const gl::PixelPackState &pack,
912 GLubyte *pixels,
913 bool readLastRowSeparately) const
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400914{
Jamie Madille39e8f42018-10-05 08:17:38 -0400915 ContextGL *contextGL = GetImplAs<ContextGL>(context);
Geoff Lang61107632018-05-09 11:32:46 -0400916 const FunctionsGL *functions = GetFunctionsGL(context);
917 StateManagerGL *stateManager = GetStateManagerGL(context);
918
Frank Henigmanfa36c332017-06-09 18:44:45 -0400919 GLint height = area.height - readLastRowSeparately;
920 if (height > 0)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400921 {
Geoff Lang61107632018-05-09 11:32:46 -0400922 stateManager->setPixelPackState(pack);
923 functions->readPixels(area.x, area.y, area.width, height, format, type, pixels);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400924 }
925
Frank Henigmanfa36c332017-06-09 18:44:45 -0400926 if (readLastRowSeparately)
927 {
928 const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400929
Frank Henigmanfa36c332017-06-09 18:44:45 -0400930 GLuint rowBytes = 0;
Jamie Madille39e8f42018-10-05 08:17:38 -0400931 ANGLE_CHECK_GL_MATH(contextGL, glFormat.computeRowPitch(type, area.width, pack.alignment,
932 pack.rowLength, &rowBytes));
Frank Henigmanfa36c332017-06-09 18:44:45 -0400933 GLuint skipBytes = 0;
Jamie Madille39e8f42018-10-05 08:17:38 -0400934 ANGLE_CHECK_GL_MATH(contextGL,
935 glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes));
Frank Henigmanfa36c332017-06-09 18:44:45 -0400936
937 gl::PixelPackState directPack;
Frank Henigmanfa36c332017-06-09 18:44:45 -0400938 directPack.alignment = 1;
Geoff Lang61107632018-05-09 11:32:46 -0400939 stateManager->setPixelPackState(directPack);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400940
941 pixels += skipBytes + (area.height - 1) * rowBytes;
Geoff Lang61107632018-05-09 11:32:46 -0400942 functions->readPixels(area.x, area.y + area.height - 1, area.width, 1, format, type,
943 pixels);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400944 }
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400945
Jamie Madille39e8f42018-10-05 08:17:38 -0400946 return angle::Result::Continue();
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400947}
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500948} // namespace rx