Skip to content

Commit

Permalink
Update render with indices
Browse files Browse the repository at this point in the history
  • Loading branch information
hackermdch committed May 25, 2024
1 parent 79be4af commit d598173
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 17 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ dependencies {
implementation "org.apiguardian:apiguardian-api:${project.apiguardian_api_version}"

modRuntimeOnly "com.terraformersmc:modmenu:${project.modmenu_version}"
// modRuntimeOnly "maven.modrinth:iris:${project.iris_version}"
modRuntimeOnly "maven.modrinth:fpsdisplay:${project.fpsdisplay_version}"
// modRuntimeOnly "maven.modrinth:iris:${project.iris_version}"
runtimeOnly "org.anarres:jcpp:${project.jcpp_version}"
runtimeOnly "io.github.douira:glsl-transformer:${project.glsl_transformer_version}"
runtimeOnly "org.antlr:antlr4-runtime:${project.antlr4_runtime_version}"
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx4G
fabric.loom.multiProjectOptimisation=true
minecraft_version=1.20.6
loader_version=0.15.10
mod_version=0.5.1
mod_version=0.5.2
maven_group=io.github.primogemstudio
archives_base_name=advancedfmk
fabric_version=0.97.8+1.20.6
Expand All @@ -13,7 +13,7 @@ kotlin_version=1.9.20
satin_version=1.17.0
sodium_version=mc1.20.6-0.5.8
modmenu_version=10.0.0-beta.1
fpsdisplay_version=4.0.0+1.20.4
fpsdisplay_version=4.1.0+1.20.5
joml_version=1.10.5
luaj_version=3.0.1
bcel_version=6.8.2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.primogemstudio.advancedfmk.interfaces

import com.primogemstudio.advancedfmk.mmd.PMXModel

interface BufferBuilderExt {
fun fullFormat(): Boolean
fun bumpNxt(d: Int)
fun submit()
fun resize()
fun padding(): Int
fun setPtr(ptr: Int)
fun getPMXModel(): PMXModel?
fun setPMXModel(model: PMXModel?)
}

interface RenderedBufferExt {
fun getPMXModel(): PMXModel?
fun setPMXModel(model: PMXModel?)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.primogemstudio.advancedfmk.mixin;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.mojang.blaze3d.platform.MemoryTracker;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferVertexConsumer;
import com.mojang.blaze3d.vertex.DefaultedVertexConsumer;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.primogemstudio.advancedfmk.interfaces.BufferBuilderExt;
import com.primogemstudio.advancedfmk.interfaces.RenderedBufferExt;
import com.primogemstudio.advancedfmk.mmd.PMXModel;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

import java.nio.ByteBuffer;

Expand All @@ -28,6 +34,9 @@ public abstract class BufferBuilderMixin extends DefaultedVertexConsumer impleme
@Shadow
public ByteBuffer buffer;

@Unique
private PMXModel model;

@Override
public boolean fullFormat() {
return fullFormat;
Expand Down Expand Up @@ -61,4 +70,38 @@ public void resize() {
buffer.rewind();
}
}

@Nullable
@Override
public PMXModel getPMXModel() {
return model;
}

@Override
public void setPMXModel(@Nullable PMXModel model) {
this.model = model;
}

@ModifyReturnValue(method = "storeRenderedBuffer", at = @At("RETURN"))
private BufferBuilder.RenderedBuffer storeRenderedBuffer(BufferBuilder.RenderedBuffer original) {
((RenderedBufferExt) original).setPMXModel(model);
model = null;
return original;
}

@Mixin(BufferBuilder.RenderedBuffer.class)
public static class RenderedBufferMixin implements RenderedBufferExt {
@Unique
private PMXModel model;

@Override
public PMXModel getPMXModel() {
return model;
}

@Override
public void setPMXModel(@Nullable PMXModel model) {
this.model = model;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.primogemstudio.advancedfmk.mixin;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.VertexBuffer;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.primogemstudio.advancedfmk.interfaces.RenderedBufferExt;
import com.primogemstudio.advancedfmk.mmd.PMXModel;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.nio.ByteBuffer;

@Mixin(VertexBuffer.class)
public abstract class VertexBufferMixin {
@Shadow
@Nullable
protected abstract RenderSystem.AutoStorageIndexBuffer uploadIndexBuffer(BufferBuilder.DrawState drawState, @Nullable ByteBuffer buffer);

@Unique
private PMXModel getModel(BufferBuilder.RenderedBuffer buffer) {
return ((RenderedBufferExt) buffer).getPMXModel();
}

@Redirect(method = "upload", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/vertex/BufferBuilder$DrawState;indexCount()I"))
private int indexCount(BufferBuilder.DrawState instance, BufferBuilder.RenderedBuffer buffer) {
var model = getModel(buffer);
return model != null ? model.indexCount : instance.indexCount();
}

@Redirect(method = "upload", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/vertex/BufferBuilder$DrawState;indexType()Lcom/mojang/blaze3d/vertex/VertexFormat$IndexType;"))
private VertexFormat.IndexType indexType(BufferBuilder.DrawState instance, BufferBuilder.RenderedBuffer buffer) {
var model = getModel(buffer);
return model != null ? VertexFormat.IndexType.INT : instance.indexType();
}

@Redirect(method = "upload", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/vertex/VertexBuffer;uploadIndexBuffer(Lcom/mojang/blaze3d/vertex/BufferBuilder$DrawState;Ljava/nio/ByteBuffer;)Lcom/mojang/blaze3d/systems/RenderSystem$AutoStorageIndexBuffer;"))
private RenderSystem.AutoStorageIndexBuffer uploadIndexBuffer(VertexBuffer instance, BufferBuilder.DrawState drawState, ByteBuffer buffer, BufferBuilder.RenderedBuffer buff) {
var model = getModel(buff);
if (model != null) {
model.bindIndices();
return null;
}
return uploadIndexBuffer(drawState, buffer);
}
}
Original file line number Diff line number Diff line change
@@ -1,55 +1,77 @@
package com.primogemstudio.advancedfmk.mmd;

import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.primogemstudio.advancedfmk.interfaces.AccessFromNative;
import com.primogemstudio.advancedfmk.mmd.renderer.MMDTextureAtlas;
import com.primogemstudio.advancedfmk.mmd.renderer.TextureManager;
import org.lwjgl.opengl.GL32;

import java.io.File;
import java.lang.ref.Cleaner;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER;

public class PMXModel implements AutoCloseable {
@AccessFromNative
private long ptr;
@AccessFromNative
private Cleaner.Cleanable cleaner;
private final Cleaner.Cleanable cleaner;
private static int ID = 0;
private final int id = ++ID;
private final String texture = "mmd_texture" + id;
@AccessFromNative
private final MMDTextureAtlas atlas;
public final TextureManager textureManager;
public final int vertexCount;
public final int vertexCount, indexCount;
public final Animation animation;
private final int ibo;

public PMXModel(File file) {
load(file);
vertexCount = getVertexCount();
indexCount = getIndexCount();
atlas = Loader.createAtlas(getTextures());
textureManager = new TextureManager(atlas);
animation = new Animation(this);
textureManager.register(texture);
mappingVertices();
ibo = GlStateManager._glGenBuffers();
GlStateManager._glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
RenderSystem.glBufferData(GL_ELEMENT_ARRAY_BUFFER, getIndices(), GL32.GL_STATIC_DRAW);
var p = ptr;
var tm = textureManager;
var t_ibo = ibo;
cleaner = SabaNative.cleaner.register(this, () -> {
SabaNative.release(PMXModel.class, p);
release(p);
tm.release();
RenderSystem.glDeleteBuffers(t_ibo);
});
}

private native void load(File file);

public native void render(ByteBuffer buff, ByteBuffer constants);

private native ByteBuffer getIndices();

private native int getVertexCount();

private native int getIndexCount();

private native List<File> getTextures();

private native void mappingVertices();

private static native void release(long ptr);

public void bindIndices() {
GlStateManager._glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
}

private float lastTime = System.nanoTime() / 1000000000f;
@AccessFromNative
private float animationTime = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ public final class SabaNative {
var path = "";
var os = System.getProperty("os.name");
var name = "";
if (arch.equals("aarch64")) path = "/natives/arm/";
else if (arch.equals("amd64")) path = "/natives/";
if (arch.equals("aarch64")) {
if (os.contains("Windows")) throw new RuntimeException("Unsupported Windows on ARM");
path = "/natives/arm/";
} else if (arch.equals("amd64")) {
if (os.contains("Mac OS")) throw new RuntimeException("Unsupported Mac OS on x86");
path = "/natives/";
}
if (path.isEmpty()) throw new RuntimeException("Unsupported CPU architecture: " + arch);
if (os.contains("Windows")) path += name = "saba-native.dll";
else if (os.contains("Linux")) path += name = "libsaba-native.so";
Expand All @@ -30,8 +35,6 @@ public final class SabaNative {

static final Cleaner cleaner = Cleaner.create();

static native void release(Class<?> cls, long ptr);

public static void init() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import net.minecraft.client.renderer.MultiBufferSource
import net.minecraft.client.renderer.RenderType
import net.minecraft.client.renderer.texture.OverlayTexture
import org.joml.Vector2i
import org.lwjgl.opengl.GL32
import java.nio.ByteBuffer
import java.nio.ByteOrder

Expand All @@ -25,6 +26,7 @@ class EntityRenderWrapper(val model: PMXModel) {
fun render(
entityYaw: Float, poseStack: PoseStack, buffer: MultiBufferSource, packedLight: Int
) {
GL32.GL_INT
val vc = buffer.getBuffer(renderType)
val buf = try {
if (vc is SodiumBufferBuilder) vc.originalBufferBuilder
Expand All @@ -33,6 +35,7 @@ class EntityRenderWrapper(val model: PMXModel) {
vc as BufferBuilder
}
buf as BufferBuilderExt
buf.setPMXModel(model)
poseStack.pushPose()
poseStack.scale(0.1f, 0.1f, 0.1f)
poseStack.mulPose(Axis.YN.rotationDegrees(entityYaw))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import net.minecraft.resources.ResourceLocation
import net.minecraft.server.packs.resources.ResourceManager
import net.minecraft.util.Mth
import java.nio.ByteBuffer
import java.nio.ByteOrder
import kotlin.math.ceil
import kotlin.math.sqrt

Expand Down Expand Up @@ -145,10 +144,7 @@ class MMDTextureAtlas(tes: List<NativeImage>) : AbstractTexture() {
}

@AccessFromNative
private val buff = ByteBuffer.allocateDirect(8).order(ByteOrder.nativeOrder())

@AccessFromNative
fun mapping(ti: Int) {
fun mapping(ti: Int, buff: ByteBuffer) {
val r = map[ti]!!
val x = buff.getFloat(0).toDouble()
val y = buff.getFloat(4).toDouble()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"compatibilityLevel": "JAVA_21",
"client": [
"BufferBuilderMixin",
"NativeImageMixin"
"BufferBuilderMixin$RenderedBufferMixin",
"NativeImageMixin",
"VertexBufferMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit d598173

Please sign in to comment.