blob: 4fd4351f86f7ba176e86c01a42d68c6df4e60bc2 [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
Cooper Partinc5cf9bc2015-08-06 10:46:48 -070038 GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib));
39 return (size - attrib.offset % stride +
40 (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) /
41 stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000042}
43
Brandon Jones5bf98292014-06-06 17:19:38 -070044static int StreamingBufferElementCount(const gl::VertexAttribute &attrib, int vertexDrawCount, int instanceDrawCount)
Jamie Madill9b4f3842013-08-26 15:29:30 -040045{
46 // For instanced rendering, we draw "instanceDrawCount" sets of "vertexDrawCount" vertices.
47 //
48 // A vertex attribute with a positive divisor loads one instanced vertex for every set of
49 // non-instanced vertices, and the instanced vertex index advances once every "mDivisor" instances.
Brandon Jones5bf98292014-06-06 17:19:38 -070050 if (instanceDrawCount > 0 && attrib.divisor > 0)
Jamie Madill9b4f3842013-08-26 15:29:30 -040051 {
Gregoire Payen de La Garanderie2c7ea052015-01-07 12:47:35 +000052 // When instanceDrawCount is not a multiple attrib.divisor, the division must round up.
53 // For instance, with 5 non-instanced vertices and a divisor equal to 3, we need 2 instanced vertices.
54 return (instanceDrawCount + attrib.divisor - 1) / attrib.divisor;
Jamie Madill9b4f3842013-08-26 15:29:30 -040055 }
56
57 return vertexDrawCount;
58}
59
Jamie Madillb3f4e8d2015-06-22 13:57:17 -040060VertexDataManager::CurrentValueState::CurrentValueState()
61 : buffer(nullptr),
62 offset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000063{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -040064 data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
65 data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
66 data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
67 data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
68 data.Type = GL_FLOAT;
69}
daniel@transgaming.com83921382011-01-08 05:46:00 +000070
Jamie Madillb3f4e8d2015-06-22 13:57:17 -040071VertexDataManager::CurrentValueState::~CurrentValueState()
72{
73 SafeDelete(buffer);
74}
75
76VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
77 : mFactory(factory),
78 mStreamingBuffer(nullptr),
79 // TODO(jmadill): use context caps
80 mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS)
81{
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040082 mStreamingBuffer = new StreamingVertexBufferInterface(factory, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000083
84 if (!mStreamingBuffer)
85 {
86 ERR("Failed to allocate the streaming vertex buffer.");
87 }
Jamie Madill27c08912015-06-22 13:57:20 -040088
89 // TODO(jmadill): use context caps
90 mActiveEnabledAttributes.reserve(gl::MAX_VERTEX_ATTRIBS);
91 mActiveDisabledAttributes.reserve(gl::MAX_VERTEX_ATTRIBS);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000092}
93
94VertexDataManager::~VertexDataManager()
95{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -040096 SafeDelete(mStreamingBuffer);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000097}
98
Geoff Lang5ead9272015-03-25 12:27:43 -040099void VertexDataManager::hintUnmapAllResources(const std::vector<gl::VertexAttribute> &vertexAttributes)
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800100{
101 mStreamingBuffer->getVertexBuffer()->hintUnmapResource();
102
Jamie Madill27c08912015-06-22 13:57:20 -0400103 for (const TranslatedAttribute *translated : mActiveEnabledAttributes)
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800104 {
Jamie Madill27c08912015-06-22 13:57:20 -0400105 gl::Buffer *buffer = translated->attribute->buffer.get();
106 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
107 StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : nullptr;
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800108
Jamie Madill27c08912015-06-22 13:57:20 -0400109 if (staticBuffer)
110 {
111 staticBuffer->getVertexBuffer()->hintUnmapResource();
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800112 }
113 }
114
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400115 for (auto &currentValue : mCurrentValueCache)
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800116 {
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400117 if (currentValue.buffer != nullptr)
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800118 {
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400119 currentValue.buffer->getVertexBuffer()->hintUnmapResource();
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800120 }
121 }
122}
123
Jamie Madill476682e2015-06-30 10:04:29 -0400124gl::Error VertexDataManager::prepareVertexData(const gl::State &state,
125 GLint start,
126 GLsizei count,
127 std::vector<TranslatedAttribute> *translatedAttribs,
128 GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000129{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000130 if (!mStreamingBuffer)
131 {
Geoff Langf7100b92014-09-08 16:17:08 -0400132 return gl::Error(GL_OUT_OF_MEMORY, "Internal streaming vertex buffer is unexpectedly NULL.");
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000133 }
134
Jamie Madill27c08912015-06-22 13:57:20 -0400135 // Compute active enabled and active disable attributes, for speed.
136 // TODO(jmadill): don't recompute if there was no state change
Geoff Lang5ead9272015-03-25 12:27:43 -0400137 const gl::VertexArray *vertexArray = state.getVertexArray();
Jamie Madill46565a42015-06-22 13:57:21 -0400138 const int *semanticIndexes = state.getProgram()->getSemanticIndexes();
Geoff Lang5ead9272015-03-25 12:27:43 -0400139 const std::vector<gl::VertexAttribute> &vertexAttributes = vertexArray->getVertexAttributes();
140
Jamie Madill27c08912015-06-22 13:57:20 -0400141 mActiveEnabledAttributes.clear();
142 mActiveDisabledAttributes.clear();
Jamie Madill476682e2015-06-30 10:04:29 -0400143 translatedAttribs->clear();
Jamie Madill27c08912015-06-22 13:57:20 -0400144
Jamie Madill9c385802015-06-22 13:57:18 -0400145 for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146 {
Jamie Madill476682e2015-06-30 10:04:29 -0400147 if (semanticIndexes[attribIndex] != -1)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000148 {
Jamie Madill476682e2015-06-30 10:04:29 -0400149 // Resize automatically puts in empty attribs
150 translatedAttribs->resize(attribIndex + 1);
151
152 TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
153
Jamie Madill9c385802015-06-22 13:57:18 -0400154 // Record the attribute now
Jamie Madill476682e2015-06-30 10:04:29 -0400155 translated->active = true;
156 translated->attribute = &vertexAttributes[attribIndex];
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700157 translated->currentValueType =
158 state.getVertexAttribCurrentValue(static_cast<unsigned int>(attribIndex)).Type;
Jamie Madill476682e2015-06-30 10:04:29 -0400159 translated->divisor = vertexAttributes[attribIndex].divisor;
Jamie Madill9c385802015-06-22 13:57:18 -0400160
161 if (vertexAttributes[attribIndex].enabled)
162 {
Jamie Madill476682e2015-06-30 10:04:29 -0400163 mActiveEnabledAttributes.push_back(translated);
Jamie Madill27c08912015-06-22 13:57:20 -0400164
Jamie Madill9c385802015-06-22 13:57:18 -0400165 // Also invalidate static buffers that don't contain matching attributes
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700166 invalidateMatchingStaticData(
167 vertexAttributes[attribIndex],
168 state.getVertexAttribCurrentValue(static_cast<unsigned int>(attribIndex)));
Jamie Madill9c385802015-06-22 13:57:18 -0400169 }
Jamie Madill27c08912015-06-22 13:57:20 -0400170 else
171 {
172 mActiveDisabledAttributes.push_back(attribIndex);
173 }
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000174 }
175 }
176
177 // Reserve the required space in the buffers
Jamie Madill16f99b72015-07-02 14:09:06 -0400178 for (const TranslatedAttribute *activeAttrib : mActiveEnabledAttributes)
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000179 {
Jamie Madill16f99b72015-07-02 14:09:06 -0400180 gl::Error error = reserveSpaceForAttrib(*activeAttrib, count, instances);
Jamie Madill27c08912015-06-22 13:57:20 -0400181 if (error.isError())
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000182 {
Jamie Madill27c08912015-06-22 13:57:20 -0400183 return error;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000184 }
185 }
186
187 // Perform the vertex data translations
Jamie Madill16f99b72015-07-02 14:09:06 -0400188 for (TranslatedAttribute *activeAttrib : mActiveEnabledAttributes)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000189 {
Jamie Madill16f99b72015-07-02 14:09:06 -0400190 gl::Error error = storeAttribute(activeAttrib, start, count, instances);
Jamie Madill27c08912015-06-22 13:57:20 -0400191
192 if (error.isError())
daniel@transgaming.com83921382011-01-08 05:46:00 +0000193 {
Jamie Madill27c08912015-06-22 13:57:20 -0400194 hintUnmapAllResources(vertexAttributes);
195 return error;
196 }
197 }
Shannon Woods1a965482014-09-22 18:00:32 -0400198
Jamie Madill27c08912015-06-22 13:57:20 -0400199 for (size_t attribIndex : mActiveDisabledAttributes)
200 {
201 if (mCurrentValueCache[attribIndex].buffer == nullptr)
202 {
203 mCurrentValueCache[attribIndex].buffer = new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE);
204 }
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000205
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700206 gl::Error error = storeCurrentValue(
207 state.getVertexAttribCurrentValue(static_cast<unsigned int>(attribIndex)),
208 &(*translatedAttribs)[attribIndex], &mCurrentValueCache[attribIndex]);
Jamie Madill27c08912015-06-22 13:57:20 -0400209 if (error.isError())
210 {
211 hintUnmapAllResources(vertexAttributes);
212 return error;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000213 }
214 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000215
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800216 // Hint to unmap all the resources
Geoff Lang5ead9272015-03-25 12:27:43 -0400217 hintUnmapAllResources(vertexAttributes);
Austin Kinrossbe0facc2015-01-07 16:22:29 -0800218
Jamie Madill16f99b72015-07-02 14:09:06 -0400219 for (const TranslatedAttribute *activeAttrib : mActiveEnabledAttributes)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000220 {
Jamie Madill16f99b72015-07-02 14:09:06 -0400221 gl::Buffer *buffer = activeAttrib->attribute->buffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000222
Jamie Madill27c08912015-06-22 13:57:20 -0400223 if (buffer)
224 {
225 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jamie Madill16f99b72015-07-02 14:09:06 -0400226 size_t typeSize = ComputeVertexAttributeTypeSize(*activeAttrib->attribute);
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700227 bufferD3D->promoteStaticUsage(count * static_cast<int>(typeSize));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000228 }
229 }
230
Geoff Langf7100b92014-09-08 16:17:08 -0400231 return gl::Error(GL_NO_ERROR);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000232}
233
Jamie Madill6d113802014-08-25 15:47:52 -0400234void VertexDataManager::invalidateMatchingStaticData(const gl::VertexAttribute &attrib,
235 const gl::VertexAttribCurrentValueData &currentValue) const
236{
237 gl::Buffer *buffer = attrib.buffer.get();
238
239 if (buffer)
240 {
Jamie Madill9236b412015-02-02 16:51:52 -0500241 BufferD3D *bufferImpl = GetImplAs<BufferD3D>(buffer);
Jamie Madill6d113802014-08-25 15:47:52 -0400242 StaticVertexBufferInterface *staticBuffer = bufferImpl->getStaticVertexBuffer();
243
244 if (staticBuffer &&
245 staticBuffer->getBufferSize() > 0 &&
246 !staticBuffer->lookupAttribute(attrib, NULL) &&
Jamie Madill3d72cc72015-06-22 13:57:19 -0400247 !staticBuffer->directStoragePossible(attrib, currentValue.Type))
Jamie Madill6d113802014-08-25 15:47:52 -0400248 {
249 bufferImpl->invalidateStaticData();
250 }
251 }
252}
253
Jamie Madill3d72cc72015-06-22 13:57:19 -0400254gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
Geoff Langf7100b92014-09-08 16:17:08 -0400255 GLsizei count,
256 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400257{
Jamie Madill3d72cc72015-06-22 13:57:19 -0400258 const gl::VertexAttribute &attrib = *translatedAttrib.attribute;
Jamie Madill6d113802014-08-25 15:47:52 -0400259 gl::Buffer *buffer = attrib.buffer.get();
Jamie Madill9236b412015-02-02 16:51:52 -0500260 BufferD3D *bufferImpl = buffer ? GetImplAs<BufferD3D>(buffer) : NULL;
Jamie Madill6d113802014-08-25 15:47:52 -0400261 StaticVertexBufferInterface *staticBuffer = bufferImpl ? bufferImpl->getStaticVertexBuffer() : NULL;
262 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
263
Jamie Madill3d72cc72015-06-22 13:57:19 -0400264 if (!vertexBuffer->directStoragePossible(attrib, translatedAttrib.currentValueType))
Jamie Madill6d113802014-08-25 15:47:52 -0400265 {
266 if (staticBuffer)
267 {
268 if (staticBuffer->getBufferSize() == 0)
269 {
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700270 int totalCount =
271 ElementsInBuffer(attrib, static_cast<unsigned int>(bufferImpl->getSize()));
Geoff Langf7100b92014-09-08 16:17:08 -0400272 gl::Error error = staticBuffer->reserveVertexSpace(attrib, totalCount, 0);
273 if (error.isError())
Jamie Madill6d113802014-08-25 15:47:52 -0400274 {
Geoff Langf7100b92014-09-08 16:17:08 -0400275 return error;
Jamie Madill6d113802014-08-25 15:47:52 -0400276 }
277 }
278 }
279 else
280 {
281 int totalCount = StreamingBufferElementCount(attrib, count, instances);
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700282 ASSERT(!bufferImpl ||
283 ElementsInBuffer(attrib, static_cast<unsigned int>(bufferImpl->getSize())) >=
284 totalCount);
Jamie Madill6d113802014-08-25 15:47:52 -0400285
Geoff Langf7100b92014-09-08 16:17:08 -0400286 gl::Error error = mStreamingBuffer->reserveVertexSpace(attrib, totalCount, instances);
287 if (error.isError())
Jamie Madill6d113802014-08-25 15:47:52 -0400288 {
Geoff Langf7100b92014-09-08 16:17:08 -0400289 return error;
Jamie Madill6d113802014-08-25 15:47:52 -0400290 }
291 }
292 }
293
Geoff Langf7100b92014-09-08 16:17:08 -0400294 return gl::Error(GL_NO_ERROR);
Jamie Madill6d113802014-08-25 15:47:52 -0400295}
296
Jamie Madill3d72cc72015-06-22 13:57:19 -0400297gl::Error VertexDataManager::storeAttribute(TranslatedAttribute *translated,
Geoff Langf7100b92014-09-08 16:17:08 -0400298 GLint start,
299 GLsizei count,
300 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400301{
Jamie Madill9c385802015-06-22 13:57:18 -0400302 const gl::VertexAttribute &attrib = *translated->attribute;
303
Jamie Madillf41522b2014-08-18 16:39:49 -0400304 gl::Buffer *buffer = attrib.buffer.get();
Jamie Madill2b976812014-08-25 15:47:49 -0400305 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400306 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400307
Jamie Madill9236b412015-02-02 16:51:52 -0500308 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : NULL;
Jamie Madillf41522b2014-08-18 16:39:49 -0400309 StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : NULL;
310 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
Jamie Madill3d72cc72015-06-22 13:57:19 -0400311 bool directStorage = vertexBuffer->directStoragePossible(attrib, translated->currentValueType);
Jamie Madillf41522b2014-08-18 16:39:49 -0400312
Jamie Madilld4b55a02015-01-09 14:21:49 -0500313 // Instanced vertices do not apply the 'start' offset
314 GLint firstVertexIndex = (instances > 0 && attrib.divisor > 0 ? 0 : start);
315
Jamie Madill7d112bb2015-06-22 13:57:19 -0400316 translated->vertexBuffer = vertexBuffer->getVertexBuffer();
317
Jamie Madillf41522b2014-08-18 16:39:49 -0400318 if (directStorage)
319 {
Jamie Madill7d112bb2015-06-22 13:57:19 -0400320 translated->storage = storage;
321 translated->serial = storage->getSerial();
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700322 translated->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400323 translated->offset = static_cast<unsigned int>(attrib.offset + translated->stride * firstVertexIndex);
324
325 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400326 }
Jamie Madill7d112bb2015-06-22 13:57:19 -0400327
328 // Compute source data pointer
329 const uint8_t *sourceData = nullptr;
330
331 if (buffer)
332 {
333 gl::Error error = storage->getData(&sourceData);
334 if (error.isError())
335 {
336 return error;
337 }
338 sourceData += static_cast<int>(attrib.offset);
339 }
340 else
341 {
342 sourceData = static_cast<const uint8_t*>(attrib.pointer);
343 }
344
345 unsigned int streamOffset = 0;
346 unsigned int outputElementSize = 0;
347
348 if (staticBuffer)
Jamie Madillf41522b2014-08-18 16:39:49 -0400349 {
Geoff Langf7100b92014-09-08 16:17:08 -0400350 gl::Error error = staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
351 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400352 {
Geoff Langf7100b92014-09-08 16:17:08 -0400353 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400354 }
355
356 if (!staticBuffer->lookupAttribute(attrib, &streamOffset))
357 {
358 // Convert the entire buffer
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700359 int totalCount =
360 ElementsInBuffer(attrib, static_cast<unsigned int>(storage->getSize()));
361 int startIndex = static_cast<int>(attrib.offset) /
362 static_cast<int>(ComputeVertexAttributeStride(attrib));
Jamie Madillf41522b2014-08-18 16:39:49 -0400363
Jamie Madill7d112bb2015-06-22 13:57:19 -0400364 error = staticBuffer->storeVertexAttributes(attrib,
Jamie Madill3d72cc72015-06-22 13:57:19 -0400365 translated->currentValueType,
Jamie Madill7d112bb2015-06-22 13:57:19 -0400366 -startIndex,
367 totalCount,
368 0,
369 &streamOffset,
370 sourceData);
Geoff Langf7100b92014-09-08 16:17:08 -0400371 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400372 {
Geoff Langf7100b92014-09-08 16:17:08 -0400373 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400374 }
375 }
376
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700377 unsigned int firstElementOffset =
378 (static_cast<unsigned int>(attrib.offset) /
379 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib))) *
380 outputElementSize;
Jamie Madilld4b55a02015-01-09 14:21:49 -0500381 unsigned int startOffset = (instances == 0 || attrib.divisor == 0) ? firstVertexIndex * outputElementSize : 0;
Jamie Madillf41522b2014-08-18 16:39:49 -0400382 if (streamOffset + firstElementOffset + startOffset < streamOffset)
383 {
Geoff Langf7100b92014-09-08 16:17:08 -0400384 return gl::Error(GL_OUT_OF_MEMORY);
Jamie Madillf41522b2014-08-18 16:39:49 -0400385 }
386
387 streamOffset += firstElementOffset + startOffset;
388 }
389 else
390 {
391 int totalCount = StreamingBufferElementCount(attrib, count, instances);
Geoff Langf7100b92014-09-08 16:17:08 -0400392 gl::Error error = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
393 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400394 {
Geoff Langf7100b92014-09-08 16:17:08 -0400395 return error;
396 }
397
Jamie Madill7d112bb2015-06-22 13:57:19 -0400398 error = mStreamingBuffer->storeVertexAttributes(attrib,
Jamie Madill3d72cc72015-06-22 13:57:19 -0400399 translated->currentValueType,
Jamie Madill7d112bb2015-06-22 13:57:19 -0400400 firstVertexIndex,
401 totalCount,
402 instances,
403 &streamOffset,
404 sourceData);
Geoff Langf7100b92014-09-08 16:17:08 -0400405 if (error.isError())
406 {
407 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400408 }
409 }
410
Jamie Madill7d112bb2015-06-22 13:57:19 -0400411 translated->storage = nullptr;
412 translated->serial = vertexBuffer->getSerial();
Jamie Madillf41522b2014-08-18 16:39:49 -0400413 translated->stride = outputElementSize;
414 translated->offset = streamOffset;
415
Geoff Langf7100b92014-09-08 16:17:08 -0400416 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400417}
418
Jamie Madill9c385802015-06-22 13:57:18 -0400419gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400420 TranslatedAttribute *translated,
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400421 CurrentValueState *cachedState)
Jamie Madillf41522b2014-08-18 16:39:49 -0400422{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400423 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400424 {
Jamie Madill9c385802015-06-22 13:57:18 -0400425 const gl::VertexAttribute &attrib = *translated->attribute;
Jamie Madill27c08912015-06-22 13:57:20 -0400426
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400427 gl::Error error = cachedState->buffer->reserveVertexSpace(attrib, 1, 0);
Geoff Langf7100b92014-09-08 16:17:08 -0400428 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400429 {
Geoff Langf7100b92014-09-08 16:17:08 -0400430 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400431 }
432
Jamie Madill7d112bb2015-06-22 13:57:19 -0400433 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400434 unsigned int streamOffset;
Jamie Madill3d72cc72015-06-22 13:57:19 -0400435 error = cachedState->buffer->storeVertexAttributes(attrib, currentValue.Type, 0, 1, 0, &streamOffset, sourceData);
Geoff Langf7100b92014-09-08 16:17:08 -0400436 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400437 {
Geoff Langf7100b92014-09-08 16:17:08 -0400438 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400439 }
440
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400441 cachedState->data = currentValue;
442 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400443 }
444
445 translated->storage = NULL;
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400446 translated->vertexBuffer = cachedState->buffer->getVertexBuffer();
447 translated->serial = cachedState->buffer->getSerial();
Jamie Madillf41522b2014-08-18 16:39:49 -0400448 translated->divisor = 0;
449
Jamie Madillf41522b2014-08-18 16:39:49 -0400450 translated->stride = 0;
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700451 translated->offset = static_cast<unsigned int>(cachedState->offset);
Jamie Madillf41522b2014-08-18 16:39:49 -0400452
Geoff Langf7100b92014-09-08 16:17:08 -0400453 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400454}
455
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000456}