blob: 42a051ef57cd57db174afe358a96bacb77f83d74 [file] [log] [blame]
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// geometry/VertexDataManager.h: Defines the VertexDataManager, a class that
8// runs the Buffer translation process.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/geometry/VertexDataManager.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000011
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000012#include "common/debug.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000013
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000014#include "libGLESv2/Buffer.h"
15#include "libGLESv2/Program.h"
16
17#include "libGLESv2/geometry/backend.h"
18#include "libGLESv2/geometry/IndexDataManager.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000019
20namespace
21{
22 enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
23}
24
25namespace gl
26{
27
28VertexDataManager::VertexDataManager(Context *context, BufferBackEnd *backend)
daniel@transgaming.com97bffae2010-05-05 18:49:44 +000029 : mContext(context), mBackend(backend), mDirtyCurrentValues(true), mCurrentValueOffset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000030{
31 mStreamBuffer = mBackend->createVertexBuffer(INITIAL_STREAM_BUFFER_SIZE);
32 try
33 {
daniel@transgaming.come4b08c82010-04-20 18:53:06 +000034 mCurrentValueBuffer = mBackend->createVertexBufferForStrideZero(4*sizeof(float)*MAX_VERTEX_ATTRIBS);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000035 }
36 catch (...)
37 {
38 delete mStreamBuffer;
39 throw;
40 }
41}
42
43VertexDataManager::~VertexDataManager()
44{
45 delete mStreamBuffer;
46 delete mCurrentValueBuffer;
47}
48
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +000049std::bitset<MAX_VERTEX_ATTRIBS> VertexDataManager::getActiveAttribs() const
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000050{
51 std::bitset<MAX_VERTEX_ATTRIBS> active;
52
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +000053 Program *program = mContext->getCurrentProgram();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000054
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +000055 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000056 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +000057 active[attributeIndex] = (program->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000058 }
59
60 return active;
61}
62
63GLenum VertexDataManager::preRenderValidate(GLint start, GLsizei count,
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +000064 TranslatedAttribute *translated)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000065{
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +000066 const AttributeState *attribs = mContext->getVertexAttribBlock();
67 const std::bitset<MAX_VERTEX_ATTRIBS> activeAttribs = getActiveAttribs();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000068
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000069 std::bitset<MAX_VERTEX_ATTRIBS> translateOrLift;
70
71 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
72 {
73 if (!activeAttribs[i] && attribs[i].mEnabled && attribs[i].mBoundBuffer != 0 && !mContext->getBuffer(attribs[i].mBoundBuffer))
74 return GL_INVALID_OPERATION;
75 }
76
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000077 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
78 {
79 translated[i].enabled = activeAttribs[i];
80 }
81
82 processNonArrayAttributes(attribs, activeAttribs, translated);
83
84 // Handle the identity-mapped attributes.
85
86 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
87 {
88 if (activeAttribs[i] && attribs[i].mEnabled)
89 {
90 if (attribs[i].mBoundBuffer != 0 && mBackend->getFormatConverter(attribs[i].mType, attribs[i].mSize, attribs[i].mNormalized).identity)
91 {
daniel@transgaming.comaa1ff872010-04-15 20:44:58 +000092 std::size_t stride = interpretGlStride(attribs[i]);
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +000093 std::size_t offset = static_cast<std::size_t>(static_cast<const char*>(attribs[i].mPointer) - static_cast<const char*>(NULL)) + stride * start;
daniel@transgaming.comaa1ff872010-04-15 20:44:58 +000094
95 if (mBackend->validateStream(attribs[i].mType, attribs[i].mSize, stride, offset))
96 {
97 translated[i].type = attribs[i].mType;
98 translated[i].size = attribs[i].mSize;
99 translated[i].normalized = attribs[i].mNormalized;
100 translated[i].stride = stride;
101 translated[i].offset = offset;
102 translated[i].buffer = mContext->getBuffer(attribs[i].mBoundBuffer)->identityBuffer();
103 }
104 else
105 {
106 translateOrLift[i] = true;
107 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000108 }
109 else
110 {
111 translateOrLift[i] = true;
112 }
113 }
114 }
115
116 // Handle any attributes needing translation or lifting.
117 if (translateOrLift.any())
118 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000119 std::size_t requiredSpace = 0;
120
121 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
122 {
123 if (translateOrLift[i])
124 {
125 requiredSpace += spaceRequired(attribs[i], count);
126 }
127 }
128
129 if (requiredSpace > mStreamBuffer->size())
130 {
131 std::size_t newSize = std::max(requiredSpace, 3 * mStreamBuffer->size() / 2); // 1.5 x mStreamBuffer->size() is arbitrary and should be checked to see we don't have too many reallocations.
132
133 TranslatedVertexBuffer *newStreamBuffer = mBackend->createVertexBuffer(newSize);
134
135 delete mStreamBuffer;
136 mStreamBuffer = newStreamBuffer;
137 }
138
139 mStreamBuffer->reserveSpace(requiredSpace);
140
141 for (size_t i = 0; i < MAX_VERTEX_ATTRIBS; i++)
142 {
143 if (translateOrLift[i])
144 {
145 FormatConverter formatConverter = mBackend->getFormatConverter(attribs[i].mType, attribs[i].mSize, attribs[i].mNormalized);
146
147 translated[i].type = attribs[i].mType;
148 translated[i].size = attribs[i].mSize;
149 translated[i].normalized = attribs[i].mNormalized;
150 translated[i].stride = formatConverter.outputVertexSize;
151 translated[i].buffer = mStreamBuffer;
152
153 void *output = mStreamBuffer->map(spaceRequired(attribs[i], count), &translated[i].offset);
154
155 const void *input;
156 if (attribs[i].mBoundBuffer)
157 {
158 input = mContext->getBuffer(attribs[i].mBoundBuffer)->data();
159 input = static_cast<const char*>(input) + reinterpret_cast<size_t>(attribs[i].mPointer);
160 }
161 else
162 {
163 input = attribs[i].mPointer;
164 }
165
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000166 size_t inputStride = interpretGlStride(attribs[i]);
167
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +0000168 input = static_cast<const char*>(input) + inputStride * start;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000169
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +0000170 formatConverter.convertArray(input, inputStride, count, output);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000171
172 mStreamBuffer->unmap();
173 }
174 }
175 }
176
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000177 return GL_NO_ERROR;
178}
179
180void VertexDataManager::reloadCurrentValues(const AttributeState *attribs, std::size_t *offset)
181{
182 if (mDirtyCurrentValues)
183 {
184 std::size_t totalSize = 4 * sizeof(float) * MAX_VERTEX_ATTRIBS;
185
186 mCurrentValueBuffer->reserveSpace(totalSize);
187
daniel@transgaming.com97bffae2010-05-05 18:49:44 +0000188 float* p = static_cast<float*>(mCurrentValueBuffer->map(totalSize, &mCurrentValueOffset));
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000189
190 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
191 {
192 memcpy(&p[i*4], attribs[i].mCurrentValue, sizeof(attribs[i].mCurrentValue)); // FIXME: this should be doing a translation. This assumes that GL_FLOATx4 is supported.
193 }
194
195 mCurrentValueBuffer->unmap();
196
197 mDirtyCurrentValues = false;
198 }
daniel@transgaming.com97bffae2010-05-05 18:49:44 +0000199
200 *offset = mCurrentValueOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000201}
202
203std::size_t VertexDataManager::typeSize(GLenum type) const
204{
205 switch (type)
206 {
207 case GL_BYTE: case GL_UNSIGNED_BYTE: return sizeof(GLbyte);
208 case GL_SHORT: case GL_UNSIGNED_SHORT: return sizeof(GLshort);
209 case GL_FIXED: return sizeof(GLfixed);
210 case GL_FLOAT: return sizeof(GLfloat);
daniel@transgaming.com9efa6f62010-03-16 06:23:20 +0000211 default: UNREACHABLE(); return sizeof(GLfloat);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000212 }
213}
214
215std::size_t VertexDataManager::interpretGlStride(const AttributeState &attrib) const
216{
217 return attrib.mStride ? attrib.mStride : typeSize(attrib.mType) * attrib.mSize;
218}
219
220// Round up x (>=0) to the next multiple of multiple (>0).
221// 0 rounds up to 0.
222std::size_t VertexDataManager::roundUp(std::size_t x, std::size_t multiple) const
223{
224 ASSERT(x >= 0);
225 ASSERT(multiple > 0);
226
227 std::size_t remainder = x % multiple;
228 if (remainder != 0)
229 {
230 return x + multiple - remainder;
231 }
232 else
233 {
234 return x;
235 }
236}
237
238std::size_t VertexDataManager::spaceRequired(const AttributeState &attrib, std::size_t maxVertex) const
239{
240 std::size_t size = mBackend->getFormatConverter(attrib.mType, attrib.mSize, attrib.mNormalized).outputVertexSize;
241 size *= maxVertex;
242
243 return roundUp(size, 4 * sizeof(GLfloat));
244}
245
246void VertexDataManager::processNonArrayAttributes(const AttributeState *attribs, const std::bitset<MAX_VERTEX_ATTRIBS> &activeAttribs, TranslatedAttribute *translated)
247{
248 bool usesCurrentValues = false;
249
250 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
251 {
252 if (activeAttribs[i] && !attribs[i].mEnabled)
253 {
254 usesCurrentValues = true;
255 break;
256 }
257 }
258
259 if (usesCurrentValues)
260 {
261 std::size_t currentValueOffset;
262
263 reloadCurrentValues(attribs, &currentValueOffset);
264
265 for (std::size_t i = 0; i < MAX_VERTEX_ATTRIBS; i++)
266 {
267 if (activeAttribs[i] && !attribs[i].mEnabled)
268 {
269 translated[i].buffer = mCurrentValueBuffer;
270
271 translated[i].type = GL_FLOAT;
272 translated[i].size = 4;
273 translated[i].normalized = false;
274 translated[i].stride = 0;
275 translated[i].offset = currentValueOffset + 4 * sizeof(float) * i;
276 }
277 }
278 }
279}
280
281}