blob: 34a4730108e65cf57650d7fd89793e2f22057903 [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 Madilldbfc6c62016-02-29 01:08:57 -050014#include "libANGLE/formatutils.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 Lang2b5420c2014-11-19 14:20:15 -050017#include "libANGLE/VertexAttribute.h"
Geoff Lang5ead9272015-03-25 12:27:43 -040018#include "libANGLE/VertexArray.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040019#include "libANGLE/renderer/d3d/BufferD3D.h"
Jamie Madilldbfc6c62016-02-29 01:08:57 -050020#include "libANGLE/renderer/d3d/RendererD3D.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040021#include "libANGLE/renderer/d3d/VertexBuffer.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000022
Jamie Madille2e406c2016-06-02 13:04:10 -040023using namespace angle;
24
daniel@transgaming.com31240482012-11-28 21:06:41 +000025namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000026{
Jamie Madilldbfc6c62016-02-29 01:08:57 -050027namespace
28{
29enum
30{
31 INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024
32};
33// This has to be at least 4k or else it fails on ATI cards.
34enum
35{
36 CONSTANT_VERTEX_BUFFER_SIZE = 4096
37};
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000038
Jiawei-Shao2597fb62016-12-09 16:38:02 +080039// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
40int ElementsInBuffer(const gl::VertexAttribute &attrib,
41 const gl::VertexBinding &binding,
42 unsigned int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000043{
Geoff Langa36ead42013-08-02 11:54:08 -040044 // Size cannot be larger than a GLsizei
45 if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
46 {
47 size = static_cast<unsigned int>(std::numeric_limits<int>::max());
48 }
49
Jiawei-Shao2597fb62016-12-09 16:38:02 +080050 GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib, binding));
51 GLsizei offset = static_cast<GLsizei>(ComputeVertexAttributeOffset(attrib, binding));
52 return (size - offset % stride +
Cooper Partin4d61f7e2015-08-12 10:56:50 -070053 (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) /
54 stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000055}
56
Jiawei-Shao2597fb62016-12-09 16:38:02 +080057// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
58bool DirectStoragePossible(const gl::VertexAttribute &attrib, const gl::VertexBinding &binding)
Jamie Madilldbfc6c62016-02-29 01:08:57 -050059{
60 // Current value attribs may not use direct storage.
61 if (!attrib.enabled)
62 {
63 return false;
64 }
65
Martin Radevdd5f27e2017-06-07 10:17:09 +030066 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madilldbfc6c62016-02-29 01:08:57 -050067 if (!buffer)
68 {
69 return false;
70 }
71
72 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
73 ASSERT(bufferD3D);
74 if (!bufferD3D->supportsDirectBinding())
75 {
76 return false;
77 }
78
Jamie Madilldbfc6c62016-02-29 01:08:57 -050079 // Alignment restrictions: In D3D, vertex data must be aligned to the format stride, or to a
80 // 4-byte boundary, whichever is smaller. (Undocumented, and experimentally confirmed)
Jamie Madilld8fa9212016-03-02 11:51:43 -050081 size_t alignment = 4;
Jamie Madilldbfc6c62016-02-29 01:08:57 -050082
Jamie Madille16a4512017-03-07 20:46:40 -050083 // TODO(jmadill): add VertexFormatCaps
84 BufferFactoryD3D *factory = bufferD3D->getFactory();
85
86 gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib);
87
88 // CPU-converted vertex data must be converted (naturally).
89 if ((factory->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_CPU) != 0)
90 {
91 return false;
92 }
93
Jamie Madilldbfc6c62016-02-29 01:08:57 -050094 if (attrib.type != GL_FLOAT)
95 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +080096 auto errorOrElementSize = factory->getVertexSpaceRequired(attrib, binding, 1, 0);
Jamie Madilld8fa9212016-03-02 11:51:43 -050097 if (errorOrElementSize.isError())
98 {
Yuly Novikovd73f8522017-01-13 17:48:57 -050099 ERR() << "Unlogged error in DirectStoragePossible.";
Jamie Madilld8fa9212016-03-02 11:51:43 -0500100 return false;
101 }
102
103 alignment = std::min<size_t>(errorOrElementSize.getResult(), 4);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500104 }
105
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800106 GLintptr offset = ComputeVertexAttributeOffset(attrib, binding);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500107 // Final alignment check - unaligned data must be converted.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800108 return (static_cast<size_t>(ComputeVertexAttributeStride(attrib, binding)) % alignment == 0) &&
109 (static_cast<size_t>(offset) % alignment == 0);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500110}
111} // anonymous namespace
112
Jamie Madille36b92d2016-03-04 15:46:58 -0500113TranslatedAttribute::TranslatedAttribute()
114 : active(false),
115 attribute(nullptr),
Jamie Madill401345e2017-08-21 10:52:40 -0400116 binding(nullptr),
Jamie Madille36b92d2016-03-04 15:46:58 -0500117 currentValueType(GL_NONE),
Jamie Madill52b09c22016-04-11 14:12:31 -0400118 baseOffset(0),
119 usesFirstVertexOffset(false),
Jamie Madille36b92d2016-03-04 15:46:58 -0500120 stride(0),
121 vertexBuffer(),
122 storage(nullptr),
123 serial(0),
124 divisor(0)
125{
126}
127
Jamie Madill52b09c22016-04-11 14:12:31 -0400128gl::ErrorOrResult<unsigned int> TranslatedAttribute::computeOffset(GLint startVertex) const
129{
Jamie Madille2e406c2016-06-02 13:04:10 -0400130 if (!usesFirstVertexOffset)
Jamie Madill52b09c22016-04-11 14:12:31 -0400131 {
Jamie Madille2e406c2016-06-02 13:04:10 -0400132 return baseOffset;
Jamie Madill52b09c22016-04-11 14:12:31 -0400133 }
Jamie Madille2e406c2016-06-02 13:04:10 -0400134
135 CheckedNumeric<unsigned int> offset;
136
137 offset = baseOffset + stride * static_cast<unsigned int>(startVertex);
138 ANGLE_TRY_CHECKED_MATH(offset);
139 return offset.ValueOrDie();
Jamie Madill52b09c22016-04-11 14:12:31 -0400140}
141
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800142// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
143VertexStorageType ClassifyAttributeStorage(const gl::VertexAttribute &attrib,
144 const gl::VertexBinding &binding)
Jamie Madille18eb972016-03-04 15:46:59 -0500145{
146 // If attribute is disabled, we use the current value.
147 if (!attrib.enabled)
148 {
149 return VertexStorageType::CURRENT_VALUE;
150 }
151
152 // If specified with immediate data, we must use dynamic storage.
Martin Radevdd5f27e2017-06-07 10:17:09 +0300153 auto *buffer = binding.getBuffer().get();
Jamie Madille18eb972016-03-04 15:46:59 -0500154 if (!buffer)
155 {
156 return VertexStorageType::DYNAMIC;
157 }
158
159 // Check if the buffer supports direct storage.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800160 if (DirectStoragePossible(attrib, binding))
Jamie Madille18eb972016-03-04 15:46:59 -0500161 {
162 return VertexStorageType::DIRECT;
163 }
164
165 // Otherwise the storage is static or dynamic.
166 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
167 ASSERT(bufferD3D);
168 switch (bufferD3D->getUsage())
169 {
170 case D3DBufferUsage::DYNAMIC:
171 return VertexStorageType::DYNAMIC;
172 case D3DBufferUsage::STATIC:
173 return VertexStorageType::STATIC;
174 default:
175 UNREACHABLE();
176 return VertexStorageType::UNKNOWN;
177 }
178}
179
Jamie Madill401345e2017-08-21 10:52:40 -0400180VertexDataManager::CurrentValueState::CurrentValueState() : buffer(), offset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000181{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400182 data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
183 data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
184 data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
185 data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
186 data.Type = GL_FLOAT;
187}
daniel@transgaming.com83921382011-01-08 05:46:00 +0000188
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400189VertexDataManager::CurrentValueState::~CurrentValueState()
190{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400191}
192
193VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
Jamie Madill401345e2017-08-21 10:52:40 -0400194 : mFactory(factory), mStreamingBuffer(), mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS)
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400195{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000196}
197
198VertexDataManager::~VertexDataManager()
199{
Jamie Madill401345e2017-08-21 10:52:40 -0400200}
201
202gl::Error VertexDataManager::initialize()
203{
204 mStreamingBuffer.reset(
205 new StreamingVertexBufferInterface(mFactory, INITIAL_STREAM_BUFFER_SIZE));
206 if (!mStreamingBuffer)
207 {
208 return gl::OutOfMemory() << "Failed to allocate the streaming vertex buffer.";
209 }
210
211 return gl::NoError();
212}
213
214void VertexDataManager::deinitialize()
215{
216 mStreamingBuffer.reset();
217 mCurrentValueCache.clear();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000218}
219
Jamie Madill476682e2015-06-30 10:04:29 -0400220gl::Error VertexDataManager::prepareVertexData(const gl::State &state,
221 GLint start,
222 GLsizei count,
223 std::vector<TranslatedAttribute> *translatedAttribs,
224 GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000225{
Jamie Madille18eb972016-03-04 15:46:59 -0500226 ASSERT(mStreamingBuffer);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000227
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.
Qin Jiajiacde4f022017-02-21 14:06:02 +0800264 ANGLE_TRY(StoreStaticAttrib(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 Madill52b09c22016-04-11 14:12:31 -0400291 ANGLE_TRY(
292 storeDynamicAttribs(translatedAttribs, mDynamicAttribsMaskCache, start, count, instances));
293
294 PromoteDynamicAttribs(*translatedAttribs, mDynamicAttribsMaskCache, count);
295
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
Qin Jiajiacde4f022017-02-21 14:06:02 +0800323gl::Error VertexDataManager::StoreStaticAttrib(TranslatedAttribute *translated)
Jamie Madille18eb972016-03-04 15:46:59 -0500324{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800325 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800326 const auto &attrib = *translated->attribute;
327 const auto &binding = *translated->binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500328
Martin Radevdd5f27e2017-06-07 10:17:09 +0300329 gl::Buffer *buffer = binding.getBuffer().get();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800330 ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500331 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
332
Jamie Madille18eb972016-03-04 15:46:59 -0500333 // Compute source data pointer
334 const uint8_t *sourceData = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800335 const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500336
Jamie Madill52b09c22016-04-11 14:12:31 -0400337 ANGLE_TRY(bufferD3D->getData(&sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800338 sourceData += offset;
Jamie Madille18eb972016-03-04 15:46:59 -0500339
340 unsigned int streamOffset = 0;
341
Jamie Madille18eb972016-03-04 15:46:59 -0500342 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800343 ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, binding, 1, 0),
Jamie Madill52b09c22016-04-11 14:12:31 -0400344 translated->stride);
Jamie Madille18eb972016-03-04 15:46:59 -0500345
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800346 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
Jamie Madille18eb972016-03-04 15:46:59 -0500347 ASSERT(staticBuffer);
348
349 if (staticBuffer->empty())
350 {
351 // Convert the entire buffer
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800352 int totalCount =
353 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
354 int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500355
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800356 ANGLE_TRY(staticBuffer->storeStaticAttribute(attrib, binding, -startIndex, totalCount, 0,
357 sourceData));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000358 }
359
Jamie Madille18eb972016-03-04 15:46:59 -0500360 unsigned int firstElementOffset =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800361 (static_cast<unsigned int>(offset) /
362 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
Jamie Madille18eb972016-03-04 15:46:59 -0500363 translated->stride;
Jamie Madill27c08912015-06-22 13:57:20 -0400364
Jamie Madille18eb972016-03-04 15:46:59 -0500365 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
366
Jamie Madille2e406c2016-06-02 13:04:10 -0400367 CheckedNumeric<unsigned int> checkedOffset(streamOffset);
368 checkedOffset += firstElementOffset;
369
370 if (!checkedOffset.IsValid())
Jamie Madill52b09c22016-04-11 14:12:31 -0400371 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500372 return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib";
Jamie Madill52b09c22016-04-11 14:12:31 -0400373 }
374
Jamie Madille18eb972016-03-04 15:46:59 -0500375 translated->vertexBuffer.set(vertexBuffer);
376 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400377 translated->baseOffset = streamOffset + firstElementOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500378
Jamie Madill52b09c22016-04-11 14:12:31 -0400379 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300380 translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madill52b09c22016-04-11 14:12:31 -0400381
382 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500383}
384
385gl::Error VertexDataManager::storeDynamicAttribs(
386 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill52b09c22016-04-11 14:12:31 -0400387 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madille18eb972016-03-04 15:46:59 -0500388 GLint start,
389 GLsizei count,
390 GLsizei instances)
391{
Jamie Madill52b09c22016-04-11 14:12:31 -0400392 // Instantiating this class will ensure the streaming buffer is never left mapped.
Jamie Madille2e406c2016-06-02 13:04:10 -0400393 class StreamingBufferUnmapper final : NonCopyable
Jamie Madill52b09c22016-04-11 14:12:31 -0400394 {
395 public:
396 StreamingBufferUnmapper(StreamingVertexBufferInterface *streamingBuffer)
397 : mStreamingBuffer(streamingBuffer)
398 {
399 ASSERT(mStreamingBuffer);
400 }
401 ~StreamingBufferUnmapper() { mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); }
402
403 private:
404 StreamingVertexBufferInterface *mStreamingBuffer;
405 };
406
407 // Will trigger unmapping on return.
Jamie Madill401345e2017-08-21 10:52:40 -0400408 StreamingBufferUnmapper localUnmapper(mStreamingBuffer.get());
Jamie Madill52b09c22016-04-11 14:12:31 -0400409
Jamie Madille18eb972016-03-04 15:46:59 -0500410 // Reserve the required space for the dynamic buffers.
Jamie Madill6de51852017-04-12 09:53:01 -0400411 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500412 {
413 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800414 ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, start, count, instances));
Jamie Madille18eb972016-03-04 15:46:59 -0500415 }
416
417 // Store dynamic attributes
Jamie Madill6de51852017-04-12 09:53:01 -0400418 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500419 {
420 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill52b09c22016-04-11 14:12:31 -0400421 ANGLE_TRY(storeDynamicAttrib(dynamicAttrib, start, count, instances));
422 }
Shannon Woods1a965482014-09-22 18:00:32 -0400423
Jamie Madill52b09c22016-04-11 14:12:31 -0400424 return gl::NoError();
425}
426
427void VertexDataManager::PromoteDynamicAttribs(
428 const std::vector<TranslatedAttribute> &translatedAttribs,
429 const gl::AttributesMask &dynamicAttribsMask,
430 GLsizei count)
431{
Jamie Madill6de51852017-04-12 09:53:01 -0400432 for (auto attribIndex : dynamicAttribsMask)
Jamie Madill52b09c22016-04-11 14:12:31 -0400433 {
434 const auto &dynamicAttrib = translatedAttribs[attribIndex];
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800435 ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800436 const auto &binding = *dynamicAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800437
Martin Radevdd5f27e2017-06-07 10:17:09 +0300438 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill27c08912015-06-22 13:57:20 -0400439 if (buffer)
440 {
441 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800442 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700443 bufferD3D->promoteStaticUsage(count * static_cast<int>(typeSize));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000444 }
445 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000446}
447
Jamie Madill3d72cc72015-06-22 13:57:19 -0400448gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800449 GLint start,
Geoff Langf7100b92014-09-08 16:17:08 -0400450 GLsizei count,
451 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400452{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800453 ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800454 const auto &attrib = *translatedAttrib.attribute;
455 const auto &binding = *translatedAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800456
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800457 ASSERT(!DirectStoragePossible(attrib, binding));
Jamie Madill6d113802014-08-25 15:47:52 -0400458
Martin Radevdd5f27e2017-06-07 10:17:09 +0300459 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500460 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800461 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
Jamie Madille36b92d2016-03-04 15:46:58 -0500462
Martin Radev553590a2017-07-31 16:40:39 +0300463 size_t totalCount = gl::ComputeVertexBindingElementCount(
464 binding.getDivisor(), static_cast<size_t>(count), static_cast<size_t>(instances));
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800465 // TODO(jiajia.qin@intel.com): force the index buffer to clamp any out of range indices instead
466 // of invalid operation here.
467 if (bufferD3D)
468 {
469 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
470 // a non-instanced draw call
471 GLint firstVertexIndex = binding.getDivisor() > 0 ? 0 : start;
472 int64_t maxVertexCount =
473 static_cast<int64_t>(firstVertexIndex) + static_cast<int64_t>(totalCount);
474 int elementsInBuffer =
475 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
Jamie Madille36b92d2016-03-04 15:46:58 -0500476
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800477 if (maxVertexCount > elementsInBuffer)
478 {
479 return gl::InvalidOperation() << "Vertex buffer is not big enough for the draw call.";
480 }
481 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800482 return mStreamingBuffer->reserveVertexSpace(attrib, binding, static_cast<GLsizei>(totalCount),
Jamie Madille36b92d2016-03-04 15:46:58 -0500483 instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400484}
485
Jamie Madille18eb972016-03-04 15:46:59 -0500486gl::Error VertexDataManager::storeDynamicAttrib(TranslatedAttribute *translated,
487 GLint start,
488 GLsizei count,
489 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400490{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800491 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800492 const auto &attrib = *translated->attribute;
493 const auto &binding = *translated->binding;
Jamie Madill9c385802015-06-22 13:57:18 -0400494
Martin Radevdd5f27e2017-06-07 10:17:09 +0300495 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -0400496 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400497 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400498
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500499 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400500
Jamie Madilld4b55a02015-01-09 14:21:49 -0500501 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300502 GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500503
Jamie Madill7d112bb2015-06-22 13:57:19 -0400504 // Compute source data pointer
505 const uint8_t *sourceData = nullptr;
506
507 if (buffer)
508 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400509 ANGLE_TRY(storage->getData(&sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800510 sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400511 }
512 else
513 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800514 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
515 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill7d112bb2015-06-22 13:57:19 -0400516 sourceData = static_cast<const uint8_t*>(attrib.pointer);
517 }
518
519 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500520
Jamie Madilld8fa9212016-03-02 11:51:43 -0500521 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800522 ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, binding, 1, 0), translated->stride);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500523
Martin Radev553590a2017-07-31 16:40:39 +0300524 size_t totalCount = gl::ComputeVertexBindingElementCount(
525 binding.getDivisor(), static_cast<size_t>(count), static_cast<size_t>(instances));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400526
Jamie Madill52b09c22016-04-11 14:12:31 -0400527 ANGLE_TRY(mStreamingBuffer->storeDynamicAttribute(
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800528 attrib, binding, translated->currentValueType, firstVertexIndex,
529 static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400530
Jamie Madille18eb972016-03-04 15:46:59 -0500531 VertexBuffer *vertexBuffer = mStreamingBuffer->getVertexBuffer();
532
533 translated->vertexBuffer.set(vertexBuffer);
534 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400535 translated->baseOffset = streamOffset;
536 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400537
Jamie Madill52b09c22016-04-11 14:12:31 -0400538 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400539}
540
Jamie Madill9c385802015-06-22 13:57:18 -0400541gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400542 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500543 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400544{
Jamie Madille18eb972016-03-04 15:46:59 -0500545 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
Jamie Madill401345e2017-08-21 10:52:40 -0400546 auto &buffer = cachedState->buffer;
Jamie Madille18eb972016-03-04 15:46:59 -0500547
548 if (!buffer)
549 {
Jamie Madill401345e2017-08-21 10:52:40 -0400550 buffer.reset(new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE));
Jamie Madille18eb972016-03-04 15:46:59 -0500551 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500552
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400553 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400554 {
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800555 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800556 const auto &attrib = *translated->attribute;
557 const auto &binding = *translated->binding;
Jamie Madill27c08912015-06-22 13:57:20 -0400558
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800559 ANGLE_TRY(buffer->reserveVertexSpace(attrib, binding, 1, 0));
Jamie Madillf41522b2014-08-18 16:39:49 -0400560
Jamie Madill7d112bb2015-06-22 13:57:19 -0400561 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400562 unsigned int streamOffset;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800563 ANGLE_TRY(buffer->storeDynamicAttribute(attrib, binding, currentValue.Type, 0, 1, 0,
564 &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400565
Jamie Madille36b92d2016-03-04 15:46:58 -0500566 buffer->getVertexBuffer()->hintUnmapResource();
567
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400568 cachedState->data = currentValue;
569 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400570 }
571
Jamie Madille36b92d2016-03-04 15:46:58 -0500572 translated->vertexBuffer.set(buffer->getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400573
Jamie Madille36b92d2016-03-04 15:46:58 -0500574 translated->storage = nullptr;
575 translated->serial = buffer->getSerial();
576 translated->divisor = 0;
577 translated->stride = 0;
Jamie Madill52b09c22016-04-11 14:12:31 -0400578 translated->baseOffset = static_cast<unsigned int>(cachedState->offset);
579 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400580
Jamie Madill52b09c22016-04-11 14:12:31 -0400581 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400582}
583
Jamie Madille36b92d2016-03-04 15:46:58 -0500584// VertexBufferBinding implementation
585VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
586{
587}
588
589VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
590 : mBoundVertexBuffer(other.mBoundVertexBuffer)
591{
592 if (mBoundVertexBuffer)
593 {
594 mBoundVertexBuffer->addRef();
595 }
596}
597
598VertexBufferBinding::~VertexBufferBinding()
599{
600 if (mBoundVertexBuffer)
601 {
602 mBoundVertexBuffer->release();
603 }
604}
605
606VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
607{
608 mBoundVertexBuffer = other.mBoundVertexBuffer;
609 if (mBoundVertexBuffer)
610 {
611 mBoundVertexBuffer->addRef();
612 }
613 return *this;
614}
615
616void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
617{
618 if (mBoundVertexBuffer == vertexBuffer)
619 return;
620
621 if (mBoundVertexBuffer)
622 {
623 mBoundVertexBuffer->release();
624 }
625 if (vertexBuffer)
626 {
627 vertexBuffer->addRef();
628 }
629
630 mBoundVertexBuffer = vertexBuffer;
631}
632
633VertexBuffer *VertexBufferBinding::get() const
634{
635 return mBoundVertexBuffer;
636}
637
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500638} // namespace rx