-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathShaderLoader.cs
146 lines (126 loc) · 5.23 KB
/
ShaderLoader.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
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
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace KSP_PostProcessing
{
// Mostly borrowed from https://github.com/Kopernicus/Kopernicus/blob/master/src/Kopernicus.Components/ShaderLoader.cs
// Credit and lots of thanks goes to Thomas P.
/// <summary>
/// Responsible for loading, indexing and selecting all KS3P shaders.
/// </summary>
public static class ShaderLoader
{
/// <summary>
/// The collection of all shaders.
/// </summary>
static Dictionary<string, Shader> shaderDictionary = new Dictionary<string, Shader>();
/// <summary>
/// The collection of all compute shaders.
/// </summary>
static Dictionary<string, ComputeShader> computeShaderDictionary = new Dictionary<string, ComputeShader>();
public static ComputeShader GetComputeShader(string shaderName)
{
Debug.Log("[KS3P]: Searching for compute shader [" + shaderName + "].");
if (computeShaderDictionary.ContainsKey(shaderName))
{
return computeShaderDictionary[shaderName];
}
else
{
// If we reach this part, we have found no shader
Debug.LogError("[KS3P]: No compute shader found with name [" + shaderName + "].");
return null;
}
}
public static Shader GetShader(string shaderName)
{
Debug.Log("[KS3P]: Searching for shader [" + shaderName + "].");
if (shaderDictionary.ContainsKey(shaderName))
{
return shaderDictionary[shaderName];
}
else
{
// If we reach this part, we have found no shader
Debug.LogError("[KS3P]: No shader found with name [" + shaderName + "].");
return null;
}
}
public static void LoadShaders(ref List<string> log)
{
string path = Path.Combine(KS3PUtil.Root, "GameData");
path = Path.Combine(path, "KS3P");
path = Path.Combine(path, "Shaders");
// gather GPU data
string gpuString = SystemInfo.graphicsDeviceVersion;
// check DX11
if(gpuString.Contains("Direct3D 11.0"))
{
// inject dx11
KS3P.Log("DX11 preference detected, loading DX11 designed shaders (big thanks to forum user jrodriguez!)", ref log);
path = Path.Combine(path, "postprocessingshaders-dx11");
}
else
{
// merge path but don't add dx11 targeting
path = Path.Combine(path, "postprocessingshaders");
}
// get target platform AND check OpenGL
if (Application.platform == RuntimePlatform.WindowsPlayer)
{
if (gpuString.StartsWith("OpenGL"))
{
KS3P.Log("OpenGL preference detected, responding appropriately.", ref log);
path += "-linux.unity3d"; // For OpenGL users on Windows we load the Linux shaders to fix OpenGL issues
}
else
{
path += "-windows.unity3d";
}
}
else if (Application.platform == RuntimePlatform.LinuxPlayer)
{
path += "-linux.unity3d";
}
else
{
path += "-macosx.unity3d";
}
// target bundle finalized. Let's roll.
KS3P.Log("Loading asset bundle at path " + path, ref log);
using (WWW www = new WWW("file://" + path))
{
AssetBundle bundle = www.assetBundle;
// load shaders
Shader[] shaders = bundle.LoadAllAssets<Shader>();
foreach (Shader shader in shaders)
{
if(shaderDictionary.ContainsKey(shader.name))
{
KS3P.Warning("Blocking duplicate shader [" + shader.name + "].", ref log);
}
else
{
KS3P.Warning("Adding shader [" + shader.name + "].", ref log);
shaderDictionary.Add(shader.name, shader);
}
}
// load compute shaders
ComputeShader[] computeshaders = bundle.LoadAllAssets<ComputeShader>();
foreach(ComputeShader cShader in computeshaders)
{
if(computeShaderDictionary.ContainsKey(cShader.name))
{
KS3P.Warning("Blocking duplicate compute shader [" + cShader.name + "].", ref log);
}
else
{
KS3P.Log("Adding compute shader [" + cShader.name + "].", ref log);
computeShaderDictionary.Add(cShader.name, cShader);
}
}
bundle.Unload(false);
}
}
}
}