<commit_before>﻿#region usings
using System;
using System.ComponentModel.Composition;
using System.Runtime.InteropServices;

using SlimDX;
using SlimDX.Direct3D9;
using VVVV.Core.Logging;
using VVVV.PluginInterfaces.V1;
using VVVV.PluginInterfaces.V2;
using VVVV.PluginInterfaces.V2.EX9;
using VVVV.Utils.VColor;
using VVVV.Utils.VMath;
using VVVV.Utils.SlimDX;
using VVVV.Utils;
using CraftLie;
using System.IO;

#endregion usings

namespace VVVV.Nodes
{
    #region PluginInfo
    [PluginInfo(Name = "UploadTexture", Category = "EX9.Texture", Help = "Copies the data from a VL DynamicTextureDescription to a texture", Author = "tonfilm")]
    #endregion PluginInfo
    public class UploadTextureDX9Node : IPluginEvaluate, IPartImportsSatisfiedNotification
    {
        public class TextureInfo
        {
            public DynamicTextureDescription Description;

            public TextureInfo(DynamicTextureDescription desc)
            {
                Description = desc;
            }
        }

        [Input("Data")]
        public ISpread<DynamicTextureDescription> FDataIn;

        [Output("Texture Out")]
        public ISpread<TextureResource<TextureInfo>> FTextureOut;

        [Import]
        public ILogger FLogger;

        public void OnImportsSatisfied()
        {
            //spreads have a length of one by default, change it
            //to zero so ResizeAndDispose works properly.
            FTextureOut.SliceCount = 0;
        }

        //called when data for any output pin is requested
        public void Evaluate(int spreadMax)
        {
            FTextureOut.ResizeAndDispose(spreadMax, CreateTextureResource);
            for (int i = 0; i < spreadMax; i++)
            {
                var textureResource = FTextureOut[i];
                var info = textureResource.Metadata.Description;
                var desc = FDataIn[i];

                //recreate textures if resolution or format was changed
                if (info == null || desc == null 
                    || (desc.Format == TextureDescriptionFormat.FromImage && desc.Set) 
                    || info.Width != desc.Width 
                    || info.Height != desc.Height 
                    || info.Format != desc.Format)
                {
                    textureResource?.Dispose();
                    textureResource = CreateTextureResource(i);
                }
                else
                {
                    textureResource.Metadata.Description = desc;
                    textureResource.NeedsUpdate = desc.Set;
                }

                FTextureOut[i] = textureResource;
            }
        }

        TextureResource<TextureInfo> CreateTextureResource(int slice)
        {
            var def = new DynamicTextureDescriptionArray<float>(new float[] { 1 }, 1, 1, TextureDescriptionFormat.R32_Float);
            var desciption = FDataIn[slice] ?? def;

            if (desciption.Format == TextureDescriptionFormat.FromImage)
            {
                return TextureResource.Create(new TextureInfo(desciption), CreateTextureFromImage);
            }
            else
            {
                return TextureResource.Create(new TextureInfo(desciption), CreateTexture, UpdateTexture);
            }
        }

        //this method gets called, when Reinitialize() was called in evaluate,
        //or a graphics device asks for its data
        Texture CreateTextureFromImage(TextureInfo info, Device device)
        {
            var description = info.Description;
            var pool = Pool.Managed;
            var usage = Usage.None;

            if (device is DeviceEx)
            {
                pool = Pool.Default;
                usage = Usage.Dynamic;
            }

            switch (description.DataType)
            {
                case TextureDescriptionDataType.IntPtr:
                    throw new NotImplementedException("Cannot create texture from image data in IntPtr");
                case TextureDescriptionDataType.Array:
                case TextureDescriptionDataType.Spread:
                    var bytes = (byte[])description.GetDataArray();
                    return Texture.FromMemory(device, bytes, description.Width, description.Height, 1, usage, Format.A8R8G8B8, pool, Filter.Default, Filter.Default, 0);
                case TextureDescriptionDataType.Stream:
                    var stream = description.GetDataStream();
                    stream.Position = 0;
                    return Texture.FromStream(device, stream, description.Width, description.Height, 1, usage, Format.A8R8G8B8, pool, Filter.Default, Filter.Default, 0);
            }

            throw new NotImplementedException("Could not create texture from image data");
        }

        //this method gets called, when Reinitialize() was called in evaluate,
        //or a graphics device asks for its data
        Texture CreateTexture(TextureInfo info, Device device)
        {
            var description = info.Description;
            var pool = Pool.Managed;
            var usage = Usage.None;

            if (device is DeviceEx)
            {
                pool = Pool.Default;
                usage = Usage.Dynamic;
            }

            return new Texture(device, Math.Max(description.Width, 1), Math.Max(description.Height, 1), 1, usage, GetDX9Format(description.Format), pool);
        }

        //this method gets called, when Update() was called in evaluate,
        //or a graphics device asks for its texture, here you fill the texture with the actual data
        //this is called for each renderer, careful here with multiscreen setups, in that case
        //calculate the pixels in evaluate and just copy the data to the device texture here
        unsafe void UpdateTexture(TextureInfo info, Texture texture)<commit_msg>fixes a wrong update case of UploadTextureDX9Node<commit_after>﻿#region usings
using System;
using System.ComponentModel.Composition;
using System.Runtime.InteropServices;

using SlimDX;
using SlimDX.Direct3D9;
using VVVV.Core.Logging;
using VVVV.PluginInterfaces.V1;
using VVVV.PluginInterfaces.V2;
using VVVV.PluginInterfaces.V2.EX9;
using VVVV.Utils.VColor;
using VVVV.Utils.VMath;
using VVVV.Utils.SlimDX;
using VVVV.Utils;
using CraftLie;
using System.IO;

#endregion usings

namespace VVVV.Nodes
{
    #region PluginInfo
    [PluginInfo(Name = "UploadTexture", Category = "EX9.Texture", Help = "Copies the data from a VL DynamicTextureDescription to a texture", Author = "tonfilm")]
    #endregion PluginInfo
    public class UploadTextureDX9Node : IPluginEvaluate, IPartImportsSatisfiedNotification
    {
        public class TextureInfo
        {
            public DynamicTextureDescription Description;

            public TextureInfo(DynamicTextureDescription desc)
            {
                Description = desc;
            }
        }

        [Input("Data")]
        public ISpread<DynamicTextureDescription> FDataIn;

        [Output("Texture Out")]
        public ISpread<TextureResource<TextureInfo>> FTextureOut;

        [Import]
        public ILogger FLogger;

        public void OnImportsSatisfied()
        {
            //spreads have a length of one by default, change it
            //to zero so ResizeAndDispose works properly.
            FTextureOut.SliceCount = 0;
        }

        //called when data for any output pin is requested
        public void Evaluate(int spreadMax)
        {
            FTextureOut.ResizeAndDispose(spreadMax, CreateTextureResource);
            for (int i = 0; i < spreadMax; i++)
            {
                var textureResource = FTextureOut[i];
                var info = textureResource.Metadata.Description;
                var desc = FDataIn[i];

                //recreate textures if resolution or format was changed
                if (info == null || desc == null 
                    || (desc.Format == TextureDescriptionFormat.FromImage && desc.Set) 
                    || info.Width != desc.Width 
                    || info.Height != desc.Height 
                    || info.Format != desc.Format)
                {
                    textureResource?.Dispose();
                    textureResource = CreateTextureResource(i);
                }
                else
                {
                    textureResource.Metadata.Description = desc;
                    textureResource.NeedsUpdate = desc.Set;
                }

                FTextureOut[i] = textureResource;
            }
        }

        TextureResource<TextureInfo> CreateTextureResource(int slice)
        {
            DynamicTextureDescription desciption = new DynamicTextureDescriptionArray<float>(new float[] { 1 }, 1, 1, TextureDescriptionFormat.R32_Float);
            var input = FDataIn[slice];

            if(input != null && input.Set)
            {
                desciption = input;
            }

            if (desciption.Format == TextureDescriptionFormat.FromImage)
            {
                return TextureResource.Create(new TextureInfo(desciption), CreateTextureFromImage);
            }
            else
            {
                return TextureResource.Create(new TextureInfo(desciption), CreateTexture, UpdateTexture);
            }
        }

        //this method gets called, when Reinitialize() was called in evaluate,
        //or a graphics device asks for its data
        Texture CreateTextureFromImage(TextureInfo info, Device device)
        {
            var description = info.Description;
            var pool = Pool.Managed;
            var usage = Usage.None;

            if (device is DeviceEx)
            {
                pool = Pool.Default;
                usage = Usage.Dynamic;
            }

            switch (description.DataType)
            {
                case TextureDescriptionDataType.IntPtr:
                    throw new NotImplementedException("Cannot create texture from image data in IntPtr");
                case TextureDescriptionDataType.Array:
                case TextureDescriptionDataType.Spread:
                    var bytes = (byte[])description.GetDataArray();
                    if(description.Width == 0 || description.Height == 0)
                        return Texture.FromMemory(device, bytes);
                    else
                        return Texture.FromMemory(device, bytes, description.Width, description.Height, 1, usage, Format.Unknown, pool, Filter.Default, Filter.Default, 0);
                case TextureDescriptionDataType.Stream:
                    var stream = description.GetDataStream();
                    stream.Position = 0;
                    if (description.Width == 0 || description.Height == 0)
                        return Texture.FromStream(device, stream);
                    else
                        return Texture.FromStream(device, stream, description.Width, description.Height, 1, usage, Format.Unknown, pool, Filter.Default, Filter.Default, 0);
            }

            throw new NotImplementedException("Could not create texture from image data");
        }

        //this method gets called, when Reinitialize() was called in evaluate,
        //or a graphics device asks for its data
        Texture CreateTexture(TextureInfo info, Device device)
        {
            var description = info.Description;
            var pool = Pool.Managed;
            var usage = Usage.None;

            if (device is DeviceEx)
            {
                pool = Pool.Default;
                usage = Usage.Dynamic;
            }

            return new Texture(device, Math.Max(description.Width, 1), Math.Max(description.Height, 1), 1, usage, GetDX9Format(description.Format), pool);
        }

        //this method gets called, when Update() was called in evaluate,
        //or a graphics device asks for its texture, here you fill the texture with the actual data
        //this is called for each renderer, careful here with multiscreen setups, in that case
        //calculate the pixels in evaluate and just copy the data to the device texture here
        unsafe void UpdateTexture(TextureInfo info, Texture texture)
{'fraction_non_alphanumeric': 0.0765680375834501, 'fraction_numerical': 0.005027610648644193, 'mean_word_length': 1.89456106870229, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '1876663', 'n_tokens_mistral': 3082, 'n_tokens_neox': 2874, 'n_words': 852}