示例#1
0
        /// <summary>
        /// Sets the screen spaced coordinates.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="deviceContext">The device context.</param>
        /// <param name="clearDepthBuffer">if set to <c>true</c> [clear depth buffer].</param>
        protected virtual void SetScreenSpacedCoordinates(RenderContext context, DeviceContextProxy deviceContext, bool clearDepthBuffer)
        {
            context.WorldMatrix = Matrix.Identity;
            DepthStencilView dsView;

            if (clearDepthBuffer)
            {
                deviceContext.GetDepthStencilView(out dsView);
                if (dsView == null)
                {
                    return;
                }

                deviceContext.ClearDepthStencilView(dsView, DepthStencilClearFlags.Depth, 1f, 0);
                dsView.Dispose();
            }
            IsRightHand = !context.Camera.CreateLeftHandSystem;
            float viewportSize = Size * SizeScale;
            var   globalTrans  = context.GlobalTransform;

            UpdateProjectionMatrix((float)context.ActualWidth, (float)context.ActualHeight);
            globalTrans.View           = CreateViewMatrix(context, out globalTrans.EyePos);
            globalTrans.Projection     = projectionMatrix;
            globalTrans.ViewProjection = globalTrans.View * globalTrans.Projection;
            globalTrans.Viewport       = new Vector4(viewportSize, viewportSize, 1f / viewportSize, 1f / viewportSize);
            globalTransformCB.UploadDataToBuffer(deviceContext, ref globalTrans);
            GlobalTransform = globalTrans;
            int offX = (int)(Width / 2 * (1 + RelativeScreenLocationX) - viewportSize / 2);
            int offY = (int)(Height / 2 * (1 - RelativeScreenLocationY) - viewportSize / 2);

            deviceContext.SetViewport(offX, offY, viewportSize, viewportSize);
            deviceContext.SetScissorRectangle(offX, offY, (int)viewportSize + offX, (int)viewportSize + offY);
        }
示例#2
0
            public override void Render(RenderContext context, DeviceContextProxy deviceContext)
            {
                if (!NeedRender)
                {
                    modelStruct.HasShadowMap = 0;
                    modelCB.Upload(deviceContext, ref modelStruct);
                    return;
                }
                OnUpdateLightSource?.Invoke(this, new UpdateLightSourceEventArgs(context));
                ++currentFrame;
                currentFrame %= Math.Max(1, UpdateFrequency);
                if (!FoundLightSource || currentFrame != 0)
                {
                    return;
                }
                if (resolutionChanged)
                {
                    RemoveAndDispose(ref viewResource);
                    viewResource = new ShaderResourceViewProxy(Device, ShadowMapTextureDesc);
                    viewResource.CreateView(DepthStencilViewDesc);
                    viewResource.CreateView(ShaderResourceViewDesc);
                    resolutionChanged = false;
                }

                deviceContext.ClearDepthStencilView(viewResource, DepthStencilClearFlags.Depth, 1.0f, 0);
                var orgFrustum = context.BoundingFrustum;
                var frustum    = new BoundingFrustum(LightView * LightProjection);

                context.BoundingFrustum = frustum;
#if !TEST
                deviceContext.SetViewport(0, 0, Width, Height);

                deviceContext.SetDepthStencil(viewResource.DepthStencilView);
                modelStruct.HasShadowMap = context.RenderHost.IsShadowMapEnabled ? 1 : 0;
                modelCB.Upload(deviceContext, ref modelStruct);
                for (var i = 0; i < context.RenderHost.PerFrameOpaqueNodes.Count; ++i)
                {
                    //Only support opaque object for throwing shadows.
                    var core = context.RenderHost.PerFrameOpaqueNodes[i];
                    if (core.RenderCore.IsThrowingShadow && core.TestViewFrustum(ref frustum))
                    {
                        core.RenderShadow(context, deviceContext);
                    }
                }
                context.BoundingFrustum = orgFrustum;
                context.RenderHost.SetDefaultRenderTargets(false);
                context.SharedResource.ShadowView = viewResource;
#endif
            }
            protected override void OnRender(RenderContext renderContext, DeviceContextProxy deviceContext)
            {
                if (needsAssignVariables)
                {
                    lock (clipParamCB)
                    {
                        if (needsAssignVariables)
                        {
                            clipParamCB.WriteValueByName(ClipPlaneStruct.CuttingOperationStr, (int)cuttingOperation);
                            clipParamCB.WriteValueByName(ClipPlaneStruct.CrossSectionColorStr, sectionColor);
                            clipParamCB.WriteValueByName(ClipPlaneStruct.EnableCrossPlaneStr, planeEnabled);
                            clipParamCB.WriteValueByName(ClipPlaneStruct.EnableCrossPlane5To8Str, plane5To8Enabled);
                            clipParamCB.WriteValueByName(ClipPlaneStruct.CrossPlane1ParamsStr, plane1Params);
                            clipParamCB.WriteValueByName(ClipPlaneStruct.CrossPlane2ParamsStr, plane2Params);
                            clipParamCB.WriteValueByName(ClipPlaneStruct.CrossPlane3ParamsStr, plane3Params);
                            clipParamCB.WriteValueByName(ClipPlaneStruct.CrossPlane4ParamsStr, plane4Params);
                            clipParamCB.WriteValueByName(ClipPlaneStruct.CrossPlane5ParamsStr, plane5Params);
                            clipParamCB.WriteValueByName(ClipPlaneStruct.CrossPlane6ParamsStr, plane6Params);
                            clipParamCB.WriteValueByName(ClipPlaneStruct.CrossPlane7ParamsStr, plane7Params);
                            clipParamCB.WriteValueByName(ClipPlaneStruct.CrossPlane8ParamsStr, plane8Params);
                            needsAssignVariables = false;
                        }
                    }
                }
                clipParamCB.Upload(deviceContext);
                base.OnRender(renderContext, deviceContext);
                // Draw backface into stencil buffer
                var dsView = renderContext.RenderHost.DepthStencilBufferView;

                deviceContext.ClearDepthStencilView(dsView, DepthStencilClearFlags.Stencil, 0, 0);
                deviceContext.SetDepthStencil(dsView);//Remove render target
                deviceContext.SetRasterState(backfaceRasterState);
                drawBackfacePass.BindShader(deviceContext);
                drawBackfacePass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                DrawIndexed(deviceContext, GeometryBuffer.IndexBuffer, InstanceBuffer);

                //Draw full screen quad to fill cross section
                deviceContext.SetRasterState(RasterState);
                drawScreenQuadPass.BindShader(deviceContext);
                drawScreenQuadPass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                renderContext.RenderHost.SetDefaultRenderTargets(false);//Rebind render target
                deviceContext.Draw(4, 0);
            }
        protected override void OnRender(RenderContext renderContext, DeviceContextProxy deviceContext)
        {
            base.OnRender(renderContext, deviceContext);
            // Draw backface into stencil buffer
            var dsView = renderContext.RenderHost.DepthStencilBufferView;

            deviceContext.ClearDepthStencilView(dsView, DepthStencilClearFlags.Stencil, 0, 0);
            deviceContext.SetDepthStencilOnly(dsView);//Remove render target
            deviceContext.SetRasterState(backfaceRasterState);
            drawBackfacePass.BindShader(deviceContext);
            drawBackfacePass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
            OnDraw(deviceContext, InstanceBuffer);

            //Draw full screen quad to fill cross section
            deviceContext.SetRasterState(RasterState);
            drawScreenQuadPass.BindShader(deviceContext);
            drawScreenQuadPass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
            renderContext.RenderHost.SetDefaultRenderTargets(false);//Rebind render target
            deviceContext.Draw(4, 0);
        }
            public override void Render(RenderContext context, DeviceContextProxy deviceContext)
            {
                if (RenderType != RenderType.ScreenSpaced)
                {
                    return;
                }
                deviceContext.GetDepthStencilView(out var dsView);
                if (dsView == null)
                {
                    return;
                }

                deviceContext.ClearDepthStencilView(dsView, DepthStencilClearFlags.Depth, 1f, 0);
                dsView.Dispose();
                context.RestoreGlobalTransform();
                context.UpdatePerFrameData(true, false, deviceContext);
                deviceContext.SetViewport(context.Viewport.X, context.Viewport.Y, context.Viewport.Width, context.Viewport.Height);
                deviceContext.SetScissorRectangle((int)context.Viewport.X, (int)context.Viewport.Y,
                                                  (int)context.Viewport.Width, (int)context.Viewport.Height);
            }
            /// <summary>
            /// Sets the screen spaced coordinates.
            /// </summary>
            /// <param name="context">The context.</param>
            /// <param name="deviceContext">The device context.</param>
            /// <param name="clearDepthBuffer">if set to <c>true</c> [clear depth buffer].</param>
            protected virtual void SetScreenSpacedCoordinates(RenderContext context, DeviceContextProxy deviceContext, bool clearDepthBuffer)
            {
                if (context.ActualWidth < Size || context.ActualHeight < Size)
                {
                    return;
                }
                if (clearDepthBuffer)
                {
                    deviceContext.GetDepthStencilView(out DepthStencilView dsView);
                    if (dsView == null)
                    {
                        return;
                    }

                    deviceContext.ClearDepthStencilView(dsView, DepthStencilClearFlags.Depth, 1f, 0);
                    dsView.Dispose();
                }
                IsRightHand = !context.Camera.CreateLeftHandSystem;
                float viewportSize = Size * SizeScale;

                switch (mode)
                {
                case ScreenSpacedMode.RelativeScreenSpaced:
                    RenderRelativeScreenSpaced(context, deviceContext);
                    break;

                case ScreenSpacedMode.AbsolutePosition3D:
                    switch (context.IsPerspective)
                    {
                    case true:
                        RenderAbsolutePositionPerspective(context, deviceContext);
                        break;

                    case false:
                        RenderAbsolutePositionOrtho(context, deviceContext);
                        break;
                    }
                    break;
                }
            }
示例#7
0
        protected override void OnRender(RenderContext context, DeviceContextProxy deviceContext)
        {
            if (resolutionChanged)
            {
                RemoveAndDispose(ref viewResource);
                viewResource = Collect(new ShaderResourceViewProxy(Device, ShadowMapTextureDesc));
                viewResource.CreateView(DepthStencilViewDesc);
                viewResource.CreateView(ShaderResourceViewDesc);
                resolutionChanged = false;
            }

            deviceContext.ClearDepthStencilView(viewResource, DepthStencilClearFlags.Depth, 1.0f, 0);
            context.IsShadowPass = true;
            var orgFrustum = context.BoundingFrustum;
            var frustum    = new BoundingFrustum(LightViewProjectMatrix);

            context.BoundingFrustum = frustum;
#if !TEST
            deviceContext.SetViewport(0, 0, Width, Height);

            deviceContext.SetDepthStencilOnly(viewResource.DepthStencilView);
            for (int i = 0; i < context.RenderHost.PerFrameOpaqueNodes.Count; ++i)
            {
                //Only support opaque object for throwing shadows.
                var core = context.RenderHost.PerFrameOpaqueNodes[i];
                if (core.RenderCore.IsThrowingShadow && core.TestViewFrustum(ref frustum))
                {
                    core.Render(context, deviceContext);
                }
            }

            context.IsShadowPass    = false;
            context.BoundingFrustum = orgFrustum;
            context.RenderHost.SetDefaultRenderTargets(false);
            context.SharedResource.ShadowView = viewResource;
#endif
        }
示例#8
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;
        }
示例#9
0
        protected override void OnRender(RenderContext context, DeviceContextProxy deviceContext)
        {
            #region Initialize textures
            var buffer = context.RenderHost.RenderBuffer;
            if (depthdesc.Width != buffer.TargetWidth ||
                depthdesc.Height != buffer.TargetHeight)
            {
                depthdesc.Width  = buffer.TargetWidth;
                depthdesc.Height = buffer.TargetHeight;

                blurCore.Resize(deviceContext,
                                depthdesc.Width / downSamplingScale,
                                depthdesc.Height / downSamplingScale);
                //Skip this frame to avoid performance hit due to texture creation
                InvalidateRenderer();
                return;
            }
            #endregion

            var depthStencilBuffer = buffer.FullResDepthStencilPool.Get(depthdesc.Format);

            #region Render objects onto offscreen texture
            var renderTargetFull = buffer.FullResPPBuffer.NextRTV;

            deviceContext.ClearDepthStencilView(depthStencilBuffer, DepthStencilClearFlags.Stencil, 0, 0);
            BindTarget(depthStencilBuffer, renderTargetFull, deviceContext, buffer.TargetWidth, buffer.TargetHeight);
            var frustum = context.BoundingFrustum;
            context.IsCustomPass = true;
            bool hasMesh = false;
            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.EffectOutlineP1;
                    var pass = mesh.EffectTechnique[DefaultPassNames.EffectOutlineP1];
                    if (pass.IsNULL)
                    {
                        continue;
                    }
                    pass.BindShader(deviceContext);
                    pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                    mesh.Render(context, deviceContext);
                    hasMesh = true;
                }
            }
            context.IsCustomPass = false;
            #endregion
            if (hasMesh)
            {
                #region Do Blur Pass
                BindTarget(null, blurCore.CurrentRTV, deviceContext, blurCore.Width, blurCore.Height, true);
                blurPassVertical.PixelShader.BindSampler(deviceContext, samplerSlot, sampler);
                blurPassVertical.PixelShader.BindTexture(deviceContext, textureSlot, buffer.FullResPPBuffer.NextSRV);
                blurPassVertical.BindShader(deviceContext);
                blurPassVertical.BindStates(deviceContext, StateType.BlendState | StateType.RasterState | StateType.DepthStencilState);
                deviceContext.Draw(4, 0);

                blurCore.Run(deviceContext, NumberOfBlurPass, 1, 0);//Already blur once on vertical, pass 1 as initial index.
                #endregion

                #region Draw back with stencil test
                BindTarget(depthStencilBuffer, renderTargetFull, deviceContext, buffer.TargetWidth, buffer.TargetHeight);
                screenQuadPass.PixelShader.BindTexture(deviceContext, textureSlot, blurCore.CurrentSRV);
                screenQuadPass.BindShader(deviceContext);
                screenQuadPass.BindStates(deviceContext, StateType.BlendState | StateType.RasterState | StateType.DepthStencilState);
                deviceContext.Draw(4, 0);
                #endregion

                #region Draw outline onto original target
                BindTarget(null, buffer.FullResPPBuffer.CurrentRTV, deviceContext, buffer.TargetWidth, buffer.TargetHeight, false);
                screenOutlinePass.PixelShader.BindTexture(deviceContext, textureSlot, buffer.FullResPPBuffer.NextSRV);
                screenOutlinePass.BindShader(deviceContext);
                screenOutlinePass.BindStates(deviceContext, StateType.BlendState | StateType.RasterState | StateType.DepthStencilState);
                deviceContext.Draw(4, 0);
                screenOutlinePass.PixelShader.BindTexture(deviceContext, textureSlot, null);
                #endregion
            }
            buffer.FullResDepthStencilPool.Put(depthdesc.Format, depthStencilBuffer);
        }
示例#10
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 dPass              = EnableDoublePass;
                var depthStencilBuffer = buffer.DepthStencilBufferNoMSAA;

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

                deviceContext.SetViewport(ref viewport);
                deviceContext.SetScissorRectangle(ref viewport);
                deviceContext.ClearDepthStencilView(depthStencilBuffer, DepthStencilClearFlags.Stencil, 1, 0);
                if (dPass)
                {
                    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.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 (var i = 0; i < currentCores.Count; ++i)
                    {
                        var mesh   = currentCores[i];
                        var effect = mesh.Value;
                        var color  = Color;
                        if (effect.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 = 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 (var i = 0; i < context.RenderHost.PerFrameNodesWithPostEffect.Count; ++i)
                    {
                        var mesh = context.RenderHost.PerFrameNodesWithPostEffect[i];
                        if (mesh.TryGetPostEffect(EffectName, out var effect))
                        {
                            var color = Color;
                            if (effect.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 = 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);
                        }
                    }
                }
            }
        protected override void OnRender(RenderContext context, DeviceContextProxy deviceContext)
        {
            #region Initialize textures
            var buffer = context.RenderHost.RenderBuffer;
            if (depthdesc.Width != buffer.TargetWidth ||
                depthdesc.Height != buffer.TargetHeight)
            {
                depthdesc.Width  = buffer.TargetWidth;
                depthdesc.Height = buffer.TargetHeight;

                blurCore.Resize(deviceContext,
                                depthdesc.Width / downSamplingScale,
                                depthdesc.Height / downSamplingScale);
                //Skip this frame to avoid performance hit due to texture creation
                InvalidateRenderer();
                return;
            }
            #endregion

            var depthStencilBuffer = buffer.FullResDepthStencilPool.Get(depthdesc.Format);
            var renderTargetFull   = buffer.FullResPPBuffer.NextRTV;

            var frustum = context.BoundingFrustum;
            if (drawMode == OutlineMode.Separated)
            {
                for (int i = 0; i < context.RenderHost.PerFrameNodesWithPostEffect.Count; ++i)
                {
                    #region Render objects onto offscreen texture
                    var mesh = context.RenderHost.PerFrameNodesWithPostEffect[i];
                    if (context.EnableBoundingFrustum && !mesh.TestViewFrustum(ref frustum))
                    {
                        continue;
                    }
                    BindTarget(depthStencilBuffer, renderTargetFull, deviceContext, buffer.TargetWidth, buffer.TargetHeight);
                    deviceContext.ClearDepthStencilView(depthStencilBuffer, DepthStencilClearFlags.Stencil, 0, 0);
                    modelCB.Upload(deviceContext, ref modelStruct);
                    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.EffectOutlineP1;
                        var pass = mesh.EffectTechnique[DefaultPassNames.EffectOutlineP1];
                        if (pass.IsNULL)
                        {
                            continue;
                        }
                        pass.BindShader(deviceContext);
                        pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                        mesh.RenderCustom(context, deviceContext);
                        DrawOutline(context, deviceContext, depthStencilBuffer, renderTargetFull);
                    }
                    #endregion
                }
            }
            else
            {
                BindTarget(depthStencilBuffer, renderTargetFull, deviceContext, buffer.TargetWidth, buffer.TargetHeight);
                #region Render objects onto offscreen texture
                deviceContext.ClearDepthStencilView(depthStencilBuffer, DepthStencilClearFlags.Stencil, 0, 0);
                bool hasMesh = false;
                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))
                    {
                        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.EffectOutlineP1;
                        var pass = mesh.EffectTechnique[DefaultPassNames.EffectOutlineP1];
                        if (pass.IsNULL)
                        {
                            continue;
                        }
                        pass.BindShader(deviceContext);
                        pass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState);
                        mesh.RenderCustom(context, deviceContext);
                        hasMesh = true;
                    }
                }
                #endregion
                if (hasMesh)
                {
                    DrawOutline(context, deviceContext, depthStencilBuffer, renderTargetFull);
                }
            }
            buffer.FullResDepthStencilPool.Put(depthdesc.Format, depthStencilBuffer);
        }
示例#12
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();
            }
示例#13
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();
        }
示例#14
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();
                }
            }
示例#15
0
            public override void Render(RenderContext context, DeviceContextProxy deviceContext)
            {
                using (var back = context.GetOffScreenRT(OffScreenTextureSize.Full, global::SharpDX.DXGI.Format.R16G16B16A16_Float))
                {
                    var slot = 0;
                    using (var depth = context.GetOffScreenDS(OffScreenTextureSize.Full, global::SharpDX.DXGI.Format.D32_Float_S8X24_UInt))
                    {
                        deviceContext.ClearDepthStencilView(depth, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1, 1);
                        deviceContext.ClearRenderTargetView(back, new Color4(0, 0, 0, 0));
                        BindTarget(depth, back, deviceContext, (int)context.ActualWidth, (int)context.ActualHeight);
                        #region Render box back face and set stencil buffer to 0
                        modelMatrices.Update(ref ModelMatrix);
                        if (!materialVariables.UpdateMaterialStruct(deviceContext, ref modelMatrices))
                        {
                            return;
                        }
                        buffer.AttachBuffers(deviceContext, ref slot, EffectTechnique.EffectsManager);
                        cubeBackPass.BindShader(deviceContext);
                        cubeBackPass.BindStates(deviceContext, StateType.All);
                        deviceContext.DrawIndexed(buffer.IndexBuffer.ElementCount, 0, 0);
                        #endregion

                        #region Render all mesh Positions onto off-screen texture region with stencil = 0 only
                        if (context.RenderHost.PerFrameOpaqueNodesInFrustum.Count > 0)
                        {
                            for (var i = 0; i < context.RenderHost.PerFrameOpaqueNodesInFrustum.Count; ++i)
                            {
                                var mesh     = context.RenderHost.PerFrameOpaqueNodesInFrustum[i];
                                var meshPass = mesh.EffectTechnique[DefaultPassNames.Positions];
                                if (meshPass.IsNULL)
                                {
                                    continue;
                                }
                                meshPass.BindShader(deviceContext);
                                meshPass.BindStates(deviceContext, StateType.BlendState);
                                // Set special depth stencil state to only render into region with stencil region is 0
                                meshFrontPass.BindStates(deviceContext, StateType.DepthStencilState);
                                mesh.RenderCustom(context, deviceContext);
                            }
                        }
                        #endregion
                    }

                    #region Render box back face again and do actual volume sampling
                    context.RenderHost.SetDefaultRenderTargets(false);
                    var pass = materialVariables.GetPass(RenderType.Opaque, context);
                    if (pass != volumePass)
                    {
                        volumePass  = pass;
                        backTexSlot = volumePass.PixelShader.ShaderResourceViewMapping.TryGetBindSlot(DefaultBufferNames.VolumeBack);
                    }
                    slot = 0;
                    buffer.AttachBuffers(deviceContext, ref slot, EffectTechnique.EffectsManager);
                    materialVariables.BindMaterialResources(context, deviceContext, pass);
                    volumePass.PixelShader.BindTexture(deviceContext, backTexSlot, back);
                    volumePass.BindShader(deviceContext);
                    volumePass.BindStates(deviceContext, StateType.All);
                    deviceContext.DrawIndexed(buffer.IndexBuffer.ElementCount, 0, 0);
                    #endregion
                    volumePass.PixelShader.BindTexture(deviceContext, backTexSlot, null);
                }
            }