Beispiel #1
0
 /// <summary>
 /// Adds the post effect.
 /// </summary>
 /// <param name="effect">The effect.</param>
 public void AddPostEffect(IEffectAttributes effect)
 {
     if (postEffectNames.ContainsKey(effect.EffectName))
     {
         return;
     }
     postEffectNames.Add(effect.EffectName, effect);
     InvalidateRender();
 }
Beispiel #2
0
        /// <summary>
        /// Called when [render].
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="deviceContext">The device context.</param>
        protected override void OnRender(RenderContext context, DeviceContextProxy deviceContext)
        {
            var  buffer             = context.RenderHost.RenderBuffer;
            bool hasMSAA            = buffer.ColorBufferSampleDesc.Count > 1;
            var  dPass              = DoublePass;
            var  depthStencilBuffer = hasMSAA ? buffer.FullResDepthStencilPool.Get(Format.D32_Float_S8X24_UInt) : buffer.DepthStencilBuffer;

            BindTarget(depthStencilBuffer, buffer.FullResPPBuffer.CurrentRTV, deviceContext, buffer.TargetWidth, buffer.TargetHeight, false);
            if (hasMSAA)
            {
                //Needs to do a depth pass for existing meshes.Because the msaa depth buffer is not resolvable.
                deviceContext.ClearDepthStencilView(depthStencilBuffer, DepthStencilClearFlags.Depth, 1, 0);
                depthPrepassCore.Render(context, deviceContext);
            }
            var frustum = context.BoundingFrustum;

            context.IsCustomPass = true;
            if (dPass)
            {
                deviceContext.ClearDepthStencilView(depthStencilBuffer, DepthStencilClearFlags.Stencil, 1, 0);
                for (int i = 0; i < context.RenderHost.PerFrameNodesWithPostEffect.Count; ++i)
                {
                    var mesh = context.RenderHost.PerFrameNodesWithPostEffect[i];
                    if (context.EnableBoundingFrustum && !mesh.TestViewFrustum(ref frustum))
                    {
                        continue;
                    }
                    if (mesh.TryGetPostEffect(EffectName, out IEffectAttributes effect))
                    {
                        currentCores.Add(new KeyValuePair <SceneNode, IEffectAttributes>(mesh, effect));
                        context.CustomPassName = DefaultPassNames.EffectMeshXRayP1;
                        var pass = mesh.EffectTechnique[DefaultPassNames.EffectMeshXRayP1];
                        if (pass.IsNULL)
                        {
                            continue;
                        }
                        pass.BindShader(deviceContext);
                        pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                        mesh.Render(context, deviceContext);
                    }
                }

                for (int i = 0; i < currentCores.Count; ++i)
                {
                    var mesh = currentCores[i];
                    IEffectAttributes effect = mesh.Value;
                    var color = Color;
                    if (effect.TryGetAttribute(EffectAttributeNames.ColorAttributeName, out object attribute) && attribute is string colorStr)
                    {
                        color = colorStr.ToColor4();
                    }
                    if (modelStruct.Color != color)
                    {
                        modelStruct.Color = color;
                        OnUploadPerModelConstantBuffers(deviceContext);
                    }

                    context.CustomPassName = DefaultPassNames.EffectMeshXRayP2;
                    var pass = mesh.Key.EffectTechnique[DefaultPassNames.EffectMeshXRayP2];
                    if (pass.IsNULL)
                    {
                        continue;
                    }
                    pass.BindShader(deviceContext);
                    pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                    mesh.Key.Render(context, deviceContext);
                }
                currentCores.Clear();
            }
            else
            {
                for (int i = 0; i < context.RenderHost.PerFrameNodesWithPostEffect.Count; ++i)
                {
                    var mesh = context.RenderHost.PerFrameNodesWithPostEffect[i];
                    if (context.EnableBoundingFrustum && !mesh.TestViewFrustum(ref frustum))
                    {
                        continue;
                    }
                    if (mesh.TryGetPostEffect(EffectName, out IEffectAttributes effect))
                    {
                        object attribute;
                        var    color = Color;
                        if (effect.TryGetAttribute(EffectAttributeNames.ColorAttributeName, out attribute) && attribute is string colorStr)
                        {
                            color = colorStr.ToColor4();
                        }
                        if (modelStruct.Color != color)
                        {
                            modelStruct.Color = color;
                            OnUploadPerModelConstantBuffers(deviceContext);
                        }
                        context.CustomPassName = DefaultPassNames.EffectMeshXRayP2;
                        var pass = mesh.EffectTechnique[DefaultPassNames.EffectMeshXRayP2];
                        if (pass.IsNULL)
                        {
                            continue;
                        }
                        pass.BindShader(deviceContext);
                        pass.BindStates(deviceContext, StateType.BlendState);
                        deviceContext.SetDepthStencilState(pass.DepthStencilState, 0);
                        mesh.Render(context, deviceContext);
                    }
                }
            }
            if (hasMSAA)
            {
                deviceContext.ClearRenderTagetBindings();
                buffer.FullResDepthStencilPool.Put(Format.D32_Float_S8X24_UInt, depthStencilBuffer);
            }
            context.IsCustomPass = false;
        }
Beispiel #3
0
 /// <summary>
 /// Tries the get post effect.
 /// </summary>
 /// <param name="effectName">Name of the effect.</param>
 /// <param name="effect">The effect.</param>
 /// <returns></returns>
 public bool TryGetPostEffect(string effectName, out IEffectAttributes effect)
 {
     return(postEffectNames.TryGetValue(effectName, out effect));
 }
Beispiel #4
0
        /// <summary>
        /// Called when [render].
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="deviceContext">The device context.</param>
        protected override void OnRender(IRenderContext context, DeviceContextProxy deviceContext)
        {
            context.IsCustomPass = true;
            if (DoublePass)
            {
                deviceContext.DeviceContext.ClearDepthStencilView(context.RenderHost.DepthStencilBufferView, DepthStencilClearFlags.Stencil, 0, 0);
                currentCores.Clear();
                for (int i = 0; i < context.RenderHost.PerFrameGeneralCoresWithPostEffect.Count; ++i)
                {
                    IEffectAttributes effect;
                    var mesh = context.RenderHost.PerFrameGeneralCoresWithPostEffect[i];
                    if (mesh.TryGetPostEffect(EffectName, out effect))
                    {
                        currentCores.Add(new KeyValuePair <RenderCore, IEffectAttributes>(mesh, effect));
                        context.CustomPassName = DefaultPassNames.EffectMeshXRayP1;
                        var pass = mesh.EffectTechnique[DefaultPassNames.EffectMeshXRayP1];
                        if (pass.IsNULL)
                        {
                            continue;
                        }
                        pass.BindShader(deviceContext);
                        pass.BindStates(deviceContext, StateType.BlendState);
                        deviceContext.SetDepthStencilState(pass.DepthStencilState, 0);//Increment the stencil value
                        mesh.Render(context, deviceContext);
                    }
                }
                for (int i = 0; i < currentCores.Count; ++i)
                {
                    var mesh = currentCores[i];
                    IEffectAttributes effect = mesh.Value;
                    object            attribute;
                    var color = Color;
                    if (effect.TryGetAttribute(EffectAttributeNames.ColorAttributeName, out attribute) && attribute is string colorStr)
                    {
                        color = colorStr.ToColor4();
                    }
                    if (modelStruct.Color != color)
                    {
                        modelStruct.Color = color;
                        OnUploadPerModelConstantBuffers(deviceContext);
                    }

                    context.CustomPassName = DefaultPassNames.EffectMeshXRayP2;
                    var pass = mesh.Key.EffectTechnique[DefaultPassNames.EffectMeshXRayP2];
                    if (pass.IsNULL)
                    {
                        continue;
                    }
                    pass.BindShader(deviceContext);
                    pass.BindStates(deviceContext, StateType.BlendState);
                    deviceContext.SetDepthStencilState(pass.DepthStencilState, 1);//Do stencil test only on value = 1.
                    mesh.Key.Render(context, deviceContext);
                }
                currentCores.Clear();
            }
            else
            {
                for (int i = 0; i < context.RenderHost.PerFrameGeneralCoresWithPostEffect.Count; ++i)
                {
                    IEffectAttributes effect;
                    var mesh = context.RenderHost.PerFrameGeneralCoresWithPostEffect[i];
                    if (mesh.TryGetPostEffect(EffectName, out effect))
                    {
                        object attribute;
                        var    color = Color;
                        if (effect.TryGetAttribute(EffectAttributeNames.ColorAttributeName, out attribute) && attribute is string colorStr)
                        {
                            color = colorStr.ToColor4();
                        }
                        if (modelStruct.Color != color)
                        {
                            modelStruct.Color = color;
                            OnUploadPerModelConstantBuffers(deviceContext);
                        }
                        context.CustomPassName = DefaultPassNames.EffectMeshXRayP2;
                        var pass = mesh.EffectTechnique[DefaultPassNames.EffectMeshXRayP2];
                        if (pass.IsNULL)
                        {
                            continue;
                        }
                        pass.BindShader(deviceContext);
                        pass.BindStates(deviceContext, StateType.BlendState);
                        deviceContext.SetDepthStencilState(pass.DepthStencilState, 0);
                        mesh.Render(context, deviceContext);
                    }
                }
            }
            context.IsCustomPass = false;
        }
Beispiel #5
0
            /// <summary>
            /// Called when [render].
            /// </summary>
            /// <param name="context">The context.</param>
            /// <param name="deviceContext">The device context.</param>
            public override void Render(RenderContext context, DeviceContextProxy deviceContext)
            {
                var  buffer             = context.RenderHost.RenderBuffer;
                bool hasMSAA            = buffer.ColorBufferSampleDesc.Count > 1;
                var  dPass              = EnableDoublePass;
                var  depthStencilBuffer = hasMSAA ? context.GetOffScreenDS(OffScreenTextureSize.Full, Format.D32_Float_S8X24_UInt) : buffer.DepthStencilBuffer;

                deviceContext.SetRenderTarget(depthStencilBuffer, buffer.FullResPPBuffer.CurrentRTV, buffer.TargetWidth, buffer.TargetHeight);
                if (hasMSAA)
                {
                    //Needs to do a depth pass for existing meshes.Because the msaa depth buffer is not resolvable.
                    deviceContext.ClearDepthStencilView(depthStencilBuffer, DepthStencilClearFlags.Depth, 1, 0);
                    depthPrepassCore.Render(context, deviceContext);
                }
                deviceContext.ClearDepthStencilView(depthStencilBuffer, DepthStencilClearFlags.Stencil, 1, 0);
                if (dPass)
                {
                    for (int i = 0; i < context.RenderHost.PerFrameNodesWithPostEffect.Count; ++i)
                    {
                        var mesh = context.RenderHost.PerFrameNodesWithPostEffect[i];
                        if (mesh.TryGetPostEffect(EffectName, out IEffectAttributes effect))
                        {
                            currentCores.Add(new KeyValuePair <SceneNode, IEffectAttributes>(mesh, effect));
                            context.CustomPassName = DefaultPassNames.EffectMeshXRayP1;
                            var pass = mesh.EffectTechnique[DefaultPassNames.EffectMeshXRayP1];
                            if (pass.IsNULL)
                            {
                                continue;
                            }
                            pass.BindShader(deviceContext);
                            pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                            mesh.RenderCustom(context, deviceContext);
                        }
                    }
                    modelCB.Upload(deviceContext, ref modelStruct);
                    for (int i = 0; i < currentCores.Count; ++i)
                    {
                        var mesh = currentCores[i];
                        IEffectAttributes effect = mesh.Value;
                        var color = Color;
                        if (effect.TryGetAttribute(EffectAttributeNames.ColorAttributeName, out object attribute) && attribute is string colorStr)
                        {
                            color = colorStr.ToColor4();
                        }
                        if (modelStruct.Color != color)
                        {
                            modelStruct.Color = color;
                            modelCB.Upload(deviceContext, ref modelStruct);
                        }

                        context.CustomPassName = DefaultPassNames.EffectMeshXRayP2;
                        var pass = mesh.Key.EffectTechnique[DefaultPassNames.EffectMeshXRayP2];
                        if (pass.IsNULL)
                        {
                            continue;
                        }
                        pass.BindShader(deviceContext);
                        pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                        mesh.Key.RenderCustom(context, deviceContext);
                    }
                    currentCores.Clear();
                }
                else
                {
                    modelCB.Upload(deviceContext, ref modelStruct);
                    for (int i = 0; i < context.RenderHost.PerFrameNodesWithPostEffect.Count; ++i)
                    {
                        var mesh = context.RenderHost.PerFrameNodesWithPostEffect[i];
                        if (mesh.TryGetPostEffect(EffectName, out IEffectAttributes effect))
                        {
                            var color = Color;
                            if (effect.TryGetAttribute(EffectAttributeNames.ColorAttributeName, out object attribute) && attribute is string colorStr)
                            {
                                color = colorStr.ToColor4();
                            }
                            if (modelStruct.Color != color)
                            {
                                modelStruct.Color = color;
                                modelCB.Upload(deviceContext, ref modelStruct);
                            }
                            context.CustomPassName = DefaultPassNames.EffectMeshXRayP2;
                            var pass = mesh.EffectTechnique[DefaultPassNames.EffectMeshXRayP2];
                            if (pass.IsNULL)
                            {
                                continue;
                            }
                            pass.BindShader(deviceContext);
                            pass.BindStates(deviceContext, StateType.BlendState);
                            deviceContext.SetDepthStencilState(pass.DepthStencilState, 0);
                            mesh.RenderCustom(context, deviceContext);
                        }
                    }
                }
                if (hasMSAA)
                {
                    deviceContext.ClearRenderTagetBindings();
                    depthStencilBuffer.Dispose();
                }
            }