From ce809ca293c5023a136c8d2ce93074c193f10381 Mon Sep 17 00:00:00 2001 From: Pyrofab Date: Tue, 15 Aug 2023 02:55:30 +0200 Subject: [PATCH] Add expected values count to ManagedUniform --- .../satin/api/managed/uniform/UniformMat4.java | 5 +---- .../ladysnake/satin/impl/ManagedUniform.java | 13 ++++++++++--- .../impl/ResettableManagedShaderBase.java | 18 +++++++++--------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/java/ladysnake/satin/api/managed/uniform/UniformMat4.java b/src/main/java/ladysnake/satin/api/managed/uniform/UniformMat4.java index 69b45b0..5de4ca4 100644 --- a/src/main/java/ladysnake/satin/api/managed/uniform/UniformMat4.java +++ b/src/main/java/ladysnake/satin/api/managed/uniform/UniformMat4.java @@ -17,7 +17,6 @@ */ package ladysnake.satin.api.managed.uniform; -import ladysnake.satin.impl.ManagedUniform; import org.apiguardian.api.API; import org.joml.Matrix4f; @@ -43,7 +42,5 @@ public interface UniformMat4 { * @apiNote {@link #set(Matrix4f)} should generally be preferred due to its inherent type safety */ @API(status = MAINTAINED, since = "1.15.0") - default void setFromArray(float[] values) { - ((ManagedUniform) this).setFromArray(values, 16); - } + void setFromArray(float[] values); } diff --git a/src/main/java/ladysnake/satin/impl/ManagedUniform.java b/src/main/java/ladysnake/satin/impl/ManagedUniform.java index 238344c..0f3cf01 100644 --- a/src/main/java/ladysnake/satin/impl/ManagedUniform.java +++ b/src/main/java/ladysnake/satin/impl/ManagedUniform.java @@ -44,13 +44,16 @@ public final class ManagedUniform extends ManagedUniformBase implements private static final GlUniform[] NO_TARGETS = new GlUniform[0]; + private final int count; + private GlUniform[] targets = NO_TARGETS; private int i0, i1, i2, i3; private float f0, f1, f2, f3; private boolean firstUpload = true; - public ManagedUniform(String name) { + public ManagedUniform(String name, int count) { super(name); + this.count = count; } @Override @@ -60,6 +63,9 @@ public boolean findUniformTargets(List shaders) { GlUniform uniform = shader.getProgram().getUniformByName(this.name); if (uniform != null) { + if (uniform.getCount() != this.count) { + throw new IllegalStateException("Mismatched number of values, expected " + this.count + " but JSON definition declares " + uniform.getCount()); + } list.add(uniform); } } @@ -258,8 +264,9 @@ public void set(Matrix4f value) { } } - public void setFromArray(float[] values, int count) { - if (count != values.length) { + @Override + public void setFromArray(float[] values) { + if (this.count != values.length) { throw new IllegalArgumentException("Mismatched values size, expected " + count + " but got " + values.length); } diff --git a/src/main/java/ladysnake/satin/impl/ResettableManagedShaderBase.java b/src/main/java/ladysnake/satin/impl/ResettableManagedShaderBase.java index e407caa..c96a8ab 100644 --- a/src/main/java/ladysnake/satin/impl/ResettableManagedShaderBase.java +++ b/src/main/java/ladysnake/satin/impl/ResettableManagedShaderBase.java @@ -128,47 +128,47 @@ protected U manageUniform(Map uniformM @Override public Uniform1i findUniform1i(String uniformName) { - return manageUniform(this.managedUniforms, ManagedUniform::new, uniformName, "uniform"); + return manageUniform(this.managedUniforms, name -> new ManagedUniform(name, 1), uniformName, "uniform"); } @Override public Uniform2i findUniform2i(String uniformName) { - return manageUniform(this.managedUniforms, ManagedUniform::new, uniformName, "uniform"); + return manageUniform(this.managedUniforms, name -> new ManagedUniform(name, 2), uniformName, "uniform"); } @Override public Uniform3i findUniform3i(String uniformName) { - return manageUniform(this.managedUniforms, ManagedUniform::new, uniformName, "uniform"); + return manageUniform(this.managedUniforms, name -> new ManagedUniform(name, 3), uniformName, "uniform"); } @Override public Uniform4i findUniform4i(String uniformName) { - return manageUniform(this.managedUniforms, ManagedUniform::new, uniformName, "uniform"); + return manageUniform(this.managedUniforms, name -> new ManagedUniform(name, 4), uniformName, "uniform"); } @Override public Uniform1f findUniform1f(String uniformName) { - return manageUniform(this.managedUniforms, ManagedUniform::new, uniformName, "uniform"); + return manageUniform(this.managedUniforms, name -> new ManagedUniform(name, 1), uniformName, "uniform"); } @Override public Uniform2f findUniform2f(String uniformName) { - return manageUniform(this.managedUniforms, ManagedUniform::new, uniformName, "uniform"); + return manageUniform(this.managedUniforms, name -> new ManagedUniform(name, 2), uniformName, "uniform"); } @Override public Uniform3f findUniform3f(String uniformName) { - return manageUniform(this.managedUniforms, ManagedUniform::new, uniformName, "uniform"); + return manageUniform(this.managedUniforms, name -> new ManagedUniform(name, 3), uniformName, "uniform"); } @Override public Uniform4f findUniform4f(String uniformName) { - return manageUniform(this.managedUniforms, ManagedUniform::new, uniformName, "uniform"); + return manageUniform(this.managedUniforms, name -> new ManagedUniform(name, 4), uniformName, "uniform"); } @Override public UniformMat4 findUniformMat4(String uniformName) { - return manageUniform(this.managedUniforms, ManagedUniform::new, uniformName, "uniform"); + return manageUniform(this.managedUniforms, name -> new ManagedUniform(name, 16), uniformName, "uniform"); } @API(status = INTERNAL)