blob: 54ad5e54f5bba365fdb0206a91229892c9406ff9 [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);
141 ANGLE_TRY_CHECKED_MATH(offset);
142 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.
Martin Radevdd5f27e2017-06-07 10:17:09 +0300156 auto *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));
209 if (!mStreamingBuffer)
210 {
211 return gl::OutOfMemory() << "Failed to allocate the streaming vertex buffer.";
212 }
213
214 return gl::NoError();
215}
216
217void VertexDataManager::deinitialize()
218{
219 mStreamingBuffer.reset();
220 mCurrentValueCache.clear();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000221}
222
Jamie Madill33510102017-09-20 10:39:18 -0400223gl::Error VertexDataManager::prepareVertexData(const gl::Context *context,
Jamie Madill476682e2015-06-30 10:04:29 -0400224 GLint start,
225 GLsizei count,
226 std::vector<TranslatedAttribute> *translatedAttribs,
227 GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000228{
Jamie Madille18eb972016-03-04 15:46:59 -0500229 ASSERT(mStreamingBuffer);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000230
Jamie Madill33510102017-09-20 10:39:18 -0400231 const gl::State &state = context->getGLState();
Geoff Lang5ead9272015-03-25 12:27:43 -0400232 const gl::VertexArray *vertexArray = state.getVertexArray();
Jamie Madill63805b42015-08-25 13:17:39 -0400233 const auto &vertexAttributes = vertexArray->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800234 const auto &vertexBindings = vertexArray->getVertexBindings();
Geoff Lang5ead9272015-03-25 12:27:43 -0400235
Jamie Madill52b09c22016-04-11 14:12:31 -0400236 mDynamicAttribsMaskCache.reset();
Jamie Madille18eb972016-03-04 15:46:59 -0500237 const gl::Program *program = state.getProgram();
238
Jamie Madill476682e2015-06-30 10:04:29 -0400239 translatedAttribs->clear();
Jamie Madill27c08912015-06-22 13:57:20 -0400240
Jamie Madill9c385802015-06-22 13:57:18 -0400241 for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000242 {
Jamie Madille18eb972016-03-04 15:46:59 -0500243 // Skip attrib locations the program doesn't use.
244 if (!program->isAttribLocationActive(attribIndex))
245 continue;
246
247 const auto &attrib = vertexAttributes[attribIndex];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800248 const auto &binding = vertexBindings[attrib.bindingIndex];
Jamie Madille18eb972016-03-04 15:46:59 -0500249
250 // Resize automatically puts in empty attribs
251 translatedAttribs->resize(attribIndex + 1);
252
253 TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
Jamie Madill6de51852017-04-12 09:53:01 -0400254 auto currentValueData = state.getVertexAttribCurrentValue(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500255
256 // Record the attribute now
257 translated->active = true;
258 translated->attribute = &attrib;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800259 translated->binding = &binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500260 translated->currentValueType = currentValueData.Type;
Martin Radevdd5f27e2017-06-07 10:17:09 +0300261 translated->divisor = binding.getDivisor();
Jamie Madille18eb972016-03-04 15:46:59 -0500262
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800263 switch (ClassifyAttributeStorage(attrib, binding))
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000264 {
Jamie Madille18eb972016-03-04 15:46:59 -0500265 case VertexStorageType::STATIC:
Jamie Madill9c385802015-06-22 13:57:18 -0400266 {
Jamie Madille18eb972016-03-04 15:46:59 -0500267 // Store static attribute.
Jamie Madill33510102017-09-20 10:39:18 -0400268 ANGLE_TRY(StoreStaticAttrib(context, translated));
Jamie Madille18eb972016-03-04 15:46:59 -0500269 break;
Jamie Madill9c385802015-06-22 13:57:18 -0400270 }
Jamie Madille18eb972016-03-04 15:46:59 -0500271 case VertexStorageType::DYNAMIC:
272 // Dynamic attributes must be handled together.
Jamie Madill52b09c22016-04-11 14:12:31 -0400273 mDynamicAttribsMaskCache.set(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500274 break;
275 case VertexStorageType::DIRECT:
276 // Update translated data for direct attributes.
Jamie Madill52b09c22016-04-11 14:12:31 -0400277 StoreDirectAttrib(translated);
Jamie Madille18eb972016-03-04 15:46:59 -0500278 break;
279 case VertexStorageType::CURRENT_VALUE:
Jamie Madill27c08912015-06-22 13:57:20 -0400280 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400281 ANGLE_TRY(storeCurrentValue(currentValueData, translated, attribIndex));
Jamie Madille18eb972016-03-04 15:46:59 -0500282 break;
Jamie Madill27c08912015-06-22 13:57:20 -0400283 }
Jamie Madille18eb972016-03-04 15:46:59 -0500284 default:
285 UNREACHABLE();
286 break;
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000287 }
288 }
289
Jamie Madill52b09c22016-04-11 14:12:31 -0400290 if (mDynamicAttribsMaskCache.none())
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000291 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400292 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500293 }
294
Jamie Madill33510102017-09-20 10:39:18 -0400295 ANGLE_TRY(storeDynamicAttribs(context, translatedAttribs, mDynamicAttribsMaskCache, start,
296 count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400297
Jamie Madill33510102017-09-20 10:39:18 -0400298 PromoteDynamicAttribs(context, *translatedAttribs, mDynamicAttribsMaskCache, count);
Jamie Madill52b09c22016-04-11 14:12:31 -0400299
300 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500301}
302
303// static
Jamie Madill52b09c22016-04-11 14:12:31 -0400304void VertexDataManager::StoreDirectAttrib(TranslatedAttribute *directAttrib)
Jamie Madille18eb972016-03-04 15:46:59 -0500305{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800306 ASSERT(directAttrib->attribute && directAttrib->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800307 const auto &attrib = *directAttrib->attribute;
308 const auto &binding = *directAttrib->binding;
309
Martin Radevdd5f27e2017-06-07 10:17:09 +0300310 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill7aa786c2017-09-19 13:57:15 -0400311 ASSERT(buffer);
312 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jamie Madille18eb972016-03-04 15:46:59 -0500313
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800314 ASSERT(DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500315 directAttrib->vertexBuffer.set(nullptr);
316 directAttrib->storage = bufferD3D;
317 directAttrib->serial = bufferD3D->getSerial();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800318 directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding));
319 directAttrib->baseOffset =
320 static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill52b09c22016-04-11 14:12:31 -0400321
322 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300323 directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madille18eb972016-03-04 15:46:59 -0500324}
325
326// static
Jamie Madill33510102017-09-20 10:39:18 -0400327gl::Error VertexDataManager::StoreStaticAttrib(const gl::Context *context,
328 TranslatedAttribute *translated)
Jamie Madille18eb972016-03-04 15:46:59 -0500329{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800330 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800331 const auto &attrib = *translated->attribute;
332 const auto &binding = *translated->binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500333
Martin Radevdd5f27e2017-06-07 10:17:09 +0300334 gl::Buffer *buffer = binding.getBuffer().get();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800335 ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500336 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
337
Jamie Madille18eb972016-03-04 15:46:59 -0500338 // Compute source data pointer
339 const uint8_t *sourceData = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800340 const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500341
Jamie Madill33510102017-09-20 10:39:18 -0400342 ANGLE_TRY(bufferD3D->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800343 sourceData += offset;
Jamie Madille18eb972016-03-04 15:46:59 -0500344
345 unsigned int streamOffset = 0;
346
Jamie Madille18eb972016-03-04 15:46:59 -0500347 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800348 ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, binding, 1, 0),
Jamie Madill52b09c22016-04-11 14:12:31 -0400349 translated->stride);
Jamie Madille18eb972016-03-04 15:46:59 -0500350
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800351 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
Jamie Madille18eb972016-03-04 15:46:59 -0500352 ASSERT(staticBuffer);
353
354 if (staticBuffer->empty())
355 {
356 // Convert the entire buffer
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800357 int totalCount =
358 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
359 int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500360
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800361 ANGLE_TRY(staticBuffer->storeStaticAttribute(attrib, binding, -startIndex, totalCount, 0,
362 sourceData));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000363 }
364
Jamie Madille18eb972016-03-04 15:46:59 -0500365 unsigned int firstElementOffset =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800366 (static_cast<unsigned int>(offset) /
367 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
Jamie Madille18eb972016-03-04 15:46:59 -0500368 translated->stride;
Jamie Madill27c08912015-06-22 13:57:20 -0400369
Jamie Madille18eb972016-03-04 15:46:59 -0500370 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
371
Jamie Madille2e406c2016-06-02 13:04:10 -0400372 CheckedNumeric<unsigned int> checkedOffset(streamOffset);
373 checkedOffset += firstElementOffset;
374
375 if (!checkedOffset.IsValid())
Jamie Madill52b09c22016-04-11 14:12:31 -0400376 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500377 return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib";
Jamie Madill52b09c22016-04-11 14:12:31 -0400378 }
379
Jamie Madille18eb972016-03-04 15:46:59 -0500380 translated->vertexBuffer.set(vertexBuffer);
381 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400382 translated->baseOffset = streamOffset + firstElementOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500383
Jamie Madill52b09c22016-04-11 14:12:31 -0400384 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300385 translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madill52b09c22016-04-11 14:12:31 -0400386
387 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500388}
389
390gl::Error VertexDataManager::storeDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400391 const gl::Context *context,
Jamie Madille18eb972016-03-04 15:46:59 -0500392 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill52b09c22016-04-11 14:12:31 -0400393 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madille18eb972016-03-04 15:46:59 -0500394 GLint start,
395 GLsizei count,
396 GLsizei instances)
397{
Jamie Madill52b09c22016-04-11 14:12:31 -0400398 // Instantiating this class will ensure the streaming buffer is never left mapped.
Jamie Madille2e406c2016-06-02 13:04:10 -0400399 class StreamingBufferUnmapper final : NonCopyable
Jamie Madill52b09c22016-04-11 14:12:31 -0400400 {
401 public:
402 StreamingBufferUnmapper(StreamingVertexBufferInterface *streamingBuffer)
403 : mStreamingBuffer(streamingBuffer)
404 {
405 ASSERT(mStreamingBuffer);
406 }
407 ~StreamingBufferUnmapper() { mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); }
408
409 private:
410 StreamingVertexBufferInterface *mStreamingBuffer;
411 };
412
413 // Will trigger unmapping on return.
Jamie Madill401345e2017-08-21 10:52:40 -0400414 StreamingBufferUnmapper localUnmapper(mStreamingBuffer.get());
Jamie Madill52b09c22016-04-11 14:12:31 -0400415
Jamie Madille18eb972016-03-04 15:46:59 -0500416 // Reserve the required space for the dynamic buffers.
Jamie Madill6de51852017-04-12 09:53:01 -0400417 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500418 {
419 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800420 ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, start, count, instances));
Jamie Madille18eb972016-03-04 15:46:59 -0500421 }
422
423 // Store dynamic attributes
Jamie Madill6de51852017-04-12 09:53:01 -0400424 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500425 {
426 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill33510102017-09-20 10:39:18 -0400427 ANGLE_TRY(storeDynamicAttrib(context, dynamicAttrib, start, count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400428 }
Shannon Woods1a965482014-09-22 18:00:32 -0400429
Jamie Madill52b09c22016-04-11 14:12:31 -0400430 return gl::NoError();
431}
432
433void VertexDataManager::PromoteDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400434 const gl::Context *context,
Jamie Madill52b09c22016-04-11 14:12:31 -0400435 const std::vector<TranslatedAttribute> &translatedAttribs,
436 const gl::AttributesMask &dynamicAttribsMask,
437 GLsizei count)
438{
Jamie Madill6de51852017-04-12 09:53:01 -0400439 for (auto attribIndex : dynamicAttribsMask)
Jamie Madill52b09c22016-04-11 14:12:31 -0400440 {
441 const auto &dynamicAttrib = translatedAttribs[attribIndex];
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800442 ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800443 const auto &binding = *dynamicAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800444
Martin Radevdd5f27e2017-06-07 10:17:09 +0300445 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill27c08912015-06-22 13:57:20 -0400446 if (buffer)
447 {
448 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800449 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
Jamie Madill33510102017-09-20 10:39:18 -0400450 bufferD3D->promoteStaticUsage(context, count * static_cast<int>(typeSize));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000451 }
452 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000453}
454
Jamie Madill3d72cc72015-06-22 13:57:19 -0400455gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800456 GLint start,
Geoff Langf7100b92014-09-08 16:17:08 -0400457 GLsizei count,
458 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400459{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800460 ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800461 const auto &attrib = *translatedAttrib.attribute;
462 const auto &binding = *translatedAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800463
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800464 ASSERT(!DirectStoragePossible(attrib, binding));
Jamie Madill6d113802014-08-25 15:47:52 -0400465
Martin Radevdd5f27e2017-06-07 10:17:09 +0300466 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500467 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800468 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
Jamie Madille36b92d2016-03-04 15:46:58 -0500469
Martin Radev553590a2017-07-31 16:40:39 +0300470 size_t totalCount = gl::ComputeVertexBindingElementCount(
471 binding.getDivisor(), static_cast<size_t>(count), static_cast<size_t>(instances));
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800472 // TODO(jiajia.qin@intel.com): force the index buffer to clamp any out of range indices instead
473 // of invalid operation here.
474 if (bufferD3D)
475 {
476 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
477 // a non-instanced draw call
478 GLint firstVertexIndex = binding.getDivisor() > 0 ? 0 : start;
479 int64_t maxVertexCount =
480 static_cast<int64_t>(firstVertexIndex) + static_cast<int64_t>(totalCount);
481 int elementsInBuffer =
482 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
Jamie Madille36b92d2016-03-04 15:46:58 -0500483
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800484 if (maxVertexCount > elementsInBuffer)
485 {
486 return gl::InvalidOperation() << "Vertex buffer is not big enough for the draw call.";
487 }
488 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800489 return mStreamingBuffer->reserveVertexSpace(attrib, binding, static_cast<GLsizei>(totalCount),
Jamie Madille36b92d2016-03-04 15:46:58 -0500490 instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400491}
492
Jamie Madill33510102017-09-20 10:39:18 -0400493gl::Error VertexDataManager::storeDynamicAttrib(const gl::Context *context,
494 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500495 GLint start,
496 GLsizei count,
497 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400498{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800499 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800500 const auto &attrib = *translated->attribute;
501 const auto &binding = *translated->binding;
Jamie Madill9c385802015-06-22 13:57:18 -0400502
Martin Radevdd5f27e2017-06-07 10:17:09 +0300503 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -0400504 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400505 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400506
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500507 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400508
Jamie Madilld4b55a02015-01-09 14:21:49 -0500509 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300510 GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500511
Jamie Madill7d112bb2015-06-22 13:57:19 -0400512 // Compute source data pointer
513 const uint8_t *sourceData = nullptr;
514
515 if (buffer)
516 {
Jamie Madill33510102017-09-20 10:39:18 -0400517 ANGLE_TRY(storage->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800518 sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400519 }
520 else
521 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800522 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
523 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill7d112bb2015-06-22 13:57:19 -0400524 sourceData = static_cast<const uint8_t*>(attrib.pointer);
525 }
526
527 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500528
Jamie Madilld8fa9212016-03-02 11:51:43 -0500529 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800530 ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, binding, 1, 0), translated->stride);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500531
Martin Radev553590a2017-07-31 16:40:39 +0300532 size_t totalCount = gl::ComputeVertexBindingElementCount(
533 binding.getDivisor(), static_cast<size_t>(count), static_cast<size_t>(instances));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400534
Jamie Madill52b09c22016-04-11 14:12:31 -0400535 ANGLE_TRY(mStreamingBuffer->storeDynamicAttribute(
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800536 attrib, binding, translated->currentValueType, firstVertexIndex,
537 static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400538
Jamie Madille18eb972016-03-04 15:46:59 -0500539 VertexBuffer *vertexBuffer = mStreamingBuffer->getVertexBuffer();
540
541 translated->vertexBuffer.set(vertexBuffer);
542 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400543 translated->baseOffset = streamOffset;
544 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400545
Jamie Madill52b09c22016-04-11 14:12:31 -0400546 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400547}
548
Jamie Madill9c385802015-06-22 13:57:18 -0400549gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400550 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500551 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400552{
Jamie Madille18eb972016-03-04 15:46:59 -0500553 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
Jamie Madill401345e2017-08-21 10:52:40 -0400554 auto &buffer = cachedState->buffer;
Jamie Madille18eb972016-03-04 15:46:59 -0500555
556 if (!buffer)
557 {
Jamie Madill401345e2017-08-21 10:52:40 -0400558 buffer.reset(new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE));
Jamie Madille18eb972016-03-04 15:46:59 -0500559 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500560
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400561 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400562 {
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800563 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800564 const auto &attrib = *translated->attribute;
565 const auto &binding = *translated->binding;
Jamie Madill27c08912015-06-22 13:57:20 -0400566
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800567 ANGLE_TRY(buffer->reserveVertexSpace(attrib, binding, 1, 0));
Jamie Madillf41522b2014-08-18 16:39:49 -0400568
Jamie Madill7d112bb2015-06-22 13:57:19 -0400569 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400570 unsigned int streamOffset;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800571 ANGLE_TRY(buffer->storeDynamicAttribute(attrib, binding, currentValue.Type, 0, 1, 0,
572 &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400573
Jamie Madille36b92d2016-03-04 15:46:58 -0500574 buffer->getVertexBuffer()->hintUnmapResource();
575
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400576 cachedState->data = currentValue;
577 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400578 }
579
Jamie Madille36b92d2016-03-04 15:46:58 -0500580 translated->vertexBuffer.set(buffer->getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400581
Jamie Madille36b92d2016-03-04 15:46:58 -0500582 translated->storage = nullptr;
583 translated->serial = buffer->getSerial();
584 translated->divisor = 0;
585 translated->stride = 0;
Jamie Madill52b09c22016-04-11 14:12:31 -0400586 translated->baseOffset = static_cast<unsigned int>(cachedState->offset);
587 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400588
Jamie Madill52b09c22016-04-11 14:12:31 -0400589 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400590}
591
Jamie Madille36b92d2016-03-04 15:46:58 -0500592// VertexBufferBinding implementation
593VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
594{
595}
596
597VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
598 : mBoundVertexBuffer(other.mBoundVertexBuffer)
599{
600 if (mBoundVertexBuffer)
601 {
602 mBoundVertexBuffer->addRef();
603 }
604}
605
606VertexBufferBinding::~VertexBufferBinding()
607{
608 if (mBoundVertexBuffer)
609 {
610 mBoundVertexBuffer->release();
611 }
612}
613
614VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
615{
616 mBoundVertexBuffer = other.mBoundVertexBuffer;
617 if (mBoundVertexBuffer)
618 {
619 mBoundVertexBuffer->addRef();
620 }
621 return *this;
622}
623
624void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
625{
626 if (mBoundVertexBuffer == vertexBuffer)
627 return;
628
629 if (mBoundVertexBuffer)
630 {
631 mBoundVertexBuffer->release();
632 }
633 if (vertexBuffer)
634 {
635 vertexBuffer->addRef();
636 }
637
638 mBoundVertexBuffer = vertexBuffer;
639}
640
641VertexBuffer *VertexBufferBinding::get() const
642{
643 return mBoundVertexBuffer;
644}
645
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500646} // namespace rx