{
  "Name": "GaussBlur",
  "_id": "61560c16-3643-426a-ade5-f8e33ef51e78",
  "Namespace": "lib.image.blur",
  "Description": "Loads an image.",
  "Inputs": [
    {
      "Name": "Code",
      "MetaInstanceID": "a729bd4c-ad8a-4e5e-8877-e2e6997af17e",
      "DefaultValue": {
        "Type": "Text",
        "Value": [
          "//>>> _common parameters\r",
          "float4x4 objectToWorldMatrix;",
          "float4x4 worldToCameraMatrix;",
          "float4x4 projMatrix;",
          "Texture2D txDiffuse;",
          "float2 RenderTargetSize;",
          "//<<< _common parameters\r",
          "\r",
          "//>>> _parameters\r",
          "Texture2D Image;",
          "float Glow;",
          "float Offset;",
          "float Size;",
          "float NumberOfSamples;",
          "//<<< _parameters\r",
          "float widthToHeight;\r",
          "float2 direction;\r",
          "\r",
          "//>>> _setup\r",
          "SamplerState samLinear",
          "{",
          "    Filter = MIN_MAG_MIP_LINEAR;",
          "    AddressU = Clamp;",
          "    AddressV = Clamp;",
          "};",
          "//<<< _setup\r",
          "\r",
          "//>>> _declarations\r",
          "struct VS_IN",
          "{",
          "    float4 pos : POSITION;",
          "    float2 texCoord : TEXCOORD;",
          "};",
          "",
          "struct PS_IN",
          "{",
          "    float4 pos : SV_POSITION;",
          "    float2 texCoord: TEXCOORD0;",
          "};",
          "//<<< _declarations\r",
          "\r",
          "//>>> _GS\r",
          "",
          "//<<< _GS\r",
          "\r",
          "//>>> _VS \r",
          "PS_IN VS( VS_IN input )",
          "{",
          "    PS_IN output = (PS_IN)0;",
          "",
          "    output.pos = mul(input.pos, worldToCameraMatrix);",
          "    output.pos = mul(output.pos, projMatrix);",
          "    output.texCoord = input.texCoord;",
          "",
          "    return output;",
          "}",
          "//<<< _VS\r",
          "\r",
          "//>>> PS\r",
          "static const int NUMWT = 10;\r",
          "static const float Gauss[NUMWT] = { 0.93, 0.86, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1 };\r",
          "\r",
          "float4 PS( PS_IN input ) : SV_Target\r",
          "{\r",
          "    float2 dir = direction;\r",
          "    dir *= 0.01*Size/NumberOfSamples;\r",
          "    dir.y *= widthToHeight;\r",
          "\r",
          "    float2 pos = dir;\r",
          "    float4 c = txDiffuse.Sample(samLinear, input.texCoord);\r",
          "    float totalWeight = 1;\r",
          "    for (int i = 0; i < NumberOfSamples; ++i)\r",
          "    {\r",
          "        float index = (float)i*(NUMWT - 1)/NumberOfSamples;\r",
          "        float weight = lerp(Gauss[(int)index], Gauss[(int)index + 1], frac(index));\r",
          "        c += txDiffuse.Sample(samLinear, input.texCoord + pos)*weight;\r",
          "        c += txDiffuse.Sample(samLinear, input.texCoord - pos)*weight;\r",
          "        pos += dir;\r",
          "        totalWeight += 2*weight;\r",
          "    }\r",
          "\r",
          "    //c.rgb = float3(Offset, Offset, Offset) + c.rgb/totalWeight*Glow;\r",
          "    //c.a = 1.0;\r",
          "    c = float4(Offset, Offset, Offset, Offset) + c/totalWeight*Glow;\r",
          "    c.a = clamp(c.a, 0,1);\r",
          "    return clamp(c,0,1000);\r",
          "}\r",
          "//<<< PS\r",
          "\r",
          "//>>> _technique\r",
          "technique10 Render",
          "{",
          "    pass P0",
          "    {",
          "        SetGeometryShader( 0 );",
          "        SetVertexShader( CompileShader( vs_4_0, VS() ) );",
          "        SetPixelShader( CompileShader( ps_4_0, PS() ) );",
          "    }",
          "}",
          "//<<< _technique\r",
          ""
        ]
      },
      "MetaID": "c522a66e-3260-4692-b3e3-79fd0361fa3d",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Image",
      "MetaInstanceID": "1944a8ac-41ce-4d96-9406-fe49d814d719",
      "DefaultValue": {
        "Type": "Image",
        "Value": "Framefield.Core.Image"
      },
      "MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab",
      "IsMultiInput": "False",
      "Relevance": "Required",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Glow",
      "MetaInstanceID": "1c992df9-496b-4d87-a43f-de5702aef102",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-10",
      "Max": "10",
      "Scale": "0.001",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Offset",
      "MetaInstanceID": "08a555a3-b28d-4830-aeb3-eaac33c50e69",
      "DefaultValue": {
        "Type": "Float",
        "Value": "0"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-10",
      "Max": "10",
      "Scale": "0.001",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Size",
      "MetaInstanceID": "a4d48e86-c687-4533-8d09-5fb5682a3456",
      "DefaultValue": {
        "Type": "Float",
        "Value": "5"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "100",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Number Of Samples",
      "MetaInstanceID": "9d6b3b96-87f8-4959-9786-3da31b481a0f",
      "DefaultValue": {
        "Type": "Float",
        "Value": "9"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "100",
      "Scale": "1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Text",
      "MetaInstanceID": "0c7e34bb-d09a-4fd0-b31e-5f64f8732554",
      "DefaultValue": {
        "Type": "Text",
        "Value": ""
      },
      "MetaID": "c522a66e-3260-4692-b3e3-79fd0361fa3d",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    }
  ],
  "Outputs": [
    {
      "Name": "Image",
      "MetaInstanceID": "cae7b4ea-ca40-4033-9819-904e99638eb8",
      "MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab"
    }
  ],
  "OperatorParts": [
    {
      "MetaInstanceID": "dad1c2ad-6540-4af8-90a6-eecf047b399a",
      "MetaID": "0a0769f5-4c10-4a95-90df-23a7e15b575f",
      "Name": "GaussBlurFunc",
      "Version": "64bf83a1-4e0c-4942-8bef-37d7eb6d8923",
      "Type": "Float",
      "IsMultiInput": "True",
      "Script": [
        "//>>> _using",
        "using System;",
        "using System.Collections.Generic;",
        "using System.Linq;",
        "using System.Text;",
        "using SharpDX;",
        "using SharpDX.Direct3D11;",
        "using SharpDX.Windows;",
        "//<<< _using",
        "using SharpDX.DXGI;",
        "",
        "namespace Framefield.Core.ID0a0769f5_4c10_4a95_90df_23a7e15b575f",
        "{",
        "    public class Class_GaussBlur : FXImageFunction",
        "    {",
        "        //>>> _inputids",
        "        private enum InputId",
        "        {",
        "            Code = 0,",
        "            Image = 1,",
        "            Glow = 2,",
        "            Offset = 3,",
        "            Size = 4,",
        "            NumberOfSamples = 5,",
        "            Text = 6",
        "        }",
        "        //<<< _inputids",
        "",
        "        protected override bool NeedsDepth { get { return false; } }",
        "",
        "        protected override bool BuildRenderTarget(SharpDX.DXGI.Format imageBufferFormat)",
        "        {",
        "            var pass1ResourceChanged = ResourceManager.ValidateRenderTargetResource(ref _pass1RenderTargetResource, OperatorPart, D3DDevice.Device,",
        "                                                                                    (int)_usedViewport.Width, (int)_usedViewport.Height);",
        "            if (pass1ResourceChanged)",
        "            {",
        "                Utilities.DisposeObj(ref _pass1RenderTargetView);",
        "                _pass1RenderTargetView = new RenderTargetView(D3DDevice.Device, _pass1RenderTargetResource.Texture);",
        "            }",
        "",
        "            return base.BuildRenderTarget(imageBufferFormat) || pass1ResourceChanged;",
        "        }",
        "",
        "        public override void Dispose()",
        "        {",
        "            ResourceManager.Dispose(_pass1RenderTargetResource);",
        "            Utilities.DisposeObj(ref _pass1RenderTargetView);",
        "            base.Dispose();",
        "        }",
        "",
        "        public override OperatorPartContext Eval(OperatorPartContext context, List<OperatorPart> inputs, int outputIdx)",
        "        {",
        "            return PrepareAndEvalOnChange(context, () =>",
        "            {",
        "                //>>> __params",
        "                var Image = inputs[(int)InputId.Image].Eval(context).Image;",
        "                    if (Image == null) return;",
        "                var Glow = inputs[(int)InputId.Glow].Eval(context).Value;",
        "                var Offset = inputs[(int)InputId.Offset].Eval(context).Value;",
        "                var Size = inputs[(int)InputId.Size].Eval(context).Value;",
        "                var NumberOfSamples = inputs[(int)InputId.NumberOfSamples].Eval(context).Value;",
        "                var Code = inputs[(int)InputId.Code].Eval(context).Text;",
        "                //<<< __params",
        "",
        "                var D3DDevice = context.D3DDevice;",
        "                var cameraProjection = Matrix.OrthoLH(1, 1, -100, 100);",
        "                var worldToCamera = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), new Vector3(0, 1, 0));",
        "",
        "                _effect.GetVariableByName(\"widthToHeight\").AsScalar().Set((float)_usedViewport.Width/_usedViewport.Height);",
        "                _effect.GetVariableByName(\"Size\").AsScalar().Set(Size);",
        "                _effect.GetVariableByName(\"Glow\").AsScalar().Set(Glow);",
        "                _effect.GetVariableByName(\"Offset\").AsScalar().Set(Offset);",
        "                _effect.GetVariableByName(\"NumberOfSamples\").AsScalar().Set(NumberOfSamples);",
        "",
        "                //pass 1 (horizontal)",
        "                D3DDevice.ImmediateContext.ClearRenderTargetView(_pass1RenderTargetView, new SharpDX.Color4(0, 0, 0, 1));",
        "",
        "                var pass1SubContext = new OperatorPartContext(context);",
        "                pass1SubContext.DepthStencilView = null;",
        "                pass1SubContext.RenderTargetView = _pass1RenderTargetView;",
        "                pass1SubContext.Effect = _effect;",
        "                pass1SubContext.Renderer = _renderer;",
        "                pass1SubContext.BlendState = OperatorPartContext.DefaultRenderer.DisabledBlendState;",
        "                pass1SubContext.InputLayout = context.Renderer.ScreenQuadInputLayout;",
        "                pass1SubContext.CameraProjection = cameraProjection;",
        "                pass1SubContext.WorldToCamera = worldToCamera;",
        "                pass1SubContext.ObjectTWorld = Matrix.Identity;",
        "                using (var textureView = new ShaderResourceView(D3DDevice, Image))",
        "                {",
        "                    pass1SubContext.Texture0 = textureView;",
        "                    pass1SubContext.Renderer.SetupEffect(pass1SubContext);",
        "",
        "                    _effect.GetVariableByName(\"direction\").AsVector().Set(new Vector2(1, 0));",
        "",
        "                    pass1SubContext.Renderer.Render(pass1SubContext.Renderer._screenQuadMesh, pass1SubContext);",
        "                }",
        "",
        "                //pass 2 (vertical)",
        "                D3DDevice.ImmediateContext.ClearRenderTargetView(_renderTargetView, new SharpDX.Color4(0, 0, 0, 1));",
        "",
        "                var pass2SubContext = new OperatorPartContext(context);",
        "                pass2SubContext.DepthStencilView = null;",
        "                pass2SubContext.RenderTargetView = _renderTargetView;",
        "                pass2SubContext.Effect = _effect;",
        "                pass2SubContext.Renderer = _renderer;",
        "                pass2SubContext.BlendState = OperatorPartContext.DefaultRenderer.DisabledBlendState;",
        "                pass2SubContext.InputLayout = context.Renderer.ScreenQuadInputLayout;",
        "                pass2SubContext.CameraProjection = cameraProjection;",
        "                pass2SubContext.WorldToCamera = worldToCamera;",
        "                pass2SubContext.ObjectTWorld = Matrix.Identity;",
        "                using (var textureView = new ShaderResourceView(D3DDevice, _pass1RenderTargetResource.Texture))",
        "                {",
        "                    pass2SubContext.Texture0 = textureView;",
        "                    pass2SubContext.Renderer.SetupEffect(pass2SubContext);",
        "",
        "                    _effect.GetVariableByName(\"direction\").AsVector().Set(new Vector2(0, 1));",
        "",
        "                    pass2SubContext.Renderer.Render(pass2SubContext.Renderer._screenQuadMesh, pass2SubContext);",
        "                }",
        "            });",
        "        }",
        "",
        "        Resource _pass1RenderTargetResource = null;",
        "        RenderTargetView _pass1RenderTargetView = null;",
        "    }",
        "}",
        "",
        "",
        ""
      ],
      "AdditionalAssemblies": []
    }
  ],
  "Operators": [],
  "Connections": [
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "dad1c2ad-6540-4af8-90a6-eecf047b399a",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "cae7b4ea-ca40-4033-9819-904e99638eb8"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "a729bd4c-ad8a-4e5e-8877-e2e6997af17e",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "dad1c2ad-6540-4af8-90a6-eecf047b399a"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "1944a8ac-41ce-4d96-9406-fe49d814d719",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "dad1c2ad-6540-4af8-90a6-eecf047b399a"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "1c992df9-496b-4d87-a43f-de5702aef102",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "dad1c2ad-6540-4af8-90a6-eecf047b399a"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "08a555a3-b28d-4830-aeb3-eaac33c50e69",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "dad1c2ad-6540-4af8-90a6-eecf047b399a"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "a4d48e86-c687-4533-8d09-5fb5682a3456",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "dad1c2ad-6540-4af8-90a6-eecf047b399a"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "9d6b3b96-87f8-4959-9786-3da31b481a0f",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "dad1c2ad-6540-4af8-90a6-eecf047b399a"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "0c7e34bb-d09a-4fd0-b31e-5f64f8732554",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "dad1c2ad-6540-4af8-90a6-eecf047b399a"
    }
  ]
}