Add proper support for IndexType and uint32 indices

This commit is contained in:
SirLynix
2022-04-04 09:02:00 +02:00
parent 66ff6cfa81
commit 9d526741b9
34 changed files with 188 additions and 57 deletions

View File

@@ -108,7 +108,7 @@ namespace Nz
}
}
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>(false, 3 * header.num_tris, parameters.indexBufferFlags, parameters.bufferFactory);
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>(IndexType::U16, 3 * header.num_tris, parameters.indexBufferFlags, parameters.bufferFactory);
// Extract triangles data
std::vector<MD2_Triangle> triangles(header.num_tris);

View File

@@ -96,7 +96,7 @@ namespace Nz
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>(largeIndices, indexCount, parameters.indexBufferFlags, parameters.bufferFactory);
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>((largeIndices) ? IndexType::U32 : IndexType::U16, indexCount, parameters.indexBufferFlags, parameters.bufferFactory);
std::shared_ptr<VertexBuffer> vertexBuffer = std::make_shared<VertexBuffer>(VertexDeclaration::Get(VertexLayout::XYZ_Normal_UV_Tangent_Skinning), UInt32(vertexCount), parameters.vertexBufferFlags, parameters.bufferFactory);
// Index buffer
@@ -241,7 +241,7 @@ namespace Nz
// Index buffer
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>(largeIndices, indexCount, parameters.indexBufferFlags, parameters.bufferFactory);
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>((largeIndices) ? IndexType::U32 : IndexType::U16, indexCount, parameters.indexBufferFlags, parameters.bufferFactory);
IndexMapper indexMapper(*indexBuffer);
IndexIterator index = indexMapper.begin();
@@ -293,7 +293,7 @@ namespace Nz
// Vertex colors (.md5mesh files have no vertex color)
if (auto colorPtr = vertexMapper.GetComponentPtr<Color>(VertexComponent::Color))
{
for (std::size_t i = 0; i < md5Mesh.vertices.size(); ++i)
for (std::size_t j = 0; j < md5Mesh.vertices.size(); ++j)
*colorPtr++ = Color::White;
}

View File

@@ -254,7 +254,9 @@ namespace Nz
}
// Création des buffers
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>(vertexCount > std::numeric_limits<UInt16>::max(), indices.size(), parameters.indexBufferFlags, parameters.bufferFactory);
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>((largeIndices) ? IndexType::U32 : IndexType::U16, indices.size(), parameters.indexBufferFlags, parameters.bufferFactory);
std::shared_ptr<VertexBuffer> vertexBuffer = std::make_shared<VertexBuffer>(parameters.vertexDeclaration, vertexCount, parameters.vertexBufferFlags, parameters.bufferFactory);
// Remplissage des indices

View File

@@ -13,11 +13,11 @@
namespace Nz
{
IndexBuffer::IndexBuffer(bool largeIndices, std::shared_ptr<Buffer> buffer) :
IndexBuffer::IndexBuffer(IndexType indexType, std::shared_ptr<Buffer> buffer) :
m_buffer(std::move(buffer)),
m_indexType(indexType),
m_endOffset(m_buffer->GetSize()),
m_startOffset(0),
m_largeIndices(largeIndices)
m_startOffset(0)
{
NazaraAssert(m_buffer, "invalid buffer");
NazaraAssert(m_buffer->GetType() == BufferType::Index, "buffer must be an index buffer");
@@ -26,23 +26,23 @@ namespace Nz
m_indexCount = m_endOffset / GetStride();
}
IndexBuffer::IndexBuffer(bool largeIndices, std::shared_ptr<Buffer> buffer, UInt64 offset, UInt64 size) :
IndexBuffer::IndexBuffer(IndexType indexType, std::shared_ptr<Buffer> buffer, UInt64 offset, UInt64 size) :
m_buffer(std::move(buffer)),
m_indexType(indexType),
m_endOffset(offset + size),
m_startOffset(offset),
m_largeIndices(largeIndices)
m_startOffset(offset)
{
NazaraAssert(m_buffer, "invalid buffer");
NazaraAssert(m_buffer->GetType() == BufferType::Index, "buffer must be an index buffer");
NazaraAssert(size > 0, "invalid size");
NazaraAssert(size > 0, "invalid buffer size");
m_indexCount = size / GetStride();
}
IndexBuffer::IndexBuffer(bool largeIndices, UInt64 indexCount, BufferUsageFlags usage, const BufferFactory& bufferFactory, const void* initialData) :
IndexBuffer::IndexBuffer(IndexType indexType, UInt64 indexCount, BufferUsageFlags usage, const BufferFactory& bufferFactory, const void* initialData) :
m_indexType(indexType),
m_indexCount(indexCount),
m_startOffset(0),
m_largeIndices(largeIndices)
m_startOffset(0)
{
NazaraAssert(indexCount > 0, "invalid index count");

View File

@@ -19,6 +19,12 @@ namespace Nz
return static_cast<UInt32>(i);
}
UInt32 Getter8(const void* buffer, std::size_t i)
{
const UInt8* ptr = static_cast<const UInt8*>(buffer);
return ptr[i];
}
UInt32 Getter16(const void* buffer, std::size_t i)
{
const UInt16* ptr = static_cast<const UInt16*>(buffer);
@@ -37,10 +43,16 @@ namespace Nz
return 0;
}
void Setter8(void* buffer, std::size_t i, UInt32 value)
{
UInt8* ptr = static_cast<UInt8*>(buffer);
ptr[i] = SafeCast<UInt8>(value);
}
void Setter16(void* buffer, std::size_t i, UInt32 value)
{
UInt16* ptr = static_cast<UInt16*>(buffer);
ptr[i] = static_cast<UInt16>(value);
ptr[i] = SafeCast<UInt16>(value);
}
void Setter32(void* buffer, std::size_t i, UInt32 value)
@@ -63,13 +75,37 @@ namespace Nz
if (!m_mapper.Map(indexBuffer, 0, m_indexCount))
NazaraError("Failed to map buffer"); ///TODO: Unexcepted
Getter rightGetter = nullptr;
Setter rightSetter = nullptr;
switch (indexBuffer.GetIndexType())
{
case IndexType::U8:
rightGetter = Getter8;
rightSetter = Setter8;
break;
case IndexType::U16:
rightGetter = Getter16;
rightSetter = Setter16;
break;
case IndexType::U32:
rightGetter = Getter32;
rightSetter = Setter32;
break;
}
if (!rightGetter)
NazaraError("unexpected index size"); ///TODO: Unexcepted
if (indexBuffer.GetBuffer()->GetUsageFlags().Test(BufferUsage::Read))
m_getter = (indexBuffer.HasLargeIndices()) ? Getter32 : Getter16;
m_getter = rightGetter;
else
m_getter = GetterError;
if (indexBuffer.GetBuffer()->GetUsageFlags().Test(BufferUsage::Write))
m_setter = (indexBuffer.HasLargeIndices()) ? Setter32 : Setter16;
m_setter = rightSetter;
else
m_setter = SetterError;
}

View File

@@ -100,7 +100,9 @@ namespace Nz
std::size_t vertexCount;
ComputeBoxIndexVertexCount(primitive.box.subdivision, &indexCount, &vertexCount);
indexBuffer = std::make_shared<IndexBuffer>(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.indexBufferFlags, params.bufferFactory);
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
indexBuffer = std::make_shared<IndexBuffer>((largeIndices) ? IndexType::U32 : IndexType::U16, indexCount, params.indexBufferFlags, params.bufferFactory);
vertexBuffer = std::make_shared<VertexBuffer>(declaration, vertexCount, params.vertexBufferFlags, params.bufferFactory);
VertexMapper vertexMapper(*vertexBuffer);
@@ -122,7 +124,9 @@ namespace Nz
std::size_t vertexCount;
ComputeConeIndexVertexCount(primitive.cone.subdivision, &indexCount, &vertexCount);
indexBuffer = std::make_shared<IndexBuffer>(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.indexBufferFlags, params.bufferFactory);
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
indexBuffer = std::make_shared<IndexBuffer>((largeIndices) ? IndexType::U32 : IndexType::U16, indexCount, params.indexBufferFlags, params.bufferFactory);
vertexBuffer = std::make_shared<VertexBuffer>(declaration, vertexCount, params.vertexBufferFlags, params.bufferFactory);
VertexMapper vertexMapper(*vertexBuffer);
@@ -144,7 +148,9 @@ namespace Nz
std::size_t vertexCount;
ComputePlaneIndexVertexCount(primitive.plane.subdivision, &indexCount, &vertexCount);
indexBuffer = std::make_shared<IndexBuffer>(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.indexBufferFlags, params.bufferFactory);
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
indexBuffer = std::make_shared<IndexBuffer>((largeIndices) ? IndexType::U32 : IndexType::U16, indexCount, params.indexBufferFlags, params.bufferFactory);
vertexBuffer = std::make_shared<VertexBuffer>(declaration, vertexCount, params.vertexBufferFlags, params.bufferFactory);
VertexMapper vertexMapper(*vertexBuffer);
@@ -170,7 +176,9 @@ namespace Nz
std::size_t vertexCount;
ComputeCubicSphereIndexVertexCount(primitive.sphere.cubic.subdivision, &indexCount, &vertexCount);
indexBuffer = std::make_shared<IndexBuffer>(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.indexBufferFlags, params.bufferFactory);
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
indexBuffer = std::make_shared<IndexBuffer>((largeIndices) ? IndexType::U32 : IndexType::U16, indexCount, params.indexBufferFlags, params.bufferFactory);
vertexBuffer = std::make_shared<VertexBuffer>(declaration, vertexCount, params.vertexBufferFlags, params.bufferFactory);
VertexMapper vertexMapper(*vertexBuffer);
@@ -192,7 +200,9 @@ namespace Nz
std::size_t vertexCount;
ComputeIcoSphereIndexVertexCount(primitive.sphere.ico.recursionLevel, &indexCount, &vertexCount);
indexBuffer = std::make_shared<IndexBuffer>(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.indexBufferFlags, params.bufferFactory);
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
indexBuffer = std::make_shared<IndexBuffer>((largeIndices) ? IndexType::U32 : IndexType::U16, indexCount, params.indexBufferFlags, params.bufferFactory);
vertexBuffer = std::make_shared<VertexBuffer>(declaration, vertexCount, params.vertexBufferFlags, params.bufferFactory);
VertexMapper vertexMapper(*vertexBuffer);
@@ -214,7 +224,9 @@ namespace Nz
std::size_t vertexCount;
ComputeUvSphereIndexVertexCount(primitive.sphere.uv.sliceCount, primitive.sphere.uv.stackCount, &indexCount, &vertexCount);
indexBuffer = std::make_shared<IndexBuffer>(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.indexBufferFlags, params.bufferFactory);
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
indexBuffer = std::make_shared<IndexBuffer>((largeIndices) ? IndexType::U32 : IndexType::U16, indexCount, params.indexBufferFlags, params.bufferFactory);
vertexBuffer = std::make_shared<VertexBuffer>(declaration, vertexCount, params.vertexBufferFlags, params.bufferFactory);
VertexMapper vertexMapper(*vertexBuffer);