Skip to content

Commit

Permalink
Build: Port to 1.21 Fabric
Browse files Browse the repository at this point in the history
GitHub: #72
  • Loading branch information
DJtheRedstoner authored Jun 12, 2024
1 parent 7b11dc2 commit 2da123e
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 65 deletions.
107 changes: 54 additions & 53 deletions api/UniversalCraft.api

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions root.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {
version = versionFromBuildIdAndBranch()

preprocess {
val fabric12100 = createNode("1.21-fabric", 12100, "srg")
val fabric12006 = createNode("1.20.6-fabric", 12006, "srg")
val forge12004 = createNode("1.20.4-forge", 12004, "srg")
val fabric12004 = createNode("1.20.4-fabric", 12004, "yarn")
Expand All @@ -34,6 +35,7 @@ preprocess {
val forge11202 = createNode("1.12.2-forge", 11202, "srg")
val forge10809 = createNode("1.8.9-forge", 10809, "srg")

fabric12100.link(fabric12006)
fabric12006.link(fabric12004)
forge12004.link(fabric12004)
fabric12004.link(fabric12002, file("versions/1.20.4-1.20.2.txt"))
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ listOf(
"1.20.4-fabric",
"1.20.4-forge",
"1.20.6-fabric",
"1.21-fabric",
).forEach { version ->
include(":$version")
project(":$version").apply {
Expand Down
95 changes: 88 additions & 7 deletions src/main/java/gg/essential/universal/UGraphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
import static org.lwjgl.opengl.GL13.GL_ACTIVE_TEXTURE;
import static org.lwjgl.opengl.GL13.GL_TEXTURE0;

//#if MC>=12100
//$$ import net.minecraft.client.render.BufferRenderer;
//$$ import net.minecraft.client.render.BuiltBuffer;
//$$ import net.minecraft.client.util.BufferAllocator;
//#endif

//#if MC>=12005
//$$ import org.joml.Vector3f;
//#endif
Expand Down Expand Up @@ -83,7 +89,9 @@
public class UGraphics {
private static final Pattern formattingCodePattern = Pattern.compile("(?i)\u00a7[0-9A-FK-OR]");

//#if MC>=11602
//#if MC>=12100
//$$ public static Style EMPTY_WITH_FONT_ID = Style.EMPTY.withFont(Identifier.of("minecraft", "alt"));
//#elseif MC>=11602
//$$ public static Style EMPTY_WITH_FONT_ID = Style.EMPTY.setFontId(new ResourceLocation("minecraft", "alt"));
//#endif
private static UMatrixStack UNIT_STACK = UMatrixStack.UNIT;
Expand All @@ -97,6 +105,14 @@ public class UGraphics {
//$$ private static final boolean TEXT_LAYER_TYPE = false;
//#endif

//#if MC>=12100
//$$ /**
//$$ * A buffer to use for storing sorted quad vertex indices. The buffer will automatically grow as needed,
//$$ * the specified size is not a hard cap.
//$$ */
//$$ private static final BufferAllocator SORTED_QUADS_ALLOCATOR = new BufferAllocator(65536);
//#endif

public UGraphics(WorldRenderer instance) {
this.instance = instance;
}
Expand All @@ -106,7 +122,11 @@ public UVertexConsumer asUVertexConsumer() {
}

public static UGraphics getFromTessellator() {
//#if MC>=12100
//$$ return new UGraphics(null);
//#else
return new UGraphics(getTessellator().getWorldRenderer());
//#endif
}

//#if MC<11700
Expand Down Expand Up @@ -150,10 +170,14 @@ public static Tessellator getTessellator() {
return Tessellator.getInstance();
}

//#if MC>=12100
//$$ // No possible alternative on 1.21. A compile time error here is better than a run time one.
//#else
@Deprecated // Use the non-static methods for 1.17+ compatibility or call UGraphics.getTessellator().draw() directly
public static void draw() {
getTessellator().draw();
}
//#endif

public static void cullFace(int mode) {
//#if MC>=11502
Expand Down Expand Up @@ -394,7 +418,11 @@ public static void drawString(String text, float x, float y, int color, boolean
public static void drawString(UMatrixStack stack, String text, float x, float y, int color, boolean shadow) {
if ((color >> 24 & 255) <= 10) return;
//#if MC>=11602
//#if MC>=12100
//$$ VertexConsumerProvider.Immediate irendertypebuffer$impl = UMinecraft.getMinecraft().getBufferBuilders().getEntityVertexConsumers();
//#else
//$$ IRenderTypeBuffer.Impl irendertypebuffer$impl = IRenderTypeBuffer.getImpl(Tessellator.getInstance().getBuffer());
//#endif
//$$ UMinecraft.getFontRenderer().renderString(text, x, y, color, shadow, stack.peek().getModel(), irendertypebuffer$impl, TEXT_LAYER_TYPE, 0, 15728880);
//$$ irendertypebuffer$impl.finish();
//#else
Expand Down Expand Up @@ -422,7 +450,11 @@ public static void drawString(UMatrixStack stack, String text, float x, float y,
if ((color >> 24 & 255) <= 10) return;
String shadowText = ChatColor.Companion.stripColorCodes(text);
//#if MC>=11602
//#if MC>=12100
//$$ VertexConsumerProvider.Immediate irendertypebuffer$impl = UMinecraft.getMinecraft().getBufferBuilders().getEntityVertexConsumers();
//#else
//$$ IRenderTypeBuffer.Impl irendertypebuffer$impl = IRenderTypeBuffer.getImpl(Tessellator.getInstance().getBuffer());
//#endif
//$$ UMinecraft.getFontRenderer().renderString(shadowText, x + 1f, y + 1f, shadowColor, false, stack.peek().getModel(), irendertypebuffer$impl, TEXT_LAYER_TYPE, 0, 15728880);
//$$ UMinecraft.getFontRenderer().renderString(text, x, y, color, false, stack.peek().getModel(), irendertypebuffer$impl, TEXT_LAYER_TYPE, 0, 15728880);
//$$ irendertypebuffer$impl.finish();
Expand Down Expand Up @@ -776,7 +808,11 @@ public UGraphics beginWithActiveShader(DrawMode mode, CommonVertexFormats format

public UGraphics beginWithActiveShader(DrawMode mode, VertexFormat format) {
vertexFormat = format;
//#if MC>=12100
//$$ instance = getTessellator().begin(mode.mcMode, format);
//#else
instance.begin(mode.mcMode, format);
//#endif
return this;
}

Expand All @@ -791,7 +827,11 @@ public UGraphics beginWithActiveShader(DrawMode mode, VertexFormat format) {
//$$ DEFAULT_SHADERS.put(VertexFormats.POSITION_COLOR, GameRenderer::getPositionColorShader);
//$$ DEFAULT_SHADERS.put(VertexFormats.POSITION_COLOR_LIGHT, GameRenderer::getPositionColorLightmapShader);
//$$ DEFAULT_SHADERS.put(VertexFormats.POSITION_TEXTURE, GameRenderer::getPositionTexShader);
//#if MC>=12100
//$$ // Shader for this format is no longer provided.
//#else
//$$ DEFAULT_SHADERS.put(VertexFormats.POSITION_COLOR_TEXTURE, GameRenderer::getPositionColorTexShader);
//#endif
//$$ DEFAULT_SHADERS.put(VertexFormats.POSITION_TEXTURE_COLOR, GameRenderer::getPositionTexColorShader);
//$$ DEFAULT_SHADERS.put(VertexFormats.POSITION_COLOR_TEXTURE_LIGHT, GameRenderer::getPositionColorTexLightmapShader);
//#if MC>=12005
Expand Down Expand Up @@ -843,40 +883,62 @@ public UGraphics begin(int glMode, VertexFormat format) {
}

public void drawDirect() {
//#if MC>=12100
//$$ BuiltBuffer builtBuffer = instance.end();
//#endif
//#if MC>=11600
//$$ if (renderLayer != null) {
//#if MC>=12000
//#if MC>=12100
//$$ renderLayer.draw(builtBuffer);
//#elseif MC>=12000
//$$ renderLayer.draw(instance, RenderSystem.getVertexSorting());
//#else
//$$ renderLayer.finish(instance, 0, 0, 0);
//#endif
//$$ return;
//$$ }
//#endif
doDraw();
doDraw(
//#if MC>=12100
//$$ builtBuffer
//#endif
);
}

public void drawSorted(int cameraX, int cameraY, int cameraZ) {
//#if MC>=12100
//$$ BuiltBuffer builtBuffer = instance.end();
//$$ builtBuffer.sortQuads(SORTED_QUADS_ALLOCATOR, RenderSystem.getVertexSorting());
//#endif
//#if MC>=11600
//$$ if (renderLayer != null) {
//#if MC>=12000
//#if MC>=12100
//$$ renderLayer.draw(builtBuffer);
//#elseif MC>=12000
//$$ renderLayer.draw(instance, RenderSystem.getVertexSorting());
//#else
//$$ renderLayer.finish(instance, cameraX, cameraY, cameraZ);
//#endif
//$$ return;
//$$ }
//#endif
//#if MC>=12000
//#if MC>=12100
//$$ // Sorting handled above.
//#elseif MC>=12000
//$$ instance.setSorter(RenderSystem.getVertexSorting());
//#elseif MC>=11700
//$$ instance.setCameraPosition(cameraX, cameraY, cameraZ);
//#else
instance.sortVertexData(cameraX, cameraY, cameraZ);
//#endif
doDraw();
doDraw(
//#if MC>=12100
//$$ builtBuffer
//#endif
);
}

//#if MC<11700
private static boolean[] getDesiredTextureUnitState(VertexFormat vertexFormat) {
// Vanilla only ever has two UV elements, so we can assume the remainder to be disabled by default and don't
// need to check them unless we want them enabled.
Expand All @@ -892,11 +954,20 @@ private static boolean[] getDesiredTextureUnitState(VertexFormat vertexFormat) {
}
return wantEnabled;
}
//#endif

private void doDraw() {
private void doDraw(
//#if MC>=12100
//$$ BuiltBuffer builtBuffer
//#endif
) {
VertexFormat vertexFormat = this.vertexFormat;
if (vertexFormat == null) {
//#if MC>=12100
//$$ BufferRenderer.drawWithGlobalProgram(builtBuffer);
//#else
getTessellator().draw();
//#endif
return;
}

Expand All @@ -919,7 +990,11 @@ private void doDraw() {
}
//#endif

//#if MC>=12100
//$$ BufferRenderer.drawWithGlobalProgram(builtBuffer);
//#else
getTessellator().draw();
//#endif

//#if MC<11700
for (int i = 0; i < wasEnabledStates.length; i++) {
Expand All @@ -942,7 +1017,11 @@ public UGraphics pos(double x, double y, double z) {

public UGraphics pos(UMatrixStack stack, double x, double y, double z) {
if (stack == UNIT_STACK) {
//#if MC>=12100
//$$ instance.vertex((float) x, (float) y, (float) z);
//#else
instance.pos(x, y, z);
//#endif
} else {
//#if MC>=11602
//$$ instance.pos(stack.peek().getModel(), (float) x, (float) y, (float) z);
Expand Down Expand Up @@ -1000,7 +1079,9 @@ public UGraphics color(Color color) {
}

public UGraphics endVertex() {
//#if MC<12100
instance.endVertex();
//#endif
return this;
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/gg/essential/universal/UMatrixStack.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gg.essential.universal

import net.minecraft.client.renderer.GLAllocation
import org.lwjgl.opengl.GL11
import org.lwjgl.util.vector.Matrix3f
import org.lwjgl.util.vector.Matrix4f
Expand All @@ -14,6 +13,8 @@ import kotlin.math.sin

//#if MC>=11700
//$$ import com.mojang.blaze3d.systems.RenderSystem
//#else
import net.minecraft.client.renderer.GLAllocation
//#endif

//#if MC>=11600
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ internal class VanillaVertexConsumer(

override fun pos(stack: UMatrixStack, x: Double, y: Double, z: Double): UVertexConsumer = apply {
if (stack === UMatrixStack.UNIT) {
//#if MC>=12100
//$$ inner.vertex(x.toFloat(), y.toFloat(), z.toFloat())
//#else
inner.pos(x, y, z)
//#endif
return@apply
}
//#if MC>=11602
Expand Down Expand Up @@ -88,6 +92,8 @@ internal class VanillaVertexConsumer(
}

override fun endVertex(): UVertexConsumer = apply {
//#if MC<12100
inner.endVertex()
//#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import gg.essential.universal.UGraphics.CommonVertexFormats
import net.minecraft.client.gl.GlUniform
import net.minecraft.client.render.Shader
import net.minecraft.client.render.VertexFormat
import net.minecraft.client.render.VertexFormatElement
import net.minecraft.client.render.VertexFormats
import net.minecraft.util.Identifier
import org.apache.commons.codec.digest.DigestUtils
Expand Down Expand Up @@ -108,15 +109,30 @@ internal class MCShader(
//#endif
}

fun buildVertexFormat(elements: Map<String, VertexFormatElement>): VertexFormat {
//#if MC>=12100
//$$ val builder = VertexFormat.builder()
//$$ elements.forEach { (name, element) -> builder.add(name, element) }
//$$ return builder.build()
//#else
return VertexFormat(ImmutableMap.copyOf(elements))
//#endif
}

val shaderVertexFormat = if (vertexFormat != null) {
// Shader calls glBindAttribLocation using the names in the VertexFormat, not the shader json...
// Easiest way to work around this is to construct a custom VertexFormat with our prefixed names.
VertexFormat(ImmutableMap.copyOf(
transformer.attributes.withIndex()
.associate { it.value to vertexFormat.mc.elements[it.index] }))
buildVertexFormat(transformer.attributes.withIndex()
.associate { it.value to vertexFormat.mc.elements[it.index] })
} else {
// Legacy fallback: The actual element doesn't matter here, Shader only cares about the names
VertexFormat(ImmutableMap.copyOf(transformer.attributes.associateWith { VertexFormats.POSITION_ELEMENT }))
buildVertexFormat(transformer.attributes.associateWith {
//#if MC>=12100
//$$ VertexFormatElement.POSITION
//#else
VertexFormats.POSITION_ELEMENT
//#endif
})
}


Expand Down
2 changes: 2 additions & 0 deletions versions/1.21-fabric/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
essential.defaults.loom.minecraft=com.mojang:minecraft:1.21-rc1
essential.defaults.loom.mappings=net.fabricmc:yarn:1.21-rc1+build.1:v2

0 comments on commit 2da123e

Please sign in to comment.