vertex_shader.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <initializer_list>
  6. #include <common/common_types.h>
  7. #include "math.h"
  8. #include "pica.h"
  9. namespace Pica {
  10. namespace VertexShader {
  11. struct InputVertex {
  12. Math::Vec4<float24> attr[16];
  13. };
  14. struct OutputVertex {
  15. OutputVertex() = default;
  16. // VS output attributes
  17. Math::Vec4<float24> pos;
  18. Math::Vec4<float24> dummy; // quaternions (not implemented, yet)
  19. Math::Vec4<float24> color;
  20. Math::Vec2<float24> tc0;
  21. // Padding for optimal alignment
  22. float24 pad[14];
  23. // Attributes used to store intermediate results
  24. // position after perspective divide
  25. Math::Vec3<float24> screenpos;
  26. float24 pad2;
  27. // Linear interpolation
  28. // factor: 0=this, 1=vtx
  29. void Lerp(float24 factor, const OutputVertex& vtx) {
  30. pos = pos * factor + vtx.pos * (float24::FromFloat32(1) - factor);
  31. // TODO: Should perform perspective correct interpolation here...
  32. tc0 = tc0 * factor + vtx.tc0 * (float24::FromFloat32(1) - factor);
  33. screenpos = screenpos * factor + vtx.screenpos * (float24::FromFloat32(1) - factor);
  34. color = color * factor + vtx.color * (float24::FromFloat32(1) - factor);
  35. }
  36. // Linear interpolation
  37. // factor: 0=v0, 1=v1
  38. static OutputVertex Lerp(float24 factor, const OutputVertex& v0, const OutputVertex& v1) {
  39. OutputVertex ret = v0;
  40. ret.Lerp(factor, v1);
  41. return ret;
  42. }
  43. };
  44. static_assert(std::is_pod<OutputVertex>::value, "Structure is not POD");
  45. static_assert(sizeof(OutputVertex) == 32 * sizeof(float), "OutputVertex has invalid size");
  46. union Instruction {
  47. enum class OpCode : u32 {
  48. ADD = 0x0,
  49. DP3 = 0x1,
  50. DP4 = 0x2,
  51. MUL = 0x8,
  52. MAX = 0xC,
  53. MIN = 0xD,
  54. RCP = 0xE,
  55. RSQ = 0xF,
  56. MOV = 0x13,
  57. RET = 0x21,
  58. FLS = 0x22, // Flush
  59. CALL = 0x24,
  60. };
  61. std::string GetOpCodeName() const {
  62. std::map<OpCode, std::string> map = {
  63. { OpCode::ADD, "ADD" },
  64. { OpCode::DP3, "DP3" },
  65. { OpCode::DP4, "DP4" },
  66. { OpCode::MUL, "MUL" },
  67. { OpCode::MAX, "MAX" },
  68. { OpCode::MIN, "MIN" },
  69. { OpCode::RCP, "RCP" },
  70. { OpCode::RSQ, "RSQ" },
  71. { OpCode::MOV, "MOV" },
  72. { OpCode::RET, "RET" },
  73. { OpCode::FLS, "FLS" },
  74. };
  75. auto it = map.find(opcode);
  76. if (it == map.end())
  77. return "UNK";
  78. else
  79. return it->second;
  80. }
  81. u32 hex;
  82. BitField<0x1a, 0x6, OpCode> opcode;
  83. // General notes:
  84. //
  85. // When two input registers are used, one of them uses a 5-bit index while the other
  86. // one uses a 7-bit index. This is because at most one floating point uniform may be used
  87. // as an input.
  88. // Format used e.g. by arithmetic instructions and comparisons
  89. // "src1" and "src2" specify register indices (i.e. indices referring to groups of 4 floats),
  90. // while "dest" addresses individual floats.
  91. union {
  92. BitField<0x00, 0x5, u32> operand_desc_id;
  93. template<class BitFieldType>
  94. struct SourceRegister : BitFieldType {
  95. enum RegisterType {
  96. Input,
  97. Temporary,
  98. FloatUniform
  99. };
  100. RegisterType GetRegisterType() const {
  101. if (BitFieldType::Value() < 0x10)
  102. return Input;
  103. else if (BitFieldType::Value() < 0x20)
  104. return Temporary;
  105. else
  106. return FloatUniform;
  107. }
  108. int GetIndex() const {
  109. if (GetRegisterType() == Input)
  110. return BitFieldType::Value();
  111. else if (GetRegisterType() == Temporary)
  112. return BitFieldType::Value() - 0x10;
  113. else if (GetRegisterType() == FloatUniform)
  114. return BitFieldType::Value() - 0x20;
  115. }
  116. std::string GetRegisterName() const {
  117. std::map<RegisterType, std::string> type = {
  118. { Input, "i" },
  119. { Temporary, "t" },
  120. { FloatUniform, "f" },
  121. };
  122. return type[GetRegisterType()] + std::to_string(GetIndex());
  123. }
  124. };
  125. SourceRegister<BitField<0x07, 0x5, u32>> src2;
  126. SourceRegister<BitField<0x0c, 0x7, u32>> src1;
  127. struct : BitField<0x15, 0x5, u32>
  128. {
  129. enum RegisterType {
  130. Output,
  131. Temporary,
  132. Unknown
  133. };
  134. RegisterType GetRegisterType() const {
  135. if (Value() < 0x8)
  136. return Output;
  137. else if (Value() < 0x10)
  138. return Unknown;
  139. else
  140. return Temporary;
  141. }
  142. int GetIndex() const {
  143. if (GetRegisterType() == Output)
  144. return Value();
  145. else if (GetRegisterType() == Temporary)
  146. return Value() - 0x10;
  147. else
  148. return Value();
  149. }
  150. std::string GetRegisterName() const {
  151. std::map<RegisterType, std::string> type = {
  152. { Output, "o" },
  153. { Temporary, "t" },
  154. { Unknown, "u" }
  155. };
  156. return type[GetRegisterType()] + std::to_string(GetIndex());
  157. }
  158. } dest;
  159. } common;
  160. // Format used for flow control instructions ("if")
  161. union {
  162. BitField<0x00, 0x8, u32> num_instructions;
  163. BitField<0x0a, 0xc, u32> offset_words;
  164. } flow_control;
  165. };
  166. static_assert(std::is_standard_layout<Instruction>::value, "Structure is not using standard layout!");
  167. union SwizzlePattern {
  168. u32 hex;
  169. enum class Selector : u32 {
  170. x = 0,
  171. y = 1,
  172. z = 2,
  173. w = 3
  174. };
  175. Selector GetSelectorSrc1(int comp) const {
  176. Selector selectors[] = {
  177. src1_selector_0, src1_selector_1, src1_selector_2, src1_selector_3
  178. };
  179. return selectors[comp];
  180. }
  181. Selector GetSelectorSrc2(int comp) const {
  182. Selector selectors[] = {
  183. src2_selector_0, src2_selector_1, src2_selector_2, src2_selector_3
  184. };
  185. return selectors[comp];
  186. }
  187. bool DestComponentEnabled(int i) const {
  188. return (dest_mask & (0x8 >> i)) != 0;
  189. }
  190. std::string SelectorToString(bool src2) const {
  191. std::map<Selector, std::string> map = {
  192. { Selector::x, "x" },
  193. { Selector::y, "y" },
  194. { Selector::z, "z" },
  195. { Selector::w, "w" }
  196. };
  197. std::string ret;
  198. for (int i = 0; i < 4; ++i) {
  199. ret += map.at(src2 ? GetSelectorSrc2(i) : GetSelectorSrc1(i));
  200. }
  201. return ret;
  202. }
  203. std::string DestMaskToString() const {
  204. std::string ret;
  205. for (int i = 0; i < 4; ++i) {
  206. if (!DestComponentEnabled(i))
  207. ret += "_";
  208. else
  209. ret += "xyzw"[i];
  210. }
  211. return ret;
  212. }
  213. // Components of "dest" that should be written to: LSB=dest.w, MSB=dest.x
  214. BitField< 0, 4, u32> dest_mask;
  215. BitField< 4, 1, u32> negate; // negates src1
  216. BitField< 5, 2, Selector> src1_selector_3;
  217. BitField< 7, 2, Selector> src1_selector_2;
  218. BitField< 9, 2, Selector> src1_selector_1;
  219. BitField<11, 2, Selector> src1_selector_0;
  220. BitField<14, 2, Selector> src2_selector_3;
  221. BitField<16, 2, Selector> src2_selector_2;
  222. BitField<18, 2, Selector> src2_selector_1;
  223. BitField<20, 2, Selector> src2_selector_0;
  224. BitField<31, 1, u32> flag; // not sure what this means, maybe it's the sign?
  225. };
  226. void SubmitShaderMemoryChange(u32 addr, u32 value);
  227. void SubmitSwizzleDataChange(u32 addr, u32 value);
  228. OutputVertex RunShader(const InputVertex& input, int num_attributes);
  229. Math::Vec4<float24>& GetFloatUniform(u32 index);
  230. } // namespace
  231. } // namespace