Skip to content

Commit

Permalink
Minor Refactors/Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thr3343 committed Dec 18, 2023
1 parent 33a6964 commit d7b85a2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 62 deletions.
2 changes: 0 additions & 2 deletions src/main/java/net/vulkanmod/render/chunk/WorldRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,6 @@ else if(terrainRenderType.equals(TRANSLUCENT))
GraphicsPipeline terrainShader = TerrainShaderManager.getTerrainShader(terrainRenderType);
Renderer.getInstance().bindGraphicsPipeline(terrainShader);
Renderer.getDrawer().bindAutoIndexBuffer(commandBuffer, 7);

terrainRenderType.setCutoutUniform();
terrainShader.bindDescriptorSets(commandBuffer, currentFrame);

final long layout = terrainShader.getLayout();
Expand Down
57 changes: 28 additions & 29 deletions src/main/java/net/vulkanmod/render/chunk/build/ChunkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@
import net.vulkanmod.render.vertex.TerrainRenderType;

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

import static net.vulkanmod.render.vertex.TerrainRenderType.*;

public abstract class ChunkTask {
private static TaskDispatcher taskDispatcher;

Expand Down Expand Up @@ -68,6 +67,8 @@ public static void setTaskDispatcher(TaskDispatcher dispatcher) {
}

public static class BuildTask extends ChunkTask {
private final BlockPos startBlockPos;
private final Iterable<BlockPos> blockPos;
@Nullable
protected RenderChunkRegion region;

Expand All @@ -79,6 +80,9 @@ public BuildTask(RenderSection renderSection, RenderChunkRegion renderChunkRegio
super(renderSection);
this.region = renderChunkRegion;
this.highPriority = highPriority;
startBlockPos = new BlockPos(this.renderSection.xOffset(), this.renderSection.yOffset(), this.renderSection.zOffset()).immutable();
BlockPos endBlockPos = startBlockPos.offset(15, 15, 15);
blockPos = BlockPos.betweenClosed(startBlockPos, endBlockPos);
}

public String name() {
Expand Down Expand Up @@ -142,20 +146,17 @@ public CompletableFuture<Result> doTask(ThreadBuilderPack chunkBufferBuilderPack
private CompileResults compile(float camX, float camY, float camZ, ThreadBuilderPack chunkBufferBuilderPack) {
CompileResults compileResults = new CompileResults();

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

BlockPos endBlockPos = startBlockPos.offset(15, 15, 15);
VisGraph visGraph = new VisGraph();
RenderChunkRegion renderChunkRegion = this.region;
this.region = null;
PoseStack poseStack = new PoseStack();
if (renderChunkRegion != null) {
ModelBlockRenderer.enableCaching();
Set<RenderType> set = new ReferenceArraySet<>(RenderType.chunkBufferLayers().size());
final EnumSet<TerrainRenderType> set = EnumSet.noneOf(TerrainRenderType.class);
RandomSource randomSource = RandomSource.create();
BlockRenderDispatcher blockRenderDispatcher = Minecraft.getInstance().getBlockRenderer();

for(BlockPos blockPos : BlockPos.betweenClosed(startBlockPos, endBlockPos)) {
for(BlockPos blockPos : blockPos) {
BlockState blockState = renderChunkRegion.getBlockState(blockPos);
if (blockState.isSolidRender(renderChunkRegion, blockPos)) {
visGraph.setOpaque(blockPos);
Expand All @@ -170,13 +171,13 @@ private CompileResults compile(float camX, float camY, float camZ, ThreadBuilder

// BlockState blockState2 = renderChunkRegion.getBlockState(blockPos);
FluidState fluidState = blockState.getFluidState();
RenderType renderType;
TerrainRenderType renderType;
TerrainBufferBuilder bufferBuilder;
if (!fluidState.isEmpty()) {
renderType = ItemBlockRenderTypes.getRenderLayer(fluidState);

//Force compact RenderType
renderType = compactRenderTypes(renderType);
renderType = TerrainRenderType.getCompact(ItemBlockRenderTypes.getRenderLayer(fluidState).name);


bufferBuilder = chunkBufferBuilderPack.builder(renderType);
if (set.add(renderType)) {
Expand All @@ -189,10 +190,10 @@ private CompileResults compile(float camX, float camY, float camZ, ThreadBuilder
}

if (blockState.getRenderShape() != RenderShape.INVISIBLE) {
renderType = ItemBlockRenderTypes.getChunkRenderType(blockState);

//Force compact RenderType
renderType = compactRenderTypes(renderType);
renderType = TerrainRenderType.getCompact(ItemBlockRenderTypes.getChunkRenderType(blockState).name);


bufferBuilder = chunkBufferBuilderPack.builder(renderType);
if (set.add(renderType)) {
Expand All @@ -208,19 +209,18 @@ private CompileResults compile(float camX, float camY, float camZ, ThreadBuilder
}
}

if (set.contains(RenderType.translucent())) {
TerrainBufferBuilder bufferBuilder2 = chunkBufferBuilderPack.builder(RenderType.translucent());
if (set.contains(TRANSLUCENT)) {
TerrainBufferBuilder bufferBuilder2 = chunkBufferBuilderPack.builder(TRANSLUCENT);
if (!bufferBuilder2.isCurrentBatchEmpty()) {
bufferBuilder2.setQuadSortOrigin(camX - (float)startBlockPos.getX(), camY - (float)startBlockPos.getY(), camZ - (float)startBlockPos.getZ());
bufferBuilder2.setQuadSortOrigin(camX - (float) startBlockPos.getX(), camY - (float) startBlockPos.getY(), camZ - (float) startBlockPos.getZ());
compileResults.transparencyState = bufferBuilder2.getSortState();
}
}

for(RenderType renderType2 : set) {
for(TerrainRenderType renderType2 : set) {
TerrainBufferBuilder.RenderedBuffer renderedBuffer = chunkBufferBuilderPack.builder(renderType2).endOrDiscardIfEmpty();
if (renderedBuffer != null) {
UploadBuffer uploadBuffer = new UploadBuffer(renderedBuffer);
compileResults.renderedLayers.put(TerrainRenderType.get(renderType2.name), uploadBuffer);
compileResults.renderedLayers.put(renderType2, new UploadBuffer(renderedBuffer));
}

if(renderedBuffer != null)
Expand All @@ -234,16 +234,16 @@ private CompileResults compile(float camX, float camY, float camZ, ThreadBuilder
return compileResults;
}

private RenderType compactRenderTypes(RenderType renderType) {
private TerrainRenderType compactRenderTypes(TerrainRenderType renderType) {

if(Initializer.CONFIG.uniqueOpaqueLayer) {
if (renderType != RenderType.translucent()) {
renderType = renderType == RenderType.tripwire() ? RenderType.translucent() : RenderType.cutoutMipped();
if (renderType != TRANSLUCENT) {
renderType = renderType == TRIPWIRE ? TRANSLUCENT : CUTOUT_MIPPED;
}
}
else {
if (renderType != RenderType.translucent() && renderType != RenderType.cutout()) {
renderType = renderType == RenderType.tripwire() ? RenderType.translucent() : RenderType.cutoutMipped();
if (renderType != TRANSLUCENT && renderType != CUTOUT) {
renderType = renderType == TRIPWIRE ? TRANSLUCENT : CUTOUT_MIPPED;
}
}

Expand Down Expand Up @@ -298,8 +298,8 @@ public CompletableFuture<Result> doTask(ThreadBuilderPack builderPack) {
float f1 = (float)vec3.y;
float f2 = (float)vec3.z;
TerrainBufferBuilder.SortState transparencyState = this.compiledSection.transparencyState;
if (transparencyState != null && this.compiledSection.renderTypes.contains(TerrainRenderType.TRANSLUCENT)) {
TerrainBufferBuilder bufferbuilder = builderPack.builder(RenderType.translucent());
if (transparencyState != null && this.compiledSection.renderTypes.contains(TRANSLUCENT)) {
TerrainBufferBuilder bufferbuilder = builderPack.builder(TRANSLUCENT);
bufferbuilder.begin(VertexFormat.Mode.QUADS, TerrainShaderManager.TERRAIN_VERTEX_FORMAT);
bufferbuilder.restoreSortState(transparencyState);
// bufferbuilder.setQuadSortOrigin(f - (float) this.renderSection.origin.getX(), f1 - (float) renderSection.origin.getY(), f2 - (float) renderSection.origin.getZ());
Expand All @@ -310,8 +310,7 @@ public CompletableFuture<Result> doTask(ThreadBuilderPack builderPack) {
return CompletableFuture.completedFuture(Result.CANCELLED);
} else {

UploadBuffer uploadBuffer = new UploadBuffer(renderedBuffer);
taskDispatcher.scheduleUploadChunkLayer(renderSection, TerrainRenderType.TRANSLUCENT, uploadBuffer);
taskDispatcher.scheduleUploadChunkLayer(renderSection, TRANSLUCENT, new UploadBuffer(renderedBuffer));
renderedBuffer.release();
return CompletableFuture.completedFuture(Result.SUCCESSFUL);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package net.vulkanmod.render.chunk.build;

import net.minecraft.client.renderer.RenderType;
import net.vulkanmod.render.vertex.TerrainBufferBuilder;
import net.vulkanmod.render.vertex.TerrainRenderType;

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

public class ThreadBuilderPack {
private static Function<RenderType, TerrainBufferBuilder> terrainBuilderConstructor;
private static Function<TerrainRenderType, TerrainBufferBuilder> terrainBuilderConstructor;

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

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

private final Map<RenderType, TerrainBufferBuilder> builders;
private final EnumMap<TerrainRenderType, TerrainBufferBuilder> builders=new EnumMap<>(TerrainRenderType.class);;

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

public TerrainBufferBuilder builder(RenderType renderType) {
public TerrainBufferBuilder builder(TerrainRenderType renderType) {
return this.builders.get(renderType);
}

Expand Down
50 changes: 30 additions & 20 deletions src/main/java/net/vulkanmod/render/vertex/TerrainRenderType.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,62 @@
package net.vulkanmod.render.vertex;

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.client.renderer.RenderType;
import net.vulkanmod.Initializer;
import net.vulkanmod.vulkan.VRenderSystem;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.stream.Collectors;

public enum TerrainRenderType {
SOLID(RenderType.solid(), 0.0f),
CUTOUT_MIPPED(RenderType.cutoutMipped(), 0.5f),
CUTOUT(RenderType.cutout(), 0.1f),
TRANSLUCENT(RenderType.translucent(), 0.0f),
TRIPWIRE(RenderType.tripwire(), 0.1f);
SOLID(RenderType.solid()),
CUTOUT_MIPPED(RenderType.cutoutMipped()),
CUTOUT(RenderType.cutout()),
TRANSLUCENT(RenderType.translucent()),
TRIPWIRE(RenderType.tripwire());

public static final TerrainRenderType[] VALUES = TerrainRenderType.values();

public static final EnumSet<TerrainRenderType> COMPACT_RENDER_TYPES = EnumSet.of(CUTOUT_MIPPED, TRANSLUCENT);
public static final EnumSet<TerrainRenderType> SEMI_COMPACT_RENDER_TYPES = EnumSet.of(CUTOUT_MIPPED, CUTOUT, TRANSLUCENT);

public final int initialSize;
final float alphaCutout;

TerrainRenderType(RenderType renderType, float alphaCutout) {
this.initialSize = renderType.bufferSize();
this.alphaCutout = alphaCutout;
public final int bufferSize;
TerrainRenderType(RenderType renderType) {
this.bufferSize = renderType.bufferSize();
}
public static EnumSet<TerrainRenderType> getActiveLayers() {
return Initializer.CONFIG.uniqueOpaqueLayer ? COMPACT_RENDER_TYPES : SEMI_COMPACT_RENDER_TYPES;
}

public void setCutoutUniform() {
VRenderSystem.alphaCutout = this.alphaCutout;
public static TerrainRenderType getCompact(String renderType) {
if(Initializer.CONFIG.uniqueOpaqueLayer) {
return switch (renderType)
{
case "solid", "cutout", "cutout_mipped" -> CUTOUT_MIPPED;
default -> TRANSLUCENT;
};

}
else {
return switch (renderType)
{
case "solid", "cutout_mipped" -> CUTOUT_MIPPED;
case "cutout" -> CUTOUT;
default -> TRANSLUCENT;
};
}


}



public static TerrainRenderType get(String renderType) {
return switch (renderType)
{
case "solid" -> SOLID;
case "cutout_mipped" -> CUTOUT_MIPPED;
case "cutout" -> CUTOUT;
case "translucent" -> TRANSLUCENT;
case "tripwire" -> TRIPWIRE;
default -> throw new IllegalStateException("Unexpected value: " + renderType);
default -> TRANSLUCENT;
};
}
}

0 comments on commit d7b85a2

Please sign in to comment.