InstGenMemory.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using System.Text;
  6. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenHelper;
  7. using static Ryujinx.Graphics.Shader.StructuredIr.InstructionInfo;
  8. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
  9. {
  10. static class InstGenMemory
  11. {
  12. public static string ImageLoadOrStore(CodeGenContext context, AstOperation operation)
  13. {
  14. AstTextureOperation texOp = (AstTextureOperation)operation;
  15. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  16. // TODO: Bindless texture support. For now we just return 0/do nothing.
  17. if (isBindless)
  18. {
  19. switch (texOp.Inst)
  20. {
  21. case Instruction.ImageStore:
  22. return "// imageStore(bindless)";
  23. case Instruction.ImageLoad:
  24. AggregateType componentType = texOp.Format.GetComponentType();
  25. NumberFormatter.TryFormat(0, componentType, out string imageConst);
  26. AggregateType outputType = texOp.GetVectorType(componentType);
  27. if ((outputType & AggregateType.ElementCountMask) != 0)
  28. {
  29. return $"{Declarations.GetVarTypeName(context, outputType, precise: false)}({imageConst})";
  30. }
  31. return imageConst;
  32. default:
  33. return NumberFormatter.FormatInt(0);
  34. }
  35. }
  36. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  37. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  38. var texCallBuilder = new StringBuilder();
  39. if (texOp.Inst == Instruction.ImageAtomic)
  40. {
  41. texCallBuilder.Append((texOp.Flags & TextureFlags.AtomicMask) switch {
  42. TextureFlags.Add => "imageAtomicAdd",
  43. TextureFlags.Minimum => "imageAtomicMin",
  44. TextureFlags.Maximum => "imageAtomicMax",
  45. TextureFlags.Increment => "imageAtomicAdd", // TODO: Clamp value.
  46. TextureFlags.Decrement => "imageAtomicAdd", // TODO: Clamp value.
  47. TextureFlags.BitwiseAnd => "imageAtomicAnd",
  48. TextureFlags.BitwiseOr => "imageAtomicOr",
  49. TextureFlags.BitwiseXor => "imageAtomicXor",
  50. TextureFlags.Swap => "imageAtomicExchange",
  51. TextureFlags.CAS => "imageAtomicCompSwap",
  52. _ => "imageAtomicAdd",
  53. });
  54. }
  55. else
  56. {
  57. texCallBuilder.Append(texOp.Inst == Instruction.ImageLoad ? "imageLoad" : "imageStore");
  58. }
  59. int srcIndex = isBindless ? 1 : 0;
  60. string Src(AggregateType type)
  61. {
  62. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  63. }
  64. string indexExpr = null;
  65. if (isIndexed)
  66. {
  67. indexExpr = Src(AggregateType.S32);
  68. }
  69. string imageName = OperandManager.GetImageName(context.Config.Stage, texOp, indexExpr);
  70. texCallBuilder.Append('(');
  71. texCallBuilder.Append(imageName);
  72. int coordsCount = texOp.Type.GetDimensions();
  73. int pCount = coordsCount + (isArray ? 1 : 0);
  74. void Append(string str)
  75. {
  76. texCallBuilder.Append(", ");
  77. texCallBuilder.Append(str);
  78. }
  79. if (pCount > 1)
  80. {
  81. string[] elems = new string[pCount];
  82. for (int index = 0; index < pCount; index++)
  83. {
  84. elems[index] = Src(AggregateType.S32);
  85. }
  86. Append($"ivec{pCount}({string.Join(", ", elems)})");
  87. }
  88. else
  89. {
  90. Append(Src(AggregateType.S32));
  91. }
  92. if (texOp.Inst == Instruction.ImageStore)
  93. {
  94. AggregateType type = texOp.Format.GetComponentType();
  95. string[] cElems = new string[4];
  96. for (int index = 0; index < 4; index++)
  97. {
  98. if (srcIndex < texOp.SourcesCount)
  99. {
  100. cElems[index] = Src(type);
  101. }
  102. else
  103. {
  104. cElems[index] = type switch
  105. {
  106. AggregateType.S32 => NumberFormatter.FormatInt(0),
  107. AggregateType.U32 => NumberFormatter.FormatUint(0),
  108. _ => NumberFormatter.FormatFloat(0)
  109. };
  110. }
  111. }
  112. string prefix = type switch
  113. {
  114. AggregateType.S32 => "i",
  115. AggregateType.U32 => "u",
  116. _ => string.Empty
  117. };
  118. Append($"{prefix}vec4({string.Join(", ", cElems)})");
  119. }
  120. if (texOp.Inst == Instruction.ImageAtomic)
  121. {
  122. AggregateType type = texOp.Format.GetComponentType();
  123. if ((texOp.Flags & TextureFlags.AtomicMask) == TextureFlags.CAS)
  124. {
  125. Append(Src(type)); // Compare value.
  126. }
  127. string value = (texOp.Flags & TextureFlags.AtomicMask) switch
  128. {
  129. TextureFlags.Increment => NumberFormatter.FormatInt(1, type), // TODO: Clamp value
  130. TextureFlags.Decrement => NumberFormatter.FormatInt(-1, type), // TODO: Clamp value
  131. _ => Src(type)
  132. };
  133. Append(value);
  134. texCallBuilder.Append(')');
  135. if (type != AggregateType.S32)
  136. {
  137. texCallBuilder
  138. .Insert(0, "int(")
  139. .Append(')');
  140. }
  141. }
  142. else
  143. {
  144. texCallBuilder.Append(')');
  145. if (texOp.Inst == Instruction.ImageLoad)
  146. {
  147. texCallBuilder.Append(GetMaskMultiDest(texOp.Index));
  148. }
  149. }
  150. return texCallBuilder.ToString();
  151. }
  152. public static string Load(CodeGenContext context, AstOperation operation)
  153. {
  154. return GenerateLoadOrStore(context, operation, isStore: false);
  155. }
  156. public static string Lod(CodeGenContext context, AstOperation operation)
  157. {
  158. AstTextureOperation texOp = (AstTextureOperation)operation;
  159. int coordsCount = texOp.Type.GetDimensions();
  160. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  161. // TODO: Bindless texture support. For now we just return 0.
  162. if (isBindless)
  163. {
  164. return NumberFormatter.FormatFloat(0);
  165. }
  166. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  167. string indexExpr = null;
  168. if (isIndexed)
  169. {
  170. indexExpr = GetSoureExpr(context, texOp.GetSource(0), AggregateType.S32);
  171. }
  172. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  173. int coordsIndex = isBindless || isIndexed ? 1 : 0;
  174. string coordsExpr;
  175. if (coordsCount > 1)
  176. {
  177. string[] elems = new string[coordsCount];
  178. for (int index = 0; index < coordsCount; index++)
  179. {
  180. elems[index] = GetSoureExpr(context, texOp.GetSource(coordsIndex + index), AggregateType.FP32);
  181. }
  182. coordsExpr = "vec" + coordsCount + "(" + string.Join(", ", elems) + ")";
  183. }
  184. else
  185. {
  186. coordsExpr = GetSoureExpr(context, texOp.GetSource(coordsIndex), AggregateType.FP32);
  187. }
  188. return $"textureQueryLod({samplerName}, {coordsExpr}){GetMask(texOp.Index)}";
  189. }
  190. public static string Store(CodeGenContext context, AstOperation operation)
  191. {
  192. return GenerateLoadOrStore(context, operation, isStore: true);
  193. }
  194. public static string TextureSample(CodeGenContext context, AstOperation operation)
  195. {
  196. AstTextureOperation texOp = (AstTextureOperation)operation;
  197. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  198. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  199. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  200. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  201. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  202. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  203. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  204. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  205. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  206. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  207. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  208. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  209. bool colorIsVector = isGather || !isShadow;
  210. SamplerType type = texOp.Type & SamplerType.Mask;
  211. bool is2D = type == SamplerType.Texture2D;
  212. bool isCube = type == SamplerType.TextureCube;
  213. // 2D Array and Cube shadow samplers with LOD level or bias requires an extension.
  214. // If the extension is not supported, just remove the LOD parameter.
  215. if (isArray && isShadow && (is2D || isCube) && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod())
  216. {
  217. hasLodBias = false;
  218. hasLodLevel = false;
  219. }
  220. // Cube shadow samplers with LOD level requires an extension.
  221. // If the extension is not supported, just remove the LOD level parameter.
  222. if (isShadow && isCube && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod())
  223. {
  224. hasLodLevel = false;
  225. }
  226. // TODO: Bindless texture support. For now we just return 0.
  227. if (isBindless)
  228. {
  229. string scalarValue = NumberFormatter.FormatFloat(0);
  230. if (colorIsVector)
  231. {
  232. AggregateType outputType = texOp.GetVectorType(AggregateType.FP32);
  233. if ((outputType & AggregateType.ElementCountMask) != 0)
  234. {
  235. return $"{Declarations.GetVarTypeName(context, outputType, precise: false)}({scalarValue})";
  236. }
  237. }
  238. return scalarValue;
  239. }
  240. string texCall = intCoords ? "texelFetch" : "texture";
  241. if (isGather)
  242. {
  243. texCall += "Gather";
  244. }
  245. else if (hasDerivatives)
  246. {
  247. texCall += "Grad";
  248. }
  249. else if (hasLodLevel && !intCoords)
  250. {
  251. texCall += "Lod";
  252. }
  253. if (hasOffset)
  254. {
  255. texCall += "Offset";
  256. }
  257. else if (hasOffsets)
  258. {
  259. texCall += "Offsets";
  260. }
  261. int srcIndex = isBindless ? 1 : 0;
  262. string Src(AggregateType type)
  263. {
  264. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  265. }
  266. string indexExpr = null;
  267. if (isIndexed)
  268. {
  269. indexExpr = Src(AggregateType.S32);
  270. }
  271. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  272. texCall += "(" + samplerName;
  273. int coordsCount = texOp.Type.GetDimensions();
  274. int pCount = coordsCount;
  275. int arrayIndexElem = -1;
  276. if (isArray)
  277. {
  278. arrayIndexElem = pCount++;
  279. }
  280. // The sampler 1D shadow overload expects a
  281. // dummy value on the middle of the vector, who knows why...
  282. bool hasDummy1DShadowElem = texOp.Type == (SamplerType.Texture1D | SamplerType.Shadow);
  283. if (hasDummy1DShadowElem)
  284. {
  285. pCount++;
  286. }
  287. if (isShadow && !isGather)
  288. {
  289. pCount++;
  290. }
  291. // On textureGather*, the comparison value is
  292. // always specified as an extra argument.
  293. bool hasExtraCompareArg = isShadow && isGather;
  294. if (pCount == 5)
  295. {
  296. pCount = 4;
  297. hasExtraCompareArg = true;
  298. }
  299. void Append(string str)
  300. {
  301. texCall += ", " + str;
  302. }
  303. AggregateType coordType = intCoords ? AggregateType.S32 : AggregateType.FP32;
  304. string AssemblePVector(int count)
  305. {
  306. if (count > 1)
  307. {
  308. string[] elems = new string[count];
  309. for (int index = 0; index < count; index++)
  310. {
  311. if (arrayIndexElem == index)
  312. {
  313. elems[index] = Src(AggregateType.S32);
  314. if (!intCoords)
  315. {
  316. elems[index] = "float(" + elems[index] + ")";
  317. }
  318. }
  319. else if (index == 1 && hasDummy1DShadowElem)
  320. {
  321. elems[index] = NumberFormatter.FormatFloat(0);
  322. }
  323. else
  324. {
  325. elems[index] = Src(coordType);
  326. }
  327. }
  328. string prefix = intCoords ? "i" : string.Empty;
  329. return prefix + "vec" + count + "(" + string.Join(", ", elems) + ")";
  330. }
  331. else
  332. {
  333. return Src(coordType);
  334. }
  335. }
  336. Append(AssemblePVector(pCount));
  337. string AssembleDerivativesVector(int count)
  338. {
  339. if (count > 1)
  340. {
  341. string[] elems = new string[count];
  342. for (int index = 0; index < count; index++)
  343. {
  344. elems[index] = Src(AggregateType.FP32);
  345. }
  346. return "vec" + count + "(" + string.Join(", ", elems) + ")";
  347. }
  348. else
  349. {
  350. return Src(AggregateType.FP32);
  351. }
  352. }
  353. if (hasExtraCompareArg)
  354. {
  355. Append(Src(AggregateType.FP32));
  356. }
  357. if (hasDerivatives)
  358. {
  359. Append(AssembleDerivativesVector(coordsCount)); // dPdx
  360. Append(AssembleDerivativesVector(coordsCount)); // dPdy
  361. }
  362. if (isMultisample)
  363. {
  364. Append(Src(AggregateType.S32));
  365. }
  366. else if (hasLodLevel)
  367. {
  368. Append(Src(coordType));
  369. }
  370. string AssembleOffsetVector(int count)
  371. {
  372. if (count > 1)
  373. {
  374. string[] elems = new string[count];
  375. for (int index = 0; index < count; index++)
  376. {
  377. elems[index] = Src(AggregateType.S32);
  378. }
  379. return "ivec" + count + "(" + string.Join(", ", elems) + ")";
  380. }
  381. else
  382. {
  383. return Src(AggregateType.S32);
  384. }
  385. }
  386. if (hasOffset)
  387. {
  388. Append(AssembleOffsetVector(coordsCount));
  389. }
  390. else if (hasOffsets)
  391. {
  392. texCall += $", ivec{coordsCount}[4](";
  393. texCall += AssembleOffsetVector(coordsCount) + ", ";
  394. texCall += AssembleOffsetVector(coordsCount) + ", ";
  395. texCall += AssembleOffsetVector(coordsCount) + ", ";
  396. texCall += AssembleOffsetVector(coordsCount) + ")";
  397. }
  398. if (hasLodBias)
  399. {
  400. Append(Src(AggregateType.FP32));
  401. }
  402. // textureGather* optional extra component index,
  403. // not needed for shadow samplers.
  404. if (isGather && !isShadow)
  405. {
  406. Append(Src(AggregateType.S32));
  407. }
  408. texCall += ")" + (colorIsVector ? GetMaskMultiDest(texOp.Index) : "");
  409. return texCall;
  410. }
  411. public static string TextureSize(CodeGenContext context, AstOperation operation)
  412. {
  413. AstTextureOperation texOp = (AstTextureOperation)operation;
  414. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  415. // TODO: Bindless texture support. For now we just return 0.
  416. if (isBindless)
  417. {
  418. return NumberFormatter.FormatInt(0);
  419. }
  420. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  421. string indexExpr = null;
  422. if (isIndexed)
  423. {
  424. indexExpr = GetSoureExpr(context, texOp.GetSource(0), AggregateType.S32);
  425. }
  426. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  427. if (texOp.Index == 3)
  428. {
  429. return $"textureQueryLevels({samplerName})";
  430. }
  431. else
  432. {
  433. TextureDescriptor descriptor = context.Config.FindTextureDescriptor(texOp);
  434. bool hasLod = !descriptor.Type.HasFlag(SamplerType.Multisample) && descriptor.Type != SamplerType.TextureBuffer;
  435. string texCall;
  436. if (hasLod)
  437. {
  438. int lodSrcIndex = isBindless || isIndexed ? 1 : 0;
  439. IAstNode lod = operation.GetSource(lodSrcIndex);
  440. string lodExpr = GetSoureExpr(context, lod, GetSrcVarType(operation.Inst, lodSrcIndex));
  441. texCall = $"textureSize({samplerName}, {lodExpr}){GetMask(texOp.Index)}";
  442. }
  443. else
  444. {
  445. texCall = $"textureSize({samplerName}){GetMask(texOp.Index)}";
  446. }
  447. return texCall;
  448. }
  449. }
  450. public static string GenerateLoadOrStore(CodeGenContext context, AstOperation operation, bool isStore)
  451. {
  452. StorageKind storageKind = operation.StorageKind;
  453. string varName;
  454. AggregateType varType;
  455. int srcIndex = 0;
  456. bool isStoreOrAtomic = operation.Inst == Instruction.Store || operation.Inst.IsAtomic();
  457. int inputsCount = isStoreOrAtomic ? operation.SourcesCount - 1 : operation.SourcesCount;
  458. if (operation.Inst == Instruction.AtomicCompareAndSwap)
  459. {
  460. inputsCount--;
  461. }
  462. switch (storageKind)
  463. {
  464. case StorageKind.ConstantBuffer:
  465. case StorageKind.StorageBuffer:
  466. if (!(operation.GetSource(srcIndex++) is AstOperand bindingIndex) || bindingIndex.Type != OperandType.Constant)
  467. {
  468. throw new InvalidOperationException($"First input of {operation.Inst} with {storageKind} storage must be a constant operand.");
  469. }
  470. int binding = bindingIndex.Value;
  471. BufferDefinition buffer = storageKind == StorageKind.ConstantBuffer
  472. ? context.Config.Properties.ConstantBuffers[binding]
  473. : context.Config.Properties.StorageBuffers[binding];
  474. if (!(operation.GetSource(srcIndex++) is AstOperand fieldIndex) || fieldIndex.Type != OperandType.Constant)
  475. {
  476. throw new InvalidOperationException($"Second input of {operation.Inst} with {storageKind} storage must be a constant operand.");
  477. }
  478. StructureField field = buffer.Type.Fields[fieldIndex.Value];
  479. varName = $"{buffer.Name}.{field.Name}";
  480. varType = field.Type;
  481. break;
  482. case StorageKind.LocalMemory:
  483. case StorageKind.SharedMemory:
  484. if (!(operation.GetSource(srcIndex++) is AstOperand bindingId) || bindingId.Type != OperandType.Constant)
  485. {
  486. throw new InvalidOperationException($"First input of {operation.Inst} with {storageKind} storage must be a constant operand.");
  487. }
  488. MemoryDefinition memory = storageKind == StorageKind.LocalMemory
  489. ? context.Config.Properties.LocalMemories[bindingId.Value]
  490. : context.Config.Properties.SharedMemories[bindingId.Value];
  491. varName = memory.Name;
  492. varType = memory.Type;
  493. break;
  494. case StorageKind.Input:
  495. case StorageKind.InputPerPatch:
  496. case StorageKind.Output:
  497. case StorageKind.OutputPerPatch:
  498. if (!(operation.GetSource(srcIndex++) is AstOperand varId) || varId.Type != OperandType.Constant)
  499. {
  500. throw new InvalidOperationException($"First input of {operation.Inst} with {storageKind} storage must be a constant operand.");
  501. }
  502. IoVariable ioVariable = (IoVariable)varId.Value;
  503. bool isOutput = storageKind.IsOutput();
  504. bool isPerPatch = storageKind.IsPerPatch();
  505. int location = -1;
  506. int component = 0;
  507. if (context.Config.HasPerLocationInputOrOutput(ioVariable, isOutput))
  508. {
  509. if (!(operation.GetSource(srcIndex++) is AstOperand vecIndex) || vecIndex.Type != OperandType.Constant)
  510. {
  511. throw new InvalidOperationException($"Second input of {operation.Inst} with {storageKind} storage must be a constant operand.");
  512. }
  513. location = vecIndex.Value;
  514. if (operation.SourcesCount > srcIndex &&
  515. operation.GetSource(srcIndex) is AstOperand elemIndex &&
  516. elemIndex.Type == OperandType.Constant &&
  517. context.Config.HasPerLocationInputOrOutputComponent(ioVariable, location, elemIndex.Value, isOutput))
  518. {
  519. component = elemIndex.Value;
  520. srcIndex++;
  521. }
  522. }
  523. (varName, varType) = IoMap.GetGlslVariable(context.Config, ioVariable, location, component, isOutput, isPerPatch);
  524. if (IoMap.IsPerVertexBuiltIn(context.Config.Stage, ioVariable, isOutput))
  525. {
  526. // Since those exist both as input and output on geometry and tessellation shaders,
  527. // we need the gl_in and gl_out prefixes to disambiguate.
  528. if (storageKind == StorageKind.Input)
  529. {
  530. string expr = GetSoureExpr(context, operation.GetSource(srcIndex++), AggregateType.S32);
  531. varName = $"gl_in[{expr}].{varName}";
  532. }
  533. else if (storageKind == StorageKind.Output)
  534. {
  535. string expr = GetSoureExpr(context, operation.GetSource(srcIndex++), AggregateType.S32);
  536. varName = $"gl_out[{expr}].{varName}";
  537. }
  538. }
  539. break;
  540. default:
  541. throw new InvalidOperationException($"Invalid storage kind {storageKind}.");
  542. }
  543. int firstSrcIndex = srcIndex;
  544. for (; srcIndex < inputsCount; srcIndex++)
  545. {
  546. IAstNode src = operation.GetSource(srcIndex);
  547. if ((varType & AggregateType.ElementCountMask) != 0 &&
  548. srcIndex == inputsCount - 1 &&
  549. src is AstOperand elementIndex &&
  550. elementIndex.Type == OperandType.Constant)
  551. {
  552. varName += "." + "xyzw"[elementIndex.Value & 3];
  553. }
  554. else if (srcIndex == firstSrcIndex && context.Config.Stage == ShaderStage.TessellationControl && storageKind == StorageKind.Output)
  555. {
  556. // GLSL requires that for tessellation control shader outputs,
  557. // that the index expression must be *exactly* "gl_InvocationID",
  558. // otherwise the compilation fails.
  559. // TODO: Get rid of this and use expression propagation to make sure we generate the correct code from IR.
  560. varName += "[gl_InvocationID]";
  561. }
  562. else
  563. {
  564. varName += $"[{GetSoureExpr(context, src, AggregateType.S32)}]";
  565. }
  566. }
  567. if (isStore)
  568. {
  569. varType &= AggregateType.ElementTypeMask;
  570. varName = $"{varName} = {GetSoureExpr(context, operation.GetSource(srcIndex), varType)}";
  571. }
  572. return varName;
  573. }
  574. private static string GetMask(int index)
  575. {
  576. return $".{"rgba".AsSpan(index, 1)}";
  577. }
  578. private static string GetMaskMultiDest(int mask)
  579. {
  580. string swizzle = ".";
  581. for (int i = 0; i < 4; i++)
  582. {
  583. if ((mask & (1 << i)) != 0)
  584. {
  585. swizzle += "xyzw"[i];
  586. }
  587. }
  588. return swizzle;
  589. }
  590. }
  591. }