|
6 | 6 | using System.Collections.Generic; |
7 | 7 | using System.IO; |
8 | 8 | using System.Reflection; |
| 9 | +using System.Runtime.CompilerServices; |
9 | 10 | using System.Runtime.InteropServices; |
10 | 11 |
|
11 | 12 | namespace HelloMeshlets |
@@ -57,31 +58,31 @@ public unsafe static void Main(string[] args) |
57 | 58 | byte[] meshletTriangles = new byte[maxMeshlets * kMaxTriangles * 3]; |
58 | 59 | Vector3[] positions = attrib.Vertices.ToArray(); |
59 | 60 |
|
60 | | - fixed (meshopt_Meshlet* pMeshlets = &meshlets[0]) |
61 | | - fixed (uint* pMeshletVertices = &meshletVertices[0]) |
62 | | - fixed (byte* pMeshletTriangles = &meshletTriangles[0]) |
63 | | - fixed (uint* pIndices = &indices[0]) |
64 | | - fixed (Vector3* pPositions = &positions[0]) |
65 | | - { |
66 | | - float* pVertsAsFloats = (float*)pPositions; |
67 | | - |
68 | | - uint meshletCount = MeshOptNative.meshopt_buildMeshlets( |
69 | | - pMeshlets, |
70 | | - pMeshletVertices, |
71 | | - pMeshletTriangles, |
72 | | - pIndices, |
73 | | - meshNumIndices, |
74 | | - pVertsAsFloats, |
75 | | - meshNumVertices, |
76 | | - (uint)sizeof(Vector3), |
77 | | - kMaxVertices, |
78 | | - kMaxTriangles, |
79 | | - kConeWeight); |
| 61 | + meshopt_Meshlet* pMeshlets = (meshopt_Meshlet*)Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(meshlets)); |
| 62 | + uint* pMeshletVertices = (uint*)Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(meshletVertices)); |
| 63 | + byte* pMeshletTriangles = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(meshletTriangles)); |
| 64 | + uint* pIndices = (uint*)Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(indices)); |
| 65 | + float* pVertsAsFloats = (float*)Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(positions)); |
80 | 66 |
|
81 | | - Console.WriteLine($"{meshletCount} meshlets generated"); |
82 | | - } |
| 67 | + uint meshletCount = MeshOptNative.meshopt_buildMeshlets( |
| 68 | + pMeshlets, |
| 69 | + pMeshletVertices, |
| 70 | + pMeshletTriangles, |
| 71 | + pIndices, |
| 72 | + meshNumIndices, |
| 73 | + pVertsAsFloats, |
| 74 | + meshNumVertices, |
| 75 | + (uint)sizeof(Vector3), |
| 76 | + kMaxVertices, |
| 77 | + kMaxTriangles, |
| 78 | + kConeWeight); |
83 | 79 |
|
| 80 | + var last = meshlets[meshletCount - 1]; |
| 81 | + Array.Resize(ref meshletVertices, (int)(last.vertex_offset + last.vertex_count)); |
| 82 | + Array.Resize(ref meshletTriangles, (int)(last.triangle_offset + ((last.triangle_count * 3 + 3) & ~3))); |
| 83 | + Array.Resize(ref meshlets, (int)meshletCount); |
84 | 84 |
|
| 85 | + Console.WriteLine($"{meshletCount} meshlets generated"); |
85 | 86 | } |
86 | 87 |
|
87 | 88 | private static IntPtr ResolveRuntimes(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) |
|
0 commit comments