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 1def0876d8
commit cbbd2bfdf8
13 changed files with 74 additions and 96 deletions

View File

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

View File

@ -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<InstanceType<?>> CULL = new Compile<>();
private static final Compile<Unit> 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");
}
}

View File

@ -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()))

View File

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

View File

@ -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<TransformedInstance> TRANSFORMED = InstanceType.REGISTRY.registerAndGet(new TransformedType());
public static final InstanceType<OrientedInstance> 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");
}
}

View File

@ -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<OrientedInstance> {
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<OrientedInstance> {
.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<OrientedInstance> {
@Override
public BufferLayout getLayout() {
return FORMAT;
return LAYOUT;
}
@Override
@ -40,8 +44,13 @@ public class OrientedType implements InstanceType<OrientedInstance> {
}
@Override
public ResourceLocation instanceShader() {
return InstanceTypes.Files.ORIENTED;
public ResourceLocation vertexShader() {
return VERTEX_SHADER;
}
@Override
public ResourceLocation cullShader() {
return CULL_SHADER;
}
@Override

View File

@ -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<TransformedInstance> {
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<TransformedInstance> {
@Override
public BufferLayout getLayout() {
return FORMAT;
return LAYOUT;
}
@Override
@ -37,8 +41,13 @@ public class TransformedType implements InstanceType<TransformedInstance> {
}
@Override
public ResourceLocation instanceShader() {
return InstanceTypes.Files.TRANSFORMED;
public ResourceLocation vertexShader() {
return VERTEX_SHADER;
}
@Override
public ResourceLocation cullShader() {
return CULL_SHADER;
}
@Override

View File

@ -11,5 +11,4 @@ public class TransformedWriter extends ColoredLitWriter<TransformedInstance> {
MatrixMath.writeUnsafe(instance.model, ptr + 8);
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.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");
}
}

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"
#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

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) {
flw_vertexPos = i.pose * flw_vertexPos;
flw_vertexNormal = i.normal * flw_vertexNormal;
flw_vertexColor = i.color;
flw_vertexLight = i.light / 15.0;
}
#endif