InstGenMemory.cs 27 KB

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