shader.h 12 KB

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