Ejemplo n.º 1
0
        private static void PreparePointLights()
        {
            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);
                }
            }

            var activePointlights = 0;

            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);
            mapping.WriteAndPosition(m_pointlightsCullBuffer, 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.ComputeShader.SetUav(0, m_tileIndices);
            RC.ComputeShader.SetSrvs(0, MyGBuffer.Main);
            RC.ComputeShader.SetSrv(MyCommon.POINTLIGHT_SLOT, m_pointlightCullHwBuffer);
            RC.ComputeShader.Set(m_preparePointLights);
            Vector2I tiles = new Vector2I(m_tilesX, m_tilesY);

            if (MyStereoRender.Enable && MyStereoRender.RenderRegion != MyStereoRegion.FULLSCREEN)
            {
                tiles.X /= 2;
            }

            RC.Dispatch(tiles.X, tiles.Y, 1);
            RC.ComputeShader.Set(null);
            RC.ComputeShader.SetUav(0, null);
            RC.ComputeShader.ResetSrvs(0, MyGBufferSrvFilter.ALL);
        }
Ejemplo n.º 2
0
        internal static void Render(ISrvTexture postProcessedShadows, IRtvTexture ambientOcclusion)
        {
            ProfilerShort.Begin("PreparePointLights");
            MyGpuProfiler.IC_BeginBlock("Map lights to tiles");
            if (MyRender11.DebugOverrides.PointLights)
            {
                PreparePointLights();
            }
            MyGpuProfiler.IC_BeginNextBlock("Apply point lights");
            ProfilerShort.End();

            ProfilerShort.Begin("RenderPointlightsTiled");
            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.SetSrv(MyCommon.MATERIAL_BUFFER_SLOT, MySceneMaterials.m_buffer);
            RC.SetBlendState(MyBlendStateManager.BlendAdditive);
            RC.SetDepthStencilState(!MyStereoRender.Enable ? MyDepthStencilStateManager.IgnoreDepthStencil : MyDepthStencilStateManager.StereoIgnoreDepthStencil);
            RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers);

            if (MyRender11.DebugOverrides.PointLights)
            {
                RenderPointlightsTiled(ambientOcclusion);
            }
            ProfilerShort.End();

            ProfilerShort.Begin("RenderSpotlights");
            MyGpuProfiler.IC_BeginNextBlock("Apply spotlights");
            if (MyRender11.DebugOverrides.SpotLights)
            {
                RenderSpotlights();
            }
            ProfilerShort.End();

            ProfilerShort.Begin("RenderDirectionalEnvironmentLight");
            MyGpuProfiler.IC_BeginNextBlock("Apply directional light");
            if (MyRender11.DebugOverrides.EnvLight)
            {
                RenderDirectionalEnvironmentLight(postProcessedShadows, ambientOcclusion);
            }
            MyGpuProfiler.IC_EndBlock();
            ProfilerShort.End();

            // 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);
        }