blob: 46592e5478a9071d0277fd77b57541cf21d759a6 [file] [log] [blame]
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +00007// VertexDataManager.h: Defines the VertexDataManager, a class that
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00008// runs the Buffer translation process.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/renderer/d3d/VertexDataManager.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040011
Jamie Madill20e005b2017-04-07 14:19:22 -040012#include "common/bitset_utils.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Buffer.h"
Jamie Madilldbfc6c62016-02-29 01:08:57 -050014#include "libANGLE/formatutils.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050015#include "libANGLE/Program.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040016#include "libANGLE/State.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050017#include "libANGLE/VertexAttribute.h"
Geoff Lang5ead9272015-03-25 12:27:43 -040018#include "libANGLE/VertexArray.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040019#include "libANGLE/renderer/d3d/BufferD3D.h"
Jamie Madilldbfc6c62016-02-29 01:08:57 -050020#include "libANGLE/renderer/d3d/RendererD3D.h"
Jamie Madillfd1bf4e2015-03-31 09:46:02 -040021#include "libANGLE/renderer/d3d/VertexBuffer.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000022
Jamie Madille2e406c2016-06-02 13:04:10 -040023using namespace angle;
24
daniel@transgaming.com31240482012-11-28 21:06:41 +000025namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000026{
Jamie Madilldbfc6c62016-02-29 01:08:57 -050027namespace
28{
29enum
30{
31 INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024
32};
33// This has to be at least 4k or else it fails on ATI cards.
34enum
35{
36 CONSTANT_VERTEX_BUFFER_SIZE = 4096
37};
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000038
Jiawei-Shao2597fb62016-12-09 16:38:02 +080039// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
40int ElementsInBuffer(const gl::VertexAttribute &attrib,
41 const gl::VertexBinding &binding,
42 unsigned int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000043{
Geoff Langa36ead42013-08-02 11:54:08 -040044 // Size cannot be larger than a GLsizei
45 if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
46 {
47 size = static_cast<unsigned int>(std::numeric_limits<int>::max());
48 }
49
Jiawei-Shao2597fb62016-12-09 16:38:02 +080050 GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib, binding));
51 GLsizei offset = static_cast<GLsizei>(ComputeVertexAttributeOffset(attrib, binding));
52 return (size - offset % stride +
Cooper Partin4d61f7e2015-08-12 10:56:50 -070053 (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) /
54 stride;
jbauman@chromium.org059fc152011-11-18 19:26:17 +000055}
56
Jiawei-Shao2597fb62016-12-09 16:38:02 +080057// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
58bool DirectStoragePossible(const gl::VertexAttribute &attrib, const gl::VertexBinding &binding)
Jamie Madilldbfc6c62016-02-29 01:08:57 -050059{
60 // Current value attribs may not use direct storage.
61 if (!attrib.enabled)
62 {
63 return false;
64 }
65
Martin Radevdd5f27e2017-06-07 10:17:09 +030066 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madilldbfc6c62016-02-29 01:08:57 -050067 if (!buffer)
68 {
69 return false;
70 }
71
72 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
73 ASSERT(bufferD3D);
74 if (!bufferD3D->supportsDirectBinding())
75 {
76 return false;
77 }
78
Jamie Madilldbfc6c62016-02-29 01:08:57 -050079 // Alignment restrictions: In D3D, vertex data must be aligned to the format stride, or to a
80 // 4-byte boundary, whichever is smaller. (Undocumented, and experimentally confirmed)
Jamie Madilld8fa9212016-03-02 11:51:43 -050081 size_t alignment = 4;
Jamie Madilldbfc6c62016-02-29 01:08:57 -050082
Jamie Madille16a4512017-03-07 20:46:40 -050083 // TODO(jmadill): add VertexFormatCaps
84 BufferFactoryD3D *factory = bufferD3D->getFactory();
85
86 gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib);
87
88 // CPU-converted vertex data must be converted (naturally).
89 if ((factory->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_CPU) != 0)
90 {
91 return false;
92 }
93
Jamie Madilldbfc6c62016-02-29 01:08:57 -050094 if (attrib.type != GL_FLOAT)
95 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +080096 auto errorOrElementSize = factory->getVertexSpaceRequired(attrib, binding, 1, 0);
Jamie Madilld8fa9212016-03-02 11:51:43 -050097 if (errorOrElementSize.isError())
98 {
Yuly Novikovd73f8522017-01-13 17:48:57 -050099 ERR() << "Unlogged error in DirectStoragePossible.";
Jamie Madilld8fa9212016-03-02 11:51:43 -0500100 return false;
101 }
102
103 alignment = std::min<size_t>(errorOrElementSize.getResult(), 4);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500104 }
105
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800106 GLintptr offset = ComputeVertexAttributeOffset(attrib, binding);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500107 // Final alignment check - unaligned data must be converted.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800108 return (static_cast<size_t>(ComputeVertexAttributeStride(attrib, binding)) % alignment == 0) &&
109 (static_cast<size_t>(offset) % alignment == 0);
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500110}
111} // anonymous namespace
112
Jamie Madille36b92d2016-03-04 15:46:58 -0500113TranslatedAttribute::TranslatedAttribute()
114 : active(false),
115 attribute(nullptr),
116 currentValueType(GL_NONE),
Jamie Madill52b09c22016-04-11 14:12:31 -0400117 baseOffset(0),
118 usesFirstVertexOffset(false),
Jamie Madille36b92d2016-03-04 15:46:58 -0500119 stride(0),
120 vertexBuffer(),
121 storage(nullptr),
122 serial(0),
123 divisor(0)
124{
125}
126
Jamie Madill52b09c22016-04-11 14:12:31 -0400127gl::ErrorOrResult<unsigned int> TranslatedAttribute::computeOffset(GLint startVertex) const
128{
Jamie Madille2e406c2016-06-02 13:04:10 -0400129 if (!usesFirstVertexOffset)
Jamie Madill52b09c22016-04-11 14:12:31 -0400130 {
Jamie Madille2e406c2016-06-02 13:04:10 -0400131 return baseOffset;
Jamie Madill52b09c22016-04-11 14:12:31 -0400132 }
Jamie Madille2e406c2016-06-02 13:04:10 -0400133
134 CheckedNumeric<unsigned int> offset;
135
136 offset = baseOffset + stride * static_cast<unsigned int>(startVertex);
137 ANGLE_TRY_CHECKED_MATH(offset);
138 return offset.ValueOrDie();
Jamie Madill52b09c22016-04-11 14:12:31 -0400139}
140
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800141// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
142VertexStorageType ClassifyAttributeStorage(const gl::VertexAttribute &attrib,
143 const gl::VertexBinding &binding)
Jamie Madille18eb972016-03-04 15:46:59 -0500144{
145 // If attribute is disabled, we use the current value.
146 if (!attrib.enabled)
147 {
148 return VertexStorageType::CURRENT_VALUE;
149 }
150
151 // If specified with immediate data, we must use dynamic storage.
Martin Radevdd5f27e2017-06-07 10:17:09 +0300152 auto *buffer = binding.getBuffer().get();
Jamie Madille18eb972016-03-04 15:46:59 -0500153 if (!buffer)
154 {
155 return VertexStorageType::DYNAMIC;
156 }
157
158 // Check if the buffer supports direct storage.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800159 if (DirectStoragePossible(attrib, binding))
Jamie Madille18eb972016-03-04 15:46:59 -0500160 {
161 return VertexStorageType::DIRECT;
162 }
163
164 // Otherwise the storage is static or dynamic.
165 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
166 ASSERT(bufferD3D);
167 switch (bufferD3D->getUsage())
168 {
169 case D3DBufferUsage::DYNAMIC:
170 return VertexStorageType::DYNAMIC;
171 case D3DBufferUsage::STATIC:
172 return VertexStorageType::STATIC;
173 default:
174 UNREACHABLE();
175 return VertexStorageType::UNKNOWN;
176 }
177}
178
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400179VertexDataManager::CurrentValueState::CurrentValueState()
180 : buffer(nullptr),
181 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{
192 SafeDelete(buffer);
193}
194
195VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
196 : mFactory(factory),
197 mStreamingBuffer(nullptr),
198 // TODO(jmadill): use context caps
199 mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS)
200{
Jamie Madillfd1bf4e2015-03-31 09:46:02 -0400201 mStreamingBuffer = new StreamingVertexBufferInterface(factory, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000202
203 if (!mStreamingBuffer)
204 {
Yuly Novikovd73f8522017-01-13 17:48:57 -0500205 ERR() << "Failed to allocate the streaming vertex buffer.";
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000206 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000207}
208
209VertexDataManager::~VertexDataManager()
210{
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400211 SafeDelete(mStreamingBuffer);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000212}
213
Jamie Madill476682e2015-06-30 10:04:29 -0400214gl::Error VertexDataManager::prepareVertexData(const gl::State &state,
215 GLint start,
216 GLsizei count,
217 std::vector<TranslatedAttribute> *translatedAttribs,
218 GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000219{
Jamie Madille18eb972016-03-04 15:46:59 -0500220 ASSERT(mStreamingBuffer);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000221
Geoff Lang5ead9272015-03-25 12:27:43 -0400222 const gl::VertexArray *vertexArray = state.getVertexArray();
Jamie Madill63805b42015-08-25 13:17:39 -0400223 const auto &vertexAttributes = vertexArray->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800224 const auto &vertexBindings = vertexArray->getVertexBindings();
Geoff Lang5ead9272015-03-25 12:27:43 -0400225
Jamie Madill52b09c22016-04-11 14:12:31 -0400226 mDynamicAttribsMaskCache.reset();
Jamie Madille18eb972016-03-04 15:46:59 -0500227 const gl::Program *program = state.getProgram();
228
Jamie Madill476682e2015-06-30 10:04:29 -0400229 translatedAttribs->clear();
Jamie Madill27c08912015-06-22 13:57:20 -0400230
Jamie Madill9c385802015-06-22 13:57:18 -0400231 for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000232 {
Jamie Madille18eb972016-03-04 15:46:59 -0500233 // Skip attrib locations the program doesn't use.
234 if (!program->isAttribLocationActive(attribIndex))
235 continue;
236
237 const auto &attrib = vertexAttributes[attribIndex];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800238 const auto &binding = vertexBindings[attrib.bindingIndex];
Jamie Madille18eb972016-03-04 15:46:59 -0500239
240 // Resize automatically puts in empty attribs
241 translatedAttribs->resize(attribIndex + 1);
242
243 TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
Jamie Madill6de51852017-04-12 09:53:01 -0400244 auto currentValueData = state.getVertexAttribCurrentValue(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500245
246 // Record the attribute now
247 translated->active = true;
248 translated->attribute = &attrib;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800249 translated->binding = &binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500250 translated->currentValueType = currentValueData.Type;
Martin Radevdd5f27e2017-06-07 10:17:09 +0300251 translated->divisor = binding.getDivisor();
Jamie Madille18eb972016-03-04 15:46:59 -0500252
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800253 switch (ClassifyAttributeStorage(attrib, binding))
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000254 {
Jamie Madille18eb972016-03-04 15:46:59 -0500255 case VertexStorageType::STATIC:
Jamie Madill9c385802015-06-22 13:57:18 -0400256 {
Jamie Madille18eb972016-03-04 15:46:59 -0500257 // Store static attribute.
Qin Jiajiacde4f022017-02-21 14:06:02 +0800258 ANGLE_TRY(StoreStaticAttrib(translated));
Jamie Madille18eb972016-03-04 15:46:59 -0500259 break;
Jamie Madill9c385802015-06-22 13:57:18 -0400260 }
Jamie Madille18eb972016-03-04 15:46:59 -0500261 case VertexStorageType::DYNAMIC:
262 // Dynamic attributes must be handled together.
Jamie Madill52b09c22016-04-11 14:12:31 -0400263 mDynamicAttribsMaskCache.set(attribIndex);
Jamie Madille18eb972016-03-04 15:46:59 -0500264 break;
265 case VertexStorageType::DIRECT:
266 // Update translated data for direct attributes.
Jamie Madill52b09c22016-04-11 14:12:31 -0400267 StoreDirectAttrib(translated);
Jamie Madille18eb972016-03-04 15:46:59 -0500268 break;
269 case VertexStorageType::CURRENT_VALUE:
Jamie Madill27c08912015-06-22 13:57:20 -0400270 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400271 ANGLE_TRY(storeCurrentValue(currentValueData, translated, attribIndex));
Jamie Madille18eb972016-03-04 15:46:59 -0500272 break;
Jamie Madill27c08912015-06-22 13:57:20 -0400273 }
Jamie Madille18eb972016-03-04 15:46:59 -0500274 default:
275 UNREACHABLE();
276 break;
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000277 }
278 }
279
Jamie Madill52b09c22016-04-11 14:12:31 -0400280 if (mDynamicAttribsMaskCache.none())
shannon.woods@transgaming.coma9a509e2013-02-28 23:10:44 +0000281 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400282 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500283 }
284
Jamie Madill52b09c22016-04-11 14:12:31 -0400285 ANGLE_TRY(
286 storeDynamicAttribs(translatedAttribs, mDynamicAttribsMaskCache, start, count, instances));
287
288 PromoteDynamicAttribs(*translatedAttribs, mDynamicAttribsMaskCache, count);
289
290 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500291}
292
293// static
Jamie Madill52b09c22016-04-11 14:12:31 -0400294void VertexDataManager::StoreDirectAttrib(TranslatedAttribute *directAttrib)
Jamie Madille18eb972016-03-04 15:46:59 -0500295{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800296 ASSERT(directAttrib->attribute && directAttrib->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800297 const auto &attrib = *directAttrib->attribute;
298 const auto &binding = *directAttrib->binding;
299
Martin Radevdd5f27e2017-06-07 10:17:09 +0300300 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille18eb972016-03-04 15:46:59 -0500301 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
302
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800303 ASSERT(DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500304 directAttrib->vertexBuffer.set(nullptr);
305 directAttrib->storage = bufferD3D;
306 directAttrib->serial = bufferD3D->getSerial();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800307 directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding));
308 directAttrib->baseOffset =
309 static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill52b09c22016-04-11 14:12:31 -0400310
311 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300312 directAttrib->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madille18eb972016-03-04 15:46:59 -0500313}
314
315// static
Qin Jiajiacde4f022017-02-21 14:06:02 +0800316gl::Error VertexDataManager::StoreStaticAttrib(TranslatedAttribute *translated)
Jamie Madille18eb972016-03-04 15:46:59 -0500317{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800318 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800319 const auto &attrib = *translated->attribute;
320 const auto &binding = *translated->binding;
Jamie Madille18eb972016-03-04 15:46:59 -0500321
Martin Radevdd5f27e2017-06-07 10:17:09 +0300322 gl::Buffer *buffer = binding.getBuffer().get();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800323 ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500324 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
325
Jamie Madille18eb972016-03-04 15:46:59 -0500326 // Compute source data pointer
327 const uint8_t *sourceData = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800328 const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500329
Jamie Madill52b09c22016-04-11 14:12:31 -0400330 ANGLE_TRY(bufferD3D->getData(&sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800331 sourceData += offset;
Jamie Madille18eb972016-03-04 15:46:59 -0500332
333 unsigned int streamOffset = 0;
334
Jamie Madille18eb972016-03-04 15:46:59 -0500335 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800336 ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, binding, 1, 0),
Jamie Madill52b09c22016-04-11 14:12:31 -0400337 translated->stride);
Jamie Madille18eb972016-03-04 15:46:59 -0500338
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800339 auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
Jamie Madille18eb972016-03-04 15:46:59 -0500340 ASSERT(staticBuffer);
341
342 if (staticBuffer->empty())
343 {
344 // Convert the entire buffer
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800345 int totalCount =
346 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
347 int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
Jamie Madille18eb972016-03-04 15:46:59 -0500348
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800349 ANGLE_TRY(staticBuffer->storeStaticAttribute(attrib, binding, -startIndex, totalCount, 0,
350 sourceData));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000351 }
352
Jamie Madille18eb972016-03-04 15:46:59 -0500353 unsigned int firstElementOffset =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800354 (static_cast<unsigned int>(offset) /
355 static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
Jamie Madille18eb972016-03-04 15:46:59 -0500356 translated->stride;
Jamie Madill27c08912015-06-22 13:57:20 -0400357
Jamie Madille18eb972016-03-04 15:46:59 -0500358 VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
359
Jamie Madille2e406c2016-06-02 13:04:10 -0400360 CheckedNumeric<unsigned int> checkedOffset(streamOffset);
361 checkedOffset += firstElementOffset;
362
363 if (!checkedOffset.IsValid())
Jamie Madill52b09c22016-04-11 14:12:31 -0400364 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500365 return gl::InternalError() << "Integer overflow in VertexDataManager::StoreStaticAttrib";
Jamie Madill52b09c22016-04-11 14:12:31 -0400366 }
367
Jamie Madille18eb972016-03-04 15:46:59 -0500368 translated->vertexBuffer.set(vertexBuffer);
369 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400370 translated->baseOffset = streamOffset + firstElementOffset;
Jamie Madille18eb972016-03-04 15:46:59 -0500371
Jamie Madill52b09c22016-04-11 14:12:31 -0400372 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300373 translated->usesFirstVertexOffset = (binding.getDivisor() == 0);
Jamie Madill52b09c22016-04-11 14:12:31 -0400374
375 return gl::NoError();
Jamie Madille18eb972016-03-04 15:46:59 -0500376}
377
378gl::Error VertexDataManager::storeDynamicAttribs(
379 std::vector<TranslatedAttribute> *translatedAttribs,
Jamie Madill52b09c22016-04-11 14:12:31 -0400380 const gl::AttributesMask &dynamicAttribsMask,
Jamie Madille18eb972016-03-04 15:46:59 -0500381 GLint start,
382 GLsizei count,
383 GLsizei instances)
384{
Jamie Madill52b09c22016-04-11 14:12:31 -0400385 // Instantiating this class will ensure the streaming buffer is never left mapped.
Jamie Madille2e406c2016-06-02 13:04:10 -0400386 class StreamingBufferUnmapper final : NonCopyable
Jamie Madill52b09c22016-04-11 14:12:31 -0400387 {
388 public:
389 StreamingBufferUnmapper(StreamingVertexBufferInterface *streamingBuffer)
390 : mStreamingBuffer(streamingBuffer)
391 {
392 ASSERT(mStreamingBuffer);
393 }
394 ~StreamingBufferUnmapper() { mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); }
395
396 private:
397 StreamingVertexBufferInterface *mStreamingBuffer;
398 };
399
400 // Will trigger unmapping on return.
401 StreamingBufferUnmapper localUnmapper(mStreamingBuffer);
402
Jamie Madille18eb972016-03-04 15:46:59 -0500403 // Reserve the required space for the dynamic buffers.
Jamie Madill6de51852017-04-12 09:53:01 -0400404 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500405 {
406 const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
Jamie Madill52b09c22016-04-11 14:12:31 -0400407 ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, count, instances));
Jamie Madille18eb972016-03-04 15:46:59 -0500408 }
409
410 // Store dynamic attributes
Jamie Madill6de51852017-04-12 09:53:01 -0400411 for (auto attribIndex : dynamicAttribsMask)
Jamie Madille18eb972016-03-04 15:46:59 -0500412 {
413 auto *dynamicAttrib = &(*translatedAttribs)[attribIndex];
Jamie Madill52b09c22016-04-11 14:12:31 -0400414 ANGLE_TRY(storeDynamicAttrib(dynamicAttrib, start, count, instances));
415 }
Shannon Woods1a965482014-09-22 18:00:32 -0400416
Jamie Madill52b09c22016-04-11 14:12:31 -0400417 return gl::NoError();
418}
419
420void VertexDataManager::PromoteDynamicAttribs(
421 const std::vector<TranslatedAttribute> &translatedAttribs,
422 const gl::AttributesMask &dynamicAttribsMask,
423 GLsizei count)
424{
Jamie Madill6de51852017-04-12 09:53:01 -0400425 for (auto attribIndex : dynamicAttribsMask)
Jamie Madill52b09c22016-04-11 14:12:31 -0400426 {
427 const auto &dynamicAttrib = translatedAttribs[attribIndex];
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800428 ASSERT(dynamicAttrib.attribute && dynamicAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800429 const auto &binding = *dynamicAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800430
Martin Radevdd5f27e2017-06-07 10:17:09 +0300431 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill27c08912015-06-22 13:57:20 -0400432 if (buffer)
433 {
434 BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800435 size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700436 bufferD3D->promoteStaticUsage(count * static_cast<int>(typeSize));
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000437 }
438 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000439}
440
Jamie Madill3d72cc72015-06-22 13:57:19 -0400441gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
Geoff Langf7100b92014-09-08 16:17:08 -0400442 GLsizei count,
443 GLsizei instances) const
Jamie Madill6d113802014-08-25 15:47:52 -0400444{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800445 ASSERT(translatedAttrib.attribute && translatedAttrib.binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800446 const auto &attrib = *translatedAttrib.attribute;
447 const auto &binding = *translatedAttrib.binding;
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800448
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800449 ASSERT(!DirectStoragePossible(attrib, binding));
Jamie Madill6d113802014-08-25 15:47:52 -0400450
Martin Radevdd5f27e2017-06-07 10:17:09 +0300451 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madille36b92d2016-03-04 15:46:58 -0500452 BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800453 ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
Jamie Madille36b92d2016-03-04 15:46:58 -0500454
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800455 size_t totalCount = ComputeVertexBindingElementCount(binding, count, instances);
Jamie Madille36b92d2016-03-04 15:46:58 -0500456 ASSERT(!bufferD3D ||
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800457 ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize())) >=
Jamie Madille36b92d2016-03-04 15:46:58 -0500458 static_cast<int>(totalCount));
459
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800460 return mStreamingBuffer->reserveVertexSpace(attrib, binding, static_cast<GLsizei>(totalCount),
Jamie Madille36b92d2016-03-04 15:46:58 -0500461 instances);
Jamie Madill6d113802014-08-25 15:47:52 -0400462}
463
Jamie Madille18eb972016-03-04 15:46:59 -0500464gl::Error VertexDataManager::storeDynamicAttrib(TranslatedAttribute *translated,
465 GLint start,
466 GLsizei count,
467 GLsizei instances)
Jamie Madillf41522b2014-08-18 16:39:49 -0400468{
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800469 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800470 const auto &attrib = *translated->attribute;
471 const auto &binding = *translated->binding;
Jamie Madill9c385802015-06-22 13:57:18 -0400472
Martin Radevdd5f27e2017-06-07 10:17:09 +0300473 gl::Buffer *buffer = binding.getBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -0400474 ASSERT(buffer || attrib.pointer);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400475 ASSERT(attrib.enabled);
Jamie Madillf41522b2014-08-18 16:39:49 -0400476
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500477 BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
Jamie Madillf41522b2014-08-18 16:39:49 -0400478
Jamie Madilld4b55a02015-01-09 14:21:49 -0500479 // Instanced vertices do not apply the 'start' offset
Martin Radevdd5f27e2017-06-07 10:17:09 +0300480 GLint firstVertexIndex = (binding.getDivisor() > 0 ? 0 : start);
Jamie Madilld4b55a02015-01-09 14:21:49 -0500481
Jamie Madill7d112bb2015-06-22 13:57:19 -0400482 // Compute source data pointer
483 const uint8_t *sourceData = nullptr;
484
485 if (buffer)
486 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400487 ANGLE_TRY(storage->getData(&sourceData));
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800488 sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
Jamie Madill7d112bb2015-06-22 13:57:19 -0400489 }
490 else
491 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800492 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
493 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill7d112bb2015-06-22 13:57:19 -0400494 sourceData = static_cast<const uint8_t*>(attrib.pointer);
495 }
496
497 unsigned int streamOffset = 0;
Jamie Madilld8fa9212016-03-02 11:51:43 -0500498
Jamie Madilld8fa9212016-03-02 11:51:43 -0500499 translated->storage = nullptr;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800500 ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, binding, 1, 0), translated->stride);
Jamie Madilld8fa9212016-03-02 11:51:43 -0500501
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800502 size_t totalCount = ComputeVertexBindingElementCount(binding, count, instances);
Jamie Madill7d112bb2015-06-22 13:57:19 -0400503
Jamie Madill52b09c22016-04-11 14:12:31 -0400504 ANGLE_TRY(mStreamingBuffer->storeDynamicAttribute(
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800505 attrib, binding, translated->currentValueType, firstVertexIndex,
506 static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400507
Jamie Madille18eb972016-03-04 15:46:59 -0500508 VertexBuffer *vertexBuffer = mStreamingBuffer->getVertexBuffer();
509
510 translated->vertexBuffer.set(vertexBuffer);
511 translated->serial = vertexBuffer->getSerial();
Jamie Madill52b09c22016-04-11 14:12:31 -0400512 translated->baseOffset = streamOffset;
513 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400514
Jamie Madill52b09c22016-04-11 14:12:31 -0400515 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400516}
517
Jamie Madill9c385802015-06-22 13:57:18 -0400518gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData &currentValue,
Geoff Langf7100b92014-09-08 16:17:08 -0400519 TranslatedAttribute *translated,
Jamie Madille18eb972016-03-04 15:46:59 -0500520 size_t attribIndex)
Jamie Madillf41522b2014-08-18 16:39:49 -0400521{
Jamie Madille18eb972016-03-04 15:46:59 -0500522 CurrentValueState *cachedState = &mCurrentValueCache[attribIndex];
523 auto *&buffer = cachedState->buffer;
524
525 if (!buffer)
526 {
527 buffer = new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE);
528 }
Jamie Madille36b92d2016-03-04 15:46:58 -0500529
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400530 if (cachedState->data != currentValue)
Jamie Madillf41522b2014-08-18 16:39:49 -0400531 {
Jiawei-Shao995c2ed2017-05-15 13:31:45 +0800532 ASSERT(translated->attribute && translated->binding);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800533 const auto &attrib = *translated->attribute;
534 const auto &binding = *translated->binding;
Jamie Madill27c08912015-06-22 13:57:20 -0400535
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800536 ANGLE_TRY(buffer->reserveVertexSpace(attrib, binding, 1, 0));
Jamie Madillf41522b2014-08-18 16:39:49 -0400537
Jamie Madill7d112bb2015-06-22 13:57:19 -0400538 const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
Jamie Madillf41522b2014-08-18 16:39:49 -0400539 unsigned int streamOffset;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800540 ANGLE_TRY(buffer->storeDynamicAttribute(attrib, binding, currentValue.Type, 0, 1, 0,
541 &streamOffset, sourceData));
Jamie Madillf41522b2014-08-18 16:39:49 -0400542
Jamie Madille36b92d2016-03-04 15:46:58 -0500543 buffer->getVertexBuffer()->hintUnmapResource();
544
Jamie Madillb3f4e8d2015-06-22 13:57:17 -0400545 cachedState->data = currentValue;
546 cachedState->offset = streamOffset;
Jamie Madillf41522b2014-08-18 16:39:49 -0400547 }
548
Jamie Madille36b92d2016-03-04 15:46:58 -0500549 translated->vertexBuffer.set(buffer->getVertexBuffer());
Jamie Madillf41522b2014-08-18 16:39:49 -0400550
Jamie Madille36b92d2016-03-04 15:46:58 -0500551 translated->storage = nullptr;
552 translated->serial = buffer->getSerial();
553 translated->divisor = 0;
554 translated->stride = 0;
Jamie Madill52b09c22016-04-11 14:12:31 -0400555 translated->baseOffset = static_cast<unsigned int>(cachedState->offset);
556 translated->usesFirstVertexOffset = false;
Jamie Madillf41522b2014-08-18 16:39:49 -0400557
Jamie Madill52b09c22016-04-11 14:12:31 -0400558 return gl::NoError();
Jamie Madillf41522b2014-08-18 16:39:49 -0400559}
560
Jamie Madille36b92d2016-03-04 15:46:58 -0500561// VertexBufferBinding implementation
562VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr)
563{
564}
565
566VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other)
567 : mBoundVertexBuffer(other.mBoundVertexBuffer)
568{
569 if (mBoundVertexBuffer)
570 {
571 mBoundVertexBuffer->addRef();
572 }
573}
574
575VertexBufferBinding::~VertexBufferBinding()
576{
577 if (mBoundVertexBuffer)
578 {
579 mBoundVertexBuffer->release();
580 }
581}
582
583VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other)
584{
585 mBoundVertexBuffer = other.mBoundVertexBuffer;
586 if (mBoundVertexBuffer)
587 {
588 mBoundVertexBuffer->addRef();
589 }
590 return *this;
591}
592
593void VertexBufferBinding::set(VertexBuffer *vertexBuffer)
594{
595 if (mBoundVertexBuffer == vertexBuffer)
596 return;
597
598 if (mBoundVertexBuffer)
599 {
600 mBoundVertexBuffer->release();
601 }
602 if (vertexBuffer)
603 {
604 vertexBuffer->addRef();
605 }
606
607 mBoundVertexBuffer = vertexBuffer;
608}
609
610VertexBuffer *VertexBufferBinding::get() const
611{
612 return mBoundVertexBuffer;
613}
614
Jamie Madilldbfc6c62016-02-29 01:08:57 -0500615} // namespace rx