Remove tessellator to buffer (#123)

* Update buildscript
* Swap out uploading a tesselator and tesselatorToBuffer with a Capturing Tesselator
This commit is contained in:
Jason Mitchell authored and GitHub committed 2024-01-03 13:22:52 -08:00
1 parent 0460f6dbcd
commit c19c812c10
7 files changed
+88 -79

No files matched your search

@@ -1,11 +1,10 @@
package com.gtnewhorizons.angelica.compat.mojang;
import com.google.common.collect.ImmutableList;
import com.gtnewhorizons.angelica.compat.mojang.VertexFormat;
import com.gtnewhorizons.angelica.compat.mojang.VertexFormatElement;
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;
public class DefaultVertexFormat {
@@ -22,7 +21,7 @@ public class DefaultVertexFormat {
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());
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());
@@ -26,47 +26,6 @@ public class VertexBuffer implements AutoCloseable {
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
public void upload(Tessellator tessellator, VertexFormat format) {
try {
this.upload(tessellatorToBuffer(tessellator, format), tessellator.vertexCount);
} catch(Exception e) {
throw new RuntimeException("Failed to upload vertex buffer", e);
} finally {
((ITessellatorInstance) tessellator).discard();
}
}
public ByteBuffer tessellatorToBuffer(Tessellator tessellator, VertexFormat format) {
final int verticesPerPrimitive = tessellator.drawMode == GL11.GL_QUADS ? 4 : 3;
final int[] rawBuffer = tessellator.rawBuffer;
final int byteSize = format.getVertexSize() * tessellator.vertexCount;
final ByteBuffer byteBuffer = BufferUtils.createByteBuffer(byteSize);
for(int quadI = 0 ; quadI < tessellator.vertexCount / verticesPerPrimitive ; quadI++) {
for(int vertexI = 0 ; vertexI < 4 ; vertexI++) {
final int i = (quadI * 4 * 8 ) + (vertexI * 8);
// Position
byteBuffer.putFloat(Float.intBitsToFloat(rawBuffer[i + 0]));
byteBuffer.putFloat(Float.intBitsToFloat(rawBuffer[i + 1]));
byteBuffer.putFloat(Float.intBitsToFloat(rawBuffer[i + 2]));
// Texture
byteBuffer.putFloat(Float.intBitsToFloat(rawBuffer[i + 3]));
byteBuffer.putFloat(Float.intBitsToFloat(rawBuffer[i + 4]));
// Normals
byteBuffer.putInt(rawBuffer[i + 6]);
}
}
return (ByteBuffer) byteBuffer.rewind();
}
public void upload(ByteBuffer buffer, int vertexCount) {
if (this.id == -1) return;
this.vertexCount = vertexCount;
@@ -0,0 +1,40 @@
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 PositionQuadWriter implements IWriteQuads {
private boolean direct;
public PositionQuadWriter() {
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));
}
}
}
@@ -1,8 +1,11 @@
package com.gtnewhorizons.angelica.mixins.early.angelica.vbo;
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.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;
import net.minecraftforge.client.model.obj.GroupObject;
@@ -12,6 +15,9 @@ 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;
@@ -30,13 +36,16 @@ public abstract class MixinWavefrontObject implements IModelCustomExt {
if (this.vertexBuffer != null) {
this.vertexBuffer.close();
}
final Tessellator tessellator = Tessellator.instance;
tessellator.startDrawing(currentGroupObject.glDrawingMode);
tessellateAll(tessellator);
TessellatorManager.startCapturing();
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();
this.vertexBuffer.upload(tessellator, format);
vertexBuffer.upload(byteBuffer, quads.size() * 4);
this.vertexBuffer.unbind();
}
@@ -46,6 +46,9 @@ public class IrisRenderSystem {
Iris.logger.info("ARB_direct_state_access detected, enabling DSA.");
}
} catch (NoSuchFieldError ignored) {}
if (dsaState == null) {
dsaState = new DSAUnsupported();
}
try {
hasMultibind |= Iris.capabilities.GL_ARB_multi_bind;
@@ -1,12 +1,17 @@
package net.coderbot.iris.pipeline;
import com.gtnewhorizons.angelica.client.renderer.CapturingTessellator;
import com.gtnewhorizons.angelica.compat.mojang.VertexBuffer;
import com.gtnewhorizons.angelica.compat.mojang.DefaultVertexFormat;
import com.gtnewhorizons.angelica.compat.nd.Quad;
import com.gtnewhorizons.angelica.glsm.TessellatorManager;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Tessellator;
import org.lwjgl.opengl.GL11;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.List;
/**
* Renders the sky horizon. Vanilla Minecraft simply uses the "clear color" for its horizon, and then draws a plane
@@ -52,15 +57,18 @@ public class HorizonRenderer {
if (this.vertexBuffer != null) {
this.vertexBuffer.close();
}
final Tessellator tessellator = Tessellator.instance;
TessellatorManager.startCapturing();
final CapturingTessellator tessellator = (CapturingTessellator) TessellatorManager.get();
// Build the horizon quads into a buffer
tessellator.startDrawingQuads(); //(GL11.GL_QUADS, DefaultVertexFormat.POSITION);
buildHorizon(currentRenderDistance * 16, tessellator);
List<Quad> quads = TessellatorManager.stopCapturing();
final ByteBuffer byteBuffer = CapturingTessellator.quadsToBuffer(quads, DefaultVertexFormat.POSITION);
this.vertexBuffer = new VertexBuffer();
this.vertexBuffer.bind();
this.vertexBuffer.upload(tessellator, DefaultVertexFormat.POSITION);
this.vertexBuffer.upload(byteBuffer, quads.size() * 4);
this.vertexBuffer.unbind();
}