forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderProfile.OpenGL.cs
80 lines (67 loc) · 3.1 KB
/
ShaderProfile.OpenGL.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
// 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;
using TwoMGFX.TPGParser;
namespace TwoMGFX
{
class OpenGLShaderProfile : ShaderProfile
{
private static readonly Regex GlslPixelShaderRegex = new Regex(@"^ps_(?<major>1|2|3|4|5)_(?<minor>0|1|)$", RegexOptions.Compiled);
private static readonly Regex GlslVertexShaderRegex = new Regex(@"^vs_(?<major>1|2|3|4|5)_(?<minor>0|1|)$", RegexOptions.Compiled);
public OpenGLShaderProfile()
: base("OpenGL", 0)
{
}
internal override void AddMacros(Dictionary<string, string> macros)
{
macros.Add("GLSL", "1");
macros.Add("OPENGL", "1");
}
internal override void ValidateShaderModels(PassInfo pass)
{
int major, minor;
if (!string.IsNullOrEmpty(pass.vsFunction))
{
ParseShaderModel(pass.vsModel, GlslVertexShaderRegex, out major, out minor);
if (major > 3)
throw new Exception(String.Format("Invalid profile '{0}'. Vertex shader '{1}' must be SM 3.0 or lower!", pass.vsModel, pass.vsFunction));
}
if (!string.IsNullOrEmpty(pass.psFunction))
{
ParseShaderModel(pass.psModel, GlslPixelShaderRegex, out major, out minor);
if (major > 3)
throw new Exception(String.Format("Invalid profile '{0}'. Pixel shader '{1}' must be SM 3.0 or lower!", pass.vsModel, pass.psFunction));
}
}
internal override ShaderData CreateShader(ShaderResult shaderResult, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings)
{
// For now GLSL is only supported via translation
// using MojoShader which works from HLSL bytecode.
var bytecode = EffectObject.CompileHLSL(shaderResult, 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 shaderInfo = shaderResult.ShaderInfo;
var shaderData = ShaderData.CreateGLSL(bytecode, isVertexShader, effect.ConstantBuffers, effect.Shaders.Count, shaderInfo.SamplerStates, shaderResult.Debug);
effect.Shaders.Add(shaderData);
return shaderData;
}
internal override bool Supports(string platform)
{
if (platform == "iOS" ||
platform == "Android" ||
platform == "DesktopGL" ||
platform == "MacOSX" ||
platform == "RaspberryPi")
return true;
return false;
}
}
}