blob: 7a6d61168e92bc9126eb5aba75b6b73ba7f2fe2d [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 Madillec1fe5b2018-08-10 10:05:52 -040021#include "libANGLE/renderer/d3d/ContextD3D.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.
Jamie Madillb1565902018-07-27 08:12:48 -040059bool DirectStoragePossible(const gl::Context *context,
60 const gl::VertexAttribute &attrib,
61 const gl::VertexBinding &binding)
Jamie Madilldbfc6c62016-02-29 01:08:57 -050062{
63 // Current value attribs may not use direct storage.
64 if (!attrib.enabled)
65 {
66 return false;
67 }
68
Martin Radevdd5f27e2017-06-07 10:17:09 +030069 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madilldbfc6c62016-02-29 01:08:57 -050070 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 Madilldbfc6c62016-02-29 01:08:57 -050082 // 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 Madilld8fa9212016-03-02 11:51:43 -050084 size_t alignment = 4;
Jamie Madilldbfc6c62016-02-29 01:08:57 -050085
Jamie Madille16a4512017-03-07 20:46:40 -050086 // 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 Madilldbfc6c62016-02-29 01:08:57 -050097 if (attrib.type != GL_FLOAT)
98 {
Jamie Madill854429d2018-07-27 08:12:48 -040099 unsigned int elementSize = 0;
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400100 angle::Result error =
Jamie Madill854429d2018-07-27 08:12:48 -0400101 factory->getVertexSpaceRequired(context, attrib, binding, 1, 0, &elementSize);
102 ASSERT(!error.isError());
103 alignment = std::min<size_t>(elementSize, 4);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500104 }
105
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800106 GLintptr offset = ComputeVertexAttributeOffset(attrib, binding);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500107 // Final alignment check - unaligned data must be converted.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800108 return (static_cast<size_t>(ComputeVertexAttributeStride(attrib, binding)) % alignment == 0) &&
109 (static_cast<size_t>(offset) % alignment == 0);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500110}
111} // anonymous namespace
112
Jamie Madille36b92d2016-03-04 15:46:58 -0500113TranslatedAttribute::TranslatedAttribute()
114 : active(false),
115 attribute(nullptr),
Jamie Madill401345e2017-08-21 10:52:40 -0400116 binding(nullptr),
Jamie Madille36b92d2016-03-04 15:46:58 -0500117 currentValueType(GL_NONE),
Jamie Madill52b09c22016-04-11 14:12:31 -0400118 baseOffset(0),
119 usesFirstVertexOffset(false),
Jamie Madille36b92d2016-03-04 15:46:58 -0500120 stride(0),
121 vertexBuffer(),
122 storage(nullptr),
123 serial(0),
124 divisor(0)
125{
126}
127
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500128TranslatedAttribute::TranslatedAttribute(const TranslatedAttribute &other) = default;
129
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400130angle::Result TranslatedAttribute::computeOffset(const gl::Context *context,
131 GLint startVertex,
132 unsigned int *offsetOut) const
Jamie Madill52b09c22016-04-11 14:12:31 -0400133{
Jamie Madille2e406c2016-06-02 13:04:10 -0400134 if (!usesFirstVertexOffset)
Jamie Madill52b09c22016-04-11 14:12:31 -0400135 {
Jamie Madill02b53282018-07-24 11:06:47 -0400136 *offsetOut = baseOffset;
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400137 return angle::Result::Continue();
Jamie Madill52b09c22016-04-11 14:12:31 -0400138 }
Jamie Madille2e406c2016-06-02 13:04:10 -0400139
Jamie Madill02b53282018-07-24 11:06:47 -0400140 CheckedNumeric<unsigned int> offset(baseOffset);
141 CheckedNumeric<unsigned int> checkedStride(stride);
Jamie Madille2e406c2016-06-02 13:04:10 -0400142
Jamie Madill02b53282018-07-24 11:06:47 -0400143 offset += checkedStride * static_cast<unsigned int>(startVertex);
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400144 ANGLE_CHECK_HR_MATH(GetImplAs<ContextD3D>(context), offset.IsValid());
Jamie Madill02b53282018-07-24 11:06:47 -0400145 *offsetOut = offset.ValueOrDie();
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400146 return angle::Result::Continue();
Jamie Madill52b09c22016-04-11 14:12:31 -0400147}
148
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800149// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
Jamie Madillb1565902018-07-27 08:12:48 -0400150VertexStorageType ClassifyAttributeStorage(const gl::Context *context,
151 const gl::VertexAttribute &attrib,
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800152 const gl::VertexBinding &binding)
Jamie Madille18eb972016-03-04 15:46:59 -0500153{
154 // If attribute is disabled, we use the current value.
155 if (!attrib.enabled)
156 {
157 return VertexStorageType::CURRENT_VALUE;
158 }
159
160 // If specified with immediate data, we must use dynamic storage.
Jamie Madill09463932018-04-04 05:26:59 -0400161 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille18eb972016-03-04 15:46:59 -0500162 if (!buffer)
163 {
164 return VertexStorageType::DYNAMIC;
165 }
166
167 // Check if the buffer supports direct storage.
Jamie Madillb1565902018-07-27 08:12:48 -0400168 if (DirectStoragePossible(context, attrib, binding))
Jamie Madille18eb972016-03-04 15:46:59 -0500169 {
170 return VertexStorageType::DIRECT;
171 }
172
173 // Otherwise the storage is static or dynamic.
174 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
175 ASSERT(bufferD3D);
176 switch (bufferD3D->getUsage())
177 {
178 case D3DBufferUsage::DYNAMIC:
179 return VertexStorageType::DYNAMIC;
180 case D3DBufferUsage::STATIC:
181 return VertexStorageType::STATIC;
182 default:
183 UNREACHABLE();
184 return VertexStorageType::UNKNOWN;
185 }
186}
187
Jamie Madillb1565902018-07-27 08:12:48 -0400188VertexDataManager::CurrentValueState::CurrentValueState(BufferFactoryD3D *factory)
189 : buffer(new StreamingVertexBufferInterface(factory)), offset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000190{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400191 data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
192 data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
193 data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
194 data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
195 data.Type = GL_FLOAT;
196}
daniel@transgaming.com83921382011-01-08 05:46:00 +0000197
Jamie Madillb1565902018-07-27 08:12:48 -0400198VertexDataManager::CurrentValueState::CurrentValueState(CurrentValueState &&other)
199{
200 std::swap(buffer, other.buffer);
201 std::swap(data, other.data);
202 std::swap(offset, other.offset);
203}
204
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400205VertexDataManager::CurrentValueState::~CurrentValueState()
206{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400207}
208
209VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
Jamie Madillb1565902018-07-27 08:12:48 -0400210 : mFactory(factory), mStreamingBuffer(factory)
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400211{
Jamie Madillb1565902018-07-27 08:12:48 -0400212 for (int currentValueIndex = 0; currentValueIndex < gl::MAX_VERTEX_ATTRIBS; ++currentValueIndex)
213 {
214 mCurrentValueCache.emplace_back(factory);
215 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000216}
217
218VertexDataManager::~VertexDataManager()
219{
Jamie Madill401345e2017-08-21 10:52:40 -0400220}
221
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400222angle::Result VertexDataManager::initialize(const gl::Context *context)
Jamie Madill401345e2017-08-21 10:52:40 -0400223{
Jamie Madillb1565902018-07-27 08:12:48 -0400224 return mStreamingBuffer.initialize(context, INITIAL_STREAM_BUFFER_SIZE);
Jamie Madill401345e2017-08-21 10:52:40 -0400225}
226
227void VertexDataManager::deinitialize()
228{
229 mStreamingBuffer.reset();
230 mCurrentValueCache.clear();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000231}
232
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400233angle::Result VertexDataManager::prepareVertexData(
234 const gl::Context *context,
235 GLint start,
236 GLsizei count,
237 std::vector<TranslatedAttribute> *translatedAttribs,
238 GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000239{
Jamie Madill33510102017-09-20 10:39:18 -0400240 const gl::State &state = context->getGLState();
Geoff Lang5ead9272015-03-25 12:27:43 -0400241 const gl::VertexArray *vertexArray = state.getVertexArray();
Jamie Madill63805b42015-08-25 13:17:39 -0400242 const auto &vertexAttributes = vertexArray->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800243 const auto &vertexBindings = vertexArray->getVertexBindings();
Geoff Lang5ead9272015-03-25 12:27:43 -0400244
Jamie Madill52b09c22016-04-11 14:12:31 -0400245 mDynamicAttribsMaskCache.reset();
Jamie Madille18eb972016-03-04 15:46:59 -0500246 const gl::Program *program = state.getProgram();
247
Jamie Madill476682e2015-06-30 10:04:29 -0400248 translatedAttribs->clear();
Jamie Madill27c08912015-06-22 13:57:20 -0400249
Jamie Madill9c385802015-06-22 13:57:18 -0400250 for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000251 {
Jamie Madille18eb972016-03-04 15:46:59 -0500252 // Skip attrib locations the program doesn't use.
253 if (!program->isAttribLocationActive(attribIndex))
254 continue;
255
256 const auto &attrib = vertexAttributes[attribIndex];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800257 const auto &binding = vertexBindings[attrib.bindingIndex];
Jamie Madille18eb972016-03-04 15:46:59 -0500258
259 // Resize automatically puts in empty attribs
260 translatedAttribs->resize(attribIndex + 1);
261
262 TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
Jamie Madill6de51852017-04-12 09:53:01 -0400263 auto currentValueData = state.getVertexAttribCurrentValue(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500264
265 // Record the attribute now
266 translated->active = true;
267 translated->attribute = &attrib;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800268 translated->binding = &binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500269 translated->currentValueType = currentValueData.Type;
Martin Radevdd5f27e2017-06-07 10:17:09 +0300270 translated->divisor = binding.getDivisor();
Jamie Madille18eb972016-03-04 15:46:59 -0500271
Jamie Madillb1565902018-07-27 08:12:48 -0400272 switch (ClassifyAttributeStorage(context, attrib, binding))
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000273 {
Jamie Madille18eb972016-03-04 15:46:59 -0500274 case VertexStorageType::STATIC:
Jamie Madill9c385802015-06-22 13:57:18 -0400275 {
Jamie Madille18eb972016-03-04 15:46:59 -0500276 // Store static attribute.
Jamie Madill33510102017-09-20 10:39:18 -0400277 ANGLE_TRY(StoreStaticAttrib(context, translated));
Jamie Madille18eb972016-03-04 15:46:59 -0500278 break;
Jamie Madill9c385802015-06-22 13:57:18 -0400279 }
Jamie Madille18eb972016-03-04 15:46:59 -0500280 case VertexStorageType::DYNAMIC:
281 // Dynamic attributes must be handled together.
Jamie Madill52b09c22016-04-11 14:12:31 -0400282 mDynamicAttribsMaskCache.set(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500283 break;
284 case VertexStorageType::DIRECT:
285 // Update translated data for direct attributes.
Jamie Madillb1565902018-07-27 08:12:48 -0400286 StoreDirectAttrib(context, translated);
Jamie Madille18eb972016-03-04 15:46:59 -0500287 break;
288 case VertexStorageType::CURRENT_VALUE:
Jamie Madill27c08912015-06-22 13:57:20 -0400289 {
Jamie Madillb1565902018-07-27 08:12:48 -0400290 ANGLE_TRY(storeCurrentValue(context, currentValueData, translated, attribIndex));
Jamie Madille18eb972016-03-04 15:46:59 -0500291 break;
Jamie Madill27c08912015-06-22 13:57:20 -0400292 }
Jamie Madille18eb972016-03-04 15:46:59 -0500293 default:
294 UNREACHABLE();
295 break;
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000296 }
297 }
298
Jamie Madill52b09c22016-04-11 14:12:31 -0400299 if (mDynamicAttribsMaskCache.none())
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000300 {
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400301 return angle::Result::Continue();
Jamie Madille18eb972016-03-04 15:46:59 -0500302 }
303
Jamie Madill33510102017-09-20 10:39:18 -0400304 ANGLE_TRY(storeDynamicAttribs(context, translatedAttribs, mDynamicAttribsMaskCache, start,
305 count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400306
Jamie Madill33510102017-09-20 10:39:18 -0400307 PromoteDynamicAttribs(context, *translatedAttribs, mDynamicAttribsMaskCache, count);
Jamie Madill52b09c22016-04-11 14:12:31 -0400308
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400309 return angle::Result::Continue();
Jamie Madille18eb972016-03-04 15:46:59 -0500310}
311
312// static
Jamie Madillb1565902018-07-27 08:12:48 -0400313void VertexDataManager::StoreDirectAttrib(const gl::Context *context,
314 TranslatedAttribute *directAttrib)
Jamie Madille18eb972016-03-04 15:46:59 -0500315{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800316 ASSERT(directAttrib->attribute && directAttrib->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800317 const auto &attrib = *directAttrib->attribute;
318 const auto &binding = *directAttrib->binding;
319
Martin Radevdd5f27e2017-06-07 10:17:09 +0300320 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill7aa786c2017-09-19 13:57:15 -0400321 ASSERT(buffer);
322 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jamie Madille18eb972016-03-04 15:46:59 -0500323
Jamie Madillb1565902018-07-27 08:12:48 -0400324 ASSERT(DirectStoragePossible(context, attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500325 directAttrib->vertexBuffer.set(nullptr);
326 directAttrib->storage = bufferD3D;
327 directAttrib->serial = bufferD3D->getSerial();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800328 directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding));
329 directAttrib->baseOffset =
330 static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill52b09c22016-04-11 14:12:31 -0400331
332 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300333 directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madille18eb972016-03-04 15:46:59 -0500334}
335
336// static
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400337angle::Result VertexDataManager::StoreStaticAttrib(const gl::Context *context,
338 TranslatedAttribute *translated)
Jamie Madille18eb972016-03-04 15:46:59 -0500339{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800340 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800341 const auto &attrib = *translated->attribute;
342 const auto &binding = *translated->binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500343
Martin Radevdd5f27e2017-06-07 10:17:09 +0300344 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madillb1565902018-07-27 08:12:48 -0400345 ASSERT(buffer && attrib.enabled && !DirectStoragePossible(context, attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500346 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
347
Jamie Madille18eb972016-03-04 15:46:59 -0500348 // Compute source data pointer
349 const uint8_t *sourceData = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800350 const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500351
Jamie Madill33510102017-09-20 10:39:18 -0400352 ANGLE_TRY(bufferD3D->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800353 sourceData += offset;
Jamie Madille18eb972016-03-04 15:46:59 -0500354
355 unsigned int streamOffset = 0;
356
Jamie Madille18eb972016-03-04 15:46:59 -0500357 translated->storage = nullptr;
Jamie Madill854429d2018-07-27 08:12:48 -0400358 ANGLE_TRY(bufferD3D->getFactory()->getVertexSpaceRequired(context, attrib, binding, 1, 0,
359 &translated->stride));
Jamie Madille18eb972016-03-04 15:46:59 -0500360
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800361 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
Jamie Madille18eb972016-03-04 15:46:59 -0500362 ASSERT(staticBuffer);
363
364 if (staticBuffer->empty())
365 {
366 // Convert the entire buffer
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800367 int totalCount =
368 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
369 int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500370
Jamie Madillb1565902018-07-27 08:12:48 -0400371 ANGLE_TRY(staticBuffer->storeStaticAttribute(context, attrib, binding, -startIndex,
372 totalCount, 0, sourceData));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000373 }
374
Jamie Madille18eb972016-03-04 15:46:59 -0500375 unsigned int firstElementOffset =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800376 (static_cast<unsigned int>(offset) /
377 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
Jamie Madille18eb972016-03-04 15:46:59 -0500378 translated->stride;
Jamie Madill27c08912015-06-22 13:57:20 -0400379
Jamie Madille18eb972016-03-04 15:46:59 -0500380 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
381
Jamie Madille2e406c2016-06-02 13:04:10 -0400382 CheckedNumeric<unsigned int> checkedOffset(streamOffset);
383 checkedOffset += firstElementOffset;
384
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400385 ANGLE_CHECK_HR_MATH(GetImplAs<ContextD3D>(context), checkedOffset.IsValid());
Jamie Madill52b09c22016-04-11 14:12:31 -0400386
Jamie Madille18eb972016-03-04 15:46:59 -0500387 translated->vertexBuffer.set(vertexBuffer);
388 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400389 translated->baseOffset = streamOffset + firstElementOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500390
Jamie Madill52b09c22016-04-11 14:12:31 -0400391 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300392 translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madill52b09c22016-04-11 14:12:31 -0400393
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400394 return angle::Result::Continue();
Jamie Madille18eb972016-03-04 15:46:59 -0500395}
396
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400397angle::Result VertexDataManager::storeDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400398 const gl::Context *context,
Jamie Madille18eb972016-03-04 15:46:59 -0500399 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill52b09c22016-04-11 14:12:31 -0400400 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madille18eb972016-03-04 15:46:59 -0500401 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400402 size_t count,
Jamie Madille18eb972016-03-04 15:46:59 -0500403 GLsizei instances)
404{
Jamie Madill52b09c22016-04-11 14:12:31 -0400405 // Instantiating this class will ensure the streaming buffer is never left mapped.
Jamie Madille2e406c2016-06-02 13:04:10 -0400406 class StreamingBufferUnmapper final : NonCopyable
Jamie Madill52b09c22016-04-11 14:12:31 -0400407 {
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 Madill021bad42018-07-27 08:12:47 -0400421 StreamingBufferUnmapper localUnmapper(&mStreamingBuffer);
Jamie Madill52b09c22016-04-11 14:12:31 -0400422
Jamie Madille18eb972016-03-04 15:46:59 -0500423 // Reserve the required space for the dynamic buffers.
Jamie Madill6de51852017-04-12 09:53:01 -0400424 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500425 {
426 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Jamie Madillb1565902018-07-27 08:12:48 -0400427 ANGLE_TRY(reserveSpaceForAttrib(context, dynamicAttrib, start, count, instances));
Jamie Madille18eb972016-03-04 15:46:59 -0500428 }
429
430 // Store dynamic attributes
Jamie Madill6de51852017-04-12 09:53:01 -0400431 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500432 {
433 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill33510102017-09-20 10:39:18 -0400434 ANGLE_TRY(storeDynamicAttrib(context, dynamicAttrib, start, count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400435 }
Shannon Woods1a965482014-09-22 18:00:32 -0400436
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400437 return angle::Result::Continue();
Jamie Madill52b09c22016-04-11 14:12:31 -0400438}
439
440void VertexDataManager::PromoteDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400441 const gl::Context *context,
Jamie Madill52b09c22016-04-11 14:12:31 -0400442 const std::vector<TranslatedAttribute> &translatedAttribs,
443 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madill18e323a2018-05-11 16:54:17 -0400444 size_t count)
Jamie Madill52b09c22016-04-11 14:12:31 -0400445{
Jamie Madill6de51852017-04-12 09:53:01 -0400446 for (auto attribIndex : dynamicAttribsMask)
Jamie Madill52b09c22016-04-11 14:12:31 -0400447 {
448 const auto &dynamicAttrib = translatedAttribs[attribIndex];
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800449 ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800450 const auto &binding = *dynamicAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800451
Martin Radevdd5f27e2017-06-07 10:17:09 +0300452 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill27c08912015-06-22 13:57:20 -0400453 if (buffer)
454 {
Jamie Madill18e323a2018-05-11 16:54:17 -0400455 // Note: this multiplication can overflow. It should not be a security problem.
Jamie Madill27c08912015-06-22 13:57:20 -0400456 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800457 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
Jamie Madill18e323a2018-05-11 16:54:17 -0400458 bufferD3D->promoteStaticUsage(context, count * typeSize);
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000459 }
460 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000461}
462
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400463angle::Result VertexDataManager::reserveSpaceForAttrib(const gl::Context *context,
464 const TranslatedAttribute &translatedAttrib,
465 GLint start,
466 size_t count,
467 GLsizei instances)
Jamie Madill6d113802014-08-25 15:47:52 -0400468{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800469 ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800470 const auto &attrib = *translatedAttrib.attribute;
471 const auto &binding = *translatedAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800472
Jamie Madillb1565902018-07-27 08:12:48 -0400473 ASSERT(!DirectStoragePossible(context, attrib, binding));
Jamie Madill6d113802014-08-25 15:47:52 -0400474
Martin Radevdd5f27e2017-06-07 10:17:09 +0300475 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500476 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800477 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
Jamie Madille36b92d2016-03-04 15:46:58 -0500478
Jamie Madill18e323a2018-05-11 16:54:17 -0400479 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
480 static_cast<size_t>(instances));
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800481 // 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 Madille36b92d2016-03-04 15:46:58 -0500492
Jamie Madille39e8f42018-10-05 08:17:38 -0400493 ANGLE_CHECK(GetImplAs<ContextD3D>(context), maxVertexCount <= elementsInBuffer,
494 "Vertex buffer is not big enough for the draw call.", E_FAIL);
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800495 }
Jamie Madillb1565902018-07-27 08:12:48 -0400496 return mStreamingBuffer.reserveVertexSpace(context, attrib, binding, totalCount, instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400497}
498
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400499angle::Result VertexDataManager::storeDynamicAttrib(const gl::Context *context,
500 TranslatedAttribute *translated,
501 GLint start,
502 size_t count,
503 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400504{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800505 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800506 const auto &attrib = *translated->attribute;
507 const auto &binding = *translated->binding;
Jamie Madill9c385802015-06-22 13:57:18 -0400508
Martin Radevdd5f27e2017-06-07 10:17:09 +0300509 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -0400510 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400511 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400512
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500513 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400514
Jamie Madilld4b55a02015-01-09 14:21:49 -0500515 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300516 GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500517
Jamie Madill7d112bb2015-06-22 13:57:19 -0400518 // Compute source data pointer
519 const uint8_t *sourceData = nullptr;
520
521 if (buffer)
522 {
Jamie Madill33510102017-09-20 10:39:18 -0400523 ANGLE_TRY(storage->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800524 sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400525 }
526 else
527 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800528 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
529 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill7d112bb2015-06-22 13:57:19 -0400530 sourceData = static_cast<const uint8_t*>(attrib.pointer);
531 }
532
533 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500534
Jamie Madilld8fa9212016-03-02 11:51:43 -0500535 translated->storage = nullptr;
Jamie Madill854429d2018-07-27 08:12:48 -0400536 ANGLE_TRY(
537 mFactory->getVertexSpaceRequired(context, attrib, binding, 1, 0, &translated->stride));
Jamie Madilld8fa9212016-03-02 11:51:43 -0500538
Jamie Madill18e323a2018-05-11 16:54:17 -0400539 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
540 static_cast<size_t>(instances));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400541
Jamie Madill021bad42018-07-27 08:12:47 -0400542 ANGLE_TRY(mStreamingBuffer.storeDynamicAttribute(
Jamie Madillb1565902018-07-27 08:12:48 -0400543 context, attrib, binding, translated->currentValueType, firstVertexIndex,
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800544 static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400545
Jamie Madill021bad42018-07-27 08:12:47 -0400546 VertexBuffer *vertexBuffer = mStreamingBuffer.getVertexBuffer();
Jamie Madille18eb972016-03-04 15:46:59 -0500547
548 translated->vertexBuffer.set(vertexBuffer);
549 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400550 translated->baseOffset = streamOffset;
551 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400552
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400553 return angle::Result::Continue();
Jamie Madillf41522b2014-08-18 16:39:49 -0400554}
555
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400556angle::Result VertexDataManager::storeCurrentValue(
557 const gl::Context *context,
558 const gl::VertexAttribCurrentValueData &currentValue,
559 TranslatedAttribute *translated,
560 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400561{
Jamie Madille18eb972016-03-04 15:46:59 -0500562 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
Jamie Madillb1565902018-07-27 08:12:48 -0400563 StreamingVertexBufferInterface &buffer = *cachedState->buffer;
Jamie Madille18eb972016-03-04 15:46:59 -0500564
Jamie Madillb1565902018-07-27 08:12:48 -0400565 if (buffer.getBufferSize() == 0)
Jamie Madille18eb972016-03-04 15:46:59 -0500566 {
Jamie Madillb1565902018-07-27 08:12:48 -0400567 ANGLE_TRY(buffer.initialize(context, CONSTANT_VERTEX_BUFFER_SIZE));
Jamie Madille18eb972016-03-04 15:46:59 -0500568 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500569
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400570 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400571 {
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800572 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800573 const auto &attrib = *translated->attribute;
574 const auto &binding = *translated->binding;
Jamie Madill27c08912015-06-22 13:57:20 -0400575
Jamie Madillb1565902018-07-27 08:12:48 -0400576 ANGLE_TRY(buffer.reserveVertexSpace(context, attrib, binding, 1, 0));
Jamie Madillf41522b2014-08-18 16:39:49 -0400577
Jamie Madill7d112bb2015-06-22 13:57:19 -0400578 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400579 unsigned int streamOffset;
Jamie Madillb1565902018-07-27 08:12:48 -0400580 ANGLE_TRY(buffer.storeDynamicAttribute(context, attrib, binding, currentValue.Type, 0, 1, 0,
581 &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400582
Jamie Madillb1565902018-07-27 08:12:48 -0400583 buffer.getVertexBuffer()->hintUnmapResource();
Jamie Madille36b92d2016-03-04 15:46:58 -0500584
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400585 cachedState->data = currentValue;
586 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400587 }
588
Jamie Madillb1565902018-07-27 08:12:48 -0400589 translated->vertexBuffer.set(buffer.getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400590
Jamie Madille36b92d2016-03-04 15:46:58 -0500591 translated->storage = nullptr;
Jamie Madillb1565902018-07-27 08:12:48 -0400592 translated->serial = buffer.getSerial();
Jamie Madille36b92d2016-03-04 15:46:58 -0500593 translated->divisor = 0;
594 translated->stride = 0;
Jamie Madill52b09c22016-04-11 14:12:31 -0400595 translated->baseOffset = static_cast<unsigned int>(cachedState->offset);
596 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400597
Jamie Madillec1fe5b2018-08-10 10:05:52 -0400598 return angle::Result::Continue();
Jamie Madillf41522b2014-08-18 16:39:49 -0400599}
600
Jamie Madille36b92d2016-03-04 15:46:58 -0500601// VertexBufferBinding implementation
602VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
603{
604}
605
606VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
607 : mBoundVertexBuffer(other.mBoundVertexBuffer)
608{
609 if (mBoundVertexBuffer)
610 {
611 mBoundVertexBuffer->addRef();
612 }
613}
614
615VertexBufferBinding::~VertexBufferBinding()
616{
617 if (mBoundVertexBuffer)
618 {
619 mBoundVertexBuffer->release();
620 }
621}
622
623VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
624{
625 mBoundVertexBuffer = other.mBoundVertexBuffer;
626 if (mBoundVertexBuffer)
627 {
628 mBoundVertexBuffer->addRef();
629 }
630 return *this;
631}
632
633void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
634{
635 if (mBoundVertexBuffer == vertexBuffer)
636 return;
637
638 if (mBoundVertexBuffer)
639 {
640 mBoundVertexBuffer->release();
641 }
642 if (vertexBuffer)
643 {
644 vertexBuffer->addRef();
645 }
646
647 mBoundVertexBuffer = vertexBuffer;
648}
649
650VertexBuffer *VertexBufferBinding::get() const
651{
652 return mBoundVertexBuffer;
653}
654
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500655} // namespace rx