Hi guys! I am developing a game in visual basic and monogame and recently updated from monogame 3.4 to 3.5. In the update process the fx file for my guassian blur effect stopped working. Apparently the update forced me to change the pixel shader to “PixelShader = compile ps_3_0 PS_GaussianBlur();”. This made it so the fx file will load, but now when I try to use it I get a nullReferenceExcpetion when passing the starting image into the file. Here is the code for the fx file:
#define RADIUS 7
#define KERNEL_SIZE (RADIUS * 2 + 1)
//-----------------------------------------------------------------------------
// Globals.
//-----------------------------------------------------------------------------
float weights[KERNEL_SIZE];
float2 offsets[KERNEL_SIZE];
//-----------------------------------------------------------------------------
// Textures.
//-----------------------------------------------------------------------------
texture2D colorMapTexture;
sampler colorMap : register(s0);
//-----------------------------------------------------------------------------
// Pixel Shaders.
//-----------------------------------------------------------------------------
float4 PS_GaussianBlur(float4 pos : SV_POSITION, float4 color1 : COLOR0, float2 texCoord : TEXCOORD0) : SV_TARGET0
{
float4 color = float4(0.0f, 0.0f, 0.0f, 0.0f);
for (int i = 0; i < KERNEL_SIZE; ++i)
color += colorMapTexture.Sample(colorMap, texCoord.xy + offsets[i]) * weights[i];
return color;
}
//-----------------------------------------------------------------------------
// Techniques.
//-----------------------------------------------------------------------------
technique GaussianBlur
{
pass
{
PixelShader = compile ps_3_0 PS_GaussianBlur();
}
}
And here is the code for the function that blurs the image in my game:
Public Shared Function blurImage(img As Texture2D, sb As SpriteBatch, ByRef blurTarget1 As RenderTarget2D, ByRef blurTarget2 As RenderTarget2D) As Texture2D
'Return img 'BROKEN
Dim radius As Integer = 2 : Dim amount As Single = 1.0F
Dim sigma As Single = radius / amount : Dim kernel As Single() = New Single(radius * 2) {}
Dim index As Integer = 0 : Dim total As Single = 0F : Dim distance As Single = 0F
Dim twoSigmaSquare As Single = 2.0F * sigma * sigma : Dim sigmaRoot As Single = CSng(Math.Sqrt(twoSigmaSquare * Math.PI))
Dim offsetsHoriz As Vector2() = New Vector2(radius * 2) {} : Dim offsetsVert As Vector2() = New Vector2(radius * 2) {}
Dim effect As Effect = content.Load(Of Effect)(“Effects\GaussianBlur”)
For i As Integer = -radius To radius index = i + radius offsetsHoriz(index) = New Vector2(i * 1.0F / img.Width, 0F) offsetsVert(index) = New Vector2(0F, i * 1.0F / img.Height) Next
index = 0 For i As Integer = -radius To radius distance = i * i : index = i + radius kernel(index) = CSng(Math.Exp(-distance / twoSigmaSquare)) / sigmaRoot total += kernel(index) Next For i As Integer = 0 To kernel.Length - 1 : kernel(i) /= total : Next
If IsNothing(blurTarget1) Then blurTarget1 = New RenderTarget2D(Game.graphics.GraphicsDevice, img.Width, img.Height, False, Game.graphics.GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.None) If IsNothing(blurTarget2) Then blurTarget2 = New RenderTarget2D(Game.graphics.GraphicsDevice, img.Width, img.Height, False, Game.graphics.GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.None)
Dim srcRect As New Rectangle(0, 0, img.Width, img.Height) Dim destRect1 As New Rectangle(0, 0, blurTarget1.Width, blurTarget1.Height) Dim destRect2 As New Rectangle(0, 0, blurTarget2.Width, blurTarget2.Height)
effect.Parameters("weights").SetValue(kernel) effect.Parameters("offsets").SetValue(offsetsHoriz) effect.Parameters("colorMapTexture").SetValue(img)
Game.graphics.GraphicsDevice.SetRenderTarget(blurTarget1) sb.Begin(0, BlendState.Opaque, Nothing, Nothing, Nothing, effect) sb.Draw(img, New Vector2(0, 0), Color.White) sb.End()
effect.Parameters("offsets").SetValue(offsetsVert)
Game.graphics.GraphicsDevice.SetRenderTarget(blurTarget2) sb.Begin(0, BlendState.Opaque, Nothing, Nothing, Nothing, effect) sb.Draw(blurTarget1, New Vector2(0, 0), Color.White) sb.End()
Game.graphics.GraphicsDevice.SetRenderTarget(Nothing)
Return blurTarget2 End Function
The error is in this line of code: effect.Parameters(“colorMapTexture”).SetValue(img)
Thanks a lot!