Skip to content

Commit 79056cd

Browse files
jkwak-workslangbot
andauthored
Legalize the Entry-point for WGSL (shader-slang#5498)
* Legalize the Entry-point for WGSL The return type of the entry-point needs to be legalized when targeting WGSL. This commit flattens the nested-structs of the return type and the input parameters of the entry-point. Most of code is copied from the legalization code for Metal. The following functions are exactly same to the implementation for Metal or almost same. - flattenInputParameters() : 136 lines - reportUnsupportedSystemAttribute() : 7 lines - ensureResultStructHasUserSemantic() : 46 lines - struct MapStructToFlatStruct : 176 lines - flattenNestedStructs() : 95 lines - maybeFlattenNestedStructs() : 42 lines - _replaceAllReturnInst() : 19 lines - _returnNonOverlappingAttributeIndex() : 16 lines - _replaceAttributeOfLayout() : 23 lines - tryConvertValue() : 41 lines - legalizeSystemValueParameters() : 11 lines They need to be refactored to reduce the duplication later. The test case, `tests/compute/assoctype-lookup.slang`, had a bug that the compute shader was trying to use the varying input/output with the user defined semantics. This commit removes the user defined semantics, because the compute shaders cannot use the user defined semantics. --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
1 parent 4fa76f3 commit 79056cd

7 files changed

+1376
-349
lines changed

source/slang/slang-emit-c-like.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -3293,6 +3293,11 @@ void CLikeSourceEmitter::emitSemanticsUsingVarLayout(IRVarLayout* varLayout)
32933293
}
32943294
}
32953295

3296+
void CLikeSourceEmitter::emitSemanticsPrefix(IRInst* inst)
3297+
{
3298+
emitSemanticsPrefixImpl(inst);
3299+
}
3300+
32963301
void CLikeSourceEmitter::emitSemantics(IRInst* inst, bool allowOffsetLayout)
32973302
{
32983303
emitSemanticsImpl(inst, allowOffsetLayout);
@@ -3869,6 +3874,7 @@ void CLikeSourceEmitter::emitStructDeclarationsBlock(
38693874
emitPackOffsetModifier(fieldKey, fieldType, packOffsetDecoration);
38703875
}
38713876
}
3877+
emitSemanticsPrefix(fieldKey);
38723878
emitStructFieldAttributes(structType, ff);
38733879
emitMemoryQualifiers(fieldKey);
38743880
emitType(fieldType, getName(fieldKey));

source/slang/slang-emit-c-like.h

+2
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ class CLikeSourceEmitter : public SourceEmitterBase
355355
void diagnoseUnhandledInst(IRInst* inst);
356356
void emitInst(IRInst* inst);
357357

358+
void emitSemanticsPrefix(IRInst* inst);
358359
void emitSemantics(IRInst* inst, bool allowOffsets = false);
359360
void emitSemanticsUsingVarLayout(IRVarLayout* varLayout);
360361

@@ -557,6 +558,7 @@ class CLikeSourceEmitter : public SourceEmitterBase
557558
SLANG_UNUSED(rate);
558559
SLANG_UNUSED(addressSpace);
559560
}
561+
virtual void emitSemanticsPrefixImpl(IRInst* inst) { SLANG_UNUSED(inst); }
560562
virtual void emitSemanticsImpl(IRInst* inst, bool allowOffsetLayout)
561563
{
562564
SLANG_UNUSED(inst);

source/slang/slang-emit-wgsl.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,32 @@ static bool isPowerOf2(const uint32_t n)
236236
return (n != 0U) && ((n - 1U) & n) == 0U;
237237
}
238238

239+
bool WGSLSourceEmitter::maybeEmitSystemSemantic(IRInst* inst)
240+
{
241+
if (auto sysSemanticDecor = inst->findDecoration<IRTargetSystemValueDecoration>())
242+
{
243+
m_writer->emit("@builtin(");
244+
m_writer->emit(sysSemanticDecor->getSemantic());
245+
m_writer->emit(")");
246+
return true;
247+
}
248+
return false;
249+
}
250+
251+
void WGSLSourceEmitter::emitSemanticsPrefixImpl(IRInst* inst)
252+
{
253+
if (!maybeEmitSystemSemantic(inst))
254+
{
255+
if (auto semanticDecoration = inst->findDecoration<IRSemanticDecoration>())
256+
{
257+
m_writer->emit("@location(");
258+
m_writer->emit(semanticDecoration->getSemanticIndex());
259+
m_writer->emit(")");
260+
return;
261+
}
262+
}
263+
}
264+
239265
void WGSLSourceEmitter::emitStructFieldAttributes(IRStructType* structType, IRStructField* field)
240266
{
241267
// Tint emits errors unless we explicitly spell out the layout in some cases, so emit

source/slang/slang-emit-wgsl.h

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class WGSLSourceEmitter : public CLikeSourceEmitter
3838
virtual void emitParamTypeImpl(IRType* type, const String& name) SLANG_OVERRIDE;
3939
virtual void _emitType(IRType* type, DeclaratorInfo* declarator) SLANG_OVERRIDE;
4040
virtual void emitFrontMatterImpl(TargetRequest* targetReq) SLANG_OVERRIDE;
41+
virtual void emitSemanticsPrefixImpl(IRInst* inst) SLANG_OVERRIDE;
4142
virtual void emitStructFieldAttributes(IRStructType* structType, IRStructField* field)
4243
SLANG_OVERRIDE;
4344
virtual void emitCallArg(IRInst* inst) SLANG_OVERRIDE;
@@ -57,6 +58,8 @@ class WGSLSourceEmitter : public CLikeSourceEmitter
5758
void ensurePrelude(const char* preludeText);
5859

5960
private:
61+
bool maybeEmitSystemSemantic(IRInst* inst);
62+
6063
// Emit the matrix type with 'rowCountWGSL' WGSL-rows and 'colCountWGSL' WGSL-columns
6164
void emitMatrixType(
6265
IRType* const elementType,

0 commit comments

Comments
 (0)