Skip to content

Commit

Permalink
Merge pull request #286 from NoiseStudio/feature/279/operators-in-nesl
Browse files Browse the repository at this point in the history
Operators in NESL
  • Loading branch information
Vixenka committed Jun 1, 2023
2 parents 3a6b6f8 + 8a19c86 commit a5c544c
Show file tree
Hide file tree
Showing 28 changed files with 506 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NoiseEngine.Tests.Environments;
using NoiseEngine.Tests.Fixtures;

namespace NoiseEngine.Tests.Nesl.CompilerTools.Execute.Methods.Call;

public class CallOpCode : NeslExecuteTestEnvironment {

public CallOpCode(ApplicationFixture fixture) : base(fixture) {
}

[Fact]
public void Add() {
RunKernelWithSingleBuffer(@"
using System;
uniform RwBuffer<f32> buffer;
[Kernel(1, 1, 1)]
void Main() {
buffer[0] = 1.0 + 2.0;
}
", null, 3f);
}

}
72 changes: 72 additions & 0 deletions NoiseEngine.Tests/Nesl/CompilerTools/Execute/Methods/Operators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using NoiseEngine.Tests.Environments;
using NoiseEngine.Tests.Fixtures;
using System;

namespace NoiseEngine.Tests.Nesl.CompilerTools.Execute.Methods;

public class Operators : NeslExecuteTestEnvironment {

public Operators(ApplicationFixture fixture) : base(fixture) {
}

[Theory]
[InlineData(new uint[] {
32, 51, 44, 91,
97, 21, 90, 35,
129, 72, 134, 126
})]
[InlineData(new int[] {
89, -90, 10, 28,
91, 18, -31, 75,
180, -72, -21, 103
})]
[InlineData(new float[] {
-3.51834f, 4.60347f, 5.26284f, 4.37281f,
1.30167f, 6.68611f, -7.74374f, 9.32226f,
-2.21667f, 11.2895793914794921875f, -2.480900287628173828125f, 13.69507f
})]
public void Add(object values) {
Invoker(values, "+");
}

private void Invoker(object values, string op) {
// Use typeof instead of is keyword to avoid implicit conversion.
Type type = values.GetType();
if (type == typeof(uint[]))
InvokerImpl((uint[])values, "u32", op);
else if (type == typeof(ulong[]))
InvokerImpl((ulong[])values, "u64", op);
else if (type == typeof(int[]))
InvokerImpl((int[])values, "i32", op);
else if (type == typeof(long[]))
InvokerImpl((long[])values, "i64", op);
else if (type == typeof(float[]))
InvokerImpl((float[])values, "f32", op);
else if (type == typeof(double[]))
InvokerImpl((double[])values, "f64", op);
else
throw new ArgumentException("Invalid type", nameof(values));
}

private void InvokerImpl<T>(T[] values, string type, string op) where T : unmanaged {
for (int i = 1; i <= 1; i++) {
int block = values.Length / 3;
int initialLength = block * 2;
T[] initialValues = values.AsSpan(0, initialLength).ToArray();
T[] expectedValues = values.AsSpan(initialLength).ToArray();

RunKernelWithSingleBuffer($@"
using System;
uniform RwBuffer<{type}> buffer;
[Kernel({block}, 1, 1)]
void Main() {{
buffer[ComputeUtils.GlobalInvocation3.X] = buffer[ComputeUtils.GlobalInvocation3.X] {op}
buffer[ComputeUtils.GlobalInvocation3.X + {block}];
}}
", initialValues, expectedValues);
}
}

}
8 changes: 7 additions & 1 deletion NoiseEngine/BuiltInResources/Shaders/Float32.nesl
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using System.Operators;

[Size(32)]
[PlatformDependentTypeRepresentation("OpTypeFloat`32")]
public struct Float32 {
public struct Float32 : IAdd<Float32, Float32, Float32> {

[CallOpCode(1)]
public static Float32 Add(Float32 lhs, Float32 rhs) {}

}
8 changes: 7 additions & 1 deletion NoiseEngine/BuiltInResources/Shaders/Float64.nesl
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using System.Operators;

[Size(64)]
[PlatformDependentTypeRepresentation("OpTypeFloat`64")]
public struct Float64 {
public struct Float64 : IAdd<Float64, Float64, Float64> {

[CallOpCode(1)]
public static Float64 Add(Float64 lhs, Float64 rhs) {}

}
8 changes: 7 additions & 1 deletion NoiseEngine/BuiltInResources/Shaders/Int32.nesl
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using System.Operators;

[Size(32)]
[PlatformDependentTypeRepresentation("OpTypeInt`32`1")]
public struct Int32 {
public struct Int32 : IAdd<Int32, Int32, Int32> {

[CallOpCode(1)]
public static Int32 Add(Int32 lhs, Int32 rhs) {}

}
5 changes: 5 additions & 0 deletions NoiseEngine/BuiltInResources/Shaders/Operators/IAdd.nesl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public interface IAdd<TLhs, TRhs, TResult> {

static TResult Add(TLhs lhs, TRhs rhs);

}
8 changes: 7 additions & 1 deletion NoiseEngine/BuiltInResources/Shaders/UInt32.nesl
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using System.Operators;

[Size(32)]
[PlatformDependentTypeRepresentation("OpTypeInt`32`0")]
public struct UInt32 {
public struct UInt32 : IAdd<UInt32, UInt32, UInt32> {

[CallOpCode(1)]
public static UInt32 Add(UInt32 lhs, UInt32 rhs) {}

}
6 changes: 6 additions & 0 deletions NoiseEngine/Nesl/CompilationErrorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public enum CompilationErrorType {
[CompilationErrorType(CompilationErrorSeverity.Error)]
ExpectedUsing,
[CompilationErrorType(CompilationErrorSeverity.Error)]
ExpectedOperatorKeyword,
[CompilationErrorType(CompilationErrorSeverity.Error)]
ExpectedReturn,
[CompilationErrorType(CompilationErrorSeverity.Error)]
ExpectedSemicolon,
Expand Down Expand Up @@ -130,6 +132,8 @@ public enum CompilationErrorType {
[CompilationErrorType(CompilationErrorSeverity.Error)]
GenericParameterNotFound,
[CompilationErrorType(CompilationErrorSeverity.Error)]
OperatorNotFound,
[CompilationErrorType(CompilationErrorSeverity.Error)]
UsingGenericNotAllowed,
[CompilationErrorType(CompilationErrorSeverity.Error)]
AttributeGenericNotAllowed,
Expand All @@ -150,6 +154,8 @@ public enum CompilationErrorType {
[CompilationErrorType(CompilationErrorSeverity.Error)]
ConstructorInInterfaceNotAllowed,
[CompilationErrorType(CompilationErrorSeverity.Error)]
UniformMethodNotAllowed,
[CompilationErrorType(CompilationErrorSeverity.Error)]
AbstractMethodMustBeInInterface,
[CompilationErrorType(CompilationErrorSeverity.Error)]
InheritanceTypeMustBeAInterface,
Expand Down
3 changes: 3 additions & 0 deletions NoiseEngine/Nesl/CompilerTools/Generics/GenericIlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ internal class GenericIlGenerator {
NeslType oldType, NeslType newType, NeslMethod genericMethod,
IReadOnlyDictionary<NeslGenericTypeParameter, NeslType> targetTypes
) {
if (genericMethod.IsAbstract)
return genericMethod.GetIlContainer();

IntrinsicAttribute intrinsic = IntrinsicAttribute.Create();
if (genericMethod.Attributes.Any(x => x.FullName == intrinsic.FullName))
return genericMethod.GetIlContainer();
Expand Down
1 change: 1 addition & 0 deletions NoiseEngine/Nesl/CompilerTools/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ internal struct Instruction {

public override string ToString() {
string? result = OpCode switch {
OpCode.Add => $"{ReadUInt32()}u {ReadUInt32()}u {ReadUInt32()}u",
OpCode.DefVariable => StringReadType(),
OpCode.Call => $"{ReadUInt32()}u {StringReadMethod()}",
OpCode.ReturnValue => $"{ReadUInt32()}u",
Expand Down
Loading

0 comments on commit a5c544c

Please sign in to comment.