@@ -497,6 +497,34 @@ void WGSLSourceEmitter::emitLayoutQualifiersImpl(IRVarLayout* layout)
497
497
}
498
498
}
499
499
500
+ static bool isStaticConst (IRInst* inst)
501
+ {
502
+ if (inst->getParent ()->getOp () == kIROp_Module )
503
+ {
504
+ return true ;
505
+ }
506
+ switch (inst->getOp ())
507
+ {
508
+ case kIROp_MakeVector :
509
+ case kIROp_swizzle :
510
+ case kIROp_swizzleSet :
511
+ case kIROp_IntCast :
512
+ case kIROp_FloatCast :
513
+ case kIROp_CastFloatToInt :
514
+ case kIROp_CastIntToFloat :
515
+ case kIROp_BitCast :
516
+ {
517
+ for (UInt i = 0 ; i < inst->getOperandCount (); i++)
518
+ {
519
+ if (!isStaticConst (inst->getOperand (i)))
520
+ return false ;
521
+ }
522
+ return true ;
523
+ }
524
+ }
525
+ return false ;
526
+ }
527
+
500
528
void WGSLSourceEmitter::emitVarKeywordImpl (IRType* type, IRInst* varDecl)
501
529
{
502
530
switch (varDecl->getOp ())
@@ -505,14 +533,10 @@ void WGSLSourceEmitter::emitVarKeywordImpl(IRType* type, IRInst* varDecl)
505
533
case kIROp_GlobalVar :
506
534
case kIROp_Var : m_writer->emit (" var" ); break ;
507
535
default :
508
- if (as<IRModuleInst>(varDecl->getParent ()))
509
- {
536
+ if (isStaticConst (varDecl))
510
537
m_writer->emit (" const" );
511
- }
512
538
else
513
- {
514
539
m_writer->emit (" var" );
515
- }
516
540
break ;
517
541
}
518
542
@@ -977,6 +1001,33 @@ void WGSLSourceEmitter::emitCallArg(IRInst* inst)
977
1001
}
978
1002
}
979
1003
1004
+ bool WGSLSourceEmitter::shouldFoldInstIntoUseSites (IRInst* inst)
1005
+ {
1006
+ bool result = CLikeSourceEmitter::shouldFoldInstIntoUseSites (inst);
1007
+ if (result)
1008
+ {
1009
+ // If inst is a matrix, and is used in a component-wise multiply,
1010
+ // we need to not fold it.
1011
+ if (as<IRMatrixType>(inst->getDataType ()))
1012
+ {
1013
+ for (auto use = inst->firstUse ; use; use = use->nextUse )
1014
+ {
1015
+ auto user = use->getUser ();
1016
+ if (user->getOp () == kIROp_Mul )
1017
+ {
1018
+ if (as<IRMatrixType>(user->getOperand (0 )->getDataType ()) &&
1019
+ as<IRMatrixType>(user->getOperand (1 )->getDataType ()))
1020
+ {
1021
+ return false ;
1022
+ }
1023
+ }
1024
+ }
1025
+ }
1026
+ }
1027
+ return result;
1028
+ }
1029
+
1030
+
980
1031
bool WGSLSourceEmitter::tryEmitInstExprImpl (IRInst* inst, const EmitOpInfo& inOuterPrec)
981
1032
{
982
1033
EmitOpInfo outerPrec = inOuterPrec;
@@ -1126,6 +1177,71 @@ bool WGSLSourceEmitter::tryEmitInstExprImpl(IRInst* inst, const EmitOpInfo& inOu
1126
1177
return true ;
1127
1178
}
1128
1179
break ;
1180
+
1181
+ case kIROp_GetStringHash :
1182
+ {
1183
+ auto getStringHashInst = as<IRGetStringHash>(inst);
1184
+ auto stringLit = getStringHashInst->getStringLit ();
1185
+
1186
+ if (stringLit)
1187
+ {
1188
+ auto slice = stringLit->getStringSlice ();
1189
+ emitType (inst->getDataType ());
1190
+ m_writer->emit (" (" );
1191
+ m_writer->emit ((int )getStableHashCode32 (slice.begin (), slice.getLength ()).hash );
1192
+ m_writer->emit (" )" );
1193
+ }
1194
+ else
1195
+ {
1196
+ // Couldn't handle
1197
+ diagnoseUnhandledInst (inst);
1198
+ }
1199
+ return true ;
1200
+ }
1201
+
1202
+ case kIROp_Mul :
1203
+ {
1204
+ if (!as<IRMatrixType>(inst->getOperand (0 )->getDataType ()) ||
1205
+ !as<IRMatrixType>(inst->getOperand (1 )->getDataType ()))
1206
+ {
1207
+ return false ;
1208
+ }
1209
+ // Mul(m1, m2) should be translated to component-wise multiplication in WGSL.
1210
+ auto matrixType = as<IRMatrixType>(inst->getDataType ());
1211
+ auto rowCount = getIntVal (matrixType->getRowCount ());
1212
+ emitType (inst->getDataType ());
1213
+ m_writer->emit (" (" );
1214
+ for (IRIntegerValue i = 0 ; i < rowCount; i++)
1215
+ {
1216
+ if (i != 0 )
1217
+ {
1218
+ m_writer->emit (" , " );
1219
+ }
1220
+ emitOperand (inst->getOperand (0 ), getInfo (EmitOp::Postfix));
1221
+ m_writer->emit (" [" );
1222
+ m_writer->emit (i);
1223
+ m_writer->emit (" ] * " );
1224
+ emitOperand (inst->getOperand (1 ), getInfo (EmitOp::Postfix));
1225
+ m_writer->emit (" [" );
1226
+ m_writer->emit (i);
1227
+ m_writer->emit (" ]" );
1228
+ }
1229
+ m_writer->emit (" )" );
1230
+
1231
+ return true ;
1232
+ }
1233
+
1234
+ case kIROp_Select :
1235
+ {
1236
+ m_writer->emit (" select(" );
1237
+ emitOperand (inst->getOperand (2 ), getInfo (EmitOp::General));
1238
+ m_writer->emit (" , " );
1239
+ emitOperand (inst->getOperand (1 ), getInfo (EmitOp::General));
1240
+ m_writer->emit (" , " );
1241
+ emitOperand (inst->getOperand (0 ), getInfo (EmitOp::General));
1242
+ m_writer->emit (" )" );
1243
+ return true ;
1244
+ }
1129
1245
}
1130
1246
1131
1247
return false ;
0 commit comments