Jump to content
  • Advertisement
Sign in to follow this  
jamesxli

DX11 Getting bitmap from multisampled backbuffer

This topic is 1726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

 

 

 

 

Share this post


Link to post
Share on other sites
Advertisement

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;
            }            
        }
Edited by jamesxli

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!