[SOLVED] Get the content of each MipMap level from a RenderTarget

Hi !
I’ve made a shader to render a defined MipMap level into a RenderTarget, in order to have a feedback of all the MipMap level for a given texture. (I don’t know if GetData allows to get a level ?)
But the level 9 seems as good as the level 0 !
Ex: (The mouse hase moved a little between the 2 screenshots)
Lv0

Lv8

Are RenderTargets’ mipmap filled with a “lower quality” or is it to the developer to fill them ?

well can you show us this part?

There are versions of the RenderTarget2D constructor that have a mipmap parameter. If set to true mipmaps will automatically be filled with downsampled images whenever you change the render target.

Yes, GetData let’s you specify a mipmap level.

It’s simply:

#if OPENGL
    #define SV_POSITION POSITION
    #define VS_SHADERMODEL vs_3_0
    #define PS_SHADERMODEL ps_3_0
#else
    #define VS_SHADERMODEL vs_5_0
    #define PS_SHADERMODEL ps_5_0
#endif

uniform int _MipLevel = 0;

Texture2D TextureToRenderMipFrom;
SamplerState TexSampler
{
    Texture = <TextureToRenderMipFrom>;
    AddressU = CLAMP; //CLAMP REPEAT
    AddressV = CLAMP;
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float4 Color : COLOR0;
    float2 TextureCoordinates : TEXCOORD0;
};

float4 MainPS(VertexShaderOutput input) : COLOR
{
    float4 TexValueMIPx = TextureToRenderMipFrom.SampleLevel(TexSampler, input.TextureCoordinates, _MipLevel);
    
    //Shows if _MipLevel is effectively passed to the shader by modulating the red channel
    //return float4(_MipLevel / 9.0f, TexValueMIPx.r, 0, 1);
    return TexValueMIPx;
}

technique RenderMipFromTex
{
    pass P0
    {
        PixelShader = compile PS_SHADERMODEL MainPS();
    }
};

with _MipLevel within [0,9]

@markus: I’m using this constructor, setting true for mipmaps. But they do not seem to be downsampled or something in my code to draw them is wrong.

Are you using DirectX or OpenGL? At least with DirectX it should work. In case you’re building MonoGame from source, the relevant code is in GraphicsDevice.DirectX.cs in PlatformResolveRenderTargets(). You could put a breakpoint there to see if _d3dContext.GenerateMips() gets called.

My bad. You pointed me to the right direction. I checked all my RenderTarget, and Murphy’s law was applicable: somewhere I recreated (Why ? Dont’ ask me I dont remember) the rendertarget, but without mipmapping (Why again ?)

Thanks to all for your help. :slight_smile: I’ll go sleeping, it seems I need some rest.

1 Like