@ -1,6 +1,8 @@
using System ;
using System ;
using System.Runtime.InteropServices ;
using System.Runtime.InteropServices ;
using AOT ;
using AOT ;
using Unity.Collections ;
using Unity.Collections.LowLevel.Unsafe ;
using UnityEngine ;
using UnityEngine ;
using UnityEngine.Profiling ;
using UnityEngine.Profiling ;
@ -138,7 +140,9 @@ public partial class RenderModule: CallbackHandler<RenderModule>
return result ;
return result ;
}
}
#if UNITY_EDITOR
private readonly byte [ ] lightmapBytes = new byte [ QConstants . LightmapBlockWidth * QConstants . LightmapBlockHeight * 4 ] ; // 32 bits per pixel
private readonly byte [ ] lightmapBytes = new byte [ QConstants . LightmapBlockWidth * QConstants . LightmapBlockHeight * 4 ] ; // 32 bits per pixel
#endif
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool UploadLightmapCallback ( IntPtr context , int lmap , IntPtr data ) ;
private delegate bool UploadLightmapCallback ( IntPtr context , int lmap , IntPtr data ) ;
@ -146,13 +150,26 @@ public partial class RenderModule: CallbackHandler<RenderModule>
[MonoPInvokeCallback(typeof(UploadLightmapCallback))]
[MonoPInvokeCallback(typeof(UploadLightmapCallback))]
private static bool Callback_UploadLightmap ( IntPtr context , int lmap , IntPtr data )
private static bool Callback_UploadLightmap ( IntPtr context , int lmap , IntPtr data )
{
{
bool result ;
Profiler . BeginSample ( "UploadLightmap" ) ;
Profiler . BeginSample ( "UploadLightmap" ) ;
// TODO: this is a fairly pointless additional data copy step; we could probably make this faster by wrapping the IntPtr directly into a NativeArray
#if UNITY_EDITOR
// This is fairly pointless additional data copy step; the below NativeArray approach is faster and doesn't
// produce any garbage, but it doesn't work inside the Unity Editor for some reason.
var self = GetSelf ( context ) ;
var self = GetSelf ( context ) ;
Marshal . Copy ( data , self . lightmapBytes , 0 , self . lightmapBytes . Length ) ;
Marshal . Copy ( data , self . lightmapBytes , 0 , self . lightmapBytes . Length ) ;
bool result = self . UploadLightmap ( lmap , QConstants . LightmapBlockWidth , QConstants . LightmapBlockHeight , self . lightmapBytes ) ;
result = self . UploadLightmap ( lmap , QConstants . LightmapBlockWidth , QConstants . LightmapBlockHeight , self . lightmapBytes ) ;
#else
unsafe
{
// More efficient code path that passes the native byte buffer directly to the Texture2D's pixel data
var dataArray = NativeArrayUnsafeUtility . ConvertExistingDataToNativeArray < byte > ( data . ToPointer ( ) ,
QConstants . LightmapBlockWidth * QConstants . LightmapBlockHeight * 4 , Allocator . None ) ;
result = GetSelf ( context ) . UploadLightmap ( lmap , QConstants . LightmapBlockWidth , QConstants . LightmapBlockHeight , dataArray ) ;
}
#endif
Profiler . EndSample ( ) ;
Profiler . EndSample ( ) ;
return result ;
return result ;
}
}