blob: f5c6cf08231c33425467487a8eb7043d0a1bb5b0 [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 Madille18eb972016-03-04 15:46:59 -0500307 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
308
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800309 ASSERT(DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500310 directAttrib->vertexBuffer.set(nullptr);
311 directAttrib->storage = bufferD3D;
312 directAttrib->serial = bufferD3D->getSerial();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800313 directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding));
314 directAttrib->baseOffset =
315 static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill52b09c22016-04-11 14:12:31 -0400316
317 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300318 directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madille18eb972016-03-04 15:46:59 -0500319}
320
321// static
Qin Jiajiacde4f022017-02-21 14:06:02 +0800322gl::Error VertexDataManager::StoreStaticAttrib(TranslatedAttribute *translated)
Jamie Madille18eb972016-03-04 15:46:59 -0500323{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800324 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800325 const auto &attrib = *translated->attribute;
326 const auto &binding = *translated->binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500327
Martin Radevdd5f27e2017-06-07 10:17:09 +0300328 gl::Buffer *buffer = binding.getBuffer().get();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800329 ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500330 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
331
Jamie Madille18eb972016-03-04 15:46:59 -0500332 // Compute source data pointer
333 const uint8_t *sourceData = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800334 const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500335
Jamie Madill52b09c22016-04-11 14:12:31 -0400336 ANGLE_TRY(bufferD3D->getData(&sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800337 sourceData += offset;
Jamie Madille18eb972016-03-04 15:46:59 -0500338
339 unsigned int streamOffset = 0;
340
Jamie Madille18eb972016-03-04 15:46:59 -0500341 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800342 ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, binding, 1, 0),
Jamie Madill52b09c22016-04-11 14:12:31 -0400343 translated->stride);
Jamie Madille18eb972016-03-04 15:46:59 -0500344
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800345 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
Jamie Madille18eb972016-03-04 15:46:59 -0500346 ASSERT(staticBuffer);
347
348 if (staticBuffer->empty())
349 {
350 // Convert the entire buffer
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800351 int totalCount =
352 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
353 int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500354
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800355 ANGLE_TRY(staticBuffer->storeStaticAttribute(attrib, binding, -startIndex, totalCount, 0,
356 sourceData));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000357 }
358
Jamie Madille18eb972016-03-04 15:46:59 -0500359 unsigned int firstElementOffset =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800360 (static_cast<unsigned int>(offset) /
361 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
Jamie Madille18eb972016-03-04 15:46:59 -0500362 translated->stride;
Jamie Madill27c08912015-06-22 13:57:20 -0400363
Jamie Madille18eb972016-03-04 15:46:59 -0500364 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
365
Jamie Madille2e406c2016-06-02 13:04:10 -0400366 CheckedNumeric<unsigned int> checkedOffset(streamOffset);
367 checkedOffset += firstElementOffset;
368
369 if (!checkedOffset.IsValid())
Jamie Madill52b09c22016-04-11 14:12:31 -0400370 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500371 return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib";
Jamie Madill52b09c22016-04-11 14:12:31 -0400372 }
373
Jamie Madille18eb972016-03-04 15:46:59 -0500374 translated->vertexBuffer.set(vertexBuffer);
375 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400376 translated->baseOffset = streamOffset + firstElementOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500377
Jamie Madill52b09c22016-04-11 14:12:31 -0400378 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300379 translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madill52b09c22016-04-11 14:12:31 -0400380
381 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500382}
383
384gl::Error VertexDataManager::storeDynamicAttribs(
385 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill52b09c22016-04-11 14:12:31 -0400386 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madille18eb972016-03-04 15:46:59 -0500387 GLint start,
388 GLsizei count,
389 GLsizei instances)
390{
Jamie Madill52b09c22016-04-11 14:12:31 -0400391 // Instantiating this class will ensure the streaming buffer is never left mapped.
Jamie Madille2e406c2016-06-02 13:04:10 -0400392 class StreamingBufferUnmapper final : NonCopyable
Jamie Madill52b09c22016-04-11 14:12:31 -0400393 {
394 public:
395 StreamingBufferUnmapper(StreamingVertexBufferInterface *streamingBuffer)
396 : mStreamingBuffer(streamingBuffer)
397 {
398 ASSERT(mStreamingBuffer);
399 }
400 ~StreamingBufferUnmapper() { mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); }
401
402 private:
403 StreamingVertexBufferInterface *mStreamingBuffer;
404 };
405
406 // Will trigger unmapping on return.
Jamie Madill401345e2017-08-21 10:52:40 -0400407 StreamingBufferUnmapper localUnmapper(mStreamingBuffer.get());
Jamie Madill52b09c22016-04-11 14:12:31 -0400408
Jamie Madille18eb972016-03-04 15:46:59 -0500409 // Reserve the required space for the dynamic buffers.
Jamie Madill6de51852017-04-12 09:53:01 -0400410 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500411 {
412 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Corentin Wallezc1346fb2017-08-24 16:11:26 +0000413 ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, count, instances));
Jamie Madille18eb972016-03-04 15:46:59 -0500414 }
415
416 // Store dynamic attributes
Jamie Madill6de51852017-04-12 09:53:01 -0400417 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500418 {
419 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill52b09c22016-04-11 14:12:31 -0400420 ANGLE_TRY(storeDynamicAttrib(dynamicAttrib, start, count, instances));
421 }
Shannon Woods1a965482014-09-22 18:00:32 -0400422
Jamie Madill52b09c22016-04-11 14:12:31 -0400423 return gl::NoError();
424}
425
426void VertexDataManager::PromoteDynamicAttribs(
427 const std::vector<TranslatedAttribute> &translatedAttribs,
428 const gl::AttributesMask &dynamicAttribsMask,
429 GLsizei count)
430{
Jamie Madill6de51852017-04-12 09:53:01 -0400431 for (auto attribIndex : dynamicAttribsMask)
Jamie Madill52b09c22016-04-11 14:12:31 -0400432 {
433 const auto &dynamicAttrib = translatedAttribs[attribIndex];
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800434 ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800435 const auto &binding = *dynamicAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800436
Martin Radevdd5f27e2017-06-07 10:17:09 +0300437 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill27c08912015-06-22 13:57:20 -0400438 if (buffer)
439 {
440 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800441 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700442 bufferD3D->promoteStaticUsage(count * static_cast<int>(typeSize));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000443 }
444 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000445}
446
Jamie Madill3d72cc72015-06-22 13:57:19 -0400447gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
Geoff Langf7100b92014-09-08 16:17:08 -0400448 GLsizei count,
449 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400450{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800451 ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800452 const auto &attrib = *translatedAttrib.attribute;
453 const auto &binding = *translatedAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800454
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800455 ASSERT(!DirectStoragePossible(attrib, binding));
Jamie Madill6d113802014-08-25 15:47:52 -0400456
Martin Radevdd5f27e2017-06-07 10:17:09 +0300457 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500458 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800459 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
Jamie Madille36b92d2016-03-04 15:46:58 -0500460
Martin Radev553590a2017-07-31 16:40:39 +0300461 size_t totalCount = gl::ComputeVertexBindingElementCount(
462 binding.getDivisor(), static_cast<size_t>(count), static_cast<size_t>(instances));
Corentin Wallezc1346fb2017-08-24 16:11:26 +0000463 ASSERT(!bufferD3D ||
464 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize())) >=
465 static_cast<int>(totalCount));
Jamie Madille36b92d2016-03-04 15:46:58 -0500466
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800467 return mStreamingBuffer->reserveVertexSpace(attrib, binding, static_cast<GLsizei>(totalCount),
Jamie Madille36b92d2016-03-04 15:46:58 -0500468 instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400469}
470
Jamie Madille18eb972016-03-04 15:46:59 -0500471gl::Error VertexDataManager::storeDynamicAttrib(TranslatedAttribute *translated,
472 GLint start,
473 GLsizei count,
474 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400475{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800476 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800477 const auto &attrib = *translated->attribute;
478 const auto &binding = *translated->binding;
Jamie Madill9c385802015-06-22 13:57:18 -0400479
Martin Radevdd5f27e2017-06-07 10:17:09 +0300480 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -0400481 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400482 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400483
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500484 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400485
Jamie Madilld4b55a02015-01-09 14:21:49 -0500486 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300487 GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500488
Jamie Madill7d112bb2015-06-22 13:57:19 -0400489 // Compute source data pointer
490 const uint8_t *sourceData = nullptr;
491
492 if (buffer)
493 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400494 ANGLE_TRY(storage->getData(&sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800495 sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400496 }
497 else
498 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800499 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
500 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill7d112bb2015-06-22 13:57:19 -0400501 sourceData = static_cast<const uint8_t*>(attrib.pointer);
502 }
503
504 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500505
Jamie Madilld8fa9212016-03-02 11:51:43 -0500506 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800507 ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, binding, 1, 0), translated->stride);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500508
Martin Radev553590a2017-07-31 16:40:39 +0300509 size_t totalCount = gl::ComputeVertexBindingElementCount(
510 binding.getDivisor(), static_cast<size_t>(count), static_cast<size_t>(instances));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400511
Jamie Madill52b09c22016-04-11 14:12:31 -0400512 ANGLE_TRY(mStreamingBuffer->storeDynamicAttribute(
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800513 attrib, binding, translated->currentValueType, firstVertexIndex,
514 static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400515
Jamie Madille18eb972016-03-04 15:46:59 -0500516 VertexBuffer *vertexBuffer = mStreamingBuffer->getVertexBuffer();
517
518 translated->vertexBuffer.set(vertexBuffer);
519 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400520 translated->baseOffset = streamOffset;
521 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400522
Jamie Madill52b09c22016-04-11 14:12:31 -0400523 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400524}
525
Jamie Madill9c385802015-06-22 13:57:18 -0400526gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400527 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500528 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400529{
Jamie Madille18eb972016-03-04 15:46:59 -0500530 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
Jamie Madill401345e2017-08-21 10:52:40 -0400531 auto &buffer = cachedState->buffer;
Jamie Madille18eb972016-03-04 15:46:59 -0500532
533 if (!buffer)
534 {
Jamie Madill401345e2017-08-21 10:52:40 -0400535 buffer.reset(new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE));
Jamie Madille18eb972016-03-04 15:46:59 -0500536 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500537
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400538 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400539 {
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800540 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800541 const auto &attrib = *translated->attribute;
542 const auto &binding = *translated->binding;
Jamie Madill27c08912015-06-22 13:57:20 -0400543
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800544 ANGLE_TRY(buffer->reserveVertexSpace(attrib, binding, 1, 0));
Jamie Madillf41522b2014-08-18 16:39:49 -0400545
Jamie Madill7d112bb2015-06-22 13:57:19 -0400546 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400547 unsigned int streamOffset;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800548 ANGLE_TRY(buffer->storeDynamicAttribute(attrib, binding, currentValue.Type, 0, 1, 0,
549 &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400550
Jamie Madille36b92d2016-03-04 15:46:58 -0500551 buffer->getVertexBuffer()->hintUnmapResource();
552
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400553 cachedState->data = currentValue;
554 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400555 }
556
Jamie Madille36b92d2016-03-04 15:46:58 -0500557 translated->vertexBuffer.set(buffer->getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400558
Jamie Madille36b92d2016-03-04 15:46:58 -0500559 translated->storage = nullptr;
560 translated->serial = buffer->getSerial();
561 translated->divisor = 0;
562 translated->stride = 0;
Jamie Madill52b09c22016-04-11 14:12:31 -0400563 translated->baseOffset = static_cast<unsigned int>(cachedState->offset);
564 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400565
Jamie Madill52b09c22016-04-11 14:12:31 -0400566 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400567}
568
Jamie Madille36b92d2016-03-04 15:46:58 -0500569// VertexBufferBinding implementation
570VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
571{
572}
573
574VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
575 : mBoundVertexBuffer(other.mBoundVertexBuffer)
576{
577 if (mBoundVertexBuffer)
578 {
579 mBoundVertexBuffer->addRef();
580 }
581}
582
583VertexBufferBinding::~VertexBufferBinding()
584{
585 if (mBoundVertexBuffer)
586 {
587 mBoundVertexBuffer->release();
588 }
589}
590
591VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
592{
593 mBoundVertexBuffer = other.mBoundVertexBuffer;
594 if (mBoundVertexBuffer)
595 {
596 mBoundVertexBuffer->addRef();
597 }
598 return *this;
599}
600
601void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
602{
603 if (mBoundVertexBuffer == vertexBuffer)
604 return;
605
606 if (mBoundVertexBuffer)
607 {
608 mBoundVertexBuffer->release();
609 }
610 if (vertexBuffer)
611 {
612 vertexBuffer->addRef();
613 }
614
615 mBoundVertexBuffer = vertexBuffer;
616}
617
618VertexBuffer *VertexBufferBinding::get() const
619{
620 return mBoundVertexBuffer;
621}
622
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500623} // namespace rx