I am working on my own batching algorithm so I can hopefully render 256 some items at a decent framerate. I used old DirectX back in the day, so I am very sketchy on effects. What is the necessary order of calls?
For example, do I have to set matrices before calling Effect.Begin()? Do I have to set the graphicsDevice.Vertices[0] after a pass.Begin()?
This is roughly what I'm doing right now, and it seems to work in most cases... but it seems like it's sometimes "skipping" a model when rending many--like they are being drawn on top of eachother maybe. I have had multiple versions of this, directly borrowing from examples and more. This is what I have at the moment after much hacking--it's probably pretty out of order now.
| 1 |
//go through each mesh in the model |
| 2 |
foreach (ModelMesh mesh in model.Meshes) |
| 3 |
{ |
| 4 |
//set the index buffer once per mesh |
| 5 |
graphicsDevice.Indices = mesh.IndexBuffer; |
| 6 |
|
| 7 |
|
| 8 |
//each mesh is made of parts (grouped by texture, etc) |
| 9 |
foreach (ModelMeshPart part in mesh.MeshParts) |
| 10 |
{ |
| 11 |
Effect partEffect = (BasicEffect)part.Effect; |
| 12 |
|
| 13 |
//assign matrices |
| 14 |
partEffect.Projection = camera.ProjectionMatrix; |
| 15 |
partEffect.View = camera.ViewMatrix; |
| 16 |
|
| 17 |
//change device settings for each part to be rendered |
| 18 |
graphicsDevice.VertexDeclaration = part.VertexDeclaration; |
| 19 |
graphicsDevice.Vertices[0].SetSource(mesh.VertexBuffer, part.StreamOffset, part.VertexStride); |
| 20 |
|
| 21 |
//set texture for this part |
| 22 |
graphicsDevice.Textures[0] = partEffect.Texture; |
| 23 |
|
| 24 |
partEffect.World = worldMatrix; |
| 25 |
|
| 26 |
partEffect.Begin(SaveStateMode.None); |
| 27 |
|
| 28 |
foreach (EffectPass pass in partEffect.CurrentTechnique.Passes) |
| 29 |
{ |
| 30 |
pass.Begin(); |
| 31 |
|
| 32 |
//finally draw the actual triangles |
| 33 |
graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.BaseVertex, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount); |
| 34 |
|
| 35 |
pass.End(); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
partEffect.End(); |
| 40 |
|
| 41 |
} |
| 42 |
} |
Note: this is part of a much larger batching algorithm. I cut out any batching stuff.
Cheers.