Cache and reuse VertexBuffers in MorphTarget
When the set of active morph targets changes often it keeps reuploading data to the GPU due to:
https://github.com/jMonkeyEngine/jmonkeyengine/blob/8ba6574ef6c2cba463e2f03a87263c4074466e10/jme3-core/src/main/java/com/jme3/anim/MorphControl.java#L313
Because MorphTarget holds FloatBuffers instead of VertexBuffers that could simply be bound instead of overwritten:
https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-core/src/main/java/com/jme3/scene/mesh/MorphTarget.java#L13
I think this was fixed at some point, because VertexBuffer.updateData currently does
public void updateData(Buffer data) {
if (id != -1) {
// request to update data is okay
}
// Check if the data buffer is read-only which is a sign
// of a bug on the part of the caller
if (data != null && data.isReadOnly()) {
throw new IllegalArgumentException("VertexBuffer data cannot be read-only.");
}
// will force renderer to call glBufferData again
if (data != null && (this.data.getClass() != data.getClass() || data.limit() != lastLimit)) {
dataSizeChanged = true;
lastLimit = data.limit();
}
this.data = data;
setUpdateNeeded();
}
-> this.data = data;
so the condition vb.getData() != b here
private void activateBuffer(Mesh mesh, int idx, int start, FloatBuffer b) {
VertexBuffer.Type t = bufferTypes[start + idx];
VertexBuffer vb = mesh.getBuffer(t);
// only set the buffer if it's different
if (vb == null || vb.getData() != b) {
mesh.setBuffer(t, 3, b);
}
}
will skip sub-sequential updates
The use-case was old-school vertex animations where the data does change every frame. But anyways I think MorphTarget should hold VertexBuffers and just bind them if possible.
Ah, i see what you mean now.
You want to keep the VertexBuffers around and just rebind them as needed instead of re-uploading the content in-place every time the morph target changes.
💰 This issue has a bounty
Resolve it to receive a reward. For details (amount, rules, eligibility), see: https://hub.jmonkeyengine.org/t/bounty-program-trial-starts-today/49394/
If you want to start working on this, comment on this issue with your intent. If accepted by a maintainer, the issue will be assigned to you.
Note
Implementation suggestions:
- keep a VertexBuffer for each float buffer inside morph target
- add a method
morphTarget.getVertexBufferthat creates, caches and returns the VertexBuffers - change the activateBuffer method in MorphControl to use this new getVertexBuffer by calling mesh.setBuffer(VertexBuffer)
- test nothing broke
Optional: Gate the new behavior behind a configurable boolean flag that is off by default
I would like to work on this issue. I have a local patch following the suggested approach: cache VertexBuffers in MorphTarget, bind the cached buffers from MorphControl, and add focused MorphTarget tests. Local focused verification passes with ./gradlew :jme3-core:test --tests com.jme3.scene.mesh.MorphTargetTest --no-daemon. I will wait for maintainer assignment before opening a PR.
Hello! We are trying to avoid vibecoded submissions, for this reason we have some requirements to participate, you can check them here.
It is not obvious to me that your account matches these requirements.