shader_ir.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <list>
  7. #include <map>
  8. #include <optional>
  9. #include <set>
  10. #include <tuple>
  11. #include <vector>
  12. #include "common/common_types.h"
  13. #include "video_core/engines/maxwell_3d.h"
  14. #include "video_core/engines/shader_bytecode.h"
  15. #include "video_core/engines/shader_header.h"
  16. #include "video_core/shader/ast.h"
  17. #include "video_core/shader/compiler_settings.h"
  18. #include "video_core/shader/node.h"
  19. #include "video_core/shader/registry.h"
  20. namespace VideoCommon::Shader {
  21. struct ShaderBlock;
  22. using ProgramCode = std::vector<u64>;
  23. constexpr u32 MAX_PROGRAM_LENGTH = 0x1000;
  24. struct ConstBuffer {
  25. constexpr explicit ConstBuffer(u32 max_offset, bool is_indirect)
  26. : max_offset{max_offset}, is_indirect{is_indirect} {}
  27. constexpr ConstBuffer() = default;
  28. void MarkAsUsed(u64 offset) {
  29. max_offset = std::max(max_offset, static_cast<u32>(offset));
  30. }
  31. void MarkAsUsedIndirect() {
  32. is_indirect = true;
  33. }
  34. bool IsIndirect() const {
  35. return is_indirect;
  36. }
  37. u32 GetSize() const {
  38. return max_offset + static_cast<u32>(sizeof(float));
  39. }
  40. u32 GetMaxOffset() const {
  41. return max_offset;
  42. }
  43. private:
  44. u32 max_offset = 0;
  45. bool is_indirect = false;
  46. };
  47. struct GlobalMemoryUsage {
  48. bool is_read{};
  49. bool is_written{};
  50. };
  51. class ShaderIR final {
  52. public:
  53. explicit ShaderIR(const ProgramCode& program_code, u32 main_offset, CompilerSettings settings,
  54. Registry& registry);
  55. ~ShaderIR();
  56. const std::map<u32, NodeBlock>& GetBasicBlocks() const {
  57. return basic_blocks;
  58. }
  59. const std::set<u32>& GetRegisters() const {
  60. return used_registers;
  61. }
  62. const std::set<Tegra::Shader::Pred>& GetPredicates() const {
  63. return used_predicates;
  64. }
  65. const std::set<Tegra::Shader::Attribute::Index>& GetInputAttributes() const {
  66. return used_input_attributes;
  67. }
  68. const std::set<Tegra::Shader::Attribute::Index>& GetOutputAttributes() const {
  69. return used_output_attributes;
  70. }
  71. const std::map<u32, ConstBuffer>& GetConstantBuffers() const {
  72. return used_cbufs;
  73. }
  74. const std::list<Sampler>& GetSamplers() const {
  75. return used_samplers;
  76. }
  77. const std::list<Image>& GetImages() const {
  78. return used_images;
  79. }
  80. const std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances>& GetClipDistances()
  81. const {
  82. return used_clip_distances;
  83. }
  84. const std::map<GlobalMemoryBase, GlobalMemoryUsage>& GetGlobalMemory() const {
  85. return used_global_memory;
  86. }
  87. std::size_t GetLength() const {
  88. return static_cast<std::size_t>(coverage_end * sizeof(u64));
  89. }
  90. bool UsesLayer() const {
  91. return uses_layer;
  92. }
  93. bool UsesViewportIndex() const {
  94. return uses_viewport_index;
  95. }
  96. bool UsesPointSize() const {
  97. return uses_point_size;
  98. }
  99. bool UsesInstanceId() const {
  100. return uses_instance_id;
  101. }
  102. bool UsesVertexId() const {
  103. return uses_vertex_id;
  104. }
  105. bool UsesLegacyVaryings() const {
  106. return uses_legacy_varyings;
  107. }
  108. bool UsesWarps() const {
  109. return uses_warps;
  110. }
  111. bool HasPhysicalAttributes() const {
  112. return uses_physical_attributes;
  113. }
  114. const Tegra::Shader::Header& GetHeader() const {
  115. return header;
  116. }
  117. bool IsFlowStackDisabled() const {
  118. return disable_flow_stack;
  119. }
  120. bool IsDecompiled() const {
  121. return decompiled;
  122. }
  123. const ASTManager& GetASTManager() const {
  124. return program_manager;
  125. }
  126. ASTNode GetASTProgram() const {
  127. return program_manager.GetProgram();
  128. }
  129. u32 GetASTNumVariables() const {
  130. return program_manager.GetVariables();
  131. }
  132. u32 ConvertAddressToNvidiaSpace(u32 address) const {
  133. return (address - main_offset) * static_cast<u32>(sizeof(Tegra::Shader::Instruction));
  134. }
  135. /// Returns a condition code evaluated from internal flags
  136. Node GetConditionCode(Tegra::Shader::ConditionCode cc) const;
  137. const Node& GetAmendNode(std::size_t index) const {
  138. return amend_code[index];
  139. }
  140. u32 GetNumCustomVariables() const {
  141. return num_custom_variables;
  142. }
  143. private:
  144. friend class ASTDecoder;
  145. struct SamplerInfo {
  146. Tegra::Shader::TextureType type;
  147. bool is_array;
  148. bool is_shadow;
  149. bool is_buffer;
  150. };
  151. void Decode();
  152. void PostDecode();
  153. NodeBlock DecodeRange(u32 begin, u32 end);
  154. void DecodeRangeInner(NodeBlock& bb, u32 begin, u32 end);
  155. void InsertControlFlow(NodeBlock& bb, const ShaderBlock& block);
  156. /**
  157. * Decodes a single instruction from Tegra to IR.
  158. * @param bb Basic block where the nodes will be written to.
  159. * @param pc Program counter. Offset to decode.
  160. * @return Next address to decode.
  161. */
  162. u32 DecodeInstr(NodeBlock& bb, u32 pc);
  163. u32 DecodeArithmetic(NodeBlock& bb, u32 pc);
  164. u32 DecodeArithmeticImmediate(NodeBlock& bb, u32 pc);
  165. u32 DecodeBfe(NodeBlock& bb, u32 pc);
  166. u32 DecodeBfi(NodeBlock& bb, u32 pc);
  167. u32 DecodeShift(NodeBlock& bb, u32 pc);
  168. u32 DecodeArithmeticInteger(NodeBlock& bb, u32 pc);
  169. u32 DecodeArithmeticIntegerImmediate(NodeBlock& bb, u32 pc);
  170. u32 DecodeArithmeticHalf(NodeBlock& bb, u32 pc);
  171. u32 DecodeArithmeticHalfImmediate(NodeBlock& bb, u32 pc);
  172. u32 DecodeFfma(NodeBlock& bb, u32 pc);
  173. u32 DecodeHfma2(NodeBlock& bb, u32 pc);
  174. u32 DecodeConversion(NodeBlock& bb, u32 pc);
  175. u32 DecodeWarp(NodeBlock& bb, u32 pc);
  176. u32 DecodeMemory(NodeBlock& bb, u32 pc);
  177. u32 DecodeTexture(NodeBlock& bb, u32 pc);
  178. u32 DecodeImage(NodeBlock& bb, u32 pc);
  179. u32 DecodeFloatSetPredicate(NodeBlock& bb, u32 pc);
  180. u32 DecodeIntegerSetPredicate(NodeBlock& bb, u32 pc);
  181. u32 DecodeHalfSetPredicate(NodeBlock& bb, u32 pc);
  182. u32 DecodePredicateSetRegister(NodeBlock& bb, u32 pc);
  183. u32 DecodePredicateSetPredicate(NodeBlock& bb, u32 pc);
  184. u32 DecodeRegisterSetPredicate(NodeBlock& bb, u32 pc);
  185. u32 DecodeFloatSet(NodeBlock& bb, u32 pc);
  186. u32 DecodeIntegerSet(NodeBlock& bb, u32 pc);
  187. u32 DecodeHalfSet(NodeBlock& bb, u32 pc);
  188. u32 DecodeVideo(NodeBlock& bb, u32 pc);
  189. u32 DecodeXmad(NodeBlock& bb, u32 pc);
  190. u32 DecodeOther(NodeBlock& bb, u32 pc);
  191. /// Generates a node for a passed register.
  192. Node GetRegister(Tegra::Shader::Register reg);
  193. /// Generates a node for a custom variable
  194. Node GetCustomVariable(u32 id);
  195. /// Generates a node representing a 19-bit immediate value
  196. Node GetImmediate19(Tegra::Shader::Instruction instr);
  197. /// Generates a node representing a 32-bit immediate value
  198. Node GetImmediate32(Tegra::Shader::Instruction instr);
  199. /// Generates a node representing a constant buffer
  200. Node GetConstBuffer(u64 index, u64 offset);
  201. /// Generates a node representing a constant buffer with a variadic offset
  202. Node GetConstBufferIndirect(u64 index, u64 offset, Node node);
  203. /// Generates a node for a passed predicate. It can be optionally negated
  204. Node GetPredicate(u64 pred, bool negated = false);
  205. /// Generates a predicate node for an immediate true or false value
  206. Node GetPredicate(bool immediate);
  207. /// Generates a node representing an input attribute. Keeps track of used attributes.
  208. Node GetInputAttribute(Tegra::Shader::Attribute::Index index, u64 element, Node buffer = {});
  209. /// Generates a node representing a physical input attribute.
  210. Node GetPhysicalInputAttribute(Tegra::Shader::Register physical_address, Node buffer = {});
  211. /// Generates a node representing an output attribute. Keeps track of used attributes.
  212. Node GetOutputAttribute(Tegra::Shader::Attribute::Index index, u64 element, Node buffer);
  213. /// Generates a node representing an internal flag
  214. Node GetInternalFlag(InternalFlag flag, bool negated = false) const;
  215. /// Generates a node representing a local memory address
  216. Node GetLocalMemory(Node address);
  217. /// Generates a node representing a shared memory address
  218. Node GetSharedMemory(Node address);
  219. /// Generates a temporary, internally it uses a post-RZ register
  220. Node GetTemporary(u32 id);
  221. /// Sets a register. src value must be a number-evaluated node.
  222. void SetRegister(NodeBlock& bb, Tegra::Shader::Register dest, Node src);
  223. /// Sets a predicate. src value must be a bool-evaluated node
  224. void SetPredicate(NodeBlock& bb, u64 dest, Node src);
  225. /// Sets an internal flag. src value must be a bool-evaluated node
  226. void SetInternalFlag(NodeBlock& bb, InternalFlag flag, Node value);
  227. /// Sets a local memory address with a value.
  228. void SetLocalMemory(NodeBlock& bb, Node address, Node value);
  229. /// Sets a shared memory address with a value.
  230. void SetSharedMemory(NodeBlock& bb, Node address, Node value);
  231. /// Sets a temporary. Internally it uses a post-RZ register
  232. void SetTemporary(NodeBlock& bb, u32 id, Node value);
  233. /// Sets internal flags from a float
  234. void SetInternalFlagsFromFloat(NodeBlock& bb, Node value, bool sets_cc = true);
  235. /// Sets internal flags from an integer
  236. void SetInternalFlagsFromInteger(NodeBlock& bb, Node value, bool sets_cc = true);
  237. /// Conditionally absolute/negated float. Absolute is applied first
  238. Node GetOperandAbsNegFloat(Node value, bool absolute, bool negate);
  239. /// Conditionally saturates a float
  240. Node GetSaturatedFloat(Node value, bool saturate = true);
  241. /// Converts an integer to different sizes.
  242. Node ConvertIntegerSize(Node value, Tegra::Shader::Register::Size size, bool is_signed);
  243. /// Conditionally absolute/negated integer. Absolute is applied first
  244. Node GetOperandAbsNegInteger(Node value, bool absolute, bool negate, bool is_signed);
  245. /// Unpacks a half immediate from an instruction
  246. Node UnpackHalfImmediate(Tegra::Shader::Instruction instr, bool has_negation);
  247. /// Unpacks a binary value into a half float pair with a type format
  248. Node UnpackHalfFloat(Node value, Tegra::Shader::HalfType type);
  249. /// Merges a half pair into another value
  250. Node HalfMerge(Node dest, Node src, Tegra::Shader::HalfMerge merge);
  251. /// Conditionally absolute/negated half float pair. Absolute is applied first
  252. Node GetOperandAbsNegHalf(Node value, bool absolute, bool negate);
  253. /// Conditionally saturates a half float pair
  254. Node GetSaturatedHalfFloat(Node value, bool saturate = true);
  255. /// Get image component value by type and size
  256. std::pair<Node, bool> GetComponentValue(Tegra::Texture::ComponentType component_type,
  257. u32 component_size, Node original_value);
  258. /// Returns a predicate comparing two floats
  259. Node GetPredicateComparisonFloat(Tegra::Shader::PredCondition condition, Node op_a, Node op_b);
  260. /// Returns a predicate comparing two integers
  261. Node GetPredicateComparisonInteger(Tegra::Shader::PredCondition condition, bool is_signed,
  262. Node op_a, Node op_b);
  263. /// Returns a predicate comparing two half floats. meta consumes how both pairs will be compared
  264. Node GetPredicateComparisonHalf(Tegra::Shader::PredCondition condition, Node op_a, Node op_b);
  265. /// Returns a predicate combiner operation
  266. OperationCode GetPredicateCombiner(Tegra::Shader::PredOperation operation);
  267. /// Queries the missing sampler info from the execution context.
  268. SamplerInfo GetSamplerInfo(std::optional<SamplerInfo> sampler_info, u32 offset,
  269. std::optional<u32> buffer = std::nullopt);
  270. /// Accesses a texture sampler
  271. std::optional<Sampler> GetSampler(const Tegra::Shader::Sampler& sampler,
  272. std::optional<SamplerInfo> sampler_info = std::nullopt);
  273. /// Accesses a texture sampler for a bindless texture.
  274. std::optional<Sampler> GetBindlessSampler(
  275. Tegra::Shader::Register reg, Node& index_var,
  276. std::optional<SamplerInfo> sampler_info = std::nullopt);
  277. /// Accesses an image.
  278. Image& GetImage(Tegra::Shader::Image image, Tegra::Shader::ImageType type);
  279. /// Access a bindless image sampler.
  280. Image& GetBindlessImage(Tegra::Shader::Register reg, Tegra::Shader::ImageType type);
  281. /// Extracts a sequence of bits from a node
  282. Node BitfieldExtract(Node value, u32 offset, u32 bits);
  283. /// Inserts a sequence of bits from a node
  284. Node BitfieldInsert(Node base, Node insert, u32 offset, u32 bits);
  285. /// Marks the usage of a input or output attribute.
  286. void MarkAttributeUsage(Tegra::Shader::Attribute::Index index, u64 element);
  287. /// Decodes VMNMX instruction and inserts its code into the passed basic block.
  288. void DecodeVMNMX(NodeBlock& bb, Tegra::Shader::Instruction instr);
  289. void WriteTexInstructionFloat(NodeBlock& bb, Tegra::Shader::Instruction instr,
  290. const Node4& components);
  291. void WriteTexsInstructionFloat(NodeBlock& bb, Tegra::Shader::Instruction instr,
  292. const Node4& components, bool ignore_mask = false);
  293. void WriteTexsInstructionHalfFloat(NodeBlock& bb, Tegra::Shader::Instruction instr,
  294. const Node4& components, bool ignore_mask = false);
  295. Node4 GetTexCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  296. Tegra::Shader::TextureProcessMode process_mode, bool depth_compare,
  297. bool is_array, bool is_aoffi,
  298. std::optional<Tegra::Shader::Register> bindless_reg);
  299. Node4 GetTexsCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  300. Tegra::Shader::TextureProcessMode process_mode, bool depth_compare,
  301. bool is_array);
  302. Node4 GetTld4Code(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  303. bool depth_compare, bool is_array, bool is_aoffi, bool is_ptp,
  304. bool is_bindless);
  305. Node4 GetTldCode(Tegra::Shader::Instruction instr);
  306. Node4 GetTldsCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  307. bool is_array);
  308. std::tuple<std::size_t, std::size_t> ValidateAndGetCoordinateElement(
  309. Tegra::Shader::TextureType texture_type, bool depth_compare, bool is_array,
  310. bool lod_bias_enabled, std::size_t max_coords, std::size_t max_inputs);
  311. std::vector<Node> GetAoffiCoordinates(Node aoffi_reg, std::size_t coord_count, bool is_tld4);
  312. std::vector<Node> GetPtpCoordinates(std::array<Node, 2> ptp_regs);
  313. Node4 GetTextureCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  314. Tegra::Shader::TextureProcessMode process_mode, std::vector<Node> coords,
  315. Node array, Node depth_compare, u32 bias_offset, std::vector<Node> aoffi,
  316. std::optional<Tegra::Shader::Register> bindless_reg);
  317. Node GetVideoOperand(Node op, bool is_chunk, bool is_signed, Tegra::Shader::VideoType type,
  318. u64 byte_height);
  319. void WriteLogicOperation(NodeBlock& bb, Tegra::Shader::Register dest,
  320. Tegra::Shader::LogicOperation logic_op, Node op_a, Node op_b,
  321. Tegra::Shader::PredicateResultMode predicate_mode,
  322. Tegra::Shader::Pred predicate, bool sets_cc);
  323. void WriteLop3Instruction(NodeBlock& bb, Tegra::Shader::Register dest, Node op_a, Node op_b,
  324. Node op_c, Node imm_lut, bool sets_cc);
  325. std::tuple<Node, u32, u32> TrackCbuf(Node tracked, const NodeBlock& code, s64 cursor) const;
  326. std::tuple<Node, TrackSampler> TrackBindlessSampler(Node tracked, const NodeBlock& code,
  327. s64 cursor);
  328. std::optional<u32> TrackImmediate(Node tracked, const NodeBlock& code, s64 cursor) const;
  329. std::pair<Node, s64> TrackRegister(const GprNode* tracked, const NodeBlock& code,
  330. s64 cursor) const;
  331. std::tuple<Node, Node, GlobalMemoryBase> TrackGlobalMemory(NodeBlock& bb,
  332. Tegra::Shader::Instruction instr,
  333. bool is_read, bool is_write);
  334. /// Register new amending code and obtain the reference id.
  335. std::size_t DeclareAmend(Node new_amend);
  336. u32 NewCustomVariable();
  337. const ProgramCode& program_code;
  338. const u32 main_offset;
  339. const CompilerSettings settings;
  340. Registry& registry;
  341. bool decompiled{};
  342. bool disable_flow_stack{};
  343. u32 coverage_begin{};
  344. u32 coverage_end{};
  345. std::map<u32, NodeBlock> basic_blocks;
  346. NodeBlock global_code;
  347. ASTManager program_manager{true, true};
  348. std::vector<Node> amend_code;
  349. u32 num_custom_variables{};
  350. std::set<u32> used_registers;
  351. std::set<Tegra::Shader::Pred> used_predicates;
  352. std::set<Tegra::Shader::Attribute::Index> used_input_attributes;
  353. std::set<Tegra::Shader::Attribute::Index> used_output_attributes;
  354. std::map<u32, ConstBuffer> used_cbufs;
  355. std::list<Sampler> used_samplers;
  356. std::list<Image> used_images;
  357. std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances> used_clip_distances{};
  358. std::map<GlobalMemoryBase, GlobalMemoryUsage> used_global_memory;
  359. bool uses_layer{};
  360. bool uses_viewport_index{};
  361. bool uses_point_size{};
  362. bool uses_physical_attributes{}; // Shader uses AL2P or physical attribute read/writes
  363. bool uses_instance_id{};
  364. bool uses_vertex_id{};
  365. bool uses_legacy_varyings{};
  366. bool uses_warps{};
  367. bool uses_indexed_samplers{};
  368. Tegra::Shader::Header header;
  369. };
  370. } // namespace VideoCommon::Shader