regs_framebuffer.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright 2017 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 "common/bit_field.h"
  7. #include "common/common_funcs.h"
  8. #include "common/common_types.h"
  9. namespace Pica {
  10. struct FramebufferRegs {
  11. enum class LogicOp : u32 {
  12. Clear = 0,
  13. And = 1,
  14. AndReverse = 2,
  15. Copy = 3,
  16. Set = 4,
  17. CopyInverted = 5,
  18. NoOp = 6,
  19. Invert = 7,
  20. Nand = 8,
  21. Or = 9,
  22. Nor = 10,
  23. Xor = 11,
  24. Equiv = 12,
  25. AndInverted = 13,
  26. OrReverse = 14,
  27. OrInverted = 15,
  28. };
  29. enum class BlendEquation : u32 {
  30. Add = 0,
  31. Subtract = 1,
  32. ReverseSubtract = 2,
  33. Min = 3,
  34. Max = 4,
  35. };
  36. enum class BlendFactor : u32 {
  37. Zero = 0,
  38. One = 1,
  39. SourceColor = 2,
  40. OneMinusSourceColor = 3,
  41. DestColor = 4,
  42. OneMinusDestColor = 5,
  43. SourceAlpha = 6,
  44. OneMinusSourceAlpha = 7,
  45. DestAlpha = 8,
  46. OneMinusDestAlpha = 9,
  47. ConstantColor = 10,
  48. OneMinusConstantColor = 11,
  49. ConstantAlpha = 12,
  50. OneMinusConstantAlpha = 13,
  51. SourceAlphaSaturate = 14,
  52. };
  53. enum class CompareFunc : u32 {
  54. Never = 0,
  55. Always = 1,
  56. Equal = 2,
  57. NotEqual = 3,
  58. LessThan = 4,
  59. LessThanOrEqual = 5,
  60. GreaterThan = 6,
  61. GreaterThanOrEqual = 7,
  62. };
  63. enum class StencilAction : u32 {
  64. Keep = 0,
  65. Zero = 1,
  66. Replace = 2,
  67. Increment = 3,
  68. Decrement = 4,
  69. Invert = 5,
  70. IncrementWrap = 6,
  71. DecrementWrap = 7,
  72. };
  73. struct {
  74. union {
  75. // If false, logic blending is used
  76. BitField<8, 1, u32> alphablend_enable;
  77. };
  78. union {
  79. BitField<0, 8, BlendEquation> blend_equation_rgb;
  80. BitField<8, 8, BlendEquation> blend_equation_a;
  81. BitField<16, 4, BlendFactor> factor_source_rgb;
  82. BitField<20, 4, BlendFactor> factor_dest_rgb;
  83. BitField<24, 4, BlendFactor> factor_source_a;
  84. BitField<28, 4, BlendFactor> factor_dest_a;
  85. } alpha_blending;
  86. union {
  87. BitField<0, 4, LogicOp> logic_op;
  88. };
  89. union {
  90. u32 raw;
  91. BitField<0, 8, u32> r;
  92. BitField<8, 8, u32> g;
  93. BitField<16, 8, u32> b;
  94. BitField<24, 8, u32> a;
  95. } blend_const;
  96. union {
  97. BitField<0, 1, u32> enable;
  98. BitField<4, 3, CompareFunc> func;
  99. BitField<8, 8, u32> ref;
  100. } alpha_test;
  101. struct {
  102. union {
  103. // Raw value of this register
  104. u32 raw_func;
  105. // If true, enable stencil testing
  106. BitField<0, 1, u32> enable;
  107. // Comparison operation for stencil testing
  108. BitField<4, 3, CompareFunc> func;
  109. // Mask used to control writing to the stencil buffer
  110. BitField<8, 8, u32> write_mask;
  111. // Value to compare against for stencil testing
  112. BitField<16, 8, u32> reference_value;
  113. // Mask to apply on stencil test inputs
  114. BitField<24, 8, u32> input_mask;
  115. };
  116. union {
  117. // Raw value of this register
  118. u32 raw_op;
  119. // Action to perform when the stencil test fails
  120. BitField<0, 3, StencilAction> action_stencil_fail;
  121. // Action to perform when stencil testing passed but depth testing fails
  122. BitField<4, 3, StencilAction> action_depth_fail;
  123. // Action to perform when both stencil and depth testing pass
  124. BitField<8, 3, StencilAction> action_depth_pass;
  125. };
  126. } stencil_test;
  127. union {
  128. BitField<0, 1, u32> depth_test_enable;
  129. BitField<4, 3, CompareFunc> depth_test_func;
  130. BitField<8, 1, u32> red_enable;
  131. BitField<9, 1, u32> green_enable;
  132. BitField<10, 1, u32> blue_enable;
  133. BitField<11, 1, u32> alpha_enable;
  134. BitField<12, 1, u32> depth_write_enable;
  135. };
  136. INSERT_PADDING_WORDS(0x8);
  137. } output_merger;
  138. // Components are laid out in reverse byte order, most significant bits first.
  139. enum class ColorFormat : u32 {
  140. RGBA8 = 0,
  141. RGB8 = 1,
  142. RGB5A1 = 2,
  143. RGB565 = 3,
  144. RGBA4 = 4,
  145. };
  146. enum class DepthFormat : u32 {
  147. D16 = 0,
  148. D24 = 2,
  149. D24S8 = 3,
  150. };
  151. // Returns the number of bytes in the specified color format
  152. static unsigned BytesPerColorPixel(ColorFormat format) {
  153. switch (format) {
  154. case ColorFormat::RGBA8:
  155. return 4;
  156. case ColorFormat::RGB8:
  157. return 3;
  158. case ColorFormat::RGB5A1:
  159. case ColorFormat::RGB565:
  160. case ColorFormat::RGBA4:
  161. return 2;
  162. default:
  163. LOG_CRITICAL(HW_GPU, "Unknown color format %u", format);
  164. UNIMPLEMENTED();
  165. }
  166. }
  167. struct FramebufferConfig {
  168. INSERT_PADDING_WORDS(0x3);
  169. union {
  170. BitField<0, 4, u32> allow_color_write; // 0 = disable, else enable
  171. };
  172. INSERT_PADDING_WORDS(0x1);
  173. union {
  174. BitField<0, 2, u32> allow_depth_stencil_write; // 0 = disable, else enable
  175. };
  176. DepthFormat depth_format; // TODO: Should be a BitField!
  177. BitField<16, 3, ColorFormat> color_format;
  178. INSERT_PADDING_WORDS(0x4);
  179. u32 depth_buffer_address;
  180. u32 color_buffer_address;
  181. union {
  182. // Apparently, the framebuffer width is stored as expected,
  183. // while the height is stored as the actual height minus one.
  184. // Hence, don't access these fields directly but use the accessors
  185. // GetWidth() and GetHeight() instead.
  186. BitField<0, 11, u32> width;
  187. BitField<12, 10, u32> height;
  188. };
  189. INSERT_PADDING_WORDS(0x1);
  190. inline PAddr GetColorBufferPhysicalAddress() const {
  191. return color_buffer_address * 8;
  192. }
  193. inline PAddr GetDepthBufferPhysicalAddress() const {
  194. return depth_buffer_address * 8;
  195. }
  196. inline u32 GetWidth() const {
  197. return width;
  198. }
  199. inline u32 GetHeight() const {
  200. return height + 1;
  201. }
  202. } framebuffer;
  203. // Returns the number of bytes in the specified depth format
  204. static u32 BytesPerDepthPixel(DepthFormat format) {
  205. switch (format) {
  206. case DepthFormat::D16:
  207. return 2;
  208. case DepthFormat::D24:
  209. return 3;
  210. case DepthFormat::D24S8:
  211. return 4;
  212. default:
  213. LOG_CRITICAL(HW_GPU, "Unknown depth format %u", format);
  214. UNIMPLEMENTED();
  215. }
  216. }
  217. // Returns the number of bits per depth component of the specified depth format
  218. static u32 DepthBitsPerPixel(DepthFormat format) {
  219. switch (format) {
  220. case DepthFormat::D16:
  221. return 16;
  222. case DepthFormat::D24:
  223. case DepthFormat::D24S8:
  224. return 24;
  225. default:
  226. LOG_CRITICAL(HW_GPU, "Unknown depth format %u", format);
  227. UNIMPLEMENTED();
  228. }
  229. }
  230. INSERT_PADDING_WORDS(0x20);
  231. };
  232. static_assert(sizeof(FramebufferRegs) == 0x40 * sizeof(u32),
  233. "FramebufferRegs struct has incorrect size");
  234. } // namespace Pica