Fragments of utility

- Give material fragment shaders a use
  - Add new variables to api/fragment.glsl
  - Add flw_initFragment to context shaders
- Add flw_vertexOverlay
- Normalize flw_vertexLight
  - Values are now [0, 1] where 0 is no light and 1 is max light
  - All light values sent to layout shaders and instance shaders are now
[0, 15] and not normalized
  - Remove util/light.glsl
- Rename program samplers
- Add FileResolution.isWeak()
- Add BackendType.getShortName()
- Tweak default material
- Move ShaderField to parse package
- Organize imports
This commit is contained in:
PepperCode1 2022-07-23 17:53:57 -07:00
parent e43261302a
commit d0c6669a49
38 changed files with 88 additions and 66 deletions

View File

@ -11,7 +11,12 @@ import com.jozufozu.flywheel.backend.model.MeshPool;
import com.jozufozu.flywheel.config.BackendTypeArgument;
import com.jozufozu.flywheel.config.FlwCommands;
import com.jozufozu.flywheel.config.FlwConfig;
import com.jozufozu.flywheel.core.*;
import com.jozufozu.flywheel.core.Components;
import com.jozufozu.flywheel.core.GameStateRegistry;
import com.jozufozu.flywheel.core.Models;
import com.jozufozu.flywheel.core.PartialModel;
import com.jozufozu.flywheel.core.QuadConverter;
import com.jozufozu.flywheel.core.StitchedSprite;
import com.jozufozu.flywheel.core.compile.ProgramCompiler;
import com.jozufozu.flywheel.core.crumbling.CrumblingRenderer;
import com.jozufozu.flywheel.core.shader.NormalDebugStateProvider;

View File

@ -2,7 +2,6 @@ package com.jozufozu.flywheel.api.material;
import com.jozufozu.flywheel.api.RenderStage;
import com.jozufozu.flywheel.core.source.FileResolution;
import com.jozufozu.flywheel.core.source.SourceFile;
import net.minecraft.client.renderer.RenderType;

View File

@ -1,5 +1,6 @@
package com.jozufozu.flywheel.backend.gl;
// TODO: support glVertexAttribIPointer
/**
* A bindable attribute in a vertex array.
*

View File

@ -14,7 +14,6 @@ import com.jozufozu.flywheel.util.WorldAttached;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.Vec3i;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.LevelAccessor;

View File

@ -8,18 +8,13 @@ import java.util.Map;
import org.jetbrains.annotations.NotNull;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import com.jozufozu.flywheel.api.RenderStage;
import com.jozufozu.flywheel.api.instancer.InstancedPart;
import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.backend.gl.GlVertexArray;
import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer;
import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType;
import com.jozufozu.flywheel.backend.instancing.Engine;
import com.jozufozu.flywheel.backend.instancing.InstanceManager;
import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher;
import com.jozufozu.flywheel.backend.instancing.TaskEngine;
import com.jozufozu.flywheel.backend.model.MeshPool;
import com.jozufozu.flywheel.core.CoreShaderInfoMap.CoreShaderInfo;

View File

@ -26,20 +26,26 @@ public enum BackendType {
static {
lookup = new HashMap<>();
for (BackendType value : values()) {
lookup.put(value.name().toLowerCase(Locale.ROOT), value);
lookup.put(value.getShortName(), value);
}
}
private final String properName;
private final String shortName;
BackendType(String properName) {
this.properName = properName;
shortName = name().toLowerCase(Locale.ROOT);
}
public String getProperName() {
return properName;
}
public String getShortName() {
return shortName;
}
@Nullable
public static BackendType byName(String name) {
return lookup.get(name);

View File

@ -12,7 +12,6 @@ import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.api.uniform.UniformProvider;
import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.core.compile.ContextShader;
import com.jozufozu.flywheel.core.vertex.BlockVertex;
import net.minecraft.resources.ResourceLocation;

View File

@ -95,7 +95,7 @@ public class Components {
public static final BiConsumer<ErrorReporter, SourceFile> MATERIAL_VERTEX = SourceChecks.checkFunctionArity("flw_materialVertex", 0);
public static final BiConsumer<ErrorReporter, SourceFile> MATERIAL_FRAGMENT = SourceChecks.checkFunctionArity("flw_materialFragment", 0);
public static final BiConsumer<ErrorReporter, SourceFile> CONTEXT_VERTEX = SourceChecks.checkFunctionArity("flw_contextVertex", 0);
public static final BiConsumer<ErrorReporter, SourceFile> CONTEXT_FRAGMENT = SourceChecks.checkFunctionArity("flw_contextFragment", 0);
public static final BiConsumer<ErrorReporter, SourceFile> CONTEXT_FRAGMENT = SourceChecks.checkFunctionArity("flw_contextFragment", 0).andThen(SourceChecks.checkFunctionArity("flw_initFragment", 0));
}
public static class Names {

View File

@ -10,7 +10,7 @@ import net.minecraft.resources.ResourceLocation;
public class Materials {
private static final ResourceLocation MINECART_LOCATION = new ResourceLocation("textures/entity/minecart.png");
public static final Material DEFAULT = ComponentRegistry.register(new SimpleMaterial(RenderStage.AFTER_BLOCK_ENTITIES, RenderType.solid(), Components.Files.DEFAULT_VERTEX, Components.Files.DEFAULT_FRAGMENT));
public static final Material DEFAULT = ComponentRegistry.register(new SimpleMaterial(RenderStage.AFTER_SOLID_TERRAIN, RenderType.cutout(), Components.Files.SHADED_VERTEX, Components.Files.DEFAULT_FRAGMENT));
public static final Material CHEST = ComponentRegistry.register(new SimpleMaterial(RenderStage.AFTER_BLOCK_ENTITIES, Sheets.chestSheet(), Components.Files.SHADED_VERTEX, Components.Files.DEFAULT_FRAGMENT));
public static final Material SHULKER = ComponentRegistry.register(new SimpleMaterial(RenderStage.AFTER_BLOCK_ENTITIES, RenderType.entityCutoutNoCull(Sheets.SHULKER_SHEET), Components.Files.SHADED_VERTEX, Components.Files.DEFAULT_FRAGMENT));
public static final Material BELL = ComponentRegistry.register(new SimpleMaterial(RenderStage.AFTER_BLOCK_ENTITIES, Sheets.solidBlockSheet(), Components.Files.SHADED_VERTEX, Components.Files.DEFAULT_FRAGMENT));

View File

@ -54,9 +54,11 @@ public class FragmentCompiler extends Memoizer<FragmentCompiler.Context, GlShade
protected String generateFooter() {
return """
void main() {
flw_initFragment();
flw_materialFragment();
flw_contextFragment();
flw_contextFragment();
}
""";
}

View File

@ -1,8 +1,5 @@
package com.jozufozu.flywheel.core.compile;
import java.util.ArrayList;
import java.util.List;
import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.backend.gl.shader.GlProgram;

View File

@ -9,8 +9,8 @@ import com.jozufozu.flywheel.backend.gl.shader.GlShader;
import com.jozufozu.flywheel.backend.gl.shader.ShaderType;
import com.jozufozu.flywheel.core.shader.StateSnapshot;
import com.jozufozu.flywheel.core.source.CompilationContext;
import com.jozufozu.flywheel.core.source.ShaderField;
import com.jozufozu.flywheel.core.source.SourceFile;
import com.jozufozu.flywheel.core.source.parse.ShaderField;
import com.jozufozu.flywheel.core.source.span.Span;
import com.jozufozu.flywheel.util.Pair;

View File

@ -5,14 +5,12 @@ import com.jozufozu.flywheel.core.shader.WorldProgram;
import net.minecraft.resources.ResourceLocation;
public class CrumblingProgram extends WorldProgram {
protected int uCrumblingTex;
public CrumblingProgram(ResourceLocation name, int handle) {
super(name, handle);
}
@Override
protected void registerSamplers() {
uCrumblingTex = setSamplerBinding("uCrumblingTex", 0);
diffuseTex = setSamplerBinding("flw_diffuseTex", 0);
}
}

View File

@ -17,7 +17,6 @@ import com.jozufozu.flywheel.core.RenderContext;
import com.jozufozu.flywheel.event.ReloadRenderersEvent;
import com.jozufozu.flywheel.mixin.LevelRendererAccessor;
import com.jozufozu.flywheel.util.Lazy;
import com.mojang.math.Matrix4f;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;

View File

@ -15,8 +15,8 @@ public class CommonItems {
public static final PrimitiveItem RGBA = new PrimitiveItem(GlNumericType.UBYTE, 4, true);
public static final PrimitiveItem RGB = new PrimitiveItem(GlNumericType.UBYTE, 3, true);
public static final PrimitiveItem LIGHT = new PrimitiveItem(GlNumericType.UBYTE, 2, true);
public static final PrimitiveItem LIGHT_SHORT = new PrimitiveItem(GlNumericType.USHORT, 2, true);
public static final PrimitiveItem LIGHT = new PrimitiveItem(GlNumericType.UBYTE, 2, false);
public static final PrimitiveItem LIGHT_SHORT = new PrimitiveItem(GlNumericType.USHORT, 2, false);
public static final PrimitiveItem NORMALIZED_BYTE = new PrimitiveItem(GlNumericType.BYTE, 1, true);

View File

@ -7,8 +7,9 @@ import net.minecraft.resources.ResourceLocation;
public class WorldProgram extends GlProgram {
// TODO: sampler registry?
protected int uBlockAtlas;
protected int uLightMap;
protected int diffuseTex;
protected int overlayTex;
protected int lightTex;
public WorldProgram(ResourceLocation name, int handle) {
super(name, handle);
@ -19,7 +20,8 @@ public class WorldProgram extends GlProgram {
}
protected void registerSamplers() {
uBlockAtlas = setSamplerBinding("uBlockAtlas", 0);
uLightMap = setSamplerBinding("uLightMap", 2);
diffuseTex = setSamplerBinding("flw_diffuseTex", 0);
overlayTex = setSamplerBinding("flw_overlayTex", 1);
lightTex = setSamplerBinding("flw_lightTex", 2);
}
}

View File

@ -34,16 +34,18 @@ public class FileResolution {
private final List<BiConsumer<ErrorReporter, SourceFile>> checks = new ArrayList<>();
private final ResourceLocation fileLoc;
private final boolean weak;
private SourceFile file;
FileResolution(ResourceLocation fileLoc) {
private FileResolution(ResourceLocation fileLoc, boolean weak) {
this.fileLoc = fileLoc;
this.weak = weak;
}
public static FileResolution get(ResourceLocation file) {
if (!tooLate) {
return ALL.computeIfAbsent(file, FileResolution::new);
return ALL.computeIfAbsent(file, loc -> new FileResolution(loc, false));
} else {
// Lock the map after resolution has run.
FileResolution fileResolution = ALL.get(file);
@ -71,7 +73,7 @@ public class FileResolution {
return fileResolution;
}
// never too late for weak resolutions.
return WEAK.computeIfAbsent(file, FileResolution::new);
return WEAK.computeIfAbsent(file, loc -> new FileResolution(loc, true));
}
/**
@ -134,6 +136,10 @@ public class FileResolution {
return file;
}
public boolean isWeak() {
return weak;
}
/**
* Store the given span so this resolution can know all the places that reference the file.
*

View File

@ -15,6 +15,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.jozufozu.flywheel.core.source.error.ErrorReporter;
import com.jozufozu.flywheel.core.source.parse.Import;
import com.jozufozu.flywheel.core.source.parse.ShaderField;
import com.jozufozu.flywheel.core.source.parse.ShaderFunction;
import com.jozufozu.flywheel.core.source.parse.ShaderStruct;
import com.jozufozu.flywheel.core.source.span.ErrorSpan;

View File

@ -1,10 +1,9 @@
package com.jozufozu.flywheel.core.source;
package com.jozufozu.flywheel.core.source.parse;
import java.util.regex.Pattern;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.core.source.parse.AbstractShaderElement;
import com.jozufozu.flywheel.core.source.span.Span;
public class ShaderField extends AbstractShaderElement {

View File

@ -16,8 +16,8 @@ public abstract class ColoredLitWriterUnsafe<D extends ColoredLitPart> extends U
@Override
protected void writeInternal(D d) {
long ptr = writePointer;
MemoryUtil.memPutByte(ptr, (byte) (d.blockLight << 4));
MemoryUtil.memPutByte(ptr + 1, (byte) (d.skyLight << 4));
MemoryUtil.memPutByte(ptr, d.blockLight);
MemoryUtil.memPutByte(ptr + 1, d.skyLight);
MemoryUtil.memPutByte(ptr + 2, d.r);
MemoryUtil.memPutByte(ptr + 3, d.g);
MemoryUtil.memPutByte(ptr + 4, d.b);

View File

@ -108,4 +108,3 @@ public class OrientedPart extends ColoredLitPart {
return out;
}
}

View File

@ -1,7 +1,5 @@
package com.jozufozu.flywheel.core.uniform;
import java.nio.ByteBuffer;
import org.lwjgl.system.MemoryUtil;
import com.jozufozu.flywheel.api.uniform.UniformProvider;

View File

@ -4,7 +4,6 @@ import java.nio.ByteBuffer;
import java.util.BitSet;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicLongArray;
import org.lwjgl.opengl.GL32;
import org.lwjgl.system.MemoryUtil;

View File

@ -1,7 +1,5 @@
package com.jozufozu.flywheel.core.uniform;
import java.nio.ByteBuffer;
import org.lwjgl.system.MemoryUtil;
import com.jozufozu.flywheel.api.uniform.UniformProvider;

View File

@ -46,7 +46,7 @@ public class BlockWriterUnsafe extends VertexWriterUnsafe<BlockVertex> {
MemoryUtil.memPutByte(ptr + 15, a);
MemoryUtil.memPutFloat(ptr + 16, u);
MemoryUtil.memPutFloat(ptr + 20, v);
MemoryUtil.memPutInt(ptr + 24, light << 8); // light is packed in the low byte of each short
MemoryUtil.memPutInt(ptr + 24, (light >> 4) & 0xF000F);
MemoryUtil.memPutByte(ptr + 28, RenderMath.nb(nX));
MemoryUtil.memPutByte(ptr + 29, RenderMath.nb(nY));
MemoryUtil.memPutByte(ptr + 30, RenderMath.nb(nZ));

View File

@ -2,7 +2,6 @@ package com.jozufozu.flywheel.util;
import java.nio.ByteBuffer;
import com.mojang.math.Matrix3f;
import com.mojang.math.Matrix4f;
/**

View File

@ -7,7 +7,6 @@ import java.util.function.BiFunction;
import org.jetbrains.annotations.NotNull;
import com.jozufozu.flywheel.api.RenderStage;
import com.jozufozu.flywheel.api.instance.DynamicInstance;
import com.jozufozu.flywheel.api.instancer.InstancedPart;
import com.jozufozu.flywheel.api.instancer.InstancerManager;

View File

@ -2,7 +2,6 @@ package com.jozufozu.flywheel.vanilla;
import org.jetbrains.annotations.NotNull;
import com.jozufozu.flywheel.api.RenderStage;
import com.jozufozu.flywheel.api.instance.DynamicInstance;
import com.jozufozu.flywheel.api.instance.TickableInstance;
import com.jozufozu.flywheel.api.instancer.InstancerManager;

View File

@ -1,6 +1,7 @@
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;
@ -10,3 +11,11 @@ in vec4 flw_var0;
in vec4 flw_var1;
in vec4 flw_var2;
in vec4 flw_var3;
//
/*const*/ vec4 flw_sampleColor;
vec4 flw_fragColor;
ivec2 flw_fragOverlay;
vec2 flw_fragLight;

View File

@ -1,6 +1,7 @@
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;

View File

@ -2,7 +2,7 @@
#use "flywheel:util/fog.glsl"
#use "flywheel:uniform/fog.glsl"
uniform sampler2D uCrumblingTex;
uniform sampler2D flw_diffuseTex;
out vec4 fragColor;
@ -25,8 +25,16 @@ vec2 flattenedPos(vec3 pos, vec3 normal) {
return (transpose(tbn) * pos).xy + vec2(0.5);
}
void flw_initFragment() {
flw_sampleColor = texture(flw_diffuseTex, flattenedPos(flw_vertexPos.xyz, flw_vertexNormal));
// Crumbling ignores vertex colors
flw_fragColor = flw_sampleColor;
flw_fragOverlay = flw_vertexOverlay;
flw_fragLight = flw_vertexLight;
}
void flw_contextFragment() {
vec4 color = texture(uCrumblingTex, flattenedPos(flw_vertexPos.xyz, flw_vertexNormal));
vec4 color = flw_fragColor;
#ifdef ALPHA_DISCARD
if (color.a < ALPHA_DISCARD) {

View File

@ -9,15 +9,26 @@ layout (depth_greater) out float gl_FragDepth;
#endif
#endif
uniform sampler2D uBlockAtlas;
uniform sampler2D uLightMap;
uniform sampler2D flw_diffuseTex;
uniform sampler2D flw_overlayTex;
uniform sampler2D flw_lightTex;
out vec4 fragColor;
void flw_initFragment() {
flw_sampleColor = texture(flw_diffuseTex, flw_vertexTexCoord);
flw_fragColor = flw_vertexColor * flw_sampleColor;
flw_fragOverlay = flw_vertexOverlay;
flw_fragLight = flw_vertexLight;
}
void flw_contextFragment() {
vec4 texColor = texture(uBlockAtlas, flw_vertexTexCoord);
vec4 lightColor = texture(uLightMap, flw_vertexLight);
vec4 color = flw_vertexColor * vec4(texColor.rgb * lightColor.rgb, texColor.a);
vec4 overlayColor = texelFetch(flw_overlayTex, flw_fragOverlay, 0);
vec4 lightColor = texture(flw_lightTex, (flw_fragLight * 15.0 + 0.5) / 16.0);
vec4 color = flw_fragColor;
color.rgb = mix(overlayColor.rgb, color.rgb, overlayColor.a);
color *= lightColor;
#ifdef ALPHA_DISCARD
if (color.a < ALPHA_DISCARD) {

View File

@ -1,8 +1,7 @@
#use "flywheel:api/vertex.glsl"
#use "flywheel:util/light.glsl"
#use "flywheel:util/quaternion.glsl"
layout(location = 0) in vec2 oriented_light;
layout(location = 0) in vec2 oriented_light; // TODO: switch to ivec2
layout(location = 1) in vec4 oriented_color;
layout(location = 2) in vec3 oriented_pos;
layout(location = 3) in vec3 oriented_pivot;
@ -12,5 +11,5 @@ void flw_instanceVertex() {
flw_vertexPos = vec4(rotateVertexByQuat(flw_vertexPos.xyz - oriented_pivot, oriented_rotation) + oriented_pivot + oriented_pos, 1.0);
flw_vertexNormal = rotateVertexByQuat(flw_vertexNormal, oriented_rotation);
flw_vertexColor = oriented_color;
flw_vertexLight = shiftLight(oriented_light);
flw_vertexLight = oriented_light / 15.0;
}

View File

@ -1,7 +1,6 @@
#use "flywheel:api/vertex.glsl"
#use "flywheel:util/light.glsl"
layout(location = 0) in vec2 transformed_light;
layout(location = 0) in vec2 transformed_light; // TODO: switch to ivec2
layout(location = 1) in vec4 transformed_color;
layout(location = 2) in mat4 transformed_pose;
layout(location = 6) in mat3 transformed_normal;
@ -10,5 +9,5 @@ void flw_instanceVertex() {
flw_vertexPos = transformed_pose * flw_vertexPos;
flw_vertexNormal = transformed_normal * flw_vertexNormal;
flw_vertexColor = transformed_color;
flw_vertexLight = shiftLight(transformed_light);
flw_vertexLight = transformed_light / 15.0;
}

View File

@ -1,16 +1,16 @@
#use "flywheel:api/vertex.glsl"
#use "flywheel:util/light.glsl"
layout(location = 0) in vec3 _flw_v_pos;
layout(location = 1) in vec4 _flw_v_color;
layout(location = 2) in vec2 _flw_v_texCoord;
layout(location = 3) in vec2 _flw_v_light;
layout(location = 3) in vec2 _flw_v_light; // TODO: switch to ivec2
layout(location = 4) in vec3 _flw_v_normal;
void flw_layoutVertex() {
flw_vertexPos = vec4(_flw_v_pos, 1.0);
flw_vertexColor = _flw_v_color;
flw_vertexTexCoord = _flw_v_texCoord;
flw_vertexLight = shiftLight(_flw_v_light);
flw_vertexOverlay = ivec2(0, 10);
flw_vertexLight = _flw_v_light / 15.0;
flw_vertexNormal = _flw_v_normal;
}

View File

@ -8,6 +8,7 @@ void flw_layoutVertex() {
flw_vertexPos = vec4(_flw_v_pos, 1.0);
flw_vertexColor = vec4(1.0);
flw_vertexTexCoord = _flw_v_texCoord;
flw_vertexLight = vec2(0.0);
flw_vertexOverlay = ivec2(0, 10);
flw_vertexLight = vec2(1.0);
flw_vertexNormal = _flw_v_normal;
}

View File

@ -1,4 +1,3 @@
layout(std140, binding = 0) uniform flw_view {
mat4 flw_viewProjection;
vec4 flw_cameraPos;

View File

@ -1,4 +0,0 @@
// Adjust the [0,1] normalized lightmap value based on the texture matrix from LightTexture#enableLightmap
vec2 shiftLight(vec2 lm) {
return lm * 0.99609375 + 0.03125; // * 255/256 + 1/32
}