public RenderTarget Render(Stage stage, Camera camera) { if (!HandlesInitialized) { InitializeHandles(); HandlesInitialized = true; } RenderedLights = 0; // Init common matrices Matrix4 view, projection; camera.GetViewMatrix(out view); camera.GetProjectionMatrix(out projection); // Render scene to GBuffer var clearColor = stage.ClearColor; clearColor.W = 0; Backend.ProfileBeginSection(Profiler.GBuffer); Backend.BeginPass(GBuffer, clearColor, true); RenderScene(stage, camera, ref view, ref projection); Backend.EndPass(); Backend.ProfileEndSection(Profiler.GBuffer); // Render light accumulation Backend.ProfileBeginSection(Profiler.Lighting); Backend.BeginPass(LightAccumulation, new Vector4(0.0f, 0.0f, 0.0f, 1.0f), true); RenderAmbientLight(stage); RenderLights(camera, ref view, ref projection, stage.GetLights(), stage); Backend.EndPass(); Backend.ProfileEndSection(Profiler.Lighting); var currentRenderTarget = Temporary; var currentSource = LightAccumulation; if (FogSettings.Enable) { Backend.BeginPass(currentRenderTarget, new Vector4(0.0f, 0.0f, 0.0f, 1.0f), false); Backend.BeginInstance(FogShader.Handle, new int[] { currentSource.Textures[0].Handle, GBuffer.Textures[2].Handle }, new int[] { Backend.DefaultSamplerNoFiltering, Backend.DefaultSamplerNoFiltering }, LightAccumulatinRenderState); Backend.BindShaderVariable(FogParams.SamplerScene, 0); Backend.BindShaderVariable(FogParams.SamplerGBuffer2, 1); Backend.BindShaderVariable(FogParams.FogStart, FogSettings.Start); Backend.BindShaderVariable(FogParams.FogEnd, FogSettings.End); Backend.BindShaderVariable(FogParams.FogColor, ref FogSettings.Color); Vector2 screenSize = new Vector2(LightAccumulation.Width, LightAccumulation.Height); Backend.BindShaderVariable(FogParams.ScreenSize, ref screenSize); Backend.DrawMesh(QuadMesh.MeshHandle); Backend.EndPass(); var tmp = currentRenderTarget; currentRenderTarget = currentSource; currentSource = tmp; } Backend.BeginPass(Output, new Vector4(0.0f, 0.0f, 0.0f, 1.0f), false); Backend.BeginInstance(CombineShader.Handle, new int[] { currentSource.Textures[0].Handle }, new int[] { Backend.DefaultSamplerNoFiltering }, LightAccumulatinRenderState); Backend.BindShaderVariable(CombineParams.SamplerLight, 0); Backend.DrawMesh(QuadMesh.MeshHandle); Backend.EndPass(); return Output; }