Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!

Use Hardware Instancing for Model and World Instances

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 PhillipHamlyn   Members   -  Reputation: 459


Posted 28 June 2013 - 02:23 AM



I have a tree model with multiple instances of the same geometry for the leaves. I have implemented hardware instancing to generate all my leaves in a single call, but I now want to multiply this by creating multiple trees in that same call.


Since VertexBufferBinding takes an array I thought I could do this;


// Set up the model instance buffer

VertexBuffer modelInstanceVertexBuffer = new VertexBuffer(graphicsDevice, SurfaceFeatureModelInstanceVertex.VertexDeclaration, modelInstanceToRender.Count, BufferUsage.None);

modelInstanceVertexBuffer.SetData(modelInstanceToRender.ToArray(), 0, modelInstanceToRender.Count);


// And the world instance buffer

VertexBuffer worldInstanceVertexBuffer = new VertexBuffer(graphicsDevice, SurfaceFeatureWorldInstanceVertex.VertexDeclaration, worldInstanceToRender.Count, BufferUsage.None);

worldInstanceVertexBuffer.SetData(worldInstanceToRender.ToArray(), 0, worldInstanceToRender.Count);


VertexBufferBinding[] bindings = new VertexBufferBinding[3];

bindings[0] = new VertexBufferBinding(renderInstance.SurfaceGeometry.VertexBuffer,0,0);

bindings[1] = new VertexBufferBinding(modelInstanceVertexBuffer, 0, 1);

bindings[2] = new VertexBufferBinding(worldInstanceVertexBuffer, 0, 1);



However I'm not getting the results I expect (i.e. nothing is getting rendered to my viewpoint). There aren't any examples that I could find of multiple VertexBufferBinding being used to generate multiplier effects like this. I am suspecting that "it just doesn't work that way" and instancing can only be used once per call rather than combining and multiplying like I am trying to do. Could it be as simple as me not understanding the meaning of the instanceFrequency parameter ? for instance, would it be correct to say modelInstanceVertexBuffer has a count of 414 (leaves) and worldInstanceBuffer has a count of 400 (trees) so the instanceFrequency for modelInstanceVertexBuffer is "1" (i.e. render one SurfaceGeometry for each item in the modelInstanceVertexBuffer), but when it comes to the 400 worldInstanceVertexBuffer I would pass 414*400 as the instanceFrequency (this doesn't make sense to me).


Can any one help ?




#2 phil_t   Crossbones+   -  Reputation: 5577


Posted 28 June 2013 - 05:49 AM

I'm not sure if there's a way to do what you're trying to do, but.... is there any reason you don't just stuff all the leaves in the model itself? Do they really need to be instanced?

#3 PhillipHamlyn   Members   -  Reputation: 459


Posted 28 June 2013 - 06:39 AM



I do have the option of baking in the model transforms but it just seems a bit of a waste - each model fragment has position, normal, binormal, color, texture coord, etc. etc. and I'm converting them from a Collada file, so they are already presented to me as "mesh" and "mesh instance transform" for each instance of the mesh. In a tree there are only perhaps 5 or six actual unique meshes, but many repetitions of those basic structures. Seemed to me that was the target use-case for hardware instancing.


I can manually multiply up the instance vertex buffer manually to include two matrix transforms, the model then the world, based on the number of world items I need, but this will create a reasonable amount of memory allocation and deallocation each frame (and not to mention a spectacularly large vertex buffer), so I had hoped to be able to pass a second Instancing vertex buffer which represented the world transform for each individual tree. since setting "1" on the frequency parameter of the binding multiplies all the non-instanced vertex buffers by the quantity in the instanced buffer, I was hoping this was a recursive concept - i.e. buffer 0 (frequncy 0, 400 vertexes), buffer 1 (frequency 1, 500 vertexes), buffer 2 (frequency 1, 600 vertexes) would give me a render call of 400*500*600 vertexes. I had read the frequency parameter to mean "repeat all un-instanced vertex buffers once for each item in this vertex buffer". I have no real concept of why you would pass more than the value of 1 in that parameter, as it would simply draw the same vertexes repeatedly; like caling the same DrawPrims twice in succession - so it seems to me there is more at work here .... ?



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.