No need for getDirtyBitSet

- Faster and simpler to just do one loop
This commit is contained in:
Jozufozu 2021-12-17 22:41:22 -08:00
parent ffe17e4449
commit a43fe2bc9a
2 changed files with 11 additions and 35 deletions

View file

@ -70,19 +70,6 @@ public abstract class AbstractInstancer<D extends InstanceData> 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();

View file

@ -150,31 +150,20 @@ public class GPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
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<D> 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<D> 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);
}
}