gpu.cpp 18 KB

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