Mesh Instancing + Transform Feedback

I would like to use transform feedback’s output as an input for the getModelMatrix() in the vertex shader of an instanced mesh. Considering the first one lives in a shader material, what would be my option?

cc @mvaligursky @slimbuck

  • transform feedback outputs to a VertexBuffer, which should be TransformFeedback.outputBuffer
  • instancing uses VertexBuffer as a per instance input.

So you just need to connect those two? Note - I have not tested this.

Yes, I tried that, but the mesh doesn’t get drawn, unfortunately. No errors, though. So, I thought I might have to do something with the tf buffer, before giving it to instancing.

Edit:
Nevermind. It is rendering, the mesh scale was simply super small.

Edit2:
Yeah, no. Have to continue digging. Drawing points here.

Hmm, all instanced meshes keep drawing at [0, 0, 0], ignoring the tf output buffer data (which is filled correctly). Do I need to map pc.SEMANTIC_ATTR12 on tf output buffer for the shader to read it as instanced position?

Edit: nevermind, I already set the semantic there, must be something else.

Trying to combine “Instancing Custom” and “Transform Feedback” engine examples into one. No luck yet.

(project requires engine 2.0)
https://playcanvas.com/project/1253650/overview/tfinstancing

Try setting up a fixed vertex buffer with that you’d expect from FF, and get instancing to work with this first (if you have not already). Then replace it by the output from FF.

Hmm, right, but I tried the other way around. My line of thought is that I would need to create a vertex buffer, that would be suitable for instancing. That is that it would have correct format and semantic elements. In this case I need ATTR12 for instanced position.

I think I did that correctly, but the result was nigh. I think the problem is that I I have to use a mesh for TF, which would have the same vertex buffer format that would be used by instancing? But then probably rendering fails, as it doesn’t have ATTR0 position semantic? Not sure. I was able to move the points, or spread around the sphere meshes. But couldn’t make spheres to move as points.

I have a suspicion. VertexFormat has instancing flag, which we set VB as instancing VB on the MeshInstance. We do vertexBuffer.format.instancing = true;
When we then assign the vertex buffer for rendering, we mark it as advance per instance instad of advance per vertex:

if (vertexBuffer.format.instancing)
    gl.vertexAttribDivisor(loc, 1);

But I suspect this is not something TF expects, as that needs to be per vertex.

I don’t see an easy way to support both on a VertexBuffer, not without changing the engine. Maybe for testing, you could allocate vertex buffer the same way TF does internally, but point it’s data to the actual output buffer. So that they share the data, but use different VertexFormat instance.