Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions api/UniversalCraft.api
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public class gg/essential/universal/UGraphics {
@1.17.1-forge,1.18.1-forge,1.19.2-forge,1.19.3-forge,1.19.4-forge,1.20.1-forge,1.20.2-forge,1.20.4-forge,1.20.4-neoforge,1.20.6-forge,1.20.6-neoforge,1.21-forge,1.21-neoforge,1.21.11-fabric,1.21.3-forge,1.21.3-neoforge,1.21.4-forge,1.21.4-neoforge,1.21.5-fabric,1.21.5-forge,1.21.5-neoforge,1.21.6-fabric,1.21.7-fabric,1.21.7-forge,1.21.7-neoforge,1.21.9-fabric,26.1-fabric,26.2-fabric
public fun begin (ILcom/mojang/blaze3d/vertex/VertexFormat;)Lgg/essential/universal/UGraphics;
public fun begin (ILgg/essential/universal/UGraphics$CommonVertexFormats;)Lgg/essential/universal/UGraphics;
@26.1-fabric,26.2-fabric
@26.1-fabric
public fun beginRenderLayer (Lnet/minecraft/client/renderer/rendertype/RenderType;)Lgg/essential/universal/UGraphics;
@1.16.2-fabric,1.17.1-fabric,1.18.1-fabric,1.19-fabric,1.19.1-fabric,1.19.2-fabric,1.19.3-fabric,1.19.4-fabric,1.20-fabric,1.20.1-fabric,1.20.2-fabric,1.20.4-fabric,1.20.6-fabric,1.21-fabric,1.21.3-fabric,1.21.4-fabric
public fun begin (ILnet/minecraft/client/render/VertexFormat;)Lgg/essential/universal/UGraphics;
Expand Down Expand Up @@ -1262,7 +1262,9 @@ public final class gg/essential/universal/render/URenderPipeline$ColorLogic : ja
}

public final class gg/essential/universal/render/URenderPipeline$Companion {
@26.1-fabric,26.2-fabric
@26.2-fabric
public final fun builder (Lnet/minecraft/resources/Identifier;Lgg/essential/universal/UGraphics$DrawMode;Lcom/mojang/blaze3d/vertex/VertexFormat;Lnet/minecraft/resources/Identifier;Lnet/minecraft/resources/Identifier;Ljava/util/List;)Lgg/essential/universal/render/URenderPipeline$Builder;
@26.1-fabric
public final fun builder (Lnet/minecraft/resources/Identifier;Lgg/essential/universal/UGraphics$DrawMode;Lcom/mojang/blaze3d/vertex/VertexFormat;Lnet/minecraft/resources/Identifier;Lnet/minecraft/resources/Identifier;Ljava/util/List;Ljava/util/Map;)Lgg/essential/universal/render/URenderPipeline$Builder;
@1.21.5-forge,1.21.5-neoforge,1.21.7-forge,1.21.7-neoforge
public final fun builder (Lnet/minecraft/resources/ResourceLocation;Lgg/essential/universal/UGraphics$DrawMode;Lcom/mojang/blaze3d/vertex/VertexFormat;Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/resources/ResourceLocation;Ljava/util/List;Ljava/util/Map;)Lgg/essential/universal/render/URenderPipeline$Builder;
Expand Down Expand Up @@ -1607,15 +1609,15 @@ public final class gg/essential/universal/vertex/UBufferBuilder$Companion {
public abstract interface class gg/essential/universal/vertex/UBuiltBuffer : java/lang/AutoCloseable {
public static final field Companion Lgg/essential/universal/vertex/UBuiltBuffer$Companion;
public fun draw (Lgg/essential/universal/render/URenderPipeline;Lkotlin/jvm/functions/Function1;)V
@26.1-fabric,26.2-fabric
@26.1-fabric
public fun draw (Lnet/minecraft/client/renderer/rendertype/RenderType;)V
@1.16.2-forge,1.17.1-forge,1.18.1-forge,1.19.2-forge,1.19.3-forge,1.19.4-forge,1.20.1-forge,1.20.2-forge,1.20.4-forge,1.20.4-neoforge,1.20.6-forge,1.20.6-neoforge,1.21-forge,1.21-neoforge,1.21.3-forge,1.21.3-neoforge,1.21.4-forge,1.21.4-neoforge,1.21.5-forge,1.21.5-neoforge,1.21.7-forge,1.21.7-neoforge
public fun draw (Lnet/minecraft/client/renderer/RenderType;)V
@1.16.2-fabric,1.17.1-fabric,1.18.1-fabric,1.19-fabric,1.19.1-fabric,1.19.2-fabric,1.19.3-fabric,1.19.4-fabric,1.20-fabric,1.20.1-fabric,1.20.2-fabric,1.20.4-fabric,1.20.6-fabric,1.21-fabric,1.21.11-fabric,1.21.3-fabric,1.21.4-fabric,1.21.5-fabric,1.21.6-fabric,1.21.7-fabric,1.21.9-fabric
public fun draw (Lnet/minecraft/client/render/RenderLayer;)V
public static synthetic fun draw$default (Lgg/essential/universal/vertex/UBuiltBuffer;Lgg/essential/universal/render/URenderPipeline;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public fun drawAndClose (Lgg/essential/universal/render/URenderPipeline;Lkotlin/jvm/functions/Function1;)V
@26.1-fabric,26.2-fabric
@26.1-fabric
public fun drawAndClose (Lnet/minecraft/client/renderer/rendertype/RenderType;)V
@1.16.2-forge,1.17.1-forge,1.18.1-forge,1.19.2-forge,1.19.3-forge,1.19.4-forge,1.20.1-forge,1.20.2-forge,1.20.4-forge,1.20.4-neoforge,1.20.6-forge,1.20.6-neoforge,1.21-forge,1.21-neoforge,1.21.3-forge,1.21.3-neoforge,1.21.4-forge,1.21.4-neoforge,1.21.5-forge,1.21.5-neoforge,1.21.7-forge,1.21.7-neoforge
public fun drawAndClose (Lnet/minecraft/client/renderer/RenderType;)V
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/gg/essential/universal/UGraphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@
//$$ import net.minecraft.util.text.StringTextComponent;
//$$ import net.minecraft.util.text.ITextProperties;
//$$ import net.minecraft.client.renderer.RenderType;
//#if MC < 1.21.6
//$$ import net.minecraft.client.renderer.IRenderTypeBuffer;
//#endif
//$$ import java.util.ArrayList;
//$$ import java.util.Optional;
//#else
Expand Down Expand Up @@ -1307,7 +1309,7 @@ public UGraphics beginWithDefaultShader(DrawMode mode, VertexFormat format) {
return beginWithActiveShader(mode, format);
}

//#if MC>=11600
//#if MC>=11600 && MC < 26.2
//$$ private RenderType renderLayer;
//$$ public UGraphics beginRenderLayer(RenderType renderLayer) {
//$$ this.renderLayer = renderLayer;
Expand Down Expand Up @@ -1340,7 +1342,7 @@ public void drawDirect() {
//$$ BuiltBuffer builtBuffer = instance.endNullable();
//$$ if (builtBuffer == null) return;
//#endif
//#if MC>=11600
//#if MC>=11600 && MC < 26.2
//$$ if (renderLayer != null) {
//#if MC>=12100
//$$ renderLayer.draw(builtBuffer);
Expand Down Expand Up @@ -1374,7 +1376,7 @@ public void drawSorted(int cameraX, int cameraY, int cameraZ) {
//$$ builtBuffer.sortQuads(ALLOCATOR, RenderSystem.getVertexSorting());
//#endif
//#endif
//#if MC>=11600
//#if MC>=11600 && MC < 26.2
//$$ if (renderLayer != null) {
//#if MC>=12100
//$$ renderLayer.draw(builtBuffer);
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/gg/essential/universal/render/ScissorState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ internal data class ScissorState(val enabled: Boolean, val x: Int, val y: Int, v
//#endif
}

fun coerceIn(x: Int, y: Int, width: Int, height: Int): ScissorState {
var x1 = this.x
var x2 = this.x + this.width
var y1 = this.y
var y2 = this.y + this.height
x1 = x1.coerceAtLeast(x)
x2 = x2.coerceAtMost(x + width)
y1 = y1.coerceAtLeast(y)
y2 = y2.coerceAtMost(y + height)
return ScissorState(
enabled,
x1,
y1,
(x2 - x1).coerceAtLeast(0),
(y2 - y1).coerceAtLeast(0),
)
}

companion object {
val DISABLED = ScissorState(false, 0, 0, 0, 0)

Expand Down
87 changes: 67 additions & 20 deletions src/main/kotlin/gg/essential/universal/render/URenderPass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package gg.essential.universal.render

import gg.essential.universal.vertex.UBuiltBuffer
import gg.essential.universal.vertex.UBuiltBufferInternal
import kotlin.ranges.coerceIn

//#if STANDALONE
//#else
//#if MC >= 26.2
//$$ import com.mojang.blaze3d.vertex.VertexFormat
//$$ import java.nio.ByteBuffer
//#endif

//#if MC>=12111
//$$ import com.mojang.blaze3d.textures.AddressMode
//$$ import com.mojang.blaze3d.textures.FilterMode
Expand Down Expand Up @@ -52,14 +58,20 @@ internal class URenderPass : AutoCloseable {
val builder = DrawCallBuilderImpl(pipeline, builtBuffer as UBuiltBufferInternal)
configure(builder)
builder.submit()
builder.close()
}

internal inner class DrawCallBuilderImpl(
private val pipeline: URenderPipeline,
private val builtBuffer: UBuiltBufferInternal,
) : DrawCallBuilder {
) : DrawCallBuilder, AutoCloseable {
//#if MC>=12106 && !STANDALONE
//$$ private val tmpBuffers = mutableListOf<GpuBuffer>()
//#endif

//#if MC>=12105 && !STANDALONE
//$$ val mc: RenderPass
//$$ val outputTextureSize: Pair<Int, Int>
//$$ init {
//#if MC>=12106
//$$ val dynamicUniforms = RenderSystem.getDynamicUniforms().write(
Expand All @@ -82,6 +94,14 @@ internal class URenderPass : AutoCloseable {
//#endif
//$$ )
//#endif
//#if MC >= 26.2
//$$ fun VertexFormat.uploadImmediateVertexBuffer(buffer: ByteBuffer) =
//$$ RenderSystem.getDevice().createBuffer({ "Immediate vertex buffer for $pipeline" }, GpuBuffer.USAGE_COPY_DST or GpuBuffer.USAGE_VERTEX, buffer)
//$$ .also { tmpBuffers.add(it) }
//$$ fun VertexFormat.uploadImmediateIndexBuffer(buffer: ByteBuffer) =
//$$ RenderSystem.getDevice().createBuffer({ "Immediate index buffer for $pipeline" }, GpuBuffer.USAGE_COPY_DST or GpuBuffer.USAGE_INDEX, buffer)
//$$ .also { tmpBuffers.add(it) }
//#endif
//$$ val builtBuffer = builtBuffer.mc
//$$ val vertexBuffer = pipeline.format.uploadImmediateVertexBuffer(builtBuffer.buffer)
//$$ val sortedBuffer = builtBuffer.sortedBuffer
Expand All @@ -91,17 +111,27 @@ internal class URenderPass : AutoCloseable {
//$$ val shapeIndexBuffer = RenderSystem.getSequentialBuffer(builtBuffer.drawParameters.mode())
//$$ shapeIndexBuffer.getIndexBuffer(builtBuffer.drawParameters.indexCount()) to shapeIndexBuffer.indexType
//$$ }
//$$ mc = MinecraftClient.getInstance().framebuffer.let { fb ->
//#if MC >= 26.2
//$$ mc = Minecraft.getInstance().gameRenderer.mainRenderTarget().let { fb ->
//#else
//$$ mc = MinecraftClient.getInstance().framebuffer.let { fb ->
//#endif
//#if MC >= 1.21.6
//$$ val outputColorTexture = RenderSystem.outputColorTextureOverride ?: fb.colorAttachmentView!!
//$$ val outputDepthTexture = RenderSystem.outputDepthTextureOverride ?: fb.depthAttachmentView
//#else
//$$ val outputColorTexture = fb.colorAttachment!!
//$$ val outputDepthTexture = fb.depthAttachment
//#endif
//$$ outputTextureSize = Pair(outputColorTexture.getWidth(0), outputColorTexture.getHeight(0))
//$$ RenderSystem.getDevice().createCommandEncoder().createRenderPass(
//#if MC>=12106
//$$ { "Immediate draw for $pipeline" },
//$$ RenderSystem.outputColorTextureOverride ?: fb.colorAttachmentView!!,
//$$ OptionalInt.empty(),
//$$ RenderSystem.outputDepthTextureOverride ?: fb.depthAttachmentView,
//$$ OptionalDouble.empty(),
//#else
//$$ fb.colorAttachment!!, OptionalInt.empty(), fb.depthAttachment, OptionalDouble.empty()
//#endif
//$$ outputColorTexture,
//$$ OptionalInt.empty(),
//$$ outputDepthTexture,
//$$ OptionalDouble.empty(),
//$$ )
//$$ }
//$$ mc.setVertexBuffer(0, vertexBuffer)
Expand All @@ -127,10 +157,6 @@ internal class URenderPass : AutoCloseable {
scissor = ScissorState(true, x, y, width, height)
}

//#if MC>=12106 && !STANDALONE
//$$ private val tmpBuffers = mutableListOf<GpuBuffer>()
//#endif

override fun uniform(name: String, vararg values: Float): DrawCallBuilder = apply {
//#if MC>=12105 && !STANDALONE
//#if MC>=12106
Expand Down Expand Up @@ -165,6 +191,15 @@ internal class URenderPass : AutoCloseable {
//#endif
}

//#if MC >= 1.21.5 && !STANDALONE
//$$ private fun samplerNameByIndex(index: Int) =
//#if MC >= 26.2
//$$ pipeline.mcRenderPipeline.bindGroupLayouts.asSequence().flatMap { it.samplers }.elementAt(index)
//#else
//$$ pipeline.mcRenderPipeline.samplers[index]
//#endif
//#endif

override fun texture(name: String, textureView: UGpuTextureView, sampler: UGpuSampler): DrawCallBuilder = apply {
//#if MC >= 1.21.5 && !STANDALONE
//#if MC >= 1.21.11
Expand All @@ -184,7 +219,7 @@ internal class URenderPass : AutoCloseable {

override fun texture(index: Int, textureView: UGpuTextureView, sampler: UGpuSampler): DrawCallBuilder = apply {
//#if MC >= 1.21.5 && !STANDALONE
//$$ texture(pipeline.mcRenderPipeline.samplers[index], textureView, sampler)
//$$ texture(samplerNameByIndex(index), textureView, sampler)
//#else
sampler.impl.configureTexture(textureView.texture.impl.glId)
pipeline.texture(index, textureView.texture.impl.glId)
Expand Down Expand Up @@ -222,27 +257,29 @@ internal class URenderPass : AutoCloseable {
override fun texture(index: Int, textureGlId: Int): DrawCallBuilder = apply {
//#if MC>=12105 && !STANDALONE
//$$ @Suppress("DEPRECATION")
//$$ texture(pipeline.mcRenderPipeline.samplers[index], textureGlId)
//$$ texture(samplerNameByIndex(index), textureGlId)
//#else
pipeline.texture(index, textureGlId)
//#endif
}

fun submit() {
//#if MC>=12105 && !STANDALONE
//$$ val scissor = scissor ?: ScissorState.active()
//$$ var scissor = scissor ?: ScissorState.active()
//$$ if (scissor.enabled) {
//$$ // As of 26.2-snapshot-4, Minecraft will throw when the given scissor is out-of-bounds or empty.
//$$ // Existing users of URenderPass are already used to it accepting such scissors, so we'll silently
//$$ // coerce the given scissor to fit, and skip drawing altogether when it's empty.
//$$ scissor = scissor.coerceIn(0, 0, outputTextureSize.first, outputTextureSize.second)
//$$ if (scissor.width <= 0 || scissor.height <= 0) {
//$$ return
//$$ }
//$$ mc.enableScissor(scissor.x, scissor.y, scissor.width, scissor.height)
//$$ } else {
//$$ mc.disableScissor()
//$$ }
//$$
//$$ pipeline.draw(mc, builtBuffer.mc)
//$$
//$$ mc.close()
//#if MC>=12106
//$$ tmpBuffers.forEach { it.close() }
//#endif
//#else
if (currPipeline != pipeline) {
currPipeline = pipeline
Expand All @@ -258,9 +295,19 @@ internal class URenderPass : AutoCloseable {
pipeline.draw(builtBuffer)

prevScissor?.activate()
//#endif
}

override fun close() {
//#if MC >= 1.21.5 && !STANDALONE
//$$ mc.close()
//#else
pipeline.unbind()
//#endif

//#if MC >= 1.21.6 && !STANDALONE
//$$ tmpBuffers.forEach { it.close() }
//#endif
}
}
}
Loading
Loading