gpu.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include <numeric>
  6. #include <type_traits>
  7. #include "common/color.h"
  8. #include "common/common_types.h"
  9. #include "common/logging/log.h"
  10. #include "common/microprofile.h"
  11. #include "common/vector_math.h"
  12. #include "core/settings.h"
  13. #include "core/memory.h"
  14. #include "core/core_timing.h"
  15. #include "core/hle/service/gsp_gpu.h"
  16. #include "core/hle/service/dsp_dsp.h"
  17. #include "core/hle/service/hid/hid.h"
  18. #include "core/hw/hw.h"
  19. #include "core/hw/gpu.h"
  20. #include "core/tracer/recorder.h"
  21. #include "video_core/command_processor.h"
  22. #include "video_core/hwrasterizer_base.h"
  23. #include "video_core/renderer_base.h"
  24. #include "video_core/utils.h"
  25. #include "video_core/video_core.h"
  26. #include "video_core/debug_utils/debug_utils.h"
  27. namespace GPU {
  28. Regs g_regs;
  29. /// True if the current frame was skipped
  30. bool g_skip_frame;
  31. /// 268MHz CPU clocks / 60Hz frames per second
  32. const u64 frame_ticks = 268123480ull / 60;
  33. /// Event id for CoreTiming
  34. static int vblank_event;
  35. /// Total number of frames drawn
  36. static u64 frame_count;
  37. /// True if the last frame was skipped
  38. static bool last_skip_frame;
  39. template <typename T>
  40. inline void Read(T &var, const u32 raw_addr) {
  41. u32 addr = raw_addr - HW::VADDR_GPU;
  42. u32 index = addr / 4;
  43. // Reads other than u32 are untested, so I'd rather have them abort than silently fail
  44. if (index >= Regs::NumIds() || !std::is_same<T, u32>::value) {
  45. LOG_ERROR(HW_GPU, "unknown Read%lu @ 0x%08X", sizeof(var) * 8, addr);
  46. return;
  47. }
  48. var = g_regs[addr / 4];
  49. }
  50. static Math::Vec4<u8> DecodePixel(Regs::PixelFormat input_format, const u8* src_pixel) {
  51. switch (input_format) {
  52. case Regs::PixelFormat::RGBA8:
  53. return Color::DecodeRGBA8(src_pixel);
  54. case Regs::PixelFormat::RGB8:
  55. return Color::DecodeRGB8(src_pixel);
  56. case Regs::PixelFormat::RGB565:
  57. return Color::DecodeRGB565(src_pixel);
  58. case Regs::PixelFormat::RGB5A1:
  59. return Color::DecodeRGB5A1(src_pixel);
  60. case Regs::PixelFormat::RGBA4:
  61. return Color::DecodeRGBA4(src_pixel);
  62. default:
  63. LOG_ERROR(HW_GPU, "Unknown source framebuffer format %x", input_format);
  64. return {0, 0, 0, 0};
  65. }
  66. }
  67. MICROPROFILE_DEFINE(GPU_DisplayTransfer, "GPU", "DisplayTransfer", MP_RGB(100, 100, 255));
  68. MICROPROFILE_DEFINE(GPU_CmdlistProcessing, "GPU", "Cmdlist Processing", MP_RGB(100, 255, 100));
  69. template <typename T>
  70. inline void Write(u32 addr, const T data) {
  71. addr -= HW::VADDR_GPU;
  72. u32 index = addr / 4;
  73. // Writes other than u32 are untested, so I'd rather have them abort than silently fail
  74. if (index >= Regs::NumIds() || !std::is_same<T, u32>::value) {
  75. LOG_ERROR(HW_GPU, "unknown Write%lu 0x%08X @ 0x%08X", sizeof(data) * 8, (u32)data, addr);
  76. return;
  77. }
  78. g_regs[index] = static_cast<u32>(data);
  79. switch (index) {
  80. // Memory fills are triggered once the fill value is written.
  81. case GPU_REG_INDEX_WORKAROUND(memory_fill_config[0].trigger, 0x00004 + 0x3):
  82. case GPU_REG_INDEX_WORKAROUND(memory_fill_config[1].trigger, 0x00008 + 0x3):
  83. {
  84. const bool is_second_filler = (index != GPU_REG_INDEX(memory_fill_config[0].trigger));
  85. auto& config = g_regs.memory_fill_config[is_second_filler];
  86. if (config.trigger) {
  87. if (config.address_start) { // Some games pass invalid values here
  88. u8* start = Memory::GetPhysicalPointer(config.GetStartAddress());
  89. u8* end = Memory::GetPhysicalPointer(config.GetEndAddress());
  90. if (config.fill_24bit) {
  91. // fill with 24-bit values
  92. for (u8* ptr = start; ptr < end; ptr += 3) {
  93. ptr[0] = config.value_24bit_r;
  94. ptr[1] = config.value_24bit_g;
  95. ptr[2] = config.value_24bit_b;
  96. }
  97. } else if (config.fill_32bit) {
  98. // fill with 32-bit values
  99. for (u32* ptr = (u32*)start; ptr < (u32*)end; ++ptr)
  100. *ptr = config.value_32bit;
  101. } else {
  102. // fill with 16-bit values
  103. for (u16* ptr = (u16*)start; ptr < (u16*)end; ++ptr)
  104. *ptr = config.value_16bit;
  105. }
  106. LOG_TRACE(HW_GPU, "MemoryFill from 0x%08x to 0x%08x", config.GetStartAddress(), config.GetEndAddress());
  107. if (!is_second_filler) {
  108. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC0);
  109. } else {
  110. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC1);
  111. }
  112. VideoCore::g_renderer->hw_rasterizer->NotifyFlush(config.GetStartAddress(), config.GetEndAddress() - config.GetStartAddress());
  113. }
  114. // Reset "trigger" flag and set the "finish" flag
  115. // NOTE: This was confirmed to happen on hardware even if "address_start" is zero.
  116. config.trigger = 0;
  117. config.finished = 1;
  118. }
  119. break;
  120. }
  121. case GPU_REG_INDEX(display_transfer_config.trigger):
  122. {
  123. MICROPROFILE_SCOPE(GPU_DisplayTransfer);
  124. const auto& config = g_regs.display_transfer_config;
  125. if (config.trigger & 1) {
  126. if (Pica::g_debug_context)
  127. Pica::g_debug_context->OnEvent(Pica::DebugContext::Event::IncomingDisplayTransfer, nullptr);
  128. u8* src_pointer = Memory::GetPhysicalPointer(config.GetPhysicalInputAddress());
  129. u8* dst_pointer = Memory::GetPhysicalPointer(config.GetPhysicalOutputAddress());
  130. if (config.is_texture_copy) {
  131. u32 input_width = config.texture_copy.input_width * 16;
  132. u32 input_gap = config.texture_copy.input_gap * 16;
  133. u32 output_width = config.texture_copy.output_width * 16;
  134. u32 output_gap = config.texture_copy.output_gap * 16;
  135. size_t contiguous_input_size = config.texture_copy.size / input_width * (input_width + input_gap);
  136. VideoCore::g_renderer->hw_rasterizer->NotifyPreRead(config.GetPhysicalInputAddress(), contiguous_input_size);
  137. u32 remaining_size = config.texture_copy.size;
  138. u32 remaining_input = input_width;
  139. u32 remaining_output = output_width;
  140. while (remaining_size > 0) {
  141. u32 copy_size = std::min({ remaining_input, remaining_output, remaining_size });
  142. std::memcpy(dst_pointer, src_pointer, copy_size);
  143. src_pointer += copy_size;
  144. dst_pointer += copy_size;
  145. remaining_input -= copy_size;
  146. remaining_output -= copy_size;
  147. remaining_size -= copy_size;
  148. if (remaining_input == 0) {
  149. remaining_input = input_width;
  150. src_pointer += input_gap;
  151. }
  152. if (remaining_output == 0) {
  153. remaining_output = output_width;
  154. dst_pointer += output_gap;
  155. }
  156. }
  157. LOG_TRACE(HW_GPU, "TextureCopy: 0x%X bytes from 0x%08X(%u+%u)-> 0x%08X(%u+%u), flags 0x%08X",
  158. config.texture_copy.size,
  159. config.GetPhysicalInputAddress(), input_width, input_gap,
  160. config.GetPhysicalOutputAddress(), output_width, output_gap,
  161. config.flags);
  162. size_t contiguous_output_size = config.texture_copy.size / output_width * (output_width + output_gap);
  163. VideoCore::g_renderer->hw_rasterizer->NotifyFlush(config.GetPhysicalOutputAddress(), contiguous_output_size);
  164. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PPF);
  165. break;
  166. }
  167. if (config.scaling > config.ScaleXY) {
  168. LOG_CRITICAL(HW_GPU, "Unimplemented display transfer scaling mode %u", config.scaling.Value());
  169. UNIMPLEMENTED();
  170. break;
  171. }
  172. if (config.input_linear && config.scaling != config.NoScale) {
  173. LOG_CRITICAL(HW_GPU, "Scaling is only implemented on tiled input");
  174. UNIMPLEMENTED();
  175. break;
  176. }
  177. bool horizontal_scale = config.scaling != config.NoScale;
  178. bool vertical_scale = config.scaling == config.ScaleXY;
  179. u32 output_width = config.output_width >> horizontal_scale;
  180. u32 output_height = config.output_height >> vertical_scale;
  181. u32 input_size = config.input_width * config.input_height * GPU::Regs::BytesPerPixel(config.input_format);
  182. u32 output_size = output_width * output_height * GPU::Regs::BytesPerPixel(config.output_format);
  183. VideoCore::g_renderer->hw_rasterizer->NotifyPreRead(config.GetPhysicalInputAddress(), input_size);
  184. for (u32 y = 0; y < output_height; ++y) {
  185. for (u32 x = 0; x < output_width; ++x) {
  186. Math::Vec4<u8> src_color;
  187. // Calculate the [x,y] position of the input image
  188. // based on the current output position and the scale
  189. u32 input_x = x << horizontal_scale;
  190. u32 input_y = y << vertical_scale;
  191. if (config.flip_vertically) {
  192. // Flip the y value of the output data,
  193. // we do this after calculating the [x,y] position of the input image
  194. // to account for the scaling options.
  195. y = output_height - y - 1;
  196. }
  197. u32 dst_bytes_per_pixel = GPU::Regs::BytesPerPixel(config.output_format);
  198. u32 src_bytes_per_pixel = GPU::Regs::BytesPerPixel(config.input_format);
  199. u32 src_offset;
  200. u32 dst_offset;
  201. if (config.input_linear) {
  202. if (!config.dont_swizzle) {
  203. // Interpret the input as linear and the output as tiled
  204. u32 coarse_y = y & ~7;
  205. u32 stride = output_width * dst_bytes_per_pixel;
  206. src_offset = (input_x + input_y * config.input_width) * src_bytes_per_pixel;
  207. dst_offset = VideoCore::GetMortonOffset(x, y, dst_bytes_per_pixel) + coarse_y * stride;
  208. } else {
  209. // Both input and output are linear
  210. src_offset = (input_x + input_y * config.input_width) * src_bytes_per_pixel;
  211. dst_offset = (x + y * output_width) * dst_bytes_per_pixel;
  212. }
  213. } else {
  214. if (!config.dont_swizzle) {
  215. // Interpret the input as tiled and the output as linear
  216. u32 coarse_y = input_y & ~7;
  217. u32 stride = config.input_width * src_bytes_per_pixel;
  218. src_offset = VideoCore::GetMortonOffset(input_x, input_y, src_bytes_per_pixel) + coarse_y * stride;
  219. dst_offset = (x + y * output_width) * dst_bytes_per_pixel;
  220. } else {
  221. // Both input and output are tiled
  222. u32 out_coarse_y = y & ~7;
  223. u32 out_stride = output_width * dst_bytes_per_pixel;
  224. u32 in_coarse_y = input_y & ~7;
  225. u32 in_stride = config.input_width * src_bytes_per_pixel;
  226. src_offset = VideoCore::GetMortonOffset(input_x, input_y, src_bytes_per_pixel) + in_coarse_y * in_stride;
  227. dst_offset = VideoCore::GetMortonOffset(x, y, dst_bytes_per_pixel) + out_coarse_y * out_stride;
  228. }
  229. }
  230. const u8* src_pixel = src_pointer + src_offset;
  231. src_color = DecodePixel(config.input_format, src_pixel);
  232. if (config.scaling == config.ScaleX) {
  233. Math::Vec4<u8> pixel = DecodePixel(config.input_format, src_pixel + src_bytes_per_pixel);
  234. src_color = ((src_color + pixel) / 2).Cast<u8>();
  235. } else if (config.scaling == config.ScaleXY) {
  236. Math::Vec4<u8> pixel1 = DecodePixel(config.input_format, src_pixel + 1 * src_bytes_per_pixel);
  237. Math::Vec4<u8> pixel2 = DecodePixel(config.input_format, src_pixel + 2 * src_bytes_per_pixel);
  238. Math::Vec4<u8> pixel3 = DecodePixel(config.input_format, src_pixel + 3 * src_bytes_per_pixel);
  239. src_color = (((src_color + pixel1) + (pixel2 + pixel3)) / 4).Cast<u8>();
  240. }
  241. u8* dst_pixel = dst_pointer + dst_offset;
  242. switch (config.output_format) {
  243. case Regs::PixelFormat::RGBA8:
  244. Color::EncodeRGBA8(src_color, dst_pixel);
  245. break;
  246. case Regs::PixelFormat::RGB8:
  247. Color::EncodeRGB8(src_color, dst_pixel);
  248. break;
  249. case Regs::PixelFormat::RGB565:
  250. Color::EncodeRGB565(src_color, dst_pixel);
  251. break;
  252. case Regs::PixelFormat::RGB5A1:
  253. Color::EncodeRGB5A1(src_color, dst_pixel);
  254. break;
  255. case Regs::PixelFormat::RGBA4:
  256. Color::EncodeRGBA4(src_color, dst_pixel);
  257. break;
  258. default:
  259. LOG_ERROR(HW_GPU, "Unknown destination framebuffer format %x", config.output_format.Value());
  260. break;
  261. }
  262. }
  263. }
  264. LOG_TRACE(HW_GPU, "DisplayTriggerTransfer: 0x%08x bytes from 0x%08x(%ux%u)-> 0x%08x(%ux%u), dst format %x, flags 0x%08X",
  265. config.output_height * output_width * GPU::Regs::BytesPerPixel(config.output_format),
  266. config.GetPhysicalInputAddress(), config.input_width.Value(), config.input_height.Value(),
  267. config.GetPhysicalOutputAddress(), output_width, output_height,
  268. config.output_format.Value(), config.flags);
  269. g_regs.display_transfer_config.trigger = 0;
  270. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PPF);
  271. VideoCore::g_renderer->hw_rasterizer->NotifyFlush(config.GetPhysicalOutputAddress(), output_size);
  272. }
  273. break;
  274. }
  275. // Seems like writing to this register triggers processing
  276. case GPU_REG_INDEX(command_processor_config.trigger):
  277. {
  278. const auto& config = g_regs.command_processor_config;
  279. if (config.trigger & 1)
  280. {
  281. MICROPROFILE_SCOPE(GPU_CmdlistProcessing);
  282. u32* buffer = (u32*)Memory::GetPhysicalPointer(config.GetPhysicalAddress());
  283. if (Pica::g_debug_context && Pica::g_debug_context->recorder) {
  284. Pica::g_debug_context->recorder->MemoryAccessed((u8*)buffer, config.size * sizeof(u32), config.GetPhysicalAddress());
  285. }
  286. Pica::CommandProcessor::ProcessCommandList(buffer, config.size);
  287. g_regs.command_processor_config.trigger = 0;
  288. }
  289. break;
  290. }
  291. default:
  292. break;
  293. }
  294. // Notify tracer about the register write
  295. // This is happening *after* handling the write to make sure we properly catch all memory reads.
  296. if (Pica::g_debug_context && Pica::g_debug_context->recorder) {
  297. // addr + GPU VBase - IO VBase + IO PBase
  298. Pica::g_debug_context->recorder->RegisterWritten<T>(addr + 0x1EF00000 - 0x1EC00000 + 0x10100000, data);
  299. }
  300. }
  301. // Explicitly instantiate template functions because we aren't defining this in the header:
  302. template void Read<u64>(u64 &var, const u32 addr);
  303. template void Read<u32>(u32 &var, const u32 addr);
  304. template void Read<u16>(u16 &var, const u32 addr);
  305. template void Read<u8>(u8 &var, const u32 addr);
  306. template void Write<u64>(u32 addr, const u64 data);
  307. template void Write<u32>(u32 addr, const u32 data);
  308. template void Write<u16>(u32 addr, const u16 data);
  309. template void Write<u8>(u32 addr, const u8 data);
  310. /// Update hardware
  311. static void VBlankCallback(u64 userdata, int cycles_late) {
  312. frame_count++;
  313. last_skip_frame = g_skip_frame;
  314. g_skip_frame = (frame_count & Settings::values.frame_skip) != 0;
  315. // Swap buffers based on the frameskip mode, which is a little bit tricky. When
  316. // a frame is being skipped, nothing is being rendered to the internal framebuffer(s).
  317. // So, we should only swap frames if the last frame was rendered. The rules are:
  318. // - If frameskip == 0 (disabled), always swap buffers
  319. // - If frameskip == 1, swap buffers every other frame (starting from the first frame)
  320. // - If frameskip > 1, swap buffers every frameskip^n frames (starting from the second frame)
  321. if ((((Settings::values.frame_skip != 1) ^ last_skip_frame) && last_skip_frame != g_skip_frame) ||
  322. Settings::values.frame_skip == 0) {
  323. VideoCore::g_renderer->SwapBuffers();
  324. }
  325. // Signal to GSP that GPU interrupt has occurred
  326. // TODO(yuriks): hwtest to determine if PDC0 is for the Top screen and PDC1 for the Sub
  327. // screen, or if both use the same interrupts and these two instead determine the
  328. // beginning and end of the VBlank period. If needed, split the interrupt firing into
  329. // two different intervals.
  330. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PDC0);
  331. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PDC1);
  332. // TODO(bunnei): Fake a DSP interrupt on each frame. This does not belong here, but
  333. // until we can emulate DSP interrupts, this is probably the only reasonable place to do
  334. // this. Certain games expect this to be periodically signaled.
  335. DSP_DSP::SignalInterrupt();
  336. // Check for user input updates
  337. Service::HID::Update();
  338. // Reschedule recurrent event
  339. CoreTiming::ScheduleEvent(frame_ticks - cycles_late, vblank_event);
  340. }
  341. /// Initialize hardware
  342. void Init() {
  343. memset(&g_regs, 0, sizeof(g_regs));
  344. auto& framebuffer_top = g_regs.framebuffer_config[0];
  345. auto& framebuffer_sub = g_regs.framebuffer_config[1];
  346. // Setup default framebuffer addresses (located in VRAM)
  347. // .. or at least these are the ones used by system applets.
  348. // There's probably a smarter way to come up with addresses
  349. // like this which does not require hardcoding.
  350. framebuffer_top.address_left1 = 0x181E6000;
  351. framebuffer_top.address_left2 = 0x1822C800;
  352. framebuffer_top.address_right1 = 0x18273000;
  353. framebuffer_top.address_right2 = 0x182B9800;
  354. framebuffer_sub.address_left1 = 0x1848F000;
  355. framebuffer_sub.address_left2 = 0x184C7800;
  356. framebuffer_top.width = 240;
  357. framebuffer_top.height = 400;
  358. framebuffer_top.stride = 3 * 240;
  359. framebuffer_top.color_format = Regs::PixelFormat::RGB8;
  360. framebuffer_top.active_fb = 0;
  361. framebuffer_sub.width = 240;
  362. framebuffer_sub.height = 320;
  363. framebuffer_sub.stride = 3 * 240;
  364. framebuffer_sub.color_format = Regs::PixelFormat::RGB8;
  365. framebuffer_sub.active_fb = 0;
  366. last_skip_frame = false;
  367. g_skip_frame = false;
  368. frame_count = 0;
  369. vblank_event = CoreTiming::RegisterEvent("GPU::VBlankCallback", VBlankCallback);
  370. CoreTiming::ScheduleEvent(frame_ticks, vblank_event);
  371. LOG_DEBUG(HW_GPU, "initialized OK");
  372. }
  373. /// Shutdown hardware
  374. void Shutdown() {
  375. LOG_DEBUG(HW_GPU, "shutdown OK");
  376. }
  377. } // namespace