1 //
2 // Copyright (c) 2002-2012 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 // VertexDataManager.h: Defines the VertexDataManager, a class that
8 // runs the Buffer translation process.
9 
10 #include "libANGLE/renderer/d3d/VertexDataManager.h"
11 
12 #include "common/bitset_utils.h"
13 #include "libANGLE/Buffer.h"
14 #include "libANGLE/Context.h"
15 #include "libANGLE/Program.h"
16 #include "libANGLE/State.h"
17 #include "libANGLE/VertexArray.h"
18 #include "libANGLE/VertexAttribute.h"
19 #include "libANGLE/formatutils.h"
20 #include "libANGLE/renderer/d3d/BufferD3D.h"
21 #include "libANGLE/renderer/d3d/RendererD3D.h"
22 #include "libANGLE/renderer/d3d/VertexBuffer.h"
23 
24 using namespace angle;
25 
26 namespace rx
27 {
28 namespace
29 {
30 enum
31 {
32     INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024
33 };
34 // This has to be at least 4k or else it fails on ATI cards.
35 enum
36 {
37     CONSTANT_VERTEX_BUFFER_SIZE = 4096
38 };
39 
40 // Warning: ensure the binding matches attrib.bindingIndex before using these functions.
GetMaxAttributeByteOffsetForDraw(const gl::VertexAttribute & attrib,const gl::VertexBinding & binding,int64_t elementCount)41 int64_t GetMaxAttributeByteOffsetForDraw(const gl::VertexAttribute &attrib,
42                                          const gl::VertexBinding &binding,
43                                          int64_t elementCount)
44 {
45     CheckedNumeric<int64_t> stride = ComputeVertexAttributeStride(attrib, binding);
46     CheckedNumeric<int64_t> offset = ComputeVertexAttributeOffset(attrib, binding);
47     CheckedNumeric<int64_t> size   = ComputeVertexAttributeTypeSize(attrib);
48 
49     ASSERT(elementCount > 0);
50 
51     CheckedNumeric<int64_t> result =
52         stride * (CheckedNumeric<int64_t>(elementCount) - 1) + size + offset;
53     return result.ValueOrDefault(std::numeric_limits<int64_t>::max());
54 }
55 
56 // Warning: ensure the binding matches attrib.bindingIndex before using these functions.
ElementsInBuffer(const gl::VertexAttribute & attrib,const gl::VertexBinding & binding,unsigned int size)57 int ElementsInBuffer(const gl::VertexAttribute &attrib,
58                      const gl::VertexBinding &binding,
59                      unsigned int size)
60 {
61     // Size cannot be larger than a GLsizei
62     if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
63     {
64         size = static_cast<unsigned int>(std::numeric_limits<int>::max());
65     }
66 
67     GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib, binding));
68     GLsizei offset = static_cast<GLsizei>(ComputeVertexAttributeOffset(attrib, binding));
69     return (size - offset % stride +
70             (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) /
71            stride;
72 }
73 
74 // Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
DirectStoragePossible(const gl::VertexAttribute & attrib,const gl::VertexBinding & binding)75 bool DirectStoragePossible(const gl::VertexAttribute &attrib, const gl::VertexBinding &binding)
76 {
77     // Current value attribs may not use direct storage.
78     if (!attrib.enabled)
79     {
80         return false;
81     }
82 
83     gl::Buffer *buffer = binding.getBuffer().get();
84     if (!buffer)
85     {
86         return false;
87     }
88 
89     BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
90     ASSERT(bufferD3D);
91     if (!bufferD3D->supportsDirectBinding())
92     {
93         return false;
94     }
95 
96     // Alignment restrictions: In D3D, vertex data must be aligned to the format stride, or to a
97     // 4-byte boundary, whichever is smaller. (Undocumented, and experimentally confirmed)
98     size_t alignment = 4;
99 
100     // TODO(jmadill): add VertexFormatCaps
101     BufferFactoryD3D *factory = bufferD3D->getFactory();
102 
103     gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib);
104 
105     // CPU-converted vertex data must be converted (naturally).
106     if ((factory->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_CPU) != 0)
107     {
108         return false;
109     }
110 
111     if (attrib.type != GL_FLOAT)
112     {
113         auto errorOrElementSize = factory->getVertexSpaceRequired(attrib, binding, 1, 0);
114         if (errorOrElementSize.isError())
115         {
116             ERR() << "Unlogged error in DirectStoragePossible.";
117             return false;
118         }
119 
120         alignment = std::min<size_t>(errorOrElementSize.getResult(), 4);
121     }
122 
123     GLintptr offset = ComputeVertexAttributeOffset(attrib, binding);
124     // Final alignment check - unaligned data must be converted.
125     return (static_cast<size_t>(ComputeVertexAttributeStride(attrib, binding)) % alignment == 0) &&
126            (static_cast<size_t>(offset) % alignment == 0);
127 }
128 }  // anonymous namespace
129 
TranslatedAttribute()130 TranslatedAttribute::TranslatedAttribute()
131     : active(false),
132       attribute(nullptr),
133       binding(nullptr),
134       currentValueType(GL_NONE),
135       baseOffset(0),
136       usesFirstVertexOffset(false),
137       stride(0),
138       vertexBuffer(),
139       storage(nullptr),
140       serial(0),
141       divisor(0)
142 {
143 }
144 
145 TranslatedAttribute::TranslatedAttribute(const TranslatedAttribute &other) = default;
146 
computeOffset(GLint startVertex) const147 gl::ErrorOrResult<unsigned int> TranslatedAttribute::computeOffset(GLint startVertex) const
148 {
149     if (!usesFirstVertexOffset)
150     {
151         return baseOffset;
152     }
153 
154     CheckedNumeric<unsigned int> offset;
155 
156     offset = baseOffset + stride * static_cast<unsigned int>(startVertex);
157     ANGLE_TRY_CHECKED_MATH(offset);
158     return offset.ValueOrDie();
159 }
160 
161 // Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
ClassifyAttributeStorage(const gl::VertexAttribute & attrib,const gl::VertexBinding & binding)162 VertexStorageType ClassifyAttributeStorage(const gl::VertexAttribute &attrib,
163                                            const gl::VertexBinding &binding)
164 {
165     // If attribute is disabled, we use the current value.
166     if (!attrib.enabled)
167     {
168         return VertexStorageType::CURRENT_VALUE;
169     }
170 
171     // If specified with immediate data, we must use dynamic storage.
172     auto *buffer = binding.getBuffer().get();
173     if (!buffer)
174     {
175         return VertexStorageType::DYNAMIC;
176     }
177 
178     // Check if the buffer supports direct storage.
179     if (DirectStoragePossible(attrib, binding))
180     {
181         return VertexStorageType::DIRECT;
182     }
183 
184     // Otherwise the storage is static or dynamic.
185     BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
186     ASSERT(bufferD3D);
187     switch (bufferD3D->getUsage())
188     {
189         case D3DBufferUsage::DYNAMIC:
190             return VertexStorageType::DYNAMIC;
191         case D3DBufferUsage::STATIC:
192             return VertexStorageType::STATIC;
193         default:
194             UNREACHABLE();
195             return VertexStorageType::UNKNOWN;
196     }
197 }
198 
CurrentValueState()199 VertexDataManager::CurrentValueState::CurrentValueState() : buffer(), offset(0)
200 {
201     data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
202     data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
203     data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
204     data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
205     data.Type = GL_FLOAT;
206 }
207 
~CurrentValueState()208 VertexDataManager::CurrentValueState::~CurrentValueState()
209 {
210 }
211 
VertexDataManager(BufferFactoryD3D * factory)212 VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
213     : mFactory(factory), mStreamingBuffer(), mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS)
214 {
215 }
216 
~VertexDataManager()217 VertexDataManager::~VertexDataManager()
218 {
219 }
220 
initialize()221 gl::Error VertexDataManager::initialize()
222 {
223     mStreamingBuffer.reset(
224         new StreamingVertexBufferInterface(mFactory, INITIAL_STREAM_BUFFER_SIZE));
225     if (!mStreamingBuffer)
226     {
227         return gl::OutOfMemory() << "Failed to allocate the streaming vertex buffer.";
228     }
229 
230     return gl::NoError();
231 }
232 
deinitialize()233 void VertexDataManager::deinitialize()
234 {
235     mStreamingBuffer.reset();
236     mCurrentValueCache.clear();
237 }
238 
prepareVertexData(const gl::Context * context,GLint start,GLsizei count,std::vector<TranslatedAttribute> * translatedAttribs,GLsizei instances)239 gl::Error VertexDataManager::prepareVertexData(const gl::Context *context,
240                                                GLint start,
241                                                GLsizei count,
242                                                std::vector<TranslatedAttribute> *translatedAttribs,
243                                                GLsizei instances)
244 {
245     ASSERT(mStreamingBuffer);
246 
247     const gl::State &state             = context->getGLState();
248     const gl::VertexArray *vertexArray = state.getVertexArray();
249     const auto &vertexAttributes       = vertexArray->getVertexAttributes();
250     const auto &vertexBindings         = vertexArray->getVertexBindings();
251 
252     mDynamicAttribsMaskCache.reset();
253     const gl::Program *program = state.getProgram();
254 
255     translatedAttribs->clear();
256 
257     for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
258     {
259         // Skip attrib locations the program doesn't use.
260         if (!program->isAttribLocationActive(attribIndex))
261             continue;
262 
263         const auto &attrib = vertexAttributes[attribIndex];
264         const auto &binding = vertexBindings[attrib.bindingIndex];
265 
266         // Resize automatically puts in empty attribs
267         translatedAttribs->resize(attribIndex + 1);
268 
269         TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
270         auto currentValueData           = state.getVertexAttribCurrentValue(attribIndex);
271 
272         // Record the attribute now
273         translated->active           = true;
274         translated->attribute        = &attrib;
275         translated->binding          = &binding;
276         translated->currentValueType = currentValueData.Type;
277         translated->divisor          = binding.getDivisor();
278 
279         switch (ClassifyAttributeStorage(attrib, binding))
280         {
281             case VertexStorageType::STATIC:
282             {
283                 // Store static attribute.
284                 ANGLE_TRY(StoreStaticAttrib(context, translated));
285                 break;
286             }
287             case VertexStorageType::DYNAMIC:
288                 // Dynamic attributes must be handled together.
289                 mDynamicAttribsMaskCache.set(attribIndex);
290                 break;
291             case VertexStorageType::DIRECT:
292                 // Update translated data for direct attributes.
293                 StoreDirectAttrib(translated);
294                 break;
295             case VertexStorageType::CURRENT_VALUE:
296             {
297                 ANGLE_TRY(storeCurrentValue(currentValueData, translated, attribIndex));
298                 break;
299             }
300             default:
301                 UNREACHABLE();
302                 break;
303         }
304     }
305 
306     if (mDynamicAttribsMaskCache.none())
307     {
308         return gl::NoError();
309     }
310 
311     ANGLE_TRY(storeDynamicAttribs(context, translatedAttribs, mDynamicAttribsMaskCache, start,
312                                   count, instances));
313 
314     PromoteDynamicAttribs(context, *translatedAttribs, mDynamicAttribsMaskCache, count);
315 
316     return gl::NoError();
317 }
318 
319 // static
StoreDirectAttrib(TranslatedAttribute * directAttrib)320 void VertexDataManager::StoreDirectAttrib(TranslatedAttribute *directAttrib)
321 {
322     ASSERT(directAttrib->attribute && directAttrib->binding);
323     const auto &attrib  = *directAttrib->attribute;
324     const auto &binding = *directAttrib->binding;
325 
326     gl::Buffer *buffer   = binding.getBuffer().get();
327     ASSERT(buffer);
328     BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
329 
330     ASSERT(DirectStoragePossible(attrib, binding));
331     directAttrib->vertexBuffer.set(nullptr);
332     directAttrib->storage = bufferD3D;
333     directAttrib->serial  = bufferD3D->getSerial();
334     directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding));
335     directAttrib->baseOffset =
336         static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding));
337 
338     // Instanced vertices do not apply the 'start' offset
339     directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0);
340 }
341 
342 // static
StoreStaticAttrib(const gl::Context * context,TranslatedAttribute * translated)343 gl::Error VertexDataManager::StoreStaticAttrib(const gl::Context *context,
344                                                TranslatedAttribute *translated)
345 {
346     ASSERT(translated->attribute && translated->binding);
347     const auto &attrib  = *translated->attribute;
348     const auto &binding = *translated->binding;
349 
350     gl::Buffer *buffer = binding.getBuffer().get();
351     ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib, binding));
352     BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
353 
354     // Compute source data pointer
355     const uint8_t *sourceData = nullptr;
356     const int offset          = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
357 
358     ANGLE_TRY(bufferD3D->getData(context, &sourceData));
359     sourceData += offset;
360 
361     unsigned int streamOffset = 0;
362 
363     translated->storage = nullptr;
364     ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, binding, 1, 0),
365                      translated->stride);
366 
367     auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
368     ASSERT(staticBuffer);
369 
370     if (staticBuffer->empty())
371     {
372         // Convert the entire buffer
373         int totalCount =
374             ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
375         int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
376 
377         ANGLE_TRY(staticBuffer->storeStaticAttribute(attrib, binding, -startIndex, totalCount, 0,
378                                                      sourceData));
379     }
380 
381     unsigned int firstElementOffset =
382         (static_cast<unsigned int>(offset) /
383          static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
384         translated->stride;
385 
386     VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
387 
388     CheckedNumeric<unsigned int> checkedOffset(streamOffset);
389     checkedOffset += firstElementOffset;
390 
391     if (!checkedOffset.IsValid())
392     {
393         return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib";
394     }
395 
396     translated->vertexBuffer.set(vertexBuffer);
397     translated->serial = vertexBuffer->getSerial();
398     translated->baseOffset = streamOffset + firstElementOffset;
399 
400     // Instanced vertices do not apply the 'start' offset
401     translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
402 
403     return gl::NoError();
404 }
405 
storeDynamicAttribs(const gl::Context * context,std::vector<TranslatedAttribute> * translatedAttribs,const gl::AttributesMask & dynamicAttribsMask,GLint start,GLsizei count,GLsizei instances)406 gl::Error VertexDataManager::storeDynamicAttribs(
407     const gl::Context *context,
408     std::vector<TranslatedAttribute> *translatedAttribs,
409     const gl::AttributesMask &dynamicAttribsMask,
410     GLint start,
411     GLsizei count,
412     GLsizei instances)
413 {
414     // Instantiating this class will ensure the streaming buffer is never left mapped.
415     class StreamingBufferUnmapper final : NonCopyable
416     {
417       public:
418         StreamingBufferUnmapper(StreamingVertexBufferInterface *streamingBuffer)
419             : mStreamingBuffer(streamingBuffer)
420         {
421             ASSERT(mStreamingBuffer);
422         }
423         ~StreamingBufferUnmapper() { mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); }
424 
425       private:
426         StreamingVertexBufferInterface *mStreamingBuffer;
427     };
428 
429     // Will trigger unmapping on return.
430     StreamingBufferUnmapper localUnmapper(mStreamingBuffer.get());
431 
432     // Reserve the required space for the dynamic buffers.
433     for (auto attribIndex : dynamicAttribsMask)
434     {
435         const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
436         ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, start, count, instances));
437     }
438 
439     // Store dynamic attributes
440     for (auto attribIndex : dynamicAttribsMask)
441     {
442         auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
443         ANGLE_TRY(storeDynamicAttrib(context, dynamicAttrib, start, count, instances));
444     }
445 
446     return gl::NoError();
447 }
448 
PromoteDynamicAttribs(const gl::Context * context,const std::vector<TranslatedAttribute> & translatedAttribs,const gl::AttributesMask & dynamicAttribsMask,GLsizei count)449 void VertexDataManager::PromoteDynamicAttribs(
450     const gl::Context *context,
451     const std::vector<TranslatedAttribute> &translatedAttribs,
452     const gl::AttributesMask &dynamicAttribsMask,
453     GLsizei count)
454 {
455     for (auto attribIndex : dynamicAttribsMask)
456     {
457         const auto &dynamicAttrib = translatedAttribs[attribIndex];
458         ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
459         const auto &binding       = *dynamicAttrib.binding;
460 
461         gl::Buffer *buffer = binding.getBuffer().get();
462         if (buffer)
463         {
464             BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
465             size_t typeSize      = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
466             bufferD3D->promoteStaticUsage(context, count * static_cast<int>(typeSize));
467         }
468     }
469 }
470 
reserveSpaceForAttrib(const TranslatedAttribute & translatedAttrib,GLint start,GLsizei count,GLsizei instances) const471 gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
472                                                    GLint start,
473                                                    GLsizei count,
474                                                    GLsizei instances) const
475 {
476     ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
477     const auto &attrib  = *translatedAttrib.attribute;
478     const auto &binding = *translatedAttrib.binding;
479 
480     ASSERT(!DirectStoragePossible(attrib, binding));
481 
482     gl::Buffer *buffer   = binding.getBuffer().get();
483     BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
484     ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
485 
486     size_t totalCount = gl::ComputeVertexBindingElementCount(
487         binding.getDivisor(), static_cast<size_t>(count), static_cast<size_t>(instances));
488     // TODO(jiajia.qin@intel.com): force the index buffer to clamp any out of range indices instead
489     // of invalid operation here.
490     if (bufferD3D)
491     {
492         // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
493         // a non-instanced draw call
494         GLint firstVertexIndex = binding.getDivisor() > 0 ? 0 : start;
495         int64_t maxVertexCount =
496             static_cast<int64_t>(firstVertexIndex) + static_cast<int64_t>(totalCount);
497 
498         int64_t maxByte = GetMaxAttributeByteOffsetForDraw(attrib, binding, maxVertexCount);
499 
500         ASSERT(bufferD3D->getSize() <= static_cast<size_t>(std::numeric_limits<int64_t>::max()));
501         if (maxByte > static_cast<int64_t>(bufferD3D->getSize()))
502         {
503             return gl::InvalidOperation() << "Vertex buffer is not big enough for the draw call.";
504         }
505     }
506     return mStreamingBuffer->reserveVertexSpace(attrib, binding, static_cast<GLsizei>(totalCount),
507                                                 instances);
508 }
509 
storeDynamicAttrib(const gl::Context * context,TranslatedAttribute * translated,GLint start,GLsizei count,GLsizei instances)510 gl::Error VertexDataManager::storeDynamicAttrib(const gl::Context *context,
511                                                 TranslatedAttribute *translated,
512                                                 GLint start,
513                                                 GLsizei count,
514                                                 GLsizei instances)
515 {
516     ASSERT(translated->attribute && translated->binding);
517     const auto &attrib  = *translated->attribute;
518     const auto &binding = *translated->binding;
519 
520     gl::Buffer *buffer = binding.getBuffer().get();
521     ASSERT(buffer || attrib.pointer);
522     ASSERT(attrib.enabled);
523 
524     BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
525 
526     // Instanced vertices do not apply the 'start' offset
527     GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
528 
529     // Compute source data pointer
530     const uint8_t *sourceData = nullptr;
531 
532     if (buffer)
533     {
534         ANGLE_TRY(storage->getData(context, &sourceData));
535         sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
536     }
537     else
538     {
539         // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
540         // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
541         sourceData = static_cast<const uint8_t*>(attrib.pointer);
542     }
543 
544     unsigned int streamOffset = 0;
545 
546     translated->storage = nullptr;
547     ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, binding, 1, 0), translated->stride);
548 
549     size_t totalCount = gl::ComputeVertexBindingElementCount(
550         binding.getDivisor(), static_cast<size_t>(count), static_cast<size_t>(instances));
551 
552     ANGLE_TRY(mStreamingBuffer->storeDynamicAttribute(
553         attrib, binding, translated->currentValueType, firstVertexIndex,
554         static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
555 
556     VertexBuffer *vertexBuffer = mStreamingBuffer->getVertexBuffer();
557 
558     translated->vertexBuffer.set(vertexBuffer);
559     translated->serial = vertexBuffer->getSerial();
560     translated->baseOffset            = streamOffset;
561     translated->usesFirstVertexOffset = false;
562 
563     return gl::NoError();
564 }
565 
storeCurrentValue(const gl::VertexAttribCurrentValueData & currentValue,TranslatedAttribute * translated,size_t attribIndex)566 gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
567                                                TranslatedAttribute *translated,
568                                                size_t attribIndex)
569 {
570     CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
571     auto &buffer                   = cachedState->buffer;
572 
573     if (!buffer)
574     {
575         buffer.reset(new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE));
576     }
577 
578     if (cachedState->data != currentValue)
579     {
580         ASSERT(translated->attribute && translated->binding);
581         const auto &attrib  = *translated->attribute;
582         const auto &binding = *translated->binding;
583 
584         ANGLE_TRY(buffer->reserveVertexSpace(attrib, binding, 1, 0));
585 
586         const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
587         unsigned int streamOffset;
588         ANGLE_TRY(buffer->storeDynamicAttribute(attrib, binding, currentValue.Type, 0, 1, 0,
589                                                 &streamOffset, sourceData));
590 
591         buffer->getVertexBuffer()->hintUnmapResource();
592 
593         cachedState->data = currentValue;
594         cachedState->offset = streamOffset;
595     }
596 
597     translated->vertexBuffer.set(buffer->getVertexBuffer());
598 
599     translated->storage = nullptr;
600     translated->serial  = buffer->getSerial();
601     translated->divisor = 0;
602     translated->stride  = 0;
603     translated->baseOffset            = static_cast<unsigned int>(cachedState->offset);
604     translated->usesFirstVertexOffset = false;
605 
606     return gl::NoError();
607 }
608 
609 // VertexBufferBinding implementation
VertexBufferBinding()610 VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
611 {
612 }
613 
VertexBufferBinding(const VertexBufferBinding & other)614 VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
615     : mBoundVertexBuffer(other.mBoundVertexBuffer)
616 {
617     if (mBoundVertexBuffer)
618     {
619         mBoundVertexBuffer->addRef();
620     }
621 }
622 
~VertexBufferBinding()623 VertexBufferBinding::~VertexBufferBinding()
624 {
625     if (mBoundVertexBuffer)
626     {
627         mBoundVertexBuffer->release();
628     }
629 }
630 
operator =(const VertexBufferBinding & other)631 VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
632 {
633     mBoundVertexBuffer = other.mBoundVertexBuffer;
634     if (mBoundVertexBuffer)
635     {
636         mBoundVertexBuffer->addRef();
637     }
638     return *this;
639 }
640 
set(VertexBuffer * vertexBuffer)641 void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
642 {
643     if (mBoundVertexBuffer == vertexBuffer)
644         return;
645 
646     if (mBoundVertexBuffer)
647     {
648         mBoundVertexBuffer->release();
649     }
650     if (vertexBuffer)
651     {
652         vertexBuffer->addRef();
653     }
654 
655     mBoundVertexBuffer = vertexBuffer;
656 }
657 
get() const658 VertexBuffer *VertexBufferBinding::get() const
659 {
660     return mBoundVertexBuffer;
661 }
662 
663 }  // namespace rx
664