-
Notifications
You must be signed in to change notification settings - Fork 66
/
Shaders.tt
681 lines (590 loc) · 26 KB
/
Shaders.tt
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ assembly name="System.Windows.Forms" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.IO.Compression" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Resources" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="System.Windows.Forms" #>
<#@ output extension=".cs" #>
<#= string.Join("\n", Encoding.UTF8.GetString(Convert.FromBase64String("R0VORVJBVEVEIEFVVE9NQVRJQ0FMTFkKRE9O4oCZVCBNT0RJRlk="))
.Split('\n').Select(x => $"/* {x} */")) #>
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
using AcTools.Render.Base.Shaders;
using AcTools.Render.Base.Structs;
using AcTools.Render.Base.Utils;
using AcTools.Utils.Helpers;
using JetBrains.Annotations;
using SlimDX;
using SlimDX.D3DCompiler;
using SlimDX.Direct3D11;
// ReSharper disable InconsistentNaming
// ReSharper disable LocalizableElement
// ReSharper disable NotNullMemberIsNotInitialized
<#
/* INITIALIZE PATHS */
var dataFolder = Path.GetFullPath(Host.ResolvePath(@"."));
string rootNamespace;
string resultNamespace = null;
string resxName = null;
/* COMPRESSION */
Func<byte[], byte[]> Compress = data => {
MemoryStream output = new MemoryStream();
using (var dstream = new DeflateStream(output, CompressionLevel.Optimal)){
dstream.Write(data, 0, data.Length);
}
return output.ToArray();
};
/* LOOKING FOR CSPROJ TO FIND PROJECT’S ROOT DIRECTORY */
var projectRoot = dataFolder;
while (true){
var files = Directory.GetFiles(projectRoot, "*.csproj");
if (files.Length > 2){
throw new Exception("Two .csproj-files found, can’t work if these conditions");
}
if (files.Length == 1){
var xml = XDocument.Load(files[0]);
var ns = xml.Root.Name.Namespace;
rootNamespace = xml.Root.Descendants(ns + "RootNamespace").First().Value;
if (resultNamespace == null){
resultNamespace = rootNamespace;
} else {
resultNamespace = rootNamespace + "." + resultNamespace;
}
break;
}
var directoryName = Path.GetFileName(projectRoot);
if (resxName == null){
resxName = directoryName + ".resources";
}
if (resultNamespace == null){
resultNamespace = directoryName;
} else {
resultNamespace = directoryName + "." + resultNamespace;
}
projectRoot = Path.GetDirectoryName(projectRoot);
if (projectRoot == null) throw new Exception("Could not find project’s root directory");
}
var baseName = rootNamespace + "." + Path.GetFileNameWithoutExtension(resxName);
#>
namespace <#= resultNamespace #> {
internal static class ShadersResourceManager {
internal static readonly ResourceManager Manager = new ResourceManager("<#= baseName #>", Assembly.GetExecutingAssembly());
}
<#
/* GENERATE SHADERS */
var tempFolder = Path.Combine(projectRoot, "obj", "Shaders");
Directory.CreateDirectory(tempFolder);
var resxLocation = Path.Combine(projectRoot, resxName);
var warned = false;
Action<string> Warn = msg => {
if (warned) return;
warned = true;
MessageBox.Show(msg, "Shaders Compilation", MessageBoxButtons.OK, MessageBoxIcon.Warning);
};
Func<string, List<string>, DateTime> LastIncludeWriteTime_Inner = null;
LastIncludeWriteTime_Inner = (filename, previous) => {
var data = File.ReadAllText(filename);
previous.Add(filename);
var lastWrite = default(DateTime);
foreach (var match in Regex.Matches(data, @"#include ""([^""]+)""").Cast<Match>()){
var included = Path.Combine(dataFolder, "Includes", match.Groups[1].Value);
if (previous.Contains(included)) continue;
var iInfo = new FileInfo(included);
if (!iInfo.Exists) {
Warn($"Included file “{Path.GetFileName(included)}” not found (from “{Path.GetFileName(filename)}”).");
return DateTime.Now;
} else {
if (iInfo.LastWriteTime > lastWrite) {
lastWrite = iInfo.LastWriteTime;
}
var inner = LastIncludeWriteTime_Inner(included, previous);
if (inner > lastWrite) {
lastWrite = inner;
}
}
}
return lastWrite;
};
Func<string, DateTime> LastIncludeWriteTime = filename => {
return LastIncludeWriteTime_Inner(filename, new List<string>());
};
Func<string, byte[]> CompileShader = f => {
if (f == null) return null;
var n = Path.GetFileName(f);
var c = f + ".o";
var h = Path.Combine(tempFolder, n + ".o");
var v = Path.Combine(tempFolder, n + ".warn");
var hInfo = new FileInfo(h);
if (hInfo.Exists && (new FileInfo(f).LastWriteTime < hInfo.LastWriteTime && LastIncludeWriteTime(f) < hInfo.LastWriteTime)){
if (File.Exists(v)) Debug.WriteLine(File.ReadAllText(v));
return File.ReadAllBytes(h);
}
var fxc = @"C:\Program Files (x86)\Windows Kits\8.1\bin\x64\fxc.exe";
if (!File.Exists(fxc)){
#> /* ! fxc.exe is missing ! */ }<#
return null;
}
var p = Process.Start(new ProcessStartInfo {
FileName = fxc,
Arguments = "/nologo /T fx_5_0 /O3 /I../Includes /E Render /Fo\"" + Path.GetFileName(c) + "\" \"" + n + "\"",
WorkingDirectory = Path.GetDirectoryName(f) ?? "",
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false
});
if (p == null) {
return null;
}
p.Start();
var w = p.StandardError.ReadToEnd().Replace("warning X4717: Effects deprecated for D3DCompiler_47", "")
.Replace(Path.GetDirectoryName(f) + "\\", "").Trim();
if (w.Length > 0) Debug.WriteLine(w);
p.WaitForExit();
if (p.ExitCode != 0){
throw new Exception(w);
}
if (File.Exists(h)) File.Delete(h);
File.WriteAllBytes(h, Compress(File.ReadAllBytes(c)));
File.Delete(c);
if (w.Length > 0){
Warn($"While compiling “{Path.GetFileName(f)}”:\n{w}");
File.WriteAllText(v, w);
} else {
File.Delete(v);
}
return File.ReadAllBytes(h);
};
using (var writer = new ResourceWriter(resxLocation)){
foreach (var f in Directory.GetFiles(dataFolder, "*.fx").Union(
Directory.GetDirectories(dataFolder)
.Where(x => !string.Equals(Path.GetFileName(x), "Includes", StringComparison.OrdinalIgnoreCase))
.SelectMany(x => Directory.GetFiles(x, "*.fx")))){
writer.AddResource(Path.GetFileNameWithoutExtension(f), CompileShader(f));
}
writer.Generate();
}
/* CONSTS */
var LiteralRegularMode = true;
/* FUNCTIONS */
Func<string,string> ConvertFxName = s => {
var t = s[0] == 'g' ? s.Substring(1) : s;
t = t.Substring(0, 1).ToUpperInvariant() + t.Substring(1);
return "Fx" + t;
};
Func<string,string> ConvertConstName = s =>
Regex.Replace(s.ToLower(), @"^\w|_\w", x => x.Value.Replace("_", "").ToUpper());
Func<string,string> ConvertConstValue = s =>
Regex.Replace(
s.Replace("float4", "new Vector4").Replace("float3", "new Vector3").Replace("float2", "new Vector2"),
@"(\d+\.\d+\b)", x => x.Value + "f");
Func<string,string> MapShaderType = s =>
s == "dword" ? "uint" :
s == "float4x4" ? "Matrix" :
s == "float2" ? "Vector2" :
s == "float3" ? "Vector3" :
s == "float4" ? "Vector4" :
s;
Func<string,string> ClearHlslCode = s => Regex.Replace(s, @"//.+", "");
Func<string,string> ConvertStringToLiteral = s => LiteralRegularMode ?
"\"" + s
.Replace("\\", @"\\")
.Replace("\"", "\\\"")
.Replace("\r", @"")
.Replace("\t", @"\t")
.Replace("\n", @"\n") +
"\"" :
"@\"" + s.Replace("\"", "\"\"") + "\"";
/* INCLUDEABLE FILES */
var includes = new Dictionary<string,string>();
foreach (var file in Directory.GetFiles(dataFolder + "/Includes")){
var includeName = Path.GetFileName(file);
includes[includeName] = File.ReadAllText(file);
}
Func<string, List<string>, string> PrependIncludes_Inner = null;
PrependIncludes_Inner = (data, added) => {
foreach (var x in Regex.Matches(data, @"\s*#include\s+""(.+)""").Cast<Match>().Select(x => x.Groups[1].Value)){
if (added.Contains(x)) continue;
added.Add(x);
var included = includes[x];
var skip = included.IndexOf("// Skip translation");
if (skip != -1) included = included.Substring(0, skip);
data = PrependIncludes_Inner(included, added) + "\n\n" + data;
}
return data;
};
Func<string, string> PrependIncludes = data => {
return PrependIncludes_Inner(data, new List<string>());
};
var alternatives = new Dictionary<string, string>();
foreach (var file in Directory.GetFiles(dataFolder).Union(Directory.GetDirectories(dataFolder).SelectMany(Directory.GetFiles))){
if (!file.EndsWith(".fx") || Path.GetDirectoryName(file).EndsWith("\\Includes")){
continue;
}
var name = Path.GetFileNameWithoutExtension(file);
var data = File.ReadAllText(file);
var alternative = Regex.Match(data, @"// Alternative for (\w+)");
if (alternative.Success){
alternatives[file] = alternative.Groups[1].Value.Trim();
}
}
foreach (var file in Directory.GetFiles(dataFolder).Union(Directory.GetDirectories(dataFolder).SelectMany(Directory.GetFiles))){
if (!file.EndsWith(".fx") || Path.GetDirectoryName(file).EndsWith("\\Includes")){
continue;
}
var name = Path.GetFileNameWithoutExtension(file);
var data = File.ReadAllText(file);
var alternative = Regex.Match(data, @"// Alternative for (\w+)");
if (alternative.Success){
continue;
}
var extraModes = alternatives.Where(x => x.Value == name).Select(x => x.Key).ToList();
/* ADD INCLUDED STUFF */
var baseData = data;
data = PrependIncludes(data);
/* TECHNIQUES */
var ts_tmp = Regex.Matches(data, @"(?:^|\n)\s*technique(?:1[01])?\s+(\w+)\s*\{(?:\s*//\s*(\w+))?\s*((?:\n\s+.+)+?)\n\s*\}").Cast<Match>().ToList();
var tls = ts_tmp.Where(x => x.Groups[2].Success).Select(x => new {
Type = x.Groups[2].Value,
Custom = (string)null,
TechName = x.Groups[1].Value
})
.Where(x => false) // disabling comment support
.ToList();
var ts = ts_tmp.Select(x => x.Groups[1].Value).ToList();
// # > /* < #= (name) # > */ < #
foreach (var match in Regex.Matches(data, @"(?:^|\n)\s*struct\s+((?:\w+_)?VS_IN)\s*\{.*((?:\n\s+.+)+?)\n\s*\}").Cast<Match>()){
var structName = match.Groups[1].Value;
// # > /* < #= (name + ": " + structName) # > */ < #
var techName = Regex.Matches(data, @"(?:^|\n)\s*\w+\s+(\w+)\s*\(\s*" + structName + @" +\w+.+?\s*\)\s*\{").Cast<Match>()
.Select(x => x.Groups[1].Value).SelectMany(x =>
ts_tmp.Where(y => y.Groups[3].Value.Contains(x)).Select(y => y.Groups[1].Value)
).FirstOrDefault();
if (techName == null) continue;
var customName = Regex.Match(match.Value, @"\{\s*//\s*layout:\s*([\w.]+)");
if (customName.Success){
tls.Add(new {
Type = customName.Groups[1].Value.Split('.').Last().Replace("Vertice", ""),
Custom = customName.Groups[1].Value,
TechName = techName
});
continue;
}
var types = Regex.Matches(match.Groups[2].Value, @"\b(POSITION|COLOR|NORMAL|TEXCOORD|TANGENT|BLENDWEIGHTS?|BLENDINDICES|[A-Z]{3,})\b").Cast<Match>().Select(x => x.Value);
var result = string.Join("", types.Select(x =>
x == "TANGENT" ? "G" :
x == "TEXCOORDSIZE" ? "X" :
x == "BLENDWEIGHT" ? "W2" :
x == "BLENDWEIGHTS" ? "W4" :
"" + x[0])).Trim();
if (result.Length == 0) continue;
tls.Add(new {
Type = result,
Custom = (string)null,
TechName = techName
});
// data = includes[match.Groups[1].Value] + "\n\n" + data;
}
if (!tls.Any()) continue;
tls = tls.GroupBy(x => x.Type).Select(x => x.First()).ToList();
/* BUFFERS, VARIABLES, TEXTURES */
var cbs = string.Join("\n", Regex.Matches(data, @"(?:^|\n)\s*cbuffer \w+.+(\n\s+.+)*?\n\s*\}").Cast<Match>().Select(x => x.Value));
var vrs = Regex.Matches(data, @"(?:^|\n)\s*(?:Texture[123]D|TextureCube)(?:<\w+>)?\s+(\w+)\s*(?::.*?)?;").Cast<Match>().ToList();
var vrsArr = Regex.Matches(data, @"(?:^|\n)\s*(?:Texture[123]D|TextureCube)(?:<\w+>)?\s+(\w+)\s*(?:\[\w+\]\s*)(?::.*?)?;").Cast<Match>().ToList();
var vms = Regex.Matches(cbs, @"\n\s+(?:matrix|float4x4)\s+(\w+)\s*;").Cast<Match>().ToList();
var vmsArr = Regex.Matches(cbs, @"\n\s+(?:matrix|float4x4)\s+(\w+)\s*(?:\[\w+\]\s*);").Cast<Match>().ToList();
var vssFloat = Regex.Matches(cbs, @"\n\s+(?:float)\s+(\w+)\s*;").Cast<Match>().ToList();
var vssFloatArr = Regex.Matches(cbs, @"\n\s+(?:float)\s+(\w+)\s*(?:\[\w+\]\s*);").Cast<Match>().ToList();
var vssInt = Regex.Matches(cbs, @"\n\s+(?:int)\s+(\w+)\s*;").Cast<Match>().ToList();
var vssIntArr = Regex.Matches(cbs, @"\n\s+(?:int)\s+(\w+)\s*(?:\[\w+\]\s*);").Cast<Match>().ToList();
var vssBool = Regex.Matches(cbs, @"\n\s+(?:bool)\s+(\w+)\s*;").Cast<Match>().ToList();
var vssBoolArr = Regex.Matches(cbs, @"\n\s+(?:bool)\s+(\w+)\s*(?:\[\w+\]\s*);").Cast<Match>().ToList();
var vvsV2 = Regex.Matches(cbs, @"\n\s+(?:float2)\s+(\w+)\s*;").Cast<Match>().ToList();
var vvsV3 = Regex.Matches(cbs, @"\n\s+(?:float3)\s+(\w+)\s*;").Cast<Match>().ToList();
var vvsV4 = Regex.Matches(cbs, @"\n\s+(?:float4)\s+(\w+)\s*;").Cast<Match>().ToList();
var vvsVArr = Regex.Matches(cbs, @"\n\s+(?:float\d)\s+(\w+)\s*(?:\[\w+\]\s*);").Cast<Match>().ToList();
var vs = Regex.Matches(cbs, @"\n\s+((?!matrix|float\d?|int|bool)\w+)\s+(\w+)\s*;").Cast<Match>().Select(x => new {
Type = x.Groups[1].Value,
Name = x.Groups[2].Value
});
var vsArr = Regex.Matches(cbs, @"\n\s+((?!matrix|float\d?|int|bool)\w+)\s+(\w+)\s*(?:\[\w+\]\s*);").Cast<Match>().Select(x => new {
Type = x.Groups[1].Value,
Name = x.Groups[2].Value
});
/* CONSTS */
var justReadonly = false;
var consts = Regex.Matches(data, @"(?:^|\n)\s*static const (\w+)\s+(\w+)\s*=\s*(.+?)\s*;").Cast<Match>().Select(x => {
var typeName = MapShaderType(x.Groups[1].Value);
var isConst = !justReadonly && !typeName.StartsWith("Vector");
var constName = ConvertConstName(x.Groups[2].Value);
var constValue = ConvertConstValue(x.Groups[3].Value);
return "public " + (isConst ? "const " : "static readonly ") + typeName + " " + constName + " = " + constValue + ";\n ";
}).ToList();
/* CONSTS FROM DEFINES (ROOT FILE ONLY) */
foreach (var x in Regex.Matches(data, @"(?:^|\n)\s*#define ((?=[A-Z])\w+)\s+(true|false|float\d.+|\d+.+)\s*(?=\n|$)").Cast<Match>()){
var v = x.Groups[2].Value.Split(new[]{ "//" }, StringSplitOptions.None)[0];
var constName = ConvertConstName(x.Groups[1].Value);
var constValue = ConvertConstValue(v).Trim();
var typeName = v.Contains("float4") ? "Vector4" :
v.Contains("float3") ? "Vector3" :
v.Contains("float2") ? "Vector2" :
v.Contains("true") || v.Contains("false") ? "bool" :
v.Contains(".") ? "float" : "int";
var isConst = !justReadonly && !typeName.StartsWith("Vector");
if (Regex.IsMatch(constName, @"^(?:Enable|Disable)[A-Z]|Flag$") && (constValue == "0" || constValue == "1")){
typeName = "bool";
constValue = constValue == "1" ? "true" : "false";
}
consts.Add("public " + (isConst ? "const " : "static readonly ") + typeName + " " + constName + " = " + constValue + ";\n ");
}
consts = consts.Distinct().ToList();
/* AND EVEN STRUCTS! */
var structsNames = new List<string>();
var structs = Regex.Matches(data, @"(?:^|\n)\s*struct\s+([A-Z][a-zA-Z]+)\s*\{.+((?:\n\s+.+)+?)\n\s*\}").Cast<Match>()
.Where(x => !x.Groups[1].Value.EndsWith("_IN", StringComparison.OrdinalIgnoreCase) &&
!x.Groups[1].Value.EndsWith("_OUTPUT", StringComparison.OrdinalIgnoreCase)).Select(x => {
structsNames.Add(x.Groups[1].Value);
return string.Format(@"[StructLayout(LayoutKind.Sequential)]
public struct {0} {{
{1}
public static readonly int Stride = Marshal.SizeOf(typeof({0}));
}}
" + (vs.Any(y => y.Type == x.Groups[1].Value) ? @"public class EffectStruct{0}Variable {{
private readonly EffectVariable _v;
public EffectStruct{0}Variable(EffectVariable v) {{
_v = v;
}}
public void Set({0} value){{
SlimDxExtension.SetObject(_v, value, {0}.Stride);
}}
}}
" : "") + (vsArr.Any(y => y.Type == x.Groups[1].Value) ? @"public class EffectStruct{0}ArrayVariable {{
private readonly EffectVariable _v;
public EffectStruct{0}ArrayVariable(EffectVariable v) {{
_v = v;
}}
public void SetArray({0}[] value){{
SlimDxExtension.SetArray(_v, value, {0}.Stride);
}}
}}
" : ""), x.Groups[1].Value,
string.Join("\n",
x.Groups[2].Value.Split('\n').Select(y => Regex.Replace(y, @"//.+", "").Trim().Split(new[]{ ' ' }, StringSplitOptions.RemoveEmptyEntries)).Where(y => y.Length == 2)
.Select(y => {
var arrayM = Regex.Match(y[1], @"^(\w+)\[(\d+)\];$");
if (arrayM.Success){
return "[MarshalAs(UnmanagedType.ByValArray, SizeConst = " + arrayM.Groups[2].Value + ")]\n public " + MapShaderType(y[0]) + "[] " + arrayM.Groups[1].Value + ";";
}
return "public " + MapShaderType(y[0]) + " " + y[1];
})
.Select(y => " " + y)
));
}).ToList();
var vsStructs = vs.Where(x => structsNames.Contains(x.Type)).ToList();
vs = vs.Where(x => !structsNames.Contains(x.Type)).ToList();
var vsArrStructs = vsArr.Where(x => structsNames.Contains(x.Type)).ToList();
vsArr = vsArr.Where(x => !structsNames.Contains(x.Type)).ToList();
/* EXTRA MODES */
var extraModesEnum = "";
if (extraModes.Count > 0){
extraModesEnum = $@"public enum Mode {{ Main, {string.Join(", ", extraModes.Select(x => (Path.GetFileNameWithoutExtension(x) ?? "").Split('.').Last()))} }}
";
}
/* IF SHADER CONTAINS ALL OBJECTS MATRICES */
var matricesNames = vms.Select(x => x.Groups[1].Value.Substring(1)).ToList();
var matricesMode = matricesNames.Contains("World") && matricesNames.Contains("WorldInvTranspose") && matricesNames.Contains("WorldViewProj");
var screenSizeMode = vvsV4.Select(x => x.Groups[1].Value).Contains("gScreenSize");
#>
public class Effect<#= name #> : <#= string.Join(", ", new[]{
"IEffectWrapper",
matricesMode ? "IEffectMatricesWrapper" : null,
screenSizeMode ? "IEffectScreenSizeWrapper" : null,
}.Where(x => x != null)) #> {
<#= extraModesEnum #><#= string.Join("", structs) #><#= string.Join("", consts) #>private ShaderBytecode _b;
public Effect E;
public ShaderSignature <#= string.Join(", ", tls.Select(x => "InputSignature" + x.Type)) #>;
public InputLayout <#= string.Join(", ", tls.Select(x => "Layout" + x.Type)) #>;
public EffectReadyTechnique <#= string.Join(", ", ts.Select(x => "Tech" + x)) #>;
<# if (vms.Any()){ #>
[NotNull]
public EffectOnlyMatrixVariable <#= string.Join(", ", vms.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vmsArr.Any()){ #>
[NotNull]
public EffectOnlyMatrixArrayVariable <#= string.Join(", ", vmsArr.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vrs.Any()){ #>
[NotNull]
public EffectOnlyResourceVariable <#= string.Join(", ", vrs.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vrsArr.Any()){ #>
[NotNull]
public EffectOnlyResourceArrayVariable <#= string.Join(", ", vrsArr.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vssInt.Any()){ #>
[NotNull]
public EffectOnlyIntVariable <#= string.Join(", ", vssInt.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vssIntArr.Any()){ #>
[NotNull]
public EffectOnlyIntArrayVariable <#= string.Join(", ", vssIntArr.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vssFloat.Any()){ #>
[NotNull]
public EffectOnlyFloatVariable <#= string.Join(", ", vssFloat.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vssFloatArr.Any()){ #>
[NotNull]
public EffectOnlyFloatArrayVariable <#= string.Join(", ", vssFloatArr.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vssBool.Any()){ #>
[NotNull]
public EffectOnlyBoolVariable <#= string.Join(", ", vssBool.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vssBoolArr.Any()){ #>
[NotNull]
public EffectOnlyBoolArrayVariable <#= string.Join(", ", vssBoolArr.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vvsV2.Any()){ #>
[NotNull]
public EffectOnlyVector2Variable <#= string.Join(", ", vvsV2.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vvsV3.Any()){ #>
[NotNull]
public EffectOnlyVector3Variable <#= string.Join(", ", vvsV3.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vvsV4.Any()){ #>
[NotNull]
public EffectOnlyVector4Variable <#= string.Join(", ", vvsV4.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# if (vvsVArr.Any()){ #>
[NotNull]
public EffectOnlyVectorArrayVariable <#= string.Join(", ", vvsVArr.Select(x => ConvertFxName(x.Groups[1].Value)).Distinct()) #>;
<# } #>
<# foreach (var vsStruct in vsStructs){ #>
[NotNull]
public EffectStruct<#= vsStruct.Type #>Variable <#= ConvertFxName(vsStruct.Name) #>;
<# } #>
<# foreach (var vsStruct in vsArrStructs){ #>
[NotNull]
public EffectStruct<#= vsStruct.Type #>ArrayVariable <#= ConvertFxName(vsStruct.Name) #>;
<# } #>
<# if (vs.Any()){ #>
public EffectVariable <#= string.Join(", ", vs.Select(x => ConvertFxName(x.Name)).Distinct()) #>;
<# } #>
<# if (screenSizeMode){ #>
EffectOnlyVector4Variable IEffectScreenSizeWrapper.FxScreenSize => FxScreenSize;
<# } #>
<# if (matricesMode){ #>
EffectOnlyMatrixVariable IEffectMatricesWrapper.FxWorld => FxWorld;
EffectOnlyMatrixVariable IEffectMatricesWrapper.FxWorldInvTranspose => FxWorldInvTranspose;
EffectOnlyMatrixVariable IEffectMatricesWrapper.FxWorldViewProj => FxWorldViewProj;
<# } #>
<# if (extraModes.Count > 0){ #>
private Mode _mode = Mode.Main;
public Mode GetMode(){
return _mode;
}
public void SetMode(Mode mode, Device device){
if (mode == _mode) return;
_mode = mode;
if (_b != null) {
Dispose();
_b = null;
Initialize(device);
}
}
public void Initialize(Device device) {
if (_b != null) return;
_b = EffectUtils.Load(ShadersResourceManager.Manager, _mode == Mode.Main ? "<#= name #>" : "<#= name #>." + _mode);
<# } else { #>
public void Initialize(Device device) {
_b = EffectUtils.Load(ShadersResourceManager.Manager, "<#= name #>");
<# } #>
E = new Effect(device, _b);
<# foreach (var x in ts.Distinct()){ #>
Tech<#= x #> = new EffectReadyTechnique(E.GetTechniqueByName("<#= x #>"));
<# } #>
<# foreach (var x in tls.Distinct()){ var a = x.Type; var b = x.TechName; var p = x.Custom != null ? x.Custom : "InputLayouts.Vertice" + a; #>
for (var i = 0; i < Tech<#= b #>.Description.PassCount && InputSignature<#= a #> == null; i++) {
InputSignature<#= a #> = Tech<#= b #>.GetPassByIndex(i).Description.Signature;
}
if (InputSignature<#= a #> == null) throw new System.Exception("input signature (<#= name #>, <#= a #>, <#= b #>) == null");
Layout<#= a #> = new InputLayout(device, InputSignature<#= a #>, <#= p #>.InputElementsValue);
<# } #>
<# foreach (var x in vms.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyMatrixVariable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vmsArr.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyMatrixArrayVariable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vrs.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyResourceVariable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vrsArr.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyResourceArrayVariable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vssInt.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyIntVariable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vssIntArr.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyIntArrayVariable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vssFloat.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyFloatVariable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vssFloatArr.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyFloatArrayVariable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vssBool.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyBoolVariable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vssBoolArr.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyBoolArrayVariable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vvsV2.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyVector2Variable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vvsV3.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyVector3Variable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vvsV4.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyVector4Variable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vvsVArr.Distinct()){ #>
<#= ConvertFxName(x.Groups[1].Value) #> = new EffectOnlyVectorArrayVariable(E.GetVariableByName("<#= x.Groups[1].Value #>"));
<# } #>
<# foreach (var x in vsStructs.Distinct()){ #>
<#= ConvertFxName(x.Name) #> = new EffectStruct<#= x.Type #>Variable(E.GetVariableByName("<#= x.Name #>"));
<# } #>
<# foreach (var x in vsArrStructs.Distinct()){ #>
<#= ConvertFxName(x.Name) #> = new EffectStruct<#= x.Type #>ArrayVariable(E.GetVariableByName("<#= x.Name #>"));
<# } #>
<# foreach (var x in vs.Distinct()){ #>
<#= ConvertFxName(x.Name) #> = E.GetVariableByName("<#= x.Name #>");
<# } #>
}
public void Dispose() {
if (E == null) return;
<# foreach (var t in tls.Select(x => x.Type).Distinct()){ #>
DisposeHelper.Dispose(ref InputSignature<#= t #>);
DisposeHelper.Dispose(ref Layout<#= t #>);
<# } #>
DisposeHelper.Dispose(ref E);
DisposeHelper.Dispose(ref _b);
}
}
<#
}
#>
}