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