Skip to content

Commit

Permalink
update to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus committed Mar 18, 2024
2 parents b96c3cf + fce9754 commit dfe70fe
Show file tree
Hide file tree
Showing 27 changed files with 289 additions and 97 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ dependencies {
//mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
neoForge "net.neoforged:neoforge:${project.neoforge_version}"

modImplementation "maven.modrinth:embeddium:0.2.15+mc1.20.4"
modImplementation "maven.modrinth:embeddium:0.3.9+mc1.20.4"
modCompileOnly "maven.modrinth:oculus:1.20.1-1.6.9"
// Fabric API
//modImplementation(fabricApi.module("fabric-api-base", project.fabric_version))
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ loader_version=0.15.0
fabric_version=0.91.1+1.20.4

# Mod Properties
mod_version=0.2.5-beta
mod_version=0.2.6-beta
maven_group=me.cortex
archives_base_name=nvidium
# Dependencies
# check this on https://modmuss50.me/fabric.html
loom.platform=neoforge
neoforge_version=20.4.48-beta
neoforge_version=20.4.200
5 changes: 1 addition & 4 deletions src/main/java/me/cortex/nvidium/Nvidium.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLConstructModEvent;
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.neoforge.network.NetworkConstants;
import org.lwjgl.opengl.GL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -31,9 +30,7 @@ public class Nvidium {
public static NvidiumConfig config;

public Nvidium() {
ModLoadingContext.get().registerExtensionPoint(IExtensionPoint.DisplayTest.class,
() -> new IExtensionPoint.DisplayTest(() -> NetworkConstants.IGNORESERVERONLY,
(remoteVersion, isFromServer) -> true ));

}

@SubscribeEvent
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/me/cortex/nvidium/NvidiumStub.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package me.cortex.nvidium;

import net.neoforged.fml.IExtensionPoint;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.network.NetworkConstants;

@Mod("nvidium")
public class NvidiumStub {
public NvidiumStub() {
ModLoadingContext.get().registerExtensionPoint(IExtensionPoint.DisplayTest.class,
() -> new IExtensionPoint.DisplayTest(() -> NetworkConstants.IGNORESERVERONLY,
(remoteVersion, isFromServer) -> true ));

}
}
9 changes: 8 additions & 1 deletion src/main/java/me/cortex/nvidium/NvidiumWorldRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.cortex.nvidium.gl.RenderDevice;
import me.cortex.nvidium.managers.AsyncOcclusionTracker;
import me.cortex.nvidium.managers.SectionManager;
import me.cortex.nvidium.sodiumCompat.NvidiumCompactChunkVertex;
import me.cortex.nvidium.util.DownloadTaskStream;
import me.cortex.nvidium.util.UploadingBufferStream;
import me.jellysquid.mods.sodium.client.SodiumClientMod;
Expand All @@ -14,6 +15,8 @@
import net.minecraft.client.render.Camera;
import net.minecraft.client.texture.Sprite;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4fc;
import org.joml.Matrix4x3fc;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -48,7 +51,7 @@ public NvidiumWorldRenderer(AsyncOcclusionTracker asyncChunkTracker) {

update_allowed_memory();
//this.sectionManager = new SectionManager(device, max_geometry_memory*1024*1024, uploadStream, 150, 24, CompactChunkVertex.STRIDE);
this.sectionManager = new SectionManager(device, max_geometry_memory*1024*1024, uploadStream, CompactChunkVertex.STRIDE, this);
this.sectionManager = new SectionManager(device, max_geometry_memory*1024*1024, uploadStream, NvidiumCompactChunkVertex.STRIDE, this);
this.renderPipeline = new RenderPipeline(device, uploadStream, downloadStream, sectionManager);


Expand Down Expand Up @@ -158,4 +161,8 @@ public Sprite[] getAnimatedSpriteSet() {
return new Sprite[0];
}
}

public void setTransformation(int id, Matrix4fc transform) {
this.renderPipeline.setTransformation(id, transform);
}
}
77 changes: 66 additions & 11 deletions src/main/java/me/cortex/nvidium/RenderPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import it.unimi.dsi.fastutil.ints.*;
import me.cortex.nvidium.api0.NvidiumAPI;
import me.cortex.nvidium.config.StatisticsLoggingLevel;
import me.cortex.nvidium.config.TranslucencySortingLevel;
import me.cortex.nvidium.gl.RenderDevice;
import me.cortex.nvidium.gl.buffers.IDeviceMappedBuffer;
import me.cortex.nvidium.managers.RegionManager;
import me.cortex.nvidium.managers.RegionVisibilityTracker;
import me.cortex.nvidium.managers.SectionManager;
import me.cortex.nvidium.renderers.*;
Expand Down Expand Up @@ -58,14 +60,15 @@ public class RenderPipeline {
private SortRegionSectionPhase regionSectionSorter;

private final IDeviceMappedBuffer sceneUniform;
private static final int SCENE_SIZE = (int) alignUp(4*4*4+4*4+4*4+4+4*4+4*4+8*7+3*4+3+4, 2);
private static final int SCENE_SIZE = (int) alignUp(4*4*4+4*4+4*4+4+4*4+4*4+8*8+3*4+3+4, 2);

private final IDeviceMappedBuffer regionVisibility;
private final IDeviceMappedBuffer sectionVisibility;
private final IDeviceMappedBuffer terrainCommandBuffer;
private final IDeviceMappedBuffer translucencyCommandBuffer;
private final IDeviceMappedBuffer regionSortingList;
private final IDeviceMappedBuffer statisticsBuffer;
private final IDeviceMappedBuffer transformationArray;

private final BitSet regionVisibilityTracker;

Expand Down Expand Up @@ -102,12 +105,35 @@ public RenderPipeline(RenderDevice device, UploadingBufferStream uploadStream, D
terrainCommandBuffer = device.createDeviceOnlyMappedBuffer(maxRegions*8L);
translucencyCommandBuffer = device.createDeviceOnlyMappedBuffer(maxRegions*8L);
regionSortingList = device.createDeviceOnlyMappedBuffer(maxRegions*2L);
this.transformationArray = device.createDeviceOnlyMappedBuffer(RegionManager.MAX_TRANSFORMATION_COUNT * (4*4*4));

regionVisibilityTracker = new BitSet(maxRegions);
regionVisibilityTracking = new RegionVisibilityTracker(downloadStream, maxRegions);

statisticsBuffer = device.createDeviceOnlyMappedBuffer(4*4);
stats = new Statistics();


//Initialize the transformationArray buffer to the identity affine transform
{
long ptr = this.uploadStream.upload(this.transformationArray, 0, RegionManager.MAX_TRANSFORMATION_COUNT * (4*4*4));
var transform = new Matrix4f().identity();
for (int i = 0; i < RegionManager.MAX_TRANSFORMATION_COUNT; i++) {
transform.getToAddress(ptr);
ptr += 4*4*4;
}
}

}

//TODO: FIXME: optimize this so that multiple uploads just upload a single time per frame!!!
// THIS IS CRITICAL
public void setTransformation(int id, Matrix4fc transform) {
if (id < 0 || id >= RegionManager.MAX_TRANSFORMATION_COUNT) {
throw new IllegalArgumentException("Id out of bounds: " + id);
}
long ptr = this.uploadStream.upload(this.transformationArray, id * (4*4*4), 4*4*4);
transform.getToAddress(ptr);
}

private int prevRegionCount;
Expand All @@ -118,12 +144,17 @@ public RenderPipeline(RenderDevice device, UploadingBufferStream uploadStream, D
public void renderFrame(Viewport frustum, ChunkRenderMatrices crm, double px, double py, double pz) {//NOTE: can use any of the command list rendering commands to basicly draw X indirects using the same shader, thus allowing for terrain to be rendered very efficently

if (sectionManager.getRegionManager().regionCount() == 0) return;//Dont render anything if there is nothing to render

final int DEBUG_RENDER_LEVEL = 0;//0: no debug, 1: region debug, 2: section debug
final boolean WRITE_DEPTH = false;
//new NvidiumAPI("nvidium").setRegionTransformId(0, 0, 2, 0);
//new NvidiumAPI("nvidium").setTransformation(0, new Matrix4f().identity().scale(1f,1,1));

Vector3i blockPos = new Vector3i(((int)Math.floor(px)), ((int)Math.floor(py)), ((int)Math.floor(pz)));
Vector3i chunkPos = new Vector3i(blockPos.x>>4,blockPos.y>>4,blockPos.z>>4);
// /tp @p 0.0 -1.62 0.0 0 0
//Clear the first gl error, not our fault
glGetError();
int err;
//glGetError();

int visibleRegions = 0;

Expand Down Expand Up @@ -217,6 +248,8 @@ public void renderFrame(Viewport frustum, ChunkRenderMatrices crm, double px, do
addr += 8;
MemoryUtil.memPutLong(addr, sectionManager.terrainAreana.buffer.getDeviceAddress());
addr += 8;
MemoryUtil.memPutLong(addr, this.transformationArray.getDeviceAddress());
addr += 8;
MemoryUtil.memPutLong(addr, statisticsBuffer == null?0:statisticsBuffer.getDeviceAddress());//Logging buffer
addr += 8;
MemoryUtil.memPutFloat(addr, RenderSystem.getShaderFogStart());//FogStart
Expand Down Expand Up @@ -250,9 +283,9 @@ public void renderFrame(Viewport frustum, ChunkRenderMatrices crm, double px, do

TickableManager.TickAll();

if ((err = glGetError()) != 0) {
throw new IllegalStateException("GLERROR: "+err);
}
//if ((err = glGetError()) != 0) {
// throw new IllegalStateException("GLERROR: "+err);
//}


glEnableClientState(GL_UNIFORM_BUFFER_UNIFIED_NV);
Expand All @@ -272,14 +305,35 @@ public void renderFrame(Viewport frustum, ChunkRenderMatrices crm, double px, do
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(false);
glColorMask(false, false, false, false);
glEnable(GL_REPRESENTATIVE_FRAGMENT_TEST_NV);
if (DEBUG_RENDER_LEVEL == 1 && WRITE_DEPTH) {
glDepthMask(true);
}
if (DEBUG_RENDER_LEVEL != 1) {
glColorMask(false, false, false, false);
}
if (DEBUG_RENDER_LEVEL == 0)
{
glEnable(GL_REPRESENTATIVE_FRAGMENT_TEST_NV);
}

regionRasterizer.raster(visibleRegions);

if (DEBUG_RENDER_LEVEL == 1) {
glColorMask(false, false, false, false);
}

//glMemoryBarrier(GL_SHADER_GLOBAL_ACCESS_BARRIER_BIT_NV);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);

//glColorMask(true, true, true, true);

if (DEBUG_RENDER_LEVEL == 2) {
glColorMask(true, true, true, true);
}
if (DEBUG_RENDER_LEVEL == 2 && WRITE_DEPTH) {
glDepthMask(true);
}

sectionRasterizer.raster(visibleRegions);
glDisable(GL_REPRESENTATIVE_FRAGMENT_TEST_NV);
glDepthMask(true);
Expand Down Expand Up @@ -325,9 +379,9 @@ public void renderFrame(Viewport frustum, ChunkRenderMatrices crm, double px, do
glDisable(GL_DEPTH_TEST);


if ((err = glGetError()) != 0) {
throw new IllegalStateException("GLERROR: "+err);
}
//if ((err = glGetError()) != 0) {
// throw new IllegalStateException("GLERROR: "+err);
//}
}

void enqueueRegionSort(int regionId) {
Expand Down Expand Up @@ -414,6 +468,7 @@ public void delete() {
temporalRasterizer.delete();
translucencyTerrainRasterizer.delete();
regionSectionSorter.delete();
this.transformationArray.delete();

if (statisticsBuffer != null) {
statisticsBuffer.delete();
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/me/cortex/nvidium/api0/NvidiumAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import me.cortex.nvidium.Nvidium;
import me.cortex.nvidium.sodiumCompat.INvidiumWorldRendererGetter;
import me.jellysquid.mods.sodium.client.render.SodiumWorldRenderer;
import org.joml.Matrix4fc;
import org.joml.Matrix4x3fc;

public class NvidiumAPI {
private final String modName;
Expand Down Expand Up @@ -36,5 +38,30 @@ public void showSection(int x, int y, int z) {
}
}

/***
* Assigns a specified region to the supplied transformation id
* @param id id to set the region too (all regions have the default id of 0)
* @param x region X pos
* @param y region Y pos
* @param z region Z pos
*/
public void setRegionTransformId(int id, int x, int y, int z) {
if (Nvidium.IS_ENABLED) {
var renderer = ((INvidiumWorldRendererGetter) SodiumWorldRenderer.instance()).getRenderer();
renderer.getSectionManager().getRegionManager().setRegionTransformId(x, y, z, id);
}
}

/***
* Sets the affine transform for the supplied id
* @param id The id to set the transform of
* @param transform The transform to set it too
*/
public void setTransformation(int id, Matrix4fc transform) {
if (Nvidium.IS_ENABLED) {
var renderer = ((INvidiumWorldRendererGetter) SodiumWorldRenderer.instance()).getRenderer();
renderer.setTransformation(id, transform);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,17 @@ private void run() {
List<RenderSection> chunkUpdates = new ArrayList<>();
List<RenderSection> blockEntitySections = new ArrayList<>();
Set<Sprite> animatedSpriteSet = animateVisibleSpritesOnly?new HashSet<>():null;
final Consumer<RenderSection> visitor = section -> {
final OcclusionCuller.Visitor visitor = (section, visible) -> {
if (section.getPendingUpdate() != null && section.getBuildCancellationToken() == null) {
if ((!((IRenderSectionExtension)section).isSubmittedRebuild()) && !((IRenderSectionExtension)section).isSeen()) {//If it is in submission queue or seen dont enqueue
//Set that the section has been seen
((IRenderSectionExtension)section).isSeen(true);
chunkUpdates.add(section);
}
}
if (!visible) {
return;
}
if ((section.getFlags()&(1<<RenderSectionFlags.HAS_BLOCK_ENTITIES))!=0 &&
section.getPosition().isWithinDistance(viewport.getChunkCoord(),33)) {//32 rd max chunk distance
blockEntitySections.add(section);
Expand All @@ -81,13 +91,6 @@ private void run() {
animatedSpriteSet.addAll(List.of(animatedSprites));
}
}
if (section.getPendingUpdate() != null && section.getBuildCancellationToken() == null) {
if ((!((IRenderSectionExtension)section).isSubmittedRebuild()) && !((IRenderSectionExtension)section).isSeen()) {//If it is in submission queue or seen dont enqueue
//Set that the section has been seen
((IRenderSectionExtension)section).isSeen(true);
chunkUpdates.add(section);
}
}
};

frame++;
Expand Down
Loading

0 comments on commit dfe70fe

Please sign in to comment.