blob: 237c0ad77e5218527f2fabe2652d84f031654422 [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 {
Gregoire Payen de La Garanderie2c7ea052015-01-07 12:47:35 +000049 // When instanceDrawCount is not a multiple attrib.divisor, the division must round up.
50 // For instance, with 5 non-instanced vertices and a divisor equal to 3, we need 2 instanced vertices.
51 return (instanceDrawCount + attrib.divisor - 1) / attrib.divisor;
Jamie Madill9b4f3842013-08-26 15:29:30 -040052 }
53
54 return vertexDrawCount;
55}
56
Jamie Madill93e13fb2014-11-06 15:27:25 -050057VertexDataManager::VertexDataManager(RendererD3D *renderer) : mRenderer(renderer)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000058{
daniel@transgaming.com31240482012-11-28 21:06:41 +000059 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000060 {
shannon.woods%transgaming.com@gtempaccount.com3026dc72013-04-13 03:37:27 +000061 mCurrentValue[i].FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
62 mCurrentValue[i].FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
63 mCurrentValue[i].FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
64 mCurrentValue[i].FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
65 mCurrentValue[i].Type = GL_FLOAT;
daniel@transgaming.com83921382011-01-08 05:46:00 +000066 mCurrentValueBuffer[i] = NULL;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000067 mCurrentValueOffsets[i] = 0;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000068 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000069
daniel@transgaming.come4e45062012-12-20 20:56:53 +000070 mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000071
72 if (!mStreamingBuffer)
73 {
74 ERR("Failed to allocate the streaming vertex buffer.");
75 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000076}
77
78VertexDataManager::~VertexDataManager()
79{
daniel@transgaming.com83921382011-01-08 05:46:00 +000080 delete mStreamingBuffer;
81
daniel@transgaming.com31240482012-11-28 21:06:41 +000082 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +000083 {
84 delete mCurrentValueBuffer[i];
85 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000086}
87
Shannon Woods1a965482014-09-22 18:00:32 -040088gl::Error VertexDataManager::prepareVertexData(const gl::State &state, GLint start, GLsizei count,
89 TranslatedAttribute *translated, GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +000090{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000091 if (!mStreamingBuffer)
92 {
Geoff Langf7100b92014-09-08 16:17:08 -040093 return gl::Error(GL_OUT_OF_MEMORY, "Internal streaming vertex buffer is unexpectedly NULL.");
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000094 }
95
Jamie Madill6d113802014-08-25 15:47:52 -040096 // Invalidate static buffers that don't contain matching attributes
daniel@transgaming.com31240482012-11-28 21:06:41 +000097 for (int attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000098 {
Geoff Lang7dd2e102014-11-10 15:19:26 -050099 translated[attributeIndex].active = (state.getProgram()->getSemanticIndex(attributeIndex) != -1);
Shannon Woods1a965482014-09-22 18:00:32 -0400100 const gl::VertexAttribute &curAttrib = state.getVertexAttribState(attributeIndex);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000101
Shannon Woods1a965482014-09-22 18:00:32 -0400102 if (translated[attributeIndex].active && curAttrib.enabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000103 {
Shannon Woods1a965482014-09-22 18:00:32 -0400104 invalidateMatchingStaticData(curAttrib, state.getVertexAttribCurrentValue(attributeIndex));
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000105 }
106 }
107
108 // Reserve the required space in the buffers
109 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
110 {
Shannon Woods1a965482014-09-22 18:00:32 -0400111 const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i);
112 if (translated[i].active && curAttrib.enabled)
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000113 {
Shannon Woods1a965482014-09-22 18:00:32 -0400114 gl::Error error = reserveSpaceForAttrib(curAttrib, state.getVertexAttribCurrentValue(i), count, instances);
Geoff Langf7100b92014-09-08 16:17:08 -0400115 if (error.isError())
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000116 {
Geoff Langf7100b92014-09-08 16:17:08 -0400117 return error;
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000118 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000119 }
120 }
121
122 // Perform the vertex data translations
daniel@transgaming.com31240482012-11-28 21:06:41 +0000123 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000124 {
Shannon Woods1a965482014-09-22 18:00:32 -0400125 const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000126 if (translated[i].active)
127 {
Shannon Woods1a965482014-09-22 18:00:32 -0400128 if (curAttrib.enabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000129 {
Shannon Woods1a965482014-09-22 18:00:32 -0400130 gl::Error error = storeAttribute(curAttrib, state.getVertexAttribCurrentValue(i),
131 &translated[i], start, count, instances);
132
Geoff Langf7100b92014-09-08 16:17:08 -0400133 if (error.isError())
134 {
135 return error;
136 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000137 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000138 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000139 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000140 if (!mCurrentValueBuffer[i])
141 {
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000142 mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE);
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000143 }
144
Shannon Woods1a965482014-09-22 18:00:32 -0400145 gl::Error error = storeCurrentValue(curAttrib, state.getVertexAttribCurrentValue(i), &translated[i],
Geoff Langf7100b92014-09-08 16:17:08 -0400146 &mCurrentValue[i], &mCurrentValueOffsets[i],
147 mCurrentValueBuffer[i]);
148 if (error.isError())
149 {
150 return error;
151 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000152 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000153 }
154 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000155
daniel@transgaming.com31240482012-11-28 21:06:41 +0000156 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000157 {
Shannon Woods1a965482014-09-22 18:00:32 -0400158 const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i);
159 if (translated[i].active && curAttrib.enabled)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000160 {
Shannon Woods1a965482014-09-22 18:00:32 -0400161 gl::Buffer *buffer = curAttrib.buffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000162
163 if (buffer)
164 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700165 BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation());
Shannon Woods1a965482014-09-22 18:00:32 -0400166 bufferImpl->promoteStaticUsage(count * ComputeVertexAttributeTypeSize(curAttrib));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000167 }
168 }
169 }
170
Geoff Langf7100b92014-09-08 16:17:08 -0400171 return gl::Error(GL_NO_ERROR);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000172}
173
Jamie Madill6d113802014-08-25 15:47:52 -0400174void VertexDataManager::invalidateMatchingStaticData(const gl::VertexAttribute &attrib,
175 const gl::VertexAttribCurrentValueData &currentValue) const
176{
177 gl::Buffer *buffer = attrib.buffer.get();
178
179 if (buffer)
180 {
181 BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation());
182 StaticVertexBufferInterface *staticBuffer = bufferImpl->getStaticVertexBuffer();
183
184 if (staticBuffer &&
185 staticBuffer->getBufferSize() > 0 &&
186 !staticBuffer->lookupAttribute(attrib, NULL) &&
187 !staticBuffer->directStoragePossible(attrib, currentValue))
188 {
189 bufferImpl->invalidateStaticData();
190 }
191 }
192}
193
Geoff Langf7100b92014-09-08 16:17:08 -0400194gl::Error VertexDataManager::reserveSpaceForAttrib(const gl::VertexAttribute &attrib,
195 const gl::VertexAttribCurrentValueData &currentValue,
196 GLsizei count,
197 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400198{
199 gl::Buffer *buffer = attrib.buffer.get();
200 BufferD3D *bufferImpl = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL;
201 StaticVertexBufferInterface *staticBuffer = bufferImpl ? bufferImpl->getStaticVertexBuffer() : NULL;
202 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
203
204 if (!vertexBuffer->directStoragePossible(attrib, currentValue))
205 {
206 if (staticBuffer)
207 {
208 if (staticBuffer->getBufferSize() == 0)
209 {
210 int totalCount = ElementsInBuffer(attrib, bufferImpl->getSize());
Geoff Langf7100b92014-09-08 16:17:08 -0400211 gl::Error error = staticBuffer->reserveVertexSpace(attrib, totalCount, 0);
212 if (error.isError())
Jamie Madill6d113802014-08-25 15:47:52 -0400213 {
Geoff Langf7100b92014-09-08 16:17:08 -0400214 return error;
Jamie Madill6d113802014-08-25 15:47:52 -0400215 }
216 }
217 }
218 else
219 {
220 int totalCount = StreamingBufferElementCount(attrib, count, instances);
221 ASSERT(!bufferImpl || ElementsInBuffer(attrib, bufferImpl->getSize()) >= totalCount);
222
Geoff Langf7100b92014-09-08 16:17:08 -0400223 gl::Error error = mStreamingBuffer->reserveVertexSpace(attrib, totalCount, instances);
224 if (error.isError())
Jamie Madill6d113802014-08-25 15:47:52 -0400225 {
Geoff Langf7100b92014-09-08 16:17:08 -0400226 return error;
Jamie Madill6d113802014-08-25 15:47:52 -0400227 }
228 }
229 }
230
Geoff Langf7100b92014-09-08 16:17:08 -0400231 return gl::Error(GL_NO_ERROR);
Jamie Madill6d113802014-08-25 15:47:52 -0400232}
233
Geoff Langf7100b92014-09-08 16:17:08 -0400234gl::Error VertexDataManager::storeAttribute(const gl::VertexAttribute &attrib,
235 const gl::VertexAttribCurrentValueData &currentValue,
236 TranslatedAttribute *translated,
237 GLint start,
238 GLsizei count,
239 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400240{
241 gl::Buffer *buffer = attrib.buffer.get();
Jamie Madill2b976812014-08-25 15:47:49 -0400242 ASSERT(buffer || attrib.pointer);
Jamie Madillf41522b2014-08-18 16:39:49 -0400243
244 BufferD3D *storage = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL;
245 StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : NULL;
246 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
247 bool directStorage = vertexBuffer->directStoragePossible(attrib, currentValue);
248
249 unsigned int streamOffset = 0;
250 unsigned int outputElementSize = 0;
251
Jamie Madilld4b55a02015-01-09 14:21:49 -0500252 // Instanced vertices do not apply the 'start' offset
253 GLint firstVertexIndex = (instances > 0 && attrib.divisor > 0 ? 0 : start);
254
Jamie Madillf41522b2014-08-18 16:39:49 -0400255 if (directStorage)
256 {
257 outputElementSize = ComputeVertexAttributeStride(attrib);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500258 streamOffset = attrib.offset + outputElementSize * firstVertexIndex;
Jamie Madillf41522b2014-08-18 16:39:49 -0400259 }
260 else if (staticBuffer)
261 {
Geoff Langf7100b92014-09-08 16:17:08 -0400262 gl::Error error = staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
263 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400264 {
Geoff Langf7100b92014-09-08 16:17:08 -0400265 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400266 }
267
268 if (!staticBuffer->lookupAttribute(attrib, &streamOffset))
269 {
270 // Convert the entire buffer
271 int totalCount = ElementsInBuffer(attrib, storage->getSize());
272 int startIndex = attrib.offset / ComputeVertexAttributeStride(attrib);
273
Geoff Langf7100b92014-09-08 16:17:08 -0400274 gl::Error error = staticBuffer->storeVertexAttributes(attrib, currentValue, -startIndex, totalCount,
275 0, &streamOffset);
276 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400277 {
Geoff Langf7100b92014-09-08 16:17:08 -0400278 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400279 }
280 }
281
282 unsigned int firstElementOffset = (attrib.offset / ComputeVertexAttributeStride(attrib)) * outputElementSize;
Jamie Madilld4b55a02015-01-09 14:21:49 -0500283 unsigned int startOffset = (instances == 0 || attrib.divisor == 0) ? firstVertexIndex * outputElementSize : 0;
Jamie Madillf41522b2014-08-18 16:39:49 -0400284 if (streamOffset + firstElementOffset + startOffset < streamOffset)
285 {
Geoff Langf7100b92014-09-08 16:17:08 -0400286 return gl::Error(GL_OUT_OF_MEMORY);
Jamie Madillf41522b2014-08-18 16:39:49 -0400287 }
288
289 streamOffset += firstElementOffset + startOffset;
290 }
291 else
292 {
293 int totalCount = StreamingBufferElementCount(attrib, count, instances);
Geoff Langf7100b92014-09-08 16:17:08 -0400294 gl::Error error = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
295 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400296 {
Geoff Langf7100b92014-09-08 16:17:08 -0400297 return error;
298 }
299
Jamie Madilld4b55a02015-01-09 14:21:49 -0500300 error = mStreamingBuffer->storeVertexAttributes(attrib, currentValue, firstVertexIndex,
301 totalCount, instances, &streamOffset);
Geoff Langf7100b92014-09-08 16:17:08 -0400302 if (error.isError())
303 {
304 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400305 }
306 }
307
308 translated->storage = directStorage ? storage : NULL;
309 translated->vertexBuffer = vertexBuffer->getVertexBuffer();
310 translated->serial = directStorage ? storage->getSerial() : vertexBuffer->getSerial();
311 translated->divisor = attrib.divisor;
312
313 translated->attribute = &attrib;
314 translated->currentValueType = currentValue.Type;
315 translated->stride = outputElementSize;
316 translated->offset = streamOffset;
317
Geoff Langf7100b92014-09-08 16:17:08 -0400318 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400319}
320
Geoff Langf7100b92014-09-08 16:17:08 -0400321gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribute &attrib,
322 const gl::VertexAttribCurrentValueData &currentValue,
323 TranslatedAttribute *translated,
324 gl::VertexAttribCurrentValueData *cachedValue,
325 size_t *cachedOffset,
326 StreamingVertexBufferInterface *buffer)
Jamie Madillf41522b2014-08-18 16:39:49 -0400327{
328 if (*cachedValue != currentValue)
329 {
Geoff Langf7100b92014-09-08 16:17:08 -0400330 gl::Error error = buffer->reserveVertexSpace(attrib, 1, 0);
331 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400332 {
Geoff Langf7100b92014-09-08 16:17:08 -0400333 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400334 }
335
336 unsigned int streamOffset;
Geoff Langf7100b92014-09-08 16:17:08 -0400337 error = buffer->storeVertexAttributes(attrib, currentValue, 0, 1, 0, &streamOffset);
338 if (error.isError())
Jamie Madillf41522b2014-08-18 16:39:49 -0400339 {
Geoff Langf7100b92014-09-08 16:17:08 -0400340 return error;
Jamie Madillf41522b2014-08-18 16:39:49 -0400341 }
342
343 *cachedValue = currentValue;
344 *cachedOffset = streamOffset;
345 }
346
347 translated->storage = NULL;
348 translated->vertexBuffer = buffer->getVertexBuffer();
349 translated->serial = buffer->getSerial();
350 translated->divisor = 0;
351
352 translated->attribute = &attrib;
353 translated->currentValueType = currentValue.Type;
354 translated->stride = 0;
355 translated->offset = *cachedOffset;
356
Geoff Langf7100b92014-09-08 16:17:08 -0400357 return gl::Error(GL_NO_ERROR);
Jamie Madillf41522b2014-08-18 16:39:49 -0400358}
359
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000360}