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