private GltfPrimitiveStorageLocation[] AllocateMeshes(MgStorageBlockAllocationRequest request, GltfMesh[] meshes, GltfAccessor[] accessors, GltfBufferView[] bufferViews) { var locations = new List <GltfPrimitiveStorageLocation>(); foreach (var mesh in meshes) { foreach (var primitive in mesh.Primitives) { var locator = primitive.VertexLocations; var finalLocation = new GltfPrimitiveStorageLocation { }; finalLocation.CopyOperations = GenerateCopyOps(primitive.VertexCount, request, accessors, bufferViews, locator, finalLocation); locations.Add(finalLocation); } } return(locations.ToArray()); }
private static GltfInterleavedOperation[] GenerateCopyOps(uint vertexCount, MgStorageBlockAllocationRequest request, GltfAccessor[] accessors, GltfBufferView[] bufferViews, IPerVertexDataLocator locator, GltfPrimitiveStorageLocation finalLocation) { var totalSize = 0UL; var vertexFields = new int?[] { locator.Position, locator.Normal, locator.Tangent, locator.TexCoords0, locator.TexCoords1, locator.Color0, locator.Color1, locator.Joints0, locator.Joints1, locator.Weights0, locator.Weights1, }; var paddingByteStride = new uint[] { 12U, 12U, 16U, 8U, 8U, 16U, 16U, 4U, 4U, 16U, 16U, }; var copyOps = new List <GltfInterleavedOperation>(); var vertexBufferStride = 0U; for (var i = 0; i < vertexFields.Length; i += 1) { var field = vertexFields[i]; if (field.HasValue) { var selected = accessors[field.Value]; var op = CreateCopyOp(copyOps, selected, bufferViews); vertexBufferStride += op.ByteStride; totalSize += selected.TotalByteSize; } else { vertexBufferStride += paddingByteStride[i]; totalSize += vertexCount * paddingByteStride[i]; } } foreach (var op in copyOps) { op.DstStride = vertexBufferStride; } var vertexInfo = new MgStorageBlockAllocationInfo { MemoryPropertyFlags = MgMemoryPropertyFlagBits.HOST_COHERENT_BIT, Usage = MgBufferUsageFlagBits.VERTEX_BUFFER_BIT, ElementByteSize = vertexBufferStride, Size = totalSize, }; finalLocation.Vertex = request.Insert(vertexInfo); if (locator.Indices.HasValue) { var selected = accessors[locator.Indices.Value]; var op = CreateCopyOp(copyOps, selected, bufferViews); op.DstStride = selected.ElementByteSize; var indexInfo = new MgStorageBlockAllocationInfo { MemoryPropertyFlags = MgMemoryPropertyFlagBits.HOST_COHERENT_BIT, Usage = MgBufferUsageFlagBits.INDEX_BUFFER_BIT, Size = selected.TotalByteSize, ElementByteSize = selected.ElementByteSize, }; finalLocation.Index = request.Insert(indexInfo); } return(copyOps.ToArray()); }