blob: b9edd5b9e3fbfde4bc156aa465ba8c6ee2cee8a6 [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{
Geoff Lang61107632018-05-09 11:32:46 -0400431 const FunctionsGL *functions = GetFunctionsGL(context);
432 StateManagerGL *stateManager = GetStateManagerGL(context);
433 const WorkaroundsGL &workarounds = GetWorkaroundsGL(context);
434
Frank Henigmanfa36c332017-06-09 18:44:45 -0400435 // Clip read area to framebuffer.
436 const gl::Extents fbSize = getState().getReadAttachment()->getSize();
437 const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height);
438 gl::Rectangle area;
439 if (!ClipRectangle(origArea, fbRect, &area))
440 {
441 // nothing to read
442 return gl::NoError();
443 }
444
Corentin Wallezcda6af12017-10-30 19:20:37 -0400445 PixelPackState packState = context->getGLState().getPackState();
Corentin Wallez336129f2017-10-17 15:55:40 -0400446 const gl::Buffer *packBuffer =
447 context->getGLState().getTargetBuffer(gl::BufferBinding::PixelPack);
Jamie Madill87de3622015-03-16 10:41:44 -0400448
Geoff Langf607c602016-09-21 11:46:48 -0400449 nativegl::ReadPixelsFormat readPixelsFormat =
Geoff Lang61107632018-05-09 11:32:46 -0400450 nativegl::GetReadPixelsFormat(functions, workarounds, format, type);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400451 GLenum readFormat = readPixelsFormat.format;
452 GLenum readType = readPixelsFormat.type;
Geoff Lang4ad17092015-03-10 16:47:44 -0400453
Geoff Lang61107632018-05-09 11:32:46 -0400454 stateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferID);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400455
Geoff Lang61107632018-05-09 11:32:46 -0400456 bool useOverlappingRowsWorkaround = workarounds.packOverlappingRowsSeparatelyPackBuffer &&
Corentin Wallezcda6af12017-10-30 19:20:37 -0400457 packBuffer && packState.rowLength != 0 &&
Frank Henigmanfa36c332017-06-09 18:44:45 -0400458 packState.rowLength < area.width;
459
Rafael Cintron05a449a2018-06-20 18:08:04 -0700460 GLubyte *pixels = static_cast<GLubyte *>(ptrOrOffset);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400461 int leftClip = area.x - origArea.x;
462 int topClip = area.y - origArea.y;
463 if (leftClip || topClip)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400464 {
Frank Henigmanfa36c332017-06-09 18:44:45 -0400465 // Adjust destination to match portion clipped off left and/or top.
466 const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(readFormat, readType);
467
468 GLuint rowBytes = 0;
Jamie Madillca2ff382018-07-11 09:01:17 -0400469 ANGLE_TRY_CHECKED_MATH(glFormat.computeRowPitch(
470 readType, origArea.width, packState.alignment, packState.rowLength, &rowBytes));
Frank Henigmanfa36c332017-06-09 18:44:45 -0400471 pixels += leftClip * glFormat.pixelBytes + topClip * rowBytes;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400472 }
473
Frank Henigmanfa36c332017-06-09 18:44:45 -0400474 if (packState.rowLength == 0 && area.width != origArea.width)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400475 {
Frank Henigmanfa36c332017-06-09 18:44:45 -0400476 // No rowLength was specified so it will derive from read width, but clipping changed the
477 // read width. Use the original width so we fill the user's buffer as they intended.
478 packState.rowLength = origArea.width;
479 }
Corentin Wallez886de362016-09-27 10:49:35 -0400480
Frank Henigmanfa36c332017-06-09 18:44:45 -0400481 // We want to use rowLength, but that might not be supported.
482 bool cannotSetDesiredRowLength =
483 packState.rowLength && !GetImplAs<ContextGL>(context)->getNativeExtensions().packSubimage;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400484
Frank Henigmanfa36c332017-06-09 18:44:45 -0400485 gl::Error retVal = gl::NoError();
486 if (cannotSetDesiredRowLength || useOverlappingRowsWorkaround)
487 {
488 retVal = readPixelsRowByRow(context, area, readFormat, readType, packState, pixels);
489 }
490 else
491 {
492 gl::ErrorOrResult<bool> useLastRowPaddingWorkaround = false;
Geoff Lang61107632018-05-09 11:32:46 -0400493 if (workarounds.packLastRowSeparatelyForPaddingInclusion)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400494 {
Corentin Wallezcda6af12017-10-30 19:20:37 -0400495 useLastRowPaddingWorkaround = ShouldApplyLastRowPaddingWorkaround(
496 gl::Extents(area.width, area.height, 1), packState, packBuffer, readFormat,
497 readType, false, pixels);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400498 }
499
500 if (useLastRowPaddingWorkaround.isError())
501 {
502 retVal = useLastRowPaddingWorkaround.getError();
503 }
504 else
505 {
506 retVal = readPixelsAllAtOnce(context, area, readFormat, readType, packState, pixels,
507 useLastRowPaddingWorkaround.getResult());
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400508 }
509 }
510
Frank Henigmanfa36c332017-06-09 18:44:45 -0400511 return retVal;
Geoff Langf9a6f082015-01-22 13:32:49 -0500512}
513
Jamie Madillc564c072017-06-01 12:45:42 -0400514Error FramebufferGL::blit(const gl::Context *context,
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500515 const gl::Rectangle &sourceArea,
516 const gl::Rectangle &destArea,
517 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -0400518 GLenum filter)
Geoff Langf9a6f082015-01-22 13:32:49 -0500519{
Geoff Lang61107632018-05-09 11:32:46 -0400520 const FunctionsGL *functions = GetFunctionsGL(context);
521 StateManagerGL *stateManager = GetStateManagerGL(context);
522
Jamie Madill231c7f52017-04-26 13:45:37 -0400523 const Framebuffer *sourceFramebuffer = context->getGLState().getReadFramebuffer();
524 const Framebuffer *destFramebuffer = context->getGLState().getDrawFramebuffer();
Geoff Lang4ad17092015-03-10 16:47:44 -0400525
Corentin Wallez6898b352016-11-10 11:41:15 -0500526 const FramebufferAttachment *colorReadAttachment = sourceFramebuffer->getReadColorbuffer();
Corentin Wallez4596a762016-12-20 14:50:18 -0500527
528 GLsizei readAttachmentSamples = 0;
529 if (colorReadAttachment != nullptr)
530 {
531 readAttachmentSamples = colorReadAttachment->getSamples();
532 }
Corentin Wallez6898b352016-11-10 11:41:15 -0500533
Corentin Wallez26a717b2016-09-27 08:45:42 -0700534 bool needManualColorBlit = false;
535
Corentin Wallez6898b352016-11-10 11:41:15 -0500536 // TODO(cwallez) when the filter is LINEAR and both source and destination are SRGB, we
537 // could avoid doing a manual blit.
538
539 // Prior to OpenGL 4.4 BlitFramebuffer (section 18.3.1 of GL 4.3 core profile) reads:
540 // When values are taken from the read buffer, no linearization is performed, even
541 // if the format of the buffer is SRGB.
542 // Starting from OpenGL 4.4 (section 18.3.1) it reads:
543 // When values are taken from the read buffer, if FRAMEBUFFER_SRGB is enabled and the
544 // value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the framebuffer attachment
545 // corresponding to the read buffer is SRGB, the red, green, and blue components are
546 // converted from the non-linear sRGB color space according [...].
Corentin Wallez26a717b2016-09-27 08:45:42 -0700547 {
Jamie Madill231c7f52017-04-26 13:45:37 -0400548 bool sourceSRGB =
549 colorReadAttachment != nullptr && colorReadAttachment->getColorEncoding() == GL_SRGB;
Corentin Wallez6898b352016-11-10 11:41:15 -0500550 needManualColorBlit =
Geoff Lang61107632018-05-09 11:32:46 -0400551 needManualColorBlit || (sourceSRGB && functions->isAtMostGL(gl::Version(4, 3)));
Corentin Wallez6898b352016-11-10 11:41:15 -0500552 }
Corentin Wallez26a717b2016-09-27 08:45:42 -0700553
Corentin Wallez6898b352016-11-10 11:41:15 -0500554 // Prior to OpenGL 4.2 BlitFramebuffer (section 4.3.2 of GL 4.1 core profile) reads:
555 // Blit operations bypass the fragment pipeline. The only fragment operations which
556 // affect a blit are the pixel ownership test and scissor test.
557 // Starting from OpenGL 4.2 (section 4.3.2) it reads:
558 // When values are written to the draw buffers, blit operations bypass the fragment
559 // pipeline. The only fragment operations which affect a blit are the pixel ownership
560 // test, the scissor test and sRGB conversion.
561 if (!needManualColorBlit)
562 {
563 bool destSRGB = false;
564 for (size_t i = 0; i < destFramebuffer->getDrawbufferStateCount(); ++i)
Corentin Wallez26a717b2016-09-27 08:45:42 -0700565 {
Corentin Wallez6898b352016-11-10 11:41:15 -0500566 const FramebufferAttachment *attachment = destFramebuffer->getDrawBuffer(i);
567 if (attachment && attachment->getColorEncoding() == GL_SRGB)
Corentin Wallez26a717b2016-09-27 08:45:42 -0700568 {
Corentin Wallez6898b352016-11-10 11:41:15 -0500569 destSRGB = true;
570 break;
Corentin Wallez26a717b2016-09-27 08:45:42 -0700571 }
Corentin Wallez26a717b2016-09-27 08:45:42 -0700572 }
Corentin Wallez6898b352016-11-10 11:41:15 -0500573
574 needManualColorBlit =
Geoff Lang61107632018-05-09 11:32:46 -0400575 needManualColorBlit || (destSRGB && functions->isAtMostGL(gl::Version(4, 1)));
Corentin Wallez26a717b2016-09-27 08:45:42 -0700576 }
577
578 // Enable FRAMEBUFFER_SRGB if needed
Geoff Lang61107632018-05-09 11:32:46 -0400579 stateManager->setFramebufferSRGBEnabledForFramebuffer(context, true, this);
Corentin Wallez26a717b2016-09-27 08:45:42 -0700580
581 GLenum blitMask = mask;
Corentin Wallez6898b352016-11-10 11:41:15 -0500582 if (needManualColorBlit && (mask & GL_COLOR_BUFFER_BIT) && readAttachmentSamples <= 1)
Corentin Wallez26a717b2016-09-27 08:45:42 -0700583 {
Geoff Lang61107632018-05-09 11:32:46 -0400584 BlitGL *blitter = GetBlitGL(context);
585 ANGLE_TRY(blitter->blitColorBufferWithShader(sourceFramebuffer, destFramebuffer, sourceArea,
586 destArea, filter));
Corentin Wallez26a717b2016-09-27 08:45:42 -0700587 blitMask &= ~GL_COLOR_BUFFER_BIT;
588 }
589
590 if (blitMask == 0)
591 {
592 return gl::NoError();
593 }
594
595 const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer);
Geoff Lang61107632018-05-09 11:32:46 -0400596 stateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID());
597 stateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID);
Geoff Lang4ad17092015-03-10 16:47:44 -0400598
Geoff Lang61107632018-05-09 11:32:46 -0400599 functions->blitFramebuffer(sourceArea.x, sourceArea.y, sourceArea.x1(), sourceArea.y1(),
600 destArea.x, destArea.y, destArea.x1(), destArea.y1(), blitMask,
601 filter);
Geoff Lang4ad17092015-03-10 16:47:44 -0400602
Corentin Wallez26a717b2016-09-27 08:45:42 -0700603 return gl::NoError();
Geoff Langf9a6f082015-01-22 13:32:49 -0500604}
605
Geoff Lang13455072018-05-09 11:24:43 -0400606gl::Error FramebufferGL::getSamplePosition(const gl::Context *context,
607 size_t index,
608 GLfloat *xy) const
JiangYizhoubddc46b2016-12-09 09:50:51 +0800609{
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 functions->getMultisamplefv(GL_SAMPLE_POSITION, static_cast<GLuint>(index), xy);
JiangYizhoubddc46b2016-12-09 09:50:51 +0800615 return gl::NoError();
616}
617
Kenneth Russellce8602a2017-10-03 18:23:08 -0700618bool FramebufferGL::checkStatus(const gl::Context *context) const
Geoff Langf9a6f082015-01-22 13:32:49 -0500619{
Geoff Lang61107632018-05-09 11:32:46 -0400620 const FunctionsGL *functions = GetFunctionsGL(context);
621 StateManagerGL *stateManager = GetStateManagerGL(context);
622
623 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
624 GLenum status = functions->checkFramebufferStatus(GL_FRAMEBUFFER);
Jamie Madillcc86d642015-11-24 13:00:07 -0500625 if (status != GL_FRAMEBUFFER_COMPLETE)
626 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500627 WARN() << "GL framebuffer returned incomplete.";
Jamie Madillcc86d642015-11-24 13:00:07 -0500628 }
629 return (status == GL_FRAMEBUFFER_COMPLETE);
Geoff Lang4ad17092015-03-10 16:47:44 -0400630}
631
Jamie Madill19fa1c62018-03-08 09:47:21 -0500632gl::Error FramebufferGL::syncState(const gl::Context *context,
633 const Framebuffer::DirtyBits &dirtyBits)
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500634{
635 // Don't need to sync state for the default FBO.
636 if (mIsDefault)
637 {
Jamie Madill19fa1c62018-03-08 09:47:21 -0500638 return gl::NoError();
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500639 }
640
Geoff Lang61107632018-05-09 11:32:46 -0400641 const FunctionsGL *functions = GetFunctionsGL(context);
642 StateManagerGL *stateManager = GetStateManagerGL(context);
643
644 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500645
Martin Radev7c2e6a92017-08-28 11:13:16 +0300646 // A pointer to one of the attachments for which the texture or the render buffer is not zero.
647 const FramebufferAttachment *attachment = nullptr;
Martin Radev878c8b12017-07-28 09:51:04 +0300648
Jamie Madill6de51852017-04-12 09:53:01 -0400649 for (auto dirtyBit : dirtyBits)
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500650 {
651 switch (dirtyBit)
652 {
653 case Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT:
Martin Radev7c2e6a92017-08-28 11:13:16 +0300654 {
655 const FramebufferAttachment *newAttachment = mState.getDepthAttachment();
Geoff Lang61107632018-05-09 11:32:46 -0400656 BindFramebufferAttachment(functions, GL_DEPTH_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_STENCIL_ATTACHMENT:
Martin Radev7c2e6a92017-08-28 11:13:16 +0300664 {
665 const FramebufferAttachment *newAttachment = mState.getStencilAttachment();
Geoff Lang61107632018-05-09 11:32:46 -0400666 BindFramebufferAttachment(functions, GL_STENCIL_ATTACHMENT, newAttachment);
Martin Radev7c2e6a92017-08-28 11:13:16 +0300667 if (newAttachment)
668 {
669 attachment = newAttachment;
670 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500671 break;
Martin Radev7c2e6a92017-08-28 11:13:16 +0300672 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500673 case Framebuffer::DIRTY_BIT_DRAW_BUFFERS:
674 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400675 const auto &drawBuffers = mState.getDrawBufferStates();
Geoff Lang61107632018-05-09 11:32:46 -0400676 functions->drawBuffers(static_cast<GLsizei>(drawBuffers.size()),
677 drawBuffers.data());
Corentin Walleze7557742017-06-01 13:09:57 -0400678 mAppliedEnabledDrawBuffers = mState.getEnabledDrawBuffers();
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500679 break;
680 }
681 case Framebuffer::DIRTY_BIT_READ_BUFFER:
Geoff Lang61107632018-05-09 11:32:46 -0400682 functions->readBuffer(mState.getReadBufferState());
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500683 break;
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800684 case Framebuffer::DIRTY_BIT_DEFAULT_WIDTH:
Geoff Lang61107632018-05-09 11:32:46 -0400685 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_WIDTH,
686 mState.getDefaultWidth());
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800687 break;
688 case Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT:
Geoff Lang61107632018-05-09 11:32:46 -0400689 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_HEIGHT,
690 mState.getDefaultHeight());
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800691 break;
692 case Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES:
Geoff Lang61107632018-05-09 11:32:46 -0400693 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES,
694 mState.getDefaultSamples());
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800695 break;
696 case Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS:
Geoff Lang61107632018-05-09 11:32:46 -0400697 functions->framebufferParameteri(
Geoff Lang92019432017-11-20 13:09:34 -0500698 GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS,
699 gl::ConvertToGLBoolean(mState.getDefaultFixedSampleLocations()));
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800700 break;
Jiawei Shaob1e91382018-05-17 14:33:55 +0800701 case Framebuffer::DIRTY_BIT_DEFAULT_LAYERS:
702 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_LAYERS_EXT,
703 mState.getDefaultLayers());
704 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500705 default:
706 {
707 ASSERT(Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0 &&
708 dirtyBit < Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX);
709 size_t index =
710 static_cast<size_t>(dirtyBit - Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0);
Martin Radev7c2e6a92017-08-28 11:13:16 +0300711 const FramebufferAttachment *newAttachment = mState.getColorAttachment(index);
712 BindFramebufferAttachment(
Geoff Lang61107632018-05-09 11:32:46 -0400713 functions, static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + index), newAttachment);
Martin Radev7c2e6a92017-08-28 11:13:16 +0300714 if (newAttachment)
715 {
716 attachment = newAttachment;
717 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500718 break;
719 }
720 }
721 }
Martin Radev878c8b12017-07-28 09:51:04 +0300722
Martin Radev7c2e6a92017-08-28 11:13:16 +0300723 if (attachment)
Martin Radev878c8b12017-07-28 09:51:04 +0300724 {
Martin Radev7c2e6a92017-08-28 11:13:16 +0300725 const bool isSideBySide =
726 (attachment->getMultiviewLayout() == GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE);
Geoff Lang61107632018-05-09 11:32:46 -0400727 stateManager->setSideBySide(isSideBySide);
728 stateManager->setViewportOffsets(attachment->getMultiviewViewportOffsets());
729 stateManager->updateMultiviewBaseViewLayerIndexUniform(context->getGLState().getProgram(),
730 getState());
Martin Radev878c8b12017-07-28 09:51:04 +0300731 }
Jamie Madill19fa1c62018-03-08 09:47:21 -0500732
733 return gl::NoError();
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500734}
735
Geoff Lang4ad17092015-03-10 16:47:44 -0400736GLuint FramebufferGL::getFramebufferID() const
737{
738 return mFramebufferID;
Geoff Langf9a6f082015-01-22 13:32:49 -0500739}
740
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700741bool FramebufferGL::isDefault() const
Geoff Langafd7f0a2015-09-09 15:33:31 -0400742{
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700743 return mIsDefault;
Geoff Langafd7f0a2015-09-09 15:33:31 -0400744}
745
Geoff Lang61107632018-05-09 11:32:46 -0400746void FramebufferGL::maskOutInactiveOutputDrawBuffers(const gl::Context *context,
747 GLenum binding,
748 DrawBufferMask maxSet)
Corentin Walleze7557742017-06-01 13:09:57 -0400749{
Geoff Lang61107632018-05-09 11:32:46 -0400750
Corentin Walleze7557742017-06-01 13:09:57 -0400751 auto targetAppliedDrawBuffers = mState.getEnabledDrawBuffers() & maxSet;
752 if (mAppliedEnabledDrawBuffers != targetAppliedDrawBuffers)
753 {
754 mAppliedEnabledDrawBuffers = targetAppliedDrawBuffers;
755
756 const auto &stateDrawBuffers = mState.getDrawBufferStates();
757 GLsizei drawBufferCount = static_cast<GLsizei>(stateDrawBuffers.size());
758 ASSERT(drawBufferCount <= IMPLEMENTATION_MAX_DRAW_BUFFERS);
759
760 GLenum drawBuffers[IMPLEMENTATION_MAX_DRAW_BUFFERS];
761 for (GLenum i = 0; static_cast<int>(i) < drawBufferCount; ++i)
762 {
763 drawBuffers[i] = targetAppliedDrawBuffers[i] ? stateDrawBuffers[i] : GL_NONE;
764 }
765
Geoff Lang61107632018-05-09 11:32:46 -0400766 const FunctionsGL *functions = GetFunctionsGL(context);
767 StateManagerGL *stateManager = GetStateManagerGL(context);
768
769 stateManager->bindFramebuffer(binding, mFramebufferID);
770 functions->drawBuffers(drawBufferCount, drawBuffers);
Corentin Walleze7557742017-06-01 13:09:57 -0400771 }
772}
773
Jamie Madillc564c072017-06-01 12:45:42 -0400774void FramebufferGL::syncClearState(const gl::Context *context, GLbitfield mask)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400775{
Geoff Lang61107632018-05-09 11:32:46 -0400776 const FunctionsGL *functions = GetFunctionsGL(context);
777
778 if (functions->standard == STANDARD_GL_DESKTOP)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400779 {
Geoff Lang61107632018-05-09 11:32:46 -0400780 StateManagerGL *stateManager = GetStateManagerGL(context);
781 const WorkaroundsGL &workarounds = GetWorkaroundsGL(context);
782
783 if (workarounds.doesSRGBClearsOnLinearFramebufferAttachments &&
Frank Henigmana3d333c2016-03-22 22:09:14 -0400784 (mask & GL_COLOR_BUFFER_BIT) != 0 && !mIsDefault)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400785 {
Corentin Wallez26a717b2016-09-27 08:45:42 -0700786 bool hasSRGBAttachment = false;
Jamie Madill48ef11b2016-04-27 15:21:52 -0400787 for (const auto &attachment : mState.getColorAttachments())
Geoff Langafd7f0a2015-09-09 15:33:31 -0400788 {
Frank Henigmana3d333c2016-03-22 22:09:14 -0400789 if (attachment.isAttached() && attachment.getColorEncoding() == GL_SRGB)
790 {
Corentin Wallez26a717b2016-09-27 08:45:42 -0700791 hasSRGBAttachment = true;
Frank Henigmana3d333c2016-03-22 22:09:14 -0400792 break;
793 }
Geoff Langafd7f0a2015-09-09 15:33:31 -0400794 }
Geoff Langafd7f0a2015-09-09 15:33:31 -0400795
Geoff Lang61107632018-05-09 11:32:46 -0400796 stateManager->setFramebufferSRGBEnabled(context, hasSRGBAttachment);
Frank Henigmana3d333c2016-03-22 22:09:14 -0400797 }
798 else
799 {
Geoff Lang61107632018-05-09 11:32:46 -0400800 stateManager->setFramebufferSRGBEnabled(context, !mIsDefault);
Frank Henigmana3d333c2016-03-22 22:09:14 -0400801 }
Geoff Langafd7f0a2015-09-09 15:33:31 -0400802 }
803}
804
Jamie Madillc564c072017-06-01 12:45:42 -0400805void FramebufferGL::syncClearBufferState(const gl::Context *context,
806 GLenum buffer,
807 GLint drawBuffer)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400808{
Geoff Lang61107632018-05-09 11:32:46 -0400809 const FunctionsGL *functions = GetFunctionsGL(context);
810
811 if (functions->standard == STANDARD_GL_DESKTOP)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400812 {
Geoff Lang61107632018-05-09 11:32:46 -0400813 StateManagerGL *stateManager = GetStateManagerGL(context);
814 const WorkaroundsGL &workarounds = GetWorkaroundsGL(context);
815
816 if (workarounds.doesSRGBClearsOnLinearFramebufferAttachments && buffer == GL_COLOR &&
Geoff Langafd7f0a2015-09-09 15:33:31 -0400817 !mIsDefault)
818 {
819 // If doing a clear on a color buffer, set SRGB blend enabled only if the color buffer
820 // is an SRGB format.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400821 const auto &drawbufferState = mState.getDrawBufferStates();
822 const auto &colorAttachments = mState.getColorAttachments();
Geoff Langafd7f0a2015-09-09 15:33:31 -0400823
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500824 const FramebufferAttachment *attachment = nullptr;
Geoff Langafd7f0a2015-09-09 15:33:31 -0400825 if (drawbufferState[drawBuffer] >= GL_COLOR_ATTACHMENT0 &&
826 drawbufferState[drawBuffer] < GL_COLOR_ATTACHMENT0 + colorAttachments.size())
827 {
828 size_t attachmentIdx =
829 static_cast<size_t>(drawbufferState[drawBuffer] - GL_COLOR_ATTACHMENT0);
830 attachment = &colorAttachments[attachmentIdx];
831 }
832
833 if (attachment != nullptr)
834 {
Geoff Lang61107632018-05-09 11:32:46 -0400835 stateManager->setFramebufferSRGBEnabled(context,
836 attachment->getColorEncoding() == GL_SRGB);
Geoff Langafd7f0a2015-09-09 15:33:31 -0400837 }
838 }
839 else
840 {
Geoff Lang61107632018-05-09 11:32:46 -0400841 stateManager->setFramebufferSRGBEnabled(context, !mIsDefault);
Geoff Langafd7f0a2015-09-09 15:33:31 -0400842 }
843 }
844}
Geoff Lang005a7012017-03-27 13:17:34 -0400845
846bool FramebufferGL::modifyInvalidateAttachmentsForEmulatedDefaultFBO(
847 size_t count,
848 const GLenum *attachments,
849 std::vector<GLenum> *modifiedAttachments) const
850{
851 bool needsModification = mIsDefault && mFramebufferID != 0;
852 if (!needsModification)
853 {
854 return false;
855 }
856
857 modifiedAttachments->resize(count);
858 for (size_t i = 0; i < count; i++)
859 {
860 switch (attachments[i])
861 {
862 case GL_COLOR:
863 (*modifiedAttachments)[i] = GL_COLOR_ATTACHMENT0;
864 break;
865
866 case GL_DEPTH:
867 (*modifiedAttachments)[i] = GL_DEPTH_ATTACHMENT;
868 break;
869
870 case GL_STENCIL:
871 (*modifiedAttachments)[i] = GL_STENCIL_ATTACHMENT;
872 break;
873
874 default:
875 UNREACHABLE();
876 break;
877 }
878 }
879
880 return true;
881}
882
Frank Henigmanfa36c332017-06-09 18:44:45 -0400883gl::Error FramebufferGL::readPixelsRowByRow(const gl::Context *context,
884 const gl::Rectangle &area,
885 GLenum format,
886 GLenum type,
887 const gl::PixelPackState &pack,
888 GLubyte *pixels) const
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400889{
Geoff Lang61107632018-05-09 11:32:46 -0400890 const FunctionsGL *functions = GetFunctionsGL(context);
891 StateManagerGL *stateManager = GetStateManagerGL(context);
892
Geoff Langca271392017-04-05 12:30:00 -0400893 const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400894
895 GLuint rowBytes = 0;
Jamie Madillca2ff382018-07-11 09:01:17 -0400896 ANGLE_TRY_CHECKED_MATH(
897 glFormat.computeRowPitch(type, area.width, pack.alignment, pack.rowLength, &rowBytes));
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400898 GLuint skipBytes = 0;
Jamie Madillca2ff382018-07-11 09:01:17 -0400899 ANGLE_TRY_CHECKED_MATH(glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes));
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400900
901 gl::PixelPackState directPack;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400902 directPack.alignment = 1;
Geoff Lang61107632018-05-09 11:32:46 -0400903 stateManager->setPixelPackState(directPack);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400904
Frank Henigmanfa36c332017-06-09 18:44:45 -0400905 pixels += skipBytes;
906 for (GLint y = area.y; y < area.y + area.height; ++y)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400907 {
Geoff Lang61107632018-05-09 11:32:46 -0400908 functions->readPixels(area.x, y, area.width, 1, format, type, pixels);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400909 pixels += rowBytes;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400910 }
911
912 return gl::NoError();
913}
914
Frank Henigmanfa36c332017-06-09 18:44:45 -0400915gl::Error FramebufferGL::readPixelsAllAtOnce(const gl::Context *context,
916 const gl::Rectangle &area,
917 GLenum format,
918 GLenum type,
919 const gl::PixelPackState &pack,
920 GLubyte *pixels,
921 bool readLastRowSeparately) const
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400922{
Geoff Lang61107632018-05-09 11:32:46 -0400923 const FunctionsGL *functions = GetFunctionsGL(context);
924 StateManagerGL *stateManager = GetStateManagerGL(context);
925
Frank Henigmanfa36c332017-06-09 18:44:45 -0400926 GLint height = area.height - readLastRowSeparately;
927 if (height > 0)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400928 {
Geoff Lang61107632018-05-09 11:32:46 -0400929 stateManager->setPixelPackState(pack);
930 functions->readPixels(area.x, area.y, area.width, height, format, type, pixels);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400931 }
932
Frank Henigmanfa36c332017-06-09 18:44:45 -0400933 if (readLastRowSeparately)
934 {
935 const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400936
Frank Henigmanfa36c332017-06-09 18:44:45 -0400937 GLuint rowBytes = 0;
Jamie Madillca2ff382018-07-11 09:01:17 -0400938 ANGLE_TRY_CHECKED_MATH(
939 glFormat.computeRowPitch(type, area.width, pack.alignment, pack.rowLength, &rowBytes));
Frank Henigmanfa36c332017-06-09 18:44:45 -0400940 GLuint skipBytes = 0;
Jamie Madillca2ff382018-07-11 09:01:17 -0400941 ANGLE_TRY_CHECKED_MATH(
942 glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes));
Frank Henigmanfa36c332017-06-09 18:44:45 -0400943
944 gl::PixelPackState directPack;
Frank Henigmanfa36c332017-06-09 18:44:45 -0400945 directPack.alignment = 1;
Geoff Lang61107632018-05-09 11:32:46 -0400946 stateManager->setPixelPackState(directPack);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400947
948 pixels += skipBytes + (area.height - 1) * rowBytes;
Geoff Lang61107632018-05-09 11:32:46 -0400949 functions->readPixels(area.x, area.y + area.height - 1, area.width, 1, format, type,
950 pixels);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400951 }
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400952
953 return gl::NoError();
954}
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500955} // namespace rx