Esempio n. 1
0
        public static void LoadSurfaceFromVolumeSlice(VolumeTexture volumeTex, int mip, int slice, Filter filter, Surface surface)
        {
            VolumeDescription vd         = volumeTex.GetLevelDescription(mip);
            OpsFormatHelper   formatHelp = OpsFormatHelper.FindByFormat(vd.Format);

            Texture sliceTex = new Texture(volumeTex.Device, vd.Width, vd.Height, 1, Usage.None, formatHelp.Format, Pool.SystemMemory);

            Box box = new Box();

            box.Left   = 0;
            box.Right  = vd.Width;
            box.Top    = 0;
            box.Bottom = vd.Height;
            box.Front  = slice;
            box.Back   = slice + 1;

            LockedBox      volumeLB;
            GraphicsStream volumeData = volumeTex.LockBox(0, box, LockFlags.ReadOnly, out volumeLB);

            int            slicePitch;
            GraphicsStream sliceData = sliceTex.LockRectangle(mip, LockFlags.None, out slicePitch);

            CopyTextureData(volumeData, vd.Width, vd.Height, formatHelp, volumeLB.RowPitch, sliceData, slicePitch);

            sliceTex.UnlockRectangle(0);
            volumeTex.UnlockBox(mip);

            SurfaceLoader.FromSurface(surface, sliceTex.GetSurfaceLevel(0), filter, 0);

            sliceTex.Dispose();
        }
Esempio n. 2
0
        /*-------------------------------------------------------------------------
         * 텍스쳐コピー
         * 例えば시스템메모リ상の텍스쳐をVRAMに전送する등
         * 사이즈등はチェックしないので注意
         * ---------------------------------------------------------------------------*/
        static public bool CopyTexture(Device device, Texture dst_texture, Texture src_texture)
        {
            if (device == null)
            {
                return(false);
            }
            if (src_texture == null)
            {
                return(false);
            }
            if (dst_texture == null)
            {
                return(false);
            }

            try{
                // 単純にコピー
                Surface dst = dst_texture.GetSurfaceLevel(0);
                Surface src = src_texture.GetSurfaceLevel(0);
                SurfaceLoader.FromSurface(dst, src, Filter.None, 0);
                dst.Dispose();
                src.Dispose();
                return(true);
            }catch {
                return(false);
            }
        }
Esempio n. 3
0
        public static Texture CloneTexture(Texture oldTexture, int width, int height, int mips, Format format, Usage usage, Filter filter, Pool pool)
        {
            Texture newTexture = new Texture(oldTexture.Device, width, height, mips, usage, format, pool);

            SurfaceLoader.FromSurface(newTexture.GetSurfaceLevel(0), oldTexture.GetSurfaceLevel(0), filter, 0);
            return(newTexture);
        }
Esempio n. 4
0
        public static void CopyToCubeSide(OpsContext context, CubeTexture newTexture, string srcArg, CubeMapFace face, Filter filter)
        {
            if (srcArg == null || srcArg.Length == 0)
            {
                return;
            }

            OpsTexture src = context.GetTexture(srcArg);

            if (src == null)
            {
                throw new OpsException("Could not find source texture: " + srcArg);
            }

            if (src.Texture is CubeTexture)
            {
                SurfaceLoader.FromSurface(newTexture.GetCubeMapSurface(face, 0), ((CubeTexture)src.Texture).GetCubeMapSurface(face, 0), filter | (src.SRGB?Filter.SrgbIn:0), 0);
            }
            else if (src.Texture is Texture)
            {
                SurfaceLoader.FromSurface(newTexture.GetCubeMapSurface(face, 0), ((Texture)src.Texture).GetSurfaceLevel(0), filter | (src.SRGB?Filter.SrgbIn:0), 0);
            }
            else
            {
                throw new OpsException("Source texture is not a texture2D: " + srcArg);
            }
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
0
        public static CubeTexture CloneCube(CubeTexture oldTexture, int size, int mips, Format format, Usage usage, Filter filter, Pool pool)
        {
            CubeTexture newTexture = new CubeTexture(oldTexture.Device, size, mips, usage, format, pool);

            SurfaceLoader.FromSurface(newTexture.GetCubeMapSurface(CubeMapFace.NegativeX, 0), oldTexture.GetCubeMapSurface(CubeMapFace.NegativeX, 0), filter, 0);
            SurfaceLoader.FromSurface(newTexture.GetCubeMapSurface(CubeMapFace.NegativeY, 0), oldTexture.GetCubeMapSurface(CubeMapFace.NegativeY, 0), filter, 0);
            SurfaceLoader.FromSurface(newTexture.GetCubeMapSurface(CubeMapFace.NegativeZ, 0), oldTexture.GetCubeMapSurface(CubeMapFace.NegativeZ, 0), filter, 0);
            SurfaceLoader.FromSurface(newTexture.GetCubeMapSurface(CubeMapFace.PositiveX, 0), oldTexture.GetCubeMapSurface(CubeMapFace.PositiveX, 0), filter, 0);
            SurfaceLoader.FromSurface(newTexture.GetCubeMapSurface(CubeMapFace.PositiveY, 0), oldTexture.GetCubeMapSurface(CubeMapFace.PositiveY, 0), filter, 0);
            SurfaceLoader.FromSurface(newTexture.GetCubeMapSurface(CubeMapFace.PositiveZ, 0), oldTexture.GetCubeMapSurface(CubeMapFace.PositiveZ, 0), filter, 0);
            return(newTexture);
        }
Esempio n. 7
0
        public void Run(OpsContext context, OpsStatement statement)
        {
            Slice2dArgs args = statement.Arguments as Slice2dArgs;

            ArrayList containers = statement.GetContent(context);

            if (containers.Count != 1)
            {
                throw new OpsException("Src argument does not resolve to 1 texture.  Textures found: " + containers.Count);
            }

            OpsTexture container = containers[0] as OpsTexture;

            OpsConsole.WriteLine("Slicing from texture:{0} and saving as {1}", container.Name, args.Dst);

            OpsTexture result = new OpsTexture();

            result.Name = args.Dst;
            result.SRGB = container.SRGB;

            Texture newTexture = null;

            if (container.Texture is Texture)
            {
                Texture            srcTexture = container.Texture as Texture;
                SurfaceDescription sd         = srcTexture.GetLevelDescription(args.Mips);

                newTexture = new Texture(srcTexture.Device, sd.Width, sd.Height, 1, Usage.None, sd.Format, Pool.Managed);
                SurfaceLoader.FromSurface(newTexture.GetSurfaceLevel(0), srcTexture.GetSurfaceLevel(0), Filter.Dither | Filter.Triangle | (container.SRGB?Filter.Srgb:0), 0);
            }
            else if (container.Texture is VolumeTexture)
            {
                VolumeTexture     srcTexture = container.Texture as VolumeTexture;
                VolumeDescription vd         = srcTexture.GetLevelDescription(args.Mips);

                newTexture = new Texture(srcTexture.Device, vd.Width, vd.Height, 1, Usage.None, vd.Format, Pool.Managed);
                OpsTextureHelper.LoadSurfaceFromVolumeSlice(srcTexture, args.Mips, args.Volume, Filter.Dither | Filter.Triangle | (container.SRGB?Filter.Srgb:0), newTexture.GetSurfaceLevel(0));
            }
            else if (container.Texture is CubeTexture)
            {
                CubeTexture        srcTexture = container.Texture as CubeTexture;
                SurfaceDescription sd         = srcTexture.GetLevelDescription(args.Mips);

                newTexture = new Texture(srcTexture.Device, sd.Width, sd.Height, 1, Usage.None, sd.Format, Pool.Managed);
                SurfaceLoader.FromSurface(newTexture.GetSurfaceLevel(0), srcTexture.GetCubeMapSurface(args.Face, 0), Filter.Dither | Filter.Triangle | (container.SRGB?Filter.Srgb:0), 0);
            }

            result.Texture = newTexture;

            context.AddTexture(result);
        }
Esempio n. 8
0
        public void Run(OpsContext context, OpsStatement statement)
        {
            Splice2dArgs args = statement.Arguments as Splice2dArgs;

            ArrayList srcList = statement.GetContent(context);

            if (srcList.Count != 1)
            {
                throw new OpsException("Could not find the source texture.  1 is require but found " + srcList.Count);
            }

            OpsTexture srcContainer = ((OpsTexture)srcList[0]);

            if (!(srcContainer.Texture is Texture))
            {
                throw new OpsException("Source texture is not 2D");
            }

            Texture srcTexture = srcContainer.Texture as Texture;

            OpsConsole.WriteLine("Splicing texture:{0} into one or more textures", srcContainer.Name);


            ArrayList dstContainers = context.FindTextures(args.Dst);

            foreach (OpsTexture dstContainer in dstContainers)
            {
                if (dstContainer.Texture is Texture)
                {
                    Texture dstTexture = dstContainer.Texture as Texture;

                    SurfaceLoader.FromSurface(dstTexture.GetSurfaceLevel(args.Mips), srcTexture.GetSurfaceLevel(0), Filter.Dither | Filter.Triangle | (srcContainer.SRGB?Filter.SrgbIn:0) | (dstContainer.SRGB?Filter.SrgbOut:0), 0);
                }
                else if (dstContainer.Texture is VolumeTexture)
                {
                    VolumeTexture dstTexture = dstContainer.Texture as VolumeTexture;

                    OpsTextureHelper.LoadVolumeSliceFromSurface(dstTexture, args.Mips, args.Volume, Filter.Dither | Filter.Triangle | (srcContainer.SRGB?Filter.SrgbIn:0) | (dstContainer.SRGB?Filter.SrgbOut:0), srcTexture.GetSurfaceLevel(0));
                }
                else if (dstContainer.Texture is CubeTexture)
                {
                    CubeTexture dstTexture = dstContainer.Texture as CubeTexture;

                    SurfaceLoader.FromSurface(dstTexture.GetCubeMapSurface(args.Face, args.Mips), srcTexture.GetSurfaceLevel(0), Filter.Dither | Filter.Triangle | (srcContainer.SRGB?Filter.SrgbIn:0) | (dstContainer.SRGB?Filter.SrgbOut:0), 0);
                }
            }
        }
Esempio n. 9
0
        private void GenerateGaussianTexture()
        {
            Surface pBlobTemp = null;
            Surface pBlobNew  = null;

            // Create a temporary texture
            Texture texTemp;

            texTemp = new Texture(device, GAUSSIAN_TEXSIZE, GAUSSIAN_TEXSIZE, 1, Usage.Dynamic, Format.R32F, Pool.Default);
            // Create the gaussian texture
            pTexBlob = new Texture(device, GAUSSIAN_TEXSIZE, GAUSSIAN_TEXSIZE, 1, Usage.Dynamic, blobTexFormat, Pool.Default);

            // Create the environment map
            pEnvMap = TextureLoader.FromCubeFile(device, "../../../Media/LobbyCube.dds");

            // Fill in the gaussian texture data
            GraphicsStream Rect = texTemp.LockRectangle(0, LockFlags.None);
            int            u, v;
            float          dx, dy, I;

            for (v = 0; v < GAUSSIAN_TEXSIZE; ++v)
            {
                for (u = 0; u < GAUSSIAN_TEXSIZE; ++u)
                {
                    dx = 2.0f * (float)u / (float)GAUSSIAN_TEXSIZE - 1.0f;
                    dy = 2.0f * (float)v / (float)GAUSSIAN_TEXSIZE - 1.0f;
                    I  = GAUSSIAN_HEIGHT * (float)Math.Exp(-((dx * dx) + (dy * dy)) / GAUSSIAN_DEVIATION);

                    //byte[] data = BitConverter.GetBytes(I);
                    Rect.Write(I);//data, 0, data.Length);
                }
            }

            texTemp.UnlockRectangle(0);

            // Copy the temporary surface to the stored gaussian texture
            pBlobTemp = texTemp.GetSurfaceLevel(0);
            pBlobNew  = pTexBlob.GetSurfaceLevel(0);
            SurfaceLoader.FromSurface(pBlobNew, pBlobTemp, Filter.None, 0);
            //SurfaceLoader.Save("c:/gaussdump.dds", ImageFileFormat.Dds, pBlobTemp);
            //SurfaceLoader.FromFile(pBlobNew, "c:/gaussdump.dds", Filter.None, 0);

            pBlobTemp.Dispose();
            pBlobNew.Dispose();
            texTemp.Dispose();
        }
Esempio n. 10
0
        private void GenerateGaussianTexture()
        {
            Surface pBlobTemp = null;
            Surface pBlobNew  = null;

            // Create a temporary texture
            Texture texTemp;

            texTemp = new Texture(device, GaussianTexsize, GaussianTexsize, 1, Usage.Dynamic, Format.R32F, Pool.Default);
            // Create the gaussian texture
            pTexBlob = new Texture(device, GaussianTexsize, GaussianTexsize, 1, Usage.Dynamic, blobTexFormat, Pool.Default);

            // Fill in the gaussian texture data
            GraphicsStream Rect = texTemp.LockRectangle(0, LockFlags.None);
            float          dx, dy, I;

            int   margin = (int)(GaussianTexsize * 0.1f);
            float size   = GaussianTexsize - margin;

            //Rect.Seek(4 * margin * GaussianTexsize, System.IO.SeekOrigin.Begin);
            for (int v = 0; v < GaussianTexsize; v++)
            {
                for (int u = 0; u < GaussianTexsize; u++)
                {
                    dx = 2.0f * (float)u / (float)GaussianTexsize - 1.0f;
                    dy = 2.0f * (float)v / (float)GaussianTexsize - 1.0f;
                    I  = GaussianHeight * (float)Math.Exp(-(dx * dx + dy * dy) / GaussianDeviation);

                    byte[] data = BitConverter.GetBytes(I);
                    Rect.Write(data, 0, data.Length);
                }
            }

            texTemp.UnlockRectangle(0);

            // Copy the temporary surface to the stored gaussian texture
            pBlobTemp = texTemp.GetSurfaceLevel(0);
            pBlobNew  = pTexBlob.GetSurfaceLevel(0);
            SurfaceLoader.FromSurface(pBlobNew, pBlobTemp, Filter.None, 0);

            pBlobTemp.Dispose();
            pBlobNew.Dispose();
            texTemp.Dispose();
        }
Esempio n. 11
0
        private void RenderCubeMapFace(OpsContext context, CubeTexture newTexture, Surface rt, CubeMapFace face, int mip, int size, bool SRGB)
        {
            context.Device.SetRenderTarget(0, rt);

            ShadeVertex[] vb = new ShadeVertex[]
            {
                ShadeVertex.ForCube(-1.0f, -1.0f, size, face),
                ShadeVertex.ForCube(1.0f, -1.0f, size, face),
                ShadeVertex.ForCube(-1.0f, 1.0f, size, face),
                ShadeVertex.ForCube(1.0f, 1.0f, size, face),
            };

            context.Device.BeginScene();
            context.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vb);
            context.Device.EndScene();

            context.Device.SetRenderTarget(0, context.Device.GetBackBuffer(0, 0, BackBufferType.Mono));

            SurfaceLoader.FromSurface(newTexture.GetCubeMapSurface(face, mip), rt, Filter.None | (SRGB?Filter.SrgbOut:0), 0);
        }
Esempio n. 12
0
        public static void CopyToTexture(OpsContext context, Texture newTexture, string srcArg, Filter filter)
        {
            if (srcArg == null || srcArg.Length == 0)
            {
                return;
            }
            OpsTexture src = context.GetTexture(srcArg);

            if (src == null)
            {
                throw new OpsException("Could not find source texture: " + srcArg);
            }

            if (!(src.Texture is Texture))
            {
                throw new OpsException("Source texture is not a texture2D: " + srcArg);
            }

            Texture srcTex = src.Texture as Texture;

            SurfaceLoader.FromSurface(newTexture.GetSurfaceLevel(0), srcTex.GetSurfaceLevel(0), filter | (src.SRGB?Filter.SrgbIn:0), 0);

            newTexture.GenerateMipSubLevels();
        }
Esempio n. 13
0
        public void Run(OpsContext context, OpsStatement statement)
        {
            ShadeArgs args = statement.Arguments as ShadeArgs;

            ConstantTable constantTable = SetupDevice(context, args);

            EffectHandle hTarget = constantTable.GetConstant(null, "Target");


            ArrayList containers = statement.GetContent(context);

            OpsConsole.WriteLine("Shading textures with \"{0}\"", args.File);

            foreach (OpsTexture container in containers)
            {
                if (hTarget != null)
                {
                    context.Device.SetTexture(
                        constantTable.GetSamplerIndex(hTarget),
                        container.Texture);
                }


                if (container.Texture is Texture)
                {
                    Texture oldTexture = container.Texture as Texture;
                    Texture newTexture = OpsTextureHelper.CloneTexture(oldTexture, Usage.None, Pool.Managed);

                    for (int mip = 0; mip < oldTexture.LevelCount; mip++)
                    {
                        SurfaceDescription sd = oldTexture.GetLevelDescription(mip);

                        CheckFormatValid(sd.Format);

                        Surface rt = context.Device.CreateRenderTarget(sd.Width, sd.Height, sd.Format, MultiSampleType.None, 0, true);

                        context.Device.SetRenderTarget(0, rt);


                        ShadeVertex[] vb = new ShadeVertex[]
                        {
                            ShadeVertex.ForTexture(-1.0f, -1.0f, sd.Width, sd.Height),
                            ShadeVertex.ForTexture(1.0f, -1.0f, sd.Width, sd.Height),
                            ShadeVertex.ForTexture(-1.0f, 1.0f, sd.Width, sd.Height),
                            ShadeVertex.ForTexture(1.0f, 1.0f, sd.Width, sd.Height),
                        };

                        context.Device.BeginScene();
                        context.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vb);
                        context.Device.EndScene();

                        context.Device.SetRenderTarget(0, context.Device.GetBackBuffer(0, 0, BackBufferType.Mono));

                        SurfaceLoader.FromSurface(newTexture.GetSurfaceLevel(mip), rt, Filter.None | (container.SRGB?Filter.SrgbOut:0), 0);
                    }

                    oldTexture.Dispose();
                    container.Texture = newTexture;
                }
                else if (container.Texture is VolumeTexture)
                {
                    VolumeTexture oldTexture = container.Texture as VolumeTexture;
                    VolumeTexture newTexture = OpsTextureHelper.CloneVolume(oldTexture, Usage.None, Pool.Managed);

                    for (int mip = 0; mip < oldTexture.LevelCount; mip++)
                    {
                        VolumeDescription vd = oldTexture.GetLevelDescription(mip);

                        CheckFormatValid(vd.Format);

                        Surface sliceRT = context.Device.CreateRenderTarget(vd.Width, vd.Height, vd.Format, MultiSampleType.None, 0, true);

                        for (int slice = 0; slice < vd.Depth; slice++)
                        {
                            context.Device.SetRenderTarget(0, sliceRT);

                            ShadeVertex[] vb = new ShadeVertex[]
                            {
                                ShadeVertex.ForVolume(-1.0f, -1.0f, slice, vd.Width, vd.Height, vd.Depth),
                                ShadeVertex.ForVolume(1.0f, -1.0f, slice, vd.Width, vd.Height, vd.Depth),
                                ShadeVertex.ForVolume(-1.0f, 1.0f, slice, vd.Width, vd.Height, vd.Depth),
                                ShadeVertex.ForVolume(1.0f, 1.0f, slice, vd.Width, vd.Height, vd.Depth),
                            };

                            context.Device.BeginScene();
                            context.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vb);
                            context.Device.EndScene();

                            context.Device.SetRenderTarget(0, context.Device.GetBackBuffer(0, 0, BackBufferType.Mono));

                            OpsTextureHelper.LoadVolumeSliceFromSurface(newTexture, mip, slice, Filter.None | (container.SRGB?Filter.SrgbOut:0), sliceRT);
                        }

                        sliceRT.Dispose();
                    }

                    oldTexture.Dispose();
                    container.Texture = newTexture;
                }
                else if (container.Texture is CubeTexture)
                {
                    CubeTexture oldTexture = container.Texture as CubeTexture;
                    CubeTexture newTexture = OpsTextureHelper.CloneCube(oldTexture, Usage.None, Pool.Managed);

                    for (int mip = 0; mip < oldTexture.LevelCount; mip++)
                    {
                        SurfaceDescription sd = oldTexture.GetLevelDescription(mip);

                        CheckFormatValid(sd.Format);

                        Surface rt = context.Device.CreateRenderTarget(sd.Width, sd.Height, sd.Format, MultiSampleType.None, 0, true);

                        RenderCubeMapFace(context, newTexture, rt, CubeMapFace.PositiveX, mip, sd.Width, container.SRGB);
                        RenderCubeMapFace(context, newTexture, rt, CubeMapFace.PositiveY, mip, sd.Width, container.SRGB);
                        RenderCubeMapFace(context, newTexture, rt, CubeMapFace.PositiveZ, mip, sd.Width, container.SRGB);
                        RenderCubeMapFace(context, newTexture, rt, CubeMapFace.NegativeX, mip, sd.Width, container.SRGB);
                        RenderCubeMapFace(context, newTexture, rt, CubeMapFace.NegativeY, mip, sd.Width, container.SRGB);
                        RenderCubeMapFace(context, newTexture, rt, CubeMapFace.NegativeZ, mip, sd.Width, container.SRGB);
                    }

                    oldTexture.Dispose();

                    container.Texture = newTexture;
                }
            }
        }
Esempio n. 14
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();
            }
        }
Esempio n. 15
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();
            }
        }