Example #1
0
        public static void CopyToVolume(OpsContext context, VolumeTexture newTexture, string srcArg, Filter filter)
        {
            if (srcArg == null || srcArg.Length == 0)
            {
                return;
            }

            ArrayList srcList = context.FindTextures(srcArg);

            if (srcList == null || srcList.Count == 0)
            {
                throw new OpsException("Could not find source texture: " + srcArg);
            }

            if (((OpsTexture)srcList[0]).Texture is VolumeTexture)
            {
                VolumeLoader.FromVolume(newTexture.GetVolumeLevel(0), ((VolumeTexture)((OpsTexture)srcList[0]).Texture).GetVolumeLevel(0), filter, 0);
            }
            else
            {
                for (int i = 0; i < srcList.Count; i++)
                {
                    OpsTexture src = srcList[i] as OpsTexture;

                    if (!(src.Texture is Texture))
                    {
                        throw new OpsException("Source texture pattern is not 1 volume texture or all 2D textures: " + srcArg);
                    }

                    OpsTextureHelper.LoadVolumeSliceFromSurface(newTexture, 0, i, filter, ((Texture)src.Texture).GetSurfaceLevel(0));
                }
            }

            newTexture.GenerateMipSubLevels();
        }
Example #2
0
        public static VolumeTexture CloneVolume(VolumeTexture oldTexture, int width, int height, int depth, int mips, Format format, Usage usage, Filter filter, Pool pool)
        {
            VolumeTexture newTexture = new VolumeTexture(oldTexture.Device, width, height, depth, mips, usage, format, pool);

            VolumeLoader.FromVolume(newTexture.GetVolumeLevel(0), oldTexture.GetVolumeLevel(0), filter, 0);
            return(newTexture);
        }
Example #3
0
        ///<summary>
        ///    @copydoc HardwarePixelBuffer.Blit
        ///</summary>
        public override void Blit(HardwarePixelBuffer _src, BasicBox srcBox, BasicBox dstBox)
        {
            D3DHardwarePixelBuffer src = (D3DHardwarePixelBuffer)_src;

            if (surface != null && src.surface != null)
            {
                // Surface-to-surface
                Rectangle dsrcRect  = ToD3DRectangle(srcBox);
                Rectangle ddestRect = ToD3DRectangle(dstBox);
                // D3DXLoadSurfaceFromSurface
                SurfaceLoader.FromSurface(surface, ddestRect, src.surface, dsrcRect, Filter.None, 0);
            }
            else if (volume != null && src.volume != null)
            {
                // Volume-to-volume
                Box dsrcBox  = ToD3DBox(srcBox);
                Box ddestBox = ToD3DBox(dstBox);
                // D3DXLoadVolumeFromVolume
                VolumeLoader.FromVolume(volume, ddestBox, src.volume, dsrcBox, Filter.None, 0);
            }
            else
            {
                // Software fallback
                base.Blit(_src, srcBox, dstBox);
            }
        }
Example #4
0
        ///<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();
            }
        }
Example #5
0
        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();
            }
        }