Shader not working

I’m trying to use this tutorial in monogame, but the effect does not work, only renders a black screen. Everyhing by itself renders just fine, I looked at the render targets if they get drawn to, but when I try to apply the effect to them it only renders a black screen.

I converted the .fx file using 2MGFX, also tried loading it with the following code:

BinaryReader Reader = new BinaryReader(File.Open(@“Content\Lighting.mgfx”, FileMode.Open));
lightingEffect = new Effect(GraphicsDevice, Reader.ReadBytes((int)Reader.BaseStream.Length));
also with

lightingEffect = content.Load(“Lighting.mgfx”);
and also with

byte[] bytecode = File.ReadAllBytes(“Content\Lighting.mgfx”);
lightingEffect = new Effect(graphics.GraphicsDevice, bytecode);
They did not make a difference.

The .fx file contains the following:

sampler s0;
texture lightMask;
sampler lightSampler = sampler_state { Texture = <lightMask>; };

float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0
    float4 color = tex2D(s0, coords);
    float4 lightColor = tex2D(lightSampler, coords);
    return color * lightColor;

technique Technique1
    pass Pass1
         PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();

And the monogame version of the game1.cs(Only the important stuff):

public class Game1 : Game
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Texture2D lightMask;
Texture2D SquareGuy;

RenderTarget2D lightsTarget;
RenderTarget2D mainTarget;

Effect lightingEffect;

public Game1()
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
    IsMouseVisible = true;

 protected override void LoadContent()
    spriteBatch = new SpriteBatch(GraphicsDevice);
    lightMask = Content.Load<Texture2D>("lightmask");
    SquareGuy = Content.Load<Texture2D>("SquareGuy");
    // lightingEffect = content.Load<Effect>("Lighting.mgfx");
    byte[] bytecode = File.ReadAllBytes("Content\\Lighting.mgfx");
    lightingEffect = new Effect(graphics.GraphicsDevice, bytecode);
    BinaryReader Reader = new     BinaryReader(File.Open(@"Content\\Lighting.mgfx", FileMode.Open));
    lightingEffect = new Effect(GraphicsDevice,     Reader.ReadBytes((int)Reader.BaseStream.Length)); 

    var pp = GraphicsDevice.PresentationParameters;
 //   pp.BackBufferHeight = 1024;
 //   pp.BackBufferWidth = 1024;
    lightsTarget = new RenderTarget2D(
        GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight);
    mainTarget = new RenderTarget2D(
        GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight);                            

 protected override void Draw(GameTime gameTime)

    // TODO: Add your drawing code here
    spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive);
    spriteBatch.Draw(lightMask, new Vector2(0, 0), Color.White);
    spriteBatch.Draw(lightMask, new Vector2(100, 0), Color.White);
    spriteBatch.Draw(lightMask, new Vector2(200, 200), Color.White);
    spriteBatch.Draw(lightMask, new Vector2(300, 300), Color.White);
    spriteBatch.Draw(lightMask, new Vector2(500, 200), Color.White);

    // Draw the main scene to the Render Target
    spriteBatch.Draw(SquareGuy, new Vector2(100, 0), Color.White);
    spriteBatch.Draw(SquareGuy, new Vector2(250, 250), Color.White);
    spriteBatch.Draw(SquareGuy, new Vector2(550, 225), Color.White);

    // Draw the main scene with a pixel
    spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
    spriteBatch.Draw(mainTarget, Vector2.Zero, Color.White);


So the question is what do I need to change to make the effect work in monogame?

I tried it in XNA with this code and worked perfectly. I know almost nothing about HLSL, so I don’t know if theres something wrong with the effect. I only changed the version thingy from ps_2_0 to ps_4_0_level_9_1. If I change it to ps_4_0_level_9_3 it still doesn’t work. I read something about assigning textures from outside of the .fx here, but since I know so little about shaders I don’t know if this is the problem.

I just did the same code and same light effect in Monogame and its working for me. The difference I did was I compiled the .FX file in the monogame pipeline GUI.

I can send you the project if you like (skype: Acrew1979)

I believe I answered this guy’s question on gamedev.stackexchange. The problem is the number of parameters in the PixelShaderFunction. Here is the answer I provided:

Thanks, but I already got a working answer on gamedev.stackexchange by Trevor like he says. I just forgot to update the post here.