m2wasabi / VRMQuickMetaLoader

load quick VRM.Meta information
MIT License
24 stars 1 forks source link

Unity 2019.4.12f1 native pointer issue with JobMetaLoader #3

Open emilianavt opened 4 years ago

emilianavt commented 4 years ago

Hi!

Thank you for your work on this! I have tested the MetaLoader class and found a 3.6x speedup compared to using VRMImporterContext.

I also wanted to try the jobs based loader, but it refused to run due to the unsafe native pointer restriction introduced in Unity. After decorating the loader pointers with [NativeDisableUnsafePtrRestrictionAttribute] (documentation), there no longer was an error, but instead Unity crashed. Do you think it is possible to make the JobMetaLoader work on the current Unity version?

I added [NativeDisableUnsafePtrRestrictionAttribute] before these lines:

https://github.com/m2wasabi/VRMQuickMetaLoader/blob/2db8c3e68e63aee55627c8dbd06bf7ac6df76aef/Assets/VRM.QuickMetaLoader/Scripts/JobMetaLoaderInternal.cs#L130

https://github.com/m2wasabi/VRMQuickMetaLoader/blob/2db8c3e68e63aee55627c8dbd06bf7ac6df76aef/Assets/VRM.QuickMetaLoader/Scripts/JobMetaLoaderInternal.cs#L208

m2wasabi commented 3 years ago

Hello emilianavt , thanks your comment.

JobMetaLoader using unsafe codes. To enable unsafe features, please install asmdef file.

VRMQuickMetaLoader.0.1.0.asmdef.unitypackage

emilianavt commented 3 years ago

Thank you for your response!

I have imported it now, but when running the following code:

using(var metaLoader = new JobMetaLoader(filename, preloadThumbnail: true))
{
    VRMMetaObject meta = metaLoader.Read();
    nameText.text = meta.Title;
    thumbnail.texture =  metaLoader.LoadThumbnail();
}

I get the following exception:

InvalidOperationException: InterpretMagicAndLoadJsonJob.loader uses unsafe Pointers which is not allowed. Unsafe Pointers can lead to crashes and no safety against race conditions can be provided.
If you really need to use unsafe pointers, you can disable this check using [NativeDisableUnsafePtrRestriction].
Unity.Jobs.LowLevel.Unsafe.JobsUtility.CreateJobReflectionData (System.Type type, Unity.Jobs.LowLevel.Unsafe.JobType jobType, System.Object managedJobFunction0, System.Object managedJobFunction1, System.Object managedJobFunction2) (at <a4546799a4ab426788e4204c69507924>:0)
Unity.Jobs.IJobExtensions+JobStruct`1[T].Initialize () (at <a4546799a4ab426788e4204c69507924>:0)
Unity.Jobs.IJobExtensions.Schedule[T] (T jobData, Unity.Jobs.JobHandle dependsOn) (at <a4546799a4ab426788e4204c69507924>:0)
VRM.QuickMetaLoader.JobMetaLoaderInternal.ScheduleJob (VRM.QuickMetaLoader.JobMetaLoaderInternal* answer) (at Assets/VRM.QuickMetaLoader/Scripts/JobMetaLoaderInternal.cs:103)
VRM.QuickMetaLoader.JobMetaLoaderInternal.Create (System.String path) (at Assets/VRM.QuickMetaLoader/Scripts/JobMetaLoaderInternal.cs:86)
VRM.QuickMetaLoader.JobMetaLoader..ctor (System.String path, System.Boolean preloadThumbnail) (at Assets/VRM.QuickMetaLoader/Scripts/JobMetaLoader.cs:25)
AvatarSelect.Start () (at Assets/VSeeFace/AvatarSelect.cs:37)

The asmdef is installed and Allow 'unsafe' code is also enabled in the player settings for other components.