blob: 7034b78eabc24402fa24ce945c13659c76f023f0 [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
Geoff Langf7100b92014-09-08 16:17:08 -040085gl::Error 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 {
Geoff Langf7100b92014-09-08 16:17:08 -040090 return gl::Error(GL_OUT_OF_MEMORY, "Internal streaming vertex buffer is unexpectedly NULL.");
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000091 }
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 {
Geoff Langf7100b92014-09-08 16:17:08 -0400109 gl::Error error = reserveSpaceForAttrib(attribs[i], currentValues[i], count, instances);
110 if (error.isError())
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000111 {
Geoff Langf7100b92014-09-08 16:17:08 -0400112 return error;
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000113 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000114 }
115 }
116
117 // Perform the vertex data translations
daniel@transgaming.com31240482012-11-28 21:06:41 +0000118 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000119 {
120 if (translated[i].active)
121 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700122 if (attribs[i].enabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000123 {
Geoff Langf7100b92014-09-08 16:17:08 -0400124 gl::Error error = storeAttribute(attribs[i], currentValues[i], &translated[i],
125 start, count, instances);
126 if (error.isError())
127 {
128 return error;
129 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000130 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000131 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000132 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000133 if (!mCurrentValueBuffer[i])
134 {
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000135 mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE);
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000136 }
137
Geoff Langf7100b92014-09-08 16:17:08 -0400138 gl::Error error = storeCurrentValue(attribs[i], currentValues[i], &translated[i],
139 &mCurrentValue[i], &mCurrentValueOffsets[i],
140 mCurrentValueBuffer[i]);
141 if (error.isError())
142 {
143 return error;
144 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000145 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146 }
147 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000148
daniel@transgaming.com31240482012-11-28 21:06:41 +0000149 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000150 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700151 if (translated[i].active && attribs[i].enabled)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000152 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700153 gl::Buffer *buffer = attribs[i].buffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000154
155 if (buffer)
156 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700157 BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation());
158 bufferImpl->promoteStaticUsage(count * ComputeVertexAttributeTypeSize(attribs[i]));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000159 }
160 }
161 }
162
Geoff Langf7100b92014-09-08 16:17:08 -0400163 return gl::Error(GL_NO_ERROR);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000164}
165
Jamie Madill6d113802014-08-25 15:47:52 -0400166void VertexDataManager::invalidateMatchingStaticData(const gl::VertexAttribute &attrib,
167 const gl::VertexAttribCurrentValueData &currentValue) const
168{
169 gl::Buffer *buffer = attrib.buffer.get();
170
171 if (buffer)
172 {
173 BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation());
174 StaticVertexBufferInterface *staticBuffer = bufferImpl->getStaticVertexBuffer();
175
176 if (staticBuffer &&
177 staticBuffer->getBufferSize() > 0 &&
178 !staticBuffer->lookupAttribute(attrib, NULL) &&
179 !staticBuffer->directStoragePossible(attrib, currentValue))
180 {
181 bufferImpl->invalidateStaticData();
182 }
183 }
184}
185
Geoff Langf7100b92014-09-08 16:17:08 -0400186gl::Error VertexDataManager::reserveSpaceForAttrib(const gl::VertexAttribute &attrib,
187 const gl::VertexAttribCurrentValueData &currentValue,
188 GLsizei count,
189 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400190{
191 gl::Buffer *buffer = attrib.buffer.get();
192 BufferD3D *bufferImpl = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL;
193 StaticVertexBufferInterface *staticBuffer = bufferImpl ? bufferImpl->getStaticVertexBuffer() : NULL;
194 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
195
196 if (!vertexBuffer->directStoragePossible(attrib, currentValue))
197 {
198 if (staticBuffer)
199 {
200 if (staticBuffer->getBufferSize() == 0)
201 {
202 int totalCount = ElementsInBuffer(attrib, bufferImpl->getSize());
Geoff Langf7100b92014-09-08 16:17:08 -0400203 gl::Error error = staticBuffer->reserveVertexSpace(attrib, totalCount, 0);
204 if (error.isError())
Jamie Madill6d113802014-08-25 15:47:52 -0400205 {
Geoff Langf7100b92014-09-08 16:17:08 -0400206 return error;
Jamie Madill6d113802014-08-25 15:47:52 -0400207 }
208 }
209 }
210 else
211 {
212 int totalCount = StreamingBufferElementCount(attrib, count, instances);
213 ASSERT(!bufferImpl || ElementsInBuffer(attrib, bufferImpl->getSize()) >= totalCount);
214
Geoff Langf7100b92014-09-08 16:17:08 -0400215 gl::Error error = mStreamingBuffer->reserveVertexSpace(attrib, totalCount, instances);
216 if (error.isError())
Jamie Madill6d113802014-08-25 15:47:52 -0400217 {
Geoff Langf7100b92014-09-08 16:17:08 -0400218 return error;
Jamie Madill6d113802014-08-25 15:47:52 -0400219 }
220 }
221 }
222
Geoff Langf7100b92014-09-08 16:17:08 -0400223 return gl::Error(GL_NO_ERROR);
Jamie Madill6d113802014-08-25 15:47:52 -0400224}
225
Geoff Langf7100b92014-09-08 16:17:08 -0400226gl::Error VertexDataManager::storeAttribute(const gl::VertexAttribute &attrib,
227 const gl::VertexAttribCurrentValueData &currentValue,
228 TranslatedAttribute *translated,
229 GLint start,
230 GLsizei count,
231 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400232{
233 gl::Buffer *buffer = attrib.buffer.get();
Jamie Madill2b976812014-08-25 15:47:49 -0400234 ASSERT(buffer || attrib.pointer);
Jamie Madillf41522b2014-08-18 16:39:49 -0400235
236 BufferD3D *storage = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL;
237 StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : NULL;
238 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
239 bool directStorage = vertexBuffer->directStoragePossible(attrib, currentValue);
240
241 unsigned int streamOffset = 0;
242 unsigned int outputElementSize = 0;
243
244 if (directStorage)
245 {
246 outputElementSize = ComputeVertexAttributeStride(attrib);
247 streamOffset = attrib.offset + outputElementSize * start;
248 }
249 else if (staticBuffer)
250 {
Geoff Langf7100b92014-09-08 16:17:08 -0400251 gl::Error error = staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
252 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400253 {
Geoff Langf7100b92014-09-08 16:17:08 -0400254 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400255 }
256
257 if (!staticBuffer->lookupAttribute(attrib, &streamOffset))
258 {
259 // Convert the entire buffer
260 int totalCount = ElementsInBuffer(attrib, storage->getSize());
261 int startIndex = attrib.offset / ComputeVertexAttributeStride(attrib);
262
Geoff Langf7100b92014-09-08 16:17:08 -0400263 gl::Error error = staticBuffer->storeVertexAttributes(attrib, currentValue, -startIndex, totalCount,
264 0, &streamOffset);
265 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400266 {
Geoff Langf7100b92014-09-08 16:17:08 -0400267 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400268 }
269 }
270
271 unsigned int firstElementOffset = (attrib.offset / ComputeVertexAttributeStride(attrib)) * outputElementSize;
272 unsigned int startOffset = (instances == 0 || attrib.divisor == 0) ? start * outputElementSize : 0;
273 if (streamOffset + firstElementOffset + startOffset < streamOffset)
274 {
Geoff Langf7100b92014-09-08 16:17:08 -0400275 return gl::Error(GL_OUT_OF_MEMORY);
Jamie Madillf41522b2014-08-18 16:39:49 -0400276 }
277
278 streamOffset += firstElementOffset + startOffset;
279 }
280 else
281 {
282 int totalCount = StreamingBufferElementCount(attrib, count, instances);
Geoff Langf7100b92014-09-08 16:17:08 -0400283 gl::Error error = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
284 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400285 {
Geoff Langf7100b92014-09-08 16:17:08 -0400286 return error;
287 }
288
289 error = mStreamingBuffer->storeVertexAttributes(attrib, currentValue, start, totalCount, instances, &streamOffset);
290 if (error.isError())
291 {
292 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400293 }
294 }
295
296 translated->storage = directStorage ? storage : NULL;
297 translated->vertexBuffer = vertexBuffer->getVertexBuffer();
298 translated->serial = directStorage ? storage->getSerial() : vertexBuffer->getSerial();
299 translated->divisor = attrib.divisor;
300
301 translated->attribute = &attrib;
302 translated->currentValueType = currentValue.Type;
303 translated->stride = outputElementSize;
304 translated->offset = streamOffset;
305
Geoff Langf7100b92014-09-08 16:17:08 -0400306 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400307}
308
Geoff Langf7100b92014-09-08 16:17:08 -0400309gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribute &attrib,
310 const gl::VertexAttribCurrentValueData &currentValue,
311 TranslatedAttribute *translated,
312 gl::VertexAttribCurrentValueData *cachedValue,
313 size_t *cachedOffset,
314 StreamingVertexBufferInterface *buffer)
Jamie Madillf41522b2014-08-18 16:39:49 -0400315{
316 if (*cachedValue != currentValue)
317 {
Geoff Langf7100b92014-09-08 16:17:08 -0400318 gl::Error error = buffer->reserveVertexSpace(attrib, 1, 0);
319 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400320 {
Geoff Langf7100b92014-09-08 16:17:08 -0400321 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400322 }
323
324 unsigned int streamOffset;
Geoff Langf7100b92014-09-08 16:17:08 -0400325 error = buffer->storeVertexAttributes(attrib, currentValue, 0, 1, 0, &streamOffset);
326 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400327 {
Geoff Langf7100b92014-09-08 16:17:08 -0400328 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400329 }
330
331 *cachedValue = currentValue;
332 *cachedOffset = streamOffset;
333 }
334
335 translated->storage = NULL;
336 translated->vertexBuffer = buffer->getVertexBuffer();
337 translated->serial = buffer->getSerial();
338 translated->divisor = 0;
339
340 translated->attribute = &attrib;
341 translated->currentValueType = currentValue.Type;
342 translated->stride = 0;
343 translated->offset = *cachedOffset;
344
Geoff Langf7100b92014-09-08 16:17:08 -0400345 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400346}
347
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000348}