blob: e9f8df68d1a85059ff742b228a032453e5a1dbea [file] [log] [blame]
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00003// 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.com8fd34bd2011-02-18 02:52:14 +00007// VertexDataManager.h: Defines the VertexDataManager, a class that
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00008// runs the Buffer translation process.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/renderer/d3d/VertexDataManager.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040011
Jamie Madill20e005b2017-04-07 14:19:22 -040012#include "common/bitset_utils.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Buffer.h"
Jamie Madill33510102017-09-20 10:39:18 -040014#include "libANGLE/Context.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050015#include "libANGLE/Program.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040016#include "libANGLE/State.h"
Geoff Lang5ead9272015-03-25 12:27:43 -040017#include "libANGLE/VertexArray.h"
Jamie Madill33510102017-09-20 10:39:18 -040018#include "libANGLE/VertexAttribute.h"
19#include "libANGLE/formatutils.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040020#include "libANGLE/renderer/d3d/BufferD3D.h"
Jamie Madilldbfc6c62016-02-29 01:08:57 -050021#include "libANGLE/renderer/d3d/RendererD3D.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040022#include "libANGLE/renderer/d3d/VertexBuffer.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000023
Jamie Madille2e406c2016-06-02 13:04:10 -040024using namespace angle;
25
daniel@transgaming.com31240482012-11-28 21:06:41 +000026namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000027{
Jamie Madilldbfc6c62016-02-29 01:08:57 -050028namespace
29{
30enum
31{
32 INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024
33};
34// This has to be at least 4k or else it fails on ATI cards.
35enum
36{
37 CONSTANT_VERTEX_BUFFER_SIZE = 4096
38};
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039
Jiawei-Shao2597fb62016-12-09 16:38:02 +080040// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
41int ElementsInBuffer(const gl::VertexAttribute &attrib,
42 const gl::VertexBinding &binding,
43 unsigned int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000044{
Geoff Langa36ead42013-08-02 11:54:08 -040045 // 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-Shao2597fb62016-12-09 16:38:02 +080051 GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib, binding));
52 GLsizei offset = static_cast<GLsizei>(ComputeVertexAttributeOffset(attrib, binding));
53 return (size - offset % stride +
Cooper Partin4d61f7e2015-08-12 10:56:50 -070054 (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) /
55 stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000056}
57
Jiawei-Shao2597fb62016-12-09 16:38:02 +080058// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
59bool DirectStoragePossible(const gl::VertexAttribute &attrib, const gl::VertexBinding &binding)
Jamie Madilldbfc6c62016-02-29 01:08:57 -050060{
61 // Current value attribs may not use direct storage.
62 if (!attrib.enabled)
63 {
64 return false;
65 }
66
Martin Radevdd5f27e2017-06-07 10:17:09 +030067 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madilldbfc6c62016-02-29 01:08:57 -050068 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 Madilldbfc6c62016-02-29 01:08:57 -050080 // 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 Madilld8fa9212016-03-02 11:51:43 -050082 size_t alignment = 4;
Jamie Madilldbfc6c62016-02-29 01:08:57 -050083
Jamie Madille16a4512017-03-07 20:46:40 -050084 // 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 Madilldbfc6c62016-02-29 01:08:57 -050095 if (attrib.type != GL_FLOAT)
96 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +080097 auto errorOrElementSize = factory->getVertexSpaceRequired(attrib, binding, 1, 0);
Jamie Madilld8fa9212016-03-02 11:51:43 -050098 if (errorOrElementSize.isError())
99 {
Yuly Novikovd73f8522017-01-13 17:48:57 -0500100 ERR() << "Unlogged error in DirectStoragePossible.";
Jamie Madilld8fa9212016-03-02 11:51:43 -0500101 return false;
102 }
103
104 alignment = std::min<size_t>(errorOrElementSize.getResult(), 4);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500105 }
106
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800107 GLintptr offset = ComputeVertexAttributeOffset(attrib, binding);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500108 // Final alignment check - unaligned data must be converted.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800109 return (static_cast<size_t>(ComputeVertexAttributeStride(attrib, binding)) % alignment == 0) &&
110 (static_cast<size_t>(offset) % alignment == 0);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500111}
112} // anonymous namespace
113
Jamie Madille36b92d2016-03-04 15:46:58 -0500114TranslatedAttribute::TranslatedAttribute()
115 : active(false),
116 attribute(nullptr),
Jamie Madill401345e2017-08-21 10:52:40 -0400117 binding(nullptr),
Jamie Madille36b92d2016-03-04 15:46:58 -0500118 currentValueType(GL_NONE),
Jamie Madill52b09c22016-04-11 14:12:31 -0400119 baseOffset(0),
120 usesFirstVertexOffset(false),
Jamie Madille36b92d2016-03-04 15:46:58 -0500121 stride(0),
122 vertexBuffer(),
123 storage(nullptr),
124 serial(0),
125 divisor(0)
126{
127}
128
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500129TranslatedAttribute::TranslatedAttribute(const TranslatedAttribute &other) = default;
130
Jamie Madill02b53282018-07-24 11:06:47 -0400131gl::Error TranslatedAttribute::computeOffset(GLint startVertex, unsigned int *offsetOut) const
Jamie Madill52b09c22016-04-11 14:12:31 -0400132{
Jamie Madille2e406c2016-06-02 13:04:10 -0400133 if (!usesFirstVertexOffset)
Jamie Madill52b09c22016-04-11 14:12:31 -0400134 {
Jamie Madill02b53282018-07-24 11:06:47 -0400135 *offsetOut = baseOffset;
136 return gl::NoError();
Jamie Madill52b09c22016-04-11 14:12:31 -0400137 }
Jamie Madille2e406c2016-06-02 13:04:10 -0400138
Jamie Madill02b53282018-07-24 11:06:47 -0400139 CheckedNumeric<unsigned int> offset(baseOffset);
140 CheckedNumeric<unsigned int> checkedStride(stride);
Jamie Madille2e406c2016-06-02 13:04:10 -0400141
Jamie Madill02b53282018-07-24 11:06:47 -0400142 offset += checkedStride * static_cast<unsigned int>(startVertex);
Jamie Madillca2ff382018-07-11 09:01:17 -0400143 ANGLE_TRY_CHECKED_MATH(offset.IsValid());
Jamie Madill02b53282018-07-24 11:06:47 -0400144 *offsetOut = offset.ValueOrDie();
145 return gl::NoError();
Jamie Madill52b09c22016-04-11 14:12:31 -0400146}
147
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800148// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
149VertexStorageType ClassifyAttributeStorage(const gl::VertexAttribute &attrib,
150 const gl::VertexBinding &binding)
Jamie Madille18eb972016-03-04 15:46:59 -0500151{
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 Madill09463932018-04-04 05:26:59 -0400159 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille18eb972016-03-04 15:46:59 -0500160 if (!buffer)
161 {
162 return VertexStorageType::DYNAMIC;
163 }
164
165 // Check if the buffer supports direct storage.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800166 if (DirectStoragePossible(attrib, binding))
Jamie Madille18eb972016-03-04 15:46:59 -0500167 {
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 Madill401345e2017-08-21 10:52:40 -0400186VertexDataManager::CurrentValueState::CurrentValueState() : buffer(), offset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000187{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400188 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.com83921382011-01-08 05:46:00 +0000194
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400195VertexDataManager::CurrentValueState::~CurrentValueState()
196{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400197}
198
199VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
Jamie Madill021bad42018-07-27 08:12:47 -0400200 : mFactory(factory), mStreamingBuffer(factory), mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS)
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400201{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000202}
203
204VertexDataManager::~VertexDataManager()
205{
Jamie Madill401345e2017-08-21 10:52:40 -0400206}
207
208gl::Error VertexDataManager::initialize()
209{
Jamie Madill021bad42018-07-27 08:12:47 -0400210 return mStreamingBuffer.initialize(INITIAL_STREAM_BUFFER_SIZE);
Jamie Madill401345e2017-08-21 10:52:40 -0400211}
212
213void VertexDataManager::deinitialize()
214{
215 mStreamingBuffer.reset();
216 mCurrentValueCache.clear();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000217}
218
Jamie Madill33510102017-09-20 10:39:18 -0400219gl::Error VertexDataManager::prepareVertexData(const gl::Context *context,
Jamie Madill476682e2015-06-30 10:04:29 -0400220 GLint start,
221 GLsizei count,
222 std::vector<TranslatedAttribute> *translatedAttribs,
223 GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000224{
Jamie Madill33510102017-09-20 10:39:18 -0400225 const gl::State &state = context->getGLState();
Geoff Lang5ead9272015-03-25 12:27:43 -0400226 const gl::VertexArray *vertexArray = state.getVertexArray();
Jamie Madill63805b42015-08-25 13:17:39 -0400227 const auto &vertexAttributes = vertexArray->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800228 const auto &vertexBindings = vertexArray->getVertexBindings();
Geoff Lang5ead9272015-03-25 12:27:43 -0400229
Jamie Madill52b09c22016-04-11 14:12:31 -0400230 mDynamicAttribsMaskCache.reset();
Jamie Madille18eb972016-03-04 15:46:59 -0500231 const gl::Program *program = state.getProgram();
232
Jamie Madill476682e2015-06-30 10:04:29 -0400233 translatedAttribs->clear();
Jamie Madill27c08912015-06-22 13:57:20 -0400234
Jamie Madill9c385802015-06-22 13:57:18 -0400235 for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000236 {
Jamie Madille18eb972016-03-04 15:46:59 -0500237 // Skip attrib locations the program doesn't use.
238 if (!program->isAttribLocationActive(attribIndex))
239 continue;
240
241 const auto &attrib = vertexAttributes[attribIndex];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800242 const auto &binding = vertexBindings[attrib.bindingIndex];
Jamie Madille18eb972016-03-04 15:46:59 -0500243
244 // Resize automatically puts in empty attribs
245 translatedAttribs->resize(attribIndex + 1);
246
247 TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
Jamie Madill6de51852017-04-12 09:53:01 -0400248 auto currentValueData = state.getVertexAttribCurrentValue(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500249
250 // Record the attribute now
251 translated->active = true;
252 translated->attribute = &attrib;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800253 translated->binding = &binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500254 translated->currentValueType = currentValueData.Type;
Martin Radevdd5f27e2017-06-07 10:17:09 +0300255 translated->divisor = binding.getDivisor();
Jamie Madille18eb972016-03-04 15:46:59 -0500256
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800257 switch (ClassifyAttributeStorage(attrib, binding))
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000258 {
Jamie Madille18eb972016-03-04 15:46:59 -0500259 case VertexStorageType::STATIC:
Jamie Madill9c385802015-06-22 13:57:18 -0400260 {
Jamie Madille18eb972016-03-04 15:46:59 -0500261 // Store static attribute.
Jamie Madill33510102017-09-20 10:39:18 -0400262 ANGLE_TRY(StoreStaticAttrib(context, translated));
Jamie Madille18eb972016-03-04 15:46:59 -0500263 break;
Jamie Madill9c385802015-06-22 13:57:18 -0400264 }
Jamie Madille18eb972016-03-04 15:46:59 -0500265 case VertexStorageType::DYNAMIC:
266 // Dynamic attributes must be handled together.
Jamie Madill52b09c22016-04-11 14:12:31 -0400267 mDynamicAttribsMaskCache.set(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500268 break;
269 case VertexStorageType::DIRECT:
270 // Update translated data for direct attributes.
Jamie Madill52b09c22016-04-11 14:12:31 -0400271 StoreDirectAttrib(translated);
Jamie Madille18eb972016-03-04 15:46:59 -0500272 break;
273 case VertexStorageType::CURRENT_VALUE:
Jamie Madill27c08912015-06-22 13:57:20 -0400274 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400275 ANGLE_TRY(storeCurrentValue(currentValueData, translated, attribIndex));
Jamie Madille18eb972016-03-04 15:46:59 -0500276 break;
Jamie Madill27c08912015-06-22 13:57:20 -0400277 }
Jamie Madille18eb972016-03-04 15:46:59 -0500278 default:
279 UNREACHABLE();
280 break;
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000281 }
282 }
283
Jamie Madill52b09c22016-04-11 14:12:31 -0400284 if (mDynamicAttribsMaskCache.none())
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000285 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400286 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500287 }
288
Jamie Madill33510102017-09-20 10:39:18 -0400289 ANGLE_TRY(storeDynamicAttribs(context, translatedAttribs, mDynamicAttribsMaskCache, start,
290 count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400291
Jamie Madill33510102017-09-20 10:39:18 -0400292 PromoteDynamicAttribs(context, *translatedAttribs, mDynamicAttribsMaskCache, count);
Jamie Madill52b09c22016-04-11 14:12:31 -0400293
294 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500295}
296
297// static
Jamie Madill52b09c22016-04-11 14:12:31 -0400298void VertexDataManager::StoreDirectAttrib(TranslatedAttribute *directAttrib)
Jamie Madille18eb972016-03-04 15:46:59 -0500299{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800300 ASSERT(directAttrib->attribute && directAttrib->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800301 const auto &attrib = *directAttrib->attribute;
302 const auto &binding = *directAttrib->binding;
303
Martin Radevdd5f27e2017-06-07 10:17:09 +0300304 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill7aa786c2017-09-19 13:57:15 -0400305 ASSERT(buffer);
306 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jamie Madille18eb972016-03-04 15:46:59 -0500307
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800308 ASSERT(DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500309 directAttrib->vertexBuffer.set(nullptr);
310 directAttrib->storage = bufferD3D;
311 directAttrib->serial = bufferD3D->getSerial();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800312 directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding));
313 directAttrib->baseOffset =
314 static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill52b09c22016-04-11 14:12:31 -0400315
316 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300317 directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madille18eb972016-03-04 15:46:59 -0500318}
319
320// static
Jamie Madill33510102017-09-20 10:39:18 -0400321gl::Error VertexDataManager::StoreStaticAttrib(const gl::Context *context,
322 TranslatedAttribute *translated)
Jamie Madille18eb972016-03-04 15:46:59 -0500323{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800324 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800325 const auto &attrib = *translated->attribute;
326 const auto &binding = *translated->binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500327
Martin Radevdd5f27e2017-06-07 10:17:09 +0300328 gl::Buffer *buffer = binding.getBuffer().get();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800329 ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500330 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
331
Jamie Madille18eb972016-03-04 15:46:59 -0500332 // Compute source data pointer
333 const uint8_t *sourceData = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800334 const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500335
Jamie Madill33510102017-09-20 10:39:18 -0400336 ANGLE_TRY(bufferD3D->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800337 sourceData += offset;
Jamie Madille18eb972016-03-04 15:46:59 -0500338
339 unsigned int streamOffset = 0;
340
Jamie Madille18eb972016-03-04 15:46:59 -0500341 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800342 ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, binding, 1, 0),
Jamie Madill52b09c22016-04-11 14:12:31 -0400343 translated->stride);
Jamie Madille18eb972016-03-04 15:46:59 -0500344
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800345 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
Jamie Madille18eb972016-03-04 15:46:59 -0500346 ASSERT(staticBuffer);
347
348 if (staticBuffer->empty())
349 {
350 // Convert the entire buffer
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800351 int totalCount =
352 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
353 int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500354
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800355 ANGLE_TRY(staticBuffer->storeStaticAttribute(attrib, binding, -startIndex, totalCount, 0,
356 sourceData));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000357 }
358
Jamie Madille18eb972016-03-04 15:46:59 -0500359 unsigned int firstElementOffset =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800360 (static_cast<unsigned int>(offset) /
361 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
Jamie Madille18eb972016-03-04 15:46:59 -0500362 translated->stride;
Jamie Madill27c08912015-06-22 13:57:20 -0400363
Jamie Madille18eb972016-03-04 15:46:59 -0500364 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
365
Jamie Madille2e406c2016-06-02 13:04:10 -0400366 CheckedNumeric<unsigned int> checkedOffset(streamOffset);
367 checkedOffset += firstElementOffset;
368
369 if (!checkedOffset.IsValid())
Jamie Madill52b09c22016-04-11 14:12:31 -0400370 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500371 return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib";
Jamie Madill52b09c22016-04-11 14:12:31 -0400372 }
373
Jamie Madille18eb972016-03-04 15:46:59 -0500374 translated->vertexBuffer.set(vertexBuffer);
375 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400376 translated->baseOffset = streamOffset + firstElementOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500377
Jamie Madill52b09c22016-04-11 14:12:31 -0400378 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300379 translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madill52b09c22016-04-11 14:12:31 -0400380
381 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500382}
383
384gl::Error VertexDataManager::storeDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400385 const gl::Context *context,
Jamie Madille18eb972016-03-04 15:46:59 -0500386 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill52b09c22016-04-11 14:12:31 -0400387 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madille18eb972016-03-04 15:46:59 -0500388 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400389 size_t count,
Jamie Madille18eb972016-03-04 15:46:59 -0500390 GLsizei instances)
391{
Jamie Madill52b09c22016-04-11 14:12:31 -0400392 // Instantiating this class will ensure the streaming buffer is never left mapped.
Jamie Madille2e406c2016-06-02 13:04:10 -0400393 class StreamingBufferUnmapper final : NonCopyable
Jamie Madill52b09c22016-04-11 14:12:31 -0400394 {
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 Madill021bad42018-07-27 08:12:47 -0400408 StreamingBufferUnmapper localUnmapper(&mStreamingBuffer);
Jamie Madill52b09c22016-04-11 14:12:31 -0400409
Jamie Madille18eb972016-03-04 15:46:59 -0500410 // Reserve the required space for the dynamic buffers.
Jamie Madill6de51852017-04-12 09:53:01 -0400411 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500412 {
413 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800414 ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, start, count, instances));
Jamie Madille18eb972016-03-04 15:46:59 -0500415 }
416
417 // Store dynamic attributes
Jamie Madill6de51852017-04-12 09:53:01 -0400418 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500419 {
420 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill33510102017-09-20 10:39:18 -0400421 ANGLE_TRY(storeDynamicAttrib(context, dynamicAttrib, start, count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400422 }
Shannon Woods1a965482014-09-22 18:00:32 -0400423
Jamie Madill52b09c22016-04-11 14:12:31 -0400424 return gl::NoError();
425}
426
427void VertexDataManager::PromoteDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400428 const gl::Context *context,
Jamie Madill52b09c22016-04-11 14:12:31 -0400429 const std::vector<TranslatedAttribute> &translatedAttribs,
430 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madill18e323a2018-05-11 16:54:17 -0400431 size_t count)
Jamie Madill52b09c22016-04-11 14:12:31 -0400432{
Jamie Madill6de51852017-04-12 09:53:01 -0400433 for (auto attribIndex : dynamicAttribsMask)
Jamie Madill52b09c22016-04-11 14:12:31 -0400434 {
435 const auto &dynamicAttrib = translatedAttribs[attribIndex];
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800436 ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800437 const auto &binding = *dynamicAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800438
Martin Radevdd5f27e2017-06-07 10:17:09 +0300439 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill27c08912015-06-22 13:57:20 -0400440 if (buffer)
441 {
Jamie Madill18e323a2018-05-11 16:54:17 -0400442 // Note: this multiplication can overflow. It should not be a security problem.
Jamie Madill27c08912015-06-22 13:57:20 -0400443 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800444 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
Jamie Madill18e323a2018-05-11 16:54:17 -0400445 bufferD3D->promoteStaticUsage(context, count * typeSize);
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000446 }
447 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000448}
449
Jamie Madill3d72cc72015-06-22 13:57:19 -0400450gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800451 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400452 size_t count,
Jamie Madill021bad42018-07-27 08:12:47 -0400453 GLsizei instances)
Jamie Madill6d113802014-08-25 15:47:52 -0400454{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800455 ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800456 const auto &attrib = *translatedAttrib.attribute;
457 const auto &binding = *translatedAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800458
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800459 ASSERT(!DirectStoragePossible(attrib, binding));
Jamie Madill6d113802014-08-25 15:47:52 -0400460
Martin Radevdd5f27e2017-06-07 10:17:09 +0300461 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500462 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800463 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
Jamie Madille36b92d2016-03-04 15:46:58 -0500464
Jamie Madill18e323a2018-05-11 16:54:17 -0400465 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
466 static_cast<size_t>(instances));
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800467 // 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 Madille36b92d2016-03-04 15:46:58 -0500478
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800479 if (maxVertexCount > elementsInBuffer)
480 {
481 return gl::InvalidOperation() << "Vertex buffer is not big enough for the draw call.";
482 }
483 }
Jamie Madill021bad42018-07-27 08:12:47 -0400484 return mStreamingBuffer.reserveVertexSpace(attrib, binding, totalCount, instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400485}
486
Jamie Madill33510102017-09-20 10:39:18 -0400487gl::Error VertexDataManager::storeDynamicAttrib(const gl::Context *context,
488 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500489 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400490 size_t count,
Jamie Madill021bad42018-07-27 08:12:47 -0400491 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400492{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800493 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800494 const auto &attrib = *translated->attribute;
495 const auto &binding = *translated->binding;
Jamie Madill9c385802015-06-22 13:57:18 -0400496
Martin Radevdd5f27e2017-06-07 10:17:09 +0300497 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -0400498 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400499 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400500
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500501 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400502
Jamie Madilld4b55a02015-01-09 14:21:49 -0500503 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300504 GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500505
Jamie Madill7d112bb2015-06-22 13:57:19 -0400506 // Compute source data pointer
507 const uint8_t *sourceData = nullptr;
508
509 if (buffer)
510 {
Jamie Madill33510102017-09-20 10:39:18 -0400511 ANGLE_TRY(storage->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800512 sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400513 }
514 else
515 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800516 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
517 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill7d112bb2015-06-22 13:57:19 -0400518 sourceData = static_cast<const uint8_t*>(attrib.pointer);
519 }
520
521 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500522
Jamie Madilld8fa9212016-03-02 11:51:43 -0500523 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800524 ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, binding, 1, 0), translated->stride);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500525
Jamie Madill18e323a2018-05-11 16:54:17 -0400526 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
527 static_cast<size_t>(instances));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400528
Jamie Madill021bad42018-07-27 08:12:47 -0400529 ANGLE_TRY(mStreamingBuffer.storeDynamicAttribute(
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800530 attrib, binding, translated->currentValueType, firstVertexIndex,
531 static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400532
Jamie Madill021bad42018-07-27 08:12:47 -0400533 VertexBuffer *vertexBuffer = mStreamingBuffer.getVertexBuffer();
Jamie Madille18eb972016-03-04 15:46:59 -0500534
535 translated->vertexBuffer.set(vertexBuffer);
536 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400537 translated->baseOffset = streamOffset;
538 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400539
Jamie Madill52b09c22016-04-11 14:12:31 -0400540 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400541}
542
Jamie Madill9c385802015-06-22 13:57:18 -0400543gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400544 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500545 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400546{
Jamie Madille18eb972016-03-04 15:46:59 -0500547 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
Jamie Madill401345e2017-08-21 10:52:40 -0400548 auto &buffer = cachedState->buffer;
Jamie Madille18eb972016-03-04 15:46:59 -0500549
550 if (!buffer)
551 {
Jamie Madill021bad42018-07-27 08:12:47 -0400552 buffer.reset(new StreamingVertexBufferInterface(mFactory));
553 ANGLE_TRY(buffer->initialize(CONSTANT_VERTEX_BUFFER_SIZE));
Jamie Madille18eb972016-03-04 15:46:59 -0500554 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500555
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400556 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400557 {
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800558 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800559 const auto &attrib = *translated->attribute;
560 const auto &binding = *translated->binding;
Jamie Madill27c08912015-06-22 13:57:20 -0400561
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800562 ANGLE_TRY(buffer->reserveVertexSpace(attrib, binding, 1, 0));
Jamie Madillf41522b2014-08-18 16:39:49 -0400563
Jamie Madill7d112bb2015-06-22 13:57:19 -0400564 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400565 unsigned int streamOffset;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800566 ANGLE_TRY(buffer->storeDynamicAttribute(attrib, binding, currentValue.Type, 0, 1, 0,
567 &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400568
Jamie Madille36b92d2016-03-04 15:46:58 -0500569 buffer->getVertexBuffer()->hintUnmapResource();
570
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400571 cachedState->data = currentValue;
572 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400573 }
574
Jamie Madille36b92d2016-03-04 15:46:58 -0500575 translated->vertexBuffer.set(buffer->getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400576
Jamie Madille36b92d2016-03-04 15:46:58 -0500577 translated->storage = nullptr;
578 translated->serial = buffer->getSerial();
579 translated->divisor = 0;
580 translated->stride = 0;
Jamie Madill52b09c22016-04-11 14:12:31 -0400581 translated->baseOffset = static_cast<unsigned int>(cachedState->offset);
582 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400583
Jamie Madill52b09c22016-04-11 14:12:31 -0400584 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400585}
586
Jamie Madille36b92d2016-03-04 15:46:58 -0500587// VertexBufferBinding implementation
588VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
589{
590}
591
592VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
593 : mBoundVertexBuffer(other.mBoundVertexBuffer)
594{
595 if (mBoundVertexBuffer)
596 {
597 mBoundVertexBuffer->addRef();
598 }
599}
600
601VertexBufferBinding::~VertexBufferBinding()
602{
603 if (mBoundVertexBuffer)
604 {
605 mBoundVertexBuffer->release();
606 }
607}
608
609VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
610{
611 mBoundVertexBuffer = other.mBoundVertexBuffer;
612 if (mBoundVertexBuffer)
613 {
614 mBoundVertexBuffer->addRef();
615 }
616 return *this;
617}
618
619void 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
636VertexBuffer *VertexBufferBinding::get() const
637{
638 return mBoundVertexBuffer;
639}
640
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500641} // namespace rx