Forum

Creating textures in VL with FeralTic

Hello, I’m trying to write a texture from bitmap data. I understand that its possible to generate an image from a bitmap and ‘Upload’ it into a texture, but I would like to try a more direct route because the bitmap is imported from a byte array of a jpg and its pixelformat be uploaded, which is an expensive conversion.

So I thought I would take a look at writing a converter in vl for use in beta, but I was wondering but I’m finding it tricky so far.

(The above is a draft I need to elaborate on. In short it seems as though there are either missing nodes or the texture cannot be read in vvvv beta)

you can’t follow the route as shown here?

\girlpower\VL\DX\DynamicBuffersAndTextures.v4p

I did previously (byte[] -> Image but I had to convert the jpg byte[] to a bitmap convert the pixel format and from then convert to Image which is expensive.
The nicer way would be to convert from bitmap directly to a texture, as seen here. I started writing this a while ago but something else popped up today, so I’ll have to find the appropriate example file, but I tried to get this by importing FeralTic dlls. I either couldn’t understand a step or one of the nodes was missing.

There is texture from stream method, witch works quite ok, but seems can’t find any track of it

@antokhio yeah I think that’s what I was looking for, but to no avail.

1 Like

Shame about the source code, but you’re a gem!

using FeralTic.DX11;
using FeralTic.DX11.Resources;
using System.IO;
using VVVV.PluginInterfaces.V1;
using VVVV.PluginInterfaces.V2;

namespace VVVV.DX11.ANT
{
  [PluginInfo(Author = "antokhio", AutoEvaluate = true, Category = "DX11", Name = "AsTexture", Version = "Texture")]
  public class JpegFromRaw : IPluginEvaluate, IPluginBase, IDX11ResourceHost, IDX11RenderGraphPart
  {
    [Input("Stream")]
    protected IDiffSpread<Stream> FIn;
    [Output("Texture Out")]
    protected ISpread<DX11Resource<DX11Texture2D>> textureOutput;

    public void Evaluate(int SpreadMax)
    {
      this.textureOutput.set_SliceCount(SpreadMax);
      for (int index = 0; index < SpreadMax; ++index)
      {
        if (this.textureOutput.get_Item(index) == null)
          this.textureOutput.set_Item(index, new DX11Resource<DX11Texture2D>());
      }
      if (!this.FIn.get_IsChanged())
        return;
      for (int index = 0; index < SpreadMax; ++index)
      {
        if (((ISpread<Stream>) this.FIn).get_Item(index) != null)
        {
          this.textureOutput.get_Item(index).Dispose();
          this.textureOutput.set_Item(index, new DX11Resource<DX11Texture2D>());
        }
      }
    }

    public void Update(DX11RenderContext context)
    {
      for (int index = 0; index < ((ISpread<Stream>) this.FIn).get_SliceCount(); ++index)
      {
        if (!this.textureOutput.get_Item(index).Contains(context))
        {
          DX11Texture2D dx11Texture2D = DX11Texture2D.FromStream(context, ((ISpread<Stream>) this.FIn).get_Item(index), (int) ((ISpread<Stream>) this.FIn).get_Item(index).Length);
          this.textureOutput.get_Item(index).set_Item(context, dx11Texture2D);
        }
      }
    }

    public void Destroy(DX11RenderContext context, bool force)
    {
      for (int index = 0; index < ((ISpread<Stream>) this.FIn).get_SliceCount(); ++index)
      {
        if (this.textureOutput.get_Item(index) != null)
          this.textureOutput.get_Item(index).Dispose(context);
      }
    }
  }
}

Courtesy of dotPeek

Funnily enough I did the same with ILSpy a little early but I’m a little busy to clean it up, so thanks!