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