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