blob: d3eab8bc9a3d9e0e610015fd6b896c7224643a63 [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"
11#include "libANGLE/renderer/d3d/BufferD3D.h"
12#include "libANGLE/renderer/d3d/VertexBuffer.h"
13#include "libANGLE/renderer/Renderer.h"
14#include "libANGLE/Buffer.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050015#include "libANGLE/Program.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/VertexAttribute.h"
17#include "libANGLE/State.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000018
19namespace
20{
21 enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000022 // This has to be at least 4k or else it fails on ATI cards.
23 enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 };
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000024}
25
daniel@transgaming.com31240482012-11-28 21:06:41 +000026namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000027{
28
Brandon Jones5bf98292014-06-06 17:19:38 -070029static int ElementsInBuffer(const gl::VertexAttribute &attrib, unsigned int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000030{
Geoff Langa36ead42013-08-02 11:54:08 -040031 // Size cannot be larger than a GLsizei
32 if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
33 {
34 size = static_cast<unsigned int>(std::numeric_limits<int>::max());
35 }
36
Brandon Jones5bf98292014-06-06 17:19:38 -070037 GLsizei stride = ComputeVertexAttributeStride(attrib);
38 return (size - attrib.offset % stride + (stride - ComputeVertexAttributeTypeSize(attrib))) / stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000039}
40
Brandon Jones5bf98292014-06-06 17:19:38 -070041static int StreamingBufferElementCount(const gl::VertexAttribute &attrib, int vertexDrawCount, int instanceDrawCount)
Jamie Madill9b4f3842013-08-26 15:29:30 -040042{
43 // For instanced rendering, we draw "instanceDrawCount" sets of "vertexDrawCount" vertices.
44 //
45 // A vertex attribute with a positive divisor loads one instanced vertex for every set of
46 // non-instanced vertices, and the instanced vertex index advances once every "mDivisor" instances.
Brandon Jones5bf98292014-06-06 17:19:38 -070047 if (instanceDrawCount > 0 && attrib.divisor > 0)
Jamie Madill9b4f3842013-08-26 15:29:30 -040048 {
Brandon Jones5bf98292014-06-06 17:19:38 -070049 return instanceDrawCount / attrib.divisor;
Jamie Madill9b4f3842013-08-26 15:29:30 -040050 }
51
52 return vertexDrawCount;
53}
54
Jamie Madill93e13fb2014-11-06 15:27:25 -050055VertexDataManager::VertexDataManager(RendererD3D *renderer) : mRenderer(renderer)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000056{
daniel@transgaming.com31240482012-11-28 21:06:41 +000057 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000058 {
shannon.woods%transgaming.com@gtempaccount.com3026dc72013-04-13 03:37:27 +000059 mCurrentValue[i].FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
60 mCurrentValue[i].FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
61 mCurrentValue[i].FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
62 mCurrentValue[i].FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
63 mCurrentValue[i].Type = GL_FLOAT;
daniel@transgaming.com83921382011-01-08 05:46:00 +000064 mCurrentValueBuffer[i] = NULL;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000065 mCurrentValueOffsets[i] = 0;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000066 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000067
daniel@transgaming.come4e45062012-12-20 20:56:53 +000068 mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000069
70 if (!mStreamingBuffer)
71 {
72 ERR("Failed to allocate the streaming vertex buffer.");
73 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000074}
75
76VertexDataManager::~VertexDataManager()
77{
daniel@transgaming.com83921382011-01-08 05:46:00 +000078 delete mStreamingBuffer;
79
daniel@transgaming.com31240482012-11-28 21:06:41 +000080 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +000081 {
82 delete mCurrentValueBuffer[i];
83 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000084}
85
Shannon Woods1a965482014-09-22 18:00:32 -040086gl::Error VertexDataManager::prepareVertexData(const gl::State &state, GLint start, GLsizei count,
87 TranslatedAttribute *translated, GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +000088{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000089 if (!mStreamingBuffer)
90 {
Geoff Langf7100b92014-09-08 16:17:08 -040091 return gl::Error(GL_OUT_OF_MEMORY, "Internal streaming vertex buffer is unexpectedly NULL.");
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000092 }
93
Jamie Madill6d113802014-08-25 15:47:52 -040094 // Invalidate static buffers that don't contain matching attributes
daniel@transgaming.com31240482012-11-28 21:06:41 +000095 for (int attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000096 {
Geoff Lang7dd2e102014-11-10 15:19:26 -050097 translated[attributeIndex].active = (state.getProgram()->getSemanticIndex(attributeIndex) != -1);
Shannon Woods1a965482014-09-22 18:00:32 -040098 const gl::VertexAttribute &curAttrib = state.getVertexAttribState(attributeIndex);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000099
Shannon Woods1a965482014-09-22 18:00:32 -0400100 if (translated[attributeIndex].active && curAttrib.enabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000101 {
Shannon Woods1a965482014-09-22 18:00:32 -0400102 invalidateMatchingStaticData(curAttrib, state.getVertexAttribCurrentValue(attributeIndex));
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000103 }
104 }
105
106 // Reserve the required space in the buffers
107 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
108 {
Shannon Woods1a965482014-09-22 18:00:32 -0400109 const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i);
110 if (translated[i].active && curAttrib.enabled)
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000111 {
Shannon Woods1a965482014-09-22 18:00:32 -0400112 gl::Error error = reserveSpaceForAttrib(curAttrib, state.getVertexAttribCurrentValue(i), count, instances);
Geoff Langf7100b92014-09-08 16:17:08 -0400113 if (error.isError())
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000114 {
Geoff Langf7100b92014-09-08 16:17:08 -0400115 return error;
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000116 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000117 }
118 }
119
120 // Perform the vertex data translations
daniel@transgaming.com31240482012-11-28 21:06:41 +0000121 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000122 {
Shannon Woods1a965482014-09-22 18:00:32 -0400123 const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000124 if (translated[i].active)
125 {
Shannon Woods1a965482014-09-22 18:00:32 -0400126 if (curAttrib.enabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000127 {
Shannon Woods1a965482014-09-22 18:00:32 -0400128 gl::Error error = storeAttribute(curAttrib, state.getVertexAttribCurrentValue(i),
129 &translated[i], start, count, instances);
130
Geoff Langf7100b92014-09-08 16:17:08 -0400131 if (error.isError())
132 {
133 return error;
134 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000135 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000136 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000137 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000138 if (!mCurrentValueBuffer[i])
139 {
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000140 mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE);
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000141 }
142
Shannon Woods1a965482014-09-22 18:00:32 -0400143 gl::Error error = storeCurrentValue(curAttrib, state.getVertexAttribCurrentValue(i), &translated[i],
Geoff Langf7100b92014-09-08 16:17:08 -0400144 &mCurrentValue[i], &mCurrentValueOffsets[i],
145 mCurrentValueBuffer[i]);
146 if (error.isError())
147 {
148 return error;
149 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000150 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000151 }
152 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000153
daniel@transgaming.com31240482012-11-28 21:06:41 +0000154 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000155 {
Shannon Woods1a965482014-09-22 18:00:32 -0400156 const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i);
157 if (translated[i].active && curAttrib.enabled)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000158 {
Shannon Woods1a965482014-09-22 18:00:32 -0400159 gl::Buffer *buffer = curAttrib.buffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000160
161 if (buffer)
162 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700163 BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation());
Shannon Woods1a965482014-09-22 18:00:32 -0400164 bufferImpl->promoteStaticUsage(count * ComputeVertexAttributeTypeSize(curAttrib));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000165 }
166 }
167 }
168
Geoff Langf7100b92014-09-08 16:17:08 -0400169 return gl::Error(GL_NO_ERROR);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000170}
171
Jamie Madill6d113802014-08-25 15:47:52 -0400172void VertexDataManager::invalidateMatchingStaticData(const gl::VertexAttribute &attrib,
173 const gl::VertexAttribCurrentValueData &currentValue) const
174{
175 gl::Buffer *buffer = attrib.buffer.get();
176
177 if (buffer)
178 {
179 BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation());
180 StaticVertexBufferInterface *staticBuffer = bufferImpl->getStaticVertexBuffer();
181
182 if (staticBuffer &&
183 staticBuffer->getBufferSize() > 0 &&
184 !staticBuffer->lookupAttribute(attrib, NULL) &&
185 !staticBuffer->directStoragePossible(attrib, currentValue))
186 {
187 bufferImpl->invalidateStaticData();
188 }
189 }
190}
191
Geoff Langf7100b92014-09-08 16:17:08 -0400192gl::Error VertexDataManager::reserveSpaceForAttrib(const gl::VertexAttribute &attrib,
193 const gl::VertexAttribCurrentValueData &currentValue,
194 GLsizei count,
195 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400196{
197 gl::Buffer *buffer = attrib.buffer.get();
198 BufferD3D *bufferImpl = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL;
199 StaticVertexBufferInterface *staticBuffer = bufferImpl ? bufferImpl->getStaticVertexBuffer() : NULL;
200 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
201
202 if (!vertexBuffer->directStoragePossible(attrib, currentValue))
203 {
204 if (staticBuffer)
205 {
206 if (staticBuffer->getBufferSize() == 0)
207 {
208 int totalCount = ElementsInBuffer(attrib, bufferImpl->getSize());
Geoff Langf7100b92014-09-08 16:17:08 -0400209 gl::Error error = staticBuffer->reserveVertexSpace(attrib, totalCount, 0);
210 if (error.isError())
Jamie Madill6d113802014-08-25 15:47:52 -0400211 {
Geoff Langf7100b92014-09-08 16:17:08 -0400212 return error;
Jamie Madill6d113802014-08-25 15:47:52 -0400213 }
214 }
215 }
216 else
217 {
218 int totalCount = StreamingBufferElementCount(attrib, count, instances);
219 ASSERT(!bufferImpl || ElementsInBuffer(attrib, bufferImpl->getSize()) >= totalCount);
220
Geoff Langf7100b92014-09-08 16:17:08 -0400221 gl::Error error = mStreamingBuffer->reserveVertexSpace(attrib, totalCount, instances);
222 if (error.isError())
Jamie Madill6d113802014-08-25 15:47:52 -0400223 {
Geoff Langf7100b92014-09-08 16:17:08 -0400224 return error;
Jamie Madill6d113802014-08-25 15:47:52 -0400225 }
226 }
227 }
228
Geoff Langf7100b92014-09-08 16:17:08 -0400229 return gl::Error(GL_NO_ERROR);
Jamie Madill6d113802014-08-25 15:47:52 -0400230}
231
Geoff Langf7100b92014-09-08 16:17:08 -0400232gl::Error VertexDataManager::storeAttribute(const gl::VertexAttribute &attrib,
233 const gl::VertexAttribCurrentValueData &currentValue,
234 TranslatedAttribute *translated,
235 GLint start,
236 GLsizei count,
237 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400238{
239 gl::Buffer *buffer = attrib.buffer.get();
Jamie Madill2b976812014-08-25 15:47:49 -0400240 ASSERT(buffer || attrib.pointer);
Jamie Madillf41522b2014-08-18 16:39:49 -0400241
242 BufferD3D *storage = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL;
243 StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : NULL;
244 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
245 bool directStorage = vertexBuffer->directStoragePossible(attrib, currentValue);
246
247 unsigned int streamOffset = 0;
248 unsigned int outputElementSize = 0;
249
Jamie Madilld4b55a02015-01-09 14:21:49 -0500250 // Instanced vertices do not apply the 'start' offset
251 GLint firstVertexIndex = (instances > 0 && attrib.divisor > 0 ? 0 : start);
252
Jamie Madillf41522b2014-08-18 16:39:49 -0400253 if (directStorage)
254 {
255 outputElementSize = ComputeVertexAttributeStride(attrib);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500256 streamOffset = attrib.offset + outputElementSize * firstVertexIndex;
Jamie Madillf41522b2014-08-18 16:39:49 -0400257 }
258 else if (staticBuffer)
259 {
Geoff Langf7100b92014-09-08 16:17:08 -0400260 gl::Error error = staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
261 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400262 {
Geoff Langf7100b92014-09-08 16:17:08 -0400263 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400264 }
265
266 if (!staticBuffer->lookupAttribute(attrib, &streamOffset))
267 {
268 // Convert the entire buffer
269 int totalCount = ElementsInBuffer(attrib, storage->getSize());
270 int startIndex = attrib.offset / ComputeVertexAttributeStride(attrib);
271
Geoff Langf7100b92014-09-08 16:17:08 -0400272 gl::Error error = staticBuffer->storeVertexAttributes(attrib, currentValue, -startIndex, totalCount,
273 0, &streamOffset);
274 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400275 {
Geoff Langf7100b92014-09-08 16:17:08 -0400276 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400277 }
278 }
279
280 unsigned int firstElementOffset = (attrib.offset / ComputeVertexAttributeStride(attrib)) * outputElementSize;
Jamie Madilld4b55a02015-01-09 14:21:49 -0500281 unsigned int startOffset = (instances == 0 || attrib.divisor == 0) ? firstVertexIndex * outputElementSize : 0;
Jamie Madillf41522b2014-08-18 16:39:49 -0400282 if (streamOffset + firstElementOffset + startOffset < streamOffset)
283 {
Geoff Langf7100b92014-09-08 16:17:08 -0400284 return gl::Error(GL_OUT_OF_MEMORY);
Jamie Madillf41522b2014-08-18 16:39:49 -0400285 }
286
287 streamOffset += firstElementOffset + startOffset;
288 }
289 else
290 {
291 int totalCount = StreamingBufferElementCount(attrib, count, instances);
Geoff Langf7100b92014-09-08 16:17:08 -0400292 gl::Error error = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
293 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400294 {
Geoff Langf7100b92014-09-08 16:17:08 -0400295 return error;
296 }
297
Jamie Madilld4b55a02015-01-09 14:21:49 -0500298 error = mStreamingBuffer->storeVertexAttributes(attrib, currentValue, firstVertexIndex,
299 totalCount, instances, &streamOffset);
Geoff Langf7100b92014-09-08 16:17:08 -0400300 if (error.isError())
301 {
302 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400303 }
304 }
305
306 translated->storage = directStorage ? storage : NULL;
307 translated->vertexBuffer = vertexBuffer->getVertexBuffer();
308 translated->serial = directStorage ? storage->getSerial() : vertexBuffer->getSerial();
309 translated->divisor = attrib.divisor;
310
311 translated->attribute = &attrib;
312 translated->currentValueType = currentValue.Type;
313 translated->stride = outputElementSize;
314 translated->offset = streamOffset;
315
Geoff Langf7100b92014-09-08 16:17:08 -0400316 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400317}
318
Geoff Langf7100b92014-09-08 16:17:08 -0400319gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribute &attrib,
320 const gl::VertexAttribCurrentValueData &currentValue,
321 TranslatedAttribute *translated,
322 gl::VertexAttribCurrentValueData *cachedValue,
323 size_t *cachedOffset,
324 StreamingVertexBufferInterface *buffer)
Jamie Madillf41522b2014-08-18 16:39:49 -0400325{
326 if (*cachedValue != currentValue)
327 {
Geoff Langf7100b92014-09-08 16:17:08 -0400328 gl::Error error = buffer->reserveVertexSpace(attrib, 1, 0);
329 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400330 {
Geoff Langf7100b92014-09-08 16:17:08 -0400331 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400332 }
333
334 unsigned int streamOffset;
Geoff Langf7100b92014-09-08 16:17:08 -0400335 error = buffer->storeVertexAttributes(attrib, currentValue, 0, 1, 0, &streamOffset);
336 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400337 {
Geoff Langf7100b92014-09-08 16:17:08 -0400338 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400339 }
340
341 *cachedValue = currentValue;
342 *cachedOffset = streamOffset;
343 }
344
345 translated->storage = NULL;
346 translated->vertexBuffer = buffer->getVertexBuffer();
347 translated->serial = buffer->getSerial();
348 translated->divisor = 0;
349
350 translated->attribute = &attrib;
351 translated->currentValueType = currentValue.Type;
352 translated->stride = 0;
353 translated->offset = *cachedOffset;
354
Geoff Langf7100b92014-09-08 16:17:08 -0400355 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400356}
357
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000358}