Skip to content

Commit 59170a9

Browse files
committed
Major advances in PanoramaRendererUtility
1 parent e62c5e1 commit 59170a9

File tree

6 files changed

+177
-129
lines changed

6 files changed

+177
-129
lines changed

src/main/java/org/visuals/legacy/animatium/mixins/accessor/GuiRendererAccessor.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
/**
2+
* Animatium
3+
* The all-you-could-want legacy animations mod for modern minecraft versions.
4+
* Brings back animations from the 1.7/1.8 era and more.
5+
* <p>
6+
* Copyright (C) 2024-2025 lowercasebtw
7+
* Copyright (C) 2024-2025 mixces
8+
* Copyright (C) 2024-2025 Contributors to the project retain their copyright
9+
* <p>
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
* <p>
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
* <p>
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
* <p>
23+
* "MINECRAFT" LINKING EXCEPTION TO THE GPL
24+
*/
25+
126
package org.visuals.legacy.animatium.mixins.accessor;
227

328
import net.minecraft.client.gui.render.GuiRenderer;

src/main/java/org/visuals/legacy/animatium/mixins/v1/gui/MixinPanoramaRenderer_LegacyRendering.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
3030
import com.llamalad7.mixinextras.sugar.Local;
3131
import com.mojang.blaze3d.pipeline.RenderPipeline;
32-
import com.mojang.blaze3d.pipeline.RenderTarget;
3332
import net.minecraft.client.Minecraft;
3433
import net.minecraft.client.gui.GuiGraphics;
3534
import net.minecraft.client.renderer.CubeMap;
@@ -46,9 +45,8 @@ public abstract class MixinPanoramaRenderer_LegacyRendering {
4645
@WrapOperation(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/CubeMap;render(Lnet/minecraft/client/Minecraft;FF)V", ordinal = 0))
4746
private void animatium$panoramaRendering(CubeMap instance, Minecraft minecraft, float xRot, float yRot, Operation<Void> original, @Local(argsOnly = true) GuiGraphics guiGraphics, @Local(argsOnly = true, ordinal = 0) int width, @Local(argsOnly = true, ordinal = 1) int height) {
4847
if (Animatium.ENABLED && AnimatiumConfig.instance().screen.panoramaRendering) {
49-
final RenderTarget renderTarget = minecraft.getMainRenderTarget();
5048
PanoramaRendererUtility.update(minecraft.getDeltaTracker().getRealtimeDeltaTicks());
51-
PanoramaRendererUtility.render(guiGraphics, renderTarget, width, height);
49+
PanoramaRendererUtility.render(guiGraphics, width, height);
5250
} else {
5351
original.call(instance, minecraft, xRot, yRot);
5452
}

src/main/java/org/visuals/legacy/animatium/util/PanoramaRendererUtility.java

Lines changed: 29 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,13 @@
2727

2828
import com.mojang.blaze3d.ProjectionType;
2929
import com.mojang.blaze3d.buffers.GpuBufferSlice;
30-
import com.mojang.blaze3d.opengl.GlStateManager;
31-
import com.mojang.blaze3d.opengl.GlTexture;
32-
import com.mojang.blaze3d.opengl.GlTextureView;
3330
import com.mojang.blaze3d.pipeline.BlendFunction;
3431
import com.mojang.blaze3d.pipeline.RenderPipeline;
3532
import com.mojang.blaze3d.pipeline.RenderTarget;
3633
import com.mojang.blaze3d.platform.DestFactor;
3734
import com.mojang.blaze3d.platform.SourceFactor;
38-
import com.mojang.blaze3d.systems.GpuDevice;
3935
import com.mojang.blaze3d.systems.RenderSystem;
40-
import com.mojang.blaze3d.textures.GpuTexture;
4136
import com.mojang.blaze3d.textures.GpuTextureView;
42-
import com.mojang.blaze3d.textures.TextureFormat;
4337
import com.mojang.blaze3d.vertex.*;
4438
import lombok.experimental.UtilityClass;
4539
import net.minecraft.client.Minecraft;
@@ -56,8 +50,6 @@
5650
import org.jetbrains.annotations.Nullable;
5751
import org.joml.Matrix3x2f;
5852
import org.joml.Matrix4fStack;
59-
import org.joml.Vector4i;
60-
import org.lwjgl.opengl.GL11;
6153
import org.visuals.legacy.animatium.Animatium;
6254

6355
@UtilityClass
@@ -78,18 +70,6 @@ public class PanoramaRendererUtility {
7870
.withVertexFormat(DefaultVertexFormat.POSITION_TEX_COLOR, VertexFormat.Mode.QUADS)
7971
.build();
8072

81-
private final RenderPipeline PANORAMA_BLUR =
82-
RenderPipeline.builder(RenderPipelines.GUI_TEXTURED_SNIPPET)
83-
.withLocation(Animatium.location("pipeline/panorama_blur"))
84-
.withBlend(PANORAMA_BLEND)
85-
.withColorWrite(true, false)
86-
.build();
87-
88-
private final RenderPipeline SIMPLE_TEXTURE =
89-
RenderPipeline.builder(RenderPipelines.GUI_TEXTURED_SNIPPET)
90-
.withLocation(Animatium.location("pipeline/blit_texture"))
91-
.build();
92-
9373
private final ResourceLocation[] PANORAMA_LOCATIONS = new ResourceLocation[]{
9474
ResourceLocation.withDefaultNamespace("textures/gui/title/background/panorama_0.png"),
9575
ResourceLocation.withDefaultNamespace("textures/gui/title/background/panorama_1.png"),
@@ -100,50 +80,33 @@ public class PanoramaRendererUtility {
10080
};
10181

10282
private CachedPerspectiveProjectionMatrixBuffer projectionMatrixBuffer = null;
103-
private GlTexture backgroundTexture = null;
104-
private GlTextureView backgroundTextureView = null;
83+
private PanoramaTarget panoramaTarget = null;
10584
private float spin = 0.0F;
10685

10786
static {
10887
setup();
10988
}
11089

11190
private void setup() {
112-
final int width = 256;
113-
final int height = 256;
114-
115-
final GpuDevice device = RenderSystem.getDevice();
116-
if (backgroundTexture == null) {
117-
final int flags = GpuTexture.USAGE_RENDER_ATTACHMENT | GpuTexture.USAGE_TEXTURE_BINDING | GpuTexture.USAGE_COPY_SRC;
118-
backgroundTexture = (GlTexture) device.createTexture(() -> "Background texture", flags, TextureFormat.RGBA8, width, height, 1, 1);
119-
}
120-
121-
if (backgroundTextureView == null) {
122-
backgroundTextureView = (GlTextureView) device.createTextureView(backgroundTexture);
91+
if (panoramaTarget == null) {
92+
panoramaTarget = new PanoramaTarget();
12393
}
12494

12595
if (projectionMatrixBuffer == null) {
12696
projectionMatrixBuffer = new CachedPerspectiveProjectionMatrixBuffer("panorama", 0.05F, 10.0F);
12797
}
12898
}
12999

130-
public void render(final GuiGraphics guiGraphics, final RenderTarget renderTarget, final int width, final int height) {
131-
final RenderUtils.RenderOverrides properties = new RenderUtils.RenderOverrides(new Vector4i(0, 0, 256, 256), 0);
132-
renderPanorama(PANORAMA, renderTarget, width, height, properties);
100+
public void render(final GuiGraphics guiGraphics, final int width, final int height) {
101+
renderPanorama(PANORAMA, panoramaTarget, width, height);
133102
for (int layer = 0; layer < 7; ++layer) {
134-
final int prevTex = GlStateManager._getInteger(GL11.GL_TEXTURE_BINDING_2D);
135-
GlStateManager._bindTexture(backgroundTexture.glId());
136-
GlStateManager._texParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
137-
GlStateManager._texParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
138-
GL11.glCopyTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, 0, 0, 256, 256);
139-
GlStateManager._bindTexture(prevTex);
140-
RenderUtils.drawInGui(renderTarget, new BlitBlurTexture(guiGraphics.pose(), backgroundTextureView, width, height), RenderUtils.RenderOverrides.DEFAULT);
103+
RenderUtils.drawInGui(panoramaTarget, new BlitBlurTexture(guiGraphics.pose(), panoramaTarget.getColorTextureView(), width, height));
141104
}
142105

143-
// RenderUtils.renderInGui(renderTarget, new BlitFinalTexture(guiGraphics.pose(), backgroundTextureView, width, height), RenderUtils.RenderProperties.DEFAULT);
106+
guiGraphics.guiRenderState.submitGuiElement(new BlitFinalTexture(guiGraphics.pose(), panoramaTarget.getColorTextureView(), width, height, ARGB.white(1.0F)));
144107
}
145108

146-
private void renderPanorama(final RenderPipeline pipeline, final RenderTarget renderTarget, final int width, final int height, final RenderUtils.RenderOverrides renderOverrides) {
109+
private void renderPanorama(final RenderPipeline pipeline, final RenderTarget renderTarget, final int width, final int height) {
147110
RenderSystem.setProjectionMatrix(projectionMatrixBuffer.getBuffer(width, height, 120.0F), ProjectionType.PERSPECTIVE);
148111
final Matrix4fStack modelViewStack = RenderSystem.getModelViewStack();
149112
modelViewStack.pushMatrix();
@@ -180,7 +143,7 @@ private void renderPanorama(final RenderPipeline pipeline, final RenderTarget re
180143
builder.addVertex(1.0F, 1.0F, 1.0F).setUv(1.0F, 1.0F).setColor(color);
181144
builder.addVertex(-1.0F, 1.0F, 1.0F).setUv(0.0F, 1.0F).setColor(color);
182145
final GpuBufferSlice dynamicTransforms = DynamicTransformsBuilder.of().withModelViewMatrix(modelViewStack).build();
183-
RenderUtils.drawWithPipeline(renderTarget, pipeline, builder.buildOrThrow(), renderOverrides, (pass) -> {
146+
RenderUtils.drawWithPipeline(renderTarget, pipeline, builder.buildOrThrow(), (pass) -> {
184147
pass.setUniform("DynamicTransforms", dynamicTransforms);
185148
pass.bindSampler("Sampler0", panoramaTexture);
186149
});
@@ -193,7 +156,6 @@ private void renderPanorama(final RenderPipeline pipeline, final RenderTarget re
193156
}
194157

195158
modelViewStack.popMatrix();
196-
197159
}
198160

199161
public void update(float tickDelta) {
@@ -208,23 +170,26 @@ private float getYRot() {
208170
return -spin * 0.1F;
209171
}
210172

211-
private record BlitBlurTexture(Matrix3x2f pose, GpuTextureView texture, int width,
212-
int height) implements GuiElementRenderState {
173+
private record BlitBlurTexture(Matrix3x2f pose, GpuTextureView texture, int width, int height) implements GuiElementRenderState {
213174
@Override
214175
public void buildVertices(VertexConsumer consumer) {
215176
for (int cycle = 0; cycle < 3; cycle++) {
216177
final int color = ARGB.white(1.0F / (cycle + 1));
217178
final float growth = (cycle - 1) / 256.0F;
218-
consumer.addVertex(this.width, this.height, 0.0F).setUv(0.0F + growth, 1.0F).setColor(color);
219-
consumer.addVertex(this.width, 0.0F, 0.0F).setUv(1.0F + growth, 1.0F).setColor(color);
220-
consumer.addVertex(0.0F, 0.0F, 0.0F).setUv(1.0F + growth, 0.0F).setColor(color);
221-
consumer.addVertex(0.0F, this.height, 0.0F).setUv(0.0F + growth, 0.0F).setColor(color);
179+
consumer.addVertexWith2DPose(this.pose, this.width, this.height).setUv(0.0F + growth, 1.0F).setColor(color);
180+
consumer.addVertexWith2DPose(this.pose, this.width, 0.0F).setUv(1.0F + growth, 1.0F).setColor(color);
181+
consumer.addVertexWith2DPose(this.pose, 0.0F, 0.0F).setUv(1.0F + growth, 0.0F).setColor(color);
182+
consumer.addVertexWith2DPose(this.pose, 0.0F, this.height).setUv(0.0F + growth, 0.0F).setColor(color);
222183
}
223184
}
224185

225186
@Override
226187
public @NotNull RenderPipeline pipeline() {
227-
return PANORAMA_BLUR;
188+
return RenderPipeline.builder(RenderPipelines.GUI_TEXTURED_SNIPPET)
189+
.withLocation(Animatium.location("pipeline/panorama_blur"))
190+
.withBlend(PANORAMA_BLEND)
191+
.withColorWrite(true, false)
192+
.build();
228193
}
229194

230195
@Override
@@ -243,28 +208,26 @@ public void buildVertices(VertexConsumer consumer) {
243208
}
244209
}
245210

246-
private record BlitFinalTexture(Matrix3x2f pose, GpuTextureView texture, int width,
247-
int height) implements GuiElementRenderState {
211+
public record BlitFinalTexture(Matrix3x2f pose, GpuTextureView texture, int width, int height, int color) implements GuiElementRenderState {
248212
@Override
249213
public void buildVertices(VertexConsumer consumer) {
250-
float aspect = 120.0F / (Math.max(this.width, this.height));
251-
float sw = this.width * aspect / 256.0F;
252-
float sh = this.height * aspect / 256.0F;
253-
final int color = ARGB.white(1.0F);
254-
consumer.addVertexWith2DPose(this.pose, 0.0F, this.height).setUv(0.5F - sh, 0.5F + sw).setColor(color);
255-
consumer.addVertexWith2DPose(this.pose, this.width, this.height).setUv(0.5F - sh, 0.5F - sw).setColor(color);
256-
consumer.addVertexWith2DPose(this.pose, this.width, 0.0F).setUv(0.5F + sh, 0.5F - sw).setColor(color);
257-
consumer.addVertexWith2DPose(this.pose, 0.0F, 0.0F).setUv(0.5F + sh, 0.5F + sw).setColor(color);
214+
final float aspect = 120.0F / (Math.max(this.width, this.height));
215+
final float sw = this.width * aspect / panoramaTarget.width;
216+
final float sh = this.height * aspect / panoramaTarget.height;
217+
consumer.addVertexWith2DPose(this.pose, 0.0F, this.height).setUv(0.5F - sh, 0.5F + sw).setColor(this.color);
218+
consumer.addVertexWith2DPose(this.pose, this.width, this.height).setUv(0.5F - sh, 0.5F - sw).setColor(this.color);
219+
consumer.addVertexWith2DPose(this.pose, this.width, 0.0F).setUv(0.5F + sh, 0.5F - sw).setColor(this.color);
220+
consumer.addVertexWith2DPose(this.pose, 0.0F, 0.0F).setUv(0.5F + sh, 0.5F + sw).setColor(this.color);
258221
}
259222

260223
@Override
261224
public @NotNull RenderPipeline pipeline() {
262-
return SIMPLE_TEXTURE;
225+
return RenderPipelines.GUI_TEXTURED;
263226
}
264227

265228
@Override
266229
public @NotNull TextureSetup textureSetup() {
267-
return TextureSetup.singleTexture(texture);
230+
return TextureSetup.singleTexture(this.texture);
268231
}
269232

270233
@Override
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Animatium
3+
* The all-you-could-want legacy animations mod for modern minecraft versions.
4+
* Brings back animations from the 1.7/1.8 era and more.
5+
* <p>
6+
* Copyright (C) 2024-2025 lowercasebtw
7+
* Copyright (C) 2024-2025 mixces
8+
* Copyright (C) 2024-2025 Contributors to the project retain their copyright
9+
* <p>
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
* <p>
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
* <p>
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
* <p>
23+
* "MINECRAFT" LINKING EXCEPTION TO THE GPL
24+
*/
25+
26+
package org.visuals.legacy.animatium.util;
27+
28+
import com.mojang.blaze3d.GpuOutOfMemoryException;
29+
import com.mojang.blaze3d.pipeline.MainTarget;
30+
import com.mojang.blaze3d.pipeline.RenderTarget;
31+
import com.mojang.blaze3d.systems.RenderSystem;
32+
import com.mojang.blaze3d.textures.AddressMode;
33+
import com.mojang.blaze3d.textures.FilterMode;
34+
import com.mojang.blaze3d.textures.GpuTexture;
35+
import com.mojang.blaze3d.textures.TextureFormat;
36+
import org.jetbrains.annotations.Nullable;
37+
38+
// Custom Version of Minecraft MainTarget
39+
public class PanoramaTarget extends RenderTarget {
40+
private final int FLAGS = GpuTexture.USAGE_RENDER_ATTACHMENT | GpuTexture.USAGE_TEXTURE_BINDING | GpuTexture.USAGE_COPY_SRC | GpuTexture.USAGE_COPY_DST;
41+
42+
public PanoramaTarget() {
43+
super("Panorama", false );
44+
45+
final MainTarget.Dimension dimension = this.allocateAttachments(256, 256);
46+
if (this.colorTexture != null) {
47+
this.colorTexture.setTextureFilter(FilterMode.LINEAR, false);
48+
this.colorTexture.setAddressMode(AddressMode.CLAMP_TO_EDGE);
49+
} else {
50+
throw new IllegalStateException("Missing color textures");
51+
}
52+
53+
if (this.depthTexture != null) {
54+
this.depthTexture.setTextureFilter(FilterMode.LINEAR, false);
55+
this.depthTexture.setAddressMode(AddressMode.CLAMP_TO_EDGE);
56+
} else {
57+
throw new IllegalStateException("Missing depth textures");
58+
}
59+
60+
this.width = dimension.width;
61+
this.height = dimension.height;
62+
RenderSystem.getDevice().createCommandEncoder().clearColorAndDepthTextures(this.getColorTexture(), 0, this.getDepthTexture(), 1.0F);
63+
}
64+
65+
private MainTarget.Dimension allocateAttachments(int width, int height) {
66+
RenderSystem.assertOnRenderThread();
67+
for (MainTarget.Dimension dimension : MainTarget.Dimension.listWithFallback(width, height)) {
68+
if (this.colorTexture != null) {
69+
this.colorTexture.close();
70+
this.colorTexture = null;
71+
if (this.colorTextureView != null) {
72+
this.colorTextureView.close();
73+
this.colorTextureView = null;
74+
}
75+
}
76+
77+
if (this.depthTexture != null) {
78+
this.depthTexture.close();
79+
this.depthTexture = null;
80+
if (this.depthTextureView != null) {
81+
this.depthTextureView.close();
82+
this.depthTextureView = null;
83+
}
84+
}
85+
86+
this.colorTexture = this.allocateColorAttachment(dimension);
87+
this.depthTexture = this.allocateDepthAttachment(dimension);
88+
if (this.colorTexture != null && this.depthTexture != null) {
89+
this.colorTextureView = RenderSystem.getDevice().createTextureView(this.colorTexture);
90+
this.depthTextureView = RenderSystem.getDevice().createTextureView(this.depthTexture);
91+
return dimension;
92+
}
93+
}
94+
95+
final String hasColor = this.colorTexture == null ? "missing color" : "have color";
96+
final String hasDepth = this.depthTexture == null ? "missing depth" : "have depth";
97+
throw new RuntimeException("Unrecoverable GL_OUT_OF_MEMORY (" + hasColor + ", " + hasDepth + ")");
98+
}
99+
100+
@Nullable
101+
private GpuTexture allocateColorAttachment(MainTarget.Dimension dimension) {
102+
try {
103+
return RenderSystem.getDevice().createTexture(() -> this.label + " / Color", FLAGS, TextureFormat.RGBA8, dimension.width, dimension.height, 1, 1);
104+
} catch (GpuOutOfMemoryException exception) {
105+
return null;
106+
}
107+
}
108+
109+
@Nullable
110+
private GpuTexture allocateDepthAttachment(MainTarget.Dimension dimension) {
111+
try {
112+
return RenderSystem.getDevice().createTexture(() -> this.label + " / Depth", FLAGS, TextureFormat.DEPTH32, dimension.width, dimension.height, 1, 1);
113+
} catch (GpuOutOfMemoryException exception) {
114+
return null;
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)