DataStream GenerateMesh()
        {
            float offsetX = 1f / TesselationLevelsX;
            float offsetY = 1f / TesselationLevelsY;

            _numVertices = TesselationLevelsX * TesselationLevelsY * 6;
            var stream = new DataStream(DXUtil.SizeOf <Vector2>() * _numVertices, false, true);

            for (int i = 0; i < TesselationLevelsY; i++)
            {
                for (int j = TesselationLevelsX - 1; j >= 0; j--)
                {
                    float x = offsetX * j;
                    float y = offsetY * i;
                    stream.Write(new Vector2(x, y));
                    stream.Write(new Vector2(x, y + offsetY));
                    stream.Write(new Vector2(x + offsetX, y));
                    stream.Write(new Vector2(x + offsetX, y));
                    stream.Write(new Vector2(x, y + offsetY));
                    stream.Write(new Vector2(x + offsetX, y + offsetY));
                }
            }
            return(stream);
        }
        //----------------------------------------------------------------------
        #region ** CustomEffect

        /// <summary>
        /// Creates any resources used repeatedly during subsequent rendering calls.
        /// </summary>
        public void Initialize(D2D.EffectContext effectContext, D2D.TransformGraph transformGraph)
        {
            if (_shaderBuffer == null)
            {
                Assembly asm = typeof(WarpEffect).Assembly;
                using (Stream stream = asm.GetManifestResourceStream("BitmapSamples.Resources.WarpEffect.cso"))
                {
                    int length = (int)stream.Length;
                    _shaderBuffer = new byte[length];
                    stream.Read(_shaderBuffer, 0, length);
                }
            }
            effectContext.LoadVertexShader(GUID_WarpVertexShader, _shaderBuffer);

            _vertexBuffer = effectContext.FindVertexBuffer(GUID_WarpVertexBuffer);
            if (_vertexBuffer == null)
            {
                using (var meshStream = GenerateMesh())
                {
                    var props     = new D2D.VertexBufferProperties(1, D2D.VertexUsage.Static, meshStream);
                    var desc      = new D2D.InputElement("MESH_POSITION", 0, DXGI.Format.R32G32_Float, 0, 0);
                    var custProps = new D2D.CustomVertexBufferProperties(_shaderBuffer, new D2D.InputElement[] { desc }, DXUtil.SizeOf <Vector2>());
                    _vertexBuffer = D2D.VertexBuffer.Create(effectContext, GUID_WarpVertexShader, props, custProps);
                }
            }

            transformGraph.SetSingleTransformNode(this);
        }