blob: 8e5315525daf593ccc839f43dea3c36a3ba77c7d [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 Madill52b09c22016-04-11 14:12:31 -0400131gl::ErrorOrResult<unsigned int> TranslatedAttribute::computeOffset(GLint startVertex) const
132{
Jamie Madille2e406c2016-06-02 13:04:10 -0400133 if (!usesFirstVertexOffset)
Jamie Madill52b09c22016-04-11 14:12:31 -0400134 {
Jamie Madille2e406c2016-06-02 13:04:10 -0400135 return baseOffset;
Jamie Madill52b09c22016-04-11 14:12:31 -0400136 }
Jamie Madille2e406c2016-06-02 13:04:10 -0400137
138 CheckedNumeric<unsigned int> offset;
139
140 offset = baseOffset + stride * static_cast<unsigned int>(startVertex);
Jamie Madillca2ff382018-07-11 09:01:17 -0400141 ANGLE_TRY_CHECKED_MATH(offset.IsValid());
Jamie Madille2e406c2016-06-02 13:04:10 -0400142 return offset.ValueOrDie();
Jamie Madill52b09c22016-04-11 14:12:31 -0400143}
144
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800145// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
146VertexStorageType ClassifyAttributeStorage(const gl::VertexAttribute &attrib,
147 const gl::VertexBinding &binding)
Jamie Madille18eb972016-03-04 15:46:59 -0500148{
149 // If attribute is disabled, we use the current value.
150 if (!attrib.enabled)
151 {
152 return VertexStorageType::CURRENT_VALUE;
153 }
154
155 // If specified with immediate data, we must use dynamic storage.
Jamie Madill09463932018-04-04 05:26:59 -0400156 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille18eb972016-03-04 15:46:59 -0500157 if (!buffer)
158 {
159 return VertexStorageType::DYNAMIC;
160 }
161
162 // Check if the buffer supports direct storage.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800163 if (DirectStoragePossible(attrib, binding))
Jamie Madille18eb972016-03-04 15:46:59 -0500164 {
165 return VertexStorageType::DIRECT;
166 }
167
168 // Otherwise the storage is static or dynamic.
169 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
170 ASSERT(bufferD3D);
171 switch (bufferD3D->getUsage())
172 {
173 case D3DBufferUsage::DYNAMIC:
174 return VertexStorageType::DYNAMIC;
175 case D3DBufferUsage::STATIC:
176 return VertexStorageType::STATIC;
177 default:
178 UNREACHABLE();
179 return VertexStorageType::UNKNOWN;
180 }
181}
182
Jamie Madill401345e2017-08-21 10:52:40 -0400183VertexDataManager::CurrentValueState::CurrentValueState() : buffer(), offset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000184{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400185 data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
186 data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
187 data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
188 data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
189 data.Type = GL_FLOAT;
190}
daniel@transgaming.com83921382011-01-08 05:46:00 +0000191
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400192VertexDataManager::CurrentValueState::~CurrentValueState()
193{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400194}
195
196VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
Jamie Madill401345e2017-08-21 10:52:40 -0400197 : mFactory(factory), mStreamingBuffer(), mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS)
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400198{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000199}
200
201VertexDataManager::~VertexDataManager()
202{
Jamie Madill401345e2017-08-21 10:52:40 -0400203}
204
205gl::Error VertexDataManager::initialize()
206{
207 mStreamingBuffer.reset(
208 new StreamingVertexBufferInterface(mFactory, INITIAL_STREAM_BUFFER_SIZE));
Jamie Madilldec86232018-07-11 09:01:18 -0400209 ANGLE_TRY_ALLOCATION(mStreamingBuffer);
Jamie Madill401345e2017-08-21 10:52:40 -0400210 return gl::NoError();
211}
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 Madille18eb972016-03-04 15:46:59 -0500225 ASSERT(mStreamingBuffer);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000226
Jamie Madill33510102017-09-20 10:39:18 -0400227 const gl::State &state = context->getGLState();
Geoff Lang5ead9272015-03-25 12:27:43 -0400228 const gl::VertexArray *vertexArray = state.getVertexArray();
Jamie Madill63805b42015-08-25 13:17:39 -0400229 const auto &vertexAttributes = vertexArray->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800230 const auto &vertexBindings = vertexArray->getVertexBindings();
Geoff Lang5ead9272015-03-25 12:27:43 -0400231
Jamie Madill52b09c22016-04-11 14:12:31 -0400232 mDynamicAttribsMaskCache.reset();
Jamie Madille18eb972016-03-04 15:46:59 -0500233 const gl::Program *program = state.getProgram();
234
Jamie Madill476682e2015-06-30 10:04:29 -0400235 translatedAttribs->clear();
Jamie Madill27c08912015-06-22 13:57:20 -0400236
Jamie Madill9c385802015-06-22 13:57:18 -0400237 for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000238 {
Jamie Madille18eb972016-03-04 15:46:59 -0500239 // Skip attrib locations the program doesn't use.
240 if (!program->isAttribLocationActive(attribIndex))
241 continue;
242
243 const auto &attrib = vertexAttributes[attribIndex];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800244 const auto &binding = vertexBindings[attrib.bindingIndex];
Jamie Madille18eb972016-03-04 15:46:59 -0500245
246 // Resize automatically puts in empty attribs
247 translatedAttribs->resize(attribIndex + 1);
248
249 TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
Jamie Madill6de51852017-04-12 09:53:01 -0400250 auto currentValueData = state.getVertexAttribCurrentValue(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500251
252 // Record the attribute now
253 translated->active = true;
254 translated->attribute = &attrib;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800255 translated->binding = &binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500256 translated->currentValueType = currentValueData.Type;
Martin Radevdd5f27e2017-06-07 10:17:09 +0300257 translated->divisor = binding.getDivisor();
Jamie Madille18eb972016-03-04 15:46:59 -0500258
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800259 switch (ClassifyAttributeStorage(attrib, binding))
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000260 {
Jamie Madille18eb972016-03-04 15:46:59 -0500261 case VertexStorageType::STATIC:
Jamie Madill9c385802015-06-22 13:57:18 -0400262 {
Jamie Madille18eb972016-03-04 15:46:59 -0500263 // Store static attribute.
Jamie Madill33510102017-09-20 10:39:18 -0400264 ANGLE_TRY(StoreStaticAttrib(context, translated));
Jamie Madille18eb972016-03-04 15:46:59 -0500265 break;
Jamie Madill9c385802015-06-22 13:57:18 -0400266 }
Jamie Madille18eb972016-03-04 15:46:59 -0500267 case VertexStorageType::DYNAMIC:
268 // Dynamic attributes must be handled together.
Jamie Madill52b09c22016-04-11 14:12:31 -0400269 mDynamicAttribsMaskCache.set(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500270 break;
271 case VertexStorageType::DIRECT:
272 // Update translated data for direct attributes.
Jamie Madill52b09c22016-04-11 14:12:31 -0400273 StoreDirectAttrib(translated);
Jamie Madille18eb972016-03-04 15:46:59 -0500274 break;
275 case VertexStorageType::CURRENT_VALUE:
Jamie Madill27c08912015-06-22 13:57:20 -0400276 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400277 ANGLE_TRY(storeCurrentValue(currentValueData, translated, attribIndex));
Jamie Madille18eb972016-03-04 15:46:59 -0500278 break;
Jamie Madill27c08912015-06-22 13:57:20 -0400279 }
Jamie Madille18eb972016-03-04 15:46:59 -0500280 default:
281 UNREACHABLE();
282 break;
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000283 }
284 }
285
Jamie Madill52b09c22016-04-11 14:12:31 -0400286 if (mDynamicAttribsMaskCache.none())
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000287 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400288 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500289 }
290
Jamie Madill33510102017-09-20 10:39:18 -0400291 ANGLE_TRY(storeDynamicAttribs(context, translatedAttribs, mDynamicAttribsMaskCache, start,
292 count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400293
Jamie Madill33510102017-09-20 10:39:18 -0400294 PromoteDynamicAttribs(context, *translatedAttribs, mDynamicAttribsMaskCache, count);
Jamie Madill52b09c22016-04-11 14:12:31 -0400295
296 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500297}
298
299// static
Jamie Madill52b09c22016-04-11 14:12:31 -0400300void VertexDataManager::StoreDirectAttrib(TranslatedAttribute *directAttrib)
Jamie Madille18eb972016-03-04 15:46:59 -0500301{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800302 ASSERT(directAttrib->attribute && directAttrib->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800303 const auto &attrib = *directAttrib->attribute;
304 const auto &binding = *directAttrib->binding;
305
Martin Radevdd5f27e2017-06-07 10:17:09 +0300306 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill7aa786c2017-09-19 13:57:15 -0400307 ASSERT(buffer);
308 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jamie Madille18eb972016-03-04 15:46:59 -0500309
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800310 ASSERT(DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500311 directAttrib->vertexBuffer.set(nullptr);
312 directAttrib->storage = bufferD3D;
313 directAttrib->serial = bufferD3D->getSerial();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800314 directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding));
315 directAttrib->baseOffset =
316 static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill52b09c22016-04-11 14:12:31 -0400317
318 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300319 directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madille18eb972016-03-04 15:46:59 -0500320}
321
322// static
Jamie Madill33510102017-09-20 10:39:18 -0400323gl::Error VertexDataManager::StoreStaticAttrib(const gl::Context *context,
324 TranslatedAttribute *translated)
Jamie Madille18eb972016-03-04 15:46:59 -0500325{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800326 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800327 const auto &attrib = *translated->attribute;
328 const auto &binding = *translated->binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500329
Martin Radevdd5f27e2017-06-07 10:17:09 +0300330 gl::Buffer *buffer = binding.getBuffer().get();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800331 ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500332 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
333
Jamie Madille18eb972016-03-04 15:46:59 -0500334 // Compute source data pointer
335 const uint8_t *sourceData = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800336 const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500337
Jamie Madill33510102017-09-20 10:39:18 -0400338 ANGLE_TRY(bufferD3D->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800339 sourceData += offset;
Jamie Madille18eb972016-03-04 15:46:59 -0500340
341 unsigned int streamOffset = 0;
342
Jamie Madille18eb972016-03-04 15:46:59 -0500343 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800344 ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, binding, 1, 0),
Jamie Madill52b09c22016-04-11 14:12:31 -0400345 translated->stride);
Jamie Madille18eb972016-03-04 15:46:59 -0500346
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800347 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
Jamie Madille18eb972016-03-04 15:46:59 -0500348 ASSERT(staticBuffer);
349
350 if (staticBuffer->empty())
351 {
352 // Convert the entire buffer
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800353 int totalCount =
354 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
355 int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500356
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800357 ANGLE_TRY(staticBuffer->storeStaticAttribute(attrib, binding, -startIndex, totalCount, 0,
358 sourceData));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000359 }
360
Jamie Madille18eb972016-03-04 15:46:59 -0500361 unsigned int firstElementOffset =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800362 (static_cast<unsigned int>(offset) /
363 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
Jamie Madille18eb972016-03-04 15:46:59 -0500364 translated->stride;
Jamie Madill27c08912015-06-22 13:57:20 -0400365
Jamie Madille18eb972016-03-04 15:46:59 -0500366 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
367
Jamie Madille2e406c2016-06-02 13:04:10 -0400368 CheckedNumeric<unsigned int> checkedOffset(streamOffset);
369 checkedOffset += firstElementOffset;
370
371 if (!checkedOffset.IsValid())
Jamie Madill52b09c22016-04-11 14:12:31 -0400372 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500373 return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib";
Jamie Madill52b09c22016-04-11 14:12:31 -0400374 }
375
Jamie Madille18eb972016-03-04 15:46:59 -0500376 translated->vertexBuffer.set(vertexBuffer);
377 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400378 translated->baseOffset = streamOffset + firstElementOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500379
Jamie Madill52b09c22016-04-11 14:12:31 -0400380 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300381 translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madill52b09c22016-04-11 14:12:31 -0400382
383 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500384}
385
386gl::Error VertexDataManager::storeDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400387 const gl::Context *context,
Jamie Madille18eb972016-03-04 15:46:59 -0500388 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill52b09c22016-04-11 14:12:31 -0400389 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madille18eb972016-03-04 15:46:59 -0500390 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400391 size_t count,
Jamie Madille18eb972016-03-04 15:46:59 -0500392 GLsizei instances)
393{
Jamie Madill52b09c22016-04-11 14:12:31 -0400394 // Instantiating this class will ensure the streaming buffer is never left mapped.
Jamie Madille2e406c2016-06-02 13:04:10 -0400395 class StreamingBufferUnmapper final : NonCopyable
Jamie Madill52b09c22016-04-11 14:12:31 -0400396 {
397 public:
398 StreamingBufferUnmapper(StreamingVertexBufferInterface *streamingBuffer)
399 : mStreamingBuffer(streamingBuffer)
400 {
401 ASSERT(mStreamingBuffer);
402 }
403 ~StreamingBufferUnmapper() { mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); }
404
405 private:
406 StreamingVertexBufferInterface *mStreamingBuffer;
407 };
408
409 // Will trigger unmapping on return.
Jamie Madill401345e2017-08-21 10:52:40 -0400410 StreamingBufferUnmapper localUnmapper(mStreamingBuffer.get());
Jamie Madill52b09c22016-04-11 14:12:31 -0400411
Jamie Madille18eb972016-03-04 15:46:59 -0500412 // Reserve the required space for the dynamic buffers.
Jamie Madill6de51852017-04-12 09:53:01 -0400413 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500414 {
415 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800416 ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, start, count, instances));
Jamie Madille18eb972016-03-04 15:46:59 -0500417 }
418
419 // Store dynamic attributes
Jamie Madill6de51852017-04-12 09:53:01 -0400420 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500421 {
422 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill33510102017-09-20 10:39:18 -0400423 ANGLE_TRY(storeDynamicAttrib(context, dynamicAttrib, start, count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400424 }
Shannon Woods1a965482014-09-22 18:00:32 -0400425
Jamie Madill52b09c22016-04-11 14:12:31 -0400426 return gl::NoError();
427}
428
429void VertexDataManager::PromoteDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400430 const gl::Context *context,
Jamie Madill52b09c22016-04-11 14:12:31 -0400431 const std::vector<TranslatedAttribute> &translatedAttribs,
432 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madill18e323a2018-05-11 16:54:17 -0400433 size_t count)
Jamie Madill52b09c22016-04-11 14:12:31 -0400434{
Jamie Madill6de51852017-04-12 09:53:01 -0400435 for (auto attribIndex : dynamicAttribsMask)
Jamie Madill52b09c22016-04-11 14:12:31 -0400436 {
437 const auto &dynamicAttrib = translatedAttribs[attribIndex];
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800438 ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800439 const auto &binding = *dynamicAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800440
Martin Radevdd5f27e2017-06-07 10:17:09 +0300441 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill27c08912015-06-22 13:57:20 -0400442 if (buffer)
443 {
Jamie Madill18e323a2018-05-11 16:54:17 -0400444 // Note: this multiplication can overflow. It should not be a security problem.
Jamie Madill27c08912015-06-22 13:57:20 -0400445 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800446 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
Jamie Madill18e323a2018-05-11 16:54:17 -0400447 bufferD3D->promoteStaticUsage(context, count * typeSize);
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000448 }
449 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000450}
451
Jamie Madill3d72cc72015-06-22 13:57:19 -0400452gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800453 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400454 size_t count,
Geoff Langf7100b92014-09-08 16:17:08 -0400455 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400456{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800457 ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800458 const auto &attrib = *translatedAttrib.attribute;
459 const auto &binding = *translatedAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800460
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800461 ASSERT(!DirectStoragePossible(attrib, binding));
Jamie Madill6d113802014-08-25 15:47:52 -0400462
Martin Radevdd5f27e2017-06-07 10:17:09 +0300463 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500464 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800465 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
Jamie Madille36b92d2016-03-04 15:46:58 -0500466
Jamie Madill18e323a2018-05-11 16:54:17 -0400467 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
468 static_cast<size_t>(instances));
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800469 // TODO(jiajia.qin@intel.com): force the index buffer to clamp any out of range indices instead
470 // of invalid operation here.
471 if (bufferD3D)
472 {
473 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
474 // a non-instanced draw call
475 GLint firstVertexIndex = binding.getDivisor() > 0 ? 0 : start;
476 int64_t maxVertexCount =
477 static_cast<int64_t>(firstVertexIndex) + static_cast<int64_t>(totalCount);
478 int elementsInBuffer =
479 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
Jamie Madille36b92d2016-03-04 15:46:58 -0500480
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800481 if (maxVertexCount > elementsInBuffer)
482 {
483 return gl::InvalidOperation() << "Vertex buffer is not big enough for the draw call.";
484 }
485 }
Jamie Madill18e323a2018-05-11 16:54:17 -0400486 return mStreamingBuffer->reserveVertexSpace(attrib, binding, totalCount, instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400487}
488
Jamie Madill33510102017-09-20 10:39:18 -0400489gl::Error VertexDataManager::storeDynamicAttrib(const gl::Context *context,
490 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500491 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400492 size_t count,
493 GLsizei instances) const
Jamie Madillf41522b2014-08-18 16:39:49 -0400494{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800495 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800496 const auto &attrib = *translated->attribute;
497 const auto &binding = *translated->binding;
Jamie Madill9c385802015-06-22 13:57:18 -0400498
Martin Radevdd5f27e2017-06-07 10:17:09 +0300499 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -0400500 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400501 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400502
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500503 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400504
Jamie Madilld4b55a02015-01-09 14:21:49 -0500505 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300506 GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500507
Jamie Madill7d112bb2015-06-22 13:57:19 -0400508 // Compute source data pointer
509 const uint8_t *sourceData = nullptr;
510
511 if (buffer)
512 {
Jamie Madill33510102017-09-20 10:39:18 -0400513 ANGLE_TRY(storage->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800514 sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400515 }
516 else
517 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800518 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
519 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill7d112bb2015-06-22 13:57:19 -0400520 sourceData = static_cast<const uint8_t*>(attrib.pointer);
521 }
522
523 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500524
Jamie Madilld8fa9212016-03-02 11:51:43 -0500525 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800526 ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, binding, 1, 0), translated->stride);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500527
Jamie Madill18e323a2018-05-11 16:54:17 -0400528 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
529 static_cast<size_t>(instances));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400530
Jamie Madill52b09c22016-04-11 14:12:31 -0400531 ANGLE_TRY(mStreamingBuffer->storeDynamicAttribute(
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800532 attrib, binding, translated->currentValueType, firstVertexIndex,
533 static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400534
Jamie Madille18eb972016-03-04 15:46:59 -0500535 VertexBuffer *vertexBuffer = mStreamingBuffer->getVertexBuffer();
536
537 translated->vertexBuffer.set(vertexBuffer);
538 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400539 translated->baseOffset = streamOffset;
540 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400541
Jamie Madill52b09c22016-04-11 14:12:31 -0400542 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400543}
544
Jamie Madill9c385802015-06-22 13:57:18 -0400545gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400546 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500547 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400548{
Jamie Madille18eb972016-03-04 15:46:59 -0500549 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
Jamie Madill401345e2017-08-21 10:52:40 -0400550 auto &buffer = cachedState->buffer;
Jamie Madille18eb972016-03-04 15:46:59 -0500551
552 if (!buffer)
553 {
Jamie Madill401345e2017-08-21 10:52:40 -0400554 buffer.reset(new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE));
Jamie Madille18eb972016-03-04 15:46:59 -0500555 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500556
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400557 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400558 {
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800559 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800560 const auto &attrib = *translated->attribute;
561 const auto &binding = *translated->binding;
Jamie Madill27c08912015-06-22 13:57:20 -0400562
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800563 ANGLE_TRY(buffer->reserveVertexSpace(attrib, binding, 1, 0));
Jamie Madillf41522b2014-08-18 16:39:49 -0400564
Jamie Madill7d112bb2015-06-22 13:57:19 -0400565 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400566 unsigned int streamOffset;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800567 ANGLE_TRY(buffer->storeDynamicAttribute(attrib, binding, currentValue.Type, 0, 1, 0,
568 &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400569
Jamie Madille36b92d2016-03-04 15:46:58 -0500570 buffer->getVertexBuffer()->hintUnmapResource();
571
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400572 cachedState->data = currentValue;
573 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400574 }
575
Jamie Madille36b92d2016-03-04 15:46:58 -0500576 translated->vertexBuffer.set(buffer->getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400577
Jamie Madille36b92d2016-03-04 15:46:58 -0500578 translated->storage = nullptr;
579 translated->serial = buffer->getSerial();
580 translated->divisor = 0;
581 translated->stride = 0;
Jamie Madill52b09c22016-04-11 14:12:31 -0400582 translated->baseOffset = static_cast<unsigned int>(cachedState->offset);
583 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400584
Jamie Madill52b09c22016-04-11 14:12:31 -0400585 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400586}
587
Jamie Madille36b92d2016-03-04 15:46:58 -0500588// VertexBufferBinding implementation
589VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
590{
591}
592
593VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
594 : mBoundVertexBuffer(other.mBoundVertexBuffer)
595{
596 if (mBoundVertexBuffer)
597 {
598 mBoundVertexBuffer->addRef();
599 }
600}
601
602VertexBufferBinding::~VertexBufferBinding()
603{
604 if (mBoundVertexBuffer)
605 {
606 mBoundVertexBuffer->release();
607 }
608}
609
610VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
611{
612 mBoundVertexBuffer = other.mBoundVertexBuffer;
613 if (mBoundVertexBuffer)
614 {
615 mBoundVertexBuffer->addRef();
616 }
617 return *this;
618}
619
620void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
621{
622 if (mBoundVertexBuffer == vertexBuffer)
623 return;
624
625 if (mBoundVertexBuffer)
626 {
627 mBoundVertexBuffer->release();
628 }
629 if (vertexBuffer)
630 {
631 vertexBuffer->addRef();
632 }
633
634 mBoundVertexBuffer = vertexBuffer;
635}
636
637VertexBuffer *VertexBufferBinding::get() const
638{
639 return mBoundVertexBuffer;
640}
641
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500642} // namespace rx