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:

lightingEffect.CurrentTechnique.Passes[0].Apply();

Please can anybody help?

Any help will be much appreciated.

Thank you very much.

Here is the sample I am trying to get going:

Shader:

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();
}
}

c#

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;


        base.Initialize();
    }

    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))
            this.Exit();

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

        base.Update(gameTime);
    }

    private void drawMain(GameTime gameTime)
    {

        GraphicsDevice.SetRenderTarget(mainScene);
        GraphicsDevice.Clear(Color.Black);

        spriteBatch.Begin();

        // 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);
        }

        spriteBatch.End();

        GraphicsDevice.SetRenderTarget(null);
    }

    private void drawLightMask(GameTime gameTime)
    {
        GraphicsDevice.SetRenderTarget(lightMask);
        GraphicsDevice.Clear(Color.Black);

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

        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);

        spriteBatch.End();

        GraphicsDevice.SetRenderTarget(null);
    }

    protected override void Draw(GameTime gameTime)
    {
        drawMain(gameTime);
        drawLightMask(gameTime);

        GraphicsDevice.Clear(Color.Black);          

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

        lightingEffect.Parameters["lightMask"].SetValue(lightMask);         

        lightingEffect.CurrentTechnique.Passes[0].Apply();          


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

       
        spriteBatch.End();


        
        


        base.Draw(gameTime);
    }
}

}

Hi,

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?

Thanks

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… ???