<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;

using VVVV.PluginInterfaces.V2;
using VVVV.PluginInterfaces.V1;

using VVVV.DX11;
using SlimDXMatrix = SlimDX.Matrix;
using FeralTic.DX11.Resources;
using FeralTic.DX11;
using CraftLie;
using SharpDX;
using VVVV.Utils.VColor;
using VL.Lib.Collections;
using System.Diagnostics;
using VVVV.Utils.VMath;
using VVVV.Core.Logging;
using SlimDX.Direct3D11;
using SlimDX.DXGI;
using System.Runtime.InteropServices;

namespace VVVV.DX11.Nodes
{

    [PluginInfo(Name = "UploadTexture", Category = "DX11.Texture", Author = "vux, tonfilm")]
    public unsafe class UploadTextureDX11Node : IPluginEvaluate, IDX11ResourceHost, IDisposable, IPartImportsSatisfiedNotification
    {
        [Import()]
        protected ILogger logger;

        [Input("Data", DefaultValue = 0, AutoValidate = false)]
        protected ISpread<DynamicTextureDescription> FInData;

        [Input("Apply", IsBang = true, DefaultValue = 1)]
        protected ISpread<bool> FApply;

        [Config("Suppress Warning", DefaultValue = 0)]
        protected ISpread<bool> FSuppressWarning;

        [Output("Texture Out")]
        protected Pin<DX11Resource<DX11DynamicTexture2D>> FTextureOutput;

        [Output("Is Valid")]
        protected ISpread<bool> FValid;

        private bool FInvalidate;

        public void Evaluate(int SpreadMax)
        {
            if (this.FApply[0])
            {
                this.FInData.Sync();
                this.FInvalidate = true;
                this.FTextureOutput.Resize(FInData.SliceCount, () => new DX11Resource<DX11DynamicTexture2D>(), r => r?.Dispose());
                this.FValid.SliceCount = FInData.SliceCount;
            }        
        }

        public unsafe void Update(DX11RenderContext context)
        {           
            if (this.FTextureOutput.SliceCount == 0 || !this.FInvalidate)
            {
                return;
            }

            for (int i = 0; i < FTextureOutput.SliceCount; i++)
            {
                FValid[i] = false;
                SetupTexture(i, context, FTextureOutput[i], FInData[i]);
            }        
        }

        private void SetupTexture(int slice, DX11RenderContext context, DX11Resource<DX11DynamicTexture2D> texture, DynamicTextureDescription description)
        {
            try
            {
                if (!texture.Contains(context))
                {
                    var fmt = (SlimDX.DXGI.Format)description.Format;
                    Texture2DDescription desc;

                    if (texture.Contains(context))
                    {
                        desc = texture[context].Resource.Description;

                        if (desc.Width != description.Width || desc.Height != description.Height || desc.Format != fmt)
                        {
                            texture.Dispose(context);
                            texture[context] = new DX11DynamicTexture2D(context, description.Width, description.Height, fmt);
                        }
                    }
                    else
                    {
                        texture[context] = new DX11DynamicTexture2D(context, description.Width, description.Height, fmt);
#if DEBUG
                    texture[context].Resource.DebugName = "DynamicTexture";
#endif
                    }

                    desc = texture[context].Resource.Description;

                    int stride = GetPixelSizeInBytes(description.Format);
                    int dataLength = desc.Width * desc.Height * stride;

                    var t = texture[context];
                    if (description.DataType == TextureDescriptionDataType.IntPtr)
                    {
                        WriteToTexture(stride, dataLength, t, description.GetDataPointer());
                    }
                    else
                    {
                        var pinnedArray = GCHandle.Alloc(description.GetDataArray(), GCHandleType.Pinned);

                        try
                        {
                            WriteToTexture(stride, dataLength, t, pinnedArray.AddrOfPinnedObject());
                        }
                        finally
                        {
                            pinnedArray.Free();
                        }

                    }

                    this.FInvalidate = false;
                }
            }
            finally
            {
                FValid[slice] = true;<commit_msg>fixed texture changed detection<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;

using VVVV.PluginInterfaces.V2;
using VVVV.PluginInterfaces.V1;

using VVVV.DX11;
using SlimDXMatrix = SlimDX.Matrix;
using FeralTic.DX11.Resources;
using FeralTic.DX11;
using CraftLie;
using SharpDX;
using VVVV.Utils.VColor;
using VL.Lib.Collections;
using System.Diagnostics;
using VVVV.Utils.VMath;
using VVVV.Core.Logging;
using SlimDX.Direct3D11;
using SlimDX.DXGI;
using System.Runtime.InteropServices;

namespace VVVV.DX11.Nodes
{

    [PluginInfo(Name = "UploadTexture", Category = "DX11.Texture", Author = "vux, tonfilm")]
    public unsafe class UploadTextureDX11Node : IPluginEvaluate, IDX11ResourceHost, IDisposable, IPartImportsSatisfiedNotification
    {
        [Import()]
        protected ILogger logger;

        [Input("Data", DefaultValue = 0, AutoValidate = false)]
        protected ISpread<DynamicTextureDescription> FInData;

        [Input("Apply", IsBang = true, DefaultValue = 1)]
        protected ISpread<bool> FApply;

        [Config("Suppress Warning", DefaultValue = 0)]
        protected ISpread<bool> FSuppressWarning;

        [Output("Texture Out")]
        protected Pin<DX11Resource<DX11DynamicTexture2D>> FTextureOutput;

        [Output("Is Valid")]
        protected ISpread<bool> FValid;

        private bool FInvalidate;

        public void Evaluate(int SpreadMax)
        {
            if (this.FApply[0])
            {
                this.FInData.Sync();
                this.FInvalidate = true;
                this.FTextureOutput.Resize(FInData.SliceCount, () => new DX11Resource<DX11DynamicTexture2D>(), r => r?.Dispose());
                this.FValid.SliceCount = FInData.SliceCount;
            }        
        }

        public unsafe void Update(DX11RenderContext context)
        {           
            if (this.FTextureOutput.SliceCount == 0 || !this.FInvalidate)
            {
                return;
            }

            for (int i = 0; i < FTextureOutput.SliceCount; i++)
            {
                FValid[i] = false;
                SetupTexture(i, context, FTextureOutput[i], FInData[i]);
            }        
        }

        private void SetupTexture(int slice, DX11RenderContext context, DX11Resource<DX11DynamicTexture2D> texture, DynamicTextureDescription description)
        {
            try
            {
                var fmt = (SlimDX.DXGI.Format)description.Format;
                Texture2DDescription desc;

                if (texture.Contains(context))
                {
                    desc = texture[context].Resource.Description;

                    if (desc.Width != description.Width || desc.Height != description.Height || desc.Format != fmt)
                    {
                        texture.Dispose(context);
                        texture[context] = new DX11DynamicTexture2D(context, description.Width, description.Height, fmt);
                    }
                }
                else
                {
                    texture[context] = new DX11DynamicTexture2D(context, description.Width, description.Height, fmt);
#if DEBUG
                    texture[context].Resource.DebugName = "DynamicTexture";
#endif
                }

                desc = texture[context].Resource.Description;

                int stride = GetPixelSizeInBytes(description.Format);
                int dataLength = desc.Width * desc.Height * stride;

                var t = texture[context];
                if (description.DataType == TextureDescriptionDataType.IntPtr)
                {
                    WriteToTexture(stride, dataLength, t, description.GetDataPointer());
                }
                else
                {
                    var pinnedArray = GCHandle.Alloc(description.GetDataArray(), GCHandleType.Pinned);
                    try
                    {
                        WriteToTexture(stride, dataLength, t, pinnedArray.AddrOfPinnedObject());
                    }
                    finally
                    {
                        pinnedArray.Free();
                    }

                }

                this.FInvalidate = false;

            }
            catch (Exception)
            {
                FValid[slice] = false;
{'fraction_non_alphanumeric': 0.08928771733034212, 'fraction_numerical': 0.011441390914189569, 'mean_word_length': 1.6614925373134328, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1877613', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2214, 'n_words': 449}