Files
ReSkate/Extension/Rendering/replay_export.cpp
T
AyeZeeBB d84b32fc17 Encode every rendered frame of a replay export
Videos exported from the replay editor played two to three times too
fast unless the frame rate was capped, or the export was slow enough to
hold it down by itself (4K, WebM). The fault is in the game's own export
path.
Replay export (Extension/Rendering/replay_export.*)
- While exporting, the game steps the replay by one frame of video
  (speed / fps) for every frame it renders. The engine's movie encoder
  takes a frame only while a permit byte is set; a world update sets it
  once, and a frame submitted without it is dropped. Rendering two or
  three frames per update, most of a clip never reached the file, and an
  MP4 is exactly as long as its frame count. One clip came out as 181
  frames at 30 fps and 297 at 60, 6.0 s and 5.0 s.
- The encoder's frame submit is detoured and the permit set before the
  original runs, for the replay editor's encoder only (matched by its
  vtable). The frame is encoded on the render thread before the call
  returns, so the renderer is held to the pace the encoder keeps and no
  frame is lost. The replay still runs fast on screen during an export:
  that is the encoder's speed, not the file's.
- The same clip afterwards, frame rate uncapped, MP4 1080p: 505 frames
  at 30 fps and 1008 at 60, 16.8 s both, audio as long as the video.
  WebM and 4K take the same path and were not run again.
- Addresses are in Engine/Game/Build/20260929/replay_export.h. The hook
  starts with the others in runtime_init.cpp.
2026-10-03 20:57:07 -05:00

49 lines
1.9 KiB
C++

#include "replay_export.h"
#include "Engine/Core/Hooks/hooks.h"
#include "Engine/Core/Platform/memory.h"
#include "Engine/Game/Build/addresses.h"
#include "Engine/Game/Build/20260929/replay_export.h"
#include <array>
#include <atomic>
namespace dingosdk::replay_export {
namespace {
namespace native = addr::replay_export;
// The encoder passes its arguments straight on to the codec, so all four
// register arguments are forwarded as they came.
using Submit = void (*)(std::uintptr_t encoder, std::uintptr_t pixels, std::uintptr_t pitch, std::uintptr_t unused);
std::atomic<Submit> original{};
std::atomic<std::uintptr_t> image{};
void submit(std::uintptr_t encoder, std::uintptr_t pixels, std::uintptr_t pitch, std::uintptr_t unused) {
// Encoding is done on this thread before the call returns, so letting every
// frame through also holds the renderer to the pace the encoder keeps.
std::uintptr_t vtable{};
if (memory::peek(encoder, vtable) && vtable == image.load() + native::replay_encoder_vtable)
*reinterpret_cast<std::uint8_t*>(encoder + native::frame_permit) = 1;
original.load()(encoder, pixels, pitch, unused);
}
} // namespace
bool start(std::uintptr_t base) noexcept {
try {
std::array<unsigned char, native::submit_frame_prefix.size()> bytes{};
if (!base || !memory::read_bytes(base + native::submit_frame, bytes.data(), bytes.size()) ||
bytes != native::submit_frame_prefix)
return false;
image = base;
auto* target = reinterpret_cast<void*>(base + native::submit_frame);
void* previous{};
if (hook_prepare(target, reinterpret_cast<void*>(&submit), &previous) != HookOk) return false;
original = reinterpret_cast<Submit>(previous);
if (hook_enable(target) != HookOk) {
hook_remove(target);
return false;
}
return true;
} catch (...) {
return false;
}
}
} // namespace dingosdk::replay_export