blob: ea891f9ca0c3637c1fb587159b2fd979fe8a54bc [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 Madill48ef11b2016-04-27 15:21:52 -040042class Framebuffer;
Geoff Langab75a052014-10-15 12:56:37 -040043class Renderbuffer;
Geoff Langb04dc822014-12-01 12:02:02 -050044class State;
Jamie Madilld1405e52015-03-05 15:41:39 -050045class Texture;
46class TextureCapsMap;
47struct Caps;
Jamie Madilldfde6ab2016-06-09 07:07:18 -070048class ContextState;
Jamie Madilld1405e52015-03-05 15:41:39 -050049struct Extensions;
50struct ImageIndex;
Geoff Lang9ad4bda2014-12-01 11:03:09 -050051struct Rectangle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052
Jamie Madill48ef11b2016-04-27 15:21:52 -040053class FramebufferState final : angle::NonCopyable
54{
55 public:
56 FramebufferState();
57 explicit FramebufferState(const Caps &caps);
58 ~FramebufferState();
59
60 const std::string &getLabel();
61
Geoff Lang4b7f12b2016-06-21 16:47:07 -040062 const FramebufferAttachment *getAttachment(GLenum attachment) const;
Jamie Madill48ef11b2016-04-27 15:21:52 -040063 const FramebufferAttachment *getReadAttachment() const;
Jamie Madill7b57b9d2017-01-13 09:33:38 -050064 const FramebufferAttachment *getFirstNonNullAttachment() const;
Jamie Madill48ef11b2016-04-27 15:21:52 -040065 const FramebufferAttachment *getFirstColorAttachment() const;
66 const FramebufferAttachment *getDepthOrStencilAttachment() const;
Corentin Wallezb1d0a2552016-12-19 16:15:54 -050067 const FramebufferAttachment *getStencilOrDepthStencilAttachment() const;
Jamie Madill48ef11b2016-04-27 15:21:52 -040068 const FramebufferAttachment *getColorAttachment(size_t colorAttachment) const;
69 const FramebufferAttachment *getDepthAttachment() const;
70 const FramebufferAttachment *getStencilAttachment() const;
71 const FramebufferAttachment *getDepthStencilAttachment() const;
72
73 const std::vector<GLenum> &getDrawBufferStates() const { return mDrawBufferStates; }
74 GLenum getReadBufferState() const { return mReadBufferState; }
75 const std::vector<FramebufferAttachment> &getColorAttachments() const
76 {
77 return mColorAttachments;
78 }
79
80 bool attachmentsHaveSameDimensions() const;
Geoff Langb21e20d2016-07-19 15:35:41 -040081 bool colorAttachmentsAreUniqueImages() const;
Jamie Madill48ef11b2016-04-27 15:21:52 -040082
Geoff Lang4b7f12b2016-06-21 16:47:07 -040083 const FramebufferAttachment *getDrawBuffer(size_t drawBufferIdx) const;
84 size_t getDrawBufferCount() const;
85
JiangYizhouf7bbc8a2016-11-16 09:57:22 +080086 GLint getDefaultWidth() const { return mDefaultWidth; };
87 GLint getDefaultHeight() const { return mDefaultHeight; };
88 GLint getDefaultSamples() const { return mDefaultSamples; };
89 GLboolean getDefaultFixedSampleLocations() const { return mDefaultFixedSampleLocations; };
90
Jamie Madill48ef11b2016-04-27 15:21:52 -040091 private:
92 friend class Framebuffer;
93
94 std::string mLabel;
95
96 std::vector<FramebufferAttachment> mColorAttachments;
97 FramebufferAttachment mDepthAttachment;
98 FramebufferAttachment mStencilAttachment;
99
100 std::vector<GLenum> mDrawBufferStates;
101 GLenum mReadBufferState;
Jamie Madilla4595b82017-01-11 17:36:34 -0500102 std::bitset<IMPLEMENTATION_MAX_DRAW_BUFFERS> mEnabledDrawBuffers;
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800103
104 GLint mDefaultWidth;
105 GLint mDefaultHeight;
106 GLint mDefaultSamples;
107 GLboolean mDefaultFixedSampleLocations;
Jamie Madilla02315b2017-02-23 14:14:47 -0500108
109 // It's necessary to store all this extra state so we can restore attachments
110 // when DEPTH_STENCIL/DEPTH/STENCIL is unbound in WebGL 1.
111 FramebufferAttachment mWebGLDepthStencilAttachment;
112 FramebufferAttachment mWebGLDepthAttachment;
113 FramebufferAttachment mWebGLStencilAttachment;
114 bool mWebGLDepthStencilConsistent;
Jamie Madill48ef11b2016-04-27 15:21:52 -0400115};
116
Jamie Madill362876b2016-06-16 14:46:59 -0400117class Framebuffer final : public LabeledObject, public angle::SignalReceiver
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118{
119 public:
Corentin Wallezccab69d2017-01-27 16:57:15 -0500120 // Constructor to build application-defined framebuffers
Jamie Madill7aea7e02016-05-10 10:39:45 -0400121 Framebuffer(const Caps &caps, rx::GLImplFactory *factory, GLuint id);
Corentin Wallezccab69d2017-01-27 16:57:15 -0500122 // Constructor to build default framebuffers for a surface
Jamie Madill6f60d052017-02-22 15:20:11 -0500123 Framebuffer(egl::Surface *surface);
Corentin Wallezccab69d2017-01-27 16:57:15 -0500124 // Constructor to build a fake default framebuffer when surfaceless
125 Framebuffer(rx::GLImplFactory *factory);
126
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000127 virtual ~Framebuffer();
Jamie Madill6c1f6712017-02-14 19:08:04 -0500128 void destroy(const Context *context);
129 void destroyDefault(const egl::Display *display);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130
Geoff Lang70d0f492015-12-10 17:45:46 -0500131 void setLabel(const std::string &label) override;
132 const std::string &getLabel() const override;
133
Jamie Madill51f40ec2016-06-15 14:06:00 -0400134 rx::FramebufferImpl *getImplementation() const { return mImpl; }
Geoff Langda88add2014-12-01 10:22:01 -0500135
Shannon Woodsaa2ab7d2014-06-24 17:51:51 -0400136 GLuint id() const { return mId; }
137
Jamie Madilla02315b2017-02-23 14:14:47 -0500138 void setAttachment(const Context *context,
139 GLenum type,
Jamie Madill2d06b732015-04-20 12:53:28 -0400140 GLenum binding,
141 const ImageIndex &textureIndex,
142 FramebufferAttachmentObject *resource);
Jamie Madilla02315b2017-02-23 14:14:47 -0500143 void resetAttachment(const Context *context, GLenum binding);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144
Jamie Madilla02315b2017-02-23 14:14:47 -0500145 void detachTexture(const Context *context, GLuint texture);
146 void detachRenderbuffer(const Context *context, GLuint renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147
Corentin Wallez37c39792015-08-20 14:19:46 -0400148 const FramebufferAttachment *getColorbuffer(size_t colorAttachment) const;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400149 const FramebufferAttachment *getDepthbuffer() const;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400150 const FramebufferAttachment *getStencilbuffer() const;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400151 const FramebufferAttachment *getDepthStencilBuffer() const;
152 const FramebufferAttachment *getDepthOrStencilbuffer() const;
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500153 const FramebufferAttachment *getStencilOrDepthStencilAttachment() const;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400154 const FramebufferAttachment *getReadColorbuffer() const;
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +0000155 GLenum getReadColorbufferType() const;
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400156 const FramebufferAttachment *getFirstColorbuffer() const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400158 const FramebufferAttachment *getAttachment(GLenum attachment) const;
Geoff Langc90d73a2013-07-22 16:39:23 -0400159
Geoff Langa15472a2015-08-11 11:48:03 -0400160 size_t getDrawbufferStateCount() const;
161 GLenum getDrawBufferState(size_t drawBuffer) const;
Jamie Madill1fbc59f2016-02-24 15:25:51 -0500162 const std::vector<GLenum> &getDrawBufferStates() const;
Geoff Lang164d54e2014-12-01 10:55:33 -0500163 void setDrawBuffers(size_t count, const GLenum *buffers);
Geoff Langa15472a2015-08-11 11:48:03 -0400164 const FramebufferAttachment *getDrawBuffer(size_t drawBuffer) const;
165 bool hasEnabledDrawBuffer() const;
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +0000166
Geoff Lang9dd95802014-12-01 11:12:59 -0500167 GLenum getReadBufferState() const;
168 void setReadBuffer(GLenum buffer);
169
Corentin Wallez37c39792015-08-20 14:19:46 -0400170 size_t getNumColorBuffers() const;
Jamie Madill0df8fe42015-11-24 16:10:24 -0500171 bool hasDepth() const;
shannon.woods%transgaming.com@gtempaccount.com3b57b4f2013-04-13 03:28:29 +0000172 bool hasStencil() const;
Jamie Madill51f40ec2016-06-15 14:06:00 -0400173
shannonwoods@chromium.org24ac8502013-05-30 00:01:37 +0000174 bool usingExtendedDrawBuffers() const;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000175
Jamie Madill51f40ec2016-06-15 14:06:00 -0400176 // This method calls checkStatus.
177 int getSamples(const ContextState &state);
JiangYizhoubddc46b2016-12-09 09:50:51 +0800178
179 Error getSamplePosition(size_t index, GLfloat *xy) const;
180
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800181 GLint getDefaultWidth() const;
182 GLint getDefaultHeight() const;
183 GLint getDefaultSamples() const;
184 GLboolean getDefaultFixedSampleLocations() const;
185 void setDefaultWidth(GLint defaultWidth);
186 void setDefaultHeight(GLint defaultHeight);
187 void setDefaultSamples(GLint defaultSamples);
188 void setDefaultFixedSampleLocations(GLboolean defaultFixedSampleLocations);
189
Jamie Madill51f40ec2016-06-15 14:06:00 -0400190 GLenum checkStatus(const ContextState &state);
191
Jamie Madill362876b2016-06-16 14:46:59 -0400192 // Helper for checkStatus == GL_FRAMEBUFFER_COMPLETE.
193 bool complete(const ContextState &state);
Jamie Madill51f40ec2016-06-15 14:06:00 -0400194
Jamie Madille92a3542014-07-03 10:38:58 -0400195 bool hasValidDepthStencil() const;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000196
Austin Kinross08332632015-05-05 13:35:47 -0700197 Error discard(size_t count, const GLenum *attachments);
Geoff Lang9ad4bda2014-12-01 11:03:09 -0500198 Error invalidate(size_t count, const GLenum *attachments);
199 Error invalidateSub(size_t count, const GLenum *attachments, const gl::Rectangle &area);
Jamie Madill400a4412014-08-29 15:46:45 -0400200
Jamie Madill8415b5f2016-04-26 13:41:39 -0400201 Error clear(rx::ContextImpl *context, GLbitfield mask);
202 Error clearBufferfv(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500203 GLenum buffer,
204 GLint drawbuffer,
205 const GLfloat *values);
Jamie Madill8415b5f2016-04-26 13:41:39 -0400206 Error clearBufferuiv(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500207 GLenum buffer,
208 GLint drawbuffer,
209 const GLuint *values);
Jamie Madill8415b5f2016-04-26 13:41:39 -0400210 Error clearBufferiv(rx::ContextImpl *context,
Jamie Madill9082b982016-04-27 15:21:51 -0400211 GLenum buffer,
212 GLint drawbuffer,
213 const GLint *values);
Jamie Madill8415b5f2016-04-26 13:41:39 -0400214 Error clearBufferfi(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400215 GLenum buffer,
216 GLint drawbuffer,
217 GLfloat depth,
218 GLint stencil);
Geoff Langb04dc822014-12-01 12:02:02 -0500219
Geoff Langbce529e2014-12-01 12:48:41 -0500220 GLenum getImplementationColorReadFormat() const;
221 GLenum getImplementationColorReadType() const;
Jamie Madill8415b5f2016-04-26 13:41:39 -0400222 Error readPixels(rx::ContextImpl *context,
Jamie Madill1b94d432015-08-07 13:23:23 -0400223 const gl::Rectangle &area,
224 GLenum format,
225 GLenum type,
226 GLvoid *pixels) const;
Geoff Langbce529e2014-12-01 12:48:41 -0500227
Jamie Madill8415b5f2016-04-26 13:41:39 -0400228 Error blit(rx::ContextImpl *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500229 const Rectangle &sourceArea,
230 const Rectangle &destArea,
Geoff Lang242468f2015-09-24 14:15:41 -0400231 GLbitfield mask,
Jamie Madill8415b5f2016-04-26 13:41:39 -0400232 GLenum filter);
Geoff Lang54bd5a42014-12-01 12:51:04 -0500233
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500234 enum DirtyBitType
235 {
236 DIRTY_BIT_COLOR_ATTACHMENT_0,
237 DIRTY_BIT_COLOR_ATTACHMENT_MAX =
238 DIRTY_BIT_COLOR_ATTACHMENT_0 + gl::IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS,
239 DIRTY_BIT_DEPTH_ATTACHMENT = DIRTY_BIT_COLOR_ATTACHMENT_MAX,
240 DIRTY_BIT_STENCIL_ATTACHMENT,
241 DIRTY_BIT_DRAW_BUFFERS,
242 DIRTY_BIT_READ_BUFFER,
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800243 DIRTY_BIT_DEFAULT_WIDTH,
244 DIRTY_BIT_DEFAULT_HEIGHT,
245 DIRTY_BIT_DEFAULT_SAMPLES,
246 DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS,
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500247 DIRTY_BIT_UNKNOWN,
JiangYizhouf7bbc8a2016-11-16 09:57:22 +0800248 DIRTY_BIT_MAX = DIRTY_BIT_UNKNOWN
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500249 };
250
251 typedef std::bitset<DIRTY_BIT_MAX> DirtyBits;
252 bool hasAnyDirtyBit() const { return mDirtyBits.any(); }
253
Jamie Madill362876b2016-06-16 14:46:59 -0400254 void syncState();
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500255
Jamie Madill362876b2016-06-16 14:46:59 -0400256 // angle::SignalReceiver implementation
257 void signal(angle::SignalToken token) override;
258
Jamie Madilla4595b82017-01-11 17:36:34 -0500259 bool formsRenderingFeedbackLoopWith(const State &state) const;
Jamie Madillfd3dd432017-02-02 19:59:59 -0500260 bool formsCopyingFeedbackLoopWith(GLuint copyTextureID,
261 GLint copyTextureLevel,
262 GLint copyTextureLayer) const;
Jamie Madilla4595b82017-01-11 17:36:34 -0500263
Jamie Madill362876b2016-06-16 14:46:59 -0400264 private:
Jamie Madilla02315b2017-02-23 14:14:47 -0500265 void detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId);
Jamie Madill362876b2016-06-16 14:46:59 -0400266 void detachMatchingAttachment(FramebufferAttachment *attachment,
267 GLenum matchType,
268 GLuint matchId,
269 size_t dirtyBit);
270 GLenum checkStatusImpl(const ContextState &state);
Jamie Madilla02315b2017-02-23 14:14:47 -0500271 void commitWebGL1DepthStencilIfConsistent();
272
273 void setAttachmentImpl(GLenum type,
274 GLenum binding,
275 const ImageIndex &textureIndex,
276 FramebufferAttachmentObject *resource);
Geoff Lang528ce3c2014-12-01 10:44:07 -0500277
Jamie Madill48ef11b2016-04-27 15:21:52 -0400278 FramebufferState mState;
Geoff Langda88add2014-12-01 10:22:01 -0500279 rx::FramebufferImpl *mImpl;
Shannon Woodsaa2ab7d2014-06-24 17:51:51 -0400280 GLuint mId;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500281
Jamie Madill362876b2016-06-16 14:46:59 -0400282 Optional<GLenum> mCachedStatus;
283 std::vector<angle::ChannelBinding> mDirtyColorAttachmentBindings;
284 angle::ChannelBinding mDirtyDepthAttachmentBinding;
285 angle::ChannelBinding mDirtyStencilAttachmentBinding;
286
287 DirtyBits mDirtyBits;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288};
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000289
Jamie Madill362876b2016-06-16 14:46:59 -0400290} // namespace gl
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291
Geoff Lang0a73dd82014-11-19 16:18:08 -0500292#endif // LIBANGLE_FRAMEBUFFER_H_