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