Skip to content

Commit

Permalink
Move custom texture image loading to pack init
Browse files Browse the repository at this point in the history
  • Loading branch information
Justsnoopy30 committed Nov 27, 2021
1 parent 9dadf81 commit fa90159
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,24 @@ public DeferredWorldRenderingPipeline(ProgramSet programs) {
normals = new NativeImageBackedSingleColorTexture(127, 127, 255, 255);
specular = new NativeImageBackedSingleColorTexture(0, 0, 0, 0);

programs.getPack().getCustomTextureDataMap().forEach((textureStage, customTextureDataMap) -> {
Object2ObjectMap<String, IntSupplier> customTextureIds = new Object2ObjectOpenHashMap<>();
customTextureDataMap.forEach((samplerName, textureData) -> {
try {
if (textureData instanceof CustomTextureData.PngData) {
AbstractTexture customTexture = new NativeImageBackedCustomTexture((CustomTextureData.PngData) textureData);
customTextureIds.put(samplerName, customTexture::getId);
} else if (textureData instanceof CustomTextureData.ResourceData) {
customTextureIds.put(samplerName, ((CustomTextureData.ResourceData) textureData)::getGlId);
}
} catch (IOException e) {
Iris.logger.error("Unable to parse the image data for the custom texture on sampler " + samplerName, e);
}
});

customTextureIdMap.put(textureStage, customTextureIds);
});

noise = programs.getPack().getCustomNoiseTexture().flatMap(textureData -> {
try {
// TODO: Support CustomTextureData types other than PngData
Expand All @@ -192,25 +210,6 @@ public DeferredWorldRenderingPipeline(ProgramSet programs) {
return new NativeImageBackedNoiseTexture(noiseTextureResolution);
});

programs.getPackDirectives().getCustomTextureData().forEach((textureStage, customTexturePropertiesMap) -> {
Object2ObjectMap<String, IntSupplier> customTextureIds = customTextureIdMap.getOrDefault(textureStage, Object2ObjectMaps.emptyMap());
customTexturePropertiesMap.forEach((samplerName, path) -> {
try {
CustomTextureData textureData = programs.getPack().readTexture(path);
if (textureData instanceof CustomTextureData.PngData) {
AbstractTexture customTexture = new NativeImageBackedCustomTexture((CustomTextureData.PngData) textureData);
customTextureIds.put(samplerName, customTexture::getId);
} else if (textureData instanceof CustomTextureData.ResourceData) {
customTextureIds.put(samplerName, ((CustomTextureData.ResourceData) textureData)::getGlId);
}
} catch (IOException e) {
Iris.logger.error("Unable to read the custom texture at " + path, e);
}
});

customTextureIdMap.put(textureStage, customTextureIds);
});

GlStateManager._activeTexture(GL20C.GL_TEXTURE0);

// TODO: Change this once earlier passes are implemented.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.coderbot.iris.Iris;
import net.coderbot.iris.shaderpack.texture.TextureStage;

import java.util.Set;

Expand All @@ -17,7 +16,6 @@ public class PackDirectives {
private boolean areCloudsEnabled;
private boolean separateAo;
private boolean oldLighting;
private Object2ObjectMap<TextureStage, Object2ObjectMap<String, String>> customTextureDataMap = new Object2ObjectOpenHashMap<>();
private Object2ObjectMap<String, Object2BooleanMap<String>> explicitFlips = new Object2ObjectOpenHashMap<>();

private final PackRenderTargetDirectives renderTargetDirectives;
Expand All @@ -36,7 +34,6 @@ private PackDirectives(Set<Integer> supportedRenderTargets) {
areCloudsEnabled = properties.areCloudsEnabled();
separateAo = properties.getSeparateAo().orElse(false);
oldLighting = properties.getOldLighting().orElse(false);
customTextureDataMap = properties.getCustomTextureData();
explicitFlips = properties.getExplicitFlips();
}

Expand All @@ -45,7 +42,6 @@ private PackDirectives(Set<Integer> supportedRenderTargets) {
areCloudsEnabled = directives.areCloudsEnabled();
separateAo = directives.separateAo;
oldLighting = directives.oldLighting;
customTextureDataMap = directives.customTextureDataMap;
explicitFlips = directives.explicitFlips;
}

Expand Down Expand Up @@ -81,10 +77,6 @@ public PackShadowDirectives getShadowDirectives() {
return shadowDirectives;
}

public Object2ObjectMap<TextureStage, Object2ObjectMap<String, String>> getCustomTextureData() {
return customTextureDataMap;
}

public void acceptDirectivesFrom(DirectiveHolder directives) {
renderTargetDirectives.acceptDirectives(directives);
shadowDirectives.acceptDirectives(directives);
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/net/coderbot/iris/shaderpack/ShaderPack.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package net.coderbot.iris.shaderpack;

import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.coderbot.iris.Iris;
import net.coderbot.iris.shaderpack.texture.CustomTextureData;
import net.coderbot.iris.shaderpack.texture.TextureFilteringData;
import net.coderbot.iris.shaderpack.texture.TextureStage;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

Expand All @@ -23,6 +26,7 @@ public class ShaderPack {

private final IdMap idMap;
private final LanguageMap languageMap;
private final Object2ObjectMap<TextureStage, Object2ObjectMap<String, CustomTextureData>> customTextureDataMap = new Object2ObjectOpenHashMap<>();
private final CustomTextureData customNoiseTexture;

/**
Expand Down Expand Up @@ -58,6 +62,19 @@ public ShaderPack(Path root) throws IOException {
return null;
}
}).orElse(null);

shaderProperties.getCustomTextures().forEach((textureStage, customTexturePropertiesMap) -> {
Object2ObjectMap<String, CustomTextureData> innerCustomTextureDataMap = new Object2ObjectOpenHashMap<>();
customTexturePropertiesMap.forEach((samplerName, path) -> {
try {
innerCustomTextureDataMap.put(samplerName, readTexture(path));
} catch (IOException e) {
Iris.logger.error("Unable to read the custom texture at " + path, e);
}
});

customTextureDataMap.put(textureStage, innerCustomTextureDataMap);
});
}

@Nullable
Expand Down Expand Up @@ -88,6 +105,7 @@ private static Optional<Properties> loadProperties(Path shaderPath, String name)
return Optional.of(properties);
}

// TODO: Implement raw texture data types
public CustomTextureData readTexture(String path) throws IOException {
CustomTextureData customTextureData;
if (path.contains(":") && ResourceLocation.isValidResourceLocation(path)) {
Expand Down Expand Up @@ -130,6 +148,10 @@ public IdMap getIdMap() {
return idMap;
}

public Object2ObjectMap<TextureStage, Object2ObjectMap<String, CustomTextureData>> getCustomTextureDataMap() {
return customTextureDataMap;
}

public Optional<CustomTextureData> getCustomNoiseTexture() {
return Optional.ofNullable(customNoiseTexture);
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/net/coderbot/iris/shaderpack/ShaderProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ShaderProperties {
private final Object2ObjectMap<String, AlphaTestOverride> alphaTestOverrides = new Object2ObjectOpenHashMap<>();
private final Object2FloatMap<String> viewportScaleOverrides = new Object2FloatOpenHashMap<>();
private final ObjectSet<String> blendDisabled = new ObjectOpenHashSet<>();
private final Object2ObjectMap<TextureStage, Object2ObjectMap<String, String>> customTextureDataMap = new Object2ObjectOpenHashMap<>();
private final Object2ObjectMap<TextureStage, Object2ObjectMap<String, String>> customTextures = new Object2ObjectOpenHashMap<>();
private final Object2ObjectMap<String, Object2BooleanMap<String>> explicitFlips = new Object2ObjectOpenHashMap<>();
private String noiseTexturePath = null;

Expand Down Expand Up @@ -180,10 +180,10 @@ public ShaderProperties(Properties properties) {

TextureStage stage = optionalTextureStage.get();

Object2ObjectMap<String, String> customTexturePropertyMap = customTextureDataMap.getOrDefault(stage, new Object2ObjectOpenHashMap<>());
Object2ObjectMap<String, String> customTexturePropertyMap = customTextures.getOrDefault(stage, new Object2ObjectOpenHashMap<>());
customTexturePropertyMap.put(samplerName, value);

customTextureDataMap.put(stage, customTexturePropertyMap);
customTextures.put(stage, customTexturePropertyMap);
});

handleTwoArgDirective("flip.", key, value, (pass, buffer) -> {
Expand Down Expand Up @@ -336,12 +336,12 @@ public ObjectSet<String> getBlendDisabled() {
return blendDisabled;
}

public Optional<String> getNoiseTexturePath() {
return Optional.ofNullable(noiseTexturePath);
public Object2ObjectMap<TextureStage, Object2ObjectMap<String, String>> getCustomTextures() {
return customTextures;
}

public Object2ObjectMap<TextureStage, Object2ObjectMap<String, String>> getCustomTextureData() {
return customTextureDataMap;
public Optional<String> getNoiseTexturePath() {
return Optional.ofNullable(noiseTexturePath);
}

public Object2ObjectMap<String, Object2BooleanMap<String>> getExplicitFlips() {
Expand Down

0 comments on commit fa90159

Please sign in to comment.