• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Implementation of the state class for mananging GLES 3 Vertex Array Objects.
7 //
8 
9 #include "libANGLE/VertexArray.h"
10 
11 #include "common/utilities.h"
12 #include "libANGLE/Buffer.h"
13 #include "libANGLE/Context.h"
14 #include "libANGLE/renderer/BufferImpl.h"
15 #include "libANGLE/renderer/GLImplFactory.h"
16 #include "libANGLE/renderer/VertexArrayImpl.h"
17 
18 namespace gl
19 {
20 namespace
21 {
IsElementArrayBufferSubjectIndex(angle::SubjectIndex subjectIndex)22 bool IsElementArrayBufferSubjectIndex(angle::SubjectIndex subjectIndex)
23 {
24     return (subjectIndex == kElementArrayBufferIndex);
25 }
26 }  // namespace
27 
28 // VertexArrayState implementation.
VertexArrayState(VertexArray * vertexArray,size_t maxAttribs,size_t maxAttribBindings)29 VertexArrayState::VertexArrayState(VertexArray *vertexArray,
30                                    size_t maxAttribs,
31                                    size_t maxAttribBindings)
32     : mElementArrayBuffer(vertexArray, kElementArrayBufferIndex)
33 {
34     ASSERT(maxAttribs <= maxAttribBindings);
35 
36     for (size_t i = 0; i < maxAttribs; i++)
37     {
38         mVertexAttributes.emplace_back(static_cast<GLuint>(i));
39         mVertexBindings.emplace_back(static_cast<GLuint>(i));
40     }
41 
42     // Initially all attributes start as "client" with no buffer bound.
43     mClientMemoryAttribsMask.set();
44 }
45 
~VertexArrayState()46 VertexArrayState::~VertexArrayState() {}
47 
hasEnabledNullPointerClientArray() const48 bool VertexArrayState::hasEnabledNullPointerClientArray() const
49 {
50     return (mNullPointerClientMemoryAttribsMask & mEnabledAttributesMask).any();
51 }
52 
getBindingToAttributesMask(GLuint bindingIndex) const53 AttributesMask VertexArrayState::getBindingToAttributesMask(GLuint bindingIndex) const
54 {
55     ASSERT(bindingIndex < MAX_VERTEX_ATTRIB_BINDINGS);
56     return mVertexBindings[bindingIndex].getBoundAttributesMask();
57 }
58 
59 // Set an attribute using a new binding.
setAttribBinding(const Context * context,size_t attribIndex,GLuint newBindingIndex)60 void VertexArrayState::setAttribBinding(const Context *context,
61                                         size_t attribIndex,
62                                         GLuint newBindingIndex)
63 {
64     ASSERT(attribIndex < MAX_VERTEX_ATTRIBS && newBindingIndex < MAX_VERTEX_ATTRIB_BINDINGS);
65 
66     VertexAttribute &attrib = mVertexAttributes[attribIndex];
67 
68     // Update the binding-attribute map.
69     const GLuint oldBindingIndex = attrib.bindingIndex;
70     ASSERT(oldBindingIndex != newBindingIndex);
71 
72     VertexBinding &oldBinding = mVertexBindings[oldBindingIndex];
73     VertexBinding &newBinding = mVertexBindings[newBindingIndex];
74 
75     ASSERT(oldBinding.getBoundAttributesMask().test(attribIndex) &&
76            !newBinding.getBoundAttributesMask().test(attribIndex));
77 
78     oldBinding.resetBoundAttribute(attribIndex);
79     newBinding.setBoundAttribute(attribIndex);
80 
81     // Set the attribute using the new binding.
82     attrib.bindingIndex = newBindingIndex;
83 
84     if (context->isBufferAccessValidationEnabled())
85     {
86         attrib.updateCachedElementLimit(newBinding);
87     }
88 
89     bool isMapped = newBinding.getBuffer().get() && newBinding.getBuffer()->isMapped();
90     mCachedMappedArrayBuffers.set(attribIndex, isMapped);
91     mEnabledAttributesMask.set(attribIndex, attrib.enabled);
92     updateCachedMutableOrNonPersistentArrayBuffers(attribIndex);
93     mCachedInvalidMappedArrayBuffer = mCachedMappedArrayBuffers & mEnabledAttributesMask &
94                                       mCachedMutableOrImpersistentArrayBuffers;
95 }
96 
updateCachedMutableOrNonPersistentArrayBuffers(size_t index)97 void VertexArrayState::updateCachedMutableOrNonPersistentArrayBuffers(size_t index)
98 {
99     const VertexBinding &vertexBinding   = mVertexBindings[index];
100     const BindingPointer<Buffer> &buffer = vertexBinding.getBuffer();
101     bool isMutableOrImpersistentArrayBuffer =
102         buffer.get() &&
103         (!buffer->isImmutable() || (buffer->getAccessFlags() & GL_MAP_PERSISTENT_BIT_EXT) == 0);
104     mCachedMutableOrImpersistentArrayBuffers.set(index, isMutableOrImpersistentArrayBuffer);
105 }
106 
107 // VertexArray implementation.
VertexArray(rx::GLImplFactory * factory,VertexArrayID id,size_t maxAttribs,size_t maxAttribBindings)108 VertexArray::VertexArray(rx::GLImplFactory *factory,
109                          VertexArrayID id,
110                          size_t maxAttribs,
111                          size_t maxAttribBindings)
112     : mId(id),
113       mState(this, maxAttribs, maxAttribBindings),
114       mVertexArray(factory->createVertexArray(mState)),
115       mBufferAccessValidationEnabled(false),
116       mContentsObservers(this)
117 {
118     for (size_t attribIndex = 0; attribIndex < maxAttribBindings; ++attribIndex)
119     {
120         mArrayBufferObserverBindings.emplace_back(this, attribIndex);
121     }
122 
123     mVertexArray->setContentsObservers(&mContentsObservers);
124 }
125 
onDestroy(const Context * context)126 void VertexArray::onDestroy(const Context *context)
127 {
128     bool isBound = context->isCurrentVertexArray(this);
129     for (uint32_t bindingIndex = 0; bindingIndex < gl::MAX_VERTEX_ATTRIB_BINDINGS; ++bindingIndex)
130     {
131         VertexBinding &binding = mState.mVertexBindings[bindingIndex];
132         Buffer *buffer         = binding.getBuffer().get();
133         if (isBound)
134         {
135             if (buffer)
136             {
137                 buffer->onNonTFBindingChanged(-1);
138             }
139         }
140         if (buffer)
141         {
142             // Note: the non-contents observer is unbound in the ObserverBinding destructor.
143             buffer->removeContentsObserver(this, bindingIndex);
144         }
145         binding.setBuffer(context, nullptr);
146     }
147     if (mState.mElementArrayBuffer.get())
148     {
149         if (isBound)
150         {
151             mState.mElementArrayBuffer->onNonTFBindingChanged(-1);
152         }
153         mState.mElementArrayBuffer->removeContentsObserver(this, kElementArrayBufferIndex);
154     }
155     mState.mElementArrayBuffer.bind(context, nullptr);
156     mVertexArray->destroy(context);
157     SafeDelete(mVertexArray);
158     delete this;
159 }
160 
~VertexArray()161 VertexArray::~VertexArray()
162 {
163     ASSERT(!mVertexArray);
164 }
165 
setLabel(const Context * context,const std::string & label)166 void VertexArray::setLabel(const Context *context, const std::string &label)
167 {
168     mState.mLabel = label;
169 }
170 
getLabel() const171 const std::string &VertexArray::getLabel() const
172 {
173     return mState.mLabel;
174 }
175 
detachBuffer(const Context * context,BufferID bufferID)176 bool VertexArray::detachBuffer(const Context *context, BufferID bufferID)
177 {
178     bool isBound           = context->isCurrentVertexArray(this);
179     bool anyBufferDetached = false;
180     for (uint32_t bindingIndex = 0; bindingIndex < gl::MAX_VERTEX_ATTRIB_BINDINGS; ++bindingIndex)
181     {
182         VertexBinding &binding                      = mState.mVertexBindings[bindingIndex];
183         const BindingPointer<Buffer> &bufferBinding = binding.getBuffer();
184         if (bufferBinding.id() == bufferID)
185         {
186             if (isBound)
187             {
188                 if (bufferBinding.get())
189                     bufferBinding->onNonTFBindingChanged(-1);
190             }
191             bufferBinding->removeContentsObserver(this, bindingIndex);
192             binding.setBuffer(context, nullptr);
193             mArrayBufferObserverBindings[bindingIndex].reset();
194 
195             if (context->getClientVersion() >= ES_3_1)
196             {
197                 setDirtyBindingBit(bindingIndex, DIRTY_BINDING_BUFFER);
198             }
199             else
200             {
201                 static_assert(gl::MAX_VERTEX_ATTRIB_BINDINGS < 8 * sizeof(uint32_t),
202                               "Not enough bits in bindingIndex");
203                 // The redundant uint32_t cast here is required to avoid a warning on MSVC.
204                 ASSERT(binding.getBoundAttributesMask() ==
205                        AttributesMask(static_cast<uint32_t>(1 << bindingIndex)));
206                 setDirtyAttribBit(bindingIndex, DIRTY_ATTRIB_POINTER);
207             }
208 
209             anyBufferDetached = true;
210             mState.mClientMemoryAttribsMask |= binding.getBoundAttributesMask();
211         }
212     }
213 
214     if (mState.mElementArrayBuffer.get() && mState.mElementArrayBuffer->id() == bufferID)
215     {
216         if (isBound && mState.mElementArrayBuffer.get())
217             mState.mElementArrayBuffer->onNonTFBindingChanged(-1);
218         mState.mElementArrayBuffer.bind(context, nullptr);
219         mDirtyBits.set(DIRTY_BIT_ELEMENT_ARRAY_BUFFER);
220         anyBufferDetached = true;
221     }
222 
223     return anyBufferDetached;
224 }
225 
getVertexAttribute(size_t attribIndex) const226 const VertexAttribute &VertexArray::getVertexAttribute(size_t attribIndex) const
227 {
228     ASSERT(attribIndex < getMaxAttribs());
229     return mState.mVertexAttributes[attribIndex];
230 }
231 
getVertexBinding(size_t bindingIndex) const232 const VertexBinding &VertexArray::getVertexBinding(size_t bindingIndex) const
233 {
234     ASSERT(bindingIndex < getMaxBindings());
235     return mState.mVertexBindings[bindingIndex];
236 }
237 
GetVertexIndexFromDirtyBit(size_t dirtyBit)238 size_t VertexArray::GetVertexIndexFromDirtyBit(size_t dirtyBit)
239 {
240     static_assert(gl::MAX_VERTEX_ATTRIBS == gl::MAX_VERTEX_ATTRIB_BINDINGS,
241                   "The stride of vertex attributes should equal to that of vertex bindings.");
242     ASSERT(dirtyBit > DIRTY_BIT_ELEMENT_ARRAY_BUFFER);
243     return (dirtyBit - DIRTY_BIT_ATTRIB_0) % gl::MAX_VERTEX_ATTRIBS;
244 }
245 
setDirtyAttribBit(size_t attribIndex,DirtyAttribBitType dirtyAttribBit)246 ANGLE_INLINE void VertexArray::setDirtyAttribBit(size_t attribIndex,
247                                                  DirtyAttribBitType dirtyAttribBit)
248 {
249     mDirtyBits.set(DIRTY_BIT_ATTRIB_0 + attribIndex);
250     mDirtyAttribBits[attribIndex].set(dirtyAttribBit);
251 }
252 
setDirtyBindingBit(size_t bindingIndex,DirtyBindingBitType dirtyBindingBit)253 ANGLE_INLINE void VertexArray::setDirtyBindingBit(size_t bindingIndex,
254                                                   DirtyBindingBitType dirtyBindingBit)
255 {
256     mDirtyBits.set(DIRTY_BIT_BINDING_0 + bindingIndex);
257     mDirtyBindingBits[bindingIndex].set(dirtyBindingBit);
258 }
259 
updateCachedBufferBindingSize(VertexBinding * binding)260 ANGLE_INLINE void VertexArray::updateCachedBufferBindingSize(VertexBinding *binding)
261 {
262     if (!mBufferAccessValidationEnabled)
263         return;
264 
265     for (size_t boundAttribute : binding->getBoundAttributesMask())
266     {
267         mState.mVertexAttributes[boundAttribute].updateCachedElementLimit(*binding);
268     }
269 }
270 
updateCachedArrayBuffersMasks(bool isMapped,bool isImmutable,bool isPersistent,const AttributesMask & boundAttributesMask)271 ANGLE_INLINE void VertexArray::updateCachedArrayBuffersMasks(
272     bool isMapped,
273     bool isImmutable,
274     bool isPersistent,
275     const AttributesMask &boundAttributesMask)
276 {
277     if (isMapped)
278     {
279         mState.mCachedMappedArrayBuffers |= boundAttributesMask;
280     }
281     else
282     {
283         mState.mCachedMappedArrayBuffers &= ~boundAttributesMask;
284     }
285 
286     if (!isImmutable || !isPersistent)
287     {
288         mState.mCachedMutableOrImpersistentArrayBuffers |= boundAttributesMask;
289     }
290     else
291     {
292         mState.mCachedMutableOrImpersistentArrayBuffers &= ~boundAttributesMask;
293     }
294 
295     mState.mCachedInvalidMappedArrayBuffer = mState.mCachedMappedArrayBuffers &
296                                              mState.mEnabledAttributesMask &
297                                              mState.mCachedMutableOrImpersistentArrayBuffers;
298 }
299 
updateCachedMappedArrayBuffersBinding(const VertexBinding & binding)300 ANGLE_INLINE void VertexArray::updateCachedMappedArrayBuffersBinding(const VertexBinding &binding)
301 {
302     const Buffer *buffer = binding.getBuffer().get();
303     bool isMapped        = buffer && buffer->isMapped();
304     bool isImmutable     = buffer && buffer->isImmutable();
305     bool isPersistent    = buffer && (buffer->getAccessFlags() & GL_MAP_PERSISTENT_BIT_EXT) != 0;
306     return updateCachedArrayBuffersMasks(isMapped, isImmutable, isPersistent,
307                                          binding.getBoundAttributesMask());
308 }
309 
updateCachedTransformFeedbackBindingValidation(size_t bindingIndex,const Buffer * buffer)310 ANGLE_INLINE void VertexArray::updateCachedTransformFeedbackBindingValidation(size_t bindingIndex,
311                                                                               const Buffer *buffer)
312 {
313     const bool hasConflict = buffer && buffer->hasWebGLXFBBindingConflict(true);
314     mCachedTransformFeedbackConflictedBindingsMask.set(bindingIndex, hasConflict);
315 }
316 
bindVertexBufferImpl(const Context * context,size_t bindingIndex,Buffer * boundBuffer,GLintptr offset,GLsizei stride)317 bool VertexArray::bindVertexBufferImpl(const Context *context,
318                                        size_t bindingIndex,
319                                        Buffer *boundBuffer,
320                                        GLintptr offset,
321                                        GLsizei stride)
322 {
323     ASSERT(bindingIndex < getMaxBindings());
324     ASSERT(context->isCurrentVertexArray(this));
325 
326     VertexBinding *binding = &mState.mVertexBindings[bindingIndex];
327 
328     Buffer *oldBuffer = binding->getBuffer().get();
329 
330     const bool sameBuffer = oldBuffer == boundBuffer;
331     const bool sameStride = static_cast<GLuint>(stride) == binding->getStride();
332     const bool sameOffset = offset == binding->getOffset();
333 
334     if (sameBuffer && sameStride && sameOffset)
335     {
336         return false;
337     }
338 
339     angle::ObserverBinding *observer = &mArrayBufferObserverBindings[bindingIndex];
340     observer->assignSubject(boundBuffer);
341 
342     // Several nullptr checks are combined here for optimization purposes.
343     if (oldBuffer)
344     {
345         oldBuffer->onNonTFBindingChanged(-1);
346         oldBuffer->removeObserver(observer);
347         oldBuffer->removeContentsObserver(this, static_cast<uint32_t>(bindingIndex));
348         oldBuffer->release(context);
349     }
350 
351     binding->assignBuffer(boundBuffer);
352     binding->setOffset(offset);
353     binding->setStride(stride);
354     updateCachedBufferBindingSize(binding);
355 
356     // Update client memory attribute pointers. Affects all bound attributes.
357     if (boundBuffer)
358     {
359         boundBuffer->addRef();
360         boundBuffer->onNonTFBindingChanged(1);
361         boundBuffer->addObserver(observer);
362         if (context->isWebGL())
363         {
364             mCachedTransformFeedbackConflictedBindingsMask.set(
365                 bindingIndex, boundBuffer->hasWebGLXFBBindingConflict(true));
366         }
367         mState.mClientMemoryAttribsMask &= ~binding->getBoundAttributesMask();
368 
369         bool isMapped     = boundBuffer->isMapped() == GL_TRUE;
370         bool isImmutable  = boundBuffer->isImmutable() == GL_TRUE;
371         bool isPersistent = (boundBuffer->getAccessFlags() & GL_MAP_PERSISTENT_BIT_EXT) != 0;
372         updateCachedArrayBuffersMasks(isMapped, isImmutable, isPersistent,
373                                       binding->getBoundAttributesMask());
374     }
375     else
376     {
377         if (context->isWebGL())
378         {
379             mCachedTransformFeedbackConflictedBindingsMask.set(bindingIndex, false);
380         }
381         mState.mClientMemoryAttribsMask |= binding->getBoundAttributesMask();
382         updateCachedArrayBuffersMasks(false, false, false, binding->getBoundAttributesMask());
383     }
384 
385     return true;
386 }
387 
bindVertexBuffer(const Context * context,size_t bindingIndex,Buffer * boundBuffer,GLintptr offset,GLsizei stride)388 void VertexArray::bindVertexBuffer(const Context *context,
389                                    size_t bindingIndex,
390                                    Buffer *boundBuffer,
391                                    GLintptr offset,
392                                    GLsizei stride)
393 {
394     if (bindVertexBufferImpl(context, bindingIndex, boundBuffer, offset, stride))
395     {
396         setDirtyBindingBit(bindingIndex, DIRTY_BINDING_BUFFER);
397     }
398 }
399 
setVertexAttribBinding(const Context * context,size_t attribIndex,GLuint bindingIndex)400 void VertexArray::setVertexAttribBinding(const Context *context,
401                                          size_t attribIndex,
402                                          GLuint bindingIndex)
403 {
404     ASSERT(attribIndex < getMaxAttribs() && bindingIndex < getMaxBindings());
405 
406     if (mState.mVertexAttributes[attribIndex].bindingIndex == bindingIndex)
407     {
408         return;
409     }
410 
411     // In ES 3.0 contexts, the binding cannot change, hence the code below is unreachable.
412     ASSERT(context->getClientVersion() >= ES_3_1);
413 
414     mState.setAttribBinding(context, attribIndex, bindingIndex);
415 
416     setDirtyAttribBit(attribIndex, DIRTY_ATTRIB_BINDING);
417 
418     // Update client attribs mask.
419     bool hasBuffer = mState.mVertexBindings[bindingIndex].getBuffer().get() != nullptr;
420     mState.mClientMemoryAttribsMask.set(attribIndex, !hasBuffer);
421 }
422 
setVertexBindingDivisor(const Context * context,size_t bindingIndex,GLuint divisor)423 void VertexArray::setVertexBindingDivisor(const Context *context,
424                                           size_t bindingIndex,
425                                           GLuint divisor)
426 {
427     ASSERT(bindingIndex < getMaxBindings());
428 
429     VertexBinding &binding = mState.mVertexBindings[bindingIndex];
430 
431     if (binding.getDivisor() == divisor)
432     {
433         return;
434     }
435 
436     binding.setDivisor(divisor);
437     setDirtyBindingBit(bindingIndex, DIRTY_BINDING_DIVISOR);
438 
439     // Trigger updates in all bound attributes.
440     if (context->isBufferAccessValidationEnabled())
441     {
442         for (size_t attribIndex : binding.getBoundAttributesMask())
443         {
444             mState.mVertexAttributes[attribIndex].updateCachedElementLimit(binding);
445         }
446     }
447 }
448 
setVertexAttribFormatImpl(VertexAttribute * attrib,GLint size,VertexAttribType type,bool normalized,bool pureInteger,GLuint relativeOffset)449 ANGLE_INLINE bool VertexArray::setVertexAttribFormatImpl(VertexAttribute *attrib,
450                                                          GLint size,
451                                                          VertexAttribType type,
452                                                          bool normalized,
453                                                          bool pureInteger,
454                                                          GLuint relativeOffset)
455 {
456     angle::FormatID formatID = gl::GetVertexFormatID(type, normalized, size, pureInteger);
457 
458     if (formatID != attrib->format->id || attrib->relativeOffset != relativeOffset)
459     {
460         attrib->relativeOffset = relativeOffset;
461         attrib->format         = &angle::Format::Get(formatID);
462         return true;
463     }
464 
465     return false;
466 }
467 
setVertexAttribFormat(size_t attribIndex,GLint size,VertexAttribType type,bool normalized,bool pureInteger,GLuint relativeOffset)468 void VertexArray::setVertexAttribFormat(size_t attribIndex,
469                                         GLint size,
470                                         VertexAttribType type,
471                                         bool normalized,
472                                         bool pureInteger,
473                                         GLuint relativeOffset)
474 {
475     VertexAttribute &attrib = mState.mVertexAttributes[attribIndex];
476 
477     ComponentType componentType = GetVertexAttributeComponentType(pureInteger, type);
478     SetComponentTypeMask(componentType, attribIndex, &mState.mVertexAttributesTypeMask);
479 
480     if (setVertexAttribFormatImpl(&attrib, size, type, normalized, pureInteger, relativeOffset))
481     {
482         setDirtyAttribBit(attribIndex, DIRTY_ATTRIB_FORMAT);
483     }
484 
485     attrib.updateCachedElementLimit(mState.mVertexBindings[attrib.bindingIndex]);
486 }
487 
setVertexAttribDivisor(const Context * context,size_t attribIndex,GLuint divisor)488 void VertexArray::setVertexAttribDivisor(const Context *context, size_t attribIndex, GLuint divisor)
489 {
490     ASSERT(attribIndex < getMaxAttribs());
491 
492     setVertexAttribBinding(context, attribIndex, static_cast<GLuint>(attribIndex));
493     setVertexBindingDivisor(context, attribIndex, divisor);
494 }
495 
enableAttribute(size_t attribIndex,bool enabledState)496 void VertexArray::enableAttribute(size_t attribIndex, bool enabledState)
497 {
498     ASSERT(attribIndex < getMaxAttribs());
499 
500     VertexAttribute &attrib = mState.mVertexAttributes[attribIndex];
501 
502     if (mState.mEnabledAttributesMask.test(attribIndex) == enabledState)
503     {
504         return;
505     }
506 
507     attrib.enabled = enabledState;
508 
509     setDirtyAttribBit(attribIndex, DIRTY_ATTRIB_ENABLED);
510 
511     // Update state cache
512     mState.mEnabledAttributesMask.set(attribIndex, enabledState);
513     mState.updateCachedMutableOrNonPersistentArrayBuffers(attribIndex);
514     mState.mCachedInvalidMappedArrayBuffer = mState.mCachedMappedArrayBuffers &
515                                              mState.mEnabledAttributesMask &
516                                              mState.mCachedMutableOrImpersistentArrayBuffers;
517 }
518 
setVertexAttribPointerImpl(const Context * context,ComponentType componentType,bool pureInteger,size_t attribIndex,Buffer * boundBuffer,GLint size,VertexAttribType type,bool normalized,GLsizei stride,const void * pointer)519 ANGLE_INLINE void VertexArray::setVertexAttribPointerImpl(const Context *context,
520                                                           ComponentType componentType,
521                                                           bool pureInteger,
522                                                           size_t attribIndex,
523                                                           Buffer *boundBuffer,
524                                                           GLint size,
525                                                           VertexAttribType type,
526                                                           bool normalized,
527                                                           GLsizei stride,
528                                                           const void *pointer)
529 {
530     ASSERT(attribIndex < getMaxAttribs());
531 
532     VertexAttribute &attrib = mState.mVertexAttributes[attribIndex];
533 
534     SetComponentTypeMask(componentType, attribIndex, &mState.mVertexAttributesTypeMask);
535 
536     bool attribDirty = setVertexAttribFormatImpl(&attrib, size, type, normalized, pureInteger, 0);
537 
538     if (attrib.bindingIndex != attribIndex)
539     {
540         setVertexAttribBinding(context, attribIndex, static_cast<GLuint>(attribIndex));
541     }
542 
543     GLsizei effectiveStride =
544         stride == 0 ? static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)) : stride;
545 
546     if (attrib.vertexAttribArrayStride != static_cast<GLuint>(stride))
547     {
548         attribDirty = true;
549     }
550     attrib.vertexAttribArrayStride = stride;
551 
552     // If we switch from an array buffer to a client pointer(or vice-versa), we set the whole
553     // attribute dirty. This notifies the Vulkan back-end to update all its caches.
554     const VertexBinding &binding = mState.mVertexBindings[attribIndex];
555     if ((boundBuffer == nullptr) != (binding.getBuffer().get() == nullptr))
556     {
557         attribDirty = true;
558     }
559 
560     // Change of attrib.pointer is not part of attribDirty. Pointer is actually the buffer offset
561     // which is handled within bindVertexBufferImpl and reflected in bufferDirty.
562     attrib.pointer  = pointer;
563     GLintptr offset = boundBuffer ? reinterpret_cast<GLintptr>(pointer) : 0;
564     const bool bufferDirty =
565         bindVertexBufferImpl(context, attribIndex, boundBuffer, offset, effectiveStride);
566 
567     if (attribDirty)
568     {
569         setDirtyAttribBit(attribIndex, DIRTY_ATTRIB_POINTER);
570     }
571     else if (bufferDirty)
572     {
573         setDirtyAttribBit(attribIndex, DIRTY_ATTRIB_POINTER_BUFFER);
574     }
575 
576     mState.mNullPointerClientMemoryAttribsMask.set(attribIndex,
577                                                    boundBuffer == nullptr && pointer == nullptr);
578 }
579 
setVertexAttribPointer(const Context * context,size_t attribIndex,gl::Buffer * boundBuffer,GLint size,VertexAttribType type,bool normalized,GLsizei stride,const void * pointer)580 void VertexArray::setVertexAttribPointer(const Context *context,
581                                          size_t attribIndex,
582                                          gl::Buffer *boundBuffer,
583                                          GLint size,
584                                          VertexAttribType type,
585                                          bool normalized,
586                                          GLsizei stride,
587                                          const void *pointer)
588 {
589     setVertexAttribPointerImpl(context, ComponentType::Float, false, attribIndex, boundBuffer, size,
590                                type, normalized, stride, pointer);
591 }
592 
setVertexAttribIPointer(const Context * context,size_t attribIndex,gl::Buffer * boundBuffer,GLint size,VertexAttribType type,GLsizei stride,const void * pointer)593 void VertexArray::setVertexAttribIPointer(const Context *context,
594                                           size_t attribIndex,
595                                           gl::Buffer *boundBuffer,
596                                           GLint size,
597                                           VertexAttribType type,
598                                           GLsizei stride,
599                                           const void *pointer)
600 {
601     ComponentType componentType = GetVertexAttributeComponentType(true, type);
602     setVertexAttribPointerImpl(context, componentType, true, attribIndex, boundBuffer, size, type,
603                                false, stride, pointer);
604 }
605 
syncState(const Context * context)606 angle::Result VertexArray::syncState(const Context *context)
607 {
608     if (mDirtyBits.any())
609     {
610         mDirtyBitsGuard = mDirtyBits;
611         ANGLE_TRY(
612             mVertexArray->syncState(context, mDirtyBits, &mDirtyAttribBits, &mDirtyBindingBits));
613         mDirtyBits.reset();
614         mDirtyBitsGuard.reset();
615 
616         // The dirty bits should be reset in the back-end. To simplify ASSERTs only check attrib 0.
617         ASSERT(mDirtyAttribBits[0].none());
618         ASSERT(mDirtyBindingBits[0].none());
619     }
620     return angle::Result::Continue;
621 }
622 
onBindingChanged(const Context * context,int incr)623 void VertexArray::onBindingChanged(const Context *context, int incr)
624 {
625     if (mState.mElementArrayBuffer.get())
626         mState.mElementArrayBuffer->onNonTFBindingChanged(incr);
627     for (auto &binding : mState.mVertexBindings)
628     {
629         binding.onContainerBindingChanged(context, incr);
630     }
631 }
632 
getDirtyBitFromIndex(bool contentsChanged,angle::SubjectIndex index) const633 VertexArray::DirtyBitType VertexArray::getDirtyBitFromIndex(bool contentsChanged,
634                                                             angle::SubjectIndex index) const
635 {
636     if (IsElementArrayBufferSubjectIndex(index))
637     {
638         mIndexRangeCache.invalidate();
639         return contentsChanged ? DIRTY_BIT_ELEMENT_ARRAY_BUFFER_DATA
640                                : DIRTY_BIT_ELEMENT_ARRAY_BUFFER;
641     }
642     else
643     {
644         // Note: this currently just gets the top-level dirty bit.
645         ASSERT(index < mArrayBufferObserverBindings.size());
646         return static_cast<DirtyBitType>(
647             (contentsChanged ? DIRTY_BIT_BUFFER_DATA_0 : DIRTY_BIT_BINDING_0) + index);
648     }
649 }
650 
onSubjectStateChange(angle::SubjectIndex index,angle::SubjectMessage message)651 void VertexArray::onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message)
652 {
653     switch (message)
654     {
655         case angle::SubjectMessage::ContentsChanged:
656             ASSERT(IsElementArrayBufferSubjectIndex(index));
657             setDependentDirtyBit(true, index);
658             break;
659 
660         case angle::SubjectMessage::SubjectChanged:
661             if (!IsElementArrayBufferSubjectIndex(index))
662             {
663                 updateCachedBufferBindingSize(&mState.mVertexBindings[index]);
664             }
665             setDependentDirtyBit(false, index);
666             break;
667 
668         case angle::SubjectMessage::BindingChanged:
669             if (!IsElementArrayBufferSubjectIndex(index))
670             {
671                 const Buffer *buffer = mState.mVertexBindings[index].getBuffer().get();
672                 updateCachedTransformFeedbackBindingValidation(index, buffer);
673             }
674             break;
675 
676         case angle::SubjectMessage::SubjectMapped:
677             if (!IsElementArrayBufferSubjectIndex(index))
678             {
679                 updateCachedMappedArrayBuffersBinding(mState.mVertexBindings[index]);
680             }
681             onStateChange(angle::SubjectMessage::SubjectMapped);
682             break;
683 
684         case angle::SubjectMessage::SubjectUnmapped:
685             setDependentDirtyBit(true, index);
686 
687             if (!IsElementArrayBufferSubjectIndex(index))
688             {
689                 updateCachedMappedArrayBuffersBinding(mState.mVertexBindings[index]);
690             }
691             onStateChange(angle::SubjectMessage::SubjectUnmapped);
692             break;
693 
694         case angle::SubjectMessage::InternalMemoryAllocationChanged:
695             setDependentDirtyBit(false, index);
696             break;
697 
698         default:
699             UNREACHABLE();
700             break;
701     }
702 }
703 
setDependentDirtyBit(bool contentsChanged,angle::SubjectIndex index)704 void VertexArray::setDependentDirtyBit(bool contentsChanged, angle::SubjectIndex index)
705 {
706     DirtyBitType dirtyBit = getDirtyBitFromIndex(contentsChanged, index);
707     ASSERT(!mDirtyBitsGuard.valid() || mDirtyBitsGuard.value().test(dirtyBit));
708     mDirtyBits.set(dirtyBit);
709     onStateChange(angle::SubjectMessage::ContentsChanged);
710 }
711 
hasTransformFeedbackBindingConflict(const gl::Context * context) const712 bool VertexArray::hasTransformFeedbackBindingConflict(const gl::Context *context) const
713 {
714     // Fast check first.
715     if (!mCachedTransformFeedbackConflictedBindingsMask.any())
716     {
717         return false;
718     }
719 
720     const AttributesMask &activeAttribues = context->getStateCache().getActiveBufferedAttribsMask();
721 
722     // Slow check. We must ensure that the conflicting attributes are enabled/active.
723     for (size_t attribIndex : activeAttribues)
724     {
725         const VertexAttribute &attrib = mState.mVertexAttributes[attribIndex];
726         if (mCachedTransformFeedbackConflictedBindingsMask[attrib.bindingIndex])
727         {
728             return true;
729         }
730     }
731 
732     return false;
733 }
734 
getIndexRangeImpl(const Context * context,DrawElementsType type,GLsizei indexCount,const void * indices,IndexRange * indexRangeOut) const735 angle::Result VertexArray::getIndexRangeImpl(const Context *context,
736                                              DrawElementsType type,
737                                              GLsizei indexCount,
738                                              const void *indices,
739                                              IndexRange *indexRangeOut) const
740 {
741     Buffer *elementArrayBuffer = mState.mElementArrayBuffer.get();
742     if (!elementArrayBuffer)
743     {
744         *indexRangeOut = ComputeIndexRange(type, indices, indexCount,
745                                            context->getState().isPrimitiveRestartEnabled());
746         return angle::Result::Continue;
747     }
748 
749     size_t offset = reinterpret_cast<uintptr_t>(indices);
750     ANGLE_TRY(elementArrayBuffer->getIndexRange(context, type, offset, indexCount,
751                                                 context->getState().isPrimitiveRestartEnabled(),
752                                                 indexRangeOut));
753 
754     mIndexRangeCache.put(type, indexCount, offset, *indexRangeOut);
755     return angle::Result::Continue;
756 }
757 
758 VertexArray::IndexRangeCache::IndexRangeCache() = default;
759 
put(DrawElementsType type,GLsizei indexCount,size_t offset,const IndexRange & indexRange)760 void VertexArray::IndexRangeCache::put(DrawElementsType type,
761                                        GLsizei indexCount,
762                                        size_t offset,
763                                        const IndexRange &indexRange)
764 {
765     ASSERT(type != DrawElementsType::InvalidEnum);
766 
767     mTypeKey       = type;
768     mIndexCountKey = indexCount;
769     mOffsetKey     = offset;
770     mPayload       = indexRange;
771 }
772 
onBufferContentsChange(uint32_t bufferIndex)773 void VertexArray::onBufferContentsChange(uint32_t bufferIndex)
774 {
775     setDependentDirtyBit(true, bufferIndex);
776 }
777 
VertexArrayBufferContentsObservers(VertexArray * vertexArray)778 VertexArrayBufferContentsObservers::VertexArrayBufferContentsObservers(VertexArray *vertexArray)
779     : mVertexArray(vertexArray)
780 {}
781 
enableForBuffer(Buffer * buffer,uint32_t bufferIndex)782 void VertexArrayBufferContentsObservers::enableForBuffer(Buffer *buffer, uint32_t bufferIndex)
783 {
784     buffer->addContentsObserver(mVertexArray, bufferIndex);
785 }
786 
disableForBuffer(Buffer * buffer,uint32_t bufferIndex)787 void VertexArrayBufferContentsObservers::disableForBuffer(Buffer *buffer, uint32_t bufferIndex)
788 {
789     buffer->removeContentsObserver(mVertexArray, bufferIndex);
790 }
791 }  // namespace gl
792