WebGLFramebuffer.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef WEBGL_FRAMEBUFFER_H_
  6. #define WEBGL_FRAMEBUFFER_H_
  7. #include <vector>
  8. #include "mozilla/LinkedList.h"
  9. #include "mozilla/WeakPtr.h"
  10. #include "nsWrapperCache.h"
  11. #include "WebGLContext.h"
  12. #include "WebGLRenderbuffer.h"
  13. #include "WebGLStrongTypes.h"
  14. #include "WebGLTexture.h"
  15. #include "WebGLTypes.h"
  16. namespace mozilla {
  17. class WebGLFramebuffer;
  18. class WebGLRenderbuffer;
  19. class WebGLTexture;
  20. template<typename T>
  21. class PlacementArray;
  22. namespace gl {
  23. class GLContext;
  24. } // namespace gl
  25. class WebGLFBAttachPoint final
  26. {
  27. friend class WebGLFramebuffer;
  28. public:
  29. WebGLFramebuffer* const mFB;
  30. const GLenum mAttachmentPoint;
  31. protected:
  32. WebGLRefPtr<WebGLTexture> mTexturePtr;
  33. WebGLRefPtr<WebGLRenderbuffer> mRenderbufferPtr;
  34. TexImageTarget mTexImageTarget;
  35. GLint mTexImageLayer;
  36. uint32_t mTexImageLevel;
  37. ////
  38. WebGLFBAttachPoint();
  39. WebGLFBAttachPoint(WebGLFramebuffer* fb, GLenum attachmentPoint);
  40. public:
  41. ~WebGLFBAttachPoint();
  42. ////
  43. void Unlink();
  44. bool IsDefined() const;
  45. bool IsDeleteRequested() const;
  46. const webgl::FormatUsageInfo* Format() const;
  47. uint32_t Samples() const;
  48. bool HasAlpha() const;
  49. bool IsReadableFloat() const;
  50. void Clear();
  51. void SetTexImage(WebGLTexture* tex, TexImageTarget target, GLint level,
  52. GLint layer = 0);
  53. void SetRenderbuffer(WebGLRenderbuffer* rb);
  54. WebGLTexture* Texture() const { return mTexturePtr; }
  55. WebGLRenderbuffer* Renderbuffer() const { return mRenderbufferPtr; }
  56. TexImageTarget ImageTarget() const {
  57. return mTexImageTarget;
  58. }
  59. GLint Layer() const {
  60. return mTexImageLayer;
  61. }
  62. uint32_t MipLevel() const {
  63. return mTexImageLevel;
  64. }
  65. void AttachmentName(nsCString* out) const;
  66. bool HasUninitializedImageData() const;
  67. void SetImageDataStatus(WebGLImageDataStatus x) const;
  68. void Size(uint32_t* const out_width, uint32_t* const out_height) const;
  69. bool HasImage() const;
  70. bool IsComplete(WebGLContext* webgl, nsCString* const out_info) const;
  71. void Resolve(gl::GLContext* gl) const;
  72. JS::Value GetParameter(const char* funcName, WebGLContext* webgl, JSContext* cx,
  73. GLenum target, GLenum attachment, GLenum pname,
  74. ErrorResult* const out_error) const;
  75. void OnBackingStoreRespecified() const;
  76. bool IsEquivalentForFeedback(const WebGLFBAttachPoint& other) const {
  77. if (!IsDefined() || !other.IsDefined())
  78. return false;
  79. #define _(X) X == other.X
  80. return ( _(mRenderbufferPtr) &&
  81. _(mTexturePtr) &&
  82. _(mTexImageTarget.get()) &&
  83. _(mTexImageLevel) &&
  84. _(mTexImageLayer) );
  85. #undef _
  86. }
  87. ////
  88. struct Ordered {
  89. const WebGLFBAttachPoint& mRef;
  90. explicit Ordered(const WebGLFBAttachPoint& ref)
  91. : mRef(ref)
  92. { }
  93. bool operator<(const Ordered& other) const {
  94. MOZ_ASSERT(mRef.IsDefined() && other.mRef.IsDefined());
  95. #define ORDER_BY(X) if (X != other.X) return X < other.X;
  96. ORDER_BY(mRef.mRenderbufferPtr)
  97. ORDER_BY(mRef.mTexturePtr)
  98. ORDER_BY(mRef.mTexImageTarget.get())
  99. ORDER_BY(mRef.mTexImageLevel)
  100. ORDER_BY(mRef.mTexImageLayer)
  101. #undef ORDER_BY
  102. return false;
  103. }
  104. };
  105. };
  106. class WebGLFramebuffer final
  107. : public nsWrapperCache
  108. , public WebGLRefCountedObject<WebGLFramebuffer>
  109. , public LinkedListElement<WebGLFramebuffer>
  110. , public SupportsWeakPtr<WebGLFramebuffer>
  111. {
  112. friend class WebGLContext;
  113. public:
  114. MOZ_DECLARE_WEAKREFERENCE_TYPENAME(WebGLFramebuffer)
  115. const GLuint mGLName;
  116. protected:
  117. WebGLFBAttachPoint mDepthAttachment;
  118. WebGLFBAttachPoint mStencilAttachment;
  119. WebGLFBAttachPoint mDepthStencilAttachment;
  120. // In theory, this number can be unbounded based on the driver. However, no driver
  121. // appears to expose more than 8. We might as well stop there too, for now.
  122. // (http://opengl.gpuinfo.org/gl_stats_caps_single.php?listreportsbycap=GL_MAX_COLOR_ATTACHMENTS)
  123. static const size_t kMaxColorAttachments = 8; // jgilbert's MacBook Pro exposes 8.
  124. WebGLFBAttachPoint mColorAttachments[kMaxColorAttachments];
  125. ////
  126. std::vector<const WebGLFBAttachPoint*> mColorDrawBuffers; // Non-null
  127. const WebGLFBAttachPoint* mColorReadBuffer; // Null if NONE
  128. ////
  129. struct ResolvedData {
  130. // IsFeedback
  131. std::vector<const WebGLFBAttachPoint*> texDrawBuffers; // Non-null
  132. std::set<WebGLFBAttachPoint::Ordered> drawSet;
  133. std::set<WebGLFBAttachPoint::Ordered> readSet;
  134. explicit ResolvedData(const WebGLFramebuffer& parent);
  135. };
  136. UniquePtr<const ResolvedData> mResolvedCompleteData;
  137. ////
  138. public:
  139. NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLFramebuffer)
  140. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLFramebuffer)
  141. WebGLFramebuffer(WebGLContext* webgl, GLuint fbo);
  142. WebGLContext* GetParentObject() const { return mContext; }
  143. virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto) override;
  144. private:
  145. ~WebGLFramebuffer() {
  146. DeleteOnce();
  147. }
  148. public:
  149. void Delete();
  150. ////
  151. bool HasDefinedAttachments() const;
  152. bool HasIncompleteAttachments(nsCString* const out_info) const;
  153. bool AllImageRectsMatch() const;
  154. bool AllImageSamplesMatch() const;
  155. FBStatus PrecheckFramebufferStatus(nsCString* const out_info) const;
  156. protected:
  157. Maybe<WebGLFBAttachPoint*> GetAttachPoint(GLenum attachment); // Fallible
  158. Maybe<WebGLFBAttachPoint*> GetColorAttachPoint(GLenum attachment); // Fallible
  159. void ResolveAttachments() const;
  160. void RefreshDrawBuffers() const;
  161. void RefreshReadBuffer() const;
  162. bool ResolveAttachmentData(const char* funcName) const;
  163. public:
  164. void DetachTexture(const WebGLTexture* tex);
  165. void DetachRenderbuffer(const WebGLRenderbuffer* rb);
  166. bool ValidateAndInitAttachments(const char* funcName);
  167. bool ValidateClearBufferType(const char* funcName, GLenum buffer, uint32_t drawBuffer,
  168. GLenum funcType) const;
  169. bool ValidateForRead(const char* info,
  170. const webgl::FormatUsageInfo** const out_format,
  171. uint32_t* const out_width, uint32_t* const out_height);
  172. ////////////////
  173. // Getters
  174. #define GETTER(X) const decltype(m##X)& X() const { return m##X; }
  175. GETTER(DepthAttachment)
  176. GETTER(StencilAttachment)
  177. GETTER(DepthStencilAttachment)
  178. GETTER(ColorDrawBuffers)
  179. GETTER(ColorReadBuffer)
  180. GETTER(ResolvedCompleteData)
  181. #undef GETTER
  182. ////////////////
  183. // Invalidation
  184. bool IsResolvedComplete() const { return bool(mResolvedCompleteData); }
  185. void InvalidateFramebufferStatus() {
  186. mResolvedCompleteData = nullptr;
  187. }
  188. void RefreshResolvedData();
  189. ////////////////
  190. // WebGL funcs
  191. FBStatus CheckFramebufferStatus(const char* funcName);
  192. void FramebufferRenderbuffer(const char* funcName, GLenum attachment, GLenum rbtarget,
  193. WebGLRenderbuffer* rb);
  194. void FramebufferTexture2D(const char* funcName, GLenum attachment,
  195. GLenum texImageTarget, WebGLTexture* tex, GLint level);
  196. void FramebufferTextureLayer(const char* funcName, GLenum attachment,
  197. WebGLTexture* tex, GLint level, GLint layer);
  198. void DrawBuffers(const char* funcName, const dom::Sequence<GLenum>& buffers);
  199. void ReadBuffer(const char* funcName, GLenum attachPoint);
  200. JS::Value GetAttachmentParameter(const char* funcName, JSContext* cx, GLenum target,
  201. GLenum attachment, GLenum pname,
  202. ErrorResult* const out_error);
  203. static void BlitFramebuffer(WebGLContext* webgl,
  204. const WebGLFramebuffer* src, GLint srcX0, GLint srcY0,
  205. GLint srcX1, GLint srcY1,
  206. const WebGLFramebuffer* dst, GLint dstX0, GLint dstY0,
  207. GLint dstX1, GLint dstY1,
  208. GLbitfield mask, GLenum filter);
  209. };
  210. } // namespace mozilla
  211. #endif // WEBGL_FRAMEBUFFER_H_