//computed world-space normal and clipping space depth (depth in range 0, 1)
        //where the result will be stored, results will be stored to the first component of the texture
        //how many samples will be used for occlusion estimation
        public static RenderPass CreateAoc(
		                                    TextureBase normalDepth,
		                                    TextureBase aoc,
		                                    IValueProvider<Matrix4> modelviewprojection,
		                                    IValueProvider<Matrix4> modelviewprojection_inv,
		                                    IValueProvider<Matrix4> projection,
		                                    IValueProvider<Matrix4> projection_inv,
		                                    IValueProvider<int> samplesCount,
		                                    IValueProvider<float> occ_max_dist = null,
		                                    IValueProvider<float> occ_pixmax = null,
		                                    IValueProvider<float> occ_pixmin = null,
		                                    IValueProvider<float> occ_min_sample_ratio = null,
		                                    IValueProvider<bool> occ_constant_area = null,
		                                    IValueProvider<float> strength = null,
		                                    IValueProvider<float> bias = null)
        {
            var viewport = ValueProvider.Create (() => new Vector2 (aoc.Width, aoc.Height));
            //			var sampling_pattern = ValueProvider.Create
            //			(
            //				 () => MathHelper2.RandomVectorSet (256, new Vector2 (1, 1))
            //			);

            var current_pattern = MathHelper2.RandomVectorSet (256, new Vector2 (1, 1));
            var sampling_pattern = ValueProvider.Create
            (
                 () => current_pattern
            );

            var uniformState = new UniformState ();
            uniformState.Set ("viewport_size", viewport);
            uniformState.Set ("sampling_pattern", sampling_pattern);
            uniformState.Set ("sampling_pattern_len", samplesCount);

            uniformState.Set ("modelviewprojection_transform", modelviewprojection);
            uniformState.Set ("modelviewprojection_inv_transform", modelviewprojection_inv);
            uniformState.Set ("projection_transform", projection);
            uniformState.Set ("projection_inv_transform", projection_inv);

            uniformState.Set ("OCCLUDER_MAX_DISTANCE", occ_max_dist ?? ValueProvider.Create(35.0f));
            uniformState.Set ("PROJECTED_OCCLUDER_DISTANCE_MAX_SIZE", occ_pixmax ?? ValueProvider.Create(35.0f));
            uniformState.Set ("PROJECTED_OCCLUDER_DISTANCE_MIN_SIZE", occ_pixmin ?? ValueProvider.Create(2.0f));
            uniformState.Set ("MINIMAL_SAMPLES_COUNT_RATIO", occ_min_sample_ratio ?? ValueProvider.Create(0.1f));
            uniformState.Set ("USE_CONSTANT_OCCLUDER_PROJECTION", occ_constant_area ?? ValueProvider.Create(false));
            uniformState.Set ("AOC_STRENGTH", strength ?? ValueProvider.Create(2f));
            uniformState.Set ("AOC_BIAS", bias ?? ValueProvider.Create(-0.5f));

            return CreateFullscreenQuad (
                "aoc", "RenderPassFactory",
                viewport, null,
                window =>
                {
                    GL.Clear (ClearBufferMask.ColorBufferBit);
                    GL.Disable (EnableCap.DepthTest);
                    GL.Disable (EnableCap.Blend);
                },

                new FramebufferBindingSet{{ "OUT_FragData_aoc", aoc }},
                uniformState,
                new TextureBindingSet {{ "normaldepth_texture", normalDepth }});
        }
Ejemplo n.º 2
0
        /// <summary>
        /// creates non-separable filter pass
        /// </summary>
        public static RenderPass CreateFilter(
			 string filterName,
			 string filterNamespace,
			 TextureBase source,
			 TextureBase result)
        {
            var viewport = ValueProvider.Create (() => new Vector2 (result.Width, result.Height));
            var uniformState = new UniformState ();
            uniformState.Set ("viewport_size", viewport);

            var _1stPass = CreateFullscreenQuad (filterName, filterNamespace, viewport,
            null
            ,
            window =>
            {
                GL.Clear (ClearBufferMask.ColorBufferBit);
                GL.Disable (EnableCap.DepthTest);
                GL.Disable (EnableCap.Blend);
            //pass state
            },
            new FramebufferBindingSet {{ "OUT_FragData_result", result }},
            uniformState,
            new TextureBindingSet {{ "source_texture", source }});
            return _1stPass;
        }
Ejemplo n.º 3
0
        //
        public static RenderPass CreateBilateralFilter(
			 TextureBase source,
			 TextureBase interm,
			 TextureBase result,
			 IValueProvider<Vector4> k)
        {
            UniformState parameters = new UniformState();
            parameters.Set ("K", k);

            return CreateSeparableFilter("bilateralfilter", "RenderPassFactory", source, interm, result, parameters);
        }
Ejemplo n.º 4
0
        private void PrepareState()
        {
            if (m_Passes != null)
            {
                return;
            }

            unsafe
            {
                AOC_Texture =
                new DataTexture<float> {
                    Name = "AOC_Texture",
                    InternalFormat = PixelInternalFormat.R8,
                    Data2D = new float[m_AocTextureSize, m_AocTextureSize],
                    Params = new TextureBase.Parameters
                    {
                        GenerateMipmap = true,
                        MinFilter = TextureMinFilter.LinearMipmapLinear,
                        MagFilter = TextureMagFilter.Linear,
                }};

                NormalDepth_Texture =
                new DataTexture<Vector4> {
                    Name = "NormalDepth_Texture",
                    InternalFormat = PixelInternalFormat.Rgba32f,
                    //Format = PixelFormat.DepthComponent,
                    Data2D = new Vector4[m_SolidModeTextureSize, m_SolidModeTextureSize],
                    Params = new TextureBase.Parameters
                    {
                        GenerateMipmap = false,
                        MinFilter = TextureMinFilter.Nearest,
                        MagFilter = TextureMagFilter.Nearest,
                }};

                Depth_Texture =
                new DataTexture<float> {
                    Name = "Depth_Texture",
                    InternalFormat = PixelInternalFormat.DepthComponent32f,
                    Format = PixelFormat.DepthComponent,
                    Data2D = new float[m_SolidModeTextureSize, m_SolidModeTextureSize],
                    Params = new TextureBase.Parameters
                    {
                        GenerateMipmap = false,
                        MinFilter = TextureMinFilter.Nearest,
                        MagFilter = TextureMagFilter.Nearest,
                }};
            }

            var ortho = Matrix4.CreateOrthographic (1, 1, (float)NEAR, (float)FAR);

            m_Projection = new MatrixStack ().Push (ortho);
            m_TransformationStack = new MatrixStack ().Push (m_Projection);

            m_UniformState = new UniformState
            {
                {"pRayMarchStepFactor", () => this.RayMarchStepFactor},
                {"k1", () => this.K1},
                {"k2", () => this.K2},
                {"k3", () => this.K3},
                {"k4", () => this.K4},
                {"time", () => this.Time},
            };

            /*//
            var firstPassSolid = RenderPassFactory.CreateFullscreenQuad
            (
                 "raymarch", "System5",
                 ValueProvider.Create(() => new Vector2(m_SolidModeTextureSize, m_SolidModeTextureSize)),
                 (window) =>
                 {
                    SetCamera (window);
                 },
                 (window) =>
                 {
                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                    GL.Enable (EnableCap.DepthTest);
                    GL.DepthMask(true);
                    GL.DepthFunc (DepthFunction.Less);
                    GL.Disable (EnableCap.Blend);
                 },

                 //pass state
                 new FramebufferBindingSet
                 {
                   { FramebufferAttachment.DepthAttachment, Depth_Texture },
                   { "normal_depth", NormalDepth_Texture }
                 },
                 m_UniformState
            );*/

            var firstPassSolid = new SeparateProgramPass (
                "raymarch",
                (window) =>
                {
                    SetCamera (window);
                },
                (window) =>
                {
                    /*GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                    GL.Enable (EnableCap.DepthTest);
                    GL.DepthMask(true);
                    GL.DepthFunc (DepthFunction.Less);
                    GL.Disable (EnableCap.Blend);*/
                },
                x =>
                {
                    GLExtensions.DispatchCompute(m_SolidModeTextureSize/(4 * 8), m_SolidModeTextureSize/(4 * 8), 1);
                },
                RenderPass.GetShaders ("raymarch", "System5"),
                //pass state
                new ImageBindingSet
                {
                    { "u_NormalDepth", NormalDepth_Texture }
                },
                m_UniformState);

            AocParameters = new AocParameters
            {
                TextureSize = 512,
                OccConstantArea = false,
                OccMaxDist = 40,
                OccMinSampleRatio = 0.5f,
                OccPixmax = 100,
                OccPixmin = 2,
                SamplesCount = 32,
                Strength = 2.3f,
                Bias = 0.4f
            };

            var aocPassSolid = RenderPassFactory.CreateAoc
            (
                 NormalDepth_Texture,
                 AOC_Texture,
                 m_TransformationStack,
                 new MatrixInversion(m_TransformationStack),
                 m_Projection,
                 new MatrixInversion(m_Projection),
                 AocParameters
            );

            //
            var thirdPassSolid = RenderPassFactory.CreateFullscreenQuad
            (
                 "lighting", "System5",
                 ValueProvider.Create(() => m_Viewport),
                 (window) =>
                 {
                    SetCamera (window);
                 },
                 (window) =>
                 {
                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                    GL.Enable (EnableCap.DepthTest);
                    GL.DepthMask(true);
                    GL.DepthFunc (DepthFunction.Less);
                    GL.Disable (EnableCap.Blend);
                 },
                //pass state
                 FramebufferBindingSet.Default,
                 m_UniformState,
                 new TextureBindingSet
                 {
                   {"normaldepth_texture", NormalDepth_Texture },
                   { "aoc_texture", AOC_Texture },
                 }
            );

            m_Passes = new RenderPass[]{ firstPassSolid, aocPassSolid, thirdPassSolid };
            m_Manip = new OrbitManipulator (m_Projection);
            //m_Grid = new Grid (m_TransformationStack);

            m_TransformationStack.Push (m_Manip.RT);
            m_UniformState.Set ("modelview_transform", m_Manip.RT);
            m_UniformState.Set ("modelviewprojection_transform", m_TransformationStack);
            m_UniformState.Set ("projection_transform", m_Projection);
            m_UniformState.Set ("projection_inv_transform", new MatrixInversion(m_Projection));
            m_UniformState.Set ("modelview_inv_transform", new MatrixInversion(m_Manip.RT));
            m_UniformState.Set ("modelviewprojection_inv_transform", new MatrixInversion(m_TransformationStack));

            PrepareState ();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// create two passes in one compound pass. First pass has set "horizontal" uniform boolean to true,
        /// the second has it set to false.
        /// </summary>
        public static RenderPass CreateSeparableFilter(
			 string filterName,
			 string filterNamespace,
			 TextureBase source,
			 TextureBase interm,
			 TextureBase result,
			 UniformState parameters)
        {
            var viewport = ValueProvider.Create (() => new Vector2 (result.Width, result.Height));
            var horizontal = false;
            var uniformState = new UniformState ();
            uniformState.Set ("viewport_size", viewport);
            uniformState.Set ("horizontal", ValueProvider.Create (() => horizontal));

            parameters = parameters?? new UniformState();

            var _1stPass = CreateFullscreenQuad (filterName, filterNamespace, viewport,
            window =>
            {
                horizontal = false;
            }
            ,
            window =>
            {
                GL.Clear (ClearBufferMask.ColorBufferBit);
                GL.Disable (EnableCap.DepthTest);
                GL.Disable (EnableCap.Blend);
            //pass state
            },
            new FramebufferBindingSet {{ "OUT_FragData_result", interm }},
            uniformState,
            parameters,
            new TextureBindingSet {{ "source_texture", source }});

            var _2ndPass = CreateFullscreenQuad (filterName, filterNamespace, viewport,
            window =>
            {
                horizontal = true;
            }
            ,
            window =>
            {
                GL.Clear (ClearBufferMask.ColorBufferBit);
                GL.Disable (EnableCap.DepthTest);
                GL.Disable (EnableCap.Blend);
            //pass state
            },
            new FramebufferBindingSet {{ "OUT_FragData_result", result }},
            uniformState,
            parameters,
            new TextureBindingSet {{ "source_texture", interm }});

            return new CompoundRenderPass(_1stPass, _2ndPass);
        }
Ejemplo n.º 6
0
        private void PrepareState()
        {
            if (m_Passes != null)
            {
                return;
            }

            unsafe
            {
                BeforeAATexture =
                new DataTexture<Vector4> {
                    Name = "BeforeAATexture",
                    InternalFormat = PixelInternalFormat.Rgba32f,
                    //Format = PixelFormat.DepthComponent,
                    Data2D = new Vector4[m_SolidModeTextureSize, m_SolidModeTextureSize],
                    Params = new TextureBase.Parameters
                    {
                        GenerateMipmap = false,
                        MinFilter = TextureMinFilter.Nearest,
                        MagFilter = TextureMagFilter.Nearest,
                }};

                AccumTexture =
                new DataTexture<Vector4> {
                    Name = "AccumTexture",
                    InternalFormat = PixelInternalFormat.Rgba32f,
                    Data2D = new Vector4[m_SolidModeTextureSize, m_SolidModeTextureSize],
                    Params = new TextureBase.Parameters
                    {
                        GenerateMipmap = false,
                        MinFilter = TextureMinFilter.Nearest,
                        MagFilter = TextureMagFilter.Nearest,
                    }};

                AATexture = new Texture
                {
                    Name = "AATexture",
                    InternalFormat = PixelInternalFormat.Rgba8,
                    Target = TextureTarget.Texture2D,
                    Width = m_SolidModeTextureSize,
                    Height = m_SolidModeTextureSize,
                    Params = new TextureBase.Parameters
                    {
                        //GenerateMipmap = true,
                        MinFilter = TextureMinFilter.Nearest,
                        MagFilter = TextureMagFilter.Nearest,
                    }
                };

                Depth_Texture =
                new DataTexture<float> {
                    Name = "Depth_Texture",
                    InternalFormat = PixelInternalFormat.DepthComponent32f,
                    Format = PixelFormat.DepthComponent,
                    Data2D = new float[m_SolidModeTextureSize, m_SolidModeTextureSize],
                    Params = new TextureBase.Parameters
                    {
                        GenerateMipmap = false,
                        MinFilter = TextureMinFilter.Nearest,
                        MagFilter = TextureMagFilter.Nearest,
                }};
            }

            var ortho = Matrix4.CreateOrthographic (1, 1, (float)NEAR, (float)FAR);

            m_Projection = new MatrixStack ().Push (ortho);
            m_TransformationStack = new MatrixStack ().Push (m_Projection);

            var frameCount = 1;
            m_UniformState = new UniformState
            {
                {"pRayMarchStepFactor", () => this.RayMarchStepFactor},
                {"k1", () => this.K1},
                {"k2", () => this.K2},
                {"k3", () => this.K3},
                {"k4", () => this.K4},
                {"time", () => this.Time},
                {"u_FrameCount", () => frameCount},
            };

            var workgroupSize = 8;
            var giPass = new SeparateProgramPass(
                "system6.globalillumination",
                window => { GLExtensions.DispatchCompute ((int)Math.Ceiling((float)Depth_Texture.Width/workgroupSize), (int)Math.Ceiling((float)Depth_Texture.Height/workgroupSize), 1); },
              new Program ("system6.gi")
                {
                    RenderPass.GetShaders ("system6", "gi", "compute")//.PrependText(namemodifier, scode),
                },
                new ImageBindingSet
                {
                    {"u_TargetDepth", () => Depth_Texture },
                    {"u_TargetColorLuma", () => BeforeAATexture },
                  {"u_TargetAccumLuma", () => AccumTexture },
                },
              m_UniformState);

            //var antialiasPass = RenderPassFactory.CreatePass( new Fxaa3Filter{ Source = BeforeAATexture, Target = AATexture});

            var finalRender = RenderPassFactory.CreateRenderTextureToBuffer
            (
                BeforeAATexture,
                Depth_Texture,
                ValueProvider.Create(() => m_Viewport),
                (window) =>
                {
                    SetCamera (window);
                },
                (window) =>
                {
                    GL.Clear (ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                    GL.Disable (EnableCap.DepthTest);
                    GL.Disable (EnableCap.Blend);
                    frameCount++;
                },
                FramebufferBindingSet.Default);

            m_Passes = new RenderPass[]{ giPass, /*antialiasPass, */finalRender };
            m_Manip = new OrbitManipulator (m_Projection);
            //m_Grid = new Grid (m_TransformationStack);

            m_TransformationStack.Push (m_Manip.RT);
            m_UniformState.Set ("modelview_transform", m_Manip.RT);
            m_UniformState.Set ("modelviewprojection_transform", m_TransformationStack);
            m_UniformState.Set ("projection_transform", m_Projection);
            m_UniformState.Set ("projection_inv_transform", new MatrixInversion(m_Projection));
            m_UniformState.Set ("modelview_inv_transform", new MatrixInversion(m_Manip.RT));
            m_UniformState.Set ("modelviewprojection_inv_transform", new MatrixInversion(m_TransformationStack));

            m_Manip.RT.PropertyChanged +=
            (s, args) => { frameCount = 0; };

            PrepareState ();
        }
        public void SetUniforms(string prefix, UniformState state)
        {
            string pp = String.IsNullOrEmpty(prefix)?
                String.IsNullOrEmpty(Prefix) ?
                    string.Empty:
                    Prefix + "_":
                prefix + "_";

            state.Set (pp + "modelview_transform", ModelView);
            state.Set (pp + "modelviewprojection_transform", ModelViewProjection);
            state.Set (pp + "projection_transform", Projection);
            state.Set (pp + "projection_inv_transform", ProjectionInv);
            state.Set (pp + "modelview_inv_transform", ModelViewInv);
            state.Set (pp + "modelviewprojection_inv_transform", ModelViewProjectionInv);
        }
Ejemplo n.º 8
0
        RenderPass IShadingSetup.GetPass(ParticleSystemBase p)
        {
            PrepareTexture();

            if(m_Pass != null)
                return m_Pass;

            m_Uniforms = new UniformState(p.Uniforms);
            m_Uniforms.Set ("particle_shape", ValueProvider.Create (() => (int)this.ParticleShape));
            m_Uniforms.Set ("particle_brightness", ValueProvider.Create (() => this.ParticleBrightness));
            m_Uniforms.Set ("smooth_shape_sharpness", ValueProvider.Create (() => this.SmoothShapeSharpness));

            //
            m_Pass = new SeparateProgramPass
            (
                 "light",
                 "SmoothShading",
                 null,
                 null,

                 //pass code
                 (window) =>
                 {
                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                    GL.Disable (EnableCap.DepthTest);
                    GL.Enable (EnableCap.Blend);
                    GL.BlendFunc (BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
                    GL.BlendEquation (BlendEquationMode.FuncAdd);

                    //TODO: viewport size actually doesn't propagate to shader, because uniform state has been already activated
                    p.SetViewport (window);
                    GL.DrawArrays (BeginMode.Points, 0, p.PARTICLES_COUNT);
                 },

                 //pass state
                 FramebufferBindingSet.Default,
                 p.ParticleStateArrayObject,
                 m_Uniforms,
                 new TextureBindingSet(
                   new TextureBinding { VariableName = "custom_texture", Texture = m_Texture }
                 )
            );

            return m_Pass;
        }