Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xCollateral committed Sep 14, 2023
1 parent 2ed323b commit 32725a6
Show file tree
Hide file tree
Showing 52 changed files with 932 additions and 869 deletions.
1 change: 0 additions & 1 deletion src/main/java/net/vulkanmod/Initializer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.vulkanmod;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.vulkanmod.config.Config;
import net.vulkanmod.config.VideoResolution;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/vulkanmod/gl/Util.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package net.vulkanmod.gl;

import net.vulkanmod.vulkan.shader.ShaderSPIRVUtils;
import net.vulkanmod.vulkan.shader.SPIRVUtils;

public class Util {

public static ShaderSPIRVUtils.ShaderKind extToShaderKind(String in) {
public static SPIRVUtils.ShaderKind extToShaderKind(String in) {
return switch (in) {
case ".vsh" -> ShaderSPIRVUtils.ShaderKind.VERTEX_SHADER;
case ".fsh" -> ShaderSPIRVUtils.ShaderKind.FRAGMENT_SHADER;
case ".vsh" -> SPIRVUtils.ShaderKind.VERTEX_SHADER;
case ".fsh" -> SPIRVUtils.ShaderKind.FRAGMENT_SHADER;
default -> throw new RuntimeException("unknown shader type: " + in);
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/vulkanmod/interfaces/ShaderMixed.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.vulkanmod.interfaces;

import net.vulkanmod.vulkan.shader.Pipeline;
import net.vulkanmod.vulkan.shader.GraphicsPipeline;

public interface ShaderMixed {

Pipeline getPipeline();
GraphicsPipeline getPipeline();
}
69 changes: 63 additions & 6 deletions src/main/java/net/vulkanmod/mixin/chunk/LevelRendererMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ public abstract class LevelRendererMixin {
@Shadow private boolean generateClouds;
@Shadow @Final private EntityRenderDispatcher entityRenderDispatcher;

@Shadow protected abstract boolean shouldShowEntityOutlines();

@Shadow public abstract void needsUpdate();

@Shadow public abstract void renderLevel(PoseStack poseStack, float f, long l, boolean bl, Camera camera, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f matrix4f);

private WorldRenderer worldRenderer;

@Unique
private Object2ReferenceOpenHashMap<Class<? extends Entity>, ObjectArrayList<Pair<Entity, MultiBufferSource>>> entitiesMap = new Object2ReferenceOpenHashMap<>();

//TODO clear VBOs

@Inject(method = "<init>", at = @At("RETURN"))
private void init(Minecraft minecraft, EntityRenderDispatcher entityRenderDispatcher, BlockEntityRenderDispatcher blockEntityRenderDispatcher, RenderBuffers renderBuffers, CallbackInfo ci) {
this.worldRenderer = WorldRenderer.init(this.renderBuffers);
Expand Down Expand Up @@ -192,11 +196,10 @@ private void popProfiler3(PoseStack poseStack, float f, long l, boolean bl, Came
*/
@Overwrite
private void renderEntity(Entity entity, double d, double e, double f, float g, PoseStack poseStack, MultiBufferSource multiBufferSource) {
//Entity lists optimization
if(!Initializer.CONFIG.entityCulling) {
double h = Mth.lerp(g, entity.xOld, entity.getX());
double i = Mth.lerp(g, entity.yOld, entity.getY());
double j = Mth.lerp(g, entity.zOld, entity.getZ());
double h = Mth.lerp((double)g, entity.xOld, entity.getX());
double i = Mth.lerp((double)g, entity.yOld, entity.getY());
double j = Mth.lerp((double)g, entity.zOld, entity.getZ());
float k = Mth.lerp(g, entity.yRotO, entity.getYRot());
this.entityRenderDispatcher.render(entity, h - d, i - e, j - f, k, g, poseStack, multiBufferSource, this.entityRenderDispatcher.getPackedLightCoords(entity, g));
return;
Expand Down Expand Up @@ -238,4 +241,58 @@ private void renderEntities(PoseStack poseStack, float partialTicks, long l, boo
}
}

// @Redirect(method = "renderLevel", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/ClientLevel;entitiesForRendering()Ljava/lang/Iterable;"))
// private Iterable<Entity> replaceIterator(ClientLevel instance) {
//
// return () -> new Iterator<Entity>() {
// @Override
// public boolean hasNext() {
// return false;
// }
//
// @Override
// public Entity next() {
// return null;
// }
// };
// }
//
// @Inject(method = "renderLevel", at = @At(value = "INVOKE",
// target = "Lnet/minecraft/client/multiplayer/ClientLevel;entitiesForRendering()Ljava/lang/Iterable;",
// shift = At.Shift.AFTER),
// locals = LocalCapture.CAPTURE_FAILHARD
// )
// private void renderEntities(PoseStack poseStack, float f, long l, boolean bl, Camera camera, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f matrix4f, CallbackInfo ci) {
// for(Entity entity : this.level.entitiesForRendering()) {
// if (this.entityRenderDispatcher.shouldRender(entity, frustum, d0, d1, d2) || entity.hasIndirectPassenger(this.minecraft.player)) {
// BlockPos blockpos = entity.blockPosition();
// if ((this.level.isOutsideBuildHeight(blockpos.getY()) || this.isChunkCompiled(blockpos)) && (entity != p_109604_.getEntity() || p_109604_.isDetached() || p_109604_.getEntity() instanceof LivingEntity && ((LivingEntity)p_109604_.getEntity()).isSleeping()) && (!(entity instanceof LocalPlayer) || p_109604_.getEntity() == entity)) {
// ++this.renderedEntities;
// if (entity.tickCount == 0) {
// entity.xOld = entity.getX();
// entity.yOld = entity.getY();
// entity.zOld = entity.getZ();
// }
//
// MultiBufferSource multibuffersource;
// if (this.shouldShowEntityOutlines() && this.minecraft.shouldEntityAppearGlowing(entity)) {
// flag3 = true;
// OutlineBufferSource outlinebuffersource = this.renderBuffers.outlineBufferSource();
// multibuffersource = outlinebuffersource;
// int i = entity.getTeamColor();
// int j = 255;
// int k = i >> 16 & 255;
// int l = i >> 8 & 255;
// int i1 = i & 255;
// outlinebuffersource.setColor(k, l, i1, 255);
// } else {
// multibuffersource = bu;
// }
//
// this.renderEntity(entity, d0, d1, d2, p_109601_, p_109600_, multibuffersource);
// }
// }
// }
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.minecraft.server.packs.resources.ResourceManager;
import net.vulkanmod.vulkan.shader.Pipeline;
import net.vulkanmod.vulkan.shader.layout.Field;
import net.vulkanmod.vulkan.shader.layout.UBO;
import net.vulkanmod.vulkan.shader.descriptor.UBO;
import net.vulkanmod.vulkan.shader.parser.GlslConverter;
import net.vulkanmod.vulkan.util.MappedBuffer;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -102,7 +102,7 @@ private void createShaders(ResourceManager resourceManager, String vertexShader,
builder.setUniforms(Collections.singletonList(ubo), converter.getSamplerList());
builder.compileShaders(converter.getVshConverted(), converter.getFshConverted());

this.pipeline = builder.createPipeline();
this.pipeline = builder.createGraphicsPipeline();

} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/vulkanmod/mixin/compatibility/ProgramM.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.mojang.blaze3d.preprocessor.GlslPreprocessor;
import com.mojang.blaze3d.shaders.Program;
import net.vulkanmod.gl.Util;
import net.vulkanmod.vulkan.shader.ShaderSPIRVUtils;
import net.vulkanmod.vulkan.shader.SPIRVUtils;
import org.apache.commons.io.IOUtils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
Expand Down Expand Up @@ -37,7 +37,7 @@ public static int compileShaderInternal(Program.Type type, String string, InputS

//TODO
glslPreprocessor.process(string3);
ShaderSPIRVUtils.compileShader(string2 + ":" + string, string3, Util.extToShaderKind(type.getExtension()));
SPIRVUtils.compileShader(string2 + ":" + string, string3, Util.extToShaderKind(type.getExtension()));
}
return 0;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/vulkanmod/mixin/render/BufferUploaderM.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.blaze3d.vertex.BufferUploader;
import net.vulkanmod.interfaces.ShaderMixed;
import net.vulkanmod.vulkan.Renderer;
import net.vulkanmod.vulkan.shader.GraphicsPipeline;
import net.vulkanmod.vulkan.shader.Pipeline;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
Expand Down Expand Up @@ -33,8 +34,8 @@ public static void drawWithShader(BufferBuilder.RenderedBuffer buffer) {
if(parameters.vertexCount() <= 0)
return;

Pipeline pipeline = ((ShaderMixed)(RenderSystem.getShader())).getPipeline();
renderer.bindPipeline(pipeline);
GraphicsPipeline pipeline = ((ShaderMixed)(RenderSystem.getShader())).getPipeline();
renderer.bindGraphicsPipeline(pipeline);
renderer.uploadAndBindUBOs(pipeline);
Renderer.getDrawer().draw(buffer.vertexBuffer(), parameters.mode(), parameters.format(), parameters.vertexCount());
}
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/net/vulkanmod/mixin/render/ShaderInstanceM.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package net.vulkanmod.mixin.render;

import com.google.gson.JsonObject;
import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.shaders.Program;
import com.mojang.blaze3d.shaders.Uniform;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceProvider;
import net.minecraft.util.GsonHelper;
import net.vulkanmod.Initializer;
import net.vulkanmod.interfaces.ShaderMixed;
import net.vulkanmod.vulkan.shader.GraphicsPipeline;
import net.vulkanmod.vulkan.shader.Pipeline;
import net.vulkanmod.vulkan.shader.layout.Field;
import net.vulkanmod.vulkan.shader.layout.UBO;
import net.vulkanmod.vulkan.shader.descriptor.UBO;
import net.vulkanmod.vulkan.shader.parser.GlslConverter;
import net.vulkanmod.vulkan.util.MappedBuffer;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -50,11 +49,11 @@ public class ShaderInstanceM implements ShaderMixed {
@Shadow @Final @Nullable public Uniform PROJECTION_MATRIX;
@Shadow @Final @Nullable public Uniform COLOR_MODULATOR;
@Shadow @Final @Nullable public Uniform LINE_WIDTH;
private Pipeline pipeline;
private GraphicsPipeline pipeline;
boolean isLegacy = false;


public Pipeline getPipeline() {
public GraphicsPipeline getPipeline() {
return pipeline;
}

Expand All @@ -69,7 +68,7 @@ private void create(ResourceProvider resourceProvider, String name, VertexFormat
Pipeline.Builder pipelineBuilder = new Pipeline.Builder(format, path);
pipelineBuilder.parseBindingsJSON();
pipelineBuilder.compileShaders();
this.pipeline = pipelineBuilder.createPipeline();
this.pipeline = pipelineBuilder.createGraphicsPipeline();
}

@Inject(method = "getOrCreate", at = @At("HEAD"), cancellable = true)
Expand Down Expand Up @@ -194,7 +193,7 @@ private void createLegacyShader(ResourceProvider resourceProvider, ResourceLocat
builder.setUniforms(Collections.singletonList(ubo), converter.getSamplerList());
builder.compileShaders(converter.getVshConverted(), converter.getFshConverted());

this.pipeline = builder.createPipeline();
this.pipeline = builder.createGraphicsPipeline();
this.isLegacy = true;

} catch (Throwable throwable) {
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/net/vulkanmod/mixin/texture/MTextureManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.renderer.texture.Tickable;
import net.minecraft.resources.ResourceLocation;
import net.vulkanmod.render.texture.SpriteUtil;
import net.vulkanmod.vulkan.Device;
import net.vulkanmod.vulkan.Renderer;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -31,16 +33,16 @@ public void tick() {
return;

//Debug D
// if(SpriteUtil.shouldUpload())
// GraphicsQueue.getInstance().startRecording();
// for (Tickable tickable : this.tickableTextures) {
// tickable.tick();
// }
// if(SpriteUtil.shouldUpload()) {
// SpriteUtil.transitionLayouts(GraphicsQueue.getInstance().getCommandBuffer());
// GraphicsQueue.getInstance().endRecordingAndSubmit();
//// Synchronization.INSTANCE.waitFences();
// }
if(SpriteUtil.shouldUpload())
Device.getGraphicsQueue().startRecording();
for (Tickable tickable : this.tickableTextures) {
tickable.tick();
}
if(SpriteUtil.shouldUpload()) {
SpriteUtil.transitionLayouts(Device.getGraphicsQueue().getCommandBuffer());
Device.getGraphicsQueue().endRecordingAndSubmit();
// Synchronization.INSTANCE.waitFences();
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/vulkanmod/render/VBO.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.vulkanmod.vulkan.Renderer;
import net.vulkanmod.vulkan.VRenderSystem;
import net.vulkanmod.vulkan.memory.*;
import net.vulkanmod.vulkan.shader.GraphicsPipeline;
import net.vulkanmod.vulkan.shader.Pipeline;
import org.joml.Matrix4f;

Expand Down Expand Up @@ -105,14 +106,14 @@ public void drawWithShader(Matrix4f MV, Matrix4f P, ShaderInstance shader) {
}
}

public void drawWithShader(Matrix4f MV, Matrix4f P, Pipeline pipeline) {
public void drawWithShader(Matrix4f MV, Matrix4f P, GraphicsPipeline pipeline) {
if (this.indexCount != 0) {
RenderSystem.assertOnRenderThread();

VRenderSystem.applyMVP(MV, P);

Renderer renderer = Renderer.getInstance();
renderer.bindPipeline(pipeline);
renderer.bindGraphicsPipeline(pipeline);
renderer.uploadAndBindUBOs(pipeline);

if(indexBuffer != null)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/vulkanmod/render/chunk/AreaBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap;
import net.vulkanmod.render.chunk.util.Util;
import net.vulkanmod.vulkan.Device;
import net.vulkanmod.vulkan.memory.*;
import net.vulkanmod.vulkan.queue.TransferQueue;

Expand Down Expand Up @@ -122,7 +123,7 @@ public Segment reallocate(int uploadSize) {
AreaUploadManager.INSTANCE.waitAllUploads();

//Sync upload
TransferQueue.getInstance().uploadBufferImmediate(this.buffer.getId(), 0, buffer.getId(), 0, this.buffer.getBufferSize());
Device.getTransferQueue().uploadBufferImmediate(this.buffer.getId(), 0, buffer.getId(), 0, this.buffer.getBufferSize());
this.buffer.freeBuffer();
this.buffer = buffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public synchronized void submitUploads() {
if(this.recordedUploads[this.currentFrame].isEmpty())
return;

TransferQueue.getInstance().submitCommands(this.commandBuffers[currentFrame]);
Device.getTransferQueue().submitCommands(this.commandBuffers[currentFrame]);
}

public void uploadAsync(AreaBuffer.Segment uploadSegment, long bufferId, long dstOffset, long bufferSize, ByteBuffer src) {
Validate.isTrue(currentFrame == Renderer.getCurrentFrame());

if(commandBuffers[currentFrame] == null)
this.commandBuffers[currentFrame] = TransferQueue.getInstance().beginCommands();
// this.commandBuffers[currentFrame] = GraphicsQueue.getInstance().beginCommands();
this.commandBuffers[currentFrame] = Device.getTransferQueue().beginCommands();
// this.commandBuffers[currentFrame] = Device.getGraphicsQueue().beginCommands();

StagingBuffer stagingBuffer = Vulkan.getStagingBuffer(this.currentFrame);
stagingBuffer.copyBuffer((int) bufferSize, src);
Expand All @@ -77,7 +77,7 @@ public void copy(Buffer src, Buffer dst) {
}

if(commandBuffers[currentFrame] == null)
this.commandBuffers[currentFrame] = TransferQueue.getInstance().beginCommands();
this.commandBuffers[currentFrame] = Device.getTransferQueue().beginCommands();

TransferQueue.uploadBufferCmd(this.commandBuffers[currentFrame], src.getId(), 0, dst.getId(), 0, src.getBufferSize());
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/net/vulkanmod/render/chunk/DrawBuffers.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.vulkanmod.vulkan.Renderer;
import net.vulkanmod.vulkan.memory.IndirectBuffer;
import net.vulkanmod.vulkan.shader.Pipeline;
import net.vulkanmod.vulkan.shader.ShaderManager;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.vulkan.VkCommandBuffer;
Expand All @@ -19,7 +18,7 @@

public class DrawBuffers {

private static final int VERTEX_SIZE = ShaderManager.TERRAIN_VERTEX_FORMAT.getVertexSize();
private static final int VERTEX_SIZE = TerrainShaderManager.TERRAIN_VERTEX_FORMAT.getVertexSize();
private static final int INDEX_SIZE = Short.BYTES;

private boolean allocated = false;
Expand Down Expand Up @@ -85,7 +84,7 @@ public int buildDrawBatchesIndirect(IndirectBuffer indirectBuffer, ChunkArea chu
terrainRenderType.setCutoutUniform();
boolean isTranslucent = terrainRenderType == TerrainRenderType.TRANSLUCENT;

Pipeline pipeline = ShaderManager.getInstance().getTerrainIndirectShader(renderType);
Pipeline pipeline = TerrainShaderManager.getTerrainIndirectShader(renderType);

if(isTranslucent) {
vkCmdBindIndexBuffer(Renderer.getCommandBuffer(), this.indexBuffer.getId(), 0, VK_INDEX_TYPE_UINT16);
Expand Down Expand Up @@ -168,7 +167,7 @@ public int buildDrawBatchesIndirect(IndirectBuffer indirectBuffer, ChunkArea chu
}

private static void fakeIndirectCmd(VkCommandBuffer commandBuffer, IndirectBuffer indirectBuffer, int drawCount, ByteBuffer offsetBuffer) {
Pipeline pipeline = ShaderManager.shaderManager.terrainDirectShader;
Pipeline pipeline = TerrainShaderManager.getTerrainDirectShader(null);
// Drawer.getInstance().bindPipeline(pipeline);
pipeline.bindDescriptorSets(Renderer.getCommandBuffer(), Renderer.getCurrentFrame());
// pipeline.bindDescriptorSets(Drawer.getCommandBuffer(), WorldRenderer.getInstance().getUniformBuffers(), Drawer.getCurrentFrame());
Expand Down
Loading

0 comments on commit 32725a6

Please sign in to comment.