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. |
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; |
| 100 | gl::Error error = |
| 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 | 02b5328 | 2018-07-24 11:06:47 -0400 | [diff] [blame] | 130 | gl::Error TranslatedAttribute::computeOffset(GLint startVertex, unsigned int *offsetOut) const |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 131 | { |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 132 | if (!usesFirstVertexOffset) |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 133 | { |
Jamie Madill | 02b5328 | 2018-07-24 11:06:47 -0400 | [diff] [blame] | 134 | *offsetOut = baseOffset; |
| 135 | return gl::NoError(); |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 136 | } |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 137 | |
Jamie Madill | 02b5328 | 2018-07-24 11:06:47 -0400 | [diff] [blame] | 138 | CheckedNumeric<unsigned int> offset(baseOffset); |
| 139 | CheckedNumeric<unsigned int> checkedStride(stride); |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 140 | |
Jamie Madill | 02b5328 | 2018-07-24 11:06:47 -0400 | [diff] [blame] | 141 | offset += checkedStride * static_cast<unsigned int>(startVertex); |
Jamie Madill | ca2ff38 | 2018-07-11 09:01:17 -0400 | [diff] [blame] | 142 | ANGLE_TRY_CHECKED_MATH(offset.IsValid()); |
Jamie Madill | 02b5328 | 2018-07-24 11:06:47 -0400 | [diff] [blame] | 143 | *offsetOut = offset.ValueOrDie(); |
| 144 | return gl::NoError(); |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 147 | // 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] | 148 | VertexStorageType ClassifyAttributeStorage(const gl::Context *context, |
| 149 | const gl::VertexAttribute &attrib, |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 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. |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 166 | if (DirectStoragePossible(context, 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 | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 186 | VertexDataManager::CurrentValueState::CurrentValueState(BufferFactoryD3D *factory) |
| 187 | : buffer(new StreamingVertexBufferInterface(factory)), offset(0) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 188 | { |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 189 | data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN(); |
| 190 | data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN(); |
| 191 | data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN(); |
| 192 | data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN(); |
| 193 | data.Type = GL_FLOAT; |
| 194 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 195 | |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 196 | VertexDataManager::CurrentValueState::CurrentValueState(CurrentValueState &&other) |
| 197 | { |
| 198 | std::swap(buffer, other.buffer); |
| 199 | std::swap(data, other.data); |
| 200 | std::swap(offset, other.offset); |
| 201 | } |
| 202 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 203 | VertexDataManager::CurrentValueState::~CurrentValueState() |
| 204 | { |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | VertexDataManager::VertexDataManager(BufferFactoryD3D *factory) |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 208 | : mFactory(factory), mStreamingBuffer(factory) |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 209 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 210 | for (int currentValueIndex = 0; currentValueIndex < gl::MAX_VERTEX_ATTRIBS; ++currentValueIndex) |
| 211 | { |
| 212 | mCurrentValueCache.emplace_back(factory); |
| 213 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | VertexDataManager::~VertexDataManager() |
| 217 | { |
Jamie Madill | 401345e | 2017-08-21 10:52:40 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 220 | gl::Error VertexDataManager::initialize(const gl::Context *context) |
Jamie Madill | 401345e | 2017-08-21 10:52:40 -0400 | [diff] [blame] | 221 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 222 | return mStreamingBuffer.initialize(context, INITIAL_STREAM_BUFFER_SIZE); |
Jamie Madill | 401345e | 2017-08-21 10:52:40 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void VertexDataManager::deinitialize() |
| 226 | { |
| 227 | mStreamingBuffer.reset(); |
| 228 | mCurrentValueCache.clear(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Jamie Madill | 3351010 | 2017-09-20 10:39:18 -0400 | [diff] [blame] | 231 | gl::Error VertexDataManager::prepareVertexData(const gl::Context *context, |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 232 | GLint start, |
| 233 | GLsizei count, |
| 234 | std::vector<TranslatedAttribute> *translatedAttribs, |
| 235 | GLsizei instances) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 236 | { |
Jamie Madill | 3351010 | 2017-09-20 10:39:18 -0400 | [diff] [blame] | 237 | const gl::State &state = context->getGLState(); |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 238 | const gl::VertexArray *vertexArray = state.getVertexArray(); |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 239 | const auto &vertexAttributes = vertexArray->getVertexAttributes(); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 240 | const auto &vertexBindings = vertexArray->getVertexBindings(); |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 241 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 242 | mDynamicAttribsMaskCache.reset(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 243 | const gl::Program *program = state.getProgram(); |
| 244 | |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 245 | translatedAttribs->clear(); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 246 | |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 247 | for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 248 | { |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 249 | // Skip attrib locations the program doesn't use. |
| 250 | if (!program->isAttribLocationActive(attribIndex)) |
| 251 | continue; |
| 252 | |
| 253 | const auto &attrib = vertexAttributes[attribIndex]; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 254 | const auto &binding = vertexBindings[attrib.bindingIndex]; |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 255 | |
| 256 | // Resize automatically puts in empty attribs |
| 257 | translatedAttribs->resize(attribIndex + 1); |
| 258 | |
| 259 | TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex]; |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 260 | auto currentValueData = state.getVertexAttribCurrentValue(attribIndex); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 261 | |
| 262 | // Record the attribute now |
| 263 | translated->active = true; |
| 264 | translated->attribute = &attrib; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 265 | translated->binding = &binding; |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 266 | translated->currentValueType = currentValueData.Type; |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame] | 267 | translated->divisor = binding.getDivisor(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 268 | |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 269 | switch (ClassifyAttributeStorage(context, attrib, binding)) |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 270 | { |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 271 | case VertexStorageType::STATIC: |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 272 | { |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 273 | // Store static attribute. |
Jamie Madill | 3351010 | 2017-09-20 10:39:18 -0400 | [diff] [blame] | 274 | ANGLE_TRY(StoreStaticAttrib(context, translated)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 275 | break; |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 276 | } |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 277 | case VertexStorageType::DYNAMIC: |
| 278 | // Dynamic attributes must be handled together. |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 279 | mDynamicAttribsMaskCache.set(attribIndex); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 280 | break; |
| 281 | case VertexStorageType::DIRECT: |
| 282 | // Update translated data for direct attributes. |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 283 | StoreDirectAttrib(context, translated); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 284 | break; |
| 285 | case VertexStorageType::CURRENT_VALUE: |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 286 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 287 | ANGLE_TRY(storeCurrentValue(context, currentValueData, translated, attribIndex)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 288 | break; |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 289 | } |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 290 | default: |
| 291 | UNREACHABLE(); |
| 292 | break; |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 296 | if (mDynamicAttribsMaskCache.none()) |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 297 | { |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 298 | return gl::NoError(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 299 | } |
| 300 | |
Jamie Madill | 3351010 | 2017-09-20 10:39:18 -0400 | [diff] [blame] | 301 | ANGLE_TRY(storeDynamicAttribs(context, translatedAttribs, mDynamicAttribsMaskCache, start, |
| 302 | count, instances)); |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 303 | |
Jamie Madill | 3351010 | 2017-09-20 10:39:18 -0400 | [diff] [blame] | 304 | PromoteDynamicAttribs(context, *translatedAttribs, mDynamicAttribsMaskCache, count); |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 305 | |
| 306 | return gl::NoError(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // static |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 310 | void VertexDataManager::StoreDirectAttrib(const gl::Context *context, |
| 311 | TranslatedAttribute *directAttrib) |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 312 | { |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 313 | ASSERT(directAttrib->attribute && directAttrib->binding); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 314 | const auto &attrib = *directAttrib->attribute; |
| 315 | const auto &binding = *directAttrib->binding; |
| 316 | |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame] | 317 | gl::Buffer *buffer = binding.getBuffer().get(); |
Jamie Madill | 7aa786c | 2017-09-19 13:57:15 -0400 | [diff] [blame] | 318 | ASSERT(buffer); |
| 319 | BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 320 | |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 321 | ASSERT(DirectStoragePossible(context, attrib, binding)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 322 | directAttrib->vertexBuffer.set(nullptr); |
| 323 | directAttrib->storage = bufferD3D; |
| 324 | directAttrib->serial = bufferD3D->getSerial(); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 325 | directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding)); |
| 326 | directAttrib->baseOffset = |
| 327 | static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 328 | |
| 329 | // Instanced vertices do not apply the 'start' offset |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame] | 330 | directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // static |
Jamie Madill | 3351010 | 2017-09-20 10:39:18 -0400 | [diff] [blame] | 334 | gl::Error VertexDataManager::StoreStaticAttrib(const gl::Context *context, |
| 335 | TranslatedAttribute *translated) |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 336 | { |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 337 | ASSERT(translated->attribute && translated->binding); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 338 | const auto &attrib = *translated->attribute; |
| 339 | const auto &binding = *translated->binding; |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 340 | |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame] | 341 | gl::Buffer *buffer = binding.getBuffer().get(); |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 342 | ASSERT(buffer && attrib.enabled && !DirectStoragePossible(context, attrib, binding)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 343 | BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer); |
| 344 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 345 | // Compute source data pointer |
| 346 | const uint8_t *sourceData = nullptr; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 347 | const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 348 | |
Jamie Madill | 3351010 | 2017-09-20 10:39:18 -0400 | [diff] [blame] | 349 | ANGLE_TRY(bufferD3D->getData(context, &sourceData)); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 350 | sourceData += offset; |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 351 | |
| 352 | unsigned int streamOffset = 0; |
| 353 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 354 | translated->storage = nullptr; |
Jamie Madill | 854429d | 2018-07-27 08:12:48 -0400 | [diff] [blame^] | 355 | ANGLE_TRY(bufferD3D->getFactory()->getVertexSpaceRequired(context, attrib, binding, 1, 0, |
| 356 | &translated->stride)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 357 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 358 | auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 359 | ASSERT(staticBuffer); |
| 360 | |
| 361 | if (staticBuffer->empty()) |
| 362 | { |
| 363 | // Convert the entire buffer |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 364 | int totalCount = |
| 365 | ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize())); |
| 366 | int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 367 | |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 368 | ANGLE_TRY(staticBuffer->storeStaticAttribute(context, attrib, binding, -startIndex, |
| 369 | totalCount, 0, sourceData)); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 372 | unsigned int firstElementOffset = |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 373 | (static_cast<unsigned int>(offset) / |
| 374 | static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) * |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 375 | translated->stride; |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 376 | |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 377 | VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer(); |
| 378 | |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 379 | CheckedNumeric<unsigned int> checkedOffset(streamOffset); |
| 380 | checkedOffset += firstElementOffset; |
| 381 | |
| 382 | if (!checkedOffset.IsValid()) |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 383 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 384 | return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib"; |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 385 | } |
| 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 | |
| 394 | return gl::NoError(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | gl::Error 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 | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 437 | return gl::NoError(); |
| 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 | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 463 | gl::Error VertexDataManager::reserveSpaceForAttrib(const gl::Context *context, |
| 464 | const TranslatedAttribute &translatedAttrib, |
Jiajia Qin | 8a7b3a0 | 2017-08-25 16:05:48 +0800 | [diff] [blame] | 465 | GLint start, |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 466 | size_t count, |
Jamie Madill | 021bad4 | 2018-07-27 08:12:47 -0400 | [diff] [blame] | 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 | |
Jiajia Qin | 8a7b3a0 | 2017-08-25 16:05:48 +0800 | [diff] [blame] | 493 | if (maxVertexCount > elementsInBuffer) |
| 494 | { |
| 495 | return gl::InvalidOperation() << "Vertex buffer is not big enough for the draw call."; |
| 496 | } |
| 497 | } |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 498 | return mStreamingBuffer.reserveVertexSpace(context, attrib, binding, totalCount, instances); |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 499 | } |
| 500 | |
Jamie Madill | 3351010 | 2017-09-20 10:39:18 -0400 | [diff] [blame] | 501 | gl::Error VertexDataManager::storeDynamicAttrib(const gl::Context *context, |
| 502 | TranslatedAttribute *translated, |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 503 | GLint start, |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 504 | size_t count, |
Jamie Madill | 021bad4 | 2018-07-27 08:12:47 -0400 | [diff] [blame] | 505 | GLsizei instances) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 506 | { |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 507 | ASSERT(translated->attribute && translated->binding); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 508 | const auto &attrib = *translated->attribute; |
| 509 | const auto &binding = *translated->binding; |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 510 | |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame] | 511 | gl::Buffer *buffer = binding.getBuffer().get(); |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 512 | ASSERT(buffer || attrib.pointer); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 513 | ASSERT(attrib.enabled); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 514 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 515 | BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 516 | |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 517 | // Instanced vertices do not apply the 'start' offset |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame] | 518 | GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start); |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 519 | |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 520 | // Compute source data pointer |
| 521 | const uint8_t *sourceData = nullptr; |
| 522 | |
| 523 | if (buffer) |
| 524 | { |
Jamie Madill | 3351010 | 2017-09-20 10:39:18 -0400 | [diff] [blame] | 525 | ANGLE_TRY(storage->getData(context, &sourceData)); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 526 | sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 527 | } |
| 528 | else |
| 529 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 530 | // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state. |
| 531 | // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 532 | sourceData = static_cast<const uint8_t*>(attrib.pointer); |
| 533 | } |
| 534 | |
| 535 | unsigned int streamOffset = 0; |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 536 | |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 537 | translated->storage = nullptr; |
Jamie Madill | 854429d | 2018-07-27 08:12:48 -0400 | [diff] [blame^] | 538 | ANGLE_TRY( |
| 539 | mFactory->getVertexSpaceRequired(context, attrib, binding, 1, 0, &translated->stride)); |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 540 | |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 541 | size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count, |
| 542 | static_cast<size_t>(instances)); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 543 | |
Jamie Madill | 021bad4 | 2018-07-27 08:12:47 -0400 | [diff] [blame] | 544 | ANGLE_TRY(mStreamingBuffer.storeDynamicAttribute( |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 545 | context, attrib, binding, translated->currentValueType, firstVertexIndex, |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 546 | static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData)); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 547 | |
Jamie Madill | 021bad4 | 2018-07-27 08:12:47 -0400 | [diff] [blame] | 548 | VertexBuffer *vertexBuffer = mStreamingBuffer.getVertexBuffer(); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 549 | |
| 550 | translated->vertexBuffer.set(vertexBuffer); |
| 551 | translated->serial = vertexBuffer->getSerial(); |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 552 | translated->baseOffset = streamOffset; |
| 553 | translated->usesFirstVertexOffset = false; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 554 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 555 | return gl::NoError(); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 556 | } |
| 557 | |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 558 | gl::Error VertexDataManager::storeCurrentValue(const gl::Context *context, |
| 559 | const gl::VertexAttribCurrentValueData ¤tValue, |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 560 | TranslatedAttribute *translated, |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 561 | size_t attribIndex) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 562 | { |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 563 | CurrentValueState *cachedState = &mCurrentValueCache[attribIndex]; |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 564 | StreamingVertexBufferInterface &buffer = *cachedState->buffer; |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 565 | |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 566 | if (buffer.getBufferSize() == 0) |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 567 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 568 | ANGLE_TRY(buffer.initialize(context, CONSTANT_VERTEX_BUFFER_SIZE)); |
Jamie Madill | e18eb97 | 2016-03-04 15:46:59 -0500 | [diff] [blame] | 569 | } |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 570 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 571 | if (cachedState->data != currentValue) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 572 | { |
Jiawei-Shao | 995c2ed | 2017-05-15 13:31:45 +0800 | [diff] [blame] | 573 | ASSERT(translated->attribute && translated->binding); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 574 | const auto &attrib = *translated->attribute; |
| 575 | const auto &binding = *translated->binding; |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 576 | |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 577 | ANGLE_TRY(buffer.reserveVertexSpace(context, attrib, binding, 1, 0)); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 578 | |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 579 | const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 580 | unsigned int streamOffset; |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 581 | ANGLE_TRY(buffer.storeDynamicAttribute(context, attrib, binding, currentValue.Type, 0, 1, 0, |
| 582 | &streamOffset, sourceData)); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 583 | |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 584 | buffer.getVertexBuffer()->hintUnmapResource(); |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 585 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 586 | cachedState->data = currentValue; |
| 587 | cachedState->offset = streamOffset; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 588 | } |
| 589 | |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 590 | translated->vertexBuffer.set(buffer.getVertexBuffer()); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 591 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 592 | translated->storage = nullptr; |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 593 | translated->serial = buffer.getSerial(); |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 594 | translated->divisor = 0; |
| 595 | translated->stride = 0; |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 596 | translated->baseOffset = static_cast<unsigned int>(cachedState->offset); |
| 597 | translated->usesFirstVertexOffset = false; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 598 | |
Jamie Madill | 52b09c2 | 2016-04-11 14:12:31 -0400 | [diff] [blame] | 599 | return gl::NoError(); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 600 | } |
| 601 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame] | 602 | // VertexBufferBinding implementation |
| 603 | VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr) |
| 604 | { |
| 605 | } |
| 606 | |
| 607 | VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other) |
| 608 | : mBoundVertexBuffer(other.mBoundVertexBuffer) |
| 609 | { |
| 610 | if (mBoundVertexBuffer) |
| 611 | { |
| 612 | mBoundVertexBuffer->addRef(); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | VertexBufferBinding::~VertexBufferBinding() |
| 617 | { |
| 618 | if (mBoundVertexBuffer) |
| 619 | { |
| 620 | mBoundVertexBuffer->release(); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other) |
| 625 | { |
| 626 | mBoundVertexBuffer = other.mBoundVertexBuffer; |
| 627 | if (mBoundVertexBuffer) |
| 628 | { |
| 629 | mBoundVertexBuffer->addRef(); |
| 630 | } |
| 631 | return *this; |
| 632 | } |
| 633 | |
| 634 | void VertexBufferBinding::set(VertexBuffer *vertexBuffer) |
| 635 | { |
| 636 | if (mBoundVertexBuffer == vertexBuffer) |
| 637 | return; |
| 638 | |
| 639 | if (mBoundVertexBuffer) |
| 640 | { |
| 641 | mBoundVertexBuffer->release(); |
| 642 | } |
| 643 | if (vertexBuffer) |
| 644 | { |
| 645 | vertexBuffer->addRef(); |
| 646 | } |
| 647 | |
| 648 | mBoundVertexBuffer = vertexBuffer; |
| 649 | } |
| 650 | |
| 651 | VertexBuffer *VertexBufferBinding::get() const |
| 652 | { |
| 653 | return mBoundVertexBuffer; |
| 654 | } |
| 655 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 656 | } // namespace rx |