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