Skip to content

Commit

Permalink
Merge branch 'dracu-unity-2'
Browse files Browse the repository at this point in the history
  • Loading branch information
atteneder committed May 21, 2021
2 parents d490a34 + d0889ce commit ebd5ea3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 37 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.0.1] - 2021-05-21
### Fixed
- AOT Burst compilation errors

## [3.0.0] - 2021-05-18
### Changed
- `DracoMeshLoader`'s coordinate space conversion from right-hand (like in glTF) to left-hand (Unity) changed. Now this is performed by inverting the X-axis (before the Z-axis was inverted). Compared to the previous behaviour, meshes are rotated 180° along the up-axis (Y). This was done to better conform to the glTF specification.

## [2.0.1] - 2021-05-21
### Fixed
- AOT Burst compilation errors

## [2.0.0] - 2021-05-17
### Added
- Experimental encoding support (ability to convert Unity Meshes into compressed Draco)
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Scripts/Draco.asmdef
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "Draco",
"rootNamespace": "",
"references": [
"GUID:2665a8d13d1b3f18800f46e256720795"
"Unity.Burst"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
114 changes: 80 additions & 34 deletions Runtime/Scripts/DracoNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using AOT;
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
Expand Down Expand Up @@ -66,6 +67,7 @@ enum AttributeType {
GENERIC
}

[BurstCompile]
unsafe internal class DracoNative {

#if UNITY_EDITOR_OSX || UNITY_WEBGL || UNITY_IOS
Expand All @@ -86,6 +88,14 @@ unsafe internal class DracoNative {
const int decoderPtrIndex = 1;
const int bufferPtrIndex = 2;

// Cached function pointers
static FunctionPointer<GetDracoBonesJob.GetIndexValueDelegate> GetIndexValueInt8Method;
static FunctionPointer<GetDracoBonesJob.GetIndexValueDelegate> GetIndexValueUInt8Method;
static FunctionPointer<GetDracoBonesJob.GetIndexValueDelegate> GetIndexValueInt16Method;
static FunctionPointer<GetDracoBonesJob.GetIndexValueDelegate> GetIndexValueUInt16Method;
static FunctionPointer<GetDracoBonesJob.GetIndexValueDelegate> GetIndexValueInt32Method;
static FunctionPointer<GetDracoBonesJob.GetIndexValueDelegate> GetIndexValueUInt32Method;

/// <summary>
/// If true, coordinate space is converted from right-hand (like in glTF) to left-hand (Unity).
/// </summary>
Expand Down Expand Up @@ -433,7 +443,8 @@ public JobHandle DecodeVertexData(
indicesAttribute = boneIndexMap.dracoAttribute,
weightsAttribute = boneWeightMap.dracoAttribute,
bonesPerVertex = bonesPerVertex,
boneWeights = boneWeights
boneWeights = boneWeights,
indexValueConverter = GetIndexValueConverter(boneIndexMap.format)
};
jobHandles[jobIndex] = job.Schedule(decodeVerticesJobHandle);
}
Expand Down Expand Up @@ -567,6 +578,50 @@ public void DisposeBoneWeightData() {
boneWeightMap = null;
}

/// <summary>
/// Returns Burst compatible function that converts a (bone) index
/// of type `format` into an int
/// </summary>
/// <param name="format">Data type of bone index</param>
/// <returns>Burst Function Pointer to correct conversion function</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
static FunctionPointer<GetDracoBonesJob.GetIndexValueDelegate> GetIndexValueConverter(VertexAttributeFormat format) {
switch (format) {
case VertexAttributeFormat.UInt8:
if (!GetIndexValueUInt8Method.IsCreated) {
GetIndexValueUInt8Method = BurstCompiler.CompileFunctionPointer<GetDracoBonesJob.GetIndexValueDelegate>(GetDracoBonesJob.GetIndexValueUInt8);
}
return GetIndexValueUInt8Method;
case VertexAttributeFormat.SInt8:
if (!GetIndexValueInt8Method.IsCreated) {
GetIndexValueInt8Method = BurstCompiler.CompileFunctionPointer<GetDracoBonesJob.GetIndexValueDelegate>(GetDracoBonesJob.GetIndexValueInt8);
}
return GetIndexValueInt8Method;
case VertexAttributeFormat.UInt16:
if (!GetIndexValueUInt16Method.IsCreated) {
GetIndexValueUInt16Method = BurstCompiler.CompileFunctionPointer<GetDracoBonesJob.GetIndexValueDelegate>(GetDracoBonesJob.GetIndexValueUInt16);
}
return GetIndexValueUInt16Method;
case VertexAttributeFormat.SInt16:
if (!GetIndexValueInt16Method.IsCreated) {
GetIndexValueInt16Method = BurstCompiler.CompileFunctionPointer<GetDracoBonesJob.GetIndexValueDelegate>(GetDracoBonesJob.GetIndexValueInt16);
}
return GetIndexValueInt16Method;
case VertexAttributeFormat.UInt32:
if (!GetIndexValueUInt32Method.IsCreated) {
GetIndexValueUInt32Method = BurstCompiler.CompileFunctionPointer<GetDracoBonesJob.GetIndexValueDelegate>(GetDracoBonesJob.GetIndexValueUInt32);
}
return GetIndexValueUInt32Method;
case VertexAttributeFormat.SInt32:
if (!GetIndexValueInt32Method.IsCreated) {
GetIndexValueInt32Method = BurstCompiler.CompileFunctionPointer<GetDracoBonesJob.GetIndexValueDelegate>(GetDracoBonesJob.GetIndexValueInt32);
}
return GetIndexValueInt32Method;
default:
throw new ArgumentOutOfRangeException(nameof(format), format, null);
}
}

// The order must be consistent with C++ interface.
[StructLayout (LayoutKind.Sequential)] public struct DracoData
{
Expand Down Expand Up @@ -877,6 +932,10 @@ public void Execute() {
[BurstCompile]
struct GetDracoBonesJob : IJob {

public delegate int GetIndexValueDelegate(IntPtr baseAddress, int index);

public FunctionPointer<GetIndexValueDelegate> indexValueConverter;

[ReadOnly]
public NativeArray<int> result;
[ReadOnly]
Expand Down Expand Up @@ -910,39 +969,14 @@ public void Execute() {
DracoData* weightsData = null;
GetAttributeData(dracoMesh, weightsAttribute, &weightsData, false);
var weightSize = DataTypeSize((DataType)weightsData->dataType) * weightsAttribute->numComponents;

Func<IntPtr, int, int> indexValueConverter = null;
switch (indicesDataType) {
case DataType.DT_INT8:
indexValueConverter = GetIndexValueInt8;
break;
case DataType.DT_UINT8:
indexValueConverter = GetIndexValueUInt8;
break;
case DataType.DT_INT16:
indexValueConverter = GetIndexValueInt16;
break;
case DataType.DT_UINT16:
indexValueConverter = GetIndexValueUInt16;
break;
case DataType.DT_INT32:
indexValueConverter = GetIndexValueInt32;
break;
case DataType.DT_UINT32:
indexValueConverter = GetIndexValueUInt32;
break;
default:
Debug.LogError($"Unsupported indicesDataType {indicesDataType}");
return;
}


for (var v = 0; v < dracoMesh->numVertices; v++) {
bonesPerVertex[v] = (byte) indicesAttribute->numComponents;
var indicesPtr = (IntPtr) (((byte*)indicesData->data) + (indexSize * v));
var weightsPtr = (float*) (((byte*)weightsData->data) + (weightSize * v));
for (var b = 0; b < indicesAttribute->numComponents; b++) {
boneWeights[v * indicesAttribute->numComponents + b] = new BoneWeight1 {
boneIndex = indexValueConverter(indicesPtr,b),
boneIndex = indexValueConverter.Invoke(indicesPtr,b),
weight = *(weightsPtr + b)
};
}
Expand All @@ -951,27 +985,39 @@ public void Execute() {
ReleaseDracoData(&weightsData);
}

static int GetIndexValueUInt8(IntPtr baseAddress, int index) {
[BurstCompile]
[MonoPInvokeCallback(typeof(GetIndexValueDelegate))]
public static int GetIndexValueUInt8(IntPtr baseAddress, int index) {
return *((byte*)baseAddress+index);
}

static int GetIndexValueInt8(IntPtr baseAddress, int index) {
[BurstCompile]
[MonoPInvokeCallback(typeof(GetIndexValueDelegate))]
public static int GetIndexValueInt8(IntPtr baseAddress, int index) {
return *(((sbyte*)baseAddress)+index);
}

static int GetIndexValueUInt16(IntPtr baseAddress, int index) {
[BurstCompile]
[MonoPInvokeCallback(typeof(GetIndexValueDelegate))]
public static int GetIndexValueUInt16(IntPtr baseAddress, int index) {
return *(((ushort*)baseAddress)+index);
}

static int GetIndexValueInt16(IntPtr baseAddress, int index) {
[BurstCompile]
[MonoPInvokeCallback(typeof(GetIndexValueDelegate))]
public static int GetIndexValueInt16(IntPtr baseAddress, int index) {
return *(((short*)baseAddress)+index);
}

static int GetIndexValueUInt32(IntPtr baseAddress, int index) {
[BurstCompile]
[MonoPInvokeCallback(typeof(GetIndexValueDelegate))]
public static int GetIndexValueUInt32(IntPtr baseAddress, int index) {
return (int) *(((uint*)baseAddress)+index);
}

static int GetIndexValueInt32(IntPtr baseAddress, int index) {
[BurstCompile]
[MonoPInvokeCallback(typeof(GetIndexValueDelegate))]
public static int GetIndexValueInt32(IntPtr baseAddress, int index) {
return *(((int*)baseAddress)+index);
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "com.atteneder.draco",
"version": "3.0.0",
"version": "3.0.1",
"displayName": "Draco 3D Data Compression",
"description": "Apply Google Draco compression to meshes and load them at runtime.",
"unity": "2019.3",
"dependencies": {
"com.unity.burst": "1.4.4"
"com.unity.burst": "1.4.8"
},
"keywords": [
"mesh",
Expand Down

0 comments on commit ebd5ea3

Please sign in to comment.