shader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <vector>
  6. #include <boost/container/static_vector.hpp>
  7. #include <nihstro/shader_binary.h>
  8. #include "common/common_funcs.h"
  9. #include "common/common_types.h"
  10. #include "common/vector_math.h"
  11. #include "video_core/pica.h"
  12. using nihstro::RegisterType;
  13. using nihstro::SourceRegister;
  14. using nihstro::DestRegister;
  15. namespace Pica {
  16. namespace Shader {
  17. struct InputVertex {
  18. Math::Vec4<float24> attr[16];
  19. };
  20. struct OutputVertex {
  21. OutputVertex() = default;
  22. // VS output attributes
  23. Math::Vec4<float24> pos;
  24. Math::Vec4<float24> dummy; // quaternions (not implemented, yet)
  25. Math::Vec4<float24> color;
  26. Math::Vec2<float24> tc0;
  27. Math::Vec2<float24> tc1;
  28. float24 pad[6];
  29. Math::Vec2<float24> tc2;
  30. // Padding for optimal alignment
  31. float24 pad2[4];
  32. // Attributes used to store intermediate results
  33. // position after perspective divide
  34. Math::Vec3<float24> screenpos;
  35. float24 pad3;
  36. // Linear interpolation
  37. // factor: 0=this, 1=vtx
  38. void Lerp(float24 factor, const OutputVertex& vtx) {
  39. pos = pos * factor + vtx.pos * (float24::FromFloat32(1) - factor);
  40. // TODO: Should perform perspective correct interpolation here...
  41. tc0 = tc0 * factor + vtx.tc0 * (float24::FromFloat32(1) - factor);
  42. tc1 = tc1 * factor + vtx.tc1 * (float24::FromFloat32(1) - factor);
  43. tc2 = tc2 * factor + vtx.tc2 * (float24::FromFloat32(1) - factor);
  44. screenpos = screenpos * factor + vtx.screenpos * (float24::FromFloat32(1) - factor);
  45. color = color * factor + vtx.color * (float24::FromFloat32(1) - factor);
  46. }
  47. // Linear interpolation
  48. // factor: 0=v0, 1=v1
  49. static OutputVertex Lerp(float24 factor, const OutputVertex& v0, const OutputVertex& v1) {
  50. OutputVertex ret = v0;
  51. ret.Lerp(factor, v1);
  52. return ret;
  53. }
  54. };
  55. static_assert(std::is_pod<OutputVertex>::value, "Structure is not POD");
  56. static_assert(sizeof(OutputVertex) == 32 * sizeof(float), "OutputVertex has invalid size");
  57. // Helper structure used to keep track of data useful for inspection of shader emulation
  58. template<bool full_debugging>
  59. struct DebugData;
  60. template<>
  61. struct DebugData<false> {
  62. // TODO: Hide these behind and interface and move them to DebugData<true>
  63. u32 max_offset; // maximum program counter ever reached
  64. u32 max_opdesc_id; // maximum swizzle pattern index ever used
  65. };
  66. template<>
  67. struct DebugData<true> {
  68. // Records store the input and output operands of a particular instruction.
  69. struct Record {
  70. enum Type {
  71. // Floating point arithmetic operands
  72. SRC1 = 0x1,
  73. SRC2 = 0x2,
  74. SRC3 = 0x4,
  75. // Initial and final output operand value
  76. DEST_IN = 0x8,
  77. DEST_OUT = 0x10,
  78. // Current and next instruction offset (in words)
  79. CUR_INSTR = 0x20,
  80. NEXT_INSTR = 0x40,
  81. // Output address register value
  82. ADDR_REG_OUT = 0x80,
  83. // Result of a comparison instruction
  84. CMP_RESULT = 0x100,
  85. // Input values for conditional flow control instructions
  86. COND_BOOL_IN = 0x200,
  87. COND_CMP_IN = 0x400,
  88. // Input values for a loop
  89. LOOP_INT_IN = 0x800,
  90. };
  91. Math::Vec4<float24> src1;
  92. Math::Vec4<float24> src2;
  93. Math::Vec4<float24> src3;
  94. Math::Vec4<float24> dest_in;
  95. Math::Vec4<float24> dest_out;
  96. s32 address_registers[2];
  97. bool conditional_code[2];
  98. bool cond_bool;
  99. bool cond_cmp[2];
  100. Math::Vec4<u8> loop_int;
  101. u32 instruction_offset;
  102. u32 next_instruction;
  103. // set of enabled fields (as a combination of Type flags)
  104. unsigned mask = 0;
  105. };
  106. u32 max_offset; // maximum program counter ever reached
  107. u32 max_opdesc_id; // maximum swizzle pattern index ever used
  108. // List of records for each executed shader instruction
  109. std::vector<DebugData<true>::Record> records;
  110. };
  111. // Type alias for better readability
  112. using DebugDataRecord = DebugData<true>::Record;
  113. // Helper function to set a DebugData<true>::Record field based on the template enum parameter.
  114. template<DebugDataRecord::Type type, typename ValueType>
  115. inline void SetField(DebugDataRecord& record, ValueType value);
  116. template<>
  117. inline void SetField<DebugDataRecord::SRC1>(DebugDataRecord& record, float24* value) {
  118. record.src1.x = value[0];
  119. record.src1.y = value[1];
  120. record.src1.z = value[2];
  121. record.src1.w = value[3];
  122. }
  123. template<>
  124. inline void SetField<DebugDataRecord::SRC2>(DebugDataRecord& record, float24* value) {
  125. record.src2.x = value[0];
  126. record.src2.y = value[1];
  127. record.src2.z = value[2];
  128. record.src2.w = value[3];
  129. }
  130. template<>
  131. inline void SetField<DebugDataRecord::SRC3>(DebugDataRecord& record, float24* value) {
  132. record.src3.x = value[0];
  133. record.src3.y = value[1];
  134. record.src3.z = value[2];
  135. record.src3.w = value[3];
  136. }
  137. template<>
  138. inline void SetField<DebugDataRecord::DEST_IN>(DebugDataRecord& record, float24* value) {
  139. record.dest_in.x = value[0];
  140. record.dest_in.y = value[1];
  141. record.dest_in.z = value[2];
  142. record.dest_in.w = value[3];
  143. }
  144. template<>
  145. inline void SetField<DebugDataRecord::DEST_OUT>(DebugDataRecord& record, float24* value) {
  146. record.dest_out.x = value[0];
  147. record.dest_out.y = value[1];
  148. record.dest_out.z = value[2];
  149. record.dest_out.w = value[3];
  150. }
  151. template<>
  152. inline void SetField<DebugDataRecord::ADDR_REG_OUT>(DebugDataRecord& record, s32* value) {
  153. record.address_registers[0] = value[0];
  154. record.address_registers[1] = value[1];
  155. }
  156. template<>
  157. inline void SetField<DebugDataRecord::CMP_RESULT>(DebugDataRecord& record, bool* value) {
  158. record.conditional_code[0] = value[0];
  159. record.conditional_code[1] = value[1];
  160. }
  161. template<>
  162. inline void SetField<DebugDataRecord::COND_BOOL_IN>(DebugDataRecord& record, bool value) {
  163. record.cond_bool = value;
  164. }
  165. template<>
  166. inline void SetField<DebugDataRecord::COND_CMP_IN>(DebugDataRecord& record, bool* value) {
  167. record.cond_cmp[0] = value[0];
  168. record.cond_cmp[1] = value[1];
  169. }
  170. template<>
  171. inline void SetField<DebugDataRecord::LOOP_INT_IN>(DebugDataRecord& record, Math::Vec4<u8> value) {
  172. record.loop_int = value;
  173. }
  174. template<>
  175. inline void SetField<DebugDataRecord::CUR_INSTR>(DebugDataRecord& record, u32 value) {
  176. record.instruction_offset = value;
  177. }
  178. template<>
  179. inline void SetField<DebugDataRecord::NEXT_INSTR>(DebugDataRecord& record, u32 value) {
  180. record.next_instruction = value;
  181. }
  182. // Helper function to set debug information on the current shader iteration.
  183. template<DebugDataRecord::Type type, typename ValueType>
  184. inline void Record(DebugData<false>& debug_data, u32 offset, ValueType value) {
  185. // Debugging disabled => nothing to do
  186. }
  187. template<DebugDataRecord::Type type, typename ValueType>
  188. inline void Record(DebugData<true>& debug_data, u32 offset, ValueType value) {
  189. if (offset >= debug_data.records.size())
  190. debug_data.records.resize(offset + 1);
  191. SetField<type, ValueType>(debug_data.records[offset], value);
  192. debug_data.records[offset].mask |= type;
  193. }
  194. /**
  195. * This structure contains the state information that needs to be unique for a shader unit. The 3DS
  196. * has four shader units that process shaders in parallel. At the present, Citra only implements a
  197. * single shader unit that processes all shaders serially. Putting the state information in a struct
  198. * here will make it easier for us to parallelize the shader processing later.
  199. */
  200. template<bool Debug>
  201. struct UnitState {
  202. struct Registers {
  203. // The registers are accessed by the shader JIT using SSE instructions, and are therefore
  204. // required to be 16-byte aligned.
  205. Math::Vec4<float24> MEMORY_ALIGNED16(input[16]);
  206. Math::Vec4<float24> MEMORY_ALIGNED16(output[16]);
  207. Math::Vec4<float24> MEMORY_ALIGNED16(temporary[16]);
  208. } registers;
  209. static_assert(std::is_pod<Registers>::value, "Structure is not POD");
  210. u32 program_counter;
  211. bool conditional_code[2];
  212. // Two Address registers and one loop counter
  213. // TODO: How many bits do these actually have?
  214. s32 address_registers[3];
  215. enum {
  216. INVALID_ADDRESS = 0xFFFFFFFF
  217. };
  218. struct CallStackElement {
  219. u32 final_address; // Address upon which we jump to return_address
  220. u32 return_address; // Where to jump when leaving scope
  221. u8 repeat_counter; // How often to repeat until this call stack element is removed
  222. u8 loop_increment; // Which value to add to the loop counter after an iteration
  223. // TODO: Should this be a signed value? Does it even matter?
  224. u32 loop_address; // The address where we'll return to after each loop iteration
  225. };
  226. // TODO: Is there a maximal size for this?
  227. boost::container::static_vector<CallStackElement, 16> call_stack;
  228. DebugData<Debug> debug;
  229. static int InputOffset(const SourceRegister& reg) {
  230. switch (reg.GetRegisterType()) {
  231. case RegisterType::Input:
  232. return (int)offsetof(UnitState::Registers, input) + reg.GetIndex()*sizeof(Math::Vec4<float24>);
  233. case RegisterType::Temporary:
  234. return (int)offsetof(UnitState::Registers, temporary) + reg.GetIndex()*sizeof(Math::Vec4<float24>);
  235. default:
  236. UNREACHABLE();
  237. return 0;
  238. }
  239. }
  240. static int OutputOffset(const DestRegister& reg) {
  241. switch (reg.GetRegisterType()) {
  242. case RegisterType::Output:
  243. return (int)offsetof(UnitState::Registers, output) + reg.GetIndex()*sizeof(Math::Vec4<float24>);
  244. case RegisterType::Temporary:
  245. return (int)offsetof(UnitState::Registers, temporary) + reg.GetIndex()*sizeof(Math::Vec4<float24>);
  246. default:
  247. UNREACHABLE();
  248. return 0;
  249. }
  250. }
  251. };
  252. /**
  253. * Performs any shader unit setup that only needs to happen once per shader (as opposed to once per
  254. * vertex, which would happen within the `Run` function).
  255. * @param state Shader unit state, must be setup per shader and per shader unit
  256. */
  257. void Setup(UnitState<false>& state);
  258. /// Performs any cleanup when the emulator is shutdown
  259. void Shutdown();
  260. /**
  261. * Runs the currently setup shader
  262. * @param state Shader unit state, must be setup per shader and per shader unit
  263. * @param input Input vertex into the shader
  264. * @param num_attributes The number of vertex shader attributes
  265. * @return The output vertex, after having been processed by the vertex shader
  266. */
  267. OutputVertex Run(UnitState<false>& state, const InputVertex& input, int num_attributes);
  268. /**
  269. * Produce debug information based on the given shader and input vertex
  270. * @param input Input vertex into the shader
  271. * @param num_attributes The number of vertex shader attributes
  272. * @param config Configuration object for the shader pipeline
  273. * @param setup Setup object for the shader pipeline
  274. * @return Debug information for this shader with regards to the given vertex
  275. */
  276. DebugData<true> ProduceDebugInfo(const InputVertex& input, int num_attributes, const Regs::ShaderConfig& config, const State::ShaderSetup& setup);
  277. } // namespace Shader
  278. } // namespace Pica