mirror of
https://github.com/Jozufozu/Flywheel.git
synced 2024-12-27 23:47:09 +01:00
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:
parent
9280ae50dd
commit
f47f547930
5 changed files with 79 additions and 19 deletions
|
@ -69,7 +69,6 @@ public class GPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
|
||||||
|
|
||||||
vao = new GlVertexArray();
|
vao = new GlVertexArray();
|
||||||
|
|
||||||
// XXX Callback seems unnecessary. Remove and extract code to run after alloc call?
|
|
||||||
model = modelAllocator.alloc(modelData, arenaModel -> {
|
model = modelAllocator.alloc(modelData, arenaModel -> {
|
||||||
// XXX VAO is bound and not reset or restored
|
// XXX VAO is bound and not reset or restored
|
||||||
vao.bind();
|
vao.bind();
|
||||||
|
@ -77,7 +76,7 @@ public class GPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
|
||||||
arenaModel.setupState(vao);
|
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.bind();
|
||||||
vao.enableArrays(model.getAttributeCount() + instanceFormat.getAttributeCount());
|
vao.enableArrays(model.getAttributeCount() + instanceFormat.getAttributeCount());
|
||||||
|
|
||||||
|
|
|
@ -79,6 +79,7 @@ public class QuadConverter {
|
||||||
|
|
||||||
public void delete() {
|
public void delete() {
|
||||||
GL32.glDeleteBuffers(ebo);
|
GL32.glDeleteBuffers(ebo);
|
||||||
|
this.cache.clear();
|
||||||
this.quadCapacity = 0;
|
this.quadCapacity = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,4 +59,15 @@ public class ModelPart implements Model {
|
||||||
return QuadConverter.getInstance()
|
return QuadConverter.getInstance()
|
||||||
.quads2Tris(vertices / 4);
|
.quads2Tris(vertices / 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete() {
|
||||||
|
if (reader instanceof AutoCloseable closeable) {
|
||||||
|
try {
|
||||||
|
closeable.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import com.mojang.blaze3d.platform.MemoryTracker;
|
||||||
import com.mojang.blaze3d.vertex.BufferBuilder;
|
import com.mojang.blaze3d.vertex.BufferBuilder;
|
||||||
import com.mojang.blaze3d.vertex.BufferBuilder.RenderedBuffer;
|
import com.mojang.blaze3d.vertex.BufferBuilder.RenderedBuffer;
|
||||||
import com.mojang.blaze3d.vertex.PoseStack;
|
import com.mojang.blaze3d.vertex.PoseStack;
|
||||||
|
import com.mojang.blaze3d.vertex.VertexFormat.IndexType;
|
||||||
|
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.resources.model.BakedModel;
|
import net.minecraft.client.resources.model.BakedModel;
|
||||||
|
@ -32,7 +33,7 @@ public class BlockModel implements Model {
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
private final Supplier<ElementBuffer> eboSupplier;
|
private final EBOSupplier eboSupplier;
|
||||||
|
|
||||||
public BlockModel(BlockState state) {
|
public BlockModel(BlockState state) {
|
||||||
this(Minecraft.getInstance()
|
this(Minecraft.getInstance()
|
||||||
|
@ -61,22 +62,7 @@ public class BlockModel implements Model {
|
||||||
reader = Formats.BLOCK.createReader(renderedBuffer, pair.second());
|
reader = Formats.BLOCK.createReader(renderedBuffer, pair.second());
|
||||||
|
|
||||||
if (!drawState.sequentialIndex()) {
|
if (!drawState.sequentialIndex()) {
|
||||||
ByteBuffer src = renderedBuffer.indexBuffer();
|
eboSupplier = new BufferEBOSupplier(renderedBuffer.indexBuffer(), drawState.indexCount(), drawState.indexType());
|
||||||
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());
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
eboSupplier = () -> QuadConverter.getInstance()
|
eboSupplier = () -> QuadConverter.getInstance()
|
||||||
.quads2Tris(vertexCount() / 4);
|
.quads2Tris(vertexCount() / 4);
|
||||||
|
@ -107,4 +93,65 @@ public class BlockModel implements Model {
|
||||||
public VertexType getType() {
|
public VertexType getType() {
|
||||||
return Formats.BLOCK;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,6 +58,8 @@ public interface Model {
|
||||||
*/
|
*/
|
||||||
ElementBuffer createEBO();
|
ElementBuffer createEBO();
|
||||||
|
|
||||||
|
void delete();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The size in bytes that this model's data takes up.
|
* The size in bytes that this model's data takes up.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue