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" |
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" |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 15 | #include "libGLESv2/mathutil.h" |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 16 | #include "libGLESv2/main.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 | { |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 20 | unsigned int IndexBuffer::mCurrentSerial = 1; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 21 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 22 | IndexDataManager::IndexDataManager(rx::Renderer9 *renderer) : mRenderer(renderer) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 23 | { |
daniel@transgaming.com | 6716a27 | 2012-10-31 18:31:39 +0000 | [diff] [blame] | 24 | mStreamingBufferShort = new StreamingIndexBuffer(mRenderer, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16); |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 25 | |
daniel@transgaming.com | 408caa5 | 2012-10-31 18:47:01 +0000 | [diff] [blame] | 26 | if (renderer->get32BitIndexSupport()) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 27 | { |
daniel@transgaming.com | 6716a27 | 2012-10-31 18:31:39 +0000 | [diff] [blame] | 28 | mStreamingBufferInt = new StreamingIndexBuffer(mRenderer, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 29 | |
| 30 | if (!mStreamingBufferInt) |
| 31 | { |
| 32 | // Don't leave it in a half-initialized state |
| 33 | delete mStreamingBufferShort; |
| 34 | mStreamingBufferShort = NULL; |
| 35 | } |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 36 | } |
| 37 | else |
| 38 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 39 | mStreamingBufferInt = NULL; |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 40 | } |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 41 | |
| 42 | if (!mStreamingBufferShort) |
| 43 | { |
| 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 | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 57 | 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> |
| 81 | 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 | d2820bf | 2012-01-27 15:38:48 +0000 | [diff] [blame] | 93 | 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 | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame^] | 110 | GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, gl::Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated, IDirect3DIndexBuffer9 **d3dIndexBuffer, unsigned int *serial) |
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 | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 117 | D3DFORMAT format = (type == GL_UNSIGNED_INT) ? D3DFMT_INDEX32 : D3DFMT_INDEX16; |
| 118 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
| 119 | bool alignedOffset = false; |
| 120 | |
| 121 | if (buffer != NULL) |
| 122 | { |
| 123 | switch (type) |
| 124 | { |
| 125 | case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break; |
| 126 | case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break; |
| 127 | case GL_UNSIGNED_INT: alignedOffset = (offset % sizeof(GLuint) == 0); break; |
| 128 | default: UNREACHABLE(); alignedOffset = false; |
| 129 | } |
| 130 | |
| 131 | if (typeSize(type) * count + offset > static_cast<std::size_t>(buffer->size())) |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 132 | { |
| 133 | return GL_INVALID_OPERATION; |
| 134 | } |
| 135 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 136 | indices = static_cast<const GLubyte*>(buffer->data()) + offset; |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 137 | } |
| 138 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 139 | StreamingIndexBuffer *streamingBuffer = (type == GL_UNSIGNED_INT) ? mStreamingBufferInt : mStreamingBufferShort; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 140 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 141 | StaticIndexBuffer *staticBuffer = buffer ? buffer->getStaticIndexBuffer() : NULL; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 142 | IndexBuffer *indexBuffer = streamingBuffer; |
| 143 | UINT streamOffset = 0; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 144 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 145 | if (staticBuffer && staticBuffer->lookupType(type) && alignedOffset) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 146 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 147 | indexBuffer = staticBuffer; |
| 148 | streamOffset = staticBuffer->lookupRange(offset, count, &translated->minIndex, &translated->maxIndex); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 149 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 150 | if (streamOffset == -1) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 151 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 152 | streamOffset = (offset / typeSize(type)) * indexSize(format); |
| 153 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
| 154 | staticBuffer->addRange(offset, count, translated->minIndex, translated->maxIndex, streamOffset); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 157 | else |
| 158 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 159 | int convertCount = count; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 160 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 161 | if (staticBuffer) |
| 162 | { |
| 163 | if (staticBuffer->size() == 0 && alignedOffset) |
| 164 | { |
| 165 | indexBuffer = staticBuffer; |
| 166 | convertCount = buffer->size() / typeSize(type); |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | buffer->invalidateStaticData(); |
| 171 | staticBuffer = NULL; |
| 172 | } |
| 173 | } |
| 174 | |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 175 | void *output = NULL; |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 176 | |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 177 | if (indexBuffer) |
| 178 | { |
| 179 | indexBuffer->reserveSpace(convertCount * indexSize(format), type); |
| 180 | output = indexBuffer->map(indexSize(format) * convertCount, &streamOffset); |
| 181 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 182 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 183 | if (output == NULL) |
| 184 | { |
| 185 | ERR("Failed to map index buffer."); |
| 186 | return GL_OUT_OF_MEMORY; |
| 187 | } |
| 188 | |
| 189 | convertIndices(type, staticBuffer ? buffer->data() : indices, convertCount, output); |
| 190 | indexBuffer->unmap(); |
| 191 | |
| 192 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
| 193 | |
| 194 | if (staticBuffer) |
| 195 | { |
| 196 | streamOffset = (offset / typeSize(type)) * indexSize(format); |
| 197 | staticBuffer->addRange(offset, count, translated->minIndex, translated->maxIndex, streamOffset); |
| 198 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 199 | } |
| 200 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 201 | *d3dIndexBuffer = indexBuffer->getBuffer(); |
| 202 | *serial = indexBuffer->getSerial(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 203 | translated->startIndex = streamOffset / indexSize(format); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 204 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 205 | if (buffer) |
| 206 | { |
| 207 | buffer->promoteStaticUsage(count * typeSize(type)); |
| 208 | } |
| 209 | |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 210 | return GL_NO_ERROR; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 211 | } |
| 212 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 213 | std::size_t IndexDataManager::indexSize(D3DFORMAT format) const |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 214 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 215 | return (format == D3DFMT_INDEX32) ? sizeof(unsigned int) : sizeof(unsigned short); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 216 | } |
| 217 | |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 218 | std::size_t IndexDataManager::typeSize(GLenum type) const |
| 219 | { |
| 220 | switch (type) |
| 221 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 222 | case GL_UNSIGNED_INT: return sizeof(GLuint); |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 223 | case GL_UNSIGNED_SHORT: return sizeof(GLushort); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 224 | case GL_UNSIGNED_BYTE: return sizeof(GLubyte); |
| 225 | default: UNREACHABLE(); return sizeof(GLushort); |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 229 | StaticIndexBuffer *IndexDataManager::getCountingIndices(GLsizei count) |
| 230 | { |
daniel@transgaming.com | 0f328a7 | 2012-03-05 15:07:50 +0000 | [diff] [blame] | 231 | if (count <= 65536) // 16-bit indices |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 232 | { |
| 233 | const unsigned int spaceNeeded = count * sizeof(unsigned short); |
| 234 | |
| 235 | if (!mCountingBuffer || mCountingBuffer->size() < spaceNeeded) |
| 236 | { |
| 237 | delete mCountingBuffer; |
daniel@transgaming.com | 6716a27 | 2012-10-31 18:31:39 +0000 | [diff] [blame] | 238 | mCountingBuffer = new StaticIndexBuffer(mRenderer); |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 239 | mCountingBuffer->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT); |
| 240 | |
| 241 | UINT offset; |
| 242 | unsigned short *data = static_cast<unsigned short*>(mCountingBuffer->map(spaceNeeded, &offset)); |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 243 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 244 | if (data) |
| 245 | { |
| 246 | for(int i = 0; i < count; i++) |
| 247 | { |
| 248 | data[i] = i; |
| 249 | } |
| 250 | |
| 251 | mCountingBuffer->unmap(); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | else if (mStreamingBufferInt) // 32-bit indices supported |
| 256 | { |
| 257 | const unsigned int spaceNeeded = count * sizeof(unsigned int); |
| 258 | |
| 259 | if (!mCountingBuffer || mCountingBuffer->size() < spaceNeeded) |
| 260 | { |
| 261 | delete mCountingBuffer; |
daniel@transgaming.com | 6716a27 | 2012-10-31 18:31:39 +0000 | [diff] [blame] | 262 | mCountingBuffer = new StaticIndexBuffer(mRenderer); |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 263 | mCountingBuffer->reserveSpace(spaceNeeded, GL_UNSIGNED_INT); |
| 264 | |
| 265 | UINT offset; |
| 266 | unsigned int *data = static_cast<unsigned int*>(mCountingBuffer->map(spaceNeeded, &offset)); |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 267 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 268 | if (data) |
| 269 | { |
| 270 | for(int i = 0; i < count; i++) |
| 271 | { |
| 272 | data[i] = i; |
| 273 | } |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 274 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 275 | mCountingBuffer->unmap(); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | else return NULL; |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 280 | |
daniel@transgaming.com | f654945 | 2012-01-27 15:39:08 +0000 | [diff] [blame] | 281 | return mCountingBuffer; |
| 282 | } |
| 283 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 284 | IndexBuffer::IndexBuffer(rx::Renderer9 *renderer, UINT size, D3DFORMAT format) : mRenderer(renderer), mBufferSize(size), mIndexBuffer(NULL) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 285 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 286 | if (size > 0) |
| 287 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 288 | // D3D9_REPLACE |
daniel@transgaming.com | 113f0eb | 2012-10-31 18:46:58 +0000 | [diff] [blame] | 289 | HRESULT result = mRenderer->createIndexBuffer(size, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, format, &mIndexBuffer); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 290 | mSerial = issueSerial(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 291 | |
| 292 | if (FAILED(result)) |
| 293 | { |
| 294 | ERR("Out of memory allocating an index buffer of size %lu.", size); |
| 295 | } |
| 296 | } |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 297 | } |
| 298 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 299 | IndexBuffer::~IndexBuffer() |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 300 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 301 | if (mIndexBuffer) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 302 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 303 | mIndexBuffer->Release(); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 304 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 305 | } |
| 306 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 307 | IDirect3DIndexBuffer9 *IndexBuffer::getBuffer() const |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 308 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 309 | return mIndexBuffer; |
| 310 | } |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 311 | |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 312 | unsigned int IndexBuffer::getSerial() const |
| 313 | { |
| 314 | return mSerial; |
| 315 | } |
| 316 | |
| 317 | unsigned int IndexBuffer::issueSerial() |
| 318 | { |
| 319 | return mCurrentSerial++; |
| 320 | } |
| 321 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 322 | void IndexBuffer::unmap() |
| 323 | { |
| 324 | if (mIndexBuffer) |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 325 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 326 | mIndexBuffer->Unlock(); |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 327 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 328 | } |
| 329 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 330 | StreamingIndexBuffer::StreamingIndexBuffer(rx::Renderer9 *renderer, UINT initialSize, D3DFORMAT format) : IndexBuffer(renderer, initialSize, format) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 331 | { |
| 332 | mWritePosition = 0; |
| 333 | } |
| 334 | |
| 335 | StreamingIndexBuffer::~StreamingIndexBuffer() |
| 336 | { |
| 337 | } |
| 338 | |
| 339 | void *StreamingIndexBuffer::map(UINT requiredSpace, UINT *offset) |
| 340 | { |
| 341 | void *mapPtr = NULL; |
| 342 | |
| 343 | if (mIndexBuffer) |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 344 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 345 | HRESULT result = mIndexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE); |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 346 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 347 | if (FAILED(result)) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 348 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 349 | ERR(" Lock failed with error 0x%08x", result); |
| 350 | return NULL; |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 351 | } |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 352 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 353 | *offset = mWritePosition; |
| 354 | mWritePosition += requiredSpace; |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 355 | } |
| 356 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 357 | return mapPtr; |
| 358 | } |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 359 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 360 | void StreamingIndexBuffer::reserveSpace(UINT requiredSpace, GLenum type) |
| 361 | { |
| 362 | if (requiredSpace > mBufferSize) |
| 363 | { |
| 364 | if (mIndexBuffer) |
| 365 | { |
| 366 | mIndexBuffer->Release(); |
| 367 | mIndexBuffer = NULL; |
| 368 | } |
| 369 | |
| 370 | mBufferSize = std::max(requiredSpace, 2 * mBufferSize); |
| 371 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 372 | // D3D9_REPLACE |
daniel@transgaming.com | 113f0eb | 2012-10-31 18:46:58 +0000 | [diff] [blame] | 373 | HRESULT result = mRenderer->createIndexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, type == GL_UNSIGNED_INT ? D3DFMT_INDEX32 : D3DFMT_INDEX16, &mIndexBuffer); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 374 | mSerial = issueSerial(); |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 375 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 376 | if (FAILED(result)) |
| 377 | { |
| 378 | ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize); |
| 379 | } |
| 380 | |
| 381 | mWritePosition = 0; |
| 382 | } |
| 383 | else if (mWritePosition + requiredSpace > mBufferSize) // Recycle |
| 384 | { |
| 385 | void *dummy; |
| 386 | mIndexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD); |
| 387 | mIndexBuffer->Unlock(); |
| 388 | |
| 389 | mWritePosition = 0; |
| 390 | } |
| 391 | } |
| 392 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 393 | StaticIndexBuffer::StaticIndexBuffer(rx::Renderer9 *renderer) : IndexBuffer(renderer, 0, D3DFMT_UNKNOWN) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 394 | { |
| 395 | mCacheType = GL_NONE; |
| 396 | } |
| 397 | |
| 398 | StaticIndexBuffer::~StaticIndexBuffer() |
| 399 | { |
| 400 | } |
| 401 | |
| 402 | void *StaticIndexBuffer::map(UINT requiredSpace, UINT *offset) |
| 403 | { |
| 404 | void *mapPtr = NULL; |
| 405 | |
| 406 | if (mIndexBuffer) |
| 407 | { |
| 408 | HRESULT result = mIndexBuffer->Lock(0, requiredSpace, &mapPtr, 0); |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 409 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 410 | if (FAILED(result)) |
| 411 | { |
| 412 | ERR(" Lock failed with error 0x%08x", result); |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | *offset = 0; |
| 417 | } |
| 418 | |
| 419 | return mapPtr; |
| 420 | } |
| 421 | |
| 422 | void StaticIndexBuffer::reserveSpace(UINT requiredSpace, GLenum type) |
| 423 | { |
| 424 | if (!mIndexBuffer && mBufferSize == 0) |
| 425 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 426 | // D3D9_REPLACE |
daniel@transgaming.com | 113f0eb | 2012-10-31 18:46:58 +0000 | [diff] [blame] | 427 | HRESULT result = mRenderer->createIndexBuffer(requiredSpace, D3DUSAGE_WRITEONLY, type == GL_UNSIGNED_INT ? D3DFMT_INDEX32 : D3DFMT_INDEX16, &mIndexBuffer); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 428 | mSerial = issueSerial(); |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 429 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 430 | if (FAILED(result)) |
| 431 | { |
| 432 | ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize); |
| 433 | } |
| 434 | |
| 435 | mBufferSize = requiredSpace; |
| 436 | mCacheType = type; |
| 437 | } |
| 438 | else if (mIndexBuffer && mBufferSize >= requiredSpace && mCacheType == type) |
| 439 | { |
| 440 | // Already allocated |
| 441 | } |
| 442 | else UNREACHABLE(); // Static index buffers can't be resized |
| 443 | } |
| 444 | |
| 445 | bool StaticIndexBuffer::lookupType(GLenum type) |
| 446 | { |
| 447 | return mCacheType == type; |
| 448 | } |
| 449 | |
| 450 | UINT StaticIndexBuffer::lookupRange(intptr_t offset, GLsizei count, UINT *minIndex, UINT *maxIndex) |
| 451 | { |
jbauman@chromium.org | 43cbe74 | 2011-09-01 22:09:40 +0000 | [diff] [blame] | 452 | IndexRange range = {offset, count}; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 453 | |
jbauman@chromium.org | 43cbe74 | 2011-09-01 22:09:40 +0000 | [diff] [blame] | 454 | std::map<IndexRange, IndexResult>::iterator res = mCache.find(range); |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 455 | |
jbauman@chromium.org | 43cbe74 | 2011-09-01 22:09:40 +0000 | [diff] [blame] | 456 | if (res == mCache.end()) |
| 457 | { |
| 458 | return -1; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 459 | } |
| 460 | |
jbauman@chromium.org | 43cbe74 | 2011-09-01 22:09:40 +0000 | [diff] [blame] | 461 | *minIndex = res->second.minIndex; |
| 462 | *maxIndex = res->second.maxIndex; |
| 463 | return res->second.streamOffset; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void StaticIndexBuffer::addRange(intptr_t offset, GLsizei count, UINT minIndex, UINT maxIndex, UINT streamOffset) |
| 467 | { |
jbauman@chromium.org | 43cbe74 | 2011-09-01 22:09:40 +0000 | [diff] [blame] | 468 | IndexRange indexRange = {offset, count}; |
| 469 | IndexResult indexResult = {minIndex, maxIndex, streamOffset}; |
| 470 | mCache[indexRange] = indexResult; |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 471 | } |
| 472 | |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 473 | } |