blob: 9b33bbb22d53852c86d4ccb3c16fc153a8134db3 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Framebuffer.h: Defines the gl::Framebuffer class. Implements GL framebuffer
8// objects and related functionality. [OpenGL ES 2.0.24] section 4.4 page 105.
9
Geoff Lang0a73dd82014-11-19 16:18:08 -050010#ifndef LIBANGLE_FRAMEBUFFER_H_
11#define LIBANGLE_FRAMEBUFFER_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000012
Jamie Madill7147f012015-03-05 15:41:40 -050013#include <vector>
Jamie Madillce20c7f2014-09-03 11:56:29 -040014
Jamie Madill362876b2016-06-16 14:46:59 -040015#include "common/Optional.h"
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000016#include "common/angleutils.h"
Jamie Madill7147f012015-03-05 15:41:40 -050017#include "libANGLE/Constants.h"
Geoff Lang70d0f492015-12-10 17:45:46 -050018#include "libANGLE/Debug.h"
Jamie Madill7147f012015-03-05 15:41:40 -050019#include "libANGLE/Error.h"
Jamie Madill2d06b732015-04-20 12:53:28 -040020#include "libANGLE/FramebufferAttachment.h"
Jamie Madill7147f012015-03-05 15:41:40 -050021#include "libANGLE/RefCountObject.h"
Jamie Madill362876b2016-06-16 14:46:59 -040022#include "libANGLE/signal_utils.h"
Geoff Lang64f23f62014-09-10 14:40:12 -040023
daniel@transgaming.com16418b12012-11-28 19:32:22 +000024namespace rx
25{
Jamie Madill8415b5f2016-04-26 13:41:39 -040026class ContextImpl;
Jamie Madill7aea7e02016-05-10 10:39:45 -040027class GLImplFactory;
Geoff Langda88add2014-12-01 10:22:01 -050028class FramebufferImpl;
Jamie Madilld1405e52015-03-05 15:41:39 -050029class RenderbufferImpl;
Corentin Wallez37c39792015-08-20 14:19:46 -040030class SurfaceImpl;
Jamie Madilld1405e52015-03-05 15:41:39 -050031}
32
33namespace egl
34{
Jamie Madill6c1f6712017-02-14 19:08:04 -050035class Display;
Jamie Madilld1405e52015-03-05 15:41:39 -050036class Surface;
daniel@transgaming.com16418b12012-11-28 19:32:22 +000037}
38
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039namespace gl
40{
Jamie Madill1b94d432015-08-07 13:23:23 -040041class Context;
Jamie Madilldd43e6c2017-03-24 14:18:49 -040042class ContextState;
Jamie Madill48ef11b2016-04-27 15:21:52 -040043class Framebuffer;
Geoff Langab75a052014-10-15 12:56:37 -040044class Renderbuffer;
Geoff Langb04dc822014-12-01 12:02:02 -050045class State;
Jamie Madilld1405e52015-03-05 15:41:39 -050046class Texture;
47class TextureCapsMap;
Jamie Madilldd43e6c2017-03-24 14:18:49 -040048class ValidationContext;
Jamie Madilld1405e52015-03-05 15:41:39 -050049struct Caps;
Jamie Madilld1405e52015-03-05 15:41:39 -050050struct Extensions;
51struct ImageIndex;
Geoff Lang9ad4bda2014-12-01 11:03:09 -050052struct Rectangle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000053
Jamie Madill48ef11b2016-04-27 15:21:52 -040054class FramebufferState final : angle::NonCopyable
55{
56 public:
57 FramebufferState();
58 explicit FramebufferState(const Caps &caps);
59 ~FramebufferState();
60
61 const std::string &getLabel();
62
Geoff Lang4b7f12b2016-06-21 16:47:07 -040063 const FramebufferAttachment *getAttachment(GLenum attachment) const;
Jamie Madill48ef11b2016-04-27 15:21:52 -040064 const FramebufferAttachment *getReadAttachment() const;
Jamie Madill7b57b9d2017-01-13 09:33:38 -050065 const FramebufferAttachment *getFirstNonNullAttachment() const;
Jamie Madill48ef11b2016-04-27 15:21:52 -040066 const FramebufferAttachment *getFirstColorAttachment() const;
67 const FramebufferAttachment *getDepthOrStencilAttachment() const;
Corentin Wallezb1d0a2552016-12-19 16:15:54 -050068 const FramebufferAttachment *getStencilOrDepthStencilAttachment() const;
Jamie Madill48ef11b2016-04-27 15:21:52 -040069 const FramebufferAttachment *getColorAttachment(size_t colorAttachment) const;
70 const FramebufferAttachment *getDepthAttachment() const;
71 const FramebufferAttachment *getStencilAttachment() const;
72 const FramebufferAttachment *getDepthStencilAttachment() const;
73
74 const std::vector<GLenum> &getDrawBufferStates() const { return mDrawBufferStates; }
75 GLenum getReadBufferState() const { return mReadBufferState; }
76 const std::vector<FramebufferAttachment> &getColorAttachments() const
77 {
78 return mColorAttachments;
79 }
80
81 bool attachmentsHaveSameDimensions() const;
Geoff Langb21e20d2016-07-19 15:35:41 -040082 bool colorAttachmentsAreUniqueImages() const;
Jamie Madill48ef11b2016-04-27 15:21:52 -040083
Geoff Lang4b7f12b2016-06-21 16:47:07 -040084 const FramebufferAttachment *getDrawBuffer(size_t drawBufferIdx) const;
85 size_t getDrawBufferCount() const;
86
JiangYizhouf7bbc8a2016-11-16 09:57:22 +080087 GLint getDefaultWidth() const { return mDefaultWidth; };
88 GLint getDefaultHeight() const { return mDefaultHeight; };
89 GLint getDefaultSamples() const { return mDefaultSamples; };
90 GLboolean getDefaultFixedSampleLocations() const { return mDefaultFixedSampleLocations; };
91
Jamie Madill48ef11b2016-04-27 15:21:52 -040092 private:
93 friend class Framebuffer;
94
95 std::string mLabel;
96
97 std::vector<FramebufferAttachment> mColorAttachments;
98 FramebufferAttachment mDepthAttachment;
99 FramebufferAttachment mStencilAttachment;
100
101 std::vector<GLenum> mDrawBufferStates;
102 GLenum mReadBufferState;
Jamie Madill6de51852017-04-12 09:53:01 -0400103 angle::BitSet<IMPLEMENTATION_MAX_DRAW_BUFFERS> mEnabledDrawBuffers;
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800104
105 GLint mDefaultWidth;
106 GLint mDefaultHeight;
107 GLint mDefaultSamples;
108 GLboolean mDefaultFixedSampleLocations;
Jamie Madilla02315b2017-02-23 14:14:47 -0500109
110 // It's necessary to store all this extra state so we can restore attachments
111 // when DEPTH_STENCIL/DEPTH/STENCIL is unbound in WebGL 1.
112 FramebufferAttachment mWebGLDepthStencilAttachment;
113 FramebufferAttachment mWebGLDepthAttachment;
114 FramebufferAttachment mWebGLStencilAttachment;
115 bool mWebGLDepthStencilConsistent;
Jamie Madill48ef11b2016-04-27 15:21:52 -0400116};
117
Jamie Madill1e5499d2017-04-05 11:22:16 -0400118using OnAttachmentDirtyBinding = angle::ChannelBinding<>;
Jamie Madill25e297e2017-04-20 17:01:20 -0400119using OnAttachmentDirtyChannel = angle::BroadcastChannel<>;
120using OnAttachmentDirtyReceiver = angle::SignalReceiver<>;
Jamie Madill1e5499d2017-04-05 11:22:16 -0400121
122class Framebuffer final : public LabeledObject, public OnAttachmentDirtyReceiver
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123{
124 public:
Corentin Wallezccab69d2017-01-27 16:57:15 -0500125 // Constructor to build application-defined framebuffers
Jamie Madill7aea7e02016-05-10 10:39:45 -0400126 Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id);
Corentin Wallezccab69d2017-01-27 16:57:15 -0500127 // Constructor to build default framebuffers for a surface
Jamie Madill6f60d052017-02-22 15:20:11 -0500128 Framebuffer(egl::Surface *surface);
Corentin Wallezccab69d2017-01-27 16:57:15 -0500129 // Constructor to build a fake default framebuffer when surfaceless
130 Framebuffer(rx::GLImplFactory *factory);
131
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000132 virtual ~Framebuffer();
Jamie Madill6c1f6712017-02-14 19:08:04 -0500133 void destroy(const Context *context);
134 void destroyDefault(const egl::Display *display);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135
Geoff Lang70d0f492015-12-10 17:45:46 -0500136 void setLabel(const std::string &label) override;
137 const std::string &getLabel() const override;
138
Jamie Madill51f40ec2016-06-15 14:06:00 -0400139 rx::FramebufferImpl *getImplementation() const { return mImpl; }
Geoff Langda88add2014-12-01 10:22:01 -0500140
Shannon Woodsaa2ab7d2014-06-24 17:51:51 -0400141 GLuint id() const { return mId; }
142
Jamie Madilla02315b2017-02-23 14:14:47 -0500143 void setAttachment(const Context *context,
144 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -0400145 GLenum binding,
146 const ImageIndex &textureIndex,
147 FramebufferAttachmentObject *resource);
Jamie Madilla02315b2017-02-23 14:14:47 -0500148 void resetAttachment(const Context *context, GLenum binding);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149
Jamie Madilla02315b2017-02-23 14:14:47 -0500150 void detachTexture(const Context *context, GLuint texture);
151 void detachRenderbuffer(const Context *context, GLuint renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152
Corentin Wallez37c39792015-08-20 14:19:46 -0400153 const FramebufferAttachment *getColorbuffer(size_t colorAttachment) const;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400154 const FramebufferAttachment *getDepthbuffer() const;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400155 const FramebufferAttachment *getStencilbuffer() const;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400156 const FramebufferAttachment *getDepthStencilBuffer() const;
157 const FramebufferAttachment *getDepthOrStencilbuffer() const;
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500158 const FramebufferAttachment *getStencilOrDepthStencilAttachment() const;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400159 const FramebufferAttachment *getReadColorbuffer() const;
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000160 GLenum getReadColorbufferType() const;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400161 const FramebufferAttachment *getFirstColorbuffer() const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000162
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400163 const FramebufferAttachment *getAttachment(GLenum attachment) const;
Geoff Langc90d73a2013-07-22 16:39:23 -0400164
Geoff Langa15472a2015-08-11 11:48:03 -0400165 size_t getDrawbufferStateCount() const;
166 GLenum getDrawBufferState(size_t drawBuffer) const;
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500167 const std::vector<GLenum> &getDrawBufferStates() const;
Geoff Lang164d54e2014-12-01 10:55:33 -0500168 void setDrawBuffers(size_t count, const GLenum *buffers);
Geoff Langa15472a2015-08-11 11:48:03 -0400169 const FramebufferAttachment *getDrawBuffer(size_t drawBuffer) const;
170 bool hasEnabledDrawBuffer() const;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000171
Geoff Lang9dd95802014-12-01 11:12:59 -0500172 GLenum getReadBufferState() const;
173 void setReadBuffer(GLenum buffer);
174
Corentin Wallez37c39792015-08-20 14:19:46 -0400175 size_t getNumColorBuffers() const;
Jamie Madill0df8fe42015-11-24 16:10:24 -0500176 bool hasDepth() const;
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000177 bool hasStencil() const;
Jamie Madill51f40ec2016-06-15 14:06:00 -0400178
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000179 bool usingExtendedDrawBuffers() const;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000180
Jamie Madill51f40ec2016-06-15 14:06:00 -0400181 // This method calls checkStatus.
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400182 int getSamples(const Context *context);
JiangYizhoubddc46b2016-12-09 09:50:51 +0800183
184 Error getSamplePosition(size_t index, GLfloat *xy) const;
185
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800186 GLint getDefaultWidth() const;
187 GLint getDefaultHeight() const;
188 GLint getDefaultSamples() const;
189 GLboolean getDefaultFixedSampleLocations() const;
190 void setDefaultWidth(GLint defaultWidth);
191 void setDefaultHeight(GLint defaultHeight);
192 void setDefaultSamples(GLint defaultSamples);
193 void setDefaultFixedSampleLocations(GLboolean defaultFixedSampleLocations);
194
Geoff Lang9aded172017-04-05 11:07:56 -0400195 void invalidateCompletenessCache();
196
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400197 GLenum checkStatus(const Context *context);
198
199 // TODO(jmadill): Remove this kludge.
200 GLenum checkStatus(const ValidationContext *context);
201 int getSamples(const ValidationContext *context);
Jamie Madill51f40ec2016-06-15 14:06:00 -0400202
Jamie Madill362876b2016-06-16 14:46:59 -0400203 // Helper for checkStatus == GL_FRAMEBUFFER_COMPLETE.
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400204 bool complete(const Context *context);
205 bool cachedComplete() const;
Jamie Madill51f40ec2016-06-15 14:06:00 -0400206
Jamie Madille92a3542014-07-03 10:38:58 -0400207 bool hasValidDepthStencil() const;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000208
Austin Kinross08332632015-05-05 13:35:47 -0700209 Error discard(size_t count, const GLenum *attachments);
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500210 Error invalidate(size_t count, const GLenum *attachments);
211 Error invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area);
Jamie Madill400a4412014-08-29 15:46:45 -0400212
Jamie Madill8415b5f2016-04-26 13:41:39 -0400213 Error clear(rx::ContextImpl *context, GLbitfield mask);
214 Error clearBufferfv(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500215 GLenum buffer,
216 GLint drawbuffer,
217 const GLfloat *values);
Jamie Madill8415b5f2016-04-26 13:41:39 -0400218 Error clearBufferuiv(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500219 GLenum buffer,
220 GLint drawbuffer,
221 const GLuint *values);
Jamie Madill8415b5f2016-04-26 13:41:39 -0400222 Error clearBufferiv(rx::ContextImpl *context,
Jamie Madill9082b982016-04-27 15:21:51 -0400223 GLenum buffer,
224 GLint drawbuffer,
225 const GLint *values);
Jamie Madill8415b5f2016-04-26 13:41:39 -0400226 Error clearBufferfi(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400227 GLenum buffer,
228 GLint drawbuffer,
229 GLfloat depth,
230 GLint stencil);
Geoff Langb04dc822014-12-01 12:02:02 -0500231
Geoff Langbce529e2014-12-01 12:48:41 -0500232 GLenum getImplementationColorReadFormat() const;
233 GLenum getImplementationColorReadType() const;
Jamie Madill8415b5f2016-04-26 13:41:39 -0400234 Error readPixels(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400235 const gl::Rectangle &area,
236 GLenum format,
237 GLenum type,
238 GLvoid *pixels) const;
Geoff Langbce529e2014-12-01 12:48:41 -0500239
Jamie Madill8415b5f2016-04-26 13:41:39 -0400240 Error blit(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500241 const Rectangle &sourceArea,
242 const Rectangle &destArea,
Geoff Lang242468f2015-09-24 14:15:41 -0400243 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -0400244 GLenum filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -0500245
Jamie Madill1e5499d2017-04-05 11:22:16 -0400246 enum DirtyBitType : uint32_t
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500247 {
248 DIRTY_BIT_COLOR_ATTACHMENT_0,
249 DIRTY_BIT_COLOR_ATTACHMENT_MAX =
250 DIRTY_BIT_COLOR_ATTACHMENT_0 + gl::IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS,
251 DIRTY_BIT_DEPTH_ATTACHMENT = DIRTY_BIT_COLOR_ATTACHMENT_MAX,
252 DIRTY_BIT_STENCIL_ATTACHMENT,
253 DIRTY_BIT_DRAW_BUFFERS,
254 DIRTY_BIT_READ_BUFFER,
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800255 DIRTY_BIT_DEFAULT_WIDTH,
256 DIRTY_BIT_DEFAULT_HEIGHT,
257 DIRTY_BIT_DEFAULT_SAMPLES,
258 DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS,
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500259 DIRTY_BIT_UNKNOWN,
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800260 DIRTY_BIT_MAX = DIRTY_BIT_UNKNOWN
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500261 };
262
Jamie Madill6de51852017-04-12 09:53:01 -0400263 typedef angle::BitSet<DIRTY_BIT_MAX> DirtyBits;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500264 bool hasAnyDirtyBit() const { return mDirtyBits.any(); }
265
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400266 void syncState(const Context *context);
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500267
Jamie Madill362876b2016-06-16 14:46:59 -0400268 // angle::SignalReceiver implementation
Jamie Madill1e5499d2017-04-05 11:22:16 -0400269 void signal(uint32_t token) override;
Jamie Madill362876b2016-06-16 14:46:59 -0400270
Jamie Madilla4595b82017-01-11 17:36:34 -0500271 bool formsRenderingFeedbackLoopWith(const State &state) const;
Jamie Madillfd3dd432017-02-02 19:59:59 -0500272 bool formsCopyingFeedbackLoopWith(GLuint copyTextureID,
273 GLint copyTextureLevel,
274 GLint copyTextureLayer) const;
Jamie Madilla4595b82017-01-11 17:36:34 -0500275
Jamie Madill362876b2016-06-16 14:46:59 -0400276 private:
Jamie Madilla02315b2017-02-23 14:14:47 -0500277 void detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId);
Jamie Madill362876b2016-06-16 14:46:59 -0400278 void detachMatchingAttachment(FramebufferAttachment *attachment,
279 GLenum matchType,
280 GLuint matchId,
281 size_t dirtyBit);
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400282 GLenum checkStatusImpl(const Context *context);
Jamie Madilla02315b2017-02-23 14:14:47 -0500283 void commitWebGL1DepthStencilIfConsistent();
284
285 void setAttachmentImpl(GLenum type,
286 GLenum binding,
287 const ImageIndex &textureIndex,
288 FramebufferAttachmentObject *resource);
Jamie Madillb8126692017-04-05 11:22:17 -0400289 void updateAttachment(FramebufferAttachment *attachment,
290 size_t dirtyBit,
291 OnAttachmentDirtyBinding *onDirtyBinding,
292 GLenum type,
293 GLenum binding,
294 const ImageIndex &textureIndex,
295 FramebufferAttachmentObject *resource);
Geoff Lang528ce3c2014-12-01 10:44:07 -0500296
Jamie Madill48ef11b2016-04-27 15:21:52 -0400297 FramebufferState mState;
Geoff Langda88add2014-12-01 10:22:01 -0500298 rx::FramebufferImpl *mImpl;
Shannon Woodsaa2ab7d2014-06-24 17:51:51 -0400299 GLuint mId;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500300
Jamie Madill362876b2016-06-16 14:46:59 -0400301 Optional<GLenum> mCachedStatus;
Jamie Madill1e5499d2017-04-05 11:22:16 -0400302 std::vector<OnAttachmentDirtyBinding> mDirtyColorAttachmentBindings;
303 OnAttachmentDirtyBinding mDirtyDepthAttachmentBinding;
304 OnAttachmentDirtyBinding mDirtyStencilAttachmentBinding;
Jamie Madill362876b2016-06-16 14:46:59 -0400305
306 DirtyBits mDirtyBits;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307};
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000308
Jamie Madill362876b2016-06-16 14:46:59 -0400309} // namespace gl
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
Geoff Lang0a73dd82014-11-19 16:18:08 -0500311#endif // LIBANGLE_FRAMEBUFFER_H_