From e208d9babc799209b4734fa4da28079c2d36b586 Mon Sep 17 00:00:00 2001 From: mcrcortex <18544518+MCRcortex@users.noreply.github.com> Date: Thu, 29 Aug 2024 10:37:28 +1000 Subject: [PATCH] Removed NativeImage Y flip (moved to gpu) Removed un-needed framebuffers Removed multi-framebuffer async path and made everything "async" --- .../flashback/exporting/ExportJob.java | 43 ++++----------- .../exporting/SaveableFramebuffer.java | 38 ++----------- .../exporting/SaveableFramebufferQueue.java | 54 ++++++++++++++++++- .../shaders/core/blit_screen_flip.json | 9 ++++ .../shaders/core/blit_screen_flip.vsh | 11 ++++ 5 files changed, 85 insertions(+), 70 deletions(-) create mode 100644 src/main/resources/assets/minecraft/shaders/core/blit_screen_flip.json create mode 100644 src/main/resources/assets/minecraft/shaders/core/blit_screen_flip.vsh diff --git a/src/main/java/com/moulberry/flashback/exporting/ExportJob.java b/src/main/java/com/moulberry/flashback/exporting/ExportJob.java index af65e9f..763c9be 100644 --- a/src/main/java/com/moulberry/flashback/exporting/ExportJob.java +++ b/src/main/java/com/moulberry/flashback/exporting/ExportJob.java @@ -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; diff --git a/src/main/java/com/moulberry/flashback/exporting/SaveableFramebuffer.java b/src/main/java/com/moulberry/flashback/exporting/SaveableFramebuffer.java index 8250b09..108c822 100644 --- a/src/main/java/com/moulberry/flashback/exporting/SaveableFramebuffer.java +++ b/src/main/java/com/moulberry/flashback/exporting/SaveableFramebuffer.java @@ -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; diff --git a/src/main/java/com/moulberry/flashback/exporting/SaveableFramebufferQueue.java b/src/main/java/com/moulberry/flashback/exporting/SaveableFramebufferQueue.java index 7e90165..432a7a6 100644 --- a/src/main/java/com/moulberry/flashback/exporting/SaveableFramebufferQueue.java +++ b/src/main/java/com/moulberry/flashback/exporting/SaveableFramebufferQueue.java @@ -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 available = new ArrayList<>(); private final List 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(); } diff --git a/src/main/resources/assets/minecraft/shaders/core/blit_screen_flip.json b/src/main/resources/assets/minecraft/shaders/core/blit_screen_flip.json new file mode 100644 index 0000000..c95969a --- /dev/null +++ b/src/main/resources/assets/minecraft/shaders/core/blit_screen_flip.json @@ -0,0 +1,9 @@ +{ + "vertex": "blit_screen_flip", + "fragment": "blit_screen", + "samplers": [ + { "name": "DiffuseSampler" } + ], + "uniforms": [ + ] +} diff --git a/src/main/resources/assets/minecraft/shaders/core/blit_screen_flip.vsh b/src/main/resources/assets/minecraft/shaders/core/blit_screen_flip.vsh new file mode 100644 index 0000000..94fb5f0 --- /dev/null +++ b/src/main/resources/assets/minecraft/shaders/core/blit_screen_flip.vsh @@ -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; +}