1 // 2 // Copyright 2013 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 // This class contains prototypes for representing GLES 3 Vertex Array Objects: 7 // 8 // The buffer objects that are to be used by the vertex stage of the GL are collected 9 // together to form a vertex array object. All state related to the definition of data used 10 // by the vertex processor is encapsulated in a vertex array object. 11 // 12 13 #ifndef LIBANGLE_VERTEXARRAY_H_ 14 #define LIBANGLE_VERTEXARRAY_H_ 15 16 #include "common/Optional.h" 17 #include "libANGLE/Constants.h" 18 #include "libANGLE/Debug.h" 19 #include "libANGLE/Observer.h" 20 #include "libANGLE/RefCountObject.h" 21 #include "libANGLE/VertexAttribute.h" 22 23 #include <vector> 24 25 namespace rx 26 { 27 class GLImplFactory; 28 class VertexArrayImpl; 29 } // namespace rx 30 31 namespace gl 32 { 33 class Buffer; 34 35 class VertexArrayState final : angle::NonCopyable 36 { 37 public: 38 VertexArrayState(VertexArray *vertexArray, size_t maxAttribs, size_t maxBindings); 39 ~VertexArrayState(); 40 getLabel()41 const std::string &getLabel() const { return mLabel; } 42 getElementArrayBuffer()43 Buffer *getElementArrayBuffer() const { return mElementArrayBuffer.get(); } getMaxAttribs()44 size_t getMaxAttribs() const { return mVertexAttributes.size(); } getMaxBindings()45 size_t getMaxBindings() const { return mVertexBindings.size(); } getEnabledAttributesMask()46 const AttributesMask &getEnabledAttributesMask() const { return mEnabledAttributesMask; } getVertexAttributes()47 const std::vector<VertexAttribute> &getVertexAttributes() const { return mVertexAttributes; } getVertexAttribute(size_t attribIndex)48 const VertexAttribute &getVertexAttribute(size_t attribIndex) const 49 { 50 return mVertexAttributes[attribIndex]; 51 } getVertexBindings()52 const std::vector<VertexBinding> &getVertexBindings() const { return mVertexBindings; } getVertexBinding(size_t bindingIndex)53 const VertexBinding &getVertexBinding(size_t bindingIndex) const 54 { 55 return mVertexBindings[bindingIndex]; 56 } getBindingFromAttribIndex(size_t attribIndex)57 const VertexBinding &getBindingFromAttribIndex(size_t attribIndex) const 58 { 59 return mVertexBindings[mVertexAttributes[attribIndex].bindingIndex]; 60 } getBindingIndexFromAttribIndex(size_t attribIndex)61 size_t getBindingIndexFromAttribIndex(size_t attribIndex) const 62 { 63 return mVertexAttributes[attribIndex].bindingIndex; 64 } 65 66 void setAttribBinding(const Context *context, size_t attribIndex, GLuint newBindingIndex); 67 68 // Extra validation performed on the Vertex Array. 69 bool hasEnabledNullPointerClientArray() const; 70 71 // Get all the attributes in an AttributesMask that are using the given binding. 72 AttributesMask getBindingToAttributesMask(GLuint bindingIndex) const; 73 74 private: 75 friend class VertexArray; 76 std::string mLabel; 77 std::vector<VertexAttribute> mVertexAttributes; 78 SubjectBindingPointer<Buffer> mElementArrayBuffer; 79 std::vector<VertexBinding> mVertexBindings; 80 AttributesMask mEnabledAttributesMask; 81 ComponentTypeMask mVertexAttributesTypeMask; 82 83 // This is a performance optimization for buffer binding. Allows element array buffer updates. 84 friend class State; 85 86 // From the GLES 3.1 spec: 87 // When a generic attribute array is sourced from client memory, the vertex attribute binding 88 // state is ignored. Thus we don't have to worry about binding state when using client memory 89 // attribs. 90 gl::AttributesMask mClientMemoryAttribsMask; 91 gl::AttributesMask mNullPointerClientMemoryAttribsMask; 92 93 // Used for validation cache. Indexed by attribute. 94 AttributesMask mCachedMappedArrayBuffers; 95 AttributesMask mCachedEnabledMappedArrayBuffers; 96 }; 97 98 class VertexArray final : public angle::ObserverInterface, 99 public LabeledObject, 100 public angle::Subject 101 { 102 public: 103 // Dirty bits for VertexArrays use a heirarchical design. At the top level, each attribute 104 // has a single dirty bit. Then an array of MAX_ATTRIBS dirty bits each has a dirty bit for 105 // enabled/pointer/format/binding. Bindings are handled similarly. Note that because the 106 // total number of dirty bits is 33, it will not be as fast on a 32-bit machine, which 107 // can't support the advanced 64-bit scanning intrinsics. We could consider packing the 108 // binding and attribute bits together if this becomes a problem. 109 // 110 // Special note on "DIRTY_ATTRIB_POINTER_BUFFER": this is a special case when the app 111 // calls glVertexAttribPointer but only changes a VBO and/or offset binding. This allows 112 // the Vulkan back-end to skip performing a pipeline change for performance. 113 enum DirtyBitType 114 { 115 DIRTY_BIT_ELEMENT_ARRAY_BUFFER, 116 DIRTY_BIT_ELEMENT_ARRAY_BUFFER_DATA, 117 118 // Dirty bits for attributes. 119 DIRTY_BIT_ATTRIB_0, 120 DIRTY_BIT_ATTRIB_MAX = DIRTY_BIT_ATTRIB_0 + gl::MAX_VERTEX_ATTRIBS, 121 122 // Dirty bits for bindings. 123 DIRTY_BIT_BINDING_0 = DIRTY_BIT_ATTRIB_MAX, 124 DIRTY_BIT_BINDING_MAX = DIRTY_BIT_BINDING_0 + gl::MAX_VERTEX_ATTRIB_BINDINGS, 125 126 // We keep separate dirty bits for bound buffers whose data changed since last update. 127 DIRTY_BIT_BUFFER_DATA_0 = DIRTY_BIT_BINDING_MAX, 128 DIRTY_BIT_BUFFER_DATA_MAX = DIRTY_BIT_BUFFER_DATA_0 + gl::MAX_VERTEX_ATTRIB_BINDINGS, 129 130 DIRTY_BIT_UNKNOWN = DIRTY_BIT_BUFFER_DATA_MAX, 131 DIRTY_BIT_MAX = DIRTY_BIT_UNKNOWN, 132 }; 133 134 // We want to keep the number of dirty bits within 64 to keep iteration times fast. 135 static_assert(DIRTY_BIT_MAX <= 64, "Too many vertex array dirty bits."); 136 137 enum DirtyAttribBitType 138 { 139 DIRTY_ATTRIB_ENABLED, 140 DIRTY_ATTRIB_POINTER, 141 DIRTY_ATTRIB_FORMAT, 142 DIRTY_ATTRIB_BINDING, 143 DIRTY_ATTRIB_POINTER_BUFFER, 144 DIRTY_ATTRIB_UNKNOWN, 145 DIRTY_ATTRIB_MAX = DIRTY_ATTRIB_UNKNOWN, 146 }; 147 148 enum DirtyBindingBitType 149 { 150 DIRTY_BINDING_BUFFER, 151 DIRTY_BINDING_DIVISOR, 152 DIRTY_BINDING_UNKNOWN, 153 DIRTY_BINDING_MAX = DIRTY_BINDING_UNKNOWN, 154 }; 155 156 using DirtyBits = angle::BitSet<DIRTY_BIT_MAX>; 157 using DirtyAttribBits = angle::BitSet<DIRTY_ATTRIB_MAX>; 158 using DirtyBindingBits = angle::BitSet<DIRTY_BINDING_MAX>; 159 using DirtyAttribBitsArray = std::array<DirtyAttribBits, gl::MAX_VERTEX_ATTRIBS>; 160 using DirtyBindingBitsArray = std::array<DirtyBindingBits, gl::MAX_VERTEX_ATTRIB_BINDINGS>; 161 162 VertexArray(rx::GLImplFactory *factory, GLuint id, size_t maxAttribs, size_t maxAttribBindings); 163 164 void onDestroy(const Context *context); 165 id()166 GLuint id() const { return mId; } 167 168 void setLabel(const Context *context, const std::string &label) override; 169 const std::string &getLabel() const override; 170 171 const VertexBinding &getVertexBinding(size_t bindingIndex) const; 172 const VertexAttribute &getVertexAttribute(size_t attribIndex) const; getBindingFromAttribIndex(size_t attribIndex)173 const VertexBinding &getBindingFromAttribIndex(size_t attribIndex) const 174 { 175 return mState.getBindingFromAttribIndex(attribIndex); 176 } 177 178 // Returns true if the function finds and detaches a bound buffer. 179 bool detachBuffer(const Context *context, GLuint bufferName); 180 181 void setVertexAttribDivisor(const Context *context, size_t index, GLuint divisor); 182 void enableAttribute(size_t attribIndex, bool enabledState); 183 184 void setVertexAttribPointer(const Context *context, 185 size_t attribIndex, 186 Buffer *boundBuffer, 187 GLint size, 188 VertexAttribType type, 189 bool normalized, 190 GLsizei stride, 191 const void *pointer); 192 193 void setVertexAttribIPointer(const Context *context, 194 size_t attribIndex, 195 Buffer *boundBuffer, 196 GLint size, 197 VertexAttribType type, 198 GLsizei stride, 199 const void *pointer); 200 201 void setVertexAttribFormat(size_t attribIndex, 202 GLint size, 203 VertexAttribType type, 204 bool normalized, 205 bool pureInteger, 206 GLuint relativeOffset); 207 void bindVertexBuffer(const Context *context, 208 size_t bindingIndex, 209 Buffer *boundBuffer, 210 GLintptr offset, 211 GLsizei stride); 212 void setVertexAttribBinding(const Context *context, size_t attribIndex, GLuint bindingIndex); 213 void setVertexBindingDivisor(size_t bindingIndex, GLuint divisor); 214 getElementArrayBuffer()215 Buffer *getElementArrayBuffer() const { return mState.getElementArrayBuffer(); } getMaxAttribs()216 size_t getMaxAttribs() const { return mState.getMaxAttribs(); } getMaxBindings()217 size_t getMaxBindings() const { return mState.getMaxBindings(); } 218 getVertexAttributes()219 const std::vector<VertexAttribute> &getVertexAttributes() const 220 { 221 return mState.getVertexAttributes(); 222 } getVertexBindings()223 const std::vector<VertexBinding> &getVertexBindings() const 224 { 225 return mState.getVertexBindings(); 226 } 227 getImplementation()228 rx::VertexArrayImpl *getImplementation() const { return mVertexArray; } 229 getEnabledAttributesMask()230 const AttributesMask &getEnabledAttributesMask() const 231 { 232 return mState.getEnabledAttributesMask(); 233 } 234 getClientAttribsMask()235 gl::AttributesMask getClientAttribsMask() const { return mState.mClientMemoryAttribsMask; } 236 hasEnabledNullPointerClientArray()237 bool hasEnabledNullPointerClientArray() const 238 { 239 return mState.hasEnabledNullPointerClientArray(); 240 } 241 hasMappedEnabledArrayBuffer()242 bool hasMappedEnabledArrayBuffer() const 243 { 244 return mState.mCachedEnabledMappedArrayBuffers.any(); 245 } 246 247 // Observer implementation 248 void onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message) override; 249 250 static size_t GetVertexIndexFromDirtyBit(size_t dirtyBit); 251 252 angle::Result syncState(const Context *context); hasAnyDirtyBit()253 bool hasAnyDirtyBit() const { return mDirtyBits.any(); } 254 getAttributesTypeMask()255 ComponentTypeMask getAttributesTypeMask() const { return mState.mVertexAttributesTypeMask; } getAttributesMask()256 AttributesMask getAttributesMask() const { return mState.mEnabledAttributesMask; } 257 258 void onBindingChanged(const Context *context, int incr); 259 bool hasTransformFeedbackBindingConflict(const gl::Context *context) const; 260 getIndexRange(const Context * context,DrawElementsType type,GLsizei indexCount,const void * indices,IndexRange * indexRangeOut)261 ANGLE_INLINE angle::Result getIndexRange(const Context *context, 262 DrawElementsType type, 263 GLsizei indexCount, 264 const void *indices, 265 IndexRange *indexRangeOut) const 266 { 267 Buffer *elementArrayBuffer = mState.mElementArrayBuffer.get(); 268 if (elementArrayBuffer && mIndexRangeCache.get(type, indexCount, indices, indexRangeOut)) 269 { 270 return angle::Result::Continue; 271 } 272 273 return getIndexRangeImpl(context, type, indexCount, indices, indexRangeOut); 274 } 275 setBufferAccessValidationEnabled(bool enabled)276 void setBufferAccessValidationEnabled(bool enabled) 277 { 278 mBufferAccessValidationEnabled = enabled; 279 } 280 281 private: 282 ~VertexArray() override; 283 284 // This is a performance optimization for buffer binding. Allows element array buffer updates. 285 friend class State; 286 287 void setDirtyAttribBit(size_t attribIndex, DirtyAttribBitType dirtyAttribBit); 288 void setDirtyBindingBit(size_t bindingIndex, DirtyBindingBitType dirtyBindingBit); 289 290 DirtyBitType getDirtyBitFromIndex(bool contentsChanged, angle::SubjectIndex index) const; 291 void setDependentDirtyBit(bool contentsChanged, angle::SubjectIndex index); 292 293 // These are used to optimize draw call validation. 294 void updateCachedBufferBindingSize(VertexBinding *binding); 295 void updateCachedTransformFeedbackBindingValidation(size_t bindingIndex, const Buffer *buffer); 296 void updateCachedMappedArrayBuffers(bool isMapped, const AttributesMask &boundAttributesMask); 297 void updateCachedMappedArrayBuffersBinding(const VertexBinding &binding); 298 299 angle::Result getIndexRangeImpl(const Context *context, 300 DrawElementsType type, 301 GLsizei indexCount, 302 const void *indices, 303 IndexRange *indexRangeOut) const; 304 305 void setVertexAttribPointerImpl(const Context *context, 306 ComponentType componentType, 307 bool pureInteger, 308 size_t attribIndex, 309 Buffer *boundBuffer, 310 GLint size, 311 VertexAttribType type, 312 bool normalized, 313 GLsizei stride, 314 const void *pointer); 315 316 // These two functions return true if the state was dirty. 317 bool setVertexAttribFormatImpl(VertexAttribute *attrib, 318 GLint size, 319 VertexAttribType type, 320 bool normalized, 321 bool pureInteger, 322 GLuint relativeOffset); 323 bool bindVertexBufferImpl(const Context *context, 324 size_t bindingIndex, 325 Buffer *boundBuffer, 326 GLintptr offset, 327 GLsizei stride); 328 329 GLuint mId; 330 331 VertexArrayState mState; 332 DirtyBits mDirtyBits; 333 DirtyAttribBitsArray mDirtyAttribBits; 334 DirtyBindingBitsArray mDirtyBindingBits; 335 Optional<DirtyBits> mDirtyBitsGuard; 336 337 rx::VertexArrayImpl *mVertexArray; 338 339 std::vector<angle::ObserverBinding> mArrayBufferObserverBindings; 340 341 AttributesMask mCachedTransformFeedbackConflictedBindingsMask; 342 343 class IndexRangeCache final : angle::NonCopyable 344 { 345 public: 346 IndexRangeCache(); 347 invalidate()348 void invalidate() { mTypeKey = DrawElementsType::InvalidEnum; } 349 get(DrawElementsType type,GLsizei indexCount,const void * indices,IndexRange * indexRangeOut)350 bool get(DrawElementsType type, 351 GLsizei indexCount, 352 const void *indices, 353 IndexRange *indexRangeOut) 354 { 355 size_t offset = reinterpret_cast<uintptr_t>(indices); 356 if (mTypeKey == type && mIndexCountKey == indexCount && mOffsetKey == offset) 357 { 358 *indexRangeOut = mPayload; 359 return true; 360 } 361 362 return false; 363 } 364 365 void put(DrawElementsType type, 366 GLsizei indexCount, 367 size_t offset, 368 const IndexRange &indexRange); 369 370 private: 371 DrawElementsType mTypeKey; 372 GLsizei mIndexCountKey; 373 size_t mOffsetKey; 374 IndexRange mPayload; 375 }; 376 377 mutable IndexRangeCache mIndexRangeCache; 378 bool mBufferAccessValidationEnabled; 379 }; 380 381 } // namespace gl 382 383 #endif // LIBANGLE_VERTEXARRAY_H_ 384