Rewriter.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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(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))
  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. Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage;
  132. storageOp = new Operation(inst, operation.Dest, sources);
  133. }
  134. else if (operation.Inst == Instruction.LoadGlobal)
  135. {
  136. storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources);
  137. }
  138. else
  139. {
  140. Instruction storeInst = operation.Inst switch
  141. {
  142. Instruction.StoreGlobal16 => Instruction.StoreStorage16,
  143. Instruction.StoreGlobal8 => Instruction.StoreStorage8,
  144. _ => Instruction.StoreStorage
  145. };
  146. storageOp = new Operation(storeInst, null, sources);
  147. }
  148. }
  149. else if (operation.Dest != null)
  150. {
  151. storageOp = new Operation(Instruction.Copy, operation.Dest, Const(0));
  152. }
  153. if (operation.Inst == Instruction.LoadGlobal)
  154. {
  155. int cbeUseMask = config.AccessibleConstantBuffersMask;
  156. while (cbeUseMask != 0)
  157. {
  158. int slot = BitOperations.TrailingZeroCount(cbeUseMask);
  159. int cbSlot = UbeFirstCbuf + slot;
  160. cbeUseMask &= ~(1 << slot);
  161. config.SetUsedConstantBuffer(cbSlot);
  162. Operand previousResult = PrependExistingOperation(storageOp);
  163. int cbOffset = GetConstantUbeOffset(slot);
  164. Operand inRange = BindingRangeCheck(cbOffset, out Operand baseAddrLow);
  165. Operand baseAddrTruncConst = PrependOperation(Instruction.BitwiseAnd, baseAddrLow, alignMask);
  166. Operand byteOffsetConst = PrependOperation(Instruction.Subtract, addrLow, baseAddrTruncConst);
  167. Operand cbIndex = PrependOperation(Instruction.ShiftRightU32, byteOffsetConst, Const(2));
  168. Operand[] sourcesCb = new Operand[operation.SourcesCount];
  169. sourcesCb[0] = Const(cbSlot);
  170. sourcesCb[1] = cbIndex;
  171. for (int index = 2; index < operation.SourcesCount; index++)
  172. {
  173. sourcesCb[index] = operation.GetSource(index);
  174. }
  175. Operand ldcResult = PrependOperation(Instruction.LoadConstant, sourcesCb);
  176. storageOp = new Operation(Instruction.ConditionalSelect, operation.Dest, inRange, ldcResult, previousResult);
  177. }
  178. }
  179. for (int index = 0; index < operation.SourcesCount; index++)
  180. {
  181. operation.SetSource(index, null);
  182. }
  183. LinkedListNode<INode> oldNode = node;
  184. LinkedList<INode> oldNodeList = oldNode.List;
  185. if (storageOp != null)
  186. {
  187. node = node.List.AddBefore(node, storageOp);
  188. }
  189. else
  190. {
  191. node = null;
  192. }
  193. oldNodeList.Remove(oldNode);
  194. return node;
  195. }
  196. private static LinkedListNode<INode> RewriteTextureSample(LinkedListNode<INode> node, ShaderConfig config)
  197. {
  198. TextureOperation texOp = (TextureOperation)node.Value;
  199. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  200. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  201. bool hasInvalidOffset = (hasOffset || hasOffsets) && !config.GpuAccessor.QueryHostSupportsNonConstantTextureOffset();
  202. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  203. bool isCoordNormalized = isBindless || config.GpuAccessor.QueryTextureCoordNormalized(texOp.Handle, texOp.CbufSlot);
  204. if (!hasInvalidOffset && isCoordNormalized)
  205. {
  206. return node;
  207. }
  208. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  209. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  210. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  211. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  212. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  213. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  214. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  215. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  216. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  217. int coordsCount = texOp.Type.GetDimensions();
  218. int offsetsCount;
  219. if (hasOffsets)
  220. {
  221. offsetsCount = coordsCount * 4;
  222. }
  223. else if (hasOffset)
  224. {
  225. offsetsCount = coordsCount;
  226. }
  227. else
  228. {
  229. offsetsCount = 0;
  230. }
  231. Operand[] offsets = new Operand[offsetsCount];
  232. Operand[] sources = new Operand[texOp.SourcesCount - offsetsCount];
  233. int copyCount = 0;
  234. if (isBindless || isIndexed)
  235. {
  236. copyCount++;
  237. }
  238. Operand[] lodSources = new Operand[copyCount + coordsCount];
  239. for (int index = 0; index < lodSources.Length; index++)
  240. {
  241. lodSources[index] = texOp.GetSource(index);
  242. }
  243. copyCount += coordsCount;
  244. if (isArray)
  245. {
  246. copyCount++;
  247. }
  248. if (isShadow)
  249. {
  250. copyCount++;
  251. }
  252. if (hasDerivatives)
  253. {
  254. copyCount += coordsCount * 2;
  255. }
  256. if (isMultisample)
  257. {
  258. copyCount++;
  259. }
  260. else if (hasLodLevel)
  261. {
  262. copyCount++;
  263. }
  264. int srcIndex = 0;
  265. int dstIndex = 0;
  266. for (int index = 0; index < copyCount; index++)
  267. {
  268. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  269. }
  270. bool areAllOffsetsConstant = true;
  271. for (int index = 0; index < offsetsCount; index++)
  272. {
  273. Operand offset = texOp.GetSource(srcIndex++);
  274. areAllOffsetsConstant &= offset.Type == OperandType.Constant;
  275. offsets[index] = offset;
  276. }
  277. hasInvalidOffset &= !areAllOffsetsConstant;
  278. if (!hasInvalidOffset && isCoordNormalized)
  279. {
  280. return node;
  281. }
  282. if (hasLodBias)
  283. {
  284. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  285. }
  286. if (isGather && !isShadow)
  287. {
  288. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  289. }
  290. int coordsIndex = isBindless || isIndexed ? 1 : 0;
  291. int componentIndex = texOp.Index;
  292. Operand Float(Operand value)
  293. {
  294. Operand res = Local();
  295. node.List.AddBefore(node, new Operation(Instruction.ConvertS32ToFP32, res, value));
  296. return res;
  297. }
  298. // Emulate non-normalized coordinates by normalizing the coordinates on the shader.
  299. // Without normalization, the coordinates are expected to the in the [0, W or H] range,
  300. // and otherwise, it is expected to be in the [0, 1] range.
  301. // We normalize by dividing the coords by the texture size.
  302. if (!isCoordNormalized && !intCoords)
  303. {
  304. config.SetUsedFeature(FeatureFlags.IntegerSampling);
  305. int normCoordsCount = (texOp.Type & SamplerType.Mask) == SamplerType.TextureCube ? 2 : coordsCount;
  306. for (int index = 0; index < normCoordsCount; index++)
  307. {
  308. Operand coordSize = Local();
  309. Operand[] texSizeSources;
  310. if (isBindless || isIndexed)
  311. {
  312. texSizeSources = new Operand[] { sources[0], Const(0) };
  313. }
  314. else
  315. {
  316. texSizeSources = new Operand[] { Const(0) };
  317. }
  318. node.List.AddBefore(node, new TextureOperation(
  319. Instruction.TextureSize,
  320. texOp.Type,
  321. texOp.Format,
  322. texOp.Flags,
  323. texOp.CbufSlot,
  324. texOp.Handle,
  325. index,
  326. new[] { coordSize },
  327. texSizeSources));
  328. config.SetUsedTexture(Instruction.TextureSize, texOp.Type, texOp.Format, texOp.Flags, texOp.CbufSlot, texOp.Handle);
  329. Operand source = sources[coordsIndex + index];
  330. Operand coordNormalized = Local();
  331. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Divide, coordNormalized, source, Float(coordSize)));
  332. sources[coordsIndex + index] = coordNormalized;
  333. }
  334. }
  335. Operand[] dests = new Operand[texOp.DestsCount];
  336. for (int i = 0; i < texOp.DestsCount; i++)
  337. {
  338. dests[i] = texOp.GetDest(i);
  339. }
  340. Operand bindlessHandle = isBindless || isIndexed ? sources[0] : null;
  341. LinkedListNode<INode> oldNode = node;
  342. // Technically, non-constant texture offsets are not allowed (according to the spec),
  343. // however some GPUs does support that.
  344. // For GPUs where it is not supported, we can replace the instruction with the following:
  345. // For texture*Offset, we replace it by texture*, and add the offset to the P coords.
  346. // The offset can be calculated as offset / textureSize(lod), where lod = textureQueryLod(coords).
  347. // For texelFetchOffset, we replace it by texelFetch and add the offset to the P coords directly.
  348. // For textureGatherOffset, we split the operation into up to 4 operations, one for each component
  349. // that is accessed, where each textureGather operation has a different offset for each pixel.
  350. if (hasInvalidOffset && isGather && !isShadow)
  351. {
  352. config.SetUsedFeature(FeatureFlags.IntegerSampling);
  353. Operand[] newSources = new Operand[sources.Length];
  354. sources.CopyTo(newSources, 0);
  355. Operand[] texSizes = InsertTextureSize(node, texOp, lodSources, bindlessHandle, coordsCount);
  356. int destIndex = 0;
  357. for (int compIndex = 0; compIndex < 4; compIndex++)
  358. {
  359. if (((texOp.Index >> compIndex) & 1) == 0)
  360. {
  361. continue;
  362. }
  363. for (int index = 0; index < coordsCount; index++)
  364. {
  365. config.SetUsedTexture(Instruction.TextureSize, texOp.Type, texOp.Format, texOp.Flags, texOp.CbufSlot, texOp.Handle);
  366. Operand offset = Local();
  367. Operand intOffset = offsets[index + (hasOffsets ? compIndex * coordsCount : 0)];
  368. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Divide, offset, Float(intOffset), Float(texSizes[index])));
  369. Operand source = sources[coordsIndex + index];
  370. Operand coordPlusOffset = Local();
  371. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Add, coordPlusOffset, source, offset));
  372. newSources[coordsIndex + index] = coordPlusOffset;
  373. }
  374. TextureOperation newTexOp = new TextureOperation(
  375. Instruction.TextureSample,
  376. texOp.Type,
  377. texOp.Format,
  378. texOp.Flags & ~(TextureFlags.Offset | TextureFlags.Offsets),
  379. texOp.CbufSlot,
  380. texOp.Handle,
  381. 1,
  382. new[] { dests[destIndex++] },
  383. newSources);
  384. node = node.List.AddBefore(node, newTexOp);
  385. }
  386. }
  387. else
  388. {
  389. if (hasInvalidOffset)
  390. {
  391. if (intCoords)
  392. {
  393. for (int index = 0; index < coordsCount; index++)
  394. {
  395. Operand source = sources[coordsIndex + index];
  396. Operand coordPlusOffset = Local();
  397. node.List.AddBefore(node, new Operation(Instruction.Add, coordPlusOffset, source, offsets[index]));
  398. sources[coordsIndex + index] = coordPlusOffset;
  399. }
  400. }
  401. else
  402. {
  403. config.SetUsedFeature(FeatureFlags.IntegerSampling);
  404. Operand[] texSizes = InsertTextureSize(node, texOp, lodSources, bindlessHandle, coordsCount);
  405. for (int index = 0; index < coordsCount; index++)
  406. {
  407. config.SetUsedTexture(Instruction.TextureSize, texOp.Type, texOp.Format, texOp.Flags, texOp.CbufSlot, texOp.Handle);
  408. Operand offset = Local();
  409. Operand intOffset = offsets[index];
  410. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Divide, offset, Float(intOffset), Float(texSizes[index])));
  411. Operand source = sources[coordsIndex + index];
  412. Operand coordPlusOffset = Local();
  413. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Add, coordPlusOffset, source, offset));
  414. sources[coordsIndex + index] = coordPlusOffset;
  415. }
  416. }
  417. }
  418. TextureOperation newTexOp = new TextureOperation(
  419. Instruction.TextureSample,
  420. texOp.Type,
  421. texOp.Format,
  422. texOp.Flags & ~(TextureFlags.Offset | TextureFlags.Offsets),
  423. texOp.CbufSlot,
  424. texOp.Handle,
  425. componentIndex,
  426. dests,
  427. sources);
  428. node = node.List.AddBefore(node, newTexOp);
  429. }
  430. node.List.Remove(oldNode);
  431. for (int index = 0; index < texOp.SourcesCount; index++)
  432. {
  433. texOp.SetSource(index, null);
  434. }
  435. return node;
  436. }
  437. private static Operand[] InsertTextureSize(
  438. LinkedListNode<INode> node,
  439. TextureOperation texOp,
  440. Operand[] lodSources,
  441. Operand bindlessHandle,
  442. int coordsCount)
  443. {
  444. Operand Int(Operand value)
  445. {
  446. Operand res = Local();
  447. node.List.AddBefore(node, new Operation(Instruction.ConvertFP32ToS32, res, value));
  448. return res;
  449. }
  450. Operand[] texSizes = new Operand[coordsCount];
  451. Operand lod = Local();
  452. node.List.AddBefore(node, new TextureOperation(
  453. Instruction.Lod,
  454. texOp.Type,
  455. texOp.Format,
  456. texOp.Flags,
  457. texOp.CbufSlot,
  458. texOp.Handle,
  459. 0,
  460. new[] { lod },
  461. lodSources));
  462. for (int index = 0; index < coordsCount; index++)
  463. {
  464. texSizes[index] = Local();
  465. Operand[] texSizeSources;
  466. if (bindlessHandle != null)
  467. {
  468. texSizeSources = new Operand[] { bindlessHandle, Int(lod) };
  469. }
  470. else
  471. {
  472. texSizeSources = new Operand[] { Int(lod) };
  473. }
  474. node.List.AddBefore(node, new TextureOperation(
  475. Instruction.TextureSize,
  476. texOp.Type,
  477. texOp.Format,
  478. texOp.Flags,
  479. texOp.CbufSlot,
  480. texOp.Handle,
  481. index,
  482. new[] { texSizes[index] },
  483. texSizeSources));
  484. }
  485. return texSizes;
  486. }
  487. private static LinkedListNode<INode> InsertSnormNormalization(LinkedListNode<INode> node, ShaderConfig config)
  488. {
  489. TextureOperation texOp = (TextureOperation)node.Value;
  490. // We can't query the format of a bindless texture,
  491. // because the handle is unknown, it can have any format.
  492. if (texOp.Flags.HasFlag(TextureFlags.Bindless))
  493. {
  494. return node;
  495. }
  496. TextureFormat format = config.GpuAccessor.QueryTextureFormat(texOp.Handle, texOp.CbufSlot);
  497. int maxPositive = format switch
  498. {
  499. TextureFormat.R8Snorm => sbyte.MaxValue,
  500. TextureFormat.R8G8Snorm => sbyte.MaxValue,
  501. TextureFormat.R8G8B8A8Snorm => sbyte.MaxValue,
  502. TextureFormat.R16Snorm => short.MaxValue,
  503. TextureFormat.R16G16Snorm => short.MaxValue,
  504. TextureFormat.R16G16B16A16Snorm => short.MaxValue,
  505. _ => 0
  506. };
  507. // The value being 0 means that the format is not a SNORM format,
  508. // so there's nothing to do here.
  509. if (maxPositive == 0)
  510. {
  511. return node;
  512. }
  513. // Do normalization. We assume SINT formats are being used
  514. // as replacement for SNORM (which is not supported).
  515. for (int i = 0; i < texOp.DestsCount; i++)
  516. {
  517. Operand dest = texOp.GetDest(i);
  518. INode[] uses = dest.UseOps.ToArray();
  519. Operation convOp = new Operation(Instruction.ConvertS32ToFP32, Local(), dest);
  520. Operation normOp = new Operation(Instruction.FP32 | Instruction.Multiply, Local(), convOp.Dest, ConstF(1f / maxPositive));
  521. node = node.List.AddAfter(node, convOp);
  522. node = node.List.AddAfter(node, normOp);
  523. foreach (INode useOp in uses)
  524. {
  525. if (useOp is not Operation op)
  526. {
  527. continue;
  528. }
  529. // Replace all uses of the texture pixel value with the normalized value.
  530. for (int index = 0; index < op.SourcesCount; index++)
  531. {
  532. if (op.GetSource(index) == dest)
  533. {
  534. op.SetSource(index, normOp.Dest);
  535. }
  536. }
  537. }
  538. }
  539. return node;
  540. }
  541. private static bool ReplaceConstantBufferWithDrawParameters(Operation operation)
  542. {
  543. bool modified = false;
  544. for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
  545. {
  546. Operand src = operation.GetSource(srcIndex);
  547. if (src.Type == OperandType.ConstantBuffer && src.GetCbufSlot() == 0)
  548. {
  549. switch (src.GetCbufOffset())
  550. {
  551. case Constants.NvnBaseVertexByteOffset / 4:
  552. operation.SetSource(srcIndex, Attribute(AttributeConsts.BaseVertex));
  553. modified = true;
  554. break;
  555. case Constants.NvnBaseInstanceByteOffset / 4:
  556. operation.SetSource(srcIndex, Attribute(AttributeConsts.BaseInstance));
  557. modified = true;
  558. break;
  559. case Constants.NvnDrawIndexByteOffset / 4:
  560. operation.SetSource(srcIndex, Attribute(AttributeConsts.DrawIndex));
  561. modified = true;
  562. break;
  563. }
  564. }
  565. }
  566. return modified;
  567. }
  568. private static bool HasConstantBufferDrawParameters(Operation operation)
  569. {
  570. for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
  571. {
  572. Operand src = operation.GetSource(srcIndex);
  573. if (src.Type == OperandType.ConstantBuffer && src.GetCbufSlot() == 0)
  574. {
  575. switch (src.GetCbufOffset())
  576. {
  577. case Constants.NvnBaseVertexByteOffset / 4:
  578. case Constants.NvnBaseInstanceByteOffset / 4:
  579. case Constants.NvnDrawIndexByteOffset / 4:
  580. return true;
  581. }
  582. }
  583. }
  584. return false;
  585. }
  586. }
  587. }