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 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 68 | const AttributeState *attribs = mContext->getVertexAttribBlock(); |
| 69 | const std::bitset<MAX_VERTEX_ATTRIBS> activeAttribs = getActiveAttribs(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 70 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 71 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 72 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 73 | translated[i].enabled = activeAttribs[i]; |
| 74 | } |
| 75 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 76 | bool usesCurrentValues = false; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 77 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 78 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 79 | { |
| 80 | if (activeAttribs[i] && !attribs[i].mEnabled) |
| 81 | { |
| 82 | usesCurrentValues = true; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Handle the identity-mapped attributes. |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 88 | // Process array attributes. |
| 89 | |
| 90 | std::size_t requiredSpace = 0; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 91 | |
| 92 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 93 | { |
| 94 | if (activeAttribs[i] && attribs[i].mEnabled) |
| 95 | { |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 96 | requiredSpace += spaceRequired(attribs[i], count); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 100 | if (requiredSpace > mStreamBuffer->size()) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 101 | { |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 102 | 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] | 103 | |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 104 | TranslatedVertexBuffer *newStreamBuffer = mBackend->createVertexBuffer(newSize); |
| 105 | |
| 106 | delete mStreamBuffer; |
| 107 | mStreamBuffer = newStreamBuffer; |
| 108 | } |
| 109 | |
| 110 | mStreamBuffer->reserveSpace(requiredSpace); |
| 111 | |
| 112 | for (size_t i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 113 | { |
| 114 | if (activeAttribs[i] && attribs[i].mEnabled) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 115 | { |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 116 | FormatConverter formatConverter = mBackend->getFormatConverter(attribs[i].mType, attribs[i].mSize, attribs[i].mNormalized); |
| 117 | |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 118 | translated[i].nonArray = false; |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 119 | translated[i].type = attribs[i].mType; |
| 120 | translated[i].size = attribs[i].mSize; |
| 121 | translated[i].normalized = attribs[i].mNormalized; |
| 122 | translated[i].stride = formatConverter.outputVertexSize; |
| 123 | translated[i].buffer = mStreamBuffer; |
| 124 | |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 125 | size_t inputStride = interpretGlStride(attribs[i]); |
| 126 | size_t elementSize = typeSize(attribs[i].mType) * attribs[i].mSize; |
| 127 | |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 128 | void *output = mStreamBuffer->map(spaceRequired(attribs[i], count), &translated[i].offset); |
| 129 | |
| 130 | const void *input; |
| 131 | if (attribs[i].mBoundBuffer) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 132 | { |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 133 | Buffer *buffer = mContext->getBuffer(attribs[i].mBoundBuffer); |
| 134 | |
| 135 | size_t offset = reinterpret_cast<size_t>(attribs[i].mPointer); |
| 136 | |
| 137 | // Before we calculate the required size below, make sure it can be computed without integer overflow. |
| 138 | if (std::numeric_limits<std::size_t>::max() - start < static_cast<std::size_t>(count) |
| 139 | || 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. |
| 140 | || std::numeric_limits<std::size_t>::max() - offset < inputStride * (start + count - 1) |
| 141 | || std::numeric_limits<std::size_t>::max() - elementSize < offset + inputStride * (start + count - 1) + elementSize) |
| 142 | { |
| 143 | mStreamBuffer->unmap(); |
| 144 | return GL_INVALID_OPERATION; |
| 145 | } |
| 146 | |
| 147 | if (offset + inputStride * (start + count - 1) + elementSize > buffer->size()) |
| 148 | { |
| 149 | mStreamBuffer->unmap(); |
| 150 | return GL_INVALID_OPERATION; |
| 151 | } |
| 152 | |
| 153 | input = static_cast<const char*>(buffer->data()) + offset; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 154 | } |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 155 | else |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 156 | { |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 157 | input = attribs[i].mPointer; |
| 158 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 159 | |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 160 | input = static_cast<const char*>(input) + inputStride * start; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 161 | |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 162 | if (formatConverter.identity && inputStride == elementSize) |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 163 | { |
| 164 | memcpy(output, input, count * inputStride); |
| 165 | } |
| 166 | else |
| 167 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 168 | formatConverter.convertArray(input, inputStride, count, output); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 169 | } |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 170 | |
| 171 | mStreamBuffer->unmap(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 175 | if (usesCurrentValues) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 176 | { |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 177 | processNonArrayAttributes(attribs, activeAttribs, translated, count); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 178 | } |
daniel@transgaming.com | 97bffae | 2010-05-05 18:49:44 +0000 | [diff] [blame] | 179 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 180 | return GL_NO_ERROR; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | std::size_t VertexDataManager::typeSize(GLenum type) const |
| 184 | { |
| 185 | switch (type) |
| 186 | { |
| 187 | case GL_BYTE: case GL_UNSIGNED_BYTE: return sizeof(GLbyte); |
| 188 | case GL_SHORT: case GL_UNSIGNED_SHORT: return sizeof(GLshort); |
| 189 | case GL_FIXED: return sizeof(GLfixed); |
| 190 | case GL_FLOAT: return sizeof(GLfloat); |
daniel@transgaming.com | 9efa6f6 | 2010-03-16 06:23:20 +0000 | [diff] [blame] | 191 | default: UNREACHABLE(); return sizeof(GLfloat); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | std::size_t VertexDataManager::interpretGlStride(const AttributeState &attrib) const |
| 196 | { |
| 197 | return attrib.mStride ? attrib.mStride : typeSize(attrib.mType) * attrib.mSize; |
| 198 | } |
| 199 | |
| 200 | // Round up x (>=0) to the next multiple of multiple (>0). |
| 201 | // 0 rounds up to 0. |
| 202 | std::size_t VertexDataManager::roundUp(std::size_t x, std::size_t multiple) const |
| 203 | { |
| 204 | ASSERT(x >= 0); |
| 205 | ASSERT(multiple > 0); |
| 206 | |
| 207 | std::size_t remainder = x % multiple; |
| 208 | if (remainder != 0) |
| 209 | { |
| 210 | return x + multiple - remainder; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | return x; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | std::size_t VertexDataManager::spaceRequired(const AttributeState &attrib, std::size_t maxVertex) const |
| 219 | { |
| 220 | std::size_t size = mBackend->getFormatConverter(attrib.mType, attrib.mSize, attrib.mNormalized).outputVertexSize; |
| 221 | size *= maxVertex; |
| 222 | |
| 223 | return roundUp(size, 4 * sizeof(GLfloat)); |
| 224 | } |
| 225 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 226 | void 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] | 227 | { |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 228 | if (mDirtyCurrentValues) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 229 | { |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 230 | std::size_t totalSize = 4 * sizeof(float) * MAX_VERTEX_ATTRIBS; |
| 231 | |
| 232 | mCurrentValueBuffer->reserveSpace(totalSize); |
| 233 | |
| 234 | float* currentValues = static_cast<float*>(mCurrentValueBuffer->map(totalSize, &mCurrentValueOffset)); |
| 235 | |
| 236 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 237 | { |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 238 | // This assumes that the GL_FLOATx4 is supported by the back-end. (For D3D9, it is a mandatory format.) |
| 239 | currentValues[i*4+0] = attribs[i].mCurrentValue[0]; |
| 240 | currentValues[i*4+1] = attribs[i].mCurrentValue[1]; |
| 241 | currentValues[i*4+2] = attribs[i].mCurrentValue[2]; |
| 242 | currentValues[i*4+3] = attribs[i].mCurrentValue[3]; |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 243 | } |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 244 | |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 245 | mCurrentValueBuffer->unmap(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 246 | } |
| 247 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 248 | for (std::size_t i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 249 | { |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 250 | if (activeAttribs[i] && !attribs[i].mEnabled) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 251 | { |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 252 | translated[i].nonArray = true; |
| 253 | |
| 254 | translated[i].buffer = mCurrentValueBuffer; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 255 | |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 256 | translated[i].type = GL_FLOAT; |
| 257 | translated[i].size = 4; |
| 258 | translated[i].normalized = false; |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 259 | translated[i].stride = 0; |
| 260 | translated[i].offset = mCurrentValueOffset + 4 * sizeof(float) * i; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | } |