Models are temporary

- Add Model.delete
- Allow BlockModel.createEBO to be called more than once for a single
instance
- Clear cache on QuadConverter.delete
This commit is contained in:
PepperCode1 2022-11-06 14:00:24 -08:00
parent 9280ae50dd
commit f47f547930
5 changed files with 79 additions and 19 deletions

View file

@ -69,7 +69,6 @@ public class GPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
vao = new GlVertexArray();
// XXX Callback seems unnecessary. Remove and extract code to run after alloc call?
model = modelAllocator.alloc(modelData, arenaModel -> {
// XXX VAO is bound and not reset or restored
vao.bind();
@ -77,7 +76,7 @@ public class GPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
arenaModel.setupState(vao);
});
// XXX VAO is already guaranteed to be bound in model callback
// XXX VAO is bound and not reset or restored
vao.bind();
vao.enableArrays(model.getAttributeCount() + instanceFormat.getAttributeCount());

View file

@ -79,6 +79,7 @@ public class QuadConverter {
public void delete() {
GL32.glDeleteBuffers(ebo);
this.cache.clear();
this.quadCapacity = 0;
}

View file

@ -59,4 +59,15 @@ public class ModelPart implements Model {
return QuadConverter.getInstance()
.quads2Tris(vertices / 4);
}
@Override
public void delete() {
if (reader instanceof AutoCloseable closeable) {
try {
closeable.close();
} catch (Exception e) {
//
}
}
}
}

View file

@ -19,6 +19,7 @@ import com.mojang.blaze3d.platform.MemoryTracker;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferBuilder.RenderedBuffer;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexFormat.IndexType;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.BakedModel;
@ -32,7 +33,7 @@ public class BlockModel implements Model {
private final String name;
private final Supplier<ElementBuffer> eboSupplier;
private final EBOSupplier eboSupplier;
public BlockModel(BlockState state) {
this(Minecraft.getInstance()
@ -61,22 +62,7 @@ public class BlockModel implements Model {
reader = Formats.BLOCK.createReader(renderedBuffer, pair.second());
if (!drawState.sequentialIndex()) {
ByteBuffer src = renderedBuffer.indexBuffer();
ByteBuffer indexBuffer = MemoryTracker.create(src.capacity());
MemoryUtil.memCopy(src, indexBuffer);
eboSupplier = () -> {
int vbo = GL32.glGenBuffers();
// XXX ARRAY_BUFFER is bound and restored
var bufferType = GlBufferType.ARRAY_BUFFER;
var oldBuffer = bufferType.getBoundBuffer();
bufferType.bind(vbo);
GL15.glBufferData(bufferType.glEnum, indexBuffer, GlBufferUsage.STATIC_DRAW.glEnum);
bufferType.bind(oldBuffer);
MemoryUtil.memFree(indexBuffer);
return new ElementBuffer(vbo, drawState.indexCount(), drawState.indexType());
};
eboSupplier = new BufferEBOSupplier(renderedBuffer.indexBuffer(), drawState.indexCount(), drawState.indexType());
} else {
eboSupplier = () -> QuadConverter.getInstance()
.quads2Tris(vertexCount() / 4);
@ -107,4 +93,65 @@ public class BlockModel implements Model {
public VertexType getType() {
return Formats.BLOCK;
}
@Override
public void delete() {
if (reader instanceof AutoCloseable closeable) {
try {
closeable.close();
} catch (Exception e) {
//
}
}
eboSupplier.delete();
}
private interface EBOSupplier extends Supplier<ElementBuffer> {
default void delete() {
}
}
private static class BufferEBOSupplier implements EBOSupplier {
private final ByteBuffer indexBuffer;
private final int indexCount;
private final IndexType indexType;
private int eboName = -1;
private ElementBuffer ebo;
public BufferEBOSupplier(ByteBuffer indexBufferSrc, int indexCount, IndexType indexType) {
indexBuffer = MemoryTracker.create(indexBufferSrc.capacity());
MemoryUtil.memCopy(indexBufferSrc, indexBuffer);
this.indexCount = indexCount;
this.indexType = indexType;
}
@Override
public ElementBuffer get() {
if (eboName == -1) {
eboName = createEBO();
ebo = new ElementBuffer(eboName, indexCount, indexType);
MemoryUtil.memFree(indexBuffer);
}
return ebo;
}
private int createEBO() {
int vbo = GL32.glGenBuffers();
// XXX ARRAY_BUFFER is bound and restored
var bufferType = GlBufferType.ARRAY_BUFFER;
var oldBuffer = bufferType.getBoundBuffer();
bufferType.bind(vbo);
GL15.glBufferData(bufferType.glEnum, indexBuffer, GlBufferUsage.STATIC_DRAW.glEnum);
bufferType.bind(oldBuffer);
return vbo;
}
@Override
public void delete() {
GL32.glDeleteBuffers(eboName);
}
}
}

View file

@ -58,6 +58,8 @@ public interface Model {
*/
ElementBuffer createEBO();
void delete();
/**
* The size in bytes that this model's data takes up.
*/