package net.coderbot.iris.gl.program; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.glsm.RenderSystem; import it.unimi.dsi.fastutil.objects.Object2ObjectMap; import net.coderbot.iris.gl.sampler.GlSampler; import net.coderbot.iris.gl.sampler.SamplerBinding; import net.coderbot.iris.gl.sampler.SamplerHolder; import net.coderbot.iris.gl.sampler.SamplerLimits; import net.coderbot.iris.gl.state.ValueUpdateNotifier; import net.coderbot.iris.gl.texture.TextureAccess; import com.gtnewhorizons.angelica.glsm.texture.TextureType; import net.coderbot.iris.shaderpack.PackRenderTargetDirectives; import org.lwjgl.opengl.GL13; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.function.IntSupplier; import java.util.function.Supplier; public class ProgramSamplers { private static ProgramSamplers active; private final ImmutableList samplerBindings; private final ImmutableList notifiersToReset; private List initializer; private ProgramSamplers(ImmutableList samplerBindings, ImmutableList notifiersToReset, List initializer) { this.samplerBindings = samplerBindings; this.notifiersToReset = notifiersToReset; this.initializer = initializer; } public void update() { if (active != null) { active.removeListeners(); } active = this; int activeTexture = GLStateManager.getActiveTextureUnit(); try { if (initializer != null) { for (GlUniform1iCall call : initializer) { RenderSystem.uniform1i(call.getLocation(), call.getValue()); } initializer = null; } for (int i = 0, n = samplerBindings.size(); i < n; i++) { samplerBindings.get(i).update(); } } catch (RuntimeException | Error e) { clearActiveSamplers(); throw e; } finally { GLStateManager.glActiveTexture(GL13.GL_TEXTURE0 + activeTexture); } } public void removeListeners() { active = null; for (int i = 0, n = notifiersToReset.size(); i < n; i++) { notifiersToReset.get(i).setListener(null); } } public static void clearActiveSamplers() { if (active != null) { active.removeListeners(); } RenderSystem.unbindAllSamplers(); } public static Builder builder(int program, Set reservedTextureUnits) { return new Builder(program, reservedTextureUnits); } public static CustomTextureSamplerInterceptor customTextureSamplerInterceptor(SamplerHolder samplerHolder, Object2ObjectMap customTextureIds) { return customTextureSamplerInterceptor(samplerHolder, customTextureIds, ImmutableSet.of()); } public static CustomTextureSamplerInterceptor customTextureSamplerInterceptor(SamplerHolder samplerHolder, Object2ObjectMap customTextureIds, ImmutableSet flippedAtLeastOnceSnapshot) { return new CustomTextureSamplerInterceptor(samplerHolder, customTextureIds, flippedAtLeastOnceSnapshot); } public static final class Builder implements SamplerHolder { private final int program; private final ImmutableSet reservedTextureUnits; private final ImmutableList.Builder samplers; private final ImmutableList.Builder notifiersToReset; private final List calls; private int remainingUnits; private int nextUnit; private Builder(int program, Set reservedTextureUnits) { this.program = program; this.reservedTextureUnits = ImmutableSet.copyOf(reservedTextureUnits); this.samplers = ImmutableList.builder(); this.notifiersToReset = ImmutableList.builder(); this.calls = new ArrayList<>(); int maxTextureUnits = SamplerLimits.get().getMaxTextureUnits(); for (int unit : reservedTextureUnits) { if (unit >= maxTextureUnits) { throw new IllegalStateException("Cannot mark texture unit " + unit + " as reserved because that " + "texture unit isn't available on this system! Only " + maxTextureUnits + " texture units are available."); } } this.remainingUnits = maxTextureUnits - reservedTextureUnits.size(); this.nextUnit = 0; while (reservedTextureUnits.contains(nextUnit)) { nextUnit += 1; } //System.out.println("Begin building samplers. Reserved texture units are " + reservedTextureUnits + // ", next texture unit is " + nextUnit + ", there are " + remainingUnits + " units remaining."); } @Override public void addExternalSampler(int textureUnit, String... names) { if (!reservedTextureUnits.contains(textureUnit)) { throw new IllegalArgumentException("Cannot add an externally-managed sampler for texture unit " + textureUnit + " since it isn't in the set of reserved texture units."); } for (String name : names) { int location = RenderSystem.getUniformLocation(program, name); if (location == -1) { // There's no active sampler with this particular name in the program. continue; } // Set up this sampler uniform to use this particular texture unit. //System.out.println("Binding external sampler " + name + " to texture unit " + textureUnit); calls.add(new GlUniform1iCall(location, textureUnit)); } } @Override public boolean hasSampler(String name) { return RenderSystem.getUniformLocation(program, name) != -1; } @Override public boolean addDefaultSampler(TextureType type, IntSupplier texture, ValueUpdateNotifier notifier, Supplier sampler, String... names) { if (nextUnit != 0) { // TODO: Relax this restriction! throw new IllegalStateException("Texture unit 0 is already used."); } return addDynamicSampler(type, texture, sampler, true, notifier, names); } @Override public boolean addDynamicSampler(TextureType type, IntSupplier texture, Supplier sampler, String... names) { return addDynamicSampler(type, texture, sampler, false, null, names); } @Override public boolean addDynamicSampler(TextureType type, IntSupplier texture, ValueUpdateNotifier notifier, Supplier sampler, String... names) { return addDynamicSampler(type, texture, sampler, false, notifier, names); } private boolean addDynamicSampler(TextureType type, IntSupplier texture, Supplier sampler, boolean used, ValueUpdateNotifier notifier, String... names) { if (notifier != null) { notifiersToReset.add(notifier); } for (String name : names) { int location = RenderSystem.getUniformLocation(program, name); if (location == -1) { // There's no active sampler with this particular name in the program. continue; } // Make sure that we aren't out of texture units. if (remainingUnits <= 0) { throw new IllegalStateException("No more available texture units while activating sampler " + name); } // Set up this sampler uniform to use this particular texture unit. calls.add(new GlUniform1iCall(location, nextUnit)); // And mark this texture unit as used. used = true; } if (!used) { return false; } samplers.add(new SamplerBinding(type, nextUnit, texture, sampler, notifier)); remainingUnits -= 1; nextUnit += 1; while (remainingUnits > 0 && reservedTextureUnits.contains(nextUnit)) { nextUnit += 1; } return true; } public ProgramSamplers build() { return new ProgramSamplers(samplers.build(), notifiersToReset.build(), calls); } } public static final class CustomTextureSamplerInterceptor implements SamplerHolder { private final SamplerHolder samplerHolder; private final Object2ObjectMap customTextureIds; private final ImmutableSet deactivatedOverrides; private CustomTextureSamplerInterceptor(SamplerHolder samplerHolder, Object2ObjectMap customTextureIds, ImmutableSet flippedAtLeastOnceSnapshot) { this.samplerHolder = samplerHolder; this.customTextureIds = customTextureIds; ImmutableSet.Builder deactivatedOverrides = new ImmutableSet.Builder<>(); for (int deactivatedOverride : flippedAtLeastOnceSnapshot) { deactivatedOverrides.add("colortex" + deactivatedOverride); if (deactivatedOverride < PackRenderTargetDirectives.LEGACY_RENDER_TARGETS.size()) { deactivatedOverrides.add(PackRenderTargetDirectives.LEGACY_RENDER_TARGETS.get(deactivatedOverride)); } } this.deactivatedOverrides = deactivatedOverrides.build(); } private TextureAccess getOverride(String... names) { for (String name : names) { if (customTextureIds.containsKey(name) && !deactivatedOverrides.contains(name)) { return customTextureIds.get(name); } } return null; } @Override public void addExternalSampler(int textureUnit, String... names) { TextureAccess override = getOverride(names); if (override != null) { addOverride(override, textureUnit == 0, names); } else { samplerHolder.addExternalSampler(textureUnit, names); } } @Override public boolean hasSampler(String name) { return samplerHolder.hasSampler(name); } @Override public boolean addDefaultSampler(TextureType type, IntSupplier texture, ValueUpdateNotifier notifier, Supplier sampler, String... names) { return addSampler(type, texture, notifier, sampler, true, names); } @Override public boolean addDynamicSampler(TextureType type, IntSupplier texture, Supplier sampler, String... names) { return addSampler(type, texture, null, sampler, false, names); } @Override public boolean addDynamicSampler(TextureType type, IntSupplier texture, ValueUpdateNotifier notifier, Supplier sampler, String... names) { return addSampler(type, texture, notifier, sampler, false, names); } private boolean addSampler(TextureType type, IntSupplier texture, ValueUpdateNotifier notifier, Supplier sampler, boolean defaultSampler, String... names) { final TextureAccess override = getOverride(names); if (override != null) { return addOverride(override, defaultSampler, names); } return defaultSampler ? samplerHolder.addDefaultSampler(type, texture, notifier, sampler, names) : samplerHolder.addDynamicSampler(type, texture, notifier, sampler, names); } private boolean addOverride(TextureAccess override, boolean defaultSampler, String... names) { return defaultSampler ? samplerHolder.addDefaultSampler(override.getType(), override.getTextureId(), null, null, names) : samplerHolder.addDynamicSampler(override.getType(), override.getTextureId(), null, names); } } }