blob: 6aad5ebb826a7a2df7b108eaf62df58e78e740de [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
daniel@transgaming.com50aadb02012-11-28 21:06:11 +000011#include "libGLESv2/renderer/VertexDataManager.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000012#include "libGLESv2/renderer/BufferStorage.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"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000017#include "libGLESv2/renderer/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
Geoff Langa36ead42013-08-02 11:54:08 -040030static int ElementsInBuffer(const gl::VertexAttribute &attribute, 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
38 GLsizei stride = attribute.stride();
jbauman@chromium.org059fc152011-11-18 19:26:17 +000039 return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride;
40}
41
Jamie Madill9b4f3842013-08-26 15:29:30 -040042static int StreamingBufferElementCount(const gl::VertexAttribute &attribute, int vertexDrawCount, int instanceDrawCount)
43{
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.
48 if (instanceDrawCount > 0 && attribute.mDivisor > 0)
49 {
50 return instanceDrawCount / attribute.mDivisor;
51 }
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 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000103 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000104 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000105 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000106 StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000107
Geoff Langa36ead42013-08-02 11:54:08 -0400108 if (staticBuffer && staticBuffer->getBufferSize() > 0 && !staticBuffer->lookupAttribute(attribs[i], NULL) &&
Jamie Madilla5d67472014-02-04 16:04:13 -0500109 !staticBuffer->directStoragePossible(attribs[i], currentValues[i]))
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000110 {
111 buffer->invalidateStaticData();
112 }
113 }
114 }
115
116 // Reserve the required space in the buffers
117 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
118 {
119 if (translated[i].active && attribs[i].mArrayEnabled)
120 {
121 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
122 StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
123 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000124
Jamie Madilla5d67472014-02-04 16:04:13 -0500125 if (!vertexBuffer->directStoragePossible(attribs[i], currentValues[i]))
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000126 {
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000127 if (staticBuffer)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000128 {
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000129 if (staticBuffer->getBufferSize() == 0)
130 {
Jamie Madilla857c362013-07-02 11:57:02 -0400131 int totalCount = ElementsInBuffer(attribs[i], buffer->size());
Geoff Langfe5b2722013-07-09 15:58:36 -0400132 if (!staticBuffer->reserveVertexSpace(attribs[i], totalCount, 0))
133 {
134 return GL_OUT_OF_MEMORY;
135 }
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000136 }
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000137 }
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000138 else
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000139 {
Jamie Madill9b4f3842013-08-26 15:29:30 -0400140 int totalCount = StreamingBufferElementCount(attribs[i], count, instances);
141
142 // [OpenGL ES 3.0.2] section 2.9.4 page 40:
143 // We can return INVALID_OPERATION if our vertex attribute does not have enough backing data.
144 if (buffer && ElementsInBuffer(attribs[i], buffer->size()) < totalCount)
145 {
146 return GL_INVALID_OPERATION;
147 }
148
149 if (!mStreamingBuffer->reserveVertexSpace(attribs[i], totalCount, instances))
Geoff Langfe5b2722013-07-09 15:58:36 -0400150 {
151 return GL_OUT_OF_MEMORY;
152 }
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000153 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000154 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000155 }
156 }
157
158 // Perform the vertex data translations
daniel@transgaming.com31240482012-11-28 21:06:41 +0000159 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000160 {
161 if (translated[i].active)
162 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000163 if (attribs[i].mArrayEnabled)
164 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000165 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000166
daniel@transgaming.com83921382011-01-08 05:46:00 +0000167 if (!buffer && attribs[i].mPointer == NULL)
168 {
169 // This is an application error that would normally result in a crash, but we catch it and return an error
170 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000171 return GL_INVALID_OPERATION;
172 }
173
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000174 StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
175 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000176
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +0000177 BufferStorage *storage = buffer ? buffer->getStorage() : NULL;
Jamie Madilla5d67472014-02-04 16:04:13 -0500178 bool directStorage = vertexBuffer->directStoragePossible(attribs[i], currentValues[i]);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +0000179
Geoff Langa36ead42013-08-02 11:54:08 -0400180 unsigned int streamOffset = 0;
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000181 unsigned int outputElementSize = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000182
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000183 if (directStorage)
184 {
185 outputElementSize = attribs[i].stride();
186 streamOffset = attribs[i].mOffset + outputElementSize * start;
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000187 }
188 else if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000189 {
Geoff Langa36ead42013-08-02 11:54:08 -0400190 if (!staticBuffer->getVertexBuffer()->getSpaceRequired(attribs[i], 1, 0, &outputElementSize))
191 {
192 return GL_OUT_OF_MEMORY;
193 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000194
Geoff Langa36ead42013-08-02 11:54:08 -0400195 if (!staticBuffer->lookupAttribute(attribs[i], &streamOffset))
daniel@transgaming.com83921382011-01-08 05:46:00 +0000196 {
197 // Convert the entire buffer
Jamie Madilla857c362013-07-02 11:57:02 -0400198 int totalCount = ElementsInBuffer(attribs[i], storage->getSize());
daniel@transgaming.com83921382011-01-08 05:46:00 +0000199 int startIndex = attribs[i].mOffset / attribs[i].stride();
200
Geoff Langa36ead42013-08-02 11:54:08 -0400201 if (!staticBuffer->storeVertexAttributes(attribs[i], currentValues[i], -startIndex, totalCount,
202 0, &streamOffset))
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000203 {
Geoff Langa36ead42013-08-02 11:54:08 -0400204 return GL_OUT_OF_MEMORY;
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000205 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000206 }
Geoff Langa36ead42013-08-02 11:54:08 -0400207
208 unsigned int firstElementOffset = (attribs[i].mOffset / attribs[i].stride()) * outputElementSize;
209 unsigned int startOffset = (instances == 0 || attribs[i].mDivisor == 0) ? start * outputElementSize : 0;
210 if (streamOffset + firstElementOffset + startOffset < streamOffset)
211 {
212 return GL_OUT_OF_MEMORY;
213 }
214
215 streamOffset += firstElementOffset + startOffset;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000216 }
217 else
218 {
Jamie Madill9b4f3842013-08-26 15:29:30 -0400219 int totalCount = StreamingBufferElementCount(attribs[i], count, instances);
Geoff Langa36ead42013-08-02 11:54:08 -0400220 if (!mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attribs[i], 1, 0, &outputElementSize) ||
Jamie Madill9b4f3842013-08-26 15:29:30 -0400221 !mStreamingBuffer->storeVertexAttributes(attribs[i], currentValues[i], start, totalCount, instances,
Geoff Langa36ead42013-08-02 11:54:08 -0400222 &streamOffset))
223 {
224 return GL_OUT_OF_MEMORY;
225 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000226 }
227
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000228 translated[i].storage = directStorage ? storage : NULL;
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000229 translated[i].vertexBuffer = vertexBuffer->getVertexBuffer();
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000230 translated[i].serial = directStorage ? storage->getSerial() : vertexBuffer->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000231 translated[i].divisor = attribs[i].mDivisor;
232
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000233 translated[i].attribute = &attribs[i];
Jamie Madilla857c362013-07-02 11:57:02 -0400234 translated[i].currentValueType = currentValues[i].Type;
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000235 translated[i].stride = outputElementSize;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000236 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000237 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000238 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000239 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000240 if (!mCurrentValueBuffer[i])
241 {
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000242 mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE);
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000243 }
244
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000245 StreamingVertexBufferInterface *buffer = mCurrentValueBuffer[i];
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000246
Geoff Lang89bf4bf2013-10-31 10:32:46 -0400247 if (mCurrentValue[i] != currentValues[i])
daniel@transgaming.com83921382011-01-08 05:46:00 +0000248 {
Geoff Langfe5b2722013-07-09 15:58:36 -0400249 if (!buffer->reserveVertexSpace(attribs[i], 1, 0))
250 {
251 return GL_OUT_OF_MEMORY;
252 }
253
Geoff Langa36ead42013-08-02 11:54:08 -0400254 unsigned int streamOffset;
255 if (!buffer->storeVertexAttributes(attribs[i], currentValues[i], 0, 1, 0, &streamOffset))
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000256 {
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000257 return GL_OUT_OF_MEMORY;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000258 }
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000259
Geoff Lang89bf4bf2013-10-31 10:32:46 -0400260 mCurrentValue[i] = currentValues[i];
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000261 mCurrentValueOffsets[i] = streamOffset;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000262 }
263
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000264 translated[i].storage = NULL;
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000265 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getVertexBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000266 translated[i].serial = mCurrentValueBuffer[i]->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000267 translated[i].divisor = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000268
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000269 translated[i].attribute = &attribs[i];
Jamie Madilla857c362013-07-02 11:57:02 -0400270 translated[i].currentValueType = currentValues[i].Type;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000271 translated[i].stride = 0;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000272 translated[i].offset = mCurrentValueOffsets[i];
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000273 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000274 }
275 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000276
daniel@transgaming.com31240482012-11-28 21:06:41 +0000277 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000278 {
279 if (translated[i].active && attribs[i].mArrayEnabled)
280 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000281 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000282
283 if (buffer)
284 {
285 buffer->promoteStaticUsage(count * attribs[i].typeSize());
286 }
287 }
288 }
289
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000290 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000291}
292
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000293}