fermi_2d.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // Copyright 2018 yuzu 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 "common/bit_field.h"
  8. #include "common/common_funcs.h"
  9. #include "common/common_types.h"
  10. #include "common/math_util.h"
  11. #include "video_core/engines/engine_interface.h"
  12. #include "video_core/gpu.h"
  13. namespace Tegra {
  14. class MemoryManager;
  15. }
  16. namespace VideoCore {
  17. class RasterizerInterface;
  18. }
  19. namespace Tegra::Engines {
  20. /**
  21. * This Engine is known as G80_2D. Documentation can be found in:
  22. * https://github.com/envytools/envytools/blob/master/rnndb/graph/g80_2d.xml
  23. * https://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/drivers/nouveau/nv50/nv50_2d.xml.h
  24. */
  25. #define FERMI2D_REG_INDEX(field_name) \
  26. (offsetof(Tegra::Engines::Fermi2D::Regs, field_name) / sizeof(u32))
  27. class Fermi2D final : public EngineInterface {
  28. public:
  29. explicit Fermi2D();
  30. ~Fermi2D();
  31. /// Binds a rasterizer to this engine.
  32. void BindRasterizer(VideoCore::RasterizerInterface& rasterizer);
  33. /// Write the value to the register identified by method.
  34. void CallMethod(u32 method, u32 method_argument, bool is_last_call) override;
  35. /// Write multiple values to the register identified by method.
  36. void CallMultiMethod(u32 method, const u32* base_start, u32 amount,
  37. u32 methods_pending) override;
  38. enum class Origin : u32 {
  39. Center = 0,
  40. Corner = 1,
  41. };
  42. enum class Filter : u32 {
  43. Point = 0,
  44. Bilinear = 1,
  45. };
  46. enum class Operation : u32 {
  47. SrcCopyAnd = 0,
  48. ROPAnd = 1,
  49. Blend = 2,
  50. SrcCopy = 3,
  51. ROP = 4,
  52. SrcCopyPremult = 5,
  53. BlendPremult = 6,
  54. };
  55. enum class MemoryLayout : u32 {
  56. BlockLinear = 0,
  57. Pitch = 1,
  58. };
  59. enum class CpuIndexWrap : u32 {
  60. Wrap = 0,
  61. NoWrap = 1,
  62. };
  63. struct Surface {
  64. RenderTargetFormat format;
  65. MemoryLayout linear;
  66. union {
  67. BitField<0, 4, u32> block_width;
  68. BitField<4, 4, u32> block_height;
  69. BitField<8, 4, u32> block_depth;
  70. };
  71. u32 depth;
  72. u32 layer;
  73. u32 pitch;
  74. u32 width;
  75. u32 height;
  76. u32 addr_upper;
  77. u32 addr_lower;
  78. [[nodiscard]] constexpr GPUVAddr Address() const noexcept {
  79. return (static_cast<GPUVAddr>(addr_upper) << 32) | static_cast<GPUVAddr>(addr_lower);
  80. }
  81. };
  82. static_assert(sizeof(Surface) == 0x28, "Surface has incorrect size");
  83. enum class SectorPromotion : u32 {
  84. NoPromotion = 0,
  85. PromoteTo2V = 1,
  86. PromoteTo2H = 2,
  87. PromoteTo4 = 3,
  88. };
  89. enum class NumTpcs : u32 {
  90. All = 0,
  91. One = 1,
  92. };
  93. enum class RenderEnableMode : u32 {
  94. False = 0,
  95. True = 1,
  96. Conditional = 2,
  97. RenderIfEqual = 3,
  98. RenderIfNotEqual = 4,
  99. };
  100. enum class ColorKeyFormat : u32 {
  101. A16R56G6B5 = 0,
  102. A1R5G55B5 = 1,
  103. A8R8G8B8 = 2,
  104. A2R10G10B10 = 3,
  105. Y8 = 4,
  106. Y16 = 5,
  107. Y32 = 6,
  108. };
  109. union Beta4 {
  110. BitField<0, 8, u32> b;
  111. BitField<8, 8, u32> g;
  112. BitField<16, 8, u32> r;
  113. BitField<24, 8, u32> a;
  114. };
  115. struct Point {
  116. u32 x;
  117. u32 y;
  118. };
  119. enum class PatternSelect : u32 {
  120. MonoChrome8x8 = 0,
  121. MonoChrome64x1 = 1,
  122. MonoChrome1x64 = 2,
  123. Color = 3,
  124. };
  125. enum class NotifyType : u32 {
  126. WriteOnly = 0,
  127. WriteThenAwaken = 1,
  128. };
  129. enum class MonochromePatternColorFormat : u32 {
  130. A8X8R8G6B5 = 0,
  131. A1R5G5B5 = 1,
  132. A8R8G8B8 = 2,
  133. A8Y8 = 3,
  134. A8X8Y16 = 4,
  135. Y32 = 5,
  136. };
  137. enum class MonochromePatternFormat : u32 {
  138. CGA6_M1 = 0,
  139. LE_M1 = 1,
  140. };
  141. union Regs {
  142. static constexpr std::size_t NUM_REGS = 0x258;
  143. struct {
  144. u32 object;
  145. INSERT_UNION_PADDING_WORDS(0x3F);
  146. u32 no_operation;
  147. NotifyType notify;
  148. INSERT_UNION_PADDING_WORDS(0x2);
  149. u32 wait_for_idle;
  150. INSERT_UNION_PADDING_WORDS(0xB);
  151. u32 pm_trigger;
  152. INSERT_UNION_PADDING_WORDS(0xF);
  153. u32 context_dma_notify;
  154. u32 dst_context_dma;
  155. u32 src_context_dma;
  156. u32 semaphore_context_dma;
  157. INSERT_UNION_PADDING_WORDS(0x1C);
  158. Surface dst;
  159. CpuIndexWrap pixels_from_cpu_index_wrap;
  160. u32 kind2d_check_enable;
  161. Surface src;
  162. SectorPromotion pixels_from_memory_sector_promotion;
  163. INSERT_UNION_PADDING_WORDS(0x1);
  164. NumTpcs num_tpcs;
  165. u32 render_enable_addr_upper;
  166. u32 render_enable_addr_lower;
  167. RenderEnableMode render_enable_mode;
  168. INSERT_UNION_PADDING_WORDS(0x4);
  169. u32 clip_x0;
  170. u32 clip_y0;
  171. u32 clip_width;
  172. u32 clip_height;
  173. BitField<0, 1, u32> clip_enable;
  174. BitField<0, 3, ColorKeyFormat> color_key_format;
  175. u32 color_key;
  176. BitField<0, 1, u32> color_key_enable;
  177. BitField<0, 8, u32> rop;
  178. u32 beta1;
  179. Beta4 beta4;
  180. Operation operation;
  181. union {
  182. BitField<0, 6, u32> x;
  183. BitField<8, 6, u32> y;
  184. } pattern_offset;
  185. BitField<0, 2, PatternSelect> pattern_select;
  186. INSERT_UNION_PADDING_WORDS(0xC);
  187. struct {
  188. BitField<0, 3, MonochromePatternColorFormat> color_format;
  189. BitField<0, 1, MonochromePatternFormat> format;
  190. u32 color0;
  191. u32 color1;
  192. u32 pattern0;
  193. u32 pattern1;
  194. } monochrome_pattern;
  195. struct {
  196. std::array<u32, 0x40> X8R8G8B8;
  197. std::array<u32, 0x20> R5G6B5;
  198. std::array<u32, 0x20> X1R5G5B5;
  199. std::array<u32, 0x10> Y8;
  200. } color_pattern;
  201. INSERT_UNION_PADDING_WORDS(0x10);
  202. struct {
  203. u32 prim_mode;
  204. u32 prim_color_format;
  205. u32 prim_color;
  206. u32 line_tie_break_bits;
  207. INSERT_UNION_PADDING_WORDS(0x14);
  208. u32 prim_point_xy;
  209. INSERT_UNION_PADDING_WORDS(0x7);
  210. std::array<Point, 0x40> prim_point;
  211. } render_solid;
  212. struct {
  213. u32 data_type;
  214. u32 color_format;
  215. u32 index_format;
  216. u32 mono_format;
  217. u32 wrap;
  218. u32 color0;
  219. u32 color1;
  220. u32 mono_opacity;
  221. INSERT_UNION_PADDING_WORDS(0x6);
  222. u32 src_width;
  223. u32 src_height;
  224. u32 dx_du_frac;
  225. u32 dx_du_int;
  226. u32 dx_dv_frac;
  227. u32 dy_dv_int;
  228. u32 dst_x0_frac;
  229. u32 dst_x0_int;
  230. u32 dst_y0_frac;
  231. u32 dst_y0_int;
  232. u32 data;
  233. } pixels_from_cpu;
  234. INSERT_UNION_PADDING_WORDS(0x3);
  235. u32 big_endian_control;
  236. INSERT_UNION_PADDING_WORDS(0x3);
  237. struct {
  238. BitField<0, 3, u32> block_shape;
  239. BitField<0, 5, u32> corral_size;
  240. BitField<0, 1, u32> safe_overlap;
  241. union {
  242. BitField<0, 1, Origin> origin;
  243. BitField<4, 1, Filter> filter;
  244. } sample_mode;
  245. INSERT_UNION_PADDING_WORDS(0x8);
  246. s32 dst_x0;
  247. s32 dst_y0;
  248. s32 dst_width;
  249. s32 dst_height;
  250. s64 du_dx;
  251. s64 dv_dy;
  252. s64 src_x0;
  253. s64 src_y0;
  254. } pixels_from_memory;
  255. };
  256. std::array<u32, NUM_REGS> reg_array;
  257. } regs{};
  258. struct Config {
  259. Operation operation;
  260. Filter filter;
  261. s32 dst_x0;
  262. s32 dst_y0;
  263. s32 dst_x1;
  264. s32 dst_y1;
  265. s32 src_x0;
  266. s32 src_y0;
  267. s32 src_x1;
  268. s32 src_y1;
  269. };
  270. private:
  271. VideoCore::RasterizerInterface* rasterizer;
  272. /// Performs the copy from the source surface to the destination surface as configured in the
  273. /// registers.
  274. void Blit();
  275. };
  276. #define ASSERT_REG_POSITION(field_name, position) \
  277. static_assert(offsetof(Fermi2D::Regs, field_name) == position, \
  278. "Field " #field_name " has invalid position")
  279. ASSERT_REG_POSITION(object, 0x0);
  280. ASSERT_REG_POSITION(no_operation, 0x100);
  281. ASSERT_REG_POSITION(notify, 0x104);
  282. ASSERT_REG_POSITION(wait_for_idle, 0x110);
  283. ASSERT_REG_POSITION(pm_trigger, 0x140);
  284. ASSERT_REG_POSITION(context_dma_notify, 0x180);
  285. ASSERT_REG_POSITION(dst_context_dma, 0x184);
  286. ASSERT_REG_POSITION(src_context_dma, 0x188);
  287. ASSERT_REG_POSITION(semaphore_context_dma, 0x18C);
  288. ASSERT_REG_POSITION(dst, 0x200);
  289. ASSERT_REG_POSITION(pixels_from_cpu_index_wrap, 0x228);
  290. ASSERT_REG_POSITION(kind2d_check_enable, 0x22C);
  291. ASSERT_REG_POSITION(src, 0x230);
  292. ASSERT_REG_POSITION(pixels_from_memory_sector_promotion, 0x258);
  293. ASSERT_REG_POSITION(num_tpcs, 0x260);
  294. ASSERT_REG_POSITION(render_enable_addr_upper, 0x264);
  295. ASSERT_REG_POSITION(render_enable_addr_lower, 0x268);
  296. ASSERT_REG_POSITION(clip_x0, 0x280);
  297. ASSERT_REG_POSITION(clip_y0, 0x284);
  298. ASSERT_REG_POSITION(clip_width, 0x288);
  299. ASSERT_REG_POSITION(clip_height, 0x28c);
  300. ASSERT_REG_POSITION(clip_enable, 0x290);
  301. ASSERT_REG_POSITION(color_key_format, 0x294);
  302. ASSERT_REG_POSITION(color_key, 0x298);
  303. ASSERT_REG_POSITION(rop, 0x2A0);
  304. ASSERT_REG_POSITION(beta1, 0x2A4);
  305. ASSERT_REG_POSITION(beta4, 0x2A8);
  306. ASSERT_REG_POSITION(operation, 0x2AC);
  307. ASSERT_REG_POSITION(pattern_offset, 0x2B0);
  308. ASSERT_REG_POSITION(pattern_select, 0x2B4);
  309. ASSERT_REG_POSITION(monochrome_pattern, 0x2E8);
  310. ASSERT_REG_POSITION(color_pattern, 0x300);
  311. ASSERT_REG_POSITION(render_solid, 0x580);
  312. ASSERT_REG_POSITION(pixels_from_cpu, 0x800);
  313. ASSERT_REG_POSITION(big_endian_control, 0x870);
  314. ASSERT_REG_POSITION(pixels_from_memory, 0x880);
  315. #undef ASSERT_REG_POSITION
  316. } // namespace Tegra::Engines