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