Skip to content

Commit

Permalink
Merge pull request #36 from Ladysnake/feature/mat4-float-array
Browse files Browse the repository at this point in the history
Add an overload method to set UniformMat4 from an array of floats
  • Loading branch information
Pyrofab authored Dec 31, 2023
2 parents 8211437 + ce809ca commit 7cc5a31
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
11 changes: 11 additions & 0 deletions src/main/java/ladysnake/satin/api/managed/uniform/UniformMat4.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,15 @@ public interface UniformMat4 {
@API(status = MAINTAINED, since = "1.4.0")
void set(Matrix4f value);

/**
* Sets the value of a 4x4 matrix uniform through a float array
*
* <p> The {@code values} array must have a length of 16, and contain the matrix elements in column-major order.
*
* @param values an array representing a 4x4 matrix
* @throws IllegalArgumentException if {@code values} has an invalid length
* @apiNote {@link #set(Matrix4f)} should generally be preferred due to its inherent type safety
*/
@API(status = MAINTAINED, since = "1.15.0")
void setFromArray(float[] values);
}
23 changes: 22 additions & 1 deletion src/main/java/ladysnake/satin/impl/ManagedUniform.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -60,6 +63,9 @@ public boolean findUniformTargets(List<PostEffectPass> 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);
}
}
Expand Down Expand Up @@ -257,4 +263,19 @@ public void set(Matrix4f value) {
}
}
}

@Override
public void setFromArray(float[] values) {
if (this.count != values.length) {
throw new IllegalArgumentException("Mismatched values size, expected " + count + " but got " + values.length);
}

GlUniform[] targets = this.targets;
int nbTargets = targets.length;
if (nbTargets > 0) {
for (GlUniform target : targets) {
target.set(values);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,47 +128,47 @@ protected <U extends ManagedUniformBase> U manageUniform(Map<String, U> 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)
Expand Down

0 comments on commit 7cc5a31

Please sign in to comment.