1717import java .nio .file .Files ;
1818import java .nio .file .Path ;
1919import java .nio .file .Paths ;
20+ import java .util .List ;
2021import java .util .Set ;
2122
2223public abstract class ShaderLoadUtil {
2324
2425 public static final String RESOURCES_PATH = SPIRVUtils .class .getResource ("/assets/vulkanmod" ).toExternalForm ();
2526 public static final String SHADERS_PATH = "%s/shaders/" .formatted (RESOURCES_PATH );
2627
27- public static final Set <String > REMAPPED_SHADERS = Sets .newHashSet ("core/screenquad.vsh" ,"core/rendertype_item_entity_translucent_cull.vsh" );
28+ public static final Set <String > REMAPPED_SHADERS = Sets .newHashSet ("core/screenquad.vsh" ,
29+ "core/rendertype_item_entity_translucent_cull.vsh" );
2830
2931 public static String resolveShaderPath (String path ) {
3032 return resolveShaderPath (SHADERS_PATH , path );
@@ -35,6 +37,10 @@ public static String resolveShaderPath(String shaderPath, String path) {
3537 }
3638
3739 public static void loadShaders (Pipeline .Builder pipelineBuilder , JsonObject config , String configName , String path ) {
40+ loadShaders (pipelineBuilder , config , configName , path , null );
41+ }
42+
43+ public static void loadShaders (Pipeline .Builder pipelineBuilder , JsonObject config , String configName , String path , List <String > defines ) {
3844 String vertexShader = config .has ("vertex" ) ? config .get ("vertex" ).getAsString () : configName ;
3945 String fragmentShader = config .has ("fragment" ) ? config .get ("fragment" ).getAsString () : configName ;
4046
@@ -51,21 +57,25 @@ public static void loadShaders(Pipeline.Builder pipelineBuilder, JsonObject conf
5157 vertexShader = getFileName (vertexShader );
5258 fragmentShader = getFileName (fragmentShader );
5359
54- loadShader (pipelineBuilder , configName , path , vertexShader , SPIRVUtils .ShaderKind .VERTEX_SHADER );
55- loadShader (pipelineBuilder , configName , path , fragmentShader , SPIRVUtils .ShaderKind .FRAGMENT_SHADER );
60+ loadShader (pipelineBuilder , configName , path , vertexShader , SPIRVUtils .ShaderKind .VERTEX_SHADER , defines );
61+ loadShader (pipelineBuilder , configName , path , fragmentShader , SPIRVUtils .ShaderKind .FRAGMENT_SHADER , defines );
5662 }
5763
5864 public static void loadShader (Pipeline .Builder pipelineBuilder , String configName , String path , SPIRVUtils .ShaderKind type ) {
5965 String [] splitPath = splitPath (path );
6066 String shaderName = splitPath [1 ];
6167 String subPath = splitPath [0 ];
6268
63- loadShader (pipelineBuilder , configName , subPath , shaderName , type );
69+ loadShader (pipelineBuilder , configName , subPath , shaderName , type , null );
6470 }
6571
66- public static void loadShader (Pipeline .Builder pipelineBuilder , String configName , String path , String shaderName , SPIRVUtils .ShaderKind type ) {
72+ public static void loadShader (Pipeline .Builder pipelineBuilder , String configName , String path , String shaderName , SPIRVUtils .ShaderKind type , List < String > defines ) {
6773 String source = getShaderSource (path , configName , shaderName , type );
6874
75+ if (defines != null && !defines .isEmpty ()) {
76+ source = injectDefines (source , defines );
77+ }
78+
6979 SPIRVUtils .SPIRV spirv = SPIRVUtils .compileShader (shaderName , source , type );
7080
7181 switch (type ) {
@@ -144,6 +154,11 @@ public static String getShaderSource(ResourceLocation resourceLocation, ShaderTy
144154 try {
145155 stream = getInputStream (shaderFile );
146156
157+ if (stream == null ) {
158+ shaderFile = "%s/shaders/%s%s" .formatted (RESOURCES_PATH , path , shaderExtension );
159+ stream = getInputStream (shaderFile );
160+ }
161+
147162 if (stream == null ) {
148163 return null ;
149164 }
@@ -256,4 +271,17 @@ public static InputStream getInputStream(String path) {
256271 throw new RuntimeException (e );
257272 }
258273 }
274+
275+ public static String injectDefines (String shaderSrc , List <String > defines ) {
276+ StringBuilder stringBuilder = new StringBuilder ();
277+
278+ for (String define : defines ) {
279+ stringBuilder .append ("#define " ).append (define ).append ('\n' );
280+ }
281+
282+ int i = shaderSrc .indexOf ('\n' );
283+
284+ String out = shaderSrc .substring (0 , i + 1 ) + "\n " + stringBuilder + "\n " + shaderSrc .substring (i + 1 );
285+ return out ;
286+ }
259287}
0 commit comments