Skip to content

Commit

Permalink
Compiler: Add CMultiplePushConstantError
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Jan 15, 2025
1 parent ee3f845 commit 7b97ce7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/NZSL/Lang/ErrorList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ NZSL_SHADERLANG_COMPILER_ERROR(MissingWorkgroupAttribute, "compute shader requir
NZSL_SHADERLANG_COMPILER_ERROR(ModuleCompilationFailed, "module {} compilation failed: {}", std::string, std::string)
NZSL_SHADERLANG_COMPILER_ERROR(ModuleFeatureMismatch, "module {} requires feature {}", std::string, Ast::ModuleFeature)
NZSL_SHADERLANG_COMPILER_ERROR(ModuleNotFound, "module {} not found", std::string)
NZSL_SHADERLANG_COMPILER_ERROR(MultiplePushConstant, "there can be only one push constant external in a shader stage", std::string)
NZSL_SHADERLANG_COMPILER_ERROR(NoModuleResolver, "import statement found but no module resolver has been set (and partial sanitization is not enabled)")
NZSL_SHADERLANG_COMPILER_ERROR(OptionHashCollision, "option {} has the same hash as option {}", std::string, std::string)
NZSL_SHADERLANG_COMPILER_ERROR(OptionDeclarationInsideFunction, "options must be declared outside of functions")
Expand Down
18 changes: 12 additions & 6 deletions src/NZSL/Ast/SanitizeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,16 @@ namespace nzsl::Ast
static constexpr std::size_t ModuleIdSentinel = std::numeric_limits<std::size_t>::max();

std::array<DeclareFunctionStatement*, ShaderStageTypeCount> entryFunctions = {};
std::vector<ModuleData> modules;
std::vector<NamedExternalBlockData> namedExternalBlocks;
std::vector<StatementPtr>* currentStatementList = nullptr;
std::shared_ptr<Environment> globalEnv;
std::shared_ptr<Environment> currentEnv;
std::shared_ptr<Environment> moduleEnv;
std::unordered_map<std::string, std::size_t> moduleByName;
std::unordered_map<std::uint64_t, UsedExternalData> usedBindingIndexes;
std::unordered_map<std::string, UsedExternalData> declaredExternalVar;
std::unordered_map<OptionHash, std::string> declaredOptions;
std::shared_ptr<Environment> globalEnv;
std::shared_ptr<Environment> currentEnv;
std::shared_ptr<Environment> moduleEnv;
std::vector<ModuleData> modules;
std::vector<NamedExternalBlockData> namedExternalBlocks;
std::vector<StatementPtr>* currentStatementList = nullptr;
IdentifierList<ConstantValue> constantValues;
IdentifierList<FunctionData> functions;
IdentifierList<Identifier> aliases;
Expand All @@ -218,6 +218,7 @@ namespace nzsl::Ast
IdentifierList<ExpressionType> variableTypes;
ModulePtr currentModule;
Options options;
SourceLocation pushConstantLocation;
FunctionData* currentFunction = nullptr;
bool allowUnknownIdentifiers = false;
bool inLoop = false;
Expand Down Expand Up @@ -1600,6 +1601,11 @@ NAZARA_WARNING_GCC_DISABLE("-Wmaybe-uninitialized")

if (IsPushConstantType(targetType))
{
if (m_context->pushConstantLocation.IsValid())
throw CompilerMultiplePushConstantError{ extVar.sourceLocation };

m_context->pushConstantLocation = extVar.sourceLocation;

if (extVar.bindingSet.HasValue())
throw CompilerUnexpectedAttributeOnPushConstantError{ extVar.sourceLocation, Ast::AttributeType::Set };
else if (extVar.bindingIndex.HasValue())
Expand Down
16 changes: 16 additions & 0 deletions tests/src/Tests/ErrorsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,22 @@ external
[nzsl_version("1.0")]
module;
struct Foo
{
}
external
{
foo: push_constant[Foo],
bar: push_constant[Foo]
}
)"), "(12,2 -> 24): CMultiplePushConstant error: there can be only one push constant external in a shader stage");

CHECK_THROWS_WITH(Compile(R"(
[nzsl_version("1.0")]
module;
external
{
[binding(0)] foo: i32
Expand Down

0 comments on commit 7b97ce7

Please sign in to comment.