internal void GatherArray(RwTexId postprocessTarget, RwTexId cascadeArray, MyProjectionInfo[] cascadeInfo, ConstantsBufferId cascadeConstantBuffer) { if (!MyRenderProxy.Settings.EnableShadows || !MyRender11.DebugOverrides.Shadows) { return; } MarkCascadesInStencil(cascadeInfo); MyGpuProfiler.IC_BeginBlock("Cascades postprocess"); MyRenderContext renderContext = MyRenderContext.Immediate; DeviceContext deviceContext = renderContext.DeviceContext; MyShadowsQuality shadowsQuality = MyRender11.RenderSettings.ShadowQuality.GetShadowsQuality(); if (shadowsQuality == MyShadowsQuality.LOW) { renderContext.SetCS(m_gatherCS_LD); } else if (shadowsQuality == MyShadowsQuality.MEDIUM) { renderContext.SetCS(m_gatherCS_MD); } else if (shadowsQuality == MyShadowsQuality.HIGH) { renderContext.SetCS(m_gatherCS_HD); } ComputeShaderId.TmpUav[0] = postprocessTarget.Uav; deviceContext.ComputeShader.SetUnorderedAccessViews(0, ComputeShaderId.TmpUav, ComputeShaderId.TmpCount); deviceContext.ComputeShader.SetShaderResource(0, MyRender11.MultisamplingEnabled ? MyScreenDependants.m_resolvedDepth.m_SRV_depth : MyGBuffer.Main.DepthStencil.m_SRV_depth); deviceContext.ComputeShader.SetShaderResource(1, MyGBuffer.Main.DepthStencil.m_SRV_stencil); deviceContext.ComputeShader.SetSampler(MyCommon.SHADOW_SAMPLER_SLOT, SamplerStates.m_shadowmap); if (!MyStereoRender.Enable) { deviceContext.ComputeShader.SetConstantBuffer(0, MyCommon.FrameConstants); } else { MyStereoRender.CSBindRawCB_FrameConstants(renderContext); } deviceContext.ComputeShader.SetConstantBuffer(4, cascadeConstantBuffer); deviceContext.ComputeShader.SetShaderResource(MyCommon.CASCADES_SM_SLOT, cascadeArray.SRV); Vector2I threadGroups = GetThreadGroupCount(); deviceContext.Dispatch(threadGroups.X, threadGroups.Y, 1); ComputeShaderId.TmpUav[0] = null; renderContext.DeviceContext.ComputeShader.SetUnorderedAccessViews(0, ComputeShaderId.TmpUav, ComputeShaderId.TmpCount); deviceContext.ComputeShader.SetShaderResource(0, null); deviceContext.ComputeShader.SetShaderResource(1, null); if (shadowsQuality == MyShadowsQuality.HIGH && MyRender11.Settings.EnableShadowBlur) { MyBlur.Run(postprocessTarget.Rtv, MyRender11.CascadesHelper.Rtv, MyRender11.CascadesHelper.SRV, postprocessTarget.SRV, depthDiscardThreshold: 0.2f); } MyGpuProfiler.IC_EndBlock(); }
internal static void PreparePointLights() { var activePointlights = 0; MyLights.Update(); BoundingFrustumD viewFrustumClippedD = MyRender11.Environment.Matrices.ViewFrustumClippedD; if (MyStereoRender.Enable) { if (MyStereoRender.RenderRegion == MyStereoRegion.LEFT) { viewFrustumClippedD = MyStereoRender.EnvMatricesLeftEye.ViewFrustumClippedD; } else if (MyStereoRender.RenderRegion == MyStereoRegion.RIGHT) { viewFrustumClippedD = MyStereoRender.EnvMatricesRightEye.ViewFrustumClippedD; } } MyLights.PointlightsBvh.OverlapAllFrustum(ref viewFrustumClippedD, VisiblePointlights); bool visiblePointlights = VisiblePointlights.Count != 0; if (!visiblePointlights && !m_lastFrameVisiblePointlights) { return; } m_lastFrameVisiblePointlights = visiblePointlights; if (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS) { VisiblePointlights.Sort((x, y) => x.ViewerDistanceSquared.CompareTo(y.ViewerDistanceSquared)); while (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS) { VisiblePointlights.RemoveAtFast(VisiblePointlights.Count - 1); } } foreach (var light in VisiblePointlights) { MyLights.WritePointlightConstants(light, ref m_pointlightsCullBuffer[activePointlights]); activePointlights++; Debug.Assert(activePointlights <= MyRender11Constants.MAX_POINT_LIGHTS); } for (int lightIndex = activePointlights; lightIndex < MyRender11Constants.MAX_POINT_LIGHTS; ++lightIndex) { MyLights.WritePointlightConstants(LightId.NULL, ref m_pointlightsCullBuffer[lightIndex]); } var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(16)); mapping.WriteAndPosition(ref activePointlights); mapping.Unmap(); mapping = MyMapping.MapDiscard(m_pointlightCullHwBuffer.Buffer); mapping.WriteAndPosition(m_pointlightsCullBuffer, 0, MyRender11Constants.MAX_POINT_LIGHTS); mapping.Unmap(); if (!MyStereoRender.Enable) { RC.ComputeShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); } else { MyStereoRender.CSBindRawCB_FrameConstants(RC); } RC.ComputeShader.SetConstantBuffer(1, MyCommon.GetObjectCB(16)); //RC.BindUAV(0, MyScreenDependants.m_test); RC.ComputeShader.SetRawUav(0, MyScreenDependants.m_tileIndices.m_uav); RC.ComputeShader.SetSrvs(0, MyGBuffer.Main); RC.ComputeShader.SetRawSrv(MyCommon.POINTLIGHT_SLOT, m_pointlightCullHwBuffer.Srv); RC.ComputeShader.Set(m_preparePointLights); Vector2I tiles = new Vector2I(MyScreenDependants.TilesX, MyScreenDependants.TilesY); if (MyStereoRender.Enable && MyStereoRender.RenderRegion != MyStereoRegion.FULLSCREEN) { tiles.X /= 2; } RC.Dispatch(tiles.X, tiles.Y, 1); RC.ComputeShader.Set(null); RC.ComputeShader.SetRawUav(0, null); RC.ComputeShader.ResetSrvs(0, MyGBufferSrvFilter.ALL); }
internal void GatherArray(IUavTexture postprocessTarget, ISrvBindable cascadeArray, MyProjectionInfo[] cascadeInfo, ConstantsBufferId cascadeConstantBuffer) { MyShadowsQuality shadowsQuality = MyRender11.RenderSettings.ShadowQuality.GetShadowsQuality(); if (!MyRender11.Settings.EnableShadows || !MyRender11.DebugOverrides.Shadows || shadowsQuality == MyShadowsQuality.DISABLED) { RC.ClearUav(postprocessTarget, new RawInt4()); return; } MarkCascadesInStencil(cascadeInfo); MyGpuProfiler.IC_BeginBlock("Cascades postprocess"); if (shadowsQuality == MyShadowsQuality.LOW) { RC.ComputeShader.Set(m_gatherCS_LD); } else if (shadowsQuality == MyShadowsQuality.MEDIUM) { RC.ComputeShader.Set(m_gatherCS_MD); } else if (shadowsQuality == MyShadowsQuality.HIGH) { RC.ComputeShader.Set(m_gatherCS_HD); } RC.ComputeShader.SetUav(0, postprocessTarget); RC.ComputeShader.SetSrv(0, MyRender11.MultisamplingEnabled ? MyScreenDependants.m_resolvedDepth.SrvDepth : MyGBuffer.Main.DepthStencil.SrvDepth); RC.ComputeShader.SetSrv(1, MyGBuffer.Main.DepthStencil.SrvStencil); RC.ComputeShader.SetSampler(MyCommon.SHADOW_SAMPLER_SLOT, MySamplerStateManager.Shadowmap); if (!MyStereoRender.Enable) { RC.ComputeShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); } else { MyStereoRender.CSBindRawCB_FrameConstants(RC); } //RC.ComputeShader.SetConstantBuffer(4, MyManagers.Shadows.GetCsmConstantBufferOldOne()); RC.ComputeShader.SetConstantBuffer(4, cascadeConstantBuffer); RC.ComputeShader.SetSrv(MyCommon.CASCADES_SM_SLOT, cascadeArray); //RC.ComputeShader.SetSrv(MyCommon.CASCADES_SM_SLOT, MyManagers.Shadow.GetCsmForGbuffer()); Vector2I threadGroups = GetThreadGroupCount(); RC.Dispatch(threadGroups.X, threadGroups.Y, 1); RC.ComputeShader.SetUav(0, null); RC.ComputeShader.SetSrv(0, null); RC.ComputeShader.SetSrv(1, null); if (shadowsQuality == MyShadowsQuality.HIGH && MyShadowCascades.Settings.Data.EnableShadowBlur) { IBorrowedUavTexture helper = MyManagers.RwTexturesPool.BorrowUav("MyShadowCascadesPostProcess.Helper", Format.R8_UNorm); MyBlur.Run(postprocessTarget, helper, postprocessTarget, depthStencilState: MyDepthStencilStateManager.IgnoreDepthStencil, depthDiscardThreshold: 0.2f, clearColor: Color4.White); helper.Release(); } MyGpuProfiler.IC_EndBlock(); }
internal static void Render(ISrvTexture postProcessedShadows) { MyLights.Update(); MyGpuProfiler.IC_BeginBlock("Map lights to tiles"); if (MyRender11.DebugOverrides.PointLights) { PreparePointLights(); } MyGpuProfiler.IC_BeginNextBlock("Apply point lights"); RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList); if (!MyStereoRender.Enable) { RC.ComputeShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); } else { MyStereoRender.CSBindRawCB_FrameConstants(RC); MyStereoRender.PSBindRawCB_FrameConstants(RC); } RC.PixelShader.SetSrvs(0, MyGBuffer.Main); RC.AllShaderStages.SetRawSrv(MyCommon.MATERIAL_BUFFER_SLOT, MySceneMaterials.m_buffer.Srv); RC.SetBlendState(MyBlendStateManager.BlendAdditive); if (!MyStereoRender.Enable) { RC.SetDepthStencilState(MyDepthStencilStateManager.IgnoreDepthStencil); } else { RC.SetDepthStencilState(MyDepthStencilStateManager.StereoIgnoreDepthStencil); } RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers); if (MyRender11.DebugOverrides.PointLights) { RenderPointlightsTiled(); } MyGpuProfiler.IC_BeginNextBlock("Apply spotlights"); if (MyRender11.DebugOverrides.SpotLights) { RenderSpotlights(); } MyGpuProfiler.IC_BeginNextBlock("Apply directional light"); if (MyRender11.DebugOverrides.EnvLight) { RenderDirectionalEnvironmentLight(postProcessedShadows); } MyGpuProfiler.IC_EndBlock(); // Because of BindGBufferForRead: RC.AllShaderStages.SetSrv(0, null); RC.AllShaderStages.SetSrv(1, null); RC.AllShaderStages.SetSrv(2, null); RC.AllShaderStages.SetSrv(3, null); RC.AllShaderStages.SetSrv(4, null); RC.SetBlendState(null); RC.SetRtv(null); }
internal static void Render() { MyLights.Update(); MyGpuProfiler.IC_BeginBlock("Map lights to tiles"); if (MyRender11.DebugOverrides.PointLights) { PreparePointLights(); } MyGpuProfiler.IC_EndBlock(); RC.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; if (!MyStereoRender.Enable) { RC.CSSetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); } else { MyStereoRender.CSBindRawCB_FrameConstants(RC); } RC.BindGBufferForRead(0, MyGBuffer.Main); RC.BindRawSRV(MyCommon.MATERIAL_BUFFER_SLOT, MySceneMaterials.m_buffer); RC.SetBS(MyRender11.BlendAdditive); if (!MyStereoRender.Enable) { RC.SetDS(MyDepthStencilState.IgnoreDepthStencil); } else { RC.SetDS(MyDepthStencilState.StereoIgnoreDepthStencil); } RC.DeviceContext.PixelShader.SetSamplers(0, SamplerStates.StandardSamplers); MyGpuProfiler.IC_BeginBlock("Apply point lights"); if (MyRender11.DebugOverrides.PointLights) { RenderPointlightsTiled(); } MyGpuProfiler.IC_EndBlock(); MyGpuProfiler.IC_BeginBlock("Apply spotlights"); if (MyRender11.DebugOverrides.SpotLights) { RenderSpotlights(); } MyGpuProfiler.IC_EndBlock(); MyGpuProfiler.IC_BeginBlock("Apply directional light"); if (MyRender11.DebugOverrides.EnvLight) { RenderDirectionalEnvironmentLight(); } MyGpuProfiler.IC_EndBlock(); // Because of BindGBufferForRead: RC.BindRawSRV(0, null); RC.BindRawSRV(1, null); RC.BindRawSRV(2, null); RC.BindRawSRV(3, null); RC.BindRawSRV(4, null); RC.CSBindRawSRV(0, null); RC.CSBindRawSRV(1, null); RC.CSBindRawSRV(2, null); RC.CSBindRawSRV(3, null); RC.CSBindRawSRV(4, null); RC.SetBS(null); }