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