shader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 <array>
  6. #include <cstddef>
  7. #include <memory>
  8. #include <type_traits>
  9. #include <vector>
  10. #include <boost/container/static_vector.hpp>
  11. #include <nihstro/shader_bytecode.h>
  12. #include "common/assert.h"
  13. #include "common/common_funcs.h"
  14. #include "common/common_types.h"
  15. #include "common/vector_math.h"
  16. #include "video_core/pica.h"
  17. #include "video_core/pica_types.h"
  18. using nihstro::RegisterType;
  19. using nihstro::SourceRegister;
  20. using nihstro::DestRegister;
  21. namespace Pica {
  22. namespace Shader {
  23. struct InputVertex {
  24. alignas(16) Math::Vec4<float24> attr[16];
  25. };
  26. struct OutputVertex {
  27. OutputVertex() = default;
  28. // VS output attributes
  29. Math::Vec4<float24> pos;
  30. Math::Vec4<float24> quat;
  31. Math::Vec4<float24> color;
  32. Math::Vec2<float24> tc0;
  33. Math::Vec2<float24> tc1;
  34. float24 tc0_w;
  35. INSERT_PADDING_WORDS(1);
  36. Math::Vec3<float24> view;
  37. INSERT_PADDING_WORDS(1);
  38. Math::Vec2<float24> tc2;
  39. // Padding for optimal alignment
  40. INSERT_PADDING_WORDS(4);
  41. // Attributes used to store intermediate results
  42. // position after perspective divide
  43. Math::Vec3<float24> screenpos;
  44. INSERT_PADDING_WORDS(1);
  45. // Linear interpolation
  46. // factor: 0=this, 1=vtx
  47. void Lerp(float24 factor, const OutputVertex& vtx) {
  48. pos = pos * factor + vtx.pos * (float24::FromFloat32(1) - factor);
  49. // TODO: Should perform perspective correct interpolation here...
  50. tc0 = tc0 * factor + vtx.tc0 * (float24::FromFloat32(1) - factor);
  51. tc1 = tc1 * factor + vtx.tc1 * (float24::FromFloat32(1) - factor);
  52. tc2 = tc2 * factor + vtx.tc2 * (float24::FromFloat32(1) - factor);
  53. screenpos = screenpos * factor + vtx.screenpos * (float24::FromFloat32(1) - factor);
  54. color = color * factor + vtx.color * (float24::FromFloat32(1) - factor);
  55. }
  56. // Linear interpolation
  57. // factor: 0=v0, 1=v1
  58. static OutputVertex Lerp(float24 factor, const OutputVertex& v0, const OutputVertex& v1) {
  59. OutputVertex ret = v0;
  60. ret.Lerp(factor, v1);
  61. return ret;
  62. }
  63. };
  64. static_assert(std::is_pod<OutputVertex>::value, "Structure is not POD");
  65. static_assert(sizeof(OutputVertex) == 32 * sizeof(float), "OutputVertex has invalid size");
  66. struct OutputRegisters {
  67. OutputRegisters() = default;
  68. alignas(16) Math::Vec4<float24> value[16];
  69. OutputVertex ToVertex(const Regs::ShaderConfig& config);
  70. };
  71. static_assert(std::is_pod<OutputRegisters>::value, "Structure is not POD");
  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> temporary[16];
  222. } registers;
  223. static_assert(std::is_pod<Registers>::value, "Structure is not POD");
  224. OutputRegisters output_registers;
  225. bool conditional_code[2];
  226. // Two Address registers and one loop counter
  227. // TODO: How many bits do these actually have?
  228. s32 address_registers[3];
  229. DebugData<Debug> debug;
  230. static size_t InputOffset(const SourceRegister& reg) {
  231. switch (reg.GetRegisterType()) {
  232. case RegisterType::Input:
  233. return offsetof(UnitState, registers.input) + reg.GetIndex()*sizeof(Math::Vec4<float24>);
  234. case RegisterType::Temporary:
  235. return offsetof(UnitState, registers.temporary) + reg.GetIndex()*sizeof(Math::Vec4<float24>);
  236. default:
  237. UNREACHABLE();
  238. return 0;
  239. }
  240. }
  241. static size_t OutputOffset(const DestRegister& reg) {
  242. switch (reg.GetRegisterType()) {
  243. case RegisterType::Output:
  244. return offsetof(UnitState, output_registers.value) + reg.GetIndex()*sizeof(Math::Vec4<float24>);
  245. case RegisterType::Temporary:
  246. return offsetof(UnitState, registers.temporary) + reg.GetIndex()*sizeof(Math::Vec4<float24>);
  247. default:
  248. UNREACHABLE();
  249. return 0;
  250. }
  251. }
  252. };
  253. /// Clears the shader cache
  254. void ClearCache();
  255. struct ShaderSetup {
  256. struct {
  257. // The float uniforms are accessed by the shader JIT using SSE instructions, and are
  258. // therefore required to be 16-byte aligned.
  259. alignas(16) Math::Vec4<float24> f[96];
  260. std::array<bool, 16> b;
  261. std::array<Math::Vec4<u8>, 4> i;
  262. } uniforms;
  263. static size_t UniformOffset(RegisterType type, unsigned index) {
  264. switch (type) {
  265. case RegisterType::FloatUniform:
  266. return offsetof(ShaderSetup, uniforms.f) + index*sizeof(Math::Vec4<float24>);
  267. case RegisterType::BoolUniform:
  268. return offsetof(ShaderSetup, uniforms.b) + index*sizeof(bool);
  269. case RegisterType::IntUniform:
  270. return offsetof(ShaderSetup, uniforms.i) + index*sizeof(Math::Vec4<u8>);
  271. default:
  272. UNREACHABLE();
  273. return 0;
  274. }
  275. }
  276. std::array<u32, 1024> program_code;
  277. std::array<u32, 1024> swizzle_data;
  278. /**
  279. * Performs any shader unit setup that only needs to happen once per shader (as opposed to once per
  280. * vertex, which would happen within the `Run` function).
  281. */
  282. void Setup();
  283. /**
  284. * Runs the currently setup shader
  285. * @param state Shader unit state, must be setup per shader and per shader unit
  286. * @param input Input vertex into the shader
  287. * @param num_attributes The number of vertex shader attributes
  288. */
  289. void Run(UnitState<false>& state, const InputVertex& input, int num_attributes);
  290. /**
  291. * Produce debug information based on the given shader and input vertex
  292. * @param input Input vertex into the shader
  293. * @param num_attributes The number of vertex shader attributes
  294. * @param config Configuration object for the shader pipeline
  295. * @param setup Setup object for the shader pipeline
  296. * @return Debug information for this shader with regards to the given vertex
  297. */
  298. DebugData<true> ProduceDebugInfo(const InputVertex& input, int num_attributes, const Regs::ShaderConfig& config, const ShaderSetup& setup);
  299. };
  300. } // namespace Shader
  301. } // namespace Pica