daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // geometry/IndexDataManager.cpp: Defines the IndexDataManager, a class that |
| 8 | // runs the Buffer translation process for index buffers. |
| 9 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 10 | #include "libGLESv2/geometry/IndexDataManager.h" |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 11 | |
| 12 | #include "common/debug.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 13 | |
| 14 | #include "libGLESv2/Buffer.h" |
| 15 | #include "libGLESv2/geometry/backend.h" |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 16 | |
| 17 | namespace |
| 18 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 19 | enum { INITIAL_INDEX_BUFFER_SIZE = 4096 * sizeof(GLuint) }; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | namespace gl |
| 23 | { |
| 24 | |
| 25 | IndexDataManager::IndexDataManager(Context *context, BufferBackEnd *backend) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 26 | : mContext(context), mBackend(backend), mIntIndicesSupported(backend->supportIntIndices()) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 27 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 28 | mStreamBufferShort = mBackend->createIndexBuffer(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT); |
| 29 | |
| 30 | if (mIntIndicesSupported) |
| 31 | { |
| 32 | mStreamBufferInt = mBackend->createIndexBuffer(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT); |
| 33 | } |
| 34 | else |
| 35 | { |
| 36 | mStreamBufferInt = NULL; |
| 37 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | IndexDataManager::~IndexDataManager() |
| 41 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 42 | delete mStreamBufferShort; |
| 43 | delete mStreamBufferInt; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | namespace |
| 47 | { |
| 48 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 49 | template <class InputIndexType, class OutputIndexType> |
| 50 | void copyIndices(const InputIndexType *in, GLsizei count, OutputIndexType *out, GLuint *minIndex, GLuint *maxIndex) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 51 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 52 | InputIndexType first = *in; |
daniel@transgaming.com | bf2b52a | 2010-04-20 18:53:03 +0000 | [diff] [blame] | 53 | GLuint minIndexSoFar = first; |
| 54 | GLuint maxIndexSoFar = first; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 55 | |
| 56 | for (GLsizei i = 0; i < count; i++) |
| 57 | { |
| 58 | if (minIndexSoFar > *in) minIndexSoFar = *in; |
| 59 | if (maxIndexSoFar < *in) maxIndexSoFar = *in; |
| 60 | |
| 61 | *out++ = *in++; |
| 62 | } |
| 63 | |
daniel@transgaming.com | bf2b52a | 2010-04-20 18:53:03 +0000 | [diff] [blame] | 64 | // It might be a line loop, so copy the loop index. |
| 65 | *out = first; |
| 66 | |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 67 | *minIndex = minIndexSoFar; |
| 68 | *maxIndex = maxIndexSoFar; |
| 69 | } |
| 70 | |
| 71 | } |
| 72 | |
| 73 | TranslatedIndexData IndexDataManager::preRenderValidate(GLenum mode, GLenum type, GLsizei count, Buffer *arrayElementBuffer, const void *indices) |
| 74 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 75 | ASSERT(type == GL_UNSIGNED_SHORT || type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 76 | ASSERT(count > 0); |
| 77 | |
| 78 | TranslatedIndexData translated; |
| 79 | |
| 80 | translated.count = count; |
| 81 | |
daniel@transgaming.com | bf2b52a | 2010-04-20 18:53:03 +0000 | [diff] [blame] | 82 | std::size_t requiredSpace = spaceRequired(type, count); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 83 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 84 | TranslatedIndexBuffer *streamIb = prepareIndexBuffer(type, requiredSpace); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 85 | |
| 86 | size_t offset; |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 87 | void *output = streamIb->map(requiredSpace, &offset); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 88 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 89 | translated.buffer = streamIb; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 90 | translated.offset = offset; |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 91 | translated.indexSize = indexSize(type); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 92 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 93 | translated.indices = output; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 94 | |
| 95 | if (arrayElementBuffer != NULL) |
| 96 | { |
| 97 | indices = static_cast<const GLubyte*>(arrayElementBuffer->data()) + reinterpret_cast<GLsizei>(indices); |
| 98 | } |
| 99 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 100 | if (type == GL_UNSIGNED_BYTE) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 101 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 102 | const GLubyte *in = static_cast<const GLubyte*>(indices); |
| 103 | GLushort *out = static_cast<GLushort*>(output); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 104 | |
| 105 | copyIndices(in, count, out, &translated.minIndex, &translated.maxIndex); |
| 106 | } |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 107 | else if (type == GL_UNSIGNED_INT) |
| 108 | { |
| 109 | const GLuint *in = static_cast<const GLuint*>(indices); |
| 110 | |
| 111 | if (mIntIndicesSupported) |
| 112 | { |
| 113 | GLuint *out = static_cast<GLuint*>(output); |
| 114 | |
| 115 | copyIndices(in, count, out, &translated.minIndex, &translated.maxIndex); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | // When 32-bit indices are unsupported, fake them by truncating to 16-bit. |
| 120 | |
| 121 | GLushort *out = static_cast<GLushort*>(output); |
| 122 | |
| 123 | copyIndices(in, count, out, &translated.minIndex, &translated.maxIndex); |
| 124 | } |
| 125 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 126 | else |
| 127 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 128 | const GLushort *in = static_cast<const GLushort*>(indices); |
| 129 | GLushort *out = static_cast<GLushort*>(output); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 130 | |
| 131 | copyIndices(in, count, out, &translated.minIndex, &translated.maxIndex); |
| 132 | } |
| 133 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 134 | streamIb->unmap(); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 135 | |
| 136 | return translated; |
| 137 | } |
| 138 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 139 | std::size_t IndexDataManager::indexSize(GLenum type) const |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 140 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 141 | return (type == GL_UNSIGNED_INT && mIntIndicesSupported) ? sizeof(GLuint) : sizeof(GLushort); |
| 142 | } |
| 143 | |
| 144 | std::size_t IndexDataManager::spaceRequired(GLenum type, GLsizei count) const |
| 145 | { |
| 146 | return (count + 1) * indexSize(type); // +1 because we always leave an extra for line loops |
| 147 | } |
| 148 | |
| 149 | TranslatedIndexBuffer *IndexDataManager::prepareIndexBuffer(GLenum type, std::size_t requiredSpace) |
| 150 | { |
| 151 | bool use32 = (type == GL_UNSIGNED_INT && mIntIndicesSupported); |
| 152 | |
| 153 | TranslatedIndexBuffer *streamIb = use32 ? mStreamBufferInt : mStreamBufferShort; |
| 154 | |
| 155 | if (requiredSpace > streamIb->size()) |
| 156 | { |
| 157 | std::size_t newSize = std::max(requiredSpace, 2 * streamIb->size()); |
| 158 | |
| 159 | TranslatedIndexBuffer *newStreamBuffer = mBackend->createIndexBuffer(newSize, use32 ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT); |
| 160 | |
| 161 | delete streamIb; |
| 162 | |
| 163 | streamIb = newStreamBuffer; |
| 164 | |
| 165 | if (use32) |
| 166 | { |
| 167 | mStreamBufferInt = streamIb; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | mStreamBufferShort = streamIb; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | streamIb->reserveSpace(requiredSpace); |
| 176 | |
| 177 | return streamIb; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | } |