Rewriter.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Numerics;
  7. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  8. using static Ryujinx.Graphics.Shader.Translation.GlobalMemory;
  9. namespace Ryujinx.Graphics.Shader.Translation
  10. {
  11. static class Rewriter
  12. {
  13. public static void RunPass(BasicBlock[] blocks, ShaderConfig config)
  14. {
  15. bool isVertexShader = config.Stage == ShaderStage.Vertex;
  16. bool hasConstantBufferDrawParameters = config.GpuAccessor.QueryHasConstantBufferDrawParameters();
  17. bool supportsSnormBufferTextureFormat = config.GpuAccessor.QueryHostSupportsSnormBufferTextureFormat();
  18. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  19. {
  20. BasicBlock block = blocks[blkIndex];
  21. for (LinkedListNode<INode> node = block.Operations.First; node != null;)
  22. {
  23. if (node.Value is not Operation operation)
  24. {
  25. node = node.Next;
  26. continue;
  27. }
  28. if (isVertexShader)
  29. {
  30. if (hasConstantBufferDrawParameters)
  31. {
  32. if (ReplaceConstantBufferWithDrawParameters(node, operation))
  33. {
  34. config.SetUsedFeature(FeatureFlags.DrawParameters);
  35. }
  36. }
  37. else if (HasConstantBufferDrawParameters(operation))
  38. {
  39. config.SetUsedFeature(FeatureFlags.DrawParameters);
  40. }
  41. }
  42. LinkedListNode<INode> nextNode = node.Next;
  43. if (operation is TextureOperation texOp)
  44. {
  45. if (texOp.Inst == Instruction.TextureSample)
  46. {
  47. node = RewriteTextureSample(node, config);
  48. if (texOp.Type == SamplerType.TextureBuffer && !supportsSnormBufferTextureFormat)
  49. {
  50. node = InsertSnormNormalization(node, config);
  51. }
  52. }
  53. nextNode = node.Next;
  54. }
  55. else if (UsesGlobalMemory(operation.Inst, operation.StorageKind))
  56. {
  57. nextNode = RewriteGlobalAccess(node, config)?.Next ?? nextNode;
  58. }
  59. node = nextNode;
  60. }
  61. }
  62. }
  63. private static LinkedListNode<INode> RewriteGlobalAccess(LinkedListNode<INode> node, ShaderConfig config)
  64. {
  65. Operation operation = (Operation)node.Value;
  66. bool isAtomic = operation.Inst.IsAtomic();
  67. bool isStg16Or8 = operation.Inst == Instruction.StoreGlobal16 || operation.Inst == Instruction.StoreGlobal8;
  68. bool isWrite = isAtomic || operation.Inst == Instruction.StoreGlobal || isStg16Or8;
  69. Operation storageOp = null;
  70. Operand PrependOperation(Instruction inst, params Operand[] sources)
  71. {
  72. Operand local = Local();
  73. node.List.AddBefore(node, new Operation(inst, local, sources));
  74. return local;
  75. }
  76. Operand PrependExistingOperation(Operation operation)
  77. {
  78. Operand local = Local();
  79. operation.Dest = local;
  80. node.List.AddBefore(node, operation);
  81. return local;
  82. }
  83. Operand addrLow = operation.GetSource(0);
  84. Operand addrHigh = operation.GetSource(1);
  85. Operand sbBaseAddrLow = Const(0);
  86. Operand sbSlot = Const(0);
  87. Operand alignMask = Const(-config.GpuAccessor.QueryHostStorageBufferOffsetAlignment());
  88. Operand BindingRangeCheck(int cbOffset, out Operand baseAddrLow)
  89. {
  90. baseAddrLow = Cbuf(0, cbOffset);
  91. Operand baseAddrHigh = Cbuf(0, cbOffset + 1);
  92. Operand size = Cbuf(0, cbOffset + 2);
  93. Operand offset = PrependOperation(Instruction.Subtract, addrLow, baseAddrLow);
  94. Operand borrow = PrependOperation(Instruction.CompareLessU32, addrLow, baseAddrLow);
  95. Operand inRangeLow = PrependOperation(Instruction.CompareLessU32, offset, size);
  96. Operand addrHighBorrowed = PrependOperation(Instruction.Add, addrHigh, borrow);
  97. Operand inRangeHigh = PrependOperation(Instruction.CompareEqual, addrHighBorrowed, baseAddrHigh);
  98. return PrependOperation(Instruction.BitwiseAnd, inRangeLow, inRangeHigh);
  99. }
  100. int sbUseMask = config.AccessibleStorageBuffersMask;
  101. while (sbUseMask != 0)
  102. {
  103. int slot = BitOperations.TrailingZeroCount(sbUseMask);
  104. sbUseMask &= ~(1 << slot);
  105. config.SetUsedStorageBuffer(slot, isWrite);
  106. int cbOffset = GetStorageCbOffset(config.Stage, slot);
  107. Operand inRange = BindingRangeCheck(cbOffset, out Operand baseAddrLow);
  108. sbBaseAddrLow = PrependOperation(Instruction.ConditionalSelect, inRange, baseAddrLow, sbBaseAddrLow);
  109. sbSlot = PrependOperation(Instruction.ConditionalSelect, inRange, Const(slot), sbSlot);
  110. }
  111. if (config.AccessibleStorageBuffersMask != 0)
  112. {
  113. Operand baseAddrTrunc = PrependOperation(Instruction.BitwiseAnd, sbBaseAddrLow, alignMask);
  114. Operand byteOffset = PrependOperation(Instruction.Subtract, addrLow, baseAddrTrunc);
  115. Operand[] sources = new Operand[operation.SourcesCount];
  116. sources[0] = sbSlot;
  117. if (isStg16Or8)
  118. {
  119. sources[1] = byteOffset;
  120. }
  121. else
  122. {
  123. sources[1] = PrependOperation(Instruction.ShiftRightU32, byteOffset, Const(2));
  124. }
  125. for (int index = 2; index < operation.SourcesCount; index++)
  126. {
  127. sources[index] = operation.GetSource(index);
  128. }
  129. if (isAtomic)
  130. {
  131. storageOp = new Operation(operation.Inst, StorageKind.StorageBuffer, operation.Dest, sources);
  132. }
  133. else if (operation.Inst == Instruction.LoadGlobal)
  134. {
  135. storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources);
  136. }
  137. else
  138. {
  139. Instruction storeInst = operation.Inst switch
  140. {
  141. Instruction.StoreGlobal16 => Instruction.StoreStorage16,
  142. Instruction.StoreGlobal8 => Instruction.StoreStorage8,
  143. _ => Instruction.StoreStorage
  144. };
  145. storageOp = new Operation(storeInst, null, sources);
  146. }
  147. }
  148. else if (operation.Dest != null)
  149. {
  150. storageOp = new Operation(Instruction.Copy, operation.Dest, Const(0));
  151. }
  152. if (operation.Inst == Instruction.LoadGlobal)
  153. {
  154. int cbeUseMask = config.AccessibleConstantBuffersMask;
  155. while (cbeUseMask != 0)
  156. {
  157. int slot = BitOperations.TrailingZeroCount(cbeUseMask);
  158. int cbSlot = UbeFirstCbuf + slot;
  159. cbeUseMask &= ~(1 << slot);
  160. config.SetUsedConstantBuffer(cbSlot);
  161. Operand previousResult = PrependExistingOperation(storageOp);
  162. int cbOffset = GetConstantUbeOffset(slot);
  163. Operand inRange = BindingRangeCheck(cbOffset, out Operand baseAddrLow);
  164. Operand baseAddrTruncConst = PrependOperation(Instruction.BitwiseAnd, baseAddrLow, alignMask);
  165. Operand byteOffsetConst = PrependOperation(Instruction.Subtract, addrLow, baseAddrTruncConst);
  166. Operand cbIndex = PrependOperation(Instruction.ShiftRightU32, byteOffsetConst, Const(2));
  167. Operand[] sourcesCb = new Operand[operation.SourcesCount];
  168. sourcesCb[0] = Const(cbSlot);
  169. sourcesCb[1] = cbIndex;
  170. for (int index = 2; index < operation.SourcesCount; index++)
  171. {
  172. sourcesCb[index] = operation.GetSource(index);
  173. }
  174. Operand ldcResult = PrependOperation(Instruction.LoadConstant, sourcesCb);
  175. storageOp = new Operation(Instruction.ConditionalSelect, operation.Dest, inRange, ldcResult, previousResult);
  176. }
  177. }
  178. for (int index = 0; index < operation.SourcesCount; index++)
  179. {
  180. operation.SetSource(index, null);
  181. }
  182. LinkedListNode<INode> oldNode = node;
  183. LinkedList<INode> oldNodeList = oldNode.List;
  184. if (storageOp != null)
  185. {
  186. node = node.List.AddBefore(node, storageOp);
  187. }
  188. else
  189. {
  190. node = null;
  191. }
  192. oldNodeList.Remove(oldNode);
  193. return node;
  194. }
  195. private static LinkedListNode<INode> RewriteTextureSample(LinkedListNode<INode> node, ShaderConfig config)
  196. {
  197. TextureOperation texOp = (TextureOperation)node.Value;
  198. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  199. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  200. bool hasInvalidOffset = (hasOffset || hasOffsets) && !config.GpuAccessor.QueryHostSupportsNonConstantTextureOffset();
  201. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  202. bool isCoordNormalized = isBindless || config.GpuAccessor.QueryTextureCoordNormalized(texOp.Handle, texOp.CbufSlot);
  203. if (!hasInvalidOffset && isCoordNormalized)
  204. {
  205. return node;
  206. }
  207. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  208. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  209. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  210. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  211. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  212. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  213. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  214. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  215. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  216. int coordsCount = texOp.Type.GetDimensions();
  217. int offsetsCount;
  218. if (hasOffsets)
  219. {
  220. offsetsCount = coordsCount * 4;
  221. }
  222. else if (hasOffset)
  223. {
  224. offsetsCount = coordsCount;
  225. }
  226. else
  227. {
  228. offsetsCount = 0;
  229. }
  230. Operand[] offsets = new Operand[offsetsCount];
  231. Operand[] sources = new Operand[texOp.SourcesCount - offsetsCount];
  232. int copyCount = 0;
  233. if (isBindless || isIndexed)
  234. {
  235. copyCount++;
  236. }
  237. Operand[] lodSources = new Operand[copyCount + coordsCount];
  238. for (int index = 0; index < lodSources.Length; index++)
  239. {
  240. lodSources[index] = texOp.GetSource(index);
  241. }
  242. copyCount += coordsCount;
  243. if (isArray)
  244. {
  245. copyCount++;
  246. }
  247. if (isShadow)
  248. {
  249. copyCount++;
  250. }
  251. if (hasDerivatives)
  252. {
  253. copyCount += coordsCount * 2;
  254. }
  255. if (isMultisample)
  256. {
  257. copyCount++;
  258. }
  259. else if (hasLodLevel)
  260. {
  261. copyCount++;
  262. }
  263. int srcIndex = 0;
  264. int dstIndex = 0;
  265. for (int index = 0; index < copyCount; index++)
  266. {
  267. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  268. }
  269. bool areAllOffsetsConstant = true;
  270. for (int index = 0; index < offsetsCount; index++)
  271. {
  272. Operand offset = texOp.GetSource(srcIndex++);
  273. areAllOffsetsConstant &= offset.Type == OperandType.Constant;
  274. offsets[index] = offset;
  275. }
  276. hasInvalidOffset &= !areAllOffsetsConstant;
  277. if (!hasInvalidOffset && isCoordNormalized)
  278. {
  279. return node;
  280. }
  281. if (hasLodBias)
  282. {
  283. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  284. }
  285. if (isGather && !isShadow)
  286. {
  287. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  288. }
  289. int coordsIndex = isBindless || isIndexed ? 1 : 0;
  290. int componentIndex = texOp.Index;
  291. Operand Float(Operand value)
  292. {
  293. Operand res = Local();
  294. node.List.AddBefore(node, new Operation(Instruction.ConvertS32ToFP32, res, value));
  295. return res;
  296. }
  297. // Emulate non-normalized coordinates by normalizing the coordinates on the shader.
  298. // Without normalization, the coordinates are expected to the in the [0, W or H] range,
  299. // and otherwise, it is expected to be in the [0, 1] range.
  300. // We normalize by dividing the coords by the texture size.
  301. if (!isCoordNormalized && !intCoords)
  302. {
  303. config.SetUsedFeature(FeatureFlags.IntegerSampling);
  304. int normCoordsCount = (texOp.Type & SamplerType.Mask) == SamplerType.TextureCube ? 2 : coordsCount;
  305. for (int index = 0; index < normCoordsCount; index++)
  306. {
  307. Operand coordSize = Local();
  308. Operand[] texSizeSources;
  309. if (isBindless || isIndexed)
  310. {
  311. texSizeSources = new Operand[] { sources[0], Const(0) };
  312. }
  313. else
  314. {
  315. texSizeSources = new Operand[] { Const(0) };
  316. }
  317. node.List.AddBefore(node, new TextureOperation(
  318. Instruction.TextureSize,
  319. texOp.Type,
  320. texOp.Format,
  321. texOp.Flags,
  322. texOp.CbufSlot,
  323. texOp.Handle,
  324. index,
  325. new[] { coordSize },
  326. texSizeSources));
  327. config.SetUsedTexture(Instruction.TextureSize, texOp.Type, texOp.Format, texOp.Flags, texOp.CbufSlot, texOp.Handle);
  328. Operand source = sources[coordsIndex + index];
  329. Operand coordNormalized = Local();
  330. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Divide, coordNormalized, source, Float(coordSize)));
  331. sources[coordsIndex + index] = coordNormalized;
  332. }
  333. }
  334. Operand[] dests = new Operand[texOp.DestsCount];
  335. for (int i = 0; i < texOp.DestsCount; i++)
  336. {
  337. dests[i] = texOp.GetDest(i);
  338. }
  339. Operand bindlessHandle = isBindless || isIndexed ? sources[0] : null;
  340. LinkedListNode<INode> oldNode = node;
  341. // Technically, non-constant texture offsets are not allowed (according to the spec),
  342. // however some GPUs does support that.
  343. // For GPUs where it is not supported, we can replace the instruction with the following:
  344. // For texture*Offset, we replace it by texture*, and add the offset to the P coords.
  345. // The offset can be calculated as offset / textureSize(lod), where lod = textureQueryLod(coords).
  346. // For texelFetchOffset, we replace it by texelFetch and add the offset to the P coords directly.
  347. // For textureGatherOffset, we split the operation into up to 4 operations, one for each component
  348. // that is accessed, where each textureGather operation has a different offset for each pixel.
  349. if (hasInvalidOffset && isGather && !isShadow)
  350. {
  351. config.SetUsedFeature(FeatureFlags.IntegerSampling);
  352. Operand[] newSources = new Operand[sources.Length];
  353. sources.CopyTo(newSources, 0);
  354. Operand[] texSizes = InsertTextureSize(node, texOp, lodSources, bindlessHandle, coordsCount);
  355. int destIndex = 0;
  356. for (int compIndex = 0; compIndex < 4; compIndex++)
  357. {
  358. if (((texOp.Index >> compIndex) & 1) == 0)
  359. {
  360. continue;
  361. }
  362. for (int index = 0; index < coordsCount; index++)
  363. {
  364. config.SetUsedTexture(Instruction.TextureSize, texOp.Type, texOp.Format, texOp.Flags, texOp.CbufSlot, texOp.Handle);
  365. Operand offset = Local();
  366. Operand intOffset = offsets[index + (hasOffsets ? compIndex * coordsCount : 0)];
  367. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Divide, offset, Float(intOffset), Float(texSizes[index])));
  368. Operand source = sources[coordsIndex + index];
  369. Operand coordPlusOffset = Local();
  370. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Add, coordPlusOffset, source, offset));
  371. newSources[coordsIndex + index] = coordPlusOffset;
  372. }
  373. TextureOperation newTexOp = new TextureOperation(
  374. Instruction.TextureSample,
  375. texOp.Type,
  376. texOp.Format,
  377. texOp.Flags & ~(TextureFlags.Offset | TextureFlags.Offsets),
  378. texOp.CbufSlot,
  379. texOp.Handle,
  380. 1,
  381. new[] { dests[destIndex++] },
  382. newSources);
  383. node = node.List.AddBefore(node, newTexOp);
  384. }
  385. }
  386. else
  387. {
  388. if (hasInvalidOffset)
  389. {
  390. if (intCoords)
  391. {
  392. for (int index = 0; index < coordsCount; index++)
  393. {
  394. Operand source = sources[coordsIndex + index];
  395. Operand coordPlusOffset = Local();
  396. node.List.AddBefore(node, new Operation(Instruction.Add, coordPlusOffset, source, offsets[index]));
  397. sources[coordsIndex + index] = coordPlusOffset;
  398. }
  399. }
  400. else
  401. {
  402. config.SetUsedFeature(FeatureFlags.IntegerSampling);
  403. Operand[] texSizes = InsertTextureSize(node, texOp, lodSources, bindlessHandle, coordsCount);
  404. for (int index = 0; index < coordsCount; index++)
  405. {
  406. config.SetUsedTexture(Instruction.TextureSize, texOp.Type, texOp.Format, texOp.Flags, texOp.CbufSlot, texOp.Handle);
  407. Operand offset = Local();
  408. Operand intOffset = offsets[index];
  409. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Divide, offset, Float(intOffset), Float(texSizes[index])));
  410. Operand source = sources[coordsIndex + index];
  411. Operand coordPlusOffset = Local();
  412. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Add, coordPlusOffset, source, offset));
  413. sources[coordsIndex + index] = coordPlusOffset;
  414. }
  415. }
  416. }
  417. TextureOperation newTexOp = new TextureOperation(
  418. Instruction.TextureSample,
  419. texOp.Type,
  420. texOp.Format,
  421. texOp.Flags & ~(TextureFlags.Offset | TextureFlags.Offsets),
  422. texOp.CbufSlot,
  423. texOp.Handle,
  424. componentIndex,
  425. dests,
  426. sources);
  427. node = node.List.AddBefore(node, newTexOp);
  428. }
  429. node.List.Remove(oldNode);
  430. for (int index = 0; index < texOp.SourcesCount; index++)
  431. {
  432. texOp.SetSource(index, null);
  433. }
  434. return node;
  435. }
  436. private static Operand[] InsertTextureSize(
  437. LinkedListNode<INode> node,
  438. TextureOperation texOp,
  439. Operand[] lodSources,
  440. Operand bindlessHandle,
  441. int coordsCount)
  442. {
  443. Operand Int(Operand value)
  444. {
  445. Operand res = Local();
  446. node.List.AddBefore(node, new Operation(Instruction.ConvertFP32ToS32, res, value));
  447. return res;
  448. }
  449. Operand[] texSizes = new Operand[coordsCount];
  450. Operand lod = Local();
  451. node.List.AddBefore(node, new TextureOperation(
  452. Instruction.Lod,
  453. texOp.Type,
  454. texOp.Format,
  455. texOp.Flags,
  456. texOp.CbufSlot,
  457. texOp.Handle,
  458. 0,
  459. new[] { lod },
  460. lodSources));
  461. for (int index = 0; index < coordsCount; index++)
  462. {
  463. texSizes[index] = Local();
  464. Operand[] texSizeSources;
  465. if (bindlessHandle != null)
  466. {
  467. texSizeSources = new Operand[] { bindlessHandle, Int(lod) };
  468. }
  469. else
  470. {
  471. texSizeSources = new Operand[] { Int(lod) };
  472. }
  473. node.List.AddBefore(node, new TextureOperation(
  474. Instruction.TextureSize,
  475. texOp.Type,
  476. texOp.Format,
  477. texOp.Flags,
  478. texOp.CbufSlot,
  479. texOp.Handle,
  480. index,
  481. new[] { texSizes[index] },
  482. texSizeSources));
  483. }
  484. return texSizes;
  485. }
  486. private static LinkedListNode<INode> InsertSnormNormalization(LinkedListNode<INode> node, ShaderConfig config)
  487. {
  488. TextureOperation texOp = (TextureOperation)node.Value;
  489. // We can't query the format of a bindless texture,
  490. // because the handle is unknown, it can have any format.
  491. if (texOp.Flags.HasFlag(TextureFlags.Bindless))
  492. {
  493. return node;
  494. }
  495. TextureFormat format = config.GpuAccessor.QueryTextureFormat(texOp.Handle, texOp.CbufSlot);
  496. int maxPositive = format switch
  497. {
  498. TextureFormat.R8Snorm => sbyte.MaxValue,
  499. TextureFormat.R8G8Snorm => sbyte.MaxValue,
  500. TextureFormat.R8G8B8A8Snorm => sbyte.MaxValue,
  501. TextureFormat.R16Snorm => short.MaxValue,
  502. TextureFormat.R16G16Snorm => short.MaxValue,
  503. TextureFormat.R16G16B16A16Snorm => short.MaxValue,
  504. _ => 0
  505. };
  506. // The value being 0 means that the format is not a SNORM format,
  507. // so there's nothing to do here.
  508. if (maxPositive == 0)
  509. {
  510. return node;
  511. }
  512. // Do normalization. We assume SINT formats are being used
  513. // as replacement for SNORM (which is not supported).
  514. for (int i = 0; i < texOp.DestsCount; i++)
  515. {
  516. Operand dest = texOp.GetDest(i);
  517. INode[] uses = dest.UseOps.ToArray();
  518. Operation convOp = new Operation(Instruction.ConvertS32ToFP32, Local(), dest);
  519. Operation normOp = new Operation(Instruction.FP32 | Instruction.Multiply, Local(), convOp.Dest, ConstF(1f / maxPositive));
  520. node = node.List.AddAfter(node, convOp);
  521. node = node.List.AddAfter(node, normOp);
  522. foreach (INode useOp in uses)
  523. {
  524. if (useOp is not Operation op)
  525. {
  526. continue;
  527. }
  528. // Replace all uses of the texture pixel value with the normalized value.
  529. for (int index = 0; index < op.SourcesCount; index++)
  530. {
  531. if (op.GetSource(index) == dest)
  532. {
  533. op.SetSource(index, normOp.Dest);
  534. }
  535. }
  536. }
  537. }
  538. return node;
  539. }
  540. private static bool ReplaceConstantBufferWithDrawParameters(LinkedListNode<INode> node, Operation operation)
  541. {
  542. Operand GenerateLoad(IoVariable ioVariable)
  543. {
  544. Operand value = Local();
  545. node.List.AddBefore(node, new Operation(Instruction.Load, StorageKind.Input, value, Const((int)ioVariable)));
  546. return value;
  547. }
  548. bool modified = false;
  549. for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
  550. {
  551. Operand src = operation.GetSource(srcIndex);
  552. if (src.Type == OperandType.ConstantBuffer && src.GetCbufSlot() == 0)
  553. {
  554. switch (src.GetCbufOffset())
  555. {
  556. case Constants.NvnBaseVertexByteOffset / 4:
  557. operation.SetSource(srcIndex, GenerateLoad(IoVariable.BaseVertex));
  558. modified = true;
  559. break;
  560. case Constants.NvnBaseInstanceByteOffset / 4:
  561. operation.SetSource(srcIndex, GenerateLoad(IoVariable.BaseInstance));
  562. modified = true;
  563. break;
  564. case Constants.NvnDrawIndexByteOffset / 4:
  565. operation.SetSource(srcIndex, GenerateLoad(IoVariable.DrawIndex));
  566. modified = true;
  567. break;
  568. }
  569. }
  570. }
  571. return modified;
  572. }
  573. private static bool HasConstantBufferDrawParameters(Operation operation)
  574. {
  575. for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
  576. {
  577. Operand src = operation.GetSource(srcIndex);
  578. if (src.Type == OperandType.ConstantBuffer && src.GetCbufSlot() == 0)
  579. {
  580. switch (src.GetCbufOffset())
  581. {
  582. case Constants.NvnBaseVertexByteOffset / 4:
  583. case Constants.NvnBaseInstanceByteOffset / 4:
  584. case Constants.NvnDrawIndexByteOffset / 4:
  585. return true;
  586. }
  587. }
  588. }
  589. return false;
  590. }
  591. }
  592. }