Browse Source

Added profiler sampling to the Game and Render callbacks, to make it easier to measure performance of native-to-managed callbacks

readme
Nico de Poel 5 years ago
parent
commit
ec87ac10b0
  1. 11
      Assets/Scripts/Modules/GameModule.Interop.cs
  2. 34
      Assets/Scripts/Modules/RenderModule.Interop.cs

11
Assets/Scripts/Modules/GameModule.Interop.cs

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using AOT; using AOT;
using UnityEngine; using UnityEngine;
using UnityEngine.Profiling;
public partial class GameModule : CallbackHandler<GameModule> public partial class GameModule : CallbackHandler<GameModule>
{ {
@ -44,7 +45,9 @@ public partial class GameModule : CallbackHandler<GameModule>
[MonoPInvokeCallback(typeof(GameSetEntityModelCallback))] [MonoPInvokeCallback(typeof(GameSetEntityModelCallback))]
private static void Callback_GameSetEntityModel(IntPtr target, int entityNum, string modelName) private static void Callback_GameSetEntityModel(IntPtr target, int entityNum, string modelName)
{ {
Profiler.BeginSample("GameSetEntityModel");
GetSelf(target).SetEntityModel(entityNum, modelName); GetSelf(target).SetEntityModel(entityNum, modelName);
Profiler.EndSample();
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@ -53,7 +56,9 @@ public partial class GameModule : CallbackHandler<GameModule>
[MonoPInvokeCallback(typeof(GameSetEntityTransformCallback))] [MonoPInvokeCallback(typeof(GameSetEntityTransformCallback))]
private static void Callback_GameSetEntityTransform(IntPtr target, int entityNum, ref QVec3 origin, ref QVec3 angles) private static void Callback_GameSetEntityTransform(IntPtr target, int entityNum, ref QVec3 origin, ref QVec3 angles)
{ {
Profiler.BeginSample("GameSetEntityTransform");
GetSelf(target).SetEntityTransform(entityNum, origin, angles); GetSelf(target).SetEntityTransform(entityNum, origin, angles);
Profiler.EndSample();
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@ -62,7 +67,9 @@ public partial class GameModule : CallbackHandler<GameModule>
[MonoPInvokeCallback(typeof(GameRemoveEntityCallback))] [MonoPInvokeCallback(typeof(GameRemoveEntityCallback))]
private static void Callback_GameRemoveEntity(IntPtr target, int entityNum) private static void Callback_GameRemoveEntity(IntPtr target, int entityNum)
{ {
Profiler.BeginSample("GameRemoveEntity");
GetSelf(target).RemoveEntity(entityNum); GetSelf(target).RemoveEntity(entityNum);
Profiler.EndSample();
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@ -71,7 +78,9 @@ public partial class GameModule : CallbackHandler<GameModule>
[MonoPInvokeCallback(typeof(GameUpdateEntityAnimationCallback))] [MonoPInvokeCallback(typeof(GameUpdateEntityAnimationCallback))]
private static void Callback_GameUpdateEntityAnimation(IntPtr target, int entityNum, int pose1, int pose2, float blend) private static void Callback_GameUpdateEntityAnimation(IntPtr target, int entityNum, int pose1, int pose2, float blend)
{ {
Profiler.BeginSample("GameUpdateEntityAnimation");
GetSelf(target).UpdateEntityAnimation(entityNum, pose1, pose2, blend); GetSelf(target).UpdateEntityAnimation(entityNum, pose1, pose2, blend);
Profiler.EndSample();
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@ -80,6 +89,8 @@ public partial class GameModule : CallbackHandler<GameModule>
[MonoPInvokeCallback(typeof(GameSetEntitySkinCallback))] [MonoPInvokeCallback(typeof(GameSetEntitySkinCallback))]
private static void Callback_GameSetEntitySkin(IntPtr target, int entityNum, int skinNum) private static void Callback_GameSetEntitySkin(IntPtr target, int entityNum, int skinNum)
{ {
Profiler.BeginSample("GameSetEntitySkin");
GetSelf(target).SetEntitySkin(entityNum, skinNum); GetSelf(target).SetEntitySkin(entityNum, skinNum);
Profiler.EndSample();
} }
} }

34
Assets/Scripts/Modules/RenderModule.Interop.cs

@ -2,6 +2,7 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using AOT; using AOT;
using UnityEngine; using UnityEngine;
using UnityEngine.Profiling;
public partial class RenderModule: CallbackHandler<RenderModule> public partial class RenderModule: CallbackHandler<RenderModule>
{ {
@ -57,6 +58,8 @@ public partial class RenderModule: CallbackHandler<RenderModule>
return 0; return 0;
} }
Profiler.BeginSample("UploadAliasModel");
if (header.numFrames > MaxAliasFrames) if (header.numFrames > MaxAliasFrames)
header.numFrames = MaxAliasFrames; header.numFrames = MaxAliasFrames;
@ -83,11 +86,14 @@ public partial class RenderModule: CallbackHandler<RenderModule>
fbTextures[i] = header.fbTextures[i * 4].ToStructArray<QGLTexture>(4); fbTextures[i] = header.fbTextures[i * 4].ToStructArray<QGLTexture>(4);
} }
return GetSelf(target).UploadAliasModel(
int result = GetSelf(target).UploadAliasModel(
name, header, frameType, poseVertices, name, header, frameType, poseVertices,
triangles.ToStructArray<QTriangle>(header.numTriangles), triangles.ToStructArray<QTriangle>(header.numTriangles),
stVerts.ToStructArray<QSTVert>(header.numVerts), stVerts.ToStructArray<QSTVert>(header.numVerts),
glTextures, fbTextures); glTextures, fbTextures);
Profiler.EndSample();
return result;
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@ -99,7 +105,10 @@ public partial class RenderModule: CallbackHandler<RenderModule>
if (model == null || model.type != QModelType.Brush) if (model == null || model.type != QModelType.Brush)
return -1; return -1;
return GetSelf(target).UploadBrushModel(model);
Profiler.BeginSample("UploadBrushModel");
int result = GetSelf(target).UploadBrushModel(model);
Profiler.EndSample();
return result;
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@ -111,7 +120,10 @@ public partial class RenderModule: CallbackHandler<RenderModule>
if (model == null || model.type != QModelType.Brush) if (model == null || model.type != QModelType.Brush)
return -1; return -1;
return GetSelf(target).UploadWorldModel(model);
Profiler.BeginSample("UploadWorldModel");
int result = GetSelf(target).UploadWorldModel(model);
Profiler.EndSample();
return result;
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@ -120,9 +132,14 @@ public partial class RenderModule: CallbackHandler<RenderModule>
[MonoPInvokeCallback(typeof(UploadTextureCallback))] [MonoPInvokeCallback(typeof(UploadTextureCallback))]
private static bool Callback_UploadTexture(IntPtr target, QGLTexture texture, IntPtr data, ref uint texNum) private static bool Callback_UploadTexture(IntPtr target, QGLTexture texture, IntPtr data, ref uint texNum)
{ {
Profiler.BeginSample("UploadTexture");
byte[] dataBytes = new byte[texture.width * texture.height * 4]; // 32 bits per pixel byte[] dataBytes = new byte[texture.width * texture.height * 4]; // 32 bits per pixel
Marshal.Copy(data, dataBytes, 0, dataBytes.Length); Marshal.Copy(data, dataBytes, 0, dataBytes.Length);
return GetSelf(target).UploadTexture(texture, dataBytes, ref texNum);
bool result = GetSelf(target).UploadTexture(texture, dataBytes, ref texNum);
Profiler.EndSample();
return result;
} }
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
@ -133,10 +150,15 @@ public partial class RenderModule: CallbackHandler<RenderModule>
[MonoPInvokeCallback(typeof(UploadLightmapCallback))] [MonoPInvokeCallback(typeof(UploadLightmapCallback))]
private static bool Callback_UploadLightmap(IntPtr target, int lmap, IntPtr data) private static bool Callback_UploadLightmap(IntPtr target, int lmap, IntPtr data)
{ {
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 // TODO: this is a fairly pointless additional data copy step; we could probably make this faster by wrapping the IntPtr directly into a NativeArray
var self = GetSelf(target); var self = GetSelf(target);
Marshal.Copy(data, self.lightmapBytes, 0, self.lightmapBytes.Length); Marshal.Copy(data, self.lightmapBytes, 0, self.lightmapBytes.Length);
return self.UploadLightmap(lmap, QConstants.LightmapBlockWidth, QConstants.LightmapBlockHeight, self.lightmapBytes);
bool result = self.UploadLightmap(lmap, QConstants.LightmapBlockWidth, QConstants.LightmapBlockHeight, self.lightmapBytes);
Profiler.EndSample();
return result;
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@ -145,6 +167,8 @@ public partial class RenderModule: CallbackHandler<RenderModule>
[MonoPInvokeCallback(typeof(SetupViewCallback))] [MonoPInvokeCallback(typeof(SetupViewCallback))]
private static void Callback_SetupView(IntPtr target, ref QVec3 origin, ref QVec3 angles, ref QLeaf viewLeaf) private static void Callback_SetupView(IntPtr target, ref QVec3 origin, ref QVec3 angles, ref QLeaf viewLeaf)
{ {
Profiler.BeginSample("SetupView");
GetSelf(target).SetupView(origin, angles, viewLeaf); GetSelf(target).SetupView(origin, angles, viewLeaf);
Profiler.EndSample();
} }
} }
Loading…
Cancel
Save