shader_ir.h 16 KB

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