add tile cache compressor factory shared

This commit is contained in:
ikpil 2023-08-19 15:31:51 +09:00
parent 2050c476a2
commit 4be459b296
32 changed files with 158 additions and 188 deletions

View File

@ -127,7 +127,7 @@ namespace DotRecast.Detour.TileCache
header.hmin = layer.hmin;
header.hmax = layer.hmax;
var comp = _compFactory.Get(storageParams.Compatibility);
var comp = _compFactory.Create(storageParams.Compatibility ? 0 : 1);
var bytes = builder.CompressTileCacheLayer(header, layer.heights, layer.areas, layer.cons, storageParams.Order, storageParams.Compatibility, comp);
result.Add(bytes);
}

View File

@ -0,0 +1,37 @@
using System.Collections.Generic;
using DotRecast.Core;
namespace DotRecast.Detour.TileCache.Io.Compress
{
public class DtTileCacheCompressorFactory : IDtTileCacheCompressorFactory
{
public static readonly DtTileCacheCompressorFactory Shared = new DtTileCacheCompressorFactory();
private readonly Dictionary<int, IRcCompressor> _compressors = new Dictionary<int, IRcCompressor>();
public bool TryAdd(int type, IRcCompressor compressor)
{
if (0 == type)
return false;
_compressors[type] = compressor;
return true;
}
public IRcCompressor Create(int compatibility)
{
// default
if (0 == compatibility)
{
return DtTileCacheFastLzCompressor.Shared;
}
if (!_compressors.TryGetValue(compatibility, out var comp))
{
return null;
}
return comp;
}
}
}

View File

@ -24,6 +24,6 @@ namespace DotRecast.Detour.TileCache.Io.Compress
{
public interface IDtTileCacheCompressorFactory
{
IRcCompressor Get(bool cCompatibility);
IRcCompressor Create(int compatibility);
}
}

View File

@ -70,7 +70,7 @@ namespace DotRecast.Detour.TileCache.Io
header.meshParams = paramReader.Read(bb);
header.cacheParams = ReadCacheParams(bb, cCompatibility);
DtNavMesh mesh = new DtNavMesh(header.meshParams, maxVertPerPoly);
IRcCompressor comp = _compFactory.Get(cCompatibility);
IRcCompressor comp = _compFactory.Create(cCompatibility ? 0 : 1);
DtTileCacheStorageParams storageParams = new DtTileCacheStorageParams(bb.Order(), cCompatibility);
DtTileCache tc = new DtTileCache(header.cacheParams, storageParams, mesh, comp, meshProcessor);
// Read tiles.

View File

@ -63,7 +63,7 @@ namespace DotRecast.Detour.TileCache.Io
Write(stream, (int)cache.GetTileRef(tile), order);
byte[] data = tile.data;
DtTileCacheLayer layer = cache.DecompressTile(tile);
var comp = _compFactory.Get(cCompatibility);
var comp = _compFactory.Create(cCompatibility ? 0 : 1);
data = builder.CompressTileCacheLayer(comp, layer, order, cCompatibility);
Write(stream, data.Length, order);
stream.Write(data);

View File

@ -1,25 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.6" />
<PackageReference Include="Silk.NET" Version="2.17.1" />
<PackageReference Include="Silk.NET.OpenGL.Extensions.ImGui" Version="2.17.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.6" />
<PackageReference Include="Silk.NET" Version="2.17.1" />
<PackageReference Include="Silk.NET.OpenGL.Extensions.ImGui" Version="2.17.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotRecast.Recast.Toolset\DotRecast.Recast.Toolset.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotRecast.Recast.Toolset\DotRecast.Recast.Toolset.csproj" />
</ItemGroup>
</Project>

View File

@ -39,34 +39,34 @@ public class ModernOpenGLDraw : IOpenGLDraw
{
string SHADER_VERSION = "#version 400\n";
string vertex_shader = SHADER_VERSION + "uniform mat4 ProjMtx;\n" //
+ "uniform mat4 ViewMtx;\n" //
+ "in vec3 Position;\n" //
+ "in vec2 TexCoord;\n" //
+ "in vec4 Color;\n" //
+ "out vec2 Frag_UV;\n" //
+ "out vec4 Frag_Color;\n" //
+ "out float Frag_Depth;\n" //
+ "void main() {\n" //
+ " Frag_UV = TexCoord;\n" //
+ " Frag_Color = Color;\n" //
+ " vec4 VSPosition = ViewMtx * vec4(Position, 1);\n" //
+ " Frag_Depth = -VSPosition.z;\n" //
+ " gl_Position = ProjMtx * VSPosition;\n" //
+ "}\n";
+ "uniform mat4 ViewMtx;\n" //
+ "in vec3 Position;\n" //
+ "in vec2 TexCoord;\n" //
+ "in vec4 Color;\n" //
+ "out vec2 Frag_UV;\n" //
+ "out vec4 Frag_Color;\n" //
+ "out float Frag_Depth;\n" //
+ "void main() {\n" //
+ " Frag_UV = TexCoord;\n" //
+ " Frag_Color = Color;\n" //
+ " vec4 VSPosition = ViewMtx * vec4(Position, 1);\n" //
+ " Frag_Depth = -VSPosition.z;\n" //
+ " gl_Position = ProjMtx * VSPosition;\n" //
+ "}\n";
string fragment_shader = SHADER_VERSION + "precision mediump float;\n" //
+ "uniform sampler2D Texture;\n" //
+ "uniform float UseTexture;\n" //
+ "uniform float EnableFog;\n" //
+ "uniform float FogStart;\n" //
+ "uniform float FogEnd;\n" //
+ "const vec4 FogColor = vec4(0.3f, 0.3f, 0.32f, 1.0f);\n" //
+ "in vec2 Frag_UV;\n" //
+ "in vec4 Frag_Color;\n" //
+ "in float Frag_Depth;\n" //
+ "out vec4 Out_Color;\n" //
+ "void main(){\n" //
+ " Out_Color = mix(FogColor, Frag_Color * mix(vec4(1), texture(Texture, Frag_UV.st), UseTexture), 1.0 - EnableFog * clamp( (Frag_Depth - FogStart) / (FogEnd - FogStart), 0.0, 1.0) );\n" //
+ "}\n";
+ "uniform sampler2D Texture;\n" //
+ "uniform float UseTexture;\n" //
+ "uniform float EnableFog;\n" //
+ "uniform float FogStart;\n" //
+ "uniform float FogEnd;\n" //
+ "const vec4 FogColor = vec4(0.3f, 0.3f, 0.32f, 1.0f);\n" //
+ "in vec2 Frag_UV;\n" //
+ "in vec4 Frag_Color;\n" //
+ "in float Frag_Depth;\n" //
+ "out vec4 Out_Color;\n" //
+ "void main(){\n" //
+ " Out_Color = mix(FogColor, Frag_Color * mix(vec4(1), texture(Texture, Frag_UV.st), UseTexture), 1.0 - EnableFog * clamp( (Frag_Depth - FogStart) / (FogEnd - FogStart), 0.0, 1.0) );\n" //
+ "}\n";
program = _gl.CreateProgram();
uint vert_shdr = _gl.CreateShader(GLEnum.VertexShader);

View File

@ -8,12 +8,23 @@ namespace DotRecast.Recast.Demo.Draw;
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public struct OpenGLVertex
{
[FieldOffset(0)] private readonly float x;
[FieldOffset(4)] private readonly float y;
[FieldOffset(8)] private readonly float z;
[FieldOffset(12)] private readonly float u;
[FieldOffset(16)] private readonly float v;
[FieldOffset(20)] private readonly int color;
[FieldOffset(0)]
private readonly float x;
[FieldOffset(4)]
private readonly float y;
[FieldOffset(8)]
private readonly float z;
[FieldOffset(12)]
private readonly float u;
[FieldOffset(16)]
private readonly float v;
[FieldOffset(20)]
private readonly int color;
public OpenGLVertex(RcVec3f pos, RcVec2f uv, int color) :
this(pos.x, pos.y, pos.z, uv.x, uv.y, color)

View File

@ -23,7 +23,6 @@ using System.Collections.Generic;
using System.Numerics;
using DotRecast.Core;
using DotRecast.Detour;
using DotRecast.Recast.Toolset.Builder;
using Silk.NET.OpenGL;

View File

@ -1,21 +0,0 @@
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io.Compress;
namespace DotRecast.Recast.Demo;
public class DtTileCacheCompressorDemoFactory : IDtTileCacheCompressorFactory
{
public static readonly DtTileCacheCompressorDemoFactory Shared = new();
private DtTileCacheCompressorDemoFactory()
{
}
public IRcCompressor Get(bool cCompatibility)
{
if (cCompatibility)
return DtTileCacheFastLzCompressor.Shared;
return DtTileCacheLZ4DemoCompressor.Shared;
}
}

View File

@ -1,49 +0,0 @@
/*
Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using DotRecast.Core;
using K4os.Compression.LZ4;
namespace DotRecast.Recast.Demo
{
public class DtTileCacheLZ4DemoCompressor : IRcCompressor
{
public static readonly DtTileCacheLZ4DemoCompressor Shared = new();
private DtTileCacheLZ4DemoCompressor()
{
}
public byte[] Decompress(byte[] buf)
{
return LZ4Pickler.Unpickle(buf);
}
public byte[] Decompress(byte[] buf, int offset, int len, int outputlen)
{
return LZ4Pickler.Unpickle(buf, offset, len);
}
public byte[] Compress(byte[] buf)
{
return LZ4Pickler.Pickle(buf);
}
}
}

View File

@ -2,5 +2,4 @@
public class IRecastDemoMessage
{
}

View File

@ -2,5 +2,4 @@
public class NavMeshBuildBeganEvent : IRecastDemoMessage
{
}

View File

@ -2,5 +2,4 @@
public class NavMeshSaveBeganEvent : IRecastDemoMessage
{
}

View File

@ -24,7 +24,6 @@ using DotRecast.Core;
using DotRecast.Detour;
using DotRecast.Detour.Crowd;
using DotRecast.Detour.Crowd.Tracking;
using DotRecast.Recast.Toolset.Builder;
using DotRecast.Recast.Demo.Draw;
using DotRecast.Recast.Toolset;

View File

@ -14,14 +14,14 @@ public class BoxGizmo : IColliderGizmo
private static readonly RcVec3f[] VERTS =
{
RcVec3f.Of( -1f, -1f, -1f),
RcVec3f.Of( 1f, -1f, -1f),
RcVec3f.Of( 1f, -1f, 1f),
RcVec3f.Of( -1f, -1f, 1f),
RcVec3f.Of( -1f, 1f, -1f),
RcVec3f.Of( 1f, 1f, -1f),
RcVec3f.Of( 1f, 1f, 1f),
RcVec3f.Of( -1f, 1f, 1f),
RcVec3f.Of(-1f, -1f, -1f),
RcVec3f.Of(1f, -1f, -1f),
RcVec3f.Of(1f, -1f, 1f),
RcVec3f.Of(-1f, -1f, 1f),
RcVec3f.Of(-1f, 1f, -1f),
RcVec3f.Of(1f, 1f, -1f),
RcVec3f.Of(1f, 1f, 1f),
RcVec3f.Of(-1f, 1f, 1f),
};
private readonly float[] vertices = new float[8 * 3];

View File

@ -1,6 +1,5 @@
using DotRecast.Core;
using DotRecast.Recast.Demo.Draw;
using static DotRecast.Core.RcMath;
using static DotRecast.Recast.Demo.Tools.Gizmos.GizmoHelper;

View File

@ -1,6 +1,5 @@
using DotRecast.Core;
using DotRecast.Recast.Demo.Draw;
using static DotRecast.Core.RcMath;
using static DotRecast.Recast.Demo.Tools.Gizmos.GizmoHelper;

View File

@ -1,4 +1,5 @@
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io.Compress;
using DotRecast.Recast.Demo.Draw;
using DotRecast.Recast.Toolset;
using DotRecast.Recast.Toolset.Tools;
@ -16,7 +17,7 @@ public class ObstacleTool : IRcTool
public ObstacleTool()
{
_impl = new(DtTileCacheCompressorDemoFactory.Shared);
_impl = new(DtTileCacheCompressorFactory.Shared);
}
public ISampleTool GetTool()

View File

@ -78,7 +78,7 @@ public class RcLogView : IRcView
var width = _canvas.Size.X - (otherWidth * 2);
//var posX = _canvas.Size.X - width;
// ImGui.SetNextWindowPos(new Vector2(otherWidth1, _canvas.Size.Y - height));
ImGui.SetWindowSize(new Vector2(width, height));
ImGui.SetWindowSize(new Vector2(width, height));
}

View File

@ -67,7 +67,7 @@ public class AbstractTileCacheTest
navMeshParams.maxPolys = 16384;
var navMesh = new DtNavMesh(navMeshParams, 6);
var comp = DtTileCacheCompressorForTestFactory.Shared.Get(cCompatibility);
var comp = DtTileCacheCompressorFactory.Shared.Create(cCompatibility ? 0 : 1);
var storageParams = new DtTileCacheStorageParams(order, cCompatibility);
var process = new TestTileCacheMeshProcess();
DtTileCache tc = new DtTileCache(option, storageParams, navMesh, comp, process);

View File

@ -1,21 +0,0 @@
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io.Compress;
namespace DotRecast.Detour.TileCache.Test.Io;
public class DtTileCacheCompressorForTestFactory : IDtTileCacheCompressorFactory
{
public static readonly DtTileCacheCompressorForTestFactory Shared = new();
private DtTileCacheCompressorForTestFactory()
{
}
public IRcCompressor Get(bool cCompatibility)
{
if (cCompatibility)
return DtTileCacheFastLzCompressor.Shared;
return DtTileCacheLZ4ForTestCompressor.Shared;
}
}

View File

@ -22,6 +22,7 @@ using System;
using System.IO;
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io;
using DotRecast.Detour.TileCache.Io.Compress;
using NUnit.Framework;
namespace DotRecast.Detour.TileCache.Test.Io;
@ -29,7 +30,7 @@ namespace DotRecast.Detour.TileCache.Test.Io;
[Parallelizable]
public class TileCacheReaderTest
{
private readonly DtTileCacheReader reader = new DtTileCacheReader(DtTileCacheCompressorForTestFactory.Shared);
private readonly DtTileCacheReader reader = new DtTileCacheReader(DtTileCacheCompressorFactory.Shared);
[Test]
public void TestNavmesh()

View File

@ -22,6 +22,7 @@ using System.Collections.Generic;
using System.IO;
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io;
using DotRecast.Detour.TileCache.Io.Compress;
using DotRecast.Recast;
using DotRecast.Recast.Geom;
using NUnit.Framework;
@ -31,8 +32,8 @@ namespace DotRecast.Detour.TileCache.Test.Io;
[Parallelizable]
public class TileCacheReaderWriterTest : AbstractTileCacheTest
{
private readonly DtTileCacheReader reader = new DtTileCacheReader(DtTileCacheCompressorForTestFactory.Shared);
private readonly DtTileCacheWriter writer = new DtTileCacheWriter(DtTileCacheCompressorForTestFactory.Shared);
private readonly DtTileCacheReader reader = new DtTileCacheReader(DtTileCacheCompressorFactory.Shared);
private readonly DtTileCacheWriter writer = new DtTileCacheWriter(DtTileCacheCompressorFactory.Shared);
[Test]
public void TestFastLz()

View File

@ -56,7 +56,7 @@ public class TestTileLayerBuilder : DtTileCacheLayerBuilder
public readonly int tw;
public readonly int th;
public TestTileLayerBuilder(IInputGeomProvider geom) : base(DtTileCacheCompressorForTestFactory.Shared)
public TestTileLayerBuilder(IInputGeomProvider geom) : base(DtTileCacheCompressorFactory.Shared)
{
_geom = geom;
_cfg = new RcConfig(true, m_tileSize, m_tileSize, RcConfig.CalcBorder(AgentRadius, CellSize),

View File

@ -22,6 +22,7 @@ using System.Collections.Generic;
using System.IO;
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io;
using DotRecast.Detour.TileCache.Io.Compress;
using DotRecast.Detour.TileCache.Test.Io;
using NUnit.Framework;
@ -39,7 +40,7 @@ public class TileCacheFindPathTest : AbstractTileCacheTest
{
using var msis = new MemoryStream(Loader.ToBytes("dungeon_all_tiles_tilecache.bin"));
using var @is = new BinaryReader(msis);
DtTileCache tcC = new DtTileCacheReader(DtTileCacheCompressorForTestFactory.Shared).Read(@is, 6, new TestTileCacheMeshProcess());
DtTileCache tcC = new DtTileCacheReader(DtTileCacheCompressorFactory.Shared).Read(@is, 6, new TestTileCacheMeshProcess());
navmesh = tcC.GetNavMesh();
query = new DtNavMeshQuery(navmesh);
}

View File

@ -0,0 +1,16 @@
using DotRecast.Detour.TileCache.Io.Compress;
using DotRecast.Detour.TileCache.Test.Io;
using NUnit.Framework;
namespace DotRecast.Detour.TileCache.Test;
[SetUpFixture]
public class TileCacheTestSetUpFixture
{
[OneTimeSetUp]
public void OneTimeSetUp()
{
// add lz4
DtTileCacheCompressorFactory.Shared.TryAdd(1, DtTileCacheLZ4ForTestCompressor.Shared);
}
}