ES31: Refactor VertexArray for Vertex Attrib Binding
OpenGL ES3.1 feature Vertex Attrib Binding requires vertex arrays should
be split into two arrays:
1. an array of vertex buffer binding points, each of which specifies:
- a bound buffer object,
- a starting offset for vertex attribute data in that buffer object,
- a stride used by all attributes using that binding point,
- a frequency divisor used by all attributes using that binding point.
2. an array of generic vertex attribute format information records, each
of which specifies:
- a reference to one of the new buffer binding points above,
- a component count and format, and a normalization flag for the
attribute data,
- the offset of the attribute data relative to the base offset of each
vertex found at the associated binding point.
Current ANGLE implementation simply uses a struct to represent a vertex
attribute object, which does not meet the requirements above.
This patch aims to be the the basis of the implementation of all ES3.1
Vertex Attrib Binding APIs by refactoring the struct VertexAttribute and
the class VertexArray to fit the new data layout and ensuring all current
functionality is retained.
BUG=angleproject:1593
TEST=angle_unittests, angle_end2end_tests, gpu_unittests
Change-Id: Ieb41f1bf503f815fd0476d2ea045dcb863465254
Reviewed-on: https://chromium-review.googlesource.com/418880
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/renderer/d3d/VertexDataManager.cpp b/src/libANGLE/renderer/d3d/VertexDataManager.cpp
index 72329d7..5b8eec5 100644
--- a/src/libANGLE/renderer/d3d/VertexDataManager.cpp
+++ b/src/libANGLE/renderer/d3d/VertexDataManager.cpp
@@ -36,7 +36,10 @@
CONSTANT_VERTEX_BUFFER_SIZE = 4096
};
-int ElementsInBuffer(const gl::VertexAttribute &attrib, unsigned int size)
+// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
+int ElementsInBuffer(const gl::VertexAttribute &attrib,
+ const gl::VertexBinding &binding,
+ unsigned int size)
{
// Size cannot be larger than a GLsizei
if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
@@ -44,13 +47,15 @@
size = static_cast<unsigned int>(std::numeric_limits<int>::max());
}
- GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib));
- return (size - attrib.offset % stride +
+ GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib, binding));
+ GLsizei offset = static_cast<GLsizei>(ComputeVertexAttributeOffset(attrib, binding));
+ return (size - offset % stride +
(stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) /
stride;
}
-bool DirectStoragePossible(const gl::VertexAttribute &attrib)
+// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
+bool DirectStoragePossible(const gl::VertexAttribute &attrib, const gl::VertexBinding &binding)
{
// Current value attribs may not use direct storage.
if (!attrib.enabled)
@@ -58,7 +63,7 @@
return false;
}
- gl::Buffer *buffer = attrib.buffer.get();
+ gl::Buffer *buffer = binding.buffer.get();
if (!buffer)
{
return false;
@@ -82,7 +87,7 @@
// TODO(jmadill): add VertexFormatCaps
BufferFactoryD3D *factory = bufferD3D->getFactory();
- auto errorOrElementSize = factory->getVertexSpaceRequired(attrib, 1, 0);
+ auto errorOrElementSize = factory->getVertexSpaceRequired(attrib, binding, 1, 0);
if (errorOrElementSize.isError())
{
ERR() << "Unlogged error in DirectStoragePossible.";
@@ -98,9 +103,10 @@
}
}
+ GLintptr offset = ComputeVertexAttributeOffset(attrib, binding);
// Final alignment check - unaligned data must be converted.
- return (static_cast<size_t>(ComputeVertexAttributeStride(attrib)) % alignment == 0) &&
- (static_cast<size_t>(attrib.offset) % alignment == 0);
+ return (static_cast<size_t>(ComputeVertexAttributeStride(attrib, binding)) % alignment == 0) &&
+ (static_cast<size_t>(offset) % alignment == 0);
}
} // anonymous namespace
@@ -132,7 +138,9 @@
return offset.ValueOrDie();
}
-VertexStorageType ClassifyAttributeStorage(const gl::VertexAttribute &attrib)
+// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
+VertexStorageType ClassifyAttributeStorage(const gl::VertexAttribute &attrib,
+ const gl::VertexBinding &binding)
{
// If attribute is disabled, we use the current value.
if (!attrib.enabled)
@@ -141,14 +149,14 @@
}
// If specified with immediate data, we must use dynamic storage.
- auto *buffer = attrib.buffer.get();
+ auto *buffer = binding.buffer.get();
if (!buffer)
{
return VertexStorageType::DYNAMIC;
}
// Check if the buffer supports direct storage.
- if (DirectStoragePossible(attrib))
+ if (DirectStoragePossible(attrib, binding))
{
return VertexStorageType::DIRECT;
}
@@ -213,6 +221,7 @@
const gl::VertexArray *vertexArray = state.getVertexArray();
const auto &vertexAttributes = vertexArray->getVertexAttributes();
+ const auto &vertexBindings = vertexArray->getVertexBindings();
mDynamicAttribsMaskCache.reset();
const gl::Program *program = state.getProgram();
@@ -226,6 +235,7 @@
continue;
const auto &attrib = vertexAttributes[attribIndex];
+ const auto &binding = vertexBindings[attrib.bindingIndex];
// Resize automatically puts in empty attribs
translatedAttribs->resize(attribIndex + 1);
@@ -237,10 +247,11 @@
// Record the attribute now
translated->active = true;
translated->attribute = &attrib;
+ translated->binding = &binding;
translated->currentValueType = currentValueData.Type;
- translated->divisor = attrib.divisor;
+ translated->divisor = binding.divisor;
- switch (ClassifyAttributeStorage(attrib))
+ switch (ClassifyAttributeStorage(attrib, binding))
{
case VertexStorageType::STATIC:
{
@@ -283,19 +294,22 @@
// static
void VertexDataManager::StoreDirectAttrib(TranslatedAttribute *directAttrib)
{
- const auto &attrib = *directAttrib->attribute;
- gl::Buffer *buffer = attrib.buffer.get();
+ const auto &attrib = *directAttrib->attribute;
+ const auto &binding = *directAttrib->binding;
+
+ gl::Buffer *buffer = binding.buffer.get();
BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
- ASSERT(DirectStoragePossible(attrib));
+ ASSERT(DirectStoragePossible(attrib, binding));
directAttrib->vertexBuffer.set(nullptr);
directAttrib->storage = bufferD3D;
directAttrib->serial = bufferD3D->getSerial();
- directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib));
- directAttrib->baseOffset = static_cast<unsigned int>(attrib.offset);
+ directAttrib->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding));
+ directAttrib->baseOffset =
+ static_cast<unsigned int>(ComputeVertexAttributeOffset(attrib, binding));
// Instanced vertices do not apply the 'start' offset
- directAttrib->usesFirstVertexOffset = (attrib.divisor == 0);
+ directAttrib->usesFirstVertexOffset = (binding.divisor == 0);
}
// static
@@ -303,41 +317,43 @@
GLsizei count,
GLsizei instances)
{
- const gl::VertexAttribute &attrib = *translated->attribute;
+ const auto &attrib = *translated->attribute;
+ const auto &binding = *translated->binding;
- gl::Buffer *buffer = attrib.buffer.get();
- ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib));
+ gl::Buffer *buffer = binding.buffer.get();
+ ASSERT(buffer && attrib.enabled && !DirectStoragePossible(attrib, binding));
BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
// Compute source data pointer
const uint8_t *sourceData = nullptr;
+ const int offset = static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
ANGLE_TRY(bufferD3D->getData(&sourceData));
- sourceData += static_cast<int>(attrib.offset);
+ sourceData += offset;
unsigned int streamOffset = 0;
translated->storage = nullptr;
- ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, 1, 0),
+ ANGLE_TRY_RESULT(bufferD3D->getFactory()->getVertexSpaceRequired(attrib, binding, 1, 0),
translated->stride);
- auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib);
+ auto *staticBuffer = bufferD3D->getStaticVertexBuffer(attrib, binding);
ASSERT(staticBuffer);
if (staticBuffer->empty())
{
// Convert the entire buffer
- int totalCount = ElementsInBuffer(attrib, static_cast<unsigned int>(bufferD3D->getSize()));
- int startIndex = static_cast<int>(attrib.offset) /
- static_cast<int>(ComputeVertexAttributeStride(attrib));
+ int totalCount =
+ ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
+ int startIndex = offset / static_cast<int>(ComputeVertexAttributeStride(attrib, binding));
- ANGLE_TRY(
- staticBuffer->storeStaticAttribute(attrib, -startIndex, totalCount, 0, sourceData));
+ ANGLE_TRY(staticBuffer->storeStaticAttribute(attrib, binding, -startIndex, totalCount, 0,
+ sourceData));
}
unsigned int firstElementOffset =
- (static_cast<unsigned int>(attrib.offset) /
- static_cast<unsigned int>(ComputeVertexAttributeStride(attrib))) *
+ (static_cast<unsigned int>(offset) /
+ static_cast<unsigned int>(ComputeVertexAttributeStride(attrib, binding))) *
translated->stride;
VertexBuffer *vertexBuffer = staticBuffer->getVertexBuffer();
@@ -356,7 +372,7 @@
translated->baseOffset = streamOffset + firstElementOffset;
// Instanced vertices do not apply the 'start' offset
- translated->usesFirstVertexOffset = (attrib.divisor == 0);
+ translated->usesFirstVertexOffset = (binding.divisor == 0);
return gl::NoError();
}
@@ -411,11 +427,12 @@
for (auto attribIndex : IterateBitSet(dynamicAttribsMask))
{
const auto &dynamicAttrib = translatedAttribs[attribIndex];
- gl::Buffer *buffer = dynamicAttrib.attribute->buffer.get();
+ const auto &binding = *dynamicAttrib.binding;
+ gl::Buffer *buffer = binding.buffer.get();
if (buffer)
{
BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
- size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
+ size_t typeSize = ComputeVertexAttributeTypeSize(*dynamicAttrib.attribute);
bufferD3D->promoteStaticUsage(count * static_cast<int>(typeSize));
}
}
@@ -425,19 +442,20 @@
GLsizei count,
GLsizei instances) const
{
- const gl::VertexAttribute &attrib = *translatedAttrib.attribute;
- ASSERT(!DirectStoragePossible(attrib));
+ const auto &attrib = *translatedAttrib.attribute;
+ const auto &binding = *translatedAttrib.binding;
+ ASSERT(!DirectStoragePossible(attrib, binding));
- gl::Buffer *buffer = attrib.buffer.get();
+ gl::Buffer *buffer = binding.buffer.get();
BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
- ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib) == nullptr);
+ ASSERT(!bufferD3D || bufferD3D->getStaticVertexBuffer(attrib, binding) == nullptr);
- size_t totalCount = ComputeVertexAttributeElementCount(attrib, count, instances);
+ size_t totalCount = ComputeVertexBindingElementCount(binding, count, instances);
ASSERT(!bufferD3D ||
- ElementsInBuffer(attrib, static_cast<unsigned int>(bufferD3D->getSize())) >=
+ ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize())) >=
static_cast<int>(totalCount));
- return mStreamingBuffer->reserveVertexSpace(attrib, static_cast<GLsizei>(totalCount),
+ return mStreamingBuffer->reserveVertexSpace(attrib, binding, static_cast<GLsizei>(totalCount),
instances);
}
@@ -446,16 +464,17 @@
GLsizei count,
GLsizei instances)
{
- const gl::VertexAttribute &attrib = *translated->attribute;
+ const auto &attrib = *translated->attribute;
+ const auto &binding = *translated->binding;
- gl::Buffer *buffer = attrib.buffer.get();
+ gl::Buffer *buffer = binding.buffer.get();
ASSERT(buffer || attrib.pointer);
ASSERT(attrib.enabled);
BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
// Instanced vertices do not apply the 'start' offset
- GLint firstVertexIndex = (attrib.divisor > 0 ? 0 : start);
+ GLint firstVertexIndex = (binding.divisor > 0 ? 0 : start);
// Compute source data pointer
const uint8_t *sourceData = nullptr;
@@ -463,23 +482,25 @@
if (buffer)
{
ANGLE_TRY(storage->getData(&sourceData));
- sourceData += static_cast<int>(attrib.offset);
+ sourceData += static_cast<int>(ComputeVertexAttributeOffset(attrib, binding));
}
else
{
+ // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
+ // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
sourceData = static_cast<const uint8_t*>(attrib.pointer);
}
unsigned int streamOffset = 0;
translated->storage = nullptr;
- ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, 1, 0), translated->stride);
+ ANGLE_TRY_RESULT(mFactory->getVertexSpaceRequired(attrib, binding, 1, 0), translated->stride);
- size_t totalCount = ComputeVertexAttributeElementCount(attrib, count, instances);
+ size_t totalCount = ComputeVertexBindingElementCount(binding, count, instances);
ANGLE_TRY(mStreamingBuffer->storeDynamicAttribute(
- attrib, translated->currentValueType, firstVertexIndex, static_cast<GLsizei>(totalCount),
- instances, &streamOffset, sourceData));
+ attrib, binding, translated->currentValueType, firstVertexIndex,
+ static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData));
VertexBuffer *vertexBuffer = mStreamingBuffer->getVertexBuffer();
@@ -505,14 +526,15 @@
if (cachedState->data != currentValue)
{
- const gl::VertexAttribute &attrib = *translated->attribute;
+ const auto &attrib = *translated->attribute;
+ const auto &binding = *translated->binding;
- ANGLE_TRY(buffer->reserveVertexSpace(attrib, 1, 0));
+ ANGLE_TRY(buffer->reserveVertexSpace(attrib, binding, 1, 0));
const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
unsigned int streamOffset;
- ANGLE_TRY(buffer->storeDynamicAttribute(attrib, currentValue.Type, 0, 1, 0, &streamOffset,
- sourceData));
+ ANGLE_TRY(buffer->storeDynamicAttribute(attrib, binding, currentValue.Type, 0, 1, 0,
+ &streamOffset, sourceData));
buffer->getVertexBuffer()->hintUnmapResource();