private static void Render3D(IPluginViewSettings pluginViewSettings, viz.Texture texture, EventTypePluginData data, nui.Registration registration)
        {
            Debug.Assert(data != null);

            RawIrPlugin3DViewSettings rawPlugin3DViewSettings = pluginViewSettings as RawIrPlugin3DViewSettings;
            if (rawPlugin3DViewSettings != null)
            {
                RawIrPlugin.UpdateData(rawPlugin3DViewSettings, texture, data);

                bool doColor = false;
                bool doBodyIndex = false;

                if (texture != null)
                {
                    uint textureWidth = texture.GetWidth();
                    uint textureHeight = texture.GetHeight();

                    doColor = (textureWidth == nui.Constants.STREAM_COLOR_WIDTH) &&
                                (textureHeight == nui.Constants.STREAM_COLOR_HEIGHT);

                    doBodyIndex = (texture.GetTextureFormat() == viz.TextureFormat.B8G8R8A8_UNORM) &&
                                    (textureWidth == nui.Constants.STREAM_IR_WIDTH) &&
                                    (textureHeight == nui.Constants.STREAM_IR_HEIGHT);
                }

                if ((!rawPlugin3DViewSettings.IsSupplyingSurface && (rawPlugin3DViewSettings.ViewType == RawIrPlugin3DViewSettings.RawIr3DViewType.Ir)) ||
                    (rawPlugin3DViewSettings.IsSupplyingSurface && doBodyIndex) ||
                    (!rawPlugin3DViewSettings.IsSupplyingSurface && rawPlugin3DViewSettings.ViewType == RawIrPlugin3DViewSettings.RawIr3DViewType.SurfaceNormal))
                {
                    if (data.depthMap != null)
                    {
                        // special case for body index
                        viz.Effect effect = new viz.Effect()
                        {
                            Direction = new viz.Vector(0.5f, 0.3f, 1.5f, 0),
                            Ambient = new viz.Vector(0.0f, 0.0f, 0.0f, 1.0f),
                            Diffuse = new viz.Vector(0.5f, 0.5f, 0.5f, 1.0f),
                            Specular = new viz.Vector(1.0f, 1.0f, 1.0f, 1.0f),
                            Power = 25.0f,
                            EnableLighting = true,
                            EnableTexture = false,
                        };

                        if ((rawPlugin3DViewSettings.IsSupplyingSurface && doBodyIndex))
                        {
                            if (data.depthMap != null)
                            {
                                data.depthMap.SetMode(viz.DepthVertexMode.SurfaceWithNormal, viz.DepthRampMode.None);
                                effect.EnableTexture = true;
                            }
                        }

                        if (!rawPlugin3DViewSettings.IsSupplyingSurface && (rawPlugin3DViewSettings.ViewType != RawIrPlugin3DViewSettings.RawIr3DViewType.Ir))
                        {
                            texture = null;
                        }

                        data.depthMap.Render(effect, texture);
                    }
                }
                else
                {
                    if (rawPlugin3DViewSettings.IsSupplyingSurface && doColor)
                    {
                        // special case for color
                        if ((registration != null) && (data.depthMap != null) && (data.uvTable != null) && (data.sharedRawFrame != null))
                        {
                            data.depthMap.SetMode(viz.DepthVertexMode.SurfaceWithUV, viz.DepthRampMode.None);

                            IntPtr ptr = data.sharedRawFrame.Buffer;

                            if (ptr != IntPtr.Zero)
                            {
                                registration.Process(ptr, data.uvTable.Buffer);
                            }

                            data.depthMap.UpdateUVTable(data.uvTable.Buffer, data.uvTable.Size);
                        }
                    }

                    if (data.depthMap != null)
                    {
                        viz.Effect effect = new viz.Effect()
                            {
                                EnableTexture = texture != null,
                            };

                        data.depthMap.Render(effect, texture);
                    }
                }
            }
        }
        // object owning data should be locked
        private static void Render3D(IPluginViewSettings pluginViewSettings, EventTypePluginData data, nui.Registration registration)
        {
            Debug.Assert(data != null);

            BodyPlugin3DViewSettings bodyPluginViewSettings = pluginViewSettings as BodyPlugin3DViewSettings;

            if ((bodyPluginViewSettings != null) && (registration != null) && (data.body != null) && data.bodiesValid)
            {
                if (bodyPluginViewSettings.RenderBodies || bodyPluginViewSettings.RenderHands)
                {
                    data.body.Begin();

                    for (uint i = 0; i < BodyPlugin.bodyOptions.Length; ++i)
                    {
                        if (bodyPluginViewSettings.RenderBodies)
                        {
                            BodyOptions bodyOption = BodyPlugin.bodyOptions[i];

                            data.body.RenderBones(i, bodyOption.BoneEffect);

                            if (bodyPluginViewSettings.RenderJointOrientations)
                            {
                                data.body.RenderJointOrientations(i, bodyOption.JointEffect);
                            }

                            data.body.RenderJoints(i, bodyOption.JointEffect);

                            if (bodyPluginViewSettings.RenderInfo && (data.font != null))
                            {
                                // PlayderIndex (sic)
                                data.body.RenderInfo(i, viz.BodyInfoFlag.PlayderIndex, data.font, bodyOption.ColorVector);
                            }
                        }

                        if (bodyPluginViewSettings.RenderHands)
                        {
                            data.body.RenderHandStates(i);
                        }
                    }

                    data.body.End(0.0f);
                }
            }
        }
        unsafe private void UpdateChartSubFrame(nui.AUDIO_SUBFRAME* pSubFrame)
        {
            if (this.outChart != null)
            {
                IntPtr ptrOut = new IntPtr(pSubFrame) + cOutOffset;
                float* pFloats = (float*)ptrOut;
                float average = 0.0f;

                for (uint i = 0; i < nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME; ++i)
                {
                    average += Math.Abs(pFloats[i]);
                }

                average = average / nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME;
                this.outChart.Update(average, pSubFrame->TimeCounter);
            }

            if (this.micCharts != null)
            {
                float[] averages = new float[this.micCharts.Length];
                IntPtr ptrMic = new IntPtr(pSubFrame) + cMicOffset;
                float* pFloats = (float*)ptrMic;

#if OLD // less performant
                fixed (float* pAverages = &averages[0])
                {
                    AverageHelper.CalculateAbsAverageBy4((uint)nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME * 4, pFloats, pAverages);
                }
#endif // OLD
                uint total = nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME * 4;

                for (uint i = 0; i < total; i += 4)
                {
                    averages[0] += Math.Abs(pFloats[i]);
                    averages[1] += Math.Abs(pFloats[i + 1]);
                    averages[2] += Math.Abs(pFloats[i + 2]);
                    averages[3] += Math.Abs(pFloats[i + 3]);
                }

                for (int i = 0; i < 4; ++i)
                {
                    averages[i] /= nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME;
                    this.micCharts[i].Update(averages[i], pSubFrame->TimeCounter);
                }
            }

            if (this.speakerCharts != null)
            {
                float[] averages = new float[this.speakerCharts.Length];
                IntPtr ptrSpeaker = new IntPtr(pSubFrame) + cSpeakerOffset;
                float* pFloats = (float*)ptrSpeaker;
#if OLD 
                fixed (float* pAverages = &averages[0])
                {
                    AverageHelper.CalculateAbsAverageBy8((uint)nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME * 8, pFloats, pAverages);
                }
#endif // OLD
                uint total = nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME * 8;
                for (uint i = 0; i < total; i += 8)
                {
                    averages[0] += Math.Abs(pFloats[i]);
                    averages[1] += Math.Abs(pFloats[i + 1]);
                    averages[2] += Math.Abs(pFloats[i + 2]);
                    averages[3] += Math.Abs(pFloats[i + 3]);
                    averages[4] += Math.Abs(pFloats[i + 4]);
                    averages[5] += Math.Abs(pFloats[i + 5]);
                    averages[6] += Math.Abs(pFloats[i + 6]);
                    averages[7] += Math.Abs(pFloats[i + 7]);
                }

                for (int i = 0; i < this.speakerCharts.Length; ++i)
                {
                    averages[i] /= nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME;
                    this.speakerCharts[i].Update(averages[i], pSubFrame->TimeCounter);
                }
            }

            if (this.IsAudible && (this.audioPlayer != null))
            {
                int offset = 0;
                uint stride = 0;

                switch (this.AudibleTrack)
                {
                    case AudioTrack.Output:
                        stride = 1;
                        offset = AudioPlugin.cOutOffset;
                        break;

                    case AudioTrack.Mic0:
                    case AudioTrack.Mic1:
                    case AudioTrack.Mic2:
                    case AudioTrack.Mic3:
                        stride = (uint)this.micCharts.Length;
                        offset = AudioPlugin.cMicOffset + (sizeof(float) * (int)(this.AudibleTrack - AudioTrack.Mic0));
                        break;

                    case AudioTrack.SpeakerL:
                    case AudioTrack.SpeakerR:
                    case AudioTrack.SpeakerC:
                    case AudioTrack.SpeakerLFE:
                    case AudioTrack.SpeakerBL:
                    case AudioTrack.SpeakerBR:
                    case AudioTrack.SpeakerSL:
                    case AudioTrack.SpeakerSR:
                        stride = (uint)this.speakerCharts.Length;
                        offset = AudioPlugin.cSpeakerOffset + (sizeof(float) * (int)(this.AudibleTrack - AudioTrack.SpeakerL));
                        break;
                }

                if (stride != 0)
                {
                    IntPtr ptr = new IntPtr(pSubFrame) + offset;
                    float* pFloats = (float*)ptr;

                    this.audioPlayer.PlayAudio(nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME, pFloats, nui.Constants.AUDIO_SAMPLERATE, stride);
                }
            }
        }
        // object owning data should be locked
        private static void Render2D(IPluginViewSettings pluginViewSettings, viz.Texture texture, float left, float top, float width, float height, EventTypePluginData data, nui.Registration registration)
        {
            Debug.Assert(data != null);

            BodyPlugin2DViewSettings bodyPluginViewSettings = pluginViewSettings as BodyPlugin2DViewSettings;

            if ((bodyPluginViewSettings != null) && (registration != null) && (data.body != null) && data.bodiesValid)
            {
                if (bodyPluginViewSettings.RenderBodies || bodyPluginViewSettings.RenderHands)
                {
                    viz.Body2DMode bodyMode = viz.Body2DMode.DepthIR;
                    if ((texture != null) && (texture.GetWidth() == nui.Constants.STREAM_COLOR_WIDTH) && (texture.GetHeight() == nui.Constants.STREAM_COLOR_HEIGHT))
                    {
                        bodyMode = viz.Body2DMode.Color;
                    }

                    data.body.Begin2D(left, top, width, height, bodyMode, registration.GetCalibrationData());

                    for (uint i = 0; i < BodyPlugin.bodyOptions.Length; ++i)
                    {
                        if (bodyPluginViewSettings.RenderBodies)
                        {
                            viz.Vector color = BodyPlugin.bodyOptions[i].ColorVector;
                            color.A = 0.7f;
                            data.body.RenderBones2D(i, color);
                        }

                        if (bodyPluginViewSettings.RenderHands)
                        {
                            data.body.RenderHandStates2D(i);
                        }
                    }

                    data.body.End2D();
                }
            }
        }