Exemplo n.º 1
0
        public DX11SliceRenderTarget(DX11RenderContext context, DX11Texture2D texture, int sliceindex)
        {
            this.isowner = false;
            this.context = context;
            this.parent  = texture;
            this.desc    = texture.Description;

            RenderTargetViewDescription rtd = new RenderTargetViewDescription()
            {
                ArraySize       = 1,
                Dimension       = RenderTargetViewDimension.Texture2DArray,
                Format          = texture.Format,
                MipSlice        = 0,
                FirstArraySlice = sliceindex,
            };

            this.RTV = new RenderTargetView(context.Device, this.parent.Resource, rtd);

            ShaderResourceViewDescription srvd = new ShaderResourceViewDescription()
            {
                ArraySize       = 1,
                Dimension       = ShaderResourceViewDimension.Texture2DArray,
                Format          = texture.Format,
                FirstArraySlice = sliceindex,
                MipLevels       = texture.Resource.Description.MipLevels,
                MostDetailedMip = 0
            };

            this.SRV = new ShaderResourceView(context.Device, this.parent.Resource, srvd);
        }
Exemplo n.º 2
0
        public static DX11Texture2D FromStream(DX11RenderContext context, Stream s, int size)
        {
            try
            {
                Texture2D tex = Texture2D.FromStream(context.Device, s, size);

                if (tex.Description.ArraySize == 1)
                {
                    DX11Texture2D res = new DX11Texture2D();
                    res.context  = context;
                    res.Resource = tex;
                    res.SRV      = new ShaderResourceView(context.Device, res.Resource);
                    res.desc     = res.Resource.Description;
                    res.isowner  = true;
                    return(res);
                }
                else
                {
                    if (tex.Description.OptionFlags.HasFlag(ResourceOptionFlags.TextureCube))
                    {
                        return(new DX11TextureCube(context, tex));
                    }
                    else
                    {
                        return(new DX11TextureArray2D(context, tex));
                    }
                }
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 3
0
        public DX11MipSliceRenderTarget(DX11RenderContext context, DX11Texture2D texture, int mipindex, int w, int h)
        {
            this.context = context;
            this.Width   = w;
            this.Height  = h;
            this.Depth   = 1;

            RenderTargetViewDescription rtd = new RenderTargetViewDescription()
            {
                Dimension = RenderTargetViewDimension.Texture2D,
                Format    = texture.Format,
                MipSlice  = mipindex
            };

            ShaderResourceViewDescription srvd = new ShaderResourceViewDescription();

            srvd.Dimension       = ShaderResourceViewDimension.Texture2D;
            srvd.MipLevels       = 1;
            srvd.MostDetailedMip = mipindex;
            srvd.Format          = texture.Format;


            this.RTV = new RenderTargetView(context.Device, texture.Resource, rtd);
            this.SRV = new ShaderResourceView(context.Device, texture.Resource, srvd);
        }
Exemplo n.º 4
0
        public DX11MipSliceRenderTarget2D(DX11RenderContext context, DX11Texture2D texture, int mipindex, int w, int h)
        {
            this.context  = context;
            this.Resource = texture.Resource;

            RenderTargetViewDescription rtd = new RenderTargetViewDescription()
            {
                Dimension = RenderTargetViewDimension.Texture2D,
                Format    = texture.Format,
                MipSlice  = mipindex
            };

            UnorderedAccessViewDescription uavd = new UnorderedAccessViewDescription()
            {
                Dimension = UnorderedAccessViewDimension.Texture2D,
                Format    = texture.Format,
                MipSlice  = mipindex,
            };

            ShaderResourceViewDescription srvd = new ShaderResourceViewDescription();

            srvd.Dimension       = ShaderResourceViewDimension.Texture2D;
            srvd.MipLevels       = 1;
            srvd.MostDetailedMip = mipindex;
            srvd.Format          = texture.Format;

            this.SRV = new ShaderResourceView(context.Device, texture.Resource, srvd);
        }
Exemplo n.º 5
0
        public static DX11Texture2D FromResource(DX11RenderContext context, Assembly assembly, string path)
        {
            try
            {
                Stream    s   = assembly.GetManifestResourceStream(path);
                Texture2D tex = Texture2D.FromStream(context.Device, s, (int)s.Length);

                if (tex.Description.ArraySize == 1)
                {
                    DX11Texture2D res = new DX11Texture2D();
                    res.context  = context;
                    res.Resource = tex;
                    res.SRV      = new ShaderResourceView(context.Device, res.Resource);
                    res.desc     = res.Resource.Description;
                    res.isowner  = true;
                    return(res);
                }
                else
                {
                    if (tex.Description.OptionFlags.HasFlag(ResourceOptionFlags.TextureCube))
                    {
                        return(new DX11TextureCube(context, tex));
                    }
                    else
                    {
                        return(new DX11TextureArray2D(context, tex));
                    }
                }
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 6
0
        public static DX11Texture2D FromFile(DX11RenderContext context, string path, ImageLoadInformation loadinfo)
        {
            try
            {
                Texture2D tex = Texture2D.FromFile(context.Device, path, loadinfo);

                if (tex.Description.ArraySize == 1)
                {
                    DX11Texture2D res = new DX11Texture2D();
                    res.context  = context;
                    res.Resource = tex;
                    res.SRV      = new ShaderResourceView(context.Device, res.Resource);
                    res.desc     = res.Resource.Description;
                    res.isowner  = true;
                    return(res);
                }
                else
                {
                    if (tex.Description.OptionFlags.HasFlag(ResourceOptionFlags.TextureCube))
                    {
                        return(new DX11TextureCube(context, tex));
                    }
                    else
                    {
                        return(new DX11TextureArray2D(context, tex));
                    }
                }
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 7
0
        public static DX11Texture2D FromReference(DxDevice device, Texture2D texture, ShaderResourceView view)
        {
            DX11Texture2D result = new DX11Texture2D();

            result.description = texture.Description;
            result.ShaderView  = view;
            result.Texture     = texture;
            return(result);
        }
Exemplo n.º 8
0
        public static void SaveToFileCompressed(DX11RenderContext device, DX11Texture2D texture, string path, DdsBlockType blockType)
        {
            long retcode = NativeMethods.SaveCompressedTextureToFile(device.Device.ComPointer, device.CurrentDeviceContext.ComPointer,
                                                                     texture.Resource.ComPointer, path, (int)blockType);

            if (retcode < 0)
            {
                throw new Exception("Failed to Save Texture");
            }
        }
Exemplo n.º 9
0
        SaveToMemoryCompressed(DX11RenderContext device, DX11Texture2D texture, DdsBlockType blockType, out IntPtr data, out int size, out IntPtr blob)
        {
            long retcode = NativeMethods.SaveCompressedTextureToMemory(device.Device.ComPointer, device.CurrentDeviceContext.ComPointer,
                                                                       texture.Resource.ComPointer, (int)blockType, out data, out size, out blob);

            if (retcode < 0)
            {
                throw new Exception("Failed to Save Texture");
            }
        }
Exemplo n.º 10
0
        public static DX11Texture2D FromDescription(DX11RenderContext context, Texture2DDescription desc)
        {
            DX11Texture2D res = new DX11Texture2D();

            res.context  = context;
            res.Resource = new Texture2D(context.Device, desc);
            res.isowner  = true;
            res.desc     = desc;
            res.SRV      = new ShaderResourceView(context.Device, res.Resource);

            return(res);
        }
Exemplo n.º 11
0
        public static DX11Texture2D FromTextureAndSRV(DX11RenderContext context, Texture2D tex, ShaderResourceView srv)
        {
            Texture2DDescription desc = tex.Description;

            DX11Texture2D res = new DX11Texture2D();

            res.context  = context;
            res.Resource = tex;
            res.SRV      = srv;
            res.desc     = desc;
            res.isowner  = false;
            return(res);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Creates a reference resource from texture and view.
        /// This does take ownership over the resource. Calling dispose on the object will effectively release ressources
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="texture">A valid Direc3D11 texture</param>
        /// <param name="shaderView">A valid Direct3D11 view (that should match the texture).</param>
        /// <returns>Packed Texture object</returns>
        public static DX11Texture2D TakeOwnership(DX11RenderContext context, Texture2D texture, ShaderResourceView shaderView)
        {
            Texture2DDescription desc = texture.Description;

            DX11Texture2D res = new DX11Texture2D();

            res.context  = context;
            res.Resource = texture;
            res.SRV      = shaderView;
            res.desc     = desc;
            res.isowner  = true;
            return(res);
        }
Exemplo n.º 13
0
        public static DX11Texture2D CreateStaging(DxDevice device, IDxTexture2D source)
        {
            var desc = source.Texture.Description;

            desc.BindFlags      = BindFlags.None;
            desc.CpuAccessFlags = CpuAccessFlags.Read;
            desc.Usage          = ResourceUsage.Staging;
            desc.OptionFlags    = ResourceOptionFlags.None;

            DX11Texture2D texture = new DX11Texture2D();

            texture.Texture     = new Texture2D(device.Device, desc);
            texture.description = texture.Texture.Description;
            return(texture);
        }
Exemplo n.º 14
0
        public static DX11Texture2D FromSharedHandle(DX11RenderContext context, IntPtr sharedHandle)
        {
            Texture2D          tex = context.Device.OpenSharedResource <Texture2D>(sharedHandle);
            ShaderResourceView srv = new ShaderResourceView(context.Device, tex);

            DX11Texture2D result = new DX11Texture2D();

            result.context  = context;
            result.Resource = tex;
            result.SRV      = srv;
            result.desc     = tex.Description;

            result.isowner = true;
            return(result);
        }
Exemplo n.º 15
0
        public DX11SliceDepthStencil(DX11RenderContext context, DX11Texture2D texture, int sliceindex, Format depthformat)
        {
            this.context = context;
            this.parent  = texture;

            DepthStencilViewDescription dsvd = new DepthStencilViewDescription()
            {
                ArraySize       = 1,
                Dimension       = DepthStencilViewDimension.Texture2DArray,
                Format          = depthformat,
                MipSlice        = 0,
                FirstArraySlice = sliceindex
            };

            this.DSV = new DepthStencilView(context.Device, this.parent.Resource, dsvd);
        }
Exemplo n.º 16
0
        public static DX11Texture2D LoadFromMemory(DxDevice device, IntPtr dataPointer, int dataLength)
        {
            IntPtr resource;

            NativeMethods.LoadTextureFromMemory(device.Device.NativePointer, dataPointer, dataLength, out resource);

            if (resource != IntPtr.Zero)
            {
                Texture2D          texture = Texture2D.FromPointer <Texture2D>(resource);
                ShaderResourceView srv     = new ShaderResourceView(device, texture);
                return(DX11Texture2D.FromReference(device, texture, srv));
            }
            else
            {
                throw new Exception("Failed to load texture");
            }
        }
Exemplo n.º 17
0
        public static DX11Texture2D LoadFromFile(DxDevice device, string path, bool mips = true)
        {
            IntPtr resource;
            int    levels = mips ? 0 : 1;

            NativeMethods.LoadTextureFromFile(device.Device.NativePointer, path, out resource, levels);

            if (resource != IntPtr.Zero)
            {
                Texture2D          texture = Texture2D.FromPointer <Texture2D>(resource);
                ShaderResourceView srv     = new ShaderResourceView(device, texture);
                return(DX11Texture2D.FromReference(device, texture, srv));
            }
            else
            {
                throw new Exception("Failed to load texture");
            }
        }
Exemplo n.º 18
0
        public static DX11Texture2D CreateDynamic(DxDevice device, int width, int height, Format format)
        {
            var desc = new Texture2DDescription()
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.Write,
                Format            = format,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                Usage             = ResourceUsage.Dynamic,
                Width             = width,
                Height            = height,
                SampleDescription = new SampleDescription(1, 0)
            };

            DX11Texture2D texture = new DX11Texture2D();

            texture.Texture     = new Texture2D(device.Device, desc);
            texture.description = texture.Texture.Description;
            texture.ShaderView  = new ShaderResourceView(device.Device, texture.Texture);
            return(texture);
        }
Exemplo n.º 19
0
        public DX11DepthStencil(DX11RenderContext context, int w, int h, SampleDescription sd, Format format)
        {
            this.context = context;
            var depthBufferDesc = new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.DepthStencil | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = DepthFormatsHelper.GetGenericTextureFormat(format),
                Height            = h,
                Width             = w,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = sd,
                Usage             = ResourceUsage.Default
            };

            this.Resource = new Texture2D(context.Device, depthBufferDesc);

            ShaderResourceViewDescription srvd = new ShaderResourceViewDescription()
            {
                ArraySize       = 1,
                Format          = DepthFormatsHelper.GetSRVFormat(format),
                Dimension       = sd.Count == 1 ? ShaderResourceViewDimension.Texture2D : ShaderResourceViewDimension.Texture2DMultisampled,
                MipLevels       = 1,
                MostDetailedMip = 0
            };

            this.SRV = new ShaderResourceView(context.Device, this.Resource, srvd);


            if (format == Format.D24_UNorm_S8_UInt || format == Format.D32_Float_S8X24_UInt)
            {
                ShaderResourceViewDescription stencild = new ShaderResourceViewDescription()
                {
                    ArraySize       = 1,
                    Format          = format == Format.D24_UNorm_S8_UInt ? SlimDX.DXGI.Format.X24_Typeless_G8_UInt : Format.X32_Typeless_G8X24_UInt,
                    Dimension       = sd.Count == 1 ? ShaderResourceViewDimension.Texture2D : ShaderResourceViewDimension.Texture2DMultisampled,
                    MipLevels       = 1,
                    MostDetailedMip = 0
                };

                this.stencilview = new ShaderResourceView(this.context.Device, this.Resource, stencild);

                this.Stencil = DX11Texture2D.FromTextureAndSRV(this.context, this.Resource, this.stencilview);
            }
            else
            {
                //Just pass depth instead
                this.Stencil = DX11Texture2D.FromTextureAndSRV(this.context, this.Resource, this.SRV);
            }

            DepthStencilViewDescription dsvd = new DepthStencilViewDescription()
            {
                Format    = DepthFormatsHelper.GetDepthFormat(format),
                Dimension = sd.Count == 1 ? DepthStencilViewDimension.Texture2D : DepthStencilViewDimension.Texture2DMultisampled,
                MipSlice  = 0
            };

            this.DSV = new DepthStencilView(context.Device, this.Resource, dsvd);

            //Read only dsv only supported in dx11 minimum
            if (context.IsFeatureLevel11)
            {
                dsvd.Flags = DepthStencilViewFlags.ReadOnlyDepth;
                if (format == Format.D24_UNorm_S8_UInt || format == Format.D32_Float_S8X24_UInt)
                {
                    dsvd.Flags |= DepthStencilViewFlags.ReadOnlyStencil;
                }

                this.ReadOnlyDSV = new DepthStencilView(context.Device, this.Resource, dsvd);
            }

            this.desc    = depthBufferDesc;
            this.isowner = true;
        }
Exemplo n.º 20
0
        public static void SaveToFileCompressed(RenderDevice device, RenderContext context, DX11Texture2D texture, string path, DdsBlockType blockType)
        {
            long retcode = NativeMethods.SaveCompressedTextureToFile(device.Device.NativePointer, context.Context.NativePointer,
                                                                     texture.Texture.NativePointer, path, (int)blockType);

            if (retcode < 0)
            {
                throw new Exception("Failed to Save Texture");
            }
        }
Exemplo n.º 21
0
        public static void SaveToMemoryCompressed(RenderDevice device, RenderContext context, DX11Texture2D texture, DdsBlockType blockType, out IntPtr data, out int size, out IntPtr blob)
        {
            long retcode = NativeMethods.SaveCompressedTextureToMemory(device.Device.NativePointer, context.Context.NativePointer,
                                                                       texture.Texture.NativePointer, (int)blockType, out data, out size, out blob);

            if (retcode < 0)
            {
                throw new Exception("Failed to Save Texture");
            }
        }
Exemplo n.º 22
0
 public static DX11Texture2D FromFile(DX11RenderContext context, string path)
 {
     return(DX11Texture2D.FromFile(context, path, ImageLoadInformation.FromDefaults()));
 }
Exemplo n.º 23
0
 public void CopyFrom(DX11Texture2D tex)
 {
     this.context.CurrentDeviceContext.CopyResource(tex.Resource, this.Resource);
 }
Exemplo n.º 24
0
        public DX11DepthStencil(DxDevice device, int w, int h, SampleDescription sd, eDepthFormat depthformat = eDepthFormat.d24s8)
        {
            this.device = device;
            var depthBufferDesc = new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.DepthStencil | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = depthformat.GetTypeLessFormat(),
                Height            = h,
                Width             = w,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = sd,
                Usage             = ResourceUsage.Default
            };

            this.Texture      = new Texture2D(device.Device, depthBufferDesc);
            this.resourceDesc = this.Texture.Description;
            this.DepthFormat  = depthformat;

            ShaderResourceViewDescription srvd = new ShaderResourceViewDescription()
            {
                Format    = depthformat.GetSRVFormat(),
                Dimension = sd.Count == 1 ? ShaderResourceViewDimension.Texture2D : ShaderResourceViewDimension.Texture2DMultisampled,
            };

            if (sd.Count == 1)
            {
                srvd.Texture2D.MipLevels       = 1;
                srvd.Texture2D.MostDetailedMip = 0;
            }

            this.ShaderView = new ShaderResourceView(device.Device, this.Texture, srvd);


            if (depthformat.HasStencil())
            {
                ShaderResourceViewDescription stencild = new ShaderResourceViewDescription()
                {
                    Dimension = sd.Count == 1 ? ShaderResourceViewDimension.Texture2D : ShaderResourceViewDimension.Texture2DMultisampled,
                    Format    = depthformat.GetStencilSRVFormat(),
                };

                if (sd.Count == 1)
                {
                    stencild.Texture2D.MipLevels       = 1;
                    stencild.Texture2D.MostDetailedMip = 0;
                }

                ShaderResourceView stencilview = new ShaderResourceView(this.device.Device, this.Texture, stencild);

                this.Stencil = DX11Texture2D.FromReference(this.device, this.Texture, stencilview);
            }
            else
            {
                //Just pass depth instead
                this.Stencil = null;
            }

            DepthStencilViewDescription dsvd = new DepthStencilViewDescription()
            {
                Format    = depthformat.GetDepthFormat(),
                Dimension = sd.Count == 1 ? DepthStencilViewDimension.Texture2D : DepthStencilViewDimension.Texture2DMultisampled,
            };

            this.DepthView = new DepthStencilView(device.Device, this.Texture, dsvd);

            //Read only dsv only supported in dx11 minimum
            if (device.IsFeatureLevel11)
            {
                dsvd.Flags = DepthStencilViewFlags.ReadOnlyDepth;
                if (depthformat.HasStencil())
                {
                    dsvd.Flags |= DepthStencilViewFlags.ReadOnlyStencil;
                }

                this.ReadOnlyView = new DepthStencilView(device.Device, this.Texture, dsvd);
            }
        }