CalculateMipLevels() public static method

Calculates total number of mip levels
public static CalculateMipLevels ( int width, int height, int depth ) : int
width int
height int
depth int
return int
示例#1
0
        /// <summary>
        /// Creates texture
        /// </summary>
        /// <param name="device"></param>
        public TextureCube(GraphicsDevice device, int size, ColorFormat format, bool mips, bool srgb = false) : base(device)
        {
            this.Width    = size;
            this.Depth    = 1;
            this.Height   = size;
            this.format   = format;
            this.mipCount = mips ? ShaderResource.CalculateMipLevels(Width, Height) : 1;

            //Log.Warning("CHECK ARRAY SIZE!");

            var texDesc = new Texture2DDescription();

            texDesc.ArraySize                 = 6;
            texDesc.BindFlags                 = BindFlags.ShaderResource;
            texDesc.CpuAccessFlags            = CpuAccessFlags.None;
            texDesc.Format                    = MakeTypeless(Converter.Convert(format));
            texDesc.Height                    = Height;
            texDesc.MipLevels                 = mipCount;
            texDesc.OptionFlags               = ResourceOptionFlags.TextureCube;
            texDesc.SampleDescription.Count   = 1;
            texDesc.SampleDescription.Quality = 0;
            texDesc.Usage = ResourceUsage.Default;
            texDesc.Width = Width;

            texCube = new D3D.Texture2D(device.Device, texDesc);
            SRV     = new ShaderResourceView(device.Device, texCube);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="device"></param>
        /// <param name="size"></param>
        /// <param name="count"></param>
        /// <param name="format"></param>
        /// <param name="mips"></param>
        public TextureCubeArray(GraphicsDevice device, int size, int count, ColorFormat format, bool mips) : base(device)
        {
            if (count > 2048 / 6)
            {
                throw new GraphicsException("Too much elements in texture array");
            }

            this.Width    = size;
            this.Depth    = 1;
            this.Height   = size;
            this.MipCount = mips ? ShaderResource.CalculateMipLevels(Width, Height) : 1;

            var texDesc = new Texture2DDescription();

            texDesc.ArraySize                 = 6 * count;
            texDesc.BindFlags                 = BindFlags.ShaderResource;
            texDesc.CpuAccessFlags            = CpuAccessFlags.None;
            texDesc.Format                    = MakeTypeless(Converter.Convert(format));
            texDesc.Height                    = Height;
            texDesc.MipLevels                 = 0;
            texDesc.OptionFlags               = ResourceOptionFlags.TextureCube;
            texDesc.SampleDescription.Count   = 1;
            texDesc.SampleDescription.Quality = 0;
            texDesc.Usage = ResourceUsage.Default;
            texDesc.Width = Width;


            texCubeArray = new D3D.Texture2D(device.Device, texDesc);
            SRV          = new ShaderResourceView(device.Device, texCubeArray);
        }
示例#3
0
        /// <summary>
        /// Creates texture
        /// </summary>
        /// <param name="device"></param>
        public Texture2D(GraphicsDevice device, int width, int height, ColorFormat format, bool mips, bool srgb = false) : base(device)
        {
            this.Width    = width;
            this.Height   = height;
            this.Depth    = 1;
            this.format   = format;
            this.mipCount = mips ? ShaderResource.CalculateMipLevels(Width, Height) : 1;

            var texDesc = new Texture2DDescription();

            texDesc.ArraySize                 = 1;
            texDesc.BindFlags                 = BindFlags.ShaderResource;
            texDesc.CpuAccessFlags            = CpuAccessFlags.None;
            texDesc.Format                    = srgb ? MakeSRgb(Converter.Convert(format)) : Converter.Convert(format);
            texDesc.Height                    = Height;
            texDesc.MipLevels                 = mipCount;
            texDesc.OptionFlags               = ResourceOptionFlags.None;
            texDesc.SampleDescription.Count   = 1;
            texDesc.SampleDescription.Quality = 0;
            texDesc.Usage = ResourceUsage.Default;
            texDesc.Width = Width;

            lock (device.DeviceContext) {
                tex2D = new D3D.Texture2D(device.Device, texDesc);
                SRV   = new ShaderResourceView(device.Device, tex2D);
            }
        }
示例#4
0
        /// <summary>
        /// Creates texture
        /// </summary>
        /// <param name="device"></param>
        public VolumeRWTexture(GraphicsDevice device, int width, int height, int depth, ColorFormat format, bool mips) : base(device)
        {
            this.Width    = width;
            this.Height   = height;
            this.Depth    = depth;
            this.format   = format;
            this.mipCount = mips ? ShaderResource.CalculateMipLevels(Width, Height, Depth) : 1;

            var texDesc = new Texture3DDescription();

            texDesc.BindFlags      = BindFlags.ShaderResource | BindFlags.UnorderedAccess;
            texDesc.CpuAccessFlags = CpuAccessFlags.None;
            texDesc.Format         = Converter.Convert(format);
            texDesc.Height         = Height;
            texDesc.MipLevels      = mipCount;
            texDesc.OptionFlags    = ResourceOptionFlags.None;
            texDesc.Usage          = ResourceUsage.Default;
            texDesc.Width          = Width;
            texDesc.Depth          = Depth;

            var uavDesc = new UnorderedAccessViewDescription();

            uavDesc.Format                = Converter.Convert(format);
            uavDesc.Dimension             = UnorderedAccessViewDimension.Texture3D;
            uavDesc.Texture3D.FirstWSlice = 0;
            uavDesc.Texture3D.MipSlice    = 0;
            uavDesc.Texture3D.WSize       = depth;

            tex3D = new D3D.Texture3D(device.Device, texDesc);
            SRV   = new D3D.ShaderResourceView(device.Device, tex3D);
            uav   = new UnorderedAccessView(device.Device, tex3D, uavDesc);
        }
示例#5
0
        /// <summary>
        /// Creates texture
        /// </summary>
        /// <param name="device"></param>
        public Texture3D(GraphicsDevice device, int width, int height, int depth, ColorFormat format, bool mips, bool srgb = false) : base(device)
        {
            this.Width    = width;
            this.Height   = height;
            this.Depth    = depth;
            this.format   = format;
            this.mipCount = mips ? ShaderResource.CalculateMipLevels(Width, Height, Depth) : 1;

            var texDesc = new Texture3DDescription();

            texDesc.BindFlags      = BindFlags.ShaderResource;
            texDesc.CpuAccessFlags = CpuAccessFlags.None;
            texDesc.Format         = Converter.Convert(format);
            texDesc.Height         = Height;
            texDesc.MipLevels      = mipCount;
            texDesc.OptionFlags    = ResourceOptionFlags.None;
            texDesc.Usage          = ResourceUsage.Default;
            texDesc.Width          = Width;
            texDesc.Depth          = Depth;

            tex3D = new D3D.Texture3D(device.Device, texDesc);
            SRV   = new D3D.ShaderResourceView(device.Device, tex3D);
        }
示例#6
0
        /// <summary>
        /// Creates render target
        /// </summary>
        /// <param name="?"></param>
        /// <param name="format"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="samples"></param>
        /// <param name="mips"></param>
        /// <param name="debugName"></param>
        void Create(ColorFormat format, int size, int samples, bool mips, string debugName)
        {
            bool msaa = samples > 1;

            CheckSamplesCount(samples);

            if (mips && samples > 1)
            {
                throw new ArgumentException("Render target should be multisampler either mipmapped");
            }

            SampleCount = samples;

            Format      = format;
            SampleCount = samples;
            Width       = size;
            Height      = size;
            Depth       = 1;
            MipCount    = mips ? ShaderResource.CalculateMipLevels(Width, Height) : 1;

            var texDesc = new Texture2DDescription();

            texDesc.Width             = Width;
            texDesc.Height            = Height;
            texDesc.ArraySize         = 6;
            texDesc.BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource;
            texDesc.CpuAccessFlags    = CpuAccessFlags.None;
            texDesc.Format            = Converter.Convert(format);
            texDesc.MipLevels         = mips ? MipCount : 1;
            texDesc.OptionFlags       = ResourceOptionFlags.TextureCube | (mips ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None);
            texDesc.SampleDescription = new DXGI.SampleDescription(samples, 0);
            texDesc.Usage             = ResourceUsage.Default;


            texCube = new D3D.Texture2D(device.Device, texDesc);
            SRV     = new ShaderResourceView(device.Device, texCube);


            texDesc.BindFlags      = BindFlags.None;
            texDesc.CpuAccessFlags = CpuAccessFlags.Write | CpuAccessFlags.Read;
            texDesc.Usage          = ResourceUsage.Staging;
            texDesc.OptionFlags    = ResourceOptionFlags.TextureCube;

            staging = new D3D.Texture2D(device.Device, texDesc);


            //
            //	Top mipmap level :
            //
            cubeMipShaderResources = new ShaderResource[MipCount];

            for (int mip = 0; mip < MipCount; mip++)
            {
                var srvDesc = new ShaderResourceViewDescription();
                srvDesc.TextureCube.MipLevels       = 1;
                srvDesc.TextureCube.MostDetailedMip = mip;
                srvDesc.Format    = Converter.Convert(format);
                srvDesc.Dimension = ShaderResourceViewDimension.TextureCube;

                cubeMipShaderResources[mip] = new ShaderResource(device, new ShaderResourceView(device.Device, texCube, srvDesc), size >> mip, size >> mip, 1);
            }



            //
            //	Create surfaces :
            //
            surfaces = new RenderTargetSurface[MipCount, 6];

            for (int mip = 0; mip < MipCount; mip++)
            {
                int width  = GetMipSize(Width, mip);
                int height = GetMipSize(Height, mip);

                for (int face = 0; face < 6; face++)
                {
                    var rtvDesc = new RenderTargetViewDescription();
                    rtvDesc.Texture2DArray.MipSlice        = mip;
                    rtvDesc.Texture2DArray.FirstArraySlice = face;
                    rtvDesc.Texture2DArray.ArraySize       = 1;
                    rtvDesc.Dimension = msaa ? RenderTargetViewDimension.Texture2DMultisampledArray : RenderTargetViewDimension.Texture2DArray;
                    rtvDesc.Format    = Converter.Convert(format);

                    var rtv = new RenderTargetView(device.Device, texCube, rtvDesc);

                    int subResId = Resource.CalculateSubResourceIndex(mip, face, MipCount);

                    surfaces[mip, face] = new RenderTargetSurface(rtv, null, texCube, subResId, format, Width, Height, samples);

                    GraphicsDevice.Clear(surfaces[mip, face], Color4.Zero);
                }
            }
        }
示例#7
0
        /// <summary>
        /// Creates render target
        /// </summary>
        /// <param name="?"></param>
        /// <param name="format"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="samples"></param>
        /// <param name="mips"></param>
        /// <param name="debugName"></param>
        void Create(ColorFormat format, int width, int height, int samples, int mips, bool enableRWBuffer)
        {
            //Log.Debug("RenderTarget2D: f:{0} w:{1} h:{2} s:{3}{4}{5}", format, width, height, samples, mips?" mips":"", enableRWBuffer?" uav":"" );

            bool msaa = samples > 1;

            CheckSamplesCount(samples);

            if (mips != 1 && samples > 1)
            {
                throw new ArgumentException("Render target should be multisampler either mipmapped");
            }

            SampleCount = samples;

            Format      = format;
            SampleCount = samples;
            Width       = width;
            Height      = height;
            Depth       = 1;
            MipCount    = mips == 0 ? ShaderResource.CalculateMipLevels(width, height) : mips;

            var texDesc = new Texture2DDescription();

            texDesc.Width             = width;
            texDesc.Height            = height;
            texDesc.ArraySize         = 1;
            texDesc.BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource;
            texDesc.CpuAccessFlags    = CpuAccessFlags.None;
            texDesc.Format            = Converter.Convert(format);
            texDesc.MipLevels         = MipCount;
            texDesc.OptionFlags       = (mips != 1) ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None;
            texDesc.SampleDescription = new DXGI.SampleDescription(samples, 0);
            texDesc.Usage             = ResourceUsage.Default;

            if (enableRWBuffer)
            {
                texDesc.BindFlags |= BindFlags.UnorderedAccess;
            }


            tex2D = new D3D.Texture2D(device.Device, texDesc);
            SRV   = new ShaderResourceView(device.Device, tex2D);



            //
            //	Create surfaces :
            //
            surfaces = new RenderTargetSurface[MipCount];

            for (int i = 0; i < MipCount; i++)
            {
                width  = GetMipSize(Width, i);
                height = GetMipSize(Height, i);

                var rtvDesc = new RenderTargetViewDescription();
                rtvDesc.Texture2D.MipSlice = i;
                rtvDesc.Dimension          = msaa ? RenderTargetViewDimension.Texture2DMultisampled : RenderTargetViewDimension.Texture2D;
                rtvDesc.Format             = Converter.Convert(format);

                var rtv = new RenderTargetView(device.Device, tex2D, rtvDesc);

                UnorderedAccessView uav = null;

                if (enableRWBuffer)
                {
                    var uavDesc = new UnorderedAccessViewDescription();
                    uavDesc.Buffer.ElementCount = width * height;
                    uavDesc.Buffer.FirstElement = 0;
                    uavDesc.Buffer.Flags        = UnorderedAccessViewBufferFlags.None;
                    uavDesc.Dimension           = UnorderedAccessViewDimension.Texture2D;
                    uavDesc.Format             = Converter.Convert(format);
                    uavDesc.Texture2D.MipSlice = i;

                    uav = new UnorderedAccessView(device.Device, tex2D, uavDesc);
                }

                surfaces[i] = new RenderTargetSurface(rtv, uav, tex2D, i, format, width, height, samples);
            }
        }