minecraft-source/src/com/mojang/blaze3d/vertex/VertexBuffer.java

72 lines
2.2 KiB
Java
Raw Normal View History

2020-07-22 06:23:34 +01:00
package com.mojang.blaze3d.vertex;
2020-07-22 06:25:47 +01:00
import com.mojang.math.Matrix4f;
import com.mojang.datafixers.util.Pair;
2020-07-22 06:23:34 +01:00
import java.nio.ByteBuffer;
2020-07-22 06:25:47 +01:00
import java.util.concurrent.CompletableFuture;
import com.mojang.blaze3d.systems.RenderSystem;
2020-07-22 06:23:34 +01:00
2020-07-22 06:25:47 +01:00
public class VertexBuffer implements AutoCloseable {
2020-07-22 06:23:34 +01:00
private int id;
private final VertexFormat format;
private int vertexCount;
2020-07-22 06:32:50 +01:00
public VertexBuffer(final VertexFormat dhq) {
this.format = dhq;
2020-07-22 06:25:47 +01:00
RenderSystem.glGenBuffers(integer -> this.id = integer);
2020-07-22 06:23:34 +01:00
}
public void bind() {
2020-07-22 06:25:47 +01:00
RenderSystem.glBindBuffer(34962, () -> this.id);
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:32:50 +01:00
public void upload(final BufferBuilder dhg) {
2020-07-22 06:25:47 +01:00
if (!RenderSystem.isOnRenderThread()) {
2020-07-22 06:32:50 +01:00
RenderSystem.recordRenderCall(() -> this.upload_(dhg));
2020-07-22 06:25:47 +01:00
}
else {
2020-07-22 06:32:50 +01:00
this.upload_(dhg);
2020-07-22 06:25:47 +01:00
}
}
2020-07-22 06:32:50 +01:00
public CompletableFuture<Void> uploadLater(final BufferBuilder dhg) {
2020-07-22 06:25:47 +01:00
if (!RenderSystem.isOnRenderThread()) {
2020-07-22 06:32:50 +01:00
return CompletableFuture.runAsync(() -> this.upload_(dhg), runnable -> RenderSystem.recordRenderCall(runnable::run));
2020-07-22 06:25:47 +01:00
}
2020-07-22 06:32:50 +01:00
this.upload_(dhg);
2020-07-22 06:25:47 +01:00
return CompletableFuture.<Void>completedFuture((Void)null);
}
2020-07-22 06:32:50 +01:00
private void upload_(final BufferBuilder dhg) {
final Pair<BufferBuilder.DrawState, ByteBuffer> pair3 = dhg.popNextBuffer();
2020-07-22 06:25:47 +01:00
if (this.id == -1) {
return;
}
final ByteBuffer byteBuffer4 = (ByteBuffer)pair3.getSecond();
this.vertexCount = byteBuffer4.remaining() / this.format.getVertexSize();
2020-07-22 06:23:34 +01:00
this.bind();
2020-07-22 06:25:47 +01:00
RenderSystem.glBufferData(34962, byteBuffer4, 35044);
2020-07-22 06:23:34 +01:00
unbind();
}
2020-07-22 06:25:47 +01:00
public void draw(final Matrix4f b, final int integer) {
RenderSystem.pushMatrix();
RenderSystem.loadIdentity();
RenderSystem.multMatrix(b);
RenderSystem.drawArrays(integer, 0, this.vertexCount);
RenderSystem.popMatrix();
2020-07-22 06:23:34 +01:00
}
public static void unbind() {
2020-07-22 06:25:47 +01:00
RenderSystem.glBindBuffer(34962, () -> 0);
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:25:47 +01:00
@Override
public void close() {
2020-07-22 06:23:34 +01:00
if (this.id >= 0) {
2020-07-22 06:25:47 +01:00
RenderSystem.glDeleteBuffers(this.id);
2020-07-22 06:23:34 +01:00
this.id = -1;
}
}
}