[SOLVED] Simple 2D lighting in HLSL not working.(Tried everything)

Hi,

I’ve been trying to learn more about shaders and coding lighting into my games.

I’m following this tutorial http://www.xnahub.com/simple-2d-lighting-system-in-c-and-monogame/

I’ve got it working semi but the problem is that the mask I use is circular while what I get on screen is in the shape of an ellipse.

Here is a picture of my problem. The right side is just the plain light mask without using the effect

I think somehow the program is stretching the light renderTarget texture vertically and it makes the circle into an ellipse. I just don’t know how.

This is my effect code

sampler s0;
texture2D lightMask;
sampler lightSampler : register(s1) = sampler_state {
	Texture = <lightMask>;
	AddressU = Clamp;
	AddressV = Clamp;
};

struct VSOutput
{
	float4 position		: SV_Position;
	float4 color		: COLOR0;
	float2 texCoord		: TEXCOORD0;
};

float4 PixelShaderFunction(VSOutput input) : SV_TARGET0
{
	float4 color = tex2D(s0, input.texCoord);
	float4 lightColor = tex2D(lightSampler, input.texCoord);
	return color * lightColor;
}


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

This is where I initialize my render targets

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

This is where I draw my lights to my lighting render target

//Begin draw lighting render target
GraphicsDevice.SetRenderTarget(lightsTarget);	//Set render target to lights
GraphicsDevice.Clear(Color.Black);              //Clear screen to black
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive);
spriteBatch.Draw(lightMask, new Vector2(0,0), Color.Red);
spriteBatch.Draw(lightMask, new Vector2(400, 100), Color.Red);
spriteBatch.End();

(I’ve ommited my mainTarget draw code because it’s extra text and I don’t think it’s the problem since it shows up correctly on the screen)

This is where I call the effect and blend the two render targets

//Blend both render targets together onto the backbuffer
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
effect1.Parameters["lightMask"].SetValue(lightsTarget);
effect1.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(mainTarget, Vector2.Zero, Color.White);
//spriteBatch.Draw(lightsTarget, Vector2.Zero, Color.White);
spriteBatch.End();

I have tried a bunch of things, including instead of using tex2D using the Sampler method from Texture2D in HLSL. I’ve also tried every possible value of AddressU and AddressV.

In the tutorial he uses ps_2_0 but I get an error when I use that saying I can only use ps 4 level 9

Is it possible your rendertarget has a different resolution to your backbuffer

I could just about hug you right now. They were different resolutions. I used the GraphicsDevice.PresentationParameters.Width twice to initialize the mainTarget so it had a resolution of 800x800 while the lightsTarget had a resolution of 800x400(correct). Also when I was printing the resolutions I was printing lightsTarget twice…

It works now. I feel very stupid but thank you.

Can I mark this as resolved?

you can still change the original title i believe, put a [SOLVED] before