blob: 9d92f7b29c771354fe81901be6f11b95afca0799 [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00003// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +00008// VertexDataManager.h: Defines the VertexDataManager, a class that
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00009// runs the Buffer translation process.
10
Brandon Jonesc7a41042014-06-23 12:03:25 -070011#include "libGLESv2/renderer/d3d/VertexDataManager.h"
Brandon Jonesd38f9262014-06-18 16:26:45 -070012#include "libGLESv2/renderer/d3d/BufferD3D.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000013
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"
Brandon Jonesc7a41042014-06-23 12:03:25 -070017#include "libGLESv2/renderer/d3d/VertexBuffer.h"
Jamie Madilla5d67472014-02-04 16:04:13 -050018#include "libGLESv2/renderer/Renderer.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000019
20namespace
21{
22 enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000023 // This has to be at least 4k or else it fails on ATI cards.
24 enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 };
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000025}
26
daniel@transgaming.com31240482012-11-28 21:06:41 +000027namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000028{
29
Brandon Jones5bf98292014-06-06 17:19:38 -070030static int ElementsInBuffer(const gl::VertexAttribute &attrib, unsigned int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000031{
Geoff Langa36ead42013-08-02 11:54:08 -040032 // Size cannot be larger than a GLsizei
33 if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
34 {
35 size = static_cast<unsigned int>(std::numeric_limits<int>::max());
36 }
37
Brandon Jones5bf98292014-06-06 17:19:38 -070038 GLsizei stride = ComputeVertexAttributeStride(attrib);
39 return (size - attrib.offset % stride + (stride - ComputeVertexAttributeTypeSize(attrib))) / stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000040}
41
Brandon Jones5bf98292014-06-06 17:19:38 -070042static int StreamingBufferElementCount(const gl::VertexAttribute &attrib, int vertexDrawCount, int instanceDrawCount)
Jamie Madill9b4f3842013-08-26 15:29:30 -040043{
44 // For instanced rendering, we draw "instanceDrawCount" sets of "vertexDrawCount" vertices.
45 //
46 // A vertex attribute with a positive divisor loads one instanced vertex for every set of
47 // non-instanced vertices, and the instanced vertex index advances once every "mDivisor" instances.
Brandon Jones5bf98292014-06-06 17:19:38 -070048 if (instanceDrawCount > 0 && attrib.divisor > 0)
Jamie Madill9b4f3842013-08-26 15:29:30 -040049 {
Brandon Jones5bf98292014-06-06 17:19:38 -070050 return instanceDrawCount / attrib.divisor;
Jamie Madill9b4f3842013-08-26 15:29:30 -040051 }
52
53 return vertexDrawCount;
54}
55
daniel@transgaming.com4150d362012-12-20 21:07:43 +000056VertexDataManager::VertexDataManager(Renderer *renderer) : mRenderer(renderer)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000057{
daniel@transgaming.com31240482012-11-28 21:06:41 +000058 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000059 {
shannon.woods%transgaming.com@gtempaccount.com3026dc72013-04-13 03:37:27 +000060 mCurrentValue[i].FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
61 mCurrentValue[i].FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
62 mCurrentValue[i].FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
63 mCurrentValue[i].FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
64 mCurrentValue[i].Type = GL_FLOAT;
daniel@transgaming.com83921382011-01-08 05:46:00 +000065 mCurrentValueBuffer[i] = NULL;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000066 mCurrentValueOffsets[i] = 0;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000067 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000068
daniel@transgaming.come4e45062012-12-20 20:56:53 +000069 mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000070
71 if (!mStreamingBuffer)
72 {
73 ERR("Failed to allocate the streaming vertex buffer.");
74 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000075}
76
77VertexDataManager::~VertexDataManager()
78{
daniel@transgaming.com83921382011-01-08 05:46:00 +000079 delete mStreamingBuffer;
80
daniel@transgaming.com31240482012-11-28 21:06:41 +000081 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +000082 {
83 delete mCurrentValueBuffer[i];
84 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000085}
86
Jamie Madilla857c362013-07-02 11:57:02 -040087GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], const gl::VertexAttribCurrentValueData currentValues[],
88 gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +000089{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000090 if (!mStreamingBuffer)
91 {
92 return GL_OUT_OF_MEMORY;
93 }
94
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 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +000097 translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000098 }
99
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000100 // Invalidate static buffers that don't contain matching attributes
daniel@transgaming.com31240482012-11-28 21:06:41 +0000101 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000102 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700103 if (translated[i].active && attribs[i].enabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000104 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700105 gl::Buffer *buffer = attribs[i].buffer.get();
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000106
Brandon Jonesd38f9262014-06-18 16:26:45 -0700107 if (buffer)
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000108 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700109 BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation());
110 StaticVertexBufferInterface *staticBuffer = bufferImpl->getStaticVertexBuffer();
111
112 if (staticBuffer && staticBuffer->getBufferSize() > 0 && !staticBuffer->lookupAttribute(attribs[i], NULL) &&
113 !staticBuffer->directStoragePossible(attribs[i], currentValues[i]))
114 {
115 bufferImpl->invalidateStaticData();
116 }
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000117 }
118 }
119 }
120
121 // Reserve the required space in the buffers
122 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
123 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700124 if (translated[i].active && attribs[i].enabled)
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000125 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700126 gl::Buffer *buffer = attribs[i].buffer.get();
Brandon Jonesb23375f2014-07-08 10:49:39 -0700127 BufferD3D *bufferImpl = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL;
128 StaticVertexBufferInterface *staticBuffer = bufferImpl ? bufferImpl->getStaticVertexBuffer() : NULL;
129 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
130
131 if (!vertexBuffer->directStoragePossible(attribs[i], currentValues[i]))
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000132 {
Brandon Jonesb23375f2014-07-08 10:49:39 -0700133 if (staticBuffer)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000134 {
Brandon Jonesb23375f2014-07-08 10:49:39 -0700135 if (staticBuffer->getBufferSize() == 0)
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000136 {
Brandon Jonesb23375f2014-07-08 10:49:39 -0700137 int totalCount = ElementsInBuffer(attribs[i], bufferImpl->getSize());
138 if (!staticBuffer->reserveVertexSpace(attribs[i], totalCount, 0))
Geoff Langfe5b2722013-07-09 15:58:36 -0400139 {
140 return GL_OUT_OF_MEMORY;
141 }
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000142 }
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000143 }
Brandon Jonesb23375f2014-07-08 10:49:39 -0700144 else
145 {
146 int totalCount = StreamingBufferElementCount(attribs[i], count, instances);
147
148 // [OpenGL ES 3.0.2] section 2.9.4 page 40:
149 // We can return INVALID_OPERATION if our vertex attribute does not have enough backing data.
150 if (bufferImpl && ElementsInBuffer(attribs[i], bufferImpl->getSize()) < totalCount)
151 {
152 return GL_INVALID_OPERATION;
153 }
154
155 if (!mStreamingBuffer->reserveVertexSpace(attribs[i], totalCount, instances))
156 {
157 return GL_OUT_OF_MEMORY;
158 }
159 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000160 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000161 }
162 }
163
164 // Perform the vertex data translations
daniel@transgaming.com31240482012-11-28 21:06:41 +0000165 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000166 {
167 if (translated[i].active)
168 {
Jamie Madillf41522b2014-08-18 16:39:49 -0400169 GLenum result;
170
Brandon Jones5bf98292014-06-06 17:19:38 -0700171 if (attribs[i].enabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000172 {
Jamie Madillf41522b2014-08-18 16:39:49 -0400173 result = storeAttribute(attribs[i], currentValues[i], &translated[i],
174 start, count, instances);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000175 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000176 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000177 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000178 if (!mCurrentValueBuffer[i])
179 {
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000180 mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE);
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000181 }
182
Jamie Madillf41522b2014-08-18 16:39:49 -0400183 result = storeCurrentValue(attribs[i], currentValues[i], &translated[i],
184 &mCurrentValue[i], &mCurrentValueOffsets[i],
185 mCurrentValueBuffer[i]);
186 }
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000187
Jamie Madillf41522b2014-08-18 16:39:49 -0400188 if (result != GL_NO_ERROR)
189 {
190 return result;
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000191 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000192 }
193 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000194
daniel@transgaming.com31240482012-11-28 21:06:41 +0000195 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000196 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700197 if (translated[i].active && attribs[i].enabled)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000198 {
Brandon Jones5bf98292014-06-06 17:19:38 -0700199 gl::Buffer *buffer = attribs[i].buffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000200
201 if (buffer)
202 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700203 BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation());
204 bufferImpl->promoteStaticUsage(count * ComputeVertexAttributeTypeSize(attribs[i]));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000205 }
206 }
207 }
208
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000209 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000210}
211
Jamie Madillf41522b2014-08-18 16:39:49 -0400212GLenum VertexDataManager::storeAttribute(const gl::VertexAttribute &attrib,
213 const gl::VertexAttribCurrentValueData &currentValue,
214 TranslatedAttribute *translated,
215 GLint start,
216 GLsizei count,
217 GLsizei instances)
218{
219 gl::Buffer *buffer = attrib.buffer.get();
220
221 if (!buffer && attrib.pointer == NULL)
222 {
223 // This is an application error that would normally result in a crash, but we catch it and return an error
224 ERR("An enabled vertex array has no buffer and no pointer.");
225 return GL_INVALID_OPERATION;
226 }
227
228 BufferD3D *storage = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL;
229 StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : NULL;
230 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
231 bool directStorage = vertexBuffer->directStoragePossible(attrib, currentValue);
232
233 unsigned int streamOffset = 0;
234 unsigned int outputElementSize = 0;
235
236 if (directStorage)
237 {
238 outputElementSize = ComputeVertexAttributeStride(attrib);
239 streamOffset = attrib.offset + outputElementSize * start;
240 }
241 else if (staticBuffer)
242 {
243 if (!staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize))
244 {
245 return GL_OUT_OF_MEMORY;
246 }
247
248 if (!staticBuffer->lookupAttribute(attrib, &streamOffset))
249 {
250 // Convert the entire buffer
251 int totalCount = ElementsInBuffer(attrib, storage->getSize());
252 int startIndex = attrib.offset / ComputeVertexAttributeStride(attrib);
253
254 if (!staticBuffer->storeVertexAttributes(attrib, currentValue, -startIndex, totalCount,
255 0, &streamOffset))
256 {
257 return GL_OUT_OF_MEMORY;
258 }
259 }
260
261 unsigned int firstElementOffset = (attrib.offset / ComputeVertexAttributeStride(attrib)) * outputElementSize;
262 unsigned int startOffset = (instances == 0 || attrib.divisor == 0) ? start * outputElementSize : 0;
263 if (streamOffset + firstElementOffset + startOffset < streamOffset)
264 {
265 return GL_OUT_OF_MEMORY;
266 }
267
268 streamOffset += firstElementOffset + startOffset;
269 }
270 else
271 {
272 int totalCount = StreamingBufferElementCount(attrib, count, instances);
273 if (!mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize) ||
274 !mStreamingBuffer->storeVertexAttributes(attrib, currentValue, start, totalCount, instances,
275 &streamOffset))
276 {
277 return GL_OUT_OF_MEMORY;
278 }
279 }
280
281 translated->storage = directStorage ? storage : NULL;
282 translated->vertexBuffer = vertexBuffer->getVertexBuffer();
283 translated->serial = directStorage ? storage->getSerial() : vertexBuffer->getSerial();
284 translated->divisor = attrib.divisor;
285
286 translated->attribute = &attrib;
287 translated->currentValueType = currentValue.Type;
288 translated->stride = outputElementSize;
289 translated->offset = streamOffset;
290
291 return GL_NO_ERROR;
292}
293
294GLenum VertexDataManager::storeCurrentValue(const gl::VertexAttribute &attrib,
295 const gl::VertexAttribCurrentValueData &currentValue,
296 TranslatedAttribute *translated,
297 gl::VertexAttribCurrentValueData *cachedValue,
298 size_t *cachedOffset,
299 StreamingVertexBufferInterface *buffer)
300{
301 if (*cachedValue != currentValue)
302 {
303 if (!buffer->reserveVertexSpace(attrib, 1, 0))
304 {
305 return GL_OUT_OF_MEMORY;
306 }
307
308 unsigned int streamOffset;
309 if (!buffer->storeVertexAttributes(attrib, currentValue, 0, 1, 0, &streamOffset))
310 {
311 return GL_OUT_OF_MEMORY;
312 }
313
314 *cachedValue = currentValue;
315 *cachedOffset = streamOffset;
316 }
317
318 translated->storage = NULL;
319 translated->vertexBuffer = buffer->getVertexBuffer();
320 translated->serial = buffer->getSerial();
321 translated->divisor = 0;
322
323 translated->attribute = &attrib;
324 translated->currentValueType = currentValue.Type;
325 translated->stride = 0;
326 translated->offset = *cachedOffset;
327
328 return GL_NO_ERROR;
329}
330
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000331}