Performance Fish

This commit is contained in:
bbradson
2022-10-24 18:24:02 +02:00
parent 594b32b4ef
commit 2efa8e466c
69 changed files with 9147 additions and 3 deletions
@@ -0,0 +1,45 @@
// Copyright (c) 2022 bradson
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
/*namespace PerformanceFish.Rendering;
public class GenDrawPatches : ClassWithFishPatches
{
public class DrawMeshNowOrLater_Patch : FishPatch
{
public override Delegate TargetMethodGroup => (Action<Mesh, Vector3, Quaternion, Material, bool>)GenDraw.DrawMeshNowOrLater;
public static void Replacement(Mesh mesh, Vector3 loc, Quaternion quat, Material mat, bool drawNow)
{
if (drawNow)
{
if (!mat.SetPass(0))
Log.Error("SetPass(0) call failed on material " + mat.name + " with shader " + mat.shader.name);
Graphics.DrawMeshNow(mesh, loc, quat);
}
else
{
Graphics.DrawMesh(mesh, loc, quat, mat, 0);
}
}
}
public class DrawMeshNowOrLaterWithMatrix_Patch : FishPatch
{
public override Delegate TargetMethodGroup => (Action<Mesh, Matrix4x4, Material, bool>)GenDraw.DrawMeshNowOrLater;
public static void Replacement(Mesh mesh, Matrix4x4 matrix, Material mat, bool drawNow)
{
if (drawNow)
{
mat.SetPass(0);
Graphics.DrawMeshNow(mesh, matrix);
}
else
{
Graphics.DrawMesh(mesh, matrix, mat, 0);
}
}
}
}*/
@@ -0,0 +1,39 @@
// Copyright (c) 2022 bradson
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
/*namespace PerformanceFish.Rendering;
public class GraphicPatches : ClassWithFishPatches
{
public class DrawMeshInt_Patch : FishPatch
{
public override MethodBase TargetMethodInfo => AccessTools.DeclaredMethod(typeof(Graphic), nameof(Graphic.DrawMeshInt));
public static void Replacement(Graphic __instance, Mesh mesh, Vector3 loc, Quaternion quat, Material mat)
{
Graphics.DrawMesh(mesh, loc, quat, mat, 0);
}
}
public class Shadow_DrawWorker_Patch : FishPatch
{
public override MethodBase TargetMethodInfo => AccessTools.DeclaredMethod(typeof(Graphic_Shadow), nameof(Graphic_Shadow.DrawWorker));
public static void Replacement(Graphic_Shadow __instance, Vector3 loc, Rot4 rot, ThingDef thingDef, Thing thing, float extraRotation)
{
if (__instance.shadowMesh is null
|| thingDef is null
|| __instance.shadowInfo is null
|| (Find.CurrentMap is { } map && loc.ToIntVec3().InBounds(map) && map.roofGrid.Roofed(loc.ToIntVec3()))
|| !DebugViewSettings.drawShadows)
{
return;
}
var position = loc + __instance.shadowInfo.offset;
position.y = AltitudeLayer.Shadows.AltitudeFor();
Graphics.DrawMesh(__instance.shadowMesh, position, rot.AsQuat, MatBases.SunShadowFade, 0);
}
}
}*/
@@ -0,0 +1,65 @@
// Copyright (c) 2022 bradson
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
/*namespace PerformanceFish.Rendering;
public class PawnCacheRendererPatches : ClassWithFishPatches // The PawnCacheRenderer's Camera.Render() call has a considerable performance impact despite only being used for rendering of single pawns. These are failed attempts at lowering its impact.
{
private static bool _optimizeCameraEnabled;
public class OnPostRender_Patch : FishPatch
{
public override MethodBase TargetMethodInfo => AccessTools.DeclaredMethod(typeof(PawnCacheRenderer), nameof(PawnCacheRenderer.OnPostRender));
public static CodeInstructions Transpiler(CodeInstructions codes)
=> Reflection.MakeReplacementCall(OnPostRender);
public static void OnPostRender(PawnCacheRenderer __instance)
{
Find.PawnCacheCamera.cullingMask = DefaultCullingMask;
__instance.pawn.Drawer.renderer.RenderCache(__instance.rotation, __instance.angle, __instance.positionOffset, __instance.renderHead, __instance.renderBody, __instance.portrait, __instance.renderHeadgear, __instance.renderClothes, __instance.overrideApparelColor, __instance.overrideHairColor, __instance.stylingStation);
Find.PawnCacheCamera.cullingMask = 16; // random
}
private static int DefaultCullingMask => _defaultCullinkMask ??= Find.PawnCacheCamera.cullingMask;
private static int? _defaultCullinkMask;
}
public class RenderPawn_Patch : FishPatch
{
public override MethodBase TargetMethodInfo => AccessTools.DeclaredMethod(typeof(PawnCacheRenderer), nameof(PawnCacheRenderer.RenderPawn));
public static void Prefix(ref Vector3 cameraOffset, ref Vector3 positionOffset)
{
cameraOffset += new Vector3(2300f, 0f, 0f);
positionOffset += new Vector3(2300f, 0f, 0f);
}
private static int DefaultCullingMask => _defaultCullinkMask ??= Find.PawnCacheCamera.cullingMask;
private static int? _defaultCullinkMask;
}
public static bool OptimizeCameraEnabled
{
get => _optimizeCameraEnabled;
set
{
_optimizeCameraEnabled = value;
OptimizeCamera();
}
}
public static void OptimizeCamera()
{
var cacheCamera = Find.PawnCacheCamera;
var camera = Find.Camera;
camera.renderingPath = _optimizeCameraEnabled ? RenderingPath.VertexLit : DefaultCameraRenderingPath;
cacheCamera.renderingPath = _optimizeCameraEnabled ? RenderingPath.VertexLit : DefaultCacheCameraRenderingPath;
}
private static RenderingPath DefaultCameraRenderingPath => _defaultCameraRenderingPath ??= Find.Camera.renderingPath;
private static RenderingPath DefaultCacheCameraRenderingPath => _defaultCacheCameraRenderingPath ??= Find.PawnCacheCamera.renderingPath;
private static RenderingPath? _defaultCameraRenderingPath;
private static RenderingPath? _defaultCacheCameraRenderingPath;
}*/