Getting bitmap from multisampled backbuffer

Started by
1 comment, last by jamesxli 10 years, 4 months ago

Hi Everyone,

In my desktop application (using SlimDX & DX11 ) I need to make a snapshot of a rendered window. I use the following code to save the bitmap of the current back buffer:


            var context = device.ImmediateContext;            
            var srcTex = renderTarget.Resource as Texture2D;
            var desc = srcTex.Description;
            desc.BindFlags = BindFlags.None;
            desc.Usage = ResourceUsage.Staging;
            desc.CpuAccessFlags = CpuAccessFlags.Read;
            desc.SampleDescription = new SampleDescription(1, 0);
            Texture2D staging = new Texture2D(device, desc);
            context.CopyResource(srcTex, staging);
            Texture2D.ToFile(context, staging, ImageFileFormat.Png, "c:/Temp/Screenshot.png");

This code works fine, if my renderTarget is not multisampled. If the renderTarget is multisampled ( as needed for anti-aliasing) I got blank image. Does anybody know a way to get a bitmap snapshot from a multisampled render target?

Advertisement

You need to use ResolveSubresource first.

Thank you very much for the quick response. It solves the problem completely. Just for people who might be encounter the same problem, my code to get a bitmap from current back-buffer looks as following:


        public Bitmap GetBitmap() {
            var ctx = device.ImmediateContext;
            var srcTex = renderTarget.Resource as Texture2D;
                
            var desc = srcTex.Description;
            desc.BindFlags = BindFlags.None;
            desc.Usage = ResourceUsage.Staging;
            desc.CpuAccessFlags = CpuAccessFlags.Read;
            desc.SampleDescription = new SampleDescription(1, 0);
            Texture2D staging = new Texture2D(device, desc);

            desc.Usage = ResourceUsage.Default;
            desc.CpuAccessFlags = CpuAccessFlags.None;
            Texture2D resolved = new Texture2D(device, desc);

            ctx.ResolveSubresource(srcTex, 0, resolved, 0, desc.Format);
            ctx.CopyResource(resolved, staging);

            using (Surface surface = staging.AsSurface()) {
                var rect = surface.Map(SlimDX.DXGI.MapFlags.Read);
                Bitmap bm = new Bitmap(desc.Width, desc.Height,
                            rect.Pitch, PixelFormat.Format32bppArgb, rect.Data.DataPointer);
                surface.Unmap();
                resolved.Dispose();
                staging.Dispose();                
                return bm;
            }            
        }

This topic is closed to new replies.

Advertisement