Weather instancing (#2127)

* Initial commit for weather-instancing

* attempt to make the builder less ugly
This commit is contained in:
Eclipse-Sol authored and GitHub committed 2026-09-22 19:27:05 -04:00
1 parent 53776684c8
commit f2c07dea28
28 files changed
+1145 -11

No files matched your search

@@ -0,0 +1,8 @@
package com.gtnewhorizons.angelica.compat.dragonapi;
public final class DragonAPICompat {
private DragonAPICompat() {}
public static boolean forcingSeasonalSnow;
}
@@ -63,6 +63,10 @@ public class AngelicaConfig {
@Config.RequiresMcRestart
public static boolean enableVBOClouds;
@Config.Comment("Replace rain/snow rendering with a cached, instanced version.")
@Config.DefaultBoolean(true)
public static boolean enableInstancedWeather;
@Config.Comment("Uses cached attributes for VBO rendering, resulting in less CPU overhead. Disable if you notice any graphical issues.")
@Config.DefaultBoolean(true)
@Config.RequiresMcRestart
@@ -22,6 +22,7 @@ import net.minecraft.util.ChunkCoordinates;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.MathHelper;
import net.minecraft.world.MinecraftException;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.IChunkProvider;
@@ -215,6 +216,8 @@ public final class FlybyRunner {
final EntityClientPlayerMP player = mc.thePlayer;
if (player == null || mc.theWorld == null) return;
pinClientWeather(mc.theWorld);
switch (this.state) {
case WAITING -> {
if (!this.armed) {
@@ -569,6 +572,13 @@ public final class FlybyRunner {
server.getConfigurationManager().sendChatMsg(new ChatComponentText(EnumChatFormatting.AQUA + "[Angelica] " + EnumChatFormatting.WHITE + "Flyby: world saving is off until you leave this world"));
}
private static void pinClientWeather(World world) {
final SystemProperties.FlybyWeather weather = SystemProperties.FLYBY_WEATHER;
world.rainingStrength = world.prevRainingStrength = weather.isRaining() ? 1.0f : 0.0f;
world.thunderingStrength = world.prevThunderingStrength = weather.isThundering() ? 1.0f : 0.0f;
}
private void applyFreeze(MinecraftServer server) {
for (WorldServer world : server.worldServers) {
if (world == null) continue;
@@ -577,13 +587,17 @@ public final class FlybyRunner {
world.getGameRules().setOrCreateGameRule("doDaylightCycle", "false");
LOGGER.warn("Flyby: doDaylightCycle was on, disabled it");
}
if (world.getWorldInfo().isRaining() || world.getWorldInfo().isThundering()) {
LOGGER.warn("Flyby: weather was active, clearing it");
world.getWorldInfo().setRaining(false);
world.getWorldInfo().setThundering(false);
final SystemProperties.FlybyWeather weather = SystemProperties.FLYBY_WEATHER;
if (world.getWorldInfo().isRaining() != weather.isRaining()
|| world.getWorldInfo().isThundering() != weather.isThundering()) {
LOGGER.warn("Flyby: forcing weather to {}", weather);
world.getWorldInfo().setRaining(weather.isRaining());
world.getWorldInfo().setThundering(weather.isThundering());
}
world.getWorldInfo().setRainTime(Integer.MAX_VALUE);
world.getWorldInfo().setThunderTime(Integer.MAX_VALUE);
world.rainingStrength = world.prevRainingStrength = weather.isRaining() ? 1.0f : 0.0f;
world.thunderingStrength = world.prevThunderingStrength = weather.isThundering() ? 1.0f : 0.0f;
if (world.getWorldTime() % 24000L != SystemProperties.FLYBY_TIME_OF_DAY) {
LOGGER.info("Flyby: freezing time at {}", SystemProperties.FLYBY_TIME_OF_DAY);
@@ -71,6 +71,13 @@ public enum Mixins implements IMixins {
)
),
ANGELICA_WEATHER(new MixinBuilder()
.setPhase(Phase.EARLY)
.addExcludedMod(TargetedMod.DYNAMIC_SURROUNDINGS_MIST)
.addExcludedMod(TargetedMod.DYNAMIC_SURROUNDINGS_ORIGINAL)
.addClientMixins("angelica.rendering.MixinEntityRenderer_Weather")
),
STARMINER_RENDERER_LIVING_ENTITY_OPTIMIZATION(new MixinBuilder()
.setPhase(Phase.LATE)
.addRequiredMod(TargetedMod.STARMINER)
@@ -505,6 +512,14 @@ public enum Mixins implements IMixins {
)
),
DRAGONAPI_SEASONAL_SNOW(new MixinBuilder("Let DragonAPI's Xmas forced-snow event override weather rendering")
.setPhase(Phase.EARLY)
.addRequiredMod(TargetedMod.DRAGON_API)
.addClientMixins(
"dragonapi.MixinSpecialDayTracker"
)
),
IRIS_RENDERING_NOBACKHAND(new MixinBuilder("Iris Hand Shaders")
.setPhase(Phase.EARLY)
.setApplyIf(() -> AngelicaConfig.enableIris)
@@ -10,4 +10,7 @@ public interface EntityRendererAccessor {
* Get the lightmap texture for shader binding.
*/
DynamicTexture getLightmapTexture();
/** Drives the rain and snow texture scroll. */
int getRendererUpdateCount();
}
@@ -0,0 +1,213 @@
package com.gtnewhorizons.angelica.render;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.world.biome.BiomeGenBase;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.Random;
/**
* Per-column rain and snow data, sampled once and reused instead of recomputed for every column of every frame.
*/
public final class WeatherColumns {
private static final float TEMPERATURE_PER_BLOCK = 0.05F / 30.0F;
private static final int TEMPERATURE_REFERENCE_Y = 65;
private static final int REFRESH_TICKS = 20;
private final Random random = new Random();
private int size;
private int[] keyX = new int[0];
private int[] keyZ = new int[0];
private boolean[] present = new boolean[0];
private boolean[] precipitates = new boolean[0];
private boolean[] strikesLightning = new boolean[0];
private int[] height = new int[0];
private int[] hash = new int[0];
private float[] temperatureAtReference = new float[0];
private float[] temperatureBelow65 = new float[0];
private int[] light = new int[0];
private int[] lightY = new int[0];
private float[] jitter = new float[0];
private int refreshCursor;
private int revision;
private int lastTick = Integer.MIN_VALUE;
private int originX = Integer.MIN_VALUE;
private int originZ = Integer.MIN_VALUE;
private WeakReference<WorldClient> lastWorld = new WeakReference<>(null);
public int revision() {
return revision;
}
public void update(WorldClient world, int cameraBlockX, int cameraBlockZ, int radius, int tick) {
final int wanted = radius * 2 + 1;
final boolean resized = wanted != size;
if (resized) resize(wanted);
final boolean worldChanged = lastWorld.get() != world;
if (worldChanged) {
lastWorld = new WeakReference<>(world);
Arrays.fill(present, false);
originX = Integer.MIN_VALUE;
originZ = Integer.MIN_VALUE;
revision++;
}
final int slots = size * size;
if (tick != lastTick) {
lastTick = tick;
final int slice = Math.max(1, slots / REFRESH_TICKS);
for (int i = 0; i < slice; i++) {
if (++refreshCursor >= slots) refreshCursor = 0;
if (present[refreshCursor]) sample(world, keyX[refreshCursor], keyZ[refreshCursor], refreshCursor);
}
}
if (!resized && cameraBlockX == originX && cameraBlockZ == originZ) return;
originX = cameraBlockX;
originZ = cameraBlockZ;
for (int dz = -radius; dz <= radius; dz++) {
for (int dx = -radius; dx <= radius; dx++) {
final int x = cameraBlockX + dx;
final int z = cameraBlockZ + dz;
final int slot = slotOf(x, z);
if (present[slot] && keyX[slot] == x && keyZ[slot] == z) continue;
sample(world, x, z, slot);
}
}
}
public boolean precipitates(int x, int z) {
return precipitates[slotOf(x, z)];
}
public int slotAt(int x, int z) {
return slotOf(x, z);
}
public boolean precipitatesAtSlot(int slot) {
return precipitates[slot];
}
public int heightAtSlot(int slot) {
return height[slot];
}
public int hashAtSlot(int slot) {
return hash[slot];
}
public float jitterAtSlot(int slot, int index) {
return jitter[slot * 4 + index];
}
public float temperatureAtSlot(int slot, int y) {
if (y <= 64) return temperatureBelow65[slot];
return temperatureAtReference[slot] - (y - TEMPERATURE_REFERENCE_Y) * TEMPERATURE_PER_BLOCK;
}
public int lightAtSlot(WorldClient world, int slot, int x, int z, int y) {
if (lightY[slot] != y) {
lightY[slot] = y;
light[slot] = world.getLightBrightnessForSkyBlocks(x, y, z, 0);
}
return light[slot];
}
public boolean covers(int x, int z) {
if (size == 0) return false;
final int slot = slotOf(x, z);
return present[slot] && keyX[slot] == x && keyZ[slot] == z;
}
public boolean strikesLightning(int x, int z) {
return strikesLightning[slotOf(x, z)];
}
public int heightAt(int x, int z) {
return height[slotOf(x, z)];
}
public int hashAt(int x, int z) {
return hash[slotOf(x, z)];
}
public float temperatureAt(int x, int z, int y) {
final int slot = slotOf(x, z);
if (y <= 64) return temperatureBelow65[slot];
return temperatureAtReference[slot] - (y - TEMPERATURE_REFERENCE_Y) * TEMPERATURE_PER_BLOCK;
}
public int lightAt(WorldClient world, int x, int z, int y) {
final int slot = slotOf(x, z);
if (lightY[slot] != y) {
lightY[slot] = y;
light[slot] = world.getLightBrightnessForSkyBlocks(x, y, z, 0);
}
return light[slot];
}
public float jitterAt(int x, int z, int index) {
return jitter[slotOf(x, z) * 4 + index];
}
private int slotOf(int x, int z) {
return Math.floorMod(x, size) * size + Math.floorMod(z, size);
}
private void resize(int wanted) {
size = wanted;
final int slots = wanted * wanted;
keyX = new int[slots];
keyZ = new int[slots];
present = new boolean[slots];
precipitates = new boolean[slots];
strikesLightning = new boolean[slots];
height = new int[slots];
hash = new int[slots];
temperatureAtReference = new float[slots];
temperatureBelow65 = new float[slots];
light = new int[slots];
lightY = new int[slots];
jitter = new float[slots * 4];
refreshCursor = 0;
}
private void sample(WorldClient world, int x, int z, int slot) {
final boolean sameColumn = present[slot] && keyX[slot] == x && keyZ[slot] == z;
final boolean wasPrecipitating = precipitates[slot];
final int wasHeight = height[slot];
keyX[slot] = x;
keyZ[slot] = z;
present[slot] = true;
lightY[slot] = Integer.MIN_VALUE;
final BiomeGenBase biome = world.getBiomeGenForCoords(x, z);
strikesLightning[slot] = biome.canSpawnLightningBolt();
precipitates[slot] = strikesLightning[slot] || biome.getEnableSnow();
if (!sameColumn || precipitates[slot] != wasPrecipitating) revision++;
if (!precipitates[slot]) return;
height[slot] = world.getPrecipitationHeight(x, z);
if (height[slot] != wasHeight) revision++;
temperatureAtReference[slot] = biome.getFloatTemperature(x, TEMPERATURE_REFERENCE_Y, z);
temperatureBelow65[slot] = biome.getFloatTemperature(x, 64, z);
// Vanilla overflows these in int before widening
hash[slot] = x * x * 3121 + x * 45238971 + z * z * 418711 + z * 13761;
random.setSeed((long) x * x * 3121 + x * 45238971L ^ (long) z * z * 418711 + z * 13761L);
final int base = slot * 4;
jitter[base] = random.nextFloat();
jitter[base + 1] = (float) random.nextGaussian();
jitter[base + 2] = random.nextFloat();
jitter[base + 3] = (float) random.nextGaussian();
}
}
@@ -0,0 +1,528 @@
package com.gtnewhorizons.angelica.render;
import com.gtnewhorizons.angelica.compat.dragonapi.DragonAPICompat;
import com.gtnewhorizons.angelica.config.AngelicaConfig;
import com.gtnewhorizons.angelica.config.SystemProperties;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import com.gtnewhorizons.angelica.glsm.ffp.Instancing;
import com.gtnewhorizons.angelica.glsm.ffp.VAOManager;
import com.gtnewhorizons.angelica.glsm.ffp.WeatherInstancedAttribs;
import com.gtnewhorizons.angelica.glsm.ffp.WeatherParams;
import com.gtnewhorizons.angelica.glsm.ffp.WeatherQuadMesh;
import com.gtnewhorizons.angelica.glsm.profiling.Tracy;
import com.gtnewhorizons.angelica.mixins.interfaces.EntityRendererAccessor;
import jss.notfine.core.SettingsManager;
import net.coderbot.iris.Iris;
import net.coderbot.iris.pipeline.DeferredWorldRenderingPipeline;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.particle.EntityRainFX;
import net.minecraft.client.particle.EntitySmokeFX;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraft.world.biome.WorldChunkManager;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;
/**
* Replaces EntityRenderer.renderRainSnow.
*
* <p>The whole grid is one instanced draw per precipitation type. Everything that varies per frame
* (billboard axis, distance falloff, texture scroll, snow drift) happens in the vertex shader, and the
* instance buffer is only rebuilt when the camera changes block or {@link WeatherColumns} actually
* resamples something. Per-column data comes from that cache rather than being recomputed every frame.
*/
public final class WeatherRenderer {
private static final ResourceLocation RAIN_TEXTURE = new ResourceLocation("textures/environment/rain.png");
private static final ResourceLocation SNOW_TEXTURE = new ResourceLocation("textures/environment/snow.png");
private static final int REBUILD_TICKS = 20;
private static final long P_STRENGTH = Tracy.plotHandle("weather.strength");
private static final long P_QUADS = Tracy.plotHandle("weather.quads");
private static final long P_DRAW_CALLS = Tracy.plotHandle("weather.drawCalls");
private static final long P_MICROS = Tracy.plotHandle("weather.micros");
private static final long P_REBUILDS = Tracy.plotHandle("weather.rebuilds");
private static final long P_PARTICLE_MICROS = Tracy.plotHandle("weather.particleMicros");
private static final long P_PARTICLES_SPAWNED = Tracy.plotHandle("weather.particlesSpawned");
private static final WeatherColumns columns = new WeatherColumns();
private static float[] axisX;
private static float[] axisZ;
private static int instanceBuffer;
private static ByteBuffer instanceData;
private static int instanceCapacity;
private static int instanceCount;
private static int rainInstances;
private static int builtRevision = Integer.MIN_VALUE;
private static int builtX = Integer.MIN_VALUE;
private static int builtY = Integer.MIN_VALUE;
private static int builtZ = Integer.MIN_VALUE;
private static int builtRadius = -1;
private static int builtUpdateCount = Integer.MIN_VALUE;
private static int builtCameraY = Integer.MIN_VALUE;
private static boolean rebuiltThisFrame;
private static final int PARTICLE_RADIUS = 10;
private static final Random particleRandom = new Random();
private static int rainSoundCounter;
private WeatherRenderer() {}
/** Replaces EntityRenderer.addRainParticles */
public static boolean spawnRainParticles() {
if (!AngelicaConfig.enableInstancedWeather) return false;
if (SettingsManager.downfallDistance < PARTICLE_RADIUS) return false;
final long startNanos = System.nanoTime();
final Minecraft mc = Minecraft.getMinecraft();
final WorldClient world = mc.theWorld;
if (world.provider.getWeatherRenderer() != null) return false;
float strength = world.getRainStrength(1.0F);
if (!mc.gameSettings.fancyGraphics) strength /= 2.0F;
if (strength == 0.0F) {
Tracy.plot(P_PARTICLE_MICROS, 0.0);
Tracy.plotInt(P_PARTICLES_SPAWNED, 0);
return true;
}
int attempts = (int) (100.0F * strength * strength);
if (mc.gameSettings.particleSetting == 1) attempts >>= 1;
else if (mc.gameSettings.particleSetting == 2) attempts = 0;
final int updateCount = ((EntityRendererAccessor) mc.entityRenderer).getRendererUpdateCount();
particleRandom.setSeed(updateCount * 312987231L);
final EntityLivingBase view = mc.renderViewEntity;
final int originX = MathHelper.floor_double(view.posX);
final int originY = MathHelper.floor_double(view.posY);
final int originZ = MathHelper.floor_double(view.posZ);
double soundX = 0.0D, soundY = 0.0D, soundZ = 0.0D;
int landed = 0;
int spawned = 0;
for (int attempt = 0; attempt < attempts; attempt++) {
final int x = originX + particleRandom.nextInt(PARTICLE_RADIUS) - particleRandom.nextInt(PARTICLE_RADIUS);
final int z = originZ + particleRandom.nextInt(PARTICLE_RADIUS) - particleRandom.nextInt(PARTICLE_RADIUS);
final int height;
final boolean eligible;
if (columns.covers(x, z)) {
height = columns.heightAt(x, z);
eligible = columns.strikesLightning(x, z)
&& height <= originY + PARTICLE_RADIUS && height >= originY - PARTICLE_RADIUS
&& columns.temperatureAt(x, z, height) >= 0.15F;
} else {
height = world.getPrecipitationHeight(x, z);
final BiomeGenBase biome = world.getBiomeGenForCoords(x, z);
eligible = height <= originY + PARTICLE_RADIUS && height >= originY - PARTICLE_RADIUS
&& biome.canSpawnLightningBolt() && biome.getFloatTemperature(x, height, z) >= 0.15F;
}
if (!eligible) continue;
final float offsetX = particleRandom.nextFloat();
final float offsetZ = particleRandom.nextFloat();
final Block block = world.getBlock(x, height - 1, z);
final Material material = block.getMaterial();
if (material == Material.air) continue;
final double dropY = (height + 0.1F) - block.getBlockBoundsMinY();
if (material == Material.lava) {
mc.effectRenderer.addEffect(new EntitySmokeFX(world, x + offsetX, dropY, z + offsetZ, 0.0D, 0.0D, 0.0D));
} else {
landed++;
if (particleRandom.nextInt(landed) == 0) {
soundX = x + offsetX;
soundY = dropY;
soundZ = z + offsetZ;
}
mc.effectRenderer.addEffect(new EntityRainFX(world, x + offsetX, dropY, z + offsetZ));
}
spawned++;
}
Tracy.plotInt(P_PARTICLES_SPAWNED, spawned);
Tracy.plot(P_PARTICLE_MICROS, (System.nanoTime() - startNanos) / 1000.0);
if (landed > 0 && particleRandom.nextInt(3) < rainSoundCounter++) {
rainSoundCounter = 0;
final int playerHeight = columns.covers(originX, originZ) && columns.precipitates(originX, originZ)
? columns.heightAt(originX, originZ)
: world.getPrecipitationHeight(originX, originZ);
if (soundY > view.posY + 1.0D && playerHeight > originY) {
world.playSound(soundX, soundY, soundZ, "ambient.weather.rain", 0.1F, 0.5F, false);
} else {
world.playSound(soundX, soundY, soundZ, "ambient.weather.rain", 0.2F, 1.0F, false);
}
}
return true;
}
public static boolean render(float partialTicks) {
final Minecraft mc = Minecraft.getMinecraft();
final WorldClient world = mc.theWorld;
final long startNanos = System.nanoTime();
if (!AngelicaConfig.enableInstancedWeather) return false;
if (world.provider.getWeatherRenderer() != null) {
Tracy.plot(P_STRENGTH, 0.0);
report(0, 0, startNanos);
return false;
}
if (DragonAPICompat.forcingSeasonalSnow) {
report(0, 0, startNanos);
return false;
}
final float rainStrength = world.getRainStrength(partialTicks);
Tracy.plot(P_STRENGTH, rainStrength);
final int radius = SettingsManager.downfallDistance;
if (rainStrength <= 0.0F || radius <= 0) {
report(0, 0, startNanos);
return true;
}
buildAxisTable();
final EntityLivingBase view = mc.renderViewEntity;
final int entityBlockX = MathHelper.floor_double(view.posX);
final int entityBlockY = MathHelper.floor_double(view.posY);
final int entityBlockZ = MathHelper.floor_double(view.posZ);
final double cameraX = view.lastTickPosX + (view.posX - view.lastTickPosX) * partialTicks;
final double cameraY = view.lastTickPosY + (view.posY - view.lastTickPosY) * partialTicks;
final double cameraZ = view.lastTickPosZ + (view.posZ - view.lastTickPosZ) * partialTicks;
final int cameraBlockY = MathHelper.floor_double(cameraY);
final int updateCount = ((EntityRendererAccessor) mc.entityRenderer).getRendererUpdateCount();
columns.update(world, entityBlockX, entityBlockZ, radius, updateCount);
final DeferredWorldRenderingPipeline deferred = deferredPipeline();
if (deferred != null && !deferred.supportsInstancing(Instancing.WEATHER)) {
renderTessellator(mc, world, view, partialTicks, rainStrength, radius, updateCount,
entityBlockX, entityBlockY, entityBlockZ, cameraX, cameraY, cameraZ, cameraBlockY, startNanos);
return true;
}
renderInstanced(mc, world, view, deferred, partialTicks, rainStrength, radius, updateCount,
entityBlockX, entityBlockY, entityBlockZ, cameraX, cameraY, cameraZ, cameraBlockY, startNanos);
return true;
}
private static DeferredWorldRenderingPipeline deferredPipeline() {
if (!AngelicaConfig.enableIris || !Iris.enabled) return null;
return Iris.getPipelineManager().getPipelineNullable() instanceof DeferredWorldRenderingPipeline pipeline
? pipeline : null;
}
private static void renderInstanced(Minecraft mc, WorldClient world, EntityLivingBase view,
DeferredWorldRenderingPipeline deferred, float partialTicks, float rainStrength, int radius,
int updateCount, int entityBlockX, int entityBlockY, int entityBlockZ, double cameraX,
double cameraY, double cameraZ, int cameraBlockY, long startNanos) {
final boolean rebuild = SystemProperties.WEATHER_REBUILD_ALWAYS
|| updateCount - builtUpdateCount >= REBUILD_TICKS
|| columns.revision() != builtRevision
|| entityBlockX != builtX || entityBlockY != builtY || entityBlockZ != builtZ
|| cameraBlockY != builtCameraY
|| radius != builtRadius;
if (rebuild) {
rebuildInstances(world, radius, entityBlockX, entityBlockY, entityBlockZ, cameraBlockY);
builtCameraY = cameraBlockY;
builtRevision = columns.revision();
builtX = entityBlockX;
builtY = entityBlockY;
builtZ = entityBlockZ;
builtRadius = radius;
builtUpdateCount = updateCount;
}
rebuiltThisFrame = rebuild;
if (instanceCount == 0) {
report(0, 0, startNanos);
return;
}
if (deferred != null) {
deferred.rebindCurrentPass();
if (!deferred.hasInstancedVariant(Instancing.WEATHER)) {
renderTessellator(mc, world, view, partialTicks, rainStrength, radius, updateCount,
entityBlockX, entityBlockY, entityBlockZ, cameraX, cameraY, cameraZ, cameraBlockY, startNanos);
return;
}
}
WeatherParams.set((float) (entityBlockX - cameraX), (float) -cameraY, (float) (entityBlockZ - cameraZ),
1.0F / radius,
(float) (view.posX - entityBlockX), (float) (view.posZ - entityBlockZ), partialTicks,
updateCount + partialTicks,
updateCount & 31, updateCount & 511, rainStrength);
mc.entityRenderer.enableLightmap(partialTicks);
GLStateManager.disableCull();
GLStateManager.glNormal3f(0.0F, 1.0F, 0.0F);
GLStateManager.enableBlend();
OpenGlHelper.glBlendFunc(770, 771, 1, 0);
GLStateManager.glAlphaFunc(GL11.GL_GREATER, 0.1F);
GLStateManager.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
int drawCalls = 0;
drawCalls += drawRange(mc, deferred, RAIN_TEXTURE, 0, rainInstances);
drawCalls += drawRange(mc, deferred, SNOW_TEXTURE, rainInstances, instanceCount - rainInstances);
GLStateManager.enableCull();
GLStateManager.disableBlend();
GLStateManager.glAlphaFunc(GL11.GL_GREATER, 0.1F);
mc.entityRenderer.disableLightmap(partialTicks);
report(instanceCount, drawCalls, startNanos);
}
private static int drawRange(Minecraft mc, DeferredWorldRenderingPipeline deferred,
ResourceLocation texture, int first, int count) {
if (count <= 0) return 0;
mc.getTextureManager().bindTexture(texture);
if (deferred != null) {
deferred.rebindCurrentPass();
if (!deferred.hasInstancedVariant(Instancing.WEATHER)) return 0;
deferred.bindInstancedVariant(Instancing.WEATHER);
}
GLStateManager.glBindVertexArray(WeatherQuadMesh.vao());
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, instanceBuffer);
WeatherInstancedAttribs.pointInstanceAttribs((long) first * WeatherInstancedAttribs.STRIDE);
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
VAOManager.setCurrentVertexFlags(WeatherQuadMesh.VERTEX_FLAGS);
if (deferred == null) GLStateManager.ffpInstancing = Instancing.WEATHER;
GLStateManager.glDrawArraysInstanced(GL11.GL_TRIANGLE_FAN, 0, WeatherQuadMesh.VERTEX_COUNT, count);
GLStateManager.ffpInstancing = Instancing.NONE;
GLStateManager.glBindVertexArray(0);
if (deferred != null) deferred.rebindCurrentPass();
return 1;
}
private static void rebuildInstances(WorldClient world, int radius, int entityBlockX, int entityBlockY,
int entityBlockZ, int cameraBlockY) {
final int slots = (radius * 2 + 1) * (radius * 2 + 1);
final int stride = WeatherInstancedAttribs.STRIDE;
if (instanceCapacity < slots) {
instanceCapacity = slots;
instanceData = ByteBuffer.allocateDirect(slots * stride).order(ByteOrder.nativeOrder());
if (instanceBuffer == 0) instanceBuffer = GLStateManager.glGenBuffers();
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, instanceBuffer);
GLStateManager.glBufferData(GL15.GL_ARRAY_BUFFER, (long) slots * stride, GL15.GL_DYNAMIC_DRAW);
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
WeatherQuadMesh.bindInstanceBuffer(instanceBuffer);
}
int count = writeColumns(world, radius, entityBlockX, entityBlockY, entityBlockZ, cameraBlockY, true, 0);
rainInstances = count;
count = writeColumns(world, radius, entityBlockX, entityBlockY, entityBlockZ, cameraBlockY, false, count);
instanceCount = count;
if (count == 0) return;
instanceData.position(0);
instanceData.limit(count * stride);
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, instanceBuffer);
GLStateManager.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0L, instanceData);
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
instanceData.clear();
}
private static int writeColumns(WorldClient world, int radius, int entityBlockX, int entityBlockY,
int entityBlockZ, int cameraBlockY, boolean wantRain, int count) {
final int stride = WeatherInstancedAttribs.STRIDE;
final WorldChunkManager chunkManager = world.getWorldChunkManager();
for (int z = entityBlockZ - radius; z <= entityBlockZ + radius; z++) {
for (int x = entityBlockX - radius; x <= entityBlockX + radius; x++) {
final int slot = columns.slotAt(x, z);
if (!columns.precipitatesAtSlot(slot)) continue;
final int precipitationHeight = columns.heightAtSlot(slot);
final int bottom = Math.max(entityBlockY - radius, precipitationHeight);
final int top = Math.max(entityBlockY + radius, precipitationHeight);
if (bottom == top) continue;
final float temperature = columns.temperatureAtSlot(slot, bottom);
final boolean rain = chunkManager.getTemperatureAtHeight(temperature, precipitationHeight) >= 0.15F;
if (rain != wantRain) continue;
final int lightY = Math.max(precipitationHeight, cameraBlockY);
int brightness = columns.lightAtSlot(world, slot, x, z, lightY);
if (!rain) brightness = (brightness * 3 + 15728880) / 4;
final int base = count * stride;
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_COLUMN_SPAN, x - entityBlockX);
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_COLUMN_SPAN + 4, z - entityBlockZ);
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_COLUMN_SPAN + 8, bottom);
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_COLUMN_SPAN + 12, top);
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_JITTER, columns.jitterAtSlot(slot, 0));
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_JITTER + 4, columns.jitterAtSlot(slot, 1));
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_JITTER + 8, columns.jitterAtSlot(slot, 2));
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_JITTER + 12, columns.jitterAtSlot(slot, 3));
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_PARAMS, brightness & 0xFFFF);
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_PARAMS + 4, brightness >> 16 & 0xFFFF);
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_PARAMS + 8, columns.hashAtSlot(slot) & 31);
instanceData.putFloat(base + WeatherInstancedAttribs.OFFSET_PARAMS + 12, rain ? 0.0F : 1.0F);
count++;
}
}
return count;
}
private static void renderTessellator(Minecraft mc, WorldClient world, EntityLivingBase view,
float partialTicks, float rainStrength, int radius, int updateCount, int entityBlockX,
int entityBlockY, int entityBlockZ, double cameraX, double cameraY, double cameraZ,
int cameraBlockY, long startNanos) {
mc.entityRenderer.enableLightmap(partialTicks);
final float age = updateCount + partialTicks;
GLStateManager.disableCull();
GLStateManager.glNormal3f(0.0F, 1.0F, 0.0F);
GLStateManager.enableBlend();
OpenGlHelper.glBlendFunc(770, 771, 1, 0);
GLStateManager.glAlphaFunc(GL11.GL_GREATER, 0.1F);
GLStateManager.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
final Tessellator tessellator = Tessellator.instance;
int bound = -1;
int quads = 0;
int drawCalls = 0;
for (int z = entityBlockZ - radius; z <= entityBlockZ + radius; z++) {
for (int x = entityBlockX - radius; x <= entityBlockX + radius; x++) {
if (!columns.precipitates(x, z)) continue;
final int precipitationHeight = columns.heightAt(x, z);
final int bottom = Math.max(entityBlockY - radius, precipitationHeight);
final int top = Math.max(entityBlockY + radius, precipitationHeight);
if (bottom == top) continue;
final int lightY = Math.max(precipitationHeight, cameraBlockY);
final float temperature = columns.temperatureAt(x, z, bottom);
final boolean rain = world.getWorldChunkManager()
.getTemperatureAtHeight(temperature, precipitationHeight) >= 0.15F;
final int axis = (z - entityBlockZ + 16) * 32 + x - entityBlockX + 16;
final float halfX = axisX[axis] * 0.5F;
final float halfZ = axisZ[axis] * 0.5F;
final double offsetX = (x + 0.5F) - view.posX;
final double offsetZ = (z + 0.5F) - view.posZ;
final float distance = MathHelper.sqrt_double(offsetX * offsetX + offsetZ * offsetZ) / radius;
if (rain) {
if (bound != 0) {
if (bound >= 0) {
tessellator.draw();
drawCalls++;
}
bound = 0;
mc.getTextureManager().bindTexture(RAIN_TEXTURE);
tessellator.startDrawingQuads();
}
final float scroll = ((updateCount + columns.hashAt(x, z) & 31) + partialTicks) / 32.0F
* (3.0F + columns.jitterAt(x, z, 0));
tessellator.setBrightness(columns.lightAt(world, x, z, lightY));
tessellator.setColorRGBA_F(1.0F, 1.0F, 1.0F,
((1.0F - distance * distance) * 0.5F + 0.5F) * rainStrength);
emit(tessellator, x, z, bottom, top, halfX, halfZ, scroll, 0.0F, 0.0F,
cameraX, cameraY, cameraZ);
} else {
if (bound != 1) {
if (bound == 0) {
tessellator.draw();
drawCalls++;
}
bound = 1;
mc.getTextureManager().bindTexture(SNOW_TEXTURE);
tessellator.startDrawingQuads();
}
final float scroll = ((updateCount & 511) + partialTicks) / 512.0F;
final float driftU = columns.jitterAt(x, z, 0) + age * 0.01F * columns.jitterAt(x, z, 1);
final float driftV = columns.jitterAt(x, z, 2) + age * columns.jitterAt(x, z, 3) * 0.001F;
tessellator.setBrightness((columns.lightAt(world, x, z, lightY) * 3 + 15728880) / 4);
tessellator.setColorRGBA_F(1.0F, 1.0F, 1.0F,
((1.0F - distance * distance) * 0.3F + 0.5F) * rainStrength);
emit(tessellator, x, z, bottom, top, halfX, halfZ, scroll, driftU, driftV,
cameraX, cameraY, cameraZ);
}
quads++;
}
}
if (bound >= 0) {
tessellator.draw();
drawCalls++;
}
GLStateManager.enableCull();
GLStateManager.disableBlend();
GLStateManager.glAlphaFunc(GL11.GL_GREATER, 0.1F);
mc.entityRenderer.disableLightmap(partialTicks);
report(quads, drawCalls, startNanos);
}
private static void emit(Tessellator tessellator, int x, int z, int bottom, int top, float halfX,
float halfZ, float scroll, float driftU, float driftV, double cameraX, double cameraY, double cameraZ) {
tessellator.setTranslation(-cameraX, -cameraY, -cameraZ);
tessellator.addVertexWithUV(x - halfX + 0.5D, bottom, z - halfZ + 0.5D, driftU, bottom / 4.0F + scroll + driftV);
tessellator.addVertexWithUV(x + halfX + 0.5D, bottom, z + halfZ + 0.5D, 1.0F + driftU, bottom / 4.0F + scroll + driftV);
tessellator.addVertexWithUV(x + halfX + 0.5D, top, z + halfZ + 0.5D, 1.0F + driftU, top / 4.0F + scroll + driftV);
tessellator.addVertexWithUV(x - halfX + 0.5D, top, z - halfZ + 0.5D, driftU, top / 4.0F + scroll + driftV);
tessellator.setTranslation(0.0D, 0.0D, 0.0D);
}
private static void report(int quads, int drawCalls, long startNanos) {
Tracy.plotInt(P_QUADS, quads);
Tracy.plotInt(P_DRAW_CALLS, drawCalls);
Tracy.plotInt(P_REBUILDS, rebuiltThisFrame ? 1 : 0);
Tracy.plot(P_MICROS, (System.nanoTime() - startNanos) / 1000.0);
rebuiltThisFrame = false;
}
private static void buildAxisTable() {
if (axisX != null) return;
axisX = new float[1024];
axisZ = new float[1024];
for (int z = 0; z < 32; z++) {
for (int x = 0; x < 32; x++) {
final float dx = x - 16;
final float dz = z - 16;
final float length = MathHelper.sqrt_float(dx * dx + dz * dz);
axisX[z << 5 | x] = -dz / length;
axisZ[z << 5 | x] = dx / length;
}
}
}
public static void clear() {
WeatherQuadMesh.delete();
if (instanceBuffer != 0) {
GLStateManager.glDeleteBuffers(instanceBuffer);
instanceBuffer = 0;
}
instanceData = null;
instanceCapacity = 0;
instanceCount = 0;
rainInstances = 0;
}
}
@@ -53,6 +53,7 @@ import com.gtnewhorizons.angelica.proxy.ClientProxy;
import com.gtnewhorizons.angelica.rendering.RenderingState;
import com.gtnewhorizons.angelica.rendering.TileEntityRenderBoundsRegistry;
import com.gtnewhorizons.angelica.rendering.culling.GpuCulling;
import com.gtnewhorizons.angelica.render.WeatherRenderer;
import com.gtnewhorizons.angelica.rendering.culling.GpuTerrainCuller;
import com.gtnewhorizons.angelica.rendering.tesr.AngelicaTesrMeshCache;
import com.gtnewhorizons.angelica.rendering.tesr.ModelPartBatcher;
@@ -168,6 +169,7 @@ public class CeleritasWorldRenderer extends SimpleWorldRenderer<WorldClient, Ang
ShadowRenderer.visibleTileEntities.clear();
ShadowRenderer.globalTileEntities.clear();
TesrLifecycle.reset();
WeatherRenderer.clear();
GpuCulling.onWorldUnload();
FfpExtendedAttribs.reset();
FogBiomeCache.invalidate();
@@ -1468,6 +1468,7 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
out[Instancing.TEMPLATE.ordinal()] = true;
out[Instancing.CUBE.ordinal()] = AngelicaConfig.cubeInstancingEnabled();
out[Instancing.PARTICLE.ordinal()] = true;
out[Instancing.WEATHER.ordinal()] = true;
return out;
}
@@ -2356,11 +2357,16 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
ProgramId.Particles, ProgramId.ParticlesTrans
};
private static final ProgramId[] WEATHER_PROGRAM_IDS = {
ProgramId.Weather
};
private static ProgramId[] attributeProgramIds(Instancing kind) {
return switch (kind) {
case NONE -> ProgramId.values();
case TEMPLATE, CUBE -> INSTANCED_PROGRAM_IDS;
case PARTICLE -> PARTICLE_PROGRAM_IDS;
case WEATHER -> WEATHER_PROGRAM_IDS;
};
}
@@ -38,6 +38,7 @@ class AttributeTransformer {
final boolean instancedVertex = vertex && parameters.instancing != Instancing.NONE;
final boolean cubeVertex = vertex && parameters.instancing == Instancing.CUBE;
final boolean particleVertex = vertex && parameters.instancing == Instancing.PARTICLE;
final boolean weatherVertex = vertex && parameters.instancing == Instancing.WEATHER;
final boolean matrixVertex = vertex && parameters.instancing.hasInstanceHead();
final boolean wantsMvInverse = matrixVertex && transformer.containsCall("gl_ModelViewMatrixInverse");
CoreTransformHelper.injectMatrixUniforms(transformer, vertex ? parameters.instancing : Instancing.NONE);
@@ -47,7 +48,7 @@ class AttributeTransformer {
aliasIfUsed(transformer, "normalMatrix", "iris_NormalMatrix");
if (parameters.type == ShaderType.VERTEX) {
if (!particleVertex) {
if (!particleVertex && !weatherVertex) {
transformer.injectVariable("layout(location = 0) in vec4 iris_Vertex;");
transformer.injectVariable("layout(location = 1) in vec4 iris_Color;");
if (cubeVertex) {
@@ -109,6 +110,10 @@ class AttributeTransformer {
init.append(" iris_MultiTexCoord0 = ").append(InstancedGlslHelpers.particleUv("iris_InstUv", "iris_ParticleCorner")).append(';');
init.append(" iris_MultiTexCoord1 = vec4(iris_InstLightmap, 0.0, 1.0);");
assignMidTexCoord(transformer, init, "vec4((iris_InstUv.x + iris_InstUv.z) * 0.5, (iris_InstUv.y + iris_InstUv.w) * 0.5, 0.0, 1.0)");
} else if (weatherVertex) {
init.append(InstancedGlslHelpers.weatherPrologue("iris_", "iris_", "iris_WeatherCorner",
"iris_Vertex", "iris_Color", "iris_MultiTexCoord0", "iris_MultiTexCoord1", " "));
assignMidTexCoord(transformer, init, "vec4(0.5, 0.5, 0.0, 1.0)");
} else {
init.append("iris_ModelViewMatrix = ").append(InstancedGlslHelpers.mat4FromRows("iris_InstRow0", "iris_InstRow1", "iris_InstRow2")).append(';');
init.append(" iris_NormalMatrix = ").append(InstancedGlslHelpers.mat3FromRows("iris_InstRow0", "iris_InstRow1", "iris_InstRow2")).append(';');
@@ -35,6 +35,20 @@ class CoreTransformHelper {
transformer.injectVariable("uniform mat4 iris_ModelViewMatrixInverse;");
transformer.injectVariable("uniform mat3 iris_NormalMatrix;");
}
case WEATHER -> {
transformer.injectVariable("layout(location = " + VertexFormatElement.Usage.PRIMARY_UV.getAttributeLocation() + ") in vec2 iris_WeatherCorner;");
injectAttributeDecls(transformer, instancing);
transformer.injectVariable("uniform vec4 iris_WeatherParams0;");
transformer.injectVariable("uniform vec4 iris_WeatherParams1;");
transformer.injectVariable("uniform vec4 iris_WeatherParams2;");
transformer.injectVariable("vec4 iris_Vertex;");
transformer.injectVariable("vec4 iris_Color;");
transformer.injectVariable("vec4 iris_MultiTexCoord0;");
transformer.injectVariable("vec4 iris_MultiTexCoord1;");
transformer.injectVariable("uniform mat4 iris_ModelViewMatrix;");
transformer.injectVariable("uniform mat4 iris_ModelViewMatrixInverse;");
transformer.injectVariable("uniform mat3 iris_NormalMatrix;");
}
case TEMPLATE, CUBE -> {
injectAttributeDecls(transformer, instancing);
transformer.injectVariable("mat4 iris_ModelViewMatrix;");
@@ -75,6 +75,7 @@ public final class CommonUniforms {
IdMapUniforms.addEntityIdMapUniforms(uniforms);
FogUniforms.addFogUniforms(uniforms, fogMode);
IrisInternalUniforms.addFogUniforms(uniforms, fogMode);
IrisInternalUniforms.addWeatherUniforms(uniforms);
uniforms.uniform2i("atlasSize", () -> {
final int glId = GLStateManager.getBoundTextureForServerState(0);
@@ -1,6 +1,7 @@
package net.coderbot.iris.uniforms;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import com.gtnewhorizons.angelica.glsm.ffp.WeatherParams;
import net.coderbot.iris.gl.state.FogMode;
import net.coderbot.iris.gl.state.StateUpdateNotifiers;
import net.coderbot.iris.gl.uniform.DynamicUniformHolder;
@@ -38,6 +39,19 @@ public class IrisInternalUniforms {
return func & 0x7;
}
private static final Vector4f WEATHER_0 = new Vector4f();
private static final Vector4f WEATHER_1 = new Vector4f();
private static final Vector4f WEATHER_2 = new Vector4f();
public static void addWeatherUniforms(DynamicUniformHolder uniforms) {
uniforms.uniform4f(PER_FRAME, "iris_WeatherParams0", () -> WEATHER_0.set(
WeatherParams.translateX, WeatherParams.translateY, WeatherParams.translateZ, WeatherParams.invRadius));
uniforms.uniform4f(PER_FRAME, "iris_WeatherParams1", () -> WEATHER_1.set(
WeatherParams.cameraFracX, WeatherParams.cameraFracZ, WeatherParams.partialTicks, WeatherParams.age));
uniforms.uniform4f(PER_FRAME, "iris_WeatherParams2", () -> WEATHER_2.set(
WeatherParams.rainScroll, WeatherParams.snowScroll, WeatherParams.rainStrength, 0.0f));
}
public static void addFogUniforms(DynamicUniformHolder uniforms, FogMode fogMode) {
uniforms.uniform4f(PER_FRAME, "iris_FogColor", () -> {
final Vector3d color = GLStateManager.getFogState().getFogColor();
@@ -47,7 +47,8 @@ public final class PerFrameUniformBlockHarvester {
private static final Set<String> LIVE_STATE_UNIFORMS = Set.of(
"gbufferModelView", "gbufferModelViewInverse", "gbufferPreviousModelView",
"gbufferProjection", "gbufferProjectionInverse", "gbufferPreviousProjection",
"fogColor", "iris_FogColor", "iris_FogStart", "iris_FogEnd", "iris_FogDensity");
"fogColor", "iris_FogColor", "iris_FogStart", "iris_FogEnd", "iris_FogDensity",
"iris_WeatherParams0", "iris_WeatherParams1", "iris_WeatherParams2");
/**
* @param primary manifest whose entry order defines the block layout