1 // 2 // Copyright 2002 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 7 // Buffer.h: Defines the gl::Buffer class, representing storage of vertex and/or 8 // index data. Implements GL buffer objects and related functionality. 9 // [OpenGL ES 2.0.24] section 2.9 page 21. 10 11 #ifndef LIBANGLE_BUFFER_H_ 12 #define LIBANGLE_BUFFER_H_ 13 14 #include "common/PackedEnums.h" 15 #include "common/angleutils.h" 16 #include "libANGLE/Debug.h" 17 #include "libANGLE/Error.h" 18 #include "libANGLE/IndexRangeCache.h" 19 #include "libANGLE/Observer.h" 20 #include "libANGLE/RefCountObject.h" 21 #include "libANGLE/angletypes.h" 22 23 namespace rx 24 { 25 class BufferImpl; 26 class GLImplFactory; 27 } // namespace rx 28 29 namespace gl 30 { 31 class Buffer; 32 class Context; 33 34 enum class WebGLBufferType 35 { 36 Undefined, 37 ElementArray, 38 OtherData, 39 }; 40 41 class BufferState final : angle::NonCopyable 42 { 43 public: 44 BufferState(); 45 ~BufferState(); 46 getUsage()47 BufferUsage getUsage() const { return mUsage; } getAccessFlags()48 GLbitfield getAccessFlags() const { return mAccessFlags; } getAccess()49 GLenum getAccess() const { return mAccess; } isMapped()50 GLboolean isMapped() const { return mMapped; } getMapPointer()51 void *getMapPointer() const { return mMapPointer; } getMapOffset()52 GLint64 getMapOffset() const { return mMapOffset; } getMapLength()53 GLint64 getMapLength() const { return mMapLength; } getSize()54 GLint64 getSize() const { return mSize; } isBoundForTransformFeedback()55 bool isBoundForTransformFeedback() const { return mTransformFeedbackIndexedBindingCount != 0; } getLabel()56 std::string getLabel() const { return mLabel; } getWebGLType()57 WebGLBufferType getWebGLType() const { return mWebGLType; } 58 59 private: 60 friend class Buffer; 61 62 std::string mLabel; 63 64 BufferUsage mUsage; 65 GLint64 mSize; 66 GLbitfield mAccessFlags; 67 GLenum mAccess; 68 GLboolean mMapped; 69 void *mMapPointer; 70 GLint64 mMapOffset; 71 GLint64 mMapLength; 72 int mBindingCount; 73 int mTransformFeedbackIndexedBindingCount; 74 int mTransformFeedbackGenericBindingCount; 75 GLboolean mImmutable; 76 GLbitfield mStorageExtUsageFlags; 77 GLboolean mExternal; 78 WebGLBufferType mWebGLType; 79 }; 80 81 // Vertex Array and Texture track buffer data updates. 82 struct ContentsObserver 83 { 84 static constexpr uint32_t kBufferTextureIndex = std::numeric_limits<uint32_t>::max(); 85 uint32_t bufferIndex = 0; 86 87 // VertexArray* (bufferIndex != kBufferTextureIndex) or Texture* 88 void *observer = nullptr; 89 }; 90 91 ANGLE_INLINE bool operator==(const ContentsObserver &lhs, const ContentsObserver &rhs) 92 { 93 return lhs.bufferIndex == rhs.bufferIndex && lhs.observer == rhs.observer; 94 } 95 96 class Buffer final : public ThreadSafeRefCountObject<BufferID>, 97 public LabeledObject, 98 public angle::ObserverInterface, 99 public angle::Subject 100 { 101 public: 102 Buffer(rx::GLImplFactory *factory, BufferID id); 103 ~Buffer() override; 104 void onDestroy(const Context *context) override; 105 106 void onBind(const Context *context, BufferBinding target); 107 108 angle::Result setLabel(const Context *context, const std::string &label) override; 109 const std::string &getLabel() const override; 110 111 angle::Result bufferStorageExternal(Context *context, 112 BufferBinding target, 113 GLsizeiptr size, 114 GLeglClientBufferEXT clientBuffer, 115 GLbitfield flags); 116 angle::Result bufferStorage(Context *context, 117 BufferBinding target, 118 GLsizeiptr size, 119 const void *data, 120 GLbitfield flags); 121 angle::Result bufferData(Context *context, 122 BufferBinding target, 123 const void *data, 124 GLsizeiptr size, 125 BufferUsage usage); 126 angle::Result bufferSubData(const Context *context, 127 BufferBinding target, 128 const void *data, 129 GLsizeiptr size, 130 GLintptr offset); 131 angle::Result copyBufferSubData(const Context *context, 132 Buffer *source, 133 GLintptr sourceOffset, 134 GLintptr destOffset, 135 GLsizeiptr size); 136 angle::Result map(const Context *context, GLenum access); 137 angle::Result mapRange(const Context *context, 138 GLintptr offset, 139 GLsizeiptr length, 140 GLbitfield access); 141 angle::Result unmap(const Context *context, GLboolean *result); 142 143 // These are called when another operation changes Buffer data. 144 void onDataChanged(); 145 146 angle::Result getIndexRange(const gl::Context *context, 147 DrawElementsType type, 148 size_t offset, 149 size_t count, 150 bool primitiveRestartEnabled, 151 IndexRange *outRange) const; getState()152 const BufferState &getState() const { return mState; } getUsage()153 BufferUsage getUsage() const { return mState.mUsage; } getAccessFlags()154 GLbitfield getAccessFlags() const { return mState.mAccessFlags; } getAccess()155 GLenum getAccess() const { return mState.mAccess; } isMapped()156 GLboolean isMapped() const { return mState.mMapped; } isPersistentlyMapped()157 bool isPersistentlyMapped() const 158 { 159 return (mState.mStorageExtUsageFlags & GL_MAP_PERSISTENT_BIT_EXT) != 0; 160 } getMapPointer()161 void *getMapPointer() const { return mState.mMapPointer; } getMapOffset()162 GLint64 getMapOffset() const { return mState.mMapOffset; } getMapLength()163 GLint64 getMapLength() const { return mState.mMapLength; } getSize()164 GLint64 getSize() const { return mState.mSize; } 165 GLint64 getMemorySize() const; isImmutable()166 GLboolean isImmutable() const { return mState.mImmutable; } getStorageExtUsageFlags()167 GLbitfield getStorageExtUsageFlags() const { return mState.mStorageExtUsageFlags; } 168 169 // Buffers are always initialized immediately when allocated initState()170 InitState initState() const { return InitState::Initialized; } 171 getImplementation()172 rx::BufferImpl *getImplementation() const { return mImpl; } 173 174 // Note: we pass "isWebGL" to this function to clarify it's only valid if WebGL is enabled. 175 // We pass the boolean flag instead of the pointer because this header can't read Context.h. hasWebGLXFBBindingConflict(bool isWebGL)176 ANGLE_INLINE bool hasWebGLXFBBindingConflict(bool isWebGL) const 177 { 178 if (!isWebGL) 179 { 180 return false; 181 } 182 183 // The transform feedback generic binding point is not an indexed binding point but it also 184 // does not count as a non-transform-feedback use of the buffer, so we subtract it from the 185 // binding count when checking if the buffer is bound to a non-transform-feedback location. 186 // See https://crbug.com/853978 187 return mState.mTransformFeedbackIndexedBindingCount > 0 && 188 mState.mTransformFeedbackIndexedBindingCount != 189 mState.mBindingCount - mState.mTransformFeedbackGenericBindingCount; 190 } 191 192 bool isDoubleBoundForTransformFeedback() const; 193 void onTFBindingChanged(const Context *context, bool bound, bool indexed); onNonTFBindingChanged(int incr)194 void onNonTFBindingChanged(int incr) { mState.mBindingCount += incr; } 195 angle::Result getSubData(const gl::Context *context, 196 GLintptr offset, 197 GLsizeiptr size, 198 void *outData); 199 200 // angle::ObserverInterface implementation. 201 void onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message) override; 202 203 void addContentsObserver(VertexArray *vertexArray, uint32_t bufferIndex); 204 void removeContentsObserver(VertexArray *vertexArray, uint32_t bufferIndex); 205 void addContentsObserver(Texture *texture); 206 void removeContentsObserver(Texture *texture); 207 bool hasContentsObserver(Texture *texture) const; 208 209 private: 210 angle::Result bufferDataImpl(Context *context, 211 BufferBinding target, 212 const void *data, 213 GLsizeiptr size, 214 BufferUsage usage, 215 GLbitfield flags); 216 angle::Result bufferExternalDataImpl(Context *context, 217 BufferBinding target, 218 GLeglClientBufferEXT clientBuffer, 219 GLsizeiptr size, 220 GLbitfield flags); 221 222 void onContentsChange(); 223 size_t getContentsObserverIndex(void *observer, uint32_t bufferIndex) const; 224 void removeContentsObserverImpl(void *observer, uint32_t bufferIndex); 225 226 BufferState mState; 227 rx::BufferImpl *mImpl; 228 angle::ObserverBinding mImplObserver; 229 230 angle::FastVector<ContentsObserver, angle::kMaxFixedObservers> mContentsObservers; 231 mutable IndexRangeCache mIndexRangeCache; 232 }; 233 234 } // namespace gl 235 236 #endif // LIBANGLE_BUFFER_H_ 237