diff --git a/src/main/java/com/jozufozu/flywheel/api/shader/FlexibleShader.java b/src/main/java/com/jozufozu/flywheel/api/shader/FlexibleShader.java
new file mode 100644
index 000000000..a46802373
--- /dev/null
+++ b/src/main/java/com/jozufozu/flywheel/api/shader/FlexibleShader.java
@@ -0,0 +1,15 @@
+package com.jozufozu.flywheel.api.shader;
+
+import com.jozufozu.flywheel.api.vertex.VertexType;
+import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
+
+/**
+ * Represents a vertex format agnostic shader.
+ */
+public interface FlexibleShader
{
+
+ /**
+ * Get a version of this shader that accepts the given VertexType as input.
+ */
+ P get(VertexType type);
+}
diff --git a/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java b/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java
index f785f4ac3..165512540 100644
--- a/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java
+++ b/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java
@@ -39,4 +39,6 @@ public interface VertexType {
default int byteOffset(int vertexIndex) {
return getStride() * vertexIndex;
}
+
+ String writeShaderHeader();
}
diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java
index b8ec0ff04..1d5f931eb 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/Backend.java
@@ -115,7 +115,7 @@ public class Backend {
enabled = switch (engine) {
case OFF -> false;
- case BATCHING -> !usingShaders;
+ case BATCHING -> true;
case INSTANCING -> !usingShaders && compat.instancedArraysSupported();
};
}
diff --git a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java
index 66585908e..96c5e563d 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java
@@ -1,19 +1,19 @@
package com.jozufozu.flywheel.backend;
-import java.util.function.Supplier;
-
+import com.jozufozu.flywheel.api.shader.FlexibleShader;
+import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
import net.minecraft.resources.ResourceLocation;
public interface ShaderContext
{
- default P getProgram(ResourceLocation loc) {
+ default P getProgram(ResourceLocation loc, VertexType inputType) {
return this.getProgramSupplier(loc)
- .get();
+ .get(inputType);
}
- Supplier
getProgramSupplier(ResourceLocation loc);
+ FlexibleShader
getProgramSupplier(ResourceLocation loc);
/**
* Load all programs associated with this context. This might be just one, if the context is very specialized.
diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlNumericType.java b/src/main/java/com/jozufozu/flywheel/backend/gl/GlNumericType.java
index 29baffd7f..aaf69e61d 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlNumericType.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/gl/GlNumericType.java
@@ -26,15 +26,15 @@ public enum GlNumericType {
private static final GlNumericType[] VALUES = values();
private static final Map NAME_LOOKUP = Arrays.stream(VALUES)
- .collect(Collectors.toMap(GlNumericType::getDisplayName, type -> type));
+ .collect(Collectors.toMap(GlNumericType::getTypeName, type -> type));
private final int byteWidth;
- private final String displayName;
+ private final String typeName;
private final int glEnum;
GlNumericType(int bytes, String name, int glEnum) {
this.byteWidth = bytes;
- this.displayName = name;
+ this.typeName = name;
this.glEnum = glEnum;
}
@@ -42,8 +42,8 @@ public enum GlNumericType {
return this.byteWidth;
}
- public String getDisplayName() {
- return this.displayName;
+ public String getTypeName() {
+ return this.typeName;
}
public int getGlEnum() {
@@ -64,4 +64,9 @@ public enum GlNumericType {
public static GlNumericType byName(String name) {
return name == null ? null : NAME_LOOKUP.get(name.toLowerCase(Locale.ROOT));
}
+
+ @Override
+ public String toString() {
+ return typeName;
+ }
}
diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java b/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java
index 1ec315fa9..c50681c7f 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java
@@ -39,8 +39,8 @@ public class GlVertexArray extends GlObject {
int offset = 0;
for (LayoutItem spec : type.getLayoutItems()) {
spec.vertexAttribPointer(type.getStride(), startIndex, offset);
- startIndex += spec.getAttributeCount();
- offset += spec.getSize();
+ startIndex += spec.attributeCount();
+ offset += spec.size();
}
}
diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java
index c386294ba..f6464778c 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java
@@ -1,10 +1,16 @@
package com.jozufozu.flywheel.backend.gl.shader;
+import java.util.List;
+
import org.lwjgl.opengl.GL20;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.GlObject;
import com.jozufozu.flywheel.backend.gl.versioned.GlCompat;
+import com.jozufozu.flywheel.backend.pipeline.ShaderCompiler;
+import com.jozufozu.flywheel.backend.source.ShaderLoadingException;
+import com.jozufozu.flywheel.backend.source.error.ErrorBuilder;
+import com.jozufozu.flywheel.backend.source.error.ErrorReporter;
import net.minecraft.resources.ResourceLocation;
@@ -13,8 +19,8 @@ public class GlShader extends GlObject {
public final ResourceLocation name;
public final ShaderType type;
- public GlShader(ResourceLocation name, ShaderType type, CharSequence source) {
- this.name = name;
+ public GlShader(ShaderCompiler env, ShaderType type, String source) {
+ name = env.name;
this.type = type;
int handle = GL20.glCreateShader(type.glEnum);
@@ -24,13 +30,31 @@ public class GlShader extends GlObject {
String log = GL20.glGetShaderInfoLog(handle);
if (!log.isEmpty()) {
- Backend.log.error("Shader compilation log for " + name + ": " + log);
- Backend.log.error(source);
+ List lines = log.lines()
+ .toList();
+
+ boolean needsSourceDump = false;
+
+ StringBuilder errors = new StringBuilder();
+ for (String line : lines) {
+ ErrorBuilder builder = env.parseCompilerError(line);
+
+ if (builder != null) {
+ errors.append(builder.build());
+ } else {
+ errors.append(line).append('\n');
+ needsSourceDump = true;
+ }
+ }
+ Backend.log.error("Errors compiling '" + name + "': \n" + errors);
+ if (needsSourceDump) {
+ // TODO: generated code gets its own "file"
+ ErrorReporter.printLines(source);
+ }
}
- //Backend.log.debug(shader.printSource());
if (GL20.glGetShaderi(handle, GL20.GL_COMPILE_STATUS) != GL20.GL_TRUE) {
- throw new RuntimeException("Could not compile " + name + ". See log for details.");
+ throw new ShaderLoadingException("Could not compile " + name + ". See log for details.");
}
setHandle(handle);
diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java
index a72dcf415..becb71114 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java
@@ -1,20 +1,17 @@
package com.jozufozu.flywheel.backend.instancing.instancing;
-import java.util.concurrent.ExecutionException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import java.util.function.Supplier;
-import com.google.common.cache.Cache;
-import com.google.common.cache.CacheBuilder;
import com.jozufozu.flywheel.api.InstanceData;
import com.jozufozu.flywheel.api.Instancer;
import com.jozufozu.flywheel.api.Material;
import com.jozufozu.flywheel.api.struct.Instanced;
-import com.jozufozu.flywheel.backend.Backend;
-import com.jozufozu.flywheel.backend.RenderWork;
-import com.jozufozu.flywheel.backend.model.ImmediateAllocator;
import com.jozufozu.flywheel.backend.model.ModelAllocator;
-import com.jozufozu.flywheel.backend.model.ModelPool;
-import com.jozufozu.flywheel.core.Formats;
import com.jozufozu.flywheel.core.model.Model;
/**
@@ -23,24 +20,14 @@ import com.jozufozu.flywheel.core.model.Model;
*/
public class InstancedMaterial implements Material {
- final ModelAllocator allocator;
- protected final Cache