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