Skip to content

Instantly share code, notes, and snippets.

@elliotwoods
Created December 22, 2015 09:05
Show Gist options
  • Save elliotwoods/5e7b1310a6f845826f61 to your computer and use it in GitHub Desktop.
Save elliotwoods/5e7b1310a6f845826f61 to your computer and use it in GitHub Desktop.
#region usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using VVVV.PluginInterfaces.V1;
using VVVV.PluginInterfaces.V2;
using SlimDX.Direct3D11;
using VVVV.Core.Logging;
using VVVV.DX11;
using FeralTic.DX11;
using FeralTic.DX11.Resources;
using System.IO;
using System.Runtime.InteropServices;
#endregion usings
[PluginInfo(Name = "BlockWriter", Category = "DX11.Texture", Version = "2d BC4", Author = "vux", AutoEvaluate = true)]
public class BlockWriterTextureNode : IPluginEvaluate, IDX11ResourceDataRetriever
{
[Input("Texture In")]
protected Pin<DX11Resource<DX11Texture2D>> FTextureIn;
[Input("Filename",StringType=StringType.Filename,DefaultString="render")]
protected ISpread<string> FInPath;
[Input("Create Folder", IsSingle = true, Visibility = PinVisibility.OnlyInspector)]
protected ISpread<bool> FCreateFolder;
[Input("Write", IsBang = true)]
protected ISpread<bool> FInSave;
[Output("Valid")]
protected ISpread<bool> FOutValid;
[Import()]
protected IPluginHost FHost;
[Import()]
protected ILogger FLogger;
[DllImport("DirectXTexLib_x64", CharSet = CharSet.Unicode)]
public static extern long SaveCompressedTextureToFile(IntPtr device, IntPtr context, IntPtr resource, string path, int blocktype);
public static void SaveToFileCompressedBC4(DX11RenderContext device, DX11Texture2D texture, string path)
{
long retcode = SaveCompressedTextureToFile(device.Device.ComPointer, device.CurrentDeviceContext.ComPointer,
texture.Resource.ComPointer, path, (int)SlimDX.DXGI.Format.BC4_UNorm);
if (retcode < 0)
{
throw new Exception("Failed to Save Texture");
}
}
public DX11RenderContext AssignedContext
{
get;
set;
}
public event DX11RenderRequestDelegate RenderRequest;
#region IPluginEvaluate Members
public void Evaluate(int SpreadMax)
{
this.FOutValid.SliceCount = 1;
if (this.FTextureIn.PluginIO.IsConnected)
{
if (this.RenderRequest != null) { this.RenderRequest(this, this.FHost); }
if (this.AssignedContext == null) { this.FOutValid.SliceCount = 0; return; }
//Do NOT cache this, assignment done by the host
for (int i = 0; i < SpreadMax; i++)
{
if (this.FTextureIn[i].Contains(this.AssignedContext) && this.FInSave[i])
{
if (this.FCreateFolder[0])
{
string path = Path.GetDirectoryName(this.FInPath[i]);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
try
{
SaveToFileCompressedBC4(this.AssignedContext,
this.FTextureIn[i][this.AssignedContext],
this.FInPath[i]);
this.FOutValid[0] = true;
}
catch (Exception ex)
{
FLogger.Log(ex);
this.FOutValid[0] = false;
}
}
else
{
this.FOutValid[0] = false;
}
}
}
else
{
this.FOutValid.SliceCount = 0;
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment