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 (); }
// private void PrepareState() { if (m_Initialized) { if (PARTICLES_COUNT != m_PositionBuffer.Data.Length) { Position = m_PositionBuffer.Data = new Vector4[PARTICLES_COUNT]; Dimension = m_DimensionBuffer.Data = new Vector4[PARTICLES_COUNT]; Color = m_ColorBuffer.Data = new Vector4[PARTICLES_COUNT]; Rotation = m_RotationBuffer.Data = new Matrix4[PARTICLES_COUNT]; RotationLocal = m_RotationLocalBuffer.Data = new Matrix4[PARTICLES_COUNT]; InitializeSystem (); } Simulate (DateTime.Now); switch (PublishMethod){ case PublishMethod.AllAtOnce: m_PositionBuffer.Publish (); m_DimensionBuffer.Publish (); m_ColorBuffer.Publish (); m_RotationBuffer.Publish(); m_RotationLocalBuffer.Publish(); Publish (0, PARTICLES_COUNT); break; case PublishMethod.Incremental: { m_PublishCounter %= PARTICLES_COUNT; var start = m_PublishCounter; var end = Math.Min(start + PublishSize, PARTICLES_COUNT); var cnt = end - start; m_PositionBuffer.PublishPart (start, cnt); m_DimensionBuffer.PublishPart (start, cnt); m_ColorBuffer.PublishPart (start, cnt); m_RotationBuffer.PublishPart (start, cnt); m_RotationLocalBuffer.PublishPart (start, cnt); Publish (start, cnt); m_PublishCounter = end; } break; default: break; } return; } unsafe { m_PositionBuffer = new BufferObject<Vector4> (sizeof(Vector4), 0) { Name = "position_buffer", Usage = BufferUsageHint.DynamicDraw }; m_DimensionBuffer = new BufferObject<Vector4> (sizeof(Vector4), 0) { Name = "dimension_buffer", Usage = BufferUsageHint.DynamicDraw }; m_ColorBuffer = new BufferObject<Vector4> (sizeof(Vector4), 0) { Name = "color_buffer", Usage = BufferUsageHint.DynamicDraw }; m_RotationBuffer = new BufferObject<Matrix4> (sizeof(Matrix4), 0) { Name = "rotation_buffer", Usage = BufferUsageHint.DynamicDraw }; m_RotationLocalBuffer = new BufferObject<Matrix4> (sizeof(Matrix4), 0) { Name = "rotation_local_buffer", Usage = BufferUsageHint.DynamicDraw }; } ParticleStateArrayObject = new ArrayObject ( new VertexAttribute { AttributeName = "sprite_pos", Buffer = m_PositionBuffer, Size = 3, Stride = 16, Type = VertexAttribPointerType.Float }, new VertexAttribute { AttributeName = "sprite_color", Buffer = m_ColorBuffer, Size = 3, Stride = 16, Type = VertexAttribPointerType.Float }, new VertexAttribute { AttributeName = "sprite_dimensions", Buffer = m_DimensionBuffer, Size = 3, Stride = 16, Type = VertexAttribPointerType.Float }, new VertexAttribute { AttributeName = "sprite_rotation_local", Buffer = m_RotationLocalBuffer, Size = 16, Stride = 64, Type = VertexAttribPointerType.Float }, new VertexAttribute { AttributeName = "sprite_rotation", Buffer = m_RotationBuffer, Size = 16, Stride = 64, Type = VertexAttribPointerType.Float } ); // PublishSize = 100000; ModelScaleFactor = 1; TransformationStack = new MatrixStack(2); ProjectionStack = new MatrixStack(1); CameraMvp = new ModelViewProjectionParameters("", TransformationStack, ProjectionStack); // m_Manip = new OrbitManipulator (ProjectionStack); m_Grid = new Grid (CameraMvp); TransformationStack[0] = m_Manip.RT; TransformationStack.ValueStack[1] = Matrix4.Scale(ModelScaleFactor); // Uniforms = new UniformState { {"particle_scale_factor", () => this.ParticleScaleFactor}, {CameraMvp} }; // Shading = GlobalContext.Container.GetExportedValues<IShadingSetup>().FirstOrDefault(); PrepareStateCore(); m_Initialized = true; PrepareState (); }
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 (); }