Ejemplo n.º 1
0
        private unsafe static byte[] GetScreenData(Vector2I resolution, byte[] screenData = null)
        {
            const uint headerPadding    = 256; // Need to allocate some space for the bitmap headers
            const uint bytesPerPixel    = 4;
            uint       imageSizeInBytes = (uint)(resolution.Size() * bytesPerPixel);
            uint       dataSizeInBytes  = imageSizeInBytes + headerPadding;

            byte[] returnData = null;
            if (screenData == null)
            {
                screenData = new byte[imageSizeInBytes];
            }
            else if (screenData.Length != imageSizeInBytes)
            {
                Debug.Fail("Preallocated buffer for GetScreenData incorrect size: " + imageSizeInBytes.ToString() + " expected, " + screenData.Length + " received");
                return(returnData);
            }

            MyBindableResource imageSource             = Backbuffer;
            MyBindableResource imageSourceResourceView = null;

            if (imageSource != null && (resolution.X != imageSource.GetSize().X || resolution.Y != imageSource.GetSize().Y))
            {
                MyViewKey viewKey = new MyViewKey {
                    View = MyViewEnum.SrvView, Fmt = MyRender11Constants.DX11_BACKBUFFER_FORMAT
                };
                if (m_backbufferCopyResource == null)
                {
                    m_backbufferCopyResource = new MyCustomTexture(m_resolution.X, m_resolution.Y, BindFlags.ShaderResource, MyRender11Constants.DX11_BACKBUFFER_FORMAT);
                    m_backbufferCopyResource.AddView(viewKey);
                }
                MyRenderContext.Immediate.DeviceContext.CopyResource(imageSource.m_resource, m_backbufferCopyResource.m_resource);
                imageSource             = m_backbufferCopyResource;
                imageSourceResourceView = m_backbufferCopyResource.GetView(viewKey);
            }

            if (imageSource == null)
            {
                return(returnData);
            }

            if (imageSizeInBytes > int.MaxValue)
            {
                Debug.Fail("Image size too large to read!");
                return(returnData);
            }

            MyBindableResource imageResource = imageSource;
            bool shouldResize = imageSourceResourceView != null;

            if (shouldResize)
            {
                imageResource = m_lastScreenDataResource;
                if (imageResource == null || (imageResource.GetSize().X != resolution.X || imageResource.GetSize().Y != resolution.Y))
                {
                    if (m_lastScreenDataResource != null && (m_lastScreenDataResource != m_backbufferCopyResource && m_lastScreenDataResource != Backbuffer))
                    {
                        m_lastScreenDataResource.Release();
                    }
                    m_lastScreenDataResource = null;

                    imageResource = new MyRenderTarget(resolution.X, resolution.Y, MyRender11Constants.DX11_BACKBUFFER_FORMAT, 1, 0);
                }
                var RC            = MyRenderContext.Immediate;
                var deviceContext = RC.DeviceContext;
                deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
                deviceContext.Rasterizer.SetViewport(0, 0, resolution.X, resolution.Y);
                deviceContext.PixelShader.Set(MyDebugRenderer.BlitTextureShader);
                deviceContext.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);

                RC.BindDepthRT(null, DepthStencilAccess.ReadWrite, imageResource);
                RC.BindSRV(0, imageSourceResourceView);
                MyDebugRenderer.DrawQuad(0, 0, resolution.X, resolution.Y, MyScreenPass.QuadVS);
                //MyCopyToRT.Run(imageResource, imageSourceResourceView, new MyViewport(resolution));
            }

            m_lastScreenDataResource = imageResource;

            Stream dataStream = m_lastDataStream;

            if (m_lastDataStream == null || m_lastDataStream.Length != dataSizeInBytes)
            {
                if (m_lastDataStream != null)
                {
                    m_lastDataStream.Dispose();
                    m_lastDataStream = null;
                }
                dataStream = new DataStream((int)dataSizeInBytes, true, true);
                dataStream.Seek(0, SeekOrigin.Begin);
            }

            m_lastDataStream = dataStream;

            Resource.ToStream(MyRenderContext.Immediate.DeviceContext, imageResource.m_resource, ImageFileFormat.Bmp, dataStream);

            if (!(dataStream.CanRead && dataStream.CanSeek))
            {
                Debug.Fail("Screen data stream does not support the necessary operations to get the data");
                return(returnData);
            }

            fixed(byte *dataPointer = screenData)
            {
                GetBmpDataFromStream(dataStream, dataPointer, imageSizeInBytes);
            }

            returnData = screenData;

            if (m_lastDataStream != null)
            {
                m_lastDataStream.Seek(0, SeekOrigin.Begin);
            }

            return(returnData);
        }
        private unsafe static byte[] GetScreenData(Vector2I resolution, byte[] screenData = null)
        {
            const uint headerPadding    = 256; // Need to allocate some space for the bitmap headers
            const uint bytesPerPixel    = 4;
            uint       imageSizeInBytes = (uint)(resolution.Size() * bytesPerPixel);
            uint       dataSizeInBytes  = imageSizeInBytes + headerPadding;

            byte[] returnData = null;
            if (screenData == null)
            {
                screenData = new byte[imageSizeInBytes];
            }
            else if (screenData.Length != imageSizeInBytes)
            {
                Debug.Fail("Preallocated buffer for GetScreenData incorrect size: " + imageSizeInBytes.ToString() + " expected, " + screenData.Length * bytesPerPixel + " received");
                return(returnData);
            }

            MyBindableResource imageSource = Backbuffer;

            if (imageSource == null)
            {
                return(returnData);
            }

            if (imageSizeInBytes > int.MaxValue)
            {
                Debug.Fail("Image size too large to read!");
                return(returnData);
            }

            MyBindableResource imageResource = imageSource;

            if (resolution.X != imageResource.GetSize().X || resolution.Y != imageResource.GetSize().Y)
            {
                imageResource = m_lastScreenDataResource;
                if (imageResource == null || (imageResource.GetSize().X != resolution.X || imageResource.GetSize().Y != resolution.Y))
                {
                    if (m_lastScreenDataResource != null && m_lastScreenDataResource != Backbuffer)
                    {
                        m_lastScreenDataResource.Release();
                    }
                    m_lastScreenDataResource = null;

                    imageResource = new MyRenderTarget(resolution.X, resolution.Y, MyRender11Constants.DX11_BACKBUFFER_FORMAT, 0, 0);
                }

                MyCopyToRT.Run(imageResource, imageSource, new MyViewport(resolution));
            }

            m_lastScreenDataResource = imageResource;

            Stream dataStream = m_lastDataStream;

            if (m_lastDataStream == null || m_lastDataStream.Length != dataSizeInBytes)
            {
                if (m_lastDataStream != null)
                {
                    m_lastDataStream.Dispose();
                    m_lastDataStream = null;
                }
                dataStream = new DataStream((int)dataSizeInBytes, true, true);
            }

            m_lastDataStream = dataStream;

            Resource.ToStream(MyRenderContext.Immediate.DeviceContext, imageResource.m_resource, ImageFileFormat.Bmp, dataStream);

            if (!(dataStream.CanRead && dataStream.CanSeek))
            {
                Debug.Fail("Screen data stream does not support the necessary operations to get the data");
                return(returnData);
            }

            fixed(byte *dataPointer = screenData)
            {
                GetBmpDataFromStream(dataStream, dataPointer, imageSizeInBytes);
            }

            returnData = screenData;

            if (m_lastDataStream != null)
            {
                m_lastDataStream.Seek(0, SeekOrigin.Begin);
            }

            return(returnData);
        }