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 | |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 12 | #include <limits> |
| 13 | |
alokp@chromium.org | ea0e1af | 2010-03-22 19:33:14 +0000 | [diff] [blame] | 14 | #include "common/debug.h" |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 15 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 16 | #include "libGLESv2/Buffer.h" |
| 17 | #include "libGLESv2/Program.h" |
| 18 | |
| 19 | #include "libGLESv2/geometry/backend.h" |
| 20 | #include "libGLESv2/geometry/IndexDataManager.h" |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 21 | |
| 22 | namespace |
| 23 | { |
| 24 | enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 }; |
| 25 | } |
| 26 | |
| 27 | namespace gl |
| 28 | { |
| 29 | |
| 30 | VertexDataManager::VertexDataManager(Context *context, BufferBackEnd *backend) |
daniel@transgaming.com | 97bffae | 2010-05-05 18:49:44 +0000 | [diff] [blame] | 31 | : mContext(context), mBackend(backend), mDirtyCurrentValues(true), mCurrentValueOffset(0) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 32 | { |
| 33 | mStreamBuffer = mBackend->createVertexBuffer(INITIAL_STREAM_BUFFER_SIZE); |
| 34 | try |
| 35 | { |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 36 | mCurrentValueBuffer = mBackend->createVertexBufferForStrideZero(4 * sizeof(float) * MAX_VERTEX_ATTRIBS); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 37 | } |
| 38 | catch (...) |
| 39 | { |
| 40 | delete mStreamBuffer; |
| 41 | throw; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | VertexDataManager::~VertexDataManager() |
| 46 | { |
| 47 | delete mStreamBuffer; |
| 48 | delete mCurrentValueBuffer; |
| 49 | } |
| 50 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 51 | std::bitset<MAX_VERTEX_ATTRIBS> VertexDataManager::getActiveAttribs() const |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 52 | { |
| 53 | std::bitset<MAX_VERTEX_ATTRIBS> active; |
| 54 | |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame] | 55 | Program *program = mContext->getCurrentProgram(); |
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 | for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 58 | { |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame] | 59 | active[attributeIndex] = (program->getSemanticIndex(attributeIndex) != -1); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return active; |
| 63 | } |
| 64 | |
| 65 | GLenum VertexDataManager::preRenderValidate(GLint start, GLsizei count, |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 66 | TranslatedAttribute *translated) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 67 | { |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame^] | 68 | GLenum error = GL_NO_ERROR; |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 69 | const AttributeState *attribs = mContext->getVertexAttribBlock(); |
| 70 | const std::bitset<MAX_VERTEX_ATTRIBS> activeAttribs = getActiveAttribs(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 71 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 72 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 73 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 74 | translated[i].enabled = activeAttribs[i]; |
| 75 | } |
| 76 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 77 | bool usesCurrentValues = false; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 78 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 79 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 80 | { |
| 81 | if (activeAttribs[i] && !attribs[i].mEnabled) |
| 82 | { |
| 83 | usesCurrentValues = true; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Handle the identity-mapped attributes. |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 89 | // Process array attributes. |
| 90 | |
| 91 | std::size_t requiredSpace = 0; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 92 | |
| 93 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 94 | { |
| 95 | if (activeAttribs[i] && attribs[i].mEnabled) |
| 96 | { |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 97 | requiredSpace += spaceRequired(attribs[i], count); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 101 | if (requiredSpace > mStreamBuffer->size()) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 102 | { |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 103 | 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. |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 104 | |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 105 | TranslatedVertexBuffer *newStreamBuffer = mBackend->createVertexBuffer(newSize); |
| 106 | |
| 107 | delete mStreamBuffer; |
| 108 | mStreamBuffer = newStreamBuffer; |
| 109 | } |
| 110 | |
| 111 | mStreamBuffer->reserveSpace(requiredSpace); |
| 112 | |
| 113 | for (size_t i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 114 | { |
| 115 | if (activeAttribs[i] && attribs[i].mEnabled) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 116 | { |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 117 | FormatConverter formatConverter = mBackend->getFormatConverter(attribs[i].mType, attribs[i].mSize, attribs[i].mNormalized); |
| 118 | |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 119 | translated[i].nonArray = false; |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 120 | translated[i].type = attribs[i].mType; |
| 121 | translated[i].size = attribs[i].mSize; |
| 122 | translated[i].normalized = attribs[i].mNormalized; |
| 123 | translated[i].stride = formatConverter.outputVertexSize; |
| 124 | translated[i].buffer = mStreamBuffer; |
| 125 | |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 126 | size_t inputStride = interpretGlStride(attribs[i]); |
| 127 | size_t elementSize = typeSize(attribs[i].mType) * attribs[i].mSize; |
| 128 | |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 129 | void *output = mStreamBuffer->map(spaceRequired(attribs[i], count), &translated[i].offset); |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame^] | 130 | if (output == NULL) |
| 131 | { |
| 132 | ERR(" failed to map vertex buffer."); |
| 133 | return GL_OUT_OF_MEMORY; |
| 134 | } |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 135 | |
| 136 | const void *input; |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 137 | if (attribs[i].mBoundBuffer.get()) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 138 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 139 | Buffer *buffer = attribs[i].mBoundBuffer.get(); |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 140 | |
| 141 | size_t offset = reinterpret_cast<size_t>(attribs[i].mPointer); |
| 142 | |
| 143 | // Before we calculate the required size below, make sure it can be computed without integer overflow. |
| 144 | if (std::numeric_limits<std::size_t>::max() - start < static_cast<std::size_t>(count) |
| 145 | || std::numeric_limits<std::size_t>::max() / inputStride < static_cast<std::size_t>(start + count - 1) // it's a prerequisite that count >= 1, so start+count-1 >= 0. |
| 146 | || std::numeric_limits<std::size_t>::max() - offset < inputStride * (start + count - 1) |
| 147 | || std::numeric_limits<std::size_t>::max() - elementSize < offset + inputStride * (start + count - 1) + elementSize) |
| 148 | { |
| 149 | mStreamBuffer->unmap(); |
| 150 | return GL_INVALID_OPERATION; |
| 151 | } |
| 152 | |
| 153 | if (offset + inputStride * (start + count - 1) + elementSize > buffer->size()) |
| 154 | { |
| 155 | mStreamBuffer->unmap(); |
| 156 | return GL_INVALID_OPERATION; |
| 157 | } |
| 158 | |
| 159 | input = static_cast<const char*>(buffer->data()) + offset; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 160 | } |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 161 | else |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 162 | { |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 163 | input = attribs[i].mPointer; |
| 164 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 165 | |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 166 | input = static_cast<const char*>(input) + inputStride * start; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 167 | |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 168 | if (formatConverter.identity && inputStride == elementSize) |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 169 | { |
| 170 | memcpy(output, input, count * inputStride); |
| 171 | } |
| 172 | else |
| 173 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 174 | formatConverter.convertArray(input, inputStride, count, output); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 175 | } |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 176 | |
| 177 | mStreamBuffer->unmap(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 181 | if (usesCurrentValues) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 182 | { |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame^] | 183 | error = processNonArrayAttributes(attribs, activeAttribs, translated, count); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 184 | } |
daniel@transgaming.com | 97bffae | 2010-05-05 18:49:44 +0000 | [diff] [blame] | 185 | |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame^] | 186 | return error; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | std::size_t VertexDataManager::typeSize(GLenum type) const |
| 190 | { |
| 191 | switch (type) |
| 192 | { |
| 193 | case GL_BYTE: case GL_UNSIGNED_BYTE: return sizeof(GLbyte); |
| 194 | case GL_SHORT: case GL_UNSIGNED_SHORT: return sizeof(GLshort); |
| 195 | case GL_FIXED: return sizeof(GLfixed); |
| 196 | case GL_FLOAT: return sizeof(GLfloat); |
daniel@transgaming.com | 9efa6f6 | 2010-03-16 06:23:20 +0000 | [diff] [blame] | 197 | default: UNREACHABLE(); return sizeof(GLfloat); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | std::size_t VertexDataManager::interpretGlStride(const AttributeState &attrib) const |
| 202 | { |
| 203 | return attrib.mStride ? attrib.mStride : typeSize(attrib.mType) * attrib.mSize; |
| 204 | } |
| 205 | |
| 206 | // Round up x (>=0) to the next multiple of multiple (>0). |
| 207 | // 0 rounds up to 0. |
| 208 | std::size_t VertexDataManager::roundUp(std::size_t x, std::size_t multiple) const |
| 209 | { |
| 210 | ASSERT(x >= 0); |
| 211 | ASSERT(multiple > 0); |
| 212 | |
| 213 | std::size_t remainder = x % multiple; |
| 214 | if (remainder != 0) |
| 215 | { |
| 216 | return x + multiple - remainder; |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | return x; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | std::size_t VertexDataManager::spaceRequired(const AttributeState &attrib, std::size_t maxVertex) const |
| 225 | { |
| 226 | std::size_t size = mBackend->getFormatConverter(attrib.mType, attrib.mSize, attrib.mNormalized).outputVertexSize; |
| 227 | size *= maxVertex; |
| 228 | |
| 229 | return roundUp(size, 4 * sizeof(GLfloat)); |
| 230 | } |
| 231 | |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame^] | 232 | GLenum VertexDataManager::processNonArrayAttributes(const AttributeState *attribs, const std::bitset<MAX_VERTEX_ATTRIBS> &activeAttribs, TranslatedAttribute *translated, std::size_t count) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 233 | { |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 234 | if (mDirtyCurrentValues) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 235 | { |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 236 | std::size_t totalSize = 4 * sizeof(float) * MAX_VERTEX_ATTRIBS; |
| 237 | |
| 238 | mCurrentValueBuffer->reserveSpace(totalSize); |
| 239 | |
| 240 | float* currentValues = static_cast<float*>(mCurrentValueBuffer->map(totalSize, &mCurrentValueOffset)); |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame^] | 241 | if (currentValues == NULL) |
| 242 | { |
| 243 | ERR(" failed to map vertex buffer."); |
| 244 | return GL_OUT_OF_MEMORY; |
| 245 | } |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 246 | |
| 247 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 248 | { |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 249 | // This assumes that the GL_FLOATx4 is supported by the back-end. (For D3D9, it is a mandatory format.) |
| 250 | currentValues[i*4+0] = attribs[i].mCurrentValue[0]; |
| 251 | currentValues[i*4+1] = attribs[i].mCurrentValue[1]; |
| 252 | currentValues[i*4+2] = attribs[i].mCurrentValue[2]; |
| 253 | currentValues[i*4+3] = attribs[i].mCurrentValue[3]; |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 254 | } |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 255 | |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 256 | mCurrentValueBuffer->unmap(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 257 | } |
| 258 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 259 | for (std::size_t i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 260 | { |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 261 | if (activeAttribs[i] && !attribs[i].mEnabled) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 262 | { |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 263 | translated[i].nonArray = true; |
| 264 | |
| 265 | translated[i].buffer = mCurrentValueBuffer; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 266 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 267 | translated[i].type = GL_FLOAT; |
| 268 | translated[i].size = 4; |
| 269 | translated[i].normalized = false; |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 270 | translated[i].stride = 0; |
| 271 | translated[i].offset = mCurrentValueOffset + 4 * sizeof(float) * i; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame^] | 274 | |
| 275 | return GL_NO_ERROR; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | } |