开发者

Is an array valid for vertex element data for programmable graphics pipline?

In response to my previous qeustion posted here:

What is a good code structure for api-independant vertex processing?

If I have say a vertex structure 开发者_如何学JAVAwith 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?


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.


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜