blob: 78b2e4f3ba6eda245958902c5e90ffaffe72b3f3 [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"
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
daniel@transgaming.com4150d362012-12-20 21:07:43 +000029static int elementsInBuffer(const gl::VertexAttribute &attribute, int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000030{
31 int stride = attribute.stride();
32 return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride;
33}
34
daniel@transgaming.com4150d362012-12-20 21:07:43 +000035VertexDataManager::VertexDataManager(Renderer *renderer) : mRenderer(renderer)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000036{
daniel@transgaming.com31240482012-11-28 21:06:41 +000037 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000038 {
shannon.woods%transgaming.com@gtempaccount.com3026dc72013-04-13 03:37:27 +000039 mCurrentValue[i].FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
40 mCurrentValue[i].FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
41 mCurrentValue[i].FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
42 mCurrentValue[i].FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
43 mCurrentValue[i].Type = GL_FLOAT;
daniel@transgaming.com83921382011-01-08 05:46:00 +000044 mCurrentValueBuffer[i] = NULL;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000045 mCurrentValueOffsets[i] = 0;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000046 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000047
daniel@transgaming.come4e45062012-12-20 20:56:53 +000048 mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000049
50 if (!mStreamingBuffer)
51 {
52 ERR("Failed to allocate the streaming vertex buffer.");
53 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000054}
55
56VertexDataManager::~VertexDataManager()
57{
daniel@transgaming.com83921382011-01-08 05:46:00 +000058 delete mStreamingBuffer;
59
daniel@transgaming.com31240482012-11-28 21:06:41 +000060 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +000061 {
62 delete mCurrentValueBuffer[i];
63 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000064}
65
shannon.woods@transgaming.com709fee92013-02-28 23:10:37 +000066static bool directStoragePossible(VertexBufferInterface* vb, const gl::VertexAttribute& attrib)
67{
68 gl::Buffer *buffer = attrib.mBoundBuffer.get();
69 BufferStorage *storage = buffer ? buffer->getStorage() : NULL;
70
shannon.woods@transgaming.com047dc622013-02-28 23:18:08 +000071 const bool isAligned = (attrib.stride() % 4 == 0) && (attrib.mOffset % 4 == 0);
72
73 return storage && storage->supportsDirectBinding() && !vb->getVertexBuffer()->requiresConversion(attrib) && isAligned;
shannon.woods@transgaming.com709fee92013-02-28 23:10:37 +000074}
75
daniel@transgaming.com31240482012-11-28 21:06:41 +000076GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +000077{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000078 if (!mStreamingBuffer)
79 {
80 return GL_OUT_OF_MEMORY;
81 }
82
daniel@transgaming.com31240482012-11-28 21:06:41 +000083 for (int attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000084 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +000085 translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000086 }
87
daniel@transgaming.com4150d362012-12-20 21:07:43 +000088 // Invalidate static buffers that don't contain matching attributes
daniel@transgaming.com31240482012-11-28 21:06:41 +000089 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000090 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000091 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +000092 {
daniel@transgaming.com31240482012-11-28 21:06:41 +000093 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.come4e45062012-12-20 20:56:53 +000094 StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +000095
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +000096 if (staticBuffer && staticBuffer->getBufferSize() > 0 && staticBuffer->lookupAttribute(attribs[i]) == -1 &&
97 !directStoragePossible(staticBuffer, attribs[i]))
98 {
99 buffer->invalidateStaticData();
100 }
101 }
102 }
103
104 // Reserve the required space in the buffers
105 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
106 {
107 if (translated[i].active && attribs[i].mArrayEnabled)
108 {
109 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
110 StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
111 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000112
shannon.woods@transgaming.com709fee92013-02-28 23:10:37 +0000113 if (!directStoragePossible(vertexBuffer, attribs[i]))
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000114 {
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000115 if (staticBuffer)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000116 {
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000117 if (staticBuffer->getBufferSize() == 0)
118 {
shannon.woods@transgaming.com709fee92013-02-28 23:10:37 +0000119 int totalCount = elementsInBuffer(attribs[i], buffer->size());
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000120 staticBuffer->reserveVertexSpace(attribs[i], totalCount, 0);
121 }
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000122 }
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000123 else
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000124 {
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000125 mStreamingBuffer->reserveVertexSpace(attribs[i], count, instances);
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000126 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000127 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000128 }
129 }
130
131 // Perform the vertex data translations
daniel@transgaming.com31240482012-11-28 21:06:41 +0000132 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000133 {
134 if (translated[i].active)
135 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000136 if (attribs[i].mArrayEnabled)
137 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000138 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000139
daniel@transgaming.com83921382011-01-08 05:46:00 +0000140 if (!buffer && attribs[i].mPointer == NULL)
141 {
142 // This is an application error that would normally result in a crash, but we catch it and return an error
143 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000144 return GL_INVALID_OPERATION;
145 }
146
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000147 StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
148 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000149
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +0000150 BufferStorage *storage = buffer ? buffer->getStorage() : NULL;
shannon.woods@transgaming.com709fee92013-02-28 23:10:37 +0000151 bool directStorage = directStoragePossible(vertexBuffer, attribs[i]);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +0000152
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000153 std::size_t streamOffset = -1;
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000154 unsigned int outputElementSize = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000155
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000156 if (directStorage)
157 {
158 outputElementSize = attribs[i].stride();
159 streamOffset = attribs[i].mOffset + outputElementSize * start;
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000160 }
161 else if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000162 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000163 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000164 outputElementSize = staticBuffer->getVertexBuffer()->getSpaceRequired(attribs[i], 1, 0);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000165
166 if (streamOffset == -1)
167 {
168 // Convert the entire buffer
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +0000169 int totalCount = elementsInBuffer(attribs[i], storage->getSize());
daniel@transgaming.com83921382011-01-08 05:46:00 +0000170 int startIndex = attribs[i].mOffset / attribs[i].stride();
171
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000172 streamOffset = staticBuffer->storeVertexAttributes(attribs[i], -startIndex, totalCount, 0);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000173 }
174
175 if (streamOffset != -1)
176 {
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000177 streamOffset += (attribs[i].mOffset / attribs[i].stride()) * outputElementSize;
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000178
179 if (instances == 0 || attribs[i].mDivisor == 0)
180 {
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000181 streamOffset += start * outputElementSize;
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000182 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000183 }
184 }
185 else
186 {
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000187 outputElementSize = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attribs[i], 1, 0);
188 streamOffset = mStreamingBuffer->storeVertexAttributes(attribs[i], start, count, instances);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000189 }
190
daniel@transgaming.com83921382011-01-08 05:46:00 +0000191 if (streamOffset == -1)
192 {
193 return GL_OUT_OF_MEMORY;
194 }
195
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000196 translated[i].storage = directStorage ? storage : NULL;
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000197 translated[i].vertexBuffer = vertexBuffer->getVertexBuffer();
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000198 translated[i].serial = directStorage ? storage->getSerial() : vertexBuffer->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000199 translated[i].divisor = attribs[i].mDivisor;
200
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000201 translated[i].attribute = &attribs[i];
202 translated[i].stride = outputElementSize;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000203 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000204 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000205 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000206 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000207 if (!mCurrentValueBuffer[i])
208 {
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000209 mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE);
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000210 }
211
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000212 StreamingVertexBufferInterface *buffer = mCurrentValueBuffer[i];
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000213
shannon.woods%transgaming.com@gtempaccount.com3026dc72013-04-13 03:37:27 +0000214 if (memcmp(&mCurrentValue[i], &attribs[i].mCurrentValue, sizeof(gl::VertexAttribute::CurrentValueData)) != 0)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000215 {
shannon.woods%transgaming.com@gtempaccount.comcf8d2f82013-04-13 03:37:34 +0000216 buffer->reserveVertexSpace(attribs[i], 1, 0);
217 int streamOffset = buffer->storeVertexAttributes(attribs[i], 0, 1, 0);
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000218 if (streamOffset == -1)
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000219 {
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000220 return GL_OUT_OF_MEMORY;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000221 }
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000222
223 mCurrentValueOffsets[i] = streamOffset;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000224 }
225
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +0000226 translated[i].storage = NULL;
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000227 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getVertexBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000228 translated[i].serial = mCurrentValueBuffer[i]->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000229 translated[i].divisor = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000230
daniel@transgaming.com4150d362012-12-20 21:07:43 +0000231 translated[i].attribute = &attribs[i];
daniel@transgaming.com83921382011-01-08 05:46:00 +0000232 translated[i].stride = 0;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000233 translated[i].offset = mCurrentValueOffsets[i];
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000234 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000235 }
236 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000237
daniel@transgaming.com31240482012-11-28 21:06:41 +0000238 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000239 {
240 if (translated[i].active && attribs[i].mArrayEnabled)
241 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000242 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000243
244 if (buffer)
245 {
246 buffer->promoteStaticUsage(count * attribs[i].typeSize());
247 }
248 }
249 }
250
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000251 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000252}
253
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000254}