Removed NativeImage Y flip (moved to gpu)

Removed un-needed framebuffers
Removed multi-framebuffer async path and made everything "async"
This commit is contained in:
mcrcortex committed 2024-08-29 10:37:28 +10:00
1 parent a6493b623c
commit e208d9babc
5 files changed
+85 -70

No files matched your search

@@ -247,21 +247,12 @@ public class ExportJob {
KeyframeHandler keyframeHandler = new MinecraftKeyframeHandler(Minecraft.getInstance()); KeyframeHandler keyframeHandler = new MinecraftKeyframeHandler(Minecraft.getInstance());
this.settings.editorState().applyKeyframes(keyframeHandler, (float)(this.settings.startTick() + currentTickDouble)); this.settings.editorState().applyKeyframes(keyframeHandler, (float)(this.settings.startTick() + currentTickDouble));
boolean asyncFrameCapture = !IrisApiWrapper.isIrisAvailable() || !IrisApiWrapper.isShaderPackInUse();
RenderTarget oldTarget = null; SaveableFramebuffer saveable;
SaveableFramebuffer saveable = null;
RenderTarget renderTarget; RenderTarget renderTarget;
if (asyncFrameCapture) { saveable = downloader.take();
oldTarget = Minecraft.getInstance().getMainRenderTarget(); renderTarget = Minecraft.getInstance().mainRenderTarget;
saveable = downloader.take();
renderTarget = saveable.getFramebuffer(this.settings.resolutionX(), this.settings.resolutionY());
Minecraft.getInstance().mainRenderTarget = renderTarget;
} else {
renderTarget = Minecraft.getInstance().mainRenderTarget;
}
renderTarget.bindWrite(true); renderTarget.bindWrite(true);
RenderSystem.clear(16640, Minecraft.ON_OSX); RenderSystem.clear(16640, Minecraft.ON_OSX);
@@ -296,30 +287,14 @@ public class ExportJob {
SOFTLoopback.alcRenderSamplesSOFT(device, audioBuffer, renderSamples); SOFTLoopback.alcRenderSamplesSOFT(device, audioBuffer, renderSamples);
} }
if (asyncFrameCapture) { this.shouldChangeFramebufferSize = false;
Minecraft.getInstance().mainRenderTarget = oldTarget; cancel = finishFrame(renderTarget, infoRenderTarget, tickIndex, ticks.size());
this.shouldChangeFramebufferSize = true;
this.shouldChangeFramebufferSize = false; submitDownloadedFrames(encoder, downloader, false);
cancel = finishFrame(renderTarget, infoRenderTarget, tickIndex, ticks.size());
this.shouldChangeFramebufferSize = true;
submitDownloadedFrames(encoder, downloader, false); saveable.audioBuffer = audioBuffer;
downloader.startDownload(renderTarget, saveable);
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;
}
if (cancel) { if (cancel) {
ExportJobQueue.drainingQueue = false; ExportJobQueue.drainingQueue = false;
@@ -1,6 +1,7 @@
package com.moulberry.flashback.exporting; package com.moulberry.flashback.exporting;
import com.mojang.blaze3d.pipeline.MainTarget; import com.mojang.blaze3d.pipeline.MainTarget;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.platform.NativeImage;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL30C; import org.lwjgl.opengl.GL30C;
@@ -10,35 +11,17 @@ import java.nio.ByteBuffer;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
public class SaveableFramebuffer implements AutoCloseable { public class SaveableFramebuffer implements AutoCloseable {
private MainTarget framebuffer;
private int pboId; private int pboId;
public @Nullable FloatBuffer audioBuffer; public @Nullable FloatBuffer audioBuffer;
private boolean isDownloading = false; private boolean isDownloading = false;
public SaveableFramebuffer() { public SaveableFramebuffer() {
this.framebuffer = null;
this.pboId = -1; this.pboId = -1;
} }
public boolean isDownloading() {
return isDownloading;
}
public MainTarget getFramebuffer(int width, int height) { public void startDownload(RenderTarget framebuffer, 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) {
if (this.isDownloading) { if (this.isDownloading) {
throw new IllegalStateException("Can't start downloading while already downloading"); 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); 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.glBindBuffer(GL30C.GL_PIXEL_PACK_BUFFER, this.pboId);
GL30C.glReadPixels(0, 0, width, height, GL30C.GL_RGBA, GL30C.GL_UNSIGNED_BYTE, 0); GL30C.glReadPixels(0, 0, width, height, GL30C.GL_RGBA, GL30C.GL_UNSIGNED_BYTE, 0);
GL30C.glBindBuffer(GL30C.GL_PIXEL_PACK_BUFFER, 0); GL30C.glBindBuffer(GL30C.GL_PIXEL_PACK_BUFFER, 0);
this.framebuffer.unbindWrite(); framebuffer.unbindWrite();
} }
public NativeImage finishDownload(int width, int height) { public NativeImage finishDownload(int width, int height) {
@@ -79,15 +62,6 @@ public class SaveableFramebuffer implements AutoCloseable {
// Copy bytes // Copy bytes
MemoryUtil.memCopy(MemoryUtil.memAddress(buffer), nativeImage.pixels, nativeImage.size); 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.glUnmapBuffer(GL30C.GL_PIXEL_PACK_BUFFER);
GL30C.glBindBuffer(GL30C.GL_PIXEL_PACK_BUFFER, 0); GL30C.glBindBuffer(GL30C.GL_PIXEL_PACK_BUFFER, 0);
@@ -95,10 +69,6 @@ public class SaveableFramebuffer implements AutoCloseable {
} }
public void close() { public void close() {
if (this.framebuffer != null) {
this.framebuffer.destroyBuffers();
this.framebuffer = null;
}
if (this.pboId != -1) { if (this.pboId != -1) {
GL30C.glDeleteBuffers(this.pboId); GL30C.glDeleteBuffers(this.pboId);
this.pboId = -1; this.pboId = -1;
@@ -1,8 +1,19 @@
package com.moulberry.flashback.exporting; 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.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 org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.util.ArrayList; import java.util.ArrayList;
@@ -18,13 +29,23 @@ public class SaveableFramebufferQueue implements AutoCloseable {
private final List<SaveableFramebuffer> available = new ArrayList<>(); private final List<SaveableFramebuffer> available = new ArrayList<>();
private final List<SaveableFramebuffer> waiting = new ArrayList<>(); private final List<SaveableFramebuffer> waiting = new ArrayList<>();
private final RenderTarget flipbuffer;
private final ShaderInstance flipShader;
public SaveableFramebufferQueue(int width, int height) { public SaveableFramebufferQueue(int width, int height) {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.flipbuffer = new TextureTarget(width, height, false, false);
for (int i = 0; i < CAPACITY; i++) { for (int i = 0; i < CAPACITY; i++) {
this.available.add(new SaveableFramebuffer()); 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() { public SaveableFramebuffer take() {
@@ -34,8 +55,35 @@ public class SaveableFramebufferQueue implements AutoCloseable {
return this.available.removeFirst(); return this.available.removeFirst();
} }
public void startDownload(SaveableFramebuffer texture) { private void blitFlip(RenderTarget src) {
texture.startDownload(this.width, this.height); 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); this.waiting.add(texture);
} }
@@ -70,6 +118,8 @@ public class SaveableFramebufferQueue implements AutoCloseable {
} }
this.waiting.clear(); this.waiting.clear();
this.available.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;
}