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