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 | |
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 | 8fd34bd | 2011-02-18 02:52:14 +0000 | [diff] [blame] | 10 | #include "libGLESv2/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 | |
| 18 | namespace |
| 19 | { |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 20 | enum { INITIAL_INDEX_BUFFER_SIZE = 4096 * sizeof(GLuint) }; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | namespace gl |
| 24 | { |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 25 | unsigned int IndexBuffer::mCurrentSerial = 1; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 26 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 27 | IndexDataManager::IndexDataManager(Context *context, IDirect3DDevice9 *device) : mDevice(device) |
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 | mStreamingBufferShort = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16); |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 30 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 31 | if (context->supports32bitIndices()) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 32 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 33 | mStreamingBufferInt = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 34 | |
| 35 | if (!mStreamingBufferInt) |
| 36 | { |
| 37 | // Don't leave it in a half-initialized state |
| 38 | delete mStreamingBufferShort; |
| 39 | mStreamingBufferShort = NULL; |
| 40 | } |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 41 | } |
| 42 | else |
| 43 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 44 | mStreamingBufferInt = NULL; |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 45 | } |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 46 | |
| 47 | if (!mStreamingBufferShort) |
| 48 | { |
| 49 | ERR("Failed to allocate the streaming index buffer(s)."); |
| 50 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | IndexDataManager::~IndexDataManager() |
| 54 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 55 | delete mStreamingBufferShort; |
| 56 | delete mStreamingBufferInt; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 57 | } |
| 58 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 59 | void convertIndices(GLenum type, const void *input, GLsizei count, void *output) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 60 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 61 | if (type == GL_UNSIGNED_BYTE) |
| 62 | { |
| 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 | } |
| 71 | else if (type == GL_UNSIGNED_INT) |
| 72 | { |
| 73 | memcpy(output, input, count * sizeof(GLuint)); |
| 74 | } |
| 75 | else if (type == GL_UNSIGNED_SHORT) |
| 76 | { |
| 77 | memcpy(output, input, count * sizeof(GLushort)); |
| 78 | } |
| 79 | else UNREACHABLE(); |
| 80 | } |
| 81 | |
| 82 | template <class IndexType> |
| 83 | void computeRange(const IndexType *indices, GLsizei count, GLuint *minIndex, GLuint *maxIndex) |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 84 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 85 | *minIndex = indices[0]; |
| 86 | *maxIndex = indices[0]; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 87 | |
| 88 | for (GLsizei i = 0; i < count; i++) |
| 89 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 90 | if (*minIndex > indices[i]) *minIndex = indices[i]; |
| 91 | if (*maxIndex < indices[i]) *maxIndex = indices[i]; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 92 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 93 | } |
| 94 | |
daniel@transgaming.com | d2820bf | 2012-01-27 15:38:48 +0000 | [diff] [blame] | 95 | 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] | 96 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 97 | if (type == GL_UNSIGNED_BYTE) |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 98 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 99 | computeRange(static_cast<const GLubyte*>(indices), count, minIndex, maxIndex); |
| 100 | } |
| 101 | else if (type == GL_UNSIGNED_INT) |
| 102 | { |
| 103 | computeRange(static_cast<const GLuint*>(indices), count, minIndex, maxIndex); |
| 104 | } |
| 105 | else if (type == GL_UNSIGNED_SHORT) |
| 106 | { |
| 107 | computeRange(static_cast<const GLushort*>(indices), count, minIndex, maxIndex); |
| 108 | } |
| 109 | else UNREACHABLE(); |
| 110 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 111 | |
daniel@transgaming.com | d2820bf | 2012-01-27 15:38:48 +0000 | [diff] [blame] | 112 | GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 113 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 114 | if (!mStreamingBufferShort) |
| 115 | { |
| 116 | return GL_OUT_OF_MEMORY; |
| 117 | } |
| 118 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 119 | D3DFORMAT format = (type == GL_UNSIGNED_INT) ? D3DFMT_INDEX32 : D3DFMT_INDEX16; |
| 120 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
| 121 | bool alignedOffset = false; |
| 122 | |
| 123 | if (buffer != NULL) |
| 124 | { |
| 125 | switch (type) |
| 126 | { |
| 127 | case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break; |
| 128 | case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break; |
| 129 | case GL_UNSIGNED_INT: alignedOffset = (offset % sizeof(GLuint) == 0); break; |
| 130 | default: UNREACHABLE(); alignedOffset = false; |
| 131 | } |
| 132 | |
| 133 | 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] | 134 | { |
| 135 | return GL_INVALID_OPERATION; |
| 136 | } |
| 137 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 138 | indices = static_cast<const GLubyte*>(buffer->data()) + offset; |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 139 | } |
| 140 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 141 | StreamingIndexBuffer *streamingBuffer = (type == GL_UNSIGNED_INT) ? mStreamingBufferInt : mStreamingBufferShort; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 142 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 143 | StaticIndexBuffer *staticBuffer = buffer ? buffer->getStaticIndexBuffer() : NULL; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 144 | IndexBuffer *indexBuffer = streamingBuffer; |
| 145 | UINT streamOffset = 0; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 146 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 147 | if (staticBuffer && staticBuffer->lookupType(type) && alignedOffset) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 148 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 149 | indexBuffer = staticBuffer; |
| 150 | streamOffset = staticBuffer->lookupRange(offset, count, &translated->minIndex, &translated->maxIndex); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 151 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 152 | if (streamOffset == -1) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 153 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 154 | streamOffset = (offset / typeSize(type)) * indexSize(format); |
| 155 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
| 156 | staticBuffer->addRange(offset, count, translated->minIndex, translated->maxIndex, streamOffset); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 159 | else |
| 160 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 161 | int convertCount = count; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 162 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 163 | if (staticBuffer) |
| 164 | { |
| 165 | if (staticBuffer->size() == 0 && alignedOffset) |
| 166 | { |
| 167 | indexBuffer = staticBuffer; |
| 168 | convertCount = buffer->size() / typeSize(type); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | buffer->invalidateStaticData(); |
| 173 | staticBuffer = NULL; |
| 174 | } |
| 175 | } |
| 176 | |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 177 | void *output = NULL; |
| 178 | |
| 179 | if (indexBuffer) |
| 180 | { |
| 181 | indexBuffer->reserveSpace(convertCount * indexSize(format), type); |
| 182 | output = indexBuffer->map(indexSize(format) * convertCount, &streamOffset); |
| 183 | } |
| 184 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 185 | if (output == NULL) |
| 186 | { |
| 187 | ERR("Failed to map index buffer."); |
| 188 | return GL_OUT_OF_MEMORY; |
| 189 | } |
| 190 | |
| 191 | convertIndices(type, staticBuffer ? buffer->data() : indices, convertCount, output); |
| 192 | indexBuffer->unmap(); |
| 193 | |
| 194 | computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); |
| 195 | |
| 196 | if (staticBuffer) |
| 197 | { |
| 198 | streamOffset = (offset / typeSize(type)) * indexSize(format); |
| 199 | staticBuffer->addRange(offset, count, translated->minIndex, translated->maxIndex, streamOffset); |
| 200 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 201 | } |
| 202 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 203 | translated->indexBuffer = indexBuffer->getBuffer(); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 204 | translated->serial = indexBuffer->getSerial(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 205 | translated->startIndex = streamOffset / indexSize(format); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 206 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 207 | if (buffer) |
| 208 | { |
| 209 | buffer->promoteStaticUsage(count * typeSize(type)); |
| 210 | } |
| 211 | |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 212 | return GL_NO_ERROR; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 213 | } |
| 214 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 215 | std::size_t IndexDataManager::indexSize(D3DFORMAT format) const |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 216 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 217 | return (format == D3DFMT_INDEX32) ? sizeof(unsigned int) : sizeof(unsigned short); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 218 | } |
| 219 | |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 220 | std::size_t IndexDataManager::typeSize(GLenum type) const |
| 221 | { |
| 222 | switch (type) |
| 223 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 224 | case GL_UNSIGNED_INT: return sizeof(GLuint); |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 225 | case GL_UNSIGNED_SHORT: return sizeof(GLushort); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 226 | case GL_UNSIGNED_BYTE: return sizeof(GLubyte); |
| 227 | default: UNREACHABLE(); return sizeof(GLushort); |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 231 | IndexBuffer::IndexBuffer(IDirect3DDevice9 *device, UINT size, D3DFORMAT format) : mDevice(device), mBufferSize(size), mIndexBuffer(NULL) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 232 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 233 | if (size > 0) |
| 234 | { |
daniel@transgaming.com | ee04e45 | 2011-01-08 05:46:27 +0000 | [diff] [blame] | 235 | D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY); |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 236 | HRESULT result = device->CreateIndexBuffer(size, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, format, pool, &mIndexBuffer, NULL); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 237 | mSerial = issueSerial(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 238 | |
| 239 | if (FAILED(result)) |
| 240 | { |
| 241 | ERR("Out of memory allocating an index buffer of size %lu.", size); |
| 242 | } |
| 243 | } |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 244 | } |
| 245 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 246 | IndexBuffer::~IndexBuffer() |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 247 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 248 | if (mIndexBuffer) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 249 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 250 | mIndexBuffer->Release(); |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 251 | } |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 252 | } |
| 253 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 254 | IDirect3DIndexBuffer9 *IndexBuffer::getBuffer() const |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 255 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 256 | return mIndexBuffer; |
| 257 | } |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 258 | |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 259 | unsigned int IndexBuffer::getSerial() const |
| 260 | { |
| 261 | return mSerial; |
| 262 | } |
| 263 | |
| 264 | unsigned int IndexBuffer::issueSerial() |
| 265 | { |
| 266 | return mCurrentSerial++; |
| 267 | } |
| 268 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 269 | void IndexBuffer::unmap() |
| 270 | { |
| 271 | if (mIndexBuffer) |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 272 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 273 | mIndexBuffer->Unlock(); |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 274 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | StreamingIndexBuffer::StreamingIndexBuffer(IDirect3DDevice9 *device, UINT initialSize, D3DFORMAT format) : IndexBuffer(device, initialSize, format) |
| 278 | { |
| 279 | mWritePosition = 0; |
| 280 | } |
| 281 | |
| 282 | StreamingIndexBuffer::~StreamingIndexBuffer() |
| 283 | { |
| 284 | } |
| 285 | |
| 286 | void *StreamingIndexBuffer::map(UINT requiredSpace, UINT *offset) |
| 287 | { |
| 288 | void *mapPtr = NULL; |
| 289 | |
| 290 | if (mIndexBuffer) |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 291 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 292 | HRESULT result = mIndexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE); |
| 293 | |
| 294 | if (FAILED(result)) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 295 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 296 | ERR(" Lock failed with error 0x%08x", result); |
| 297 | return NULL; |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 298 | } |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 299 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 300 | *offset = mWritePosition; |
| 301 | mWritePosition += requiredSpace; |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 302 | } |
| 303 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 304 | return mapPtr; |
| 305 | } |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 306 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 307 | void StreamingIndexBuffer::reserveSpace(UINT requiredSpace, GLenum type) |
| 308 | { |
| 309 | if (requiredSpace > mBufferSize) |
| 310 | { |
| 311 | if (mIndexBuffer) |
| 312 | { |
| 313 | mIndexBuffer->Release(); |
| 314 | mIndexBuffer = NULL; |
| 315 | } |
| 316 | |
| 317 | mBufferSize = std::max(requiredSpace, 2 * mBufferSize); |
| 318 | |
daniel@transgaming.com | ee04e45 | 2011-01-08 05:46:27 +0000 | [diff] [blame] | 319 | D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY); |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 320 | HRESULT result = mDevice->CreateIndexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, type == GL_UNSIGNED_INT ? D3DFMT_INDEX32 : D3DFMT_INDEX16, pool, &mIndexBuffer, NULL); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 321 | mSerial = issueSerial(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 322 | |
| 323 | if (FAILED(result)) |
| 324 | { |
| 325 | ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize); |
| 326 | } |
| 327 | |
| 328 | mWritePosition = 0; |
| 329 | } |
| 330 | else if (mWritePosition + requiredSpace > mBufferSize) // Recycle |
| 331 | { |
| 332 | void *dummy; |
| 333 | mIndexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD); |
| 334 | mIndexBuffer->Unlock(); |
| 335 | |
| 336 | mWritePosition = 0; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | StaticIndexBuffer::StaticIndexBuffer(IDirect3DDevice9 *device) : IndexBuffer(device, 0, D3DFMT_UNKNOWN) |
| 341 | { |
| 342 | mCacheType = GL_NONE; |
| 343 | } |
| 344 | |
| 345 | StaticIndexBuffer::~StaticIndexBuffer() |
| 346 | { |
| 347 | } |
| 348 | |
| 349 | void *StaticIndexBuffer::map(UINT requiredSpace, UINT *offset) |
| 350 | { |
| 351 | void *mapPtr = NULL; |
| 352 | |
| 353 | if (mIndexBuffer) |
| 354 | { |
| 355 | HRESULT result = mIndexBuffer->Lock(0, requiredSpace, &mapPtr, 0); |
| 356 | |
| 357 | if (FAILED(result)) |
| 358 | { |
| 359 | ERR(" Lock failed with error 0x%08x", result); |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | *offset = 0; |
| 364 | } |
| 365 | |
| 366 | return mapPtr; |
| 367 | } |
| 368 | |
| 369 | void StaticIndexBuffer::reserveSpace(UINT requiredSpace, GLenum type) |
| 370 | { |
| 371 | if (!mIndexBuffer && mBufferSize == 0) |
| 372 | { |
daniel@transgaming.com | ee04e45 | 2011-01-08 05:46:27 +0000 | [diff] [blame] | 373 | D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY); |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 374 | HRESULT result = mDevice->CreateIndexBuffer(requiredSpace, D3DUSAGE_WRITEONLY, type == GL_UNSIGNED_INT ? D3DFMT_INDEX32 : D3DFMT_INDEX16, pool, &mIndexBuffer, NULL); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 375 | mSerial = issueSerial(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 376 | |
| 377 | if (FAILED(result)) |
| 378 | { |
| 379 | ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize); |
| 380 | } |
| 381 | |
| 382 | mBufferSize = requiredSpace; |
| 383 | mCacheType = type; |
| 384 | } |
| 385 | else if (mIndexBuffer && mBufferSize >= requiredSpace && mCacheType == type) |
| 386 | { |
| 387 | // Already allocated |
| 388 | } |
| 389 | else UNREACHABLE(); // Static index buffers can't be resized |
| 390 | } |
| 391 | |
| 392 | bool StaticIndexBuffer::lookupType(GLenum type) |
| 393 | { |
| 394 | return mCacheType == type; |
| 395 | } |
| 396 | |
| 397 | UINT StaticIndexBuffer::lookupRange(intptr_t offset, GLsizei count, UINT *minIndex, UINT *maxIndex) |
| 398 | { |
jbauman@chromium.org | 43cbe74 | 2011-09-01 22:09:40 +0000 | [diff] [blame] | 399 | IndexRange range = {offset, count}; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 400 | |
jbauman@chromium.org | 43cbe74 | 2011-09-01 22:09:40 +0000 | [diff] [blame] | 401 | std::map<IndexRange, IndexResult>::iterator res = mCache.find(range); |
| 402 | |
| 403 | if (res == mCache.end()) |
| 404 | { |
| 405 | return -1; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 406 | } |
| 407 | |
jbauman@chromium.org | 43cbe74 | 2011-09-01 22:09:40 +0000 | [diff] [blame] | 408 | *minIndex = res->second.minIndex; |
| 409 | *maxIndex = res->second.maxIndex; |
| 410 | return res->second.streamOffset; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void StaticIndexBuffer::addRange(intptr_t offset, GLsizei count, UINT minIndex, UINT maxIndex, UINT streamOffset) |
| 414 | { |
jbauman@chromium.org | 43cbe74 | 2011-09-01 22:09:40 +0000 | [diff] [blame] | 415 | IndexRange indexRange = {offset, count}; |
| 416 | IndexResult indexResult = {minIndex, maxIndex, streamOffset}; |
| 417 | mCache[indexRange] = indexResult; |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 418 | } |
| 419 | |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 420 | } |