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
This commit is contained in:
PepperCode1 2023-12-08 17:02:12 -08:00
parent 2c9a3edb97
commit a8b51aacbe
13 changed files with 74 additions and 96 deletions

View file

@ -27,7 +27,9 @@ public interface InstanceType<I extends Instance> {
InstanceWriter<I> getWriter(); InstanceWriter<I> getWriter();
ResourceLocation instanceShader(); ResourceLocation vertexShader();
ResourceLocation cullShader();
InstanceVertexTransformer<I> getVertexTransformer(); InstanceVertexTransformer<I> getVertexTransformer();

View file

@ -22,6 +22,9 @@ import com.jozufozu.flywheel.lib.util.Unit;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
public class IndirectPrograms { 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; public static IndirectPrograms instance;
private static final Compile<InstanceType<?>> CULL = new Compile<>(); private static final Compile<InstanceType<?>> CULL = new Compile<>();
private static final Compile<Unit> APPLY = new Compile<>(); private static final Compile<Unit> APPLY = new Compile<>();
@ -89,8 +92,8 @@ public class IndirectPrograms {
.define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) .define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE)
.withComponent(uniformComponent) .withComponent(uniformComponent)
.withComponent(IndirectComponent::create) .withComponent(IndirectComponent::create)
.withResource(InstanceType::instanceShader) .withResource(InstanceType::cullShader)
.withResource(Files.INDIRECT_CULL)) .withResource(CULL_SHADER_MAIN))
.then((key, program) -> program.setUniformBlockBinding("FlwUniforms", 0))) .then((key, program) -> program.setUniformBlockBinding("FlwUniforms", 0)))
.build(); .build();
} }
@ -101,7 +104,7 @@ public class IndirectPrograms {
.compiler(APPLY.program() .compiler(APPLY.program()
.link(APPLY.shader(GlslVersion.V460, ShaderType.COMPUTE) .link(APPLY.shader(GlslVersion.V460, ShaderType.COMPUTE)
.define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) .define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE)
.withResource(Files.INDIRECT_APPLY))) .withResource(APPLY_SHADER_MAIN)))
.build(); .build();
} }
@ -124,9 +127,4 @@ public class IndirectPrograms {
.forEach(GlProgram::delete); .forEach(GlProgram::delete);
apply.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");
}
} }

View file

@ -24,7 +24,7 @@ public class PipelineCompiler {
.assemble(new Pipeline.InstanceAssemblerContext(InternalVertex.LAYOUT.getAttributeCount(), key.instanceType()))) .assemble(new Pipeline.InstanceAssemblerContext(InternalVertex.LAYOUT.getAttributeCount(), key.instanceType())))
.withComponents(vertexComponents) .withComponents(vertexComponents)
.withResource(key -> key.instanceType() .withResource(key -> key.instanceType()
.instanceShader()) .vertexShader())
.withResource(key -> key.contextShader() .withResource(key -> key.contextShader()
.vertexShader()) .vertexShader())
.withResource(pipeline.vertexMain())) .withResource(pipeline.vertexMain()))

View file

@ -6,23 +6,27 @@ import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.context.Context; import com.jozufozu.flywheel.api.context.Context;
import com.jozufozu.flywheel.gl.shader.GlProgram; import com.jozufozu.flywheel.gl.shader.GlProgram;
import net.minecraft.resources.ResourceLocation;
public final class Contexts { public final class Contexts {
public static final SimpleContext DEFAULT = Context.REGISTRY.registerAndGet(new SimpleContext(Files.DEFAULT_VERTEX, Files.DEFAULT_FRAGMENT, program -> { public static final SimpleContext DEFAULT = Context.REGISTRY.registerAndGet(new SimpleContext(
program.bind(); Flywheel.rl("context/default.vert"),
program.setSamplerBinding("_flw_diffuseTex", 0); Flywheel.rl("context/default.frag"),
program.setSamplerBinding("_flw_overlayTex", 1); program -> {
program.setSamplerBinding("_flw_lightTex", 2); program.bind();
GlProgram.unbind(); 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 -> { public static final SimpleContext CRUMBLING = Context.REGISTRY.registerAndGet(new SimpleContext(
program.bind(); Flywheel.rl("context/crumbling.vert"),
program.setSamplerBinding("_flw_crumblingTex", 0); Flywheel.rl("context/crumbling.frag"),
program.setSamplerBinding("_flw_diffuseTex", 1); program -> {
GlProgram.unbind(); program.bind();
})); program.setSamplerBinding("_flw_crumblingTex", 0);
program.setSamplerBinding("_flw_diffuseTex", 1);
GlProgram.unbind();
}));
private Contexts() { private Contexts() {
} }
@ -30,16 +34,4 @@ public final class Contexts {
@ApiStatus.Internal @ApiStatus.Internal
public static void init() { 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");
}
} }

View file

@ -2,11 +2,8 @@ package com.jozufozu.flywheel.lib.instance;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.InstanceType;
import net.minecraft.resources.ResourceLocation;
public final class InstanceTypes { public final class InstanceTypes {
public static final InstanceType<TransformedInstance> TRANSFORMED = InstanceType.REGISTRY.registerAndGet(new TransformedType()); public static final InstanceType<TransformedInstance> TRANSFORMED = InstanceType.REGISTRY.registerAndGet(new TransformedType());
public static final InstanceType<OrientedInstance> ORIENTED = InstanceType.REGISTRY.registerAndGet(new OrientedType()); public static final InstanceType<OrientedInstance> ORIENTED = InstanceType.REGISTRY.registerAndGet(new OrientedType());
@ -17,14 +14,4 @@ public final class InstanceTypes {
@ApiStatus.Internal @ApiStatus.Internal
public static void init() { 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");
}
} }

View file

@ -3,6 +3,7 @@ package com.jozufozu.flywheel.lib.instance;
import org.joml.Matrix3f; import org.joml.Matrix3f;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.instance.InstanceBoundingSphereTransformer; import com.jozufozu.flywheel.api.instance.InstanceBoundingSphereTransformer;
import com.jozufozu.flywheel.api.instance.InstanceHandle; import com.jozufozu.flywheel.api.instance.InstanceHandle;
import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.InstanceType;
@ -16,7 +17,7 @@ import com.jozufozu.flywheel.lib.vertex.VertexTransformations;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
public class OrientedType implements InstanceType<OrientedInstance> { public class OrientedType implements InstanceType<OrientedInstance> {
public static final BufferLayout FORMAT = BufferLayout.builder() private static final BufferLayout LAYOUT = BufferLayout.builder()
.addItem(CommonItems.LIGHT_COORD, "light") .addItem(CommonItems.LIGHT_COORD, "light")
.addItem(CommonItems.UNORM_4x8, "color") .addItem(CommonItems.UNORM_4x8, "color")
.addItem(CommonItems.VEC3, "position") .addItem(CommonItems.VEC3, "position")
@ -24,6 +25,9 @@ public class OrientedType implements InstanceType<OrientedInstance> {
.addItem(CommonItems.VEC4, "rotation") .addItem(CommonItems.VEC4, "rotation")
.build(); .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 @Override
public OrientedInstance create(InstanceHandle handle) { public OrientedInstance create(InstanceHandle handle) {
return new OrientedInstance(this, handle); return new OrientedInstance(this, handle);
@ -31,7 +35,7 @@ public class OrientedType implements InstanceType<OrientedInstance> {
@Override @Override
public BufferLayout getLayout() { public BufferLayout getLayout() {
return FORMAT; return LAYOUT;
} }
@Override @Override
@ -40,8 +44,13 @@ public class OrientedType implements InstanceType<OrientedInstance> {
} }
@Override @Override
public ResourceLocation instanceShader() { public ResourceLocation vertexShader() {
return InstanceTypes.Files.ORIENTED; return VERTEX_SHADER;
}
@Override
public ResourceLocation cullShader() {
return CULL_SHADER;
} }
@Override @Override

View file

@ -1,5 +1,6 @@
package com.jozufozu.flywheel.lib.instance; package com.jozufozu.flywheel.lib.instance;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.instance.InstanceBoundingSphereTransformer; import com.jozufozu.flywheel.api.instance.InstanceBoundingSphereTransformer;
import com.jozufozu.flywheel.api.instance.InstanceHandle; import com.jozufozu.flywheel.api.instance.InstanceHandle;
import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.InstanceType;
@ -14,13 +15,16 @@ import com.jozufozu.flywheel.lib.vertex.VertexTransformations;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
public class TransformedType implements InstanceType<TransformedInstance> { public class TransformedType implements InstanceType<TransformedInstance> {
public static final BufferLayout FORMAT = BufferLayout.builder() private static final BufferLayout LAYOUT = BufferLayout.builder()
.addItem(CommonItems.LIGHT_COORD, "light") .addItem(CommonItems.LIGHT_COORD, "light")
.addItem(CommonItems.UNORM_4x8, "color") .addItem(CommonItems.UNORM_4x8, "color")
.addItem(CommonItems.MAT4, "pose") .addItem(CommonItems.MAT4, "pose")
.addItem(CommonItems.MAT3, "normal") .addItem(CommonItems.MAT3, "normal")
.build(); .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 @Override
public TransformedInstance create(InstanceHandle handle) { public TransformedInstance create(InstanceHandle handle) {
return new TransformedInstance(this, handle); return new TransformedInstance(this, handle);
@ -28,7 +32,7 @@ public class TransformedType implements InstanceType<TransformedInstance> {
@Override @Override
public BufferLayout getLayout() { public BufferLayout getLayout() {
return FORMAT; return LAYOUT;
} }
@Override @Override
@ -37,8 +41,13 @@ public class TransformedType implements InstanceType<TransformedInstance> {
} }
@Override @Override
public ResourceLocation instanceShader() { public ResourceLocation vertexShader() {
return InstanceTypes.Files.TRANSFORMED; return VERTEX_SHADER;
}
@Override
public ResourceLocation cullShader() {
return CULL_SHADER;
} }
@Override @Override

View file

@ -11,5 +11,4 @@ public class TransformedWriter extends ColoredLitWriter<TransformedInstance> {
MatrixMath.writeUnsafe(instance.model, ptr + 8); MatrixMath.writeUnsafe(instance.model, ptr + 8);
MatrixMath.writeUnsafe(instance.normal, ptr + 72); MatrixMath.writeUnsafe(instance.normal, ptr + 72);
} }
} }

View file

@ -5,11 +5,10 @@ import org.jetbrains.annotations.ApiStatus;
import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.material.MaterialShaders; import com.jozufozu.flywheel.api.material.MaterialShaders;
import net.minecraft.resources.ResourceLocation;
public final class StandardMaterialShaders { public final class StandardMaterialShaders {
public static final MaterialShaders DEFAULT = MaterialShaders.REGISTRY.registerAndGet(new SimpleMaterialShaders(
public static final MaterialShaders DEFAULT = MaterialShaders.REGISTRY.registerAndGet(new SimpleMaterialShaders(Files.DEFAULT_VERTEX, Files.DEFAULT_FRAGMENT)); Flywheel.rl("material/default.vert"),
Flywheel.rl("material/default.frag")));
private StandardMaterialShaders() { private StandardMaterialShaders() {
} }
@ -17,13 +16,4 @@ public final class StandardMaterialShaders {
@ApiStatus.Internal @ApiStatus.Internal
public static void init() { 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");
}
} }

View file

@ -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;
}

View file

@ -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;
}

View file

@ -1,20 +1,8 @@
#include "flywheel:util/quaternion.glsl" #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) { void flw_instanceVertex(in FlwInstance i) {
flw_vertexPos = vec4(rotateVertexByQuat(flw_vertexPos.xyz - i.pivot, i.rotation) + i.pivot + i.position, 1.0); 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_vertexNormal = rotateVertexByQuat(flw_vertexNormal, i.rotation);
flw_vertexColor = i.color; flw_vertexColor = i.color;
flw_vertexLight = i.light / 15.0; flw_vertexLight = i.light / 15.0;
} }
#endif

View file

@ -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) { 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_vertexLight = i.light / 15.0; flw_vertexLight = i.light / 15.0;
} }
#endif