blob: e8b24824750d4fa00c9ed243373dcce97254590c [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.
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 Madillb1565902018-07-27 08:12:48 -040099 auto errorOrElementSize = factory->getVertexSpaceRequired(context, attrib, binding, 1, 0);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500100 if (errorOrElementSize.isError())
101 {
Yuly Novikovd73f8522017-01-13 17:48:57 -0500102 ERR() << "Unlogged error in DirectStoragePossible.";
Jamie Madilld8fa9212016-03-02 11:51:43 -0500103 return false;
104 }
105
106 alignment = std::min<size_t>(errorOrElementSize.getResult(), 4);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500107 }
108
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800109 GLintptr offset = ComputeVertexAttributeOffset(attrib, binding);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500110 // Final alignment check - unaligned data must be converted.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800111 return (static_cast<size_t>(ComputeVertexAttributeStride(attrib, binding)) % alignment == 0) &&
112 (static_cast<size_t>(offset) % alignment == 0);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500113}
114} // anonymous namespace
115
Jamie Madille36b92d2016-03-04 15:46:58 -0500116TranslatedAttribute::TranslatedAttribute()
117 : active(false),
118 attribute(nullptr),
Jamie Madill401345e2017-08-21 10:52:40 -0400119 binding(nullptr),
Jamie Madille36b92d2016-03-04 15:46:58 -0500120 currentValueType(GL_NONE),
Jamie Madill52b09c22016-04-11 14:12:31 -0400121 baseOffset(0),
122 usesFirstVertexOffset(false),
Jamie Madille36b92d2016-03-04 15:46:58 -0500123 stride(0),
124 vertexBuffer(),
125 storage(nullptr),
126 serial(0),
127 divisor(0)
128{
129}
130
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500131TranslatedAttribute::TranslatedAttribute(const TranslatedAttribute &other) = default;
132
Jamie Madill02b53282018-07-24 11:06:47 -0400133gl::Error TranslatedAttribute::computeOffset(GLint startVertex, unsigned int *offsetOut) const
Jamie Madill52b09c22016-04-11 14:12:31 -0400134{
Jamie Madille2e406c2016-06-02 13:04:10 -0400135 if (!usesFirstVertexOffset)
Jamie Madill52b09c22016-04-11 14:12:31 -0400136 {
Jamie Madill02b53282018-07-24 11:06:47 -0400137 *offsetOut = baseOffset;
138 return gl::NoError();
Jamie Madill52b09c22016-04-11 14:12:31 -0400139 }
Jamie Madille2e406c2016-06-02 13:04:10 -0400140
Jamie Madill02b53282018-07-24 11:06:47 -0400141 CheckedNumeric<unsigned int> offset(baseOffset);
142 CheckedNumeric<unsigned int> checkedStride(stride);
Jamie Madille2e406c2016-06-02 13:04:10 -0400143
Jamie Madill02b53282018-07-24 11:06:47 -0400144 offset += checkedStride * static_cast<unsigned int>(startVertex);
Jamie Madillca2ff382018-07-11 09:01:17 -0400145 ANGLE_TRY_CHECKED_MATH(offset.IsValid());
Jamie Madill02b53282018-07-24 11:06:47 -0400146 *offsetOut = offset.ValueOrDie();
147 return gl::NoError();
Jamie Madill52b09c22016-04-11 14:12:31 -0400148}
149
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800150// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
Jamie Madillb1565902018-07-27 08:12:48 -0400151VertexStorageType ClassifyAttributeStorage(const gl::Context *context,
152 const gl::VertexAttribute &attrib,
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800153 const gl::VertexBinding &binding)
Jamie Madille18eb972016-03-04 15:46:59 -0500154{
155 // If attribute is disabled, we use the current value.
156 if (!attrib.enabled)
157 {
158 return VertexStorageType::CURRENT_VALUE;
159 }
160
161 // If specified with immediate data, we must use dynamic storage.
Jamie Madill09463932018-04-04 05:26:59 -0400162 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille18eb972016-03-04 15:46:59 -0500163 if (!buffer)
164 {
165 return VertexStorageType::DYNAMIC;
166 }
167
168 // Check if the buffer supports direct storage.
Jamie Madillb1565902018-07-27 08:12:48 -0400169 if (DirectStoragePossible(context, attrib, binding))
Jamie Madille18eb972016-03-04 15:46:59 -0500170 {
171 return VertexStorageType::DIRECT;
172 }
173
174 // Otherwise the storage is static or dynamic.
175 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
176 ASSERT(bufferD3D);
177 switch (bufferD3D->getUsage())
178 {
179 case D3DBufferUsage::DYNAMIC:
180 return VertexStorageType::DYNAMIC;
181 case D3DBufferUsage::STATIC:
182 return VertexStorageType::STATIC;
183 default:
184 UNREACHABLE();
185 return VertexStorageType::UNKNOWN;
186 }
187}
188
Jamie Madillb1565902018-07-27 08:12:48 -0400189VertexDataManager::CurrentValueState::CurrentValueState(BufferFactoryD3D *factory)
190 : buffer(new StreamingVertexBufferInterface(factory)), offset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000191{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400192 data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
193 data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
194 data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
195 data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
196 data.Type = GL_FLOAT;
197}
daniel@transgaming.com83921382011-01-08 05:46:00 +0000198
Jamie Madillb1565902018-07-27 08:12:48 -0400199VertexDataManager::CurrentValueState::CurrentValueState(CurrentValueState &&other)
200{
201 std::swap(buffer, other.buffer);
202 std::swap(data, other.data);
203 std::swap(offset, other.offset);
204}
205
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400206VertexDataManager::CurrentValueState::~CurrentValueState()
207{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400208}
209
210VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
Jamie Madillb1565902018-07-27 08:12:48 -0400211 : mFactory(factory), mStreamingBuffer(factory)
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400212{
Jamie Madillb1565902018-07-27 08:12:48 -0400213 for (int currentValueIndex = 0; currentValueIndex < gl::MAX_VERTEX_ATTRIBS; ++currentValueIndex)
214 {
215 mCurrentValueCache.emplace_back(factory);
216 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000217}
218
219VertexDataManager::~VertexDataManager()
220{
Jamie Madill401345e2017-08-21 10:52:40 -0400221}
222
Jamie Madillb1565902018-07-27 08:12:48 -0400223gl::Error VertexDataManager::initialize(const gl::Context *context)
Jamie Madill401345e2017-08-21 10:52:40 -0400224{
Jamie Madillb1565902018-07-27 08:12:48 -0400225 return mStreamingBuffer.initialize(context, INITIAL_STREAM_BUFFER_SIZE);
Jamie Madill401345e2017-08-21 10:52:40 -0400226}
227
228void VertexDataManager::deinitialize()
229{
230 mStreamingBuffer.reset();
231 mCurrentValueCache.clear();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000232}
233
Jamie Madill33510102017-09-20 10:39:18 -0400234gl::Error VertexDataManager::prepareVertexData(const gl::Context *context,
Jamie Madill476682e2015-06-30 10:04:29 -0400235 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 Madill52b09c22016-04-11 14:12:31 -0400301 return gl::NoError();
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
309 return gl::NoError();
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 Madill33510102017-09-20 10:39:18 -0400337gl::Error 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 Madillb1565902018-07-27 08:12:48 -0400358 ANGLE_TRY_RESULT(
359 bufferD3D->getFactory()->getVertexSpaceRequired(context, attrib, binding, 1, 0),
360 translated->stride);
Jamie Madille18eb972016-03-04 15:46:59 -0500361
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800362 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
Jamie Madille18eb972016-03-04 15:46:59 -0500363 ASSERT(staticBuffer);
364
365 if (staticBuffer->empty())
366 {
367 // Convert the entire buffer
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800368 int totalCount =
369 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
370 int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500371
Jamie Madillb1565902018-07-27 08:12:48 -0400372 ANGLE_TRY(staticBuffer->storeStaticAttribute(context, attrib, binding, -startIndex,
373 totalCount, 0, sourceData));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000374 }
375
Jamie Madille18eb972016-03-04 15:46:59 -0500376 unsigned int firstElementOffset =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800377 (static_cast<unsigned int>(offset) /
378 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
Jamie Madille18eb972016-03-04 15:46:59 -0500379 translated->stride;
Jamie Madill27c08912015-06-22 13:57:20 -0400380
Jamie Madille18eb972016-03-04 15:46:59 -0500381 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
382
Jamie Madille2e406c2016-06-02 13:04:10 -0400383 CheckedNumeric<unsigned int> checkedOffset(streamOffset);
384 checkedOffset += firstElementOffset;
385
386 if (!checkedOffset.IsValid())
Jamie Madill52b09c22016-04-11 14:12:31 -0400387 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500388 return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib";
Jamie Madill52b09c22016-04-11 14:12:31 -0400389 }
390
Jamie Madille18eb972016-03-04 15:46:59 -0500391 translated->vertexBuffer.set(vertexBuffer);
392 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400393 translated->baseOffset = streamOffset + firstElementOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500394
Jamie Madill52b09c22016-04-11 14:12:31 -0400395 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300396 translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madill52b09c22016-04-11 14:12:31 -0400397
398 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500399}
400
401gl::Error VertexDataManager::storeDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400402 const gl::Context *context,
Jamie Madille18eb972016-03-04 15:46:59 -0500403 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill52b09c22016-04-11 14:12:31 -0400404 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madille18eb972016-03-04 15:46:59 -0500405 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400406 size_t count,
Jamie Madille18eb972016-03-04 15:46:59 -0500407 GLsizei instances)
408{
Jamie Madill52b09c22016-04-11 14:12:31 -0400409 // Instantiating this class will ensure the streaming buffer is never left mapped.
Jamie Madille2e406c2016-06-02 13:04:10 -0400410 class StreamingBufferUnmapper final : NonCopyable
Jamie Madill52b09c22016-04-11 14:12:31 -0400411 {
412 public:
413 StreamingBufferUnmapper(StreamingVertexBufferInterface *streamingBuffer)
414 : mStreamingBuffer(streamingBuffer)
415 {
416 ASSERT(mStreamingBuffer);
417 }
418 ~StreamingBufferUnmapper() { mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); }
419
420 private:
421 StreamingVertexBufferInterface *mStreamingBuffer;
422 };
423
424 // Will trigger unmapping on return.
Jamie Madill021bad42018-07-27 08:12:47 -0400425 StreamingBufferUnmapper localUnmapper(&mStreamingBuffer);
Jamie Madill52b09c22016-04-11 14:12:31 -0400426
Jamie Madille18eb972016-03-04 15:46:59 -0500427 // Reserve the required space for the dynamic buffers.
Jamie Madill6de51852017-04-12 09:53:01 -0400428 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500429 {
430 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Jamie Madillb1565902018-07-27 08:12:48 -0400431 ANGLE_TRY(reserveSpaceForAttrib(context, dynamicAttrib, start, count, instances));
Jamie Madille18eb972016-03-04 15:46:59 -0500432 }
433
434 // Store dynamic attributes
Jamie Madill6de51852017-04-12 09:53:01 -0400435 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500436 {
437 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill33510102017-09-20 10:39:18 -0400438 ANGLE_TRY(storeDynamicAttrib(context, dynamicAttrib, start, count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400439 }
Shannon Woods1a965482014-09-22 18:00:32 -0400440
Jamie Madill52b09c22016-04-11 14:12:31 -0400441 return gl::NoError();
442}
443
444void VertexDataManager::PromoteDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400445 const gl::Context *context,
Jamie Madill52b09c22016-04-11 14:12:31 -0400446 const std::vector<TranslatedAttribute> &translatedAttribs,
447 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madill18e323a2018-05-11 16:54:17 -0400448 size_t count)
Jamie Madill52b09c22016-04-11 14:12:31 -0400449{
Jamie Madill6de51852017-04-12 09:53:01 -0400450 for (auto attribIndex : dynamicAttribsMask)
Jamie Madill52b09c22016-04-11 14:12:31 -0400451 {
452 const auto &dynamicAttrib = translatedAttribs[attribIndex];
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800453 ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800454 const auto &binding = *dynamicAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800455
Martin Radevdd5f27e2017-06-07 10:17:09 +0300456 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill27c08912015-06-22 13:57:20 -0400457 if (buffer)
458 {
Jamie Madill18e323a2018-05-11 16:54:17 -0400459 // Note: this multiplication can overflow. It should not be a security problem.
Jamie Madill27c08912015-06-22 13:57:20 -0400460 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800461 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
Jamie Madill18e323a2018-05-11 16:54:17 -0400462 bufferD3D->promoteStaticUsage(context, count * typeSize);
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000463 }
464 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000465}
466
Jamie Madillb1565902018-07-27 08:12:48 -0400467gl::Error VertexDataManager::reserveSpaceForAttrib(const gl::Context *context,
468 const TranslatedAttribute &translatedAttrib,
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800469 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400470 size_t count,
Jamie Madill021bad42018-07-27 08:12:47 -0400471 GLsizei instances)
Jamie Madill6d113802014-08-25 15:47:52 -0400472{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800473 ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800474 const auto &attrib = *translatedAttrib.attribute;
475 const auto &binding = *translatedAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800476
Jamie Madillb1565902018-07-27 08:12:48 -0400477 ASSERT(!DirectStoragePossible(context, attrib, binding));
Jamie Madill6d113802014-08-25 15:47:52 -0400478
Martin Radevdd5f27e2017-06-07 10:17:09 +0300479 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500480 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800481 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
Jamie Madille36b92d2016-03-04 15:46:58 -0500482
Jamie Madill18e323a2018-05-11 16:54:17 -0400483 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
484 static_cast<size_t>(instances));
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800485 // TODO(jiajia.qin@intel.com): force the index buffer to clamp any out of range indices instead
486 // of invalid operation here.
487 if (bufferD3D)
488 {
489 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
490 // a non-instanced draw call
491 GLint firstVertexIndex = binding.getDivisor() > 0 ? 0 : start;
492 int64_t maxVertexCount =
493 static_cast<int64_t>(firstVertexIndex) + static_cast<int64_t>(totalCount);
494 int elementsInBuffer =
495 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
Jamie Madille36b92d2016-03-04 15:46:58 -0500496
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800497 if (maxVertexCount > elementsInBuffer)
498 {
499 return gl::InvalidOperation() << "Vertex buffer is not big enough for the draw call.";
500 }
501 }
Jamie Madillb1565902018-07-27 08:12:48 -0400502 return mStreamingBuffer.reserveVertexSpace(context, attrib, binding, totalCount, instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400503}
504
Jamie Madill33510102017-09-20 10:39:18 -0400505gl::Error VertexDataManager::storeDynamicAttrib(const gl::Context *context,
506 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500507 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400508 size_t count,
Jamie Madill021bad42018-07-27 08:12:47 -0400509 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400510{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800511 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800512 const auto &attrib = *translated->attribute;
513 const auto &binding = *translated->binding;
Jamie Madill9c385802015-06-22 13:57:18 -0400514
Martin Radevdd5f27e2017-06-07 10:17:09 +0300515 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -0400516 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400517 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400518
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500519 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400520
Jamie Madilld4b55a02015-01-09 14:21:49 -0500521 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300522 GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500523
Jamie Madill7d112bb2015-06-22 13:57:19 -0400524 // Compute source data pointer
525 const uint8_t *sourceData = nullptr;
526
527 if (buffer)
528 {
Jamie Madill33510102017-09-20 10:39:18 -0400529 ANGLE_TRY(storage->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800530 sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400531 }
532 else
533 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800534 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
535 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill7d112bb2015-06-22 13:57:19 -0400536 sourceData = static_cast<const uint8_t*>(attrib.pointer);
537 }
538
539 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500540
Jamie Madilld8fa9212016-03-02 11:51:43 -0500541 translated->storage = nullptr;
Jamie Madillb1565902018-07-27 08:12:48 -0400542 ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(context, attrib, binding, 1, 0),
543 translated->stride);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500544
Jamie Madill18e323a2018-05-11 16:54:17 -0400545 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
546 static_cast<size_t>(instances));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400547
Jamie Madill021bad42018-07-27 08:12:47 -0400548 ANGLE_TRY(mStreamingBuffer.storeDynamicAttribute(
Jamie Madillb1565902018-07-27 08:12:48 -0400549 context, attrib, binding, translated->currentValueType, firstVertexIndex,
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800550 static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400551
Jamie Madill021bad42018-07-27 08:12:47 -0400552 VertexBuffer *vertexBuffer = mStreamingBuffer.getVertexBuffer();
Jamie Madille18eb972016-03-04 15:46:59 -0500553
554 translated->vertexBuffer.set(vertexBuffer);
555 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400556 translated->baseOffset = streamOffset;
557 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400558
Jamie Madill52b09c22016-04-11 14:12:31 -0400559 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400560}
561
Jamie Madillb1565902018-07-27 08:12:48 -0400562gl::Error VertexDataManager::storeCurrentValue(const gl::Context *context,
563 const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400564 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500565 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400566{
Jamie Madille18eb972016-03-04 15:46:59 -0500567 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
Jamie Madillb1565902018-07-27 08:12:48 -0400568 StreamingVertexBufferInterface &buffer = *cachedState->buffer;
Jamie Madille18eb972016-03-04 15:46:59 -0500569
Jamie Madillb1565902018-07-27 08:12:48 -0400570 if (buffer.getBufferSize() == 0)
Jamie Madille18eb972016-03-04 15:46:59 -0500571 {
Jamie Madillb1565902018-07-27 08:12:48 -0400572 ANGLE_TRY(buffer.initialize(context, CONSTANT_VERTEX_BUFFER_SIZE));
Jamie Madille18eb972016-03-04 15:46:59 -0500573 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500574
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400575 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400576 {
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800577 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800578 const auto &attrib = *translated->attribute;
579 const auto &binding = *translated->binding;
Jamie Madill27c08912015-06-22 13:57:20 -0400580
Jamie Madillb1565902018-07-27 08:12:48 -0400581 ANGLE_TRY(buffer.reserveVertexSpace(context, attrib, binding, 1, 0));
Jamie Madillf41522b2014-08-18 16:39:49 -0400582
Jamie Madill7d112bb2015-06-22 13:57:19 -0400583 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400584 unsigned int streamOffset;
Jamie Madillb1565902018-07-27 08:12:48 -0400585 ANGLE_TRY(buffer.storeDynamicAttribute(context, attrib, binding, currentValue.Type, 0, 1, 0,
586 &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400587
Jamie Madillb1565902018-07-27 08:12:48 -0400588 buffer.getVertexBuffer()->hintUnmapResource();
Jamie Madille36b92d2016-03-04 15:46:58 -0500589
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400590 cachedState->data = currentValue;
591 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400592 }
593
Jamie Madillb1565902018-07-27 08:12:48 -0400594 translated->vertexBuffer.set(buffer.getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400595
Jamie Madille36b92d2016-03-04 15:46:58 -0500596 translated->storage = nullptr;
Jamie Madillb1565902018-07-27 08:12:48 -0400597 translated->serial = buffer.getSerial();
Jamie Madille36b92d2016-03-04 15:46:58 -0500598 translated->divisor = 0;
599 translated->stride = 0;
Jamie Madill52b09c22016-04-11 14:12:31 -0400600 translated->baseOffset = static_cast<unsigned int>(cachedState->offset);
601 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400602
Jamie Madill52b09c22016-04-11 14:12:31 -0400603 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400604}
605
Jamie Madille36b92d2016-03-04 15:46:58 -0500606// VertexBufferBinding implementation
607VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
608{
609}
610
611VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
612 : mBoundVertexBuffer(other.mBoundVertexBuffer)
613{
614 if (mBoundVertexBuffer)
615 {
616 mBoundVertexBuffer->addRef();
617 }
618}
619
620VertexBufferBinding::~VertexBufferBinding()
621{
622 if (mBoundVertexBuffer)
623 {
624 mBoundVertexBuffer->release();
625 }
626}
627
628VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
629{
630 mBoundVertexBuffer = other.mBoundVertexBuffer;
631 if (mBoundVertexBuffer)
632 {
633 mBoundVertexBuffer->addRef();
634 }
635 return *this;
636}
637
638void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
639{
640 if (mBoundVertexBuffer == vertexBuffer)
641 return;
642
643 if (mBoundVertexBuffer)
644 {
645 mBoundVertexBuffer->release();
646 }
647 if (vertexBuffer)
648 {
649 vertexBuffer->addRef();
650 }
651
652 mBoundVertexBuffer = vertexBuffer;
653}
654
655VertexBuffer *VertexBufferBinding::get() const
656{
657 return mBoundVertexBuffer;
658}
659
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500660} // namespace rx