MipMapping and RenderTarget

Working on cascaded variance shadow mapping for my game.

It works except MipMap functionality doesn’t seem to work for the RenderTarget2D into which I drew my shadows. I tested this by rendering the RenderTarget2D with a simple isolated shader that draws the first mip level. The screen appears black. If I draw mip level zero, I see the exact shadow map.

I found this old bug when I was looking for answers:
https://github.com/mono/MonoGame/issues/1001.
https://monogame.codeplex.com/discussions/440282

These are pretty old. Is this still the case? Do I need a hack or something to enable mipmapping? BTW, this is Windows platform with the Windows8 environment variable, so I’m using DirectX11

Here’s my RenderTarget2D definition. I’ve tried tweaking quite a few parameters:

ShadowMap = new RenderTarget2D(GraphicsDevice,
                                            ShadowMapSize * NumSplits,
                                            ShadowMapSize,
                                            true,
                                            SurfaceFormat.Vector2,
                                            DepthFormat.Depth24);

Here’s my simple shader:

Texture2D TestTex;

SamplerState DiffuseSampler
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Clamp;
    AddressV = Clamp;
};

float4 ps_pixelshader(float4 pos : SV_POSITION, float4 color1 : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
	return TestTex.SampleLevel(DiffuseSampler, texCoord, 1);
}

technique TestShader
{
    pass Pass1
    {
		PixelShader = compile ps_4_0 ps_pixelshader();
    }
}

I have the same issue … When I’m trying to convert my png file into Texture2D with mipmap, I get a transparent texture. In reality, all mipmap texture level are blank (colors: 0,0,0,0) except the first mipmap level !

My code:

using (FileStream _fileStream = new FileStream("Content/grass.png", FileMode.Open))
        using (Texture2D intermediateTexture = Texture2D.FromStream(graphicsDevice, _fileStream))
        {
            // create mip mapped texture
            using (RenderTarget2D _renderTarget = new RenderTarget2D(
                    graphicsDevice,
                    intermediateTexture.Width,
                    intermediateTexture.Height,
                    mipMap: true,
                    preferredFormat: SurfaceFormat.Color,
                    preferredDepthFormat: DepthFormat.None,
                    preferredMultiSampleCount: 0,
                    usage: RenderTargetUsage.DiscardContents))
            {
                graphicsDevice.SetRenderTarget(_renderTarget);

                spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.LinearWrap, DepthStencilState.None, RasterizerState.CullNone, effect: null);
                spriteBatch.Draw(intermediateTexture, Vector2.Zero, Color.White);
                spriteBatch.End();

                graphicsDevice.SetRenderTarget(null);

                // since rendertarget textures are volatile (contents get lost on device) we have to copy data in new texture.
                Texture2D mergedTexture = new Texture2D(graphicsDevice, intermediateTexture.Width, intermediateTexture.Height, true, SurfaceFormat.Color);
                Color[] _content = new Color[intermediateTexture.Width * intermediateTexture.Height];

                for (int i = 0; i < _renderTarget.LevelCount; i++)
                {
                    int n = _renderTarget.Width * _renderTarget.Height / ((1 << i) * (1 << i));
                    _renderTarget.GetData<Color>(i, null, _content, 0, n);
                    mergedTexture.SetData<Color>(i, null, _content, 0, n);
                }

               return mergedTexture;
            }
        }