shannon.woods@transgaming.com | bdf2d80 | 2013-02-28 23:16:20 +0000 | [diff] [blame] | 1 | #include "precompiled.h" |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 2 | // |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 3 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 4 | // Use of this source code is governed by a BSD-style license that can be |
| 5 | // found in the LICENSE file. |
| 6 | // |
| 7 | |
daniel@transgaming.com | 8fd34bd | 2011-02-18 02:52:14 +0000 | [diff] [blame] | 8 | // IndexDataManager.cpp: Defines the IndexDataManager, a class that |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 9 | // runs the Buffer translation process for index buffers. |
| 10 | |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 11 | #include "libGLESv2/renderer/IndexDataManager.h" |
shannon.woods@transgaming.com | d2811d6 | 2013-02-28 23:11:19 +0000 | [diff] [blame] | 12 | #include "libGLESv2/renderer/BufferStorage.h" |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 13 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 14 | #include "libGLESv2/Buffer.h" |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 15 | #include "libGLESv2/main.h" |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 16 | #include "libGLESv2/formatutils.h" |
shannon.woods@transgaming.com | 486d9e9 | 2013-02-28 23:15:41 +0000 | [diff] [blame] | 17 | #include "libGLESv2/renderer/IndexBuffer.h" |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 18 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 19 | namespace rx |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 20 | { |
| 21 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 22 | IndexDataManager::IndexDataManager(Renderer *renderer) : mRenderer(renderer) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 23 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 24 | mStreamingBufferShort = new StreamingIndexBufferInterface(mRenderer); |
| 25 | if (!mStreamingBufferShort->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT)) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 26 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 27 | delete mStreamingBufferShort; |
| 28 | mStreamingBufferShort = NULL; |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 29 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 30 | |
| 31 | mStreamingBufferInt = new StreamingIndexBufferInterface(mRenderer); |
| 32 | if (!mStreamingBufferInt->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 33 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 34 | delete mStreamingBufferInt; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 35 | mStreamingBufferInt = NULL; |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 36 | } |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 37 | |
| 38 | if (!mStreamingBufferShort) |
| 39 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 40 | // Make sure both buffers are deleted. |
| 41 | delete mStreamingBufferInt; |
| 42 | mStreamingBufferInt = NULL; |
| 43 | |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 44 | ERR("Failed to allocate the streaming index buffer(s)."); |
| 45 | } |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 46 | |
| 47 | mCountingBuffer = NULL; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | IndexDataManager::~IndexDataManager() |
| 51 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 52 | delete mStreamingBufferShort; |
| 53 | delete mStreamingBufferInt; |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 54 | delete mCountingBuffer; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 55 | } |
| 56 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 57 | static void convertIndices(GLenum type, const void *input, GLsizei count, void *output) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 58 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 59 | if (type == GL_UNSIGNED_BYTE) |
| 60 | { |
| 61 | const GLubyte *in = static_cast<const GLubyte*>(input); |
| 62 | GLushort *out = static_cast<GLushort*>(output); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 63 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 64 | for (GLsizei i = 0; i < count; i++) |
| 65 | { |
| 66 | out[i] = in[i]; |
| 67 | } |
| 68 | } |
| 69 | else if (type == GL_UNSIGNED_INT) |
| 70 | { |
| 71 | memcpy(output, input, count * sizeof(GLuint)); |
| 72 | } |
| 73 | else if (type == GL_UNSIGNED_SHORT) |
| 74 | { |
| 75 | memcpy(output, input, count * sizeof(GLushort)); |
| 76 | } |
| 77 | else UNREACHABLE(); |
| 78 | } |
| 79 | |
| 80 | template <class IndexType> |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 81 | static void computeRange(const IndexType *indices, GLsizei count, GLuint *minIndex, GLuint *maxIndex) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 82 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 83 | *minIndex = indices[0]; |
| 84 | *maxIndex = indices[0]; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 85 | |
| 86 | for (GLsizei i = 0; i < count; i++) |
| 87 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 88 | if (*minIndex > indices[i]) *minIndex = indices[i]; |
| 89 | if (*maxIndex < indices[i]) *maxIndex = indices[i]; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 90 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 91 | } |
| 92 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 93 | static 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] | 94 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 95 | if (type == GL_UNSIGNED_BYTE) |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 96 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 97 | computeRange(static_cast<const GLubyte*>(indices), count, minIndex, maxIndex); |
| 98 | } |
| 99 | else if (type == GL_UNSIGNED_INT) |
| 100 | { |
| 101 | computeRange(static_cast<const GLuint*>(indices), count, minIndex, maxIndex); |
| 102 | } |
| 103 | else if (type == GL_UNSIGNED_SHORT) |
| 104 | { |
| 105 | computeRange(static_cast<const GLushort*>(indices), count, minIndex, maxIndex); |
| 106 | } |
| 107 | else UNREACHABLE(); |
| 108 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 109 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 110 | GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, gl::Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 111 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 112 | if (!mStreamingBufferShort) |
| 113 | { |
| 114 | return GL_OUT_OF_MEMORY; |
| 115 | } |
| 116 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 117 | GLenum destinationIndexType = (type == GL_UNSIGNED_INT) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 118 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
| 119 | bool alignedOffset = false; |
| 120 | |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 121 | BufferStorage *storage = NULL; |
| 122 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 123 | if (buffer != NULL) |
| 124 | { |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 125 | storage = buffer->getStorage(); |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 126 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 127 | switch (type) |
| 128 | { |
| 129 | case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break; |
| 130 | case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break; |
| 131 | case GL_UNSIGNED_INT: alignedOffset = (offset % sizeof(GLuint) == 0); break; |
| 132 | default: UNREACHABLE(); alignedOffset = false; |
| 133 | } |
| 134 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 135 | unsigned int typeSize = gl::GetTypeBytes(type); |
| 136 | |
| 137 | // check for interger overflows and underflows |
| 138 | if (static_cast<unsigned int>(offset) > (std::numeric_limits<unsigned int>::max() / typeSize) || |
| 139 | static_cast<unsigned int>(count) > ((std::numeric_limits<unsigned int>::max() / typeSize) - offset)) |
| 140 | { |
| 141 | return GL_OUT_OF_MEMORY; |
| 142 | } |
| 143 | |
| 144 | if (typeSize * static_cast<unsigned int>(count) + offset > storage->getSize()) |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 145 | { |
| 146 | return GL_INVALID_OPERATION; |
| 147 | } |
| 148 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 149 | indices = static_cast<const GLubyte*>(storage->getData()) + offset; |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 150 | } |
| 151 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 152 | StreamingIndexBufferInterface *streamingBuffer = (type == GL_UNSIGNED_INT) ? mStreamingBufferInt : mStreamingBufferShort; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 153 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 154 | StaticIndexBufferInterface *staticBuffer = buffer ? buffer->getStaticIndexBuffer() : NULL; |
| 155 | IndexBufferInterface *indexBuffer = streamingBuffer; |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 156 | bool directStorage = alignedOffset && storage && storage->supportsDirectBinding() && |
| 157 | destinationIndexType == type; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 158 | unsigned int streamOffset = 0; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 159 | |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 160 | if (directStorage) |
| 161 | { |
| 162 | indexBuffer = streamingBuffer; |
| 163 | streamOffset = offset; |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 164 | |
| 165 | if (!buffer->getIndexRangeCache()->findRange(type, offset, count, &translated->minIndex, |
| 166 | &translated->maxIndex, NULL)) |
| 167 | { |
| 168 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
| 169 | buffer->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex, |
| 170 | translated->maxIndex, offset); |
| 171 | } |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 172 | } |
shannon.woods@transgaming.com | 38e8788 | 2013-02-28 23:18:32 +0000 | [diff] [blame] | 173 | else if (staticBuffer && staticBuffer->getBufferSize() != 0 && staticBuffer->getIndexType() == type && alignedOffset) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 174 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 175 | indexBuffer = staticBuffer; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 176 | |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 177 | if (!staticBuffer->getIndexRangeCache()->findRange(type, offset, count, &translated->minIndex, |
| 178 | &translated->maxIndex, &streamOffset)) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 179 | { |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 180 | streamOffset = (offset / gl::GetTypeBytes(type)) * gl::GetTypeBytes(destinationIndexType); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 181 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 182 | staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex, |
| 183 | translated->maxIndex, streamOffset); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 186 | else |
| 187 | { |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 188 | unsigned int convertCount = count; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 189 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 190 | if (staticBuffer) |
| 191 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 192 | if (staticBuffer->getBufferSize() == 0 && alignedOffset) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 193 | { |
| 194 | indexBuffer = staticBuffer; |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 195 | convertCount = storage->getSize() / gl::GetTypeBytes(type); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 196 | } |
| 197 | else |
| 198 | { |
| 199 | buffer->invalidateStaticData(); |
| 200 | staticBuffer = NULL; |
| 201 | } |
| 202 | } |
| 203 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 204 | if (!indexBuffer) |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 205 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 206 | ERR("No valid index buffer."); |
| 207 | return GL_INVALID_OPERATION; |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 208 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 209 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 210 | unsigned int indexTypeSize = gl::GetTypeBytes(destinationIndexType); |
| 211 | if (convertCount > std::numeric_limits<unsigned int>::max() / indexTypeSize) |
| 212 | { |
| 213 | ERR("Reserving %u indicies of %u bytes each exceeds the maximum buffer size.", convertCount, indexTypeSize); |
| 214 | return GL_OUT_OF_MEMORY; |
| 215 | } |
| 216 | |
| 217 | unsigned int bufferSizeRequired = convertCount * indexTypeSize; |
| 218 | if (!indexBuffer->reserveBufferSpace(bufferSizeRequired, type)) |
| 219 | { |
| 220 | ERR("Failed to reserve %u bytes in an index buffer.", bufferSizeRequired); |
| 221 | return GL_OUT_OF_MEMORY; |
| 222 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 223 | |
| 224 | void* output = NULL; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 225 | if (!indexBuffer->mapBuffer(bufferSizeRequired, &output, &streamOffset)) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 226 | { |
| 227 | ERR("Failed to map index buffer."); |
| 228 | return GL_OUT_OF_MEMORY; |
| 229 | } |
| 230 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 231 | convertIndices(type, staticBuffer ? storage->getData() : indices, convertCount, output); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 232 | |
| 233 | if (!indexBuffer->unmapBuffer()) |
| 234 | { |
| 235 | ERR("Failed to unmap index buffer."); |
| 236 | return GL_OUT_OF_MEMORY; |
| 237 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 238 | |
| 239 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
| 240 | |
| 241 | if (staticBuffer) |
| 242 | { |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 243 | streamOffset = (offset / gl::GetTypeBytes(type)) * gl::GetTypeBytes(destinationIndexType); |
| 244 | staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex, |
| 245 | translated->maxIndex, streamOffset); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 246 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 247 | } |
| 248 | |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 249 | translated->storage = directStorage ? storage : NULL; |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 250 | translated->indexBuffer = indexBuffer->getIndexBuffer(); |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 251 | translated->serial = directStorage ? storage->getSerial() : indexBuffer->getSerial(); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 252 | translated->startIndex = streamOffset / gl::GetTypeBytes(destinationIndexType); |
daniel@transgaming.com | 22ada2c | 2013-01-11 04:07:12 +0000 | [diff] [blame] | 253 | translated->startOffset = streamOffset; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 254 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 255 | if (buffer) |
| 256 | { |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 257 | buffer->promoteStaticUsage(count * gl::GetTypeBytes(type)); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 258 | } |
| 259 | |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 260 | return GL_NO_ERROR; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 261 | } |
| 262 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 263 | StaticIndexBufferInterface *IndexDataManager::getCountingIndices(GLsizei count) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 264 | { |
daniel@transgaming.com | 0f328a7 | 2012-03-05 15:07:50 +0000 | [diff] [blame] | 265 | if (count <= 65536) // 16-bit indices |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 266 | { |
| 267 | const unsigned int spaceNeeded = count * sizeof(unsigned short); |
| 268 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 269 | if (!mCountingBuffer || mCountingBuffer->getBufferSize() < spaceNeeded) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 270 | { |
| 271 | delete mCountingBuffer; |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 272 | mCountingBuffer = new StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 273 | mCountingBuffer->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT); |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 274 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 275 | void* mappedMemory = NULL; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 276 | if (!mCountingBuffer->mapBuffer(spaceNeeded, &mappedMemory, NULL)) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 277 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 278 | ERR("Failed to map counting buffer."); |
| 279 | return NULL; |
| 280 | } |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 281 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 282 | unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory); |
| 283 | for(int i = 0; i < count; i++) |
| 284 | { |
| 285 | data[i] = i; |
| 286 | } |
| 287 | |
| 288 | if (!mCountingBuffer->unmapBuffer()) |
| 289 | { |
| 290 | ERR("Failed to unmap counting buffer."); |
| 291 | return NULL; |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |
| 295 | else if (mStreamingBufferInt) // 32-bit indices supported |
| 296 | { |
| 297 | const unsigned int spaceNeeded = count * sizeof(unsigned int); |
| 298 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 299 | if (!mCountingBuffer || mCountingBuffer->getBufferSize() < spaceNeeded) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 300 | { |
| 301 | delete mCountingBuffer; |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 302 | mCountingBuffer = new StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 303 | mCountingBuffer->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT); |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 304 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 305 | void* mappedMemory = NULL; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 306 | if (!mCountingBuffer->mapBuffer(spaceNeeded, &mappedMemory, NULL)) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 307 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 308 | ERR("Failed to map counting buffer."); |
| 309 | return NULL; |
| 310 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 311 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 312 | unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); |
| 313 | for(int i = 0; i < count; i++) |
| 314 | { |
| 315 | data[i] = i; |
| 316 | } |
| 317 | |
| 318 | if (!mCountingBuffer->unmapBuffer()) |
| 319 | { |
| 320 | ERR("Failed to unmap counting buffer."); |
| 321 | return NULL; |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 325 | else |
| 326 | { |
| 327 | return NULL; |
| 328 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 329 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 330 | return mCountingBuffer; |
| 331 | } |
| 332 | |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 333 | } |