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 class BufferState final : angle::NonCopyable 35 { 36 public: 37 BufferState(); 38 ~BufferState(); 39 getUsage()40 BufferUsage getUsage() const { return mUsage; } getAccessFlags()41 GLbitfield getAccessFlags() const { return mAccessFlags; } getAccess()42 GLenum getAccess() const { return mAccess; } isMapped()43 GLboolean isMapped() const { return mMapped; } getMapPointer()44 void *getMapPointer() const { return mMapPointer; } getMapOffset()45 GLint64 getMapOffset() const { return mMapOffset; } getMapLength()46 GLint64 getMapLength() const { return mMapLength; } getSize()47 GLint64 getSize() const { return mSize; } isBoundForTransformFeedback()48 bool isBoundForTransformFeedback() const { return mTransformFeedbackIndexedBindingCount != 0; } getLabel()49 std::string getLabel() const { return mLabel; } 50 51 private: 52 friend class Buffer; 53 54 std::string mLabel; 55 56 BufferUsage mUsage; 57 GLint64 mSize; 58 GLbitfield mAccessFlags; 59 GLenum mAccess; 60 GLboolean mMapped; 61 void *mMapPointer; 62 GLint64 mMapOffset; 63 GLint64 mMapLength; 64 int mBindingCount; 65 int mTransformFeedbackIndexedBindingCount; 66 int mTransformFeedbackGenericBindingCount; 67 GLboolean mImmutable; 68 GLbitfield mStorageExtUsageFlags; 69 GLboolean mExternal; 70 }; 71 72 // Some Vertex Array Objects track buffer data updates. 73 struct ContentsObserver 74 { 75 VertexArray *vertexArray = nullptr; 76 uint32_t bufferIndex = 0; 77 }; 78 79 ANGLE_INLINE bool operator==(const ContentsObserver &lhs, const ContentsObserver &rhs) 80 { 81 return lhs.vertexArray == rhs.vertexArray && lhs.bufferIndex == rhs.bufferIndex; 82 } 83 84 class Buffer final : public RefCountObject<BufferID>, 85 public LabeledObject, 86 public angle::ObserverInterface, 87 public angle::Subject 88 { 89 public: 90 Buffer(rx::GLImplFactory *factory, BufferID id); 91 ~Buffer() override; 92 void onDestroy(const Context *context) override; 93 94 void setLabel(const Context *context, const std::string &label) override; 95 const std::string &getLabel() const override; 96 97 angle::Result bufferStorageExternal(Context *context, 98 BufferBinding target, 99 GLsizeiptr size, 100 GLeglClientBufferEXT clientBuffer, 101 GLbitfield flags); 102 angle::Result bufferStorage(Context *context, 103 BufferBinding target, 104 GLsizeiptr size, 105 const void *data, 106 GLbitfield flags); 107 angle::Result bufferData(Context *context, 108 BufferBinding target, 109 const void *data, 110 GLsizeiptr size, 111 BufferUsage usage); 112 angle::Result bufferSubData(const Context *context, 113 BufferBinding target, 114 const void *data, 115 GLsizeiptr size, 116 GLintptr offset); 117 angle::Result copyBufferSubData(const Context *context, 118 Buffer *source, 119 GLintptr sourceOffset, 120 GLintptr destOffset, 121 GLsizeiptr size); 122 angle::Result map(const Context *context, GLenum access); 123 angle::Result mapRange(const Context *context, 124 GLintptr offset, 125 GLsizeiptr length, 126 GLbitfield access); 127 angle::Result unmap(const Context *context, GLboolean *result); 128 129 // These are called when another operation changes Buffer data. 130 void onDataChanged(); 131 132 angle::Result getIndexRange(const gl::Context *context, 133 DrawElementsType type, 134 size_t offset, 135 size_t count, 136 bool primitiveRestartEnabled, 137 IndexRange *outRange) const; getState()138 const BufferState &getState() const { return mState; } getUsage()139 BufferUsage getUsage() const { return mState.mUsage; } getAccessFlags()140 GLbitfield getAccessFlags() const { return mState.mAccessFlags; } getAccess()141 GLenum getAccess() const { return mState.mAccess; } isMapped()142 GLboolean isMapped() const { return mState.mMapped; } isPersistentlyMapped()143 bool isPersistentlyMapped() const 144 { 145 return (mState.mStorageExtUsageFlags & GL_MAP_PERSISTENT_BIT_EXT) != 0; 146 } getMapPointer()147 void *getMapPointer() const { return mState.mMapPointer; } getMapOffset()148 GLint64 getMapOffset() const { return mState.mMapOffset; } getMapLength()149 GLint64 getMapLength() const { return mState.mMapLength; } getSize()150 GLint64 getSize() const { return mState.mSize; } 151 GLint64 getMemorySize() const; isImmutable()152 GLboolean isImmutable() const { return mState.mImmutable; } getStorageExtUsageFlags()153 GLbitfield getStorageExtUsageFlags() const { return mState.mStorageExtUsageFlags; } 154 155 // Buffers are always initialized immediately when allocated initState()156 InitState initState() const { return InitState::Initialized; } 157 getImplementation()158 rx::BufferImpl *getImplementation() const { return mImpl; } 159 160 // Note: we pass "isWebGL" to this function to clarify it's only valid if WebGL is enabled. 161 // We pass the boolean flag instead of the pointer because this header can't read Context.h. hasWebGLXFBBindingConflict(bool isWebGL)162 ANGLE_INLINE bool hasWebGLXFBBindingConflict(bool isWebGL) const 163 { 164 if (!isWebGL) 165 { 166 return false; 167 } 168 169 // The transform feedback generic binding point is not an indexed binding point but it also 170 // does not count as a non-transform-feedback use of the buffer, so we subtract it from the 171 // binding count when checking if the buffer is bound to a non-transform-feedback location. 172 // See https://crbug.com/853978 173 return mState.mTransformFeedbackIndexedBindingCount > 0 && 174 mState.mTransformFeedbackIndexedBindingCount != 175 mState.mBindingCount - mState.mTransformFeedbackGenericBindingCount; 176 } 177 178 bool isDoubleBoundForTransformFeedback() const; 179 void onTFBindingChanged(const Context *context, bool bound, bool indexed); onNonTFBindingChanged(int incr)180 void onNonTFBindingChanged(int incr) { mState.mBindingCount += incr; } 181 angle::Result getSubData(const gl::Context *context, 182 GLintptr offset, 183 GLsizeiptr size, 184 void *outData); 185 186 // angle::ObserverInterface implementation. 187 void onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message) override; 188 189 void addContentsObserver(VertexArray *vertexArray, uint32_t bufferIndex); 190 void removeContentsObserver(VertexArray *vertexArray, uint32_t bufferIndex); 191 192 private: 193 angle::Result bufferDataImpl(Context *context, 194 BufferBinding target, 195 const void *data, 196 GLsizeiptr size, 197 BufferUsage usage, 198 GLbitfield flags); 199 angle::Result bufferExternalDataImpl(Context *context, 200 BufferBinding target, 201 GLeglClientBufferEXT clientBuffer, 202 GLsizeiptr size, 203 GLbitfield flags); 204 205 void onContentsChange(); 206 size_t getContentsObserverIndex(VertexArray *vertexArray, uint32_t bufferIndex) const; 207 208 BufferState mState; 209 rx::BufferImpl *mImpl; 210 angle::ObserverBinding mImplObserver; 211 212 angle::FastVector<ContentsObserver, angle::kMaxFixedObservers> mContentsObservers; 213 mutable IndexRangeCache mIndexRangeCache; 214 }; 215 216 } // namespace gl 217 218 #endif // LIBANGLE_BUFFER_H_ 219