blob: 724a58b7fc267e0a4211b88cb926fa0fea45768e [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"
Jamie Madilldbfc6c62016-02-29 01:08:57 -050013#include "libANGLE/formatutils.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050014#include "libANGLE/Program.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040015#include "libANGLE/State.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/VertexAttribute.h"
Geoff Lang5ead9272015-03-25 12:27:43 -040017#include "libANGLE/VertexArray.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040018#include "libANGLE/renderer/d3d/BufferD3D.h"
Jamie Madilldbfc6c62016-02-29 01:08:57 -050019#include "libANGLE/renderer/d3d/RendererD3D.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040020#include "libANGLE/renderer/d3d/VertexBuffer.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000021
daniel@transgaming.com31240482012-11-28 21:06:41 +000022namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000023{
Jamie Madilldbfc6c62016-02-29 01:08:57 -050024namespace
25{
26enum
27{
28 INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024
29};
30// This has to be at least 4k or else it fails on ATI cards.
31enum
32{
33 CONSTANT_VERTEX_BUFFER_SIZE = 4096
34};
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000035
Jamie Madilldbfc6c62016-02-29 01:08:57 -050036int ElementsInBuffer(const gl::VertexAttribute &attrib, unsigned int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000037{
Geoff Langa36ead42013-08-02 11:54:08 -040038 // Size cannot be larger than a GLsizei
39 if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
40 {
41 size = static_cast<unsigned int>(std::numeric_limits<int>::max());
42 }
43
Cooper Partin4d61f7e2015-08-12 10:56:50 -070044 GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib));
45 return (size - attrib.offset % stride +
46 (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) /
47 stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000048}
49
Jamie Madilldbfc6c62016-02-29 01:08:57 -050050bool DirectStoragePossible(const gl::VertexAttribute &attrib)
51{
52 // Current value attribs may not use direct storage.
53 if (!attrib.enabled)
54 {
55 return false;
56 }
57
58 gl::Buffer *buffer = attrib.buffer.get();
59 if (!buffer)
60 {
61 return false;
62 }
63
64 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
65 ASSERT(bufferD3D);
66 if (!bufferD3D->supportsDirectBinding())
67 {
68 return false;
69 }
70
Jamie Madilldbfc6c62016-02-29 01:08:57 -050071 // Alignment restrictions: In D3D, vertex data must be aligned to the format stride, or to a
72 // 4-byte boundary, whichever is smaller. (Undocumented, and experimentally confirmed)
Jamie Madilld8fa9212016-03-02 11:51:43 -050073 size_t alignment = 4;
Jamie Madilldbfc6c62016-02-29 01:08:57 -050074
75 if (attrib.type != GL_FLOAT)
76 {
77 gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib);
78
Jamie Madilldbfc6c62016-02-29 01:08:57 -050079 // TODO(jmadill): add VertexFormatCaps
80 BufferFactoryD3D *factory = bufferD3D->getFactory();
Jamie Madilld8fa9212016-03-02 11:51:43 -050081
82 auto errorOrElementSize = factory->getVertexSpaceRequired(attrib, 1, 0);
83 if (errorOrElementSize.isError())
84 {
85 ERR("Unlogged error in DirectStoragePossible.");
86 return false;
87 }
88
89 alignment = std::min<size_t>(errorOrElementSize.getResult(), 4);
90
91 // CPU-converted vertex data must be converted (naturally).
92 if ((factory->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_CPU) != 0)
93 {
94 return false;
95 }
Jamie Madilldbfc6c62016-02-29 01:08:57 -050096 }
97
Jamie Madilld8fa9212016-03-02 11:51:43 -050098 // Final alignment check - unaligned data must be converted.
99 return (static_cast<size_t>(ComputeVertexAttributeStride(attrib)) % alignment == 0) &&
100 (static_cast<size_t>(attrib.offset) % alignment == 0);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500101}
102} // anonymous namespace
103
Jamie Madille36b92d2016-03-04 15:46:58 -0500104TranslatedAttribute::TranslatedAttribute()
105 : active(false),
106 attribute(nullptr),
107 currentValueType(GL_NONE),
Jamie Madill53a36002016-04-08 19:03:18 +0000108 offset(0),
Jamie Madille36b92d2016-03-04 15:46:58 -0500109 stride(0),
110 vertexBuffer(),
111 storage(nullptr),
112 serial(0),
113 divisor(0)
114{
115}
116
Jamie Madille18eb972016-03-04 15:46:59 -0500117VertexStorageType ClassifyAttributeStorage(const gl::VertexAttribute &attrib)
118{
119 // If attribute is disabled, we use the current value.
120 if (!attrib.enabled)
121 {
122 return VertexStorageType::CURRENT_VALUE;
123 }
124
125 // If specified with immediate data, we must use dynamic storage.
126 auto *buffer = attrib.buffer.get();
127 if (!buffer)
128 {
129 return VertexStorageType::DYNAMIC;
130 }
131
132 // Check if the buffer supports direct storage.
133 if (DirectStoragePossible(attrib))
134 {
135 return VertexStorageType::DIRECT;
136 }
137
138 // Otherwise the storage is static or dynamic.
139 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
140 ASSERT(bufferD3D);
141 switch (bufferD3D->getUsage())
142 {
143 case D3DBufferUsage::DYNAMIC:
144 return VertexStorageType::DYNAMIC;
145 case D3DBufferUsage::STATIC:
146 return VertexStorageType::STATIC;
147 default:
148 UNREACHABLE();
149 return VertexStorageType::UNKNOWN;
150 }
151}
152
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400153VertexDataManager::CurrentValueState::CurrentValueState()
154 : buffer(nullptr),
155 offset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000156{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400157 data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
158 data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
159 data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
160 data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
161 data.Type = GL_FLOAT;
162}
daniel@transgaming.com83921382011-01-08 05:46:00 +0000163
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400164VertexDataManager::CurrentValueState::~CurrentValueState()
165{
166 SafeDelete(buffer);
167}
168
169VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
170 : mFactory(factory),
171 mStreamingBuffer(nullptr),
172 // TODO(jmadill): use context caps
173 mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS)
174{
Jamie Madillfd1bf4e2015-03-31 09:46:02 -0400175 mStreamingBuffer = new StreamingVertexBufferInterface(factory, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000176
177 if (!mStreamingBuffer)
178 {
179 ERR("Failed to allocate the streaming vertex buffer.");
180 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000181}
182
183VertexDataManager::~VertexDataManager()
184{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400185 SafeDelete(mStreamingBuffer);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000186}
187
Jamie Madill53a36002016-04-08 19:03:18 +0000188void VertexDataManager::unmapStreamingBuffer()
189{
190 mStreamingBuffer->getVertexBuffer()->hintUnmapResource();
191}
192
Jamie Madill476682e2015-06-30 10:04:29 -0400193gl::Error VertexDataManager::prepareVertexData(const gl::State &state,
194 GLint start,
195 GLsizei count,
196 std::vector<TranslatedAttribute> *translatedAttribs,
197 GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000198{
Jamie Madille18eb972016-03-04 15:46:59 -0500199 ASSERT(mStreamingBuffer);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000200
Geoff Lang5ead9272015-03-25 12:27:43 -0400201 const gl::VertexArray *vertexArray = state.getVertexArray();
Jamie Madill63805b42015-08-25 13:17:39 -0400202 const auto &vertexAttributes = vertexArray->getVertexAttributes();
Geoff Lang5ead9272015-03-25 12:27:43 -0400203
Jamie Madill53a36002016-04-08 19:03:18 +0000204 mDynamicAttributeIndexesCache.clear();
Jamie Madille18eb972016-03-04 15:46:59 -0500205 const gl::Program *program = state.getProgram();
206
Jamie Madill476682e2015-06-30 10:04:29 -0400207 translatedAttribs->clear();
Jamie Madill27c08912015-06-22 13:57:20 -0400208
Jamie Madill9c385802015-06-22 13:57:18 -0400209 for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000210 {
Jamie Madille18eb972016-03-04 15:46:59 -0500211 // Skip attrib locations the program doesn't use.
212 if (!program->isAttribLocationActive(attribIndex))
213 continue;
214
215 const auto &attrib = vertexAttributes[attribIndex];
216
217 // Resize automatically puts in empty attribs
218 translatedAttribs->resize(attribIndex + 1);
219
220 TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
221 auto currentValueData =
222 state.getVertexAttribCurrentValue(static_cast<unsigned int>(attribIndex));
223
224 // Record the attribute now
225 translated->active = true;
226 translated->attribute = &attrib;
227 translated->currentValueType = currentValueData.Type;
228 translated->divisor = attrib.divisor;
229
230 switch (ClassifyAttributeStorage(attrib))
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000231 {
Jamie Madille18eb972016-03-04 15:46:59 -0500232 case VertexStorageType::STATIC:
Jamie Madill9c385802015-06-22 13:57:18 -0400233 {
Jamie Madille18eb972016-03-04 15:46:59 -0500234 // Store static attribute.
Jamie Madill53a36002016-04-08 19:03:18 +0000235 gl::Error error = StoreStaticAttrib(translated, start, count, instances);
236 if (error.isError())
237 {
238 return error;
239 }
Jamie Madille18eb972016-03-04 15:46:59 -0500240 break;
Jamie Madill9c385802015-06-22 13:57:18 -0400241 }
Jamie Madille18eb972016-03-04 15:46:59 -0500242 case VertexStorageType::DYNAMIC:
243 // Dynamic attributes must be handled together.
Jamie Madill53a36002016-04-08 19:03:18 +0000244 mDynamicAttributeIndexesCache.push_back(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500245 break;
246 case VertexStorageType::DIRECT:
247 // Update translated data for direct attributes.
Jamie Madill53a36002016-04-08 19:03:18 +0000248 StoreDirectAttrib(translated, start);
Jamie Madille18eb972016-03-04 15:46:59 -0500249 break;
250 case VertexStorageType::CURRENT_VALUE:
Jamie Madill27c08912015-06-22 13:57:20 -0400251 {
Jamie Madill53a36002016-04-08 19:03:18 +0000252 gl::Error error = storeCurrentValue(currentValueData, translated, attribIndex);
253 if (error.isError())
254 {
255 return error;
256 }
Jamie Madille18eb972016-03-04 15:46:59 -0500257 break;
Jamie Madill27c08912015-06-22 13:57:20 -0400258 }
Jamie Madille18eb972016-03-04 15:46:59 -0500259 default:
260 UNREACHABLE();
261 break;
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000262 }
263 }
264
Jamie Madill53a36002016-04-08 19:03:18 +0000265 if (mDynamicAttributeIndexesCache.empty())
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000266 {
Jamie Madill53a36002016-04-08 19:03:18 +0000267 gl::Error(GL_NO_ERROR);
Jamie Madille18eb972016-03-04 15:46:59 -0500268 }
269
Jamie Madill53a36002016-04-08 19:03:18 +0000270 return storeDynamicAttribs(translatedAttribs, mDynamicAttributeIndexesCache, start, count,
271 instances);
Jamie Madille18eb972016-03-04 15:46:59 -0500272}
273
274// static
Jamie Madill53a36002016-04-08 19:03:18 +0000275void VertexDataManager::StoreDirectAttrib(TranslatedAttribute *directAttrib, GLint start)
Jamie Madille18eb972016-03-04 15:46:59 -0500276{
277 const auto &attrib = *directAttrib->attribute;
278 gl::Buffer *buffer = attrib.buffer.get();
279 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
280
Jamie Madill53a36002016-04-08 19:03:18 +0000281 // Instanced vertices do not apply the 'start' offset
282 GLint firstVertexIndex = (attrib.divisor > 0 ? 0 : start);
283
Jamie Madille18eb972016-03-04 15:46:59 -0500284 ASSERT(DirectStoragePossible(attrib));
285 directAttrib->vertexBuffer.set(nullptr);
286 directAttrib->storage = bufferD3D;
287 directAttrib->serial = bufferD3D->getSerial();
288 directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib));
Jamie Madill53a36002016-04-08 19:03:18 +0000289 directAttrib->offset =
290 static_cast<unsigned int>(attrib.offset + directAttrib->stride * firstVertexIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500291}
292
293// static
294gl::Error VertexDataManager::StoreStaticAttrib(TranslatedAttribute *translated,
Jamie Madill53a36002016-04-08 19:03:18 +0000295 GLint start,
Jamie Madille18eb972016-03-04 15:46:59 -0500296 GLsizei count,
297 GLsizei instances)
298{
299 const gl::VertexAttribute &attrib = *translated->attribute;
300
301 gl::Buffer *buffer = attrib.buffer.get();
302 ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib));
303 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
304
Jamie Madill53a36002016-04-08 19:03:18 +0000305 // Instanced vertices do not apply the 'start' offset
306 GLint firstVertexIndex = (attrib.divisor > 0 ? 0 : start);
307
Jamie Madille18eb972016-03-04 15:46:59 -0500308 // Compute source data pointer
309 const uint8_t *sourceData = nullptr;
310
Jamie Madill53a36002016-04-08 19:03:18 +0000311 gl::Error error = bufferD3D->getData(&sourceData);
312 if (error.isError())
313 {
314 return error;
315 }
Jamie Madille18eb972016-03-04 15:46:59 -0500316 sourceData += static_cast<int>(attrib.offset);
317
318 unsigned int streamOffset = 0;
319
Jamie Madill53a36002016-04-08 19:03:18 +0000320 auto errorOrOutputElementSize = bufferD3D->getFactory()->getVertexSpaceRequired(attrib, 1, 0);
321 if (errorOrOutputElementSize.isError())
322 {
323 return errorOrOutputElementSize.getError();
324 }
325
Jamie Madille18eb972016-03-04 15:46:59 -0500326 translated->storage = nullptr;
Jamie Madill53a36002016-04-08 19:03:18 +0000327 translated->stride = errorOrOutputElementSize.getResult();
Jamie Madille18eb972016-03-04 15:46:59 -0500328
329 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib);
330 ASSERT(staticBuffer);
331
332 if (staticBuffer->empty())
333 {
334 // Convert the entire buffer
335 int totalCount = ElementsInBuffer(attrib, static_cast<unsigned int>(bufferD3D->getSize()));
336 int startIndex = static_cast<int>(attrib.offset) /
337 static_cast<int>(ComputeVertexAttributeStride(attrib));
338
Jamie Madill53a36002016-04-08 19:03:18 +0000339 error = staticBuffer->storeStaticAttribute(attrib, -startIndex, totalCount, 0, sourceData);
340 if (error.isError())
341 {
342 return error;
343 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000344 }
345
Jamie Madille18eb972016-03-04 15:46:59 -0500346 unsigned int firstElementOffset =
347 (static_cast<unsigned int>(attrib.offset) /
348 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib))) *
349 translated->stride;
Jamie Madill53a36002016-04-08 19:03:18 +0000350 ASSERT(attrib.divisor == 0 || firstVertexIndex == 0);
351 unsigned int startOffset = firstVertexIndex * translated->stride;
352 if (streamOffset + firstElementOffset + startOffset < streamOffset)
353 {
354 return gl::Error(GL_OUT_OF_MEMORY);
355 }
Jamie Madill27c08912015-06-22 13:57:20 -0400356
Jamie Madille18eb972016-03-04 15:46:59 -0500357 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
358
359 translated->vertexBuffer.set(vertexBuffer);
360 translated->serial = vertexBuffer->getSerial();
Jamie Madill53a36002016-04-08 19:03:18 +0000361 translated->offset = streamOffset + firstElementOffset + startOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500362
Jamie Madill53a36002016-04-08 19:03:18 +0000363 return gl::Error(GL_NO_ERROR);
Jamie Madille18eb972016-03-04 15:46:59 -0500364}
365
366gl::Error VertexDataManager::storeDynamicAttribs(
367 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill53a36002016-04-08 19:03:18 +0000368 const std::vector<size_t> &dynamicAttribIndexes,
Jamie Madille18eb972016-03-04 15:46:59 -0500369 GLint start,
370 GLsizei count,
371 GLsizei instances)
372{
373 // Reserve the required space for the dynamic buffers.
Jamie Madill53a36002016-04-08 19:03:18 +0000374 for (size_t attribIndex : dynamicAttribIndexes)
Jamie Madille18eb972016-03-04 15:46:59 -0500375 {
376 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Jamie Madill53a36002016-04-08 19:03:18 +0000377 gl::Error error = reserveSpaceForAttrib(dynamicAttrib, count, instances);
378 if (error.isError())
379 {
380 return error;
381 }
Jamie Madille18eb972016-03-04 15:46:59 -0500382 }
383
384 // Store dynamic attributes
Jamie Madill53a36002016-04-08 19:03:18 +0000385 for (size_t attribIndex : dynamicAttribIndexes)
Jamie Madille18eb972016-03-04 15:46:59 -0500386 {
387 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill53a36002016-04-08 19:03:18 +0000388 gl::Error error = storeDynamicAttrib(dynamicAttrib, start, count, instances);
389 if (error.isError())
390 {
391 unmapStreamingBuffer();
392 return error;
393 }
Shannon Woods1a965482014-09-22 18:00:32 -0400394
Jamie Madill53a36002016-04-08 19:03:18 +0000395 // Promote static usage of dynamic buffers.
396 gl::Buffer *buffer = dynamicAttrib->attribute->buffer.get();
Jamie Madill27c08912015-06-22 13:57:20 -0400397 if (buffer)
398 {
399 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jamie Madill53a36002016-04-08 19:03:18 +0000400 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib->attribute);
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700401 bufferD3D->promoteStaticUsage(count * static_cast<int>(typeSize));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000402 }
403 }
Jamie Madill53a36002016-04-08 19:03:18 +0000404
405 unmapStreamingBuffer();
406 return gl::Error(GL_NO_ERROR);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000407}
408
Jamie Madill3d72cc72015-06-22 13:57:19 -0400409gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
Geoff Langf7100b92014-09-08 16:17:08 -0400410 GLsizei count,
411 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400412{
Jamie Madill3d72cc72015-06-22 13:57:19 -0400413 const gl::VertexAttribute &attrib = *translatedAttrib.attribute;
Jamie Madille18eb972016-03-04 15:46:59 -0500414 ASSERT(!DirectStoragePossible(attrib));
Jamie Madill6d113802014-08-25 15:47:52 -0400415
Jamie Madille18eb972016-03-04 15:46:59 -0500416 gl::Buffer *buffer = attrib.buffer.get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500417 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madille18eb972016-03-04 15:46:59 -0500418 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib) == nullptr);
419 UNUSED_ASSERTION_VARIABLE(bufferD3D);
Jamie Madille36b92d2016-03-04 15:46:58 -0500420
421 size_t totalCount = ComputeVertexAttributeElementCount(attrib, count, instances);
422 ASSERT(!bufferD3D ||
423 ElementsInBuffer(attrib, static_cast<unsigned int>(bufferD3D->getSize())) >=
424 static_cast<int>(totalCount));
425
426 return mStreamingBuffer->reserveVertexSpace(attrib, static_cast<GLsizei>(totalCount),
427 instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400428}
429
Jamie Madille18eb972016-03-04 15:46:59 -0500430gl::Error VertexDataManager::storeDynamicAttrib(TranslatedAttribute *translated,
431 GLint start,
432 GLsizei count,
433 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400434{
Jamie Madill9c385802015-06-22 13:57:18 -0400435 const gl::VertexAttribute &attrib = *translated->attribute;
436
Jamie Madillf41522b2014-08-18 16:39:49 -0400437 gl::Buffer *buffer = attrib.buffer.get();
Jamie Madill2b976812014-08-25 15:47:49 -0400438 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400439 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400440
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500441 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400442
Jamie Madilld4b55a02015-01-09 14:21:49 -0500443 // Instanced vertices do not apply the 'start' offset
Jamie Madilla0537332015-11-19 13:55:26 -0500444 GLint firstVertexIndex = (attrib.divisor > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500445
Jamie Madill7d112bb2015-06-22 13:57:19 -0400446 // Compute source data pointer
447 const uint8_t *sourceData = nullptr;
448
449 if (buffer)
450 {
Jamie Madill53a36002016-04-08 19:03:18 +0000451 gl::Error error = storage->getData(&sourceData);
452 if (error.isError())
453 {
454 return error;
455 }
Jamie Madill7d112bb2015-06-22 13:57:19 -0400456 sourceData += static_cast<int>(attrib.offset);
457 }
458 else
459 {
460 sourceData = static_cast<const uint8_t*>(attrib.pointer);
461 }
462
463 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500464
Jamie Madill53a36002016-04-08 19:03:18 +0000465 auto errorOrOutputElementSize = mFactory->getVertexSpaceRequired(attrib, 1, 0);
466 if (errorOrOutputElementSize.isError())
467 {
468 return errorOrOutputElementSize.getError();
469 }
470
Jamie Madilld8fa9212016-03-02 11:51:43 -0500471 translated->storage = nullptr;
Jamie Madill53a36002016-04-08 19:03:18 +0000472 translated->stride = errorOrOutputElementSize.getResult();
Jamie Madilld8fa9212016-03-02 11:51:43 -0500473
Jamie Madille18eb972016-03-04 15:46:59 -0500474 size_t totalCount = ComputeVertexAttributeElementCount(attrib, count, instances);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400475
Jamie Madill53a36002016-04-08 19:03:18 +0000476 gl::Error error = mStreamingBuffer->storeDynamicAttribute(
Jamie Madille18eb972016-03-04 15:46:59 -0500477 attrib, translated->currentValueType, firstVertexIndex, static_cast<GLsizei>(totalCount),
Jamie Madill53a36002016-04-08 19:03:18 +0000478 instances, &streamOffset, sourceData);
479 if (error.isError())
480 {
481 return error;
482 }
Jamie Madillf41522b2014-08-18 16:39:49 -0400483
Jamie Madille18eb972016-03-04 15:46:59 -0500484 VertexBuffer *vertexBuffer = mStreamingBuffer->getVertexBuffer();
485
486 translated->vertexBuffer.set(vertexBuffer);
487 translated->serial = vertexBuffer->getSerial();
Jamie Madill53a36002016-04-08 19:03:18 +0000488 translated->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400489
Jamie Madill53a36002016-04-08 19:03:18 +0000490 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400491}
492
Jamie Madill9c385802015-06-22 13:57:18 -0400493gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400494 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500495 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400496{
Jamie Madille18eb972016-03-04 15:46:59 -0500497 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
498 auto *&buffer = cachedState->buffer;
499
500 if (!buffer)
501 {
502 buffer = new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE);
503 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500504
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400505 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400506 {
Jamie Madill9c385802015-06-22 13:57:18 -0400507 const gl::VertexAttribute &attrib = *translated->attribute;
Jamie Madill27c08912015-06-22 13:57:20 -0400508
Jamie Madill53a36002016-04-08 19:03:18 +0000509 gl::Error error = buffer->reserveVertexSpace(attrib, 1, 0);
510 if (error.isError())
511 {
512 return error;
513 }
Jamie Madillf41522b2014-08-18 16:39:49 -0400514
Jamie Madill7d112bb2015-06-22 13:57:19 -0400515 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400516 unsigned int streamOffset;
Jamie Madill53a36002016-04-08 19:03:18 +0000517 error = buffer->storeDynamicAttribute(attrib, currentValue.Type, 0, 1, 0, &streamOffset,
518 sourceData);
519 if (error.isError())
520 {
521 return error;
522 }
Jamie Madillf41522b2014-08-18 16:39:49 -0400523
Jamie Madille36b92d2016-03-04 15:46:58 -0500524 buffer->getVertexBuffer()->hintUnmapResource();
525
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400526 cachedState->data = currentValue;
527 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400528 }
529
Jamie Madille36b92d2016-03-04 15:46:58 -0500530 translated->vertexBuffer.set(buffer->getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400531
Jamie Madille36b92d2016-03-04 15:46:58 -0500532 translated->storage = nullptr;
533 translated->serial = buffer->getSerial();
534 translated->divisor = 0;
535 translated->stride = 0;
Jamie Madill53a36002016-04-08 19:03:18 +0000536 translated->offset = static_cast<unsigned int>(cachedState->offset);
Jamie Madillf41522b2014-08-18 16:39:49 -0400537
Jamie Madill53a36002016-04-08 19:03:18 +0000538 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400539}
540
Jamie Madille36b92d2016-03-04 15:46:58 -0500541// VertexBufferBinding implementation
542VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
543{
544}
545
546VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
547 : mBoundVertexBuffer(other.mBoundVertexBuffer)
548{
549 if (mBoundVertexBuffer)
550 {
551 mBoundVertexBuffer->addRef();
552 }
553}
554
555VertexBufferBinding::~VertexBufferBinding()
556{
557 if (mBoundVertexBuffer)
558 {
559 mBoundVertexBuffer->release();
560 }
561}
562
563VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
564{
565 mBoundVertexBuffer = other.mBoundVertexBuffer;
566 if (mBoundVertexBuffer)
567 {
568 mBoundVertexBuffer->addRef();
569 }
570 return *this;
571}
572
573void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
574{
575 if (mBoundVertexBuffer == vertexBuffer)
576 return;
577
578 if (mBoundVertexBuffer)
579 {
580 mBoundVertexBuffer->release();
581 }
582 if (vertexBuffer)
583 {
584 vertexBuffer->addRef();
585 }
586
587 mBoundVertexBuffer = vertexBuffer;
588}
589
590VertexBuffer *VertexBufferBinding::get() const
591{
592 return mBoundVertexBuffer;
593}
594
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500595} // namespace rx