Skip to content

Commit

Permalink
Fix flipped post effect framebuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
xCollateral committed Dec 21, 2023
1 parent 89786a8 commit f08c62d
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 126 deletions.
241 changes: 117 additions & 124 deletions src/main/java/net/vulkanmod/mixin/compatibility/PostChainM.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.renderer.PostChain;
Expand All @@ -12,17 +11,16 @@
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.ChainedJsonException;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.GsonHelper;
import net.vulkanmod.vulkan.Renderer;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand All @@ -33,25 +31,19 @@ public abstract class PostChainM {

@Shadow @Final private ResourceManager resourceManager;

@Shadow public abstract void addTempTarget(String string, int i, int j);

@Shadow private int screenWidth;

@Shadow private int screenHeight;

@Shadow @Final private Map<String, RenderTarget> customRenderTargets;

@Shadow @Final private RenderTarget screenTarget;

@Shadow @Final private List<PostPass> passes;

@Shadow protected abstract void parseUniformNode(JsonElement jsonElement) throws ChainedJsonException;

@Shadow private float lastStamp;

@Shadow private float time;

@Shadow public abstract void addTempTarget(String string, int i, int j);
@Shadow protected abstract void parseTargetNode(JsonElement jsonElement) throws ChainedJsonException;
@Shadow protected abstract void parseUniformNode(JsonElement jsonElement) throws ChainedJsonException;

// /**
// * @author
Expand Down Expand Up @@ -146,119 +138,119 @@ public abstract class PostChainM {
//
// }

/**
* @author
* @reason
*/
@Overwrite
private void parsePassNode(TextureManager textureManager, JsonElement jsonElement) throws IOException {
JsonObject jsonObject = GsonHelper.convertToJsonObject(jsonElement, "pass");
String string = GsonHelper.getAsString(jsonObject, "name");
String string2 = GsonHelper.getAsString(jsonObject, "intarget");
String string3 = GsonHelper.getAsString(jsonObject, "outtarget");
RenderTarget renderTarget = this.getRenderTarget(string2);
RenderTarget renderTarget2 = this.getRenderTarget(string3);
if (renderTarget == null) {
throw new ChainedJsonException("Input target '" + string2 + "' does not exist");
} else if (renderTarget2 == null) {
throw new ChainedJsonException("Output target '" + string3 + "' does not exist");
} else {
PostPass postPass = this.addPass(string, renderTarget, renderTarget2);
JsonArray jsonArray = GsonHelper.getAsJsonArray(jsonObject, "auxtargets", null);
if (jsonArray != null) {
int i = 0;

for(Iterator var12 = jsonArray.iterator(); var12.hasNext(); ++i) {
JsonElement jsonElement2 = (JsonElement)var12.next();

try {
JsonObject jsonObject2 = GsonHelper.convertToJsonObject(jsonElement2, "auxtarget");
String string4 = GsonHelper.getAsString(jsonObject2, "name");
String string5 = GsonHelper.getAsString(jsonObject2, "id");
boolean bl;
String string6;
if (string5.endsWith(":depth")) {
bl = true;
string6 = string5.substring(0, string5.lastIndexOf(58));
} else {
bl = false;
string6 = string5;
}

RenderTarget renderTarget3 = this.getRenderTarget(string6);
if (renderTarget3 == null) {
if (bl) {
throw new ChainedJsonException("Render target '" + string6 + "' can't be used as depth buffer");
}

ResourceLocation resourceLocation = new ResourceLocation("textures/effect/" + string6 + ".png");
this.resourceManager.getResource(resourceLocation).orElseThrow(() -> {
return new ChainedJsonException("Render target or texture '" + string6 + "' does not exist");
});
RenderSystem.setShaderTexture(0, resourceLocation);
textureManager.bindForSetup(resourceLocation);
AbstractTexture abstractTexture = textureManager.getTexture(resourceLocation);
int j = GsonHelper.getAsInt(jsonObject2, "width");
int k = GsonHelper.getAsInt(jsonObject2, "height");
boolean bl2 = GsonHelper.getAsBoolean(jsonObject2, "bilinear");
if (bl2) {
RenderSystem.texParameter(3553, 10241, 9729);
RenderSystem.texParameter(3553, 10240, 9729);
} else {
RenderSystem.texParameter(3553, 10241, 9728);
RenderSystem.texParameter(3553, 10240, 9728);
}

Objects.requireNonNull(abstractTexture);
postPass.addAuxAsset(string4, abstractTexture::getId, j, k);
} else if (bl) {
Objects.requireNonNull(renderTarget3);
postPass.addAuxAsset(string4, renderTarget3::getDepthTextureId, renderTarget3.width, renderTarget3.height);
} else {
Objects.requireNonNull(renderTarget3);
postPass.addAuxAsset(string4, renderTarget3::getColorTextureId, renderTarget3.width, renderTarget3.height);
}
} catch (Exception e) {
ChainedJsonException chainedJsonException = ChainedJsonException.forException(e);
chainedJsonException.prependJsonKey("auxtargets[" + i + "]");
throw chainedJsonException;
}
}
}

JsonArray jsonArray2 = GsonHelper.getAsJsonArray(jsonObject, "uniforms", null);
if (jsonArray2 != null) {
int l = 0;

for(Iterator<JsonElement> var29 = jsonArray2.iterator(); var29.hasNext(); ++l) {
JsonElement jsonElement3 = var29.next();

try {
this.parseUniformNode(jsonElement3);
} catch (Exception var25) {
ChainedJsonException chainedJsonException2 = ChainedJsonException.forException(var25);
chainedJsonException2.prependJsonKey("uniforms[" + l + "]");
throw chainedJsonException2;
}
}
}

}
}

private RenderTarget getRenderTarget(@Nullable String string) {
if (string == null) {
return null;
} else {
return string.equals("minecraft:main") ? this.screenTarget : this.customRenderTargets.get(string);
}
}

public PostPass addPass(String string, RenderTarget renderTarget, RenderTarget renderTarget2) throws IOException {
PostPass postPass = new PostPass(this.resourceManager, string, renderTarget, renderTarget2);
this.passes.add(this.passes.size(), postPass);
return postPass;
}
// /**
// * @author
// * @reason
// */
// @Overwrite
// private void parsePassNode(TextureManager textureManager, JsonElement jsonElement) throws IOException {
// JsonObject jsonObject = GsonHelper.convertToJsonObject(jsonElement, "pass");
// String string = GsonHelper.getAsString(jsonObject, "name");
// String string2 = GsonHelper.getAsString(jsonObject, "intarget");
// String string3 = GsonHelper.getAsString(jsonObject, "outtarget");
// RenderTarget renderTarget = this.getRenderTarget(string2);
// RenderTarget renderTarget2 = this.getRenderTarget(string3);
// if (renderTarget == null) {
// throw new ChainedJsonException("Input target '" + string2 + "' does not exist");
// } else if (renderTarget2 == null) {
// throw new ChainedJsonException("Output target '" + string3 + "' does not exist");
// } else {
// PostPass postPass = this.addPass(string, renderTarget, renderTarget2);
// JsonArray jsonArray = GsonHelper.getAsJsonArray(jsonObject, "auxtargets", null);
// if (jsonArray != null) {
// int i = 0;
//
// for(Iterator var12 = jsonArray.iterator(); var12.hasNext(); ++i) {
// JsonElement jsonElement2 = (JsonElement)var12.next();
//
// try {
// JsonObject jsonObject2 = GsonHelper.convertToJsonObject(jsonElement2, "auxtarget");
// String string4 = GsonHelper.getAsString(jsonObject2, "name");
// String string5 = GsonHelper.getAsString(jsonObject2, "id");
// boolean bl;
// String string6;
// if (string5.endsWith(":depth")) {
// bl = true;
// string6 = string5.substring(0, string5.lastIndexOf(58));
// } else {
// bl = false;
// string6 = string5;
// }
//
// RenderTarget renderTarget3 = this.getRenderTarget(string6);
// if (renderTarget3 == null) {
// if (bl) {
// throw new ChainedJsonException("Render target '" + string6 + "' can't be used as depth buffer");
// }
//
// ResourceLocation resourceLocation = new ResourceLocation("textures/effect/" + string6 + ".png");
// this.resourceManager.getResource(resourceLocation).orElseThrow(() -> {
// return new ChainedJsonException("Render target or texture '" + string6 + "' does not exist");
// });
// RenderSystem.setShaderTexture(0, resourceLocation);
// textureManager.bindForSetup(resourceLocation);
// AbstractTexture abstractTexture = textureManager.getTexture(resourceLocation);
// int j = GsonHelper.getAsInt(jsonObject2, "width");
// int k = GsonHelper.getAsInt(jsonObject2, "height");
// boolean bl2 = GsonHelper.getAsBoolean(jsonObject2, "bilinear");
// if (bl2) {
// RenderSystem.texParameter(3553, 10241, 9729);
// RenderSystem.texParameter(3553, 10240, 9729);
// } else {
// RenderSystem.texParameter(3553, 10241, 9728);
// RenderSystem.texParameter(3553, 10240, 9728);
// }
//
// Objects.requireNonNull(abstractTexture);
// postPass.addAuxAsset(string4, abstractTexture::getId, j, k);
// } else if (bl) {
// Objects.requireNonNull(renderTarget3);
// postPass.addAuxAsset(string4, renderTarget3::getDepthTextureId, renderTarget3.width, renderTarget3.height);
// } else {
// Objects.requireNonNull(renderTarget3);
// postPass.addAuxAsset(string4, renderTarget3::getColorTextureId, renderTarget3.width, renderTarget3.height);
// }
// } catch (Exception e) {
// ChainedJsonException chainedJsonException = ChainedJsonException.forException(e);
// chainedJsonException.prependJsonKey("auxtargets[" + i + "]");
// throw chainedJsonException;
// }
// }
// }
//
// JsonArray jsonArray2 = GsonHelper.getAsJsonArray(jsonObject, "uniforms", null);
// if (jsonArray2 != null) {
// int l = 0;
//
// for(Iterator<JsonElement> var29 = jsonArray2.iterator(); var29.hasNext(); ++l) {
// JsonElement jsonElement3 = var29.next();
//
// try {
// this.parseUniformNode(jsonElement3);
// } catch (Exception var25) {
// ChainedJsonException chainedJsonException2 = ChainedJsonException.forException(var25);
// chainedJsonException2.prependJsonKey("uniforms[" + l + "]");
// throw chainedJsonException2;
// }
// }
// }
//
// }
// }
//
// private RenderTarget getRenderTarget(@Nullable String string) {
// if (string == null) {
// return null;
// } else {
// return string.equals("minecraft:main") ? this.screenTarget : this.customRenderTargets.get(string);
// }
// }
//
// public PostPass addPass(String string, RenderTarget renderTarget, RenderTarget renderTarget2) throws IOException {
// PostPass postPass = new PostPass(this.resourceManager, string, renderTarget, renderTarget2);
// this.passes.add(this.passes.size(), postPass);
// return postPass;
// }

/**
* @author
Expand All @@ -280,6 +272,7 @@ public void process(float f) {
postPass.process(this.time / 20.0F);
}

Renderer.resetViewport();
}

}
11 changes: 9 additions & 2 deletions src/main/java/net/vulkanmod/mixin/compatibility/PostPassM.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.EffectInstance;
import net.minecraft.client.renderer.PostPass;
import net.vulkanmod.vulkan.Renderer;
import net.vulkanmod.vulkan.VRenderSystem;
import org.joml.Matrix4f;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand Down Expand Up @@ -64,10 +66,14 @@ public void process(float f) {
this.outTarget.clear(Minecraft.ON_OSX);
this.outTarget.bindWrite(false);

this.effect.apply();

VRenderSystem.disableCull();
RenderSystem.depthFunc(519);

Renderer.setViewport(0, this.outTarget.height, this.outTarget.width, -this.outTarget.height);
Renderer.resetScissor();

this.effect.apply();

BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder();
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION);
bufferBuilder.vertex(0.0, 0.0, 500.0).endVertex();
Expand All @@ -87,5 +93,6 @@ public void process(float f) {
}
}

VRenderSystem.enableCull();
}
}
17 changes: 17 additions & 0 deletions src/main/java/net/vulkanmod/vulkan/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,23 @@ public static void setViewport(int x, int y, int width, int height) {
}
}

public static void resetViewport() {
try(MemoryStack stack = stackPush()) {
int width = getSwapChain().getWidth();
int height = getSwapChain().getHeight();

VkViewport.Buffer viewport = VkViewport.malloc(1, stack);
viewport.x(0.0f);
viewport.y(height);
viewport.width(width);
viewport.height(-height);
viewport.minDepth(0.0f);
viewport.maxDepth(1.0f);

vkCmdSetViewport(INSTANCE.currentCmdBuffer, 0, viewport);
}
}

public static void setScissor(int x, int y, int width, int height) {
if(INSTANCE.boundFramebuffer == null)
return;
Expand Down

0 comments on commit f08c62d

Please sign in to comment.