mirror of
https://github.com/Jozufozu/Flywheel.git
synced 2025-02-11 20:55:00 +01:00
Memcpy: Batching Edition
- Add BatchedMeshPool - Rename MeshPool to InstancedMeshPool - Rename/remove/add methods to VertexList/ReusableVertexList
This commit is contained in:
parent
d7613fc5e4
commit
5ac6065205
19 changed files with 341 additions and 105 deletions
|
@ -5,7 +5,7 @@ public interface ReusableVertexList extends MutableVertexList {
|
||||||
|
|
||||||
void ptr(long ptr);
|
void ptr(long ptr);
|
||||||
|
|
||||||
void shiftPtr(int vertices);
|
int vertexStride();
|
||||||
|
|
||||||
void setVertexCount(int vertexCount);
|
void vertexCount(int vertexCount);
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,12 +70,12 @@ public interface VertexList {
|
||||||
}
|
}
|
||||||
|
|
||||||
default void writeAll(MutableVertexList dst) {
|
default void writeAll(MutableVertexList dst) {
|
||||||
write(dst, 0, 0, getVertexCount());
|
write(dst, 0, 0, vertexCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
int getVertexCount();
|
int vertexCount();
|
||||||
|
|
||||||
default boolean isEmpty() {
|
default boolean isEmpty() {
|
||||||
return getVertexCount() == 0;
|
return vertexCount() == 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,208 @@
|
||||||
|
package com.jozufozu.flywheel.backend.instancing.batching;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.lwjgl.system.MemoryUtil;
|
||||||
|
|
||||||
|
import com.jozufozu.flywheel.Flywheel;
|
||||||
|
import com.jozufozu.flywheel.api.vertex.ReusableVertexList;
|
||||||
|
import com.jozufozu.flywheel.api.vertex.VertexListProvider;
|
||||||
|
import com.jozufozu.flywheel.backend.memory.MemoryBlock;
|
||||||
|
import com.jozufozu.flywheel.core.model.Mesh;
|
||||||
|
import com.mojang.blaze3d.vertex.VertexFormat;
|
||||||
|
|
||||||
|
public class BatchedMeshPool {
|
||||||
|
private final VertexFormat vertexFormat;
|
||||||
|
private final ReusableVertexList vertexList;
|
||||||
|
private final int growthMargin;
|
||||||
|
|
||||||
|
private final Map<Mesh, BufferedMesh> meshes = new HashMap<>();
|
||||||
|
private final List<BufferedMesh> allBuffered = new ArrayList<>();
|
||||||
|
private final List<BufferedMesh> pendingUpload = new ArrayList<>();
|
||||||
|
|
||||||
|
private MemoryBlock memory;
|
||||||
|
private long byteSize;
|
||||||
|
|
||||||
|
private boolean dirty;
|
||||||
|
private boolean anyToRemove;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new mesh pool.
|
||||||
|
*/
|
||||||
|
public BatchedMeshPool(VertexFormat vertexFormat) {
|
||||||
|
this.vertexFormat = vertexFormat;
|
||||||
|
vertexList = VertexListProvider.get(vertexFormat).createVertexList();
|
||||||
|
growthMargin = vertexList.vertexStride() * 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocate a mesh in the arena.
|
||||||
|
*
|
||||||
|
* @param mesh The mesh to allocate.
|
||||||
|
* @return A handle to the allocated mesh.
|
||||||
|
*/
|
||||||
|
public BufferedMesh alloc(Mesh mesh) {
|
||||||
|
return meshes.computeIfAbsent(mesh, m -> {
|
||||||
|
BufferedMesh bufferedMesh = new BufferedMesh(m, byteSize, m.getVertexCount() * vertexList.vertexStride());
|
||||||
|
byteSize += bufferedMesh.size();
|
||||||
|
allBuffered.add(bufferedMesh);
|
||||||
|
pendingUpload.add(bufferedMesh);
|
||||||
|
|
||||||
|
dirty = true;
|
||||||
|
return bufferedMesh;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public BufferedMesh get(Mesh mesh) {
|
||||||
|
return meshes.get(mesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void flush() {
|
||||||
|
if (dirty) {
|
||||||
|
if (anyToRemove) {
|
||||||
|
processDeletions();
|
||||||
|
}
|
||||||
|
|
||||||
|
realloc();
|
||||||
|
uploadPending();
|
||||||
|
|
||||||
|
dirty = false;
|
||||||
|
pendingUpload.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processDeletions() {
|
||||||
|
// remove deleted meshes
|
||||||
|
allBuffered.removeIf(bufferedMesh -> {
|
||||||
|
boolean deleted = bufferedMesh.isDeleted();
|
||||||
|
if (deleted) {
|
||||||
|
meshes.remove(bufferedMesh.mesh);
|
||||||
|
}
|
||||||
|
return deleted;
|
||||||
|
});
|
||||||
|
|
||||||
|
// re-evaluate first vertex for each mesh
|
||||||
|
int byteIndex = 0;
|
||||||
|
for (BufferedMesh mesh : allBuffered) {
|
||||||
|
if (mesh.byteIndex != byteIndex) {
|
||||||
|
pendingUpload.add(mesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
mesh.byteIndex = byteIndex;
|
||||||
|
|
||||||
|
byteIndex += mesh.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.byteSize = byteIndex;
|
||||||
|
this.anyToRemove = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void realloc() {
|
||||||
|
if (byteSize < 0) {
|
||||||
|
throw new IllegalArgumentException("Size " + byteSize + " < 0");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (byteSize == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (memory == null) {
|
||||||
|
memory = MemoryBlock.malloc(byteSize);
|
||||||
|
} else if (byteSize > memory.size()) {
|
||||||
|
memory = memory.realloc(byteSize + growthMargin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void uploadPending() {
|
||||||
|
try {
|
||||||
|
for (BufferedMesh mesh : pendingUpload) {
|
||||||
|
mesh.buffer(vertexList);
|
||||||
|
}
|
||||||
|
|
||||||
|
pendingUpload.clear();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Flywheel.LOGGER.error("Error uploading pooled meshes:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete() {
|
||||||
|
if (memory != null) {
|
||||||
|
memory.free();
|
||||||
|
}
|
||||||
|
meshes.clear();
|
||||||
|
allBuffered.clear();
|
||||||
|
pendingUpload.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public VertexFormat getVertexFormat() {
|
||||||
|
return vertexFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "BatchedMeshPool{" + "vertexFormat=" + vertexFormat + ", byteSize=" + byteSize + ", meshCount=" + meshes.size() + '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BufferedMesh {
|
||||||
|
private final Mesh mesh;
|
||||||
|
private long byteIndex;
|
||||||
|
private int byteSize;
|
||||||
|
private boolean deleted;
|
||||||
|
|
||||||
|
private BufferedMesh(Mesh mesh, long byteIndex, int byteSize) {
|
||||||
|
this.mesh = mesh;
|
||||||
|
this.byteIndex = byteIndex;
|
||||||
|
this.byteSize = byteSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buffer(ReusableVertexList vertexList) {
|
||||||
|
vertexList.ptr(ptr());
|
||||||
|
vertexList.vertexCount(mesh.getVertexCount());
|
||||||
|
|
||||||
|
mesh.write(vertexList);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void copyTo(long ptr) {
|
||||||
|
if (isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MemoryUtil.memCopy(ptr(), ptr, byteSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isEmpty() {
|
||||||
|
return mesh.isEmpty() || isDeleted();
|
||||||
|
}
|
||||||
|
|
||||||
|
private long ptr() {
|
||||||
|
return BatchedMeshPool.this.memory.ptr() + byteIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete() {
|
||||||
|
deleted = true;
|
||||||
|
BatchedMeshPool.this.dirty = true;
|
||||||
|
BatchedMeshPool.this.anyToRemove = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mesh getMesh() {
|
||||||
|
return mesh;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return byteSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public VertexFormat getVertexFormat() {
|
||||||
|
return vertexFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDeleted() {
|
||||||
|
return deleted;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -14,7 +15,9 @@ import com.google.common.collect.ArrayListMultimap;
|
||||||
import com.google.common.collect.ImmutableListMultimap;
|
import com.google.common.collect.ImmutableListMultimap;
|
||||||
import com.google.common.collect.ListMultimap;
|
import com.google.common.collect.ListMultimap;
|
||||||
import com.jozufozu.flywheel.api.RenderStage;
|
import com.jozufozu.flywheel.api.RenderStage;
|
||||||
|
import com.jozufozu.flywheel.core.model.Mesh;
|
||||||
import com.jozufozu.flywheel.core.model.Model;
|
import com.jozufozu.flywheel.core.model.Model;
|
||||||
|
import com.mojang.blaze3d.vertex.VertexFormat;
|
||||||
|
|
||||||
import net.minecraft.client.renderer.RenderType;
|
import net.minecraft.client.renderer.RenderType;
|
||||||
|
|
||||||
|
@ -23,6 +26,7 @@ public class BatchingTransformManager {
|
||||||
private final List<CPUInstancer<?>> allInstancers = new ArrayList<>();
|
private final List<CPUInstancer<?>> allInstancers = new ArrayList<>();
|
||||||
private final Map<RenderStage, TransformSet> transformSets = new EnumMap<>(RenderStage.class);
|
private final Map<RenderStage, TransformSet> transformSets = new EnumMap<>(RenderStage.class);
|
||||||
private final Map<RenderStage, TransformSet> transformSetsView = Collections.unmodifiableMap(transformSets);
|
private final Map<RenderStage, TransformSet> transformSetsView = Collections.unmodifiableMap(transformSets);
|
||||||
|
private final Map<VertexFormat, BatchedMeshPool> meshPools = new HashMap<>();
|
||||||
|
|
||||||
public TransformSet get(RenderStage stage) {
|
public TransformSet get(RenderStage stage) {
|
||||||
return transformSets.getOrDefault(stage, TransformSet.EMPTY);
|
return transformSets.getOrDefault(stage, TransformSet.EMPTY);
|
||||||
|
@ -41,9 +45,17 @@ public class BatchingTransformManager {
|
||||||
add(model.instancer(), model.model());
|
add(model.instancer(), model.model());
|
||||||
}
|
}
|
||||||
uninitializedModels.clear();
|
uninitializedModels.clear();
|
||||||
|
|
||||||
|
for (var pool : meshPools.values()) {
|
||||||
|
pool.flush();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete() {
|
public void delete() {
|
||||||
|
meshPools.values()
|
||||||
|
.forEach(BatchedMeshPool::delete);
|
||||||
|
meshPools.clear();
|
||||||
|
|
||||||
allInstancers.forEach(CPUInstancer::delete);
|
allInstancers.forEach(CPUInstancer::delete);
|
||||||
allInstancers.clear();
|
allInstancers.clear();
|
||||||
}
|
}
|
||||||
|
@ -55,9 +67,9 @@ public class BatchingTransformManager {
|
||||||
private void add(CPUInstancer<?> instancer, Model model) {
|
private void add(CPUInstancer<?> instancer, Model model) {
|
||||||
var meshes = model.getMeshes();
|
var meshes = model.getMeshes();
|
||||||
for (var entry : meshes.entrySet()) {
|
for (var entry : meshes.entrySet()) {
|
||||||
TransformCall<?> transformCall = new TransformCall<>(instancer, entry.getKey(), entry.getValue());
|
var material = entry.getKey();
|
||||||
var material = transformCall.getMaterial();
|
|
||||||
var renderType = material.getBatchingRenderType();
|
var renderType = material.getBatchingRenderType();
|
||||||
|
TransformCall<?> transformCall = new TransformCall<>(instancer, material, alloc(entry.getValue(), renderType.format()));
|
||||||
|
|
||||||
transformSets.computeIfAbsent(material.getRenderStage(), TransformSet::new)
|
transformSets.computeIfAbsent(material.getRenderStage(), TransformSet::new)
|
||||||
.put(renderType, transformCall);
|
.put(renderType, transformCall);
|
||||||
|
@ -65,6 +77,11 @@ public class BatchingTransformManager {
|
||||||
allInstancers.add(instancer);
|
allInstancers.add(instancer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private BatchedMeshPool.BufferedMesh alloc(Mesh mesh, VertexFormat format) {
|
||||||
|
return meshPools.computeIfAbsent(format, BatchedMeshPool::new)
|
||||||
|
.alloc(mesh);
|
||||||
|
}
|
||||||
|
|
||||||
public static class TransformSet implements Iterable<Map.Entry<RenderType, Collection<TransformCall<?>>>> {
|
public static class TransformSet implements Iterable<Map.Entry<RenderType, Collection<TransformCall<?>>>> {
|
||||||
public static final TransformSet EMPTY = new TransformSet(ImmutableListMultimap.of());
|
public static final TransformSet EMPTY = new TransformSet(ImmutableListMultimap.of());
|
||||||
|
|
||||||
|
|
|
@ -72,9 +72,8 @@ public class DrawBuffer {
|
||||||
}
|
}
|
||||||
|
|
||||||
ReusableVertexList vertexList = provider.createVertexList();
|
ReusableVertexList vertexList = provider.createVertexList();
|
||||||
vertexList.ptr(memory.ptr());
|
vertexList.ptr(memory.ptr() + startVertex * vertexList.vertexStride());
|
||||||
vertexList.shiftPtr(startVertex);
|
vertexList.vertexCount(vertexCount);
|
||||||
vertexList.setVertexCount(vertexCount);
|
|
||||||
return vertexList;
|
return vertexList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,6 +90,10 @@ public class DrawBuffer {
|
||||||
bufferBuilder.flywheel$injectForRender(buffer, format, vertexCount);
|
bufferBuilder.flywheel$injectForRender(buffer, format, vertexCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public VertexFormat getVertexFormat() {
|
||||||
|
return format;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isPrepared() {
|
public boolean isPrepared() {
|
||||||
return prepared;
|
return prepared;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@ import com.jozufozu.flywheel.api.struct.StructType;
|
||||||
import com.jozufozu.flywheel.api.vertex.MutableVertexList;
|
import com.jozufozu.flywheel.api.vertex.MutableVertexList;
|
||||||
import com.jozufozu.flywheel.api.vertex.ReusableVertexList;
|
import com.jozufozu.flywheel.api.vertex.ReusableVertexList;
|
||||||
import com.jozufozu.flywheel.backend.instancing.TaskEngine;
|
import com.jozufozu.flywheel.backend.instancing.TaskEngine;
|
||||||
import com.jozufozu.flywheel.core.model.Mesh;
|
|
||||||
import com.mojang.blaze3d.vertex.PoseStack;
|
import com.mojang.blaze3d.vertex.PoseStack;
|
||||||
|
import com.mojang.blaze3d.vertex.VertexFormat;
|
||||||
import com.mojang.math.Matrix3f;
|
import com.mojang.math.Matrix3f;
|
||||||
import com.mojang.math.Matrix4f;
|
import com.mojang.math.Matrix4f;
|
||||||
import com.mojang.math.Vector3f;
|
import com.mojang.math.Vector3f;
|
||||||
|
@ -18,23 +18,22 @@ import com.mojang.math.Vector4f;
|
||||||
import net.minecraft.client.multiplayer.ClientLevel;
|
import net.minecraft.client.multiplayer.ClientLevel;
|
||||||
|
|
||||||
public class TransformCall<D extends InstancedPart> {
|
public class TransformCall<D extends InstancedPart> {
|
||||||
|
|
||||||
private final CPUInstancer<D> instancer;
|
private final CPUInstancer<D> instancer;
|
||||||
private final Material material;
|
private final Material material;
|
||||||
private final Mesh mesh;
|
private final BatchedMeshPool.BufferedMesh bufferedMesh;
|
||||||
|
|
||||||
public TransformCall(CPUInstancer<D> instancer, Material material, Mesh mesh) {
|
public TransformCall(CPUInstancer<D> instancer, Material material, BatchedMeshPool.BufferedMesh mesh) {
|
||||||
this.instancer = instancer;
|
this.instancer = instancer;
|
||||||
this.material = material;
|
this.material = material;
|
||||||
this.mesh = mesh;
|
this.bufferedMesh = mesh;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Material getMaterial() {
|
public Material getMaterial() {
|
||||||
return material;
|
return material;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Mesh getMesh() {
|
public VertexFormat getVertexFormat() {
|
||||||
return mesh;
|
return bufferedMesh.getVertexFormat();
|
||||||
}
|
}
|
||||||
|
|
||||||
void submitTasks(TaskEngine pool, DrawBuffer buffer, int startVertex, PoseStack stack, ClientLevel level) {
|
void submitTasks(TaskEngine pool, DrawBuffer buffer, int startVertex, PoseStack stack, ClientLevel level) {
|
||||||
|
@ -47,7 +46,7 @@ public class TransformCall<D extends InstancedPart> {
|
||||||
instances -= 512;
|
instances -= 512;
|
||||||
int start = Math.max(instances, 0);
|
int start = Math.max(instances, 0);
|
||||||
|
|
||||||
int vertexCount = mesh.getVertexCount() * (end - start);
|
int vertexCount = bufferedMesh.getMesh().getVertexCount() * (end - start);
|
||||||
ReusableVertexList sub = buffer.slice(startVertex, vertexCount);
|
ReusableVertexList sub = buffer.slice(startVertex, vertexCount);
|
||||||
startVertex += vertexCount;
|
startVertex += vertexCount;
|
||||||
|
|
||||||
|
@ -65,23 +64,24 @@ public class TransformCall<D extends InstancedPart> {
|
||||||
|
|
||||||
private void transformList(ReusableVertexList vertexList, List<D> parts, PoseStack stack, ClientLevel level) {
|
private void transformList(ReusableVertexList vertexList, List<D> parts, PoseStack stack, ClientLevel level) {
|
||||||
long anchorPtr = vertexList.ptr();
|
long anchorPtr = vertexList.ptr();
|
||||||
int totalVertexCount = vertexList.getVertexCount();
|
int totalVertexCount = vertexList.vertexCount();
|
||||||
|
|
||||||
int meshVertexCount = mesh.getVertexCount();
|
int meshVertexCount = bufferedMesh.getMesh().getVertexCount();
|
||||||
vertexList.setVertexCount(meshVertexCount);
|
int meshByteSize = bufferedMesh.size();
|
||||||
|
vertexList.vertexCount(meshVertexCount);
|
||||||
|
|
||||||
StructType.VertexTransformer<D> structVertexTransformer = instancer.type.getVertexTransformer();
|
StructType.VertexTransformer<D> structVertexTransformer = instancer.type.getVertexTransformer();
|
||||||
|
|
||||||
for (D d : parts) {
|
for (D d : parts) {
|
||||||
mesh.write(vertexList);
|
bufferedMesh.copyTo(vertexList.ptr());
|
||||||
|
|
||||||
structVertexTransformer.transform(vertexList, d, level);
|
structVertexTransformer.transform(vertexList, d, level);
|
||||||
|
|
||||||
vertexList.shiftPtr(meshVertexCount);
|
vertexList.ptr(vertexList.ptr() + meshByteSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
vertexList.ptr(anchorPtr);
|
vertexList.ptr(anchorPtr);
|
||||||
vertexList.setVertexCount(totalVertexCount);
|
vertexList.vertexCount(totalVertexCount);
|
||||||
material.getVertexTransformer().transform(vertexList, level);
|
material.getVertexTransformer().transform(vertexList, level);
|
||||||
applyPoseStack(vertexList, stack);
|
applyPoseStack(vertexList, stack);
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ public class TransformCall<D extends InstancedPart> {
|
||||||
Matrix4f modelMatrix = stack.last().pose();
|
Matrix4f modelMatrix = stack.last().pose();
|
||||||
Matrix3f normalMatrix = stack.last().normal();
|
Matrix3f normalMatrix = stack.last().normal();
|
||||||
|
|
||||||
for (int i = 0; i < vertexList.getVertexCount(); i++) {
|
for (int i = 0; i < vertexList.vertexCount(); i++) {
|
||||||
pos.set(
|
pos.set(
|
||||||
vertexList.x(i),
|
vertexList.x(i),
|
||||||
vertexList.y(i),
|
vertexList.y(i),
|
||||||
|
@ -119,6 +119,6 @@ public class TransformCall<D extends InstancedPart> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTotalVertexCount() {
|
public int getTotalVertexCount() {
|
||||||
return mesh.getVertexCount() * instancer.getInstanceCount();
|
return bufferedMesh.getMesh().getVertexCount() * instancer.getInstanceCount();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,10 @@ public class DrawCall {
|
||||||
final GPUInstancer<?> instancer;
|
final GPUInstancer<?> instancer;
|
||||||
final Material material;
|
final Material material;
|
||||||
private final int meshAttributes;
|
private final int meshAttributes;
|
||||||
MeshPool.BufferedMesh bufferedMesh;
|
InstancedMeshPool.BufferedMesh bufferedMesh;
|
||||||
GlVertexArray vao;
|
GlVertexArray vao;
|
||||||
|
|
||||||
DrawCall(GPUInstancer<?> instancer, Material material, MeshPool.BufferedMesh mesh) {
|
DrawCall(GPUInstancer<?> instancer, Material material, InstancedMeshPool.BufferedMesh mesh) {
|
||||||
this.instancer = instancer;
|
this.instancer = instancer;
|
||||||
this.material = material;
|
this.material = material;
|
||||||
this.vao = new GlVertexArray();
|
this.vao = new GlVertexArray();
|
||||||
|
|
|
@ -19,16 +19,14 @@ import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType;
|
||||||
import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer;
|
import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer;
|
||||||
import com.jozufozu.flywheel.core.model.Mesh;
|
import com.jozufozu.flywheel.core.model.Mesh;
|
||||||
|
|
||||||
public class MeshPool {
|
public class InstancedMeshPool {
|
||||||
|
|
||||||
private final VertexType vertexType;
|
private final VertexType vertexType;
|
||||||
|
|
||||||
private final Map<Mesh, BufferedMesh> meshes = new HashMap<>();
|
private final Map<Mesh, BufferedMesh> meshes = new HashMap<>();
|
||||||
private final List<BufferedMesh> allBuffered = new ArrayList<>();
|
private final List<BufferedMesh> allBuffered = new ArrayList<>();
|
||||||
|
|
||||||
private final List<BufferedMesh> pendingUpload = new ArrayList<>();
|
private final List<BufferedMesh> pendingUpload = new ArrayList<>();
|
||||||
|
|
||||||
private final GlBuffer vbo;
|
private final GlBuffer vbo;
|
||||||
|
|
||||||
private long byteSize;
|
private long byteSize;
|
||||||
|
|
||||||
private boolean dirty;
|
private boolean dirty;
|
||||||
|
@ -37,7 +35,7 @@ public class MeshPool {
|
||||||
/**
|
/**
|
||||||
* Create a new mesh pool.
|
* Create a new mesh pool.
|
||||||
*/
|
*/
|
||||||
public MeshPool(VertexType vertexType) {
|
public InstancedMeshPool(VertexType vertexType) {
|
||||||
this.vertexType = vertexType;
|
this.vertexType = vertexType;
|
||||||
int stride = vertexType.getStride();
|
int stride = vertexType.getStride();
|
||||||
this.vbo = new GlBuffer(GlBufferType.ARRAY_BUFFER);
|
this.vbo = new GlBuffer(GlBufferType.ARRAY_BUFFER);
|
||||||
|
@ -46,10 +44,10 @@ public class MeshPool {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allocate a model in the arena.
|
* Allocate a mesh in the arena.
|
||||||
*
|
*
|
||||||
* @param mesh The model to allocate.
|
* @param mesh The mesh to allocate.
|
||||||
* @return A handle to the allocated model.
|
* @return A handle to the allocated mesh.
|
||||||
*/
|
*/
|
||||||
public BufferedMesh alloc(Mesh mesh) {
|
public BufferedMesh alloc(Mesh mesh) {
|
||||||
return meshes.computeIfAbsent(mesh, m -> {
|
return meshes.computeIfAbsent(mesh, m -> {
|
||||||
|
@ -57,13 +55,13 @@ public class MeshPool {
|
||||||
throw new IllegalArgumentException("Mesh has wrong vertex type");
|
throw new IllegalArgumentException("Mesh has wrong vertex type");
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferedMesh bufferedModel = new BufferedMesh(m, byteSize);
|
BufferedMesh bufferedMesh = new BufferedMesh(m, byteSize);
|
||||||
byteSize += m.size();
|
byteSize += bufferedMesh.size();
|
||||||
allBuffered.add(bufferedModel);
|
allBuffered.add(bufferedMesh);
|
||||||
pendingUpload.add(bufferedModel);
|
pendingUpload.add(bufferedMesh);
|
||||||
|
|
||||||
dirty = true;
|
dirty = true;
|
||||||
return bufferedModel;
|
return bufferedMesh;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +88,6 @@ public class MeshPool {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processDeletions() {
|
private void processDeletions() {
|
||||||
|
|
||||||
// remove deleted meshes
|
// remove deleted meshes
|
||||||
allBuffered.removeIf(bufferedMesh -> {
|
allBuffered.removeIf(bufferedMesh -> {
|
||||||
boolean deleted = bufferedMesh.isDeleted();
|
boolean deleted = bufferedMesh.isDeleted();
|
||||||
|
@ -100,16 +97,16 @@ public class MeshPool {
|
||||||
return deleted;
|
return deleted;
|
||||||
});
|
});
|
||||||
|
|
||||||
// re-evaluate first vertex for each model
|
// re-evaluate first vertex for each mesh
|
||||||
int byteIndex = 0;
|
int byteIndex = 0;
|
||||||
for (BufferedMesh model : allBuffered) {
|
for (BufferedMesh mesh : allBuffered) {
|
||||||
if (model.byteIndex != byteIndex) {
|
if (mesh.byteIndex != byteIndex) {
|
||||||
pendingUpload.add(model);
|
pendingUpload.add(mesh);
|
||||||
}
|
}
|
||||||
|
|
||||||
model.byteIndex = byteIndex;
|
mesh.byteIndex = byteIndex;
|
||||||
|
|
||||||
byteIndex += model.mesh.size();
|
byteIndex += mesh.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.byteSize = byteIndex;
|
this.byteSize = byteIndex;
|
||||||
|
@ -130,14 +127,13 @@ public class MeshPool {
|
||||||
long ptr = mapped.getPtr();
|
long ptr = mapped.getPtr();
|
||||||
|
|
||||||
int byteIndex = 0;
|
int byteIndex = 0;
|
||||||
for (BufferedMesh model : allBuffered) {
|
for (BufferedMesh mesh : allBuffered) {
|
||||||
model.byteIndex = byteIndex;
|
mesh.byteIndex = byteIndex;
|
||||||
|
|
||||||
model.buffer(ptr);
|
mesh.buffer(ptr);
|
||||||
|
|
||||||
byteIndex += model.mesh.size();
|
byteIndex += mesh.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Flywheel.LOGGER.error("Error uploading pooled meshes:", e);
|
Flywheel.LOGGER.error("Error uploading pooled meshes:", e);
|
||||||
}
|
}
|
||||||
|
@ -145,10 +141,12 @@ public class MeshPool {
|
||||||
|
|
||||||
private void uploadPending() {
|
private void uploadPending() {
|
||||||
try (MappedBuffer mapped = vbo.map()) {
|
try (MappedBuffer mapped = vbo.map()) {
|
||||||
long buffer = mapped.getPtr();
|
long ptr = mapped.getPtr();
|
||||||
for (BufferedMesh model : pendingUpload) {
|
|
||||||
model.buffer(buffer);
|
for (BufferedMesh mesh : pendingUpload) {
|
||||||
|
mesh.buffer(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
pendingUpload.clear();
|
pendingUpload.clear();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Flywheel.LOGGER.error("Error uploading pooled meshes:", e);
|
Flywheel.LOGGER.error("Error uploading pooled meshes:", e);
|
||||||
|
@ -162,32 +160,41 @@ public class MeshPool {
|
||||||
pendingUpload.clear();
|
pendingUpload.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public VertexType getVertexType() {
|
||||||
|
return vertexType;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "MeshPool{" + "vertexType=" + vertexType + ", byteSize=" + byteSize + ", meshCount=" + meshes.size() + '}';
|
return "InstancedMeshPool{" + "vertexType=" + vertexType + ", byteSize=" + byteSize + ", meshCount=" + meshes.size() + '}';
|
||||||
}
|
}
|
||||||
|
|
||||||
public class BufferedMesh {
|
public class BufferedMesh {
|
||||||
|
|
||||||
private final ElementBuffer ebo;
|
|
||||||
private final Mesh mesh;
|
private final Mesh mesh;
|
||||||
|
private final ElementBuffer ebo;
|
||||||
private long byteIndex;
|
private long byteIndex;
|
||||||
private boolean deleted;
|
private boolean deleted;
|
||||||
|
|
||||||
private final Set<GlVertexArray> boundTo = new HashSet<>();
|
private final Set<GlVertexArray> boundTo = new HashSet<>();
|
||||||
|
|
||||||
public BufferedMesh(Mesh mesh, long byteIndex) {
|
private BufferedMesh(Mesh mesh, long byteIndex) {
|
||||||
this.mesh = mesh;
|
this.mesh = mesh;
|
||||||
this.byteIndex = byteIndex;
|
this.byteIndex = byteIndex;
|
||||||
this.ebo = mesh.createEBO();
|
this.ebo = mesh.createEBO();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void buffer(long ptr) {
|
||||||
|
mesh.write(ptr + byteIndex);
|
||||||
|
|
||||||
|
boundTo.clear();
|
||||||
|
}
|
||||||
|
|
||||||
public void drawCall(GlVertexArray vao) {
|
public void drawCall(GlVertexArray vao) {
|
||||||
drawInstances(vao, 1);
|
drawInstances(vao, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drawInstances(GlVertexArray vao, int instanceCount) {
|
public void drawInstances(GlVertexArray vao, int instanceCount) {
|
||||||
if (hasAnythingToRender()) {
|
if (isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,50 +203,51 @@ public class MeshPool {
|
||||||
draw(instanceCount);
|
draw(instanceCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasAnythingToRender() {
|
private boolean isEmpty() {
|
||||||
return mesh.isEmpty() || isDeleted();
|
return mesh.isEmpty() || isDeleted();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setup(GlVertexArray vao) {
|
||||||
|
if (boundTo.add(vao)) {
|
||||||
|
vao.enableArrays(getAttributeCount());
|
||||||
|
vao.bindAttributes(InstancedMeshPool.this.vbo, 0, vertexType.getLayout(), byteIndex);
|
||||||
|
}
|
||||||
|
vao.bindElementArray(ebo.buffer);
|
||||||
|
vao.bind();
|
||||||
|
}
|
||||||
|
|
||||||
private void draw(int instanceCount) {
|
private void draw(int instanceCount) {
|
||||||
if (instanceCount > 1) {
|
if (instanceCount > 1) {
|
||||||
GL32.glDrawElementsInstanced(GlPrimitive.TRIANGLES.glEnum, this.ebo.elementCount, this.ebo.eboIndexType.getGlEnum(), 0, instanceCount);
|
GL32.glDrawElementsInstanced(GlPrimitive.TRIANGLES.glEnum, ebo.elementCount, ebo.eboIndexType.getGlEnum(), 0, instanceCount);
|
||||||
} else {
|
} else {
|
||||||
GL32.glDrawElements(GlPrimitive.TRIANGLES.glEnum, this.ebo.elementCount, this.ebo.eboIndexType.getGlEnum(), 0);
|
GL32.glDrawElements(GlPrimitive.TRIANGLES.glEnum, ebo.elementCount, ebo.eboIndexType.getGlEnum(), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setup(GlVertexArray vao) {
|
|
||||||
if (this.boundTo.add(vao)) {
|
|
||||||
vao.enableArrays(getAttributeCount());
|
|
||||||
vao.bindAttributes(MeshPool.this.vbo, 0, vertexType.getLayout(), this.byteIndex);
|
|
||||||
}
|
|
||||||
vao.bindElementArray(this.ebo.buffer);
|
|
||||||
vao.bind();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDeleted() {
|
|
||||||
return this.deleted;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void delete() {
|
public void delete() {
|
||||||
MeshPool.this.dirty = true;
|
deleted = true;
|
||||||
MeshPool.this.anyToRemove = true;
|
InstancedMeshPool.this.dirty = true;
|
||||||
this.deleted = true;
|
InstancedMeshPool.this.anyToRemove = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buffer(long ptr) {
|
public Mesh getMesh() {
|
||||||
this.mesh.write(ptr + byteIndex);
|
return mesh;
|
||||||
|
}
|
||||||
|
|
||||||
this.boundTo.clear();
|
public int size() {
|
||||||
|
return mesh.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public VertexType getVertexType() {
|
||||||
|
return vertexType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAttributeCount() {
|
public int getAttributeCount() {
|
||||||
return vertexType.getLayout().getAttributeCount();
|
return vertexType.getLayout().getAttributeCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
public VertexType getVertexType() {
|
public boolean isDeleted() {
|
||||||
return vertexType;
|
return deleted;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -23,7 +23,7 @@ public class InstancingDrawManager {
|
||||||
private final List<UninitializedModel> uninitializedModels = new ArrayList<>();
|
private final List<UninitializedModel> uninitializedModels = new ArrayList<>();
|
||||||
private final List<GPUInstancer<?>> allInstancers = new ArrayList<>();
|
private final List<GPUInstancer<?>> allInstancers = new ArrayList<>();
|
||||||
private final Map<RenderStage, DrawSet> renderLists = new EnumMap<>(RenderStage.class);
|
private final Map<RenderStage, DrawSet> renderLists = new EnumMap<>(RenderStage.class);
|
||||||
private final Map<VertexType, MeshPool> meshPools = new HashMap<>();
|
private final Map<VertexType, InstancedMeshPool> meshPools = new HashMap<>();
|
||||||
|
|
||||||
public DrawSet get(RenderStage stage) {
|
public DrawSet get(RenderStage stage) {
|
||||||
return renderLists.getOrDefault(stage, DrawSet.EMPTY);
|
return renderLists.getOrDefault(stage, DrawSet.EMPTY);
|
||||||
|
@ -49,7 +49,7 @@ public class InstancingDrawManager {
|
||||||
|
|
||||||
public void delete() {
|
public void delete() {
|
||||||
meshPools.values()
|
meshPools.values()
|
||||||
.forEach(MeshPool::delete);
|
.forEach(InstancedMeshPool::delete);
|
||||||
meshPools.clear();
|
meshPools.clear();
|
||||||
|
|
||||||
renderLists.values()
|
renderLists.values()
|
||||||
|
@ -77,8 +77,8 @@ public class InstancingDrawManager {
|
||||||
allInstancers.add(instancer);
|
allInstancers.add(instancer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MeshPool.BufferedMesh alloc(Mesh mesh) {
|
private InstancedMeshPool.BufferedMesh alloc(Mesh mesh) {
|
||||||
return meshPools.computeIfAbsent(mesh.getVertexType(), MeshPool::new)
|
return meshPools.computeIfAbsent(mesh.getVertexType(), InstancedMeshPool::new)
|
||||||
.alloc(mesh);
|
.alloc(mesh);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ public final class Materials {
|
||||||
}
|
}
|
||||||
|
|
||||||
DiffuseLightCalculator diffuseCalc = DiffuseLightCalculator.forLevel(level);
|
DiffuseLightCalculator diffuseCalc = DiffuseLightCalculator.forLevel(level);
|
||||||
for (int i = 0; i < vertexList.getVertexCount(); i++) {
|
for (int i = 0; i < vertexList.vertexCount(); i++) {
|
||||||
float diffuse = diffuseCalc.getDiffuse(vertexList.normalX(i), vertexList.normalY(i), vertexList.normalZ(i), true);
|
float diffuse = diffuseCalc.getDiffuse(vertexList.normalX(i), vertexList.normalY(i), vertexList.normalZ(i), true);
|
||||||
vertexList.r(i, (byte) Mth.clamp((int) (Byte.toUnsignedInt(vertexList.r(i)) * diffuse), 0, 255));
|
vertexList.r(i, (byte) Mth.clamp((int) (Byte.toUnsignedInt(vertexList.r(i)) * diffuse), 0, 255));
|
||||||
vertexList.g(i, (byte) Mth.clamp((int) (Byte.toUnsignedInt(vertexList.g(i)) * diffuse), 0, 255));
|
vertexList.g(i, (byte) Mth.clamp((int) (Byte.toUnsignedInt(vertexList.g(i)) * diffuse), 0, 255));
|
||||||
|
|
|
@ -35,7 +35,7 @@ public class ModelPart implements Mesh {
|
||||||
|
|
||||||
vertexList = getVertexType().createVertexList();
|
vertexList = getVertexType().createVertexList();
|
||||||
vertexList.ptr(ptr);
|
vertexList.ptr(ptr);
|
||||||
vertexList.setVertexCount(vertexCount);
|
vertexList.vertexCount(vertexCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PartBuilder builder(String name, int sizeU, int sizeV) {
|
public static PartBuilder builder(String name, int sizeU, int sizeV) {
|
||||||
|
|
|
@ -62,8 +62,8 @@ public class ModelUtil {
|
||||||
ReusableVertexList dstList = dstVertexType.createVertexList();
|
ReusableVertexList dstList = dstVertexType.createVertexList();
|
||||||
srcList.ptr(srcPtr);
|
srcList.ptr(srcPtr);
|
||||||
dstList.ptr(dstPtr);
|
dstList.ptr(dstPtr);
|
||||||
srcList.setVertexCount(vertexCount);
|
srcList.vertexCount(vertexCount);
|
||||||
dstList.setVertexCount(vertexCount);
|
dstList.vertexCount(vertexCount);
|
||||||
|
|
||||||
srcList.writeAll(dstList);
|
srcList.writeAll(dstList);
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ public class SimpleMesh implements Mesh {
|
||||||
|
|
||||||
vertexList = getVertexType().createVertexList();
|
vertexList = getVertexType().createVertexList();
|
||||||
vertexList.ptr(contents.ptr());
|
vertexList.ptr(contents.ptr());
|
||||||
vertexList.setVertexCount(vertexCount);
|
vertexList.vertexCount(vertexCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -56,7 +56,7 @@ public class OrientedType implements StructType<OrientedPart> {
|
||||||
Matrix3f normalMatrix = new Matrix3f(q);
|
Matrix3f normalMatrix = new Matrix3f(q);
|
||||||
|
|
||||||
int light = struct.getPackedLight();
|
int light = struct.getPackedLight();
|
||||||
for (int i = 0; i < vertexList.getVertexCount(); i++) {
|
for (int i = 0; i < vertexList.vertexCount(); i++) {
|
||||||
pos.set(
|
pos.set(
|
||||||
vertexList.x(i),
|
vertexList.x(i),
|
||||||
vertexList.y(i),
|
vertexList.y(i),
|
||||||
|
|
|
@ -43,7 +43,7 @@ public class TransformedType implements StructType<TransformedPart> {
|
||||||
Vector3f normal = new Vector3f();
|
Vector3f normal = new Vector3f();
|
||||||
|
|
||||||
int light = struct.getPackedLight();
|
int light = struct.getPackedLight();
|
||||||
for (int i = 0; i < vertexList.getVertexCount(); i++) {
|
for (int i = 0; i < vertexList.vertexCount(); i++) {
|
||||||
pos.set(
|
pos.set(
|
||||||
vertexList.x(i),
|
vertexList.x(i),
|
||||||
vertexList.y(i),
|
vertexList.y(i),
|
||||||
|
|
|
@ -17,12 +17,12 @@ public abstract class AbstractVertexList implements ReusableVertexList {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getVertexCount() {
|
public int vertexCount() {
|
||||||
return vertexCount;
|
return vertexCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setVertexCount(int vertexCount) {
|
public void vertexCount(int vertexCount) {
|
||||||
this.vertexCount = vertexCount;
|
this.vertexCount = vertexCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -153,7 +153,7 @@ public class BlockVertexList extends AbstractVertexList {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void shiftPtr(int vertices) {
|
public int vertexStride() {
|
||||||
ptr += vertices * STRIDE;
|
return STRIDE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -203,7 +203,7 @@ public class InferredVertexListImpl extends AbstractVertexList implements Reusab
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void shiftPtr(int vertices) {
|
public int vertexStride() {
|
||||||
ptr += vertices * stride;
|
return stride;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,7 +148,7 @@ public class PosTexNormalVertexList extends AbstractVertexList {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void shiftPtr(int vertices) {
|
public int vertexStride() {
|
||||||
ptr += vertices * STRIDE;
|
return STRIDE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue