shader_ir.h 15 KB

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