Sign in to follow this  
freeworld

Would a UNION work for this scenerio?

Recommended Posts

I'm trying to simplify, aswell as increase the amount of data that can be passed to a D3DXEffect shader in my 2D renderer. Right now I've got a very hack and slash method of allowing only a certain amount of matricies, textures and floats be passed through, and each type is stored in it's own arrays of those type. example
struct MyShaderData
{
    float floatValues[4];
    D3DXMATRIX matrixValues[4];
    ...etc
}

But would a union work to replace that idea, I've never used a UNION before so correct me if I'm wrong.
union ShaderData
{
    float floatValue;
    D3DXMATRIX matrixValue;
    ....etc;
}

struct MyShaderData
{
    std::vector<ShaderData> values;
}

And maybe store a data type id somehow that would let me know what value to extract, and what function to use to pass it to the shader?
if (dataType == MATRIX)
    shader->SetMatrix("MyMatrix", values[index].matrixValue);

Share this post


Link to post
Share on other sites
I don't understand.

What are all the possibilities for the kind of data you can legally pass to the shader? What are the limitations, and why do the limitations exist?

You have to think about requirements - in much more detail - before you can design implementation.

Share this post


Link to post
Share on other sites
union ShaderData
{
float floatValue;
D3DXMATRIX matrixValue;
....etc;
}


Do you want to access the underlying data in some situations as floats and in other situations as whatever D3DXMATRIX is?

I understand this in C, so using that convention, here's one example of using a union.

typedef union tagPointerData
{
unsigned long dword;
unsigned long *pointer;
}
PointerData;

PointerData foo;

foo.pointer = FunctionThatReturnsPointerToULong();

use foo.dword to use the value in foo as unsigned long rather than as a pointer to unsigned long.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this