From cbbd2bfdf8825d86f98b3bfd5ea757b73d9709aa Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Fri, 8 Dec 2023 17:02:12 -0800 Subject: [PATCH] Separation, for instance - Separate instance vertex shaders and instance cull shaders into different files with separate getters in InstanceType - Remove all occurrences of Names and Files inner classes --- .../flywheel/api/instance/InstanceType.java | 4 +- .../backend/compile/IndirectPrograms.java | 14 +++--- .../backend/compile/PipelineCompiler.java | 2 +- .../flywheel/lib/context/Contexts.java | 46 ++++++++----------- .../flywheel/lib/instance/InstanceTypes.java | 13 ------ .../flywheel/lib/instance/OrientedType.java | 17 +++++-- .../lib/instance/TransformedType.java | 17 +++++-- .../lib/instance/TransformedWriter.java | 1 - .../lib/material/StandardMaterialShaders.java | 16 ++----- .../flywheel/instance/cull/oriented.glsl | 9 ++++ .../flywheel/instance/cull/transformed.glsl | 7 +++ .../flywheel/flywheel/instance/oriented.vert | 12 ----- .../flywheel/instance/transformed.vert | 12 ----- 13 files changed, 74 insertions(+), 96 deletions(-) create mode 100644 src/main/resources/assets/flywheel/flywheel/instance/cull/oriented.glsl create mode 100644 src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl diff --git a/src/main/java/com/jozufozu/flywheel/api/instance/InstanceType.java b/src/main/java/com/jozufozu/flywheel/api/instance/InstanceType.java index 1c53b4cbb..68509c366 100644 --- a/src/main/java/com/jozufozu/flywheel/api/instance/InstanceType.java +++ b/src/main/java/com/jozufozu/flywheel/api/instance/InstanceType.java @@ -27,7 +27,9 @@ public interface InstanceType { InstanceWriter getWriter(); - ResourceLocation instanceShader(); + ResourceLocation vertexShader(); + + ResourceLocation cullShader(); InstanceVertexTransformer getVertexTransformer(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java b/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java index 8754363ff..39453ea5a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java @@ -22,6 +22,9 @@ import com.jozufozu.flywheel.lib.util.Unit; import net.minecraft.resources.ResourceLocation; public class IndirectPrograms { + private static final ResourceLocation CULL_SHADER_MAIN = Flywheel.rl("internal/indirect/cull.glsl"); + private static final ResourceLocation APPLY_SHADER_MAIN = Flywheel.rl("internal/indirect/apply.glsl"); + public static IndirectPrograms instance; private static final Compile> CULL = new Compile<>(); private static final Compile APPLY = new Compile<>(); @@ -89,8 +92,8 @@ public class IndirectPrograms { .define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) .withComponent(uniformComponent) .withComponent(IndirectComponent::create) - .withResource(InstanceType::instanceShader) - .withResource(Files.INDIRECT_CULL)) + .withResource(InstanceType::cullShader) + .withResource(CULL_SHADER_MAIN)) .then((key, program) -> program.setUniformBlockBinding("FlwUniforms", 0))) .build(); } @@ -101,7 +104,7 @@ public class IndirectPrograms { .compiler(APPLY.program() .link(APPLY.shader(GlslVersion.V460, ShaderType.COMPUTE) .define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) - .withResource(Files.INDIRECT_APPLY))) + .withResource(APPLY_SHADER_MAIN))) .build(); } @@ -124,9 +127,4 @@ public class IndirectPrograms { .forEach(GlProgram::delete); apply.delete(); } - - private static final class Files { - public static final ResourceLocation INDIRECT_CULL = Flywheel.rl("internal/indirect/cull.glsl"); - public static final ResourceLocation INDIRECT_APPLY = Flywheel.rl("internal/indirect/apply.glsl"); - } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineCompiler.java b/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineCompiler.java index a1f9004df..560e2407a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineCompiler.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineCompiler.java @@ -24,7 +24,7 @@ public class PipelineCompiler { .assemble(new Pipeline.InstanceAssemblerContext(InternalVertex.LAYOUT.getAttributeCount(), key.instanceType()))) .withComponents(vertexComponents) .withResource(key -> key.instanceType() - .instanceShader()) + .vertexShader()) .withResource(key -> key.contextShader() .vertexShader()) .withResource(pipeline.vertexMain())) diff --git a/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java b/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java index 303ed12c2..d0b9d871f 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java +++ b/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java @@ -6,23 +6,27 @@ import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.context.Context; import com.jozufozu.flywheel.gl.shader.GlProgram; -import net.minecraft.resources.ResourceLocation; - public final class Contexts { - public static final SimpleContext DEFAULT = Context.REGISTRY.registerAndGet(new SimpleContext(Files.DEFAULT_VERTEX, Files.DEFAULT_FRAGMENT, program -> { - program.bind(); - program.setSamplerBinding("_flw_diffuseTex", 0); - program.setSamplerBinding("_flw_overlayTex", 1); - program.setSamplerBinding("_flw_lightTex", 2); - GlProgram.unbind(); - })); + public static final SimpleContext DEFAULT = Context.REGISTRY.registerAndGet(new SimpleContext( + Flywheel.rl("context/default.vert"), + Flywheel.rl("context/default.frag"), + program -> { + program.bind(); + program.setSamplerBinding("_flw_diffuseTex", 0); + program.setSamplerBinding("_flw_overlayTex", 1); + program.setSamplerBinding("_flw_lightTex", 2); + GlProgram.unbind(); + })); - public static final SimpleContext CRUMBLING = Context.REGISTRY.registerAndGet(new SimpleContext(Files.CRUMBLING_VERTEX, Files.CRUMBLING_FRAGMENT, program -> { - program.bind(); - program.setSamplerBinding("_flw_crumblingTex", 0); - program.setSamplerBinding("_flw_diffuseTex", 1); - GlProgram.unbind(); - })); + public static final SimpleContext CRUMBLING = Context.REGISTRY.registerAndGet(new SimpleContext( + Flywheel.rl("context/crumbling.vert"), + Flywheel.rl("context/crumbling.frag"), + program -> { + program.bind(); + program.setSamplerBinding("_flw_crumblingTex", 0); + program.setSamplerBinding("_flw_diffuseTex", 1); + GlProgram.unbind(); + })); private Contexts() { } @@ -30,16 +34,4 @@ public final class Contexts { @ApiStatus.Internal public static void init() { } - - public static final class Files { - public static final ResourceLocation DEFAULT_VERTEX = Names.DEFAULT.withSuffix(".vert"); - public static final ResourceLocation DEFAULT_FRAGMENT = Names.DEFAULT.withSuffix(".frag"); - public static final ResourceLocation CRUMBLING_VERTEX = Names.CRUMBLING.withSuffix(".vert"); - public static final ResourceLocation CRUMBLING_FRAGMENT = Names.CRUMBLING.withSuffix(".frag"); - } - - public static final class Names { - public static final ResourceLocation DEFAULT = Flywheel.rl("context/default"); - public static final ResourceLocation CRUMBLING = Flywheel.rl("context/crumbling"); - } } diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java b/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java index 7229b4c7f..e111b621c 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java @@ -2,11 +2,8 @@ package com.jozufozu.flywheel.lib.instance; import org.jetbrains.annotations.ApiStatus; -import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.instance.InstanceType; -import net.minecraft.resources.ResourceLocation; - public final class InstanceTypes { public static final InstanceType TRANSFORMED = InstanceType.REGISTRY.registerAndGet(new TransformedType()); public static final InstanceType ORIENTED = InstanceType.REGISTRY.registerAndGet(new OrientedType()); @@ -17,14 +14,4 @@ public final class InstanceTypes { @ApiStatus.Internal public static void init() { } - - public static final class Files { - public static final ResourceLocation TRANSFORMED = Names.TRANSFORMED.withSuffix(".vert"); - public static final ResourceLocation ORIENTED = Names.ORIENTED.withSuffix(".vert"); - } - - public static final class Names { - public static final ResourceLocation TRANSFORMED = Flywheel.rl("instance/transformed"); - public static final ResourceLocation ORIENTED = Flywheel.rl("instance/oriented"); - } } diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/OrientedType.java b/src/main/java/com/jozufozu/flywheel/lib/instance/OrientedType.java index 25059520d..cd9b50401 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/OrientedType.java +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/OrientedType.java @@ -3,6 +3,7 @@ package com.jozufozu.flywheel.lib.instance; import org.joml.Matrix3f; import org.joml.Matrix4f; +import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.instance.InstanceBoundingSphereTransformer; import com.jozufozu.flywheel.api.instance.InstanceHandle; import com.jozufozu.flywheel.api.instance.InstanceType; @@ -16,7 +17,7 @@ import com.jozufozu.flywheel.lib.vertex.VertexTransformations; import net.minecraft.resources.ResourceLocation; public class OrientedType implements InstanceType { - public static final BufferLayout FORMAT = BufferLayout.builder() + private static final BufferLayout LAYOUT = BufferLayout.builder() .addItem(CommonItems.LIGHT_COORD, "light") .addItem(CommonItems.UNORM_4x8, "color") .addItem(CommonItems.VEC3, "position") @@ -24,6 +25,9 @@ public class OrientedType implements InstanceType { .addItem(CommonItems.VEC4, "rotation") .build(); + private static final ResourceLocation VERTEX_SHADER = Flywheel.rl("instance/oriented.vert"); + private static final ResourceLocation CULL_SHADER = Flywheel.rl("instance/cull/oriented.glsl"); + @Override public OrientedInstance create(InstanceHandle handle) { return new OrientedInstance(this, handle); @@ -31,7 +35,7 @@ public class OrientedType implements InstanceType { @Override public BufferLayout getLayout() { - return FORMAT; + return LAYOUT; } @Override @@ -40,8 +44,13 @@ public class OrientedType implements InstanceType { } @Override - public ResourceLocation instanceShader() { - return InstanceTypes.Files.ORIENTED; + public ResourceLocation vertexShader() { + return VERTEX_SHADER; + } + + @Override + public ResourceLocation cullShader() { + return CULL_SHADER; } @Override diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedType.java b/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedType.java index 5b522b2c7..e7cc580c3 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedType.java +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedType.java @@ -1,5 +1,6 @@ package com.jozufozu.flywheel.lib.instance; +import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.instance.InstanceBoundingSphereTransformer; import com.jozufozu.flywheel.api.instance.InstanceHandle; import com.jozufozu.flywheel.api.instance.InstanceType; @@ -14,13 +15,16 @@ import com.jozufozu.flywheel.lib.vertex.VertexTransformations; import net.minecraft.resources.ResourceLocation; public class TransformedType implements InstanceType { - public static final BufferLayout FORMAT = BufferLayout.builder() + private static final BufferLayout LAYOUT = BufferLayout.builder() .addItem(CommonItems.LIGHT_COORD, "light") .addItem(CommonItems.UNORM_4x8, "color") .addItem(CommonItems.MAT4, "pose") .addItem(CommonItems.MAT3, "normal") .build(); + private static final ResourceLocation VERTEX_SHADER = Flywheel.rl("instance/transformed.vert"); + private static final ResourceLocation CULL_SHADER = Flywheel.rl("instance/cull/transformed.glsl"); + @Override public TransformedInstance create(InstanceHandle handle) { return new TransformedInstance(this, handle); @@ -28,7 +32,7 @@ public class TransformedType implements InstanceType { @Override public BufferLayout getLayout() { - return FORMAT; + return LAYOUT; } @Override @@ -37,8 +41,13 @@ public class TransformedType implements InstanceType { } @Override - public ResourceLocation instanceShader() { - return InstanceTypes.Files.TRANSFORMED; + public ResourceLocation vertexShader() { + return VERTEX_SHADER; + } + + @Override + public ResourceLocation cullShader() { + return CULL_SHADER; } @Override diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedWriter.java b/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedWriter.java index 52508e21a..a1406fcfe 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedWriter.java +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedWriter.java @@ -11,5 +11,4 @@ public class TransformedWriter extends ColoredLitWriter { MatrixMath.writeUnsafe(instance.model, ptr + 8); MatrixMath.writeUnsafe(instance.normal, ptr + 72); } - } diff --git a/src/main/java/com/jozufozu/flywheel/lib/material/StandardMaterialShaders.java b/src/main/java/com/jozufozu/flywheel/lib/material/StandardMaterialShaders.java index d11c81d1c..84baf4275 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/material/StandardMaterialShaders.java +++ b/src/main/java/com/jozufozu/flywheel/lib/material/StandardMaterialShaders.java @@ -5,11 +5,10 @@ import org.jetbrains.annotations.ApiStatus; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.material.MaterialShaders; -import net.minecraft.resources.ResourceLocation; - public final class StandardMaterialShaders { - - public static final MaterialShaders DEFAULT = MaterialShaders.REGISTRY.registerAndGet(new SimpleMaterialShaders(Files.DEFAULT_VERTEX, Files.DEFAULT_FRAGMENT)); + public static final MaterialShaders DEFAULT = MaterialShaders.REGISTRY.registerAndGet(new SimpleMaterialShaders( + Flywheel.rl("material/default.vert"), + Flywheel.rl("material/default.frag"))); private StandardMaterialShaders() { } @@ -17,13 +16,4 @@ public final class StandardMaterialShaders { @ApiStatus.Internal public static void init() { } - - public static final class Files { - public static final ResourceLocation DEFAULT_VERTEX = Names.DEFAULT.withSuffix(".vert"); - public static final ResourceLocation DEFAULT_FRAGMENT = Names.DEFAULT.withSuffix(".frag"); - } - - public static final class Names { - public static final ResourceLocation DEFAULT = Flywheel.rl("material/default"); - } } diff --git a/src/main/resources/assets/flywheel/flywheel/instance/cull/oriented.glsl b/src/main/resources/assets/flywheel/flywheel/instance/cull/oriented.glsl new file mode 100644 index 000000000..d38a3b58c --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/instance/cull/oriented.glsl @@ -0,0 +1,9 @@ +#include "flywheel:util/quaternion.glsl" + +void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout float radius) { + vec4 rotation = i.rotation; + vec3 pivot = i.pivot; + vec3 pos = i.position; + + center = rotateVertexByQuat(center - pivot, rotation) + pivot + pos; +} diff --git a/src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl b/src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl new file mode 100644 index 000000000..db7b66341 --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl @@ -0,0 +1,7 @@ +void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout float radius) { + mat4 pose = i.pose; + center = (pose * vec4(center, 1.0)).xyz; + + float scale = max(length(pose[0].xyz), max(length(pose[1].xyz), length(pose[2].xyz))); + radius *= scale; +} diff --git a/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert b/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert index 86289f220..dc96d1d7f 100644 --- a/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert +++ b/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert @@ -1,20 +1,8 @@ #include "flywheel:util/quaternion.glsl" -#ifdef COMPUTE_SHADER -void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout float radius) { - vec4 rotation = i.rotation; - vec3 pivot = i.pivot; - vec3 pos = i.position; - - center = rotateVertexByQuat(center - pivot, rotation) + pivot + pos; -} -#endif - -#ifdef VERTEX_SHADER void flw_instanceVertex(in FlwInstance i) { flw_vertexPos = vec4(rotateVertexByQuat(flw_vertexPos.xyz - i.pivot, i.rotation) + i.pivot + i.position, 1.0); flw_vertexNormal = rotateVertexByQuat(flw_vertexNormal, i.rotation); flw_vertexColor = i.color; flw_vertexLight = i.light / 15.0; } -#endif diff --git a/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert b/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert index 504c35275..6e7d125d3 100644 --- a/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert +++ b/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert @@ -1,18 +1,6 @@ -#ifdef COMPUTE_SHADER -void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout float radius) { - mat4 pose = i.pose; - center = (pose * vec4(center, 1.0)).xyz; - - float scale = max(length(pose[0].xyz), max(length(pose[1].xyz), length(pose[2].xyz))); - radius *= scale; -} -#endif - -#ifdef VERTEX_SHADER void flw_instanceVertex(in FlwInstance i) { flw_vertexPos = i.pose * flw_vertexPos; flw_vertexNormal = i.normal * flw_vertexNormal; flw_vertexColor = i.color; flw_vertexLight = i.light / 15.0; } -#endif