Skip to main content
edited title
Link
danbystrom
  • 289
  • 1
  • 2
  • 14

SharpDx Null Reference Exception onCustom effect in SpriteBatch.End()

added 261 characters in body
Source Link
danbystrom
  • 289
  • 1
  • 2
  • 14

The render targets are created like this:

    ShadowDepthTarget = RenderTarget2D.New(_graphicsDevice, width, height, PixelFormat.R16G16.Float);
    _shadowBlurTarg = RenderTarget2D.New(_graphicsDevice, width, height, PixelFormat.R16G16.Float);

This is the shader code, I have tried variuos versions of the method signatures - in vain:

This is the shader code, I have tried variuos versions of the method signatures - in vain:

The render targets are created like this:

    ShadowDepthTarget = RenderTarget2D.New(_graphicsDevice, width, height, PixelFormat.R16G16.Float);
    _shadowBlurTarg = RenderTarget2D.New(_graphicsDevice, width, height, PixelFormat.R16G16.Float);

This is the shader code, I have tried variuos versions of the method signatures - in vain:

Source Link
danbystrom
  • 289
  • 1
  • 2
  • 14

SharpDx Null Reference Exception on SpriteBatch.End()

I'm trying to port a blur effect from an XNA project over to SharpDx, and I get a "Null Reference Exception" wich I'm unable to find the reason for.

    private void blurShadow(RenderTarget2D to, RenderTarget2D from, int dir)
    {
        _graphicsDevice.SetRenderTargets(to);
        _shadowBlurEffect.CurrentTechnique.Passes[dir].Apply();
        _spriteBatch.Begin(SpriteSortMode.Deferred, _shadowBlurEffect);
        _spriteBatch.Draw(from, Vector2.Zero, Color.White);
        _spriteBatch.End();  // <-- Null Reference Exception here
    }

    public void Draw()
    {
        _graphicsDevice.SetRenderTargets(_graphicsDevice.DepthStencilBuffer, ShadowDepthTarget);
        _graphicsDevice.Clear(Color.White); // Clear the render target to 1 (infinite depth)
        foreach (var obj in ShadowCastingObjects)
            obj.Draw(Camera, DrawingReason.ShadowDepthMap, this);

        blurShadow(_shadowBlurTarg, ShadowDepthTarget, 0);
        blurShadow(ShadowDepthTarget, _shadowBlurTarg, 1);

        _graphicsDevice.SetRenderTargets(_graphicsDevice.DepthStencilBuffer, _graphicsDevice.BackBuffer);
    }

This is the shader code, I have tried variuos versions of the method signatures - in vain:

Texture2D Texture;
SamplerState TextureSampler;

// Precalculated weights and offsets
float weights[15] = { 0.1061154, 0.1028506, 0.1028506, 0.09364651, 0.09364651, 
    0.0801001, 0.0801001, 0.06436224, 0.06436224, 0.04858317, 0.04858317, 
    0.03445063, 0.03445063, 0.02294906, 0.02294906 };

float offsets[15] = { 0, 0.00125, -0.00125, 0.002916667, -0.002916667, 
    0.004583334, -0.004583334, 0.00625, -0.00625, 0.007916667, -0.007916667, 
    0.009583334, -0.009583334, 0.01125, -0.01125 };

float4 BlurHorizontal(
    float2 UV : TEXCOORD0) : SV_Target
{
    float4 output = float4(0, 0, 0, 1);
    for (int i = 0; i < 15; i++)
        output += Texture.Sample(TextureSampler, UV + float2(offsets[i], 0)) * weights[i];
    return output;
}

float4 BlurVertical(
    float2 UV : TEXCOORD0) : SV_Target
{
    float4 output = float4(0, 0, 0, 1);
    for (int i = 0; i < 15; i++)
        output += Texture.Sample(TextureSampler, UV + float2(0, offsets[i])) * weights[i];
    return output;
}


technique Tech
{
    pass Horizontal
    {
        SetGeometryShader(0);
        SetVertexShader(0);
        SetPixelShader(CompileShader(ps_4_0, BlurHorizontal()));
    }

    pass Verical
    {
        SetGeometryShader(0);
        SetVertexShader(0);
        SetPixelShader(CompileShader(ps_4_0, BlurVertical()));
    }
}