Allow mods to specify shader import namespace (#9021)

This commit is contained in:
Monica S authored and GitHub committed 2022-09-08 16:10:31 +01:00
1 parent 6d233ac75c
commit 82026d11a4
8 files changed
+156

No files matched your search

@@ -30,3 +30,28 @@
InputStream inputstream = resource.m_215507_();
try {
@@ -232,11 +_,11 @@
private final Set<String> f_173369_ = Sets.newHashSet();
public String m_142138_(boolean p_173374_, String p_173375_) {
- p_173375_ = FileUtil.m_179924_((p_173374_ ? s1 : "shaders/include/") + p_173375_);
- if (!this.f_173369_.add(p_173375_)) {
+ // FORGE: use the mod's namespace to look up resources if specified
+ ResourceLocation resourcelocation = net.minecraftforge.client.ForgeHooksClient.getShaderImportLocation(s1, p_173374_, p_173375_);
+ if (!this.f_173369_.add(resourcelocation.toString())) {
return null;
} else {
- ResourceLocation resourcelocation = new ResourceLocation(p_173375_);
try {
Reader reader = p_173341_.m_215597_(resourcelocation);
@@ -262,7 +_,8 @@
return s2;
} catch (IOException ioexception) {
- ShaderInstance.f_173323_.error("Could not open GLSL import {}: {}", p_173375_, ioexception.getMessage());
+ // FORGE: specify the namespace of the failed import in case of duplicates from multiple mods
+ ShaderInstance.f_173323_.error("Could not open GLSL import {}: {}", resourcelocation, ioexception.getMessage());
return "#error " + ioexception.getMessage();
}
}
@@ -17,6 +17,7 @@ import com.mojang.datafixers.util.Either;
import com.mojang.math.Matrix4f;
import com.mojang.math.Vector3f;
import net.minecraft.ChatFormatting;
import net.minecraft.FileUtil;
import net.minecraft.client.Camera;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
@@ -1155,4 +1156,12 @@ public class ForgeHooksClient
Mth.log2(Math.max(1, height))
);
}
public static ResourceLocation getShaderImportLocation(String basePath, boolean isRelative, String importPath)
{
final var loc = new ResourceLocation(importPath);
final var normalised = FileUtil.normalizeResourcePath(
(isRelative ? basePath : "shaders/include/") + loc.getPath());
return new ResourceLocation(loc.getNamespace(), normalised);
}
}
@@ -0,0 +1,77 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug.client.rendering;
import java.io.IOException;
import org.slf4j.Logger;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.logging.LogUtils;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RegisterShadersEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.loading.FMLEnvironment;
@Mod(ShaderResourcesTest.MODID)
public class ShaderResourcesTest
{
private static Logger LOGGER;
public static final String MODID = "shader_resources_test";
private static final boolean ENABLE = false;
public ShaderResourcesTest()
{
if (ENABLE)
{
LOGGER = LogUtils.getLogger();
DistExecutor.unsafeRunWhenOn(Dist.CLIENT,() -> ClientInit::new);
}
}
private class ClientInit
{
public ClientInit()
{
final var modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
modEventBus.addListener(ClientInit::registerShaders);
}
public static void registerShaders(final RegisterShadersEvent event)
{
if (!ENABLE)
return;
try
{
event.registerShader(
new ShaderInstance(
event.getResourceManager(),
new ResourceLocation(MODID, "vertex_cubemap"),
DefaultVertexFormat.POSITION),
shader ->
{
LOGGER.info("Completely loaded shader {} with no issues", shader.getName());
});
LOGGER.info("Loaded registered shaders with no exceptions");
}
catch (IOException e)
{
LOGGER.error("Failed to load shaders with exception", e);
}
}
}
}
+2
View File
@@ -17,6 +17,8 @@ license="LGPL v2.1"
[[mods]]
modId="advancement_event_test"
[[mods]]
modId="shader_resources_test"
# LEGACY TEST CASES
###### The mods below are from the old test framework and need to be yeeted later again.
@@ -0,0 +1,8 @@
#version 150
#moj_import <fog.glsl>
#moj_import <shader_resources_test:cubemap_includes.glsl>
void main() {
fragColor = vec4(vertexPos, 0);
}
@@ -0,0 +1,17 @@
{
"blend": {
"func": "add",
"srcrgb": "srcalpha",
"dstrgb": "1-srcalpha"
},
"vertex": "shader_resources_test:vertex_cubemap",
"fragment": "shader_resources_test:vertex_cubemap",
"attributes": [
],
"samplers": [
],
"uniforms": [
{ "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] },
{ "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }
]
}
@@ -0,0 +1,13 @@
#version 150
in vec3 Position;
uniform mat4 ProjMat;
uniform mat4 ModelViewMat;
out vec3 vertexPos;
void main() {
gl_Position = ProjMat * ModelViewMat * vec4(Position, 1.0);
vertexPos = Position;
}
@@ -0,0 +1,5 @@
#version 150
in vec3 vertexPos;
out vec4 fragColor;