**Refactor meshes format to support custom vertex layouts and new flexible api to access mesh data**

#3044 #2667
This commit is contained in:
Wojtek Figat
2025-01-06 22:47:19 +01:00
parent 29bfef677f
commit db4d7d2a05
65 changed files with 4428 additions and 3106 deletions

View File

@@ -9,7 +9,9 @@ namespace FlaxEngine
{
/// <summary>
/// The Vertex Buffer 0 structure format.
/// [Deprecated in v1.10]
/// </summary>
[Obsolete("Use new MeshAccessor and depend on GPUVertexLayout when accessing mesh data.")]
public struct Vertex0
{
/// <summary>
@@ -20,7 +22,9 @@ namespace FlaxEngine
/// <summary>
/// The Vertex Buffer 1 structure format.
/// [Deprecated in v1.10]
/// </summary>
[Obsolete("Use new MeshAccessor and depend on GPUVertexLayout when accessing mesh data.")]
public struct Vertex1
{
/// <summary>
@@ -46,7 +50,9 @@ namespace FlaxEngine
/// <summary>
/// The Vertex Buffer 2 structure format.
/// [Deprecated in v1.10]
/// </summary>
[Obsolete("Use new MeshAccessor and depend on GPUVertexLayout when accessing mesh data.")]
public struct Vertex2
{
/// <summary>
@@ -57,7 +63,9 @@ namespace FlaxEngine
/// <summary>
/// The raw Vertex Buffer structure format.
/// [Deprecated in v1.10]
/// </summary>
[Obsolete("Use new MeshAccessor and depend on GPUVertexLayout when accessing mesh data.")]
public struct Vertex
{
/// <summary>
@@ -425,6 +433,10 @@ namespace FlaxEngine
UpdateMesh(Utils.ConvertCollection(vertices), triangles, Utils.ConvertCollection(normals), Utils.ConvertCollection(tangents), Utils.ConvertCollection(uv), colors);
}
/// <summary>
/// [Deprecated in v1.10]
/// </summary>
[Obsolete("Use new MeshAccessor and depend on GPUVertexLayout when accessing mesh data.")]
internal enum InternalBufferType
{
VB0 = 0,
@@ -436,9 +448,11 @@ namespace FlaxEngine
/// <summary>
/// Downloads the first vertex buffer that contains mesh vertices data. To download data from GPU set <paramref name="forceGpu"/> to true and call this method from the thread other than main thread (see <see cref="Platform.IsInMainThread"/>).
/// [Deprecated in v1.10]
/// </summary>
/// <param name="forceGpu">If set to <c>true</c> the data will be downloaded from the GPU, otherwise it can be loaded from the drive (source asset file) or from memory (if cached). Downloading mesh from GPU requires this call to be made from the other thread than main thread. Virtual assets are always downloaded from GPU memory due to lack of dedicated storage container for the asset data.</param>
/// <returns>The gathered data.</returns>
[Obsolete("Use new MeshAccessor and depend on GPUVertexLayout when accessing mesh data.")]
public Vertex0[] DownloadVertexBuffer0(bool forceGpu = false)
{
var result = (Vertex0[])Internal_DownloadBuffer(__unmanagedPtr, forceGpu, typeof(Vertex0), (int)InternalBufferType.VB0);
@@ -449,9 +463,11 @@ namespace FlaxEngine
/// <summary>
/// Downloads the second vertex buffer that contains mesh vertices data. To download data from GPU set <paramref name="forceGpu"/> to true and call this method from the thread other than main thread (see <see cref="Platform.IsInMainThread"/>).
/// [Deprecated in v1.10]
/// </summary>
/// <param name="forceGpu">If set to <c>true</c> the data will be downloaded from the GPU, otherwise it can be loaded from the drive (source asset file) or from memory (if cached). Downloading mesh from GPU requires this call to be made from the other thread than main thread. Virtual assets are always downloaded from GPU memory due to lack of dedicated storage container for the asset data.</param>
/// <returns>The gathered data.</returns>
[Obsolete("Use new MeshAccessor and depend on GPUVertexLayout when accessing mesh data.")]
public Vertex1[] DownloadVertexBuffer1(bool forceGpu = false)
{
var result = (Vertex1[])Internal_DownloadBuffer(__unmanagedPtr, forceGpu, typeof(Vertex1), (int)InternalBufferType.VB1);
@@ -462,12 +478,14 @@ namespace FlaxEngine
/// <summary>
/// Downloads the third vertex buffer that contains mesh vertices data. To download data from GPU set <paramref name="forceGpu"/> to true and call this method from the thread other than main thread (see <see cref="Platform.IsInMainThread"/>).
/// [Deprecated in v1.10]
/// </summary>
/// <remarks>
/// If mesh has no vertex colors (stored in vertex buffer 2) the returned value is null.
/// </remarks>
/// <param name="forceGpu">If set to <c>true</c> the data will be downloaded from the GPU, otherwise it can be loaded from the drive (source asset file) or from memory (if cached). Downloading mesh from GPU requires this call to be made from the other thread than main thread. Virtual assets are always downloaded from GPU memory due to lack of dedicated storage container for the asset data.</param>
/// <returns>The gathered data or null if mesh has no vertex colors.</returns>
[Obsolete("Use new MeshAccessor and depend on GPUVertexLayout when accessing mesh data.")]
public Vertex2[] DownloadVertexBuffer2(bool forceGpu = false)
{
if (!HasVertexColors)
@@ -480,14 +498,13 @@ namespace FlaxEngine
/// <summary>
/// Downloads the raw vertex buffer that contains mesh vertices data. To download data from GPU set <paramref name="forceGpu"/> to true and call this method from the thread other than main thread (see <see cref="Platform.IsInMainThread"/>).
/// [Deprecated in v1.10]
/// </summary>
/// <param name="forceGpu">If set to <c>true</c> the data will be downloaded from the GPU, otherwise it can be loaded from the drive (source asset file) or from memory (if cached). Downloading mesh from GPU requires this call to be made from the other thread than main thread. Virtual assets are always downloaded from GPU memory due to lack of dedicated storage container for the asset data.</param>
/// <returns>The gathered data.</returns>
[Obsolete("Use new MeshAccessor and depend on GPUVertexLayout when accessing mesh data.")]
public Vertex[] DownloadVertexBuffer(bool forceGpu = false)
{
// TODO: perform data conversion on C++ side to make it faster
// TODO: implement batched data download (3 buffers at once) to reduce stall
var vb0 = DownloadVertexBuffer0(forceGpu);
var vb1 = DownloadVertexBuffer1(forceGpu);
var vb2 = DownloadVertexBuffer2(forceGpu);
@@ -519,33 +536,5 @@ namespace FlaxEngine
return result;
}
/// <summary>
/// Downloads the index buffer that contains mesh triangles data. To download data from GPU set <paramref name="forceGpu"/> to true and call this method from the thread other than main thread (see <see cref="Platform.IsInMainThread"/>).
/// </summary>
/// <remarks>If mesh index buffer format (see <see cref="MeshBase.IndexBufferFormat"/>) is <see cref="PixelFormat.R16_UInt"/> then it's faster to call .</remarks>
/// <param name="forceGpu">If set to <c>true</c> the data will be downloaded from the GPU, otherwise it can be loaded from the drive (source asset file) or from memory (if cached). Downloading mesh from GPU requires this call to be made from the other thread than main thread. Virtual assets are always downloaded from GPU memory due to lack of dedicated storage container for the asset data.</param>
/// <returns>The gathered data.</returns>
public uint[] DownloadIndexBuffer(bool forceGpu = false)
{
var result = (uint[])Internal_DownloadBuffer(__unmanagedPtr, forceGpu, typeof(uint), (int)InternalBufferType.IB32);
if (result == null)
throw new Exception("Failed to download mesh data.");
return result;
}
/// <summary>
/// Downloads the index buffer that contains mesh triangles data. To download data from GPU set <paramref name="forceGpu"/> to true and call this method from the thread other than main thread (see <see cref="Platform.IsInMainThread"/>).
/// </summary>
/// <remarks>If mesh index buffer format (see <see cref="MeshBase.IndexBufferFormat"/>) is <see cref="PixelFormat.R32_UInt"/> then data won't be downloaded.</remarks>
/// <param name="forceGpu">If set to <c>true</c> the data will be downloaded from the GPU, otherwise it can be loaded from the drive (source asset file) or from memory (if cached). Downloading mesh from GPU requires this call to be made from the other thread than main thread. Virtual assets are always downloaded from GPU memory due to lack of dedicated storage container for the asset data.</param>
/// <returns>The gathered data.</returns>
public ushort[] DownloadIndexBufferUShort(bool forceGpu = false)
{
var result = (ushort[])Internal_DownloadBuffer(__unmanagedPtr, forceGpu, typeof(ushort), (int)InternalBufferType.IB16);
if (result == null)
throw new Exception("Failed to download mesh data.");
return result;
}
}
}