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 | |
Jamie Madill | 20e005b | 2017-04-07 14:19:22 -0400 | [diff] [blame] | 12 | #include "common/bitset_utils.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 13 | #include "libANGLE/Buffer.h" |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 14 | #include "libANGLE/formatutils.h" |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 15 | #include "libANGLE/Program.h" |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 16 | #include "libANGLE/State.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 17 | #include "libANGLE/VertexAttribute.h" |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 18 | #include "libANGLE/VertexArray.h" |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 19 | #include "libANGLE/renderer/d3d/BufferD3D.h" |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 20 | #include "libANGLE/renderer/d3d/RendererD3D.h" |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 21 | #include "libANGLE/renderer/d3d/VertexBuffer.h" |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 22 | |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 23 | using namespace angle; |
| 24 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 25 | namespace rx |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 26 | { |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 27 | namespace |
| 28 | { |
| 29 | enum |
| 30 | { |
| 31 | INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024 |
| 32 | }; |
| 33 | // This has to be at least 4k or else it fails on ATI cards. |
| 34 | enum |
| 35 | { |
| 36 | CONSTANT_VERTEX_BUFFER_SIZE = 4096 |
| 37 | }; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 38 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 39 | // Warning: you should ensure binding really matches attrib.bindingIndex before using this function. |
| 40 | int ElementsInBuffer(const gl::VertexAttribute &attrib, |
| 41 | const gl::VertexBinding &binding, |
| 42 | unsigned int size) |
jbauman@chromium.org | 059fc15 | 2011-11-18 19:26:17 +0000 | [diff] [blame] | 43 | { |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 44 | // Size cannot be larger than a GLsizei |
| 45 | if (size > static_cast<unsigned int>(std::numeric_limits<int>::max())) |
| 46 | { |
| 47 | size = static_cast<unsigned int>(std::numeric_limits<int>::max()); |
| 48 | } |
| 49 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 50 | GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib, binding)); |
| 51 | GLsizei offset = static_cast<GLsizei>(ComputeVertexAttributeOffset(attrib, binding)); |
| 52 | return (size - offset % stride + |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 53 | (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) / |
| 54 | stride; |
jbauman@chromium.org | 059fc15 | 2011-11-18 19:26:17 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 57 | // Warning: you should ensure binding really matches attrib.bindingIndex before using this function. |
| 58 | bool DirectStoragePossible(const gl::VertexAttribute &attrib, const gl::VertexBinding &binding) |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 59 | { |
| 60 | // Current value attribs may not use direct storage. |
| 61 | if (!attrib.enabled) |
| 62 | { |
| 63 | return false; |
| 64 | } |
| 65 | |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 66 | gl::Buffer *buffer = binding.getBuffer().get(); |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 67 | if (!buffer) |
| 68 | { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer); |
| 73 | ASSERT(bufferD3D); |
| 74 | if (!bufferD3D->supportsDirectBinding()) |
| 75 | { |
| 76 | return false; |
| 77 | } |
| 78 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 79 | // Alignment restrictions: In D3D, vertex data must be aligned to the format stride, or to a |
| 80 | // 4-byte boundary, whichever is smaller. (Undocumented, and experimentally confirmed) |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 81 | size_t alignment = 4; |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 82 | |
Jamie Madill | e16a451 | 2017-03-07 20:46:40 -0500 | [diff] [blame] | 83 | // TODO(jmadill): add VertexFormatCaps |
| 84 | BufferFactoryD3D *factory = bufferD3D->getFactory(); |
| 85 | |
| 86 | gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib); |
| 87 | |
| 88 | // CPU-converted vertex data must be converted (naturally). |
| 89 | if ((factory->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_CPU) != 0) |
| 90 | { |
| 91 | return false; |
| 92 | } |
| 93 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 94 | if (attrib.type != GL_FLOAT) |
| 95 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 96 | auto errorOrElementSize = factory->getVertexSpaceRequired(attrib, binding, 1, 0); |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 97 | if (errorOrElementSize.isError()) |
| 98 | { |
Yuly Novikov | d73f852 | 2017-01-13 17:48:57 -0500 | [diff] [blame] | 99 | ERR() << "Unlogged error in DirectStoragePossible."; |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
| 103 | alignment = std::min<size_t>(errorOrElementSize.getResult(), 4); |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 106 | GLintptr offset = ComputeVertexAttributeOffset(attrib, binding); |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 107 | // Final alignment check - unaligned data must be converted. |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 108 | return (static_cast<size_t>(ComputeVertexAttributeStride(attrib, binding)) % alignment == 0) && |
| 109 | (static_cast<size_t>(offset) % alignment == 0); |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 110 | } |
| 111 | } // anonymous namespace |
| 112 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 113 | TranslatedAttribute::TranslatedAttribute() |
| 114 | : active(false), |
| 115 | attribute(nullptr), |
| 116 | currentValueType(GL_NONE), |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 117 | baseOffset(0), |
| 118 | usesFirstVertexOffset(false), |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 119 | stride(0), |
| 120 | vertexBuffer(), |
| 121 | storage(nullptr), |
| 122 | serial(0), |
| 123 | divisor(0) |
| 124 | { |
| 125 | } |
| 126 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 127 | gl::ErrorOrResult<unsigned int> TranslatedAttribute::computeOffset(GLint startVertex) const |
| 128 | { |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 129 | if (!usesFirstVertexOffset) |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 130 | { |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 131 | return baseOffset; |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 132 | } |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 133 | |
| 134 | CheckedNumeric<unsigned int> offset; |
| 135 | |
| 136 | offset = baseOffset + stride * static_cast<unsigned int>(startVertex); |
| 137 | ANGLE_TRY_CHECKED_MATH(offset); |
| 138 | return offset.ValueOrDie(); |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 141 | // Warning: you should ensure binding really matches attrib.bindingIndex before using this function. |
| 142 | VertexStorageType ClassifyAttributeStorage(const gl::VertexAttribute &attrib, |
| 143 | const gl::VertexBinding &binding) |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 144 | { |
| 145 | // If attribute is disabled, we use the current value. |
| 146 | if (!attrib.enabled) |
| 147 | { |
| 148 | return VertexStorageType::CURRENT_VALUE; |
| 149 | } |
| 150 | |
| 151 | // If specified with immediate data, we must use dynamic storage. |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 152 | auto *buffer = binding.getBuffer().get(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 153 | if (!buffer) |
| 154 | { |
| 155 | return VertexStorageType::DYNAMIC; |
| 156 | } |
| 157 | |
| 158 | // Check if the buffer supports direct storage. |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 159 | if (DirectStoragePossible(attrib, binding)) |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 160 | { |
| 161 | return VertexStorageType::DIRECT; |
| 162 | } |
| 163 | |
| 164 | // Otherwise the storage is static or dynamic. |
| 165 | BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer); |
| 166 | ASSERT(bufferD3D); |
| 167 | switch (bufferD3D->getUsage()) |
| 168 | { |
| 169 | case D3DBufferUsage::DYNAMIC: |
| 170 | return VertexStorageType::DYNAMIC; |
| 171 | case D3DBufferUsage::STATIC: |
| 172 | return VertexStorageType::STATIC; |
| 173 | default: |
| 174 | UNREACHABLE(); |
| 175 | return VertexStorageType::UNKNOWN; |
| 176 | } |
| 177 | } |
| 178 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 179 | VertexDataManager::CurrentValueState::CurrentValueState() |
| 180 | : buffer(nullptr), |
| 181 | offset(0) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 182 | { |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 183 | data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN(); |
| 184 | data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN(); |
| 185 | data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN(); |
| 186 | data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN(); |
| 187 | data.Type = GL_FLOAT; |
| 188 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 189 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 190 | VertexDataManager::CurrentValueState::~CurrentValueState() |
| 191 | { |
| 192 | SafeDelete(buffer); |
| 193 | } |
| 194 | |
| 195 | VertexDataManager::VertexDataManager(BufferFactoryD3D *factory) |
| 196 | : mFactory(factory), |
| 197 | mStreamingBuffer(nullptr), |
| 198 | // TODO(jmadill): use context caps |
| 199 | mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS) |
| 200 | { |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 201 | mStreamingBuffer = new StreamingVertexBufferInterface(factory, INITIAL_STREAM_BUFFER_SIZE); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 202 | |
| 203 | if (!mStreamingBuffer) |
| 204 | { |
Yuly Novikov | d73f852 | 2017-01-13 17:48:57 -0500 | [diff] [blame] | 205 | ERR() << "Failed to allocate the streaming vertex buffer."; |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 206 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | VertexDataManager::~VertexDataManager() |
| 210 | { |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 211 | SafeDelete(mStreamingBuffer); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 214 | gl::Error VertexDataManager::prepareVertexData(const gl::State &state, |
| 215 | GLint start, |
| 216 | GLsizei count, |
| 217 | std::vector<TranslatedAttribute> *translatedAttribs, |
| 218 | GLsizei instances) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 219 | { |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 220 | ASSERT(mStreamingBuffer); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 221 | |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 222 | const gl::VertexArray *vertexArray = state.getVertexArray(); |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 223 | const auto &vertexAttributes = vertexArray->getVertexAttributes(); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 224 | const auto &vertexBindings = vertexArray->getVertexBindings(); |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 225 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 226 | mDynamicAttribsMaskCache.reset(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 227 | const gl::Program *program = state.getProgram(); |
| 228 | |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 229 | translatedAttribs->clear(); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 230 | |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 231 | for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 232 | { |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 233 | // Skip attrib locations the program doesn't use. |
| 234 | if (!program->isAttribLocationActive(attribIndex)) |
| 235 | continue; |
| 236 | |
| 237 | const auto &attrib = vertexAttributes[attribIndex]; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 238 | const auto &binding = vertexBindings[attrib.bindingIndex]; |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 239 | |
| 240 | // Resize automatically puts in empty attribs |
| 241 | translatedAttribs->resize(attribIndex + 1); |
| 242 | |
| 243 | TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex]; |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 244 | auto currentValueData = state.getVertexAttribCurrentValue(attribIndex); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 245 | |
| 246 | // Record the attribute now |
| 247 | translated->active = true; |
| 248 | translated->attribute = &attrib; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 249 | translated->binding = &binding; |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 250 | translated->currentValueType = currentValueData.Type; |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 251 | translated->divisor = binding.getDivisor(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 252 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 253 | switch (ClassifyAttributeStorage(attrib, binding)) |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 254 | { |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 255 | case VertexStorageType::STATIC: |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 256 | { |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 257 | // Store static attribute. |
Qin Jiajia | cde4f02 | 2017-02-21 14:06:02 +0800 | [diff] [blame] | 258 | ANGLE_TRY(StoreStaticAttrib(translated)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 259 | break; |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 260 | } |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 261 | case VertexStorageType::DYNAMIC: |
| 262 | // Dynamic attributes must be handled together. |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 263 | mDynamicAttribsMaskCache.set(attribIndex); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 264 | break; |
| 265 | case VertexStorageType::DIRECT: |
| 266 | // Update translated data for direct attributes. |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 267 | StoreDirectAttrib(translated); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 268 | break; |
| 269 | case VertexStorageType::CURRENT_VALUE: |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 270 | { |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 271 | ANGLE_TRY(storeCurrentValue(currentValueData, translated, attribIndex)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 272 | break; |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 273 | } |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 274 | default: |
| 275 | UNREACHABLE(); |
| 276 | break; |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 280 | if (mDynamicAttribsMaskCache.none()) |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 281 | { |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 282 | return gl::NoError(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 283 | } |
| 284 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 285 | ANGLE_TRY( |
| 286 | storeDynamicAttribs(translatedAttribs, mDynamicAttribsMaskCache, start, count, instances)); |
| 287 | |
| 288 | PromoteDynamicAttribs(*translatedAttribs, mDynamicAttribsMaskCache, count); |
| 289 | |
| 290 | return gl::NoError(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // static |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 294 | void VertexDataManager::StoreDirectAttrib(TranslatedAttribute *directAttrib) |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 295 | { |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 296 | ASSERT(directAttrib->attribute && directAttrib->binding); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 297 | const auto &attrib = *directAttrib->attribute; |
| 298 | const auto &binding = *directAttrib->binding; |
| 299 | |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 300 | gl::Buffer *buffer = binding.getBuffer().get(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 301 | BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr; |
| 302 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 303 | ASSERT(DirectStoragePossible(attrib, binding)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 304 | directAttrib->vertexBuffer.set(nullptr); |
| 305 | directAttrib->storage = bufferD3D; |
| 306 | directAttrib->serial = bufferD3D->getSerial(); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 307 | directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding)); |
| 308 | directAttrib->baseOffset = |
| 309 | static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 310 | |
| 311 | // Instanced vertices do not apply the 'start' offset |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 312 | directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // static |
Qin Jiajia | cde4f02 | 2017-02-21 14:06:02 +0800 | [diff] [blame] | 316 | gl::Error VertexDataManager::StoreStaticAttrib(TranslatedAttribute *translated) |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 317 | { |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 318 | ASSERT(translated->attribute && translated->binding); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 319 | const auto &attrib = *translated->attribute; |
| 320 | const auto &binding = *translated->binding; |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 321 | |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 322 | gl::Buffer *buffer = binding.getBuffer().get(); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 323 | ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib, binding)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 324 | BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer); |
| 325 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 326 | // Compute source data pointer |
| 327 | const uint8_t *sourceData = nullptr; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 328 | const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 329 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 330 | ANGLE_TRY(bufferD3D->getData(&sourceData)); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 331 | sourceData += offset; |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 332 | |
| 333 | unsigned int streamOffset = 0; |
| 334 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 335 | translated->storage = nullptr; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 336 | ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, binding, 1, 0), |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 337 | translated->stride); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 338 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 339 | auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 340 | ASSERT(staticBuffer); |
| 341 | |
| 342 | if (staticBuffer->empty()) |
| 343 | { |
| 344 | // Convert the entire buffer |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 345 | int totalCount = |
| 346 | ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize())); |
| 347 | int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 348 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 349 | ANGLE_TRY(staticBuffer->storeStaticAttribute(attrib, binding, -startIndex, totalCount, 0, |
| 350 | sourceData)); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 353 | unsigned int firstElementOffset = |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 354 | (static_cast<unsigned int>(offset) / |
| 355 | static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) * |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 356 | translated->stride; |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 357 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 358 | VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer(); |
| 359 | |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 360 | CheckedNumeric<unsigned int> checkedOffset(streamOffset); |
| 361 | checkedOffset += firstElementOffset; |
| 362 | |
| 363 | if (!checkedOffset.IsValid()) |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 364 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 365 | return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib"; |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 368 | translated->vertexBuffer.set(vertexBuffer); |
| 369 | translated->serial = vertexBuffer->getSerial(); |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 370 | translated->baseOffset = streamOffset + firstElementOffset; |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 371 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 372 | // Instanced vertices do not apply the 'start' offset |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 373 | translated->usesFirstVertexOffset = (binding.getDivisor() == 0); |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 374 | |
| 375 | return gl::NoError(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | gl::Error VertexDataManager::storeDynamicAttribs( |
| 379 | std::vector<TranslatedAttribute> *translatedAttribs, |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 380 | const gl::AttributesMask &dynamicAttribsMask, |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 381 | GLint start, |
| 382 | GLsizei count, |
| 383 | GLsizei instances) |
| 384 | { |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 385 | // Instantiating this class will ensure the streaming buffer is never left mapped. |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 386 | class StreamingBufferUnmapper final : NonCopyable |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 387 | { |
| 388 | public: |
| 389 | StreamingBufferUnmapper(StreamingVertexBufferInterface *streamingBuffer) |
| 390 | : mStreamingBuffer(streamingBuffer) |
| 391 | { |
| 392 | ASSERT(mStreamingBuffer); |
| 393 | } |
| 394 | ~StreamingBufferUnmapper() { mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); } |
| 395 | |
| 396 | private: |
| 397 | StreamingVertexBufferInterface *mStreamingBuffer; |
| 398 | }; |
| 399 | |
| 400 | // Will trigger unmapping on return. |
| 401 | StreamingBufferUnmapper localUnmapper(mStreamingBuffer); |
| 402 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 403 | // Reserve the required space for the dynamic buffers. |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 404 | for (auto attribIndex : dynamicAttribsMask) |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 405 | { |
| 406 | const auto &dynamicAttrib = (*translatedAttribs)[attribIndex]; |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 407 | ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, count, instances)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // Store dynamic attributes |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 411 | for (auto attribIndex : dynamicAttribsMask) |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 412 | { |
| 413 | auto *dynamicAttrib = &(*translatedAttribs)[attribIndex]; |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 414 | ANGLE_TRY(storeDynamicAttrib(dynamicAttrib, start, count, instances)); |
| 415 | } |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 416 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 417 | return gl::NoError(); |
| 418 | } |
| 419 | |
| 420 | void VertexDataManager::PromoteDynamicAttribs( |
| 421 | const std::vector<TranslatedAttribute> &translatedAttribs, |
| 422 | const gl::AttributesMask &dynamicAttribsMask, |
| 423 | GLsizei count) |
| 424 | { |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 425 | for (auto attribIndex : dynamicAttribsMask) |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 426 | { |
| 427 | const auto &dynamicAttrib = translatedAttribs[attribIndex]; |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 428 | ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 429 | const auto &binding = *dynamicAttrib.binding; |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 430 | |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 431 | gl::Buffer *buffer = binding.getBuffer().get(); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 432 | if (buffer) |
| 433 | { |
| 434 | BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 435 | size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 436 | bufferD3D->promoteStaticUsage(count * static_cast<int>(typeSize)); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Jamie Madill | 3d72cc7 | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 441 | gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib, |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 442 | GLsizei count, |
| 443 | GLsizei instances) const |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 444 | { |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 445 | ASSERT(translatedAttrib.attribute && translatedAttrib.binding); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 446 | const auto &attrib = *translatedAttrib.attribute; |
| 447 | const auto &binding = *translatedAttrib.binding; |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 448 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 449 | ASSERT(!DirectStoragePossible(attrib, binding)); |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 450 | |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 451 | gl::Buffer *buffer = binding.getBuffer().get(); |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 452 | BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 453 | ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr); |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 454 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 455 | size_t totalCount = ComputeVertexBindingElementCount(binding, count, instances); |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 456 | ASSERT(!bufferD3D || |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 457 | ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize())) >= |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 458 | static_cast<int>(totalCount)); |
| 459 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 460 | return mStreamingBuffer->reserveVertexSpace(attrib, binding, static_cast<GLsizei>(totalCount), |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 461 | instances); |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 462 | } |
| 463 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 464 | gl::Error VertexDataManager::storeDynamicAttrib(TranslatedAttribute *translated, |
| 465 | GLint start, |
| 466 | GLsizei count, |
| 467 | GLsizei instances) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 468 | { |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 469 | ASSERT(translated->attribute && translated->binding); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 470 | const auto &attrib = *translated->attribute; |
| 471 | const auto &binding = *translated->binding; |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 472 | |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 473 | gl::Buffer *buffer = binding.getBuffer().get(); |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 474 | ASSERT(buffer || attrib.pointer); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 475 | ASSERT(attrib.enabled); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 476 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 477 | BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 478 | |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 479 | // Instanced vertices do not apply the 'start' offset |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 480 | GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start); |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 481 | |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 482 | // Compute source data pointer |
| 483 | const uint8_t *sourceData = nullptr; |
| 484 | |
| 485 | if (buffer) |
| 486 | { |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 487 | ANGLE_TRY(storage->getData(&sourceData)); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 488 | sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 489 | } |
| 490 | else |
| 491 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 492 | // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state. |
| 493 | // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 494 | sourceData = static_cast<const uint8_t*>(attrib.pointer); |
| 495 | } |
| 496 | |
| 497 | unsigned int streamOffset = 0; |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 498 | |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 499 | translated->storage = nullptr; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 500 | ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, binding, 1, 0), translated->stride); |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 501 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 502 | size_t totalCount = ComputeVertexBindingElementCount(binding, count, instances); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 503 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 504 | ANGLE_TRY(mStreamingBuffer->storeDynamicAttribute( |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 505 | attrib, binding, translated->currentValueType, firstVertexIndex, |
| 506 | static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData)); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 507 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 508 | VertexBuffer *vertexBuffer = mStreamingBuffer->getVertexBuffer(); |
| 509 | |
| 510 | translated->vertexBuffer.set(vertexBuffer); |
| 511 | translated->serial = vertexBuffer->getSerial(); |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 512 | translated->baseOffset = streamOffset; |
| 513 | translated->usesFirstVertexOffset = false; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 514 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 515 | return gl::NoError(); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 516 | } |
| 517 | |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 518 | gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData ¤tValue, |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 519 | TranslatedAttribute *translated, |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 520 | size_t attribIndex) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 521 | { |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 522 | CurrentValueState *cachedState = &mCurrentValueCache[attribIndex]; |
| 523 | auto *&buffer = cachedState->buffer; |
| 524 | |
| 525 | if (!buffer) |
| 526 | { |
| 527 | buffer = new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE); |
| 528 | } |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 529 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 530 | if (cachedState->data != currentValue) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 531 | { |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 532 | ASSERT(translated->attribute && translated->binding); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 533 | const auto &attrib = *translated->attribute; |
| 534 | const auto &binding = *translated->binding; |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 535 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 536 | ANGLE_TRY(buffer->reserveVertexSpace(attrib, binding, 1, 0)); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 537 | |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 538 | const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 539 | unsigned int streamOffset; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 540 | ANGLE_TRY(buffer->storeDynamicAttribute(attrib, binding, currentValue.Type, 0, 1, 0, |
| 541 | &streamOffset, sourceData)); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 542 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 543 | buffer->getVertexBuffer()->hintUnmapResource(); |
| 544 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 545 | cachedState->data = currentValue; |
| 546 | cachedState->offset = streamOffset; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 547 | } |
| 548 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 549 | translated->vertexBuffer.set(buffer->getVertexBuffer()); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 550 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 551 | translated->storage = nullptr; |
| 552 | translated->serial = buffer->getSerial(); |
| 553 | translated->divisor = 0; |
| 554 | translated->stride = 0; |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 555 | translated->baseOffset = static_cast<unsigned int>(cachedState->offset); |
| 556 | translated->usesFirstVertexOffset = false; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 557 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 558 | return gl::NoError(); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 559 | } |
| 560 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 561 | // VertexBufferBinding implementation |
| 562 | VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr) |
| 563 | { |
| 564 | } |
| 565 | |
| 566 | VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other) |
| 567 | : mBoundVertexBuffer(other.mBoundVertexBuffer) |
| 568 | { |
| 569 | if (mBoundVertexBuffer) |
| 570 | { |
| 571 | mBoundVertexBuffer->addRef(); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | VertexBufferBinding::~VertexBufferBinding() |
| 576 | { |
| 577 | if (mBoundVertexBuffer) |
| 578 | { |
| 579 | mBoundVertexBuffer->release(); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other) |
| 584 | { |
| 585 | mBoundVertexBuffer = other.mBoundVertexBuffer; |
| 586 | if (mBoundVertexBuffer) |
| 587 | { |
| 588 | mBoundVertexBuffer->addRef(); |
| 589 | } |
| 590 | return *this; |
| 591 | } |
| 592 | |
| 593 | void VertexBufferBinding::set(VertexBuffer *vertexBuffer) |
| 594 | { |
| 595 | if (mBoundVertexBuffer == vertexBuffer) |
| 596 | return; |
| 597 | |
| 598 | if (mBoundVertexBuffer) |
| 599 | { |
| 600 | mBoundVertexBuffer->release(); |
| 601 | } |
| 602 | if (vertexBuffer) |
| 603 | { |
| 604 | vertexBuffer->addRef(); |
| 605 | } |
| 606 | |
| 607 | mBoundVertexBuffer = vertexBuffer; |
| 608 | } |
| 609 | |
| 610 | VertexBuffer *VertexBufferBinding::get() const |
| 611 | { |
| 612 | return mBoundVertexBuffer; |
| 613 | } |
| 614 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 615 | } // namespace rx |