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 | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 10 | #include "libGLESv2/renderer/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 | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 14 | #include "libGLESv2/renderer/Renderer9.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 15 | #include "libGLESv2/Buffer.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 | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 19 | #include "libGLESv2/renderer/vertexconversion.h" |
| 20 | #include "libGLESv2/renderer/IndexDataManager.h" |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 21 | |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 22 | #include <limits> |
| 23 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 24 | namespace |
| 25 | { |
| 26 | enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 }; |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 27 | // This has to be at least 4k or else it fails on ATI cards. |
| 28 | enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 }; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 29 | } |
| 30 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 31 | namespace rx |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 32 | { |
| 33 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 34 | int elementsInBuffer(const gl::VertexAttribute &attribute, int size) |
jbauman@chromium.org | 059fc15 | 2011-11-18 19:26:17 +0000 | [diff] [blame] | 35 | { |
| 36 | int stride = attribute.stride(); |
| 37 | return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride; |
| 38 | } |
| 39 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 40 | VertexDataManager::VertexDataManager(Renderer9 *renderer) : mRenderer(renderer) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 41 | { |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 42 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 43 | { |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 44 | mCurrentValue[i][0] = std::numeric_limits<float>::quiet_NaN(); |
| 45 | mCurrentValue[i][1] = std::numeric_limits<float>::quiet_NaN(); |
| 46 | mCurrentValue[i][2] = std::numeric_limits<float>::quiet_NaN(); |
| 47 | mCurrentValue[i][3] = std::numeric_limits<float>::quiet_NaN(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 48 | mCurrentValueBuffer[i] = NULL; |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 49 | mCurrentValueOffsets[i] = 0; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 50 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 51 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 52 | // D3D9_REPLACE |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 53 | checkVertexCaps(renderer->getCapsDeclTypes()); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 54 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 55 | mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 56 | |
| 57 | if (!mStreamingBuffer) |
| 58 | { |
| 59 | ERR("Failed to allocate the streaming vertex buffer."); |
| 60 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | VertexDataManager::~VertexDataManager() |
| 64 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 65 | delete mStreamingBuffer; |
| 66 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 67 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 68 | { |
| 69 | delete mCurrentValueBuffer[i]; |
| 70 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 71 | } |
| 72 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 73 | std::size_t VertexDataManager::writeAttributeData(VertexBufferInterface *vertexBuffer, GLint start, GLsizei count, const gl::VertexAttribute &attribute, GLsizei instances) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 74 | { |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 75 | gl::Buffer *buffer = attribute.mBoundBuffer.get(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 76 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 77 | int inputStride = attribute.stride(); |
| 78 | int elementSize = attribute.typeSize(); |
| 79 | const FormatConverter &converter = formatConverter(attribute); |
daniel@transgaming.com | 58f76fe | 2011-06-21 14:21:07 +0000 | [diff] [blame] | 80 | std::size_t streamOffset = 0; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 81 | |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 82 | void *output = NULL; |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 83 | |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 84 | if (vertexBuffer) |
| 85 | { |
daniel@transgaming.com | 2fc9f90 | 2012-01-27 15:39:00 +0000 | [diff] [blame] | 86 | output = vertexBuffer->map(attribute, spaceRequired(attribute, count, instances), &streamOffset); |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 87 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 88 | |
| 89 | if (output == NULL) |
| 90 | { |
| 91 | ERR("Failed to map vertex buffer."); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | const char *input = NULL; |
| 96 | |
| 97 | if (buffer) |
| 98 | { |
| 99 | int offset = attribute.mOffset; |
| 100 | |
| 101 | input = static_cast<const char*>(buffer->data()) + offset; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | input = static_cast<const char*>(attribute.mPointer); |
| 106 | } |
| 107 | |
daniel@transgaming.com | c41a6fe | 2012-01-27 15:39:04 +0000 | [diff] [blame] | 108 | if (instances == 0 || attribute.mDivisor == 0) |
| 109 | { |
| 110 | input += inputStride * start; |
| 111 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 112 | |
| 113 | if (converter.identity && inputStride == elementSize) |
| 114 | { |
| 115 | memcpy(output, input, count * inputStride); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | converter.convertArray(input, inputStride, count, output); |
| 120 | } |
| 121 | |
| 122 | vertexBuffer->unmap(); |
| 123 | |
| 124 | return streamOffset; |
| 125 | } |
| 126 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 127 | GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 128 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 129 | if (!mStreamingBuffer) |
| 130 | { |
| 131 | return GL_OUT_OF_MEMORY; |
| 132 | } |
| 133 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 134 | for (int attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 135 | { |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 136 | translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 137 | } |
| 138 | |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 139 | // Determine the required storage size per used buffer, and invalidate static buffers that don't contain matching attributes |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 140 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 141 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 142 | if (translated[i].active && attribs[i].mArrayEnabled) |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 143 | { |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 144 | gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 145 | StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 146 | |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 147 | if (staticBuffer) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 148 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 149 | if (staticBuffer->size() == 0) |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 150 | { |
jbauman@chromium.org | 059fc15 | 2011-11-18 19:26:17 +0000 | [diff] [blame] | 151 | int totalCount = elementsInBuffer(attribs[i], buffer->size()); |
daniel@transgaming.com | 2fc9f90 | 2012-01-27 15:39:00 +0000 | [diff] [blame] | 152 | staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount, 0)); |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 153 | } |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 154 | else if (staticBuffer->lookupAttribute(attribs[i]) == -1) |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 155 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 156 | // 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] | 157 | // Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer |
| 158 | for (int previous = 0; previous < i; previous++) |
| 159 | { |
| 160 | if (translated[previous].active && attribs[previous].mArrayEnabled) |
| 161 | { |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 162 | gl::Buffer *previousBuffer = attribs[previous].mBoundBuffer.get(); |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 163 | StaticVertexBufferInterface *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL; |
daniel@transgaming.com | b0eb697 | 2011-07-08 16:23:42 +0000 | [diff] [blame] | 164 | |
| 165 | if (staticBuffer == previousStaticBuffer) |
| 166 | { |
daniel@transgaming.com | 2fc9f90 | 2012-01-27 15:39:00 +0000 | [diff] [blame] | 167 | mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count, instances)); |
daniel@transgaming.com | b0eb697 | 2011-07-08 16:23:42 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
daniel@transgaming.com | 2fc9f90 | 2012-01-27 15:39:00 +0000 | [diff] [blame] | 172 | mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances)); |
daniel@transgaming.com | cb325c8 | 2011-08-02 12:34:36 +0000 | [diff] [blame] | 173 | |
| 174 | buffer->invalidateStaticData(); |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 175 | } |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
daniel@transgaming.com | 2fc9f90 | 2012-01-27 15:39:00 +0000 | [diff] [blame] | 179 | mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances)); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Reserve the required space per used buffer |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 185 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 186 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 187 | if (translated[i].active && attribs[i].mArrayEnabled) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 188 | { |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 189 | gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 190 | VertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; |
| 191 | VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 192 | |
daniel@transgaming.com | 5ee2ad0 | 2011-01-08 05:46:20 +0000 | [diff] [blame] | 193 | if (vertexBuffer) |
| 194 | { |
| 195 | vertexBuffer->reserveRequiredSpace(); |
| 196 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | // Perform the vertex data translations |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 201 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 202 | { |
| 203 | if (translated[i].active) |
| 204 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 205 | if (attribs[i].mArrayEnabled) |
| 206 | { |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 207 | gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 208 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 209 | if (!buffer && attribs[i].mPointer == NULL) |
| 210 | { |
| 211 | // This is an application error that would normally result in a crash, but we catch it and return an error |
| 212 | ERR("An enabled vertex array has no buffer and no pointer."); |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 213 | return GL_INVALID_OPERATION; |
| 214 | } |
| 215 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 216 | const FormatConverter &converter = formatConverter(attribs[i]); |
| 217 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 218 | StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; |
| 219 | VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 220 | |
daniel@transgaming.com | 58f76fe | 2011-06-21 14:21:07 +0000 | [diff] [blame] | 221 | std::size_t streamOffset = -1; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 222 | |
| 223 | if (staticBuffer) |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 224 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 225 | streamOffset = staticBuffer->lookupAttribute(attribs[i]); |
| 226 | |
| 227 | if (streamOffset == -1) |
| 228 | { |
| 229 | // Convert the entire buffer |
jbauman@chromium.org | 059fc15 | 2011-11-18 19:26:17 +0000 | [diff] [blame] | 230 | int totalCount = elementsInBuffer(attribs[i], buffer->size()); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 231 | int startIndex = attribs[i].mOffset / attribs[i].stride(); |
| 232 | |
daniel@transgaming.com | 2fc9f90 | 2012-01-27 15:39:00 +0000 | [diff] [blame] | 233 | streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i], 0); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | if (streamOffset != -1) |
| 237 | { |
daniel@transgaming.com | c41a6fe | 2012-01-27 15:39:04 +0000 | [diff] [blame] | 238 | streamOffset += (attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize; |
| 239 | |
| 240 | if (instances == 0 || attribs[i].mDivisor == 0) |
| 241 | { |
| 242 | streamOffset += start * converter.outputElementSize; |
| 243 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | else |
| 247 | { |
daniel@transgaming.com | 2fc9f90 | 2012-01-27 15:39:00 +0000 | [diff] [blame] | 248 | streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i], instances); |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 249 | } |
| 250 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 251 | if (streamOffset == -1) |
| 252 | { |
| 253 | return GL_OUT_OF_MEMORY; |
| 254 | } |
| 255 | |
| 256 | translated[i].vertexBuffer = vertexBuffer->getBuffer(); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 257 | translated[i].serial = vertexBuffer->getSerial(); |
daniel@transgaming.com | 8ca9c6e | 2012-01-27 15:38:54 +0000 | [diff] [blame] | 258 | translated[i].divisor = attribs[i].mDivisor; |
| 259 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 260 | translated[i].type = converter.d3dDeclType; |
| 261 | translated[i].stride = converter.outputElementSize; |
| 262 | translated[i].offset = streamOffset; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 263 | } |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 264 | else |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 265 | { |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 266 | if (!mCurrentValueBuffer[i]) |
| 267 | { |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 268 | mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE); |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 269 | } |
| 270 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 271 | StreamingVertexBufferInterface *buffer = mCurrentValueBuffer[i]; |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 272 | |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 273 | if (mCurrentValue[i][0] != attribs[i].mCurrentValue[0] || |
| 274 | mCurrentValue[i][1] != attribs[i].mCurrentValue[1] || |
| 275 | mCurrentValue[i][2] != attribs[i].mCurrentValue[2] || |
| 276 | mCurrentValue[i][3] != attribs[i].mCurrentValue[3]) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 277 | { |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 278 | const int requiredSpace = 4 * sizeof(float); |
| 279 | buffer->addRequiredSpace(requiredSpace); |
| 280 | buffer->reserveRequiredSpace(); |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 281 | float *data = static_cast<float*>(buffer->map(gl::VertexAttribute(), requiredSpace, &mCurrentValueOffsets[i])); |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 282 | if (data) |
| 283 | { |
| 284 | data[0] = attribs[i].mCurrentValue[0]; |
| 285 | data[1] = attribs[i].mCurrentValue[1]; |
| 286 | data[2] = attribs[i].mCurrentValue[2]; |
| 287 | data[3] = attribs[i].mCurrentValue[3]; |
| 288 | buffer->unmap(); |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 289 | |
| 290 | mCurrentValue[i][0] = attribs[i].mCurrentValue[0]; |
| 291 | mCurrentValue[i][1] = attribs[i].mCurrentValue[1]; |
| 292 | mCurrentValue[i][2] = attribs[i].mCurrentValue[2]; |
| 293 | mCurrentValue[i][3] = attribs[i].mCurrentValue[3]; |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 294 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer(); |
jbauman@chromium.org | d8f3faa | 2011-09-02 01:10:47 +0000 | [diff] [blame] | 298 | translated[i].serial = mCurrentValueBuffer[i]->getSerial(); |
daniel@transgaming.com | 8ca9c6e | 2012-01-27 15:38:54 +0000 | [diff] [blame] | 299 | translated[i].divisor = 0; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 300 | |
| 301 | translated[i].type = D3DDECLTYPE_FLOAT4; |
| 302 | translated[i].stride = 0; |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 303 | translated[i].offset = mCurrentValueOffsets[i]; |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 304 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 307 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 308 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 309 | { |
| 310 | if (translated[i].active && attribs[i].mArrayEnabled) |
| 311 | { |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 312 | gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 313 | |
| 314 | if (buffer) |
| 315 | { |
| 316 | buffer->promoteStaticUsage(count * attribs[i].typeSize()); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 321 | return GL_NO_ERROR; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 322 | } |
| 323 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 324 | std::size_t VertexDataManager::spaceRequired(const gl::VertexAttribute &attrib, std::size_t count, GLsizei instances) const |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 325 | { |
daniel@transgaming.com | 2fc9f90 | 2012-01-27 15:39:00 +0000 | [diff] [blame] | 326 | size_t elementSize = formatConverter(attrib).outputElementSize; |
| 327 | |
| 328 | if (instances == 0 || attrib.mDivisor == 0) |
| 329 | { |
| 330 | return elementSize * count; |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | return elementSize * ((instances + attrib.mDivisor - 1) / attrib.mDivisor); |
| 335 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // Mapping from OpenGL-ES vertex attrib type to D3D decl type: |
| 339 | // |
| 340 | // BYTE SHORT (Cast) |
| 341 | // BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm) |
| 342 | // UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast) |
| 343 | // UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize) |
| 344 | // SHORT SHORT (Identity) |
| 345 | // SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize) |
| 346 | // UNSIGNED_SHORT FLOAT (Cast) |
| 347 | // UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize) |
| 348 | // FIXED (not in WebGL) FLOAT (FixedToFloat) |
| 349 | // FLOAT FLOAT (Identity) |
| 350 | |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 351 | // GLToCType maps from GL type (as GLenum) to the C typedef. |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 352 | template <GLenum GLType> struct GLToCType { }; |
| 353 | |
| 354 | template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; }; |
| 355 | template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; }; |
| 356 | template <> struct GLToCType<GL_SHORT> { typedef GLshort type; }; |
| 357 | template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; }; |
| 358 | template <> struct GLToCType<GL_FIXED> { typedef GLuint type; }; |
| 359 | template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; }; |
| 360 | |
| 361 | // This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.) |
| 362 | enum D3DVertexType |
| 363 | { |
| 364 | D3DVT_FLOAT, |
| 365 | D3DVT_SHORT, |
| 366 | D3DVT_SHORT_NORM, |
| 367 | D3DVT_UBYTE, |
| 368 | D3DVT_UBYTE_NORM, |
| 369 | D3DVT_USHORT_NORM |
| 370 | }; |
| 371 | |
| 372 | // D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type. |
| 373 | template <unsigned int D3DType> struct D3DToCType { }; |
| 374 | |
| 375 | template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; }; |
| 376 | template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; }; |
| 377 | template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; }; |
| 378 | template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; }; |
| 379 | template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; }; |
| 380 | template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; }; |
| 381 | |
| 382 | // Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size. |
| 383 | template <unsigned int type, int size> |
| 384 | struct WidenRule |
| 385 | { |
| 386 | }; |
| 387 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 388 | template <int size> struct WidenRule<D3DVT_FLOAT, size> : NoWiden<size> { }; |
| 389 | template <int size> struct WidenRule<D3DVT_SHORT, size> : WidenToEven<size> { }; |
| 390 | template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : WidenToEven<size> { }; |
| 391 | template <int size> struct WidenRule<D3DVT_UBYTE, size> : WidenToFour<size> { }; |
| 392 | template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : WidenToFour<size> { }; |
| 393 | template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : WidenToEven<size> { }; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 394 | |
| 395 | // VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination. |
| 396 | template <unsigned int d3dtype, int size> |
| 397 | struct VertexTypeFlags |
| 398 | { |
| 399 | }; |
| 400 | |
daniel@transgaming.com | 29ab952 | 2012-08-27 16:25:37 +0000 | [diff] [blame] | 401 | template <unsigned int _capflag, unsigned int _declflag> |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 402 | struct VertexTypeFlagsHelper |
| 403 | { |
daniel@transgaming.com | 29ab952 | 2012-08-27 16:25:37 +0000 | [diff] [blame] | 404 | enum { capflag = _capflag }; |
| 405 | enum { declflag = _declflag }; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 406 | }; |
| 407 | |
| 408 | template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { }; |
| 409 | template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { }; |
| 410 | template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { }; |
| 411 | template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { }; |
| 412 | template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { }; |
| 413 | template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { }; |
| 414 | template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { }; |
| 415 | template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { }; |
| 416 | template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { }; |
| 417 | template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { }; |
| 418 | template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { }; |
| 419 | template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { }; |
| 420 | |
| 421 | |
| 422 | // VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums). |
| 423 | template <GLenum GLtype, bool normalized> |
| 424 | struct VertexTypeMapping |
| 425 | { |
| 426 | }; |
| 427 | |
| 428 | template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred> |
| 429 | struct VertexTypeMappingBase |
| 430 | { |
| 431 | enum { preferred = Preferred }; |
| 432 | enum { fallback = Fallback }; |
| 433 | }; |
| 434 | |
| 435 | template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast |
| 436 | template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize |
| 437 | template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast |
| 438 | template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize |
| 439 | template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity |
| 440 | template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize |
| 441 | template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast |
| 442 | template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize |
| 443 | template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat |
| 444 | template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity |
| 445 | |
| 446 | |
| 447 | // Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat). |
| 448 | // The conversion rules themselves are defined in vertexconversion.h. |
| 449 | |
| 450 | // Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping). |
| 451 | template <GLenum fromType, bool normalized, unsigned int toType> |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 452 | struct ConversionRule : Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type> |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 453 | { |
| 454 | }; |
| 455 | |
| 456 | // All conversions from normalized types to float use the Normalize operator. |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 457 | template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : Normalize<typename GLToCType<fromType>::type> { }; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 458 | |
| 459 | // Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules. |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 460 | template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { }; |
| 461 | template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { }; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 462 | |
| 463 | // A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1) |
| 464 | // whether it is normalized or not. |
| 465 | template <class T, bool normalized> |
| 466 | struct DefaultVertexValuesStage2 |
| 467 | { |
| 468 | }; |
| 469 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 470 | template <class T> struct DefaultVertexValuesStage2<T, true> : NormalizedDefaultValues<T> { }; |
| 471 | template <class T> struct DefaultVertexValuesStage2<T, false> : SimpleDefaultValues<T> { }; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 472 | |
daniel@transgaming.com | 50aadb0 | 2012-11-28 21:06:11 +0000 | [diff] [blame] | 473 | // Work out the default value rule for a D3D type (expressed as the C type) and |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 474 | template <class T, bool normalized> |
| 475 | struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized> |
| 476 | { |
| 477 | }; |
| 478 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 479 | template <bool normalized> struct DefaultVertexValues<float, normalized> : SimpleDefaultValues<float> { }; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 480 | |
| 481 | // Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion. |
| 482 | // The fallback conversion produces an output that all D3D9 devices must support. |
| 483 | template <class T> struct UsePreferred { enum { type = T::preferred }; }; |
| 484 | template <class T> struct UseFallback { enum { type = T::fallback }; }; |
| 485 | |
| 486 | // Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion, |
| 487 | // it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag |
| 488 | // and the D3DDECLTYPE member needed for the vertex declaration in declflag. |
| 489 | template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule> |
| 490 | struct Converter |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 491 | : VertexDataConverter<typename GLToCType<fromType>::type, |
| 492 | WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>, |
| 493 | ConversionRule<fromType, |
| 494 | normalized, |
| 495 | PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>, |
| 496 | DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > > |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 497 | { |
| 498 | private: |
| 499 | enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type }; |
| 500 | enum { d3dsize = WidenRule<d3dtype, size>::finalWidth }; |
| 501 | |
| 502 | public: |
| 503 | enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag }; |
| 504 | enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag }; |
| 505 | }; |
| 506 | |
| 507 | // Initialise a TranslationInfo |
| 508 | #define TRANSLATION(type, norm, size, preferred) \ |
| 509 | { \ |
| 510 | Converter<type, norm, size, preferred>::identity, \ |
| 511 | Converter<type, norm, size, preferred>::finalSize, \ |
| 512 | Converter<type, norm, size, preferred>::convertArray, \ |
| 513 | static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \ |
| 514 | } |
| 515 | |
| 516 | #define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \ |
| 517 | { \ |
| 518 | Converter<type, norm, size, UsePreferred>::capflag, \ |
| 519 | TRANSLATION(type, norm, size, UsePreferred), \ |
| 520 | TRANSLATION(type, norm, size, UseFallback) \ |
| 521 | } |
| 522 | |
| 523 | #define TRANSLATIONS_FOR_TYPE(type) \ |
| 524 | { \ |
| 525 | { 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) }, \ |
| 526 | { 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) }, \ |
| 527 | } |
| 528 | |
daniel@transgaming.com | cb37afd | 2012-02-01 18:10:40 +0000 | [diff] [blame] | 529 | #define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \ |
| 530 | { \ |
| 531 | { 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) }, \ |
| 532 | { 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) }, \ |
| 533 | } |
| 534 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 535 | const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1] |
| 536 | { |
| 537 | TRANSLATIONS_FOR_TYPE(GL_BYTE), |
| 538 | TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE), |
| 539 | TRANSLATIONS_FOR_TYPE(GL_SHORT), |
| 540 | TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT), |
daniel@transgaming.com | cb37afd | 2012-02-01 18:10:40 +0000 | [diff] [blame] | 541 | TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED), |
| 542 | TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 543 | }; |
| 544 | |
| 545 | void VertexDataManager::checkVertexCaps(DWORD declTypes) |
| 546 | { |
| 547 | for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++) |
| 548 | { |
| 549 | for (unsigned int j = 0; j < 2; j++) |
| 550 | { |
| 551 | for (unsigned int k = 0; k < 4; k++) |
| 552 | { |
| 553 | if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0) |
| 554 | { |
| 555 | mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion; |
| 556 | } |
| 557 | else |
| 558 | { |
| 559 | mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | // This is used to index mAttributeTypes and mPossibleTranslations. |
| 567 | unsigned int VertexDataManager::typeIndex(GLenum type) const |
| 568 | { |
| 569 | switch (type) |
| 570 | { |
| 571 | case GL_BYTE: return 0; |
| 572 | case GL_UNSIGNED_BYTE: return 1; |
| 573 | case GL_SHORT: return 2; |
| 574 | case GL_UNSIGNED_SHORT: return 3; |
| 575 | case GL_FIXED: return 4; |
| 576 | case GL_FLOAT: return 5; |
| 577 | |
| 578 | default: UNREACHABLE(); return 5; |
| 579 | } |
| 580 | } |
| 581 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 582 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 583 | const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const gl::VertexAttribute &attribute) const |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 584 | { |
| 585 | return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1]; |
| 586 | } |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 587 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 588 | } |