Beispiel #1
0
        public Shadowmap(Device device)
        {
            TextureSize         = 1024;                                         //todo: make this a setting...
            CascadeCount        = Math.Min(Settings.Default.ShadowCascades, 8); // 6; //use setting
            PCFSize             = 3;
            PCFOffset           = 0.000125f;                                    //0.002f
            BlurBetweenCascades = 0.05f;

            ShadowVars = new GpuVarsBuffer <ShadowmapVars>(device);


            DepthTexture    = DXUtility.CreateTexture2D(device, TextureSize * CascadeCount, TextureSize, 1, 1, Format.R32_Typeless, 1, 0, ResourceUsage.Default, BindFlags.DepthStencil | BindFlags.ShaderResource, 0, 0);
            DepthTextureSS  = DXUtility.CreateSamplerState(device, TextureAddressMode.Border, new Color4(0.0f), Comparison.Less, Filter.ComparisonMinMagLinearMipPoint, 0, 0.0f, 0.0f, 0.0f);
            DepthTextureSRV = DXUtility.CreateShaderResourceView(device, DepthTexture, Format.R32_Float, ShaderResourceViewDimension.Texture2D, 1, 0, 0, 0);
            DepthTextureDSV = DXUtility.CreateDepthStencilView(device, DepthTexture, Format.D32_Float, DepthStencilViewDimension.Texture2D);

            Cascades = new List <ShadowmapCascade>(CascadeCount);
            for (int i = 0; i < CascadeCount; i++)
            {
                ShadowmapCascade c = new ShadowmapCascade();
                c.Owner         = this;
                c.Index         = i;
                c.ZNear         = 0.0f;
                c.ZFar          = 1.0f;
                c.IntervalNear  = 0.0f;
                c.IntervalFar   = 1.0f;
                c.DepthRenderVP = new ViewportF()
                {
                    Height   = (float)TextureSize,
                    Width    = (float)TextureSize,
                    MaxDepth = 1.0f,
                    MinDepth = 0.0f,
                    X        = (float)(TextureSize * i),
                    Y        = 0,
                };
                Cascades.Add(c);
            }

            DepthRenderRS = DXUtility.CreateRasterizerState(device, FillMode.Solid, CullMode.None, true, false, true, 0, 0.0f, 1.0f);
            DepthRenderDS = DXUtility.CreateDepthStencilState(device, true, DepthWriteMask.All);

            DepthRenderVP          = new ViewportF();
            DepthRenderVP.Height   = (float)TextureSize;
            DepthRenderVP.Width    = (float)TextureSize;
            DepthRenderVP.MaxDepth = 1.0f;
            DepthRenderVP.MinDepth = 0.0f;
            DepthRenderVP.X        = 0;
            DepthRenderVP.Y        = 0;

            graphicsMemoryUsage = (long)(TextureSize * TextureSize * CascadeCount * 4);
        }