daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +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 | // VertexDataManager.h: Defines the VertexDataManager, a class that |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 8 | // runs the Buffer translation process. |
| 9 | |
daniel@transgaming.com | 8fd34bd | 2011-02-18 02:52:14 +0000 | [diff] [blame] | 10 | #include "libGLESv2/VertexDataManager.h" |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 11 | |
alokp@chromium.org | ea0e1af | 2010-03-22 19:33:14 +0000 | [diff] [blame] | 12 | #include "common/debug.h" |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 13 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 14 | #include "libGLESv2/Buffer.h" |
| 15 | #include "libGLESv2/Program.h" |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 16 | #include "libGLESv2/main.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 17 | |
daniel@transgaming.com | 8fd34bd | 2011-02-18 02:52:14 +0000 | [diff] [blame] | 18 | #include "libGLESv2/vertexconversion.h" |
| 19 | #include "libGLESv2/IndexDataManager.h" |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 20 | |
| 21 | namespace |
| 22 | { |
| 23 | enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 }; |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 24 | // This has to be at least 4k or else it fails on ATI cards. |
| 25 | enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 }; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | namespace gl |
| 29 | { |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 30 | unsigned int VertexBuffer::mCurrentSerial = 1; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 31 | |
daniel@transgaming.com | baa7451 | 2011-04-13 14:56:47 +0000 | [diff] [blame] | 32 | VertexDataManager::VertexDataManager(Context *context, IDirect3DDevice9 *device) : mContext(context), mDevice(device) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 33 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 34 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 35 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 36 | mDirtyCurrentValue[i] = true; |
| 37 | mCurrentValueBuffer[i] = NULL; |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 38 | mCurrentValueOffsets[i] = 0; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 39 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 40 | |
| 41 | const D3DCAPS9 &caps = context->getDeviceCaps(); |
| 42 | checkVertexCaps(caps.DeclTypes); |
| 43 | |
| 44 | mStreamingBuffer = new StreamingVertexBuffer(mDevice, INITIAL_STREAM_BUFFER_SIZE); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 45 | |
| 46 | if (!mStreamingBuffer) |
| 47 | { |
| 48 | ERR("Failed to allocate the streaming vertex buffer."); |
| 49 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | VertexDataManager::~VertexDataManager() |
| 53 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 54 | delete mStreamingBuffer; |
| 55 | |
| 56 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 57 | { |
| 58 | delete mCurrentValueBuffer[i]; |
| 59 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 60 | } |
| 61 | |
daniel@transgaming.com | 58f76fe | 2011-06-21 14:21:07 +0000 | [diff] [blame] | 62 | std::size_t VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 63 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 64 | Buffer *buffer = attribute.mBoundBuffer.get(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 65 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 66 | int inputStride = attribute.stride(); |
| 67 | int elementSize = attribute.typeSize(); |
| 68 | const FormatConverter &converter = formatConverter(attribute); |
daniel@transgaming.com | 58f76fe | 2011-06-21 14:21:07 +0000 | [diff] [blame] | 69 | std::size_t streamOffset = 0; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 70 | |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 71 | void *output = NULL; |
| 72 | |
| 73 | if (vertexBuffer) |
| 74 | { |
| 75 | output = vertexBuffer->map(attribute, spaceRequired(attribute, count), &streamOffset); |
| 76 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 77 | |
| 78 | if (output == NULL) |
| 79 | { |
| 80 | ERR("Failed to map vertex buffer."); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | const char *input = NULL; |
| 85 | |
| 86 | if (buffer) |
| 87 | { |
| 88 | int offset = attribute.mOffset; |
| 89 | |
| 90 | input = static_cast<const char*>(buffer->data()) + offset; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | input = static_cast<const char*>(attribute.mPointer); |
| 95 | } |
| 96 | |
| 97 | input += inputStride * start; |
| 98 | |
| 99 | if (converter.identity && inputStride == elementSize) |
| 100 | { |
| 101 | memcpy(output, input, count * inputStride); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | converter.convertArray(input, inputStride, count, output); |
| 106 | } |
| 107 | |
| 108 | vertexBuffer->unmap(); |
| 109 | |
| 110 | return streamOffset; |
| 111 | } |
| 112 | |
| 113 | GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated) |
| 114 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 115 | if (!mStreamingBuffer) |
| 116 | { |
| 117 | return GL_OUT_OF_MEMORY; |
| 118 | } |
| 119 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 120 | const VertexAttributeArray &attribs = mContext->getVertexAttributes(); |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame] | 121 | Program *program = mContext->getCurrentProgram(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 122 | |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame] | 123 | for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 124 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 125 | translated[attributeIndex].active = (program->getSemanticIndex(attributeIndex) != -1); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 126 | } |
| 127 | |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 128 | // Determine the required storage size per used buffer, and invalidate static buffers that don't contain matching attributes |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 129 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 130 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 131 | if (translated[i].active && attribs[i].mArrayEnabled) |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 132 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 133 | Buffer *buffer = attribs[i].mBoundBuffer.get(); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 134 | StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 135 | |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 136 | if (staticBuffer) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 137 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 138 | if (staticBuffer->size() == 0) |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 139 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 140 | int totalCount = buffer->size() / attribs[i].stride(); |
| 141 | staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount)); |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 142 | } |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 143 | else if (staticBuffer->lookupAttribute(attribs[i]) == -1) |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 144 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 145 | // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer |
daniel@transgaming.com | b0eb697 | 2011-07-08 16:23:42 +0000 | [diff] [blame] | 146 | // Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer |
| 147 | for (int previous = 0; previous < i; previous++) |
| 148 | { |
| 149 | if (translated[previous].active && attribs[previous].mArrayEnabled) |
| 150 | { |
| 151 | Buffer *previousBuffer = attribs[previous].mBoundBuffer.get(); |
| 152 | StaticVertexBuffer *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL; |
| 153 | |
| 154 | if (staticBuffer == previousStaticBuffer) |
| 155 | { |
| 156 | mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count)); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 161 | mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count)); |
daniel@transgaming.com | cb325c8 | 2011-08-02 12:34:36 +0000 | [diff] [blame] | 162 | |
| 163 | buffer->invalidateStaticData(); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count)); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Reserve the required space per used buffer |
| 174 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 175 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 176 | if (translated[i].active && attribs[i].mArrayEnabled) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 177 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 178 | Buffer *buffer = attribs[i].mBoundBuffer.get(); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 179 | ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 180 | ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer; |
| 181 | |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 182 | if (vertexBuffer) |
| 183 | { |
| 184 | vertexBuffer->reserveRequiredSpace(); |
| 185 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | // Perform the vertex data translations |
| 190 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 191 | { |
| 192 | if (translated[i].active) |
| 193 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 194 | if (attribs[i].mArrayEnabled) |
| 195 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 196 | Buffer *buffer = attribs[i].mBoundBuffer.get(); |
| 197 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 198 | if (!buffer && attribs[i].mPointer == NULL) |
| 199 | { |
| 200 | // This is an application error that would normally result in a crash, but we catch it and return an error |
| 201 | ERR("An enabled vertex array has no buffer and no pointer."); |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 202 | return GL_INVALID_OPERATION; |
| 203 | } |
| 204 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 205 | const FormatConverter &converter = formatConverter(attribs[i]); |
| 206 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 207 | StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 208 | ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer); |
| 209 | |
daniel@transgaming.com | 58f76fe | 2011-06-21 14:21:07 +0000 | [diff] [blame] | 210 | std::size_t streamOffset = -1; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 211 | |
| 212 | if (staticBuffer) |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 213 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 214 | streamOffset = staticBuffer->lookupAttribute(attribs[i]); |
| 215 | |
| 216 | if (streamOffset == -1) |
| 217 | { |
| 218 | // Convert the entire buffer |
| 219 | int totalCount = buffer->size() / attribs[i].stride(); |
| 220 | int startIndex = attribs[i].mOffset / attribs[i].stride(); |
| 221 | |
| 222 | streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i]); |
| 223 | } |
| 224 | |
| 225 | if (streamOffset != -1) |
| 226 | { |
| 227 | streamOffset += (start + attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize; |
| 228 | } |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i]); |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 233 | } |
| 234 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 235 | if (streamOffset == -1) |
| 236 | { |
| 237 | return GL_OUT_OF_MEMORY; |
| 238 | } |
| 239 | |
| 240 | translated[i].vertexBuffer = vertexBuffer->getBuffer(); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 241 | translated[i].serial = vertexBuffer->getSerial(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 242 | translated[i].type = converter.d3dDeclType; |
| 243 | translated[i].stride = converter.outputElementSize; |
| 244 | translated[i].offset = streamOffset; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 245 | } |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 246 | else |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 247 | { |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 248 | if (!mCurrentValueBuffer[i]) |
| 249 | { |
| 250 | mCurrentValueBuffer[i] = new StreamingVertexBuffer(mDevice, CONSTANT_VERTEX_BUFFER_SIZE); |
| 251 | } |
| 252 | |
| 253 | StreamingVertexBuffer *buffer = mCurrentValueBuffer[i]; |
| 254 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 255 | if (mDirtyCurrentValue[i]) |
| 256 | { |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 257 | const int requiredSpace = 4 * sizeof(float); |
| 258 | buffer->addRequiredSpace(requiredSpace); |
| 259 | buffer->reserveRequiredSpace(); |
| 260 | float *data = static_cast<float*>(buffer->map(VertexAttribute(), requiredSpace, &mCurrentValueOffsets[i])); |
| 261 | if (data) |
| 262 | { |
| 263 | data[0] = attribs[i].mCurrentValue[0]; |
| 264 | data[1] = attribs[i].mCurrentValue[1]; |
| 265 | data[2] = attribs[i].mCurrentValue[2]; |
| 266 | data[3] = attribs[i].mCurrentValue[3]; |
| 267 | buffer->unmap(); |
| 268 | mDirtyCurrentValue[i] = false; |
| 269 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer(); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 273 | translated[i].serial = mCurrentValueBuffer[i]->getSerial(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 274 | |
| 275 | translated[i].type = D3DDECLTYPE_FLOAT4; |
| 276 | translated[i].stride = 0; |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 277 | translated[i].offset = mCurrentValueOffsets[i]; |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 278 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 281 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 282 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 283 | { |
| 284 | if (translated[i].active && attribs[i].mArrayEnabled) |
| 285 | { |
| 286 | Buffer *buffer = attribs[i].mBoundBuffer.get(); |
| 287 | |
| 288 | if (buffer) |
| 289 | { |
| 290 | buffer->promoteStaticUsage(count * attribs[i].typeSize()); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 295 | return GL_NO_ERROR; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 296 | } |
| 297 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 298 | std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count) const |
| 299 | { |
| 300 | return formatConverter(attrib).outputElementSize * count; |
| 301 | } |
| 302 | |
| 303 | // Mapping from OpenGL-ES vertex attrib type to D3D decl type: |
| 304 | // |
| 305 | // BYTE SHORT (Cast) |
| 306 | // BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm) |
| 307 | // UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast) |
| 308 | // UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize) |
| 309 | // SHORT SHORT (Identity) |
| 310 | // SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize) |
| 311 | // UNSIGNED_SHORT FLOAT (Cast) |
| 312 | // UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize) |
| 313 | // FIXED (not in WebGL) FLOAT (FixedToFloat) |
| 314 | // FLOAT FLOAT (Identity) |
| 315 | |
| 316 | // GLToCType maps from GL type (as GLenum) to the C typedef. |
| 317 | template <GLenum GLType> struct GLToCType { }; |
| 318 | |
| 319 | template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; }; |
| 320 | template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; }; |
| 321 | template <> struct GLToCType<GL_SHORT> { typedef GLshort type; }; |
| 322 | template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; }; |
| 323 | template <> struct GLToCType<GL_FIXED> { typedef GLuint type; }; |
| 324 | template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; }; |
| 325 | |
| 326 | // This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.) |
| 327 | enum D3DVertexType |
| 328 | { |
| 329 | D3DVT_FLOAT, |
| 330 | D3DVT_SHORT, |
| 331 | D3DVT_SHORT_NORM, |
| 332 | D3DVT_UBYTE, |
| 333 | D3DVT_UBYTE_NORM, |
| 334 | D3DVT_USHORT_NORM |
| 335 | }; |
| 336 | |
| 337 | // D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type. |
| 338 | template <unsigned int D3DType> struct D3DToCType { }; |
| 339 | |
| 340 | template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; }; |
| 341 | template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; }; |
| 342 | template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; }; |
| 343 | template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; }; |
| 344 | template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; }; |
| 345 | template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; }; |
| 346 | |
| 347 | // Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size. |
| 348 | template <unsigned int type, int size> |
| 349 | struct WidenRule |
| 350 | { |
| 351 | }; |
| 352 | |
| 353 | template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { }; |
| 354 | template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { }; |
| 355 | template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { }; |
| 356 | template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { }; |
| 357 | template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { }; |
| 358 | template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { }; |
| 359 | |
| 360 | // VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination. |
| 361 | template <unsigned int d3dtype, int size> |
| 362 | struct VertexTypeFlags |
| 363 | { |
| 364 | }; |
| 365 | |
| 366 | template <unsigned int capflag, unsigned int declflag> |
| 367 | struct VertexTypeFlagsHelper |
| 368 | { |
| 369 | enum { capflag = capflag }; |
| 370 | enum { declflag = declflag }; |
| 371 | }; |
| 372 | |
| 373 | template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { }; |
| 374 | template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { }; |
| 375 | template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { }; |
| 376 | template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { }; |
| 377 | template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { }; |
| 378 | template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { }; |
| 379 | template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { }; |
| 380 | template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { }; |
| 381 | template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { }; |
| 382 | template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { }; |
| 383 | template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { }; |
| 384 | template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { }; |
| 385 | |
| 386 | |
| 387 | // VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums). |
| 388 | template <GLenum GLtype, bool normalized> |
| 389 | struct VertexTypeMapping |
| 390 | { |
| 391 | }; |
| 392 | |
| 393 | template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred> |
| 394 | struct VertexTypeMappingBase |
| 395 | { |
| 396 | enum { preferred = Preferred }; |
| 397 | enum { fallback = Fallback }; |
| 398 | }; |
| 399 | |
| 400 | template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast |
| 401 | template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize |
| 402 | template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast |
| 403 | template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize |
| 404 | template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity |
| 405 | template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize |
| 406 | template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast |
| 407 | template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize |
| 408 | template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat |
| 409 | template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity |
| 410 | |
| 411 | |
| 412 | // Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat). |
| 413 | // The conversion rules themselves are defined in vertexconversion.h. |
| 414 | |
| 415 | // Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping). |
| 416 | template <GLenum fromType, bool normalized, unsigned int toType> |
| 417 | struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type> |
| 418 | { |
| 419 | }; |
| 420 | |
| 421 | // All conversions from normalized types to float use the Normalize operator. |
| 422 | template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { }; |
| 423 | |
| 424 | // Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules. |
| 425 | template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { }; |
| 426 | template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { }; |
| 427 | |
| 428 | // A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1) |
| 429 | // whether it is normalized or not. |
| 430 | template <class T, bool normalized> |
| 431 | struct DefaultVertexValuesStage2 |
| 432 | { |
| 433 | }; |
| 434 | |
| 435 | template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { }; |
| 436 | template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { }; |
| 437 | |
| 438 | // Work out the default value rule for a D3D type (expressed as the C type) and |
| 439 | template <class T, bool normalized> |
| 440 | struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized> |
| 441 | { |
| 442 | }; |
| 443 | |
| 444 | template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { }; |
| 445 | |
| 446 | // Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion. |
| 447 | // The fallback conversion produces an output that all D3D9 devices must support. |
| 448 | template <class T> struct UsePreferred { enum { type = T::preferred }; }; |
| 449 | template <class T> struct UseFallback { enum { type = T::fallback }; }; |
| 450 | |
| 451 | // Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion, |
| 452 | // it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag |
| 453 | // and the D3DDECLTYPE member needed for the vertex declaration in declflag. |
| 454 | template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule> |
| 455 | struct Converter |
| 456 | : gl::VertexDataConverter<typename GLToCType<fromType>::type, |
| 457 | WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>, |
| 458 | ConversionRule<fromType, |
| 459 | normalized, |
| 460 | PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>, |
| 461 | DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > > |
| 462 | { |
| 463 | private: |
| 464 | enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type }; |
| 465 | enum { d3dsize = WidenRule<d3dtype, size>::finalWidth }; |
| 466 | |
| 467 | public: |
| 468 | enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag }; |
| 469 | enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag }; |
| 470 | }; |
| 471 | |
| 472 | // Initialise a TranslationInfo |
| 473 | #define TRANSLATION(type, norm, size, preferred) \ |
| 474 | { \ |
| 475 | Converter<type, norm, size, preferred>::identity, \ |
| 476 | Converter<type, norm, size, preferred>::finalSize, \ |
| 477 | Converter<type, norm, size, preferred>::convertArray, \ |
| 478 | static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \ |
| 479 | } |
| 480 | |
| 481 | #define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \ |
| 482 | { \ |
| 483 | Converter<type, norm, size, UsePreferred>::capflag, \ |
| 484 | TRANSLATION(type, norm, size, UsePreferred), \ |
| 485 | TRANSLATION(type, norm, size, UseFallback) \ |
| 486 | } |
| 487 | |
| 488 | #define TRANSLATIONS_FOR_TYPE(type) \ |
| 489 | { \ |
| 490 | { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \ |
| 491 | { TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 4) }, \ |
| 492 | } |
| 493 | |
| 494 | const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1] |
| 495 | { |
| 496 | TRANSLATIONS_FOR_TYPE(GL_BYTE), |
| 497 | TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE), |
| 498 | TRANSLATIONS_FOR_TYPE(GL_SHORT), |
| 499 | TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT), |
| 500 | TRANSLATIONS_FOR_TYPE(GL_FIXED), |
| 501 | TRANSLATIONS_FOR_TYPE(GL_FLOAT) |
| 502 | }; |
| 503 | |
| 504 | void VertexDataManager::checkVertexCaps(DWORD declTypes) |
| 505 | { |
| 506 | for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++) |
| 507 | { |
| 508 | for (unsigned int j = 0; j < 2; j++) |
| 509 | { |
| 510 | for (unsigned int k = 0; k < 4; k++) |
| 511 | { |
| 512 | if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0) |
| 513 | { |
| 514 | mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion; |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // This is used to index mAttributeTypes and mPossibleTranslations. |
| 526 | unsigned int VertexDataManager::typeIndex(GLenum type) const |
| 527 | { |
| 528 | switch (type) |
| 529 | { |
| 530 | case GL_BYTE: return 0; |
| 531 | case GL_UNSIGNED_BYTE: return 1; |
| 532 | case GL_SHORT: return 2; |
| 533 | case GL_UNSIGNED_SHORT: return 3; |
| 534 | case GL_FIXED: return 4; |
| 535 | case GL_FLOAT: return 5; |
| 536 | |
| 537 | default: UNREACHABLE(); return 5; |
| 538 | } |
| 539 | } |
| 540 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 541 | VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL) |
| 542 | { |
| 543 | if (size > 0) |
| 544 | { |
daniel@transgaming.com | ee04e45 | 2011-01-08 05:46:27 +0000 | [diff] [blame] | 545 | D3DPOOL pool = getDisplay()->getBufferPool(usageFlags); |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 546 | HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 547 | mSerial = issueSerial(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 548 | |
| 549 | if (FAILED(result)) |
| 550 | { |
| 551 | ERR("Out of memory allocating a vertex buffer of size %lu.", size); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | VertexBuffer::~VertexBuffer() |
| 557 | { |
| 558 | if (mVertexBuffer) |
| 559 | { |
| 560 | mVertexBuffer->Release(); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | void VertexBuffer::unmap() |
| 565 | { |
| 566 | if (mVertexBuffer) |
| 567 | { |
| 568 | mVertexBuffer->Unlock(); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const |
| 573 | { |
| 574 | return mVertexBuffer; |
| 575 | } |
| 576 | |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 577 | unsigned int VertexBuffer::getSerial() const |
| 578 | { |
| 579 | return mSerial; |
| 580 | } |
| 581 | |
| 582 | unsigned int VertexBuffer::issueSerial() |
| 583 | { |
| 584 | return mCurrentSerial++; |
| 585 | } |
| 586 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 587 | ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags) |
| 588 | { |
| 589 | mBufferSize = size; |
| 590 | mWritePosition = 0; |
| 591 | mRequiredSpace = 0; |
| 592 | } |
| 593 | |
| 594 | ArrayVertexBuffer::~ArrayVertexBuffer() |
| 595 | { |
| 596 | } |
| 597 | |
| 598 | void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace) |
| 599 | { |
| 600 | mRequiredSpace += requiredSpace; |
| 601 | } |
| 602 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 603 | StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY) |
| 604 | { |
| 605 | } |
| 606 | |
| 607 | StreamingVertexBuffer::~StreamingVertexBuffer() |
| 608 | { |
| 609 | } |
| 610 | |
| 611 | void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset) |
| 612 | { |
| 613 | void *mapPtr = NULL; |
| 614 | |
| 615 | if (mVertexBuffer) |
| 616 | { |
| 617 | HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE); |
| 618 | |
| 619 | if (FAILED(result)) |
| 620 | { |
| 621 | ERR("Lock failed with error 0x%08x", result); |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | *offset = mWritePosition; |
| 626 | mWritePosition += requiredSpace; |
| 627 | } |
| 628 | |
| 629 | return mapPtr; |
| 630 | } |
| 631 | |
| 632 | void StreamingVertexBuffer::reserveRequiredSpace() |
| 633 | { |
| 634 | if (mRequiredSpace > mBufferSize) |
| 635 | { |
| 636 | if (mVertexBuffer) |
| 637 | { |
| 638 | mVertexBuffer->Release(); |
| 639 | mVertexBuffer = NULL; |
| 640 | } |
| 641 | |
| 642 | mBufferSize = std::max(mRequiredSpace, 3 * mBufferSize / 2); // 1.5 x mBufferSize is arbitrary and should be checked to see we don't have too many reallocations. |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 643 | |
daniel@transgaming.com | ee04e45 | 2011-01-08 05:46:27 +0000 | [diff] [blame] | 644 | D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY); |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 645 | HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 646 | mSerial = issueSerial(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 647 | |
| 648 | if (FAILED(result)) |
| 649 | { |
| 650 | ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize); |
| 651 | } |
| 652 | |
| 653 | mWritePosition = 0; |
| 654 | } |
| 655 | else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle |
| 656 | { |
| 657 | if (mVertexBuffer) |
| 658 | { |
| 659 | void *dummy; |
| 660 | mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD); |
| 661 | mVertexBuffer->Unlock(); |
| 662 | } |
| 663 | |
| 664 | mWritePosition = 0; |
| 665 | } |
| 666 | |
| 667 | mRequiredSpace = 0; |
| 668 | } |
| 669 | |
| 670 | StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY) |
| 671 | { |
| 672 | } |
| 673 | |
| 674 | StaticVertexBuffer::~StaticVertexBuffer() |
| 675 | { |
| 676 | } |
| 677 | |
daniel@transgaming.com | 58f76fe | 2011-06-21 14:21:07 +0000 | [diff] [blame] | 678 | void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 679 | { |
| 680 | void *mapPtr = NULL; |
| 681 | |
| 682 | if (mVertexBuffer) |
| 683 | { |
| 684 | HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0); |
| 685 | |
| 686 | if (FAILED(result)) |
| 687 | { |
| 688 | ERR("Lock failed with error 0x%08x", result); |
| 689 | return NULL; |
| 690 | } |
| 691 | |
| 692 | int attributeOffset = attribute.mOffset % attribute.stride(); |
| 693 | VertexElement element = {attribute.mType, attribute.mSize, attribute.mNormalized, attributeOffset, mWritePosition}; |
| 694 | mCache.push_back(element); |
| 695 | |
| 696 | *streamOffset = mWritePosition; |
| 697 | mWritePosition += requiredSpace; |
| 698 | } |
| 699 | |
| 700 | return mapPtr; |
| 701 | } |
| 702 | |
| 703 | void StaticVertexBuffer::reserveRequiredSpace() |
| 704 | { |
| 705 | if (!mVertexBuffer && mBufferSize == 0) |
| 706 | { |
daniel@transgaming.com | ee04e45 | 2011-01-08 05:46:27 +0000 | [diff] [blame] | 707 | D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY); |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 708 | HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 709 | mSerial = issueSerial(); |
| 710 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 711 | if (FAILED(result)) |
| 712 | { |
| 713 | ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace); |
| 714 | } |
| 715 | |
| 716 | mBufferSize = mRequiredSpace; |
| 717 | } |
| 718 | else if (mVertexBuffer && mBufferSize >= mRequiredSpace) |
| 719 | { |
| 720 | // Already allocated |
| 721 | } |
| 722 | else UNREACHABLE(); // Static vertex buffers can't be resized |
| 723 | |
| 724 | mRequiredSpace = 0; |
| 725 | } |
| 726 | |
daniel@transgaming.com | 0608ad1 | 2011-07-29 16:32:17 +0000 | [diff] [blame] | 727 | std::size_t StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 728 | { |
| 729 | for (unsigned int element = 0; element < mCache.size(); element++) |
| 730 | { |
| 731 | if (mCache[element].type == attribute.mType && mCache[element].size == attribute.mSize && mCache[element].normalized == attribute.mNormalized) |
| 732 | { |
| 733 | if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride()) |
| 734 | { |
| 735 | return mCache[element].streamOffset; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | return -1; |
| 741 | } |
| 742 | |
| 743 | const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const |
| 744 | { |
| 745 | return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1]; |
| 746 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 747 | } |