forked from IrisShaders/Iris
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathStandardMacros.java
More file actions
313 lines (268 loc) · 11.3 KB
/
Copy pathStandardMacros.java
File metadata and controls
313 lines (268 loc) · 11.3 KB
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
package net.irisshaders.iris.gl.shader;
import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.GlUtil;
import net.irisshaders.iris.Iris;
import net.irisshaders.iris.compat.dh.DHCompat;
import net.irisshaders.iris.helpers.StringPair;
import net.irisshaders.iris.pathways.HandRenderer;
import net.irisshaders.iris.pipeline.WorldRenderingPhase;
import net.irisshaders.iris.texture.format.TextureFormat;
import net.irisshaders.iris.texture.format.TextureFormatLoader;
import net.minecraft.Util;
import net.minecraftforge.fml.loading.LoadingModList;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20C;
import org.lwjgl.opengl.GL30C;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class StandardMacros {
private static final Pattern SEMVER_PATTERN = Pattern.compile("(?<major>\\d+)\\.(?<minor>\\d+)\\.*(?<bugfix>\\d*)(.*)");
private static void define(List<StringPair> defines, String key) {
defines.add(new StringPair(key, ""));
}
private static void define(List<StringPair> defines, String key, String value) {
defines.add(new StringPair(key, value));
}
public static ImmutableList<StringPair> createStandardEnvironmentDefines() {
ArrayList<StringPair> standardDefines = new ArrayList<>();
define(standardDefines, "MC_VERSION", getMcVersion());
define(standardDefines, "MC_GL_VERSION", getGlVersion(GL20C.GL_VERSION));
define(standardDefines, "MC_GLSL_VERSION", getGlVersion(GL20C.GL_SHADING_LANGUAGE_VERSION));
define(standardDefines, getOsString());
define(standardDefines, getVendor());
define(standardDefines, getRenderer());
define(standardDefines, "IS_IRIS");
define(standardDefines, "IS_OCULUS");
if (LoadingModList.get().getModFileById("distanthorizons") != null && DHCompat.hasRenderingEnabled()) {
define(standardDefines, "DISTANT_HORIZONS");
}
if (Iris.getIrisConfig().shouldAllowUnknownShaders()) {
define(standardDefines, "ALLOWS_UNKNOWN_SHADERS");
}
define(standardDefines, "DH_BLOCK_UNKNOWN", String.valueOf(0));
define(standardDefines, "DH_BLOCK_LEAVES", String.valueOf(1));
define(standardDefines, "DH_BLOCK_STONE", String.valueOf(2));
define(standardDefines, "DH_BLOCK_WOOD", String.valueOf(3));
define(standardDefines, "DH_BLOCK_METAL", String.valueOf(4));
define(standardDefines, "DH_BLOCK_DIRT", String.valueOf(5));
define(standardDefines, "DH_BLOCK_LAVA", String.valueOf(6));
define(standardDefines, "DH_BLOCK_DEEPSLATE", String.valueOf(7));
define(standardDefines, "DH_BLOCK_SNOW", String.valueOf(8));
define(standardDefines, "DH_BLOCK_SAND", String.valueOf(9));
define(standardDefines, "DH_BLOCK_TERRACOTTA", String.valueOf(10));
define(standardDefines, "DH_BLOCK_NETHER_STONE", String.valueOf(11));
define(standardDefines, "DH_BLOCK_WATER", String.valueOf(12));
define(standardDefines, "DH_BLOCK_GRASS", String.valueOf(13));
define(standardDefines, "DH_BLOCK_AIR", String.valueOf(14));
define(standardDefines, "DH_BLOCK_ILLUMINATED", String.valueOf(15));
for (String glExtension : getGlExtensions()) {
define(standardDefines, glExtension);
}
define(standardDefines, "MC_NORMAL_MAP");
define(standardDefines, "MC_SPECULAR_MAP");
define(standardDefines, "MC_RENDER_QUALITY", "1.0");
define(standardDefines, "MC_SHADOW_QUALITY", "1.0");
define(standardDefines, "MC_HAND_DEPTH", Float.toString(HandRenderer.DEPTH));
TextureFormat textureFormat = TextureFormatLoader.getFormat();
if (textureFormat != null) {
for (String define : textureFormat.getDefines()) {
define(standardDefines, define);
}
}
getRenderStages().forEach((stage, index) -> define(standardDefines, stage, index));
for (String irisDefine : getIrisDefines()) {
define(standardDefines, irisDefine);
}
return ImmutableList.copyOf(standardDefines);
}
/**
* Gets the current mc version String in a 5 digit format
*
* @return mc version string
* @see <a href="https://github.com/sp614x/optifine/blob/9c6a5b5326558ccc57c6490b66b3be3b2dc8cbef/OptiFineDoc/doc/shaders.txt#L696-L699">Optifine Doc</a>
*/
public static String getMcVersion() {
String version = Iris.getReleaseTarget();
if (version == null) {
throw new IllegalStateException("Could not get the current minecraft version!");
}
String[] splitVersion = version.split("\\.");
if (splitVersion.length < 2) {
Iris.logger.error("Could not parse game version \"" + version + "\"");
splitVersion = Iris.getBackupVersionNumber().split("\\.");
}
String major = splitVersion[0];
String minor = splitVersion[1];
String bugfix;
if (splitVersion.length < 3) {
bugfix = "00";
} else {
bugfix = splitVersion[2];
}
if (minor.length() == 1) {
minor = 0 + minor;
}
if (bugfix.length() == 1) {
bugfix = 0 + bugfix;
}
return major + minor + bugfix;
}
/**
* Returns the current GL Version using regex
*
* @param name the name of the gl attribute to parse
* @return current gl version stripped of semantic versioning
* @see <a href="https://github.com/sp614x/optifine/blob/9c6a5b5326558ccc57c6490b66b3be3b2dc8cbef/OptiFineDoc/doc/shaders.txt#L701-L703">Optifine Doc for GL Version</a>
* @see <a href="https://github.com/sp614x/optifine/blob/9c6a5b5326558ccc57c6490b66b3be3b2dc8cbef/OptiFineDoc/doc/shaders.txt#L705-L707">Optifine Doc for GLSL Version</a>
*/
public static String getGlVersion(int name) {
String info = GlStateManager._getString(name);
Matcher matcher = SEMVER_PATTERN.matcher(Objects.requireNonNull(info));
if (!matcher.matches()) {
throw new IllegalStateException("Could not parse GL version from \"" + info + "\"");
}
String major = group(matcher, "major");
String minor = group(matcher, "minor");
String bugfix = group(matcher, "bugfix");
if (bugfix == null) {
// if bugfix is not there, it is 0
bugfix = "0";
}
if (major == null || minor == null) {
throw new IllegalStateException("Could not parse GL version from \"" + info + "\"");
}
return major + minor + bugfix;
}
/**
* Expanded version of {@link Matcher#group(String)} that does not throw an exception.
* If the argument is incorrect (normally resulting in an exception), it returns null
*
* @param matcher matcher to check the group by
* @param name name of the group
* @return the section of the matcher that is a group, or null, if that matcher does not contain said group
*/
public static String group(Matcher matcher, String name) {
try {
return matcher.group(name);
} catch (IllegalArgumentException | IllegalStateException exception) {
return null;
}
}
/**
* Returns the current OS String
*
* @return the string based on the current OS
* @see <a href="https://github.com/sp614x/optifine/blob/9c6a5b5326558ccc57c6490b66b3be3b2dc8cbef/OptiFineDoc/doc/shaders.txt#L709-L714">Optifine Doc</a>
*/
public static String getOsString() {
return switch (Util.getPlatform()) {
case OSX -> "MC_OS_MAC";
case LINUX -> "MC_OS_LINUX";
case WINDOWS ->
"MC_OS_WINDOWS"; // Note: Optifine doesn't have a macro for Solaris. https://github.com/sp614x/optifine/blob/9c6a5b5326558ccc57c6490b66b3be3b2dc8cbef/OptiFineDoc/doc/shaders.txt#L709-L714
default -> "MC_OS_UNKNOWN";
};
}
/**
* Returns a string indicating the graphics card being used
*
* @return the graphics card prefixed with "MC_GL_VENDOR_"
* @see <a href="https://github.com/sp614x/optifine/blob/9c6a5b5326558ccc57c6490b66b3be3b2dc8cbef/OptiFineDoc/doc/shaders.txt#L716-L723">Optifine Doc</a>
*/
public static String getVendor() {
String vendor = Objects.requireNonNull(GlUtil.getVendor()).toLowerCase(Locale.ROOT);
if (vendor.startsWith("ati")) {
return "MC_GL_VENDOR_ATI";
} else if (vendor.startsWith("intel")) {
return "MC_GL_VENDOR_INTEL";
} else if (vendor.startsWith("nvidia")) {
return "MC_GL_VENDOR_NVIDIA";
} else if (vendor.startsWith("amd")) {
return "MC_GL_VENDOR_AMD";
} else if (vendor.startsWith("x.org")) {
return "MC_GL_VENDOR_XORG";
}
return "MC_GL_VENDOR_OTHER";
}
/**
* Returns the graphics driver being used
*
* @return graphics driver prefixed with "MC_GL_RENDERER_"
* @see <a href="https://github.com/sp614x/optifine/blob/9c6a5b5326558ccc57c6490b66b3be3b2dc8cbef/OptiFineDoc/doc/shaders.txt#L725-L733">Optifine Doc</a>
*/
public static String getRenderer() {
String renderer = Objects.requireNonNull(GlUtil.getRenderer()).toLowerCase(Locale.ROOT);
if (renderer.startsWith("amd")) {
return "MC_GL_RENDERER_RADEON";
} else if (renderer.startsWith("ati")) {
return "MC_GL_RENDERER_RADEON";
} else if (renderer.startsWith("radeon")) {
return "MC_GL_RENDERER_RADEON";
} else if (renderer.startsWith("gallium")) {
return "MC_GL_RENDERER_GALLIUM";
} else if (renderer.startsWith("intel")) {
return "MC_GL_RENDERER_INTEL";
} else if (renderer.startsWith("geforce")) {
return "MC_GL_RENDERER_GEFORCE";
} else if (renderer.startsWith("nvidia")) {
return "MC_GL_RENDERER_GEFORCE";
} else if (renderer.startsWith("quadro")) {
return "MC_GL_RENDERER_QUADRO";
} else if (renderer.startsWith("nvs")) {
return "MC_GL_RENDERER_QUADRO";
} else if (renderer.startsWith("mesa")) {
return "MC_GL_RENDERER_MESA";
}
return "MC_GL_RENDERER_OTHER";
}
/**
* Returns the list of currently enabled GL extensions
* This is done by calling {@link GL11#glGetString} with the arg {@link GL11#GL_EXTENSIONS}
*
* @return set of activated extensions prefixed with "MC_"
* @see <a href="https://github.com/sp614x/optifine/blob/9c6a5b5326558ccc57c6490b66b3be3b2dc8cbef/OptiFineDoc/doc/shaders.txt#L735-L738">Optifine Doc</a>
*/
public static Set<String> getGlExtensions() {
// In OpenGL Core, we must use a new way of retrieving extensions.
int numExtensions = GL30C.glGetInteger(GL30C.GL_NUM_EXTENSIONS);
String[] extensions = new String[numExtensions];
for (int i = 0; i < numExtensions; i++) {
extensions[i] = GL30C.glGetStringi(GL30C.GL_EXTENSIONS, i);
}
// TODO note that we do not add extensions based on if the shader uses them and if they are supported
// see https://github.com/sp614x/optifine/blob/master/OptiFineDoc/doc/shaders.txt#L738
// NB: Use Collectors.toSet(). In some cases, there are duplicate extensions in the extension list.
// RenderDoc is one example - it causes the GL_KHR_debug extension to appear twice:
//
// https://github.com/IrisShaders/Iris/issues/971
return Arrays.stream(extensions).map(s -> "MC_" + s).collect(Collectors.toSet());
}
public static Map<String, String> getRenderStages() {
Map<String, String> stages = new HashMap<>();
for (WorldRenderingPhase phase : WorldRenderingPhase.values()) {
stages.put("MC_RENDER_STAGE_" + phase.name(), String.valueOf(phase.ordinal()));
}
return stages;
}
/**
* Returns the list of Iris-exclusive uniforms supported in the current version of Iris.
*
* @return List of definitions corresponding to the uniform names prefixed with "MC_"
*/
public static List<String> getIrisDefines() {
List<String> defines = new ArrayList<>();
// All Iris-exclusive uniforms should have a corresponding definition here. Example:
// defines.add("MC_UNIFORM_DRAGON_DEATH_PROGRESS");
return defines;
}
}