Skip to content

Commit b6359a5

Browse files
committed
oit...again
1 parent ab0a61f commit b6359a5

15 files changed

Lines changed: 304 additions & 368 deletions

src/main/java/cn/ussshenzhou/madparticle/designer/gui/panel/SettingPanel.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ public class SettingPanel extends TOptionsPanel {
3030

3131
@SuppressWarnings({"rawtypes", "unchecked"})
3232
public SettingPanel() {
33+
var cfg = getConfigRead();
3334
addOptionSplitter(Component.translatable("gui.mp.de.setting.universal"));
3435
addOptionSliderDoubleInit(Component.translatable("gui.mp.de.setting.amount"),
3536
//FIXME target value < actual value
36-
0x2000, Math.max(1000000, getConfigRead().maxParticleAmountOfSingleQueue),
37+
0x2000, Math.max(1000000, cfg.maxParticleAmountOfSingleQueue),
3738
(component, aDouble) -> Component.literal(String.format("%d", aDouble.intValue())),
3839
Component.translatable("gui.mp.de.setting.amount.tooltip"),
3940
(s, d) -> {
@@ -45,15 +46,15 @@ public SettingPanel() {
4546
newQueue.addAll(particleGroup.particles);
4647
particleGroup.particles = (Queue) newQueue;
4748
});
48-
}, getConfigRead().maxParticleAmountOfSingleQueue, false);
49+
}, cfg.maxParticleAmountOfSingleQueue, false);
4950
addOptionCycleButtonInit(Component.translatable("gui.mp.de.setting.real_force"),
5051
List.of(Boolean.TRUE, Boolean.FALSE),
5152
List.of(b -> ConfigHelper.getConfigWrite(MadParticleConfig.class, madParticleConfig -> madParticleConfig.limitMaxParticleGenerateDistance = true),
5253
b -> ConfigHelper.getConfigWrite(MadParticleConfig.class, madParticleConfig -> madParticleConfig.limitMaxParticleGenerateDistance = false)),
53-
e -> e.getContent() == getConfigRead().limitMaxParticleGenerateDistance
54+
e -> e.getContent() == cfg.limitMaxParticleGenerateDistance
5455
)
5556
.getSecond().setTooltip(Tooltip.create(Component.translatable("gui.mp.de.setting.real_force.tooltip")));
56-
int amount = getConfigRead().getBufferFillerThreads();
57+
int amount = cfg.getBufferFillerThreads();
5758
addOptionCycleButtonInit(Component.translatable("gui.mp.de.setting.threads"),
5859
//FIXME
5960
Sets.newLinkedHashSet(List.of("gui.mp.de.setting.threads.zero", 2, 4, 6, 8, 12, 16, amount == 1 ? 6 : amount)).stream().toList(),
@@ -70,25 +71,25 @@ public SettingPanel() {
7071
method -> b -> {
7172
ConfigHelper.getConfigWrite(MadParticleConfig.class, madParticleConfig -> madParticleConfig.translucentMethod = method);
7273
b.setTooltip(Tooltip.create(Component.translatable(method.translateKey() + ".tooltip")));
73-
}, entry -> entry.getContent() == getConfigRead().translucentMethod
74-
);
74+
}, entry -> entry.getContent() == cfg.translucentMethod
75+
).getSecond().setTooltip(Tooltip.create(Component.translatable(cfg.translucentMethod.translateKey() + ".tooltip")));
7576

7677

7778
addOptionSplitter(Component.translatable("gui.mp.de.setting.additional"));
7879
addOptionCycleButtonInit(Component.translatable("gui.mp.de.setting.additional.takeover_render"),
7980
List.of(TakeOver.values()), takeOver -> b -> ConfigHelper.getConfigWrite(MadParticleConfig.class, madParticleConfig -> madParticleConfig.takeOverRendering = b.getSelected().getContent()),
80-
entry -> entry.getContent() == getConfigRead().takeOverRendering
81+
entry -> entry.getContent() == cfg.takeOverRendering
8182
)
8283
.getSecond().setTooltip(Tooltip.create(Component.translatable("gui.mp.de.setting.additional.takeover_render.tooltip")));
8384
addOptionCycleButtonInit(Component.translatable("gui.mp.de.setting.additional.takeover_tick"),
8485
List.of(TakeOver.values()), takeOver -> b -> ConfigHelper.getConfigWrite(MadParticleConfig.class, madParticleConfig -> madParticleConfig.takeOverTicking = b.getSelected().getContent()),
85-
entry -> entry.getContent() == getConfigRead().takeOverTicking
86+
entry -> entry.getContent() == cfg.takeOverTicking
8687
)
8788
.getSecond().setTooltip(Tooltip.create(Component.translatable("gui.mp.de.setting.additional.takeover_tick.tooltip")));
8889
addOptionCycleButtonInit(Component.translatable("gui.mp.de.setting.additional.optimize_command_block"),
8990
List.of(Boolean.TRUE, Boolean.FALSE),
9091
bool -> b -> ConfigHelper.getConfigWrite(MadParticleConfig.class, madParticleConfig -> madParticleConfig.optimizeCommandBlockEditScreen = bool),
91-
entry -> entry.getContent() == getConfigRead().optimizeCommandBlockEditScreen
92+
entry -> entry.getContent() == cfg.optimizeCommandBlockEditScreen
9293
);
9394
addOptionCycleButton(Component.translatable("gui.mp.de.setting.additional.counter"),
9495
List.of(Boolean.FALSE, Boolean.TRUE),
@@ -103,26 +104,26 @@ public SettingPanel() {
103104

104105
addOptionSplitter(Component.translatable("gui.mp.de.setting.light"));
105106
addOptionCycleButtonInit(Component.translatable("gui.mp.de.setting.light.hor"),
106-
Sets.newLinkedHashSet(List.of(16, 64, 128, 256, 512, getConfigRead().lightCacheXZRange)).stream().toList(),
107+
Sets.newLinkedHashSet(List.of(16, 64, 128, 256, 512, cfg.lightCacheXZRange)).stream().toList(),
107108
integer -> i -> ConfigHelper.getConfigWrite(MadParticleConfig.class, madParticleConfig -> madParticleConfig.lightCacheXZRange = i.getSelected().getContent()),
108-
entry -> entry.getContent() == getConfigRead().lightCacheXZRange).getSecond()
109+
entry -> entry.getContent() == cfg.lightCacheXZRange).getSecond()
109110
.setTooltip(Tooltip.create(Component.translatable("gui.mp.de.setting.light.hor.tooltip")));
110111
addOptionCycleButtonInit(Component.translatable("gui.mp.de.setting.light.ver"),
111-
Sets.newLinkedHashSet(List.of(16, 64, 128, 256, 512, getConfigRead().lightCacheYRange)).stream().toList(),
112+
Sets.newLinkedHashSet(List.of(16, 64, 128, 256, 512, cfg.lightCacheYRange)).stream().toList(),
112113
integer -> i -> ConfigHelper.getConfigWrite(MadParticleConfig.class, madParticleConfig -> madParticleConfig.lightCacheYRange = i.getSelected().getContent()),
113-
entry -> entry.getContent() == getConfigRead().lightCacheYRange).getSecond()
114+
entry -> entry.getContent() == cfg.lightCacheYRange).getSecond()
114115
.setTooltip(Tooltip.create(Component.translatable("gui.mp.de.setting.light.ver.tooltip")));
115116
ramUsage = addOption(Component.translatable("gui.mp.de.setting.light.ram"), new TLabel()).getSecond()
116117
.setHorizontalAlignment(HorizontalAlignment.LEFT);
117118
addOptionCycleButtonInit(Component.translatable("gui.mp.de.setting.light.force"),
118119
List.of(Boolean.TRUE, Boolean.FALSE),
119120
bool -> b -> ConfigHelper.getConfigWrite(MadParticleConfig.class, madParticleConfig -> madParticleConfig.forceMaxLight = bool),
120-
entry -> entry.getContent() == getConfigRead().forceMaxLight).getSecond()
121+
entry -> entry.getContent() == cfg.forceMaxLight).getSecond()
121122
.setTooltip(Tooltip.create(Component.translatable("gui.mp.de.setting.light.force.tooltip")));
122123
addOptionCycleButtonInit(Component.translatable("gui.mp.de.setting.light.update"),
123124
List.of(LightCacheRefreshInterval.values()),
124125
interval -> b -> ConfigHelper.getConfigWrite(MadParticleConfig.class, madParticleConfig -> madParticleConfig.lightCacheRefreshInterval = interval),
125-
entry -> entry.getContent() == getConfigRead().lightCacheRefreshInterval
126+
entry -> entry.getContent() == cfg.lightCacheRefreshInterval
126127
)
127128
.getSecond().setTooltip(Tooltip.create(Component.translatable("gui.mp.de.setting.light.update.tooltip")));
128129

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package cn.ussshenzhou.madparticle.mixin;
2+
3+
import cn.ussshenzhou.madparticle.particle.render.ModRenderPipelines;
4+
import com.mojang.blaze3d.opengl.GlCommandEncoder;
5+
import com.mojang.blaze3d.opengl.GlConst;
6+
import com.mojang.blaze3d.opengl.GlStateManager;
7+
import com.mojang.blaze3d.pipeline.BlendFunction;
8+
import com.mojang.blaze3d.pipeline.ColorTargetState;
9+
import com.mojang.blaze3d.pipeline.DepthStencilState;
10+
import com.mojang.blaze3d.pipeline.RenderPipeline;
11+
import org.jspecify.annotations.Nullable;
12+
import org.lwjgl.opengl.ARBDrawBuffersBlend;
13+
import org.lwjgl.opengl.GL33C;
14+
import org.lwjgl.opengl.GL40;
15+
import org.spongepowered.asm.mixin.Mixin;
16+
import org.spongepowered.asm.mixin.Shadow;
17+
import org.spongepowered.asm.mixin.injection.At;
18+
import org.spongepowered.asm.mixin.injection.Inject;
19+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
20+
21+
@Mixin(GlCommandEncoder.class)
22+
public class GlCommandEncoderMixin {
23+
24+
@Shadow
25+
@Nullable
26+
public RenderPipeline lastPipeline;
27+
28+
@Inject(method = "applyPipelineState", at = @At("HEAD"), cancellable = true)
29+
public void madparticleSupportSeparateBlend(RenderPipeline pipeline, CallbackInfo ci) {
30+
if (pipeline == ModRenderPipelines.INSTANCED_OIT) {
31+
if (this.lastPipeline != pipeline) {
32+
this.lastPipeline = pipeline;
33+
DepthStencilState depthStencilState = pipeline.getDepthStencilState();
34+
if (depthStencilState != null) {
35+
GlStateManager._enableDepthTest();
36+
GlStateManager._depthFunc(GlConst.toGl(depthStencilState.depthTest()));
37+
GlStateManager._depthMask(depthStencilState.writeDepth());
38+
if (depthStencilState.depthBiasConstant() == 0.0F && depthStencilState.depthBiasScaleFactor() == 0.0F) {
39+
GlStateManager._disablePolygonOffset();
40+
} else {
41+
GlStateManager._polygonOffset(depthStencilState.depthBiasScaleFactor(), depthStencilState.depthBiasConstant());
42+
GlStateManager._enablePolygonOffset();
43+
}
44+
} else {
45+
GlStateManager._disableDepthTest();
46+
GlStateManager._depthMask(false);
47+
GlStateManager._disablePolygonOffset();
48+
}
49+
50+
if (pipeline.isCull()) {
51+
GlStateManager._enableCull();
52+
} else {
53+
GlStateManager._disableCull();
54+
}
55+
56+
ColorTargetState[] colorTargetStates = pipeline.getColorTargetStates();
57+
58+
for (int i = 0; i < colorTargetStates.length; i++) {
59+
ColorTargetState state = colorTargetStates[i];
60+
if (state != null) {
61+
if (state.blendFunction().isPresent()) {
62+
GlStateManager._enableBlend(i);
63+
BlendFunction blendFunction = state.blendFunction().get();
64+
ARBDrawBuffersBlend.glBlendFuncSeparateiARB(
65+
i,
66+
GlConst.toGl(blendFunction.color().sourceFactor()),
67+
GlConst.toGl(blendFunction.color().destFactor()),
68+
GlConst.toGl(blendFunction.alpha().sourceFactor()),
69+
GlConst.toGl(blendFunction.alpha().destFactor())
70+
);
71+
ARBDrawBuffersBlend.glBlendEquationSeparateiARB(
72+
i,
73+
GlConst.toGl(blendFunction.color().op()),
74+
GlConst.toGl(blendFunction.alpha().op())
75+
);
76+
} else {
77+
GlStateManager._disableBlend(i);
78+
}
79+
}
80+
}
81+
82+
GlStateManager._polygonMode(1032, GlConst.toGl(pipeline.getPolygonMode()));
83+
84+
for (int i = 0; i < colorTargetStates.length; i++) {
85+
ColorTargetState state = colorTargetStates[i];
86+
if (state != null) {
87+
GlStateManager._colorMask(i, state.writeMask());
88+
}
89+
}
90+
}
91+
ci.cancel();
92+
}
93+
}
94+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.ussshenzhou.madparticle.mixin;
2+
3+
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
4+
import com.mojang.blaze3d.pipeline.RenderPipeline;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.injection.At;
7+
8+
@Mixin(RenderPipeline.Builder.class)
9+
public class RenderPipelineBuilderMixin {
10+
11+
@ModifyExpressionValue(method = "build", at = @At(value = "INVOKE", target = "Ljava/util/Optional;equals(Ljava/lang/Object;)Z"))
12+
private boolean madparticleSupportSeparateBlend(boolean original) {
13+
return true;
14+
}
15+
16+
}

src/main/java/cn/ussshenzhou/madparticle/particle/render/ModRenderPipelines.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import com.mojang.blaze3d.pipeline.*;
77
import com.mojang.blaze3d.platform.*;
88
import com.mojang.blaze3d.shaders.UniformType;
9+
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
910
import com.mojang.blaze3d.vertex.VertexFormat;
1011
import com.mojang.blaze3d.vertex.VertexFormatElement;
1112
import net.minecraft.client.renderer.BindGroupLayouts;
1213
import net.minecraft.client.renderer.RenderPipelines;
1314
import net.minecraft.resources.Identifier;
1415

16+
import java.util.Optional;
17+
1518
/**
1619
* @author USS_Shenzhou
1720
*/
@@ -64,21 +67,34 @@ public class ModRenderPipelines {
6467
RenderPipeline.builder(INSTANCED_SNIPPET)
6568
.withLocation(Identifier.fromNamespaceAndPath(MadParticle.MOD_ID, "instanced_oit"))
6669
.withFragmentShader(Identifier.fromNamespaceAndPath(MadParticle.MOD_ID, "instanced_particle_oit"))
67-
.withColorTargetState(new ColorTargetState(BlendFunction.ADDITIVE))
70+
.withColorTargetState(0, new ColorTargetState(Optional.of(BlendFunction.ADDITIVE), GpuFormat.RGBA32_FLOAT, ColorTargetState.WRITE_ALL))
71+
.withColorTargetState(1, new ColorTargetState(Optional.of(new BlendFunction(BlendFactor.ZERO, BlendFactor.ONE_MINUS_SRC_COLOR)), GpuFormat.R16_FLOAT, ColorTargetState.WRITE_ALL))
6872
.withDepthStencilState(new DepthStencilState(CompareOp.GREATER_THAN_OR_EQUAL, false))
6973
.build()
7074
);
7175

72-
//public static final RenderPipeline INSTANCED_OIT_POST = RenderPipelines.register(
73-
// RenderPipeline.builder()
74-
// .withVertexFormat(, VertexFormat.Mode.QUADS)
75-
// .withLocation(Identifier.fromNamespaceAndPath(MadParticle.MOD_ID, "instanced_oit_post"))
76-
// .withVertexShader(Identifier.fromNamespaceAndPath(MadParticle.MOD_ID, "instanced_particle_oit_post"))
77-
// .withFragmentShader(Identifier.fromNamespaceAndPath(MadParticle.MOD_ID, "instanced_particle_oit_post"))
78-
// .withColorWrite(true, true)
79-
// .withDepthTestFunction(DepthTestFunction.LEQUAL_DEPTH_TEST)
80-
// .withBlend(BlendFunction.TRANSLUCENT)
81-
// .withDepthWrite(true)
82-
// .build()
83-
//);
76+
public static final VertexFormat OIT_POST_VERTEX_FORMAT = VertexFormat.builder(0)
77+
.addAttribute("Position", GpuFormat.RG32_FLOAT)
78+
.addAttribute("UV0", GpuFormat.RG32_FLOAT)
79+
.build();
80+
81+
public static final BindGroupLayout ACCUM = BindGroupLayout.builder().withSampler("accum").build();
82+
public static final BindGroupLayout REVEAL = BindGroupLayout.builder().withSampler("reveal").build();
83+
84+
public static final RenderPipeline INSTANCED_OIT_POST = RenderPipelines.register(
85+
RenderPipeline.builder()
86+
.withLocation(Identifier.fromNamespaceAndPath(MadParticle.MOD_ID, "instanced_oit_post"))
87+
.withVertexShader(Identifier.fromNamespaceAndPath(MadParticle.MOD_ID, "instanced_particle_oit_post"))
88+
89+
.withVertexBinding(0, OIT_POST_VERTEX_FORMAT)
90+
91+
.withFragmentShader(Identifier.fromNamespaceAndPath(MadParticle.MOD_ID, "instanced_particle_oit_post"))
92+
.withBindGroupLayout(ACCUM)
93+
.withBindGroupLayout(REVEAL)
94+
.withColorTargetState(0, new ColorTargetState(Optional.of(new BlendFunction(BlendFactor.SRC_ALPHA, BlendFactor.ONE_MINUS_SRC_ALPHA)), GpuFormat.RGBA8_UNORM, ColorTargetState.WRITE_ALL))
95+
.withDepthStencilState(new DepthStencilState(CompareOp.ALWAYS_PASS, true))
96+
.withPrimitiveTopology(PrimitiveTopology.QUADS)
97+
.withPolygonMode(PolygonMode.FILL)
98+
.build()
99+
);
84100
}

src/main/java/cn/ussshenzhou/madparticle/particle/render/NeoInstancedRenderManager.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import cn.ussshenzhou.madparticle.util.MemoryUtil;
1010
import cn.ussshenzhou.t88.config.ConfigHelper;
1111
import com.mojang.blaze3d.buffers.GpuBuffer;
12-
import com.mojang.blaze3d.systems.RenderSystem;
1312
import com.mojang.logging.LogUtils;
1413
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
1514
import net.minecraft.client.Minecraft;
@@ -24,9 +23,6 @@
2423
import net.neoforged.neoforge.client.event.ClientTickEvent;
2524
import net.neoforged.neoforge.client.event.RenderLevelStageEvent;
2625
import net.neoforged.neoforge.common.NeoForge;
27-
import org.joml.Matrix4f;
28-
import org.joml.Vector3f;
29-
import org.joml.Vector4f;
3026
import org.lwjgl.BufferUtils;
3127

3228
import java.util.concurrent.CompletableFuture;
@@ -112,7 +108,8 @@ public static void init() {
112108
GpuBuffer.USAGE_UNIFORM | GpuBuffer.USAGE_MAP_WRITE,
113109
(4 + 4) * 4);
114110
//-----translucent methods related-----
115-
private final NormalRenderer normalRenderer = new NormalRenderer(this);
111+
final SimpleRenderer simpleRenderer = new SimpleRenderer(this);
112+
final OitRenderer oitRenderer = new OitRenderer(this);
116113
//-----oit-----
117114
//final RenderTarget accum = getDevice().createTexture(
118115
// "OIT Accum",
@@ -150,12 +147,18 @@ public void render() {
150147
if (amount == 0) {
151148
return;
152149
}
153-
154-
var encoder = RenderSystem.getDevice().createCommandEncoder();
155-
var cameraUbo = cameraCorrectionUbo.currentBuffer().map(false, true);
156-
var dynamicUbo = RenderSystem.getDynamicUniforms().writeTransform(RenderSystem.getModelViewMatrixCopy(), new Vector4f(1.0F, 1.0F, 1.0F, 1.0F), new Vector3f(), new Matrix4f());
157-
158-
normalRenderer.doRender(encoder, cameraUbo, dynamicUbo);
150+
var mainTarget = Minecraft.getInstance().gameRenderer.mainRenderTarget();
151+
switch (ConfigHelper.getConfigRead(MadParticleConfig.class).translucentMethod) {
152+
case DEPTH_TRUE -> {
153+
simpleRenderer.doRender(mainTarget.getColorTextureView(), mainTarget.getDepthTextureView(), ModRenderPipelines.INSTANCED_COMMON_DEPTH);
154+
}
155+
case DEPTH_FALSE -> {
156+
simpleRenderer.doRender(mainTarget.getColorTextureView(), mainTarget.getDepthTextureView(), ModRenderPipelines.INSTANCED_COMMON_BLEND);
157+
}
158+
case OIT -> {
159+
oitRenderer.doRender(mainTarget.getColorTextureView(), mainTarget.getDepthTextureView());
160+
}
161+
}
159162
cleanUp();
160163
}
161164

0 commit comments

Comments
 (0)