Skip to content

Conversation

Kibsgaard
Copy link

Fixes racing issues with vertex colored or skinned models that have submeshes.

Loading this model will currently give two errors and abort the load.
Model: SkinnedVertexColor.zip
Preview:
image

Platform: Windows, Editor, Runtime loading

Bones issue:

InvalidOperationException: The previously scheduled job RenormalizeBoneWeightsJob writes to the Unity.Collections.NativeArray`1[GLTFast.Vertex.VBones] RenormalizeBoneWeightsJob.bones. You must call JobHandle.Complete() on the job RenormalizeBoneWeightsJob, before you can read from the Unity.Collections.NativeArray`1[GLTFast.Vertex.VBones] safely.
Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <fc22648a2f8c48a38d6136ea0e187002>:0)
Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <fc22648a2f8c48a38d6136ea0e187002>:0)
Unity.Collections.LowLevel.Unsafe.NativeArrayUnsafeUtility.GetUnsafeReadOnlyPtr[T] (Unity.Collections.NativeArray`1[T] nativeArray) (at <fc22648a2f8c48a38d6136ea0e187002>:0)
GLTFast.VertexBufferBones.ScheduleVertexBonesJob (System.Int32 weightsAccessorIndex, System.Int32 jointsAccessorIndex, System.Int32 offset, GLTFast.IGltfBuffers buffers) (at ./Packages/glTFast/Packages/com.unity.cloud.gltfast/Runtime/Scripts/VertexBufferBones.cs:47)
GLTFast.VertexBufferGenerator`1[TMainBuffer].ScheduleVertexBonesJobs (GLTFast.Schema.Attributes att, System.Int32 i, Unity.Collections.NativeArray`1[T] handles, System.Int32 handleIndex) (at ./Packages/glTFast/Packages/com.unity.cloud.gltfast/Runtime/Scripts/VertexBufferGenerator.cs:373)
GLTFast.VertexBufferGenerator`1[TMainBuffer].CreateVertexBuffer () (at ./Packages/glTFast/Packages/com.unity.cloud.gltfast/Runtime/Scripts/VertexBufferGenerator.cs:192)
GLTFast.MeshGenerator.GenerateMesh (GLTFast.GltfImportBase gltfImport) (at ./Packages/glTFast/Packages/com.unity.cloud.gltfast/Runtime/Scripts/MeshGenerator.cs:189)
Rethrow as AggregateException: One or more errors occurred. (The previously scheduled job Renormaliz

Vertex Color issue:

InvalidOperationException: The previously scheduled job ConvertColorsRgbaUInt8ToRGBAFloatJob writes to the Unity.Collections.NativeArray`1[Unity.Mathematics.float4] ConvertColorsRgbaUInt8ToRGBAFloatJob.result. You are trying to schedule a new job ConvertColorsRgbaUInt8ToRGBAFloatJob, which writes to the same Unity.Collections.NativeArray`1[Unity.Mathematics.float4] (via ConvertColorsRgbaUInt8ToRGBAFloatJob.result). To guarantee safety, you must include ConvertColorsRgbaUInt8ToRGBAFloatJob as a dependency of the newly scheduled job.
Unity.Jobs.LowLevel.Unsafe.JobsUtility.ScheduleParallelFor (Unity.Jobs.LowLevel.Unsafe.JobsUtility+JobScheduleParameters& parameters, System.Int32 arrayLength, System.Int32 innerloopBatchCount) (at <fc22648a2f8c48a38d6136ea0e187002>:0)
Unity.Jobs.IJobParallelForExtensions.Schedule[T] (T jobData, System.Int32 arrayLength, System.Int32 innerloopBatchCount, Unity.Jobs.JobHandle dependsOn) (at <fc22648a2f8c48a38d6136ea0e187002>:0)
GLTFast.VertexBufferColors.GetColors32Job (System.Void* input, GLTFast.Schema.GltfComponentType inputType, GLTFast.Schema.GltfAccessorAttributeType attributeType, System.Int32 inputByteStride, Unity.Collections.NativeSlice`1[T] output) (at ./Packages/glTFast/Packages/com.unity.cloud.gltfast/Runtime/Scripts/VertexBufferColors.cs:155)
GLTFast.VertexBufferColors.ScheduleVertexColorJob (System.Int32 colorAccessorIndex, System.Int32 offset, Unity.Collections.NativeSlice`1[T] handles, GLTFast.IGltfBuffers buffers) (at ./Packages/glTFast/Packages/com.unity.cloud.gltfast/Runtime/Scripts/VertexBufferColors.cs:46)
GLTFast.VertexBufferGenerator`1[TMainBuffer].ScheduleColorsJobs (GLTFast.Schema.Attributes att, System.Int32 i, Unity.Collections.NativeArray`1[T] handles, System.Int32& handleIndex) (at ./Packages/glTFast/Packages/com.unity.cloud.gltfast/Runtime/Scripts/VertexBufferGenerator.cs:356)

The cause is trying to reuse the results NativeArray for multiple submeshes, without having the previous job as a dependency.

These changes fixes that. As the submeshes always write to different parts of the NativeArray, I decided to use pointers directly similar to the other vertex jobs instead of adding dependencies to previous job handles.

A semi-related issue is having vertex colors on a skinned model gives this warning:

Skinned mesh attributes use wrong streams: TexCoord0 should use stream 1 (was 2), TexCoord1 should use stream 1 (was 2), TexCoord2 should use stream 1 (was 2), TexCoord3 should use stream 1 (was 2), TexCoord4 should use stream 1 (was 2), TexCoord5 should use stream 1 (was 2), TexCoord6 should use stream 1 (was 2), TexCoord7 should use stream 1 (was 2), BlendWeight should use stream 2 (was 3), BlendIndices should use stream 2 (was 3)
UnityEngine.Mesh:SetVertexBufferParams (int,UnityEngine.Rendering.VertexAttributeDescriptor[])
GLTFast.VertexBufferGenerator`1<GLTFast.Vertex.VPosNorm>:ApplyOnMesh (UnityEngine.Mesh,UnityEngine.Rendering.MeshUpdateFlags) (at ./Packages/glTFast/Packages/com.unity.cloud.gltfast/Runtime/Scripts/VertexBufferGenerator.cs:448)

It still loads fine, but I guess the warning means Unity is doing some extra work behind the scene to fix the streams. I tried to fix it, but couldn't find a solution to that. The same model can be use for testing this issue.

@unity-cla-assistant
Copy link

unity-cla-assistant commented Apr 14, 2025

CLA assistant check
All committers have signed the CLA.

@Kibsgaard Kibsgaard force-pushed the vertexcolorsbonesfix branch from daac4ef to 6a93ae6 Compare April 14, 2025 15:05
@Kibsgaard
Copy link
Author

Ah, I had overlooked that there was a new version and the vertex color issue was fixed in 6.12.0 (#30).

However, I can see the BoneWeights job is still causing errors when GLTFAST_SAFE define symbol is set, so I'll leave this up.

On a related note: We have forked glTFast to customize some things with animations and always normalize bone weights (without requiring the full GLTFAST_SAFE validation set). Is there a way for us to get notified of new versions of glTFast? Seems like github and openupm generally are a few patches behind the package registry. If possible, it would be really helpful if this repo is updated at the same time as releases, to make it easier to create clean pull requests and update forked versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants