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); } }
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 }
/// <summary> /// Draw indexed, non-instanced primitives. Wrapper for collect drawcalls statistics. /// </summary> /// <param name="indexCount">Number of indices to draw.</param> /// <param name="startIndexLocation">The location of the first index read by the GPU from the index buffer.</param> /// <param name="baseVertexLocation">A value added to each index before reading a vertex from the vertex buffer.</param> protected void DX_DrawIndexed(int indexCount, int startIndexLocation, int baseVertexLocation) { RenderBackend.DrawIndexedWrapper(indexCount, startIndexLocation, baseVertexLocation); }