DisplayList -> VBO for stars, skies, and ModelRenderer (#129)

This commit is contained in:
Jason Mitchell authored and GitHub committed 2024-01-04 10:49:27 -05:00
1 parent a8ccc8903a
commit c58870fdeb
15 files changed
+266 -76

No files matched your search

@@ -5,7 +5,7 @@ import com.gtnewhorizons.angelica.compat.mojang.VertexFormatElement.Type;
import com.gtnewhorizons.angelica.compat.mojang.VertexFormatElement.Usage;
import com.gtnewhorizons.angelica.compat.nd.writers.ItemVBOQuadWriter;
import com.gtnewhorizons.angelica.compat.nd.writers.PositionQuadWriter;
import net.minecraft.client.renderer.OpenGlHelper;
import com.gtnewhorizons.angelica.compat.nd.writers.PositionTextureQuadWriter;
public class DefaultVertexFormat {
@@ -17,14 +17,14 @@ public class DefaultVertexFormat {
public static final VertexFormatElement OVERLAY_ELEMENT = new VertexFormatElement(1, Type.SHORT, Usage.UV, 2);
public static final VertexFormatElement NORMAL_ELEMENT = new VertexFormatElement(0, Type.BYTE, Usage.NORMAL, 3);
public static final VertexFormatElement PADDING_ELEMENT = new VertexFormatElement(0, Type.BYTE, Usage.PADDING, 1);
public static final VertexFormat ITEM_VBO = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(TEXTURE_ELEMENT).add(NORMAL_ELEMENT).add(PADDING_ELEMENT).build(), new ItemVBOQuadWriter());
public static final VertexFormat POSITION_TEXTURE_NORMAL = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(TEXTURE_ELEMENT).add(NORMAL_ELEMENT).add(PADDING_ELEMENT).build(), new ItemVBOQuadWriter());
public static final VertexFormat POSITION_COLOR_TEXTURE_LIGHT_NORMAL = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(COLOR_ELEMENT).add(TEXTURE_ELEMENT).add(LIGHT_ELEMENT).add(NORMAL_ELEMENT).add(PADDING_ELEMENT).build());
public static final VertexFormat POSITION_COLOR_TEXTURE_OVERLAY_LIGHT_NORMAL = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(COLOR_ELEMENT).add(TEXTURE_ELEMENT).add(OVERLAY_ELEMENT).add(LIGHT_ELEMENT).add(NORMAL_ELEMENT).add(PADDING_ELEMENT).build());
public static final VertexFormat POSITION_TEXTURE_COLOR_LIGHT = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(TEXTURE_ELEMENT).add(COLOR_ELEMENT).add(LIGHT_ELEMENT).build());
public static final VertexFormat POSITION = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).build(), new PositionQuadWriter());
public static final VertexFormat POSITION_COLOR = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(COLOR_ELEMENT).build());
public static final VertexFormat POSITION_COLOR_LIGHT = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(COLOR_ELEMENT).add(LIGHT_ELEMENT).build());
public static final VertexFormat POSITION_TEXTURE = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(TEXTURE_ELEMENT).build());
public static final VertexFormat POSITION_TEXTURE = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(TEXTURE_ELEMENT).build(), new PositionTextureQuadWriter());
public static final VertexFormat POSITION_COLOR_TEXTURE = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(COLOR_ELEMENT).add(TEXTURE_ELEMENT).build());
public static final VertexFormat POSITION_TEXTURE_COLOR = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(TEXTURE_ELEMENT).add(COLOR_ELEMENT).build());
public static final VertexFormat POSITION_COLOR_TEX_LIGHTMAP = new VertexFormat(new ImmutableList.Builder<VertexFormatElement>().add(POSITION_ELEMENT).add(COLOR_ELEMENT).add(TEXTURE_ELEMENT).add(LIGHT_ELEMENT).build());
@@ -1,8 +1,5 @@
package com.gtnewhorizons.angelica.compat.mojang;
import com.gtnewhorizons.angelica.mixins.interfaces.ITessellatorInstance;
import net.minecraft.client.renderer.Tessellator;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
@@ -13,11 +10,17 @@ import java.nio.FloatBuffer;
public class VertexBuffer implements AutoCloseable {
private int id;
private int vertexCount;
private VertexFormat format;
public VertexBuffer() {
this.id = GL15.glGenBuffers();
}
public VertexBuffer(VertexFormat format) {
this();
this.format = format;
}
public void bind() {
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.id);
}
@@ -34,6 +37,12 @@ public class VertexBuffer implements AutoCloseable {
this.unbind();
}
public VertexBuffer upload(ByteBuffer buffer) {
if(format == null) throw new IllegalStateException("No format specified for VBO upload");
upload(buffer, buffer.capacity() / format.getVertexSize());
return this;
}
public void close() {
if (this.id >= 0) {
GL15.glDeleteBuffers(this.id);
@@ -52,4 +61,13 @@ public class VertexBuffer implements AutoCloseable {
public void draw(int mode) {
GL11.glDrawArrays(mode, 0, this.vertexCount);
}
public void render(int mode) {
if(format == null) throw new IllegalStateException("No format specified for VBO render");
bind();
format.setupBufferState(0L);
draw(mode);
format.clearBufferState();
unbind();
}
}
@@ -0,0 +1,44 @@
package com.gtnewhorizons.angelica.compat.nd.writers;
import com.gtnewhorizons.angelica.compat.nd.IWriteQuads;
import com.gtnewhorizons.angelica.compat.nd.Quad;
import java.nio.ByteBuffer;
public class PositionTextureQuadWriter implements IWriteQuads {
private boolean direct;
public PositionTextureQuadWriter() {
init(false);
}
public void init(boolean direct) {
// This would need to be re-initialized if the direct vs indirect changes
this.direct = direct;
}
@Override
public void writeQuad(Quad quad, ByteBuffer buf) {
if(direct) {
writeQuadDirect(quad, buf);
} else {
writeQuadIndirect(quad, buf);
}
}
protected void writeQuadDirect(Quad quad, ByteBuffer buf) {
throw new UnsupportedOperationException("Direct mode not supported yet");
}
protected void writeQuadIndirect(Quad quad, ByteBuffer buf) {
for(int idx = 0; idx < 4; ++idx) {
// Position
buf.putFloat(quad.getX(idx));
buf.putFloat(quad.getY(idx));
buf.putFloat(quad.getZ(idx));
// Texture
buf.putFloat(quad.getTexU(idx));
buf.putFloat(quad.getTexV(idx));
}
}
}
@@ -1,10 +1,13 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizons.angelica.client.renderer.CapturingTessellator;
import com.gtnewhorizons.angelica.compat.mojang.VertexBuffer;
import com.gtnewhorizons.angelica.compat.mojang.VertexFormat;
import com.gtnewhorizons.angelica.compat.nd.Quad;
import com.gtnewhorizons.angelica.mixins.interfaces.ITessellatorInstance;
import net.minecraft.client.renderer.Tessellator;
import java.nio.ByteBuffer;
import java.util.List;
@SuppressWarnings("unused")
@@ -36,11 +39,16 @@ public class TessellatorManager {
public static void startCapturing() {
if(currentlyCapturing.get()) throw new IllegalStateException("Tried to start capturing when already capturing!");
if(capturingTessellator.get().getQuads().size() > 0) throw new IllegalStateException("Tried to start capturing with existing collected Quads!");
currentlyCapturing.set(true);
}
public static List<Quad> stopCapturing() {
/*
* Stop the CapturingTessellator and return the pooled quads. The quads are valid until clearQuads() is
* called on the CapturingTesselator, which must be done before starting capturing again.
*/
public static List<Quad> stopCapturingToPooledQuads() {
if(!currentlyCapturing.get()) throw new IllegalStateException("Tried to stop capturing when not capturing!");
currentlyCapturing.set(false);
final CapturingTessellator tess = capturingTessellator.get();
@@ -54,8 +62,22 @@ public class TessellatorManager {
return quads;
}
public static void clearQuads() {
/*
* Stops the CapturingTessellator, stores the quads in a buffer (based on the VertexFormat provided),
* and clears the quads.
*/
public static ByteBuffer stopCapturingToBuffer(VertexFormat format) {
final ByteBuffer buf = CapturingTessellator.quadsToBuffer(stopCapturingToPooledQuads(), format);
capturingTessellator.get().clearQuads();
return buf;
}
/*
* Stops the CapturingTessellator, stores the quads in a buffer (based on the VertexFormat provided),
* uploads the buffer to a new VertexBuffer, and clears the quads.
*/
public static VertexBuffer stopCapturingToVBO(VertexFormat format) {
return new VertexBuffer(format).upload(stopCapturingToBuffer(format));
}
static {
@@ -64,11 +86,10 @@ public class TessellatorManager {
public static void cleanup() {
// Ensure we've cleaned everything up
if(currentlyCapturing.get()) {
currentlyCapturing.set(false);
final CapturingTessellator tess = capturingTessellator.get();
((ITessellatorInstance)tess).discard();
tess.clearQuads();
}
final CapturingTessellator tessellator = capturingTessellator.get();
currentlyCapturing.set(false);
((ITessellatorInstance)tessellator).discard();
tessellator.clearQuads();
}
}
@@ -27,10 +27,16 @@ public enum Mixins {
.setPhase(Phase.EARLY).addMixinClasses(
"angelica.MixinActiveRenderInfo"
,"angelica.MixinEntityRenderer"
,"angelica.vbo.MixinWavefrontObject"
)
),
ANGELICA_VBO(
new Builder("Angelica VBO").addTargetedMod(TargetedMod.VANILLA).setSide(Side.CLIENT)
.setPhase(Phase.EARLY).addMixinClasses(
"angelica.vbo.MixinWavefrontObject"
,"angelica.vbo.MixinRenderGlobal"
,"angelica.vbo.MixinModelRenderer"
)
),
ANGELICA_FONT_RENDERER(new Builder("Angelica Font Renderer").addTargetedMod(TargetedMod.VANILLA).setSide(Side.CLIENT)
.setPhase(Phase.EARLY).setApplyIf(() -> AngelicaConfig.enableFontRenderer).addMixinClasses(
"angelica.fontrenderer.MixinGuiIngameForge"
@@ -0,0 +1,47 @@
package com.gtnewhorizons.angelica.mixins.early.angelica.vbo;
import com.gtnewhorizons.angelica.compat.mojang.DefaultVertexFormat;
import com.gtnewhorizons.angelica.compat.mojang.VertexBuffer;
import com.gtnewhorizons.angelica.glsm.TessellatorManager;
import net.minecraft.client.model.ModelRenderer;
import org.lwjgl.opengl.GL11;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import java.util.ArrayList;
@Mixin(ModelRenderer.class)
public class MixinModelRenderer {
@Shadow
public int displayList;
private static int nextDisplayList = 0;
private static int getNextDisplayList() {
return nextDisplayList++;
}
private static ArrayList<VertexBuffer> vertexBuffers = new ArrayList<>();
@Redirect(method = "compileDisplayList", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/GLAllocation;generateDisplayLists(I)I"))
public int generateDisplayLists(int range) {
return getNextDisplayList();
}
@Redirect(method = "compileDisplayList", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glNewList(II)V", remap = false))
public void startCapturingVBO(int list, int mode) {
TessellatorManager.startCapturing();
}
@Redirect(method = "compileDisplayList", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glEndList()V", remap = false))
public void stopCapturingVBO() {
vertexBuffers.add(this.displayList, TessellatorManager.stopCapturingToVBO(DefaultVertexFormat.POSITION_TEXTURE_NORMAL));
}
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glCallList(I)V", remap = false))
public void renderVBO(int list) {
vertexBuffers.get(list).render(GL11.GL_QUADS);
}
@Redirect(method = "renderWithRotation", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glCallList(I)V", remap = false))
public void renderWithRotationVBO(int list) {
vertexBuffers.get(list).render(GL11.GL_QUADS);
}
}
@@ -0,0 +1,83 @@
package com.gtnewhorizons.angelica.mixins.early.angelica.vbo;
import com.gtnewhorizons.angelica.compat.mojang.DefaultVertexFormat;
import com.gtnewhorizons.angelica.compat.mojang.VertexBuffer;
import com.gtnewhorizons.angelica.glsm.TessellatorManager;
import com.gtnewhorizons.angelica.mixins.interfaces.IRenderGlobalVBOCapture;
import net.minecraft.client.renderer.RenderGlobal;
import net.minecraft.client.renderer.Tessellator;
import org.lwjgl.opengl.GL11;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(value = RenderGlobal.class, remap = false)
public class MixinRenderGlobal implements IRenderGlobalVBOCapture {
@Unique private VertexBuffer starVBO;
@Unique private VertexBuffer skyVBO;
@Unique private VertexBuffer sky2VBO;
private static final int MAGIC_NUMER = -1000;
private static final int STARS = MAGIC_NUMER;
private static final int SKY = MAGIC_NUMER + 1;
private static final int SKY2 = MAGIC_NUMER + 2;
@Redirect(method="<init>", at = @At(value="INVOKE", target="Lnet/minecraft/client/renderer/GLAllocation;generateDisplayLists(I)I"))
private int generateDisplayLists(int range) {
// Don't allocate the display lists, we'll be making VBOs instead. Return a magic number for identification below.
return MAGIC_NUMER;
}
@Override
@Redirect(method="<init>", at = @At(value="INVOKE", target="Lorg/lwjgl/opengl/GL11;glNewList(II)V", ordinal = 0))
public void startStarsVBO(int list, int mode) {
TessellatorManager.startCapturing();
}
@Override
@Redirect(method="<init>", at = @At(value="INVOKE", target="Lorg/lwjgl/opengl/GL11;glEndList()V", ordinal = 0))
public void finishStarsVBO() {
this.starVBO = TessellatorManager.stopCapturingToVBO(DefaultVertexFormat.POSITION);
}
@Redirect(method="<init>", at = @At(value="FIELD", target="Lnet/minecraft/client/renderer/Tessellator;instance:Lnet/minecraft/client/renderer/Tessellator;"))
private Tessellator redirectTessellator() {
TessellatorManager.startCapturing();
return TessellatorManager.get();
}
@Override
@Redirect(method="<init>", at = @At(value="INVOKE", target="Lorg/lwjgl/opengl/GL11;glNewList(II)V", ordinal = 1))
public void startSkyVBO(int list, int mode) {
// Do nothing, we'll be making a VBO instead.
}
@Override
@Redirect(method="<init>", at = @At(value="INVOKE", target="Lorg/lwjgl/opengl/GL11;glEndList()V", ordinal = 1))
public void finishSkyVBO() {
this.skyVBO = TessellatorManager.stopCapturingToVBO(DefaultVertexFormat.POSITION);
}
@Override
@Redirect(method="<init>", at = @At(value="INVOKE", target="Lorg/lwjgl/opengl/GL11;glNewList(II)V", ordinal = 2))
public void startSky2VBO(int list, int mode) {
TessellatorManager.startCapturing();
}
@Override
@Redirect(method="<init>", at = @At(value="INVOKE", target="Lorg/lwjgl/opengl/GL11;glEndList()V", ordinal = 2))
public void finishSky2VBO() {
this.sky2VBO = TessellatorManager.stopCapturingToVBO(DefaultVertexFormat.POSITION);
}
@Redirect(method="renderSky(F)V", at = @At(value="INVOKE", target="Lorg/lwjgl/opengl/GL11;glCallList(I)V"))
public void renderSky(int list) {
VertexBuffer vbo = switch (list) {
case STARS -> this.starVBO;
case SKY -> this.skyVBO;
case SKY2 -> this.sky2VBO;
default -> throw new RuntimeException("Unexpected display list: " + list);
};
vbo.render(GL11.GL_QUADS);
}
}
@@ -1,10 +1,9 @@
package com.gtnewhorizons.angelica.mixins.early.angelica.vbo;
import com.gtnewhorizons.angelica.client.renderer.CapturingTessellator;
import com.gtnewhorizons.angelica.compat.mojang.DefaultVertexFormat;
import com.gtnewhorizons.angelica.compat.mojang.VertexBuffer;
import com.gtnewhorizons.angelica.compat.mojang.VertexFormat;
import com.gtnewhorizons.angelica.compat.mojang.DefaultVertexFormat;
import com.gtnewhorizons.angelica.compat.nd.Quad;
import com.gtnewhorizons.angelica.glsm.TessellatorManager;
import com.gtnewhorizons.angelica.mixins.interfaces.IModelCustomExt;
import net.minecraft.client.renderer.Tessellator;
@@ -15,9 +14,6 @@ import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import java.nio.ByteBuffer;
import java.util.List;
@Mixin(value = WavefrontObject.class, remap = false)
public abstract class MixinWavefrontObject implements IModelCustomExt {
@Unique private VertexBuffer vertexBuffer;
@@ -26,7 +22,7 @@ public abstract class MixinWavefrontObject implements IModelCustomExt {
@Shadow public abstract void tessellateAll(Tessellator tessellator);
@Unique VertexFormat format = DefaultVertexFormat.ITEM_VBO;
@Unique VertexFormat format = DefaultVertexFormat.POSITION_TEXTURE_NORMAL;
@Override
public void rebuildVBO() {
@@ -40,13 +36,8 @@ public abstract class MixinWavefrontObject implements IModelCustomExt {
final CapturingTessellator tess = (CapturingTessellator) TessellatorManager.get();
tess.startDrawing(currentGroupObject.glDrawingMode);
tessellateAll(tess);
List<Quad> quads = TessellatorManager.stopCapturing();
final ByteBuffer byteBuffer = CapturingTessellator.quadsToBuffer(quads, format);
this.vertexBuffer = new VertexBuffer();
this.vertexBuffer.bind();
vertexBuffer.upload(byteBuffer, quads.size() * 4);
this.vertexBuffer.unbind();
this.vertexBuffer = TessellatorManager.stopCapturingToVBO(format);
}
@Override
@@ -54,11 +45,7 @@ public abstract class MixinWavefrontObject implements IModelCustomExt {
if(vertexBuffer == null) {
rebuildVBO();
}
vertexBuffer.bind();
format.setupBufferState(0L);
vertexBuffer.draw(GL11.GL_QUADS);
format.clearBufferState();
vertexBuffer.unbind();
vertexBuffer.render(GL11.GL_QUADS);
}
}
@@ -6,6 +6,6 @@ import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(BiomeEvent.BiomeColor.class)
public interface AccessorBiomeColorEvent {
@Accessor("originalColor")
@Accessor(value = "originalColor", remap = false)
void setOriginalColor(int value);
}
@@ -0,0 +1,14 @@
package com.gtnewhorizons.angelica.mixins.interfaces;
public interface IRenderGlobalVBOCapture {
void startStarsVBO(int list, int mode);
void finishStarsVBO();
void startSkyVBO(int list, int mode);
void finishSkyVBO();
void startSky2VBO(int list, int mode);
void finishSky2VBO();
}