blob: c11febac83fd79966d6ccaae7ad51939b1a6bab6 [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
10#include "geometry/VertexDataManager.h"
11
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000012#include "common/debug.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000013#include "Program.h"
14
15#include "Buffer.h"
16#include "geometry/backend.h"
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000017#include "geometry/IndexDataManager.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000018
19namespace
20{
21 enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
22}
23
24namespace gl
25{
26
27VertexDataManager::VertexDataManager(Context *context, BufferBackEnd *backend)
28 : mContext(context), mBackend(backend), mDirtyCurrentValues(true)
29{
30 mStreamBuffer = mBackend->createVertexBuffer(INITIAL_STREAM_BUFFER_SIZE);
31 try
32 {
33 mCurrentValueBuffer = mBackend->createVertexBuffer(4*sizeof(float)*MAX_VERTEX_ATTRIBS);
34 }
35 catch (...)
36 {
37 delete mStreamBuffer;
38 throw;
39 }
40}
41
42VertexDataManager::~VertexDataManager()
43{
44 delete mStreamBuffer;
45 delete mCurrentValueBuffer;
46}
47
48VertexDataManager::ArrayTranslationHelper::ArrayTranslationHelper(GLint first, GLsizei count)
49 : mFirst(first), mCount(count)
50{
51}
52
53void VertexDataManager::ArrayTranslationHelper::translate(const FormatConverter &converter, GLsizei stride, const void *source, void *dest)
54{
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000055 converter.convertArray(source, stride, mCount, dest);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000056}
57
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000058VertexDataManager::IndexedTranslationHelper::IndexedTranslationHelper(const Index *indices, Index minIndex, GLsizei count)
59 : mIndices(indices), mMinIndex(minIndex), mCount(count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000060{
61}
62
63void VertexDataManager::IndexedTranslationHelper::translate(const FormatConverter &converter, GLsizei stride, const void *source, void *dest)
64{
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000065 converter.convertIndexed(source, stride, mMinIndex, mCount, mIndices, dest);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000066}
67
68std::bitset<MAX_VERTEX_ATTRIBS> VertexDataManager::activeAttribs()
69{
70 std::bitset<MAX_VERTEX_ATTRIBS> active;
71
72 Program *p = mContext->getCurrentProgram();
73
74 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
75 {
76 if (p->isActiveAttribute(i))
77 active[i] = true;
78 }
79
80 return active;
81}
82
83GLenum VertexDataManager::preRenderValidate(GLint start, GLsizei count,
84 TranslatedAttribute *outAttribs)
85
86{
87 ArrayTranslationHelper translationHelper(start, count);
88
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000089 return internalPreRenderValidate(mContext->vertexAttribute, activeAttribs(), start, start+count-1, &translationHelper, outAttribs);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000090}
91
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000092GLenum VertexDataManager::preRenderValidate(const TranslatedIndexData &indexInfo,
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000093 TranslatedAttribute *outAttribs)
94
95{
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000096 IndexedTranslationHelper translationHelper(indexInfo.indices, indexInfo.minIndex, indexInfo.count);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000097
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000098 return internalPreRenderValidate(mContext->vertexAttribute, activeAttribs(), indexInfo.minIndex, indexInfo.maxIndex, &translationHelper, outAttribs);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000099}
100
101GLenum VertexDataManager::internalPreRenderValidate(const AttributeState *attribs,
102 const std::bitset<MAX_VERTEX_ATTRIBS> &activeAttribs,
103 Index minIndex,
104 Index maxIndex,
105 TranslationHelper *translator,
106 TranslatedAttribute *outAttribs)
107{
108 std::bitset<MAX_VERTEX_ATTRIBS> translateOrLift;
109
110 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
111 {
112 if (!activeAttribs[i] && attribs[i].mEnabled && attribs[i].mBoundBuffer != 0 && !mContext->getBuffer(attribs[i].mBoundBuffer))
113 return GL_INVALID_OPERATION;
114 }
115
116 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
117
118 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
119 {
120 translated[i].enabled = activeAttribs[i];
121 }
122
123 processNonArrayAttributes(attribs, activeAttribs, translated);
124
125 // Handle the identity-mapped attributes.
126
127 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
128 {
129 if (activeAttribs[i] && attribs[i].mEnabled)
130 {
131 if (attribs[i].mBoundBuffer != 0 && mBackend->getFormatConverter(attribs[i].mType, attribs[i].mSize, attribs[i].mNormalized).identity)
132 {
133 translated[i].type = attribs[i].mType;
134 translated[i].size = attribs[i].mSize;
135 translated[i].normalized = attribs[i].mNormalized;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000136 translated[i].stride = interpretGlStride(attribs[i]);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000137 translated[i].offset = static_cast<std::size_t>(static_cast<const char*>(attribs[i].mPointer) - static_cast<const char*>(NULL)) + translated[i].stride * minIndex;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000138 translated[i].buffer = mContext->getBuffer(attribs[i].mBoundBuffer)->identityBuffer();
139 }
140 else
141 {
142 translateOrLift[i] = true;
143 }
144 }
145 }
146
147 // Handle any attributes needing translation or lifting.
148 if (translateOrLift.any())
149 {
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000150 std::size_t count = maxIndex - minIndex + 1;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000151
152 std::size_t requiredSpace = 0;
153
154 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
155 {
156 if (translateOrLift[i])
157 {
158 requiredSpace += spaceRequired(attribs[i], count);
159 }
160 }
161
162 if (requiredSpace > mStreamBuffer->size())
163 {
164 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.
165
166 TranslatedVertexBuffer *newStreamBuffer = mBackend->createVertexBuffer(newSize);
167
168 delete mStreamBuffer;
169 mStreamBuffer = newStreamBuffer;
170 }
171
172 mStreamBuffer->reserveSpace(requiredSpace);
173
174 for (size_t i = 0; i < MAX_VERTEX_ATTRIBS; i++)
175 {
176 if (translateOrLift[i])
177 {
178 FormatConverter formatConverter = mBackend->getFormatConverter(attribs[i].mType, attribs[i].mSize, attribs[i].mNormalized);
179
180 translated[i].type = attribs[i].mType;
181 translated[i].size = attribs[i].mSize;
182 translated[i].normalized = attribs[i].mNormalized;
183 translated[i].stride = formatConverter.outputVertexSize;
184 translated[i].buffer = mStreamBuffer;
185
186 void *output = mStreamBuffer->map(spaceRequired(attribs[i], count), &translated[i].offset);
187
188 const void *input;
189 if (attribs[i].mBoundBuffer)
190 {
191 input = mContext->getBuffer(attribs[i].mBoundBuffer)->data();
192 input = static_cast<const char*>(input) + reinterpret_cast<size_t>(attribs[i].mPointer);
193 }
194 else
195 {
196 input = attribs[i].mPointer;
197 }
198
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000199 size_t inputStride = interpretGlStride(attribs[i]);
200
201 input = static_cast<const char*>(input) + inputStride * minIndex;
202
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000203 translator->translate(formatConverter, interpretGlStride(attribs[i]), input, output);
204
205 mStreamBuffer->unmap();
206 }
207 }
208 }
209
210 std::copy(translated, translated + MAX_VERTEX_ATTRIBS, outAttribs);
211
212 return GL_NO_ERROR;
213}
214
215void VertexDataManager::reloadCurrentValues(const AttributeState *attribs, std::size_t *offset)
216{
217 if (mDirtyCurrentValues)
218 {
219 std::size_t totalSize = 4 * sizeof(float) * MAX_VERTEX_ATTRIBS;
220
221 mCurrentValueBuffer->reserveSpace(totalSize);
222
223 float* p = static_cast<float*>(mCurrentValueBuffer->map(totalSize, offset));
224
225 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
226 {
227 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.
228 }
229
230 mCurrentValueBuffer->unmap();
231
232 mDirtyCurrentValues = false;
233 }
234}
235
236std::size_t VertexDataManager::typeSize(GLenum type) const
237{
238 switch (type)
239 {
240 case GL_BYTE: case GL_UNSIGNED_BYTE: return sizeof(GLbyte);
241 case GL_SHORT: case GL_UNSIGNED_SHORT: return sizeof(GLshort);
242 case GL_FIXED: return sizeof(GLfixed);
243 case GL_FLOAT: return sizeof(GLfloat);
daniel@transgaming.com9efa6f62010-03-16 06:23:20 +0000244 default: UNREACHABLE(); return sizeof(GLfloat);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000245 }
246}
247
248std::size_t VertexDataManager::interpretGlStride(const AttributeState &attrib) const
249{
250 return attrib.mStride ? attrib.mStride : typeSize(attrib.mType) * attrib.mSize;
251}
252
253// Round up x (>=0) to the next multiple of multiple (>0).
254// 0 rounds up to 0.
255std::size_t VertexDataManager::roundUp(std::size_t x, std::size_t multiple) const
256{
257 ASSERT(x >= 0);
258 ASSERT(multiple > 0);
259
260 std::size_t remainder = x % multiple;
261 if (remainder != 0)
262 {
263 return x + multiple - remainder;
264 }
265 else
266 {
267 return x;
268 }
269}
270
271std::size_t VertexDataManager::spaceRequired(const AttributeState &attrib, std::size_t maxVertex) const
272{
273 std::size_t size = mBackend->getFormatConverter(attrib.mType, attrib.mSize, attrib.mNormalized).outputVertexSize;
274 size *= maxVertex;
275
276 return roundUp(size, 4 * sizeof(GLfloat));
277}
278
279void VertexDataManager::processNonArrayAttributes(const AttributeState *attribs, const std::bitset<MAX_VERTEX_ATTRIBS> &activeAttribs, TranslatedAttribute *translated)
280{
281 bool usesCurrentValues = false;
282
283 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
284 {
285 if (activeAttribs[i] && !attribs[i].mEnabled)
286 {
287 usesCurrentValues = true;
288 break;
289 }
290 }
291
292 if (usesCurrentValues)
293 {
294 std::size_t currentValueOffset;
295
296 reloadCurrentValues(attribs, &currentValueOffset);
297
298 for (std::size_t i = 0; i < MAX_VERTEX_ATTRIBS; i++)
299 {
300 if (activeAttribs[i] && !attribs[i].mEnabled)
301 {
302 translated[i].buffer = mCurrentValueBuffer;
303
304 translated[i].type = GL_FLOAT;
305 translated[i].size = 4;
306 translated[i].normalized = false;
307 translated[i].stride = 0;
308 translated[i].offset = currentValueOffset + 4 * sizeof(float) * i;
309 }
310 }
311 }
312}
313
314}