Skip to content

Commit

Permalink
Allow mods to specify shader import namespace (MinecraftForge#9021)
Browse files Browse the repository at this point in the history
  • Loading branch information
FiniteReality authored Sep 8, 2022
1 parent 6d233ac commit 82026d1
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
9 changes: 9 additions & 0 deletions src/main/java/net/minecraftforge/client/ForgeHooksClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -1155,4 +1156,12 @@ public static int getMaxMipmapLevel(int width, int height)
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);
}
}
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions src/test/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#version 150

#moj_import <fog.glsl>
#moj_import <shader_resources_test:cubemap_includes.glsl>

void main() {
fragColor = vec4(vertexPos, 0);
}
Original file line number Diff line number Diff line change
@@ -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 ] }
]
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#version 150

in vec3 vertexPos;

out vec4 fragColor;

0 comments on commit 82026d1

Please sign in to comment.