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 | |
Brandon Jones | c7a4104 | 2014-06-23 12:03:25 -0700 | [diff] [blame] | 11 | #include "libGLESv2/renderer/d3d/IndexDataManager.h" |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame^] | 12 | #include "libGLESv2/renderer/d3d/BufferD3D.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" |
Brandon Jones | c7a4104 | 2014-06-23 12:03:25 -0700 | [diff] [blame] | 17 | #include "libGLESv2/renderer/d3d/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; |
Geoff Lang | b23fc09 | 2013-08-06 10:43:14 -0400 | [diff] [blame] | 118 | unsigned int offset = 0; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 119 | bool alignedOffset = false; |
| 120 | |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame^] | 121 | BufferD3D *storage = NULL; |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 122 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 123 | if (buffer != NULL) |
| 124 | { |
Geoff Lang | b23fc09 | 2013-08-06 10:43:14 -0400 | [diff] [blame] | 125 | if (reinterpret_cast<uintptr_t>(indices) > std::numeric_limits<unsigned int>::max()) |
| 126 | { |
| 127 | return GL_OUT_OF_MEMORY; |
| 128 | } |
| 129 | offset = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(indices)); |
| 130 | |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame^] | 131 | storage = BufferD3D::makeBufferD3D(buffer->getImplementation()); |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 132 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 133 | switch (type) |
| 134 | { |
| 135 | case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break; |
| 136 | case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break; |
| 137 | case GL_UNSIGNED_INT: alignedOffset = (offset % sizeof(GLuint) == 0); break; |
| 138 | default: UNREACHABLE(); alignedOffset = false; |
| 139 | } |
| 140 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 141 | unsigned int typeSize = gl::GetTypeBytes(type); |
| 142 | |
Geoff Lang | fdf0694 | 2013-08-06 10:41:05 -0400 | [diff] [blame] | 143 | // check for integer overflows |
| 144 | if (static_cast<unsigned int>(count) > (std::numeric_limits<unsigned int>::max() / typeSize) || |
| 145 | typeSize * static_cast<unsigned int>(count) + offset < offset) |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 146 | { |
| 147 | return GL_OUT_OF_MEMORY; |
| 148 | } |
| 149 | |
| 150 | if (typeSize * static_cast<unsigned int>(count) + offset > storage->getSize()) |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 151 | { |
| 152 | return GL_INVALID_OPERATION; |
| 153 | } |
| 154 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 155 | indices = static_cast<const GLubyte*>(storage->getData()) + offset; |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 156 | } |
| 157 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 158 | StreamingIndexBufferInterface *streamingBuffer = (type == GL_UNSIGNED_INT) ? mStreamingBufferInt : mStreamingBufferShort; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 159 | |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame^] | 160 | StaticIndexBufferInterface *staticBuffer = storage ? storage->getStaticIndexBuffer() : NULL; |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 161 | IndexBufferInterface *indexBuffer = streamingBuffer; |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 162 | bool directStorage = alignedOffset && storage && storage->supportsDirectBinding() && |
| 163 | destinationIndexType == type; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 164 | unsigned int streamOffset = 0; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 165 | |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 166 | if (directStorage) |
| 167 | { |
| 168 | indexBuffer = streamingBuffer; |
| 169 | streamOffset = offset; |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 170 | |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame^] | 171 | if (!storage->getIndexRangeCache()->findRange(type, offset, count, &translated->minIndex, |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 172 | &translated->maxIndex, NULL)) |
| 173 | { |
| 174 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame^] | 175 | storage->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex, |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 176 | translated->maxIndex, offset); |
| 177 | } |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 178 | } |
shannon.woods@transgaming.com | 38e8788 | 2013-02-28 23:18:32 +0000 | [diff] [blame] | 179 | else if (staticBuffer && staticBuffer->getBufferSize() != 0 && staticBuffer->getIndexType() == type && alignedOffset) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 180 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 181 | indexBuffer = staticBuffer; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 182 | |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 183 | if (!staticBuffer->getIndexRangeCache()->findRange(type, offset, count, &translated->minIndex, |
| 184 | &translated->maxIndex, &streamOffset)) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 185 | { |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 186 | streamOffset = (offset / gl::GetTypeBytes(type)) * gl::GetTypeBytes(destinationIndexType); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 187 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 188 | staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex, |
| 189 | translated->maxIndex, streamOffset); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 192 | else |
| 193 | { |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 194 | unsigned int convertCount = count; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 195 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 196 | if (staticBuffer) |
| 197 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 198 | if (staticBuffer->getBufferSize() == 0 && alignedOffset) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 199 | { |
| 200 | indexBuffer = staticBuffer; |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 201 | convertCount = storage->getSize() / gl::GetTypeBytes(type); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 202 | } |
| 203 | else |
| 204 | { |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame^] | 205 | storage->invalidateStaticData(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 206 | staticBuffer = NULL; |
| 207 | } |
| 208 | } |
| 209 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 210 | if (!indexBuffer) |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 211 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 212 | ERR("No valid index buffer."); |
| 213 | return GL_INVALID_OPERATION; |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 214 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 215 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 216 | unsigned int indexTypeSize = gl::GetTypeBytes(destinationIndexType); |
| 217 | if (convertCount > std::numeric_limits<unsigned int>::max() / indexTypeSize) |
| 218 | { |
| 219 | ERR("Reserving %u indicies of %u bytes each exceeds the maximum buffer size.", convertCount, indexTypeSize); |
| 220 | return GL_OUT_OF_MEMORY; |
| 221 | } |
| 222 | |
| 223 | unsigned int bufferSizeRequired = convertCount * indexTypeSize; |
| 224 | if (!indexBuffer->reserveBufferSpace(bufferSizeRequired, type)) |
| 225 | { |
| 226 | ERR("Failed to reserve %u bytes in an index buffer.", bufferSizeRequired); |
| 227 | return GL_OUT_OF_MEMORY; |
| 228 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 229 | |
| 230 | void* output = NULL; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 231 | if (!indexBuffer->mapBuffer(bufferSizeRequired, &output, &streamOffset)) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 232 | { |
| 233 | ERR("Failed to map index buffer."); |
| 234 | return GL_OUT_OF_MEMORY; |
| 235 | } |
| 236 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 237 | convertIndices(type, staticBuffer ? storage->getData() : indices, convertCount, output); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 238 | |
| 239 | if (!indexBuffer->unmapBuffer()) |
| 240 | { |
| 241 | ERR("Failed to unmap index buffer."); |
| 242 | return GL_OUT_OF_MEMORY; |
| 243 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 244 | |
| 245 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
| 246 | |
| 247 | if (staticBuffer) |
| 248 | { |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 249 | streamOffset = (offset / gl::GetTypeBytes(type)) * gl::GetTypeBytes(destinationIndexType); |
| 250 | staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex, |
| 251 | translated->maxIndex, streamOffset); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 252 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 253 | } |
| 254 | |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 255 | translated->storage = directStorage ? storage : NULL; |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 256 | translated->indexBuffer = indexBuffer->getIndexBuffer(); |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 257 | translated->serial = directStorage ? storage->getSerial() : indexBuffer->getSerial(); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 258 | translated->startIndex = streamOffset / gl::GetTypeBytes(destinationIndexType); |
daniel@transgaming.com | 22ada2c | 2013-01-11 04:07:12 +0000 | [diff] [blame] | 259 | translated->startOffset = streamOffset; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 260 | |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame^] | 261 | if (storage) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 262 | { |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame^] | 263 | storage->promoteStaticUsage(count * gl::GetTypeBytes(type)); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 264 | } |
| 265 | |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 266 | return GL_NO_ERROR; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 267 | } |
| 268 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 269 | StaticIndexBufferInterface *IndexDataManager::getCountingIndices(GLsizei count) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 270 | { |
daniel@transgaming.com | 0f328a7 | 2012-03-05 15:07:50 +0000 | [diff] [blame] | 271 | if (count <= 65536) // 16-bit indices |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 272 | { |
| 273 | const unsigned int spaceNeeded = count * sizeof(unsigned short); |
| 274 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 275 | if (!mCountingBuffer || mCountingBuffer->getBufferSize() < spaceNeeded) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 276 | { |
| 277 | delete mCountingBuffer; |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 278 | mCountingBuffer = new StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 279 | mCountingBuffer->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT); |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 280 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 281 | void* mappedMemory = NULL; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 282 | if (!mCountingBuffer->mapBuffer(spaceNeeded, &mappedMemory, NULL)) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 283 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 284 | ERR("Failed to map counting buffer."); |
| 285 | return NULL; |
| 286 | } |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 287 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 288 | unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory); |
| 289 | for(int i = 0; i < count; i++) |
| 290 | { |
| 291 | data[i] = i; |
| 292 | } |
| 293 | |
| 294 | if (!mCountingBuffer->unmapBuffer()) |
| 295 | { |
| 296 | ERR("Failed to unmap counting buffer."); |
| 297 | return NULL; |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
| 301 | else if (mStreamingBufferInt) // 32-bit indices supported |
| 302 | { |
| 303 | const unsigned int spaceNeeded = count * sizeof(unsigned int); |
| 304 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 305 | if (!mCountingBuffer || mCountingBuffer->getBufferSize() < spaceNeeded) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 306 | { |
| 307 | delete mCountingBuffer; |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 308 | mCountingBuffer = new StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 309 | mCountingBuffer->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT); |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 310 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 311 | void* mappedMemory = NULL; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 312 | if (!mCountingBuffer->mapBuffer(spaceNeeded, &mappedMemory, NULL)) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 313 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 314 | ERR("Failed to map counting buffer."); |
| 315 | return NULL; |
| 316 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 317 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 318 | unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); |
| 319 | for(int i = 0; i < count; i++) |
| 320 | { |
| 321 | data[i] = i; |
| 322 | } |
| 323 | |
| 324 | if (!mCountingBuffer->unmapBuffer()) |
| 325 | { |
| 326 | ERR("Failed to unmap counting buffer."); |
| 327 | return NULL; |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 331 | else |
| 332 | { |
| 333 | return NULL; |
| 334 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 335 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 336 | return mCountingBuffer; |
| 337 | } |
| 338 | |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 339 | } |