daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1 | // |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 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 | |
Brandon Jones | c7a4104 | 2014-06-23 12:03:25 -0700 | [diff] [blame] | 10 | #include "libGLESv2/renderer/d3d/IndexDataManager.h" |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 11 | #include "libGLESv2/renderer/d3d/BufferD3D.h" |
Geoff Lang | 0b7eef7 | 2014-06-12 14:10:47 -0400 | [diff] [blame^] | 12 | #include "libGLESv2/renderer/d3d/IndexBuffer.h" |
| 13 | #include "libGLESv2/renderer/Renderer.h" |
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" |
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 | |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 21 | static void ConvertIndices(GLenum sourceType, GLenum destinationType, const void *input, GLsizei count, void *output) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 22 | { |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 23 | if (sourceType == GL_UNSIGNED_BYTE) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 24 | { |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 25 | ASSERT(destinationType == GL_UNSIGNED_SHORT); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 26 | const GLubyte *in = static_cast<const GLubyte*>(input); |
| 27 | GLushort *out = static_cast<GLushort*>(output); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 28 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 29 | for (GLsizei i = 0; i < count; i++) |
| 30 | { |
| 31 | out[i] = in[i]; |
| 32 | } |
| 33 | } |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 34 | else if (sourceType == GL_UNSIGNED_INT) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 35 | { |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 36 | ASSERT(destinationType == GL_UNSIGNED_INT); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 37 | memcpy(output, input, count * sizeof(GLuint)); |
| 38 | } |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 39 | else if (sourceType == GL_UNSIGNED_SHORT) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 40 | { |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 41 | if (destinationType == GL_UNSIGNED_SHORT) |
| 42 | { |
| 43 | memcpy(output, input, count * sizeof(GLushort)); |
| 44 | } |
| 45 | else if (destinationType == GL_UNSIGNED_INT) |
| 46 | { |
| 47 | const GLushort *in = static_cast<const GLushort*>(input); |
| 48 | GLuint *out = static_cast<GLuint*>(output); |
| 49 | |
| 50 | for (GLsizei i = 0; i < count; i++) |
| 51 | { |
| 52 | out[i] = in[i]; |
| 53 | } |
| 54 | } |
| 55 | else UNREACHABLE(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 56 | } |
| 57 | else UNREACHABLE(); |
| 58 | } |
| 59 | |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 60 | IndexDataManager::IndexDataManager(Renderer *renderer) |
| 61 | : mRenderer(renderer) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 62 | { |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 63 | mStreamingBufferShort = new StreamingIndexBufferInterface(mRenderer); |
| 64 | if (!mStreamingBufferShort->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT)) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 65 | { |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 66 | SafeDelete(mStreamingBufferShort); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 67 | } |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 68 | |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 69 | mStreamingBufferInt = new StreamingIndexBufferInterface(mRenderer); |
| 70 | if (!mStreamingBufferInt->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) |
| 71 | { |
| 72 | SafeDelete(mStreamingBufferInt); |
| 73 | } |
| 74 | |
| 75 | if (!mStreamingBufferShort) |
| 76 | { |
| 77 | // Make sure both buffers are deleted. |
| 78 | SafeDelete(mStreamingBufferInt); |
| 79 | ERR("Failed to allocate the streaming index buffer(s)."); |
| 80 | } |
| 81 | |
| 82 | mCountingBuffer = NULL; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 85 | IndexDataManager::~IndexDataManager() |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 86 | { |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 87 | SafeDelete(mStreamingBufferShort); |
| 88 | SafeDelete(mStreamingBufferInt); |
| 89 | SafeDelete(mCountingBuffer); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 90 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 91 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 92 | 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] | 93 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 94 | if (!mStreamingBufferShort) |
| 95 | { |
| 96 | return GL_OUT_OF_MEMORY; |
| 97 | } |
| 98 | |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 99 | const gl::Type &typeInfo = gl::GetTypeInfo(type); |
| 100 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 101 | GLenum destinationIndexType = (type == GL_UNSIGNED_INT) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT; |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 102 | |
Geoff Lang | b23fc09 | 2013-08-06 10:43:14 -0400 | [diff] [blame] | 103 | unsigned int offset = 0; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 104 | bool alignedOffset = false; |
| 105 | |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 106 | BufferD3D *storage = NULL; |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 107 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 108 | if (buffer != NULL) |
| 109 | { |
Geoff Lang | b23fc09 | 2013-08-06 10:43:14 -0400 | [diff] [blame] | 110 | if (reinterpret_cast<uintptr_t>(indices) > std::numeric_limits<unsigned int>::max()) |
| 111 | { |
| 112 | return GL_OUT_OF_MEMORY; |
| 113 | } |
| 114 | offset = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(indices)); |
| 115 | |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 116 | storage = BufferD3D::makeBufferD3D(buffer->getImplementation()); |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 117 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 118 | switch (type) |
| 119 | { |
| 120 | case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break; |
| 121 | case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break; |
| 122 | case GL_UNSIGNED_INT: alignedOffset = (offset % sizeof(GLuint) == 0); break; |
| 123 | default: UNREACHABLE(); alignedOffset = false; |
| 124 | } |
| 125 | |
Jamie Madill | ae3000b | 2014-08-25 15:47:51 -0400 | [diff] [blame] | 126 | ASSERT(typeInfo.bytes * static_cast<unsigned int>(count) + offset <= storage->getSize()); |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 127 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 128 | indices = static_cast<const GLubyte*>(storage->getData()) + offset; |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 131 | StaticIndexBufferInterface *staticBuffer = storage ? storage->getStaticIndexBuffer() : NULL; |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 132 | IndexBufferInterface *indexBuffer = NULL; |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 133 | bool directStorage = alignedOffset && storage && storage->supportsDirectBinding() && |
| 134 | destinationIndexType == type; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 135 | unsigned int streamOffset = 0; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 136 | |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 137 | if (directStorage) |
| 138 | { |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 139 | streamOffset = offset; |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 140 | |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 141 | if (!buffer->getIndexRangeCache()->findRange(type, offset, count, NULL, NULL)) |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 142 | { |
Jamie Madill | ef9d63e | 2014-08-04 10:48:02 -0400 | [diff] [blame] | 143 | buffer->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, offset); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 144 | } |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 145 | } |
shannon.woods@transgaming.com | 38e8788 | 2013-02-28 23:18:32 +0000 | [diff] [blame] | 146 | else if (staticBuffer && staticBuffer->getBufferSize() != 0 && staticBuffer->getIndexType() == type && alignedOffset) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 147 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 148 | indexBuffer = staticBuffer; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 149 | |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 150 | if (!staticBuffer->getIndexRangeCache()->findRange(type, offset, count, NULL, &streamOffset)) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 151 | { |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 152 | streamOffset = (offset / typeInfo.bytes) * gl::GetTypeInfo(destinationIndexType).bytes; |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 153 | staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, streamOffset); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 156 | |
| 157 | // Avoid D3D11's primitive restart index value |
| 158 | // see http://msdn.microsoft.com/en-us/library/windows/desktop/bb205124(v=vs.85).aspx |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 159 | if (translated->indexRange.end == 0xFFFF && type == GL_UNSIGNED_SHORT && mRenderer->getMajorShaderModel() > 3) |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 160 | { |
| 161 | destinationIndexType = GL_UNSIGNED_INT; |
| 162 | directStorage = false; |
| 163 | indexBuffer = NULL; |
| 164 | } |
| 165 | |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 166 | const gl::Type &destTypeInfo = gl::GetTypeInfo(destinationIndexType); |
| 167 | |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 168 | if (!directStorage && !indexBuffer) |
| 169 | { |
| 170 | indexBuffer = (destinationIndexType == GL_UNSIGNED_INT) ? mStreamingBufferInt : mStreamingBufferShort; |
| 171 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 172 | unsigned int convertCount = count; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 173 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 174 | if (staticBuffer) |
| 175 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 176 | if (staticBuffer->getBufferSize() == 0 && alignedOffset) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 177 | { |
| 178 | indexBuffer = staticBuffer; |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 179 | convertCount = storage->getSize() / typeInfo.bytes; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 180 | } |
| 181 | else |
| 182 | { |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 183 | storage->invalidateStaticData(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 184 | staticBuffer = NULL; |
| 185 | } |
| 186 | } |
| 187 | |
Jamie Madill | ae3000b | 2014-08-25 15:47:51 -0400 | [diff] [blame] | 188 | ASSERT(indexBuffer); |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 189 | |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 190 | if (convertCount > std::numeric_limits<unsigned int>::max() / destTypeInfo.bytes) |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 191 | { |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 192 | ERR("Reserving %u indicies of %u bytes each exceeds the maximum buffer size.", convertCount, destTypeInfo.bytes); |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 193 | return GL_OUT_OF_MEMORY; |
| 194 | } |
| 195 | |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 196 | unsigned int bufferSizeRequired = convertCount * destTypeInfo.bytes; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 197 | if (!indexBuffer->reserveBufferSpace(bufferSizeRequired, type)) |
| 198 | { |
| 199 | ERR("Failed to reserve %u bytes in an index buffer.", bufferSizeRequired); |
| 200 | return GL_OUT_OF_MEMORY; |
| 201 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 202 | |
| 203 | void* output = NULL; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 204 | if (!indexBuffer->mapBuffer(bufferSizeRequired, &output, &streamOffset)) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 205 | { |
| 206 | ERR("Failed to map index buffer."); |
| 207 | return GL_OUT_OF_MEMORY; |
| 208 | } |
| 209 | |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 210 | ConvertIndices(type, destinationIndexType, staticBuffer ? storage->getData() : indices, convertCount, output); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 211 | |
| 212 | if (!indexBuffer->unmapBuffer()) |
| 213 | { |
| 214 | ERR("Failed to unmap index buffer."); |
| 215 | return GL_OUT_OF_MEMORY; |
| 216 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 217 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 218 | if (staticBuffer) |
| 219 | { |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 220 | streamOffset = (offset / typeInfo.bytes) * destTypeInfo.bytes; |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 221 | staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, streamOffset); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 222 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 223 | } |
| 224 | |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 225 | translated->storage = directStorage ? storage : NULL; |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 226 | translated->indexBuffer = indexBuffer ? indexBuffer->getIndexBuffer() : NULL; |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 227 | translated->serial = directStorage ? storage->getSerial() : indexBuffer->getSerial(); |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 228 | translated->startIndex = streamOffset / destTypeInfo.bytes; |
daniel@transgaming.com | 22ada2c | 2013-01-11 04:07:12 +0000 | [diff] [blame] | 229 | translated->startOffset = streamOffset; |
Nicolas Capens | 8d62bcc | 2014-07-25 15:08:21 -0400 | [diff] [blame] | 230 | translated->indexType = destinationIndexType; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 231 | |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 232 | if (storage) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 233 | { |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 234 | storage->promoteStaticUsage(count * typeInfo.bytes); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 235 | } |
| 236 | |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 237 | return GL_NO_ERROR; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 238 | } |
| 239 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 240 | StaticIndexBufferInterface *IndexDataManager::getCountingIndices(GLsizei count) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 241 | { |
daniel@transgaming.com | 0f328a7 | 2012-03-05 15:07:50 +0000 | [diff] [blame] | 242 | if (count <= 65536) // 16-bit indices |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 243 | { |
| 244 | const unsigned int spaceNeeded = count * sizeof(unsigned short); |
| 245 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 246 | if (!mCountingBuffer || mCountingBuffer->getBufferSize() < spaceNeeded) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 247 | { |
| 248 | delete mCountingBuffer; |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 249 | mCountingBuffer = new StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 250 | mCountingBuffer->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT); |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 251 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 252 | void* mappedMemory = NULL; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 253 | if (!mCountingBuffer->mapBuffer(spaceNeeded, &mappedMemory, NULL)) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 254 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 255 | ERR("Failed to map counting buffer."); |
| 256 | return NULL; |
| 257 | } |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 258 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 259 | unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory); |
| 260 | for(int i = 0; i < count; i++) |
| 261 | { |
| 262 | data[i] = i; |
| 263 | } |
| 264 | |
| 265 | if (!mCountingBuffer->unmapBuffer()) |
| 266 | { |
| 267 | ERR("Failed to unmap counting buffer."); |
| 268 | return NULL; |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | else if (mStreamingBufferInt) // 32-bit indices supported |
| 273 | { |
| 274 | const unsigned int spaceNeeded = count * sizeof(unsigned int); |
| 275 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 276 | if (!mCountingBuffer || mCountingBuffer->getBufferSize() < spaceNeeded) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 277 | { |
| 278 | delete mCountingBuffer; |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 279 | mCountingBuffer = new StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 280 | mCountingBuffer->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT); |
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 | void* mappedMemory = NULL; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 283 | if (!mCountingBuffer->mapBuffer(spaceNeeded, &mappedMemory, NULL)) |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 284 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 285 | ERR("Failed to map counting buffer."); |
| 286 | return NULL; |
| 287 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 288 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 289 | unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); |
| 290 | for(int i = 0; i < count; i++) |
| 291 | { |
| 292 | data[i] = i; |
| 293 | } |
| 294 | |
| 295 | if (!mCountingBuffer->unmapBuffer()) |
| 296 | { |
| 297 | ERR("Failed to unmap counting buffer."); |
| 298 | return NULL; |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 302 | else |
| 303 | { |
| 304 | return NULL; |
| 305 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 306 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 307 | return mCountingBuffer; |
| 308 | } |
| 309 | |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 310 | } |