blob: a69bc6797da589390ca8445e0204edd2ac8632c3 [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
Brandon Jonesc7a41042014-06-23 12:03:25 -070010#include "libGLESv2/renderer/d3d/VertexDataManager.h"
Brandon Jonesd38f9262014-06-18 16:26:45 -070011#include "libGLESv2/renderer/d3d/BufferD3D.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040012#include "libGLESv2/renderer/d3d/VertexBuffer.h"
13#include "libGLESv2/renderer/Renderer.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000014#include "libGLESv2/Buffer.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000015#include "libGLESv2/ProgramBinary.h"
Jamie Madill87939712013-07-02 11:57:01 -040016#include "libGLESv2/VertexAttribute.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000017
18namespace
19{
20 enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000021 // This has to be at least 4k or else it fails on ATI cards.
22 enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 };
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000023}
24
daniel@transgaming.com31240482012-11-28 21:06:41 +000025namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000026{
27
Brandon Jones5bf98292014-06-06 17:19:38 -070028static int ElementsInBuffer(const gl::VertexAttribute &attrib, unsigned int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000029{
Geoff Langa36ead42013-08-02 11:54:08 -040030 // Size cannot be larger than a GLsizei
31 if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
32 {
33 size = static_cast<unsigned int>(std::numeric_limits<int>::max());
34 }
35
Brandon Jones5bf98292014-06-06 17:19:38 -070036 GLsizei stride = ComputeVertexAttributeStride(attrib);
37 return (size - attrib.offset % stride + (stride - ComputeVertexAttributeTypeSize(attrib))) / stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000038}
39
Brandon Jones5bf98292014-06-06 17:19:38 -070040static int StreamingBufferElementCount(const gl::VertexAttribute &attrib, int vertexDrawCount, int instanceDrawCount)
Jamie Madill9b4f3842013-08-26 15:29:30 -040041{
42 // For instanced rendering, we draw "instanceDrawCount" sets of "vertexDrawCount" vertices.
43 //
44 // A vertex attribute with a positive divisor loads one instanced vertex for every set of
45 // non-instanced vertices, and the instanced vertex index advances once every "mDivisor" instances.
Brandon Jones5bf98292014-06-06 17:19:38 -070046 if (instanceDrawCount > 0 && attrib.divisor > 0)
Jamie Madill9b4f3842013-08-26 15:29:30 -040047 {
Brandon Jones5bf98292014-06-06 17:19:38 -070048 return instanceDrawCount / attrib.divisor;
Jamie Madill9b4f3842013-08-26 15:29:30 -040049 }
50
51 return vertexDrawCount;
52}
53
daniel@transgaming.com4150d362012-12-20 21:07:43 +000054VertexDataManager::VertexDataManager(Renderer *renderer) : mRenderer(renderer)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000055{
daniel@transgaming.com31240482012-11-28 21:06:41 +000056 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000057 {
shannon.woods%transgaming.com@gtempaccount.com3026dc72013-04-13 03:37:27 +000058 mCurrentValue[i].FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
59 mCurrentValue[i].FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
60 mCurrentValue[i].FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
61 mCurrentValue[i].FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
62 mCurrentValue[i].Type = GL_FLOAT;
daniel@transgaming.com83921382011-01-08 05:46:00 +000063 mCurrentValueBuffer[i] = NULL;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000064 mCurrentValueOffsets[i] = 0;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000065 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000066
daniel@transgaming.come4e45062012-12-20 20:56:53 +000067 mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000068
69 if (!mStreamingBuffer)
70 {
71 ERR("Failed to allocate the streaming vertex buffer.");
72 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000073}
74
75VertexDataManager::~VertexDataManager()
76{
daniel@transgaming.com83921382011-01-08 05:46:00 +000077 delete mStreamingBuffer;
78
daniel@transgaming.com31240482012-11-28 21:06:41 +000079 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +000080 {
81 delete mCurrentValueBuffer[i];
82 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000083}
84
Jamie Madilla857c362013-07-02 11:57:02 -040085GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], const gl::VertexAttribCurrentValueData currentValues[],
86 gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +000087{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000088 if (!mStreamingBuffer)
89 {
90 return GL_OUT_OF_MEMORY;
91 }
92
Jamie Madill6d113802014-08-25 15:47:52 -040093 // Invalidate static buffers that don't contain matching attributes
daniel@transgaming.com31240482012-11-28 21:06:41 +000094 for (int attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000095 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +000096 translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000097
Jamie Madill6d113802014-08-25 15:47:52 -040098 if (translated[attributeIndex].active && attribs[attributeIndex].enabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +000099 {
Jamie Madill6d113802014-08-25 15:47:52 -0400100 invalidateMatchingStaticData(attribs[attributeIndex], currentValues[attributeIndex]);
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000101 }
102 }
103
104 // Reserve the required space in the buffers
105 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
106 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700107 if (translated[i].active && attribs[i].enabled)
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000108 {
Jamie Madill6d113802014-08-25 15:47:52 -0400109 if (!reserveSpaceForAttrib(attribs[i], currentValues[i], count, instances))
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000110 {
Jamie Madill6d113802014-08-25 15:47:52 -0400111 return GL_OUT_OF_MEMORY;
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000112 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000113 }
114 }
115
116 // Perform the vertex data translations
daniel@transgaming.com31240482012-11-28 21:06:41 +0000117 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000118 {
119 if (translated[i].active)
120 {
Jamie Madillf41522b2014-08-18 16:39:49 -0400121 GLenum result;
122
Brandon Jones5bf98292014-06-06 17:19:38 -0700123 if (attribs[i].enabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000124 {
Jamie Madillf41522b2014-08-18 16:39:49 -0400125 result = storeAttribute(attribs[i], currentValues[i], &translated[i],
126 start, count, instances);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000127 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000128 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000129 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000130 if (!mCurrentValueBuffer[i])
131 {
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000132 mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE);
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000133 }
134
Jamie Madillf41522b2014-08-18 16:39:49 -0400135 result = storeCurrentValue(attribs[i], currentValues[i], &translated[i],
136 &mCurrentValue[i], &mCurrentValueOffsets[i],
137 mCurrentValueBuffer[i]);
138 }
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000139
Jamie Madillf41522b2014-08-18 16:39:49 -0400140 if (result != GL_NO_ERROR)
141 {
142 return result;
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000143 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000144 }
145 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000146
daniel@transgaming.com31240482012-11-28 21:06:41 +0000147 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000148 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700149 if (translated[i].active && attribs[i].enabled)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000150 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700151 gl::Buffer *buffer = attribs[i].buffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000152
153 if (buffer)
154 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700155 BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation());
156 bufferImpl->promoteStaticUsage(count * ComputeVertexAttributeTypeSize(attribs[i]));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000157 }
158 }
159 }
160
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000161 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000162}
163
Jamie Madill6d113802014-08-25 15:47:52 -0400164void VertexDataManager::invalidateMatchingStaticData(const gl::VertexAttribute &attrib,
165 const gl::VertexAttribCurrentValueData &currentValue) const
166{
167 gl::Buffer *buffer = attrib.buffer.get();
168
169 if (buffer)
170 {
171 BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation());
172 StaticVertexBufferInterface *staticBuffer = bufferImpl->getStaticVertexBuffer();
173
174 if (staticBuffer &&
175 staticBuffer->getBufferSize() > 0 &&
176 !staticBuffer->lookupAttribute(attrib, NULL) &&
177 !staticBuffer->directStoragePossible(attrib, currentValue))
178 {
179 bufferImpl->invalidateStaticData();
180 }
181 }
182}
183
184bool VertexDataManager::reserveSpaceForAttrib(const gl::VertexAttribute &attrib,
185 const gl::VertexAttribCurrentValueData &currentValue,
186 GLsizei count,
187 GLsizei instances) const
188{
189 gl::Buffer *buffer = attrib.buffer.get();
190 BufferD3D *bufferImpl = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL;
191 StaticVertexBufferInterface *staticBuffer = bufferImpl ? bufferImpl->getStaticVertexBuffer() : NULL;
192 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
193
194 if (!vertexBuffer->directStoragePossible(attrib, currentValue))
195 {
196 if (staticBuffer)
197 {
198 if (staticBuffer->getBufferSize() == 0)
199 {
200 int totalCount = ElementsInBuffer(attrib, bufferImpl->getSize());
201 if (!staticBuffer->reserveVertexSpace(attrib, totalCount, 0))
202 {
203 return false;
204 }
205 }
206 }
207 else
208 {
209 int totalCount = StreamingBufferElementCount(attrib, count, instances);
210 ASSERT(!bufferImpl || ElementsInBuffer(attrib, bufferImpl->getSize()) >= totalCount);
211
212 if (!mStreamingBuffer->reserveVertexSpace(attrib, totalCount, instances))
213 {
214 return false;
215 }
216 }
217 }
218
219 return true;
220}
221
Jamie Madillf41522b2014-08-18 16:39:49 -0400222GLenum VertexDataManager::storeAttribute(const gl::VertexAttribute &attrib,
223 const gl::VertexAttribCurrentValueData &currentValue,
224 TranslatedAttribute *translated,
225 GLint start,
226 GLsizei count,
227 GLsizei instances)
228{
229 gl::Buffer *buffer = attrib.buffer.get();
Jamie Madill2b976812014-08-25 15:47:49 -0400230 ASSERT(buffer || attrib.pointer);
Jamie Madillf41522b2014-08-18 16:39:49 -0400231
232 BufferD3D *storage = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL;
233 StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : NULL;
234 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
235 bool directStorage = vertexBuffer->directStoragePossible(attrib, currentValue);
236
237 unsigned int streamOffset = 0;
238 unsigned int outputElementSize = 0;
239
240 if (directStorage)
241 {
242 outputElementSize = ComputeVertexAttributeStride(attrib);
243 streamOffset = attrib.offset + outputElementSize * start;
244 }
245 else if (staticBuffer)
246 {
247 if (!staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize))
248 {
249 return GL_OUT_OF_MEMORY;
250 }
251
252 if (!staticBuffer->lookupAttribute(attrib, &streamOffset))
253 {
254 // Convert the entire buffer
255 int totalCount = ElementsInBuffer(attrib, storage->getSize());
256 int startIndex = attrib.offset / ComputeVertexAttributeStride(attrib);
257
258 if (!staticBuffer->storeVertexAttributes(attrib, currentValue, -startIndex, totalCount,
259 0, &streamOffset))
260 {
261 return GL_OUT_OF_MEMORY;
262 }
263 }
264
265 unsigned int firstElementOffset = (attrib.offset / ComputeVertexAttributeStride(attrib)) * outputElementSize;
266 unsigned int startOffset = (instances == 0 || attrib.divisor == 0) ? start * outputElementSize : 0;
267 if (streamOffset + firstElementOffset + startOffset < streamOffset)
268 {
269 return GL_OUT_OF_MEMORY;
270 }
271
272 streamOffset += firstElementOffset + startOffset;
273 }
274 else
275 {
276 int totalCount = StreamingBufferElementCount(attrib, count, instances);
277 if (!mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize) ||
278 !mStreamingBuffer->storeVertexAttributes(attrib, currentValue, start, totalCount, instances,
279 &streamOffset))
280 {
281 return GL_OUT_OF_MEMORY;
282 }
283 }
284
285 translated->storage = directStorage ? storage : NULL;
286 translated->vertexBuffer = vertexBuffer->getVertexBuffer();
287 translated->serial = directStorage ? storage->getSerial() : vertexBuffer->getSerial();
288 translated->divisor = attrib.divisor;
289
290 translated->attribute = &attrib;
291 translated->currentValueType = currentValue.Type;
292 translated->stride = outputElementSize;
293 translated->offset = streamOffset;
294
295 return GL_NO_ERROR;
296}
297
298GLenum VertexDataManager::storeCurrentValue(const gl::VertexAttribute &attrib,
299 const gl::VertexAttribCurrentValueData &currentValue,
300 TranslatedAttribute *translated,
301 gl::VertexAttribCurrentValueData *cachedValue,
302 size_t *cachedOffset,
303 StreamingVertexBufferInterface *buffer)
304{
305 if (*cachedValue != currentValue)
306 {
307 if (!buffer->reserveVertexSpace(attrib, 1, 0))
308 {
309 return GL_OUT_OF_MEMORY;
310 }
311
312 unsigned int streamOffset;
313 if (!buffer->storeVertexAttributes(attrib, currentValue, 0, 1, 0, &streamOffset))
314 {
315 return GL_OUT_OF_MEMORY;
316 }
317
318 *cachedValue = currentValue;
319 *cachedOffset = streamOffset;
320 }
321
322 translated->storage = NULL;
323 translated->vertexBuffer = buffer->getVertexBuffer();
324 translated->serial = buffer->getSerial();
325 translated->divisor = 0;
326
327 translated->attribute = &attrib;
328 translated->currentValueType = currentValue.Type;
329 translated->stride = 0;
330 translated->offset = *cachedOffset;
331
332 return GL_NO_ERROR;
333}
334
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000335}