-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFxcEffectProcessor.cs
More file actions
105 lines (90 loc) · 3.86 KB
/
FxcEffectProcessor.cs
File metadata and controls
105 lines (90 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
namespace FNA.NET.ContentPipeline
{
[ContentProcessor(DisplayName = "Effect - Fxc(FNA)")]
public class FxcEffectProcessor : EffectProcessor
{
static string FxcExePath;
static void EnsureFxcTool()
{
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fnaToolsFolder = Path.Combine(appDataPath, "FNA.NET", "tools");
if (!Directory.Exists(fnaToolsFolder))
Directory.CreateDirectory(fnaToolsFolder);
FxcExePath = Path.Combine(fnaToolsFolder, Resources.FxcExeFileName);
if (File.Exists(FxcExePath))
return;
File.WriteAllBytes(FxcExePath, Resources.FxcExeBinary);
File.WriteAllBytes(Path.Combine(fnaToolsFolder, Resources.D3dcompilerDllFileName), Resources.D3dcompilerBinary);
File.WriteAllBytes(Path.Combine(fnaToolsFolder, Resources.D3dx9DllFileName), Resources.D3dx9Binary);
}
static FxcEffectProcessor()
{
EnsureFxcTool();
}
public override CompiledEffectContent Process(EffectContent input, ContentProcessorContext context)
{
string compiledTempFile = Path.GetTempFileName() + ".fxb";
var defineList = new List<string>();
defineList.Add("/D OPENGL");
if (!string.IsNullOrEmpty(Defines))
{
foreach (var define in Defines.Split(";", StringSplitOptions.TrimEntries))
{
if (string.IsNullOrEmpty(define)) continue;
defineList.Add($"/D {define}");
}
}
var defineStr = string.Join(" ", defineList);
Process process;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = FxcExePath,
Arguments = string.Format("/nologo /Vd /T fx_2_0 {0} /Fo\"{1}\" \"{2}\"",
defineStr, compiledTempFile, input.Identity.SourceFilename),
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false
}
};
}
else
{
var relativeSourceFilename = Path.GetRelativePath(Environment.CurrentDirectory, input.Identity.SourceFilename);
process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "wine",
Arguments = string.Format("\"{0}\" /nologo /Vd /T fx_2_0 {1} /Fo\"{2}\" \"{3}\"",
FxcExePath, defineStr, compiledTempFile, relativeSourceFilename),
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false
}
};
}
process.Start();
process.WaitForExit();
if (!File.Exists(compiledTempFile))
{
string output = process.StandardError.ReadToEnd();
throw new InvalidContentException(output, input.Identity);
}
byte[] buffer = File.ReadAllBytes(compiledTempFile);
return new CompiledEffectContent(buffer);
}
}
}