Issues with spritebatch + custom effect on Windows + DirectX

Hi all,

Apologies if this is a known issue, I’ve searched and found vaguely similar issues, but the apparent fixes rolled into the development branch don’t help. I’ve been working away on a project, started to implement a post-process bloom. Took a look at the samples, including the NeonShooter etc.

I’ve been having real trouble getting this to work, and it isn’t clear to me why. I was on MonoGame 3.4 (stable release), also tried the latest development installer in case this was a known issue, as I did find references to something vaguely similar that was fixed - “Fixing sampler texture being unbound when using render targets #3720”. I am on Windows / DirectX.

I’ve cut almost everything out, now, to try to narrow this down, and now I’m simply trying to render a simple scene with some primitive shapes into a rendertarget, then use spritebatch with a custom effect to extract the bright parts. What I can say is:

  • rendering into a RenderTarget2D works.

  • Setting the render target back to null, then rendering my RenderTarget2D into the backbuffer with a default spritebatch works, so I see the full scene as expected. So:

    GraphicsDevice.SetRenderTarget(null);
    spriteBatch.Begin();
    spriteBatch.Draw(sceneRenderTarget,
    new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
    spriteBatch.End();

The above works as expected. I get a full screen quad with the contents of my original scene.

But, if I instead try to render using a custom effect to process this, I get a blank coloured screen (with the colour being identical to the colour of whatever pixel is at (0,0), so it’s as though the pixel shader is not sampling the rendertarget correctly?

 GraphicsDevice.SetRenderTarget(null);
        spriteBatch.Begin(0, BlendState.Opaque, null, null, null, effect);
        spriteBatch.Draw(sceneRenderTarget, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
        spriteBatch.End();

I am using the BloomExtract.fx shader from the NeonShooter sample, unchanged:

sampler TextureSampler : register(s0);

float BloomThreshold;

float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
{
// Look up the original image color.
float4 c = tex2D(TextureSampler, texCoord);

// Adjust it to keep only values brighter than the specified threshold.
return saturate((c - BloomThreshold) / (1 - BloomThreshold));

}

technique BloomExtract
{
pass Pass1
{
#if SM4
PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
#else
PixelShader = compile ps_2_0 PixelShaderFunction();
#endif
}
}

I don’t know enough about the internals to make much of a guess at what’s going on here. Can anyone help?

And wouldn’t you know it, as soon as I post this, I find my issue. Apologies!