Shader Effect Flips Out

Hi There,

First my disclaimer: I am an complete beginner with any game developement - especially shaders, currently shader code might as well be in greek - I wouldn’t know the diffrence.

I found some samples and just want to get it to work, then from there I’ll start figuring the rest out. PS. I am not intensionally starting with shaders, I am simply trying
to create a certain effect. I tried diffrent BlendState with multiple spriteBatches, but couldn’t get it right either with those.

I have a strange problem when applying a shader. The whole scene seems to be turned upside-down, and zoomed out. I have been messing about whole day but just can’t get it right.

Here is the scene witout apply shader effect:

Here is the same scene with shader effect:

By only commenting this line in and out flips it around:


Please can anybody help?

Any help will be much appreciated.

Thank you very much.

Here is the sample I am trying to get going:


texture lightMask;
sampler mainSampler : register(s0);
sampler lightSampler = sampler_state{ Texture = ; };

struct PixelShaderInput
float4 TextureCoords: TEXCOORD0;

float4 PixelShaderFunction(PixelShaderInput input) : COLOR0
float2 texCoord = input.TextureCoords;

float4 mainColor = tex2D(mainSampler, texCoord);
float4 lightColor = tex2D(lightSampler, texCoord);
lightColor.rgb * float3(1.2f, 1.2f, 1.2f);
mainColor.rgb /= mainColor.a;
return saturate(mainColor * lightColor) * 4;


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


using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;

namespace Game1
public class Game1 : Game

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Texture2D background;
    Texture2D blackSquare;
    Texture2D ground;
    Texture2D torch;
    Texture2D lightmask;

    RenderTarget2D mainScene;
    RenderTarget2D lightMask;

    Effect lightingEffect;

    List<Vector2> torchPositions;

    Vector2 mousePosition = Vector2.Zero;

    const int LIGHTOFFSET = 115;

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

    protected override void Initialize()

        torchPositions = new List<Vector2>();
        torchPositions.Add(new Vector2(40, 420));
        torchPositions.Add(new Vector2(340, 420));
        torchPositions.Add(new Vector2(640, 420));

        GraphicsDevice.SamplerStates[1] = SamplerState.LinearClamp;


    protected override void LoadContent()
        spriteBatch = new SpriteBatch(GraphicsDevice);

        background = Content.Load<Texture2D>("Effects/bg5");
        ground = Content.Load<Texture2D>("Effects/ground");
        torch = Content.Load<Texture2D>("Effects/torch");
        lightmask = Content.Load<Texture2D>("Effects/lightmask");
        blackSquare = Content.Load<Texture2D>("Effects/blacksquare");
        lightingEffect = Content.Load<Effect>("Effects/lighting");

        var pp = GraphicsDevice.PresentationParameters;
        mainScene = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight);
        lightMask = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight);

    protected override void Update(GameTime gameTime)
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))

        var mouse = Mouse.GetState();
        mousePosition = new Vector2(mouse.X, mouse.Y);


    private void drawMain(GameTime gameTime)



        // Draws a background image.
        spriteBatch.Draw(background, new Vector2(0, 0), Color.White);

        // Lays out some torches based on torch positions and mouse
        foreach (var position in torchPositions)
            spriteBatch.Draw(torch, position, Color.White);
        spriteBatch.Draw(torch, mousePosition, Color.White);

        // Lay out some ground.
        for (var idx = 0; idx < 25; idx++)
            spriteBatch.Draw(ground, new Vector2(32 * idx, 450), Color.White);



    private void drawLightMask(GameTime gameTime)

        // Create a Black Background
        spriteBatch.Draw(blackSquare, new Vector2(0, 0), new Rectangle(0, 0, 800, 800), Color.White);

        spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive);

        // Draw out lightmasks based on torch positions.
        foreach (var position in torchPositions)
            var new_pos = new Vector2(position.X - LIGHTOFFSET, position.Y - LIGHTOFFSET);
            spriteBatch.Draw(lightmask, new_pos, Color.White);
        spriteBatch.Draw(lightmask, new Vector2(mousePosition.X - LIGHTOFFSET, mousePosition.Y - LIGHTOFFSET), Color.White);



    protected override void Draw(GameTime gameTime)


        spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);



        spriteBatch.Draw(mainScene, new Vector2(0, 0), Color.White);






I saw a another post where someone also had shader problems, then he tested the same code in OpenGL and it worked.

So I created a OpenGL project and copied and pasted the exact same code and it worked. I guess it is a bug then with MonoGame and DX, or perhaps I need to use another method?


I’m stuck on the same subject; shaders…

But, are you sure everything is expressed the same in opengl vs otherwise?
-Perhaps you have been following a cross-platform-like tutrial, and thats why the code only runs there?

Well, I don’t recall that they specified, my understanding was it would work on anything, but then again, I am truly a beginner with this stuff, I hook, crook and hack as I go along. :slight_smile:

However, I converted to OpenGL project, now it works 100% the way I wanted:

Good! -But just in case, before you settle in too well with OpenGL, unless you know you need it, you might want to examine it for weaknesses…

I have no experience with it, but I have definately read posts implying that its not as well supported or documented…
-You may be cut off from certain features, if the implementation is a bit lagging… ???