// SPDX-FileCopyrightText: 2023 Unity Technologies and the Draco for Unity authors
// SPDX-License-Identifier: Apache-2.0
using System;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Rendering;
namespace Draco.Encode
{
///
/// Contains encoded data and additional meta information.
/// The responsibility to dispose this struct and the native resources behind it (via )
/// is handed over to the receiver.
///
public unsafe struct EncodeResult : IDisposable
{
/// Number of triangle indices
public readonly uint indexCount;
/// Number vertices
public readonly uint vertexCount;
/// Encoded data
public readonly NativeArray data;
/// Vertex attribute to Draco property ID mapping
public readonly Dictionary vertexAttributes;
IntPtr m_DracoEncoder;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
readonly AtomicSafetyHandle m_SafetyHandle;
#endif
///
/// Constructs an EncodeResult.
///
/// Native Draco encoder instance.
/// Number of indices.
/// Number of vertices.
/// For each vertex attribute type there's a tuple containing
/// the draco identifier and the attribute dimensions (e.g. 3 for 3D positions).
public EncodeResult(
IntPtr dracoEncoder,
uint indexCount,
uint vertexCount,
Dictionary vertexAttributes
)
{
m_DracoEncoder = dracoEncoder;
this.indexCount = indexCount;
this.vertexCount = vertexCount;
DracoEncoder.dracoEncoderGetEncodeBuffer(m_DracoEncoder, out var dracoData, out var size);
data = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray(dracoData, (int)size, Allocator.None);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
m_SafetyHandle = AtomicSafetyHandle.Create();
NativeArrayUnsafeUtility.SetAtomicSafetyHandle(array: ref data, m_SafetyHandle);
#endif
this.vertexAttributes = vertexAttributes;
}
///
/// Releases allocated resources.
///
public void Dispose()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.Release(m_SafetyHandle);
#endif
if (m_DracoEncoder != IntPtr.Zero)
DracoEncoder.dracoEncoderRelease(m_DracoEncoder);
m_DracoEncoder = IntPtr.Zero;
}
}
}