[Solved] Null Reference Exception when calling Apply() on custom effect

Hi, I’m very new to shader programming in Monogame and after having spent severals hours now trying to find the issue in my code, I’ve decided to open this topic because I just can’t find the problem.

After trying out the basic effect, which worked just fine, I tried writing a custom effect, but I get a null reference error when calling apply on the pass, although the pass itself is not null in the debug inspector. I’ve noticed that, for some reason, the effect of the pass (_testEffect) has the property “isDisposed” set to true (I never call dispose on it). Could this be the problem? Any help would be greatly appreciated.

Game1 code: Pastebin.com
fx code (pretty much simplified to do nothing, after many iterations of trying to find the problem):
Pastebin.com

Edit: I’ve changed my vertex data type to VertexPosition now, and adapted the vertex shader to only take a position as parameter to try and really keep the shader as basic as possible. This is the shader code now:

#if OPENGL
   #define SV_POSITION POSITION
   #define VS_SHADERMODEL vs_3_0
   #define PS_SHADERMODEL ps_3_0
#else
   #define VS_SHADERMODEL vs_4_0_level_9_1
   #define PS_SHADERMODEL ps_4_0_level_9_1
#endif

float4x4 WorldViewProjection;

struct VertexShaderInput
{
   float4 Position : POSITION;
};

struct VertexShaderOutput
{
   float4 Position : POSITION;
};

VertexShaderOutput MainVS(VertexShaderInput input)
{
   VertexShaderOutput output = (VertexShaderOutput)0;
   output.Position = mul(input.Position, WorldViewProjection);
   return output;
}

float4 MainPS(VertexShaderOutput input) : COLOR0
{
   float4 red = {1.0f, 0.0f, 0.0f, 1.0f};
   return red;
}

technique BasicColorDrawing
{
   pass P0
   {
       VertexShader = compile VS_SHADERMODEL MainVS();
       PixelShader = compile PS_SHADERMODEL MainPS();
   }
};

And then all I’m doing with the effect in the game code is basically loading it with the content manager and then in the draw method looping through its passes and applying them. Still no dice though :frowning:

Effect.IsDisposed should not be true, also Effect.GraphicsDevice should not be null. Check those properties right after you load the effect. If they are already like this, then the loading failed for some reason. Do you get any suspicious log output during effect loading?

1 Like

Thanks alot, I can’t believe I didn’t think earlier about simply stepping through the code and checking when the effect is actually disposed. Calling ApplyChanges() on the graphics device after loading the effect was the culprit. I guess I was so hung up on there being something wrong with the shader that I completely forgot about that option :smiley: