shader.h 12 KB

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