Debugging broken overlay/lighting

- Break instance overlay/light back into 2 layout elements
- Write light directly as an int
- Make light a short backed float element
- Clamp overlay coords in common.frag to avoid explosions
- Explosions: integer vertex attributes sometimes blow up to massive
  sizes, greater than Short.MAX_VALUE which should not be possible.
- Reorganize some texture/uniform binding code
This commit is contained in:
Jozufozu 2024-02-21 12:37:46 -06:00
parent df83277692
commit 68daac8659
8 changed files with 33 additions and 24 deletions

View File

@ -4,9 +4,9 @@ import java.util.List;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.layout.FloatRepr;
import com.jozufozu.flywheel.api.layout.IntegerRepr;
import com.jozufozu.flywheel.api.layout.Layout;
import com.jozufozu.flywheel.api.layout.LayoutBuilder;
import com.jozufozu.flywheel.api.layout.UnsignedIntegerRepr;
import com.jozufozu.flywheel.api.vertex.VertexView;
import com.jozufozu.flywheel.backend.engine.LayoutAttributes;
import com.jozufozu.flywheel.backend.gl.array.VertexAttribute;
@ -19,7 +19,7 @@ public final class InternalVertex {
.vector("position", FloatRepr.FLOAT, 3)
.vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4)
.vector("tex", FloatRepr.FLOAT, 2)
.vector("overlay_light", UnsignedIntegerRepr.UNSIGNED_SHORT, 4)
.vector("overlay_light", IntegerRepr.SHORT, 4)
.vector("normal", FloatRepr.NORMALIZED_BYTE, 3)
.build();

View File

@ -207,12 +207,13 @@ public class MeshPool {
}
public void setup(GlVertexArray vao) {
if (boundTo.add(vao)) {
if (!boundTo.add(vao)) {
return;
}
MeshPool.this.indexPool.bind(vao);
vao.bindVertexBuffer(0, MeshPool.this.vbo.handle(), byteIndex, InternalVertex.STRIDE);
vao.bindAttributes(0, 0, InternalVertex.ATTRIBUTES);
}
}
public void acquire() {
referenceCount++;

View File

@ -64,6 +64,7 @@ public class InstancingEngine extends AbstractEngine {
}
try (var state = GlStateTracker.getRestoreState()) {
Uniforms.bindForDraw();
render(drawSet);
}
}
@ -104,7 +105,6 @@ public class InstancingEngine extends AbstractEngine {
var program = programs.get(shader.instanceType(), context.contextShader());
program.bind();
Uniforms.bindForDraw();
uploadMaterialUniform(program, material);
context.prepare(material, program, textures);

View File

@ -48,14 +48,15 @@ public class TextureBinder {
public static void bindLightAndOverlay() {
var gameRenderer = Minecraft.getInstance().gameRenderer;
gameRenderer.overlayTexture()
.setupOverlayColor();
gameRenderer.lightTexture()
.turnOnLightLayer();
GlTextureUnit.T1.makeActive();
gameRenderer.overlayTexture()
.setupOverlayColor();
RenderSystem.bindTexture(RenderSystem.getShaderTexture(1));
GlTextureUnit.T2.makeActive();
gameRenderer.lightTexture()
.turnOnLightLayer();
RenderSystem.bindTexture(RenderSystem.getShaderTexture(2));
}

View File

@ -14,7 +14,8 @@ public final class InstanceTypes {
public static final InstanceType<TransformedInstance> TRANSFORMED = SimpleInstanceType.builder(TransformedInstance::new)
.layout(LayoutBuilder.create()
.vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4)
.vector("overlay_light", IntegerRepr.SHORT, 4)
.vector("overlay", IntegerRepr.SHORT, 2)
.vector("light", FloatRepr.UNSIGNED_SHORT, 2)
.matrix("pose", FloatRepr.FLOAT, 4)
.matrix("normal", FloatRepr.FLOAT, 3)
.build())
@ -24,8 +25,7 @@ public final class InstanceTypes {
MemoryUtil.memPutByte(ptr + 2, instance.b);
MemoryUtil.memPutByte(ptr + 3, instance.a);
MemoryUtil.memPutInt(ptr + 4, instance.overlay);
MemoryUtil.memPutShort(ptr + 8, instance.blockLight);
MemoryUtil.memPutShort(ptr + 10, instance.skyLight);
MemoryUtil.memPutInt(ptr + 8, (int) instance.blockLight | (int) instance.skyLight << 16);
MatrixMath.writeUnsafe(instance.model, ptr + 12);
MatrixMath.writeUnsafe(instance.normal, ptr + 76);
})
@ -36,7 +36,8 @@ public final class InstanceTypes {
public static final InstanceType<OrientedInstance> ORIENTED = SimpleInstanceType.builder(OrientedInstance::new)
.layout(LayoutBuilder.create()
.vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4)
.vector("overlay_light", IntegerRepr.SHORT, 4)
.vector("overlay", IntegerRepr.SHORT, 2)
.vector("light", FloatRepr.UNSIGNED_SHORT, 2)
.vector("position", FloatRepr.FLOAT, 3)
.vector("pivot", FloatRepr.FLOAT, 3)
.vector("rotation", FloatRepr.FLOAT, 4)
@ -47,8 +48,7 @@ public final class InstanceTypes {
MemoryUtil.memPutByte(ptr + 2, instance.b);
MemoryUtil.memPutByte(ptr + 3, instance.a);
MemoryUtil.memPutInt(ptr + 4, instance.overlay);
MemoryUtil.memPutShort(ptr + 8, instance.blockLight);
MemoryUtil.memPutShort(ptr + 10, instance.skyLight);
MemoryUtil.memPutInt(ptr + 8, (int) instance.blockLight | (int) instance.skyLight << 16);
MemoryUtil.memPutFloat(ptr + 12, instance.posX);
MemoryUtil.memPutFloat(ptr + 16, instance.posY);
MemoryUtil.memPutFloat(ptr + 20, instance.posZ);

View File

@ -4,6 +4,6 @@ void flw_instanceVertex(in FlwInstance i) {
flw_vertexPos = vec4(rotateByQuaternion(flw_vertexPos.xyz - i.pivot, i.rotation) + i.pivot + i.position, 1.0);
flw_vertexNormal = rotateByQuaternion(flw_vertexNormal, i.rotation);
flw_vertexColor = i.color;
flw_vertexOverlay = i.overlay_light.xy;
flw_vertexLight = i.overlay_light.zw / 15.0;
flw_vertexOverlay = i.overlay;
flw_vertexLight = i.light / 15.;
}

View File

@ -2,6 +2,6 @@ void flw_instanceVertex(in FlwInstance i) {
flw_vertexPos = i.pose * flw_vertexPos;
flw_vertexNormal = i.normal * flw_vertexNormal;
flw_vertexColor = i.color;
flw_vertexOverlay = i.overlay_light.xy;
flw_vertexLight = i.overlay_light.zw / 15.0;
flw_vertexOverlay = i.overlay;
flw_vertexLight = i.light / 15.;
}

View File

@ -37,7 +37,14 @@ void _flw_main() {
}
if (flw_material.useOverlay) {
vec4 overlayColor = texelFetch(_flw_overlayTex, flw_fragOverlay, 0);
// Need to clamp the overlay texture coords to sane coordinates because integer vertex attributes explode on
// some drivers for some draw calls.
// This can be removed once instancing uses sampler buffers, though
// we may need a solution for the internal vertex format. Perhaps
// pass as floats and convert to integers in the shader?
ivec2 actualCoord = clamp(flw_fragOverlay, ivec2(3), ivec2(10));
vec4 overlayColor = texelFetch(_flw_overlayTex, actualCoord, 0);
color.rgb = mix(overlayColor.rgb, color.rgb, overlayColor.a);
}