public DirectionalLightRenderer(DX11Game game, GBuffer gBuffer)
        {
            this.game    = game;
            this.gBuffer = gBuffer;
            var device = game.Device;

            context = device.ImmediateContext;

            shadowsShader = BasicShader.LoadAutoreload(game,
                                                       new System.IO.FileInfo(
                                                           CompiledShaderCache.Current.RootShaderPath + "Deferred\\DirectionalLight.fx"));

            shadowsShader.SetTechnique("Technique0");

            noShadowsShader = BasicShader.LoadAutoreload(game,
                                                         new System.IO.FileInfo(
                                                             CompiledShaderCache.Current.RootShaderPath + "Deferred\\DirectionalLight.fx"), null, new[] { new ShaderMacro("DISABLE_SHADOWS") });

            noShadowsShader.SetTechnique("Technique0");

            quad = new FullScreenQuad(device);

            layout = FullScreenQuad.CreateInputLayout(device, shadowsShader.GetCurrentPass(0));

            LightDirection = Vector3.Normalize(new Vector3(1, 2, 1));
            Color          = new Vector3(1, 1, 0.9f);

            CSMRenderer = new CSM.CSMRenderer(game);
        }
Exemple #2
0
        public void TestBasicShaderIncludeRoot()
        {
            BasicShader    shader = null;
            FullScreenQuad quad   = null;
            var            game   = new DX11Game();

            game.InitDirectX();

            var fi      = new FileInfo("../../Common.Core/Shaders/TestAutoReload.fx");
            var include = new FileInfo("../../Common.Core/Shaders/IncludeTest.fx");


            using (var fs = new StreamWriter(fi.OpenWrite()))
            {
                fs.WriteLine("float4 Color = float4(1,0,0,1);");
            }
            using (var fs = new StreamWriter(include.OpenWrite()))
            {
                fs.WriteLine("float4 Color2 = float4(0,0,0,1);");
            }

            shader = BasicShader.LoadAutoreload(game, fi);
            shader.SetTechnique("TestAutoReload");
            quad = new FullScreenQuad(game.Device);



            var inputLayout = FullScreenQuad.CreateInputLayout(game.Device, shader.GetCurrentPass(0));
            var time        = 0f;

            game.GameLoopEvent += delegate
            {
                shader.Apply();
                quad.Draw(inputLayout);

                if (time > 2 && time < 3)
                {
                    using (var fs = new StreamWriter(fi.OpenWrite()))
                    {
                        fs.WriteLine("float4 Color = float4(1,1,0,1);");
                        time = 5;
                    }
                }


                if (time > 6 && time < 7)
                {
                    using (var fs = new StreamWriter(include.OpenWrite()))
                    {
                        fs.WriteLine("float4 Color2 = float4(-1,0,0,1);");
                        time = 10;
                    }
                }

                time += game.Elapsed;
            };
            game.Run();
        }
Exemple #3
0
        /// <summary>
        /// Creates the renderer
        /// </summary>
        public CSMRenderer(DX11Game game)
        {
            this.game = game;
            var device = game.Device;

            context = device.ImmediateContext;

            // Load the effect we need
            shadowMapShader = BasicShader.LoadAutoreload(game, new FileInfo(CompiledShaderCache.Current.RootShaderPath + @"CSM\CSM.fx"));

            // Create the shadow map, using a 32-bit floating-point surface format
            shadowMap = new Texture2D(device, new Texture2DDescription
            {
                Width             = ShadowMapSize * NumSplits,
                Height            = ShadowMapSize,
                MipLevels         = 1,
                ArraySize         = 1,
                BindFlags         = BindFlags.DepthStencil | BindFlags.ShaderResource,
                Format            = global::SlimDX.DXGI.Format.R32_Typeless,
                SampleDescription = new global::SlimDX.DXGI.SampleDescription(1, 0)
            });

            shadowMapDsv = new DepthStencilView(device, shadowMap, new DepthStencilViewDescription
            {
                ArraySize = 1,
                Dimension = DepthStencilViewDimension.Texture2D,
                Flags     = DepthStencilViewFlags.None,
                Format    = global::SlimDX.DXGI.Format.D32_Float,
                MipSlice  = 0
            });
            ShadowMapRV = new ShaderResourceView(device, shadowMap, new ShaderResourceViewDescription
            {
                ArraySize       = 1,
                Dimension       = ShaderResourceViewDimension.Texture2D,
                MipLevels       = 1,
                MostDetailedMip = 0,
                Format          = global::SlimDX.DXGI.Format.R32_Float
            });


            // Create the full-screen quad
            fullScreenQuad = new FullScreenQuad(device);



            // We'll keep an array of EffectTechniques that will let us map a
            // ShadowFilteringType to a technique for calculating shadow occlusion
            shadowOcclusionTechniques[0] = "CreateShadowTerm2x2PCF";
            shadowOcclusionTechniques[1] = "CreateShadowTerm3x3PCF";
            shadowOcclusionTechniques[2] = "CreateShadowTerm5x5PCF";
            shadowOcclusionTechniques[3] = "CreateShadowTerm7x7PCF";

            shadowMapShader.SetTechnique(shadowOcclusionTechniques[0]);

            layout = FullScreenQuad.CreateInputLayout(device, shadowMapShader.GetCurrentPass(0));
        }
Exemple #4
0
        public CombineFinalRenderer(DX11Game game, GBuffer gBuffer)
        {
            this.game    = game;
            this.gBuffer = gBuffer;
            var device = game.Device;

            context = device.ImmediateContext;

            shader = BasicShader.LoadAutoreload(game,
                                                new System.IO.FileInfo(
                                                    CompiledShaderCache.Current.RootShaderPath + "Deferred\\CombineFinal.fx"));

            shader.SetTechnique("Technique0");

            quad = new FullScreenQuad(device);

            layout = FullScreenQuad.CreateInputLayout(device, shader.GetCurrentPass(0));

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

            lightAccumulationMap = new Texture2D(device, desc);

            lightAccumulationRTV = new RenderTargetView(device, lightAccumulationMap);
            LightAccumulationRV  = new ShaderResourceView(device, lightAccumulationMap);

            var bsDesc = new BlendStateDescription();
            var b      = new RenderTargetBlendDescription();

            b.BlendEnable           = true;
            b.BlendOperation        = BlendOperation.Add;
            b.BlendOperationAlpha   = BlendOperation.Add;
            b.DestinationBlend      = BlendOption.One;
            b.DestinationBlendAlpha = BlendOption.One;
            b.SourceBlend           = BlendOption.One;
            b.SourceBlendAlpha      = BlendOption.One;
            b.RenderTargetWriteMask = ColorWriteMaskFlags.All;
            bsDesc.RenderTargets[0] = b;


            blendState = BlendState.FromDescription(device, bsDesc);
        }
        public FogEffect(DX11Game game)
        {
            this.game = game;
            var device = game.Device;

            context = device.ImmediateContext;

            shader = BasicShader.LoadAutoreload(game,
                                                new System.IO.FileInfo(
                                                    CompiledShaderCache.Current.RootShaderPath + "Deferred\\Fog.fx"));

            shader.SetTechnique("Technique0");

            quad = new FullScreenQuad(device);

            layout = FullScreenQuad.CreateInputLayout(device, shader.GetCurrentPass(0));
        }
Exemple #6
0
        public void TestFullscreenQuad()
        {
            BasicShader    shader = null;
            FullScreenQuad quad   = null;
            var            game   = new DX11Game();

            game.InitDirectX();

            var device = game.Device;

            var bytecode  = ShaderBytecode.CompileFromFile("../../Common.Core/Shaders/FullScreenQuad.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None);
            var effect    = new Effect(game.Device, bytecode);
            var technique = effect.GetTechniqueByName("TestQuadTextured");
            var pass      = technique.GetPassByIndex(0);

            quad = new FullScreenQuad(game.Device);

            var diffuseShaderVariable = effect.GetVariableByName("txDiffuse").AsResource();

            var texturePath = Wallpaper001_png;

            var diffuseTexture = Texture2D.FromFile(device, texturePath);

            var diffuseTextureRv = new ShaderResourceView(device, diffuseTexture);

            diffuseShaderVariable.SetResource(diffuseTextureRv);

            var inputLayout = FullScreenQuad.CreateInputLayout(device, pass);

            game.GameLoopEvent += delegate
            {
                pass.Apply(game.Device.ImmediateContext);

                quad.Draw(inputLayout);
            };
            game.Run();
        }
        public ToneMapRenderer(DX11Game game)
        {
            this.game = game;
            var device = game.Device;
            context = device.ImmediateContext;

            shader = BasicShader.LoadAutoreload(game,
                                                new System.IO.FileInfo(
                                                    CompiledShaderCache.Current.RootShaderPath + "Deferred\\ToneMap.fx"));

            shader.SetTechnique("Technique0");

            quad = new FullScreenQuad(device);

            layout = FullScreenQuad.CreateInputLayout(device, shader.GetCurrentPass(0));


            //Create test average rt

            var descMini = new Texture2DDescription
            {
                BindFlags =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format = Format.R32_Float,
                Width = 1,
                Height = 1,
                ArraySize = 1,
                SampleDescription = new SampleDescription(1, 0),
                MipLevels = 1
            };

            var averageLum = new Texture2D(device, descMini);

            averageLumRTV = new RenderTargetView(device, averageLum);
            averageLumRV = new ShaderResourceView(device, averageLum);

        }
Exemple #8
0
        public PointLightRenderer(DX11Game game, GBuffer gBuffer)
        {
            this.game    = game;
            this.gBuffer = gBuffer;
            var device = game.Device;

            context = device.ImmediateContext;

            reloadShader(game);

            quad = new FullScreenQuad(device);

            layout = FullScreenQuad.CreateInputLayout(device, noShadowsShader.GetCurrentPass(0));

            LightPosition  = new Vector3(0, 6, 0);
            LightRadius    = 6;
            LightIntensity = 1;


            Color = new Vector3(1, 1, 0.9f);

            //var rasterizerInside = RasterizerState.FromDescription(device, new RasterizerStateDescription
            //                                                                   {
            //                                                                       CullMode = CullMode.Front
            //                                                                   });

            //var rasterizerOutside = RasterizerState.FromDescription(device, new RasterizerStateDescription
            //{
            //    CullMode = CullMode.Back
            //});

            var shadowCubeMap = new Texture2D(device, new Texture2DDescription
            {
                ArraySize = 6,
                BindFlags =
                    BindFlags.DepthStencil | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = global::SlimDX.DXGI.Format.R32_Typeless,
                Height            = shadowMapSize,
                Width             = shadowMapSize,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.TextureCube,
                SampleDescription =
                    new global::SlimDX.DXGI.SampleDescription(1, 0),
                Usage = ResourceUsage.Default
            });

            depthStencilViewFaces = new DepthStencilView[6];
            shadowCubeMapRVs      = new ShaderResourceView[6];
            LightCameras          = new CustomCamera[6];

            for (int i = 0; i < 6; i++)
            {
                depthStencilViewFaces[i] = new DepthStencilView(device, shadowCubeMap, new DepthStencilViewDescription
                {
                    Dimension =
                        DepthStencilViewDimension
                        .Texture2DArray,
                    ArraySize       = 1,
                    FirstArraySlice = i,
                    Flags           =
                        DepthStencilViewFlags
                        .None,
                    Format =
                        global::SlimDX.DXGI.Format.
                        D32_Float,
                    MipSlice = 0
                });

                //ShadowCubeMapRVs[i] = new ShaderResourceView(device, shadowCubeMap, new ShaderResourceViewDescription
                //{
                //    Dimension = ShaderResourceViewDimension.Texture2D,
                //    ArraySize = 1,
                //    FirstArraySlice = i,
                //    Format = SlimDX.DXGI.Format.R32_Float,
                //    MipLevels = 1,
                //    MostDetailedMip = 0
                //});
                LightCameras[i] = new CustomCamera();
            }

            ShadowCubeMapRv = new ShaderResourceView(device, shadowCubeMap, new ShaderResourceViewDescription
            {
                Dimension       = ShaderResourceViewDimension.TextureCube,
                MostDetailedMip = 0,
                MipLevels       = 1,
                Format          = global::SlimDX.DXGI.Format.R32_Float
            });
        }
        public AverageLuminanceCalculater(DX11Game game, ShaderResourceView hdrImage)
        {
            this.game     = game;
            this.hdrImage = hdrImage;
            var device = game.Device;

            context = device.ImmediateContext;

            shader = BasicShader.LoadAutoreload(game,
                                                new System.IO.FileInfo(
                                                    CompiledShaderCache.Current.RootShaderPath + "Deferred\\Luminance.fx"));

            shader.SetTechnique("CalculateLuminance");

            quad = new FullScreenQuad(device);

            layout = FullScreenQuad.CreateInputLayout(device, shader.GetCurrentPass(0));



            var desc = new Texture2DDescription
            {
                BindFlags =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R32_Float,
                Width             = hdrImage.Resource.AsSurface().Description.Width,
                Height            = hdrImage.Resource.AsSurface().Description.Height,
                ArraySize         = 1,
                SampleDescription = new SampleDescription(1, 0),
                MipLevels         = 0,
                OptionFlags       = ResourceOptionFlags.GenerateMipMaps
            };

            luminanceMap = new Texture2D(device, desc);

            luminanceRTV       = new RenderTargetView(device, luminanceMap);
            LuminanceRV        = new ShaderResourceView(device, luminanceMap);
            AverageLuminanceRV = new ShaderResourceView(device, luminanceMap, new ShaderResourceViewDescription
            {
                Dimension       = ShaderResourceViewDimension.Texture2D,
                Flags           = ShaderResourceViewExtendedBufferFlags.None,
                MipLevels       = 1,
                MostDetailedMip = luminanceMap.Description.MipLevels - 1,
                Format          = Format.R32_Float
            });



            var descMini = new Texture2DDescription
            {
                BindFlags =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R32_Float,
                Width             = 1,
                Height            = 1,
                ArraySize         = 1,
                SampleDescription = new SampleDescription(1, 0),
                MipLevels         = 1,
            };

            averageLum1 = new Texture2D(device, descMini);
            averageLum2 = new Texture2D(device, descMini);

            prevAverageLumRTV = new RenderTargetView(device, averageLum1);
            prevAverageLumRV  = new ShaderResourceView(device, averageLum1);
            currAverageLumRTV = new RenderTargetView(device, averageLum2);
            CurrAverageLumRV  = new ShaderResourceView(device, averageLum2);
        }
Exemple #10
0
        public void Initialize()
        {
            //quad = new FullScreenQuad(game.GraphicsDevice);
            quad   = new FullScreenQuad(game.Device);
            shader = BasicShader.LoadAutoreload(game,
                                                new System.IO.FileInfo(
                                                    CompiledShaderCache.Current.RootShaderPath + "Particles\\BasicParticleAnimation.fx"), null, new[] { new ShaderMacro("CALCULATE_ACCELERATION", simulation) });
            shader.SetTechnique("particleSimulation");
            // shader.AddCustomIncludeHandler("generated.fx",generateIncludeCallback);
            //shader.InitFromEmbeddedFile(game, Assembly.GetExecutingAssembly(), "MHGameWork.TheWizards.Particles.Files.BasicParticleAnimation.fx", "..\\..\\NewModules\\Particles\\Files\\BasicParticleAnimation.fx", new EffectPool());

            layout = FullScreenQuad.CreateInputLayout(game.Device, shader.GetCurrentPass(0));

            positionTex = new Texture2D(game.Device, new Texture2DDescription()
            {
                ArraySize      = 1,
                CpuAccessFlags = CpuAccessFlags.None,
                BindFlags      =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R16G16B16A16_Float,
                Height            = size,
                Width             = size,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription =
                    new SlimDX.DXGI.SampleDescription(1, 0),
                Usage = ResourceUsage.Default //TODO: render the new position to the texture
            });
            position2Tex = new Texture2D(game.Device, new Texture2DDescription()
            {
                ArraySize      = 1,
                CpuAccessFlags = CpuAccessFlags.None,
                BindFlags      =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R16G16B16A16_Float,
                Height            = size,
                Width             = size,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription =
                    new SlimDX.DXGI.SampleDescription(1, 0),
                Usage = ResourceUsage.Default
            });
            velocityTex = new Texture2D(game.Device, new Texture2DDescription()
            {
                ArraySize      = 1,
                CpuAccessFlags = CpuAccessFlags.None,
                BindFlags      =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R16G16B16A16_Float,
                Height            = size,
                Width             = size,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription =
                    new SlimDX.DXGI.SampleDescription(1, 0),
                Usage = ResourceUsage.Default
            });
            velocity2Tex = new Texture2D(game.Device, new Texture2DDescription()
            {
                ArraySize      = 1,
                CpuAccessFlags = CpuAccessFlags.None,
                BindFlags      =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R16G16B16A16_Float,
                Height            = size,
                Width             = size,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription =
                    new SlimDX.DXGI.SampleDescription(1, 0),
                Usage = ResourceUsage.Default
            });
            positionRv  = new ShaderResourceView(game.Device, positionTex);
            position2Rv = new ShaderResourceView(game.Device, position2Tex);
            velocityRv  = new ShaderResourceView(game.Device, velocityTex);
            velocity2Rv = new ShaderResourceView(game.Device, velocity2Tex);

            positionTarget  = new RenderTargetView(game.Device, positionTex);
            positionTarget2 = new RenderTargetView(game.Device, position2Tex);
            velocityTarget  = new RenderTargetView(game.Device, velocityTex);
            velocityTarget2 = new RenderTargetView(game.Device, velocity2Tex);
        }
Exemple #11
0
        public SpotLightRenderer(DX11Game game, GBuffer gBuffer)
        {
            this.game    = game;
            this.gBuffer = gBuffer;
            var device = game.Device;

            context = device.ImmediateContext;

            reloadShader(game);

            quad = new FullScreenQuad(device);

            layout = FullScreenQuad.CreateInputLayout(device, noShadowsShader.GetCurrentPass(0));

            LightPosition     = new Vector3(0, 6, 0);
            LightRadius       = 6;
            LightIntensity    = 1;
            SpotDirection     = MathHelper.Down;
            SpotLightAngle    = MathHelper.ToRadians(30);
            SpotDecayExponent = 1;


            Color = new Vector3(1, 1, 0.9f);



            shadowMap = new Texture2D(device, new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.DepthStencil | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = global::SlimDX.DXGI.Format.R32_Typeless,
                Width             = shadowMapSize,
                Height            = shadowMapSize,
                MipLevels         = 1,
                Usage             = ResourceUsage.Default,
                SampleDescription = new global::SlimDX.DXGI.SampleDescription(1, 0)
            });
            shadowMapRV = new ShaderResourceView(device, shadowMap, new ShaderResourceViewDescription
            {
                Dimension       = ShaderResourceViewDimension.Texture2D,
                Format          = global::SlimDX.DXGI.Format.R32_Float,
                MipLevels       = 1,
                MostDetailedMip = 0
            });
            shadowMapDSV = new DepthStencilView(device, shadowMap, new DepthStencilViewDescription
            {
                Dimension = DepthStencilViewDimension.Texture2D,
                Format    = global::SlimDX.DXGI.Format.D32_Float
            });

            LightCamera = new CustomCamera();


            //var rasterizerInside = RasterizerState.FromDescription(device, new RasterizerStateDescription
            //                                                                   {
            //                                                                       CullMode = CullMode.Front
            //                                                                   });

            //var rasterizerOutside = RasterizerState.FromDescription(device, new RasterizerStateDescription
            //{
            //    CullMode = CullMode.Back
            //});
        }