jmonkeyengine icon indicating copy to clipboard operation
jmonkeyengine copied to clipboard

Cache and reuse VertexBuffers in MorphTarget

Open Trass3r opened this issue 1 year ago • 6 comments

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

Trass3r avatar Feb 02 '25 00:02 Trass3r

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

riccardobl avatar Jan 26 '26 12:01 riccardobl

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.

Trass3r avatar Jan 27 '26 22:01 Trass3r

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.

riccardobl avatar Jan 28 '26 09:01 riccardobl

💰 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.

bounty reservation

Note

Implementation suggestions:

  • keep a VertexBuffer for each float buffer inside morph target
  • add a method morphTarget.getVertexBuffer that 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

github-actions[bot] avatar Jan 28 '26 09:01 github-actions[bot]

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.

hunterbastian avatar May 18 '26 08:05 hunterbastian

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.

riccardobl avatar May 18 '26 09:05 riccardobl