Beispiel #1
0
        protected override void CreateShadowMap(EntityLightShadow light)
        {
            var shadowMapDesc = light.Light.Shadow as LightShadowMap;


            // create the shadow map
            var shadowMap = new ShadowMap
            {
                LightDirection     = light.Light.Direction,
                LightPosition      = light.Entity.Transformation.Translation,
                ShadowMapSize      = shadowMapDesc.MaxSize,
                ShadowNearDistance = shadowMapDesc.NearDistance,
                ShadowFarDistance  = shadowMapDesc.FarDistance,
                CascadeCount       = light.Light.Type is LightDirectional ? shadowMapDesc.CascadeCount : 1, // cascades are only supported for directional shadow maps
                //Fov = light.Light.SpotFieldAngle,
                Filter = shadowMapDesc.FilterType,
                Layers = light.Light.Layers
            };

            // find or create the shadow map texture
            ShadowMapTexture chosenTexture = null;

            chosenTexture = AllocateOrChooseTexture(shadowMap, shadowMapDesc.FilterType == LightShadowMapFilterType.Variance ? texturesVsm : texturesDefault);

            shadowMap.Texture = chosenTexture;
            InternalShadowMaps.Add(shadowMap);
            light.ShadowMap = shadowMap;
        }
Beispiel #2
0
        private ShadowMapTexture AllocateOrChooseTexture(ShadowMap newShadowMap, Dictionary <ShadowMapTexture, List <ShadowMap> > shadowMapTextures)
        {
            ShadowMapTexture chosenTexture = null;

            // find best texture
            foreach (var shadowMapTexture in shadowMapTextures)
            {
                var shadowTexture = shadowMapTexture.Key;
                var shadowMaps    = shadowMapTexture.Value;

                shadowTexture.GuillotinePacker.Clear(shadowTexture.ShadowMapDepthTexture.ViewWidth, shadowTexture.ShadowMapDepthTexture.ViewHeight);

                var useShadowTexture = true;
                for (var i = 0; i < shadowMaps.Count && useShadowTexture; ++i)
                {
                    useShadowTexture = shadowTexture.GuillotinePacker.TryInsert(shadowMaps[i].ShadowMapSize, shadowMaps[i].ShadowMapSize, shadowMaps[i].CascadeCount);
                }

                useShadowTexture = useShadowTexture && shadowTexture.GuillotinePacker.TryInsert(newShadowMap.ShadowMapSize, newShadowMap.ShadowMapSize, newShadowMap.CascadeCount);

                shadowTexture.GuillotinePacker.Clear();

                if (useShadowTexture)
                {
                    chosenTexture = shadowMapTexture.Key;
                    break;
                }
            }

            if (chosenTexture == null)
            {
                // allocate a new texture
                chosenTexture = new ShadowMapTexture(GraphicsDevice, newShadowMap.Filter, 2048);
                chosenTexture.GuillotinePacker.Clear(chosenTexture.ShadowMapDepthTexture.ViewWidth, chosenTexture.ShadowMapDepthTexture.ViewHeight);

                // TODO: choose texture size based on the shadow map. For now throw exception
                if (!chosenTexture.GuillotinePacker.TryInsert(newShadowMap.ShadowMapSize, newShadowMap.ShadowMapSize, newShadowMap.CascadeCount))
                {
                    var message = new StringBuilder();
                    message.AppendFormat("Unable to allocate shadow map texture. The default size (2048 x 2048) is too small for the shadow map ({0} cascade(s) with size {1}).", newShadowMap.CascadeCount, newShadowMap.ShadowMapSize);
                    throw new Exception(message.ToString());
                }

                chosenTexture.GuillotinePacker.Clear();

                InternalShadowMapTextures.Add(chosenTexture);
                var shadowMaps = new List <ShadowMap> {
                    newShadowMap
                };
                shadowMapTextures.Add(chosenTexture, shadowMaps);
            }
            else
            {
                shadowMapTextures[chosenTexture].Add(newShadowMap);
            }

            return(chosenTexture);
        }
Beispiel #3
0
        public override void GenerateShadowMaps(Device device, DeviceContext deviceContext, CRenderScene renderScene)
        {
            UserDefinedAnnotation annotation = deviceContext.QueryInterface <UserDefinedAnnotation>();

            annotation.BeginEvent("SpotLightShadowMap");

            deviceContext.Rasterizer.SetViewport(0.0f, 0.0f, CStaticRendererCvars.ShadowMapSize, CStaticRendererCvars.ShadowMapSize);

            deviceContext.ClearDepthStencilView(ShadowMapTexture.GetRenderTarget(), DepthStencilClearFlags.Depth, 1.0f, 0);
            deviceContext.OutputMerger.SetRenderTargets(ShadowMapTexture.GetRenderTarget());

            DepthStencilStateDescription depthStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled  = true,
                DepthWriteMask  = DepthWriteMask.All,
                DepthComparison = Comparison.Less,

                IsStencilEnabled = false,
                StencilReadMask  = 0xFF,
                StencilWriteMask = 0xFF,
            };

            DepthStencilState depthState = new DepthStencilState(device, depthStateDesc);

            deviceContext.OutputMerger.SetDepthStencilState(depthState);

            SSceneViewInfo viewInfo = new SSceneViewInfo()
            {
                FitProjectionToScene = false,
                Fov          = MathUtil.Pi / 2.0f,
                ScreenFar    = Range,
                ScreenNear   = 0.1f,
                ScreenHeight = CStaticRendererCvars.ShadowMapSize,
                ScreenWidth  = CStaticRendererCvars.ShadowMapSize,
                ScreenLeft   = 0.0f,
                ScreenTop    = 0.0f,
                ViewLocation = Transform.WorldPosition,
                ViewMatrix   = Matrix.Invert(Transform.WorldMatrix)
            };

            m_projectionMatrix        = Matrix.PerspectiveFovLH(viewInfo.Fov, 1.0f, viewInfo.ScreenNear, viewInfo.ScreenFar);
            viewInfo.ProjectionMatrix = m_projectionMatrix;
            viewInfo.CreateBoundingFrustum();

            depthState.Dispose();
            renderScene.RenderSceneDepth(device, deviceContext, in viewInfo);

            DepthStencilView nullDepthView = null;

            deviceContext.OutputMerger.SetRenderTargets(nullDepthView);

            annotation.EndEvent();
            annotation.Dispose();
        }
        /// <summary>
        /// Adds the shadow map texture to the budget of textures.
        /// </summary>
        /// <param name="shadowMapTexture">The shadow map texture.</param>
        /// <param name="filterType">The filtering that will be applied to this shadow.</param>
        protected void AddShadowMapTexture(ShadowMapTexture shadowMapTexture, LightShadowMapFilterType filterType)
        {
            if (filterType == LightShadowMapFilterType.Variance)
            {
                texturesVsm.Add(shadowMapTexture, shadowMapTexture.ShadowMapDepthTexture.ViewWidth * shadowMapTexture.ShadowMapDepthTexture.ViewHeight);
                shadowMapVsmTextures.Add(shadowMapTexture);
            }
            else
            {
                texturesDefault.Add(shadowMapTexture, shadowMapTexture.ShadowMapDepthTexture.ViewWidth * shadowMapTexture.ShadowMapDepthTexture.ViewHeight);
                shadowMapDefaultTextures.Add(shadowMapTexture);
            }

            InternalShadowMapTextures.Add(shadowMapTexture);
        }
        public void Dispose()
        {
            ShaderLibrary.Dispose();

            foreach (var pass in Passes)
            {
                pass.Dispose();
            }

            Quad.Dispose();

            CameraUniformBuffer.Dispose();
            SceneUniformBuffer.Dispose();
            MaterialUniformBuffer.Dispose();
            ToneMapUniformBuffer.Dispose();

            ShadowMapFramebuffer.Dispose();
            ShadowMapTexture.Dispose();

            ShadowMapFilteredFramebuffer.Dispose();
            ShadowMapFilteredTexture.Dispose();

            SSSDepthTexture.Dispose();
            SSSHighFramebuffer.Dispose();
            SSSHighTexture.Dispose();
            SSSMiddleFramebuffer.Dispose();
            SSSMiddleTexture.Dispose();
            SSSLowFramebuffer.Dispose();
            SSSLowTexture.Dispose();
            SSSLowFilteredFramebuffer.Dispose();
            SSSLowFilteredTexture.Dispose();

            SceneFramebuffer.Dispose();
            SceneColorTexture.Dispose();
            SceneDepthTexture.Dispose();

            ToneMapFramebuffer.Dispose();
            ToneMapColorTexture.Dispose();
        }
 private int ShadowMapTextureComparerVsm(ShadowMapTexture texture0, ShadowMapTexture texture1)
 {
     return texturesVsm[texture0] - texturesVsm[texture1];
 }
 private int ShadowMapTextureComparerDefault(ShadowMapTexture texture0, ShadowMapTexture texture1)
 {
     return texturesDefault[texture0] - texturesDefault[texture1];
 }
        /// <summary>
        /// Adds the shadow map texture to the budget of textures.
        /// </summary>
        /// <param name="shadowMapTexture">The shadow map texture.</param>
        /// <param name="filterType">The filtering that will be applied to this shadow.</param>
        protected void AddShadowMapTexture(ShadowMapTexture shadowMapTexture, LightShadowMapFilterType filterType)
        {
            if (filterType == LightShadowMapFilterType.Variance)
            {
                texturesVsm.Add(shadowMapTexture, shadowMapTexture.ShadowMapDepthTexture.ViewWidth * shadowMapTexture.ShadowMapDepthTexture.ViewHeight);
                shadowMapVsmTextures.Add(shadowMapTexture);
            }
            else
            {
                texturesDefault.Add(shadowMapTexture, shadowMapTexture.ShadowMapDepthTexture.ViewWidth * shadowMapTexture.ShadowMapDepthTexture.ViewHeight);
                shadowMapDefaultTextures.Add(shadowMapTexture);
            }

            InternalShadowMapTextures.Add(shadowMapTexture);
        }
 private int ShadowMapTextureComparerVsm(ShadowMapTexture texture0, ShadowMapTexture texture1)
 {
     return(texturesVsm[texture0] - texturesVsm[texture1]);
 }
 private int ShadowMapTextureComparerDefault(ShadowMapTexture texture0, ShadowMapTexture texture1)
 {
     return(texturesDefault[texture0] - texturesDefault[texture1]);
 }
Beispiel #11
0
 public override ShaderResourceView GetShadowMapView()
 {
     return(ShadowMapTexture.GetTexture());
 }
Beispiel #12
0
 public override void Dispose()
 {
     ShadowMapTexture.Dispose();
     m_isShadowMapIntialized = false;
 }
        private ShadowMapTexture AllocateOrChooseTexture(ShadowMap newShadowMap, Dictionary<ShadowMapTexture, List<ShadowMap>> shadowMapTextures)
        {
            ShadowMapTexture chosenTexture = null;

            // find best texture
            foreach (var shadowMapTexture in shadowMapTextures)
            {
                var shadowTexture = shadowMapTexture.Key;
                var shadowMaps = shadowMapTexture.Value;

                shadowTexture.GuillotinePacker.Clear(shadowTexture.ShadowMapDepthTexture.ViewWidth, shadowTexture.ShadowMapDepthTexture.ViewHeight);

                var useShadowTexture = true;
                for (var i = 0; i < shadowMaps.Count && useShadowTexture; ++i)
                    useShadowTexture = shadowTexture.GuillotinePacker.TryInsert(shadowMaps[i].ShadowMapSize, shadowMaps[i].ShadowMapSize, shadowMaps[i].CascadeCount);

                useShadowTexture = useShadowTexture && shadowTexture.GuillotinePacker.TryInsert(newShadowMap.ShadowMapSize, newShadowMap.ShadowMapSize, newShadowMap.CascadeCount);

                shadowTexture.GuillotinePacker.Clear();

                if (useShadowTexture)
                {
                    chosenTexture = shadowMapTexture.Key;
                    break;
                }
            }

            if (chosenTexture == null)
            {
                // allocate a new texture
                chosenTexture = new ShadowMapTexture(GraphicsDevice, newShadowMap.Filter, 2048);
                chosenTexture.GuillotinePacker.Clear(chosenTexture.ShadowMapDepthTexture.ViewWidth, chosenTexture.ShadowMapDepthTexture.ViewHeight);

                // TODO: choose texture size based on the shadow map. For now throw exception
                if (!chosenTexture.GuillotinePacker.TryInsert(newShadowMap.ShadowMapSize, newShadowMap.ShadowMapSize, newShadowMap.CascadeCount))
                {
                    var message = new StringBuilder();
                    message.AppendFormat("Unable to allocate shadow map texture. The default size (2048 x 2048) is too small for the shadow map ({0} cascade(s) with size {1}).", newShadowMap.CascadeCount, newShadowMap.ShadowMapSize);
                    throw new Exception(message.ToString());
                }

                chosenTexture.GuillotinePacker.Clear();

                InternalShadowMapTextures.Add(chosenTexture);
                var shadowMaps = new List<ShadowMap> { newShadowMap };
                shadowMapTextures.Add(chosenTexture, shadowMaps);
            }
            else
            {
                shadowMapTextures[chosenTexture].Add(newShadowMap);
            }

            return chosenTexture;
        }