Sign in to follow this  
Mathy

[SlimDX] I want to parse more data to my hardware instancing shader!

Recommended Posts

Hi there. I recently made a hardware instancing shader. I know quite a lot about how to transfer data to this shader, but I want to transfer more data to it. For every instance, other than a transformation being passed through a Matrix, I want a float. Depending on that float's value, I want the shader to change the color of the whole instance to another color. Below you see my vertex declaration.
        private static VertexElement[] VertexElements =
             {
                 new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                 new VertexElement(0, sizeof(float)*3, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0),
                 new VertexElement(0, sizeof(float)*6, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
                 new VertexElement(1, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 1),
                 new VertexElement(1, 16, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 2),
                 new VertexElement(1, 32, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 3),
                 new VertexElement(1, 48, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 4),
                 new VertexElement(1, 64, DeclarationType.Float1, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
                 VertexElement.VertexDeclarationEnd
             };
Now, this vertex declaration is able to transfer PositionNormalColored vertices, and then on stream 2, it is compatible with a Matrix object. I added this line to make it compatible with the float that I want transferred. new VertexElement(1, 64, DeclarationType.Float1, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0) I am not sure if that is the correct way of doing it, I just assumed so. In my actual shader, the code looks like this:
float4x4 View;
float4x4 Projection;

float3 LightDirection = normalize(float3(-3, -1, -1));
float3 DiffuseLight = 0.2;
float3 AmbientDarkness = 0;

struct computerShellInput
{
    float4 position : POSITION0;
    float4 color : COLOR0;
    float3 normal : NORMAL0;
};

struct instanceData
{
	float4x4 transform : TEXCOORD1;
	float state : TEXCOORD1;
};

struct VertexShaderOutput
{
    float4 position : POSITION0;
    float4 color : COLOR0;
};

VertexShaderOutput doComputerShellInstancing(computerShellInput input, float4x4 instanceTransform, float state)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.position, instanceTransform);
    float4 viewPosition = mul(worldPosition, View);
    
    output.position = mul(viewPosition, Projection);
    
	if(state==3) {
		output.color=float4(0,0,1,1);
	} else {
	        output.color=float4(1,1,1,1);
	}

    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    return input.color;
}

VertexShaderOutput computerShellInstancing(computerShellInput input,
                                                instanceData iInput)
{
    return doComputerShellInstancing(input, transpose(iInput.transform), iInput.state);
}

technique computerShellInstancingTechnique
{
    pass Pass1
    {
        VertexShader = compile vs_3_0 computerShellInstancing();
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}
I built the instanceData structure to make it compatible with a Matrix as well as a float. I'm not sure if that's correct either. Can anyone help me get this working? Edit: I should mention that the transforms are being transferred into the instance stream correctly. Only the state seems to always be the same.

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