Exemplo n.º 1
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;
        }
Exemplo n.º 2
0
 public void Put(DeviceContextProxy context)
 {
     context.ClearRenderTagetBindings();
     context.Reset();
     contextPool.Add(context);
 }
Exemplo n.º 3
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  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;

            //First pass, draw onto stencil buffer
            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.EffectMeshXRayGridP1;
                    var pass = mesh.EffectTechnique[DefaultPassNames.EffectMeshXRayGridP1];
                    if (pass.IsNULL)
                    {
                        continue;
                    }
                    pass.BindShader(deviceContext);
                    pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                    mesh.RenderCustom(context, deviceContext);
                }
            }
            //Second pass, remove not covered part from stencil buffer
            for (int i = 0; i < currentCores.Count; ++i)
            {
                var mesh = currentCores[i].Key;
                context.CustomPassName = DefaultPassNames.EffectMeshXRayGridP2;
                var pass = mesh.EffectTechnique[DefaultPassNames.EffectMeshXRayGridP2];
                if (pass.IsNULL)
                {
                    continue;
                }
                pass.BindShader(deviceContext);
                pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                mesh.RenderCustom(context, deviceContext);
            }

            deviceContext.ClearDepthStencilView(depthStencilBuffer, DepthStencilClearFlags.Depth, 1, 0);
            OnUpdatePerModelStruct(context);
            modelCB.Upload(deviceContext, ref modelStruct);
            //Thrid pass, draw mesh with grid overlay
            for (int i = 0; i < currentCores.Count; ++i)
            {
                var mesh  = currentCores[i].Key;
                var color = Color;
                if (currentCores[i].Value.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 = XRayDrawingPassName;
                var pass = mesh.EffectTechnique[XRayDrawingPassName];
                if (pass.IsNULL)
                {
                    continue;
                }
                pass.BindShader(deviceContext);
                pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                if (mesh.RenderCore is IMaterialRenderParams material)
                {
                    material.MaterialVariables.BindMaterialResources(context, deviceContext, pass);
                }
                mesh.RenderCustom(context, deviceContext);
            }
            if (hasMSAA)
            {
                deviceContext.ClearRenderTagetBindings();
                buffer.FullResDepthStencilPool.Put(Format.D32_Float_S8X24_UInt, depthStencilBuffer);
            }
            currentCores.Clear();
        }
Exemplo n.º 4
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;
                var hasMSAA            = buffer.ColorBufferSampleDesc.Count > 1;
                var depthStencilBuffer = hasMSAA ? context.GetOffScreenDS(OffScreenTextureSize.Full, Format.D32_Float_S8X24_UInt) : buffer.DepthStencilBuffer;

                deviceContext.SetRenderTarget(depthStencilBuffer, buffer.FullResPPBuffer.CurrentRTV);
                var viewport = context.Viewport;

                deviceContext.SetViewport(ref viewport);
                deviceContext.SetScissorRectangle(ref viewport);
                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);
                }
                //First pass, draw onto stencil buffer
                for (var i = 0; i < context.RenderHost.PerFrameNodesWithPostEffect.Count; ++i)
                {
                    var mesh = context.RenderHost.PerFrameNodesWithPostEffect[i];
                    if (mesh.TryGetPostEffect(EffectName, out var effect))
                    {
                        currentCores.Add(new KeyValuePair <SceneNode, IEffectAttributes>(mesh, effect));
                        context.CustomPassName = DefaultPassNames.EffectMeshXRayGridP1;
                        var pass = mesh.EffectTechnique[DefaultPassNames.EffectMeshXRayGridP1];
                        if (pass.IsNULL)
                        {
                            continue;
                        }
                        pass.BindShader(deviceContext);
                        pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                        mesh.RenderCustom(context, deviceContext);
                    }
                }
                //Second pass, remove not covered part from stencil buffer
                for (var i = 0; i < currentCores.Count; ++i)
                {
                    var mesh = currentCores[i].Key;
                    context.CustomPassName = DefaultPassNames.EffectMeshXRayGridP2;
                    var pass = mesh.EffectTechnique[DefaultPassNames.EffectMeshXRayGridP2];
                    if (pass.IsNULL)
                    {
                        continue;
                    }
                    pass.BindShader(deviceContext);
                    pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                    mesh.RenderCustom(context, deviceContext);
                }

                OnUpdatePerModelStruct(context);
                modelCB.Upload(deviceContext, ref modelStruct);
                //Thrid pass, draw mesh with grid overlay
                for (var i = 0; i < currentCores.Count; ++i)
                {
                    var mesh  = currentCores[i].Key;
                    var color = Color;
                    if (currentCores[i].Value.TryGetAttribute(EffectAttributeNames.ColorAttributeName, out var attribute) && attribute is string colorStr)
                    {
                        color = colorStr.ToColor4();
                    }
                    if (modelStruct.Color != color)
                    {
                        modelStruct.Color = color;
                        modelCB.Upload(deviceContext, ref modelStruct);
                    }
                    context.CustomPassName = XRayDrawingPassName;
                    var pass = mesh.EffectTechnique[XRayDrawingPassName];
                    if (pass.IsNULL)
                    {
                        continue;
                    }
                    pass.BindShader(deviceContext);
                    pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                    if (mesh.RenderCore is IMaterialRenderParams material)
                    {
                        material.MaterialVariables.BindMaterialResources(context, deviceContext, pass);
                    }
                    mesh.RenderCustom(context, deviceContext);
                }
                if (hasMSAA)
                {
                    deviceContext.ClearRenderTagetBindings();
                    depthStencilBuffer.Dispose();
                }
                currentCores.Clear();
            }
Exemplo n.º 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();
                }
            }