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 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 10 | #include "libANGLE/renderer/d3d/VertexDataManager.h" |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 11 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 12 | #include "libANGLE/Buffer.h" |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame^] | 13 | #include "libANGLE/formatutils.h" |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 14 | #include "libANGLE/Program.h" |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 15 | #include "libANGLE/State.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 16 | #include "libANGLE/VertexAttribute.h" |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 17 | #include "libANGLE/VertexArray.h" |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/d3d/BufferD3D.h" |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame^] | 19 | #include "libANGLE/renderer/d3d/RendererD3D.h" |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 20 | #include "libANGLE/renderer/d3d/VertexBuffer.h" |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 21 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 22 | namespace rx |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 23 | { |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame^] | 24 | namespace |
| 25 | { |
| 26 | enum |
| 27 | { |
| 28 | INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024 |
| 29 | }; |
| 30 | // This has to be at least 4k or else it fails on ATI cards. |
| 31 | enum |
| 32 | { |
| 33 | CONSTANT_VERTEX_BUFFER_SIZE = 4096 |
| 34 | }; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 35 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame^] | 36 | int ElementsInBuffer(const gl::VertexAttribute &attrib, unsigned int size) |
jbauman@chromium.org | 059fc15 | 2011-11-18 19:26:17 +0000 | [diff] [blame] | 37 | { |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 38 | // Size cannot be larger than a GLsizei |
| 39 | if (size > static_cast<unsigned int>(std::numeric_limits<int>::max())) |
| 40 | { |
| 41 | size = static_cast<unsigned int>(std::numeric_limits<int>::max()); |
| 42 | } |
| 43 | |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 44 | GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib)); |
| 45 | return (size - attrib.offset % stride + |
| 46 | (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) / |
| 47 | stride; |
jbauman@chromium.org | 059fc15 | 2011-11-18 19:26:17 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame^] | 50 | bool DirectStoragePossible(const gl::VertexAttribute &attrib) |
| 51 | { |
| 52 | // Current value attribs may not use direct storage. |
| 53 | if (!attrib.enabled) |
| 54 | { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | gl::Buffer *buffer = attrib.buffer.get(); |
| 59 | if (!buffer) |
| 60 | { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer); |
| 65 | ASSERT(bufferD3D); |
| 66 | if (!bufferD3D->supportsDirectBinding()) |
| 67 | { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | StaticVertexBufferInterface *staticBuffer = |
| 72 | bufferD3D->getStaticVertexBuffer(attrib, D3D_BUFFER_CREATE_IF_NECESSARY); |
| 73 | // Dynamic buffers can not be stored directly. |
| 74 | if (!staticBuffer) |
| 75 | { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // Alignment restrictions: In D3D, vertex data must be aligned to the format stride, or to a |
| 80 | // 4-byte boundary, whichever is smaller. (Undocumented, and experimentally confirmed) |
| 81 | size_t alignment = 4; |
| 82 | bool requiresConversion = false; |
| 83 | |
| 84 | if (attrib.type != GL_FLOAT) |
| 85 | { |
| 86 | gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib); |
| 87 | |
| 88 | unsigned int outputElementSize; |
| 89 | staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize); |
| 90 | alignment = std::min<size_t>(outputElementSize, 4); |
| 91 | |
| 92 | // TODO(jmadill): add VertexFormatCaps |
| 93 | BufferFactoryD3D *factory = bufferD3D->getFactory(); |
| 94 | requiresConversion = |
| 95 | (factory->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_CPU) != 0; |
| 96 | } |
| 97 | |
| 98 | bool isAligned = (static_cast<size_t>(ComputeVertexAttributeStride(attrib)) % alignment == 0) && |
| 99 | (static_cast<size_t>(attrib.offset) % alignment == 0); |
| 100 | |
| 101 | return !requiresConversion && isAligned; |
| 102 | } |
| 103 | } // anonymous namespace |
| 104 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 105 | VertexDataManager::CurrentValueState::CurrentValueState() |
| 106 | : buffer(nullptr), |
| 107 | offset(0) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 108 | { |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 109 | data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN(); |
| 110 | data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN(); |
| 111 | data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN(); |
| 112 | data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN(); |
| 113 | data.Type = GL_FLOAT; |
| 114 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 115 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 116 | VertexDataManager::CurrentValueState::~CurrentValueState() |
| 117 | { |
| 118 | SafeDelete(buffer); |
| 119 | } |
| 120 | |
| 121 | VertexDataManager::VertexDataManager(BufferFactoryD3D *factory) |
| 122 | : mFactory(factory), |
| 123 | mStreamingBuffer(nullptr), |
| 124 | // TODO(jmadill): use context caps |
| 125 | mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS) |
| 126 | { |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 127 | mStreamingBuffer = new StreamingVertexBufferInterface(factory, INITIAL_STREAM_BUFFER_SIZE); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 128 | |
| 129 | if (!mStreamingBuffer) |
| 130 | { |
| 131 | ERR("Failed to allocate the streaming vertex buffer."); |
| 132 | } |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 133 | |
| 134 | // TODO(jmadill): use context caps |
| 135 | mActiveEnabledAttributes.reserve(gl::MAX_VERTEX_ATTRIBS); |
| 136 | mActiveDisabledAttributes.reserve(gl::MAX_VERTEX_ATTRIBS); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | VertexDataManager::~VertexDataManager() |
| 140 | { |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 141 | SafeDelete(mStreamingBuffer); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 144 | void VertexDataManager::hintUnmapAllResources(const std::vector<gl::VertexAttribute> &vertexAttributes) |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame] | 145 | { |
| 146 | mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); |
| 147 | |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 148 | for (const TranslatedAttribute *translated : mActiveEnabledAttributes) |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame] | 149 | { |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 150 | gl::Buffer *buffer = translated->attribute->buffer.get(); |
| 151 | BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr; |
Austin Kinross | 84b0c3b | 2015-11-05 15:15:28 -0800 | [diff] [blame] | 152 | StaticVertexBufferInterface *staticBuffer = |
| 153 | storage |
| 154 | ? storage->getStaticVertexBuffer(*translated->attribute, D3D_BUFFER_DO_NOT_CREATE) |
| 155 | : nullptr; |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame] | 156 | |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 157 | if (staticBuffer) |
| 158 | { |
Austin Kinross | 84b0c3b | 2015-11-05 15:15:28 -0800 | [diff] [blame] | 159 | // Commit all the static vertex buffers. This fixes them in size/contents, and forces |
| 160 | // ANGLE to use a new static buffer (or recreate the static buffers) next time |
| 161 | staticBuffer->commit(); |
| 162 | |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 163 | staticBuffer->getVertexBuffer()->hintUnmapResource(); |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 167 | for (auto ¤tValue : mCurrentValueCache) |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame] | 168 | { |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 169 | if (currentValue.buffer != nullptr) |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame] | 170 | { |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 171 | currentValue.buffer->getVertexBuffer()->hintUnmapResource(); |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 176 | gl::Error VertexDataManager::prepareVertexData(const gl::State &state, |
| 177 | GLint start, |
| 178 | GLsizei count, |
| 179 | std::vector<TranslatedAttribute> *translatedAttribs, |
| 180 | GLsizei instances) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 181 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 182 | if (!mStreamingBuffer) |
| 183 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 184 | return gl::Error(GL_OUT_OF_MEMORY, "Internal streaming vertex buffer is unexpectedly NULL."); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 187 | // Compute active enabled and active disable attributes, for speed. |
| 188 | // TODO(jmadill): don't recompute if there was no state change |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 189 | const gl::VertexArray *vertexArray = state.getVertexArray(); |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 190 | const gl::Program *program = state.getProgram(); |
| 191 | const auto &vertexAttributes = vertexArray->getVertexAttributes(); |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 192 | |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 193 | mActiveEnabledAttributes.clear(); |
| 194 | mActiveDisabledAttributes.clear(); |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 195 | translatedAttribs->clear(); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 196 | |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 197 | for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 198 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 199 | if (program->isAttribLocationActive(attribIndex)) |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 200 | { |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 201 | // Resize automatically puts in empty attribs |
| 202 | translatedAttribs->resize(attribIndex + 1); |
| 203 | |
| 204 | TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex]; |
| 205 | |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 206 | // Record the attribute now |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 207 | translated->active = true; |
| 208 | translated->attribute = &vertexAttributes[attribIndex]; |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 209 | translated->currentValueType = |
| 210 | state.getVertexAttribCurrentValue(static_cast<unsigned int>(attribIndex)).Type; |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 211 | translated->divisor = vertexAttributes[attribIndex].divisor; |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 212 | |
| 213 | if (vertexAttributes[attribIndex].enabled) |
| 214 | { |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 215 | mActiveEnabledAttributes.push_back(translated); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 216 | |
Austin Kinross | 84b0c3b | 2015-11-05 15:15:28 -0800 | [diff] [blame] | 217 | gl::Buffer *buffer = vertexAttributes[attribIndex].buffer.get(); |
| 218 | if (buffer) |
| 219 | { |
| 220 | // Also reinitialize static buffers which didn't contain matching data |
| 221 | // last time they were used |
| 222 | BufferD3D *bufferImpl = GetImplAs<BufferD3D>(buffer); |
| 223 | bufferImpl->reinitOutOfDateStaticData(); |
| 224 | } |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 225 | } |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 226 | else |
| 227 | { |
| 228 | mActiveDisabledAttributes.push_back(attribIndex); |
| 229 | } |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | // Reserve the required space in the buffers |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 234 | for (const TranslatedAttribute *activeAttrib : mActiveEnabledAttributes) |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 235 | { |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 236 | gl::Error error = reserveSpaceForAttrib(*activeAttrib, count, instances); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 237 | if (error.isError()) |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 238 | { |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 239 | return error; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | // Perform the vertex data translations |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 244 | for (TranslatedAttribute *activeAttrib : mActiveEnabledAttributes) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 245 | { |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 246 | gl::Error error = storeAttribute(activeAttrib, start, count, instances); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 247 | |
| 248 | if (error.isError()) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 249 | { |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 250 | hintUnmapAllResources(vertexAttributes); |
| 251 | return error; |
| 252 | } |
| 253 | } |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 254 | |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 255 | for (size_t attribIndex : mActiveDisabledAttributes) |
| 256 | { |
| 257 | if (mCurrentValueCache[attribIndex].buffer == nullptr) |
| 258 | { |
| 259 | mCurrentValueCache[attribIndex].buffer = new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE); |
| 260 | } |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 261 | |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 262 | gl::Error error = storeCurrentValue( |
| 263 | state.getVertexAttribCurrentValue(static_cast<unsigned int>(attribIndex)), |
| 264 | &(*translatedAttribs)[attribIndex], &mCurrentValueCache[attribIndex]); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 265 | if (error.isError()) |
| 266 | { |
| 267 | hintUnmapAllResources(vertexAttributes); |
| 268 | return error; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 271 | |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame] | 272 | // Hint to unmap all the resources |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 273 | hintUnmapAllResources(vertexAttributes); |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame] | 274 | |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 275 | for (const TranslatedAttribute *activeAttrib : mActiveEnabledAttributes) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 276 | { |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 277 | gl::Buffer *buffer = activeAttrib->attribute->buffer.get(); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 278 | |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 279 | if (buffer) |
| 280 | { |
| 281 | BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer); |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 282 | size_t typeSize = ComputeVertexAttributeTypeSize(*activeAttrib->attribute); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 283 | bufferD3D->promoteStaticUsage(count * static_cast<int>(typeSize)); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 287 | return gl::Error(GL_NO_ERROR); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Jamie Madill | 3d72cc7 | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 290 | gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib, |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 291 | GLsizei count, |
| 292 | GLsizei instances) const |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 293 | { |
Jamie Madill | 3d72cc7 | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 294 | const gl::VertexAttribute &attrib = *translatedAttrib.attribute; |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 295 | gl::Buffer *buffer = attrib.buffer.get(); |
Jamie Madill | 9236b41 | 2015-02-02 16:51:52 -0500 | [diff] [blame] | 296 | BufferD3D *bufferImpl = buffer ? GetImplAs<BufferD3D>(buffer) : NULL; |
Austin Kinross | 84b0c3b | 2015-11-05 15:15:28 -0800 | [diff] [blame] | 297 | StaticVertexBufferInterface *staticBuffer = |
| 298 | bufferImpl ? bufferImpl->getStaticVertexBuffer(attrib, D3D_BUFFER_CREATE_IF_NECESSARY) |
| 299 | : NULL; |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 300 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame^] | 301 | if (!DirectStoragePossible(attrib)) |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 302 | { |
| 303 | if (staticBuffer) |
| 304 | { |
| 305 | if (staticBuffer->getBufferSize() == 0) |
| 306 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 307 | int totalCount = |
| 308 | ElementsInBuffer(attrib, static_cast<unsigned int>(bufferImpl->getSize())); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 309 | gl::Error error = staticBuffer->reserveVertexSpace(attrib, totalCount, 0); |
| 310 | if (error.isError()) |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 311 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 312 | return error; |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | else |
| 317 | { |
Geoff Lang | 3cf12ce | 2015-08-27 14:40:48 -0400 | [diff] [blame] | 318 | size_t totalCount = ComputeVertexAttributeElementCount(attrib, count, instances); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 319 | ASSERT(!bufferImpl || |
| 320 | ElementsInBuffer(attrib, static_cast<unsigned int>(bufferImpl->getSize())) >= |
Jamie Madill | 846f107 | 2015-09-01 09:07:15 -0400 | [diff] [blame] | 321 | static_cast<int>(totalCount)); |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 322 | |
Geoff Lang | 3cf12ce | 2015-08-27 14:40:48 -0400 | [diff] [blame] | 323 | gl::Error error = mStreamingBuffer->reserveVertexSpace( |
| 324 | attrib, static_cast<GLsizei>(totalCount), instances); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 325 | if (error.isError()) |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 326 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 327 | return error; |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 332 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 333 | } |
| 334 | |
Jamie Madill | 3d72cc7 | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 335 | gl::Error VertexDataManager::storeAttribute(TranslatedAttribute *translated, |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 336 | GLint start, |
| 337 | GLsizei count, |
| 338 | GLsizei instances) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 339 | { |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 340 | const gl::VertexAttribute &attrib = *translated->attribute; |
| 341 | |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 342 | gl::Buffer *buffer = attrib.buffer.get(); |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 343 | ASSERT(buffer || attrib.pointer); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 344 | ASSERT(attrib.enabled); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 345 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame^] | 346 | BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr; |
| 347 | auto *staticBuffer = |
| 348 | storage ? storage->getStaticVertexBuffer(attrib, D3D_BUFFER_DO_NOT_CREATE) : nullptr; |
| 349 | auto *vertexBuffer = |
| 350 | staticBuffer ? staticBuffer : static_cast<VertexBufferInterface *>(mStreamingBuffer); |
| 351 | translated->vertexBuffer = vertexBuffer->getVertexBuffer(); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 352 | |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 353 | // Instanced vertices do not apply the 'start' offset |
Jamie Madill | a053733 | 2015-11-19 13:55:26 -0500 | [diff] [blame] | 354 | GLint firstVertexIndex = (attrib.divisor > 0 ? 0 : start); |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 355 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame^] | 356 | if (DirectStoragePossible(attrib)) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 357 | { |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 358 | translated->storage = storage; |
| 359 | translated->serial = storage->getSerial(); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 360 | translated->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib)); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 361 | translated->offset = static_cast<unsigned int>(attrib.offset + translated->stride * firstVertexIndex); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 362 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 363 | } |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 364 | |
| 365 | // Compute source data pointer |
| 366 | const uint8_t *sourceData = nullptr; |
| 367 | |
| 368 | if (buffer) |
| 369 | { |
| 370 | gl::Error error = storage->getData(&sourceData); |
| 371 | if (error.isError()) |
| 372 | { |
| 373 | return error; |
| 374 | } |
| 375 | sourceData += static_cast<int>(attrib.offset); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | sourceData = static_cast<const uint8_t*>(attrib.pointer); |
| 380 | } |
| 381 | |
| 382 | unsigned int streamOffset = 0; |
| 383 | unsigned int outputElementSize = 0; |
| 384 | |
| 385 | if (staticBuffer) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 386 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 387 | gl::Error error = staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize); |
| 388 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 389 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 390 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | if (!staticBuffer->lookupAttribute(attrib, &streamOffset)) |
| 394 | { |
| 395 | // Convert the entire buffer |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 396 | int totalCount = |
| 397 | ElementsInBuffer(attrib, static_cast<unsigned int>(storage->getSize())); |
| 398 | int startIndex = static_cast<int>(attrib.offset) / |
| 399 | static_cast<int>(ComputeVertexAttributeStride(attrib)); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 400 | |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 401 | error = staticBuffer->storeVertexAttributes(attrib, |
Jamie Madill | 3d72cc7 | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 402 | translated->currentValueType, |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 403 | -startIndex, |
| 404 | totalCount, |
| 405 | 0, |
| 406 | &streamOffset, |
| 407 | sourceData); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 408 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 409 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 410 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 414 | unsigned int firstElementOffset = |
| 415 | (static_cast<unsigned int>(attrib.offset) / |
| 416 | static_cast<unsigned int>(ComputeVertexAttributeStride(attrib))) * |
| 417 | outputElementSize; |
Jamie Madill | a053733 | 2015-11-19 13:55:26 -0500 | [diff] [blame] | 418 | ASSERT(attrib.divisor == 0 || firstVertexIndex == 0); |
| 419 | unsigned int startOffset = firstVertexIndex * outputElementSize; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 420 | if (streamOffset + firstElementOffset + startOffset < streamOffset) |
| 421 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 422 | return gl::Error(GL_OUT_OF_MEMORY); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | streamOffset += firstElementOffset + startOffset; |
| 426 | } |
| 427 | else |
| 428 | { |
Geoff Lang | 3cf12ce | 2015-08-27 14:40:48 -0400 | [diff] [blame] | 429 | size_t totalCount = ComputeVertexAttributeElementCount(attrib, count, instances); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 430 | gl::Error error = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize); |
| 431 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 432 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 433 | return error; |
| 434 | } |
| 435 | |
Geoff Lang | 3cf12ce | 2015-08-27 14:40:48 -0400 | [diff] [blame] | 436 | error = mStreamingBuffer->storeVertexAttributes( |
| 437 | attrib, translated->currentValueType, firstVertexIndex, |
| 438 | static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 439 | if (error.isError()) |
| 440 | { |
| 441 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 445 | translated->storage = nullptr; |
| 446 | translated->serial = vertexBuffer->getSerial(); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 447 | translated->stride = outputElementSize; |
| 448 | translated->offset = streamOffset; |
| 449 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 450 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 451 | } |
| 452 | |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 453 | gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData ¤tValue, |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 454 | TranslatedAttribute *translated, |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 455 | CurrentValueState *cachedState) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 456 | { |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 457 | if (cachedState->data != currentValue) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 458 | { |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 459 | const gl::VertexAttribute &attrib = *translated->attribute; |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 460 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 461 | gl::Error error = cachedState->buffer->reserveVertexSpace(attrib, 1, 0); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 462 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 463 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 464 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 465 | } |
| 466 | |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 467 | const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 468 | unsigned int streamOffset; |
Jamie Madill | 3d72cc7 | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 469 | error = cachedState->buffer->storeVertexAttributes(attrib, currentValue.Type, 0, 1, 0, &streamOffset, sourceData); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 470 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 471 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 472 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 473 | } |
| 474 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 475 | cachedState->data = currentValue; |
| 476 | cachedState->offset = streamOffset; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | translated->storage = NULL; |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 480 | translated->vertexBuffer = cachedState->buffer->getVertexBuffer(); |
| 481 | translated->serial = cachedState->buffer->getSerial(); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 482 | translated->divisor = 0; |
| 483 | |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 484 | translated->stride = 0; |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 485 | translated->offset = static_cast<unsigned int>(cachedState->offset); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 486 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 487 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 488 | } |
| 489 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame^] | 490 | } // namespace rx |