private void WireframePass(StandardFrameData frameData, DepthStencilView depthStencilView, RenderTargetView renderTargetView)
        {
            CurrentPass = Pass.Wireframe;
            GetContext.OutputMerger.SetTargets(depthStencilView, renderTargetView);
            SetDepthStencilState(DepthStencilStates.Greater);

            // Bind buffers
            m_PerFrameConstBuffer = new CommonStructs.ConstBufferPerFrameStruct()
            {
                Projection          = frameData.CamerasList[0].Projection,
                ProjectionInv       = Matrix.Invert(frameData.CamerasList[0].Projection),
                CameraPos           = frameData.CamerasList[0].Position,
                AlphaTest           = 0.5f,
                MaxNumLightsPerTile = (uint)0,
                //TODO: provide lights number
                NumLights    = (uint)400,
                WindowHeight = (uint)RenderBackend.DisplayRef.Height,
                WindowWidth  = (uint)RenderBackend.DisplayRef.Width,
            };
            GetContext.UpdateSubresource(ref m_PerFrameConstBuffer, PerFrameConstantBuffer);

            SetVertexShader("CommonVS");
            SetPixelShader("ForwardPlusPosTexPS");
            GetContext.InputAssembler.InputLayout = GetSharedItems.StandardInputLayout;

            SetRasterizerState(RasterizerStates.WireframeBackCull);
            m_PerObjectConstBuffer.AlbedoColor = new Vector4(0.8f, 0.5f, 0.5f, 1.0f);

            string MeshName = "";

            foreach (var rendererData in frameData.RenderersList)
            {
                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;
                GetContext.UpdateSubresource(ref m_PerObjectConstBuffer, PerObjConstantBuffer);

                RenderBackend.DrawIndexedWrapper(m_CachedMesh.IndexCount, 0, 0);
            }
        }
        private void ColourPass(StandardFrameData frameData, DepthStencilView depthStencilView, RenderTargetView renderTargetView)
        {
            CurrentPass = Pass.ColourPass;
            // Setup targets and states
            // renderTargetView
            GetContext.OutputMerger.SetRenderTargets(depthStencilView, hdrRenderTargetView);
            SetDepthStencilState(DepthStencilStates.EqualAndDisableWrite);

            // Setup vertex shader
            SetVertexShader("CommonVS");
            GetContext.InputAssembler.InputLayout = GetSharedItems.StandardInputLayout;

            // Bind buffers
            m_PerFrameConstBuffer = new CommonStructs.ConstBufferPerFrameStruct()
            {
                Projection          = frameData.CamerasList[0].Projection,
                ProjectionInv       = Matrix.Invert(frameData.CamerasList[0].Projection),
                CameraPos           = frameData.CamerasList[0].Position,
                AlphaTest           = 0.5f,
                MaxNumLightsPerTile = (uint)0,
                //TODO: provide lights number
                NumLights    = (uint)400,
                WindowHeight = (uint)RenderBackend.DisplayRef.Height,
                WindowWidth  = (uint)RenderBackend.DisplayRef.Width,
            };

            int n = frameData.LightsList.Count;

            n = n > 3 ? 3 : n;
            // Lights buffer max x3
            StandardFrameData.LightData light;
            for (int i = 0; i < n; i++)
            {
                light = frameData.LightsList[i];
                m_LightBuffer[i].viewProjMatrix = light.ViewProjection;
                m_LightBuffer[i].lightTint      = Vector4.One;
                m_LightBuffer[i].type           = (float)light.Type;
                m_LightBuffer[i].position       = light.Position;
                m_LightBuffer[i].direction      = light.Direction;
                m_LightBuffer[i].distanceSqr    = light.Radius * light.Radius;
            }

            GetContext.UpdateSubresource(ref m_PerFrameConstBuffer, PerFrameConstantBuffer);
            GetContext.UpdateSubresource(m_LightBuffer, LightBuffer);

            // Draw scene
            string MeshName      = "";
            string MaterialName  = "";
            int    MaterialQueue = -999999;

            foreach (var rendererData in frameData.RenderersList)
            {
                if (MaterialName != rendererData.MaterialName)
                {
                    MaterialName = rendererData.MaterialName;
                    SetMaterial(MaterialName, MaterialQueue != rendererData.MaterialQueue);
                    MaterialQueue = rendererData.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;
                GetContext.UpdateSubresource(ref m_PerObjectConstBuffer, PerObjConstantBuffer);

                RenderBackend.DrawIndexedWrapper(m_CachedMesh.IndexCount, 0, 0);
            }
        }
        private void DepthPrePass(StandardFrameData frameData, DepthStencilView depthStencilView, RenderTargetView renderTargetView)
        {
            CurrentPass = Pass.DepthPrePass;
            // Setup targets and states
            GetContext.OutputMerger.SetTargets(depthStencilView, (RenderTargetView)null);

            SetDepthStencilState(DepthStencilStates.Greater);

            SetRasterizerState(RasterizerStates.SolidBackCull);

            GetContext.InputAssembler.InputLayout = GetSharedItems.StandardInputLayout;

            // Setup vertex shader
            GetContext.VertexShader.Set(AssetsLoader.GetShader <VertexShader>("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);

            m_PerFrameConstBuffer = new CommonStructs.ConstBufferPerFrameStruct()
            {
                Projection          = frameData.CamerasList[0].Projection,
                ProjectionInv       = Matrix.Invert(frameData.CamerasList[0].Projection),
                CameraPos           = frameData.CamerasList[0].Position,
                AlphaTest           = 0.5f,
                MaxNumLightsPerTile = (uint)0,
                //TODO: provide lights number
                NumLights    = (uint)400,
                WindowHeight = (uint)RenderBackend.DisplayRef.Height,
                WindowWidth  = (uint)RenderBackend.DisplayRef.Width,
            };
            GetContext.UpdateSubresource(ref m_PerFrameConstBuffer, PerFrameConstantBuffer);

            string MeshName     = "";
            string MaterialName = "";
            bool   IsOpaquePass = true;

            foreach (var rendererData in frameData.RenderersList)
            {
                if (MeshName != rendererData.MeshName)
                {
                    MeshName = rendererData.MeshName;
                    SetMesh(MeshName);
                }

                if (MaterialName != rendererData.MaterialName)
                {
                    MaterialName = rendererData.MaterialName;
                    SetMaterial(MaterialName, false);
                    if (IsOpaquePass)
                    {
                        if (CurrentMaterialInstance.MetaMaterial.blendMode == ShaderGraph.MetaMaterial.BlendMode.Masked)
                        {
                            GetContext.OutputMerger.SetTargets(depthStencilView, renderTargetView);
                            SetRasterizerState(RasterizerStates.SolidNoneCull);
                            GetContext.PixelShader.SetSampler(0,
                                                              GetSharedItems.GetSamplerState(SamplerType.AnisotropicWrap));
                            //if (msaa) DepthOnlyAlphaToCoverageState
                            SetBlendState(BlendStates.DepthOnlyAlphaTest);

                            GetContext.VertexShader.Set(AssetsLoader.GetShader <VertexShader>("ForwardPlusPosTexVS"));
                            GetContext.PixelShader.Set(AssetsLoader.GetShader <PixelShader>("ForwardPlusPosTexPS"));
                            GetContext.PixelShader.SetShaderResources(0, 1,
                                                                      GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.AlbedoMapAsset));
                            GetContext.PixelShader.SetSamplers(0, 1,
                                                               GetSharedItems.GetSamplerState(SamplerType.BilinearClamp));
                            IsOpaquePass = false;
                        }
                    }
                    else
                    {
                        GetContext.PixelShader.SetShaderResources(0, 1,
                                                                  GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.AlbedoMapAsset));
                    }
                    if (CurrentMaterialInstance.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       = CurrentMaterialInstance.PropetyBlock.Tile;
                m_PerObjectConstBuffer.textureShift        = CurrentMaterialInstance.PropetyBlock.Shift;
                GetContext.UpdateSubresource(ref m_PerObjectConstBuffer, PerObjConstantBuffer);

                RenderBackend.DrawIndexedWrapper(m_CachedMesh.IndexCount, 0, 0);
            }
        }
Exemplo n.º 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);
            }
        }