PixelEngine.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <mutex>
  5. #include "Common/BitField.h"
  6. #include "Common/CommonTypes.h"
  7. class PointerWrap;
  8. namespace Core
  9. {
  10. class System;
  11. }
  12. namespace CoreTiming
  13. {
  14. struct EventType;
  15. }
  16. namespace MMIO
  17. {
  18. class Mapping;
  19. }
  20. namespace PixelEngine
  21. {
  22. // internal hardware addresses
  23. enum
  24. {
  25. PE_ZCONF = 0x00, // Z Config
  26. PE_ALPHACONF = 0x02, // Alpha Config
  27. PE_DSTALPHACONF = 0x04, // Destination Alpha Config
  28. PE_ALPHAMODE = 0x06, // Alpha Mode Config
  29. PE_ALPHAREAD = 0x08, // Alpha Read
  30. PE_CTRL_REGISTER = 0x0a, // Control
  31. PE_TOKEN_REG = 0x0e, // Token
  32. PE_BBOX_LEFT = 0x10, // Bounding Box Left Pixel
  33. PE_BBOX_RIGHT = 0x12, // Bounding Box Right Pixel
  34. PE_BBOX_TOP = 0x14, // Bounding Box Top Pixel
  35. PE_BBOX_BOTTOM = 0x16, // Bounding Box Bottom Pixel
  36. // NOTE: Order not verified
  37. // These indicate the number of quads that are being used as input/output for each particular
  38. // stage
  39. PE_PERF_ZCOMP_INPUT_ZCOMPLOC_L = 0x18,
  40. PE_PERF_ZCOMP_INPUT_ZCOMPLOC_H = 0x1a,
  41. PE_PERF_ZCOMP_OUTPUT_ZCOMPLOC_L = 0x1c,
  42. PE_PERF_ZCOMP_OUTPUT_ZCOMPLOC_H = 0x1e,
  43. PE_PERF_ZCOMP_INPUT_L = 0x20,
  44. PE_PERF_ZCOMP_INPUT_H = 0x22,
  45. PE_PERF_ZCOMP_OUTPUT_L = 0x24,
  46. PE_PERF_ZCOMP_OUTPUT_H = 0x26,
  47. PE_PERF_BLEND_INPUT_L = 0x28,
  48. PE_PERF_BLEND_INPUT_H = 0x2a,
  49. PE_PERF_EFB_COPY_CLOCKS_L = 0x2c,
  50. PE_PERF_EFB_COPY_CLOCKS_H = 0x2e,
  51. };
  52. // ReadMode specifies the returned alpha channel for EFB peeks
  53. enum class AlphaReadMode : u16
  54. {
  55. Read00 = 0, // Always read 0x00
  56. ReadFF = 1, // Always read 0xFF
  57. ReadNone = 2, // Always read the real alpha value
  58. };
  59. // Note: These enums are (assumed to be) identical to the one in BPMemory, but the base type is set
  60. // to u16 instead of u32 for BitField
  61. enum class CompareMode : u16
  62. {
  63. Never = 0,
  64. Less = 1,
  65. Equal = 2,
  66. LEqual = 3,
  67. Greater = 4,
  68. NEqual = 5,
  69. GEqual = 6,
  70. Always = 7
  71. };
  72. union UPEZConfReg
  73. {
  74. u16 hex = 0;
  75. BitField<0, 1, bool, u16> z_comparator_enable;
  76. BitField<1, 3, CompareMode, u16> function;
  77. BitField<4, 1, bool, u16> z_update_enable;
  78. };
  79. enum class SrcBlendFactor : u16
  80. {
  81. Zero = 0,
  82. One = 1,
  83. DstClr = 2,
  84. InvDstClr = 3,
  85. SrcAlpha = 4,
  86. InvSrcAlpha = 5,
  87. DstAlpha = 6,
  88. InvDstAlpha = 7
  89. };
  90. enum class DstBlendFactor : u16
  91. {
  92. Zero = 0,
  93. One = 1,
  94. SrcClr = 2,
  95. InvSrcClr = 3,
  96. SrcAlpha = 4,
  97. InvSrcAlpha = 5,
  98. DstAlpha = 6,
  99. InvDstAlpha = 7
  100. };
  101. enum class LogicOp : u16
  102. {
  103. Clear = 0,
  104. And = 1,
  105. AndReverse = 2,
  106. Copy = 3,
  107. AndInverted = 4,
  108. NoOp = 5,
  109. Xor = 6,
  110. Or = 7,
  111. Nor = 8,
  112. Equiv = 9,
  113. Invert = 10,
  114. OrReverse = 11,
  115. CopyInverted = 12,
  116. OrInverted = 13,
  117. Nand = 14,
  118. Set = 15
  119. };
  120. union UPEAlphaConfReg
  121. {
  122. u16 hex = 0;
  123. BitField<0, 1, bool, u16> blend; // Set for GX_BM_BLEND or GX_BM_SUBTRACT
  124. BitField<1, 1, bool, u16> logic; // Set for GX_BM_LOGIC
  125. BitField<2, 1, bool, u16> dither;
  126. BitField<3, 1, bool, u16> color_update_enable;
  127. BitField<4, 1, bool, u16> alpha_update_enable;
  128. BitField<5, 3, DstBlendFactor, u16> dst_factor;
  129. BitField<8, 3, SrcBlendFactor, u16> src_factor;
  130. BitField<11, 1, bool, u16> subtract; // Set for GX_BM_SUBTRACT
  131. BitField<12, 4, LogicOp, u16> logic_op;
  132. };
  133. union UPEDstAlphaConfReg
  134. {
  135. u16 hex = 0;
  136. BitField<0, 8, u8, u16> alpha;
  137. BitField<8, 1, bool, u16> enable;
  138. };
  139. union UPEAlphaModeConfReg
  140. {
  141. u16 hex = 0;
  142. BitField<0, 8, u8, u16> threshold;
  143. // Yagcd and libogc use 8 bits for this, but the enum only needs 3
  144. BitField<8, 3, CompareMode, u16> compare_mode;
  145. };
  146. union UPEAlphaReadReg
  147. {
  148. u16 hex = 0;
  149. BitField<0, 2, AlphaReadMode, u16> read_mode;
  150. };
  151. // fifo Control Register
  152. union UPECtrlReg
  153. {
  154. u16 hex = 0;
  155. BitField<0, 1, bool, u16> pe_token_enable;
  156. BitField<1, 1, bool, u16> pe_finish_enable;
  157. BitField<2, 1, bool, u16> pe_token; // Write only
  158. BitField<3, 1, bool, u16> pe_finish; // Write only
  159. };
  160. class PixelEngineManager
  161. {
  162. public:
  163. explicit PixelEngineManager(Core::System& system);
  164. void Init();
  165. void DoState(PointerWrap& p);
  166. void RegisterMMIO(MMIO::Mapping* mmio, u32 base);
  167. // gfx backend support
  168. void SetToken(const u16 token, const bool interrupt, int cycle_delay);
  169. void SetFinish(int cycle_delay);
  170. AlphaReadMode GetAlphaReadMode() const { return m_alpha_read.read_mode; }
  171. private:
  172. void RaiseEvent(int cycles_into_future);
  173. void UpdateInterrupts();
  174. void SetTokenFinish_OnMainThread(u64 userdata, s64 cycles_late);
  175. static void SetTokenFinish_OnMainThread_Static(Core::System& system, u64 userdata,
  176. s64 cycles_late);
  177. // STATE_TO_SAVE
  178. UPEZConfReg m_z_conf;
  179. UPEAlphaConfReg m_alpha_conf;
  180. UPEDstAlphaConfReg m_dst_alpha_conf;
  181. UPEAlphaModeConfReg m_alpha_mode_conf;
  182. UPEAlphaReadReg m_alpha_read;
  183. UPECtrlReg m_control;
  184. std::mutex m_token_finish_mutex;
  185. u16 m_token = 0;
  186. u16 m_token_pending = 0;
  187. bool m_token_interrupt_pending = false;
  188. bool m_finish_interrupt_pending = false;
  189. bool m_event_raised = false;
  190. bool m_signal_token_interrupt = false;
  191. bool m_signal_finish_interrupt = false;
  192. CoreTiming::EventType* m_event_type_set_token_finish = nullptr;
  193. Core::System& m_system;
  194. };
  195. } // namespace PixelEngine