Example #1
0
        // has 6 rtv subresources and mipLevels * 6 srv/uav subresources
        internal static RwTexId CreateCubemap(int resolution, Format resourceFormat, string debugName = null)
        {
            int mipLevels = 1;
            while ((resolution >> mipLevels) > 0)
            {
                ++mipLevels;
            }

            var desc = new Texture2DDescription
            {
                ArraySize = 6,
                BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget | BindFlags.UnorderedAccess,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = resourceFormat,
                MipLevels = mipLevels,
                Usage = ResourceUsage.Default,
                Width = resolution,
                Height = resolution,
                OptionFlags = ResourceOptionFlags.TextureCube,
                SampleDescription = new SampleDescription(1, 0)
            };

            var handle = new RwTexId { Index = Textures.Allocate() };
            Textures.Data[handle.Index] = new MyRwTextureInfo { Description2D = desc };
            Textures.Data[handle.Index].Resource = new Texture2D(MyRender11.Device, desc);

            var res = Textures.Data[handle.Index].Resource;
            Srvs[handle] = new MySrvInfo { Description = null, View = new ShaderResourceView(MyRender11.Device, Textures.Data[handle.Index].Resource) };
            Index.Add(handle);

            var srvDesc = new ShaderResourceViewDescription();
            srvDesc.Dimension = ShaderResourceViewDimension.Texture2DArray;
            srvDesc.Format = resourceFormat;
            srvDesc.Texture2DArray.MipLevels = 1;
            srvDesc.Texture2DArray.ArraySize = 1;

            var uavDesc = new UnorderedAccessViewDescription();
            uavDesc.Dimension = UnorderedAccessViewDimension.Texture2DArray;
            uavDesc.Format = resourceFormat;
            uavDesc.Texture2DArray.ArraySize = 1;

            for (int m = 0; m < mipLevels; ++m)
            {
                for (int i = 0; i < 6; i++)
                {
                    var subresource = i * mipLevels + m;

                    srvDesc.Texture2DArray.FirstArraySlice = i;
                    srvDesc.Texture2DArray.MostDetailedMip = m;

                    SubresourceSrvs[new MySubresourceId { Id = handle, Subresource = subresource }] = new MySrvInfo
                    {
                        Description = srvDesc,
                        View = new ShaderResourceView(MyRender11.Device, res, srvDesc)
                    };

                    uavDesc.Texture2DArray.FirstArraySlice = i;
                    uavDesc.Texture2DArray.MipSlice = m;

                    SubresourceUavs[new MySubresourceId { Id = handle, Subresource = subresource }] = new MyUavInfo
                    {
                        Description = uavDesc,
                        View = new UnorderedAccessView(MyRender11.Device, res, uavDesc)
                    };
                }
            }

            var rtvDesc = new RenderTargetViewDescription();
            rtvDesc.Dimension = RenderTargetViewDimension.Texture2DArray;
            rtvDesc.Format = resourceFormat;
            for (int i = 0; i < 6; i++)
            {
                rtvDesc.Texture2DArray.MipSlice = 0;

                rtvDesc.Texture2DArray.FirstArraySlice = i;
                rtvDesc.Texture2DArray.ArraySize = 1;

                SubresourceRtvs[new MySubresourceId { Id = handle, Subresource = i }] = new MyRtvInfo
                {
                    Description = rtvDesc,
                    View = new RenderTargetView(MyRender11.Device, res, rtvDesc)
                };
            }
                

            return handle;
        }
Example #2
0
        internal static RwTexId CreateRenderTarget(int width, int height, Format fmt, bool mipmapAutogen = false)
        {
            var desc = new Texture2DDescription
            {
                ArraySize = 1,
                BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = fmt,
                Height = height,
                Width = width,
                MipLevels = mipmapAutogen ? 0 : 1,
                OptionFlags = mipmapAutogen ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default
            };

            var handle = new RwTexId { Index = Textures.Allocate() };
            Textures.Data[handle.Index] = new MyRwTextureInfo { Description2D = desc };
            Textures.Data[handle.Index].Resource = new Texture2D(MyRender11.Device, desc);

            Srvs[handle] = new MySrvInfo { Description = null, View = new ShaderResourceView(MyRender11.Device, Textures.Data[handle.Index].Resource) };
            Rtvs[handle] = new MyRtvInfo { Description = null, View = new RenderTargetView(MyRender11.Device, Textures.Data[handle.Index].Resource) };

            Index.Add(handle);

            return handle;
        }