Add support for 32-bit index buffers
Some checks failed
CMake on multiple platforms / build (Release, cl, cl, windows-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, clang, clang++, macos-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, clang, clang++, ubuntu-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Has been cancelled
Some checks failed
CMake on multiple platforms / build (Release, cl, cl, windows-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, clang, clang++, macos-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, clang, clang++, ubuntu-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Has been cancelled
This commit is contained in:
@@ -488,14 +488,24 @@ void KRMesh::LoadDesc(const KRMesh::MeshDesc& mi, bool calculate_normals, bool c
|
|||||||
primitive.layout.topology = p.format;
|
primitive.layout.topology = p.format;
|
||||||
primitive.vertexCount = p.vertices.size();
|
primitive.vertexCount = p.vertices.size();
|
||||||
primitive.indexCount = p.indexes.size();
|
primitive.indexCount = p.indexes.size();
|
||||||
|
int indexSize = 0;
|
||||||
|
if (primitive.indexCount == 0) {
|
||||||
|
primitive.layout.indexType = ComponentType::empty;
|
||||||
|
} else if (primitive.vertexCount < 0xffff) {
|
||||||
|
// Some devices reserve uint16 index 0xffff
|
||||||
|
primitive.layout.indexType = ComponentType::uint16;
|
||||||
|
indexSize = 2;
|
||||||
|
} else {
|
||||||
|
primitive.layout.indexType = ComponentType::uint32;
|
||||||
|
indexSize = 4;
|
||||||
|
}
|
||||||
|
|
||||||
const int kBufferAlignment = 64;
|
const int kBufferAlignment = 64;
|
||||||
const int kIndexSize = 2; // TODO: implement mixed 16 and 32 bit indexes
|
|
||||||
|
|
||||||
dataOffset = (dataOffset + kBufferAlignment - 1) / kBufferAlignment * kBufferAlignment;
|
dataOffset = (dataOffset + kBufferAlignment - 1) / kBufferAlignment * kBufferAlignment;
|
||||||
|
|
||||||
primitive.indexOffset = dataOffset;
|
primitive.indexOffset = dataOffset;
|
||||||
dataOffset += primitive.indexCount * kIndexSize;
|
dataOffset += primitive.indexCount * indexSize;
|
||||||
|
|
||||||
dataOffset = (dataOffset + kBufferAlignment - 1) / kBufferAlignment * kBufferAlignment;
|
dataOffset = (dataOffset + kBufferAlignment - 1) / kBufferAlignment * kBufferAlignment;
|
||||||
primitive.vertexOffset = dataOffset;
|
primitive.vertexOffset = dataOffset;
|
||||||
@@ -1944,23 +1954,47 @@ void KRMesh::convertToIndexed(std::bitset<64> primitives)
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
int KRMesh::getVertexIndex(int primitive, int index) const
|
int KRMesh::getVertexIndex(int primitiveIndex, int index) const
|
||||||
{
|
{
|
||||||
if (getIndexCount(primitive) > 0) {
|
const pack_primitive* primitive = getPrimitive(primitiveIndex);
|
||||||
const int kVertexIndexSize = 2; // TODO: Support both 16 and 32 bit vertex indexes
|
switch (primitive->layout.indexType) {
|
||||||
std::byte* vertexIndexData = (std::byte*)m_indexBlocks[primitive]->getStart() + kVertexIndexSize * index;
|
case ComponentType::uint16:
|
||||||
return *((uint16_t*)vertexIndexData);
|
{
|
||||||
} else {
|
std::byte* vertexIndexData = (std::byte*)m_indexBlocks[primitiveIndex]->getStart() + index * 2;
|
||||||
|
return *((uint16_t*)vertexIndexData);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ComponentType::uint32:
|
||||||
|
{
|
||||||
|
std::byte* vertexIndexData = (std::byte*)m_indexBlocks[primitiveIndex]->getStart() + index * 4;
|
||||||
|
return *((uint32_t*)vertexIndexData);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
return index;
|
return index;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void KRMesh::setVertexIndex(int primitive, int index, int indexVal)
|
void KRMesh::setVertexIndex(int primitiveIndex, int index, int indexVal)
|
||||||
{
|
{
|
||||||
const int kVertexIndexSize = 2; // TODO: Support both 16 and 32 bit vertex indexes
|
const pack_primitive* primitive = getPrimitive(primitiveIndex);
|
||||||
std::byte* vertexIndexData = (std::byte*)m_indexBlocks[primitive]->getStart() + kVertexIndexSize * index;
|
switch (primitive->layout.indexType) {
|
||||||
uint16_t val = static_cast<uint16_t>(indexVal);
|
case ComponentType::uint16:
|
||||||
*((uint16_t*)vertexIndexData) = val;
|
{
|
||||||
|
std::byte* vertexIndexData = (std::byte*)m_indexBlocks[primitiveIndex]->getStart() + index * 2;
|
||||||
|
*((uint16_t*)vertexIndexData) = (uint16_t)indexVal;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ComponentType::uint32:
|
||||||
|
{
|
||||||
|
std::byte* vertexIndexData = (std::byte*)m_indexBlocks[primitiveIndex]->getStart() + index * 4;
|
||||||
|
*((uint32_t*)vertexIndexData) = (uint32_t)indexVal;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void KRMesh::optimizeIndexes(std::bitset<64> primitives)
|
void KRMesh::optimizeIndexes(std::bitset<64> primitives)
|
||||||
|
|||||||
@@ -651,8 +651,7 @@ void KRMeshManager::KRVBOData::bind(VkCommandBuffer& commandBuffer)
|
|||||||
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
|
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
|
||||||
|
|
||||||
if (m_index_data && m_index_data->getSize() > 0) {
|
if (m_index_data && m_index_data->getSize() > 0) {
|
||||||
// TODO - Support 32-bit index buffers
|
vkCmdBindIndexBuffer(commandBuffer, getIndexBuffer(), 0, m_layout->indexType == ComponentType::uint16 ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32);
|
||||||
vkCmdBindIndexBuffer(commandBuffer, getIndexBuffer(), 0, VK_INDEX_TYPE_UINT16);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -164,6 +164,7 @@ struct VertexBufferLayout
|
|||||||
int16_t offsets[kMaxAttributes];
|
int16_t offsets[kMaxAttributes];
|
||||||
int16_t vertexSize;
|
int16_t vertexSize;
|
||||||
Topology topology;
|
Topology topology;
|
||||||
|
ComponentType indexType;
|
||||||
};
|
};
|
||||||
|
|
||||||
class KRMeshManager : public KRResourceManager
|
class KRMeshManager : public KRResourceManager
|
||||||
|
|||||||
Reference in New Issue
Block a user