XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Extending the Instancing Sample... Having Issues

Last post 10/7/2007 5:35 PM by Matthew Castellana. 3 replies.
  • 10/7/2007 2:54 AM

    Extending the Instancing Sample... Having Issues

    Hey everyone, Im having issues with extending the instancing sample and need a little guidence. This is something Im sure Shawn or the XNA Team can answer. Im trying to pass an array of colors into the shader to assign to depending on the instance index. Sounds easy? Thats what I thought but for some reason am having a problem with variables. Ive been programming shaders for about 2 years off and on so Im still learning. As with the sample, I am also passing an array of colors for the instances...

                    // In The Draw Instancing Function (Shader/VFetch) Instancing

    ...

    Array.Copy(instanceTransforms, i, tempMatrices, 0, instanceCount);
    Array.Copy(instanceColors, i, tempColors, 0, instanceCount);

    int index = 0;
    foreach (Color color in tempColors)
    {
    colorArray[index] = color.ToVector4();
    index++;
    }

    effect.Parameters["InstanceTransforms"].SetValue(tempMatrices);
    effect.Parameters["InstanceColors"].SetValue(colorArray);
    effect.CommitChanges();

    ...

    Ive debugged and the colorArray is holding the correct values, so I know its not empty... I set it into the instancing effect and figured I should be fine to use the colors as I pleased. The array is defined to only hold 60 different Colors just like the Matrix array. Within the shader, for Shader and VFetch, it calculates or gets the index of the current transform in the array. I assumed I could use the variable instanceIndex within the array to get the current Color value from the InstanceColors[...] array I created inside the shader... My problem is that when I try to multiply the output color by the current color... I see nothing. So I know that Im not gettin a value and dont understand why? My shader code is below...

    ...

    #define MAX_SHADER_MATRICES 60


    // Array of instance transforms used by the VFetch and ShaderInstancing techniques.
    float4x4 InstanceTransforms[MAX_SHADER_MATRICES];

    // Single instance transform used by the NoInstancing technique.
    float4x4 NoInstancingTransform;

    // MATT: Adding Color Array
    float4 InstanceColors[MAX_SHADER_MATRICES];

    // Camera settings.
    float4x4 View;
    float4x4 Projection;

    ...
    ...

    VertexShaderOutput ShaderInstancingVertexShader(VertexShaderInput input,
    float instanceIndex : TEXCOORD1)
    {
    return VertexShaderColor(input, InstanceTransforms[instanceIndex],
    InstanceColors[instanceIndex]);
    //return VertexShaderCommon(input, InstanceTransforms[instanceIndex]);
    }

    ...
    ...

    VertexShaderOutput VertexShaderColor(VertexShaderInput input,
    float4x4 instanceTransform,
    float4 CurrentColor)
    {
    VertexShaderOutput output;

    // Apply the world and camera matrices to compute the output position.
    float4 worldPosition = mul(input.Position, instanceTransform);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);

    // Compute lighting, using a simple Lambert model.
    float3 worldNormal = mul(input.Normal, instanceTransform);

    float diffuseAmount = max(-dot(worldNormal, LightDirection), 0);

    float3 lightingResult = saturate(diffuseAmount * DiffuseLight + AmbientLight);

    output.Color = float4(lightingResult, 1);
    //CurrentColor.a = 1.0f;
    //CurrentColor.rgb = 1.0f;
    //CurrentColor = float4(0, 0, 0, 1);

    output.Color = output.Color * CurrentColor;

    // Copy across the input texture coordinate.
    output.TextureCoordinate = input.TextureCoordinate;

    return output;
    }

    ...
    This is a simple mod to the shader but I dont know why I cant get this right. Maybe I still dont understand accessability 100% within shaders... Anyway, if someone could help out, it would be very appreciated... hope the community is doing well!!!
  • 10/7/2007 10:37 AM In reply to

    Re: Extending the Instancing Sample... Having Issues

    You see nothing, as in nothing renders, or you see nothing, as in you don't see any change of colour? The basic idea of what you've shown sounds ok (I'm not sure if another 60 float4 constants fits into the max number of constant registers but otherwise good).

    Just a silly thought, you're presumably only changing the shader instancing technique so you need to switch the demo to use that technique, rather than the default (on shader 3 cards) of hardware instancing.

    More detail is needed before I could tell you whats wrong.

    Cheers,
    Leaf.

  • 10/7/2007 2:37 PM In reply to

    Re: Extending the Instancing Sample... Having Issues

    Your code changes look fine to me (as long as you using the Shader Instancing technique and not Hardware Instancing).

    I suspect the problem is you are overflowing the 256 shader constants. We chose the MAX_INSTANCES value of 60 to be as high as would fit into 256 constants while leaving room for the other effect parameters that also need constant registers (remember a matrix occupies 4 float4 constant registers). You will need to reduce this limit to make room for your new parameters.
    XNA Framework Developer - blog - homepage
  • 10/7/2007 5:35 PM In reply to

    Re: Extending the Instancing Sample... Having Issues

    Hey guyz, thanks for the quick response. Ok, JunkMailer, as a reply to you post. When I say I mean nothing, the model isnt visible. If I comment out that field ex. ) color = output.Color; // * CurrentColor; ... it renders White and is visible. The second I say multiply by CurrentColor, it doesnt render or its rendering but the color its coming up with is an empty value. As far as the constant registers, your probably right. Also, Ive actually modified the code from the sample to work within my game and have set the technique for Shader for Windows or VFetch on XBOX and only those. Hope that helps buddy and thanks again!

    To Shawn: Like I mentioned, Im still understanding certain things about Shader Programming and dont know too much about shader constants. Just tried a little search to see if I could find out more about them, not too much luck but found a little info on nVidia's site. So, just for understanding, Shader Model 2.0 has 256 max constant registers. Is a constant register a variable or does it account for variables and the lines of code that make up a function? You said Shawn that a Matrix actually accounts for 4 float4 constant registers, which by the array size would equal 240 constant registers, correct? Ok, so in order to make room for the color array and any additional features (per-pixel lighting, normal mapping, etc.), I need to reduce the MAX_SHADER_MATRICES? Would making the Max number 40 instead of 60 work (40 * 4(float4) + 40 * (float4) = 200 constant registers? One more quick question, what are Instruction Slots and Max Number Of Instructions Executed? I have an idea but I just wanna clarify to be sure. Thanks a lot for responding, and really looking forward to hearing back from you... In the meantime, I will go back and study up more on Shader Model limitations and Shader programming... take care guyz!!!

    UPDATE: Hey Shawn, how would you do it (Shader, VFetch)? Also, you mentioned with Hardware, the way would be to create an extra data channel on the secondary vertex buffer... how would you do that, Im a little unsure? One last question... lets say I have a vertex and pixel shader that can compile in 2.0, if I compile it in 3.0, will I see any performance differences or are there any that take place. Hey thanks again and hope all is well!

Page 1 of 1 (4 items) Previous Next