Skip to content

Commit b3a6360

Browse files
committed
barebones implementation of the P/invoke wrapper
0 parents  commit b3a6360

12 files changed

+448
-0
lines changed

.gitignore

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#Junk Files
2+
*.DS_Store
3+
[Tt]humbs.db
4+
packages/*
5+
6+
#Visual Studio Files
7+
[Oo]bj
8+
[Bb]in
9+
[Dd]ebug
10+
[Bb]uild/
11+
*.user
12+
*.suo
13+
*.exe
14+
*.pdb
15+
*.aps
16+
*_i.c
17+
*_p.c
18+
*.ncb
19+
*.tlb
20+
*.tlh
21+
*.[Cc]ache
22+
*.bak
23+
*.ncb
24+
*.ilk
25+
*.log
26+
*.lib
27+
*.sbr
28+
*.sdf
29+
ipch/
30+
*.dbmdl
31+
*.csproj.user
32+
*.cache
33+
*.swp
34+
*.vspscc
35+
*.vssscc
36+
*.bak.*
37+
*.bak
38+
39+
#Tools
40+
_ReSharper.*
41+
_ReSharper*/
42+
*.resharper
43+
*.resharper.user
44+
[Nn][Dd]epend[Oo]ut*/
45+
Ankh.NoLoad
46+
47+
#Other
48+
.svn

MeshOptimizer.NET.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MeshOptimizer", "MeshOptimizer\MeshOptimizer.csproj", "{E4F045EF-D44B-4AE0-BBBC-AD05C0C9D890}"
4+
EndProject
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MeshOptimizerTests", "MeshOptimizerTests\MeshOptimizerTests.csproj", "{C8754784-739A-46F1-9C7F-1FD0238D2652}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{E4F045EF-D44B-4AE0-BBBC-AD05C0C9D890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{E4F045EF-D44B-4AE0-BBBC-AD05C0C9D890}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{E4F045EF-D44B-4AE0-BBBC-AD05C0C9D890}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{E4F045EF-D44B-4AE0-BBBC-AD05C0C9D890}.Release|Any CPU.Build.0 = Release|Any CPU
17+
{C8754784-739A-46F1-9C7F-1FD0238D2652}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{C8754784-739A-46F1-9C7F-1FD0238D2652}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{C8754784-739A-46F1-9C7F-1FD0238D2652}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{C8754784-739A-46F1-9C7F-1FD0238D2652}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
EndGlobal

MeshOptimizer/Mesh.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
3+
namespace MeshOptimizer
4+
{
5+
public class Mesh<T>
6+
{
7+
public T[] Vertices { get; set; }
8+
public uint[] Indices { get; set; }
9+
public uint VertexSize { get; }
10+
11+
public Mesh(T[] Vertices, uint[] Indices, uint VertexSize)
12+
{
13+
this.Vertices = Vertices;
14+
this.Indices = Indices;
15+
this.VertexSize = VertexSize;
16+
}
17+
}
18+
}

MeshOptimizer/MeshOptimizer.csproj

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{E4F045EF-D44B-4AE0-BBBC-AD05C0C9D890}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>MeshOptimizer</RootNamespace>
11+
<AssemblyName>MeshOptimizer</AssemblyName>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Xml" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="Mesh.cs" />
44+
<Compile Include="Optimizer.cs" />
45+
<Compile Include="MeshOptimizerNative.cs" />
46+
<Compile Include="Pointer.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
50+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
51+
Other similar extension points exist, see Microsoft.Common.targets.
52+
<Target Name="BeforeBuild">
53+
</Target>
54+
<Target Name="AfterBuild">
55+
</Target>
56+
-->
57+
</Project>

MeshOptimizer/MeshOptimizerNative.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace MeshOptimizer
5+
{
6+
internal static class MeshOptimizerNative
7+
{
8+
private const string MeshOptimizerDLL = "meshoptimizer32.dll";
9+
10+
[DllImport(MeshOptimizerDLL, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
11+
internal static extern uint meshopt_generateVertexRemap(uint[] Destination, uint[] Indices, UIntPtr IndexCount, IntPtr Vertices, UIntPtr VertexCount, UIntPtr VertexSize);
12+
13+
[DllImport(MeshOptimizerDLL, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
14+
internal static extern void meshopt_remapIndexBuffer(uint[] Destination, uint[] Indices, UIntPtr IndexCount, uint[] Remap);
15+
16+
[DllImport(MeshOptimizerDLL, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
17+
internal static extern void meshopt_remapVertexBuffer(IntPtr Destination, IntPtr Vertices, UIntPtr VertexCount, UIntPtr VertexSize, uint[] Remap);
18+
19+
/*
20+
*
21+
size_t meshopt_generateVertexRemap(unsigned int* destination, const unsigned int* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size);
22+
void meshopt_remapIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const unsigned int* remap);
23+
void meshopt_remapVertexBuffer(void* destination, const void* vertices, size_t vertex_count, size_t vertex_size, const unsigned int* remap);
24+
25+
* size_t index_count = face_count * 3;
26+
std::vector<unsigned int> remap(index_count); // allocate temporary memory for the remap table
27+
size_t vertex_count = meshopt_generateVertexRemap(&remap[0], NULL, index_count, &unindexed_vertices[0], index_count, sizeof(Vertex));
28+
meshopt_remapIndexBuffer(indices, NULL, index_count, &remap[0]);
29+
meshopt_remapVertexBuffer(vertices, &unindexed_vertices[0], index_count, sizeof(Vertex), &remap[0]);
30+
*/
31+
}
32+
}

MeshOptimizer/Optimizer.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace MeshOptimizer
5+
{
6+
public static class Optimizer
7+
{
8+
public static void Reindex<T>(Mesh<T> Target)
9+
{
10+
var remap = new uint[Target.Vertices.Length];
11+
var vertexPointer = Pointer.Create(Target.Vertices);
12+
var indexCount = (Target.Indices?.Length ?? Target.Vertices.Length);
13+
var totalVertices = MeshOptimizerNative.meshopt_generateVertexRemap(
14+
remap,
15+
Target.Indices,
16+
(UIntPtr) indexCount,
17+
vertexPointer.Address,
18+
(UIntPtr) Target.Vertices.Length,
19+
(UIntPtr) Target.VertexSize
20+
);
21+
22+
var indices = new uint[indexCount];
23+
MeshOptimizerNative.meshopt_remapIndexBuffer(indices, Target.Indices, (UIntPtr) indexCount, remap);
24+
25+
var vertices = new T[totalVertices];
26+
var targetVerticesPointer = Pointer.Create(vertices);
27+
MeshOptimizerNative.meshopt_remapVertexBuffer(targetVerticesPointer.Address, vertexPointer.Address, (UIntPtr) Target.Vertices.Length, (UIntPtr) Target.VertexSize, remap);
28+
29+
Target.Vertices = vertices;
30+
Target.Indices = indices;
31+
vertexPointer.Free();
32+
targetVerticesPointer.Free();
33+
}
34+
}
35+
}

MeshOptimizer/Pointer.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace MeshOptimizer
5+
{
6+
public class Pointer
7+
{
8+
private GCHandle _handle;
9+
public IntPtr Address { get; private set; }
10+
11+
private Pointer()
12+
{
13+
}
14+
15+
public void Free()
16+
{
17+
_handle.Free();
18+
}
19+
20+
public static Pointer Create<T>(T Object)
21+
{
22+
var pointer = new Pointer
23+
{
24+
_handle = GCHandle.Alloc(Object, GCHandleType.Pinned)
25+
};
26+
pointer.Address = pointer._handle.AddrOfPinnedObject();
27+
return pointer;
28+
}
29+
}
30+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// General Information about an assembly is controlled through the following
5+
// set of attributes. Change these attribute values to modify the information
6+
// associated with an assembly.
7+
[assembly: AssemblyTitle("MeshOptimizer")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("MeshOptimizer")]
12+
[assembly: AssemblyCopyright("Copyright © 2019")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// Setting ComVisible to false makes the types in this assembly not visible
17+
// to COM components. If you need to access a type in this assembly from
18+
// COM, set the ComVisible attribute to true on that type.
19+
[assembly: ComVisible(false)]
20+
21+
// The following GUID is for the ID of the typelib if this project is exposed to COM
22+
[assembly: Guid("E4F045EF-D44B-4AE0-BBBC-AD05C0C9D890")]
23+
24+
// Version information for an assembly consists of the following four values:
25+
//
26+
// Major Version
27+
// Minor Version
28+
// Build Number
29+
// Revision
30+
//
31+
// You can specify all the values or you can default the Build and Revision Numbers
32+
// by using the '*' as shown below:
33+
// [assembly: AssemblyVersion("1.0.*")]
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\NUnit.3.12.0\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.12.0\build\NUnit.props')" />
4+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
5+
<PropertyGroup>
6+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
7+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
8+
<ProjectGuid>{C8754784-739A-46F1-9C7F-1FD0238D2652}</ProjectGuid>
9+
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
10+
<OutputType>Library</OutputType>
11+
<AppDesignerFolder>Properties</AppDesignerFolder>
12+
<RootNamespace>MeshOptimizerTests</RootNamespace>
13+
<AssemblyName>MeshOptimizerTests</AssemblyName>
14+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<PlatformTarget>AnyCPU</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28+
<PlatformTarget>AnyCPU</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="nunit.framework, Version=3.12.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb">
38+
<HintPath>..\packages\NUnit.3.12.0\lib\net40\nunit.framework.dll</HintPath>
39+
<Private>True</Private>
40+
</Reference>
41+
<Reference Include="System" />
42+
<Reference Include="System.Core" />
43+
<Reference Include="System.Data" />
44+
<Reference Include="System.Xml" />
45+
</ItemGroup>
46+
<ItemGroup>
47+
<Compile Include="RunnerTests.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<ProjectReference Include="..\MeshOptimizer\MeshOptimizer.csproj">
52+
<Project>{e4f045ef-d44b-4ae0-bbbc-ad05c0c9d890}</Project>
53+
<Name>MeshOptimizer</Name>
54+
</ProjectReference>
55+
</ItemGroup>
56+
<ItemGroup>
57+
<None Include="packages.config" />
58+
</ItemGroup>
59+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
60+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
61+
<PropertyGroup>
62+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
63+
</PropertyGroup>
64+
<Error Condition="!Exists('..\packages\NUnit.3.12.0\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.12.0\build\NUnit.props'))" />
65+
</Target>
66+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
67+
Other similar extension points exist, see Microsoft.Common.targets.
68+
<Target Name="BeforeBuild">
69+
</Target>
70+
<Target Name="AfterBuild">
71+
</Target>
72+
-->
73+
</Project>

0 commit comments

Comments
 (0)