Skip to content

Commit f93d5c0

Browse files
committed
fix ssbo not set when join world
1 parent df6129c commit f93d5c0

File tree

4 files changed

+117
-45
lines changed

4 files changed

+117
-45
lines changed

Common/src/main/java/com/lowdragmc/shimmer/client/light/LightManager.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ public void renderLevelPre(int blockLightSize, float camX, float camY, float cam
206206
env.bufferSubData(0, new int[]{UV_LIGHT.size() + blockLightSize});
207207
env.bufferSubData(4,new int[]{NO_UV_LIGHT_COUNT});
208208
env.bufferSubData(16, new float[]{camX, camY, camZ});
209+
210+
light.bindIndex(irisHandle.getLightsIndex());
211+
env.bindIndex(irisHandle.getEnvIndex());
209212
}
210213
}else {
211214
envUBO.bufferSubData(0,new int[4]);
@@ -217,8 +220,10 @@ public void renderLevelPre(int blockLightSize, float camX, float camY, float cam
217220

218221
public void updateNoUVLight(){
219222
LocalPlayer localPlayer = Minecraft.getInstance().player;
223+
if (localPlayer == null) return;
220224
NO_UV_LIGHT_COUNT = 0;
221225
Vec3 localPlayerPosition = localPlayer.position();
226+
if (Minecraft.getInstance().level == null) return;
222227
List<AbstractClientPlayer> players = Minecraft.getInstance().level.players();
223228
float partialTicks = Minecraft.getInstance().getFrameTime();
224229
for (AbstractClientPlayer player : players) {
@@ -328,6 +333,7 @@ public ColorPointLight addLight(Vector3f pos, int color, float radius, boolean u
328333
}
329334

330335
@Nullable
336+
@SuppressWarnings("unused")
331337
public ColorPointLight addLight(Vector3f pos, int color, float radius) {
332338
return addLight(pos, color, radius, false);
333339
}
@@ -424,7 +430,7 @@ public void registerFluidLight(Fluid fluid, int color, float radius) {
424430
public void loadConfig() {
425431
ITEM_MAP.clear();
426432
TAG_MAP.clear();
427-
BLOCK_MAP.clear();;
433+
BLOCK_MAP.clear();
428434
FLUID_MAP.clear();
429435

430436
for (var config : Configuration.configs){
@@ -437,7 +443,8 @@ public void loadConfig() {
437443

438444
if (blockLight.hasState()){
439445

440-
if (Utils.checkBlockProperties(config.configSource, blockLight.state, blockPair.first())) continue;
446+
assert blockLight.state != null;
447+
if (Utils.checkBlockProperties(config.configSource, blockLight.state, blockPair.first())) continue;
441448

442449
List<BlockState> validStates = Utils.getAvailableStates(blockLight.state, block);
443450
var light = new ColorPointLight.Template(blockLight.radius,blockLight.color());
@@ -550,6 +557,7 @@ public void registerTagLight(ResourceLocation tag,Function<ItemStack,ColorPointL
550557
* @param playerUUID the specified player's UUID
551558
* @return instance created. null -- if no more available space. control enable/disable yourself
552559
*/
560+
@SuppressWarnings("unused")
553561
public ColorPointLight addPlayerItemLight(Vector3f pos, int color, float radius, UUID playerUUID) {
554562
if (maxFixedLight() == MAXIMUM_LIGHT_SUPPORT) return null;
555563
ColorPointLight light = new ColorPointLight(this,pos,color,radius,-1,false);
@@ -562,10 +570,12 @@ public ColorPointLight addPlayerItemLight(Vector3f pos, int color, float radius,
562570
return NO_UV_LIGHT_PLAYER.get(player.getUUID());
563571
}
564572

573+
@SuppressWarnings("UnusedReturnValue")
565574
public boolean removePlayerLight(UUID playerUUID){
566575
return NO_UV_LIGHT_PLAYER.remove(playerUUID) != null;
567576
}
568577

578+
@SuppressWarnings("unused")
569579
public boolean removePlayerLight(ColorPointLight removeLight){
570580
Set<UUID> set = NO_UV_LIGHT_PLAYER.keySet();
571581
for (UUID uuid : set) {

Common/src/main/java/com/lowdragmc/shimmer/comp/iris/IrisHandle.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@ public interface IrisHandle {
1717
*/
1818
void updateInfo(Object buffers);
1919

20+
/**
21+
* don't destroy ssbo buffer, it will be destroyed by iris since we delegate to it
22+
*/
2023
void onSSBODestroyed();
2124

2225
void setLightsIndex(int index);
2326

27+
int getLightsIndex();
28+
2429
void setEnvIndex(int index);
2530

31+
int getEnvIndex();
32+
2633
static <T> T make(Supplier<T> supplier) {
2734
return supplier.get();
2835
}
@@ -58,10 +65,14 @@ static void analyzeShaderProperties(String shaderProperties) {
5865
try {
5966
final int index = Integer.parseInt(analyze[1]);
6067
switch (analyze[0]) {
61-
case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_LIGHTS_IDENTIFIER ->
62-
INSTANCE.setLightsIndex(index);
63-
case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_ENV_IDENTIFIER ->
64-
INSTANCE.setEnvIndex(index);
68+
case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_LIGHTS_IDENTIFIER -> {
69+
ShimmerConstants.LOGGER.info("detect LIGHT BUFFER ssbo index:" + index);
70+
INSTANCE.setLightsIndex(index);
71+
}
72+
case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_ENV_IDENTIFIER -> {
73+
ShimmerConstants.LOGGER.info("detect ENV BUFFER ssbo index:" + index);
74+
INSTANCE.setEnvIndex(index);
75+
}
6576
}
6677
} catch (NumberFormatException ignored) {
6778

Fabric/src/main/java/com/lowdragmc/shimmer/fabric/compact/iris/FabricIrisHandle.java

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,35 @@
1111
import org.apache.commons.lang3.tuple.Pair;
1212
import org.jetbrains.annotations.Nullable;
1313
import org.lwjgl.opengl.GL46;
14-
import org.lwjgl.system.MemoryStack;
15-
import org.lwjgl.system.MemoryUtil;
1614

15+
@SuppressWarnings("unused")
1716
public class FabricIrisHandle implements IrisHandle {
1817

18+
/**
19+
* must have this, this is called by reflect
20+
*/
1921
public FabricIrisHandle() {
2022
}
2123

2224
private boolean available = MixinPluginShared.IS_IRIS_LOAD;
2325
@Nullable
2426
private Pair<ShaderSSBO, ShaderSSBO> ssbos;
27+
/**
28+
* global ssbo index
29+
*/
2530
private int lightIndex = -1;
2631
private int envIndex = -1;
2732

2833
@Override
2934
public void updateInfo(Object buffers) {
3035
if (!available || !ShimmerConstants.IRIS_COMPACT_ENABLE) return;
36+
if (lightIndex == -1 || envIndex == -1) {
37+
ShimmerConstants.LOGGER.info("env buffer not set fully, light:{}, env:{}", lightIndex, envIndex);
38+
ShimmerConstants.LOGGER.info("shimmer shader support for colored light with ssbo is now offline");
39+
return;
40+
}
3141
if (buffers instanceof ShaderStorageBuffer[] suffers) {
42+
//index in the ShaderStorageBuffer[]
3243
int lightBufferIndex = -1;
3344
int envBufferIndex = -1;
3445
for (int i = 0; i < suffers.length; i++) {
@@ -47,58 +58,75 @@ public void updateInfo(Object buffers) {
4758
}
4859
}
4960
}
50-
if (lightBufferIndex == -1 || envBufferIndex == -1) return;
61+
if (lightBufferIndex == -1 || envBufferIndex == -1) {
62+
ShimmerConstants.LOGGER.error("failed to detect ssbo created by iris");
63+
return;
64+
} else {
65+
ShimmerConstants.LOGGER.info("detect ssbo created by iris success");
66+
}
5167
int finalLightBufferIndex = lightBufferIndex;
5268
int finalEnvBufferIndex = envBufferIndex;
69+
if (checkIsRelative(suffers[lightBufferIndex], "light") || checkIsRelative(suffers[envBufferIndex],"env")) return;
5370
RenderUtils.warpGLDebugLabel("initSSBO", () -> {
54-
var lightBuffer = new ShaderSSBO();
55-
//no need to call glShaderStorageBlockBinding here, as we set layout in shader explicitly
56-
{
57-
var oldBuffer = suffers[finalLightBufferIndex];
58-
lightBuffer.createBufferData(oldBuffer.getSize(), GL46.GL_DYNAMIC_COPY);
59-
lightBuffer.bindIndex(oldBuffer.getIndex());
60-
((ShaderStorageBufferAccessor)oldBuffer).callDestroy();
61-
suffers[finalLightBufferIndex] = new ShaderStorageBuffer(lightBuffer.id, ((ShaderStorageBufferAccessor)oldBuffer).getInfo());
62-
}
63-
var envBuffer = new ShaderSSBO();
64-
{
65-
var oldBuffer = suffers[finalEnvBufferIndex];
66-
envBuffer.createBufferData(oldBuffer.getSize(), GL46.GL_DYNAMIC_COPY);
67-
envBuffer.bufferSubData(0,new int[8]);
68-
envBuffer.bindIndex(oldBuffer.getIndex());
69-
((ShaderStorageBufferAccessor)oldBuffer).callDestroy();
70-
suffers[finalEnvBufferIndex] = new ShaderStorageBuffer(envBuffer.id, ((ShaderStorageBufferAccessor)oldBuffer).getInfo());
71-
}
72-
suffers[finalLightBufferIndex].bind();
73-
suffers[finalEnvBufferIndex].bind();
71+
var lightBuffer = replaceSSBO(suffers, finalLightBufferIndex);
72+
var envBuffer = replaceSSBO(suffers, finalEnvBufferIndex);
7473
ssbos = Pair.of(lightBuffer, envBuffer);
7574
});
7675
} else {
7776
ShimmerConstants.LOGGER.error("expect:{} as ShaderStorageBuffer[],actual:{}", buffers.toString(), buffers.getClass().getName());
7877
}
7978
}
8079

80+
private static boolean checkIsRelative(ShaderStorageBuffer buffer, String name) {
81+
if (((ShaderStorageBufferAccessor)buffer).getInfo().relative()) {
82+
ShimmerConstants.LOGGER.error("expect buffer:{} not relative", name);
83+
return true;
84+
} {
85+
return false;
86+
}
87+
}
88+
89+
private static ShaderSSBO replaceSSBO(ShaderStorageBuffer[] suffers, int replaceIndex) {
90+
var oldBuffer = suffers[replaceIndex];
91+
//destroy origin
92+
((ShaderStorageBufferAccessor) oldBuffer).callDestroy();
93+
//create ours
94+
var buffer = new ShaderSSBO();
95+
buffer.createBufferData(oldBuffer.getSize(), GL46.GL_DYNAMIC_COPY);
96+
buffer.bindIndex(oldBuffer.getIndex());
97+
//warp and bind
98+
var irisSSBO = new ShaderStorageBuffer(buffer.id, ((ShaderStorageBufferAccessor) oldBuffer).getInfo());
99+
suffers[replaceIndex] = irisSSBO;
100+
irisSSBO.bind();
101+
102+
return buffer;
103+
}
104+
81105
@Override
82106
public void onSSBODestroyed() {
83-
if (ssbos != null) {
84-
ssbos.getLeft().close();
85-
ssbos.getRight().close();
86-
ssbos = null;
87-
}
88-
lightIndex = -1;
89-
envIndex = -1;
107+
ssbos = null;
90108
}
91109

92110
@Override
93111
public void setLightsIndex(int index) {
94112
this.lightIndex = index;
95113
}
96114

115+
@Override
116+
public int getLightsIndex() {
117+
return lightIndex;
118+
}
119+
97120
@Override
98121
public void setEnvIndex(int index) {
99122
this.envIndex = index;
100123
}
101124

125+
@Override
126+
public int getEnvIndex() {
127+
return envIndex;
128+
}
129+
102130
@Override
103131
@Nullable
104132
public Pair<ShaderSSBO, ShaderSSBO> getBuffer() {

Forge/src/main/java/com/lowdragmc/shimmer/forge/compat/oculus/ForgeOculusHandle.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,32 @@
1111
import org.jetbrains.annotations.Nullable;
1212
import org.lwjgl.opengl.GL46;
1313

14+
@SuppressWarnings("unused")
1415
public class ForgeOculusHandle implements IrisHandle {
1516

17+
/**
18+
* must have this, this is called by reflect
19+
*/
1620
public ForgeOculusHandle() {
1721
}
1822

1923
private boolean available = MixinPluginShared.IS_OCULUS_LOAD;
2024
@Nullable
2125
private Pair<ShaderSSBO, ShaderSSBO> ssbos;
26+
/**
27+
* global ssbo index
28+
*/
2229
private int lightIndex = -1;
2330
private int envIndex = -1;
2431

2532
@Override
2633
public void updateInfo(Object buffers) {
2734
if (!available || !ShimmerConstants.IRIS_COMPACT_ENABLE) return;
35+
if (lightIndex == -1 || envIndex == -1) {
36+
ShimmerConstants.LOGGER.info("env buffer not set fully, light:{}, env:{}", lightIndex, envIndex);
37+
ShimmerConstants.LOGGER.info("shimmer shader support for colored light with ssbo is now offline");
38+
return;
39+
}
2840
if (buffers instanceof ShaderStorageBuffer[] suffers) {
2941
int lightBufferIndex = -1;
3042
int envBufferIndex = -1;
@@ -44,11 +56,16 @@ public void updateInfo(Object buffers) {
4456
}
4557
}
4658
}
47-
if (lightBufferIndex == -1 || envBufferIndex == -1) return;
59+
if (lightBufferIndex == -1 || envBufferIndex == -1) {
60+
ShimmerConstants.LOGGER.error("failed to detect ssbo created by iris");
61+
return;
62+
} else {
63+
ShimmerConstants.LOGGER.info("detect ssbo created by iris success");
64+
}
4865
int finalLightBufferIndex = lightBufferIndex;
4966
int finalEnvBufferIndex = envBufferIndex;
5067
RenderUtils.warpGLDebugLabel("initSSBO", () -> {
51-
var lightBuffer = new ShaderSSBO();
68+
var lightBuffer = new ShaderSSBO();//TODO add check after oculus follows iris
5269
//no need to call glShaderStorageBlockBinding here, as we set layout in shader explicitly
5370
{
5471
var oldBuffer = suffers[finalLightBufferIndex];
@@ -75,27 +92,33 @@ public void updateInfo(Object buffers) {
7592
}
7693
}
7794

95+
96+
7897
@Override
7998
public void onSSBODestroyed() {
80-
if (ssbos != null) {
81-
ssbos.getLeft().close();
82-
ssbos.getRight().close();
83-
ssbos = null;
84-
}
85-
lightIndex = -1;
86-
envIndex = -1;
99+
ssbos = null;
87100
}
88101

89102
@Override
90103
public void setLightsIndex(int index) {
91104
this.lightIndex = index;
92105
}
93106

107+
@Override
108+
public int getLightsIndex() {
109+
return lightIndex;
110+
}
111+
94112
@Override
95113
public void setEnvIndex(int index) {
96114
this.envIndex = index;
97115
}
98116

117+
@Override
118+
public int getEnvIndex() {
119+
return envIndex;
120+
}
121+
99122
@Override
100123
@Nullable
101124
public Pair<ShaderSSBO, ShaderSSBO> getBuffer() {

0 commit comments

Comments
 (0)