Skip to content

Commit

Permalink
Merge pull request #267 from NoiseStudio/feature/266/add-intrinsic-to…
Browse files Browse the repository at this point in the history
…-compiled-default-shader

Add intrinsic to compiled default shader
  • Loading branch information
Vixenka authored May 21, 2023
2 parents 7ef8cf8 + d4f1490 commit 1920332
Show file tree
Hide file tree
Showing 23 changed files with 418 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using NoiseEngine.Tests.Environments;

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

public class AccessModifiers : NeslParsingTestEnvironment {

[Fact]
public void Default() {
CompileSingle(@"
struct Kkard2 {
Mock Method() {
return new Mock();
}
}
struct Mock {}
");
}

[Fact]
public void Local() {
CompileSingle(@"
struct Xori {
local Mock Method() {
return new Mock();
}
}
struct Mock {}
");
}

[Fact]
public void Internal() {
CompileSingle(@"
struct Vixen {
internal Mock Method() {
return new Mock();
}
}
struct Mock {}
");
}

[Fact]
public void Public() {
CompileSingle(@"
struct Pp {
public Mock Method() {
return new Mock();
}
}
struct Mock {}
");
}

[Fact]
public void NotExists() {
CompileSingleThrowAny(@"
struct TerQ {
NotExistsAccessModifier Mock Method() {
return new Mock();
}
}
struct Mock {}
");
}

}
17 changes: 17 additions & 0 deletions NoiseEngine.Tests/Nesl/CompilerTools/Parsing/Methods/Universal.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using NoiseEngine.Nesl;
using NoiseEngine.Nesl.Emit.Attributes.Internal;
using NoiseEngine.Tests.Environments;
using System.Linq;

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

Expand Down Expand Up @@ -41,4 +43,19 @@ void Main(f32 a) {}
");
}

[Fact]
public void Intrinsic() {
NeslMethod method = CompileSingle(@"
struct Mock {}
[Intrinsic]
Mock Main() {
return new Mock();
}
").Types.SelectMany(x => x.Methods).Single(x => x.Name == "Main");

Assert.True(method.Attributes.HasAnyAttribute(IntrinsicAttribute.Create().FullName));
Assert.Empty(method.GetIlContainer().GetInstructions());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using NoiseEngine.Tests.Environments;

namespace NoiseEngine.Tests.Nesl.CompilerTools.Parsing.Properties;

public class AccessModifiers : NeslParsingTestEnvironment {

[Fact]
public void Default() {
CompileSingle(@"
struct Kkard2 {
f32 value { get; }
}
");
}

[Fact]
public void Local() {
CompileSingle(@"
struct Xori {
local f32 value { get; }
}
");
}

[Fact]
public void Internal() {
CompileSingle(@"
struct Vixen {
internal f32 value { get; }
}
");
}

[Fact]
public void Public() {
CompileSingle(@"
struct Pp {
public f32 Value { get; }
}
");
}

[Fact]
public void NotExists() {
CompileSingleThrowAny(@"
struct TerQ {
NotExistsAccessModifier f32 value { get; }
}
");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NoiseEngine.Nesl;
using NoiseEngine.Nesl.Emit.Attributes.Internal;
using NoiseEngine.Tests.Environments;
using System.Linq;

namespace NoiseEngine.Tests.Nesl.CompilerTools.Parsing.Properties;

public class Universal : NeslParsingTestEnvironment {

[Fact]
public void Intrinsic() {
NeslType type = CompileSingle(@"
local static class Box {
[Intrinsic]
public static f32 TestProperty { get; }
}
").Types.Single();

string methodName = NeslOperators.PropertyGet + "TestProperty";
NeslMethod method = type.Methods.Single(x => x.Name == methodName);

Assert.True(method.Attributes.HasAnyAttribute(IntrinsicAttribute.Create().FullName));
Assert.Empty(method.GetIlContainer().GetInstructions());

Assert.Empty(type.Fields);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal struct Mazdamer<T> {
struct Mock {}
");

NeslType? a = assembly.Types.SingleOrDefault(x => x.Name == "Mazdamer");
NeslType? a = assembly.Types.SingleOrDefault(x => x.Name == "Mazdamer`1");
Assert.NotNull(a);
NeslType? b = assembly.Types.SingleOrDefault(x => x.Name == "Mock");
Assert.NotNull(b);
Expand All @@ -38,7 +38,7 @@ struct MockA {}
struct MockB {}
");

NeslType? a = assembly.Types.SingleOrDefault(x => x.Name == "Mazdamer");
NeslType? a = assembly.Types.SingleOrDefault(x => x.Name == "Mazdamer`2");
Assert.NotNull(a);
NeslType? b = assembly.Types.SingleOrDefault(x => x.Name == "MockA");
Assert.NotNull(b);
Expand Down Expand Up @@ -69,13 +69,13 @@ struct MockA {}
struct MockB {}
");

NeslType? a = assembly.Types.SingleOrDefault(x => x.Name == "Mazdamer");
NeslType? a = assembly.Types.SingleOrDefault(x => x.Name == "Mazdamer`2");
Assert.NotNull(a);
NeslType? b = assembly.Types.SingleOrDefault(x => x.Name == "MockA");
Assert.NotNull(b);
NeslType? c = assembly.Types.SingleOrDefault(x => x.Name == "MockB");
Assert.NotNull(c);
NeslType? compact = assembly.Types.SingleOrDefault(x => x.Name == "Compact");
NeslType? compact = assembly.Types.SingleOrDefault(x => x.Name == "Compact`1");
Assert.NotNull(compact);

NeslType maked = a!.MakeGeneric(b!, c!);
Expand Down
6 changes: 6 additions & 0 deletions NoiseEngine/BuiltInResources/Shaders/ComputeUtils.nesl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public static class ComputeUtils {

[Intrinsic]
public static u32v3 GlobalInvocation3 { get; }

}
9 changes: 9 additions & 0 deletions NoiseEngine/BuiltInResources/Shaders/Matrix4x4.nesl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[PlatformDependentTypeRepresentation("OpTypeMatrix4`{&T}`4")]
public struct Matrix4x4<T> {

public Vector4<T> C0;
public Vector4<T> C1;
public Vector4<T> C2;
public Vector4<T> C3;

}
3 changes: 3 additions & 0 deletions NoiseEngine/BuiltInResources/Shaders/RwBuffer.nesl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[PlatformDependentTypeRepresentation("OpTypeArray`{&T}")]
public class RwBuffer<T> {
}
4 changes: 2 additions & 2 deletions NoiseEngine/BuiltInResources/Shaders/Vector2.nesl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[PlatformDependentTypeRepresentation("OpTypeVector`{&T}`2")]
public struct Vector2<T> {

public T X;
public T Y;
public T X;
public T Y;

}
6 changes: 3 additions & 3 deletions NoiseEngine/BuiltInResources/Shaders/Vector3.nesl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[PlatformDependentTypeRepresentation("OpTypeVector`{&T}`3")]
public struct Vector3<T> {

public T X;
public T Y;
public T Z;
public T X;
public T Y;
public T Z;

}
8 changes: 4 additions & 4 deletions NoiseEngine/BuiltInResources/Shaders/Vector4.nesl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[PlatformDependentTypeRepresentation("OpTypeVector`{&T}`4")]
public struct Vector4<T> {

public T X;
public T Y;
public T Z;
public T W;
public T X;
public T Y;
public T Z;
public T W;

}
9 changes: 9 additions & 0 deletions NoiseEngine/BuiltInResources/Shaders/VertexUtils.nesl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public static class VertexUtils {

[Intrinsic]
public static i32 Index { get; }

[Intrinsic]
public static f32v4 ObjectToClipPos(f32v3 position) {}

}
4 changes: 3 additions & 1 deletion NoiseEngine/Nesl/CompilationErrorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ public enum CompilationErrorType {
[CompilationErrorType(CompilationErrorSeverity.Error)]
ExpectedGenericParameter,
[CompilationErrorType(CompilationErrorSeverity.Error)]
ExpectedGetter,
[CompilationErrorType(CompilationErrorSeverity.Error)]
InvalidTypeKind,
[CompilationErrorType(CompilationErrorSeverity.Warning)]
UsingAlreadyExists,
[CompilationErrorType(CompilationErrorSeverity.Error)]
TypeAlreadyExists,
[CompilationErrorType(CompilationErrorSeverity.Error)]
FieldAlreadyExists,
FieldOrPropertyAlreadyExists,
[CompilationErrorType(CompilationErrorSeverity.Error)]
MethodAlreadyExists,
[CompilationErrorType(CompilationErrorSeverity.Error)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ public void Define(
AccessModifiersToken accessModifiers, ModifiersToken modifiers, TypeIdentifierToken typeIdentifier,
NameToken name
) {
if (!Parser.TryDefineField(typeIdentifier, name.Name))
Parser.Throw(new CompilationError(name.Pointer, CompilationErrorType.FieldAlreadyExists, name.Name));
if (!Parser.TryDefineField(typeIdentifier, name.Name)) {
Parser.Throw(new CompilationError(
name.Pointer, CompilationErrorType.FieldOrPropertyAlreadyExists, name.Name
));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ public MethodDeclaration(Parser parser) : base(parser) {
}

[ParserExpression(ParserStep.TopLevel | ParserStep.Type)]
[ParserExpressionParameter(ParserTokenType.Attributes)]
[ParserExpressionParameter(ParserTokenType.AccessModifiers)]
[ParserExpressionParameter(ParserTokenType.Modifiers)]
[ParserExpressionParameter(ParserTokenType.TypeIdentifier)]
[ParserExpressionParameter(ParserTokenType.Name)]
[ParserExpressionParameter(ParserTokenType.RoundBrackets)]
[ParserExpressionParameter(ParserTokenType.CurlyBrackets)]
public void Define(
ModifiersToken modifiers, TypeIdentifierToken typeIdentifier, NameToken name, RoundBracketsToken parameters,
CurlyBracketsToken codeBlock
AttributesToken attributes, AccessModifiersToken accessModifiers, ModifiersToken modifiers,
TypeIdentifierToken typeIdentifier, NameToken name, RoundBracketsToken parameters, CurlyBracketsToken codeBlock
) {
Parser.DefineMethod(typeIdentifier, name, parameters.Buffer, codeBlock.Buffer);
Parser.DefineMethod(new MethodDefinitionData(
typeIdentifier, name, parameters.Buffer, codeBlock.Buffer,
attributes.Compile(Parser, AttributeTargets.Method
)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using NoiseEngine.Nesl.CompilerTools.Parsing.Tokens;
using System;

namespace NoiseEngine.Nesl.CompilerTools.Parsing.Expressions;

internal class PropertyDeclaration : ParserExpressionContainer {

public PropertyDeclaration(Parser parser) : base(parser) {
}

[ParserExpression(ParserStep.TopLevel | ParserStep.Type)]
[ParserExpressionParameter(ParserTokenType.Attributes)]
[ParserExpressionParameter(ParserTokenType.AccessModifiers)]
[ParserExpressionParameter(ParserTokenType.Modifiers)]
[ParserExpressionParameter(ParserTokenType.TypeIdentifier)]
[ParserExpressionParameter(ParserTokenType.Name)]
[ParserExpressionParameter(ParserTokenType.CurlyBrackets)]
public void Define(
AttributesToken attribute, AccessModifiersToken accessModifiers, ModifiersToken modifiers,
TypeIdentifierToken typeIdentifier, NameToken name, CurlyBracketsToken curlyBrackets
) {
TokenBuffer buffer = curlyBrackets.Buffer;
bool hasWord = buffer.TryReadNext(TokenType.Word, out Token word);
if (!hasWord || word.Value! != "get")
Parser.Throw(new CompilationError(word, CompilationErrorType.ExpectedGetter));
if (hasWord && !SemicolonToken.Parse(buffer, Parser.ErrorMode, out _, out CompilationError semicolonError))
Parser.Throw(semicolonError);

if (!hasWord)
return;

if (!Parser.TryDefineProperty(new PropertyDefinitionData(
typeIdentifier, name, false, false, null, attribute.Compile(Parser, AttributeTargets.Method), null,
Array.Empty<NeslAttribute>()
))) {
Parser.Throw(new CompilationError(
name.Pointer, CompilationErrorType.FieldOrPropertyAlreadyExists, name.Name
));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public void Define(
TypeKindToken typeKind, NameToken name, GenericDefineToken genericParameters, CurlyBracketsToken codeBlock
) {
string fullName = $"{Parser.GetNamespaceFromFilePath(name.Pointer.Path)}.{name.Name}";
if (genericParameters.GenericParameters.Count > 0)
fullName += $"`{genericParameters.GenericParameters.Count}";

bool successful = true;
if (!Assembly.TryDefineType(fullName, out NeslTypeBuilder? typeBuilder)) {
Parser.Throw(new CompilationError(name.Pointer, CompilationErrorType.TypeAlreadyExists, fullName));
Expand Down
Loading

0 comments on commit 1920332

Please sign in to comment.