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