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