forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderProfile.cs
107 lines (88 loc) · 3.48 KB
/
ShaderProfile.cs
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
// MonoGame - Copyright (C) The MonoGame Team
// 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.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Xna.Framework.Content.Pipeline;
using TwoMGFX.TPGParser;
namespace TwoMGFX
{
[TypeConverter(typeof(StringConverter))]
public abstract class ShaderProfile
{
private static readonly LoadedTypeCollection<ShaderProfile> _profiles = new LoadedTypeCollection<ShaderProfile>();
protected ShaderProfile(string name, byte formatId)
{
Name = name;
FormatId = formatId;
}
public static readonly ShaderProfile OpenGL = FromName("OpenGL");
public static readonly ShaderProfile DirectX_11 = FromName("DirectX_11");
/// <summary>
/// Returns all the loaded shader profiles.
/// </summary>
public static IEnumerable<ShaderProfile> All
{
get { return _profiles; }
}
/// <summary>
/// Returns the name of the shader profile.
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Returns the format identifier used in the MGFX file format.
/// </summary>
public byte FormatId { get; private set; }
/// <summary>
/// Returns the correct profile for the named platform or
/// null if no supporting profile is found.
/// </summary>
public static ShaderProfile ForPlatform(string platform)
{
return _profiles.FirstOrDefault(p => p.Supports(platform));
}
/// <summary>
/// Returns the profile by name or null if no match is found.
/// </summary>
public static ShaderProfile FromName(string name)
{
return _profiles.FirstOrDefault(p => p.Name == name);
}
internal abstract void AddMacros(Dictionary<string, string> macros);
internal abstract void ValidateShaderModels(PassInfo pass);
internal abstract ShaderData CreateShader(ShaderResult shaderResult, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings);
internal abstract bool Supports(string platform);
protected static void ParseShaderModel(string text, Regex regex, out int major, out int minor)
{
var match = regex.Match(text);
if (!match.Success)
{
major = 0;
minor = 0;
return;
}
major = int.Parse(match.Groups["major"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture);
minor = int.Parse(match.Groups["minor"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture);
}
private class StringConverter : TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
var name = value as string;
foreach (var e in All)
{
if (e.Name == name)
return e;
}
}
return base.ConvertFrom(context, culture, value);
}
}
}
}