Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.util.FastColor.ABGR32;
import net.neoforged.neoforge.client.GlStateBackup;
Expand All @@ -15,6 +16,7 @@
import com.mojang.blaze3d.vertex.VertexSorting;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import org.joml.Matrix4f;
import org.jspecify.annotations.Nullable;
import org.lwjgl.opengl.GL11;
Expand All @@ -23,33 +25,39 @@
import org.lwjgl.opengl.GL21;
import org.lwjgl.opengl.GL30;

import java.util.List;
import java.util.SequencedMap;

/** Reusable render-thread transparent target, with IO-thread pixel conversion before PNG encoding. */
final class CraftingPlanGraphCapture implements AutoCloseable {

private final RenderState previous;
private final RenderTarget target = new CaptureTarget();
private final ByteBufferBuilder vertexMemory;
private final SequencedMap<RenderType, ByteBufferBuilder> glintBuffers = new Object2ObjectLinkedOpenHashMap<>(5);

CraftingPlanGraphCapture(int width, int height) {
RenderSystem.assertOnRenderThread();
this.previous = new RenderState();
ByteBufferBuilder allocated = null;
this.vertexMemory = new ByteBufferBuilder(786432);
try {
allocated = new ByteBufferBuilder(786432);
// Item rendering keeps both its glint and model consumers open while emitting vertices.
for (RenderType layer : List.of(RenderType.armorEntityGlint(), RenderType.glint(), RenderType.glintTranslucent(),
RenderType.entityGlint(), RenderType.entityGlintDirect())) {
this.glintBuffers.put(layer, new ByteBufferBuilder(layer.bufferSize()));
}
RenderSystem.disableScissor();
GL11.glDisable(GL11.GL_STENCIL_TEST);
RenderSystem.colorMask(true, true, true, true);
RenderSystem.depthMask(true);
RenderSystem.glBindBuffer(GL21.GL_PIXEL_UNPACK_BUFFER, 0);
this.target.resize(width, height, Minecraft.ON_OSX);
this.target.setClearColor(0, 0, 0, 0);
this.vertexMemory = allocated;
} catch (Throwable failure) {
try {
this.target.destroyBuffers();
if (allocated != null) allocated.close();
} finally {
this.previous.restore();
close();
} catch (Throwable cleanupFailure) {
failure.addSuppressed(cleanupFailure);
}
throw failure;
}
Expand All @@ -70,7 +78,7 @@ GuiGraphics begin(Minecraft client, double scale, double x, double y) {
RenderSystem.setShaderColor(1, 1, 1, 1);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
GuiGraphics graphics = new GuiGraphics(client, MultiBufferSource.immediate(this.vertexMemory));
GuiGraphics graphics = new GuiGraphics(client, MultiBufferSource.immediateWithBuffers(this.glintBuffers, this.vertexMemory));
graphics.pose().scale((float) scale, (float) scale, 1);
graphics.pose().translate(x, y, 0);
return graphics;
Expand Down Expand Up @@ -136,6 +144,7 @@ private static int unpremultiplyChannel(int channel, int alpha) {
public void close() {
try {
this.vertexMemory.close();
this.glintBuffers.values().forEach(ByteBufferBuilder::close);
} finally {
try {
this.target.destroyBuffers();
Expand Down
Loading