first try

This commit is contained in:
2026-07-08 21:34:37 +04:00
parent 96bce7f38d
commit e2e04fa86f
3 changed files with 162 additions and 41 deletions

View File

@@ -87,6 +87,11 @@ public abstract class OpenGlFrameCapturer<F extends Frame, D extends CaptureData
}
protected OpenGlFrame renderFrame(int frameId, float partialTicks, D captureData) {
renderWorldOnly(partialTicks, captureData);
return captureFrame(frameId, captureData);
}
protected void renderWorldOnly(float partialTicks, D captureData) {
resizeMainWindow(mc, getFrameWidth(), getFrameHeight());
pushMatrix();
@@ -115,8 +120,6 @@ public abstract class OpenGlFrameCapturer<F extends Frame, D extends CaptureData
frameBuffer().endWrite();
//#endif
popMatrix();
return captureFrame(frameId, captureData);
}
protected OpenGlFrame captureFrame(int frameId, D captureData) {

View File

@@ -10,6 +10,7 @@ import com.replaymod.pathing.properties.LensProperties;
import com.replaymod.render.RenderSettings;
import com.replaymod.render.utils.ByteBufferPool;
import com.replaymod.render.utils.AccumulationBuffer;
import com.replaymod.replay.ReplayHandler;
import com.replaymod.replay.ReplayModReplay;
import com.replaymod.replaystudio.pathing.path.Timeline;
@@ -109,6 +110,7 @@ public class RealLensOpenGlFrameCapturer
private final Path positionPath;
private final Timeline timeline;
private final ReplayHandler replayHandler;
private final AccumulationBuffer accum = new AccumulationBuffer();
public RealLensOpenGlFrameCapturer(
WorldRenderer worldRenderer,
@@ -123,72 +125,61 @@ public class RealLensOpenGlFrameCapturer
@Override
public Map<Channel, RealLensOpenGlFrame> process() {
// Продвигает состояние игры к времени ТЕКУЩЕГО выходного кадра
// (через onTick -> timeline.applyToGame(videoTime)).
renderInfo.updateForNextFrame();
int frameId = framesDone++;
RenderSettings settings = renderInfo.getRenderSettings();
int fps = settings.getFramesPerSecond();
int N = Math.max(1, settings.getLensBlurSamples()); // единый бюджет сэмплов
float shutter = settings.getShutterAngle(); // градусы
int fps = settings.getFramesPerSecond();
int N = Math.max(1, settings.getLensBlurSamples());
float shutter = settings.getShutterAngle();
boolean motionBlur = shutter > 0f;
double dt = 1000.0 / fps; // длительность выходного кадра, мс
double shutterFrac = motionBlur ? (shutter / 360.0) : 0.0;
double dt = 1000.0 / fps;
double shutterFrac = motionBlur ? (shutter / 360.0) : 0.0;
int width = getFrameWidth();
int width = getFrameWidth();
int height = getFrameHeight();
int bpp = 4;
int totalBytes = width * height * bpp;
long[] accumulator = new long[totalBytes];
// перемешанные слои времени, чтобы время не коррелировало с индексом апертурной спирали
int[] order = new int[N];
for (int i = 0; i < N; i++) order[i] = i;
for (int i = N - 1; i > 0; i--) { // Fisher-Yates
// перемешанный апертурный индекс (декорреляция DOF <-> время)
int[] apertureOrder = new int[N];
for (int i = 0; i < N; i++) apertureOrder[i] = i;
for (int i = N - 1; i > 0; i--) {
int j = ThreadLocalRandom.current().nextInt(i + 1);
int tmp = order[i];
order[i] = order[j];
order[j] = tmp;
int tmp = apertureOrder[i]; apertureOrder[i] = apertureOrder[j]; apertureOrder[j] = tmp;
}
accum.begin(width, height); // float-FBO нужного размера + очистка в 0
for (int i = 0; i < N; i++) {
// --- время: 1 стратифицированный джиттернутый сэмпл на слой ---
long tSub;
if (motionBlur) {
double u = (i + ThreadLocalRandom.current().nextDouble()) / N;
double u = (i + ThreadLocalRandom.current().nextDouble()) / N; // монотонно вперёд
tSub = (long) (frameId * dt + u * shutterFrac * dt);
timeline.applyToGame(tSub, replayHandler);
} else {
tSub = (long) (frameId * dt);
}
// partialTicks от РЕАЛЬНОГО времени реплея (frac(replayTime/50)),
// как в AbstractTimelinePlayer.onTick. (tSub % 50) даёт рассинхрон -> раздвоение.
long replayTime = replayHandler.getReplaySender().currentTimeStamp();
float subPartialTicks = (float) ((replayTime % 50L) / 50.0);
float focal = positionPath.getValue(LensProperties.FOCAL_DISTANCE, tSub).map(Triple::getLeft).orElse(5.0f);
float focal = positionPath.getValue(LensProperties.FOCAL_DISTANCE, tSub).map(Triple::getLeft).orElse(5.0f);
float apert = positionPath.getValue(LensProperties.APERTURE_RADIUS, tSub).map(Triple::getLeft).orElse(0.05f);
Data cam = singleDiskSample(order[i], N, focal, apert);
Data cam = singleDiskSample(apertureOrder[i], N, focal, apert);
OpenGlFrame sub = renderFrame(frameId, subPartialTicks, cam);
ByteBuffer buf = sub.getByteBuffer();
buf.rewind();
for (int k = 0; k < totalBytes; k++) {
accumulator[k] += (buf.get(k) & 0xFF);
}
ByteBufferPool.release(buf);
renderWorldOnly(subPartialTicks, cam); // мир -> главный FBO, без readback
accum.add(frameBuffer().getColorAttachment()); // прибавить на GPU
}
ByteBuffer out = ByteBufferPool.allocate(totalBytes);
for (int k = 0; k < totalBytes; k++) {
out.put((byte) (accumulator[k] / N));
}
out.rewind();
ByteBuffer out = accum.finishAveraged(N); // ОДИН readback + деление на N
accum.unbind(); // вернуться в главный FBO
OpenGlFrame averaged = new OpenGlFrame(frameId, new Dimension(width, height), bpp, out);
return Collections.singletonMap(Channel.BRGA, new RealLensOpenGlFrame(new OpenGlFrame[]{averaged}));
OpenGlFrame averaged = new OpenGlFrame(frameId, new Dimension(width, height), 4, out);
return Collections.singletonMap(Channel.BRGA, new RealLensOpenGlFrame(new OpenGlFrame[]{ averaged }));
}
@Override
public void close() throws java.io.IOException {
super.close();
accum.close();
}
}