blob: fa47053ff3ca9ae92d77e071e4db98bdfbddb2f3 [file] [log] [blame]
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +00007// VertexDataManager.h: Defines the VertexDataManager, a class that
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00008// runs the Buffer translation process.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/renderer/d3d/VertexDataManager.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040011
Jamie Madill20e005b2017-04-07 14:19:22 -040012#include "common/bitset_utils.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Buffer.h"
Jamie Madill33510102017-09-20 10:39:18 -040014#include "libANGLE/Context.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050015#include "libANGLE/Program.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040016#include "libANGLE/State.h"
Geoff Lang5ead9272015-03-25 12:27:43 -040017#include "libANGLE/VertexArray.h"
Jamie Madill33510102017-09-20 10:39:18 -040018#include "libANGLE/VertexAttribute.h"
19#include "libANGLE/formatutils.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040020#include "libANGLE/renderer/d3d/BufferD3D.h"
Jamie Madilldbfc6c62016-02-29 01:08:57 -050021#include "libANGLE/renderer/d3d/RendererD3D.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040022#include "libANGLE/renderer/d3d/VertexBuffer.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000023
Jamie Madille2e406c2016-06-02 13:04:10 -040024using namespace angle;
25
daniel@transgaming.com31240482012-11-28 21:06:41 +000026namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000027{
Jamie Madilldbfc6c62016-02-29 01:08:57 -050028namespace
29{
30enum
31{
32 INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024
33};
34// This has to be at least 4k or else it fails on ATI cards.
35enum
36{
37 CONSTANT_VERTEX_BUFFER_SIZE = 4096
38};
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039
Jiawei-Shao2597fb62016-12-09 16:38:02 +080040// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
41int ElementsInBuffer(const gl::VertexAttribute &attrib,
42 const gl::VertexBinding &binding,
43 unsigned int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000044{
Geoff Langa36ead42013-08-02 11:54:08 -040045 // Size cannot be larger than a GLsizei
46 if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
47 {
48 size = static_cast<unsigned int>(std::numeric_limits<int>::max());
49 }
50
Jiawei-Shao2597fb62016-12-09 16:38:02 +080051 GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib, binding));
52 GLsizei offset = static_cast<GLsizei>(ComputeVertexAttributeOffset(attrib, binding));
53 return (size - offset % stride +
Cooper Partin4d61f7e2015-08-12 10:56:50 -070054 (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) /
55 stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000056}
57
Jiawei-Shao2597fb62016-12-09 16:38:02 +080058// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
59bool DirectStoragePossible(const gl::VertexAttribute &attrib, const gl::VertexBinding &binding)
Jamie Madilldbfc6c62016-02-29 01:08:57 -050060{
61 // Current value attribs may not use direct storage.
62 if (!attrib.enabled)
63 {
64 return false;
65 }
66
Martin Radevdd5f27e2017-06-07 10:17:09 +030067 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madilldbfc6c62016-02-29 01:08:57 -050068 if (!buffer)
69 {
70 return false;
71 }
72
73 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
74 ASSERT(bufferD3D);
75 if (!bufferD3D->supportsDirectBinding())
76 {
77 return false;
78 }
79
Jamie Madilldbfc6c62016-02-29 01:08:57 -050080 // Alignment restrictions: In D3D, vertex data must be aligned to the format stride, or to a
81 // 4-byte boundary, whichever is smaller. (Undocumented, and experimentally confirmed)
Jamie Madilld8fa9212016-03-02 11:51:43 -050082 size_t alignment = 4;
Jamie Madilldbfc6c62016-02-29 01:08:57 -050083
Jamie Madille16a4512017-03-07 20:46:40 -050084 // TODO(jmadill): add VertexFormatCaps
85 BufferFactoryD3D *factory = bufferD3D->getFactory();
86
87 gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib);
88
89 // CPU-converted vertex data must be converted (naturally).
90 if ((factory->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_CPU) != 0)
91 {
92 return false;
93 }
94
Jamie Madilldbfc6c62016-02-29 01:08:57 -050095 if (attrib.type != GL_FLOAT)
96 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +080097 auto errorOrElementSize = factory->getVertexSpaceRequired(attrib, binding, 1, 0);
Jamie Madilld8fa9212016-03-02 11:51:43 -050098 if (errorOrElementSize.isError())
99 {
Yuly Novikovd73f8522017-01-13 17:48:57 -0500100 ERR() << "Unlogged error in DirectStoragePossible.";
Jamie Madilld8fa9212016-03-02 11:51:43 -0500101 return false;
102 }
103
104 alignment = std::min<size_t>(errorOrElementSize.getResult(), 4);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500105 }
106
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800107 GLintptr offset = ComputeVertexAttributeOffset(attrib, binding);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500108 // Final alignment check - unaligned data must be converted.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800109 return (static_cast<size_t>(ComputeVertexAttributeStride(attrib, binding)) % alignment == 0) &&
110 (static_cast<size_t>(offset) % alignment == 0);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500111}
112} // anonymous namespace
113
Jamie Madille36b92d2016-03-04 15:46:58 -0500114TranslatedAttribute::TranslatedAttribute()
115 : active(false),
116 attribute(nullptr),
Jamie Madill401345e2017-08-21 10:52:40 -0400117 binding(nullptr),
Jamie Madille36b92d2016-03-04 15:46:58 -0500118 currentValueType(GL_NONE),
Jamie Madill52b09c22016-04-11 14:12:31 -0400119 baseOffset(0),
120 usesFirstVertexOffset(false),
Jamie Madille36b92d2016-03-04 15:46:58 -0500121 stride(0),
122 vertexBuffer(),
123 storage(nullptr),
124 serial(0),
125 divisor(0)
126{
127}
128
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500129TranslatedAttribute::TranslatedAttribute(const TranslatedAttribute &other) = default;
130
Jamie Madill02b53282018-07-24 11:06:47 -0400131gl::Error TranslatedAttribute::computeOffset(GLint startVertex, unsigned int *offsetOut) const
Jamie Madill52b09c22016-04-11 14:12:31 -0400132{
Jamie Madille2e406c2016-06-02 13:04:10 -0400133 if (!usesFirstVertexOffset)
Jamie Madill52b09c22016-04-11 14:12:31 -0400134 {
Jamie Madill02b53282018-07-24 11:06:47 -0400135 *offsetOut = baseOffset;
136 return gl::NoError();
Jamie Madill52b09c22016-04-11 14:12:31 -0400137 }
Jamie Madille2e406c2016-06-02 13:04:10 -0400138
Jamie Madill02b53282018-07-24 11:06:47 -0400139 CheckedNumeric<unsigned int> offset(baseOffset);
140 CheckedNumeric<unsigned int> checkedStride(stride);
Jamie Madille2e406c2016-06-02 13:04:10 -0400141
Jamie Madill02b53282018-07-24 11:06:47 -0400142 offset += checkedStride * static_cast<unsigned int>(startVertex);
Jamie Madillca2ff382018-07-11 09:01:17 -0400143 ANGLE_TRY_CHECKED_MATH(offset.IsValid());
Jamie Madill02b53282018-07-24 11:06:47 -0400144 *offsetOut = offset.ValueOrDie();
145 return gl::NoError();
Jamie Madill52b09c22016-04-11 14:12:31 -0400146}
147
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800148// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
149VertexStorageType ClassifyAttributeStorage(const gl::VertexAttribute &attrib,
150 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.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800166 if (DirectStoragePossible(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 Madill401345e2017-08-21 10:52:40 -0400186VertexDataManager::CurrentValueState::CurrentValueState() : buffer(), offset(0)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000187{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400188 data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
189 data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
190 data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
191 data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
192 data.Type = GL_FLOAT;
193}
daniel@transgaming.com83921382011-01-08 05:46:00 +0000194
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400195VertexDataManager::CurrentValueState::~CurrentValueState()
196{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400197}
198
199VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
Jamie Madill401345e2017-08-21 10:52:40 -0400200 : mFactory(factory), mStreamingBuffer(), mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS)
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400201{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000202}
203
204VertexDataManager::~VertexDataManager()
205{
Jamie Madill401345e2017-08-21 10:52:40 -0400206}
207
208gl::Error VertexDataManager::initialize()
209{
210 mStreamingBuffer.reset(
211 new StreamingVertexBufferInterface(mFactory, INITIAL_STREAM_BUFFER_SIZE));
Jamie Madilldec86232018-07-11 09:01:18 -0400212 ANGLE_TRY_ALLOCATION(mStreamingBuffer);
Jamie Madill401345e2017-08-21 10:52:40 -0400213 return gl::NoError();
214}
215
216void VertexDataManager::deinitialize()
217{
218 mStreamingBuffer.reset();
219 mCurrentValueCache.clear();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000220}
221
Jamie Madill33510102017-09-20 10:39:18 -0400222gl::Error VertexDataManager::prepareVertexData(const gl::Context *context,
Jamie Madill476682e2015-06-30 10:04:29 -0400223 GLint start,
224 GLsizei count,
225 std::vector<TranslatedAttribute> *translatedAttribs,
226 GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000227{
Jamie Madille18eb972016-03-04 15:46:59 -0500228 ASSERT(mStreamingBuffer);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000229
Jamie Madill33510102017-09-20 10:39:18 -0400230 const gl::State &state = context->getGLState();
Geoff Lang5ead9272015-03-25 12:27:43 -0400231 const gl::VertexArray *vertexArray = state.getVertexArray();
Jamie Madill63805b42015-08-25 13:17:39 -0400232 const auto &vertexAttributes = vertexArray->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800233 const auto &vertexBindings = vertexArray->getVertexBindings();
Geoff Lang5ead9272015-03-25 12:27:43 -0400234
Jamie Madill52b09c22016-04-11 14:12:31 -0400235 mDynamicAttribsMaskCache.reset();
Jamie Madille18eb972016-03-04 15:46:59 -0500236 const gl::Program *program = state.getProgram();
237
Jamie Madill476682e2015-06-30 10:04:29 -0400238 translatedAttribs->clear();
Jamie Madill27c08912015-06-22 13:57:20 -0400239
Jamie Madill9c385802015-06-22 13:57:18 -0400240 for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000241 {
Jamie Madille18eb972016-03-04 15:46:59 -0500242 // Skip attrib locations the program doesn't use.
243 if (!program->isAttribLocationActive(attribIndex))
244 continue;
245
246 const auto &attrib = vertexAttributes[attribIndex];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800247 const auto &binding = vertexBindings[attrib.bindingIndex];
Jamie Madille18eb972016-03-04 15:46:59 -0500248
249 // Resize automatically puts in empty attribs
250 translatedAttribs->resize(attribIndex + 1);
251
252 TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
Jamie Madill6de51852017-04-12 09:53:01 -0400253 auto currentValueData = state.getVertexAttribCurrentValue(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500254
255 // Record the attribute now
256 translated->active = true;
257 translated->attribute = &attrib;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800258 translated->binding = &binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500259 translated->currentValueType = currentValueData.Type;
Martin Radevdd5f27e2017-06-07 10:17:09 +0300260 translated->divisor = binding.getDivisor();
Jamie Madille18eb972016-03-04 15:46:59 -0500261
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800262 switch (ClassifyAttributeStorage(attrib, binding))
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000263 {
Jamie Madille18eb972016-03-04 15:46:59 -0500264 case VertexStorageType::STATIC:
Jamie Madill9c385802015-06-22 13:57:18 -0400265 {
Jamie Madille18eb972016-03-04 15:46:59 -0500266 // Store static attribute.
Jamie Madill33510102017-09-20 10:39:18 -0400267 ANGLE_TRY(StoreStaticAttrib(context, translated));
Jamie Madille18eb972016-03-04 15:46:59 -0500268 break;
Jamie Madill9c385802015-06-22 13:57:18 -0400269 }
Jamie Madille18eb972016-03-04 15:46:59 -0500270 case VertexStorageType::DYNAMIC:
271 // Dynamic attributes must be handled together.
Jamie Madill52b09c22016-04-11 14:12:31 -0400272 mDynamicAttribsMaskCache.set(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500273 break;
274 case VertexStorageType::DIRECT:
275 // Update translated data for direct attributes.
Jamie Madill52b09c22016-04-11 14:12:31 -0400276 StoreDirectAttrib(translated);
Jamie Madille18eb972016-03-04 15:46:59 -0500277 break;
278 case VertexStorageType::CURRENT_VALUE:
Jamie Madill27c08912015-06-22 13:57:20 -0400279 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400280 ANGLE_TRY(storeCurrentValue(currentValueData, translated, attribIndex));
Jamie Madille18eb972016-03-04 15:46:59 -0500281 break;
Jamie Madill27c08912015-06-22 13:57:20 -0400282 }
Jamie Madille18eb972016-03-04 15:46:59 -0500283 default:
284 UNREACHABLE();
285 break;
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000286 }
287 }
288
Jamie Madill52b09c22016-04-11 14:12:31 -0400289 if (mDynamicAttribsMaskCache.none())
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000290 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400291 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500292 }
293
Jamie Madill33510102017-09-20 10:39:18 -0400294 ANGLE_TRY(storeDynamicAttribs(context, translatedAttribs, mDynamicAttribsMaskCache, start,
295 count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400296
Jamie Madill33510102017-09-20 10:39:18 -0400297 PromoteDynamicAttribs(context, *translatedAttribs, mDynamicAttribsMaskCache, count);
Jamie Madill52b09c22016-04-11 14:12:31 -0400298
299 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500300}
301
302// static
Jamie Madill52b09c22016-04-11 14:12:31 -0400303void VertexDataManager::StoreDirectAttrib(TranslatedAttribute *directAttrib)
Jamie Madille18eb972016-03-04 15:46:59 -0500304{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800305 ASSERT(directAttrib->attribute && directAttrib->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800306 const auto &attrib = *directAttrib->attribute;
307 const auto &binding = *directAttrib->binding;
308
Martin Radevdd5f27e2017-06-07 10:17:09 +0300309 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill7aa786c2017-09-19 13:57:15 -0400310 ASSERT(buffer);
311 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jamie Madille18eb972016-03-04 15:46:59 -0500312
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800313 ASSERT(DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500314 directAttrib->vertexBuffer.set(nullptr);
315 directAttrib->storage = bufferD3D;
316 directAttrib->serial = bufferD3D->getSerial();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800317 directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding));
318 directAttrib->baseOffset =
319 static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill52b09c22016-04-11 14:12:31 -0400320
321 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300322 directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madille18eb972016-03-04 15:46:59 -0500323}
324
325// static
Jamie Madill33510102017-09-20 10:39:18 -0400326gl::Error VertexDataManager::StoreStaticAttrib(const gl::Context *context,
327 TranslatedAttribute *translated)
Jamie Madille18eb972016-03-04 15:46:59 -0500328{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800329 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800330 const auto &attrib = *translated->attribute;
331 const auto &binding = *translated->binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500332
Martin Radevdd5f27e2017-06-07 10:17:09 +0300333 gl::Buffer *buffer = binding.getBuffer().get();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800334 ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500335 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
336
Jamie Madille18eb972016-03-04 15:46:59 -0500337 // Compute source data pointer
338 const uint8_t *sourceData = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800339 const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500340
Jamie Madill33510102017-09-20 10:39:18 -0400341 ANGLE_TRY(bufferD3D->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800342 sourceData += offset;
Jamie Madille18eb972016-03-04 15:46:59 -0500343
344 unsigned int streamOffset = 0;
345
Jamie Madille18eb972016-03-04 15:46:59 -0500346 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800347 ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, binding, 1, 0),
Jamie Madill52b09c22016-04-11 14:12:31 -0400348 translated->stride);
Jamie Madille18eb972016-03-04 15:46:59 -0500349
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800350 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
Jamie Madille18eb972016-03-04 15:46:59 -0500351 ASSERT(staticBuffer);
352
353 if (staticBuffer->empty())
354 {
355 // Convert the entire buffer
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800356 int totalCount =
357 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
358 int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500359
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800360 ANGLE_TRY(staticBuffer->storeStaticAttribute(attrib, binding, -startIndex, totalCount, 0,
361 sourceData));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000362 }
363
Jamie Madille18eb972016-03-04 15:46:59 -0500364 unsigned int firstElementOffset =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800365 (static_cast<unsigned int>(offset) /
366 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
Jamie Madille18eb972016-03-04 15:46:59 -0500367 translated->stride;
Jamie Madill27c08912015-06-22 13:57:20 -0400368
Jamie Madille18eb972016-03-04 15:46:59 -0500369 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
370
Jamie Madille2e406c2016-06-02 13:04:10 -0400371 CheckedNumeric<unsigned int> checkedOffset(streamOffset);
372 checkedOffset += firstElementOffset;
373
374 if (!checkedOffset.IsValid())
Jamie Madill52b09c22016-04-11 14:12:31 -0400375 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500376 return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib";
Jamie Madill52b09c22016-04-11 14:12:31 -0400377 }
378
Jamie Madille18eb972016-03-04 15:46:59 -0500379 translated->vertexBuffer.set(vertexBuffer);
380 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400381 translated->baseOffset = streamOffset + firstElementOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500382
Jamie Madill52b09c22016-04-11 14:12:31 -0400383 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300384 translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madill52b09c22016-04-11 14:12:31 -0400385
386 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500387}
388
389gl::Error VertexDataManager::storeDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400390 const gl::Context *context,
Jamie Madille18eb972016-03-04 15:46:59 -0500391 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill52b09c22016-04-11 14:12:31 -0400392 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madille18eb972016-03-04 15:46:59 -0500393 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400394 size_t count,
Jamie Madille18eb972016-03-04 15:46:59 -0500395 GLsizei instances)
396{
Jamie Madill52b09c22016-04-11 14:12:31 -0400397 // Instantiating this class will ensure the streaming buffer is never left mapped.
Jamie Madille2e406c2016-06-02 13:04:10 -0400398 class StreamingBufferUnmapper final : NonCopyable
Jamie Madill52b09c22016-04-11 14:12:31 -0400399 {
400 public:
401 StreamingBufferUnmapper(StreamingVertexBufferInterface *streamingBuffer)
402 : mStreamingBuffer(streamingBuffer)
403 {
404 ASSERT(mStreamingBuffer);
405 }
406 ~StreamingBufferUnmapper() { mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); }
407
408 private:
409 StreamingVertexBufferInterface *mStreamingBuffer;
410 };
411
412 // Will trigger unmapping on return.
Jamie Madill401345e2017-08-21 10:52:40 -0400413 StreamingBufferUnmapper localUnmapper(mStreamingBuffer.get());
Jamie Madill52b09c22016-04-11 14:12:31 -0400414
Jamie Madille18eb972016-03-04 15:46:59 -0500415 // Reserve the required space for the dynamic buffers.
Jamie Madill6de51852017-04-12 09:53:01 -0400416 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500417 {
418 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800419 ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, start, count, instances));
Jamie Madille18eb972016-03-04 15:46:59 -0500420 }
421
422 // Store dynamic attributes
Jamie Madill6de51852017-04-12 09:53:01 -0400423 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500424 {
425 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill33510102017-09-20 10:39:18 -0400426 ANGLE_TRY(storeDynamicAttrib(context, dynamicAttrib, start, count, instances));
Jamie Madill52b09c22016-04-11 14:12:31 -0400427 }
Shannon Woods1a965482014-09-22 18:00:32 -0400428
Jamie Madill52b09c22016-04-11 14:12:31 -0400429 return gl::NoError();
430}
431
432void VertexDataManager::PromoteDynamicAttribs(
Jamie Madill33510102017-09-20 10:39:18 -0400433 const gl::Context *context,
Jamie Madill52b09c22016-04-11 14:12:31 -0400434 const std::vector<TranslatedAttribute> &translatedAttribs,
435 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madill18e323a2018-05-11 16:54:17 -0400436 size_t count)
Jamie Madill52b09c22016-04-11 14:12:31 -0400437{
Jamie Madill6de51852017-04-12 09:53:01 -0400438 for (auto attribIndex : dynamicAttribsMask)
Jamie Madill52b09c22016-04-11 14:12:31 -0400439 {
440 const auto &dynamicAttrib = translatedAttribs[attribIndex];
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800441 ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800442 const auto &binding = *dynamicAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800443
Martin Radevdd5f27e2017-06-07 10:17:09 +0300444 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill27c08912015-06-22 13:57:20 -0400445 if (buffer)
446 {
Jamie Madill18e323a2018-05-11 16:54:17 -0400447 // Note: this multiplication can overflow. It should not be a security problem.
Jamie Madill27c08912015-06-22 13:57:20 -0400448 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800449 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
Jamie Madill18e323a2018-05-11 16:54:17 -0400450 bufferD3D->promoteStaticUsage(context, count * typeSize);
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000451 }
452 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000453}
454
Jamie Madill3d72cc72015-06-22 13:57:19 -0400455gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800456 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400457 size_t count,
Geoff Langf7100b92014-09-08 16:17:08 -0400458 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400459{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800460 ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800461 const auto &attrib = *translatedAttrib.attribute;
462 const auto &binding = *translatedAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800463
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800464 ASSERT(!DirectStoragePossible(attrib, binding));
Jamie Madill6d113802014-08-25 15:47:52 -0400465
Martin Radevdd5f27e2017-06-07 10:17:09 +0300466 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500467 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800468 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
Jamie Madille36b92d2016-03-04 15:46:58 -0500469
Jamie Madill18e323a2018-05-11 16:54:17 -0400470 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
471 static_cast<size_t>(instances));
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800472 // TODO(jiajia.qin@intel.com): force the index buffer to clamp any out of range indices instead
473 // of invalid operation here.
474 if (bufferD3D)
475 {
476 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
477 // a non-instanced draw call
478 GLint firstVertexIndex = binding.getDivisor() > 0 ? 0 : start;
479 int64_t maxVertexCount =
480 static_cast<int64_t>(firstVertexIndex) + static_cast<int64_t>(totalCount);
481 int elementsInBuffer =
482 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
Jamie Madille36b92d2016-03-04 15:46:58 -0500483
Jiajia Qin8a7b3a02017-08-25 16:05:48 +0800484 if (maxVertexCount > elementsInBuffer)
485 {
486 return gl::InvalidOperation() << "Vertex buffer is not big enough for the draw call.";
487 }
488 }
Jamie Madill18e323a2018-05-11 16:54:17 -0400489 return mStreamingBuffer->reserveVertexSpace(attrib, binding, totalCount, instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400490}
491
Jamie Madill33510102017-09-20 10:39:18 -0400492gl::Error VertexDataManager::storeDynamicAttrib(const gl::Context *context,
493 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500494 GLint start,
Jamie Madill18e323a2018-05-11 16:54:17 -0400495 size_t count,
496 GLsizei instances) const
Jamie Madillf41522b2014-08-18 16:39:49 -0400497{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800498 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800499 const auto &attrib = *translated->attribute;
500 const auto &binding = *translated->binding;
Jamie Madill9c385802015-06-22 13:57:18 -0400501
Martin Radevdd5f27e2017-06-07 10:17:09 +0300502 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -0400503 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400504 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400505
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500506 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400507
Jamie Madilld4b55a02015-01-09 14:21:49 -0500508 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300509 GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500510
Jamie Madill7d112bb2015-06-22 13:57:19 -0400511 // Compute source data pointer
512 const uint8_t *sourceData = nullptr;
513
514 if (buffer)
515 {
Jamie Madill33510102017-09-20 10:39:18 -0400516 ANGLE_TRY(storage->getData(context, &sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800517 sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400518 }
519 else
520 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800521 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
522 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill7d112bb2015-06-22 13:57:19 -0400523 sourceData = static_cast<const uint8_t*>(attrib.pointer);
524 }
525
526 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500527
Jamie Madilld8fa9212016-03-02 11:51:43 -0500528 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800529 ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, binding, 1, 0), translated->stride);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500530
Jamie Madill18e323a2018-05-11 16:54:17 -0400531 size_t totalCount = gl::ComputeVertexBindingElementCount(binding.getDivisor(), count,
532 static_cast<size_t>(instances));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400533
Jamie Madill52b09c22016-04-11 14:12:31 -0400534 ANGLE_TRY(mStreamingBuffer->storeDynamicAttribute(
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800535 attrib, binding, translated->currentValueType, firstVertexIndex,
536 static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400537
Jamie Madille18eb972016-03-04 15:46:59 -0500538 VertexBuffer *vertexBuffer = mStreamingBuffer->getVertexBuffer();
539
540 translated->vertexBuffer.set(vertexBuffer);
541 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400542 translated->baseOffset = streamOffset;
543 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400544
Jamie Madill52b09c22016-04-11 14:12:31 -0400545 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400546}
547
Jamie Madill9c385802015-06-22 13:57:18 -0400548gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400549 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500550 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400551{
Jamie Madille18eb972016-03-04 15:46:59 -0500552 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
Jamie Madill401345e2017-08-21 10:52:40 -0400553 auto &buffer = cachedState->buffer;
Jamie Madille18eb972016-03-04 15:46:59 -0500554
555 if (!buffer)
556 {
Jamie Madill401345e2017-08-21 10:52:40 -0400557 buffer.reset(new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE));
Jamie Madille18eb972016-03-04 15:46:59 -0500558 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500559
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400560 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400561 {
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800562 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800563 const auto &attrib = *translated->attribute;
564 const auto &binding = *translated->binding;
Jamie Madill27c08912015-06-22 13:57:20 -0400565
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800566 ANGLE_TRY(buffer->reserveVertexSpace(attrib, binding, 1, 0));
Jamie Madillf41522b2014-08-18 16:39:49 -0400567
Jamie Madill7d112bb2015-06-22 13:57:19 -0400568 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400569 unsigned int streamOffset;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800570 ANGLE_TRY(buffer->storeDynamicAttribute(attrib, binding, currentValue.Type, 0, 1, 0,
571 &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400572
Jamie Madille36b92d2016-03-04 15:46:58 -0500573 buffer->getVertexBuffer()->hintUnmapResource();
574
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400575 cachedState->data = currentValue;
576 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400577 }
578
Jamie Madille36b92d2016-03-04 15:46:58 -0500579 translated->vertexBuffer.set(buffer->getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400580
Jamie Madille36b92d2016-03-04 15:46:58 -0500581 translated->storage = nullptr;
582 translated->serial = buffer->getSerial();
583 translated->divisor = 0;
584 translated->stride = 0;
Jamie Madill52b09c22016-04-11 14:12:31 -0400585 translated->baseOffset = static_cast<unsigned int>(cachedState->offset);
586 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400587
Jamie Madill52b09c22016-04-11 14:12:31 -0400588 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400589}
590
Jamie Madille36b92d2016-03-04 15:46:58 -0500591// VertexBufferBinding implementation
592VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
593{
594}
595
596VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
597 : mBoundVertexBuffer(other.mBoundVertexBuffer)
598{
599 if (mBoundVertexBuffer)
600 {
601 mBoundVertexBuffer->addRef();
602 }
603}
604
605VertexBufferBinding::~VertexBufferBinding()
606{
607 if (mBoundVertexBuffer)
608 {
609 mBoundVertexBuffer->release();
610 }
611}
612
613VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
614{
615 mBoundVertexBuffer = other.mBoundVertexBuffer;
616 if (mBoundVertexBuffer)
617 {
618 mBoundVertexBuffer->addRef();
619 }
620 return *this;
621}
622
623void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
624{
625 if (mBoundVertexBuffer == vertexBuffer)
626 return;
627
628 if (mBoundVertexBuffer)
629 {
630 mBoundVertexBuffer->release();
631 }
632 if (vertexBuffer)
633 {
634 vertexBuffer->addRef();
635 }
636
637 mBoundVertexBuffer = vertexBuffer;
638}
639
640VertexBuffer *VertexBufferBinding::get() const
641{
642 return mBoundVertexBuffer;
643}
644
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500645} // namespace rx