Пример #1
0
        void Render()
        {
            // Clear targets
            _immediateContext.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
            //_immediateContext.ClearRenderTargetView(renderView, ambient);
            //_immediateContext.ClearRenderTargetView(gBufferLightView, ambient);
            _immediateContext.ClearRenderTargetView(gBufferNormalView, ambient);
            _immediateContext.ClearRenderTargetView(gBufferDiffuseView, ambient);

            _meshFactory.InitInstancedRender(Demo.World.CollisionObjectArray);

            // Light depth map pass
            if (shadowsEnabled)
            {
                _immediateContext.ClearDepthStencilView(lightDepthView, DepthStencilClearFlags.Depth, 1.0f, 0);
                outputMerger.SetDepthStencilState(lightDepthStencilState);
                outputMerger.SetRenderTargets(lightDepthView);
                shadowGenPass.Apply(_immediateContext);
                OnRender();
                lightDepthMapVar.SetResource(lightDepthRes);
            }

            // Render to G-buffer
            lightBufferVar.SetResource(null);
            normalBufferVar.SetResource(null);
            diffuseBufferVar.SetResource(null);
            depthMapVar.SetResource(null);
            lightDepthMapVar.SetResource(null);

            outputMerger.SetDepthStencilState(depthStencilState);
            outputMerger.SetTargets(depthView, gBufferViews);
            gBufferGenPass.Apply(_immediateContext);
            OnRender();

            if (Demo.IsDebugDrawEnabled)
            {
                debugDrawPass.Apply(_immediateContext);
                (Demo.World.DebugDrawer as PhysicsDebugDraw).DrawDebugWorld(Demo.World);
            }

            outputMerger.SetDepthStencilState(null);
            info.OnRender(Demo.FramesPerSecond);


            outputMerger.SetTargets(renderView);
            inputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;

            // Render G-buffer
            lightBufferVar.SetResource(lightBufferRes);
            normalBufferVar.SetResource(normalBufferRes);
            diffuseBufferVar.SetResource(diffuseBufferRes);
            depthMapVar.SetResource(depthRes);
            lightDepthMapVar.SetResource(lightDepthRes);
            gBufferRenderPass.Apply(_immediateContext);
            _immediateContext.Draw(3, 0);


            // Render overlay
            outputMerger.SetBlendState(alphaBlendState);
            diffuseBufferVar.SetResource(info.OverlayBufferRes);
            gBufferOverlayPass.Apply(_immediateContext);
            _immediateContext.Draw(4, 0);

            _swapChain.Present(0, PresentFlags.None);
        }
        void Render()
        {
            // Clear targets
            _immediateContext.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
            //_immediateContext.ClearRenderTargetView(renderView, ambient);
            _immediateContext.ClearRenderTargetView(gBufferLightView, Color4.Black);
            _immediateContext.ClearRenderTargetView(gBufferNormalView, Color4.Black);
            _immediateContext.ClearRenderTargetView(gBufferDiffuseView, Color4.Black);


            // Read collision object transforms, create geometry, etc.
            _meshFactory.InitInstancedRender(Demo.World.CollisionObjectArray);


            // Light depth map pass
            if (shadowsEnabled)
            {
                _immediateContext.ClearDepthStencilView(lightDepthView, DepthStencilClearFlags.Depth, 1.0f, 0);
                outputMerger.SetDepthStencilState(lightDepthStencilState);
                outputMerger.SetRenderTargets(lightDepthView);
                shadowGenPass.Apply(_immediateContext);
                OnRender();
                shadowLightDepthBufferVar.SetResource(lightDepthRes);
            }

            // Render geometry (colors, normals, depth) to G-buffer
            outputMerger.SetDepthStencilState(depthState);
            outputMerger.SetTargets(depthView, gBufferViews);
            gBufferGenPass.Apply(_immediateContext);
            OnRender();

            if (Demo.IsDebugDrawEnabled)
            {
                debugDrawPass.Apply(_immediateContext);
                (Demo.World.DebugDrawer as PhysicsDebugDraw).DrawDebugWorld(Demo.World);
            }


            // Light accumulation to G-buffer
            outputMerger.SetBlendState(additiveBlendState);
            // Can't set depthView as render target and shader variable at the same time,
            // so early HW depth test is not available for light volumes.
            //outputMerger.SetTargets(depthView, gBufferLightView);
            outputMerger.SetTargets(gBufferLightView);
            RenderLights();


            // Render G-buffer
            outputMerger.SetBlendState(alphaBlendState);
            outputMerger.SetDepthStencilState(null);
            if (depthOfFieldEnabled)
            {
                outputMerger.SetTargets(gBufferPostProcessView);
            }
            else
            {
                outputMerger.SetTargets(renderView);
            }
            inputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;

            lightBufferVar.SetResource(lightBufferRes);
            normalBufferVar.SetResource(normalBufferRes);
            diffuseBufferVar.SetResource(diffuseBufferRes);
            depthMapVar.SetResource(depthBufferRes);

            //_immediateContext.ClearRenderTargetView(gBufferPostProcessView, Color4.Black);
            gBufferRenderPass.Apply(_immediateContext);
            _immediateContext.Draw(3, 0);

            if (depthOfFieldEnabled)
            {
                diffuseBufferVar.SetResource(postProcessBufferRes);
                outputMerger.SetTargets(gBufferPostProcessViewBlur1);
                gBufferPostProcessPass.Apply(_immediateContext);
                _immediateContext.Draw(3, 0);

                diffuseBufferVar.SetResource(postProcessBufferBlur1Res);
                outputMerger.SetTargets(gBufferPostProcessViewBlur2);
                gBufferPostProcessPass.Apply(_immediateContext);
                _immediateContext.Draw(3, 0);


                diffuseBufferVar.SetResource(postProcessBufferBlur2Res);
                outputMerger.SetTargets(gBufferPostProcessViewBlur1);
                gBufferPostProcessPass.Apply(_immediateContext);
                _immediateContext.Draw(3, 0);


                diffuseBufferVar.SetResource(postProcessBufferBlur1Res);
                outputMerger.SetTargets(gBufferPostProcessViewBlur2);
                gBufferPostProcessPass.Apply(_immediateContext);
                _immediateContext.Draw(3, 0);


                diffuseBufferVar.SetResource(postProcessBufferRes);
                normalBufferVar.SetResource(postProcessBufferBlur2Res);
                outputMerger.SetTargets(renderView);
                gBufferPostProcessPass2.Apply(_immediateContext);
                _immediateContext.Draw(3, 0);
            }


            // Render overlay
            info.OnRender(Demo.FramesPerSecond);
            outputMerger.SetBlendState(alphaBlendState);
            diffuseBufferVar.SetResource(info.OverlayBufferRes);
            gBufferOverlayPass.Apply(_immediateContext);
            _immediateContext.Draw(4, 0);

            _swapChain.Present(0, PresentFlags.None);
        }