Skip to content

Commit

Permalink
Merge pull request #272 from NoiseStudio/feature/271/replace-nesl-def…
Browse files Browse the repository at this point in the history
…ault-by-compiled-shader

Replace NESL default by compiled shader
  • Loading branch information
Vixenka authored May 23, 2023
2 parents 37ef8fe + d44e334 commit 4d9673c
Show file tree
Hide file tree
Showing 57 changed files with 656 additions and 497 deletions.
3 changes: 2 additions & 1 deletion NoiseEngine.Builder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
Directory.GetParent(Environment.CurrentDirectory)!.FullName, "NoiseEngine", "BuiltInResources"
);
string outputPath = Path.Combine(
Directory.GetParent(Environment.CurrentDirectory)!.FullName, "NoiseEngine", args[0]
Directory.GetParent(Environment.CurrentDirectory)!.FullName, "NoiseEngine",
args.Length == 0 ? "bin\\Debug\\net7.0\\" : args[0]
);

Console.Write("Compiling built-in shaders...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal BufferOutputTestHelper(ApplicationFixture fixture, bool singleOutput) {
neslType = NeslTestEnvironment.GetNeslTypeFromT<T>();
shaderType = TestEmitHelper.NewType();

shaderOutput = shaderType.DefineField("Output", singleOutput ? neslType : Buffers.GetReadWriteBuffer(neslType));
shaderOutput = shaderType.DefineField("Output", singleOutput ? neslType : Buffers.GetRwBuffer(neslType));
shaderOutput.AddAttribute(UniformAttribute.Create());
}

Expand Down
2 changes: 1 addition & 1 deletion NoiseEngine.Tests/Nesl/Intrinsics/GlobalInvocationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Invocation3() {

il.Emit(OpCode.DefVariable, BuiltInTypes.UInt32);
il.Emit(OpCode.DefVariable, Vectors.GetVector3(BuiltInTypes.UInt32));
il.Emit(OpCode.Call, 2u, Compute.GlobalInvocation3, stackalloc uint[0]);
il.Emit(OpCode.Call, 2u, ComputeUtils.GlobalInvocation3, stackalloc uint[0]);
il.Emit(OpCode.LoadField, 1u, 2u, 0u);
il.Emit(OpCode.SetElement, 0u, 1u, 1u);
il.Emit(OpCode.Return);
Expand Down
11 changes: 4 additions & 7 deletions NoiseEngine.Tests/Nesl/NeslAssemblyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using NoiseEngine.Tests.Environments;
using NoiseEngine.Tests.Fixtures;
using System;
using System.IO;
using System.Linq;

namespace NoiseEngine.Tests.Nesl;
Expand Down Expand Up @@ -46,7 +45,7 @@ struct FragmentData {
FragmentData Vertex(VertexData data) {
return new FragmentData() {
Position = Vertex.ObjectToClipPos(data.Position),
Position = VertexUtils.ObjectToClipPos(data.Position),
Color = new f32v4(data.Color, data.Color.X)
};
}
Expand All @@ -59,15 +58,13 @@ f32v4 Fragment(FragmentData data) {
NeslCompilerTest.ExecuteVector3PositionVector3Color(GraphicsDevices, created, path);

byte[] bytes = created.GetRawBytes();
NeslAssembly loaded = NeslAssembly.Load(
bytes, new NeslAssembly[] { NoiseEngine.Nesl.Default.Manager.AssemblyBuilder }
);
NeslAssembly loaded = NeslAssembly.Load(bytes);

Assert.Equal(created.Name, loaded.Name);
Assert.True(created.Dependencies.Select(x => x.Name).SequenceEqual(loaded.Dependencies.Select(x => x.Name)));
Assert.True(
created.Types.Select(x => x.FullNameWithAssembly)
.SequenceEqual(loaded.Types.Select(x => x.FullNameWithAssembly))
created.Types.Select(x => x.FullNameWithAssembly).OrderBy(x => x)
.SequenceEqual(loaded.Types.Select(x => x.FullNameWithAssembly).OrderBy(x => x))
);

NeslCompilerTest.ExecuteVector3PositionVector3Color(GraphicsDevices, loaded, path);
Expand Down
2 changes: 1 addition & 1 deletion NoiseEngine.Tests/Nesl/NeslCompilerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct FragmentData {
FragmentData Vertex(VertexData data) {
return new FragmentData() {
Position = Vertex.ObjectToClipPos(data.Position),
Position = VertexUtils.ObjectToClipPos(data.Position),
Color = new f32v4(data.Color, data.Color.X)
};
}
Expand Down
2 changes: 1 addition & 1 deletion NoiseEngine.Tests/Nesl/NeslTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void MakeGeneric() {
NeslField genericField = genericType.DefineField(FieldName, genericTypeParameter);

NeslMethodBuilder genericMethod =
genericType.DefineMethod(MethodName, genericType, Buffers.GetReadWriteBuffer(genericTypeParameter));
genericType.DefineMethod(MethodName, genericType, Buffers.GetRwBuffer(genericTypeParameter));
IlGenerator il = genericMethod.IlGenerator;

il.Emit(OpCode.Load, 0u, 0u);
Expand Down
4 changes: 2 additions & 2 deletions NoiseEngine.Tests/Nesl/Operations/DefaultDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Vector4Float() {
BufferOutputTestHelper<Vector4<float>> helper = CreateBufferOutputTestHelper<Vector4<float>>();

NeslFieldBuilder field = helper.DefineField(
Buffers.GetReadWriteBuffer(Vectors.GetVector4(BuiltInTypes.Float32))
Buffers.GetRwBuffer(Vectors.GetVector4(BuiltInTypes.Float32))
);
byte[] data = new byte[4 * sizeof(float) * vectors.Length];

Expand All @@ -54,7 +54,7 @@ public void Vector4Float() {

il.Emit(OpCode.DefVariable, BuiltInTypes.UInt32);
il.Emit(OpCode.DefVariable, Vectors.GetVector3(BuiltInTypes.UInt32));
il.Emit(OpCode.Call, 3u, Compute.GlobalInvocation3, stackalloc uint[0]);
il.Emit(OpCode.Call, 3u, ComputeUtils.GlobalInvocation3, stackalloc uint[0]);
il.Emit(OpCode.LoadField, 2u, 3u, 0u);
il.Emit(OpCode.DefVariable, Vectors.GetVector4(BuiltInTypes.Float32));
il.Emit(OpCode.LoadElement, 4u, 1u, 2u);
Expand Down
1 change: 1 addition & 0 deletions NoiseEngine.Tests/NoiseEngine.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Target Name="CompileTestsNative" BeforeTargets="AfterBuild">
<Exec WorkingDirectory="../NoiseEngine.Tests.Native/" Command="cargo build $(CargoReleaseFlag)" />
<Copy SourceFiles="../NoiseEngine.Tests.Native/$(CargoBuildPath)/$(OSLibPrefix)noise_engine_tests_native.$(OSLibExt)" DestinationFiles="$(OutputPath)/$(OSLibPrefix)NoiseEngine.Native.$(OSLibExt)" />
<Copy SourceFiles="../NoiseEngine/$(OutputPath)/System.nesil" DestinationFiles="$(OutputPath)/Resources/Shaders/System.nesil" />
</Target>

</Project>
2 changes: 1 addition & 1 deletion NoiseEngine.Tests/Rendering/MeshT2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void Figure3D() {
il.Emit(OpCode.DefVariable, BuiltInTypes.Float32);

il.Emit(OpCode.LoadField, 3u, 0u, 0u);
il.Emit(OpCode.Call, 2u, Vertex.ObjectToClipPos, stackalloc uint[] { 3u });
il.Emit(OpCode.Call, 2u, VertexUtils.ObjectToClipPos, stackalloc uint[] { 3u });
il.Emit(OpCode.SetField, 1u, 0u, 2u);

il.Emit(OpCode.LoadField, 3u, 0u, 1u);
Expand Down
4 changes: 2 additions & 2 deletions NoiseEngine.Tests/Rendering/ShaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Triangle() {
NeslTypeBuilder shaderClassData = TestEmitHelper.NewType();

NeslFieldBuilder positions = shaderClassData.DefineField(
"positions", NoiseEngine.Nesl.Default.Buffers.GetReadWriteBuffer(Vectors.GetVector4(BuiltInTypes.Float32))
"positions", NoiseEngine.Nesl.Default.Buffers.GetRwBuffer(Vectors.GetVector4(BuiltInTypes.Float32))
);
byte[] data = new byte[4 * sizeof(float) * 3];

Expand Down Expand Up @@ -55,7 +55,7 @@ public void Triangle() {
il.Emit(OpCode.DefVariable, vertexData);
il.Emit(OpCode.DefVariable, Vectors.GetVector4(BuiltInTypes.Float32));
il.Emit(OpCode.DefVariable, BuiltInTypes.Int32);
il.Emit(OpCode.Call, 4u, Vertex.Index, stackalloc uint[0]);
il.Emit(OpCode.Call, 4u, VertexUtils.Index, stackalloc uint[0]);
il.Emit(OpCode.LoadElement, 3u, 0u, 4u);
il.Emit(OpCode.SetField, 2u, 0u, 3u);
il.Emit(OpCode.ReturnValue, 2u);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static class DefaultDataHelper {
public static void Store(
SpirVCompiler compiler, SpirVVariable variable, NeslField neslField, SpirVGenerator generator
) {
if (neslField.FieldType.FullNameWithAssembly != Buffers.ReadWriteBufferName)
if (neslField.FieldType.FullNameWithAssembly != Buffers.RwBufferName)
throw new NotImplementedException();

SpirVId constId = compiler.Consts.GetConsts(variable.NeslType, neslField.DefaultData!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NoiseEngine.Nesl.CompilerTools.Architectures.SpirV.Types;
using NoiseEngine.Nesl.Default;
using NoiseEngine.Rendering.Vulkan;
using System.Diagnostics;
using System.Linq;

namespace NoiseEngine.Nesl.CompilerTools.Architectures.SpirV;
Expand Down Expand Up @@ -104,6 +105,8 @@ private bool GetInnerVariablesVector(
ref uint location, ref uint offset, NeslType type, FastList<VertexInputAttributeDescription> inputAttributes,
FastList<SpirVVariable> innerVariables, uint size, VulkanFormat floatFormat
) {
Debug.Assert(!type.IsGeneric, "Type cannot be generic.");

NeslType tType = type.GenericMakedTypeParameters.Single();
switch (tType.FullNameWithAssembly) {
case BuiltInTypes.Float32Name:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace NoiseEngine.Nesl.CompilerTools.Architectures.SpirV.Intrinsics;

internal class Compute : IntrinsicsContainer {
internal class ComputeUtils : IntrinsicsContainer {

public Compute(
public ComputeUtils(
SpirVGenerator generator, SpirVFunction function, IReadOnlyList<SpirVVariable> parameters
) : base(generator, function, parameters) {
}
Expand All @@ -19,7 +19,7 @@ public override void Process() {
case NeslOperators.PropertyGet + nameof(Default.Compute.WorkgroupSize3):
EmitBuiltInVariable(Vectors.GetVector3(BuiltInTypes.UInt32), 25u);
break;*/
case NeslOperators.PropertyGet + nameof(Default.Compute.GlobalInvocation3):
case NeslOperators.PropertyGet + nameof(Default.ComputeUtils.GlobalInvocation3):
EmitBuiltInVariable(Vectors.GetVector3(BuiltInTypes.UInt32), 28u);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public static void Process(
}

switch (function.NeslMethod.Type.FullName) {
case $"{DefaultAssembly}.{nameof(Compute)}":
new Compute(generator, function, parameters).Process();
case $"{DefaultAssembly}.{nameof(ComputeUtils)}":
new ComputeUtils(generator, function, parameters).Process();
break;
case $"{DefaultAssembly}.{nameof(Vertex)}":
new Vertex(generator, function, parameters).Process();
case $"{DefaultAssembly}.{nameof(VertexUtils)}":
new VertexUtils(generator, function, parameters).Process();
break;
default:
throw new InvalidOperationException($"Unable to find given {nameof(IntrinsicAttribute)} definition.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

namespace NoiseEngine.Nesl.CompilerTools.Architectures.SpirV.Intrinsics;

internal class Vertex : IntrinsicsContainer {
internal class VertexUtils : IntrinsicsContainer {

public Vertex(
public VertexUtils(
SpirVGenerator generator, SpirVFunction function, IReadOnlyList<SpirVVariable> parameters
) : base(generator, function, parameters) {
}

public override void Process() {
switch (NeslMethod.Name) {
case NeslOperators.PropertyGet + nameof(Default.Vertex.Index):
case NeslOperators.PropertyGet + nameof(Default.VertexUtils.Index):
EmitBuiltInVariable(BuiltInTypes.Int32, 42u);
break;
case nameof(Default.Vertex.ObjectToClipPos):
case nameof(Default.VertexUtils.ObjectToClipPos):
ObjectToClipPos();
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public SpirVId GetConsts(NeslType type, IReadOnlyList<byte> data) {
return Compiler.GetConst(BinaryPrimitives.ReadInt32LittleEndian(data.ToArray()));
case BuiltInTypes.Float32Name:
return Compiler.GetConst(BinaryPrimitives.ReadSingleLittleEndian(data.ToArray()));
case Buffers.ReadWriteBufferName:
case Buffers.RwBufferName:
return GetConstsBuffer(type, data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SpirVVariable(

SpirVType type;
if (
neslType.FullNameWithAssembly == Buffers.ReadWriteBufferName &&
neslType.FullNameWithAssembly == Buffers.RwBufferName &&
storageClass != StorageClass.Uniform
) {
type = Compiler.GetSpirVType(neslType, additionalData);
Expand Down Expand Up @@ -101,7 +101,7 @@ private static StorageClass GetStorageClass(NeslField neslField) {
private static object? GetAdditionalData(NeslField neslField) {
if (
neslField.DefaultData is null ||
neslField.FieldType.FullNameWithAssembly != Buffers.ReadWriteBufferName
neslField.FieldType.FullNameWithAssembly != Buffers.RwBufferName
) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion NoiseEngine/Nesl/CompilerTools/Generics/GenericHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static class GenericHelper {

public static NeslType GetFinalType(
NeslType oldType, NeslType newType, NeslType currentType,
IDictionary<NeslGenericTypeParameter, NeslType> targetTypes
IReadOnlyDictionary<NeslGenericTypeParameter, NeslType> targetTypes
) {
if (currentType is NeslGenericTypeParameter genericTypeParameter)
return targetTypes[genericTypeParameter];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ internal static class GenericIlGenerator {

public static IlContainer RemoveGenerics(
NeslType oldType, NeslType newType, NeslMethod genericMethod,
IDictionary<NeslGenericTypeParameter, NeslType> targetTypes
IReadOnlyDictionary<NeslGenericTypeParameter, NeslType> targetTypes
) {
SerializedIlContainer container = new SerializedIlContainer(
genericMethod.Assembly,
genericMethod.GetIlContainer().GetRawInstructions(),
genericMethod.GetIlContainer().GetTail(0).ToArray()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;

namespace NoiseEngine.Nesl.CompilerTools.Generics;

internal sealed class NotFullyConstructedGenericNeslField : NeslField {

private readonly NeslField genericMaked;

public override IEnumerable<NeslAttribute> Attributes => genericMaked.Attributes;
public override IReadOnlyList<byte>? DefaultData => genericMaked.DefaultData;

public NotFullyConstructedGenericNeslField(
NotFullyConstructedGenericNeslType parentType, NeslField genericMaked, NeslType fieldType
) : base(parentType, genericMaked.Name, fieldType) {
this.genericMaked = genericMaked;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace NoiseEngine.Nesl.CompilerTools.Generics;

internal class NotFullyConstructedGenericNeslMethod : NeslMethod {
internal sealed class NotFullyConstructedGenericNeslMethod : NeslMethod {

public NeslMethod ParentMethod { get; }
public ImmutableArray<NeslType> TypeArguments { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;

namespace NoiseEngine.Nesl.CompilerTools.Generics;

internal sealed class NotFullyConstructedGenericNeslMethodForType : NeslMethod {

public NeslMethod ParentMethod { get; }

public override IEnumerable<NeslAttribute> Attributes => ParentMethod.Attributes;
public override IEnumerable<NeslAttribute> ReturnValueAttributes => ParentMethod.ReturnValueAttributes;
public override IReadOnlyList<IEnumerable<NeslAttribute>> ParameterAttributes => ParentMethod.ParameterAttributes;
public override IEnumerable<NeslGenericTypeParameter> GenericTypeParameters => ParentMethod.GenericTypeParameters;
protected override IlContainer IlContainer => ParentMethod.GetIlContainer();

public NotFullyConstructedGenericNeslMethodForType(
NotFullyConstructedGenericNeslType type, NeslMethod parentMethod, NeslType? returnType,
NeslType[] parameterTypes
) : base(type, parentMethod.Name, returnType, parameterTypes) {
ParentMethod = parentMethod;
}

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,64 @@
using System;
using NoiseEngine.Nesl.Serialization;
using NoiseEngine.Serialization;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;

namespace NoiseEngine.Nesl.CompilerTools.Generics;

internal class NotFullyConstructedGenericNeslType : NeslType {
internal sealed class NotFullyConstructedGenericNeslType : NeslType {

public NeslType ParentType { get; }
public ImmutableArray<NeslType> TypeArguments { get; }

public override IEnumerable<NeslAttribute> Attributes => ParentType.Attributes;
public override IEnumerable<NeslGenericTypeParameter> GenericTypeParameters => ParentType.GenericTypeParameters;
public override IReadOnlyList<NeslField> Fields => ParentType.Fields;
public override IEnumerable<NeslMethod> Methods => ParentType.Methods;
public override IEnumerable<NeslAttribute> Attributes => GenericMakedFrom!.Attributes;
public override IEnumerable<NeslGenericTypeParameter> GenericTypeParameters =>
GenericMakedFrom!.GenericTypeParameters;
public override IReadOnlyList<NeslField> Fields { get; }
public override IEnumerable<NeslMethod> Methods { get; }
public override NeslType? GenericMakedFrom { get; }

public NotFullyConstructedGenericNeslType(NeslType parentType, ImmutableArray<NeslType> typeArguments) : base(
public NotFullyConstructedGenericNeslType(
NeslType parentType, Dictionary<NeslGenericTypeParameter, NeslType> targetTypes,
ImmutableArray<NeslType> typeArguments
) : base(
parentType.Assembly, parentType.FullName
) {
ParentType = parentType;
GenericMakedFrom = parentType;
TypeArguments = typeArguments;

Fields = GenericMakedFrom.Fields.Select(field => new NotFullyConstructedGenericNeslField(
this, field, GenericHelper.GetFinalType(GenericMakedFrom, this, field.FieldType, targetTypes)
)).ToArray();

Methods = GenericMakedFrom.Methods.Select(method => new NotFullyConstructedGenericNeslMethodForType(
this, method,
method.ReturnType is null ? null : GenericHelper.GetFinalType(
GenericMakedFrom, this, method.ReturnType, targetTypes
),
method.ParameterTypes.Select(x => GenericHelper.GetFinalType(
GenericMakedFrom, this, x, targetTypes
)).ToArray()
)).ToArray();
}

public override NeslType MakeGeneric(params NeslType[] typeArguments) {
return ParentType.MakeGeneric(typeArguments);
return GenericMakedFrom!.MakeGeneric(typeArguments);
}

internal override void PrepareHeader(SerializationUsed used, NeslAssembly serializedAssembly) {
used.Add(this, GenericMakedFrom!);
used.Add(this, TypeArguments);
}

internal override bool SerializeHeader(NeslAssembly serializedAssembly, SerializationWriter writer) {
Debug.Assert(serializedAssembly == Assembly);

writer.WriteBool(true);
writer.WriteUInt8((byte)NeslTypeUsageKind.GenericNotFullyConstructed);
writer.WriteUInt64(Assembly.GetLocalTypeId(GenericMakedFrom!));
writer.WriteEnumerable(TypeArguments.Select(Assembly.GetLocalTypeId));
return false;
}

}
Loading

0 comments on commit 4d9673c

Please sign in to comment.