SDL GPU backend, TESR Batching/caching API, Tracy Integration (#1993)

This commit is contained in:
Jason Mitchell authored and GitHub committed 2026-08-08 15:53:58 -07:00
1 parent 413df76ad4
commit 8da561d3b6
647 files changed
+60152 -5331

No files matched your search

@@ -0,0 +1,335 @@
package net.coderbot.batchedentityrendering.impl;
import com.gtnewhorizons.angelica.compat.mojang.RenderLayer;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.coderbot.batchedentityrendering.impl.ordering.SimpleRenderOrderManager;
import net.coderbot.iris.Iris;
import net.coderbot.iris.pipeline.WorldRenderingPipeline;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
/** Collects geometry into per-RenderLayer buffers during a pass and draws it all at endBatch, one state bracket per layer. Based on Iris's FullyBufferedMultiBufferSource */
public class AngelicaBufferSource implements Groupable {
public static final int SAVED_STATE_BITS = GL11.GL_ENABLE_BIT | GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_CURRENT_BIT | GL11.GL_LIGHTING_BIT | GL11.GL_TEXTURE_BIT;
private static final int NUM_BUFFERS = 32;
public enum GroupIdKind { BLOCK_ENTITY, ENTITY }
public interface LayerDrawHook {
boolean hasDraws(RenderLayer layer);
void drawLayer(RenderLayer layer);
}
private static final Comparator<BufferSegment> OPAQUE_ORDER = Comparator.comparingInt(BufferSegment::getBlockEntityId);
private final SimpleRenderOrderManager renderOrderManager = new SimpleRenderOrderManager();
private final SegmentedBufferBuilder[] builders = new SegmentedBufferBuilder[NUM_BUFFERS];
private final Object2IntLinkedOpenHashMap<RenderLayer> affinities = new Object2IntLinkedOpenHashMap<>(NUM_BUFFERS);
private final Map<RenderLayer, List<BufferSegment>> typeToSegment = new Object2ObjectOpenHashMap<>();
private boolean prepared;
private final List<RenderLayer> order = new ArrayList<>();
private boolean stateSaved;
private boolean anyIdSet;
private GroupIdKind idKind = GroupIdKind.BLOCK_ENTITY;
public AngelicaBufferSource() {
for (int i = 0; i < builders.length; i++) {
builders[i] = new SegmentedBufferBuilder();
}
affinities.defaultReturnValue(-1);
}
public SegmentedBufferBuilder getBuffer(RenderLayer type, int blockEntityId) {
renderOrderManager.begin(type);
int affinity = affinities.getAndMoveToLast(type);
if (affinity == -1) {
if (affinities.size() < builders.length) {
affinity = affinities.size();
} else {
affinity = affinities.removeFirstInt();
}
affinities.put(type, affinity);
}
final SegmentedBufferBuilder builder = builders[affinity];
builder.begin(type, blockEntityId);
return builder;
}
public void declareUse(RenderLayer type) {
renderOrderManager.begin(type);
}
public boolean isEmpty() {
for (SegmentedBufferBuilder builder : builders) {
if (!builder.isEmpty()) return false;
}
return true;
}
private void ensurePrepared() {
if (prepared) return;
prepared = true;
clearSegmentLists();
for (SegmentedBufferBuilder builder : builders) {
final List<BufferSegment> builderSegments = builder.getSegments();
for (int i = 0, n = builderSegments.size(); i < n; i++) {
final BufferSegment segment = builderSegments.get(i);
typeToSegment.computeIfAbsent(segment.getRenderType(), t -> new ArrayList<>()).add(segment);
}
}
order.clear();
final List<RenderLayer> renderOrder = renderOrderManager.getRenderOrder();
for (int i = 0, n = renderOrder.size(); i < n; i++) {
order.add(renderOrder.get(i));
}
renderOrderManager.reset();
for (int i = 0, n = order.size(); i < n; i++) {
final RenderLayer layer = order.get(i);
if (getTransparencyType(layer) != TransparencyType.OPAQUE) continue;
final List<BufferSegment> segments = typeToSegment.get(layer);
if (segments != null) {
segments.sort(OPAQUE_ORDER);
}
}
affinities.clear();
stateSaved = false;
anyIdSet = false;
}
List<RenderLayer> prepare() {
ensurePrepared();
return new ArrayList<>(order);
}
List<BufferSegment> segmentsFor(RenderLayer layer) {
return typeToSegment.get(layer);
}
public void endBatchWithType(TransparencyType type, LayerDrawHook hook) {
ensurePrepared();
int keep = 0;
for (int i = 0, n = order.size(); i < n; i++) {
final RenderLayer layer = order.get(i);
if (getTransparencyType(layer) != type) {
order.set(keep++, layer);
continue;
}
drawLayer(layer, hook);
}
for (int i = order.size() - 1; i >= keep; i--) {
order.remove(i);
}
}
public void endBatch(LayerDrawHook hook) {
ensurePrepared();
for (int i = 0, n = order.size(); i < n; i++) {
drawLayer(order.get(i), hook);
}
order.clear();
finish();
}
public void pauseBatch() {
releaseAttribState();
clearCurrentId();
}
public void discard() {
order.clear();
finish();
}
private void releaseAttribState() {
if (stateSaved) {
GLStateManager.glPopAttrib();
stateSaved = false;
}
}
private void clearCurrentId() {
if (!anyIdSet) return;
if (idKind == GroupIdKind.ENTITY) {
CapturedRenderingState.INSTANCE.setCurrentEntity(-1);
CapturedRenderingState.INSTANCE.setCurrentEntityColor(0f, 0f, 0f, 0f);
} else {
CapturedRenderingState.INSTANCE.setCurrentBlockEntity(0);
}
anyIdSet = false;
}
private void drawLayer(RenderLayer layer, LayerDrawHook hook) {
final List<BufferSegment> segments = typeToSegment.get(layer);
final boolean hasDynamic = segments != null && !segments.isEmpty();
final boolean hasRetained = hook != null && hook.hasDraws(layer);
if (!hasDynamic && !hasRetained) return;
if (!stateSaved) {
stateSaved = true;
GLStateManager.glPushAttrib(SAVED_STATE_BITS);
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
}
layer.startDrawing();
if (hasDynamic) {
anyIdSet = true;
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
SegmentedBufferBuilder.LayerBuffer bound = null;
final boolean entityKind = idKind == GroupIdKind.ENTITY;
int currentId = Integer.MIN_VALUE;
int currentColor = 0;
boolean colorSet = false;
for (int i = 0, n = segments.size(); i < n; i++) {
final BufferSegment segment = segments.get(i);
if (entityKind && (!colorSet || segment.getEntityColor() != currentColor)) {
colorSet = true;
currentColor = segment.getEntityColor();
setEntityColor(currentColor);
}
if (segment.getBlockEntityId() != currentId) {
currentId = segment.getBlockEntityId();
applyIdAndRebind(currentId);
}
final SegmentedBufferBuilder.LayerBuffer owner = segment.getOwner();
if (owner != bound) {
if (bound != null) bound.finishDraw();
bound = owner;
bound.uploadForDraw();
bound.setupDraw();
}
bound.draw(segment.getFirstVertex(), segment.getVertexCount());
}
if (bound != null) bound.finishDraw();
GLStateManager.glPopMatrix();
}
if (hasRetained) {
anyIdSet = true;
hook.drawLayer(layer);
}
layer.endDrawing();
}
private void clearSegmentLists() {
for (List<BufferSegment> segments : typeToSegment.values()) {
segments.clear();
}
}
private void finish() {
releaseAttribState();
clearCurrentId();
clearSegmentLists();
final long now = System.currentTimeMillis();
final long maxIdle = getTargetClearTime();
for (SegmentedBufferBuilder builder : builders) {
builder.resetAndReclaim(now, maxIdle);
}
prepared = false;
}
public void freeBuffers() {
for (SegmentedBufferBuilder builder : builders) {
builder.freeAll();
}
typeToSegment.clear();
order.clear();
affinities.clear();
renderOrderManager.reset();
prepared = false;
}
public long allocatedBytes() {
long bytes = 0;
for (SegmentedBufferBuilder builder : builders) {
bytes += builder.allocatedBytes();
}
return bytes;
}
private long getTargetClearTime() {
final long sizeInMiB = allocatedBytes() / 1024L / 1024L;
if (sizeInMiB > 5000) {
return 1_000;
} else if (sizeInMiB > 1000) {
return 5_000;
}
return 10_000;
}
public void setIdKind(GroupIdKind kind) {
this.idKind = kind;
}
public void applyIdAndRebind(int id) {
anyIdSet = true;
if (idKind == GroupIdKind.ENTITY) {
CapturedRenderingState.INSTANCE.setCurrentEntity(id);
rebindPass();
} else {
setBlockEntityAndRebind(id);
}
}
public void applyIdNoRebind(int id) {
anyIdSet = true;
if (idKind == GroupIdKind.ENTITY) {
CapturedRenderingState.INSTANCE.setCurrentEntity(id);
} else {
CapturedRenderingState.INSTANCE.setCurrentBlockEntity(id);
}
}
public static void setEntityColor(int packed) {
CapturedRenderingState.INSTANCE.setCurrentEntityColor(
((packed >>> 16) & 0xFF) / 255f, ((packed >>> 8) & 0xFF) / 255f,
(packed & 0xFF) / 255f, (packed >>> 24) / 255f);
}
public static void setBlockEntityAndRebind(int blockEntityId) {
CapturedRenderingState.INSTANCE.setCurrentBlockEntity(blockEntityId);
rebindPass();
}
public static void rebindPass() {
if (Iris.enabled) {
final WorldRenderingPipeline pipeline = Iris.getPipelineManager().getPipelineNullable();
if (pipeline != null) {
pipeline.rebindCurrentPass();
}
}
}
static TransparencyType getTransparencyType(RenderLayer type) {
while (type instanceof WrappableRenderType wrappable) {
type = wrappable.unwrap();
}
if (type instanceof BlendingStateHolder holder) {
return holder.getTransparencyType();
}
return TransparencyType.GENERAL_TRANSPARENT;
}
@Override
public void startGroup() {
renderOrderManager.startGroup();
}
@Override
public boolean maybeStartGroup() {
return renderOrderManager.maybeStartGroup();
}
@Override
public void endGroup() {
renderOrderManager.endGroup();
}
}
@@ -0,0 +1,43 @@
package net.coderbot.batchedentityrendering.impl;
import com.gtnewhorizon.gtnhlib.client.renderer.vertex.DefaultVertexFormat;
import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat;
import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement;
import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFlags;
import com.gtnewhorizon.gtnhlib.client.renderer.vertex.writers.IVertexAttributeWriter;
import net.minecraft.client.renderer.Tessellator;
import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memGetFloat;
import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutFloat;
import static com.gtnewhorizon.gtnhlib.client.renderer.cel.util.ModelQuadUtil.LIGHT_INDEX;
public final class BatchVertexFormats {
public static final VertexFormatElement LIGHT_FLOAT_ELEMENT = new VertexFormatElement(1, VertexFormatElement.Type.FLOAT, VertexFormatElement.Usage.SECONDARY_UV, 2, VertexFlags.BRIGHTNESS_BIT, new FloatLightVertexAttributeWriter());
public static final VertexFormat POSITION_COLOR_TEXTURE_LIGHTF_NORMAL = new VertexFormat(DefaultVertexFormat.POSITION_ELEMENT, DefaultVertexFormat.COLOR_ELEMENT, DefaultVertexFormat.TEXTURE_ELEMENT, LIGHT_FLOAT_ELEMENT, DefaultVertexFormat.NORMAL_ELEMENT);
private BatchVertexFormats() {}
private static final class FloatLightVertexAttributeWriter implements IVertexAttributeWriter {
@Override
public int writeAttribute(long pointer, int[] data, int index) {
final int light = data[index | LIGHT_INDEX];
memPutFloat(pointer, light & 0xFFFF);
memPutFloat(pointer + 4, light >>> 16);
return 8;
}
@Override
public int writeAttribute(long pointer, Tessellator tessellator) {
final int light = tessellator.brightness;
memPutFloat(pointer, light & 0xFFFF);
memPutFloat(pointer + 4, light >>> 16);
return 8;
}
@Override
public int readAttribute(long pointer, Tessellator tessellator) {
tessellator.brightness = (((int) memGetFloat(pointer + 4)) << 16) | (((int) memGetFloat(pointer)) & 0xFFFF);
return 8;
}
}
}
@@ -1,25 +1,45 @@
package net.coderbot.batchedentityrendering.impl;
import com.gtnewhorizons.angelica.compat.toremove.RenderLayer;
import lombok.Getter;
import java.nio.ByteBuffer;
import com.gtnewhorizons.angelica.compat.mojang.RenderLayer;
public class BufferSegment {
private final ByteBuffer slice;
@Getter
private final RenderLayer type;
private final int blockEntityId;
private final int entityColor;
private final int firstVertex;
private final int vertexCount;
private final SegmentedBufferBuilder.LayerBuffer owner;
public BufferSegment(ByteBuffer slice, RenderLayer type) {
this.slice = slice;
public BufferSegment(RenderLayer type, int blockEntityId, int entityColor, int firstVertex, int vertexCount, SegmentedBufferBuilder.LayerBuffer owner) {
this.type = type;
}
public ByteBuffer getSlice() {
return slice;
this.blockEntityId = blockEntityId;
this.entityColor = entityColor;
this.firstVertex = firstVertex;
this.vertexCount = vertexCount;
this.owner = owner;
}
public RenderLayer getRenderType() {
return type;
}
public int getBlockEntityId() {
return blockEntityId;
}
public int getEntityColor() {
return entityColor;
}
public int getFirstVertex() {
return firstVertex;
}
public int getVertexCount() {
return vertexCount;
}
public SegmentedBufferBuilder.LayerBuffer getOwner() {
return owner;
}
}
@@ -0,0 +1,217 @@
package net.coderbot.batchedentityrendering.impl;
import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.ModelQuadView;
import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat;
import com.gtnewhorizons.angelica.compat.mojang.RenderLayer;
import com.gtnewhorizons.angelica.rendering.tesr.TemplateBuffer;
import com.gtnewhorizons.angelica.rendering.tesr.MeshBuffer;
import com.gtnewhorizons.angelica.rendering.tesr.VertexTransform;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import org.joml.Matrix4fc;
import org.joml.Vector3f;
import org.joml.Vector4fc;
import org.lwjgl.BufferUtils;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAddress0;
public class SegmentedBufferBuilder {
public static final VertexFormat FORMAT = BatchVertexFormats.POSITION_COLOR_TEXTURE_LIGHTF_NORMAL;
static final class LayerBuffer {
final RenderLayer layer;
ByteBuffer buffer;
final MeshBuffer mesh = new MeshBuffer();
int vertexCount;
boolean uploaded;
long lastUsedMs;
LayerBuffer(RenderLayer layer) {
this.layer = layer;
this.buffer = BufferUtils.createByteBuffer(Math.max(layer.getExpectedBufferSize(), 4096));
}
void ensureCapacity(int bytes) {
buffer = MeshBuffer.ensureCapacity(buffer, bytes, true);
}
void uploadForDraw() {
if (uploaded) return;
uploaded = true;
final ByteBuffer view = buffer.duplicate();
view.flip();
mesh.upload(layer.getVertexFormat(), layer.getDrawMode(), view, vertexCount);
}
void setupDraw() {
mesh.bind();
}
void draw(int firstVertex, int count) {
mesh.draw(firstVertex, count);
}
void finishDraw() {
mesh.unbind();
}
void reset() {
buffer.clear();
vertexCount = 0;
uploaded = false;
}
}
private final Map<RenderLayer, LayerBuffer> layerBuffers = new Object2ObjectOpenHashMap<>();
private final List<LayerBuffer> bufferList = new ArrayList<>();
private final List<BufferSegment> segments = new ArrayList<>();
private LayerBuffer current;
private RenderLayer currentType;
private int currentBlockEntityId;
private int currentEntityColor;
private int segmentStartVertex;
private long allocatedBytes;
public void begin(RenderLayer type, int blockEntityId) {
final int entityColor = packEntityColor(CapturedRenderingState.INSTANCE.getCurrentEntityColor());
if (type == currentType && blockEntityId == currentBlockEntityId && entityColor == currentEntityColor && current != null) {
return;
}
endSegment();
LayerBuffer buffer = layerBuffers.get(type);
if (buffer == null) {
buffer = new LayerBuffer(type);
layerBuffers.put(type, buffer);
bufferList.add(buffer);
allocatedBytes += buffer.buffer.capacity();
}
current = buffer;
currentType = type;
currentBlockEntityId = blockEntityId;
currentEntityColor = entityColor;
segmentStartVertex = current.vertexCount;
}
public static int packEntityColor(Vector4fc c) {
return ((int) (c.w() * 255f + 0.5f) << 24) | ((int) (c.x() * 255f + 0.5f) << 16) | ((int) (c.y() * 255f + 0.5f) << 8) | (int) (c.z() * 255f + 0.5f);
}
public void addQuad(ModelQuadView quad) {
if (current == null) {
throw new IllegalStateException("addQuad() without begin()");
}
final VertexFormat format = currentType.getVertexFormat();
ensureCurrentCapacity(format.getVertexSize() * 4);
format.writeQuad(quad, current.buffer);
current.vertexCount += 4;
}
public void addTemplateInstance(TemplateBuffer template, Matrix4fc mv, Vector3f scratch, int colorABGR, int packedLight, Matrix4fc texMatrix) {
if (current == null) {
throw new IllegalStateException("addTemplateInstance() without begin()");
}
final VertexFormat format = currentType.getVertexFormat();
ensureCurrentCapacity(format.getVertexSize() * template.vertexCount);
final ByteBuffer buffer = current.buffer;
final long ptr = memAddress0(buffer) + buffer.position();
final long end = VertexTransform.writeInstance(ptr, format, template, mv, scratch, colorABGR, packedLight, texMatrix);
buffer.position(buffer.position() + (int) (end - ptr));
current.vertexCount += template.vertexCount;
}
private void ensureCurrentCapacity(int bytes) {
final int before = current.buffer.capacity();
current.ensureCapacity(bytes);
allocatedBytes += current.buffer.capacity() - before;
}
private void endSegment() {
if (current != null && current.vertexCount > segmentStartVertex) {
segments.add(new BufferSegment(currentType, currentBlockEntityId, currentEntityColor, segmentStartVertex,
current.vertexCount - segmentStartVertex, current));
}
segmentStartVertex = current == null ? 0 : current.vertexCount;
}
public List<BufferSegment> getSegments() {
endSegment();
currentType = null;
current = null;
return segments;
}
public boolean isEmpty() {
for (int i = 0, n = bufferList.size(); i < n; i++) {
if (bufferList.get(i).vertexCount != 0) return false;
}
return true;
}
public int getVertexCount() {
int total = 0;
for (int i = 0, n = bufferList.size(); i < n; i++) {
total += bufferList.get(i).vertexCount;
}
return total;
}
public void reset() {
for (int i = 0, n = bufferList.size(); i < n; i++) {
bufferList.get(i).reset();
}
segments.clear();
currentType = null;
current = null;
currentBlockEntityId = 0;
segmentStartVertex = 0;
}
public void resetAndReclaim(long nowMs, long maxIdleMs) {
for (int i = bufferList.size() - 1; i >= 0; i--) {
final LayerBuffer lb = bufferList.get(i);
if (lb.vertexCount > 0) {
lb.lastUsedMs = nowMs;
}
lb.reset();
if (nowMs - lb.lastUsedMs > maxIdleMs) {
allocatedBytes -= lb.buffer.capacity();
lb.mesh.delete();
layerBuffers.remove(lb.layer);
bufferList.remove(i);
}
}
segments.clear();
currentType = null;
current = null;
currentBlockEntityId = 0;
segmentStartVertex = 0;
}
public void freeAll() {
for (int i = 0, n = bufferList.size(); i < n; i++) {
bufferList.get(i).mesh.delete();
}
layerBuffers.clear();
bufferList.clear();
segments.clear();
currentType = null;
current = null;
currentBlockEntityId = 0;
segmentStartVertex = 0;
allocatedBytes = 0;
}
public long allocatedBytes() {
return allocatedBytes;
}
public int bufferCount() {
return layerBuffers.size();
}
}
@@ -1,6 +1,6 @@
package net.coderbot.batchedentityrendering.impl;
import com.gtnewhorizons.angelica.compat.toremove.RenderLayer;
import com.gtnewhorizons.angelica.compat.mojang.RenderLayer;
public interface WrappableRenderType {
/**
@@ -10,7 +10,7 @@ import de.odysseus.ithaka.digraph.util.fas.SimpleFeedbackArcSetProvider;
import net.coderbot.batchedentityrendering.impl.BlendingStateHolder;
import net.coderbot.batchedentityrendering.impl.TransparencyType;
import net.coderbot.batchedentityrendering.impl.WrappableRenderType;
import com.gtnewhorizons.angelica.compat.toremove.RenderLayer;
import com.gtnewhorizons.angelica.compat.mojang.RenderLayer;
import java.util.ArrayList;
import java.util.EnumMap;
@@ -1,6 +1,6 @@
package net.coderbot.batchedentityrendering.impl.ordering;
import com.gtnewhorizons.angelica.compat.toremove.RenderLayer;
import com.gtnewhorizons.angelica.compat.mojang.RenderLayer;
public interface RenderOrderManager {
void begin(RenderLayer type);
@@ -1,18 +1,19 @@
package net.coderbot.batchedentityrendering.impl.ordering;
import com.gtnewhorizons.angelica.compat.toremove.RenderLayer;
import com.gtnewhorizons.angelica.compat.mojang.RenderLayer;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import java.util.LinkedHashSet;
import java.util.ArrayList;
import java.util.List;
public class SimpleRenderOrderManager implements RenderOrderManager {
private final LinkedHashSet<RenderLayer> renderTypes;
public SimpleRenderOrderManager() {
renderTypes = new LinkedHashSet<>();
}
private final ObjectOpenHashSet<RenderLayer> seen = new ObjectOpenHashSet<>();
private final ArrayList<RenderLayer> ordered = new ArrayList<>();
public void begin(RenderLayer type) {
renderTypes.add(type);
if (seen.add(type)) {
ordered.add(type);
}
}
public void startGroup() {
@@ -30,10 +31,11 @@ public class SimpleRenderOrderManager implements RenderOrderManager {
@Override
public void reset() {
renderTypes.clear();
seen.clear();
ordered.clear();
}
public Iterable<RenderLayer> getRenderOrder() {
return renderTypes;
public List<RenderLayer> getRenderOrder() {
return ordered;
}
}
@@ -3,7 +3,7 @@ package net.coderbot.batchedentityrendering.impl.ordering;
import net.coderbot.batchedentityrendering.impl.BlendingStateHolder;
import net.coderbot.batchedentityrendering.impl.TransparencyType;
import net.coderbot.batchedentityrendering.impl.WrappableRenderType;
import com.gtnewhorizons.angelica.compat.toremove.RenderLayer;
import com.gtnewhorizons.angelica.compat.mojang.RenderLayer;
import java.util.ArrayList;
import java.util.EnumMap;