Compare commits

...

2 commits

Author SHA1 Message Date
Jozufozu
9b9089ea64 Oopershaders
- Do not crash when an ubershader'd component is missing
- Reset instancing/indirect programs immediately on reload to invalidate
  them in case ubershader loading fails
2024-02-16 13:58:04 -08:00
Jozufozu
51458c2768 NEA: Not Enough Attributes
- Transformed had snuck up to attribute location 17
- Combine overlay and light attributes into one
- Flip order of overlay and light in instances to be consistent with
  the internal vertex layout
2024-02-16 13:34:34 -08:00
9 changed files with 51 additions and 33 deletions

View file

@ -19,8 +19,7 @@ public final class InternalVertex {
.vector("position", FloatRepr.FLOAT, 3) .vector("position", FloatRepr.FLOAT, 3)
.vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4) .vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4)
.vector("tex", FloatRepr.FLOAT, 2) .vector("tex", FloatRepr.FLOAT, 2)
.vector("overlay", UnsignedIntegerRepr.UNSIGNED_SHORT, 2) .vector("overlay_light", UnsignedIntegerRepr.UNSIGNED_SHORT, 4)
.vector("light", UnsignedIntegerRepr.UNSIGNED_SHORT, 2)
.vector("normal", FloatRepr.NORMALIZED_BYTE, 3) .vector("normal", FloatRepr.NORMALIZED_BYTE, 3)
.build(); .build();

View file

@ -2,6 +2,7 @@ package com.jozufozu.flywheel.backend.compile;
import java.util.List; import java.util.List;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -22,26 +23,37 @@ import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.ResourceManagerReloadListener; import net.minecraft.server.packs.resources.ResourceManagerReloadListener;
public final class FlwPrograms { public final class FlwPrograms {
public static final Logger LOGGER = LoggerFactory.getLogger(Flywheel.ID + "/compile"); public static final Logger LOGGER = LoggerFactory.getLogger(Flywheel.ID + "/shaders");
private FlwPrograms() { private FlwPrograms() {
} }
private static void reload(ResourceManager resourceManager) { private static void reload(ResourceManager resourceManager) {
// Reset the programs in case the ubershader load fails.
InstancingPrograms.setInstance(null);
IndirectPrograms.setInstance(null);
var sources = new ShaderSources(resourceManager); var sources = new ShaderSources(resourceManager);
var preLoadStats = new CompilerStats("Preload"); var stats = new CompilerStats("ubershaders");
var loadChecker = new SourceLoader(sources, preLoadStats); var loadChecker = new SourceLoader(sources, stats);
var vertexMaterialComponent = createVertexMaterialComponent(loadChecker);
var fragmentMaterialComponent = createFragmentMaterialComponent(loadChecker);
var fogComponent = createFogComponent(loadChecker);
var cutoutComponent = createCutoutComponent(loadChecker);
if (stats.errored() || vertexMaterialComponent == null || fragmentMaterialComponent == null || fogComponent == null || cutoutComponent == null) {
// Probably means the shader sources are missing.
stats.emitErrorLog();
return;
}
List<SourceComponent> vertexComponents = List.of(vertexMaterialComponent);
List<SourceComponent> fragmentComponents = List.of(fragmentMaterialComponent, fogComponent, cutoutComponent);
var pipelineKeys = createPipelineKeys(); var pipelineKeys = createPipelineKeys();
List<SourceComponent> vertexComponents = List.of(createVertexMaterialComponent(loadChecker));
List<SourceComponent> fragmentComponents = List.of(createFragmentMaterialComponent(loadChecker), createFogComponent(loadChecker), createCutoutComponent(loadChecker));
InstancingPrograms.reload(sources, pipelineKeys, vertexComponents, fragmentComponents); InstancingPrograms.reload(sources, pipelineKeys, vertexComponents, fragmentComponents);
IndirectPrograms.reload(sources, pipelineKeys, vertexComponents, fragmentComponents); IndirectPrograms.reload(sources, pipelineKeys, vertexComponents, fragmentComponents);
if (preLoadStats.errored()) {
preLoadStats.emitErrorLog();
}
} }
private static ImmutableList<PipelineProgramKey> createPipelineKeys() { private static ImmutableList<PipelineProgramKey> createPipelineKeys() {
@ -54,6 +66,7 @@ public final class FlwPrograms {
return builder.build(); return builder.build();
} }
@Nullable
private static UberShaderComponent createVertexMaterialComponent(SourceLoader loadChecker) { private static UberShaderComponent createVertexMaterialComponent(SourceLoader loadChecker) {
return UberShaderComponent.builder(Flywheel.rl("uber_material_vertex")) return UberShaderComponent.builder(Flywheel.rl("uber_material_vertex"))
.materialSources(ShaderIndices.materialVertex() .materialSources(ShaderIndices.materialVertex()
@ -63,6 +76,7 @@ public final class FlwPrograms {
.build(loadChecker); .build(loadChecker);
} }
@Nullable
private static UberShaderComponent createFragmentMaterialComponent(SourceLoader loadChecker) { private static UberShaderComponent createFragmentMaterialComponent(SourceLoader loadChecker) {
return UberShaderComponent.builder(Flywheel.rl("uber_material_fragment")) return UberShaderComponent.builder(Flywheel.rl("uber_material_fragment"))
.materialSources(ShaderIndices.materialFragment() .materialSources(ShaderIndices.materialFragment()
@ -72,6 +86,7 @@ public final class FlwPrograms {
.build(loadChecker); .build(loadChecker);
} }
@Nullable
private static UberShaderComponent createFogComponent(SourceLoader loadChecker) { private static UberShaderComponent createFogComponent(SourceLoader loadChecker) {
return UberShaderComponent.builder(Flywheel.rl("uber_fog")) return UberShaderComponent.builder(Flywheel.rl("uber_fog"))
.materialSources(ShaderIndices.fog() .materialSources(ShaderIndices.fog()
@ -85,6 +100,7 @@ public final class FlwPrograms {
.build(loadChecker); .build(loadChecker);
} }
@Nullable
private static UberShaderComponent createCutoutComponent(SourceLoader loadChecker) { private static UberShaderComponent createCutoutComponent(SourceLoader loadChecker) {
return UberShaderComponent.builder(Flywheel.rl("uber_cutout")) return UberShaderComponent.builder(Flywheel.rl("uber_cutout"))
.materialSources(ShaderIndices.cutout() .materialSources(ShaderIndices.cutout()

View file

@ -3,6 +3,7 @@ package com.jozufozu.flywheel.backend.compile;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -102,7 +103,8 @@ public class IndirectPrograms extends AbstractPrograms {
return builder.build(); return builder.build();
} }
private static void setInstance(@Nullable IndirectPrograms newInstance) { @ApiStatus.Internal
public static void setInstance(@Nullable IndirectPrograms newInstance) {
if (instance != null) { if (instance != null) {
instance.release(); instance.release();
} }

View file

@ -3,6 +3,7 @@ package com.jozufozu.flywheel.backend.compile;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -42,7 +43,8 @@ public class InstancingPrograms extends AbstractPrograms {
setInstance(newInstance); setInstance(newInstance);
} }
private static void setInstance(@Nullable InstancingPrograms newInstance) { @ApiStatus.Internal
public static void setInstance(@Nullable InstancingPrograms newInstance) {
if (instance != null) { if (instance != null) {
instance.release(); instance.release();
} }

View file

@ -108,6 +108,7 @@ public class UberShaderComponent implements SourceComponent {
private final ResourceLocation name; private final ResourceLocation name;
private final List<ResourceLocation> materialSources = new ArrayList<>(); private final List<ResourceLocation> materialSources = new ArrayList<>();
private final List<AdaptedFn> adaptedFunctions = new ArrayList<>(); private final List<AdaptedFn> adaptedFunctions = new ArrayList<>();
@Nullable
private GlslExpr switchArg; private GlslExpr switchArg;
public Builder(ResourceLocation name) { public Builder(ResourceLocation name) {
@ -134,6 +135,7 @@ public class UberShaderComponent implements SourceComponent {
return this; return this;
} }
@Nullable
public UberShaderComponent build(SourceLoader sources) { public UberShaderComponent build(SourceLoader sources) {
if (switchArg == null) { if (switchArg == null) {
throw new NullPointerException("Switch argument must be set"); throw new NullPointerException("Switch argument must be set");

View file

@ -14,8 +14,7 @@ public final class InstanceTypes {
public static final InstanceType<TransformedInstance> TRANSFORMED = SimpleInstanceType.builder(TransformedInstance::new) public static final InstanceType<TransformedInstance> TRANSFORMED = SimpleInstanceType.builder(TransformedInstance::new)
.layout(LayoutBuilder.create() .layout(LayoutBuilder.create()
.vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4) .vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4)
.vector("light", IntegerRepr.SHORT, 2) .vector("overlay_light", IntegerRepr.SHORT, 4)
.vector("overlay", IntegerRepr.SHORT, 2)
.matrix("pose", FloatRepr.FLOAT, 4) .matrix("pose", FloatRepr.FLOAT, 4)
.matrix("normal", FloatRepr.FLOAT, 3) .matrix("normal", FloatRepr.FLOAT, 3)
.build()) .build())
@ -24,9 +23,9 @@ public final class InstanceTypes {
MemoryUtil.memPutByte(ptr + 1, instance.g); MemoryUtil.memPutByte(ptr + 1, instance.g);
MemoryUtil.memPutByte(ptr + 2, instance.b); MemoryUtil.memPutByte(ptr + 2, instance.b);
MemoryUtil.memPutByte(ptr + 3, instance.a); MemoryUtil.memPutByte(ptr + 3, instance.a);
MemoryUtil.memPutShort(ptr + 4, instance.blockLight); MemoryUtil.memPutInt(ptr + 4, instance.overlay);
MemoryUtil.memPutShort(ptr + 6, instance.skyLight); MemoryUtil.memPutShort(ptr + 8, instance.blockLight);
MemoryUtil.memPutInt(ptr + 8, instance.overlay); MemoryUtil.memPutShort(ptr + 10, instance.skyLight);
MatrixMath.writeUnsafe(instance.model, ptr + 12); MatrixMath.writeUnsafe(instance.model, ptr + 12);
MatrixMath.writeUnsafe(instance.normal, ptr + 76); MatrixMath.writeUnsafe(instance.normal, ptr + 76);
}) })
@ -37,8 +36,7 @@ public final class InstanceTypes {
public static final InstanceType<OrientedInstance> ORIENTED = SimpleInstanceType.builder(OrientedInstance::new) public static final InstanceType<OrientedInstance> ORIENTED = SimpleInstanceType.builder(OrientedInstance::new)
.layout(LayoutBuilder.create() .layout(LayoutBuilder.create()
.vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4) .vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4)
.vector("light", IntegerRepr.SHORT, 2) .vector("overlay_light", IntegerRepr.SHORT, 4)
.vector("overlay", IntegerRepr.SHORT, 2)
.vector("position", FloatRepr.FLOAT, 3) .vector("position", FloatRepr.FLOAT, 3)
.vector("pivot", FloatRepr.FLOAT, 3) .vector("pivot", FloatRepr.FLOAT, 3)
.vector("rotation", FloatRepr.FLOAT, 4) .vector("rotation", FloatRepr.FLOAT, 4)
@ -48,9 +46,9 @@ public final class InstanceTypes {
MemoryUtil.memPutByte(ptr + 1, instance.g); MemoryUtil.memPutByte(ptr + 1, instance.g);
MemoryUtil.memPutByte(ptr + 2, instance.b); MemoryUtil.memPutByte(ptr + 2, instance.b);
MemoryUtil.memPutByte(ptr + 3, instance.a); MemoryUtil.memPutByte(ptr + 3, instance.a);
MemoryUtil.memPutShort(ptr + 4, instance.blockLight); MemoryUtil.memPutInt(ptr + 4, instance.overlay);
MemoryUtil.memPutShort(ptr + 6, instance.skyLight); MemoryUtil.memPutShort(ptr + 8, instance.blockLight);
MemoryUtil.memPutInt(ptr + 8, instance.overlay); MemoryUtil.memPutShort(ptr + 10, instance.skyLight);
MemoryUtil.memPutFloat(ptr + 12, instance.posX); MemoryUtil.memPutFloat(ptr + 12, instance.posX);
MemoryUtil.memPutFloat(ptr + 16, instance.posY); MemoryUtil.memPutFloat(ptr + 16, instance.posY);
MemoryUtil.memPutFloat(ptr + 20, instance.posZ); MemoryUtil.memPutFloat(ptr + 20, instance.posZ);

View file

@ -4,6 +4,6 @@ void flw_instanceVertex(in FlwInstance i) {
flw_vertexPos = vec4(rotateByQuaternion(flw_vertexPos.xyz - i.pivot, i.rotation) + i.pivot + i.position, 1.0); flw_vertexPos = vec4(rotateByQuaternion(flw_vertexPos.xyz - i.pivot, i.rotation) + i.pivot + i.position, 1.0);
flw_vertexNormal = rotateByQuaternion(flw_vertexNormal, i.rotation); flw_vertexNormal = rotateByQuaternion(flw_vertexNormal, i.rotation);
flw_vertexColor = i.color; flw_vertexColor = i.color;
flw_vertexOverlay = i.overlay; flw_vertexOverlay = i.overlay_light.xy;
flw_vertexLight = i.light / 15.0; flw_vertexLight = i.overlay_light.zw / 15.0;
} }

View file

@ -2,6 +2,6 @@ void flw_instanceVertex(in FlwInstance i) {
flw_vertexPos = i.pose * flw_vertexPos; flw_vertexPos = i.pose * flw_vertexPos;
flw_vertexNormal = i.normal * flw_vertexNormal; flw_vertexNormal = i.normal * flw_vertexNormal;
flw_vertexColor = i.color; flw_vertexColor = i.color;
flw_vertexOverlay = i.overlay; flw_vertexOverlay = i.overlay_light.xy;
flw_vertexLight = i.light / 15.0; flw_vertexLight = i.overlay_light.zw / 15.0;
} }

View file

@ -1,15 +1,14 @@
layout(location = 0) in vec3 _flw_a_pos; layout(location = 0) in vec3 _flw_a_pos;
layout(location = 1) in vec4 _flw_a_color; layout(location = 1) in vec4 _flw_a_color;
layout(location = 2) in vec2 _flw_a_texCoord; layout(location = 2) in vec2 _flw_a_texCoord;
layout(location = 3) in ivec2 _flw_a_overlay; layout(location = 3) in ivec4 _flw_a_overlay_light;
layout(location = 4) in ivec2 _flw_a_light; layout(location = 4) in vec3 _flw_a_normal;
layout(location = 5) in vec3 _flw_a_normal;
void _flw_layoutVertex() { void _flw_layoutVertex() {
flw_vertexPos = vec4(_flw_a_pos, 1.0); flw_vertexPos = vec4(_flw_a_pos, 1.0);
flw_vertexColor = _flw_a_color; flw_vertexColor = _flw_a_color;
flw_vertexTexCoord = _flw_a_texCoord; flw_vertexTexCoord = _flw_a_texCoord;
flw_vertexOverlay = _flw_a_overlay; flw_vertexOverlay = _flw_a_overlay_light.xy;
flw_vertexLight = (_flw_a_light >> 4) / 15.0; flw_vertexLight = (_flw_a_overlay_light.zw >> 4) / 15.0;
flw_vertexNormal = _flw_a_normal; flw_vertexNormal = _flw_a_normal;
} }