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