Home » Community » Forums » » Designing a Flexible Vertex Element System for XNA Using Attributes
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Designing a Flexible Vertex Element System for XNA Using Attributes
Post Reply 
Wow, I don't think this could have been more timely if you'd have been shoulder-surfing me this morning!

I was *just* converting some custom vertex formats while in the process of cleaning up someone else's code which had accumulated a bunch of cruft, and was actually kind of thinking of whether there were some more unified and consistent way of doing this!

I'll be trying this out today, thanks for the article!

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Nice article Mikey. The whole thing is a brilliant idea, I wish I had thought of it . I think I'm going to experiment with this system.


Oh internet... so many idiots... so few bullets.

Linky goodness: [journal] | [Gorgon]


 User Rating: 1436   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

I developed the exact same thing earlier in the year for an OpenGL engine I have been working on. I created a VertexBuffer<> class that handles the VBOs transparently. I am glad to see the implementation has enough merit to be a feature.





 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Great article!

I'd change the signature of "CreateVertexDeclaration" to public static VertexDeclaration CreateVertexDeclaration<T>(this GraphicsDevice device) where T : struct

and also I'd create a static dictionary to hold the vertex element format: static Dictionary<Type, VertexElementFormat> "cachedFormats" so that you can then replace all the "if, else if" statements as follows:

if (attributes[0].Format == VertexElementFormat.Unused)
attributes[0].Format = cachedFormats[field.FieldType];

~Pete
http://amapplease.blogspot.com



 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Your first suggestion is mostly just a stylistic choice, but the second one of using a map is good. The type detection as a whole was basically thrown in at the end, I haven't really made use of it very much.


Mike Popoloski | Journal | SlimDX

 User Rating: 1791   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

Exactly.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Have you done any performance tests to see what effect the use of reflection has?

 User Rating: 1088   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by BLiTZWiNG
Have you done any performance tests to see what effect the use of reflection has?


This code would not be called every frame - in fact, it's probably only called when you load your level/start the game - so performance is not an issue.

 User Rating: 1645   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Yes, the code is only used when your vertex declaration needs to be recreated, which should not be every frame. Even so, the reflected data is cached in a dictionary, so the reflection itself only occurs for the first creation of a particular vertex declaration; after that, recreating the declaration is a simple method call.


Mike Popoloski | Journal | SlimDX

 User Rating: 1791   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

Marshal.OffsetOf doesn't exist on the Xbox 360. Any suggested work-around?

 User Rating: 1000   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

#1. For completeness sake, some using statements are called for:
using System.Reflection;
using System.Runtime.InteropServices;

#2. Most built-in vertices have an int SizeInBytes property. I can get that value for a custom vertex as well:

int vertexSize = Marshal.SizeOf(vertexType);

Can this property be added dynamically as well or am I stuck with the old "count the elements and their sizes" routine?



 User Rating: 1000   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: