shader_ir.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. struct ShaderBlock;
  21. using ProgramCode = std::vector<u64>;
  22. constexpr u32 MAX_PROGRAM_LENGTH = 0x1000;
  23. class ConstBuffer {
  24. public:
  25. explicit ConstBuffer(u32 max_offset, bool is_indirect)
  26. : max_offset{max_offset}, is_indirect{is_indirect} {}
  27. 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 + sizeof(float);
  39. }
  40. u32 GetMaxOffset() const {
  41. return max_offset;
  42. }
  43. private:
  44. u32 max_offset{};
  45. bool is_indirect{};
  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, std::size_t size);
  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::set<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 HasPhysicalAttributes() const {
  90. return uses_physical_attributes;
  91. }
  92. const Tegra::Shader::Header& GetHeader() const {
  93. return header;
  94. }
  95. bool IsFlowStackDisabled() const {
  96. return disable_flow_stack;
  97. }
  98. private:
  99. void Decode();
  100. NodeBlock DecodeRange(u32 begin, u32 end);
  101. void DecodeRangeInner(NodeBlock& bb, u32 begin, u32 end);
  102. void InsertControlFlow(NodeBlock& bb, const ShaderBlock& block);
  103. /**
  104. * Decodes a single instruction from Tegra to IR.
  105. * @param bb Basic block where the nodes will be written to.
  106. * @param pc Program counter. Offset to decode.
  107. * @return Next address to decode.
  108. */
  109. u32 DecodeInstr(NodeBlock& bb, u32 pc);
  110. u32 DecodeArithmetic(NodeBlock& bb, u32 pc);
  111. u32 DecodeArithmeticImmediate(NodeBlock& bb, u32 pc);
  112. u32 DecodeBfe(NodeBlock& bb, u32 pc);
  113. u32 DecodeBfi(NodeBlock& bb, u32 pc);
  114. u32 DecodeShift(NodeBlock& bb, u32 pc);
  115. u32 DecodeArithmeticInteger(NodeBlock& bb, u32 pc);
  116. u32 DecodeArithmeticIntegerImmediate(NodeBlock& bb, u32 pc);
  117. u32 DecodeArithmeticHalf(NodeBlock& bb, u32 pc);
  118. u32 DecodeArithmeticHalfImmediate(NodeBlock& bb, u32 pc);
  119. u32 DecodeFfma(NodeBlock& bb, u32 pc);
  120. u32 DecodeHfma2(NodeBlock& bb, u32 pc);
  121. u32 DecodeConversion(NodeBlock& bb, u32 pc);
  122. u32 DecodeMemory(NodeBlock& bb, u32 pc);
  123. u32 DecodeTexture(NodeBlock& bb, u32 pc);
  124. u32 DecodeImage(NodeBlock& bb, u32 pc);
  125. u32 DecodeFloatSetPredicate(NodeBlock& bb, u32 pc);
  126. u32 DecodeIntegerSetPredicate(NodeBlock& bb, u32 pc);
  127. u32 DecodeHalfSetPredicate(NodeBlock& bb, u32 pc);
  128. u32 DecodePredicateSetRegister(NodeBlock& bb, u32 pc);
  129. u32 DecodePredicateSetPredicate(NodeBlock& bb, u32 pc);
  130. u32 DecodeRegisterSetPredicate(NodeBlock& bb, u32 pc);
  131. u32 DecodeFloatSet(NodeBlock& bb, u32 pc);
  132. u32 DecodeIntegerSet(NodeBlock& bb, u32 pc);
  133. u32 DecodeHalfSet(NodeBlock& bb, u32 pc);
  134. u32 DecodeVideo(NodeBlock& bb, u32 pc);
  135. u32 DecodeXmad(NodeBlock& bb, u32 pc);
  136. u32 DecodeOther(NodeBlock& bb, u32 pc);
  137. /// Generates a node for a passed register.
  138. Node GetRegister(Tegra::Shader::Register reg);
  139. /// Generates a node representing a 19-bit immediate value
  140. Node GetImmediate19(Tegra::Shader::Instruction instr);
  141. /// Generates a node representing a 32-bit immediate value
  142. Node GetImmediate32(Tegra::Shader::Instruction instr);
  143. /// Generates a node representing a constant buffer
  144. Node GetConstBuffer(u64 index, u64 offset);
  145. /// Generates a node representing a constant buffer with a variadic offset
  146. Node GetConstBufferIndirect(u64 index, u64 offset, Node node);
  147. /// Generates a node for a passed predicate. It can be optionally negated
  148. Node GetPredicate(u64 pred, bool negated = false);
  149. /// Generates a predicate node for an immediate true or false value
  150. Node GetPredicate(bool immediate);
  151. /// Generates a node representing an input attribute. Keeps track of used attributes.
  152. Node GetInputAttribute(Tegra::Shader::Attribute::Index index, u64 element, Node buffer = {});
  153. /// Generates a node representing a physical input attribute.
  154. Node GetPhysicalInputAttribute(Tegra::Shader::Register physical_address, Node buffer = {});
  155. /// Generates a node representing an output attribute. Keeps track of used attributes.
  156. Node GetOutputAttribute(Tegra::Shader::Attribute::Index index, u64 element, Node buffer);
  157. /// Generates a node representing an internal flag
  158. Node GetInternalFlag(InternalFlag flag, bool negated = false);
  159. /// Generates a node representing a local memory address
  160. Node GetLocalMemory(Node address);
  161. /// Generates a temporal, internally it uses a post-RZ register
  162. Node GetTemporal(u32 id);
  163. /// Sets a register. src value must be a number-evaluated node.
  164. void SetRegister(NodeBlock& bb, Tegra::Shader::Register dest, Node src);
  165. /// Sets a predicate. src value must be a bool-evaluated node
  166. void SetPredicate(NodeBlock& bb, u64 dest, Node src);
  167. /// Sets an internal flag. src value must be a bool-evaluated node
  168. void SetInternalFlag(NodeBlock& bb, InternalFlag flag, Node value);
  169. /// Sets a local memory address. address and value must be a number-evaluated node
  170. void SetLocalMemory(NodeBlock& bb, Node address, Node value);
  171. /// Sets a temporal. Internally it uses a post-RZ register
  172. void SetTemporal(NodeBlock& bb, u32 id, Node value);
  173. /// Sets internal flags from a float
  174. void SetInternalFlagsFromFloat(NodeBlock& bb, Node value, bool sets_cc = true);
  175. /// Sets internal flags from an integer
  176. void SetInternalFlagsFromInteger(NodeBlock& bb, Node value, bool sets_cc = true);
  177. /// Conditionally absolute/negated float. Absolute is applied first
  178. Node GetOperandAbsNegFloat(Node value, bool absolute, bool negate);
  179. /// Conditionally saturates a float
  180. Node GetSaturatedFloat(Node value, bool saturate = true);
  181. /// Converts an integer to different sizes.
  182. Node ConvertIntegerSize(Node value, Tegra::Shader::Register::Size size, bool is_signed);
  183. /// Conditionally absolute/negated integer. Absolute is applied first
  184. Node GetOperandAbsNegInteger(Node value, bool absolute, bool negate, bool is_signed);
  185. /// Unpacks a half immediate from an instruction
  186. Node UnpackHalfImmediate(Tegra::Shader::Instruction instr, bool has_negation);
  187. /// Unpacks a binary value into a half float pair with a type format
  188. Node UnpackHalfFloat(Node value, Tegra::Shader::HalfType type);
  189. /// Merges a half pair into another value
  190. Node HalfMerge(Node dest, Node src, Tegra::Shader::HalfMerge merge);
  191. /// Conditionally absolute/negated half float pair. Absolute is applied first
  192. Node GetOperandAbsNegHalf(Node value, bool absolute, bool negate);
  193. /// Conditionally saturates a half float pair
  194. Node GetSaturatedHalfFloat(Node value, bool saturate = true);
  195. /// Returns a predicate comparing two floats
  196. Node GetPredicateComparisonFloat(Tegra::Shader::PredCondition condition, Node op_a, Node op_b);
  197. /// Returns a predicate comparing two integers
  198. Node GetPredicateComparisonInteger(Tegra::Shader::PredCondition condition, bool is_signed,
  199. Node op_a, Node op_b);
  200. /// Returns a predicate comparing two half floats. meta consumes how both pairs will be compared
  201. Node GetPredicateComparisonHalf(Tegra::Shader::PredCondition condition, Node op_a, Node op_b);
  202. /// Returns a predicate combiner operation
  203. OperationCode GetPredicateCombiner(Tegra::Shader::PredOperation operation);
  204. /// Returns a condition code evaluated from internal flags
  205. Node GetConditionCode(Tegra::Shader::ConditionCode cc);
  206. /// Accesses a texture sampler
  207. const Sampler& GetSampler(const Tegra::Shader::Sampler& sampler,
  208. Tegra::Shader::TextureType type, bool is_array, bool is_shadow);
  209. // Accesses a texture sampler for a bindless texture.
  210. const Sampler& GetBindlessSampler(const Tegra::Shader::Register& reg,
  211. Tegra::Shader::TextureType type, bool is_array,
  212. bool is_shadow);
  213. /// Accesses an image.
  214. const Image& GetImage(Tegra::Shader::Image image, Tegra::Shader::ImageType type);
  215. /// Access a bindless image sampler.
  216. const Image& GetBindlessImage(Tegra::Shader::Register reg, Tegra::Shader::ImageType type);
  217. /// Extracts a sequence of bits from a node
  218. Node BitfieldExtract(Node value, u32 offset, u32 bits);
  219. void WriteTexInstructionFloat(NodeBlock& bb, Tegra::Shader::Instruction instr,
  220. const Node4& components);
  221. void WriteTexsInstructionFloat(NodeBlock& bb, Tegra::Shader::Instruction instr,
  222. const Node4& components);
  223. void WriteTexsInstructionHalfFloat(NodeBlock& bb, Tegra::Shader::Instruction instr,
  224. const Node4& components);
  225. Node4 GetTexCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  226. Tegra::Shader::TextureProcessMode process_mode, bool depth_compare,
  227. bool is_array, bool is_aoffi,
  228. std::optional<Tegra::Shader::Register> bindless_reg);
  229. Node4 GetTexsCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  230. Tegra::Shader::TextureProcessMode process_mode, bool depth_compare,
  231. bool is_array);
  232. Node4 GetTld4Code(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  233. bool depth_compare, bool is_array, bool is_aoffi);
  234. Node4 GetTldCode(Tegra::Shader::Instruction instr);
  235. Node4 GetTldsCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  236. bool is_array);
  237. std::tuple<std::size_t, std::size_t> ValidateAndGetCoordinateElement(
  238. Tegra::Shader::TextureType texture_type, bool depth_compare, bool is_array,
  239. bool lod_bias_enabled, std::size_t max_coords, std::size_t max_inputs);
  240. std::vector<Node> GetAoffiCoordinates(Node aoffi_reg, std::size_t coord_count, bool is_tld4);
  241. Node4 GetTextureCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  242. Tegra::Shader::TextureProcessMode process_mode, std::vector<Node> coords,
  243. Node array, Node depth_compare, u32 bias_offset, std::vector<Node> aoffi,
  244. std::optional<Tegra::Shader::Register> bindless_reg);
  245. Node GetVideoOperand(Node op, bool is_chunk, bool is_signed, Tegra::Shader::VideoType type,
  246. u64 byte_height);
  247. void WriteLogicOperation(NodeBlock& bb, Tegra::Shader::Register dest,
  248. Tegra::Shader::LogicOperation logic_op, Node op_a, Node op_b,
  249. Tegra::Shader::PredicateResultMode predicate_mode,
  250. Tegra::Shader::Pred predicate, bool sets_cc);
  251. void WriteLop3Instruction(NodeBlock& bb, Tegra::Shader::Register dest, Node op_a, Node op_b,
  252. Node op_c, Node imm_lut, bool sets_cc);
  253. Node TrackCbuf(Node tracked, const NodeBlock& code, s64 cursor) const;
  254. std::optional<u32> TrackImmediate(Node tracked, const NodeBlock& code, s64 cursor) const;
  255. std::pair<Node, s64> TrackRegister(const GprNode* tracked, const NodeBlock& code,
  256. s64 cursor) const;
  257. std::tuple<Node, Node, GlobalMemoryBase> TrackAndGetGlobalMemory(
  258. NodeBlock& bb, Tegra::Shader::Instruction instr, bool is_write);
  259. const ProgramCode& program_code;
  260. const u32 main_offset;
  261. const std::size_t program_size;
  262. bool disable_flow_stack{};
  263. u32 coverage_begin{};
  264. u32 coverage_end{};
  265. std::map<u32, NodeBlock> basic_blocks;
  266. NodeBlock global_code;
  267. std::set<u32> used_registers;
  268. std::set<Tegra::Shader::Pred> used_predicates;
  269. std::set<Tegra::Shader::Attribute::Index> used_input_attributes;
  270. std::set<Tegra::Shader::Attribute::Index> used_output_attributes;
  271. std::map<u32, ConstBuffer> used_cbufs;
  272. std::set<Sampler> used_samplers;
  273. std::set<Image> used_images;
  274. std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances> used_clip_distances{};
  275. std::map<GlobalMemoryBase, GlobalMemoryUsage> used_global_memory;
  276. bool uses_physical_attributes{}; // Shader uses AL2P or physical attribute read/writes
  277. Tegra::Shader::Header header;
  278. };
  279. } // namespace VideoCommon::Shader