Source of all pain

- Strip out almost all source registries
- Fog will be dealt with in a follow-up commit
- Remove most static #init methods
- Remove old ubershader indices from shaders
This commit is contained in:
Jozufozu 2024-09-28 14:48:22 -07:00
parent 48fdcdb751
commit bce657804a
22 changed files with 39 additions and 142 deletions

View file

@ -1,8 +1,6 @@
package dev.engine_room.flywheel.api.instance;
import dev.engine_room.flywheel.api.internal.FlwApiLink;
import dev.engine_room.flywheel.api.layout.Layout;
import dev.engine_room.flywheel.api.registry.Registry;
import net.minecraft.resources.ResourceLocation;
/**
@ -11,8 +9,6 @@ import net.minecraft.resources.ResourceLocation;
* @param <I> The java representation of the instance.
*/
public interface InstanceType<I extends Instance> {
Registry<InstanceType<?>> REGISTRY = FlwApiLink.INSTANCE.createRegistry();
/**
* @param handle A handle that allows you to mark the instance as dirty or deleted.
* @return A new, zeroed instance of I.

View file

@ -1,11 +1,7 @@
package dev.engine_room.flywheel.api.material;
import dev.engine_room.flywheel.api.internal.FlwApiLink;
import dev.engine_room.flywheel.api.registry.Registry;
import net.minecraft.resources.ResourceLocation;
public interface CutoutShader {
Registry<CutoutShader> REGISTRY = FlwApiLink.INSTANCE.createRegistry();
ResourceLocation source();
}

View file

@ -1,11 +1,7 @@
package dev.engine_room.flywheel.api.material;
import dev.engine_room.flywheel.api.internal.FlwApiLink;
import dev.engine_room.flywheel.api.registry.Registry;
import net.minecraft.resources.ResourceLocation;
public interface LightShader {
Registry<LightShader> REGISTRY = FlwApiLink.INSTANCE.createRegistry();
ResourceLocation source();
}

View file

@ -1,12 +1,8 @@
package dev.engine_room.flywheel.api.material;
import dev.engine_room.flywheel.api.internal.FlwApiLink;
import dev.engine_room.flywheel.api.registry.Registry;
import net.minecraft.resources.ResourceLocation;
public interface MaterialShaders {
Registry<MaterialShaders> REGISTRY = FlwApiLink.INSTANCE.createRegistry();
ResourceLocation vertexSource();
ResourceLocation fragmentSource();

View file

@ -8,7 +8,6 @@ import org.jetbrains.annotations.Unmodifiable;
import dev.engine_room.flywheel.api.material.CutoutShader;
import dev.engine_room.flywheel.api.material.FogShader;
import dev.engine_room.flywheel.api.material.MaterialShaders;
import dev.engine_room.flywheel.api.registry.Registry;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
@ -17,10 +16,6 @@ import it.unimi.dsi.fastutil.objects.ObjectList;
import net.minecraft.resources.ResourceLocation;
public final class MaterialShaderIndices {
@Nullable
private static Index vertexSources;
@Nullable
private static Index fragmentSources;
@Nullable
private static Index fogSources;
@Nullable
@ -29,20 +24,6 @@ public final class MaterialShaderIndices {
private MaterialShaderIndices() {
}
public static Index vertexSources() {
if (vertexSources == null) {
vertexSources = indexFromRegistry(MaterialShaders.REGISTRY, MaterialShaders::vertexSource);
}
return vertexSources;
}
public static Index fragmentSources() {
if (fragmentSources == null) {
fragmentSources = indexFromRegistry(MaterialShaders.REGISTRY, MaterialShaders::fragmentSource);
}
return fragmentSources;
}
public static Index fogSources() {
if (fogSources == null) {
fogSources = indexFromRegistry(FogShader.REGISTRY, FogShader::source);
@ -51,26 +32,18 @@ public final class MaterialShaderIndices {
}
public static Index cutoutSources() {
if (cutoutSources == null) {
cutoutSources = indexFromRegistry(CutoutShader.REGISTRY, CutoutShader::source);
}
// if (cutoutSources == null) {
// cutoutSources = indexFromRegistry(CutoutShader.REGISTRY, CutoutShader::source);
// }
return cutoutSources;
}
public static int vertexIndex(MaterialShaders shaders) {
return vertexSources().index(shaders.vertexSource());
}
public static int fragmentIndex(MaterialShaders shaders) {
return fragmentSources().index(shaders.fragmentSource());
}
public static int fogIndex(FogShader fogShader) {
return fogSources().index(fogShader.source());
}
public static int cutoutIndex(CutoutShader cutoutShader) {
return cutoutSources().index(cutoutShader.source());
return 0;//cutoutSources().index(cutoutShader.source());
}
private static <T> Index indexFromRegistry(Registry<T> registry, Function<T, ResourceLocation> sourceFunc) {

View file

@ -21,7 +21,6 @@ import net.minecraft.server.packs.resources.ResourceManager;
public final class FlwPrograms {
public static final Logger LOGGER = LoggerFactory.getLogger(Flywheel.ID + "/backend/shaders");
private static final ResourceLocation COMPONENTS_HEADER_VERT = Flywheel.rl("internal/components_header.vert");
private static final ResourceLocation COMPONENTS_HEADER_FRAG = Flywheel.rl("internal/components_header.frag");
private FlwPrograms() {
@ -36,19 +35,18 @@ public final class FlwPrograms {
var stats = new CompilerStats("ubershaders");
var loader = new SourceLoader(sources, stats);
var vertexComponentsHeader = loader.find(COMPONENTS_HEADER_VERT);
var fragmentComponentsHeader = loader.find(COMPONENTS_HEADER_FRAG);
var fogComponent = createFogComponent(loader);
// TODO: separate compilation for cutout OFF, but keep the rest uber'd?
if (stats.errored() || vertexComponentsHeader == null || fragmentComponentsHeader == null || fogComponent == null) {
if (stats.errored() || fragmentComponentsHeader == null || fogComponent == null) {
// Probably means the shader sources are missing.
stats.emitErrorLog();
return;
}
List<SourceComponent> vertexComponents = List.of(vertexComponentsHeader);
List<SourceComponent> vertexComponents = List.of();
List<SourceComponent> fragmentComponents = List.of(fragmentComponentsHeader, fogComponent);
InstancingPrograms.reload(sources, vertexComponents, fragmentComponents);

View file

@ -19,7 +19,7 @@ public class IndirectBuffers {
public static final long MODEL_STRIDE = 28;
// Byte size of a draw command, plus our added mesh data.
public static final long DRAW_COMMAND_STRIDE = 44;
public static final long DRAW_COMMAND_STRIDE = 36;
public static final long DRAW_COMMAND_OFFSET = 0;
// Offsets to the 3 segments

View file

@ -4,7 +4,6 @@ import org.lwjgl.system.MemoryUtil;
import dev.engine_room.flywheel.api.material.Material;
import dev.engine_room.flywheel.api.visualization.VisualType;
import dev.engine_room.flywheel.backend.MaterialShaderIndices;
import dev.engine_room.flywheel.backend.engine.MaterialEncoder;
import dev.engine_room.flywheel.backend.engine.MeshPool;
import dev.engine_room.flywheel.backend.engine.embed.EmbeddedEnvironment;
@ -17,8 +16,6 @@ public class IndirectDraw {
private final int bias;
private final int indexOfMeshInModel;
private final int materialVertexIndex;
private final int materialFragmentIndex;
private final int packedFogAndCutout;
private final int packedMaterialProperties;
private boolean deleted;
@ -33,8 +30,6 @@ public class IndirectDraw {
mesh.acquire();
this.materialVertexIndex = MaterialShaderIndices.vertexIndex(material.shaders());
this.materialFragmentIndex = MaterialShaderIndices.fragmentIndex(material.shaders());
this.packedFogAndCutout = MaterialEncoder.packUberShader(material);
this.packedMaterialProperties = MaterialEncoder.packProperties(material);
}
@ -78,10 +73,8 @@ public class IndirectDraw {
MemoryUtil.memPutInt(ptr + 24, instancer.environment.matrixIndex()); // matrixIndex
MemoryUtil.memPutInt(ptr + 28, materialVertexIndex); // materialVertexIndex
MemoryUtil.memPutInt(ptr + 32, materialFragmentIndex); // materialFragmentIndex
MemoryUtil.memPutInt(ptr + 36, packedFogAndCutout); // packedFogAndCutout
MemoryUtil.memPutInt(ptr + 40, packedMaterialProperties); // packedMaterialProperties
MemoryUtil.memPutInt(ptr + 28, packedFogAndCutout); // packedFogAndCutout
MemoryUtil.memPutInt(ptr + 32, packedMaterialProperties); // packedMaterialProperties
}
public void writeWithOverrides(long ptr, int instanceIndex, Material materialOverride) {
@ -95,10 +88,8 @@ public class IndirectDraw {
MemoryUtil.memPutInt(ptr + 24, instancer.environment.matrixIndex()); // matrixIndex
MemoryUtil.memPutInt(ptr + 28, MaterialShaderIndices.vertexIndex(materialOverride.shaders())); // materialVertexIndex
MemoryUtil.memPutInt(ptr + 32, MaterialShaderIndices.fragmentIndex(materialOverride.shaders())); // materialFragmentIndex
MemoryUtil.memPutInt(ptr + 36, MaterialEncoder.packUberShader(materialOverride)); // packedFogAndCutout
MemoryUtil.memPutInt(ptr + 40, MaterialEncoder.packProperties(materialOverride)); // packedMaterialProperties
MemoryUtil.memPutInt(ptr + 28, MaterialEncoder.packUberShader(materialOverride)); // packedFogAndCutout
MemoryUtil.memPutInt(ptr + 32, MaterialEncoder.packProperties(materialOverride)); // packedMaterialProperties
}
public void delete() {

View file

@ -4,13 +4,10 @@ import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import org.lwjgl.opengl.GL32;
import dev.engine_room.flywheel.api.backend.Engine;
import dev.engine_room.flywheel.api.instance.Instance;
import dev.engine_room.flywheel.api.material.Material;
import dev.engine_room.flywheel.api.visualization.VisualType;
import dev.engine_room.flywheel.backend.MaterialShaderIndices;
import dev.engine_room.flywheel.backend.Samplers;
import dev.engine_room.flywheel.backend.compile.ContextShader;
import dev.engine_room.flywheel.backend.compile.InstancingPrograms;
@ -203,11 +200,8 @@ public class InstancedDrawManager extends DrawManager<InstancedInstancer<?>> {
}
public static void uploadMaterialUniform(GlProgram program, Material material) {
int uniformLocation = program.getUniformLocation("_flw_packedMaterial");
int vertexIndex = MaterialShaderIndices.vertexIndex(material.shaders());
int fragmentIndex = MaterialShaderIndices.fragmentIndex(material.shaders());
int packedFogAndCutout = MaterialEncoder.packUberShader(material);
int packedMaterialProperties = MaterialEncoder.packProperties(material);
GL32.glUniform4ui(uniformLocation, vertexIndex, fragmentIndex, packedFogAndCutout, packedMaterialProperties);
program.setUVec2("_flw_packedMaterial", packedFogAndCutout, packedMaterialProperties);
}
}

View file

@ -1,3 +1,2 @@
uint _flw_uberMaterialFragmentIndex;
uint _flw_uberFogIndex;
uint _flw_uberCutoutIndex;

View file

@ -1 +0,0 @@
uint _flw_uberMaterialVertexIndex;

View file

@ -8,8 +8,6 @@ struct MeshDrawCommand {
uint modelIndex;
uint matrixIndex;
uint materialVertexIndex;
uint materialFragmentIndex;
uint packedFogAndCutout;
uint packedMaterialProperties;
};

View file

@ -2,12 +2,11 @@
#include "flywheel:internal/indirect/buffer_bindings.glsl"
#include "flywheel:internal/indirect/light.glsl"
flat in uvec3 _flw_packedMaterial;
flat in uvec2 _flw_packedMaterial;
void main() {
_flw_uberMaterialFragmentIndex = _flw_packedMaterial.x;
_flw_unpackUint2x16(_flw_packedMaterial.y, _flw_uberFogIndex, _flw_uberCutoutIndex);
_flw_unpackMaterialProperties(_flw_packedMaterial.z, flw_material);
_flw_unpackUint2x16(_flw_packedMaterial.x, _flw_uberFogIndex, _flw_uberCutoutIndex);
_flw_unpackMaterialProperties(_flw_packedMaterial.y, flw_material);
_flw_main();
}

View file

@ -21,7 +21,7 @@ layout(std430, binding = _FLW_MATRIX_BUFFER_BINDING) restrict buffer MatrixBuffe
uniform uint _flw_baseDraw;
flat out uvec3 _flw_packedMaterial;
flat out uvec2 _flw_packedMaterial;
#if __VERSION__ < 460
#define flw_baseInstance gl_BaseInstanceARB
@ -35,15 +35,12 @@ void main() {
uint drawIndex = flw_drawId + _flw_baseDraw;
MeshDrawCommand draw = _flw_drawCommands[drawIndex];
_flw_uberMaterialVertexIndex = draw.materialVertexIndex;
uint packedMaterialProperties = draw.packedMaterialProperties;
_flw_unpackMaterialProperties(packedMaterialProperties, flw_material);
_flw_packedMaterial = uvec3(draw.materialFragmentIndex, draw.packedFogAndCutout, packedMaterialProperties);
_flw_packedMaterial = uvec2(draw.packedFogAndCutout, packedMaterialProperties);
#ifdef FLW_EMBEDDED
_flw_unpackMatrices(_flw_matrices[draw.matrixIndex], _flw_modelMatrix, _flw_normalMatrix);
// _flw_modelMatrix = mat4(1.);
// _flw_normalMatrix = mat3(1.);
#endif
#ifdef _FLW_CRUMBLING

View file

@ -1,12 +1,11 @@
#include "flywheel:internal/common.frag"
#include "flywheel:internal/instancing/light.glsl"
uniform uvec4 _flw_packedMaterial;
uniform uvec2 _flw_packedMaterial;
void main() {
_flw_uberMaterialFragmentIndex = _flw_packedMaterial.y;
_flw_unpackUint2x16(_flw_packedMaterial.z, _flw_uberFogIndex, _flw_uberCutoutIndex);
_flw_unpackMaterialProperties(_flw_packedMaterial.w, flw_material);
_flw_unpackUint2x16(_flw_packedMaterial.x, _flw_uberFogIndex, _flw_uberCutoutIndex);
_flw_unpackMaterialProperties(_flw_packedMaterial.y, flw_material);
_flw_main();
}

View file

@ -2,7 +2,7 @@
#include "flywheel:internal/packed_material.glsl"
#include "flywheel:internal/instancing/light.glsl"
uniform uvec4 _flw_packedMaterial;
uniform uvec2 _flw_packedMaterial;
uniform int _flw_baseInstance = 0;
#ifdef FLW_EMBEDDED
@ -11,8 +11,7 @@ uniform mat3 _flw_normalMatrixUniform;
#endif
void main() {
_flw_uberMaterialVertexIndex = _flw_packedMaterial.x;
_flw_unpackMaterialProperties(_flw_packedMaterial.w, flw_material);
_flw_unpackMaterialProperties(_flw_packedMaterial.y, flw_material);
FlwInstance instance = _flw_unpackInstance(_flw_baseInstance + gl_InstanceID);

View file

@ -1,6 +1,5 @@
package dev.engine_room.flywheel.lib.instance;
import org.jetbrains.annotations.ApiStatus;
import org.lwjgl.system.MemoryUtil;
import dev.engine_room.flywheel.api.Flywheel;
@ -29,7 +28,7 @@ public final class InstanceTypes {
})
.vertexShader(Flywheel.rl("instance/transformed.vert"))
.cullShader(Flywheel.rl("instance/cull/transformed.glsl"))
.register();
.build();
public static final InstanceType<PosedInstance> POSED = SimpleInstanceType.builder(PosedInstance::new)
.layout(LayoutBuilder.create()
@ -51,7 +50,7 @@ public final class InstanceTypes {
})
.vertexShader(Flywheel.rl("instance/posed.vert"))
.cullShader(Flywheel.rl("instance/cull/posed.glsl"))
.register();
.build();
public static final InstanceType<OrientedInstance> ORIENTED = SimpleInstanceType.builder(OrientedInstance::new)
.layout(LayoutBuilder.create()
@ -79,7 +78,7 @@ public final class InstanceTypes {
})
.vertexShader(Flywheel.rl("instance/oriented.vert"))
.cullShader(Flywheel.rl("instance/cull/oriented.glsl"))
.register();
.build();
public static final InstanceType<ShadowInstance> SHADOW = SimpleInstanceType.builder(ShadowInstance::new)
.layout(LayoutBuilder.create()
@ -102,12 +101,8 @@ public final class InstanceTypes {
})
.vertexShader(Flywheel.rl("instance/shadow.vert"))
.cullShader(Flywheel.rl("instance/cull/shadow.glsl"))
.register();
.build();
private InstanceTypes() {
}
@ApiStatus.Internal
public static void init() {
}
}

View file

@ -89,14 +89,13 @@ public final class SimpleInstanceType<I extends Instance> implements InstanceTyp
return this;
}
public SimpleInstanceType<I> register() {
public SimpleInstanceType<I> build() {
Objects.requireNonNull(layout);
Objects.requireNonNull(writer);
Objects.requireNonNull(vertexShader);
Objects.requireNonNull(cullShader);
var out = new SimpleInstanceType<>(factory, layout, writer, vertexShader, cullShader);
return InstanceType.REGISTRY.registerAndGet(out);
return new SimpleInstanceType<>(factory, layout, writer, vertexShader, cullShader);
}
}
}

View file

@ -1,7 +1,5 @@
package dev.engine_room.flywheel.lib.material;
import org.jetbrains.annotations.ApiStatus;
import dev.engine_room.flywheel.api.Flywheel;
import dev.engine_room.flywheel.api.material.CutoutShader;
@ -9,24 +7,20 @@ public final class CutoutShaders {
/**
* Do not discard any fragments based on alpha.
*/
public static final CutoutShader OFF = CutoutShader.REGISTRY.registerAndGet(new SimpleCutoutShader(Flywheel.rl("cutout/off.glsl")));
public static final CutoutShader OFF = new SimpleCutoutShader(Flywheel.rl("cutout/off.glsl"));
/**
* Discard fragments with alpha close to or equal to zero.
*/
public static final CutoutShader EPSILON = CutoutShader.REGISTRY.registerAndGet(new SimpleCutoutShader(Flywheel.rl("cutout/epsilon.glsl")));
public static final CutoutShader EPSILON = new SimpleCutoutShader(Flywheel.rl("cutout/epsilon.glsl"));
/**
* Discard fragments with alpha less than to 0.1.
*/
public static final CutoutShader ONE_TENTH = CutoutShader.REGISTRY.registerAndGet(new SimpleCutoutShader(Flywheel.rl("cutout/one_tenth.glsl")));
public static final CutoutShader ONE_TENTH = new SimpleCutoutShader(Flywheel.rl("cutout/one_tenth.glsl"));
/**
* Discard fragments with alpha less than to 0.5.
*/
public static final CutoutShader HALF = CutoutShader.REGISTRY.registerAndGet(new SimpleCutoutShader(Flywheel.rl("cutout/half.glsl")));
public static final CutoutShader HALF = new SimpleCutoutShader(Flywheel.rl("cutout/half.glsl"));
private CutoutShaders() {
}
@ApiStatus.Internal
public static void init() {
}
}

View file

@ -1,19 +1,13 @@
package dev.engine_room.flywheel.lib.material;
import org.jetbrains.annotations.ApiStatus;
import dev.engine_room.flywheel.api.Flywheel;
import dev.engine_room.flywheel.api.material.LightShader;
public class LightShaders {
public static final LightShader SMOOTH_WHEN_EMBEDDED = LightShader.REGISTRY.registerAndGet(new SimpleLightShader(Flywheel.rl("light/smooth_when_embedded.glsl")));
public static final LightShader SMOOTH = LightShader.REGISTRY.registerAndGet(new SimpleLightShader(Flywheel.rl("light/smooth.glsl")));
public static final LightShader FLAT = LightShader.REGISTRY.registerAndGet(new SimpleLightShader(Flywheel.rl("light/flat.glsl")));
public static final LightShader SMOOTH_WHEN_EMBEDDED = new SimpleLightShader(Flywheel.rl("light/smooth_when_embedded.glsl"));
public static final LightShader SMOOTH = new SimpleLightShader(Flywheel.rl("light/smooth.glsl"));
public static final LightShader FLAT = new SimpleLightShader(Flywheel.rl("light/flat.glsl"));
private LightShaders() {
}
@ApiStatus.Internal
public static void init() {
}
}

View file

@ -1,23 +1,16 @@
package dev.engine_room.flywheel.lib.material;
import org.jetbrains.annotations.ApiStatus;
import dev.engine_room.flywheel.api.Flywheel;
import dev.engine_room.flywheel.api.material.MaterialShaders;
public final class StandardMaterialShaders {
public static final MaterialShaders DEFAULT = MaterialShaders.REGISTRY.registerAndGet(new SimpleMaterialShaders(
Flywheel.rl("material/default.vert"),
Flywheel.rl("material/default.frag")));
public static final MaterialShaders DEFAULT = new SimpleMaterialShaders(
Flywheel.rl("material/default.vert"), Flywheel.rl("material/default.frag"));
public static final MaterialShaders WIREFRAME = MaterialShaders.REGISTRY.registerAndGet(new SimpleMaterialShaders(Flywheel.rl("material/wireframe.vert"), Flywheel.rl("material/wireframe.frag")));
public static final MaterialShaders WIREFRAME = new SimpleMaterialShaders(Flywheel.rl("material/wireframe.vert"), Flywheel.rl("material/wireframe.frag"));
public static final MaterialShaders LINE = MaterialShaders.REGISTRY.registerAndGet(new SimpleMaterialShaders(Flywheel.rl("material/lines.vert"), Flywheel.rl("material/lines.frag")));
public static final MaterialShaders LINE = new SimpleMaterialShaders(Flywheel.rl("material/lines.vert"), Flywheel.rl("material/lines.frag"));
private StandardMaterialShaders() {
}
@ApiStatus.Internal
public static void init() {
}
}

View file

@ -7,11 +7,7 @@ import dev.engine_room.flywheel.api.Flywheel;
import dev.engine_room.flywheel.backend.FlwBackend;
import dev.engine_room.flywheel.impl.registry.IdRegistryImpl;
import dev.engine_room.flywheel.impl.registry.RegistryImpl;
import dev.engine_room.flywheel.lib.instance.InstanceTypes;
import dev.engine_room.flywheel.lib.material.CutoutShaders;
import dev.engine_room.flywheel.lib.material.FogShaders;
import dev.engine_room.flywheel.lib.material.LightShaders;
import dev.engine_room.flywheel.lib.material.StandardMaterialShaders;
import dev.engine_room.flywheel.lib.util.ShadersModHandler;
import dev.engine_room.flywheel.vanilla.VanillaVisuals;
@ -28,11 +24,7 @@ public final class FlwImpl {
// lib
ShadersModHandler.init();
InstanceTypes.init();
CutoutShaders.init();
FogShaders.init();
LightShaders.init();
StandardMaterialShaders.init();
// backend
FlwBackend.init(FlwConfig.INSTANCE.backendConfig());