Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xCollateral committed Nov 2, 2023
1 parent efe861c commit 0537d53
Show file tree
Hide file tree
Showing 18 changed files with 280 additions and 225 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ org.gradle.jvmargs=-Xmx3G
fabric_version=0.89.0+1.20.2

# Mod Properties
mod_version = 0.3.5
mod_version = 0.3.5_dev
maven_group = net.vulkanmod
archives_base_name = VulkanMod_1.20
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void tick() {
tickable.tick();
}
if(SpriteUtil.shouldUpload()) {
SpriteUtil.transitionLayouts(Device.getGraphicsQueue().getCommandBuffer());
SpriteUtil.transitionLayouts(Device.getGraphicsQueue().getCommandBuffer().getHandle());
Device.getGraphicsQueue().endRecordingAndSubmit();
// Synchronization.INSTANCE.waitFences();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public ChunkTask.BuildTask createCompileTask(RenderRegionCache renderRegionCache
RenderChunkRegion renderchunkregion = renderRegionCache.createRegion(WorldRenderer.getLevel(), blockpos.offset(-1, -1, -1), blockpos.offset(16, 16, 16), 1);
boolean flag1 = this.compileStatus.compiledSection == CompiledSection.UNCOMPILED;

this.compileStatus.rebuildTask = new ChunkTask.BuildTask(this, renderchunkregion, !flag1);
this.compileStatus.rebuildTask = ChunkTask.createBuildTask(this, renderchunkregion, !flag1);
return this.compileStatus.rebuildTask;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.renderer.RenderType;
import net.vulkanmod.Initializer;
import net.vulkanmod.render.chunk.build.ThreadBuilderPack;
import net.vulkanmod.render.vertex.CustomVertexFormat;
import net.vulkanmod.vulkan.shader.GraphicsPipeline;
import net.vulkanmod.vulkan.shader.Pipeline;
Expand All @@ -26,6 +27,7 @@ public static void init() {
setTerrainVertexFormat(CustomVertexFormat.COMPRESSED_TERRAIN);
createBasicPipelines();
setDefaultShader();
ThreadBuilderPack.defaultTerrainBuilderConstructor();
}

public static void setDefaultShader() {
Expand Down
56 changes: 30 additions & 26 deletions src/main/java/net/vulkanmod/render/chunk/build/ChunkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,31 @@
import net.vulkanmod.Initializer;
import net.vulkanmod.interfaces.VisibilitySetExtended;
import net.vulkanmod.render.chunk.RenderSection;
import net.vulkanmod.render.chunk.TerrainShaderManager;
import net.vulkanmod.render.chunk.WorldRenderer;
import net.vulkanmod.render.vertex.TerrainBufferBuilder;
import net.vulkanmod.render.vertex.TerrainRenderType;
import net.vulkanmod.render.chunk.TerrainShaderManager;

import javax.annotation.Nullable;
import java.util.*;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class ChunkTask {
public abstract class ChunkTask {
private static TaskDispatcher taskDispatcher;

//TODO stats
public static final boolean bench = false;
public static AtomicInteger totalBuildTime = new AtomicInteger(0);
public static AtomicInteger buildCount = new AtomicInteger(0);

public static BuildTask createBuildTask(RenderSection renderSection, RenderChunkRegion renderChunkRegion, boolean highPriority) {
return new BuildTask(renderSection, renderChunkRegion, highPriority);
}

protected AtomicBoolean cancelled = new AtomicBoolean(false);
protected final RenderSection renderSection;
public boolean highPriority = false;
Expand All @@ -49,13 +55,9 @@ public class ChunkTask {
this.renderSection = renderSection;
}

public String name() {
return "generic_chk_task";
}
public abstract String name();

public CompletableFuture<Result> doTask(ThreadBuilderPack builderPack) {
return null;
}
public abstract CompletableFuture<Result> doTask(ThreadBuilderPack builderPack);

public void cancel() {
this.cancelled.set(true);
Expand Down Expand Up @@ -115,9 +117,8 @@ public CompletableFuture<Result> doTask(ThreadBuilderPack chunkBufferBuilderPack
compiledChunk.renderableBlockEntities.addAll(compileResults.blockEntities);
compiledChunk.transparencyState = compileResults.transparencyState;

if(!compileResults.renderedLayers.isEmpty()) {
if(!compileResults.renderedLayers.isEmpty())
compiledChunk.isCompletelyEmpty = false;
}

taskDispatcher.scheduleSectionUpdate(renderSection, compileResults.renderedLayers);
compiledChunk.renderTypes.addAll(compileResults.renderedLayers.keySet());
Expand All @@ -141,9 +142,9 @@ public CompletableFuture<Result> doTask(ThreadBuilderPack chunkBufferBuilderPack
private CompileResults compile(float camX, float camY, float camZ, ThreadBuilderPack chunkBufferBuilderPack) {
CompileResults compileResults = new CompileResults();

BlockPos blockPos = new BlockPos(renderSection.xOffset(), renderSection.yOffset(), renderSection.zOffset()).immutable();
BlockPos startBlockPos = new BlockPos(renderSection.xOffset(), renderSection.yOffset(), renderSection.zOffset()).immutable();

BlockPos blockPos2 = blockPos.offset(15, 15, 15);
BlockPos endBlockPos = startBlockPos.offset(15, 15, 15);
VisGraph visGraph = new VisGraph();
RenderChunkRegion renderChunkRegion = this.region;
this.region = null;
Expand All @@ -154,21 +155,21 @@ private CompileResults compile(float camX, float camY, float camZ, ThreadBuilder
RandomSource randomSource = RandomSource.create();
BlockRenderDispatcher blockRenderDispatcher = Minecraft.getInstance().getBlockRenderer();

for(BlockPos blockPos3 : BlockPos.betweenClosed(blockPos, blockPos2)) {
BlockState blockState = renderChunkRegion.getBlockState(blockPos3);
if (blockState.isSolidRender(renderChunkRegion, blockPos3)) {
visGraph.setOpaque(blockPos3);
for(BlockPos blockPos : BlockPos.betweenClosed(startBlockPos, endBlockPos)) {
BlockState blockState = renderChunkRegion.getBlockState(blockPos);
if (blockState.isSolidRender(renderChunkRegion, blockPos)) {
visGraph.setOpaque(blockPos);
}

if (blockState.hasBlockEntity()) {
BlockEntity blockEntity = renderChunkRegion.getBlockEntity(blockPos3);
BlockEntity blockEntity = renderChunkRegion.getBlockEntity(blockPos);
if (blockEntity != null) {
this.handleBlockEntity(compileResults, blockEntity);
}
}

BlockState blockState2 = renderChunkRegion.getBlockState(blockPos3);
FluidState fluidState = blockState2.getFluidState();
// BlockState blockState2 = renderChunkRegion.getBlockState(blockPos);
FluidState fluidState = blockState.getFluidState();
RenderType renderType;
TerrainBufferBuilder bufferBuilder;
if (!fluidState.isEmpty()) {
Expand All @@ -182,7 +183,9 @@ private CompileResults compile(float camX, float camY, float camZ, ThreadBuilder
bufferBuilder.begin(VertexFormat.Mode.QUADS, TerrainShaderManager.TERRAIN_VERTEX_FORMAT);
}

blockRenderDispatcher.renderLiquid(blockPos3, renderChunkRegion, bufferBuilder, blockState2, fluidState);
bufferBuilder.setBlockAttributes(fluidState.createLegacyBlock());

blockRenderDispatcher.renderLiquid(blockPos, renderChunkRegion, bufferBuilder, blockState, fluidState);
}

if (blockState.getRenderShape() != RenderShape.INVISIBLE) {
Expand All @@ -196,17 +199,19 @@ private CompileResults compile(float camX, float camY, float camZ, ThreadBuilder
bufferBuilder.begin(VertexFormat.Mode.QUADS, TerrainShaderManager.TERRAIN_VERTEX_FORMAT);
}

bufferBuilder.setBlockAttributes(blockState);

poseStack.pushPose();
poseStack.translate(blockPos3.getX() & 15, blockPos3.getY() & 15, blockPos3.getZ() & 15);
blockRenderDispatcher.renderBatched(blockState, blockPos3, renderChunkRegion, poseStack, bufferBuilder, true, randomSource);
poseStack.translate(blockPos.getX() & 15, blockPos.getY() & 15, blockPos.getZ() & 15);
blockRenderDispatcher.renderBatched(blockState, blockPos, renderChunkRegion, poseStack, bufferBuilder, true, randomSource);
poseStack.popPose();
}
}

if (set.contains(RenderType.translucent())) {
TerrainBufferBuilder bufferBuilder2 = chunkBufferBuilderPack.builder(RenderType.translucent());
if (!bufferBuilder2.isCurrentBatchEmpty()) {
bufferBuilder2.setQuadSortOrigin(camX - (float)blockPos.getX(), camY - (float)blockPos.getY(), camZ - (float)blockPos.getZ());
bufferBuilder2.setQuadSortOrigin(camX - (float)startBlockPos.getX(), camY - (float)startBlockPos.getY(), camZ - (float)startBlockPos.getZ());
compileResults.transparencyState = bufferBuilder2.getSortState();
}
}
Expand Down Expand Up @@ -329,6 +334,5 @@ public CompletableFuture<Result> doTask(ThreadBuilderPack builderPack) {
public enum Result {
CANCELLED,
SUCCESSFUL;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import java.util.Queue;

public class TaskDispatcher {
private static final Logger LOGGER = LogUtils.getLogger();

private int highPriorityQuota = 2;

private final Queue<Runnable> toUpload = Queues.newLinkedBlockingDeque();
Expand Down Expand Up @@ -84,10 +82,10 @@ public void schedule(ChunkTask chunkTask) {
return;

if (chunkTask.highPriority) {
this.highPriorityTasks.offer(chunkTask);
} else {
this.lowPriorityTasks.offer(chunkTask);
}
this.highPriorityTasks.offer(chunkTask);
} else {
this.lowPriorityTasks.offer(chunkTask);
}

synchronized (this) {
notify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,27 @@
import net.vulkanmod.render.vertex.TerrainBufferBuilder;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ThreadBuilderPack {
private final Map<RenderType, TerrainBufferBuilder> builders = RenderType.chunkBufferLayers().stream().collect(Collectors.toMap(
(renderType) -> renderType,
(renderType) -> new TerrainBufferBuilder(renderType.bufferSize())));
private static Function<RenderType, TerrainBufferBuilder> terrainBuilderConstructor;

public static void defaultTerrainBuilderConstructor() {
terrainBuilderConstructor = renderType -> new TerrainBufferBuilder(renderType.bufferSize());
}

public static void setTerrainBuilderConstructor(Function<RenderType, TerrainBufferBuilder> constructor) {
terrainBuilderConstructor = constructor;
}

private final Map<RenderType, TerrainBufferBuilder> builders;

public ThreadBuilderPack() {
builders = RenderType.chunkBufferLayers().stream().collect(Collectors.toMap(
(renderType) -> renderType,
renderType -> terrainBuilderConstructor.apply(renderType)));
}

public TerrainBufferBuilder builder(RenderType renderType) {
return this.builders.get(renderType);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/vulkanmod/render/texture/SpriteUtil.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.vulkanmod.render.texture;

import net.vulkanmod.vulkan.queue.CommandPool;
import net.vulkanmod.vulkan.texture.VulkanImage;
import org.lwjgl.vulkan.VkCommandBuffer;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -24,7 +24,7 @@ public static void addTransitionedLayout(VulkanImage image) {
transitionedLayouts.add(image);
}

public static void transitionLayouts(CommandPool.CommandBuffer commandBuffer) {
public static void transitionLayouts(VkCommandBuffer commandBuffer) {
transitionedLayouts.forEach(image -> image.readOnlyLayout(commandBuffer));

transitionedLayouts.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.blaze3d.vertex.*;
import com.mojang.logging.LogUtils;
import net.minecraft.util.Mth;
import net.minecraft.world.level.block.state.BlockState;
import net.vulkanmod.render.util.SortUtil;
import net.vulkanmod.render.chunk.TerrainShaderManager;
import org.apache.commons.lang3.mutable.MutableInt;
Expand All @@ -18,16 +19,16 @@
import java.util.function.IntConsumer;

public class TerrainBufferBuilder implements VertexConsumer {
private static final float POS_CONV = 1900.0f;
private static final float UV_CONV = 65536.0f;
protected static final float POS_CONV = 1900.0f;
protected static final float UV_CONV = 65536.0f;

private static final int GROWTH_SIZE = 2097152;
private static final Logger LOGGER = LogUtils.getLogger();

private ByteBuffer buffer;
private int renderedBufferCount;
private int renderedBufferPointer;
private int nextElementByte;
protected int nextElementByte;
private int vertices;
@Nullable
private VertexFormatElement currentElement;
Expand All @@ -44,10 +45,10 @@ public class TerrainBufferBuilder implements VertexConsumer {
private float sortZ = Float.NaN;
private boolean indexOnly;

private long bufferPtr;
protected long bufferPtr;
// private long ptr;

VertexBuilder vertexBuilder;
protected VertexBuilder vertexBuilder;

public TerrainBufferBuilder(int i) {
this.buffer = MemoryTracker.create(i * 6);
Expand Down Expand Up @@ -342,58 +343,7 @@ public void vertex(float x, float y, float z, float red, float green, float blue
this.vertexBuilder.vertex(x, y, z, red, green, blue, alpha, u, v, overlay, light, normalX, normalY, normalZ);
}

private void defaultVertex(float x, float y, float z, float red, float green, float blue, float alpha, float u, float v, int overlay, int light, float normalX, float normalY, float normalZ) {
this.putFloat(0, x);
this.putFloat(4, y);
this.putFloat(8, z);
this.putByte(12, (byte)((int)(red * 255.0F)));
this.putByte(13, (byte)((int)(green * 255.0F)));
this.putByte(14, (byte)((int)(blue * 255.0F)));
this.putByte(15, (byte)((int)(alpha * 255.0F)));
this.putFloat(16, u);
this.putFloat(20, v);
byte i;
i = 24;

this.putShort(i, (short)(light & '\uffff'));
this.putShort(i + 2, (short)(light >> 16 & '\uffff'));
this.putByte(i + 4, BufferVertexConsumer.normalIntValue(normalX));
this.putByte(i + 5, BufferVertexConsumer.normalIntValue(normalY));
this.putByte(i + 6, BufferVertexConsumer.normalIntValue(normalZ));
this.nextElementByte += i + 8;
this.endVertex();
}

private void compressedVertex(float x, float y, float z, float red, float green, float blue, float alpha, float u, float v, int light) {
long ptr = this.bufferPtr + this.nextElementByte;

short sX = (short) (x * POS_CONV + 0.1f);
short sY = (short) (y * POS_CONV + 0.1f);
short sZ = (short) (z * POS_CONV + 0.1f);

//Debug
// short x1 = (short) Math.round((x) * POS_CONV);
// float y1 = (short) Math.round((y) * POS_CONV);
// float z1 = (short) Math.round((z) * POS_CONV);
//
// if(x1 != sX || y1 != sY || z1 != sZ)
// System.nanoTime();

MemoryUtil.memPutShort(ptr + 0, sX);
MemoryUtil.memPutShort(ptr + 2, sY);
MemoryUtil.memPutShort(ptr + 4, sZ);

int temp = VertexUtil.packColor(red, green, blue, alpha);
MemoryUtil.memPutInt(ptr + 8, temp);

MemoryUtil.memPutShort(ptr + 12, (short) (u * UV_CONV));
MemoryUtil.memPutShort(ptr + 14, (short) (v * UV_CONV));

MemoryUtil.memPutInt(ptr + 16, light);

this.nextElementByte += 20;
this.endVertex();
}
public void setBlockAttributes(BlockState blockState) {}

@Override
public VertexConsumer vertex(double d, double e, double f) {
Expand Down Expand Up @@ -602,7 +552,7 @@ public boolean sequentialIndex() {
}
}

interface VertexBuilder {
public interface VertexBuilder {
void vertex(float x, float y, float z, float red, float green, float blue, float alpha, float u, float v, int overlay, int light, float normalX, float normalY, float normalZ);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static void addImage(VulkanImage image) {
images.putIfAbsent(image.getId(), image);
}

public void MapAndCopy(long allocation, long bufferSize, Consumer<PointerBuffer> consumer){
public static void MapAndCopy(long allocation, Consumer<PointerBuffer> consumer){

try(MemoryStack stack = stackPush()) {
PointerBuffer data = stack.mallocPointer(1);
Expand Down
Loading

0 comments on commit 0537d53

Please sign in to comment.