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) |
daniel@transgaming.com | 97bffae | 2010-05-05 18:49:44 +0000 | [diff] [blame] | 29 | : mContext(context), mBackend(backend), mDirtyCurrentValues(true), mCurrentValueOffset(0) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame^] | 49 | std::bitset<MAX_VERTEX_ATTRIBS> VertexDataManager::getActiveAttribs() const |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 50 | { |
| 51 | std::bitset<MAX_VERTEX_ATTRIBS> active; |
| 52 | |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame] | 53 | Program *program = mContext->getCurrentProgram(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 54 | |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame] | 55 | for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 56 | { |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame] | 57 | active[attributeIndex] = (program->getSemanticIndex(attributeIndex) != -1); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | return active; |
| 61 | } |
| 62 | |
| 63 | GLenum VertexDataManager::preRenderValidate(GLint start, GLsizei count, |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame^] | 64 | TranslatedAttribute *translated) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 65 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame^] | 66 | const AttributeState *attribs = mContext->getVertexAttribBlock(); |
| 67 | const std::bitset<MAX_VERTEX_ATTRIBS> activeAttribs = getActiveAttribs(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 68 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 69 | 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.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 77 | 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.com | aa1ff87 | 2010-04-15 20:44:58 +0000 | [diff] [blame] | 92 | std::size_t stride = interpretGlStride(attribs[i]); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame^] | 93 | 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.com | aa1ff87 | 2010-04-15 20:44:58 +0000 | [diff] [blame] | 94 | |
| 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.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 108 | } |
| 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.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 119 | 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.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 166 | size_t inputStride = interpretGlStride(attribs[i]); |
| 167 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame^] | 168 | input = static_cast<const char*>(input) + inputStride * start; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 169 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame^] | 170 | formatConverter.convertArray(input, inputStride, count, output); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 171 | |
| 172 | mStreamBuffer->unmap(); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 177 | return GL_NO_ERROR; |
| 178 | } |
| 179 | |
| 180 | void 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.com | 97bffae | 2010-05-05 18:49:44 +0000 | [diff] [blame] | 188 | float* p = static_cast<float*>(mCurrentValueBuffer->map(totalSize, &mCurrentValueOffset)); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 189 | |
| 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.com | 97bffae | 2010-05-05 18:49:44 +0000 | [diff] [blame] | 199 | |
| 200 | *offset = mCurrentValueOffset; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | std::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.com | 9efa6f6 | 2010-03-16 06:23:20 +0000 | [diff] [blame] | 211 | default: UNREACHABLE(); return sizeof(GLfloat); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | std::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. |
| 222 | std::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 | |
| 238 | std::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 | |
| 246 | void 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, ¤tValueOffset); |
| 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 | } |