Skip to content

Commit

Permalink
Added multidraw terrain renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
mineLdiver committed Jul 12, 2023
1 parent 10ae9c8 commit 6c0abcf
Show file tree
Hide file tree
Showing 43 changed files with 2,834 additions and 274 deletions.
67 changes: 38 additions & 29 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '0.12.cursed_1'
id 'babric-loom' version '1.1.7'
id 'maven-publish'
}

Expand All @@ -10,59 +10,67 @@ archivesBaseName = project.archives_base_name
version = project.mod_version
group = project.maven_group

loom {
gluedMinecraftJar()
customMinecraftManifest.set("https://babric.github.io/manifest-polyfill/${minecraft_version}.json")
}

repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.

// Used for the fabric toolchain in this project.
maven {
name = 'Jitpack'
url = 'https://jitpack.io'
name = 'Babric'
url = 'https://maven.glass-launcher.net/babric'
}
// Used for mappings.
maven {
name = 'Glass'
url = 'https://maven.glass-launcher.net/snapshots'
name = 'Glass Releases'
url = 'https://maven.glass-launcher.net/releases'
}
// Used for StationAPI and HowManyItems.
maven {
name = 'HalfOf2'
url = 'https://storage.googleapis.com/devan-maven/'
name = 'Glass Snapshots'
url = 'https://maven.glass-launcher.net/snapshots'
}
// Used for a StationAPI dependency.
maven {
name = 'Froge'
url 'https://maven.minecraftforge.net/'
}
}

loom {
customMinecraftManifest = "https://pymcl.net/b1.7.3.json"
intermediaryUrl = "https://pymcl.net/b1.7.3-int.jar"
cursedMinecraftJar()
// Used for projects that do not have a maven repository, but do have a GitHub repository with working build scripts.
maven {
name = 'Jitpack'
url = 'https://jitpack.io'
}
mavenCentral()
}

dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "com.github.calmilamsy:BIN-Mappings:${project.yarn_mappings}"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
mappings "net.glasslauncher:bin:${project.yarn_mappings}"
modImplementation "babric:fabric-loader:${project.loader_version}"

modImplementation ("net.modificationstation:StationAPI:99ccdc3") {
exclude module: "cursed-fabric-loader"
}

implementation "com.google.code.gson:gson:2.9.0"
implementation "org.slf4j:slf4j-api:1.8.0-beta4"
implementation 'org.apache.logging.log4j:log4j-slf4j18-impl:2.17.2'

compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
modImplementation "net.modificationstation:StationAPI:${project.stapi_version}"

implementation "blue.endless:jankson:1.2.1"
modImplementation("com.github.calmilamsy:HowManyItems-Fabric-Unofficial:1c6873a") {
exclude module: "cursed-fabric-loader"
exclude module: "StationAPI"
// Optional, but convenient mods for mod creators and users alike.
modImplementation("com.github.calmilamsy:ModMenu:${project.modmenu_version}") {
transitive false
}
modImplementation("net.glasslauncher.mods:GlassConfigAPI:${project.gcapi_version}") {
transitive false
}
modImplementation("net.glasslauncher:HowManyItems-Fabric-Unofficial:${project.howmanyitems_version}") {
transitive false
}

// Fabric API. This is technically optional, but you probably want it anyway.
// modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
}

processResources {
Expand All @@ -75,6 +83,7 @@ processResources {

tasks.withType(JavaCompile).configureEach {
// Minecraft 1.18 (1.18-pre2) upwards uses Java 17.
// Loom also requires J17.
it.options.release = 17
}

Expand Down
14 changes: 10 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ org.gradle.daemon=false
# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=b1.7.3
yarn_mappings=43d39fa
loader_version=0.12.12
yarn_mappings=b1.7.3-build.2
loader_version=0.14.19-babric.1

# Mod Properties
mod_version = 1.0.0
mod_version = 1.1
maven_group = net.mine_diver
archives_base_name = SmoothBeta

# Dependencies
# Dependencies
stapi_version=2c6d948

# Extra Dependencies
gcapi_version=1.1.5
howmanyitems_version=5.0.13
modmenu_version=v1.8.5-beta.3
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Sun Jun 14 19:21:24 BST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
9 changes: 2 additions & 7 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ pluginManagement {
url = 'https://maven.fabricmc.net/'
}
maven {
name = 'Jitpack'
url = 'https://jitpack.io'
name = 'Babric'
url = 'https://maven.glass-launcher.net/babric'
}
maven {
name = "Glass"
url = "https://maven.glass-launcher.net/snapshots"
}
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/mine_diver/smoothbeta/SmoothBeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import net.modificationstation.stationapi.api.mod.entrypoint.EventBusPolicy;
import net.modificationstation.stationapi.api.registry.ModID;
import net.modificationstation.stationapi.api.util.Null;
import org.apache.logging.log4j.Logger;

@Entrypoint(eventBus = @EventBusPolicy(registerStatic = false, registerInstance = false))
public class SmoothBeta {

@Entrypoint.ModID
public static final ModID MODID = Null.get();

@Entrypoint.Logger
public static final Logger LOGGER = Null.get();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.mine_diver.smoothbeta.client.render;

import net.mine_diver.smoothbeta.client.render.gl.VertexBuffer;

public class BufferRenderer {
public static void unbindAll() {
VertexBuffer.unbind();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package net.mine_diver.smoothbeta.client.render;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.modificationstation.stationapi.api.util.math.MathHelper;
import org.lwjgl.opengl.GL15;

import java.nio.ByteBuffer;
import java.util.function.IntConsumer;

import static net.mine_diver.smoothbeta.SmoothBeta.LOGGER;

@Environment(EnvType.CLIENT)
public final class IndexBuffer {
private static final IndexBuffer sharedSequential = new IndexBuffer(1, 1, java.util.function.IntConsumer::accept);;
private static final IndexBuffer sharedSequentialQuad = new IndexBuffer(4, 6, (intConsumer, i) -> {
intConsumer.accept(i);
intConsumer.accept(i + 1);
intConsumer.accept(i + 2);
intConsumer.accept(i + 2);
intConsumer.accept(i + 3);
intConsumer.accept(i);
});
private static final IndexBuffer sharedSequentialLines = new IndexBuffer(4, 6, (intConsumer, i) -> {
intConsumer.accept(i);
intConsumer.accept(i + 1);
intConsumer.accept(i + 2);
intConsumer.accept(i + 3);
intConsumer.accept(i + 2);
intConsumer.accept(i + 1);
});

public static IndexBuffer getSequentialBuffer(VertexFormat.DrawMode drawMode) {
return switch (drawMode) {
case QUADS -> sharedSequentialQuad;
case LINES -> sharedSequentialLines;
default -> sharedSequential;
};
}

private final int sizeMultiplier;
private final int increment;
private final IndexMapper indexMapper;
private int id;
private VertexFormat.IndexType indexType = VertexFormat.IndexType.BYTE;
private int size;

IndexBuffer(int sizeMultiplier, int increment, IndexMapper indexMapper) {
this.sizeMultiplier = sizeMultiplier;
this.increment = increment;
this.indexMapper = indexMapper;
}

public boolean isSizeLessThanOrEqual(int size) {
return size <= this.size;
}

public void bindAndGrow(int newSize) {
if (this.id == 0) this.id = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.id);
this.grow(newSize);
}

private void grow(int newSize) {
if (this.isSizeLessThanOrEqual(newSize)) return;
newSize = MathHelper.roundUpToMultiple(newSize * 2, this.increment);
LOGGER.debug("Growing IndexBuffer: Old limit {}, new limit {}.", this.size, newSize);
VertexFormat.IndexType indexType = VertexFormat.IndexType.smallestFor(newSize);
int i = MathHelper.roundUpToMultiple(newSize * indexType.size, 4);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, i, GL15.GL_DYNAMIC_DRAW);
ByteBuffer byteBuffer = GL15.glMapBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, GL15.GL_WRITE_ONLY, null);
if (byteBuffer == null) throw new RuntimeException("Failed to map GL buffer");
this.indexType = indexType;
IntConsumer intConsumer = this.getIndexConsumer(byteBuffer);
for (int j = 0; j < newSize; j += this.increment)
this.indexMapper.accept(intConsumer, j * this.sizeMultiplier / this.increment);
GL15.glUnmapBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER);
this.size = newSize;
}

private IntConsumer getIndexConsumer(ByteBuffer indicesBuffer) {
return switch (this.indexType) {
case BYTE -> index -> indicesBuffer.put((byte) index);
case SHORT -> index -> indicesBuffer.putShort((short) index);
default -> indicesBuffer::putInt;
};
}

public VertexFormat.IndexType getIndexType() {
return this.indexType;
}

@Environment(EnvType.CLIENT)
interface IndexMapper {
void accept(IntConsumer var1, int var2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package net.mine_diver.smoothbeta.client.render;

import net.mine_diver.smoothbeta.client.render.gl.GlUniform;
import net.mine_diver.smoothbeta.client.render.gl.VertexBuffer;
import net.mine_diver.smoothbeta.mixin.client.RenderListAccessor;
import net.minecraft.client.render.RenderList;
import net.minecraft.client.render.WorldRenderer;
import net.modificationstation.stationapi.api.util.math.Vec3f;

import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;

public class RenderRegion extends RenderList {

private final RenderListAccessor _super = (RenderListAccessor) this;
private final SmoothWorldRenderer stationWorldRenderer;
private final List<VertexBuffer> buffers = new ArrayList<>();

public RenderRegion(WorldRenderer worldRenderer) {
_super.smoothbeta_setField_2486(IntBuffer.allocate(0));
stationWorldRenderer = ((SmoothWorldRenderer) worldRenderer);
}

@Override
public void method_1912(int i, int j, int k, double d, double e, double f) {
super.method_1912(i, j, k, d, e, f);
buffers.clear();
}

@Override
public void method_1910(int i) {
throw new UnsupportedOperationException("Call lists can't be added to VBO regions!");
}

public void addBuffer(VertexBuffer buffer) {
buffers.add(buffer);
}

public void method_1909() {
if (!_super.smoothbeta_getField_2487()) {
return;
}
if (!buffers.isEmpty()) {
Shader shader = Shaders.getTerrainShader();
GlUniform chunkOffset = null;
if (shader != null)
chunkOffset = shader.chunkOffset;
if (chunkOffset != null) {
chunkOffset.set(_super.smoothbeta_getField_2480() - _super.smoothbeta_getField_2483(), _super.smoothbeta_getField_2481() - _super.smoothbeta_getField_2484(), _super.smoothbeta_getField_2482() - _super.smoothbeta_getField_2485());
chunkOffset.upload();
}
for (VertexBuffer vertexBuffer : buffers) vertexBuffer.uploadToPool();
stationWorldRenderer.smoothbeta_getTerrainVboPool().drawAll();
if (chunkOffset != null)
chunkOffset.set(Vec3f.ZERO);
}
}
}
Loading

0 comments on commit 6c0abcf

Please sign in to comment.