Effect parameters not working ?

Hey,

I tried to apply my shader but can’t get to work it with parameters. Is it supported in Mono Game now ?
spriteBatch.Begin(SpriteSortMode.Immediate,
BlendState.AlphaBlend,
null,
null,
null,
null,
camera.GetTransform());
if (isInMatrixMode)
{
shader.Parameters[“Param”].SetValue(0.7f);
shader.CurrentTechnique.Passes[0].Apply();
}
spriteBatch.Draw(Texture, Rect, animation.getSourceRect(), Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);
spriteBatch.End();

This line is the problem:
shader.Parameters[“Param”].SetValue(0.7f);

My shader is really simple at it works fine with “hardcoded” parameters but it is useless without passing parameter in runtime. Any ideas ? I’m really stuck with this.

HLSL code looks like:
sampler TextureSampler;

float Param;
float4 PixelShaderFunction(float4 position : SV_POSITION, float4 color : COLOR, float2 texCoords : TEXCOORD) : COLOR
{
// return color here
}

technique Technique1
{
pass Pass1
{
// TODO: set renderstates here.

    //VertexShader = compile vs_4_0_level_9_1 VertexShaderFunction();
    PixelShader = compile ps_4_0_level_9_3 PixelShaderFunction();
}

}

It would be useful, if you could post your error message.

EffectParameter param = shader.Parameters[“Param”];
param.SetValue(0.7f);

param is NULL but it is obviously declared in HLSL shader file.

  •    $exception    {System.NullReferenceException: Object reference not set to an instance of an object.
    
    at Game.Scenes.Gameplay.EnemiesComponent.Terrorist.Draw(Effect shader, Camera camera, SpriteBatch spriteBatch, Int32 onMushrooms, Boolean isInMatrixMode)
    at Game.Scenes.Gameplay.EnemiesComponent.Enemies.Draw(Camera camera, SpriteBatch spriteBatch, Int32 onMushrooms, Boolean isInMatrixMode)
    at Game.Scenes.Gameplay.SceneGameplay.Draw()
    at Game.Scenes.SceneManager.DrawScene()
    at Game.Game1.Draw(GameTime gameTime)
    at Microsoft.Xna.Framework.Game.DoDraw(GameTime gameTime)
    at Microsoft.Xna.Framework.Game.Tick()
    at MonoGame.Framework.WindowsPhone.SurfaceUpdateHandler.Draw(Device device, DeviceContext context, RenderTargetView renderTargetView)
    at MonoGame.Framework.WindowsPhone.DrawingSurfaceUpdateHandler.DrawingSurfaceContentProvider.GetTexture(Size2F surfaceSize, DrawingSurfaceSynchronizedTexture& synchronizedTexture, RectangleF& textureSubRectangle)
    at SharpDX.Direct3D11.DrawingSurfaceContentProviderShadow.DrawingSurfaceContentProviderVtbl.GetTexture(IntPtr thisPtr, IntPtr surfaceSize, IntPtr synchronizedTexture, IntPtr textureSubRectangle)} System.Exception {System.NullReferenceException}

Finally I got it, solution:

Parameter was declared in HLSL file but it was never used, so probably in compilation time variable was “cutted off”

1 Like

Yes, MonoGame does that with parameters that are not used. :smile: