SHA256
Add Renderer Feature
This commit is contained in:
1 parent
c9314c264d
commit
2556bc7f62
13 files changed
+727
-21
No files matched your search
+123
-3
@@ -6,6 +6,7 @@ using ADOFAIRenderer.Util;
|
||||
using MelonLoader;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.Rendering;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
[assembly: MelonInfo(typeof(RenderMod), "ADOFAI Renderer", "0.0.3-INDEV", "ADOFAI Editor Forum")]
|
||||
@@ -17,15 +18,24 @@ namespace ADOFAIRenderer
|
||||
public class RenderMod : MelonMod
|
||||
{
|
||||
public static AssetBundle FontBundle = null!;
|
||||
// ReSharper disable once MemberCanBePrivate.Global
|
||||
public static AssetBundle SceneBundle = null!;
|
||||
public static bool WatchGameLoaded;
|
||||
public static string[] ModScenes = {};
|
||||
|
||||
private static double _beforeTime = 0;
|
||||
private static double _beforeTime;
|
||||
|
||||
public static bool IsRendering = false;
|
||||
public static bool IsCapturing = false;
|
||||
|
||||
|
||||
public static double TimeBase;
|
||||
public static double UnscaledTimeBase;
|
||||
public static double DspTimeBase;
|
||||
public static int FrameCountBase;
|
||||
|
||||
public static bool RenderCheck = true;
|
||||
public static int SkipCount = 0;
|
||||
|
||||
public override void OnInitializeMelon()
|
||||
{
|
||||
var renderManagerObj = new GameObject("Render Manager");
|
||||
@@ -65,14 +75,124 @@ namespace ADOFAIRenderer
|
||||
_beforeTime = Time.realtimeSinceStartupAsDouble;
|
||||
|
||||
if (!IsRendering) return;
|
||||
|
||||
// [렉으로 인한 프레임 렌더링 스킵 시 재시도 로직 (원본 모드 이식)]
|
||||
if (RenderCheck)
|
||||
{
|
||||
if (SkipCount < 60)
|
||||
{
|
||||
Melon<RenderMod>.Logger.Warning($"[Render Skipped] Frame {RenderManager.FrameCount} rendering missed. Retrying... (Attempt {SkipCount + 1}/60)");
|
||||
SkipCount++;
|
||||
Time.timeScale = 0f; // 진행 시간 멈추기 (재시도)
|
||||
return; // 업데이트 진행하지 않고 대기
|
||||
}
|
||||
|
||||
// 60회 초과 시 최후의 블랙 프레임 삽입하여 전체 파이프라인 마비 방지
|
||||
Melon<RenderMod>.Logger.Error($"[Render Skipped] Frame {RenderManager.FrameCount} rendering failed after 60 retries. Supplying black frame.");
|
||||
|
||||
int targetFps = (int)RenderManager.Configuration.TargetFPS;
|
||||
int videoFps = (int)RenderManager.Configuration.VideoFPS;
|
||||
int num = targetFps % videoFps != 0 ? 1 : targetFps / videoFps;
|
||||
|
||||
if (FrameCountForVideo % num == 0)
|
||||
{
|
||||
int byteSize = RenderManager.Configuration.RenderWidth * RenderManager.Configuration.RenderHeight * 4;
|
||||
byte[] blackFrame = System.Buffers.ArrayPool<byte>.Shared.Rent(byteSize);
|
||||
Array.Clear(blackFrame, 0, byteSize);
|
||||
|
||||
lock (RenderManager.ImageQueue)
|
||||
{
|
||||
RenderManager.ImageQueue[FrameCountForVideo] = blackFrame;
|
||||
}
|
||||
}
|
||||
|
||||
FrameCountForVideo++;
|
||||
RenderCheck = false; // 진행 허용
|
||||
}
|
||||
|
||||
SkipCount = 0;
|
||||
RenderCheck = true; // 다음 프레임 검사용으로 초기화
|
||||
RenderManager.FrameCount++;
|
||||
|
||||
Time.timeScale = (float)(Time.deltaTime / realDeltaTime);
|
||||
}
|
||||
|
||||
public static void OnPostRender()
|
||||
public static int FrameCountForVideo = 0;
|
||||
public static int ReadbackFailureCount = 0;
|
||||
public static int ThrottleCount = 0;
|
||||
public static bool IsThrottling = false;
|
||||
|
||||
public static void OnPostRender(RenderTexture? texture)
|
||||
{
|
||||
if (!IsCapturing || !texture) return;
|
||||
|
||||
RenderCheck = false; // 이번 프레임 정상 렌더링 완료
|
||||
|
||||
// [Backpressure Throttling (2번 작업)]
|
||||
const int maxQueuedFrames = 60;
|
||||
IsThrottling = false;
|
||||
|
||||
while (IsCapturing && IsRendering)
|
||||
{
|
||||
int count;
|
||||
lock (RenderManager.ImageQueue)
|
||||
{
|
||||
count = RenderManager.ImageQueue.Count;
|
||||
}
|
||||
|
||||
if (count <= maxQueuedFrames) break;
|
||||
|
||||
if (!IsThrottling)
|
||||
{
|
||||
IsThrottling = true;
|
||||
ThrottleCount++;
|
||||
}
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
IsThrottling = false;
|
||||
|
||||
var currentFrame = FrameCountForVideo++;
|
||||
|
||||
var targetFps = RenderManager.Configuration.TargetFPS;
|
||||
var videoFps = RenderManager.Configuration.VideoFPS;
|
||||
var num = targetFps % videoFps != 0.0 ? 1 : (int)(targetFps / videoFps);
|
||||
|
||||
if (currentFrame % num != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AsyncGPUReadback.Request(texture, 0, delegate(AsyncGPUReadbackRequest req)
|
||||
{
|
||||
if (!IsCapturing) return;
|
||||
|
||||
byte[] rawBytes;
|
||||
if (req.hasError)
|
||||
{
|
||||
ReadbackFailureCount++;
|
||||
Melon<RenderMod>.Logger.Warning($"[OOM Prevent] GPU Readback failed for frame {currentFrame}. Queue deadlock prevented by sending an empty frame.");
|
||||
rawBytes = Array.Empty<byte>(); // 1번 작업: 빈 배열로 대체하여 정체 방지
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = req.GetData<byte>();
|
||||
rawBytes = FFmpegProcess.RentRawBytes(data); // 3번 작업: ArrayPool에서 대여
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ReadbackFailureCount++;
|
||||
Melon<RenderMod>.Logger.Error($"[OOM Prevent] Native conversion failed for frame {currentFrame}: {ex}");
|
||||
rawBytes = Array.Empty<byte>();
|
||||
}
|
||||
}
|
||||
|
||||
lock (RenderManager.ImageQueue)
|
||||
{
|
||||
RenderManager.ImageQueue[currentFrame] = rawBytes;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void StartRender()
|
||||
|
||||
Reference in new issue
Block a user