From a7fda8d04d48f2d16142760b943f008aae568fd9 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 4 Feb 2022 16:33:30 +0900 Subject: [PATCH 1/3] =?UTF-8?q?unittest=20=E3=81=AE=20Dispose=20=E5=91=BC?= =?UTF-8?q?=E3=81=B3=E5=BF=98=E3=82=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs | 62 ++++++++++--------- Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs | 8 +-- Assets/VRM/Tests/MToonTest.cs | 29 +++++---- .../Tests/SampleTests/VRMImportExportTests.cs | 8 +-- Assets/VRM/Tests/VRMTextureEnumerateTests.cs | 27 ++++---- 5 files changed, 71 insertions(+), 63 deletions(-) diff --git a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs index 41f2c97fa..5af60274e 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs @@ -54,10 +54,12 @@ namespace UniGLTF } }; - var data = GltfData.CreateFromGltfDataForTest(gltf, new ArraySegment(bytes)); - var (getter, len) = WeightsAccessor.GetAccessor(data, 0); - Assert.AreEqual((1.0f, 2.0f, 3.0f, 4.0f), getter(0)); - Assert.AreEqual((5.0f, 6.0f, 7.0f, 8.0f), getter(1)); + using (var data = GltfData.CreateFromGltfDataForTest(gltf, new ArraySegment(bytes))) + { + var (getter, len) = WeightsAccessor.GetAccessor(data, 0); + Assert.AreEqual((1.0f, 2.0f, 3.0f, 4.0f), getter(0)); + Assert.AreEqual((5.0f, 6.0f, 7.0f, 8.0f), getter(1)); + } } /// @@ -188,33 +190,35 @@ namespace UniGLTF : MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings) ; - var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF, data.BinBytes); + using (var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF, data.BinBytes)) { - var indices = parsed.GetIndices(gltfMesh.primitives[0].indices); - Assert.AreEqual(0, indices[0]); - Assert.AreEqual(1, indices[1]); - Assert.AreEqual(3, indices[2]); - Assert.AreEqual(3, indices[3]); - Assert.AreEqual(1, indices[4]); - Assert.AreEqual(2, indices[5]); - } - { - var positions = parsed.GetArrayFromAccessor(gltfMesh.primitives[0].attributes.POSITION); - Assert.AreEqual(4, positions.Length); - } + { + var indices = parsed.GetIndices(gltfMesh.primitives[0].indices); + Assert.AreEqual(0, indices[0]); + Assert.AreEqual(1, indices[1]); + Assert.AreEqual(3, indices[2]); + Assert.AreEqual(3, indices[3]); + Assert.AreEqual(1, indices[4]); + Assert.AreEqual(2, indices[5]); + } + { + var positions = parsed.GetArrayFromAccessor(gltfMesh.primitives[0].attributes.POSITION); + Assert.AreEqual(4, positions.Length); + } - { - var indices = parsed.GetIndices(gltfMesh.primitives[1].indices); - Assert.AreEqual(0, indices[0]); - Assert.AreEqual(1, indices[1]); - Assert.AreEqual(3, indices[2]); - Assert.AreEqual(3, indices[3]); - Assert.AreEqual(1, indices[4]); - Assert.AreEqual(2, indices[5]); - } - { - var positions = parsed.GetArrayFromAccessor(gltfMesh.primitives[1].attributes.POSITION); - Assert.AreEqual(4, positions.Length); + { + var indices = parsed.GetIndices(gltfMesh.primitives[1].indices); + Assert.AreEqual(0, indices[0]); + Assert.AreEqual(1, indices[1]); + Assert.AreEqual(3, indices[2]); + Assert.AreEqual(3, indices[3]); + Assert.AreEqual(1, indices[4]); + Assert.AreEqual(2, indices[5]); + } + { + var positions = parsed.GetArrayFromAccessor(gltfMesh.primitives[1].attributes.POSITION); + Assert.AreEqual(4, positions.Length); + } } } } diff --git a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs index a0392b6a9..dd39a0fcf 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs @@ -551,8 +551,8 @@ namespace UniGLTF Assert.AreNotEqual(gltf.nodes[0].mesh, gltf.nodes[1].mesh); // import + using (var parsed = GltfData.CreateFromExportForTest(data)) { - var parsed = GltfData.CreateFromExportForTest(data); using (var context = new ImporterContext(parsed)) using (var loaded = context.Load()) { @@ -569,8 +569,8 @@ namespace UniGLTF } // import new version + using (var parsed = GltfData.CreateFromExportForTest(data)) { - var parsed = GltfData.CreateFromExportForTest(data); using (var context = new ImporterContext(parsed)) using (var loaded = context.Load()) { @@ -621,8 +621,8 @@ namespace UniGLTF Assert.AreEqual(-1, gltf.nodes[0].mesh); // import + using (var parsed = GltfData.CreateFromExportForTest(data)) { - var parsed = GltfData.CreateFromExportForTest(data); using (var context = new ImporterContext(parsed)) using (var loaded = context.Load()) { @@ -685,8 +685,8 @@ namespace UniGLTF Assert.AreEqual(-1, gltf.nodes[1].mesh); // import + using (var parsed = GltfData.CreateFromExportForTest(data)) { - var parsed = GltfData.CreateFromExportForTest(data); using (var context = new ImporterContext(parsed)) using (var loaded = context.Load()) { diff --git a/Assets/VRM/Tests/MToonTest.cs b/Assets/VRM/Tests/MToonTest.cs index 3d208f424..dd4eec418 100644 --- a/Assets/VRM/Tests/MToonTest.cs +++ b/Assets/VRM/Tests/MToonTest.cs @@ -46,13 +46,14 @@ namespace VRM return; } - var data = new GlbFileParser(path).Parse(); - var vrm = new VRMData(data); + using (var data = new GlbFileParser(path).Parse()) + { + var vrm = new VRMData(data); + var importer = new VRMImporterContext(vrm, null); - var importer = new VRMImporterContext(vrm, null); - - Assert.AreEqual(73, vrm.Data.GLTF.materials.Count); - Assert.True(VRMMToonMaterialImporter.TryCreateParam(vrm.Data, importer.VRM, 0, out MaterialDescriptor matDesc)); + Assert.AreEqual(73, vrm.Data.GLTF.materials.Count); + Assert.True(VRMMToonMaterialImporter.TryCreateParam(vrm.Data, importer.VRM, 0, out MaterialDescriptor matDesc)); + } } static string AliciaPath @@ -68,14 +69,16 @@ namespace VRM public void MaterialImporterTest() { var path = AliciaPath; - var data = new GlbFileParser(path).Parse(); - var vrmImporter = new VRMImporterContext(new VRMData(data), null); - var materialParam = new VRMMaterialDescriptorGenerator(vrmImporter.VRM).Get(data, 0); - Assert.AreEqual("VRM/MToon", materialParam.ShaderName); - Assert.AreEqual("Alicia_body", materialParam.TextureSlots["_MainTex"].UnityObjectName); + using (var data = new GlbFileParser(path).Parse()) + { + var vrmImporter = new VRMImporterContext(new VRMData(data), null); + var materialParam = new VRMMaterialDescriptorGenerator(vrmImporter.VRM).Get(data, 0); + Assert.AreEqual("VRM/MToon", materialParam.ShaderName); + Assert.AreEqual("Alicia_body", materialParam.TextureSlots["_MainTex"].UnityObjectName); - var (key, value) = materialParam.EnumerateSubAssetKeyValue().First(); - Assert.AreEqual(new SubAssetKey(typeof(Texture), "Alicia_body"), key); + var (key, value) = materialParam.EnumerateSubAssetKeyValue().First(); + Assert.AreEqual(new SubAssetKey(typeof(Texture), "Alicia_body"), key); + } } } } diff --git a/Assets/VRM/Tests/SampleTests/VRMImportExportTests.cs b/Assets/VRM/Tests/SampleTests/VRMImportExportTests.cs index ba87552d0..95b081ba5 100644 --- a/Assets/VRM/Tests/SampleTests/VRMImportExportTests.cs +++ b/Assets/VRM/Tests/SampleTests/VRMImportExportTests.cs @@ -43,8 +43,7 @@ namespace VRM.Samples public void ImportExportTest() { var path = AliciaPath; - var data = new GlbFileParser(path).Parse(); - + using (var data = new GlbFileParser(path).Parse()) using (var context = new VRMImporterContext(new VRMData(data))) using (var loaded = context.Load()) { @@ -191,8 +190,7 @@ namespace VRM.Samples public void MeshCopyTest() { var path = AliciaPath; - var data = new GlbFileParser(path).Parse(); - + using (var data = new GlbFileParser(path).Parse()) using (var context = new VRMImporterContext(new VRMData(data))) using (var loaded = context.Load()) { @@ -212,8 +210,8 @@ namespace VRM.Samples { // Aliciaを古いデシリアライザでロードする var path = AliciaPath; - var data = new GlbFileParser(path).Parse(); + using (var data = new GlbFileParser(path).Parse()) using (var context = new VRMImporterContext(new VRMData(data))) { var oldJson = context.GLTF.ToJson().ParseAsJson().ToString(" "); diff --git a/Assets/VRM/Tests/VRMTextureEnumerateTests.cs b/Assets/VRM/Tests/VRMTextureEnumerateTests.cs index 760d6106f..9ae342c83 100644 --- a/Assets/VRM/Tests/VRMTextureEnumerateTests.cs +++ b/Assets/VRM/Tests/VRMTextureEnumerateTests.cs @@ -16,7 +16,7 @@ namespace VRM [Test] public void TextureEnumerationTest() { - { + using ( var data = GltfData.CreateFromGltfDataForTest( new glTF { @@ -60,7 +60,8 @@ namespace VRM } }, default - ); + )) + { var vrm = new glTF_VRM_extensions { materialProperties = new List @@ -89,7 +90,7 @@ namespace VRM [Test] public void TextureEnumerationInUnknownShader() { - var data = GltfData.CreateFromGltfDataForTest( + using (var data = GltfData.CreateFromGltfDataForTest( new glTF { images = new List @@ -122,10 +123,11 @@ namespace VRM } }, default - ); - var vrm = new glTF_VRM_extensions + )) { - materialProperties = new List + var vrm = new glTF_VRM_extensions + { + materialProperties = new List { new glTF_VRM_Material { @@ -136,14 +138,15 @@ namespace VRM } }, } - }; + }; - // 2系統ある? - Assert.IsTrue(VRMMToonMaterialImporter.TryCreateParam(data, vrm, 0, out VRMShaders.MaterialDescriptor matDesc)); - Assert.AreEqual(1, matDesc.TextureSlots.Count); + // 2系統ある? + Assert.IsTrue(VRMMToonMaterialImporter.TryCreateParam(data, vrm, 0, out VRMShaders.MaterialDescriptor matDesc)); + Assert.AreEqual(1, matDesc.TextureSlots.Count); - var items = new VrmTextureDescriptorGenerator(data, vrm).Get().GetEnumerable().ToArray(); - Assert.AreEqual(1, items.Length); + var items = new VrmTextureDescriptorGenerator(data, vrm).Get().GetEnumerable().ToArray(); + Assert.AreEqual(1, items.Length); + } } } } From 0d93bab5390243a431d178401376680770c94316 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 4 Feb 2022 16:33:54 +0900 Subject: [PATCH 2/3] using --- Assets/VRM10/Runtime/IO/Model/MeshImporter.cs | 69 ++++---- .../Runtime/IO/Model/MeshImporterDivided.cs | 150 +++++++++--------- 2 files changed, 111 insertions(+), 108 deletions(-) diff --git a/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs b/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs index 4f60fb8dc..7fdf0c4fe 100644 --- a/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs +++ b/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs @@ -30,45 +30,46 @@ namespace UniVRM10 var weights = src.VertexBuffer.Weights?.AsNativeArray(Allocator.TempJob) ?? default; var joints = src.VertexBuffer.Joints?.AsNativeArray(Allocator.TempJob) ?? default; - var vertices = new NativeArray(positions.Length, Allocator.TempJob); - - // JobとBindPoseの更新を並行して行う - var jobHandle = - new InterleaveMeshVerticesJob(vertices, positions, normals, texCoords, colors, weights, joints) - .Schedule(vertices.Length, 1); - JobHandle.ScheduleBatchedJobs(); - - // BindPoseを更新 - if (weights.IsCreated && joints.IsCreated) + using (var vertices = new NativeArray(positions.Length, Allocator.TempJob)) { - if (weights.Length != positions.Length || joints.Length != positions.Length) + + // JobとBindPoseの更新を並行して行う + var jobHandle = + new InterleaveMeshVerticesJob(vertices, positions, normals, texCoords, colors, weights, joints) + .Schedule(vertices.Length, 1); + JobHandle.ScheduleBatchedJobs(); + + // BindPoseを更新 + if (weights.IsCreated && joints.IsCreated) { - throw new ArgumentException(); - } - if (skin != null) - { - mesh.bindposes = skin.InverseMatrices.GetSpan().ToArray(); + if (weights.Length != positions.Length || joints.Length != positions.Length) + { + throw new ArgumentException(); + } + if (skin != null) + { + mesh.bindposes = skin.InverseMatrices.GetSpan().ToArray(); + } } + + // Jobを完了 + jobHandle.Complete(); + + // 入力のNativeArrayを開放 + positions.Dispose(); + if (normals.IsCreated) normals.Dispose(); + if (texCoords.IsCreated) texCoords.Dispose(); + if (colors.IsCreated) colors.Dispose(); + if (weights.IsCreated) weights.Dispose(); + if (joints.IsCreated) joints.Dispose(); + + // 頂点を更新 + MeshVertex.SetVertexBufferParamsToMesh(mesh, vertices.Length); + mesh.SetVertexBufferData(vertices, 0, 0, vertices.Length); + + // 出力のNativeArrayを開放 } - // Jobを完了 - jobHandle.Complete(); - - // 入力のNativeArrayを開放 - positions.Dispose(); - if (normals.IsCreated) normals.Dispose(); - if (texCoords.IsCreated) texCoords.Dispose(); - if (colors.IsCreated) colors.Dispose(); - if (weights.IsCreated) weights.Dispose(); - if (joints.IsCreated) joints.Dispose(); - - // 頂点を更新 - MeshVertex.SetVertexBufferParamsToMesh(mesh, vertices.Length); - mesh.SetVertexBufferData(vertices, 0, 0, vertices.Length); - - // 出力のNativeArrayを開放 - vertices.Dispose(); - // Indexを更新 switch (src.IndexBuffer.ComponentType) { diff --git a/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs b/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs index 53577d6fe..f27badeea 100644 --- a/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs +++ b/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs @@ -57,40 +57,42 @@ namespace UniVRM10 normalsCount += morphTarget.VertexBuffer.Normals?.Count ?? morphTarget.VertexBuffer.Count; } - var blendShapePositions = new NativeArray(positionsCount, Allocator.Temp); - var blendShapeNormals = new NativeArray(normalsCount, Allocator.Temp); - - var blendShapePositionOffset = 0; - var blendShapeNormalOffset = 0; - foreach (var mesh in meshGroup.Meshes) + using (var blendShapePositions = new NativeArray(positionsCount, Allocator.Temp)) + using (var blendShapeNormals = new NativeArray(normalsCount, Allocator.Temp)) { - var morphTarget = mesh.MorphTargets[i]; - morphTarget.VertexBuffer.Positions.CopyToNativeSlice( - new NativeSlice( - blendShapePositions, - blendShapePositionOffset, - morphTarget.VertexBuffer.Positions.Count - ) - ); - // nullならdefault(0)のまま - morphTarget.VertexBuffer.Normals?.CopyToNativeSlice( - new NativeSlice( - blendShapeNormals, - blendShapeNormalOffset, - morphTarget.VertexBuffer.Normals.Count - ) - ); + var blendShapePositionOffset = 0; + var blendShapeNormalOffset = 0; + foreach (var mesh in meshGroup.Meshes) + { + var morphTarget = mesh.MorphTargets[i]; + morphTarget.VertexBuffer.Positions.CopyToNativeSlice( + new NativeSlice( + blendShapePositions, + blendShapePositionOffset, + morphTarget.VertexBuffer.Positions.Count + ) + ); - blendShapePositionOffset += morphTarget.VertexBuffer.Positions.Count; - blendShapeNormalOffset += morphTarget.VertexBuffer.Normals?.Count ?? morphTarget.VertexBuffer.Count; + // nullならdefault(0)のまま + morphTarget.VertexBuffer.Normals?.CopyToNativeSlice( + new NativeSlice( + blendShapeNormals, + blendShapeNormalOffset, + morphTarget.VertexBuffer.Normals.Count + ) + ); + + blendShapePositionOffset += morphTarget.VertexBuffer.Positions.Count; + blendShapeNormalOffset += morphTarget.VertexBuffer.Normals?.Count ?? morphTarget.VertexBuffer.Count; + } + + resultMesh.AddBlendShapeFrame(meshGroup.Meshes[0].MorphTargets[i].Name, + 100.0f, + blendShapePositions.ToArray(), + blendShapeNormals.ToArray(), + null); } - - resultMesh.AddBlendShapeFrame(meshGroup.Meshes[0].MorphTargets[i].Name, - 100.0f, - blendShapePositions.ToArray(), - blendShapeNormals.ToArray(), - null); } Profiler.EndSample(); @@ -122,29 +124,29 @@ namespace UniVRM10 switch (mesh.IndexBuffer.ComponentType) { case AccessorValueType.SHORT: - { - // unsigned short -> unsigned short - var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob); - disposables.Add(source); - jobHandle = new CopyIndicesJobs.Ushort2Ushort( - (ushort)vertexOffset, - new NativeSlice(source), - new NativeSlice(indices, indexOffset, mesh.IndexBuffer.Count)) - .Schedule(mesh.IndexBuffer.Count, 1, jobHandle); - break; - } + { + // unsigned short -> unsigned short + var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob); + disposables.Add(source); + jobHandle = new CopyIndicesJobs.Ushort2Ushort( + (ushort)vertexOffset, + new NativeSlice(source), + new NativeSlice(indices, indexOffset, mesh.IndexBuffer.Count)) + .Schedule(mesh.IndexBuffer.Count, 1, jobHandle); + break; + } case AccessorValueType.UNSIGNED_INT: - { - // unsigned int -> unsigned short - var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob); - disposables.Add(source); - jobHandle = new CopyIndicesJobs.Uint2Ushort( - (ushort)vertexOffset, - source, - new NativeSlice(indices, indexOffset, mesh.IndexBuffer.Count)) - .Schedule(mesh.IndexBuffer.Count, 1, jobHandle); - break; - } + { + // unsigned int -> unsigned short + var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob); + disposables.Add(source); + jobHandle = new CopyIndicesJobs.Uint2Ushort( + (ushort)vertexOffset, + source, + new NativeSlice(indices, indexOffset, mesh.IndexBuffer.Count)) + .Schedule(mesh.IndexBuffer.Count, 1, jobHandle); + break; + } default: throw new ArgumentOutOfRangeException(); } @@ -169,29 +171,29 @@ namespace UniVRM10 switch (mesh.IndexBuffer.ComponentType) { case AccessorValueType.SHORT: - { - // unsigned short -> unsigned int - var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob); - disposables.Add(source); - jobHandle = new CopyIndicesJobs.Ushort2Uint( - (uint)vertexOffset, - source, - new NativeSlice(indices, indexOffset, mesh.IndexBuffer.Count)) - .Schedule(mesh.IndexBuffer.Count, 1, jobHandle); - break; - } + { + // unsigned short -> unsigned int + var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob); + disposables.Add(source); + jobHandle = new CopyIndicesJobs.Ushort2Uint( + (uint)vertexOffset, + source, + new NativeSlice(indices, indexOffset, mesh.IndexBuffer.Count)) + .Schedule(mesh.IndexBuffer.Count, 1, jobHandle); + break; + } case AccessorValueType.UNSIGNED_INT: - { - // unsigned int -> unsigned int - var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob); - disposables.Add(source); - jobHandle = new CopyIndicesJobs.UInt2UInt( - (uint)vertexOffset, - source, - new NativeSlice(indices, indexOffset, mesh.IndexBuffer.Count)) - .Schedule(mesh.IndexBuffer.Count, 1, jobHandle); - break; - } + { + // unsigned int -> unsigned int + var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob); + disposables.Add(source); + jobHandle = new CopyIndicesJobs.UInt2UInt( + (uint)vertexOffset, + source, + new NativeSlice(indices, indexOffset, mesh.IndexBuffer.Count)) + .Schedule(mesh.IndexBuffer.Count, 1, jobHandle); + break; + } default: throw new ArgumentOutOfRangeException(); } From 285e393ca81f4992a147ebc799aac48114328a85 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 4 Feb 2022 16:34:18 +0900 Subject: [PATCH 3/3] fix Dispose --- Assets/VRM10/Runtime/Migration/MigrationVrm.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrm.cs b/Assets/VRM10/Runtime/Migration/MigrationVrm.cs index 83dd2bf0f..dbbd89c0d 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrm.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrm.cs @@ -12,8 +12,10 @@ namespace UniVRM10 { public static byte[] Migrate(byte[] src) { - var data = new GlbBinaryParser(src, "migration").Parse(); - return Migrate(data); + using (var data = new GlbBinaryParser(src, "migration").Parse()) + { + return Migrate(data); + } } static (int, int) GetVertexRange(SpanLike indices)