/// <summary> /// given color and depth textures, render them. /// </summary> private static RenderPass CreateSolidSphere( FramebufferBindingSet targets, BufferObject<Vector4> sprite_pos_buffer, BufferObject<Vector4> sprite_color_buffer, BufferObject<Vector4> sprite_dimensions_buffer, IValueProvider<Vector2> viewport, IValueProvider<int> particles_count, IValueProvider<float> particle_scale_factor, IValueProvider<string> fragdepthroutine, IValueProvider<string> outputroutine, ModelViewProjectionParameters mvp, UniformState subroutineMapping, IEnumerable<Shader> subroutines ) { var uniform_state = subroutineMapping != null? new UniformState (subroutineMapping): new UniformState(); uniform_state.Set ("viewport_size", viewport); uniform_state.Set ("particle_scale_factor", particle_scale_factor); uniform_state.Set ("u_SetFragmentDepth", ShaderType.FragmentShader, fragdepthroutine); uniform_state.Set ("u_SetOutputs", ShaderType.FragmentShader, outputroutine); uniform_state.SetMvp("", mvp); var array_state = new ArrayObject ( new VertexAttribute { AttributeName = "sprite_pos", Buffer = sprite_pos_buffer, Size = 3, Stride = 16, Type = VertexAttribPointerType.Float }, new VertexAttribute { AttributeName = "sprite_color", Buffer = sprite_color_buffer, Size = 3, Stride = 16, Type = VertexAttribPointerType.Float }, new VertexAttribute { AttributeName = "sprite_dimensions", Buffer = sprite_dimensions_buffer, Size = 3, Stride = 16, Type = VertexAttribPointerType.Float } ); var shaders = SeparateProgramPass.GetShaders("solid_sphere", "RenderPassFactory"); shaders = shaders.Concat(subroutines ?? new Shader[0]).ToArray(); // var resultPass = new SeparateProgramPass ( //the name of the pass-program "solid_sphere_RenderPassFactory", //before state null, //before render null, //render code (window) => { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Enable (EnableCap.DepthTest); GL.DepthMask(true); GL.DepthFunc (DepthFunction.Less); GL.Disable (EnableCap.Blend); //setup viewport GL.Viewport(0, 0, (int)viewport.Value.X, (int)viewport.Value.Y); GL.DrawArrays (BeginMode.Points, 0, particles_count.Value); }, //shaders shaders, //pass state array_state, uniform_state, targets ); return resultPass; }
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 (); }
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 (); }
protected override void PassSetup(ParticleSystemBase p) { base.PassSetup(p); var particle_scale_factor = ValueProvider.Create (() => p.ParticleScaleFactor); var particle_count = ValueProvider.Create (() => p.PARTICLES_COUNT); var firstPassSolid = RenderPassFactory.CreateSolidBox ( NormalDepth_Texture, m_ParticleAttribute1_Texture, Depth_Texture, p.PositionBuffer, p.ColorBuffer, p.DimensionBuffer, p.RotationLocalBuffer, p.RotationBuffer, particle_count, particle_scale_factor, p.CameraMvp ); var firstPassShadow = RenderPassFactory.CreateSolidBox ( Shadow_Texture, p.PositionBuffer, p.ColorBuffer, p.DimensionBuffer, p.RotationLocalBuffer, p.RotationBuffer, particle_count, particle_scale_factor, ValueProvider.Create ( () => "FragDepth" + SunLightImpl.ShadowmapType.ToString ()), SunLightImpl.LightMvp ); /*var aocPassSolid = RenderPassFactory.CreateAoc ( NormalDepth_Texture, AOC_Texture, p.CameraMvp.ModelViewProjection, p.CameraMvp.ModelViewProjectionInv, p.CameraMvp.Projection, p.CameraMvp.ProjectionInv, AocParameters );*/ SsaoEffect.SourceNormalDepth = NormalDepth_Texture; SsaoEffect.Target = AOC_Texture; SsaoEffect.SourceMvp = p.CameraMvp; var aocPassSolid = RenderPassFactory.CreatePass(SsaoEffect); /*var aocBlur = RenderPassFactory.CreateBilateralFilter ( AOC_Texture, AOC_Texture_Blurred_H, AOC_Texture_Blurred_HV, ValueProvider.Create(() => 20 * new Vector4(AocParameters.BlurEdgeAvoidance, AocParameters.BlurEdgeAvoidance, AocParameters.BlurEdgeAvoidance, 0)) );*/ var aocBlur = RenderPassFactory.CreatePass(new BilateralFilter { Source = AOC_Texture, SourceK = NormalDepth_Texture, Target = AOC_Texture_Blurred_HV, Width = 4 }); string scode = @" {0}version 440 {0}define T_LAYOUT_OUT_DEPTH {1} {0}define T_LAYOUT_OUT_COLORLUMA {2} layout(local_size_x = {3}, local_size_y = {4}) in; {0}line 1 "; int workgroupSize = 16; scode = string.Format (scode, "#", ImageFormat.R32f, BeforeAA_Texture.InternalFormat, workgroupSize, workgroupSize); var namemodifier = string.Format ("wgsize:{0}x{0},fi:{1},fi:{2}", workgroupSize, ImageFormat.R32f, BeforeAA_Texture.InternalFormat); var deferredLigthing = new SeparateProgramPass ( "shadingsetup.solidbox.deferredligthing", window => { GLExtensions.DispatchCompute ((int)Math.Ceiling((float)Depth_Texture.Width/workgroupSize), (int)Math.Ceiling((float)Depth_Texture.Height/workgroupSize), 1); }, new Program ("shadingsetup.solidbox.deferredligthing") { RenderPass.GetShaders ("shadingsetup", "solid3", "compute").PrependText(namemodifier, scode), }, new ImageBindingSet { {"u_TargetDepth", () => Depth_Texture }, {"u_TargetColorLuma", () => BeforeAA_Texture }, }, p.ParticleStateArrayObject, m_Uniforms, new TextureBindingSet { { "u_ColorRampTexture", ValueProvider.Create(() => (ColorRamp ?? ColorRamps.RedBlue).Texture)}, { "u_NormalDepthTexture", NormalDepth_Texture }, { "u_ParticleAttribute1Texture", m_ParticleAttribute1_Texture }, { "u_ShadowTexture", Shadow_Texture }, { "u_SsaoTexture", AOC_Texture_Blurred_HV } } ); var antialiasPass = RenderPassFactory.CreatePass( new Fxaa3Filter{ Source = BeforeAA_Texture, Target = AA_Texture}); var finalRender = RenderPassFactory.CreateRenderTextureToBuffer ( AA_Texture, Depth_Texture, ValueProvider.Create(() => p.Viewport), (window) => { p.SetViewport (window); }, (window) => { GL.Clear (ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Disable (EnableCap.DepthTest); GL.Disable (EnableCap.Blend); }, //TODO: BUG m_ParticleRenderingState is necessary, but it shouldn't be FramebufferBindingSet.Default, p.ParticleStateArrayObject ); m_Pass = new CompoundRenderPass ( firstPassSolid, firstPassShadow, aocPassSolid, aocBlur, deferredLigthing, antialiasPass, finalRender ); }