blob: 518fcd38256b7b24ba943085116472f2e201be81 [file] [log] [blame]
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +00007// VertexDataManager.h: Defines the VertexDataManager, a class that
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00008// runs the Buffer translation process.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/renderer/d3d/VertexDataManager.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040011
Jamie Madill20e005b2017-04-07 14:19:22 -040012#include "common/bitset_utils.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Buffer.h"
Jamie Madill33510102017-09-20 10:39:18 -040014#include "libANGLE/Context.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050015#include "libANGLE/Program.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040016#include "libANGLE/State.h"
Geoff Lang5ead9272015-03-25 12:27:43 -040017#include "libANGLE/VertexArray.h"
Jamie Madill33510102017-09-20 10:39:18 -040018#include "libANGLE/VertexAttribute.h"
19#include "libANGLE/formatutils.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040020#include "libANGLE/renderer/d3d/BufferD3D.h"
Jamie Madilldbfc6c62016-02-29 01:08:57 -050021#include "libANGLE/renderer/d3d/RendererD3D.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040022#include "libANGLE/renderer/d3d/VertexBuffer.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000023
Jamie Madille2e406c2016-06-02 13:04:10 -040024using namespace angle;
25
daniel@transgaming.com31240482012-11-28 21:06:41 +000026namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000027{
Jamie Madilldbfc6c62016-02-29 01:08:57 -050028namespace
29{
30enum
31{
32 INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024
33};
34// This has to be at least 4k or else it fails on ATI cards.
35enum
36{
37 CONSTANT_VERTEX_BUFFER_SIZE = 4096
38};
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039
Jiawei-Shao2597fb62016-12-09 16:38:02 +080040// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
41int ElementsInBuffer(const gl::VertexAttribute &attrib,
42 const gl::VertexBinding &binding,
43 unsigned int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000044{
Geoff Langa36ead42013-08-02 11:54:08 -040045 // Size cannot be larger than a GLsizei
46 if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
47 {
48 size = static_cast<unsigned int>(std::numeric_limits<int>::max());
49 }
50
Jiawei-Shao2597fb62016-12-09 16:38:02 +080051 GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib, binding));
52 GLsizei offset = static_cast<GLsizei>(ComputeVertexAttributeOffset(attrib, binding));
53 return (size - offset % stride +
Cooper Partin4d61f7e2015-08-12 10:56:50 -070054 (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) /
55 stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000056}
57
Jiawei-Shao2597fb62016-12-09 16:38:02 +080058// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
59bool DirectStoragePossible(const gl::VertexAttribute &attrib, const gl::VertexBinding &binding)
Jamie Madilldbfc6c62016-02-29 01:08:57 -050060{
61 // Current value attribs may not use direct storage.
62 if (!attrib.enabled)
63 {
64 return false;
65 }
66
Martin Radevdd5f27e2017-06-07 10:17:09 +030067 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madilldbfc6c62016-02-29 01:08:57 -050068 if (!buffer)
69 {
70 return false;
71 }
72
73 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
74 ASSERT(bufferD3D);
75 if (!bufferD3D->supportsDirectBinding())
76 {
77 return false;
78 }
79
Jamie Madilldbfc6c62016-02-29 01:08:57 -050080 // Alignment restrictions: In D3D, vertex data must be aligned to the format stride, or to a
81 // 4-byte boundary, whichever is smaller. (Undocumented, and experimentally confirmed)
Jamie Madilld8fa9212016-03-02 11:51:43 -050082 size_t alignment = 4;
Jamie Madilldbfc6c62016-02-29 01:08:57 -050083
Jamie Madille16a4512017-03-07 20:46:40 -050084 // TODO(jmadill): add VertexFormatCaps
85 BufferFactoryD3D *factory = bufferD3D->getFactory();
86
87 gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib);
88
89 // CPU-converted vertex data must be converted (naturally).
90 if ((factory->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_CPU) != 0)
91 {
92 return false;
93 }
94
Jamie Madilldbfc6c62016-02-29 01:08:57 -050095 if (attrib.type != GL_FLOAT)
96 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +080097 auto errorOrElementSize = factory->getVertexSpaceRequired(attrib, binding, 1, 0);
Jamie Madilld8fa9212016-03-02 11:51:43 -050098 if (errorOrElementSize.isError())
99 {
Yuly Novikovd73f8522017-01-13 17:48:57 -0500100 ERR() << "Unlogged error in DirectStoragePossible.";
Jamie Madilld8fa9212016-03-02 11:51:43 -0500101 return false;
102 }
103
104 alignment = std::min<size_t>(errorOrElementSize.getResult(), 4);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500105 }
106
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800107 GLintptr offset = ComputeVertexAttributeOffset(attrib, binding);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500108 // Final alignment check - unaligned data must be converted.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800109 return (static_cast<size_t>(ComputeVertexAttributeStride(attrib, binding)) % alignment == 0) &&
110 (static_cast<size_t>(offset) % alignment == 0);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500111}
112} // anonymous namespace
113
Jamie Madille36b92d2016-03-04 15:46:58 -0500114TranslatedAttribute::TranslatedAttribute()
115 : active(false),
116 attribute(nullptr),
Jamie Madill401345e2017-08-21 10:52:40 -0400117 binding(nullptr),
Jamie Madille36b92d2016-03-04 15:46:58 -0500118 currentValueType(GL_NONE),
Jamie Madill52b09c22016-04-11 14:12:31 -0400119 baseOffset(0),
120 usesFirstVertexOffset(false),
Jamie Madille36b92d2016-03-04 15:46:58 -0500121 stride(0),
122 vertexBuffer(),
123 storage(nullptr),
124 serial(0),
125 divisor(0)
126{
127}
128
Jamie Madill52b09c22016-04-11 14:12:31 -0400129gl::ErrorOrResult<unsigned int> TranslatedAttribute::computeOffset(GLint startVertex) const
130{
Jamie Madille2e406c2016-06-02 13:04:10 -0400131 if (!usesFirstVertexOffset)
Jamie Madill52b09c22016-04-11 14:12:31 -0400132 {
Jamie Madille2e406c2016-06-02 13:04:10 -0400133 return baseOffset;
Jamie Madill52b09c22016-04-11 14:12:31 -0400134 }
Jamie Madille2e406c2016-06-02 13:04:10 -0400135
136 CheckedNumeric<unsigned int> offset;
137
138 offset = baseOffset + stride * static_cast<unsigned int>(startVertex);
139 ANGLE_TRY_CHECKED_MATH(offset);
140 return offset.ValueOrDie();
Jamie Madill52b09c22016-04-11 14:12:31 -0400141}
142
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800143// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
144VertexStorageType ClassifyAttributeStorage(const gl::VertexAttribute &attrib,
145 const gl::VertexBinding &binding)
Jamie Madille18eb972016-03-04 15:46:59 -0500146{
147 // If attribute is disabled, we use the current value.
148 if (!attrib.enabled)
149 {
150 return VertexStorageType::CURRENT_VALUE;
151 }
152
153 // If specified with immediate data, we must use dynamic storage.
Martin Radevdd5f27e2017-06-07 10:17:09 +0300154 auto *buffer = binding.getBuffer().get();
Jamie Madille18eb972016-03-04 15:46:59 -0500155 if (!buffer)
156 {
157 return VertexStorageType::DYNAMIC;
158 }
159
160 // Check if the buffer supports direct storage.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800161 if (DirectStoragePossible(attrib, binding))
Jamie Madille18eb972016-03-04 15:46:59 -0500162 {
163 return VertexStorageType::DIRECT;
164 }
165
166 // Otherwise the storage is static or dynamic.
167 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
168 ASSERT(bufferD3D);
169 switch (bufferD3D->getUsage())
170 {
171 case D3DBufferUsage::DYNAMIC:
172 return VertexStorageType::DYNAMIC;
173 case D3DBufferUsage::STATIC:
174 return VertexStorageType::STATIC;
175 default:
176 UNREACHABLE();
177 return VertexStorageType::UNKNOWN;
178 }
179}
180
Jamie Madill401345e2017-08-21 10:52:40 -0400181VertexDataManager::CurrentValueState::CurrentValueState() : buffer(), offset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000182{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400183 data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
184 data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
185 data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
186 data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
187 data.Type = GL_FLOAT;
188}
daniel@transgaming.com83921382011-01-08 05:46:00 +0000189
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400190VertexDataManager::CurrentValueState::~CurrentValueState()
191{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400192}
193
194VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
Jamie Madill401345e2017-08-21 10:52:40 -0400195 : mFactory(factory), mStreamingBuffer(), mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS)
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400196{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000197}
198
199VertexDataManager::~VertexDataManager()
200{
Jamie Madill401345e2017-08-21 10:52:40 -0400201}
202
203gl::Error VertexDataManager::initialize()
204{
205 mStreamingBuffer.reset(
206 new StreamingVertexBufferInterface(mFactory, INITIAL_STREAM_BUFFER_SIZE));
207 if (!mStreamingBuffer)
208 {
209 return gl::OutOfMemory() << "Failed to allocate the streaming vertex buffer.";
210 }
211
212 return gl::NoError();
213}
214
215void VertexDataManager::deinitialize()
216{
217 mStreamingBuffer.reset();
218 mCurrentValueCache.clear();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000219}
220
Jamie Madill33510102017-09-20 10:39:18 -0400221gl::Error VertexDataManager::prepareVertexData(const gl::Context *context,
Jamie Madill476682e2015-06-30 10:04:29 -0400222 GLint start,
223 GLsizei count,
224 std::vector<TranslatedAttribute> *translatedAttribs,
225 GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000226{
Jamie Madille18eb972016-03-04 15:46:59 -0500227 ASSERT(mStreamingBuffer);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000228
Jamie Madill33510102017-09-20 10:39:18 -0400229 const gl::State &state = context->getGLState();
Geoff Lang5ead9272015-03-25 12:27:43 -0400230 const gl::VertexArray *vertexArray = state.getVertexArray();
Jamie Madill63805b42015-08-25 13:17:39 -0400231 const auto &vertexAttributes = vertexArray->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800232 const auto &vertexBindings = vertexArray->getVertexBindings();
Geoff Lang5ead9272015-03-25 12:27:43 -0400233
Jamie Madill52b09c22016-04-11 14:12:31 -0400234 mDynamicAttribsMaskCache.reset();
Jamie Madille18eb972016-03-04 15:46:59 -0500235 const gl::Program *program = state.getProgram();
236
Jamie Madill476682e2015-06-30 10:04:29 -0400237 translatedAttribs->clear();
Jamie Madill27c08912015-06-22 13:57:20 -0400238
Jamie Madill9c385802015-06-22 13:57:18 -0400239 for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000240 {
Jamie Madille18eb972016-03-04 15:46:59 -0500241 // Skip attrib locations the program doesn't use.
242 if (!program->isAttribLocationActive(attribIndex))
243 continue;
244
245 const auto &attrib = vertexAttributes[attribIndex];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800246 const auto &binding = vertexBindings[attrib.bindingIndex];
Jamie Madille18eb972016-03-04 15:46:59 -0500247
248 // Resize automatically puts in empty attribs
249 translatedAttribs->resize(attribIndex + 1);
250
251 TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
Jamie Madill6de51852017-04-12 09:53:01 -0400252 auto currentValueData = state.getVertexAttribCurrentValue(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500253
254 // Record the attribute now
255 translated->active = true;
256 translated->attribute = &attrib;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800257 translated->binding = &binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500258 translated->currentValueType = currentValueData.Type;
Martin Radevdd5f27e2017-06-07 10:17:09 +0300259 translated->divisor = binding.getDivisor();
Jamie Madille18eb972016-03-04 15:46:59 -0500260
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800261 switch (ClassifyAttributeStorage(attrib, binding))
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000262 {
Jamie Madille18eb972016-03-04 15:46:59 -0500263 case VertexStorageType::STATIC:
Jamie Madill9c385802015-06-22 13:57:18 -0400264 {
Jamie Madille18eb972016-03-04 15:46:59 -0500265 // Store static attribute.
Jamie Madill33510102017-09-20 10:39:18 -0400266 ANGLE_TRY(StoreStaticAttrib(context, translated));
Jamie Madille18eb972016-03-04 15:46:59 -0500267 break;
Jamie Madill9c385802015-06-22 13:57:18 -0400268 }
Jamie Madille18eb972016-03-04 15:46:59 -0500269 case VertexStorageType::DYNAMIC:
270 // Dynamic attributes must be handled together.
Jamie Madill52b09c22016-04-11 14:12:31 -0400271 mDynamicAttribsMaskCache.set(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500272 break;
273 case VertexStorageType::DIRECT:
274 // Update translated data for direct attributes.
Jamie Madill52b09c22016-04-11 14:12:31 -0400275 StoreDirectAttrib(translated);
Jamie Madille18eb972016-03-04 15:46:59 -0500276 break;
277 case VertexStorageType::CURRENT_VALUE:
Jamie Madill27c08912015-06-22 13:57:20 -0400278 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400279 ANGLE_TRY(storeCurrentValue(currentValueData, translated, attribIndex));
Jamie Madille18eb972016-03-04 15:46:59 -0500280 break;
Jamie Madill27c08912015-06-22 13:57:20 -0400281 }
Jamie Madille18eb972016-03-04 15:46:59 -0500282 default:
283 UNREACHABLE();
284 break;
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000285 }
286 }
287
Jamie Madill52b09c22016-04-11 14:12:31 -0400288 if (mDynamicAttribsMaskCache.none())
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000289 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400290 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500291 }
292
Jamie Madill33510102017-09-20 10:39:18 -0400293 ANGLE_TRY(storeDynamicAttribs(context, translatedAttribs, mDynamicAttribsMaskCache, start,
294 count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400295
Jamie Madill33510102017-09-20 10:39:18 -0400296 PromoteDynamicAttribs(context, *translatedAttribs, mDynamicAttribsMaskCache, count);
Jamie Madill52b09c22016-04-11 14:12:31 -0400297
298 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500299}
300
301// static
Jamie Madill52b09c22016-04-11 14:12:31 -0400302void VertexDataManager::StoreDirectAttrib(TranslatedAttribute *directAttrib)
Jamie Madille18eb972016-03-04 15:46:59 -0500303{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800304 ASSERT(directAttrib->attribute && directAttrib->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800305 const auto &attrib = *directAttrib->attribute;
306 const auto &binding = *directAttrib->binding;
307
Martin Radevdd5f27e2017-06-07 10:17:09 +0300308 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill7aa786c2017-09-19 13:57:15 -0400309 ASSERT(buffer);
310 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jamie Madille18eb972016-03-04 15:46:59 -0500311
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800312 ASSERT(DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500313 directAttrib->vertexBuffer.set(nullptr);
314 directAttrib->storage = bufferD3D;
315 directAttrib->serial = bufferD3D->getSerial();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800316 directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding));
317 directAttrib->baseOffset =
318 static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill52b09c22016-04-11 14:12:31 -0400319
320 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300321 directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madille18eb972016-03-04 15:46:59 -0500322}
323
324// static
Jamie Madill33510102017-09-20 10:39:18 -0400325gl::Error VertexDataManager::StoreStaticAttrib(const gl::Context *context,
326 TranslatedAttribute *translated)
Jamie Madille18eb972016-03-04 15:46:59 -0500327{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800328 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800329 const auto &attrib = *translated->attribute;
330 const auto &binding = *translated->binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500331
Martin Radevdd5f27e2017-06-07 10:17:09 +0300332 gl::Buffer *buffer = binding.getBuffer().get();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800333 ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500334 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
335
Jamie Madille18eb972016-03-04 15:46:59 -0500336 // Compute source data pointer
337 const uint8_t *sourceData = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800338 const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500339
Jamie Madill33510102017-09-20 10:39:18 -0400340 ANGLE_TRY(bufferD3D->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800341 sourceData += offset;
Jamie Madille18eb972016-03-04 15:46:59 -0500342
343 unsigned int streamOffset = 0;
344
Jamie Madille18eb972016-03-04 15:46:59 -0500345 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800346 ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, binding, 1, 0),
Jamie Madill52b09c22016-04-11 14:12:31 -0400347 translated->stride);
Jamie Madille18eb972016-03-04 15:46:59 -0500348
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800349 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
Jamie Madille18eb972016-03-04 15:46:59 -0500350 ASSERT(staticBuffer);
351
352 if (staticBuffer->empty())
353 {
354 // Convert the entire buffer
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800355 int totalCount =
356 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
357 int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500358
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800359 ANGLE_TRY(staticBuffer->storeStaticAttribute(attrib, binding, -startIndex, totalCount, 0,
360 sourceData));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000361 }
362
Jamie Madille18eb972016-03-04 15:46:59 -0500363 unsigned int firstElementOffset =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800364 (static_cast<unsigned int>(offset) /
365 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
Jamie Madille18eb972016-03-04 15:46:59 -0500366 translated->stride;
Jamie Madill27c08912015-06-22 13:57:20 -0400367
Jamie Madille18eb972016-03-04 15:46:59 -0500368 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
369
Jamie Madille2e406c2016-06-02 13:04:10 -0400370 CheckedNumeric<unsigned int> checkedOffset(streamOffset);
371 checkedOffset += firstElementOffset;
372
373 if (!checkedOffset.IsValid())
Jamie Madill52b09c22016-04-11 14:12:31 -0400374 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500375 return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib";
Jamie Madill52b09c22016-04-11 14:12:31 -0400376 }
377
Jamie Madille18eb972016-03-04 15:46:59 -0500378 translated->vertexBuffer.set(vertexBuffer);
379 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400380 translated->baseOffset = streamOffset + firstElementOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500381
Jamie Madill52b09c22016-04-11 14:12:31 -0400382 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300383 translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madill52b09c22016-04-11 14:12:31 -0400384
385 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500386}
387
388gl::Error VertexDataManager::storeDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400389 const gl::Context *context,
Jamie Madille18eb972016-03-04 15:46:59 -0500390 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill52b09c22016-04-11 14:12:31 -0400391 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madille18eb972016-03-04 15:46:59 -0500392 GLint start,
393 GLsizei count,
394 GLsizei instances)
395{
Jamie Madill52b09c22016-04-11 14:12:31 -0400396 // Instantiating this class will ensure the streaming buffer is never left mapped.
Jamie Madille2e406c2016-06-02 13:04:10 -0400397 class StreamingBufferUnmapper final : NonCopyable
Jamie Madill52b09c22016-04-11 14:12:31 -0400398 {
399 public:
400 StreamingBufferUnmapper(StreamingVertexBufferInterface *streamingBuffer)
401 : mStreamingBuffer(streamingBuffer)
402 {
403 ASSERT(mStreamingBuffer);
404 }
405 ~StreamingBufferUnmapper() { mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); }
406
407 private:
408 StreamingVertexBufferInterface *mStreamingBuffer;
409 };
410
411 // Will trigger unmapping on return.
Jamie Madill401345e2017-08-21 10:52:40 -0400412 StreamingBufferUnmapper localUnmapper(mStreamingBuffer.get());
Jamie Madill52b09c22016-04-11 14:12:31 -0400413
Jamie Madille18eb972016-03-04 15:46:59 -0500414 // Reserve the required space for the dynamic buffers.
Jamie Madill6de51852017-04-12 09:53:01 -0400415 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500416 {
417 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800418 ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, start, count, instances));
Jamie Madille18eb972016-03-04 15:46:59 -0500419 }
420
421 // Store dynamic attributes
Jamie Madill6de51852017-04-12 09:53:01 -0400422 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500423 {
424 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill33510102017-09-20 10:39:18 -0400425 ANGLE_TRY(storeDynamicAttrib(context, dynamicAttrib, start, count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400426 }
Shannon Woods1a965482014-09-22 18:00:32 -0400427
Jamie Madill52b09c22016-04-11 14:12:31 -0400428 return gl::NoError();
429}
430
431void VertexDataManager::PromoteDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400432 const gl::Context *context,
Jamie Madill52b09c22016-04-11 14:12:31 -0400433 const std::vector<TranslatedAttribute> &translatedAttribs,
434 const gl::AttributesMask &dynamicAttribsMask,
435 GLsizei count)
436{
Jamie Madill6de51852017-04-12 09:53:01 -0400437 for (auto attribIndex : dynamicAttribsMask)
Jamie Madill52b09c22016-04-11 14:12:31 -0400438 {
439 const auto &dynamicAttrib = translatedAttribs[attribIndex];
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800440 ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800441 const auto &binding = *dynamicAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800442
Martin Radevdd5f27e2017-06-07 10:17:09 +0300443 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill27c08912015-06-22 13:57:20 -0400444 if (buffer)
445 {
446 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800447 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
Jamie Madill33510102017-09-20 10:39:18 -0400448 bufferD3D->promoteStaticUsage(context, count * static_cast<int>(typeSize));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000449 }
450 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000451}
452
Jamie Madill3d72cc72015-06-22 13:57:19 -0400453gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800454 GLint start,
Geoff Langf7100b92014-09-08 16:17:08 -0400455 GLsizei count,
456 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400457{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800458 ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800459 const auto &attrib = *translatedAttrib.attribute;
460 const auto &binding = *translatedAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800461
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800462 ASSERT(!DirectStoragePossible(attrib, binding));
Jamie Madill6d113802014-08-25 15:47:52 -0400463
Martin Radevdd5f27e2017-06-07 10:17:09 +0300464 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500465 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800466 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
Jamie Madille36b92d2016-03-04 15:46:58 -0500467
Martin Radev553590a2017-07-31 16:40:39 +0300468 size_t totalCount = gl::ComputeVertexBindingElementCount(
469 binding.getDivisor(), static_cast<size_t>(count), static_cast<size_t>(instances));
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800470 // TODO(jiajia.qin@intel.com): force the index buffer to clamp any out of range indices instead
471 // of invalid operation here.
472 if (bufferD3D)
473 {
474 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
475 // a non-instanced draw call
476 GLint firstVertexIndex = binding.getDivisor() > 0 ? 0 : start;
477 int64_t maxVertexCount =
478 static_cast<int64_t>(firstVertexIndex) + static_cast<int64_t>(totalCount);
479 int elementsInBuffer =
480 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
Jamie Madille36b92d2016-03-04 15:46:58 -0500481
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800482 if (maxVertexCount > elementsInBuffer)
483 {
484 return gl::InvalidOperation() << "Vertex buffer is not big enough for the draw call.";
485 }
486 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800487 return mStreamingBuffer->reserveVertexSpace(attrib, binding, static_cast<GLsizei>(totalCount),
Jamie Madille36b92d2016-03-04 15:46:58 -0500488 instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400489}
490
Jamie Madill33510102017-09-20 10:39:18 -0400491gl::Error VertexDataManager::storeDynamicAttrib(const gl::Context *context,
492 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500493 GLint start,
494 GLsizei count,
495 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400496{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800497 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800498 const auto &attrib = *translated->attribute;
499 const auto &binding = *translated->binding;
Jamie Madill9c385802015-06-22 13:57:18 -0400500
Martin Radevdd5f27e2017-06-07 10:17:09 +0300501 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -0400502 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400503 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400504
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500505 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400506
Jamie Madilld4b55a02015-01-09 14:21:49 -0500507 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300508 GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500509
Jamie Madill7d112bb2015-06-22 13:57:19 -0400510 // Compute source data pointer
511 const uint8_t *sourceData = nullptr;
512
513 if (buffer)
514 {
Jamie Madill33510102017-09-20 10:39:18 -0400515 ANGLE_TRY(storage->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800516 sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400517 }
518 else
519 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800520 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
521 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill7d112bb2015-06-22 13:57:19 -0400522 sourceData = static_cast<const uint8_t*>(attrib.pointer);
523 }
524
525 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500526
Jamie Madilld8fa9212016-03-02 11:51:43 -0500527 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800528 ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, binding, 1, 0), translated->stride);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500529
Martin Radev553590a2017-07-31 16:40:39 +0300530 size_t totalCount = gl::ComputeVertexBindingElementCount(
531 binding.getDivisor(), static_cast<size_t>(count), static_cast<size_t>(instances));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400532
Jamie Madill52b09c22016-04-11 14:12:31 -0400533 ANGLE_TRY(mStreamingBuffer->storeDynamicAttribute(
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800534 attrib, binding, translated->currentValueType, firstVertexIndex,
535 static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400536
Jamie Madille18eb972016-03-04 15:46:59 -0500537 VertexBuffer *vertexBuffer = mStreamingBuffer->getVertexBuffer();
538
539 translated->vertexBuffer.set(vertexBuffer);
540 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400541 translated->baseOffset = streamOffset;
542 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400543
Jamie Madill52b09c22016-04-11 14:12:31 -0400544 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400545}
546
Jamie Madill9c385802015-06-22 13:57:18 -0400547gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400548 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500549 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400550{
Jamie Madille18eb972016-03-04 15:46:59 -0500551 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
Jamie Madill401345e2017-08-21 10:52:40 -0400552 auto &buffer = cachedState->buffer;
Jamie Madille18eb972016-03-04 15:46:59 -0500553
554 if (!buffer)
555 {
Jamie Madill401345e2017-08-21 10:52:40 -0400556 buffer.reset(new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE));
Jamie Madille18eb972016-03-04 15:46:59 -0500557 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500558
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400559 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400560 {
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800561 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800562 const auto &attrib = *translated->attribute;
563 const auto &binding = *translated->binding;
Jamie Madill27c08912015-06-22 13:57:20 -0400564
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800565 ANGLE_TRY(buffer->reserveVertexSpace(attrib, binding, 1, 0));
Jamie Madillf41522b2014-08-18 16:39:49 -0400566
Jamie Madill7d112bb2015-06-22 13:57:19 -0400567 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400568 unsigned int streamOffset;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800569 ANGLE_TRY(buffer->storeDynamicAttribute(attrib, binding, currentValue.Type, 0, 1, 0,
570 &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400571
Jamie Madille36b92d2016-03-04 15:46:58 -0500572 buffer->getVertexBuffer()->hintUnmapResource();
573
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400574 cachedState->data = currentValue;
575 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400576 }
577
Jamie Madille36b92d2016-03-04 15:46:58 -0500578 translated->vertexBuffer.set(buffer->getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400579
Jamie Madille36b92d2016-03-04 15:46:58 -0500580 translated->storage = nullptr;
581 translated->serial = buffer->getSerial();
582 translated->divisor = 0;
583 translated->stride = 0;
Jamie Madill52b09c22016-04-11 14:12:31 -0400584 translated->baseOffset = static_cast<unsigned int>(cachedState->offset);
585 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400586
Jamie Madill52b09c22016-04-11 14:12:31 -0400587 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400588}
589
Jamie Madille36b92d2016-03-04 15:46:58 -0500590// VertexBufferBinding implementation
591VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
592{
593}
594
595VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
596 : mBoundVertexBuffer(other.mBoundVertexBuffer)
597{
598 if (mBoundVertexBuffer)
599 {
600 mBoundVertexBuffer->addRef();
601 }
602}
603
604VertexBufferBinding::~VertexBufferBinding()
605{
606 if (mBoundVertexBuffer)
607 {
608 mBoundVertexBuffer->release();
609 }
610}
611
612VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
613{
614 mBoundVertexBuffer = other.mBoundVertexBuffer;
615 if (mBoundVertexBuffer)
616 {
617 mBoundVertexBuffer->addRef();
618 }
619 return *this;
620}
621
622void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
623{
624 if (mBoundVertexBuffer == vertexBuffer)
625 return;
626
627 if (mBoundVertexBuffer)
628 {
629 mBoundVertexBuffer->release();
630 }
631 if (vertexBuffer)
632 {
633 vertexBuffer->addRef();
634 }
635
636 mBoundVertexBuffer = vertexBuffer;
637}
638
639VertexBuffer *VertexBufferBinding::get() const
640{
641 return mBoundVertexBuffer;
642}
643
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500644} // namespace rx