From a43fe2bc9a1ae6a140b9aec8b8e159d620858198 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Fri, 17 Dec 2021 22:41:22 -0800 Subject: [PATCH] No need for getDirtyBitSet - Faster and simpler to just do one loop --- .../backend/instancing/AbstractInstancer.java | 13 -------- .../instancing/instancing/GPUInstancer.java | 33 +++++++------------ 2 files changed, 11 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java index 06e6071fd..af7005abb 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java @@ -70,19 +70,6 @@ public abstract class AbstractInstancer implements Insta return getModelVertexCount() * numInstances(); } - protected BitSet getDirtyBitSet() { - final int size = data.size(); - final BitSet dirtySet = new BitSet(size); - - for (int i = 0; i < size; i++) { - D element = data.get(i); - if (element.checkDirtyAndClear()) { - dirtySet.set(i); - } - } - return dirtySet; - } - protected void removeDeletedInstances() { // Figure out which elements are to be removed. final int oldSize = this.data.size(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java index 879f35785..fc1480ebf 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java @@ -150,31 +150,20 @@ public class GPUInstancer extends AbstractInstancer { if (size <= 0) return; - final int stride = instanceFormat.getStride(); - final BitSet dirtySet = getDirtyBitSet(); + try (MappedBuffer mapped = instanceVBO.getBuffer(0, glBufferSize)) { - if (dirtySet.isEmpty()) return; + final StructWriter writer = type.asInstanced() + .getWriter(mapped); - final int firstDirty = dirtySet.nextSetBit(0); - final int lastDirty = dirtySet.previousSetBit(size); - - final int offset = firstDirty * stride; - final int length = (1 + lastDirty - firstDirty) * stride; - - if (length > 0) { - try (MappedBuffer mapped = instanceVBO.getBuffer(offset, length)) { - - StructWriter writer = type.asInstanced() - .getWriter(mapped); - - dirtySet.stream() - .forEach(i -> { - writer.seek(i); - writer.write(data.get(i)); - }); - } catch (Exception e) { - Flywheel.log.error("Error updating GPUInstancer:", e); + for (int i = 0; i < size; i++) { + final D element = data.get(i); + if (element.checkDirtyAndClear()) { + writer.seek(i); + writer.write(element); + } } + } catch (Exception e) { + Flywheel.log.error("Error updating GPUInstancer:", e); } }