Skip to content

Commit 8ea777c

Browse files
committed
Add target attribute
1 parent afc5a5f commit 8ea777c

File tree

14 files changed

+189
-4
lines changed

14 files changed

+189
-4
lines changed

include/NZSL/Ast/Compare.inl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,12 @@ namespace nzsl::Ast
834834

835835
bool Compare(const ScopedStatement& lhs, const ScopedStatement& rhs, const ComparisonParams& params)
836836
{
837+
if (!Compare(lhs.targetType, rhs.targetType, params))
838+
return false;
839+
840+
if (!Compare(lhs.targetVersion, rhs.targetVersion, params))
841+
return false;
842+
837843
if (!Compare(lhs.statement, rhs.statement, params))
838844
return false;
839845

include/NZSL/Ast/Enums.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace nzsl::Ast
4747
Tag = 16, //< Tag (external block and external var only) - has argument string
4848
Unroll = 11, //< Unroll (for/for each only) - has argument mode
4949
Workgroup = 18, //< Work group size (function only) - has arguments X, Y, Z
50+
Target = 20, //<
5051
};
5152

5253
enum class BinaryType
@@ -268,6 +269,13 @@ namespace nzsl::Ast
268269
Minus = 1, //< -v
269270
Plus = 2, //< +v
270271
};
272+
273+
enum class TargetType
274+
{
275+
GLSL = 0,
276+
GLES = 1,
277+
SPIRV = 3,
278+
};
271279
}
272280

273281
#endif // NZSL_AST_ENUMS_HPP

include/NZSL/Ast/Nodes.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ namespace nzsl::Ast
526526
NodeType GetType() const override;
527527
void Visit(StatementVisitor& visitor) override;
528528

529+
ExpressionValue<TargetType> targetType;
530+
ExpressionValue<std::uint32_t> targetVersion;
529531
StatementPtr statement;
530532
};
531533

include/NZSL/Lang/ErrorList.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ NZSL_SHADERLANG_PARSER_ERROR(AttributeInvalidParameter, "invalid parameter {} fo
3939
NZSL_SHADERLANG_PARSER_ERROR(AttributeMultipleUnique, "attribute {} can only be present once", Ast::AttributeType)
4040
NZSL_SHADERLANG_PARSER_ERROR(AttributeParameterIdentifier, "attribute {} parameter can only be an identifier", Ast::AttributeType)
4141
NZSL_SHADERLANG_PARSER_ERROR(AttributeUnexpectedParameterCount, "attribute {} expects {} arguments, got {}", Ast::AttributeType, std::size_t, std::size_t)
42+
NZSL_SHADERLANG_PARSER_ERROR(AttributeInvalidTargetVersion, "invalid target version {} for target {}", std::int32_t, std::string)
4243
NZSL_SHADERLANG_PARSER_ERROR(ExpectedToken, "expected token {}, got {}", TokenType, TokenType)
4344
NZSL_SHADERLANG_PARSER_ERROR(DuplicateIdentifier, "duplicate identifier")
4445
NZSL_SHADERLANG_PARSER_ERROR(DuplicateModule, "duplicate module")

include/NZSL/LangWriter.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ namespace nzsl
5858
struct TagAttribute;
5959
struct UnrollAttribute;
6060
struct WorkgroupAttribute;
61+
struct TargetAttribute;
6162

6263
void Append(const Ast::AliasType& type);
6364
void Append(const Ast::ArrayType& type);
@@ -104,6 +105,7 @@ namespace nzsl
104105
void AppendAttribute(TagAttribute attribute);
105106
void AppendAttribute(UnrollAttribute attribute);
106107
void AppendAttribute(WorkgroupAttribute attribute);
108+
void AppendAttribute(TargetAttribute attribute);
107109
void AppendComment(std::string_view section);
108110
void AppendCommentSection(std::string_view section);
109111
void AppendHeader();

include/NZSL/Parser.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace nzsl
3131
static std::string_view ToString(Ast::LoopUnroll loopUnroll);
3232
static std::string_view ToString(Ast::MemoryLayout memoryLayout);
3333
static std::string_view ToString(Ast::ModuleFeature moduleFeature);
34+
static std::string_view ToString(Ast::TargetType targetType);
3435
static std::string_view ToString(ShaderStageType shaderStage);
3536

3637
private:
@@ -74,7 +75,7 @@ namespace nzsl
7475
Ast::StatementPtr ParseReturnStatement();
7576
Ast::StatementPtr ParseRootStatement(std::vector<Attribute> attributes = {});
7677
Ast::StatementPtr ParseSingleStatement();
77-
Ast::StatementPtr ParseStatement();
78+
Ast::StatementPtr ParseStatement(std::vector<Attribute> attributes = {});
7879
std::vector<Ast::StatementPtr> ParseStatementList(SourceLocation* sourceLocation);
7980
Ast::StatementPtr ParseStructDeclaration(std::vector<Attribute> attributes = {});
8081
Ast::StatementPtr ParseVariableDeclaration();

src/NZSL/Ast/AstSerializer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ namespace nzsl::Ast
504504

505505
void SerializerBase::Serialize(ScopedStatement& node)
506506
{
507+
ExprValue(node.targetType);
508+
ExprValue(node.targetVersion);
507509
Node(node.statement);
508510
}
509511

src/NZSL/Ast/Cloner.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ namespace nzsl::Ast
332332
StatementPtr Cloner::Clone(ScopedStatement& node)
333333
{
334334
auto clone = std::make_unique<ScopedStatement>();
335+
clone->targetType = Clone(node.targetType);
336+
clone->targetVersion = Clone(node.targetVersion);
335337
clone->statement = CloneStatement(node.statement);
336338

337339
clone->sourceLocation = node.sourceLocation;

src/NZSL/GlslWriter.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,6 +2703,20 @@ namespace nzsl
27032703

27042704
void GlslWriter::Visit(Ast::ScopedStatement& node)
27052705
{
2706+
if (node.targetType.IsResultingValue())
2707+
{
2708+
unsigned int glVersion = m_environment.glMajorVersion * 100 + m_environment.glMinorVersion * 10;
2709+
auto targetType = node.targetType.GetResultingValue();
2710+
std::uint32_t targetVersion = 0;
2711+
if (node.targetVersion.IsResultingValue())
2712+
targetVersion = node.targetVersion.GetResultingValue();
2713+
2714+
const auto isGLSL = !m_environment.glES && targetType == Ast::TargetType::GLSL;
2715+
const auto isGLES = m_environment.glES && targetType == Ast::TargetType::GLES;
2716+
if (!((isGLSL || isGLES) && targetVersion <= glVersion))
2717+
return;
2718+
}
2719+
27062720
EnterScope();
27072721
node.statement->Visit(*this);
27082722
LeaveScope(true);

src/NZSL/Lang/Errors.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ struct fmt::formatter<nzsl::TokenType> : formatter<string_view>
8484
}
8585
};
8686

87+
template<>
88+
struct fmt::formatter<nzsl::Ast::TargetType> : formatter<string_view>
89+
{
90+
template<typename FormatContext>
91+
auto format(const nzsl::Ast::TargetType& p, FormatContext& ctx) const -> decltype(ctx.out())
92+
{
93+
auto it = nzsl::LangData::s_targets.find(p);
94+
assert(it != nzsl::LangData::s_targets.end());
95+
96+
return formatter<string_view>::format(it->second.identifier, ctx);
97+
}
98+
};
99+
87100
namespace nzsl
88101
{
89102
std::string_view ToString(ErrorCategory errorCategory)

0 commit comments

Comments
 (0)