locked
Shader file loads in MDX but fails to load in XNA RRS feed

  • Question

  • The shader file I'm trying to load in is shown below.
    It works perfectly in MDX, however when I try to load it in XNA with the following lines:
                CompiledEffect compiledEffect = Effect.CompileEffectFromFile("@/../../../../test.fx", null, null, CompilerOptions.None, TargetPlatform.Windows);
                effect = new Effect(graphics.GraphicsDevice, compiledEffect.GetEffectCode(), CompilerOptions.None, null);

    I get a NullReferenceException as soon as I try to use the the effect object....

    What am i doing wrong?


    //--------------------------------------------------------------------------------------
    // File: ManagedInstancing.fx
    //
    // The effect file for the ManagedInstancing sample.  
    //
    // Copyright (c) Microsoft Corporation. All rights reserved.
    //--------------------------------------------------------------------------------------


    //--------------------------------------------------------------------------------------
    // Global variables
    //--------------------------------------------------------------------------------------
    float4x4 ViewProjection;    // View * Projection matrix

    texture Tex0;
    sampler Tex0Sampler=
    sampler_state
    {
        Texture = <Tex0>;
        
        MinFilter = Linear;
        MagFilter = Linear;
        MipFilter = Linear;    
    };


    //application to vertex structure
    struct a2v
    {
        float4 position     : POSITION0;
        float3 normal        : NORMAL;
        float2 tex0         : TEXCOORD0;    
        float4 instanceData : COLOR0;
    };
     
    //vertex to pixel shader structure
    struct v2p
    {       
        float4 position    : POSITION0;
        float2 tex0        : TEXCOORD0;
        float texIndex       : TEXCOORD1; // yeah, right
    };
     
    //pixel shader to screen
    struct p2f
    {
        float4 color    : COLOR0;
    };

    void ps( in v2p IN,  out p2f OUT)
    {    
        float2 tex0 = IN.tex0;
        tex0.x = (tex0.x + IN.texIndex) / 4; // shift u texcoord to appropriate index
        OUT.color = tex2D(Tex0Sampler, tex0);
    };

    void vs( in a2v IN, out v2p OUT )
    {       
        float4 data = IN.instanceData;    
        float4 instancePos = IN.position;    
        instancePos.x += data.x;
        instancePos.y += data.y;
        instancePos.z += data.z;    
            
        OUT.texIndex = data.w;
        OUT.position = mul(instancePos, ViewProjection);       
        OUT.tex0 = IN.tex0;
    };


    //--------------------------------------------------------------------------------------
    // Techniques
    //--------------------------------------------------------------------------------------
    technique BatchedShaderInstancing
    {
        pass P0
        {      
            VertexShader = compile vs_2_0 vs();           
            PixelShader  = compile ps_2_0 ps();          
        }
    }

    Monday, February 19, 2007 2:52 PM

Answers

  • Looking at your fx file and these parameters, I'm a bit puzzled about what the two have in common. You don't reference the xView anywhere in the effect file so how are you attempting to set it?
    Tuesday, February 20, 2007 11:10 AM

All replies

  • Run your game in debug mode and then check with line is causing the problem. Then, hover over the variables until you find one that is "null". It's more likely your xna code and not the effect file itself.
    Monday, February 19, 2007 5:06 PM
  • You need to check the CompiledEffect structure returned by Effect.CompileEffectFromFile to see if there were any compile errors, and if so, print out the list of errors so you can see what went wrong. As it stands your code will just carry on even if the compiler failed, but isn't giving you any way to see why it failed.

    btw. if you just add your .fx file to the C# Express project, the content pipeline will take care of compiling the effect for you, reporting any errors in the same way as for regular C# compilation.
    Monday, February 19, 2007 5:57 PM
  • I added by .fx file using the content pipeline as followed:
    effect = content.Load<Effect>("Content\\Shaders\\test");

    and it compiles and loads no problem.

    The problem occurs here: on the line with the arrows, and it says nullreferenceexception.
    I checked the effect object and it seems to have the correct number of Techinques/passes loaded into its members... any ideas what the problem is?

    private void InitializeEffect()
    {
    vertexDeclaration = new VertexDeclaration(
    graphics.GraphicsDevice,
    VertexPositionNormalTexture.VertexElements);

    -> effect.Parameters["xView"].SetValue(Matrix.Identity); <- NullReferenceException
    effect.Parameters["xProjection"].SetValue(projectionMatrix);
    effect.Parameters["xWorld"].SetValue(Matrix.Identity);
    }
    Tuesday, February 20, 2007 9:43 AM
  • Looking at your fx file and these parameters, I'm a bit puzzled about what the two have in common. You don't reference the xView anywhere in the effect file so how are you attempting to set it?
    Tuesday, February 20, 2007 11:10 AM
  • Ahhh, my lack of shader knowledge is quite apparent, I never actually realised that 'variables' in the shader files were actually accessed from effect.Parameters, until this point i just thought of shaders as little magic files that u could just lobtain from someone else who wrote it and load in and just use whatever effects u wanted...

    I see there is now no way around it, I will have to learn HLSL
    Tuesday, February 20, 2007 1:01 PM