Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writer2 #258

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions Nodes/VVVV.DX11.Nodes/Nodes/Textures/2D/TextureLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,59 @@ private class NativeMethods32
public static extern void DeleteBlob(IntPtr blob);
}

public static ImageMetadata LoadMetadataFromFile(string path)
{
ImageMetadata info = NativeMethods.LoadMetadataFromFile(path);

return info;
}

public static void LoadTextureFromFile(IntPtr device, string path, out IntPtr resource, int miplevels)
{
long retcode = NativeMethods.LoadTextureFromFile(device, path, out resource, miplevels);

if (retcode < 0)
{
throw new Exception("Failed to Load Texture");
}
}

public static void SaveToFile(DX11RenderContext device, DX11Texture2D texture, string path, eImageFormat format)
{
long retcode = NativeMethods.SaveTextureToFile(device.Device.ComPointer, device.CurrentDeviceContext.ComPointer,
texture.Resource.ComPointer, path, (int)format);

if (retcode < 0)
{
throw new Exception("Failed to Save Texture");
}
}

public static void SaveToFile(DX11RenderContext device, Texture2D texture, string path, eImageFormat format)
{
long retcode = NativeMethods.SaveTextureToFile(device.Device.ComPointer, device.CurrentDeviceContext.ComPointer,
texture.ComPointer, path, (int)format);

if (retcode < 0)
{
throw new Exception("Failed to Save Texture");
}
}



public static void SaveToFile(DeviceContext device, Texture2D texture, string path, eImageFormat format)
{
long retcode = NativeMethods.SaveTextureToFile(device.Device.ComPointer, device.ComPointer,
texture.ComPointer, path, (int)format);

if (retcode < 0)
{
throw new Exception("Failed to Save Texture");
}
}


public static void SaveToFileCompressed(DX11RenderContext device, DX11Texture2D texture, string path, DdsBlockType blockType)
{
long retcode = NativeMethods.SaveCompressedTextureToFile(device.Device.ComPointer, device.CurrentDeviceContext.ComPointer,
Expand Down
307 changes: 307 additions & 0 deletions Nodes/VVVV.DX11.Nodes/Nodes/Textures/2D/Writer2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
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 SlimDX.DXGI;

using VVVV.Core.Logging;

using FeralTic.DX11;
using FeralTic.DX11.Resources;
using System.IO;
using System.Threading.Tasks;
using System.Security.Permissions;

namespace VVVV.DX11.Nodes
{
[PluginInfo(Name = "Writer", Category = "DX11.Texture", Version = "2d native", Author = "vux", AutoEvaluate = true)]
public class WriterTextureNode2 : IPluginEvaluate, IDX11ResourceDataRetriever
{
[Input("Texture In")]
protected Pin<DX11Resource<DX11Texture2D>> FTextureIn;

[Input("Filename", StringType = StringType.Filename, DefaultString = "render")]
protected ISpread<string> FInPath;

[Input("Format")]
protected ISpread<eImageFormat> FInFormat;
//protected ISpread<ImageFileFormat> FInFormat;

[Input("Threaded", IsSingle = true, DefaultBoolean = true)]
protected ISpread<bool> FThreaded;

[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;

public DX11RenderContext AssignedContext
{
get;
set;
}

public event DX11RenderRequestDelegate RenderRequest;

//List<Task> tasks = new List<Task>();


#region IPluginEvaluate Members

public async void Evaluate(int SpreadMax)
{
this.FOutValid.SliceCount = SpreadMax;

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])
{
tasks.Clear();

FLogger.Log(LogType.Debug, "Task {0} adding:", i);

Task t = Task.Run(() =>
TextureLoader.SaveToFile(this.AssignedContext,
this.FTextureIn[i][this.AssignedContext],
this.FInPath[i], this.FInFormat[i])
);
tasks.Add(t);
FLogger.Log(LogType.Debug, "Task {0} added:", i);
}
}

if (tasks.Count > 0)
{
try
{
Task.WaitAll(tasks.ToArray());

foreach (Task t in tasks)
FLogger.Log(LogType.Debug, "Task {0} Status: {1}", t.Id, t.Status);
}
catch (Exception e)
{
FLogger.Log(LogType.Debug, "Exception: " + e);
}

}
*/





for (int i = 0; i < SpreadMax; i++)
{
if (this.FTextureIn[i].Contains(this.AssignedContext) && this.FInSave[i])
{
//List<Task> tasks = new List<Task>();

if (this.FCreateFolder[0])
{
string path = Path.GetDirectoryName(this.FInPath[i]);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}

//await Task.Run(() => DoSave(i));





try
{
/*
DX11RenderContext threadContext = new DX11RenderContext(this.AssignedContext.Device);

Texture2D FBackSurface = new Texture2D(threadContext.Device, this.FTextureIn[i][this.AssignedContext].Description);

threadContext.CurrentDeviceContext.CopyResource(this.FTextureIn[i][this.AssignedContext].Resource, FBackSurface);
*/
//DX11Texture2D FBackSurface = DX11Texture2D.FromTextureAndSRV(threadContext, this.FTextureIn[i][this.AssignedContext].Resource, this.FTextureIn[i][this.AssignedContext].SRV);


if (FThreaded[0])
{

// working half-way:
// await Task.Run(() => TextureLoader.SaveToFile(threadContext, FBackSurface, FInPath[i], FInFormat[i]) );

//saver(this.AssignedContext, this.FTextureIn[i][this.AssignedContext], FInPath[i], FInFormat[i]);


await Task.Run(() => saver(this.AssignedContext, this.FTextureIn[i][this.AssignedContext], FInPath[i], FInFormat[i]));

//try
//{
// await Task.Run(() => saver(threadContext, FBackSurface, FInPath[i], FInFormat[i]));

//}
//catch (Exception e)
//{
// FLogger.Log(e);
//}
//finally
//{
// if (threadContext.CurrentDeviceContext != null)
// {
// threadContext.CurrentDeviceContext.Dispose();
// }
//}



}
else
{
TextureLoader.SaveToFile(this.AssignedContext, this.FTextureIn[i][this.AssignedContext], FInPath[i], FInFormat[i]);

//saver(this.AssignedContext, this.FTextureIn[i][this.AssignedContext], FInPath[i], FInFormat[i]);

//TextureLoader.SaveToFile(threadContext,
//FBackSurface,
//FInPath[i], FInFormat[i]);
}

// formerly:
// TextureLoader.SaveToFile(this.AssignedContext, this.FTextureIn[i][this.AssignedContext], this.FInPath[i], this.FInFormat[i]);



this.FOutValid[i] = true;
}
catch (Exception ex)
{
FLogger.Log(ex);
this.FOutValid[i] = false;
}
}
else
{
this.FOutValid[i] = false;
}
}

}
else
{
this.FOutValid.SliceCount = 0;

}
}



public async void saver( DX11RenderContext assignedContext, DX11Texture2D textureIn, string path, eImageFormat format)
{

DX11RenderContext threadContext = new DX11RenderContext(assignedContext.Device);
//DX11RenderContext threadContext = new DX11RenderContext(assignedContext.Adapter);

Texture2DDescription desc = textureIn.Description;
//desc.Usage = ResourceUsage.Staging;
//desc.CpuAccessFlags = CpuAccessFlags.Read;

//Texture2D FBackSurface;

//if (FBackSurface == null || FBackSurface.Description.Width != texture.Width || FBackSurface.Description.Height != texture.Height || context != FContext)
//{
//if (FBackSurface != null)
//{
// FBackSurface.Dispose();
//}
var description = new Texture2DDescription()
{
Width = textureIn.Width,
Height = textureIn.Height,
Format = textureIn.Format,
MipLevels = 1,
Usage = ResourceUsage.Staging,
BindFlags = BindFlags.None,
CpuAccessFlags = CpuAccessFlags.Read,
SampleDescription = new SampleDescription(1, 0),
ArraySize = 1
};

Texture2D FBackSurface = new Texture2D(assignedContext.Device, description);

//}
//Texture2D FBackSurface = new Texture2D(threadContext.Device, textureIn.Description);
//Texture2D FBackSurface = new Texture2D(threadContext.Device, desc);
//Texture2D FBackSurface;
threadContext.CurrentDeviceContext.CopyResource(textureIn.Resource, FBackSurface);

//DeviceCreationFlags dcf = threadContext.Device.CreationFlags;

// what's the difference?
//FBackSurface.AsSurface();

bool ts;
bool cs;
//threadContext.Device.CheckThreadingSupport(out ts, out cs);
threadContext.CurrentDeviceContext.Device.CheckThreadingSupport(out ts, out cs);
if (cs && ts)
{
(new FileIOPermission(FileIOPermissionAccess.Write, path)).Demand();

//ImageFileFormat f = ImageFileFormat.Png;
//Texture2D.ToFile(threadContext.CurrentDeviceContext, FBackSurface, f, path);

TextureLoader.SaveToFile(threadContext, FBackSurface, path, format);

//if (threadContext.CurrentDeviceContext != null)
//{
// threadContext.CurrentDeviceContext.Dispose();
//}

//try
//{
// TextureLoader.SaveToFile(threadContext, FBackSurface, path, format);
//}
//catch (Exception e)
//{
// //FLogger.Log(e);
//}
//finally
//{
// if (threadContext.CurrentDeviceContext != null)
// {
// threadContext.CurrentDeviceContext.Dispose();
// }
//}
}

}

#endregion
}
}