2021-08-11 00:20:51 +02:00
|
|
|
package com.jozufozu.flywheel.backend;
|
|
|
|
|
2022-07-24 03:18:48 +02:00
|
|
|
import com.jozufozu.flywheel.api.material.Material;
|
|
|
|
import com.jozufozu.flywheel.api.struct.StructType;
|
|
|
|
import com.jozufozu.flywheel.api.vertex.VertexType;
|
2021-08-11 00:20:51 +02:00
|
|
|
import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher;
|
2022-07-24 03:18:48 +02:00
|
|
|
import com.jozufozu.flywheel.core.ComponentRegistry;
|
|
|
|
import com.jozufozu.flywheel.core.compile.ContextShader;
|
|
|
|
import com.jozufozu.flywheel.core.compile.ProgramCompiler;
|
2022-05-17 21:56:09 +02:00
|
|
|
import com.jozufozu.flywheel.core.source.FileResolution;
|
|
|
|
import com.jozufozu.flywheel.core.source.ShaderLoadingException;
|
2022-01-13 06:25:03 +01:00
|
|
|
import com.jozufozu.flywheel.core.source.ShaderSources;
|
2022-05-17 21:56:09 +02:00
|
|
|
import com.jozufozu.flywheel.core.source.error.ErrorReporter;
|
2022-07-24 03:18:48 +02:00
|
|
|
import com.jozufozu.flywheel.util.StringUtil;
|
2021-08-11 00:20:51 +02:00
|
|
|
|
|
|
|
import net.minecraft.client.Minecraft;
|
2021-09-15 08:45:29 +02:00
|
|
|
import net.minecraft.client.multiplayer.ClientLevel;
|
|
|
|
import net.minecraft.server.packs.resources.ReloadableResourceManager;
|
|
|
|
import net.minecraft.server.packs.resources.ResourceManager;
|
2021-09-15 09:26:51 +02:00
|
|
|
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;
|
|
|
|
|
2021-08-11 00:20:51 +02:00
|
|
|
/**
|
|
|
|
* The main entity for loading shaders.
|
|
|
|
*
|
|
|
|
* <p>
|
2021-09-15 09:26:51 +02:00
|
|
|
* This class is responsible for invoking the loading, parsing, and compilation stages.
|
2021-08-11 00:20:51 +02:00
|
|
|
* </p>
|
|
|
|
*/
|
2021-09-15 09:26:51 +02:00
|
|
|
public class Loader implements ResourceManagerReloadListener {
|
2021-08-13 20:23:09 +02:00
|
|
|
|
2022-01-13 06:25:03 +01:00
|
|
|
Loader() {
|
2021-08-11 02:39:11 +02:00
|
|
|
// Can be null when running datagenerators due to the unfortunate time we call this
|
|
|
|
Minecraft minecraft = Minecraft.getInstance();
|
|
|
|
if (minecraft != null) {
|
2021-09-15 08:45:29 +02:00
|
|
|
ResourceManager manager = minecraft.getResourceManager();
|
|
|
|
if (manager instanceof ReloadableResourceManager) {
|
|
|
|
((ReloadableResourceManager) manager).registerReloadListener(this);
|
2021-08-11 02:39:11 +02:00
|
|
|
}
|
2021-08-11 00:20:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-09-15 09:26:51 +02:00
|
|
|
public void onResourceManagerReload(ResourceManager manager) {
|
2022-01-13 06:25:03 +01:00
|
|
|
Backend.refresh();
|
2021-08-11 00:20:51 +02:00
|
|
|
|
2022-05-17 21:56:09 +02:00
|
|
|
var errorReporter = new ErrorReporter();
|
|
|
|
ShaderSources sources = new ShaderSources(errorReporter, manager);
|
2021-08-11 00:20:51 +02:00
|
|
|
|
2022-07-24 03:18:48 +02:00
|
|
|
Backend.LOGGER.info("Loaded all shader sources in " + sources.getLoadTime());
|
|
|
|
|
2022-05-17 21:56:09 +02:00
|
|
|
FileResolution.run(errorReporter, sources);
|
2021-08-11 00:20:51 +02:00
|
|
|
|
2022-05-17 21:56:09 +02:00
|
|
|
if (errorReporter.hasErrored()) {
|
|
|
|
errorReporter.dump();
|
|
|
|
throw new ShaderLoadingException("Failed to resolve all source files, see log for details");
|
|
|
|
}
|
2021-08-11 00:20:51 +02:00
|
|
|
|
2022-07-23 02:24:41 +02:00
|
|
|
sources.postResolve();
|
|
|
|
|
2022-07-24 03:18:48 +02:00
|
|
|
Backend.LOGGER.info("Successfully resolved all source files.");
|
|
|
|
|
2022-07-23 02:24:41 +02:00
|
|
|
FileResolution.checkAll(errorReporter);
|
|
|
|
|
2022-07-24 03:18:48 +02:00
|
|
|
Backend.LOGGER.info("All shaders passed checks.");
|
|
|
|
|
|
|
|
long compileStart = System.nanoTime();
|
|
|
|
|
|
|
|
for (Material material : ComponentRegistry.materials) {
|
|
|
|
for (StructType<?> structType : ComponentRegistry.structTypes) {
|
|
|
|
for (VertexType vertexType : ComponentRegistry.vertexTypes) {
|
|
|
|
for (ContextShader contextShader : ComponentRegistry.contextShaders) {
|
|
|
|
var ctx = new ProgramCompiler.Context(vertexType, material, structType.getInstanceShader(), contextShader);
|
|
|
|
ProgramCompiler.INSTANCE.getProgram(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long compileEnd = System.nanoTime();
|
|
|
|
|
|
|
|
Backend.LOGGER.info("Compiled all programs in " + StringUtil.formatTime(compileEnd - compileStart));
|
2021-08-11 00:20:51 +02:00
|
|
|
|
2021-12-23 06:35:48 +01:00
|
|
|
ClientLevel world = Minecraft.getInstance().level;
|
2021-12-31 00:15:25 +01:00
|
|
|
if (Backend.canUseInstancing(world)) {
|
2021-12-23 06:35:48 +01:00
|
|
|
InstancedRenderDispatcher.resetInstanceWorld(world);
|
2021-08-11 00:20:51 +02:00
|
|
|
}
|
2021-09-15 09:26:51 +02:00
|
|
|
|
2021-08-11 00:20:51 +02:00
|
|
|
}
|
|
|
|
}
|