daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1 | // |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
daniel@transgaming.com | 8fd34bd | 2011-02-18 02:52:14 +0000 | [diff] [blame] | 7 | // IndexDataManager.cpp: Defines the IndexDataManager, a class that |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 8 | // runs the Buffer translation process for index buffers. |
| 9 | |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 10 | #include "libGLESv2/renderer/IndexDataManager.h" |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 11 | |
| 12 | #include "common/debug.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 13 | |
| 14 | #include "libGLESv2/Buffer.h" |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 15 | #include "libGLESv2/mathutil.h" |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 16 | #include "libGLESv2/main.h" |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 17 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 18 | namespace rx |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 19 | { |
| 20 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 21 | IndexDataManager::IndexDataManager(rx::Renderer9 *renderer) : mRenderer(renderer) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 22 | { |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 23 | mStreamingBufferShort = new StreamingIndexBufferInterface(mRenderer, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16); |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 24 | |
daniel@transgaming.com | 408caa5 | 2012-10-31 18:47:01 +0000 | [diff] [blame] | 25 | if (renderer->get32BitIndexSupport()) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 26 | { |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 27 | mStreamingBufferInt = new StreamingIndexBufferInterface(mRenderer, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 28 | |
| 29 | if (!mStreamingBufferInt) |
| 30 | { |
| 31 | // Don't leave it in a half-initialized state |
| 32 | delete mStreamingBufferShort; |
| 33 | mStreamingBufferShort = NULL; |
| 34 | } |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 35 | } |
| 36 | else |
| 37 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 38 | mStreamingBufferInt = NULL; |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 39 | } |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 40 | |
| 41 | if (!mStreamingBufferShort) |
| 42 | { |
| 43 | ERR("Failed to allocate the streaming index buffer(s)."); |
| 44 | } |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 45 | |
| 46 | mCountingBuffer = NULL; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | IndexDataManager::~IndexDataManager() |
| 50 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 51 | delete mStreamingBufferShort; |
| 52 | delete mStreamingBufferInt; |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 53 | delete mCountingBuffer; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 54 | } |
| 55 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 56 | void convertIndices(GLenum type, const void *input, GLsizei count, void *output) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 57 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 58 | if (type == GL_UNSIGNED_BYTE) |
| 59 | { |
| 60 | const GLubyte *in = static_cast<const GLubyte*>(input); |
| 61 | GLushort *out = static_cast<GLushort*>(output); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 62 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 63 | for (GLsizei i = 0; i < count; i++) |
| 64 | { |
| 65 | out[i] = in[i]; |
| 66 | } |
| 67 | } |
| 68 | else if (type == GL_UNSIGNED_INT) |
| 69 | { |
| 70 | memcpy(output, input, count * sizeof(GLuint)); |
| 71 | } |
| 72 | else if (type == GL_UNSIGNED_SHORT) |
| 73 | { |
| 74 | memcpy(output, input, count * sizeof(GLushort)); |
| 75 | } |
| 76 | else UNREACHABLE(); |
| 77 | } |
| 78 | |
| 79 | template <class IndexType> |
| 80 | void computeRange(const IndexType *indices, GLsizei count, GLuint *minIndex, GLuint *maxIndex) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 81 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 82 | *minIndex = indices[0]; |
| 83 | *maxIndex = indices[0]; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 84 | |
| 85 | for (GLsizei i = 0; i < count; i++) |
| 86 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 87 | if (*minIndex > indices[i]) *minIndex = indices[i]; |
| 88 | if (*maxIndex < indices[i]) *maxIndex = indices[i]; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 89 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 90 | } |
| 91 | |
daniel@transgaming.com | d2820bf | 2012-01-27 15:38:48 +0000 | [diff] [blame] | 92 | void computeRange(GLenum type, const GLvoid *indices, GLsizei count, GLuint *minIndex, GLuint *maxIndex) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 93 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 94 | if (type == GL_UNSIGNED_BYTE) |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 95 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 96 | computeRange(static_cast<const GLubyte*>(indices), count, minIndex, maxIndex); |
| 97 | } |
| 98 | else if (type == GL_UNSIGNED_INT) |
| 99 | { |
| 100 | computeRange(static_cast<const GLuint*>(indices), count, minIndex, maxIndex); |
| 101 | } |
| 102 | else if (type == GL_UNSIGNED_SHORT) |
| 103 | { |
| 104 | computeRange(static_cast<const GLushort*>(indices), count, minIndex, maxIndex); |
| 105 | } |
| 106 | else UNREACHABLE(); |
| 107 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 108 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 109 | GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, gl::Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated, IDirect3DIndexBuffer9 **d3dIndexBuffer, unsigned int *serial) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 110 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 111 | if (!mStreamingBufferShort) |
| 112 | { |
| 113 | return GL_OUT_OF_MEMORY; |
| 114 | } |
| 115 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 116 | D3DFORMAT format = (type == GL_UNSIGNED_INT) ? D3DFMT_INDEX32 : D3DFMT_INDEX16; |
| 117 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
| 118 | bool alignedOffset = false; |
| 119 | |
| 120 | if (buffer != NULL) |
| 121 | { |
| 122 | switch (type) |
| 123 | { |
| 124 | case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break; |
| 125 | case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break; |
| 126 | case GL_UNSIGNED_INT: alignedOffset = (offset % sizeof(GLuint) == 0); break; |
| 127 | default: UNREACHABLE(); alignedOffset = false; |
| 128 | } |
| 129 | |
| 130 | if (typeSize(type) * count + offset > static_cast<std::size_t>(buffer->size())) |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 131 | { |
| 132 | return GL_INVALID_OPERATION; |
| 133 | } |
| 134 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 135 | indices = static_cast<const GLubyte*>(buffer->data()) + offset; |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 136 | } |
| 137 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 138 | StreamingIndexBufferInterface *streamingBuffer = (type == GL_UNSIGNED_INT) ? mStreamingBufferInt : mStreamingBufferShort; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 139 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 140 | StaticIndexBufferInterface *staticBuffer = buffer ? buffer->getStaticIndexBuffer() : NULL; |
| 141 | IndexBufferInterface *indexBuffer = streamingBuffer; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 142 | UINT streamOffset = 0; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 143 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 144 | if (staticBuffer && staticBuffer->lookupType(type) && alignedOffset) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 145 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 146 | indexBuffer = staticBuffer; |
| 147 | streamOffset = staticBuffer->lookupRange(offset, count, &translated->minIndex, &translated->maxIndex); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 148 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 149 | if (streamOffset == -1) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 150 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 151 | streamOffset = (offset / typeSize(type)) * indexSize(format); |
| 152 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
| 153 | staticBuffer->addRange(offset, count, translated->minIndex, translated->maxIndex, streamOffset); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 156 | else |
| 157 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 158 | int convertCount = count; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 159 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 160 | if (staticBuffer) |
| 161 | { |
| 162 | if (staticBuffer->size() == 0 && alignedOffset) |
| 163 | { |
| 164 | indexBuffer = staticBuffer; |
| 165 | convertCount = buffer->size() / typeSize(type); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | buffer->invalidateStaticData(); |
| 170 | staticBuffer = NULL; |
| 171 | } |
| 172 | } |
| 173 | |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 174 | void *output = NULL; |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 175 | |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 176 | if (indexBuffer) |
| 177 | { |
| 178 | indexBuffer->reserveSpace(convertCount * indexSize(format), type); |
| 179 | output = indexBuffer->map(indexSize(format) * convertCount, &streamOffset); |
| 180 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 181 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 182 | if (output == NULL) |
| 183 | { |
| 184 | ERR("Failed to map index buffer."); |
| 185 | return GL_OUT_OF_MEMORY; |
| 186 | } |
| 187 | |
| 188 | convertIndices(type, staticBuffer ? buffer->data() : indices, convertCount, output); |
| 189 | indexBuffer->unmap(); |
| 190 | |
| 191 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
| 192 | |
| 193 | if (staticBuffer) |
| 194 | { |
| 195 | streamOffset = (offset / typeSize(type)) * indexSize(format); |
| 196 | staticBuffer->addRange(offset, count, translated->minIndex, translated->maxIndex, streamOffset); |
| 197 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 198 | } |
| 199 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 200 | *d3dIndexBuffer = indexBuffer->getBuffer(); |
| 201 | *serial = indexBuffer->getSerial(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 202 | translated->startIndex = streamOffset / indexSize(format); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 203 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 204 | if (buffer) |
| 205 | { |
| 206 | buffer->promoteStaticUsage(count * typeSize(type)); |
| 207 | } |
| 208 | |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 209 | return GL_NO_ERROR; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 210 | } |
| 211 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 212 | std::size_t IndexDataManager::indexSize(D3DFORMAT format) const |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 213 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 214 | return (format == D3DFMT_INDEX32) ? sizeof(unsigned int) : sizeof(unsigned short); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 215 | } |
| 216 | |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 217 | std::size_t IndexDataManager::typeSize(GLenum type) const |
| 218 | { |
| 219 | switch (type) |
| 220 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 221 | case GL_UNSIGNED_INT: return sizeof(GLuint); |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 222 | case GL_UNSIGNED_SHORT: return sizeof(GLushort); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 223 | case GL_UNSIGNED_BYTE: return sizeof(GLubyte); |
| 224 | default: UNREACHABLE(); return sizeof(GLushort); |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 228 | StaticIndexBufferInterface *IndexDataManager::getCountingIndices(GLsizei count) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 229 | { |
daniel@transgaming.com | 0f328a7 | 2012-03-05 15:07:50 +0000 | [diff] [blame] | 230 | if (count <= 65536) // 16-bit indices |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 231 | { |
| 232 | const unsigned int spaceNeeded = count * sizeof(unsigned short); |
| 233 | |
| 234 | if (!mCountingBuffer || mCountingBuffer->size() < spaceNeeded) |
| 235 | { |
| 236 | delete mCountingBuffer; |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 237 | mCountingBuffer = new StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 238 | mCountingBuffer->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT); |
| 239 | |
| 240 | UINT offset; |
| 241 | unsigned short *data = static_cast<unsigned short*>(mCountingBuffer->map(spaceNeeded, &offset)); |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 242 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 243 | if (data) |
| 244 | { |
| 245 | for(int i = 0; i < count; i++) |
| 246 | { |
| 247 | data[i] = i; |
| 248 | } |
| 249 | |
| 250 | mCountingBuffer->unmap(); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | else if (mStreamingBufferInt) // 32-bit indices supported |
| 255 | { |
| 256 | const unsigned int spaceNeeded = count * sizeof(unsigned int); |
| 257 | |
| 258 | if (!mCountingBuffer || mCountingBuffer->size() < spaceNeeded) |
| 259 | { |
| 260 | delete mCountingBuffer; |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 261 | mCountingBuffer = new StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 262 | mCountingBuffer->reserveSpace(spaceNeeded, GL_UNSIGNED_INT); |
| 263 | |
| 264 | UINT offset; |
| 265 | unsigned int *data = static_cast<unsigned int*>(mCountingBuffer->map(spaceNeeded, &offset)); |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 266 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 267 | if (data) |
| 268 | { |
| 269 | for(int i = 0; i < count; i++) |
| 270 | { |
| 271 | data[i] = i; |
| 272 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 273 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 274 | mCountingBuffer->unmap(); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | else return NULL; |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 279 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 280 | return mCountingBuffer; |
| 281 | } |
| 282 | |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 283 | } |