private void updateTonemapLuminance(AverageLuminanceCalculater calculator)
 {
     calculator.DrawUpdatedLogLuminance();
     calculator.DrawUpdatedAdaptedLogLuminance();
 }
        public void TestFog()
        {
            var game = new DX11Game();

            game.InitDirectX();
            var device  = game.Device;
            var context = device.ImmediateContext;



            var desc = new Texture2DDescription
            {
                BindFlags =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R16G16B16A16_Float,
                Width             = 800,
                Height            = 600,
                ArraySize         = 1,
                SampleDescription = new SampleDescription(1, 0),
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.GenerateMipMaps
            };
            var hdrImage = new Texture2D(device, desc);

            var hdrImageRTV = new RenderTargetView(device, hdrImage);
            var hdrImageRV  = new ShaderResourceView(device, hdrImage);


            var desc2 = new Texture2DDescription
            {
                BindFlags =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R8G8B8A8_UNorm,
                Width             = 800,
                Height            = 600,
                ArraySize         = 1,
                SampleDescription = new SampleDescription(1, 0),
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.GenerateMipMaps
            };

            var toneMappedImage = new Texture2D(device, desc2);

            var toneMappedImageRTV = new RenderTargetView(device, toneMappedImage);
            var toneMappedImageRV  = new ShaderResourceView(device, toneMappedImage);

            var foggedImage = new Texture2D(device, desc2);

            var foggedImageRTV = new RenderTargetView(device, foggedImage);
            var foggedImageRV  = new ShaderResourceView(device, foggedImage);


            var calculater = new AverageLuminanceCalculater(game, hdrImageRV);

            var toneMap = new ToneMapRenderer(game);

            var combineFinal = new TestCombineFinalClass(game);

            var fogRenderer = new FogEffect(game);

            game.GameLoopEvent += delegate
            {
                combineFinal.DrawUpdatedDeferredRendering();
                if (game.Keyboard.IsKeyDown(Key.I))
                {
                    return;
                }
                context.Rasterizer.SetViewports(new Viewport(0, 0, 800, 600));

                context.OutputMerger.SetTargets(hdrImageRTV);

                combineFinal.DrawCombined();



                calculater.DrawUpdatedLogLuminance();
                calculater.DrawUpdatedAdaptedLogLuminance();

                context.ClearState();
                game.SetBackbuffer();
                context.OutputMerger.SetTargets(toneMappedImageRTV);



                if (game.Keyboard.IsKeyDown(Key.K))
                {
                    game.TextureRenderer.Draw(calculater.AverageLuminanceRV, new Vector2(10, 10),
                                              new Vector2(300, 300));
                    game.TextureRenderer.Draw(hdrImageRV, new Vector2(320, 10),
                                              new Vector2(300, 300));
                    game.TextureRenderer.Draw(calculater.CurrAverageLumRV, new Vector2(10, 320),
                                              new Vector2(270, 270));
                }
                else
                {
                    toneMap.DrawTonemapped(hdrImageRV, calculater.CurrAverageLumRV);
                }


                context.ClearState();
                game.SetBackbuffer();
                fogRenderer.PostProcessFog(toneMappedImageRV, combineFinal.FilledGBuffer.GBuffer, foggedImageRTV);
                context.ClearState();
                game.SetBackbuffer();
                //context.ClearRenderTargetView(foggedImageRTV, new Color4(1, 1, 0));
                game.TextureRenderer.Draw(foggedImageRV, new Vector2(0, 0), new Vector2(800, 600));
            };

            game.Run();
        }
        public DeferredRenderer(DX11Game game)
        {
            this.game = game;
            var device = game.Device;

            context = device.ImmediateContext;

            screenWidth  = game.Form.Form.ClientSize.Width;
            screenHeight = game.Form.Form.ClientSize.Height;
            int width  = screenWidth;
            int height = screenHeight;

            gBuffer     = new GBuffer(game.Device, width, height);
            texturePool = new TexturePool(game);

            meshesRenderer = new DeferredMeshesRenderer(game, gBuffer, TexturePool);

            directionalLightRenderer = new DirectionalLightRenderer(game, GBuffer);
            spotLightRenderer        = new SpotLightRenderer(game, GBuffer);
            pointLightRenderer       = new PointLightRenderer(game, GBuffer);

            combineFinalRenderer = new CombineFinalRenderer(game, GBuffer);

            var desc = new Texture2DDescription
            {
                BindFlags =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R16G16B16A16_Float,
                Width             = screenWidth,
                Height            = screenHeight,
                ArraySize         = 1,
                SampleDescription = new SampleDescription(1, 0),
                MipLevels         = 1
            };

            hdrImage = new Texture2D(device, desc);

            hdrImageRtv = new RenderTargetView(device, hdrImage);
            hdrImageRV  = new ShaderResourceView(device, hdrImage);

            calculater = new AverageLuminanceCalculater(game, hdrImageRV);

            toneMap = new ToneMapRenderer(game);


            var tempDesc = new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.None,
                CpuAccessFlags    = CpuAccessFlags.Read,
                Format            = Format.R32_Float,
                Height            = 1,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Staging,
                Width             = 1
            };

            tempTex = new Texture2D(device, tempDesc);



            ssao = new HorizonSSAORenderer(game, screenWidth, screenHeight);



            Vector3 radius = new Vector3(500, 1000, 500);

            frustumCuller = new FrustumCuller(new BoundingBox(-radius, radius), 1);

            gbufferView           = frustumCuller.CreateView();
            meshesRenderer.Culler = frustumCuller;

            Texture2D skyColorTexture;// = Texture2D.FromFile(game.Device, TWDir.GameData.CreateSubdirectory("Core") + "\\skyColor.bmp");

            var strm = new DataStream(16 * 4, true, true);

            var multiplier = 2;

            strm.Write(new Half4(new Half(135f / 255f * multiplier), new Half(206f / 255f * multiplier), new Half(235 / 255f * multiplier), new Half(1)));
            strm.Position = 0;
            var dataRectangle = new DataRectangle(16 * 4, strm);

            skyColorTexture = new Texture2D(game.Device, new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = Format.R16G16B16A16_Float,
                Height            = 1,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                Width             = 1
            }, dataRectangle);

            skyColorRV = new ShaderResourceView(game.Device, skyColorTexture);


            postProcessRT1 = CreateBackbufferLikeRT();
            postProcessRT2 = CreateBackbufferLikeRT();

            fogRenderer = new FogEffect(game);

            backgroundDepthStencilState = DepthStencilState.FromDescription(game.Device, new DepthStencilStateDescription()
            {
                IsDepthEnabled  = true,
                DepthComparison = Comparison.LessEqual,
                DepthWriteMask  = DepthWriteMask.Zero,
            });

            lineManager = new LineManager3D(game.Device);
            updateRasterizerState();
        }
        public void TestAutoAdjustTonemap()
        {
            var game = new DX11Game();

            game.InitDirectX();
            var device  = game.Device;
            var context = device.ImmediateContext;



            var hdrImage1 = Texture2D.FromFile(device, HdrImageDDS);

            var hdrImage1RV = new ShaderResourceView(device, hdrImage1);



            var desc = new Texture2DDescription
            {
                BindFlags =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R16G16B16A16_Float,
                Width             = 300,
                Height            = 300,
                ArraySize         = 1,
                SampleDescription = new SampleDescription(1, 0),
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.GenerateMipMaps
            };
            var hdrImage = new Texture2D(device, desc);

            var hdrImageRTV = new RenderTargetView(device, hdrImage);
            var hdrImageRV  = new ShaderResourceView(device, hdrImage);

            var calculater = new AverageLuminanceCalculater(game, hdrImageRV);

            var toneMap = new ToneMapRenderer(game);

            game.GameLoopEvent += delegate
            {
                context.Rasterizer.SetViewports(new Viewport(0, 0, 300, 300));

                context.OutputMerger.SetTargets(hdrImageRTV);

                if (game.Keyboard.IsKeyPressed(Key.D1))
                {
                    game.TextureRenderer.Draw(hdrImage1RV, new Vector2(), new Vector2(300, 300));
                }
                if (game.Keyboard.IsKeyPressed(Key.D2))
                {
                    context.ClearRenderTargetView(hdrImageRTV, new Color4(1, 1, 1, 1));
                }


                calculater.DrawUpdatedLogLuminance();
                calculater.DrawUpdatedAdaptedLogLuminance();

                context.ClearState();
                game.SetBackbuffer();


                //game.TextureRenderer.Draw(calculater.AverageLuminanceRV, new Vector2(10, 10),
                //                          new Vector2(300, 300));
                game.TextureRenderer.Draw(hdrImageRV, new Vector2(320, 10),
                                          new Vector2(300, 300));
                game.TextureRenderer.Draw(calculater.CurrAverageLumRV, new Vector2(10, 320),
                                          new Vector2(270, 270));

                context.Rasterizer.SetViewports(new Viewport(10, 10, 300, 300));

                toneMap.DrawTonemapped(hdrImageRV, calculater.CurrAverageLumRV);
            };

            game.Run();
        }