Skip to content

Commit

Permalink
Initial CSM...
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Aug 29, 2024
1 parent ac1f3a2 commit 2735463
Show file tree
Hide file tree
Showing 29 changed files with 1,362 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.irisshaders.iris.pipeline.IrisRenderingPipeline;
import net.irisshaders.iris.shaderpack.programs.ProgramSource;
import net.irisshaders.iris.shaderpack.properties.CloudSetting;
import net.irisshaders.iris.shadows.ShadowRenderTargets;
import net.irisshaders.iris.targets.Blaze3dRenderTargetExt;
import net.irisshaders.iris.targets.DepthTexture;
import net.irisshaders.iris.uniforms.CapturedRenderingState;
Expand Down Expand Up @@ -81,7 +82,7 @@ public DHCompatInternal(IrisRenderingPipeline pipeline, boolean dhShadowEnabled)
ProgramSource shadow = pipeline.getDHShadowShader().get();
shadowProgram = IrisLodRenderProgram.createProgram(shadow.getName(), true, false, shadow, pipeline.getCustomUniforms(), pipeline);
if (pipeline.hasShadowRenderTargets()) {
dhShadowFramebuffer = pipeline.createDHFramebufferShadow(shadow);
dhShadowFramebuffer = pipeline.createDHFramebufferShadow(shadow, ShadowRenderTargets.TEMP_LAYER);
dhShadowFramebufferWrapper = new DhFrameBufferWrapper(dhShadowFramebuffer);
}
shouldOverrideShadow = true;
Expand Down
40 changes: 40 additions & 0 deletions common/src/main/java/net/irisshaders/iris/gl/IrisRenderSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.irisshaders.iris.Iris;
import net.irisshaders.iris.gl.sampler.SamplerLimits;
import net.irisshaders.iris.mixin.GlStateManagerAccessor;
import net.irisshaders.iris.pbr.TextureInfoCache;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Vector3i;
Expand Down Expand Up @@ -89,13 +90,16 @@ public static void texImage1D(int texture, int target, int level, int internalfo

public static void texImage2D(int texture, int target, int level, int internalformat, int width, int height, int border, int format, int type, @Nullable ByteBuffer pixels) {
RenderSystem.assertOnRenderThreadOrInit();
TextureInfoCache.INSTANCE.onTexImage(target, texture, level, internalformat, width, height);

IrisRenderSystem.bindTextureForSetup(target, texture);
GL32C.glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
}

public static void texImage3D(int texture, int target, int level, int internalformat, int width, int height, int depth, int border, int format, int type, @Nullable ByteBuffer pixels) {
RenderSystem.assertOnRenderThreadOrInit();
IrisRenderSystem.bindTextureForSetup(target, texture);
TextureInfoCache.INSTANCE.onTexImage(target, texture, level, internalformat, width, height);
GL30C.glTexImage3D(target, level, internalformat, width, height, depth, border, format, type, pixels);
}

Expand Down Expand Up @@ -236,6 +240,10 @@ public static void framebufferTexture2D(int fb, int fbtarget, int attachment, in
dsaState.framebufferTexture2D(fb, fbtarget, attachment, target, texture, levels);
}

public static void framebufferTextureLayer(int fb, int fbtarget, int attachment, int target, int texture, int levels, int layer) {
dsaState.framebufferTextureLayer(fb, fbtarget, attachment, target, texture, levels, layer);
}

public static int getTexParameteri(int texture, int target, int pname) {
RenderSystem.assertOnRenderThreadOrInit();
return dsaState.getTexParameteri(texture, target, pname);
Expand Down Expand Up @@ -454,6 +462,10 @@ public static int createBuffers() {
return dsaState.createBuffers();
}

public static void createTextures(int target, int[] textures) {
dsaState.createTextures(target, textures);
}

public interface DSAAccess {
void generateMipmaps(int texture, int target);

Expand All @@ -479,10 +491,14 @@ public interface DSAAccess {

void framebufferTexture2D(int fb, int fbtarget, int attachment, int target, int texture, int levels);

void framebufferTextureLayer(int fb, int fbtarget, int attachment, int target, int texture, int levels, int layer);

int createFramebuffer();

int createTexture(int target);

void createTextures(int target, int[] textures);

int createBuffers();
}

Expand Down Expand Up @@ -566,6 +582,11 @@ public void framebufferTexture2D(int fb, int fbtarget, int attachment, int targe
ARBDirectStateAccess.glNamedFramebufferTexture(fb, attachment, texture, levels);
}

@Override
public void framebufferTextureLayer(int fb, int fbtarget, int attachment, int target, int texture, int levels, int layer) {
ARBDirectStateAccess.glNamedFramebufferTextureLayer(fb, attachment, texture, levels, layer);
}

@Override
public int createFramebuffer() {
return ARBDirectStateAccess.glCreateFramebuffers();
Expand All @@ -575,6 +596,11 @@ public int createFramebuffer() {
public int createTexture(int target) {
return ARBDirectStateAccess.glCreateTextures(target);
}

@Override
public void createTextures(int target, int[] textures) {
ARBDirectStateAccess.glCreateTextures(target, textures);
}
}

public static class DSAUnsupported implements DSAAccess {
Expand Down Expand Up @@ -659,6 +685,12 @@ public void framebufferTexture2D(int fb, int fbtarget, int attachment, int targe
GL32C.glFramebufferTexture2D(fbtarget, attachment, target, texture, levels);
}

@Override
public void framebufferTextureLayer(int fb, int fbtarget, int attachment, int target, int texture, int levels, int layer) {
GlStateManager._glBindFramebuffer(fbtarget, fb);
GL32C.glFramebufferTextureLayer(fbtarget, attachment, texture, levels, layer);
}

@Override
public int createFramebuffer() {
int framebuffer = GlStateManager.glGenFramebuffers();
Expand All @@ -673,6 +705,14 @@ public int createTexture(int target) {
return texture;
}

@Override
public void createTextures(int target, int[] textures) {
GL46C.glGenTextures(textures);
for (int texture : textures) {
GL46C.glBindTexture(target, texture);
}
}

@Override
public int createBuffers() {
return GlStateManager._glGenBuffers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,35 @@ public void addDepthAttachment(int texture) {
this.hasDepthAttachment = true;
}

public void addDepthAttachmentLayer(int texture, int layer) {
int internalFormat = TextureInfoCache.INSTANCE.getInfo(texture).getInternalFormat();
DepthBufferFormat depthBufferFormat = DepthBufferFormat.fromGlEnumOrDefault(internalFormat);

int fb = getGlId();

if (depthBufferFormat.isCombinedStencil()) {
IrisRenderSystem.framebufferTextureLayer(fb, GL30C.GL_FRAMEBUFFER, GL30C.GL_DEPTH_STENCIL_ATTACHMENT, GL30C.GL_TEXTURE_2D_ARRAY, texture, 0, layer);
} else {
IrisRenderSystem.framebufferTextureLayer(fb, GL30C.GL_FRAMEBUFFER, GL30C.GL_DEPTH_ATTACHMENT, GL30C.GL_TEXTURE_2D_ARRAY, texture, 0, layer);
}

this.hasDepthAttachment = true;
}

public void addColorAttachment(int index, int texture) {
int fb = getGlId();

IrisRenderSystem.framebufferTexture2D(fb, GL30C.GL_FRAMEBUFFER, GL30C.GL_COLOR_ATTACHMENT0 + index, GL30C.GL_TEXTURE_2D, texture, 0);
attachments.put(index, texture);
}

public void addColorAttachmentLayer(int index, int texture, int layer) {
int fb = getGlId();

IrisRenderSystem.framebufferTextureLayer(fb, GL30C.GL_FRAMEBUFFER, GL30C.GL_COLOR_ATTACHMENT0 + index, GL30C.GL_TEXTURE_2D_ARRAY, texture, 0, layer);
attachments.put(index, texture);
}

public void noDrawBuffers() {
IrisRenderSystem.drawBuffers(getGlId(), new int[]{GL30C.GL_NONE});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.irisshaders.iris.gl.uniform;

import net.irisshaders.iris.gl.IrisRenderSystem;
import net.irisshaders.iris.gl.state.ValueUpdateNotifier;
import org.lwjgl.opengl.GL46C;

import java.util.Arrays;
import java.util.function.Supplier;

public class FloatArrayUniform extends Uniform {
private final Supplier<float[]> value;
private float[] cachedValue;

FloatArrayUniform(int location, int count, Supplier<float[]> value) {
this(location, value, count, null);
}

FloatArrayUniform(int location, Supplier<float[]> value, int count, ValueUpdateNotifier notifier) {
super(location, notifier);

this.cachedValue = new float[count];
this.value = value;
}

@Override
public void update() {
updateValue();

if (notifier != null) {
notifier.setListener(this::updateValue);
}
}

private void updateValue() {
float[] newValue = value.get();

if (!Arrays.equals(newValue, cachedValue)) {
System.arraycopy(newValue, 0, cachedValue, 0, newValue.length);
GL46C.glUniform1fv(location, newValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.irisshaders.iris.gl.uniform;

import net.irisshaders.iris.uniforms.custom.CustomUniformFixedInputUniformsHolder;
import org.joml.Matrix4fc;
import org.joml.Vector2f;
import org.joml.Vector2i;
Expand All @@ -26,6 +27,13 @@ default LocationalUniformHolder uniform1f(UniformUpdateFrequency updateFrequency
return this;
}

@Override
default LocationalUniformHolder uniform1fArray(UniformUpdateFrequency updateFrequency, String name, int count, Supplier<float[]> value) {
location(name, UniformType.FLOAT).ifPresent(id -> addUniform(updateFrequency, new FloatArrayUniform(id, count, value)));

return this;
}

@Override
default LocationalUniformHolder uniform1f(UniformUpdateFrequency updateFrequency, String name, IntSupplier value) {
location(name, UniformType.FLOAT).ifPresent(id -> addUniform(updateFrequency, new FloatUniform(id, () -> (float) value.getAsInt())));
Expand Down Expand Up @@ -61,6 +69,13 @@ default LocationalUniformHolder uniform2f(UniformUpdateFrequency updateFrequency
return this;
}

@Override
default LocationalUniformHolder uniform2fArray(UniformUpdateFrequency updateFrequency, String name, int count, Supplier<Vector2f[]> value) {
location(name, UniformType.VEC2).ifPresent(id -> addUniform(updateFrequency, new Vector2ArrayUniform(id, count, value)));

return this;
}

@Override
default LocationalUniformHolder uniform2i(UniformUpdateFrequency updateFrequency, String name, Supplier<Vector2i> value) {
location(name, UniformType.VEC2I).ifPresent(id -> addUniform(updateFrequency, new Vector2IntegerJomlUniform(id, value)));
Expand Down Expand Up @@ -118,8 +133,8 @@ default LocationalUniformHolder uniformMatrix(UniformUpdateFrequency updateFrequ
}

@Override
default LocationalUniformHolder uniformMatrixFromArray(UniformUpdateFrequency updateFrequency, String name, Supplier<float[]> value) {
location(name, UniformType.MAT4).ifPresent(id -> addUniform(updateFrequency, new MatrixFromFloatArrayUniform(id, value)));
default LocationalUniformHolder uniformMatrixArray(UniformUpdateFrequency updateFrequency, String name, int count, Supplier<Matrix4fc[]> value) {
location(name, UniformType.MAT4).ifPresent(id -> addUniform(updateFrequency, new MatrixArrayUniform(id, count, value)));

return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package net.irisshaders.iris.gl.uniform;

import com.mojang.blaze3d.systems.RenderSystem;
import net.irisshaders.iris.gl.state.ValueUpdateNotifier;
import org.joml.Matrix4f;
import org.joml.Matrix4fc;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL46C;
import org.lwjgl.system.MemoryStack;

import java.nio.FloatBuffer;
import java.util.Arrays;
import java.util.function.Supplier;

public class MatrixArrayUniform extends Uniform {
private final Supplier<Matrix4fc[]> value;
private final int count;
private Matrix4fc[] cachedValue;

MatrixArrayUniform(int location, int count, Supplier<Matrix4fc[]> value) {
super(location);

this.count = count;
this.value = value;
}

MatrixArrayUniform(int location, int count, Supplier<Matrix4fc[]> value, ValueUpdateNotifier notifier) {
super(location, notifier);

this.count = count;
this.value = value;
}

@Override
public void update() {
updateValue();

if (notifier != null) {
notifier.setListener(this::updateValue);
}
}

public void updateValue() {
Matrix4fc[] values = value.get();

if (Arrays.equals(cachedValue, values)) {
return;
} else {
System.arraycopy(values, 0, cachedValue, 0, values.length);
}

try (MemoryStack stack = MemoryStack.stackPush()) {
long buffer = stack.nmalloc(64 * count);

int offset = 0;

for (Matrix4fc matrix : values) {
matrix.getToAddress(buffer + offset);
offset += 64;
}

GL46C.nglUniformMatrix4fv(location, count, false, buffer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
public interface UniformHolder {
UniformHolder uniform1f(UniformUpdateFrequency updateFrequency, String name, FloatSupplier value);

UniformHolder uniform1fArray(UniformUpdateFrequency updateFrequency, String name, int count, Supplier<float[]> value);

UniformHolder uniform1f(UniformUpdateFrequency updateFrequency, String name, IntSupplier value);

UniformHolder uniform1f(UniformUpdateFrequency updateFrequency, String name, DoubleSupplier value);
Expand All @@ -26,6 +28,8 @@ public interface UniformHolder {

UniformHolder uniform2f(UniformUpdateFrequency updateFrequency, String name, Supplier<Vector2f> value);

UniformHolder uniform2fArray(UniformUpdateFrequency updateFrequency, String name, int count, Supplier<Vector2f[]> value);

UniformHolder uniform2i(UniformUpdateFrequency updateFrequency, String name, Supplier<Vector2i> value);

UniformHolder uniform3f(UniformUpdateFrequency updateFrequency, String name, Supplier<Vector3f> value);
Expand All @@ -42,7 +46,7 @@ public interface UniformHolder {

UniformHolder uniformMatrix(UniformUpdateFrequency updateFrequency, String name, Supplier<Matrix4fc> value);

UniformHolder uniformMatrixFromArray(UniformUpdateFrequency updateFrequency, String name, Supplier<float[]> value);
UniformHolder uniformMatrixArray(UniformUpdateFrequency updateFrequency, String name, int count, Supplier<Matrix4fc[]> value);

UniformHolder externallyManagedUniform(String name, UniformType type);
}
Loading

0 comments on commit 2735463

Please sign in to comment.