Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't enlarge vertex buffer #5878

Closed
thisredone opened this issue Dec 4, 2023 · 5 comments
Closed

Can't enlarge vertex buffer #5878

thisredone opened this issue Dec 4, 2023 · 5 comments

Comments

@thisredone
Copy link

after #5758 can't enlarge vertex buffers

image

bufferSubData throws when calling vertexBuffer.setData with bigger buffer. This breaks for example instancing

@mvaligursky
Copy link
Contributor

The API was not designed to allow you to resize a VB this way, you need to re-create the VertexBuffer. You can always also create it larger and use only portion of it.
What you do worked by accident, and won't work on WebGPU for example.

@slimbuck
Copy link
Member

slimbuck commented Dec 4, 2023

@leonidaspir
Copy link
Contributor

leonidaspir commented Dec 5, 2023

So if we are using instancing with e.g. a custom LOD and culling system I imagine the only way to use a dynamic buffer is by doing this per frame right, if the buffer has grown (works in v1.67.3):

    const vertexBuffer = new pc.VertexBuffer(
        this.app.graphicsDevice,
        pc.VertexFormat.defaultInstancingFormat,
        instancesCount,
        pc.BUFFER_STATIC,
        bufferArray
    );
    payload.meshInstance.setInstancing(vertexBuffer);

But if the buffer is smaller on a given frame and we try to use setData() with a smaller buffer then it fails with VertexBuffer: wrong initial data size: expected 2176, got 0:

        vertexBuffer.setData(bufferArray);

        payload.meshInstance.instancingData.count = instancesCount;
        vertexBuffer.numVertices = instancesCount;

This last bit worked with the previous version. Any other way to reuse a vertex buffer and avoid unnecessary garbage collection?

@mvaligursky
Copy link
Contributor

the setData used to error for a long time now .. how were you using it without getting the errors?
Screenshot 2023-12-05 at 12 48 45

I think what you should do is to call vb.Lock();, update partial data as needed, and then vb.unlock();
The number of instances for instancing can be then controlled using MeshInstance.instancingCount = 10;

@thisredone
Copy link
Author

I was always updating buffer.byteLength when updating the contents so the Debug.error never came up ;-)

@thisredone thisredone closed this as not planned Won't fix, can't repro, duplicate, stale Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants