Skip to content

Commit

Permalink
C-like emitter: Pass switch condition type to case emitter
Browse files Browse the repository at this point in the history
In WGSL, the switch condition and case types must match.
https://www.w3.org/TR/WGSL/#switch-statement

The Slang WGSL emitter solves this by doing an explicit conversion.
For this reason, pass the switch condition type to the function that emits the cases.

Note that this type mismatch should eventually be fixed in the front-end,
as discussed in issue shader-slang#4921.
However, that is not yet done, so for now this helps to address issue shader-slang#4807.
  • Loading branch information
aleino-nv committed Sep 4, 2024
1 parent 35c8c55 commit 57b73c4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions source/slang/slang-emit-c-like.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3200,7 +3200,7 @@ void CLikeSourceEmitter::emitLayoutSemantics(IRInst* inst, char const* uniformSe
emitLayoutSemanticsImpl(inst, uniformSemanticSpelling, EmitLayoutSemanticOption::kPostType);
}

void CLikeSourceEmitter::emitSwitchCaseSelectorsImpl(SwitchRegion::Case const*const currentCase, bool const isDefault)
void CLikeSourceEmitter::emitSwitchCaseSelectorsImpl(IRBasicType *const /* switchCondition */, SwitchRegion::Case const*const currentCase, bool const isDefault)
{
for(auto caseVal : currentCase->values)
{
Expand Down Expand Up @@ -3370,7 +3370,8 @@ void CLikeSourceEmitter::emitRegion(Region* inRegion)
for(auto currentCase : switchRegion->cases)
{
bool const isDefault {currentCase.Ptr() == defaultCase};
emitSwitchCaseSelectors(currentCase.Ptr(), isDefault);
IRBasicType *const switchConditionType {as<IRBasicType>(switchRegion->getCondition()->getDataType())};
emitSwitchCaseSelectors(switchConditionType, currentCase.Ptr(), isDefault);
m_writer->indent();
m_writer->emit("{\n");
m_writer->indent();
Expand Down
4 changes: 2 additions & 2 deletions source/slang/slang-emit-c-like.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class CLikeSourceEmitter: public SourceEmitterBase
void emitFuncHeader(IRFunc* func) { emitFuncHeaderImpl(func); }
void emitSimpleFunc(IRFunc* func) { emitSimpleFuncImpl(func); }

void emitSwitchCaseSelectors(SwitchRegion::Case const*const currentCase, bool const isDefault) {emitSwitchCaseSelectorsImpl(currentCase, isDefault);}
void emitSwitchCaseSelectors(IRBasicType *const switchConditionType, SwitchRegion::Case const*const currentCase, bool const isDefault) {emitSwitchCaseSelectorsImpl(switchConditionType, currentCase, isDefault);}

void emitParamType(IRType* type, String const& name) { emitParamTypeImpl(type, name); }

Expand Down Expand Up @@ -526,7 +526,7 @@ class CLikeSourceEmitter: public SourceEmitterBase
virtual void emitLoopControlDecorationImpl(IRLoopControlDecoration* decl) { SLANG_UNUSED(decl); }
virtual void emitIfDecorationsImpl(IRIfElse* ifInst) { SLANG_UNUSED(ifInst); }
virtual void emitSwitchDecorationsImpl(IRSwitch* switchInst) { SLANG_UNUSED(switchInst); }
virtual void emitSwitchCaseSelectorsImpl(SwitchRegion::Case const*const currentCase, bool const isDefault);
virtual void emitSwitchCaseSelectorsImpl(IRBasicType *const switchConditionType, SwitchRegion::Case const*const currentCase, bool const isDefault);

virtual void emitFuncDecorationImpl(IRDecoration* decoration) { SLANG_UNUSED(decoration); }
virtual void emitLivenessImpl(IRInst* inst);
Expand Down

0 comments on commit 57b73c4

Please sign in to comment.