blob: 9f254da4baaffd22503d7cac0532ebcf00b12884 [file] [log] [blame]
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +00007// VertexDataManager.h: Defines the VertexDataManager, a class that
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00008// runs the Buffer translation process.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/renderer/d3d/VertexDataManager.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040011
Jamie Madill20e005b2017-04-07 14:19:22 -040012#include "common/bitset_utils.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Buffer.h"
Jamie Madill33510102017-09-20 10:39:18 -040014#include "libANGLE/Context.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050015#include "libANGLE/Program.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040016#include "libANGLE/State.h"
Geoff Lang5ead9272015-03-25 12:27:43 -040017#include "libANGLE/VertexArray.h"
Jamie Madill33510102017-09-20 10:39:18 -040018#include "libANGLE/VertexAttribute.h"
19#include "libANGLE/formatutils.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040020#include "libANGLE/renderer/d3d/BufferD3D.h"
Jamie Madilldbfc6c62016-02-29 01:08:57 -050021#include "libANGLE/renderer/d3d/RendererD3D.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040022#include "libANGLE/renderer/d3d/VertexBuffer.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000023
Jamie Madille2e406c2016-06-02 13:04:10 -040024using namespace angle;
25
daniel@transgaming.com31240482012-11-28 21:06:41 +000026namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000027{
Jamie Madilldbfc6c62016-02-29 01:08:57 -050028namespace
29{
30enum
31{
32 INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024
33};
34// This has to be at least 4k or else it fails on ATI cards.
35enum
36{
37 CONSTANT_VERTEX_BUFFER_SIZE = 4096
38};
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039
Jiawei-Shao2597fb62016-12-09 16:38:02 +080040// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
41int ElementsInBuffer(const gl::VertexAttribute &attrib,
42 const gl::VertexBinding &binding,
43 unsigned int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000044{
Geoff Langa36ead42013-08-02 11:54:08 -040045 // Size cannot be larger than a GLsizei
46 if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
47 {
48 size = static_cast<unsigned int>(std::numeric_limits<int>::max());
49 }
50
Jiawei-Shao2597fb62016-12-09 16:38:02 +080051 GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib, binding));
52 GLsizei offset = static_cast<GLsizei>(ComputeVertexAttributeOffset(attrib, binding));
53 return (size - offset % stride +
Cooper Partin4d61f7e2015-08-12 10:56:50 -070054 (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) /
55 stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000056}
57
Jiawei-Shao2597fb62016-12-09 16:38:02 +080058// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
Jamie Madillb1565902018-07-27 08:12:48 -040059bool DirectStoragePossible(const gl::Context *context,
60 const gl::VertexAttribute &attrib,
61 const gl::VertexBinding &binding)
Jamie Madilldbfc6c62016-02-29 01:08:57 -050062{
63 // Current value attribs may not use direct storage.
64 if (!attrib.enabled)
65 {
66 return false;
67 }
68
Martin Radevdd5f27e2017-06-07 10:17:09 +030069 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madilldbfc6c62016-02-29 01:08:57 -050070 if (!buffer)
71 {
72 return false;
73 }
74
75 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
76 ASSERT(bufferD3D);
77 if (!bufferD3D->supportsDirectBinding())
78 {
79 return false;
80 }
81
Jamie Madilldbfc6c62016-02-29 01:08:57 -050082 // Alignment restrictions: In D3D, vertex data must be aligned to the format stride, or to a
83 // 4-byte boundary, whichever is smaller. (Undocumented, and experimentally confirmed)
Jamie Madilld8fa9212016-03-02 11:51:43 -050084 size_t alignment = 4;
Jamie Madilldbfc6c62016-02-29 01:08:57 -050085
Jamie Madille16a4512017-03-07 20:46:40 -050086 // TODO(jmadill): add VertexFormatCaps
87 BufferFactoryD3D *factory = bufferD3D->getFactory();
88
89 gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib);
90
91 // CPU-converted vertex data must be converted (naturally).
92 if ((factory->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_CPU) != 0)
93 {
94 return false;
95 }
96
Jamie Madilldbfc6c62016-02-29 01:08:57 -050097 if (attrib.type != GL_FLOAT)
98 {
Jamie Madill854429d2018-07-27 08:12:48 -040099 unsigned int elementSize = 0;
100 gl::Error error =
101 factory->getVertexSpaceRequired(context, attrib, binding, 1, 0, &elementSize);
102 ASSERT(!error.isError());
103 alignment = std::min<size_t>(elementSize, 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 Madillacf2f3a2017-11-21 19:22:44 -0500128TranslatedAttribute::TranslatedAttribute(const TranslatedAttribute &other) = default;
129
Jamie Madill02b53282018-07-24 11:06:47 -0400130gl::Error TranslatedAttribute::computeOffset(GLint startVertex, unsigned int *offsetOut) const
Jamie Madill52b09c22016-04-11 14:12:31 -0400131{
Jamie Madille2e406c2016-06-02 13:04:10 -0400132 if (!usesFirstVertexOffset)
Jamie Madill52b09c22016-04-11 14:12:31 -0400133 {
Jamie Madill02b53282018-07-24 11:06:47 -0400134 *offsetOut = baseOffset;
135 return gl::NoError();
Jamie Madill52b09c22016-04-11 14:12:31 -0400136 }
Jamie Madille2e406c2016-06-02 13:04:10 -0400137
Jamie Madill02b53282018-07-24 11:06:47 -0400138 CheckedNumeric<unsigned int> offset(baseOffset);
139 CheckedNumeric<unsigned int> checkedStride(stride);
Jamie Madille2e406c2016-06-02 13:04:10 -0400140
Jamie Madill02b53282018-07-24 11:06:47 -0400141 offset += checkedStride * static_cast<unsigned int>(startVertex);
Jamie Madillca2ff382018-07-11 09:01:17 -0400142 ANGLE_TRY_CHECKED_MATH(offset.IsValid());
Jamie Madill02b53282018-07-24 11:06:47 -0400143 *offsetOut = offset.ValueOrDie();
144 return gl::NoError();
Jamie Madill52b09c22016-04-11 14:12:31 -0400145}
146
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800147// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
Jamie Madillb1565902018-07-27 08:12:48 -0400148VertexStorageType ClassifyAttributeStorage(const gl::Context *context,
149 const gl::VertexAttribute &attrib,
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800150 const gl::VertexBinding &binding)
Jamie Madille18eb972016-03-04 15:46:59 -0500151{
152 // If attribute is disabled, we use the current value.
153 if (!attrib.enabled)
154 {
155 return VertexStorageType::CURRENT_VALUE;
156 }
157
158 // If specified with immediate data, we must use dynamic storage.
Jamie Madill09463932018-04-04 05:26:59 -0400159 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille18eb972016-03-04 15:46:59 -0500160 if (!buffer)
161 {
162 return VertexStorageType::DYNAMIC;
163 }
164
165 // Check if the buffer supports direct storage.
Jamie Madillb1565902018-07-27 08:12:48 -0400166 if (DirectStoragePossible(context, attrib, binding))
Jamie Madille18eb972016-03-04 15:46:59 -0500167 {
168 return VertexStorageType::DIRECT;
169 }
170
171 // Otherwise the storage is static or dynamic.
172 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
173 ASSERT(bufferD3D);
174 switch (bufferD3D->getUsage())
175 {
176 case D3DBufferUsage::DYNAMIC:
177 return VertexStorageType::DYNAMIC;
178 case D3DBufferUsage::STATIC:
179 return VertexStorageType::STATIC;
180 default:
181 UNREACHABLE();
182 return VertexStorageType::UNKNOWN;
183 }
184}
185
Jamie Madillb1565902018-07-27 08:12:48 -0400186VertexDataManager::CurrentValueState::CurrentValueState(BufferFactoryD3D *factory)
187 : buffer(new StreamingVertexBufferInterface(factory)), offset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000188{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400189 data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
190 data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
191 data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
192 data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
193 data.Type = GL_FLOAT;
194}
daniel@transgaming.com83921382011-01-08 05:46:00 +0000195
Jamie Madillb1565902018-07-27 08:12:48 -0400196VertexDataManager::CurrentValueState::CurrentValueState(CurrentValueState &&other)
197{
198 std::swap(buffer, other.buffer);
199 std::swap(data, other.data);
200 std::swap(offset, other.offset);
201}
202
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400203VertexDataManager::CurrentValueState::~CurrentValueState()
204{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400205}
206
207VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
Jamie Madillb1565902018-07-27 08:12:48 -0400208 : mFactory(factory), mStreamingBuffer(factory)
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400209{
Jamie Madillb1565902018-07-27 08:12:48 -0400210 for (int currentValueIndex = 0; currentValueIndex < gl::MAX_VERTEX_ATTRIBS; ++currentValueIndex)
211 {
212 mCurrentValueCache.emplace_back(factory);
213 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000214}
215
216VertexDataManager::~VertexDataManager()
217{
Jamie Madill401345e2017-08-21 10:52:40 -0400218}
219
Jamie Madillb1565902018-07-27 08:12:48 -0400220gl::Error VertexDataManager::initialize(const gl::Context *context)
Jamie Madill401345e2017-08-21 10:52:40 -0400221{
Jamie Madillb1565902018-07-27 08:12:48 -0400222 return mStreamingBuffer.initialize(context, INITIAL_STREAM_BUFFER_SIZE);
Jamie Madill401345e2017-08-21 10:52:40 -0400223}
224
225void VertexDataManager::deinitialize()
226{
227 mStreamingBuffer.reset();
228 mCurrentValueCache.clear();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000229}
230
Jamie Madill33510102017-09-20 10:39:18 -0400231gl::Error VertexDataManager::prepareVertexData(const gl::Context *context,
Jamie Madill476682e2015-06-30 10:04:29 -0400232 GLint start,
233 GLsizei count,
234 std::vector<TranslatedAttribute> *translatedAttribs,
235 GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000236{
Jamie Madill33510102017-09-20 10:39:18 -0400237 const gl::State &state = context->getGLState();
Geoff Lang5ead9272015-03-25 12:27:43 -0400238 const gl::VertexArray *vertexArray = state.getVertexArray();
Jamie Madill63805b42015-08-25 13:17:39 -0400239 const auto &vertexAttributes = vertexArray->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800240 const auto &vertexBindings = vertexArray->getVertexBindings();
Geoff Lang5ead9272015-03-25 12:27:43 -0400241
Jamie Madill52b09c22016-04-11 14:12:31 -0400242 mDynamicAttribsMaskCache.reset();
Jamie Madille18eb972016-03-04 15:46:59 -0500243 const gl::Program *program = state.getProgram();
244
Jamie Madill476682e2015-06-30 10:04:29 -0400245 translatedAttribs->clear();
Jamie Madill27c08912015-06-22 13:57:20 -0400246
Jamie Madill9c385802015-06-22 13:57:18 -0400247 for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000248 {
Jamie Madille18eb972016-03-04 15:46:59 -0500249 // Skip attrib locations the program doesn't use.
250 if (!program->isAttribLocationActive(attribIndex))
251 continue;
252
253 const auto &attrib = vertexAttributes[attribIndex];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800254 const auto &binding = vertexBindings[attrib.bindingIndex];
Jamie Madille18eb972016-03-04 15:46:59 -0500255
256 // Resize automatically puts in empty attribs
257 translatedAttribs->resize(attribIndex + 1);
258
259 TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
Jamie Madill6de51852017-04-12 09:53:01 -0400260 auto currentValueData = state.getVertexAttribCurrentValue(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500261
262 // Record the attribute now
263 translated->active = true;
264 translated->attribute = &attrib;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800265 translated->binding = &binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500266 translated->currentValueType = currentValueData.Type;
Martin Radevdd5f27e2017-06-07 10:17:09 +0300267 translated->divisor = binding.getDivisor();
Jamie Madille18eb972016-03-04 15:46:59 -0500268
Jamie Madillb1565902018-07-27 08:12:48 -0400269 switch (ClassifyAttributeStorage(context, attrib, binding))
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000270 {
Jamie Madille18eb972016-03-04 15:46:59 -0500271 case VertexStorageType::STATIC:
Jamie Madill9c385802015-06-22 13:57:18 -0400272 {
Jamie Madille18eb972016-03-04 15:46:59 -0500273 // Store static attribute.
Jamie Madill33510102017-09-20 10:39:18 -0400274 ANGLE_TRY(StoreStaticAttrib(context, translated));
Jamie Madille18eb972016-03-04 15:46:59 -0500275 break;
Jamie Madill9c385802015-06-22 13:57:18 -0400276 }
Jamie Madille18eb972016-03-04 15:46:59 -0500277 case VertexStorageType::DYNAMIC:
278 // Dynamic attributes must be handled together.
Jamie Madill52b09c22016-04-11 14:12:31 -0400279 mDynamicAttribsMaskCache.set(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500280 break;
281 case VertexStorageType::DIRECT:
282 // Update translated data for direct attributes.
Jamie Madillb1565902018-07-27 08:12:48 -0400283 StoreDirectAttrib(context, translated);
Jamie Madille18eb972016-03-04 15:46:59 -0500284 break;
285 case VertexStorageType::CURRENT_VALUE:
Jamie Madill27c08912015-06-22 13:57:20 -0400286 {
Jamie Madillb1565902018-07-27 08:12:48 -0400287 ANGLE_TRY(storeCurrentValue(context, currentValueData, translated, attribIndex));
Jamie Madille18eb972016-03-04 15:46:59 -0500288 break;
Jamie Madill27c08912015-06-22 13:57:20 -0400289 }
Jamie Madille18eb972016-03-04 15:46:59 -0500290 default:
291 UNREACHABLE();
292 break;
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000293 }
294 }
295
Jamie Madill52b09c22016-04-11 14:12:31 -0400296 if (mDynamicAttribsMaskCache.none())
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000297 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400298 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500299 }
300
Jamie Madill33510102017-09-20 10:39:18 -0400301 ANGLE_TRY(storeDynamicAttribs(context, translatedAttribs, mDynamicAttribsMaskCache, start,
302 count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400303
Jamie Madill33510102017-09-20 10:39:18 -0400304 PromoteDynamicAttribs(context, *translatedAttribs, mDynamicAttribsMaskCache, count);
Jamie Madill52b09c22016-04-11 14:12:31 -0400305
306 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500307}
308
309// static
Jamie Madillb1565902018-07-27 08:12:48 -0400310void VertexDataManager::StoreDirectAttrib(const gl::Context *context,
311 TranslatedAttribute *directAttrib)
Jamie Madille18eb972016-03-04 15:46:59 -0500312{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800313 ASSERT(directAttrib->attribute && directAttrib->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800314 const auto &attrib = *directAttrib->attribute;
315 const auto &binding = *directAttrib->binding;
316
Martin Radevdd5f27e2017-06-07 10:17:09 +0300317 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill7aa786c2017-09-19 13:57:15 -0400318 ASSERT(buffer);
319 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jamie Madille18eb972016-03-04 15:46:59 -0500320
Jamie Madillb1565902018-07-27 08:12:48 -0400321 ASSERT(DirectStoragePossible(context, attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500322 directAttrib->vertexBuffer.set(nullptr);
323 directAttrib->storage = bufferD3D;
324 directAttrib->serial = bufferD3D->getSerial();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800325 directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding));
326 directAttrib->baseOffset =
327 static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill52b09c22016-04-11 14:12:31 -0400328
329 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300330 directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madille18eb972016-03-04 15:46:59 -0500331}
332
333// static
Jamie Madill33510102017-09-20 10:39:18 -0400334gl::Error VertexDataManager::StoreStaticAttrib(const gl::Context *context,
335 TranslatedAttribute *translated)
Jamie Madille18eb972016-03-04 15:46:59 -0500336{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800337 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800338 const auto &attrib = *translated->attribute;
339 const auto &binding = *translated->binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500340
Martin Radevdd5f27e2017-06-07 10:17:09 +0300341 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madillb1565902018-07-27 08:12:48 -0400342 ASSERT(buffer && attrib.enabled && !DirectStoragePossible(context, attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500343 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
344
Jamie Madille18eb972016-03-04 15:46:59 -0500345 // Compute source data pointer
346 const uint8_t *sourceData = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800347 const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500348
Jamie Madill33510102017-09-20 10:39:18 -0400349 ANGLE_TRY(bufferD3D->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800350 sourceData += offset;
Jamie Madille18eb972016-03-04 15:46:59 -0500351
352 unsigned int streamOffset = 0;
353
Jamie Madille18eb972016-03-04 15:46:59 -0500354 translated->storage = nullptr;
Jamie Madill854429d2018-07-27 08:12:48 -0400355 ANGLE_TRY(bufferD3D->getFactory()->getVertexSpaceRequired(context, attrib, binding, 1, 0,
356 &translated->stride));
Jamie Madille18eb972016-03-04 15:46:59 -0500357
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800358 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
Jamie Madille18eb972016-03-04 15:46:59 -0500359 ASSERT(staticBuffer);
360
361 if (staticBuffer->empty())
362 {
363 // Convert the entire buffer
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800364 int totalCount =
365 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
366 int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500367
Jamie Madillb1565902018-07-27 08:12:48 -0400368 ANGLE_TRY(staticBuffer->storeStaticAttribute(context, attrib, binding, -startIndex,
369 totalCount, 0, sourceData));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000370 }
371
Jamie Madille18eb972016-03-04 15:46:59 -0500372 unsigned int firstElementOffset =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800373 (static_cast<unsigned int>(offset) /
374 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
Jamie Madille18eb972016-03-04 15:46:59 -0500375 translated->stride;
Jamie Madill27c08912015-06-22 13:57:20 -0400376
Jamie Madille18eb972016-03-04 15:46:59 -0500377 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
378
Jamie Madille2e406c2016-06-02 13:04:10 -0400379 CheckedNumeric<unsigned int> checkedOffset(streamOffset);
380 checkedOffset += firstElementOffset;
381
382 if (!checkedOffset.IsValid())
Jamie Madill52b09c22016-04-11 14:12:31 -0400383 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500384 return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib";
Jamie Madill52b09c22016-04-11 14:12:31 -0400385 }
386
Jamie Madille18eb972016-03-04 15:46:59 -0500387 translated->vertexBuffer.set(vertexBuffer);
388 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400389 translated->baseOffset = streamOffset + firstElementOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500390
Jamie Madill52b09c22016-04-11 14:12:31 -0400391 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300392 translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madill52b09c22016-04-11 14:12:31 -0400393
394 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500395}
396
397gl::Error VertexDataManager::storeDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400398 const gl::Context *context,
Jamie Madille18eb972016-03-04 15:46:59 -0500399 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill52b09c22016-04-11 14:12:31 -0400400 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madille18eb972016-03-04 15:46:59 -0500401 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400402 size_t count,
Jamie Madille18eb972016-03-04 15:46:59 -0500403 GLsizei instances)
404{
Jamie Madill52b09c22016-04-11 14:12:31 -0400405 // Instantiating this class will ensure the streaming buffer is never left mapped.
Jamie Madille2e406c2016-06-02 13:04:10 -0400406 class StreamingBufferUnmapper final : NonCopyable
Jamie Madill52b09c22016-04-11 14:12:31 -0400407 {
408 public:
409 StreamingBufferUnmapper(StreamingVertexBufferInterface *streamingBuffer)
410 : mStreamingBuffer(streamingBuffer)
411 {
412 ASSERT(mStreamingBuffer);
413 }
414 ~StreamingBufferUnmapper() { mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); }
415
416 private:
417 StreamingVertexBufferInterface *mStreamingBuffer;
418 };
419
420 // Will trigger unmapping on return.
Jamie Madill021bad42018-07-27 08:12:47 -0400421 StreamingBufferUnmapper localUnmapper(&mStreamingBuffer);
Jamie Madill52b09c22016-04-11 14:12:31 -0400422
Jamie Madille18eb972016-03-04 15:46:59 -0500423 // Reserve the required space for the dynamic buffers.
Jamie Madill6de51852017-04-12 09:53:01 -0400424 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500425 {
426 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Jamie Madillb1565902018-07-27 08:12:48 -0400427 ANGLE_TRY(reserveSpaceForAttrib(context, dynamicAttrib, start, count, instances));
Jamie Madille18eb972016-03-04 15:46:59 -0500428 }
429
430 // Store dynamic attributes
Jamie Madill6de51852017-04-12 09:53:01 -0400431 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500432 {
433 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill33510102017-09-20 10:39:18 -0400434 ANGLE_TRY(storeDynamicAttrib(context, dynamicAttrib, start, count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400435 }
Shannon Woods1a965482014-09-22 18:00:32 -0400436
Jamie Madill52b09c22016-04-11 14:12:31 -0400437 return gl::NoError();
438}
439
440void VertexDataManager::PromoteDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400441 const gl::Context *context,
Jamie Madill52b09c22016-04-11 14:12:31 -0400442 const std::vector<TranslatedAttribute> &translatedAttribs,
443 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madill18e323a2018-05-11 16:54:17 -0400444 size_t count)
Jamie Madill52b09c22016-04-11 14:12:31 -0400445{
Jamie Madill6de51852017-04-12 09:53:01 -0400446 for (auto attribIndex : dynamicAttribsMask)
Jamie Madill52b09c22016-04-11 14:12:31 -0400447 {
448 const auto &dynamicAttrib = translatedAttribs[attribIndex];
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800449 ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800450 const auto &binding = *dynamicAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800451
Martin Radevdd5f27e2017-06-07 10:17:09 +0300452 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill27c08912015-06-22 13:57:20 -0400453 if (buffer)
454 {
Jamie Madill18e323a2018-05-11 16:54:17 -0400455 // Note: this multiplication can overflow. It should not be a security problem.
Jamie Madill27c08912015-06-22 13:57:20 -0400456 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800457 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
Jamie Madill18e323a2018-05-11 16:54:17 -0400458 bufferD3D->promoteStaticUsage(context, count * typeSize);
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000459 }
460 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000461}
462
Jamie Madillb1565902018-07-27 08:12:48 -0400463gl::Error VertexDataManager::reserveSpaceForAttrib(const gl::Context *context,
464 const TranslatedAttribute &translatedAttrib,
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800465 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400466 size_t count,
Jamie Madill021bad42018-07-27 08:12:47 -0400467 GLsizei instances)
Jamie Madill6d113802014-08-25 15:47:52 -0400468{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800469 ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800470 const auto &attrib = *translatedAttrib.attribute;
471 const auto &binding = *translatedAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800472
Jamie Madillb1565902018-07-27 08:12:48 -0400473 ASSERT(!DirectStoragePossible(context, attrib, binding));
Jamie Madill6d113802014-08-25 15:47:52 -0400474
Martin Radevdd5f27e2017-06-07 10:17:09 +0300475 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500476 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800477 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
Jamie Madille36b92d2016-03-04 15:46:58 -0500478
Jamie Madill18e323a2018-05-11 16:54:17 -0400479 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
480 static_cast<size_t>(instances));
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800481 // TODO(jiajia.qin@intel.com): force the index buffer to clamp any out of range indices instead
482 // of invalid operation here.
483 if (bufferD3D)
484 {
485 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
486 // a non-instanced draw call
487 GLint firstVertexIndex = binding.getDivisor() > 0 ? 0 : start;
488 int64_t maxVertexCount =
489 static_cast<int64_t>(firstVertexIndex) + static_cast<int64_t>(totalCount);
490 int elementsInBuffer =
491 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
Jamie Madille36b92d2016-03-04 15:46:58 -0500492
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800493 if (maxVertexCount > elementsInBuffer)
494 {
495 return gl::InvalidOperation() << "Vertex buffer is not big enough for the draw call.";
496 }
497 }
Jamie Madillb1565902018-07-27 08:12:48 -0400498 return mStreamingBuffer.reserveVertexSpace(context, attrib, binding, totalCount, instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400499}
500
Jamie Madill33510102017-09-20 10:39:18 -0400501gl::Error VertexDataManager::storeDynamicAttrib(const gl::Context *context,
502 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500503 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400504 size_t count,
Jamie Madill021bad42018-07-27 08:12:47 -0400505 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400506{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800507 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800508 const auto &attrib = *translated->attribute;
509 const auto &binding = *translated->binding;
Jamie Madill9c385802015-06-22 13:57:18 -0400510
Martin Radevdd5f27e2017-06-07 10:17:09 +0300511 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -0400512 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400513 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400514
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500515 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400516
Jamie Madilld4b55a02015-01-09 14:21:49 -0500517 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300518 GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500519
Jamie Madill7d112bb2015-06-22 13:57:19 -0400520 // Compute source data pointer
521 const uint8_t *sourceData = nullptr;
522
523 if (buffer)
524 {
Jamie Madill33510102017-09-20 10:39:18 -0400525 ANGLE_TRY(storage->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800526 sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400527 }
528 else
529 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800530 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
531 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill7d112bb2015-06-22 13:57:19 -0400532 sourceData = static_cast<const uint8_t*>(attrib.pointer);
533 }
534
535 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500536
Jamie Madilld8fa9212016-03-02 11:51:43 -0500537 translated->storage = nullptr;
Jamie Madill854429d2018-07-27 08:12:48 -0400538 ANGLE_TRY(
539 mFactory->getVertexSpaceRequired(context, attrib, binding, 1, 0, &translated->stride));
Jamie Madilld8fa9212016-03-02 11:51:43 -0500540
Jamie Madill18e323a2018-05-11 16:54:17 -0400541 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
542 static_cast<size_t>(instances));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400543
Jamie Madill021bad42018-07-27 08:12:47 -0400544 ANGLE_TRY(mStreamingBuffer.storeDynamicAttribute(
Jamie Madillb1565902018-07-27 08:12:48 -0400545 context, attrib, binding, translated->currentValueType, firstVertexIndex,
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800546 static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400547
Jamie Madill021bad42018-07-27 08:12:47 -0400548 VertexBuffer *vertexBuffer = mStreamingBuffer.getVertexBuffer();
Jamie Madille18eb972016-03-04 15:46:59 -0500549
550 translated->vertexBuffer.set(vertexBuffer);
551 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400552 translated->baseOffset = streamOffset;
553 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400554
Jamie Madill52b09c22016-04-11 14:12:31 -0400555 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400556}
557
Jamie Madillb1565902018-07-27 08:12:48 -0400558gl::Error VertexDataManager::storeCurrentValue(const gl::Context *context,
559 const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400560 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500561 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400562{
Jamie Madille18eb972016-03-04 15:46:59 -0500563 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
Jamie Madillb1565902018-07-27 08:12:48 -0400564 StreamingVertexBufferInterface &buffer = *cachedState->buffer;
Jamie Madille18eb972016-03-04 15:46:59 -0500565
Jamie Madillb1565902018-07-27 08:12:48 -0400566 if (buffer.getBufferSize() == 0)
Jamie Madille18eb972016-03-04 15:46:59 -0500567 {
Jamie Madillb1565902018-07-27 08:12:48 -0400568 ANGLE_TRY(buffer.initialize(context, CONSTANT_VERTEX_BUFFER_SIZE));
Jamie Madille18eb972016-03-04 15:46:59 -0500569 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500570
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400571 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400572 {
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800573 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800574 const auto &attrib = *translated->attribute;
575 const auto &binding = *translated->binding;
Jamie Madill27c08912015-06-22 13:57:20 -0400576
Jamie Madillb1565902018-07-27 08:12:48 -0400577 ANGLE_TRY(buffer.reserveVertexSpace(context, attrib, binding, 1, 0));
Jamie Madillf41522b2014-08-18 16:39:49 -0400578
Jamie Madill7d112bb2015-06-22 13:57:19 -0400579 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400580 unsigned int streamOffset;
Jamie Madillb1565902018-07-27 08:12:48 -0400581 ANGLE_TRY(buffer.storeDynamicAttribute(context, attrib, binding, currentValue.Type, 0, 1, 0,
582 &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400583
Jamie Madillb1565902018-07-27 08:12:48 -0400584 buffer.getVertexBuffer()->hintUnmapResource();
Jamie Madille36b92d2016-03-04 15:46:58 -0500585
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400586 cachedState->data = currentValue;
587 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400588 }
589
Jamie Madillb1565902018-07-27 08:12:48 -0400590 translated->vertexBuffer.set(buffer.getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400591
Jamie Madille36b92d2016-03-04 15:46:58 -0500592 translated->storage = nullptr;
Jamie Madillb1565902018-07-27 08:12:48 -0400593 translated->serial = buffer.getSerial();
Jamie Madille36b92d2016-03-04 15:46:58 -0500594 translated->divisor = 0;
595 translated->stride = 0;
Jamie Madill52b09c22016-04-11 14:12:31 -0400596 translated->baseOffset = static_cast<unsigned int>(cachedState->offset);
597 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400598
Jamie Madill52b09c22016-04-11 14:12:31 -0400599 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400600}
601
Jamie Madille36b92d2016-03-04 15:46:58 -0500602// VertexBufferBinding implementation
603VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
604{
605}
606
607VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
608 : mBoundVertexBuffer(other.mBoundVertexBuffer)
609{
610 if (mBoundVertexBuffer)
611 {
612 mBoundVertexBuffer->addRef();
613 }
614}
615
616VertexBufferBinding::~VertexBufferBinding()
617{
618 if (mBoundVertexBuffer)
619 {
620 mBoundVertexBuffer->release();
621 }
622}
623
624VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
625{
626 mBoundVertexBuffer = other.mBoundVertexBuffer;
627 if (mBoundVertexBuffer)
628 {
629 mBoundVertexBuffer->addRef();
630 }
631 return *this;
632}
633
634void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
635{
636 if (mBoundVertexBuffer == vertexBuffer)
637 return;
638
639 if (mBoundVertexBuffer)
640 {
641 mBoundVertexBuffer->release();
642 }
643 if (vertexBuffer)
644 {
645 vertexBuffer->addRef();
646 }
647
648 mBoundVertexBuffer = vertexBuffer;
649}
650
651VertexBuffer *VertexBufferBinding::get() const
652{
653 return mBoundVertexBuffer;
654}
655
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500656} // namespace rx