Exemplo n.º 1
0
 public AntiAliasStage(GraphicsDevice device, FxaaEffect effect, float strength)
 {
     this.Strength           = strength;
     this.Device             = device;
     this.Effect             = effect;
     this.FullScreenTriangle = new FullScreenTriangle();
 }
Exemplo n.º 2
0
        //Initialize graphicsDevice
        public void Initialize(GraphicsDevice graphicsDevice, int width, int height, FullScreenTriangle fullScreenTriangle)
        {
            _fullScreenTriangle = fullScreenTriangle;

            _graphicsDevice = graphicsDevice;
            UpdateResolution(width, height);
        }
Exemplo n.º 3
0
 public DirectionalLightSystem(GraphicsDevice device, EffectFactory effectFactory, IComponentContainer <DirectionalLight> lights)
 {
     this.Device             = device;
     this.Effect             = effectFactory.Construct <DirectionalLightEffect>();
     this.Lights             = lights;
     this.FullScreenTriangle = new FullScreenTriangle();
 }
Exemplo n.º 4
0
        public void DrawSky(GraphicsDevice graphicsDevice, FullScreenTriangle quadRenderer)
        {
            graphicsDevice.DepthStencilState = DepthStencilState.None;
            graphicsDevice.RasterizerState   = RasterizerState.CullCounterClockwise;

            _passSky.Apply();
            quadRenderer.Draw(graphicsDevice);
        }
Exemplo n.º 5
0
        public SunlightSystem(GraphicsDevice device, EffectFactory effectFactory, IComponentContainer <Sunlight> Lights,
                              CascadedShadowMapFactory cascadedShadowMapFactory)
        {
            this.Device = device;
            this.Effect = effectFactory.Construct <SunlightEffect>();
            this.Lights = Lights;

            this.FullScreenTriangle = new FullScreenTriangle();
            this.Frustum            = new Frustum();
        }
Exemplo n.º 6
0
        public RenderTarget2D DrawGaussianBlur(RenderTarget2D renderTargetOutput, FullScreenTriangle triangle)
        {
            //select rendertarget
            RenderTarget2D renderTargetBlur = null;

            if (renderTargetOutput.Format != SurfaceFormat.Vector2)
            {
                throw new NotImplementedException("Unsupported Format for blurring");
            }

            //Only square expected
            int size = renderTargetOutput.Width;

            switch (size)
            {
            case 256:
                renderTargetBlur = _rt2562;
                break;

            case 512:
                renderTargetBlur = _rt5122;
                break;

            case 1024:
                renderTargetBlur = _rt10242;
                break;

            case 2048:
                renderTargetBlur = _rt20482;
                break;
            }

            if (renderTargetBlur == null)
            {
                throw new NotImplementedException("Unsupported Size for blurring");
            }

            _graphicsDevice.SetRenderTarget(renderTargetBlur);

            Vector2 invRes = new Vector2(1.0f / size, 1.0f / size);

            Shaders.GaussianBlurEffectParameter_InverseResolution.SetValue(invRes);
            Shaders.GaussianBlurEffectParameter_TargetMap.SetValue(renderTargetOutput);

            _horizontalPass.Apply();
            triangle.Draw(_graphicsDevice);

            _graphicsDevice.SetRenderTarget(renderTargetOutput);
            Shaders.GaussianBlurEffectParameter_TargetMap.SetValue(renderTargetBlur);
            _verticalPass.Apply();
            triangle.Draw(_graphicsDevice);

            return(renderTargetOutput);
        }
        public void Initialize(GraphicsDevice graphicsDevice, FullScreenTriangle fullScreenTriangle, Assets assets)
        {
            _graphicsDevice     = graphicsDevice;
            _fullScreenTriangle = fullScreenTriangle;
            _assets             = assets;

            _lightBlendState = new BlendState
            {
                AlphaSourceBlend      = Blend.One,
                ColorSourceBlend      = Blend.One,
                ColorDestinationBlend = Blend.One,
                AlphaDestinationBlend = Blend.One
            };
        }
Exemplo n.º 8
0
        public void DrawEnvironmentMap(GraphicsDevice graphicsDevice, Matrix view, FullScreenTriangle fullScreenTriangle, EnvironmentSample envSample, bool fireflyReduction, float ffThreshold)
        {
            FireflyReduction = fireflyReduction;
            FireflyThreshold = ffThreshold;

            SpecularStrength = envSample.SpecularStrength;
            DiffuseStrength  = envSample.DiffuseStrength;

            graphicsDevice.DepthStencilState = DepthStencilState.None;
            graphicsDevice.RasterizerState   = RasterizerState.CullCounterClockwise;
            _paramTransposeView.SetValue(Matrix.Transpose(view));

            _passBasic.Apply();
            fullScreenTriangle.Draw(graphicsDevice);
        }
Exemplo n.º 9
0
        public AveragedParticleSystem(
            GraphicsDevice device,
            EffectFactory effectFactory,
            IComponentContainer <AveragedEmitter> emitters)
        {
            this.Device = device;
            this.WeightedParticlesEffect = effectFactory.Construct <WeightedParticlesEffect>();
            this.AverageParticlesEffect  = effectFactory.Construct <AverageParticlesEffect>();
            this.Emitters = emitters;

            this.Particles = new List <ParticlePose>();

            this.FullScreenTriangle = new FullScreenTriangle();
            this.Quad = new UnitQuad(device);
        }
        public void Initialize(GraphicsDevice graphicsDevice, FullScreenTriangle fullScreenTriangle, Assets assets)
        {
            _graphicsDevice     = graphicsDevice;
            _fullScreenTriangle = fullScreenTriangle;
            _assets             = assets;

            _lightBlendState = new BlendState
            {
                AlphaSourceBlend      = Blend.One,
                ColorSourceBlend      = Blend.One,
                ColorDestinationBlend = Blend.One,
                AlphaDestinationBlend = Blend.One
            };

            _stencilCullPass1 = new DepthStencilState()
            {
                DepthBufferEnable                      = true,
                DepthBufferWriteEnable                 = false,
                DepthBufferFunction                    = CompareFunction.LessEqual,
                StencilFunction                        = CompareFunction.Always,
                StencilDepthBufferFail                 = StencilOperation.IncrementSaturation,
                StencilPass                            = StencilOperation.Keep,
                StencilFail                            = StencilOperation.Keep,
                CounterClockwiseStencilFunction        = CompareFunction.Always,
                CounterClockwiseStencilDepthBufferFail = StencilOperation.Keep,
                CounterClockwiseStencilPass            = StencilOperation.Keep,
                CounterClockwiseStencilFail            = StencilOperation.Keep,
                StencilMask                            = 0,
                ReferenceStencil                       = 0,
                StencilEnable                          = true,
            };

            _stencilCullPass2 = new DepthStencilState()
            {
                DepthBufferEnable               = false,
                DepthBufferWriteEnable          = false,
                DepthBufferFunction             = CompareFunction.GreaterEqual,
                CounterClockwiseStencilFunction = CompareFunction.Equal,
                StencilFunction             = CompareFunction.Equal,
                StencilFail                 = StencilOperation.Zero,
                StencilPass                 = StencilOperation.Zero,
                CounterClockwiseStencilFail = StencilOperation.Zero,
                CounterClockwiseStencilPass = StencilOperation.Zero,
                ReferenceStencil            = 0,
                StencilEnable               = true,
                StencilMask                 = 0,
            };
        }
        public void Draw(GraphicsDevice graphicsDevice, Camera camera, FullScreenTriangle fullScreenTriangle)
        {
            CameraPosition = camera.Position;

            //CheckUpdate
            CheckForShaderChanges();

            if (GameSettings.sdf_drawvolume)
            {
                _volumePass.Apply();
            }
            else
            {
                _distancePass.Apply();
            }
            fullScreenTriangle.Draw(graphicsDevice);
            //quadRenderer.RenderFullscreenQuad(graphicsDevice);
        }
Exemplo n.º 12
0
        public void Initialize(GraphicsDevice graphicsDevice)
        {
            _clearGBufferPass = _clearShader.Techniques["Clear"].Passes[0];

            _fullScreenTriangle = new FullScreenTriangle(graphicsDevice);

            _WorldView     = _gbufferShader.Parameters["WorldView"];
            _WorldViewProj = _gbufferShader.Parameters["WorldViewProj"];
            _WorldViewIT   = _gbufferShader.Parameters["WorldViewIT"];
            _Camera        = _gbufferShader.Parameters["Camera"];
            _FarClip       = _gbufferShader.Parameters["FarClip"];

            _Material_Metallic     = _gbufferShader.Parameters["Metallic"];
            _Material_MetallicMap  = _gbufferShader.Parameters["MetallicMap"];
            _Material_DiffuseColor = _gbufferShader.Parameters["DiffuseColor"];
            _Material_Roughness    = _gbufferShader.Parameters["Roughness"];

            _Material_MaskMap         = _gbufferShader.Parameters["Mask"];
            _Material_Texture         = _gbufferShader.Parameters["Texture"];
            _Material_NormalMap       = _gbufferShader.Parameters["NormalMap"];
            _Material_RoughnessMap    = _gbufferShader.Parameters["RoughnessMap"];
            _Material_DisplacementMap = _gbufferShader.Parameters["DisplacementMap"];

            _Material_MaterialType = _gbufferShader.Parameters["MaterialType"];

            //Techniques

            _DrawTextureDisplacement       = _gbufferShader.Techniques["DrawTextureDisplacement"];
            _DrawTextureSpecularNormalMask = _gbufferShader.Techniques["DrawTextureSpecularNormalMask"];
            _DrawTextureNormalMask         = _gbufferShader.Techniques["DrawTextureNormalMask"];
            _DrawTextureSpecularMask       = _gbufferShader.Techniques["DrawTextureSpecularMask"];
            _DrawTextureMask = _gbufferShader.Techniques["DrawTextureMask"];
            _DrawTextureSpecularNormalMetallic = _gbufferShader.Techniques["DrawTextureSpecularNormalMetallic"];
            _DrawTextureSpecularNormal         = _gbufferShader.Techniques["DrawTextureSpecularNormal"];
            _DrawTextureNormal           = _gbufferShader.Techniques["DrawTextureNormal"];
            _DrawTextureSpecular         = _gbufferShader.Techniques["DrawTextureSpecular"];
            _DrawTextureSpecularMetallic = _gbufferShader.Techniques["DrawTextureSpecularMetallic"];
            _DrawTexture = _gbufferShader.Techniques["DrawTexture"];
            _DrawNormal  = _gbufferShader.Techniques["DrawNormal"];
            _DrawBasic   = _gbufferShader.Techniques["DrawBasic"];
        }
Exemplo n.º 13
0
        public AmbientLightSystem(GraphicsDevice device, EffectFactory effectFactory, IComponentContainer <AmbientLight> lights)
        {
            this.Device             = device;
            this.Effect             = effectFactory.Construct <AmbientLightEffect>();
            this.BlurEffect         = effectFactory.Construct <BlurEffect>();
            this.Lights             = lights;
            this.FullScreenTriangle = new FullScreenTriangle();

            this.Kernel = this.GenerateKernel();

            var random = new Random(255);

            this.NoiseMap     = new Texture2D(device, 64, 64, false, SurfaceFormat.Color);
            SimplexNoise.Seed = random.Next();
            var noiseX = SimplexNoise.Calc2D(this.NoiseMap.Width, this.NoiseMap.Height, 1.0f);

            SimplexNoise.Seed = random.Next();
            var noiseY = SimplexNoise.Calc2D(this.NoiseMap.Width, this.NoiseMap.Height, 1.0f);

            SimplexNoise.Seed = random.Next();
            var noiseZ = SimplexNoise.Calc2D(this.NoiseMap.Width, this.NoiseMap.Height, 1.0f);

            var noise = new Color[this.NoiseMap.Width * this.NoiseMap.Height];

            for (var y = 0; y < this.NoiseMap.Height; y++)
            {
                for (var x = 0; x < this.NoiseMap.Width; x++)
                {
                    var r = (noiseX[x, y] / 128.0f) - 1.0f;
                    var g = (noiseY[x, y] / 128.0f) - 1.0f;
                    var b = (noiseZ[x, y] / 128.0f) - 1.0f;

                    noise[x * y] = new Color(r, g, b);
                }
            }

            this.NoiseMap.SetData(noise);
        }
Exemplo n.º 14
0
 public CombineDiffuseWithLightingStage(GraphicsDevice device, CombineEffect effect)
 {
     this.Device             = device;
     this.Effect             = effect;
     this.FullScreenTriangle = new FullScreenTriangle();
 }
        public void Draw(GraphicsDevice _graphicsDevice, bool useTonemap, RenderTarget2D currentFrame, RenderTarget2D previousFrames, RenderTarget2D output, Matrix currentViewToPreviousViewProjection, FullScreenTriangle fullScreenTriangle)
        {
            UseTonemap = useTonemap;

            _graphicsDevice.SetRenderTarget(output);
            _graphicsDevice.BlendState = BlendState.Opaque;

            _paramAccumulationMap.SetValue(previousFrames);
            _paramUpdateMap.SetValue(currentFrame);
            _paramCurrentToPrevious.SetValue(currentViewToPreviousViewProjection);

            _taaPass.Apply();
            fullScreenTriangle.Draw(_graphicsDevice);

            if (useTonemap)
            {
                _graphicsDevice.SetRenderTarget(currentFrame);
                _paramUpdateMap.SetValue(output);
                _invTonemapPass.Apply();
                fullScreenTriangle.Draw(_graphicsDevice);
            }
        }
Exemplo n.º 16
0
        public void Draw(GraphicsDevice graphicsDevice, Camera camera, VolumeTextureEntity volumeTextureEntity, FullScreenTriangle fullScreenTriangle)
        {
            CameraPosition = camera.Position;

            VolumeTex           = volumeTextureEntity.Texture;
            VolumeTexPosition   = volumeTextureEntity.Position;
            VolumeTexResolution = volumeTextureEntity.Resolution;
            VolumeTexSize       = volumeTextureEntity.Size;

            _basicPass.Apply();
            fullScreenTriangle.Draw(graphicsDevice);
            //quadRenderer.RenderFullscreenQuad(graphicsDevice);
        }
Exemplo n.º 17
0
        public void GenerateDistanceFields(BasicEntity entity, GraphicsDevice graphics, DistanceFieldRenderModule distanceFieldRenderModule, FullScreenTriangle fullScreenTriangle)
        {
            SignedDistanceField uncomputedSignedDistanceField = entity.SignedDistanceField;
            Model unprocessedModel = entity.Model;

            //Set to false so it won't get covered in future
            uncomputedSignedDistanceField.NeedsToBeGenerated = false;
            uncomputedSignedDistanceField.IsLoaded           = false;
            uncomputedSignedDistanceField.SdfTexture?.Dispose();

            //First generate tris
            Triangle[] triangles;
            GenerateTriangles(unprocessedModel, out triangles);

            int xsteps = (int)uncomputedSignedDistanceField.TextureResolution.X;
            int ysteps = (int)uncomputedSignedDistanceField.TextureResolution.Y;
            int zsteps = (int)uncomputedSignedDistanceField.TextureResolution.Z;

            Texture2D output;

            if (!GameSettings.sdf_cpu)
            {
                Stopwatch stopwatch = Stopwatch.StartNew();

                int maxwidth     = 4096; //16384
                int requiredData = triangles.Length * 3;

                int x = maxwidth;//Math.Min(requiredData, maxwidth);
                int y = requiredData / x + 1;

                Vector4[] data = new Vector4[x * y];

                int index = 0;
                for (int i = 0; i < triangles.Length; i++, index += 3)
                {
                    data[index]     = new Vector4(triangles[i].a, 0);
                    data[index + 1] = new Vector4(triangles[i].b, 0);
                    data[index + 2] = new Vector4(triangles[i].c, 0);
                }

                //16k

                Texture2D triangleData = new Texture2D(graphics, x, y, false, SurfaceFormat.Vector4);

                triangleData.SetData(data);

                output = distanceFieldRenderModule.CreateSDFTexture(graphics, triangleData, xsteps, ysteps,
                                                                    zsteps, uncomputedSignedDistanceField, fullScreenTriangle, triangles.Length);

                stopwatch.Stop();

                Debug.Write("\nSDF generated in " + stopwatch.ElapsedMilliseconds + "ms on GPU");

                float[] texData = new float[xsteps * ysteps * zsteps];

                output.GetData(texData);

                string path = uncomputedSignedDistanceField.TexturePath;
                DataStream.SaveImageData(texData, xsteps, ysteps, zsteps, path);
                uncomputedSignedDistanceField.TextureResolution = new Vector4(xsteps, ysteps, zsteps, 0);
                uncomputedSignedDistanceField.SdfTexture        = output;
                uncomputedSignedDistanceField.IsLoaded          = true;
            }
            else
            {
                generateTask = Task.Factory.StartNew(() =>
                {
                    output = new Texture2D(graphics, xsteps * zsteps, ysteps, false, SurfaceFormat.Single);

                    float[] data = new float[xsteps * ysteps * zsteps];

                    Stopwatch stopwatch = Stopwatch.StartNew();

                    int numberOfThreads = GameSettings.sdf_threads;

                    if (numberOfThreads > 1)
                    {
                        Task[] threads = new Task[numberOfThreads - 1];

                        //Make local datas

                        float[][] dataArray = new float[numberOfThreads][];

                        for (int index = 0; index < threads.Length; index++)
                        {
                            int i = index;
                            dataArray[index + 1] = new float[xsteps * ysteps * zsteps];
                            threads[i]           = Task.Factory.StartNew(() =>
                            {
                                GenerateData(xsteps, ysteps, zsteps, uncomputedSignedDistanceField,
                                             ref dataArray[i + 1], i + 1,
                                             numberOfThreads, triangles);
                            });
                        }

                        dataArray[0] = data;
                        GenerateData(xsteps, ysteps, zsteps, uncomputedSignedDistanceField, ref dataArray[0], 0,
                                     numberOfThreads, triangles);

                        Task.WaitAll(threads);

                        //Something broke?
                        for (int i = 0; i < data.Length; i++)
                        {
                            //data[i] = dataArray[i % numberOfThreads][i];
                            for (int j = 0; j < numberOfThreads; j++)
                            {
                                if (dataArray[j][i] != 0)
                                {
                                    data[i] = dataArray[j][i];
                                    break;
                                }
                            }
                        }

                        for (var index2 = 0; index2 < threads.Length; index2++)
                        {
                            threads[index2].Dispose();
                        }
                    }
                    else
                    {
                        GenerateData(xsteps, ysteps, zsteps, uncomputedSignedDistanceField, ref data, 0,
                                     numberOfThreads, triangles);
                    }

                    stopwatch.Stop();

                    Debug.Write("\nSDF generated in " + stopwatch.ElapsedMilliseconds + "ms with " +
                                GameSettings.sdf_threads + " thread(s)");

                    string path = uncomputedSignedDistanceField.TexturePath;
                    DataStream.SaveImageData(data, xsteps, ysteps, zsteps, path);
                    output.SetData(data);
                    uncomputedSignedDistanceField.TextureResolution = new Vector4(xsteps, ysteps, zsteps, 0);
                    uncomputedSignedDistanceField.SdfTexture        = output;
                    uncomputedSignedDistanceField.IsLoaded          = true;
                });
            }
        }
Exemplo n.º 18
0
        public void Update(List <BasicEntity> entities, GraphicsDevice graphics, DistanceFieldRenderModule distanceFieldRenderModule, FullScreenTriangle fullScreenTriangle, ref List <SignedDistanceField> sdfDefinitionsOut)
        {
            //First let's check which entities need building, if at all!
            sdfDefinitions.Clear();

            //This should preferably be a list of meshes that are in the scene, instead of a list of entities
            for (var index0 = 0; index0 < entities.Count; index0++)
            {
                BasicEntity entity = entities[index0];


                if (!entity.SignedDistanceField.IsUsed)
                {
                    continue;
                }

                if (entity.SignedDistanceField.NeedsToBeGenerated)
                {
                    GenerateDistanceFields(entity, graphics, distanceFieldRenderModule, fullScreenTriangle);
                }

                bool found = false;
                //Compile a list of all mbbs used right now
                for (var i = 0; i < sdfDefinitions.Count; i++)
                {
                    if (entity.SignedDistanceField == sdfDefinitions[i])
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    sdfDefinitions.Add(entity.SignedDistanceField);
                }
            }

            //Now for the model definitions
            for (var i = 0; i < sdfDefinitions.Count; i++)
            {
                if (GameSettings.sdf_regenerate)
                {
                    sdfDefinitions[i].NeedsToBeGenerated = true;
                }
            }

            GameSettings.sdf_regenerate = false;

            sdfDefinitionsOut = sdfDefinitions;
        }
        public void DrawEnvironmentMap(GraphicsDevice graphicsDevice, Camera camera, Matrix view, FullScreenTriangle fullScreenTriangle, EnvironmentSample envSample, GameTime gameTime, bool fireflyReduction, float ffThreshold)
        {
            FireflyReduction = fireflyReduction;
            FireflyThreshold = ffThreshold;

            SpecularStrength = envSample.SpecularStrength;
            DiffuseStrength  = envSample.DiffuseStrength;
            CameraPositionWS = camera.Position;

            Time = (float)gameTime.TotalGameTime.TotalSeconds % 1000;

            graphicsDevice.DepthStencilState = DepthStencilState.None;
            graphicsDevice.RasterizerState   = RasterizerState.CullCounterClockwise;
            UseSDFAO = envSample.UseSDFAO;
            _paramTransposeView.SetValue(Matrix.Transpose(view));
            _passBasic.Apply();
            fullScreenTriangle.Draw(graphicsDevice);
        }
        public RenderTarget2D CreateSDFTexture(GraphicsDevice graphics, Texture2D triangleData, int xsteps, int ysteps, int zsteps, SignedDistanceField sdf, FullScreenTriangle fullScreenTriangle, int trianglesLength)
        {
            RenderTarget2D output = new RenderTarget2D(graphics, xsteps * zsteps, ysteps, false, SurfaceFormat.Single, DepthFormat.None);

            graphics.SetRenderTarget(output);

            //Offset isntead of position!
            _volumeTexResolutionArray[0] = new Vector4(xsteps, ysteps, zsteps, 0);
            _volumeTexSizeArray[0]       = sdf.VolumeSize;

            _volumeTexSizeParam.SetValue(_volumeTexSizeArray);
            _volumeTexResolutionParam.SetValue(_volumeTexResolutionArray);

            MeshOffset = sdf.Offset;
            VolumeTex  = triangleData;

            _triangleTexResolution.SetValue(new Vector2(triangleData.Width, triangleData.Height));
            _triangleAmount.SetValue((float)trianglesLength);

            _generateSDFPass.Apply();
            fullScreenTriangle.Draw(graphics);

            _signedDistanceFieldDefinitionsCount = -1;

            return(output);
        }
Exemplo n.º 21
0
 public void Initialize(GraphicsDevice graphics)
 {
     _fullScreenTriangle = new FullScreenTriangle(graphics);
 }