///////////////////////////////////////// protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { base.OnRender(context, frameData, ref actualTexture); //PixelFormat.A8R8G8B8 ); //bright pass //!!!!или A8R8G8B8? //R8G8B8 var brightTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, PixelFormat.A8R8G8B8); //!!!! PixelFormat.R8G8B8 ); { context.SetViewport(brightTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\Bloom\Bright_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point)); shader.Parameters.Set("brightThreshold", (float)BrightThreshold); context.RenderQuadToCurrentViewport(shader); } //blur var pipeline = (Component_RenderingPipeline_Basic)context.RenderingPipeline; var blurTexture = pipeline.GaussianBlur(context, this, brightTexture, BlurFactor, BlurDownscalingMode, BlurDownscalingValue); context.DynamicTexture_Free(brightTexture); //create final var finalTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, actualTexture.Result.ResultFormat); { context.SetViewport(finalTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\Bloom\Final_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1 /*"bloomTexture"*/, blurTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("intensity", (float)Intensity); shader.Parameters.Set("scale", (float)Scale); context.RenderQuadToCurrentViewport(shader); } //free old textures context.DynamicTexture_Free(actualTexture); context.DynamicTexture_Free(blurTexture); //update actual texture actualTexture = finalTexture; }
protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { var demandFormat = PixelFormat.Float16RGBA; if (actualTexture.Result.ResultFormat != demandFormat) { var newTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, demandFormat); context.SetViewport(newTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; //shader.VertexProgramFunctionName = "main_vp"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ToHDR_fs.sc"; //shader.FragmentProgramFunctionName = "main_fp"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); //var size = context.owner.DimensionsInPixels.Size; //shader.Parameters.Set( "viewportSize", new Vec4( size.X, size.Y, 1.0 / (double)size.X, 1.0 / (double)size.Y ).ToVec4F() ); //Mat4F identity = Mat4F.Identity; //shader.Parameters.Set( "worldViewProjMatrix", ParameterType.Matrix4x4, 1, &identity, sizeof( Mat4F ) ); context.RenderQuadToCurrentViewport(shader); //free old texture context.DynamicTexture_Free(actualTexture); actualTexture = newTexture; } }
protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { var newTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, PixelFormat.A8R8G8B8); context.SetViewport(newTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ToneMapping_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("u_tonemapping_parameters", new Vector4F((float)Intensity, (float)GammaInput, (float)Exposure, (float)GammaOutput)); shader.Defines.Add(new CanvasRenderer.ShaderItem.DefineItem($"TONEMAPPING_METHOD_{Method.Value.ToString().ToUpper()}")); if (Method.Value == MethodEnum.Custom) { shader.Defines.Add(new CanvasRenderer.ShaderItem.DefineItem("CUSTOM_CODE", CustomCode.Value)); } context.RenderQuadToCurrentViewport(shader); //free old texture context.DynamicTexture_Free(actualTexture); actualTexture = newTexture; }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public virtual void ConvertToLDR(ViewportRenderingContext context, ref Component_Image actualTexture) { //!!!!может другие тоже форматы? провер¤ть на FloatXX? var demandFormat = PixelFormat.A8R8G8B8; if (actualTexture.Result.ResultFormat != demandFormat) { var newTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, demandFormat); context.SetViewport(newTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ToLDR_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); //var size = context.owner.DimensionsInPixels.Size; //shader.Parameters.Set( "viewportSize", new Vec4( size.X, size.Y, 1.0 / (double)size.X, 1.0 / (double)size.Y ).ToVec4F() ); //Mat4F identity = Mat4F.Identity; //shader.Parameters.Set( "worldViewProjMatrix", ParameterType.Matrix4x4, 1, &identity, sizeof( Mat4F ) ); context.RenderQuadToCurrentViewport(shader); //free old texture context.DynamicTexture_Free(actualTexture); actualTexture = newTexture; } }
public void RenderDownscale(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, Vector2I destinationSize, ref Component_Image actualTexture) { if (Technique.Value >= TechniqueEnum.SSAAx2 && Technique.Value <= TechniqueEnum.SSAAx4 && context.Owner.SizeInPixels != actualTexture.Result.ResultSize) { var rescaledTexture = context.RenderTarget2D_Alloc(context.Owner.SizeInPixels, actualTexture.Result.ResultFormat); //copy to scene texture with downscale { context.SetViewport(rescaledTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\DownscaleAntialiasing_fs.sc"; shader.Defines.Add(new CanvasRenderer.ShaderItem.DefineItem(Technique.Value.ToString().ToUpper())); var multiplier = new Vector2F(1, 1) / destinationSize.ToVector2F() * (float)DownscaleSamplerMultiplier.Value; shader.Parameters.Set("antialiasing_multiplier", multiplier); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.None)); context.RenderQuadToCurrentViewport(shader); } context.DynamicTexture_Free(actualTexture); actualTexture = rescaledTexture; } }
protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { base.OnRender(context, frameData, ref actualTexture); //create final var finalTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, PixelFormat.A8R8G8B8); { context.SetViewport(finalTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ShowRenderTarget_fs.sc"; string textureName = ""; switch (Texture.Value) { case TextureType.Normal: textureName = "normalTexture"; break; case TextureType.Depth: textureName = "depthTexture"; break; case TextureType.MotionVector: textureName = "motionTexture"; break; } context.objectsDuringUpdate.namedTextures.TryGetValue(textureName, out var showTexture); if (showTexture == null) { context.DynamicTexture_Free(finalTexture); return; } shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1 /*"showTexture"*/, showTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("intensity", (float)Intensity); shader.Parameters.Set("nearClipDistance", (float)context.Owner.CameraSettings.NearClipDistance); shader.Parameters.Set("farClipDistance", (float)context.Owner.CameraSettings.FarClipDistance); shader.Parameters.Set("mode", (float)Texture.Value); shader.Parameters.Set("depthMultiplier", (float)DepthMultiplier); shader.Parameters.Set("motionMultiplier", (float)MotionMultiplier); context.RenderQuadToCurrentViewport(shader); } //free old textures context.DynamicTexture_Free(actualTexture); //update actual texture actualTexture = finalTexture; }
///////////////////////////////////////// protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { context.objectsDuringUpdate.namedTextures.TryGetValue("motionTexture", out var motionTexture); if (motionTexture != null) { //create final var finalTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, actualTexture.Result.ResultFormat); { context.SetViewport(finalTexture.Result.GetRenderTarget().Viewports[0]); var shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\MotionBlur_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1 /*"motionTexture"*/, motionTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("intensity", (float)Intensity); var multiplier = 0.0; if (context.Owner.LastUpdateTimeStep != 0) { multiplier = (1.0 / context.Owner.LastUpdateTimeStep) / 45.0; } multiplier *= Multiplier.Value; //fix initial rattling if (context.Owner.LastUpdateTimeStep > 0.1) { multiplier = 0; } shader.Parameters.Set("motionBlurMultiplier", (float)multiplier); var size = actualTexture.Result.ResultSize; shader.Parameters.Set("viewportSize", new Vector4(size.X, size.Y, 1.0 / (double)size.X, 1.0 / (double)size.Y).ToVector4F()); context.RenderQuadToCurrentViewport(shader); } //free old textures context.DynamicTexture_Free(actualTexture); //update actual texture actualTexture = finalTexture; } }
///////////////////////////////////////// protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { base.OnRender(context, frameData, ref actualTexture); //create final var finalTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, actualTexture.Result.ResultFormat); { context.SetViewport(finalTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\EdgeDetection_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); context.objectsDuringUpdate.namedTextures.TryGetValue("depthTexture", out var depthTexture); //if( depthTexture == null ) //{ //} shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1 /*"depthTexture"*/, depthTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); context.objectsDuringUpdate.namedTextures.TryGetValue("normalTexture", out var normalTexture); if (normalTexture == null) { normalTexture = ResourceUtility.WhiteTexture2D; } shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(2 /*"normalTexture"*/, normalTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("intensity", (float)Intensity); shader.Parameters.Set("depthThreshold", (float)DepthThreshold); shader.Parameters.Set("normalsThreshold", (float)NormalsThreshold); shader.Parameters.Set("edgeColor", EdgeColor.Value.ToVector4F()); shader.Parameters.Set("thickness", (float)Thickness); shader.Parameters.Set("maxDistance", (float)MaxDistance); context.RenderQuadToCurrentViewport(shader); } //free old textures context.DynamicTexture_Free(actualTexture); //update actual texture actualTexture = finalTexture; }
protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { base.OnRender(context, frameData, ref actualTexture); var pipeline = context.RenderingPipeline as Component_RenderingPipeline_Basic; if (pipeline == null) { return; } var frameData2 = frameData as Component_RenderingPipeline_Basic.FrameData; if (frameData2 == null) { return; } if (Intensity.Value == 0) { return; } bool skip = true; foreach (var lightIndex in frameData2.LightsInFrustumSorted) { var lightItem = frameData2.Lights[lightIndex]; if (lightItem.data.children != null) { foreach (var child in lightItem.data.children) { var lensFlares = child as Component_LensFlares; if (lensFlares != null) { skip = false; break; } } } } if (skip) { return; } //init context data ViewportRenderingContextData contextData; if (context.anyData.TryGetValue("LensEffects", out var contextData2)) { contextData = (ViewportRenderingContextData)contextData2; } else { contextData = new ViewportRenderingContextData(); context.anyData["LensEffects"] = contextData; } //update context data light items contextData.Update(context.Owner.LastUpdateTimeStep, Intensity, FadeSpeed); //create copy var newTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, actualTexture.Result.ResultFormat, createSimple3DRenderer: true, createCanvasRenderer: true); var viewport = newTexture.Result.GetRenderTarget().Viewports[0]; //!!!!double precision Matrix4F viewMatrix = context.Owner.CameraSettings.ViewMatrix.ToMatrix4F(); Matrix4F projectionMatrix = context.Owner.CameraSettings.ProjectionMatrix.ToMatrix4F(); context.SetViewport(viewport, viewMatrix, projectionMatrix); pipeline.CopyToCurrentViewport(context, actualTexture); var simple3DRenderer = viewport.Simple3DRenderer; var canvasRenderer = viewport.CanvasRenderer; //render foreach (var lightIndex in frameData2.LightsInFrustumSorted) { var lightItem = frameData2.Lights[lightIndex]; if (lightItem.data.children != null) { foreach (var child in lightItem.data.children) { var lensFlares = child as Component_LensFlares; if (lensFlares != null) { var light = lightItem.data.Creator as Component_Light; if (light != null) { CheckVisibilityScreenPosition(context, lightItem.data.Position, out var gotScreenPosition, out var insideScreen); if (gotScreenPosition) { //update { switch (CheckVisibilityMethod.Value) { case CheckVisibilityMethodEnum.OcclusionQuery: { var parameter = (contextData, light); if (RenderingSystem.TryCreateOcclusionQuery(OcclusionQueryCallback, parameter, out var query)) { var thickness = context.Owner.Simple3DRenderer.GetThicknessByPixelSize(lightItem.data.Position, 5); var bounds = new Bounds(lightItem.data.Position); bounds.Expand(thickness * 0.5); simple3DRenderer.SetColor(new ColorValue(1, 0, 0)); simple3DRenderer.SetOcclusionQuery(query); simple3DRenderer.AddBounds(bounds, true); //simple3DRenderer.AddSphere( new Sphere( lightItem.data.Position, 0.5 ), 4, true ); simple3DRenderer.SetOcclusionQuery(null); } } break; case CheckVisibilityMethodEnum.PhysicsRayCast: if (CheckVisibilityPhysics(context, lightItem.data.Position)) { contextData.UpdateLastTimeVisible(light); } break; } } //render var intensity = contextData.GetIntensity(light); if (intensity > 0) { //simple3DRenderer.SetColor( new ColorValue( 1, 0, 0 ) ); //simple3DRenderer.AddSphere( new Sphere( lightItem.data.Position, 0.5 ), 10, true ); lensFlares.RenderUI(canvasRenderer, lightItem.data, context.Owner, intensity); } } else { contextData.Remove(light); } } } } } } //render if (simple3DRenderer._ViewportRendering_PrepareRenderables()) { simple3DRenderer._ViewportRendering_RenderToCurrentViewport(context); simple3DRenderer._Clear(); } canvasRenderer._ViewportRendering_RenderToCurrentViewport(context, true, context.Owner.LastUpdateTime); //free old textures context.DynamicTexture_Free(actualTexture); //update actual texture actualTexture = newTexture; }
protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { var result = Result as CompiledDataSimple; //!!!!так? if (result == null) { return; } if (result.vertexProgram == null || result.fragmentProgram == null) { return; } //!!!!валится если файла нет. или не указан. //!!!!!!как быстро проверять о том что файла нет? может из кеша шейдеров или типа того. как тогда обновлять //string shaderFile = Shader; //if( string.IsNullOrEmpty( shaderFile ) ) // return; //!!!!formats. т.е. эффект поддерживает и LDR и HDR. где еще //!!!!!можно по сути у эффекта указывать что он ожидает получить на входе. ну или это EffectsHDR, EffectsLDR var outputTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, actualTexture.Result.ResultFormat); context.SetViewport(outputTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.CompiledVertexProgram = result.vertexProgram; shader.CompiledFragmentProgram = result.fragmentProgram; //shader.VertexProgramFileName = shaderFile; //shader.VertexProgramFunctionName = "main_vp"; //shader.FragmentProgramFileName = shaderFile; //shader.FragmentProgramFunctionName = "main_fp"; //{ // var defines = new List<GuiRenderer.ShaderItem.DefineItem>(); // //defines.Add( new GuiRenderer.ShaderItem.DefineItem( "FRAGMENT_CODE_BODY", "color = float4(0,1,0,1);" ) ); // if( defines.Count != 0 ) // shader.Defines = defines.ToArray(); //} shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.Point)); //shader.Parameters.Set( "0"/*"sourceTexture"*/, new GpuMaterialPass.TextureParameterValue( actualTexture, // TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.Point ) ); var size = actualTexture.Result.ResultSize; shader.Parameters.Set("viewportSize", new Vector4(size.X, size.Y, 1.0 / (double)size.X, 1.0 / (double)size.Y).ToVector4F()); //!!!!типы //bool, int, uint, dword, half, float, double //Vec2, Vec3, Vec4, VecxF, Mat2, 3, 4 //Texture //Structs //arrays? //Structured Buffer //byte buffers //set shader parameters by properties foreach (var m in MetadataGetMembers()) { var p = m as Metadata.Property; if (p != null) { if (CanBeShaderParameter(this, p, out object value)) { Component_Image texture = value as Component_Image; if (texture != null) { foreach (Component_Image.BindSettingsAttribute attr in p.GetCustomAttributes(typeof(Component_Image.BindSettingsAttribute), true)) { shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(attr.SamplerIndex, texture, attr.AddressingMode, attr.FilteringMin, attr.FilteringMag, attr.FilteringMip)); break; } //GpuMaterialPass.TextureParameterValue textureValue = null; //int samplerIndex = -1; //foreach( Component_Image.BindSettingsAttribute attr in p.GetCustomAttributes( // typeof( Component_Image.BindSettingsAttribute ), true ) ) //{ // textureValue = new GpuMaterialPass.TextureParameterValue( texture, // attr.AddressingMode, attr.FilteringMin, attr.FilteringMag, attr.FilteringMip ); // samplerIndex = attr.SamplerIndex; // break; //} //if( samplerIndex != -1 ) // shader.Parameters.Set( samplerIndex.ToString(), textureValue ); } else { //!!!! //convert to float type value = SimpleTypes.ConvertDoubleToFloat(value); //ColorValuePowered: convert to ColorValue if (value is ColorValuePowered) { value = ((ColorValuePowered)value).ToColorValue(); } shader.Parameters.Set(p.Name, value); shader.Parameters.Set(ToLowerFirstCharacter(p.Name), value); } } } } OnSetShaderParameters(context, frameData, actualTexture, shader); SetShaderParametersEvent?.Invoke(this, context, shader); context.RenderQuadToCurrentViewport(shader); //change actual texture context.DynamicTexture_Free(actualTexture); actualTexture = outputTexture; }
///////////////////////////////////////// protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { base.OnRender(context, frameData, ref actualTexture); //blur var pipeline = (Component_RenderingPipeline_Basic)context.RenderingPipeline; var blurTexture = pipeline.GaussianBlur(context, this, actualTexture, BlurFactor, BlurDownscalingMode, BlurDownscalingValue); //context.RenderTarget_Free( brightTexture ); ////horizontal blur //var texture1 = context.RenderTarget2D_Alloc( actualTexture.Result.ResultSize, actualTexture.Result.Format ); //{ // context.SetViewport( texture1.Result.GetRenderTarget().Viewports[ 0 ] ); // CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); // shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; // shader.FragmentProgramFileName = @"Base\Shaders\Effects\Blur_fs.sc"; // //shader.FragmentProgramFileName = @"Base\Shaders\Effects\DepthOfField\Blur_fs.sc"; // shader.Parameters.Set( "0"/*"sourceTexture"*/, new GpuMaterialPass.TextureParameterValue( actualTexture, // TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.Point ) ); // BlurSetShaderParameters( context, shader, true ); // context.RenderQuadToCurrentViewport( shader ); //} ////vertical blur //var blurTexture = context.RenderTarget2D_Alloc( actualTexture.Result.ResultSize, texture1.Result.Format ); //{ // context.SetViewport( blurTexture.Result.GetRenderTarget().Viewports[ 0 ] ); // CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); // shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; // shader.FragmentProgramFileName = @"Base\Shaders\Effects\Blur_fs.sc"; // //shader.FragmentProgramFileName = @"Base\Shaders\Effects\DepthOfField\Blur_fs.sc"; // shader.Parameters.Set( "0"/*"sourceTexture"*/, new GpuMaterialPass.TextureParameterValue( texture1, // TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point ) ); // BlurSetShaderParameters( context, shader, false ); // context.RenderQuadToCurrentViewport( shader ); //} ////free old texture //context.RenderTarget_Free( texture1 ); //create final var finalTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, blurTexture.Result.ResultFormat); { context.SetViewport(finalTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\DepthOfField\Final_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1 /*"blurTexture"*/, blurTexture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point)); context.objectsDuringUpdate.namedTextures.TryGetValue("depthTexture", out Component_Image depthTexture); if (depthTexture == null) { //!!!! } shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(2 /*"depthTexture"*/, depthTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); Vector4 properties = new Vector4(FocalDistance, FocalSize, BackgroundTransitionLength, BlurForeground ? ForegroundTransitionLength : -1); shader.Parameters.Set("u_depthOfFieldProperties", properties.ToVector4F()); shader.Parameters.Set("intensity", (float)Intensity); context.RenderQuadToCurrentViewport(shader); } //free old textures context.DynamicTexture_Free(actualTexture); context.DynamicTexture_Free(blurTexture); //update actual texture actualTexture = finalTexture; }
public static void RenderFXAA(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture, double intensity) { //render luma //!!!!!A8R8G8B8 где еще. когда tonemapping //!!!!Specifically do FXAA after tonemapping. //!!!!? PixelFormat.Float16RGBA var lumaTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, PixelFormat.A8R8G8B8); { context.SetViewport(lumaTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\FXAA\Luma_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); //!!!!одно и тоже ставится //var size = context.owner.SizeInPixels; //shader.Parameters.Set( "viewportSize", new Vec4( size.X, size.Y, 1.0 / (double)size.X, 1.0 / (double)size.Y ).ToVec4F() ); //Mat4F identity = Mat4F.Identity; //shader.Parameters.Set( "worldViewProjMatrix", ParameterType.Matrix4x4, 1, &identity, sizeof( Mat4F ) ); context.RenderQuadToCurrentViewport(shader); } //free old texture context.DynamicTexture_Free(actualTexture); //render final { actualTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, PixelFormat.A8R8G8B8); context.SetViewport(actualTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\FXAA\Final_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, lumaTexture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point)); var size = actualTexture.Result.ResultSize; shader.Parameters.Set("viewportSize", new Vector4(size.X, size.Y, 1.0 / (double)size.X, 1.0 / (double)size.Y).ToVector4F()); shader.Parameters.Set("intensity", (float)intensity); // Intensity ); //Mat4F identity = Mat4F.Identity; //shader.Parameters.Set( "worldViewProjMatrix", ParameterType.Matrix4x4, 1, &identity, sizeof( Mat4F ) ); context.RenderQuadToCurrentViewport(shader); } //free luma texture context.DynamicTexture_Free(lumaTexture); }
public virtual Component_Image GaussianBlur(ViewportRenderingContext context, Component_RenderingEffect forEffect, Component_Image sourceTexture, double blurFactor, DownscalingModeEnum downscalingMode, int downscalingValue) { Component_Image currentTexture = sourceTexture; //downscaling int downscalingValue2; if (downscalingMode == DownscalingModeEnum.Auto) { downscalingValue2 = (int)(blurFactor / 2.0 + 0.999); if (blurFactor <= 1) { downscalingValue2 = 0; } //downscalingValue2 = (int)( Math.Pow( blurFactor, 1.0 / 1.1 ) ) - 1; //downscalingValue2 = (int)( Math.Sqrt( blurFactor ) ) - 1; //downscalingValue2 = (int)( blurFactor / 1.5 ); } else { downscalingValue2 = downscalingValue; } for (int n = 0; n < downscalingValue2; n++) { var size = currentTexture.Result.ResultSize / 2; if (size.X < 1) { size.X = 1; } if (size.Y < 1) { size.Y = 1; } var texture = context.RenderTarget2D_Alloc(size, currentTexture.Result.ResultFormat); { context.SetViewport(texture.Result.GetRenderTarget().Viewports[0]); var shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\Downscale2_fs.sc"; shader.Parameters.Set("sourceSizeInv", new Vector2F(1, 1) / currentTexture.Result.ResultSize.ToVector2F()); //shader.Parameters.Set( "0"/*"sourceTexture"*/, new GpuMaterialPass.TextureParameterValue( actualTexture, // TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.Point ) ); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, currentTexture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point)); context.RenderQuadToCurrentViewport(shader); } if (currentTexture != sourceTexture) { context.DynamicTexture_Free(currentTexture); } currentTexture = texture; } //horizontal blur //if( dimensions == BlurDimensionsEnum.HorizontalAndVertical || dimensions == BlurDimensionsEnum.Horizontal ) { var texture = context.RenderTarget2D_Alloc(sourceTexture.Result.ResultSize, currentTexture.Result.ResultFormat); //var texture = context.RenderTarget2D_Alloc( currentTexture.Result.Size, currentTexture.Result.Format ); { context.SetViewport(texture.Result.GetRenderTarget().Viewports[0]); var shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\Blur_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, currentTexture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point)); var values = GaussianBlurMath.Calculate15(texture.Result.ResultSize, true, blurFactor); shader.Parameters.Set("sampleOffsets", values.SampleOffsetsAsVec4Array); shader.Parameters.Set("sampleWeights", values.SampleWeights); context.RenderQuadToCurrentViewport(shader); } if (currentTexture != sourceTexture) { context.DynamicTexture_Free(currentTexture); } currentTexture = texture; } //vertical blur //if( dimensions == BlurDimensionsEnum.HorizontalAndVertical || dimensions == BlurDimensionsEnum.Vertical ) { var texture = context.RenderTarget2D_Alloc(sourceTexture.Result.ResultSize, currentTexture.Result.ResultFormat); //var texture = context.RenderTarget2D_Alloc( currentTexture.Result.Size, currentTexture.Result.Format ); { context.SetViewport(texture.Result.GetRenderTarget().Viewports[0]); var shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\Blur_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, currentTexture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point)); var values = GaussianBlurMath.Calculate15(texture.Result.ResultSize, false, blurFactor); shader.Parameters.Set("sampleOffsets", values.SampleOffsetsAsVec4Array); shader.Parameters.Set("sampleWeights", values.SampleWeights); context.RenderQuadToCurrentViewport(shader); } if (currentTexture != sourceTexture) { context.DynamicTexture_Free(currentTexture); } currentTexture = texture; } return(currentTexture); }
unsafe protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { base.OnRender(context, frameData, ref actualTexture); //is not supported if (!context.RenderingPipeline.GetUseMultiRenderTargets()) { return; } //downscale for SSAA var actualTextureSource = actualTexture; if (actualTexture.Result.ResultSize != context.Owner.SizeInPixels) { actualTexture = context.RenderTarget2D_Alloc(context.Owner.SizeInPixels, actualTextureSource.Result.ResultFormat); //copy to scene texture with downscale context.SetViewport(actualTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\Downscale2_fs.sc"; shader.Parameters.Set("sourceSizeInv", new Vector2F(1, 1) / actualTextureSource.Result.ResultSize.ToVector2F()); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTextureSource, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.None)); context.RenderQuadToCurrentViewport(shader); } // Setup Constants: Vector2F viewportPixelSize = new Vector2F(1.0f / (float)actualTexture.Result.ResultSize.X, 1.0f / (float)actualTexture.Result.ResultSize.Y); Vector2F halfViewportPixelSize = new Vector2F(1.0f / ((float)actualTexture.Result.ResultSize.X * 0.5f), 1.0f / ((float)actualTexture.Result.ResultSize.Y * 0.5f)); Vector4F quarterViewportPixelSize = new Vector4F(1.0f / ((float)actualTexture.Result.ResultSize.X * 0.25f), 1.0f / ((float)actualTexture.Result.ResultSize.Y * 0.25f), (float)((actualTexture.Result.ResultSize.X / 4) * (actualTexture.Result.ResultSize.Y / 4)), 0.0f); Vector2F viewport2xPixelSize = new Vector2F(viewportPixelSize.X * 2.0f, viewportPixelSize.Y * 2.0f); Matrix4F projectionMatrix = context.Owner.CameraSettings.ProjectionMatrix.ToMatrix4F(); float depthLinearizeMul = -projectionMatrix[3][2]; float depthLinearizeAdd = projectionMatrix[2][2]; if (depthLinearizeMul * depthLinearizeAdd < 0.0f) { depthLinearizeAdd = -depthLinearizeAdd; } Vector2F depthUnpackConsts = new Vector2F(depthLinearizeMul, depthLinearizeAdd); float tanHalfFOVY = 1.0f / projectionMatrix[1][1]; float tanHalfFOVX = 1.0F / projectionMatrix[0][0]; Vector2F cameraTanHalfFOV = new Vector2F(tanHalfFOVX, tanHalfFOVY); Vector2F NDCToViewMul = new Vector2F(cameraTanHalfFOV.X * 2.0f, cameraTanHalfFOV.Y * -2.0f); Vector2F NDCToViewAdd = new Vector2F(cameraTanHalfFOV.X * -1.0f, cameraTanHalfFOV.Y * 1.0f); Matrix4F itViewMatrix = (context.Owner.CameraSettings.ViewMatrix.GetInverse().ToMatrix4F()).GetTranspose(); // Effect Params: float effectSamplingRadiusNearLimit = (float)Radius * 1.2f; if (Quality.Value == QualityEnum.Low) { effectSamplingRadiusNearLimit *= 1.50f; } effectSamplingRadiusNearLimit /= tanHalfFOVY; float effectSamplingRadiusNearLimitRec = 1.0f / effectSamplingRadiusNearLimit; Vector4F effectRadiusParams = new Vector4F((float)Radius, -1.0f / (float)Radius, effectSamplingRadiusNearLimitRec, 0.0f); float effectFadeOutMul = -1.0f / ((float)FadeOutTo - (float)FadeOutFrom); float effectFadeOutAdd = (float)FadeOutFrom / ((float)FadeOutTo - (float)FadeOutFrom) + 1.0f; float detailAOStrength = (float)DetailStrength; float effectShadowStrength = (float)Multiplier; float effectShadowClamp = 1.0f; float effectShadowPow = (float)Power; float invSharpness = 1.0f - (float)Sharpness; if (invSharpness < 0.0f) { invSharpness = 0.0f; } if (invSharpness > 1.0f) { invSharpness = 1.0f; } // First Pass: Prepare 4 Depth half-Buffers: Component_Image[] halfDepths = new Component_Image[4]; for (int i = 0; i < 4; i++) { halfDepths[i] = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize / 2, PixelFormat.Float16R); } var fourDepthsMRT = context.MultiRenderTarget_Create(new[] { new MultiRenderTarget.Item(halfDepths[0]), new MultiRenderTarget.Item(halfDepths[1]), new MultiRenderTarget.Item(halfDepths[2]), new MultiRenderTarget.Item(halfDepths[3]) }); { context.SetViewport(fourDepthsMRT.Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\PrepareDepths_fs.sc"; context.objectsDuringUpdate.namedTextures.TryGetValue("depthTexture", out Component_Image depthTexture); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, depthTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("depthUnpackConsts", depthUnpackConsts); //!!!!use actualTextureSource size? shader.Parameters.Set("viewportPixelSize", viewportPixelSize); context.RenderQuadToCurrentViewport(shader); } // Second Pass: prepare 4 Mip-Maps for each Half-Depth-Map: Component_Image[,] depthMipMaps = null; if (Quality.Value > QualityEnum.Medium) { Vector2I[] mipMapSizes = new Vector2I[4]; // Setup Mip-Map sizes: mipMapSizes[0] = new Vector2I(actualTexture.Result.ResultSize / 2); for (int m = 1; m < 4; m++) { mipMapSizes[m] = new Vector2I(mipMapSizes[m - 1] / 2); } // Prepare MipMaps textures: depthMipMaps = new Component_Image[4, 4]; for (int d = 0; d < 4; d++) { depthMipMaps[d, 0] = halfDepths[d]; // MipMaps 0 is original halfDepthMaps for (int m = 1; m < 4; m++) { depthMipMaps[d, m] = context.RenderTarget2D_Alloc(mipMapSizes[m], PixelFormat.Float16R); } } for (int m = 1; m < 4; m++) // Mip-Maps loop { var fourDepthsMipsMRT = context.MultiRenderTarget_Create(new[] { new MultiRenderTarget.Item(depthMipMaps[0, m]), new MultiRenderTarget.Item(depthMipMaps[1, m]), new MultiRenderTarget.Item(depthMipMaps[2, m]), new MultiRenderTarget.Item(depthMipMaps[3, m]) }); context.SetViewport(fourDepthsMipsMRT.Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\PrepareDepthMips_fs.sc"; // For current Mip-Map generation using previous Mip-Map: float prevMipLevel = (float)(m - 1); var prevMipSize = mipMapSizes[m - 1]; Vector4F prevMipParams = new Vector4F(1.0f / (float)prevMipSize.X, 1.0f / (float)prevMipSize.Y, prevMipLevel, 0.0f); for (int i = 0; i < 4; i++) { // previous MipMap as input: shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(i, depthMipMaps[i, m - 1], TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); } shader.Parameters.Set("prevMipParams", prevMipParams); shader.Parameters.Set("effectRadiusParams", effectRadiusParams); context.RenderQuadToCurrentViewport(shader); } } Component_Image SSAOTextureArray = new Component_Image(); Component_Image SSAOBaseTextureArray = null; if (Quality.Value == QualityEnum.HighestAdaptive) { SSAOBaseTextureArray = new Component_Image(); SSAOBaseTextureArray = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize / 2, PixelFormat.R8G8_UInt, 0, false, /*0,*/ 4); } Component_Image importanceMap = null; Component_Image averageImportance = null; // Generate Importance Map for Highest/Adaptive Quality mode: if (Quality.Value == QualityEnum.HighestAdaptive) { // 4 SSAO passes: for (int pass = 0; pass < 4; pass++) { Vector4F[] patternRotScaleMatrices; GeneratePatternRotScaleMatrices(pass, out patternRotScaleMatrices); Vector2F perPassFullResCoordOffset = new Vector2F((float)(pass % 2), (float)(pass / 2)); { context.SetViewport(SSAOBaseTextureArray.Result.GetRenderTarget(0, pass).Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\GenerateSSAO_AdaptiveBase_fs.sc"; context.objectsDuringUpdate.namedTextures.TryGetValue("normalTexture", out Component_Image normalTexture); for (int m = 0; m < 4; m++) { shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(m, depthMipMaps[pass, m], TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); } shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(4, halfDepths[pass], TextureAddressingMode.Mirror, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(5, normalTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("NDCToViewMul", NDCToViewMul); shader.Parameters.Set("NDCToViewAdd", NDCToViewAdd); shader.Parameters.Set("effectRadiusParams", effectRadiusParams); shader.Parameters.Set("viewportPixelSize", viewportPixelSize); shader.Parameters.Set("viewport2xPixelSize", viewport2xPixelSize); shader.Parameters.Set("halfViewportPixelSize", halfViewportPixelSize); shader.Parameters.Set("perPassFullResCoordOffset", perPassFullResCoordOffset); shader.Parameters.Set("patternRotScaleMatrices", patternRotScaleMatrices); shader.Parameters.Set("effectFadeOutMul", effectFadeOutMul); shader.Parameters.Set("effectFadeOutAdd", effectFadeOutAdd); shader.Parameters.Set("effectShadowStrength", effectShadowStrength); shader.Parameters.Set("effectShadowClamp", effectShadowClamp); shader.Parameters.Set("effectShadowPow", effectShadowPow); shader.Parameters.Set("detailAOStrength", detailAOStrength); shader.Parameters.Set("itViewMatrix", itViewMatrix); context.RenderQuadToCurrentViewport(shader); } } // Importance Map Generation: importanceMap = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize / 4, PixelFormat.L8); { context.SetViewport(importanceMap.Result.GetRenderTarget().Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\GenerateImportanceMap_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, SSAOBaseTextureArray, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("halfViewportPixelSize", halfViewportPixelSize); shader.Parameters.Set("effectShadowStrength", effectShadowStrength); shader.Parameters.Set("effectShadowPow", effectShadowPow); context.RenderQuadToCurrentViewport(shader); } // Importance Map Post-Process A: var importanceMapPong = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize / 4, PixelFormat.L8); { context.SetViewport(importanceMapPong.Result.GetRenderTarget().Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\PostProcessImportanceMapA_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, importanceMap, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.None)); shader.Parameters.Set("quarterViewportPixelSize", quarterViewportPixelSize); context.RenderQuadToCurrentViewport(shader); } // Importance Map Post-Process B: { context.SetViewport(importanceMap.Result.GetRenderTarget().Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\PostProcessImportanceMapB_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, importanceMapPong, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.None)); shader.Parameters.Set("quarterViewportPixelSize", quarterViewportPixelSize); context.RenderQuadToCurrentViewport(shader); } context.DynamicTexture_Free(importanceMapPong); // Get Average Importance Pass: averageImportance = context.RenderTarget2D_Alloc(new Vector2I(1, 1), PixelFormat.L8); { context.SetViewport(averageImportance.Result.GetRenderTarget().Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\GetAverageImportance_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, importanceMap, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("quarterViewportPixelSize", quarterViewportPixelSize); context.RenderQuadToCurrentViewport(shader); } } // Third Pass: Generate 4 SSAO buffers: Component_Image blurPingTexture = null, blurPongTexture = null; if (BlurAmount.Value > 0) { blurPingTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize / 2, PixelFormat.R8G8_UInt); blurPongTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize / 2, PixelFormat.R8G8_UInt); } SSAOTextureArray = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize / 2, PixelFormat.R8G8_UInt, 0, false, /*0,*/ 4); for (int pass = 0; pass < 4; pass++) { Vector4F[] patternRotScaleMatrices; GeneratePatternRotScaleMatrices(pass, out patternRotScaleMatrices); Vector2F perPassFullResCoordOffset = new Vector2F((float)(pass % 2), (float)(pass / 2)); { if (BlurAmount.Value == 0) { context.SetViewport(SSAOTextureArray.Result.GetRenderTarget(0, pass).Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); } else { context.SetViewport(blurPingTexture.Result.GetRenderTarget().Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); } CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; if (Quality.Value == QualityEnum.Low) { shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\GenerateSSAO_LQ_fs.sc"; } else if (Quality.Value == QualityEnum.Medium) { shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\GenerateSSAO_MQ_fs.sc"; } else if (Quality.Value == QualityEnum.High) { shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\GenerateSSAO_HQ_fs.sc"; } else if (Quality.Value == QualityEnum.HighestAdaptive) { shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\GenerateSSAO_HAQ_fs.sc"; } context.objectsDuringUpdate.namedTextures.TryGetValue("normalTexture", out Component_Image normalTexture); if (Quality.Value > QualityEnum.Medium) { for (int m = 0; m < 4; m++) { shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(m, depthMipMaps[pass, m], TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); } shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(4, halfDepths[pass], TextureAddressingMode.Mirror, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(5, normalTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); if (Quality.Value == QualityEnum.HighestAdaptive) { shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(6, importanceMap, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(7, averageImportance, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(8, SSAOBaseTextureArray, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); } } else { shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, halfDepths[pass], TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1, halfDepths[pass], TextureAddressingMode.Mirror, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(2, normalTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); } shader.Parameters.Set("NDCToViewMul", NDCToViewMul); shader.Parameters.Set("NDCToViewAdd", NDCToViewAdd); shader.Parameters.Set("effectRadiusParams", effectRadiusParams); shader.Parameters.Set("viewportPixelSize", viewportPixelSize); shader.Parameters.Set("viewport2xPixelSize", viewport2xPixelSize); shader.Parameters.Set("halfViewportPixelSize", halfViewportPixelSize); shader.Parameters.Set("perPassFullResCoordOffset", perPassFullResCoordOffset); shader.Parameters.Set("patternRotScaleMatrices", patternRotScaleMatrices); shader.Parameters.Set("effectFadeOutMul", effectFadeOutMul); shader.Parameters.Set("effectFadeOutAdd", effectFadeOutAdd); shader.Parameters.Set("effectShadowStrength", effectShadowStrength); shader.Parameters.Set("effectShadowClamp", effectShadowClamp); shader.Parameters.Set("effectShadowPow", effectShadowPow); shader.Parameters.Set("detailAOStrength", detailAOStrength); shader.Parameters.Set("itViewMatrix", itViewMatrix); if (Quality.Value == QualityEnum.HighestAdaptive) { shader.Parameters.Set("adaptiveSampleCountLimit", (float)AdaptiveQualityLimit); shader.Parameters.Set("passNumber", (float)pass); } context.RenderQuadToCurrentViewport(shader); } if (Quality.Value > QualityEnum.Medium) { // Free Mip-Maps Targets for this Pass: for (int m = 0; m < 4; m++) { context.DynamicTexture_Free(depthMipMaps[pass, m]); } } else { context.DynamicTexture_Free(halfDepths[pass]); } // Blur SSAO Texture: if (BlurAmount.Value > 0) { int wideBlursRemaining = Math.Max(0, BlurAmount.Value - 2); for (int i = 0; i < BlurAmount.Value; i++) { if (i == (BlurAmount.Value - 1)) { context.SetViewport(SSAOTextureArray.Result.GetRenderTarget(0, pass).Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); } else { context.SetViewport(blurPongTexture.Result.GetRenderTarget().Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); } CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; if (Quality.Value > QualityEnum.Low) { if (wideBlursRemaining > 0) { shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\SmartBlurWide_fs.sc"; wideBlursRemaining--; } else { shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\SmartBlur_fs.sc"; } } else { shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\NonSmartBlur_fs.sc"; } if (Quality.Value > QualityEnum.Low) { shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, blurPingTexture, TextureAddressingMode.Mirror, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("invSharpness", invSharpness); } else { shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, blurPingTexture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.None)); } shader.Parameters.Set("halfViewportPixelSize", halfViewportPixelSize); context.RenderQuadToCurrentViewport(shader); // Swap Ping-Pong Blur textures: var tmp_tex = blurPingTexture; blurPingTexture = blurPongTexture; blurPongTexture = tmp_tex; } } } if (Quality.Value == QualityEnum.HighestAdaptive) { context.DynamicTexture_Free(SSAOBaseTextureArray); } if (Quality.Value == QualityEnum.HighestAdaptive) { context.DynamicTexture_Free(importanceMap); context.DynamicTexture_Free(averageImportance); } if (BlurAmount.Value > 0) { // Free Blur ping/pong Targets: context.DynamicTexture_Free(blurPingTexture); context.DynamicTexture_Free(blurPongTexture); } // 4th Pass: Apply 4 SSAO Textures to Final SSAO Result: var FullSSAOTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, PixelFormat.R8G8_UInt); { context.SetViewport(FullSSAOTexture.Result.GetRenderTarget().Viewports[0], Matrix4F.Identity, Matrix4F.Identity, FrameBufferTypes.All, ColorValue.Zero); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; if (Quality.Value > QualityEnum.Low) { shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\Apply_fs.sc"; } else { shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\Apply_noSmart_fs.sc"; } if (Quality.Value > QualityEnum.Low) { shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, SSAOTextureArray, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1, SSAOTextureArray, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.None)); } else { shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, SSAOTextureArray, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.None)); } if (Quality.Value > QualityEnum.Low) { shader.Parameters.Set("invSharpness", invSharpness); shader.Parameters.Set("halfViewportPixelSize", halfViewportPixelSize); } context.RenderQuadToCurrentViewport(shader); } // Free SSAO Texture Array Target: context.DynamicTexture_Free(SSAOTextureArray); // 5th Final Pass: var finalTexture = context.RenderTarget2D_Alloc(actualTextureSource.Result.ResultSize, actualTextureSource.Result.ResultFormat); { context.SetViewport(finalTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ASSAO\Final_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, actualTextureSource, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1, FullSSAOTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("intensity", (float)Intensity); shader.Parameters.Set("showAO", ShowAO ? 1.0f : -1.0f); context.RenderQuadToCurrentViewport(shader); } // Free Targets: context.DynamicTexture_Free(actualTexture); if (actualTextureSource != actualTexture) { context.DynamicTexture_Free(actualTextureSource); } context.DynamicTexture_Free(FullSSAOTexture); // Update actual Texture: actualTexture = finalTexture; }
///////////////////////////////////////// protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { base.OnRender(context, frameData, ref actualTexture); //blur var pipeline = (Component_RenderingPipeline_Basic)context.RenderingPipeline; var blurTexture = pipeline.GaussianBlur(context, this, actualTexture, BlurFactor, DownscalingMode, DownscalingValue); //create final var finalTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, actualTexture.Result.ResultFormat); { context.SetViewport(finalTexture.Result.GetRenderTarget().Viewports[0]); var shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Lerp_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"source1Texture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1 /*"source2Texture"*/, blurTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("intensity", (float)Intensity); context.RenderQuadToCurrentViewport(shader); } //free old textures context.DynamicTexture_Free(actualTexture); context.DynamicTexture_Free(blurTexture); //update actual texture actualTexture = finalTexture; ////////////// //int downscaling; //if( DownscalingMode.Value == DownscalingModeEnum.Auto ) // downscaling = (int)( BlurFactor / 1.5 ); //else // downscaling = Downscaling; ////var dimensions = BlurDimensions.Value; ////var downscaling = Math.Max( Downscaling.Value, 1 ); ////var size = context.Owner.SizeInPixels / downscaling; //List<Component_Texture> textureToFree = new List<Component_Texture>(); //Component_Texture currentTexture = actualTexture; ////Component_Texture textureDownscalingd = null; ////Component_Texture textureH = null; ////Component_Texture textureV = null; ////downscaling //for( int n = 0; n < downscaling; n++ ) //{ // var texture = context.RenderTarget2D_Alloc( currentTexture.Result.Size / 2, currentTexture.Result.Format ); // { // context.SetViewport( texture.Result.GetRenderTarget().Viewports[ 0 ] ); // var shader = new CanvasRenderer.ShaderItem(); // shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; // shader.FragmentProgramFileName = @"Base\Shaders\Effects\Downscale2_fs.sc"; // shader.Parameters.Set( "sourceSizeInv", new Vec2F( 1, 1 ) / currentTexture.Result.Size.ToVec2F() ); // //shader.Parameters.Set( "0"/*"sourceTexture"*/, new GpuMaterialPass.TextureParameterValue( actualTexture, // // TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.Point ) ); // shader.Parameters.Set( "0"/*"sourceTexture"*/, new GpuMaterialPass.TextureParameterValue( currentTexture, // TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point ) ); // context.RenderQuadToCurrentViewport( shader ); // } // currentTexture = texture; // textureToFree.Add( texture ); //} ////horizontal blur ////if( dimensions == BlurDimensionsEnum.HorizontalAndVertical || dimensions == BlurDimensionsEnum.Horizontal ) //{ // var texture = context.RenderTarget2D_Alloc( actualTexture.Result.Size, currentTexture.Result.Format ); // //var texture = context.RenderTarget2D_Alloc( currentTexture.Result.Size, currentTexture.Result.Format ); // { // context.SetViewport( texture.Result.GetRenderTarget().Viewports[ 0 ] ); // var shader = new CanvasRenderer.ShaderItem(); // shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; // shader.FragmentProgramFileName = @"Base\Shaders\Effects\Blur_fs.sc"; // shader.Parameters.Set( "0"/*"sourceTexture"*/, new GpuMaterialPass.TextureParameterValue( currentTexture, // TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point ) ); // SetShaderParameters( shader, texture.Result.Size, true ); // context.RenderQuadToCurrentViewport( shader ); // } // currentTexture = texture; // textureToFree.Add( texture ); //} ////vertical blur ////if( dimensions == BlurDimensionsEnum.HorizontalAndVertical || dimensions == BlurDimensionsEnum.Vertical ) //{ // var texture = context.RenderTarget2D_Alloc( actualTexture.Result.Size, currentTexture.Result.Format ); // //var texture = context.RenderTarget2D_Alloc( currentTexture.Result.Size, currentTexture.Result.Format ); // { // context.SetViewport( texture.Result.GetRenderTarget().Viewports[ 0 ] ); // var shader = new CanvasRenderer.ShaderItem(); // shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; // shader.FragmentProgramFileName = @"Base\Shaders\Effects\Blur_fs.sc"; // shader.Parameters.Set( "0"/*"sourceTexture"*/, new GpuMaterialPass.TextureParameterValue( currentTexture, // TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point ) ); // SetShaderParameters( shader, texture.Result.Size, false ); // context.RenderQuadToCurrentViewport( shader ); // } // currentTexture = texture; // textureToFree.Add( texture ); //} //////horizontal blur ////if( dimensions == BlurDimensionsEnum.HorizontalAndVertical || dimensions == BlurDimensionsEnum.Horizontal ) ////{ //// textureH = context.RenderTarget2D_Alloc( size, actualTexture.Result.Format ); //// { //// context.SetViewport( textureH.Result.GetRenderTarget().Viewports[ 0 ] ); //// var shader = new CanvasRenderer.ShaderItem(); //// shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; //// shader.FragmentProgramFileName = @"Base\Shaders\Effects\Blur_fs.sc"; //// shader.Parameters.Set( "0"/*"sourceTexture"*/, new GpuMaterialPass.TextureParameterValue( actualTexture, //// TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point ) ); //// SetShaderParameters( shader, size, true ); //// context.RenderQuadToCurrentViewport( shader ); //// } ////} //////vertical blur ////if( dimensions == BlurDimensionsEnum.HorizontalAndVertical || dimensions == BlurDimensionsEnum.Vertical ) ////{ //// textureV = context.RenderTarget2D_Alloc( size, actualTexture.Result.Format ); //// { //// context.SetViewport( textureV.Result.GetRenderTarget().Viewports[ 0 ] ); //// var shader = new CanvasRenderer.ShaderItem(); //// shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; //// shader.FragmentProgramFileName = @"Base\Shaders\Effects\Blur_fs.sc"; //// shader.Parameters.Set( "0"/*"sourceTexture"*/, new GpuMaterialPass.TextureParameterValue( textureH != null ? textureH : actualTexture, //// TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Point ) ); //// SetShaderParameters( shader, size, false ); //// context.RenderQuadToCurrentViewport( shader ); //// } ////} //////free old texture ////if( textureH != null && textureV != null ) ////{ //// context.RenderTarget_Free( textureH ); //// textureH = null; ////} ////create final //var finalTexture = context.RenderTarget2D_Alloc( actualTexture.Result.ResultSize, actualTexture.Result.Format ); //{ // context.SetViewport( finalTexture.Result.GetRenderTarget().Viewports[ 0 ] ); // var shader = new CanvasRenderer.ShaderItem(); // shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; // shader.FragmentProgramFileName = @"Base\Shaders\Lerp_fs.sc"; // shader.Parameters.Set( "0"/*"source1Texture"*/, new GpuMaterialPass.TextureParameterValue( actualTexture, // TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None ) ); // shader.Parameters.Set( "1"/*"source2Texture"*/, new GpuMaterialPass.TextureParameterValue( currentTexture, // TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None ) ); // shader.Parameters.Set( "intensity", (float)Intensity ); // context.RenderQuadToCurrentViewport( shader ); //} ////free old textures //context.RenderTarget_Free( actualTexture ); //foreach( var texture in textureToFree ) // context.RenderTarget_Free( texture ); ////if( textureH != null ) //// context.RenderTarget_Free( textureH ); ////if( textureV != null ) //// context.RenderTarget_Free( textureV ); ////update actual texture //actualTexture = finalTexture; }
//!!!! //!!!!need per camera context //double smoothIntensityFactorLastUpdate; //double? smoothIntensityFactor; ///////////////////////////////////////// protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { base.OnRender(context, frameData, ref actualTexture); var cameraSettings = context.Owner.CameraSettings; bool lightFound = false; Vector3 lightPosition = Vector3.Zero; if (OverrideDirection.Value != Vector3.Zero) { lightFound = true; lightPosition = cameraSettings.Position - OverrideDirection.Value.GetNormalize() * cameraSettings.FarClipDistance; } else { if (Light.ReferenceSpecified) { var light = Light.Value; if (light != null) { lightFound = true; if (light.Type.Value == Component_Light.TypeEnum.Directional) { lightPosition = cameraSettings.Position - light.TransformV.Rotation.GetForward() * cameraSettings.FarClipDistance; } else { lightPosition = light.TransformV.Position; } } } else { var frameData2 = frameData as Component_RenderingPipeline_Basic.FrameData; if (frameData2 != null) { foreach (var lightIndex in frameData2.LightsInFrustumSorted) { var item = frameData2.Lights[lightIndex]; if (item.data.Type == Component_Light.TypeEnum.Directional) { lightFound = true; lightPosition = cameraSettings.Position - item.data.Rotation.GetForward() * (float)cameraSettings.FarClipDistance; break; } } } } } if (!lightFound) { return; } if (!context.Owner.CameraSettings.ProjectToScreenCoordinates(lightPosition, out var screenLightPosition)) { return; } //calculate intensity factor by the sun position on the screen. double demandIntensityFactor = 0; { Degree angle = 0; if (lightPosition != cameraSettings.Position) { angle = MathAlgorithms.GetVectorsAngle(cameraSettings.Rotation.GetForward(), (lightPosition - cameraSettings.Position).GetNormalize()).InDegrees(); } else { angle = 10000; } var curve = new CurveLine(); curve.AddPoint(0, new Vector3(1, 0, 0)); curve.AddPoint(60, new Vector3(1, 0, 0)); curve.AddPoint(75, new Vector3(0, 0, 0)); curve.AddPoint(90, new Vector3(0, 0, 0)); var angleFactor = curve.CalculateValueByTime(angle).X; var curve2 = new CurveLine(); curve2.AddPoint(-0.2, new Vector3(0, 0, 0)); curve2.AddPoint(-0.1, new Vector3(1, 0, 0)); curve2.AddPoint(1.1, new Vector3(1, 0, 0)); curve2.AddPoint(1.2, new Vector3(0, 0, 0)); var heightFactor = curve2.CalculateValueByTime(screenLightPosition.Y).X; demandIntensityFactor = angleFactor * heightFactor; //const float screenFadingBorder = .1f; //double minDistance = 1; //for( int axis = 0; axis < 2; axis++ ) //{ // if( screenLightPosition[ axis ] < screenFadingBorder ) // { // var d = screenLightPosition[ axis ]; // if( d < minDistance ) // minDistance = d; // } // else if( screenLightPosition[ axis ] > 1.0f - screenFadingBorder ) // { // var d = 1.0f - screenLightPosition[ axis ]; // if( d < minDistance ) // minDistance = d; // } //} //needIntensityFactor = minDistance / screenFadingBorder; //MathEx.Saturate( ref needIntensityFactor ); ////clamp screenLightPosition //if( !new Rectangle( 0, 0, 1, 1 ).Contains( screenLightPosition ) ) //{ // if( MathAlgorithms.IntersectRectangleLine( new Rectangle( .0001f, .0001f, .9999f, .9999f ), // new Vector2( .5f, .5f ), screenLightPosition, out var intersectPoint1, out var intersectPoint2 ) != 0 ) // { // screenLightPosition = intersectPoint1; // } //} } var smoothIntensityFactor = demandIntensityFactor; ////update smooth intensity factor //if( smoothIntensityFactor == null ) // smoothIntensityFactor = needIntensityFactor; //if( smoothIntensityFactorLastUpdate != context.Owner.LastUpdateTime ) //{ // smoothIntensityFactorLastUpdate = context.Owner.LastUpdateTime; // const double smoothSpeed = 1; // var step = context.Owner.LastUpdateTimeStep * smoothSpeed; // if( needIntensityFactor > smoothIntensityFactor ) // { // smoothIntensityFactor += step; // if( smoothIntensityFactor > needIntensityFactor ) // smoothIntensityFactor = needIntensityFactor; // } // else // { // smoothIntensityFactor -= step; // if( smoothIntensityFactor < needIntensityFactor ) // smoothIntensityFactor = needIntensityFactor; // } //} //get result intensity var resultIntensity = Intensity.Value * smoothIntensityFactor; if (resultIntensity <= 0) { return; } double divisor = 9.0 - Resolution; if (divisor <= 1) { divisor = 1; } var sizeFloat = actualTexture.Result.ResultSize.ToVector2() / divisor; var size = new Vector2I((int)sizeFloat.X, (int)sizeFloat.Y); //scattering pass var scatteringTexture = context.RenderTarget2D_Alloc(size, PixelFormat.A8R8G8B8); //!!!! PixelFormat.R8G8B8 ); { context.SetViewport(scatteringTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\LightShafts\Scattering_fs.sc"; context.objectsDuringUpdate.namedTextures.TryGetValue("depthTexture", out var depthTexture); if (depthTexture == null) { //!!!! } shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"depthTexture"*/, depthTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set("screenLightPosition", screenLightPosition.ToVector2F()); shader.Parameters.Set("decay", (float)Decay); shader.Parameters.Set("density", (float)Density); context.RenderQuadToCurrentViewport(shader); } //blur pass var blurTexture = context.RenderTarget2D_Alloc(size, PixelFormat.A8R8G8B8); //!!!! PixelFormat.R8G8B8 ); { context.SetViewport(blurTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\LightShafts\Blur_fs.sc"; //!!!!Linear? shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"scatteringTexture"*/, scatteringTexture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.None)); shader.Parameters.Set("color", Color.Value.ToVector3F()); shader.Parameters.Set("screenLightPosition", screenLightPosition.ToVector2F()); shader.Parameters.Set("blurFactor", (float)BlurFactor); context.RenderQuadToCurrentViewport(shader); } //free old textures context.DynamicTexture_Free(scatteringTexture); //create final var finalTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, actualTexture.Result.ResultFormat); { context.SetViewport(finalTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\LightShafts\Final_fs.sc"; shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1 /*"blurTexture"*/, blurTexture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.None)); shader.Parameters.Set("intensity", (float)resultIntensity); context.RenderQuadToCurrentViewport(shader); } //free old textures context.DynamicTexture_Free(actualTexture); context.DynamicTexture_Free(blurTexture); //update actual texture actualTexture = finalTexture; }
//private void Pipeline_RenderBegin( Component_RenderingPipeline_Basic sender, ViewportRenderingContext context, Component_RenderingPipeline_Basic.FrameData frameData ) //{ // if( EnabledInHierarchy && sender.UseRenderTargets ) // { // //frameData.GenerateIBLSpecularTexture = true; // //frameData.DeferredSpecularIBLItensity = 1.0 - Intensity; // } //} private void Pipeline_RenderDeferredShadingEnd(Component_RenderingPipeline_Basic sender, ViewportRenderingContext context, Component_RenderingPipeline_Basic.FrameData frameData, ref Component_Image sceneTexture) { if (EnabledInHierarchy && sender.GetUseMultiRenderTargets()) { var actualTexture = sceneTexture; var pipeline = (Component_RenderingPipeline_Basic)context.RenderingPipeline; Vector3 cameraPos = context.Owner.CameraSettings.Position; //!!!!double Vector3F cameraPosition = cameraPos.ToVector3F(); //!!!!double Matrix4F projectionMatrix = context.Owner.CameraSettings.ProjectionMatrix.ToMatrix4F(); Matrix4F viewMatrix = context.Owner.CameraSettings.ViewMatrix.ToMatrix4F(); Matrix4F viewProjMatrix = projectionMatrix * viewMatrix; Matrix4F invViewProjMatrix = viewProjMatrix.GetInverse(); float aspectRatio = (float)context.Owner.CameraSettings.AspectRatio; float fov = (float)context.Owner.CameraSettings.FieldOfView; float zNear = (float)context.Owner.CameraSettings.NearClipDistance; float zFar = (float)context.Owner.CameraSettings.FarClipDistance; var ambientLight = frameData.Lights[frameData.LightsInFrustumSorted[0]]; var ambientLightPower = ambientLight.data.Power; var reflectionTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, actualTexture.Result.ResultFormat); { context.SetViewport(reflectionTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ScreenSpaceReflection\SSR_fs.sc"; int maxSteps = 50; switch (Quality.Value) { case QualityEnum.Lowest: maxSteps = 20; break; case QualityEnum.Low: maxSteps = 50; break; case QualityEnum.Medium: maxSteps = 80; break; case QualityEnum.High: maxSteps = 120; break; case QualityEnum.Highest: maxSteps = 160; break; } shader.Defines.Add(new CanvasRenderer.ShaderItem.DefineItem("MAX_STEPS", maxSteps.ToString())); context.objectsDuringUpdate.namedTextures.TryGetValue("gBuffer0Texture", out var gBuffer0Texture); context.objectsDuringUpdate.namedTextures.TryGetValue("depthTexture", out var depthTexture); context.objectsDuringUpdate.namedTextures.TryGetValue("normalTexture", out var normalTexture); context.objectsDuringUpdate.namedTextures.TryGetValue("gBuffer2Texture", out var gBuffer2Texture); //!!!!reflection probes? pipeline.GetBackgroundEnvironmentTextures(context, frameData, /*true, */ out var environmentTexture, out var environmentTextureIBL); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, depthTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(2, normalTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); //!!!!rotation, multiplier shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(3, environmentTexture.Value.texture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Linear)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(4, environmentTextureIBL.Value.texture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Linear)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(5, Component_RenderingPipeline_Basic.BrdfLUT, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Linear)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(6, gBuffer2Texture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.Point)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(7, gBuffer0Texture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.Point)); //!!!часть параметров есть UniformsGeneral.sh shader.Parameters.Set("viewProj", viewProjMatrix); shader.Parameters.Set("invViewProj", invViewProjMatrix); shader.Parameters.Set("cameraPosition", cameraPosition); shader.Parameters.Set("edgeFactorPower", (float)EdgeFactorPower); shader.Parameters.Set("colorTextureSize", new Vector4F((float)actualTexture.Result.ResultSize.X, (float)actualTexture.Result.ResultSize.Y, 0.0f, 0.0f)); shader.Parameters.Set("zNear", zNear); shader.Parameters.Set("zFar", zFar); shader.Parameters.Set("fov", fov); shader.Parameters.Set("aspectRatio", aspectRatio); context.RenderQuadToCurrentViewport(shader); } var min = BlurRoughnessMin.Value; var max = BlurRoughnessMax.Value; if (min > max) { min = max; } var blurRoughnessMin = pipeline.GaussianBlur(context, this, reflectionTexture, min, BlurDownscalingMode, BlurDownscalingValue); var blurRoughnessMax = pipeline.GaussianBlur(context, this, reflectionTexture, max, BlurDownscalingMode, BlurDownscalingValue); //// Blur Pass: //var bluredReflection = pipeline.GaussianBlur( context, this, reflectionTexture, BlurFactorOnMaxRoughness, BlurDownscalingMode, BlurDownscalingValue ); // Final Pass: var finalTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, actualTexture.Result.ResultFormat); { context.SetViewport(finalTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.VertexProgramFileName = @"Base\Shaders\EffectsCommon_vs.sc"; shader.FragmentProgramFileName = @"Base\Shaders\Effects\ScreenSpaceReflection\SSR_Apply_fs.sc"; context.objectsDuringUpdate.namedTextures.TryGetValue("gBuffer0Texture", out var gBuffer0Texture); context.objectsDuringUpdate.namedTextures.TryGetValue("depthTexture", out var depthTexture); context.objectsDuringUpdate.namedTextures.TryGetValue("normalTexture", out var normalTexture); context.objectsDuringUpdate.namedTextures.TryGetValue("gBuffer2Texture", out var gBuffer2Texture); //!!!!reflection probes? pipeline.GetBackgroundEnvironmentTextures(context, frameData, /*true, */ out var environmentTexture, out var environmentTextureIBL); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(1, blurRoughnessMin, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(2, blurRoughnessMax, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); //shader.Parameters.Set( "1", new GpuMaterialPass.TextureParameterValue( reflectionTexture, // TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None ) ); //shader.Parameters.Set( "2", new GpuMaterialPass.TextureParameterValue( bluredReflection, // TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None ) ); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(3, depthTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(4, normalTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.None)); //!!!!rotation, multiplier shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(5, environmentTexture.Value.texture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Linear)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(6, environmentTextureIBL.Value.texture, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Linear)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(7, Component_RenderingPipeline_Basic.BrdfLUT, TextureAddressingMode.Clamp, FilterOption.Linear, FilterOption.Linear, FilterOption.Linear)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(8, gBuffer2Texture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.Point)); shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(9, gBuffer0Texture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.Point)); shader.Parameters.Set("invViewProj", invViewProjMatrix); shader.Parameters.Set("cameraPosition", cameraPosition); shader.Parameters.Set("intensity", (float)Intensity); shader.Parameters.Set("ambientLightPower", ambientLightPower); context.RenderQuadToCurrentViewport(shader); } // Free Targets: context.DynamicTexture_Free(reflectionTexture); context.DynamicTexture_Free(blurRoughnessMin); context.DynamicTexture_Free(blurRoughnessMax); //context.RenderTarget_Free( bluredReflection ); context.DynamicTexture_Free(actualTexture); // Update actual Texture: actualTexture = finalTexture; //result sceneTexture = actualTexture; } }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// protected override void OnRender(ViewportRenderingContext context, Component_RenderingPipeline.IFrameData frameData, ref Component_Image actualTexture) { var result = Result as CompiledDataCodeGenerated; //!!!!так? if (result == null) { return; } if (result.vertexProgram == null || result.fragmentProgram == null) { return; } //!!!!валится если файла нет. или не указан. //!!!!!!как быстро проверять о том что файла нет? может из кеша шейдеров или типа того. как тогда обновлять //string shaderFile = Shader; //if( string.IsNullOrEmpty( shaderFile ) ) // return; //!!!!formats. т.е. эффект поддерживает и LDR и HDR. где еще //!!!!!можно по сути у эффекта указывать что он ожидает получить на входе. ну или это EffectsHDR, EffectsLDR var outputTexture = context.RenderTarget2D_Alloc(actualTexture.Result.ResultSize, actualTexture.Result.ResultFormat); context.SetViewport(outputTexture.Result.GetRenderTarget().Viewports[0]); CanvasRenderer.ShaderItem shader = new CanvasRenderer.ShaderItem(); shader.CompiledVertexProgram = result.vertexProgram; shader.CompiledFragmentProgram = result.fragmentProgram; //shader.VertexProgramFileName = shaderFile; //shader.VertexProgramFunctionName = "main_vp"; //shader.FragmentProgramFileName = shaderFile; //shader.FragmentProgramFunctionName = "main_fp"; //{ // var defines = new List<GuiRenderer.ShaderItem.DefineItem>(); // //defines.Add( new GuiRenderer.ShaderItem.DefineItem( "FRAGMENT_CODE_BODY", "color = float4(0,1,0,1);" ) ); // if( defines.Count != 0 ) // shader.Defines = defines.ToArray(); //} shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(0 /*"sourceTexture"*/, actualTexture, TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.Point)); //shader.Parameters.Set( "0"/*"sourceTexture"*/, new GpuMaterialPass.TextureParameterValue( actualTexture, // TextureAddressingMode.Clamp, FilterOption.Point, FilterOption.Point, FilterOption.Point ) ); unsafe { Vector4F value = Vector4F.One; if (!Color.ReferenceSpecified) { value = Color.Value.ToVector4F(); } shader.Parameters.Set("u_paramColor", ParameterType.Vector4, 1, &value, sizeof(Vector4F)); } //!!!!пока так if (result.fragmentGeneratedCode != null) { //!!!!copy code из MaterialStandard //var materialParameters = materialData.GetShaderParameters(); var code = result.fragmentGeneratedCode; //foreach( var code in codes ) //{ if (code.parameters != null) { foreach (var item in code.parameters) { //!!!!надо ссылки сконвертить или как-то так? чтобы работало, если это в типе var value = item.component.GetValue(); if (value != null) { //!!!! //convert to float type value = SimpleTypes.ConvertDoubleToFloat(value); //ColorValuePowered: convert to ColorValue if (value is ColorValuePowered) { value = ((ColorValuePowered)value).ToColorValue(); } shader.Parameters /*container*/.Set(item.nameInShader, value); } } } if (code.textures != null) { foreach (var item in code.textures) { //!!!!надо ссылки сконвертить или как-то так? чтобы работало, если это в типе //!!!!не только 2D Component_Image texture = item.component.Texture; if (texture == null) { texture = ResourceUtility.WhiteTexture2D; } //!!!!options shader.Parameters.Set(new ViewportRenderingContext.BindTextureData(item.textureRegister, texture, TextureAddressingMode.Wrap, FilterOption.Linear, FilterOption.Linear, FilterOption.Linear)); ////!!!! //var textureValue = new GpuMaterialPass.TextureParameterValue( texture, // TextureAddressingMode.Wrap, FilterOption.Linear, FilterOption.Linear, FilterOption.Linear ); ////!!!! //ParameterType parameterType = ParameterType.Texture2D; //shader.Parameters/*container*/.Set( item.textureRegister.ToString(), textureValue, parameterType ); ////shader.Parameters/*container*/.Set( item.nameInShader, textureValue, parameterType ); } } //} } var size = actualTexture.Result.ResultSize; shader.Parameters.Set("viewportSize", new Vector4(size.X, size.Y, 1.0 / (double)size.X, 1.0 / (double)size.Y).ToVector4F()); shader.Parameters.Set("intensity", (float)Intensity.Value); //!!!!auto parameters //if( result.fragmentGeneratedCode != null ) { //!!!!не так потом shader.Parameters.Set("param_viewportSize", new Vector2F(size.X, size.Y)); //foreach( var item in result.fragmentGeneratedCode.autoConstantParameters ) //{ //} } context.RenderQuadToCurrentViewport(shader); //change actual texture context.DynamicTexture_Free(actualTexture); actualTexture = outputTexture; }