1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker
7*8975f5c5SAndroid Build Coastguard Worker // FramebufferGL.cpp: Implements the class methods for FramebufferGL.
8*8975f5c5SAndroid Build Coastguard Worker
9*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/gl/FramebufferGL.h"
10*8975f5c5SAndroid Build Coastguard Worker
11*8975f5c5SAndroid Build Coastguard Worker #include "common/bitset_utils.h"
12*8975f5c5SAndroid Build Coastguard Worker #include "common/debug.h"
13*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/ErrorStrings.h"
14*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/FramebufferAttachment.h"
15*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/State.h"
16*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/angletypes.h"
17*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/formatutils.h"
18*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/queryconversions.h"
19*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/ContextImpl.h"
20*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/gl/BlitGL.h"
21*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/gl/ClearMultiviewGL.h"
22*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/gl/ContextGL.h"
23*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/gl/FunctionsGL.h"
24*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/gl/RenderbufferGL.h"
25*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/gl/StateManagerGL.h"
26*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/gl/TextureGL.h"
27*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/gl/formatutilsgl.h"
28*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/gl/renderergl_utils.h"
29*8975f5c5SAndroid Build Coastguard Worker #include "platform/PlatformMethods.h"
30*8975f5c5SAndroid Build Coastguard Worker #include "platform/autogen/FeaturesGL_autogen.h"
31*8975f5c5SAndroid Build Coastguard Worker
32*8975f5c5SAndroid Build Coastguard Worker using namespace gl;
33*8975f5c5SAndroid Build Coastguard Worker using angle::CheckedNumeric;
34*8975f5c5SAndroid Build Coastguard Worker
35*8975f5c5SAndroid Build Coastguard Worker namespace rx
36*8975f5c5SAndroid Build Coastguard Worker {
37*8975f5c5SAndroid Build Coastguard Worker
38*8975f5c5SAndroid Build Coastguard Worker namespace
39*8975f5c5SAndroid Build Coastguard Worker {
40*8975f5c5SAndroid Build Coastguard Worker
41*8975f5c5SAndroid Build Coastguard Worker struct BlitFramebufferBounds
42*8975f5c5SAndroid Build Coastguard Worker {
43*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle sourceBounds;
44*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle sourceRegion;
45*8975f5c5SAndroid Build Coastguard Worker
46*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle destBounds;
47*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle destRegion;
48*8975f5c5SAndroid Build Coastguard Worker
49*8975f5c5SAndroid Build Coastguard Worker bool xFlipped;
50*8975f5c5SAndroid Build Coastguard Worker bool yFlipped;
51*8975f5c5SAndroid Build Coastguard Worker };
52*8975f5c5SAndroid Build Coastguard Worker
GetBlitFramebufferBounds(const gl::Context * context,const gl::Rectangle & sourceArea,const gl::Rectangle & destArea)53*8975f5c5SAndroid Build Coastguard Worker static BlitFramebufferBounds GetBlitFramebufferBounds(const gl::Context *context,
54*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &sourceArea,
55*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &destArea)
56*8975f5c5SAndroid Build Coastguard Worker {
57*8975f5c5SAndroid Build Coastguard Worker BlitFramebufferBounds bounds;
58*8975f5c5SAndroid Build Coastguard Worker
59*8975f5c5SAndroid Build Coastguard Worker const Framebuffer *sourceFramebuffer = context->getState().getReadFramebuffer();
60*8975f5c5SAndroid Build Coastguard Worker const Framebuffer *destFramebuffer = context->getState().getDrawFramebuffer();
61*8975f5c5SAndroid Build Coastguard Worker
62*8975f5c5SAndroid Build Coastguard Worker gl::Extents readSize = sourceFramebuffer->getExtents();
63*8975f5c5SAndroid Build Coastguard Worker gl::Extents drawSize = destFramebuffer->getExtents();
64*8975f5c5SAndroid Build Coastguard Worker
65*8975f5c5SAndroid Build Coastguard Worker bounds.sourceBounds = gl::Rectangle(0, 0, readSize.width, readSize.height);
66*8975f5c5SAndroid Build Coastguard Worker bounds.sourceRegion = sourceArea.removeReversal();
67*8975f5c5SAndroid Build Coastguard Worker
68*8975f5c5SAndroid Build Coastguard Worker bounds.destBounds = gl::Rectangle(0, 0, drawSize.width, drawSize.height);
69*8975f5c5SAndroid Build Coastguard Worker bounds.destRegion = destArea.removeReversal();
70*8975f5c5SAndroid Build Coastguard Worker
71*8975f5c5SAndroid Build Coastguard Worker bounds.xFlipped = sourceArea.isReversedX() != destArea.isReversedX();
72*8975f5c5SAndroid Build Coastguard Worker bounds.yFlipped = sourceArea.isReversedY() != destArea.isReversedY();
73*8975f5c5SAndroid Build Coastguard Worker
74*8975f5c5SAndroid Build Coastguard Worker return bounds;
75*8975f5c5SAndroid Build Coastguard Worker }
76*8975f5c5SAndroid Build Coastguard Worker
BindFramebufferAttachment(const FunctionsGL * functions,GLenum attachmentPoint,const FramebufferAttachment * attachment,const angle::FeaturesGL & features)77*8975f5c5SAndroid Build Coastguard Worker void BindFramebufferAttachment(const FunctionsGL *functions,
78*8975f5c5SAndroid Build Coastguard Worker GLenum attachmentPoint,
79*8975f5c5SAndroid Build Coastguard Worker const FramebufferAttachment *attachment,
80*8975f5c5SAndroid Build Coastguard Worker const angle::FeaturesGL &features)
81*8975f5c5SAndroid Build Coastguard Worker {
82*8975f5c5SAndroid Build Coastguard Worker if (attachment)
83*8975f5c5SAndroid Build Coastguard Worker {
84*8975f5c5SAndroid Build Coastguard Worker if (attachment->type() == GL_TEXTURE)
85*8975f5c5SAndroid Build Coastguard Worker {
86*8975f5c5SAndroid Build Coastguard Worker const Texture *texture = attachment->getTexture();
87*8975f5c5SAndroid Build Coastguard Worker const TextureGL *textureGL = GetImplAs<TextureGL>(texture);
88*8975f5c5SAndroid Build Coastguard Worker
89*8975f5c5SAndroid Build Coastguard Worker if (texture->getType() == TextureType::_2D ||
90*8975f5c5SAndroid Build Coastguard Worker texture->getType() == TextureType::_2DMultisample ||
91*8975f5c5SAndroid Build Coastguard Worker texture->getType() == TextureType::Rectangle ||
92*8975f5c5SAndroid Build Coastguard Worker texture->getType() == TextureType::External)
93*8975f5c5SAndroid Build Coastguard Worker {
94*8975f5c5SAndroid Build Coastguard Worker if (attachment->isRenderToTexture())
95*8975f5c5SAndroid Build Coastguard Worker {
96*8975f5c5SAndroid Build Coastguard Worker if (functions->framebufferTexture2DMultisampleEXT)
97*8975f5c5SAndroid Build Coastguard Worker {
98*8975f5c5SAndroid Build Coastguard Worker functions->framebufferTexture2DMultisampleEXT(
99*8975f5c5SAndroid Build Coastguard Worker GL_FRAMEBUFFER, attachmentPoint, ToGLenum(texture->getType()),
100*8975f5c5SAndroid Build Coastguard Worker textureGL->getTextureID(), attachment->mipLevel(),
101*8975f5c5SAndroid Build Coastguard Worker attachment->getSamples());
102*8975f5c5SAndroid Build Coastguard Worker }
103*8975f5c5SAndroid Build Coastguard Worker else
104*8975f5c5SAndroid Build Coastguard Worker {
105*8975f5c5SAndroid Build Coastguard Worker ASSERT(functions->framebufferTexture2DMultisampleIMG);
106*8975f5c5SAndroid Build Coastguard Worker functions->framebufferTexture2DMultisampleIMG(
107*8975f5c5SAndroid Build Coastguard Worker GL_FRAMEBUFFER, attachmentPoint, ToGLenum(texture->getType()),
108*8975f5c5SAndroid Build Coastguard Worker textureGL->getTextureID(), attachment->mipLevel(),
109*8975f5c5SAndroid Build Coastguard Worker attachment->getSamples());
110*8975f5c5SAndroid Build Coastguard Worker }
111*8975f5c5SAndroid Build Coastguard Worker }
112*8975f5c5SAndroid Build Coastguard Worker else
113*8975f5c5SAndroid Build Coastguard Worker {
114*8975f5c5SAndroid Build Coastguard Worker functions->framebufferTexture2D(
115*8975f5c5SAndroid Build Coastguard Worker GL_FRAMEBUFFER, attachmentPoint, ToGLenum(texture->getType()),
116*8975f5c5SAndroid Build Coastguard Worker textureGL->getTextureID(), attachment->mipLevel());
117*8975f5c5SAndroid Build Coastguard Worker }
118*8975f5c5SAndroid Build Coastguard Worker }
119*8975f5c5SAndroid Build Coastguard Worker else if (attachment->isLayered())
120*8975f5c5SAndroid Build Coastguard Worker {
121*8975f5c5SAndroid Build Coastguard Worker TextureType textureType = texture->getType();
122*8975f5c5SAndroid Build Coastguard Worker ASSERT(textureType == TextureType::_2DArray || textureType == TextureType::_3D ||
123*8975f5c5SAndroid Build Coastguard Worker textureType == TextureType::CubeMap ||
124*8975f5c5SAndroid Build Coastguard Worker textureType == TextureType::_2DMultisampleArray ||
125*8975f5c5SAndroid Build Coastguard Worker textureType == TextureType::CubeMapArray);
126*8975f5c5SAndroid Build Coastguard Worker functions->framebufferTexture(GL_FRAMEBUFFER, attachmentPoint,
127*8975f5c5SAndroid Build Coastguard Worker textureGL->getTextureID(), attachment->mipLevel());
128*8975f5c5SAndroid Build Coastguard Worker }
129*8975f5c5SAndroid Build Coastguard Worker else if (texture->getType() == TextureType::CubeMap)
130*8975f5c5SAndroid Build Coastguard Worker {
131*8975f5c5SAndroid Build Coastguard Worker functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint,
132*8975f5c5SAndroid Build Coastguard Worker ToGLenum(attachment->cubeMapFace()),
133*8975f5c5SAndroid Build Coastguard Worker textureGL->getTextureID(), attachment->mipLevel());
134*8975f5c5SAndroid Build Coastguard Worker }
135*8975f5c5SAndroid Build Coastguard Worker else if (texture->getType() == TextureType::_2DArray ||
136*8975f5c5SAndroid Build Coastguard Worker texture->getType() == TextureType::_3D ||
137*8975f5c5SAndroid Build Coastguard Worker texture->getType() == TextureType::_2DMultisampleArray ||
138*8975f5c5SAndroid Build Coastguard Worker texture->getType() == TextureType::CubeMapArray)
139*8975f5c5SAndroid Build Coastguard Worker {
140*8975f5c5SAndroid Build Coastguard Worker if (attachment->isMultiview())
141*8975f5c5SAndroid Build Coastguard Worker {
142*8975f5c5SAndroid Build Coastguard Worker ASSERT(functions->framebufferTexture);
143*8975f5c5SAndroid Build Coastguard Worker functions->framebufferTexture(GL_FRAMEBUFFER, attachmentPoint,
144*8975f5c5SAndroid Build Coastguard Worker textureGL->getTextureID(),
145*8975f5c5SAndroid Build Coastguard Worker attachment->mipLevel());
146*8975f5c5SAndroid Build Coastguard Worker }
147*8975f5c5SAndroid Build Coastguard Worker else
148*8975f5c5SAndroid Build Coastguard Worker {
149*8975f5c5SAndroid Build Coastguard Worker functions->framebufferTextureLayer(GL_FRAMEBUFFER, attachmentPoint,
150*8975f5c5SAndroid Build Coastguard Worker textureGL->getTextureID(),
151*8975f5c5SAndroid Build Coastguard Worker attachment->mipLevel(), attachment->layer());
152*8975f5c5SAndroid Build Coastguard Worker }
153*8975f5c5SAndroid Build Coastguard Worker }
154*8975f5c5SAndroid Build Coastguard Worker else
155*8975f5c5SAndroid Build Coastguard Worker {
156*8975f5c5SAndroid Build Coastguard Worker UNREACHABLE();
157*8975f5c5SAndroid Build Coastguard Worker }
158*8975f5c5SAndroid Build Coastguard Worker }
159*8975f5c5SAndroid Build Coastguard Worker else if (attachment->type() == GL_RENDERBUFFER)
160*8975f5c5SAndroid Build Coastguard Worker {
161*8975f5c5SAndroid Build Coastguard Worker const Renderbuffer *renderbuffer = attachment->getRenderbuffer();
162*8975f5c5SAndroid Build Coastguard Worker const RenderbufferGL *renderbufferGL = GetImplAs<RenderbufferGL>(renderbuffer);
163*8975f5c5SAndroid Build Coastguard Worker
164*8975f5c5SAndroid Build Coastguard Worker if (features.alwaysUnbindFramebufferTexture2D.enabled)
165*8975f5c5SAndroid Build Coastguard Worker {
166*8975f5c5SAndroid Build Coastguard Worker functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, 0,
167*8975f5c5SAndroid Build Coastguard Worker 0);
168*8975f5c5SAndroid Build Coastguard Worker }
169*8975f5c5SAndroid Build Coastguard Worker
170*8975f5c5SAndroid Build Coastguard Worker functions->framebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER,
171*8975f5c5SAndroid Build Coastguard Worker renderbufferGL->getRenderbufferID());
172*8975f5c5SAndroid Build Coastguard Worker }
173*8975f5c5SAndroid Build Coastguard Worker else
174*8975f5c5SAndroid Build Coastguard Worker {
175*8975f5c5SAndroid Build Coastguard Worker UNREACHABLE();
176*8975f5c5SAndroid Build Coastguard Worker }
177*8975f5c5SAndroid Build Coastguard Worker }
178*8975f5c5SAndroid Build Coastguard Worker else
179*8975f5c5SAndroid Build Coastguard Worker {
180*8975f5c5SAndroid Build Coastguard Worker // Unbind this attachment
181*8975f5c5SAndroid Build Coastguard Worker functions->framebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, GL_TEXTURE_2D, 0, 0);
182*8975f5c5SAndroid Build Coastguard Worker }
183*8975f5c5SAndroid Build Coastguard Worker }
184*8975f5c5SAndroid Build Coastguard Worker
AreAllLayersActive(const FramebufferAttachment & attachment)185*8975f5c5SAndroid Build Coastguard Worker bool AreAllLayersActive(const FramebufferAttachment &attachment)
186*8975f5c5SAndroid Build Coastguard Worker {
187*8975f5c5SAndroid Build Coastguard Worker int baseViewIndex = attachment.getBaseViewIndex();
188*8975f5c5SAndroid Build Coastguard Worker if (baseViewIndex != 0)
189*8975f5c5SAndroid Build Coastguard Worker {
190*8975f5c5SAndroid Build Coastguard Worker return false;
191*8975f5c5SAndroid Build Coastguard Worker }
192*8975f5c5SAndroid Build Coastguard Worker const ImageIndex &imageIndex = attachment.getTextureImageIndex();
193*8975f5c5SAndroid Build Coastguard Worker int numLayers = static_cast<int>(
194*8975f5c5SAndroid Build Coastguard Worker attachment.getTexture()->getDepth(imageIndex.getTarget(), imageIndex.getLevelIndex()));
195*8975f5c5SAndroid Build Coastguard Worker return (attachment.getNumViews() == numLayers);
196*8975f5c5SAndroid Build Coastguard Worker }
197*8975f5c5SAndroid Build Coastguard Worker
RequiresMultiviewClear(const FramebufferState & state,bool scissorTestEnabled)198*8975f5c5SAndroid Build Coastguard Worker bool RequiresMultiviewClear(const FramebufferState &state, bool scissorTestEnabled)
199*8975f5c5SAndroid Build Coastguard Worker {
200*8975f5c5SAndroid Build Coastguard Worker // Get one attachment and check whether all layers are attached.
201*8975f5c5SAndroid Build Coastguard Worker const FramebufferAttachment *attachment = nullptr;
202*8975f5c5SAndroid Build Coastguard Worker bool allTextureArraysAreFullyAttached = true;
203*8975f5c5SAndroid Build Coastguard Worker for (const FramebufferAttachment &colorAttachment : state.getColorAttachments())
204*8975f5c5SAndroid Build Coastguard Worker {
205*8975f5c5SAndroid Build Coastguard Worker if (colorAttachment.isAttached())
206*8975f5c5SAndroid Build Coastguard Worker {
207*8975f5c5SAndroid Build Coastguard Worker if (!colorAttachment.isMultiview())
208*8975f5c5SAndroid Build Coastguard Worker {
209*8975f5c5SAndroid Build Coastguard Worker return false;
210*8975f5c5SAndroid Build Coastguard Worker }
211*8975f5c5SAndroid Build Coastguard Worker attachment = &colorAttachment;
212*8975f5c5SAndroid Build Coastguard Worker allTextureArraysAreFullyAttached =
213*8975f5c5SAndroid Build Coastguard Worker allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment);
214*8975f5c5SAndroid Build Coastguard Worker }
215*8975f5c5SAndroid Build Coastguard Worker }
216*8975f5c5SAndroid Build Coastguard Worker
217*8975f5c5SAndroid Build Coastguard Worker const FramebufferAttachment *depthAttachment = state.getDepthAttachment();
218*8975f5c5SAndroid Build Coastguard Worker if (depthAttachment)
219*8975f5c5SAndroid Build Coastguard Worker {
220*8975f5c5SAndroid Build Coastguard Worker if (!depthAttachment->isMultiview())
221*8975f5c5SAndroid Build Coastguard Worker {
222*8975f5c5SAndroid Build Coastguard Worker return false;
223*8975f5c5SAndroid Build Coastguard Worker }
224*8975f5c5SAndroid Build Coastguard Worker attachment = depthAttachment;
225*8975f5c5SAndroid Build Coastguard Worker allTextureArraysAreFullyAttached =
226*8975f5c5SAndroid Build Coastguard Worker allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment);
227*8975f5c5SAndroid Build Coastguard Worker }
228*8975f5c5SAndroid Build Coastguard Worker const FramebufferAttachment *stencilAttachment = state.getStencilAttachment();
229*8975f5c5SAndroid Build Coastguard Worker if (stencilAttachment)
230*8975f5c5SAndroid Build Coastguard Worker {
231*8975f5c5SAndroid Build Coastguard Worker if (!stencilAttachment->isMultiview())
232*8975f5c5SAndroid Build Coastguard Worker {
233*8975f5c5SAndroid Build Coastguard Worker return false;
234*8975f5c5SAndroid Build Coastguard Worker }
235*8975f5c5SAndroid Build Coastguard Worker attachment = stencilAttachment;
236*8975f5c5SAndroid Build Coastguard Worker allTextureArraysAreFullyAttached =
237*8975f5c5SAndroid Build Coastguard Worker allTextureArraysAreFullyAttached && AreAllLayersActive(*attachment);
238*8975f5c5SAndroid Build Coastguard Worker }
239*8975f5c5SAndroid Build Coastguard Worker
240*8975f5c5SAndroid Build Coastguard Worker if (attachment == nullptr)
241*8975f5c5SAndroid Build Coastguard Worker {
242*8975f5c5SAndroid Build Coastguard Worker return false;
243*8975f5c5SAndroid Build Coastguard Worker }
244*8975f5c5SAndroid Build Coastguard Worker if (attachment->isMultiview())
245*8975f5c5SAndroid Build Coastguard Worker {
246*8975f5c5SAndroid Build Coastguard Worker // If all layers of each texture array are active, then there is no need to issue a
247*8975f5c5SAndroid Build Coastguard Worker // special multiview clear.
248*8975f5c5SAndroid Build Coastguard Worker return !allTextureArraysAreFullyAttached;
249*8975f5c5SAndroid Build Coastguard Worker }
250*8975f5c5SAndroid Build Coastguard Worker return false;
251*8975f5c5SAndroid Build Coastguard Worker }
252*8975f5c5SAndroid Build Coastguard Worker
IsEmulatedAlphaChannelTextureAttachment(const FramebufferAttachment * attachment)253*8975f5c5SAndroid Build Coastguard Worker bool IsEmulatedAlphaChannelTextureAttachment(const FramebufferAttachment *attachment)
254*8975f5c5SAndroid Build Coastguard Worker {
255*8975f5c5SAndroid Build Coastguard Worker if (!attachment || attachment->type() != GL_TEXTURE)
256*8975f5c5SAndroid Build Coastguard Worker {
257*8975f5c5SAndroid Build Coastguard Worker return false;
258*8975f5c5SAndroid Build Coastguard Worker }
259*8975f5c5SAndroid Build Coastguard Worker
260*8975f5c5SAndroid Build Coastguard Worker const Texture *texture = attachment->getTexture();
261*8975f5c5SAndroid Build Coastguard Worker const TextureGL *textureGL = GetImplAs<TextureGL>(texture);
262*8975f5c5SAndroid Build Coastguard Worker return textureGL->hasEmulatedAlphaChannel(attachment->getTextureImageIndex());
263*8975f5c5SAndroid Build Coastguard Worker }
264*8975f5c5SAndroid Build Coastguard Worker
265*8975f5c5SAndroid Build Coastguard Worker class [[nodiscard]] ScopedEXTTextureNorm16ReadbackWorkaround
266*8975f5c5SAndroid Build Coastguard Worker {
267*8975f5c5SAndroid Build Coastguard Worker public:
ScopedEXTTextureNorm16ReadbackWorkaround()268*8975f5c5SAndroid Build Coastguard Worker ScopedEXTTextureNorm16ReadbackWorkaround()
269*8975f5c5SAndroid Build Coastguard Worker : tmpPixels(nullptr), clientPixels(nullptr), enabled(false)
270*8975f5c5SAndroid Build Coastguard Worker {}
271*8975f5c5SAndroid Build Coastguard Worker
~ScopedEXTTextureNorm16ReadbackWorkaround()272*8975f5c5SAndroid Build Coastguard Worker ~ScopedEXTTextureNorm16ReadbackWorkaround()
273*8975f5c5SAndroid Build Coastguard Worker {
274*8975f5c5SAndroid Build Coastguard Worker if (tmpPixels)
275*8975f5c5SAndroid Build Coastguard Worker {
276*8975f5c5SAndroid Build Coastguard Worker delete[] tmpPixels;
277*8975f5c5SAndroid Build Coastguard Worker }
278*8975f5c5SAndroid Build Coastguard Worker }
279*8975f5c5SAndroid Build Coastguard Worker
Initialize(const gl::Context * context,const gl::Rectangle & area,GLenum originalReadFormat,GLenum format,GLenum type,GLuint skipBytes,GLuint rowBytes,GLuint pixelBytes,GLubyte * pixels)280*8975f5c5SAndroid Build Coastguard Worker angle::Result Initialize(const gl::Context *context,
281*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &area,
282*8975f5c5SAndroid Build Coastguard Worker GLenum originalReadFormat,
283*8975f5c5SAndroid Build Coastguard Worker GLenum format,
284*8975f5c5SAndroid Build Coastguard Worker GLenum type,
285*8975f5c5SAndroid Build Coastguard Worker GLuint skipBytes,
286*8975f5c5SAndroid Build Coastguard Worker GLuint rowBytes,
287*8975f5c5SAndroid Build Coastguard Worker GLuint pixelBytes,
288*8975f5c5SAndroid Build Coastguard Worker GLubyte *pixels)
289*8975f5c5SAndroid Build Coastguard Worker {
290*8975f5c5SAndroid Build Coastguard Worker // Separate from constructor as there may be checked math result exception that needs to
291*8975f5c5SAndroid Build Coastguard Worker // early return
292*8975f5c5SAndroid Build Coastguard Worker ASSERT(tmpPixels == nullptr);
293*8975f5c5SAndroid Build Coastguard Worker ASSERT(clientPixels == nullptr);
294*8975f5c5SAndroid Build Coastguard Worker
295*8975f5c5SAndroid Build Coastguard Worker ContextGL *contextGL = GetImplAs<ContextGL>(context);
296*8975f5c5SAndroid Build Coastguard Worker const angle::FeaturesGL &features = GetFeaturesGL(context);
297*8975f5c5SAndroid Build Coastguard Worker
298*8975f5c5SAndroid Build Coastguard Worker enabled = features.readPixelsUsingImplementationColorReadFormatForNorm16.enabled &&
299*8975f5c5SAndroid Build Coastguard Worker type == GL_UNSIGNED_SHORT && originalReadFormat == GL_RGBA &&
300*8975f5c5SAndroid Build Coastguard Worker (format == GL_RED || format == GL_RG);
301*8975f5c5SAndroid Build Coastguard Worker
302*8975f5c5SAndroid Build Coastguard Worker clientPixels = pixels;
303*8975f5c5SAndroid Build Coastguard Worker
304*8975f5c5SAndroid Build Coastguard Worker if (enabled)
305*8975f5c5SAndroid Build Coastguard Worker {
306*8975f5c5SAndroid Build Coastguard Worker CheckedNumeric<GLuint> checkedRowBytes(rowBytes);
307*8975f5c5SAndroid Build Coastguard Worker CheckedNumeric<GLuint> checkedRows(area.height);
308*8975f5c5SAndroid Build Coastguard Worker CheckedNumeric<GLuint> checkedSkipBytes(skipBytes);
309*8975f5c5SAndroid Build Coastguard Worker auto checkedAllocatedBytes = checkedSkipBytes + checkedRowBytes * checkedRows;
310*8975f5c5SAndroid Build Coastguard Worker if (rowBytes < area.width * pixelBytes)
311*8975f5c5SAndroid Build Coastguard Worker {
312*8975f5c5SAndroid Build Coastguard Worker checkedAllocatedBytes += area.width * pixelBytes - rowBytes;
313*8975f5c5SAndroid Build Coastguard Worker }
314*8975f5c5SAndroid Build Coastguard Worker ANGLE_CHECK_GL_MATH(contextGL, checkedAllocatedBytes.IsValid());
315*8975f5c5SAndroid Build Coastguard Worker const GLuint allocatedBytes = checkedAllocatedBytes.ValueOrDie();
316*8975f5c5SAndroid Build Coastguard Worker tmpPixels = new GLubyte[allocatedBytes];
317*8975f5c5SAndroid Build Coastguard Worker memset(tmpPixels, 0, allocatedBytes);
318*8975f5c5SAndroid Build Coastguard Worker }
319*8975f5c5SAndroid Build Coastguard Worker
320*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
321*8975f5c5SAndroid Build Coastguard Worker }
322*8975f5c5SAndroid Build Coastguard Worker
Pixels() const323*8975f5c5SAndroid Build Coastguard Worker GLubyte *Pixels() const { return tmpPixels ? tmpPixels : clientPixels; }
324*8975f5c5SAndroid Build Coastguard Worker
IsEnabled() const325*8975f5c5SAndroid Build Coastguard Worker bool IsEnabled() const { return enabled; }
326*8975f5c5SAndroid Build Coastguard Worker
327*8975f5c5SAndroid Build Coastguard Worker private:
328*8975f5c5SAndroid Build Coastguard Worker // Temporarily allocated pixel readback buffer
329*8975f5c5SAndroid Build Coastguard Worker GLubyte *tmpPixels;
330*8975f5c5SAndroid Build Coastguard Worker // Client pixel array pointer passed to readPixels
331*8975f5c5SAndroid Build Coastguard Worker GLubyte *clientPixels;
332*8975f5c5SAndroid Build Coastguard Worker
333*8975f5c5SAndroid Build Coastguard Worker bool enabled;
334*8975f5c5SAndroid Build Coastguard Worker };
335*8975f5c5SAndroid Build Coastguard Worker
336*8975f5c5SAndroid Build Coastguard Worker // Workaround to rearrange pixels read by RED/RG to RGBA for RGBA/UNSIGNED_SHORT pixel type
337*8975f5c5SAndroid Build Coastguard Worker // combination
RearrangeEXTTextureNorm16Pixels(const gl::Context * context,const gl::Rectangle & area,GLenum originalReadFormat,GLenum format,GLenum type,GLuint skipBytes,GLuint rowBytes,GLuint pixelBytes,const gl::PixelPackState & pack,GLubyte * clientPixels,GLubyte * tmpPixels)338*8975f5c5SAndroid Build Coastguard Worker angle::Result RearrangeEXTTextureNorm16Pixels(const gl::Context *context,
339*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &area,
340*8975f5c5SAndroid Build Coastguard Worker GLenum originalReadFormat,
341*8975f5c5SAndroid Build Coastguard Worker GLenum format,
342*8975f5c5SAndroid Build Coastguard Worker GLenum type,
343*8975f5c5SAndroid Build Coastguard Worker GLuint skipBytes,
344*8975f5c5SAndroid Build Coastguard Worker GLuint rowBytes,
345*8975f5c5SAndroid Build Coastguard Worker GLuint pixelBytes,
346*8975f5c5SAndroid Build Coastguard Worker const gl::PixelPackState &pack,
347*8975f5c5SAndroid Build Coastguard Worker GLubyte *clientPixels,
348*8975f5c5SAndroid Build Coastguard Worker GLubyte *tmpPixels)
349*8975f5c5SAndroid Build Coastguard Worker {
350*8975f5c5SAndroid Build Coastguard Worker ASSERT(tmpPixels != nullptr);
351*8975f5c5SAndroid Build Coastguard Worker ASSERT(originalReadFormat == GL_RGBA);
352*8975f5c5SAndroid Build Coastguard Worker ASSERT(format == GL_RED_EXT || format == GL_RG_EXT);
353*8975f5c5SAndroid Build Coastguard Worker ASSERT(type == GL_UNSIGNED_SHORT);
354*8975f5c5SAndroid Build Coastguard Worker
355*8975f5c5SAndroid Build Coastguard Worker ContextGL *contextGL = GetImplAs<ContextGL>(context);
356*8975f5c5SAndroid Build Coastguard Worker
357*8975f5c5SAndroid Build Coastguard Worker const gl::InternalFormat &glFormatOriginal =
358*8975f5c5SAndroid Build Coastguard Worker gl::GetInternalFormatInfo(originalReadFormat, type);
359*8975f5c5SAndroid Build Coastguard Worker
360*8975f5c5SAndroid Build Coastguard Worker GLuint originalReadFormatRowBytes = 0;
361*8975f5c5SAndroid Build Coastguard Worker ANGLE_CHECK_GL_MATH(
362*8975f5c5SAndroid Build Coastguard Worker contextGL, glFormatOriginal.computeRowPitch(type, area.width, pack.alignment,
363*8975f5c5SAndroid Build Coastguard Worker pack.rowLength, &originalReadFormatRowBytes));
364*8975f5c5SAndroid Build Coastguard Worker GLuint originalReadFormatSkipBytes = 0;
365*8975f5c5SAndroid Build Coastguard Worker ANGLE_CHECK_GL_MATH(contextGL,
366*8975f5c5SAndroid Build Coastguard Worker glFormatOriginal.computeSkipBytes(type, originalReadFormatRowBytes, 0, pack,
367*8975f5c5SAndroid Build Coastguard Worker false, &originalReadFormatSkipBytes));
368*8975f5c5SAndroid Build Coastguard Worker
369*8975f5c5SAndroid Build Coastguard Worker GLuint originalReadFormatPixelBytes = glFormatOriginal.computePixelBytes(type);
370*8975f5c5SAndroid Build Coastguard Worker GLuint alphaChannelBytes = glFormatOriginal.alphaBits / 8;
371*8975f5c5SAndroid Build Coastguard Worker
372*8975f5c5SAndroid Build Coastguard Worker ASSERT(originalReadFormatPixelBytes > pixelBytes);
373*8975f5c5SAndroid Build Coastguard Worker ASSERT(originalReadFormatPixelBytes > alphaChannelBytes);
374*8975f5c5SAndroid Build Coastguard Worker ASSERT(alphaChannelBytes != 0);
375*8975f5c5SAndroid Build Coastguard Worker ASSERT(glFormatOriginal.alphaBits % 8 == 0);
376*8975f5c5SAndroid Build Coastguard Worker
377*8975f5c5SAndroid Build Coastguard Worker // Populating rearrangedPixels values from pixels
378*8975f5c5SAndroid Build Coastguard Worker GLubyte *srcRowStart = tmpPixels;
379*8975f5c5SAndroid Build Coastguard Worker GLubyte *dstRowStart = clientPixels;
380*8975f5c5SAndroid Build Coastguard Worker
381*8975f5c5SAndroid Build Coastguard Worker srcRowStart += skipBytes;
382*8975f5c5SAndroid Build Coastguard Worker dstRowStart += originalReadFormatSkipBytes;
383*8975f5c5SAndroid Build Coastguard Worker
384*8975f5c5SAndroid Build Coastguard Worker for (GLint y = 0; y < area.height; ++y)
385*8975f5c5SAndroid Build Coastguard Worker {
386*8975f5c5SAndroid Build Coastguard Worker GLubyte *src = srcRowStart;
387*8975f5c5SAndroid Build Coastguard Worker GLubyte *dst = dstRowStart;
388*8975f5c5SAndroid Build Coastguard Worker for (GLint x = 0; x < area.width; ++x)
389*8975f5c5SAndroid Build Coastguard Worker {
390*8975f5c5SAndroid Build Coastguard Worker GLushort *srcPixel = reinterpret_cast<GLushort *>(src);
391*8975f5c5SAndroid Build Coastguard Worker GLushort *dstPixel = reinterpret_cast<GLushort *>(dst);
392*8975f5c5SAndroid Build Coastguard Worker dstPixel[0] = srcPixel[0];
393*8975f5c5SAndroid Build Coastguard Worker dstPixel[1] = format == GL_RG ? srcPixel[1] : 0;
394*8975f5c5SAndroid Build Coastguard Worker // Set other channel of RGBA to 0 (GB when format == GL_RED, B when format == GL_RG)
395*8975f5c5SAndroid Build Coastguard Worker dstPixel[2] = 0;
396*8975f5c5SAndroid Build Coastguard Worker // Set alpha channel to 1
397*8975f5c5SAndroid Build Coastguard Worker dstPixel[3] = 0xFFFF;
398*8975f5c5SAndroid Build Coastguard Worker
399*8975f5c5SAndroid Build Coastguard Worker src += pixelBytes;
400*8975f5c5SAndroid Build Coastguard Worker dst += originalReadFormatPixelBytes;
401*8975f5c5SAndroid Build Coastguard Worker }
402*8975f5c5SAndroid Build Coastguard Worker
403*8975f5c5SAndroid Build Coastguard Worker srcRowStart += rowBytes;
404*8975f5c5SAndroid Build Coastguard Worker dstRowStart += originalReadFormatRowBytes;
405*8975f5c5SAndroid Build Coastguard Worker }
406*8975f5c5SAndroid Build Coastguard Worker
407*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
408*8975f5c5SAndroid Build Coastguard Worker }
409*8975f5c5SAndroid Build Coastguard Worker
IsValidUnsignedShortReadPixelsFormat(GLenum readFormat,const gl::Context * context)410*8975f5c5SAndroid Build Coastguard Worker bool IsValidUnsignedShortReadPixelsFormat(GLenum readFormat, const gl::Context *context)
411*8975f5c5SAndroid Build Coastguard Worker {
412*8975f5c5SAndroid Build Coastguard Worker return (readFormat == GL_RED) || (readFormat == GL_RG) || (readFormat == GL_RGBA) ||
413*8975f5c5SAndroid Build Coastguard Worker ((readFormat == GL_DEPTH_COMPONENT) && (context->getExtensions().readDepthNV));
414*8975f5c5SAndroid Build Coastguard Worker }
415*8975f5c5SAndroid Build Coastguard Worker
416*8975f5c5SAndroid Build Coastguard Worker // Returns true for all colors except
417*8975f5c5SAndroid Build Coastguard Worker // - transparent/opaque black
418*8975f5c5SAndroid Build Coastguard Worker // - transparent/opaque white
IsNonTrivialClearColor(const GLfloat * color)419*8975f5c5SAndroid Build Coastguard Worker bool IsNonTrivialClearColor(const GLfloat *color)
420*8975f5c5SAndroid Build Coastguard Worker {
421*8975f5c5SAndroid Build Coastguard Worker return !(((color[0] == 0.0f && color[1] == 0.0f && color[2] == 0.0f) ||
422*8975f5c5SAndroid Build Coastguard Worker (color[0] == 1.0f && color[1] == 1.0f && color[2] == 1.0f)) &&
423*8975f5c5SAndroid Build Coastguard Worker (color[3] == 0.0f || color[3] == 1.0f));
424*8975f5c5SAndroid Build Coastguard Worker }
425*8975f5c5SAndroid Build Coastguard Worker
426*8975f5c5SAndroid Build Coastguard Worker // Returns true for all colors except
427*8975f5c5SAndroid Build Coastguard Worker // - (0, 0, 0, 0 or 1)
428*8975f5c5SAndroid Build Coastguard Worker // - (1, 1, 1, 0 or 1)
IsNonTrivialClearColor(const GLuint * color)429*8975f5c5SAndroid Build Coastguard Worker bool IsNonTrivialClearColor(const GLuint *color)
430*8975f5c5SAndroid Build Coastguard Worker {
431*8975f5c5SAndroid Build Coastguard Worker return !(((color[0] == 0 && color[1] == 0 && color[2] == 0) ||
432*8975f5c5SAndroid Build Coastguard Worker (color[0] == 1 && color[1] == 1 && color[2] == 1)) &&
433*8975f5c5SAndroid Build Coastguard Worker (color[3] == 0 || color[3] == 1));
434*8975f5c5SAndroid Build Coastguard Worker }
435*8975f5c5SAndroid Build Coastguard Worker
436*8975f5c5SAndroid Build Coastguard Worker } // namespace
437*8975f5c5SAndroid Build Coastguard Worker
FramebufferGL(const gl::FramebufferState & data,GLuint id,bool emulatedAlpha)438*8975f5c5SAndroid Build Coastguard Worker FramebufferGL::FramebufferGL(const gl::FramebufferState &data, GLuint id, bool emulatedAlpha)
439*8975f5c5SAndroid Build Coastguard Worker : FramebufferImpl(data),
440*8975f5c5SAndroid Build Coastguard Worker mFramebufferID(id),
441*8975f5c5SAndroid Build Coastguard Worker mHasEmulatedAlphaAttachment(emulatedAlpha),
442*8975f5c5SAndroid Build Coastguard Worker mAppliedEnabledDrawBuffers(1)
443*8975f5c5SAndroid Build Coastguard Worker {
444*8975f5c5SAndroid Build Coastguard Worker ASSERT((isDefault() && id == 0) || !isDefault());
445*8975f5c5SAndroid Build Coastguard Worker }
446*8975f5c5SAndroid Build Coastguard Worker
~FramebufferGL()447*8975f5c5SAndroid Build Coastguard Worker FramebufferGL::~FramebufferGL()
448*8975f5c5SAndroid Build Coastguard Worker {
449*8975f5c5SAndroid Build Coastguard Worker ASSERT(mFramebufferID == 0);
450*8975f5c5SAndroid Build Coastguard Worker }
451*8975f5c5SAndroid Build Coastguard Worker
destroy(const gl::Context * context)452*8975f5c5SAndroid Build Coastguard Worker void FramebufferGL::destroy(const gl::Context *context)
453*8975f5c5SAndroid Build Coastguard Worker {
454*8975f5c5SAndroid Build Coastguard Worker if (mFramebufferID)
455*8975f5c5SAndroid Build Coastguard Worker {
456*8975f5c5SAndroid Build Coastguard Worker ASSERT(!isDefault());
457*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
458*8975f5c5SAndroid Build Coastguard Worker stateManager->deleteFramebuffer(mFramebufferID);
459*8975f5c5SAndroid Build Coastguard Worker mFramebufferID = 0;
460*8975f5c5SAndroid Build Coastguard Worker }
461*8975f5c5SAndroid Build Coastguard Worker }
462*8975f5c5SAndroid Build Coastguard Worker
discard(const gl::Context * context,size_t count,const GLenum * attachments)463*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::discard(const gl::Context *context,
464*8975f5c5SAndroid Build Coastguard Worker size_t count,
465*8975f5c5SAndroid Build Coastguard Worker const GLenum *attachments)
466*8975f5c5SAndroid Build Coastguard Worker {
467*8975f5c5SAndroid Build Coastguard Worker // glInvalidateFramebuffer accepts the same enums as glDiscardFramebufferEXT
468*8975f5c5SAndroid Build Coastguard Worker return invalidate(context, count, attachments);
469*8975f5c5SAndroid Build Coastguard Worker }
470*8975f5c5SAndroid Build Coastguard Worker
invalidate(const gl::Context * context,size_t count,const GLenum * attachments)471*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::invalidate(const gl::Context *context,
472*8975f5c5SAndroid Build Coastguard Worker size_t count,
473*8975f5c5SAndroid Build Coastguard Worker const GLenum *attachments)
474*8975f5c5SAndroid Build Coastguard Worker {
475*8975f5c5SAndroid Build Coastguard Worker const GLenum *finalAttachmentsPtr = attachments;
476*8975f5c5SAndroid Build Coastguard Worker
477*8975f5c5SAndroid Build Coastguard Worker std::vector<GLenum> modifiedAttachments;
478*8975f5c5SAndroid Build Coastguard Worker if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments))
479*8975f5c5SAndroid Build Coastguard Worker {
480*8975f5c5SAndroid Build Coastguard Worker finalAttachmentsPtr = modifiedAttachments.data();
481*8975f5c5SAndroid Build Coastguard Worker }
482*8975f5c5SAndroid Build Coastguard Worker
483*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
484*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
485*8975f5c5SAndroid Build Coastguard Worker
486*8975f5c5SAndroid Build Coastguard Worker // Since this function is just a hint, only call a native function if it exists.
487*8975f5c5SAndroid Build Coastguard Worker if (functions->invalidateFramebuffer)
488*8975f5c5SAndroid Build Coastguard Worker {
489*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
490*8975f5c5SAndroid Build Coastguard Worker functions->invalidateFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count),
491*8975f5c5SAndroid Build Coastguard Worker finalAttachmentsPtr);
492*8975f5c5SAndroid Build Coastguard Worker }
493*8975f5c5SAndroid Build Coastguard Worker else if (functions->discardFramebufferEXT)
494*8975f5c5SAndroid Build Coastguard Worker {
495*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
496*8975f5c5SAndroid Build Coastguard Worker functions->discardFramebufferEXT(GL_FRAMEBUFFER, static_cast<GLsizei>(count),
497*8975f5c5SAndroid Build Coastguard Worker finalAttachmentsPtr);
498*8975f5c5SAndroid Build Coastguard Worker }
499*8975f5c5SAndroid Build Coastguard Worker
500*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
501*8975f5c5SAndroid Build Coastguard Worker }
502*8975f5c5SAndroid Build Coastguard Worker
invalidateSub(const gl::Context * context,size_t count,const GLenum * attachments,const gl::Rectangle & area)503*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::invalidateSub(const gl::Context *context,
504*8975f5c5SAndroid Build Coastguard Worker size_t count,
505*8975f5c5SAndroid Build Coastguard Worker const GLenum *attachments,
506*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &area)
507*8975f5c5SAndroid Build Coastguard Worker {
508*8975f5c5SAndroid Build Coastguard Worker
509*8975f5c5SAndroid Build Coastguard Worker const GLenum *finalAttachmentsPtr = attachments;
510*8975f5c5SAndroid Build Coastguard Worker
511*8975f5c5SAndroid Build Coastguard Worker std::vector<GLenum> modifiedAttachments;
512*8975f5c5SAndroid Build Coastguard Worker if (modifyInvalidateAttachmentsForEmulatedDefaultFBO(count, attachments, &modifiedAttachments))
513*8975f5c5SAndroid Build Coastguard Worker {
514*8975f5c5SAndroid Build Coastguard Worker finalAttachmentsPtr = modifiedAttachments.data();
515*8975f5c5SAndroid Build Coastguard Worker }
516*8975f5c5SAndroid Build Coastguard Worker
517*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
518*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
519*8975f5c5SAndroid Build Coastguard Worker
520*8975f5c5SAndroid Build Coastguard Worker // Since this function is just a hint and not available until OpenGL 4.3, only call it if it is
521*8975f5c5SAndroid Build Coastguard Worker // available.
522*8975f5c5SAndroid Build Coastguard Worker if (functions->invalidateSubFramebuffer)
523*8975f5c5SAndroid Build Coastguard Worker {
524*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
525*8975f5c5SAndroid Build Coastguard Worker functions->invalidateSubFramebuffer(GL_FRAMEBUFFER, static_cast<GLsizei>(count),
526*8975f5c5SAndroid Build Coastguard Worker finalAttachmentsPtr, area.x, area.y, area.width,
527*8975f5c5SAndroid Build Coastguard Worker area.height);
528*8975f5c5SAndroid Build Coastguard Worker }
529*8975f5c5SAndroid Build Coastguard Worker
530*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
531*8975f5c5SAndroid Build Coastguard Worker }
532*8975f5c5SAndroid Build Coastguard Worker
clear(const gl::Context * context,GLbitfield mask)533*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::clear(const gl::Context *context, GLbitfield mask)
534*8975f5c5SAndroid Build Coastguard Worker {
535*8975f5c5SAndroid Build Coastguard Worker ContextGL *contextGL = GetImplAs<ContextGL>(context);
536*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
537*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
538*8975f5c5SAndroid Build Coastguard Worker
539*8975f5c5SAndroid Build Coastguard Worker syncClearState(context, mask);
540*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
541*8975f5c5SAndroid Build Coastguard Worker
542*8975f5c5SAndroid Build Coastguard Worker if (!RequiresMultiviewClear(mState, context->getState().isScissorTestEnabled()))
543*8975f5c5SAndroid Build Coastguard Worker {
544*8975f5c5SAndroid Build Coastguard Worker functions->clear(mask);
545*8975f5c5SAndroid Build Coastguard Worker }
546*8975f5c5SAndroid Build Coastguard Worker else
547*8975f5c5SAndroid Build Coastguard Worker {
548*8975f5c5SAndroid Build Coastguard Worker ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
549*8975f5c5SAndroid Build Coastguard Worker multiviewClearer->clearMultiviewFBO(mState, context->getState().getScissor(),
550*8975f5c5SAndroid Build Coastguard Worker ClearMultiviewGL::ClearCommandType::Clear, mask,
551*8975f5c5SAndroid Build Coastguard Worker GL_NONE, 0, nullptr, 0.0f, 0);
552*8975f5c5SAndroid Build Coastguard Worker }
553*8975f5c5SAndroid Build Coastguard Worker
554*8975f5c5SAndroid Build Coastguard Worker contextGL->markWorkSubmitted();
555*8975f5c5SAndroid Build Coastguard Worker
556*8975f5c5SAndroid Build Coastguard Worker // Perform cheaper checks first, relying on short-circuiting
557*8975f5c5SAndroid Build Coastguard Worker if ((mask & GL_COLOR_BUFFER_BIT) != 0 && mState.getEnabledDrawBuffers().hasGaps() &&
558*8975f5c5SAndroid Build Coastguard Worker GetFeaturesGL(context).clearsWithGapsNeedFlush.enabled &&
559*8975f5c5SAndroid Build Coastguard Worker IsNonTrivialClearColor(context->getState().getColorClearValue().data()))
560*8975f5c5SAndroid Build Coastguard Worker {
561*8975f5c5SAndroid Build Coastguard Worker return contextGL->flush(context);
562*8975f5c5SAndroid Build Coastguard Worker }
563*8975f5c5SAndroid Build Coastguard Worker
564*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
565*8975f5c5SAndroid Build Coastguard Worker }
566*8975f5c5SAndroid Build Coastguard Worker
clearBufferfv(const gl::Context * context,GLenum buffer,GLint drawbuffer,const GLfloat * values)567*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::clearBufferfv(const gl::Context *context,
568*8975f5c5SAndroid Build Coastguard Worker GLenum buffer,
569*8975f5c5SAndroid Build Coastguard Worker GLint drawbuffer,
570*8975f5c5SAndroid Build Coastguard Worker const GLfloat *values)
571*8975f5c5SAndroid Build Coastguard Worker {
572*8975f5c5SAndroid Build Coastguard Worker ContextGL *contextGL = GetImplAs<ContextGL>(context);
573*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
574*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
575*8975f5c5SAndroid Build Coastguard Worker
576*8975f5c5SAndroid Build Coastguard Worker syncClearBufferState(context, buffer, drawbuffer);
577*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
578*8975f5c5SAndroid Build Coastguard Worker
579*8975f5c5SAndroid Build Coastguard Worker if (!RequiresMultiviewClear(mState, context->getState().isScissorTestEnabled()))
580*8975f5c5SAndroid Build Coastguard Worker {
581*8975f5c5SAndroid Build Coastguard Worker functions->clearBufferfv(buffer, drawbuffer, values);
582*8975f5c5SAndroid Build Coastguard Worker }
583*8975f5c5SAndroid Build Coastguard Worker else
584*8975f5c5SAndroid Build Coastguard Worker {
585*8975f5c5SAndroid Build Coastguard Worker ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
586*8975f5c5SAndroid Build Coastguard Worker multiviewClearer->clearMultiviewFBO(mState, context->getState().getScissor(),
587*8975f5c5SAndroid Build Coastguard Worker ClearMultiviewGL::ClearCommandType::ClearBufferfv,
588*8975f5c5SAndroid Build Coastguard Worker static_cast<GLbitfield>(0u), buffer, drawbuffer,
589*8975f5c5SAndroid Build Coastguard Worker reinterpret_cast<const uint8_t *>(values), 0.0f, 0);
590*8975f5c5SAndroid Build Coastguard Worker }
591*8975f5c5SAndroid Build Coastguard Worker
592*8975f5c5SAndroid Build Coastguard Worker contextGL->markWorkSubmitted();
593*8975f5c5SAndroid Build Coastguard Worker
594*8975f5c5SAndroid Build Coastguard Worker // Perform cheaper checks first, relying on short-circuiting
595*8975f5c5SAndroid Build Coastguard Worker if (buffer == GL_COLOR && mState.getEnabledDrawBuffers().hasGaps() &&
596*8975f5c5SAndroid Build Coastguard Worker GetFeaturesGL(context).clearsWithGapsNeedFlush.enabled && IsNonTrivialClearColor(values))
597*8975f5c5SAndroid Build Coastguard Worker {
598*8975f5c5SAndroid Build Coastguard Worker return contextGL->flush(context);
599*8975f5c5SAndroid Build Coastguard Worker }
600*8975f5c5SAndroid Build Coastguard Worker
601*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
602*8975f5c5SAndroid Build Coastguard Worker }
603*8975f5c5SAndroid Build Coastguard Worker
clearBufferuiv(const gl::Context * context,GLenum buffer,GLint drawbuffer,const GLuint * values)604*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::clearBufferuiv(const gl::Context *context,
605*8975f5c5SAndroid Build Coastguard Worker GLenum buffer,
606*8975f5c5SAndroid Build Coastguard Worker GLint drawbuffer,
607*8975f5c5SAndroid Build Coastguard Worker const GLuint *values)
608*8975f5c5SAndroid Build Coastguard Worker {
609*8975f5c5SAndroid Build Coastguard Worker ContextGL *contextGL = GetImplAs<ContextGL>(context);
610*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
611*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
612*8975f5c5SAndroid Build Coastguard Worker
613*8975f5c5SAndroid Build Coastguard Worker syncClearBufferState(context, buffer, drawbuffer);
614*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
615*8975f5c5SAndroid Build Coastguard Worker
616*8975f5c5SAndroid Build Coastguard Worker if (!RequiresMultiviewClear(mState, context->getState().isScissorTestEnabled()))
617*8975f5c5SAndroid Build Coastguard Worker {
618*8975f5c5SAndroid Build Coastguard Worker functions->clearBufferuiv(buffer, drawbuffer, values);
619*8975f5c5SAndroid Build Coastguard Worker }
620*8975f5c5SAndroid Build Coastguard Worker else
621*8975f5c5SAndroid Build Coastguard Worker {
622*8975f5c5SAndroid Build Coastguard Worker ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
623*8975f5c5SAndroid Build Coastguard Worker multiviewClearer->clearMultiviewFBO(mState, context->getState().getScissor(),
624*8975f5c5SAndroid Build Coastguard Worker ClearMultiviewGL::ClearCommandType::ClearBufferuiv,
625*8975f5c5SAndroid Build Coastguard Worker static_cast<GLbitfield>(0u), buffer, drawbuffer,
626*8975f5c5SAndroid Build Coastguard Worker reinterpret_cast<const uint8_t *>(values), 0.0f, 0);
627*8975f5c5SAndroid Build Coastguard Worker }
628*8975f5c5SAndroid Build Coastguard Worker
629*8975f5c5SAndroid Build Coastguard Worker contextGL->markWorkSubmitted();
630*8975f5c5SAndroid Build Coastguard Worker
631*8975f5c5SAndroid Build Coastguard Worker // Perform cheaper checks first, relying on short-circuiting
632*8975f5c5SAndroid Build Coastguard Worker if (mState.getEnabledDrawBuffers().hasGaps() &&
633*8975f5c5SAndroid Build Coastguard Worker GetFeaturesGL(context).clearsWithGapsNeedFlush.enabled && IsNonTrivialClearColor(values))
634*8975f5c5SAndroid Build Coastguard Worker {
635*8975f5c5SAndroid Build Coastguard Worker return contextGL->flush(context);
636*8975f5c5SAndroid Build Coastguard Worker }
637*8975f5c5SAndroid Build Coastguard Worker
638*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
639*8975f5c5SAndroid Build Coastguard Worker }
640*8975f5c5SAndroid Build Coastguard Worker
clearBufferiv(const gl::Context * context,GLenum buffer,GLint drawbuffer,const GLint * values)641*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::clearBufferiv(const gl::Context *context,
642*8975f5c5SAndroid Build Coastguard Worker GLenum buffer,
643*8975f5c5SAndroid Build Coastguard Worker GLint drawbuffer,
644*8975f5c5SAndroid Build Coastguard Worker const GLint *values)
645*8975f5c5SAndroid Build Coastguard Worker {
646*8975f5c5SAndroid Build Coastguard Worker ContextGL *contextGL = GetImplAs<ContextGL>(context);
647*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
648*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
649*8975f5c5SAndroid Build Coastguard Worker
650*8975f5c5SAndroid Build Coastguard Worker syncClearBufferState(context, buffer, drawbuffer);
651*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
652*8975f5c5SAndroid Build Coastguard Worker
653*8975f5c5SAndroid Build Coastguard Worker if (!RequiresMultiviewClear(mState, context->getState().isScissorTestEnabled()))
654*8975f5c5SAndroid Build Coastguard Worker {
655*8975f5c5SAndroid Build Coastguard Worker functions->clearBufferiv(buffer, drawbuffer, values);
656*8975f5c5SAndroid Build Coastguard Worker }
657*8975f5c5SAndroid Build Coastguard Worker else
658*8975f5c5SAndroid Build Coastguard Worker {
659*8975f5c5SAndroid Build Coastguard Worker ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
660*8975f5c5SAndroid Build Coastguard Worker multiviewClearer->clearMultiviewFBO(mState, context->getState().getScissor(),
661*8975f5c5SAndroid Build Coastguard Worker ClearMultiviewGL::ClearCommandType::ClearBufferiv,
662*8975f5c5SAndroid Build Coastguard Worker static_cast<GLbitfield>(0u), buffer, drawbuffer,
663*8975f5c5SAndroid Build Coastguard Worker reinterpret_cast<const uint8_t *>(values), 0.0f, 0);
664*8975f5c5SAndroid Build Coastguard Worker }
665*8975f5c5SAndroid Build Coastguard Worker
666*8975f5c5SAndroid Build Coastguard Worker contextGL->markWorkSubmitted();
667*8975f5c5SAndroid Build Coastguard Worker
668*8975f5c5SAndroid Build Coastguard Worker // Perform cheaper checks first, relying on short-circuiting
669*8975f5c5SAndroid Build Coastguard Worker if (buffer == GL_COLOR && mState.getEnabledDrawBuffers().hasGaps() &&
670*8975f5c5SAndroid Build Coastguard Worker GetFeaturesGL(context).clearsWithGapsNeedFlush.enabled &&
671*8975f5c5SAndroid Build Coastguard Worker IsNonTrivialClearColor(reinterpret_cast<const GLuint *>(values)))
672*8975f5c5SAndroid Build Coastguard Worker {
673*8975f5c5SAndroid Build Coastguard Worker return contextGL->flush(context);
674*8975f5c5SAndroid Build Coastguard Worker }
675*8975f5c5SAndroid Build Coastguard Worker
676*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
677*8975f5c5SAndroid Build Coastguard Worker }
678*8975f5c5SAndroid Build Coastguard Worker
clearBufferfi(const gl::Context * context,GLenum buffer,GLint drawbuffer,GLfloat depth,GLint stencil)679*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::clearBufferfi(const gl::Context *context,
680*8975f5c5SAndroid Build Coastguard Worker GLenum buffer,
681*8975f5c5SAndroid Build Coastguard Worker GLint drawbuffer,
682*8975f5c5SAndroid Build Coastguard Worker GLfloat depth,
683*8975f5c5SAndroid Build Coastguard Worker GLint stencil)
684*8975f5c5SAndroid Build Coastguard Worker {
685*8975f5c5SAndroid Build Coastguard Worker ContextGL *contextGL = GetImplAs<ContextGL>(context);
686*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
687*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
688*8975f5c5SAndroid Build Coastguard Worker
689*8975f5c5SAndroid Build Coastguard Worker syncClearBufferState(context, buffer, drawbuffer);
690*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
691*8975f5c5SAndroid Build Coastguard Worker
692*8975f5c5SAndroid Build Coastguard Worker if (!RequiresMultiviewClear(mState, context->getState().isScissorTestEnabled()))
693*8975f5c5SAndroid Build Coastguard Worker {
694*8975f5c5SAndroid Build Coastguard Worker functions->clearBufferfi(buffer, drawbuffer, depth, stencil);
695*8975f5c5SAndroid Build Coastguard Worker }
696*8975f5c5SAndroid Build Coastguard Worker else
697*8975f5c5SAndroid Build Coastguard Worker {
698*8975f5c5SAndroid Build Coastguard Worker ClearMultiviewGL *multiviewClearer = GetMultiviewClearer(context);
699*8975f5c5SAndroid Build Coastguard Worker multiviewClearer->clearMultiviewFBO(mState, context->getState().getScissor(),
700*8975f5c5SAndroid Build Coastguard Worker ClearMultiviewGL::ClearCommandType::ClearBufferfi,
701*8975f5c5SAndroid Build Coastguard Worker static_cast<GLbitfield>(0u), buffer, drawbuffer,
702*8975f5c5SAndroid Build Coastguard Worker nullptr, depth, stencil);
703*8975f5c5SAndroid Build Coastguard Worker }
704*8975f5c5SAndroid Build Coastguard Worker
705*8975f5c5SAndroid Build Coastguard Worker contextGL->markWorkSubmitted();
706*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
707*8975f5c5SAndroid Build Coastguard Worker }
708*8975f5c5SAndroid Build Coastguard Worker
readPixels(const gl::Context * context,const gl::Rectangle & area,GLenum format,GLenum type,const gl::PixelPackState & pack,gl::Buffer * packBuffer,void * pixels)709*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::readPixels(const gl::Context *context,
710*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &area,
711*8975f5c5SAndroid Build Coastguard Worker GLenum format,
712*8975f5c5SAndroid Build Coastguard Worker GLenum type,
713*8975f5c5SAndroid Build Coastguard Worker const gl::PixelPackState &pack,
714*8975f5c5SAndroid Build Coastguard Worker gl::Buffer *packBuffer,
715*8975f5c5SAndroid Build Coastguard Worker void *pixels)
716*8975f5c5SAndroid Build Coastguard Worker {
717*8975f5c5SAndroid Build Coastguard Worker ContextGL *contextGL = GetImplAs<ContextGL>(context);
718*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
719*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
720*8975f5c5SAndroid Build Coastguard Worker const angle::FeaturesGL &features = GetFeaturesGL(context);
721*8975f5c5SAndroid Build Coastguard Worker gl::PixelPackState packState = pack;
722*8975f5c5SAndroid Build Coastguard Worker
723*8975f5c5SAndroid Build Coastguard Worker // Clip read area to framebuffer.
724*8975f5c5SAndroid Build Coastguard Worker const auto *readAttachment = mState.getReadPixelsAttachment(format);
725*8975f5c5SAndroid Build Coastguard Worker const gl::Extents fbSize = readAttachment->getSize();
726*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height);
727*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle clippedArea;
728*8975f5c5SAndroid Build Coastguard Worker if (!ClipRectangle(area, fbRect, &clippedArea))
729*8975f5c5SAndroid Build Coastguard Worker {
730*8975f5c5SAndroid Build Coastguard Worker // nothing to read
731*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
732*8975f5c5SAndroid Build Coastguard Worker }
733*8975f5c5SAndroid Build Coastguard Worker
734*8975f5c5SAndroid Build Coastguard Worker GLenum attachmentReadFormat =
735*8975f5c5SAndroid Build Coastguard Worker readAttachment->getFormat().info->getReadPixelsFormat(context->getExtensions());
736*8975f5c5SAndroid Build Coastguard Worker nativegl::ReadPixelsFormat readPixelsFormat =
737*8975f5c5SAndroid Build Coastguard Worker nativegl::GetReadPixelsFormat(functions, features, attachmentReadFormat, format, type);
738*8975f5c5SAndroid Build Coastguard Worker GLenum readFormat = readPixelsFormat.format;
739*8975f5c5SAndroid Build Coastguard Worker GLenum readType = readPixelsFormat.type;
740*8975f5c5SAndroid Build Coastguard Worker if (features.readPixelsUsingImplementationColorReadFormatForNorm16.enabled &&
741*8975f5c5SAndroid Build Coastguard Worker readType == GL_UNSIGNED_SHORT)
742*8975f5c5SAndroid Build Coastguard Worker {
743*8975f5c5SAndroid Build Coastguard Worker ANGLE_CHECK(contextGL, IsValidUnsignedShortReadPixelsFormat(readFormat, context),
744*8975f5c5SAndroid Build Coastguard Worker "glReadPixels: GL_IMPLEMENTATION_COLOR_READ_FORMAT advertised by the driver is "
745*8975f5c5SAndroid Build Coastguard Worker "not handled by RGBA16 readPixels workaround.",
746*8975f5c5SAndroid Build Coastguard Worker GL_INVALID_OPERATION);
747*8975f5c5SAndroid Build Coastguard Worker }
748*8975f5c5SAndroid Build Coastguard Worker
749*8975f5c5SAndroid Build Coastguard Worker GLenum framebufferTarget =
750*8975f5c5SAndroid Build Coastguard Worker stateManager->getHasSeparateFramebufferBindings() ? GL_READ_FRAMEBUFFER : GL_FRAMEBUFFER;
751*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(framebufferTarget, mFramebufferID);
752*8975f5c5SAndroid Build Coastguard Worker
753*8975f5c5SAndroid Build Coastguard Worker bool useOverlappingRowsWorkaround = features.packOverlappingRowsSeparatelyPackBuffer.enabled &&
754*8975f5c5SAndroid Build Coastguard Worker packBuffer && packState.rowLength != 0 &&
755*8975f5c5SAndroid Build Coastguard Worker packState.rowLength < clippedArea.width;
756*8975f5c5SAndroid Build Coastguard Worker
757*8975f5c5SAndroid Build Coastguard Worker GLubyte *outPtr = static_cast<GLubyte *>(pixels);
758*8975f5c5SAndroid Build Coastguard Worker int leftClip = clippedArea.x - area.x;
759*8975f5c5SAndroid Build Coastguard Worker int topClip = clippedArea.y - area.y;
760*8975f5c5SAndroid Build Coastguard Worker if (leftClip || topClip)
761*8975f5c5SAndroid Build Coastguard Worker {
762*8975f5c5SAndroid Build Coastguard Worker // Adjust destination to match portion clipped off left and/or top.
763*8975f5c5SAndroid Build Coastguard Worker const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(readFormat, readType);
764*8975f5c5SAndroid Build Coastguard Worker
765*8975f5c5SAndroid Build Coastguard Worker GLuint rowBytes = 0;
766*8975f5c5SAndroid Build Coastguard Worker ANGLE_CHECK_GL_MATH(contextGL,
767*8975f5c5SAndroid Build Coastguard Worker glFormat.computeRowPitch(readType, area.width, packState.alignment,
768*8975f5c5SAndroid Build Coastguard Worker packState.rowLength, &rowBytes));
769*8975f5c5SAndroid Build Coastguard Worker outPtr += leftClip * glFormat.pixelBytes + topClip * rowBytes;
770*8975f5c5SAndroid Build Coastguard Worker }
771*8975f5c5SAndroid Build Coastguard Worker
772*8975f5c5SAndroid Build Coastguard Worker if (packState.rowLength == 0 && clippedArea.width != area.width)
773*8975f5c5SAndroid Build Coastguard Worker {
774*8975f5c5SAndroid Build Coastguard Worker // No rowLength was specified so it will derive from read width, but clipping changed the
775*8975f5c5SAndroid Build Coastguard Worker // read width. Use the original width so we fill the user's buffer as they intended.
776*8975f5c5SAndroid Build Coastguard Worker packState.rowLength = area.width;
777*8975f5c5SAndroid Build Coastguard Worker }
778*8975f5c5SAndroid Build Coastguard Worker
779*8975f5c5SAndroid Build Coastguard Worker // We want to use rowLength, but that might not be supported.
780*8975f5c5SAndroid Build Coastguard Worker bool cannotSetDesiredRowLength =
781*8975f5c5SAndroid Build Coastguard Worker packState.rowLength && !GetImplAs<ContextGL>(context)->getNativeExtensions().packSubimageNV;
782*8975f5c5SAndroid Build Coastguard Worker
783*8975f5c5SAndroid Build Coastguard Worker bool usePackSkipWorkaround = features.emulatePackSkipRowsAndPackSkipPixels.enabled &&
784*8975f5c5SAndroid Build Coastguard Worker (packState.skipRows != 0 || packState.skipPixels != 0);
785*8975f5c5SAndroid Build Coastguard Worker
786*8975f5c5SAndroid Build Coastguard Worker if (cannotSetDesiredRowLength || useOverlappingRowsWorkaround || usePackSkipWorkaround)
787*8975f5c5SAndroid Build Coastguard Worker {
788*8975f5c5SAndroid Build Coastguard Worker return readPixelsRowByRow(context, clippedArea, format, readFormat, readType, packState,
789*8975f5c5SAndroid Build Coastguard Worker outPtr);
790*8975f5c5SAndroid Build Coastguard Worker }
791*8975f5c5SAndroid Build Coastguard Worker
792*8975f5c5SAndroid Build Coastguard Worker bool useLastRowPaddingWorkaround = false;
793*8975f5c5SAndroid Build Coastguard Worker if (features.packLastRowSeparatelyForPaddingInclusion.enabled)
794*8975f5c5SAndroid Build Coastguard Worker {
795*8975f5c5SAndroid Build Coastguard Worker ANGLE_TRY(ShouldApplyLastRowPaddingWorkaround(
796*8975f5c5SAndroid Build Coastguard Worker contextGL, gl::Extents(clippedArea.width, clippedArea.height, 1), packState, packBuffer,
797*8975f5c5SAndroid Build Coastguard Worker readFormat, readType, false, outPtr, &useLastRowPaddingWorkaround));
798*8975f5c5SAndroid Build Coastguard Worker }
799*8975f5c5SAndroid Build Coastguard Worker
800*8975f5c5SAndroid Build Coastguard Worker return readPixelsAllAtOnce(context, clippedArea, format, readFormat, readType, packState,
801*8975f5c5SAndroid Build Coastguard Worker outPtr, useLastRowPaddingWorkaround);
802*8975f5c5SAndroid Build Coastguard Worker }
803*8975f5c5SAndroid Build Coastguard Worker
blit(const gl::Context * context,const gl::Rectangle & sourceArea,const gl::Rectangle & destArea,GLbitfield mask,GLenum filter)804*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::blit(const gl::Context *context,
805*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &sourceArea,
806*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &destArea,
807*8975f5c5SAndroid Build Coastguard Worker GLbitfield mask,
808*8975f5c5SAndroid Build Coastguard Worker GLenum filter)
809*8975f5c5SAndroid Build Coastguard Worker {
810*8975f5c5SAndroid Build Coastguard Worker ContextGL *contextGL = GetImplAs<ContextGL>(context);
811*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
812*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
813*8975f5c5SAndroid Build Coastguard Worker const angle::FeaturesGL &features = GetFeaturesGL(context);
814*8975f5c5SAndroid Build Coastguard Worker
815*8975f5c5SAndroid Build Coastguard Worker const Framebuffer *sourceFramebuffer = context->getState().getReadFramebuffer();
816*8975f5c5SAndroid Build Coastguard Worker const Framebuffer *destFramebuffer = context->getState().getDrawFramebuffer();
817*8975f5c5SAndroid Build Coastguard Worker
818*8975f5c5SAndroid Build Coastguard Worker const FramebufferAttachment *colorReadAttachment = sourceFramebuffer->getReadColorAttachment();
819*8975f5c5SAndroid Build Coastguard Worker
820*8975f5c5SAndroid Build Coastguard Worker GLsizei readAttachmentSamples = 0;
821*8975f5c5SAndroid Build Coastguard Worker if (colorReadAttachment != nullptr)
822*8975f5c5SAndroid Build Coastguard Worker {
823*8975f5c5SAndroid Build Coastguard Worker // Blitting requires that the textures be single sampled. getSamples will return
824*8975f5c5SAndroid Build Coastguard Worker // emulated sample number, but the EXT_multisampled_render_to_texture extension will
825*8975f5c5SAndroid Build Coastguard Worker // take care of resolving the texture, so even if emulated samples > 0, we should still
826*8975f5c5SAndroid Build Coastguard Worker // be able to blit as long as the underlying resource samples is single sampled.
827*8975f5c5SAndroid Build Coastguard Worker readAttachmentSamples = colorReadAttachment->getResourceSamples();
828*8975f5c5SAndroid Build Coastguard Worker }
829*8975f5c5SAndroid Build Coastguard Worker
830*8975f5c5SAndroid Build Coastguard Worker bool needManualColorBlit = false;
831*8975f5c5SAndroid Build Coastguard Worker
832*8975f5c5SAndroid Build Coastguard Worker // TODO(cwallez) when the filter is LINEAR and both source and destination are SRGB, we
833*8975f5c5SAndroid Build Coastguard Worker // could avoid doing a manual blit.
834*8975f5c5SAndroid Build Coastguard Worker
835*8975f5c5SAndroid Build Coastguard Worker // Prior to OpenGL 4.4 BlitFramebuffer (section 18.3.1 of GL 4.3 core profile) reads:
836*8975f5c5SAndroid Build Coastguard Worker // When values are taken from the read buffer, no linearization is performed, even
837*8975f5c5SAndroid Build Coastguard Worker // if the format of the buffer is SRGB.
838*8975f5c5SAndroid Build Coastguard Worker // Starting from OpenGL 4.4 (section 18.3.1) it reads:
839*8975f5c5SAndroid Build Coastguard Worker // When values are taken from the read buffer, if FRAMEBUFFER_SRGB is enabled and the
840*8975f5c5SAndroid Build Coastguard Worker // value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the framebuffer attachment
841*8975f5c5SAndroid Build Coastguard Worker // corresponding to the read buffer is SRGB, the red, green, and blue components are
842*8975f5c5SAndroid Build Coastguard Worker // converted from the non-linear sRGB color space according [...].
843*8975f5c5SAndroid Build Coastguard Worker {
844*8975f5c5SAndroid Build Coastguard Worker bool sourceSRGB =
845*8975f5c5SAndroid Build Coastguard Worker colorReadAttachment != nullptr && colorReadAttachment->getColorEncoding() == GL_SRGB;
846*8975f5c5SAndroid Build Coastguard Worker needManualColorBlit =
847*8975f5c5SAndroid Build Coastguard Worker needManualColorBlit || (sourceSRGB && functions->isAtMostGL(gl::Version(4, 3)));
848*8975f5c5SAndroid Build Coastguard Worker }
849*8975f5c5SAndroid Build Coastguard Worker
850*8975f5c5SAndroid Build Coastguard Worker // Prior to OpenGL 4.2 BlitFramebuffer (section 4.3.2 of GL 4.1 core profile) reads:
851*8975f5c5SAndroid Build Coastguard Worker // Blit operations bypass the fragment pipeline. The only fragment operations which
852*8975f5c5SAndroid Build Coastguard Worker // affect a blit are the pixel ownership test and scissor test.
853*8975f5c5SAndroid Build Coastguard Worker // Starting from OpenGL 4.2 (section 4.3.2) it reads:
854*8975f5c5SAndroid Build Coastguard Worker // When values are written to the draw buffers, blit operations bypass the fragment
855*8975f5c5SAndroid Build Coastguard Worker // pipeline. The only fragment operations which affect a blit are the pixel ownership
856*8975f5c5SAndroid Build Coastguard Worker // test, the scissor test and sRGB conversion.
857*8975f5c5SAndroid Build Coastguard Worker if (!needManualColorBlit)
858*8975f5c5SAndroid Build Coastguard Worker {
859*8975f5c5SAndroid Build Coastguard Worker bool destSRGB = false;
860*8975f5c5SAndroid Build Coastguard Worker for (size_t i = 0; i < destFramebuffer->getDrawbufferStateCount(); ++i)
861*8975f5c5SAndroid Build Coastguard Worker {
862*8975f5c5SAndroid Build Coastguard Worker const FramebufferAttachment *attachment = destFramebuffer->getDrawBuffer(i);
863*8975f5c5SAndroid Build Coastguard Worker if (attachment && attachment->getColorEncoding() == GL_SRGB)
864*8975f5c5SAndroid Build Coastguard Worker {
865*8975f5c5SAndroid Build Coastguard Worker destSRGB = true;
866*8975f5c5SAndroid Build Coastguard Worker break;
867*8975f5c5SAndroid Build Coastguard Worker }
868*8975f5c5SAndroid Build Coastguard Worker }
869*8975f5c5SAndroid Build Coastguard Worker
870*8975f5c5SAndroid Build Coastguard Worker needManualColorBlit =
871*8975f5c5SAndroid Build Coastguard Worker needManualColorBlit || (destSRGB && functions->isAtMostGL(gl::Version(4, 1)));
872*8975f5c5SAndroid Build Coastguard Worker }
873*8975f5c5SAndroid Build Coastguard Worker
874*8975f5c5SAndroid Build Coastguard Worker // If the destination has an emulated alpha channel, we need to blit with a shader with alpha
875*8975f5c5SAndroid Build Coastguard Worker // writes disabled.
876*8975f5c5SAndroid Build Coastguard Worker if (mHasEmulatedAlphaAttachment)
877*8975f5c5SAndroid Build Coastguard Worker {
878*8975f5c5SAndroid Build Coastguard Worker needManualColorBlit = true;
879*8975f5c5SAndroid Build Coastguard Worker }
880*8975f5c5SAndroid Build Coastguard Worker
881*8975f5c5SAndroid Build Coastguard Worker // Enable FRAMEBUFFER_SRGB if needed
882*8975f5c5SAndroid Build Coastguard Worker stateManager->setFramebufferSRGBEnabledForFramebuffer(context, true, this);
883*8975f5c5SAndroid Build Coastguard Worker
884*8975f5c5SAndroid Build Coastguard Worker GLenum blitMask = mask;
885*8975f5c5SAndroid Build Coastguard Worker if (needManualColorBlit && (mask & GL_COLOR_BUFFER_BIT) && readAttachmentSamples <= 1)
886*8975f5c5SAndroid Build Coastguard Worker {
887*8975f5c5SAndroid Build Coastguard Worker BlitGL *blitter = GetBlitGL(context);
888*8975f5c5SAndroid Build Coastguard Worker ANGLE_TRY(blitter->blitColorBufferWithShader(context, sourceFramebuffer, destFramebuffer,
889*8975f5c5SAndroid Build Coastguard Worker sourceArea, destArea, filter,
890*8975f5c5SAndroid Build Coastguard Worker !mHasEmulatedAlphaAttachment));
891*8975f5c5SAndroid Build Coastguard Worker blitMask &= ~GL_COLOR_BUFFER_BIT;
892*8975f5c5SAndroid Build Coastguard Worker }
893*8975f5c5SAndroid Build Coastguard Worker
894*8975f5c5SAndroid Build Coastguard Worker if (blitMask == 0)
895*8975f5c5SAndroid Build Coastguard Worker {
896*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
897*8975f5c5SAndroid Build Coastguard Worker }
898*8975f5c5SAndroid Build Coastguard Worker
899*8975f5c5SAndroid Build Coastguard Worker const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(sourceFramebuffer);
900*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID());
901*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferID);
902*8975f5c5SAndroid Build Coastguard Worker
903*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle finalSourceArea(sourceArea);
904*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle finalDestArea(destArea);
905*8975f5c5SAndroid Build Coastguard Worker
906*8975f5c5SAndroid Build Coastguard Worker if (features.adjustSrcDstRegionForBlitFramebuffer.enabled)
907*8975f5c5SAndroid Build Coastguard Worker {
908*8975f5c5SAndroid Build Coastguard Worker angle::Result result = adjustSrcDstRegion(context, finalSourceArea, finalDestArea,
909*8975f5c5SAndroid Build Coastguard Worker &finalSourceArea, &finalDestArea);
910*8975f5c5SAndroid Build Coastguard Worker if (result != angle::Result::Continue)
911*8975f5c5SAndroid Build Coastguard Worker {
912*8975f5c5SAndroid Build Coastguard Worker return result;
913*8975f5c5SAndroid Build Coastguard Worker }
914*8975f5c5SAndroid Build Coastguard Worker }
915*8975f5c5SAndroid Build Coastguard Worker if (features.clipSrcRegionForBlitFramebuffer.enabled)
916*8975f5c5SAndroid Build Coastguard Worker {
917*8975f5c5SAndroid Build Coastguard Worker angle::Result result = clipSrcRegion(context, finalSourceArea, finalDestArea,
918*8975f5c5SAndroid Build Coastguard Worker &finalSourceArea, &finalDestArea);
919*8975f5c5SAndroid Build Coastguard Worker if (result != angle::Result::Continue)
920*8975f5c5SAndroid Build Coastguard Worker {
921*8975f5c5SAndroid Build Coastguard Worker return result;
922*8975f5c5SAndroid Build Coastguard Worker }
923*8975f5c5SAndroid Build Coastguard Worker }
924*8975f5c5SAndroid Build Coastguard Worker
925*8975f5c5SAndroid Build Coastguard Worker functions->blitFramebuffer(finalSourceArea.x, finalSourceArea.y, finalSourceArea.x1(),
926*8975f5c5SAndroid Build Coastguard Worker finalSourceArea.y1(), finalDestArea.x, finalDestArea.y,
927*8975f5c5SAndroid Build Coastguard Worker finalDestArea.x1(), finalDestArea.y1(), blitMask, filter);
928*8975f5c5SAndroid Build Coastguard Worker
929*8975f5c5SAndroid Build Coastguard Worker contextGL->markWorkSubmitted();
930*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
931*8975f5c5SAndroid Build Coastguard Worker }
932*8975f5c5SAndroid Build Coastguard Worker
adjustSrcDstRegion(const gl::Context * context,const gl::Rectangle & sourceArea,const gl::Rectangle & destArea,gl::Rectangle * newSourceArea,gl::Rectangle * newDestArea)933*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::adjustSrcDstRegion(const gl::Context *context,
934*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &sourceArea,
935*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &destArea,
936*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle *newSourceArea,
937*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle *newDestArea)
938*8975f5c5SAndroid Build Coastguard Worker {
939*8975f5c5SAndroid Build Coastguard Worker BlitFramebufferBounds bounds = GetBlitFramebufferBounds(context, sourceArea, destArea);
940*8975f5c5SAndroid Build Coastguard Worker
941*8975f5c5SAndroid Build Coastguard Worker if (bounds.destRegion.width == 0 || bounds.sourceRegion.width == 0 ||
942*8975f5c5SAndroid Build Coastguard Worker bounds.destRegion.height == 0 || bounds.sourceRegion.height == 0)
943*8975f5c5SAndroid Build Coastguard Worker {
944*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Stop;
945*8975f5c5SAndroid Build Coastguard Worker }
946*8975f5c5SAndroid Build Coastguard Worker if (!ClipRectangle(bounds.destBounds, bounds.destRegion, nullptr))
947*8975f5c5SAndroid Build Coastguard Worker {
948*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Stop;
949*8975f5c5SAndroid Build Coastguard Worker }
950*8975f5c5SAndroid Build Coastguard Worker
951*8975f5c5SAndroid Build Coastguard Worker if (!bounds.destBounds.encloses(bounds.destRegion))
952*8975f5c5SAndroid Build Coastguard Worker {
953*8975f5c5SAndroid Build Coastguard Worker // destRegion is not within destBounds. We want to adjust it to a
954*8975f5c5SAndroid Build Coastguard Worker // reasonable size. This is done by halving the destRegion until it is at
955*8975f5c5SAndroid Build Coastguard Worker // most twice the size of the framebuffer. We cut it in half instead
956*8975f5c5SAndroid Build Coastguard Worker // of arbitrarily shrinking it to fit so that we don't end up with
957*8975f5c5SAndroid Build Coastguard Worker // non-power-of-two scale factors which could mess up pixel interpolation.
958*8975f5c5SAndroid Build Coastguard Worker // Naively clipping the dst rect and then proportionally sizing the
959*8975f5c5SAndroid Build Coastguard Worker // src rect yields incorrect results.
960*8975f5c5SAndroid Build Coastguard Worker
961*8975f5c5SAndroid Build Coastguard Worker GLuint destXHalvings = 0;
962*8975f5c5SAndroid Build Coastguard Worker GLuint destYHalvings = 0;
963*8975f5c5SAndroid Build Coastguard Worker GLint destOriginX = bounds.destRegion.x;
964*8975f5c5SAndroid Build Coastguard Worker GLint destOriginY = bounds.destRegion.y;
965*8975f5c5SAndroid Build Coastguard Worker
966*8975f5c5SAndroid Build Coastguard Worker GLint destClippedWidth = bounds.destRegion.width;
967*8975f5c5SAndroid Build Coastguard Worker while (destClippedWidth > 2 * bounds.destBounds.width)
968*8975f5c5SAndroid Build Coastguard Worker {
969*8975f5c5SAndroid Build Coastguard Worker destClippedWidth = destClippedWidth / 2;
970*8975f5c5SAndroid Build Coastguard Worker destXHalvings++;
971*8975f5c5SAndroid Build Coastguard Worker }
972*8975f5c5SAndroid Build Coastguard Worker
973*8975f5c5SAndroid Build Coastguard Worker GLint destClippedHeight = bounds.destRegion.height;
974*8975f5c5SAndroid Build Coastguard Worker while (destClippedHeight > 2 * bounds.destBounds.height)
975*8975f5c5SAndroid Build Coastguard Worker {
976*8975f5c5SAndroid Build Coastguard Worker destClippedHeight = destClippedHeight / 2;
977*8975f5c5SAndroid Build Coastguard Worker destYHalvings++;
978*8975f5c5SAndroid Build Coastguard Worker }
979*8975f5c5SAndroid Build Coastguard Worker
980*8975f5c5SAndroid Build Coastguard Worker // Before this block, we check that the two rectangles intersect.
981*8975f5c5SAndroid Build Coastguard Worker // Now, compute the location of a new region origin such that we use the
982*8975f5c5SAndroid Build Coastguard Worker // scaled dimensions but the new region has the same intersection as the
983*8975f5c5SAndroid Build Coastguard Worker // original region.
984*8975f5c5SAndroid Build Coastguard Worker
985*8975f5c5SAndroid Build Coastguard Worker GLint left = bounds.destRegion.x0();
986*8975f5c5SAndroid Build Coastguard Worker GLint right = bounds.destRegion.x1();
987*8975f5c5SAndroid Build Coastguard Worker GLint top = bounds.destRegion.y0();
988*8975f5c5SAndroid Build Coastguard Worker GLint bottom = bounds.destRegion.y1();
989*8975f5c5SAndroid Build Coastguard Worker
990*8975f5c5SAndroid Build Coastguard Worker GLint extraXOffset = 0;
991*8975f5c5SAndroid Build Coastguard Worker if (left >= 0 && left < bounds.destBounds.width)
992*8975f5c5SAndroid Build Coastguard Worker {
993*8975f5c5SAndroid Build Coastguard Worker // Left edge is in-bounds
994*8975f5c5SAndroid Build Coastguard Worker destOriginX = bounds.destRegion.x;
995*8975f5c5SAndroid Build Coastguard Worker }
996*8975f5c5SAndroid Build Coastguard Worker else if (right > 0 && right <= bounds.destBounds.width)
997*8975f5c5SAndroid Build Coastguard Worker {
998*8975f5c5SAndroid Build Coastguard Worker // Right edge is in-bounds
999*8975f5c5SAndroid Build Coastguard Worker destOriginX = right - destClippedWidth;
1000*8975f5c5SAndroid Build Coastguard Worker }
1001*8975f5c5SAndroid Build Coastguard Worker else
1002*8975f5c5SAndroid Build Coastguard Worker {
1003*8975f5c5SAndroid Build Coastguard Worker // Region completely spans bounds
1004*8975f5c5SAndroid Build Coastguard Worker extraXOffset = (bounds.destRegion.width - destClippedWidth) / 2;
1005*8975f5c5SAndroid Build Coastguard Worker destOriginX = bounds.destRegion.x + extraXOffset;
1006*8975f5c5SAndroid Build Coastguard Worker }
1007*8975f5c5SAndroid Build Coastguard Worker
1008*8975f5c5SAndroid Build Coastguard Worker GLint extraYOffset = 0;
1009*8975f5c5SAndroid Build Coastguard Worker if (top >= 0 && top < bounds.destBounds.height)
1010*8975f5c5SAndroid Build Coastguard Worker {
1011*8975f5c5SAndroid Build Coastguard Worker // Top edge is in-bounds
1012*8975f5c5SAndroid Build Coastguard Worker destOriginY = bounds.destRegion.y;
1013*8975f5c5SAndroid Build Coastguard Worker }
1014*8975f5c5SAndroid Build Coastguard Worker else if (bottom > 0 && bottom <= bounds.destBounds.height)
1015*8975f5c5SAndroid Build Coastguard Worker {
1016*8975f5c5SAndroid Build Coastguard Worker // Bottom edge is in-bounds
1017*8975f5c5SAndroid Build Coastguard Worker destOriginY = bottom - destClippedHeight;
1018*8975f5c5SAndroid Build Coastguard Worker }
1019*8975f5c5SAndroid Build Coastguard Worker else
1020*8975f5c5SAndroid Build Coastguard Worker {
1021*8975f5c5SAndroid Build Coastguard Worker // Region completely spans bounds
1022*8975f5c5SAndroid Build Coastguard Worker extraYOffset = (bounds.destRegion.height - destClippedHeight) / 2;
1023*8975f5c5SAndroid Build Coastguard Worker destOriginY = bounds.destRegion.y + extraYOffset;
1024*8975f5c5SAndroid Build Coastguard Worker }
1025*8975f5c5SAndroid Build Coastguard Worker
1026*8975f5c5SAndroid Build Coastguard Worker // Offsets from the bottom left corner of the original region to
1027*8975f5c5SAndroid Build Coastguard Worker // the bottom left corner of the clipped region.
1028*8975f5c5SAndroid Build Coastguard Worker // This value (after it is scaled) is the respective offset we will apply
1029*8975f5c5SAndroid Build Coastguard Worker // to the src origin.
1030*8975f5c5SAndroid Build Coastguard Worker
1031*8975f5c5SAndroid Build Coastguard Worker CheckedNumeric<GLuint> checkedXOffset(destOriginX - bounds.destRegion.x - extraXOffset / 2);
1032*8975f5c5SAndroid Build Coastguard Worker CheckedNumeric<GLuint> checkedYOffset(destOriginY - bounds.destRegion.y - extraYOffset / 2);
1033*8975f5c5SAndroid Build Coastguard Worker
1034*8975f5c5SAndroid Build Coastguard Worker // if X/Y is reversed, use the top/right out-of-bounds region to compute
1035*8975f5c5SAndroid Build Coastguard Worker // the origin offset instead of the left/bottom out-of-bounds region
1036*8975f5c5SAndroid Build Coastguard Worker if (bounds.xFlipped)
1037*8975f5c5SAndroid Build Coastguard Worker {
1038*8975f5c5SAndroid Build Coastguard Worker checkedXOffset =
1039*8975f5c5SAndroid Build Coastguard Worker (bounds.destRegion.x1() - (destOriginX + destClippedWidth) + extraXOffset / 2);
1040*8975f5c5SAndroid Build Coastguard Worker }
1041*8975f5c5SAndroid Build Coastguard Worker if (bounds.yFlipped)
1042*8975f5c5SAndroid Build Coastguard Worker {
1043*8975f5c5SAndroid Build Coastguard Worker checkedYOffset =
1044*8975f5c5SAndroid Build Coastguard Worker (bounds.destRegion.y1() - (destOriginY + destClippedHeight) + extraYOffset / 2);
1045*8975f5c5SAndroid Build Coastguard Worker }
1046*8975f5c5SAndroid Build Coastguard Worker
1047*8975f5c5SAndroid Build Coastguard Worker // These offsets should never overflow
1048*8975f5c5SAndroid Build Coastguard Worker GLuint xOffset, yOffset;
1049*8975f5c5SAndroid Build Coastguard Worker if (!checkedXOffset.AssignIfValid(&xOffset) || !checkedYOffset.AssignIfValid(&yOffset))
1050*8975f5c5SAndroid Build Coastguard Worker {
1051*8975f5c5SAndroid Build Coastguard Worker UNREACHABLE();
1052*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Stop;
1053*8975f5c5SAndroid Build Coastguard Worker }
1054*8975f5c5SAndroid Build Coastguard Worker
1055*8975f5c5SAndroid Build Coastguard Worker bounds.destRegion =
1056*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle(destOriginX, destOriginY, destClippedWidth, destClippedHeight);
1057*8975f5c5SAndroid Build Coastguard Worker
1058*8975f5c5SAndroid Build Coastguard Worker // Adjust the src region by the same factor
1059*8975f5c5SAndroid Build Coastguard Worker bounds.sourceRegion = gl::Rectangle(bounds.sourceRegion.x + (xOffset >> destXHalvings),
1060*8975f5c5SAndroid Build Coastguard Worker bounds.sourceRegion.y + (yOffset >> destYHalvings),
1061*8975f5c5SAndroid Build Coastguard Worker bounds.sourceRegion.width >> destXHalvings,
1062*8975f5c5SAndroid Build Coastguard Worker bounds.sourceRegion.height >> destYHalvings);
1063*8975f5c5SAndroid Build Coastguard Worker
1064*8975f5c5SAndroid Build Coastguard Worker // if the src was scaled to 0, set it to 1 so the src is non-empty
1065*8975f5c5SAndroid Build Coastguard Worker if (bounds.sourceRegion.width == 0)
1066*8975f5c5SAndroid Build Coastguard Worker {
1067*8975f5c5SAndroid Build Coastguard Worker bounds.sourceRegion.width = 1;
1068*8975f5c5SAndroid Build Coastguard Worker }
1069*8975f5c5SAndroid Build Coastguard Worker if (bounds.sourceRegion.height == 0)
1070*8975f5c5SAndroid Build Coastguard Worker {
1071*8975f5c5SAndroid Build Coastguard Worker bounds.sourceRegion.height = 1;
1072*8975f5c5SAndroid Build Coastguard Worker }
1073*8975f5c5SAndroid Build Coastguard Worker }
1074*8975f5c5SAndroid Build Coastguard Worker
1075*8975f5c5SAndroid Build Coastguard Worker if (!bounds.sourceBounds.encloses(bounds.sourceRegion))
1076*8975f5c5SAndroid Build Coastguard Worker {
1077*8975f5c5SAndroid Build Coastguard Worker // sourceRegion is not within sourceBounds. We want to adjust it to a
1078*8975f5c5SAndroid Build Coastguard Worker // reasonable size. This is done by halving the sourceRegion until it is at
1079*8975f5c5SAndroid Build Coastguard Worker // most twice the size of the framebuffer. We cut it in half instead
1080*8975f5c5SAndroid Build Coastguard Worker // of arbitrarily shrinking it to fit so that we don't end up with
1081*8975f5c5SAndroid Build Coastguard Worker // non-power-of-two scale factors which could mess up pixel interpolation.
1082*8975f5c5SAndroid Build Coastguard Worker // Naively clipping the source rect and then proportionally sizing the
1083*8975f5c5SAndroid Build Coastguard Worker // dest rect yields incorrect results.
1084*8975f5c5SAndroid Build Coastguard Worker
1085*8975f5c5SAndroid Build Coastguard Worker GLuint sourceXHalvings = 0;
1086*8975f5c5SAndroid Build Coastguard Worker GLuint sourceYHalvings = 0;
1087*8975f5c5SAndroid Build Coastguard Worker GLint sourceOriginX = bounds.sourceRegion.x;
1088*8975f5c5SAndroid Build Coastguard Worker GLint sourceOriginY = bounds.sourceRegion.y;
1089*8975f5c5SAndroid Build Coastguard Worker
1090*8975f5c5SAndroid Build Coastguard Worker GLint sourceClippedWidth = bounds.sourceRegion.width;
1091*8975f5c5SAndroid Build Coastguard Worker while (sourceClippedWidth > 2 * bounds.sourceBounds.width)
1092*8975f5c5SAndroid Build Coastguard Worker {
1093*8975f5c5SAndroid Build Coastguard Worker sourceClippedWidth = sourceClippedWidth / 2;
1094*8975f5c5SAndroid Build Coastguard Worker sourceXHalvings++;
1095*8975f5c5SAndroid Build Coastguard Worker }
1096*8975f5c5SAndroid Build Coastguard Worker
1097*8975f5c5SAndroid Build Coastguard Worker GLint sourceClippedHeight = bounds.sourceRegion.height;
1098*8975f5c5SAndroid Build Coastguard Worker while (sourceClippedHeight > 2 * bounds.sourceBounds.height)
1099*8975f5c5SAndroid Build Coastguard Worker {
1100*8975f5c5SAndroid Build Coastguard Worker sourceClippedHeight = sourceClippedHeight / 2;
1101*8975f5c5SAndroid Build Coastguard Worker sourceYHalvings++;
1102*8975f5c5SAndroid Build Coastguard Worker }
1103*8975f5c5SAndroid Build Coastguard Worker
1104*8975f5c5SAndroid Build Coastguard Worker // Before this block, we check that the two rectangles intersect.
1105*8975f5c5SAndroid Build Coastguard Worker // Now, compute the location of a new region origin such that we use the
1106*8975f5c5SAndroid Build Coastguard Worker // scaled dimensions but the new region has the same intersection as the
1107*8975f5c5SAndroid Build Coastguard Worker // original region.
1108*8975f5c5SAndroid Build Coastguard Worker
1109*8975f5c5SAndroid Build Coastguard Worker GLint left = bounds.sourceRegion.x0();
1110*8975f5c5SAndroid Build Coastguard Worker GLint right = bounds.sourceRegion.x1();
1111*8975f5c5SAndroid Build Coastguard Worker GLint top = bounds.sourceRegion.y0();
1112*8975f5c5SAndroid Build Coastguard Worker GLint bottom = bounds.sourceRegion.y1();
1113*8975f5c5SAndroid Build Coastguard Worker
1114*8975f5c5SAndroid Build Coastguard Worker GLint extraXOffset = 0;
1115*8975f5c5SAndroid Build Coastguard Worker if (left >= 0 && left < bounds.sourceBounds.width)
1116*8975f5c5SAndroid Build Coastguard Worker {
1117*8975f5c5SAndroid Build Coastguard Worker // Left edge is in-bounds
1118*8975f5c5SAndroid Build Coastguard Worker sourceOriginX = bounds.sourceRegion.x;
1119*8975f5c5SAndroid Build Coastguard Worker }
1120*8975f5c5SAndroid Build Coastguard Worker else if (right > 0 && right <= bounds.sourceBounds.width)
1121*8975f5c5SAndroid Build Coastguard Worker {
1122*8975f5c5SAndroid Build Coastguard Worker // Right edge is in-bounds
1123*8975f5c5SAndroid Build Coastguard Worker sourceOriginX = right - sourceClippedWidth;
1124*8975f5c5SAndroid Build Coastguard Worker }
1125*8975f5c5SAndroid Build Coastguard Worker else
1126*8975f5c5SAndroid Build Coastguard Worker {
1127*8975f5c5SAndroid Build Coastguard Worker // Region completely spans bounds
1128*8975f5c5SAndroid Build Coastguard Worker extraXOffset = (bounds.sourceRegion.width - sourceClippedWidth) / 2;
1129*8975f5c5SAndroid Build Coastguard Worker sourceOriginX = bounds.sourceRegion.x + extraXOffset;
1130*8975f5c5SAndroid Build Coastguard Worker }
1131*8975f5c5SAndroid Build Coastguard Worker
1132*8975f5c5SAndroid Build Coastguard Worker GLint extraYOffset = 0;
1133*8975f5c5SAndroid Build Coastguard Worker if (top >= 0 && top < bounds.sourceBounds.height)
1134*8975f5c5SAndroid Build Coastguard Worker {
1135*8975f5c5SAndroid Build Coastguard Worker // Top edge is in-bounds
1136*8975f5c5SAndroid Build Coastguard Worker sourceOriginY = bounds.sourceRegion.y;
1137*8975f5c5SAndroid Build Coastguard Worker }
1138*8975f5c5SAndroid Build Coastguard Worker else if (bottom > 0 && bottom <= bounds.sourceBounds.height)
1139*8975f5c5SAndroid Build Coastguard Worker {
1140*8975f5c5SAndroid Build Coastguard Worker // Bottom edge is in-bounds
1141*8975f5c5SAndroid Build Coastguard Worker sourceOriginY = bottom - sourceClippedHeight;
1142*8975f5c5SAndroid Build Coastguard Worker }
1143*8975f5c5SAndroid Build Coastguard Worker else
1144*8975f5c5SAndroid Build Coastguard Worker {
1145*8975f5c5SAndroid Build Coastguard Worker // Region completely spans bounds
1146*8975f5c5SAndroid Build Coastguard Worker extraYOffset = (bounds.sourceRegion.height - sourceClippedHeight) / 2;
1147*8975f5c5SAndroid Build Coastguard Worker sourceOriginY = bounds.sourceRegion.y + extraYOffset;
1148*8975f5c5SAndroid Build Coastguard Worker }
1149*8975f5c5SAndroid Build Coastguard Worker
1150*8975f5c5SAndroid Build Coastguard Worker // Offsets from the bottom left corner of the original region to
1151*8975f5c5SAndroid Build Coastguard Worker // the bottom left corner of the clipped region.
1152*8975f5c5SAndroid Build Coastguard Worker // This value (after it is scaled) is the respective offset we will apply
1153*8975f5c5SAndroid Build Coastguard Worker // to the dest origin.
1154*8975f5c5SAndroid Build Coastguard Worker
1155*8975f5c5SAndroid Build Coastguard Worker CheckedNumeric<GLuint> checkedXOffset(sourceOriginX - bounds.sourceRegion.x -
1156*8975f5c5SAndroid Build Coastguard Worker extraXOffset / 2);
1157*8975f5c5SAndroid Build Coastguard Worker CheckedNumeric<GLuint> checkedYOffset(sourceOriginY - bounds.sourceRegion.y -
1158*8975f5c5SAndroid Build Coastguard Worker extraYOffset / 2);
1159*8975f5c5SAndroid Build Coastguard Worker
1160*8975f5c5SAndroid Build Coastguard Worker // if X/Y is reversed, use the top/right out-of-bounds region to compute
1161*8975f5c5SAndroid Build Coastguard Worker // the origin offset instead of the left/bottom out-of-bounds region
1162*8975f5c5SAndroid Build Coastguard Worker if (bounds.xFlipped)
1163*8975f5c5SAndroid Build Coastguard Worker {
1164*8975f5c5SAndroid Build Coastguard Worker checkedXOffset = (bounds.sourceRegion.x1() - (sourceOriginX + sourceClippedWidth) +
1165*8975f5c5SAndroid Build Coastguard Worker extraXOffset / 2);
1166*8975f5c5SAndroid Build Coastguard Worker }
1167*8975f5c5SAndroid Build Coastguard Worker if (bounds.yFlipped)
1168*8975f5c5SAndroid Build Coastguard Worker {
1169*8975f5c5SAndroid Build Coastguard Worker checkedYOffset = (bounds.sourceRegion.y1() - (sourceOriginY + sourceClippedHeight) +
1170*8975f5c5SAndroid Build Coastguard Worker extraYOffset / 2);
1171*8975f5c5SAndroid Build Coastguard Worker }
1172*8975f5c5SAndroid Build Coastguard Worker
1173*8975f5c5SAndroid Build Coastguard Worker // These offsets should never overflow
1174*8975f5c5SAndroid Build Coastguard Worker GLuint xOffset, yOffset;
1175*8975f5c5SAndroid Build Coastguard Worker if (!checkedXOffset.AssignIfValid(&xOffset) || !checkedYOffset.AssignIfValid(&yOffset))
1176*8975f5c5SAndroid Build Coastguard Worker {
1177*8975f5c5SAndroid Build Coastguard Worker UNREACHABLE();
1178*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Stop;
1179*8975f5c5SAndroid Build Coastguard Worker }
1180*8975f5c5SAndroid Build Coastguard Worker
1181*8975f5c5SAndroid Build Coastguard Worker bounds.sourceRegion =
1182*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle(sourceOriginX, sourceOriginY, sourceClippedWidth, sourceClippedHeight);
1183*8975f5c5SAndroid Build Coastguard Worker
1184*8975f5c5SAndroid Build Coastguard Worker // Adjust the dest region by the same factor
1185*8975f5c5SAndroid Build Coastguard Worker bounds.destRegion = gl::Rectangle(bounds.destRegion.x + (xOffset >> sourceXHalvings),
1186*8975f5c5SAndroid Build Coastguard Worker bounds.destRegion.y + (yOffset >> sourceYHalvings),
1187*8975f5c5SAndroid Build Coastguard Worker bounds.destRegion.width >> sourceXHalvings,
1188*8975f5c5SAndroid Build Coastguard Worker bounds.destRegion.height >> sourceYHalvings);
1189*8975f5c5SAndroid Build Coastguard Worker }
1190*8975f5c5SAndroid Build Coastguard Worker // Set the src and dst endpoints. If they were previously flipped,
1191*8975f5c5SAndroid Build Coastguard Worker // set them as flipped.
1192*8975f5c5SAndroid Build Coastguard Worker *newSourceArea = bounds.sourceRegion.flip(sourceArea.isReversedX(), sourceArea.isReversedY());
1193*8975f5c5SAndroid Build Coastguard Worker *newDestArea = bounds.destRegion.flip(destArea.isReversedX(), destArea.isReversedY());
1194*8975f5c5SAndroid Build Coastguard Worker
1195*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
1196*8975f5c5SAndroid Build Coastguard Worker }
1197*8975f5c5SAndroid Build Coastguard Worker
clipSrcRegion(const gl::Context * context,const gl::Rectangle & sourceArea,const gl::Rectangle & destArea,gl::Rectangle * newSourceArea,gl::Rectangle * newDestArea)1198*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::clipSrcRegion(const gl::Context *context,
1199*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &sourceArea,
1200*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &destArea,
1201*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle *newSourceArea,
1202*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle *newDestArea)
1203*8975f5c5SAndroid Build Coastguard Worker {
1204*8975f5c5SAndroid Build Coastguard Worker BlitFramebufferBounds bounds = GetBlitFramebufferBounds(context, sourceArea, destArea);
1205*8975f5c5SAndroid Build Coastguard Worker
1206*8975f5c5SAndroid Build Coastguard Worker if (bounds.destRegion.width == 0 || bounds.sourceRegion.width == 0 ||
1207*8975f5c5SAndroid Build Coastguard Worker bounds.destRegion.height == 0 || bounds.sourceRegion.height == 0)
1208*8975f5c5SAndroid Build Coastguard Worker {
1209*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Stop;
1210*8975f5c5SAndroid Build Coastguard Worker }
1211*8975f5c5SAndroid Build Coastguard Worker if (!ClipRectangle(bounds.destBounds, bounds.destRegion, nullptr))
1212*8975f5c5SAndroid Build Coastguard Worker {
1213*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Stop;
1214*8975f5c5SAndroid Build Coastguard Worker }
1215*8975f5c5SAndroid Build Coastguard Worker
1216*8975f5c5SAndroid Build Coastguard Worker if (!bounds.sourceBounds.encloses(bounds.sourceRegion))
1217*8975f5c5SAndroid Build Coastguard Worker {
1218*8975f5c5SAndroid Build Coastguard Worker // If pixels lying outside the read framebuffer, adjust src region
1219*8975f5c5SAndroid Build Coastguard Worker // and dst region to appropriate in-bounds regions respectively.
1220*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle realSourceRegion;
1221*8975f5c5SAndroid Build Coastguard Worker if (!ClipRectangle(bounds.sourceRegion, bounds.sourceBounds, &realSourceRegion))
1222*8975f5c5SAndroid Build Coastguard Worker {
1223*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Stop;
1224*8975f5c5SAndroid Build Coastguard Worker }
1225*8975f5c5SAndroid Build Coastguard Worker GLuint xOffset = realSourceRegion.x - bounds.sourceRegion.x;
1226*8975f5c5SAndroid Build Coastguard Worker GLuint yOffset = realSourceRegion.y - bounds.sourceRegion.y;
1227*8975f5c5SAndroid Build Coastguard Worker
1228*8975f5c5SAndroid Build Coastguard Worker // if X/Y is reversed, use the top/right out-of-bounds region for mapping
1229*8975f5c5SAndroid Build Coastguard Worker // to dst region, instead of left/bottom out-of-bounds region for mapping.
1230*8975f5c5SAndroid Build Coastguard Worker if (bounds.xFlipped)
1231*8975f5c5SAndroid Build Coastguard Worker {
1232*8975f5c5SAndroid Build Coastguard Worker xOffset = bounds.sourceRegion.x1() - realSourceRegion.x1();
1233*8975f5c5SAndroid Build Coastguard Worker }
1234*8975f5c5SAndroid Build Coastguard Worker if (bounds.yFlipped)
1235*8975f5c5SAndroid Build Coastguard Worker {
1236*8975f5c5SAndroid Build Coastguard Worker yOffset = bounds.sourceRegion.y1() - realSourceRegion.y1();
1237*8975f5c5SAndroid Build Coastguard Worker }
1238*8975f5c5SAndroid Build Coastguard Worker
1239*8975f5c5SAndroid Build Coastguard Worker GLfloat destMappingWidth = static_cast<GLfloat>(realSourceRegion.width) *
1240*8975f5c5SAndroid Build Coastguard Worker bounds.destRegion.width / bounds.sourceRegion.width;
1241*8975f5c5SAndroid Build Coastguard Worker GLfloat destMappingHeight = static_cast<GLfloat>(realSourceRegion.height) *
1242*8975f5c5SAndroid Build Coastguard Worker bounds.destRegion.height / bounds.sourceRegion.height;
1243*8975f5c5SAndroid Build Coastguard Worker GLfloat destMappingXOffset =
1244*8975f5c5SAndroid Build Coastguard Worker static_cast<GLfloat>(xOffset) * bounds.destRegion.width / bounds.sourceRegion.width;
1245*8975f5c5SAndroid Build Coastguard Worker GLfloat destMappingYOffset =
1246*8975f5c5SAndroid Build Coastguard Worker static_cast<GLfloat>(yOffset) * bounds.destRegion.height / bounds.sourceRegion.height;
1247*8975f5c5SAndroid Build Coastguard Worker
1248*8975f5c5SAndroid Build Coastguard Worker GLuint destMappingX0 =
1249*8975f5c5SAndroid Build Coastguard Worker static_cast<GLuint>(std::round(bounds.destRegion.x + destMappingXOffset));
1250*8975f5c5SAndroid Build Coastguard Worker GLuint destMappingY0 =
1251*8975f5c5SAndroid Build Coastguard Worker static_cast<GLuint>(std::round(bounds.destRegion.y + destMappingYOffset));
1252*8975f5c5SAndroid Build Coastguard Worker
1253*8975f5c5SAndroid Build Coastguard Worker GLuint destMappingX1 = static_cast<GLuint>(
1254*8975f5c5SAndroid Build Coastguard Worker std::round(bounds.destRegion.x + destMappingXOffset + destMappingWidth));
1255*8975f5c5SAndroid Build Coastguard Worker GLuint destMappingY1 = static_cast<GLuint>(
1256*8975f5c5SAndroid Build Coastguard Worker std::round(bounds.destRegion.y + destMappingYOffset + destMappingHeight));
1257*8975f5c5SAndroid Build Coastguard Worker
1258*8975f5c5SAndroid Build Coastguard Worker bounds.destRegion =
1259*8975f5c5SAndroid Build Coastguard Worker gl::Rectangle(destMappingX0, destMappingY0, destMappingX1 - destMappingX0,
1260*8975f5c5SAndroid Build Coastguard Worker destMappingY1 - destMappingY0);
1261*8975f5c5SAndroid Build Coastguard Worker
1262*8975f5c5SAndroid Build Coastguard Worker bounds.sourceRegion = realSourceRegion;
1263*8975f5c5SAndroid Build Coastguard Worker }
1264*8975f5c5SAndroid Build Coastguard Worker // Set the src and dst endpoints. If they were previously flipped,
1265*8975f5c5SAndroid Build Coastguard Worker // set them as flipped.
1266*8975f5c5SAndroid Build Coastguard Worker *newSourceArea = bounds.sourceRegion.flip(sourceArea.isReversedX(), sourceArea.isReversedY());
1267*8975f5c5SAndroid Build Coastguard Worker *newDestArea = bounds.destRegion.flip(destArea.isReversedX(), destArea.isReversedY());
1268*8975f5c5SAndroid Build Coastguard Worker
1269*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
1270*8975f5c5SAndroid Build Coastguard Worker }
1271*8975f5c5SAndroid Build Coastguard Worker
getSamplePosition(const gl::Context * context,size_t index,GLfloat * xy) const1272*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::getSamplePosition(const gl::Context *context,
1273*8975f5c5SAndroid Build Coastguard Worker size_t index,
1274*8975f5c5SAndroid Build Coastguard Worker GLfloat *xy) const
1275*8975f5c5SAndroid Build Coastguard Worker {
1276*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
1277*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
1278*8975f5c5SAndroid Build Coastguard Worker
1279*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
1280*8975f5c5SAndroid Build Coastguard Worker functions->getMultisamplefv(GL_SAMPLE_POSITION, static_cast<GLuint>(index), xy);
1281*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
1282*8975f5c5SAndroid Build Coastguard Worker }
1283*8975f5c5SAndroid Build Coastguard Worker
shouldSyncStateBeforeCheckStatus() const1284*8975f5c5SAndroid Build Coastguard Worker bool FramebufferGL::shouldSyncStateBeforeCheckStatus() const
1285*8975f5c5SAndroid Build Coastguard Worker {
1286*8975f5c5SAndroid Build Coastguard Worker return true;
1287*8975f5c5SAndroid Build Coastguard Worker }
1288*8975f5c5SAndroid Build Coastguard Worker
checkStatus(const gl::Context * context) const1289*8975f5c5SAndroid Build Coastguard Worker gl::FramebufferStatus FramebufferGL::checkStatus(const gl::Context *context) const
1290*8975f5c5SAndroid Build Coastguard Worker {
1291*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
1292*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
1293*8975f5c5SAndroid Build Coastguard Worker
1294*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
1295*8975f5c5SAndroid Build Coastguard Worker GLenum status = functions->checkFramebufferStatus(GL_FRAMEBUFFER);
1296*8975f5c5SAndroid Build Coastguard Worker if (status != GL_FRAMEBUFFER_COMPLETE)
1297*8975f5c5SAndroid Build Coastguard Worker {
1298*8975f5c5SAndroid Build Coastguard Worker WARN() << "GL framebuffer returned incomplete: " << gl::FmtHex(status);
1299*8975f5c5SAndroid Build Coastguard Worker return gl::FramebufferStatus::Incomplete(GL_FRAMEBUFFER_UNSUPPORTED,
1300*8975f5c5SAndroid Build Coastguard Worker gl::err::kFramebufferIncompleteDriverUnsupported);
1301*8975f5c5SAndroid Build Coastguard Worker }
1302*8975f5c5SAndroid Build Coastguard Worker
1303*8975f5c5SAndroid Build Coastguard Worker return gl::FramebufferStatus::Complete();
1304*8975f5c5SAndroid Build Coastguard Worker }
1305*8975f5c5SAndroid Build Coastguard Worker
ensureAttachmentsInitialized(const gl::Context * context,const gl::DrawBufferMask & colorAttachments,bool depth,bool stencil)1306*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::ensureAttachmentsInitialized(
1307*8975f5c5SAndroid Build Coastguard Worker const gl::Context *context,
1308*8975f5c5SAndroid Build Coastguard Worker const gl::DrawBufferMask &colorAttachments,
1309*8975f5c5SAndroid Build Coastguard Worker bool depth,
1310*8975f5c5SAndroid Build Coastguard Worker bool stencil)
1311*8975f5c5SAndroid Build Coastguard Worker {
1312*8975f5c5SAndroid Build Coastguard Worker if (colorAttachments != getState().getEnabledDrawBuffers())
1313*8975f5c5SAndroid Build Coastguard Worker {
1314*8975f5c5SAndroid Build Coastguard Worker // Fall back to the default implementation when there are gaps in the enabled draw buffers
1315*8975f5c5SAndroid Build Coastguard Worker // to avoid modifying the draw buffer state.
1316*8975f5c5SAndroid Build Coastguard Worker return FramebufferImpl::ensureAttachmentsInitialized(context, colorAttachments, depth,
1317*8975f5c5SAndroid Build Coastguard Worker stencil);
1318*8975f5c5SAndroid Build Coastguard Worker }
1319*8975f5c5SAndroid Build Coastguard Worker
1320*8975f5c5SAndroid Build Coastguard Worker BlitGL *blitter = GetBlitGL(context);
1321*8975f5c5SAndroid Build Coastguard Worker return blitter->clearFramebuffer(context, colorAttachments, depth, stencil, this);
1322*8975f5c5SAndroid Build Coastguard Worker }
1323*8975f5c5SAndroid Build Coastguard Worker
syncState(const gl::Context * context,GLenum binding,const gl::Framebuffer::DirtyBits & dirtyBits,gl::Command command)1324*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::syncState(const gl::Context *context,
1325*8975f5c5SAndroid Build Coastguard Worker GLenum binding,
1326*8975f5c5SAndroid Build Coastguard Worker const gl::Framebuffer::DirtyBits &dirtyBits,
1327*8975f5c5SAndroid Build Coastguard Worker gl::Command command)
1328*8975f5c5SAndroid Build Coastguard Worker {
1329*8975f5c5SAndroid Build Coastguard Worker // Don't need to sync state for the default FBO.
1330*8975f5c5SAndroid Build Coastguard Worker if (isDefault())
1331*8975f5c5SAndroid Build Coastguard Worker {
1332*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
1333*8975f5c5SAndroid Build Coastguard Worker }
1334*8975f5c5SAndroid Build Coastguard Worker
1335*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
1336*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
1337*8975f5c5SAndroid Build Coastguard Worker
1338*8975f5c5SAndroid Build Coastguard Worker stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
1339*8975f5c5SAndroid Build Coastguard Worker
1340*8975f5c5SAndroid Build Coastguard Worker // A pointer to one of the attachments for which the texture or the render buffer is not zero.
1341*8975f5c5SAndroid Build Coastguard Worker const FramebufferAttachment *attachment = nullptr;
1342*8975f5c5SAndroid Build Coastguard Worker
1343*8975f5c5SAndroid Build Coastguard Worker for (auto dirtyBit : dirtyBits)
1344*8975f5c5SAndroid Build Coastguard Worker {
1345*8975f5c5SAndroid Build Coastguard Worker switch (dirtyBit)
1346*8975f5c5SAndroid Build Coastguard Worker {
1347*8975f5c5SAndroid Build Coastguard Worker case Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT:
1348*8975f5c5SAndroid Build Coastguard Worker {
1349*8975f5c5SAndroid Build Coastguard Worker const FramebufferAttachment *newAttachment = mState.getDepthAttachment();
1350*8975f5c5SAndroid Build Coastguard Worker BindFramebufferAttachment(functions, GL_DEPTH_ATTACHMENT, newAttachment,
1351*8975f5c5SAndroid Build Coastguard Worker GetFeaturesGL(context));
1352*8975f5c5SAndroid Build Coastguard Worker if (newAttachment)
1353*8975f5c5SAndroid Build Coastguard Worker {
1354*8975f5c5SAndroid Build Coastguard Worker attachment = newAttachment;
1355*8975f5c5SAndroid Build Coastguard Worker }
1356*8975f5c5SAndroid Build Coastguard Worker break;
1357*8975f5c5SAndroid Build Coastguard Worker }
1358*8975f5c5SAndroid Build Coastguard Worker case Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT:
1359*8975f5c5SAndroid Build Coastguard Worker {
1360*8975f5c5SAndroid Build Coastguard Worker const FramebufferAttachment *newAttachment = mState.getStencilAttachment();
1361*8975f5c5SAndroid Build Coastguard Worker BindFramebufferAttachment(functions, GL_STENCIL_ATTACHMENT, newAttachment,
1362*8975f5c5SAndroid Build Coastguard Worker GetFeaturesGL(context));
1363*8975f5c5SAndroid Build Coastguard Worker if (newAttachment)
1364*8975f5c5SAndroid Build Coastguard Worker {
1365*8975f5c5SAndroid Build Coastguard Worker attachment = newAttachment;
1366*8975f5c5SAndroid Build Coastguard Worker }
1367*8975f5c5SAndroid Build Coastguard Worker break;
1368*8975f5c5SAndroid Build Coastguard Worker }
1369*8975f5c5SAndroid Build Coastguard Worker case Framebuffer::DIRTY_BIT_DRAW_BUFFERS:
1370*8975f5c5SAndroid Build Coastguard Worker {
1371*8975f5c5SAndroid Build Coastguard Worker const auto &drawBuffers = mState.getDrawBufferStates();
1372*8975f5c5SAndroid Build Coastguard Worker functions->drawBuffers(static_cast<GLsizei>(drawBuffers.size()),
1373*8975f5c5SAndroid Build Coastguard Worker drawBuffers.data());
1374*8975f5c5SAndroid Build Coastguard Worker mAppliedEnabledDrawBuffers = mState.getEnabledDrawBuffers();
1375*8975f5c5SAndroid Build Coastguard Worker break;
1376*8975f5c5SAndroid Build Coastguard Worker }
1377*8975f5c5SAndroid Build Coastguard Worker case Framebuffer::DIRTY_BIT_READ_BUFFER:
1378*8975f5c5SAndroid Build Coastguard Worker functions->readBuffer(mState.getReadBufferState());
1379*8975f5c5SAndroid Build Coastguard Worker break;
1380*8975f5c5SAndroid Build Coastguard Worker case Framebuffer::DIRTY_BIT_DEFAULT_WIDTH:
1381*8975f5c5SAndroid Build Coastguard Worker functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_WIDTH,
1382*8975f5c5SAndroid Build Coastguard Worker mState.getDefaultWidth());
1383*8975f5c5SAndroid Build Coastguard Worker break;
1384*8975f5c5SAndroid Build Coastguard Worker case Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT:
1385*8975f5c5SAndroid Build Coastguard Worker functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_HEIGHT,
1386*8975f5c5SAndroid Build Coastguard Worker mState.getDefaultHeight());
1387*8975f5c5SAndroid Build Coastguard Worker break;
1388*8975f5c5SAndroid Build Coastguard Worker case Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES:
1389*8975f5c5SAndroid Build Coastguard Worker functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES,
1390*8975f5c5SAndroid Build Coastguard Worker mState.getDefaultSamples());
1391*8975f5c5SAndroid Build Coastguard Worker break;
1392*8975f5c5SAndroid Build Coastguard Worker case Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1393*8975f5c5SAndroid Build Coastguard Worker functions->framebufferParameteri(
1394*8975f5c5SAndroid Build Coastguard Worker GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS,
1395*8975f5c5SAndroid Build Coastguard Worker gl::ConvertToGLBoolean(mState.getDefaultFixedSampleLocations()));
1396*8975f5c5SAndroid Build Coastguard Worker break;
1397*8975f5c5SAndroid Build Coastguard Worker case Framebuffer::DIRTY_BIT_DEFAULT_LAYERS:
1398*8975f5c5SAndroid Build Coastguard Worker functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_LAYERS_EXT,
1399*8975f5c5SAndroid Build Coastguard Worker mState.getDefaultLayers());
1400*8975f5c5SAndroid Build Coastguard Worker break;
1401*8975f5c5SAndroid Build Coastguard Worker case Framebuffer::DIRTY_BIT_FLIP_Y:
1402*8975f5c5SAndroid Build Coastguard Worker ASSERT(functions->framebufferParameteri || functions->framebufferParameteriMESA);
1403*8975f5c5SAndroid Build Coastguard Worker if (functions->framebufferParameteri)
1404*8975f5c5SAndroid Build Coastguard Worker {
1405*8975f5c5SAndroid Build Coastguard Worker functions->framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_FLIP_Y_MESA,
1406*8975f5c5SAndroid Build Coastguard Worker gl::ConvertToGLBoolean(mState.getFlipY()));
1407*8975f5c5SAndroid Build Coastguard Worker }
1408*8975f5c5SAndroid Build Coastguard Worker else
1409*8975f5c5SAndroid Build Coastguard Worker {
1410*8975f5c5SAndroid Build Coastguard Worker functions->framebufferParameteriMESA(GL_FRAMEBUFFER, GL_FRAMEBUFFER_FLIP_Y_MESA,
1411*8975f5c5SAndroid Build Coastguard Worker gl::ConvertToGLBoolean(mState.getFlipY()));
1412*8975f5c5SAndroid Build Coastguard Worker }
1413*8975f5c5SAndroid Build Coastguard Worker break;
1414*8975f5c5SAndroid Build Coastguard Worker default:
1415*8975f5c5SAndroid Build Coastguard Worker {
1416*8975f5c5SAndroid Build Coastguard Worker static_assert(Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0, "FB dirty bits");
1417*8975f5c5SAndroid Build Coastguard Worker if (dirtyBit < Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX)
1418*8975f5c5SAndroid Build Coastguard Worker {
1419*8975f5c5SAndroid Build Coastguard Worker size_t index =
1420*8975f5c5SAndroid Build Coastguard Worker static_cast<size_t>(dirtyBit - Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0);
1421*8975f5c5SAndroid Build Coastguard Worker const FramebufferAttachment *newAttachment = mState.getColorAttachment(index);
1422*8975f5c5SAndroid Build Coastguard Worker BindFramebufferAttachment(functions,
1423*8975f5c5SAndroid Build Coastguard Worker static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + index),
1424*8975f5c5SAndroid Build Coastguard Worker newAttachment, GetFeaturesGL(context));
1425*8975f5c5SAndroid Build Coastguard Worker if (newAttachment)
1426*8975f5c5SAndroid Build Coastguard Worker {
1427*8975f5c5SAndroid Build Coastguard Worker attachment = newAttachment;
1428*8975f5c5SAndroid Build Coastguard Worker }
1429*8975f5c5SAndroid Build Coastguard Worker
1430*8975f5c5SAndroid Build Coastguard Worker // Hiding an alpha channel is only supported when it's the first attachment
1431*8975f5c5SAndroid Build Coastguard Worker // currently. Assert that these emulated textures are not bound to a framebuffer
1432*8975f5c5SAndroid Build Coastguard Worker // using MRT.
1433*8975f5c5SAndroid Build Coastguard Worker if (index == 0)
1434*8975f5c5SAndroid Build Coastguard Worker {
1435*8975f5c5SAndroid Build Coastguard Worker mHasEmulatedAlphaAttachment =
1436*8975f5c5SAndroid Build Coastguard Worker IsEmulatedAlphaChannelTextureAttachment(attachment);
1437*8975f5c5SAndroid Build Coastguard Worker }
1438*8975f5c5SAndroid Build Coastguard Worker ASSERT(index == 0 || !IsEmulatedAlphaChannelTextureAttachment(attachment));
1439*8975f5c5SAndroid Build Coastguard Worker }
1440*8975f5c5SAndroid Build Coastguard Worker break;
1441*8975f5c5SAndroid Build Coastguard Worker }
1442*8975f5c5SAndroid Build Coastguard Worker }
1443*8975f5c5SAndroid Build Coastguard Worker }
1444*8975f5c5SAndroid Build Coastguard Worker
1445*8975f5c5SAndroid Build Coastguard Worker if (attachment && mState.id() == context->getState().getDrawFramebuffer()->id())
1446*8975f5c5SAndroid Build Coastguard Worker {
1447*8975f5c5SAndroid Build Coastguard Worker stateManager->updateMultiviewBaseViewLayerIndexUniform(
1448*8975f5c5SAndroid Build Coastguard Worker context->getState().getProgramExecutable(), getState());
1449*8975f5c5SAndroid Build Coastguard Worker }
1450*8975f5c5SAndroid Build Coastguard Worker
1451*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
1452*8975f5c5SAndroid Build Coastguard Worker }
1453*8975f5c5SAndroid Build Coastguard Worker
updateDefaultFramebufferID(GLuint framebufferID)1454*8975f5c5SAndroid Build Coastguard Worker void FramebufferGL::updateDefaultFramebufferID(GLuint framebufferID)
1455*8975f5c5SAndroid Build Coastguard Worker {
1456*8975f5c5SAndroid Build Coastguard Worker // We only update framebufferID for a default frambuffer, and the framebufferID is created
1457*8975f5c5SAndroid Build Coastguard Worker // externally. ANGLE doesn't owne it.
1458*8975f5c5SAndroid Build Coastguard Worker ASSERT(isDefault());
1459*8975f5c5SAndroid Build Coastguard Worker mFramebufferID = framebufferID;
1460*8975f5c5SAndroid Build Coastguard Worker }
1461*8975f5c5SAndroid Build Coastguard Worker
hasEmulatedAlphaChannelTextureAttachment() const1462*8975f5c5SAndroid Build Coastguard Worker bool FramebufferGL::hasEmulatedAlphaChannelTextureAttachment() const
1463*8975f5c5SAndroid Build Coastguard Worker {
1464*8975f5c5SAndroid Build Coastguard Worker return mHasEmulatedAlphaAttachment;
1465*8975f5c5SAndroid Build Coastguard Worker }
1466*8975f5c5SAndroid Build Coastguard Worker
syncClearState(const gl::Context * context,GLbitfield mask)1467*8975f5c5SAndroid Build Coastguard Worker void FramebufferGL::syncClearState(const gl::Context *context, GLbitfield mask)
1468*8975f5c5SAndroid Build Coastguard Worker {
1469*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
1470*8975f5c5SAndroid Build Coastguard Worker const angle::FeaturesGL &features = GetFeaturesGL(context);
1471*8975f5c5SAndroid Build Coastguard Worker
1472*8975f5c5SAndroid Build Coastguard Worker // Clip origin must not affect scissor box but some drivers flip it for clear ops.
1473*8975f5c5SAndroid Build Coastguard Worker if (context->getState().isScissorTestEnabled())
1474*8975f5c5SAndroid Build Coastguard Worker {
1475*8975f5c5SAndroid Build Coastguard Worker stateManager->setClipControl(ClipOrigin::LowerLeft, ClipDepthMode::NegativeOneToOne);
1476*8975f5c5SAndroid Build Coastguard Worker }
1477*8975f5c5SAndroid Build Coastguard Worker
1478*8975f5c5SAndroid Build Coastguard Worker if (features.doesSRGBClearsOnLinearFramebufferAttachments.enabled &&
1479*8975f5c5SAndroid Build Coastguard Worker (mask & GL_COLOR_BUFFER_BIT) != 0 && !isDefault())
1480*8975f5c5SAndroid Build Coastguard Worker {
1481*8975f5c5SAndroid Build Coastguard Worker bool hasSRGBAttachment = false;
1482*8975f5c5SAndroid Build Coastguard Worker for (const auto &attachment : mState.getColorAttachments())
1483*8975f5c5SAndroid Build Coastguard Worker {
1484*8975f5c5SAndroid Build Coastguard Worker if (attachment.isAttached() && attachment.getColorEncoding() == GL_SRGB)
1485*8975f5c5SAndroid Build Coastguard Worker {
1486*8975f5c5SAndroid Build Coastguard Worker hasSRGBAttachment = true;
1487*8975f5c5SAndroid Build Coastguard Worker break;
1488*8975f5c5SAndroid Build Coastguard Worker }
1489*8975f5c5SAndroid Build Coastguard Worker }
1490*8975f5c5SAndroid Build Coastguard Worker
1491*8975f5c5SAndroid Build Coastguard Worker stateManager->setFramebufferSRGBEnabled(context, hasSRGBAttachment);
1492*8975f5c5SAndroid Build Coastguard Worker }
1493*8975f5c5SAndroid Build Coastguard Worker else
1494*8975f5c5SAndroid Build Coastguard Worker {
1495*8975f5c5SAndroid Build Coastguard Worker stateManager->setFramebufferSRGBEnabled(context, !isDefault());
1496*8975f5c5SAndroid Build Coastguard Worker }
1497*8975f5c5SAndroid Build Coastguard Worker }
1498*8975f5c5SAndroid Build Coastguard Worker
syncClearBufferState(const gl::Context * context,GLenum buffer,GLint drawBuffer)1499*8975f5c5SAndroid Build Coastguard Worker void FramebufferGL::syncClearBufferState(const gl::Context *context,
1500*8975f5c5SAndroid Build Coastguard Worker GLenum buffer,
1501*8975f5c5SAndroid Build Coastguard Worker GLint drawBuffer)
1502*8975f5c5SAndroid Build Coastguard Worker {
1503*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
1504*8975f5c5SAndroid Build Coastguard Worker const angle::FeaturesGL &features = GetFeaturesGL(context);
1505*8975f5c5SAndroid Build Coastguard Worker
1506*8975f5c5SAndroid Build Coastguard Worker // Clip origin must not affect scissor box but some drivers flip it for clear ops.
1507*8975f5c5SAndroid Build Coastguard Worker if (context->getState().isScissorTestEnabled())
1508*8975f5c5SAndroid Build Coastguard Worker {
1509*8975f5c5SAndroid Build Coastguard Worker stateManager->setClipControl(ClipOrigin::LowerLeft, ClipDepthMode::NegativeOneToOne);
1510*8975f5c5SAndroid Build Coastguard Worker }
1511*8975f5c5SAndroid Build Coastguard Worker
1512*8975f5c5SAndroid Build Coastguard Worker if (features.doesSRGBClearsOnLinearFramebufferAttachments.enabled && buffer == GL_COLOR &&
1513*8975f5c5SAndroid Build Coastguard Worker !isDefault())
1514*8975f5c5SAndroid Build Coastguard Worker {
1515*8975f5c5SAndroid Build Coastguard Worker // If doing a clear on a color buffer, set SRGB blend enabled only if the color buffer
1516*8975f5c5SAndroid Build Coastguard Worker // is an SRGB format.
1517*8975f5c5SAndroid Build Coastguard Worker const auto &drawbufferState = mState.getDrawBufferStates();
1518*8975f5c5SAndroid Build Coastguard Worker const auto &colorAttachments = mState.getColorAttachments();
1519*8975f5c5SAndroid Build Coastguard Worker
1520*8975f5c5SAndroid Build Coastguard Worker const FramebufferAttachment *attachment = nullptr;
1521*8975f5c5SAndroid Build Coastguard Worker if (drawbufferState[drawBuffer] >= GL_COLOR_ATTACHMENT0 &&
1522*8975f5c5SAndroid Build Coastguard Worker drawbufferState[drawBuffer] < GL_COLOR_ATTACHMENT0 + colorAttachments.size())
1523*8975f5c5SAndroid Build Coastguard Worker {
1524*8975f5c5SAndroid Build Coastguard Worker size_t attachmentIdx =
1525*8975f5c5SAndroid Build Coastguard Worker static_cast<size_t>(drawbufferState[drawBuffer] - GL_COLOR_ATTACHMENT0);
1526*8975f5c5SAndroid Build Coastguard Worker attachment = &colorAttachments[attachmentIdx];
1527*8975f5c5SAndroid Build Coastguard Worker }
1528*8975f5c5SAndroid Build Coastguard Worker
1529*8975f5c5SAndroid Build Coastguard Worker if (attachment != nullptr)
1530*8975f5c5SAndroid Build Coastguard Worker {
1531*8975f5c5SAndroid Build Coastguard Worker stateManager->setFramebufferSRGBEnabled(context,
1532*8975f5c5SAndroid Build Coastguard Worker attachment->getColorEncoding() == GL_SRGB);
1533*8975f5c5SAndroid Build Coastguard Worker }
1534*8975f5c5SAndroid Build Coastguard Worker }
1535*8975f5c5SAndroid Build Coastguard Worker else
1536*8975f5c5SAndroid Build Coastguard Worker {
1537*8975f5c5SAndroid Build Coastguard Worker stateManager->setFramebufferSRGBEnabled(context, !isDefault());
1538*8975f5c5SAndroid Build Coastguard Worker }
1539*8975f5c5SAndroid Build Coastguard Worker }
1540*8975f5c5SAndroid Build Coastguard Worker
modifyInvalidateAttachmentsForEmulatedDefaultFBO(size_t count,const GLenum * attachments,std::vector<GLenum> * modifiedAttachments) const1541*8975f5c5SAndroid Build Coastguard Worker bool FramebufferGL::modifyInvalidateAttachmentsForEmulatedDefaultFBO(
1542*8975f5c5SAndroid Build Coastguard Worker size_t count,
1543*8975f5c5SAndroid Build Coastguard Worker const GLenum *attachments,
1544*8975f5c5SAndroid Build Coastguard Worker std::vector<GLenum> *modifiedAttachments) const
1545*8975f5c5SAndroid Build Coastguard Worker {
1546*8975f5c5SAndroid Build Coastguard Worker bool needsModification = isDefault() && mFramebufferID != 0;
1547*8975f5c5SAndroid Build Coastguard Worker if (!needsModification)
1548*8975f5c5SAndroid Build Coastguard Worker {
1549*8975f5c5SAndroid Build Coastguard Worker return false;
1550*8975f5c5SAndroid Build Coastguard Worker }
1551*8975f5c5SAndroid Build Coastguard Worker
1552*8975f5c5SAndroid Build Coastguard Worker modifiedAttachments->resize(count);
1553*8975f5c5SAndroid Build Coastguard Worker for (size_t i = 0; i < count; i++)
1554*8975f5c5SAndroid Build Coastguard Worker {
1555*8975f5c5SAndroid Build Coastguard Worker switch (attachments[i])
1556*8975f5c5SAndroid Build Coastguard Worker {
1557*8975f5c5SAndroid Build Coastguard Worker case GL_COLOR:
1558*8975f5c5SAndroid Build Coastguard Worker (*modifiedAttachments)[i] = GL_COLOR_ATTACHMENT0;
1559*8975f5c5SAndroid Build Coastguard Worker break;
1560*8975f5c5SAndroid Build Coastguard Worker
1561*8975f5c5SAndroid Build Coastguard Worker case GL_DEPTH:
1562*8975f5c5SAndroid Build Coastguard Worker (*modifiedAttachments)[i] = GL_DEPTH_ATTACHMENT;
1563*8975f5c5SAndroid Build Coastguard Worker break;
1564*8975f5c5SAndroid Build Coastguard Worker
1565*8975f5c5SAndroid Build Coastguard Worker case GL_STENCIL:
1566*8975f5c5SAndroid Build Coastguard Worker (*modifiedAttachments)[i] = GL_STENCIL_ATTACHMENT;
1567*8975f5c5SAndroid Build Coastguard Worker break;
1568*8975f5c5SAndroid Build Coastguard Worker
1569*8975f5c5SAndroid Build Coastguard Worker default:
1570*8975f5c5SAndroid Build Coastguard Worker UNREACHABLE();
1571*8975f5c5SAndroid Build Coastguard Worker break;
1572*8975f5c5SAndroid Build Coastguard Worker }
1573*8975f5c5SAndroid Build Coastguard Worker }
1574*8975f5c5SAndroid Build Coastguard Worker
1575*8975f5c5SAndroid Build Coastguard Worker return true;
1576*8975f5c5SAndroid Build Coastguard Worker }
1577*8975f5c5SAndroid Build Coastguard Worker
readPixelsRowByRow(const gl::Context * context,const gl::Rectangle & area,GLenum originalReadFormat,GLenum format,GLenum type,const gl::PixelPackState & pack,GLubyte * pixels) const1578*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::readPixelsRowByRow(const gl::Context *context,
1579*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &area,
1580*8975f5c5SAndroid Build Coastguard Worker GLenum originalReadFormat,
1581*8975f5c5SAndroid Build Coastguard Worker GLenum format,
1582*8975f5c5SAndroid Build Coastguard Worker GLenum type,
1583*8975f5c5SAndroid Build Coastguard Worker const gl::PixelPackState &pack,
1584*8975f5c5SAndroid Build Coastguard Worker GLubyte *pixels) const
1585*8975f5c5SAndroid Build Coastguard Worker {
1586*8975f5c5SAndroid Build Coastguard Worker ContextGL *contextGL = GetImplAs<ContextGL>(context);
1587*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
1588*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
1589*8975f5c5SAndroid Build Coastguard Worker GLubyte *originalReadFormatPixels = pixels;
1590*8975f5c5SAndroid Build Coastguard Worker
1591*8975f5c5SAndroid Build Coastguard Worker const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type);
1592*8975f5c5SAndroid Build Coastguard Worker
1593*8975f5c5SAndroid Build Coastguard Worker GLuint rowBytes = 0;
1594*8975f5c5SAndroid Build Coastguard Worker ANGLE_CHECK_GL_MATH(contextGL, glFormat.computeRowPitch(type, area.width, pack.alignment,
1595*8975f5c5SAndroid Build Coastguard Worker pack.rowLength, &rowBytes));
1596*8975f5c5SAndroid Build Coastguard Worker GLuint skipBytes = 0;
1597*8975f5c5SAndroid Build Coastguard Worker ANGLE_CHECK_GL_MATH(contextGL,
1598*8975f5c5SAndroid Build Coastguard Worker glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes));
1599*8975f5c5SAndroid Build Coastguard Worker
1600*8975f5c5SAndroid Build Coastguard Worker ScopedEXTTextureNorm16ReadbackWorkaround workaround;
1601*8975f5c5SAndroid Build Coastguard Worker angle::Result result =
1602*8975f5c5SAndroid Build Coastguard Worker workaround.Initialize(context, area, originalReadFormat, format, type, skipBytes, rowBytes,
1603*8975f5c5SAndroid Build Coastguard Worker glFormat.computePixelBytes(type), pixels);
1604*8975f5c5SAndroid Build Coastguard Worker if (result != angle::Result::Continue)
1605*8975f5c5SAndroid Build Coastguard Worker {
1606*8975f5c5SAndroid Build Coastguard Worker return result;
1607*8975f5c5SAndroid Build Coastguard Worker }
1608*8975f5c5SAndroid Build Coastguard Worker
1609*8975f5c5SAndroid Build Coastguard Worker gl::PixelPackState directPack;
1610*8975f5c5SAndroid Build Coastguard Worker directPack.alignment = 1;
1611*8975f5c5SAndroid Build Coastguard Worker ANGLE_TRY(stateManager->setPixelPackState(context, directPack));
1612*8975f5c5SAndroid Build Coastguard Worker
1613*8975f5c5SAndroid Build Coastguard Worker GLubyte *readbackPixels = workaround.Pixels();
1614*8975f5c5SAndroid Build Coastguard Worker readbackPixels += skipBytes;
1615*8975f5c5SAndroid Build Coastguard Worker for (GLint y = area.y; y < area.y + area.height; ++y)
1616*8975f5c5SAndroid Build Coastguard Worker {
1617*8975f5c5SAndroid Build Coastguard Worker ANGLE_GL_TRY(context,
1618*8975f5c5SAndroid Build Coastguard Worker functions->readPixels(area.x, y, area.width, 1, format, type, readbackPixels));
1619*8975f5c5SAndroid Build Coastguard Worker readbackPixels += rowBytes;
1620*8975f5c5SAndroid Build Coastguard Worker }
1621*8975f5c5SAndroid Build Coastguard Worker
1622*8975f5c5SAndroid Build Coastguard Worker if (workaround.IsEnabled())
1623*8975f5c5SAndroid Build Coastguard Worker {
1624*8975f5c5SAndroid Build Coastguard Worker return RearrangeEXTTextureNorm16Pixels(
1625*8975f5c5SAndroid Build Coastguard Worker context, area, originalReadFormat, format, type, skipBytes, rowBytes,
1626*8975f5c5SAndroid Build Coastguard Worker glFormat.computePixelBytes(type), pack, originalReadFormatPixels, workaround.Pixels());
1627*8975f5c5SAndroid Build Coastguard Worker }
1628*8975f5c5SAndroid Build Coastguard Worker
1629*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
1630*8975f5c5SAndroid Build Coastguard Worker }
1631*8975f5c5SAndroid Build Coastguard Worker
readPixelsAllAtOnce(const gl::Context * context,const gl::Rectangle & area,GLenum originalReadFormat,GLenum format,GLenum type,const gl::PixelPackState & pack,GLubyte * pixels,bool readLastRowSeparately) const1632*8975f5c5SAndroid Build Coastguard Worker angle::Result FramebufferGL::readPixelsAllAtOnce(const gl::Context *context,
1633*8975f5c5SAndroid Build Coastguard Worker const gl::Rectangle &area,
1634*8975f5c5SAndroid Build Coastguard Worker GLenum originalReadFormat,
1635*8975f5c5SAndroid Build Coastguard Worker GLenum format,
1636*8975f5c5SAndroid Build Coastguard Worker GLenum type,
1637*8975f5c5SAndroid Build Coastguard Worker const gl::PixelPackState &pack,
1638*8975f5c5SAndroid Build Coastguard Worker GLubyte *pixels,
1639*8975f5c5SAndroid Build Coastguard Worker bool readLastRowSeparately) const
1640*8975f5c5SAndroid Build Coastguard Worker {
1641*8975f5c5SAndroid Build Coastguard Worker ContextGL *contextGL = GetImplAs<ContextGL>(context);
1642*8975f5c5SAndroid Build Coastguard Worker const FunctionsGL *functions = GetFunctionsGL(context);
1643*8975f5c5SAndroid Build Coastguard Worker StateManagerGL *stateManager = GetStateManagerGL(context);
1644*8975f5c5SAndroid Build Coastguard Worker GLubyte *originalReadFormatPixels = pixels;
1645*8975f5c5SAndroid Build Coastguard Worker
1646*8975f5c5SAndroid Build Coastguard Worker const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type);
1647*8975f5c5SAndroid Build Coastguard Worker
1648*8975f5c5SAndroid Build Coastguard Worker GLuint rowBytes = 0;
1649*8975f5c5SAndroid Build Coastguard Worker ANGLE_CHECK_GL_MATH(contextGL, glFormat.computeRowPitch(type, area.width, pack.alignment,
1650*8975f5c5SAndroid Build Coastguard Worker pack.rowLength, &rowBytes));
1651*8975f5c5SAndroid Build Coastguard Worker GLuint skipBytes = 0;
1652*8975f5c5SAndroid Build Coastguard Worker ANGLE_CHECK_GL_MATH(contextGL,
1653*8975f5c5SAndroid Build Coastguard Worker glFormat.computeSkipBytes(type, rowBytes, 0, pack, false, &skipBytes));
1654*8975f5c5SAndroid Build Coastguard Worker
1655*8975f5c5SAndroid Build Coastguard Worker ScopedEXTTextureNorm16ReadbackWorkaround workaround;
1656*8975f5c5SAndroid Build Coastguard Worker angle::Result result =
1657*8975f5c5SAndroid Build Coastguard Worker workaround.Initialize(context, area, originalReadFormat, format, type, skipBytes, rowBytes,
1658*8975f5c5SAndroid Build Coastguard Worker glFormat.computePixelBytes(type), pixels);
1659*8975f5c5SAndroid Build Coastguard Worker if (result != angle::Result::Continue)
1660*8975f5c5SAndroid Build Coastguard Worker {
1661*8975f5c5SAndroid Build Coastguard Worker return result;
1662*8975f5c5SAndroid Build Coastguard Worker }
1663*8975f5c5SAndroid Build Coastguard Worker
1664*8975f5c5SAndroid Build Coastguard Worker GLint height = area.height - readLastRowSeparately;
1665*8975f5c5SAndroid Build Coastguard Worker if (height > 0)
1666*8975f5c5SAndroid Build Coastguard Worker {
1667*8975f5c5SAndroid Build Coastguard Worker ANGLE_TRY(stateManager->setPixelPackState(context, pack));
1668*8975f5c5SAndroid Build Coastguard Worker ANGLE_GL_TRY(context, functions->readPixels(area.x, area.y, area.width, height, format,
1669*8975f5c5SAndroid Build Coastguard Worker type, workaround.Pixels()));
1670*8975f5c5SAndroid Build Coastguard Worker }
1671*8975f5c5SAndroid Build Coastguard Worker
1672*8975f5c5SAndroid Build Coastguard Worker if (readLastRowSeparately)
1673*8975f5c5SAndroid Build Coastguard Worker {
1674*8975f5c5SAndroid Build Coastguard Worker gl::PixelPackState directPack;
1675*8975f5c5SAndroid Build Coastguard Worker directPack.alignment = 1;
1676*8975f5c5SAndroid Build Coastguard Worker ANGLE_TRY(stateManager->setPixelPackState(context, directPack));
1677*8975f5c5SAndroid Build Coastguard Worker
1678*8975f5c5SAndroid Build Coastguard Worker GLubyte *readbackPixels = workaround.Pixels();
1679*8975f5c5SAndroid Build Coastguard Worker readbackPixels += skipBytes + (area.height - 1) * rowBytes;
1680*8975f5c5SAndroid Build Coastguard Worker ANGLE_GL_TRY(context, functions->readPixels(area.x, area.y + area.height - 1, area.width, 1,
1681*8975f5c5SAndroid Build Coastguard Worker format, type, readbackPixels));
1682*8975f5c5SAndroid Build Coastguard Worker }
1683*8975f5c5SAndroid Build Coastguard Worker
1684*8975f5c5SAndroid Build Coastguard Worker if (workaround.IsEnabled())
1685*8975f5c5SAndroid Build Coastguard Worker {
1686*8975f5c5SAndroid Build Coastguard Worker return RearrangeEXTTextureNorm16Pixels(
1687*8975f5c5SAndroid Build Coastguard Worker context, area, originalReadFormat, format, type, skipBytes, rowBytes,
1688*8975f5c5SAndroid Build Coastguard Worker glFormat.computePixelBytes(type), pack, originalReadFormatPixels, workaround.Pixels());
1689*8975f5c5SAndroid Build Coastguard Worker }
1690*8975f5c5SAndroid Build Coastguard Worker
1691*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
1692*8975f5c5SAndroid Build Coastguard Worker }
1693*8975f5c5SAndroid Build Coastguard Worker } // namespace rx
1694