forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderProfile.DirectX.cs
73 lines (61 loc) · 2.86 KB
/
ShaderProfile.DirectX.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
// 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.Linq;
using System.Text.RegularExpressions;
namespace TwoMGFX
{
class DirectX11ShaderProfile : ShaderProfile
{
private static readonly Regex HlslPixelShaderRegex = new Regex(@"^ps_(?<major>1|2|3|4|5)_(?<minor>0|1|)(_level_(9_1|9_2|9_3))?$", RegexOptions.Compiled);
private static readonly Regex HlslVertexShaderRegex = new Regex(@"^vs_(?<major>1|2|3|4|5)_(?<minor>0|1|)(_level_(9_1|9_2|9_3))?$", RegexOptions.Compiled);
public DirectX11ShaderProfile()
: base("DirectX_11", 1)
{
}
internal override void AddMacros(Dictionary<string, string> macros)
{
macros.Add("HLSL", "1");
macros.Add("SM4", "1");
}
internal override void ValidateShaderModels(PassInfo pass)
{
int major, minor;
if (!string.IsNullOrEmpty(pass.vsFunction))
{
ParseShaderModel(pass.vsModel, HlslVertexShaderRegex, out major, out minor);
if (major <= 3)
throw new Exception(String.Format("Invalid profile '{0}'. Vertex shader '{1}' must be SM 4.0 level 9.1 or higher!", pass.vsModel, pass.vsFunction));
}
if (!string.IsNullOrEmpty(pass.psFunction))
{
ParseShaderModel(pass.psModel, HlslPixelShaderRegex, out major, out minor);
if (major <= 3)
throw new Exception(String.Format("Invalid profile '{0}'. Pixel shader '{1}' must be SM 4.0 level 9.1 or higher!", pass.vsModel, pass.psFunction));
}
}
internal override ShaderData CreateShader(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings)
{
var bytecode = EffectObject.CompileHLSL(shaderInfo, shaderFunction, shaderProfile, ref errorsAndWarnings);
// First look to see if we already created this same shader.
foreach (var shader in effect.Shaders)
{
if (bytecode.SequenceEqual(shader.Bytecode))
return shader;
}
var shaderData = ShaderData.CreateHLSL(bytecode, isVertexShader, effect.ConstantBuffers, effect.Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug);
effect.Shaders.Add(shaderData);
return shaderData;
}
internal override bool Supports(string platform)
{
if (platform == "Windows" ||
platform == "WindowsPhone8" ||
platform == "WindowsStoreApp")
return true;
return false;
}
}
}