2021-12-15 09:35:48 +01:00
|
|
|
package com.jozufozu.flywheel.mixin;
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
2021-12-22 07:47:29 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2021-12-15 09:35:48 +01:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
import org.spongepowered.asm.mixin.Mixin;
|
|
|
|
import org.spongepowered.asm.mixin.Shadow;
|
|
|
|
|
|
|
|
import com.jozufozu.flywheel.backend.model.DirectVertexConsumer;
|
|
|
|
import com.jozufozu.flywheel.backend.model.DirectBufferBuilder;
|
|
|
|
import com.mojang.blaze3d.vertex.BufferBuilder;
|
|
|
|
import com.mojang.blaze3d.vertex.VertexFormat;
|
|
|
|
import com.mojang.blaze3d.vertex.VertexFormatElement;
|
|
|
|
|
|
|
|
@Mixin(BufferBuilder.class)
|
|
|
|
public abstract class BufferBuilderMixin implements DirectBufferBuilder {
|
|
|
|
@Shadow
|
|
|
|
private ByteBuffer buffer;
|
|
|
|
|
|
|
|
@Shadow
|
|
|
|
private VertexFormat format;
|
|
|
|
|
|
|
|
@Shadow
|
|
|
|
protected abstract void ensureCapacity(int p_85723_);
|
|
|
|
|
|
|
|
@Shadow
|
|
|
|
private int vertices;
|
|
|
|
|
|
|
|
@Shadow
|
|
|
|
@Nullable
|
|
|
|
private VertexFormatElement currentElement;
|
|
|
|
|
|
|
|
@Shadow
|
|
|
|
private int elementIndex;
|
|
|
|
|
|
|
|
@Shadow
|
|
|
|
private int nextElementByte;
|
|
|
|
|
|
|
|
@Override
|
2021-12-22 07:47:29 +01:00
|
|
|
@Nonnull
|
2021-12-16 19:49:26 +01:00
|
|
|
public DirectVertexConsumer intoDirectConsumer(int vertexCount) {
|
|
|
|
int bytes = vertexCount * format.getVertexSize();
|
2021-12-22 07:47:29 +01:00
|
|
|
// ensure we have capacity for one extra vertex, BufferBuilder does this on #endVertex
|
|
|
|
ensureCapacity(bytes + format.getVertexSize());
|
2021-12-15 09:35:48 +01:00
|
|
|
|
2021-12-17 08:10:24 +01:00
|
|
|
DirectVertexConsumer consumer = new DirectVertexConsumer(this.buffer, this.format, vertexCount);
|
2021-12-15 09:35:48 +01:00
|
|
|
|
|
|
|
this.vertices += vertexCount;
|
|
|
|
this.currentElement = format.getElements()
|
|
|
|
.get(0);
|
|
|
|
this.elementIndex = 0;
|
2021-12-16 19:49:26 +01:00
|
|
|
this.nextElementByte += bytes;
|
2021-12-22 07:47:29 +01:00
|
|
|
this.buffer.position(this.buffer.position() + bytes);
|
2021-12-16 19:49:26 +01:00
|
|
|
|
|
|
|
return consumer;
|
2021-12-15 09:35:48 +01:00
|
|
|
}
|
|
|
|
}
|