Skip to content

Commit

Permalink
With this one weird trick, you too can lose 100 uniforms
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Nov 27, 2023
1 parent a972c91 commit a51c9d2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
28 changes: 19 additions & 9 deletions src/main/java/net/coderbot/iris/shaderpack/ShaderPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,17 @@
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -75,7 +85,7 @@ public class ShaderPack {
private List<String> dimensionIds;
private Map<NamespacedId, String> dimensionMap;

public ShaderPack(Path root, Iterable<StringPair> environmentDefines) throws IOException, IllegalStateException {
public ShaderPack(Path root, ImmutableList<StringPair> environmentDefines) throws IOException, IllegalStateException {
this(root, Collections.emptyMap(), environmentDefines);
}

Expand All @@ -89,11 +99,13 @@ public ShaderPack(Path root, Iterable<StringPair> environmentDefines) throws IOE
* have completed, and there is no need to hold on to the path for that reason.
* @throws IOException if there are any IO errors during shader pack loading.
*/
public ShaderPack(Path root, Map<String, String> changedConfigs, Iterable<StringPair> environmentDefines) throws IOException, IllegalStateException {
public ShaderPack(Path root, Map<String, String> changedConfigs, ImmutableList<StringPair> environmentDefines) throws IOException, IllegalStateException {
// A null path is not allowed.
Objects.requireNonNull(root);


ArrayList<StringPair> envDefines1 = new ArrayList<>(environmentDefines);
envDefines1.addAll(IrisDefines.createIrisReplacements());
environmentDefines = ImmutableList.copyOf(envDefines1);
ImmutableList.Builder<AbsolutePackPath> starts = ImmutableList.builder();
ImmutableList<String> potentialFileNames = ShaderPackSourceNames.POTENTIAL_STARTS;

Expand Down Expand Up @@ -153,10 +165,9 @@ public ShaderPack(Path root, Map<String, String> changedConfigs, Iterable<String
this.shaderPackOptions = new ShaderPackOptions(graph, changedConfigs);
graph = this.shaderPackOptions.getIncludes();

Iterable<StringPair> replacements = IrisDefines.createIrisReplacements();
Iterable<StringPair> finalEnvironmentDefines = environmentDefines;
this.shaderProperties = loadProperties(root, "shaders.properties")
.map(source -> new ShaderProperties(source, shaderPackOptions, finalEnvironmentDefines, replacements))
.map(source -> new ShaderProperties(source, shaderPackOptions, finalEnvironmentDefines))
.orElseGet(ShaderProperties::empty);

activeFeatures = new HashSet<>();
Expand Down Expand Up @@ -201,7 +212,7 @@ public ShaderPack(Path root, Map<String, String> changedConfigs, Iterable<String
List<String> optionalFeatureFlags = shaderProperties.getOptionalFeatureFlags().stream().filter(flag -> !FeatureFlags.isInvalid(flag)).collect(Collectors.toList());

if (!optionalFeatureFlags.isEmpty()) {

optionalFeatureFlags.forEach(flag -> Iris.logger.warn("Found flag " + flag));
optionalFeatureFlags.forEach(flag -> newEnvDefines.add(new StringPair("IRIS_FEATURE_" + flag, "")));

}
Expand Down Expand Up @@ -241,8 +252,7 @@ public ShaderPack(Path root, Map<String, String> changedConfigs, Iterable<String
IncludeProcessor includeProcessor = new IncludeProcessor(graph);

// Set up our source provider for creating ProgramSets
ArrayList<StringPair> finalEnvironmentDefines1 = new ArrayList<>((Collection) finalEnvironmentDefines);
finalEnvironmentDefines1.addAll(IrisDefines.createIrisReplacements());
Iterable<StringPair> finalEnvironmentDefines1 = environmentDefines;
this.sourceProvider = (path) -> {
String pathString = path.getPathString();
// Removes the first "/" in the path if present, and the file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ private ShaderProperties() {
}

// TODO: Is there a better solution than having ShaderPack pass a root path to ShaderProperties to be able to read textures?
public ShaderProperties(String contents, ShaderPackOptions shaderPackOptions, Iterable<StringPair> environmentDefines, Iterable<StringPair> replacements) {
for (StringPair pair : replacements) {
contents = contents.replaceAll("\\b" + pair.getKey() + "\\b", pair.getValue());
}

public ShaderProperties(String contents, ShaderPackOptions shaderPackOptions, Iterable<StringPair> environmentDefines) {
String preprocessedContents = PropertiesPreprocessor.preprocessSource(contents, shaderPackOptions, environmentDefines);

Properties preprocessed = new OrderBackedProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,7 @@ private static String process(Preprocessor preprocessor, String source) {
// Not super efficient, but this removes trailing whitespace on lines, fixing an issue with whitespace after
// line continuations (see PreprocessorTest#testWeirdPropertiesLineContinuation)
// Required for Voyager Shader
source = Arrays.stream(source.split("\\R")).map(String::trim)
.map(line -> {
if (line.startsWith("#")) {
// In PropertyCollectingListener we suppress "unknown preprocessor directive errors" and
// assume the line to be a comment, since in .properties files `#` also functions as a comment
// marker.
return line;
} else {
// This is a hack to ensure that non-macro lines don't have any preprocessing applied...
// In properties files, we don't substitute #define values except on macro lines.
return "#warning IRIS_PASSTHROUGH " + line;
}
}).collect(Collectors.joining("\n")) + "\n";
source = Arrays.stream(source.split("\\R")).map(String::trim).collect(Collectors.joining("\n")) + "\n";
// TODO: This is a horrible fix to trick the preprocessor into not seeing the backslashes during processing. We need a better way to do this.
source = source.replace("\\", "IRIS_PASSTHROUGHBACKSLASH");

Expand Down

0 comments on commit a51c9d2

Please sign in to comment.