Merge pull request #11 from MCRcortex/simple_perf_improvement

Improved export performance (especially when using shaders) and decreased vram utilization
This commit is contained in:
Moulberry authored and GitHub committed 2024-08-29 09:00:40 +08:00
commit d2a217509f
5 files changed
+85 -70

No files matched your search

@@ -247,21 +247,12 @@ public class ExportJob {
KeyframeHandler keyframeHandler = new MinecraftKeyframeHandler(Minecraft.getInstance());
this.settings.editorState().applyKeyframes(keyframeHandler, (float)(this.settings.startTick() + currentTickDouble));
boolean asyncFrameCapture = !IrisApiWrapper.isIrisAvailable() || !IrisApiWrapper.isShaderPackInUse();
RenderTarget oldTarget = null;
SaveableFramebuffer saveable = null;
SaveableFramebuffer saveable;
RenderTarget renderTarget;
if (asyncFrameCapture) {
oldTarget = Minecraft.getInstance().getMainRenderTarget();
saveable = downloader.take();
renderTarget = saveable.getFramebuffer(this.settings.resolutionX(), this.settings.resolutionY());
Minecraft.getInstance().mainRenderTarget = renderTarget;
} else {
renderTarget = Minecraft.getInstance().mainRenderTarget;
}
saveable = downloader.take();
renderTarget = Minecraft.getInstance().mainRenderTarget;
renderTarget.bindWrite(true);
RenderSystem.clear(16640, Minecraft.ON_OSX);
@@ -296,30 +287,14 @@ public class ExportJob {
SOFTLoopback.alcRenderSamplesSOFT(device, audioBuffer, renderSamples);
}
if (asyncFrameCapture) {
Minecraft.getInstance().mainRenderTarget = oldTarget;
this.shouldChangeFramebufferSize = false;
cancel = finishFrame(renderTarget, infoRenderTarget, tickIndex, ticks.size());
this.shouldChangeFramebufferSize = true;
this.shouldChangeFramebufferSize = false;
cancel = finishFrame(renderTarget, infoRenderTarget, tickIndex, ticks.size());
this.shouldChangeFramebufferSize = true;
submitDownloadedFrames(encoder, downloader, false);
submitDownloadedFrames(encoder, downloader, false);
saveable.audioBuffer = audioBuffer;
downloader.startDownload(saveable);
} else {
this.shouldChangeFramebufferSize = false;
cancel = finishFrame(renderTarget, infoRenderTarget, tickIndex, ticks.size());
this.shouldChangeFramebufferSize = true;
start = System.nanoTime();
NativeImage image = Screenshot.takeScreenshot(renderTarget);
downloadTimeNanos += System.nanoTime() - start;
start = System.nanoTime();
encoder.encode(image, audioBuffer);
encodeTimeNanos += System.nanoTime() - start;
}
saveable.audioBuffer = audioBuffer;
downloader.startDownload(renderTarget, saveable);
if (cancel) {
ExportJobQueue.drainingQueue = false;
@@ -1,6 +1,7 @@
package com.moulberry.flashback.exporting;
import com.mojang.blaze3d.pipeline.MainTarget;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.platform.NativeImage;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL30C;
@@ -10,35 +11,17 @@ import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
public class SaveableFramebuffer implements AutoCloseable {
private MainTarget framebuffer;
private int pboId;
public @Nullable FloatBuffer audioBuffer;
private boolean isDownloading = false;
public SaveableFramebuffer() {
this.framebuffer = null;
this.pboId = -1;
}
public boolean isDownloading() {
return isDownloading;
}
public MainTarget getFramebuffer(int width, int height) {
if (this.isDownloading) {
throw new IllegalStateException("Can't get the framebuffer for rendering while still downloading");
}
if (this.framebuffer == null) {
this.framebuffer = new MainTarget(width, height);
}
return this.framebuffer;
}
public void startDownload(int width, int height) {
public void startDownload(RenderTarget framebuffer, int width, int height) {
if (this.isDownloading) {
throw new IllegalStateException("Can't start downloading while already downloading");
}
@@ -52,13 +35,13 @@ public class SaveableFramebuffer implements AutoCloseable {
GL30C.glBindBuffer(GL30C.GL_PIXEL_PACK_BUFFER, 0);
}
this.framebuffer.bindWrite(true);
framebuffer.bindWrite(true);
GL30C.glBindBuffer(GL30C.GL_PIXEL_PACK_BUFFER, this.pboId);
GL30C.glReadPixels(0, 0, width, height, GL30C.GL_RGBA, GL30C.GL_UNSIGNED_BYTE, 0);
GL30C.glBindBuffer(GL30C.GL_PIXEL_PACK_BUFFER, 0);
this.framebuffer.unbindWrite();
framebuffer.unbindWrite();
}
public NativeImage finishDownload(int width, int height) {
@@ -79,15 +62,6 @@ public class SaveableFramebuffer implements AutoCloseable {
// Copy bytes
MemoryUtil.memCopy(MemoryUtil.memAddress(buffer), nativeImage.pixels, nativeImage.size);
// for (int y = 0; y < nativeImage.getHeight(); ++y) {
// for (int x = 0; x < nativeImage.getWidth(); ++x) {
// int rgba = nativeImage.getPixelRGBA(x, y);
// nativeImage.setPixelRGBA(x, y, (rgba & 0xFFFFFF) | (80 << NativeImage.Format.RGBA.alphaOffset()));
// }
// }
nativeImage.flipY();
GL30C.glUnmapBuffer(GL30C.GL_PIXEL_PACK_BUFFER);
GL30C.glBindBuffer(GL30C.GL_PIXEL_PACK_BUFFER, 0);
@@ -95,10 +69,6 @@ public class SaveableFramebuffer implements AutoCloseable {
}
public void close() {
if (this.framebuffer != null) {
this.framebuffer.destroyBuffers();
this.framebuffer = null;
}
if (this.pboId != -1) {
GL30C.glDeleteBuffers(this.pboId);
this.pboId = -1;
@@ -1,8 +1,19 @@
package com.moulberry.flashback.exporting;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.pipeline.TextureTarget;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ShaderInstance;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.ArrayList;
@@ -18,13 +29,23 @@ public class SaveableFramebufferQueue implements AutoCloseable {
private final List<SaveableFramebuffer> available = new ArrayList<>();
private final List<SaveableFramebuffer> waiting = new ArrayList<>();
private final RenderTarget flipbuffer;
private final ShaderInstance flipShader;
public SaveableFramebufferQueue(int width, int height) {
this.width = width;
this.height = height;
this.flipbuffer = new TextureTarget(width, height, false, false);
for (int i = 0; i < CAPACITY; i++) {
this.available.add(new SaveableFramebuffer());
}
try {
this.flipShader = new ShaderInstance(Minecraft.getInstance().getResourceManager(), "blit_screen_flip", DefaultVertexFormat.BLIT_SCREEN);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public SaveableFramebuffer take() {
@@ -34,8 +55,35 @@ public class SaveableFramebufferQueue implements AutoCloseable {
return this.available.removeFirst();
}
public void startDownload(SaveableFramebuffer texture) {
texture.startDownload(this.width, this.height);
private void blitFlip(RenderTarget src) {
GlStateManager._colorMask(true, true, true, true);
GlStateManager._disableDepthTest();
GlStateManager._depthMask(false);
GlStateManager._viewport(0, 0, src.width, src.height);
GlStateManager._disableBlend();
RenderSystem.disableCull();
this.flipbuffer.bindWrite(true);
this.flipShader.setSampler("DiffuseSampler", src.colorTextureId);
this.flipShader.apply();
BufferBuilder bufferBuilder = RenderSystem.renderThreadTesselator().begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLIT_SCREEN);
bufferBuilder.addVertex(0.0F, 1.0F, 0.0F);
bufferBuilder.addVertex(1.0F, 1.0F, 0.0F);
bufferBuilder.addVertex(1.0F, 0.0F, 0.0F);
bufferBuilder.addVertex(0.0F, 0.0F, 0.0F);
BufferUploader.draw(bufferBuilder.buildOrThrow());
this.flipShader.clear();
GlStateManager._depthMask(true);
GlStateManager._colorMask(true, true, true, true);
RenderSystem.enableCull();
}
public void startDownload(RenderTarget target, SaveableFramebuffer texture) {
//Do an inline flip
this.blitFlip(target);
texture.startDownload(this.flipbuffer, this.width, this.height);
this.waiting.add(texture);
}
@@ -70,6 +118,8 @@ public class SaveableFramebufferQueue implements AutoCloseable {
}
this.waiting.clear();
this.available.clear();
this.flipbuffer.destroyBuffers();
this.flipShader.close();
}
@@ -0,0 +1,9 @@
{
"vertex": "blit_screen_flip",
"fragment": "blit_screen",
"samplers": [
{ "name": "DiffuseSampler" }
],
"uniforms": [
]
}
@@ -0,0 +1,11 @@
#version 150
in vec3 Position;
out vec2 texCoord;
void main() {
vec2 screenPos = vec2(Position.x, 1-Position.y) * 2.0 - 1.0;
gl_Position = vec4(screenPos.x, screenPos.y, 1.0, 1.0);
texCoord = Position.xy;
}