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