blob: bf49c2035c81c9c3f076a7b0f1818b83641f403e [file] [log] [blame]
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +00007// VertexDataManager.h: Defines the VertexDataManager, a class that
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00008// runs the Buffer translation process.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/renderer/d3d/VertexDataManager.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040011
Geoff Lang2b5420c2014-11-19 14:20:15 -050012#include "libANGLE/Buffer.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050013#include "libANGLE/Program.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040014#include "libANGLE/State.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050015#include "libANGLE/VertexAttribute.h"
Geoff Lang5ead9272015-03-25 12:27:43 -040016#include "libANGLE/VertexArray.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040017#include "libANGLE/renderer/d3d/BufferD3D.h"
18#include "libANGLE/renderer/d3d/VertexBuffer.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000019
20namespace
21{
22 enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000023 // This has to be at least 4k or else it fails on ATI cards.
24 enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 };
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000025}
26
daniel@transgaming.com31240482012-11-28 21:06:41 +000027namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000028{
29
Brandon Jones5bf98292014-06-06 17:19:38 -070030static int ElementsInBuffer(const gl::VertexAttribute &attrib, unsigned int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000031{
Geoff Langa36ead42013-08-02 11:54:08 -040032 // Size cannot be larger than a GLsizei
33 if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
34 {
35 size = static_cast<unsigned int>(std::numeric_limits<int>::max());
36 }
37
Brandon Jones5bf98292014-06-06 17:19:38 -070038 GLsizei stride = ComputeVertexAttributeStride(attrib);
39 return (size - attrib.offset % stride + (stride - ComputeVertexAttributeTypeSize(attrib))) / stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000040}
41
Brandon Jones5bf98292014-06-06 17:19:38 -070042static int StreamingBufferElementCount(const gl::VertexAttribute &attrib, int vertexDrawCount, int instanceDrawCount)
Jamie Madill9b4f3842013-08-26 15:29:30 -040043{
44 // For instanced rendering, we draw "instanceDrawCount" sets of "vertexDrawCount" vertices.
45 //
46 // A vertex attribute with a positive divisor loads one instanced vertex for every set of
47 // non-instanced vertices, and the instanced vertex index advances once every "mDivisor" instances.
Brandon Jones5bf98292014-06-06 17:19:38 -070048 if (instanceDrawCount > 0 && attrib.divisor > 0)
Jamie Madill9b4f3842013-08-26 15:29:30 -040049 {
Gregoire Payen de La Garanderie2c7ea052015-01-07 12:47:35 +000050 // When instanceDrawCount is not a multiple attrib.divisor, the division must round up.
51 // For instance, with 5 non-instanced vertices and a divisor equal to 3, we need 2 instanced vertices.
52 return (instanceDrawCount + attrib.divisor - 1) / attrib.divisor;
Jamie Madill9b4f3842013-08-26 15:29:30 -040053 }
54
55 return vertexDrawCount;
56}
57
Jamie Madillb3f4e8d2015-06-22 13:57:17 -040058VertexDataManager::CurrentValueState::CurrentValueState()
59 : buffer(nullptr),
60 offset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000061{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -040062 data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
63 data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
64 data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
65 data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
66 data.Type = GL_FLOAT;
67}
daniel@transgaming.com83921382011-01-08 05:46:00 +000068
Jamie Madillb3f4e8d2015-06-22 13:57:17 -040069VertexDataManager::CurrentValueState::~CurrentValueState()
70{
71 SafeDelete(buffer);
72}
73
74VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
75 : mFactory(factory),
76 mStreamingBuffer(nullptr),
77 // TODO(jmadill): use context caps
78 mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS)
79{
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040080 mStreamingBuffer = new StreamingVertexBufferInterface(factory, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000081
82 if (!mStreamingBuffer)
83 {
84 ERR("Failed to allocate the streaming vertex buffer.");
85 }
Jamie Madill27c08912015-06-22 13:57:20 -040086
87 // TODO(jmadill): use context caps
88 mActiveEnabledAttributes.reserve(gl::MAX_VERTEX_ATTRIBS);
89 mActiveDisabledAttributes.reserve(gl::MAX_VERTEX_ATTRIBS);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000090}
91
92VertexDataManager::~VertexDataManager()
93{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -040094 SafeDelete(mStreamingBuffer);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000095}
96
Geoff Lang5ead9272015-03-25 12:27:43 -040097void VertexDataManager::hintUnmapAllResources(const std::vector<gl::VertexAttribute> &vertexAttributes)
Austin Kinrossbe0facc2015-01-07 16:22:29 -080098{
99 mStreamingBuffer->getVertexBuffer()->hintUnmapResource();
100
Jamie Madill27c08912015-06-22 13:57:20 -0400101 for (const TranslatedAttribute *translated : mActiveEnabledAttributes)
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800102 {
Jamie Madill27c08912015-06-22 13:57:20 -0400103 gl::Buffer *buffer = translated->attribute->buffer.get();
104 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
105 StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : nullptr;
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800106
Jamie Madill27c08912015-06-22 13:57:20 -0400107 if (staticBuffer)
108 {
109 staticBuffer->getVertexBuffer()->hintUnmapResource();
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800110 }
111 }
112
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400113 for (auto &currentValue : mCurrentValueCache)
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800114 {
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400115 if (currentValue.buffer != nullptr)
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800116 {
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400117 currentValue.buffer->getVertexBuffer()->hintUnmapResource();
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800118 }
119 }
120}
121
Shannon Woods1a965482014-09-22 18:00:32 -0400122gl::Error VertexDataManager::prepareVertexData(const gl::State &state, GLint start, GLsizei count,
123 TranslatedAttribute *translated, GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000124{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000125 if (!mStreamingBuffer)
126 {
Geoff Langf7100b92014-09-08 16:17:08 -0400127 return gl::Error(GL_OUT_OF_MEMORY, "Internal streaming vertex buffer is unexpectedly NULL.");
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000128 }
129
Jamie Madill27c08912015-06-22 13:57:20 -0400130 // Compute active enabled and active disable attributes, for speed.
131 // TODO(jmadill): don't recompute if there was no state change
Geoff Lang5ead9272015-03-25 12:27:43 -0400132 const gl::VertexArray *vertexArray = state.getVertexArray();
Jamie Madill46565a42015-06-22 13:57:21 -0400133 const int *semanticIndexes = state.getProgram()->getSemanticIndexes();
Geoff Lang5ead9272015-03-25 12:27:43 -0400134 const std::vector<gl::VertexAttribute> &vertexAttributes = vertexArray->getVertexAttributes();
135
Jamie Madill27c08912015-06-22 13:57:20 -0400136 mActiveEnabledAttributes.clear();
137 mActiveDisabledAttributes.clear();
138
Jamie Madill9c385802015-06-22 13:57:18 -0400139 for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000140 {
Jamie Madill46565a42015-06-22 13:57:21 -0400141 translated[attribIndex].active = (semanticIndexes[attribIndex] != -1);
Jamie Madill9c385802015-06-22 13:57:18 -0400142 if (translated[attribIndex].active)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000143 {
Jamie Madill9c385802015-06-22 13:57:18 -0400144 // Record the attribute now
145 translated[attribIndex].attribute = &vertexAttributes[attribIndex];
Jamie Madill3d72cc72015-06-22 13:57:19 -0400146 translated[attribIndex].currentValueType = state.getVertexAttribCurrentValue(attribIndex).Type;
147 translated[attribIndex].divisor = vertexAttributes[attribIndex].divisor;
Jamie Madill9c385802015-06-22 13:57:18 -0400148
149 if (vertexAttributes[attribIndex].enabled)
150 {
Jamie Madill27c08912015-06-22 13:57:20 -0400151 mActiveEnabledAttributes.push_back(&translated[attribIndex]);
152
Jamie Madill9c385802015-06-22 13:57:18 -0400153 // Also invalidate static buffers that don't contain matching attributes
154 invalidateMatchingStaticData(vertexAttributes[attribIndex],
155 state.getVertexAttribCurrentValue(attribIndex));
156 }
Jamie Madill27c08912015-06-22 13:57:20 -0400157 else
158 {
159 mActiveDisabledAttributes.push_back(attribIndex);
160 }
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000161 }
162 }
163
164 // Reserve the required space in the buffers
Jamie Madill27c08912015-06-22 13:57:20 -0400165 for (const TranslatedAttribute *translated : mActiveEnabledAttributes)
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000166 {
Jamie Madill27c08912015-06-22 13:57:20 -0400167 gl::Error error = reserveSpaceForAttrib(*translated, count, instances);
168 if (error.isError())
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000169 {
Jamie Madill27c08912015-06-22 13:57:20 -0400170 return error;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000171 }
172 }
173
174 // Perform the vertex data translations
Jamie Madill27c08912015-06-22 13:57:20 -0400175 for (TranslatedAttribute *translated : mActiveEnabledAttributes)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000176 {
Jamie Madill27c08912015-06-22 13:57:20 -0400177 gl::Error error = storeAttribute(translated, start, count, instances);
178
179 if (error.isError())
daniel@transgaming.com83921382011-01-08 05:46:00 +0000180 {
Jamie Madill27c08912015-06-22 13:57:20 -0400181 hintUnmapAllResources(vertexAttributes);
182 return error;
183 }
184 }
Shannon Woods1a965482014-09-22 18:00:32 -0400185
Jamie Madill27c08912015-06-22 13:57:20 -0400186 for (size_t attribIndex : mActiveDisabledAttributes)
187 {
188 if (mCurrentValueCache[attribIndex].buffer == nullptr)
189 {
190 mCurrentValueCache[attribIndex].buffer = new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE);
191 }
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000192
Jamie Madill27c08912015-06-22 13:57:20 -0400193 gl::Error error = storeCurrentValue(state.getVertexAttribCurrentValue(attribIndex),
194 &translated[attribIndex],
195 &mCurrentValueCache[attribIndex]);
196 if (error.isError())
197 {
198 hintUnmapAllResources(vertexAttributes);
199 return error;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000200 }
201 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000202
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800203 // Hint to unmap all the resources
Geoff Lang5ead9272015-03-25 12:27:43 -0400204 hintUnmapAllResources(vertexAttributes);
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800205
Jamie Madill27c08912015-06-22 13:57:20 -0400206 for (const TranslatedAttribute *translated : mActiveEnabledAttributes)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000207 {
Jamie Madill27c08912015-06-22 13:57:20 -0400208 gl::Buffer *buffer = translated->attribute->buffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000209
Jamie Madill27c08912015-06-22 13:57:20 -0400210 if (buffer)
211 {
212 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
213 size_t typeSize = ComputeVertexAttributeTypeSize(*translated->attribute);
214 bufferD3D->promoteStaticUsage(count * typeSize);
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000215 }
216 }
217
Geoff Langf7100b92014-09-08 16:17:08 -0400218 return gl::Error(GL_NO_ERROR);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000219}
220
Jamie Madill6d113802014-08-25 15:47:52 -0400221void VertexDataManager::invalidateMatchingStaticData(const gl::VertexAttribute &attrib,
222 const gl::VertexAttribCurrentValueData &currentValue) const
223{
224 gl::Buffer *buffer = attrib.buffer.get();
225
226 if (buffer)
227 {
Jamie Madill9236b412015-02-02 16:51:52 -0500228 BufferD3D *bufferImpl = GetImplAs<BufferD3D>(buffer);
Jamie Madill6d113802014-08-25 15:47:52 -0400229 StaticVertexBufferInterface *staticBuffer = bufferImpl->getStaticVertexBuffer();
230
231 if (staticBuffer &&
232 staticBuffer->getBufferSize() > 0 &&
233 !staticBuffer->lookupAttribute(attrib, NULL) &&
Jamie Madill3d72cc72015-06-22 13:57:19 -0400234 !staticBuffer->directStoragePossible(attrib, currentValue.Type))
Jamie Madill6d113802014-08-25 15:47:52 -0400235 {
236 bufferImpl->invalidateStaticData();
237 }
238 }
239}
240
Jamie Madill3d72cc72015-06-22 13:57:19 -0400241gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
Geoff Langf7100b92014-09-08 16:17:08 -0400242 GLsizei count,
243 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400244{
Jamie Madill3d72cc72015-06-22 13:57:19 -0400245 const gl::VertexAttribute &attrib = *translatedAttrib.attribute;
Jamie Madill6d113802014-08-25 15:47:52 -0400246 gl::Buffer *buffer = attrib.buffer.get();
Jamie Madill9236b412015-02-02 16:51:52 -0500247 BufferD3D *bufferImpl = buffer ? GetImplAs<BufferD3D>(buffer) : NULL;
Jamie Madill6d113802014-08-25 15:47:52 -0400248 StaticVertexBufferInterface *staticBuffer = bufferImpl ? bufferImpl->getStaticVertexBuffer() : NULL;
249 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
250
Jamie Madill3d72cc72015-06-22 13:57:19 -0400251 if (!vertexBuffer->directStoragePossible(attrib, translatedAttrib.currentValueType))
Jamie Madill6d113802014-08-25 15:47:52 -0400252 {
253 if (staticBuffer)
254 {
255 if (staticBuffer->getBufferSize() == 0)
256 {
257 int totalCount = ElementsInBuffer(attrib, bufferImpl->getSize());
Geoff Langf7100b92014-09-08 16:17:08 -0400258 gl::Error error = staticBuffer->reserveVertexSpace(attrib, totalCount, 0);
259 if (error.isError())
Jamie Madill6d113802014-08-25 15:47:52 -0400260 {
Geoff Langf7100b92014-09-08 16:17:08 -0400261 return error;
Jamie Madill6d113802014-08-25 15:47:52 -0400262 }
263 }
264 }
265 else
266 {
267 int totalCount = StreamingBufferElementCount(attrib, count, instances);
268 ASSERT(!bufferImpl || ElementsInBuffer(attrib, bufferImpl->getSize()) >= totalCount);
269
Geoff Langf7100b92014-09-08 16:17:08 -0400270 gl::Error error = mStreamingBuffer->reserveVertexSpace(attrib, totalCount, instances);
271 if (error.isError())
Jamie Madill6d113802014-08-25 15:47:52 -0400272 {
Geoff Langf7100b92014-09-08 16:17:08 -0400273 return error;
Jamie Madill6d113802014-08-25 15:47:52 -0400274 }
275 }
276 }
277
Geoff Langf7100b92014-09-08 16:17:08 -0400278 return gl::Error(GL_NO_ERROR);
Jamie Madill6d113802014-08-25 15:47:52 -0400279}
280
Jamie Madill3d72cc72015-06-22 13:57:19 -0400281gl::Error VertexDataManager::storeAttribute(TranslatedAttribute *translated,
Geoff Langf7100b92014-09-08 16:17:08 -0400282 GLint start,
283 GLsizei count,
284 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400285{
Jamie Madill9c385802015-06-22 13:57:18 -0400286 const gl::VertexAttribute &attrib = *translated->attribute;
287
Jamie Madillf41522b2014-08-18 16:39:49 -0400288 gl::Buffer *buffer = attrib.buffer.get();
Jamie Madill2b976812014-08-25 15:47:49 -0400289 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400290 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400291
Jamie Madill9236b412015-02-02 16:51:52 -0500292 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : NULL;
Jamie Madillf41522b2014-08-18 16:39:49 -0400293 StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : NULL;
294 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
Jamie Madill3d72cc72015-06-22 13:57:19 -0400295 bool directStorage = vertexBuffer->directStoragePossible(attrib, translated->currentValueType);
Jamie Madillf41522b2014-08-18 16:39:49 -0400296
Jamie Madilld4b55a02015-01-09 14:21:49 -0500297 // Instanced vertices do not apply the 'start' offset
298 GLint firstVertexIndex = (instances > 0 && attrib.divisor > 0 ? 0 : start);
299
Jamie Madill7d112bb2015-06-22 13:57:19 -0400300 translated->vertexBuffer = vertexBuffer->getVertexBuffer();
301
Jamie Madillf41522b2014-08-18 16:39:49 -0400302 if (directStorage)
303 {
Jamie Madill7d112bb2015-06-22 13:57:19 -0400304 translated->storage = storage;
305 translated->serial = storage->getSerial();
Jamie Madill7d112bb2015-06-22 13:57:19 -0400306 translated->stride = ComputeVertexAttributeStride(attrib);
307 translated->offset = static_cast<unsigned int>(attrib.offset + translated->stride * firstVertexIndex);
308
309 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400310 }
Jamie Madill7d112bb2015-06-22 13:57:19 -0400311
312 // Compute source data pointer
313 const uint8_t *sourceData = nullptr;
314
315 if (buffer)
316 {
317 gl::Error error = storage->getData(&sourceData);
318 if (error.isError())
319 {
320 return error;
321 }
322 sourceData += static_cast<int>(attrib.offset);
323 }
324 else
325 {
326 sourceData = static_cast<const uint8_t*>(attrib.pointer);
327 }
328
329 unsigned int streamOffset = 0;
330 unsigned int outputElementSize = 0;
331
332 if (staticBuffer)
Jamie Madillf41522b2014-08-18 16:39:49 -0400333 {
Geoff Langf7100b92014-09-08 16:17:08 -0400334 gl::Error error = staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
335 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400336 {
Geoff Langf7100b92014-09-08 16:17:08 -0400337 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400338 }
339
340 if (!staticBuffer->lookupAttribute(attrib, &streamOffset))
341 {
342 // Convert the entire buffer
343 int totalCount = ElementsInBuffer(attrib, storage->getSize());
344 int startIndex = attrib.offset / ComputeVertexAttributeStride(attrib);
345
Jamie Madill7d112bb2015-06-22 13:57:19 -0400346 error = staticBuffer->storeVertexAttributes(attrib,
Jamie Madill3d72cc72015-06-22 13:57:19 -0400347 translated->currentValueType,
Jamie Madill7d112bb2015-06-22 13:57:19 -0400348 -startIndex,
349 totalCount,
350 0,
351 &streamOffset,
352 sourceData);
Geoff Langf7100b92014-09-08 16:17:08 -0400353 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400354 {
Geoff Langf7100b92014-09-08 16:17:08 -0400355 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400356 }
357 }
358
359 unsigned int firstElementOffset = (attrib.offset / ComputeVertexAttributeStride(attrib)) * outputElementSize;
Jamie Madilld4b55a02015-01-09 14:21:49 -0500360 unsigned int startOffset = (instances == 0 || attrib.divisor == 0) ? firstVertexIndex * outputElementSize : 0;
Jamie Madillf41522b2014-08-18 16:39:49 -0400361 if (streamOffset + firstElementOffset + startOffset < streamOffset)
362 {
Geoff Langf7100b92014-09-08 16:17:08 -0400363 return gl::Error(GL_OUT_OF_MEMORY);
Jamie Madillf41522b2014-08-18 16:39:49 -0400364 }
365
366 streamOffset += firstElementOffset + startOffset;
367 }
368 else
369 {
370 int totalCount = StreamingBufferElementCount(attrib, count, instances);
Geoff Langf7100b92014-09-08 16:17:08 -0400371 gl::Error error = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
372 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400373 {
Geoff Langf7100b92014-09-08 16:17:08 -0400374 return error;
375 }
376
Jamie Madill7d112bb2015-06-22 13:57:19 -0400377 error = mStreamingBuffer->storeVertexAttributes(attrib,
Jamie Madill3d72cc72015-06-22 13:57:19 -0400378 translated->currentValueType,
Jamie Madill7d112bb2015-06-22 13:57:19 -0400379 firstVertexIndex,
380 totalCount,
381 instances,
382 &streamOffset,
383 sourceData);
Geoff Langf7100b92014-09-08 16:17:08 -0400384 if (error.isError())
385 {
386 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400387 }
388 }
389
Jamie Madill7d112bb2015-06-22 13:57:19 -0400390 translated->storage = nullptr;
391 translated->serial = vertexBuffer->getSerial();
Jamie Madillf41522b2014-08-18 16:39:49 -0400392 translated->stride = outputElementSize;
393 translated->offset = streamOffset;
394
Geoff Langf7100b92014-09-08 16:17:08 -0400395 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400396}
397
Jamie Madill9c385802015-06-22 13:57:18 -0400398gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400399 TranslatedAttribute *translated,
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400400 CurrentValueState *cachedState)
Jamie Madillf41522b2014-08-18 16:39:49 -0400401{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400402 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400403 {
Jamie Madill9c385802015-06-22 13:57:18 -0400404 const gl::VertexAttribute &attrib = *translated->attribute;
Jamie Madill27c08912015-06-22 13:57:20 -0400405
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400406 gl::Error error = cachedState->buffer->reserveVertexSpace(attrib, 1, 0);
Geoff Langf7100b92014-09-08 16:17:08 -0400407 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400408 {
Geoff Langf7100b92014-09-08 16:17:08 -0400409 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400410 }
411
Jamie Madill7d112bb2015-06-22 13:57:19 -0400412 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400413 unsigned int streamOffset;
Jamie Madill3d72cc72015-06-22 13:57:19 -0400414 error = cachedState->buffer->storeVertexAttributes(attrib, currentValue.Type, 0, 1, 0, &streamOffset, sourceData);
Geoff Langf7100b92014-09-08 16:17:08 -0400415 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400416 {
Geoff Langf7100b92014-09-08 16:17:08 -0400417 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400418 }
419
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400420 cachedState->data = currentValue;
421 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400422 }
423
424 translated->storage = NULL;
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400425 translated->vertexBuffer = cachedState->buffer->getVertexBuffer();
426 translated->serial = cachedState->buffer->getSerial();
Jamie Madillf41522b2014-08-18 16:39:49 -0400427 translated->divisor = 0;
428
Jamie Madillf41522b2014-08-18 16:39:49 -0400429 translated->stride = 0;
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400430 translated->offset = cachedState->offset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400431
Geoff Langf7100b92014-09-08 16:17:08 -0400432 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400433}
434
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000435}