forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
160 lines (136 loc) · 6.04 KB
/
Program.cs
File metadata and controls
160 lines (136 loc) · 6.04 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// MonoGame - Copyright (C) MonoGame Foundation, Inc
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.IO;
namespace MonoGame.Effect.Compiler
{
public static class Program
{
public static int Main(string[] args)
{
if (!Environment.Is64BitProcess && Environment.OSVersion.Platform != PlatformID.Unix)
{
Console.Error.WriteLine("The MonoGame content tools only work on a 64bit OS.");
return -1;
}
var options = new Options();
var parser = new CommandLineParser(options);
parser.Title = "mgfxc - The MonoGame Effect compiler.";
if (!parser.ParseCommandLine(args))
return 1;
int ret = CompileFile(options, out bool tryAgainUsingMojo);
if (tryAgainUsingMojo)
{
options.UseMojoShader = true;
ret = CompileFile(options, out tryAgainUsingMojo);
}
return ret;
}
private static int CompileFile(Options options, out bool tryAgainUsingMojo)
{
tryAgainUsingMojo = false;
//
if (options.Profile is OpenGLShaderProfile && !options.UseMojoShader && options.IsDefined("MOJO"))
{
tryAgainUsingMojo = true;
return 0;
}
// We don't support running MojoShader on Unix platforms
// however Wine can be used to make it work so lets try that.
if (options.UseMojoShader && Environment.OSVersion.Platform == PlatformID.Unix)
return WineHelper.Run(options);
// Validate the input file exits.
if (!File.Exists(options.SourceFile))
{
Console.Error.WriteLine("The input file '{0}' was not found!", options.SourceFile);
return 1;
}
// TODO: This would be where we would decide the user
// is trying to convert an FX file to a MGFX glsl file.
//
// For now we assume we're going right to a compiled MGFXO file.
// Parse the MGFX file expanding includes, macros, and returning the techniques.
ShaderResult shaderResult;
try
{
shaderResult = ShaderResult.FromFile(options.SourceFile, options, new ConsoleEffectCompilerOutput());
foreach (var dependency in shaderResult.Dependencies)
Console.WriteLine("Dependency: " + dependency);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
Console.Error.WriteLine("Failed to parse '{0}'!", options.SourceFile);
return 1;
}
// Fallback to MojoShader compilation for shader model 2 and 3 shaders
if (options.Profile is OpenGLShaderProfile && !options.UseMojoShader)
{
if(!options.IsDefined("CONDUCTOR") && !EffectObject.DoesEffectNeedSM4OrHigher(shaderResult))
{
tryAgainUsingMojo = true;
return 0;
}
}
// Create the effect object.
EffectObject effect;
var shaderErrorsAndWarnings = string.Empty;
try
{
effect = EffectObject.CompileEffect(shaderResult, options, out shaderErrorsAndWarnings);
if (!string.IsNullOrEmpty(shaderErrorsAndWarnings))
Console.Error.WriteLine(shaderErrorsAndWarnings);
}
catch (ShaderCompilerException)
{
// Write the compiler errors and warnings and let the user know what happened.
Console.Error.WriteLine(shaderErrorsAndWarnings);
Console.Error.WriteLine("Failed to compile '{0}'!", options.SourceFile);
return 1;
}
catch (Exception ex)
{
// First write all the compiler errors and warnings.
if (!string.IsNullOrEmpty(shaderErrorsAndWarnings))
Console.Error.WriteLine(shaderErrorsAndWarnings);
// If we have an exception message then write that.
if (!string.IsNullOrEmpty(ex.Message))
Console.Error.WriteLine(ex.Message);
// Let the user know what happened.
Console.Error.WriteLine("Unexpected error compiling '{0}'!", options.SourceFile);
return 1;
}
// Get the output file path.
if (options.OutputFile == string.Empty)
options.OutputFile = Path.GetFileNameWithoutExtension(options.SourceFile) + ".mgfxo";
// Write out the effect to a runtime format.
try
{
using (var stream = new FileStream(options.OutputFile, FileMode.Create, FileAccess.Write))
using (var writer = new BinaryWriter(stream))
effect.Write(writer, options);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
Console.Error.WriteLine("Failed to write '{0}'!", options.OutputFile);
return 1;
}
// We finished succesfully.
Console.WriteLine("Compiled '{0}' to '{1}'.", options.SourceFile, options.OutputFile);
return 0;
}
private class ConsoleEffectCompilerOutput : IEffectCompilerOutput
{
public void WriteWarning(string file, int line, int column, string message)
{
Console.WriteLine("Warning: {0}({1},{2}): {3}" , file, line, column, message);
}
public void WriteError(string file, int line, int column, string message)
{
throw new Exception(string.Format("Error: {0}({1},{2}): {3}", file, line, column, message));
}
}
}
}