public override void Draw(StandardFrameData frameData)
        {
            // Clear
            DepthStencilView depthStencilView = RenderBackend.DisplayRef.DepthStencilViewRef;
            RenderTargetView renderTargetView = RenderBackend.DisplayRef.RenderTargetViewRef;

            GetContext.ClearRenderTargetView(renderTargetView, Color.Gray);
            GetContext.ClearRenderTargetView(hdrRenderTargetView, Color.Gray);
            GetContext.ClearDepthStencilView(
                depthStencilView,
                DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil,
                0.0f, 0
                );

            if (isWireframe)
            {
                WireframePass(frameData, depthStencilView, renderTargetView);
                return;
            }
            //DepthPrePassWithVelocity(frameData, depthStencilView);
            DepthPrePass(frameData, depthStencilView, renderTargetView);
            // TODO: shadow maps draw pass
            ColourPass(frameData, depthStencilView, renderTargetView);
            ScreenQuadPass(frameData, depthStencilView, renderTargetView);
        }
        public override void Draw(StandardFrameData frameData)
        {
            // Clear
            DepthStencilView depthStencilView = GetDisplay.DepthStencilViewRef;
            RenderTargetView renderTargetView = GetDisplay.RenderTargetViewRef;

            GetContext.ClearRenderTargetView(renderTargetView, Color.Gray);
            GetContext.ClearDepthStencilView(depthStencilView,
                                             DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 0.0f, 0);

            DepthPrePass(frameData, depthStencilView, renderTargetView);
            LightCullingPass(frameData);
            ColourPass(frameData, depthStencilView, renderTargetView);
            //TODO: colour pass
        }
        private void DepthPrePassWithVelocity(StandardFrameData frameData, DepthStencilView depthStencilView)
        {
            CurrentPass = Pass.DepthPrePass;
            // Setup targets and states
            GetContext.OutputMerger.SetTargets(depthStencilView, (RenderTargetView)null);
            SetDepthStencilState(DepthStencilStates.Greater);
            GetContext.InputAssembler.InputLayout = DepthPassLayout;

            // Setup vertex shader

            SetVertexShader("ForwardPlusPosOnlyVS");

            // Cleanup pixel shader
            GetContext.PixelShader.Set(null);
            for (int i = 0; i < 2; i++)
            {
                GetContext.PixelShader.SetShaderResources(0, i, (ShaderResourceView)null);
            }
            GetContext.PixelShader.SetSamplers(0, 1, (SamplerState)null);

            // Draw opaque
            string MeshName      = "";
            int    MaterialQueue = -999999;

            DynamicMeshes.Clear();
            foreach (var rendererData in frameData.RenderersList)
            {
                if (rendererData.IsDynamic)
                {
                    DynamicMeshes.Add(rendererData);
                    continue;
                }
                if (MaterialQueue != rendererData.MaterialQueue)
                {
                    MaterialQueue = rendererData.MaterialQueue;

                    // TODO: correct changing
                    //SetMergerStates(AssetsLoader.LoadMaterial(rendererData.MaterialName).MetaMaterial);*/

                    //SetMergerStates(MaterialQueue);
                }

                if (MeshName != rendererData.MeshName)
                {
                    MeshName = rendererData.MeshName;
                    SetMesh(MeshName);
                }

                m_PerObjectConstBuffer.WorldMatrix                 = rendererData.TransformMatrix;
                m_PerObjectConstBuffer.WorldViewMatrix             = rendererData.TransformMatrix * frameData.CamerasList[0].View;
                m_PerObjectConstBuffer.WorldViewProjMatrix         = rendererData.TransformMatrix * frameData.CamerasList[0].ViewProjection;
                m_PerObjectConstBuffer.PreviousWorldViewProjMatrix = rendererData.PreviousTransformMatrix * frameData.CamerasList[0].PreviousViewProjection;
                GetContext.UpdateSubresource(ref m_PerObjectConstBuffer, PerObjConstantBuffer);

                RenderBackend.DrawIndexedWrapper(m_CachedMesh.IndexCount, 0, 0);
            }

            GetContext.OutputMerger.SetTargets(depthStencilView, velocityRenderTargetView);
            GetContext.ClearRenderTargetView(velocityRenderTargetView, Color.Yellow);

            // Setup pixel shader
            SetPixelShader("VelocityPS");

            foreach (var rendererData in DynamicMeshes)
            {
                if (MaterialQueue != rendererData.MaterialQueue)
                {
                    MaterialQueue = rendererData.MaterialQueue;

                    // TODO: correct changing
                    //SetMergerStates(AssetsLoader.LoadMaterial(rendererData.MaterialName).MetaMaterial);*/

                    //SetMergerStates(MaterialQueue);
                }

                if (MeshName != rendererData.MeshName)
                {
                    MeshName = rendererData.MeshName;
                    SetMesh(MeshName);
                }

                m_PerObjectConstBuffer.WorldMatrix                 = rendererData.TransformMatrix;
                m_PerObjectConstBuffer.WorldViewMatrix             = rendererData.TransformMatrix * frameData.CamerasList[0].View;
                m_PerObjectConstBuffer.WorldViewProjMatrix         = rendererData.TransformMatrix * frameData.CamerasList[0].ViewProjection;
                m_PerObjectConstBuffer.PreviousWorldViewProjMatrix = rendererData.PreviousTransformMatrix * frameData.CamerasList[0].PreviousViewProjection;
                GetContext.UpdateSubresource(ref m_PerObjectConstBuffer, PerObjConstantBuffer);

                RenderBackend.DrawIndexedWrapper(m_CachedMesh.IndexCount, 0, 0);
            }
            //TODO: Alpha to coverage pre-pass
        }
Example #4
0
        protected void DepthPrePass(StandardFrameData frameData)
        {
            CurrentPass = Pass.DepthPrePass;

            GetContext.ClearRenderTargetView(GetDisplay.RenderTargetViewRef, Color.CornflowerBlue);
            GetContext.ClearDepthStencilView(
                GetDisplay.DepthStencilViewRef,
                DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil,
                0.0f, 0
                );

            GetContext.OutputMerger.SetTargets(GetDisplay.DepthStencilViewRef, (RenderTargetView)null);
            SetDepthStencilState(DepthStencilStates.Greater);
            SetRasterizerState(RasterizerStates.SolidBackCull);

            SetVertexShader("ForwardPlusPosOnlyVS");
            SetNullPixelShader();
            GetContext.InputAssembler.InputLayout = GetSharedItems.StandardInputLayout;

            m_PerFrameConstBuffer = new CommonStructs.ConstBufferPerFrameStruct()
            {
                Projection    = frameData.CamerasList[0].Projection,
                ProjectionInv = Matrix.Invert(frameData.CamerasList[0].Projection),
                ViewInv       = Matrix.Invert(frameData.CamerasList[0].View),
                PreviousView  = frameData.CamerasList[0].PreviousView,
                CameraPos     = frameData.CamerasList[0].Position,
                CameraForward = new Vector4(frameData.CamerasList[0].Forward, 1),
                AlphaTest     = 0.5f,
                WindowWidth   = (uint)GetDisplay.Width,
                WindowHeight  = (uint)GetDisplay.Height,
                CurrentFPS    = (uint)(1.0f / RenderBackend.EngineRef.Time.DeltaTime),
            };
            GetContext.UpdateSubresource(ref m_PerFrameConstBuffer, PerFrameConstantBuffer);

            GetContext.VertexShader.SetConstantBuffer(0, PerObjConstantBuffer);
            GetContext.VertexShader.SetConstantBuffer(1, PerFrameConstantBuffer);
            GetContext.PixelShader.SetConstantBuffer(0, PerObjConstantBuffer);
            GetContext.PixelShader.SetConstantBuffer(1, PerFrameConstantBuffer);
            //GetContext.PixelShader.SetConstantBuffer(2, LightsDataBuffer);
            GetContext.PixelShader.SetConstantBuffer(3, ShadowLightsDataBuffer);
            GetContext.UpdateSubresource(ref m_PerFrameConstBuffer, PerFrameConstantBuffer);

            string MeshName        = "";
            string MaterialName    = "";
            bool   IsMaskedSubPass = false;

            if (postProcessSettings.MotionBlurEnabled)
            {
                GetContext.ClearRenderTargetView(VelocityTarget.View, Color.Yellow);
                DynamicMeshes.Clear();
            }

            StandardFrameData.RendererData rendererData;
            foreach (var index in frameData.PerCameraRenderersList[0])
            {
                rendererData = frameData.RenderersList[index];
                if (postProcessSettings.MotionBlurEnabled && rendererData.IsDynamic)
                {
                    DynamicMeshes.Add(rendererData);
                    continue;
                }

                if (MeshName != rendererData.MeshName)
                {
                    MeshName = rendererData.MeshName;
                    SetMesh(MeshName);
                }

                if (MaterialName != rendererData.MaterialName)
                {
                    MaterialName = rendererData.MaterialName;
                    SetMaterial(MaterialName);
                    if (!IsMaskedSubPass)
                    {
                        if (m_CachedMaterial.MetaMaterial.blendMode == ShaderGraph.MetaMaterial.BlendMode.Masked)
                        {
                            GetContext.OutputMerger.SetTargets(GetDisplay.DepthStencilViewRef, GetDisplay.RenderTargetViewRef);
                            if (EnabledMSAA)
                            {
                                SetBlendState(BlendStates.DepthOnlyAlphaToCoverage);
                            }
                            else
                            {
                                SetBlendState(BlendStates.DepthOnlyAlphaTest);
                            }
                            DepthMaskedSubPath(false);
                            IsMaskedSubPass = true;
                        }
                    }
                    else
                    {
                        GetContext.PixelShader.SetShaderResource(0,
                                                                 GetSharedItems.LoadTextureSRV(m_CachedMaterial.AlbedoMapAsset));
                    }
                    if (m_CachedMaterial.MetaMaterial.blendMode > ShaderGraph.MetaMaterial.BlendMode.Masked)
                    {
                        break; // Break on translucent objects
                    }
                }

                m_PerObjectConstBuffer.WorldMatrix         = rendererData.TransformMatrix;
                m_PerObjectConstBuffer.WorldViewMatrix     = rendererData.TransformMatrix * frameData.CamerasList[0].View;
                m_PerObjectConstBuffer.WorldViewProjMatrix = rendererData.TransformMatrix * frameData.CamerasList[0].ViewProjection;
                m_PerObjectConstBuffer.textureTiling       = m_CachedMaterial.PropetyBlock.Tile;
                m_PerObjectConstBuffer.textureShift        = m_CachedMaterial.PropetyBlock.Shift;
                GetContext.UpdateSubresource(ref m_PerObjectConstBuffer, PerObjConstantBuffer);

                DX_DrawIndexed(m_CachedMesh.IndexCount, 0, 0);
            }

            if (!postProcessSettings.MotionBlurEnabled || DynamicMeshes.Count == 0)
            {
                return;
            }

            GetContext.OutputMerger.SetTargets(GetDisplay.DepthStencilViewRef, VelocityTarget.View);
            SetBlendState(BlendStates.Opaque);
            SetRasterizerState(RasterizerStates.SolidBackCull);

            SetVertexShader("ForwardPlusPosOnlyVS");
            SetPixelShader("VelocityPS");

            IsMaskedSubPass = false;
            foreach (var dRendererData in DynamicMeshes)
            {
                if (MeshName != dRendererData.MeshName)
                {
                    MeshName = dRendererData.MeshName;
                    SetMesh(MeshName);
                }

                if (MaterialName != dRendererData.MaterialName)
                {
                    MaterialName = dRendererData.MaterialName;
                    SetMaterial(MaterialName);

                    if (!IsMaskedSubPass)
                    {
                        if (m_CachedMaterial.MetaMaterial.blendMode == ShaderGraph.MetaMaterial.BlendMode.Masked)
                        {
                            DepthMaskedSubPath(true);
                            IsMaskedSubPass = true;
                        }
                    }
                    else
                    {
                        GetContext.PixelShader.SetShaderResource(0,
                                                                 GetSharedItems.LoadTextureSRV(m_CachedMaterial.AlbedoMapAsset));
                    }

                    if (m_CachedMaterial.MetaMaterial.blendMode > ShaderGraph.MetaMaterial.BlendMode.Masked)
                    {
                        break; // Break on translucent objects
                    }
                }

                m_PerObjectConstBuffer.WorldMatrix                 = dRendererData.TransformMatrix;
                m_PerObjectConstBuffer.WorldViewMatrix             = dRendererData.TransformMatrix * frameData.CamerasList[0].View;
                m_PerObjectConstBuffer.WorldViewProjMatrix         = dRendererData.TransformMatrix * frameData.CamerasList[0].ViewProjection;
                m_PerObjectConstBuffer.PreviousWorldViewProjMatrix = dRendererData.PreviousTransformMatrix * frameData.CamerasList[0].PreviousViewProjection;
                GetContext.UpdateSubresource(ref m_PerObjectConstBuffer, PerObjConstantBuffer);

                DX_DrawIndexed(m_CachedMesh.IndexCount, 0, 0);
            }
        }
Example #5
0
        protected void ShadowMapsPass(StandardFrameData frameData)
        {
            CurrentPass = Pass.ShadowMapsPass;

            GetContext.ClearRenderTargetView(ShadowMapsAtlasTarget.View, Color.White);
            GetContext.ClearDepthStencilView(
                ShadowMapsAtlasDepthTarget.View,
                DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil,
                1.0f, 0
                );
            GetContext.OutputMerger.SetTargets(ShadowMapsAtlasDepthTarget.View, ShadowMapsAtlasTarget.View);

            SetDepthStencilState(DepthStencilStates.Less);
            // TODO: select right cull mode
            SetRasterizerState(RasterizerStates.SolidFrontCull);

            Viewport atlasViewport = new Viewport(0, 0, ShadowAtlasSize, ShadowAtlasSize, 0.0f, 1.0f);

            GetContext.Rasterizer.SetViewport(atlasViewport);

            SetVertexShader("DepthShadowsVS");
            GetContext.VertexShader.SetConstantBuffer(0, PerObjectShadowPassConstBuffer);
            SetPixelShader("DepthShadowsPS");
            //SetNullPixelShader();

            string MeshName        = "";
            string MaterialName    = "";
            bool   IsMaskedSubPass = false;

            StandardFrameData.RendererData rendererData;
            foreach (var light in frameData.LightsList)
            {
                if (!light.IsCastShadows)
                {
                    continue;
                }
                foreach (int index in frameData.PerLightRenderersList[light.index])
                {
                    rendererData = frameData.RenderersList[index];

                    if (MeshName != rendererData.MeshName)
                    {
                        MeshName = rendererData.MeshName;
                        SetMesh(MeshName);
                    }

                    if (MaterialName != rendererData.MaterialName)
                    {
                        MaterialName = rendererData.MaterialName;
                        SetMaterial(MaterialName);

                        /*
                         * if (!IsMaskedSubPass)
                         * {
                         *  if (m_CachedMaterial.MetaMaterial.blendMode == ShaderGraph.MetaMaterial.BlendMode.Masked)
                         *  {
                         *      GetContext.OutputMerger.SetTargets(GetDisplay.DepthStencilViewRef, GetDisplay.RenderTargetViewRef);
                         *      if (postProcessSettings.UsingMSAA) {
                         *          SetBlendState(SharedRenderItemsStorage.BlendStates.DepthOnlyAlphaToCoverage);
                         *      } else {
                         *          SetBlendState(SharedRenderItemsStorage.BlendStates.DepthOnlyAlphaTest);
                         *      }
                         *      //DepthMaskedSubPath(false);
                         *      IsMaskedSubPass = true;
                         *  }
                         * }
                         * else
                         * {
                         *  GetContext.PixelShader.SetShaderResource(0,
                         *      GetSharedItems.LoadTextureSRV(m_CachedMaterial.AlbedoMapAsset));
                         * }
                         */
                        if (m_CachedMaterial.MetaMaterial.blendMode > ShaderGraph.MetaMaterial.BlendMode.Masked)
                        {
                            break; // Break on translucent objects
                        }
                    }

                    m_PerObjectShadowPassConstBuffer.WorldMatrix          = rendererData.TransformMatrix;
                    m_PerObjectShadowPassConstBuffer.ViewProjectionMatrix = light.ViewProjection;
                    GetContext.UpdateSubresource(ref m_PerObjectShadowPassConstBuffer, PerObjectShadowPassConstBuffer);

                    DX_DrawIndexed(m_CachedMesh.IndexCount, 0, 0);
                }
                break;
            }

            GetContext.Rasterizer.SetViewport(new Viewport(
                                                  0, 0,
                                                  GetDisplay.Width,
                                                  GetDisplay.Height,
                                                  0.0f, 1.0f
                                                  ));
        }