From 65de5452b71a311d66169ea16334e84d7e6465c1 Mon Sep 17 00:00:00 2001 From: Jay Kwak <82421531+jkwak-work@users.noreply.github.com> Date: Wed, 6 Nov 2024 16:42:46 -0800 Subject: Fix the WGSL error about semantic when a nested struct is inlined (#5506) After a struct with a semantic is inlined to a newly synthasized struct, the previous semantic must be removed in order to avoid duplicated semantic on the same location. The following function is copied from slang-ir-metal-legalize.cpp, - removeSemanticLayoutsFromLegalizedStructs() This must be refactored to reduce the code duplication later. Co-authored-by: Yong He --- source/slang/slang-ir-wgsl-legalize.cpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'source') diff --git a/source/slang/slang-ir-wgsl-legalize.cpp b/source/slang/slang-ir-wgsl-legalize.cpp index 6e554a8f8..b990ad12f 100644 --- a/source/slang/slang-ir-wgsl-legalize.cpp +++ b/source/slang/slang-ir-wgsl-legalize.cpp @@ -30,6 +30,32 @@ struct LegalizeWGSLEntryPointContext { } + void removeSemanticLayoutsFromLegalizedStructs() + { + // WGSL does not allow duplicate attributes to appear in the same shader. + // If we emit our own struct with `[[color(0)]`, all existing uses of `[[color(0)]]` + // must be removed. + for (auto field : semanticInfoToRemove) + { + auto key = field->getKey(); + // Some decorations appear twice, destroy all found + for (;;) + { + if (auto semanticDecor = key->findDecoration()) + { + semanticDecor->removeAndDeallocate(); + continue; + } + else if (auto layoutDecor = key->findDecoration()) + { + layoutDecor->removeAndDeallocate(); + continue; + } + break; + } + } + } + // Flattens all struct parameters of an entryPoint to ensure parameters are a flat struct void flattenInputParameters(EntryPointInfo entryPoint) { @@ -1419,9 +1445,8 @@ void legalizeIRForWGSL(IRModule* module, DiagnosticSink* sink) LegalizeWGSLEntryPointContext context(sink, module); for (auto entryPoint : entryPoints) - { context.legalizeEntryPointForWGSL(entryPoint); - } + context.removeSemanticLayoutsFromLegalizedStructs(); // Go through every instruction in the module and legalize them as needed. context.processInst(module->getModuleInst()); -- cgit v1.2.3