From 9cf7a1c307ce1ffc90d396510d9ea98672e7341e Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Wed, 6 Dec 2023 22:00:31 -0800 Subject: [PATCH] Sparse changes - Add Model.boundingSphere() - Fix MaterialRenderState not setting up polygon offset correctly - Fix GlslSwitch using incorrect indentation for first and last line - Tweak how instanced crumbling works - Add CutoutShaders.ONE_TENTH - Rename Contexts.WORLD to DEFAULT - Rename Material.baseTexture() to texture - Rename Transparency.LIGHTING to LIGHTNING - Rename WriteMask.BOTH to COLOR_DEPTH - Rename SimpleModel to SingleMeshModel and add new SimpleModel that stores an arbitrary amount of meshes - Remove flywheel:flywheel/api/* files and assume appropriate symbols are automatically defined - Rename many GLSL variables, functions, and constants - Reorganize GLSL files - Add NonExtendable annotation to some API classes - Rename some Java classes, methods, fields, and variables --- .../api => docs/shader-api}/fragment.glsl | 11 +- .../api => docs/shader-api}/material.glsl | 4 +- .../api => docs/shader-api}/vertex.glsl | 9 +- .../flywheel/api/material/Material.java | 2 +- .../flywheel/api/material/Transparency.java | 2 +- .../flywheel/api/material/WriteMask.java | 10 +- .../jozufozu/flywheel/api/model/Model.java | 24 +- .../flywheel/api/task/TaskExecutor.java | 21 +- .../api/visualization/VisualManager.java | 3 + .../visualization/VisualizationManager.java | 2 + .../flywheel/backend/compile/Compile.java | 8 +- .../flywheel/backend/compile/FlwPrograms.java | 49 ++-- .../backend/compile/IndirectPrograms.java | 23 +- .../flywheel/backend/compile/Pipeline.java | 44 ++-- .../backend/compile/PipelineCompiler.java | 10 +- .../flywheel/backend/compile/Pipelines.java | 22 +- .../compile/component/UniformComponent.java | 4 +- .../backend/compile/core/Compilation.java | 10 +- .../backend/compile/core/ShaderCompiler.java | 6 +- .../backend/{ => engine}/MaterialEncoder.java | 3 +- .../{ => engine}/MaterialRenderState.java | 10 +- .../backend/engine/UniformBuffer.java | 47 ++-- .../engine/batching/BatchedDrawManager.java | 2 +- .../engine/indirect/IndirectBuffers.java | 29 ++- .../engine/indirect/IndirectCullingGroup.java | 236 +++++++++--------- .../backend/engine/indirect/IndirectDraw.java | 29 +-- .../engine/indirect/IndirectDrawManager.java | 18 +- .../engine/indirect/IndirectEngine.java | 6 +- .../engine/indirect/IndirectInstancer.java | 16 +- .../engine/indirect/IndirectMeshPool.java | 21 +- .../engine/indirect/IndirectModel.java | 30 +-- .../{EBOCache.java => EboCache.java} | 9 +- .../engine/instancing/InstancedCrumbling.java | 17 +- .../instancing/InstancedDrawManager.java | 4 +- .../engine/instancing/InstancedMeshPool.java | 4 +- .../engine/instancing/InstancingEngine.java | 9 +- .../jozufozu/flywheel/config/FlwCommands.java | 8 +- .../com/jozufozu/flywheel/gl/GlCompat.java | 52 ++-- .../{GLSLVersion.java => GlslVersion.java} | 4 +- .../jozufozu/flywheel/glsl/ShaderSources.java | 7 - .../flywheel/glsl/generate/GlslSwitch.java | 6 +- .../flywheel/lib/context/Contexts.java | 8 +- .../lib/instance/AbstractInstance.java | 4 +- .../flywheel/lib/material/CutoutShaders.java | 4 + .../flywheel/lib/material/Materials.java | 21 +- .../flywheel/lib/material/SimpleMaterial.java | 24 +- .../flywheel/lib/model/ModelUtil.java | 29 ++- .../flywheel/lib/model/SimpleModel.java | 31 ++- .../flywheel/lib/model/SingleMeshModel.java | 40 +++ .../lib/model/baked/TessellatedModel.java | 20 +- .../lib/uniform/FlwShaderUniforms.java | 14 +- .../flywheel/mixin/FogUpdateMixin.java | 2 +- .../jozufozu/flywheel/vanilla/BellVisual.java | 4 +- .../flywheel/vanilla/ChestVisual.java | 8 +- .../flywheel/vanilla/MinecartVisual.java | 4 +- .../flywheel/vanilla/ShulkerBoxVisual.java | 6 +- .../flywheel/flywheel/context/crumbling.frag | 16 +- .../flywheel/flywheel/context/crumbling.vert | 3 - .../context/{world.frag => default.frag} | 2 - .../flywheel/flywheel/context/default.vert | 5 + .../flywheel/flywheel/context/world.vert | 8 - .../flywheel/flywheel/cutout/epsilon.glsl | 4 +- .../assets/flywheel/flywheel/cutout/half.glsl | 4 +- .../assets/flywheel/flywheel/cutout/off.glsl | 2 +- .../flywheel/flywheel/cutout/one_tenth.glsl | 3 + .../assets/flywheel/flywheel/fog/linear.glsl | 11 +- .../flywheel/flywheel/fog/linear_fade.glsl | 12 +- .../flywheel/flywheel/instance/oriented.vert | 1 - .../flywheel/instance/transformed.vert | 2 - .../flywheel/internal/common_api_impl.frag | 18 ++ .../flywheel/internal/common_api_impl.vert | 12 + .../flywheel/{util => internal}/diffuse.glsl | 0 .../flywheel/internal/fog_distance.glsl | 21 ++ .../internal/indirect/api/fragment.glsl | 44 ---- .../internal/indirect/api/vertex.glsl | 36 --- .../flywheel/internal/indirect/api_impl.frag | 5 + .../flywheel/internal/indirect/api_impl.vert | 3 + .../flywheel/internal/indirect/apply.glsl | 18 +- .../flywheel/internal/indirect/buffers.glsl | 8 +- .../flywheel/internal/indirect/cull.glsl | 33 ++- .../flywheel/internal/indirect/draw.vert | 56 ----- .../internal/indirect/draw_command.glsl | 7 +- .../indirect/{draw.frag => main.frag} | 16 +- .../flywheel/internal/indirect/main.vert | 57 +++++ .../internal/indirect/model_descriptor.glsl | 10 +- .../flywheel/internal/indirect/object.glsl | 2 +- .../internal/instancing/api/fragment.glsl | 43 ---- .../internal/instancing/api/vertex.glsl | 35 --- .../internal/instancing/api_impl.frag | 5 + .../internal/instancing/api_impl.vert | 3 + .../instancing/{draw.frag => main.frag} | 12 +- .../instancing/{draw.vert => main.vert} | 17 +- .../internal/{block.vert => layout.vert} | 2 - .../flywheel/flywheel/internal/material.glsl | 84 +++---- .../flywheel/internal/packed_material.glsl | 55 ++++ .../flywheel/flywheel/material/default.frag | 2 - .../flywheel/flywheel/material/default.vert | 2 - .../flywheel/flywheel/uniform/flywheel.glsl | 6 +- .../assets/flywheel/flywheel/util/fog.glsl | 40 --- .../flywheel/flywheel/util/quaternion.glsl | 6 +- 100 files changed, 860 insertions(+), 921 deletions(-) rename {src/main/resources/assets/flywheel/flywheel/api => docs/shader-api}/fragment.glsl (80%) rename {src/main/resources/assets/flywheel/flywheel/api => docs/shader-api}/material.glsl (90%) rename {src/main/resources/assets/flywheel/flywheel/api => docs/shader-api}/vertex.glsl (80%) rename src/main/java/com/jozufozu/flywheel/backend/{ => engine}/MaterialEncoder.java (97%) rename src/main/java/com/jozufozu/flywheel/backend/{ => engine}/MaterialRenderState.java (96%) rename src/main/java/com/jozufozu/flywheel/backend/engine/instancing/{EBOCache.java => EboCache.java} (93%) rename src/main/java/com/jozufozu/flywheel/glsl/{GLSLVersion.java => GlslVersion.java} (88%) create mode 100644 src/main/java/com/jozufozu/flywheel/lib/model/SingleMeshModel.java rename src/main/resources/assets/flywheel/flywheel/context/{world.frag => default.frag} (59%) create mode 100644 src/main/resources/assets/flywheel/flywheel/context/default.vert delete mode 100644 src/main/resources/assets/flywheel/flywheel/context/world.vert create mode 100644 src/main/resources/assets/flywheel/flywheel/cutout/one_tenth.glsl create mode 100644 src/main/resources/assets/flywheel/flywheel/internal/common_api_impl.frag create mode 100644 src/main/resources/assets/flywheel/flywheel/internal/common_api_impl.vert rename src/main/resources/assets/flywheel/flywheel/{util => internal}/diffuse.glsl (100%) create mode 100644 src/main/resources/assets/flywheel/flywheel/internal/fog_distance.glsl delete mode 100644 src/main/resources/assets/flywheel/flywheel/internal/indirect/api/fragment.glsl delete mode 100644 src/main/resources/assets/flywheel/flywheel/internal/indirect/api/vertex.glsl create mode 100644 src/main/resources/assets/flywheel/flywheel/internal/indirect/api_impl.frag create mode 100644 src/main/resources/assets/flywheel/flywheel/internal/indirect/api_impl.vert delete mode 100644 src/main/resources/assets/flywheel/flywheel/internal/indirect/draw.vert rename src/main/resources/assets/flywheel/flywheel/internal/indirect/{draw.frag => main.frag} (71%) create mode 100644 src/main/resources/assets/flywheel/flywheel/internal/indirect/main.vert delete mode 100644 src/main/resources/assets/flywheel/flywheel/internal/instancing/api/fragment.glsl delete mode 100644 src/main/resources/assets/flywheel/flywheel/internal/instancing/api/vertex.glsl create mode 100644 src/main/resources/assets/flywheel/flywheel/internal/instancing/api_impl.frag create mode 100644 src/main/resources/assets/flywheel/flywheel/internal/instancing/api_impl.vert rename src/main/resources/assets/flywheel/flywheel/internal/instancing/{draw.frag => main.frag} (78%) rename src/main/resources/assets/flywheel/flywheel/internal/instancing/{draw.vert => main.vert} (62%) rename src/main/resources/assets/flywheel/flywheel/internal/{block.vert => layout.vert} (92%) create mode 100644 src/main/resources/assets/flywheel/flywheel/internal/packed_material.glsl delete mode 100644 src/main/resources/assets/flywheel/flywheel/util/fog.glsl diff --git a/src/main/resources/assets/flywheel/flywheel/api/fragment.glsl b/docs/shader-api/fragment.glsl similarity index 80% rename from src/main/resources/assets/flywheel/flywheel/api/fragment.glsl rename to docs/shader-api/fragment.glsl index 4011f821f..659f788a0 100644 --- a/src/main/resources/assets/flywheel/flywheel/api/fragment.glsl +++ b/docs/shader-api/fragment.glsl @@ -7,22 +7,17 @@ /*const*/ vec2 flw_vertexLight; /*const*/ vec3 flw_vertexNormal; -/*const*/ float flw_distance; - -/*const*/ vec4 flw_var0; -/*const*/ vec4 flw_var1; -/*const*/ vec4 flw_var2; -/*const*/ vec4 flw_var3; - /*const*/ FlwMaterial flw_material; /*const*/ vec4 flw_sampleColor; +/*const*/ float flw_distance; + vec4 flw_fragColor; ivec2 flw_fragOverlay; vec2 flw_fragLight; -// To be implemented by material shaders. +// To be implemented by the material fragment shader. vec4 flw_fogFilter(vec4 color); bool flw_discardPredicate(vec4 finalColor); void flw_materialFragment(); diff --git a/src/main/resources/assets/flywheel/flywheel/api/material.glsl b/docs/shader-api/material.glsl similarity index 90% rename from src/main/resources/assets/flywheel/flywheel/api/material.glsl rename to docs/shader-api/material.glsl index ceb23458e..40af43e2f 100644 --- a/src/main/resources/assets/flywheel/flywheel/api/material.glsl +++ b/docs/shader-api/material.glsl @@ -10,12 +10,12 @@ const uint FLW_MAT_DEPTH_TEST_ALWAYS = 8u; const uint FLW_MAT_TRANSPARENCY_OPAQUE = 0u; const uint FLW_MAT_TRANSPARENCY_ADDITIVE = 1u; -const uint FLW_MAT_TRANSPARENCY_LIGHTING = 2u; +const uint FLW_MAT_TRANSPARENCY_LIGHTNING = 2u; const uint FLW_MAT_TRANSPARENCY_GLINT = 3u; const uint FLW_MAT_TRANSPARENCY_CRUMBLING = 4u; const uint FLW_MAT_TRANSPARENCY_TRANSLUCENT = 5u; -const uint FLW_MAT_WRITE_MASK_BOTH = 0u; +const uint FLW_MAT_WRITE_MASK_COLOR_DEPTH = 0u; const uint FLW_MAT_WRITE_MASK_COLOR = 1u; const uint FLW_MAT_WRITE_MASK_DEPTH = 2u; diff --git a/src/main/resources/assets/flywheel/flywheel/api/vertex.glsl b/docs/shader-api/vertex.glsl similarity index 80% rename from src/main/resources/assets/flywheel/flywheel/api/vertex.glsl rename to docs/shader-api/vertex.glsl index 5282cbfe2..6601e8da0 100644 --- a/src/main/resources/assets/flywheel/flywheel/api/vertex.glsl +++ b/docs/shader-api/vertex.glsl @@ -7,20 +7,13 @@ ivec2 flw_vertexOverlay; vec2 flw_vertexLight; vec3 flw_vertexNormal; -float flw_distance; - -vec4 flw_var0; -vec4 flw_var1; -vec4 flw_var2; -vec4 flw_var3; - /*const*/ FlwMaterial flw_material; // To be implemented by the instance shader. void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout float radius); void flw_instanceVertex(FlwInstance i); -// To be implemented by material shaders. +// To be implemented by the material vertex shader. void flw_materialVertex(); // To be implemented by the context shader. diff --git a/src/main/java/com/jozufozu/flywheel/api/material/Material.java b/src/main/java/com/jozufozu/flywheel/api/material/Material.java index 3e6295d26..008f6f8ba 100644 --- a/src/main/java/com/jozufozu/flywheel/api/material/Material.java +++ b/src/main/java/com/jozufozu/flywheel/api/material/Material.java @@ -14,7 +14,7 @@ public interface Material { CutoutShader cutout(); - ResourceLocation baseTexture(); + ResourceLocation texture(); /** * Should this material have linear filtering applied to the diffuse sampler? diff --git a/src/main/java/com/jozufozu/flywheel/api/material/Transparency.java b/src/main/java/com/jozufozu/flywheel/api/material/Transparency.java index ecdc3cebb..a8c42142d 100644 --- a/src/main/java/com/jozufozu/flywheel/api/material/Transparency.java +++ b/src/main/java/com/jozufozu/flywheel/api/material/Transparency.java @@ -3,7 +3,7 @@ package com.jozufozu.flywheel.api.material; public enum Transparency { OPAQUE, ADDITIVE, - LIGHTING, + LIGHTNING, GLINT, CRUMBLING, TRANSLUCENT, diff --git a/src/main/java/com/jozufozu/flywheel/api/material/WriteMask.java b/src/main/java/com/jozufozu/flywheel/api/material/WriteMask.java index 74511f3d8..8c91b559c 100644 --- a/src/main/java/com/jozufozu/flywheel/api/material/WriteMask.java +++ b/src/main/java/com/jozufozu/flywheel/api/material/WriteMask.java @@ -4,7 +4,7 @@ public enum WriteMask { /** * Write to both the color and depth buffers. */ - BOTH, + COLOR_DEPTH, /** * Write to the color buffer only. */ @@ -15,11 +15,11 @@ public enum WriteMask { DEPTH, ; - public boolean depth() { - return this == BOTH || this == DEPTH; + public boolean color() { + return this == COLOR_DEPTH || this == COLOR; } - public boolean color() { - return this == BOTH || this == COLOR; + public boolean depth() { + return this == COLOR_DEPTH || this == DEPTH; } } diff --git a/src/main/java/com/jozufozu/flywheel/api/model/Model.java b/src/main/java/com/jozufozu/flywheel/api/model/Model.java index d3d4234e6..b7835540b 100644 --- a/src/main/java/com/jozufozu/flywheel/api/model/Model.java +++ b/src/main/java/com/jozufozu/flywheel/api/model/Model.java @@ -2,18 +2,24 @@ package com.jozufozu.flywheel.api.model; import java.util.Map; +import org.joml.Vector4fc; + import com.jozufozu.flywheel.api.material.Material; public interface Model { - Map getMeshes(); + Map meshes(); + + /** + * Get a vec4 representing this model's bounding sphere in the format (x, y, z, radius). + * It should encompass all meshes' bounding spheres. + * + * @return A vec4 view. + */ + Vector4fc boundingSphere(); + + // TODO: unused. remove? + @Deprecated + int vertexCount(); void delete(); - - default int getVertexCount() { - int size = 0; - for (Mesh mesh : getMeshes().values()) { - size += mesh.vertexCount(); - } - return size; - } } diff --git a/src/main/java/com/jozufozu/flywheel/api/task/TaskExecutor.java b/src/main/java/com/jozufozu/flywheel/api/task/TaskExecutor.java index 92298d28e..a736ef091 100644 --- a/src/main/java/com/jozufozu/flywheel/api/task/TaskExecutor.java +++ b/src/main/java/com/jozufozu/flywheel/api/task/TaskExecutor.java @@ -3,6 +3,9 @@ package com.jozufozu.flywheel.api.task; import java.util.concurrent.Executor; import java.util.function.BooleanSupplier; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.NonExtendable public interface TaskExecutor extends Executor { /** * Wait for all running tasks to finish. @@ -38,15 +41,6 @@ public interface TaskExecutor extends Executor { */ boolean syncWhile(BooleanSupplier cond); - /** - * Check for the number of threads this executor uses. - *
- * May be helpful when determining how many chunks to divide a task into. - * - * @return The number of threads this executor uses. - */ - int getThreadCount(); - /** * Schedule a task to be run on the main thread. *
@@ -64,4 +58,13 @@ public interface TaskExecutor extends Executor { * @return {@code true} if the current thread is the main thread. */ boolean isMainThread(); + + /** + * Check for the number of threads this executor uses. + *
+ * May be helpful when determining how many chunks to divide a task into. + * + * @return The number of threads this executor uses. + */ + int getThreadCount(); } diff --git a/src/main/java/com/jozufozu/flywheel/api/visualization/VisualManager.java b/src/main/java/com/jozufozu/flywheel/api/visualization/VisualManager.java index 36b398b7e..b3ba831d7 100644 --- a/src/main/java/com/jozufozu/flywheel/api/visualization/VisualManager.java +++ b/src/main/java/com/jozufozu/flywheel/api/visualization/VisualManager.java @@ -1,5 +1,8 @@ package com.jozufozu.flywheel.api.visualization; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.NonExtendable public interface VisualManager { /** * Get the number of game objects that are currently being visualized. diff --git a/src/main/java/com/jozufozu/flywheel/api/visualization/VisualizationManager.java b/src/main/java/com/jozufozu/flywheel/api/visualization/VisualizationManager.java index ab42306ff..edeb95552 100644 --- a/src/main/java/com/jozufozu/flywheel/api/visualization/VisualizationManager.java +++ b/src/main/java/com/jozufozu/flywheel/api/visualization/VisualizationManager.java @@ -1,5 +1,6 @@ package com.jozufozu.flywheel.api.visualization; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import com.jozufozu.flywheel.api.visual.Effect; @@ -12,6 +13,7 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.block.entity.BlockEntity; +@ApiStatus.NonExtendable public interface VisualizationManager { static boolean supportsVisualization(@Nullable LevelAccessor level) { return VisualizationManagerImpl.supportsVisualization(level); diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/Compile.java b/src/main/java/com/jozufozu/flywheel/backend/compile/Compile.java index 753c2c0b1..bd9308ad1 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/Compile.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/Compile.java @@ -19,7 +19,7 @@ import com.jozufozu.flywheel.backend.compile.core.ShaderCompiler; import com.jozufozu.flywheel.gl.shader.GlProgram; import com.jozufozu.flywheel.gl.shader.GlShader; import com.jozufozu.flywheel.gl.shader.ShaderType; -import com.jozufozu.flywheel.glsl.GLSLVersion; +import com.jozufozu.flywheel.glsl.GlslVersion; import com.jozufozu.flywheel.glsl.ShaderSources; import com.jozufozu.flywheel.glsl.SourceComponent; @@ -36,7 +36,7 @@ import net.minecraft.resources.ResourceLocation; * @param The type of the key used to compile shaders. */ public class Compile { - public ShaderCompilerBuilder shader(GLSLVersion glslVersion, ShaderType shaderType) { + public ShaderCompilerBuilder shader(GlslVersion glslVersion, ShaderType shaderType) { return new ShaderCompilerBuilder<>(glslVersion, shaderType); } @@ -99,13 +99,13 @@ public class Compile { } public static class ShaderCompilerBuilder { - private final GLSLVersion glslVersion; + private final GlslVersion glslVersion; private final ShaderType shaderType; private Consumer compilationCallbacks = $ -> { }; private final List> fetchers = new ArrayList<>(); - public ShaderCompilerBuilder(GLSLVersion glslVersion, ShaderType shaderType) { + public ShaderCompilerBuilder(GlslVersion glslVersion, ShaderType shaderType) { this.glslVersion = glslVersion; this.shaderType = shaderType; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/FlwPrograms.java b/src/main/java/com/jozufozu/flywheel/backend/compile/FlwPrograms.java index 2519c67d5..a0e60ee5d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/FlwPrograms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/FlwPrograms.java @@ -24,8 +24,7 @@ public class FlwPrograms { } public static void reload(ResourceManager resourceManager) { - var empty = List.of(Flywheel.rl("api/fragment.glsl"), Flywheel.rl("api/vertex.glsl")); - var sources = new ShaderSources(resourceManager, empty); + var sources = new ShaderSources(resourceManager); var preLoadStats = new CompilerStats(); var loadChecker = new SourceLoader(sources, preLoadStats); @@ -43,12 +42,31 @@ public class FlwPrograms { } } + private static ImmutableList createPipelineKeys() { + ImmutableList.Builder builder = ImmutableList.builder(); + for (Context context : Context.REGISTRY) { + for (InstanceType instanceType : InstanceType.REGISTRY) { + builder.add(new PipelineProgramKey(instanceType, context)); + } + } + return builder.build(); + } + + private static UberShaderComponent createVertexMaterialComponent(SourceLoader loadChecker) { + return UberShaderComponent.builder(Flywheel.rl("uber_material_vertex")) + .materialSources(ShaderIndices.materialVertex() + .all()) + .adapt(FnSignature.ofVoid("flw_materialVertex")) + .switchOn(GlslExpr.variable("_flw_uberMaterialVertexIndex")) + .build(loadChecker); + } + private static UberShaderComponent createFragmentMaterialComponent(SourceLoader loadChecker) { - return UberShaderComponent.builder(Flywheel.rl("uber_fragment_material")) + return UberShaderComponent.builder(Flywheel.rl("uber_material_fragment")) .materialSources(ShaderIndices.materialFragment() .all()) .adapt(FnSignature.ofVoid("flw_materialFragment")) - .switchOn(GlslExpr.variable("_flw_materialFragmentID")) + .switchOn(GlslExpr.variable("_flw_uberMaterialFragmentIndex")) .build(loadChecker); } @@ -61,7 +79,7 @@ public class FlwPrograms { .name("flw_fogFilter") .arg("vec4", "color") .build(), GlslExpr.variable("color")) - .switchOn(GlslExpr.variable("_flw_fogID")) + .switchOn(GlslExpr.variable("_flw_uberFogIndex")) .build(loadChecker); } @@ -74,16 +92,7 @@ public class FlwPrograms { .name("flw_discardPredicate") .arg("vec4", "color") .build(), GlslExpr.boolLiteral(false)) - .switchOn(GlslExpr.variable("_flw_cutoutID")) - .build(loadChecker); - } - - private static UberShaderComponent createVertexMaterialComponent(SourceLoader loadChecker) { - return UberShaderComponent.builder(Flywheel.rl("vertex_material_adapter")) - .materialSources(ShaderIndices.materialVertex() - .all()) - .adapt(FnSignature.ofVoid("flw_materialVertex")) - .switchOn(GlslExpr.variable("_flw_materialVertexID")) + .switchOn(GlslExpr.variable("_flw_uberCutoutIndex")) .build(loadChecker); } @@ -96,16 +105,6 @@ public class FlwPrograms { .build(loadChecker); } - private static ImmutableList createPipelineKeys() { - ImmutableList.Builder builder = ImmutableList.builder(); - for (Context context : Context.REGISTRY) { - for (InstanceType instanceType : InstanceType.REGISTRY) { - builder.add(new PipelineProgramKey(instanceType, context)); - } - } - return builder.build(); - } - public static class ResourceReloadListener implements ResourceManagerReloadListener { public static final ResourceReloadListener INSTANCE = new ResourceReloadListener(); 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 8fd97faa9..aed5ba5c8 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java @@ -14,7 +14,7 @@ import com.jozufozu.flywheel.backend.compile.component.UniformComponent; import com.jozufozu.flywheel.gl.GlCompat; import com.jozufozu.flywheel.gl.shader.GlProgram; import com.jozufozu.flywheel.gl.shader.ShaderType; -import com.jozufozu.flywheel.glsl.GLSLVersion; +import com.jozufozu.flywheel.glsl.GlslVersion; import com.jozufozu.flywheel.glsl.ShaderSources; import com.jozufozu.flywheel.glsl.SourceComponent; import com.jozufozu.flywheel.lib.util.Unit; @@ -39,21 +39,22 @@ public class IndirectPrograms { _delete(); var pipelineCompiler = PipelineCompiler.create(sources, Pipelines.INDIRECT, pipelineKeys, uniformComponent, vertexComponents, fragmentComponents); var cullingCompiler = createCullingCompiler(uniformComponent, sources); - var stage2Compiler = createStage2Compiler(sources); + var applyCompiler = createApplyCompiler(sources); try { var pipelineResult = pipelineCompiler.compileAndReportErrors(); var cullingResult = cullingCompiler.compileAndReportErrors(); - var stage2Result = stage2Compiler.compileAndReportErrors(); + var applyResult = applyCompiler.compileAndReportErrors(); - if (pipelineResult != null && cullingResult != null && stage2Result != null) { - instance = new IndirectPrograms(pipelineResult, cullingResult, stage2Result.get(Unit.INSTANCE)); + if (pipelineResult != null && cullingResult != null && applyResult != null) { + instance = new IndirectPrograms(pipelineResult, cullingResult, applyResult.get(Unit.INSTANCE)); } } catch (Throwable e) { Flywheel.LOGGER.error("Failed to compile indirect programs", e); } pipelineCompiler.delete(); cullingCompiler.delete(); + applyCompiler.delete(); } private static ImmutableList> createCullingKeys() { @@ -84,22 +85,22 @@ public class IndirectPrograms { return CULL.harness(sources) .keys(createCullingKeys()) .compiler(CULL.program() - .link(CULL.shader(GLSLVersion.V460, ShaderType.COMPUTE) - .define("FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) + .link(CULL.shader(GlslVersion.V460, ShaderType.COMPUTE) + .define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) .withComponent(uniformComponent) .withComponent(IndirectComponent::create) .withResource(Files.INDIRECT_CULL) .withResource(InstanceType::instanceShader)) - .then((key, program) -> program.setUniformBlockBinding("FLWUniforms", 0))) + .then((key, program) -> program.setUniformBlockBinding("FlwUniforms", 0))) .build(); } - private static CompilationHarness createStage2Compiler(ShaderSources sources) { + private static CompilationHarness createApplyCompiler(ShaderSources sources) { return APPLY.harness(sources) .keys(ImmutableList.of(Unit.INSTANCE)) .compiler(APPLY.program() - .link(APPLY.shader(GLSLVersion.V460, ShaderType.COMPUTE) - .define("FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) + .link(APPLY.shader(GlslVersion.V460, ShaderType.COMPUTE) + .define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) .withResource(Files.INDIRECT_APPLY))) .build(); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/Pipeline.java b/src/main/java/com/jozufozu/flywheel/backend/compile/Pipeline.java index 9f9ba6b93..be3b09e2d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/Pipeline.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/Pipeline.java @@ -3,13 +3,13 @@ package com.jozufozu.flywheel.backend.compile; import java.util.Objects; import com.jozufozu.flywheel.api.instance.InstanceType; -import com.jozufozu.flywheel.glsl.GLSLVersion; +import com.jozufozu.flywheel.glsl.GlslVersion; import com.jozufozu.flywheel.glsl.SourceComponent; import net.minecraft.resources.ResourceLocation; -public record Pipeline(GLSLVersion glslVersion, ResourceLocation vertexShader, ResourceLocation fragmentShader, - ResourceLocation vertexAPI, ResourceLocation fragmentAPI, InstanceAssembler assembler) { +public record Pipeline(GlslVersion glslVersion, ResourceLocation vertexMain, ResourceLocation fragmentMain, + ResourceLocation vertexApiImpl, ResourceLocation fragmentApiImpl, InstanceAssembler assembler) { @FunctionalInterface public interface InstanceAssembler { /** @@ -28,35 +28,35 @@ public record Pipeline(GLSLVersion glslVersion, ResourceLocation vertexShader, R } public static class Builder { - private GLSLVersion glslVersion; - private ResourceLocation vertex; - private ResourceLocation fragment; - private ResourceLocation vertexAPI; - private ResourceLocation fragmentAPI; + private GlslVersion glslVersion; + private ResourceLocation vertexMain; + private ResourceLocation fragmentMain; + private ResourceLocation vertexApiImpl; + private ResourceLocation fragmentApiImpl; private InstanceAssembler assembler; - public Builder glslVersion(GLSLVersion glslVersion) { + public Builder glslVersion(GlslVersion glslVersion) { this.glslVersion = glslVersion; return this; } - public Builder vertex(ResourceLocation vertex) { - this.vertex = vertex; + public Builder vertexMain(ResourceLocation shader) { + this.vertexMain = shader; return this; } - public Builder fragment(ResourceLocation fragment) { - this.fragment = fragment; + public Builder fragmentMain(ResourceLocation shader) { + this.fragmentMain = shader; return this; } - public Builder vertexAPI(ResourceLocation vertex) { - this.vertexAPI = vertex; + public Builder vertexApiImpl(ResourceLocation shader) { + this.vertexApiImpl = shader; return this; } - public Builder fragmentAPI(ResourceLocation fragment) { - this.fragmentAPI = fragment; + public Builder fragmentApiImpl(ResourceLocation shader) { + this.fragmentApiImpl = shader; return this; } @@ -67,12 +67,12 @@ public record Pipeline(GLSLVersion glslVersion, ResourceLocation vertexShader, R public Pipeline build() { Objects.requireNonNull(glslVersion); - Objects.requireNonNull(vertex); - Objects.requireNonNull(fragment); - Objects.requireNonNull(vertexAPI); - Objects.requireNonNull(fragmentAPI); + Objects.requireNonNull(vertexMain); + Objects.requireNonNull(fragmentMain); + Objects.requireNonNull(vertexApiImpl); + Objects.requireNonNull(fragmentApiImpl); Objects.requireNonNull(assembler); - return new Pipeline(glslVersion, vertex, fragment, vertexAPI, fragmentAPI, assembler); + return new Pipeline(glslVersion, vertexMain, fragmentMain, vertexApiImpl, fragmentApiImpl, assembler); } } } 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 0f710ea7d..8443955e6 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineCompiler.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineCompiler.java @@ -20,25 +20,25 @@ public class PipelineCompiler { .withComponent(uniformComponent) .withComponent(key -> pipeline.assembler() .assemble(new Pipeline.InstanceAssemblerContext(BlockVertex.FORMAT.getAttributeCount(), key.instanceType()))) - .withResource(pipeline.vertexAPI()) + .withResource(pipeline.vertexApiImpl()) .withComponents(vertexComponents) .withResource(key -> key.instanceType() .instanceShader()) .withResource(key -> key.contextShader() .vertexShader()) - .withResource(pipeline.vertexShader())) + .withResource(pipeline.vertexMain())) .link(PIPELINE.shader(pipeline.glslVersion(), ShaderType.FRAGMENT) .enableExtension("GL_ARB_conservative_depth") .withComponent(uniformComponent) - .withResource(pipeline.fragmentAPI()) + .withResource(pipeline.fragmentApiImpl()) .withComponents(fragmentComponents) .withResource(key -> key.contextShader() .fragmentShader()) - .withResource(pipeline.fragmentShader())) + .withResource(pipeline.fragmentMain())) .then((key, program) -> { key.contextShader() .onProgramLink(program); - program.setUniformBlockBinding("FLWUniforms", 0); + program.setUniformBlockBinding("FlwUniforms", 0); })) .build(); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/Pipelines.java b/src/main/java/com/jozufozu/flywheel/backend/compile/Pipelines.java index a9aa7c8e7..373215036 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/Pipelines.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/Pipelines.java @@ -3,23 +3,23 @@ package com.jozufozu.flywheel.backend.compile; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.backend.compile.component.IndirectComponent; import com.jozufozu.flywheel.backend.compile.component.InstancedArraysComponent; -import com.jozufozu.flywheel.glsl.GLSLVersion; +import com.jozufozu.flywheel.glsl.GlslVersion; public final class Pipelines { public static final Pipeline INSTANCED_ARRAYS = Pipeline.builder() - .glslVersion(GLSLVersion.V330) - .vertex(Flywheel.rl("internal/instancing/draw.vert")) - .fragment(Flywheel.rl("internal/instancing/draw.frag")) - .vertexAPI(Flywheel.rl("internal/instancing/api/vertex.glsl")) - .fragmentAPI(Flywheel.rl("internal/instancing/api/fragment.glsl")) + .glslVersion(GlslVersion.V330) + .vertexMain(Flywheel.rl("internal/instancing/main.vert")) + .fragmentMain(Flywheel.rl("internal/instancing/main.frag")) + .vertexApiImpl(Flywheel.rl("internal/instancing/api_impl.vert")) + .fragmentApiImpl(Flywheel.rl("internal/instancing/api_impl.frag")) .assembler(InstancedArraysComponent::new) .build(); public static final Pipeline INDIRECT = Pipeline.builder() - .glslVersion(GLSLVersion.V460) - .vertex(Flywheel.rl("internal/indirect/draw.vert")) - .fragment(Flywheel.rl("internal/indirect/draw.frag")) - .vertexAPI(Flywheel.rl("internal/indirect/api/vertex.glsl")) - .fragmentAPI(Flywheel.rl("internal/indirect/api/fragment.glsl")) + .glslVersion(GlslVersion.V460) + .vertexMain(Flywheel.rl("internal/indirect/main.vert")) + .fragmentMain(Flywheel.rl("internal/indirect/main.frag")) + .vertexApiImpl(Flywheel.rl("internal/indirect/api_impl.vert")) + .fragmentApiImpl(Flywheel.rl("internal/indirect/api_impl.frag")) .assembler(IndirectComponent::create) .build(); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/component/UniformComponent.java b/src/main/java/com/jozufozu/flywheel/backend/compile/component/UniformComponent.java index 1552bddf3..899389af0 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/component/UniformComponent.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/component/UniformComponent.java @@ -36,8 +36,8 @@ public class UniformComponent implements SourceComponent { builder.uniformBlock() .layout("std140") - .name("FLWUniforms") - .member("flywheel_uniforms", "flywheel"); + .name("FlwUniforms") + .member("FlywheelUniforms", "flywheel"); builder.blankLine(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/core/Compilation.java b/src/main/java/com/jozufozu/flywheel/backend/compile/core/Compilation.java index 99c403004..25406f187 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/core/Compilation.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/core/Compilation.java @@ -12,7 +12,7 @@ import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.gl.GlCompat; import com.jozufozu.flywheel.gl.shader.GlShader; import com.jozufozu.flywheel.gl.shader.ShaderType; -import com.jozufozu.flywheel.glsl.GLSLVersion; +import com.jozufozu.flywheel.glsl.GlslVersion; import com.jozufozu.flywheel.glsl.SourceComponent; import com.jozufozu.flywheel.glsl.SourceFile; import com.jozufozu.flywheel.lib.util.StringUtil; @@ -31,11 +31,11 @@ public class Compilation { private final List files = new ArrayList<>(); private final StringBuilder generatedSource; private final StringBuilder fullSource; - private final GLSLVersion glslVersion; + private final GlslVersion glslVersion; private final ShaderType shaderType; private int generatedLines = 0; - public Compilation(GLSLVersion glslVersion, ShaderType shaderType) { + public Compilation(GlslVersion glslVersion, ShaderType shaderType) { this.glslVersion = glslVersion; this.shaderType = shaderType; @@ -88,12 +88,12 @@ public class Compilation { private void appendHeader(SourceComponent component, String source) { if (component instanceof SourceFile file) { - int fileID = files.size() + 1; + int fileId = files.size() + 1; files.add(file); fullSource.append("\n#line 0 ") - .append(fileID) + .append(fileId) .append(" // ") .append(file.name) .append('\n'); diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/core/ShaderCompiler.java b/src/main/java/com/jozufozu/flywheel/backend/compile/core/ShaderCompiler.java index 8ee9551f3..62407a1ac 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/core/ShaderCompiler.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/core/ShaderCompiler.java @@ -12,7 +12,7 @@ import org.jetbrains.annotations.Nullable; import com.jozufozu.flywheel.gl.shader.GlShader; import com.jozufozu.flywheel.gl.shader.ShaderType; -import com.jozufozu.flywheel.glsl.GLSLVersion; +import com.jozufozu.flywheel.glsl.GlslVersion; import com.jozufozu.flywheel.glsl.SourceComponent; public class ShaderCompiler { @@ -24,7 +24,7 @@ public class ShaderCompiler { } @Nullable - public GlShader compile(GLSLVersion glslVersion, ShaderType shaderType, Consumer callback, List sourceComponents) { + public GlShader compile(GlslVersion glslVersion, ShaderType shaderType, Consumer callback, List sourceComponents) { var key = new ShaderKey(glslVersion, shaderType, sourceComponents); var cached = shaderCache.get(key); if (cached != null) { @@ -67,6 +67,6 @@ public class ShaderCompiler { included.addAll(component.included()); } - private record ShaderKey(GLSLVersion glslVersion, ShaderType shaderType, List sourceComponents) { + private record ShaderKey(GlslVersion glslVersion, ShaderType shaderType, List sourceComponents) { } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/MaterialEncoder.java b/src/main/java/com/jozufozu/flywheel/backend/engine/MaterialEncoder.java similarity index 97% rename from src/main/java/com/jozufozu/flywheel/backend/MaterialEncoder.java rename to src/main/java/com/jozufozu/flywheel/backend/engine/MaterialEncoder.java index 638bf06da..9b6dce85d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/MaterialEncoder.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/MaterialEncoder.java @@ -1,9 +1,10 @@ -package com.jozufozu.flywheel.backend; +package com.jozufozu.flywheel.backend.engine; import com.jozufozu.flywheel.api.material.DepthTest; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.material.Transparency; import com.jozufozu.flywheel.api.material.WriteMask; +import com.jozufozu.flywheel.backend.ShaderIndices; import net.minecraft.util.Mth; diff --git a/src/main/java/com/jozufozu/flywheel/backend/MaterialRenderState.java b/src/main/java/com/jozufozu/flywheel/backend/engine/MaterialRenderState.java similarity index 96% rename from src/main/java/com/jozufozu/flywheel/backend/MaterialRenderState.java rename to src/main/java/com/jozufozu/flywheel/backend/engine/MaterialRenderState.java index 76a478a8d..2b66c1b5d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/MaterialRenderState.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/MaterialRenderState.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.backend; +package com.jozufozu.flywheel.backend.engine; import java.util.Comparator; @@ -16,7 +16,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.texture.AbstractTexture; public final class MaterialRenderState { - public static final Comparator COMPARATOR = Comparator.comparing(Material::baseTexture) + public static final Comparator COMPARATOR = Comparator.comparing(Material::texture) .thenComparing(Material::blur) .thenComparing(Material::mipmap) .thenComparing(Material::backfaceCulling) @@ -41,7 +41,7 @@ public final class MaterialRenderState { GlTextureUnit.T0.makeActive(); AbstractTexture texture = Minecraft.getInstance() .getTextureManager() - .getTexture(material.baseTexture()); + .getTexture(material.texture()); texture.setFilter(material.blur(), material.mipmap()); var textureId = texture.getId(); RenderSystem.setShaderTexture(0, textureId); @@ -62,7 +62,7 @@ public final class MaterialRenderState { RenderSystem.enablePolygonOffset(); } else { RenderSystem.polygonOffset(0.0F, 0.0F); - RenderSystem.enablePolygonOffset(); + RenderSystem.disablePolygonOffset(); } } @@ -115,7 +115,7 @@ public final class MaterialRenderState { RenderSystem.enableBlend(); RenderSystem.blendFunc(GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ONE); } - case LIGHTING -> { + case LIGHTNING -> { RenderSystem.enableBlend(); RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/UniformBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/engine/UniformBuffer.java index 3efd685d0..90910758a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/UniformBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/UniformBuffer.java @@ -9,39 +9,30 @@ import com.google.common.collect.ImmutableList; import com.jozufozu.flywheel.api.event.ReloadLevelRendererEvent; import com.jozufozu.flywheel.api.uniform.ShaderUniforms; import com.jozufozu.flywheel.gl.buffer.GlBuffer; -import com.jozufozu.flywheel.gl.shader.GlProgram; import com.jozufozu.flywheel.lib.math.MoreMath; import com.jozufozu.flywheel.lib.memory.MemoryBlock; -import net.minecraft.util.Mth; - public class UniformBuffer { - - private static final int OFFSET_ALIGNMENT = GL32.glGetInteger(GL32.GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT); - private static final int MAX_SIZE = GL32.glGetInteger(GL32.GL_MAX_UNIFORM_BLOCK_SIZE); - private static final int MAX_BINDINGS = GL32.glGetInteger(GL32.GL_MAX_UNIFORM_BUFFER_BINDINGS); - private static final boolean PO2_ALIGNMENT = Mth.isPowerOfTwo(OFFSET_ALIGNMENT); +// private static final int OFFSET_ALIGNMENT = GL32.glGetInteger(GL32.GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT); +// private static final int MAX_SIZE = GL32.glGetInteger(GL32.GL_MAX_UNIFORM_BLOCK_SIZE); +// private static final int MAX_BINDINGS = GL32.glGetInteger(GL32.GL_MAX_UNIFORM_BUFFER_BINDINGS); +// private static final boolean PO2_ALIGNMENT = Mth.isPowerOfTwo(OFFSET_ALIGNMENT); private static UniformBuffer instance; - private final ProviderSet providerSet; - - public static UniformBuffer getInstance() { - if (instance == null) { - instance = new UniformBuffer(); - } - return instance; - } private final GlBuffer buffer; + private final ProviderSet providerSet; private UniformBuffer() { buffer = new GlBuffer(); providerSet = new ProviderSet(ShaderUniforms.REGISTRY.getAll()); } - public static void syncAndBind(GlProgram program) { - getInstance().sync(); - program.bind(); + public static UniformBuffer get() { + if (instance == null) { + instance = new UniformBuffer(); + } + return instance; } public void sync() { @@ -52,15 +43,6 @@ public class UniformBuffer { GL32.glBindBufferRange(GL32.GL_UNIFORM_BUFFER, 0, buffer.handle(), 0, providerSet.data.size()); } - // https://stackoverflow.com/questions/3407012/rounding-up-to-the-nearest-multiple-of-a-number - private static int alignUniformBuffer(int numToRound) { - if (PO2_ALIGNMENT) { - return (numToRound + OFFSET_ALIGNMENT - 1) & -OFFSET_ALIGNMENT; - } else { - return ((numToRound + OFFSET_ALIGNMENT - 1) / OFFSET_ALIGNMENT) * OFFSET_ALIGNMENT; - } - } - private void delete() { providerSet.delete(); buffer.delete(); @@ -73,6 +55,15 @@ public class UniformBuffer { } } +// // https://stackoverflow.com/questions/3407012/rounding-up-to-the-nearest-multiple-of-a-number +// private static int alignUniformBuffer(int numToRound) { +// if (PO2_ALIGNMENT) { +// return (numToRound + OFFSET_ALIGNMENT - 1) & -OFFSET_ALIGNMENT; +// } else { +// return ((numToRound + OFFSET_ALIGNMENT - 1) / OFFSET_ALIGNMENT) * OFFSET_ALIGNMENT; +// } +// } + private static class LiveProvider { private final ShaderUniforms shaderUniforms; private final int offset; diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchedDrawManager.java b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchedDrawManager.java index 9c645e3a2..56293b43c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchedDrawManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchedDrawManager.java @@ -48,7 +48,7 @@ class BatchedDrawManager extends InstancerStorage> { @Override protected void add(InstancerKey key, BatchedInstancer instancer, Model model, RenderStage stage) { var stagePlan = stagePlans.computeIfAbsent(stage, renderStage -> new BatchedStagePlan(renderStage, drawTracker)); - var meshes = model.getMeshes(); + var meshes = model.meshes(); for (var entry : meshes.entrySet()) { var material = entry.getKey(); RenderType renderType = material.getFallbackRenderType(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectBuffers.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectBuffers.java index 90ef672ad..042cd2744 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectBuffers.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectBuffers.java @@ -30,12 +30,12 @@ public class IndirectBuffers { public static final long INT_SIZE = Integer.BYTES; public static final long PTR_SIZE = Pointer.POINTER_SIZE; + public static final long MODEL_STRIDE = 24; + // Byte size of a draw command, plus our added mesh data. public static final long DRAW_COMMAND_STRIDE = 40; public static final long DRAW_COMMAND_OFFSET = 0; - public static final long MODEL_STRIDE = 24; - // BITS private static final int SUB_DATA_BITS = GL_DYNAMIC_STORAGE_BIT; private static final int PERSISTENT_BITS = GL_MAP_PERSISTENT_BIT | GL_MAP_WRITE_BIT; @@ -43,11 +43,10 @@ public class IndirectBuffers { private static final int GPU_ONLY_BITS = 0; // Offsets to the vbos - private static final long VBO_OFFSET = 0; - private static final long OBJECT_OFFSET = VBO_OFFSET; - private static final long TARGET_OFFSET = INT_SIZE; - private static final long MODEL_OFFSET = INT_SIZE * 2; - private static final long DRAW_OFFSET = INT_SIZE * 3; + private static final long OBJECT_OFFSET = 0; + private static final long TARGET_OFFSET = OBJECT_OFFSET + INT_SIZE; + private static final long MODEL_OFFSET = TARGET_OFFSET + INT_SIZE; + private static final long DRAW_OFFSET = MODEL_OFFSET + INT_SIZE; // Offsets to the 3 segments private static final long OFFSET_OFFSET = BUFFER_COUNT * INT_SIZE; @@ -59,6 +58,9 @@ public class IndirectBuffers { // Total size of the buffer. private static final long BUFFERS_SIZE_BYTES = SIZE_OFFSET + BUFFER_COUNT * PTR_SIZE; + private static final float OBJECT_GROWTH_FACTOR = 1.25f; + private static final float MODEL_GROWTH_FACTOR = 2f; + private static final float DRAW_GROWTH_FACTOR = 2f; /** * A small block of memory divided into 3 contiguous segments: @@ -74,6 +76,7 @@ public class IndirectBuffers { */ private final MemoryBlock buffers; private final long objectStride; + private int object; private int target; private int model; @@ -87,10 +90,6 @@ public class IndirectBuffers { private int maxModelCount = 0; private int maxDrawCount = 0; - private static final float OBJECT_GROWTH_FACTOR = 1.25f; - private static final float MODEL_GROWTH_FACTOR = 2f; - private static final float DRAW_GROWTH_FACTOR = 2f; - IndirectBuffers(long objectStride) { this.objectStride = objectStride; this.buffers = MemoryBlock.calloc(BUFFERS_SIZE_BYTES, 1); @@ -105,7 +104,7 @@ public class IndirectBuffers { draw = MemoryUtil.memGetInt(ptr + DRAW_OFFSET); } - void updateCounts(int objectCount, int drawCount, int modelCount) { + void updateCounts(int objectCount, int modelCount, int drawCount) { if (objectCount > maxObjectCount) { createObjectStorage((int) (objectCount * OBJECT_GROWTH_FACTOR)); } @@ -123,7 +122,7 @@ public class IndirectBuffers { MemoryUtil.memPutAddress(ptr + DRAW_SIZE_OFFSET, DRAW_COMMAND_STRIDE * drawCount); } - void createObjectStorage(int objectCount) { + private void createObjectStorage(int objectCount) { freeObjectStorage(); var objectSize = objectStride * objectCount; var targetSize = INT_SIZE * objectCount; @@ -157,7 +156,7 @@ public class IndirectBuffers { FlwMemoryTracker._allocGPUMemory(maxObjectCount * objectStride); } - void createModelStorage(int modelCount) { + private void createModelStorage(int modelCount) { freeModelStorage(); var modelSize = MODEL_STRIDE * modelCount; @@ -179,7 +178,7 @@ public class IndirectBuffers { FlwMemoryTracker._allocGPUMemory(maxModelCount * MODEL_STRIDE); } - void createDrawStorage(int drawCount) { + private void createDrawStorage(int drawCount) { freeDrawStorage(); var drawSize = DRAW_COMMAND_STRIDE * drawCount; diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectCullingGroup.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectCullingGroup.java index ed0afdddd..9d5593a15 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectCullingGroup.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectCullingGroup.java @@ -21,31 +21,39 @@ import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.model.Mesh; import com.jozufozu.flywheel.api.model.Model; -import com.jozufozu.flywheel.backend.MaterialRenderState; import com.jozufozu.flywheel.backend.compile.IndirectPrograms; +import com.jozufozu.flywheel.backend.engine.MaterialRenderState; import com.jozufozu.flywheel.backend.engine.UniformBuffer; import com.jozufozu.flywheel.gl.GlCompat; import com.jozufozu.flywheel.gl.shader.GlProgram; import com.jozufozu.flywheel.lib.context.Contexts; -import com.jozufozu.flywheel.lib.model.ModelUtil; public class IndirectCullingGroup { + private static final Comparator DRAW_COMPARATOR = Comparator.comparing(IndirectDraw::stage) + .thenComparing(IndirectDraw::material, MaterialRenderState.COMPARATOR); + private static final int DRAW_BARRIER_BITS = GL_SHADER_STORAGE_BARRIER_BIT | GL_COMMAND_BARRIER_BIT; - private final GlProgram cull; - private final GlProgram draw; + private final GlProgram cullProgram; + private final GlProgram applyProgram; + private final GlProgram drawProgram; + private final long objectStride; private final IndirectBuffers buffers; - public final IndirectMeshPool meshPool; + private final IndirectMeshPool meshPool; private final List indirectModels = new ArrayList<>(); private final List indirectDraws = new ArrayList<>(); private final Map> multiDraws = new EnumMap<>(RenderStage.class); private boolean needsDrawBarrier; private boolean needsSortDraws; private int instanceCountThisFrame; - private final GlProgram apply; IndirectCullingGroup(InstanceType instanceType) { + var programs = IndirectPrograms.get(); + cullProgram = programs.getCullingProgram(instanceType); + applyProgram = programs.getApplyProgram(); + drawProgram = programs.getIndirectProgram(instanceType, Contexts.DEFAULT); + objectStride = instanceType.getLayout() .getStride() + IndirectBuffers.INT_SIZE; @@ -53,35 +61,75 @@ public class IndirectCullingGroup { buffers.createBuffers(); meshPool = new IndirectMeshPool(); - - var indirectPrograms = IndirectPrograms.get(); - cull = indirectPrograms.getCullingProgram(instanceType); - apply = indirectPrograms.getApplyProgram(); - draw = indirectPrograms.getIndirectProgram(instanceType, Contexts.WORLD); } - public void add(IndirectInstancer instancer, RenderStage stage, Model model) { - var meshes = model.getMeshes(); + public void flush() { + needsDrawBarrier = true; + instanceCountThisFrame = prepareModels(); - var boundingSphere = ModelUtil.computeBoundingSphere(meshes.values()); - - int modelID = indirectModels.size(); - var indirectModel = new IndirectModel(instancer, modelID, boundingSphere); - indirectModels.add(indirectModel); - - for (Map.Entry materialMeshEntry : meshes.entrySet()) { - IndirectMeshPool.BufferedMesh bufferedMesh = meshPool.alloc(materialMeshEntry.getValue()); - indirectDraws.add(new IndirectDraw(indirectModel, materialMeshEntry.getKey(), bufferedMesh, stage)); + if (nothingToDo()) { + return; } - needsSortDraws = true; + buffers.updateCounts(instanceCountThisFrame, indirectModels.size(), indirectDraws.size()); + + if (needsSortDraws) { + sortDraws(); + needsSortDraws = false; + } + + meshPool.flush(); + uploadObjects(); + uploadModels(); + uploadDraws(); + } + + public void dispatchCull() { + if (nothingToDo()) { + return; + } + + UniformBuffer.get().sync(); + cullProgram.bind(); + buffers.bindForCompute(); + glDispatchCompute(GlCompat.getComputeGroupCount(instanceCountThisFrame), 1, 1); + } + + public void dispatchApply() { + if (nothingToDo()) { + return; + } + + applyProgram.bind(); + buffers.bindForCompute(); + glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); + glDispatchCompute(GlCompat.getComputeGroupCount(indirectDraws.size()), 1, 1); + } + + private boolean nothingToDo() { + return indirectDraws.isEmpty() || instanceCountThisFrame == 0; + } + + private boolean nothingToDo(RenderStage stage) { + return nothingToDo() || !multiDraws.containsKey(stage); + } + + /** + * @return the total instance count + */ + private int prepareModels() { + int baseInstance = 0; + for (var model : indirectModels) { + model.prepare(baseInstance); + baseInstance += model.instancer.getInstanceCount(); + } + return baseInstance; } private void sortDraws() { multiDraws.clear(); // sort by stage, then material - indirectDraws.sort(Comparator.comparing(IndirectDraw::stage) - .thenComparing(IndirectDraw::material, MaterialRenderState.COMPARATOR)); + indirectDraws.sort(DRAW_COMPARATOR); for (int start = 0, i = 0; i < indirectDraws.size(); i++) { var draw1 = indirectDraws.get(i); @@ -99,52 +147,53 @@ public class IndirectCullingGroup { } } - public void flush() { - needsDrawBarrier = true; - instanceCountThisFrame = calculateTotalInstanceCountAndPrepareBatches(); + private void uploadObjects() { + long objectPtr = buffers.objectPtr; - if (nothingToDo()) { - return; + for (IndirectModel model : indirectModels) { + var instanceCount = model.instancer.getInstanceCount(); + model.writeObjects(objectPtr); + + objectPtr += instanceCount * objectStride; } - buffers.updateCounts(instanceCountThisFrame, indirectDraws.size(), indirectModels.size()); + buffers.flushObjects(objectPtr - buffers.objectPtr); + } - if (needsSortDraws) { - sortDraws(); - needsSortDraws = false; + private void uploadModels() { + long writePtr = buffers.modelPtr.ptr(); + for (var model : indirectModels) { + model.write(writePtr); + writePtr += IndirectBuffers.MODEL_STRIDE; + } + buffers.flushModels(writePtr - buffers.modelPtr.ptr()); + } + + private void uploadDraws() { + long writePtr = buffers.drawPtr.ptr(); + for (var draw : indirectDraws) { + draw.write(writePtr); + writePtr += IndirectBuffers.DRAW_COMMAND_STRIDE; + } + buffers.flushDrawCommands(writePtr - buffers.drawPtr.ptr()); + } + + public boolean hasStage(RenderStage stage) { + return multiDraws.containsKey(stage); + } + + public void add(IndirectInstancer instancer, Model model, RenderStage stage) { + int modelIndex = indirectModels.size(); + var boundingSphere = model.boundingSphere(); + var indirectModel = new IndirectModel(instancer, modelIndex, boundingSphere); + indirectModels.add(indirectModel); + + for (Map.Entry entry : model.meshes().entrySet()) { + IndirectMeshPool.BufferedMesh bufferedMesh = meshPool.alloc(entry.getValue()); + indirectDraws.add(new IndirectDraw(indirectModel, entry.getKey(), bufferedMesh, stage)); } - meshPool.flush(); - uploadInstances(); - uploadModels(); - uploadIndirectCommands(); - } - - public void dispatchCull() { - if (nothingToDo()) { - return; - } - UniformBuffer.syncAndBind(cull); - buffers.bindForCompute(); - glDispatchCompute(getGroupCount(instanceCountThisFrame), 1, 1); - } - - public void dispatchApply() { - if (nothingToDo()) { - return; - } - apply.bind(); - buffers.bindForCompute(); - glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); - glDispatchCompute(getGroupCount(indirectDraws.size()), 1, 1); - } - - private boolean nothingToDo() { - return indirectDraws.isEmpty() || instanceCountThisFrame == 0; - } - - private boolean nothingToDo(RenderStage stage) { - return nothingToDo() || !multiDraws.containsKey(stage); + needsSortDraws = true; } public void submit(RenderStage stage) { @@ -152,13 +201,14 @@ public class IndirectCullingGroup { return; } - UniformBuffer.syncAndBind(draw); + UniformBuffer.get().sync(); + drawProgram.bind(); meshPool.bindForDraw(); buffers.bindForDraw(); drawBarrier(); - var flwBaseDraw = draw.getUniformLocation("_flw_baseDraw"); + var flwBaseDraw = drawProgram.getUniformLocation("_flw_baseDraw"); for (var multiDraw : multiDraws.get(stage)) { glUniform1ui(flwBaseDraw, multiDraw.start); @@ -173,63 +223,11 @@ public class IndirectCullingGroup { } } - private void uploadInstances() { - long objectPtr = buffers.objectPtr; - - for (IndirectModel batch : indirectModels) { - var instanceCount = batch.instancer.getInstanceCount(); - batch.writeObjects(objectPtr); - - objectPtr += instanceCount * objectStride; - } - - buffers.flushObjects(objectPtr - buffers.objectPtr); - } - - private void uploadModels() { - long writePtr = buffers.modelPtr.ptr(); - for (var batch : indirectModels) { - batch.writeModel(writePtr); - writePtr += IndirectBuffers.MODEL_STRIDE; - } - buffers.flushModels(writePtr - buffers.modelPtr.ptr()); - } - - private void uploadIndirectCommands() { - long writePtr = buffers.drawPtr.ptr(); - for (var batch : indirectDraws) { - batch.writeIndirectCommand(writePtr); - writePtr += IndirectBuffers.DRAW_COMMAND_STRIDE; - } - buffers.flushDrawCommands(writePtr - buffers.drawPtr.ptr()); - } - - private int calculateTotalInstanceCountAndPrepareBatches() { - int baseInstance = 0; - for (var batch : indirectModels) { - batch.prepare(baseInstance); - baseInstance += batch.instancer.getInstanceCount(); - } - return baseInstance; - } - public void delete() { buffers.delete(); meshPool.delete(); } - public boolean hasStage(RenderStage stage) { - return multiDraws.containsKey(stage); - } - - private static int getGroupCount(int threadCount) { - if (GlCompat.amd) { - return (threadCount + 63) >> 6; // ceil(threadCount / 64) - } else { - return (threadCount + 31) >> 5; // ceil(threadCount / 32) - } - } - private record MultiDraw(Material material, int start, int end) { void submit() { MaterialRenderState.setup(material); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDraw.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDraw.java index b2091d614..ee0cfb206 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDraw.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDraw.java @@ -4,17 +4,17 @@ import org.lwjgl.system.MemoryUtil; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.material.Material; -import com.jozufozu.flywheel.backend.MaterialEncoder; import com.jozufozu.flywheel.backend.ShaderIndices; +import com.jozufozu.flywheel.backend.engine.MaterialEncoder; public class IndirectDraw { private final IndirectModel model; - private final IndirectMeshPool.BufferedMesh mesh; private final Material material; + private final IndirectMeshPool.BufferedMesh mesh; private final RenderStage stage; - private final int vertexMaterialID; - private final int fragmentMaterialID; + private final int materialVertexIndex; + private final int materialFragmentIndex; private final int packedFogAndCutout; private final int packedMaterialProperties; @@ -24,8 +24,8 @@ public class IndirectDraw { this.mesh = mesh; this.stage = stage; - this.vertexMaterialID = ShaderIndices.getVertexShaderIndex(material.shaders()); - this.fragmentMaterialID = ShaderIndices.getFragmentShaderIndex(material.shaders()); + this.materialVertexIndex = ShaderIndices.getVertexShaderIndex(material.shaders()); + this.materialFragmentIndex = ShaderIndices.getFragmentShaderIndex(material.shaders()); this.packedFogAndCutout = MaterialEncoder.packFogAndCutout(material); this.packedMaterialProperties = MaterialEncoder.packProperties(material); } @@ -42,16 +42,17 @@ public class IndirectDraw { return stage; } - public void writeIndirectCommand(long ptr) { + public void write(long ptr) { MemoryUtil.memPutInt(ptr, mesh.indexCount()); // count - MemoryUtil.memPutInt(ptr + 4, 0); // instanceCount - MemoryUtil.memPutInt(ptr + 8, mesh.firstIndex); // firstIndex - MemoryUtil.memPutInt(ptr + 12, mesh.baseVertex); // baseVertex - MemoryUtil.memPutInt(ptr + 16, model.baseInstance); // baseInstance + MemoryUtil.memPutInt(ptr + 4, 0); // instanceCount - to be set by the apply shader + MemoryUtil.memPutInt(ptr + 8, mesh.firstIndex()); // firstIndex + MemoryUtil.memPutInt(ptr + 12, mesh.baseVertex()); // baseVertex + MemoryUtil.memPutInt(ptr + 16, model.baseInstance()); // baseInstance - MemoryUtil.memPutInt(ptr + 20, model.id); // modelID - MemoryUtil.memPutInt(ptr + 24, vertexMaterialID); // vertexMaterialID - MemoryUtil.memPutInt(ptr + 28, fragmentMaterialID); // fragmentMaterialID + MemoryUtil.memPutInt(ptr + 20, model.index); // modelIndex + + MemoryUtil.memPutInt(ptr + 24, materialVertexIndex); // materialVertexIndex + MemoryUtil.memPutInt(ptr + 28, materialFragmentIndex); // materialFragmentIndex MemoryUtil.memPutInt(ptr + 32, packedFogAndCutout); // packedFogAndCutout MemoryUtil.memPutInt(ptr + 36, packedMaterialProperties); // packedMaterialProperties } diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawManager.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawManager.java index fe8943076..18846ded3 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawManager.java @@ -11,29 +11,35 @@ import com.jozufozu.flywheel.backend.engine.InstancerKey; import com.jozufozu.flywheel.backend.engine.InstancerStorage; public class IndirectDrawManager extends InstancerStorage> { - public final Map, IndirectCullingGroup> renderLists = new HashMap<>(); + private final Map, IndirectCullingGroup> renderLists = new HashMap<>(); @Override protected IndirectInstancer create(InstanceType type) { return new IndirectInstancer<>(type); } + @SuppressWarnings("unchecked") @Override protected void add(InstancerKey key, IndirectInstancer instancer, Model model, RenderStage stage) { - var indirectList = (IndirectCullingGroup) renderLists.computeIfAbsent(key.type(), IndirectCullingGroup::new); - - indirectList.add((IndirectInstancer) instancer, stage, model); + var group = (IndirectCullingGroup) renderLists.computeIfAbsent(key.type(), IndirectCullingGroup::new); + group.add((IndirectInstancer) instancer, model, stage); } public boolean hasStage(RenderStage stage) { - for (var list : renderLists.values()) { - if (list.hasStage(stage)) { + for (var group : renderLists.values()) { + if (group.hasStage(stage)) { return true; } } return false; } + public void renderStage(RenderStage stage) { + for (var group : renderLists.values()) { + group.submit(stage); + } + } + @Override public void flush() { super.flush(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java index c39f52399..743ff5b45 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java @@ -6,10 +6,10 @@ import com.jozufozu.flywheel.api.event.RenderContext; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.task.Plan; import com.jozufozu.flywheel.api.task.TaskExecutor; -import com.jozufozu.flywheel.backend.MaterialRenderState; import com.jozufozu.flywheel.backend.engine.AbstractEngine; import com.jozufozu.flywheel.backend.engine.AbstractInstancer; import com.jozufozu.flywheel.backend.engine.InstancerStorage; +import com.jozufozu.flywheel.backend.engine.MaterialRenderState; import com.jozufozu.flywheel.gl.GlStateTracker; import com.jozufozu.flywheel.gl.GlTextureUnit; import com.jozufozu.flywheel.lib.task.Flag; @@ -61,9 +61,7 @@ public class IndirectEngine extends AbstractEngine { GlTextureUnit.T2.makeActive(); RenderSystem.bindTexture(RenderSystem.getShaderTexture(2)); - for (var list : drawManager.renderLists.values()) { - list.submit(stage); - } + drawManager.renderStage(stage); MaterialRenderState.reset(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectInstancer.java index eebbe8bf2..a5e135f9f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectInstancer.java @@ -22,28 +22,24 @@ public class IndirectInstancer extends AbstractInstancer removeDeletedInstances(); } - public void writeSparse(long objectPtr, int batchID) { + public void writeChanged(long objectPtr, int modelIndex) { int count = instances.size(); InstanceWriter writer = type.getWriter(); for (int i = changed.nextSetBit(0); i >= 0 && i < count; i = changed.nextSetBit(i + 1)) { long ptr = objectPtr + objectStride * i; - // write batchID - MemoryUtil.memPutInt(ptr, batchID); - // write object - writer.write(ptr + IndirectBuffers.INT_SIZE, instances.get(i)); + MemoryUtil.memPutInt(ptr, modelIndex); // modelIndex + writer.write(ptr + IndirectBuffers.INT_SIZE, instances.get(i)); // instance } changed.clear(); } - public void writeFull(long objectPtr, int modelID) { + public void writeAll(long objectPtr, int modelIndex) { InstanceWriter writer = type.getWriter(); for (I object : instances) { - // write modelID - MemoryUtil.memPutInt(objectPtr, modelID); + MemoryUtil.memPutInt(objectPtr, modelIndex); // modelIndex objectPtr += IndirectBuffers.INT_SIZE; - // write object - writer.write(objectPtr, object); + writer.write(objectPtr, object); // instance objectPtr += instanceStride; } changed.clear(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java index 80e436581..ad9a10a91 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java @@ -6,7 +6,6 @@ import java.util.List; import java.util.Map; import org.jetbrains.annotations.Nullable; -import org.joml.Vector4fc; import com.jozufozu.flywheel.api.layout.BufferLayout; import com.jozufozu.flywheel.api.model.Mesh; @@ -24,9 +23,9 @@ public class IndirectMeshPool { private final Map meshes = new HashMap<>(); private final List meshList = new ArrayList<>(); - final GlVertexArray vertexArray; - final GlBuffer vbo; - final GlBuffer ebo; + private final GlVertexArray vertexArray; + private final GlBuffer vbo; + private final GlBuffer ebo; private boolean dirty; @@ -145,9 +144,9 @@ public class IndirectMeshPool { public static class BufferedMesh { private final Mesh mesh; - public long byteIndex; - public int firstIndex; - public int baseVertex; + private long byteIndex; + private int baseVertex; + private int firstIndex; private BufferedMesh(Mesh mesh) { this.mesh = mesh; @@ -161,8 +160,12 @@ public class IndirectMeshPool { return mesh.indexCount(); } - public Vector4fc boundingSphere() { - return mesh.boundingSphere(); + public int baseVertex() { + return baseVertex; + } + + public int firstIndex() { + return firstIndex; } } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectModel.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectModel.java index 3959982e5..f46873391 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectModel.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectModel.java @@ -1,28 +1,24 @@ package com.jozufozu.flywheel.backend.engine.indirect; -import org.joml.Vector4f; import org.joml.Vector4fc; import org.lwjgl.system.MemoryUtil; public class IndirectModel { public final IndirectInstancer instancer; - public final int id; - - public int baseInstance = -1; - private boolean needsFullWrite = true; - + public final int index; private final Vector4fc boundingSphere; - public IndirectModel(IndirectInstancer instancer, int id, Vector4f boundingSphere) { + private int baseInstance = -1; + private boolean needsFullWrite = true; + + public IndirectModel(IndirectInstancer instancer, int index, Vector4fc boundingSphere) { this.instancer = instancer; - this.id = id; + this.index = index; this.boundingSphere = boundingSphere; } - public void writeModel(long ptr) { - MemoryUtil.memPutInt(ptr, 0); // instanceCount - to be incremented by the compute shader - MemoryUtil.memPutInt(ptr + 4, baseInstance); // baseInstance - boundingSphere.getToAddress(ptr + 8); // boundingSphere + public int baseInstance() { + return baseInstance; } public void prepare(int baseInstance) { @@ -37,9 +33,15 @@ public class IndirectModel { public void writeObjects(long objectPtr) { if (needsFullWrite) { - instancer.writeFull(objectPtr, id); + instancer.writeAll(objectPtr, index); } else { - instancer.writeSparse(objectPtr, id); + instancer.writeChanged(objectPtr, index); } } + + public void write(long ptr) { + MemoryUtil.memPutInt(ptr, 0); // instanceCount - to be incremented by the cull shader + MemoryUtil.memPutInt(ptr + 4, baseInstance); // baseInstance + boundingSphere.getToAddress(ptr + 8); // boundingSphere + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/EBOCache.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/EboCache.java similarity index 93% rename from src/main/java/com/jozufozu/flywheel/backend/engine/instancing/EBOCache.java rename to src/main/java/com/jozufozu/flywheel/backend/engine/instancing/EboCache.java index 1fefedb4c..f90b019fd 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/EBOCache.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/EboCache.java @@ -17,7 +17,7 @@ import com.mojang.blaze3d.platform.GlStateManager; import it.unimi.dsi.fastutil.objects.Object2ReferenceMap; import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenHashMap; -public class EBOCache { +public class EboCache { private final List quads = new ArrayList<>(); private final Object2ReferenceMap others = new Object2ReferenceOpenHashMap<>(); @@ -37,9 +37,9 @@ public class EBOCache { private int getQuads(int indexCount) { // Use an existing quad EBO if there's one big enough. - for (Entry quadEBO : quads) { - if (quadEBO.gpuSize >= indexCount * GlNumericType.UINT.byteWidth()) { - return quadEBO.ebo; + for (Entry quadEbo : quads) { + if (quadEbo.gpuSize >= indexCount * GlNumericType.UINT.byteWidth()) { + return quadEbo.ebo; } } // If not, create a new one. @@ -55,7 +55,6 @@ public class EBOCache { } private record Entry(int ebo, int gpuSize) { - @NotNull private static Entry create(IndexSequence provider, int indexCount) { int byteSize = indexCount * GlNumericType.UINT.byteWidth(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedCrumbling.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedCrumbling.java index d28ac6105..88f550ee2 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedCrumbling.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedCrumbling.java @@ -12,14 +12,15 @@ import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.material.Transparency; import com.jozufozu.flywheel.api.material.WriteMask; -import com.jozufozu.flywheel.backend.MaterialRenderState; import com.jozufozu.flywheel.backend.compile.InstancingPrograms; import com.jozufozu.flywheel.backend.engine.InstanceHandleImpl; +import com.jozufozu.flywheel.backend.engine.MaterialRenderState; import com.jozufozu.flywheel.backend.engine.UniformBuffer; import com.jozufozu.flywheel.gl.GlStateTracker; import com.jozufozu.flywheel.gl.GlTextureUnit; import com.jozufozu.flywheel.lib.context.Contexts; import com.jozufozu.flywheel.lib.material.CutoutShaders; +import com.jozufozu.flywheel.lib.material.FogShaders; import com.jozufozu.flywheel.lib.material.SimpleMaterial; import com.mojang.blaze3d.systems.RenderSystem; @@ -37,6 +38,8 @@ public class InstancedCrumbling { return; } + var crumblingMaterial = SimpleMaterial.builder(); + try (var state = GlStateTracker.getRestoreState()) { for (var shaderStateEntry : byShaderState.entrySet()) { var byProgress = shaderStateEntry.getValue(); @@ -50,8 +53,9 @@ public class InstancedCrumbling { var baseMaterial = shader.material(); int diffuseTexture = getDiffuseTexture(baseMaterial); - var crumblingMaterial = SimpleMaterial.builderOf(baseMaterial) - .cutout(CutoutShaders.OFF) + crumblingMaterial.copyFrom(baseMaterial) + .fog(FogShaders.NONE) + .cutout(CutoutShaders.ONE_TENTH) .polygonOffset(true) .transparency(Transparency.CRUMBLING) .writeMask(WriteMask.COLOR) @@ -60,8 +64,9 @@ public class InstancedCrumbling { var program = InstancingPrograms.get() .get(shader.instanceType(), Contexts.CRUMBLING); - UniformBuffer.syncAndBind(program); + program.bind(); + UniformBuffer.get().sync(); InstancingEngine.uploadMaterialUniform(program, crumblingMaterial); for (Int2ObjectMap.Entry> progressEntry : byProgress.int2ObjectEntrySet()) { @@ -71,7 +76,7 @@ public class InstancedCrumbling { continue; } - crumblingMaterial.baseTexture(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey())); + crumblingMaterial.texture(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey())); MaterialRenderState.setup(crumblingMaterial); @@ -126,7 +131,7 @@ public class InstancedCrumbling { private static int getDiffuseTexture(Material material) { return Minecraft.getInstance() .getTextureManager() - .getTexture(material.baseTexture()) + .getTexture(material.texture()) .getId(); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java index c50946dfd..4ab53ee0b 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java @@ -27,7 +27,7 @@ public class InstancedDrawManager extends InstancerStorage * A map of vertex types to their mesh pools. */ private final InstancedMeshPool meshPool = new InstancedMeshPool(); - private final EBOCache eboCache = new EBOCache(); + private final EboCache eboCache = new EboCache(); public DrawSet get(RenderStage stage) { return drawSets.getOrDefault(stage, DrawSet.EMPTY); @@ -66,7 +66,7 @@ public class InstancedDrawManager extends InstancerStorage DrawSet drawSet = drawSets.computeIfAbsent(stage, DrawSet::new); - var meshes = model.getMeshes(); + var meshes = model.meshes(); for (var entry : meshes.entrySet()) { var mesh = alloc(entry.getValue()); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java index cb47de048..72ff452dc 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java @@ -49,7 +49,7 @@ public class InstancedMeshPool { * @param eboCache The EBO cache to use. * @return A handle to the allocated mesh. */ - public BufferedMesh alloc(Mesh mesh, EBOCache eboCache) { + public BufferedMesh alloc(Mesh mesh, EboCache eboCache) { return meshes.computeIfAbsent(mesh, m -> { BufferedMesh bufferedMesh = new BufferedMesh(m, byteSize, eboCache); byteSize += bufferedMesh.size(); @@ -147,7 +147,7 @@ public class InstancedMeshPool { private final Set boundTo = new HashSet<>(); - private BufferedMesh(Mesh mesh, long byteIndex, EBOCache eboCache) { + private BufferedMesh(Mesh mesh, long byteIndex, EboCache eboCache) { this.mesh = mesh; this.byteIndex = byteIndex; this.ebo = eboCache.get(mesh.indexSequence(), mesh.indexCount()); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java index 24e9c6801..f2c99b841 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java @@ -9,13 +9,13 @@ import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.task.Plan; import com.jozufozu.flywheel.api.task.TaskExecutor; -import com.jozufozu.flywheel.backend.MaterialEncoder; -import com.jozufozu.flywheel.backend.MaterialRenderState; import com.jozufozu.flywheel.backend.ShaderIndices; import com.jozufozu.flywheel.backend.compile.InstancingPrograms; import com.jozufozu.flywheel.backend.engine.AbstractEngine; import com.jozufozu.flywheel.backend.engine.AbstractInstancer; import com.jozufozu.flywheel.backend.engine.InstancerStorage; +import com.jozufozu.flywheel.backend.engine.MaterialEncoder; +import com.jozufozu.flywheel.backend.engine.MaterialRenderState; import com.jozufozu.flywheel.backend.engine.UniformBuffer; import com.jozufozu.flywheel.gl.GlStateTracker; import com.jozufozu.flywheel.gl.GlTextureUnit; @@ -111,9 +111,10 @@ public class InstancingEngine extends AbstractEngine { } var program = InstancingPrograms.get() - .get(shader.instanceType(), Contexts.WORLD); - UniformBuffer.syncAndBind(program); + .get(shader.instanceType(), Contexts.DEFAULT); + program.bind(); + UniformBuffer.get().sync(); uploadMaterialUniform(program, shader.material()); MaterialRenderState.setup(shader.material()); diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java index bfd1079a8..7290b7f67 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java @@ -135,18 +135,18 @@ public class FlwCommands { command.then(Commands.literal("debugFrustum") .then(Commands.literal("pause") .executes(context -> { - FlwShaderUniforms.FRUSTUM_PAUSED = true; + FlwShaderUniforms.frustumPaused = true; return 1; })) .then(Commands.literal("unpause") .executes(context -> { - FlwShaderUniforms.FRUSTUM_PAUSED = false; + FlwShaderUniforms.frustumPaused = false; return 1; })) .then(Commands.literal("capture") .executes(context -> { - FlwShaderUniforms.FRUSTUM_PAUSED = true; - FlwShaderUniforms.FRUSTUM_CAPTURE = true; + FlwShaderUniforms.frustumPaused = true; + FlwShaderUniforms.frustumCapture = true; return 1; }))); diff --git a/src/main/java/com/jozufozu/flywheel/gl/GlCompat.java b/src/main/java/com/jozufozu/flywheel/gl/GlCompat.java index 9b7fe1abe..4ab492405 100644 --- a/src/main/java/com/jozufozu/flywheel/gl/GlCompat.java +++ b/src/main/java/com/jozufozu/flywheel/gl/GlCompat.java @@ -10,6 +10,8 @@ import org.lwjgl.opengl.GLCapabilities; import org.lwjgl.opengl.KHRShaderSubgroup; import org.lwjgl.system.MemoryStack; +import com.jozufozu.flywheel.lib.math.MoreMath; + import net.minecraft.Util; /** @@ -18,19 +20,19 @@ import net.minecraft.Util; * Each field stores an enum variant that provides access to the most appropriate version of a feature for the current * system. */ -public class GlCompat { - public static final boolean ALLOW_DSA = true; +public final class GlCompat { public static final GLCapabilities CAPABILITIES = GL.createCapabilities(); - public static final boolean amd = _decideIfWeAreAMD(); - public static final boolean windows = _decideIfWeAreWindows(); - public static final boolean supportsIndirect = _decideIfWeSupportIndirect(); + public static final boolean AMD = _decideIfWeAreAMD(); + public static final boolean WINDOWS = _decideIfWeAreWindows(); + public static final boolean ALLOW_DSA = true; + public static final boolean SUPPORTS_INDIRECT = _decideIfWeSupportIndirect(); public static final int SUBGROUP_SIZE = _subgroupSize(); private GlCompat() { } public static boolean onAMDWindows() { - return amd && windows; + return AMD && WINDOWS; } public static boolean supportsInstancing() { @@ -38,7 +40,22 @@ public class GlCompat { } public static boolean supportsIndirect() { - return supportsIndirect; + return SUPPORTS_INDIRECT; + } + + private static boolean _decideIfWeAreAMD() { + String vendor = GL20C.glGetString(GL20C.GL_VENDOR); + + if (vendor == null) { + return false; + } + + // vendor string I got was "ATI Technologies Inc." + return vendor.contains("ATI") || vendor.contains("AMD"); + } + + private static boolean _decideIfWeAreWindows() { + return Util.getPlatform() == Util.OS.WINDOWS; } private static boolean _decideIfWeSupportIndirect() { @@ -50,7 +67,11 @@ public class GlCompat { return GL31C.glGetInteger(KHRShaderSubgroup.GL_SUBGROUP_SIZE_KHR); } // try to guess - return amd ? 64 : 32; + return AMD ? 64 : 32; + } + + public static int getComputeGroupCount(int invocations) { + return MoreMath.ceilingDiv(invocations, SUBGROUP_SIZE); } /** @@ -73,20 +94,5 @@ public class GlCompat { GL20C.nglShaderSource(glId, 1, pointers.address0(), 0); } } - - private static boolean _decideIfWeAreWindows() { - return Util.getPlatform() == Util.OS.WINDOWS; - } - - private static boolean _decideIfWeAreAMD() { - String vendor = GL20C.glGetString(GL20C.GL_VENDOR); - - if (vendor == null) { - return false; - } - - // vendor string I got was "ATI Technologies Inc." - return vendor.contains("ATI") || vendor.contains("AMD"); - } } diff --git a/src/main/java/com/jozufozu/flywheel/glsl/GLSLVersion.java b/src/main/java/com/jozufozu/flywheel/glsl/GlslVersion.java similarity index 88% rename from src/main/java/com/jozufozu/flywheel/glsl/GLSLVersion.java rename to src/main/java/com/jozufozu/flywheel/glsl/GlslVersion.java index 60b1feecb..d3cbe62c0 100644 --- a/src/main/java/com/jozufozu/flywheel/glsl/GLSLVersion.java +++ b/src/main/java/com/jozufozu/flywheel/glsl/GlslVersion.java @@ -1,6 +1,6 @@ package com.jozufozu.flywheel.glsl; -public enum GLSLVersion { +public enum GlslVersion { V110(110), V120(120), V130(130), @@ -18,7 +18,7 @@ public enum GLSLVersion { public final int version; - GLSLVersion(int version) { + GlslVersion(int version) { this.version = version; } diff --git a/src/main/java/com/jozufozu/flywheel/glsl/ShaderSources.java b/src/main/java/com/jozufozu/flywheel/glsl/ShaderSources.java index a2993ccc1..5a0f7f088 100644 --- a/src/main/java/com/jozufozu/flywheel/glsl/ShaderSources.java +++ b/src/main/java/com/jozufozu/flywheel/glsl/ShaderSources.java @@ -40,13 +40,6 @@ public class ShaderSources { cache.putAll(preloadCache); } - public ShaderSources(ResourceManager manager, List preloadEmpty) { - this.manager = manager; - for (ResourceLocation rl : preloadEmpty) { - cache.put(rl, SourceFile.empty(rl)); - } - } - @NotNull public LoadResult find(ResourceLocation location) { if (findStack.contains(location)) { diff --git a/src/main/java/com/jozufozu/flywheel/glsl/generate/GlslSwitch.java b/src/main/java/com/jozufozu/flywheel/glsl/generate/GlslSwitch.java index 6c3e0da93..d21a36711 100644 --- a/src/main/java/com/jozufozu/flywheel/glsl/generate/GlslSwitch.java +++ b/src/main/java/com/jozufozu/flywheel/glsl/generate/GlslSwitch.java @@ -39,9 +39,9 @@ public class GlslSwitch implements GlslStmt { @Override public String prettyPrint() { return """ - switch (%s) { - %s - }""".formatted(on.prettyPrint(), formatCases()); + switch (%s) { + %s + }""".formatted(on.prettyPrint(), formatCases()); } @NotNull 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 ba2500b95..303ed12c2 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java +++ b/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java @@ -9,7 +9,7 @@ import com.jozufozu.flywheel.gl.shader.GlProgram; import net.minecraft.resources.ResourceLocation; public final class Contexts { - public static final SimpleContext WORLD = Context.REGISTRY.registerAndGet(new SimpleContext(Files.WORLD_VERTEX, Files.WORLD_FRAGMENT, program -> { + 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); @@ -32,14 +32,14 @@ public final class Contexts { } public static final class Files { - public static final ResourceLocation WORLD_VERTEX = Names.WORLD.withSuffix(".vert"); - public static final ResourceLocation WORLD_FRAGMENT = Names.WORLD.withSuffix(".frag"); + 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 WORLD = Flywheel.rl("context/world"); + 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/AbstractInstance.java b/src/main/java/com/jozufozu/flywheel/lib/instance/AbstractInstance.java index 33d790d12..7e02a5ae1 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/AbstractInstance.java +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/AbstractInstance.java @@ -14,12 +14,12 @@ public abstract class AbstractInstance implements Instance { } @Override - public InstanceType type() { + public final InstanceType type() { return type; } @Override - public InstanceHandle handle() { + public final InstanceHandle handle() { return handle; } diff --git a/src/main/java/com/jozufozu/flywheel/lib/material/CutoutShaders.java b/src/main/java/com/jozufozu/flywheel/lib/material/CutoutShaders.java index 10696b854..8ce015841 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/material/CutoutShaders.java +++ b/src/main/java/com/jozufozu/flywheel/lib/material/CutoutShaders.java @@ -14,6 +14,10 @@ public class CutoutShaders { * 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"))); + /** + * 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"))); /** * Discard fragments with alpha less than to 0.5. */ diff --git a/src/main/java/com/jozufozu/flywheel/lib/material/Materials.java b/src/main/java/com/jozufozu/flywheel/lib/material/Materials.java index 0cfb04741..91aad042d 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/material/Materials.java +++ b/src/main/java/com/jozufozu/flywheel/lib/material/Materials.java @@ -39,27 +39,27 @@ public final class Materials { .build(); public static final Material CHUNK_CUTOUT_MIPPED_SHADED = SimpleMaterial.builder() - .cutout(CutoutShaders.EPSILON) + .cutout(CutoutShaders.HALF) .useOverlay(false) .fallbackRenderType(RenderType.cutoutMipped()) .vertexTransformer(SHADING_TRANSFORMER) .build(); public static final Material CHUNK_CUTOUT_MIPPED_UNSHADED = SimpleMaterial.builder() - .cutout(CutoutShaders.EPSILON) + .cutout(CutoutShaders.HALF) .useOverlay(false) .diffuse(false) .fallbackRenderType(RenderType.cutoutMipped()) .build(); public static final Material CHUNK_CUTOUT_SHADED = SimpleMaterial.builder() - .cutout(CutoutShaders.EPSILON) + .cutout(CutoutShaders.ONE_TENTH) .mipmap(false) .useOverlay(false) .fallbackRenderType(RenderType.cutout()) .vertexTransformer(SHADING_TRANSFORMER) .build(); public static final Material CHUNK_CUTOUT_UNSHADED = SimpleMaterial.builder() - .cutout(CutoutShaders.EPSILON) + .cutout(CutoutShaders.ONE_TENTH) .mipmap(false) .useOverlay(false) .diffuse(false) @@ -80,14 +80,14 @@ public final class Materials { .build(); public static final Material CHUNK_TRIPWIRE_SHADED = SimpleMaterial.builder() - .cutout(CutoutShaders.EPSILON) + .cutout(CutoutShaders.ONE_TENTH) .transparency(Transparency.TRANSLUCENT) .useOverlay(false) .fallbackRenderType(RenderType.tripwire()) .vertexTransformer(SHADING_TRANSFORMER) .build(); public static final Material CHUNK_TRIPWIRE_UNSHADED = SimpleMaterial.builder() - .cutout(CutoutShaders.EPSILON) + .cutout(CutoutShaders.ONE_TENTH) .transparency(Transparency.TRANSLUCENT) .useOverlay(false) .diffuse(false) @@ -95,13 +95,14 @@ public final class Materials { .build(); public static final Material CHEST = SimpleMaterial.builder() - .baseTexture(Sheets.CHEST_SHEET) + .cutout(CutoutShaders.ONE_TENTH) + .texture(Sheets.CHEST_SHEET) .mipmap(false) .fallbackRenderType(Sheets.chestSheet()) .build(); public static final Material SHULKER = SimpleMaterial.builder() - .cutout(CutoutShaders.EPSILON) - .baseTexture(Sheets.SHULKER_SHEET) + .cutout(CutoutShaders.ONE_TENTH) + .texture(Sheets.SHULKER_SHEET) .mipmap(false) .backfaceCulling(false) .fallbackRenderType(Sheets.shulkerBoxSheet()) @@ -111,7 +112,7 @@ public final class Materials { .fallbackRenderType(Sheets.solidBlockSheet()) .build(); public static final Material MINECART = SimpleMaterial.builder() - .baseTexture(MINECART_LOCATION) + .texture(MINECART_LOCATION) .mipmap(false) .fallbackRenderType(RenderType.entitySolid(MINECART_LOCATION)) .build(); diff --git a/src/main/java/com/jozufozu/flywheel/lib/material/SimpleMaterial.java b/src/main/java/com/jozufozu/flywheel/lib/material/SimpleMaterial.java index fddd1fecc..caddd26e2 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/material/SimpleMaterial.java +++ b/src/main/java/com/jozufozu/flywheel/lib/material/SimpleMaterial.java @@ -21,7 +21,7 @@ public class SimpleMaterial implements Material { protected final FogShader fog; protected final CutoutShader cutout; - protected final ResourceLocation baseTexture; + protected final ResourceLocation texture; protected final boolean blur; protected final boolean mipmap; @@ -41,7 +41,7 @@ public class SimpleMaterial implements Material { shaders = builder.shaders(); fog = builder.fog(); cutout = builder.cutout(); - baseTexture = builder.baseTexture(); + texture = builder.texture(); blur = builder.blur(); mipmap = builder.mipmap(); backfaceCulling = builder.backfaceCulling(); @@ -88,8 +88,8 @@ public class SimpleMaterial implements Material { } @Override - public ResourceLocation baseTexture() { - return baseTexture; + public ResourceLocation texture() { + return texture; } @Override @@ -150,7 +150,7 @@ public class SimpleMaterial implements Material { protected FogShader fog; protected CutoutShader cutout; - protected ResourceLocation baseTexture; + protected ResourceLocation texture; protected boolean blur; protected boolean mipmap; @@ -171,14 +171,14 @@ public class SimpleMaterial implements Material { shaders = StandardMaterialShaders.DEFAULT; fog = FogShaders.LINEAR; cutout = CutoutShaders.OFF; - baseTexture = InventoryMenu.BLOCK_ATLAS; + texture = InventoryMenu.BLOCK_ATLAS; blur = false; mipmap = true; backfaceCulling = true; polygonOffset = false; depthTest = DepthTest.LEQUAL; transparency = Transparency.OPAQUE; - writeMask = WriteMask.BOTH; + writeMask = WriteMask.COLOR_DEPTH; useOverlay = true; useLight = true; diffuse = true; @@ -194,7 +194,7 @@ public class SimpleMaterial implements Material { shaders = material.shaders(); fog = material.fog(); cutout = material.cutout(); - baseTexture = material.baseTexture(); + texture = material.texture(); blur = material.blur(); mipmap = material.mipmap(); backfaceCulling = material.backfaceCulling(); @@ -233,8 +233,8 @@ public class SimpleMaterial implements Material { return this; } - public Builder baseTexture(ResourceLocation value) { - this.baseTexture = value; + public Builder texture(ResourceLocation value) { + this.texture = value; return this; } @@ -314,8 +314,8 @@ public class SimpleMaterial implements Material { } @Override - public ResourceLocation baseTexture() { - return baseTexture; + public ResourceLocation texture() { + return texture; } @Override diff --git a/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java b/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java index fbb659c3b..e726e4527 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java +++ b/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java @@ -2,7 +2,6 @@ package com.jozufozu.flywheel.lib.model; import java.lang.reflect.Field; import java.nio.ByteBuffer; -import java.util.Collection; import org.jetbrains.annotations.Nullable; import org.joml.Vector4f; @@ -101,29 +100,33 @@ public final class ModelUtil { return null; } - public static Vector4f computeBoundingSphere(Collection values) { - int totalVertices = 0; - for (Mesh value : values) { - totalVertices += value.vertexCount(); + public static int computeTotalVertexCount(Iterable meshes) { + int vertexCount = 0; + for (Mesh mesh : meshes) { + vertexCount += mesh.vertexCount(); } - var block = MemoryBlock.malloc((long) totalVertices * PositionOnlyVertexList.STRIDE); + return vertexCount; + } + public static Vector4f computeBoundingSphere(Iterable meshes) { + int vertexCount = computeTotalVertexCount(meshes); + var block = MemoryBlock.malloc((long) vertexCount * PositionOnlyVertexList.STRIDE); var vertexList = new PositionOnlyVertexList(); int baseVertex = 0; - for (Mesh value : values) { + for (Mesh mesh : meshes) { vertexList.ptr(block.ptr() + (long) baseVertex * PositionOnlyVertexList.STRIDE); - value.write(vertexList); - baseVertex += value.vertexCount(); + mesh.write(vertexList); + baseVertex += mesh.vertexCount(); } vertexList.ptr(block.ptr()); - vertexList.vertexCount(totalVertices); - - var out = computeBoundingSphere(vertexList); + vertexList.vertexCount(vertexCount); + var sphere = computeBoundingSphere(vertexList); block.free(); - return out; + + return sphere; } public static Vector4f computeBoundingSphere(VertexList vertexList) { diff --git a/src/main/java/com/jozufozu/flywheel/lib/model/SimpleModel.java b/src/main/java/com/jozufozu/flywheel/lib/model/SimpleModel.java index e58d6ec21..c01502631 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/model/SimpleModel.java +++ b/src/main/java/com/jozufozu/flywheel/lib/model/SimpleModel.java @@ -2,27 +2,42 @@ package com.jozufozu.flywheel.lib.model; import java.util.Map; +import org.joml.Vector4fc; + import com.google.common.collect.ImmutableMap; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.model.Mesh; import com.jozufozu.flywheel.api.model.Model; public class SimpleModel implements Model { - private final Mesh mesh; - private final Map meshMap; + private final ImmutableMap meshes; + private final Vector4fc boundingSphere; + private final int vertexCount; - public SimpleModel(Mesh mesh, Material material) { - this.mesh = mesh; - meshMap = ImmutableMap.of(material, mesh); + public SimpleModel(ImmutableMap meshes) { + this.meshes = meshes; + this.boundingSphere = ModelUtil.computeBoundingSphere(meshes.values()); + this.vertexCount = ModelUtil.computeTotalVertexCount(meshes.values()); } @Override - public Map getMeshes() { - return meshMap; + public Map meshes() { + return meshes; + } + + @Override + public Vector4fc boundingSphere() { + return boundingSphere; + } + + @Override + public int vertexCount() { + return vertexCount; } @Override public void delete() { - mesh.delete(); + meshes.values() + .forEach(Mesh::delete); } } diff --git a/src/main/java/com/jozufozu/flywheel/lib/model/SingleMeshModel.java b/src/main/java/com/jozufozu/flywheel/lib/model/SingleMeshModel.java new file mode 100644 index 000000000..c502fe5f3 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/lib/model/SingleMeshModel.java @@ -0,0 +1,40 @@ +package com.jozufozu.flywheel.lib.model; + +import java.util.Map; + +import org.joml.Vector4fc; + +import com.google.common.collect.ImmutableMap; +import com.jozufozu.flywheel.api.material.Material; +import com.jozufozu.flywheel.api.model.Mesh; +import com.jozufozu.flywheel.api.model.Model; + +public class SingleMeshModel implements Model { + private final Mesh mesh; + private final Map meshMap; + + public SingleMeshModel(Mesh mesh, Material material) { + this.mesh = mesh; + meshMap = ImmutableMap.of(material, mesh); + } + + @Override + public Map meshes() { + return meshMap; + } + + @Override + public Vector4fc boundingSphere() { + return mesh.boundingSphere(); + } + + @Override + public int vertexCount() { + return mesh.vertexCount(); + } + + @Override + public void delete() { + mesh.delete(); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/lib/model/baked/TessellatedModel.java b/src/main/java/com/jozufozu/flywheel/lib/model/baked/TessellatedModel.java index f20dff2d4..9b7bf8e30 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/model/baked/TessellatedModel.java +++ b/src/main/java/com/jozufozu/flywheel/lib/model/baked/TessellatedModel.java @@ -1,32 +1,18 @@ package com.jozufozu.flywheel.lib.model.baked; -import java.util.Map; - import com.google.common.collect.ImmutableMap; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.model.Mesh; -import com.jozufozu.flywheel.api.model.Model; +import com.jozufozu.flywheel.lib.model.SimpleModel; -public class TessellatedModel implements Model { - private final ImmutableMap meshes; +public class TessellatedModel extends SimpleModel { private final boolean shadeSeparated; public TessellatedModel(ImmutableMap meshes, boolean shadeSeparated) { - this.meshes = meshes; + super(meshes); this.shadeSeparated = shadeSeparated; } - @Override - public Map getMeshes() { - return meshes; - } - - @Override - public void delete() { - meshes.values() - .forEach(Mesh::delete); - } - public boolean isShadeSeparated() { return shadeSeparated; } diff --git a/src/main/java/com/jozufozu/flywheel/lib/uniform/FlwShaderUniforms.java b/src/main/java/com/jozufozu/flywheel/lib/uniform/FlwShaderUniforms.java index 86c87d568..e1ca16006 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/uniform/FlwShaderUniforms.java +++ b/src/main/java/com/jozufozu/flywheel/lib/uniform/FlwShaderUniforms.java @@ -24,9 +24,9 @@ public class FlwShaderUniforms implements ShaderUniforms { public static final ResourceLocation FILE = Flywheel.rl("uniform/flywheel.glsl"); public static final int SIZE = 224; - public static boolean FRUSTUM_PAUSED = false; - public static boolean FRUSTUM_CAPTURE = false; - public static boolean FOG_UPDATE = true; + public static boolean frustumPaused = false; + public static boolean frustumCapture = false; + public static boolean fogUpdate = true; @Override public int byteSize() { @@ -94,9 +94,9 @@ public class FlwShaderUniforms implements ShaderUniforms { MemoryUtil.memPutFloat(ptr + 108, 0f); // vec4 alignment MemoryUtil.memPutInt(ptr + 112, getConstantAmbientLightFlag(context)); - if (!FRUSTUM_PAUSED || FRUSTUM_CAPTURE) { + if (!frustumPaused || frustumCapture) { MatrixMath.writePackedFrustumPlanes(ptr + 128, viewProjection); - FRUSTUM_CAPTURE = false; + frustumCapture = false; } dirty = true; @@ -110,7 +110,7 @@ public class FlwShaderUniforms implements ShaderUniforms { } private boolean maybeUpdateFog() { - if (!FOG_UPDATE || ptr == MemoryUtil.NULL) { + if (!fogUpdate || ptr == MemoryUtil.NULL) { return false; } @@ -125,7 +125,7 @@ public class FlwShaderUniforms implements ShaderUniforms { MemoryUtil.memPutInt(ptr + 24, RenderSystem.getShaderFogShape() .getIndex()); - FOG_UPDATE = false; + fogUpdate = false; return true; } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/FogUpdateMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/FogUpdateMixin.java index e557076f0..43e8f6059 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/FogUpdateMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/FogUpdateMixin.java @@ -14,7 +14,7 @@ import net.minecraft.client.renderer.FogRenderer; abstract class FogUpdateMixin { @Unique private static void flywheel$updateFog() { - FlwShaderUniforms.FOG_UPDATE = true; + FlwShaderUniforms.fogUpdate = true; } @Inject(method = "setupNoFog()V", at = @At("RETURN")) diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/BellVisual.java b/src/main/java/com/jozufozu/flywheel/vanilla/BellVisual.java index 14ff861da..f23e0d33f 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/BellVisual.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/BellVisual.java @@ -15,7 +15,7 @@ import com.jozufozu.flywheel.lib.instance.InstanceTypes; import com.jozufozu.flywheel.lib.instance.OrientedInstance; import com.jozufozu.flywheel.lib.material.Materials; import com.jozufozu.flywheel.lib.model.ModelHolder; -import com.jozufozu.flywheel.lib.model.SimpleModel; +import com.jozufozu.flywheel.lib.model.SingleMeshModel; import com.jozufozu.flywheel.lib.model.part.ModelPartConverter; import com.jozufozu.flywheel.lib.visual.AbstractBlockEntityVisual; @@ -26,7 +26,7 @@ import net.minecraft.world.level.block.entity.BellBlockEntity; public class BellVisual extends AbstractBlockEntityVisual implements DynamicVisual { private static final ModelHolder BELL_MODEL = new ModelHolder(() -> { - return new SimpleModel(ModelPartConverter.convert(ModelLayers.BELL, BellRenderer.BELL_RESOURCE_LOCATION.sprite(), "bell_body"), Materials.BELL); + return new SingleMeshModel(ModelPartConverter.convert(ModelLayers.BELL, BellRenderer.BELL_RESOURCE_LOCATION.sprite(), "bell_body"), Materials.BELL); }); private OrientedInstance bell; diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestVisual.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestVisual.java index 341215887..af6fae6e1 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ChestVisual.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestVisual.java @@ -17,7 +17,7 @@ import com.jozufozu.flywheel.lib.instance.OrientedInstance; import com.jozufozu.flywheel.lib.instance.TransformedInstance; import com.jozufozu.flywheel.lib.material.Materials; import com.jozufozu.flywheel.lib.model.ModelCache; -import com.jozufozu.flywheel.lib.model.SimpleModel; +import com.jozufozu.flywheel.lib.model.SingleMeshModel; import com.jozufozu.flywheel.lib.model.part.ModelPartConverter; import com.jozufozu.flywheel.lib.util.Pair; import com.jozufozu.flywheel.lib.visual.AbstractBlockEntityVisual; @@ -45,13 +45,13 @@ public class ChestVisual extends Abstrac } private static final ModelCache> BOTTOM_MODELS = new ModelCache<>(key -> { - return new SimpleModel(ModelPartConverter.convert(LAYER_LOCATIONS.get(key.first()), key.second().sprite(), "bottom"), Materials.CHEST); + return new SingleMeshModel(ModelPartConverter.convert(LAYER_LOCATIONS.get(key.first()), key.second().sprite(), "bottom"), Materials.CHEST); }); private static final ModelCache> LID_MODELS = new ModelCache<>(key -> { - return new SimpleModel(ModelPartConverter.convert(LAYER_LOCATIONS.get(key.first()), key.second().sprite(), "lid"), Materials.CHEST); + return new SingleMeshModel(ModelPartConverter.convert(LAYER_LOCATIONS.get(key.first()), key.second().sprite(), "lid"), Materials.CHEST); }); private static final ModelCache> LOCK_MODELS = new ModelCache<>(key -> { - return new SimpleModel(ModelPartConverter.convert(LAYER_LOCATIONS.get(key.first()), key.second().sprite(), "lock"), Materials.CHEST); + return new SingleMeshModel(ModelPartConverter.convert(LAYER_LOCATIONS.get(key.first()), key.second().sprite(), "lock"), Materials.CHEST); }); private OrientedInstance bottom; diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartVisual.java b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartVisual.java index cbb451a20..a00a3ebed 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartVisual.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartVisual.java @@ -11,7 +11,7 @@ import com.jozufozu.flywheel.lib.instance.TransformedInstance; import com.jozufozu.flywheel.lib.material.Materials; import com.jozufozu.flywheel.lib.model.ModelHolder; import com.jozufozu.flywheel.lib.model.Models; -import com.jozufozu.flywheel.lib.model.SimpleModel; +import com.jozufozu.flywheel.lib.model.SingleMeshModel; import com.jozufozu.flywheel.lib.model.part.ModelPartConverter; import com.jozufozu.flywheel.lib.visual.AbstractEntityVisual; import com.mojang.blaze3d.vertex.PoseStack; @@ -26,7 +26,7 @@ import net.minecraft.world.phys.Vec3; public class MinecartVisual extends AbstractEntityVisual implements TickableVisual, DynamicVisual { private static final ModelHolder BODY_MODEL = new ModelHolder(() -> { - return new SimpleModel(ModelPartConverter.convert(ModelLayers.MINECART), Materials.MINECART); + return new SingleMeshModel(ModelPartConverter.convert(ModelLayers.MINECART), Materials.MINECART); }); private TransformedInstance body; diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxVisual.java b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxVisual.java index 7054c1ad6..5fcd540cc 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxVisual.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxVisual.java @@ -13,7 +13,7 @@ import com.jozufozu.flywheel.lib.instance.InstanceTypes; import com.jozufozu.flywheel.lib.instance.TransformedInstance; import com.jozufozu.flywheel.lib.material.Materials; import com.jozufozu.flywheel.lib.model.ModelCache; -import com.jozufozu.flywheel.lib.model.SimpleModel; +import com.jozufozu.flywheel.lib.model.SingleMeshModel; import com.jozufozu.flywheel.lib.model.part.ModelPartConverter; import com.jozufozu.flywheel.lib.transform.TransformStack; import com.jozufozu.flywheel.lib.visual.AbstractBlockEntityVisual; @@ -30,10 +30,10 @@ import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity; public class ShulkerBoxVisual extends AbstractBlockEntityVisual implements DynamicVisual { private static final ModelCache BASE_MODELS = new ModelCache<>(texture -> { - return new SimpleModel(ModelPartConverter.convert(ModelLayers.SHULKER, texture.sprite(), "base"), Materials.SHULKER); + return new SingleMeshModel(ModelPartConverter.convert(ModelLayers.SHULKER, texture.sprite(), "base"), Materials.SHULKER); }); private static final ModelCache LID_MODELS = new ModelCache<>(texture -> { - return new SimpleModel(ModelPartConverter.convert(ModelLayers.SHULKER, texture.sprite(), "lid"), Materials.SHULKER); + return new SingleMeshModel(ModelPartConverter.convert(ModelLayers.SHULKER, texture.sprite(), "lid"), Materials.SHULKER); }); private TransformedInstance base; diff --git a/src/main/resources/assets/flywheel/flywheel/context/crumbling.frag b/src/main/resources/assets/flywheel/flywheel/context/crumbling.frag index c1c88cbdc..a722816bb 100644 --- a/src/main/resources/assets/flywheel/flywheel/context/crumbling.frag +++ b/src/main/resources/assets/flywheel/flywheel/context/crumbling.frag @@ -1,5 +1,3 @@ -#include "flywheel:api/fragment.glsl" - uniform sampler2D _flw_crumblingTex; in vec2 crumblingTexCoord; @@ -7,16 +5,12 @@ in vec2 crumblingTexCoord; vec4 crumblingSampleColor; void flw_beginFragment() { - crumblingSampleColor = texture(_flw_crumblingTex, crumblingTexCoord); - - // Make the crumbling overlay transparent when the diffuse layer is transparent. - crumblingSampleColor.a *= flw_fragColor.a; - - if (crumblingSampleColor.a < 0.01) { - discard; - } } void flw_endFragment() { - flw_fragColor = crumblingSampleColor; + crumblingSampleColor = texture(_flw_crumblingTex, crumblingTexCoord); + + // Make the crumbling overlay transparent when the fragment color after the material shader is transparent. + flw_fragColor.rgb = crumblingSampleColor.rgb; + flw_fragColor.a *= crumblingSampleColor.a; } diff --git a/src/main/resources/assets/flywheel/flywheel/context/crumbling.vert b/src/main/resources/assets/flywheel/flywheel/context/crumbling.vert index 8e916fa0e..d765f4ee2 100644 --- a/src/main/resources/assets/flywheel/flywheel/context/crumbling.vert +++ b/src/main/resources/assets/flywheel/flywheel/context/crumbling.vert @@ -1,6 +1,3 @@ -#include "flywheel:api/vertex.glsl" -#include "flywheel:util/fog.glsl" - out vec2 crumblingTexCoord; const int DOWN = 0; diff --git a/src/main/resources/assets/flywheel/flywheel/context/world.frag b/src/main/resources/assets/flywheel/flywheel/context/default.frag similarity index 59% rename from src/main/resources/assets/flywheel/flywheel/context/world.frag rename to src/main/resources/assets/flywheel/flywheel/context/default.frag index 29e68d96e..d5d083a2a 100644 --- a/src/main/resources/assets/flywheel/flywheel/context/world.frag +++ b/src/main/resources/assets/flywheel/flywheel/context/default.frag @@ -1,5 +1,3 @@ -#include "flywheel:api/fragment.glsl" - void flw_beginFragment() { } diff --git a/src/main/resources/assets/flywheel/flywheel/context/default.vert b/src/main/resources/assets/flywheel/flywheel/context/default.vert new file mode 100644 index 000000000..56efd2eae --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/context/default.vert @@ -0,0 +1,5 @@ +void flw_beginVertex() { +} + +void flw_endVertex() { +} diff --git a/src/main/resources/assets/flywheel/flywheel/context/world.vert b/src/main/resources/assets/flywheel/flywheel/context/world.vert deleted file mode 100644 index ec41e0c40..000000000 --- a/src/main/resources/assets/flywheel/flywheel/context/world.vert +++ /dev/null @@ -1,8 +0,0 @@ -#include "flywheel:api/vertex.glsl" -#include "flywheel:util/fog.glsl" - -void flw_beginVertex() { -} - -void flw_endVertex() { -} diff --git a/src/main/resources/assets/flywheel/flywheel/cutout/epsilon.glsl b/src/main/resources/assets/flywheel/flywheel/cutout/epsilon.glsl index 84b8c88b5..6661504e8 100644 --- a/src/main/resources/assets/flywheel/flywheel/cutout/epsilon.glsl +++ b/src/main/resources/assets/flywheel/flywheel/cutout/epsilon.glsl @@ -1,3 +1,3 @@ -bool flw_discardPredicate(vec4 finalColor) { - return finalColor.a < 0.01; +bool flw_discardPredicate(vec4 color) { + return color.a < 0.01; } diff --git a/src/main/resources/assets/flywheel/flywheel/cutout/half.glsl b/src/main/resources/assets/flywheel/flywheel/cutout/half.glsl index 30b871c45..8e8cc253a 100644 --- a/src/main/resources/assets/flywheel/flywheel/cutout/half.glsl +++ b/src/main/resources/assets/flywheel/flywheel/cutout/half.glsl @@ -1,3 +1,3 @@ -bool flw_discardPredicate(vec4 finalColor) { - return finalColor.a < 0.5; +bool flw_discardPredicate(vec4 color) { + return color.a < 0.5; } diff --git a/src/main/resources/assets/flywheel/flywheel/cutout/off.glsl b/src/main/resources/assets/flywheel/flywheel/cutout/off.glsl index f55d02f66..9daa3b3cf 100644 --- a/src/main/resources/assets/flywheel/flywheel/cutout/off.glsl +++ b/src/main/resources/assets/flywheel/flywheel/cutout/off.glsl @@ -1,3 +1,3 @@ -bool flw_discardPredicate(vec4 finalColor) { +bool flw_discardPredicate(vec4 color) { return false; } diff --git a/src/main/resources/assets/flywheel/flywheel/cutout/one_tenth.glsl b/src/main/resources/assets/flywheel/flywheel/cutout/one_tenth.glsl new file mode 100644 index 000000000..83e4aeacc --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/cutout/one_tenth.glsl @@ -0,0 +1,3 @@ +bool flw_discardPredicate(vec4 color) { + return color.a < 0.1; +} diff --git a/src/main/resources/assets/flywheel/flywheel/fog/linear.glsl b/src/main/resources/assets/flywheel/flywheel/fog/linear.glsl index 45d8dcaa6..0b774aee8 100644 --- a/src/main/resources/assets/flywheel/flywheel/fog/linear.glsl +++ b/src/main/resources/assets/flywheel/flywheel/fog/linear.glsl @@ -1,5 +1,12 @@ -#include "flywheel:util/fog.glsl" +vec4 linearFog(vec4 color, float distance, float fogStart, float fogEnd, vec4 fogColor) { + if (distance <= fogStart) { + return color; + } + + float fogValue = distance < fogEnd ? smoothstep(fogStart, fogEnd, distance) : 1.0; + return vec4(mix(color.rgb, fogColor.rgb, fogValue * fogColor.a), color.a); +} vec4 flw_fogFilter(vec4 color) { - return linear_fog(color, flw_distance, flywheel.fogRange.x, flywheel.fogRange.y, flywheel.fogColor); + return linearFog(color, flw_distance, flywheel.fogRange.x, flywheel.fogRange.y, flywheel.fogColor); } diff --git a/src/main/resources/assets/flywheel/flywheel/fog/linear_fade.glsl b/src/main/resources/assets/flywheel/flywheel/fog/linear_fade.glsl index dde13e9b0..0adf99a10 100644 --- a/src/main/resources/assets/flywheel/flywheel/fog/linear_fade.glsl +++ b/src/main/resources/assets/flywheel/flywheel/fog/linear_fade.glsl @@ -1,5 +1,13 @@ -#include "flywheel:util/fog.glsl" +vec4 linearFogFade(vec4 color, float distance, float fogStart, float fogEnd) { + if (distance <= fogStart) { + return color; + } else if (distance >= fogEnd) { + return vec4(0.0); + } + + return color * smoothstep(fogEnd, fogStart, distance); +} vec4 flw_fogFilter(vec4 color) { - return linear_fog_fade(color, flw_distance, flywheel.fogRange.x, flywheel.fogRange.y); + return linearFogFade(color, flw_distance, flywheel.fogRange.x, flywheel.fogRange.y); } diff --git a/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert b/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert index 9e4da218c..660bb3992 100644 --- a/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert +++ b/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert @@ -1,4 +1,3 @@ -#include "flywheel:api/vertex.glsl" #include "flywheel:util/quaternion.glsl" void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout float radius) { diff --git a/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert b/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert index 93d6ee798..f7a2d5c70 100644 --- a/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert +++ b/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert @@ -1,5 +1,3 @@ -#include "flywheel:api/vertex.glsl" - void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout float radius) { mat4 pose = i.pose; center = (pose * vec4(center, 1.0)).xyz; diff --git a/src/main/resources/assets/flywheel/flywheel/internal/common_api_impl.frag b/src/main/resources/assets/flywheel/flywheel/internal/common_api_impl.frag new file mode 100644 index 000000000..06539b86c --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/common_api_impl.frag @@ -0,0 +1,18 @@ +#include "flywheel:internal/material.glsl" + +in vec4 flw_vertexPos; +in vec4 flw_vertexColor; +in vec2 flw_vertexTexCoord; +flat in ivec2 flw_vertexOverlay; +in vec2 flw_vertexLight; +in vec3 flw_vertexNormal; + +in float flw_distance; + +vec4 flw_sampleColor; + +FlwMaterial flw_material; + +vec4 flw_fragColor; +ivec2 flw_fragOverlay; +vec2 flw_fragLight; diff --git a/src/main/resources/assets/flywheel/flywheel/internal/common_api_impl.vert b/src/main/resources/assets/flywheel/flywheel/internal/common_api_impl.vert new file mode 100644 index 000000000..13f709796 --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/common_api_impl.vert @@ -0,0 +1,12 @@ +#include "flywheel:internal/material.glsl" + +out vec4 flw_vertexPos; +out vec4 flw_vertexColor; +out vec2 flw_vertexTexCoord; +flat out ivec2 flw_vertexOverlay; +out vec2 flw_vertexLight; +out vec3 flw_vertexNormal; + +out float flw_distance; + +FlwMaterial flw_material; diff --git a/src/main/resources/assets/flywheel/flywheel/util/diffuse.glsl b/src/main/resources/assets/flywheel/flywheel/internal/diffuse.glsl similarity index 100% rename from src/main/resources/assets/flywheel/flywheel/util/diffuse.glsl rename to src/main/resources/assets/flywheel/flywheel/internal/diffuse.glsl diff --git a/src/main/resources/assets/flywheel/flywheel/internal/fog_distance.glsl b/src/main/resources/assets/flywheel/flywheel/internal/fog_distance.glsl new file mode 100644 index 000000000..7cedf058d --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/fog_distance.glsl @@ -0,0 +1,21 @@ +float sphericalDistance(vec3 relativePos) { + return length(relativePos); +} + +float cylindricalDistance(vec3 relativePos) { + float distXZ = length(relativePos.xz); + float distY = abs(relativePos.y); + return max(distXZ, distY); +} + +float fogDistance(vec3 relativePos, int fogShape) { + if (fogShape == 0) { + return sphericalDistance(relativePos); + } else { + return cylindricalDistance(relativePos); + } +} + +float fogDistance(vec3 worldPos, vec3 cameraPos, int fogShape) { + return fogDistance(worldPos - cameraPos, fogShape); +} diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/api/fragment.glsl b/src/main/resources/assets/flywheel/flywheel/internal/indirect/api/fragment.glsl deleted file mode 100644 index b7daeaefc..000000000 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/api/fragment.glsl +++ /dev/null @@ -1,44 +0,0 @@ -// API -// ------------------------------------------ - -#include "flywheel:api/material.glsl" - -in vec4 flw_vertexPos; -in vec4 flw_vertexColor; -in vec2 flw_vertexTexCoord; -flat in ivec2 flw_vertexOverlay; -in vec2 flw_vertexLight; -in vec3 flw_vertexNormal; - -in float flw_distance; - -in vec4 flw_var0; -in vec4 flw_var1; -in vec4 flw_var2; -in vec4 flw_var3; - -FlwMaterial flw_material; - -vec4 flw_sampleColor; - -vec4 flw_fragColor; -ivec2 flw_fragOverlay; -vec2 flw_fragLight; - -vec4 flw_fogFilter(vec4 color); - -bool flw_discardPredicate(vec4 finalColor); - -void flw_beginFragment(); -void flw_materialFragment(); -void flw_endFragment(); - -// ------------------------------------------ -// INTERNAL -// ------------------------------------------ - -uint _flw_materialFragmentID; -uint _flw_fogID; -uint _flw_cutoutID; - -// ------------------------------------------ diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/api/vertex.glsl b/src/main/resources/assets/flywheel/flywheel/internal/indirect/api/vertex.glsl deleted file mode 100644 index 43ee469fe..000000000 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/api/vertex.glsl +++ /dev/null @@ -1,36 +0,0 @@ -// API -// ------------------------------------------ - -#include "flywheel:api/material.glsl" - -out vec4 flw_vertexPos; -out vec4 flw_vertexColor; -out vec2 flw_vertexTexCoord; -flat out ivec2 flw_vertexOverlay; -out vec2 flw_vertexLight; -out vec3 flw_vertexNormal; - -out float flw_distance; - -out vec4 flw_var0; -out vec4 flw_var1; -out vec4 flw_var2; -out vec4 flw_var3; - -FlwMaterial flw_material; - -void flw_layoutVertex(); -void flw_beginVertex(); -void flw_instanceVertex(FlwInstance i); -void flw_materialVertex(); -void flw_endVertex(); - -// ------------------------------------------ -// INTERNAL -// ------------------------------------------ - -uint _flw_materialVertexID; - -FlwInstance _flw_unpackInstance(FlwPackedInstance i); - -// ------------------------------------------ diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/api_impl.frag b/src/main/resources/assets/flywheel/flywheel/internal/indirect/api_impl.frag new file mode 100644 index 000000000..cf01e512f --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/api_impl.frag @@ -0,0 +1,5 @@ +#include "flywheel:internal/common_api_impl.frag" + +uint _flw_uberMaterialFragmentIndex; +uint _flw_uberFogIndex; +uint _flw_uberCutoutIndex; diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/api_impl.vert b/src/main/resources/assets/flywheel/flywheel/internal/indirect/api_impl.vert new file mode 100644 index 000000000..fafdd66f4 --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/api_impl.vert @@ -0,0 +1,3 @@ +#include "flywheel:internal/common_api_impl.vert" + +uint _flw_uberMaterialVertexIndex; diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/apply.glsl b/src/main/resources/assets/flywheel/flywheel/internal/indirect/apply.glsl index 09b673f2b..6c697615c 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/apply.glsl +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/apply.glsl @@ -2,27 +2,25 @@ #include "flywheel:internal/indirect/model_descriptor.glsl" #include "flywheel:internal/indirect/draw_command.glsl" -layout(local_size_x = FLW_SUBGROUP_SIZE) in; +layout(local_size_x = _FLW_SUBGROUP_SIZE) in; -layout(std430, binding = MODEL_BINDING) restrict readonly buffer ModelDescriptors { +layout(std430, binding = _FLW_MODEL_BUFFER_BINDING) restrict readonly buffer ModelBuffer { ModelDescriptor models[]; }; -layout(std430, binding = DRAW_BINDING) restrict buffer MeshDrawCommands { +layout(std430, binding = _FLW_DRAW_BUFFER_BINDING) restrict buffer DrawBuffer { MeshDrawCommand drawCommands[]; }; // Apply the results of culling to the draw commands. void main() { - uint drawID = gl_GlobalInvocationID.x; + uint drawIndex = gl_GlobalInvocationID.x; - if (drawID >= drawCommands.length()) { + if (drawIndex >= drawCommands.length()) { return; } - uint modelID = drawCommands[drawID].modelID; - - uint instanceCount = models[modelID].instanceCount; - - drawCommands[drawID].instanceCount = instanceCount; + uint modelIndex = drawCommands[drawIndex].modelIndex; + uint instanceCount = models[modelIndex].instanceCount; + drawCommands[drawIndex].instanceCount = instanceCount; } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/buffers.glsl b/src/main/resources/assets/flywheel/flywheel/internal/indirect/buffers.glsl index 4458976a3..0fd422350 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/buffers.glsl +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/buffers.glsl @@ -1,4 +1,4 @@ -#define OBJECT_BINDING 0 -#define TARGET_BINDING 1 -#define MODEL_BINDING 2 -#define DRAW_BINDING 3 +#define _FLW_OBJECT_BUFFER_BINDING 0 +#define _FLW_TARGET_BUFFER_BINDING 1 +#define _FLW_MODEL_BUFFER_BINDING 2 +#define _FLW_DRAW_BUFFER_BINDING 3 diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/cull.glsl b/src/main/resources/assets/flywheel/flywheel/internal/indirect/cull.glsl index 8b1323c49..01012c46d 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/cull.glsl +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/cull.glsl @@ -2,7 +2,7 @@ #include "flywheel:internal/indirect/model_descriptor.glsl" #include "flywheel:internal/indirect/object.glsl" -layout(local_size_x = FLW_SUBGROUP_SIZE) in; +layout(local_size_x = _FLW_SUBGROUP_SIZE) in; // need to add stubs so the instance shader compiles. vec4 flw_vertexPos; @@ -19,15 +19,15 @@ vec4 flw_var3; void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout float radius); -layout(std430, binding = OBJECT_BINDING) restrict readonly buffer ObjectBuffer { +layout(std430, binding = _FLW_OBJECT_BUFFER_BINDING) restrict readonly buffer ObjectBuffer { Object objects[]; }; -layout(std430, binding = TARGET_BINDING) restrict writeonly buffer TargetBuffer { - uint objectIDs[]; +layout(std430, binding = _FLW_TARGET_BUFFER_BINDING) restrict writeonly buffer TargetBuffer { + uint objectIndices[]; }; -layout(std430, binding = MODEL_BINDING) restrict buffer ModelDescriptors { +layout(std430, binding = _FLW_MODEL_BUFFER_BINDING) restrict buffer ModelBuffer { ModelDescriptor models[]; }; @@ -44,14 +44,14 @@ bool testSphere(vec3 center, float radius) { return all(xyInside) && all(zInside); } -bool isVisible(uint objectID, uint modelID) { - BoundingSphere sphere = models[modelID].boundingSphere; +bool isVisible(uint objectIndex, uint modelIndex) { + BoundingSphere sphere = models[modelIndex].boundingSphere; vec3 center; float radius; - unpackBoundingSphere(sphere, center, radius); + _flw_unpackBoundingSphere(sphere, center, radius); - FlwInstance instance = _flw_unpackInstance(objects[objectID].instance); + FlwInstance instance = _flw_unpackInstance(objects[objectIndex].instance); flw_transformBoundingSphere(instance, center, radius); @@ -59,18 +59,17 @@ bool isVisible(uint objectID, uint modelID) { } void main() { - uint objectID = gl_GlobalInvocationID.x; + uint objectIndex = gl_GlobalInvocationID.x; - if (objectID >= objects.length()) { + if (objectIndex >= objects.length()) { return; } - uint modelID = objects[objectID].modelID; + uint modelIndex = objects[objectIndex].modelIndex; - if (isVisible(objectID, modelID)) { - uint batchIndex = atomicAdd(models[modelID].instanceCount, 1); - uint globalIndex = models[modelID].baseInstance + batchIndex; - - objectIDs[globalIndex] = objectID; + if (isVisible(objectIndex, modelIndex)) { + uint localIndex = atomicAdd(models[modelIndex].instanceCount, 1); + uint targetIndex = models[modelIndex].baseInstance + localIndex; + objectIndices[targetIndex] = objectIndex; } } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/draw.vert b/src/main/resources/assets/flywheel/flywheel/internal/indirect/draw.vert deleted file mode 100644 index ed7d88c64..000000000 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/draw.vert +++ /dev/null @@ -1,56 +0,0 @@ -#include "flywheel:internal/indirect/api/vertex.glsl" -#include "flywheel:internal/indirect/buffers.glsl" -#include "flywheel:internal/indirect/draw_command.glsl" -#include "flywheel:internal/indirect/object.glsl" -#include "flywheel:internal/material.glsl" -#include "flywheel:internal/block.vert" -#include "flywheel:util/diffuse.glsl" - -flat out uvec3 _flw_material; - -layout(std430, binding = OBJECT_BINDING) restrict readonly buffer ObjectBuffer { - Object objects[]; -}; - -layout(std430, binding = TARGET_BINDING) restrict readonly buffer TargetBuffer { - uint objectIDs[]; -}; - -layout(std430, binding = DRAW_BINDING) restrict readonly buffer DrawCommands { - MeshDrawCommand drawCommands[]; -}; - -uniform uint _flw_baseDraw; - -void main() { - uint instanceIndex = objectIDs[gl_BaseInstance + gl_InstanceID]; - uint batchID = gl_DrawID + _flw_baseDraw; - FlwInstance i = _flw_unpackInstance(objects[instanceIndex].instance); - - _flw_materialVertexID = drawCommands[batchID].vertexMaterialID; - uint p = drawCommands[batchID].packedMaterialProperties; - - _flw_unpackMaterialProperties(p, flw_material); - _flw_material = uvec3(drawCommands[batchID].fragmentMaterialID, drawCommands[batchID].packedFogAndCutout, p); - - _flw_layoutVertex(); - flw_beginVertex(); - flw_instanceVertex(i); - flw_materialVertex(); - flw_endVertex(); - - flw_vertexNormal = normalize(flw_vertexNormal); - - if (flw_material.diffuse) { - float diffuseFactor; - if (flywheel.constantAmbientLight == 1) { - diffuseFactor = diffuseNether(flw_vertexNormal); - } else { - diffuseFactor = diffuse(flw_vertexNormal); - } - flw_vertexColor = vec4(flw_vertexColor.rgb * diffuseFactor, flw_vertexColor.a); - } - - flw_distance = fog_distance(flw_vertexPos.xyz, flywheel.cameraPos.xyz, flywheel.fogShape); - gl_Position = flywheel.viewProjection * flw_vertexPos; -} diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/draw_command.glsl b/src/main/resources/assets/flywheel/flywheel/internal/indirect/draw_command.glsl index e33b4026e..e8575930c 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/draw_command.glsl +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/draw_command.glsl @@ -5,9 +5,10 @@ struct MeshDrawCommand { uint vertexOffset; uint baseInstance; - uint modelID; - uint vertexMaterialID; - uint fragmentMaterialID; + uint modelIndex; + + uint materialVertexIndex; + uint materialFragmentIndex; uint packedFogAndCutout; uint packedMaterialProperties; }; diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/draw.frag b/src/main/resources/assets/flywheel/flywheel/internal/indirect/main.frag similarity index 71% rename from src/main/resources/assets/flywheel/flywheel/internal/indirect/draw.frag rename to src/main/resources/assets/flywheel/flywheel/internal/indirect/main.frag index a9bba7eaf..f8a8dd043 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/draw.frag +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/main.frag @@ -1,5 +1,4 @@ -#include "flywheel:internal/indirect/api/fragment.glsl" -#include "flywheel:internal/material.glsl" +#include "flywheel:internal/packed_material.glsl" // optimize discard usage #ifdef GL_ARB_conservative_depth @@ -10,15 +9,14 @@ uniform sampler2D _flw_diffuseTex; uniform sampler2D _flw_overlayTex; uniform sampler2D _flw_lightTex; -flat in uvec3 _flw_material; +flat in uvec3 _flw_packedMaterial; -out vec4 _flw_fragColor; +out vec4 _flw_outputColor; void main() { - _flw_materialFragmentID = _flw_material.x; - - _flw_unpackUint2x16(_flw_material.y, _flw_cutoutID, _flw_fogID); - _flw_unpackMaterialProperties(_flw_material.z, flw_material); + _flw_uberMaterialFragmentIndex = _flw_packedMaterial.x; + _flw_unpackUint2x16(_flw_packedMaterial.y, _flw_uberCutoutIndex, _flw_uberFogIndex); + _flw_unpackMaterialProperties(_flw_packedMaterial.z, flw_material); flw_sampleColor = texture(_flw_diffuseTex, flw_vertexTexCoord); flw_fragColor = flw_vertexColor * flw_sampleColor; @@ -45,5 +43,5 @@ void main() { discard; } - _flw_fragColor = flw_fogFilter(color); + _flw_outputColor = flw_fogFilter(color); } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/main.vert b/src/main/resources/assets/flywheel/flywheel/internal/indirect/main.vert new file mode 100644 index 000000000..41cd07a4b --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/main.vert @@ -0,0 +1,57 @@ +#include "flywheel:internal/diffuse.glsl" +#include "flywheel:internal/fog_distance.glsl" +#include "flywheel:internal/layout.vert" +#include "flywheel:internal/packed_material.glsl" +#include "flywheel:internal/indirect/buffers.glsl" +#include "flywheel:internal/indirect/draw_command.glsl" +#include "flywheel:internal/indirect/object.glsl" + +layout(std430, binding = _FLW_OBJECT_BUFFER_BINDING) restrict readonly buffer ObjectBuffer { + Object objects[]; +}; + +layout(std430, binding = _FLW_TARGET_BUFFER_BINDING) restrict readonly buffer TargetBuffer { + uint objectIndices[]; +}; + +layout(std430, binding = _FLW_DRAW_BUFFER_BINDING) restrict readonly buffer DrawBuffer { + MeshDrawCommand drawCommands[]; +}; + +uniform uint _flw_baseDraw; + +flat out uvec3 _flw_packedMaterial; + +void main() { + uint drawIndex = gl_DrawID + _flw_baseDraw; + MeshDrawCommand draw = drawCommands[drawIndex]; + + _flw_uberMaterialVertexIndex = draw.materialVertexIndex; + uint packedMaterialProperties = draw.packedMaterialProperties; + _flw_unpackMaterialProperties(packedMaterialProperties, flw_material); + _flw_packedMaterial = uvec3(draw.materialFragmentIndex, draw.packedFogAndCutout, packedMaterialProperties); + + uint objectIndex = objectIndices[gl_BaseInstance + gl_InstanceID]; + FlwInstance instance = _flw_unpackInstance(objects[objectIndex].instance); + + _flw_layoutVertex(); + flw_beginVertex(); + flw_instanceVertex(instance); + flw_materialVertex(); + flw_endVertex(); + + flw_vertexNormal = normalize(flw_vertexNormal); + + if (flw_material.diffuse) { + float diffuseFactor; + if (flywheel.constantAmbientLight == 1) { + diffuseFactor = diffuseNether(flw_vertexNormal); + } else { + diffuseFactor = diffuse(flw_vertexNormal); + } + flw_vertexColor = vec4(flw_vertexColor.rgb * diffuseFactor, flw_vertexColor.a); + } + + flw_distance = fogDistance(flw_vertexPos.xyz, flywheel.cameraPos.xyz, flywheel.fogShape); + gl_Position = flywheel.viewProjection * flw_vertexPos; +} diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/model_descriptor.glsl b/src/main/resources/assets/flywheel/flywheel/internal/indirect/model_descriptor.glsl index a24138310..a873a92a8 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/model_descriptor.glsl +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/model_descriptor.glsl @@ -5,13 +5,13 @@ struct BoundingSphere { float radius; }; -void unpackBoundingSphere(in BoundingSphere sphere, out vec3 center, out float radius) { - center = vec3(sphere.x, sphere.y, sphere.z); - radius = sphere.radius; -} - struct ModelDescriptor { uint instanceCount; uint baseInstance; BoundingSphere boundingSphere; }; + +void _flw_unpackBoundingSphere(in BoundingSphere sphere, out vec3 center, out float radius) { + center = vec3(sphere.x, sphere.y, sphere.z); + radius = sphere.radius; +} diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/object.glsl b/src/main/resources/assets/flywheel/flywheel/internal/indirect/object.glsl index 790b6377a..3a880d47f 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/object.glsl +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/object.glsl @@ -1,4 +1,4 @@ struct Object { - uint modelID; + uint modelIndex; FlwPackedInstance instance; }; diff --git a/src/main/resources/assets/flywheel/flywheel/internal/instancing/api/fragment.glsl b/src/main/resources/assets/flywheel/flywheel/internal/instancing/api/fragment.glsl deleted file mode 100644 index 77552d003..000000000 --- a/src/main/resources/assets/flywheel/flywheel/internal/instancing/api/fragment.glsl +++ /dev/null @@ -1,43 +0,0 @@ -// API -// ----------------------------------------- -#include "flywheel:api/material.glsl" - -in vec4 flw_vertexPos; -in vec4 flw_vertexColor; -in vec2 flw_vertexTexCoord; -flat in ivec2 flw_vertexOverlay; -in vec2 flw_vertexLight; -in vec3 flw_vertexNormal; - -in float flw_distance; - -in vec4 flw_var0; -in vec4 flw_var1; -in vec4 flw_var2; -in vec4 flw_var3; - -vec4 flw_sampleColor; - -vec4 flw_fragColor; -ivec2 flw_fragOverlay; -vec2 flw_fragLight; - -FlwMaterial flw_material; - -vec4 flw_fogFilter(vec4 color); - -bool flw_discardPredicate(vec4 finalColor); - -void flw_beginFragment(); -void flw_materialFragment(); -void flw_endFragment(); - -// ----------------------------------------- -// INTERNAL -// ----------------------------------------- - -uint _flw_materialFragmentID; -uint _flw_fogID; -uint _flw_cutoutID; - -// ----------------------------------------- diff --git a/src/main/resources/assets/flywheel/flywheel/internal/instancing/api/vertex.glsl b/src/main/resources/assets/flywheel/flywheel/internal/instancing/api/vertex.glsl deleted file mode 100644 index 21b367afe..000000000 --- a/src/main/resources/assets/flywheel/flywheel/internal/instancing/api/vertex.glsl +++ /dev/null @@ -1,35 +0,0 @@ -// API -// ------------------------------------ -#include "flywheel:api/material.glsl" - -out vec4 flw_vertexPos; -out vec4 flw_vertexColor; -out vec2 flw_vertexTexCoord; -flat out ivec2 flw_vertexOverlay; -out vec2 flw_vertexLight; -out vec3 flw_vertexNormal; - -out float flw_distance; - -out vec4 flw_var0; -out vec4 flw_var1; -out vec4 flw_var2; -out vec4 flw_var3; - -FlwMaterial flw_material; - -void flw_layoutVertex(); -void flw_beginVertex(); -void flw_instanceVertex(FlwInstance i); -void flw_materialVertex(); -void flw_endVertex(); - -// ------------------------------------ -// INTERNAL -// ------------------------------------ - -uint _flw_materialVertexID; - -FlwInstance _flw_unpackInstance(); - -// ------------------------------------ diff --git a/src/main/resources/assets/flywheel/flywheel/internal/instancing/api_impl.frag b/src/main/resources/assets/flywheel/flywheel/internal/instancing/api_impl.frag new file mode 100644 index 000000000..cf01e512f --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/instancing/api_impl.frag @@ -0,0 +1,5 @@ +#include "flywheel:internal/common_api_impl.frag" + +uint _flw_uberMaterialFragmentIndex; +uint _flw_uberFogIndex; +uint _flw_uberCutoutIndex; diff --git a/src/main/resources/assets/flywheel/flywheel/internal/instancing/api_impl.vert b/src/main/resources/assets/flywheel/flywheel/internal/instancing/api_impl.vert new file mode 100644 index 000000000..fafdd66f4 --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/instancing/api_impl.vert @@ -0,0 +1,3 @@ +#include "flywheel:internal/common_api_impl.vert" + +uint _flw_uberMaterialVertexIndex; diff --git a/src/main/resources/assets/flywheel/flywheel/internal/instancing/draw.frag b/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.frag similarity index 78% rename from src/main/resources/assets/flywheel/flywheel/internal/instancing/draw.frag rename to src/main/resources/assets/flywheel/flywheel/internal/instancing/main.frag index 8648e344e..6c9a0dc33 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/instancing/draw.frag +++ b/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.frag @@ -1,5 +1,4 @@ -#include "flywheel:internal/instancing/api/fragment.glsl" -#include "flywheel:internal/material.glsl" +#include "flywheel:internal/packed_material.glsl" // optimize discard usage #ifdef GL_ARB_conservative_depth @@ -12,12 +11,11 @@ uniform sampler2D _flw_lightTex; uniform uvec4 _flw_packedMaterial; -out vec4 _flw_fragColor; +out vec4 _flw_outputColor; void main() { - _flw_materialFragmentID = _flw_packedMaterial.y; - - _flw_unpackUint2x16(_flw_packedMaterial.z, _flw_cutoutID, _flw_fogID); + _flw_uberMaterialFragmentIndex = _flw_packedMaterial.y; + _flw_unpackUint2x16(_flw_packedMaterial.z, _flw_uberCutoutIndex, _flw_uberFogIndex); _flw_unpackMaterialProperties(_flw_packedMaterial.w, flw_material); flw_sampleColor = texture(_flw_diffuseTex, flw_vertexTexCoord); @@ -45,5 +43,5 @@ void main() { discard; } - _flw_fragColor = flw_fogFilter(color); + _flw_outputColor = flw_fogFilter(color); } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/instancing/draw.vert b/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.vert similarity index 62% rename from src/main/resources/assets/flywheel/flywheel/internal/instancing/draw.vert rename to src/main/resources/assets/flywheel/flywheel/internal/instancing/main.vert index 310ff1cb1..99b976622 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/instancing/draw.vert +++ b/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.vert @@ -1,20 +1,19 @@ -#include "flywheel:internal/instancing/api/vertex.glsl" -#include "flywheel:internal/material.glsl" -#include "flywheel:internal/block.vert" -#include "flywheel:util/diffuse.glsl" +#include "flywheel:internal/diffuse.glsl" +#include "flywheel:internal/fog_distance.glsl" +#include "flywheel:internal/layout.vert" +#include "flywheel:internal/packed_material.glsl" uniform uvec4 _flw_packedMaterial; void main() { - _flw_materialVertexID = _flw_packedMaterial.x; - + _flw_uberMaterialVertexIndex = _flw_packedMaterial.x; _flw_unpackMaterialProperties(_flw_packedMaterial.w, flw_material); - FlwInstance i = _flw_unpackInstance(); + FlwInstance instance = _flw_unpackInstance(); _flw_layoutVertex(); flw_beginVertex(); - flw_instanceVertex(i); + flw_instanceVertex(instance); flw_materialVertex(); flw_endVertex(); @@ -30,6 +29,6 @@ void main() { flw_vertexColor = vec4(flw_vertexColor.rgb * diffuseFactor, flw_vertexColor.a); } - flw_distance = fog_distance(flw_vertexPos.xyz, flywheel.cameraPos.xyz, flywheel.fogShape); + flw_distance = fogDistance(flw_vertexPos.xyz, flywheel.cameraPos.xyz, flywheel.fogShape); gl_Position = flywheel.viewProjection * flw_vertexPos; } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/block.vert b/src/main/resources/assets/flywheel/flywheel/internal/layout.vert similarity index 92% rename from src/main/resources/assets/flywheel/flywheel/internal/block.vert rename to src/main/resources/assets/flywheel/flywheel/internal/layout.vert index 6f276857c..fe56f5999 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/block.vert +++ b/src/main/resources/assets/flywheel/flywheel/internal/layout.vert @@ -1,5 +1,3 @@ -#include "flywheel:api/vertex.glsl" - layout(location = 0) in vec3 v_pos; layout(location = 1) in vec4 v_color; layout(location = 2) in vec2 v_texCoord; diff --git a/src/main/resources/assets/flywheel/flywheel/internal/material.glsl b/src/main/resources/assets/flywheel/flywheel/internal/material.glsl index b29166df2..40af43e2f 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/material.glsl +++ b/src/main/resources/assets/flywheel/flywheel/internal/material.glsl @@ -1,57 +1,33 @@ -#include "flywheel:api/material.glsl" +const uint FLW_MAT_DEPTH_TEST_OFF = 0u; +const uint FLW_MAT_DEPTH_TEST_NEVER = 1u; +const uint FLW_MAT_DEPTH_TEST_LESS = 2u; +const uint FLW_MAT_DEPTH_TEST_EQUAL = 3u; +const uint FLW_MAT_DEPTH_TEST_LEQUAL = 4u; +const uint FLW_MAT_DEPTH_TEST_GREATER = 5u; +const uint FLW_MAT_DEPTH_TEST_NOTEQUAL = 6u; +const uint FLW_MAT_DEPTH_TEST_GEQUAL = 7u; +const uint FLW_MAT_DEPTH_TEST_ALWAYS = 8u; -// The number of bits each property takes up -const uint _FLW_BLUR_LENGTH = 1u; -const uint _FLW_MIPMAP_LENGTH = 1u; -const uint _FLW_BACKFACE_CULLING_LENGTH = 1u; -const uint _FLW_POLYGON_OFFSET_LENGTH = 1u; -const uint _FLW_DEPTH_TEST_LENGTH = 4u; -const uint _FLW_TRANSPARENCY_LENGTH = 3u; -const uint _FLW_WRITE_MASK_LENGTH = 2u; -const uint _FLW_USE_OVERLAY_LENGTH = 1u; -const uint _FLW_USE_LIGHT_LENGTH = 1u; -const uint _FLW_DIFFUSE_LENGTH = 1u; +const uint FLW_MAT_TRANSPARENCY_OPAQUE = 0u; +const uint FLW_MAT_TRANSPARENCY_ADDITIVE = 1u; +const uint FLW_MAT_TRANSPARENCY_LIGHTNING = 2u; +const uint FLW_MAT_TRANSPARENCY_GLINT = 3u; +const uint FLW_MAT_TRANSPARENCY_CRUMBLING = 4u; +const uint FLW_MAT_TRANSPARENCY_TRANSLUCENT = 5u; -// The bit offset of each property -const uint _FLW_BLUR_OFFSET = 0u; -const uint _FLW_MIPMAP_OFFSET = _FLW_BLUR_OFFSET + _FLW_BLUR_LENGTH; -const uint _FLW_BACKFACE_CULLING_OFFSET = _FLW_MIPMAP_OFFSET + _FLW_MIPMAP_LENGTH; -const uint _FLW_POLYGON_OFFSET_OFFSET = _FLW_BACKFACE_CULLING_OFFSET + _FLW_BACKFACE_CULLING_LENGTH; -const uint _FLW_DEPTH_TEST_OFFSET = _FLW_POLYGON_OFFSET_OFFSET + _FLW_POLYGON_OFFSET_LENGTH; -const uint _FLW_TRANSPARENCY_OFFSET = _FLW_DEPTH_TEST_OFFSET + _FLW_DEPTH_TEST_LENGTH; -const uint _FLW_WRITE_MASK_OFFSET = _FLW_TRANSPARENCY_OFFSET + _FLW_TRANSPARENCY_LENGTH; -const uint _FLW_USE_OVERLAY_OFFSET = _FLW_WRITE_MASK_OFFSET + _FLW_WRITE_MASK_LENGTH; -const uint _FLW_USE_LIGHT_OFFSET = _FLW_USE_OVERLAY_OFFSET + _FLW_USE_OVERLAY_LENGTH; -const uint _FLW_DIFFUSE_OFFSET = _FLW_USE_LIGHT_OFFSET + _FLW_USE_LIGHT_LENGTH; +const uint FLW_MAT_WRITE_MASK_COLOR_DEPTH = 0u; +const uint FLW_MAT_WRITE_MASK_COLOR = 1u; +const uint FLW_MAT_WRITE_MASK_DEPTH = 2u; -// The bit mask for each property -const uint _FLW_BLUR_MASK = ((1u << _FLW_BLUR_LENGTH) - 1u) << _FLW_BLUR_OFFSET; -const uint _FLW_MIPMAP_MASK = ((1u << _FLW_MIPMAP_LENGTH) - 1u) << _FLW_MIPMAP_OFFSET; -const uint _FLW_BACKFACE_CULLING_MASK = ((1u << _FLW_BACKFACE_CULLING_LENGTH) - 1u) << _FLW_BACKFACE_CULLING_OFFSET; -const uint _FLW_POLYGON_OFFSET_MASK = ((1u << _FLW_POLYGON_OFFSET_LENGTH) - 1u) << _FLW_POLYGON_OFFSET_OFFSET; -const uint _FLW_DEPTH_TEST_MASK = ((1u << _FLW_DEPTH_TEST_LENGTH) - 1u) << _FLW_DEPTH_TEST_OFFSET; -const uint _FLW_TRANSPARENCY_MASK = ((1u << _FLW_TRANSPARENCY_LENGTH) - 1u) << _FLW_TRANSPARENCY_OFFSET; -const uint _FLW_WRITE_MASK_MASK = ((1u << _FLW_WRITE_MASK_LENGTH) - 1u) << _FLW_WRITE_MASK_OFFSET; -const uint _FLW_USE_OVERLAY_MASK = ((1u << _FLW_USE_OVERLAY_LENGTH) - 1u) << _FLW_USE_OVERLAY_OFFSET; -const uint _FLW_USE_LIGHT_MASK = ((1u << _FLW_USE_LIGHT_LENGTH) - 1u) << _FLW_USE_LIGHT_OFFSET; -const uint _FLW_DIFFUSE_MASK = ((1u << _FLW_DIFFUSE_LENGTH) - 1u) << _FLW_DIFFUSE_OFFSET; - -// Packed format: -// diffuse[1] | useOverlay[1] | useLight[1] | writeMask[2] | transparency[3] | depthTest[4] | polygonOffset[1] | backfaceCulling[1] | mipmap[1] | blur[1] -void _flw_unpackMaterialProperties(uint p, out FlwMaterial m) { - m.blur = (p & _FLW_BLUR_MASK) != 0u; - m.mipmap = (p & _FLW_MIPMAP_MASK) != 0u; - m.backfaceCulling = (p & _FLW_BACKFACE_CULLING_MASK) != 0u; - m.polygonOffset = (p & _FLW_POLYGON_OFFSET_MASK) != 0u; - m.depthTest = (p & _FLW_DEPTH_TEST_MASK) >> _FLW_DEPTH_TEST_OFFSET; - m.transparency = (p & _FLW_TRANSPARENCY_MASK) >> _FLW_TRANSPARENCY_OFFSET; - m.writeMask = (p & _FLW_WRITE_MASK_MASK) >> _FLW_WRITE_MASK_OFFSET; - m.useOverlay = (p & _FLW_USE_OVERLAY_MASK) != 0u; - m.useLight = (p & _FLW_USE_LIGHT_MASK) != 0u; - m.diffuse = (p & _FLW_DIFFUSE_MASK) != 0u; -} - -void _flw_unpackUint2x16(uint s, out uint hi, out uint lo) { - hi = (s >> 16) & 0xFFFFu; - lo = s & 0xFFFFu; -} +struct FlwMaterial { + bool blur; + bool mipmap; + bool backfaceCulling; + bool polygonOffset; + uint depthTest; + uint transparency; + uint writeMask; + bool useOverlay; + bool useLight; + bool diffuse; +}; diff --git a/src/main/resources/assets/flywheel/flywheel/internal/packed_material.glsl b/src/main/resources/assets/flywheel/flywheel/internal/packed_material.glsl new file mode 100644 index 000000000..193667a1d --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/packed_material.glsl @@ -0,0 +1,55 @@ +// The number of bits each property takes up +const uint _FLW_BLUR_LENGTH = 1u; +const uint _FLW_MIPMAP_LENGTH = 1u; +const uint _FLW_BACKFACE_CULLING_LENGTH = 1u; +const uint _FLW_POLYGON_OFFSET_LENGTH = 1u; +const uint _FLW_DEPTH_TEST_LENGTH = 4u; +const uint _FLW_TRANSPARENCY_LENGTH = 3u; +const uint _FLW_WRITE_MASK_LENGTH = 2u; +const uint _FLW_USE_OVERLAY_LENGTH = 1u; +const uint _FLW_USE_LIGHT_LENGTH = 1u; +const uint _FLW_DIFFUSE_LENGTH = 1u; + +// The bit offset of each property +const uint _FLW_BLUR_OFFSET = 0u; +const uint _FLW_MIPMAP_OFFSET = _FLW_BLUR_OFFSET + _FLW_BLUR_LENGTH; +const uint _FLW_BACKFACE_CULLING_OFFSET = _FLW_MIPMAP_OFFSET + _FLW_MIPMAP_LENGTH; +const uint _FLW_POLYGON_OFFSET_OFFSET = _FLW_BACKFACE_CULLING_OFFSET + _FLW_BACKFACE_CULLING_LENGTH; +const uint _FLW_DEPTH_TEST_OFFSET = _FLW_POLYGON_OFFSET_OFFSET + _FLW_POLYGON_OFFSET_LENGTH; +const uint _FLW_TRANSPARENCY_OFFSET = _FLW_DEPTH_TEST_OFFSET + _FLW_DEPTH_TEST_LENGTH; +const uint _FLW_WRITE_MASK_OFFSET = _FLW_TRANSPARENCY_OFFSET + _FLW_TRANSPARENCY_LENGTH; +const uint _FLW_USE_OVERLAY_OFFSET = _FLW_WRITE_MASK_OFFSET + _FLW_WRITE_MASK_LENGTH; +const uint _FLW_USE_LIGHT_OFFSET = _FLW_USE_OVERLAY_OFFSET + _FLW_USE_OVERLAY_LENGTH; +const uint _FLW_DIFFUSE_OFFSET = _FLW_USE_LIGHT_OFFSET + _FLW_USE_LIGHT_LENGTH; + +// The bit mask for each property +const uint _FLW_BLUR_MASK = ((1u << _FLW_BLUR_LENGTH) - 1u) << _FLW_BLUR_OFFSET; +const uint _FLW_MIPMAP_MASK = ((1u << _FLW_MIPMAP_LENGTH) - 1u) << _FLW_MIPMAP_OFFSET; +const uint _FLW_BACKFACE_CULLING_MASK = ((1u << _FLW_BACKFACE_CULLING_LENGTH) - 1u) << _FLW_BACKFACE_CULLING_OFFSET; +const uint _FLW_POLYGON_OFFSET_MASK = ((1u << _FLW_POLYGON_OFFSET_LENGTH) - 1u) << _FLW_POLYGON_OFFSET_OFFSET; +const uint _FLW_DEPTH_TEST_MASK = ((1u << _FLW_DEPTH_TEST_LENGTH) - 1u) << _FLW_DEPTH_TEST_OFFSET; +const uint _FLW_TRANSPARENCY_MASK = ((1u << _FLW_TRANSPARENCY_LENGTH) - 1u) << _FLW_TRANSPARENCY_OFFSET; +const uint _FLW_WRITE_MASK_MASK = ((1u << _FLW_WRITE_MASK_LENGTH) - 1u) << _FLW_WRITE_MASK_OFFSET; +const uint _FLW_USE_OVERLAY_MASK = ((1u << _FLW_USE_OVERLAY_LENGTH) - 1u) << _FLW_USE_OVERLAY_OFFSET; +const uint _FLW_USE_LIGHT_MASK = ((1u << _FLW_USE_LIGHT_LENGTH) - 1u) << _FLW_USE_LIGHT_OFFSET; +const uint _FLW_DIFFUSE_MASK = ((1u << _FLW_DIFFUSE_LENGTH) - 1u) << _FLW_DIFFUSE_OFFSET; + +// Packed format: +// diffuse[1] | useOverlay[1] | useLight[1] | writeMask[2] | transparency[3] | depthTest[4] | polygonOffset[1] | backfaceCulling[1] | mipmap[1] | blur[1] +void _flw_unpackMaterialProperties(uint p, out FlwMaterial m) { + m.blur = (p & _FLW_BLUR_MASK) != 0u; + m.mipmap = (p & _FLW_MIPMAP_MASK) != 0u; + m.backfaceCulling = (p & _FLW_BACKFACE_CULLING_MASK) != 0u; + m.polygonOffset = (p & _FLW_POLYGON_OFFSET_MASK) != 0u; + m.depthTest = (p & _FLW_DEPTH_TEST_MASK) >> _FLW_DEPTH_TEST_OFFSET; + m.transparency = (p & _FLW_TRANSPARENCY_MASK) >> _FLW_TRANSPARENCY_OFFSET; + m.writeMask = (p & _FLW_WRITE_MASK_MASK) >> _FLW_WRITE_MASK_OFFSET; + m.useOverlay = (p & _FLW_USE_OVERLAY_MASK) != 0u; + m.useLight = (p & _FLW_USE_LIGHT_MASK) != 0u; + m.diffuse = (p & _FLW_DIFFUSE_MASK) != 0u; +} + +void _flw_unpackUint2x16(uint s, out uint hi, out uint lo) { + hi = (s >> 16) & 0xFFFFu; + lo = s & 0xFFFFu; +} diff --git a/src/main/resources/assets/flywheel/flywheel/material/default.frag b/src/main/resources/assets/flywheel/flywheel/material/default.frag index 4a1a35464..68dfe9a5b 100644 --- a/src/main/resources/assets/flywheel/flywheel/material/default.frag +++ b/src/main/resources/assets/flywheel/flywheel/material/default.frag @@ -1,4 +1,2 @@ -#include "flywheel:api/fragment.glsl" - void flw_materialFragment() { } diff --git a/src/main/resources/assets/flywheel/flywheel/material/default.vert b/src/main/resources/assets/flywheel/flywheel/material/default.vert index c067c086c..5d92e7ffb 100644 --- a/src/main/resources/assets/flywheel/flywheel/material/default.vert +++ b/src/main/resources/assets/flywheel/flywheel/material/default.vert @@ -1,4 +1,2 @@ -#include "flywheel:api/vertex.glsl" - void flw_materialVertex() { } diff --git a/src/main/resources/assets/flywheel/flywheel/uniform/flywheel.glsl b/src/main/resources/assets/flywheel/flywheel/uniform/flywheel.glsl index 0032a6388..70ff97abd 100644 --- a/src/main/resources/assets/flywheel/flywheel/uniform/flywheel.glsl +++ b/src/main/resources/assets/flywheel/flywheel/uniform/flywheel.glsl @@ -1,4 +1,4 @@ -struct FLWPackedPlanes { +struct FrustumPlanes { vec4 xyX; // vec4 xyY; // vec4 xyZ; // @@ -9,12 +9,12 @@ struct FLWPackedPlanes { vec2 zW; // }; -struct flywheel_uniforms { +struct FlywheelUniforms { vec4 fogColor; vec2 fogRange; int fogShape; mat4 viewProjection; vec4 cameraPos; int constantAmbientLight; - FLWPackedPlanes planes; + FrustumPlanes planes; }; diff --git a/src/main/resources/assets/flywheel/flywheel/util/fog.glsl b/src/main/resources/assets/flywheel/flywheel/util/fog.glsl deleted file mode 100644 index a9c8d043d..000000000 --- a/src/main/resources/assets/flywheel/flywheel/util/fog.glsl +++ /dev/null @@ -1,40 +0,0 @@ -float spherical_distance(vec3 relativePos) { - return length(relativePos); -} - -float cylindrical_distance(vec3 relativePos) { - float distXZ = length(relativePos.xz); - float distY = abs(relativePos.y); - return max(distXZ, distY); -} - -float fog_distance(vec3 relativePos, int fogShape) { - if (fogShape == 0) { - return spherical_distance(relativePos); - } else { - return cylindrical_distance(relativePos); - } -} - -float fog_distance(vec3 worldPos, vec3 cameraPos, int fogShape) { - return fog_distance(worldPos - cameraPos, fogShape); -} - -vec4 linear_fog(vec4 color, float distance, float fogStart, float fogEnd, vec4 fogColor) { - if (distance <= fogStart) { - return color; - } - - float fogValue = distance < fogEnd ? smoothstep(fogStart, fogEnd, distance) : 1.0; - return vec4(mix(color.rgb, fogColor.rgb, fogValue * fogColor.a), color.a); -} - -vec4 linear_fog_fade(vec4 color, float distance, float fogStart, float fogEnd) { - if (distance <= fogStart) { - return color; - } else if (distance >= fogEnd) { - return vec4(0.0); - } - - return color * smoothstep(fogEnd, fogStart, distance); -} diff --git a/src/main/resources/assets/flywheel/flywheel/util/quaternion.glsl b/src/main/resources/assets/flywheel/flywheel/util/quaternion.glsl index fc6f16eae..8db184956 100644 --- a/src/main/resources/assets/flywheel/flywheel/util/quaternion.glsl +++ b/src/main/resources/assets/flywheel/flywheel/util/quaternion.glsl @@ -1,8 +1,8 @@ -#define PIOVER2 1.5707963268 +#define PI_OVER_2 1.5707963268 vec4 quat(vec3 axis, float angle) { - float halfAngle = angle * PIOVER2 / 180.0; - vec2 cs = sin(vec2(PIOVER2 - halfAngle, halfAngle)); // compute sin and cos in one instruction + float halfAngle = angle * PI_OVER_2 / 180.0; + vec2 cs = sin(vec2(PI_OVER_2 - halfAngle, halfAngle)); // compute sin and cos in one instruction return vec4(axis.xyz * cs.y, cs.x); }