DrawUserPrimitives

Can someone explain why this method uses generics

graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, 12, nonIndexedCube);

I am a VB.NET business programmer and I am familiar with generics, but pretty much only use the generic collections. This seems silly to me. With generic collections, I define a collection to be of a certain type, and then where I use it, it is typed, keeping me from doing something stupid and saving me some extra coding. Since the 'definition', <VertexPositionColor>, is right next to the usage, actually in the same method call, I don't see that being the case. What does this give me or is it a performance thing



Answer this question

DrawUserPrimitives

  • ChrisErven

    Thanks. I figured it had to be performance. Mostly it is just looks strange, but once I realized what it was doing I just wasn't sure why.
  • vgvKarthik

    It protect you a little bit from doing silly things because there is a where constraint that limited you to value types (VB: Of T As ValueType). If you would use a reference type there very strange things would happen.

    The second part is a performance optimization. The under laying Direct3D need to know the size of one vertex. If you don’t use generics here this size need to be calculated every time you call the function. With the generic it is calculated only once during the JIT compilation.



  • Handra

    If you dissassemble code for this method, you can see that they do perform a "sizeof(VertexType)" call to determine the vertex stride before they call into their unmanaged code. This is a minor performance improvement over what would otherwise be a dictionary lookup, but not really significant. It's mostly done because generics are new and cool, and the developers want to use them. Seriously.

    Generics are very cool, but hopefully they provide non-generic draw methods as well (I know, the devs hate to add more overloads to their clean and simple api, but they are needed.)

    Not all .Net languages will support generics, and the non-generic methods are alot more flexible. You're final version of XNA should be language agnostic.

  • DrawUserPrimitives