Skip to content

Commit

Permalink
Add placeholder PBR textures fixing many issues with many shaderpacks (
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbot16 committed Mar 23, 2021
1 parent 503952c commit 306b490
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.coderbot.iris.layer.GbufferProgram;
import net.coderbot.iris.postprocess.CompositeRenderer;
import net.coderbot.iris.rendertarget.BuiltinNoiseTexture;
import net.coderbot.iris.rendertarget.SingleColorTexture;
import net.coderbot.iris.rendertarget.RenderTargets;
import net.coderbot.iris.shaderpack.ProgramSet;
import net.coderbot.iris.shaderpack.ProgramSource;
Expand Down Expand Up @@ -73,6 +74,8 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline {

private final EmptyShadowMapRenderer shadowMapRenderer;
private final CompositeRenderer compositeRenderer;
private final SingleColorTexture normals;
private final SingleColorTexture specular;

private final int waterId;

Expand Down Expand Up @@ -108,6 +111,25 @@ public DeferredWorldRenderingPipeline(ProgramSet programs) {
this.clearMainBuffers = renderTargets.createFramebufferWritingToMain(buffersToBeCleared);
this.baseline = renderTargets.createFramebufferWritingToMain(new int[] {0});

// Don't clobber anything in texture unit 0. It probably won't cause issues, but we're just being cautious here.
GlStateManager.activeTexture(GL20C.GL_TEXTURE2);

// Ensure that the pixel storage mode is in a sane state, otherwise the uploaded texture data will be quite
// incorrect.
//
// It is likely that this also avoids the crashes on AMD that I previously experienced with texture creation.
//
// This code is from Canvas: https://github.com/grondag/canvas/commit/f0ab652d7a8b7cc9febf0209bee15cffce9eac83
GlStateManager.pixelStore(GL20C.GL_UNPACK_ROW_LENGTH, 0);
GlStateManager.pixelStore(GL20C.GL_UNPACK_SKIP_ROWS, 0);
GlStateManager.pixelStore(GL20C.GL_UNPACK_SKIP_PIXELS, 0);
GlStateManager.pixelStore(GL20C.GL_UNPACK_ALIGNMENT, 4);

// Create some placeholder PBR textures for now
normals = new SingleColorTexture(0xFF7F7FFF);
specular = new SingleColorTexture(0);
GlStateManager.activeTexture(GL20C.GL_TEXTURE0);

this.shadowMapRenderer = new EmptyShadowMapRenderer(2048);
this.compositeRenderer = new CompositeRenderer(programs, renderTargets, shadowMapRenderer);
}
Expand Down Expand Up @@ -337,6 +359,10 @@ public void use() {
// a given program and bind the required textures instead.
GlStateManager.activeTexture(GL15C.GL_TEXTURE15);
BuiltinNoiseTexture.bind();
GlStateManager.activeTexture(GL15C.GL_TEXTURE2);
GlStateManager.bindTexture(normals.getTextureId());
GlStateManager.activeTexture(GL15C.GL_TEXTURE3);
GlStateManager.bindTexture(specular.getTextureId());
GlStateManager.activeTexture(GL15C.GL_TEXTURE4);
GlStateManager.bindTexture(shadowMapRenderer.getDepthTextureId());
GlStateManager.activeTexture(GL15C.GL_TEXTURE5);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.coderbot.iris.rendertarget;

import com.mojang.blaze3d.platform.GlStateManager;
import net.coderbot.iris.gl.GlResource;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11C;
import org.lwjgl.opengl.GL13C;

import java.nio.IntBuffer;

public class SingleColorTexture extends GlResource {
public SingleColorTexture(int color) {
super(GL11C.glGenTextures());
GlStateManager.bindTexture(getGlId());

IntBuffer pixel = createBuffer(color);

GL11C.glTexParameteri(GL11C.GL_TEXTURE_2D, GL11C.GL_TEXTURE_MIN_FILTER, GL11C.GL_LINEAR);
GL11C.glTexParameteri(GL11C.GL_TEXTURE_2D, GL11C.GL_TEXTURE_MAG_FILTER, GL11C.GL_LINEAR);
GL11C.glTexParameteri(GL11C.GL_TEXTURE_2D, GL11C.GL_TEXTURE_WRAP_S, GL13C.GL_REPEAT);
GL11C.glTexParameteri(GL11C.GL_TEXTURE_2D, GL11C.GL_TEXTURE_WRAP_T, GL13C.GL_REPEAT);
GL11C.glTexImage2D(GL11C.GL_TEXTURE_2D, 0, GL11C.GL_RGBA, 1, 1, 0, GL11C.GL_RGBA, GL11C.GL_UNSIGNED_BYTE, pixel);

GlStateManager.bindTexture(0);
}

private IntBuffer createBuffer(int color) {
IntBuffer pixel = BufferUtils.createIntBuffer(1);
pixel.put(color);
pixel.position(0);

return pixel;
}

public int getTextureId() {
return getGlId();
}

@Override
protected void destroyInternal() {
GL11C.glDeleteTextures(getGlId());
}
}
4 changes: 2 additions & 2 deletions src/main/java/net/coderbot/iris/texunits/TextureUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
public enum TextureUnit {
TERRAIN(0),
LIGHTMAP(1),
// TODO: Relocate this to a different texture unit, this is used by the normal map for normal shaders
OVERLAY(2);
// TODO: Relocate this to a different texture unit, this is used by depthtex0...
OVERLAY(6);

private final int samplerId;
private final int unitId;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/coderbot/iris/uniforms/CommonUniforms.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public static void addCommonUniforms(UniformHolder uniforms, IdMap idMap) {
.uniform1i(ONCE, "lightmap", TextureUnit.LIGHTMAP::getSamplerId)
.uniform1b(PER_FRAME, "hideGUI", () -> client.options.hudHidden)
.uniform1i(ONCE, "noisetex", () -> 15)
.uniform1i(ONCE, "normals", () -> 2)
.uniform1i(ONCE, "specular", () -> 3)
.uniform1i(ONCE, "shadowtex0", () -> 4)
.uniform1i(ONCE, "shadowtex1", () -> 5)
.uniform1f(PER_FRAME, "eyeAltitude", () -> Objects.requireNonNull(client.getCameraEntity()).getEyeY())
Expand Down

0 comments on commit 306b490

Please sign in to comment.