private Direct3D.Surface CopyRenderTargetSurfaceToSysmem(Direct3D.Surface surf) { Direct3D.Surface newSurf = Direct3D.Surface.CreateOffscreenPlain( mDevice.Device, surf.Description.Width, surf.Description.Height, surf.Description.Format, Pool.SystemMemory); mDevice.Device.GetRenderTargetData(surf, newSurf); return(newSurf); }
public override void SaveTo(string filename, ImageFileFormat format) { Direct3D.Surface surf = mTexture.Value.GetSurfaceLevel(0); bool disposeSurfWhenDone = false; if (surf.Description.Pool == Pool.Default) { surf = CopyRenderTargetSurfaceToSysmem(surf); disposeSurfWhenDone = true; } //Direct3D.ImageFileFormat d3dformat = SlimDX.Direct3D9.ImageFileFormat.Png; //switch (format) //{ // case ImageFileFormat.Bmp: d3dformat = Direct3D.ImageFileFormat.Bmp; break; // case ImageFileFormat.Png: d3dformat = Direct3D.ImageFileFormat.Png; break; // case ImageFileFormat.Jpg: d3dformat = Direct3D.ImageFileFormat.Jpg; break; // case ImageFileFormat.Tga: d3dformat = Direct3D.ImageFileFormat.Tga; break; //} DataRectangle rect = surf.LockRectangle(LockFlags.ReadOnly); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(surf.Description.Width, surf.Description.Height); var target = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); byte[] buffer = new byte[rect.Data.Length]; rect.Data.Read(buffer, 0, (int)rect.Data.Length); Marshal.Copy(buffer, 0, target.Scan0, buffer.Length); bmp.UnlockBits(target); surf.UnlockRectangle(); System.Drawing.Imaging.ImageFormat bmpFormat = System.Drawing.Imaging.ImageFormat.Png; switch (format) { case ImageFileFormat.Bmp: bmpFormat = System.Drawing.Imaging.ImageFormat.Bmp; break; case ImageFileFormat.Jpg: bmpFormat = System.Drawing.Imaging.ImageFormat.Jpeg; break; } bmp.Save(filename, bmpFormat); if (disposeSurfWhenDone) { surf.Dispose(); } }
public override void SetSourceSurface(SurfaceImpl surf, Rectangle srcRect) { mTexture.Dispose(); mTexture = new Ref <Texture>((surf as SDX_Surface).mTexture); mSrcRect = srcRect; Direct3D.Surface d3dsurf = mTexture.Value.GetSurfaceLevel(0); mTextureSize = new Size(d3dsurf.Description.Width, d3dsurf.Description.Height); SetVertsTextureCoordinates(mVerts, 0, mSrcRect); }
// TODO: Test this method: public override void WritePixels(PixelBuffer buffer, Point startPoint) { Direct3D.Surface surf = mTexture.Value.GetSurfaceLevel(0); Rectangle updateRect = new Rectangle(startPoint, buffer.Size); int pixelPitch = mDisplay.GetPixelPitch(surf.Description.Format); PixelFormat pixelFormat = mDisplay.GetPixelFormat(surf.Description.Format); surf.Dispose(); // This should probably only lock the region of the surface we intend to update. // However, as is usually the case with DirectX, doing so gives weird errors // with no real explanation as to what is wrong. DataRectangle stm = mTexture.Value.LockRectangle (0, LockFlags.None); if (buffer.PixelFormat != pixelFormat) { buffer = buffer.ConvertTo(pixelFormat); } unsafe { for (int i = 0; i < buffer.Height; i++) { int startIndex = buffer.GetPixelIndex(0, i); int rowStride = buffer.RowStride; IntPtr dest = (IntPtr) ((byte *)stm.Data.DataPointer + (i + updateRect.Top) * stm.Pitch + updateRect.Left * pixelPitch); Marshal.Copy(buffer.Data, startIndex, dest, rowStride); } } mTexture.Value.UnlockRectangle(0); }
public override void WritePixels(PixelBuffer buffer) { Direct3D.Surface surf = mTexture.Value.GetSurfaceLevel(0); if (surf.Description.Pool == Pool.Default) { throw new AgateLib.AgateException( "Cannot write to FrameBuffer surface in Direct3D."); } int pixelPitch = mDisplay.GetPixelPitch(surf.Description.Format); PixelFormat pixelFormat = mDisplay.GetPixelFormat(surf.Description.Format); surf.Dispose(); DataRectangle stm = mTexture.Value.LockRectangle(0, 0); if (buffer.PixelFormat != pixelFormat) { buffer = buffer.ConvertTo(pixelFormat); } unsafe { for (int i = 0; i < SurfaceHeight; i++) { int startIndex = buffer.GetPixelIndex(0, i); int rowStride = buffer.RowStride; IntPtr dest = (IntPtr)((byte *)stm.Data.DataPointer + i * stm.Pitch); Marshal.Copy(buffer.Data, startIndex, dest, rowStride); } } mTexture.Value.UnlockRectangle(0); }
public override PixelBuffer ReadPixels(PixelFormat format, Rectangle rect) { Direct3D.Surface surf = mTexture.Value.GetSurfaceLevel(0); bool disposeSurfWhenDone = false; if (surf.Description.Pool == Pool.Default) { surf = CopyRenderTargetSurfaceToSysmem(surf); disposeSurfWhenDone = true; } rect.X += mSrcRect.X; rect.Y += mSrcRect.Y; int pixelPitch = mDisplay.GetPixelPitch(surf.Description.Format); PixelFormat pixelFormat = mDisplay.GetPixelFormat(surf.Description.Format); if (format == PixelFormat.Any) { format = pixelFormat; } DataRectangle stm = surf.LockRectangle( new Drawing.Rectangle(0, 0, mTextureSize.Width, mTextureSize.Height), LockFlags.ReadOnly); byte[] array = new byte[SurfaceWidth * SurfaceHeight * pixelPitch]; int length = SurfaceWidth * pixelPitch; int index = 0; unsafe { byte *ptr = (byte *)stm.Data.DataPointer; for (int i = rect.Top; i < rect.Bottom; i++) { // hack if the size requested is too large. if (i >= mTextureSize.Height) { break; } //IntPtr ptr = (IntPtr)((int)stm.InternalData + i * stride + rect.Left * pixelPitch); IntPtr mptr = (IntPtr)(ptr + i * stm.Pitch + rect.Left * pixelPitch); Marshal.Copy(mptr, array, index, length); index += length; } } surf.UnlockRectangle(); if (disposeSurfWhenDone) { surf.Dispose(); } return(new PixelBuffer(format, rect.Size, array, pixelFormat)); }