TextureBinder for dealing with vanilla RenderTypes

This commit is contained in:
Jozufozu 2021-11-09 16:48:54 -08:00
parent af98782a89
commit 94e83a10d1
5 changed files with 95 additions and 7 deletions

View file

@ -39,9 +39,10 @@ public enum GlError {
} }
public static void pollAndThrow(Supplier<String> context) { public static void pollAndThrow(Supplier<String> context) {
// TODO: build flag? to enable or disable this function
GlError poll = GlError.poll(); GlError poll = GlError.poll();
if (poll != null) { if (poll != null) {
throw new GlException(poll, context.get()); //throw new GlException(poll, context.get());
} }
} }
} }

View file

@ -9,11 +9,10 @@ import com.jozufozu.flywheel.core.shader.WorldProgram;
import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.BeginFrameEvent;
import com.jozufozu.flywheel.event.RenderLayerEvent; import com.jozufozu.flywheel.event.RenderLayerEvent;
import com.jozufozu.flywheel.util.ChunkIter; import com.jozufozu.flywheel.util.ChunkIter;
import com.mojang.blaze3d.systems.RenderSystem; import com.jozufozu.flywheel.util.TextureBinder;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
@ -103,9 +102,7 @@ public class InstanceWorld {
public void renderLayer(RenderLayerEvent event) { public void renderLayer(RenderLayerEvent event) {
event.type.setupRenderState(); event.type.setupRenderState();
ShaderInstance shader = RenderSystem.getShader(); TextureBinder.bindActiveTextures();
if (shader != null)
shader.apply();
materialManager.render(event.layer, event.viewProjection, event.camX, event.camY, event.camZ); materialManager.render(event.layer, event.viewProjection, event.camX, event.camY, event.camZ);

View file

@ -0,0 +1,59 @@
package com.jozufozu.flywheel.mixin;
import java.util.List;
import java.util.Map;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import com.jozufozu.flywheel.util.TextureBinder;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.client.renderer.texture.AbstractTexture;
@Mixin(ShaderInstance.class)
public class ShaderInstanceMixin implements TextureBinder {
@Shadow
@Final
private List<Integer> samplerLocations;
@Shadow
@Final
private List<String> samplerNames;
@Shadow
@Final
private Map<String, Object> samplerMap;
@Override
public void bind() {
int i = GlStateManager._getActiveTexture();
for(int j = 0; j < samplerLocations.size(); ++j) {
String s = samplerNames.get(j);
if (samplerMap.get(s) != null) {
RenderSystem.activeTexture('\u84c0' + j);
RenderSystem.enableTexture();
Object object = this.samplerMap.get(s);
int l = -1;
if (object instanceof RenderTarget) {
l = ((RenderTarget)object).getColorTextureId();
} else if (object instanceof AbstractTexture) {
l = ((AbstractTexture)object).getId();
} else if (object instanceof Integer) {
l = (Integer)object;
}
if (l != -1) {
RenderSystem.bindTexture(l);
}
}
}
GlStateManager._activeTexture(i);
}
}

View file

@ -0,0 +1,30 @@
package com.jozufozu.flywheel.util;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.ShaderInstance;
/**
* This is a silly hack that's needed because flywheel does things too different from vanilla.
*
* <p>
* When a {@link RenderType} is setup, the associated textures are passed to the active ShaderInstance, and properly
* bound later on when {@link ShaderInstance#apply()} is called.
* This interface (and {@link com.jozufozu.flywheel.mixin.ShaderInstanceMixin mixin} binds textures to opengl
* without binding the shader.
* </p>
*/
public interface TextureBinder {
/**
* Call this after calling {@link RenderType#setupRenderState()}.
*/
static void bindActiveTextures() {
TextureBinder shader = (TextureBinder) RenderSystem.getShader();
if (shader != null)
shader.bind();
}
void bind();
}

View file

@ -18,7 +18,8 @@
"InstanceAddMixin", "InstanceAddMixin",
"InstanceRemoveMixin", "InstanceRemoveMixin",
"LeakChunkStorageArrayMixin", "LeakChunkStorageArrayMixin",
"PausedPartialTickAccessor" "PausedPartialTickAccessor",
"ShaderInstanceMixin"
], ],
"mixins": [ "mixins": [
"matrix.Matrix3fMixin", "matrix.Matrix3fMixin",