Skip to content

Commit

Permalink
fix ssbo not set when join world
Browse files Browse the repository at this point in the history
  • Loading branch information
zomb-676 committed Aug 15, 2023
1 parent df6129c commit f93d5c0
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ public void renderLevelPre(int blockLightSize, float camX, float camY, float cam
env.bufferSubData(0, new int[]{UV_LIGHT.size() + blockLightSize});
env.bufferSubData(4,new int[]{NO_UV_LIGHT_COUNT});
env.bufferSubData(16, new float[]{camX, camY, camZ});

light.bindIndex(irisHandle.getLightsIndex());
env.bindIndex(irisHandle.getEnvIndex());
}
}else {
envUBO.bufferSubData(0,new int[4]);
Expand All @@ -217,8 +220,10 @@ public void renderLevelPre(int blockLightSize, float camX, float camY, float cam

public void updateNoUVLight(){
LocalPlayer localPlayer = Minecraft.getInstance().player;
if (localPlayer == null) return;
NO_UV_LIGHT_COUNT = 0;
Vec3 localPlayerPosition = localPlayer.position();
if (Minecraft.getInstance().level == null) return;
List<AbstractClientPlayer> players = Minecraft.getInstance().level.players();
float partialTicks = Minecraft.getInstance().getFrameTime();
for (AbstractClientPlayer player : players) {
Expand Down Expand Up @@ -328,6 +333,7 @@ public ColorPointLight addLight(Vector3f pos, int color, float radius, boolean u
}

@Nullable
@SuppressWarnings("unused")
public ColorPointLight addLight(Vector3f pos, int color, float radius) {
return addLight(pos, color, radius, false);
}
Expand Down Expand Up @@ -424,7 +430,7 @@ public void registerFluidLight(Fluid fluid, int color, float radius) {
public void loadConfig() {
ITEM_MAP.clear();
TAG_MAP.clear();
BLOCK_MAP.clear();;
BLOCK_MAP.clear();
FLUID_MAP.clear();

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

if (blockLight.hasState()){

if (Utils.checkBlockProperties(config.configSource, blockLight.state, blockPair.first())) continue;
assert blockLight.state != null;
if (Utils.checkBlockProperties(config.configSource, blockLight.state, blockPair.first())) continue;

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

@SuppressWarnings("UnusedReturnValue")
public boolean removePlayerLight(UUID playerUUID){
return NO_UV_LIGHT_PLAYER.remove(playerUUID) != null;
}

@SuppressWarnings("unused")
public boolean removePlayerLight(ColorPointLight removeLight){
Set<UUID> set = NO_UV_LIGHT_PLAYER.keySet();
for (UUID uuid : set) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ public interface IrisHandle {
*/
void updateInfo(Object buffers);

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

void setLightsIndex(int index);

int getLightsIndex();

void setEnvIndex(int index);

int getEnvIndex();

static <T> T make(Supplier<T> supplier) {
return supplier.get();
}
Expand Down Expand Up @@ -58,10 +65,14 @@ static void analyzeShaderProperties(String shaderProperties) {
try {
final int index = Integer.parseInt(analyze[1]);
switch (analyze[0]) {
case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_LIGHTS_IDENTIFIER ->
INSTANCE.setLightsIndex(index);
case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_ENV_IDENTIFIER ->
INSTANCE.setEnvIndex(index);
case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_LIGHTS_IDENTIFIER -> {
ShimmerConstants.LOGGER.info("detect LIGHT BUFFER ssbo index:" + index);
INSTANCE.setLightsIndex(index);
}
case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_ENV_IDENTIFIER -> {
ShimmerConstants.LOGGER.info("detect ENV BUFFER ssbo index:" + index);
INSTANCE.setEnvIndex(index);
}
}
} catch (NumberFormatException ignored) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,35 @@
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL46;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;

@SuppressWarnings("unused")
public class FabricIrisHandle implements IrisHandle {

/**
* must have this, this is called by reflect
*/
public FabricIrisHandle() {
}

private boolean available = MixinPluginShared.IS_IRIS_LOAD;
@Nullable
private Pair<ShaderSSBO, ShaderSSBO> ssbos;
/**
* global ssbo index
*/
private int lightIndex = -1;
private int envIndex = -1;

@Override
public void updateInfo(Object buffers) {
if (!available || !ShimmerConstants.IRIS_COMPACT_ENABLE) return;
if (lightIndex == -1 || envIndex == -1) {
ShimmerConstants.LOGGER.info("env buffer not set fully, light:{}, env:{}", lightIndex, envIndex);
ShimmerConstants.LOGGER.info("shimmer shader support for colored light with ssbo is now offline");
return;
}
if (buffers instanceof ShaderStorageBuffer[] suffers) {
//index in the ShaderStorageBuffer[]
int lightBufferIndex = -1;
int envBufferIndex = -1;
for (int i = 0; i < suffers.length; i++) {
Expand All @@ -47,58 +58,75 @@ public void updateInfo(Object buffers) {
}
}
}
if (lightBufferIndex == -1 || envBufferIndex == -1) return;
if (lightBufferIndex == -1 || envBufferIndex == -1) {
ShimmerConstants.LOGGER.error("failed to detect ssbo created by iris");
return;
} else {
ShimmerConstants.LOGGER.info("detect ssbo created by iris success");
}
int finalLightBufferIndex = lightBufferIndex;
int finalEnvBufferIndex = envBufferIndex;
if (checkIsRelative(suffers[lightBufferIndex], "light") || checkIsRelative(suffers[envBufferIndex],"env")) return;
RenderUtils.warpGLDebugLabel("initSSBO", () -> {
var lightBuffer = new ShaderSSBO();
//no need to call glShaderStorageBlockBinding here, as we set layout in shader explicitly
{
var oldBuffer = suffers[finalLightBufferIndex];
lightBuffer.createBufferData(oldBuffer.getSize(), GL46.GL_DYNAMIC_COPY);
lightBuffer.bindIndex(oldBuffer.getIndex());
((ShaderStorageBufferAccessor)oldBuffer).callDestroy();
suffers[finalLightBufferIndex] = new ShaderStorageBuffer(lightBuffer.id, ((ShaderStorageBufferAccessor)oldBuffer).getInfo());
}
var envBuffer = new ShaderSSBO();
{
var oldBuffer = suffers[finalEnvBufferIndex];
envBuffer.createBufferData(oldBuffer.getSize(), GL46.GL_DYNAMIC_COPY);
envBuffer.bufferSubData(0,new int[8]);
envBuffer.bindIndex(oldBuffer.getIndex());
((ShaderStorageBufferAccessor)oldBuffer).callDestroy();
suffers[finalEnvBufferIndex] = new ShaderStorageBuffer(envBuffer.id, ((ShaderStorageBufferAccessor)oldBuffer).getInfo());
}
suffers[finalLightBufferIndex].bind();
suffers[finalEnvBufferIndex].bind();
var lightBuffer = replaceSSBO(suffers, finalLightBufferIndex);
var envBuffer = replaceSSBO(suffers, finalEnvBufferIndex);
ssbos = Pair.of(lightBuffer, envBuffer);
});
} else {
ShimmerConstants.LOGGER.error("expect:{} as ShaderStorageBuffer[],actual:{}", buffers.toString(), buffers.getClass().getName());
}
}

private static boolean checkIsRelative(ShaderStorageBuffer buffer, String name) {
if (((ShaderStorageBufferAccessor)buffer).getInfo().relative()) {
ShimmerConstants.LOGGER.error("expect buffer:{} not relative", name);
return true;
} {
return false;
}
}

private static ShaderSSBO replaceSSBO(ShaderStorageBuffer[] suffers, int replaceIndex) {
var oldBuffer = suffers[replaceIndex];
//destroy origin
((ShaderStorageBufferAccessor) oldBuffer).callDestroy();
//create ours
var buffer = new ShaderSSBO();
buffer.createBufferData(oldBuffer.getSize(), GL46.GL_DYNAMIC_COPY);
buffer.bindIndex(oldBuffer.getIndex());
//warp and bind
var irisSSBO = new ShaderStorageBuffer(buffer.id, ((ShaderStorageBufferAccessor) oldBuffer).getInfo());
suffers[replaceIndex] = irisSSBO;
irisSSBO.bind();

return buffer;
}

@Override
public void onSSBODestroyed() {
if (ssbos != null) {
ssbos.getLeft().close();
ssbos.getRight().close();
ssbos = null;
}
lightIndex = -1;
envIndex = -1;
ssbos = null;
}

@Override
public void setLightsIndex(int index) {
this.lightIndex = index;
}

@Override
public int getLightsIndex() {
return lightIndex;
}

@Override
public void setEnvIndex(int index) {
this.envIndex = index;
}

@Override
public int getEnvIndex() {
return envIndex;
}

@Override
@Nullable
public Pair<ShaderSSBO, ShaderSSBO> getBuffer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL46;

@SuppressWarnings("unused")
public class ForgeOculusHandle implements IrisHandle {

/**
* must have this, this is called by reflect
*/
public ForgeOculusHandle() {
}

private boolean available = MixinPluginShared.IS_OCULUS_LOAD;
@Nullable
private Pair<ShaderSSBO, ShaderSSBO> ssbos;
/**
* global ssbo index
*/
private int lightIndex = -1;
private int envIndex = -1;

@Override
public void updateInfo(Object buffers) {
if (!available || !ShimmerConstants.IRIS_COMPACT_ENABLE) return;
if (lightIndex == -1 || envIndex == -1) {
ShimmerConstants.LOGGER.info("env buffer not set fully, light:{}, env:{}", lightIndex, envIndex);
ShimmerConstants.LOGGER.info("shimmer shader support for colored light with ssbo is now offline");
return;
}
if (buffers instanceof ShaderStorageBuffer[] suffers) {
int lightBufferIndex = -1;
int envBufferIndex = -1;
Expand All @@ -44,11 +56,16 @@ public void updateInfo(Object buffers) {
}
}
}
if (lightBufferIndex == -1 || envBufferIndex == -1) return;
if (lightBufferIndex == -1 || envBufferIndex == -1) {
ShimmerConstants.LOGGER.error("failed to detect ssbo created by iris");
return;
} else {
ShimmerConstants.LOGGER.info("detect ssbo created by iris success");
}
int finalLightBufferIndex = lightBufferIndex;
int finalEnvBufferIndex = envBufferIndex;
RenderUtils.warpGLDebugLabel("initSSBO", () -> {
var lightBuffer = new ShaderSSBO();
var lightBuffer = new ShaderSSBO();//TODO add check after oculus follows iris
//no need to call glShaderStorageBlockBinding here, as we set layout in shader explicitly
{
var oldBuffer = suffers[finalLightBufferIndex];
Expand All @@ -75,27 +92,33 @@ public void updateInfo(Object buffers) {
}
}



@Override
public void onSSBODestroyed() {
if (ssbos != null) {
ssbos.getLeft().close();
ssbos.getRight().close();
ssbos = null;
}
lightIndex = -1;
envIndex = -1;
ssbos = null;
}

@Override
public void setLightsIndex(int index) {
this.lightIndex = index;
}

@Override
public int getLightsIndex() {
return lightIndex;
}

@Override
public void setEnvIndex(int index) {
this.envIndex = index;
}

@Override
public int getEnvIndex() {
return envIndex;
}

@Override
@Nullable
public Pair<ShaderSSBO, ShaderSSBO> getBuffer() {
Expand Down

0 comments on commit f93d5c0

Please sign in to comment.