Skip to content

Commit

Permalink
Refs vrm-c#2521 - Improved exporting performance drastically by incre…
Browse files Browse the repository at this point in the history
…asing allocated buffer more than needed to be able to skip extending chunk by chunk. No difference in export quality
  • Loading branch information
cpetry committed Dec 3, 2024
1 parent e5e7313 commit 66f37ba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
18 changes: 7 additions & 11 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,18 @@ public glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target = d

public glTFBufferView Extend(IntPtr p, int bytesLength, int stride, glBufferTarget target)
{
var tmp = m_bytes;
// alignment
var padding = m_used % stride == 0 ? 0 : stride - m_used % stride;
var requiredLength = m_used + padding + bytesLength;

if (m_bytes == null || m_used + padding + bytesLength > m_bytes.Length)
if (m_bytes == null || requiredLength > m_bytes.Length)
{
// recreate buffer
m_bytes = new Byte[m_used + padding + bytesLength];
var newLength = Math.Max(requiredLength, m_bytes?.Length * 2 ?? 256);
var newBuffer = new Byte[newLength];
if (m_used > 0)
{
Buffer.BlockCopy(tmp, 0, m_bytes, 0, m_used);
}
}
if (m_used + padding + bytesLength > m_bytes.Length)
{
throw new ArgumentOutOfRangeException();
Buffer.BlockCopy(m_bytes, 0, newBuffer, 0, m_used);
m_bytes = newBuffer;
}

Marshal.Copy(p, m_bytes, m_used + padding, bytesLength);
Expand All @@ -70,7 +66,7 @@ public glTFBufferView Extend(IntPtr p, int bytesLength, int stride, glBufferTarg
byteStride = stride,
target = target,
};
m_used = m_used + padding + bytesLength;
m_used = requiredLength;
return result;
}

Expand Down
4 changes: 2 additions & 2 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public NativeArray<T> CreateNativeArray<T>(int size) where T : struct
public NativeArray<T> CreateNativeArray<T>(ArraySegment<T> data) where T : struct
{
var array = CreateNativeArray<T>(data.Count);
// TODO: remove ToArray
array.CopyFrom(data.ToArray());
for (int i = 0; i < data.Count; i++)
array[i] = data.Array[data.Offset + i];
return array;
}

Expand Down

0 comments on commit 66f37ba

Please sign in to comment.