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