tags:

views:

52

answers:

2

In response to my previous qeustion posted here:

http://stackoverflow.com/questions/2189494/what-is-a-good-code-structure-for-api-independant-vertex-processing

If I have say a vertex structure with an array of floating point values such as:

public struct Vertex
{
    float[] array = new float[4];

    public Vertex(float x, float, y, float z, float w)
    { 
         array[0] = x; array[1] = y; array[2] = z; array[3] = w;
    }
}

Could that vertex structure be sent to a vertex buffer (using OpenGL or DirectX)? Does an array contain any other byte data or does it only contain the data of the floating point values? Like if I were (in DirectX) use this vertex as Transformed in a declaration could I send the vertex to a buffer and all that will be sent is the floating point values, or will the array contain other information that could break my shader program?

A: 

I'm not a C# programmer, but it looks here that you store a array reference (pointer) in the Vertex struct, hence an array of Vertex would be an array of pointers. A graphics API demands a continous sequence of floats.

Try:

public struct Vertex
{
    public fixed float array[4];

    public Vertex(float x, float, y, float z, float w)
    { 
         array[0] = x; array[1] = y; array[2] = z; array[3] = w;
    }
}
Kornel Kisielewicz
I guess your right :/ , but what if the contents of the array are all struct, ie. ValueType ?
qc.zackf-FissureStudios
@qc - honestly, I'm not a C# programmer :>
Kornel Kisielewicz
Goch'ya ;), sorry.. just really eager to figure it out :(
qc.zackf-FissureStudios
@qf, in the worst case, have a wrapper class around a big fixed array of float. The wrapper class would handle data interpretation, while the array of floats would be formatted according to the desired vertex stream (3v2t3c or whatever).
Kornel Kisielewicz
I forgot about the fixed keyword as I haven't seen much use for it, so thanks.
qc.zackf-FissureStudios
A: 

No, don't do it. the structure may not be correctly aligned or have extra data...its generally not a safe thing to do considering that the .net framework may change and those are not guaranteed feature.

Your best bet is to prepare the data into float array[] and pick up the pointer using the fixed keyword. in that way I can ASSURE (i'm doing it by myself) that works fine. Anyway don't try to do extra things as optimizing on these petty things. They will never be the bottleneck in your app/game. (not even really important)

Do what makes your code more readable-maintanable.

Remember anyway that the speed of struct vs array is different depending on what are you doing. Generally struct are faster on random access, array are faster on linear access, so if you have some REALLY intensive part of application (like a software skinner is) try to choose the correct for your need.

feal87
Well, it's not any issue of speed, rather than if it is possible. I am trying to figure out the best way to structure elements of a vertex structure, but in a way that allows my users to create custom vertices without me knowing how the data is arranged. (ie. abstract vertex processing). This question is just part of many that I have on what is the best approach to this. No matter what approach I use, I will make sure it's optimal in performance (by any means necc.).
qc.zackf-FissureStudios