///<summary> /// Convert Axiom PixelBox extent to D3D box ///</summary> protected static D3D.Box ToD3DBoxExtent(PixelBox lockBox) { D3D.Box pbox = new D3D.Box(); pbox.Left = 0; pbox.Right = lockBox.Width; pbox.Top = 0; pbox.Bottom = lockBox.Height; pbox.Front = 0; pbox.Back = lockBox.Depth; return(pbox); }
///<summary> /// Convert Axiom Box to D3D box ///</summary> protected static D3D.Box ToD3DBox(BasicBox lockBox) { D3D.Box pbox = new D3D.Box(); pbox.Left = lockBox.Left; pbox.Right = lockBox.Right; pbox.Top = lockBox.Top; pbox.Bottom = lockBox.Bottom; pbox.Front = lockBox.Front; pbox.Back = lockBox.Back; return(pbox); }
public Array LockBox(Type typeLock, Box box, LockFlags flags, params int[] ranks) { throw new NotImplementedException(); }
public GraphicsStream LockBox(Box box, LockFlags flags) { throw new NotImplementedException(); }
public GraphicsStream LockBox(Box box, LockFlags flags, out LockedBox lockedVolume) { throw new NotImplementedException(); }
///<summary> /// @copydoc HardwarePixelBuffer.BlitToMemory ///</summary> public override void BlitToMemory(BasicBox srcBox, PixelBox dst) { // Decide on pixel format of temp surface PixelFormat tmpFormat = format; if (D3DHelper.ConvertEnum(dst.Format) == D3D.Format.Unknown) { tmpFormat = dst.Format; } if (surface != null) { Debug.Assert(srcBox.Depth == 1 && dst.Depth == 1); // Create temp texture D3D.Texture tmp = new D3D.Texture(device, dst.Width, dst.Height, 1, // 1 mip level ie topmost, generate no mipmaps 0, D3DHelper.ConvertEnum(tmpFormat), Pool.Scratch); D3D.Surface subSurface = tmp.GetSurfaceLevel(0); // Copy texture to this temp surface Rectangle destRect, srcRect; srcRect = ToD3DRectangle(srcBox); destRect = ToD3DRectangleExtent(dst); SurfaceLoader.FromSurface(subSurface, destRect, surface, srcRect, Filter.None, 0); // Lock temp surface and copy it to memory int pitch; // Filled in by D3D GraphicsStream data = subSurface.LockRectangle(D3D.LockFlags.ReadOnly, out pitch); // Copy it PixelBox locked = new PixelBox(dst.Width, dst.Height, dst.Depth, tmpFormat); FromD3DLock(locked, pitch, data); PixelUtil.BulkPixelConversion(locked, dst); subSurface.UnlockRectangle(); // Release temporary surface and texture subSurface.Dispose(); tmp.Dispose(); } else { // Create temp texture D3D.VolumeTexture tmp = new D3D.VolumeTexture(device, dst.Width, dst.Height, dst.Depth, 0, D3D.Usage.None, D3DHelper.ConvertEnum(tmpFormat), Pool.Scratch); D3D.Volume subVolume = tmp.GetVolumeLevel(0); // Volume D3D.Box ddestBox = ToD3DBoxExtent(dst); D3D.Box dsrcBox = ToD3DBox(srcBox); VolumeLoader.FromVolume(subVolume, ddestBox, volume, dsrcBox, Filter.None, 0); // Lock temp surface and copy it to memory D3D.LockedBox lbox; // Filled in by D3D GraphicsStream data = subVolume.LockBox(LockFlags.ReadOnly, out lbox); // Copy it PixelBox locked = new PixelBox(dst.Width, dst.Height, dst.Depth, tmpFormat); FromD3DLock(locked, lbox, data); PixelUtil.BulkPixelConversion(locked, dst); subVolume.UnlockBox(); // Release temporary surface and texture subVolume.Dispose(); tmp.Dispose(); } }
protected void BlitFromMemoryImpl(PixelBox src, BasicBox dstBox) { // TODO: This currently does way too many copies. We copy // from src to a converted buffer (if needed), then from // converted to a byte array, then into the temporary surface, // and finally from the temporary surface to the real surface. PixelBox converted = src; IntPtr bufPtr = IntPtr.Zero; GCHandle bufGCHandle = new GCHandle(); // convert to pixelbuffer's native format if necessary if (D3DHelper.ConvertEnum(src.Format) == D3D.Format.Unknown) { int bufSize = PixelUtil.GetMemorySize(src.Width, src.Height, src.Depth, format); byte[] newBuffer = new byte[bufSize]; bufGCHandle = GCHandle.Alloc(newBuffer, GCHandleType.Pinned); bufPtr = bufGCHandle.AddrOfPinnedObject(); converted = new PixelBox(src.Width, src.Height, src.Depth, format, bufPtr); PixelUtil.BulkPixelConversion(src, converted); } // int formatBytes = PixelUtil.GetNumElemBytes(converted.Format); Surface tmpSurface = device.CreateOffscreenPlainSurface(converted.Width, converted.Height, D3DHelper.ConvertEnum(converted.Format), Pool.Scratch); int pitch; // Ideally I would be using the Array mechanism here, but that doesn't seem to work GraphicsStream buf = tmpSurface.LockRectangle(LockFlags.NoSystemLock, out pitch); buf.Position = 0; unsafe { int bufSize = PixelUtil.GetMemorySize(converted.Width, converted.Height, converted.Depth, converted.Format); byte * srcPtr = (byte *)converted.Data.ToPointer(); byte[] ugh = new byte[bufSize]; for (int i = 0; i < bufSize; ++i) { ugh[i] = srcPtr[i]; } buf.Write(ugh); } tmpSurface.UnlockRectangle(); buf.Dispose(); //ImageInformation imageInfo = new ImageInformation(); //imageInfo.Format = D3DHelper.ConvertEnum(converted.Format); //imageInfo.Width = converted.Width; //imageInfo.Height = converted.Height; //imageInfo.Depth = converted.Depth; if (surface != null) { // I'm trying to write to surface using the data in converted Rectangle srcRect = ToD3DRectangleExtent(converted); Rectangle destRect = ToD3DRectangle(dstBox); SurfaceLoader.FromSurface(surface, destRect, tmpSurface, srcRect, Filter.None, 0); } else { D3D.Box srcBox = ToD3DBoxExtent(converted); D3D.Box destBox = ToD3DBox(dstBox); Debug.Assert(false, "Volume textures not yet supported"); // VolumeLoader.FromStream(volume, destBox, converted.Data, converted.RowPitch * converted.SlicePitch * formatBytes, srcBox, Filter.None, 0); VolumeLoader.FromStream(volume, destBox, buf, srcBox, Filter.None, 0); } tmpSurface.Dispose(); // If we allocated a buffer for the temporary conversion, free it here // If I used bufPtr to store my temporary data while I converted // it, I need to free it here. This invalidates converted. // My data has already been copied to tmpSurface and then to the // real surface. if (bufGCHandle.IsAllocated) { bufGCHandle.Free(); } if (doMipmapGen) { GenMipmaps(); } }
///<summary> /// Lock a box ///</summary> public override PixelBox LockImpl(BasicBox lockBox, BufferLocking options) { // Check for misuse if (((int)usage & (int)TextureUsage.RenderTarget) != 0) { throw new Exception("DirectX does not allow locking of or directly writing to RenderTargets. Use BlitFromMemory if you need the contents; " + "in D3D9HardwarePixelBuffer.LockImpl"); } // Set extents and format PixelBox rval = new PixelBox(lockBox, format); // Set locking flags according to options D3D.LockFlags flags = D3D.LockFlags.None; switch (options) { case BufferLocking.Discard: // D3D only likes D3D.LockFlags.Discard if you created the texture with D3DUSAGE_DYNAMIC // debug runtime flags this up, could cause problems on some drivers if ((usage & BufferUsage.Dynamic) != 0) { flags |= D3D.LockFlags.Discard; } break; case BufferLocking.ReadOnly: flags |= D3D.LockFlags.ReadOnly; break; default: break; } if (surface != null) { // Surface GraphicsStream data = null; int pitch; if (lockBox.Left == 0 && lockBox.Top == 0 && lockBox.Right == width && lockBox.Bottom == height) { // Lock whole surface data = surface.LockRectangle(flags, out pitch); } else { Rectangle prect = ToD3DRectangle(lockBox); // specify range to lock data = surface.LockRectangle(prect, flags, out pitch); } if (data == null) { throw new Exception("Surface locking failed; in D3D9HardwarePixelBuffer.LockImpl"); } FromD3DLock(rval, pitch, data); } else { // Volume D3D.Box pbox = ToD3DBox(lockBox); // specify range to lock D3D.LockedBox lbox; // Filled in by D3D GraphicsStream data = volume.LockBox(pbox, flags, out lbox); FromD3DLock(rval, lbox, data); } return(rval); }
///<summary> /// Convert Axiom PixelBox extent to D3D box ///</summary> protected static D3D.Box ToD3DBoxExtent(PixelBox lockBox) { D3D.Box pbox = new D3D.Box(); pbox.Left = 0; pbox.Right = lockBox.Width; pbox.Top = 0; pbox.Bottom = lockBox.Height; pbox.Front = 0; pbox.Back = lockBox.Depth; return pbox; }
///<summary> /// Convert Axiom Box to D3D box ///</summary> protected static D3D.Box ToD3DBox(BasicBox lockBox) { D3D.Box pbox = new D3D.Box(); pbox.Left = lockBox.Left; pbox.Right = lockBox.Right; pbox.Top = lockBox.Top; pbox.Bottom = lockBox.Bottom; pbox.Front = lockBox.Front; pbox.Back = lockBox.Back; return pbox; }