Hey all
I’m getting glitches with texture output in a dynamic texture node. I’m using a method which fills the whole texture simultaneously from an IntPtr.
Rather than post files that people would have to downlaod, it’s much easier just to paste this into a node (thanks VVVV group, CodeEditor!)
- 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;
- endregion usings
//here you can change the vertex type
using VertexType = VVVV.Utils.SlimDX.TexturedVertex;
namespace VVVV.Nodes
{
#region PluginInfo
[PluginInfo(Name = "SpeedyTextureOut", Category = "EX9.Texture", Help = "Basic template which creates a texture", Tags = "")](PluginInfo(Name = "SpeedyTextureOut", Category = "EX9.Texture", Help = "Basic template which creates a texture", Tags = ""))
#endregion PluginInfo
public class Highspeedtextureout : DXTextureOutPluginBase, IPluginEvaluate
{
#region fields & pins
[Input("Wave Phase", DefaultValue = 0.0, IsSingle = true)](Input("Wave Phase", DefaultValue = 0.0, IsSingle = true))
IDiffSpread<float> FPhaseIn;
[Input("Width", DefaultValue = 64, IsSingle = true)](Input("Width", DefaultValue = 64, IsSingle = true))
IDiffSpread<int> FWidthIn;
[Input("Height", DefaultValue = 64, IsSingle = true)](Input("Height", DefaultValue = 64, IsSingle = true))
IDiffSpread<int> FHeightIn;
[Import()](Import())
ILogger FLogger;
private IntPtr FData = IntPtr.Zero;
#endregion fields & pins
// import host and hand it to base constructor
[ImportingConstructor()](ImportingConstructor())
public Highspeedtextureout(IPluginHost host) : base(host)
{
}
//called when data for any output pin is requested
public void Evaluate(int SpreadMax)
{
//recreate texture if resolution was changed
if (FWidthIn.IsChanged || FHeightIn.IsChanged) {
//set new texture size
this.FData = Marshal.AllocCoTaskMem(FWidthIn[0](0) * FHeightIn[0](0) * 4);
Reinitialize();
}
//update texture
if (FPhaseIn.IsChanged) {
FillData();
Update();
}
}
//this method gets called, when Reinitialize() was called in evaluate,
//or a graphics device asks for its data
protected override Texture CreateTexture(Device device)
{
FLogger.Log(LogType.Debug, "Creating new texture...");
return TextureUtils.CreateTexture(device, Math.Max(FWidthIn[0](0), 1), Math.Max(FHeightIn[0](0), 1));
}
//this method gets called, when Update() was called in evaluate,
//using high speed texture fill
//copied from a bit of vux's test code writted at Node10 (post beers)
unsafe protected override void UpdateTexture(Texture texture)
{
Surface srf = texture.GetSurfaceLevel(0);
DataRectangle rect = srf.LockRectangle(LockFlags.Discard);
rect.Data.WriteRange(this.FData, FWidthIn[0](0) * FHeightIn[0](0) * 4);
srf.UnlockRectangle();
}
private unsafe void FillData()
{
int w = FWidthIn[0](0);
int h = FHeightIn[0](0);
Single fw = Convert.ToSingle(w);
Single fh = Convert.ToSingle(h);
float phase = FPhaseIn[0](0);
byte[]() byteArray = new byte[w * h * 4](w * h * 4);
byte wave;
for (int x=0; x<w; x++)
for (int y=0; y<h; y++)
{
wave = Convert.ToByte(255 *
((Math.Sin(
Convert.ToSingle(x)/fw *
Convert.ToSingle(y)/fh * 10 + phase) + 1) / 2)
);
byteArray[x * 4 + y*w*4 + 0](x * 4 + y*w*4 + 0) = 255;
byteArray[x * 4 + y*w*4 + 1](x * 4 + y*w*4 + 1) = wave;
byteArray[x * 4 + y*w*4 + 2](x * 4 + y*w*4 + 2) = wave;
byteArray[x * 4 + y*w*4 + 3](x * 4 + y*w*4 + 3) = 255;
}
Marshal.Copy(byteArray, 0, FData, w * h * 4);
}
}
}
You’ll get a node called ‘SpeedyTextureOut’. And, probably, glitches every 1,2,3 seconds in the framerate. I’m not sure where it’s coming from. CPU usage isn’t very high. It’s about the same size of glitch as a recompile of the plugin.
Any ideas?