blob: 97a808122539d786dc33321d7afa804d48fbcfdf [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"
Geoff Lang4ad17092015-03-10 16:47:44 -040013#include "libANGLE/FramebufferAttachment.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040014#include "libANGLE/State.h"
Geoff Lang4ad17092015-03-10 16:47:44 -040015#include "libANGLE/angletypes.h"
16#include "libANGLE/formatutils.h"
Geoff Lang92019432017-11-20 13:09:34 -050017#include "libANGLE/queryconversions.h"
Jamie Madill8415b5f2016-04-26 13:41:39 -040018#include "libANGLE/renderer/ContextImpl.h"
Corentin Wallez26a717b2016-09-27 08:45:42 -070019#include "libANGLE/renderer/gl/BlitGL.h"
Martin Radev5e424fa2017-08-09 16:25:36 +030020#include "libANGLE/renderer/gl/ClearMultiviewGL.h"
Frank Henigmanfa36c332017-06-09 18:44:45 -040021#include "libANGLE/renderer/gl/ContextGL.h"
Geoff Lang4ad17092015-03-10 16:47:44 -040022#include "libANGLE/renderer/gl/FunctionsGL.h"
Jacek Cabanfa60f692015-04-27 18:23:44 +020023#include "libANGLE/renderer/gl/RenderbufferGL.h"
Geoff Lang4ad17092015-03-10 16:47:44 -040024#include "libANGLE/renderer/gl/StateManagerGL.h"
25#include "libANGLE/renderer/gl/TextureGL.h"
Geoff Langafd7f0a2015-09-09 15:33:31 -040026#include "libANGLE/renderer/gl/WorkaroundsGL.h"
Geoff Langf607c602016-09-21 11:46:48 -040027#include "libANGLE/renderer/gl/formatutilsgl.h"
Corentin Wallez886de362016-09-27 10:49:35 -040028#include "libANGLE/renderer/gl/renderergl_utils.h"
Jamie Madillcc86d642015-11-24 13:00:07 -050029#include "platform/Platform.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050030
Jamie Madill60ec6ea2016-01-22 15:27:19 -050031using namespace gl;
Corentin Wallez9a8d3662016-09-22 12:18:29 -040032using angle::CheckedNumeric;
Jamie Madill60ec6ea2016-01-22 15:27:19 -050033
Geoff Langf9a6f082015-01-22 13:32:49 -050034namespace rx
35{
36
Martin Radev878c8b12017-07-28 09:51:04 +030037namespace
Geoff Lang4ad17092015-03-10 16:47:44 -040038{
Geoff Langf9a6f082015-01-22 13:32:49 -050039
Martin Radev878c8b12017-07-28 09:51:04 +030040void BindFramebufferAttachment(const FunctionsGL *functions,
41 GLenum attachmentPoint,
42 const FramebufferAttachment *attachment)
Geoff Lang4ad17092015-03-10 16:47:44 -040043{
44 if (attachment)
45 {
46 if (attachment->type() == GL_TEXTURE)
47 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -050048 const Texture *texture = attachment->getTexture();
Geoff Lang4ad17092015-03-10 16:47:44 -040049 const TextureGL *textureGL = GetImplAs<TextureGL>(texture);
50
Corentin Wallez99d492c2018-02-27 15:17:10 -050051 if (texture->getType() == TextureType::_2D ||
52 texture->getType() == TextureType::_2DMultisample ||
53 texture->getType() == TextureType::Rectangle)
Geoff Lang4ad17092015-03-10 16:47:44 -040054 {
JiangYizhoubddc46b2016-12-09 09:50:51 +080055 functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint,
Corentin Wallez99d492c2018-02-27 15:17:10 -050056 ToGLenum(texture->getType()),
Geoff Lang4ad17092015-03-10 16:47:44 -040057 textureGL->getTextureID(), attachment->mipLevel());
58 }
Jiawei Shaoa8802472018-05-28 11:17:47 +080059 else if (attachment->isLayered())
60 {
61 TextureType textureType = texture->getType();
62 ASSERT(textureType == TextureType::_2DArray || textureType == TextureType::_3D ||
Olli Etuahodff32a02018-08-28 14:35:50 +030063 textureType == TextureType::CubeMap ||
64 textureType == TextureType::_2DMultisampleArray);
Jiawei Shaoa8802472018-05-28 11:17:47 +080065 functions->framebufferTexture(GL_FRAMEBUFFER, attachmentPoint,
66 textureGL->getTextureID(), attachment->mipLevel());
67 }
Corentin Wallez99d492c2018-02-27 15:17:10 -050068 else if (texture->getType() == TextureType::CubeMap)
69 {
70 functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint,
71 ToGLenum(attachment->cubeMapFace()),
72 textureGL->getTextureID(), attachment->mipLevel());
73 }
74 else if (texture->getType() == TextureType::_2DArray ||
Olli Etuahofd162102018-08-27 16:14:57 +030075 texture->getType() == TextureType::_3D ||
76 texture->getType() == TextureType::_2DMultisampleArray)
Geoff Lang4ad17092015-03-10 16:47:44 -040077 {
Martin Radev5e424fa2017-08-09 16:25:36 +030078 if (attachment->getMultiviewLayout() == GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE)
79 {
80 ASSERT(functions->framebufferTexture);
81 functions->framebufferTexture(GL_FRAMEBUFFER, attachmentPoint,
82 textureGL->getTextureID(),
83 attachment->mipLevel());
84 }
85 else
86 {
87 functions->framebufferTextureLayer(GL_FRAMEBUFFER, attachmentPoint,
88 textureGL->getTextureID(),
89 attachment->mipLevel(), attachment->layer());
90 }
Geoff Lang4ad17092015-03-10 16:47:44 -040091 }
92 else
93 {
94 UNREACHABLE();
95 }
96 }
97 else if (attachment->type() == GL_RENDERBUFFER)
98 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -050099 const Renderbuffer *renderbuffer = attachment->getRenderbuffer();
Geoff Langcd69f1c2015-03-18 14:33:23 -0400100 const RenderbufferGL *renderbufferGL = GetImplAs<RenderbufferGL>(renderbuffer);
Geoff Lang4ad17092015-03-10 16:47:44 -0400101
Geoff Langcd69f1c2015-03-18 14:33:23 -0400102 functions->framebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER,
103 renderbufferGL->getRenderbufferID());
Geoff Lang4ad17092015-03-10 16:47:44 -0400104 }
105 else
106 {
107 UNREACHABLE();
108 }
109 }
110 else
111 {
112 // Unbind this attachment
113 functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, 0, 0);
114 }
115}
Geoff Langf9a6f082015-01-22 13:32:49 -0500116
Martin Radev61e710b2017-09-05 11:59:52 +0300117bool AreAllLayersActive(const FramebufferAttachment &attachment)
Martin Radevb0761932017-07-25 17:42:25 +0300118{
Martin Radev61e710b2017-09-05 11:59:52 +0300119 int baseViewIndex = attachment.getBaseViewIndex();
120 if (baseViewIndex != 0)
121 {
122 return false;
123 }
124 const ImageIndex &imageIndex = attachment.getTextureImageIndex();
Jamie Madillcc129372018-04-12 09:13:18 -0400125 int numLayers = static_cast<int>(
126 attachment.getTexture()->getDepth(imageIndex.getTarget(), imageIndex.getLevelIndex()));
Martin Radev61e710b2017-09-05 11:59:52 +0300127 return (attachment.getNumViews() == numLayers);
128}
129
130bool RequiresMultiviewClear(const FramebufferState &state, bool scissorTestEnabled)
131{
132 // Get one attachment and check whether all layers are attached.
133 const FramebufferAttachment *attachment = nullptr;
134 bool allTextureArraysAreFullyAttached = true;
135 for (const FramebufferAttachment &colorAttachment : state.getColorAttachments())
136 {
137 if (colorAttachment.isAttached())
138 {
139 if (colorAttachment.getMultiviewLayout() == GL_NONE)
140 {
141 return false;
142 }
143 attachment = &colorAttachment;
144 allTextureArraysAreFullyAttached =
145 allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment);
146 }
147 }
148
149 const FramebufferAttachment *depthAttachment = state.getDepthAttachment();
150 if (depthAttachment)
151 {
152 if (depthAttachment->getMultiviewLayout() == GL_NONE)
153 {
154 return false;
155 }
156 attachment = depthAttachment;
157 allTextureArraysAreFullyAttached =
158 allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment);
159 }
160 const FramebufferAttachment *stencilAttachment = state.getStencilAttachment();
161 if (stencilAttachment)
162 {
163 if (stencilAttachment->getMultiviewLayout() == GL_NONE)
164 {
165 return false;
166 }
167 attachment = stencilAttachment;
168 allTextureArraysAreFullyAttached =
169 allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment);
170 }
171
Martin Radev5e424fa2017-08-09 16:25:36 +0300172 if (attachment == nullptr)
173 {
174 return false;
175 }
176 switch (attachment->getMultiviewLayout())
177 {
178 case GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE:
Martin Radev61e710b2017-09-05 11:59:52 +0300179 // If all layers of each texture array are active, then there is no need to issue a
180 // special multiview clear.
181 return !allTextureArraysAreFullyAttached;
Martin Radev5e424fa2017-08-09 16:25:36 +0300182 case GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE:
183 return (scissorTestEnabled == true);
Martin Radev5e424fa2017-08-09 16:25:36 +0300184 default:
185 UNREACHABLE();
186 }
187 return false;
Martin Radevb0761932017-07-25 17:42:25 +0300188}
189
Martin Radev878c8b12017-07-28 09:51:04 +0300190} // namespace
191
Geoff Lang61107632018-05-09 11:32:46 -0400192FramebufferGL::FramebufferGL(const gl::FramebufferState &data, GLuint id, bool isDefault)
193 : FramebufferImpl(data),
Martin Radev878c8b12017-07-28 09:51:04 +0300194 mFramebufferID(id),
Geoff Lang61107632018-05-09 11:32:46 -0400195 mIsDefault(isDefault),
Martin Radev878c8b12017-07-28 09:51:04 +0300196 mAppliedEnabledDrawBuffers(1)
197{
198}
199
200FramebufferGL::~FramebufferGL()
201{
Geoff Lang61107632018-05-09 11:32:46 -0400202 ASSERT(mFramebufferID == 0);
203}
204
205void FramebufferGL::destroy(const gl::Context *context)
206{
207 StateManagerGL *stateManager = GetStateManagerGL(context);
208 stateManager->deleteFramebuffer(mFramebufferID);
Martin Radev878c8b12017-07-28 09:51:04 +0300209 mFramebufferID = 0;
210}
211
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400212angle::Result FramebufferGL::discard(const gl::Context *context,
213 size_t count,
214 const GLenum *attachments)
Austin Kinross08332632015-05-05 13:35:47 -0700215{
Geoff Lang57ce9ea2016-11-24 12:03:14 -0500216 // glInvalidateFramebuffer accepts the same enums as glDiscardFramebufferEXT
Jamie Madill4928b7c2017-06-20 12:57:39 -0400217 return invalidate(context, count, attachments);
Austin Kinross08332632015-05-05 13:35:47 -0700218}
219
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400220angle::Result FramebufferGL::invalidate(const gl::Context *context,
221 size_t count,
222 const GLenum *attachments)
Geoff Langf9a6f082015-01-22 13:32:49 -0500223{
Geoff Lang005a7012017-03-27 13:17:34 -0400224 const GLenum *finalAttachmentsPtr = attachments;
225
226 std::vector<GLenum> modifiedAttachments;
227 if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments))
228 {
229 finalAttachmentsPtr = modifiedAttachments.data();
230 }
231
Geoff Lang61107632018-05-09 11:32:46 -0400232 const FunctionsGL *functions = GetFunctionsGL(context);
233 StateManagerGL *stateManager = GetStateManagerGL(context);
234
Geoff Lang57ce9ea2016-11-24 12:03:14 -0500235 // Since this function is just a hint, only call a native function if it exists.
Geoff Lang61107632018-05-09 11:32:46 -0400236 if (functions->invalidateFramebuffer)
Geoff Lang64a72442015-04-01 14:43:11 -0400237 {
Geoff Lang61107632018-05-09 11:32:46 -0400238 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
239 functions->invalidateFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count),
240 finalAttachmentsPtr);
Geoff Lang64a72442015-04-01 14:43:11 -0400241 }
Geoff Lang61107632018-05-09 11:32:46 -0400242 else if (functions->discardFramebufferEXT)
Geoff Lang57ce9ea2016-11-24 12:03:14 -0500243 {
Geoff Lang61107632018-05-09 11:32:46 -0400244 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
245 functions->discardFramebufferEXT(GL_FRAMEBUFFER, static_cast<GLsizei>(count),
246 finalAttachmentsPtr);
Geoff Lang57ce9ea2016-11-24 12:03:14 -0500247 }
Geoff Lang64a72442015-04-01 14:43:11 -0400248
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400249 return angle::Result::Continue();
Geoff Langf9a6f082015-01-22 13:32:49 -0500250}
251
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400252angle::Result FramebufferGL::invalidateSub(const gl::Context *context,
253 size_t count,
254 const GLenum *attachments,
255 const gl::Rectangle &area)
Geoff Langf9a6f082015-01-22 13:32:49 -0500256{
Geoff Lang005a7012017-03-27 13:17:34 -0400257
258 const GLenum *finalAttachmentsPtr = attachments;
259
260 std::vector<GLenum> modifiedAttachments;
261 if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments))
262 {
263 finalAttachmentsPtr = modifiedAttachments.data();
264 }
265
Geoff Lang61107632018-05-09 11:32:46 -0400266 const FunctionsGL *functions = GetFunctionsGL(context);
267 StateManagerGL *stateManager = GetStateManagerGL(context);
268
Jamie Madill231c7f52017-04-26 13:45:37 -0400269 // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is
270 // available.
Geoff Lang61107632018-05-09 11:32:46 -0400271 if (functions->invalidateSubFramebuffer)
Geoff Lang64a72442015-04-01 14:43:11 -0400272 {
Geoff Lang61107632018-05-09 11:32:46 -0400273 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
274 functions->invalidateSubFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count),
275 finalAttachmentsPtr, area.x, area.y, area.width,
276 area.height);
Geoff Lang64a72442015-04-01 14:43:11 -0400277 }
278
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400279 return angle::Result::Continue();
Geoff Langf9a6f082015-01-22 13:32:49 -0500280}
281
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400282angle::Result FramebufferGL::clear(const gl::Context *context, GLbitfield mask)
Geoff Langf9a6f082015-01-22 13:32:49 -0500283{
Geoff Lang61107632018-05-09 11:32:46 -0400284 const FunctionsGL *functions = GetFunctionsGL(context);
285 StateManagerGL *stateManager = GetStateManagerGL(context);
286
Frank Henigman308d7452017-02-15 22:51:21 -0500287 syncClearState(context, mask);
Geoff Lang61107632018-05-09 11:32:46 -0400288 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Martin Radevb0761932017-07-25 17:42:25 +0300289
Martin Radev61e710b2017-09-05 11:59:52 +0300290 if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled()))
Martin Radevb0761932017-07-25 17:42:25 +0300291 {
Geoff Lang61107632018-05-09 11:32:46 -0400292 functions->clear(mask);
Martin Radevb0761932017-07-25 17:42:25 +0300293 }
294 else
295 {
Geoff Lang61107632018-05-09 11:32:46 -0400296 ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
297 multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(),
298 ClearMultiviewGL::ClearCommandType::Clear, mask,
299 GL_NONE, 0, nullptr, 0.0f, 0);
Martin Radevb0761932017-07-25 17:42:25 +0300300 }
Geoff Lang4ad17092015-03-10 16:47:44 -0400301
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400302 return angle::Result::Continue();
Geoff Lang4ad17092015-03-10 16:47:44 -0400303}
304
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400305angle::Result FramebufferGL::clearBufferfv(const gl::Context *context,
306 GLenum buffer,
307 GLint drawbuffer,
308 const GLfloat *values)
Geoff Langf9a6f082015-01-22 13:32:49 -0500309{
Geoff Lang61107632018-05-09 11:32:46 -0400310 const FunctionsGL *functions = GetFunctionsGL(context);
311 StateManagerGL *stateManager = GetStateManagerGL(context);
312
Frank Henigman308d7452017-02-15 22:51:21 -0500313 syncClearBufferState(context, buffer, drawbuffer);
Geoff Lang61107632018-05-09 11:32:46 -0400314 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Martin Radevb0761932017-07-25 17:42:25 +0300315
Martin Radev61e710b2017-09-05 11:59:52 +0300316 if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled()))
Martin Radevb0761932017-07-25 17:42:25 +0300317 {
Geoff Lang61107632018-05-09 11:32:46 -0400318 functions->clearBufferfv(buffer, drawbuffer, values);
Martin Radevb0761932017-07-25 17:42:25 +0300319 }
320 else
321 {
Geoff Lang61107632018-05-09 11:32:46 -0400322 ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
323 multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(),
324 ClearMultiviewGL::ClearCommandType::ClearBufferfv,
325 static_cast<GLbitfield>(0u), buffer, drawbuffer,
326 reinterpret_cast<const uint8_t *>(values), 0.0f, 0);
Martin Radevb0761932017-07-25 17:42:25 +0300327 }
Geoff Lang4ad17092015-03-10 16:47:44 -0400328
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400329 return angle::Result::Continue();
Geoff Langf9a6f082015-01-22 13:32:49 -0500330}
331
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400332angle::Result FramebufferGL::clearBufferuiv(const gl::Context *context,
333 GLenum buffer,
334 GLint drawbuffer,
335 const GLuint *values)
Geoff Langf9a6f082015-01-22 13:32:49 -0500336{
Geoff Lang61107632018-05-09 11:32:46 -0400337 const FunctionsGL *functions = GetFunctionsGL(context);
338 StateManagerGL *stateManager = GetStateManagerGL(context);
339
Frank Henigman308d7452017-02-15 22:51:21 -0500340 syncClearBufferState(context, buffer, drawbuffer);
Geoff Lang61107632018-05-09 11:32:46 -0400341 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Martin Radevb0761932017-07-25 17:42:25 +0300342
Martin Radev61e710b2017-09-05 11:59:52 +0300343 if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled()))
Martin Radevb0761932017-07-25 17:42:25 +0300344 {
Geoff Lang61107632018-05-09 11:32:46 -0400345 functions->clearBufferuiv(buffer, drawbuffer, values);
Martin Radevb0761932017-07-25 17:42:25 +0300346 }
347 else
348 {
Geoff Lang61107632018-05-09 11:32:46 -0400349 ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
350 multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(),
351 ClearMultiviewGL::ClearCommandType::ClearBufferuiv,
352 static_cast<GLbitfield>(0u), buffer, drawbuffer,
353 reinterpret_cast<const uint8_t *>(values), 0.0f, 0);
Martin Radevb0761932017-07-25 17:42:25 +0300354 }
Geoff Lang4ad17092015-03-10 16:47:44 -0400355
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400356 return angle::Result::Continue();
Geoff Langf9a6f082015-01-22 13:32:49 -0500357}
358
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400359angle::Result FramebufferGL::clearBufferiv(const gl::Context *context,
360 GLenum buffer,
361 GLint drawbuffer,
362 const GLint *values)
Geoff Langf9a6f082015-01-22 13:32:49 -0500363{
Geoff Lang61107632018-05-09 11:32:46 -0400364 const FunctionsGL *functions = GetFunctionsGL(context);
365 StateManagerGL *stateManager = GetStateManagerGL(context);
366
Frank Henigman308d7452017-02-15 22:51:21 -0500367 syncClearBufferState(context, buffer, drawbuffer);
Geoff Lang61107632018-05-09 11:32:46 -0400368 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Martin Radevb0761932017-07-25 17:42:25 +0300369
Martin Radev61e710b2017-09-05 11:59:52 +0300370 if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled()))
Martin Radevb0761932017-07-25 17:42:25 +0300371 {
Geoff Lang61107632018-05-09 11:32:46 -0400372 functions->clearBufferiv(buffer, drawbuffer, values);
Martin Radevb0761932017-07-25 17:42:25 +0300373 }
374 else
375 {
Geoff Lang61107632018-05-09 11:32:46 -0400376 ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
377 multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(),
378 ClearMultiviewGL::ClearCommandType::ClearBufferiv,
379 static_cast<GLbitfield>(0u), buffer, drawbuffer,
380 reinterpret_cast<const uint8_t *>(values), 0.0f, 0);
Martin Radevb0761932017-07-25 17:42:25 +0300381 }
Geoff Lang4ad17092015-03-10 16:47:44 -0400382
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400383 return angle::Result::Continue();
Geoff Langf9a6f082015-01-22 13:32:49 -0500384}
385
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400386angle::Result FramebufferGL::clearBufferfi(const gl::Context *context,
387 GLenum buffer,
388 GLint drawbuffer,
389 GLfloat depth,
390 GLint stencil)
Geoff Langf9a6f082015-01-22 13:32:49 -0500391{
Geoff Lang61107632018-05-09 11:32:46 -0400392 const FunctionsGL *functions = GetFunctionsGL(context);
393 StateManagerGL *stateManager = GetStateManagerGL(context);
394
Frank Henigman308d7452017-02-15 22:51:21 -0500395 syncClearBufferState(context, buffer, drawbuffer);
Geoff Lang61107632018-05-09 11:32:46 -0400396 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Martin Radevb0761932017-07-25 17:42:25 +0300397
Martin Radev61e710b2017-09-05 11:59:52 +0300398 if (!RequiresMultiviewClear(mState, context->getGLState().isScissorTestEnabled()))
Martin Radevb0761932017-07-25 17:42:25 +0300399 {
Geoff Lang61107632018-05-09 11:32:46 -0400400 functions->clearBufferfi(buffer, drawbuffer, depth, stencil);
Martin Radevb0761932017-07-25 17:42:25 +0300401 }
Martin Radev5e424fa2017-08-09 16:25:36 +0300402 else
403 {
Geoff Lang61107632018-05-09 11:32:46 -0400404 ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
405 multiviewClearer->clearMultiviewFBO(mState, context->getGLState().getScissor(),
406 ClearMultiviewGL::ClearCommandType::ClearBufferfi,
407 static_cast<GLbitfield>(0u), buffer, drawbuffer,
408 nullptr, depth, stencil);
Martin Radev5e424fa2017-08-09 16:25:36 +0300409 }
Geoff Lang4ad17092015-03-10 16:47:44 -0400410
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400411 return angle::Result::Continue();
Geoff Langf9a6f082015-01-22 13:32:49 -0500412}
413
Jamie Madill4928b7c2017-06-20 12:57:39 -0400414GLenum FramebufferGL::getImplementationColorReadFormat(const gl::Context *context) const
Geoff Langf9a6f082015-01-22 13:32:49 -0500415{
Jamie Madilla3944d42016-07-22 22:13:26 -0400416 const auto *readAttachment = mState.getReadAttachment();
417 const Format &format = readAttachment->getFormat();
Geoff Langf607c602016-09-21 11:46:48 -0400418 return format.info->getReadPixelsFormat();
Geoff Langf9a6f082015-01-22 13:32:49 -0500419}
420
Jamie Madill4928b7c2017-06-20 12:57:39 -0400421GLenum FramebufferGL::getImplementationColorReadType(const gl::Context *context) const
Geoff Langf9a6f082015-01-22 13:32:49 -0500422{
Jamie Madilla3944d42016-07-22 22:13:26 -0400423 const auto *readAttachment = mState.getReadAttachment();
424 const Format &format = readAttachment->getFormat();
Geoff Langc71ea662017-09-26 17:06:02 -0400425 return format.info->getReadPixelsType(context->getClientVersion());
Geoff Langf9a6f082015-01-22 13:32:49 -0500426}
427
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400428angle::Result FramebufferGL::readPixels(const gl::Context *context,
429 const gl::Rectangle &area,
430 GLenum format,
431 GLenum type,
432 void *pixels)
Geoff Langf9a6f082015-01-22 13:32:49 -0500433{
Jamie Madille39e8f42018-10-05 08:17:38 -0400434 ContextGL *contextGL = GetImplAs<ContextGL>(context);
Geoff Lang61107632018-05-09 11:32:46 -0400435 const FunctionsGL *functions = GetFunctionsGL(context);
436 StateManagerGL *stateManager = GetStateManagerGL(context);
437 const WorkaroundsGL &workarounds = GetWorkaroundsGL(context);
438
Frank Henigmanfa36c332017-06-09 18:44:45 -0400439 // Clip read area to framebuffer.
440 const gl::Extents fbSize = getState().getReadAttachment()->getSize();
441 const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height);
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400442 gl::Rectangle clippedArea;
443 if (!ClipRectangle(area, fbRect, &clippedArea))
Frank Henigmanfa36c332017-06-09 18:44:45 -0400444 {
445 // nothing to read
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400446 return angle::Result::Continue();
Frank Henigmanfa36c332017-06-09 18:44:45 -0400447 }
448
Corentin Wallezcda6af12017-10-30 19:20:37 -0400449 PixelPackState packState = context->getGLState().getPackState();
Corentin Wallez336129f2017-10-17 15:55:40 -0400450 const gl::Buffer *packBuffer =
451 context->getGLState().getTargetBuffer(gl::BufferBinding::PixelPack);
Jamie Madill87de3622015-03-16 10:41:44 -0400452
Geoff Langf607c602016-09-21 11:46:48 -0400453 nativegl::ReadPixelsFormat readPixelsFormat =
Geoff Lang61107632018-05-09 11:32:46 -0400454 nativegl::GetReadPixelsFormat(functions, workarounds, format, type);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400455 GLenum readFormat = readPixelsFormat.format;
456 GLenum readType = readPixelsFormat.type;
Geoff Lang4ad17092015-03-10 16:47:44 -0400457
Geoff Lang61107632018-05-09 11:32:46 -0400458 stateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferID);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400459
Geoff Lang61107632018-05-09 11:32:46 -0400460 bool useOverlappingRowsWorkaround = workarounds.packOverlappingRowsSeparatelyPackBuffer &&
Corentin Wallezcda6af12017-10-30 19:20:37 -0400461 packBuffer && packState.rowLength != 0 &&
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400462 packState.rowLength < clippedArea.width;
Frank Henigmanfa36c332017-06-09 18:44:45 -0400463
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400464 GLubyte *outPtr = static_cast<GLubyte *>(pixels);
465 int leftClip = clippedArea.x - area.x;
466 int topClip = clippedArea.y - area.y;
Frank Henigmanfa36c332017-06-09 18:44:45 -0400467 if (leftClip || topClip)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400468 {
Frank Henigmanfa36c332017-06-09 18:44:45 -0400469 // Adjust destination to match portion clipped off left and/or top.
470 const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(readFormat, readType);
471
472 GLuint rowBytes = 0;
Jamie Madille39e8f42018-10-05 08:17:38 -0400473 ANGLE_CHECK_GL_MATH(contextGL,
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400474 glFormat.computeRowPitch(readType, area.width, packState.alignment,
Jamie Madille39e8f42018-10-05 08:17:38 -0400475 packState.rowLength, &rowBytes));
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400476 outPtr += leftClip * glFormat.pixelBytes + topClip * rowBytes;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400477 }
478
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400479 if (packState.rowLength == 0 && clippedArea.width != area.width)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400480 {
Frank Henigmanfa36c332017-06-09 18:44:45 -0400481 // No rowLength was specified so it will derive from read width, but clipping changed the
482 // read width. Use the original width so we fill the user's buffer as they intended.
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400483 packState.rowLength = area.width;
Frank Henigmanfa36c332017-06-09 18:44:45 -0400484 }
Corentin Wallez886de362016-09-27 10:49:35 -0400485
Frank Henigmanfa36c332017-06-09 18:44:45 -0400486 // We want to use rowLength, but that might not be supported.
487 bool cannotSetDesiredRowLength =
488 packState.rowLength && !GetImplAs<ContextGL>(context)->getNativeExtensions().packSubimage;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400489
Frank Henigmanfa36c332017-06-09 18:44:45 -0400490 if (cannotSetDesiredRowLength || useOverlappingRowsWorkaround)
491 {
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400492 return readPixelsRowByRow(context, clippedArea, readFormat, readType, packState, outPtr);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400493 }
Jamie Madill13951342018-09-30 15:24:28 -0400494
495 bool useLastRowPaddingWorkaround = false;
496 if (workarounds.packLastRowSeparatelyForPaddingInclusion)
Frank Henigmanfa36c332017-06-09 18:44:45 -0400497 {
Jamie Madille39e8f42018-10-05 08:17:38 -0400498 ANGLE_TRY(ShouldApplyLastRowPaddingWorkaround(
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400499 contextGL, gl::Extents(clippedArea.width, clippedArea.height, 1), packState, packBuffer,
500 readFormat, readType, false, outPtr, &useLastRowPaddingWorkaround));
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400501 }
502
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400503 return readPixelsAllAtOnce(context, clippedArea, readFormat, readType, packState, outPtr,
Jamie Madill13951342018-09-30 15:24:28 -0400504 useLastRowPaddingWorkaround);
Geoff Langf9a6f082015-01-22 13:32:49 -0500505}
506
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400507angle::Result FramebufferGL::blit(const gl::Context *context,
508 const gl::Rectangle &sourceArea,
509 const gl::Rectangle &destArea,
510 GLbitfield mask,
511 GLenum filter)
Geoff Langf9a6f082015-01-22 13:32:49 -0500512{
Geoff Lang61107632018-05-09 11:32:46 -0400513 const FunctionsGL *functions = GetFunctionsGL(context);
514 StateManagerGL *stateManager = GetStateManagerGL(context);
515
Jamie Madill231c7f52017-04-26 13:45:37 -0400516 const Framebuffer *sourceFramebuffer = context->getGLState().getReadFramebuffer();
517 const Framebuffer *destFramebuffer = context->getGLState().getDrawFramebuffer();
Geoff Lang4ad17092015-03-10 16:47:44 -0400518
Corentin Wallez6898b352016-11-10 11:41:15 -0500519 const FramebufferAttachment *colorReadAttachment = sourceFramebuffer->getReadColorbuffer();
Corentin Wallez4596a762016-12-20 14:50:18 -0500520
521 GLsizei readAttachmentSamples = 0;
522 if (colorReadAttachment != nullptr)
523 {
524 readAttachmentSamples = colorReadAttachment->getSamples();
525 }
Corentin Wallez6898b352016-11-10 11:41:15 -0500526
Corentin Wallez26a717b2016-09-27 08:45:42 -0700527 bool needManualColorBlit = false;
528
Corentin Wallez6898b352016-11-10 11:41:15 -0500529 // TODO(cwallez) when the filter is LINEAR and both source and destination are SRGB, we
530 // could avoid doing a manual blit.
531
532 // Prior to OpenGL 4.4 BlitFramebuffer (section 18.3.1 of GL 4.3 core profile) reads:
533 // When values are taken from the read buffer, no linearization is performed, even
534 // if the format of the buffer is SRGB.
535 // Starting from OpenGL 4.4 (section 18.3.1) it reads:
536 // When values are taken from the read buffer, if FRAMEBUFFER_SRGB is enabled and the
537 // value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the framebuffer attachment
538 // corresponding to the read buffer is SRGB, the red, green, and blue components are
539 // converted from the non-linear sRGB color space according [...].
Corentin Wallez26a717b2016-09-27 08:45:42 -0700540 {
Jamie Madill231c7f52017-04-26 13:45:37 -0400541 bool sourceSRGB =
542 colorReadAttachment != nullptr && colorReadAttachment->getColorEncoding() == GL_SRGB;
Corentin Wallez6898b352016-11-10 11:41:15 -0500543 needManualColorBlit =
Geoff Lang61107632018-05-09 11:32:46 -0400544 needManualColorBlit || (sourceSRGB && functions->isAtMostGL(gl::Version(4, 3)));
Corentin Wallez6898b352016-11-10 11:41:15 -0500545 }
Corentin Wallez26a717b2016-09-27 08:45:42 -0700546
Corentin Wallez6898b352016-11-10 11:41:15 -0500547 // Prior to OpenGL 4.2 BlitFramebuffer (section 4.3.2 of GL 4.1 core profile) reads:
548 // Blit operations bypass the fragment pipeline. The only fragment operations which
549 // affect a blit are the pixel ownership test and scissor test.
550 // Starting from OpenGL 4.2 (section 4.3.2) it reads:
551 // When values are written to the draw buffers, blit operations bypass the fragment
552 // pipeline. The only fragment operations which affect a blit are the pixel ownership
553 // test, the scissor test and sRGB conversion.
554 if (!needManualColorBlit)
555 {
556 bool destSRGB = false;
557 for (size_t i = 0; i < destFramebuffer->getDrawbufferStateCount(); ++i)
Corentin Wallez26a717b2016-09-27 08:45:42 -0700558 {
Corentin Wallez6898b352016-11-10 11:41:15 -0500559 const FramebufferAttachment *attachment = destFramebuffer->getDrawBuffer(i);
560 if (attachment && attachment->getColorEncoding() == GL_SRGB)
Corentin Wallez26a717b2016-09-27 08:45:42 -0700561 {
Corentin Wallez6898b352016-11-10 11:41:15 -0500562 destSRGB = true;
563 break;
Corentin Wallez26a717b2016-09-27 08:45:42 -0700564 }
Corentin Wallez26a717b2016-09-27 08:45:42 -0700565 }
Corentin Wallez6898b352016-11-10 11:41:15 -0500566
567 needManualColorBlit =
Geoff Lang61107632018-05-09 11:32:46 -0400568 needManualColorBlit || (destSRGB && functions->isAtMostGL(gl::Version(4, 1)));
Corentin Wallez26a717b2016-09-27 08:45:42 -0700569 }
570
571 // Enable FRAMEBUFFER_SRGB if needed
Geoff Lang61107632018-05-09 11:32:46 -0400572 stateManager->setFramebufferSRGBEnabledForFramebuffer(context, true, this);
Corentin Wallez26a717b2016-09-27 08:45:42 -0700573
574 GLenum blitMask = mask;
Corentin Wallez6898b352016-11-10 11:41:15 -0500575 if (needManualColorBlit && (mask & GL_COLOR_BUFFER_BIT) && readAttachmentSamples <= 1)
Corentin Wallez26a717b2016-09-27 08:45:42 -0700576 {
Geoff Lang61107632018-05-09 11:32:46 -0400577 BlitGL *blitter = GetBlitGL(context);
Jamie Madille39e8f42018-10-05 08:17:38 -0400578 ANGLE_TRY(blitter->blitColorBufferWithShader(context, sourceFramebuffer, destFramebuffer,
579 sourceArea, destArea, filter));
Corentin Wallez26a717b2016-09-27 08:45:42 -0700580 blitMask &= ~GL_COLOR_BUFFER_BIT;
581 }
582
583 if (blitMask == 0)
584 {
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400585 return angle::Result::Continue();
Corentin Wallez26a717b2016-09-27 08:45:42 -0700586 }
587
588 const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer);
Geoff Lang61107632018-05-09 11:32:46 -0400589 stateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID());
590 stateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID);
Geoff Lang4ad17092015-03-10 16:47:44 -0400591
Geoff Lang61107632018-05-09 11:32:46 -0400592 functions->blitFramebuffer(sourceArea.x, sourceArea.y, sourceArea.x1(), sourceArea.y1(),
593 destArea.x, destArea.y, destArea.x1(), destArea.y1(), blitMask,
594 filter);
Geoff Lang4ad17092015-03-10 16:47:44 -0400595
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400596 return angle::Result::Continue();
Geoff Langf9a6f082015-01-22 13:32:49 -0500597}
598
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400599angle::Result FramebufferGL::getSamplePosition(const gl::Context *context,
600 size_t index,
601 GLfloat *xy) const
JiangYizhoubddc46b2016-12-09 09:50:51 +0800602{
Geoff Lang61107632018-05-09 11:32:46 -0400603 const FunctionsGL *functions = GetFunctionsGL(context);
604 StateManagerGL *stateManager = GetStateManagerGL(context);
605
606 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
607 functions->getMultisamplefv(GL_SAMPLE_POSITION, static_cast<GLuint>(index), xy);
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400608 return angle::Result::Continue();
JiangYizhoubddc46b2016-12-09 09:50:51 +0800609}
610
Kenneth Russellce8602a2017-10-03 18:23:08 -0700611bool FramebufferGL::checkStatus(const gl::Context *context) const
Geoff Langf9a6f082015-01-22 13:32:49 -0500612{
Geoff Lang61107632018-05-09 11:32:46 -0400613 const FunctionsGL *functions = GetFunctionsGL(context);
614 StateManagerGL *stateManager = GetStateManagerGL(context);
615
616 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
617 GLenum status = functions->checkFramebufferStatus(GL_FRAMEBUFFER);
Jamie Madillcc86d642015-11-24 13:00:07 -0500618 if (status != GL_FRAMEBUFFER_COMPLETE)
619 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500620 WARN() << "GL framebuffer returned incomplete.";
Jamie Madillcc86d642015-11-24 13:00:07 -0500621 }
622 return (status == GL_FRAMEBUFFER_COMPLETE);
Geoff Lang4ad17092015-03-10 16:47:44 -0400623}
624
Jamie Madill6f755b22018-10-09 12:48:54 -0400625angle::Result FramebufferGL::syncState(const gl::Context *context,
626 const gl::Framebuffer::DirtyBits &dirtyBits)
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500627{
628 // Don't need to sync state for the default FBO.
629 if (mIsDefault)
630 {
Jamie Madill6f755b22018-10-09 12:48:54 -0400631 return angle::Result::Continue();
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500632 }
633
Geoff Lang61107632018-05-09 11:32:46 -0400634 const FunctionsGL *functions = GetFunctionsGL(context);
635 StateManagerGL *stateManager = GetStateManagerGL(context);
636
637 stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500638
Martin Radev7c2e6a92017-08-28 11:13:16 +0300639 // A pointer to one of the attachments for which the texture or the render buffer is not zero.
640 const FramebufferAttachment *attachment = nullptr;
Martin Radev878c8b12017-07-28 09:51:04 +0300641
Jamie Madill6de51852017-04-12 09:53:01 -0400642 for (auto dirtyBit : dirtyBits)
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500643 {
644 switch (dirtyBit)
645 {
646 case Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT:
Martin Radev7c2e6a92017-08-28 11:13:16 +0300647 {
648 const FramebufferAttachment *newAttachment = mState.getDepthAttachment();
Geoff Lang61107632018-05-09 11:32:46 -0400649 BindFramebufferAttachment(functions, GL_DEPTH_ATTACHMENT, newAttachment);
Martin Radev7c2e6a92017-08-28 11:13:16 +0300650 if (newAttachment)
651 {
652 attachment = newAttachment;
653 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500654 break;
Martin Radev7c2e6a92017-08-28 11:13:16 +0300655 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500656 case Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT:
Martin Radev7c2e6a92017-08-28 11:13:16 +0300657 {
658 const FramebufferAttachment *newAttachment = mState.getStencilAttachment();
Geoff Lang61107632018-05-09 11:32:46 -0400659 BindFramebufferAttachment(functions, GL_STENCIL_ATTACHMENT, newAttachment);
Martin Radev7c2e6a92017-08-28 11:13:16 +0300660 if (newAttachment)
661 {
662 attachment = newAttachment;
663 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500664 break;
Martin Radev7c2e6a92017-08-28 11:13:16 +0300665 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500666 case Framebuffer::DIRTY_BIT_DRAW_BUFFERS:
667 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400668 const auto &drawBuffers = mState.getDrawBufferStates();
Geoff Lang61107632018-05-09 11:32:46 -0400669 functions->drawBuffers(static_cast<GLsizei>(drawBuffers.size()),
670 drawBuffers.data());
Corentin Walleze7557742017-06-01 13:09:57 -0400671 mAppliedEnabledDrawBuffers = mState.getEnabledDrawBuffers();
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500672 break;
673 }
674 case Framebuffer::DIRTY_BIT_READ_BUFFER:
Geoff Lang61107632018-05-09 11:32:46 -0400675 functions->readBuffer(mState.getReadBufferState());
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500676 break;
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800677 case Framebuffer::DIRTY_BIT_DEFAULT_WIDTH:
Geoff Lang61107632018-05-09 11:32:46 -0400678 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_WIDTH,
679 mState.getDefaultWidth());
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800680 break;
681 case Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT:
Geoff Lang61107632018-05-09 11:32:46 -0400682 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_HEIGHT,
683 mState.getDefaultHeight());
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800684 break;
685 case Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES:
Geoff Lang61107632018-05-09 11:32:46 -0400686 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES,
687 mState.getDefaultSamples());
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800688 break;
689 case Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS:
Geoff Lang61107632018-05-09 11:32:46 -0400690 functions->framebufferParameteri(
Geoff Lang92019432017-11-20 13:09:34 -0500691 GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS,
692 gl::ConvertToGLBoolean(mState.getDefaultFixedSampleLocations()));
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800693 break;
Jiawei Shaob1e91382018-05-17 14:33:55 +0800694 case Framebuffer::DIRTY_BIT_DEFAULT_LAYERS:
695 functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_LAYERS_EXT,
696 mState.getDefaultLayers());
697 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500698 default:
699 {
700 ASSERT(Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0 &&
701 dirtyBit < Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX);
702 size_t index =
703 static_cast<size_t>(dirtyBit - Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0);
Martin Radev7c2e6a92017-08-28 11:13:16 +0300704 const FramebufferAttachment *newAttachment = mState.getColorAttachment(index);
705 BindFramebufferAttachment(
Geoff Lang61107632018-05-09 11:32:46 -0400706 functions, static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + index), newAttachment);
Martin Radev7c2e6a92017-08-28 11:13:16 +0300707 if (newAttachment)
708 {
709 attachment = newAttachment;
710 }
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500711 break;
712 }
713 }
714 }
Martin Radev878c8b12017-07-28 09:51:04 +0300715
Jamie Madillf38cb6d2018-10-22 11:53:40 -0400716 if (attachment && mState.id() == context->getGLState().getDrawFramebuffer()->id())
Martin Radev878c8b12017-07-28 09:51:04 +0300717 {
Martin Radev7c2e6a92017-08-28 11:13:16 +0300718 const bool isSideBySide =
719 (attachment->getMultiviewLayout() == GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE);
Geoff Lang61107632018-05-09 11:32:46 -0400720 stateManager->setSideBySide(isSideBySide);
Geoff Lang61107632018-05-09 11:32:46 -0400721 stateManager->updateMultiviewBaseViewLayerIndexUniform(context->getGLState().getProgram(),
722 getState());
Martin Radev878c8b12017-07-28 09:51:04 +0300723 }
Jamie Madill19fa1c62018-03-08 09:47:21 -0500724
Jamie Madill6f755b22018-10-09 12:48:54 -0400725 return angle::Result::Continue();
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500726}
727
Geoff Lang4ad17092015-03-10 16:47:44 -0400728GLuint FramebufferGL::getFramebufferID() const
729{
730 return mFramebufferID;
Geoff Langf9a6f082015-01-22 13:32:49 -0500731}
732
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700733bool FramebufferGL::isDefault() const
Geoff Langafd7f0a2015-09-09 15:33:31 -0400734{
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700735 return mIsDefault;
Geoff Langafd7f0a2015-09-09 15:33:31 -0400736}
737
Jamie Madillf38cb6d2018-10-22 11:53:40 -0400738void FramebufferGL::maskOutInactiveOutputDrawBuffersImpl(const gl::Context *context,
739 DrawBufferMask targetAppliedDrawBuffers)
Corentin Walleze7557742017-06-01 13:09:57 -0400740{
Geoff Lang61107632018-05-09 11:32:46 -0400741
Jamie Madillf38cb6d2018-10-22 11:53:40 -0400742 ASSERT(mAppliedEnabledDrawBuffers != targetAppliedDrawBuffers);
743 mAppliedEnabledDrawBuffers = targetAppliedDrawBuffers;
744
745 const auto &stateDrawBuffers = mState.getDrawBufferStates();
746 GLsizei drawBufferCount = static_cast<GLsizei>(stateDrawBuffers.size());
747 ASSERT(drawBufferCount <= IMPLEMENTATION_MAX_DRAW_BUFFERS);
748
749 GLenum drawBuffers[IMPLEMENTATION_MAX_DRAW_BUFFERS];
750 for (GLenum i = 0; static_cast<int>(i) < drawBufferCount; ++i)
Corentin Walleze7557742017-06-01 13:09:57 -0400751 {
Jamie Madillf38cb6d2018-10-22 11:53:40 -0400752 drawBuffers[i] = targetAppliedDrawBuffers[i] ? stateDrawBuffers[i] : GL_NONE;
Corentin Walleze7557742017-06-01 13:09:57 -0400753 }
Jamie Madillf38cb6d2018-10-22 11:53:40 -0400754
755 const FunctionsGL *functions = GetFunctionsGL(context);
756 StateManagerGL *stateManager = GetStateManagerGL(context);
757
758 ASSERT(stateManager->getFramebufferID(angle::FramebufferBindingDraw) == mFramebufferID);
759 functions->drawBuffers(drawBufferCount, drawBuffers);
Corentin Walleze7557742017-06-01 13:09:57 -0400760}
761
Jamie Madillc564c072017-06-01 12:45:42 -0400762void FramebufferGL::syncClearState(const gl::Context *context, GLbitfield mask)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400763{
Geoff Lang61107632018-05-09 11:32:46 -0400764 const FunctionsGL *functions = GetFunctionsGL(context);
765
766 if (functions->standard == STANDARD_GL_DESKTOP)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400767 {
Geoff Lang61107632018-05-09 11:32:46 -0400768 StateManagerGL *stateManager = GetStateManagerGL(context);
769 const WorkaroundsGL &workarounds = GetWorkaroundsGL(context);
770
771 if (workarounds.doesSRGBClearsOnLinearFramebufferAttachments &&
Frank Henigmana3d333c2016-03-22 22:09:14 -0400772 (mask & GL_COLOR_BUFFER_BIT) != 0 && !mIsDefault)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400773 {
Corentin Wallez26a717b2016-09-27 08:45:42 -0700774 bool hasSRGBAttachment = false;
Jamie Madill48ef11b2016-04-27 15:21:52 -0400775 for (const auto &attachment : mState.getColorAttachments())
Geoff Langafd7f0a2015-09-09 15:33:31 -0400776 {
Frank Henigmana3d333c2016-03-22 22:09:14 -0400777 if (attachment.isAttached() && attachment.getColorEncoding() == GL_SRGB)
778 {
Corentin Wallez26a717b2016-09-27 08:45:42 -0700779 hasSRGBAttachment = true;
Frank Henigmana3d333c2016-03-22 22:09:14 -0400780 break;
781 }
Geoff Langafd7f0a2015-09-09 15:33:31 -0400782 }
Geoff Langafd7f0a2015-09-09 15:33:31 -0400783
Geoff Lang61107632018-05-09 11:32:46 -0400784 stateManager->setFramebufferSRGBEnabled(context, hasSRGBAttachment);
Frank Henigmana3d333c2016-03-22 22:09:14 -0400785 }
786 else
787 {
Geoff Lang61107632018-05-09 11:32:46 -0400788 stateManager->setFramebufferSRGBEnabled(context, !mIsDefault);
Frank Henigmana3d333c2016-03-22 22:09:14 -0400789 }
Geoff Langafd7f0a2015-09-09 15:33:31 -0400790 }
791}
792
Jamie Madillc564c072017-06-01 12:45:42 -0400793void FramebufferGL::syncClearBufferState(const gl::Context *context,
794 GLenum buffer,
795 GLint drawBuffer)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400796{
Geoff Lang61107632018-05-09 11:32:46 -0400797 const FunctionsGL *functions = GetFunctionsGL(context);
798
799 if (functions->standard == STANDARD_GL_DESKTOP)
Geoff Langafd7f0a2015-09-09 15:33:31 -0400800 {
Geoff Lang61107632018-05-09 11:32:46 -0400801 StateManagerGL *stateManager = GetStateManagerGL(context);
802 const WorkaroundsGL &workarounds = GetWorkaroundsGL(context);
803
804 if (workarounds.doesSRGBClearsOnLinearFramebufferAttachments && buffer == GL_COLOR &&
Geoff Langafd7f0a2015-09-09 15:33:31 -0400805 !mIsDefault)
806 {
807 // If doing a clear on a color buffer, set SRGB blend enabled only if the color buffer
808 // is an SRGB format.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400809 const auto &drawbufferState = mState.getDrawBufferStates();
810 const auto &colorAttachments = mState.getColorAttachments();
Geoff Langafd7f0a2015-09-09 15:33:31 -0400811
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500812 const FramebufferAttachment *attachment = nullptr;
Geoff Langafd7f0a2015-09-09 15:33:31 -0400813 if (drawbufferState[drawBuffer] >= GL_COLOR_ATTACHMENT0 &&
814 drawbufferState[drawBuffer] < GL_COLOR_ATTACHMENT0 + colorAttachments.size())
815 {
816 size_t attachmentIdx =
817 static_cast<size_t>(drawbufferState[drawBuffer] - GL_COLOR_ATTACHMENT0);
818 attachment = &colorAttachments[attachmentIdx];
819 }
820
821 if (attachment != nullptr)
822 {
Geoff Lang61107632018-05-09 11:32:46 -0400823 stateManager->setFramebufferSRGBEnabled(context,
824 attachment->getColorEncoding() == GL_SRGB);
Geoff Langafd7f0a2015-09-09 15:33:31 -0400825 }
826 }
827 else
828 {
Geoff Lang61107632018-05-09 11:32:46 -0400829 stateManager->setFramebufferSRGBEnabled(context, !mIsDefault);
Geoff Langafd7f0a2015-09-09 15:33:31 -0400830 }
831 }
832}
Geoff Lang005a7012017-03-27 13:17:34 -0400833
834bool FramebufferGL::modifyInvalidateAttachmentsForEmulatedDefaultFBO(
835 size_t count,
836 const GLenum *attachments,
837 std::vector<GLenum> *modifiedAttachments) const
838{
839 bool needsModification = mIsDefault && mFramebufferID != 0;
840 if (!needsModification)
841 {
842 return false;
843 }
844
845 modifiedAttachments->resize(count);
846 for (size_t i = 0; i < count; i++)
847 {
848 switch (attachments[i])
849 {
850 case GL_COLOR:
851 (*modifiedAttachments)[i] = GL_COLOR_ATTACHMENT0;
852 break;
853
854 case GL_DEPTH:
855 (*modifiedAttachments)[i] = GL_DEPTH_ATTACHMENT;
856 break;
857
858 case GL_STENCIL:
859 (*modifiedAttachments)[i] = GL_STENCIL_ATTACHMENT;
860 break;
861
862 default:
863 UNREACHABLE();
864 break;
865 }
866 }
867
868 return true;
869}
870
Jamie Madille39e8f42018-10-05 08:17:38 -0400871angle::Result FramebufferGL::readPixelsRowByRow(const gl::Context *context,
872 const gl::Rectangle &area,
873 GLenum format,
874 GLenum type,
875 const gl::PixelPackState &pack,
876 GLubyte *pixels) const
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400877{
Jamie Madille39e8f42018-10-05 08:17:38 -0400878 ContextGL *contextGL = GetImplAs<ContextGL>(context);
Geoff Lang61107632018-05-09 11:32:46 -0400879 const FunctionsGL *functions = GetFunctionsGL(context);
880 StateManagerGL *stateManager = GetStateManagerGL(context);
881
Geoff Langca271392017-04-05 12:30:00 -0400882 const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400883
884 GLuint rowBytes = 0;
Jamie Madille39e8f42018-10-05 08:17:38 -0400885 ANGLE_CHECK_GL_MATH(contextGL, glFormat.computeRowPitch(type, area.width, pack.alignment,
886 pack.rowLength, &rowBytes));
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400887 GLuint skipBytes = 0;
Jamie Madille39e8f42018-10-05 08:17:38 -0400888 ANGLE_CHECK_GL_MATH(contextGL,
889 glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes));
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400890
891 gl::PixelPackState directPack;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400892 directPack.alignment = 1;
Geoff Lang61107632018-05-09 11:32:46 -0400893 stateManager->setPixelPackState(directPack);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400894
Frank Henigmanfa36c332017-06-09 18:44:45 -0400895 pixels += skipBytes;
896 for (GLint y = area.y; y < area.y + area.height; ++y)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400897 {
Geoff Lang61107632018-05-09 11:32:46 -0400898 functions->readPixels(area.x, y, area.width, 1, format, type, pixels);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400899 pixels += rowBytes;
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400900 }
901
Jamie Madille39e8f42018-10-05 08:17:38 -0400902 return angle::Result::Continue();
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400903}
904
Jamie Madille39e8f42018-10-05 08:17:38 -0400905angle::Result FramebufferGL::readPixelsAllAtOnce(const gl::Context *context,
906 const gl::Rectangle &area,
907 GLenum format,
908 GLenum type,
909 const gl::PixelPackState &pack,
910 GLubyte *pixels,
911 bool readLastRowSeparately) const
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400912{
Jamie Madille39e8f42018-10-05 08:17:38 -0400913 ContextGL *contextGL = GetImplAs<ContextGL>(context);
Geoff Lang61107632018-05-09 11:32:46 -0400914 const FunctionsGL *functions = GetFunctionsGL(context);
915 StateManagerGL *stateManager = GetStateManagerGL(context);
916
Frank Henigmanfa36c332017-06-09 18:44:45 -0400917 GLint height = area.height - readLastRowSeparately;
918 if (height > 0)
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400919 {
Geoff Lang61107632018-05-09 11:32:46 -0400920 stateManager->setPixelPackState(pack);
921 functions->readPixels(area.x, area.y, area.width, height, format, type, pixels);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400922 }
923
Frank Henigmanfa36c332017-06-09 18:44:45 -0400924 if (readLastRowSeparately)
925 {
926 const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type);
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400927
Frank Henigmanfa36c332017-06-09 18:44:45 -0400928 GLuint rowBytes = 0;
Jamie Madille39e8f42018-10-05 08:17:38 -0400929 ANGLE_CHECK_GL_MATH(contextGL, glFormat.computeRowPitch(type, area.width, pack.alignment,
930 pack.rowLength, &rowBytes));
Frank Henigmanfa36c332017-06-09 18:44:45 -0400931 GLuint skipBytes = 0;
Jamie Madille39e8f42018-10-05 08:17:38 -0400932 ANGLE_CHECK_GL_MATH(contextGL,
933 glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes));
Frank Henigmanfa36c332017-06-09 18:44:45 -0400934
935 gl::PixelPackState directPack;
Frank Henigmanfa36c332017-06-09 18:44:45 -0400936 directPack.alignment = 1;
Geoff Lang61107632018-05-09 11:32:46 -0400937 stateManager->setPixelPackState(directPack);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400938
939 pixels += skipBytes + (area.height - 1) * rowBytes;
Geoff Lang61107632018-05-09 11:32:46 -0400940 functions->readPixels(area.x, area.y + area.height - 1, area.width, 1, format, type,
941 pixels);
Frank Henigmanfa36c332017-06-09 18:44:45 -0400942 }
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400943
Jamie Madille39e8f42018-10-05 08:17:38 -0400944 return angle::Result::Continue();
Corentin Wallez9a8d3662016-09-22 12:18:29 -0400945}
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500946} // namespace rx